starweaver-session 0.10.0

Durable session contracts for Starweaver
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
//! Host-neutral continuation preparation over durable session evidence.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use serde_json::Value;
use starweaver_context::AgentRunState;
use starweaver_model::TOOL_RETURN_APPROVAL_ARGUMENTS_METADATA_KEY;
use thiserror::Error;

use crate::{ApprovalStatus, ExecutionStatus, SessionResumeSnapshot};

/// Continuation preparation mode selected by the host.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ContinuationPreparationMode {
    /// Restore durable context without consuming HITL decisions.
    Ordinary,
    /// Validate a waiting run and prepare its terminal approval/deferred decisions.
    WaitingHitl,
}

/// Side-effect-free continuation package shared by every product host.
///
/// This package deliberately contains durable evidence only. Approved tools are executed later by
/// the agent layer, after the host has atomically admitted a waiting-run replacement and moved its
/// claim to `Started`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct PreparedContinuation {
    /// Preparation mode used to validate the evidence.
    pub mode: ContinuationPreparationMode,
    /// Canonical durable snapshot. Waiting checkpoint state is normalized to `Waiting` in-memory.
    pub snapshot: SessionResumeSnapshot,
}

impl PreparedContinuation {
    /// Prepare an ordinary context continuation.
    ///
    /// # Errors
    ///
    /// Returns an error when the snapshot contains inconsistent session/run identities.
    pub fn ordinary(snapshot: SessionResumeSnapshot) -> Result<Self, ContinuationPreparationError> {
        validate_snapshot_identity(&snapshot)?;
        Ok(Self {
            mode: ContinuationPreparationMode::Ordinary,
            snapshot,
        })
    }

    /// Prepare a waiting HITL continuation without executing hooks or tools.
    ///
    /// # Errors
    ///
    /// Returns an error when the source is not waiting, has no checkpoint, or its pending HITL
    /// items do not have one matching terminal durable decision/result.
    pub fn waiting_hitl(
        mut snapshot: SessionResumeSnapshot,
    ) -> Result<Self, ContinuationPreparationError> {
        validate_snapshot_identity(&snapshot)?;
        if snapshot.run.status != crate::RunStatus::Waiting {
            return Err(ContinuationPreparationError::SourceNotWaiting(
                snapshot.run.run_id.as_str().to_string(),
            ));
        }
        let checkpoint_state = {
            let checkpoint = snapshot.latest_checkpoint.as_mut().ok_or_else(|| {
                ContinuationPreparationError::MissingCheckpoint(
                    snapshot.run.run_id.as_str().to_string(),
                )
            })?;
            // Checkpoints may precede the terminal Waiting projection. The durable run record is
            // authoritative; normalization is process-local and never rewrites immutable evidence.
            checkpoint.state.status = starweaver_core::RunLifecycle::Waiting;
            checkpoint.state.clone()
        };
        validate_waiting_evidence(&snapshot, &checkpoint_state)?;
        Ok(Self {
            mode: ContinuationPreparationMode::WaitingHitl,
            snapshot,
        })
    }

    /// Return the normalized checkpoint state for a waiting HITL continuation.
    #[must_use]
    pub fn waiting_state(&self) -> Option<&AgentRunState> {
        matches!(self.mode, ContinuationPreparationMode::WaitingHitl)
            .then(|| {
                self.snapshot
                    .latest_checkpoint
                    .as_ref()
                    .map(|checkpoint| &checkpoint.state)
            })
            .flatten()
    }

    /// Consume the package and return its canonical snapshot.
    #[must_use]
    pub fn into_snapshot(self) -> SessionResumeSnapshot {
        self.snapshot
    }
}

/// Stable continuation preparation failures shared by CLI, RPC, and future hosts.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
pub enum ContinuationPreparationError {
    /// Snapshot session and run ownership disagree.
    #[error("continuation snapshot identity mismatch: {0}")]
    IdentityMismatch(String),
    /// Waiting preparation was requested for another source status.
    #[error("run {0} is not waiting")]
    SourceNotWaiting(String),
    /// A waiting source has no resumable checkpoint.
    #[error("waiting run {0} has no resumable checkpoint")]
    MissingCheckpoint(String),
    /// The checkpoint has no pending HITL work.
    #[error("waiting run {0} has no pending HITL work")]
    NoPendingHitl(String),
    /// Durable decision evidence contains duplicate identities.
    #[error("duplicate durable {kind} decision for {id}")]
    DuplicateDecision {
        /// Durable decision family (`approval` or `deferred`).
        kind: &'static str,
        /// Duplicate durable identity.
        id: String,
    },
    /// One pending item has no terminal durable decision.
    #[error("missing terminal durable {kind} decision for {id}")]
    MissingDecision {
        /// Durable decision family (`approval` or `deferred`).
        kind: &'static str,
        /// Pending identity without a terminal decision.
        id: String,
    },
    /// Durable evidence references an item that is not pending in the checkpoint.
    #[error("unknown durable {kind} decision for {id}")]
    UnknownDecision {
        /// Durable decision family (`approval` or `deferred`).
        kind: &'static str,
        /// Durable identity absent from the waiting checkpoint.
        id: String,
    },
    /// Durable evidence uses the expected durable id but references another tool call.
    #[error("durable {kind} decision {id} references {actual}; expected {expected}")]
    MismatchedDecisionIdentity {
        /// Durable decision family (`approval` or `deferred`).
        kind: &'static str,
        /// Durable decision identity.
        id: String,
        /// Referenced tool call identity.
        actual: String,
        /// Expected tool call identity.
        expected: String,
    },
    /// Durable decision evidence does not match the pending checkpoint evidence.
    #[error("durable {kind} decision {id} has mismatched {field}")]
    MismatchedDecisionEvidence {
        /// Durable decision family (`approval` or `deferred`).
        kind: &'static str,
        /// Durable decision identity.
        id: String,
        /// Evidence field that did not match.
        field: &'static str,
    },
    /// A durable approval status and decision record disagree.
    #[error("inconsistent durable approval decision for {0}")]
    InconsistentApproval(String),
}

fn validate_snapshot_identity(
    snapshot: &SessionResumeSnapshot,
) -> Result<(), ContinuationPreparationError> {
    if snapshot.session.session_id != snapshot.run.session_id {
        return Err(ContinuationPreparationError::IdentityMismatch(format!(
            "session {} does not own run {}",
            snapshot.session.session_id.as_str(),
            snapshot.run.run_id.as_str()
        )));
    }
    if snapshot
        .state
        .run_id
        .as_ref()
        .is_some_and(|run_id| run_id != &snapshot.run.run_id)
    {
        return Err(ContinuationPreparationError::IdentityMismatch(format!(
            "context run does not match {}",
            snapshot.run.run_id.as_str()
        )));
    }
    if snapshot
        .state
        .conversation_id
        .as_ref()
        .is_some_and(|conversation_id| conversation_id != &snapshot.run.conversation_id)
    {
        return Err(ContinuationPreparationError::IdentityMismatch(format!(
            "context conversation does not match {}",
            snapshot.run.conversation_id.as_str()
        )));
    }
    validate_optional_durable_id(
        &snapshot.state.metadata,
        "starweaver.durable_session_id",
        snapshot.session.session_id.as_str(),
    )?;
    validate_optional_durable_id(
        &snapshot.state.metadata,
        "starweaver.durable_run_id",
        snapshot.run.run_id.as_str(),
    )?;
    if let Some(checkpoint) = &snapshot.latest_checkpoint {
        if checkpoint.run_id != snapshot.run.run_id {
            return Err(ContinuationPreparationError::IdentityMismatch(format!(
                "checkpoint run does not match {}",
                snapshot.run.run_id.as_str()
            )));
        }
        if checkpoint.state.run_id != snapshot.run.run_id {
            return Err(ContinuationPreparationError::IdentityMismatch(format!(
                "checkpoint state run does not match {}",
                snapshot.run.run_id.as_str()
            )));
        }
        if checkpoint.conversation_id != snapshot.run.conversation_id {
            return Err(ContinuationPreparationError::IdentityMismatch(format!(
                "checkpoint conversation does not match {}",
                snapshot.run.conversation_id.as_str()
            )));
        }
        if checkpoint.state.conversation_id != snapshot.run.conversation_id {
            return Err(ContinuationPreparationError::IdentityMismatch(format!(
                "checkpoint state conversation does not match {}",
                snapshot.run.conversation_id.as_str()
            )));
        }
        if checkpoint.node != checkpoint.resume.node
            || checkpoint.run_step != checkpoint.resume.run_step
            || checkpoint.run_step != checkpoint.state.run_step
        {
            return Err(ContinuationPreparationError::IdentityMismatch(
                "checkpoint boundary metadata does not match checkpoint state".to_string(),
            ));
        }
    }
    Ok(())
}

fn validate_optional_durable_id(
    metadata: &serde_json::Map<String, Value>,
    key: &'static str,
    expected: &str,
) -> Result<(), ContinuationPreparationError> {
    if metadata
        .get(key)
        .is_some_and(|value| value.as_str() != Some(expected))
    {
        return Err(ContinuationPreparationError::IdentityMismatch(format!(
            "context metadata {key} does not match {expected}"
        )));
    }
    Ok(())
}

fn validate_waiting_evidence(
    snapshot: &SessionResumeSnapshot,
    state: &AgentRunState,
) -> Result<(), ContinuationPreparationError> {
    if !state.has_pending_hitl() {
        return Err(ContinuationPreparationError::NoPendingHitl(
            snapshot.run.run_id.as_str().to_string(),
        ));
    }
    validate_approval_evidence(snapshot, state)?;

    let pending_deferred = state
        .deferred_tool_returns
        .iter()
        .map(|item| {
            (
                format!("deferred_{}_{}", state.run_id.as_str(), item.tool_call_id),
                item.tool_call_id.clone(),
            )
        })
        .collect::<BTreeMap<_, _>>();
    let mut deferred = BTreeMap::new();
    for record in &snapshot.deferred_tools {
        if record.session_id != snapshot.run.session_id || record.run_id != snapshot.run.run_id {
            return Err(ContinuationPreparationError::IdentityMismatch(format!(
                "deferred record {} does not belong to the source run",
                record.deferred_id
            )));
        }
        if deferred
            .insert(record.deferred_id.clone(), record)
            .is_some()
        {
            return Err(ContinuationPreparationError::DuplicateDecision {
                kind: "deferred",
                id: record.deferred_id.clone(),
            });
        }
    }
    validate_deferred_set(&pending_deferred, &deferred)
}

fn validate_approval_evidence(
    snapshot: &SessionResumeSnapshot,
    state: &AgentRunState,
) -> Result<(), ContinuationPreparationError> {
    let pending_tool_calls = state
        .latest_response
        .iter()
        .flat_map(starweaver_model::ModelResponse::tool_calls)
        .chain(state.pending_tool_calls.iter().cloned())
        .map(|call| (call.id.clone(), call))
        .collect::<BTreeMap<_, _>>();
    let mut pending_approvals = BTreeMap::new();
    for item in &state.pending_approval_tool_returns {
        let action_id = item.tool_call_id.clone();
        let approval_id = format!("approval_{}_{}", state.run_id.as_str(), action_id);
        let tool_call = pending_tool_calls.get(&action_id).ok_or_else(|| {
            ContinuationPreparationError::IdentityMismatch(format!(
                "pending approval {approval_id} has no matching checkpoint tool call"
            ))
        })?;
        if tool_call.name != item.name {
            return Err(ContinuationPreparationError::MismatchedDecisionEvidence {
                kind: "approval",
                id: approval_id,
                field: "checkpoint tool name",
            });
        }
        let request = item
            .metadata
            .get("approval")
            .cloned()
            .unwrap_or(Value::Null);
        let reviewed_arguments = item
            .metadata
            .get(TOOL_RETURN_APPROVAL_ARGUMENTS_METADATA_KEY)
            .ok_or_else(
                || ContinuationPreparationError::MismatchedDecisionEvidence {
                    kind: "approval",
                    id: approval_id.clone(),
                    field: "reviewed tool arguments",
                },
            )?;
        if reviewed_arguments != &tool_call.arguments.execution_value() {
            return Err(ContinuationPreparationError::MismatchedDecisionEvidence {
                kind: "approval",
                id: approval_id,
                field: "checkpoint tool arguments",
            });
        }
        if pending_approvals
            .insert(
                action_id.clone(),
                PendingApprovalEvidence {
                    approval_id,
                    action_name: item.name.clone(),
                    request,
                    reviewed_arguments: reviewed_arguments.clone(),
                },
            )
            .is_some()
        {
            return Err(ContinuationPreparationError::DuplicateDecision {
                kind: "pending approval",
                id: action_id,
            });
        }
    }
    let mut approvals = BTreeMap::new();
    for record in &snapshot.approvals {
        if record.session_id != snapshot.run.session_id || record.run_id != snapshot.run.run_id {
            return Err(ContinuationPreparationError::IdentityMismatch(format!(
                "approval {} does not belong to the source run",
                record.approval_id
            )));
        }
        let Some(expected) = pending_approvals.get(&record.action_id) else {
            return Err(ContinuationPreparationError::UnknownDecision {
                kind: "approval",
                id: record.action_id.clone(),
            });
        };
        if record.approval_id != expected.approval_id {
            return Err(ContinuationPreparationError::MismatchedDecisionEvidence {
                kind: "approval",
                id: record.action_id.clone(),
                field: "approval id",
            });
        }
        if approvals.insert(record.action_id.clone(), record).is_some() {
            return Err(ContinuationPreparationError::DuplicateDecision {
                kind: "approval",
                id: record.action_id.clone(),
            });
        }
    }
    validate_approval_set(&pending_approvals, &approvals)
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct PendingApprovalEvidence {
    approval_id: String,
    action_name: String,
    request: Value,
    reviewed_arguments: Value,
}

fn validate_approval_set(
    pending: &BTreeMap<String, PendingApprovalEvidence>,
    records: &BTreeMap<String, &crate::ApprovalRecord>,
) -> Result<(), ContinuationPreparationError> {
    for (id, expected) in pending {
        let record =
            records
                .get(id)
                .ok_or_else(|| ContinuationPreparationError::MissingDecision {
                    kind: "approval",
                    id: id.clone(),
                })?;
        if record.action_name != expected.action_name {
            return Err(ContinuationPreparationError::MismatchedDecisionEvidence {
                kind: "approval",
                id: record.approval_id.clone(),
                field: "action name",
            });
        }
        if record.request != expected.request {
            return Err(ContinuationPreparationError::MismatchedDecisionEvidence {
                kind: "approval",
                id: record.approval_id.clone(),
                field: "request",
            });
        }
        if record.reviewed_arguments.as_ref() != Some(&expected.reviewed_arguments) {
            return Err(ContinuationPreparationError::MismatchedDecisionEvidence {
                kind: "approval",
                id: record.approval_id.clone(),
                field: "durable reviewed tool arguments",
            });
        }
        if record.status == ApprovalStatus::Pending {
            return Err(ContinuationPreparationError::MissingDecision {
                kind: "approval",
                id: id.clone(),
            });
        }
        let Some(decision) = record.decision.as_ref() else {
            return Err(ContinuationPreparationError::MissingDecision {
                kind: "approval",
                id: id.clone(),
            });
        };
        let consistent = decision.status == record.status
            || (matches!(
                record.status,
                ApprovalStatus::Expired | ApprovalStatus::Cancelled
            ) && matches!(decision.status, ApprovalStatus::Denied));
        if !consistent {
            return Err(ContinuationPreparationError::InconsistentApproval(
                id.clone(),
            ));
        }
    }
    if let Some(id) = records.keys().find(|id| !pending.contains_key(*id)) {
        return Err(ContinuationPreparationError::UnknownDecision {
            kind: "approval",
            id: id.clone(),
        });
    }
    Ok(())
}

fn validate_deferred_set(
    pending: &BTreeMap<String, String>,
    records: &BTreeMap<String, &crate::DeferredToolRecord>,
) -> Result<(), ContinuationPreparationError> {
    for (id, expected_tool_call_id) in pending {
        let record =
            records
                .get(id)
                .ok_or_else(|| ContinuationPreparationError::MissingDecision {
                    kind: "deferred",
                    id: id.clone(),
                })?;
        if record.tool_call_id != *expected_tool_call_id {
            return Err(ContinuationPreparationError::MismatchedDecisionIdentity {
                kind: "deferred",
                id: id.clone(),
                actual: record.tool_call_id.clone(),
                expected: expected_tool_call_id.clone(),
            });
        }
        if matches!(
            record.status,
            ExecutionStatus::Pending | ExecutionStatus::Running | ExecutionStatus::Waiting
        ) {
            return Err(ContinuationPreparationError::MissingDecision {
                kind: "deferred",
                id: id.clone(),
            });
        }
    }
    if let Some(id) = records.keys().find(|id| !pending.contains_key(*id)) {
        return Err(ContinuationPreparationError::UnknownDecision {
            kind: "deferred",
            id: id.clone(),
        });
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use serde_json::json;
    use starweaver_context::{AgentCheckpoint, AgentRunState, ResumableState};
    use starweaver_core::{AgentExecutionNode, ConversationId, RunId, SessionId};
    use starweaver_model::{
        TOOL_RETURN_APPROVAL_ARGUMENTS_METADATA_KEY, ToolArguments, ToolCallPart, ToolReturnPart,
    };

    use super::{ContinuationPreparationError, PreparedContinuation, validate_deferred_set};
    use crate::{
        ApprovalRecord, ApprovalStatus, DeferredToolRecord, ExecutionStatus, RunRecord, RunStatus,
        SessionRecord, SessionResumeSnapshot, ToolApprovalDecision, ToolReturnRecordInput,
    };

    fn waiting_approval_snapshot() -> SessionResumeSnapshot {
        let session_id = SessionId::from_string("session_waiting");
        let run_id = RunId::from_string("run_waiting");
        let conversation_id = ConversationId::from_string("conversation_waiting");
        let arguments = json!({"command": "rm -rf target/tmp"});
        let approval_request = json!({
            "reviewer": "shell_command_reviewer",
            "reason": "destructive command needs review",
            "risk_level": "high",
            "command": "rm -rf target/tmp",
        });
        let mut metadata = serde_json::Map::new();
        metadata.insert("control_flow".to_string(), json!("approval_required"));
        metadata.insert("approval".to_string(), approval_request);
        metadata.insert(
            TOOL_RETURN_APPROVAL_ARGUMENTS_METADATA_KEY.to_string(),
            arguments.clone(),
        );
        let pending_return =
            ToolReturnPart::new("call_approval", "shell", json!("approval required"))
                .with_error(true)
                .with_metadata(metadata);
        let mut run_state = AgentRunState::new(run_id.clone(), conversation_id.clone());
        run_state.pending_tool_calls.push(ToolCallPart {
            id: "call_approval".to_string(),
            name: "shell".to_string(),
            arguments: ToolArguments::parsed(arguments),
        });
        run_state
            .pending_approval_tool_returns
            .push(pending_return.clone());
        let checkpoint = AgentCheckpoint::new(AgentExecutionNode::ToolReturn, &run_state);

        let input = ToolReturnRecordInput::new(
            &session_id,
            &run_id,
            &pending_return.tool_call_id,
            &pending_return.name,
            &pending_return.metadata,
        );
        let Some(mut approval) = ApprovalRecord::from_tool_return(&input) else {
            panic!("approval-required tool return must produce durable evidence");
        };
        approval.status = ApprovalStatus::Approved;
        approval.decision = Some(ToolApprovalDecision::approved().into_approval_decision());

        let mut state = ResumableState {
            run_id: Some(run_id.clone()),
            // Provider-routing affinity deliberately differs from durable session identity.
            session_id: Some(SessionId::from_string("provider_routing_affinity")),
            conversation_id: Some(conversation_id.clone()),
            ..ResumableState::default()
        };
        state.metadata.insert(
            "starweaver.durable_session_id".to_string(),
            json!(session_id.as_str()),
        );
        state.metadata.insert(
            "starweaver.durable_run_id".to_string(),
            json!(run_id.as_str()),
        );
        let mut run = RunRecord::new(session_id.clone(), run_id, conversation_id);
        run.status = RunStatus::Waiting;
        SessionResumeSnapshot {
            session: SessionRecord::new(session_id),
            run,
            state,
            environment_state: None,
            latest_checkpoint: Some(checkpoint),
            stream_records: Vec::new(),
            approvals: vec![approval],
            deferred_tools: Vec::new(),
            stream_cursors: Vec::new(),
        }
    }

    #[test]
    fn waiting_approval_binds_durable_identity_name_request_and_tool_arguments() {
        let snapshot = waiting_approval_snapshot();
        assert!(PreparedContinuation::waiting_hitl(snapshot.clone()).is_ok());

        let mut missing_arguments_binding = snapshot.clone();
        let Some(checkpoint) = missing_arguments_binding.latest_checkpoint.as_mut() else {
            panic!("test snapshot must include a checkpoint");
        };
        checkpoint.state.pending_approval_tool_returns[0]
            .metadata
            .remove(TOOL_RETURN_APPROVAL_ARGUMENTS_METADATA_KEY);
        assert!(matches!(
            PreparedContinuation::waiting_hitl(missing_arguments_binding),
            Err(ContinuationPreparationError::MismatchedDecisionEvidence {
                field: "reviewed tool arguments",
                ..
            })
        ));

        let mut jointly_tampered_arguments = snapshot.clone();
        let Some(checkpoint) = jointly_tampered_arguments.latest_checkpoint.as_mut() else {
            panic!("test snapshot must include a checkpoint");
        };
        let tampered = json!({"command": "other", "environment": {"TOKEN": "changed"}});
        checkpoint.state.pending_tool_calls[0].arguments = ToolArguments::parsed(tampered.clone());
        checkpoint.state.pending_approval_tool_returns[0]
            .metadata
            .insert(
                TOOL_RETURN_APPROVAL_ARGUMENTS_METADATA_KEY.to_string(),
                tampered,
            );
        assert!(matches!(
            PreparedContinuation::waiting_hitl(jointly_tampered_arguments),
            Err(ContinuationPreparationError::MismatchedDecisionEvidence {
                field: "durable reviewed tool arguments",
                ..
            })
        ));

        let mut wrong_id = snapshot.clone();
        wrong_id.approvals[0].approval_id = "approval_other".to_string();
        assert!(matches!(
            PreparedContinuation::waiting_hitl(wrong_id),
            Err(ContinuationPreparationError::MismatchedDecisionEvidence {
                field: "approval id",
                ..
            })
        ));

        let mut wrong_name = snapshot.clone();
        wrong_name.approvals[0].action_name = "other_tool".to_string();
        assert!(matches!(
            PreparedContinuation::waiting_hitl(wrong_name),
            Err(ContinuationPreparationError::MismatchedDecisionEvidence {
                field: "action name",
                ..
            })
        ));

        let mut wrong_request = snapshot.clone();
        wrong_request.approvals[0].request = json!({"arguments": {"command": "other"}});
        assert!(matches!(
            PreparedContinuation::waiting_hitl(wrong_request),
            Err(ContinuationPreparationError::MismatchedDecisionEvidence {
                field: "request",
                ..
            })
        ));

        let mut wrong_arguments = snapshot;
        let Some(checkpoint) = wrong_arguments.latest_checkpoint.as_mut() else {
            panic!("test snapshot must include a checkpoint");
        };
        checkpoint.state.pending_tool_calls[0].arguments =
            ToolArguments::parsed(json!({"command": "other"}));
        assert!(matches!(
            PreparedContinuation::waiting_hitl(wrong_arguments),
            Err(ContinuationPreparationError::MismatchedDecisionEvidence {
                field: "checkpoint tool arguments",
                ..
            })
        ));
    }

    fn assert_identity_rejected(snapshot: SessionResumeSnapshot) {
        assert!(matches!(
            PreparedContinuation::ordinary(snapshot.clone()),
            Err(ContinuationPreparationError::IdentityMismatch(_))
        ));
        assert!(matches!(
            PreparedContinuation::waiting_hitl(snapshot),
            Err(ContinuationPreparationError::IdentityMismatch(_))
        ));
    }

    #[test]
    fn continuation_rejects_context_and_checkpoint_identity_tampering() {
        let snapshot = waiting_approval_snapshot();

        let mut context_conversation = snapshot.clone();
        context_conversation.state.conversation_id =
            Some(ConversationId::from_string("conversation_other"));
        assert_identity_rejected(context_conversation);

        let mut durable_session = snapshot.clone();
        durable_session.state.metadata.insert(
            "starweaver.durable_session_id".to_string(),
            json!("session_other"),
        );
        assert_identity_rejected(durable_session);

        let mut checkpoint_run = snapshot.clone();
        let Some(checkpoint) = checkpoint_run.latest_checkpoint.as_mut() else {
            panic!("test snapshot must include a checkpoint");
        };
        checkpoint.run_id = RunId::from_string("run_other");
        assert_identity_rejected(checkpoint_run);

        let mut checkpoint_conversation = snapshot.clone();
        let Some(checkpoint) = checkpoint_conversation.latest_checkpoint.as_mut() else {
            panic!("test snapshot must include a checkpoint");
        };
        checkpoint.conversation_id = ConversationId::from_string("conversation_other");
        assert_identity_rejected(checkpoint_conversation);

        let mut checkpoint_state_conversation = snapshot;
        let Some(checkpoint) = checkpoint_state_conversation.latest_checkpoint.as_mut() else {
            panic!("test snapshot must include a checkpoint");
        };
        checkpoint.state.conversation_id = ConversationId::from_string("conversation_other");
        assert_identity_rejected(checkpoint_state_conversation);
    }

    #[test]
    fn deferred_decision_rejects_mismatched_tool_call_identity() {
        let deferred_id = "deferred_run_waiting_call_expected".to_string();
        let pending = BTreeMap::from([(deferred_id.clone(), "call_expected".to_string())]);
        let mut record = DeferredToolRecord::new(
            deferred_id.clone(),
            SessionId::from_string("session_waiting"),
            RunId::from_string("run_waiting"),
            "call_other",
            "shell",
        );
        record.status = ExecutionStatus::Completed;
        let records = BTreeMap::from([(deferred_id.clone(), &record)]);

        assert_eq!(
            validate_deferred_set(&pending, &records),
            Err(ContinuationPreparationError::MismatchedDecisionIdentity {
                kind: "deferred",
                id: deferred_id,
                actual: "call_other".to_string(),
                expected: "call_expected".to_string(),
            })
        );
    }

    #[test]
    fn deferred_decision_accepts_matching_terminal_identity() {
        let deferred_id = "deferred_run_waiting_call_expected".to_string();
        let pending = BTreeMap::from([(deferred_id.clone(), "call_expected".to_string())]);
        let mut record = DeferredToolRecord::new(
            deferred_id.clone(),
            SessionId::from_string("session_waiting"),
            RunId::from_string("run_waiting"),
            "call_expected",
            "shell",
        );
        record.status = ExecutionStatus::Completed;
        let records = BTreeMap::from([(deferred_id, &record)]);

        assert_eq!(validate_deferred_set(&pending, &records), Ok(()));
    }
}