a3s-box-runtime 3.2.2

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
//! Runtime lifecycle projection over Box's durable local execution manager.

use std::collections::BTreeMap;
use std::time::Duration;

use a3s_box_core::{ExecutionManager, KillOutcome, ReconcileOutcome};
use a3s_runtime::contract::{
    IsolationLevel, RestartPolicy, RuntimeActionRequest, RuntimeEvidence, RuntimeFailure,
    RuntimeHealthState, RuntimeInspection, RuntimeObservation, RuntimeRemoval, RuntimeUnitClass,
    RuntimeUnitSpec, RuntimeUnitState,
};
use a3s_runtime::{RuntimeError, RuntimeResult, RuntimeUnitRecord};
use sha2::{Digest, Sha256};

use crate::{BoxRecord, ManagedExecutionState};

use super::mapping::{creation_request_for, operation, validate_creation_spec};
use super::metadata::{
    local_identity, map_execution_error, now_ms, provider_identity_matches, timestamp_ms,
    validate_record_for_spec, SPEC_DIGEST_LABEL,
};
use super::BoxRuntimeDriver;

impl BoxRuntimeDriver {
    pub(super) async fn apply_unit(
        &self,
        spec: &RuntimeUnitSpec,
        current: &RuntimeObservation,
    ) -> RuntimeResult<RuntimeObservation> {
        // Complete the side-effect-free validation pass before storage
        // preparation can materialize a caller view or create a named Volume.
        validate_creation_spec(
            spec,
            self.execution_isolation,
            self.sev_snp_config(),
            &self.config.secret_root,
        )?;
        self.secret_materialization.require_configured_for(spec)?;
        self.artifact_storage.require_configured_for(spec)?;
        let mut record = self.find_generation(spec).await?;

        if let Some(existing) = record.as_ref() {
            provider_identity_matches(current, existing)?;
            if should_replace_confirmed_loss(existing, current)? {
                self.retire_record(existing.clone(), &spec.unit_id).await?;
                record = None;
            }
        }

        let record = match record {
            Some(record) => record,
            None => {
                let storage = self.artifact_storage.prepare_plan(spec).await?;
                let request = creation_request_for(
                    spec,
                    self.execution_isolation,
                    self.sev_snp_config(),
                    &self.config.secret_root,
                    &storage,
                )?;
                let operation_id = operation(spec)?;
                let reservation = self
                    .bounded("reservation", async {
                        self.manager
                            .create(request, &operation_id)
                            .await
                            .map_err(|error| map_execution_error(&spec.unit_id, error))
                    })
                    .await?;
                let record = self
                    .manager
                    .managed_record(&reservation.execution_id)
                    .await
                    .map_err(|error| map_execution_error(&spec.unit_id, error))?
                    .ok_or_else(|| {
                        RuntimeError::Protocol(
                            "Box lost a durable execution immediately after reservation".into(),
                        )
                    })?;
                validate_record_for_spec(
                    &record,
                    spec,
                    self.execution_isolation,
                    self.sev_snp_config(),
                    &self.config.secret_root,
                )?;
                self.artifact_storage.validate_record(spec, &record).await?;
                record
            }
        };
        self.prepare_secret_material(spec, &record).await?;
        let record = self.ensure_started(spec, record).await?;

        // Retire older Runtime generations as soon as the current generation
        // has a durable provider identity. A retry resumes any partial cleanup.
        self.retire_stale_generations(spec, &record.id).await?;

        // A confidential guest is intentionally booted IDLE. Its first live
        // observation acquires and persists attestation, then releases the main
        // process. This must happen before a Task wait or a Service health probe.
        let attested_running = if spec.isolation == IsolationLevel::Confidential {
            // Building the raw evidence acquires attestation and releases the
            // deferred main process. A running Service is not validated or
            // published until its readiness and liveness evidence is attached.
            Some(self.build_observation(spec, &record, None, None).await?)
        } else {
            None
        };

        let observation = match spec.class {
            RuntimeUnitClass::Task => self.wait_for_task(spec, record).await?,
            RuntimeUnitClass::Service if spec.health.is_some() => {
                self.wait_for_service_health(spec, record).await?
            }
            RuntimeUnitClass::Service => match attested_running {
                Some(observation) => self.finish_observation(spec, &record, observation).await?,
                None => self.observation(spec, &record, None, None).await?,
            },
        };
        let observation = self.recover_unhealthy_liveness(spec, observation).await?;
        super::attestation::validate_continuity(current, &observation)?;
        Ok(observation)
    }

    pub(super) async fn inspect_unit(
        &self,
        unit: &RuntimeUnitRecord,
    ) -> RuntimeResult<RuntimeInspection> {
        unit.validate().map_err(RuntimeError::Protocol)?;
        let Some(record) = self.find_generation(&unit.spec).await? else {
            self.service_endpoints
                .remove_runtime(&unit.spec.unit_id, unit.spec.generation)
                .await;
            self.secret_materialization.cleanup_spec(&unit.spec).await?;
            self.artifact_storage.cleanup_spec(&unit.spec).await?;
            return Ok(not_found(&unit.spec));
        };
        provider_identity_matches(&unit.observation, &record)?;
        let before = local_identity(&record)?.2;
        let record = self.refresh_record(&unit.spec, record).await?;
        if provider_was_lost(before, &record)? {
            self.service_endpoints
                .remove_runtime(&unit.spec.unit_id, unit.spec.generation)
                .await;
            return Ok(not_found(&unit.spec));
        }

        self.prepare_secret_material(&unit.spec, &record).await?;

        let record = if should_restart(&unit.spec, &record)? {
            self.restart_record(&unit.spec, record).await?
        } else {
            record
        };
        let observation = self.observe_service_health(&unit.spec, &record).await?;
        let observation = self
            .recover_unhealthy_liveness(&unit.spec, observation)
            .await?;
        super::attestation::validate_continuity(&unit.observation, &observation)?;
        Ok(RuntimeInspection::Found {
            schema: RuntimeInspection::SCHEMA.into(),
            observation: Box::new(observation),
        })
    }

    pub(super) async fn stop_unit(
        &self,
        unit: &RuntimeUnitRecord,
        request: &RuntimeActionRequest,
    ) -> RuntimeResult<RuntimeObservation> {
        request.validate().map_err(RuntimeError::InvalidRequest)?;
        validate_action_identity(unit, request)?;
        if unit.observation.state.is_terminal() {
            self.service_endpoints
                .remove_runtime(&unit.spec.unit_id, unit.spec.generation)
                .await;
            return Ok(unit.observation.clone());
        }

        let Some(record) = self.find_generation(&unit.spec).await? else {
            self.service_endpoints
                .remove_runtime(&unit.spec.unit_id, unit.spec.generation)
                .await;
            return unknown_observation(&unit.observation);
        };
        provider_identity_matches(&unit.observation, &record)?;
        let before = local_identity(&record)?.2;
        let mut record = self.refresh_record(&unit.spec, record).await?;
        if provider_was_lost(before, &record)? {
            self.service_endpoints
                .remove_runtime(&unit.spec.unit_id, unit.spec.generation)
                .await;
            return unknown_observation(&unit.observation);
        }

        let (_, generation, state) = local_identity(&record)?;
        if !state.is_terminal() {
            let execution_id = a3s_box_core::ExecutionId::new(record.id.clone())
                .map_err(|error| RuntimeError::Protocol(error.to_string()))?;
            self.manager
                .kill(&execution_id, generation)
                .await
                .map_err(|error| map_execution_error(&unit.spec.unit_id, error))?;
            record = self.load_record(&unit.spec, &execution_id).await?;
        }

        self.service_endpoints
            .remove_runtime(&unit.spec.unit_id, unit.spec.generation)
            .await;

        let observation = self
            .observation(&unit.spec, &record, Some(RuntimeUnitState::Stopped), None)
            .await?;
        super::attestation::validate_continuity(&unit.observation, &observation)?;
        Ok(observation)
    }

    pub(super) async fn remove_unit(
        &self,
        unit: &RuntimeUnitRecord,
        request: &RuntimeActionRequest,
    ) -> RuntimeResult<RuntimeRemoval> {
        request.validate().map_err(RuntimeError::InvalidRequest)?;
        validate_action_identity(unit, request)?;
        let record = self.find_generation(&unit.spec).await?;
        let already_absent = record.is_none();
        if let Some(record) = record {
            provider_identity_matches(&unit.observation, &record)?;
            self.retire_record(record, &unit.spec.unit_id).await?;
        } else {
            // A previous removal may have crossed the durable Box boundary
            // before caller-owned Artifact cleanup completed.
            self.artifact_storage.cleanup_spec(&unit.spec).await?;
        }
        self.service_endpoints
            .remove_runtime(&unit.spec.unit_id, unit.spec.generation)
            .await;
        self.secret_materialization.cleanup_spec(&unit.spec).await?;
        let removal = RuntimeRemoval {
            schema: RuntimeRemoval::SCHEMA.into(),
            request_id: request.request_id.clone(),
            unit_id: request.unit_id.clone(),
            generation: request.generation,
            removed_at_ms: now_ms(),
            already_absent,
        };
        removal.validate().map_err(RuntimeError::Protocol)?;
        Ok(removal)
    }

    async fn ensure_started(
        &self,
        spec: &RuntimeUnitSpec,
        record: BoxRecord,
    ) -> RuntimeResult<BoxRecord> {
        validate_record_for_spec(
            &record,
            spec,
            self.execution_isolation,
            self.sev_snp_config(),
            &self.config.secret_root,
        )?;
        self.artifact_storage.validate_record(spec, &record).await?;
        let (execution_id, generation, state) = local_identity(&record)?;
        match state {
            ManagedExecutionState::Creating
            | ManagedExecutionState::Created
            | ManagedExecutionState::Starting => {
                let _registry_auth = self
                    .secret_materialization
                    .prepare_registry_auth_for_start(
                        spec,
                        &record,
                        &self.config.home_dir,
                        self.transient_registry_auth.as_ref(),
                    )
                    .await?;
                // A recovered `Starting` record has already crossed the start
                // boundary, so resetting it would race a live attachment. For
                // a fresh `Creating`/`Created` record, clear output staging at
                // the last safe point before Box attaches and starts it.
                if state != ManagedExecutionState::Starting {
                    self.artifact_storage.reset_outputs_for_start(spec).await?;
                }
                let result = self
                    .bounded("startup", async {
                        self.manager
                            .start(&execution_id, generation)
                            .await
                            .map_err(|error| map_execution_error(&spec.unit_id, error))
                    })
                    .await;
                if let Err(error) = result {
                    return self
                        .load_terminal_after_control_error(spec, &execution_id, error)
                        .await;
                }
                self.load_record(spec, &execution_id).await
            }
            ManagedExecutionState::Running => Ok(record),
            ManagedExecutionState::Stopped | ManagedExecutionState::Failed => {
                if should_restart(spec, &record)? {
                    self.restart_record(spec, record).await
                } else {
                    Ok(record)
                }
            }
            ManagedExecutionState::Removing => Err(RuntimeError::ProviderUnavailable(format!(
                "Box execution {} is still being removed",
                record.id
            ))),
            state => Err(RuntimeError::ProviderUnavailable(format!(
                "Box execution {} cannot be applied while in state {state}",
                record.id
            ))),
        }
    }

    async fn prepare_secret_material(
        &self,
        spec: &RuntimeUnitSpec,
        record: &BoxRecord,
    ) -> RuntimeResult<()> {
        if spec.secrets.is_empty() {
            return Ok(());
        }
        let state = local_identity(record)?.2;
        if matches!(
            state,
            ManagedExecutionState::Creating | ManagedExecutionState::Created
        ) {
            self.secret_materialization
                .materialize_for_start(spec)
                .await
        } else if matches!(
            state,
            ManagedExecutionState::Stopped | ManagedExecutionState::Failed
        ) && should_restart(spec, record)?
        {
            Ok(())
        } else {
            self.secret_materialization.require_materialized(spec).await
        }
    }

    async fn wait_for_task(
        &self,
        spec: &RuntimeUnitSpec,
        mut record: BoxRecord,
    ) -> RuntimeResult<RuntimeObservation> {
        let timeout_ms = spec.resources.execution_timeout_ms.ok_or_else(|| {
            RuntimeError::InvalidRequest("Runtime Task has no execution timeout".into())
        })?;
        let timeout = Duration::from_millis(timeout_ms);
        let deadline = tokio::time::Instant::now() + timeout;

        loop {
            let before = local_identity(&record)?.2;
            record = self.refresh_record(spec, record).await?;
            let state = local_identity(&record)?.2;
            if state.is_terminal() {
                if provider_was_lost(before, &record)? {
                    return self
                        .observation(
                            spec,
                            &record,
                            Some(RuntimeUnitState::Failed),
                            Some(RuntimeFailure {
                                code: "provider_lost".into(),
                                message: "Box lost the provider resource".into(),
                                retryable: true,
                            }),
                        )
                        .await;
                }
                if should_restart(spec, &record)? {
                    record = self.restart_record(spec, record).await?;
                    if spec.isolation == IsolationLevel::Confidential {
                        self.observation(spec, &record, None, None).await?;
                    }
                    continue;
                }
                return self.observation(spec, &record, None, None).await;
            }

            if tokio::time::Instant::now() >= deadline {
                let (execution_id, generation, _) = local_identity(&record)?;
                self.bounded("Task timeout cleanup", async {
                    self.manager
                        .kill(&execution_id, generation)
                        .await
                        .map_err(|error| map_execution_error(&spec.unit_id, error))
                })
                .await?;
                record = self.load_record(spec, &execution_id).await?;
                return self
                    .observation(
                        spec,
                        &record,
                        Some(RuntimeUnitState::Failed),
                        Some(RuntimeFailure {
                            code: "execution_timeout".into(),
                            message: format!("Box Task exceeded its {timeout_ms} ms timeout"),
                            retryable: false,
                        }),
                    )
                    .await;
            }

            let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
            tokio::time::sleep(self.config.task_poll_interval.min(remaining)).await;
        }
    }

    pub(super) async fn refresh_record(
        &self,
        spec: &RuntimeUnitSpec,
        record: BoxRecord,
    ) -> RuntimeResult<BoxRecord> {
        let (execution_id, _, _) = local_identity(&record)?;
        self.bounded("inspection", async {
            self.manager
                .inspect(&execution_id)
                .await
                .map_err(|error| map_execution_error(&spec.unit_id, error))
        })
        .await?;
        self.load_record(spec, &execution_id).await
    }

    async fn load_record(
        &self,
        spec: &RuntimeUnitSpec,
        execution_id: &a3s_box_core::ExecutionId,
    ) -> RuntimeResult<BoxRecord> {
        let record = self
            .manager
            .managed_record(execution_id)
            .await
            .map_err(|error| map_execution_error(&spec.unit_id, error))?
            .ok_or_else(|| RuntimeError::NotFound {
                unit_id: spec.unit_id.clone(),
            })?;
        validate_record_for_spec(
            &record,
            spec,
            self.execution_isolation,
            self.sev_snp_config(),
            &self.config.secret_root,
        )?;
        self.artifact_storage.validate_record(spec, &record).await?;
        Ok(record)
    }

    async fn load_terminal_after_control_error(
        &self,
        spec: &RuntimeUnitSpec,
        execution_id: &a3s_box_core::ExecutionId,
        control_error: RuntimeError,
    ) -> RuntimeResult<BoxRecord> {
        let current = self.load_record(spec, execution_id).await?;
        if local_identity(&current)?.2.is_terminal() && current.exit_code.is_some() {
            Ok(current)
        } else {
            Err(control_error)
        }
    }

    async fn restart_record(
        &self,
        spec: &RuntimeUnitSpec,
        record: BoxRecord,
    ) -> RuntimeResult<BoxRecord> {
        self.secret_materialization
            .materialize_for_start(spec)
            .await?;
        let _registry_auth = self
            .secret_materialization
            .prepare_registry_auth_for_start(
                spec,
                &record,
                &self.config.home_dir,
                self.transient_registry_auth.as_ref(),
            )
            .await?;
        self.artifact_storage.reset_outputs_for_start(spec).await?;
        let (execution_id, generation, _) = local_identity(&record)?;
        let operation_id = restart_operation(spec, generation)?;
        let result = self
            .bounded_lifecycle(spec, "restart", async {
                self.manager
                    .restart(&execution_id, generation, &operation_id)
                    .await
                    .map_err(|error| map_execution_error(&spec.unit_id, error))
            })
            .await;
        if let Err(error) = result {
            return self
                .load_terminal_after_control_error(spec, &execution_id, error)
                .await;
        }
        self.load_record(spec, &execution_id).await
    }

    /// Applies at most one liveness-driven recovery per public Runtime
    /// operation. This keeps an `Always` policy from spinning forever inside
    /// the provider while still ensuring that unhealthy liveness is an active
    /// recovery signal rather than passive telemetry.
    pub(super) async fn recover_unhealthy_liveness(
        &self,
        spec: &RuntimeUnitSpec,
        observation: RuntimeObservation,
    ) -> RuntimeResult<RuntimeObservation> {
        if spec.class != RuntimeUnitClass::Service
            || observation.state != RuntimeUnitState::Running
            || observation.liveness.as_ref().map(|health| health.state)
                != Some(RuntimeHealthState::Unhealthy)
        {
            return Ok(observation);
        }

        let Some(record) = self.find_generation(spec).await? else {
            return Err(RuntimeError::ProviderUnavailable(format!(
                "Box lost Service {:?} before liveness recovery",
                spec.unit_id
            )));
        };
        provider_identity_matches(&observation, &record)?;
        let record = self.refresh_record(spec, record).await?;
        let state = local_identity(&record)?.2;
        let restart = if state == ManagedExecutionState::Running {
            should_restart_unhealthy_liveness(spec, &record)?
        } else if state.is_terminal() {
            should_restart(spec, &record)?
        } else {
            false
        };
        if !restart {
            if state == ManagedExecutionState::Running {
                return Ok(observation);
            }
            return self.observation(spec, &record, None, None).await;
        }

        let record = self.restart_record(spec, record).await?;
        if spec.isolation == IsolationLevel::Confidential {
            self.build_observation(spec, &record, None, None).await?;
        }
        self.wait_for_service_health(spec, record).await
    }

    async fn retire_stale_generations(
        &self,
        spec: &RuntimeUnitSpec,
        current_id: &str,
    ) -> RuntimeResult<()> {
        let records = self.unit_records(&spec.unit_id).await?;
        if !records.iter().any(|record| record.id == current_id) {
            return Err(RuntimeError::ProviderUnavailable(
                "Box lost the current execution during generation reconciliation".into(),
            ));
        }
        for record in records {
            if record.id != current_id {
                self.retire_record(record, &spec.unit_id).await?;
            }
        }

        let remaining = self.unit_records(&spec.unit_id).await?;
        if remaining.len() != 1 || remaining[0].id != current_id {
            return Err(RuntimeError::Protocol(format!(
                "Box generation reconciliation for unit {:?} left {} executions",
                spec.unit_id,
                remaining.len()
            )));
        }
        validate_record_for_spec(
            &remaining[0],
            spec,
            self.execution_isolation,
            self.sev_snp_config(),
            &self.config.secret_root,
        )?;
        self.artifact_storage
            .validate_record(spec, &remaining[0])
            .await
    }

    pub(super) async fn retire_record(
        &self,
        mut record: BoxRecord,
        unit_id: &str,
    ) -> RuntimeResult<()> {
        let secret_digest = record
            .labels
            .get(SPEC_DIGEST_LABEL)
            .cloned()
            .ok_or_else(|| {
                RuntimeError::Protocol(format!(
                    "Box execution {} has no Runtime specification digest",
                    record.id
                ))
            })?;
        let (execution_id, mut generation, mut state) = local_identity(&record)?;
        if matches!(
            state,
            ManagedExecutionState::RestartStopping | ManagedExecutionState::RestartStarting
        ) {
            let create_operation = record
                .managed_execution
                .as_ref()
                .ok_or_else(|| RuntimeError::Protocol("Box execution lost metadata".into()))?
                .operation_id
                .clone();
            match self
                .bounded_record_lifecycle(&record, "restart reconciliation", async {
                    self.manager
                        .reconcile(&create_operation)
                        .await
                        .map_err(|error| map_execution_error(unit_id, error))
                })
                .await
            {
                Ok(ReconcileOutcome::Absent) => {
                    self.service_endpoints.remove_provider(&execution_id).await;
                    self.secret_materialization
                        .cleanup_digest(&secret_digest)
                        .await?;
                    self.artifact_storage.cleanup_digest(&secret_digest).await?;
                    return Ok(());
                }
                Ok(_) => {}
                Err(error) => return Err(error),
            }
            record = self
                .manager
                .managed_record(&execution_id)
                .await
                .map_err(|error| map_execution_error(unit_id, error))?
                .ok_or_else(|| RuntimeError::NotFound {
                    unit_id: unit_id.into(),
                })?;
            (_, generation, state) = local_identity(&record)?;
        }

        if !matches!(
            state,
            ManagedExecutionState::Created
                | ManagedExecutionState::Stopped
                | ManagedExecutionState::Failed
                | ManagedExecutionState::Removing
        ) {
            match self
                .bounded_record_lifecycle(&record, "generation retirement stop", async {
                    self.manager
                        .kill(&execution_id, generation)
                        .await
                        .map_err(|error| map_execution_error(unit_id, error))
                })
                .await
            {
                Ok(KillOutcome::Killed | KillOutcome::AlreadyStopped) => {}
                Err(error) => return Err(error),
            }
            record = self
                .manager
                .managed_record(&execution_id)
                .await
                .map_err(|error| map_execution_error(unit_id, error))?
                .ok_or_else(|| RuntimeError::NotFound {
                    unit_id: unit_id.into(),
                })?;
            generation = local_identity(&record)?.1;
        }

        self.service_endpoints.remove_provider(&execution_id).await;
        self.secret_materialization
            .cleanup_digest(&secret_digest)
            .await?;

        self.bounded("generation retirement removal", async {
            self.manager
                .remove_execution(&execution_id, generation)
                .await
                .map_err(|error| map_execution_error(unit_id, error))
        })
        .await?;
        if self
            .manager
            .managed_record(&execution_id)
            .await
            .map_err(|error| map_execution_error(unit_id, error))?
            .is_some()
        {
            return Err(RuntimeError::Protocol(format!(
                "Box removal left execution {} in durable inventory",
                execution_id
            )));
        }
        self.artifact_storage.cleanup_digest(&secret_digest).await?;
        Ok(())
    }

    pub(super) async fn observation(
        &self,
        spec: &RuntimeUnitSpec,
        record: &BoxRecord,
        state_override: Option<RuntimeUnitState>,
        failure_override: Option<RuntimeFailure>,
    ) -> RuntimeResult<RuntimeObservation> {
        let observation = self
            .build_observation(spec, record, state_override, failure_override)
            .await?;
        self.finish_observation(spec, record, observation).await
    }

    /// Builds provider evidence without publishing or validating a potentially
    /// incomplete running Service observation. Health owns the only call path
    /// that adds readiness and liveness before final endpoint reconciliation.
    pub(super) async fn build_observation(
        &self,
        spec: &RuntimeUnitSpec,
        record: &BoxRecord,
        state_override: Option<RuntimeUnitState>,
        failure_override: Option<RuntimeFailure>,
    ) -> RuntimeResult<RuntimeObservation> {
        validate_record_for_spec(
            record,
            spec,
            self.execution_isolation,
            self.sev_snp_config(),
            &self.config.secret_root,
        )?;
        self.artifact_storage.validate_record(spec, record).await?;
        let state = state_override.unwrap_or(runtime_state(spec, record)?);
        let terminal = state.is_terminal();
        let metadata = record.managed_execution.as_ref().ok_or_else(|| {
            RuntimeError::Protocol(format!("Box execution {} lost metadata", record.id))
        })?;
        let started_at_ms = record.started_at.map(timestamp_ms).transpose()?;
        let finished_at_ms = if terminal {
            Some(
                metadata
                    .finished_at
                    .map(timestamp_ms)
                    .transpose()?
                    .unwrap_or_else(now_ms),
            )
        } else {
            None
        };
        let mut observed_at_ms = now_ms();
        if let Some(started) = started_at_ms {
            observed_at_ms = observed_at_ms.max(started);
        }
        if let Some(finished) = finished_at_ms {
            observed_at_ms = observed_at_ms.max(finished);
        }
        let failure = if state == RuntimeUnitState::Failed {
            Some(failure_override.unwrap_or_else(|| exit_failure(record)))
        } else {
            None
        };
        let outputs = if state == RuntimeUnitState::Succeeded {
            self.artifact_storage.capture_outputs(spec).await?
        } else {
            Vec::new()
        };
        let provider_build = self.provider_build().await?;
        let provider_attestation = self
            .bounded(
                "SEV-SNP attestation",
                self.attestation
                    .reference_for(spec, record, self.sev_snp_config()),
            )
            .await?;
        let mut claims = BTreeMap::from([
            (
                "a3s.box.execution-isolation".into(),
                match self.execution_isolation {
                    a3s_box_core::ExecutionIsolation::Microvm => "microvm".into(),
                    a3s_box_core::ExecutionIsolation::Sandbox => "sandbox".into(),
                },
            ),
            (
                "a3s.box.execution-generation".into(),
                metadata.generation.get().to_string(),
            ),
        ]);
        if spec.isolation == a3s_runtime::contract::IsolationLevel::Confidential {
            let mode = if self.sev_snp_config().is_some_and(|config| config.simulate) {
                "sev-snp-simulated"
            } else {
                "sev-snp-hardware"
            };
            claims.insert("a3s.box.tee".into(), mode.into());
        }
        let observation = RuntimeObservation {
            schema: RuntimeObservation::SCHEMA.into(),
            unit_id: spec.unit_id.clone(),
            generation: spec.generation,
            spec_digest: spec.digest().map_err(RuntimeError::Protocol)?,
            class: spec.class,
            state,
            provider_resource_id: Some(record.id.clone()),
            provider_build: Some(provider_build.clone()),
            observed_at_ms,
            started_at_ms,
            finished_at_ms,
            health: None,
            liveness: None,
            outputs,
            usage: None,
            evidence: Some(RuntimeEvidence {
                provider_build,
                spec_digest: spec.digest().map_err(RuntimeError::Protocol)?,
                semantics_profile_digest: spec.semantics_profile_digest.clone(),
                identity_attachment_digest: spec.identity_attachment_digest.clone(),
                claims,
            }),
            provider_attestation,
            failure,
        };
        Ok(observation)
    }

    /// Reconciles Service endpoints after all lifecycle evidence has been
    /// attached, then performs the single externally relevant validation.
    pub(super) async fn finish_observation(
        &self,
        spec: &RuntimeUnitSpec,
        record: &BoxRecord,
        observation: RuntimeObservation,
    ) -> RuntimeResult<RuntimeObservation> {
        if spec.class == RuntimeUnitClass::Service {
            let (execution_id, execution_generation, _) = local_identity(record)?;
            return self
                .service_endpoints
                .reconcile(spec, execution_id, execution_generation, observation)
                .await;
        }
        observation
            .validate_against(spec)
            .map_err(RuntimeError::Protocol)?;
        Ok(observation)
    }
}

fn runtime_state(spec: &RuntimeUnitSpec, record: &BoxRecord) -> RuntimeResult<RuntimeUnitState> {
    let state = local_identity(record)?.2;
    match state {
        ManagedExecutionState::Creating => Ok(RuntimeUnitState::Preparing),
        ManagedExecutionState::Created
        | ManagedExecutionState::Starting
        | ManagedExecutionState::RestartStarting => Ok(RuntimeUnitState::Starting),
        ManagedExecutionState::Running
        | ManagedExecutionState::Pausing
        | ManagedExecutionState::Paused
        | ManagedExecutionState::Resuming
        | ManagedExecutionState::UpdatingResources
        | ManagedExecutionState::Snapshotting
        | ManagedExecutionState::RestartStopping => Ok(RuntimeUnitState::Running),
        ManagedExecutionState::Killing | ManagedExecutionState::Removing => {
            Ok(RuntimeUnitState::Stopping)
        }
        ManagedExecutionState::Stopped | ManagedExecutionState::Failed => match spec.class {
            RuntimeUnitClass::Task if record.exit_code == Some(0) => {
                Ok(RuntimeUnitState::Succeeded)
            }
            RuntimeUnitClass::Task => Ok(RuntimeUnitState::Failed),
            RuntimeUnitClass::Service if record.exit_code.unwrap_or_default() == 0 => {
                Ok(RuntimeUnitState::Stopped)
            }
            RuntimeUnitClass::Service => Ok(RuntimeUnitState::Failed),
        },
    }
}

fn exit_failure(record: &BoxRecord) -> RuntimeFailure {
    let message = match record.exit_code {
        Some(code) => format!("Box execution exited with code {code}"),
        None => "Box execution exited without a recoverable exit code".into(),
    };
    RuntimeFailure {
        code: "sandbox_exit".into(),
        message,
        retryable: false,
    }
}

fn should_replace_confirmed_loss(
    record: &BoxRecord,
    current: &RuntimeObservation,
) -> RuntimeResult<bool> {
    Ok(current.state == RuntimeUnitState::Unknown
        && local_identity(record)?.2.is_terminal()
        && record.exit_code.is_none())
}

fn provider_was_lost(before: ManagedExecutionState, after: &BoxRecord) -> RuntimeResult<bool> {
    Ok(before.keeps_resources()
        && local_identity(after)?.2 == ManagedExecutionState::Failed
        && after.exit_code.is_none())
}

fn should_restart(spec: &RuntimeUnitSpec, record: &BoxRecord) -> RuntimeResult<bool> {
    if !local_identity(record)?.2.is_terminal() || record.exit_code.is_none() {
        return Ok(false);
    }
    let attempts = local_identity(record)?.1.get().saturating_sub(1);
    Ok(match &spec.restart {
        RestartPolicy::Never => false,
        RestartPolicy::Always => spec.class == RuntimeUnitClass::Service,
        RestartPolicy::OnFailure { max_retries } => {
            record.exit_code != Some(0) && attempts < u64::from(*max_retries)
        }
    })
}

fn should_restart_unhealthy_liveness(
    spec: &RuntimeUnitSpec,
    record: &BoxRecord,
) -> RuntimeResult<bool> {
    if local_identity(record)?.2 != ManagedExecutionState::Running {
        return Ok(false);
    }
    let attempts = local_identity(record)?.1.get().saturating_sub(1);
    Ok(match &spec.restart {
        RestartPolicy::Never => false,
        RestartPolicy::Always => spec.class == RuntimeUnitClass::Service,
        RestartPolicy::OnFailure { max_retries } => attempts < u64::from(*max_retries),
    })
}

fn restart_operation(
    spec: &RuntimeUnitSpec,
    generation: a3s_box_core::ExecutionGeneration,
) -> RuntimeResult<a3s_box_core::OperationId> {
    let mut digest = Sha256::new();
    digest.update(b"a3s-box-runtime-restart-v1\0");
    digest.update(spec.unit_id.as_bytes());
    digest.update(spec.generation.to_be_bytes());
    digest.update(spec.digest().map_err(RuntimeError::Protocol)?.as_bytes());
    digest.update(generation.get().to_be_bytes());
    a3s_box_core::OperationId::new(format!("a3s-runtime-restart-v1:{:x}", digest.finalize()))
        .map_err(|error| RuntimeError::InvalidRequest(error.to_string()))
}

fn validate_action_identity(
    unit: &RuntimeUnitRecord,
    request: &RuntimeActionRequest,
) -> RuntimeResult<()> {
    if request.unit_id != unit.spec.unit_id || request.generation != unit.spec.generation {
        return Err(RuntimeError::InvalidRequest(
            "Runtime action identity does not match its unit record".into(),
        ));
    }
    Ok(())
}

fn unknown_observation(current: &RuntimeObservation) -> RuntimeResult<RuntimeObservation> {
    let mut unknown = current.clone();
    unknown.state = RuntimeUnitState::Unknown;
    unknown.observed_at_ms = unknown.observed_at_ms.max(now_ms());
    unknown.finished_at_ms = None;
    unknown.health = None;
    unknown.liveness = None;
    unknown.outputs.clear();
    unknown.failure = None;
    unknown.clear_service_endpoints();
    unknown.validate().map_err(RuntimeError::Protocol)?;
    Ok(unknown)
}

fn not_found(spec: &RuntimeUnitSpec) -> RuntimeInspection {
    RuntimeInspection::NotFound {
        schema: RuntimeInspection::SCHEMA.into(),
        unit_id: spec.unit_id.clone(),
        last_generation: Some(spec.generation),
    }
}