|
3 | 3 | import asyncio |
4 | 4 | import json |
5 | 5 | import socket |
| 6 | +from pathlib import Path |
6 | 7 | from typing import Any |
7 | 8 | from unittest.mock import patch |
8 | 9 |
|
@@ -181,6 +182,81 @@ def initialize_request(protocol_version: int = v2.PROTOCOL_VERSION) -> v2.schema |
181 | 182 | ) |
182 | 183 |
|
183 | 184 |
|
| 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 | + |
184 | 260 | @pytest.mark.asyncio |
185 | 261 | async def test_v2_runtime_initializes_and_routes_generated_models() -> None: |
186 | 262 | client_transport, agent_transport = memory_transport_pair() |
|
0 commit comments