zagens-core 0.8.2

Core runtime boundaries for Zagens agent architecture
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
//! KernelEvent schema — Phase 3a (立契约).
//!
//! The single source of truth for all observable state transitions in a turn.
//! Every variant is `#[non_exhaustive]`; consumers must handle unknown variants
//! gracefully. Adding fields is backward-compatible; removing or renaming
//! requires a schema upcast function + `schema_version` bump.
//!
//! **Status**: v1 (2026-06-15). Double-write started in Phase 3a; only
//! consumed in Phase 3b once completeness verification passes.

use serde::{Deserialize, Serialize};

use crate::engine::request_fingerprint::RequestFingerprint;
use crate::models::Usage;
use crate::turn::{TurnLoopMode, TurnOutcomeStatus};

// ── Opaque ID aliases ────────────────────────────────────────────────────────

/// Unique identifier for a turn (maps to `TurnContext::id`).
pub type TurnId = String;
/// Unique identifier for a single tool call attempt.
pub type CallId = String;
/// Identifier for a compaction/snapshot artifact stored in the artifact store.
pub type ArtifactId = String;

// ── Supporting enums ─────────────────────────────────────────────────────────

/// Reason a turn ended. Supersedes bare `TurnOutcomeStatus` with richer
/// detail so that the state-machine can branch without out-of-band flags.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum TurnOutcome {
    Completed,
    Failed { message: String },
    Interrupted,
    Budget,
    LoopGuard { reason: String },
    MaxSteps,
    CycleHandoff { next_cycle: u32 },
}

impl TurnOutcome {
    /// Map to the legacy `TurnOutcomeStatus` for callers not yet migrated.
    #[must_use]
    pub fn as_status(&self) -> TurnOutcomeStatus {
        match self {
            TurnOutcome::Completed => TurnOutcomeStatus::Completed,
            TurnOutcome::Interrupted => TurnOutcomeStatus::Interrupted,
            _ => TurnOutcomeStatus::Failed,
        }
    }
}

/// Stream delta type during model response.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum DeltaKind {
    Text,
    ThinkingText,
    ToolCallArg,
}

/// Resolved outcome for a single tool call.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ToolOutcome {
    /// Tool ran and returned a non-error result (may be empty).
    Success,
    /// Blocked pre-execution (loop-guard duplicate or approval rejected).
    Blocked { reason: String },
    /// Loop guard halted the turn during or after execution.
    GuardHalt { reason: String },
    /// Tool process or network timed out.
    Timeout,
    /// Tool returned a tool-level error (not kernel error).
    ToolError { message: String },
}

/// Whether the user approved or rejected a planned tool call.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ApprovalVerdict {
    Approved,
    Rejected,
    /// User chose retry with an elevated sandbox policy (not a plain approval).
    Retried,
}

/// Policy metadata resolved for a planned call (subset relevant to replay).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PolicyDecision {
    pub approval_required: bool,
    pub parallel_eligible: bool,
    pub read_only: bool,
}

impl PolicyDecision {
    #[must_use]
    pub fn new(approval_required: bool, parallel_eligible: bool, read_only: bool) -> Self {
        Self {
            approval_required,
            parallel_eligible,
            read_only,
        }
    }
}

/// Strategy applied when recovering from a context overflow.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum OverflowStrategy {
    BudgetRecompile,
    LlmCompaction,
    CycleHandoff,
}

/// Which code path triggered a capacity checkpoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CapacityCheckpointKind {
    PreRequest,
    PostTool,
    ErrorEscalation,
}

/// What the capacity subsystem decided to do at a checkpoint.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CapacityAction {
    Continue,
    Trim,
    Handoff,
    Abort { reason: String },
}

impl CapacityAction {
    /// Map capacity-controller output to kernel schema action.
    #[must_use]
    pub fn from_guardrail(action: crate::capacity::GuardrailAction, reason: &str) -> Self {
        let mapped = match action {
            crate::capacity::GuardrailAction::NoIntervention => Self::Continue,
            crate::capacity::GuardrailAction::TargetedContextRefresh => Self::Trim,
            crate::capacity::GuardrailAction::VerifyWithToolReplay => Self::Continue,
            crate::capacity::GuardrailAction::VerifyAndReplan => Self::Handoff,
        };
        if !reason.is_empty() && !matches!(mapped, Self::Continue) {
            tracing::debug!(
                target: "capacity",
                guardrail = ?action,
                kernel_action = ?mapped,
                reason,
                "CapacityAction mapped from guardrail"
            );
        }
        mapped
    }
}

/// Inclusive message-index range `[from, to]` (0-based, across all messages).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct MessageRange {
    pub from: u32,
    pub to: u32,
}

// ── KernelEvent ──────────────────────────────────────────────────────────────

/// Append-only, strongly-typed, monotonically-sequenced log of all observable
/// state transitions in a single turn.
///
/// Replaces: `core::Event` free-string table, `RuntimeEventRecord`, `EventFrame`,
/// and SSE-compat surface — four representations become one.
///
/// # Schema evolution
/// - **Adding a variant**: add `#[serde(default)]` fields; increment nothing.
/// - **Adding a field** to existing variant: add `#[serde(default)]`; no bump.
/// - **Removing or renaming**: provide `upcast_v{N}_to_v{N+1}()` and bump
///   [`SchemaVersion`](KernelEvent::SchemaVersion).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "event_type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum KernelEvent {
    // ── Schema sentinel ─────────────────────────────────────────────────────
    /// First record in every `kernel_events` table partition.
    /// Currently always `version: 1`.
    SchemaVersion {
        version: u32,
    },

    // ── Turn lifecycle ───────────────────────────────────────────────────────
    TurnStarted {
        turn_id: TurnId,
        mode: TurnLoopMode,
        /// Serialised user message text. Attachments / large blobs: `None` with
        /// a separate `EmitArtifact` event carrying the blob reference.
        input_text: String,
        max_steps: u32,
    },
    TurnEnded {
        turn_id: TurnId,
        outcome: TurnOutcome,
        total_steps: u32,
    },

    // ── Model request ────────────────────────────────────────────────────────
    ModelRequestIssued {
        turn_id: TurnId,
        step_idx: u32,
        request_fp: RequestFingerprint,
        /// Resolved token budget for this request.
        token_budget: u32,
    },
    ModelDelta {
        turn_id: TurnId,
        step_idx: u32,
        kind: DeltaKind,
        /// For `ToolCallArg` deltas: the call_id of the in-progress tool block.
        call_id: Option<CallId>,
        text: String,
    },
    ModelMessage {
        turn_id: TurnId,
        step_idx: u32,
        usage: Usage,
        /// Number of content blocks in the final message (avoids embedding full
        /// message text which belongs in the session store).
        block_count: u32,
        /// Truncated assistant text for log-driven transcript rebuild (Phase 3b 5c).
        #[serde(default, skip_serializing_if = "String::is_empty")]
        text_preview: String,
        /// Full assistant text written to session JSON (Phase 3b 5c closure).
        #[serde(default, skip_serializing_if = "String::is_empty")]
        assistant_text: String,
    },

    // ── Tool calls ───────────────────────────────────────────────────────────
    ToolCallPlanned {
        turn_id: TurnId,
        step_idx: u32,
        call_id: CallId,
        tool_name: String,
        /// JSON-serialised tool input. Large inputs (>16 KB) are stored as an
        /// artifact reference; this field holds the truncated preview or `{}`
        /// with a `large_input_artifact_id` field added.
        input_json: String,
        decision: PolicyDecision,
    },
    ToolCallStarted {
        turn_id: TurnId,
        call_id: CallId,
        /// DAG wave index (0 in legacy sequential mode).
        wave_idx: u32,
    },
    ToolCallFinished {
        turn_id: TurnId,
        call_id: CallId,
        tool_name: String,
        outcome: ToolOutcome,
        duration_ms: u32,
        /// Whether the tool performed any filesystem/state writes. Mirrors
        /// `tool_writes_state()` at call time; used to rebuild deferred-tool
        /// activation projection in Phase 3b.
        wrote_state: bool,
        /// Truncated tool result / error text for log-driven transcript rebuild (Phase 3b 5c).
        #[serde(default, skip_serializing_if = "String::is_empty")]
        result_preview: String,
        /// Exact tool-result body written to session JSON (Phase 3b 5c closure).
        #[serde(default, skip_serializing_if = "String::is_empty")]
        session_content: String,
    },
    ApprovalResolved {
        turn_id: TurnId,
        call_id: CallId,
        verdict: ApprovalVerdict,
    },

    // ── Context & compaction ─────────────────────────────────────────────────
    CompactionArtifactCreated {
        turn_id: TurnId,
        artifact_id: ArtifactId,
        /// Message indices `[from, to]` replaced by this compaction artifact.
        replaced_range: MessageRange,
        summary_token_count: u32,
    },
    ContextOverflowRecovered {
        turn_id: TurnId,
        step_idx: u32,
        strategy: OverflowStrategy,
        /// Token budget cap applied during budget-recompile (None for other strategies).
        source_budget_cap: Option<u32>,
    },

    // ── Memory injections ────────────────────────────────────────────────────
    SteerInjected {
        turn_id: TurnId,
        step_idx: u32,
        text: String,
    },
    ScratchpadReminderInjected {
        turn_id: TurnId,
        step_idx: u32,
        area_path: String,
    },
    ScratchpadSummaryInjected {
        turn_id: TurnId,
        /// Step at which the summary was first injected. Subsequent steps read
        /// the flag from the projection without a new event.
        at_step: u32,
    },
    CycleBriefingInjected {
        turn_id: TurnId,
        cycle: u32,
        step_idx: u32,
    },
    /// Episodic topic-memory block injected into the system prompt (B2 double-write).
    TopicMemoryInjected {
        turn_id: TurnId,
        step_idx: u32,
        /// Estimated tokens in the injected `<topic_memory>` block.
        #[serde(default)]
        block_token_est: u32,
    },
    /// Read-side memory plane query executed (v3 `Effect::QueryMemory` double-write).
    MemoryPlaneQueried {
        turn_id: TurnId,
        step_idx: u32,
        /// `working` | `episodic` | `archival`
        layer: String,
        query_key: String,
        /// ContextCompiler source id resolved for this query (empty when unknown).
        #[serde(default)]
        compiler_source: String,
    },
    /// Flash layered-context seam appended as assistant message (v3 `#159` double-write).
    LayeredContextSeamInjected {
        turn_id: TurnId,
        step_idx: u32,
        level: u32,
        messages_covered: u32,
        /// Truncated seam text for observability (full body remains in session store until 5c).
        text_preview: String,
    },

    // ── Guard decisions ──────────────────────────────────────────────────────
    LoopGuardTriggered {
        turn_id: TurnId,
        call_id: CallId,
        /// "identical_call" | "deferred_set_area_batch" | "failure_halt" …
        reason: String,
    },
    CapacityCheckpoint {
        turn_id: TurnId,
        step_idx: u32,
        kind: CapacityCheckpointKind,
        tokens_used: u32,
        token_budget: u32,
        action: CapacityAction,
        /// When true, a proposed guardrail was suppressed by cooldown (replay → `Effect::Sleep`).
        #[serde(default)]
        cooldown_blocked: bool,
    },

    // ── LHT / Cycle continuation ─────────────────────────────────────────────
    CycleAdvanced {
        turn_id: TurnId,
        from_cycle: u32,
        to_cycle: u32,
        /// Human-readable reason emitted by the LHT cycle-advance hook.
        reason: String,
    },
    StepLimitContinuation {
        turn_id: TurnId,
        step_idx: u32,
        lht_objective_injected: bool,
    },
    LoopGuardContinuation {
        turn_id: TurnId,
        step_idx: u32,
    },

    // ── Tool catalog mutations ────────────────────────────────────────────────
    /// A previously-deferred tool was promoted into the active tool set.
    ///
    /// This event is emitted by `maybe_activate_deferred_tool` whenever the
    /// model requests a tool that was not yet active. It is necessary for
    /// Phase 3b: `active_tool_names` (host state) must be rebuildable from
    /// the log so that `TurnMachine::step` can be a pure function.
    DeferredToolActivated {
        turn_id: TurnId,
        step_idx: u32,
        tool_name: String,
    },
}

impl KernelEvent {
    /// Extract the `turn_id` field present in every variant except
    /// [`KernelEvent::SchemaVersion`].
    #[must_use]
    pub fn turn_id(&self) -> Option<&str> {
        match self {
            KernelEvent::SchemaVersion { .. } => None,
            KernelEvent::TurnStarted { turn_id, .. }
            | KernelEvent::TurnEnded { turn_id, .. }
            | KernelEvent::ModelRequestIssued { turn_id, .. }
            | KernelEvent::ModelDelta { turn_id, .. }
            | KernelEvent::ModelMessage { turn_id, .. }
            | KernelEvent::ToolCallPlanned { turn_id, .. }
            | KernelEvent::ToolCallStarted { turn_id, .. }
            | KernelEvent::ToolCallFinished { turn_id, .. }
            | KernelEvent::ApprovalResolved { turn_id, .. }
            | KernelEvent::CompactionArtifactCreated { turn_id, .. }
            | KernelEvent::ContextOverflowRecovered { turn_id, .. }
            | KernelEvent::SteerInjected { turn_id, .. }
            | KernelEvent::ScratchpadReminderInjected { turn_id, .. }
            | KernelEvent::ScratchpadSummaryInjected { turn_id, .. }
            | KernelEvent::CycleBriefingInjected { turn_id, .. }
            | KernelEvent::TopicMemoryInjected { turn_id, .. }
            | KernelEvent::MemoryPlaneQueried { turn_id, .. }
            | KernelEvent::LayeredContextSeamInjected { turn_id, .. }
            | KernelEvent::LoopGuardTriggered { turn_id, .. }
            | KernelEvent::CapacityCheckpoint { turn_id, .. }
            | KernelEvent::CycleAdvanced { turn_id, .. }
            | KernelEvent::StepLimitContinuation { turn_id, .. }
            | KernelEvent::LoopGuardContinuation { turn_id, .. }
            | KernelEvent::DeferredToolActivated { turn_id, .. } => Some(turn_id.as_str()),
        }
    }

    /// Variant name string for logging and schema drift CI.
    #[must_use]
    pub fn kind_str(&self) -> &'static str {
        match self {
            KernelEvent::SchemaVersion { .. } => "schema_version",
            KernelEvent::TurnStarted { .. } => "turn_started",
            KernelEvent::TurnEnded { .. } => "turn_ended",
            KernelEvent::ModelRequestIssued { .. } => "model_request_issued",
            KernelEvent::ModelDelta { .. } => "model_delta",
            KernelEvent::ModelMessage { .. } => "model_message",
            KernelEvent::ToolCallPlanned { .. } => "tool_call_planned",
            KernelEvent::ToolCallStarted { .. } => "tool_call_started",
            KernelEvent::ToolCallFinished { .. } => "tool_call_finished",
            KernelEvent::ApprovalResolved { .. } => "approval_resolved",
            KernelEvent::CompactionArtifactCreated { .. } => "compaction_artifact_created",
            KernelEvent::ContextOverflowRecovered { .. } => "context_overflow_recovered",
            KernelEvent::SteerInjected { .. } => "steer_injected",
            KernelEvent::ScratchpadReminderInjected { .. } => "scratchpad_reminder_injected",
            KernelEvent::ScratchpadSummaryInjected { .. } => "scratchpad_summary_injected",
            KernelEvent::CycleBriefingInjected { .. } => "cycle_briefing_injected",
            KernelEvent::TopicMemoryInjected { .. } => "topic_memory_injected",
            KernelEvent::MemoryPlaneQueried { .. } => "memory_plane_queried",
            KernelEvent::LayeredContextSeamInjected { .. } => "layered_context_seam_injected",
            KernelEvent::LoopGuardTriggered { .. } => "loop_guard_triggered",
            KernelEvent::CapacityCheckpoint { .. } => "capacity_checkpoint",
            KernelEvent::CycleAdvanced { .. } => "cycle_advanced",
            KernelEvent::StepLimitContinuation { .. } => "step_limit_continuation",
            KernelEvent::LoopGuardContinuation { .. } => "loop_guard_continuation",
            KernelEvent::DeferredToolActivated { .. } => "deferred_tool_activated",
        }
    }
}

// ── Envelope for log persistence ──────────────────────────────────────────────

/// Row written to the `kernel_events` SQLite table.
///
/// The `seq` field is a global monotone counter within a runtime session
/// (not per-turn); the `(turn_id, seq)` pair is unique across the table.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KernelEventEnvelope {
    /// Global monotone sequence number assigned by the writer.
    pub seq: u64,
    /// Unix timestamp (milliseconds).
    pub ts_ms: u64,
    /// Variant name (mirrors `KernelEvent::kind_str()`).
    pub kind: String,
    pub event: KernelEvent,
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_turn_id() -> TurnId {
        "test-turn-001".to_string()
    }

    #[test]
    fn turn_started_round_trips() {
        let ev = KernelEvent::TurnStarted {
            turn_id: make_turn_id(),
            mode: TurnLoopMode::Agent,
            input_text: "hello".to_string(),
            max_steps: 20,
        };
        let json = serde_json::to_string(&ev).expect("serialize");
        let back: KernelEvent = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.kind_str(), "turn_started");
        assert_eq!(back.turn_id(), Some("test-turn-001"));
    }

    #[test]
    fn turn_ended_round_trips() {
        let ev = KernelEvent::TurnEnded {
            turn_id: make_turn_id(),
            outcome: TurnOutcome::LoopGuard {
                reason: "identical call".to_string(),
            },
            total_steps: 7,
        };
        let json = serde_json::to_string(&ev).expect("serialize");
        let back: KernelEvent = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.kind_str(), "turn_ended");
    }

    #[test]
    fn tool_call_round_trips() {
        let ev = KernelEvent::ToolCallFinished {
            turn_id: make_turn_id(),
            call_id: "call-abc".to_string(),
            tool_name: "read_file".to_string(),
            outcome: ToolOutcome::Success,
            duration_ms: 120,
            wrote_state: true,
            result_preview: String::new(),
            session_content: String::new(),
        };
        let json = serde_json::to_string(&ev).expect("serialize");
        let back: KernelEvent = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.kind_str(), "tool_call_finished");
    }

    #[test]
    fn capacity_checkpoint_round_trips() {
        let ev = KernelEvent::CapacityCheckpoint {
            turn_id: make_turn_id(),
            step_idx: 3,
            kind: CapacityCheckpointKind::PostTool,
            tokens_used: 8000,
            token_budget: 32000,
            action: CapacityAction::Continue,
            cooldown_blocked: false,
        };
        let json = serde_json::to_string(&ev).expect("serialize");
        let _back: KernelEvent = serde_json::from_str(&json).expect("deserialize");
    }

    #[test]
    fn schema_version_has_no_turn_id() {
        let ev = KernelEvent::SchemaVersion { version: 1 };
        assert!(ev.turn_id().is_none());
        assert_eq!(ev.kind_str(), "schema_version");
    }

    #[test]
    fn turn_outcome_as_status_mapping() {
        assert_eq!(
            TurnOutcome::Completed.as_status(),
            TurnOutcomeStatus::Completed
        );
        assert_eq!(
            TurnOutcome::Interrupted.as_status(),
            TurnOutcomeStatus::Interrupted
        );
        assert_eq!(TurnOutcome::Budget.as_status(), TurnOutcomeStatus::Failed);
    }

    // ── Schema completeness verification (Phase 3a) ──────────────────────────
    //
    // Each test below demonstrates that a specific "A-class" host state field
    // can be rebuilt purely from a KernelEvent sequence.  These are the
    // preconditions for TurnMachine::step to be a pure function in Phase 3b.

    /// Projection helper: rebuild `scratchpad_summary_injected` flag.
    /// Rule: true iff a `ScratchpadSummaryInjected` event appeared for this turn.
    fn proj_scratchpad_summary_injected(events: &[KernelEvent], turn: &str) -> bool {
        events.iter().any(|ev| {
            matches!(ev, KernelEvent::ScratchpadSummaryInjected { turn_id, .. }
                if turn_id == turn)
        })
    }

    #[test]
    fn completeness_scratchpad_summary_injected() {
        let tid = "t1".to_string();
        let events: Vec<KernelEvent> = vec![
            KernelEvent::TurnStarted {
                turn_id: tid.clone(),
                mode: TurnLoopMode::Agent,
                input_text: "do stuff".into(),
                max_steps: 10,
            },
            KernelEvent::ScratchpadSummaryInjected {
                turn_id: tid.clone(),
                at_step: 2,
            },
        ];
        assert!(proj_scratchpad_summary_injected(&events, &tid));

        // Without the event, flag is false.
        let empty: Vec<KernelEvent> = vec![KernelEvent::TurnStarted {
            turn_id: tid.clone(),
            mode: TurnLoopMode::Agent,
            input_text: "do stuff".into(),
            max_steps: 10,
        }];
        assert!(!proj_scratchpad_summary_injected(&empty, &tid));
    }

    /// Projection helper: rebuild `active_tool_names` set.
    /// Rule: starts from initial set; a `DeferredToolActivated` event adds to it.
    fn proj_active_tools<'a>(
        events: &'a [KernelEvent],
        turn: &str,
        initial: &[&'a str],
    ) -> std::collections::HashSet<String> {
        let mut active: std::collections::HashSet<String> =
            initial.iter().map(|s| s.to_string()).collect();
        for ev in events {
            if let KernelEvent::DeferredToolActivated {
                turn_id, tool_name, ..
            } = ev
                && turn_id == turn
            {
                active.insert(tool_name.clone());
            }
        }
        active
    }

    #[test]
    fn completeness_deferred_tool_activation() {
        let tid = "t2".to_string();
        let initial = &["read_file", "shell_exec"];
        let events = vec![
            KernelEvent::TurnStarted {
                turn_id: tid.clone(),
                mode: TurnLoopMode::Agent,
                input_text: "search for foo".into(),
                max_steps: 10,
            },
            KernelEvent::DeferredToolActivated {
                turn_id: tid.clone(),
                step_idx: 1,
                tool_name: "tool_search_tool_regex".to_string(),
            },
        ];
        let active = proj_active_tools(&events, &tid, initial);
        assert!(active.contains("tool_search_tool_regex"));
        assert!(active.contains("read_file"));
        // A tool NOT activated should not appear.
        assert!(!active.contains("write_file"));
    }

    /// Projection helper: rebuild `ScratchpadStepState` counters for the
    /// **current** step (since last `ModelRequestIssued`).
    /// Rule: reset at each ModelRequestIssued; increment from ToolCallFinished.
    #[derive(Default, PartialEq, Eq, Debug)]
    struct ScratchpadCounters {
        readonly_successes: usize,
        scratchpad_writes: usize,
    }

    fn proj_scratchpad_step_state(events: &[KernelEvent], turn: &str) -> ScratchpadCounters {
        let mut counters = ScratchpadCounters::default();
        for ev in events {
            match ev {
                // Reset at each new model request for this turn.
                KernelEvent::ModelRequestIssued { turn_id, .. } if turn_id == turn => {
                    counters = ScratchpadCounters::default();
                }
                KernelEvent::ToolCallFinished {
                    turn_id,
                    outcome,
                    wrote_state,
                    tool_name,
                    ..
                } if turn_id == turn => {
                    if matches!(outcome, ToolOutcome::Success) {
                        if *wrote_state {
                            // scratchpad_append / scratchpad_set_area
                            if tool_name.starts_with("scratchpad_") {
                                counters.scratchpad_writes += 1;
                            }
                        } else {
                            counters.readonly_successes += 1;
                        }
                    }
                }
                _ => {}
            }
        }
        counters
    }

    #[test]
    fn completeness_scratchpad_step_state_projection() {
        let tid = "t3".to_string();
        let fp = crate::engine::request_fingerprint::RequestFingerprint {
            static_prefix_sha256: "aaa".into(),
            full_prefix_sha256: "bbb".into(),
        };
        let events = vec![
            KernelEvent::ModelRequestIssued {
                turn_id: tid.clone(),
                step_idx: 1,
                request_fp: fp.clone(),
                token_budget: 32000,
            },
            KernelEvent::ToolCallFinished {
                turn_id: tid.clone(),
                call_id: "c1".into(),
                outcome: ToolOutcome::Success,
                duration_ms: 50,
                wrote_state: false,
                tool_name: "read_file".into(),
                result_preview: String::new(),
                session_content: String::new(),
            },
            KernelEvent::ToolCallFinished {
                turn_id: tid.clone(),
                call_id: "c2".into(),
                outcome: ToolOutcome::Success,
                duration_ms: 30,
                wrote_state: false,
                tool_name: "shell_exec".into(),
                result_preview: String::new(),
                session_content: String::new(),
            },
            // Second model request resets counters.
            KernelEvent::ModelRequestIssued {
                turn_id: tid.clone(),
                step_idx: 2,
                request_fp: fp,
                token_budget: 32000,
            },
            KernelEvent::ToolCallFinished {
                turn_id: tid.clone(),
                call_id: "c3".into(),
                outcome: ToolOutcome::Success,
                duration_ms: 20,
                wrote_state: true,
                tool_name: "scratchpad_append".into(),
                result_preview: String::new(),
                session_content: String::new(),
            },
        ];

        let state = proj_scratchpad_step_state(&events, &tid);
        // After step 2: only the scratchpad_append write, no readonly hits.
        assert_eq!(state.readonly_successes, 0);
        assert_eq!(state.scratchpad_writes, 1);
    }

    /// Projection helper: rebuild LHT continuation step count.
    /// Rule: count of `StepLimitContinuation` events for this turn.
    fn proj_lht_continuation_count(events: &[KernelEvent], turn: &str) -> u32 {
        events
            .iter()
            .filter(|ev| {
                matches!(ev, KernelEvent::StepLimitContinuation { turn_id, .. }
                    if turn_id == turn)
            })
            .count() as u32
    }

    #[test]
    fn completeness_lht_continuation_count() {
        let tid = "t4".to_string();
        let events = vec![
            KernelEvent::StepLimitContinuation {
                turn_id: tid.clone(),
                step_idx: 20,
                lht_objective_injected: true,
            },
            KernelEvent::StepLimitContinuation {
                turn_id: tid.clone(),
                step_idx: 40,
                lht_objective_injected: false,
            },
        ];
        assert_eq!(proj_lht_continuation_count(&events, &tid), 2);
        assert_eq!(proj_lht_continuation_count(&events, "other-turn"), 0);
    }

    #[test]
    fn completeness_steer_injection_consumed() {
        // Rule: steer is consumed if a SteerInjected event exists at or before
        // the current step. This is a boolean projection.
        let tid = "t5".to_string();
        let events = [KernelEvent::SteerInjected {
            turn_id: tid.clone(),
            step_idx: 3,
            text: "change approach".into(),
        }];
        let injected = events
            .iter()
            .any(|ev| matches!(ev, KernelEvent::SteerInjected { turn_id, .. } if turn_id == &tid));
        assert!(injected);
    }

    #[test]
    fn completeness_capacity_state_projection() {
        // Rule: the most recent CapacityCheckpoint.action for the turn determines
        // capacity state. If it's Abort, the turn should have ended.
        let tid = "t6".to_string();
        let events = [
            KernelEvent::CapacityCheckpoint {
                turn_id: tid.clone(),
                step_idx: 1,
                kind: CapacityCheckpointKind::PreRequest,
                tokens_used: 5000,
                token_budget: 32000,
                action: CapacityAction::Continue,
                cooldown_blocked: false,
            },
            KernelEvent::CapacityCheckpoint {
                turn_id: tid.clone(),
                step_idx: 2,
                kind: CapacityCheckpointKind::PostTool,
                tokens_used: 28000,
                token_budget: 32000,
                action: CapacityAction::Trim,
                cooldown_blocked: false,
            },
        ];
        let last_action = events
            .iter()
            .filter_map(|ev| {
                if let KernelEvent::CapacityCheckpoint {
                    turn_id, action, ..
                } = ev
                    && turn_id == &tid
                {
                    return Some(action.clone());
                }
                None
            })
            .next_back();
        assert_eq!(last_action, Some(CapacityAction::Trim));
    }

    #[test]
    fn deferred_tool_activated_round_trips() {
        let ev = KernelEvent::DeferredToolActivated {
            turn_id: "t7".into(),
            step_idx: 2,
            tool_name: "tool_search_bm25".into(),
        };
        let json = serde_json::to_string(&ev).expect("serialize");
        let back: KernelEvent = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.kind_str(), "deferred_tool_activated");
    }

    // ── Schema drift CI (Phase 3a §2.3) ─────────────────────────────────────
    //
    // These tests pin the exact JSON shape of representative KernelEvent
    // variants. If the serde layout changes (rename, tag rename, new field
    // without #[serde(default)], etc.), the string comparison fails immediately
    // rather than silently breaking deserialization of stored logs.
    //
    // Update the golden strings ONLY when a schema version bump is also done
    // (add a `SchemaVersion` event with incremented version AND an upcast fn).

    #[test]
    fn schema_drift_turn_started_shape() {
        let ev = KernelEvent::TurnStarted {
            turn_id: "TURN-001".into(),
            mode: TurnLoopMode::Agent,
            input_text: "hello".into(),
            max_steps: 20,
        };
        let json = serde_json::to_string(&ev).expect("serialize");
        // Pin the field names and tag value.  New *optional* fields are ok
        // (they'll appear in the JSON but won't break old readers with
        // #[serde(default)]).  Renaming any field here is a schema break.
        assert!(
            json.contains(r#""event_type":"turn_started""#),
            "tag must be event_type:turn_started, got: {json}"
        );
        assert!(
            json.contains(r#""turn_id":"TURN-001""#),
            "missing turn_id, got: {json}"
        );
        assert!(
            json.contains(r#""mode":"agent""#),
            "missing mode, got: {json}"
        );
        assert!(
            json.contains(r#""input_text":"hello""#),
            "missing input_text, got: {json}"
        );
        assert!(
            json.contains(r#""max_steps":20"#),
            "missing max_steps, got: {json}"
        );
    }

    #[test]
    fn schema_drift_tool_call_finished_shape() {
        let ev = KernelEvent::ToolCallFinished {
            turn_id: "TURN-001".into(),
            call_id: "CALL-001".into(),
            tool_name: "read_file".into(),
            outcome: ToolOutcome::Success,
            duration_ms: 42,
            wrote_state: false,
            result_preview: String::new(),
            session_content: String::new(),
        };
        let json = serde_json::to_string(&ev).expect("serialize");
        assert!(
            json.contains(r#""event_type":"tool_call_finished""#),
            "tag drift: {json}"
        );
        assert!(
            json.contains(r#""call_id":"CALL-001""#),
            "missing call_id: {json}"
        );
        assert!(
            json.contains(r#""tool_name":"read_file""#),
            "missing tool_name: {json}"
        );
        assert!(
            json.contains(r#""wrote_state":false"#),
            "missing wrote_state: {json}"
        );
    }

    #[test]
    fn schema_drift_model_request_issued_shape() {
        let fp = crate::engine::request_fingerprint::RequestFingerprint {
            static_prefix_sha256: "aaabbb".into(),
            full_prefix_sha256: "cccddd".into(),
        };
        let ev = KernelEvent::ModelRequestIssued {
            turn_id: "TURN-001".into(),
            step_idx: 1,
            request_fp: fp,
            token_budget: 16000,
        };
        let json = serde_json::to_string(&ev).expect("serialize");
        assert!(
            json.contains(r#""event_type":"model_request_issued""#),
            "tag drift: {json}"
        );
        assert!(
            json.contains(r#""static_prefix_sha256":"aaabbb""#),
            "missing static fp: {json}"
        );
        assert!(
            json.contains(r#""token_budget":16000"#),
            "missing token_budget: {json}"
        );
    }

    #[test]
    fn schema_drift_deferred_tool_activated_shape() {
        let ev = KernelEvent::DeferredToolActivated {
            turn_id: "TURN-001".into(),
            step_idx: 3,
            tool_name: "tool_search_bm25".into(),
        };
        let json = serde_json::to_string(&ev).expect("serialize");
        assert!(
            json.contains(r#""event_type":"deferred_tool_activated""#),
            "tag drift: {json}"
        );
        assert!(
            json.contains(r#""tool_name":"tool_search_bm25""#),
            "missing tool_name: {json}"
        );
    }

    /// Verify all 22 variant kind strings are accounted for (prevents silent
    /// addition of variants without updating `kind_str()`).
    #[test]
    fn all_variants_have_kind_str() {
        // We can't enumerate non_exhaustive enums at compile time, but we can
        // verify the count we know about hasn't silently changed.
        let known_kinds = [
            "schema_version",
            "turn_started",
            "turn_ended",
            "model_request_issued",
            "model_delta",
            "model_message",
            "tool_call_planned",
            "tool_call_started",
            "tool_call_finished",
            "approval_resolved",
            "compaction_artifact_created",
            "context_overflow_recovered",
            "steer_injected",
            "scratchpad_reminder_injected",
            "scratchpad_summary_injected",
            "cycle_briefing_injected",
            "loop_guard_triggered",
            "capacity_checkpoint",
            "cycle_advanced",
            "step_limit_continuation",
            "loop_guard_continuation",
            "deferred_tool_activated",
        ];
        assert_eq!(
            known_kinds.len(),
            22,
            "Update this count when adding variants"
        );
    }
}