Something went wrong. Try again.
MCP based ACP agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109"""The `task` MCP tool — dispatch guards, no subprocess.
Driven through a REAL in-process fastmcp Client (the shape the agentuses — meta included); the live paths (launch/re-prompt/cancel drivinga real child agent) are e2e: tests/e2e/test_task_mcp_launch.py. Herethe tool talks to a REAL temp sqlite and exercises every branch thatmust be correct BEFORE any ACP traffic: owner attribution, unknownsessions, double-launch guards."""
import pytestfrom fastmcp import Client
from crow_cli.memory.db import create_database, get_enginefrom crow_cli.memory.writes import launch_task
pytestmark = pytest.mark.asyncio
OWNER = "owner-session"
@pytest.fixturedef task_env(tmp_path, monkeypatch): uri = f"sqlite:///{tmp_path / 'task-tool.db'}" create_database(uri) monkeypatch.setenv("CROW_DB_URI", uri) from crow_cli.mcp.server.app import mcp import crow_cli.mcp.task.main # noqa: F401 — registers the tool
return mcp, get_engine(uri)
async def _call(mcp, updates, owner=OWNER): async with Client(mcp) as client: kwargs = {"meta": {"session_id": owner}} if owner else {} result = await client.call_tool("task", {"updates": updates}, **kwargs) return result.data
async def test_schema_hides_attribution(task_env): """The LLM sees ONLY updates — session_id rides the call meta.""" mcp, _ = task_env async with Client(mcp) as client: tools = await client.list_tools() [tool] = [t for t in tools if t.name == "task"] assert list(tool.inputSchema.get("properties", {}).keys()) == ["updates"]
async def test_missing_owner_is_refused(task_env): mcp, _ = task_env out = await _call(mcp, [{"action": "prompt", "prompt": "x"}], owner=None) assert "no session_id" in out
async def test_cancel_of_a_session_not_live_here(task_env): mcp, _ = task_env out = await _call(mcp, [{"action": "cancel", "session_id": "ghost-session"}]) assert "not live" in out
async def test_reprompt_of_an_unknown_session(task_env): mcp, _ = task_env out = await _call( mcp, [{"action": "prompt", "prompt": "again", "session_id": "ghost-session"}], ) assert "no task owns" in out
async def test_reprompt_refuses_a_running_task(task_env): """A task row still marked running (its _watch driver lives elsewhere or died uncleanly) must not be double-driven from this process.""" mcp, engine = task_env launch_task( engine, task_id="task-1", owner_session=OWNER, sub_session="busy-session", ) out = await _call( mcp, [{"action": "prompt", "prompt": "again", "session_id": "busy-session"}], ) assert "already running" in out
async def test_task_ids_do_not_collide_across_owners(task_env): """Regression: task-N numbering is global because the UNIQUE constraint on task_id is. A per-owner counter made every session's FIRST task compute count(own)+1 == 1 and collide with the first-ever task-1.""" from crow_cli.mcp.task.main import PromptItem, _register_task
_, engine = task_env item = PromptItem(prompt="x") assert _register_task(engine, "session-a", item) == "task-1" assert _register_task(engine, "session-b", item) == "task-2"
async def test_register_task_absorbs_id_collisions(task_env): """count()+1 can still land on a taken id (deleted rows, concurrent launches); the IntegrityError retry moves on to the next free N.""" from crow_cli.mcp.task.main import PromptItem, _register_task
_, engine = task_env launch_task(engine, task_id="task-1", owner_session="session-a") launch_task(engine, task_id="task-3", owner_session="session-a") # count() == 2 -> first candidate task-3 collides -> retry lands task-4 assert _register_task(engine, "session-b", PromptItem(prompt="x")) == "task-4"