1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//! Snapshot test for trace events from a real yopo interaction.
//!
//! This test runs yopo -> conductor (with arrow_proxy -> test_agent) and
//! captures trace events to a channel for expect_test snapshot verification.
//!
//! Run `just prep-tests` before running this test.
use agent_client_protocol_conductor::trace::TraceEvent;
use agent_client_protocol_conductor::{ConductorImpl, McpBridgeMode, ProxiesAndAgent};
use agent_client_protocol_test::test_binaries::{arrow_proxy_example, testy};
use agent_client_protocol_test::testy::TestyCommand;
use agent_client_protocol_tokio::AcpAgent;
use expect_test::expect;
use futures::StreamExt;
use futures::channel::mpsc;
use std::collections::HashMap;
use tokio::io::duplex;
use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt};
/// Normalize events for stable snapshot testing.
///
/// - Strips timestamps (set to 0.0)
/// - Replaces UUIDs with sequential IDs (id:0, id:1, etc.)
/// - Replaces session IDs with "session:0", etc.
struct EventNormalizer {
id_map: HashMap<String, String>,
next_id: usize,
session_map: HashMap<String, String>,
next_session: usize,
}
impl EventNormalizer {
fn new() -> Self {
Self {
id_map: HashMap::new(),
next_id: 0,
session_map: HashMap::new(),
next_session: 0,
}
}
fn normalize_id(&mut self, id: serde_json::Value) -> serde_json::Value {
let id_str = match &id {
serde_json::Value::String(s) => s.clone(),
serde_json::Value::Number(n) => n.to_string(),
_ => return id,
};
let normalized = self.id_map.entry(id_str).or_insert_with(|| {
let n = format!("id:{}", self.next_id);
self.next_id += 1;
n
});
serde_json::Value::String(normalized.clone())
}
fn normalize_session(&mut self, session: Option<String>) -> Option<String> {
session.map(|s| self.normalize_session_id(&s))
}
fn normalize_session_id(&mut self, session: &str) -> String {
self.session_map
.entry(session.to_string())
.or_insert_with(|| {
let n = format!("session:{}", self.next_session);
self.next_session += 1;
n
})
.clone()
}
/// Recursively normalize session IDs in JSON values.
fn normalize_json(&mut self, value: serde_json::Value) -> serde_json::Value {
match value {
serde_json::Value::Object(map) => {
let normalized: serde_json::Map<String, serde_json::Value> = map
.into_iter()
.map(|(k, v)| {
let v = if k == "sessionId" {
if let serde_json::Value::String(s) = &v {
serde_json::Value::String(self.normalize_session_id(s))
} else {
self.normalize_json(v)
}
} else {
self.normalize_json(v)
};
(k, v)
})
.collect();
serde_json::Value::Object(normalized)
}
serde_json::Value::Array(arr) => {
serde_json::Value::Array(arr.into_iter().map(|v| self.normalize_json(v)).collect())
}
other => other,
}
}
fn normalize_events(&mut self, events: Vec<TraceEvent>) -> Vec<TraceEvent> {
events
.into_iter()
.map(|event| match event {
TraceEvent::Request(mut r) => {
r.ts = 0.0;
r.id = self.normalize_id(r.id);
r.session = self.normalize_session(r.session);
r.params = self.normalize_json(r.params);
TraceEvent::Request(r)
}
TraceEvent::Response(mut r) => {
r.ts = 0.0;
r.id = self.normalize_id(r.id);
r.payload = self.normalize_json(r.payload);
TraceEvent::Response(r)
}
TraceEvent::Notification(mut n) => {
n.ts = 0.0;
n.session = self.normalize_session(n.session);
n.params = self.normalize_json(n.params);
TraceEvent::Notification(n)
}
_ => panic!("unknown trace event type"),
})
.collect()
}
}
#[tokio::test]
async fn test_trace_snapshot() -> Result<(), agent_client_protocol::Error> {
// Create channel for collecting trace events
let (tx, rx) = mpsc::unbounded();
// Create the component chain: arrow_proxy -> eliza
// Uses pre-built binaries to avoid cargo run races during `cargo test --all`
let arrow_proxy_agent =
AcpAgent::from_args([arrow_proxy_example().to_string_lossy().to_string()])?;
let eliza_agent = testy();
// Create duplex streams for editor <-> conductor communication
let (editor_write, conductor_read) = duplex(8192);
let (conductor_write, editor_read) = duplex(8192);
// Spawn the conductor with tracing to the channel
let conductor_handle = tokio::spawn(async move {
ConductorImpl::new_agent(
"conductor".to_string(),
ProxiesAndAgent::new(eliza_agent).proxy(arrow_proxy_agent),
McpBridgeMode::default(),
)
.trace_to(tx)
.run(agent_client_protocol::ByteStreams::new(
conductor_write.compat_write(),
conductor_read.compat(),
))
.await
});
// Run a simple prompt through the conductor
let result = tokio::time::timeout(std::time::Duration::from_secs(30), async move {
yopo::prompt(
agent_client_protocol::ByteStreams::new(
editor_write.compat_write(),
editor_read.compat(),
),
TestyCommand::Greet.to_prompt(),
)
.await
})
.await
.expect("Test timed out")?;
// Abort the conductor to close the trace channel
conductor_handle.abort();
// Collect and normalize events
let mut normalizer = EventNormalizer::new();
let events = normalizer.normalize_events(rx.collect().await);
// Snapshot the trace events
expect![[r#"
[
Request(
RequestEvent {
ts: 0.0,
protocol: Acp,
from: "Client",
to: "Proxy(0)",
id: String("id:0"),
method: "_proxy/initialize",
session: None,
params: Object {
"clientCapabilities": Object {
"auth": Object {
"terminal": Bool(false),
},
"fs": Object {
"readTextFile": Bool(false),
"writeTextFile": Bool(false),
},
"terminal": Bool(false),
},
"protocolVersion": Number(1),
},
},
),
Response(
ResponseEvent {
ts: 0.0,
from: "Proxy(0)",
to: "Client",
id: String("id:0"),
is_error: false,
payload: Object {
"agentCapabilities": Object {
"auth": Object {},
"loadSession": Bool(false),
"mcpCapabilities": Object {
"http": Bool(false),
"sse": Bool(false),
},
"promptCapabilities": Object {
"audio": Bool(false),
"embeddedContext": Bool(false),
"image": Bool(false),
},
"sessionCapabilities": Object {},
},
"authMethods": Array [],
"protocolVersion": Number(1),
},
},
),
Request(
RequestEvent {
ts: 0.0,
protocol: Acp,
from: "Client",
to: "Proxy(0)",
id: String("id:1"),
method: "session/new",
session: None,
params: Object {
"cwd": String("."),
"mcpServers": Array [],
},
},
),
Response(
ResponseEvent {
ts: 0.0,
from: "Proxy(0)",
to: "Client",
id: String("id:1"),
is_error: false,
payload: Object {
"sessionId": String("session:0"),
},
},
),
Request(
RequestEvent {
ts: 0.0,
protocol: Acp,
from: "Client",
to: "Proxy(0)",
id: String("id:2"),
method: "session/prompt",
session: None,
params: Object {
"prompt": Array [
Object {
"text": String("{\"command\":\"greet\"}"),
"type": String("text"),
},
],
"sessionId": String("session:0"),
},
},
),
Notification(
NotificationEvent {
ts: 0.0,
protocol: Acp,
from: "Proxy(1)",
to: "Proxy(0)",
method: "session/update",
session: None,
params: Object {
"sessionId": String("session:0"),
"update": Object {
"content": Object {
"text": String("Hello, world!"),
"type": String("text"),
},
"sessionUpdate": String("agent_message_chunk"),
},
},
},
),
Response(
ResponseEvent {
ts: 0.0,
from: "Proxy(0)",
to: "Client",
id: String("id:2"),
is_error: false,
payload: Object {
"stopReason": String("end_turn"),
},
},
),
]
"#]]
.assert_debug_eq(&events);
println!("Response: {result}");
Ok(())
}