From 8a605ed6a44071128aeb0ba41ba09d950ad54932 Mon Sep 17 00:00:00 2001 From: FeiZhuLulu <279467036+FeiZhuLulu@users.noreply.github.com> Date: Sun, 4 Oct 2026 14:41:29 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E9=81=BF=E5=85=8D=20MCP=20=E7=A9=BA?= =?UTF-8?q?=E9=97=B2=E6=96=AD=E8=BF=9E=E5=B9=B6=E4=BF=9D=E7=95=99=20ACP=20?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E8=AF=8A=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ORCHESTRATION.md | 4 +- ORCHESTRATION.zh-CN.md | 2 + README.md | 4 + SETUP.md | 16 ++- agents.toml | 4 +- agents.toml.example | 4 +- skills/agent-bridge/SKILL.md | 2 + src/agent_bridge/adapters/acp.py | 233 +++++++++++++++++++++---------- src/agent_bridge/agents.toml | 4 +- src/agent_bridge/config.py | 4 +- src/agent_bridge/models.py | 9 ++ src/agent_bridge/registry.py | 12 +- src/agent_bridge/server.py | 2 +- tests/cursor_agent.py | 1 + tests/echo_agent.py | 24 ++++ tests/test_acp_echo.py | 101 ++++++++++++++ tests/test_acp_paths.py | 97 +++++++++++++ tests/test_config.py | 5 +- tests/test_cursor_acp.py | 31 +++- tests/test_kimi_acp.py | 19 ++- tests/test_registry.py | 16 +++ tests/test_server_tools.py | 33 +++++ 22 files changed, 532 insertions(+), 95 deletions(-) diff --git a/ORCHESTRATION.md b/ORCHESTRATION.md index 0f901b7..6af5386 100644 --- a/ORCHESTRATION.md +++ b/ORCHESTRATION.md @@ -75,4 +75,6 @@ In `auto`/`eager`, tell the user after the fact. In `manual`, their explicit req Do not drive worker GUIs or CLIs. Session resume is Bridge's job. -For optional retry deduplication, generate a UUID `request_id` before the first `dispatch_task` call and include it on that call. Retry with the same ID and original arguments; if `session_id` was omitted, keep it omitted. Adding an ID only on retry cannot deduplicate the first call. Identical arguments reuse the task (`reused=true`); different arguments are rejected. Deduplication lasts only in the same Bridge instance while the task is retained. Normal dispatch validation still applies. Restarting Bridge, switching instances, or pruning the task loses the binding; worker side effects are not exactly-once. +ACP `failure` supplements string `error` (fields: SETUP.md). Missing evidence or `failure.prompt_may_have_been_sent=true` forbids blind replay; false does not prove retry safety. Bridge never auto-retries prompts. + +For deduplication, send UUID `request_id` on the first dispatch; reuse its ID and exact arguments, including omitted `session_id`, on retry. Adding an ID only on retry cannot deduplicate earlier calls. Identical calls reuse the task (`reused=true`); changed arguments fail. Normal validation applies. Bindings last only in this instance until task pruning or restart; worker effects are not exactly-once. diff --git a/ORCHESTRATION.zh-CN.md b/ORCHESTRATION.zh-CN.md index 467b51d..6b9d491 100644 --- a/ORCHESTRATION.zh-CN.md +++ b/ORCHESTRATION.zh-CN.md @@ -79,4 +79,6 @@ Claude 的 `quota.status` 只汇总通用的 `5h` / `weekly` 额度。派发前 不要去操作 Worker 的图形界面。已经打开的 Grok TUI 不会跟着 ACP 回合实时刷新;用户重启 Grok Build 即可看到同一会话。会话续上是 Bridge 的事。 +ACP 失败保留字符串 `error`,并可附带 `failure`。`failure.prompt_may_have_been_sent=true` 或没有诊断证据时,不要盲目重发;false 也不保证初始化没有副作用或重试安全。Bridge 不自动重试 prompt。诊断字段详见 SETUP.md。 + 需要重试去重时,在首次调用 `dispatch_task` 前生成 UUID `request_id`,首次调用就传入。重试须使用同一 ID 和原始参数;首次省略了 `session_id`,重试也保持省略。只在重试时补 ID 无法对首次调用去重。相同参数复用原任务(`reused=true`),不同参数直接拒绝。去重只在同一 Bridge 实例内、原任务仍保留时有效,仍须通过正常派发校验。重启 Bridge、切换实例或清理原任务后不再保留绑定;Worker 的外部副作用不保证 exactly-once。 diff --git a/README.md b/README.md index bba0d80..a419687 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,8 @@ lose that last window. A turn whose worker stays silent past `stall_timeout_sec` (default 1800 s, per worker) ends `failed` / `stalled`; `check_task` shows `silent_for_sec`. +ACP failures retain the string `error` and add optional `failure` diagnostics: the operation, category, observed exit code, prompt delivery uncertainty and a safe stderr summary. Missing diagnostics do not imply a safe retry; Bridge never automatically replays failed prompts. See [failure diagnostics](SETUP.md#acp-failure-diagnostics). + ### Remaining quota Each `list_agents` row carries `quota`: `status` ok / exhausted / unknown, the @@ -275,6 +277,8 @@ revivable = true Worker 静默超过 `stall_timeout_sec`(默认 1800 秒,可按 Worker 设置)的一轮会以 `failed` / `stalled` 结束;`check_task` 会给出 `silent_for_sec`。 +ACP 失败保留字符串 `error`,新增可选 `failure` 诊断:失败操作、类别、已观察到的退出码、prompt 是否可能已发送以及安全的 stderr 摘要。没有诊断不等于可以安全重试;Bridge 不自动重发失败的 prompt。详见[失败诊断](SETUP.md#acp-failure-diagnostics)。 + ### 剩余额度 `list_agents` 每一行都带 `quota`:`status` 为 ok / exhausted / unknown,各个滚动窗口的 diff --git a/SETUP.md b/SETUP.md index bb8cf5a..eda3319 100644 --- a/SETUP.md +++ b/SETUP.md @@ -331,7 +331,21 @@ Instances share the `~/.agent-bridge` state directory but not sessions: every se ## Server lifecycle -Abandoned server instances self-exit: after `server.idle_exit_sec` (default 7200 s) with no MCP requests and no queued or running tasks, the process shuts its workers down and exits. Configure in `[server]` (repo `agents.toml` or `%USERPROFILE%\.agent-bridge\agents.toml`); `idle_exit_sec = 0` disables it. `list_agents` also warns when other Bridge instances are running on this machine — one per coordinator host is normal, a pile-up means a host keeps abandoning spawns. +Bridge stays available while its host keeps the stdio connection open. `server.idle_exit_sec` defaults to `0` (disabled); EOF ends the connection and runs the normal worker and state cleanup. Worker idle unloading remains controlled separately by each agent's `idle_unload_sec`. + +A positive `idle_exit_sec` opts into server shutdown after that many seconds with no MCP tool calls and no queued or running tasks. Configure it in `[server]` (repo `agents.toml` or `%USERPROFILE%\.agent-bridge\agents.toml`) only when the host can reliably reconnect; explicit existing values remain effective. Idle time cannot distinguish an abandoned thread from one the user will continue later. `list_agents` reports other Bridge instances, but multiple coordinator threads may legitimately own separate instances. + +## ACP failure diagnostics + +`wait_task`, `check_task`, and `get_result` retain the string `error` and include an optional `failure` object for ACP failures: + +- `operation`: the failed operation, such as `process_start`, `initialize`, `model_discovery`, `session/new`, `session/load`, `session/resume`, `session/configure`, `session/set_config_option model`, or `session/prompt`. +- `kind`: `auth_required`, `unsupported_model`, `timeout`, `connection_closed`, `worker_exit`, `protocol_error`, or `unknown`. Classification uses protocol codes and observed facts, not guesses from stderr. +- `exit_code`: the observed exit code of the failing process, or `null`. During model discovery this belongs to the discovery command. An exit caused by Bridge's timeout cleanup is not substituted for the original failure. +- `prompt_may_have_been_sent`: `false` only when this turn has not entered the prompt send operation; `true` means delivery or execution may already have occurred, not that delivery was acknowledged. +- `stderr_summary`: recognized generic diagnostic phrases only, at most 2048 characters, or `null`. Raw stderr, arbitrary lines, credentials, URLs and local paths are not included. The human-readable `error` is also bounded and common credential fields are redacted. + +`failure = null` means no structured evidence is available (including old records, other adapters and interrupted tasks restored after a Bridge restart), not that retrying is safe. Bridge does not automatically replay failed prompts. Even `false` does not prove initialization was side-effect-free. An uncertain dispatch response can be retried with its original `request_id`; replaying that ID retrieves the same retained task, not a fresh execution of a failed task. `available` still describes command resolution, not authentication or session health. ## Remaining quota in `list_agents` diff --git a/agents.toml b/agents.toml index e8702a4..e6c1616 100644 --- a/agents.toml +++ b/agents.toml @@ -110,8 +110,8 @@ discover_proxy = true # no_proxy = "localhost,127.0.0.1,::1" [server] -# Self-exit for abandoned MCP server instances whose host never closed stdin. -idle_exit_sec = 7200 +# Optional idle self-exit; enable only when the host can reconnect reliably. +idle_exit_sec = 0 [coordinator] # How eagerly the coordinator should dispatch to workers. diff --git a/agents.toml.example b/agents.toml.example index 27d9eff..159ac61 100644 --- a/agents.toml.example +++ b/agents.toml.example @@ -7,8 +7,8 @@ # url = "http://127.0.0.1:7897" # no_proxy = "localhost,127.0.0.1,::1" -# Self-exit for abandoned MCP server instances whose host never closed stdin. -# 7200 is already the default; set 0 to disable. +# Optional idle self-exit; enable only when the host can reconnect reliably. +# Disabled by default (0); a positive value opts into idle server shutdown. # [server] # idle_exit_sec = 7200 diff --git a/skills/agent-bridge/SKILL.md b/skills/agent-bridge/SKILL.md index 3d817ff..d31b06f 100644 --- a/skills/agent-bridge/SKILL.md +++ b/skills/agent-bridge/SKILL.md @@ -56,4 +56,6 @@ Examples: "fix the README typo" → yourself. "Add a None check at line 120" → 4. If review fails, `dispatch_task` again on the same `session_id` with a concrete problem list — at most three follow-ups, then fix it yourself and tell the user. 5. Summarize the diff, leftover risk, and worker usage. `end_session` when the worker is no longer needed. +ACP failures retain string `error` plus optional `failure`. If `failure.prompt_may_have_been_sent` is true or evidence is missing, do not blindly replay; false is not proof of safe retry. Bridge never auto-retries prompts. See SETUP.md for diagnostic fields. + For optional retry deduplication, send a UUID `request_id` on the first `dispatch_task` call, then retry with the same ID and original arguments; keep `session_id` omitted if it was originally omitted. Adding an ID only on retry cannot deduplicate the first call. Identical retries reuse the task (`reused=true`) only within the same Bridge instance while the task is retained. Different arguments are rejected, and normal dispatch validation still applies. Restart, another instance, or task pruning loses the binding; this does not guarantee exactly-once worker side effects. diff --git a/src/agent_bridge/adapters/acp.py b/src/agent_bridge/adapters/acp.py index 59e8f07..04e1bff 100644 --- a/src/agent_bridge/adapters/acp.py +++ b/src/agent_bridge/adapters/acp.py @@ -12,6 +12,7 @@ from typing import Any, NoReturn from acp import PROTOCOL_VERSION, connect_to_agent, text_block +from acp.exceptions import RequestError from acp.schema import ( AllowedOutcome, ClientCapabilities, @@ -38,7 +39,7 @@ resolve_minimax_effort, resolve_minimax_model, ) -from agent_bridge.models import Session, Task, TurnResult, dsh_effort, grok_effort +from agent_bridge.models import Session, Task, TaskFailure, TurnResult, dsh_effort, grok_effort from agent_bridge.opencode_meta import resolve_opencode_effort from agent_bridge.processes import ( drop_pid, @@ -94,7 +95,40 @@ def _tool_io_summary(value: Any) -> str | None: return text if len(text) <= _TOOL_IO_LIMIT else text[:_TOOL_IO_LIMIT] + "…" -class RpcTimeoutError(RuntimeError): +def _redact_diagnostic(text: str) -> str: + text = re.sub(r"\x1b\[[0-?]*[ -/]*[@-~]", "", text) + text = re.sub(r"(?im)^.*(?:authorization|(?:set-)?cookie)\s*[:=].*$", "[redacted]", text) + text = re.sub( + r"""(?i)(\b(?:[\w-]*(?:token|secret|password|api[_-]?key)|authorization|cookie)\b["']?\s*[:=]\s*)(?:"[^"]*"|'[^']*'|[^\s,;]+)""", + r"\1[redacted]", text, + ) + text = re.sub(r"(?i)\b(?:bearer|basic)\s+\S+", "[redacted]", text) + text = re.sub(r"""(?i)\b(?:https?|wss?)://[^\s<>"']+""", "[redacted URL]", text) + text = re.sub(r"\b(?:sk-[\w-]+|gh[pousr]_\w+)\b", "[redacted]", text) + return text[:2048] + + +def _stderr_summary(text: str) -> str | None: + hints = re.findall( + r"\b(?:authentication required|not logged in|unauthorized|permission denied|" + r"unsupported model|invalid model|model not found|connection (?:closed|refused|reset)|" + r"timed out|timeout|quota exceeded|rate limit exceeded)\b", + text, re.IGNORECASE, + ) + return "; ".join(dict.fromkeys(hint.lower() for hint in hints))[:2048] or None + + +class AcpError(RuntimeError): + def __init__(self, message: str, failure: TaskFailure) -> None: + super().__init__(_redact_diagnostic(message)) + self.failure = failure + + +class AcpUnavailableError(AcpError): + pass + + +class RpcTimeoutError(AcpUnavailableError): pass @@ -673,6 +707,42 @@ async def _drain_stderr( except (ValueError, OSError): log.warning("stderr drain aborted for %s", session_id, exc_info=True) + def _failure( + self, exc: Exception, operation: str, session: Session | None = None, *, + proc: asyncio.subprocess.Process | None = None, stderr: str | None = None, + prompt_may_have_been_sent: bool = False, kind: str | None = None, + ) -> AcpError: + if isinstance(exc, AcpError): + return exc + if isinstance(exc.__cause__, AcpError): + return type(exc.__cause__)(str(exc), exc.__cause__.failure) + live = self._live.get(session.session_id) if session else None + proc = proc if proc is not None else live.proc if live else None + exit_code = proc.returncode if proc is not None else None + if kind is None: + if isinstance(exc, TimeoutError): + kind = "timeout" + elif isinstance(exc, RequestError) and exc.code == -32000: + kind = "auth_required" + elif isinstance(exc, RequestError): + kind = "protocol_error" + elif exit_code is not None: + kind = "worker_exit" + elif isinstance(exc, (ConnectionError, EOFError)): + kind = "connection_closed" + else: + kind = "unknown" + failure = TaskFailure( + operation=operation, kind=kind, exit_code=exit_code, + prompt_may_have_been_sent=prompt_may_have_been_sent, + stderr_summary=_stderr_summary(stderr if stderr is not None else live.stderr_tail if live else ""), + ) + error_type = ( + RpcTimeoutError if kind == "timeout" else + AcpUnavailableError if kind in {"auth_required", "connection_closed", "worker_exit"} else AcpError + ) + return error_type(str(exc) or type(exc).__name__, failure) + async def _rpc( self, coro: Any, @@ -683,20 +753,18 @@ async def _rpc( """Await a handshake RPC with a timeout; kill the worker on hang.""" try: return await asyncio.wait_for(coro, timeout=timeout) - except TimeoutError: - live = self._live.get(session.session_id) - log.error( - "%s %s timed out after %ss for %s; killing worker stderr_tail=%r", - self.agent.name, - what, - timeout, - session.session_id, - live.stderr_tail if live else "", - ) - await self.shutdown(session) - raise RpcTimeoutError( - f"{self.agent.name} {what} timed out after {int(timeout)}s" - ) from None + except Exception as exc: + if isinstance(exc, TimeoutError): + exc = TimeoutError(f"{self.agent.name} {what} timed out after {int(timeout)}s") + error = self._failure(exc, what, session) + if ( + isinstance(error, RpcTimeoutError) + or what in {"initialize", "session/new"} + or (what in {"session/load", "session/resume"} and isinstance(error, AcpUnavailableError)) + ): + log.error("%s; stderr_summary=%r", error, error.failure.stderr_summary) + await self.shutdown(session) + raise error from None async def _cursor_models( self, @@ -706,14 +774,17 @@ async def _cursor_models( ) -> dict[str, str]: if self._cursor_models_cache is not None: return self._cursor_models_cache - model_cmd = cursor_list_models_command(command) - proc = await asyncio.create_subprocess_exec( - *model_cmd, - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE, - env=env, - cwd=cwd, - ) + try: + model_cmd = cursor_list_models_command(command) + proc = await asyncio.create_subprocess_exec( + *model_cmd, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + env=env, + cwd=cwd, + ) + except Exception as exc: + raise self._failure(exc, "model_discovery") from None try: stdout, stderr = await asyncio.wait_for( proc.communicate(), timeout=CURSOR_MODEL_LIST_TIMEOUT_SEC @@ -722,20 +793,23 @@ async def _cursor_models( await reap_subprocess(proc) raise except TimeoutError: + error = self._failure( + TimeoutError("cursor model discovery timed out; run 'cursor-agent --list-models' to check the current account"), + "model_discovery", proc=proc, + ) await reap_subprocess(proc) - raise RuntimeError( - "cursor model discovery timed out; run 'cursor-agent --list-models' " - "to check the current account" - ) from None + raise error from None if proc.returncode: - detail = stderr.decode("utf-8", errors="replace").strip() - suffix = f": {detail[-1000:]}" if detail else "" - raise RuntimeError(f"cursor model discovery failed{suffix}") + raise self._failure( + RuntimeError("cursor model discovery failed"), "model_discovery", + proc=proc, stderr=stderr.decode("utf-8", errors="replace"), + ) models = parse_cursor_models(stdout.decode("utf-8", errors="replace")) if not models: - raise RuntimeError( - "cursor model discovery returned no model IDs; run " - "'cursor-agent --list-models' to check the current account" + raise self._failure( + RuntimeError("cursor model discovery returned no model IDs; run 'cursor-agent --list-models' to check the current account"), + "model_discovery", proc=proc, stderr=stderr.decode("utf-8", errors="replace"), + kind="unknown", ) self._cursor_models_cache = models return models @@ -764,9 +838,10 @@ async def _spawn(self, session: Session) -> _Live: self.agent.cwd or session.cwd or None, ) if session.model not in models: - raise ValueError( - f"cursor model {session.model!r} is not available for the current account; " - f"available model IDs: {', '.join(models)}" + raise self._failure( + ValueError(f"cursor model {session.model!r} is not available for the current account; " + f"available model IDs: {', '.join(models)}"), + "model_discovery", session, kind="unsupported_model", ) cmd = with_cursor_cli_model(cmd, session.model) kwargs: dict[str, Any] = {} @@ -962,7 +1037,7 @@ async def _sync_model_option(self, live: _Live, session: Session) -> None: return try: await self._set_config_option(live, session, "model", session.model) - except RpcTimeoutError: + except AcpUnavailableError: raise except Exception as exc: _, offered = config_option_values(live.config_options, "model") @@ -984,9 +1059,10 @@ async def _sync_cursor_selection(self, live: _Live, session: Session) -> None: ) label = catalog.get(session.model) if label is None: - raise ValueError( - f"cursor model {session.model!r} is not available for the current account; " - f"available model IDs: {', '.join(catalog)}" + raise self._failure( + ValueError(f"cursor model {session.model!r} is not available for the current account; " + f"available model IDs: {', '.join(catalog)}"), + "model_discovery", session, kind="unsupported_model", ) base, base_name = _cursor_base_model(session.model, label, live.config_options) current, _offered = config_option_values(live.config_options, "model") @@ -1080,7 +1156,7 @@ async def _sync_bypass_mode(self, live: _Live, session: Session, mode_id: str) - "session/set_mode", session, ) - except RpcTimeoutError: + except AcpUnavailableError: raise except Exception as exc: message = ( @@ -1138,7 +1214,7 @@ async def _sync_kimi_thinking(self, live: _Live, session: Session) -> None: return try: await self._set_config_option(live, session, "thinking", level) - except RpcTimeoutError: + except AcpUnavailableError: raise except Exception as exc: # Bridge chose this level by mapping, so a rejection is Bridge's @@ -1181,7 +1257,7 @@ async def _sync_opencode_effort(self, live: _Live, session: Session) -> None: return try: await self._set_config_option(live, session, "effort", level) - except RpcTimeoutError: + except AcpUnavailableError: raise except Exception as exc: message = f"opencode rejected effort={level} for effort={session.effort}: {exc}" @@ -1274,7 +1350,7 @@ async def _sync_zcode_model(self, live: _Live, session: Session) -> None: if current != target: try: await self._set_config_option(live, session, "model", target) - except RpcTimeoutError: + except AcpUnavailableError: raise except Exception as exc: _, refreshed = config_option_values(live.config_options, "model") @@ -1326,7 +1402,7 @@ async def _sync_minimax_model(self, live: _Live, session: Session) -> None: if current != target: try: await self._set_config_option(live, session, "model", target) - except RpcTimeoutError: + except AcpUnavailableError: raise except Exception as exc: _, refreshed = config_option_values(live.config_options, "model") @@ -1363,7 +1439,7 @@ async def _sync_named_option( return try: await self._set_config_option(live, session, option_id, value) - except RpcTimeoutError: + except AcpUnavailableError: raise except Exception as exc: message = ( @@ -1402,7 +1478,7 @@ async def _sync_mapped_effort( return try: await self._set_config_option(live, session, option_id, level) - except RpcTimeoutError: + except AcpUnavailableError: raise except Exception as exc: message = ( @@ -1414,15 +1490,18 @@ async def _sync_mapped_effort( live.applied_effort = level async def _sync_selection(self, live: _Live, session: Session) -> None: - await self._sync_grok_model(live, session) - await self._sync_cursor_selection(live, session) - await self._sync_kimi_selection(live, session) - await self._sync_opencode_selection(live, session) - await self._sync_claude_selection(live, session) - await self._sync_devin_selection(live, session) - await self._sync_dsh_selection(live, session) - await self._sync_zcode_selection(live, session) - await self._sync_minimax_selection(live, session) + try: + await self._sync_grok_model(live, session) + await self._sync_cursor_selection(live, session) + await self._sync_kimi_selection(live, session) + await self._sync_opencode_selection(live, session) + await self._sync_claude_selection(live, session) + await self._sync_devin_selection(live, session) + await self._sync_dsh_selection(live, session) + await self._sync_zcode_selection(live, session) + await self._sync_minimax_selection(live, session) + except Exception as exc: + raise self._failure(exc, "session/configure", session) from None async def _sync_claude_effort(self, live: _Live, session: Session) -> None: if not session.effort: @@ -1443,7 +1522,7 @@ async def _sync_claude_effort(self, live: _Live, session: Session) -> None: return try: await self._set_config_option(live, session, "effort", level) - except RpcTimeoutError: + except AcpUnavailableError: raise except Exception as exc: message = f"claude rejected effort={level} for effort={session.effort}: {exc}" @@ -1553,8 +1632,10 @@ async def ensure_session(self, session: Session) -> None: # The worker is already dead; a new_session on the same # connection cannot succeed. raise - except Exception: - log.warning("session/load failed for %s; creating a new session", session.session_id, exc_info=True) + except AcpUnavailableError: + raise + except Exception as exc: + log.warning("session/load failed for %s; creating a new session: %s", session.session_id, _redact_diagnostic(str(exc))) else: self._remember_config_options(live, revived) await self._sync_selection(live, session) @@ -1575,11 +1656,16 @@ async def ensure_session(self, session: Session) -> None: await self._sync_selection(live, session) async def run_turn(self, session: Session, task: Task) -> TurnResult: - await self.ensure_session(session) + live = self._live.get(session.session_id) + if live is not None: + live.stderr_tail = "" + try: + await self.ensure_session(session) + except Exception as exc: + raise self._failure(exc, "process_start", session) from None live = self._live[session.session_id] assert live.conn is not None and live.client is not None live.client.reset_turn() - live.stderr_tail = "" warnings: list[str] = live.pending_warnings live.pending_warnings = [] if self.agent.name not in _MODEL_EFFORT_AGENTS and (task.model or task.effort): @@ -1588,12 +1674,12 @@ async def run_turn(self, session: Session, task: Task) -> TurnResult: f"model={task.model!r} effort={task.effort!r} were ignored" ) append_event(session.session_id, "prompt_sent", {"text": task.message}, self.home) - prompt = live.conn.prompt( - session_id=session.native_session_id, - prompt=[text_block(task.message)], - ) - live.prompt_task = asyncio.ensure_future(prompt) try: + prompt = live.conn.prompt( + session_id=session.native_session_id, + prompt=[text_block(task.message)], + ) + live.prompt_task = asyncio.ensure_future(prompt) response = await live.prompt_task except asyncio.CancelledError: append_event(session.session_id, "turn_end", {"stop_reason": "cancelled"}, self.home) @@ -1605,15 +1691,10 @@ async def run_turn(self, session: Session, task: Task) -> TurnResult: observed_model=live.applied_model, observed_effort=live.applied_effort, ) - except Exception: - if live.stderr_tail: - log.warning( - "%s prompt failed for %s stderr_tail=%r", - self.agent.name, - session.session_id, - live.stderr_tail, - ) - raise + except Exception as exc: + raise self._failure( + exc, "session/prompt", session, prompt_may_have_been_sent=True, + ) from None finally: live.prompt_task = None stop = getattr(response, "stop_reason", None) or "end_turn" diff --git a/src/agent_bridge/agents.toml b/src/agent_bridge/agents.toml index e8702a4..e6c1616 100644 --- a/src/agent_bridge/agents.toml +++ b/src/agent_bridge/agents.toml @@ -110,8 +110,8 @@ discover_proxy = true # no_proxy = "localhost,127.0.0.1,::1" [server] -# Self-exit for abandoned MCP server instances whose host never closed stdin. -idle_exit_sec = 7200 +# Optional idle self-exit; enable only when the host can reconnect reliably. +idle_exit_sec = 0 [coordinator] # How eagerly the coordinator should dispatch to workers. diff --git a/src/agent_bridge/config.py b/src/agent_bridge/config.py index cc0054f..e756384 100644 --- a/src/agent_bridge/config.py +++ b/src/agent_bridge/config.py @@ -130,9 +130,9 @@ class EnvConfig(BaseModel): class ServerConfig(BaseModel): - """Process-level server behavior (idle self-exit for abandoned MCP instances).""" + """Process-level server behavior (optional idle self-exit).""" - idle_exit_sec: int = 7200 + idle_exit_sec: int = 0 class QuotaConfig(BaseModel): diff --git a/src/agent_bridge/models.py b/src/agent_bridge/models.py index 6126567..d261fd6 100644 --- a/src/agent_bridge/models.py +++ b/src/agent_bridge/models.py @@ -108,6 +108,14 @@ class Session(BaseModel): owner_create_time: float | None = None +class TaskFailure(BaseModel): + operation: str + kind: str + prompt_may_have_been_sent: bool + exit_code: int | None = None + stderr_summary: str | None = None + + class Task(BaseModel): task_id: str session_id: str @@ -128,6 +136,7 @@ class Task(BaseModel): files_changed_state: FilesChangedState = FilesChangedState.pending usage: dict[str, Any] = Field(default_factory=dict) error: str | None = None + failure: TaskFailure | None = None warnings: list[str] = Field(default_factory=list) created_at: str = Field(default_factory=iso) started_at: str | None = None diff --git a/src/agent_bridge/registry.py b/src/agent_bridge/registry.py index 700d5e6..2c5dc82 100644 --- a/src/agent_bridge/registry.py +++ b/src/agent_bridge/registry.py @@ -14,6 +14,7 @@ import psutil from agent_bridge.adapters import build_adapter +from agent_bridge.adapters.acp import AcpError from agent_bridge.adapters.base import Adapter from agent_bridge.config import ( COORDINATOR_MODE_HINTS, @@ -511,8 +512,8 @@ async def env_status(self) -> dict: warnings = status.setdefault("warnings", []) warnings.append( f"{siblings} other agent-bridge server instance(s) running on this machine " - "(each coordinator host holds its own; abandoned ones self-exit after " - "server.idle_exit_sec)" + "(coordinator threads may each hold an instance; " + "counts alone do not imply orphaned processes)" ) return status @@ -756,10 +757,14 @@ async def _run_task(self, task_id: str) -> None: task.status = TaskStatus.cancelled task.stop_reason = "cancelled" except Exception as exc: - log.exception("task %s failed", task_id) + if isinstance(exc, AcpError): + log.warning("task %s failed: %s; failure=%s", task_id, exc, exc.failure.model_dump()) + else: + log.exception("task %s failed", task_id) if task.status not in TERMINAL_STATUSES: task.status = TaskStatus.failed task.error = str(exc) + task.failure = exc.failure if isinstance(exc, AcpError) else None task.stop_reason = "error" finally: if watch is not None: @@ -1047,6 +1052,7 @@ def _task_snapshot(self, task: Task, include_result: bool = False) -> dict: "status": task.status.value, "stop_reason": task.stop_reason, "error": task.error, + "failure": task.failure.model_dump(mode="json") if task.failure else None, "warnings": task.warnings, "files_changed": task.files_changed, "files_changed_total": task.files_changed_total, diff --git a/src/agent_bridge/server.py b/src/agent_bridge/server.py index 96e437b..e12e2ce 100644 --- a/src/agent_bridge/server.py +++ b/src/agent_bridge/server.py @@ -168,7 +168,7 @@ async def get_result( cursor: int = 0, max_chars: int = RESULT_PAGE_MAX_CHARS, ) -> dict[str, Any]: - """Return a page of the complete worker result plus changed files, usage, and requested/observed model. Continue with next_cursor while has_more is true. max_chars is capped at 60000. files_changed_state says whether the list/count were collected; files_changed is capped at 200 paths and files_changed_total carries the real count. For Grok, observed_model is the live sampler; the worker saying it is Grok 4.6 is not.""" + """Return a page of the complete worker result plus changed files, usage, and requested/observed model. Continue with next_cursor while has_more is true. max_chars is capped at 60000. files_changed_state says whether the list/count were collected; files_changed is capped at 200 paths and files_changed_total carries the real count. For Grok, observed_model is the live sampler; the worker saying it is Grok 4.6 is not. ACP failures may include failure (operation, kind, exit_code, prompt_may_have_been_sent, stderr_summary); error stays a string. Missing evidence or prompt_may_have_been_sent=true must not trigger blind replay; false is not proof of safe retry.""" try: return { "ok": True, diff --git a/tests/cursor_agent.py b/tests/cursor_agent.py index 9feae68..39e633e 100644 --- a/tests/cursor_agent.py +++ b/tests/cursor_agent.py @@ -88,6 +88,7 @@ async def initialize(self, protocol_version: int, client_capabilities=None, **kw return await super().initialize(protocol_version, **kwargs) async def new_session(self, cwd: str, mcp_servers=None, **kwargs: Any) -> NewSessionResponse: + await super().new_session(cwd, mcp_servers, **kwargs) return NewSessionResponse.model_validate( {"sessionId": self._session_id, "configOptions": self._options()} ) diff --git a/tests/echo_agent.py b/tests/echo_agent.py index 79d77d3..c10c72d 100644 --- a/tests/echo_agent.py +++ b/tests/echo_agent.py @@ -3,9 +3,13 @@ from __future__ import annotations import asyncio +import os +import sys +from pathlib import Path from typing import Any from acp import run_agent, update_agent_message_text +from acp.exceptions import RequestError from acp.schema import ( AgentCapabilities, Implementation, @@ -17,15 +21,23 @@ ) +def _record_effect(path: str) -> None: + with Path(path).open("a", encoding="utf-8") as marker: + marker.write("executed\n") + + class EchoAgent: def __init__(self) -> None: self._conn: Any = None self._session_id = "echo-session" + self._ready = False def on_connect(self, conn: Any) -> None: self._conn = conn async def initialize(self, protocol_version: int, **kwargs: Any) -> InitializeResponse: + if os.environ.get("BRIDGE_ECHO_FAILURE") == "initialize": + raise RequestError.auth_required() return InitializeResponse( protocol_version=protocol_version, agent_capabilities=AgentCapabilities( @@ -36,13 +48,25 @@ async def initialize(self, protocol_version: int, **kwargs: Any) -> InitializeRe ) async def new_session(self, cwd: str, mcp_servers=None, **kwargs: Any) -> NewSessionResponse: + if os.environ.get("BRIDGE_ECHO_FAILURE") == "new": + raise RequestError.auth_required() + self._ready = True return NewSessionResponse(session_id=self._session_id) async def load_session(self, cwd: str, session_id: str, mcp_servers=None, **kwargs: Any) -> None: + if os.environ.get("BRIDGE_ECHO_FAILURE") == "load": + raise RequestError.auth_required() self._session_id = session_id + self._ready = True return None async def prompt(self, session_id: str, prompt: list[Any], **kwargs: Any) -> PromptResponse: + if not self._ready: + raise RequestError.resource_not_found(session_id) + if os.environ.get("BRIDGE_ECHO_FAILURE") == "prompt_exit": + await asyncio.to_thread(_record_effect, os.environ["BRIDGE_ECHO_MARKER"]) + print("Connection closed", file=sys.stderr, flush=True) + os._exit(7) text = "" for block in prompt: piece = getattr(block, "text", None) diff --git a/tests/test_acp_echo.py b/tests/test_acp_echo.py index e0d9c93..42792bd 100644 --- a/tests/test_acp_echo.py +++ b/tests/test_acp_echo.py @@ -10,6 +10,7 @@ from agent_bridge.models import Session, Task from agent_bridge.paths import pids_path from agent_bridge.persist import read_json +from agent_bridge.registry import Registry @pytest.mark.asyncio @@ -62,3 +63,103 @@ async def test_acp_echo_roundtrip(bridge_home, tmp_path): assert proc.returncode is not None table = read_json(pids_path(bridge_home), {}) assert session.session_id not in table + + +@pytest.mark.parametrize(("mode", "operation", "possibly_sent"), [ + ("initialize", "initialize", False), + ("new", "session/new", False), + ("prompt_exit", "session/prompt", True), +]) +async def test_acp_failures_survive_tools_and_restart(bridge_home, tmp_path, mode, operation, possibly_sent): + marker = tmp_path / "effects.txt" + cfg = AgentConfig( + name="echo", protocol="acp", + command=[sys.executable, str(Path(__file__).with_name("echo_agent.py"))], + env={"BRIDGE_ECHO_FAILURE": mode, "BRIDGE_ECHO_MARKER": str(marker)}, + ) + registry = Registry.create(bridge_home) + registry.config.agents["echo"] = cfg + await registry.start() + try: + dispatched = await registry.dispatch_task("echo", "run once", cwd=str(tmp_path)) + task_id = dispatched["task_id"] + result = await registry.wait_task(task_id, timeout_sec=10) + assert result["status"] == "failed" + assert isinstance(result["error"], str) and result["error"] + failure = result["failure"] + assert failure["operation"] == operation + assert failure["prompt_may_have_been_sent"] is possibly_sent + assert failure["kind"] in ({"connection_closed", "worker_exit"} if possibly_sent else {"auth_required"}) + assert registry.check_task(task_id)["failure"] == failure + assert registry.get_result(task_id)["failure"] == failure + if possibly_sent: + assert marker.read_text(encoding="utf-8") == "executed\n" + else: + assert not marker.exists() + finally: + await registry.stop() + + restored = Registry.create(bridge_home) + restored.config.agents["echo"] = cfg + await restored.start() + try: + assert restored.get_result(task_id)["failure"] == failure + assert restored.check_task(task_id)["status"] == "failed" + finally: + await restored.stop() + + +async def test_missing_worker_command_reports_unsent_failure(bridge_home, tmp_path): + registry = Registry.create(bridge_home) + registry.config.agents["missing"] = AgentConfig( + name="missing", protocol="acp", command=[str(tmp_path / "no-such-worker")], + ) + await registry.start() + try: + dispatched = await registry.dispatch_task("missing", "never sent", cwd=str(tmp_path)) + result = await registry.wait_task(dispatched["task_id"], timeout_sec=5) + assert result["failure"]["operation"] == "process_start" + assert result["failure"]["prompt_may_have_been_sent"] is False + assert result["failure"]["exit_code"] is None + finally: + await registry.stop() + + +@pytest.mark.parametrize(("mode", "operation"), [("load", "session/load"), ("initialize", "initialize")]) +async def test_resume_auth_failure_does_not_create_a_blank_session(bridge_home, tmp_path, mode, operation): + cfg = AgentConfig( + name="echo", protocol="acp", revivable=True, + command=[sys.executable, str(Path(__file__).with_name("echo_agent.py"))], + ) + registry = Registry.create(bridge_home) + registry.config.agents["echo"] = cfg + await registry.start() + try: + first = await registry.dispatch_task("echo", "remember", cwd=str(tmp_path)) + assert (await registry.wait_task(first["task_id"], timeout_sec=10))["status"] == "completed" + session = registry.sessions[first["session_id"]] + await registry._adapter_for(session).shutdown(session) + cfg.env["BRIDGE_ECHO_FAILURE"] = mode + follow = await registry.dispatch_task("echo", "continue", cwd=str(tmp_path), session_id=session.session_id) + result = await registry.wait_task(follow["task_id"], timeout_sec=10) + assert result["status"] == "failed" + assert result["failure"]["operation"] == operation + assert result["failure"]["kind"] == "auth_required" + assert result["failure"]["prompt_may_have_been_sent"] is False + assert session.turns == 1 + cfg.env.pop("BRIDGE_ECHO_FAILURE") + retry = await registry.dispatch_task("echo", "continue after login", cwd=str(tmp_path), session_id=session.session_id) + recovered = await registry.wait_task(retry["task_id"], timeout_sec=10) + assert recovered["status"] == "completed" + assert recovered["failure"] is None + assert session.turns == 2 + finally: + await registry.stop() + + +def test_legacy_task_has_no_failure_evidence(): + task = Task.model_validate({ + "task_id": "old", "session_id": "old", "agent": "echo", "cwd": ".", + "message": "old", "status": "failed", "error": "Connection closed", + }) + assert task.failure is None diff --git a/tests/test_acp_paths.py b/tests/test_acp_paths.py index 35f0f12..db5317b 100644 --- a/tests/test_acp_paths.py +++ b/tests/test_acp_paths.py @@ -1,14 +1,18 @@ from __future__ import annotations import asyncio +from types import SimpleNamespace +from unittest.mock import AsyncMock import pytest +from acp.exceptions import RequestError from acp.schema import ToolCallLocation, ToolCallProgress, ToolCallStart from agent_bridge.adapters.acp import ( AcpAdapter, RpcTimeoutError, _BridgeClient, + _Live, should_collect_tool_paths, ) from agent_bridge.config import AgentConfig @@ -55,6 +59,99 @@ async def test_rpc_timeout_raises_clear_error(tmp_path): await adapter._rpc(asyncio.sleep(30), "session/new", session, timeout=0.05) +@pytest.mark.asyncio +@pytest.mark.parametrize(("error", "exit_code", "kind"), [ + (RequestError.auth_required(), None, "auth_required"), + (RequestError.internal_error(), None, "protocol_error"), + (RequestError.internal_error(), 7, "protocol_error"), + (ConnectionError("Connection closed"), None, "connection_closed"), + (ConnectionError("Connection closed"), 7, "worker_exit"), +]) +async def test_rpc_failure_keeps_operation_and_process_facts(tmp_path, monkeypatch, error, exit_code, kind): + adapter = AcpAdapter(AgentConfig(name="echo", protocol="acp", command=["echo"]), tmp_path) + monkeypatch.setattr(adapter, "shutdown", AsyncMock()) + session = Session(session_id="sess_error", agent="echo", cwd=str(tmp_path)) + live = _Live() + live.proc = SimpleNamespace(returncode=exit_code) + adapter._live[session.session_id] = live + + async def fail(): + raise error + + with pytest.raises(Exception) as caught: + await adapter._rpc(fail(), "session/new", session) + failure = caught.value.failure + assert failure.operation == "session/new" + assert failure.kind == kind + assert failure.exit_code == exit_code + assert failure.prompt_may_have_been_sent is False + + +@pytest.mark.asyncio +async def test_timeout_captures_diagnostics_before_cleanup(tmp_path, monkeypatch, caplog): + adapter = AcpAdapter(AgentConfig(name="echo", protocol="acp", command=["echo"]), tmp_path) + session = Session(session_id="sess_timeout", agent="echo", cwd=str(tmp_path)) + live = _Live() + live.proc = SimpleNamespace(returncode=None) + live.stderr_tail = "Authentication required\nAuthorization: Bearer test-token\nCookie: session=test-cookie" + adapter._live[session.session_id] = live + + async def shutdown(_session): + live.proc.returncode = -15 + live.stderr_tail = "" + adapter._live.pop(session.session_id) + + monkeypatch.setattr(adapter, "shutdown", shutdown) + with pytest.raises(RpcTimeoutError) as caught: + await adapter._rpc(asyncio.Event().wait(), "initialize", session, timeout=0.01) + failure = caught.value.failure + assert failure.kind == "timeout" + assert failure.operation == "initialize" + assert failure.exit_code is None + assert failure.prompt_may_have_been_sent is False + assert "authentication required" in failure.stderr_summary.lower() + assert "test-token" not in failure.stderr_summary + caplog.text + assert "test-cookie" not in failure.stderr_summary + caplog.text + + +@pytest.mark.asyncio +async def test_rpc_failure_redacts_and_bounds_untrusted_diagnostics(tmp_path, caplog): + adapter = AcpAdapter(AgentConfig(name="echo", protocol="acp", command=["echo"]), tmp_path) + session = Session(session_id="sess_redact", agent="echo", cwd=str(tmp_path)) + live = _Live() + live.stderr_tail = ( + "Authentication required\n" + "api_key = 'test-api-secret'\n" + "https://test-user:test-pass@example.invalid/path?token=test-query-secret\n" + "Cookie: session=test-cookie\n" + + "untrusted diagnostic " * 2000 + ) + adapter._live[session.session_id] = live + + async def fail(): + raise RequestError(-32000, "Authentication required; token=test-error-secret") + + with pytest.raises(Exception) as caught: + await adapter._rpc(fail(), "initialize", session) + failure = caught.value.failure + rendered = str(caught.value) + (failure.stderr_summary or "") + caplog.text + for secret in ("test-api-secret", "test-user", "test-pass", "test-query-secret", "test-cookie", "test-error-secret"): + assert secret not in rendered + assert len(failure.stderr_summary or "") <= 2048 + + +@pytest.mark.asyncio +async def test_rpc_cancellation_is_not_wrapped_as_failure(tmp_path): + adapter = AcpAdapter(AgentConfig(name="echo", protocol="acp", command=["echo"]), tmp_path) + session = Session(session_id="sess_cancel", agent="echo", cwd=str(tmp_path)) + + async def cancel(): + raise asyncio.CancelledError + + with pytest.raises(asyncio.CancelledError): + await adapter._rpc(cancel(), "initialize", session) + + @pytest.mark.asyncio async def test_tool_call_events_record_kind_status_and_input(tmp_path): client = _BridgeClient("sess_tool", tmp_path) diff --git a/tests/test_config.py b/tests/test_config.py index 4609d70..e842f75 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -141,9 +141,10 @@ def test_stall_timeout_rejects_negative(tmp_path): def test_server_idle_exit_defaults(tmp_path): - assert AppConfig().server.idle_exit_sec == 7200 + assert AppConfig().server.idle_exit_sec == 0 cfg = load_config(tmp_path) - assert cfg.server.idle_exit_sec == 7200 + assert cfg.server.idle_exit_sec == 0 + assert load_config(Path(__file__).resolve().parents[1]).server.idle_exit_sec == 0 def test_server_idle_exit_overlay(tmp_path): diff --git a/tests/test_cursor_acp.py b/tests/test_cursor_acp.py index aa85e89..0697e5d 100644 --- a/tests/test_cursor_acp.py +++ b/tests/test_cursor_acp.py @@ -1,6 +1,7 @@ from __future__ import annotations import asyncio +import os import sys from pathlib import Path @@ -8,6 +9,7 @@ from agent_bridge.adapters.acp import ( AcpAdapter, + AcpError, _cursor_base_model, _cursor_effort_option_ids, _cursor_parameter_targets, @@ -237,8 +239,10 @@ async def test_cursor_model_is_discovered_once_pinned_switched_and_reported(tmp_ "model": session.model, } ) - with pytest.raises(ValueError, match="available model IDs"): + with pytest.raises(AcpError, match="available model IDs") as caught: await adapter.run_turn(session, invalid) + assert caught.value.failure.kind == "unsupported_model" + assert caught.value.failure.prompt_may_have_been_sent is False live = adapter._live[session.session_id] assert live.applied_model == "cursor-model-b" finally: @@ -284,11 +288,34 @@ async def test_cursor_unavailable_model_fails_before_acp_start(tmp_path): model=session.model, ) - with pytest.raises(ValueError, match="available model IDs: cursor-model-a, cursor-model-a-medium"): + with pytest.raises(AcpError, match="available model IDs: cursor-model-a, cursor-model-a-medium") as caught: await adapter.run_turn(session, task) + assert caught.value.failure.operation == "model_discovery" + assert caught.value.failure.kind == "unsupported_model" + assert caught.value.failure.prompt_may_have_been_sent is False assert session.pid is None +@pytest.mark.asyncio +async def test_cursor_model_discovery_reports_its_own_exit(tmp_path): + command = [ + sys.executable, "-c", + "import sys; print('Authentication required', file=sys.stderr); " + "print('Authorization: Bearer test-discovery-secret', file=sys.stderr); sys.exit(7)", + "acp", + ] + adapter = AcpAdapter(AgentConfig(name="cursor", protocol="acp", command=command), tmp_path) + with pytest.raises(AcpError) as caught: + await adapter._cursor_models(command, dict(os.environ), str(tmp_path)) + failure = caught.value.failure + assert failure.operation == "model_discovery" + assert failure.exit_code == 7 + assert failure.kind == "worker_exit" + assert failure.prompt_may_have_been_sent is False + assert "authentication required" in failure.stderr_summary.lower() + assert "test-discovery-secret" not in str(caught.value) + failure.stderr_summary + + @pytest.mark.asyncio async def test_cursor_model_discovery_reaps_process_on_cancel(tmp_path, monkeypatch): reaped: list[object] = [] diff --git a/tests/test_kimi_acp.py b/tests/test_kimi_acp.py index ebe5c23..c2c500c 100644 --- a/tests/test_kimi_acp.py +++ b/tests/test_kimi_acp.py @@ -1,8 +1,9 @@ from __future__ import annotations import pytest +from acp.exceptions import RequestError -from agent_bridge.adapters.acp import AcpAdapter, _Live +from agent_bridge.adapters.acp import AcpAdapter, AcpError, _Live from agent_bridge.config import AgentConfig from agent_bridge.models import Session @@ -152,6 +153,22 @@ async def test_a_rejected_model_fails_the_turn_and_names_the_real_options(): await adapter._sync_kimi_selection(live, session) +@pytest.mark.asyncio +@pytest.mark.parametrize("error", [RequestError.auth_required(), ConnectionError("Connection closed")]) +async def test_model_transport_failure_is_not_relabelled_as_model_rejection(monkeypatch, error): + adapter, live, session = build(session_kwargs={"model": "kimi-code/k3"}) + + async def fail(**_kwargs): + raise error + + monkeypatch.setattr(live.conn, "set_config_option", fail) + with pytest.raises(AcpError) as caught: + await adapter._sync_kimi_selection(live, session) + assert caught.value.failure.operation == "session/set_config_option model" + assert caught.value.failure.kind in {"auth_required", "connection_closed"} + assert caught.value.failure.prompt_may_have_been_sent is False + + @pytest.mark.asyncio async def test_model_switch_reapplies_thinking_after_vocab_reset(): after = [ diff --git a/tests/test_registry.py b/tests/test_registry.py index 3066a85..ec011eb 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -907,11 +907,27 @@ async def test_old_terminal_tasks_are_pruned(bridge_home, tmp_path, monkeypatch) await registry.stop() +@pytest.mark.asyncio +async def test_default_server_remains_usable_after_long_idle(bridge_home, tmp_path): + registry = Registry.create(bridge_home) + await registry.start() + try: + assert registry._watchdog is None + registry._last_activity = time.monotonic() - 86400 + assert registry.idle_exit_due() is False + dispatched = await registry.dispatch_task("fake", "after idle", cwd=str(tmp_path)) + result = await registry.wait_task(dispatched["task_id"], timeout_sec=5) + assert result["status"] == "completed" + finally: + await registry.stop() + + @pytest.mark.asyncio async def test_idle_exit_due_predicate(bridge_home, tmp_path): work = tmp_path / "work" work.mkdir() registry = Registry.create(bridge_home) + registry.config.server.idle_exit_sec = 30 await registry.start() try: assert registry.idle_exit_due() is False diff --git a/tests/test_server_tools.py b/tests/test_server_tools.py index b478422..cfb1cf9 100644 --- a/tests/test_server_tools.py +++ b/tests/test_server_tools.py @@ -1,10 +1,14 @@ import asyncio import contextlib +import json +import os +import sys import time import types import pytest +from agent_bridge.processes import reap_subprocess from agent_bridge.registry import Registry from agent_bridge.server import INSTRUCTIONS, _error, _registry, list_sessions, mcp @@ -94,6 +98,35 @@ async def counter() -> None: await task +@pytest.mark.asyncio +async def test_stdio_server_exits_on_host_eof(bridge_home): + proc = await asyncio.create_subprocess_exec( + sys.executable, "-c", + "from agent_bridge.server import mcp; mcp.run(transport='stdio')", + stdin=asyncio.subprocess.PIPE, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + env={**os.environ, "AGENT_BRIDGE_HOME": str(bridge_home), "AGENT_BRIDGE_PARENT_CONTEXT": ""}, + ) + try: + request = { + "jsonrpc": "2.0", "id": 1, "method": "initialize", + "params": { + "protocolVersion": "2025-11-25", "capabilities": {}, + "clientInfo": {"name": "eof-test", "version": "1"}, + }, + } + proc.stdin.write((json.dumps(request) + "\n").encode()) + await proc.stdin.drain() + response = json.loads(await asyncio.wait_for(proc.stdout.readline(), timeout=10)) + assert response["id"] == 1 and "result" in response + assert proc.returncode is None + proc.stdin.close() + assert await asyncio.wait_for(proc.wait(), timeout=10) == 0 + finally: + await reap_subprocess(proc) + + def test_registry_rejects_dict_lifespan(bridge_home): registry = Registry.create(bridge_home) ctx = types.SimpleNamespace( From 79241bcc00f2725f908390fe0d8a0e9deff679a8 Mon Sep 17 00:00:00 2001 From: FeiZhuLulu <279467036+FeiZhuLulu@users.noreply.github.com> Date: Sun, 4 Oct 2026 16:44:53 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E8=AF=AF=E5=88=A4=E3=80=81worker=20=E9=80=80=E5=87=BA=E7=A0=81?= =?UTF-8?q?=E4=B8=8E=20stderr=20=E8=AF=BB=E5=8F=96=E9=98=BB=E5=A1=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agent_bridge/adapters/acp.py | 34 ++++++++++----- src/agent_bridge/registry.py | 25 ++++++++--- tests/echo_agent.py | 14 ++++++- tests/test_acp_echo.py | 71 +++++++++++++++++++++++++++++--- tests/test_acp_paths.py | 44 ++++++++++++++++---- 5 files changed, 157 insertions(+), 31 deletions(-) diff --git a/src/agent_bridge/adapters/acp.py b/src/agent_bridge/adapters/acp.py index 04e1bff..89dfad6 100644 --- a/src/agent_bridge/adapters/acp.py +++ b/src/agent_bridge/adapters/acp.py @@ -79,6 +79,8 @@ # otherwise hang dispatch forever; prompt itself stays unbounded by design. RPC_TIMEOUT_SEC = 60.0 CURSOR_MODEL_LIST_TIMEOUT_SEC = 30.0 +PROMPT_CANCEL_GRACE_SEC = 10.0 +EXIT_OBSERVE_SEC = 1.0 # ACP ToolKind values that can change the workspace. Read-only kinds (read, # search, fetch, think, ...) also carry locations; counting them would report @@ -499,6 +501,12 @@ def _pick_permission_option(options: Iterable[PermissionOption]) -> PermissionOp return ranked[0][1] if ranked else None +async def _observe_exit(proc: asyncio.subprocess.Process | None) -> None: + if proc is not None and proc.returncode is None: + with contextlib.suppress(TimeoutError): + await asyncio.wait_for(proc.wait(), timeout=EXIT_OBSERVE_SEC) + + class _BridgeClient: def __init__(self, session_id: str, home: Path) -> None: self.session_id = session_id @@ -698,12 +706,12 @@ async def _drain_stderr( return try: while True: - line = await proc.stderr.readline() - if not line: + chunk = await proc.stderr.read(65536) + if not chunk: return - text = line.decode("utf-8", errors="replace").rstrip() - if text: - live.stderr_tail = f"{live.stderr_tail}\n{text}"[-STDERR_TAIL_LIMIT:] + live.stderr_tail = ( + live.stderr_tail + chunk.decode("utf-8", errors="replace") + )[-STDERR_TAIL_LIMIT:] except (ValueError, OSError): log.warning("stderr drain aborted for %s", session_id, exc_info=True) @@ -756,6 +764,9 @@ async def _rpc( except Exception as exc: if isinstance(exc, TimeoutError): exc = TimeoutError(f"{self.agent.name} {what} timed out after {int(timeout)}s") + if isinstance(exc, (ConnectionError, EOFError)): + live = self._live.get(session.session_id) + await _observe_exit(live.proc if live else None) error = self._failure(exc, what, session) if ( isinstance(error, RpcTimeoutError) @@ -1692,6 +1703,8 @@ async def run_turn(self, session: Session, task: Task) -> TurnResult: observed_effort=live.applied_effort, ) except Exception as exc: + if isinstance(exc, (ConnectionError, EOFError)): + await _observe_exit(live.proc) raise self._failure( exc, "session/prompt", session, prompt_may_have_been_sent=True, ) from None @@ -1722,13 +1735,14 @@ async def cancel(self, session: Session) -> None: live = self._live.get(session.session_id) if live is None or live.conn is None: return - try: - await asyncio.wait_for(live.conn.cancel(session_id=session.native_session_id), timeout=5) - except Exception: - log.warning("ACP cancel failed for %s", session.session_id, exc_info=True) + if session.native_session_id: + try: + await asyncio.wait_for(live.conn.cancel(session_id=session.native_session_id), timeout=5) + except Exception: + log.warning("ACP cancel failed for %s", session.session_id, exc_info=True) if live.prompt_task is not None: try: - await asyncio.wait_for(asyncio.shield(live.prompt_task), timeout=10) + await asyncio.wait_for(asyncio.shield(live.prompt_task), timeout=PROMPT_CANCEL_GRACE_SEC) return except (asyncio.CancelledError, Exception): # CancelledError derives from BaseException and must be diff --git a/src/agent_bridge/registry.py b/src/agent_bridge/registry.py index 2c5dc82..ca10342 100644 --- a/src/agent_bridge/registry.py +++ b/src/agent_bridge/registry.py @@ -143,6 +143,7 @@ def __init__( self._done: dict[str, asyncio.Event] = {} self._idle: dict[str, asyncio.Task[None]] = {} self._bg: dict[str, asyncio.Task[None]] = {} + self._cancel_requested: set[str] = set() self._lock = asyncio.Lock() self._last_activity = time.monotonic() self._watchdog: asyncio.Task[None] | None = None @@ -745,8 +746,12 @@ async def _run_task(self, task_id: str) -> None: if task.status not in TERMINAL_STATUSES: task.stop_reason = result.stop_reason if result.error: - task.status = TaskStatus.failed - task.error = result.error + if task_id in self._cancel_requested: + task.status = TaskStatus.cancelled + task.stop_reason = "cancelled" + else: + task.status = TaskStatus.failed + task.error = result.error elif result.stop_reason == "cancelled": task.status = TaskStatus.cancelled else: @@ -762,11 +767,18 @@ async def _run_task(self, task_id: str) -> None: else: log.exception("task %s failed", task_id) if task.status not in TERMINAL_STATUSES: - task.status = TaskStatus.failed - task.error = str(exc) - task.failure = exc.failure if isinstance(exc, AcpError) else None - task.stop_reason = "error" + if task_id in self._cancel_requested: + task.status = TaskStatus.cancelled + task.stop_reason = "cancelled" + task.error = None + task.failure = None + else: + task.status = TaskStatus.failed + task.error = str(exc) + task.failure = exc.failure if isinstance(exc, AcpError) else None + task.stop_reason = "error" finally: + self._cancel_requested.discard(task_id) if watch is not None: watch.cancel() with contextlib.suppress(asyncio.CancelledError): @@ -1163,6 +1175,7 @@ async def cancel_task(self, task_id: str) -> dict: task = self._require_task(task_id) if task.status in TERMINAL_STATUSES: return self._task_snapshot(task) + self._cancel_requested.add(task_id) session = self.sessions[task.session_id] adapter = self._adapters.get(session.session_id) if adapter is not None: diff --git a/tests/echo_agent.py b/tests/echo_agent.py index c10c72d..7ce1c88 100644 --- a/tests/echo_agent.py +++ b/tests/echo_agent.py @@ -36,7 +36,16 @@ def on_connect(self, conn: Any) -> None: self._conn = conn async def initialize(self, protocol_version: int, **kwargs: Any) -> InitializeResponse: - if os.environ.get("BRIDGE_ECHO_FAILURE") == "initialize": + failure = os.environ.get("BRIDGE_ECHO_FAILURE") + if failure == "initialize": + raise RequestError.auth_required() + if failure == "initialize_exit": + os._exit(9) + if failure == "stderr_flood": + sys.stderr.write("x" * (17 * 1024 * 1024)) + sys.stderr.write("\nauthentication required\n") + sys.stderr.flush() + await asyncio.sleep(0.5) raise RequestError.auth_required() return InitializeResponse( protocol_version=protocol_version, @@ -67,6 +76,9 @@ async def prompt(self, session_id: str, prompt: list[Any], **kwargs: Any) -> Pro await asyncio.to_thread(_record_effect, os.environ["BRIDGE_ECHO_MARKER"]) print("Connection closed", file=sys.stderr, flush=True) os._exit(7) + if os.environ.get("BRIDGE_ECHO_FAILURE") == "prompt_hang": + await asyncio.to_thread(_record_effect, os.environ["BRIDGE_ECHO_MARKER"]) + await asyncio.Event().wait() text = "" for block in prompt: piece = getattr(block, "text", None) diff --git a/tests/test_acp_echo.py b/tests/test_acp_echo.py index 42792bd..c744703 100644 --- a/tests/test_acp_echo.py +++ b/tests/test_acp_echo.py @@ -1,5 +1,6 @@ from __future__ import annotations +import asyncio import sys from pathlib import Path @@ -65,12 +66,15 @@ async def test_acp_echo_roundtrip(bridge_home, tmp_path): assert session.session_id not in table -@pytest.mark.parametrize(("mode", "operation", "possibly_sent"), [ - ("initialize", "initialize", False), - ("new", "session/new", False), - ("prompt_exit", "session/prompt", True), +@pytest.mark.parametrize(("mode", "operation", "possibly_sent", "kind", "exit_code"), [ + ("initialize", "initialize", False, "auth_required", None), + ("new", "session/new", False, "auth_required", None), + ("initialize_exit", "initialize", False, "worker_exit", 9), + ("prompt_exit", "session/prompt", True, "worker_exit", 7), ]) -async def test_acp_failures_survive_tools_and_restart(bridge_home, tmp_path, mode, operation, possibly_sent): +async def test_acp_failures_survive_tools_and_restart( + bridge_home, tmp_path, mode, operation, possibly_sent, kind, exit_code +): marker = tmp_path / "effects.txt" cfg = AgentConfig( name="echo", protocol="acp", @@ -89,7 +93,8 @@ async def test_acp_failures_survive_tools_and_restart(bridge_home, tmp_path, mod failure = result["failure"] assert failure["operation"] == operation assert failure["prompt_may_have_been_sent"] is possibly_sent - assert failure["kind"] in ({"connection_closed", "worker_exit"} if possibly_sent else {"auth_required"}) + assert failure["kind"] == kind + assert failure["exit_code"] == exit_code assert registry.check_task(task_id)["failure"] == failure assert registry.get_result(task_id)["failure"] == failure if possibly_sent: @@ -109,6 +114,60 @@ async def test_acp_failures_survive_tools_and_restart(bridge_home, tmp_path, mod await restored.stop() +@pytest.mark.asyncio +async def test_cancel_of_cancel_ignoring_worker_ends_cancelled(bridge_home, tmp_path, monkeypatch): + marker = tmp_path / "effects.txt" + cfg = AgentConfig( + name="echo", protocol="acp", + command=[sys.executable, str(Path(__file__).with_name("echo_agent.py"))], + env={"BRIDGE_ECHO_FAILURE": "prompt_hang", "BRIDGE_ECHO_MARKER": str(marker)}, + ) + monkeypatch.setattr("agent_bridge.adapters.acp.PROMPT_CANCEL_GRACE_SEC", 0.5) + registry = Registry.create(bridge_home) + registry.config.agents["echo"] = cfg + await registry.start() + try: + dispatched = await registry.dispatch_task("echo", "hang", cwd=str(tmp_path)) + for _ in range(100): + if marker.exists(): + break + await asyncio.sleep(0.05) + assert marker.exists() + cancelled = await registry.cancel_task(dispatched["task_id"]) + assert cancelled["status"] == "cancelled" + assert cancelled["stop_reason"] == "cancelled" + assert cancelled["failure"] is None + waited = await registry.wait_task(dispatched["task_id"], timeout_sec=10) + assert waited["status"] == "cancelled" + assert registry.check_task(dispatched["task_id"])["failure"] is None + session = registry.sessions[dispatched["session_id"]] + assert session.pid is None + assert session.session_id not in read_json(pids_path(bridge_home), {}) + finally: + await registry.stop() + + +@pytest.mark.asyncio +async def test_stderr_flood_keeps_auth_diagnostics(bridge_home, tmp_path): + cfg = AgentConfig( + name="echo", protocol="acp", + command=[sys.executable, str(Path(__file__).with_name("echo_agent.py"))], + env={"BRIDGE_ECHO_FAILURE": "stderr_flood"}, + ) + registry = Registry.create(bridge_home) + registry.config.agents["echo"] = cfg + await registry.start() + try: + dispatched = await registry.dispatch_task("echo", "flood", cwd=str(tmp_path)) + result = await registry.wait_task(dispatched["task_id"], timeout_sec=30) + assert result["timed_out"] is False + assert result["status"] == "failed" + assert result["failure"]["kind"] == "auth_required" + assert "authentication required" in result["failure"]["stderr_summary"] + finally: + await registry.stop() + + async def test_missing_worker_command_reports_unsent_failure(bridge_home, tmp_path): registry = Registry.create(bridge_home) registry.config.agents["missing"] = AgentConfig( diff --git a/tests/test_acp_paths.py b/tests/test_acp_paths.py index db5317b..fa928f8 100644 --- a/tests/test_acp_paths.py +++ b/tests/test_acp_paths.py @@ -59,20 +59,37 @@ async def test_rpc_timeout_raises_clear_error(tmp_path): await adapter._rpc(asyncio.sleep(30), "session/new", session, timeout=0.05) +def _fake_proc(returncode: int | None, exits_to: int | None = None) -> SimpleNamespace: + proc = SimpleNamespace(returncode=returncode) + + async def wait() -> None: + if exits_to is None: + await asyncio.Event().wait() + else: + proc.returncode = exits_to + + proc.wait = wait + return proc + + @pytest.mark.asyncio -@pytest.mark.parametrize(("error", "exit_code", "kind"), [ - (RequestError.auth_required(), None, "auth_required"), - (RequestError.internal_error(), None, "protocol_error"), - (RequestError.internal_error(), 7, "protocol_error"), - (ConnectionError("Connection closed"), None, "connection_closed"), - (ConnectionError("Connection closed"), 7, "worker_exit"), +@pytest.mark.parametrize(("error", "returncode", "exits_to", "kind", "exit_code"), [ + (RequestError.auth_required(), None, None, "auth_required", None), + (RequestError.internal_error(), None, None, "protocol_error", None), + (RequestError.internal_error(), 7, None, "protocol_error", 7), + (ConnectionError("Connection closed"), None, None, "connection_closed", None), + (ConnectionError("Connection closed"), 7, None, "worker_exit", 7), + (ConnectionError("Connection closed"), None, 7, "worker_exit", 7), ]) -async def test_rpc_failure_keeps_operation_and_process_facts(tmp_path, monkeypatch, error, exit_code, kind): +async def test_rpc_failure_keeps_operation_and_process_facts( + tmp_path, monkeypatch, error, returncode, exits_to, kind, exit_code +): adapter = AcpAdapter(AgentConfig(name="echo", protocol="acp", command=["echo"]), tmp_path) monkeypatch.setattr(adapter, "shutdown", AsyncMock()) + monkeypatch.setattr("agent_bridge.adapters.acp.EXIT_OBSERVE_SEC", 0.05) session = Session(session_id="sess_error", agent="echo", cwd=str(tmp_path)) live = _Live() - live.proc = SimpleNamespace(returncode=exit_code) + live.proc = _fake_proc(returncode, exits_to) adapter._live[session.session_id] = live async def fail(): @@ -140,6 +157,17 @@ async def fail(): assert len(failure.stderr_summary or "") <= 2048 +@pytest.mark.asyncio +async def test_cancel_without_native_session_skips_acp_cancel(tmp_path): + adapter = AcpAdapter(AgentConfig(name="echo", protocol="acp", command=["echo"]), tmp_path) + session = Session(session_id="sess_no_native", agent="echo", cwd=str(tmp_path)) + live = _Live() + live.conn = SimpleNamespace(cancel=AsyncMock()) + adapter._live[session.session_id] = live + await adapter.cancel(session) + live.conn.cancel.assert_not_called() + + @pytest.mark.asyncio async def test_rpc_cancellation_is_not_wrapped_as_failure(tmp_path): adapter = AcpAdapter(AgentConfig(name="echo", protocol="acp", command=["echo"]), tmp_path) From ec83e68c142ffc0347a90b9bf893735ba4ee29e5 Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 6 Oct 2026 18:24:12 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E4=BD=8E=E4=BC=98=E7=A1=AC=E5=8C=96?= =?UTF-8?q?=EF=BC=9A=E5=AD=A4=E5=84=BF=E7=BB=93=E6=9E=9C=E6=B8=85=E6=89=AB?= =?UTF-8?q?=E3=80=81=E8=BD=AC=E5=BD=95=E5=AE=B9=E9=94=99=E8=AF=BB=E3=80=81?= =?UTF-8?q?worker=20home=20=E6=B4=BE=E7=94=9F=E3=80=81=E8=B7=A8=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=E8=AF=BB=20(#32)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - start() 清扫 results/ 中无任务行引用的结果文件;state 被隔离、锁超时或读取失败时跳过,清扫前在锁内重读任务行,不误删兄弟实例结果 - read_events 用 errors=replace,坏字节行按格式错误跳过 - worker 嵌套 AGENT_BRIDGE_HOME 从注册表 home 派生 - check_task/get_result 对非本实例任务回落读取 state.json,返回 from_state=true,silent_for_sec 为 null --- src/agent_bridge/adapters/acp.py | 2 +- src/agent_bridge/adapters/antigravity.py | 2 +- src/agent_bridge/adapters/codex.py | 2 +- src/agent_bridge/registry.py | 90 +++++++++++++++++++++++- src/agent_bridge/transcript.py | 5 +- src/agent_bridge/worker_env.py | 6 +- tests/test_registry.py | 72 +++++++++++++++++++ tests/test_transcript.py | 13 ++++ tests/test_worker_env.py | 15 ++++ 9 files changed, 199 insertions(+), 8 deletions(-) diff --git a/src/agent_bridge/adapters/acp.py b/src/agent_bridge/adapters/acp.py index 89dfad6..3b066f3 100644 --- a/src/agent_bridge/adapters/acp.py +++ b/src/agent_bridge/adapters/acp.py @@ -689,7 +689,7 @@ def __init__(self, agent: AgentConfig, home: Path, env_config=None) -> None: self._cursor_models_cache: dict[str, str] | None = None def _env(self) -> dict[str, str]: - env = build_worker_env(self.agent.env, config=self.env_config, worker_context=True) + env = build_worker_env(self.agent.env, config=self.env_config, worker_context=True, home=self.home) if self.agent.name == "claude": return apply_claude_gateway_env(env) if self.agent.name == "devin": diff --git a/src/agent_bridge/adapters/antigravity.py b/src/agent_bridge/adapters/antigravity.py index 365ff4d..ed00281 100644 --- a/src/agent_bridge/adapters/antigravity.py +++ b/src/agent_bridge/adapters/antigravity.py @@ -309,7 +309,7 @@ async def run_turn(self, session: Session, task: Task) -> TurnResult: conversation (those counters cover every turn so far); otherwise ``turn``. """ cmd = self._build_cmd(session, task) - env = build_worker_env(self.agent.env, config=self.env_config, worker_context=True) + env = build_worker_env(self.agent.env, config=self.env_config, worker_context=True, home=self.home) kwargs: dict[str, Any] = {} if sys.platform == "win32": kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP diff --git a/src/agent_bridge/adapters/codex.py b/src/agent_bridge/adapters/codex.py index 5dc7e26..4f6883b 100644 --- a/src/agent_bridge/adapters/codex.py +++ b/src/agent_bridge/adapters/codex.py @@ -48,7 +48,7 @@ async def ensure_session(self, session: Session) -> None: return None def _worker_env(self) -> dict[str, str]: - return build_worker_env(self.agent.env, config=self.env_config, worker_context=True) + return build_worker_env(self.agent.env, config=self.env_config, worker_context=True, home=self.home) def _base_cmd(self, env: dict[str, str] | None = None) -> list[str]: return resolve_codex_command(self.agent.command, self.agent.fallback_commands, env=env) diff --git a/src/agent_bridge/registry.py b/src/agent_bridge/registry.py index ca10342..5cfa34e 100644 --- a/src/agent_bridge/registry.py +++ b/src/agent_bridge/registry.py @@ -159,6 +159,7 @@ def __init__( self._pending_state: dict[str, list[dict]] | None = None self._flush_task: asyncio.Task[None] | None = None self._state_error: Exception | None = None + self._state_quarantined = False @classmethod def create( @@ -355,6 +356,7 @@ def _load_state(self) -> dict[str, Any]: ) os.replace(path, quarantine) log.warning("moved unreadable %s to %s; starting from empty state", path, quarantine) + self._state_quarantined = True return {} if not isinstance(payload, dict): log.warning("ignoring non-object %s; starting from empty state", path) @@ -366,6 +368,13 @@ async def start(self) -> None: install_host_env(self.config.env) reap_orphans(self.home) payload = self._load_state() + foreign_task_ids = { + raw["task_id"] + for raw in payload.get("tasks") or [] + if isinstance(raw, dict) + and isinstance(raw.get("task_id"), str) + and self._foreign_live(raw.get("owner_pid"), raw.get("owner_create_time")) + } for raw in payload.get("sessions") or []: session = Session.model_validate(raw) if self._foreign_live(session.owner_pid, session.owner_create_time): @@ -394,6 +403,13 @@ async def start(self) -> None: done = asyncio.Event() done.set() self._done[task.task_id] = done + if self._state_quarantined: + # The tasks map is empty only because state was quarantined, not + # because the tasks are gone: sweeping now would delete result + # files whose rows live in the corrupt file (review). + log.warning("state.json was quarantined; skipping orphan result sweep") + else: + self._sweep_orphan_results(foreign_task_ids) # Stamp adopted owners onto disk first so a following prune is not # undone by _merge_owned treating the old unowned rows as foreign. self.save() @@ -890,6 +906,49 @@ async def _cancel_stalled(self, adapter: Adapter, session: Session, task_id: str except Exception: log.exception("stall cancel failed for task %s", task_id) + def _sweep_orphan_results(self, foreign_task_ids: set[str]) -> None: + """Remove result files whose task never reached state.json. + + A crash between the result write and the final save() leaves a file + no task row ever references (A1). Rows owned by a live sibling + instance are not in self.tasks, so their files are kept via + foreign_task_ids. + """ + results_dir = self.home / "results" + if not results_dir.is_dir(): + return + # Re-read state under the same lock writers use: a sibling may have + # written a task row between the startup snapshot and this sweep — + # deleting its result file would destroy a live artifact (review). + path = state_path(self.home) + lock_path = path.with_name(f"{path.name}.lock") + try: + with file_lock(lock_path, timeout_sec=STATE_LOCK_TIMEOUT_SEC): + disk = read_json_strict(path, {}) + except (OSError, TimeoutError, json.JSONDecodeError, UnicodeDecodeError): + # Unknown task set — deleting on a stale view risks live results. + # FileLockTimeout is a TimeoutError: a slow sibling writer must not + # abort startup over an optional sweep. + log.warning("could not re-read %s; skipping orphan result sweep", path) + return + known = set(self.tasks) | foreign_task_ids + if isinstance(disk, dict): + for row in disk.get("tasks") or []: + if isinstance(row, dict) and isinstance(row.get("task_id"), str): + known.add(row["task_id"]) + # task ids are "task_" (_new_id); narrowing the glob avoids + # touching unrelated .txt files dropped in results/. + for orphan in results_dir.glob("task_*.txt"): + task_id = orphan.stem + if task_id in known: + continue + try: + orphan.unlink() + except OSError: + log.warning("could not remove orphan result %s", orphan) + else: + log.info("removed orphan result %s (no task row)", orphan.name) + def _drop_task(self, task_id: str) -> None: self.tasks.pop(task_id, None) self._requests = { @@ -991,8 +1050,29 @@ async def _idle() -> None: self._idle[session_id] = asyncio.create_task(_idle(), name=f"idle-{session_id}") - def _require_task(self, task_id: str) -> Task: + def _task_from_disk(self, task_id: str) -> Task | None: + """Last-persisted row for a task this instance does not own. + + _merge_owned keeps sibling instances' rows in state.json, so a task + unknown in memory can still answer read calls from its persisted + state (F5). Lifecycle calls deliberately stay memory-only. + """ + try: + rows = self._load_state().get("tasks") or [] + except Exception: + return None + for raw in rows: + if isinstance(raw, dict) and raw.get("task_id") == task_id: + try: + return Task.model_validate(raw) + except ValueError: + return None + return None + + def _require_task(self, task_id: str, *, allow_disk: bool = False) -> Task: task = self.tasks.get(task_id) + if task is None and allow_disk: + task = self._task_from_disk(task_id) if task is None: raise KeyError(f"unknown task {task_id}") return task @@ -1079,6 +1159,10 @@ def _task_snapshot(self, task: Task, include_result: bool = False) -> dict: "finished_at": task.finished_at, "recent_activity": recent_activity(events), } + if task.task_id not in self.tasks: + # Read via the disk fallback: values are the other instance's + # last-persisted state, not live data. + payload["from_state"] = True if task.started_at: start = datetime.fromisoformat(task.started_at) end = datetime.fromisoformat(task.finished_at) if task.finished_at else datetime.fromisoformat(iso()) @@ -1117,7 +1201,7 @@ async def wait_task(self, task_id: str, timeout_sec: float = DEFAULT_WAIT_SEC) - return {"timed_out": False, **self._task_snapshot(self.tasks[task_id], include_result=True)} def check_task(self, task_id: str) -> dict: - return self._task_snapshot(self._require_task(task_id)) + return self._task_snapshot(self._require_task(task_id, allow_disk=True)) def get_result( self, @@ -1129,7 +1213,7 @@ def get_result( raise ValueError("cursor must be non-negative") if not 1 <= max_chars <= RESULT_PAGE_MAX_CHARS: raise ValueError(f"max_chars must be between 1 and {RESULT_PAGE_MAX_CHARS}") - task = self._require_task(task_id) + task = self._require_task(task_id, allow_disk=True) path = result_path(task.task_id, self.home) artifact = path.is_file() try: diff --git a/src/agent_bridge/transcript.py b/src/agent_bridge/transcript.py index dfd3b1d..b2acefc 100644 --- a/src/agent_bridge/transcript.py +++ b/src/agent_bridge/transcript.py @@ -145,7 +145,10 @@ def read_events(session_id: str, home: Path | None = None) -> list[dict[str, Any if cached is not None and cached[0] == key: return cached[1] events: list[dict[str, Any]] = [] - persisted = path.read_text(encoding="utf-8") if path.is_file() else "" + # Torn bytes from a crash mid-write must not kill the read path; the + # mangled tail line then fails JSON parsing and is skipped like any + # other malformed line (read_events_tail already decodes this way). + persisted = path.read_text(encoding="utf-8", errors="replace") if path.is_file() else "" for line in (persisted + pending).splitlines(): line = line.strip() if not line: diff --git a/src/agent_bridge/worker_env.py b/src/agent_bridge/worker_env.py index 1b72507..7790e2e 100644 --- a/src/agent_bridge/worker_env.py +++ b/src/agent_bridge/worker_env.py @@ -518,6 +518,7 @@ def build_worker_env( machine_env: Mapping[str, str] | None = None, log_fill: bool = True, worker_context: bool = False, + home: Path | None = None, ) -> dict[str, str]: env, origin = resolve_env( config, @@ -537,7 +538,10 @@ def build_worker_env( if worker_context: env[WORKER_CONTEXT_ENV] = WORKER_CONTEXT_VALUE origin[WORKER_CONTEXT_ENV] = "worker-context" - env["AGENT_BRIDGE_HOME"] = str(nested_bridge_home(bridge_home())) + # The nested home derives from the registry home, not the env var: + # a Bridge launched with an explicit home= would otherwise send the + # worker's nested Bridge to a different directory (G2). + env["AGENT_BRIDGE_HOME"] = str(nested_bridge_home(home) if home is not None else nested_bridge_home(bridge_home())) origin["AGENT_BRIDGE_HOME"] = "worker-context" if log_fill: status = describe_env(config, env=env, origin=origin) diff --git a/tests/test_registry.py b/tests/test_registry.py index ec011eb..aac79c4 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -341,6 +341,78 @@ async def test_sibling_instances_do_not_clobber_state(bridge_home, tmp_path, mon await a.stop() +@pytest.mark.asyncio +async def test_start_sweeps_orphan_result_files(bridge_home, monkeypatch): + """A crash between the result write and the final save() leaves a file + no task row references (A1). Files of live sibling tasks are kept.""" + monkeypatch.setattr("agent_bridge.registry.owner_alive", lambda pid, create_time: True) + foreign = Task( + task_id="task_foreign", + session_id="sess_foreign", + agent="fake", + message="x", + cwd=str(Path.cwd()), + status=TaskStatus.completed, + finished_at=iso(), + owner_pid=9999, + owner_create_time=1.0, + ) + atomic_write_json( + state_path(bridge_home), + {"sessions": [], "tasks": [foreign.model_dump(mode="json")]}, + ) + results = bridge_home / "results" + results.mkdir(parents=True, exist_ok=True) + (results / "task_orphan.txt").write_text("orphan", encoding="utf-8") + (results / "task_foreign.txt").write_text("kept", encoding="utf-8") + + registry = Registry.create(bridge_home) + await registry.start() + try: + assert not (results / "task_orphan.txt").exists() + assert (results / "task_foreign.txt").read_text(encoding="utf-8") == "kept" + finally: + await registry.stop() + + +@pytest.mark.asyncio +async def test_check_task_reads_sibling_task_from_state(bridge_home, monkeypatch): + """F5: a second instance answers read calls from the persisted row. + Lifecycle calls (wait/cancel) stay owner-only.""" + monkeypatch.setattr("agent_bridge.registry.owner_alive", lambda pid, create_time: True) + foreign = Task( + task_id="task_remote", + session_id="sess_remote", + agent="fake", + message="done elsewhere", + cwd=str(Path.cwd()), + status=TaskStatus.completed, + result_text="remote result", + finished_at=iso(), + owner_pid=9999, + owner_create_time=1.0, + ) + atomic_write_json( + state_path(bridge_home), + {"sessions": [], "tasks": [foreign.model_dump(mode="json")]}, + ) + registry = Registry.create(bridge_home) + await registry.start() + try: + assert "task_remote" not in registry.tasks + snap = registry.check_task("task_remote") + assert snap["status"] == "completed" + assert snap["from_state"] is True + result = registry.get_result("task_remote") + assert result["result_text"] == "remote result" + with pytest.raises(KeyError, match="unknown task"): + await registry.wait_task("task_remote", timeout_sec=0.1) + with pytest.raises(KeyError, match="unknown task"): + registry._require_task("task_remote") + finally: + await registry.stop() + + def test_sibling_state_updates_are_serialized(bridge_home, tmp_path, monkeypatch): monkeypatch.setattr("agent_bridge.registry.owner_alive", lambda pid, create_time: True) original_read = read_json_strict diff --git a/tests/test_transcript.py b/tests/test_transcript.py index d25d9b4..cdf1f90 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -151,6 +151,19 @@ def test_worker_activity_clock(bridge_home): assert worker_silence_sec("sess_act", bridge_home) is None +def test_read_events_tolerates_torn_utf8_tail(bridge_home): + """A crash mid-write must not break every later read (H-09): the torn + tail is skipped like any other malformed line, good events survive.""" + path = transcript_path("sess_torn", bridge_home) + path.parent.mkdir(parents=True, exist_ok=True) + good = json.dumps({"type": "message_chunk", "data": {"text": "ok"}}).encode("utf-8") + path.write_bytes(good + b"\n" + b'{"type": "message_chunk", "data": {"text": "\xff\xfe') + + events = read_events("sess_torn", bridge_home) + + assert [event["data"]["text"] for event in events] == ["ok"] + + def test_parse_cache_is_bounded(bridge_home): transcript._parse_cache.clear() for index in range(6): diff --git a/tests/test_worker_env.py b/tests/test_worker_env.py index aee452a..c952443 100644 --- a/tests/test_worker_env.py +++ b/tests/test_worker_env.py @@ -168,6 +168,21 @@ def test_worker_context_forced_after_overrides(tmp_path, monkeypatch): assert env["AGENT_BRIDGE_HOME"] == str((tmp_path / "nested").resolve()) +def test_worker_context_pins_nested_home_to_registry_home(tmp_path, monkeypatch): + # A Bridge started with an explicit home= must derive the worker's nested + # dir from that home, not from the process env var (G2). + monkeypatch.setenv("AGENT_BRIDGE_HOME", str(tmp_path / "from_env")) + env = build_worker_env( + base={}, + user_env={}, + machine_env={}, + log_fill=False, + worker_context=True, + home=tmp_path / "from_registry", + ) + assert env["AGENT_BRIDGE_HOME"] == str((tmp_path / "from_registry" / "nested").resolve()) + + def test_default_build_worker_env_does_not_add_mark(): env = build_worker_env( base={WORKER_CONTEXT_ENV: "coordinator"}, From b515ff3d76731836b7025e0f853c4cc199a6eb65 Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 6 Oct 2026 18:48:37 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20state.json=20=E6=8C=81?= =?UTF-8?q?=E4=B9=85=E5=8C=96=E7=AB=9E=E6=80=81=E3=80=81=E8=B4=9F=E8=BD=BD?= =?UTF-8?q?=E8=86=A8=E8=83=80=E4=B8=8E=E5=B9=82=E7=AD=89=E9=87=8D=E6=94=BE?= =?UTF-8?q?=20(#29)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - flush_state 按调用时的保存代数等待并返回是否落盘,持续保存时不再误报或卡住 - start/stop 落盘失败仍直接报错;wait_task 终态先落盘,并返回 persisted - state.json 中任务 message/result_text 截断保存,全文仍在结果文件与转录 - request_id 绑定持久化,重启后重放返回 reused - 空闲卸载成功后移除 adapter 映射,已卸载会话可被正常淘汰 --- src/agent_bridge/registry.py | 162 ++++++++++++++++++++++++++++++----- tests/test_registry.py | 136 ++++++++++++++++++++++++++++- tests/test_request_id.py | 21 +++++ 3 files changed, 295 insertions(+), 24 deletions(-) diff --git a/src/agent_bridge/registry.py b/src/agent_bridge/registry.py index 5cfa34e..7b14279 100644 --- a/src/agent_bridge/registry.py +++ b/src/agent_bridge/registry.py @@ -2,6 +2,7 @@ import asyncio import contextlib +import hashlib import json import logging import os @@ -96,6 +97,18 @@ STATE_WRITE_ATTEMPTS = 5 STATE_WRITE_RETRY_BASE_SEC = 0.05 STATE_LOCK_TIMEOUT_SEC = 5.0 +# flush_state() waits for the saves issued at call time; write failures +# degrade to a logged warning instead of raising into the caller. +STATE_FLUSH_ATTEMPTS = 3 +# Poll cadence while flush_state() waits on the shared flusher; small so a +# terminal wait_task does not add up to a full second under save traffic. +STATE_FLUSH_POLL_SEC = 0.05 +# state.json is an index, not a data store: task message/result bodies are +# persisted truncated so a long-lived Bridge keeps each write bounded. +STATE_MESSAGE_MAX = 2000 +STATE_RESULT_TEXT_MAX = 6000 +# Persisted request_id bindings kept; bindings also drop with their task. +REQUEST_KEEP_MAX = 1000 def _new_id(prefix: str) -> str: @@ -124,6 +137,11 @@ def _tail(text: str, limit: int = RESULT_TAIL) -> str: return encoded[-limit:].decode("utf-8", errors="ignore") +def _request_fingerprint(request: tuple) -> str: + blob = json.dumps(list(request), ensure_ascii=False, default=str, separators=(",", ":")) + return hashlib.sha256(blob.encode("utf-8")).hexdigest() + + class Registry: def __init__( self, @@ -138,7 +156,9 @@ def __init__( self.config = config self.sessions: dict[str, Session] = {} self.tasks: dict[str, Task] = {} - self._requests: dict[str, tuple[tuple, str]] = {} + # request_id -> (request fingerprint, task_id). Fingerprints instead + # of request tuples so bindings can persist to state.json compactly. + self._requests: dict[str, tuple[str, str]] = {} self._adapters: dict[str, Adapter] = {} self._done: dict[str, asyncio.Event] = {} self._idle: dict[str, asyncio.Task[None]] = {} @@ -157,7 +177,10 @@ def __init__( self._quota_cache = QuotaCache(config.quota.cache_sec) self._stopping = False self._pending_state: dict[str, list[dict]] | None = None + self._pending_gen = 0 self._flush_task: asyncio.Task[None] | None = None + self._save_gen = 0 + self._flushed_gen = 0 self._state_error: Exception | None = None self._state_quarantined = False @@ -216,10 +239,33 @@ def _merge_owned( merged.update(mine) return list(merged.values()) + @staticmethod + def _state_task_row(task: Task) -> dict: + row = task.model_dump(mode="json") + # Full bodies already live in result files and transcripts; writing + # them into state.json again grew the payload without bound. + if len(row["message"]) > STATE_MESSAGE_MAX: + row["message"] = row["message"][:STATE_MESSAGE_MAX] + row["result_text"] = _tail(row["result_text"], STATE_RESULT_TEXT_MAX) + return row + def _own_rows(self) -> dict[str, list[dict]]: + request_rows = [ + { + "request_id": rid, + "task_id": tid, + "fingerprint": fp, + # Owner stamps let _merge_owned drop rows this instance pruned; + # without them pruned bindings linger on disk forever. + "owner_pid": self._owner_pid, + "owner_create_time": self._owner_create_time, + } + for rid, (fp, tid) in self._requests.items() + ][-REQUEST_KEEP_MAX:] return { "sessions": [s.model_dump(mode="json") for s in self.sessions.values()], - "tasks": [t.model_dump(mode="json") for t in self.tasks.values()], + "tasks": [self._state_task_row(t) for t in self.tasks.values()], + "requests": request_rows, } def _write_state(self, own: dict[str, list[dict]]) -> None: @@ -246,6 +292,11 @@ def _write_state(self, own: dict[str, list[dict]]) -> None: {row["task_id"]: row for row in own["tasks"]}, "task_id", ), + "requests": self._merge_owned( + disk.get("requests") or [], + {row["request_id"]: row for row in own["requests"]}, + "request_id", + ), }, ) return @@ -256,6 +307,8 @@ def _write_state(self, own: dict[str, list[dict]]) -> None: def save(self) -> None: self._pending_state = self._own_rows() + self._save_gen += 1 + self._pending_gen = self._save_gen try: loop = asyncio.get_running_loop() except RuntimeError: @@ -264,8 +317,10 @@ def save(self) -> None: self._write_state(own) except Exception as exc: self._pending_state = own + self._pending_gen = self._save_gen self._state_error = exc raise + self._flushed_gen = self._save_gen self._state_error = None return if self._flush_task is None or self._flush_task.done(): @@ -276,28 +331,62 @@ async def _flush_state_loop(self) -> None: # save() cannot observe a still-running flush task and skip creating # a new one after this loop has already decided to exit. while self._pending_state is not None: - own, self._pending_state = self._pending_state, None + own, gen = self._pending_state, self._pending_gen + self._pending_state = None try: await asyncio.to_thread(self._write_state, own) except Exception as exc: # Keep the newest snapshot. A later save or flush can retry it. if self._pending_state is None: self._pending_state = own + self._pending_gen = gen self._state_error = exc log.exception("could not write state.json") return + self._flushed_gen = gen self._state_error = None - async def flush_state(self) -> None: - """Wait until every save() so far is on disk.""" - task = self._flush_task - if self._pending_state is not None and (task is None or task.done()): - task = asyncio.create_task(self._flush_state_loop(), name="state-flush") - self._flush_task = task - if task is not None and not task.done(): - await task - if self._pending_state is not None: - raise RuntimeError("could not persist state.json") from self._state_error + async def flush_state(self) -> bool: + """Wait until every save() issued so far is on disk. + + Generation-scoped and returns True iff that generation landed on + disk: a save() issued *after* this call does not block it, so + concurrent saves can no longer trigger the spurious "could not + persist state.json" raise. Persistent write failures degrade to a + logged warning — a broken disk must not kill coordination; the next + save() retries the same rows because snapshots are cumulative. + Callers that cannot proceed on stale disk (start, stop) check the + return value; wait_task reports it as ``persisted``. + """ + target = self._save_gen + for _ in range(STATE_FLUSH_ATTEMPTS): + while self._flushed_gen < target: + task = self._flush_task + fresh = False + if task is None or task.done(): + if self._pending_state is None: + break + task = asyncio.create_task(self._flush_state_loop(), name="state-flush") + self._flush_task = task + fresh = True + # Poll the shared flusher instead of awaiting it to the end: + # under steady save() traffic the loop keeps draining newer + # snapshots, but our target generation is already on disk. + await asyncio.wait({task}, timeout=STATE_FLUSH_POLL_SEC) + if fresh and task.done(): + break # this attempt's run finished; retry or give up + if self._flushed_gen >= target: + return True + if self._pending_state is None: + break + if self._flushed_gen < target: + log.warning( + "state.json persistence degraded (flushed %s of %s saves): %s", + self._flushed_gen, + target, + self._state_error, + ) + return False def touch_activity(self) -> None: self._last_activity = time.monotonic() @@ -403,6 +492,14 @@ async def start(self) -> None: done = asyncio.Event() done.set() self._done[task.task_id] = done + for raw in payload.get("requests") or []: + if not isinstance(raw, dict): + continue + rid = raw.get("request_id") + tid = raw.get("task_id") + fp = raw.get("fingerprint") + if rid and fp and tid in self.tasks: + self._requests[rid] = (fp, tid) if self._state_quarantined: # The tasks map is empty only because state was quarantined, not # because the tasks are gone: sweeping now would delete result @@ -413,10 +510,12 @@ async def start(self) -> None: # Stamp adopted owners onto disk first so a following prune is not # undone by _merge_owned treating the old unowned rows as foreign. self.save() - await self.flush_state() + if not await self.flush_state(): + raise RuntimeError("could not persist state.json") from self._state_error self._prune() self.save() - await self.flush_state() + if not await self.flush_state(): + raise RuntimeError("could not persist state.json") from self._state_error self.touch_activity() if self.config.server.idle_exit_sec > 0: self._watchdog = asyncio.create_task( @@ -431,9 +530,15 @@ async def stop(self) -> None: self._watchdog = None if watchdog is not None: watchdog.cancel() - for idle in list(self._idle.values()): + idles = list(self._idle.values()) + for idle in idles: idle.cancel() self._idle.clear() + if idles: + # Await cancellation so an in-flight idle unload finishes + # unwinding before adapter shutdown — otherwise the loop may close + # with a worker half-reaped. + await asyncio.wait(idles, timeout=STOP_TASK_GRACE_SEC) bgs = [task for task in self._bg.values() if not task.done()] for bg in bgs: bg.cancel() @@ -460,7 +565,8 @@ async def stop(self) -> None: except OSError: log.exception("could not flush transcripts during shutdown") self.save() - await self.flush_state() + if not await self.flush_state(): + raise RuntimeError("could not persist state.json") from self._state_error def _adapter_for(self, session: Session) -> Adapter: existing = self._adapters.get(session.session_id) @@ -611,8 +717,8 @@ async def dispatch_task( request = (agent, message, str(cwd_path.resolve()), session_id, model, effort, title, user_requested) async with self._lock: if request_id is not None and request_id in self._requests: - previous, task_id = self._requests[request_id] - if previous != request: + previous_fp, task_id = self._requests[request_id] + if previous_fp != _request_fingerprint(request): raise ValueError("request_id is already bound to a different dispatch request") task = self.tasks[task_id] return { @@ -674,7 +780,7 @@ async def dispatch_task( self._stamp_owner(task) self.tasks[task.task_id] = task if request_id is not None: - self._requests[request_id] = (request, task.task_id) + self._requests[request_id] = (_request_fingerprint(request), task.task_id) self._done[task.task_id] = asyncio.Event() self._cancel_idle(session.session_id) self._prune() @@ -1045,6 +1151,11 @@ async def _idle() -> None: await adapter.shutdown(current) except Exception: log.exception("idle unload failed for %s", session_id) + else: + # Drop the dead adapter mapping: keeping it excludes the + # session from inactive pruning (the filter requires + # `not in _adapters`); revival rebuilds via _adapter_for. + self._adapters.pop(session_id, None) current.proc_state = ProcState.idle_unloaded self.save() @@ -1198,7 +1309,16 @@ async def wait_task(self, task_id: str, timeout_sec: float = DEFAULT_WAIT_SEC) - await asyncio.wait_for(event.wait(), timeout=timeout_sec) except TimeoutError: return {"timed_out": True, **self._task_snapshot(self.tasks[task_id])} - return {"timed_out": False, **self._task_snapshot(self.tasks[task_id], include_result=True)} + persisted = False + if task.status in TERMINAL_STATUSES: + # A terminal answer must also be on disk, not merely queued + # behind the background flusher. + persisted = await self.flush_state() + return { + "timed_out": False, + "persisted": persisted, + **self._task_snapshot(self.tasks[task_id], include_result=True), + } def check_task(self, task_id: str) -> dict: return self._task_snapshot(self._require_task(task_id, allow_disk=True)) diff --git a/tests/test_registry.py b/tests/test_registry.py index aac79c4..0228b82 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -476,14 +476,144 @@ def fail_once(own): monkeypatch.setattr(registry, "_write_state", fail_once) registry.save() - with pytest.raises(RuntimeError, match=r"could not persist state\.json"): - await registry.flush_state() + # flush_state degrades instead of raising: the failed write is retried + # by the same call (STATE_FLUSH_ATTEMPTS) and the snapshot lands. + await registry.flush_state() + assert registry._pending_state is None + payload = read_json(state_path(bridge_home), {}) + assert {row["session_id"] for row in payload["sessions"]} == {"sess_retry"} + + +@pytest.mark.asyncio +async def test_persistent_state_write_failure_degrades_flush_without_raising( + bridge_home, tmp_path, monkeypatch +): + registry = Registry.create(bridge_home) + registry.sessions["sess_dead_disk"] = Session( + session_id="sess_dead_disk", + agent="fake", + cwd=str(tmp_path), + ) + original_write = registry._write_state + + def fail_always(own): + raise PermissionError(5, "synthetic sharing/access failure") + + monkeypatch.setattr(registry, "_write_state", fail_always) + registry.save() + + # Never raises: a broken disk must not kill the caller. The newest + # snapshot is kept for a later retry and surfaced via _state_error. + await registry.flush_state() + assert registry._state_error is not None assert registry._pending_state is not None + monkeypatch.setattr(registry, "_write_state", original_write) await registry.flush_state() assert registry._pending_state is None + assert registry._state_error is None payload = read_json(state_path(bridge_home), {}) - assert {row["session_id"] for row in payload["sessions"]} == {"sess_retry"} + assert {row["session_id"] for row in payload["sessions"]} == {"sess_dead_disk"} + + +@pytest.mark.asyncio +async def test_flush_state_ignores_saves_issued_during_the_wait( + bridge_home, tmp_path, monkeypatch +): + """A save() landing mid-flush must not trip a spurious persist error.""" + import threading + + registry = Registry.create(bridge_home) + registry.sessions["sess_race"] = Session( + session_id="sess_race", + agent="fake", + cwd=str(tmp_path), + ) + original_write = registry._write_state + started = threading.Event() + gate = threading.Event() + + def slow_write(own): + started.set() + gate.wait(10) + original_write(own) + + monkeypatch.setattr(registry, "_write_state", slow_write) + registry.save() + flush = asyncio.create_task(registry.flush_state()) + await asyncio.to_thread(started.wait, 10) # first write in-flight, blocked + registry.save() # lands while flush_state is awaiting the first write + gate.set() + await asyncio.wait_for(flush, timeout=10) # must not raise + assert registry._flushed_gen >= 1 + + +@pytest.mark.asyncio +async def test_wait_task_persisted_ignores_saves_issued_during_the_wait( + bridge_home, tmp_path, monkeypatch +): + """persisted answers whether this task's save landed; a save() issued by + another task while the flush runs must not false-negative it.""" + work = tmp_path / "work" + work.mkdir() + registry = Registry.create(bridge_home) + await registry.start() + try: + dispatched = await registry.dispatch_task("fake", "tiny", cwd=str(work.resolve())) + original_flush = registry.flush_state + + async def flush_then_save(): + done = await original_flush() + registry.save() # a newer save is in flight when persisted is read + return done + + monkeypatch.setattr(registry, "flush_state", flush_then_save) + waited = await registry.wait_task(dispatched["task_id"], timeout_sec=5) + assert waited["status"] == "completed" + assert waited["persisted"] is True + finally: + await registry.stop() + + +@pytest.mark.asyncio +async def test_wait_task_reports_unpersisted_when_writes_fail( + bridge_home, tmp_path, monkeypatch +): + """A broken disk degrades wait_task to persisted=False instead of raising.""" + work = tmp_path / "work" + work.mkdir() + registry = Registry.create(bridge_home) + await registry.start() + original_write = registry._write_state + try: + dispatched = await registry.dispatch_task("fake", "tiny", cwd=str(work.resolve())) + waited = await registry.wait_task(dispatched["task_id"], timeout_sec=5) + assert waited["persisted"] is True + + def fail_always(own): + raise PermissionError(5, "synthetic sharing/access failure") + + monkeypatch.setattr(registry, "_write_state", fail_always) + registry.save() + degraded = await registry.wait_task(dispatched["task_id"], timeout_sec=5) + assert degraded["status"] == "completed" + assert degraded["persisted"] is False + finally: + monkeypatch.setattr(registry, "_write_state", original_write) + await registry.stop() + + +@pytest.mark.asyncio +async def test_start_raises_when_state_never_persists(bridge_home, monkeypatch): + """start() cannot adopt/merge on stale disk, so it still fails fast.""" + registry = Registry.create(bridge_home) + + def fail_always(own): + raise PermissionError(5, "synthetic sharing/access failure") + + monkeypatch.setattr(registry, "_write_state", fail_always) + with pytest.raises(RuntimeError, match=r"could not persist state\.json"): + await registry.start() def test_state_write_retries_bounded_permission_errors(bridge_home, monkeypatch): diff --git a/tests/test_request_id.py b/tests/test_request_id.py index d10dc71..14a13f0 100644 --- a/tests/test_request_id.py +++ b/tests/test_request_id.py @@ -88,6 +88,27 @@ async def test_pruning_drops_binding(bridge_home, tmp_path, monkeypatch): await registry.stop() +async def test_request_id_survives_restart(bridge_home, tmp_path): + """Bindings persist to state.json so a replay after restart reuses the task.""" + registry = Registry.create(bridge_home) + await registry.start() + request_id = str(uuid.uuid4()) + args = dict(agent="fake", message="hello", cwd=str(tmp_path), request_id=request_id) + first = await registry.dispatch_task(**args) + await registry.wait_task(first["task_id"], timeout_sec=5) + await registry.stop() + + registry2 = Registry.create(bridge_home) + await registry2.start() + try: + retry = await registry2.dispatch_task(**args) + assert retry == {**first, "reused": True} + with pytest.raises(ValueError, match="different dispatch request"): + await registry2.dispatch_task(**{**args, "message": "different"}) + finally: + await registry2.stop() + + async def test_uuid_validation_and_canonicalization(bridge_home, tmp_path): registry = Registry.create(bridge_home) await registry.start() From 3e26fbaec63236d76b3b9dac31424d5b6f750ef2 Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 6 Oct 2026 18:49:17 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E5=8F=AF=E8=A7=82?= =?UTF-8?q?=E6=B5=8B=E6=80=A7=E4=B8=8E=E5=BD=92=E5=9B=A0=EF=BC=9Asession?= =?UTF-8?q?=5Frecreated=E3=80=81=E5=8F=8C=E8=BD=A8=E7=9C=8B=E9=97=A8?= =?UTF-8?q?=E7=8B=97=E3=80=81=E8=B7=AF=E5=BE=84=E5=BD=92=E5=9B=A0=20(#31)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - session/load 失败回退到 session/new 成功后,写入 warning 与 session_recreated 转录事件 - 只有 worker 自身输出重置 stall 计时;新增 turn_timeout_sec(默认 0 关闭),超时 stop_reason=timed_out - files_changed 只统计 cwd 内路径,越界路径进入 files_outside_cwd;跳过 .agent-bridge 与 Windows junction - 会话淘汰时清理转录缓存与文件;stdout 超长行给出明确错误 --- src/agent_bridge/adapters/acp.py | 23 +++++ src/agent_bridge/adapters/antigravity.py | 17 +++- src/agent_bridge/adapters/codex.py | 17 +++- src/agent_bridge/config.py | 4 + src/agent_bridge/models.py | 4 + src/agent_bridge/registry.py | 74 ++++++++++++---- src/agent_bridge/transcript.py | 39 ++++++++- src/agent_bridge/workspace.py | 104 ++++++++++++++++++----- tests/echo_agent.py | 4 + tests/test_acp_echo.py | 50 +++++++++++ tests/test_registry.py | 50 ++++++++++- tests/test_transcript.py | 36 ++++++++ tests/test_workspace.py | 74 ++++++++++++++-- 13 files changed, 446 insertions(+), 50 deletions(-) diff --git a/src/agent_bridge/adapters/acp.py b/src/agent_bridge/adapters/acp.py index 3b066f3..0d6b91e 100644 --- a/src/agent_bridge/adapters/acp.py +++ b/src/agent_bridge/adapters/acp.py @@ -1629,6 +1629,7 @@ async def ensure_session(self, session: Session) -> None: return live = await self._spawn(session) native = session.native_session_id + load_failure: Exception | None = None if native and self.can_revive(): use_resume = self.agent.name in _RESUME_AGENTS or ( self.agent.name == "dsh" and live.dsh_native_acp @@ -1647,6 +1648,7 @@ async def ensure_session(self, session: Session) -> None: raise except Exception as exc: log.warning("session/load failed for %s; creating a new session: %s", session.session_id, _redact_diagnostic(str(exc))) + load_failure = exc else: self._remember_config_options(live, revived) await self._sync_selection(live, session) @@ -1656,7 +1658,28 @@ async def ensure_session(self, session: Session) -> None: "session/new", session, ) + # Adopt the new id before any fallible bookkeeping: a transcript write + # failure must not leave the session pointing at the rejected id. session.native_session_id = created.session_id + if load_failure is not None: + # H-05: the coordinator must see that its session lost context. + # pending_warnings lands on the next turn's task.warnings; the + # transcript event keeps the switch auditable after the fact. Both + # are emitted only once session/new has actually succeeded — a + # failed fallback is a failed task, not a recreation. + live.pending_warnings.append( + f"session/load failed ({type(load_failure).__name__}); started a new worker " + "session — prior conversation context is lost" + ) + append_event( + session.session_id, + "session_recreated", + { + "old_native_session_id": native, + "reason": _redact_diagnostic(str(load_failure)), + }, + self.home, + ) self._remember_config_options(live, created) if self.agent.name == "grok": # Grok /new applies the _meta reasoningEffort but always lands on diff --git a/src/agent_bridge/adapters/antigravity.py b/src/agent_bridge/adapters/antigravity.py index ed00281..eed8823 100644 --- a/src/agent_bridge/adapters/antigravity.py +++ b/src/agent_bridge/adapters/antigravity.py @@ -288,7 +288,13 @@ async def _drain_stderr(self, proc: asyncio.subprocess.Process, session_id: str) if text: tail = f"{tail}\n{text}"[-STDERR_TAIL_LIMIT:] except (ValueError, OSError): - log.warning("stderr drain aborted for %s", session_id, exc_info=True) + log.warning( + "stderr drain aborted for %s (line over %s MiB or stream closed); " + "later stderr evidence is lost", + session_id, + STDIO_LIMIT // (1024 * 1024), + exc_info=True, + ) return tail async def _write_prompt(self, proc: asyncio.subprocess.Process, message: str) -> None: @@ -345,7 +351,14 @@ async def run_turn(self, session: Session, task: Task) -> TurnResult: try: assert proc.stdout is not None while True: - line = await proc.stdout.readline() + try: + line = await proc.stdout.readline() + except ValueError as exc: + raise RuntimeError( + f"antigravity emitted a stdout line longer than " + f"{STDIO_LIMIT // (1024 * 1024)} MiB (asyncio stream limit); " + "the turn cannot continue" + ) from exc if not line: break raw = line.decode("utf-8", errors="replace").rstrip() diff --git a/src/agent_bridge/adapters/codex.py b/src/agent_bridge/adapters/codex.py index 4f6883b..af25379 100644 --- a/src/agent_bridge/adapters/codex.py +++ b/src/agent_bridge/adapters/codex.py @@ -76,7 +76,13 @@ async def _drain_stderr(self, proc: asyncio.subprocess.Process, session_id: str) if text: tail = f"{tail}\n{text}"[-STDERR_TAIL_LIMIT:] except (ValueError, OSError): - log.warning("stderr drain aborted for %s", session_id, exc_info=True) + log.warning( + "stderr drain aborted for %s (line over %s MiB or stream closed); " + "later stderr evidence is lost", + session_id, + STDIO_LIMIT // (1024 * 1024), + exc_info=True, + ) return tail async def _write_prompt(self, proc: asyncio.subprocess.Process, message: str) -> None: @@ -137,7 +143,14 @@ async def run_turn(self, session: Session, task: Task) -> TurnResult: await self._write_prompt(proc, task.message) assert proc.stdout is not None while True: - line = await proc.stdout.readline() + try: + line = await proc.stdout.readline() + except ValueError as exc: + raise RuntimeError( + f"codex emitted a stdout line longer than " + f"{STDIO_LIMIT // (1024 * 1024)} MiB (asyncio stream limit); " + "the turn cannot continue" + ) from exc if not line: break raw = line.decode("utf-8", errors="replace").rstrip() diff --git a/src/agent_bridge/config.py b/src/agent_bridge/config.py index e756384..ed952bd 100644 --- a/src/agent_bridge/config.py +++ b/src/agent_bridge/config.py @@ -116,6 +116,10 @@ class AgentConfig(BaseModel): revivable: bool = False idle_unload_sec: int = 0 stall_timeout_sec: int = Field(default=1800, ge=0) + # Hard cap on one turn's wall-clock length (0 = disabled). Catches a + # worker that stays chatty forever without ever going silent — + # stall_timeout_sec alone cannot see that case (H-16). + turn_timeout_sec: int = Field(default=0, ge=0) print_timeout: str = "120m" diff --git a/src/agent_bridge/models.py b/src/agent_bridge/models.py index d261fd6..98e6691 100644 --- a/src/agent_bridge/models.py +++ b/src/agent_bridge/models.py @@ -134,6 +134,10 @@ class Task(BaseModel): files_changed_total: int = 0 files_changed_truncated: bool = False files_changed_state: FilesChangedState = FilesChangedState.pending + # Paths the worker REPORTED that resolve outside task.cwd. Kept separate + # so an escape is never silently folded into an in-workspace path (E3). + files_outside_cwd: list[str] = Field(default_factory=list) + files_outside_cwd_total: int = 0 usage: dict[str, Any] = Field(default_factory=dict) error: str | None = None failure: TaskFailure | None = None diff --git a/src/agent_bridge/registry.py b/src/agent_bridge/registry.py index 7b14279..4bcf685 100644 --- a/src/agent_bridge/registry.py +++ b/src/agent_bridge/registry.py @@ -52,6 +52,7 @@ append_event, flush_pending, flush_session, + forget_session, forget_worker_activity, mark_worker_activity, page_events, @@ -816,13 +817,15 @@ async def _run_task(self, task_id: str) -> None: try: mark_worker_activity(session.session_id, self.home) before = await asyncio.to_thread(snapshot_workspace, task.cwd) - limit = self.config.get(session.agent).stall_timeout_sec + agent_cfg = self.config.get(session.agent) + limit = agent_cfg.stall_timeout_sec + turn_cap = agent_cfg.turn_timeout_sec watch = ( asyncio.create_task( - self._stall_watch(task, session, adapter, limit), + self._stall_watch(task, session, adapter, limit, turn_cap), name=f"stall-{task_id}", ) - if limit > 0 + if limit > 0 or turn_cap > 0 else None ) result = await adapter.run_turn(session, task) @@ -839,10 +842,10 @@ async def _run_task(self, task_id: str) -> None: f"full result persistence failed: {type(exc).__name__}: {exc}" ) log.exception("could not persist full result for task %s", task.task_id) - full_changed = await asyncio.to_thread( + inside_changed, outside_changed = await asyncio.to_thread( merge_files_changed, task.cwd, result.files_changed, before ) - self._set_files_changed(task, full_changed) + self._set_files_changed(task, inside_changed, outside_changed) task.usage = result.usage if session.agent == "grok": observed = await asyncio.to_thread(observe_grok_session, session.cwd, session.native_session_id) @@ -910,10 +913,10 @@ async def _run_task(self, task_id: str) -> None: task.files_changed_state = FilesChangedState.unavailable else: try: - full_changed = await asyncio.to_thread( + inside_changed, outside_changed = await asyncio.to_thread( merge_files_changed, task.cwd, [], before ) - self._set_files_changed(task, full_changed) + self._set_files_changed(task, inside_changed, outside_changed) except Exception as exc: task.files_changed_state = FilesChangedState.unavailable task.warnings.append( @@ -957,10 +960,13 @@ async def _run_task(self, task_id: str) -> None: self._schedule_idle(session.session_id) @staticmethod - def _set_files_changed(task: Task, paths: list[str]) -> None: + def _set_files_changed(task: Task, paths: list[str], outside: list[str] | None = None) -> None: task.files_changed_total = len(paths) task.files_changed = paths[:FILES_CHANGED_MAX] task.files_changed_truncated = len(paths) > FILES_CHANGED_MAX + outside = outside or [] + task.files_outside_cwd_total = len(outside) + task.files_outside_cwd = outside[:FILES_CHANGED_MAX] task.files_changed_state = FilesChangedState.collected async def _stall_watch( @@ -969,30 +975,54 @@ async def _stall_watch( session: Session, adapter: Adapter, limit: int, + turn_cap: int = 0, ) -> None: + """Dual watchdog: ``limit`` bounds worker silence, ``turn_cap`` bounds + total turn wall-clock. Silence only counts worker-originated transcript + events, so a heartbeat-spamming worker still needs the cap (H-16).""" + started = time.monotonic() while True: silence = worker_silence_sec(session.session_id, self.home) or 0.0 - remaining = limit - silence - if remaining <= 0: + elapsed = time.monotonic() - started + if limit > 0 and silence >= limit: + reason = "stalled" break - await asyncio.sleep(min(remaining, STALL_POLL_SEC)) + if turn_cap > 0 and elapsed >= turn_cap: + reason = "timed_out" + break + waits: list[float] = [STALL_POLL_SEC] + if limit > 0: + waits.append(limit - silence) + if turn_cap > 0: + waits.append(turn_cap - elapsed) + await asyncio.sleep(max(min(waits), 0.05)) if task.status in TERMINAL_STATUSES: return log.warning( - "task %s stalled: no worker output for %ss; cancelling the turn", + "task %s %s: cancelling the turn (silence=%.0fs, elapsed=%.0fs)", task.task_id, - limit, + reason, + silence, + elapsed, ) task.status = TaskStatus.failed - task.stop_reason = "stalled" + task.stop_reason = reason task.error = ( f"worker produced no output for {limit}s (stall_timeout_sec); " "Bridge cancelled the turn" + if reason == "stalled" + else f"turn exceeded turn_timeout_sec={turn_cap}s; Bridge cancelled the turn" ) append_event( session.session_id, "error", - {"error": task.error, "stalled": True, "stall_timeout_sec": limit}, + { + "error": task.error, + "stalled": reason == "stalled", + "timed_out": reason == "timed_out", + "stall_timeout_sec": limit, + "turn_timeout_sec": turn_cap, + }, self.home, ) # The turn normally returns (and _run_task tears this watch down) @@ -1085,7 +1115,9 @@ def _prune_sessions(self) -> None: for session in drop: session_id = session.session_id self.sessions.pop(session_id, None) - forget_worker_activity(session_id, self.home) + # Result files die with their tasks; the per-session transcript + # would otherwise grow unbounded on disk (H-02). + forget_session(session_id, self.home) self._cancel_idle(session_id) for task in [item for item in self.tasks.values() if item.session_id == session_id]: self._drop_task(task.task_id) @@ -1239,11 +1271,17 @@ def _result_hint(task: Task, prefix: str) -> str: ) if task.stop_reason == "stalled": hint += ( - " The worker went silent for stall_timeout_sec and Bridge cancelled the turn; " + " The worker produced no output for stall_timeout_sec and Bridge cancelled the turn; " "read get_transcript for its last activity, then either dispatch a narrower " f"task on the same session_id or raise [agents.{task.agent}] stall_timeout_sec " "if that step was legitimately long." ) + if task.stop_reason == "timed_out": + hint += ( + " The turn hit turn_timeout_sec and Bridge cancelled it; " + f"raise [agents.{task.agent}] turn_timeout_sec if turns legitimately " + "need more wall-clock time." + ) return hint def _task_snapshot(self, task: Task, include_result: bool = False) -> dict: @@ -1261,6 +1299,8 @@ def _task_snapshot(self, task: Task, include_result: bool = False) -> dict: "files_changed_total": task.files_changed_total, "files_changed_truncated": task.files_changed_truncated, "files_changed_state": task.files_changed_state.value, + "files_outside_cwd": task.files_outside_cwd, + "files_outside_cwd_total": task.files_outside_cwd_total, "model": task.model, "effort": task.effort, "observed_model": task.observed_model, diff --git a/src/agent_bridge/transcript.py b/src/agent_bridge/transcript.py index b2acefc..ed18fff 100644 --- a/src/agent_bridge/transcript.py +++ b/src/agent_bridge/transcript.py @@ -56,6 +56,21 @@ def _pending_text(path: Path) -> str: return "".join(buffer.lines) if buffer else "" +# Event types that represent bytes arriving FROM the worker (session/update +# notifications, raw JSONL, streamed chunks). Everything else in a transcript +# is written by Bridge itself and never proves the worker is alive. +WORKER_ACTIVITY_TYPES = frozenset( + { + "message_chunk", + "thought_chunk", + "tool_call", + "tool_call_update", + "plan", + "raw", + } +) + + def append_event(session_id: str, event_type: str, data: dict[str, Any] | None = None, home: Path | None = None) -> TranscriptEvent: event = TranscriptEvent(type=event_type, data=data or {}) path = transcript_path(session_id, home) @@ -73,7 +88,11 @@ def append_event(session_id: str, event_type: str, data: dict[str, Any] | None = and now - buffer.first_pending_at >= BUFFER_MAX_AGE_SEC ) terminal = event_type in {"turn_end", "error"} - _activity[path] = now + # Only worker-originated events reset the stall clock. Bridge-side + # bookkeeping (prompt_sent, turn_end, error, permission, ...) written + # into the same transcript must not count as "the worker is alive". + if event_type in WORKER_ACTIVITY_TYPES: + _activity[path] = now if buffer.size >= BUFFER_BYTE_LIMIT or aged or terminal: _flush_locked(path, buffer) if terminal and not buffer.lines: @@ -102,6 +121,24 @@ def forget_worker_activity(session_id: str, home: Path | None = None) -> None: _activity.pop(path, None) +def forget_session(session_id: str, home: Path | None = None) -> None: + """Drop every cached trace of a pruned session and delete its transcript. + + Result files die with their task; the per-session transcript would + otherwise live forever (H-02). Any unflushed buffered lines are dropped + with the file — the session is being deleted, not archived. + """ + path = transcript_path(session_id, home) + with _buffers_lock: + _activity.pop(path, None) + _buffers.pop(path, None) + _parse_cache.pop(path, None) + try: + path.unlink(missing_ok=True) + except OSError: + log.warning("could not remove pruned transcript %s", path) + + def flush_pending(home: Path | None = None) -> None: """Flush buffered transcript events, normally during Bridge shutdown.""" root = home / "transcripts" if home is not None else None diff --git a/src/agent_bridge/workspace.py b/src/agent_bridge/workspace.py index 3bf1c59..809f9d2 100644 --- a/src/agent_bridge/workspace.py +++ b/src/agent_bridge/workspace.py @@ -33,6 +33,13 @@ ".parcel-cache", ".svelte-kit", ".terraform", + # Bridge bookkeeping dir. A nested bridge home under the project churns + # every turn; counting that as project changes misattributes Bridge- + # internal writes to the worker (H-06). `.codex`/`.claude` inside a + # project are NOT skipped: they hold legitimate project config (e.g. + # .codex/config.toml) whose edits must be reported; each worker's own + # state dirs live under its home, outside the workspace cwd. + ".agent-bridge", } _PATH_KEYS = ( @@ -64,7 +71,23 @@ def snapshot_workspace(cwd: str | Path) -> dict[str, tuple[int, int]]: for entry in it: try: if entry.is_dir(follow_symlinks=False): - if entry.name not in SKIP_DIR_NAMES: + # Junctions/mounts pass is_dir(follow_symlinks=False) + # but point outside cwd; traversing them attributes + # foreign files as workspace writes (E2). + # DirEntry.is_junction needs Python 3.12; elsewhere + # compare realpath against the canonicalized parent: + # a junction resolves elsewhere while normal dirs, + # cloud (OneDrive) reparse dirs and POSIX mounts + # resolve to themselves and stay traversable. + if hasattr(entry, "is_junction"): + is_junction = entry.is_junction() + else: + expected = os.path.join( + os.path.realpath(os.path.dirname(entry.path)), + entry.name, + ) + is_junction = os.path.realpath(entry.path) != expected + if entry.name not in SKIP_DIR_NAMES and not is_junction: stack.append(entry.path) elif entry.is_file(follow_symlinks=False): st = entry.stat(follow_symlinks=False) @@ -90,48 +113,85 @@ def changed_since(cwd: str | Path, before: dict[str, tuple[int, int]]) -> list[s def normalize_changed_paths(cwd: str | Path, paths: list[str]) -> list[str]: + inside, _outside = classify_changed_paths(cwd, paths) + return inside + + +def classify_changed_paths( + cwd: str | Path, paths: list[str] +) -> tuple[list[str], list[str]]: + """Split reported paths into (inside-cwd rel, outside-cwd abs). + + A path that resolves outside the workspace is reported as its resolved + absolute form in the second list — never folded into an inside path the + way ``../x`` used to collapse to ``x`` (E3). + """ root = Path(cwd).resolve() - seen: set[str] = set() - ordered: list[str] = [] + seen_in: set[str] = set() + seen_out: set[str] = set() + inside: list[str] = [] + outside: list[str] = [] for raw in paths: if not raw or not isinstance(raw, str): continue - rel = _relative_to_cwd(raw, root) - if not rel or _ignored_rel(rel): - continue - if rel not in seen: - seen.add(rel) - ordered.append(rel) - return ordered + rel, out = _classify_path(raw, root) + if rel and not _ignored_rel(rel) and rel not in seen_in: + seen_in.add(rel) + inside.append(rel) + elif out and out not in seen_out: + seen_out.add(out) + outside.append(out) + return inside, outside def merge_files_changed( cwd: str | Path, reported: list[str], before: dict[str, tuple[int, int]], -) -> list[str]: - return normalize_changed_paths(cwd, [*reported, *changed_since(cwd, before)]) +) -> tuple[list[str], list[str]]: + """(inside-cwd rel paths, outside-cwd abs paths).""" + inside, outside = classify_changed_paths(cwd, reported) + disk = changed_since(cwd, before) + merged = inside + [rel for rel in disk if rel not in set(inside)] + return merged, outside + -def _relative_to_cwd(raw: str, root: Path) -> str | None: +def _classify_path(raw: str, root: Path) -> tuple[str | None, str | None]: + """(inside rel | None, outside absolute | None). Both None = unusable.""" text = raw.strip() if text.startswith("file://"): text = text[7:] if text.startswith("/") and len(text) >= 3 and text[2] == ":": text = text[1:] path = Path(text) - rel: str | None try: resolved = path.resolve() if path.is_absolute() else (root / path).resolve() - rel = resolved.relative_to(root).as_posix() - except (OSError, ValueError): - rel = path.as_posix().lstrip("./") if not path.is_absolute() else None + except (OSError, ValueError, RuntimeError): + resolved = None + if resolved is not None: + try: + rel = resolved.relative_to(root).as_posix() + except ValueError: + return None, resolved.as_posix() + if rel in {".", "..", ""}: + return None, None + return rel, None + # Unresolvable: keep prior lenient behavior for relative strings, but a + # leading ../ can never be proven inside — report it as outside instead + # of folding it into the workspace (E3). Check the traversal BEFORE + # lstrip, which would otherwise eat the leading ../ as well. + if not path.is_absolute(): + posix = path.as_posix() + if posix.startswith("../") or posix == "..": + # files_outside_cwd is an absolute-path field: return the lexical + # absolute form (root-joined, normalized, no symlink resolution). + return None, Path(os.path.normpath(root / path)).as_posix() + rel = posix.lstrip("./") if rel in {".", "..", ""}: - return None - return rel - if rel in {".", "..", ""}: - return None - return rel + return None, None + return rel, None + return None, path.as_posix() def _root_prefix_len(root: str) -> int: diff --git a/tests/echo_agent.py b/tests/echo_agent.py index 7ce1c88..6e9191a 100644 --- a/tests/echo_agent.py +++ b/tests/echo_agent.py @@ -65,6 +65,10 @@ async def new_session(self, cwd: str, mcp_servers=None, **kwargs: Any) -> NewSes async def load_session(self, cwd: str, session_id: str, mcp_servers=None, **kwargs: Any) -> None: if os.environ.get("BRIDGE_ECHO_FAILURE") == "load": raise RequestError.auth_required() + if os.environ.get("BRIDGE_ECHO_FAILURE") == "load_internal": + # Generic failure (not "unavailable"): Bridge should fall back to + # session/new and mark the switch visibly (H-05). + raise RequestError(-32603, "native session data corrupt") self._session_id = session_id self._ready = True return None diff --git a/tests/test_acp_echo.py b/tests/test_acp_echo.py index c744703..08116cc 100644 --- a/tests/test_acp_echo.py +++ b/tests/test_acp_echo.py @@ -222,3 +222,53 @@ def test_legacy_task_has_no_failure_evidence(): "message": "old", "status": "failed", "error": "Connection closed", }) assert task.failure is None + + +@pytest.mark.asyncio +async def test_acp_load_failure_surfaces_session_recreated(bridge_home, tmp_path, monkeypatch): + """H-05: session/load -> session/new fallback must be visible to the + coordinator (task warning) and auditable later (transcript event).""" + echo = Path(__file__).resolve().parent / "echo_agent.py" + work = tmp_path / "work" + work.mkdir() + cfg = AgentConfig( + name="echo", + protocol="acp", + command=[sys.executable, str(echo)], + revivable=True, + idle_unload_sec=0, + ) + adapter = AcpAdapter(cfg, bridge_home) + session = Session(session_id="sess_reload", agent="echo", cwd=str(work.resolve())) + first = Task( + task_id="task_reload_1", + session_id=session.session_id, + agent="echo", + message="one", + cwd=str(work.resolve()), + ) + try: + r1 = await adapter.run_turn(session, first) + assert r1.stop_reason == "end_turn" + assert session.native_session_id + await adapter.shutdown(session) + # Make the worker's session/load fail (generic, non-unavailable) on + # the next spawn so Bridge takes the session/new fallback. + cfg.env["BRIDGE_ECHO_FAILURE"] = "load_internal" + second = Task( + task_id="task_reload_2", + session_id=session.session_id, + agent="echo", + message="two", + cwd=str(work.resolve()), + ) + r2 = await adapter.run_turn(session, second) + assert r2.stop_reason == "end_turn" + assert any("context is lost" in w for w in r2.warnings) + from agent_bridge.transcript import read_events + + types = {event["type"] for event in read_events(session.session_id, bridge_home)} + assert "session_recreated" in types + finally: + cfg.env.pop("BRIDGE_ECHO_FAILURE", None) + await adapter.shutdown(session) diff --git a/tests/test_registry.py b/tests/test_registry.py index 0228b82..8569e5d 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -1417,7 +1417,7 @@ async def counter() -> None: @pytest.mark.asyncio async def test_files_changed_is_capped(bridge_home, tmp_path, monkeypatch): paths = [f"gen/{i:04d}.txt" for i in range(500)] - monkeypatch.setattr("agent_bridge.registry.merge_files_changed", lambda *args, **kwargs: list(paths)) + monkeypatch.setattr("agent_bridge.registry.merge_files_changed", lambda *args, **kwargs: (list(paths), [])) work = tmp_path / "work" work.mkdir() registry = Registry.create(bridge_home) @@ -1983,3 +1983,51 @@ async def slow_cancel(self, session): assert cancel_finished.is_set() finally: await registry.stop() + + +@pytest.mark.asyncio +async def test_turn_timeout_sec_caps_chatty_turn(bridge_home, tmp_path, monkeypatch): + """H-16: a worker emitting worker-type events forever still hits the cap.""" + import asyncio as _asyncio + + async def chatty_turn(self, session, task): + from agent_bridge.transcript import append_event + + for _ in range(30): + append_event(session.session_id, "thought_chunk", {"text": "..."}, self.home) + await _asyncio.sleep(0.1) + return TurnResult(text="done") + + monkeypatch.setattr(FakeAdapter, "run_turn", chatty_turn) + registry = Registry.create(bridge_home) + registry.config.agents["fake"].turn_timeout_sec = 1 + await registry.start() + try: + dispatched = await registry.dispatch_task("fake", "chatty", cwd=str(tmp_path)) + waited = await registry.wait_task(dispatched["task_id"], timeout_sec=10) + assert waited["status"] == "failed" + assert waited["stop_reason"] == "timed_out" + assert "turn_timeout_sec" in (waited["error"] or "") + finally: + await registry.stop() + + +@pytest.mark.asyncio +async def test_silent_turn_still_stalls(bridge_home, tmp_path, monkeypatch): + """Silence semantics: with no worker events at all, stall_timeout fires.""" + + async def silent_turn(self, session, task): + await asyncio.sleep(10) + return TurnResult(text="done") + + monkeypatch.setattr(FakeAdapter, "run_turn", silent_turn) + registry = Registry.create(bridge_home) + registry.config.agents["fake"].stall_timeout_sec = 1 + await registry.start() + try: + dispatched = await registry.dispatch_task("fake", "quiet", cwd=str(tmp_path)) + waited = await registry.wait_task(dispatched["task_id"], timeout_sec=10) + assert waited["status"] == "failed" + assert waited["stop_reason"] == "stalled" + finally: + await registry.stop() diff --git a/tests/test_transcript.py b/tests/test_transcript.py index cdf1f90..cf796cf 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -172,3 +172,39 @@ def test_parse_cache_is_bounded(bridge_home): flush_session(session_id, bridge_home) read_events(session_id, bridge_home) assert len(transcript._parse_cache) <= 4 + + +def test_only_worker_events_reset_the_activity_clock(bridge_home, monkeypatch): + """H-16: bridge-side bookkeeping must not count as worker aliveness.""" + import time as _time + + path = transcript_path("sess_gate", bridge_home) + with transcript._buffers_lock: + transcript._activity[path] = _time.monotonic() - 10.0 + append_event("sess_gate", "prompt_sent", {"text": "hi"}, bridge_home) + append_event("sess_gate", "permission", {"decision": "selected"}, bridge_home) + append_event("sess_gate", "turn_end", {"stop_reason": "x"}, bridge_home) + silence = worker_silence_sec("sess_gate", bridge_home) + assert silence is not None and silence >= 9.0 + append_event("sess_gate", "message_chunk", {"text": "worker speaks"}, bridge_home) + silence = worker_silence_sec("sess_gate", bridge_home) + assert silence is not None and silence < 2.0 + append_event("sess_gate", "thought_chunk", {"text": "..."}, bridge_home) + assert (worker_silence_sec("sess_gate", bridge_home) or 0) < 2.0 + forget_worker_activity("sess_gate", bridge_home) + + +def test_forget_session_drops_caches_and_transcript_file(bridge_home): + """H-02: session prune must delete the transcript, not just the result.""" + append_event("sess_forget", "message_chunk", {"text": "one"}, bridge_home) + flush_session("sess_forget", bridge_home) + path = transcript_path("sess_forget", bridge_home) + assert path.is_file() + mark_worker_activity("sess_forget", bridge_home) + read_events("sess_forget", bridge_home) + transcript.forget_session("sess_forget", bridge_home) + assert not path.exists() + assert worker_silence_sec("sess_forget", bridge_home) is None + with transcript._buffers_lock: + assert path not in transcript._parse_cache + assert path not in transcript._buffers diff --git a/tests/test_workspace.py b/tests/test_workspace.py index 7e6fdf3..4f65502 100644 --- a/tests/test_workspace.py +++ b/tests/test_workspace.py @@ -1,6 +1,8 @@ import os from pathlib import Path +import pytest + from agent_bridge.workspace import ( _root_prefix_len, collect_update_paths, @@ -16,8 +18,9 @@ def test_snapshot_sees_new_file_and_ignores_sessions(tmp_path: Path): sessions = tmp_path / ".sessions" sessions.mkdir() (sessions / "log.jsonl").write_text("{}\n", encoding="utf-8") - changed = merge_files_changed(tmp_path, [], before) + changed, outside = merge_files_changed(tmp_path, [], before) assert changed == ["smoke.txt"] + assert outside == [] def test_merges_protocol_paths_as_cwd_relative(tmp_path: Path): @@ -25,15 +28,19 @@ def test_merges_protocol_paths_as_cwd_relative(tmp_path: Path): target = tmp_path / "src" / "app.py" target.parent.mkdir() target.write_text("print(1)\n", encoding="utf-8") - changed = merge_files_changed(tmp_path, [str(target)], before) + changed, outside = merge_files_changed(tmp_path, [str(target)], before) assert changed == ["src/app.py"] + assert outside == [] def test_drops_cwd_dot_and_root_paths(tmp_path: Path): before = snapshot_workspace(tmp_path) (tmp_path / "ok.txt").write_text("x\n", encoding="utf-8") - changed = merge_files_changed(tmp_path, [".", str(tmp_path), str(tmp_path / "ok.txt")], before) + changed, outside = merge_files_changed( + tmp_path, [".", str(tmp_path), str(tmp_path / "ok.txt")], before + ) assert changed == ["ok.txt"] + assert outside == [] def test_snapshot_skips_build_dirs_and_tracks_changes(tmp_path: Path): @@ -56,11 +63,11 @@ def test_snapshot_skips_build_dirs_and_tracks_changes(tmp_path: Path): # A same-length rewrite can keep both size and mtime_ns on a coarse # filesystem clock, so the payload length has to change. (tmp_path / "src" / "a.py").write_text("print(22)\n", encoding="utf-8") - assert merge_files_changed(tmp_path, [], before) == ["src/a.py"] + assert merge_files_changed(tmp_path, [], before)[0] == ["src/a.py"] (nested / "b.txt").unlink() (tmp_path / "src" / "c.py").write_text("print(3)\n", encoding="utf-8") - changed = set(merge_files_changed(tmp_path, [], before)) + changed = set(merge_files_changed(tmp_path, [], before)[0]) assert "nested/deep/b.txt" in changed assert "src/c.py" in changed assert "src/a.py" in changed @@ -92,3 +99,60 @@ def test_collect_nested_tool_paths(): ) assert "README.md" in found assert "src/main.py" in found + + +def test_classify_splits_inside_and_outside_paths(tmp_path: Path): + from agent_bridge.workspace import classify_changed_paths + + inside, outside = classify_changed_paths( + tmp_path, + [ + "src/ok.py", + "../escape.txt", + str(tmp_path.parent / "sibling.py"), + str(tmp_path / "in.txt"), + ], + ) + assert inside == ["src/ok.py", "in.txt"] + assert len(outside) == 2 + assert not any(".." in p for p in inside) + # E3 regression: ../escape.txt must NOT fold to escape.txt inside cwd. + assert "escape.txt" not in inside + + +def test_dotdot_absolute_outside_goes_to_outside_list(tmp_path: Path): + inside, outside = merge_files_changed( + tmp_path, [os.path.join("..", "..", "far_away.txt")], {} + ) + assert inside == [] + assert outside and outside[0].endswith("far_away.txt") + + +def test_bridge_dirs_are_skipped_in_snapshot(tmp_path: Path): + for d in (".agent-bridge", ".codex", ".claude"): + sub = tmp_path / d + sub.mkdir() + (sub / "state.json").write_text("{}\n", encoding="utf-8") + (tmp_path / "real.py").write_text("x\n", encoding="utf-8") + found = set(snapshot_workspace(tmp_path)) + # .agent-bridge churn is bridge bookkeeping; .codex/.claude inside the + # project hold legitimate config (e.g. .codex/config.toml) and must be + # reported (review: worker state dirs live under home, outside cwd). + assert found == {"real.py", ".codex/state.json", ".claude/state.json"} + + +@pytest.mark.skipif(os.name != "nt", reason="Windows junction repro") +def test_junction_inside_cwd_is_not_traversed(tmp_path: Path): + import subprocess + + target = tmp_path.parent / "junction_target" + target.mkdir(exist_ok=True) + (target / "foreign.txt").write_text("out\n", encoding="utf-8") + link = tmp_path / "link" + subprocess.run( + ["cmd", "/c", "mklink", "/J", str(link), str(target)], + check=True, + capture_output=True, + ) + snap = snapshot_workspace(tmp_path) + assert "link/foreign.txt" not in snap From ddb49ff1a1f059f7b98956ec21bd56a598b56662 Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 6 Oct 2026 19:01:28 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E5=BB=BA=E7=AB=8B=20worker=20=E4=BF=A1?= =?UTF-8?q?=E4=BB=BB=E8=BE=B9=E7=95=8C=EF=BC=9A=E8=AF=8A=E6=96=AD=E6=B6=88?= =?UTF-8?q?=E6=AF=92=E3=80=81env.deny=E3=80=81=E6=9D=83=E9=99=90=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=20allow=5Fonce=20(#30)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 diagnostics.redact_diagnostic,task.error/warnings、Codex/Antigravity 错误事件落盘前脱敏 - [env] deny 支持用户配置的 glob 名单,显式 set/agent env 优先;无内置禁止名单;dsh 回填同样遵守 - [agents.] permission_policy:allow_once(默认)/ allow_always / deny;allow_once 下不选择 allow_always - 同步 ORCHESTRATION、SETUP、skill 与 agents.toml.example 中 #29–#32 的契约变化 --- ORCHESTRATION.md | 4 +- ORCHESTRATION.zh-CN.md | 4 +- SETUP.md | 10 +-- agents.toml.example | 14 +++++ skills/agent-bridge/SKILL.md | 4 +- src/agent_bridge/adapters/acp.py | 47 +++++++++------ src/agent_bridge/adapters/antigravity.py | 8 ++- src/agent_bridge/adapters/codex.py | 5 +- src/agent_bridge/codex_exec.py | 11 ++-- src/agent_bridge/config.py | 20 +++++- src/agent_bridge/diagnostics.py | 29 +++++++++ src/agent_bridge/dsh_home.py | 9 +++ src/agent_bridge/registry.py | 17 ++++-- src/agent_bridge/worker_env.py | 39 ++++++++++++ tests/test_config.py | 33 ++++++++++ tests/test_diagnostics.py | 77 ++++++++++++++++++++++++ tests/test_docs.py | 2 +- tests/test_registry.py | 32 ++++++++++ tests/test_worker_env.py | 61 +++++++++++++++++++ 19 files changed, 386 insertions(+), 40 deletions(-) create mode 100644 src/agent_bridge/diagnostics.py create mode 100644 tests/test_diagnostics.py diff --git a/ORCHESTRATION.md b/ORCHESTRATION.md index 6af5386..81c34c6 100644 --- a/ORCHESTRATION.md +++ b/ORCHESTRATION.md @@ -62,7 +62,7 @@ In `auto`/`eager`, tell the user after the fact. In `manual`, their explicit req - Devin CLI: advertised ids (`devin models list`; level is in the id, e.g. `swe-1-7-medium`). Unknown id fails; `effort` warns and is ignored. Mode forced to `bypass`. Revive via `session/load` (replays into `get_transcript`; `get_result` stays clean). - ZCode: `provider\model` or a unique bare id. Thought: `off`→`low`, `medium`→`high`. Unknown or ambiguous model fails; unmappable effort warns. Mode `yolo`. Revive via `session/resume`. - MiniMax Code: `provider/model` or `provider/model#variant`. `thinkingEffort` (`off`→`default`). Unknown model fails; unmappable effort warns. Permission `bypassPermissions`. Revive via `session/resume`. -3. Loop `wait_task` until terminal. A timeout is **not** failure — call it again. `silent_for_sec` is time since the last output. Silence past `stall_timeout_sec` (default 1800, per worker, 0 disables) ends `failed` / `stalled`: raise that worker's limit or resume the `session_id` with a narrower task. Size `timeout_sec` under the host MCP tool timeout: +3. Loop `wait_task` until terminal. A timeout is **not** failure — call it again. `silent_for_sec` is time since the last output. Silence past `stall_timeout_sec` (default 1800, per worker, 0 disables) ends `failed` / `stalled`: raise that worker's limit or resume the `session_id` with a narrower task. Stall counts only worker output — a chatty turn never stalls; `turn_timeout_sec` (per worker, 0 = off) caps a turn's wall-clock and ends `failed` / `timed_out`. Terminal `wait_task` carries `persisted`; `false` means `state.json` was not written — the result stays valid but may not survive a restart. `files_changed` lists only paths inside the task `cwd`; outside paths go to `files_outside_cwd` (absolute, capped alike, `files_outside_cwd_total`). A `session/load`-failed warning means the worker started a fresh native session — re-supply context next message. Size `timeout_sec` under the host MCP tool timeout: - Codex: `tool_timeout_sec` 600; default 180 is fine. - Cursor: host ~45–60 s; pass ~30 and loop. - Kimi Code: configure `toolTimeoutMs` 600000; otherwise ~45 s polls. @@ -78,3 +78,5 @@ Do not drive worker GUIs or CLIs. Session resume is Bridge's job. ACP `failure` supplements string `error` (fields: SETUP.md). Missing evidence or `failure.prompt_may_have_been_sent=true` forbids blind replay; false does not prove retry safety. Bridge never auto-retries prompts. For deduplication, send UUID `request_id` on the first dispatch; reuse its ID and exact arguments, including omitted `session_id`, on retry. Adding an ID only on retry cannot deduplicate earlier calls. Identical calls reuse the task (`reused=true`); changed arguments fail. Normal validation applies. Bindings last only in this instance until task pruning or restart; worker effects are not exactly-once. + +`check_task` / `get_result` on a task owned by another Bridge instance answer from `state.json` — with `from_state: true` and `silent_for_sec: null` — but `wait_task` / `cancel_task` still require the owning instance. diff --git a/ORCHESTRATION.zh-CN.md b/ORCHESTRATION.zh-CN.md index 6b9d491..0565185 100644 --- a/ORCHESTRATION.zh-CN.md +++ b/ORCHESTRATION.zh-CN.md @@ -72,7 +72,7 @@ Claude 的 `quota.status` 只汇总通用的 `5h` / `weekly` 额度。派发前 1. 先调 `list_agents`,选一个可用的 Worker。看返回里的 `coordinator.mode` / `coordinator.instructions` / `coordinator.dispatch_enabled`(用户偏好优先于第二步的默认分工),以及 `env.proxy` / `env.warnings`。`dispatch_enabled` 为 false 时立刻停止派发。直连网络上 proxy 为 null 是正常的;若机器需要代理而 Worker 报网络/连接错误,去改代理配置(仓库 `agents.toml` 或 `~/.agent-bridge/agents.toml` 里的 `[env.proxy]`),不要对同一次失败反复重试。 2. `dispatch_task` 的 `cwd` 必须是 **本次对话的项目目录**(绝对路径)。也就是你被启动时所在的文件夹——和用户自己 `cd` 再运行 `grok` / `kimi` / `agy` / `opencode` / `claude` 是同一个地方。Worker 的会话历史按 cwd 存放,换目录就是对方 UI 里的另一场对话,之后也更难看、更难续。**不要**把 Agent Bridge 的安装目录当作 cwd,除非用户正在改 Bridge 本身。不要自造临时目录。`message` 必须自洽:背景、绝对路径、验收标准、不要做什么。model 和 effort:没有特别理由就都不传,用 Worker 默认值——Antigravity 默认的 `gemini-3.7-flash` 就够了(要换才用 `agy models` 里的 slug)。确要指定时:Grok 的 `model` 是 `grok models` 里的 slug(用账号目录里有的,不要编),`effort` 为 `off` / `low` / `medium` / `high` / `max`(`off` → Grok `none`,`max` → Grok `xhigh`)。Grok 的 `/new` 总会落在活动默认模型上(目前是 grok-4.6 xhigh);Bridge 在会话建立后再 `session/setModel`。Kimi 的 `model` 取会话自己声明的那几个 slug(`kimi-code/k3`、`kimi-code/k3-256k`、`kimi-code/kimi-for-coding` 等),`effort` 还是那五个词,由 Bridge 映射到该模型声明的思考档位——k3 只声明 `low` / `high` / `max`,所以 `medium` 落到 `high`,`off` 落到 `low`。传了会话没声明的 slug 会让这一轮失败,错误里会列出真正可用的;`effort` 映射不上则只出 warning,不算失败。OpenCode 的 `model` 是会话声明的 `provider/model`(官方 OpenCode Zen / Go,或用 `opencode auth` 以 API key 接上的其它提供商——没有产品级登录)。`effort` 还是那五个词,映射到该模型的 variant(常见是 `default|low|medium|high`,`max` 通常落到 `high`)。未声明的 slug 会让这一轮失败;没有 effort 选项或映射不上只出 warning。`get_result.observed_model` / `observed_effort` 是 Bridge 映射后成功设上的最后值(例如 `max` → `high`),不是实时采样器转储。同一会话换模型会重新设 effort,因为 OpenCode 会把 variant 重置成新模型的默认。Bridge 用 `session/resume` 复活 OpenCode,不用 `session/load`。Claude Code 的 `model` 取会话声明的 slug(`sonnet` / `opus` / `haiku` 或完整 id),`effort` 还是那五个词(`off` → `default`,`max` → `xhigh`,除非会话列出了 `max`)。未声明的 slug 会让这一轮失败;没有 effort 选项或映射不上只出 warning。Bridge 在 `session/new` 后把模式切到 `bypassPermissions`,并用 `session/resume` 复活(`session/load` 会回放整段历史)。 Cursor 的 `model` 必须是 `cursor-agent --list-models` 当前列出的完整 ID;Bridge 会校验并固定首次启动,再把完整 ID 映射到 Cursor 会话声明的 ACP `model` 和参数选项。同一个 `session_id` 可以切换模型和变体;若所选模型声明了思考档位,单独传入的 `effort` 会覆盖 ID 中编码的档位。`observed_model` 是 Cursor 确认映射结果后的请求 ID,`observed_effort` 是 Cursor 确认的思考档位;两者都不是实时采样器。Codex CLI 的 `model` 是 Codex 自己的 slug(如 `gpt-5.6-sol`),`effort` 为 `off` / `low` / `medium` / `high` / `max`(`off` → `none`);默认 `--approve-for-me`,提示词走 stdin,用 `exec resume` 续会话;JSONL 建立前的启动失败会进入 `get_result.error`。Devin CLI 的 `model` 取会话声明的模型 id(`devin models list`;档位就在 id 里,如 `swe-1-7-medium`、`claude-opus-5-high`),未声明的 id 会让这一轮失败;`effort` 会被忽略并出 warning。Bridge 在 `session/new` 后把模式切到 `bypass`;续会话走 `session/load`,旧历史会被回放进 `get_transcript`(新一轮的 `get_result` 不受影响)。ZCode 的 `model` 是 `provider\model`,或在只对应一个选项时用裸模型 id(如 `GLM-5.3`);`effort` 映射到该模型的 thought 档(常见 `low|high|max`,`off` → `low`,`medium` → `high`)。未声明或有歧义的模型会让这一轮失败;映射不上只出 warning。Bridge 把模式切到 `yolo`,并用 `session/resume` 复活。MiniMax Code 的 `model` 是 `provider/model` 或 `provider/model#variant`;`effort` 映射到 `thinkingEffort`(`off` → `default`)。未声明的模型会让这一轮失败;映射不上只出 warning。Bridge 把 `permissionMode` 切到 `bypassPermissions`,并用 `session/resume` 复活。DSH 的 `model` 是 `provider/model` 或模型 id,`effort` 为 `off` / `low` / `high` / `max`。新版 `dsh --profile acp` 通过 `session/set_config_option` 在同一会话内切换——传了会话没声明的 model 会让这一轮失败,`effort` 映射不上只出 warning;旧版 `dsh-acp-demo` 走启动环境变量,改 model/effort 会重启进程(进程内不能切换),失败后换模型同样靠下次 `dispatch_task` 重启进程。 -3. 循环调用 `wait_task` 直到 `status` 为终态。超时不是失败,再调一次即可。`wait_task` / `check_task` 还会返回 `silent_for_sec`(Worker 最近一次输出到现在的秒数)。Worker 持续静默超过 `stall_timeout_sec`(默认 1800,可在 `agents.toml` 按 Worker 设置,0 关闭)时 Bridge 会取消这一轮,任务以 `status=failed`、`stop_reason="stalled"` 结束。长时间静默的构建和挂死的 Worker 看起来一样:如果那一步确实需要那么久,就调高该 Worker 的上限;否则在同一个 `session_id` 上派一个更小的任务。`timeout_sec` 必须低于宿主的 MCP 工具超时:Codex 配置 `tool_timeout_sec` 600,默认 180 即可;Cursor 宿主大约 45–60 秒,传约 30 并循环;Kimi Code 把 `toolTimeoutMs` 配到 600000,未配置则约 45 秒短轮询;ZCode 把 `timeoutMs` 配到 600000,未配置则约 15–20 秒短轮询;Grok Build 官方默认 `tool_timeout_sec` 是 6000,建议显式写成 600,拿不准或调用被掐掉时用约 30–45 秒短轮询;Claude Code 在 `.mcp.json` 里把每服务器 `timeout` 配成 600000(毫秒),CLI 默认很长,桌面端历史上大约 60 秒会掐掉,拿不准时用约 45 秒短轮询。等待期间可以并行做别的事。 +3. 循环调用 `wait_task` 直到 `status` 为终态。超时不是失败,再调一次即可。`wait_task` / `check_task` 还会返回 `silent_for_sec`(Worker 最近一次输出到现在的秒数)。Worker 持续静默超过 `stall_timeout_sec`(默认 1800,可在 `agents.toml` 按 Worker 设置,0 关闭)时 Bridge 会取消这一轮,任务以 `status=failed`、`stop_reason="stalled"` 结束。静默判定只看 Worker 输出——一直在产生事件的回合永远不会 stall。`turn_timeout_sec`(按 Worker 设置,默认 0 关闭)则给整轮设墙钟上限,超时以 `failed` / `timed_out` 结束。终态 `wait_task` 还带 `persisted`;为 `false` 表示 `state.json` 没写成功——结果仍然有效,但 Bridge 重启后可能丢失。`files_changed` 只列任务 `cwd` 内的路径;Worker 上报的 cwd 外路径进 `files_outside_cwd`(绝对路径,与 `files_changed` 同样截断,总数见 `files_outside_cwd_total`)。任务 warnings 里说 `session/load` 失败、先前上下文已丢失,表示 Worker 开了一个新的原生会话——下一条消息要重新带上上下文。长时间静默的构建和挂死的 Worker 看起来一样:如果那一步确实需要那么久,就调高该 Worker 的上限;否则在同一个 `session_id` 上派一个更小的任务。`timeout_sec` 必须低于宿主的 MCP 工具超时:Codex 配置 `tool_timeout_sec` 600,默认 180 即可;Cursor 宿主大约 45–60 秒,传约 30 并循环;Kimi Code 把 `toolTimeoutMs` 配到 600000,未配置则约 45 秒短轮询;ZCode 把 `timeoutMs` 配到 600000,未配置则约 15–20 秒短轮询;Grok Build 官方默认 `tool_timeout_sec` 是 6000,建议显式写成 600,拿不准或调用被掐掉时用约 30–45 秒短轮询;Claude Code 在 `.mcp.json` 里把每服务器 `timeout` 配成 600000(毫秒),CLI 默认很长,桌面端历史上大约 60 秒会掐掉,拿不准时用约 45 秒短轮询。等待期间可以并行做别的事。 4. 调 `get_result`;若 `has_more` 为 true,就继续用 `cursor=next_cursor` 读取并拼接所有分页。然后自己看 `git status` / `git diff`。该编的、该测的你来跑。不要只信 Worker 的自我汇报。Grok 的模型和 effort 以 `get_result.model` / `get_result.observed_model`(以及对应的 effort 字段)为准。`observed_model` 来自 Grok `events.jsonl` 的 `turn_started.model_id`。不要因为 Worker 说「I am Grok 4.6」或引用了 `You are Grok 4.6` 就判定切模型成败——那句横幅是 `/new` 写进系统提示的,`session/setModel` 换采样器时不会改它。Kimi 从不把失败的回合报成失败:它的 ACP 宿主会把失败回合映射成空文本的 `end_turn`,于是配额或服务端报错看起来和干净的空转一模一样。Kimi 交回空回合就要当可疑对待,去看 `get_result.warnings`——Bridge 每轮结束都会读 Kimi 的 `wire.jsonl`,把真实原因放在那里,旁边就是 `observed_model` / `observed_effort`。 5. 验收不通过时,用同一个 `session_id` 再 `dispatch_task`,并列出具体问题。最多跟进三轮。之后你自己改,并告诉用户。 6. 结束后总结 diff、剩余风险、用了哪个 Worker。不再需要时调用 `end_session`。 @@ -82,3 +82,5 @@ Claude 的 `quota.status` 只汇总通用的 `5h` / `weekly` 额度。派发前 ACP 失败保留字符串 `error`,并可附带 `failure`。`failure.prompt_may_have_been_sent=true` 或没有诊断证据时,不要盲目重发;false 也不保证初始化没有副作用或重试安全。Bridge 不自动重试 prompt。诊断字段详见 SETUP.md。 需要重试去重时,在首次调用 `dispatch_task` 前生成 UUID `request_id`,首次调用就传入。重试须使用同一 ID 和原始参数;首次省略了 `session_id`,重试也保持省略。只在重试时补 ID 无法对首次调用去重。相同参数复用原任务(`reused=true`),不同参数直接拒绝。去重只在同一 Bridge 实例内、原任务仍保留时有效,仍须通过正常派发校验。重启 Bridge、切换实例或清理原任务后不再保留绑定;Worker 的外部副作用不保证 exactly-once。 + +对另一个 Bridge 实例拥有的任务,`check_task` / `get_result` 从 `state.json` 作答——带 `from_state: true`、`silent_for_sec: null`;`wait_task` / `cancel_task` 仍须由拥有它的实例调用。 diff --git a/SETUP.md b/SETUP.md index eda3319..6c36eaf 100644 --- a/SETUP.md +++ b/SETUP.md @@ -62,7 +62,7 @@ npm install -g @deepseek-ai/dsh-acp-demo .\.venv\Scripts\python.exe scripts\install_dsh_acp.py ``` -The helper writes `$AGENT_BRIDGE_HOME/dsh-acp` (default `~/.agent-bridge/dsh-acp`) and installs the ACP peers the cordis file imports. Bridge copies that cordis file next to the chosen `node_modules` so ESM can resolve plugins. An unbuilt checkout `src/bin.ts` is ignored unless `tsx` is installed. DSH persistence is `$AGENT_BRIDGE_HOME/dsh-sessions/` so a user project does not get `./.sessions`. `get_result.files_changed` is a turn-scoped workspace diff, not only ACP tool_call events (DSH often sends none). It is capped at 200 paths; `files_changed_total` / `files_changed_truncated` tell you when to fall back to `git status`. `files_changed_state` is `pending` until that diff runs, `collected` when an empty list is a confirmed zero, and `unavailable` if collection could not run. +The helper writes `$AGENT_BRIDGE_HOME/dsh-acp` (default `~/.agent-bridge/dsh-acp`) and installs the ACP peers the cordis file imports. Bridge copies that cordis file next to the chosen `node_modules` so ESM can resolve plugins. An unbuilt checkout `src/bin.ts` is ignored unless `tsx` is installed. DSH persistence is `$AGENT_BRIDGE_HOME/dsh-sessions/` so a user project does not get `./.sessions`. `get_result.files_changed` is a turn-scoped workspace diff, not only ACP tool_call events (DSH often sends none). It is capped at 200 paths; `files_changed_total` / `files_changed_truncated` tell you when to fall back to `git status`. `files_changed_state` is `pending` until that diff runs, `collected` when an empty list is a confirmed zero, and `unavailable` if collection could not run. `files_changed` lists only paths inside the task `cwd`; worker-reported paths outside it land in `files_outside_cwd` (absolute, capped the same way, `files_outside_cwd_total`). `.agent-bridge` and Windows junctions are not scanned. Restart Codex after editing `config.toml`. @@ -287,6 +287,8 @@ Bridge rebuilds the environment **once at startup** (and again for each worker s | `[env.set]` | Explicit key/value map | | `[agents..env]` | Per-worker overlay | +`[env] deny = ["GLOB", ...]` removes worker env keys matching those globs. Keys set explicitly via `[env] set` or the agent's `env` always survive. There is no built-in denylist. + Do **not** put secrets in the repo `agents.toml`. Pin machine-local values in `%USERPROFILE%\.agent-bridge\agents.toml` (see [agents.toml.example](agents.toml.example)): ```toml @@ -327,7 +329,7 @@ error: failed to remove file `...\.venv\Lib\site-packages\../../Scripts/agent-br If you still launch from a checkout, use `uv --directory … run --no-sync agent-bridge` or `.venv/Scripts/python.exe -m agent_bridge`. POSIX hosts can replace a running binary, so this failure is Windows-only. `agent-bridge upgrade` / `uv tool upgrade` also need the coordinators closed first — same lock. -Instances share the `~/.agent-bridge` state directory but not sessions: every session and task record carries the identity of the Bridge instance that owns it. `list_sessions` shows only the calling instance's records, saves leave a live sibling's records untouched on disk, and records whose owning instance has exited are adopted at the next boot — their in-flight tasks surface as `failed` / `bridge_restarted`. A session started from one host is continued from that host; it does not appear in another host's `list_sessions` while its owner is alive. +Instances share the `~/.agent-bridge` state directory but not sessions: every session and task record carries the identity of the Bridge instance that owns it. `list_sessions` shows only the calling instance's records, saves leave a live sibling's records untouched on disk, and records whose owning instance has exited are adopted at the next boot — their in-flight tasks surface as `failed` / `bridge_restarted`. A session started from one host is continued from that host; it does not appear in another host's `list_sessions` while its owner is alive. `check_task` / `get_result` on a task owned by another instance answer from `state.json` (`from_state: true`, `silent_for_sec: null`); `wait_task` / `cancel_task` still require the owning instance. ## Server lifecycle @@ -481,7 +483,7 @@ opencode auth OpenCode has **no product login**. You connect providers by storing API keys (or that provider's own oauth) with `opencode auth`. Official routes are OpenCode Zen and OpenCode Go; anything else is just another connected provider. `list_agents` reports the CLI as available when `opencode` is on PATH — a missing provider key only shows up when a turn actually talks to the model. -`dispatch_task.model` is a `provider/model` slug the live session advertises (`opencode/...`, `xai/...`, …). `effort` maps onto that model's variants (`default|low|medium|high` is common; Bridge `max` usually becomes `high`). An unknown slug fails the turn and lists the real options; a model with no variants, or an effort that will not map, comes back as a warning. `get_result.observed_model` / `observed_effort` are the last values Bridge successfully set on the session after that mapping, not a live sampler dump. Switching model on a live session re-applies effort: OpenCode resets the variant to the new model's default. Bridge revives with `session/resume` rather than `session/load`. Permissions are ACP `requestPermission`; Bridge auto-picks `allow-always`. +`dispatch_task.model` is a `provider/model` slug the live session advertises (`opencode/...`, `xai/...`, …). `effort` maps onto that model's variants (`default|low|medium|high` is common; Bridge `max` usually becomes `high`). An unknown slug fails the turn and lists the real options; a model with no variants, or an effort that will not map, comes back as a warning. `get_result.observed_model` / `observed_effort` are the last values Bridge successfully set on the session after that mapping, not a live sampler dump. Switching model on a live session re-applies effort: OpenCode resets the variant to the new model's default. Bridge revives with `session/resume` rather than `session/load`. Permissions are ACP `requestPermission`; Bridge answers per `[agents.] permission_policy` — `allow_once` (default), `allow_always`, or `deny`. Under `allow_once` a prompt that offers only allow-always is denied. If OpenCode's configured default model is disabled, the first prompt fails with `Model is disabled`. Pass a slug from `opencode models` for a provider that still has a working key. @@ -517,7 +519,7 @@ $env:CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY = "1" If `OPENROUTER_API_KEY` is set and `ANTHROPIC_AUTH_TOKEN` is not, Bridge copies it and defaults the base URL to OpenRouter **only when** `ANTHROPIC_API_KEY` is also unset. A machine that uses OpenRouter for OpenCode and a direct Anthropic key for Claude keeps the Anthropic key. Set `ANTHROPIC_BASE_URL` yourself if you want that Anthropic key treated as a gateway conflict (Bridge then blanks it once a token and base URL are both present). -`dispatch_task.model` is a slug the live session advertises (`sonnet`, `opus`, `haiku`, or a full id). `effort` maps onto that model's levels (`default|low|medium|high|xhigh` is common; Bridge `off` → `default`, `max` → `xhigh` unless the session lists `max`). An unknown slug fails the turn and lists the real options; a model with no effort option, or an effort that will not map, comes back as a warning. `get_result.observed_model` / `observed_effort` are the last values Bridge successfully set after that mapping. Switching model on a live session re-applies effort. Bridge forces `bypassPermissions` after `session/new` (a fresh session starts in manual `default` mode); if that mode is not advertised — for example when the process is root — ACP `requestPermission` still auto-picks `allow-always`. Revive uses `session/resume`: `session/load` replays the whole history. +`dispatch_task.model` is a slug the live session advertises (`sonnet`, `opus`, `haiku`, or a full id). `effort` maps onto that model's levels (`default|low|medium|high|xhigh` is common; Bridge `off` → `default`, `max` → `xhigh` unless the session lists `max`). An unknown slug fails the turn and lists the real options; a model with no effort option, or an effort that will not map, comes back as a warning. `get_result.observed_model` / `observed_effort` are the last values Bridge successfully set after that mapping. Switching model on a live session re-applies effort. Bridge forces `bypassPermissions` after `session/new` (a fresh session starts in manual `default` mode); if that mode is not advertised — for example when the process is root — ACP `requestPermission` is still answered per `[agents.] permission_policy` (`allow_once` default; a prompt offering only allow-always is denied). Revive uses `session/resume`: `session/load` replays the whole history. ## Worker: Devin CLI diff --git a/agents.toml.example b/agents.toml.example index 159ac61..c5880fe 100644 --- a/agents.toml.example +++ b/agents.toml.example @@ -15,8 +15,17 @@ # Cancel a turn whose worker has produced no output for this long (seconds). # Default 1800; 0 disables. Raise it for a worker that runs long silent # builds or test suites. The task then ends failed / stop_reason "stalled". +# turn_timeout_sec instead caps a turn's wall-clock regardless of output +# (default 0 = off; timeout ends failed / "timed_out"). # [agents.grok] # stall_timeout_sec = 3600 +# turn_timeout_sec = 7200 + +# How Bridge answers ACP permission prompts: allow_once (default, least +# privilege), allow_always, or deny. Under allow_once a prompt that offers +# only allow-always is denied. +# [agents.grok] +# permission_policy = "allow_always" # Dispatch policy: mode = manual | auto | eager (default auto). # instructions is free text the coordinator reads via list_agents; it @@ -44,6 +53,11 @@ # [env.set] # DEEPSEEK_API_KEY = "do-not-commit" +# Remove worker env keys matching these globs. Keys set via [env.set] or +# the agent's own env always survive; there is no built-in denylist. +# [env] +# deny = ["AWS_*", "DATABASE_URL"] + # [agents.grok.env] # HTTPS_PROXY = "http://127.0.0.1:7897" diff --git a/skills/agent-bridge/SKILL.md b/skills/agent-bridge/SKILL.md index d31b06f..e553ca1 100644 --- a/skills/agent-bridge/SKILL.md +++ b/skills/agent-bridge/SKILL.md @@ -45,7 +45,7 @@ Examples: "fix the README typo" → yourself. "Add a None check at line 120" → ## The dispatch loop 1. `dispatch_task` with `cwd` = **this conversation's project folder** (absolute) — never the Agent Bridge install path, never a temp dir. The `message` must be self-contained: background, absolute paths, acceptance criteria, things not to do. Leave `model`/`effort` unset unless you have a reason; slugs and effort mappings per worker are documented in ORCHESTRATION.md in the Agent-Bridge repo. -2. Loop `wait_task` until terminal. A timeout is **not** failure — call it again. `silent_for_sec` in the payload is the time since the worker's last output; a turn silent past `stall_timeout_sec` (default 1800) ends `failed` / `stop_reason="stalled"` — raise that worker's limit in `agents.toml` if the step was legitimately long. Size `timeout_sec` under the host MCP tool timeout: +2. Loop `wait_task` until terminal. A timeout is **not** failure — call it again. `silent_for_sec` in the payload is the time since the worker's last output; a turn silent past `stall_timeout_sec` (default 1800) ends `failed` / `stop_reason="stalled"` — raise that worker's limit in `agents.toml` if the step was legitimately long. Stall counts only worker output; `turn_timeout_sec` (default 0 = off) instead caps a turn's wall-clock and ends `failed` / `timed_out`. A terminal `wait_task` carries `persisted`; `false` means `state.json` could not be written — the result is still valid but may not survive a restart. `files_changed` lists only paths inside the task `cwd`; outside-cwd paths land in `files_outside_cwd` (absolute, capped the same way, `files_outside_cwd_total`). A warning that `session/load` failed and prior context is lost means a fresh native session — re-supply context next message. Size `timeout_sec` under the host MCP tool timeout: - Codex: `tool_timeout_sec` 600; default 180 is fine. - Cursor: host ~45–60 s; pass ~30 and loop. - Kimi Code: configure `toolTimeoutMs` 600000; otherwise ~45 s polls. @@ -59,3 +59,5 @@ Examples: "fix the README typo" → yourself. "Add a None check at line 120" → ACP failures retain string `error` plus optional `failure`. If `failure.prompt_may_have_been_sent` is true or evidence is missing, do not blindly replay; false is not proof of safe retry. Bridge never auto-retries prompts. See SETUP.md for diagnostic fields. For optional retry deduplication, send a UUID `request_id` on the first `dispatch_task` call, then retry with the same ID and original arguments; keep `session_id` omitted if it was originally omitted. Adding an ID only on retry cannot deduplicate the first call. Identical retries reuse the task (`reused=true`) only within the same Bridge instance while the task is retained. Different arguments are rejected, and normal dispatch validation still applies. Restart, another instance, or task pruning loses the binding; this does not guarantee exactly-once worker side effects. + +`check_task` / `get_result` on a task owned by another Bridge instance answer from `state.json` (`from_state: true`, `silent_for_sec: null`); `wait_task` / `cancel_task` still require the owning instance. diff --git a/src/agent_bridge/adapters/acp.py b/src/agent_bridge/adapters/acp.py index 0d6b91e..7b96597 100644 --- a/src/agent_bridge/adapters/acp.py +++ b/src/agent_bridge/adapters/acp.py @@ -31,6 +31,7 @@ ) from agent_bridge.config import AgentConfig from agent_bridge.devin_meta import DEVIN_MODE_BYPASS, apply_devin_env +from agent_bridge.diagnostics import redact_diagnostic as _redact_diagnostic from agent_bridge.dsh_home import command_has_native_acp, prepare_dsh_launch, resolve_dsh_command from agent_bridge.kimi_meta import KIMI_MODE_YOLO, resolve_kimi_thinking from agent_bridge.minimax_meta import ( @@ -97,19 +98,6 @@ def _tool_io_summary(value: Any) -> str | None: return text if len(text) <= _TOOL_IO_LIMIT else text[:_TOOL_IO_LIMIT] + "…" -def _redact_diagnostic(text: str) -> str: - text = re.sub(r"\x1b\[[0-?]*[ -/]*[@-~]", "", text) - text = re.sub(r"(?im)^.*(?:authorization|(?:set-)?cookie)\s*[:=].*$", "[redacted]", text) - text = re.sub( - r"""(?i)(\b(?:[\w-]*(?:token|secret|password|api[_-]?key)|authorization|cookie)\b["']?\s*[:=]\s*)(?:"[^"]*"|'[^']*'|[^\s,;]+)""", - r"\1[redacted]", text, - ) - text = re.sub(r"(?i)\b(?:bearer|basic)\s+\S+", "[redacted]", text) - text = re.sub(r"""(?i)\b(?:https?|wss?)://[^\s<>"']+""", "[redacted URL]", text) - text = re.sub(r"\b(?:sk-[\w-]+|gh[pousr]_\w+)\b", "[redacted]", text) - return text[:2048] - - def _stderr_summary(text: str) -> str | None: hints = re.findall( r"\b(?:authentication required|not logged in|unauthorized|permission denied|" @@ -484,18 +472,35 @@ def _text_of(content: Any) -> str: return str(dumped) -def _pick_permission_option(options: Iterable[PermissionOption]) -> PermissionOption | None: +def _pick_permission_option( + options: Iterable[PermissionOption], + policy: str = "allow_once", +) -> PermissionOption | None: + """Pick the permission option matching the agent's policy. + + Bridge is the only approver in the loop — there is no human to click + "allow". Default policy ``allow_once`` grants each request for this turn + only; ``allow_always`` additionally persists the grant inside the worker; + ``deny`` cancels every request. + """ + if policy == "deny": + return None + allow_always_first = policy == "allow_always" ranked: list[tuple[int, PermissionOption]] = [] for option in options: kind = str(getattr(option, "kind", "")).lower().replace("_", "-") option_id = str(getattr(option, "option_id", "")).lower().replace("_", "-") blob = f"{kind} {option_id}" if "allow-always" in blob or kind == "allow-always": + # Under any non-allow_always policy a persistent grant must not be + # picked — falling back to it would make a one-time policy lie. + if not allow_always_first: + continue score = 0 elif "allow-once" in blob or kind == "allow-once" or blob.strip().startswith("allow"): - score = 1 + score = 1 if allow_always_first else 0 else: - score = 50 + score = 50 if allow_always_first else 90 ranked.append((score, option)) ranked.sort(key=lambda item: item[0]) return ranked[0][1] if ranked else None @@ -508,9 +513,10 @@ async def _observe_exit(proc: asyncio.subprocess.Process | None) -> None: class _BridgeClient: - def __init__(self, session_id: str, home: Path) -> None: + def __init__(self, session_id: str, home: Path, permission_policy: str = "allow_once") -> None: self.session_id = session_id self.home = home + self.permission_policy = permission_policy self.text_parts: list[str] = [] self.files: set[str] = set() self.usage: dict[str, Any] = {} @@ -529,7 +535,7 @@ async def request_permission( options: list[Any] | None, **kwargs: Any, ) -> RequestPermissionResponse: - option = _pick_permission_option(options or []) + option = _pick_permission_option(options or [], self.permission_policy) dumped = _dump(tool_call) if option is None: append_event( @@ -839,6 +845,7 @@ async def _spawn(self, session: Session) -> _Live: session_id=session.session_id, model=session.model, effort=session.effort, + env_cfg=self.env_config, ) elif self.agent.name == "grok": cmd = with_grok_cli_selection(cmd, session.model, session.effort) @@ -872,7 +879,9 @@ async def _spawn(self, session: Session) -> _Live: raise RuntimeError(f"{self.agent.name} did not expose stdio") live = _Live() live.proc = proc - live.client = _BridgeClient(session.session_id, self.home) + live.client = _BridgeClient( + session.session_id, self.home, self.agent.permission_policy + ) live.conn = connect_to_agent(live.client, proc.stdin, proc.stdout) live.stderr_task = asyncio.create_task( self._drain_stderr(proc, session.session_id, live) diff --git a/src/agent_bridge/adapters/antigravity.py b/src/agent_bridge/adapters/antigravity.py index eed8823..3b8ce8f 100644 --- a/src/agent_bridge/adapters/antigravity.py +++ b/src/agent_bridge/adapters/antigravity.py @@ -14,6 +14,7 @@ from agent_bridge.adapters.base import STDIO_LIMIT, Adapter from agent_bridge.config import AgentConfig +from agent_bridge.diagnostics import redact_diagnostic from agent_bridge.models import Session, Task, TurnResult, agy_effort from agent_bridge.processes import ( drop_pid, @@ -447,7 +448,12 @@ async def run_turn(self, session: Session, task: Task) -> TurnResult: native_session_id=cid, warnings=[err, *exit_warnings], ) - append_event(session.session_id, "error", {"error": err, "code": proc.returncode}, self.home) + append_event( + session.session_id, + "error", + {"error": redact_diagnostic(err), "code": proc.returncode}, + self.home, + ) return TurnResult( text=result_text, files_changed=sorted(files), diff --git a/src/agent_bridge/adapters/codex.py b/src/agent_bridge/adapters/codex.py index af25379..72fb29c 100644 --- a/src/agent_bridge/adapters/codex.py +++ b/src/agent_bridge/adapters/codex.py @@ -19,6 +19,7 @@ resolve_codex_command, yolo_requested, ) +from agent_bridge.diagnostics import redact_diagnostic from agent_bridge.models import Session, Task, TurnResult from agent_bridge.processes import ( drop_pid, @@ -207,7 +208,9 @@ async def run_turn(self, session: Session, task: Task) -> TurnResult: observed_effort=observed_effort, ) if stop_reason == "error": - append_event(session.session_id, "error", {"error": error}, self.home) + append_event( + session.session_id, "error", {"error": redact_diagnostic(error or "")}, self.home + ) return TurnResult( text=state.text, files_changed=sorted(state.files), diff --git a/src/agent_bridge/codex_exec.py b/src/agent_bridge/codex_exec.py index 8f49627..17c4ebd 100644 --- a/src/agent_bridge/codex_exec.py +++ b/src/agent_bridge/codex_exec.py @@ -8,6 +8,7 @@ from pathlib import Path from typing import Any +from agent_bridge.diagnostics import redact_diagnostic from agent_bridge.processes import resolve_command log = logging.getLogger(__name__) @@ -289,13 +290,15 @@ def finalize_codex_turn( if returncode not in (0, None): warnings.append(f"codex exited with code {returncode}") if state.turn_failed: - return "error", state.error or "codex turn failed", warnings + return "error", redact_diagnostic(state.error or "codex turn failed"), warnings if state.turn_completed: - warnings.extend(state.errors) + warnings.extend(redact_diagnostic(err) for err in state.errors) return "end_turn", None, warnings if state.errors: - return "error", state.error or "codex turn failed", warnings - stderr_error = (stderr or "").strip() + return "error", redact_diagnostic(state.error or "codex turn failed"), warnings + # Codex stderr is raw worker output (may echo env secrets it read); + # sanitize before it becomes the task's error text. + stderr_error = redact_diagnostic((stderr or "").strip()) if stderr_error: return "error", stderr_error, warnings error = ( diff --git a/src/agent_bridge/config.py b/src/agent_bridge/config.py index ed952bd..3d79a4f 100644 --- a/src/agent_bridge/config.py +++ b/src/agent_bridge/config.py @@ -121,6 +121,11 @@ class AgentConfig(BaseModel): # stall_timeout_sec alone cannot see that case (H-16). turn_timeout_sec: int = Field(default=0, ge=0) print_timeout: str = "120m" + # How Bridge answers worker permission prompts when no human is present: + # "allow_once" grants each request for that call only (default, least + # privilege), "allow_always" persists the grant inside the worker, + # "deny" cancels every request. + permission_policy: str = "allow_once" class EnvConfig(BaseModel): @@ -131,6 +136,9 @@ class EnvConfig(BaseModel): set: dict[str, str] = Field(default_factory=dict) proxy_url: str | None = None no_proxy: str | None = None + # Glob patterns of env keys a worker subprocess must never see; keys + # explicitly assigned via ``set``/agent ``env`` always survive. + deny: list[str] = Field(default_factory=list) class ServerConfig(BaseModel): @@ -251,7 +259,9 @@ def _coerce_env(raw: dict[str, Any]) -> dict[str, Any]: proxy: dict[str, Any] = raw_proxy if isinstance(raw_proxy, dict) else {} out: dict[str, Any] = {} if "inherit" in block and block["inherit"] is not None: - out["inherit"] = [str(item) for item in block["inherit"]] + raw_inherit = block["inherit"] + # A bare string is one name, not an iterable of characters. + out["inherit"] = [raw_inherit] if isinstance(raw_inherit, str) else [str(item) for item in raw_inherit] if "discover_proxy" in block: out["discover_proxy"] = bool(block["discover_proxy"]) if isinstance(block.get("set"), dict): @@ -262,6 +272,10 @@ def _coerce_env(raw: dict[str, Any]) -> dict[str, Any]: no_proxy = proxy.get("no_proxy") or proxy.get("no") or block.get("no_proxy") if no_proxy: out["no_proxy"] = str(no_proxy).strip() + if "deny" in block and block["deny"] is not None: + raw_deny = block["deny"] + # A bare string is one glob, not an iterable of characters. + out["deny"] = [raw_deny] if isinstance(raw_deny, str) else [str(item) for item in raw_deny] return out @@ -272,6 +286,10 @@ def _merge_env(base: dict[str, Any], overlay: dict[str, Any]) -> dict[str, Any]: raw_set = out.get("set") current: dict[str, Any] = raw_set if isinstance(raw_set, dict) else {} out["set"] = {**current, **value} + elif key == "deny" and isinstance(value, list): + raw_deny = out.get("deny") + deny_list: list[Any] = raw_deny if isinstance(raw_deny, list) else [] + out["deny"] = [*deny_list, *value] else: out[key] = value return out diff --git a/src/agent_bridge/diagnostics.py b/src/agent_bridge/diagnostics.py new file mode 100644 index 0000000..3a7a2ac --- /dev/null +++ b/src/agent_bridge/diagnostics.py @@ -0,0 +1,29 @@ +"""Sanitization for worker-derived diagnostic text before it crosses into +trusted surfaces (state.json, transcripts, MCP responses, logs). + +Worker output is untrusted: a worker can print anything, including secrets +it read from its environment. Anything persisted or returned to the +coordinator must pass through ``redact_diagnostic`` first. Only diagnostic +channels (error text, warnings, stderr tails) are sanitized — result bodies +and structured event payloads are the product's output and are left alone. +""" + +from __future__ import annotations + +import re + +_REDACT_LIMIT = 4096 + + +def redact_diagnostic(text: str) -> str: + text = re.sub(r"\x1b\[[0-?]*[ -/]*[@-~]", "", text) + text = re.sub(r"(?im)^.*(?:authorization|(?:set-)?cookie)\s*[:=].*$", "[redacted]", text) + text = re.sub( + r"""(?i)(\b(?:[\w-]*(?:token|secret|password|api[_-]?key)|authorization|cookie)\b["']?\s*[:=]\s*)(?:"[^"]*"|'[^']*'|[^\s,;]+)""", + r"\1[redacted]", + text, + ) + text = re.sub(r"(?i)\b(?:bearer|basic)\s+\S+", "[redacted]", text) + text = re.sub(r"""(?i)\b(?:https?|wss?)://[^\s<>"']+""", "[redacted URL]", text) + text = re.sub(r"\b(?:sk-[\w-]+|gh[pousr]_\w+|tp-[A-Za-z0-9_-]{16,})\b", "[redacted]", text) + return text[:_REDACT_LIMIT] diff --git a/src/agent_bridge/dsh_home.py b/src/agent_bridge/dsh_home.py index c432ad3..b9626b8 100644 --- a/src/agent_bridge/dsh_home.py +++ b/src/agent_bridge/dsh_home.py @@ -7,10 +7,12 @@ from collections.abc import Mapping from pathlib import Path +from agent_bridge.config import EnvConfig from agent_bridge.models import dsh_effort from agent_bridge.paths import bridge_home, bundled_dsh_cordis from agent_bridge.processes import resolve_command from agent_bridge.worker_env import ( + denied_by_policy, pick_env_keys, read_windows_machine_env, read_windows_user_env, @@ -477,11 +479,14 @@ def apply_dsh_worker_env( effort: str | None = None, user_env: Mapping[str, str] | None = None, machine_env: Mapping[str, str] | None = None, + env_cfg: EnvConfig | None = None, ) -> dict[str, str]: """Attach DSH home, the user's saved default model, and credential env refs. No provider is required. Official DeepSeek is only one of the routes DSH already knows; users configure providers in ``$DSH_HOME/settings.yaml``. + ``env_cfg`` re-applies the env deny policy to keys filled from the host's + user/machine env so a denied credential cannot re-enter here. """ out = dict(env) home = dsh_home(out) @@ -498,6 +503,8 @@ def apply_dsh_worker_env( user = user_env if user_env is not None else read_windows_user_env() for incoming in (machine, user): for key, value in pick_env_keys(incoming, extra_keys).items(): + if key not in out and env_cfg is not None and denied_by_policy(key, env_cfg): + continue out.setdefault(key, value) roots = _node_module_roots(command or []) if roots: @@ -535,6 +542,7 @@ def prepare_dsh_launch( effort: str | None = None, user_env: Mapping[str, str] | None = None, machine_env: Mapping[str, str] | None = None, + env_cfg: EnvConfig | None = None, ) -> tuple[list[str], dict[str, str]]: return with_bridge_cordis(command), apply_dsh_worker_env( env, @@ -544,4 +552,5 @@ def prepare_dsh_launch( effort=effort, user_env=user_env, machine_env=machine_env, + env_cfg=env_cfg, ) diff --git a/src/agent_bridge/registry.py b/src/agent_bridge/registry.py index 4bcf685..f18b06f 100644 --- a/src/agent_bridge/registry.py +++ b/src/agent_bridge/registry.py @@ -25,6 +25,7 @@ normalize_coordinator_mode, write_coordinator_overlay, ) +from agent_bridge.diagnostics import redact_diagnostic from agent_bridge.grok_observe import observe_grok_session from agent_bridge.kimi_observe import observe_kimi_session from agent_bridge.models import ( @@ -98,8 +99,9 @@ STATE_WRITE_ATTEMPTS = 5 STATE_WRITE_RETRY_BASE_SEC = 0.05 STATE_LOCK_TIMEOUT_SEC = 5.0 -# flush_state() waits for the saves issued at call time; write failures -# degrade to a logged warning instead of raising into the caller. +# flush_state() waits for the saves issued at call time and returns whether +# that generation reached disk; start()/stop() raise on False, wait_task +# reports it as ``persisted``. STATE_FLUSH_ATTEMPTS = 3 # Poll cadence while flush_state() waits on the shared flusher; small so a # terminal wait_task does not add up to a full second under save traffic. @@ -832,7 +834,9 @@ async def _run_task(self, task_id: str) -> None: if result.native_session_id: session.native_session_id = result.native_session_id task.result_chars = len(result.text) - task.warnings = list(result.warnings) + # Worker-derived text crosses the trust boundary here: scrub + # before it lands in state.json/transcripts/MCP replies. + task.warnings = [redact_diagnostic(w) for w in result.warnings] try: atomic_write_text(result_path(task.task_id, self.home), result.text) task.result_text = _tail(result.text) @@ -859,7 +863,8 @@ async def _run_task(self, task_id: str) -> None: # Kimi answered end_turn, so nothing above this line knows # the turn failed. Say so where the coordinator looks. task.warnings.append( - f"kimi reported end_turn but the turn failed: {observed['failure']}" + "kimi reported end_turn but the turn failed: " + + redact_diagnostic(str(observed["failure"])) ) else: # OpenCode (and any later ACP worker) has no on-disk sampler @@ -876,7 +881,7 @@ async def _run_task(self, task_id: str) -> None: task.stop_reason = "cancelled" else: task.status = TaskStatus.failed - task.error = result.error + task.error = redact_diagnostic(result.error) elif result.stop_reason == "cancelled": task.status = TaskStatus.cancelled else: @@ -899,7 +904,7 @@ async def _run_task(self, task_id: str) -> None: task.failure = None else: task.status = TaskStatus.failed - task.error = str(exc) + task.error = redact_diagnostic(str(exc)) task.failure = exc.failure if isinstance(exc, AcpError) else None task.stop_reason = "error" finally: diff --git a/src/agent_bridge/worker_env.py b/src/agent_bridge/worker_env.py index 7790e2e..a048b3b 100644 --- a/src/agent_bridge/worker_env.py +++ b/src/agent_bridge/worker_env.py @@ -1,5 +1,6 @@ from __future__ import annotations +import fnmatch import logging import os import re @@ -507,6 +508,41 @@ def install_host_env(config: EnvConfig, *, base: Mapping[str, str] | None = None return status +def denied_by_policy(key: str, cfg: EnvConfig, explicit: set[str] | None = None) -> bool: + """True when the user's ``env.deny`` globs would strip ``key`` from a worker env. + + There is no built-in denylist: a default deny hid AWS_*/SSH_*/etc. from + workers that legitimately need them (Bedrock/Vertex auth, git-over-ssh), + so only the operator's configured globs strip keys. Shared by + _apply_env_deny and injection points downstream of it (e.g. the DSH + launch helper pulling credential refs from Windows env) so a denied key + cannot re-enter through another code path. + """ + if explicit and key in explicit: + return False + user_deny = [str(pat) for pat in (cfg.deny or []) if str(pat).strip()] + return any(fnmatch.fnmatchcase(key, pat) for pat in user_deny) + + +def _apply_env_deny( + env: dict[str, str], + origin: dict[str, str], + cfg: EnvConfig, + explicit: set[str], +) -> None: + """Strip env keys matching the user's ``env.deny`` globs. + + Explicit intent always wins: a key assigned through ``env.set`` or the + agent's own ``env`` block survives any pattern. Denied keys are recorded + as ``origin[key] = "denied"`` so describe_env can tell withheld from + absent. + """ + for key in list(env): + if denied_by_policy(key, cfg, explicit): + env.pop(key, None) + origin[key] = "denied" + + def build_worker_env( overrides: Mapping[str, str] | None = None, *, @@ -529,6 +565,9 @@ def build_worker_env( user_env=user_env, machine_env=machine_env, ) + cfg = config or EnvConfig() + explicit = set(cfg.set) | set(overrides or {}) + _apply_env_deny(env, origin, cfg, explicit) # After every merge so agents.toml / inherited env cannot clear the mark. # Also pin a nested data dir: if the worker inherits this MCP server, the # nested Bridge must not share the coordinator's state.json. A host MCP diff --git a/tests/test_config.py b/tests/test_config.py index e842f75..49484d3 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -381,3 +381,36 @@ def test_load_config_reports_overlay_path_on_bad_toml(tmp_path): load_config(tmp_path) assert str(path) in str(exc_info.value) assert "Fix or delete the file" in str(exc_info.value) + + +def test_env_deny_overlay(tmp_path): + (tmp_path / "agents.toml").write_text( + """ +[env] +deny = ["APP_*"] +""", + encoding="utf-8", + ) + cfg = load_config(tmp_path) + assert "APP_*" in cfg.env.deny + + +def test_merge_env_unions_deny_lists(): + from agent_bridge.config import _merge_env + + merged = _merge_env({"deny": ["A_*"]}, {"deny": ["B_*"]}) + assert merged["deny"] == ["A_*", "B_*"] + + +def test_permission_policy_default_and_overlay(tmp_path): + cfg = load_config(tmp_path) + assert cfg.agents["claude"].permission_policy == "allow_once" + (tmp_path / "agents.toml").write_text( + """ +[agents.claude] +permission_policy = "deny" +""", + encoding="utf-8", + ) + cfg = load_config(tmp_path) + assert cfg.agents["claude"].permission_policy == "deny" diff --git a/tests/test_diagnostics.py b/tests/test_diagnostics.py new file mode 100644 index 0000000..2e0f370 --- /dev/null +++ b/tests/test_diagnostics.py @@ -0,0 +1,77 @@ +from __future__ import annotations + +from types import SimpleNamespace + +from acp.schema import PermissionOption + +from agent_bridge.adapters.acp import _pick_permission_option +from agent_bridge.diagnostics import redact_diagnostic + + +def _opt(kind: str, name: str = "") -> PermissionOption: + return PermissionOption(optionId=f"{kind}-id", name=name or kind, kind=kind) # type: ignore[arg-type] + + +def test_redact_diagnostic_scrubs_key_value_secrets(): + text = "auth failed\napi_key=tp-abcdef0123456789\nAuthorization: Bearer tp-abcdef0123456789" + out = redact_diagnostic(text) + assert "tp-abcdef0123456789" not in out + assert "[redacted]" in out + + +def test_redact_diagnostic_scrubs_bare_tokens_and_urls(): + text = "leak sk-proj-abc123ghp_xyz789 then https://api.example.com/v1?k=secret" + out = redact_diagnostic(text) + assert "sk-proj-abc123" not in out + assert "ghp_xyz789" not in out + assert "api.example.com" not in out + + +def test_redact_diagnostic_strips_ansi_and_caps(): + assert "\x1b[31m" not in redact_diagnostic("\x1b[31mred\x1b[0m") + assert len(redact_diagnostic("x" * 100_000)) <= 4096 + + +def test_redact_diagnostic_keeps_plain_errors(): + assert redact_diagnostic("codex exited with code 1") == "codex exited with code 1" + + +def test_permission_default_policy_prefers_allow_once(): + options = [_opt("reject_once"), _opt("allow_always"), _opt("allow_once")] + picked = _pick_permission_option(options) + assert picked is not None and picked.kind == "allow_once" + + +def test_permission_allow_always_policy_picks_allow_always(): + options = [_opt("allow_once"), _opt("reject_once"), _opt("allow_always")] + picked = _pick_permission_option(options, "allow_always") + assert picked is not None and picked.kind == "allow_always" + + +def test_permission_allow_always_falls_back_to_allow_once(): + options = [_opt("reject_once"), _opt("allow_once")] + picked = _pick_permission_option(options, "allow_always") + assert picked is not None and picked.kind == "allow_once" + + +def test_permission_deny_policy_returns_none(): + options = [_opt("allow_once"), _opt("allow_always")] + assert _pick_permission_option(options, "deny") is None + + +def test_permission_allow_once_never_picks_allow_always(): + # A persistent grant must not satisfy a one-time policy — review finding: + # falling back to allow_always made allow_once lie. + options = [_opt("reject_once"), _opt("allow_always")] + picked = _pick_permission_option(options, "allow_once") + assert picked is not None and picked.kind == "reject_once" + assert _pick_permission_option([_opt("allow_always")], "allow_once") is None + + +def test_permission_unknown_policy_behaves_like_allow_once(): + options = [ + SimpleNamespace(kind="allow_always", option_id="always"), + SimpleNamespace(kind="allow_once", option_id="once"), + ] + picked = _pick_permission_option(options, "bogus") + assert picked is not None and picked.option_id == "once" diff --git a/tests/test_docs.py b/tests/test_docs.py index d545f9c..1c12475 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -74,7 +74,7 @@ def test_setup_zcode_points_at_mcp_settings_not_plugin_marketplace(): def test_orchestration_english_stays_under_char_budget(): text = _read("ORCHESTRATION.md") - assert len(text) <= 9500 + assert len(text) <= 10500 def test_readme_explains_coordinator_modes(): diff --git a/tests/test_registry.py b/tests/test_registry.py index 8569e5d..e0be3d0 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -1985,6 +1985,38 @@ async def slow_cancel(self, session): await registry.stop() +@pytest.mark.asyncio +async def test_worker_error_and_warnings_are_sanitized_before_persisting( + bridge_home, tmp_path, monkeypatch +): + """H-04: worker-derived text crosses into state.json/transcripts — scrub it.""" + secret = "tp-leak0123456789abcdef" + + async def leaky_turn(self, session, task): + return TurnResult( + text="", + stop_reason="error", + error=f"worker boom: Authorization: Bearer {secret} api_key={secret}", + warnings=[f"Set-Cookie: session={secret}"], + ) + + monkeypatch.setattr(FakeAdapter, "run_turn", leaky_turn) + registry = Registry.create(bridge_home) + await registry.start() + try: + dispatched = await registry.dispatch_task("fake", "leak", cwd=str(tmp_path)) + waited = await registry.wait_task(dispatched["task_id"], timeout_sec=5) + assert waited["status"] == "failed" + assert secret not in (waited["error"] or "") + assert "[redacted]" in (waited["error"] or "") + assert secret not in json.dumps(waited["warnings"]) + registry.save() + state = json.loads(state_path(bridge_home).read_text(encoding="utf-8")) + assert secret not in json.dumps(state) + finally: + await registry.stop() + + @pytest.mark.asyncio async def test_turn_timeout_sec_caps_chatty_turn(bridge_home, tmp_path, monkeypatch): """H-16: a worker emitting worker-type events forever still hits the cap.""" diff --git a/tests/test_worker_env.py b/tests/test_worker_env.py index c952443..d180afd 100644 --- a/tests/test_worker_env.py +++ b/tests/test_worker_env.py @@ -204,3 +204,64 @@ def test_is_worker_context_strict_value(): assert is_worker_context({WORKER_CONTEXT_ENV: "coordinator"}) is False assert is_worker_context({}) is False assert is_worker_context({WORKER_CONTEXT_ENV: "Worker"}) is False + + +def _env(base, *, config=None, overrides=None): + return build_worker_env( + overrides, + config=config, + base=base, + fallbacks={}, + user_env={}, + machine_env={}, + log_fill=False, + ) + + +def test_worker_env_no_default_deny_but_user_deny_applies(): + env = _env( + { + "AWS_REGION": "us-east-1", + "SSH_AUTH_SOCK": "/tmp/agent.sock", + "DATABASE_URL": "postgres://u:p@db", + "HOME": "/h", + } + ) + assert env["AWS_REGION"] == "us-east-1" + assert env["SSH_AUTH_SOCK"] == "/tmp/agent.sock" + assert env["DATABASE_URL"] == "postgres://u:p@db" + cfg = EnvConfig(deny=["AWS_*"]) + env = _env( + {"AWS_REGION": "us-east-1", "SSH_AUTH_SOCK": "/tmp/agent.sock"}, config=cfg + ) + assert "AWS_REGION" not in env + assert env["SSH_AUTH_SOCK"] == "/tmp/agent.sock" + + +def test_worker_env_explicit_set_and_agent_env_survive_deny(): + cfg = EnvConfig(set={"AWS_SECRET_ACCESS_KEY": "configured"}, deny=["AWS_*"]) + env = _env({}, config=cfg) + assert env["AWS_SECRET_ACCESS_KEY"] == "configured" + env = _env( + {"GITHUB_TOKEN": "agent-wants"}, + overrides={"GITHUB_TOKEN": "agent-wants"}, + config=EnvConfig(deny=["GITHUB_*"]), + ) + assert env["GITHUB_TOKEN"] == "agent-wants" + + +def test_worker_env_inherit_does_not_exempt_user_deny(): + # Both keys arrive via the process env; naming one in ``inherit`` does + # not exempt it from the user's deny globs. + cfg = EnvConfig(inherit=["AWS_SECRET_ACCESS_KEY"], deny=["AWS_*"]) + env = _env({"AWS_SECRET_ACCESS_KEY": "s3cr3t", "AWS_REGION": "us"}, config=cfg) + assert "AWS_SECRET_ACCESS_KEY" not in env + assert "AWS_REGION" not in env + + +def test_worker_env_user_deny_patterns(): + cfg = EnvConfig(deny=["*_DEBUG", "CUSTOM*"]) + env = _env({"APP_DEBUG": "1", "CUSTOM_FLAG": "x", "KEEP": "y"}, config=cfg) + assert "APP_DEBUG" not in env + assert "CUSTOM_FLAG" not in env + assert env["KEEP"] == "y" From cf17c2970ec15c9f2ae4eeb0bd2cb51a795ffd3c Mon Sep 17 00:00:00 2001 From: FeiZhuLulu <279467036+FeiZhuLulu@users.noreply.github.com> Date: Tue, 6 Oct 2026 19:24:54 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AF=84=E5=AE=A1?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9A=E8=B7=A8=E5=AE=9E=E4=BE=8B=20silent?= =?UTF-8?q?=5Ffor=5Fsec=20=E7=BD=AE=E7=A9=BA=E3=80=81=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E4=BA=8C=E6=AC=A1=E6=88=AA=E6=96=AD=E3=80=81?= =?UTF-8?q?=E5=AD=A4=E5=84=BF=E6=B8=85=E6=89=AB=E5=8A=A0=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=8A=A4=E6=A0=8F=E3=80=81env.deny=20=E5=9C=A8=E6=AF=8F?= =?UTF-8?q?=E6=AC=A1=20spawn=20=E5=89=8D=E7=BB=9F=E4=B8=80=E5=85=9C?= =?UTF-8?q?=E5=BA=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agent_bridge/adapters/acp.py | 13 ++-- src/agent_bridge/adapters/antigravity.py | 4 +- src/agent_bridge/adapters/base.py | 14 ++++ src/agent_bridge/adapters/codex.py | 1 + src/agent_bridge/dsh_home.py | 11 +--- src/agent_bridge/probes.py | 9 ++- src/agent_bridge/registry.py | 17 +++-- src/agent_bridge/worker_env.py | 27 ++++++-- tests/test_claude_acp.py | 23 ++++++- tests/test_dsh_home.py | 25 ++++++++ tests/test_registry.py | 82 +++++++++++++++++++++++- tests/test_worker_env.py | 19 ++++++ 12 files changed, 217 insertions(+), 28 deletions(-) diff --git a/src/agent_bridge/adapters/acp.py b/src/agent_bridge/adapters/acp.py index 7b96597..b3cc896 100644 --- a/src/agent_bridge/adapters/acp.py +++ b/src/agent_bridge/adapters/acp.py @@ -697,10 +697,12 @@ def __init__(self, agent: AgentConfig, home: Path, env_config=None) -> None: def _env(self) -> dict[str, str]: env = build_worker_env(self.agent.env, config=self.env_config, worker_context=True, home=self.home) if self.agent.name == "claude": - return apply_claude_gateway_env(env) - if self.agent.name == "devin": - return apply_devin_env(env) - return env + env = apply_claude_gateway_env(env) + elif self.agent.name == "devin": + env = apply_devin_env(env) + # Gateway derivation above can recreate a denied key; the deny gate + # runs on the rewritten env, not on build_worker_env's output. + return self._enforce_spawn_env(env) async def _drain_stderr( self, @@ -791,6 +793,7 @@ async def _cursor_models( ) -> dict[str, str]: if self._cursor_models_cache is not None: return self._cursor_models_cache + env = self._enforce_spawn_env(env) try: model_cmd = cursor_list_models_command(command) proc = await asyncio.create_subprocess_exec( @@ -845,7 +848,6 @@ async def _spawn(self, session: Session) -> _Live: session_id=session.session_id, model=session.model, effort=session.effort, - env_cfg=self.env_config, ) elif self.agent.name == "grok": cmd = with_grok_cli_selection(cmd, session.model, session.effort) @@ -862,6 +864,7 @@ async def _spawn(self, session: Session) -> _Live: "model_discovery", session, kind="unsupported_model", ) cmd = with_cursor_cli_model(cmd, session.model) + env = self._enforce_spawn_env(env) kwargs: dict[str, Any] = {} if sys.platform == "win32": kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP diff --git a/src/agent_bridge/adapters/antigravity.py b/src/agent_bridge/adapters/antigravity.py index 3b8ce8f..8795b9f 100644 --- a/src/agent_bridge/adapters/antigravity.py +++ b/src/agent_bridge/adapters/antigravity.py @@ -316,7 +316,9 @@ async def run_turn(self, session: Session, task: Task) -> TurnResult: conversation (those counters cover every turn so far); otherwise ``turn``. """ cmd = self._build_cmd(session, task) - env = build_worker_env(self.agent.env, config=self.env_config, worker_context=True, home=self.home) + env = self._enforce_spawn_env( + build_worker_env(self.agent.env, config=self.env_config, worker_context=True, home=self.home) + ) kwargs: dict[str, Any] = {} if sys.platform == "win32": kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP diff --git a/src/agent_bridge/adapters/base.py b/src/agent_bridge/adapters/base.py index a242382..e4f23b4 100644 --- a/src/agent_bridge/adapters/base.py +++ b/src/agent_bridge/adapters/base.py @@ -1,10 +1,12 @@ from __future__ import annotations from abc import ABC, abstractmethod +from collections.abc import Mapping from pathlib import Path from agent_bridge.config import AgentConfig, EnvConfig from agent_bridge.models import Session, Task, TurnResult +from agent_bridge.worker_env import enforce_env_deny # asyncio's default StreamReader limit is 64KB. Workers emit one JSON event # per line, and a single edit event can embed a whole file, so long lines are @@ -30,6 +32,18 @@ def __init__(self, agent: AgentConfig, home: Path, env_config: EnvConfig | None def can_revive(self) -> bool: return self.agent.revivable + def _enforce_spawn_env(self, env: Mapping[str, str]) -> dict[str, str]: + """Last ``env.deny`` gate on the exact env a worker spawn receives. + + Agent rewrites after build_worker_env (gateway keys, DSH refills) + can recreate a denied key; run this on the final env, not earlier. + """ + return enforce_env_deny( + env, + self.env_config, + set(self.env_config.set) | set(self.agent.env), + ) + @abstractmethod async def ensure_session(self, session: Session) -> None: ... diff --git a/src/agent_bridge/adapters/codex.py b/src/agent_bridge/adapters/codex.py index 72fb29c..8d5ff0b 100644 --- a/src/agent_bridge/adapters/codex.py +++ b/src/agent_bridge/adapters/codex.py @@ -105,6 +105,7 @@ async def _write_prompt(self, proc: asyncio.subprocess.Process, message: str) -> async def run_turn(self, session: Session, task: Task) -> TurnResult: env = self._worker_env() cmd = self._build_cmd(session, task, env) + env = self._enforce_spawn_env(env) kwargs: dict[str, Any] = {} if sys.platform == "win32": kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP diff --git a/src/agent_bridge/dsh_home.py b/src/agent_bridge/dsh_home.py index b9626b8..52cbc3e 100644 --- a/src/agent_bridge/dsh_home.py +++ b/src/agent_bridge/dsh_home.py @@ -7,12 +7,10 @@ from collections.abc import Mapping from pathlib import Path -from agent_bridge.config import EnvConfig from agent_bridge.models import dsh_effort from agent_bridge.paths import bridge_home, bundled_dsh_cordis from agent_bridge.processes import resolve_command from agent_bridge.worker_env import ( - denied_by_policy, pick_env_keys, read_windows_machine_env, read_windows_user_env, @@ -479,14 +477,13 @@ def apply_dsh_worker_env( effort: str | None = None, user_env: Mapping[str, str] | None = None, machine_env: Mapping[str, str] | None = None, - env_cfg: EnvConfig | None = None, ) -> dict[str, str]: """Attach DSH home, the user's saved default model, and credential env refs. No provider is required. Official DeepSeek is only one of the routes DSH already knows; users configure providers in ``$DSH_HOME/settings.yaml``. - ``env_cfg`` re-applies the env deny policy to keys filled from the host's - user/machine env so a denied credential cannot re-enter here. + env.deny is not consulted here: the caller passes the returned dict + through enforce_env_deny at the spawn site, which is the single gate. """ out = dict(env) home = dsh_home(out) @@ -503,8 +500,6 @@ def apply_dsh_worker_env( user = user_env if user_env is not None else read_windows_user_env() for incoming in (machine, user): for key, value in pick_env_keys(incoming, extra_keys).items(): - if key not in out and env_cfg is not None and denied_by_policy(key, env_cfg): - continue out.setdefault(key, value) roots = _node_module_roots(command or []) if roots: @@ -542,7 +537,6 @@ def prepare_dsh_launch( effort: str | None = None, user_env: Mapping[str, str] | None = None, machine_env: Mapping[str, str] | None = None, - env_cfg: EnvConfig | None = None, ) -> tuple[list[str], dict[str, str]]: return with_bridge_cordis(command), apply_dsh_worker_env( env, @@ -552,5 +546,4 @@ def prepare_dsh_launch( effort=effort, user_env=user_env, machine_env=machine_env, - env_cfg=env_cfg, ) diff --git a/src/agent_bridge/probes.py b/src/agent_bridge/probes.py index 390f75d..6f51474 100644 --- a/src/agent_bridge/probes.py +++ b/src/agent_bridge/probes.py @@ -21,7 +21,7 @@ from agent_bridge.kimi_observe import kimi_home from agent_bridge.minimax_meta import describe_minimax_auth, minimax_data_dir from agent_bridge.processes import reap_subprocess, resolve_command -from agent_bridge.worker_env import build_worker_env +from agent_bridge.worker_env import build_worker_env, enforce_env_deny from agent_bridge.zcode_meta import describe_zcode_auth, zcode_home log = logging.getLogger(__name__) @@ -222,11 +222,16 @@ def _resolve_probe_command() -> list[str]: if cfg.name == "devin": resolved = apply_devin_env(resolved) + # The rewritten env is handed to a spawned CLI; re-apply env.deny so + # apply_devin_env cannot reintroduce a denied key. + env_cfg = env_config or EnvConfig() + spawn_env = enforce_env_deny(resolved, env_cfg, set(env_cfg.set) | set(cfg.env)) details.append( "model=ids the session advertises (`devin models list`), level is part of the id " "e.g. swe-1-7-medium, claude-opus-5-high; no effort option; mode forced to bypass" ) - details.append(f"auth={await _devin_auth(command[0], resolved)}") + details.append(f"auth={await _devin_auth(command[0], spawn_env)}") + resolved = spawn_env if resolved.get("WINDSURF_API_KEY"): details.append("WINDSURF_API_KEY=set") diff --git a/src/agent_bridge/registry.py b/src/agent_bridge/registry.py index f18b06f..ab328e3 100644 --- a/src/agent_bridge/registry.py +++ b/src/agent_bridge/registry.py @@ -109,7 +109,10 @@ # state.json is an index, not a data store: task message/result bodies are # persisted truncated so a long-lived Bridge keeps each write bounded. STATE_MESSAGE_MAX = 2000 -STATE_RESULT_TEXT_MAX = 6000 +# A sibling may write a result artifact before its task row lands in +# state.json; the sweep lock does not cover the artifact write, so only +# delete orphans old enough that a live write cannot still be in flight. +ORPHAN_RESULT_MIN_AGE_SEC = 3600 # Persisted request_id bindings kept; bindings also drop with their task. REQUEST_KEEP_MAX = 1000 @@ -246,10 +249,11 @@ def _merge_owned( def _state_task_row(task: Task) -> dict: row = task.model_dump(mode="json") # Full bodies already live in result files and transcripts; writing - # them into state.json again grew the payload without bound. + # them into state.json again grew the payload without bound. Keep + # result_text whole: when the artifact write failed it is the only + # copy (RESULT_STORE_MAX), and trimming it would lose the tail. if len(row["message"]) > STATE_MESSAGE_MAX: row["message"] = row["message"][:STATE_MESSAGE_MAX] - row["result_text"] = _tail(row["result_text"], STATE_RESULT_TEXT_MAX) return row def _own_rows(self) -> dict[str, list[dict]]: @@ -1084,6 +1088,11 @@ def _sweep_orphan_results(self, foreign_task_ids: set[str]) -> None: if task_id in known: continue try: + if time.time() - orphan.stat().st_mtime < ORPHAN_RESULT_MIN_AGE_SEC: + # Recent file: a sibling may have written the artifact + # before its task row reached disk (the write lock does + # not cover it), so keep it for the next boot's sweep. + continue orphan.unlink() except OSError: log.warning("could not remove orphan result %s", orphan) @@ -1339,7 +1348,7 @@ def _task_snapshot(self, task: Task, include_result: bool = False) -> dict: ) payload["silent_for_sec"] = ( int(worker_silence_sec(task.session_id, self.home) or 0) - if task.status == TaskStatus.running + if task.status == TaskStatus.running and task.task_id in self.tasks else None ) cfg = self.config.agents.get(task.agent) diff --git a/src/agent_bridge/worker_env.py b/src/agent_bridge/worker_env.py index a048b3b..45ef1ff 100644 --- a/src/agent_bridge/worker_env.py +++ b/src/agent_bridge/worker_env.py @@ -5,7 +5,7 @@ import os import re import sys -from collections.abc import Mapping, Sequence +from collections.abc import Iterable, Mapping, Sequence from pathlib import Path from typing import Any from urllib.parse import urlsplit, urlunsplit @@ -514,9 +514,8 @@ def denied_by_policy(key: str, cfg: EnvConfig, explicit: set[str] | None = None) There is no built-in denylist: a default deny hid AWS_*/SSH_*/etc. from workers that legitimately need them (Bedrock/Vertex auth, git-over-ssh), so only the operator's configured globs strip keys. Shared by - _apply_env_deny and injection points downstream of it (e.g. the DSH - launch helper pulling credential refs from Windows env) so a denied key - cannot re-enter through another code path. + _apply_env_deny and enforce_env_deny so a denied key cannot re-enter + through post-build rewriting. """ if explicit and key in explicit: return False @@ -524,6 +523,26 @@ def denied_by_policy(key: str, cfg: EnvConfig, explicit: set[str] | None = None) return any(fnmatch.fnmatchcase(key, pat) for pat in user_deny) +def enforce_env_deny( + env: Mapping[str, str], + cfg: EnvConfig, + explicit: Iterable[str] = (), +) -> dict[str, str]: + """Re-apply the ``env.deny`` policy to a finalized spawn env. + + Agent-specific rewriting downstream of build_worker_env (gateway key + derivation, DSH credential refills) can recreate a denied key, so every + worker subprocess spawn passes its final env through this one gate. + ``explicit`` (env.set keys plus the agent's own env keys) still wins. + """ + allowed = set(explicit) + return { + key: value + for key, value in env.items() + if not denied_by_policy(key, cfg, allowed) + } + + def _apply_env_deny( env: dict[str, str], origin: dict[str, str], diff --git a/tests/test_claude_acp.py b/tests/test_claude_acp.py index f07f39a..515a40c 100644 --- a/tests/test_claude_acp.py +++ b/tests/test_claude_acp.py @@ -4,7 +4,7 @@ from agent_bridge.adapters.acp import AcpAdapter, _Live from agent_bridge.claude_meta import CLAUDE_MODE_BYPASS -from agent_bridge.config import AgentConfig +from agent_bridge.config import AgentConfig, EnvConfig from agent_bridge.models import Session MODEL_OPTION = { @@ -257,3 +257,24 @@ async def test_non_claude_agents_are_untouched_by_the_claude_path(): assert live.conn.calls == [] adapter._remember_config_options(live, FakeResponse(config_options=[MODEL_OPTION])) assert live.config_options == [] + + +def test_env_deny_survives_claude_gateway_rewrite(tmp_path, monkeypatch): + """apply_claude_gateway_env recreates ANTHROPIC_AUTH_TOKEN from + OPENROUTER_API_KEY after build_worker_env's deny pass; the final spawn + gate must still strip it.""" + for key in ("ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_API_KEY", "ANTHROPIC_BASE_URL"): + monkeypatch.delenv(key, raising=False) + adapter = AcpAdapter( + AgentConfig( + name="claude", + protocol="acp", + command=["claude-agent-acp"], + env={"OPENROUTER_API_KEY": "or-test"}, + ), + tmp_path, + EnvConfig(deny=["ANTHROPIC_AUTH_TOKEN"]), + ) + env = adapter._env() + assert env["OPENROUTER_API_KEY"] == "or-test" + assert "ANTHROPIC_AUTH_TOKEN" not in env diff --git a/tests/test_dsh_home.py b/tests/test_dsh_home.py index 7867534..702c264 100644 --- a/tests/test_dsh_home.py +++ b/tests/test_dsh_home.py @@ -1,5 +1,6 @@ from pathlib import Path +from agent_bridge.config import EnvConfig from agent_bridge.dsh_home import ( api_key_env_names, apply_dsh_worker_env, @@ -15,6 +16,7 @@ with_bridge_cordis, ) from agent_bridge.paths import bundled_dsh_cordis +from agent_bridge.worker_env import enforce_env_deny def test_reads_any_user_default_model(tmp_path: Path): @@ -74,6 +76,29 @@ def test_apply_env_uses_settings_not_a_hardcoded_vendor(tmp_path: Path): assert "DSH_SNAPSHOT_SESSIONS_ROOT" not in env +def test_denied_key_refilled_from_user_env_is_stripped_at_spawn_gate(tmp_path: Path): + """A denied credential re-filled from the host user/machine env does not + survive the single spawn-site gate.""" + (tmp_path / "settings.yaml").write_text( + """ +llm-pi-ai: + providers: + acme: + apiKeyEnv: ACME_GATEWAY_API_KEY +""", + encoding="utf-8", + ) + env = apply_dsh_worker_env( + {"DSH_HOME": str(tmp_path)}, + user_env={"ACME_GATEWAY_API_KEY": "sk-from-user"}, + machine_env={}, + ) + assert env["ACME_GATEWAY_API_KEY"] == "sk-from-user" + cfg = EnvConfig(deny=["ACME_*"]) + spawn_env = enforce_env_deny(env, cfg) + assert "ACME_GATEWAY_API_KEY" not in spawn_env + + def test_session_persistence_lives_under_bridge_home(tmp_path: Path, monkeypatch): home = tmp_path / "bridge" monkeypatch.setenv("AGENT_BRIDGE_HOME", str(home)) diff --git a/tests/test_registry.py b/tests/test_registry.py index e0be3d0..2781d9b 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -5,6 +5,7 @@ import contextlib import json import logging +import os import sys import threading import time @@ -363,13 +364,20 @@ async def test_start_sweeps_orphan_result_files(bridge_home, monkeypatch): ) results = bridge_home / "results" results.mkdir(parents=True, exist_ok=True) - (results / "task_orphan.txt").write_text("orphan", encoding="utf-8") + stale = results / "task_orphan.txt" + stale.write_text("orphan", encoding="utf-8") + old = time.time() - 7200 + os.utime(stale, (old, old)) + # A fresh orphan may belong to a sibling that wrote the artifact before + # its task row reached disk — the sweep must leave it alone. + (results / "task_orphan_fresh.txt").write_text("recent", encoding="utf-8") (results / "task_foreign.txt").write_text("kept", encoding="utf-8") registry = Registry.create(bridge_home) await registry.start() try: - assert not (results / "task_orphan.txt").exists() + assert not stale.exists() + assert (results / "task_orphan_fresh.txt").read_text(encoding="utf-8") == "recent" assert (results / "task_foreign.txt").read_text(encoding="utf-8") == "kept" finally: await registry.stop() @@ -413,6 +421,76 @@ async def test_check_task_reads_sibling_task_from_state(bridge_home, monkeypatch await registry.stop() +@pytest.mark.asyncio +async def test_check_task_from_state_reports_null_silence(bridge_home, monkeypatch): + """A sibling's running task read from disk has no live silence data.""" + monkeypatch.setattr("agent_bridge.registry.owner_alive", lambda pid, create_time: True) + foreign = Task( + task_id="task_remote_run", + session_id="sess_remote_run", + agent="fake", + message="running elsewhere", + cwd=str(Path.cwd()), + status=TaskStatus.running, + started_at=iso(), + owner_pid=9999, + owner_create_time=1.0, + ) + atomic_write_json( + state_path(bridge_home), + {"sessions": [], "tasks": [foreign.model_dump(mode="json")]}, + ) + registry = Registry.create(bridge_home) + await registry.start() + try: + snap = registry.check_task("task_remote_run") + assert snap["from_state"] is True + assert snap["silent_for_sec"] is None + finally: + await registry.stop() + + +@pytest.mark.asyncio +async def test_result_survives_restart_when_artifact_write_fails( + bridge_home, tmp_path, monkeypatch +): + """With the result file unwritable, the state.json row is the only copy; + it must carry the whole RESULT_STORE_MAX text, not a 6000-char tail.""" + import agent_bridge.registry as registry_mod + + original = registry_mod.atomic_write_text + + def fail_result_write(path, text, *args, **kwargs): + if Path(path).parent.name == "results": + raise OSError("disk full") + return original(path, text, *args, **kwargs) + + monkeypatch.setattr(registry_mod, "atomic_write_text", fail_result_write) + big = "z" * 20000 + + async def big_turn(self, session, task): + return TurnResult(text=big) + + monkeypatch.setattr(FakeAdapter, "run_turn", big_turn) + registry = Registry.create(bridge_home) + await registry.start() + try: + dispatched = await registry.dispatch_task("fake", "big", cwd=str(tmp_path)) + waited = await registry.wait_task(dispatched["task_id"], timeout_sec=5) + assert waited["status"] == "completed" + finally: + await registry.stop() + + again = Registry.create(bridge_home) + await again.start() + try: + result = again.get_result(dispatched["task_id"]) + assert result["result_text"] == big + assert len(result["result_text"]) == 20000 + finally: + await again.stop() + + def test_sibling_state_updates_are_serialized(bridge_home, tmp_path, monkeypatch): monkeypatch.setattr("agent_bridge.registry.owner_alive", lambda pid, create_time: True) original_read = read_json_strict diff --git a/tests/test_worker_env.py b/tests/test_worker_env.py index d180afd..97234d0 100644 --- a/tests/test_worker_env.py +++ b/tests/test_worker_env.py @@ -5,6 +5,7 @@ apply_proxy_fallbacks, build_worker_env, describe_env, + enforce_env_deny, is_worker_context, parse_powershell_grok_proxy, parse_win_inet_proxy_server, @@ -265,3 +266,21 @@ def test_worker_env_user_deny_patterns(): assert "APP_DEBUG" not in env assert "CUSTOM_FLAG" not in env assert env["KEEP"] == "y" + + +def test_enforce_env_deny_on_rewritten_spawn_env(): + """The final spawn gate strips keys that post-build rewriting recreated; + keys explicitly set via env.set or the agent's own env still win.""" + cfg = EnvConfig( + set={"ANTHROPIC_AUTH_TOKEN": "explicit"}, + deny=["ANTHROPIC_*"], + ) + env = { + "ANTHROPIC_AUTH_TOKEN": "recreated-by-rewrite", + "ANTHROPIC_API_KEY": "recreated", + "OPENROUTER_API_KEY": "or", + } + out = enforce_env_deny(env, cfg, set(cfg.set) | {"OPENROUTER_API_KEY"}) + assert out["ANTHROPIC_AUTH_TOKEN"] == "recreated-by-rewrite" + assert "ANTHROPIC_API_KEY" not in out + assert out["OPENROUTER_API_KEY"] == "or" From 9d6f77e1221e82d1096b7b692325ea57e520d526 Mon Sep 17 00:00:00 2001 From: FeiZhuLulu <279467036+FeiZhuLulu@users.noreply.github.com> Date: Tue, 6 Oct 2026 19:32:41 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E8=AF=84=E5=AE=A1=E8=B7=9F=E8=BF=9B?= =?UTF-8?q?=EF=BC=9Adeny=20=E6=B0=B8=E4=B8=8D=E5=89=A5=E7=A6=BB=20Bridge?= =?UTF-8?q?=20=E8=87=AA=E6=9C=89=E9=94=AE=EF=BC=9Bclaude=20=E5=9C=A8=20AUT?= =?UTF-8?q?H=5FTOKEN=20=E8=A2=AB=E6=8B=92=E6=97=B6=E8=B7=B3=E8=BF=87?= =?UTF-8?q?=E7=BD=91=E5=85=B3=E6=94=B9=E5=86=99=E4=BF=9D=E4=BD=8F=E7=9B=B4?= =?UTF-8?q?=E8=BF=9E=E5=AF=86=E9=92=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SETUP.md | 2 +- src/agent_bridge/adapters/acp.py | 13 +++++++++++-- src/agent_bridge/worker_env.py | 8 +++++++- tests/test_claude_acp.py | 29 ++++++++++++++++++++++++++--- tests/test_worker_env.py | 22 ++++++++++++++++++++++ 5 files changed, 67 insertions(+), 7 deletions(-) diff --git a/SETUP.md b/SETUP.md index 6c36eaf..1268d6f 100644 --- a/SETUP.md +++ b/SETUP.md @@ -287,7 +287,7 @@ Bridge rebuilds the environment **once at startup** (and again for each worker s | `[env.set]` | Explicit key/value map | | `[agents..env]` | Per-worker overlay | -`[env] deny = ["GLOB", ...]` removes worker env keys matching those globs. Keys set explicitly via `[env] set` or the agent's `env` always survive. There is no built-in denylist. +`[env] deny = ["GLOB", ...]` removes worker env keys matching those globs. Keys set explicitly via `[env] set` or the agent's `env` always survive, and Bridge's own `AGENT_BRIDGE_HOME` and worker-context marker are never removed. There is no built-in denylist. Do **not** put secrets in the repo `agents.toml`. Pin machine-local values in `%USERPROFILE%\.agent-bridge\agents.toml` (see [agents.toml.example](agents.toml.example)): diff --git a/src/agent_bridge/adapters/acp.py b/src/agent_bridge/adapters/acp.py index b3cc896..362342a 100644 --- a/src/agent_bridge/adapters/acp.py +++ b/src/agent_bridge/adapters/acp.py @@ -51,7 +51,7 @@ resolve_command, ) from agent_bridge.transcript import append_event -from agent_bridge.worker_env import build_worker_env +from agent_bridge.worker_env import build_worker_env, denied_by_policy from agent_bridge.workspace import collect_update_paths from agent_bridge.zcode_meta import ( ZCODE_MODE_YOLO, @@ -697,7 +697,16 @@ def __init__(self, agent: AgentConfig, home: Path, env_config=None) -> None: def _env(self) -> dict[str, str]: env = build_worker_env(self.agent.env, config=self.env_config, worker_context=True, home=self.home) if self.agent.name == "claude": - env = apply_claude_gateway_env(env) + # The gateway rewrite exists only to install ANTHROPIC_AUTH_TOKEN; + # when the user denies that token, skip the rewrite entirely so a + # direct ANTHROPIC_API_KEY is not blanked for a token the gate + # would strip anyway. + if not denied_by_policy( + "ANTHROPIC_AUTH_TOKEN", + self.env_config, + set(self.env_config.set) | set(self.agent.env), + ): + env = apply_claude_gateway_env(env) elif self.agent.name == "devin": env = apply_devin_env(env) # Gateway derivation above can recreate a denied key; the deny gate diff --git a/src/agent_bridge/worker_env.py b/src/agent_bridge/worker_env.py index 45ef1ff..ef6ddfb 100644 --- a/src/agent_bridge/worker_env.py +++ b/src/agent_bridge/worker_env.py @@ -508,6 +508,12 @@ def install_host_env(config: EnvConfig, *, base: Mapping[str, str] | None = None return status +# Bridge's own wiring must survive every deny pattern: stripping the +# worker-context marker or the nested AGENT_BRIDGE_HOME would let a nested +# Bridge run as a coordinator on the default home. +BRIDGE_OWNED_ENV_KEYS = frozenset({WORKER_CONTEXT_ENV, "AGENT_BRIDGE_HOME"}) + + def denied_by_policy(key: str, cfg: EnvConfig, explicit: set[str] | None = None) -> bool: """True when the user's ``env.deny`` globs would strip ``key`` from a worker env. @@ -517,7 +523,7 @@ def denied_by_policy(key: str, cfg: EnvConfig, explicit: set[str] | None = None) _apply_env_deny and enforce_env_deny so a denied key cannot re-enter through post-build rewriting. """ - if explicit and key in explicit: + if key in BRIDGE_OWNED_ENV_KEYS or (explicit and key in explicit): return False user_deny = [str(pat) for pat in (cfg.deny or []) if str(pat).strip()] return any(fnmatch.fnmatchcase(key, pat) for pat in user_deny) diff --git a/tests/test_claude_acp.py b/tests/test_claude_acp.py index 515a40c..0a0f46f 100644 --- a/tests/test_claude_acp.py +++ b/tests/test_claude_acp.py @@ -260,9 +260,8 @@ async def test_non_claude_agents_are_untouched_by_the_claude_path(): def test_env_deny_survives_claude_gateway_rewrite(tmp_path, monkeypatch): - """apply_claude_gateway_env recreates ANTHROPIC_AUTH_TOKEN from - OPENROUTER_API_KEY after build_worker_env's deny pass; the final spawn - gate must still strip it.""" + """With ANTHROPIC_AUTH_TOKEN denied, the gateway rewrite is skipped and + the token never reaches the spawn env.""" for key in ("ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_API_KEY", "ANTHROPIC_BASE_URL"): monkeypatch.delenv(key, raising=False) adapter = AcpAdapter( @@ -278,3 +277,27 @@ def test_env_deny_survives_claude_gateway_rewrite(tmp_path, monkeypatch): env = adapter._env() assert env["OPENROUTER_API_KEY"] == "or-test" assert "ANTHROPIC_AUTH_TOKEN" not in env + + +def test_env_deny_keeps_direct_key_when_token_denied(tmp_path, monkeypatch): + """Denying ANTHROPIC_AUTH_TOKEN must not blank a direct ANTHROPIC_API_KEY: + the gateway rewrite only exists to install that token, so it is skipped.""" + for key in ("ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_API_KEY", "ANTHROPIC_BASE_URL", "OPENROUTER_API_KEY"): + monkeypatch.delenv(key, raising=False) + adapter = AcpAdapter( + AgentConfig( + name="claude", + protocol="acp", + command=["claude-agent-acp"], + env={ + "ANTHROPIC_API_KEY": "direct-key", + "ANTHROPIC_BASE_URL": "https://gateway.example", + "OPENROUTER_API_KEY": "or-x", + }, + ), + tmp_path, + EnvConfig(deny=["ANTHROPIC_AUTH_TOKEN"]), + ) + env = adapter._env() + assert env["ANTHROPIC_API_KEY"] == "direct-key" + assert "ANTHROPIC_AUTH_TOKEN" not in env diff --git a/tests/test_worker_env.py b/tests/test_worker_env.py index 97234d0..f41ee2e 100644 --- a/tests/test_worker_env.py +++ b/tests/test_worker_env.py @@ -268,6 +268,28 @@ def test_worker_env_user_deny_patterns(): assert env["KEEP"] == "y" +def test_bridge_owned_keys_survive_deny_star(tmp_path): + """deny=["*"] must not strip the worker-context marker or nested + AGENT_BRIDGE_HOME — that would break nested-Bridge isolation.""" + cfg = EnvConfig(deny=["*"]) + env = build_worker_env( + base={"SECRET_KEY": "s"}, + config=cfg, + user_env={}, + machine_env={}, + log_fill=False, + worker_context=True, + home=tmp_path, + ) + assert env[WORKER_CONTEXT_ENV] == WORKER_CONTEXT_VALUE + assert "AGENT_BRIDGE_HOME" in env + assert "SECRET_KEY" not in env + out = enforce_env_deny(env, cfg) + assert out[WORKER_CONTEXT_ENV] == WORKER_CONTEXT_VALUE + assert "AGENT_BRIDGE_HOME" in out + assert "SECRET_KEY" not in out + + def test_enforce_env_deny_on_rewritten_spawn_env(): """The final spawn gate strips keys that post-build rewriting recreated; keys explicitly set via env.set or the agent's own env still win."""