diff --git a/tests/golden/session_update_subagent_update.json b/tests/golden/session_update_subagent_update.json new file mode 100644 index 00000000..b93463f2 --- /dev/null +++ b/tests/golden/session_update_subagent_update.json @@ -0,0 +1,4 @@ +{ + "sessionUpdate": "subagent_update", + "sessionId": "child" +} diff --git a/tests/golden/v2/resume_replay_transcript.json b/tests/golden/v2/resume_replay_transcript.json new file mode 100644 index 00000000..aa3cb0e3 --- /dev/null +++ b/tests/golden/v2/resume_replay_transcript.json @@ -0,0 +1,114 @@ +{ + "s1": [ + { + "jsonrpc": "2.0", + "method": "session/update", + "params": { + "sessionId": "s1", + "update": { + "sessionUpdate": "user_message", + "messageId": "u1", + "content": [ + { + "type": "text", + "text": "Parse the input" + } + ] + } + } + }, + { + "jsonrpc": "2.0", + "method": "session/update", + "params": { + "sessionId": "s1", + "update": { + "sessionUpdate": "subagent_update", + "sessionId": "child", + "title": "Parser" + } + } + }, + { + "jsonrpc": "2.0", + "method": "session/update", + "params": { + "sessionId": "child", + "update": { + "sessionUpdate": "session_message", + "messageId": "m1", + "senderSessionId": "s1", + "recipientSessionId": "child" + } + } + }, + { + "jsonrpc": "2.0", + "method": "session/update", + "params": { + "sessionId": "child", + "update": { + "sessionUpdate": "replay_complete" + } + } + }, + { + "jsonrpc": "2.0", + "method": "session/update", + "params": { + "sessionId": "s1", + "update": { + "sessionUpdate": "replay_complete" + } + } + }, + { + "jsonrpc": "2.0", + "method": "session/update", + "params": { + "sessionId": "s1", + "update": { + "sessionUpdate": "state_update", + "state": "idle" + } + } + } + ], + "empty": [ + { + "jsonrpc": "2.0", + "method": "session/update", + "params": { + "sessionId": "empty", + "update": { + "sessionUpdate": "replay_complete" + } + } + }, + { + "jsonrpc": "2.0", + "method": "session/update", + "params": { + "sessionId": "empty", + "update": { + "sessionUpdate": "state_update", + "state": "idle" + } + } + } + ], + "plain": [ + { + "jsonrpc": "2.0", + "method": "session/update", + "params": { + "sessionId": "plain", + "update": { + "sessionUpdate": "state_update", + "state": "idle" + } + } + } + ], + "rejected": [] +} diff --git a/tests/test_golden.py b/tests/test_golden.py index 0a289db4..d4995365 100644 --- a/tests/test_golden.py +++ b/tests/test_golden.py @@ -7,6 +7,7 @@ import pytest from pydantic import BaseModel +from acp.experimental import v2 from acp.schema import ( AgentMessageChunk, AgentPlanUpdate, @@ -32,8 +33,10 @@ RequestPermissionRequest, RequestPermissionResponse, ResourceContentBlock, + SessionNotification, SessionUpdateCompactionSummaryChunk, SessionUpdateCompactionUpdate, + SessionUpdateSubagentUpdate, SetSessionConfigOptionResponse, SetSessionConfigOptionSelectRequest, TerminalToolCallContent, @@ -75,6 +78,7 @@ "session_update_compaction_update": SessionUpdateCompactionUpdate, "session_update_config_option_update": ConfigOptionUpdate, "session_update_plan": AgentPlanUpdate, + "session_update_subagent_update": SessionUpdateSubagentUpdate, "session_update_tool_call": ToolCallStart, "session_update_tool_call_edit": ToolCallStart, "session_update_tool_call_locations_rawinput": ToolCallStart, @@ -168,6 +172,7 @@ status="completed", summary=[TextContentBlock(text="Earlier context summarized.")], ), + "session_update_subagent_update": lambda: SessionUpdateSubagentUpdate(session_id="child"), "session_update_tool_call": lambda: ToolCallStart( tool_call_id="call_001", title="Reading configuration file", @@ -257,6 +262,29 @@ def test_models_match_golden(name: str, builder: Callable[[], BaseModel]) -> Non assert _dump_model(model) == raw +@pytest.mark.parametrize( + "notification", + [ + SessionNotification(session_id="parent", update=SessionUpdateSubagentUpdate(session_id="child")), + v2.schema.UpdateSessionNotification( + session_id="parent", update=v2.schema.SessionSubagentUpdate(session_id="child") + ), + ], + ids=["v1", "v2"], +) +def test_parent_addressed_subagent_update_golden( + notification: SessionNotification | v2.schema.UpdateSessionNotification, +) -> None: + wire = _dump_model(notification) + assert wire == {"sessionId": "parent", "update": _load_golden("session_update_subagent_update")} + + decoded = type(notification).model_validate(wire) + assert decoded.session_id == "parent" + assert type(decoded.update) is type(notification.update) + assert isinstance(decoded.update, SessionUpdateSubagentUpdate | v2.schema.SessionSubagentUpdate) + assert decoded.update.session_id == "child" + + def test_generated_field_descriptions_are_introspectable() -> None: path_description = "Absolute path to the file to read." assert ReadTextFileRequest.model_fields["path"].description == path_description diff --git a/tests/test_v2_runtime.py b/tests/test_v2_runtime.py index 94d599a7..74f9d2b8 100644 --- a/tests/test_v2_runtime.py +++ b/tests/test_v2_runtime.py @@ -3,6 +3,7 @@ import asyncio import json import socket +from pathlib import Path from typing import Any from unittest.mock import patch @@ -181,6 +182,81 @@ def initialize_request(protocol_version: int = v2.PROTOCOL_VERSION) -> v2.schema ) +@pytest.mark.asyncio +async def test_v2_resume_replay_transcript_golden() -> None: + unsupported_cursor = v2.schema.OtherReplayFrom.model_validate({"type": "_cursor", "after": "u1"}) + + class ReplayAgent(Agent): + def on_connect(self, connection: v2.AgentSideConnection) -> None: + self.connection = connection + + async def resume_session( + self, + session_id: str, + cwd: str, + replay_from: v2.schema.ReplayFromStartVariant | v2.schema.OtherReplayFrom | None = None, + **kwargs: Any, + ) -> v2.schema.ResumeSessionResponse: + assert cwd == "/work" + if isinstance(replay_from, v2.schema.OtherReplayFrom): + assert replay_from == unsupported_cursor + raise RequestError.invalid_params() + + if isinstance(replay_from, v2.schema.ReplayFromStartVariant): + if session_id == "s1": + await self.connection.session_update( + session_id=session_id, + update=v2.schema.UserMessageUpdate( + message_id="u1", content=[v2.schema.TextContentBlock(text="Parse the input")] + ), + ) + await self.connection.session_update( + session_id=session_id, + update=v2.schema.SessionSubagentUpdate(session_id="child", title="Parser"), + ) + await self.connection.session_update( + session_id="child", + update=v2.schema.SessionMessageUpdate( + message_id="m1", sender_session_id=session_id, recipient_session_id="child" + ), + ) + await self.connection.session_update(session_id="child", update=v2.schema.SessionReplayComplete()) + await self.connection.session_update(session_id=session_id, update=v2.schema.SessionReplayComplete()) + + await self.connection.session_update(session_id=session_id, update=v2.schema.IdleSessionStateUpdate()) + return v2.schema.ResumeSessionResponse() + + wire: list[dict[str, Any]] = [] + + def observe(event: StreamEvent) -> None: + if event.direction == StreamDirection.INCOMING and event.message.get("method") == "session/update": + wire.append(event.message) + + fixture = Path(__file__).parent / "golden" / "v2" / "resume_replay_transcript.json" + expected = json.loads(fixture.read_text(encoding="utf-8")) + client_transport, agent_transport = memory_transport_pair() + async with ( + v2.AgentSideConnection(ReplayAgent(), agent_transport), + v2.ClientSideConnection(SessionClient(), client_transport, observers=[observe]) as client, + ): + await client.initialize(v2.PROTOCOL_VERSION, initialize_request().info) + + for session_id in ("s1", "empty"): + await client.resume_session( + session_id=session_id, cwd="/work", replay_from=v2.schema.ReplayFromStartVariant() + ) + assert wire == expected[session_id] + wire.clear() + + await client.resume_session(session_id="plain", cwd="/work") + assert wire == expected["plain"] + wire.clear() + + with pytest.raises(RequestError, check=lambda error: error.code == -32602): + await client.resume_session(session_id="rejected", cwd="/work", replay_from=unsupported_cursor) + assert wire == expected["rejected"] + + @pytest.mark.asyncio async def test_v2_runtime_initializes_and_routes_generated_models() -> None: client_transport, agent_transport = memory_transport_pair()