a3s-box-runtime 3.1.0

MicroVM runtime engine — VM lifecycle, OCI images, attestation, networking
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
1019
1020
1021
1022
//! Durable generation-fenced transitions for managed local executions.

use std::path::{Path, PathBuf};

use a3s_box_core::{ExecutionGeneration, ExecutionId, OperationId};
use thiserror::Error;

use crate::{
    BoxRecord, BoxStateStore, ManagedExecutionOperation, ManagedExecutionState,
    ManagedRestartCompletion, ManagedRestartOutcome,
};

/// Strict durable repository used by the local `ExecutionManager`.
#[derive(Debug, Clone)]
pub struct ManagedExecutionStore {
    path: PathBuf,
}

/// Result of reserving an idempotent create operation.
#[derive(Debug, Clone)]
pub enum ManagedExecutionReservation {
    /// The creation intent was inserted by this call.
    Reserved(BoxRecord),
    /// The operation already existed with the same creation intent.
    Existing(BoxRecord),
}

impl ManagedExecutionReservation {
    pub const fn is_new(&self) -> bool {
        matches!(self, Self::Reserved(_))
    }

    pub fn record(&self) -> &BoxRecord {
        match self {
            Self::Reserved(record) | Self::Existing(record) => record,
        }
    }

    pub fn into_record(self) -> BoxRecord {
        match self {
            Self::Reserved(record) | Self::Existing(record) => record,
        }
    }
}

/// Fail-closed errors from managed lifecycle persistence.
#[derive(Debug, Error)]
pub enum ManagedExecutionStoreError {
    #[error("managed execution state I/O failed: {0}")]
    Io(#[from] std::io::Error),
    #[error("managed execution not found: {0}")]
    NotFound(ExecutionId),
    #[error("execution record is not managed: {0}")]
    Unmanaged(ExecutionId),
    #[error("managed execution conflict for {execution_id}: {message}")]
    Conflict {
        execution_id: ExecutionId,
        message: String,
    },
    #[error("invalid managed execution record: {0}")]
    InvalidRecord(String),
    #[error("invalid managed execution transition for {execution_id}: {from} -> {to}")]
    InvalidTransition {
        execution_id: ExecutionId,
        from: ManagedExecutionState,
        to: ManagedExecutionState,
    },
}

pub type ManagedExecutionStoreResult<T> = std::result::Result<T, ManagedExecutionStoreError>;

impl ManagedExecutionStore {
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self { path: path.into() }
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Return one managed record without mutating or reconciling state.
    pub fn get(
        &self,
        execution_id: &ExecutionId,
    ) -> ManagedExecutionStoreResult<Option<BoxRecord>> {
        let store = BoxStateStore::load_readonly(&self.path)?;
        let Some(record) = store.find_by_id(execution_id.as_str()).cloned() else {
            return Ok(None);
        };
        if record.managed_execution.is_none() {
            return Err(ManagedExecutionStoreError::Unmanaged(execution_id.clone()));
        }
        Ok(Some(record))
    }

    /// Return every managed record without reconciling provider state.
    ///
    /// Legacy CLI records share the same state file and are deliberately
    /// excluded. Loading remains strict: one malformed managed record fails the
    /// complete snapshot instead of letting provider discovery skip corrupt
    /// ownership metadata.
    pub fn list(&self) -> ManagedExecutionStoreResult<Vec<BoxRecord>> {
        let store = BoxStateStore::load_readonly(&self.path)?;
        Ok(store
            .records()
            .iter()
            .filter(|record| record.managed_execution.is_some())
            .cloned()
            .collect())
    }

    /// Return the record reserved by an idempotent creation operation.
    pub fn get_by_operation_id(
        &self,
        operation_id: &OperationId,
    ) -> ManagedExecutionStoreResult<Option<BoxRecord>> {
        let store = BoxStateStore::load_readonly(&self.path)?;
        Ok(store.find_by_operation_id(operation_id).cloned())
    }

    /// Atomically reserve one creation operation before backend side effects.
    ///
    /// Retrying the same operation with the same full request returns the
    /// existing record. Reusing an operation ID for different creation intent
    /// fails without changing durable state.
    pub fn reserve(
        &self,
        mut record: BoxRecord,
    ) -> ManagedExecutionStoreResult<ManagedExecutionReservation> {
        let execution_id = validate_new_record(&record)?;
        record.status = ManagedExecutionState::Created.as_status().to_string();
        let incoming_metadata = record
            .managed_execution
            .as_ref()
            .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(execution_id.clone()))?
            .clone();

        BoxStateStore::transact(&self.path, move |store| {
            if let Some(existing) = store
                .find_by_operation_id(&incoming_metadata.operation_id)
                .cloned()
            {
                let existing_id = ExecutionId::new(existing.id.clone()).map_err(|error| {
                    ManagedExecutionStoreError::InvalidRecord(error.to_string())
                })?;
                let existing_metadata = existing
                    .managed_execution
                    .as_ref()
                    .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(existing_id.clone()))?;
                if !same_creation_intent(existing_metadata, &incoming_metadata)? {
                    return Err(ManagedExecutionStoreError::Conflict {
                        execution_id: existing_id,
                        message: format!(
                            "operation {} was already reserved with different creation intent",
                            incoming_metadata.operation_id
                        ),
                    });
                }
                return Ok(ManagedExecutionReservation::Existing(existing));
            }

            if store.find_by_id(execution_id.as_str()).is_some() {
                return Err(ManagedExecutionStoreError::Conflict {
                    execution_id,
                    message: "execution ID is already present".to_string(),
                });
            }

            store.records_mut().push(record.clone());
            Ok(ManagedExecutionReservation::Reserved(record))
        })
    }

    /// Claim terminal-record removal before deleting host resources.
    ///
    /// The durable `removing` state prevents another lifecycle operation from
    /// reviving the execution while cleanup is in progress. A retry observes
    /// the same claim and can resume cleanup after a process crash.
    pub fn begin_remove(
        &self,
        execution_id: &ExecutionId,
        expected_generation: ExecutionGeneration,
    ) -> ManagedExecutionStoreResult<Option<BoxRecord>> {
        let execution_id = execution_id.clone();
        BoxStateStore::transact(&self.path, move |store| {
            let Some(record) = store.find_by_id_mut(execution_id.as_str()) else {
                return Ok(None);
            };
            let state = record
                .managed_state()
                .map_err(|error| ManagedExecutionStoreError::InvalidRecord(error.to_string()))?
                .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(execution_id.clone()))?;
            let metadata = record
                .managed_execution
                .as_mut()
                .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(execution_id.clone()))?;
            if metadata.generation != expected_generation {
                return Err(ManagedExecutionStoreError::Conflict {
                    execution_id: execution_id.clone(),
                    message: format!(
                        "expected generation {}, found {}",
                        expected_generation.get(),
                        metadata.generation.get()
                    ),
                });
            }
            match state {
                ManagedExecutionState::Removing => Ok(Some(record.clone())),
                ManagedExecutionState::Created
                | ManagedExecutionState::Stopped
                | ManagedExecutionState::Failed => {
                    record.status = ManagedExecutionState::Removing.as_status().to_string();
                    metadata.pending_operation = Some(ManagedExecutionOperation::Remove);
                    Ok(Some(record.clone()))
                }
                _ => Err(ManagedExecutionStoreError::Conflict {
                    execution_id: execution_id.clone(),
                    message: format!("cannot remove execution in state {state}"),
                }),
            }
        })
    }

    /// Forget a generation only after its durable removal claim has completed.
    pub fn finish_remove(
        &self,
        execution_id: &ExecutionId,
        expected_generation: ExecutionGeneration,
    ) -> ManagedExecutionStoreResult<bool> {
        let execution_id = execution_id.clone();
        BoxStateStore::transact(&self.path, move |store| {
            let Some(record) = store.find_by_id(execution_id.as_str()) else {
                return Ok(false);
            };
            let state = record
                .managed_state()
                .map_err(|error| ManagedExecutionStoreError::InvalidRecord(error.to_string()))?
                .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(execution_id.clone()))?;
            let metadata = record
                .managed_execution
                .as_ref()
                .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(execution_id.clone()))?;
            if metadata.generation != expected_generation
                || state != ManagedExecutionState::Removing
            {
                return Err(ManagedExecutionStoreError::Conflict {
                    execution_id: execution_id.clone(),
                    message: format!(
                        "expected removing generation {}, found {state} generation {}",
                        expected_generation.get(),
                        metadata.generation.get()
                    ),
                });
            }
            Ok(store.remove_by_id(execution_id.as_str()))
        })
    }

    /// Atomically compare generation and state, then persist one legal edge.
    ///
    /// Completing pause and resume, and advancing a restart from teardown to
    /// startup, increments the runtime generation exactly once. Other edges
    /// retain the current generation.
    pub fn transition(
        &self,
        execution_id: &ExecutionId,
        expected_generation: ExecutionGeneration,
        expected_state: ManagedExecutionState,
        next_state: ManagedExecutionState,
    ) -> ManagedExecutionStoreResult<BoxRecord> {
        self.transition_with(
            execution_id,
            expected_generation,
            expected_state,
            next_state,
            |_| {},
        )
    }

    /// Persist one legal transition and update runtime evidence in the same
    /// transaction.
    pub fn transition_with(
        &self,
        execution_id: &ExecutionId,
        expected_generation: ExecutionGeneration,
        expected_state: ManagedExecutionState,
        next_state: ManagedExecutionState,
        update: impl FnOnce(&mut BoxRecord),
    ) -> ManagedExecutionStoreResult<BoxRecord> {
        let execution_id = execution_id.clone();
        BoxStateStore::transact(&self.path, move |store| {
            let record = store
                .find_by_id_mut(execution_id.as_str())
                .ok_or_else(|| ManagedExecutionStoreError::NotFound(execution_id.clone()))?;
            let actual_state = record
                .managed_state()
                .map_err(|error| ManagedExecutionStoreError::InvalidRecord(error.to_string()))?
                .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(execution_id.clone()))?;
            let metadata = record
                .managed_execution
                .as_ref()
                .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(execution_id.clone()))?;

            if metadata.generation != expected_generation || actual_state != expected_state {
                return Err(ManagedExecutionStoreError::Conflict {
                    execution_id: execution_id.clone(),
                    message: format!(
                        "expected {expected_state} generation {}, found {actual_state} generation {}",
                        expected_generation.get(),
                        metadata.generation.get()
                    ),
                });
            }

            let next_generation = transition_generation(
                &execution_id,
                expected_state,
                next_state,
                expected_generation,
            )?;
            let original_metadata = record
                .managed_execution
                .as_ref()
                .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(execution_id.clone()))?
                .clone();
            update(record);
            if record.id != execution_id.as_str() {
                return Err(ManagedExecutionStoreError::InvalidRecord(format!(
                    "transition changed execution ID {execution_id}"
                )));
            }
            let updated_metadata = record
                .managed_execution
                .as_ref()
                .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(execution_id.clone()))?;
            if updated_metadata.operation_id != original_metadata.operation_id
                || !same_creation_intent(updated_metadata, &original_metadata)?
            {
                return Err(ManagedExecutionStoreError::InvalidRecord(format!(
                    "transition changed creation identity for {execution_id}"
                )));
            }
            record.status = next_state.as_status().to_string();
            let metadata = record
                .managed_execution
                .as_mut()
                .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(execution_id.clone()))?;
            metadata.generation = next_generation;
            if expected_state == ManagedExecutionState::RestartStarting
                && matches!(
                    next_state,
                    ManagedExecutionState::Running | ManagedExecutionState::Failed
                )
            {
                let Some(ManagedExecutionOperation::Restart {
                    operation_id,
                    source_generation,
                    stop_timeout_secs,
                    ..
                }) = metadata.pending_operation.as_ref()
                else {
                    return Err(ManagedExecutionStoreError::InvalidRecord(format!(
                        "restart completion for {execution_id} has no persisted restart intent"
                    )));
                };
                metadata.last_restart = Some(ManagedRestartCompletion {
                    operation_id: operation_id.clone(),
                    source_generation: *source_generation,
                    target_generation: next_generation,
                    outcome: if next_state == ManagedExecutionState::Running {
                        ManagedRestartOutcome::Running
                    } else {
                        ManagedRestartOutcome::Failed
                    },
                    stop_timeout_secs: *stop_timeout_secs,
                });
            }
            metadata.pending_operation = match next_state {
                ManagedExecutionState::Starting => Some(ManagedExecutionOperation::Start),
                ManagedExecutionState::Pausing => match metadata.pending_operation.take() {
                    Some(operation @ ManagedExecutionOperation::Pause { .. }) => Some(operation),
                    _ => Some(ManagedExecutionOperation::Pause { keep_memory: false }),
                },
                ManagedExecutionState::Resuming => Some(ManagedExecutionOperation::Resume),
                ManagedExecutionState::Snapshotting => match metadata.pending_operation.take() {
                    Some(operation @ ManagedExecutionOperation::Snapshot { .. }) => Some(operation),
                    _ => {
                        return Err(ManagedExecutionStoreError::InvalidRecord(format!(
                        "snapshot transition for {execution_id} has no persisted snapshot intent"
                    )))
                    }
                },
                ManagedExecutionState::Killing => match metadata.pending_operation.take() {
                    Some(operation @ ManagedExecutionOperation::Kill { .. }) => Some(operation),
                    _ => Some(ManagedExecutionOperation::Kill {
                        signal: None,
                        timeout_secs: None,
                    }),
                },
                ManagedExecutionState::Removing => Some(ManagedExecutionOperation::Remove),
                ManagedExecutionState::RestartStopping | ManagedExecutionState::RestartStarting => {
                    match metadata.pending_operation.take() {
                        Some(operation @ ManagedExecutionOperation::Restart { .. }) => {
                            Some(operation)
                        }
                        _ => {
                            return Err(ManagedExecutionStoreError::InvalidRecord(format!(
                            "restart transition for {execution_id} has no persisted restart intent"
                        )))
                        }
                    }
                }
                ManagedExecutionState::Creating
                | ManagedExecutionState::Created
                | ManagedExecutionState::Running
                | ManagedExecutionState::Paused
                | ManagedExecutionState::Stopped
                | ManagedExecutionState::Failed => None,
            };
            Ok(record.clone())
        })
    }
}

fn validate_new_record(record: &BoxRecord) -> ManagedExecutionStoreResult<ExecutionId> {
    let execution_id = ExecutionId::new(record.id.clone())
        .map_err(|error| ManagedExecutionStoreError::InvalidRecord(error.to_string()))?;
    let metadata = record
        .managed_execution
        .as_ref()
        .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(execution_id.clone()))?;
    metadata
        .validate()
        .map_err(|error| ManagedExecutionStoreError::InvalidRecord(error.to_string()))?;
    let state = record
        .managed_state()
        .map_err(|error| ManagedExecutionStoreError::InvalidRecord(error.to_string()))?
        .ok_or_else(|| ManagedExecutionStoreError::Unmanaged(execution_id.clone()))?;
    if state != ManagedExecutionState::Created {
        return Err(ManagedExecutionStoreError::InvalidRecord(format!(
            "new execution {execution_id} must be created, found {state}"
        )));
    }
    if metadata.generation != ExecutionGeneration::INITIAL {
        return Err(ManagedExecutionStoreError::InvalidRecord(format!(
            "new execution {execution_id} must start at generation {}",
            ExecutionGeneration::INITIAL.get()
        )));
    }
    Ok(execution_id)
}

fn same_creation_intent(
    left: &crate::ManagedExecutionMetadata,
    right: &crate::ManagedExecutionMetadata,
) -> ManagedExecutionStoreResult<bool> {
    let left_request = serde_json::to_value(&left.request)
        .map_err(|error| ManagedExecutionStoreError::InvalidRecord(error.to_string()))?;
    let right_request = serde_json::to_value(&right.request)
        .map_err(|error| ManagedExecutionStoreError::InvalidRecord(error.to_string()))?;
    Ok(left_request == right_request && left.plan == right.plan)
}

fn transition_generation(
    execution_id: &ExecutionId,
    from: ManagedExecutionState,
    to: ManagedExecutionState,
    current: ExecutionGeneration,
) -> ManagedExecutionStoreResult<ExecutionGeneration> {
    use ManagedExecutionState::{
        Created, Creating, Failed, Killing, Paused, Pausing, RestartStarting, RestartStopping,
        Resuming, Running, Snapshotting, Starting, Stopped,
    };

    let legal = matches!(
        (from, to),
        (Creating, Created | Starting | Killing | Stopped | Failed)
            | (
                Created,
                Starting | Killing | RestartStopping | Stopped | Failed
            )
            | (
                Starting,
                Created | Creating | Running | Killing | Stopped | Failed
            )
            | (
                Running,
                Pausing | Snapshotting | Killing | RestartStopping | Stopped | Failed
            )
            | (Pausing, Paused | Running | Killing | Stopped | Failed)
            | (
                Paused,
                Resuming | Snapshotting | Killing | RestartStopping | Stopped | Failed
            )
            | (Resuming, Running | Paused | Killing | Stopped | Failed)
            | (Snapshotting, Running | Paused | Stopped | Failed)
            | (Killing, Stopped | Failed)
            | (Stopped | Failed, RestartStopping)
            | (RestartStopping, RestartStarting)
            | (RestartStarting, Running | Failed)
    );
    if !legal {
        return Err(ManagedExecutionStoreError::InvalidTransition {
            execution_id: execution_id.clone(),
            from,
            to,
        });
    }

    if matches!(
        (from, to),
        (Pausing, Paused) | (Resuming, Running) | (RestartStopping, RestartStarting)
    ) {
        let value = current.get().checked_add(1).ok_or_else(|| {
            ManagedExecutionStoreError::InvalidRecord(format!(
                "execution {execution_id} generation is exhausted"
            ))
        })?;
        return ExecutionGeneration::new(value)
            .map_err(|error| ManagedExecutionStoreError::InvalidRecord(error.to_string()));
    }
    Ok(current)
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Barrier};

    use a3s_box_core::{CreateExecutionRequest, ExecutionIsolation, ExecutionSnapshotId};

    use super::*;
    use crate::ManagedExecutionMetadata;

    fn managed_record(id: &str, operation: &str) -> BoxRecord {
        let mut record: BoxRecord = serde_json::from_value(serde_json::json!({
            "id": id,
            "short_id": BoxRecord::make_short_id(id),
            "name": format!("box-{id}"),
            "image": "alpine:latest",
            "isolation": "sandbox",
            "status": "created",
            "pid": null,
            "cpus": 1,
            "memory_mb": 128,
            "volumes": [],
            "env": {},
            "cmd": ["sh"],
            "box_dir": format!("/tmp/{id}"),
            "console_log": format!("/tmp/{id}/console.log"),
            "created_at": "2026-07-14T12:00:00Z",
            "started_at": null,
            "auto_remove": false
        }))
        .unwrap();
        let config = a3s_box_core::BoxConfig {
            image: "alpine:latest".to_string(),
            isolation: ExecutionIsolation::Sandbox,
            ..Default::default()
        };
        record.managed_execution = Some(
            ManagedExecutionMetadata::new(
                OperationId::new(operation).unwrap(),
                ExecutionGeneration::INITIAL,
                CreateExecutionRequest {
                    external_sandbox_id: "sandbox-1".to_string(),
                    config,
                    labels: Default::default(),
                    policy: Default::default(),
                    rootfs_snapshot_id: None,
                },
            )
            .unwrap(),
        );
        record
    }

    #[test]
    fn reservation_is_idempotent_for_the_same_full_request() {
        let directory = tempfile::tempdir().unwrap();
        let store = ManagedExecutionStore::new(directory.path().join("boxes.json"));

        let first = store.reserve(managed_record("execution-1", "operation-1"));
        let retry = store.reserve(managed_record("execution-2", "operation-1"));

        assert!(first.unwrap().is_new());
        let retry = retry.unwrap();
        assert!(!retry.is_new());
        assert_eq!(retry.record().id, "execution-1");
        assert_eq!(
            BoxStateStore::load(store.path()).unwrap().records().len(),
            1
        );
    }

    #[test]
    fn reservation_rejects_operation_reuse_with_different_intent() {
        let directory = tempfile::tempdir().unwrap();
        let store = ManagedExecutionStore::new(directory.path().join("boxes.json"));
        store
            .reserve(managed_record("execution-1", "operation-1"))
            .unwrap();
        let mut conflicting = managed_record("execution-2", "operation-1");
        conflicting
            .managed_execution
            .as_mut()
            .unwrap()
            .request
            .external_sandbox_id = "sandbox-2".to_string();

        let error = store.reserve(conflicting).unwrap_err();

        assert!(matches!(error, ManagedExecutionStoreError::Conflict { .. }));
        assert_eq!(
            BoxStateStore::load(store.path()).unwrap().records().len(),
            1
        );
    }

    #[test]
    fn removal_claim_is_generation_fenced_durable_and_idempotent() {
        let directory = tempfile::tempdir().unwrap();
        let store = ManagedExecutionStore::new(directory.path().join("boxes.json"));
        let id = ExecutionId::new("execution-1").unwrap();
        store
            .reserve(managed_record(id.as_str(), "operation-1"))
            .unwrap();

        let claimed = store
            .begin_remove(&id, ExecutionGeneration::INITIAL)
            .unwrap()
            .unwrap();
        assert_eq!(
            claimed.managed_state().unwrap(),
            Some(ManagedExecutionState::Removing)
        );
        assert!(matches!(
            claimed
                .managed_execution
                .as_ref()
                .unwrap()
                .pending_operation,
            Some(ManagedExecutionOperation::Remove)
        ));

        let reopened = ManagedExecutionStore::new(store.path().to_path_buf());
        assert_eq!(
            reopened
                .begin_remove(&id, ExecutionGeneration::INITIAL)
                .unwrap()
                .unwrap()
                .managed_state()
                .unwrap(),
            Some(ManagedExecutionState::Removing)
        );
        assert!(matches!(
            reopened.begin_remove(&id, ExecutionGeneration::new(2).unwrap()),
            Err(ManagedExecutionStoreError::Conflict { .. })
        ));

        assert!(reopened
            .finish_remove(&id, ExecutionGeneration::INITIAL)
            .unwrap());
        assert!(!reopened
            .finish_remove(&id, ExecutionGeneration::INITIAL)
            .unwrap());
        assert!(reopened.get(&id).unwrap().is_none());
    }

    #[test]
    fn pause_and_resume_completion_advance_generation_once() {
        let directory = tempfile::tempdir().unwrap();
        let store = ManagedExecutionStore::new(directory.path().join("boxes.json"));
        let id = ExecutionId::new("execution-1").unwrap();
        store
            .reserve(managed_record(id.as_str(), "operation-1"))
            .unwrap();

        store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Created,
                ManagedExecutionState::Starting,
            )
            .unwrap();
        let running = store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Starting,
                ManagedExecutionState::Running,
            )
            .unwrap();
        assert_eq!(
            running.managed_execution.unwrap().generation,
            ExecutionGeneration::INITIAL
        );
        store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Running,
                ManagedExecutionState::Pausing,
            )
            .unwrap();
        let paused = store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Pausing,
                ManagedExecutionState::Paused,
            )
            .unwrap();
        let generation_two = ExecutionGeneration::new(2).unwrap();
        assert_eq!(paused.managed_execution.unwrap().generation, generation_two);
        store
            .transition(
                &id,
                generation_two,
                ManagedExecutionState::Paused,
                ManagedExecutionState::Resuming,
            )
            .unwrap();
        let resumed = store
            .transition(
                &id,
                generation_two,
                ManagedExecutionState::Resuming,
                ManagedExecutionState::Running,
            )
            .unwrap();
        assert_eq!(
            resumed.managed_execution.unwrap().generation,
            ExecutionGeneration::new(3).unwrap()
        );
    }

    #[test]
    fn snapshot_intent_is_durable_and_preserves_runtime_generation() {
        let directory = tempfile::tempdir().unwrap();
        let store = ManagedExecutionStore::new(directory.path().join("boxes.json"));
        let id = ExecutionId::new("execution-1").unwrap();
        store
            .reserve(managed_record(id.as_str(), "operation-1"))
            .unwrap();
        store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Created,
                ManagedExecutionState::Starting,
            )
            .unwrap();
        store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Starting,
                ManagedExecutionState::Running,
            )
            .unwrap();
        let snapshot_id = ExecutionSnapshotId::new("snapshot-1").unwrap();
        let claimed = store
            .transition_with(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Running,
                ManagedExecutionState::Snapshotting,
                |record| {
                    record.managed_execution.as_mut().unwrap().pending_operation =
                        Some(ManagedExecutionOperation::Snapshot {
                            snapshot_id: snapshot_id.clone(),
                            source_state: ManagedExecutionState::Running,
                        });
                },
            )
            .unwrap();
        assert_eq!(
            claimed.managed_execution.as_ref().unwrap().generation,
            ExecutionGeneration::INITIAL
        );
        assert!(matches!(
            claimed
                .managed_execution
                .as_ref()
                .unwrap()
                .pending_operation
                .as_ref(),
            Some(ManagedExecutionOperation::Snapshot {
                snapshot_id,
                source_state: ManagedExecutionState::Running,
            }) if snapshot_id.as_str() == "snapshot-1"
        ));

        let reopened = ManagedExecutionStore::new(store.path().to_path_buf());
        let persisted = reopened.get(&id).unwrap().unwrap();
        assert_eq!(
            persisted.managed_state().unwrap(),
            Some(ManagedExecutionState::Snapshotting)
        );
        let completed = reopened
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Snapshotting,
                ManagedExecutionState::Running,
            )
            .unwrap();
        let metadata = completed.managed_execution.unwrap();
        assert_eq!(metadata.generation, ExecutionGeneration::INITIAL);
        assert!(metadata.pending_operation.is_none());
        assert!(matches!(
            reopened.transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Running,
                ManagedExecutionState::Snapshotting,
            ),
            Err(ManagedExecutionStoreError::InvalidRecord(_))
        ));
    }

    #[test]
    fn restart_advances_generation_between_durable_teardown_and_startup() {
        let directory = tempfile::tempdir().unwrap();
        let store = ManagedExecutionStore::new(directory.path().join("boxes.json"));
        let id = ExecutionId::new("execution-1").unwrap();
        store
            .reserve(managed_record(id.as_str(), "operation-create"))
            .unwrap();
        store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Created,
                ManagedExecutionState::Starting,
            )
            .unwrap();
        store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Starting,
                ManagedExecutionState::Running,
            )
            .unwrap();
        let restart_operation = OperationId::new("operation-restart").unwrap();
        let stopping = store
            .transition_with(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Running,
                ManagedExecutionState::RestartStopping,
                |record| {
                    record.managed_execution.as_mut().unwrap().pending_operation =
                        Some(ManagedExecutionOperation::Restart {
                            operation_id: restart_operation.clone(),
                            source_generation: ExecutionGeneration::INITIAL,
                            source_state: ManagedExecutionState::Running,
                            stop_timeout_secs: Some(10),
                        });
                },
            )
            .unwrap();
        assert_eq!(
            stopping.managed_execution.as_ref().unwrap().generation,
            ExecutionGeneration::INITIAL
        );

        let starting = store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::RestartStopping,
                ManagedExecutionState::RestartStarting,
            )
            .unwrap();
        let generation_two = ExecutionGeneration::new(2).unwrap();
        assert_eq!(
            starting.managed_execution.as_ref().unwrap().generation,
            generation_two
        );
        let running = store
            .transition(
                &id,
                generation_two,
                ManagedExecutionState::RestartStarting,
                ManagedExecutionState::Running,
            )
            .unwrap();
        let metadata = running.managed_execution.unwrap();
        assert_eq!(metadata.generation, generation_two);
        assert!(metadata.pending_operation.is_none());
        let completed = metadata.last_restart.unwrap();
        assert_eq!(completed.operation_id, restart_operation);
        assert_eq!(completed.source_generation, ExecutionGeneration::INITIAL);
        assert_eq!(completed.target_generation, generation_two);
        assert_eq!(completed.outcome, ManagedRestartOutcome::Running);
        assert_eq!(completed.stop_timeout_secs, Some(10));
    }

    #[test]
    fn stale_generation_and_invalid_edges_do_not_change_disk() {
        let directory = tempfile::tempdir().unwrap();
        let store = ManagedExecutionStore::new(directory.path().join("boxes.json"));
        let id = ExecutionId::new("execution-1").unwrap();
        store
            .reserve(managed_record(id.as_str(), "operation-1"))
            .unwrap();
        store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Created,
                ManagedExecutionState::Starting,
            )
            .unwrap();
        store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Starting,
                ManagedExecutionState::Running,
            )
            .unwrap();

        let stale = store.transition(
            &id,
            ExecutionGeneration::new(2).unwrap(),
            ManagedExecutionState::Running,
            ManagedExecutionState::Pausing,
        );
        let invalid = store.transition(
            &id,
            ExecutionGeneration::INITIAL,
            ManagedExecutionState::Running,
            ManagedExecutionState::Paused,
        );

        assert!(matches!(
            stale,
            Err(ManagedExecutionStoreError::Conflict { .. })
        ));
        assert!(matches!(
            invalid,
            Err(ManagedExecutionStoreError::InvalidTransition { .. })
        ));
        let persisted = store.get(&id).unwrap().unwrap();
        assert_eq!(
            persisted.managed_state().unwrap(),
            Some(ManagedExecutionState::Running)
        );
        assert_eq!(
            persisted.managed_execution.unwrap().generation,
            ExecutionGeneration::INITIAL
        );
    }

    #[cfg(unix)]
    #[test]
    fn concurrent_claims_have_one_winner() {
        let directory = tempfile::tempdir().unwrap();
        let store = Arc::new(ManagedExecutionStore::new(
            directory.path().join("boxes.json"),
        ));
        let id = ExecutionId::new("execution-1").unwrap();
        store
            .reserve(managed_record(id.as_str(), "operation-1"))
            .unwrap();
        store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Created,
                ManagedExecutionState::Starting,
            )
            .unwrap();
        store
            .transition(
                &id,
                ExecutionGeneration::INITIAL,
                ManagedExecutionState::Starting,
                ManagedExecutionState::Running,
            )
            .unwrap();
        let barrier = Arc::new(Barrier::new(3));
        let handles: Vec<_> = (0..2)
            .map(|_| {
                let store = Arc::clone(&store);
                let id = id.clone();
                let barrier = Arc::clone(&barrier);
                std::thread::spawn(move || {
                    barrier.wait();
                    store.transition(
                        &id,
                        ExecutionGeneration::INITIAL,
                        ManagedExecutionState::Running,
                        ManagedExecutionState::Pausing,
                    )
                })
            })
            .collect();
        barrier.wait();
        let results: Vec<_> = handles
            .into_iter()
            .map(|handle| handle.join().unwrap())
            .collect();

        assert_eq!(results.iter().filter(|result| result.is_ok()).count(), 1);
        assert_eq!(
            results
                .iter()
                .filter(|result| matches!(result, Err(ManagedExecutionStoreError::Conflict { .. })))
                .count(),
            1
        );
        assert_eq!(
            store.get(&id).unwrap().unwrap().managed_state().unwrap(),
            Some(ManagedExecutionState::Pausing)
        );
    }
}