From 1c214b57115ff697afc3f4c3fda08a55ae064c27 Mon Sep 17 00:00:00 2001 From: "edge.xyz" <32131214+edgexyz@users.noreply.github.com> Date: Fri, 16 Oct 2026 21:24:12 -0400 Subject: [PATCH 1/6] Add on-demand worker model discovery via MCP --- ORCHESTRATION.md | 4 +- ORCHESTRATION.zh-CN.md | 4 + README.md | 17 + SETUP.md | 37 ++ agents.toml | 5 + scripts/smoke_models.py | 80 ++++ skills/agent-bridge/SKILL.md | 6 +- src/agent_bridge/adapters/acp.py | 176 ++++++++ src/agent_bridge/adapters/antigravity.py | 32 ++ src/agent_bridge/adapters/base.py | 5 + src/agent_bridge/adapters/codex.py | 32 ++ src/agent_bridge/agents.toml | 5 + src/agent_bridge/config.py | 18 +- src/agent_bridge/model_catalog.py | 42 ++ src/agent_bridge/model_discovery_exec.py | 473 +++++++++++++++++++++ src/agent_bridge/registry.py | 48 +++ src/agent_bridge/server.py | 14 + tests/test_model_catalog.py | 171 ++++++++ tests/test_model_discovery_acp.py | 308 ++++++++++++++ tests/test_model_discovery_exec.py | 510 +++++++++++++++++++++++ tests/test_server_tools.py | 3 +- 21 files changed, 1986 insertions(+), 4 deletions(-) create mode 100644 scripts/smoke_models.py create mode 100644 src/agent_bridge/model_catalog.py create mode 100644 src/agent_bridge/model_discovery_exec.py create mode 100644 tests/test_model_catalog.py create mode 100644 tests/test_model_discovery_acp.py create mode 100644 tests/test_model_discovery_exec.py diff --git a/ORCHESTRATION.md b/ORCHESTRATION.md index 3554677..3457565 100644 --- a/ORCHESTRATION.md +++ b/ORCHESTRATION.md @@ -14,7 +14,7 @@ Call `list_agents` first and re-read `coordinator` before every dispatch. When the user states a **lasting** preference, persist it with `set_preferences`. Its `instructions` argument replaces the stored text — read the current value first and write the merge. One-off wishes are not preferences. -Workers are reached **only** through Agent Bridge MCP tools (`list_agents`, `dispatch_task`, `wait_task`, `check_task`, `get_result`, `get_transcript`, `cancel_task`, `list_sessions`, `end_session`). If those tools are missing, stop and say so. Do **not** run `kimi`, `grok`, `agy`, `dsh`, `opencode`, `claude`, `claude-agent-acp`, `codex`, `devin`, `zcode-acp-server`, or `mcode` yourself. `git` / `pytest` after a turn is review, not a substitute for dispatch. +Workers are reached **only** through Agent Bridge MCP tools (`list_agents`, `list_models`, `dispatch_task`, `wait_task`, `check_task`, `get_result`, `get_transcript`, `cancel_task`, `list_sessions`, `end_session`). If those tools are missing, stop and say so. Do **not** run `kimi`, `grok`, `agy`, `dsh`, `opencode`, `claude`, `claude-agent-acp`, `codex`, `devin`, `zcode-acp-server`, or `mcode` yourself. `git` / `pytest` after a turn is review, not a substitute for dispatch. ## Step 1 — dispatch, or do it yourself? @@ -49,6 +49,8 @@ In `auto`/`eager`, tell the user after the fact. In `manual`, their explicit req ## How to dispatch +Before selecting a model, use supported workers' `list_models(agent,cwd)`; see SETUP.md for caching, effort levels and session side effects. + 1. `list_agents`. Read `coordinator.mode` / `instructions` / `dispatch_enabled` and `env.proxy` / `env.warnings`. A null proxy on a direct network is normal; if a worker fails with connect errors on a proxied machine, fix `[env.proxy]` instead of retrying. 2. `dispatch_task` with `cwd` = **this conversation's project folder** (absolute). Never the Agent Bridge install path (unless the user is editing Bridge). 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. - Antigravity: `agy models` slugs; default `gemini-3.7-flash`. diff --git a/ORCHESTRATION.zh-CN.md b/ORCHESTRATION.zh-CN.md index 177f31f..0a76417 100644 --- a/ORCHESTRATION.zh-CN.md +++ b/ORCHESTRATION.zh-CN.md @@ -84,3 +84,7 @@ ACP 失败保留字符串 `error`,并可附带 `failure`。`failure.prompt_may 需要重试去重时,在首次调用 `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` 仍须由拥有它的实例调用。 + +## 按需查询模型目录 + +明确指定模型前,若 `list_agents.model_discovery.supported=true`,调用 `list_models(agent=..., cwd=...)` 获取 worker 公布的模型 ID。首批支持 Kimi、原生 DSH、Antigravity 和 Codex。默认缓存一小时,过期后仅在下一次查询时刷新;配置或账号变更后可用 `refresh=true`。`supported_efforts` 是 worker 原生档位,未知模型的档位保持 null,`dispatch_task.effort` 的五档输入与映射不变。目录不保证账号额度或调用成功。查询不发送模型提示、不改变活动会话;ACP 可能留下空的 worker 会话。worker 继承实例不能查询模型目录。 diff --git a/README.md b/README.md index a419687..38dcd48 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,7 @@ Close coordinators that are holding Bridge, then `agent-bridge upgrade`, then re | Tool | Role | | --- | --- | | `list_agents` | Probe workers, report remaining quota, proxy/env + coordinator policy | +| `list_models` | Discover advertised models for one worker/project, with cached results | | `set_preferences` | Persist coordinator mode / routing preferences | | `dispatch_task` | Start or resume a turn in the project `cwd` | | `wait_task` | Block up to `timeout_sec` (default 180) | @@ -114,6 +115,22 @@ Close coordinators that are holding Bridge, then `agent-bridge upgrade`, then re | `list_sessions` | Known sessions | | `end_session` | Shut down a worker process | +Call `list_models(agent="kimi", cwd="/absolute/project")` before selecting a +model. Initial support covers Kimi and native DSH through shared ACP discovery, +Antigravity through `agy models`, and Codex through app-server `model/list`. +`list_agents` reports `model_discovery` capability without querying catalogs. +Discovery never sends a model prompt or changes an active work session; ACP +discovery creates a separate worker-owned session that may remain in its history. +Catalog entries are advertised options, not guarantees of account access or quota. +`supported_efforts` contains worker-native levels only when known for that model; +`null` means unknown. `dispatch_task.effort` still accepts only +`off|low|medium|high|max` and applies the worker's mapping. +Catalogs are cached in memory per worker/project for one hour, refreshed only on +the next query after expiry. `refresh=true` bypasses the cache. Configure +`[models] cache_sec` / `timeout_sec` in `agents.toml` (defaults 3600 / 30). +Failed refreshes return errors without overwriting the last successful cache; +worker-inherited instances cannot launch discovery processes. + `get_result` returns up to 60,000 characters per call. Continue with `next_cursor` while `has_more` is true. Detailed work events remain available through `get_transcript` and `~/.agent-bridge/transcripts/`; sparse task diff --git a/SETUP.md b/SETUP.md index eddc7d9..83ab266 100644 --- a/SETUP.md +++ b/SETUP.md @@ -446,6 +446,43 @@ uv run --no-sync python scripts/setup_lab.py If a worker is missing, `list_agents` reports `available: false` and the coordinator should pick another worker or do the work itself. +### Model discovery + +`list_agents` includes a `model_discovery` capability block. Query a supported +worker on demand, using this conversation's absolute project directory: + +```text +list_models(agent="kimi", cwd="/absolute/project") +list_models(agent="antigravity", cwd="/absolute/project", refresh=true) +``` + +Initial support: Kimi and native DSH share the ACP `initialize` / `session/new` +discovery path; AGY uses `models`; Codex uses app-server `model/list` with +pagination. Claude and the remaining workers are not enabled in this release. +Discovery does not send a model prompt, configure model/effort, change active +sessions, or register a Bridge task. ACP may create an empty session in the +worker's own history; all discovery processes are closed after the lookup, and +permission requests during ACP discovery are denied. + +Results contain `models` (`id`, `name`, `is_default`, `supported_efforts`), +`default_model`, `source`, `fetched_at`, `cached`, and `warnings`. +`availability="advertised"` / `invocation_verified=false` distinguish a catalog +from a successful model request. Unknown defaults and model-specific effort +levels remain null. Effort levels are worker-native, not necessarily accepted +as `dispatch_task.effort` (whose vocabulary remains unchanged). + +```toml +[models] +cache_sec = 3600 # one hour; 0 disables reuse; no background polling +timeout_sec = 30 # complete lookup deadline, including worker initialization +``` + +The cache is per worker and project, in memory for this Bridge instance. Use +`refresh=true` after changing accounts, worker configuration, or model access. +A failed refresh is explicit and does not replace the last successful cache. +`list_agents` never launches model discovery, and worker-inherited Bridge +instances cannot call `list_models`. + The coordinator can pin a worker model and thinking intensity on `dispatch_task`: ```text diff --git a/agents.toml b/agents.toml index e6c1616..d22e5ef 100644 --- a/agents.toml +++ b/agents.toml @@ -253,3 +253,8 @@ command = ["codex"] # session_meta = { yolo = true } revivable = true idle_unload_sec = 0 + +# On-demand advertised model catalogs; no background polling. +[models] +cache_sec = 3600 +timeout_sec = 30 diff --git a/scripts/smoke_models.py b/scripts/smoke_models.py new file mode 100644 index 0000000..3470168 --- /dev/null +++ b/scripts/smoke_models.py @@ -0,0 +1,80 @@ +"""Exercise list_models through MCP without model prompts or global Bridge state. + + uv run python scripts/smoke_models.py --cwd /absolute/project + +Uses a temporary Bridge home under ignored lab/, preserving worker login homes. +ACP may leave empty discovery sessions in the worker's own history. +""" + +from __future__ import annotations + +import argparse +import asyncio +import json +import os +import shutil +import sys +import tempfile +from pathlib import Path +from typing import Any + +from mcp.client.session import ClientSession +from mcp.client.stdio import StdioServerParameters, stdio_client + +from agent_bridge.paths import bridge_home + + +def payload(result: Any) -> dict: + structured = result.structured_content + if isinstance(structured, dict): + return structured + for content in result.content: + if getattr(content, "type", None) == "text": + return json.loads(content.text) + raise RuntimeError("MCP result contained no JSON payload") + + +async def run(cwd: Path, agents: list[str], config_home: Path, output: Path | None) -> int: + lab = Path(__file__).resolve().parents[1] / "lab" + lab.mkdir(exist_ok=True) + results = {} + with tempfile.TemporaryDirectory(prefix="model-discovery-", dir=lab) as scratch: + overlay = config_home / "agents.toml" + if overlay.is_file(): + shutil.copyfile(overlay, Path(scratch) / "agents.toml") + params = StdioServerParameters( + command=sys.executable, + args=["-c", "from agent_bridge.server import mcp; mcp.run(transport='stdio')"], + cwd=str(cwd), + env={**os.environ, "AGENT_BRIDGE_HOME": scratch, "AGENT_BRIDGE_PARENT_CONTEXT": ""}, + ) + async with stdio_client(params) as streams, ClientSession(*streams, read_timeout_seconds=120) as session: + await session.initialize() + listing = await session.list_tools() + assert "list_models" in {tool.name for tool in listing.tools} + for agent in agents: + args = {"agent": agent, "cwd": str(cwd)} + discovered = payload(await session.call_tool("list_models", args)) + if discovered.get("ok"): + cached = payload(await session.call_tool("list_models", args)) + assert cached.get("cached") is True + assert cached["models"] == discovered["models"] + results[agent] = discovered + print(json.dumps({"agent": agent, "ok": discovered.get("ok"), + "models": len(discovered.get("models", [])), + "error": discovered.get("error")}, ensure_ascii=False), flush=True) + sessions = payload(await session.call_tool("list_sessions", {})) + assert sessions["sessions"] == [], "Discovery registered a Bridge work session" + if output: + output.write_text(json.dumps(results, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") + return 0 if all(result.get("ok") for result in results.values()) else 1 + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--cwd", type=Path, required=True) + parser.add_argument("--agents", nargs="+", default=["kimi", "dsh", "antigravity", "codex"]) + parser.add_argument("--config-home", type=Path, default=bridge_home()) + parser.add_argument("--output", type=Path) + args = parser.parse_args() + raise SystemExit(asyncio.run(run(args.cwd.resolve(), args.agents, args.config_home, args.output))) diff --git a/skills/agent-bridge/SKILL.md b/skills/agent-bridge/SKILL.md index 440fb3c..77fbe42 100644 --- a/skills/agent-bridge/SKILL.md +++ b/skills/agent-bridge/SKILL.md @@ -5,7 +5,7 @@ description: Coordinate local worker agents (Grok Build, Kimi Code, Antigravity, # Dispatching workers through Agent Bridge -You are the coordinator: users talk only to you, and you call the workers. Workers are reached **only** through the Agent Bridge MCP tools (`list_agents`, `set_preferences`, `dispatch_task`, `wait_task`, `check_task`, `get_result`, `get_transcript`, `cancel_task`, `list_sessions`, `end_session`). If those tools are missing from this session, stop and say so — never run `grok`, `kimi`, `agy`, `dsh`, `opencode`, `claude`, `claude-agent-acp`, `codex`, `devin`, `zcode-acp-server`, or `mcode` CLIs directly, and never drive their GUIs. The same product can be a coordinator and a worker; those are different processes. +You are the coordinator: users talk only to you, and you call the workers. Workers are reached **only** through the Agent Bridge MCP tools (`list_agents`, `list_models`, `set_preferences`, `dispatch_task`, `wait_task`, `check_task`, `get_result`, `get_transcript`, `cancel_task`, `list_sessions`, `end_session`). If those tools are missing from this session, stop and say so — never run `grok`, `kimi`, `agy`, `dsh`, `opencode`, `claude`, `claude-agent-acp`, `codex`, `devin`, `zcode-acp-server`, or `mcode` CLIs directly, and never drive their GUIs. The same product can be a coordinator and a worker; those are different processes. ## Before anything: read the policy @@ -19,6 +19,10 @@ Call `list_agents` first. Its result carries: Claude `status`: shared `5h`/`weekly` only. Before dispatch, even if `ok`, check the target model's `weekly:opus`/`weekly:sonnet`: 0% is exhausted; missing/null unknown. Report reset time or a user-permitted alternative. Model-only data leaves shared status unknown. +## Model discovery + +Use `list_models(agent=..., cwd=...)` to obtain confirmed worker-advertised model IDs before explicitly selecting a model. `list_agents.model_discovery` reports whether discovery is supported; initial support is Kimi, native DSH, Antigravity, and Codex. Supply this conversation's absolute project directory. Catalog presence does not prove account access or remaining quota. `supported_efforts` contains worker-native levels where known, while `dispatch_task.effort` still accepts `off|low|medium|high|max` and applies the adapter mapping. Unknown model-specific levels remain null. Catalogs are cached for one hour by default (`[models] cache_sec`); `refresh=true` forces an on-demand query. Discovery sends no prompt and does not configure an active session, but ACP may create an empty worker-owned session. Do not call `list_models` when `dispatch_enabled=false`. + ## Step 1 — dispatch, or do it yourself? A cost question, not a category question; "it is implementation work" is never by itself a reason to dispatch. diff --git a/src/agent_bridge/adapters/acp.py b/src/agent_bridge/adapters/acp.py index 362342a..8b0fd22 100644 --- a/src/agent_bridge/adapters/acp.py +++ b/src/agent_bridge/adapters/acp.py @@ -688,12 +688,188 @@ def __init__(self) -> None: self.pending_warnings: list[str] = [] +class _DiscoveryClient(_BridgeClient): + """Read-only handshake client, with no transcript or session bookkeeping.""" + + async def request_permission( + self, + session_id: str, + tool_call: Any, + options: list[Any] | None, + **kwargs: Any, + ) -> RequestPermissionResponse: + return RequestPermissionResponse(outcome=DeniedOutcome(outcome="cancelled")) + + async def session_update(self, session_id: str, update: Any, **kwargs: Any) -> None: + return None + + +def _discovery_field(value: Any, alias: str, field: str) -> Any: + if isinstance(value, dict): + return value.get(alias, value.get(field)) + return getattr(value, field, None) + + +def _acp_model_catalog(response: Any, agent_name: str) -> dict[str, Any]: + """Read a fresh session snapshot without switching any of its settings. + + Thought-level choices belong to the current model only. Other models' + support remains unknown unless a separate session snapshot advertises it. + """ + config_options = _discovery_field(response, "configOptions", "config_options") or [] + if not isinstance(config_options, (list, tuple)): + config_options = [] + options = list(config_options) + model_option_id = next( + ( + option["id"] + for item in options + if isinstance(option := _dump(item), dict) + and isinstance(option.get("id"), str) + and (option["id"] == "model" or option.get("category") == "model") + ), + "model", + ) + current, _offered = config_option_values(options, model_option_id) + choices: dict[str, str | None] = {} + + def model_id(value: str) -> str: + return (_dsh_native_model_label(value) or value) if agent_name == "dsh" else value + + for value, name in _cursor_choices(options, model_option_id): + choices[model_id(value)] = name if name != value else None + state = _discovery_field(response, "models", "models") + current = current or _discovery_field(state, "currentModelId", "current_model_id") + default = model_id(current) if isinstance(current, str) and current else None + available = _discovery_field(state, "availableModels", "available_models") or [] + if isinstance(available, (list, tuple)): + for entry in available: + value = _discovery_field(entry, "modelId", "model_id") + name = _discovery_field(entry, "name", "name") + if isinstance(value, str) and value: + key = model_id(value) + if key not in choices or choices[key] is None: + choices[key] = name if isinstance(name, str) else None + if default is not None: + choices.setdefault(default, None) + efforts: list[str] | None = None + for item in options: + option = _dump(item) + if not isinstance(option, dict) or not isinstance(option.get("id"), str): + continue + option_id = option["id"] + if option.get("category") == "thought_level" or option_id in { + "thinking", "effort", "reasoning", "reasoning_effort", "thinkingEffort", + }: + _current_effort, offered = config_option_values(options, option_id) + # A boolean switch describes thinking enablement, not effort levels. + if offered and set(offered) != {"true", "false"}: + efforts = list(dict.fromkeys(offered)) + break + warnings = [] if choices else ["The fresh ACP session did not advertise any models."] + return { + "source": "acp", + "models": [ + { + "id": value, + "name": name, + "is_default": value == default, + "supported_efforts": efforts if value == default else None, + } + for value, name in choices.items() + ], + "default_model": default, + "warnings": warnings, + } + + +async def _cleanup_discovery(live: _Live) -> None: + try: + if live.conn is not None: + with contextlib.suppress(Exception): + await asyncio.wait_for(live.conn.close(), timeout=2) + finally: + try: + if live.proc is not None: + await reap_subprocess(live.proc) + finally: + if live.stderr_task is not None: + live.stderr_task.cancel() + with contextlib.suppress(asyncio.CancelledError): + await live.stderr_task + + class AcpAdapter(Adapter): def __init__(self, agent: AgentConfig, home: Path, env_config=None) -> None: super().__init__(agent, home, env_config) self._live: dict[str, _Live] = {} self._cursor_models_cache: dict[str, str] | None = None + async def list_models(self, cwd: str) -> dict[str, Any]: + """Discover advertised choices in a private, permission-denying process. + + Do not reuse ``_spawn``: it registers a worker and may select a model, + effort, or bypass mode. Discovery sends initialize and session/new only. + The caller owns the discovery deadline; cancellation still reaps here. + """ + if self.agent.name == "dsh": + cmd = resolve_dsh_command(self.agent.command, self.agent.fallback_commands) + if not command_has_native_acp(cmd): + return { + "source": "acp", + "models": [], + "default_model": None, + "warnings": ["Model discovery is unsupported by legacy DSH ACP; use the native ACP profile."], + } + else: + cmd = resolve_command(self.agent.command, self.agent.fallback_commands) + env = self._env() + if self.agent.name == "dsh": + cmd, env = prepare_dsh_launch(cmd, env) + env = self._enforce_spawn_env(env) + kwargs: dict[str, Any] = {} + if sys.platform == "win32": + kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP + live = _Live() + try: + live.proc = await asyncio.create_subprocess_exec( + *cmd, + stdin=asyncio.subprocess.PIPE, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + env=env, + cwd=self.agent.cwd or cwd or None, + limit=STDIO_LIMIT, + **kwargs, + ) + proc = live.proc + if proc.stdin is None or proc.stdout is None: + raise RuntimeError(f"{self.agent.name} did not expose stdio") + client = _DiscoveryClient("model-discovery", self.home, permission_policy="deny") + live.conn = connect_to_agent(client, proc.stdin, proc.stdout) + live.stderr_task = asyncio.create_task(self._drain_stderr(proc, "model-discovery", live)) + await live.conn.initialize( + protocol_version=PROTOCOL_VERSION, + client_info=Implementation(name="agent-bridge", version="0.1.0"), + client_capabilities=ClientCapabilities(), + ) + created = await live.conn.new_session(cwd=cwd, mcp_servers=[]) + return _acp_model_catalog(created, self.agent.name) + except Exception as exc: + raise self._failure( + exc, "model_discovery", proc=live.proc, stderr=live.stderr_tail, + ) from None + finally: + # The outer deadline can fire during close(), after the handshake + # already succeeded. Keep cleanup alive and await the reap before + # propagating that cancellation to the registry. + cleanup = asyncio.create_task(_cleanup_discovery(live)) + try: + await asyncio.shield(cleanup) + except asyncio.CancelledError: + await cleanup + raise + 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": diff --git a/src/agent_bridge/adapters/antigravity.py b/src/agent_bridge/adapters/antigravity.py index 8795b9f..9a98b7a 100644 --- a/src/agent_bridge/adapters/antigravity.py +++ b/src/agent_bridge/adapters/antigravity.py @@ -15,6 +15,10 @@ 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.model_discovery_exec import ( + parse_agy_models_output, + run_bounded_command, +) from agent_bridge.models import Session, Task, TurnResult, agy_effort from agent_bridge.processes import ( drop_pid, @@ -243,6 +247,34 @@ def __init__(self, agent: AgentConfig, home: Path, env_config=None) -> None: async def ensure_session(self, session: Session) -> None: return None + async def list_models(self, cwd: str) -> dict[str, Any]: + """List models via ``agy models``; never dispatches a prompt. + + The resolved configured command plus ``models`` is run read-only in + ``cwd``. Failures propagate so a failed refresh cannot poison the cache. + """ + source = "agy models" + cmd = resolve_command(self.agent.command, self.agent.fallback_commands) + env = self._enforce_spawn_env( + build_worker_env(self.agent.env, config=self.env_config, worker_context=True, home=self.home) + ) + returncode, stdout, stderr, warnings = await run_bounded_command([*cmd, "models"], env=env, cwd=cwd) + if returncode != 0: + detail = redact_diagnostic(stderr.strip()) + warning = f"agy models exited with code {returncode}" + raise RuntimeError(warning + (f": {detail}" if detail else "")) + if warnings: + raise RuntimeError("; ".join(warnings)) + models, default_id, parse_warnings = parse_agy_models_output(stdout) + if not models: + raise RuntimeError("; ".join(parse_warnings) or "agy models returned no recognizable catalog") + return { + "source": source, + "models": models, + "default_model": default_id, + "warnings": [*warnings, *parse_warnings], + } + def _build_cmd(self, session: Session, task: Task) -> list[str]: cmd = resolve_command(self.agent.command, self.agent.fallback_commands) model = task.model or session.model diff --git a/src/agent_bridge/adapters/base.py b/src/agent_bridge/adapters/base.py index e4f23b4..6a1c541 100644 --- a/src/agent_bridge/adapters/base.py +++ b/src/agent_bridge/adapters/base.py @@ -3,6 +3,7 @@ from abc import ABC, abstractmethod from collections.abc import Mapping from pathlib import Path +from typing import Any from agent_bridge.config import AgentConfig, EnvConfig from agent_bridge.models import Session, Task, TurnResult @@ -32,6 +33,10 @@ def __init__(self, agent: AgentConfig, home: Path, env_config: EnvConfig | None def can_revive(self) -> bool: return self.agent.revivable + async def list_models(self, cwd: str) -> dict[str, Any]: + """Discover advertised models without prompting or changing a live session.""" + raise NotImplementedError(f"model discovery is not implemented for {self.agent.name}") + def _enforce_spawn_env(self, env: Mapping[str, str]) -> dict[str, str]: """Last ``env.deny`` gate on the exact env a worker spawn receives. diff --git a/src/agent_bridge/adapters/codex.py b/src/agent_bridge/adapters/codex.py index bca1947..fbf693f 100644 --- a/src/agent_bridge/adapters/codex.py +++ b/src/agent_bridge/adapters/codex.py @@ -20,6 +20,10 @@ yolo_requested, ) from agent_bridge.diagnostics import redact_diagnostic +from agent_bridge.model_discovery_exec import ( + codex_app_server_list_models, + normalize_model_list, +) from agent_bridge.models import Session, Task, TurnResult from agent_bridge.processes import ( drop_pid, @@ -48,6 +52,34 @@ def __init__(self, agent, home: Path, env_config=None) -> None: async def ensure_session(self, session: Session) -> None: return None + async def list_models(self, cwd: str) -> dict[str, Any]: + """List models via ``codex app-server`` JSON-RPC ``model/list``. + + Read-only: no prompt is dispatched and no config is written. The + agents.toml ``config_overrides`` are passed as ``-c`` before the + ``app-server`` subcommand so custom providers/endpoints are reflected + in the listing. Failures propagate to the registry without caching. + """ + source = "codex app-server model/list" + env = self._enforce_spawn_env(self._worker_env()) + command = await asyncio.to_thread( + resolve_codex_command, self.agent.command, self.agent.fallback_commands, env=env + ) + argv = [*command] + for item in self.agent.config_overrides: + argv += ["-c", item] + argv.append("app-server") + entries = await codex_app_server_list_models(argv, env=env, cwd=cwd) + models, default_id, warnings = normalize_model_list(entries) + if entries and not models: + raise RuntimeError("Codex model/list contained no valid model entries") + return { + "source": source, + "models": models, + "default_model": default_id, + "warnings": warnings, + } + def _worker_env(self) -> dict[str, str]: return build_worker_env(self.agent.env, config=self.env_config, worker_context=True, home=self.home) diff --git a/src/agent_bridge/agents.toml b/src/agent_bridge/agents.toml index e6c1616..d22e5ef 100644 --- a/src/agent_bridge/agents.toml +++ b/src/agent_bridge/agents.toml @@ -253,3 +253,8 @@ command = ["codex"] # session_meta = { yolo = true } revivable = true idle_unload_sec = 0 + +# On-demand advertised model catalogs; no background polling. +[models] +cache_sec = 3600 +timeout_sec = 30 diff --git a/src/agent_bridge/config.py b/src/agent_bridge/config.py index 78219b3..9f6a33d 100644 --- a/src/agent_bridge/config.py +++ b/src/agent_bridge/config.py @@ -151,6 +151,13 @@ class ServerConfig(BaseModel): idle_exit_sec: int = 0 +class ModelsConfig(BaseModel): + """On-demand model discovery; expiry never schedules background work.""" + + cache_sec: float = Field(default=3600.0, ge=0) + timeout_sec: float = Field(default=30.0, gt=0) + + class QuotaConfig(BaseModel): """Remaining-quota lookup that rides along with ``list_agents``. @@ -218,6 +225,7 @@ class AppConfig(BaseModel): server: ServerConfig = Field(default_factory=ServerConfig) coordinator: CoordinatorConfig = Field(default_factory=CoordinatorConfig) quota: QuotaConfig = Field(default_factory=QuotaConfig) + models: ModelsConfig = Field(default_factory=ModelsConfig) warnings: list[str] = Field(default_factory=list) def get(self, name: str) -> AgentConfig: @@ -472,7 +480,7 @@ def load_config(home: Path | None = None) -> AppConfig: f"{overlay_path} is not valid TOML: {exc}. " "Fix or delete the file, then restart the Bridge." ) from exc - supported_sections = {"agents", "env", "server", "coordinator", "quota"} + supported_sections = {"agents", "env", "server", "coordinator", "quota", "models"} unsupported = sorted(set(overlay_raw) - supported_sections) warnings = [] if unsupported: @@ -509,11 +517,19 @@ def load_config(home: Path | None = None) -> AppConfig: coord_raw["mode"] = normalize_coordinator_mode(coord_raw.get("mode")) coordinator = CoordinatorConfig.model_validate(coord_raw) quota = QuotaConfig.model_validate({**_coerce_quota(bundled_raw), **_coerce_quota(overlay_raw)}) + models_raw: dict[str, Any] = {} + for raw in (bundled_raw, overlay_raw): + block = raw.get("models", {}) + if not isinstance(block, dict): + raise ValueError("[models] must be a TOML table") + models_raw.update(block) + models = ModelsConfig.model_validate(models_raw) return AppConfig( agents=agents, env=env, server=server, coordinator=coordinator, quota=quota, + models=models, warnings=warnings, ) diff --git a/src/agent_bridge/model_catalog.py b/src/agent_bridge/model_catalog.py new file mode 100644 index 0000000..8e2a2ff --- /dev/null +++ b/src/agent_bridge/model_catalog.py @@ -0,0 +1,42 @@ +"""Normalized worker-advertised model catalogs, not invocation guarantees.""" + +from __future__ import annotations + +from pydantic import BaseModel, Field, model_validator + +from agent_bridge.config import AgentConfig + + +class ModelInfo(BaseModel): + id: str = Field(min_length=1) + name: str | None = None + is_default: bool = False + # Worker-native levels; null means unknown, [] means explicitly no levels. + supported_efforts: list[str] | None = None + + +class ModelCatalog(BaseModel): + source: str = Field(min_length=1) + models: list[ModelInfo] + default_model: str | None = None + warnings: list[str] = Field(default_factory=list) + + @model_validator(mode="after") + def unique_ids(self) -> ModelCatalog: + ids = [model.id for model in self.models] + if len(ids) != len(set(ids)): + raise ValueError("model catalog contains duplicate IDs") + return self + + +def model_discovery_capability(cfg: AgentConfig) -> dict: + """Describe support without launching a process or querying a provider.""" + supported = cfg.protocol in {"agy", "codex"} or ( + cfg.protocol == "acp" and cfg.name in {"kimi", "dsh"} + ) + source = {"agy": "agy models", "codex": "codex app-server model/list", "acp": "acp session/new"} + return { + "supported": supported, + "source": source.get(cfg.protocol) if supported else None, + "requires_session": supported and cfg.protocol == "acp", + } diff --git a/src/agent_bridge/model_discovery_exec.py b/src/agent_bridge/model_discovery_exec.py new file mode 100644 index 0000000..9aedc6f --- /dev/null +++ b/src/agent_bridge/model_discovery_exec.py @@ -0,0 +1,473 @@ +"""Bounded model discovery for one-shot exec workers. + +Antigravity (``agy models``) and Codex (``codex app-server model/list``) can +answer "which models can this worker run" without dispatching a turn. No prompt +is ever sent and Bridge does not change worker configuration; the listing +reflects whatever the resolved, configured worker reports — including custom +endpoints configured through the worker's own config overrides. + +Worker output is untrusted: diagnostics that cross into MCP responses or logs +pass through ``redact_diagnostic`` first, and stdout/stderr are bounded so a +chatty or stuck worker cannot stall or exhaust the coordinator. The registry +owns the outer timeout and cache; the bounds here keep one run self-contained. +""" + +from __future__ import annotations + +import asyncio +import contextlib +import json +import logging +import re +import subprocess +import sys +from collections.abc import Mapping, Sequence +from typing import Any + +from agent_bridge.diagnostics import redact_diagnostic +from agent_bridge.processes import reap_subprocess + +log = logging.getLogger(__name__) + +# The registry owns the configurable deadline and caching. Helpers may also +# receive an explicit timeout when used independently in focused tests. +# asyncio's default 64 KiB stream limit is too small for worker banners; one +# JSON event per line still must fit under this. +DISCOVERY_STDIO_LIMIT = 1024 * 1024 +# Kept output caps: enough for any real model catalog, small enough to ignore +# a worker that streams garbage forever. +DISCOVERY_OUTPUT_BYTE_LIMIT = 4 * 1024 * 1024 +DISCOVERY_LINE_LIMIT = 20000 +DISCOVERY_STDERR_TAIL_LIMIT = 16000 +# model/list pages and a hard page cap so a broken cursor loop cannot run away. +MODEL_LIST_PAGE_LIMIT = 100 +MODEL_LIST_MAX_PAGES = 100 + +CLIENT_INFO = {"name": "agent-bridge", "title": "Agent Bridge", "version": "1"} + +_ANSI_RE = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]") +_AGY_SLUG_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._:+/-]*$") +_AGY_BULLET_RE = re.compile(r"^\s*(?:[-*•]|\d+[.)])\s+") +_AGY_DEFAULT_SUFFIX_RE = re.compile(r"\s*\(default\)\s*$", re.IGNORECASE) +_AGY_BORDER_CHARS = set("-\u2013\u2014=*_#+|") + + +def strip_ansi(text: str) -> str: + return _ANSI_RE.sub("", text) + + +def _efforts_of(raw: Mapping[str, Any]) -> list[str] | None: + """Worker-native offered effort values, verbatim, never Bridge-mapped.""" + for key in ("supported_efforts", "supportedEfforts", "supportedReasoningEfforts"): + if key not in raw: + continue + items = raw[key] + if not isinstance(items, list): + continue + efforts: list[str] = [] + for item in items: + if isinstance(item, str) and item.strip(): + efforts.append(item) + elif isinstance(item, Mapping): + for sub_key in ("reasoningEffort", "effort", "value", "id"): + value = item.get(sub_key) + if isinstance(value, str) and value.strip(): + efforts.append(value) + break + return efforts + return None + + +def normalize_model_entry(raw: Any) -> dict[str, Any] | None: + """One worker-native model entry normalized to the discovery shape. + + The identifier is returned exactly as the worker reported it. ``name`` and + ``supported_efforts`` stay ``None`` when the worker did not offer them; + effort values are the worker's own offered labels, not values Bridge + accepts as input. + """ + if not isinstance(raw, Mapping): + return None + ident = raw.get("id") + if not isinstance(ident, str) or not ident.strip(): + return None + name = raw.get("displayName") or raw.get("name") + if not isinstance(name, str) or not name.strip(): + name = None + return { + "id": ident, + "name": name, + "is_default": raw.get("is_default") is True or raw.get("isDefault") is True, + "supported_efforts": _efforts_of(raw), + } + + +def normalize_model_list(entries: Sequence[Any]) -> tuple[list[dict[str, Any]], str | None, list[str]]: + """Normalize a raw entry list; dedupe by id, warn on malformed rows.""" + warnings: list[str] = [] + models: list[dict[str, Any]] = [] + seen: set[str] = set() + default_id: str | None = None + defaults_seen = 0 + skipped = 0 + for raw in entries: + entry = normalize_model_entry(raw) + if entry is None: + skipped += 1 + continue + if entry["id"] in seen: + continue + seen.add(entry["id"]) + models.append(entry) + if entry["is_default"]: + defaults_seen += 1 + if default_id is None: + default_id = entry["id"] + if defaults_seen > 1: + warnings.append(f"worker reported {defaults_seen} default models; keeping {default_id}") + for model in models: + model["is_default"] = model["id"] == default_id + if skipped: + plural = "entry" if skipped == 1 else "entries" + warnings.append(f"skipped {skipped} malformed model {plural}") + return models, default_id, warnings + + +def _models_from_payload(payload: Any) -> tuple[list[dict[str, Any]], str | None, list[str]]: + warnings: list[str] = [] + raw_default: Any = None + if isinstance(payload, Mapping): + raw_entries = payload.get("models") + if raw_entries is None: + raw_entries = payload.get("data") + raw_default = payload.get("default_model", payload.get("defaultModel")) + elif isinstance(payload, list): + raw_entries = payload + else: + raw_entries = None + if not isinstance(raw_entries, list): + return [], None, ["unrecognized JSON payload from agy models"] + models, default_id, list_warnings = normalize_model_list(raw_entries) + warnings.extend(list_warnings) + if isinstance(raw_default, str) and raw_default.strip() and default_id is None: + if any(model["id"] == raw_default for model in models): + default_id = raw_default + for model in models: + model["is_default"] = model["id"] == default_id + else: + warnings.append("agy default model was absent from its advertised catalog") + return models, default_id, warnings + + +def parse_agy_models_output(stdout: str) -> tuple[list[dict[str, Any]], str | None, list[str]]: + """Parse ``agy models`` output. + + The verified AGY 1.4 format is tab-separated slug and display name. + Also accept structured catalogs or one slug per line, optionally with + bullets, ANSI styling, and a ``(default)`` marker. Other text is ignored + with a warning rather than guessed into a model id. + """ + warnings: list[str] = [] + text = (stdout or "").strip() + if not text: + return [], None, ["agy models returned no output"] + if text[0] in "[{": + try: + payload = json.loads(text) + except json.JSONDecodeError: + payload = None + if payload is not None and isinstance(payload, (Mapping, list)): + models, default_id, json_warnings = _models_from_payload(payload) + if models or not any("unrecognized" in w for w in json_warnings): + return models, default_id, json_warnings + # A JSON-looking payload we cannot map: fall through to the text + # parse so a slug listing that merely starts with [ or { survives. + warnings.extend(json_warnings) + models = [] + seen: set[str] = set() + default_id = None + skipped = 0 + for raw_line in strip_ansi(text).splitlines(): + line = raw_line.strip() + if not line or set(line) <= _AGY_BORDER_CHARS: + continue + line = _AGY_BULLET_RE.sub("", line).strip() + if not line: + continue + name = None + if "\t" in line: + columns = line.split("\t", 1) + line, name = columns[0].strip(), columns[1].strip() or None + is_default = False + suffix = _AGY_DEFAULT_SUFFIX_RE.search(line) + if suffix: + is_default = True + line = line[: suffix.start()].strip() + if name and (suffix := _AGY_DEFAULT_SUFFIX_RE.search(name)): + is_default = True + name = name[:suffix.start()].strip() or None + if not _AGY_SLUG_RE.fullmatch(line): + skipped += 1 + continue + if line not in seen: + seen.add(line) + models.append( + {"id": line, "name": name, "is_default": is_default, "supported_efforts": None} + ) + if is_default and default_id is None: + default_id = line + if skipped: + warnings.append(f"ignored {skipped} line(s) that do not look like model slugs") + if not models: + warnings.append("no model slugs recognized in agy models output") + else: + warnings.append("parsed agy models text output; effort options are unavailable") + return models, default_id, warnings + + +async def _drain_stderr(proc: asyncio.subprocess.Process) -> str: + if proc.stderr is None: + return "" + tail = "" + try: + while True: + line = await proc.stderr.readline() + if not line: + return tail + text = line.decode("utf-8", errors="replace").rstrip() + if text: + tail = f"{tail}\n{text}"[-DISCOVERY_STDERR_TAIL_LIMIT:] + except (ValueError, OSError): + log.warning( + "discovery stderr drain aborted (line over %s MiB or stream closed); later stderr evidence is lost", + DISCOVERY_STDIO_LIMIT // (1024 * 1024), + exc_info=True, + ) + return tail + + +async def _read_bounded_stdout(proc: asyncio.subprocess.Process, warnings: list[str]) -> str: + """Read stdout to EOF, keeping at most the byte/line caps. + + Lines past the caps are still drained (the worker must not block on a full + pipe) but not retained. + """ + assert proc.stdout is not None + parts: list[bytes] = [] + total = 0 + truncated = False + while True: + try: + line = await proc.stdout.readline() + except ValueError as exc: + raise RuntimeError( + f"worker emitted a stdout line longer than {DISCOVERY_STDIO_LIMIT // (1024 * 1024)} MiB" + ) from exc + if not line: + break + if total + len(line) <= DISCOVERY_OUTPUT_BYTE_LIMIT and len(parts) < DISCOVERY_LINE_LIMIT: + parts.append(line) + total += len(line) + elif not truncated: + warnings.append("worker output exceeded discovery bounds; extra output was discarded") + truncated = True + # Keep draining without retaining so the pipe never backs up. + return b"".join(parts).decode("utf-8", errors="replace") + + +async def run_bounded_command( + argv: Sequence[str], + *, + env: Mapping[str, str], + cwd: str, + timeout: float | None = None, + stdin_bytes: bytes | None = None, +) -> tuple[int | None, str, str, list[str]]: + """Run one short-lived read-only worker command with bounded output. + + Returns ``(returncode, stdout_text, stderr_tail, warnings)``. Raises + ``RuntimeError`` on timeout; the process is always reaped, including when + the caller's task is cancelled mid-run. + """ + warnings: list[str] = [] + kwargs: dict[str, Any] = {} + if sys.platform == "win32": + kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP + proc = await asyncio.create_subprocess_exec( + *argv, + stdin=asyncio.subprocess.PIPE if stdin_bytes is not None else asyncio.subprocess.DEVNULL, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + env=dict(env), + cwd=cwd, + limit=DISCOVERY_STDIO_LIMIT, + **kwargs, + ) + stderr_task = asyncio.create_task(_drain_stderr(proc)) + + async def _exchange() -> str: + if proc.stdin is not None: + try: + if stdin_bytes is not None: + proc.stdin.write(stdin_bytes) + await proc.stdin.drain() + except (BrokenPipeError, ConnectionResetError, OSError): + pass + with contextlib.suppress(BrokenPipeError, ConnectionResetError, OSError): + proc.stdin.close() + stdout_text = await _read_bounded_stdout(proc, warnings) + await proc.wait() + return stdout_text + + try: + stdout_text = await asyncio.wait_for(_exchange(), timeout=timeout) + if stderr_task.done(): + with contextlib.suppress(asyncio.CancelledError): + stderr_tail = stderr_task.result() + else: + stderr_tail = await stderr_task + except TimeoutError: + await reap_subprocess(proc) + raise RuntimeError(f"worker command did not finish within {timeout}s") from None + finally: + if not stderr_task.done(): + stderr_task.cancel() + with contextlib.suppress(asyncio.CancelledError): + await stderr_task + # A registry deadline can land after a successful response while this + # block is already reaping; finish the kill escalation regardless. + cleanup = asyncio.create_task(reap_subprocess(proc, timeout=2.0)) + try: + await asyncio.shield(cleanup) + except asyncio.CancelledError: + await cleanup + raise + return proc.returncode, stdout_text, stderr_tail, warnings + + +async def rpc_send(proc: asyncio.subprocess.Process, payload: Mapping[str, Any]) -> None: + assert proc.stdin is not None + proc.stdin.write((json.dumps(payload) + "\n").encode("utf-8")) + await proc.stdin.drain() + + +def _rpc_error_text(error: Any) -> str: + if isinstance(error, Mapping): + message = error.get("message") + if isinstance(message, str) and message: + return message + return json.dumps(error, ensure_ascii=False)[:200] + return str(error)[:200] + + +async def rpc_read_response(proc: asyncio.subprocess.Process, wanted_id: int) -> Mapping[str, Any]: + """Read JSON-RPC lines until the response for ``wanted_id`` arrives. + + Notifications and unparseable lines are skipped; an error envelope raises + with a redacted message. + """ + assert proc.stdout is not None + while True: + try: + line = await proc.stdout.readline() + except ValueError as exc: + raise RuntimeError( + f"worker emitted a stdout line longer than {DISCOVERY_STDIO_LIMIT // (1024 * 1024)} MiB" + ) from exc + if not line: + raise RuntimeError("worker closed stdout before answering the request") + text = line.decode("utf-8", errors="replace").strip() + if not text: + continue + try: + message = json.loads(text) + except json.JSONDecodeError: + continue + if not isinstance(message, Mapping) or message.get("id") != wanted_id: + continue + if "error" in message: + raise RuntimeError(f"worker request failed: {redact_diagnostic(_rpc_error_text(message['error']))}") + result = message.get("result") + if not isinstance(result, Mapping): + raise RuntimeError("worker returned a malformed result payload") + return result + + +async def codex_app_server_list_models( + argv: Sequence[str], + *, + env: Mapping[str, str], + cwd: str, + timeout: float | None = None, +) -> list[Any]: + """``initialize`` → ``initialized`` → paginated ``model/list`` over stdio. + + ``argv`` must already include any ``-c`` config overrides and end with + ``app-server``. Hidden models are excluded from the selectable catalog. + Raises ``RuntimeError`` (process reaped) on timeout, RPC errors, or a + malformed payload; propagates ``CancelledError`` after killing the worker. + """ + kwargs: dict[str, Any] = {} + if sys.platform == "win32": + kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP + proc = await asyncio.create_subprocess_exec( + *argv, + stdin=asyncio.subprocess.PIPE, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.DEVNULL, + env=dict(env), + cwd=cwd, + limit=DISCOVERY_STDIO_LIMIT, + **kwargs, + ) + entries: list[Any] = [] + + async def _exchange() -> None: + await rpc_send( + proc, + {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"clientInfo": CLIENT_INFO}}, + ) + await rpc_read_response(proc, 1) + await rpc_send(proc, {"jsonrpc": "2.0", "method": "initialized", "params": {}}) + cursor: str | None = None + seen_cursors: set[str] = set() + for page in range(MODEL_LIST_MAX_PAGES): + params: dict[str, Any] = {"limit": MODEL_LIST_PAGE_LIMIT, "includeHidden": False} + if cursor: + params["cursor"] = cursor + request_id = page + 2 + await rpc_send(proc, {"jsonrpc": "2.0", "id": request_id, "method": "model/list", "params": params}) + result = await rpc_read_response(proc, request_id) + data = result.get("data") + if not isinstance(data, list): + raise RuntimeError("Codex model/list returned missing or malformed data") + entries.extend(data) + next_cursor = result.get("nextCursor") + if next_cursor is not None and not isinstance(next_cursor, str): + raise RuntimeError("Codex model/list returned a malformed nextCursor") + cursor = next_cursor if isinstance(next_cursor, str) and next_cursor else None + if cursor is None: + break + if cursor in seen_cursors: + raise RuntimeError("model/list did not finish paginating: repeated cursor") + seen_cursors.add(cursor) + else: + raise RuntimeError(f"model/list did not finish paginating within {MODEL_LIST_MAX_PAGES} pages") + with contextlib.suppress(BrokenPipeError, ConnectionResetError, OSError): + assert proc.stdin is not None + proc.stdin.close() + await proc.wait() + + try: + await asyncio.wait_for(_exchange(), timeout=timeout) + except TimeoutError: + await reap_subprocess(proc) + raise RuntimeError(f"model/list did not finish within {timeout}s") from None + finally: + cleanup = asyncio.create_task(reap_subprocess(proc, timeout=2.0)) + try: + await asyncio.shield(cleanup) + except asyncio.CancelledError: + await cleanup + raise + return entries diff --git a/src/agent_bridge/registry.py b/src/agent_bridge/registry.py index 3a3ed69..a3cc303 100644 --- a/src/agent_bridge/registry.py +++ b/src/agent_bridge/registry.py @@ -2,6 +2,7 @@ import asyncio import contextlib +import copy import hashlib import json import logging @@ -28,6 +29,7 @@ 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.model_catalog import ModelCatalog, model_discovery_capability from agent_bridge.models import ( DEFAULT_WAIT_SEC, TERMINAL_STATUSES, @@ -115,6 +117,7 @@ ORPHAN_RESULT_MIN_AGE_SEC = 3600 # Persisted request_id bindings kept; bindings also drop with their task. REQUEST_KEEP_MAX = 1000 +MODEL_CACHE_MAX = 128 def _new_id(prefix: str) -> str: @@ -166,6 +169,8 @@ def __init__( # of request tuples so bindings can persist to state.json compactly. self._requests: dict[str, tuple[str, str]] = {} self._adapters: dict[str, Adapter] = {} + self._model_cache: dict[tuple[str, str], tuple[float, dict]] = {} + self._model_locks: dict[str, asyncio.Lock] = {} self._done: dict[str, asyncio.Event] = {} self._idle: dict[str, asyncio.Task[None]] = {} self._bg: dict[str, asyncio.Task[None]] = {} @@ -593,10 +598,53 @@ async def list_agents(self) -> list[dict]: async def describe(cfg: AgentConfig) -> dict: row = await probe_agent(cfg, self.config.env) row["quota"] = await self._quota_for(cfg, available=bool(row.get("available"))) + row["model_discovery"] = model_discovery_capability(cfg) return row return list(await asyncio.gather(*(describe(cfg) for cfg in self.config.agents.values()))) + async def list_models(self, agent: str, cwd: str, *, refresh: bool = False) -> dict: + """Query a private discovery process; never reuse or persist a work session.""" + if not self.dispatch_enabled: + raise RuntimeError("model discovery is disabled inside worker-inherited Bridge instances") + cfg = self.config.get(agent) + if not model_discovery_capability(cfg)["supported"]: + raise NotImplementedError(f"model discovery is not implemented for {agent}") + path = Path(cwd) + if not path.is_absolute() or not path.is_dir(): + raise ValueError("cwd must be an existing absolute project directory") + resolved = str(path.resolve()) + key = (agent, resolved) + lock = self._model_locks.setdefault(agent, asyncio.Lock()) + try: + async with asyncio.timeout(self.config.models.timeout_sec), lock: + previous = self._model_cache.get(key) + if not refresh and previous and time.monotonic() - previous[0] < self.config.models.cache_sec: + return {**copy.deepcopy(previous[1]), "cached": True} + adapter = build_adapter(cfg, self.home, self.config.env) + raw = await adapter.list_models(resolved) + catalog = ModelCatalog.model_validate(raw) + result = { + **catalog.model_dump(mode="json"), + "agent": agent, + "cwd": resolved, + "fetched_at": iso(), + "cached": False, + "availability": "advertised", + "invocation_verified": False, + "effort_vocabulary": "worker_native", + } + result["warnings"] = [redact_diagnostic(w) for w in result["warnings"]] + if key not in self._model_cache and len(self._model_cache) >= MODEL_CACHE_MAX: + oldest = min(self._model_cache, key=lambda k: self._model_cache[k][0]) + self._model_cache.pop(oldest) + self._model_cache[key] = (time.monotonic(), copy.deepcopy(result)) + return result + except Exception as exc: + # Failed refreshes are explicit; never mask errors with stale data or + # expose credentials in worker diagnostics. Cleanup runs before exit. + raise RuntimeError(redact_diagnostic(str(exc)) or type(exc).__name__) from None + async def _quota_for(self, cfg: AgentConfig, *, available: bool) -> dict: """The ``quota`` block for one ``list_agents`` row. Never raises, never gates ``available``.""" quota_cfg = self.config.quota diff --git a/src/agent_bridge/server.py b/src/agent_bridge/server.py index 754c6fc..ce2f857 100644 --- a/src/agent_bridge/server.py +++ b/src/agent_bridge/server.py @@ -64,6 +64,11 @@ async def lifespan(_server: MCPServer[Registry]) -> AsyncIterator[Registry]: "or end_session. When " "dispatch_enabled is true and the user states a lasting preference, " "persist it with set_preferences." + " Use list_models(agent, cwd) to discover model IDs before choosing one; " + "list_agents.model_discovery describes support. Catalogs are worker-advertised, " + "not verified successful invocations. supported_efforts uses worker-native " + "vocabulary; dispatch_task still accepts only off/low/medium/high/max. " + "Model discovery is disabled in worker-inherited instances." ) mcp = MCPServer[Registry]("agent-bridge", instructions=INSTRUCTIONS, lifespan=lifespan) @@ -97,6 +102,15 @@ async def list_agents(ctx: Context) -> dict[str, Any]: return _error(exc) +@mcp.tool(annotations=READ_ONLY) +async def list_models(ctx: Context, agent: str, cwd: str, refresh: bool = False) -> dict[str, Any]: + """Discover the selected worker's advertised models. cwd must be this conversation's absolute project directory. Supports Kimi/native DSH through a short-lived ACP session, Antigravity through `agy models`, and Codex through app-server model/list. Does not prompt a model or modify active sessions. Results are cached per worker/project for one hour by default ([models] cache_sec); refresh bypasses the cache. supported_efforts contains worker-native values only for models whose levels are known; null means unknown. Catalog presence does not prove account quota or successful invocation. Discovery may initialize a worker-owned session and contact its service. Disabled inside worker-inherited Bridge instances.""" + try: + return {"ok": True, **await _registry(ctx).list_models(agent, cwd, refresh=refresh)} + except Exception as exc: + return _error(exc) + + @mcp.tool() async def set_preferences( ctx: Context, diff --git a/tests/test_model_catalog.py b/tests/test_model_catalog.py new file mode 100644 index 0000000..1af50dc --- /dev/null +++ b/tests/test_model_catalog.py @@ -0,0 +1,171 @@ +from __future__ import annotations + +import asyncio +import types + +import pytest + +from agent_bridge.config import AgentConfig, AppConfig, EnvConfig, ModelsConfig, load_config +from agent_bridge.model_catalog import model_discovery_capability +from agent_bridge.registry import Registry +from agent_bridge.server import list_models + + +@pytest.fixture +def catalog_registry(tmp_path, monkeypatch): + config = AppConfig( + agents={"kimi": AgentConfig(name="kimi", protocol="acp", command=["not-launched"])}, + env=EnvConfig(discover_proxy=False), + ) + registry = Registry(tmp_path / "bridge", config, runtime_context="coordinator") + calls = [] + + class Discovery: + async def list_models(self, cwd): + calls.append(cwd) + await asyncio.sleep(0) + return { + "source": "fake catalog", + "models": [{"id": "small", "is_default": True, "supported_efforts": ["low", "high"]}], + "default_model": "small", + } + + monkeypatch.setattr("agent_bridge.registry.build_adapter", lambda *args: Discovery()) + return registry, calls + + +async def test_cache_refresh_and_defensive_copy(catalog_registry, tmp_path): + registry, calls = catalog_registry + first = await registry.list_models("kimi", str(tmp_path)) + assert first["cached"] is False + first["models"][0]["id"] = "changed by caller" + cached = await registry.list_models("kimi", str(tmp_path)) + assert cached["cached"] is True + assert cached["models"][0]["id"] == "small" + assert cached["availability"] == "advertised" + assert cached["invocation_verified"] is False + assert cached["effort_vocabulary"] == "worker_native" + assert len(calls) == 1 + assert not (await registry.list_models("kimi", str(tmp_path), refresh=True))["cached"] + assert len(calls) == 2 + assert registry.sessions == registry.tasks == registry._adapters == {} + + +async def test_concurrent_queries_share_discovery(catalog_registry, tmp_path): + registry, calls = catalog_registry + results = await asyncio.gather(*(registry.list_models("kimi", str(tmp_path)) for _ in range(5))) + assert len(calls) == 1 + assert sum(not r["cached"] for r in results) == 1 + + +async def test_cache_scope_and_expiry(catalog_registry, tmp_path, monkeypatch): + registry, calls = catalog_registry + clock = [1000.0] + monkeypatch.setattr("agent_bridge.registry.time.monotonic", lambda: clock[0]) + await registry.list_models("kimi", str(tmp_path)) + clock[0] += 3599 + assert (await registry.list_models("kimi", str(tmp_path)))["cached"] + clock[0] += 2 + assert not (await registry.list_models("kimi", str(tmp_path)))["cached"] + other = tmp_path / "other" + other.mkdir() + await registry.list_models("kimi", str(other)) + assert len(calls) == 3 + + +async def test_failed_refresh_is_explicit_and_preserves_cache(catalog_registry, tmp_path, monkeypatch): + registry, _ = catalog_registry + await registry.list_models("kimi", str(tmp_path)) + + class Broken: + async def list_models(self, cwd): + raise RuntimeError("Authorization: Bearer catalog-secret") + + monkeypatch.setattr("agent_bridge.registry.build_adapter", lambda *args: Broken()) + with pytest.raises(RuntimeError) as caught: + await registry.list_models("kimi", str(tmp_path), refresh=True) + assert "catalog-secret" not in str(caught.value) + assert (await registry.list_models("kimi", str(tmp_path)))["cached"] + + +async def test_discovery_timeout_cancels_adapter(catalog_registry, tmp_path, monkeypatch): + registry, _ = catalog_registry + registry.config.models.timeout_sec = 0.01 + cleaned = asyncio.Event() + + class Slow: + async def list_models(self, cwd): + try: + await asyncio.Event().wait() + finally: + cleaned.set() + + monkeypatch.setattr("agent_bridge.registry.build_adapter", lambda *args: Slow()) + with pytest.raises(RuntimeError, match="TimeoutError"): + await registry.list_models("kimi", str(tmp_path)) + assert cleaned.is_set() + assert not registry._model_cache + + +async def test_deadline_includes_waiting_for_worker_lock(catalog_registry, tmp_path): + registry, calls = catalog_registry + registry.config.models.timeout_sec = 0.01 + lock = registry._model_locks.setdefault("kimi", asyncio.Lock()) + async with lock: + with pytest.raises(RuntimeError, match="TimeoutError"): + await registry.list_models("kimi", str(tmp_path), refresh=True) + assert calls == [] + + +async def test_list_agents_reports_capability_without_discovery(catalog_registry, monkeypatch): + registry, calls = catalog_registry + registry.config.quota.enabled = False + + async def probe(cfg, env): + return {"agent": cfg.name, "available": True} + + monkeypatch.setattr("agent_bridge.registry.probe_agent", probe) + rows = await registry.list_agents() + assert rows[0]["model_discovery"]["supported"] + assert calls == [] + + +async def test_rejects_bad_context_and_unsupported_worker(catalog_registry, tmp_path): + registry, calls = catalog_registry + with pytest.raises(ValueError, match="absolute"): + await registry.list_models("kimi", "relative") + registry.config.agents["claude"] = AgentConfig(name="claude", protocol="acp", command=["unused"]) + with pytest.raises(NotImplementedError): + await registry.list_models("claude", str(tmp_path)) + registry.dispatch_enabled = False + with pytest.raises(RuntimeError, match="worker-inherited"): + await registry.list_models("kimi", str(tmp_path)) + assert calls == [] + + +async def test_mcp_tool_wraps_success_and_error(catalog_registry, tmp_path): + registry, _ = catalog_registry + ctx = types.SimpleNamespace(request_context=types.SimpleNamespace(lifespan_context=registry)) + result = await list_models(ctx, "kimi", str(tmp_path)) + assert result["ok"] and result["default_model"] == "small" + assert not (await list_models(ctx, "unknown", str(tmp_path)))["ok"] + + +def test_model_discovery_config_overlay(tmp_path): + (tmp_path / "agents.toml").write_text("[models]\ncache_sec = 86400\ntimeout_sec = 12\n", encoding="utf-8") + config = load_config(tmp_path) + assert config.models.cache_sec == 86400 + assert config.models.timeout_sec == 12 + assert not config.warnings + assert ModelsConfig().cache_sec == 3600 + with pytest.raises(ValueError): + ModelsConfig(cache_sec=-1) + with pytest.raises(ValueError): + ModelsConfig(timeout_sec=0) + + +def test_capability_does_not_claim_unimplemented_agents(): + for name, protocol in [("kimi", "acp"), ("dsh", "acp"), ("codex", "codex"), ("antigravity", "agy")]: + cfg = AgentConfig(name=name, protocol=protocol, command=["unused"]) + assert model_discovery_capability(cfg)["supported"] + assert not model_discovery_capability(AgentConfig(name="grok", protocol="acp", command=["unused"]))["supported"] diff --git a/tests/test_model_discovery_acp.py b/tests/test_model_discovery_acp.py new file mode 100644 index 0000000..2884572 --- /dev/null +++ b/tests/test_model_discovery_acp.py @@ -0,0 +1,308 @@ +from __future__ import annotations + +import asyncio +from types import SimpleNamespace + +import pytest +from acp.schema import PermissionOption + +from agent_bridge.adapters import acp +from agent_bridge.adapters.acp import AcpAdapter, AcpError, _acp_model_catalog, _Live +from agent_bridge.config import AgentConfig + + +class FakeConn: + def __init__(self, response=None, fail_at=None, block_at=None): + self.response = response or {} + self.fail_at = fail_at + self.block_at = block_at + self.calls = [] + self.started = asyncio.Event() + self.client = None + + async def _call(self, name, kwargs): + self.calls.append((name, kwargs)) + if name == self.fail_at: + raise RuntimeError(f"{name} failed") + if name == self.block_at: + self.started.set() + await asyncio.Event().wait() + + async def initialize(self, **kwargs): + await self._call("initialize", kwargs) + + async def new_session(self, **kwargs): + await self._call("new_session", kwargs) + return self.response + + async def close(self): + await self._call("close", {}) + + +@pytest.fixture +def discovery(monkeypatch, tmp_path): + """Fail on registry, persistence, or normal-session paths, even on errors.""" + def forbidden(*args, **kwargs): + pytest.fail("Discovery must not persist or configure a Bridge session") + + for name in ("append_event", "record_pid", "drop_pid"): + monkeypatch.setattr(acp, name, forbidden) + monkeypatch.setattr(acp, "resolve_command", lambda command, fallback: list(command)) + monkeypatch.setattr(acp, "resolve_dsh_command", lambda command, fallback: list(command)) + conn = FakeConn() + proc = SimpleNamespace(stdin=object(), stdout=object(), stderr=None, returncode=None) + spawns = [] + reaped = [] + + async def spawn(*args, **kwargs): + spawns.append((args, kwargs)) + return proc + + async def reap(value): + reaped.append(value) + + def connect(client, stdin, stdout): + assert (stdin, stdout) == (proc.stdin, proc.stdout) + conn.client = client + return conn + + monkeypatch.setattr(acp.asyncio, "create_subprocess_exec", spawn) + monkeypatch.setattr(acp, "connect_to_agent", connect) + monkeypatch.setattr(acp, "reap_subprocess", reap) + + def build(name="kimi", command=None, **kwargs): + adapter = AcpAdapter( + AgentConfig( + name=name, protocol="acp", command=command or [name, "acp"], + session_meta={"yoloMode": True, "modelId": "must-not-select"}, + permission_policy="allow_always", **kwargs, + ), + tmp_path / "home", + ) + monkeypatch.setattr(adapter, "_env", lambda: {"SAFE": "yes"}) + for method in ("_spawn", "ensure_session", "_sync_selection", "shutdown"): + monkeypatch.setattr(adapter, method, forbidden) + active = _Live() + adapter._live["existing-session"] = active + return adapter, active + + return SimpleNamespace( + conn=conn, proc=proc, spawns=spawns, reaped=reaped, build=build, home=tmp_path / "home", + ) + + +def test_grouped_options_and_standard_names_are_combined(): + result = _acp_model_catalog( + { + "configOptions": [ + { + "id": "model", "currentValue": "model-b", + "options": [{"group": "provider", "options": [ + {"value": "model-a", "name": "Model A"}, + {"value": "model-b"}, + ]}], + }, + { + "id": "thinking", "currentValue": "high", + "options": [{"group": "levels", "options": [ + {"value": "low"}, {"value": "high"}, {"value": "high"}, + ]}], + }, + ], + "models": { + "currentModelId": "model-b", + "availableModels": [{"modelId": "model-b", "name": "Model B"}], + }, + }, + "kimi", + ) + assert result == { + "source": "acp", + "models": [ + {"id": "model-a", "name": "Model A", "is_default": False, "supported_efforts": None}, + {"id": "model-b", "name": "Model B", "is_default": True, "supported_efforts": ["low", "high"]}, + ], + "default_model": "model-b", "warnings": [], + } + + +def test_standard_models_metadata_and_sdk_fields(): + class Model: + def model_dump(self, **kwargs): + return {"id": "custom-picker", "category": "model", "currentValue": "a", "options": [ + {"value": "a", "name": "A"}, + ]} + + result = _acp_model_catalog(SimpleNamespace( + config_options=[Model()], models=SimpleNamespace( + current_model_id="a", available_models=[SimpleNamespace(model_id="b", name="B")], + ), + ), "kimi") + assert result["default_model"] == "a" + assert [(m["id"], m["name"], m["supported_efforts"]) for m in result["models"]] == [ + ("a", "A", None), ("b", "B", None), + ] + + +def test_standard_metadata_without_config_options(): + result = _acp_model_catalog({"models": { + "currentModelId": "a", "availableModels": [{"modelId": "a", "name": "Alpha"}], + }}, "other-acp-worker") + assert result["default_model"] == "a" + assert result["models"] == [ + {"id": "a", "name": "Alpha", "is_default": True, "supported_efforts": None}, + ] + + +@pytest.mark.parametrize("payload", [{}, {"configOptions": "malformed"}, {"models": {"availableModels": "bad"}}]) +def test_unadvertised_models_are_not_invented(payload): + result = _acp_model_catalog(payload, "kimi") + assert result["models"] == [] + assert result["default_model"] is None + assert result["warnings"] + + +def test_boolean_thinking_is_not_reported_as_effort(): + result = _acp_model_catalog({"configOptions": [ + {"id": "model", "currentValue": "a", "options": [{"value": "a"}]}, + {"id": "thinking", "options": [{"value": "true"}, {"value": "false"}]}, + ]}, "kimi") + assert result["models"][0]["supported_efforts"] is None + + +async def test_private_handshake_never_prompts_configures_or_persists(discovery): + adapter, active = discovery.build(cwd="process-directory") + discovery.conn.response = {"models": {"currentModelId": "a", "availableModels": [{"modelId": "a"}]}} + result = await adapter.list_models("workspace-directory") + assert result["models"][0]["id"] == "a" + assert [name for name, _ in discovery.conn.calls] == ["initialize", "new_session", "close"] + assert discovery.conn.calls[1][1] == {"cwd": "workspace-directory", "mcp_servers": []} + assert discovery.spawns[0][1]["cwd"] == "process-directory" + assert discovery.spawns[0][1]["env"] == {"SAFE": "yes"} + assert discovery.reaped == [discovery.proc] + assert adapter._live == {"existing-session": active} + assert not discovery.home.exists() + + +async def test_discovery_denies_permission_even_with_allow_always_policy(discovery): + adapter, _active = discovery.build() + await adapter.list_models(".") + response = await discovery.conn.client.request_permission( + "private", {"kind": "edit"}, + [PermissionOption(option_id="allow", name="Allow", kind="allow_always")], + ) + assert response.outcome.outcome == "cancelled" + await discovery.conn.client.session_update("private", {"toolCallId": "ignored"}) + with pytest.raises(RuntimeError, match="disabled"): + await discovery.conn.client.write_text_file("private", "file", "content") + with pytest.raises(RuntimeError, match="disabled"): + await discovery.conn.client.create_terminal("private", "command") + assert not discovery.home.exists() + + +@pytest.mark.parametrize("fail_at", ["initialize", "new_session"]) +async def test_handshake_failure_reaps_private_process(discovery, fail_at): + adapter, active = discovery.build() + discovery.conn.fail_at = fail_at + with pytest.raises(AcpError, match=f"{fail_at} failed") as caught: + await adapter.list_models(".") + assert caught.value.failure.operation == "model_discovery" + assert caught.value.failure.prompt_may_have_been_sent is False + assert discovery.conn.calls[-1][0] == "close" + assert discovery.reaped == [discovery.proc] + assert adapter._live == {"existing-session": active} + + +@pytest.mark.parametrize("block_at", ["initialize", "new_session"]) +@pytest.mark.parametrize("cancel_method", ["cancel", "timeout"]) +async def test_cancel_or_outer_timeout_reaps_private_process(discovery, block_at, cancel_method): + adapter, active = discovery.build() + discovery.conn.block_at = block_at + task = asyncio.create_task(adapter.list_models(".")) + await discovery.conn.started.wait() + if cancel_method == "cancel": + task.cancel() + with pytest.raises(asyncio.CancelledError): + await task + else: + with pytest.raises(TimeoutError): + await asyncio.wait_for(task, timeout=0.01) + assert discovery.conn.calls[-1][0] == "close" + assert discovery.reaped == [discovery.proc] + assert adapter._live == {"existing-session": active} + + +async def test_connect_failure_still_reaps_process(discovery, monkeypatch): + adapter, _active = discovery.build() + + def fail(*args): + raise RuntimeError("connect failed") + + monkeypatch.setattr(acp, "connect_to_agent", fail) + with pytest.raises(AcpError, match="connect failed"): + await adapter.list_models(".") + assert discovery.reaped == [discovery.proc] + + +async def test_close_failure_still_reaps_process(discovery): + adapter, _active = discovery.build() + discovery.conn.fail_at = "close" + await adapter.list_models(".") + assert discovery.reaped == [discovery.proc] + + +async def test_cancellation_during_cleanup_still_waits_for_reaping(discovery, monkeypatch): + adapter, active = discovery.build() + closing = asyncio.Event() + finish_close = asyncio.Event() + + async def close(): + closing.set() + await finish_close.wait() + + monkeypatch.setattr(discovery.conn, "close", close) + task = asyncio.create_task(adapter.list_models(".")) + await closing.wait() + task.cancel() + await asyncio.sleep(0) + assert not task.done() + finish_close.set() + with pytest.raises(asyncio.CancelledError): + await task + assert discovery.reaped == [discovery.proc] + assert adapter._live == {"existing-session": active} + + +async def test_native_dsh_uses_advertised_provider_model_ids(discovery, monkeypatch): + prepared = [] + + def prepare(command, env, **kwargs): + prepared.append(kwargs) + return command, env + + monkeypatch.setattr(acp, "prepare_dsh_launch", prepare) + adapter, _active = discovery.build("dsh", ["dsh", "--profile", "acp"]) + discovery.conn.response = {"configOptions": [ + {"id": "model", "currentValue": '["openai","reasoner"]', "options": [ + {"group": "openai", "options": [ + {"value": '["openai","reasoner"]', "name": "Reasoner"}, + {"value": '["openai","fast"]', "name": "Fast"}, + ]}, + ]}, + {"id": "reasoning_effort", "options": [{"value": "medium"}, {"value": "high"}]}, + ]} + result = await adapter.list_models(".") + assert prepared == [{}] + assert result["default_model"] == "openai/reasoner" + assert [(m["id"], m["supported_efforts"]) for m in result["models"]] == [ + ("openai/reasoner", ["medium", "high"]), ("openai/fast", None), + ] + + +async def test_legacy_dsh_returns_warning_without_starting_process(discovery): + adapter, _active = discovery.build("dsh", ["dsh-acp-demo"]) + result = await adapter.list_models(".") + assert result["models"] == [] + assert "legacy" in result["warnings"][0] + assert discovery.spawns == [] + assert discovery.reaped == [] diff --git a/tests/test_model_discovery_exec.py b/tests/test_model_discovery_exec.py new file mode 100644 index 0000000..aa64b11 --- /dev/null +++ b/tests/test_model_discovery_exec.py @@ -0,0 +1,510 @@ +from __future__ import annotations + +import asyncio +import json +import os +import sys +import time +from pathlib import Path + +import pytest + +from agent_bridge import model_discovery_exec +from agent_bridge.adapters.antigravity import AgyAdapter +from agent_bridge.adapters.codex import CodexAdapter +from agent_bridge.config import AgentConfig, EnvConfig +from agent_bridge.model_discovery_exec import ( + codex_app_server_list_models, + normalize_model_entry, + normalize_model_list, + parse_agy_models_output, + run_bounded_command, +) + +FAKE_AGY = r''' +import json +import os +import sys +import time + +mode = os.environ.get("FAKE_AGY_MODE", "text") + +if mode == "exit3": + print("Authorization: Bearer sk-testsecret123", file=sys.stderr) + print("boom: token=apikeyhunter2", file=sys.stderr) + sys.exit(3) + +if mode == "hang": + time.sleep(30) + sys.exit(0) + +if mode == "json": + print(json.dumps({ + "models": [ + {"id": "gemini-3-pro", "name": "Gemini 3 Pro", "isDefault": True, + "supportedEfforts": ["low", "high"]}, + {"id": "claude-x", "name": "Claude X", "isDefault": False, + "supportedEfforts": []}, + {"name": "no id here"}, + ], + "defaultModel": "gemini-3-pro", + })) + sys.exit(0) + +print("\x1b[32m gemini-3.8-flash-medium \x1b[0m") +print("claude-sonnet-4-5 (default)") +print("2) gemini-3-pro") +print("not a model line with spaces") +print("---") +report = os.environ.get("FAKE_AGY_REPORT") +if report: + with open(report, "w", encoding="utf-8") as fh: + json.dump({ + "cwd": os.getcwd(), + "denied_present": "DISCOVERY_SECRET" in os.environ, + "agent_env": os.environ.get("AGY_MARKER"), + "worker_context": os.environ.get("AGENT_BRIDGE_PARENT_CONTEXT"), + }, fh) +sys.exit(0) +''' + +FAKE_CODEX = r''' +import json +import os +import sys +import time + +argv = sys.argv[1:] + +if argv and argv[-1] != "app-server": + print("unknown command", file=sys.stderr) + sys.exit(2) + +report = os.environ.get("FAKE_CODEX_REPORT") +requests = [] + +def emit(payload): + text = json.dumps(payload) + print(text, flush=True) + +mode = os.environ.get("FAKE_CODEX_MODELS_MODE", "ok") + +if mode == "hang": + time.sleep(30) + sys.exit(0) + +for line in sys.stdin: + line = line.strip() + if not line: + continue + msg = json.loads(line) + requests.append(msg) + method = msg.get("method") + if method == "initialize": + emit({"id": msg["id"], "result": {"userAgent": "fake-codex"}}) + continue + if method == "initialized": + continue + if method == "model/list": + params = msg.get("params") or {} + if mode == "rpc_error": + emit({"id": msg["id"], "error": {"code": -32000, + "message": "Authorization: Bearer sk-leakedtok987"}}) + continue + if mode == "bad_page": + result = {"data": "not-a-list"} + elif mode == "bad_cursor": + result = {"data": [], "nextCursor": 17} + elif mode == "malformed": + result = {"data": [ + {"id": "gpt-good", "displayName": "Good", "isDefault": True}, + "not-a-mapping", + {"displayName": "missing id"}, + ], "nextCursor": None} + elif mode == "cursor_loop": + result = {"data": [{"id": "gpt-a"}], "nextCursor": "again"} + elif params.get("cursor") == "page2": + result = {"data": [{"id": "gpt-b", "displayName": "B", "isDefault": False, + "supportedReasoningEfforts": [{"reasoningEffort": "high"}]}], + "nextCursor": None} + else: + result = {"data": [{"id": "gpt-a", "model": "gpt-a", "displayName": "A", + "isDefault": True, "hidden": False, + "supportedReasoningEfforts": [ + {"reasoningEffort": "medium"}, + {"reasoningEffort": "xhigh"}, + ]}], + "nextCursor": "page2"} + emit({"id": msg["id"], "result": result}) + continue + emit({"id": msg["id"], "error": {"code": -32601, "message": "unknown method"}}) + +if report: + with open(report, "w", encoding="utf-8") as fh: + json.dump({"argv": argv, "cwd": os.getcwd(), "requests": requests}, fh) +sys.exit(0) +''' + + +def _write_script(tmp_path: Path, name: str, source: str) -> Path: + script = tmp_path / name + script.write_text(source, encoding="utf-8") + return script + + +def _agy_adapter(home: Path, command: list[str], *, agent_env=None, env_config=None) -> AgyAdapter: + agent = AgentConfig(name="agy", protocol="antigravity", command=command, env=agent_env or {}) + return AgyAdapter(agent, home, env_config) + + +def _codex_adapter(home: Path, command: list[str], *, overrides=None) -> CodexAdapter: + agent = AgentConfig( + name="codex", + protocol="codex", + command=command, + config_overrides=overrides or [], + ) + return CodexAdapter(agent, home) + + +# --- pure helpers ------------------------------------------------------------- + + +def test_normalize_model_entry_verbatim_id_and_raw_efforts(): + entry = normalize_model_entry( + { + "id": "gpt-6.1-sol", + "displayName": "GPT-6.1 Sol", + "isDefault": True, + "supportedReasoningEfforts": [ + {"reasoningEffort": "medium", "description": "balances"}, + {"reasoningEffort": "xhigh"}, + "raw-string-effort", + {"no_effort_key": True}, + ], + } + ) + assert entry == { + "id": "gpt-6.1-sol", + "name": "GPT-6.1 Sol", + "is_default": True, + "supported_efforts": ["medium", "xhigh", "raw-string-effort"], + } + # Underscore spellings and missing optionals. + entry = normalize_model_entry({"id": "m", "name": " ", "is_default": False}) + assert entry == {"id": "m", "name": None, "is_default": False, "supported_efforts": None} + # id must be a non-empty string; nothing is invented. + assert normalize_model_entry({"displayName": "no id"}) is None + assert normalize_model_entry("not-a-mapping") is None + assert normalize_model_entry({"id": " "}) is None + + +def test_normalize_model_list_dedupes_and_warns(): + models, default_id, warnings = normalize_model_list( + [ + {"id": "a", "isDefault": True}, + {"id": "a", "isDefault": False}, + {"id": "b", "isDefault": True}, + {"oops": 1}, + "garbage", + ] + ) + assert [m["id"] for m in models] == ["a", "b"] + assert default_id == "a" + assert any("2 default models" in w for w in warnings) + assert any("skipped 2 malformed" in w for w in warnings) + + +def test_parse_agy_models_output_json_payload(): + models, default_id, warnings = parse_agy_models_output( + json.dumps( + { + "models": [ + {"id": "gemini-3-pro", "name": "Gemini 3 Pro", "isDefault": True, + "supportedEfforts": ["low", "high"]}, + {"id": "claude-x", "supportedEfforts": []}, + ] + } + ) + ) + assert [m["id"] for m in models] == ["gemini-3-pro", "claude-x"] + assert models[0]["supported_efforts"] == ["low", "high"] + assert models[1]["supported_efforts"] == [] + assert default_id == "gemini-3-pro" + assert not any("text output" in w for w in warnings) + + +def test_parse_agy_models_output_text_bullets_default_ansi_junk(): + stdout = ( + "\x1b[32m gemini-3.8-flash-medium \x1b[0m\n" + "claude-sonnet-4-5 (default)\n" + "2) gemini-3-pro\n" + "not a model line with spaces\n" + "---\n" + ) + models, default_id, warnings = parse_agy_models_output(stdout) + assert [m["id"] for m in models] == ["gemini-3.8-flash-medium", "claude-sonnet-4-5", "gemini-3-pro"] + assert all(m["name"] is None and m["supported_efforts"] is None for m in models) + assert [m["is_default"] for m in models] == [False, True, False] + assert default_id == "claude-sonnet-4-5" + assert any("ignored 1 line(s)" in w for w in warnings) + assert any("text output" in w for w in warnings) + + +def test_parse_agy_models_output_empty_and_unrecognized(): + assert parse_agy_models_output("") == ([], None, ["agy models returned no output"]) + models, default_id, warnings = parse_agy_models_output('{"totally": "different"}') + assert models == [] and default_id is None + assert any("unrecognized JSON" in w for w in warnings) + + +# --- agy adapter -------------------------------------------------------------- + + +async def test_agy_list_models_text_success(bridge_home, tmp_path): + script = _write_script(tmp_path, "fake_agy.py", FAKE_AGY) + report = tmp_path / "report.json" + adapter = _agy_adapter( + bridge_home, + [sys.executable, str(script)], + agent_env={"AGY_MARKER": "yes", "FAKE_AGY_REPORT": str(report)}, + ) + cwd = str(tmp_path / "work") + Path(cwd).mkdir() + result = await adapter.list_models(cwd) + assert result["source"] == "agy models" + assert [m["id"] for m in result["models"]] == [ + "gemini-3.8-flash-medium", + "claude-sonnet-4-5", + "gemini-3-pro", + ] + assert result["default_model"] == "claude-sonnet-4-5" + assert result["models"][0]["supported_efforts"] is None + evidence = json.loads(report.read_text(encoding="utf-8")) + assert evidence["cwd"] == cwd # discovery ran in the requested cwd + assert evidence["agent_env"] == "yes" # agent env block respected + assert evidence["worker_context"] == "worker" # nested-bridge marker set + + +async def test_agy_list_models_env_deny_strips_secret(bridge_home, tmp_path, monkeypatch): + monkeypatch.setenv("DISCOVERY_SECRET", "topsecret") + script = _write_script(tmp_path, "fake_agy.py", FAKE_AGY) + report = tmp_path / "report.json" + adapter = _agy_adapter( + bridge_home, + [sys.executable, str(script)], + agent_env={"FAKE_AGY_REPORT": str(report)}, + env_config=EnvConfig(deny=["DISCOVERY_*"]), + ) + result = await adapter.list_models(str(tmp_path)) + assert [m["id"] for m in result["models"]] == [ + "gemini-3.8-flash-medium", + "claude-sonnet-4-5", + "gemini-3-pro", + ] + evidence = json.loads(report.read_text(encoding="utf-8")) + assert evidence["denied_present"] is False + + +async def test_agy_list_models_json_payload(bridge_home, tmp_path, monkeypatch): + script = _write_script(tmp_path, "fake_agy.py", FAKE_AGY) + monkeypatch.setenv("FAKE_AGY_MODE", "json") + adapter = _agy_adapter(bridge_home, [sys.executable, str(script)]) + result = await adapter.list_models(str(tmp_path)) + assert result["default_model"] == "gemini-3-pro" + by_id = {m["id"]: m for m in result["models"]} + assert by_id["gemini-3-pro"]["name"] == "Gemini 3 Pro" + assert by_id["gemini-3-pro"]["supported_efforts"] == ["low", "high"] + assert by_id["gemini-3-pro"]["is_default"] is True + assert by_id["claude-x"]["supported_efforts"] == [] + # The id-less entry was skipped with a warning, silently dropped otherwise. + assert any("malformed" in w for w in result["warnings"]) + + +async def test_agy_list_models_nonzero_exit_redacts_stderr(bridge_home, tmp_path, monkeypatch): + script = _write_script(tmp_path, "fake_agy.py", FAKE_AGY) + monkeypatch.setenv("FAKE_AGY_MODE", "exit3") + adapter = _agy_adapter(bridge_home, [sys.executable, str(script)]) + with pytest.raises(RuntimeError, match="exited with code 3") as caught: + await adapter.list_models(str(tmp_path)) + joined = str(caught.value) + assert "sk-testsecret123" not in joined + assert "apikeyhunter2" not in joined + + +async def test_agy_list_models_timeout_reaps_worker(bridge_home, tmp_path, monkeypatch): + script = _write_script(tmp_path, "fake_agy.py", FAKE_AGY) + monkeypatch.setenv("FAKE_AGY_MODE", "hang") + adapter = _agy_adapter(bridge_home, [sys.executable, str(script)]) + started = time.monotonic() + with pytest.raises(TimeoutError): + await asyncio.wait_for(adapter.list_models(str(tmp_path)), timeout=0.3) + assert time.monotonic() - started < 10 + + +async def test_agy_list_models_cancellation_kills_worker(bridge_home, tmp_path, monkeypatch): + script = _write_script(tmp_path, "fake_agy.py", FAKE_AGY) + monkeypatch.setenv("FAKE_AGY_MODE", "hang") + adapter = _agy_adapter(bridge_home, [sys.executable, str(script)]) + task = asyncio.create_task(adapter.list_models(str(tmp_path))) + await asyncio.sleep(0.2) + task.cancel() + with pytest.raises(asyncio.CancelledError): + await asyncio.wait_for(task, timeout=10) + + +# --- codex adapter ------------------------------------------------------------ + + +def _patch_codex_command(monkeypatch, script: Path): + monkeypatch.setattr( + "agent_bridge.adapters.codex.resolve_codex_command", + lambda command, fallbacks=None, *, env=None: [sys.executable, str(script)], + ) + + +async def test_codex_list_models_pagination_overrides_and_cwd( + bridge_home, tmp_path, monkeypatch +): + script = _write_script(tmp_path, "fake_codex.py", FAKE_CODEX) + _patch_codex_command(monkeypatch, script) + report = tmp_path / "codex_report.json" + monkeypatch.setenv("FAKE_CODEX_REPORT", str(report)) + override = 'model_providers.custom.base_url="http://localhost:9999/v1"' + adapter = _codex_adapter(bridge_home, ["codex"], overrides=[override]) + cwd = str(tmp_path / "work") + Path(cwd).mkdir() + result = await adapter.list_models(cwd) + assert result["source"] == "codex app-server model/list" + by_id = {m["id"]: m for m in result["models"]} + assert set(by_id) == {"gpt-a", "gpt-b"} + # Verbatim worker-native values: ids, display names, raw effort labels. + assert by_id["gpt-a"]["name"] == "A" + assert by_id["gpt-a"]["supported_efforts"] == ["medium", "xhigh"] + assert by_id["gpt-a"]["is_default"] is True + assert by_id["gpt-b"]["supported_efforts"] == ["high"] + assert result["default_model"] == "gpt-a" + evidence = json.loads(report.read_text(encoding="utf-8")) + argv = evidence["argv"] + assert argv[-1] == "app-server" + # config_overrides are forwarded as -c before the subcommand. + assert argv[argv.index("-c") + 1] == override + assert evidence["cwd"] == cwd + list_requests = [r for r in evidence["requests"] if r.get("method") == "model/list"] + assert len(list_requests) == 2 # paginated via nextCursor + assert list_requests[0]["params"]["includeHidden"] is False + assert list_requests[1]["params"]["cursor"] == "page2" + + +async def test_codex_list_models_malformed_entries_warn(bridge_home, tmp_path, monkeypatch): + script = _write_script(tmp_path, "fake_codex.py", FAKE_CODEX) + _patch_codex_command(monkeypatch, script) + monkeypatch.setenv("FAKE_CODEX_MODELS_MODE", "malformed") + adapter = _codex_adapter(bridge_home, ["codex"]) + result = await adapter.list_models(str(tmp_path)) + assert [m["id"] for m in result["models"]] == ["gpt-good"] + assert result["default_model"] == "gpt-good" + assert any("skipped 2 malformed" in w for w in result["warnings"]) + + +async def test_codex_list_models_rpc_error_redacted(bridge_home, tmp_path, monkeypatch): + script = _write_script(tmp_path, "fake_codex.py", FAKE_CODEX) + _patch_codex_command(monkeypatch, script) + monkeypatch.setenv("FAKE_CODEX_MODELS_MODE", "rpc_error") + adapter = _codex_adapter(bridge_home, ["codex"]) + with pytest.raises(RuntimeError, match="request failed") as caught: + await adapter.list_models(str(tmp_path)) + joined = str(caught.value) + assert "sk-leakedtok987" not in joined + + +async def test_codex_list_models_timeout_reaps_worker(bridge_home, tmp_path, monkeypatch): + script = _write_script(tmp_path, "fake_codex.py", FAKE_CODEX) + _patch_codex_command(monkeypatch, script) + monkeypatch.setenv("FAKE_CODEX_MODELS_MODE", "hang") + adapter = _codex_adapter(bridge_home, ["codex"]) + started = time.monotonic() + with pytest.raises(TimeoutError): + await asyncio.wait_for(adapter.list_models(str(tmp_path)), timeout=0.3) + assert time.monotonic() - started < 10 + + +async def test_codex_list_models_command_not_found_raises( + bridge_home, tmp_path, monkeypatch +): + def _raise(command, fallbacks=None, *, env=None): + raise FileNotFoundError("codex not found") + + monkeypatch.setattr("agent_bridge.adapters.codex.resolve_codex_command", _raise) + adapter = _codex_adapter(bridge_home, ["codex"]) + with pytest.raises(FileNotFoundError, match="codex not found"): + await adapter.list_models(str(tmp_path)) + + +# --- run_bounded_command ------------------------------------------------------ + + +async def test_run_bounded_command_truncates_extra_output(tmp_path, monkeypatch): + script = _write_script( + tmp_path, + "chatty.py", + "for i in range(10):\n print(f'line-{i}')\n", + ) + monkeypatch.setattr(model_discovery_exec, "DISCOVERY_LINE_LIMIT", 5) + returncode, stdout, _stderr, warnings = await run_bounded_command( + [sys.executable, str(script)], env={"PATH": os.environ.get("PATH", "")}, cwd=str(tmp_path) + ) + assert returncode == 0 + assert stdout.splitlines() == [f"line-{i}" for i in range(5)] + assert warnings == ["worker output exceeded discovery bounds; extra output was discarded"] + + +async def test_codex_app_server_cursor_loop_is_bounded(tmp_path): + script = _write_script(tmp_path, "fake_codex.py", FAKE_CODEX) + with pytest.raises(RuntimeError, match="did not finish paginating"): + await codex_app_server_list_models( + [sys.executable, str(script), "app-server"], + env={**os.environ, "FAKE_CODEX_MODELS_MODE": "cursor_loop"}, + cwd=str(tmp_path), + timeout=5.0, + ) + + +def test_agy_tab_separated_catalog_matches_live_format(): + models, default, warnings = parse_agy_models_output( + "gemini-4-argon-low\tGemini 4 Argon (Low)\n" + "claude-sonnet-5-5-high\tClaude Sonnet 5.5 (High)\n" + ) + assert [(m["id"], m["name"]) for m in models] == [ + ("gemini-4-argon-low", "Gemini 4 Argon (Low)"), + ("claude-sonnet-5-5-high", "Claude Sonnet 5.5 (High)"), + ] + assert default is None + assert all(m["supported_efforts"] is None for m in models) + assert not any("ignored" in warning for warning in warnings) + + +def test_agy_top_level_default_keeps_row_flag_consistent(): + models, default, _ = parse_agy_models_output(json.dumps({ + "defaultModel": "model-a", "models": [{"id": "model-a"}, {"id": "model-b"}], + })) + assert default == "model-a" + assert [m["is_default"] for m in models] == [True, False] + + +@pytest.mark.parametrize("mode", ["bad_page", "bad_cursor"]) +async def test_codex_rejects_malformed_page(tmp_path, mode): + script = _write_script(tmp_path, "fake_codex.py", FAKE_CODEX) + with pytest.raises(RuntimeError, match="malformed"): + await codex_app_server_list_models( + [sys.executable, str(script), "app-server"], + env={**os.environ, "FAKE_CODEX_MODELS_MODE": mode}, + cwd=str(tmp_path), timeout=5, + ) + + +async def test_agy_unknown_output_is_failure(bridge_home, tmp_path): + script = _write_script(tmp_path, "fake_agy.py", 'print("unexpected response without model IDs")') + adapter = _agy_adapter(bridge_home, [sys.executable, str(script)]) + with pytest.raises(RuntimeError, match="no model slugs recognized"): + await adapter.list_models(str(tmp_path)) diff --git a/tests/test_server_tools.py b/tests/test_server_tools.py index cfb1cf9..2c8a322 100644 --- a/tests/test_server_tools.py +++ b/tests/test_server_tools.py @@ -13,7 +13,7 @@ from agent_bridge.server import INSTRUCTIONS, _error, _registry, list_sessions, mcp -def test_ten_tools_registered(): +def test_eleven_tools_registered(): names = sorted(mcp._tool_manager._tools) assert names == [ "cancel_task", @@ -23,6 +23,7 @@ def test_ten_tools_registered(): "get_result", "get_transcript", "list_agents", + "list_models", "list_sessions", "set_preferences", "wait_task", From 6ee3e9210f59a4bec5404773c3bb8c010bd8e313 Mon Sep 17 00:00:00 2001 From: "edge.xyz" <32131214+edgexyz@users.noreply.github.com> Date: Fri, 16 Oct 2026 21:39:24 -0400 Subject: [PATCH 2/6] Discover DSH models from advertised ACP capabilities --- ORCHESTRATION.zh-CN.md | 2 +- README.md | 2 +- SETUP.md | 5 ++- skills/agent-bridge/SKILL.md | 2 +- src/agent_bridge/adapters/acp.py | 11 ++----- src/agent_bridge/server.py | 2 +- tests/test_model_discovery_acp.py | 55 ++++++++++++++++++++++++++++--- 7 files changed, 61 insertions(+), 18 deletions(-) diff --git a/ORCHESTRATION.zh-CN.md b/ORCHESTRATION.zh-CN.md index 0a76417..c2cbf24 100644 --- a/ORCHESTRATION.zh-CN.md +++ b/ORCHESTRATION.zh-CN.md @@ -87,4 +87,4 @@ ACP 失败保留字符串 `error`,并可附带 `failure`。`failure.prompt_may ## 按需查询模型目录 -明确指定模型前,若 `list_agents.model_discovery.supported=true`,调用 `list_models(agent=..., cwd=...)` 获取 worker 公布的模型 ID。首批支持 Kimi、原生 DSH、Antigravity 和 Codex。默认缓存一小时,过期后仅在下一次查询时刷新;配置或账号变更后可用 `refresh=true`。`supported_efforts` 是 worker 原生档位,未知模型的档位保持 null,`dispatch_task.effort` 的五档输入与映射不变。目录不保证账号额度或调用成功。查询不发送模型提示、不改变活动会话;ACP 可能留下空的 worker 会话。worker 继承实例不能查询模型目录。 +明确指定模型前,若 `list_agents.model_discovery.supported=true`,调用 `list_models(agent=..., cwd=...)` 获取 worker 公布的模型 ID。首批支持 Kimi、DSH、Antigravity 和 Codex。默认缓存一小时,过期后仅在下一次查询时刷新;配置或账号变更后可用 `refresh=true`。`supported_efforts` 是 worker 原生档位,未知模型的档位保持 null,`dispatch_task.effort` 的五档输入与映射不变。目录不保证账号额度或调用成功。查询不发送模型提示、不改变活动会话;ACP 可能留下空的 worker 会话。worker 继承实例不能查询模型目录。 diff --git a/README.md b/README.md index 38dcd48..e320eaa 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ Close coordinators that are holding Bridge, then `agent-bridge upgrade`, then re | `end_session` | Shut down a worker process | Call `list_models(agent="kimi", cwd="/absolute/project")` before selecting a -model. Initial support covers Kimi and native DSH through shared ACP discovery, +model. Initial support covers Kimi and DSH through shared ACP discovery, Antigravity through `agy models`, and Codex through app-server `model/list`. `list_agents` reports `model_discovery` capability without querying catalogs. Discovery never sends a model prompt or changes an active work session; ACP diff --git a/SETUP.md b/SETUP.md index 83ab266..2c3bed9 100644 --- a/SETUP.md +++ b/SETUP.md @@ -456,9 +456,12 @@ list_models(agent="kimi", cwd="/absolute/project") list_models(agent="antigravity", cwd="/absolute/project", refresh=true) ``` -Initial support: Kimi and native DSH share the ACP `initialize` / `session/new` +Initial support: Kimi and DSH share the ACP `initialize` / `session/new` discovery path; AGY uses `models`; Codex uses app-server `model/list` with pagination. Claude and the remaining workers are not enabled in this release. +DSH discovery attempts the same ACP handshake for native and legacy launchers; +only a session that advertises no catalog receives an unavailable warning. +Handshake failures remain errors rather than being reported as unsupported. Discovery does not send a model prompt, configure model/effort, change active sessions, or register a Bridge task. ACP may create an empty session in the worker's own history; all discovery processes are closed after the lookup, and diff --git a/skills/agent-bridge/SKILL.md b/skills/agent-bridge/SKILL.md index 77fbe42..67fb660 100644 --- a/skills/agent-bridge/SKILL.md +++ b/skills/agent-bridge/SKILL.md @@ -21,7 +21,7 @@ Claude `status`: shared `5h`/`weekly` only. Before dispatch, even if `ok`, check ## Model discovery -Use `list_models(agent=..., cwd=...)` to obtain confirmed worker-advertised model IDs before explicitly selecting a model. `list_agents.model_discovery` reports whether discovery is supported; initial support is Kimi, native DSH, Antigravity, and Codex. Supply this conversation's absolute project directory. Catalog presence does not prove account access or remaining quota. `supported_efforts` contains worker-native levels where known, while `dispatch_task.effort` still accepts `off|low|medium|high|max` and applies the adapter mapping. Unknown model-specific levels remain null. Catalogs are cached for one hour by default (`[models] cache_sec`); `refresh=true` forces an on-demand query. Discovery sends no prompt and does not configure an active session, but ACP may create an empty worker-owned session. Do not call `list_models` when `dispatch_enabled=false`. +Use `list_models(agent=..., cwd=...)` to obtain confirmed worker-advertised model IDs before explicitly selecting a model. `list_agents.model_discovery` reports whether discovery is supported; initial support is Kimi, DSH, Antigravity, and Codex. Supply this conversation's absolute project directory. Catalog presence does not prove account access or remaining quota. `supported_efforts` contains worker-native levels where known, while `dispatch_task.effort` still accepts `off|low|medium|high|max` and applies the adapter mapping. Unknown model-specific levels remain null. Catalogs are cached for one hour by default (`[models] cache_sec`); `refresh=true` forces an on-demand query. Discovery sends no prompt and does not configure an active session, but ACP may create an empty worker-owned session. Do not call `list_models` when `dispatch_enabled=false`. ## Step 1 — dispatch, or do it yourself? diff --git a/src/agent_bridge/adapters/acp.py b/src/agent_bridge/adapters/acp.py index 8b0fd22..f9c11f4 100644 --- a/src/agent_bridge/adapters/acp.py +++ b/src/agent_bridge/adapters/acp.py @@ -766,7 +766,9 @@ def model_id(value: str) -> str: if offered and set(offered) != {"true", "false"}: efforts = list(dict.fromkeys(offered)) break - warnings = [] if choices else ["The fresh ACP session did not advertise any models."] + warnings = [] if choices else [ + "Model discovery is unavailable: the ACP session did not advertise a model catalog." + ] return { "source": "acp", "models": [ @@ -814,13 +816,6 @@ async def list_models(self, cwd: str) -> dict[str, Any]: """ if self.agent.name == "dsh": cmd = resolve_dsh_command(self.agent.command, self.agent.fallback_commands) - if not command_has_native_acp(cmd): - return { - "source": "acp", - "models": [], - "default_model": None, - "warnings": ["Model discovery is unsupported by legacy DSH ACP; use the native ACP profile."], - } else: cmd = resolve_command(self.agent.command, self.agent.fallback_commands) env = self._env() diff --git a/src/agent_bridge/server.py b/src/agent_bridge/server.py index ce2f857..5605aba 100644 --- a/src/agent_bridge/server.py +++ b/src/agent_bridge/server.py @@ -104,7 +104,7 @@ async def list_agents(ctx: Context) -> dict[str, Any]: @mcp.tool(annotations=READ_ONLY) async def list_models(ctx: Context, agent: str, cwd: str, refresh: bool = False) -> dict[str, Any]: - """Discover the selected worker's advertised models. cwd must be this conversation's absolute project directory. Supports Kimi/native DSH through a short-lived ACP session, Antigravity through `agy models`, and Codex through app-server model/list. Does not prompt a model or modify active sessions. Results are cached per worker/project for one hour by default ([models] cache_sec); refresh bypasses the cache. supported_efforts contains worker-native values only for models whose levels are known; null means unknown. Catalog presence does not prove account quota or successful invocation. Discovery may initialize a worker-owned session and contact its service. Disabled inside worker-inherited Bridge instances.""" + """Discover the selected worker's advertised models. cwd must be this conversation's absolute project directory. Supports Kimi/DSH through a short-lived ACP session, Antigravity through `agy models`, and Codex through app-server model/list. Does not prompt a model or modify active sessions. Results are cached per worker/project for one hour by default ([models] cache_sec); refresh bypasses the cache. supported_efforts contains worker-native values only for models whose levels are known; null means unknown. Catalog presence does not prove account quota or successful invocation. Discovery may initialize a worker-owned session and contact its service. Disabled inside worker-inherited Bridge instances.""" try: return {"ok": True, **await _registry(ctx).list_models(agent, cwd, refresh=refresh)} except Exception as exc: diff --git a/tests/test_model_discovery_acp.py b/tests/test_model_discovery_acp.py index 2884572..9deb234 100644 --- a/tests/test_model_discovery_acp.py +++ b/tests/test_model_discovery_acp.py @@ -299,10 +299,55 @@ def prepare(command, env, **kwargs): ] -async def test_legacy_dsh_returns_warning_without_starting_process(discovery): - adapter, _active = discovery.build("dsh", ["dsh-acp-demo"]) +@pytest.mark.parametrize("payload", [ + {"configOptions": [{"id": "model", "currentValue": "legacy-model", "options": [ + {"value": "legacy-model", "name": "Legacy Model"}, + ]}]}, + {"models": {"currentModelId": "legacy-model", "availableModels": [ + {"modelId": "legacy-model", "name": "Legacy Model"}, + ]}}, +]) +async def test_legacy_dsh_discovers_actual_advertised_catalog(discovery, monkeypatch, payload): + prepared = [] + + def prepare(command, env, **kwargs): + prepared.append(kwargs) + return command, env + + monkeypatch.setattr(acp, "prepare_dsh_launch", prepare) + adapter, active = discovery.build("dsh", ["dsh-acp-demo"]) + discovery.conn.response = payload + result = await adapter.list_models(".") + assert prepared == [{}] + assert result["default_model"] == "legacy-model" + assert result["models"] == [{ + "id": "legacy-model", "name": "Legacy Model", + "is_default": True, "supported_efforts": None, + }] + assert result["warnings"] == [] + assert [name for name, _ in discovery.conn.calls] == ["initialize", "new_session", "close"] + assert len(discovery.spawns) == 1 + assert discovery.reaped == [discovery.proc] + assert adapter._live == {"existing-session": active} + + +async def test_legacy_dsh_warns_only_after_handshake_without_catalog(discovery, monkeypatch): + monkeypatch.setattr(acp, "prepare_dsh_launch", lambda command, env: (command, env)) + adapter, active = discovery.build("dsh", ["dsh-acp-demo"]) result = await adapter.list_models(".") assert result["models"] == [] - assert "legacy" in result["warnings"][0] - assert discovery.spawns == [] - assert discovery.reaped == [] + assert result["default_model"] is None + assert "did not advertise a model catalog" in result["warnings"][0] + assert [name for name, _ in discovery.conn.calls] == ["initialize", "new_session", "close"] + assert len(discovery.spawns) == 1 + assert discovery.reaped == [discovery.proc] + assert adapter._live == {"existing-session": active} + + +async def test_legacy_dsh_handshake_error_is_failure_not_unsupported(discovery, monkeypatch): + monkeypatch.setattr(acp, "prepare_dsh_launch", lambda command, env: (command, env)) + adapter, _active = discovery.build("dsh", ["dsh-acp-demo"]) + discovery.conn.fail_at = "new_session" + with pytest.raises(AcpError, match="new_session failed"): + await adapter.list_models(".") + assert discovery.reaped == [discovery.proc] From 5b77b2470a81faec7a32a3cc083747eb0408c322 Mon Sep 17 00:00:00 2001 From: "edge.xyz" <32131214+edgexyz@users.noreply.github.com> Date: Fri, 16 Oct 2026 22:03:04 -0400 Subject: [PATCH 3/6] Simplify advertised model discovery and serve cache hits without waiting --- README.md | 3 + SETUP.md | 6 + src/agent_bridge/adapters/antigravity.py | 21 +- src/agent_bridge/adapters/codex.py | 8 +- src/agent_bridge/model_catalog.py | 1 + src/agent_bridge/model_discovery_exec.py | 505 +++++------------------ src/agent_bridge/registry.py | 3 + src/agent_bridge/server.py | 2 +- tests/test_model_catalog.py | 13 + tests/test_model_discovery_exec.py | 505 +++++++---------------- 10 files changed, 298 insertions(+), 769 deletions(-) diff --git a/README.md b/README.md index e320eaa..6e317ee 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,9 @@ Antigravity through `agy models`, and Codex through app-server `model/list`. Discovery never sends a model prompt or changes an active work session; ACP discovery creates a separate worker-owned session that may remain in its history. Catalog entries are advertised options, not guarantees of account access or quota. +`default_model` is the query-side advertised default, not the coordinator's +selection for a later dispatch. Codex discovery uses app-server configuration; +exec retains its existing user-config isolation. `supported_efforts` contains worker-native levels only when known for that model; `null` means unknown. `dispatch_task.effort` still accepts only `off|low|medium|high|max` and applies the worker's mapping. diff --git a/SETUP.md b/SETUP.md index 2c3bed9..cdb6632 100644 --- a/SETUP.md +++ b/SETUP.md @@ -469,6 +469,12 @@ permission requests during ACP discovery are denied. Results contain `models` (`id`, `name`, `is_default`, `supported_efforts`), `default_model`, `source`, `fetched_at`, `cached`, and `warnings`. +Defaults and effort options describe what the query session advertises. They do +not predict the coordinator's later model selection. In particular, Codex +app-server reads its existing configuration while exec intentionally ignores +user config; discovery neither changes that behavior nor switches CODEX_HOME. +AGY parsing supports its verified tab-separated ID/name output and rejects +unrecognized output rather than guessing model IDs. `availability="advertised"` / `invocation_verified=false` distinguish a catalog from a successful model request. Unknown defaults and model-specific effort levels remain null. Effort levels are worker-native, not necessarily accepted diff --git a/src/agent_bridge/adapters/antigravity.py b/src/agent_bridge/adapters/antigravity.py index 9a98b7a..14e5e9b 100644 --- a/src/agent_bridge/adapters/antigravity.py +++ b/src/agent_bridge/adapters/antigravity.py @@ -17,7 +17,7 @@ from agent_bridge.diagnostics import redact_diagnostic from agent_bridge.model_discovery_exec import ( parse_agy_models_output, - run_bounded_command, + read_agy_models, ) from agent_bridge.models import Session, Task, TurnResult, agy_effort from agent_bridge.processes import ( @@ -258,22 +258,9 @@ async def list_models(self, cwd: str) -> dict[str, Any]: env = self._enforce_spawn_env( build_worker_env(self.agent.env, config=self.env_config, worker_context=True, home=self.home) ) - returncode, stdout, stderr, warnings = await run_bounded_command([*cmd, "models"], env=env, cwd=cwd) - if returncode != 0: - detail = redact_diagnostic(stderr.strip()) - warning = f"agy models exited with code {returncode}" - raise RuntimeError(warning + (f": {detail}" if detail else "")) - if warnings: - raise RuntimeError("; ".join(warnings)) - models, default_id, parse_warnings = parse_agy_models_output(stdout) - if not models: - raise RuntimeError("; ".join(parse_warnings) or "agy models returned no recognizable catalog") - return { - "source": source, - "models": models, - "default_model": default_id, - "warnings": [*warnings, *parse_warnings], - } + stdout = await read_agy_models(cmd, env=env, cwd=cwd) + return {"source": source, "models": parse_agy_models_output(stdout), + "default_model": None, "warnings": []} def _build_cmd(self, session: Session, task: Task) -> list[str]: cmd = resolve_command(self.agent.command, self.agent.fallback_commands) diff --git a/src/agent_bridge/adapters/codex.py b/src/agent_bridge/adapters/codex.py index fbf693f..7651f76 100644 --- a/src/agent_bridge/adapters/codex.py +++ b/src/agent_bridge/adapters/codex.py @@ -55,10 +55,10 @@ async def ensure_session(self, session: Session) -> None: async def list_models(self, cwd: str) -> dict[str, Any]: """List models via ``codex app-server`` JSON-RPC ``model/list``. - Read-only: no prompt is dispatched and no config is written. The - agents.toml ``config_overrides`` are passed as ``-c`` before the - ``app-server`` subcommand so custom providers/endpoints are reflected - in the listing. Failures propagate to the registry without caching. + Report the app-server catalog under its own configuration and the + worker's config_overrides. Its advertised default is not a promise + about a later exec turn, which intentionally ignores user config. + No prompt is sent and no configuration or Home path is changed. """ source = "codex app-server model/list" env = self._enforce_spawn_env(self._worker_env()) diff --git a/src/agent_bridge/model_catalog.py b/src/agent_bridge/model_catalog.py index 8e2a2ff..761a302 100644 --- a/src/agent_bridge/model_catalog.py +++ b/src/agent_bridge/model_catalog.py @@ -18,6 +18,7 @@ class ModelInfo(BaseModel): class ModelCatalog(BaseModel): source: str = Field(min_length=1) models: list[ModelInfo] + # Default advertised by the query, not an effective dispatch selection. default_model: str | None = None warnings: list[str] = Field(default_factory=list) diff --git a/src/agent_bridge/model_discovery_exec.py b/src/agent_bridge/model_discovery_exec.py index 9aedc6f..308b7a1 100644 --- a/src/agent_bridge/model_discovery_exec.py +++ b/src/agent_bridge/model_discovery_exec.py @@ -1,23 +1,9 @@ -"""Bounded model discovery for one-shot exec workers. - -Antigravity (``agy models``) and Codex (``codex app-server model/list``) can -answer "which models can this worker run" without dispatching a turn. No prompt -is ever sent and Bridge does not change worker configuration; the listing -reflects whatever the resolved, configured worker reports — including custom -endpoints configured through the worker's own config overrides. - -Worker output is untrusted: diagnostics that cross into MCP responses or logs -pass through ``redact_diagnostic`` first, and stdout/stderr are bounded so a -chatty or stuck worker cannot stall or exhaust the coordinator. The registry -owns the outer timeout and cache; the bounds here keep one run self-contained. -""" +"""Worker-specific model catalog parsing; no inference requests.""" from __future__ import annotations import asyncio import contextlib -import json -import logging import re import subprocess import sys @@ -26,387 +12,138 @@ from agent_bridge.diagnostics import redact_diagnostic from agent_bridge.processes import reap_subprocess +from agent_bridge.quota_codex import CLIENT_INFO, READ_LIMIT, _read_response, _send -log = logging.getLogger(__name__) - -# The registry owns the configurable deadline and caching. Helpers may also -# receive an explicit timeout when used independently in focused tests. -# asyncio's default 64 KiB stream limit is too small for worker banners; one -# JSON event per line still must fit under this. -DISCOVERY_STDIO_LIMIT = 1024 * 1024 -# Kept output caps: enough for any real model catalog, small enough to ignore -# a worker that streams garbage forever. -DISCOVERY_OUTPUT_BYTE_LIMIT = 4 * 1024 * 1024 -DISCOVERY_LINE_LIMIT = 20000 -DISCOVERY_STDERR_TAIL_LIMIT = 16000 -# model/list pages and a hard page cap so a broken cursor loop cannot run away. -MODEL_LIST_PAGE_LIMIT = 100 +AGY_OUTPUT_LIMIT = 4 * 1024 * 1024 +AGY_STDERR_LIMIT = 16000 MODEL_LIST_MAX_PAGES = 100 - -CLIENT_INFO = {"name": "agent-bridge", "title": "Agent Bridge", "version": "1"} - _ANSI_RE = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]") -_AGY_SLUG_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._:+/-]*$") -_AGY_BULLET_RE = re.compile(r"^\s*(?:[-*•]|\d+[.)])\s+") -_AGY_DEFAULT_SUFFIX_RE = re.compile(r"\s*\(default\)\s*$", re.IGNORECASE) -_AGY_BORDER_CHARS = set("-\u2013\u2014=*_#+|") - - -def strip_ansi(text: str) -> str: - return _ANSI_RE.sub("", text) - - -def _efforts_of(raw: Mapping[str, Any]) -> list[str] | None: - """Worker-native offered effort values, verbatim, never Bridge-mapped.""" - for key in ("supported_efforts", "supportedEfforts", "supportedReasoningEfforts"): - if key not in raw: - continue - items = raw[key] - if not isinstance(items, list): - continue - efforts: list[str] = [] - for item in items: - if isinstance(item, str) and item.strip(): - efforts.append(item) - elif isinstance(item, Mapping): - for sub_key in ("reasoningEffort", "effort", "value", "id"): - value = item.get(sub_key) - if isinstance(value, str) and value.strip(): - efforts.append(value) - break - return efforts - return None - - -def normalize_model_entry(raw: Any) -> dict[str, Any] | None: - """One worker-native model entry normalized to the discovery shape. - - The identifier is returned exactly as the worker reported it. ``name`` and - ``supported_efforts`` stay ``None`` when the worker did not offer them; - effort values are the worker's own offered labels, not values Bridge - accepts as input. - """ - if not isinstance(raw, Mapping): - return None - ident = raw.get("id") - if not isinstance(ident, str) or not ident.strip(): - return None - name = raw.get("displayName") or raw.get("name") - if not isinstance(name, str) or not name.strip(): - name = None - return { - "id": ident, - "name": name, - "is_default": raw.get("is_default") is True or raw.get("isDefault") is True, - "supported_efforts": _efforts_of(raw), - } def normalize_model_list(entries: Sequence[Any]) -> tuple[list[dict[str, Any]], str | None, list[str]]: - """Normalize a raw entry list; dedupe by id, warn on malformed rows.""" - warnings: list[str] = [] - models: list[dict[str, Any]] = [] - seen: set[str] = set() - default_id: str | None = None - defaults_seen = 0 - skipped = 0 - for raw in entries: - entry = normalize_model_entry(raw) - if entry is None: - skipped += 1 + """Normalize the documented Codex model/list schema, preserving level order.""" + models = [] + seen = set() + warnings = [] + default = None + for entry in entries: + if not isinstance(entry, Mapping) or not isinstance(entry.get("id"), str) or not entry["id"].strip(): + warnings.append("Codex model/list contained a row without a model ID") continue - if entry["id"] in seen: + ident = entry["id"] + if ident in seen: continue - seen.add(entry["id"]) - models.append(entry) - if entry["is_default"]: - defaults_seen += 1 - if default_id is None: - default_id = entry["id"] - if defaults_seen > 1: - warnings.append(f"worker reported {defaults_seen} default models; keeping {default_id}") - for model in models: - model["is_default"] = model["id"] == default_id - if skipped: - plural = "entry" if skipped == 1 else "entries" - warnings.append(f"skipped {skipped} malformed model {plural}") - return models, default_id, warnings - - -def _models_from_payload(payload: Any) -> tuple[list[dict[str, Any]], str | None, list[str]]: - warnings: list[str] = [] - raw_default: Any = None - if isinstance(payload, Mapping): - raw_entries = payload.get("models") - if raw_entries is None: - raw_entries = payload.get("data") - raw_default = payload.get("default_model", payload.get("defaultModel")) - elif isinstance(payload, list): - raw_entries = payload - else: - raw_entries = None - if not isinstance(raw_entries, list): - return [], None, ["unrecognized JSON payload from agy models"] - models, default_id, list_warnings = normalize_model_list(raw_entries) - warnings.extend(list_warnings) - if isinstance(raw_default, str) and raw_default.strip() and default_id is None: - if any(model["id"] == raw_default for model in models): - default_id = raw_default - for model in models: - model["is_default"] = model["id"] == default_id - else: - warnings.append("agy default model was absent from its advertised catalog") - return models, default_id, warnings - + seen.add(ident) + raw_efforts = entry.get("supportedReasoningEfforts") + efforts = None + if isinstance(raw_efforts, list): + parsed = [ + item["reasoningEffort"] + for item in raw_efforts + if isinstance(item, Mapping) + and isinstance(item.get("reasoningEffort"), str) + and item["reasoningEffort"] + ] + efforts = parsed if parsed or not raw_efforts else None + if raw_efforts and not parsed: + warnings.append(f"Codex did not provide recognizable reasoning levels for {ident}") + if entry.get("isDefault") is True: + if default is None: + default = ident + else: + warnings.append("Codex reported multiple default models; keeping the first") + models.append( + { + "id": ident, + "name": entry.get("displayName") if isinstance(entry.get("displayName"), str) else None, + "is_default": ident == default, + "supported_efforts": efforts, + } + ) + return models, default, warnings -def parse_agy_models_output(stdout: str) -> tuple[list[dict[str, Any]], str | None, list[str]]: - """Parse ``agy models`` output. - The verified AGY 1.4 format is tab-separated slug and display name. - Also accept structured catalogs or one slug per line, optionally with - bullets, ANSI styling, and a ``(default)`` marker. Other text is ignored - with a warning rather than guessed into a model id. - """ - warnings: list[str] = [] - text = (stdout or "").strip() - if not text: - return [], None, ["agy models returned no output"] - if text[0] in "[{": - try: - payload = json.loads(text) - except json.JSONDecodeError: - payload = None - if payload is not None and isinstance(payload, (Mapping, list)): - models, default_id, json_warnings = _models_from_payload(payload) - if models or not any("unrecognized" in w for w in json_warnings): - return models, default_id, json_warnings - # A JSON-looking payload we cannot map: fall through to the text - # parse so a slug listing that merely starts with [ or { survives. - warnings.extend(json_warnings) +def parse_agy_models_output(stdout: str) -> list[dict[str, Any]]: + """AGY 1.4 emits tab-separated ID and display name; other formats fail.""" models = [] - seen: set[str] = set() - default_id = None - skipped = 0 - for raw_line in strip_ansi(text).splitlines(): - line = raw_line.strip() - if not line or set(line) <= _AGY_BORDER_CHARS: - continue - line = _AGY_BULLET_RE.sub("", line).strip() - if not line: + seen = set() + for line in _ANSI_RE.sub("", stdout).splitlines(): + if not line.strip(): continue - name = None - if "\t" in line: - columns = line.split("\t", 1) - line, name = columns[0].strip(), columns[1].strip() or None - is_default = False - suffix = _AGY_DEFAULT_SUFFIX_RE.search(line) - if suffix: - is_default = True - line = line[: suffix.start()].strip() - if name and (suffix := _AGY_DEFAULT_SUFFIX_RE.search(name)): - is_default = True - name = name[:suffix.start()].strip() or None - if not _AGY_SLUG_RE.fullmatch(line): - skipped += 1 + fields = line.split("\t") + if len(fields) != 2: + raise RuntimeError("Unrecognized agy models output: expected tab-separated ID and name") + ident, name = (field.strip() for field in fields) + if not ident or any(char.isspace() for char in ident) or not name: + raise RuntimeError("Unrecognized agy models output: missing or invalid ID/name") + if ident in seen: continue - if line not in seen: - seen.add(line) - models.append( - {"id": line, "name": name, "is_default": is_default, "supported_efforts": None} - ) - if is_default and default_id is None: - default_id = line - if skipped: - warnings.append(f"ignored {skipped} line(s) that do not look like model slugs") + seen.add(ident) + models.append({"id": ident, "name": name, "is_default": False, "supported_efforts": None}) if not models: - warnings.append("no model slugs recognized in agy models output") - else: - warnings.append("parsed agy models text output; effort options are unavailable") - return models, default_id, warnings - + raise RuntimeError("agy models returned no model catalog") + return models -async def _drain_stderr(proc: asyncio.subprocess.Process) -> str: - if proc.stderr is None: - return "" - tail = "" - try: - while True: - line = await proc.stderr.readline() - if not line: - return tail - text = line.decode("utf-8", errors="replace").rstrip() - if text: - tail = f"{tail}\n{text}"[-DISCOVERY_STDERR_TAIL_LIMIT:] - except (ValueError, OSError): - log.warning( - "discovery stderr drain aborted (line over %s MiB or stream closed); later stderr evidence is lost", - DISCOVERY_STDIO_LIMIT // (1024 * 1024), - exc_info=True, - ) - return tail - - -async def _read_bounded_stdout(proc: asyncio.subprocess.Process, warnings: list[str]) -> str: - """Read stdout to EOF, keeping at most the byte/line caps. - Lines past the caps are still drained (the worker must not block on a full - pipe) but not retained. - """ +async def _agy_stdout(proc: asyncio.subprocess.Process) -> str: assert proc.stdout is not None - parts: list[bytes] = [] - total = 0 - truncated = False - while True: - try: - line = await proc.stdout.readline() - except ValueError as exc: - raise RuntimeError( - f"worker emitted a stdout line longer than {DISCOVERY_STDIO_LIMIT // (1024 * 1024)} MiB" - ) from exc - if not line: - break - if total + len(line) <= DISCOVERY_OUTPUT_BYTE_LIMIT and len(parts) < DISCOVERY_LINE_LIMIT: - parts.append(line) - total += len(line) - elif not truncated: - warnings.append("worker output exceeded discovery bounds; extra output was discarded") - truncated = True - # Keep draining without retaining so the pipe never backs up. + parts = [] + size = 0 + while chunk := await proc.stdout.read(65536): + size += len(chunk) + if size > AGY_OUTPUT_LIMIT: + raise RuntimeError("agy models exceeded the output limit") + parts.append(chunk) return b"".join(parts).decode("utf-8", errors="replace") -async def run_bounded_command( - argv: Sequence[str], - *, - env: Mapping[str, str], - cwd: str, - timeout: float | None = None, - stdin_bytes: bytes | None = None, -) -> tuple[int | None, str, str, list[str]]: - """Run one short-lived read-only worker command with bounded output. +async def _agy_stderr(proc: asyncio.subprocess.Process) -> bytes: + assert proc.stderr is not None + tail = b"" + while chunk := await proc.stderr.read(65536): + tail = (tail + chunk)[-AGY_STDERR_LIMIT:] + return tail + - Returns ``(returncode, stdout_text, stderr_tail, warnings)``. Raises - ``RuntimeError`` on timeout; the process is always reaped, including when - the caller's task is cancelled mid-run. - """ - warnings: list[str] = [] +async def read_agy_models(command: Sequence[str], *, env: Mapping[str, str], cwd: str) -> str: + """Run agy models; caller owns deadline, this function always reaps its child.""" kwargs: dict[str, Any] = {} if sys.platform == "win32": kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP proc = await asyncio.create_subprocess_exec( - *argv, - stdin=asyncio.subprocess.PIPE if stdin_bytes is not None else asyncio.subprocess.DEVNULL, + *command, + "models", + stdin=asyncio.subprocess.DEVNULL, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, env=dict(env), cwd=cwd, - limit=DISCOVERY_STDIO_LIMIT, **kwargs, ) - stderr_task = asyncio.create_task(_drain_stderr(proc)) - - async def _exchange() -> str: - if proc.stdin is not None: - try: - if stdin_bytes is not None: - proc.stdin.write(stdin_bytes) - await proc.stdin.drain() - except (BrokenPipeError, ConnectionResetError, OSError): - pass - with contextlib.suppress(BrokenPipeError, ConnectionResetError, OSError): - proc.stdin.close() - stdout_text = await _read_bounded_stdout(proc, warnings) - await proc.wait() - return stdout_text - + stderr = asyncio.create_task(_agy_stderr(proc)) try: - stdout_text = await asyncio.wait_for(_exchange(), timeout=timeout) - if stderr_task.done(): - with contextlib.suppress(asyncio.CancelledError): - stderr_tail = stderr_task.result() - else: - stderr_tail = await stderr_task - except TimeoutError: - await reap_subprocess(proc) - raise RuntimeError(f"worker command did not finish within {timeout}s") from None + stdout = await _agy_stdout(proc) + await proc.wait() + diagnostic = (await stderr).decode("utf-8", errors="replace").strip() + if proc.returncode != 0: + raise RuntimeError(f"agy models exited with code {proc.returncode}: {redact_diagnostic(diagnostic)}") + return stdout finally: - if not stderr_task.done(): - stderr_task.cancel() - with contextlib.suppress(asyncio.CancelledError): - await stderr_task - # A registry deadline can land after a successful response while this - # block is already reaping; finish the kill escalation regardless. cleanup = asyncio.create_task(reap_subprocess(proc, timeout=2.0)) try: await asyncio.shield(cleanup) except asyncio.CancelledError: await cleanup raise - return proc.returncode, stdout_text, stderr_tail, warnings - - -async def rpc_send(proc: asyncio.subprocess.Process, payload: Mapping[str, Any]) -> None: - assert proc.stdin is not None - proc.stdin.write((json.dumps(payload) + "\n").encode("utf-8")) - await proc.stdin.drain() - - -def _rpc_error_text(error: Any) -> str: - if isinstance(error, Mapping): - message = error.get("message") - if isinstance(message, str) and message: - return message - return json.dumps(error, ensure_ascii=False)[:200] - return str(error)[:200] - - -async def rpc_read_response(proc: asyncio.subprocess.Process, wanted_id: int) -> Mapping[str, Any]: - """Read JSON-RPC lines until the response for ``wanted_id`` arrives. - - Notifications and unparseable lines are skipped; an error envelope raises - with a redacted message. - """ - assert proc.stdout is not None - while True: - try: - line = await proc.stdout.readline() - except ValueError as exc: - raise RuntimeError( - f"worker emitted a stdout line longer than {DISCOVERY_STDIO_LIMIT // (1024 * 1024)} MiB" - ) from exc - if not line: - raise RuntimeError("worker closed stdout before answering the request") - text = line.decode("utf-8", errors="replace").strip() - if not text: - continue - try: - message = json.loads(text) - except json.JSONDecodeError: - continue - if not isinstance(message, Mapping) or message.get("id") != wanted_id: - continue - if "error" in message: - raise RuntimeError(f"worker request failed: {redact_diagnostic(_rpc_error_text(message['error']))}") - result = message.get("result") - if not isinstance(result, Mapping): - raise RuntimeError("worker returned a malformed result payload") - return result - + finally: + stderr.cancel() + with contextlib.suppress(asyncio.CancelledError): + await stderr -async def codex_app_server_list_models( - argv: Sequence[str], - *, - env: Mapping[str, str], - cwd: str, - timeout: float | None = None, -) -> list[Any]: - """``initialize`` → ``initialized`` → paginated ``model/list`` over stdio. - ``argv`` must already include any ``-c`` config overrides and end with - ``app-server``. Hidden models are excluded from the selectable catalog. - Raises ``RuntimeError`` (process reaped) on timeout, RPC errors, or a - malformed payload; propagates ``CancelledError`` after killing the worker. - """ +async def codex_app_server_list_models(argv: Sequence[str], *, env: Mapping[str, str], cwd: str) -> list[Any]: + """Read visible pages using the existing Codex stdio request helpers.""" + entries: list[Any] = [] kwargs: dict[str, Any] = {} if sys.platform == "win32": kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP @@ -417,52 +154,37 @@ async def codex_app_server_list_models( stderr=asyncio.subprocess.DEVNULL, env=dict(env), cwd=cwd, - limit=DISCOVERY_STDIO_LIMIT, + limit=READ_LIMIT, **kwargs, ) - entries: list[Any] = [] - - async def _exchange() -> None: - await rpc_send( - proc, - {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"clientInfo": CLIENT_INFO}}, - ) - await rpc_read_response(proc, 1) - await rpc_send(proc, {"jsonrpc": "2.0", "method": "initialized", "params": {}}) - cursor: str | None = None - seen_cursors: set[str] = set() + try: + # Reuse the existing Codex stdio helpers rather than adding a transport. + await _send(proc, {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"clientInfo": CLIENT_INFO}}) + await _read_response(proc, 1) + await _send(proc, {"jsonrpc": "2.0", "method": "initialized", "params": {}}) + cursor = None + seen_cursors = set() for page in range(MODEL_LIST_MAX_PAGES): - params: dict[str, Any] = {"limit": MODEL_LIST_PAGE_LIMIT, "includeHidden": False} + params: dict[str, Any] = {"limit": 100, "includeHidden": False} if cursor: params["cursor"] = cursor request_id = page + 2 - await rpc_send(proc, {"jsonrpc": "2.0", "id": request_id, "method": "model/list", "params": params}) - result = await rpc_read_response(proc, request_id) - data = result.get("data") - if not isinstance(data, list): + await _send(proc, {"jsonrpc": "2.0", "id": request_id, "method": "model/list", "params": params}) + result = await _read_response(proc, request_id) + if not isinstance(result.get("data"), list): raise RuntimeError("Codex model/list returned missing or malformed data") - entries.extend(data) - next_cursor = result.get("nextCursor") - if next_cursor is not None and not isinstance(next_cursor, str): + entries.extend(result["data"]) + cursor = result.get("nextCursor") + if cursor is None or cursor == "": + return entries + if not isinstance(cursor, str): raise RuntimeError("Codex model/list returned a malformed nextCursor") - cursor = next_cursor if isinstance(next_cursor, str) and next_cursor else None - if cursor is None: - break if cursor in seen_cursors: - raise RuntimeError("model/list did not finish paginating: repeated cursor") + raise RuntimeError("Codex model/list repeated a pagination cursor") seen_cursors.add(cursor) - else: - raise RuntimeError(f"model/list did not finish paginating within {MODEL_LIST_MAX_PAGES} pages") - with contextlib.suppress(BrokenPipeError, ConnectionResetError, OSError): - assert proc.stdin is not None - proc.stdin.close() - await proc.wait() - - try: - await asyncio.wait_for(_exchange(), timeout=timeout) - except TimeoutError: - await reap_subprocess(proc) - raise RuntimeError(f"model/list did not finish within {timeout}s") from None + raise RuntimeError("Codex model/list exceeded the page limit") + except Exception as exc: + raise RuntimeError(f"Codex model discovery failed: {redact_diagnostic(str(exc))}") from None finally: cleanup = asyncio.create_task(reap_subprocess(proc, timeout=2.0)) try: @@ -470,4 +192,3 @@ async def _exchange() -> None: except asyncio.CancelledError: await cleanup raise - return entries diff --git a/src/agent_bridge/registry.py b/src/agent_bridge/registry.py index a3cc303..3e88a16 100644 --- a/src/agent_bridge/registry.py +++ b/src/agent_bridge/registry.py @@ -615,6 +615,9 @@ async def list_models(self, agent: str, cwd: str, *, refresh: bool = False) -> d raise ValueError("cwd must be an existing absolute project directory") resolved = str(path.resolve()) key = (agent, resolved) + previous = self._model_cache.get(key) + if not refresh and previous and time.monotonic() - previous[0] < self.config.models.cache_sec: + return {**copy.deepcopy(previous[1]), "cached": True} lock = self._model_locks.setdefault(agent, asyncio.Lock()) try: async with asyncio.timeout(self.config.models.timeout_sec), lock: diff --git a/src/agent_bridge/server.py b/src/agent_bridge/server.py index 5605aba..85424f7 100644 --- a/src/agent_bridge/server.py +++ b/src/agent_bridge/server.py @@ -104,7 +104,7 @@ async def list_agents(ctx: Context) -> dict[str, Any]: @mcp.tool(annotations=READ_ONLY) async def list_models(ctx: Context, agent: str, cwd: str, refresh: bool = False) -> dict[str, Any]: - """Discover the selected worker's advertised models. cwd must be this conversation's absolute project directory. Supports Kimi/DSH through a short-lived ACP session, Antigravity through `agy models`, and Codex through app-server model/list. Does not prompt a model or modify active sessions. Results are cached per worker/project for one hour by default ([models] cache_sec); refresh bypasses the cache. supported_efforts contains worker-native values only for models whose levels are known; null means unknown. Catalog presence does not prove account quota or successful invocation. Discovery may initialize a worker-owned session and contact its service. Disabled inside worker-inherited Bridge instances.""" + """Discover the selected worker's advertised models. cwd must be this conversation's absolute project directory. Supports Kimi/DSH through a short-lived ACP session, Antigravity through `agy models`, and Codex through app-server model/list. Does not prompt a model or modify active sessions. Results are cached per worker/project for one hour by default ([models] cache_sec); refresh bypasses the cache. supported_efforts contains worker-native values only for models whose levels are known; null means unknown. Defaults describe the query session, not a later dispatch selection. Catalog presence does not prove account quota or successful invocation. Discovery may initialize a worker-owned session and contact its service. Disabled inside worker-inherited Bridge instances.""" try: return {"ok": True, **await _registry(ctx).list_models(agent, cwd, refresh=refresh)} except Exception as exc: diff --git a/tests/test_model_catalog.py b/tests/test_model_catalog.py index 1af50dc..80ea3de 100644 --- a/tests/test_model_catalog.py +++ b/tests/test_model_catalog.py @@ -117,6 +117,19 @@ async def test_deadline_includes_waiting_for_worker_lock(catalog_registry, tmp_p assert calls == [] +async def test_fresh_cache_does_not_wait_for_discovery_lock(catalog_registry, tmp_path): + registry, calls = catalog_registry + await registry.list_models("kimi", str(tmp_path)) + registry.config.models.timeout_sec = 0.01 + async with registry._model_locks["kimi"]: + cached = await registry.list_models("kimi", str(tmp_path)) + assert cached["cached"] + cached["models"][0]["id"] = "caller mutation" + again = await registry.list_models("kimi", str(tmp_path)) + assert again["models"][0]["id"] == "small" + assert len(calls) == 1 + + async def test_list_agents_reports_capability_without_discovery(catalog_registry, monkeypatch): registry, calls = catalog_registry registry.config.quota.enabled = False diff --git a/tests/test_model_discovery_exec.py b/tests/test_model_discovery_exec.py index aa64b11..64accb2 100644 --- a/tests/test_model_discovery_exec.py +++ b/tests/test_model_discovery_exec.py @@ -4,9 +4,8 @@ import json import os import sys -import time -from pathlib import Path +import psutil import pytest from agent_bridge import model_discovery_exec @@ -15,18 +14,19 @@ from agent_bridge.config import AgentConfig, EnvConfig from agent_bridge.model_discovery_exec import ( codex_app_server_list_models, - normalize_model_entry, normalize_model_list, parse_agy_models_output, - run_bounded_command, ) -FAKE_AGY = r''' +FAKE_AGY = r""" import json import os import sys import time +pid_file = os.environ.get("FAKE_AGY_PID") +if pid_file: + with open(pid_file,"w") as f:f.write(str(os.getpid())) mode = os.environ.get("FAKE_AGY_MODE", "text") if mode == "exit3": @@ -38,24 +38,8 @@ time.sleep(30) sys.exit(0) -if mode == "json": - print(json.dumps({ - "models": [ - {"id": "gemini-3-pro", "name": "Gemini 3 Pro", "isDefault": True, - "supportedEfforts": ["low", "high"]}, - {"id": "claude-x", "name": "Claude X", "isDefault": False, - "supportedEfforts": []}, - {"name": "no id here"}, - ], - "defaultModel": "gemini-3-pro", - })) - sys.exit(0) - -print("\x1b[32m gemini-3.8-flash-medium \x1b[0m") -print("claude-sonnet-4-5 (default)") -print("2) gemini-3-pro") -print("not a model line with spaces") -print("---") +print("gemini-4-argon-low\tGemini 4 Argon (Low)") +print("claude-sonnet-5-5-high\tClaude Sonnet 5.5 (High)") report = os.environ.get("FAKE_AGY_REPORT") if report: with open(report, "w", encoding="utf-8") as fh: @@ -66,9 +50,9 @@ "worker_context": os.environ.get("AGENT_BRIDGE_PARENT_CONTEXT"), }, fh) sys.exit(0) -''' +""" -FAKE_CODEX = r''' +FAKE_CODEX = r""" import json import os import sys @@ -99,6 +83,9 @@ def emit(payload): continue msg = json.loads(line) requests.append(msg) + if report: + with open(report, "w", encoding="utf-8") as fh: + json.dump({"argv": argv, "cwd": os.getcwd(), "requests": requests}, fh) method = msg.get("method") if method == "initialize": emit({"id": msg["id"], "result": {"userAgent": "fake-codex"}}) @@ -143,368 +130,176 @@ def emit(payload): with open(report, "w", encoding="utf-8") as fh: json.dump({"argv": argv, "cwd": os.getcwd(), "requests": requests}, fh) sys.exit(0) -''' - - -def _write_script(tmp_path: Path, name: str, source: str) -> Path: - script = tmp_path / name - script.write_text(source, encoding="utf-8") - return script +""" -def _agy_adapter(home: Path, command: list[str], *, agent_env=None, env_config=None) -> AgyAdapter: - agent = AgentConfig(name="agy", protocol="antigravity", command=command, env=agent_env or {}) - return AgyAdapter(agent, home, env_config) +def script_file(tmp_path, name, source): + path = tmp_path / name + path.write_text(source, encoding="utf-8") + return path -def _codex_adapter(home: Path, command: list[str], *, overrides=None) -> CodexAdapter: - agent = AgentConfig( - name="codex", - protocol="codex", - command=command, - config_overrides=overrides or [], +def agy_adapter(tmp_path, script, env=None, env_config=None): + return AgyAdapter( + AgentConfig(name="antigravity", protocol="agy", command=[sys.executable, str(script)], env=env or {}), + tmp_path / "home", + env_config, ) - return CodexAdapter(agent, home) -# --- pure helpers ------------------------------------------------------------- - - -def test_normalize_model_entry_verbatim_id_and_raw_efforts(): - entry = normalize_model_entry( - { - "id": "gpt-6.1-sol", - "displayName": "GPT-6.1 Sol", - "isDefault": True, - "supportedReasoningEfforts": [ - {"reasoningEffort": "medium", "description": "balances"}, - {"reasoningEffort": "xhigh"}, - "raw-string-effort", - {"no_effort_key": True}, - ], - } +def codex_adapter(tmp_path, monkeypatch, script, overrides=None): + monkeypatch.setattr( + "agent_bridge.adapters.codex.resolve_codex_command", lambda *args, **kwargs: [sys.executable, str(script)] ) - assert entry == { - "id": "gpt-6.1-sol", - "name": "GPT-6.1 Sol", - "is_default": True, - "supported_efforts": ["medium", "xhigh", "raw-string-effort"], - } - # Underscore spellings and missing optionals. - entry = normalize_model_entry({"id": "m", "name": " ", "is_default": False}) - assert entry == {"id": "m", "name": None, "is_default": False, "supported_efforts": None} - # id must be a non-empty string; nothing is invented. - assert normalize_model_entry({"displayName": "no id"}) is None - assert normalize_model_entry("not-a-mapping") is None - assert normalize_model_entry({"id": " "}) is None - - -def test_normalize_model_list_dedupes_and_warns(): - models, default_id, warnings = normalize_model_list( - [ - {"id": "a", "isDefault": True}, - {"id": "a", "isDefault": False}, - {"id": "b", "isDefault": True}, - {"oops": 1}, - "garbage", - ] + return CodexAdapter( + AgentConfig(name="codex", protocol="codex", command=["unused"], config_overrides=overrides or []), + tmp_path / "home", ) - assert [m["id"] for m in models] == ["a", "b"] - assert default_id == "a" - assert any("2 default models" in w for w in warnings) - assert any("skipped 2 malformed" in w for w in warnings) -def test_parse_agy_models_output_json_payload(): - models, default_id, warnings = parse_agy_models_output( - json.dumps( - { - "models": [ - {"id": "gemini-3-pro", "name": "Gemini 3 Pro", "isDefault": True, - "supportedEfforts": ["low", "high"]}, - {"id": "claude-x", "supportedEfforts": []}, - ] - } - ) - ) - assert [m["id"] for m in models] == ["gemini-3-pro", "claude-x"] - assert models[0]["supported_efforts"] == ["low", "high"] - assert models[1]["supported_efforts"] == [] - assert default_id == "gemini-3-pro" - assert not any("text output" in w for w in warnings) - - -def test_parse_agy_models_output_text_bullets_default_ansi_junk(): - stdout = ( - "\x1b[32m gemini-3.8-flash-medium \x1b[0m\n" - "claude-sonnet-4-5 (default)\n" - "2) gemini-3-pro\n" - "not a model line with spaces\n" - "---\n" - ) - models, default_id, warnings = parse_agy_models_output(stdout) - assert [m["id"] for m in models] == ["gemini-3.8-flash-medium", "claude-sonnet-4-5", "gemini-3-pro"] - assert all(m["name"] is None and m["supported_efforts"] is None for m in models) - assert [m["is_default"] for m in models] == [False, True, False] - assert default_id == "claude-sonnet-4-5" - assert any("ignored 1 line(s)" in w for w in warnings) - assert any("text output" in w for w in warnings) +def test_agy_verified_tsv_format(): + result = parse_agy_models_output("\x1b[32mmodel-a\tModel A\x1b[0m\nmodel-b\tModel B\n") + assert result == [ + {"id": "model-a", "name": "Model A", "is_default": False, "supported_efforts": None}, + {"id": "model-b", "name": "Model B", "is_default": False, "supported_efforts": None}, + ] -def test_parse_agy_models_output_empty_and_unrecognized(): - assert parse_agy_models_output("") == ([], None, ["agy models returned no output"]) - models, default_id, warnings = parse_agy_models_output('{"totally": "different"}') - assert models == [] and default_id is None - assert any("unrecognized JSON" in w for w in warnings) +@pytest.mark.parametrize( + "text", + [ + "ERROR", + "Unauthorized", + "Models:\nmodel-a\tModel A", + '{"models":[{"id":"a"}]}', + "- model-a", + "model-a (default)", + "", + "model-a\t", + "bad id\tName", + ], +) +def test_agy_unverified_formats_and_diagnostics_are_not_catalogs(text): + with pytest.raises(RuntimeError): + parse_agy_models_output(text) -# --- agy adapter -------------------------------------------------------------- +def test_codex_advertised_metadata_preserves_levels_and_unknowns(): + models, default, warnings = normalize_model_list( + [ + { + "id": "a", + "displayName": "A", + "isDefault": True, + "supportedReasoningEfforts": [{"reasoningEffort": "medium"}, {"reasoningEffort": "xhigh"}], + }, + {"id": "b", "supportedReasoningEfforts": []}, + {"id": "c", "supportedReasoningEfforts": [{"unexpected": "high"}]}, + {"id": "d"}, + ] + ) + assert default == "a" + assert [m["supported_efforts"] for m in models] == [["medium", "xhigh"], [], None, None] + assert models[0]["name"] == "A" + assert warnings -async def test_agy_list_models_text_success(bridge_home, tmp_path): - script = _write_script(tmp_path, "fake_agy.py", FAKE_AGY) +async def test_agy_adapter_env_cwd_and_catalog(tmp_path): + script = script_file(tmp_path, "agy.py", FAKE_AGY) report = tmp_path / "report.json" - adapter = _agy_adapter( - bridge_home, - [sys.executable, str(script)], - agent_env={"AGY_MARKER": "yes", "FAKE_AGY_REPORT": str(report)}, - ) - cwd = str(tmp_path / "work") - Path(cwd).mkdir() - result = await adapter.list_models(cwd) - assert result["source"] == "agy models" - assert [m["id"] for m in result["models"]] == [ - "gemini-3.8-flash-medium", - "claude-sonnet-4-5", - "gemini-3-pro", - ] - assert result["default_model"] == "claude-sonnet-4-5" - assert result["models"][0]["supported_efforts"] is None - evidence = json.loads(report.read_text(encoding="utf-8")) - assert evidence["cwd"] == cwd # discovery ran in the requested cwd - assert evidence["agent_env"] == "yes" # agent env block respected - assert evidence["worker_context"] == "worker" # nested-bridge marker set + adapter = agy_adapter(tmp_path, script, {"FAKE_AGY_REPORT": str(report), "AGY_MARKER": "yes"}) + result = await adapter.list_models(str(tmp_path)) + assert [m["id"] for m in result["models"]] == ["gemini-4-argon-low", "claude-sonnet-5-5-high"] + assert result["default_model"] is None and result["warnings"] == [] + data = json.loads(report.read_text()) + assert data["cwd"] == str(tmp_path) and data["agent_env"] == "yes" + assert data["worker_context"] == "worker" -async def test_agy_list_models_env_deny_strips_secret(bridge_home, tmp_path, monkeypatch): - monkeypatch.setenv("DISCOVERY_SECRET", "topsecret") - script = _write_script(tmp_path, "fake_agy.py", FAKE_AGY) +async def test_agy_respects_env_deny(tmp_path, monkeypatch): + monkeypatch.setenv("DISCOVERY_SECRET", "secret") + script = script_file(tmp_path, "agy.py", FAKE_AGY) report = tmp_path / "report.json" - adapter = _agy_adapter( - bridge_home, - [sys.executable, str(script)], - agent_env={"FAKE_AGY_REPORT": str(report)}, - env_config=EnvConfig(deny=["DISCOVERY_*"]), - ) - result = await adapter.list_models(str(tmp_path)) - assert [m["id"] for m in result["models"]] == [ - "gemini-3.8-flash-medium", - "claude-sonnet-4-5", - "gemini-3-pro", - ] - evidence = json.loads(report.read_text(encoding="utf-8")) - assert evidence["denied_present"] is False + adapter = agy_adapter(tmp_path, script, {"FAKE_AGY_REPORT": str(report)}, EnvConfig(deny=["DISCOVERY_*"])) + await adapter.list_models(str(tmp_path)) + assert not json.loads(report.read_text())["denied_present"] -async def test_agy_list_models_json_payload(bridge_home, tmp_path, monkeypatch): - script = _write_script(tmp_path, "fake_agy.py", FAKE_AGY) - monkeypatch.setenv("FAKE_AGY_MODE", "json") - adapter = _agy_adapter(bridge_home, [sys.executable, str(script)]) - result = await adapter.list_models(str(tmp_path)) - assert result["default_model"] == "gemini-3-pro" - by_id = {m["id"]: m for m in result["models"]} - assert by_id["gemini-3-pro"]["name"] == "Gemini 3 Pro" - assert by_id["gemini-3-pro"]["supported_efforts"] == ["low", "high"] - assert by_id["gemini-3-pro"]["is_default"] is True - assert by_id["claude-x"]["supported_efforts"] == [] - # The id-less entry was skipped with a warning, silently dropped otherwise. - assert any("malformed" in w for w in result["warnings"]) - - -async def test_agy_list_models_nonzero_exit_redacts_stderr(bridge_home, tmp_path, monkeypatch): - script = _write_script(tmp_path, "fake_agy.py", FAKE_AGY) - monkeypatch.setenv("FAKE_AGY_MODE", "exit3") - adapter = _agy_adapter(bridge_home, [sys.executable, str(script)]) - with pytest.raises(RuntimeError, match="exited with code 3") as caught: +async def test_agy_nonzero_exit_is_redacted_failure(tmp_path): + script = script_file(tmp_path, "agy.py", FAKE_AGY) + adapter = agy_adapter(tmp_path, script, {"FAKE_AGY_MODE": "exit3"}) + with pytest.raises(RuntimeError, match="exited with code 3") as exc: await adapter.list_models(str(tmp_path)) - joined = str(caught.value) - assert "sk-testsecret123" not in joined - assert "apikeyhunter2" not in joined + assert "sk-testsecret123" not in str(exc.value) + assert "apikeyhunter2" not in str(exc.value) -async def test_agy_list_models_timeout_reaps_worker(bridge_home, tmp_path, monkeypatch): - script = _write_script(tmp_path, "fake_agy.py", FAKE_AGY) - monkeypatch.setenv("FAKE_AGY_MODE", "hang") - adapter = _agy_adapter(bridge_home, [sys.executable, str(script)]) - started = time.monotonic() - with pytest.raises(TimeoutError): - await asyncio.wait_for(adapter.list_models(str(tmp_path)), timeout=0.3) - assert time.monotonic() - started < 10 +@pytest.mark.parametrize("timeout", [True, False]) +async def test_agy_timeout_or_cancel_reaps_child(tmp_path, monkeypatch, timeout): + script = script_file(tmp_path, "agy.py", FAKE_AGY) + adapter = agy_adapter(tmp_path, script, {"FAKE_AGY_MODE": "hang"}) + started = asyncio.Event() + children = [] + original = asyncio.create_subprocess_exec + async def spawn(*args, **kwargs): + proc = await original(*args, **kwargs) + children.append(proc) + started.set() + return proc -async def test_agy_list_models_cancellation_kills_worker(bridge_home, tmp_path, monkeypatch): - script = _write_script(tmp_path, "fake_agy.py", FAKE_AGY) - monkeypatch.setenv("FAKE_AGY_MODE", "hang") - adapter = _agy_adapter(bridge_home, [sys.executable, str(script)]) + monkeypatch.setattr(asyncio, "create_subprocess_exec", spawn) task = asyncio.create_task(adapter.list_models(str(tmp_path))) - await asyncio.sleep(0.2) - task.cancel() - with pytest.raises(asyncio.CancelledError): - await asyncio.wait_for(task, timeout=10) - - -# --- codex adapter ------------------------------------------------------------ - - -def _patch_codex_command(monkeypatch, script: Path): - monkeypatch.setattr( - "agent_bridge.adapters.codex.resolve_codex_command", - lambda command, fallbacks=None, *, env=None: [sys.executable, str(script)], - ) - - -async def test_codex_list_models_pagination_overrides_and_cwd( - bridge_home, tmp_path, monkeypatch -): - script = _write_script(tmp_path, "fake_codex.py", FAKE_CODEX) - _patch_codex_command(monkeypatch, script) - report = tmp_path / "codex_report.json" + await asyncio.wait_for(started.wait(), timeout=5) + if timeout: + with pytest.raises(TimeoutError): + await asyncio.wait_for(task, 0.01) + else: + task.cancel() + with pytest.raises(asyncio.CancelledError): + await task + assert not psutil.pid_exists(children[0].pid) + + +async def test_agy_output_limit_fails_and_reaps(tmp_path, monkeypatch): + script = script_file(tmp_path, "agy.py", 'print("x"*10000)') + monkeypatch.setattr(model_discovery_exec, "AGY_OUTPUT_LIMIT", 100) + with pytest.raises(RuntimeError, match="output limit"): + await agy_adapter(tmp_path, script).list_models(str(tmp_path)) + + +async def test_codex_pages_overrides_and_observed_defaults(tmp_path, monkeypatch): + script = script_file(tmp_path, "codex.py", FAKE_CODEX) + report = tmp_path / "report.json" monkeypatch.setenv("FAKE_CODEX_REPORT", str(report)) - override = 'model_providers.custom.base_url="http://localhost:9999/v1"' - adapter = _codex_adapter(bridge_home, ["codex"], overrides=[override]) - cwd = str(tmp_path / "work") - Path(cwd).mkdir() - result = await adapter.list_models(cwd) - assert result["source"] == "codex app-server model/list" - by_id = {m["id"]: m for m in result["models"]} - assert set(by_id) == {"gpt-a", "gpt-b"} - # Verbatim worker-native values: ids, display names, raw effort labels. - assert by_id["gpt-a"]["name"] == "A" - assert by_id["gpt-a"]["supported_efforts"] == ["medium", "xhigh"] - assert by_id["gpt-a"]["is_default"] is True - assert by_id["gpt-b"]["supported_efforts"] == ["high"] + override = 'model_provider="custom"' + result = await codex_adapter(tmp_path, monkeypatch, script, [override]).list_models(str(tmp_path)) + assert [m["id"] for m in result["models"]] == ["gpt-a", "gpt-b"] assert result["default_model"] == "gpt-a" - evidence = json.loads(report.read_text(encoding="utf-8")) - argv = evidence["argv"] - assert argv[-1] == "app-server" - # config_overrides are forwarded as -c before the subcommand. - assert argv[argv.index("-c") + 1] == override - assert evidence["cwd"] == cwd - list_requests = [r for r in evidence["requests"] if r.get("method") == "model/list"] - assert len(list_requests) == 2 # paginated via nextCursor - assert list_requests[0]["params"]["includeHidden"] is False - assert list_requests[1]["params"]["cursor"] == "page2" - - -async def test_codex_list_models_malformed_entries_warn(bridge_home, tmp_path, monkeypatch): - script = _write_script(tmp_path, "fake_codex.py", FAKE_CODEX) - _patch_codex_command(monkeypatch, script) - monkeypatch.setenv("FAKE_CODEX_MODELS_MODE", "malformed") - adapter = _codex_adapter(bridge_home, ["codex"]) - result = await adapter.list_models(str(tmp_path)) - assert [m["id"] for m in result["models"]] == ["gpt-good"] - assert result["default_model"] == "gpt-good" - assert any("skipped 2 malformed" in w for w in result["warnings"]) - - -async def test_codex_list_models_rpc_error_redacted(bridge_home, tmp_path, monkeypatch): - script = _write_script(tmp_path, "fake_codex.py", FAKE_CODEX) - _patch_codex_command(monkeypatch, script) - monkeypatch.setenv("FAKE_CODEX_MODELS_MODE", "rpc_error") - adapter = _codex_adapter(bridge_home, ["codex"]) - with pytest.raises(RuntimeError, match="request failed") as caught: - await adapter.list_models(str(tmp_path)) - joined = str(caught.value) - assert "sk-leakedtok987" not in joined - - -async def test_codex_list_models_timeout_reaps_worker(bridge_home, tmp_path, monkeypatch): - script = _write_script(tmp_path, "fake_codex.py", FAKE_CODEX) - _patch_codex_command(monkeypatch, script) - monkeypatch.setenv("FAKE_CODEX_MODELS_MODE", "hang") - adapter = _codex_adapter(bridge_home, ["codex"]) - started = time.monotonic() - with pytest.raises(TimeoutError): - await asyncio.wait_for(adapter.list_models(str(tmp_path)), timeout=0.3) - assert time.monotonic() - started < 10 - - -async def test_codex_list_models_command_not_found_raises( - bridge_home, tmp_path, monkeypatch -): - def _raise(command, fallbacks=None, *, env=None): - raise FileNotFoundError("codex not found") - - monkeypatch.setattr("agent_bridge.adapters.codex.resolve_codex_command", _raise) - adapter = _codex_adapter(bridge_home, ["codex"]) - with pytest.raises(FileNotFoundError, match="codex not found"): - await adapter.list_models(str(tmp_path)) - - -# --- run_bounded_command ------------------------------------------------------ - - -async def test_run_bounded_command_truncates_extra_output(tmp_path, monkeypatch): - script = _write_script( - tmp_path, - "chatty.py", - "for i in range(10):\n print(f'line-{i}')\n", - ) - monkeypatch.setattr(model_discovery_exec, "DISCOVERY_LINE_LIMIT", 5) - returncode, stdout, _stderr, warnings = await run_bounded_command( - [sys.executable, str(script)], env={"PATH": os.environ.get("PATH", "")}, cwd=str(tmp_path) - ) - assert returncode == 0 - assert stdout.splitlines() == [f"line-{i}" for i in range(5)] - assert warnings == ["worker output exceeded discovery bounds; extra output was discarded"] - - -async def test_codex_app_server_cursor_loop_is_bounded(tmp_path): - script = _write_script(tmp_path, "fake_codex.py", FAKE_CODEX) - with pytest.raises(RuntimeError, match="did not finish paginating"): - await codex_app_server_list_models( - [sys.executable, str(script), "app-server"], - env={**os.environ, "FAKE_CODEX_MODELS_MODE": "cursor_loop"}, - cwd=str(tmp_path), - timeout=5.0, - ) - - -def test_agy_tab_separated_catalog_matches_live_format(): - models, default, warnings = parse_agy_models_output( - "gemini-4-argon-low\tGemini 4 Argon (Low)\n" - "claude-sonnet-5-5-high\tClaude Sonnet 5.5 (High)\n" - ) - assert [(m["id"], m["name"]) for m in models] == [ - ("gemini-4-argon-low", "Gemini 4 Argon (Low)"), - ("claude-sonnet-5-5-high", "Claude Sonnet 5.5 (High)"), - ] - assert default is None - assert all(m["supported_efforts"] is None for m in models) - assert not any("ignored" in warning for warning in warnings) - - -def test_agy_top_level_default_keeps_row_flag_consistent(): - models, default, _ = parse_agy_models_output(json.dumps({ - "defaultModel": "model-a", "models": [{"id": "model-a"}, {"id": "model-b"}], - })) - assert default == "model-a" - assert [m["is_default"] for m in models] == [True, False] - - -@pytest.mark.parametrize("mode", ["bad_page", "bad_cursor"]) -async def test_codex_rejects_malformed_page(tmp_path, mode): - script = _write_script(tmp_path, "fake_codex.py", FAKE_CODEX) - with pytest.raises(RuntimeError, match="malformed"): + assert result["models"][0]["supported_efforts"] == ["medium", "xhigh"] + data = json.loads(report.read_text()) + assert data["argv"] == ["-c", override, "app-server"] + assert data["cwd"] == str(tmp_path) + requests = [r for r in data["requests"] if r.get("method") == "model/list"] + assert len(requests) == 2 and requests[1]["params"]["cursor"] == "page2" + assert requests[0]["params"]["includeHidden"] is False + + +@pytest.mark.parametrize( + "mode,match", + [ + ("rpc_error", "discovery failed"), + ("bad_page", "malformed"), + ("bad_cursor", "malformed"), + ("cursor_loop", "repeated"), + ], +) +async def test_codex_invalid_responses_fail(tmp_path, mode, match): + script = script_file(tmp_path, "codex.py", FAKE_CODEX) + with pytest.raises(RuntimeError, match=match) as exc: await codex_app_server_list_models( [sys.executable, str(script), "app-server"], env={**os.environ, "FAKE_CODEX_MODELS_MODE": mode}, - cwd=str(tmp_path), timeout=5, + cwd=str(tmp_path), ) - - -async def test_agy_unknown_output_is_failure(bridge_home, tmp_path): - script = _write_script(tmp_path, "fake_agy.py", 'print("unexpected response without model IDs")') - adapter = _agy_adapter(bridge_home, [sys.executable, str(script)]) - with pytest.raises(RuntimeError, match="no model slugs recognized"): - await adapter.list_models(str(tmp_path)) + assert "sk-leakedtok987" not in str(exc.value) From 6aecb23d78eb2f54010d2d89925b32b72dc5cb3c Mon Sep 17 00:00:00 2001 From: "edge.xyz" <32131214+edgexyz@users.noreply.github.com> Date: Fri, 16 Oct 2026 22:24:26 -0400 Subject: [PATCH 4/6] Simplify model discovery documentation and tool descriptions --- ORCHESTRATION.md | 2 +- ORCHESTRATION.zh-CN.md | 2 +- README.md | 24 +++------ SETUP.md | 62 +++++++++++++----------- agents.toml | 2 +- scripts/smoke_models.py | 2 +- skills/agent-bridge/SKILL.md | 2 +- src/agent_bridge/adapters/acp.py | 14 ++---- src/agent_bridge/adapters/antigravity.py | 6 +-- src/agent_bridge/adapters/base.py | 2 +- src/agent_bridge/adapters/codex.py | 8 +-- src/agent_bridge/agents.toml | 2 +- src/agent_bridge/config.py | 2 +- src/agent_bridge/model_catalog.py | 6 +-- src/agent_bridge/model_discovery_exec.py | 5 +- src/agent_bridge/registry.py | 4 +- src/agent_bridge/server.py | 8 ++- 17 files changed, 62 insertions(+), 91 deletions(-) diff --git a/ORCHESTRATION.md b/ORCHESTRATION.md index 3457565..ff7c217 100644 --- a/ORCHESTRATION.md +++ b/ORCHESTRATION.md @@ -49,7 +49,7 @@ In `auto`/`eager`, tell the user after the fact. In `manual`, their explicit req ## How to dispatch -Before selecting a model, use supported workers' `list_models(agent,cwd)`; see SETUP.md for caching, effort levels and session side effects. +When `dispatch_enabled=true`, use supported workers' `list_models(agent,cwd)` before choosing a model. See SETUP.md for usage details. 1. `list_agents`. Read `coordinator.mode` / `instructions` / `dispatch_enabled` and `env.proxy` / `env.warnings`. A null proxy on a direct network is normal; if a worker fails with connect errors on a proxied machine, fix `[env.proxy]` instead of retrying. 2. `dispatch_task` with `cwd` = **this conversation's project folder** (absolute). Never the Agent Bridge install path (unless the user is editing Bridge). 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. diff --git a/ORCHESTRATION.zh-CN.md b/ORCHESTRATION.zh-CN.md index c2cbf24..221cef6 100644 --- a/ORCHESTRATION.zh-CN.md +++ b/ORCHESTRATION.zh-CN.md @@ -87,4 +87,4 @@ ACP 失败保留字符串 `error`,并可附带 `failure`。`failure.prompt_may ## 按需查询模型目录 -明确指定模型前,若 `list_agents.model_discovery.supported=true`,调用 `list_models(agent=..., cwd=...)` 获取 worker 公布的模型 ID。首批支持 Kimi、DSH、Antigravity 和 Codex。默认缓存一小时,过期后仅在下一次查询时刷新;配置或账号变更后可用 `refresh=true`。`supported_efforts` 是 worker 原生档位,未知模型的档位保持 null,`dispatch_task.effort` 的五档输入与映射不变。目录不保证账号额度或调用成功。查询不发送模型提示、不改变活动会话;ACP 可能留下空的 worker 会话。worker 继承实例不能查询模型目录。 +当 `dispatch_enabled=true` 时,选择模型前调用支持此功能的 worker 的 `list_models(agent,cwd)`;字段、缓存、effort 映射和会话行为见 SETUP.md。 diff --git a/README.md b/README.md index 6e317ee..51ead8a 100644 --- a/README.md +++ b/README.md @@ -115,24 +115,12 @@ Close coordinators that are holding Bridge, then `agent-bridge upgrade`, then re | `list_sessions` | Known sessions | | `end_session` | Shut down a worker process | -Call `list_models(agent="kimi", cwd="/absolute/project")` before selecting a -model. Initial support covers Kimi and DSH through shared ACP discovery, -Antigravity through `agy models`, and Codex through app-server `model/list`. -`list_agents` reports `model_discovery` capability without querying catalogs. -Discovery never sends a model prompt or changes an active work session; ACP -discovery creates a separate worker-owned session that may remain in its history. -Catalog entries are advertised options, not guarantees of account access or quota. -`default_model` is the query-side advertised default, not the coordinator's -selection for a later dispatch. Codex discovery uses app-server configuration; -exec retains its existing user-config isolation. -`supported_efforts` contains worker-native levels only when known for that model; -`null` means unknown. `dispatch_task.effort` still accepts only -`off|low|medium|high|max` and applies the worker's mapping. -Catalogs are cached in memory per worker/project for one hour, refreshed only on -the next query after expiry. `refresh=true` bypasses the cache. Configure -`[models] cache_sec` / `timeout_sec` in `agents.toml` (defaults 3600 / 30). -Failed refreshes return errors without overwriting the last successful cache; -worker-inherited instances cannot launch discovery processes. +Call `list_models(agent="kimi", cwd="/absolute/project")` to discover a worker's +advertised models before choosing one. Supported workers are Kimi, DSH, +Antigravity, and Codex; check `list_agents.model_discovery` for support. +Results are cached per worker/project for one hour by default. Use +`refresh=true` for an immediate update. See [Model discovery](SETUP.md#model-discovery) +for result fields, configuration, and session behavior. `get_result` returns up to 60,000 characters per call. Continue with `next_cursor` while `has_more` is true. Detailed work events remain available diff --git a/SETUP.md b/SETUP.md index cdb6632..fe146a2 100644 --- a/SETUP.md +++ b/SETUP.md @@ -456,41 +456,45 @@ list_models(agent="kimi", cwd="/absolute/project") list_models(agent="antigravity", cwd="/absolute/project", refresh=true) ``` -Initial support: Kimi and DSH share the ACP `initialize` / `session/new` -discovery path; AGY uses `models`; Codex uses app-server `model/list` with -pagination. Claude and the remaining workers are not enabled in this release. -DSH discovery attempts the same ACP handshake for native and legacy launchers; -only a session that advertises no catalog receives an unavailable warning. -Handshake failures remain errors rather than being reported as unsupported. -Discovery does not send a model prompt, configure model/effort, change active -sessions, or register a Bridge task. ACP may create an empty session in the -worker's own history; all discovery processes are closed after the lookup, and -permission requests during ACP discovery are denied. - -Results contain `models` (`id`, `name`, `is_default`, `supported_efforts`), -`default_model`, `source`, `fetched_at`, `cached`, and `warnings`. -Defaults and effort options describe what the query session advertises. They do -not predict the coordinator's later model selection. In particular, Codex -app-server reads its existing configuration while exec intentionally ignores -user config; discovery neither changes that behavior nor switches CODEX_HOME. -AGY parsing supports its verified tab-separated ID/name output and rejects -unrecognized output rather than guessing model IDs. -`availability="advertised"` / `invocation_verified=false` distinguish a catalog -from a successful model request. Unknown defaults and model-specific effort -levels remain null. Effort levels are worker-native, not necessarily accepted -as `dispatch_task.effort` (whose vocabulary remains unchanged). +Supported workers and catalog sources: + +| Worker | Source | +| --- | --- | +| Kimi, DSH | ACP `initialize` / `session/new` | +| Antigravity | `agy models` | +| Codex | app-server `model/list`, using app-server configuration and the worker's `config_overrides` | + +Discovery uses a separate worker process, closed after the lookup. ACP discovery +denies permission requests and may leave an empty session in worker history. +`list_models` is available when `coordinator.dispatch_enabled=true`. + +Result fields: + +| Field | Meaning | +| --- | --- | +| `models` | Advertised choices with `id`, `name`, `is_default`, and `supported_efforts` | +| `default_model` | Default model ID reported by the query session; `null` when unknown | +| `supported_efforts` | Worker-native effort levels for a model; `null` means unknown, `[]` means explicitly no levels | +| `source` | Worker interface used for discovery | +| `agent`, `cwd` | Worker name and resolved project directory | +| `fetched_at`, `cached` | Fetch timestamp and whether this response came from cache | +| `availability="advertised"`, `invocation_verified=false` | Results describe the worker's catalog; successful model invocation is a separate check | +| `effort_vocabulary="worker_native"` | Effort names use the worker's vocabulary | +| `warnings` | Discovery diagnostics | + +For dispatch, pass `off|low|medium|high|max` to `dispatch_task.effort`; +the adapter maps that value to the worker's effort settings. ```toml [models] -cache_sec = 3600 # one hour; 0 disables reuse; no background polling +cache_sec = 3600 # one hour; 0 disables reuse timeout_sec = 30 # complete lookup deadline, including worker initialization ``` -The cache is per worker and project, in memory for this Bridge instance. Use -`refresh=true` after changing accounts, worker configuration, or model access. -A failed refresh is explicit and does not replace the last successful cache. -`list_agents` never launches model discovery, and worker-inherited Bridge -instances cannot call `list_models`. +The cache is per worker and project, in memory for this Bridge instance. +Expired entries are refreshed on the next query. Use `refresh=true` after +changing accounts, worker configuration, or model access. A failed refresh +returns an error and preserves the last successful cache entry. The coordinator can pin a worker model and thinking intensity on `dispatch_task`: diff --git a/agents.toml b/agents.toml index d22e5ef..0a7178e 100644 --- a/agents.toml +++ b/agents.toml @@ -254,7 +254,7 @@ command = ["codex"] revivable = true idle_unload_sec = 0 -# On-demand advertised model catalogs; no background polling. +# On-demand worker model catalogs. [models] cache_sec = 3600 timeout_sec = 30 diff --git a/scripts/smoke_models.py b/scripts/smoke_models.py index 3470168..937bef9 100644 --- a/scripts/smoke_models.py +++ b/scripts/smoke_models.py @@ -1,4 +1,4 @@ -"""Exercise list_models through MCP without model prompts or global Bridge state. +"""Smoke-test list_models through an isolated MCP server. uv run python scripts/smoke_models.py --cwd /absolute/project diff --git a/skills/agent-bridge/SKILL.md b/skills/agent-bridge/SKILL.md index 67fb660..3159946 100644 --- a/skills/agent-bridge/SKILL.md +++ b/skills/agent-bridge/SKILL.md @@ -21,7 +21,7 @@ Claude `status`: shared `5h`/`weekly` only. Before dispatch, even if `ok`, check ## Model discovery -Use `list_models(agent=..., cwd=...)` to obtain confirmed worker-advertised model IDs before explicitly selecting a model. `list_agents.model_discovery` reports whether discovery is supported; initial support is Kimi, DSH, Antigravity, and Codex. Supply this conversation's absolute project directory. Catalog presence does not prove account access or remaining quota. `supported_efforts` contains worker-native levels where known, while `dispatch_task.effort` still accepts `off|low|medium|high|max` and applies the adapter mapping. Unknown model-specific levels remain null. Catalogs are cached for one hour by default (`[models] cache_sec`); `refresh=true` forces an on-demand query. Discovery sends no prompt and does not configure an active session, but ACP may create an empty worker-owned session. Do not call `list_models` when `dispatch_enabled=false`. +When `coordinator.dispatch_enabled=true` and `list_agents.model_discovery.supported=true`, use `list_models(agent=..., cwd=...)` to obtain worker-advertised model IDs before explicitly selecting a model. Supply this conversation's absolute project directory; use `refresh=true` after changing accounts or worker configuration. `supported_efforts` reports worker-native levels; pass `off|low|medium|high|max` to `dispatch_task.effort` for adapter mapping. See SETUP.md for result fields, caching, and session behavior. ## Step 1 — dispatch, or do it yourself? diff --git a/src/agent_bridge/adapters/acp.py b/src/agent_bridge/adapters/acp.py index f9c11f4..9468839 100644 --- a/src/agent_bridge/adapters/acp.py +++ b/src/agent_bridge/adapters/acp.py @@ -689,7 +689,7 @@ def __init__(self) -> None: class _DiscoveryClient(_BridgeClient): - """Read-only handshake client, with no transcript or session bookkeeping.""" + """ACP discovery client that denies permission requests and discards updates.""" async def request_permission( self, @@ -711,11 +711,7 @@ def _discovery_field(value: Any, alias: str, field: str) -> Any: def _acp_model_catalog(response: Any, agent_name: str) -> dict[str, Any]: - """Read a fresh session snapshot without switching any of its settings. - - Thought-level choices belong to the current model only. Other models' - support remains unknown unless a separate session snapshot advertises it. - """ + """Extract model choices and current-model effort levels from an ACP snapshot.""" config_options = _discovery_field(response, "configOptions", "config_options") or [] if not isinstance(config_options, (list, tuple)): config_options = [] @@ -808,11 +804,9 @@ def __init__(self, agent: AgentConfig, home: Path, env_config=None) -> None: self._cursor_models_cache: dict[str, str] | None = None async def list_models(self, cwd: str) -> dict[str, Any]: - """Discover advertised choices in a private, permission-denying process. + """Read a catalog through a separate ACP initialize/session-new handshake. - Do not reuse ``_spawn``: it registers a worker and may select a model, - effort, or bypass mode. Discovery sends initialize and session/new only. - The caller owns the discovery deadline; cancellation still reaps here. + The caller controls the deadline; this method closes the process on exit. """ if self.agent.name == "dsh": cmd = resolve_dsh_command(self.agent.command, self.agent.fallback_commands) diff --git a/src/agent_bridge/adapters/antigravity.py b/src/agent_bridge/adapters/antigravity.py index 14e5e9b..a92001d 100644 --- a/src/agent_bridge/adapters/antigravity.py +++ b/src/agent_bridge/adapters/antigravity.py @@ -248,11 +248,7 @@ async def ensure_session(self, session: Session) -> None: return None async def list_models(self, cwd: str) -> dict[str, Any]: - """List models via ``agy models``; never dispatches a prompt. - - The resolved configured command plus ``models`` is run read-only in - ``cwd``. Failures propagate so a failed refresh cannot poison the cache. - """ + """Read the catalog from ``agy models`` in the project directory.""" source = "agy models" cmd = resolve_command(self.agent.command, self.agent.fallback_commands) env = self._enforce_spawn_env( diff --git a/src/agent_bridge/adapters/base.py b/src/agent_bridge/adapters/base.py index 6a1c541..841eb02 100644 --- a/src/agent_bridge/adapters/base.py +++ b/src/agent_bridge/adapters/base.py @@ -34,7 +34,7 @@ def can_revive(self) -> bool: return self.agent.revivable async def list_models(self, cwd: str) -> dict[str, Any]: - """Discover advertised models without prompting or changing a live session.""" + """Return the worker's advertised model catalog.""" raise NotImplementedError(f"model discovery is not implemented for {self.agent.name}") def _enforce_spawn_env(self, env: Mapping[str, str]) -> dict[str, str]: diff --git a/src/agent_bridge/adapters/codex.py b/src/agent_bridge/adapters/codex.py index 7651f76..4996ce7 100644 --- a/src/agent_bridge/adapters/codex.py +++ b/src/agent_bridge/adapters/codex.py @@ -53,13 +53,7 @@ async def ensure_session(self, session: Session) -> None: return None async def list_models(self, cwd: str) -> dict[str, Any]: - """List models via ``codex app-server`` JSON-RPC ``model/list``. - - Report the app-server catalog under its own configuration and the - worker's config_overrides. Its advertised default is not a promise - about a later exec turn, which intentionally ignores user config. - No prompt is sent and no configuration or Home path is changed. - """ + """Query app-server model/list using its configuration and config_overrides.""" source = "codex app-server model/list" env = self._enforce_spawn_env(self._worker_env()) command = await asyncio.to_thread( diff --git a/src/agent_bridge/agents.toml b/src/agent_bridge/agents.toml index d22e5ef..0a7178e 100644 --- a/src/agent_bridge/agents.toml +++ b/src/agent_bridge/agents.toml @@ -254,7 +254,7 @@ command = ["codex"] revivable = true idle_unload_sec = 0 -# On-demand advertised model catalogs; no background polling. +# On-demand worker model catalogs. [models] cache_sec = 3600 timeout_sec = 30 diff --git a/src/agent_bridge/config.py b/src/agent_bridge/config.py index 9f6a33d..0979900 100644 --- a/src/agent_bridge/config.py +++ b/src/agent_bridge/config.py @@ -152,7 +152,7 @@ class ServerConfig(BaseModel): class ModelsConfig(BaseModel): - """On-demand model discovery; expiry never schedules background work.""" + """Cache lifetime and timeout for on-demand model discovery.""" cache_sec: float = Field(default=3600.0, ge=0) timeout_sec: float = Field(default=30.0, gt=0) diff --git a/src/agent_bridge/model_catalog.py b/src/agent_bridge/model_catalog.py index 761a302..20e0685 100644 --- a/src/agent_bridge/model_catalog.py +++ b/src/agent_bridge/model_catalog.py @@ -1,4 +1,4 @@ -"""Normalized worker-advertised model catalogs, not invocation guarantees.""" +"""Worker-advertised model catalog schemas.""" from __future__ import annotations @@ -18,7 +18,7 @@ class ModelInfo(BaseModel): class ModelCatalog(BaseModel): source: str = Field(min_length=1) models: list[ModelInfo] - # Default advertised by the query, not an effective dispatch selection. + # Default model ID advertised by the query session. default_model: str | None = None warnings: list[str] = Field(default_factory=list) @@ -31,7 +31,7 @@ def unique_ids(self) -> ModelCatalog: def model_discovery_capability(cfg: AgentConfig) -> dict: - """Describe support without launching a process or querying a provider.""" + """Describe model-discovery support for a worker.""" supported = cfg.protocol in {"agy", "codex"} or ( cfg.protocol == "acp" and cfg.name in {"kimi", "dsh"} ) diff --git a/src/agent_bridge/model_discovery_exec.py b/src/agent_bridge/model_discovery_exec.py index 308b7a1..8b44bf3 100644 --- a/src/agent_bridge/model_discovery_exec.py +++ b/src/agent_bridge/model_discovery_exec.py @@ -1,4 +1,4 @@ -"""Worker-specific model catalog parsing; no inference requests.""" +"""Worker model catalog discovery and parsing.""" from __future__ import annotations @@ -64,7 +64,7 @@ def normalize_model_list(entries: Sequence[Any]) -> tuple[list[dict[str, Any]], def parse_agy_models_output(stdout: str) -> list[dict[str, Any]]: - """AGY 1.4 emits tab-separated ID and display name; other formats fail.""" + """Parse AGY tab-separated model ID and display name lines.""" models = [] seen = set() for line in _ANSI_RE.sub("", stdout).splitlines(): @@ -158,7 +158,6 @@ async def codex_app_server_list_models(argv: Sequence[str], *, env: Mapping[str, **kwargs, ) try: - # Reuse the existing Codex stdio helpers rather than adding a transport. await _send(proc, {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"clientInfo": CLIENT_INFO}}) await _read_response(proc, 1) await _send(proc, {"jsonrpc": "2.0", "method": "initialized", "params": {}}) diff --git a/src/agent_bridge/registry.py b/src/agent_bridge/registry.py index 3e88a16..86b4437 100644 --- a/src/agent_bridge/registry.py +++ b/src/agent_bridge/registry.py @@ -604,7 +604,7 @@ async def describe(cfg: AgentConfig) -> dict: return list(await asyncio.gather(*(describe(cfg) for cfg in self.config.agents.values()))) async def list_models(self, agent: str, cwd: str, *, refresh: bool = False) -> dict: - """Query a private discovery process; never reuse or persist a work session.""" + """Discover and cache a worker's model catalog.""" if not self.dispatch_enabled: raise RuntimeError("model discovery is disabled inside worker-inherited Bridge instances") cfg = self.config.get(agent) @@ -644,8 +644,6 @@ async def list_models(self, agent: str, cwd: str, *, refresh: bool = False) -> d self._model_cache[key] = (time.monotonic(), copy.deepcopy(result)) return result except Exception as exc: - # Failed refreshes are explicit; never mask errors with stale data or - # expose credentials in worker diagnostics. Cleanup runs before exit. raise RuntimeError(redact_diagnostic(str(exc)) or type(exc).__name__) from None async def _quota_for(self, cfg: AgentConfig, *, available: bool) -> dict: diff --git a/src/agent_bridge/server.py b/src/agent_bridge/server.py index 85424f7..ea6c888 100644 --- a/src/agent_bridge/server.py +++ b/src/agent_bridge/server.py @@ -65,10 +65,8 @@ async def lifespan(_server: MCPServer[Registry]) -> AsyncIterator[Registry]: "dispatch_enabled is true and the user states a lasting preference, " "persist it with set_preferences." " Use list_models(agent, cwd) to discover model IDs before choosing one; " - "list_agents.model_discovery describes support. Catalogs are worker-advertised, " - "not verified successful invocations. supported_efforts uses worker-native " - "vocabulary; dispatch_task still accepts only off/low/medium/high/max. " - "Model discovery is disabled in worker-inherited instances." + "list_agents.model_discovery describes support. See the tool description " + "for result semantics and eligibility." ) mcp = MCPServer[Registry]("agent-bridge", instructions=INSTRUCTIONS, lifespan=lifespan) @@ -104,7 +102,7 @@ async def list_agents(ctx: Context) -> dict[str, Any]: @mcp.tool(annotations=READ_ONLY) async def list_models(ctx: Context, agent: str, cwd: str, refresh: bool = False) -> dict[str, Any]: - """Discover the selected worker's advertised models. cwd must be this conversation's absolute project directory. Supports Kimi/DSH through a short-lived ACP session, Antigravity through `agy models`, and Codex through app-server model/list. Does not prompt a model or modify active sessions. Results are cached per worker/project for one hour by default ([models] cache_sec); refresh bypasses the cache. supported_efforts contains worker-native values only for models whose levels are known; null means unknown. Defaults describe the query session, not a later dispatch selection. Catalog presence does not prove account quota or successful invocation. Discovery may initialize a worker-owned session and contact its service. Disabled inside worker-inherited Bridge instances.""" + """List models advertised by a supported worker. Pass this conversation's absolute project directory as cwd. Results are cached per worker/project for one hour by default ([models] cache_sec); refresh=true requests an immediate update. default_model reports the query session's advertised default. supported_efforts reports worker-native levels: null means unknown, [] means explicitly no levels. For dispatch_task.effort, use off|low|medium|high|max for adapter mapping. Available when coordinator.dispatch_enabled=true.""" try: return {"ok": True, **await _registry(ctx).list_models(agent, cwd, refresh=refresh)} except Exception as exc: From eba4734011c19679b6363b03b5dd39146669ce83 Mon Sep 17 00:00:00 2001 From: "edge.xyz" <32131214+edgexyz@users.noreply.github.com> Date: Fri, 16 Oct 2026 22:44:01 -0400 Subject: [PATCH 5/6] Simplify model catalog parsing around worker contracts --- src/agent_bridge/adapters/acp.py | 66 +++-------- src/agent_bridge/adapters/codex.py | 13 +- src/agent_bridge/model_catalog.py | 9 +- src/agent_bridge/model_discovery_exec.py | 59 +++------- tests/test_model_discovery_acp.py | 144 +++++++++++------------ tests/test_model_discovery_exec.py | 27 +++-- 6 files changed, 125 insertions(+), 193 deletions(-) diff --git a/src/agent_bridge/adapters/acp.py b/src/agent_bridge/adapters/acp.py index 9468839..568a1e1 100644 --- a/src/agent_bridge/adapters/acp.py +++ b/src/agent_bridge/adapters/acp.py @@ -18,6 +18,7 @@ ClientCapabilities, DeniedOutcome, Implementation, + NewSessionResponse, PermissionOption, RequestPermissionResponse, ) @@ -704,67 +705,30 @@ async def session_update(self, session_id: str, update: Any, **kwargs: Any) -> N return None -def _discovery_field(value: Any, alias: str, field: str) -> Any: - if isinstance(value, dict): - return value.get(alias, value.get(field)) - return getattr(value, field, None) - - -def _acp_model_catalog(response: Any, agent_name: str) -> dict[str, Any]: +def _acp_model_catalog(response: NewSessionResponse, agent_name: str) -> dict[str, Any]: """Extract model choices and current-model effort levels from an ACP snapshot.""" - config_options = _discovery_field(response, "configOptions", "config_options") or [] - if not isinstance(config_options, (list, tuple)): - config_options = [] - options = list(config_options) + options = response.config_options or [] model_option_id = next( - ( - option["id"] - for item in options - if isinstance(option := _dump(item), dict) - and isinstance(option.get("id"), str) - and (option["id"] == "model" or option.get("category") == "model") - ), + (option.id for option in options if option.id == "model" or option.category == "model"), "model", ) current, _offered = config_option_values(options, model_option_id) - choices: dict[str, str | None] = {} def model_id(value: str) -> str: return (_dsh_native_model_label(value) or value) if agent_name == "dsh" else value - for value, name in _cursor_choices(options, model_option_id): - choices[model_id(value)] = name if name != value else None - state = _discovery_field(response, "models", "models") - current = current or _discovery_field(state, "currentModelId", "current_model_id") - default = model_id(current) if isinstance(current, str) and current else None - available = _discovery_field(state, "availableModels", "available_models") or [] - if isinstance(available, (list, tuple)): - for entry in available: - value = _discovery_field(entry, "modelId", "model_id") - name = _discovery_field(entry, "name", "name") - if isinstance(value, str) and value: - key = model_id(value) - if key not in choices or choices[key] is None: - choices[key] = name if isinstance(name, str) else None - if default is not None: - choices.setdefault(default, None) + default = model_id(current) if current else None + choices = [(model_id(value), name) for value, name in _cursor_choices(options, model_option_id)] efforts: list[str] | None = None - for item in options: - option = _dump(item) - if not isinstance(option, dict) or not isinstance(option.get("id"), str): - continue - option_id = option["id"] - if option.get("category") == "thought_level" or option_id in { - "thinking", "effort", "reasoning", "reasoning_effort", "thinkingEffort", + for option in options: + if option.category == "thought_level" or option.id in { + "thinking", "reasoning_effort", }: - _current_effort, offered = config_option_values(options, option_id) + _current_effort, offered = config_option_values(options, option.id) # A boolean switch describes thinking enablement, not effort levels. if offered and set(offered) != {"true", "false"}: - efforts = list(dict.fromkeys(offered)) + efforts = offered break - warnings = [] if choices else [ - "Model discovery is unavailable: the ACP session did not advertise a model catalog." - ] return { "source": "acp", "models": [ @@ -774,10 +738,12 @@ def model_id(value: str) -> str: "is_default": value == default, "supported_efforts": efforts if value == default else None, } - for value, name in choices.items() + for value, name in choices ], "default_model": default, - "warnings": warnings, + "warnings": [] if choices else [ + "Model discovery is unavailable: the ACP session did not advertise a model catalog." + ], } @@ -832,8 +798,6 @@ async def list_models(self, cwd: str) -> dict[str, Any]: **kwargs, ) proc = live.proc - if proc.stdin is None or proc.stdout is None: - raise RuntimeError(f"{self.agent.name} did not expose stdio") client = _DiscoveryClient("model-discovery", self.home, permission_policy="deny") live.conn = connect_to_agent(client, proc.stdin, proc.stdout) live.stderr_task = asyncio.create_task(self._drain_stderr(proc, "model-discovery", live)) diff --git a/src/agent_bridge/adapters/codex.py b/src/agent_bridge/adapters/codex.py index 4996ce7..7a656e8 100644 --- a/src/agent_bridge/adapters/codex.py +++ b/src/agent_bridge/adapters/codex.py @@ -22,7 +22,7 @@ from agent_bridge.diagnostics import redact_diagnostic from agent_bridge.model_discovery_exec import ( codex_app_server_list_models, - normalize_model_list, + parse_codex_models, ) from agent_bridge.models import Session, Task, TurnResult from agent_bridge.processes import ( @@ -54,7 +54,6 @@ async def ensure_session(self, session: Session) -> None: async def list_models(self, cwd: str) -> dict[str, Any]: """Query app-server model/list using its configuration and config_overrides.""" - source = "codex app-server model/list" env = self._enforce_spawn_env(self._worker_env()) command = await asyncio.to_thread( resolve_codex_command, self.agent.command, self.agent.fallback_commands, env=env @@ -64,15 +63,7 @@ async def list_models(self, cwd: str) -> dict[str, Any]: argv += ["-c", item] argv.append("app-server") entries = await codex_app_server_list_models(argv, env=env, cwd=cwd) - models, default_id, warnings = normalize_model_list(entries) - if entries and not models: - raise RuntimeError("Codex model/list contained no valid model entries") - return { - "source": source, - "models": models, - "default_model": default_id, - "warnings": warnings, - } + return parse_codex_models(entries) def _worker_env(self) -> dict[str, str]: return build_worker_env(self.agent.env, config=self.env_config, worker_context=True, home=self.home) diff --git a/src/agent_bridge/model_catalog.py b/src/agent_bridge/model_catalog.py index 20e0685..ade023b 100644 --- a/src/agent_bridge/model_catalog.py +++ b/src/agent_bridge/model_catalog.py @@ -2,7 +2,7 @@ from __future__ import annotations -from pydantic import BaseModel, Field, model_validator +from pydantic import BaseModel, Field from agent_bridge.config import AgentConfig @@ -22,13 +22,6 @@ class ModelCatalog(BaseModel): default_model: str | None = None warnings: list[str] = Field(default_factory=list) - @model_validator(mode="after") - def unique_ids(self) -> ModelCatalog: - ids = [model.id for model in self.models] - if len(ids) != len(set(ids)): - raise ValueError("model catalog contains duplicate IDs") - return self - def model_discovery_capability(cfg: AgentConfig) -> dict: """Describe model-discovery support for a worker.""" diff --git a/src/agent_bridge/model_discovery_exec.py b/src/agent_bridge/model_discovery_exec.py index 8b44bf3..9d0e2ef 100644 --- a/src/agent_bridge/model_discovery_exec.py +++ b/src/agent_bridge/model_discovery_exec.py @@ -20,53 +20,31 @@ _ANSI_RE = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]") -def normalize_model_list(entries: Sequence[Any]) -> tuple[list[dict[str, Any]], str | None, list[str]]: - """Normalize the documented Codex model/list schema, preserving level order.""" +def parse_codex_models(entries: Sequence[Any]) -> dict[str, Any]: + """Map app-server model/list metadata to a worker catalog.""" models = [] - seen = set() - warnings = [] - default = None for entry in entries: - if not isinstance(entry, Mapping) or not isinstance(entry.get("id"), str) or not entry["id"].strip(): - warnings.append("Codex model/list contained a row without a model ID") - continue - ident = entry["id"] - if ident in seen: - continue - seen.add(ident) raw_efforts = entry.get("supportedReasoningEfforts") - efforts = None - if isinstance(raw_efforts, list): - parsed = [ - item["reasoningEffort"] - for item in raw_efforts - if isinstance(item, Mapping) - and isinstance(item.get("reasoningEffort"), str) - and item["reasoningEffort"] - ] - efforts = parsed if parsed or not raw_efforts else None - if raw_efforts and not parsed: - warnings.append(f"Codex did not provide recognizable reasoning levels for {ident}") - if entry.get("isDefault") is True: - if default is None: - default = ident - else: - warnings.append("Codex reported multiple default models; keeping the first") - models.append( - { - "id": ident, - "name": entry.get("displayName") if isinstance(entry.get("displayName"), str) else None, - "is_default": ident == default, - "supported_efforts": efforts, - } - ) - return models, default, warnings + models.append({ + "id": entry["id"], + "name": entry.get("displayName"), + "is_default": entry.get("isDefault", False), + "supported_efforts": ( + [item["reasoningEffort"] for item in raw_efforts] + if raw_efforts is not None else None + ), + }) + return { + "source": "codex app-server model/list", + "models": models, + "default_model": next((model["id"] for model in models if model["is_default"]), None), + "warnings": [], + } def parse_agy_models_output(stdout: str) -> list[dict[str, Any]]: """Parse AGY tab-separated model ID and display name lines.""" models = [] - seen = set() for line in _ANSI_RE.sub("", stdout).splitlines(): if not line.strip(): continue @@ -76,9 +54,6 @@ def parse_agy_models_output(stdout: str) -> list[dict[str, Any]]: ident, name = (field.strip() for field in fields) if not ident or any(char.isspace() for char in ident) or not name: raise RuntimeError("Unrecognized agy models output: missing or invalid ID/name") - if ident in seen: - continue - seen.add(ident) models.append({"id": ident, "name": name, "is_default": False, "supported_efforts": None}) if not models: raise RuntimeError("agy models returned no model catalog") diff --git a/tests/test_model_discovery_acp.py b/tests/test_model_discovery_acp.py index 9deb234..a177c30 100644 --- a/tests/test_model_discovery_acp.py +++ b/tests/test_model_discovery_acp.py @@ -4,7 +4,12 @@ from types import SimpleNamespace import pytest -from acp.schema import PermissionOption +from acp.schema import ( + NewSessionResponse, + PermissionOption, + SessionConfigOptionBoolean, + SessionConfigOptionSelect, +) from agent_bridge.adapters import acp from agent_bridge.adapters.acp import AcpAdapter, AcpError, _acp_model_catalog, _Live @@ -13,7 +18,7 @@ class FakeConn: def __init__(self, response=None, fail_at=None, block_at=None): - self.response = response or {} + self.response = response or NewSessionResponse(session_id="discovery") self.fail_at = fail_at self.block_at = block_at self.calls = [] @@ -91,31 +96,31 @@ def build(name="kimi", command=None, **kwargs): ) -def test_grouped_options_and_standard_names_are_combined(): - result = _acp_model_catalog( - { - "configOptions": [ - { - "id": "model", "currentValue": "model-b", - "options": [{"group": "provider", "options": [ - {"value": "model-a", "name": "Model A"}, - {"value": "model-b"}, - ]}], - }, - { - "id": "thinking", "currentValue": "high", - "options": [{"group": "levels", "options": [ - {"value": "low"}, {"value": "high"}, {"value": "high"}, - ]}], - }, - ], - "models": { - "currentModelId": "model-b", - "availableModels": [{"modelId": "model-b", "name": "Model B"}], - }, - }, - "kimi", +def select_option(option_id, current, choices, **kwargs): + return SessionConfigOptionSelect( + id=option_id, name=option_id, type="select", + current_value=current, options=choices, **kwargs, ) + + +def snapshot(*options): + return NewSessionResponse(session_id="discovery", config_options=list(options)) + + +def test_grouped_model_and_effort_options(): + result = _acp_model_catalog(snapshot( + select_option("model", "model-b", [{ + "group": "provider", "name": "Provider", "options": [ + {"value": "model-a", "name": "Model A"}, + {"value": "model-b", "name": "Model B"}, + ], + }]), + select_option("thinking", "high", [{ + "group": "levels", "name": "Levels", "options": [ + {"value": "low", "name": "Low"}, {"value": "high", "name": "High"}, + ], + }]), + ), "kimi") assert result == { "source": "acp", "models": [ @@ -126,53 +131,48 @@ def test_grouped_options_and_standard_names_are_combined(): } -def test_standard_models_metadata_and_sdk_fields(): - class Model: - def model_dump(self, **kwargs): - return {"id": "custom-picker", "category": "model", "currentValue": "a", "options": [ - {"value": "a", "name": "A"}, - ]} - - result = _acp_model_catalog(SimpleNamespace( - config_options=[Model()], models=SimpleNamespace( - current_model_id="a", available_models=[SimpleNamespace(model_id="b", name="B")], - ), +def test_model_and_effort_categories(): + result = _acp_model_catalog(snapshot( + select_option("custom-picker", "a", [{"value": "a", "name": "A"}], category="model"), + select_option("custom-depth", "high", [{"value": "high", "name": "High"}], category="thought_level"), ), "kimi") assert result["default_model"] == "a" - assert [(m["id"], m["name"], m["supported_efforts"]) for m in result["models"]] == [ - ("a", "A", None), ("b", "B", None), - ] - - -def test_standard_metadata_without_config_options(): - result = _acp_model_catalog({"models": { - "currentModelId": "a", "availableModels": [{"modelId": "a", "name": "Alpha"}], - }}, "other-acp-worker") - assert result["default_model"] == "a" assert result["models"] == [ - {"id": "a", "name": "Alpha", "is_default": True, "supported_efforts": None}, + {"id": "a", "name": "A", "is_default": True, "supported_efforts": ["high"]}, ] -@pytest.mark.parametrize("payload", [{}, {"configOptions": "malformed"}, {"models": {"availableModels": "bad"}}]) -def test_unadvertised_models_are_not_invented(payload): - result = _acp_model_catalog(payload, "kimi") +def test_missing_catalog_warns(): + result = _acp_model_catalog(NewSessionResponse(session_id="discovery"), "kimi") assert result["models"] == [] assert result["default_model"] is None assert result["warnings"] -def test_boolean_thinking_is_not_reported_as_effort(): - result = _acp_model_catalog({"configOptions": [ - {"id": "model", "currentValue": "a", "options": [{"value": "a"}]}, - {"id": "thinking", "options": [{"value": "true"}, {"value": "false"}]}, - ]}, "kimi") +def test_current_model_is_not_added_to_advertised_choices(): + result = _acp_model_catalog(snapshot( + select_option("model", "current", [{"value": "offered", "name": "Offered"}]), + ), "kimi") + assert result["default_model"] == "current" + assert [model["id"] for model in result["models"]] == ["offered"] + + +@pytest.mark.parametrize("thinking", [ + SessionConfigOptionBoolean(id="thinking", name="Thinking", type="boolean", current_value=True), + select_option("thinking", "true", [ + {"value": "true", "name": "On"}, {"value": "false", "name": "Off"}, + ]), +]) +def test_boolean_thinking_is_not_reported_as_effort(thinking): + result = _acp_model_catalog(snapshot( + select_option("model", "a", [{"value": "a", "name": "A"}]), thinking, + ), "kimi") assert result["models"][0]["supported_efforts"] is None async def test_private_handshake_never_prompts_configures_or_persists(discovery): adapter, active = discovery.build(cwd="process-directory") - discovery.conn.response = {"models": {"currentModelId": "a", "availableModels": [{"modelId": "a"}]}} + discovery.conn.response = snapshot(select_option("model", "a", [{"value": "a", "name": "A"}])) result = await adapter.list_models("workspace-directory") assert result["models"][0]["id"] == "a" assert [name for name, _ in discovery.conn.calls] == ["initialize", "new_session", "close"] @@ -282,15 +282,17 @@ def prepare(command, env, **kwargs): monkeypatch.setattr(acp, "prepare_dsh_launch", prepare) adapter, _active = discovery.build("dsh", ["dsh", "--profile", "acp"]) - discovery.conn.response = {"configOptions": [ - {"id": "model", "currentValue": '["openai","reasoner"]', "options": [ - {"group": "openai", "options": [ + discovery.conn.response = snapshot( + select_option("model", '["openai","reasoner"]', [{ + "group": "openai", "name": "OpenAI", "options": [ {"value": '["openai","reasoner"]', "name": "Reasoner"}, {"value": '["openai","fast"]', "name": "Fast"}, - ]}, - ]}, - {"id": "reasoning_effort", "options": [{"value": "medium"}, {"value": "high"}]}, - ]} + ], + }]), + select_option("reasoning_effort", "high", [ + {"value": "medium", "name": "Medium"}, {"value": "high", "name": "High"}, + ]), + ) result = await adapter.list_models(".") assert prepared == [{}] assert result["default_model"] == "openai/reasoner" @@ -299,15 +301,7 @@ def prepare(command, env, **kwargs): ] -@pytest.mark.parametrize("payload", [ - {"configOptions": [{"id": "model", "currentValue": "legacy-model", "options": [ - {"value": "legacy-model", "name": "Legacy Model"}, - ]}]}, - {"models": {"currentModelId": "legacy-model", "availableModels": [ - {"modelId": "legacy-model", "name": "Legacy Model"}, - ]}}, -]) -async def test_legacy_dsh_discovers_actual_advertised_catalog(discovery, monkeypatch, payload): +async def test_legacy_dsh_discovers_actual_advertised_catalog(discovery, monkeypatch): prepared = [] def prepare(command, env, **kwargs): @@ -316,7 +310,9 @@ def prepare(command, env, **kwargs): monkeypatch.setattr(acp, "prepare_dsh_launch", prepare) adapter, active = discovery.build("dsh", ["dsh-acp-demo"]) - discovery.conn.response = payload + discovery.conn.response = snapshot(select_option( + "model", "legacy-model", [{"value": "legacy-model", "name": "Legacy Model"}], + )) result = await adapter.list_models(".") assert prepared == [{}] assert result["default_model"] == "legacy-model" diff --git a/tests/test_model_discovery_exec.py b/tests/test_model_discovery_exec.py index 64accb2..4289198 100644 --- a/tests/test_model_discovery_exec.py +++ b/tests/test_model_discovery_exec.py @@ -14,8 +14,8 @@ from agent_bridge.config import AgentConfig, EnvConfig from agent_bridge.model_discovery_exec import ( codex_app_server_list_models, - normalize_model_list, parse_agy_models_output, + parse_codex_models, ) FAKE_AGY = r""" @@ -185,7 +185,7 @@ def test_agy_unverified_formats_and_diagnostics_are_not_catalogs(text): def test_codex_advertised_metadata_preserves_levels_and_unknowns(): - models, default, warnings = normalize_model_list( + catalog = parse_codex_models( [ { "id": "a", @@ -194,14 +194,27 @@ def test_codex_advertised_metadata_preserves_levels_and_unknowns(): "supportedReasoningEfforts": [{"reasoningEffort": "medium"}, {"reasoningEffort": "xhigh"}], }, {"id": "b", "supportedReasoningEfforts": []}, - {"id": "c", "supportedReasoningEfforts": [{"unexpected": "high"}]}, {"id": "d"}, ] ) - assert default == "a" - assert [m["supported_efforts"] for m in models] == [["medium", "xhigh"], [], None, None] - assert models[0]["name"] == "A" - assert warnings + assert catalog["default_model"] == "a" + assert [m["supported_efforts"] for m in catalog["models"]] == [["medium", "xhigh"], [], None] + assert catalog["models"][0]["name"] == "A" + assert catalog["warnings"] == [] + + +def test_codex_malformed_effort_metadata_fails(): + with pytest.raises(KeyError, match="reasoningEffort"): + parse_codex_models([{"id": "a", "supportedReasoningEfforts": [{"unexpected": "high"}]}]) + + +def test_codex_preserves_advertised_rows(): + catalog = parse_codex_models([ + {"id": "a", "displayName": "First", "isDefault": True}, + {"id": "a", "displayName": "Second", "isDefault": True}, + ]) + assert [model["name"] for model in catalog["models"]] == ["First", "Second"] + assert all(model["is_default"] for model in catalog["models"]) async def test_agy_adapter_env_cwd_and_catalog(tmp_path): From d3a6f498599c717b04b2387a4cbcbda42d495468 Mon Sep 17 00:00:00 2001 From: "edge.xyz" <32131214+edgexyz@users.noreply.github.com> Date: Wed, 21 Oct 2026 21:33:21 -0400 Subject: [PATCH 6/6] Enable Claude model discovery through shared ACP handshake --- README.md | 2 +- SETUP.md | 2 +- scripts/smoke_models.py | 2 +- src/agent_bridge/model_catalog.py | 2 +- tests/test_model_catalog.py | 6 +++--- tests/test_model_discovery_acp.py | 25 +++++++++++++++++++++++++ 6 files changed, 32 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 51ead8a..174f83e 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ Close coordinators that are holding Bridge, then `agent-bridge upgrade`, then re Call `list_models(agent="kimi", cwd="/absolute/project")` to discover a worker's advertised models before choosing one. Supported workers are Kimi, DSH, -Antigravity, and Codex; check `list_agents.model_discovery` for support. +Claude Code, Antigravity, and Codex; check `list_agents.model_discovery` for support. Results are cached per worker/project for one hour by default. Use `refresh=true` for an immediate update. See [Model discovery](SETUP.md#model-discovery) for result fields, configuration, and session behavior. diff --git a/SETUP.md b/SETUP.md index fe146a2..be2cb8b 100644 --- a/SETUP.md +++ b/SETUP.md @@ -460,7 +460,7 @@ Supported workers and catalog sources: | Worker | Source | | --- | --- | -| Kimi, DSH | ACP `initialize` / `session/new` | +| Kimi, DSH, Claude Code | ACP `initialize` / `session/new` | | Antigravity | `agy models` | | Codex | app-server `model/list`, using app-server configuration and the worker's `config_overrides` | diff --git a/scripts/smoke_models.py b/scripts/smoke_models.py index 937bef9..2b83cf2 100644 --- a/scripts/smoke_models.py +++ b/scripts/smoke_models.py @@ -73,7 +73,7 @@ async def run(cwd: Path, agents: list[str], config_home: Path, output: Path | No if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--cwd", type=Path, required=True) - parser.add_argument("--agents", nargs="+", default=["kimi", "dsh", "antigravity", "codex"]) + parser.add_argument("--agents", nargs="+", default=["kimi", "dsh", "claude", "antigravity", "codex"]) parser.add_argument("--config-home", type=Path, default=bridge_home()) parser.add_argument("--output", type=Path) args = parser.parse_args() diff --git a/src/agent_bridge/model_catalog.py b/src/agent_bridge/model_catalog.py index ade023b..8a2ca60 100644 --- a/src/agent_bridge/model_catalog.py +++ b/src/agent_bridge/model_catalog.py @@ -26,7 +26,7 @@ class ModelCatalog(BaseModel): def model_discovery_capability(cfg: AgentConfig) -> dict: """Describe model-discovery support for a worker.""" supported = cfg.protocol in {"agy", "codex"} or ( - cfg.protocol == "acp" and cfg.name in {"kimi", "dsh"} + cfg.protocol == "acp" and cfg.name in {"kimi", "dsh", "claude"} ) source = {"agy": "agy models", "codex": "codex app-server model/list", "acp": "acp session/new"} return { diff --git a/tests/test_model_catalog.py b/tests/test_model_catalog.py index 80ea3de..224d28b 100644 --- a/tests/test_model_catalog.py +++ b/tests/test_model_catalog.py @@ -147,9 +147,9 @@ async def test_rejects_bad_context_and_unsupported_worker(catalog_registry, tmp_ registry, calls = catalog_registry with pytest.raises(ValueError, match="absolute"): await registry.list_models("kimi", "relative") - registry.config.agents["claude"] = AgentConfig(name="claude", protocol="acp", command=["unused"]) + registry.config.agents["grok"] = AgentConfig(name="grok", protocol="acp", command=["unused"]) with pytest.raises(NotImplementedError): - await registry.list_models("claude", str(tmp_path)) + await registry.list_models("grok", str(tmp_path)) registry.dispatch_enabled = False with pytest.raises(RuntimeError, match="worker-inherited"): await registry.list_models("kimi", str(tmp_path)) @@ -178,7 +178,7 @@ def test_model_discovery_config_overlay(tmp_path): def test_capability_does_not_claim_unimplemented_agents(): - for name, protocol in [("kimi", "acp"), ("dsh", "acp"), ("codex", "codex"), ("antigravity", "agy")]: + for name, protocol in [("kimi", "acp"), ("dsh", "acp"), ("claude", "acp"), ("codex", "codex"), ("antigravity", "agy")]: cfg = AgentConfig(name=name, protocol=protocol, command=["unused"]) assert model_discovery_capability(cfg)["supported"] assert not model_discovery_capability(AgentConfig(name="grok", protocol="acp", command=["unused"]))["supported"] diff --git a/tests/test_model_discovery_acp.py b/tests/test_model_discovery_acp.py index a177c30..b80b582 100644 --- a/tests/test_model_discovery_acp.py +++ b/tests/test_model_discovery_acp.py @@ -347,3 +347,28 @@ async def test_legacy_dsh_handshake_error_is_failure_not_unsupported(discovery, with pytest.raises(AcpError, match="new_session failed"): await adapter.list_models(".") assert discovery.reaped == [discovery.proc] + + +async def test_claude_catalog_uses_current_model_effort(discovery): + adapter, active = discovery.build("claude") + discovery.conn.response = snapshot( + select_option("model", "opus", [ + {"value": "opus", "name": "Opus"}, + {"value": "sonnet", "name": "Sonnet"}, + ], category="model"), + select_option("effort", "default", [ + {"value": "default", "name": "Default"}, + {"value": "low", "name": "Low"}, + {"value": "high", "name": "High"}, + ], category="thought_level"), + ) + result = await adapter.list_models(".") + assert result["default_model"] == "opus" + assert result["models"] == [ + {"id": "opus", "name": "Opus", "is_default": True, + "supported_efforts": ["default", "low", "high"]}, + {"id": "sonnet", "name": "Sonnet", "is_default": False, "supported_efforts": None}, + ] + assert [name for name, _ in discovery.conn.calls] == ["initialize", "new_session", "close"] + assert adapter._live == {"existing-session": active} + assert discovery.reaped == [discovery.proc]