Something went wrong. Try again.
MCP based ACP agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427"""react.py unit tests — the crown jewel's inner ring.
Covers the pure helpers, the send_request retry matrix (real openaiexception types, scripted transport), process_response chunk logging andcancel-time state, and execute_tool_calls error paths. The wire-level loopbehaviors live in tests/integration/test_react_loop_*.py."""
import asyncioimport jsonimport loggingfrom dataclasses import dataclassfrom types import SimpleNamespace
import httpximport pytestfrom mcp.types import TextContentfrom openai import ( APIConnectionError, APIError, AuthenticationError, BadRequestError, InternalServerError, RateLimitError,)from openai._exceptions import APITimeoutError
from crow_cli.agent.context import TurnCtxfrom crow_cli.agent.react import ( TOOL_CALL_CANCELLED_MESSAGE, _is_transient_provider_400, cancelled_tool_results, execute_tool_calls, process_response, send_request, session_from_agent_id,)
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------# Chunk fakes (same shape as the integration helpers; kept local so the unit# tier has no dependency on the integration module).# ---------------------------------------------------------------------------
@dataclassclass MockDelta: reasoning_content: str | None = None content: str | None = None tool_calls: list | None = None
@dataclassclass MockChoice: delta: MockDelta finish_reason: str | None = None
@dataclassclass MockChunk: choices: list usage: object | None = None
def content_chunk(text: str) -> MockChunk: return MockChunk(choices=[MockChoice(delta=MockDelta(content=text))])
def usage_chunk(total_tokens: int = 100) -> MockChunk: usage = SimpleNamespace( prompt_tokens=total_tokens // 2, completion_tokens=total_tokens // 2, total_tokens=total_tokens, ) return MockChunk(choices=[], usage=usage)
async def fake_stream(chunks: list, hang_after: bool = False): for chunk in chunks: yield chunk if hang_after: await asyncio.Event().wait()
class ScriptedLLM: """chat.completions.create plays a script: Exception instances raise, lists of chunks return as a stream. Counts attempts."""
def __init__(self, script: list): self.script = list(script) self.attempts = 0 self.create_kwargs = None outer = self
class Completions: async def create(self, **kwargs): outer.attempts += 1 outer.create_kwargs = kwargs action = outer.script.pop(0) if isinstance(action, BaseException): raise action return fake_stream(action)
class Chat: completions = Completions()
self.chat = Chat()
def fake_session(messages=None): return SimpleNamespace( messages=messages or [{"role": "user", "content": "hi"}], model_identifier="test-model", )
def _http_req(): return httpx.Request("POST", "https://provider.test/chat/completions")
def _api_error(status: int, body) -> APIError: """Real openai status exceptions (BadRequestError, ...) carry the status_code attribute the retry logic keys on.""" req = _http_req() resp = httpx.Response(status, request=req) cls = { 400: BadRequestError, 401: AuthenticationError, 429: RateLimitError, 500: InternalServerError, }[status] return cls("provider error", response=resp, body=body)
async def _send(llm, **kw): defaults = dict( session=fake_session(), tools=[], max_tokens=64, max_retries=3, retry_delay=0, config=None, ) defaults.update(kw) return await send_request(llm, **defaults)
# ---------------------------------------------------------------------------# helpers# ---------------------------------------------------------------------------
def test_session_from_agent_id(): # trunk routes to the bare session_id; a fork keeps its agent_id (wire id) assert session_from_agent_id("s-1-1") == "s" assert session_from_agent_id("cool-name-3-1") == "cool-name" assert session_from_agent_id("cool-name-3-2") == "cool-name-3-2"
def test_cancelled_tool_results_one_per_call(): results = cancelled_tool_results([{"id": "a"}, {"id": "b"}]) assert [r["tool_call_id"] for r in results] == ["a", "b"] assert all(r["role"] == "tool" for r in results) assert all(r["content"] == TOOL_CALL_CANCELLED_MESSAGE for r in results)
def test_cancelled_tool_results_empty(): assert cancelled_tool_results([]) == []
# ---------------------------------------------------------------------------# transient-400 detection# ---------------------------------------------------------------------------
def test_transient_400_marker_detected(): e = _api_error(400, {"error": {"message": "Download multimodal file timed out"}}) assert _is_transient_provider_400(e) is True
def test_plain_400_not_transient(): e = _api_error(400, {"error": {"message": "invalid model name"}}) assert _is_transient_provider_400(e) is False
def test_500_with_marker_is_not_a_transient_400(): e = _api_error(500, {"error": {"message": "ingest timed out"}}) assert _is_transient_provider_400(e) is False
def test_unserializable_body_does_not_crash(): e = _api_error(400, object()) assert _is_transient_provider_400(e) is False
# ---------------------------------------------------------------------------# send_request retry matrix# ---------------------------------------------------------------------------
@pytest.mark.asyncioasync def test_timeout_retried_then_succeeds(): req = _http_req() llm = ScriptedLLM( [APITimeoutError(request=req), APITimeoutError(request=req), [content_chunk("ok")]] ) await _send(llm) assert llm.attempts == 3
@pytest.mark.asyncioasync def test_rate_limit_retried_then_succeeds(): llm = ScriptedLLM([_api_error(429, None), [content_chunk("ok")]]) await _send(llm) assert llm.attempts == 2
@pytest.mark.asyncioasync def test_connection_error_retried_then_succeeds(): llm = ScriptedLLM( [APIConnectionError(request=_http_req()), [content_chunk("ok")]] ) await _send(llm) assert llm.attempts == 2
@pytest.mark.asyncioasync def test_5xx_exhausts_retries_and_raises(): llm = ScriptedLLM([_api_error(500, None)] * 3) with pytest.raises(APIError): await _send(llm) assert llm.attempts == 3
@pytest.mark.asyncioasync def test_non_retryable_401_raises_immediately(): llm = ScriptedLLM([_api_error(401, {"error": "bad key"}), [content_chunk("never")]]) with pytest.raises(APIError): await _send(llm) assert llm.attempts == 1
@pytest.mark.asyncioasync def test_transient_400_retried_then_succeeds(): t400 = _api_error(400, {"error": {"message": "Download multimodal file timed out"}}) llm = ScriptedLLM([t400, t400, [content_chunk("ok")]]) await _send(llm) assert llm.attempts == 3
@pytest.mark.asyncioasync def test_plain_400_raises_immediately(): llm = ScriptedLLM([_api_error(400, {"error": {"message": "bad request"}})]) with pytest.raises(APIError): await _send(llm) assert llm.attempts == 1
@pytest.mark.asyncioasync def test_cancellation_is_not_retried(): llm = ScriptedLLM([asyncio.CancelledError(), [content_chunk("never")]]) with pytest.raises(asyncio.CancelledError): await _send(llm) assert llm.attempts == 1
@pytest.mark.asyncioasync def test_request_carries_stream_and_usage_options(): llm = ScriptedLLM([[content_chunk("ok")]]) await _send(llm) kw = llm.create_kwargs assert kw["stream"] is True assert kw["stream_options"] == {"include_usage": True} # False ON THE WIRE, not omitted: OpenAI's own default is true, so a # disabled flag has to be sent explicitly or the provider enables it. assert kw["parallel_tool_calls"] is False # config=None fallback: plain temperature, no reasoning_effort assert kw["temperature"] == 0.6 assert "reasoning_effort" not in kw
# ---------------------------------------------------------------------------# process_response: chunk log + cancel-time state# ---------------------------------------------------------------------------
@pytest.mark.asyncioasync def test_process_response_writes_chunk_log(tmp_path): chunks = [content_chunk("a"), content_chunk("b"), usage_chunk()] log_path = tmp_path / "chunks.jsonl" acc = {} finals = [] async for msg_type, token in process_response( fake_stream(chunks), acc, chunk_log_path=str(log_path) ): if msg_type == "final": finals.append(token) lines = [json.loads(line) for line in log_path.read_text().splitlines()] assert [l["chunk_index"] for l in lines] == [1, 2, 3] assert all("chunk" in l and "msg_types" in l for l in lines) # every logged chunk must be JSON-serializable by construction here json.dumps(lines) thinking, content, tool_call_inputs, usage = finals[0] assert "".join(content) == "ab" assert usage["total_tokens"] == 100
@pytest.mark.asyncioasync def test_process_response_cancel_keeps_partial_state(): acc = {}
async def consume(): async for _ in process_response( fake_stream([content_chunk("par")], hang_after=True), acc ): pass
task = asyncio.create_task(consume()) await asyncio.sleep(0.05) task.cancel() with pytest.raises(asyncio.CancelledError): await task assert "".join(acc["content"]) == "par" assert acc["tool_calls"] == {}
# ---------------------------------------------------------------------------# execute_tool_calls error paths# ---------------------------------------------------------------------------
class RecordingConn: def __init__(self): self.updates = []
async def session_update(self, session_id, update): self.updates.append(update)
def _tool_call(id: str, name: str, arguments: str) -> dict: return {"id": id, "type": "function", "function": {"name": name, "arguments": arguments}}
def _ctx(conn, agent_id: str = "s-1-1") -> TurnCtx: """One prompt turn for exercising execute_tool_calls in isolation.
``agent_id`` is a trunk id, so its wire sessionId is the bare "s" — which is also how these tests key mcp_clients. """ session = SimpleNamespace(agent_id=agent_id, cwd="/tmp") return TurnCtx(conn=conn, config=None, session=session, turn_id="t", logger=logger)
@pytest.mark.asyncioasync def test_malformed_tool_args_produce_error_result_and_repair_in_place(): calls = [_tool_call("c1", "search", "[1, 2]")] # valid JSON, not an object results = await execute_tool_calls( ctx=_ctx(RecordingConn()), mcp_clients={}, tool_call_inputs=calls, ) assert len(results) == 1 assert results[0]["tool_call_id"] == "c1" assert "malformed arguments" in results[0]["content"] # history is detoxified: the stored call now carries valid JSON assert calls[0]["function"]["arguments"] == "{}"
class ExplodingMCP: async def call_tool(self, name, args): raise RuntimeError("boom")
@pytest.mark.asyncioasync def test_tool_exception_yields_error_result_and_failed_progress(): conn = RecordingConn() results = await execute_tool_calls( ctx=_ctx(conn), mcp_clients={"s": ExplodingMCP()}, tool_call_inputs=[_tool_call("c1", "search", '{"q": "x"}')], ) assert len(results) == 1 assert "Error" in results[0]["content"] assert any(getattr(u, "status", None) == "failed" for u in conn.updates)
class OneThenHangMCP: """First call returns a real result, second hangs until cancelled."""
def __init__(self): self.calls = []
async def call_tool(self, name, args): self.calls.append(name) if len(self.calls) == 1: return SimpleNamespace( content=[TextContent(type="text", text="ok-1")], isError=False ) await asyncio.Event().wait()
@pytest.mark.asyncioasync def test_cancel_keeps_finished_results_and_fills_placeholders(): results: list[dict] = [] task = asyncio.create_task( execute_tool_calls( ctx=_ctx(RecordingConn()), mcp_clients={"s": OneThenHangMCP()}, tool_call_inputs=[ _tool_call("c1", "search", '{"q": "a"}'), _tool_call("c2", "search", '{"q": "b"}'), ], tool_results=results, ) ) await asyncio.sleep(0.05) task.cancel() with pytest.raises(asyncio.CancelledError): await task by_id = {r["tool_call_id"]: r for r in results} assert set(by_id) == {"c1", "c2"} assert "ok-1" in str(by_id["c1"]["content"]) assert by_id["c2"]["content"] == TOOL_CALL_CANCELLED_MESSAGE