Skip to main content

locode_protocol/
lib.rs

1//! locode-protocol — shared, provider-neutral types with no I/O.
2//!
3//! Two concerns live here:
4//! - the **conversation model** (4-role, Anthropic-shaped content blocks — [ADR-0013]),
5//!   which the loop accumulates and hands to a `Provider`; and
6//! - the **report envelope** ([ADR-0009]), the single JSON artifact `locode-exec` prints.
7//!
8//! Types are Rust-native and serialize with `serde` for our own persistence/reporting;
9//! conversion to a specific provider wire (Anthropic, OpenAI) lives in each `Provider`
10//! impl, not here.
11//!
12//! [ADR-0013]: https://github.com/Luolc/locode-core/blob/main/docs/decisions/ADR-0013-conversation-protocol.md
13//! [ADR-0009]: https://github.com/Luolc/locode-core/blob/main/docs/decisions/ADR-0009-headless-io-contract.md
14
15use serde::{Deserialize, Serialize};
16use serde_json::Value;
17
18// ============================ Conversation model ============================
19
20/// A full conversation: one uniform stream of role-tagged messages (ADR-0013).
21///
22/// There is no separate `system` field — a [`Role::System`] message *is* the base
23/// prompt; the Anthropic wire hoists leading System messages into its top-level
24/// `system` param.
25#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
26pub struct Conversation {
27    /// The ordered turns of the conversation.
28    pub messages: Vec<Message>,
29}
30
31/// One message: a role plus an ordered list of content blocks.
32#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
33pub struct Message {
34    /// Who this message is from.
35    pub role: Role,
36    /// The message's content, as typed blocks.
37    pub content: Vec<ContentBlock>,
38}
39
40/// The author of a message (ADR-0013).
41///
42/// `System` is the static base identity; `Developer` is app-author instructions that map
43/// **1:1 and losslessly** onto a native provider role. On the wire, `System` maps to
44/// Anthropic's top-level `system` (or an OpenAI `system` message), while `Developer` maps to
45/// an Anthropic mid-conversation `system` message (or an OpenAI `developer` message).
46/// Injected framing/reminders (e.g. `AGENTS.md` project instructions) are **not** `Developer`
47/// — they are authored as `User` `<system-reminder>` so the conversation ⇄ payload
48/// conversion stays reversible (ADR-0013 amendment / ADR-0023).
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
50#[serde(rename_all = "snake_case")]
51pub enum Role {
52    /// Immutable base identity and safety/policy — the "constitution".
53    System,
54    /// App-author instructions with a native provider role (OpenAI `developer` / Anthropic
55    /// beta mid-conversation `system`). Not the vehicle for reminders — those are `User`.
56    Developer,
57    /// The human's turns; also carries [`ContentBlock::ToolResult`] blocks.
58    User,
59    /// The model's turns: text, thinking, and tool-use blocks.
60    Assistant,
61}
62
63/// A typed piece of message content, modeled on Anthropic's content blocks.
64///
65/// `#[non_exhaustive]` so new block kinds can be added without a breaking change;
66/// only `Text`, `ToolUse`, and `ToolResult` are exercised in v0.
67#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
68#[serde(tag = "type", rename_all = "snake_case")]
69#[non_exhaustive]
70pub enum ContentBlock {
71    /// Plain text.
72    Text {
73        /// The text content.
74        text: String,
75    },
76    /// An image (multimodal).
77    Image {
78        /// Where the image bytes come from.
79        source: ImageSource,
80    },
81    /// Assistant reasoning, unified across wires (ADR-0013 amendment
82    /// 2026-07-19; replaces the earlier `Thinking`/`RedactedThinking` pair).
83    ///
84    /// [`ReasoningFormat`] selects the replay contract; each wire's build
85    /// replays only its own format(s) and **drops foreign formats** (a session
86    /// never crosses wires, so nothing is lost).
87    Reasoning {
88        /// Which encoding/replay contract this data follows.
89        format: ReasoningFormat,
90        /// Human-readable reasoning: the full text (`anthropic`), empty
91        /// (`anthropic_redacted`), the summary (`openai_responses`), or the
92        /// captured text (`text_only`).
93        text: String,
94        /// Anthropic's validator over `text` (`anthropic` format only).
95        signature: Option<String>,
96        /// The wire's opaque replay payload, replayed verbatim and never
97        /// interpreted: the whole Responses reasoning item
98        /// (`openai_responses`) or Anthropic's redacted-thinking data
99        /// (`anthropic_redacted`).
100        payload: Option<Value>,
101    },
102    /// A tool call emitted by the assistant.
103    ToolUse {
104        /// Provider-assigned id, paired with a later [`ContentBlock::ToolResult`].
105        id: String,
106        /// The client-facing tool name (the harness pack's name).
107        name: String,
108        /// The tool arguments as a JSON value.
109        input: Value,
110    },
111    /// The result of a tool call, carried in a [`Role::User`] message.
112    ToolResult {
113        /// The id of the [`ContentBlock::ToolUse`] this answers.
114        tool_use_id: String,
115        /// The result content (text and/or images).
116        content: Vec<ResultChunk>,
117        /// Whether the tool call failed (a soft error the model can recover from).
118        is_error: bool,
119    },
120}
121
122/// A single chunk of a tool result (a restricted set of block kinds).
123#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
124#[serde(tag = "type", rename_all = "snake_case")]
125pub enum ResultChunk {
126    /// Text output.
127    Text {
128        /// The text content.
129        text: String,
130    },
131    /// Image output (e.g. a screenshot).
132    Image {
133        /// Where the image bytes come from.
134        source: ImageSource,
135    },
136}
137
138/// The source of an image block.
139#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
140#[serde(tag = "type", rename_all = "snake_case")]
141pub enum ImageSource {
142    /// Inline base64-encoded bytes.
143    Base64 {
144        /// The MIME type, e.g. `image/png`.
145        media_type: String,
146        /// The base64-encoded image data.
147        data: String,
148    },
149    /// A URL the provider fetches.
150    Url {
151        /// The image URL.
152        url: String,
153    },
154}
155
156/// The encoding/replay contract of a [`ContentBlock::Reasoning`] block.
157///
158/// Named after the wire's own vocabulary (Responses reasoning items tag
159/// themselves `format: "openai-responses-v1"`). Serialized values deliberately
160/// echo the `api_schema` strings so a trace reader maps block → wire at a
161/// glance.
162#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
163#[serde(rename_all = "snake_case")]
164#[non_exhaustive]
165pub enum ReasoningFormat {
166    /// Anthropic extended thinking: full `text` + `signature` validator.
167    Anthropic,
168    /// Anthropic `redacted_thinking`: encrypted `payload`, empty `text`.
169    AnthropicRedacted,
170    /// OpenAI Responses reasoning item: summary in `text`, the WHOLE item in
171    /// `payload` (id + summary + `encrypted_content` + `format` + future fields).
172    OpenAiResponses,
173    /// Capture-only reasoning with no replay contract (e.g. Chat Completions
174    /// gateway extensions). Never replayed by any wire.
175    TextOnly,
176}
177
178// ============================== Report envelope ==============================
179
180/// The single JSON document `locode-exec` prints to stdout (ADR-0009).
181///
182/// `schema_version` is frozen at `1`; changing the envelope shape is a deliberate,
183/// versioned change (see the golden test).
184#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
185pub struct Report {
186    /// Envelope schema version. Always `1` for this contract.
187    pub schema_version: u32,
188    /// The terminal state of the run.
189    pub status: Status,
190    /// The harness pack the run used (e.g. `grok`).
191    pub harness: String,
192    /// The wire schema the run used (e.g. `anthropic`) — the provider's `api_schema()`.
193    /// Names the request/response protocol shape, not a gateway/endpoint.
194    pub api_schema: String,
195    /// The assistant's final text message, if the run completed with one.
196    pub final_message: Option<String>,
197    /// A schema-constrained task answer, if one was requested (`--json-schema`).
198    pub structured_output: Option<Value>,
199    /// How many sample→dispatch→append turns the loop ran.
200    pub turns: u32,
201    /// A record of every tool call made during the run.
202    pub tool_calls: Vec<ToolCallRecord>,
203    /// Token accounting for the run: **summed over every turn**. This is the cost
204    /// basis — what the run generated in total — and grows without bound as a run takes
205    /// more turns.
206    pub usage: Usage,
207    /// Token accounting for the run's **final turn only** — the context occupancy the
208    /// next request starts from.
209    ///
210    /// Not derivable from [`Report::usage`]: each turn's request re-sends the whole
211    /// conversation, so a per-turn sum counts the same history once per turn and has no
212    /// relationship to the context window. The last turn's request *is* the whole
213    /// conversation, which is why this one number answers "how full is the context?".
214    ///
215    /// Additive field (ADR-0018's envelope-evolution policy: new optional report fields
216    /// are non-breaking and do not bump `schema_version`); absent in traces written
217    /// before 2026-07-25, where it reads as all-zero.
218    #[serde(default)]
219    pub context_usage: Usage,
220    /// The session identifier.
221    pub session_id: String,
222    /// The final model stop reason (`"end_turn"`, `"max_tokens"`, …), when a
223    /// completion was received (ADR-0009 amendment 2026-07-19): lets an eval
224    /// pipeline distinguish "model finished" from "model got truncated"
225    /// without re-reading the trace.
226    #[serde(default, skip_serializing_if = "Option::is_none")]
227    pub stop_reason: Option<String>,
228    /// A fatal error message, if the run ended in `status == error`/`model_error`.
229    pub error: Option<String>,
230}
231
232/// The terminal state of a run. Serializes to the exact strings in ADR-0009.
233///
234/// **Envelope evolution policy at `schema_version: 1` (ADR-0018):** additions
235/// — new status values, new optional record/report fields — are
236/// **non-breaking** and do not bump `schema_version`; renames and removals
237/// are breaking and would. JSON consumers should therefore treat an
238/// unrecognized status string as "unknown terminal state", not a parse
239/// error; Rust consumers get the same discipline from `#[non_exhaustive]`
240/// (match with a wildcard arm — `locode-exec` maps unknown statuses to
241/// exit 1).
242#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
243#[serde(rename_all = "snake_case")]
244#[non_exhaustive]
245pub enum Status {
246    /// The model finished with a text answer and no further tool calls.
247    Completed,
248    /// The max-turns ceiling was hit.
249    MaxTurns,
250    /// A provider/network error after bounded retry.
251    ModelError,
252    /// A fatal (`Tool`/host) error aborted the run.
253    Error,
254    /// The run was cancelled through the session's cancel handle (Esc, a
255    /// SIGTERM-driven timeout, …) — a **structured** terminal state, distinct
256    /// from failure (ADR-0018): partial work is preserved and the report is
257    /// still emitted.
258    Cancelled,
259}
260
261/// A report-side record of one tool call (distinct from the in-conversation
262/// [`ContentBlock::ToolUse`]): the structured `output` view, not the model-facing text.
263#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
264pub struct ToolCallRecord {
265    /// The tool-call id (matches the conversation's `tool_use` id).
266    pub id: String,
267    /// The client-facing tool name the model called.
268    pub name: String,
269    /// The canonical `ToolKind` tag for cross-pack A/B alignment (e.g. `shell`).
270    pub kind: String,
271    /// The arguments the model supplied.
272    pub args: Value,
273    /// Whether the call succeeded.
274    pub ok: bool,
275    /// The structured output of the call (the report view, not `prompt_text`).
276    pub output: Value,
277    /// The approver's reason, iff this call was **denied by the approval seam**
278    /// (ADR-0017). Set only from the approver-deny path — never reused for
279    /// other failures, and cancellation synthetics never carry it — so deny
280    /// stays structurally separable from failure and from cancel (the
281    /// codex-`Declined` / grok-taxonomy lesson).
282    #[serde(default, skip_serializing_if = "Option::is_none")]
283    pub denial_reason: Option<String>,
284}
285
286/// Token accounting parsed from the provider's terminal usage event.
287#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
288pub struct Usage {
289    /// Input (prompt) tokens.
290    pub input_tokens: u64,
291    /// Output (completion) tokens.
292    pub output_tokens: u64,
293    /// Tokens served from the prompt cache. **`None` = this wire/provider does
294    /// not report the counter; `Some(0)` = reported as zero** (a real signal:
295    /// no cache hit). ADR-0009 amendment 2026-07-19 — zero-as-N/A rejected.
296    pub cache_read_tokens: Option<u64>,
297    /// Tokens written to the prompt cache (`None` on wires that never report
298    /// writes, e.g. the OpenAI family).
299    pub cache_creation_tokens: Option<u64>,
300    /// Reasoning/thinking tokens (`None` on wires that fold them into
301    /// `output_tokens`, e.g. Anthropic).
302    pub reasoning_tokens: Option<u64>,
303}
304
305impl Usage {
306    /// Everything this turn put on the wire and got back: the full prompt (input plus
307    /// both cache counters) plus the completion.
308    ///
309    /// **Both cache counters belong in the total.** A cached read and a cache write are
310    /// prompt tokens that a provider bills differently — they occupy the context window
311    /// exactly like uncached input, so leaving either out under-reports how full it is.
312    #[must_use]
313    pub fn context_tokens(&self) -> u64 {
314        self.input_tokens
315            + self.cache_read_tokens.unwrap_or(0)
316            + self.cache_creation_tokens.unwrap_or(0)
317            + self.output_tokens
318    }
319}
320
321/// `Some+Some` sums; `None` is the identity — a run total is `None` only if no
322/// turn ever reported the counter.
323fn add_opt(a: Option<u64>, b: Option<u64>) -> Option<u64> {
324    match (a, b) {
325        (Some(x), Some(y)) => Some(x + y),
326        (Some(x), None) | (None, Some(x)) => Some(x),
327        (None, None) => None,
328    }
329}
330
331impl std::ops::AddAssign for Usage {
332    /// Accumulate another turn's usage field-wise (the engine sums across turns).
333    fn add_assign(&mut self, rhs: Self) {
334        self.input_tokens += rhs.input_tokens;
335        self.output_tokens += rhs.output_tokens;
336        self.cache_read_tokens = add_opt(self.cache_read_tokens, rhs.cache_read_tokens);
337        self.cache_creation_tokens = add_opt(self.cache_creation_tokens, rhs.cache_creation_tokens);
338        self.reasoning_tokens = add_opt(self.reasoning_tokens, rhs.reasoning_tokens);
339    }
340}
341
342// ================================= Tool spec =================================
343
344/// A provider-neutral tool spec: name + description + args JSON Schema.
345///
346/// This is the wire-agnostic representation a harness pack produces (from a
347/// `Registry`) and a `Provider` wire maps onto its own tool format (e.g. Anthropic's
348/// `{name, description, input_schema}` vs OpenAI's `{type:"function", function:{…}}`).
349/// It lives in `locode-protocol` because both `locode-tools` (which builds it) and
350/// `locode-provider` (which consumes it via `ConversationRequest`) need it, and the
351/// dependency graph forbids `provider → tools`.
352#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
353pub struct ToolSpec {
354    /// The model-facing wire name (the harness pack's name for the tool).
355    pub name: String,
356    /// The tool description offered to the model.
357    pub description: String,
358    /// How the tool's input is specified to the model (ADR-0003 amendment
359    /// 2026-07-19; replaces the bare `parameters: Value`).
360    pub input: ToolInputFormat,
361}
362
363/// How a tool's input is specified: a JSON-schema function tool, or a freeform
364/// tool whose raw-text input is constrained by a server-side grammar (OpenAI
365/// Responses `custom` tools — codex's `apply_patch`). Exactly one of the two —
366/// an enum, not optional fields, so invalid states are unrepresentable.
367#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
368#[serde(tag = "type", rename_all = "snake_case")]
369pub enum ToolInputFormat {
370    /// A JSON-schema function tool (every tool today).
371    JsonSchema {
372        /// The derived JSON Schema for the tool's arguments.
373        parameters: Value,
374    },
375    /// A freeform tool: raw text constrained by a grammar. On wires without
376    /// custom-tool support it degrades to a `{"input": string}` function tool;
377    /// the raw text reaches the tool identically either way.
378    Freeform {
379        /// The grammar language.
380        syntax: GrammarSyntax,
381        /// The grammar source, verbatim.
382        definition: String,
383    },
384}
385
386/// The grammar language of a [`ToolInputFormat::Freeform`] tool.
387#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
388#[serde(rename_all = "snake_case")]
389pub enum GrammarSyntax {
390    /// A Lark grammar (codex `apply_patch.lark`).
391    Lark,
392    /// A regular expression.
393    Regex,
394}
395
396// ========================= Streaming events (stream-json) =========================
397
398/// One event in the `stream-json` trajectory (one JSON object per line).
399///
400/// The stream is a **self-sufficient, replayable source of the whole run**: `Init`
401/// carries the base prompt + tool specs + model, and each [`Event::Message`] carries a
402/// full turn — so [`reconstruct_conversation`] rebuilds the entire history with nothing
403/// else. (Claude Code's stream omits `system`/`tools`, forcing a proxy capture to
404/// recover them; `Init` closes that gap.) `#[non_exhaustive]` so events can be added
405/// (e.g. per-turn markers) without a breaking change.
406#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
407#[serde(tag = "type", rename_all = "snake_case")]
408#[non_exhaustive]
409pub enum Event {
410    /// Emitted once at the start — everything needed to reconstruct context.
411    Init {
412        /// The session identifier.
413        session_id: String,
414        /// The harness pack in use (e.g. `grok`).
415        harness: String,
416        /// The wire schema in use (e.g. `anthropic`) — the provider's `api_schema()`.
417        api_schema: String,
418        /// The model id.
419        model: String,
420        /// The working directory.
421        cwd: String,
422        /// The max-turns ceiling; absent = unlimited (the default — ADR-0005
423        /// amendment 2026-07-18).
424        #[serde(default, skip_serializing_if = "Option::is_none")]
425        max_turns: Option<u32>,
426        /// The base `System` + `Developer` messages (prompt + capabilities).
427        preamble: Vec<Message>,
428        /// The tool specs offered to the model (name + JSON schema), as JSON values.
429        tools: Vec<Value>,
430    },
431    /// A full message appended to the history (the trace): role + content blocks.
432    Message {
433        /// The appended message.
434        message: Message,
435    },
436    /// An incremental assistant-text fragment during a **streaming** turn
437    /// (ADR-0021). **Display-only**: the whole [`Message`] is still appended at
438    /// turn end, so deltas are *not* part of the reconstructed history (the trace
439    /// stays whole-message — Q1). Emitted only when the engine runs in `streaming`
440    /// mode; consumers that want a whole-message trace drop this variant.
441    MessageDelta {
442        /// One assistant-text fragment — append to the live buffer.
443        text: String,
444    },
445    /// The terminal event: the final report (identical to `--output-format json`).
446    Result {
447        /// The run's report envelope.
448        report: Report,
449    },
450    /// A non-terminal error note (e.g. a retry); terminal errors ride in [`Event::Result`].
451    Error {
452        /// A human-readable message.
453        message: String,
454    },
455    /// An approver resolution at the dispatch gate (ADR-0017) — grok's journal
456    /// shape (`PermissionResolved`). Emitted for **every** consulted call,
457    /// allowed or denied, so interactive traces are complete; `wait_ms` (human
458    /// decision latency) is unrecoverable from any other artifact.
459    Approval {
460        /// The `tool_use` id the decision applies to.
461        tool_use_id: String,
462        /// The client-facing tool name.
463        tool_name: String,
464        /// The resolution: `"allow"` or `"deny"`.
465        decision: String,
466        /// Milliseconds spent awaiting the approver (human decision latency).
467        wait_ms: u64,
468    },
469}
470
471/// Reconstruct the full [`Conversation`] from a `stream-json` event trajectory.
472///
473/// `Init.preamble` seeds the `System`/`Developer` prompt and each [`Event::Message`]
474/// appends a turn; `Result`/`Error` events are run metadata, not part of the history.
475/// This is the inverse of what `locode-exec` emits — the stream is a complete source.
476#[must_use]
477pub fn reconstruct_conversation(events: &[Event]) -> Conversation {
478    let mut messages = Vec::new();
479    for event in events {
480        match event {
481            Event::Init { preamble, .. } => messages.extend(preamble.iter().cloned()),
482            Event::Message { message } => messages.push(message.clone()),
483            // Deltas are display-only — the whole `Message` is appended at turn
484            // end, so skipping them here keeps reconstruction whole-message (Q1).
485            Event::MessageDelta { .. }
486            | Event::Result { .. }
487            | Event::Error { .. }
488            | Event::Approval { .. } => {}
489        }
490    }
491    Conversation { messages }
492}
493
494#[cfg(test)]
495mod tests {
496    use super::*;
497    use serde_json::json;
498
499    #[test]
500    fn conversation_round_trips_all_roles_and_tool_pairing() {
501        let call_id = "call_42";
502        let conversation = Conversation {
503            messages: vec![
504                Message {
505                    role: Role::System,
506                    content: vec![ContentBlock::Text {
507                        text: "You are locode.".into(),
508                    }],
509                },
510                Message {
511                    role: Role::Developer,
512                    content: vec![ContentBlock::Text {
513                        text: "Available tools: run_terminal_command.".into(),
514                    }],
515                },
516                Message {
517                    role: Role::User,
518                    content: vec![ContentBlock::Text {
519                        text: "run echo hi".into(),
520                    }],
521                },
522                Message {
523                    role: Role::Assistant,
524                    content: vec![
525                        ContentBlock::Text {
526                            text: "Sure.".into(),
527                        },
528                        ContentBlock::ToolUse {
529                            id: call_id.into(),
530                            name: "run_terminal_command".into(),
531                            input: json!({ "command": "echo hi" }),
532                        },
533                    ],
534                },
535                Message {
536                    role: Role::User,
537                    content: vec![ContentBlock::ToolResult {
538                        tool_use_id: call_id.into(),
539                        content: vec![ResultChunk::Text {
540                            text: "hi\n".into(),
541                        }],
542                        is_error: false,
543                    }],
544                },
545            ],
546        };
547
548        let wire = serde_json::to_string(&conversation).expect("serialize");
549        let back: Conversation = serde_json::from_str(&wire).expect("deserialize");
550        assert_eq!(
551            conversation, back,
552            "conversation did not round-trip losslessly"
553        );
554
555        // The tool_use id and tool_result tool_use_id are the pairing link (ADR-0004).
556        let ContentBlock::ToolUse { id, .. } = &conversation.messages[3].content[1] else {
557            panic!("expected a tool_use block");
558        };
559        let ContentBlock::ToolResult { tool_use_id, .. } = &conversation.messages[4].content[0]
560        else {
561            panic!("expected a tool_result block");
562        };
563        assert_eq!(id, tool_use_id);
564    }
565
566    #[test]
567    fn content_block_uses_anthropic_style_type_tags() {
568        let block = ContentBlock::Text { text: "hi".into() };
569        assert_eq!(
570            serde_json::to_value(&block).unwrap(),
571            json!({ "type": "text", "text": "hi" })
572        );
573    }
574
575    #[test]
576    fn status_serializes_to_adr_0009_strings() {
577        let cases = [
578            (Status::Completed, "completed"),
579            (Status::MaxTurns, "max_turns"),
580            (Status::ModelError, "model_error"),
581            (Status::Error, "error"),
582        ];
583        for (status, want) in cases {
584            assert_eq!(serde_json::to_value(status).unwrap(), json!(want));
585        }
586    }
587
588    fn minimal_report() -> Report {
589        Report {
590            schema_version: 1,
591            status: Status::Completed,
592            harness: "grok".into(),
593            api_schema: "anthropic".into(),
594            final_message: Some("done".into()),
595            structured_output: None,
596            turns: 1,
597            tool_calls: vec![],
598            usage: Usage::default(),
599            context_usage: Usage::default(),
600            session_id: "sess-1".into(),
601            stop_reason: None,
602            error: None,
603        }
604    }
605
606    /// The JSONL event stream is a self-sufficient source: `init.preamble` + every
607    /// `message` event reconstruct the entire conversation (system/developer included).
608    #[test]
609    fn events_reconstruct_full_conversation() {
610        let system = Message {
611            role: Role::System,
612            content: vec![ContentBlock::Text {
613                text: "base".into(),
614            }],
615        };
616        let developer = Message {
617            role: Role::Developer,
618            content: vec![ContentBlock::Text {
619                text: "capabilities".into(),
620            }],
621        };
622        let user = Message {
623            role: Role::User,
624            content: vec![ContentBlock::Text {
625                text: "run echo hi".into(),
626            }],
627        };
628        let assistant = Message {
629            role: Role::Assistant,
630            content: vec![ContentBlock::ToolUse {
631                id: "c1".into(),
632                name: "run_terminal_command".into(),
633                input: json!({ "command": "echo hi" }),
634            }],
635        };
636        let tool_result = Message {
637            role: Role::User,
638            content: vec![ContentBlock::ToolResult {
639                tool_use_id: "c1".into(),
640                content: vec![ResultChunk::Text {
641                    text: "hi\n".into(),
642                }],
643                is_error: false,
644            }],
645        };
646
647        let events = vec![
648            Event::Init {
649                session_id: "sess-1".into(),
650                harness: "grok".into(),
651                api_schema: "anthropic".into(),
652                model: "claude-opus-4-8".into(),
653                cwd: "/repo".into(),
654                max_turns: Some(30),
655                preamble: vec![system.clone(), developer.clone()],
656                tools: vec![json!({ "name": "run_terminal_command" })],
657            },
658            Event::Message {
659                message: user.clone(),
660            },
661            Event::Message {
662                message: assistant.clone(),
663            },
664            Event::Message {
665                message: tool_result.clone(),
666            },
667            Event::Result {
668                report: minimal_report(),
669            },
670        ];
671
672        // JSONL round-trip: one JSON object per line, parsed back losslessly.
673        let jsonl = events
674            .iter()
675            .map(|e| serde_json::to_string(e).unwrap())
676            .collect::<Vec<_>>()
677            .join("\n");
678        let parsed: Vec<Event> = jsonl
679            .lines()
680            .map(|l| serde_json::from_str(l).unwrap())
681            .collect();
682        assert_eq!(parsed, events, "events did not round-trip through JSONL");
683
684        // Reconstruction yields the FULL history, system/developer included.
685        let rebuilt = reconstruct_conversation(&parsed);
686        assert_eq!(
687            rebuilt,
688            Conversation {
689                messages: vec![system, developer, user, assistant, tool_result]
690            }
691        );
692    }
693
694    #[test]
695    fn event_uses_snake_case_type_tags() {
696        let event = Event::Message {
697            message: Message {
698                role: Role::User,
699                content: vec![],
700            },
701        };
702        assert_eq!(
703            serde_json::to_value(&event).unwrap()["type"],
704            json!("message")
705        );
706    }
707
708    #[test]
709    fn message_delta_round_trips_as_jsonl() {
710        let event = Event::MessageDelta {
711            text: "hello ".into(),
712        };
713        let value = serde_json::to_value(&event).unwrap();
714        assert_eq!(value["type"], json!("message_delta"), "{value}");
715        assert_eq!(value["text"], json!("hello "));
716        let back: Event = serde_json::from_value(value).unwrap();
717        assert_eq!(back, event);
718    }
719
720    #[test]
721    fn message_delta_is_not_part_of_reconstructed_history() {
722        // A streaming turn: deltas then the whole Message. Reconstruction must
723        // ignore the deltas (the trace stays whole-message — ADR-0021 Q1).
724        let assistant = Message {
725            role: Role::Assistant,
726            content: vec![ContentBlock::Text {
727                text: "hello world".into(),
728            }],
729        };
730        let with_deltas = vec![
731            Event::MessageDelta {
732                text: "hello ".into(),
733            },
734            Event::MessageDelta {
735                text: "world".into(),
736            },
737            Event::Message {
738                message: assistant.clone(),
739            },
740        ];
741        let without_deltas = vec![Event::Message {
742            message: assistant.clone(),
743        }];
744        assert_eq!(
745            reconstruct_conversation(&with_deltas),
746            reconstruct_conversation(&without_deltas),
747            "deltas must not affect reconstruction"
748        );
749        // And the reconstructed history is exactly the one whole message.
750        assert_eq!(
751            reconstruct_conversation(&with_deltas).messages,
752            vec![assistant]
753        );
754    }
755
756    /// `denial_reason` (ADR-0017) is additive at `schema_version: 1`: absent
757    /// from the wire when `None`, round-trips when set, and pre-field JSON
758    /// still deserializes.
759    #[test]
760    fn tool_call_record_denial_reason_is_additive() {
761        let record = ToolCallRecord {
762            id: "c1".into(),
763            name: "shell".into(),
764            kind: "shell".into(),
765            args: json!({}),
766            ok: false,
767            output: Value::Null,
768            denial_reason: None,
769        };
770        let value = serde_json::to_value(&record).unwrap();
771        assert!(
772            !value.as_object().unwrap().contains_key("denial_reason"),
773            "None must not appear on the wire: {value}"
774        );
775
776        let denied = ToolCallRecord {
777            denial_reason: Some("not allowed".into()),
778            ..record
779        };
780        let value = serde_json::to_value(&denied).unwrap();
781        assert_eq!(value["denial_reason"], json!("not allowed"));
782        let back: ToolCallRecord = serde_json::from_value(value).unwrap();
783        assert_eq!(back, denied);
784
785        // A record serialized before the field existed still parses.
786        let old = json!({
787            "id": "c1", "name": "shell", "kind": "shell",
788            "args": {}, "ok": true, "output": null
789        });
790        let back: ToolCallRecord = serde_json::from_value(old).unwrap();
791        assert_eq!(back.denial_reason, None);
792    }
793
794    /// `Status::Cancelled` (ADR-0018) rides the wire as `"cancelled"` — an
795    /// additive value at `schema_version: 1` per the documented policy.
796    #[test]
797    fn cancelled_status_wire_string() {
798        assert_eq!(
799            serde_json::to_value(Status::Cancelled).unwrap(),
800            json!("cancelled")
801        );
802        let back: Status = serde_json::from_value(json!("cancelled")).unwrap();
803        assert_eq!(back, Status::Cancelled);
804    }
805
806    /// `Event::Approval` (ADR-0017): grok's journal shape, `snake_case`
807    /// tagged, round-trips, and reconstruction ignores it.
808    #[test]
809    fn approval_event_shape_and_reconstruction() {
810        let event = Event::Approval {
811            tool_use_id: "c1".into(),
812            tool_name: "run_terminal_cmd".into(),
813            decision: "deny".into(),
814            wait_ms: 1234,
815        };
816        let value = serde_json::to_value(&event).unwrap();
817        assert_eq!(
818            value,
819            json!({
820                "type": "approval",
821                "tool_use_id": "c1",
822                "tool_name": "run_terminal_cmd",
823                "decision": "deny",
824                "wait_ms": 1234
825            })
826        );
827        let back: Event = serde_json::from_value(value).unwrap();
828        assert_eq!(back, event);
829
830        let conversation = reconstruct_conversation(&[event]);
831        assert!(
832            conversation.messages.is_empty(),
833            "approval events are run metadata, not history"
834        );
835    }
836}