subc-daemon 0.48.1

Embeddable subc daemon: bootstrap, module supervision, and opaque-byte splice routing.
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
use super::*;

#[tokio::test]
async fn current_hold_notification_preserves_pending_reload() {
    held_pending_work("reload", false).await;
}

#[tokio::test]
async fn current_hold_notification_preserves_crash_backoff() {
    held_pending_work("spawn", false).await;
}

#[tokio::test]
async fn current_hold_notification_preserves_previous_instance_wait() {
    held_pending_work("wait", false).await;
}

#[tokio::test]
async fn refused_restart_preserves_pending_reload_and_daemon_attribution() {
    held_pending_work("reload", true).await;
}

async fn held_pending_work(work: &str, operator_restart: bool) {
    let (_scratch, owner, supervisor, _, spec) =
        crate::config_gate::runtime::RuntimeOwner::unactivated_supervisor_fixture_for_test();
    std::fs::write(
        spec.program
            .parent()
            .unwrap()
            .join("store")
            .join(crate::config_store::STORE_FILE),
        b"tampered",
    )
    .unwrap();
    assert!(owner.check_recovery().unwrap());
    let RegisteredConfiguredModule {
        mut spec,
        mut runtime,
        module,
        mut commands,
    } = supervisor
        .register_configured_with_health(
            spec,
            true,
            Default::default(),
            None,
            Default::default(),
            DEFAULT_REGISTER_TIMEOUT,
        )
        .unwrap();
    let deadline = Instant::now() + Duration::from_secs(120);
    update_snapshot(&module.inner.snapshot, Some("a"), |state| {
        if work == "wait" {
            state.waiting_for_previous_pid = Some(4242);
            state.waiting_for_previous_detail = Some("previous process still alive".into());
        } else {
            state.state = ModuleState::Restarting;
            state.respawn_pending = true;
        }
    })
    .unwrap();
    if work != "wait" {
        *runtime.scheduled_respawn.lock().unwrap() = Some(PendingRespawn {
            deadline,
            kind: if work == "reload" {
                RespawnKind::Reload
            } else {
                RespawnKind::Spawn
            },
            daemon_initiated: true,
            approval: None,
        });
    }
    let (reload_reply, mut reload_result) = oneshot::channel();
    if work == "reload" {
        *runtime.deferred_reload_reply.lock().unwrap() = Some(reload_reply);
    }
    runtime.recovery_is_daemon = true;
    let before = module.inner.snapshot.lock().unwrap().clone();
    let (reply, result) = oneshot::channel();
    let command = if operator_restart {
        SupervisorCommand::Restart {
            requester: None,
            drain_timeout_ms: None,
            received_at_generation: 0,
            queued_at: Instant::now(),
            reply,
        }
    } else {
        SupervisorCommand::ConfigExitHold { reply }
    };
    assert!(
        handle_supervisor_command(
            command,
            &mut spec,
            &mut runtime,
            &supervisor.registry,
            &supervisor.process_liveness,
            &module.inner.snapshot,
            &mut None,
            &mut commands,
            &mut VecDeque::new()
        )
        .await
    );
    let answer = result.await.unwrap();
    if operator_restart {
        assert!(matches!(answer, Err(SuperviseError::ConfigHeld { .. })));
    } else {
        answer.unwrap();
    }
    assert_eq!(
        *module.inner.snapshot.lock().unwrap(),
        before,
        "a notification or refused command cannot own the pending work"
    );
    assert!(
        runtime.recovery_is_daemon,
        "refused commands cannot relabel pending daemon work"
    );
    let scheduled = runtime.scheduled_respawn.lock().unwrap().clone();
    if work == "wait" {
        assert!(scheduled.is_none());
    } else {
        let pending = scheduled.expect("the command erased pending work");
        assert_eq!(pending.deadline, deadline);
        assert_eq!(
            matches!(pending.kind, RespawnKind::Reload),
            work == "reload"
        );
        assert!(pending.daemon_initiated);
    }
    if work == "reload" {
        assert!(runtime.deferred_reload_reply.lock().unwrap().is_some());
        assert!(matches!(
            reload_result.try_recv(),
            Err(oneshot::error::TryRecvError::Empty)
        ));
    }
}

#[tokio::test]
async fn deferred_restart_keeps_target_unavailable_wire_code() {
    deferred_wire_code("restart", "target_unavailable").await;
}

#[tokio::test]
async fn deferred_reload_keeps_reload_failed_wire_code() {
    deferred_wire_code("reload", "reload_failed").await;
}

#[tokio::test]
async fn deferred_enable_keeps_target_unavailable_wire_code() {
    deferred_wire_code("set_enabled", "target_unavailable").await;
}

async fn deferred_wire_code(operation: &str, expected: &str) {
    let (_scratch, _, supervisor, _, spec) =
        crate::config_gate::runtime::RuntimeOwner::unactivated_supervisor_fixture_for_test();
    let RegisteredConfiguredModule {
        module,
        mut commands,
        ..
    } = supervisor
        .register_configured_with_health(
            spec,
            true,
            Default::default(),
            None,
            Default::default(),
            DEFAULT_REGISTER_TIMEOUT,
        )
        .unwrap();
    let responder = tokio::spawn(async move {
        let error = SuperviseError::ConfigDeliveryDeferred {
            module_id: "a".into(),
        };
        match commands.recv().await.unwrap() {
            SupervisorCommand::Restart { reply, .. } | SupervisorCommand::Reload { reply, .. } => {
                reply.send(Err(error)).unwrap();
            }
            SupervisorCommand::SetEnabled { reply, .. } => {
                reply.send(Err(error)).unwrap();
            }
            other => panic!("unexpected request: {other:?}"),
        }
    });
    let handler = crate::ControlHandler::new(supervisor.registry.clone())
        .with_supervisor(supervisor.supervisor_handle.as_ref().unwrap().clone());
    let (tx, _rx) = mpsc::channel(8);
    let ctx = crate::RouteCtx {
        connection_id: ConnectionId::new(51),
        egress: crate::FrameSink::new(tx),
    };
    let request = serde_json::json!({"op":format!("supervisor.{operation}"),"module_id":module.module_id(),"enabled":true});
    let frames = handler
        .handle_control_frame(&ctx, crate::control::claims::tests::request(request))
        .await
        .unwrap();
    responder.await.unwrap();
    let reply: serde_json::Value = serde_json::from_slice(&frames[0].body).unwrap();
    assert_eq!(reply["code"], expected, "{reply}");
    assert!(
        reply["message"]
            .as_str()
            .unwrap()
            .contains("exited delivery target"),
        "{reply}"
    );
}

#[tokio::test]
async fn stale_config_exit_hold_keeps_reload_work_and_answers_its_caller() {
    let (_scratch, owner, supervisor, _, spec) =
        crate::config_gate::runtime::RuntimeOwner::unactivated_supervisor_fixture_for_test();
    assert!(!owner.has_recorded_admission_hold("a"));
    std::fs::write(&spec.args[1], b"keep running").unwrap();
    let RegisteredConfiguredModule {
        mut spec,
        mut runtime,
        module,
        mut commands,
    } = supervisor
        .register_configured_with_health(
            spec,
            true,
            Default::default(),
            None,
            Default::default(),
            DEFAULT_REGISTER_TIMEOUT,
        )
        .unwrap();
    update_snapshot(&module.inner.snapshot, Some("a"), |state| {
        state.state = ModuleState::Restarting;
        state.respawn_pending = true;
    })
    .unwrap();
    let (reply, result) = oneshot::channel();
    *runtime.deferred_reload_reply.lock().unwrap() = Some(reply);
    *runtime.scheduled_respawn.lock().unwrap() = Some(PendingRespawn {
        deadline: Instant::now() + Duration::from_millis(20),
        kind: RespawnKind::Reload,
        daemon_initiated: false,
        approval: None,
    });
    let (reply, held) = oneshot::channel();
    let mut child = None;
    assert!(
        handle_supervisor_command(
            SupervisorCommand::ConfigExitHold { reply },
            &mut spec,
            &mut runtime,
            &supervisor.registry,
            &supervisor.process_liveness,
            &module.inner.snapshot,
            &mut child,
            &mut commands,
            &mut VecDeque::new()
        )
        .await
    );
    held.await.unwrap().unwrap();
    let reload_retained = matches!(
        runtime
            .scheduled_respawn
            .lock()
            .unwrap()
            .as_ref()
            .unwrap()
            .kind,
        RespawnKind::Reload
    );
    supervisor.start_monitor(spec, runtime, &module, None, commands);
    let answer = tokio::time::timeout(Duration::from_secs(3), result).await;
    module.set_enabled(false).await.unwrap();
    module.stop().await.unwrap();
    assert!(reload_retained, "a stale hold replaced Reload with Spawn");
    // The native fixture never registers, so completion is the normal explicit
    // registration failure. A plain Spawn would leave this caller unanswered.
    assert!(
        matches!(answer, Ok(Ok(Err(SuperviseError::ReloadFailed { ref reason, .. })))
        if reason.contains("new child did not register")),
        "the reload caller must receive its result: {answer:?}"
    );
}

#[tokio::test]
async fn stale_config_exit_hold_preserves_crash_backoff_deadline() {
    let (_scratch, owner, supervisor, _, spec) =
        crate::config_gate::runtime::RuntimeOwner::unactivated_supervisor_fixture_for_test();
    assert!(!owner.has_recorded_admission_hold("a"));
    let RegisteredConfiguredModule {
        mut spec,
        mut runtime,
        module,
        mut commands,
    } = supervisor
        .register_configured_with_health(
            spec,
            true,
            Default::default(),
            None,
            Default::default(),
            DEFAULT_REGISTER_TIMEOUT,
        )
        .unwrap();
    update_snapshot(&module.inner.snapshot, Some("a"), |state| {
        state.state = ModuleState::Restarting;
        state.respawn_pending = true;
    })
    .unwrap();
    let deadline = Instant::now() + Duration::from_secs(120);
    *runtime.scheduled_respawn.lock().unwrap() = Some(PendingRespawn {
        deadline,
        kind: RespawnKind::Spawn,
        daemon_initiated: true,
        approval: None,
    });
    let before = format!("{:?}", module.status().unwrap());
    let (reply, held) = oneshot::channel();
    assert!(
        handle_supervisor_command(
            SupervisorCommand::ConfigExitHold { reply },
            &mut spec,
            &mut runtime,
            &supervisor.registry,
            &supervisor.process_liveness,
            &module.inner.snapshot,
            &mut None,
            &mut commands,
            &mut VecDeque::new()
        )
        .await
    );
    held.await.unwrap().unwrap();
    let pending = runtime.scheduled_respawn.lock().unwrap().clone().unwrap();
    assert_eq!(
        pending.deadline, deadline,
        "stale holds must not shorten crash backoff"
    );
    assert!(pending.daemon_initiated);
    assert_eq!(format!("{:?}", module.status().unwrap()), before);
}

#[tokio::test]
async fn stale_config_exit_hold_preserves_previous_instance_wait() {
    let (_scratch, owner, supervisor, _, spec) =
        crate::config_gate::runtime::RuntimeOwner::unactivated_supervisor_fixture_for_test();
    assert!(!owner.has_recorded_admission_hold("a"));
    let RegisteredConfiguredModule {
        mut spec,
        mut runtime,
        module,
        mut commands,
    } = supervisor
        .register_configured_with_health(
            spec,
            true,
            Default::default(),
            None,
            Default::default(),
            DEFAULT_REGISTER_TIMEOUT,
        )
        .unwrap();
    update_snapshot(&module.inner.snapshot, Some("a"), |state| {
        state.waiting_for_previous_pid = Some(4242);
        state.waiting_for_previous_detail = Some("previous instance is still alive".into());
    })
    .unwrap();
    let before = format!("{:?}", module.status().unwrap());
    let (reply, held) = oneshot::channel();
    assert!(
        handle_supervisor_command(
            SupervisorCommand::ConfigExitHold { reply },
            &mut spec,
            &mut runtime,
            &supervisor.registry,
            &supervisor.process_liveness,
            &module.inner.snapshot,
            &mut None,
            &mut commands,
            &mut VecDeque::new()
        )
        .await
    );
    held.await.unwrap().unwrap();
    assert_eq!(
        module.status().unwrap().waiting_for_previous_pid,
        Some(4242)
    );
    assert_eq!(format!("{:?}", module.status().unwrap()), before);
    assert!(runtime.scheduled_respawn.lock().unwrap().is_none());
}

#[tokio::test]
async fn staged_unstamp_enable_preserves_a_live_child_instead_of_parking_it() {
    let (_scratch, owner, supervisor, _, spec) =
        crate::config_gate::runtime::RuntimeOwner::unactivated_supervisor_fixture_for_test();
    std::fs::write(&spec.args[1], b"keep running").unwrap();
    let RegisteredConfiguredModule {
        mut spec,
        mut runtime,
        module,
        mut commands,
    } = supervisor
        .register_configured_with_health(
            spec,
            true,
            Default::default(),
            None,
            Default::default(),
            DEFAULT_REGISTER_TIMEOUT,
        )
        .unwrap();
    let mut child = Some(
        spawn_and_mark_running_async(&spec, &runtime, &module.inner.snapshot)
            .await
            .unwrap(),
    );
    #[cfg(target_os = "macos")]
    child.as_mut().unwrap().confirm_privacy_exec().await;
    let child_spawned_at = child.as_ref().unwrap().spawned_at;
    let before = module.status().unwrap();
    // Model the desired-state update before reaping: the actor still owns a
    // live child even though its desired enabled flag has already been cleared.
    update_snapshot(&module.inner.snapshot, Some("a"), |state| {
        state.enabled = false
    })
    .unwrap();
    std::fs::write(
        spec.program
            .parent()
            .unwrap()
            .join("store")
            .join(crate::config_store::STORE_FILE),
        b"tampered",
    )
    .unwrap();
    assert!(owner.check_recovery().unwrap());
    let plan = owner
        .finish_plan(
            owner
                .prepare(crate::config_gate::PlanKind::Unstamp)
                .unwrap(),
            crate::config_gate::runtime::daemon_review,
        )
        .unwrap();
    let facts = crate::config_gate::ApprovalFacts {
        connection: 7,
        owner_uid: true,
        desktop_app: true,
        attested_operator_app: true,
        os_prompt_available: true,
        phone: None,
        caller: Default::default(),
    };
    owner
        .desktop_view_delivered(ConnectionId::new(7), &plan)
        .unwrap();
    let approval = owner.begin_approval(&plan.digest, &facts).unwrap();
    owner
        .finish_approval(
            approval.attempt(),
            &facts,
            crate::config_gate::ApprovalReply::Presence(
                crate::config_gate::PresenceDecision::InteractiveApproval,
            ),
            2,
        )
        .unwrap();
    assert!(owner.can_stage_unstamp_enable());
    assert!(owner.has_recorded_admission_hold("a"));
    let (tx, rx) = oneshot::channel();
    let command = SupervisorCommand::SetEnabled {
        enabled: true,
        reset_budget: true,
        requester: Some(Principal::Direct),
        reply: tx,
    };
    assert!(
        handle_supervisor_command(
            command,
            &mut spec,
            &mut runtime,
            &supervisor.registry,
            &supervisor.process_liveness,
            &module.inner.snapshot,
            &mut child,
            &mut commands,
            &mut VecDeque::new()
        )
        .await
    );
    assert!(rx.await.unwrap().unwrap());
    let staged = module.status().unwrap();
    let parked = lock_snapshot(&module.inner.snapshot)
        .unwrap()
        .config_hold_parked;
    let retained_child = child.as_ref().unwrap().spawned_at;
    let (tx, rx) = oneshot::channel();
    assert!(
        !handle_supervisor_command(
            SupervisorCommand::Drain {
                requester: None,
                reply: tx
            },
            &mut spec,
            &mut runtime,
            &supervisor.registry,
            &supervisor.process_liveness,
            &module.inner.snapshot,
            &mut child,
            &mut commands,
            &mut VecDeque::new()
        )
        .await
    );
    rx.await.unwrap().unwrap();
    assert!(staged.enabled && staged.process_alive);
    assert_eq!(staged.state, ModuleState::Running);
    assert_eq!(retained_child, child_spawned_at);
    assert_eq!(staged.spawn_generation, before.spawn_generation);
    assert!(
        !parked,
        "an actor with its live child is not parked for release"
    );
}

#[tokio::test]
async fn config_exit_hold_completes_behind_a_queued_config_restart_without_an_active_generation() {
    let (_scratch, owner, supervisor, handler, spec) =
        crate::config_gate::runtime::RuntimeOwner::unactivated_supervisor_fixture_for_test();
    let registered = supervisor
        .register_configured_with_health(
            spec,
            true,
            HealthConfig::default(),
            None,
            RestartPolicy::default(),
            DEFAULT_REGISTER_TIMEOUT,
        )
        .unwrap();
    let RegisteredConfiguredModule {
        mut spec,
        mut runtime,
        module,
        mut commands,
    } = registered;
    let spawn = owner
        .delivery_spawned_in_slot(
            &spec.module_id,
            Instant::now(),
            Duration::from_secs(5),
            Duration::from_secs(2),
            true,
        )
        .unwrap()
        .unwrap();
    owner.delivery_child_exited(&spawn).unwrap();
    let exited = owner.delivery_exits().unwrap().pop().unwrap();
    module.config_apply_restart(1).await.unwrap();

    // No task processes supervisor commands until the recorder has queued its
    // hold behind the restart. Waiting on the queue length fixes their order.
    let recording = tokio::spawn(async move { handler.record_config_exit_for_test(exited).await });
    tokio::time::timeout(Duration::from_secs(1), async {
        while commands.len() < 2 {
            tokio::task::yield_now().await;
        }
    })
    .await
    .unwrap();
    assert!(owner.delivery_exit_fence("a").unwrap().is_some());
    let mut child = None;
    let drive = async {
        for index in 0..2 {
            let command = commands.recv().await.unwrap();
            if index == 0 {
                assert!(matches!(
                    command,
                    SupervisorCommand::ConfigApplyRestart { .. }
                ));
            } else {
                assert!(matches!(command, SupervisorCommand::ConfigExitHold { .. }));
            }
            assert!(
                handle_supervisor_command(
                    command,
                    &mut spec,
                    &mut runtime,
                    &supervisor.registry,
                    &supervisor.process_liveness,
                    &module.inner.snapshot,
                    &mut child,
                    &mut commands,
                    &mut VecDeque::new(),
                )
                .await
            );
        }
    };
    tokio::time::timeout(Duration::from_secs(1), async {
        let (recorded, ()) = tokio::join!(recording, drive);
        recorded.unwrap();
    })
    .await
    .expect("an earlier restart must not deadlock the exit hold behind the module gate");
    assert!(owner.delivery_exit_fence("a").unwrap().is_none());
    let status = module.status().unwrap();
    assert_eq!(status.state, ModuleState::Stopped);
    assert!(!status.process_alive);
    assert!(child.is_none());
    assert_eq!(status.restart_count, 0);
    assert!(
        status.stop_reason.is_none(),
        "a successful first-generation hold is not an infrastructure failure"
    );
}

#[tokio::test]
async fn config_exit_record_failure_retries_then_retains_stopped_reason_activity_and_fence() {
    assert_exit_reconciliation_failure(true).await;
}

#[tokio::test]
async fn config_exit_hold_failure_retries_then_retains_stopped_reason_activity_and_fence() {
    assert_exit_reconciliation_failure(false).await;
}

async fn assert_exit_reconciliation_failure(tamper_store: bool) {
    let (_scratch, owner, supervisor, handler, spec) =
        crate::config_gate::runtime::RuntimeOwner::unactivated_supervisor_fixture_for_test();
    let root = spec.program.parent().unwrap().to_owned();
    let handler = match Arc::try_unwrap(handler) {
        Ok(handler) => Arc::new(handler.with_activity_ledger(&root, "exit-failure".into())),
        Err(_) => panic!("the unactivated fixture must not retain its handler"),
    };
    let RegisteredConfiguredModule {
        module, commands, ..
    } = supervisor
        .register_configured_with_health(
            spec,
            true,
            HealthConfig::default(),
            None,
            RestartPolicy::default(),
            DEFAULT_REGISTER_TIMEOUT,
        )
        .unwrap();
    // A real closed command channel makes the hold fail without a synthetic
    // supervisor error. The process has already exited; a restart was pending.
    drop(commands);
    update_snapshot(&module.inner.snapshot, Some("a"), |state| {
        state.state = ModuleState::Failed;
        state.respawn_pending = true;
    })
    .unwrap();
    let spawn = owner
        .delivery_spawned_in_slot(
            "a",
            Instant::now(),
            Duration::from_secs(5),
            Duration::from_secs(2),
            true,
        )
        .unwrap()
        .unwrap();
    owner.delivery_child_exited(&spawn).unwrap();
    let exited = owner.delivery_exits().unwrap().pop().unwrap();
    if tamper_store {
        // Corrupt the actual authenticated store, not a mock return value.
        std::fs::write(
            root.join("store").join(crate::config_store::STORE_FILE),
            b"{}",
        )
        .unwrap();
    }
    let retry_again = tokio::time::timeout(
        Duration::from_secs(2),
        handler.record_config_exit_for_test(exited),
    )
    .await
    .unwrap();
    assert_eq!(owner.exit_record_attempts_for_test(), 3);
    assert!(
        !retry_again,
        "terminal reconciliation must not begin a fresh retry cycle on notification"
    );
    let status = module.status().unwrap();
    let (code, kind) = if tamper_store {
        ("config_exit_record_failed", "store_tampered")
    } else {
        ("config_exit_hold_failed", "supervisor_command_closed")
    };
    assert_eq!(
        status.stop_reason,
        Some(ModuleStopReason {
            code: code.into(),
            error_kind: kind.into()
        })
    );
    assert_eq!(status.state, ModuleState::Stopped);
    assert!(!status.process_alive);
    assert!(
        !lock_snapshot(&module.inner.snapshot)
            .unwrap()
            .respawn_pending
    );
    assert_eq!(status.restart_count, 0);
    assert!(owner.delivery_exit_fence("a").unwrap().is_some());
    assert_eq!(supervisor.child_roster.live_count_for_test(), 0);
    let ledger = supervisor
        .supervisor_handle
        .as_ref()
        .unwrap()
        .activity()
        .unwrap();
    // A successful append is a FIFO writer barrier for the preceding terminal
    // lifecycle event. No sleeps or speculative file-read timing are needed.
    ledger
        .record(
            "barrier".into(),
            serde_json::from_value(serde_json::json!({
                "op":"activity.record", "kind":"barrier.flushed", "summary":"Queue flushed"
            }))
            .unwrap(),
            subc_control::ActivityActor {
                principal: subc_protocol::Principal::Direct,
                scope_owner: None,
                agent_id: None,
                run_id: None,
                flow_id: None,
                project_root: None,
                workdir: None,
                project_id: None,
            },
            1,
        )
        .await
        .unwrap();
    let entries = ledger
        .query(
            serde_json::from_value(serde_json::json!({
                "op":"activity.query", "limit":1000
            }))
            .unwrap(),
        )
        .await
        .unwrap()
        .entries;
    let event = entries
        .iter()
        .find(|entry| entry.kind == "config.exit_failure")
        .expect("the terminal stopped reason must reach the actual activity log");
    let activity = serde_json::to_value(event).unwrap();
    assert_eq!(activity["subject"]["id"], "a");
    assert_eq!(activity["details"]["reason"], code);
    assert_eq!(activity["details"]["error_kind"], kind);
}

#[tokio::test]
async fn operator_enable_park_error_does_not_inherit_daemon_retry_attribution() {
    let (_scratch, owner, supervisor, _, spec) =
        crate::config_gate::runtime::RuntimeOwner::unactivated_supervisor_fixture_for_test();
    let RegisteredConfiguredModule {
        spec,
        mut runtime,
        module,
        commands,
    } = supervisor
        .register_configured_with_health(
            spec,
            false,
            Default::default(),
            None,
            Default::default(),
            DEFAULT_REGISTER_TIMEOUT,
        )
        .unwrap();
    runtime.recovery_is_daemon = true;
    std::fs::write(
        spec.program
            .parent()
            .unwrap()
            .join("store")
            .join(crate::config_store::STORE_FILE),
        b"tampered",
    )
    .unwrap();
    assert!(
        owner.check_module_hold("a").is_ok(),
        "the cached projection must admit the operator's preflight"
    );
    owner.poison_config_owner_at_park_for_test();
    supervisor.start_monitor(spec, runtime, &module, None, commands);
    let error = module.set_enabled(true).await.unwrap_err();
    assert!(
        error
            .to_string()
            .contains("configuration owner is poisoned"),
        "{error}"
    );
    // The reply can precede the monitor's next loop iteration. This command must
    // cross that loop before the snapshot and retry deadline are sampled.
    module.config_exit_hold().await.unwrap();
    let status = module.status().unwrap();
    let deadline = module.retry_deadline_for_test();
    module.stop().await.unwrap();
    assert_eq!(status.state, ModuleState::Failed);
    assert!(!status.process_alive);
    assert_eq!(
        deadline, None,
        "a failed operator enable cannot arm the stale daemon work's retry"
    );
}

#[tokio::test]
async fn staged_unstamp_park_error_uses_the_operator_attempts_attribution() {
    let (_scratch, owner, supervisor, _, spec) =
        crate::config_gate::runtime::RuntimeOwner::unactivated_supervisor_fixture_for_test();
    let RegisteredConfiguredModule {
        spec,
        mut runtime,
        module,
        commands,
    } = supervisor
        .register_configured_with_health(
            spec,
            false,
            Default::default(),
            None,
            Default::default(),
            DEFAULT_REGISTER_TIMEOUT,
        )
        .unwrap();
    runtime.recovery_is_daemon = true;
    std::fs::write(
        spec.program
            .parent()
            .unwrap()
            .join("store")
            .join(crate::config_store::STORE_FILE),
        b"tampered",
    )
    .unwrap();
    assert!(owner.check_recovery().unwrap());
    let plan = owner
        .finish_plan(
            owner
                .prepare(crate::config_gate::PlanKind::Unstamp)
                .unwrap(),
            crate::config_gate::runtime::daemon_review,
        )
        .unwrap();
    let facts = crate::config_gate::ApprovalFacts {
        connection: 7,
        owner_uid: true,
        desktop_app: true,
        attested_operator_app: true,
        os_prompt_available: true,
        phone: None,
        caller: Default::default(),
    };
    owner
        .desktop_view_delivered(ConnectionId::new(7), &plan)
        .unwrap();
    let approval = owner.begin_approval(&plan.digest, &facts).unwrap();
    owner
        .finish_approval(
            approval.attempt(),
            &facts,
            crate::config_gate::ApprovalReply::Presence(
                crate::config_gate::PresenceDecision::InteractiveApproval,
            ),
            2,
        )
        .unwrap();
    assert!(owner.can_stage_unstamp_enable());
    assert!(owner.has_recorded_admission_hold("a"));
    owner.poison_config_owner_at_park_for_test();
    supervisor.start_monitor(spec, runtime, &module, None, commands);
    let error = module.set_enabled(true).await.unwrap_err();
    assert!(
        error
            .to_string()
            .contains("configuration owner is poisoned"),
        "{error}"
    );
    module.config_exit_hold().await.unwrap();
    let status = module.status().unwrap();
    let deadline = module.retry_deadline_for_test();
    module.stop().await.unwrap();
    assert!(status.enabled);
    assert_eq!(status.state, ModuleState::Failed);
    assert!(!status.process_alive);
    assert_eq!(
        deadline, None,
        "a staged operator enable is not daemon work"
    );
}

#[tokio::test]
async fn disabled_config_spawn_hold_leaves_snapshot_and_pending_work_unchanged() {
    let (_scratch, _, supervisor, _, spec) =
        crate::config_gate::runtime::RuntimeOwner::unactivated_supervisor_fixture_for_test();
    let RegisteredConfiguredModule {
        spec,
        runtime,
        module,
        ..
    } = supervisor
        .register_configured_with_health(
            spec,
            false,
            Default::default(),
            None,
            Default::default(),
            DEFAULT_REGISTER_TIMEOUT,
        )
        .unwrap();
    let before = module.inner.snapshot.lock().unwrap().clone();
    hold_config_spawn(
        &spec,
        &runtime,
        &module.inner.snapshot,
        &runtime.failure_context(true),
    )
    .unwrap();
    assert_eq!(
        *module.inner.snapshot.lock().unwrap(),
        before,
        "a disabled module has no consumed start attempt to defer"
    );
    assert!(runtime.scheduled_respawn.lock().unwrap().is_none());
    assert!(runtime.deferred_reload_reply.lock().unwrap().is_none());
}