Something went wrong. Try again.
MCP based ACP agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731"""The headless ACP v2 client, tested against real children over real pipes.
Two kinds of child, and neither is a mock of the code under test. The codeunder test is :class:`crow_cli.client2.SubagentDriver`; a child agent is the*other end of the protocol*, so standing one up is not stubbing anything:
* a **scripted child** — a small v2 agent written to ``tmp_path`` and spawned as a subprocess. It exists because the behaviours worth testing have to be provoked on demand: a turn that hangs so cancellation has something to cancel, a quarter-megabyte stderr flood so the drain has something to drain.* a **real ``crow_cli.agent2.main`` child** — one test, because every other test points ``agent_argv`` at the scripted child. An argv that no test ever executes is how a subagent ends up dead in production and green in CI.
``agent_argv`` is patched in the scripted tests rather than exercised, becauseit is a pure function of the environment with its own tests below. Everythingelse — spawn, stderr drain, handshake, sessions, turns, cancel, teardown — isreal in every test here, including the pipes."""
from __future__ import annotations
import asyncioimport sysfrom contextlib import asynccontextmanagerfrom pathlib import Path
import pytestimport yamlfrom acp.experimental import v2from pydantic import ValidationError
from crow_cli.client2 import ( ChildExited, HeadlessClient, SubagentDriver, child_config,)from crow_cli.client2 import subagent as subagent_modfrom crow_cli.client2.subagent import ( STDERR_LINES, agent_argv, mcp_servers_to_models,)
#: An interpreter start plus a crow import, and the flood test also pushes a#: quarter megabyte through a pipe. Generous on purpose — but every wait here#: is bounded, so a regression fails instead of hanging the suite.START_TIMEOUT = 60.0TURN_TIMEOUT = 30.0
#: Well past the 64KB a Linux pipe buffers. A parent that does not read the#: child's stderr blocks the child on this many lines and never hears back.FLOOD_LINES = 3000
SCRIPTED_CHILD = r'''"""A scripted ACP v2 agent: the peer end of the wire for the client2 tests.
Speaks real JSON-RPC over real stdio. Answers the handshake, mints sessionids, and reports turn outcomes as ``state_update`` notifications the way v2requires — the prompt response is empty, so this is the only channel theoutcome has. Hangs on a prompt containing ``hang`` so cancellation hassomething to cancel, and floods stderr on demand so the parent's drain hassomething to drain."""
import asyncioimport osimport sysimport traceback
from acp.experimental import v2
s = v2.schema
FLOOD = __FLOOD__HANG_ON = "hang"
def say(line): """stderr, flushed: the parent asserts on these, so they must arrive.""" print(line, file=sys.stderr) sys.stderr.flush()
def report(task): """A turn that raises has to be NOISY.
Nobody retrieves a bare ``create_task``'s exception, so a crash in the turn looks exactly like a hang from the parent's side of the pipe. """ if task.cancelled(): return if exc := task.exception(): say("turn failed: %r" % (exc,)) traceback.print_exc(file=sys.stderr) sys.stderr.flush()
class ScriptedChild: def __init__(self): self.conn = None self.sessions = 0 self.turn_task = None
def on_connect(self, conn): self.conn = conn
# rc2's router hands a handler the request's FIELDS as keywords and # spreads its _meta in among them, so each signature below names the # fields it cares about and catches the rest. async def initialize(self, protocol_version, info, capabilities=None, **kw): say("initialized protocol=%s" % protocol_version) return s.InitializeResponse( protocol_version=v2.PROTOCOL_VERSION, info=s.Implementation(name="scripted-child", version="0.0.1"), capabilities=s.AgentCapabilities(), )
async def new_session(self, cwd, additional_directories=None, mcp_servers=None, **kw): self.sessions += 1 say("new-session cwd=%s servers=%d" % (cwd, len(mcp_servers or []))) return s.NewSessionResponse(session_id="child-%d" % self.sessions)
async def resume_session(self, session_id, cwd, additional_directories=None, mcp_servers=None, replay_from=None, **kw): say("resumed %s replay_from=%s" % (session_id, replay_from)) return s.ResumeSessionResponse()
async def fork_session(self, session_id, cwd, additional_directories=None, mcp_servers=None, **meta): say("forked %s meta=%s" % (session_id, meta or None)) return s.ForkSessionResponse(session_id="fork-of-%s" % session_id)
async def prompt(self, session_id, prompt, **kw): text = "".join(getattr(b, "text", "") for b in prompt) say("prompt %s %r" % (session_id, text)) if session_id.startswith("fork-of-"): # Forks park before their first prompt. Deliver the pre-turn idle # after the client subscribed, as it can arrive on a real pipe. await self._send(session_id, s.IdleSessionStateUpdate()) self.turn_task = asyncio.create_task(self._turn(session_id, text)) self.turn_task.add_done_callback(report) return s.PromptResponse(message_id="u1")
async def _turn(self, session_id, text): # The wire models are Running/Idle/RequiresActionSessionStateUpdate. # RunningStateUpdate and IdleStateUpdate are their payload BASES and # are not members of UpdateSessionNotification.update's union, so # sending one raises a ValidationError that nobody retrieves. await self._send(session_id, s.RunningSessionStateUpdate()) if "die" in text: say("dying on %r" % text) os._exit(3) if HANG_ON in text: say("hanging on %r" % text) await asyncio.Event().wait() return await self._send( session_id, s.AgentMessageChunk( message_id="m1", content=s.TextContentBlock(text="pong: " + text) ), ) await self._send(session_id, s.IdleSessionStateUpdate(stop_reason="end_turn"))
async def cancel_session(self, session_id, **kw): say("cancelled %s" % session_id) if self.turn_task is not None: self.turn_task.cancel() await self._send(session_id, s.IdleSessionStateUpdate(stop_reason="cancelled"))
async def _send(self, session_id, update): await self.conn.session_update(session_id=session_id, update=update)
for _i in range(FLOOD): say("stderr-flood-line-%06d %s" % (_i, "x" * 60))if FLOOD: say("stderr-flood-done")
_probe = os.environ.get("CROW_CLIENT2_PROBE")if _probe: say("env-probe=%s" % _probe)
asyncio.run(v2.run_agent(ScriptedChild()))'''
def child_source(flood: int = 0) -> str: return SCRIPTED_CHILD.replace("__FLOOD__", str(flood))
@asynccontextmanagerasync def scripted(tmp_path, monkeypatch, *, flood: int = 0, name: str = "child.py"): """A started :class:`SubagentDriver` talking to the scripted child.
``agent_argv`` is pointed at the script instead of ``crow_cli.agent2.main`` so the child's behaviour is controllable; the argv itself is tested directly below and end to end in the real-child test. """ script = tmp_path / name script.write_text(child_source(flood)) argv = [sys.executable, str(script)] monkeypatch.setattr(subagent_mod, "agent_argv", lambda **_: argv) driver = SubagentDriver() try: await asyncio.wait_for(driver.start(str(tmp_path)), START_TIMEOUT) yield driver finally: await driver.close()
async def wait_for_update(driver, pred, timeout: float = TURN_TIMEOUT): """The first recorded update matching ``pred``.
Polls the record rather than sleeping a fixed amount: the child is a separate process, so there is no in-process event to await. """
async def poll(): while True: for update in list(driver.client.updates): if pred(update): return update await asyncio.sleep(0.02)
return await asyncio.wait_for(poll(), timeout)
async def wait_for_stderr(driver, needle: str, timeout: float = TURN_TIMEOUT) -> str: """The child's stderr tail, once it contains ``needle``.
The drainer is its own task reading a pipe from its own process, so "the child printed it" and "the parent has read it" are two moments. Asserting on the tail without waiting for it is a race that passes most of the time. """
async def poll(): while needle not in driver.stderr_tail: await asyncio.sleep(0.02)
await asyncio.wait_for(poll(), timeout) return driver.stderr_tail
def is_running(update) -> bool: return getattr(update, "session_update", None) == "state_update" and getattr( update, "state", None ) == "running"
def make_child_config(tmp_path: Path) -> Path: """A hermetic config dir for a real agent2 child.
A provider and a model must exist for ``config.is_configured``; neither is contacted, because nothing here sends a prompt. ``db_uri`` points at a throwaway sqlite so the child's ``session/new`` writes rows nowhere else can see. """ config_dir = tmp_path / "config" config_dir.mkdir(parents=True, exist_ok=True) (config_dir / ".env").write_text("API_KEY=child-key\n") (config_dir / "config.yaml").write_text( yaml.safe_dump( { "providers": { "child-provider": { "api_key": "${API_KEY}", "base_url": "https://child.invalid/v1", } }, "models": { "child-model": { "provider": "child-provider", "model": "child-model-id", } }, "db_uri": "sqlite:///%s" % (tmp_path / "child.db"), } ) ) return config_dir
# -- the driver, over a real pipe ------------------------------------------
async def test_the_handshake_yields_a_child_that_mints_session_ids(tmp_path, monkeypatch): async with scripted(tmp_path, monkeypatch) as driver: first = await asyncio.wait_for(driver.new_session(str(tmp_path)), TURN_TIMEOUT) second = await asyncio.wait_for(driver.new_session(str(tmp_path)), TURN_TIMEOUT) tail = await wait_for_stderr(driver, "initialized protocol=") assert first == "child-1" assert second == "child-2", "a second session/new must mint a second id" assert "initialized protocol=2" in tail
async def test_prompt_returns_the_stop_reason_the_idle_update_carried( tmp_path, monkeypatch): """The v2 inversion: ``PromptResponse`` is empty, so the outcome arrives as a notification and somebody has to watch for it.""" async with scripted(tmp_path, monkeypatch) as driver: sid = await asyncio.wait_for(driver.new_session(str(tmp_path)), TURN_TIMEOUT) stop = await asyncio.wait_for(driver.prompt(sid, "ping"), TURN_TIMEOUT) updates = list(driver.client.updates)
assert stop == "end_turn" assert is_running(updates[0]), "running must precede the chunks" text = "".join( getattr(u.content, "text", "") for u in updates if getattr(u, "session_update", None) == "agent_message_chunk" ) assert text == "pong: ping"
async def test_a_fresh_session_initial_idle_does_not_end_a_turn(): client = HeadlessClient() queue = client.watch("fresh") await client.session_update( session_id="fresh", update=v2.schema.IdleSessionStateUpdate() ) assert queue.empty() await client.session_update( session_id="fresh", update=v2.schema.RunningSessionStateUpdate() ) await client.session_update( session_id="fresh", update=v2.schema.IdleSessionStateUpdate() ) assert queue.get_nowait() is None
async def test_a_prompt_that_never_idles_times_out_rather_than_hanging( tmp_path, monkeypatch): async with scripted(tmp_path, monkeypatch) as driver: sid = await asyncio.wait_for(driver.new_session(str(tmp_path)), TURN_TIMEOUT) with pytest.raises(TimeoutError): await driver.prompt(sid, "please hang", timeout=0.5) await driver.cancel(sid) await wait_for_stderr(driver, "cancelled")
async def test_wait_picks_up_a_turn_a_timed_out_prompt_left_in_flight( tmp_path, monkeypatch): """The seam a hand-off rides: ``prompt`` is ``wait`` plus the send.
A timeout cancels the getter but leaves the queue, so a second wait finds the reason that arrives afterwards instead of waiting for a notification that already came. Without this, the only way to stop waiting on a slow delegate is to kill it — which is what rlm used to do while its own error message promised the delegate was "not lost". """ async with scripted(tmp_path, monkeypatch) as driver: sid = await asyncio.wait_for(driver.new_session(str(tmp_path)), TURN_TIMEOUT) with pytest.raises(TimeoutError): await driver.prompt(sid, "please hang", timeout=0.5) handed_off = asyncio.create_task(driver.wait(sid)) await driver.cancel(sid) assert await asyncio.wait_for(handed_off, TURN_TIMEOUT) == "cancelled" await wait_for_stderr(driver, "cancelled")
async def test_a_child_that_dies_mid_turn_raises_instead_of_hanging( tmp_path, monkeypatch): """The queue the idle would have landed in is never going to fill, so waiting on it is waiting for nothing. A task watcher that waits forever leaves its row "running" forever, with its owner parked behind it — which is the one failure the whole mailbox design exists to prevent. """ async with scripted(tmp_path, monkeypatch) as driver: sid = await asyncio.wait_for(driver.new_session(str(tmp_path)), TURN_TIMEOUT) with pytest.raises(ChildExited) as exc: await asyncio.wait_for(driver.prompt(sid, "die please"), TURN_TIMEOUT) assert driver.proc.returncode == 3 assert "child process exited with code 3" in str(exc.value)
async def test_cancel_ends_a_hung_turn_and_the_session_survives_it(tmp_path, monkeypatch): async with scripted(tmp_path, monkeypatch) as driver: sid = await asyncio.wait_for(driver.new_session(str(tmp_path)), TURN_TIMEOUT) hung = asyncio.create_task(driver.prompt(sid, "hang please")) await wait_for_update(driver, is_running) await driver.cancel(sid) assert await asyncio.wait_for(hung, TURN_TIMEOUT) == "cancelled"
# The redirect story: cancellation is per-turn, so the same session id # is still promptable and still reports its own outcome. again = await asyncio.wait_for(driver.prompt(sid, "again"), TURN_TIMEOUT) assert again == "end_turn"
async def test_a_child_that_floods_stderr_does_not_deadlock_its_parent( tmp_path, monkeypatch): """v1 opened the child's stderr pipe and never read it. A child that logs more than the pipe buffers then blocks on the write and hangs with no error on either side — so the drain is load-bearing, not tidy.""" async with scripted(tmp_path, monkeypatch, flood=FLOOD_LINES) as driver: sid = await asyncio.wait_for(driver.new_session(str(tmp_path)), TURN_TIMEOUT) stop = await asyncio.wait_for(driver.prompt(sid, "ping"), TURN_TIMEOUT) tail = await wait_for_stderr(driver, "stderr-flood-done")
assert stop == "end_turn", "the turn completed, so the pipe never filled" assert len(tail.splitlines()) <= STDERR_LINES, "the tail is bounded" assert "stderr-flood-line-000000" not in tail, "it keeps the END, not the start"
async def test_the_child_inherits_the_parents_environment(tmp_path, monkeypatch): """``spawn_stdio_transport`` defaults to a trimmed MCP-style environment. A crow child needs the parent's, or shell-exported keys and service URLs vanish between the two processes.""" monkeypatch.setenv("CROW_CLIENT2_PROBE", "inherited") async with scripted(tmp_path, monkeypatch) as driver: tail = await wait_for_stderr(driver, "env-probe=") assert "env-probe=inherited" in tail
async def test_resume_asks_for_no_replay_and_fork_returns_a_new_id(tmp_path, monkeypatch): async with scripted(tmp_path, monkeypatch) as driver: sid = await asyncio.wait_for(driver.new_session(str(tmp_path)), TURN_TIMEOUT) await asyncio.wait_for(driver.resume_session(sid, str(tmp_path)), TURN_TIMEOUT) forked = await asyncio.wait_for( driver.fork_session(sid, str(tmp_path)), TURN_TIMEOUT ) tail = await wait_for_stderr(driver, "forked child-1")
assert forked == "fork-of-child-1" assert "resumed child-1 replay_from=None" in tail, ( "the transcript is in sqlite; replaying it over the wire is pure cost" ) assert "meta=None" in tail, "a fork with nothing to anchor sends no _meta"
async def test_a_fork_initial_idle_cannot_complete_its_first_turn(tmp_path, monkeypatch): async with scripted(tmp_path, monkeypatch) as driver: sid = await driver.new_session(str(tmp_path)) fork = await driver.fork_session(sid, str(tmp_path)) stop = await asyncio.wait_for(driver.prompt(fork, "ping"), TURN_TIMEOUT) updates = list(driver.client.updates)
assert stop == "end_turn", "the fork must wait for its own turn, not its first park" assert any(is_running(u) for u in updates) assert any( getattr(u, "session_update", None) == "agent_message_chunk" and getattr(u.content, "text", "") == "pong: ping" for u in updates )
async def test_fork_anchors_ride_the_request_meta(tmp_path, monkeypatch): """``messageOffset``/``rlmDepth`` are the delegation half of the fork contract, and they are not in the schema — v2's ``ForkSessionRequest`` carries the environment and nothing else — so they ride ``_meta``.
The offset steps the fork's history back over the call that made it, and the depth is persisted on the fork's agent row so the budget survives a resume in a different process. A delegate that forgets either is the infinity mirror, and neither is observable from this side of the wire except by asking the child what it received. """ async with scripted(tmp_path, monkeypatch) as driver: sid = await asyncio.wait_for(driver.new_session(str(tmp_path)), TURN_TIMEOUT) forked = await asyncio.wait_for( driver.fork_session( sid, str(tmp_path), message_offset=2, rlm_depth=1 ), TURN_TIMEOUT, ) tail = await wait_for_stderr(driver, "meta=")
assert forked == "fork-of-child-1" assert "meta={'messageOffset': 2, 'rlmDepth': 1}" in tail
async def test_new_session_carries_the_mcp_servers_it_was_given(tmp_path, monkeypatch): servers = [{"type": "stdio", "name": "gate", "command": "/bin/true"}] async with scripted(tmp_path, monkeypatch) as driver: await asyncio.wait_for( driver.new_session(str(tmp_path), mcp_servers=servers), TURN_TIMEOUT ) tail = await wait_for_stderr(driver, "servers=1") assert "new-session cwd=%s servers=1" % tmp_path in tail
async def test_close_reaps_the_child_process(tmp_path, monkeypatch): async with scripted(tmp_path, monkeypatch) as driver: proc = driver.proc assert proc is not None and proc.returncode is None assert driver.conn is None and driver.proc is None assert proc.returncode is not None, "close() must not leave a zombie"
async def test_close_on_a_driver_that_never_started_is_a_no_op(): driver = SubagentDriver() await driver.close() assert driver.conn is None assert driver.proc is None assert driver.stderr_tail == ""
async def test_a_real_agent_fork_completes_its_own_prompt_not_its_initial_idle(tmp_path): config_dir = make_child_config(tmp_path) driver = SubagentDriver() try: await asyncio.wait_for( driver.start(str(tmp_path), config_dir=config_dir), START_TIMEOUT ) sid = await asyncio.wait_for(driver.new_session(str(tmp_path)), START_TIMEOUT) assert await asyncio.wait_for(driver.prompt(sid, "/help"), TURN_TIMEOUT) == "end_turn" fork = await asyncio.wait_for(driver.fork_session(sid, str(tmp_path)), TURN_TIMEOUT) assert await asyncio.wait_for(driver.prompt(fork, "/help"), TURN_TIMEOUT) == "end_turn" updates = list(driver.client.updates) finally: await driver.close()
assert fork != sid assert sum( getattr(u, "session_update", None) == "agent_message" and "Available slash commands" in str(getattr(u, "content", "")) for u in updates ) == 2, "both the trunk and the fork must answer their own prompt"
async def test_agent_argv_really_starts_a_v2_agent_that_can_open_a_session(tmp_path): """The one test that does not patch ``agent_argv``.
Everything above proves the driver speaks v2 correctly to *a* child; this proves the argv it builds starts *our* child. No model is needed — ``make_llm`` runs per turn and nothing here prompts. """ config_dir = make_child_config(tmp_path) driver = SubagentDriver() try: await asyncio.wait_for( driver.start(str(tmp_path), config_dir=config_dir), START_TIMEOUT ) proc = driver.proc sid = await asyncio.wait_for(driver.new_session(str(tmp_path)), START_TIMEOUT) tail = driver.stderr_tail finally: await driver.close()
assert isinstance(sid, str) and sid assert sid.count("-") >= 2, "expected a coolname session id, got %r" % sid assert proc.returncode is not None assert "Traceback" not in tail, tail
# -- the pure pieces --------------------------------------------------------
def test_agent_argv_names_the_v2_entry_point(): assert agent_argv() == [sys.executable, "-m", "crow_cli.agent2.main"]
def test_agent_argv_threads_the_config_pointers_and_the_model(): assert agent_argv(model="m", config_dir=Path("/cd"), config_file=Path("/cf.yaml")) == [ sys.executable, "-m", "crow_cli.agent2.main", "--config-dir", "/cd", "--config-file", "/cf.yaml", "--model", "m", ]
def test_agent_argv_names_the_acp2_subcommand_in_a_frozen_build(monkeypatch): """A packaged crow's ``sys.executable`` IS the ``crow-cli`` binary, so the v2 agent is a subcommand of it.
Not ``-m crow_cli.agent2.main`` — a frozen interpreter has no crow_cli on its path — and not ``acp``, which would produce a child that answers a v2 handshake with a v1 one. The flags are the same in both shapes, because ``cli.main.run_agent2`` declares the ones ``agent.main`` does. """ monkeypatch.setattr(sys, "frozen", True, raising=False) assert agent_argv() == [sys.executable, "acp2"] assert agent_argv(model="m", config_dir=Path("/cd"), config_file=Path("/cf.yaml")) == [ sys.executable, "acp2", "--config-dir", "/cd", "--config-file", "/cf.yaml", "--model", "m", ]
def test_child_config_forwards_the_env_pointers(monkeypatch, tmp_path): monkeypatch.setenv("CROW_CONFIG_FILE", str(tmp_path / "c.yaml")) monkeypatch.setenv("CROW_CONFIG_DIR", str(tmp_path / "cd")) assert child_config() == { "config_file": tmp_path / "c.yaml", "config_dir": tmp_path / "cd", }
def test_child_config_is_empty_when_the_parent_used_the_defaults(monkeypatch): monkeypatch.delenv("CROW_CONFIG_FILE", raising=False) monkeypatch.delenv("CROW_CONFIG_DIR", raising=False) assert child_config() == {}
def test_mcp_servers_to_models_round_trips_what_the_registry_stored(): stored = [ { "type": "stdio", "name": "crow", "command": "/bin/crow-cli", "args": ["mcp"], "env": [{"name": "K", "value": "V"}], }, { "type": "http", "name": "h", "url": "https://h.invalid/mcp", "headers": [{"name": "A", "value": "B"}], }, {"type": "acp", "name": "a", "server_id": "srv"}, ] models = mcp_servers_to_models(stored) assert [type(m).__name__ for m in models] == [ "StdioMcpServer", "HttpMcpServer", "AcpMcpServer", ] assert models[0].command == "/bin/crow-cli" assert models[0].env[0].name == "K" assert models[1].headers[0].value == "B" assert models[2].server_id == "srv"
def test_sse_is_rewritten_to_http_rather_than_dropped(): """v2 deleted ``SseMcpServer``. An SSE endpoint is an HTTP endpoint, so an old stored server keeps working instead of silently disappearing and leaving the child toolless.""" (model,) = mcp_servers_to_models( [{"type": "sse", "name": "old", "url": "https://old.invalid/sse"}] ) assert type(model).__name__ == "HttpMcpServer" assert model.type == "http" assert str(model.url) == "https://old.invalid/sse"
def test_a_stdio_server_stored_without_args_or_env_still_validates(): (model,) = mcp_servers_to_models([{"name": "bare", "command": "/bin/true"}]) assert model.type == "stdio" assert model.args == [] assert model.env == []
def test_an_unknown_type_is_kept_as_an_other_server(): (model,) = mcp_servers_to_models([{"type": "carrier-pigeon", "name": "p"}]) assert type(model).__name__ == "OtherMcpServer" assert model.type == "carrier-pigeon"
def test_a_server_that_is_already_a_model_passes_through(): original = v2.schema.StdioMcpServer(name="m", command="/bin/true") assert mcp_servers_to_models([original]) == [original]
def test_none_and_empty_both_mean_no_servers(): assert mcp_servers_to_models(None) == [] assert mcp_servers_to_models([]) == []
def test_a_malformed_server_raises_instead_of_vanishing(): """v1 validated the whole list at once and pydantic dropped the bad item, so the child came up toolless with no error anywhere.""" with pytest.raises(ValidationError): mcp_servers_to_models([{"type": "stdio", "name": "no-command"}])
def test_the_stop_queue_is_per_session_and_stable_across_calls(): client = HeadlessClient() queue = client.watch("s1") assert client.watch("s1") is queue, "watching twice must not lose a stop" assert client.watch("s2") is not queue
async def test_only_an_idle_for_a_watched_session_ends_the_wait(): client = HeadlessClient() queue = client.watch("s1") # Called the way the router calls it: the notification's fields as # keywords, never the notification. await client.session_update( session_id="s1", update=v2.schema.RunningSessionStateUpdate() ) assert queue.empty(), "running is not a stop" await client.session_update( session_id="s2", update=v2.schema.IdleSessionStateUpdate(stop_reason="end_turn") ) assert queue.empty(), "another session's idle must not wake ours" await client.session_update( session_id="s1", update=v2.schema.IdleSessionStateUpdate(stop_reason="cancelled") ) assert queue.get_nowait() == "cancelled"
async def test_an_idle_with_no_stop_reason_still_ends_the_wait(): """``stop_reason`` is optional in v2; omitting it means "not reporting", not "still running". A driver that waited for a value would hang.""" client = HeadlessClient() queue = client.watch("s1") await client.session_update( session_id="s1", update=v2.schema.RunningSessionStateUpdate() ) await client.session_update( session_id="s1", update=v2.schema.IdleSessionStateUpdate() ) assert queue.get_nowait() is None
async def test_updates_are_recorded_even_for_sessions_nobody_watches(): client = HeadlessClient() await client.session_update( session_id="ghost", update=v2.schema.IdleSessionStateUpdate(stop_reason="end_turn"), ) assert len(client.updates) == 1