Skip to main content

agent_core/core/
rpc_protocol.rs

1//! RPC protocol types for the `synaps-bridge` parent↔child IPC channel.
2//!
3//! # Overview
4//!
5//! The synaps-bridge RPC protocol enables a parent process to spawn a
6//! long-lived "rpc child" process and communicate with it over a pair of
7//! `stdio` pipes (child's `stdin` / `stdout`). This module defines every
8//! message type exchanged over that channel.
9//!
10//! See also: `synaps-bridge.SPEC.md §4` (path:
11//! `/home/jr/Projects/Maha-Media/synaps-bridge.SPEC.md`).
12//!
13//! # Framing
14//!
15//! * **Encoding:** UTF-8, line-delimited JSON (LDJSON / NDJSON).
16//! * **One frame per line:** each JSON object is terminated by a single `\n`
17//!   (`0x0A`). No `Content-Length` header or other envelope.
18//! * **Max frame size:** 1 MiB (1 048 576 bytes). Frames that exceed this
19//!   limit are considered malformed. The rpc child must emit an
20//!   [`RpcEvent::Error`] with `id: None` and remain alive when it encounters
21//!   an oversized inbound frame. Enforcement logic lives in Task 2.
22//! * **Direction:** the parent writes [`RpcCommand`] frames to the child's
23//!   `stdin`; the child writes [`RpcEvent`] frames to its `stdout`.
24//!
25//! # Version semantics
26//!
27//! The current protocol version is [`RPC_PROTOCOL_VERSION`] = `1`. The child
28//! emits [`RpcEvent::Ready`] immediately after startup, advertising its
29//! `protocol_version`. The parent must refuse to proceed if the version does
30//! not match its own expectation.
31//!
32//! # Correlation
33//!
34//! Every [`RpcCommand`] variant **except** [`RpcCommand::Shutdown`] carries
35//! an `id: String` field. The rpc child echoes the same `id` in the
36//! corresponding [`RpcEvent::Response`] frame, allowing the parent to
37//! correlate requests and responses. The `id` format is opaque to the child
38//! (UUID, monotonic counter, or any other string).
39
40use serde::{Deserialize, Serialize};
41
42// ---------------------------------------------------------------------------
43// Protocol version
44// ---------------------------------------------------------------------------
45
46/// Wire-format protocol version.  Both sides must agree on this value;
47/// the child advertises it in its [`RpcEvent::Ready`] frame.
48pub const RPC_PROTOCOL_VERSION: u32 = 1;
49
50// ---------------------------------------------------------------------------
51// Auxiliary types
52// ---------------------------------------------------------------------------
53
54/// A file attachment included with a [`RpcCommand::Prompt`] message.
55///
56/// The rpc child reads the file at `path` from the local filesystem.
57/// `name` and `mime` are compatibility hints, not authority: the child uses
58/// the basename and content-based MIME detection. Bytes are embedded in the
59/// selected provider request and private session history after validation.
60#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
61pub struct RpcAttachment {
62    /// Local filesystem path the rpc child can read.
63    ///
64    /// MUST be an absolute path; MUST NOT contain `..` segments. The local
65    /// RPC handler validates this before bounded regular-file ingestion.
66    pub path: String,
67    /// Optional human-meaningful filename (defaults to basename of `path`).
68    #[serde(default, skip_serializing_if = "Option::is_none")]
69    pub name: Option<String>,
70    /// Optional MIME hint; the RPC child always re-detects actual content.
71    #[serde(default, skip_serializing_if = "Option::is_none")]
72    pub mime: Option<String>,
73}
74
75/// Canonical runtime event payload carried by `RpcEvent::Event`.
76/// Server mode internalizes the same formatted event into its owning conversation
77/// rather than exposing a raw transport frame.
78///
79/// All string fields are UTF-8; `formatted` is the full XML-tagged payload
80/// already safe for injection into the agent context (prompt-injection
81/// stripped by `format_event_for_agent`).
82#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
83pub struct EventPayload {
84    /// UUID of the originating event.
85    pub id: String,
86    /// Source type string (e.g. `"discord"`, `"uptime-kuma"`, `"cli"`).
87    pub source: String,
88    /// Severity label: `"low"` | `"medium"` | `"high"` | `"critical"`.
89    pub severity: String,
90    /// Content type label (e.g. `"message"`, `"alert"`, `"subagent_completion"`).
91    pub content_type: String,
92    /// Raw event text (before XML wrapping).
93    pub text: String,
94    /// ISO-8601 UTC timestamp string.
95    pub timestamp: String,
96    /// Full `format_event_for_agent` output — XML-wrapped, injection-safe.
97    pub formatted: String,
98    /// Typed disclosure class (spec §9.7) as its canonical snake_case name.
99    /// Retained through the wire so RPC/server parents can honor
100    /// persistence policy after handoff. Absent = `model_visible`.
101    #[serde(default, skip_serializing_if = "Option::is_none")]
102    pub disclosure: Option<String>,
103}
104
105/// Token-usage summary for a completed agent turn.
106///
107/// Mirrors the shape of `runtime::types::SessionEvent::Usage` so that
108/// consumers of the RPC protocol have identical fields without depending on
109/// the internal runtime type.
110#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
111pub struct TurnUsage {
112    /// Prompt tokens sent to the model.
113    pub input_tokens: u64,
114    /// Completion tokens returned by the model.
115    pub output_tokens: u64,
116    /// Tokens served from the prompt cache (not billed at full rate).
117    #[serde(default)]
118    pub cache_read_input_tokens: u64,
119    /// Tokens written into the prompt cache during this turn.
120    #[serde(default)]
121    pub cache_creation_input_tokens: u64,
122    /// 5-minute-TTL share of cache writes. Optional, omitted when unknown.
123    #[serde(default, skip_serializing_if = "Option::is_none")]
124    pub cache_creation_5m: Option<u64>,
125    /// 1-hour-TTL share of cache writes. Optional, omitted when unknown.
126    #[serde(default, skip_serializing_if = "Option::is_none")]
127    pub cache_creation_1h: Option<u64>,
128    /// The model identifier used for this turn, if known.
129    #[serde(default, skip_serializing_if = "Option::is_none")]
130    pub model: Option<String>,
131}
132
133// ---------------------------------------------------------------------------
134// Commands: parent → rpc child
135// ---------------------------------------------------------------------------
136
137/// Commands sent from the **parent** process to the **rpc child** over the
138/// child's `stdin`.
139///
140/// All variants except [`RpcCommand::Shutdown`] carry an `id` field that the
141/// child echoes back in the matching [`RpcEvent::Response`] frame.
142#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
143#[serde(tag = "type")]
144pub enum RpcCommand {
145    /// Submit a new user prompt, optionally with file attachments.
146    #[serde(rename = "prompt")]
147    Prompt {
148        /// Correlation id; echoed in the corresponding `Response` event.
149        id: String,
150        /// The user's message text.
151        message: String,
152        /// Zero or more file attachments.  Defaults to an empty list when
153        /// the field is absent from the JSON frame.
154        #[serde(default)]
155        attachments: Vec<RpcAttachment>,
156    },
157
158    /// Send a follow-up message continuing the current conversation turn.
159    #[serde(rename = "follow_up")]
160    FollowUp {
161        /// Correlation id.
162        id: String,
163        /// The follow-up message text.
164        message: String,
165    },
166
167    /// Request in-context compaction of the conversation history.
168    #[serde(rename = "compact")]
169    Compact {
170        /// Correlation id.
171        id: String,
172    },
173
174    /// Start a fresh conversation session, discarding history.
175    #[serde(rename = "new_session")]
176    NewSession {
177        /// Correlation id.
178        id: String,
179    },
180
181    /// Retrieve the current conversation message history.
182    #[serde(rename = "get_messages")]
183    GetMessages {
184        /// Correlation id.
185        id: String,
186    },
187
188    /// Switch the active model for subsequent turns.
189    #[serde(rename = "set_model")]
190    SetModel {
191        /// Correlation id.
192        id: String,
193        /// The model identifier to activate (e.g. `"claude-opus-4-5"`).
194        model: String,
195    },
196
197    /// Enumerate models available to the current auth context.
198    #[serde(rename = "get_available_models")]
199    GetAvailableModels {
200        /// Correlation id.
201        id: String,
202    },
203
204    /// Abort the currently running prompt / agent turn.
205    #[serde(rename = "abort")]
206    Abort {
207        /// Correlation id.
208        id: String,
209    },
210
211    /// Retrieve aggregated token-usage statistics for the session.
212    #[serde(rename = "get_session_stats")]
213    GetSessionStats {
214        /// Correlation id.
215        id: String,
216    },
217
218    /// Retrieve the full runtime state snapshot of the rpc child.
219    #[serde(rename = "get_state")]
220    GetState {
221        /// Correlation id.
222        id: String,
223    },
224
225    /// Enumerate all tools currently registered in this rpc session's tool
226    /// registry (built-ins + any MCP / extension tools loaded at boot).
227    ///
228    /// The `id` field follows the same optional-correlation convention used by
229    /// other commands: when present it is echoed in the `Response` frame so the
230    /// bridge can match the reply to its pending probe.  The bridge Phase 8
231    /// router sends `{"type":"tools_list"}` without an `id`; both forms are
232    /// accepted.
233    #[serde(rename = "tools_list")]
234    ToolsList {
235        /// Optional correlation id; echoed in the corresponding `Response`.
236        #[serde(default, skip_serializing_if = "Option::is_none")]
237        id: Option<String>,
238    },
239
240    /// Instruct the rpc child to exit cleanly.
241    ///
242    /// No `id` field — the child does not send a `Response` for shutdown.
243    #[serde(rename = "shutdown")]
244    Shutdown,
245}
246
247// ---------------------------------------------------------------------------
248// Events: rpc child → parent
249// ---------------------------------------------------------------------------
250
251/// Events emitted by the **rpc child** to the **parent** over the child's
252/// `stdout`.
253#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
254#[serde(tag = "type")]
255pub enum RpcEvent {
256    /// A runtime event forwarded to the parent without triggering a new model
257    /// turn.  The parent may inject this into the next authorised
258    /// [`RpcCommand::FollowUp`] / [`RpcCommand::Prompt`].  This variant is
259    /// **additive** — old protocol consumers that don't recognise `"event"`
260    /// will deserialise it as an unknown type and can ignore it safely.
261    #[serde(rename = "event")]
262    Event {
263        /// Canonical structured payload from the runtime event queue.
264        payload: Box<EventPayload>,
265    },
266
267    /// A streaming update from the assistant (text delta, thinking, tool
268    /// call lifecycle, …).  One or more of these frames precede each
269    /// [`RpcEvent::AgentEnd`].
270    #[serde(rename = "message_update")]
271    MessageUpdate {
272        /// The granular assistant event payload.
273        event: AssistantEvent,
274    },
275
276    /// A subagent has been spawned to handle a delegated task.
277    #[serde(rename = "subagent_start")]
278    SubagentStart {
279        /// Opaque monotonic id for this subagent instance.
280        subagent_id: u64,
281        /// Human-readable agent name.
282        agent_name: String,
283        /// First few words of the task description.
284        task_preview: String,
285    },
286
287    /// A running subagent has produced an intermediate status update.
288    #[serde(rename = "subagent_update")]
289    SubagentUpdate {
290        /// Identifies the subagent (matches a prior [`RpcEvent::SubagentStart`]).
291        subagent_id: u64,
292        /// Human-readable agent name.
293        agent_name: String,
294        /// Free-form status string (e.g. `"running"`, `"tool_call"`).
295        status: String,
296    },
297
298    /// A subagent has finished.
299    #[serde(rename = "subagent_done")]
300    SubagentDone {
301        /// Identifies the subagent.
302        subagent_id: u64,
303        /// Human-readable agent name.
304        agent_name: String,
305        /// First few words of the result.
306        result_preview: String,
307        /// Wall-clock seconds the subagent ran for.
308        duration_secs: f64,
309    },
310
311    /// The agent turn has completed.  Carries final token-usage data.
312    #[serde(rename = "agent_end")]
313    AgentEnd {
314        /// Token usage for the completed turn.
315        usage: TurnUsage,
316    },
317
318    /// A response to a specific [`RpcCommand`], correlated by `id`.
319    ///
320    /// The `body` is **flattened** into the enclosing JSON object — its keys
321    /// appear at the top level alongside `"type"`, `"id"`, and `"command"`.
322    #[serde(rename = "response")]
323    Response {
324        /// Echoed from the originating [`RpcCommand`]'s `id` field.
325        id: String,
326        /// The command name this is responding to (e.g. `"get_messages"`).
327        command: String,
328        /// Arbitrary response payload, flattened into the JSON frame.
329        ///
330        /// Type-erased to `serde_json::Value` for forward-compat with new
331        /// `command` strings. Rust consumers wanting strong typing should
332        /// inspect `command` and re-deserialise `body` into a per-command struct.
333        #[serde(flatten)]
334        body: serde_json::Value,
335    },
336
337    /// A protocol-level or runtime error.
338    ///
339    /// `id` is `None` for errors not attributable to a specific command
340    /// (e.g. oversized frame, internal crash).
341    #[serde(rename = "error")]
342    Error {
343        /// Correlation id of the command that caused the error, if any.
344        #[serde(default, skip_serializing_if = "Option::is_none")]
345        id: Option<String>,
346        /// Human-readable error description.
347        message: String,
348    },
349
350    /// Emitted by the rpc child immediately after startup, before any
351    /// commands are accepted.
352    #[serde(rename = "ready")]
353    Ready {
354        /// Unique identifier for this session (UUID or similar).
355        session_id: String,
356        /// The model currently active.
357        model: String,
358        /// The protocol version implemented by this child.
359        /// Must equal [`RPC_PROTOCOL_VERSION`] for the parent to proceed.
360        protocol_version: u32,
361    },
362}
363
364// ---------------------------------------------------------------------------
365// Assistant streaming events
366// ---------------------------------------------------------------------------
367
368/// Granular events emitted by the assistant during a streaming turn.
369///
370/// Carried inside [`RpcEvent::MessageUpdate`].
371#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
372#[serde(tag = "type")]
373pub enum AssistantEvent {
374    #[serde(rename = "response_start")]
375    ResponseStart,
376    #[serde(rename = "response_reset")]
377    ResponseReset,
378    /// An incremental text chunk from the assistant's response.
379    #[serde(rename = "text_delta")]
380    TextDelta {
381        /// The text fragment.
382        delta: String,
383    },
384
385    /// An incremental thinking/reasoning chunk (extended thinking mode).
386    #[serde(rename = "thinking_delta")]
387    ThinkingDelta {
388        /// The thinking fragment.
389        delta: String,
390    },
391
392    /// A tool call has started streaming.  Subsequent
393    /// [`AssistantEvent::ToolcallInputDelta`] frames carry the JSON input.
394    #[serde(rename = "toolcall_start")]
395    ToolcallStart {
396        /// Model-assigned opaque identifier for this tool call.
397        tool_id: String,
398        /// The tool being invoked.
399        tool_name: String,
400    },
401
402    /// An incremental JSON fragment of a tool call's input.
403    #[serde(rename = "toolcall_input_delta")]
404    ToolcallInputDelta {
405        /// Matches the `tool_id` from [`AssistantEvent::ToolcallStart`].
406        tool_id: String,
407        /// Raw JSON fragment (not yet a complete object).
408        delta: String,
409    },
410
411    /// The complete, finalised input for a tool call.
412    #[serde(rename = "toolcall_input")]
413    ToolcallInput {
414        /// Matches the `tool_id` from [`AssistantEvent::ToolcallStart`].
415        tool_id: String,
416        /// The fully parsed JSON input value.
417        input: serde_json::Value,
418    },
419
420    /// The result returned by tool execution.
421    #[serde(rename = "toolcall_result")]
422    ToolcallResult {
423        /// Matches the `tool_id` from [`AssistantEvent::ToolcallStart`].
424        tool_id: String,
425        /// The serialised tool result string.
426        result: String,
427    },
428}
429
430// ---------------------------------------------------------------------------
431// Tests
432// ---------------------------------------------------------------------------
433
434#[cfg(test)]
435mod tests {
436    use super::*;
437    use serde_json::{from_str, json, to_string};
438
439    // ── RpcCommand::ToolsList round-trip ────────────────────────────────────
440
441    #[test]
442    fn tools_list_no_id_serialises() {
443        let cmd = RpcCommand::ToolsList { id: None };
444        let json = to_string(&cmd).expect("serialise");
445        let val: serde_json::Value = from_str(&json).unwrap();
446        assert_eq!(val["type"], "tools_list");
447        assert!(
448            val.get("id").is_none(),
449            "absent id must be omitted (skip_serializing_if)"
450        );
451    }
452
453    #[test]
454    fn tools_list_with_id_serialises() {
455        let cmd = RpcCommand::ToolsList {
456            id: Some("req-42".to_string()),
457        };
458        let json = to_string(&cmd).expect("serialise");
459        let val: serde_json::Value = from_str(&json).unwrap();
460        assert_eq!(val["type"], "tools_list");
461        assert_eq!(val["id"], "req-42");
462    }
463
464    #[test]
465    fn tools_list_no_id_deserialises() {
466        let line = r#"{"type":"tools_list"}"#;
467        let cmd: RpcCommand = from_str(line).expect("deserialise");
468        match cmd {
469            RpcCommand::ToolsList { id } => assert!(id.is_none()),
470            other => panic!("expected ToolsList, got {other:?}"),
471        }
472    }
473
474    #[test]
475    fn tools_list_with_id_deserialises() {
476        let line = r#"{"type":"tools_list","id":"abc-123"}"#;
477        let cmd: RpcCommand = from_str(line).expect("deserialise");
478        match cmd {
479            RpcCommand::ToolsList { id } => assert_eq!(id.as_deref(), Some("abc-123")),
480            other => panic!("expected ToolsList, got {other:?}"),
481        }
482    }
483
484    // ── Response body shape expected by the bridge ──────────────────────────
485
486    /// The bridge validates: `response.ok === true && Array.isArray(response.tools)`.
487    /// Verify the flattened RpcEvent::Response body produces exactly that shape.
488    #[test]
489    fn tools_list_response_body_shape_matches_bridge_expectation() {
490        let tools_json = json!([
491            {
492                "name": "bash",
493                "description": "Run a bash command",
494                "input_schema": {"type": "object", "properties": {"command": {"type": "string"}}}
495            }
496        ]);
497        let ev = RpcEvent::Response {
498            id: "req-1".to_string(),
499            command: "tools_list".to_string(),
500            body: json!({ "ok": true, "tools": tools_json }),
501        };
502        let serialised = to_string(&ev).expect("serialise");
503        let val: serde_json::Value = from_str(&serialised).unwrap();
504
505        assert_eq!(val["type"], "response");
506        assert_eq!(val["id"], "req-1");
507        assert_eq!(val["command"], "tools_list");
508        // Bridge checks these two fields at the top level (flattened):
509        assert_eq!(val["ok"], true, "bridge needs ok=true at top level");
510        assert!(
511            val["tools"].is_array(),
512            "bridge needs tools array at top level"
513        );
514        assert_eq!(val["tools"][0]["name"], "bash");
515    }
516}