wcode_harness/protocol.rs
1//! The kernel's request/event interface — one protocol for every peer.
2//!
3//! The kernel already exposes one half of a protocol: [`AgentEvent`] is a
4//! typed, serializable stream of everything a session does (text deltas, tool
5//! lifecycle, turn boundaries). This module adds the inbound half, [`Request`],
6//! and a versioned, addressed envelope, [`Frame`], so that a client and a peer
7//! become the same kind of thing:
8//!
9//! - a **request** goes into a session's mailbox,
10//! - an **event** comes out of its stream.
11//!
12//! A TUI client, a script, and another session differ only in which address
13//! ([`Frame::session`]) they hold and how they render the stream — not in the
14//! protocol they speak. That is what lets the client seam and the future
15//! agent-to-agent seam share one implementation.
16//!
17//! Transport is deliberately absent: today the seam is an in-process
18//! `tokio::mpsc`; framing and sockets live above the kernel (a future
19//! `wcode-protocol` crate). This module is serde types only, so it adds no
20//! behavior — it is the shape the actor (S1) and the socket (S2) will speak.
21//!
22//! See `docs/interface-protocol-brainstorm.md` for the design and the staged
23//! path that lands these types.
24
25use serde::{Deserialize, Serialize};
26
27use crate::event::AgentEvent;
28
29/// Wire-protocol major version. Bumped only by a breaking change; additive
30/// changes leave it alone. [`Request`] already carries a [`Request::Unknown`]
31/// fallback so an older peer survives a newer request; the matching catch-all
32/// on the event side lands with the actor (S1), where an unknown event can be
33/// handled rather than silently dropped.
34pub const PROTOCOL_VERSION: u32 = 1;
35
36/// The well-known address of the human peer (brainstorm §5.2): the sender an
37/// in-process actor attributes an inbound message to until addressing lands
38/// (S4-2).
39pub const USER: &str = "user";
40
41/// Whether `request` is an A2A inbound message — the delivery verbs a peer sends
42/// (`Notify`/`Interrupt`/`Wake`), as opposed to a local command. The actor runs
43/// its `before_inbound` policy on exactly these.
44pub fn is_inbound(request: &Request) -> bool {
45 matches!(
46 request,
47 Request::Notify { .. } | Request::Interrupt { .. } | Request::Wake { .. }
48 )
49}
50
51/// A session's stable address.
52///
53/// Reuses the id already written in `SessionEntry::Header`, so a session is
54/// reachable by the same name it persists under. Transitively serialized as a
55/// bare string. Newtypes a plain `String` only to keep addressing legible at
56/// call sites — the conventions (the human is `"user"`, a peer is
57/// `"agent:<id>"`) are not encoded in the type yet.
58#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
59#[serde(transparent)]
60pub struct SessionId(pub String);
61
62impl SessionId {
63 pub fn new(id: impl Into<String>) -> Self {
64 Self(id.into())
65 }
66
67 /// The well-known address of the human peer (see [`USER`]).
68 pub fn user() -> Self {
69 Self(USER.to_string())
70 }
71
72 /// A peer session's address: `"agent:<id>"` (§5.2).
73 pub fn agent(id: impl AsRef<str>) -> Self {
74 Self(format!("agent:{}", id.as_ref()))
75 }
76
77 pub fn as_str(&self) -> &str {
78 &self.0
79 }
80}
81
82impl From<String> for SessionId {
83 fn from(id: String) -> Self {
84 Self(id)
85 }
86}
87
88impl From<&str> for SessionId {
89 fn from(id: &str) -> Self {
90 Self(id.to_string())
91 }
92}
93
94impl std::fmt::Display for SessionId {
95 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96 f.write_str(&self.0)
97 }
98}
99
100/// A session's run-state — the server-side liveness fact (bug 2).
101///
102/// The wire twin of the TUI's client-local `TeamState`: born at the actor's
103/// `run()` boundary (actor.rs), mirrored into the registry roster, and carried
104/// on [`SessionInfo`] so it RIDES the existing
105/// [`crate::event::AgentEvent::Sessions`] push — no new `Request`/`Reply`
106/// variant is needed for the query (`Request::ListSessions` already returns
107/// `Sessions`; making it `state`-aware is the whole poll path).
108///
109/// serde shape: `snake_case` — `"idle" | "running" | "done" | "failed"`.
110/// `Default = Idle`, so an older server's [`SessionInfo`] (no `state` key)
111/// still deserializes.
112#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
113#[serde(rename_all = "snake_case")]
114pub enum MemberState {
115 /// Not running and never has — a fresh worker before its first turn.
116 #[default]
117 Idle,
118 /// A run (a `Submit`/`Wake` turn) is in flight right now.
119 Running,
120 /// The last run finished normally; idle but not virgin.
121 Done,
122 /// The last run ended on a run-failure (`StopReason::Error`); sticky
123 /// across the run boundary, cleared by the next start.
124 Failed,
125}
126
127impl MemberState {
128 /// Lowercase label the roster push / `peers` listing render.
129 pub fn label(self) -> &'static str {
130 match self {
131 MemberState::Idle => "idle",
132 MemberState::Running => "running",
133 MemberState::Done => "done",
134 MemberState::Failed => "failed",
135 }
136 }
137}
138
139// Follow-on (not this change): the map from `MemberState` to the TUI's
140// `TeamState` does NOT live here — the harness must not depend on `wcode-tui`
141// (layering). That map, and the glyph styling it drives, is a separate TUI
142// change outside the roster work.
143
144#[cfg(test)]
145mod member_state_tests {
146 use super::*;
147 use serde_json::json;
148
149 #[test]
150 fn serde_is_snake_case() {
151 assert_eq!(
152 serde_json::to_value(MemberState::Running).unwrap(),
153 json!("running")
154 );
155 assert_eq!(
156 serde_json::from_str::<MemberState>("\"failed\"").unwrap(),
157 MemberState::Failed
158 );
159 assert_eq!(MemberState::Done.label(), "done");
160 }
161
162 #[test]
163 fn absent_state_field_defaults_to_idle() {
164 let info: SessionInfo = serde_json::from_value(json!({"id": "agent:w1"})).unwrap();
165 assert_eq!(info.id, SessionId::agent("w1"));
166 assert_eq!(info.state, MemberState::Idle);
167 }
168}
169/// A served session's address plus the little metadata a roster shows: today
170/// only its **effective model**. Carried by
171/// [`crate::event::AgentEvent::Sessions`], so a client can label each member by
172/// its own model instead of substituting the root's.
173#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
174pub struct SessionInfo {
175 pub id: SessionId,
176 /// The session's effective model id, when the server knows one (a worker
177 /// registered with its model); `None` for a peer whose model is unknown.
178 #[serde(default, skip_serializing_if = "Option::is_none")]
179 pub model: Option<String>,
180 /// Server-side liveness (see [`MemberState`] above). `#[serde(default)]`
181 /// keeps an older peer's `SessionInfo` (no `state` key) round-tripping as
182 /// `Idle`.
183 #[serde(default)]
184 pub state: MemberState,
185}
186
187/// Inbound intent: the requests a session's mailbox accepts.
188///
189/// Every variant maps one-to-one onto an existing [`crate::agent::Agent`]
190/// method, so the current session behavior is expressible without the actor
191/// inventing anything new:
192///
193/// | `Request` | today |
194/// |-----------|-------|
195/// | `Submit` | `Agent::run` |
196/// | `Notify` | `Agent::notify` (append, no turn) |
197/// | `Interrupt` | `Agent::steer` |
198/// | `Wake` | run — a turn even when idle |
199/// | `Cancel` | `Agent::cancel` |
200/// | `SetModel` | `Agent::set_model` |
201/// | `SetEffort` | `Agent::set_effort` |
202/// | `Compact` | `Agent::compact` |
203///
204/// The one exception is [`Request::ListSessions`]: a transport-level probe the
205/// server answers, not an `Agent` method.
206///
207/// Inbound payloads carry user **content**, not an `AgentMessage`: a remote peer
208/// must not be able to inject an `Assistant`/`ToolResult` message and impersonate
209/// the model. The session wraps content as a user message, exactly as
210/// `Agent::run` does. `GetHistory` is a read: it carries no payload and its
211/// answer comes back as [`AgentEvent::History`]
212/// (with the other replies — [`AgentEvent::Ack`],
213/// and [`AgentEvent::Error`] — alongside it).
214/// These reply variants are never *streamed*: they answer a specific request,
215/// which the transport correlates (an in-process oneshot today, the envelope's
216/// `reply_to` on the wire).
217#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
218#[serde(tag = "type", rename_all = "snake_case")]
219pub enum Request {
220 /// Start a turn: persist the text as a user message and run the loop until
221 /// it stops. The run's progress and result stream back as [`AgentEvent`]s.
222 Submit { text: String },
223
224 /// Append `content` to the conversation *without* starting a turn (recorded
225 /// now if idle, or at the next turn boundary if a run is in flight). The
226 /// "tell, don't ask" delivery mode — `Agent::notify`.
227 Notify { content: String },
228
229 /// Inject `content` at the next turn boundary of the in-flight run — a soft
230 /// interrupt that does not cancel the run. Formerly `Steer`; `"steer"`
231 /// still deserializes here.
232 #[serde(alias = "steer")]
233 Interrupt { content: String },
234
235 /// Wake the session: run `content` as a turn **even if it is idle** (mid-run
236 /// it rides the follow-up channel instead). Formerly `FollowUp`;
237 /// `"follow_up"` still deserializes here.
238 /// `"follow_up"` still deserializes here.
239 #[serde(alias = "follow_up")]
240 Wake { content: String },
241
242 /// Cancel the in-flight run (and any retry/backoff wait). Idempotent.
243 Cancel,
244
245 /// Swap the model for subsequent turns.
246 SetModel { model: String },
247
248 /// Set the reasoning effort, or clear it (`None` = send nothing).
249 SetEffort { effort: Option<String> },
250
251 /// Summarize the older prefix of the conversation now, optionally focused
252 /// by `instructions`.
253 Compact { instructions: Option<String> },
254
255 /// Toggle plan mode. The actor flips the agent's shared `PlanModeHandle`
256 /// (removing mutating tools + mutating `bash`) and recomposes the system
257 /// prompt. Infallible — replies [`AgentEvent::Ack`].
258 SetPlanMode { on: bool },
259
260 /// Ask a side question (`/btw`): answer `text` tool-free, from the current
261 /// context, WITHOUT recording it. A read — carries user content, names no
262 /// `AgentMessage` (see the enum doc above). Reply:
263 /// [`AgentEvent::SideAnswer`]. A `SideAsk` arriving mid-run is answered from
264 /// the latest turn-boundary snapshot (like `GetHistory`), not refused.
265 SideAsk { text: String },
266 /// Read back the conversation so far. Reply:
267 /// [`AgentEvent::History`].
268 GetHistory,
269
270 /// A compact read of the session's latest activity — the text of its most
271 /// recent assistant message — WITHOUT shipping the transcript (unlike
272 /// [`Request::GetHistory`]). A `member`-style peek: the reply is one small
273 /// string, not the whole history. Reply: [`AgentEvent::Status`].
274 Status,
275
276 /// Ask the **transport** for the sessions a server serves — serve order,
277 /// root first. The one request that names no `Agent` method: an in-process
278 /// session has no roster, so the actor answers it with [`AgentEvent::Ack`]
279 /// (like [`Request::Unknown`]); a socket server intercepts it and replies
280 /// [`AgentEvent::Sessions`].
281 ListSessions,
282
283 /// Define a worker **on a served peer** (§8, S4). Like [`Request::ListSessions`]
284 /// it names no `Agent` method: a socket server intercepts it and asks an
285 /// injected handler to build a session, replying [`AgentEvent::Spawned`] with
286 /// its address (or a correlated [`AgentEvent::Error`]). Mirrors the CLI's
287 /// worker spec, so a peer can customize the worker's model, role, tool set,
288 /// `read_only`, `effort`, and provider. An in-process session has no
289 /// factory, so the actor answers
290 /// it with [`AgentEvent::Error`].
291 Define {
292 /// The worker's address; auto-assigned (`w1`, `w2`, …) when absent.
293 name: Option<String>,
294 /// Model id override; `None` inherits the server's model.
295 model: Option<String>,
296 /// Role text appended to the worker's system prompt.
297 role: Option<String>,
298 /// Tool allow-list; `None` = the full default set.
299 tools: Option<Vec<String>>,
300 /// Provider base URL override; `None` inherits the server's.
301 base_url: Option<String>,
302 /// Provider API key override; `None` inherits the server's.
303 api_key: Option<String>,
304 /// Enforce read-only for the defined worker (D1/D2); default false.
305 #[serde(default)]
306 read_only: bool,
307 /// Reasoning-effort override (D3); `None` inherits, synonyms clear.
308 #[serde(default)]
309 effort: Option<String>,
310 },
311
312 /// Forward-compatibility catch-all: an older peer must skip a request it
313 /// does not understand rather than fail the connection.
314 #[serde(other)]
315 Unknown,
316}
317
318/// Versioned, addressed envelope around a [`Request`] or an [`AgentEvent`].
319///
320/// Generic over the body so the same frame carries either direction: the
321/// request-to-a-session frame and the event-from-a-session frame have identical
322/// headers. The body is `#[serde(flatten)]`ed, so on the wire a frame is one
323/// flat JSON object — `{"v":1,"id":7,"session":"s1","type":"cancel"}`.
324#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
325pub struct Frame<P> {
326 /// Protocol major version ([`PROTOCOL_VERSION`]).
327 pub v: u32,
328 /// Sender-assigned id, monotonic per connection; answers echo it in
329 /// `reply_to`.
330 pub id: u64,
331 /// The request this frame answers, if any. Streaming events that are not
332 /// direct replies leave it absent.
333 #[serde(default, skip_serializing_if = "Option::is_none")]
334 pub reply_to: Option<u64>,
335 /// The session this frame concerns — the target of a request, the origin of
336 /// an event. The whole addressing scheme.
337 pub session: SessionId,
338 /// The sender's address, when it is not the transport's own client (an
339 /// in-process handle leaves it unset; an A2A peer sets its `"agent:<id>"`).
340 #[serde(default, skip_serializing_if = "Option::is_none")]
341 pub sender: Option<SessionId>,
342 #[serde(flatten)]
343 pub body: P,
344}
345
346impl<P> Frame<P> {
347 /// A fresh top-level frame at the current version, not a reply.
348 pub fn new(id: u64, session: SessionId, body: P) -> Self {
349 Self {
350 v: PROTOCOL_VERSION,
351 id,
352 reply_to: None,
353 session,
354 sender: None,
355 body,
356 }
357 }
358}
359
360/// A client-to-session frame.
361pub type RequestFrame = Frame<Request>;
362/// A session-to-client frame.
363pub type EventFrame = Frame<AgentEvent>;
364
365#[cfg(test)]
366mod tests {
367 use super::*;
368 use crate::event::{TodoItem, TodoStatus};
369 use serde::de::DeserializeOwned;
370 use serde_json::json;
371
372 fn roundtrip<P>(p: P, expected_tag: &str)
373 where
374 P: Serialize + DeserializeOwned + PartialEq + std::fmt::Debug,
375 {
376 let v = serde_json::to_value(&p).unwrap();
377 assert_eq!(v["type"], expected_tag);
378 let back: P = serde_json::from_value(v).unwrap();
379 assert_eq!(back, p);
380 }
381
382 #[test]
383 fn request_roundtrip_all_variants() {
384 roundtrip(Request::Submit { text: "hi".into() }, "submit");
385 roundtrip(Request::Cancel, "cancel");
386 roundtrip(Request::SetModel { model: "m1".into() }, "set_model");
387 roundtrip(
388 Request::SetEffort {
389 effort: Some("high".into()),
390 },
391 "set_effort",
392 );
393 roundtrip(Request::SetEffort { effort: None }, "set_effort");
394 roundtrip(
395 Request::Compact {
396 instructions: Some("focus on the parser".into()),
397 },
398 "compact",
399 );
400 roundtrip(Request::Compact { instructions: None }, "compact");
401 roundtrip(Request::SetPlanMode { on: true }, "set_plan_mode");
402 roundtrip(Request::Notify { content: "n".into() }, "notify");
403 roundtrip(Request::Interrupt { content: "i".into() }, "interrupt");
404 roundtrip(Request::Wake { content: "w".into() }, "wake");
405 roundtrip(Request::SideAsk { text: "why?".into() }, "side_ask");
406 roundtrip(Request::Status, "status");
407 roundtrip(Request::ListSessions, "list_sessions");
408 roundtrip(
409 Request::Define {
410 name: Some("w1".into()),
411 model: Some("m".into()),
412 role: Some("reviewer".into()),
413 tools: Some(vec!["read".into()]),
414 base_url: Some("http://w/v1".into()),
415 api_key: Some("k".into()),
416 read_only: true,
417 effort: Some("high".into()),
418 },
419 "define",
420 );
421 roundtrip(
422 Request::Define {
423 name: None,
424 model: None,
425 role: None,
426 tools: None,
427 base_url: None,
428 api_key: None,
429 read_only: false,
430 effort: None,
431 },
432 "define",
433 );
434 }
435
436 #[test]
437 fn legacy_verb_tags_deserialize_to_canonical_variants() {
438 let steer: Request = serde_json::from_str(r#"{"type":"steer","content":"s"}"#).unwrap();
439 assert_eq!(steer, Request::Interrupt { content: "s".into() });
440 let follow: Request = serde_json::from_str(r#"{"type":"follow_up","content":"f"}"#).unwrap();
441 assert_eq!(follow, Request::Wake { content: "f".into() });
442 }
443
444 #[test]
445 fn request_unknown_tag_is_caught_not_fatal() {
446 let back: Request = serde_json::from_str(r#"{"type":"from_the_future"}"#).unwrap();
447 assert_eq!(back, Request::Unknown);
448 }
449
450 #[test]
451 fn is_inbound_marks_only_the_delivery_verbs() {
452 assert!(is_inbound(&Request::Notify { content: "x".into() }));
453 assert!(is_inbound(&Request::Interrupt { content: "x".into() }));
454 assert!(is_inbound(&Request::Wake { content: "x".into() }));
455 assert!(!is_inbound(&Request::Submit { text: "x".into() }));
456 assert!(!is_inbound(&Request::GetHistory));
457 assert!(!is_inbound(&Request::Status));
458 assert!(!is_inbound(&Request::SideAsk { text: "x".into() }));
459 assert!(!is_inbound(&Request::SetPlanMode { on: true }));
460 assert!(!is_inbound(&Request::Cancel));
461 assert!(!is_inbound(&Request::ListSessions));
462 assert!(!is_inbound(&Request::Define {
463 name: None,
464 model: None,
465 role: None,
466 tools: None,
467 base_url: None,
468 api_key: None,
469 read_only: false,
470 effort: None,
471 }));
472 }
473
474 #[test]
475 fn frame_flattens_body_and_omits_absent_reply_to() {
476 let f = Frame::new(7, SessionId::new("s1"), Request::Cancel);
477 let v = serde_json::to_value(&f).unwrap();
478 assert_eq!(
479 v,
480 json!({"v": PROTOCOL_VERSION, "id": 7, "session": "s1", "type": "cancel"})
481 );
482 let back: Frame<Request> = serde_json::from_value(v).unwrap();
483 assert_eq!(back, f);
484 }
485
486 #[test]
487 fn frame_carries_reply_to_and_an_event_body() {
488 let f = EventFrame {
489 v: PROTOCOL_VERSION,
490 id: 8,
491 reply_to: Some(7),
492 session: SessionId::from("s1"),
493 sender: None,
494 body: AgentEvent::AgentEnd,
495 };
496 let v = serde_json::to_value(&f).unwrap();
497 assert_eq!(
498 v,
499 json!({"v": PROTOCOL_VERSION, "id": 8, "reply_to": 7, "session": "s1", "type": "agent_end"})
500 );
501 // `AgentEvent` is not `PartialEq` (see event.rs); compare re-serialized
502 // JSON, matching that module's round-trip convention.
503 let back: EventFrame = serde_json::from_value(v.clone()).unwrap();
504 assert_eq!(serde_json::to_value(&back).unwrap(), v);
505 }
506
507 #[test]
508 fn frame_carries_a_todo_event_body() {
509 let f = EventFrame {
510 v: PROTOCOL_VERSION,
511 id: 9,
512 reply_to: None,
513 session: SessionId::from("s1"),
514 sender: None,
515 body: AgentEvent::Todo {
516 todos: vec![TodoItem {
517 content: "write tests".into(),
518 status: TodoStatus::Pending,
519 }],
520 },
521 };
522 let v = serde_json::to_value(&f).unwrap();
523 assert_eq!(
524 v,
525 json!({
526 "v": PROTOCOL_VERSION, "id": 9, "session": "s1", "type": "todo",
527 "todos": [{"content": "write tests", "status": "pending"}],
528 })
529 );
530 let back: EventFrame = serde_json::from_value(v.clone()).unwrap();
531 assert_eq!(serde_json::to_value(&back).unwrap(), v);
532 }
533
534 #[test]
535 fn session_id_serializes_transparently() {
536 assert_eq!(
537 serde_json::to_value(SessionId::new("abc")).unwrap(),
538 json!("abc")
539 );
540 assert_eq!(SessionId::new("abc").as_str(), "abc");
541 assert_eq!(SessionId::new("abc").to_string(), "abc");
542 assert_eq!(SessionId::from("x"), SessionId(String::from("x")));
543 assert_eq!(SessionId::user().as_str(), USER);
544 assert_eq!(SessionId::agent("abc").as_str(), "agent:abc");
545 }
546}