Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,14 @@ def runs():
# model-oriented for internal scheduling callers and enrich only this API
# representation.
values = []
for run in store.runs():
item = run.model_dump(mode="json")
for item in store.run_summaries():
responses = [
record.get("response")
for record in run.supervisor_results
for record in item.get("supervisor_results", [])
if isinstance(record, dict) and isinstance(record.get("response"), dict)
]
if not responses and isinstance(run.supervisor_response, dict):
responses = [run.supervisor_response]
if not responses and isinstance(item.get("supervisor_response"), dict):
responses = [item["supervisor_response"]]
improvements = [
improvement
for response in responses
Expand Down Expand Up @@ -574,6 +573,8 @@ class BuildCreate(BaseModel):
candidates_per_iteration: int = Field(default=2, ge=2, le=8)
approval_score: int = Field(ge=0, le=10)
require_human_approval_before_apply: bool = False
agent_apply_policy: Literal["review_required", "auto_commit_branch"] = "review_required"
agent_auto_commit_allowed_paths: list[str] = Field(default_factory=list, max_length=32)
executor_type: str = Field(
default="local", pattern=r"^(local|remote-http)$"
) # Legacy fallback; selected execution environment is authoritative.
Expand Down
55 changes: 55 additions & 0 deletions backend/app/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,8 @@ def builds(self) -> list[dict[str, Any]]:
# Existing builds predate this optional policy, so leave it off
# unless an operator explicitly enabled it.
build.setdefault("require_human_approval_before_apply", False)
build.setdefault("agent_apply_policy", "review_required")
build.setdefault("agent_auto_commit_allowed_paths", [])
build.setdefault("starred", False)
self._hydrate_build_environment(build)
build.update(self._repository_metadata(str(build.get("repository", ""))))
Expand Down Expand Up @@ -2283,6 +2285,8 @@ def create_build(self, values: dict[str, Any]) -> dict[str, Any]:
"require_human_approval_before_apply": bool(
values.get("require_human_approval_before_apply", False)
),
"agent_apply_policy": values.get("agent_apply_policy", "review_required"),
"agent_auto_commit_allowed_paths": list(values.get("agent_auto_commit_allowed_paths", [])),
"executor": executor,
}
builds = self.builds()
Expand Down Expand Up @@ -2377,6 +2381,8 @@ def update_build(self, build_id: str, values: dict[str, Any]) -> dict[str, Any]:
"require_human_approval_before_apply": bool(
values.get("require_human_approval_before_apply", False)
),
"agent_apply_policy": values.get("agent_apply_policy", "review_required"),
"agent_auto_commit_allowed_paths": list(values.get("agent_auto_commit_allowed_paths", [])),
"executor": executor,
}
builds[index] = build
Expand Down Expand Up @@ -3765,6 +3771,55 @@ def runs(self) -> list[Run]:
entries = [Run.model_validate_json(path.read_text(encoding="utf-8")) for path in RUNS.glob("*.json")]
return sorted(entries, key=lambda run: run.created_at, reverse=True)

def run_summaries(self) -> list[dict[str, Any]]:
"""Return the run-history fields without repeatedly loading full evidence.

The control room polls this collection. Browser snapshots, command
output, and saved artifacts belong to the detail endpoint; loading
every one of them for every poll can starve the local API and leave the
UI in its loading state.
"""
summaries: list[dict[str, Any]] = []
step_keys = {
"step_id",
"phase",
"loop_index",
"candidate_id",
"name",
"started_at",
"ended_at",
"in_progress",
"exit_code",
"error",
}
for path in RUNS.glob("*.json"):
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except (OSError, ValueError):
continue
if not isinstance(payload, dict):
continue
steps = payload.get("step_results", [])
compact_steps = []
for step in steps if isinstance(steps, list) else []:
if not isinstance(step, dict):
continue
compact = {key: value for key, value in step.items() if key in step_keys}
result = step.get("result")
simulator = result.get("insighta_persona_simulator") if isinstance(result, dict) else None
active = simulator.get("active_personas") if isinstance(simulator, dict) else None
if isinstance(active, list):
compact["result"] = {"insighta_persona_simulator": {"active_personas": active}}
compact_steps.append(compact)
payload["step_results"] = compact_steps
payload.pop("prompt_snapshot", None)
payload.pop("workflow_graph", None)
for record in payload.get("supervisor_results", []):
if isinstance(record, dict):
record.pop("prompt", None)
summaries.append(payload)
return sorted(summaries, key=lambda run: str(run.get("created_at") or ""), reverse=True)

def delete_run(self, run_id: str) -> None:
run = self._load(run_id)
if run.status in {"queued", "running", "awaiting_approval"}:
Expand Down
8 changes: 7 additions & 1 deletion backend/orbit_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import sys
from types import ModuleType

from .browser import NetworkSettleOptions, install_selenium_network_tracker, wait_for_selenium_page_settlement
from .browser import (
NetworkSettleOptions,
install_selenium_network_tracker,
selenium_http_failures,
wait_for_selenium_page_settlement,
)
from .core import context as _context
from .core.context import (
ORBIT_APP_DATA,
Expand Down Expand Up @@ -49,6 +54,7 @@ def __setattr__(self, name: str, value: object) -> None:
"canonical_phase",
"graph",
"install_selenium_network_tracker",
"selenium_http_failures",
"runner",
"wait_for_selenium_page_settlement",
]
40 changes: 40 additions & 0 deletions backend/orbit_sdk/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import time
from dataclasses import asdict, dataclass
from typing import Any, Iterable
from urllib.parse import urlsplit, urlunsplit

DEFAULT_IGNORED_URL_PATTERNS = (
r"analytics",
Expand Down Expand Up @@ -86,6 +87,45 @@ def install_selenium_network_tracker(
driver.execute_script(script)


def selenium_http_failures(driver: Any, *, limit: int = 20) -> list[dict[str, Any]]:
"""Return newly emitted failed HTTP responses from Chrome performance logs.

Query strings and fragments are deliberately removed: browser evidence must
identify a failing asset without persisting credentials or user data.
Callers enable Chrome's ``performance`` log independently; unsupported
drivers simply return no diagnostic entries.
"""
try:
entries = driver.get_log("performance")
except Exception:
return []

failures: list[dict[str, Any]] = []
for entry in entries if isinstance(entries, list) else []:
try:
message = json.loads(entry.get("message", "{}")).get("message", {})
if message.get("method") != "Network.responseReceived":
continue
params = message.get("params", {})
response = params.get("response", {})
status = int(response.get("status", 0))
url = str(response.get("url", ""))
if status < 400 or not url:
continue
parsed = urlsplit(url)
failures.append(
{
"status": status,
"url": urlunsplit((parsed.scheme, parsed.netloc, parsed.path, "", "")),
"mime_type": str(response.get("mimeType", "")),
"request_type": str(params.get("type", "")),
}
)
except (TypeError, ValueError, json.JSONDecodeError):
continue
return failures[-limit:]


def wait_for_selenium_page_settlement(
driver: Any,
*,
Expand Down
73 changes: 73 additions & 0 deletions backend/orbit_sdk/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import annotations

import base64
import fnmatch
import hashlib
import json
import os
Expand All @@ -29,6 +30,18 @@
PROJECT_ROOT = Path(os.environ.get("ORBIT_TARGET_REPOSITORY", Path.cwd())).resolve()
ORBIT_APP_DATA = Path(os.environ.get("ORBIT_APP_DATA", Path.home() / ".local" / "share" / "orbit")).resolve()

# These paths are never eligible for unattended agent commits, even when an
# operator accidentally includes them in an allow-list. They alter deployment,
# credentials, or persistent data and must remain review-only.
_AUTO_COMMIT_BLOCKED_PATHS = (
".github/**",
"infra/**",
"migrations/**",
"**/migrations/**",
"*.pem",
"*.key",
)


def ORBIT_PROJECT_PATH(*parts: str) -> Path:
"""Resolve a safe path relative to the evaluation target repository.
Expand Down Expand Up @@ -1701,6 +1714,41 @@ def register_evaluation(
self.emit_result({"evaluation_request": request})
return request

def _auto_commit_policy_result(self, changed_files: list[str], feedback: str) -> dict[str, object]:
"""Return whether this isolated proposal may receive an unattended commit.

This is deliberately an allow-list gate rather than a convenience
switch: the policy does not push or merge and sensitive repository
paths cannot be opted in.
"""
build = self.build
if str(build.get("agent_apply_policy", "review_required")) != "auto_commit_branch":
return {"status": "not_requested"}
if not feedback or not changed_files:
return {"status": "skipped", "reason": "proposal_has_no_feedback_or_changes"}
allowed = [
str(pattern).strip()
for pattern in build.get("agent_auto_commit_allowed_paths", [])
if str(pattern).strip()
]
if not allowed:
return {"status": "skipped", "reason": "allow_list_is_empty"}
blocked = [
path
for path in changed_files
if any(fnmatch.fnmatchcase(path, pattern) for pattern in _AUTO_COMMIT_BLOCKED_PATHS)
]
if blocked:
return {"status": "skipped", "reason": "blocked_path", "paths": blocked}
outside_allow_list = [
path
for path in changed_files
if not any(fnmatch.fnmatchcase(path, pattern) for pattern in allowed)
]
if outside_allow_list:
return {"status": "skipped", "reason": "path_not_allow_listed", "paths": outside_allow_list}
return {"status": "eligible"}

def run_ai_agent(
self,
prompt: str,
Expand All @@ -1714,6 +1762,11 @@ def run_ai_agent(
The agent must print ``ORBIT_AGENT_FEEDBACK: <summary>`` as its final
feedback line to opt its work into evaluation. This keeps a silent or
no-op agent run from creating a score for the iteration.

When the Build's ``agent_apply_policy`` is ``auto_commit_branch``, a
proposal that stays within its explicit allow-list is committed only to
its isolated proposal branch. It is never pushed, merged, or applied to
the evaluated repository.
"""
normalized_prompt = prompt.strip()
if not normalized_prompt:
Expand All @@ -1738,6 +1791,8 @@ def run_ai_agent(
output = ""
changed_files: list[str] = []
patch = ""
feedback = ""
auto_commit: dict[str, object] = {"status": "not_requested"}
keep_worktree = False
try:
output = self.exec(
Expand All @@ -1746,6 +1801,9 @@ def run_ai_agent(
timeout=timeout,
target_log_source="ai-agent",
)
agent_head = self.exec(["git", "rev-parse", "HEAD"], cwd=worktree).strip()
if agent_head and agent_head != base_revision:
raise RuntimeError("AI agent proposals must not create commits")
# Include newly-created files in the review patch without creating
# a commit. Intent-to-add is local to this disposable proposal
# worktree and is finalized only after Issue approval.
Expand All @@ -1765,6 +1823,20 @@ def run_ai_agent(
),
"",
)
auto_commit = self._auto_commit_policy_result(changed_files, feedback)
if auto_commit["status"] == "eligible":
self.exec(["git", "add", "--all"], cwd=worktree)
self.exec(
["git", "commit", "-m", f"orbit: autonomous proposal for {run_id}"],
cwd=worktree,
target_log_source="ai-agent",
)
auto_commit = {
"status": "committed",
"commit": self.exec(["git", "rev-parse", "HEAD"], cwd=worktree).strip(),
"branch": branch,
"allowed_paths": list(self.build.get("agent_auto_commit_allowed_paths", [])),
}
keep_worktree = bool(feedback and patch)
finally:
if not keep_worktree:
Expand Down Expand Up @@ -1802,6 +1874,7 @@ def run_ai_agent(
"diff": patch,
"fingerprint": _sha256(patch.encode("utf-8")) if patch else "",
"diff_artifact": artifact,
"auto_commit": auto_commit,
},
}
self.emit_result({"agent_run": result})
Expand Down
Loading
Loading