kian-core 0.3.0

Concept model, session state machine, six-invariant assertions
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
//! Event classification and envelope (DESIGN §3.1 "Event classification
//! (borrowing dsh's durable/live split)" and §3.3 "Schema versioning").
//!
//! The append-only `Event` envelope carries a schema `version` (v0, DESIGN
//! §3.3) and the durable `EventKind` set. Provider payloads stay opaque
//! `bytes` — no provider-specific field names inside `EventKind`. After
//! merges, event kinds are additions-only (constraint 3, cache-byte
//! stability).

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::model::{Epoch, SessionId};

/// Current event-schema version (DESIGN §3.3). v0 is frozen by; later
/// versions arrive exclusively as adjacent migrations — committed
/// generations are never renamed, replaced or deleted.
pub const SCHEMA_VERSION: u32 = 0;

/// The append-only event envelope — the sole truth (constraint 1). Events
/// are never modified; every projection is derived from a stream of these.
///
/// The exact serialized JSON form is frozen (golden-tested): field order and
/// byte shape are stable content, because envelopes feed replay and audit
/// exports.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Event {
    /// Monotone per-session sequence number (CAS fence input, ADR-005).
    pub seq: u64,
    pub session: SessionId,
    /// Fencing epoch of the drain that appended this event.
    pub epoch: Epoch,
    /// Appended at, unix epoch milliseconds.
    pub ts: u64,
    /// Flattened into the envelope: the JSON carries `"kind": "<name>"`
    /// (plus payload fields such as `reason` for `session/ended`) at the
    /// envelope's top level.
    #[serde(flatten)]
    pub kind: EventKind,
    /// Opaque payload. `EventKind` names the semantics, `bytes` carries
    /// them — no provider-specific schema is allowed inside the envelope
    /// (narrow waist; constraint 3). Serialized as a JSON value:
    /// deterministic re-serialization keeps replay byte-identical.
    pub bytes: Value,
    /// Envelope schema version. Defaults to 0 (v0) when absent (§3.3).
    #[serde(default)]
    pub version: u32,
}

impl Event {
    /// The current schema version, for constructors that want to be
    /// explicit.
    pub fn current_version() -> u32 {
        SCHEMA_VERSION
    }
}

/// The derived event id: `"evt_" + uuid5(session, "kian:event:<seq>")`
/// — RFC 4122 v5 with the session uuid as the namespace and the literal
/// `kian:event:<seq>` as the name.
///
/// Deterministic and derivable by anyone who knows the session id and
/// seq, so tooling (CLI `session export --with-event-ids`, the SDK
/// helper) can reference an event without storing an id. It is **not**
/// an envelope field and **not** stored: envelope bytes are frozen v0
/// (constraint 3), and `(session, seq)` is already unique — the id is a
/// naming convention over that pair, never a second source of truth.
pub fn event_id(session: &SessionId, seq: u64) -> String {
    format!(
        "evt_{}",
        uuid::Uuid::new_v5(&session.0, format!("kian:event:{seq}").as_bytes())
    )
}

/// Why a session ended. Honesty over completion rate (constraint 7):
/// `Interrupted` is deliberately distinct from `Failed` — an abort or crash
/// must never be mapped onto `Completed`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum EndReason {
    Completed,
    Cancelled,
    Interrupted,
    Failed,
    /// The drain stopped with a non-empty pending-confirmation set
    /// (CONFIRMATION-MODEL, ADR-033): `running -> idle` (resumable), not
    /// terminal. Deliberately distinct from `interrupted` (an owner
    /// stopped the drain) and `failed` (a failure).
    #[serde(rename = "awaiting_input")]
    AwaitingInput,
    /// The drain stopped because the session's token budget cannot fund
    /// the next provider turn (ADR-055): `running -> idle` (resumable), not
    /// terminal. A `budget/raised` input plus its wake re-opens the turn;
    /// deliberately distinct from `failed` (a failure) and `interrupted`
    /// (an owner stopped the drain).
    #[serde(rename = "awaiting_budget")]
    AwaitingBudget,
    /// A resident session was retired by rotation (ADR-040): a
    /// deliberate, operator-policy retirement (`running -> rotated`,
    /// terminal, frozen), never a failure and never an "it finished its
    /// job" claim. Deliberately distinct from `completed`.
    Rotated,
}

/// The durable event set (DESIGN §3.1). Live events (streaming chunks,
/// heartbeats, progress) are *not* here — they are never persisted.
///
/// Only `SessionEnded` carries structured data (`EndReason`) because the
/// terminal state transition must be projectable without parsing opaque
/// bytes (NoHalfTerminated, constraint 4). Every other kind's payload lives
/// in [`Event::bytes`] under a payload schema owned by the emitting
/// subsystem (e.g. `compact`'s `[a,b)` range + summary:).
///
/// **Additions-only after ** (constraint 3): no renames, no reformats.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum EventKind {
    /// Session created (`idle`).
    #[serde(rename = "session/created")]
    SessionCreated,

    /// An input row became durable (admit-before-wake, invariant 1).
    #[serde(rename = "input/admitted")]
    InputAdmitted,

    /// A drain claimed the session (epoch issued, lease started).
    #[serde(rename = "drain/claimed")]
    DrainClaimed,

    /// A provider turn began.
    #[serde(rename = "turn/start")]
    TurnStart,

    /// A provider turn ended.
    #[serde(rename = "turn/end")]
    TurnEnd,

    /// Exact request bytes persisted *before* the request flew (A2).
    #[serde(rename = "step/request")]
    StepRequest,

    /// Assistant message — the sent bytes are the truth (A2/A3).
    #[serde(rename = "assistant/message")]
    AssistantMessage,

    /// Tool invocation requested (logged before execution, invariant 1).
    #[serde(rename = "tool/call")]
    ToolCall,

    /// Tool invocation result.
    #[serde(rename = "tool/result")]
    ToolResult,

    /// Compression — the only deliberate prefix-invalidation point (A3).
    /// Carries the pre-compression range `[a,b)` and the post-compression
    /// summary in its payload (schema owned ).
    #[serde(rename = "compact")]
    Compact,

    /// Slot → provider/model binding fixed at claim (ADR-006).
    #[serde(rename = "slot/binding")]
    SlotBinding,

    /// One fallback-chain switch within a bound slot.
    #[serde(rename = "slot/fallback")]
    SlotFallback,

    /// Explicit slot migration — the only escape hatch from an immutable
    /// binding (ADR-006).
    #[serde(rename = "slot/migration")]
    SlotMigration,

    /// Operator reset of a session's recovery budget. The
    /// reset is itself an appended event — there is no silent counter
    /// wipe (constraint 1: every observable change is an append). The
    /// payload (schema owned by: `{"reason": string}`) records why.
    /// Neutral by design: the state machine maps it to no transition
    /// and the model-context projection skips it, so replay derives
    /// fresh budgets from config while the event remains the audit
    /// record that a reset happened.
    #[serde(rename = "recovery/budget-reset")]
    RecoveryBudgetReset,

    /// Thread lineage start (bundle): a Router's drain dispatched
    /// work to a worker session, or the worker's stream records that it
    /// was spawned by delegation. Payload schema (owned ):
    /// `{"thread_id": uuid, "parent": session-uuid, "worker":
    /// session-uuid, "pinned_version": u64, "task": <message payload>}`.
    /// State-preserving in `idle`/`running`/`recoverable` (same row as
    /// `input/admitted`); not context-bearing.
    #[serde(rename = "thread/started")]
    ThreadStarted,

    /// Thread lineage completion (bundle): the worker's own drain
    /// appended its outcome before the end write. Payload schema (owned
    /// ): `{"thread_id": uuid, "status": "completed"|"failed"|
    /// "interrupted", "summary": <message payload>}`. Drain activity
    /// (default mapping); not context-bearing.
    #[serde(rename = "thread/completed")]
    ThreadCompleted,

    /// Evidence-gate verdict (bundle): the bounded, declarative
    /// stop-gate recorded its decision. Payload schema (owned ):
    /// `{"gate": str, "passed": bool, "checks": [{"name": str,
    /// "passed": bool, "detail"?: str}], "attempt": u64}`. Drain activity
    /// (default mapping); not context-bearing.
    #[serde(rename = "evidence/verdict")]
    EvidenceVerdict,

    /// Channel delivery receipt (bundle): a channel output policy
    /// delivered a message and recorded the honest receipt. Payload
    /// schema (owned ): `{"channel": str, "target": <value>,
    /// "policy": str, "words"?: u64, "truncated": bool}`. Drain activity
    /// (default mapping); not context-bearing.
    #[serde(rename = "channel/delivered")]
    ChannelDelivered,

    /// Spilled raw channel payload (ecosystem): the verbatim channel
    /// payload kept in-stream but out of context. Payload schema (owned
    /// ): `{"event_id": str, "channel": str, "topic": str|null,
    /// "raw": <verbatim channel payload>}`. Appended by the drain's
    /// consumption write in the same fenced batch as its sibling
    /// `input/admitted` (the Inline form is what the model sees; this
    /// event is what recall reads back from the stream — constraint 1:
    /// the stream stays the sole truth, no side store owns the raw).
    /// Drain activity (the additions-only default mapping); not
    /// context-bearing.
    #[serde(rename = "input/spilled")]
    InputSpilled,

    /// Durable interrupt intent (ADR-014): the control plane's
    /// interrupt request, consumed and appended by the owning drain at a
    /// safe boundary. Payload schema (owned ):
    /// `{"action": "interrupt", "reason": string|null,
    /// "requested_by": string|null, "requested_at_ms": u64,
    /// "target_epoch": u64|null, "stale"?: true}` — `stale` is present
    /// (and `true`) only when the row's target episode had already
    /// settled before consumption (the audit-only arm; ends nothing).
    /// Drain activity (default mapping); not context-bearing — the kind
    /// never enters a model request (constraint 3).
    #[serde(rename = "control/interrupt")]
    ControlInterrupt,

    /// A memory note (MEMORY-MODEL, ADR-016): a *claim* about the agent,
    /// extracted from a source session's transcript by the dreamer drain
    /// and appended to the memory session's stream. Payload schema
    /// (owned by the memory dreamer drain): `{"subject": <non-empty
    /// str>, "text": <str>, "status": "candidate"|"verified"|"stale",
    /// "derived_from": [{"session": <uuid-str>, "start_seq": <u64>,
    /// "end_seq": <u64>}], "verified_by"?: {"method":
    /// "transcript"|"evidence_gate"|"human", "ref": <str>}}` —
    /// `verified_by` rides only on verified notes. Non-context-bearing
    /// (a note is never a message, constraint 3); drain activity (the
    /// additions-only default) — legal only while `running`; the
    /// latest-per-subject fold is a projection in `kian-core::memory`,
    /// never state outside the stream.
    #[serde(rename = "memory/note")]
    MemoryNote,

    /// A human decision on one pending ask, consumed from the reserved
    /// confirm row (CONFIRMATION-MODEL §5, ADR-033): non-context drain
    /// activity, the durable record that the answer arrived, before any
    /// effect.
    #[serde(rename = "control/confirm")]
    ControlConfirm,

    /// A durable budget raise (ADR-055): a control-plane intent consumed by
    /// the owning drain appended the tokens it adds to the session budget.
    /// Payload schema: `{"tokens"?: u64, "wall_clock_ms"?: u64,
    /// "reason": string|null, "requested_by": string|null,
    /// "requested_at_ms": u64}`. Drain activity (default mapping);
    /// not context-bearing — the kind never enters a model request.
    #[serde(rename = "budget/raised")]
    BudgetRaised,

    /// Terminal end write: session end + state transition in one atomic
    /// append (NoHalfTerminated, constraint 4).
    #[serde(rename = "session/ended")]
    SessionEnded { reason: EndReason },
}

impl EventKind {
    /// The durable kind name (e.g. `"session/created"`). Golden-tested to
    /// stay in sync with the serde tag.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::SessionCreated => "session/created",
            Self::InputAdmitted => "input/admitted",
            Self::DrainClaimed => "drain/claimed",
            Self::TurnStart => "turn/start",
            Self::TurnEnd => "turn/end",
            Self::StepRequest => "step/request",
            Self::AssistantMessage => "assistant/message",
            Self::ToolCall => "tool/call",
            Self::ToolResult => "tool/result",
            Self::Compact => "compact",
            Self::SlotBinding => "slot/binding",
            Self::SlotFallback => "slot/fallback",
            Self::SlotMigration => "slot/migration",
            Self::RecoveryBudgetReset => "recovery/budget-reset",
            Self::ThreadStarted => "thread/started",
            Self::ThreadCompleted => "thread/completed",
            Self::EvidenceVerdict => "evidence/verdict",
            Self::ChannelDelivered => "channel/delivered",
            Self::InputSpilled => "input/spilled",
            Self::ControlInterrupt => "control/interrupt",
            Self::MemoryNote => "memory/note",
            Self::ControlConfirm => "control/confirm",
            Self::BudgetRaised => "budget/raised",
            Self::SessionEnded { .. } => "session/ended",
        }
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;
    use uuid::Uuid;

    use super::{EndReason, Event, EventKind, SCHEMA_VERSION};
    use crate::model::{Epoch, SessionId};

    fn sample_event(kind: EventKind) -> Event {
        Event {
            seq: 7,
            session: SessionId(Uuid::nil()),
            epoch: Epoch(3),
            ts: 1_761_900_000_000,
            kind,
            bytes: json!({"opaque": true}),
            version: SCHEMA_VERSION,
        }
    }

    fn tag_of(golden: &str) -> String {
        serde_json::from_str::<serde_json::Value>(golden)
            .unwrap()
            .get("kind")
            .and_then(|v| v.as_str())
            .map(str::to_owned)
            .unwrap()
    }

    /// Every EventKind has an exact golden JSON form and round-trips (
    /// acceptance). The `as_str` name must stay in sync with the serde tag.
    /// After merges this set is additions-only.
    #[test]
    fn event_kind_golden_json_round_trip() {
        let cases = [
            (EventKind::SessionCreated, r#"{"kind":"session/created"}"#),
            (EventKind::InputAdmitted, r#"{"kind":"input/admitted"}"#),
            (EventKind::DrainClaimed, r#"{"kind":"drain/claimed"}"#),
            (EventKind::TurnStart, r#"{"kind":"turn/start"}"#),
            (EventKind::TurnEnd, r#"{"kind":"turn/end"}"#),
            (EventKind::StepRequest, r#"{"kind":"step/request"}"#),
            (
                EventKind::AssistantMessage,
                r#"{"kind":"assistant/message"}"#,
            ),
            (EventKind::ToolCall, r#"{"kind":"tool/call"}"#),
            (EventKind::ToolResult, r#"{"kind":"tool/result"}"#),
            (EventKind::Compact, r#"{"kind":"compact"}"#),
            (EventKind::SlotBinding, r#"{"kind":"slot/binding"}"#),
            (EventKind::SlotFallback, r#"{"kind":"slot/fallback"}"#),
            (EventKind::SlotMigration, r#"{"kind":"slot/migration"}"#),
            (
                EventKind::RecoveryBudgetReset,
                r#"{"kind":"recovery/budget-reset"}"#,
            ),
            (EventKind::ThreadStarted, r#"{"kind":"thread/started"}"#),
            (EventKind::ThreadCompleted, r#"{"kind":"thread/completed"}"#),
            (EventKind::EvidenceVerdict, r#"{"kind":"evidence/verdict"}"#),
            (
                EventKind::ChannelDelivered,
                r#"{"kind":"channel/delivered"}"#,
            ),
            (EventKind::InputSpilled, r#"{"kind":"input/spilled"}"#),
            (
                EventKind::ControlInterrupt,
                r#"{"kind":"control/interrupt"}"#,
            ),
            (EventKind::MemoryNote, r#"{"kind":"memory/note"}"#),
            (EventKind::ControlConfirm, r#"{"kind":"control/confirm"}"#),
            (EventKind::BudgetRaised, r#"{"kind":"budget/raised"}"#),
            (
                EventKind::SessionEnded {
                    reason: EndReason::Completed,
                },
                r#"{"kind":"session/ended","reason":"completed"}"#,
            ),
        ];
        for (kind, golden) in &cases {
            assert_eq!(&serde_json::to_string(kind).unwrap(), golden);
            let back: EventKind = serde_json::from_str(golden).unwrap();
            assert_eq!(&back, kind);
            assert_eq!(kind.as_str(), tag_of(golden));
        }
        // The durable set is exactly the twenty-four kinds above (the
        // fourteen v0-frozen kinds + the additive
        // `recovery/budget-reset` + the four additive bundle kinds
        // + the additive `input/spilled` + the additive
        // `control/interrupt` + the additive `memory/note` + the
        // additive `control/confirm` + the additive `budget/raised`;
        // additions-only, no renames).
        assert_eq!(cases.len(), 24);
    }

    /// Additions-only regression: every pre-existing kind's golden
    /// bytes are byte-identical to the built-in frozen forms. This list is
    /// deliberately independent of the growing `cases` array above — it
    /// is the frozen built-in contract, copied verbatim, so a future edit
    /// to an existing kind's serialization fails here even if `cases`
    /// was updated alongside it.
    #[test]
    fn additions_only_regression_initial_kind_bytes_are_frozen() {
        let frozen_initial: [(EventKind, &str); 15] = [
            (EventKind::SessionCreated, r#"{"kind":"session/created"}"#),
            (EventKind::InputAdmitted, r#"{"kind":"input/admitted"}"#),
            (EventKind::DrainClaimed, r#"{"kind":"drain/claimed"}"#),
            (EventKind::TurnStart, r#"{"kind":"turn/start"}"#),
            (EventKind::TurnEnd, r#"{"kind":"turn/end"}"#),
            (EventKind::StepRequest, r#"{"kind":"step/request"}"#),
            (
                EventKind::AssistantMessage,
                r#"{"kind":"assistant/message"}"#,
            ),
            (EventKind::ToolCall, r#"{"kind":"tool/call"}"#),
            (EventKind::ToolResult, r#"{"kind":"tool/result"}"#),
            (EventKind::Compact, r#"{"kind":"compact"}"#),
            (EventKind::SlotBinding, r#"{"kind":"slot/binding"}"#),
            (EventKind::SlotFallback, r#"{"kind":"slot/fallback"}"#),
            (EventKind::SlotMigration, r#"{"kind":"slot/migration"}"#),
            (
                EventKind::RecoveryBudgetReset,
                r#"{"kind":"recovery/budget-reset"}"#,
            ),
            (
                EventKind::SessionEnded {
                    reason: EndReason::Failed,
                },
                r#"{"kind":"session/ended","reason":"failed"}"#,
            ),
        ];
        for (kind, golden) in &frozen_initial {
            assert_eq!(
                &serde_json::to_string(kind).unwrap(),
                golden,
                "pre-existing kind {:?} changed bytes (additions-only violated)",
                kind
            );
            let back: EventKind = serde_json::from_str(golden).unwrap();
            assert_eq!(&back, kind);
        }
    }

    /// Additions-only regression: the four bundle kinds' golden
    /// bytes are frozen exactly as they landed in bundle. Together with
    /// the built-in frozen set above this pins every one of the nineteen
    /// legacy kinds, independently of the growing `cases` array, so the
    /// addition cannot disturb a single existing serialization byte.
    #[test]
    fn additions_only_regression_bundle_kind_bytes_are_frozen() {
        let frozen_bundle: [(EventKind, &str); 4] = [
            (EventKind::ThreadStarted, r#"{"kind":"thread/started"}"#),
            (EventKind::ThreadCompleted, r#"{"kind":"thread/completed"}"#),
            (EventKind::EvidenceVerdict, r#"{"kind":"evidence/verdict"}"#),
            (
                EventKind::ChannelDelivered,
                r#"{"kind":"channel/delivered"}"#,
            ),
        ];
        for (kind, golden) in &frozen_bundle {
            assert_eq!(
                &serde_json::to_string(kind).unwrap(),
                golden,
                "pre-existing kind {:?} changed bytes (additions-only violated)",
                kind
            );
            let back: EventKind = serde_json::from_str(golden).unwrap();
            assert_eq!(&back, kind);
        }
    }

    /// Additions-only regression: all twenty legacy kinds' golden
    /// bytes are frozen exactly as they stood at the end of.
    /// Like the built-in/bundle frozen lists above, this is
    /// deliberately independent of the growing `cases` array, so the
    /// `control/interrupt` addition cannot disturb a single
    /// existing serialization byte.
    #[test]
    fn additions_only_regression_all_kind_bytes_are_frozen() {
        let frozen_all: [(EventKind, &str); 20] = [
            (EventKind::SessionCreated, r#"{"kind":"session/created"}"#),
            (EventKind::InputAdmitted, r#"{"kind":"input/admitted"}"#),
            (EventKind::DrainClaimed, r#"{"kind":"drain/claimed"}"#),
            (EventKind::TurnStart, r#"{"kind":"turn/start"}"#),
            (EventKind::TurnEnd, r#"{"kind":"turn/end"}"#),
            (EventKind::StepRequest, r#"{"kind":"step/request"}"#),
            (
                EventKind::AssistantMessage,
                r#"{"kind":"assistant/message"}"#,
            ),
            (EventKind::ToolCall, r#"{"kind":"tool/call"}"#),
            (EventKind::ToolResult, r#"{"kind":"tool/result"}"#),
            (EventKind::Compact, r#"{"kind":"compact"}"#),
            (EventKind::SlotBinding, r#"{"kind":"slot/binding"}"#),
            (EventKind::SlotFallback, r#"{"kind":"slot/fallback"}"#),
            (EventKind::SlotMigration, r#"{"kind":"slot/migration"}"#),
            (
                EventKind::RecoveryBudgetReset,
                r#"{"kind":"recovery/budget-reset"}"#,
            ),
            (EventKind::ThreadStarted, r#"{"kind":"thread/started"}"#),
            (EventKind::ThreadCompleted, r#"{"kind":"thread/completed"}"#),
            (EventKind::EvidenceVerdict, r#"{"kind":"evidence/verdict"}"#),
            (
                EventKind::ChannelDelivered,
                r#"{"kind":"channel/delivered"}"#,
            ),
            (EventKind::InputSpilled, r#"{"kind":"input/spilled"}"#),
            (
                EventKind::SessionEnded {
                    reason: EndReason::Failed,
                },
                r#"{"kind":"session/ended","reason":"failed"}"#,
            ),
        ];
        for (kind, golden) in &frozen_all {
            assert_eq!(
                &serde_json::to_string(kind).unwrap(),
                golden,
                "pre-existing kind {:?} changed bytes (additions-only violated)",
                kind
            );
            let back: EventKind = serde_json::from_str(golden).unwrap();
            assert_eq!(&back, kind);
        }
        assert_eq!(frozen_all.len(), 20);
    }

    /// The `control/interrupt` kind round-trips through the full
    /// envelope with its tabled payload riding opaquely in `bytes`,
    /// including the audit-only `stale` marker variant.
    #[test]
    fn control_interrupt_envelope_round_trips_with_tabled_payload() {
        for (payload, with_stale) in [
            (
                json!({
                    "action": "interrupt",
                    "reason": "operator requested",
                    "requested_by": "ops@replica-a",
                    "requested_at_ms": 1_762_000_000_000_u64,
                    "target_epoch": 3
                }),
                false,
            ),
            (
                json!({
                    "action": "interrupt",
                    "reason": null,
                    "requested_by": null,
                    "requested_at_ms": 1_762_000_000_001_u64,
                    "target_epoch": null,
                    "stale": true
                }),
                true,
            ),
        ] {
            let mut ev = sample_event(EventKind::ControlInterrupt);
            ev.bytes = payload.clone();
            let line = serde_json::to_string(&ev).unwrap();
            let back: Event = serde_json::from_str(&line).unwrap();
            assert_eq!(back, ev);
            assert_eq!(tag_of(&line), "control/interrupt");
            assert_eq!(back.bytes, payload);
            // The stale marker rides only when set (additive key).
            if !with_stale {
                assert!(back.bytes.get("stale").is_none());
            } else {
                assert_eq!(back.bytes.get("stale"), Some(&json!(true)));
            }
        }
    }

    /// The `memory/note` kind round-trips through the full envelope with
    /// its tabled payload (round-8 plan §2.1) riding opaquely in `bytes`;
    /// `verified_by` is the one optional key (absent for notes that have
    /// not been verified yet).
    #[test]
    fn memory_note_envelope_round_trips_with_tabled_payload() {
        let verified = json!({
            "subject": "deploys via the dsh bridge",
            "text": "The team deploys through the dsh bridge; verify the environment before suggesting commands.",
            "status": "verified",
            "derived_from": [{
                "session": "0f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b",
                "start_seq": 4,
                "end_seq": 38
            }],
            "verified_by": {"method": "transcript", "ref": "seq range [4,38) read back from the source stream"}
        });
        let candidate = json!({
            "subject": "prefers concise replies",
            "text": "Keep answers short and to the point.",
            "status": "candidate",
            "derived_from": [{
                "session": "0f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b",
                "start_seq": 4,
                "end_seq": 9
            }]
        });
        for (payload, has_verified_by) in [(verified, true), (candidate, false)] {
            let mut ev = sample_event(EventKind::MemoryNote);
            ev.bytes = payload.clone();
            let line = serde_json::to_string(&ev).unwrap();
            let back: Event = serde_json::from_str(&line).unwrap();
            assert_eq!(back, ev);
            assert_eq!(tag_of(&line), "memory/note");
            assert_eq!(back.bytes, payload);
            // The optional key rides only when set (additive key).
            if has_verified_by {
                assert!(back.bytes.get("verified_by").is_some());
            } else {
                assert!(back.bytes.get("verified_by").is_none());
            }
        }
    }

    /// Additions-only regression: all twenty-one pre-`memory/note` kinds'
    /// golden bytes are frozen exactly as they stood at the end of round
    /// 7 (the twenty of `frozen_all` plus `control/interrupt`). Like the
    /// frozen lists above, this is deliberately independent of the
    /// growing `cases` array, so the `memory/note` addition cannot
    /// disturb a single existing serialization byte.
    #[test]
    fn additions_only_regression_round8_kind_bytes_are_frozen() {
        let frozen_round7: [(EventKind, &str); 21] = [
            (EventKind::SessionCreated, r#"{"kind":"session/created"}"#),
            (EventKind::InputAdmitted, r#"{"kind":"input/admitted"}"#),
            (EventKind::DrainClaimed, r#"{"kind":"drain/claimed"}"#),
            (EventKind::TurnStart, r#"{"kind":"turn/start"}"#),
            (EventKind::TurnEnd, r#"{"kind":"turn/end"}"#),
            (EventKind::StepRequest, r#"{"kind":"step/request"}"#),
            (
                EventKind::AssistantMessage,
                r#"{"kind":"assistant/message"}"#,
            ),
            (EventKind::ToolCall, r#"{"kind":"tool/call"}"#),
            (EventKind::ToolResult, r#"{"kind":"tool/result"}"#),
            (EventKind::Compact, r#"{"kind":"compact"}"#),
            (EventKind::SlotBinding, r#"{"kind":"slot/binding"}"#),
            (EventKind::SlotFallback, r#"{"kind":"slot/fallback"}"#),
            (EventKind::SlotMigration, r#"{"kind":"slot/migration"}"#),
            (
                EventKind::RecoveryBudgetReset,
                r#"{"kind":"recovery/budget-reset"}"#,
            ),
            (EventKind::ThreadStarted, r#"{"kind":"thread/started"}"#),
            (EventKind::ThreadCompleted, r#"{"kind":"thread/completed"}"#),
            (EventKind::EvidenceVerdict, r#"{"kind":"evidence/verdict"}"#),
            (
                EventKind::ChannelDelivered,
                r#"{"kind":"channel/delivered"}"#,
            ),
            (EventKind::InputSpilled, r#"{"kind":"input/spilled"}"#),
            (
                EventKind::ControlInterrupt,
                r#"{"kind":"control/interrupt"}"#,
            ),
            (
                EventKind::SessionEnded {
                    reason: EndReason::Failed,
                },
                r#"{"kind":"session/ended","reason":"failed"}"#,
            ),
        ];
        for (kind, golden) in &frozen_round7 {
            assert_eq!(
                &serde_json::to_string(kind).unwrap(),
                golden,
                "pre-existing kind {:?} changed bytes (additions-only violated)",
                kind
            );
            let back: EventKind = serde_json::from_str(golden).unwrap();
            assert_eq!(&back, kind);
        }
        assert_eq!(frozen_round7.len(), 21);
    }

    /// The `input/spilled` kind round-trips through the full envelope
    /// with a payload of its tabled schema (§2 of the plan) riding
    /// opaquely in `bytes` (JSONL-export shape, the replay path).
    #[test]
    fn input_spilled_envelope_round_trips_with_tabled_payload() {
        let payload = json!({
            "event_id": "evt-91",
            "channel": "mock-im",
            "topic": "worker-1",
            "raw": {"text": "@kian deploy", "attachments": [{"type": "file", "size": 4821}]}
        });
        let ev = Event {
            seq: 5,
            session: SessionId(Uuid::nil()),
            epoch: Epoch(2),
            ts: 1_762_000_000_000,
            kind: EventKind::InputSpilled,
            bytes: payload.clone(),
            version: SCHEMA_VERSION,
        };
        let line = serde_json::to_string(&ev).unwrap();
        let back: Event = serde_json::from_str(&line).unwrap();
        assert_eq!(back, ev);
        assert_eq!(tag_of(&line), "input/spilled");
        // The verbatim raw survives the round trip byte-identically —
        // the spill is the audit copy the recall path reads back.
        assert_eq!(back.bytes, payload);
    }

    /// Each bundle kind round-trips through the full envelope with
    /// a payload of its tabled schema riding opaquely in `bytes`
    /// (JSONL-export shape, the replay path).
    #[test]
    fn bundle_kind_envelopes_round_trip_with_tabled_payloads() {
        let payloads = [
            (
                EventKind::ThreadStarted,
                json!({
                    "thread_id": "0b6a18c4-0e52-4b8f-9d5a-2f4c9c1b7d01",
                    "parent": "0b6a18c4-0e52-4b8f-9d5a-2f4c9c1b7d02",
                    "worker": "0b6a18c4-0e52-4b8f-9d5a-2f4c9c1b7d03",
                    "pinned_version": 3,
                    "task": {"role": "user", "content": "refactor the parser"}
                }),
            ),
            (
                EventKind::ThreadCompleted,
                json!({
                    "thread_id": "0b6a18c4-0e52-4b8f-9d5a-2f4c9c1b7d01",
                    "status": "completed",
                    "summary": {"role": "assistant", "content": "done: 3 files"}
                }),
            ),
            (
                EventKind::EvidenceVerdict,
                json!({
                    "gate": "worker-stop",
                    "passed": true,
                    "checks": [
                        {"name": "final_message_present", "passed": true},
                        {"name": "no_error_tool_result_tail", "passed": true, "detail": "tail=2"}
                    ],
                    "attempt": 1
                }),
            ),
            (
                EventKind::ChannelDelivered,
                json!({
                    "channel": "mock-im",
                    "target": {"chat_id": 42},
                    "policy": "im/max-words",
                    "words": 87,
                    "truncated": false
                }),
            ),
        ];
        for (kind, payload) in &payloads {
            let ev = Event {
                seq: 4,
                session: SessionId(Uuid::nil()),
                epoch: Epoch(2),
                ts: 1_762_000_000_000,
                kind: *kind,
                bytes: payload.clone(),
                version: SCHEMA_VERSION,
            };
            let line = serde_json::to_string(&ev).unwrap();
            let back: Event = serde_json::from_str(&line).unwrap();
            assert_eq!(back, ev, "envelope round-trip for {}", kind.as_str());
            // The kind tag is exactly the tabled serde name.
            assert_eq!(tag_of(&line), kind.as_str());
        }
    }

    /// `session/ended` carries every `EndReason`; `interrupted` is distinct
    /// from `failed` (honesty, constraint 7).
    #[test]
    fn session_ended_round_trips_every_end_reason() {
        for (reason, golden_reason) in [
            (EndReason::Completed, "completed"),
            (EndReason::Cancelled, "cancelled"),
            (EndReason::Interrupted, "interrupted"),
            (EndReason::Failed, "failed"),
            (EndReason::AwaitingInput, "awaiting_input"),
            (EndReason::Rotated, "rotated"),
        ] {
            let kind = EventKind::SessionEnded { reason };
            let ser = serde_json::to_string(&kind).unwrap();
            assert_eq!(
                ser,
                format!(r#"{{"kind":"session/ended","reason":"{golden_reason}"}}"#)
            );
            let back: EventKind = serde_json::from_str(&ser).unwrap();
            assert_eq!(back, kind);
        }
    }

    /// The v0 envelope has an exact, frozen JSON shape — field order
    /// included (cache-byte discipline starts at the envelope).
    #[test]
    fn event_envelope_golden_json_and_round_trip() {
        let ev = sample_event(EventKind::TurnStart);
        let golden = concat!(
            r#"{"seq":7,"session":"00000000-0000-0000-0000-000000000000","#,
            r#""epoch":3,"ts":1761900000000,"kind":"turn/start","#,
            r#""bytes":{"opaque":true},"version":0}"#
        );
        assert_eq!(serde_json::to_string(&ev).unwrap(), golden);
        let back: Event = serde_json::from_str(golden).unwrap();
        assert_eq!(back, ev);
    }

    /// `session/ended` flattens its `EndReason` into the envelope and the
    /// envelope still round-trips.
    #[test]
    fn event_envelope_session_ended_golden_json() {
        let ev = sample_event(EventKind::SessionEnded {
            reason: EndReason::Interrupted,
        });
        let golden = concat!(
            r#"{"seq":7,"session":"00000000-0000-0000-0000-000000000000","#,
            r#""epoch":3,"ts":1761900000000,"kind":"session/ended","#,
            r#""reason":"interrupted","bytes":{"opaque":true},"version":0}"#
        );
        assert_eq!(serde_json::to_string(&ev).unwrap(), golden);
        let back: Event = serde_json::from_str(golden).unwrap();
        assert_eq!(back, ev);
    }

    /// Envelope `version` defaults to 0 (v0) when absent (DESIGN §3.3).
    #[test]
    fn envelope_version_defaults_to_zero_when_absent() {
        let ev = sample_event(EventKind::Compact);
        let mut v = serde_json::to_value(&ev).unwrap();
        assert_eq!(v.get("version"), Some(&json!(SCHEMA_VERSION)));
        assert_eq!(SCHEMA_VERSION, 0);
        v.as_object_mut().unwrap().remove("version");
        let back: Event = serde_json::from_value(v).unwrap();
        assert_eq!(back.version, 0);
        assert_eq!(back, ev);
    }

    // -- derived event id -------------------------------------------------

    /// Golden vectors for the derived event id rule: `evt_` + RFC 4122
    /// v5(session uuid, "kian:event:<seq>"). The literals are generated
    /// by an independent implementation (Python `uuid.uuid5`) so the
    /// Rust `uuid` crate and any mirror (SDK, external tooling) are
    /// pinned to the same bytes.
    #[test]
    fn event_id_golden_vectors_match_rfc4122_v5() {
        let ns = SessionId(Uuid::parse_str("0f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b").unwrap());
        let cases: &[(u64, &str)] = &[
            (1, "evt_ec671a02-5108-5e7a-811a-3517bea635e6"),
            (7, "evt_42124c15-3948-56f1-9606-517dc61d2829"),
            (42, "evt_b1a35135-d4f4-50cd-b457-e8b96fb9e5dd"),
        ];
        for (seq, expected) in cases {
            assert_eq!(&super::event_id(&ns, *seq), expected, "seq {seq}");
        }
        // A second namespace pins the "distinct per session" half of the
        // rule with its own golden literal.
        let ns2 = SessionId(Uuid::parse_str("00000000-0000-0000-0000-000000000001").unwrap());
        assert_eq!(
            super::event_id(&ns2, 7),
            "evt_ac6b5bb7-fa8f-5314-868d-4c342a94d273"
        );
    }

    /// The derived id is deterministic, distinct per seq, distinct per
    /// session, and never collides across a (session × seq) grid.
    #[test]
    fn event_id_is_deterministic_and_distinct_per_seq_and_session() {
        let a = SessionId(Uuid::parse_str("0f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b").unwrap());
        let b = SessionId(Uuid::new_v4());
        // Determinism.
        assert_eq!(super::event_id(&a, 1), super::event_id(&a, 1));
        // Distinct per seq within a session.
        let ids: Vec<String> = (0..64).map(|seq| super::event_id(&a, seq)).collect();
        let mut unique = ids.clone();
        unique.sort();
        unique.dedup();
        assert_eq!(ids.len(), unique.len(), "ids collide across seq");
        // Distinct per session at the same seq.
        assert_ne!(super::event_id(&a, 7), super::event_id(&b, 7));
        // Every id carries the documented prefix and a v5-shaped uuid.
        for id in &ids {
            assert!(id.starts_with("evt_"));
            assert_eq!(id.len(), "evt_".len() + 36);
            assert_eq!(id.as_bytes()[18], b'5', "uuid version nibble must be 5");
        }
    }
}