agent-types 0.9.0

Pure type definitions for the agent-base runtime
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
//! Checkpoint-related pure types: CheckpointData, CheckpointStep, ToolResultData.
//!
//! M3 seam: `Before/AfterActions` carry the action vocabulary (`Vec<Action>`)
//! and, on the way out, the backend's [`Observation`] (D3 observation replay —
//! resume rebuilds history from it and never re-executes). Pre-M3 payloads
//! (`Before/AfterToolCalls` with `(id, name, args)` triples) are read by the
//! T5 migration layer, not by serde aliases: the field shapes differ.
//!
//! Persistence contract (OV-5+11):
//! - **Versioned** — native writes carry [`CHECKPOINT_VERSION`]; a payload
//!   without a `version` field is the pre-versioning corpus and reads as 0.
//!   A payload newer than this build is refused explicitly (拒绝时明说).
//! - **Backend affinity** — `backend` pins the action protocol the payload
//!   speaks; [`CheckpointData::verify_for_resume`] refuses cross-backend
//!   resumes and label mismatches, spelling the mismatch out.
//! - **Source marks** — data converted on read keeps a [`MigrationSource`]
//!   mark: read what is on the wire, never guess, never backfill (不猜不回填).

use llm_trait::message::ChatMessage;
use serde::{Deserialize, Deserializer, Serialize, de};
use serde_json::Value;

use crate::action::{Action, ActionFormat, Observation};
use crate::run_state::RunState;
use crate::session::SessionId;

/// The checkpoint payload version this build writes and understands. Payloads
/// without a `version` field are pre-versioning and read as 0.
pub const CHECKPOINT_VERSION: u32 = 1;

#[derive(Clone, Debug, Serialize)]
pub struct CheckpointData {
    pub session_id: SessionId,
    pub user_input: String,
    pub step: CheckpointStep,
    pub turn_count: u32,
    /// Payload version ([`CHECKPOINT_VERSION`] on native writes; 0 for the
    /// pre-versioning corpus, which had no such field). Never backfilled.
    pub version: u32,
    /// Backend affinity pin (OV-5+11): the action protocol this payload
    /// speaks. Absent on the wire means the pre-labeling tool-call corpus —
    /// the step shape itself is the evidence.
    pub backend: ActionFormat,
    /// Source mark set when the payload was converted on read (T5 migration
    /// layer). Absent for natively written data.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub migrated_from: Option<MigrationSource>,
    /// The counting evidence (D5+OV-11): co-written with the step in the one
    /// journal line, so the recorded batch and the counts can never disagree
    /// across a crash window. Native writes ALWAYS carry it (`new` stores
    /// `Some`); on the wire it is optional so the pre-T7 corpus (and converted
    /// legacy payloads) stay readable with the field genuinely absent —
    /// restores of those fall back to the old-format paths, never guessed
    /// (不猜不回填).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub run_state: Option<RunState>,
}

impl CheckpointData {
    /// Native checkpoint of [`CHECKPOINT_VERSION`], pinned to `backend`, with
    /// the counting state co-written (D5+OV-11 事务同写).
    pub fn new(
        session_id: SessionId,
        user_input: impl Into<String>,
        step: CheckpointStep,
        turn_count: u32,
        backend: ActionFormat,
        run_state: RunState,
    ) -> Self {
        Self {
            session_id,
            user_input: user_input.into(),
            step,
            turn_count,
            version: CHECKPOINT_VERSION,
            backend,
            migrated_from: None,
            run_state: Some(run_state),
        }
    }

    /// The T5 persistence contract, checked before resume: the checkpoint must
    /// speak the resuming backend's protocol, and every action payload must
    /// carry a matching format label. Refusals spell out the mismatch.
    pub fn verify_for_resume(&self, expected: ActionFormat) -> Result<(), String> {
        if self.version > CHECKPOINT_VERSION {
            return Err(format!(
                "resume refused: checkpoint version {} is newer than this build supports ({CHECKPOINT_VERSION})",
                self.version
            ));
        }
        if self.backend != expected {
            return Err(format!(
                "resume refused: checkpoint is pinned to the {:?} backend, this runtime resumes {:?} checkpoints",
                self.backend, expected
            ));
        }
        self.verify_action_labels()
    }

    /// The same contract over the set of formats one runtime can resume
    /// (T10c): a session may interleave built-in tool-call turns with a
    /// mounted backend's turns, so the pin check accepts every format the
    /// runtime is assembled with — and refuses a pin naming a backend it does
    /// not carry ("mount it to resume this session"), never a silent replay
    /// through the wrong protocol.
    pub fn verify_for_resume_any(&self, accepted: &[ActionFormat]) -> Result<(), String> {
        if self.version > CHECKPOINT_VERSION {
            return Err(format!(
                "resume refused: checkpoint version {} is newer than this build supports ({CHECKPOINT_VERSION})",
                self.version
            ));
        }
        if !accepted.contains(&self.backend) {
            return Err(format!(
                "resume refused: checkpoint is pinned to the {:?} backend, this runtime resumes {:?} checkpoints",
                self.backend, accepted
            ));
        }
        self.verify_action_labels()
    }

    /// Every action payload must carry the pin's format label.
    fn verify_action_labels(&self) -> Result<(), String> {
        for action in self.step.actions() {
            if action.format != self.backend {
                return Err(format!(
                    "resume refused: checkpoint action '{}' carries format label {:?} but the checkpoint is pinned to {:?}",
                    action.id, action.format, self.backend
                ));
            }
        }
        Ok(())
    }
}

/// `CheckpointData` is read through the T5 migration layer: native shapes
/// first, then the pre-M3 shapes (converted, marked). Deserialize is manual so
/// the version/backend/source-mark contract lives in one place — Serialize
/// stays derived and always writes the native shape.
impl<'de> Deserialize<'de> for CheckpointData {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct Wire {
            session_id: SessionId,
            user_input: String,
            step: Value,
            turn_count: u32,
            #[serde(default)]
            version: Option<u32>,
            #[serde(default)]
            backend: Option<ActionFormat>,
            #[serde(default)]
            migrated_from: Option<MigrationSource>,
            #[serde(default)]
            run_state: Option<RunState>,
        }

        let wire = Wire::deserialize(deserializer)?;
        // Absent version = pre-versioning corpus (0). Never backfilled upward.
        let version = wire.version.unwrap_or(0);
        if version > CHECKPOINT_VERSION {
            return Err(de::Error::custom(format!(
                "checkpoint version {version} is newer than this build supports ({CHECKPOINT_VERSION}); refusing to read"
            )));
        }

        let (step, migrated_from) = match serde_json::from_value::<CheckpointStep>(
            wire.step.clone(),
        ) {
            Ok(step) => (step, wire.migrated_from),
            Err(native_err) => match serde_json::from_value::<LegacyStep>(wire.step) {
                Ok(legacy) => {
                    let (step, legacy_results) = legacy.into_step();
                    let mark = wire.migrated_from.unwrap_or(MigrationSource {
                        kind: MigrationKind::ToolCallTriplesV0,
                        legacy_results,
                    });
                    (step, Some(mark))
                }
                Err(_) => {
                    return Err(de::Error::custom(format!(
                        "unrecognized checkpoint step payload (not a native or pre-M3 shape): {native_err}"
                    )));
                }
            },
        };

        Ok(CheckpointData {
            session_id: wire.session_id,
            user_input: wire.user_input,
            step,
            turn_count: wire.turn_count,
            version,
            // Absent label = the pre-labeling tool-call corpus (the triples on
            // the wire are the evidence).
            backend: wire.backend.unwrap_or(ActionFormat::ToolCall),
            migrated_from,
            // Absent counting state = pre-T7 corpus (or a converted legacy
            // payload). Restores of those fall back to the old-format paths.
            run_state: wire.run_state,
        })
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum CheckpointStep {
    AfterUserInput,
    BeforeLlm {
        messages: Vec<ChatMessage>,
        tools: Vec<Value>,
    },
    /// About to execute a batch of actions (id-paired to the observation).
    BeforeActions {
        actions: Vec<Action>,
    },
    /// The batch ran; `observation` is what the backend observed (D3).
    AfterActions {
        actions: Vec<Action>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        observation: Option<Observation>,
    },
}

impl CheckpointStep {
    /// The batch this step pins (empty for non-action steps).
    pub fn actions(&self) -> &[Action] {
        match self {
            CheckpointStep::AfterUserInput | CheckpointStep::BeforeLlm { .. } => &[],
            CheckpointStep::BeforeActions { actions }
            | CheckpointStep::AfterActions { actions, .. } => actions,
        }
    }
}

/// Provenance mark for checkpoint data converted on read (T5 migration layer):
/// the payload was written by a pre-M3 binary in an older wire shape.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct MigrationSource {
    /// Which pre-M3 wire shape was converted.
    pub kind: MigrationKind,
    /// `AfterToolCalls { results }` records kept verbatim. The pre-M3 payload
    /// recorded no per-action fates, so nothing is guessed from them and no
    /// observation is backfilled (不猜不回填).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub legacy_results: Vec<ToolResultData>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MigrationKind {
    /// Pre-M3 `Before/AfterToolCalls` steps: `(id, name, args)` triples.
    ToolCallTriplesV0,
}

/// The pre-M3 checkpoint step wire shape (tool-call triples). Read-only
/// migration input — never constructed, never serialized.
#[derive(Deserialize)]
enum LegacyStep {
    BeforeToolCalls {
        tool_calls: Vec<(String, String, String)>,
    },
    AfterToolCalls {
        tool_calls: Vec<(String, String, String)>,
        #[serde(default)]
        results: Vec<ToolResultData>,
    },
}

impl LegacyStep {
    /// Convert to the action vocabulary. Nothing is guessed: triples become
    /// tool-call actions (labels and identity computed by the one constructor),
    /// and the pre-M3 `results` ride along as-is instead of being pressed into
    /// an `Observation` (the payload recorded no fates).
    fn into_step(self) -> (CheckpointStep, Vec<ToolResultData>) {
        fn actions(triples: Vec<(String, String, String)>) -> Vec<Action> {
            triples
                .into_iter()
                .map(|(id, name, args)| Action::from_tool_call(id, name, args))
                .collect()
        }
        match self {
            LegacyStep::BeforeToolCalls { tool_calls } => (
                CheckpointStep::BeforeActions {
                    actions: actions(tool_calls),
                },
                Vec::new(),
            ),
            LegacyStep::AfterToolCalls {
                tool_calls,
                results,
            } => (
                CheckpointStep::AfterActions {
                    actions: actions(tool_calls),
                    observation: None,
                },
                results,
            ),
        }
    }
}

/// Legacy per-result record from the pre-M3 checkpoint payload. Kept so the
/// T5 migration layer can read old `AfterToolCalls { results }` data.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ToolResultData {
    pub tool_call_id: String,
    pub tool_name: String,
    pub summary: String,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::action::{ActionFormat, ActionOutcome, BatchStatus};

    fn sample_messages() -> Vec<ChatMessage> {
        vec![
            ChatMessage::system("You are a helpful agent."),
            ChatMessage::user("What is the weather in Tokyo?"),
        ]
    }

    fn sample_tools() -> Vec<Value> {
        vec![serde_json::json!({"name": "weather", "args": {"city": "Tokyo"}})]
    }

    fn sample_actions() -> Vec<Action> {
        vec![
            Action {
                id: "call_1".to_string(),
                name: "weather".to_string(),
                args: Value::String(r#"{"city": "Tokyo"}"#.to_string()),
                identity: r#"weather::{"city":"Tokyo"}"#.to_string(),
                format: ActionFormat::ToolCall,
            },
            Action {
                id: "call_2".to_string(),
                name: "time".to_string(),
                args: Value::String(r#"{"tz": "Asia/Tokyo"}"#.to_string()),
                identity: r#"time::{"tz":"Asia/Tokyo"}"#.to_string(),
                format: ActionFormat::ToolCall,
            },
        ]
    }

    fn sample_observation() -> Observation {
        Observation {
            outcomes: vec![
                ActionOutcome {
                    action_id: "call_1".to_string(),
                    name: "weather".to_string(),
                    ok: true,
                    denied: false,
                    summary: "20C, clear".to_string(),
                    details: None,
                },
                ActionOutcome {
                    action_id: "call_2".to_string(),
                    name: "time".to_string(),
                    ok: true,
                    denied: false,
                    summary: "09:41 JST".to_string(),
                    details: None,
                },
            ],
            events: vec![],
            history_messages: vec![ChatMessage::tool("call_1", "20C, clear")],
            batch_status: BatchStatus::Completed,
        }
    }

    fn checkpoint_data(step: CheckpointStep) -> CheckpointData {
        CheckpointData::new(
            SessionId::with_external_id(42, "test-session"),
            "What is the weather in Tokyo?",
            step,
            3,
            ActionFormat::ToolCall,
            RunState::default(),
        )
    }

    #[test]
    fn after_user_input_variant_survives_serde() {
        let wire = serde_json::to_string(&CheckpointStep::AfterUserInput).unwrap();
        let back: CheckpointStep = serde_json::from_str(&wire).unwrap();
        assert!(matches!(back, CheckpointStep::AfterUserInput));
    }

    #[test]
    fn before_llm_variant_keeps_its_payload_through_serde() {
        let original = CheckpointStep::BeforeLlm {
            messages: sample_messages(),
            tools: sample_tools(),
        };
        let wire = serde_json::to_string(&original).unwrap();
        let back: CheckpointStep = serde_json::from_str(&wire).unwrap();
        match back {
            CheckpointStep::BeforeLlm { messages, tools } => {
                // ChatMessage has no PartialEq, so compare canonical JSON.
                assert_eq!(
                    serde_json::to_string(&messages).unwrap(),
                    serde_json::to_string(&sample_messages()).unwrap()
                );
                assert_eq!(tools, sample_tools());
            }
            other => panic!("expected BeforeLlm, got {other:?}"),
        }
    }

    #[test]
    fn before_actions_variant_keeps_its_payload_through_serde() {
        let original = CheckpointStep::BeforeActions {
            actions: sample_actions(),
        };
        let wire = serde_json::to_string(&original).unwrap();
        let back: CheckpointStep = serde_json::from_str(&wire).unwrap();
        match back {
            CheckpointStep::BeforeActions { actions } => {
                assert_eq!(actions, sample_actions());
            }
            other => panic!("expected BeforeActions, got {other:?}"),
        }
    }

    #[test]
    fn after_actions_variant_keeps_actions_and_observation_through_serde() {
        let original = CheckpointStep::AfterActions {
            actions: sample_actions(),
            observation: Some(sample_observation()),
        };
        let wire = serde_json::to_string(&original).unwrap();
        let back: CheckpointStep = serde_json::from_str(&wire).unwrap();
        match back {
            CheckpointStep::AfterActions {
                actions,
                observation,
            } => {
                assert_eq!(actions, sample_actions());
                let observation = observation.expect("observation must survive the round trip");
                assert_eq!(observation.outcomes.len(), 2);
                assert_eq!(observation.outcomes[0].action_id, "call_1");
                assert_eq!(observation.outcomes[0].name, "weather");
                assert_eq!(observation.outcomes[0].summary, "20C, clear");
                assert_eq!(observation.outcomes[1].action_id, "call_2");
                assert_eq!(observation.outcomes[1].name, "time");
                assert_eq!(observation.outcomes[1].summary, "09:41 JST");
                assert_eq!(observation.batch_status, BatchStatus::Completed);
                // ChatMessage has no PartialEq — canonical JSON comparison.
                assert_eq!(
                    serde_json::to_string(&observation.history_messages).unwrap(),
                    serde_json::to_string(&sample_observation().history_messages).unwrap()
                );
            }
            other => panic!("expected AfterActions, got {other:?}"),
        }
    }

    #[test]
    fn checkpoint_step_after_actions_observation_is_optional() {
        // A batch checkpoint written before the observation was available
        // still deserializes (T5 migration fills what it can).
        let step = CheckpointStep::AfterActions {
            actions: sample_actions(),
            observation: None,
        };
        let json = serde_json::to_string(&step).unwrap();
        assert!(!json.contains("observation"));
        let decoded: CheckpointStep = serde_json::from_str(&json).unwrap();
        match decoded {
            CheckpointStep::AfterActions { observation, .. } => {
                assert!(observation.is_none());
            }
            other => panic!("expected AfterActions, got {other:?}"),
        }
    }

    #[test]
    fn checkpoint_data_round_trips_for_every_step_variant() {
        let variants = [
            CheckpointStep::AfterUserInput,
            CheckpointStep::BeforeLlm {
                messages: sample_messages(),
                tools: sample_tools(),
            },
            CheckpointStep::BeforeActions {
                actions: sample_actions(),
            },
            CheckpointStep::AfterActions {
                actions: sample_actions(),
                observation: Some(sample_observation()),
            },
        ];

        for step in variants {
            let data = checkpoint_data(step);
            let json = serde_json::to_string(&data).unwrap();
            let decoded: CheckpointData = serde_json::from_str(&json).unwrap();
            assert_eq!(decoded.session_id, data.session_id);
            assert_eq!(decoded.user_input, data.user_input);
            assert_eq!(decoded.turn_count, data.turn_count);
            // CheckpointStep does not derive PartialEq, so compare variant
            // discriminants for the step round-trip.
            assert_eq!(
                std::mem::discriminant(&data.step),
                std::mem::discriminant(&decoded.step)
            );
        }
    }

    #[test]
    fn checkpoint_step_rejects_malformed_json_instead_of_panicking() {
        // Unknown variant tag.
        assert!(serde_json::from_str::<CheckpointStep>(r#"{"NotAStep":{}}"#).is_err());
        // Wrong field type on a known variant.
        assert!(
            serde_json::from_str::<CheckpointStep>(
                r#"{"BeforeLlm":{"messages":"not-a-list","tools":[]}}"#,
            )
            .is_err()
        );
        // Truncated JSON.
        assert!(serde_json::from_str::<CheckpointStep>(r#"{"AfterUserInput""#).is_err());
        // Empty input.
        assert!(serde_json::from_str::<CheckpointStep>("").is_err());
    }

    #[test]
    fn checkpoint_data_rejects_malformed_json_instead_of_panicking() {
        // Missing required fields.
        assert!(
            serde_json::from_str::<CheckpointData>(r#"{"session_id":{"id":1},"user_input":"hi"}"#,)
                .is_err()
        );
        // Wrong session_id shape.
        assert!(
            serde_json::from_str::<CheckpointData>(
                r#"{"session_id":"oops","user_input":"hi","step":"AfterUserInput","turn_count":1}"#,
            )
            .is_err()
        );
        // turn_count is not a number.
        assert!(
            serde_json::from_str::<CheckpointData>(
                r#"{"session_id":{"id":1},"user_input":"hi","step":"AfterUserInput","turn_count":"three"}"#,
            )
            .is_err()
        );
    }

    // -----------------------------------------------------------------------
    // T5 persistence contract: versioning, source marks, backend affinity.
    // -----------------------------------------------------------------------

    #[test]
    fn native_checkpoint_pins_version_and_backend() {
        let data = checkpoint_data(CheckpointStep::AfterUserInput);
        assert_eq!(data.version, CHECKPOINT_VERSION);
        assert_eq!(data.backend, ActionFormat::ToolCall);
        assert!(data.migrated_from.is_none());

        let json = serde_json::to_string(&data).unwrap();
        assert!(
            json.contains("\"version\":1"),
            "native write carries version: {json}"
        );
        assert!(
            json.contains("\"backend\":\"tool_call\""),
            "native write carries the format label: {json}"
        );
        let decoded: CheckpointData = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.version, CHECKPOINT_VERSION);
        assert_eq!(decoded.backend, ActionFormat::ToolCall);
        assert!(decoded.migrated_from.is_none());
    }

    #[test]
    fn pre_versioning_payload_reads_as_version_zero_without_a_mark() {
        // A pre-M3 `AfterUserInput`/`BeforeLlm` payload is shape-identical to
        // the native one — nothing migrated, so nothing is marked (不猜).
        let json = r#"{"session_id":{"id":1,"external_id":"s1"},"user_input":"hi","step":"AfterUserInput","turn_count":1}"#;
        let decoded: CheckpointData = serde_json::from_str(json).unwrap();
        assert_eq!(decoded.version, 0, "absent version must not be backfilled");
        assert_eq!(decoded.backend, ActionFormat::ToolCall);
        assert!(decoded.migrated_from.is_none());
    }

    #[test]
    fn legacy_before_tool_calls_migrates_with_source_mark() {
        // A checkpoint line written by a pre-M3 binary: tool-call triples, no
        // version/backend fields.
        let json = r#"{"session_id":{"id":7,"external_id":"s7"},"user_input":"resume me","step":{"BeforeToolCalls":{"tool_calls":[["call_1","echo","{\"text\":\"hi\"}"]]}},"turn_count":2}"#;
        let decoded: CheckpointData = serde_json::from_str(json).unwrap();
        assert_eq!(decoded.version, 0);
        assert_eq!(decoded.backend, ActionFormat::ToolCall);
        let mark = decoded
            .migrated_from
            .as_ref()
            .expect("legacy shape must carry its source mark");
        assert_eq!(mark.kind, MigrationKind::ToolCallTriplesV0);
        assert!(mark.legacy_results.is_empty());
        match &decoded.step {
            CheckpointStep::BeforeActions { actions } => {
                assert_eq!(actions.len(), 1);
                assert_eq!(actions[0].id, "call_1");
                assert_eq!(actions[0].name, "echo");
                assert_eq!(actions[0].raw_args(), r#"{"text":"hi"}"#);
                assert_eq!(actions[0].format, ActionFormat::ToolCall);
                assert_eq!(
                    actions[0].identity,
                    Action::tool_call_identity("echo", r#"{"text":"hi"}"#)
                );
            }
            other => panic!("expected BeforeActions, got {other:?}"),
        }
    }

    #[test]
    fn legacy_after_tool_calls_migrates_without_backfilling_an_observation() {
        let json = r#"{"session_id":{"id":8,"external_id":"s8"},"user_input":"go","step":{"AfterToolCalls":{"tool_calls":[["call_2","ls","{}"]],"results":[]}},"turn_count":3}"#;
        let decoded: CheckpointData = serde_json::from_str(json).unwrap();
        let mark = decoded
            .migrated_from
            .as_ref()
            .expect("legacy shape must carry its source mark");
        assert_eq!(mark.kind, MigrationKind::ToolCallTriplesV0);
        assert!(mark.legacy_results.is_empty());
        match &decoded.step {
            CheckpointStep::AfterActions {
                actions,
                observation,
            } => {
                assert_eq!(actions.len(), 1);
                assert_eq!(actions[0].id, "call_2");
                assert!(
                    observation.is_none(),
                    "nothing observed was recorded — no backfill"
                );
            }
            other => panic!("expected AfterActions, got {other:?}"),
        }
    }

    #[test]
    fn legacy_after_tool_calls_results_ride_along_verbatim() {
        let json = r#"{"session_id":{"id":8,"external_id":"s8"},"user_input":"go","step":{"AfterToolCalls":{"tool_calls":[["call_2","ls","{}"]],"results":[{"tool_call_id":"call_2","tool_name":"ls","summary":"file list"}]}},"turn_count":3}"#;
        let decoded: CheckpointData = serde_json::from_str(json).unwrap();
        let mark = decoded
            .migrated_from
            .as_ref()
            .expect("legacy shape must carry its source mark");
        assert_eq!(
            mark.legacy_results,
            vec![ToolResultData {
                tool_call_id: "call_2".to_string(),
                tool_name: "ls".to_string(),
                summary: "file list".to_string(),
            }],
            "legacy results are preserved as-is, not guessed into fates"
        );
        match &decoded.step {
            CheckpointStep::AfterActions { observation, .. } => {
                assert!(
                    observation.is_none(),
                    "results ≠ observation (no fates recorded)"
                );
            }
            other => panic!("expected AfterActions, got {other:?}"),
        }
    }

    #[test]
    fn legacy_payload_unknown_fields_are_tolerated() {
        let json = r#"{"session_id":{"id":9,"external_id":"s9"},"user_input":"hi","step":{"BeforeToolCalls":{"tool_calls":[["call_1","echo","{}"]],"future_field":true}},"turn_count":1,"some_new_top_level_field":"ignored"}"#;
        let decoded: CheckpointData = serde_json::from_str(json).unwrap();
        assert_eq!(decoded.version, 0);
        assert!(decoded.migrated_from.is_some());
        assert!(matches!(decoded.step, CheckpointStep::BeforeActions { .. }));
    }

    #[test]
    fn migrated_checkpoint_re_serializes_in_the_native_shape_and_keeps_its_mark() {
        let json = r#"{"session_id":{"id":8,"external_id":"s8"},"user_input":"go","step":{"AfterToolCalls":{"tool_calls":[["call_2","ls","{}"]],"results":[{"tool_call_id":"call_2","tool_name":"ls","summary":"file list"}]}},"turn_count":3}"#;
        let decoded: CheckpointData = serde_json::from_str(json).unwrap();
        let rewritten = serde_json::to_string(&decoded).unwrap();
        assert!(
            rewritten.contains("\"AfterActions\""),
            "rewrite is native-shape: {rewritten}"
        );
        assert!(!rewritten.contains("AfterToolCalls"));
        assert!(
            rewritten.contains("\"migrated_from\""),
            "the source mark survives persistence: {rewritten}"
        );
        assert!(rewritten.contains("\"legacy_results\""));

        let again: CheckpointData = serde_json::from_str(&rewritten).unwrap();
        assert_eq!(
            again.version, 0,
            "the mark and version keep agreeing across rounds"
        );
        let mark = again.migrated_from.expect("mark survives the round trip");
        assert_eq!(mark.kind, MigrationKind::ToolCallTriplesV0);
        assert_eq!(mark.legacy_results.len(), 1);
        assert_eq!(mark.legacy_results[0].summary, "file list");
    }

    #[test]
    fn future_version_payload_is_refused_explicitly() {
        let json = r#"{"session_id":{"id":1,"external_id":"s1"},"user_input":"hi","step":"AfterUserInput","turn_count":1,"version":2}"#;
        let err = serde_json::from_str::<CheckpointData>(json).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("version 2"),
            "refusal names the version: {msg}"
        );
        assert!(msg.contains("refusing"), "refusal is explicit: {msg}");
    }

    #[test]
    fn unrecognized_step_payload_is_refused_explicitly() {
        let json = r#"{"session_id":{"id":1,"external_id":"s1"},"user_input":"hi","step":{"NotAStep":{}},"turn_count":1}"#;
        let err = serde_json::from_str::<CheckpointData>(json).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("NotAStep"), "refusal names the payload: {msg}");
        assert!(msg.contains("unrecognized"), "refusal is explicit: {msg}");
    }

    #[test]
    fn verify_for_resume_accepts_a_native_tool_call_checkpoint() {
        let data = checkpoint_data(CheckpointStep::BeforeActions {
            actions: sample_actions(),
        });
        assert!(data.verify_for_resume(ActionFormat::ToolCall).is_ok());
    }

    #[test]
    fn verify_for_resume_refuses_cross_backend_pinning() {
        let data = CheckpointData::new(
            SessionId::with_external_id(1, "s1"),
            "go",
            CheckpointStep::AfterUserInput,
            0,
            ActionFormat::CodeAct,
            RunState::default(),
        );
        let msg = data.verify_for_resume(ActionFormat::ToolCall).unwrap_err();
        assert!(
            msg.contains("CodeAct"),
            "refusal names the pinned backend: {msg}"
        );
        assert!(
            msg.contains("ToolCall"),
            "refusal names the resuming backend: {msg}"
        );
        assert!(msg.contains("refused"), "refusal is explicit: {msg}");
    }

    #[test]
    fn verify_for_resume_refuses_mismatched_format_labels() {
        // Hand-built contradiction: the pin says tool_call, the payload says
        // code_act. The label is the evidence — no guessing.
        let data = CheckpointData::new(
            SessionId::with_external_id(1, "s1"),
            "go",
            CheckpointStep::BeforeActions {
                actions: vec![Action {
                    id: "cell_1".to_string(),
                    name: "code_cell".to_string(),
                    args: Value::String(r#"{"code":"1+1"}"#.to_string()),
                    identity: r#"code_cell::{"code":"1+1"}"#.to_string(),
                    format: ActionFormat::CodeAct,
                }],
            },
            0,
            ActionFormat::ToolCall,
            RunState::default(),
        );
        let msg = data.verify_for_resume(ActionFormat::ToolCall).unwrap_err();
        assert!(msg.contains("cell_1"), "refusal names the action: {msg}");
        assert!(
            msg.contains("format label"),
            "refusal explains the label mismatch: {msg}"
        );
    }

    #[test]
    fn verify_for_resume_refuses_a_future_version_value() {
        // Hand-built value — the read-side check is the real gate; this covers
        // callers that assemble CheckpointData in memory.
        let mut data = checkpoint_data(CheckpointStep::AfterUserInput);
        data.version = CHECKPOINT_VERSION + 1;
        let msg = data.verify_for_resume(ActionFormat::ToolCall).unwrap_err();
        assert!(msg.contains("newer"), "refusal explains the version: {msg}");
    }

    #[test]
    fn native_checkpoint_co_writes_run_state() {
        // D5+OV-11 事务同写: a native write always carries the counting state
        // alongside the step in one payload.
        let mut run_state = RunState::default();
        run_state.record_actions(2, &[]);
        let data = CheckpointData::new(
            SessionId::with_external_id(42, "test-session"),
            "go",
            CheckpointStep::BeforeActions {
                actions: sample_actions(),
            },
            3,
            ActionFormat::ToolCall,
            run_state,
        );
        let carried = data
            .run_state
            .clone()
            .expect("native write co-writes RunState");
        assert_eq!(carried.turn_actions, 2);
        assert_eq!(carried.total_actions, 2);

        let json = serde_json::to_string(&data).unwrap();
        assert!(
            json.contains("\"run_state\""),
            "wire carries the state: {json}"
        );
        let decoded: CheckpointData = serde_json::from_str(&json).unwrap();
        let restored = decoded.run_state.expect("state survives the round trip");
        assert_eq!(restored.turn_actions, 2);
        assert_eq!(restored.total_actions, 2);
        assert!(restored.run_has_actions);
    }

    #[test]
    fn pre_t7_checkpoint_reads_without_run_state() {
        // The pre-T7 corpus has no counting state on the wire. It must read
        // with the field genuinely absent — never guessed, never backfilled
        // (不猜不回填) — so restores fall back to the old-format paths.
        let json = r#"{"session_id":{"id":42,"external_id":"test-session"},"user_input":"go","step":{"BeforeActions":{"actions":[]}},"turn_count":3,"version":1,"backend":"tool_call"}"#;
        let decoded: CheckpointData = serde_json::from_str(json).unwrap();
        assert!(
            decoded.run_state.is_none(),
            "absent stays absent: {:?}",
            decoded.run_state
        );
    }

    #[test]
    fn converted_legacy_checkpoint_keeps_run_state_absent() {
        // A migrated pre-M3 payload gains the step migration and source mark,
        // but not a counting state — conversion must not invent one.
        let json = r#"{"session_id":{"id":1,"external_id":"s1"},"user_input":"go","step":{"BeforeToolCalls":{"tool_calls":[["call_1","weather","{\"city\":\"Tokyo\"}"]]}},"turn_count":1}"#;
        let decoded: CheckpointData = serde_json::from_str(json).unwrap();
        assert!(decoded.migrated_from.is_some(), "step migration is marked");
        assert!(
            decoded.run_state.is_none(),
            "conversion does not invent counting state: {:?}",
            decoded.run_state
        );
    }
}