Skip to content

Commit 1f7f18f

Browse files
authored
test(golden): add subagent and v2 replay transcript goldens (#222)
1 parent 38799b4 commit 1f7f18f

4 files changed

Lines changed: 222 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"sessionUpdate": "subagent_update",
3+
"sessionId": "child"
4+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"s1": [
3+
{
4+
"jsonrpc": "2.0",
5+
"method": "session/update",
6+
"params": {
7+
"sessionId": "s1",
8+
"update": {
9+
"sessionUpdate": "user_message",
10+
"messageId": "u1",
11+
"content": [
12+
{
13+
"type": "text",
14+
"text": "Parse the input"
15+
}
16+
]
17+
}
18+
}
19+
},
20+
{
21+
"jsonrpc": "2.0",
22+
"method": "session/update",
23+
"params": {
24+
"sessionId": "s1",
25+
"update": {
26+
"sessionUpdate": "subagent_update",
27+
"sessionId": "child",
28+
"title": "Parser"
29+
}
30+
}
31+
},
32+
{
33+
"jsonrpc": "2.0",
34+
"method": "session/update",
35+
"params": {
36+
"sessionId": "child",
37+
"update": {
38+
"sessionUpdate": "session_message",
39+
"messageId": "m1",
40+
"senderSessionId": "s1",
41+
"recipientSessionId": "child"
42+
}
43+
}
44+
},
45+
{
46+
"jsonrpc": "2.0",
47+
"method": "session/update",
48+
"params": {
49+
"sessionId": "child",
50+
"update": {
51+
"sessionUpdate": "replay_complete"
52+
}
53+
}
54+
},
55+
{
56+
"jsonrpc": "2.0",
57+
"method": "session/update",
58+
"params": {
59+
"sessionId": "s1",
60+
"update": {
61+
"sessionUpdate": "replay_complete"
62+
}
63+
}
64+
},
65+
{
66+
"jsonrpc": "2.0",
67+
"method": "session/update",
68+
"params": {
69+
"sessionId": "s1",
70+
"update": {
71+
"sessionUpdate": "state_update",
72+
"state": "idle"
73+
}
74+
}
75+
}
76+
],
77+
"empty": [
78+
{
79+
"jsonrpc": "2.0",
80+
"method": "session/update",
81+
"params": {
82+
"sessionId": "empty",
83+
"update": {
84+
"sessionUpdate": "replay_complete"
85+
}
86+
}
87+
},
88+
{
89+
"jsonrpc": "2.0",
90+
"method": "session/update",
91+
"params": {
92+
"sessionId": "empty",
93+
"update": {
94+
"sessionUpdate": "state_update",
95+
"state": "idle"
96+
}
97+
}
98+
}
99+
],
100+
"plain": [
101+
{
102+
"jsonrpc": "2.0",
103+
"method": "session/update",
104+
"params": {
105+
"sessionId": "plain",
106+
"update": {
107+
"sessionUpdate": "state_update",
108+
"state": "idle"
109+
}
110+
}
111+
}
112+
],
113+
"rejected": []
114+
}

‎tests/test_golden.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88
from pydantic import BaseModel
99

10+
from acp.experimental import v2
1011
from acp.schema import (
1112
AgentMessageChunk,
1213
AgentPlanUpdate,
@@ -32,8 +33,10 @@
3233
RequestPermissionRequest,
3334
RequestPermissionResponse,
3435
ResourceContentBlock,
36+
SessionNotification,
3537
SessionUpdateCompactionSummaryChunk,
3638
SessionUpdateCompactionUpdate,
39+
SessionUpdateSubagentUpdate,
3740
SetSessionConfigOptionResponse,
3841
SetSessionConfigOptionSelectRequest,
3942
TerminalToolCallContent,
@@ -75,6 +78,7 @@
7578
"session_update_compaction_update": SessionUpdateCompactionUpdate,
7679
"session_update_config_option_update": ConfigOptionUpdate,
7780
"session_update_plan": AgentPlanUpdate,
81+
"session_update_subagent_update": SessionUpdateSubagentUpdate,
7882
"session_update_tool_call": ToolCallStart,
7983
"session_update_tool_call_edit": ToolCallStart,
8084
"session_update_tool_call_locations_rawinput": ToolCallStart,
@@ -168,6 +172,7 @@
168172
status="completed",
169173
summary=[TextContentBlock(text="Earlier context summarized.")],
170174
),
175+
"session_update_subagent_update": lambda: SessionUpdateSubagentUpdate(session_id="child"),
171176
"session_update_tool_call": lambda: ToolCallStart(
172177
tool_call_id="call_001",
173178
title="Reading configuration file",
@@ -257,6 +262,29 @@ def test_models_match_golden(name: str, builder: Callable[[], BaseModel]) -> Non
257262
assert _dump_model(model) == raw
258263

259264

265+
@pytest.mark.parametrize(
266+
"notification",
267+
[
268+
SessionNotification(session_id="parent", update=SessionUpdateSubagentUpdate(session_id="child")),
269+
v2.schema.UpdateSessionNotification(
270+
session_id="parent", update=v2.schema.SessionSubagentUpdate(session_id="child")
271+
),
272+
],
273+
ids=["v1", "v2"],
274+
)
275+
def test_parent_addressed_subagent_update_golden(
276+
notification: SessionNotification | v2.schema.UpdateSessionNotification,
277+
) -> None:
278+
wire = _dump_model(notification)
279+
assert wire == {"sessionId": "parent", "update": _load_golden("session_update_subagent_update")}
280+
281+
decoded = type(notification).model_validate(wire)
282+
assert decoded.session_id == "parent"
283+
assert type(decoded.update) is type(notification.update)
284+
assert isinstance(decoded.update, SessionUpdateSubagentUpdate | v2.schema.SessionSubagentUpdate)
285+
assert decoded.update.session_id == "child"
286+
287+
260288
def test_generated_field_descriptions_are_introspectable() -> None:
261289
path_description = "Absolute path to the file to read."
262290
assert ReadTextFileRequest.model_fields["path"].description == path_description

‎tests/test_v2_runtime.py‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import asyncio
44
import json
55
import socket
6+
from pathlib import Path
67
from typing import Any
78
from unittest.mock import patch
89

@@ -181,6 +182,81 @@ def initialize_request(protocol_version: int = v2.PROTOCOL_VERSION) -> v2.schema
181182
)
182183

183184

185+
@pytest.mark.asyncio
186+
async def test_v2_resume_replay_transcript_golden() -> None:
187+
unsupported_cursor = v2.schema.OtherReplayFrom.model_validate({"type": "_cursor", "after": "u1"})
188+
189+
class ReplayAgent(Agent):
190+
def on_connect(self, connection: v2.AgentSideConnection) -> None:
191+
self.connection = connection
192+
193+
async def resume_session(
194+
self,
195+
session_id: str,
196+
cwd: str,
197+
replay_from: v2.schema.ReplayFromStartVariant | v2.schema.OtherReplayFrom | None = None,
198+
**kwargs: Any,
199+
) -> v2.schema.ResumeSessionResponse:
200+
assert cwd == "/work"
201+
if isinstance(replay_from, v2.schema.OtherReplayFrom):
202+
assert replay_from == unsupported_cursor
203+
raise RequestError.invalid_params()
204+
205+
if isinstance(replay_from, v2.schema.ReplayFromStartVariant):
206+
if session_id == "s1":
207+
await self.connection.session_update(
208+
session_id=session_id,
209+
update=v2.schema.UserMessageUpdate(
210+
message_id="u1", content=[v2.schema.TextContentBlock(text="Parse the input")]
211+
),
212+
)
213+
await self.connection.session_update(
214+
session_id=session_id,
215+
update=v2.schema.SessionSubagentUpdate(session_id="child", title="Parser"),
216+
)
217+
await self.connection.session_update(
218+
session_id="child",
219+
update=v2.schema.SessionMessageUpdate(
220+
message_id="m1", sender_session_id=session_id, recipient_session_id="child"
221+
),
222+
)
223+
await self.connection.session_update(session_id="child", update=v2.schema.SessionReplayComplete())
224+
await self.connection.session_update(session_id=session_id, update=v2.schema.SessionReplayComplete())
225+
226+
await self.connection.session_update(session_id=session_id, update=v2.schema.IdleSessionStateUpdate())
227+
return v2.schema.ResumeSessionResponse()
228+
229+
wire: list[dict[str, Any]] = []
230+
231+
def observe(event: StreamEvent) -> None:
232+
if event.direction == StreamDirection.INCOMING and event.message.get("method") == "session/update":
233+
wire.append(event.message)
234+
235+
fixture = Path(__file__).parent / "golden" / "v2" / "resume_replay_transcript.json"
236+
expected = json.loads(fixture.read_text(encoding="utf-8"))
237+
client_transport, agent_transport = memory_transport_pair()
238+
async with (
239+
v2.AgentSideConnection(ReplayAgent(), agent_transport),
240+
v2.ClientSideConnection(SessionClient(), client_transport, observers=[observe]) as client,
241+
):
242+
await client.initialize(v2.PROTOCOL_VERSION, initialize_request().info)
243+
244+
for session_id in ("s1", "empty"):
245+
await client.resume_session(
246+
session_id=session_id, cwd="/work", replay_from=v2.schema.ReplayFromStartVariant()
247+
)
248+
assert wire == expected[session_id]
249+
wire.clear()
250+
251+
await client.resume_session(session_id="plain", cwd="/work")
252+
assert wire == expected["plain"]
253+
wire.clear()
254+
255+
with pytest.raises(RequestError, check=lambda error: error.code == -32602):
256+
await client.resume_session(session_id="rejected", cwd="/work", replay_from=unsupported_cursor)
257+
assert wire == expected["rejected"]
258+
259+
184260
@pytest.mark.asyncio
185261
async def test_v2_runtime_initializes_and_routes_generated_models() -> None:
186262
client_transport, agent_transport = memory_transport_pair()

0 commit comments

Comments
 (0)