rust-tokio-supervisor 0.1.3

A Rust tokio supervisor with declarative task supervision, restart policy, shutdown coordination, and observability.
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
//! Integration tests for generation fencing.

use rust_supervisor::child_runner::run_exit::TaskExit;
use rust_supervisor::child_runner::runner::ChildRunReport;
use rust_supervisor::control::command::{CommandResult, CurrentState};
use rust_supervisor::control::outcome::{
    ChildAttemptStatus, ChildControlOperation, ChildControlResult, ChildLivenessState,
    ChildStopState, GenerationFenceDecision, RestartLimitState,
};
use rust_supervisor::dashboard::model::dashboard_command_result_value;
use rust_supervisor::error::types::{TaskFailure, TaskFailureKind};
use rust_supervisor::event::payload::What;
use rust_supervisor::id::types::{ChildId, ChildStartCount, Generation, SupervisorPath};
use rust_supervisor::readiness::signal::ReadinessState;
use rust_supervisor::registry::entry::{ChildRuntime, ChildRuntimeStatus};
use rust_supervisor::runtime::supervisor::Supervisor;
use rust_supervisor::spec::child::{ChildSpec, TaskKind};
use rust_supervisor::spec::supervisor::{SupervisionStrategy, SupervisorSpec};
use rust_supervisor::task::context::TaskContext;
use rust_supervisor::task::factory::{TaskFactory, TaskResult, service_fn};
use rust_supervisor::test_support::test_time::{advance_test_clock, with_auto_clock_drive};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use tokio::sync::mpsc;

/// Creates a worker child from a task factory.
fn worker_child(name: &'static str, factory: impl TaskFactory) -> ChildSpec {
    ChildSpec::worker(
        ChildId::new(name),
        name,
        TaskKind::AsyncWorker,
        Arc::new(factory),
    )
}

/// Parses a [`CurrentState`] from a supervisor command envelope.
fn expect_current_state(result: CommandResult) -> CurrentState {
    match result {
        CommandResult::CurrentState { state } => state,
        other => panic!("expected CurrentState command result but got {other:?}"),
    }
}

/// Smoke test: only checks that this test target is registered and runs.
#[test]
fn supervisor_generation_fencing_smoke() {
    assert_eq!(Generation::initial().value, 0);
}

/// Dashboard `child_control` projection must include a nullable `generation_fence` key.
#[test]
fn generation_fence_optional_field_present_in_dashboard_child_control_projection_test() {
    let outcome = ChildControlResult::new(
        ChildId::new("demo_child"),
        Some(ChildStartCount::first()),
        Some(Generation::initial()),
        ChildControlOperation::Active,
        ChildControlOperation::Active,
        Some(ChildAttemptStatus::Running),
        false,
        ChildStopState::Idle,
        RestartLimitState::new(Duration::from_secs(60), 10, 0, 1),
        ChildLivenessState::new(Some(10), false, ReadinessState::Unreported),
        false,
        None,
        None,
    );
    let wrapped = CommandResult::ChildControl { outcome };
    let value = dashboard_command_result_value(&wrapped).expect("dashboard serialization");
    assert!(value["outcome"].get("generation_fence").is_some());
    assert_eq!(
        value["outcome"]["generation_fence"],
        serde_json::Value::Null
    );
}

/// Verifies restart delivers cancellation, fences the attempt, and keeps a single active handle.
#[tokio::test(start_paused = true)]
async fn restart_child_sends_cancel_before_second_spawn_test() {
    let child_id = ChildId::new("worker");
    let start_counter_inner = Arc::new(AtomicUsize::new(0));
    let start_counter_body = start_counter_inner.clone();

    let (boot_tx, mut boot_rx) = mpsc::channel(1);
    let (cancel_ack_tx, mut cancel_ack_rx) = mpsc::channel(1);
    let (second_boot_tx, mut second_boot_rx) = mpsc::channel(1);

    let boot_clone = boot_tx.clone();
    let cancel_clone = cancel_ack_tx.clone();
    let second_clone = second_boot_tx.clone();

    let spec = SupervisorSpec::root(vec![worker_child(
        "worker",
        service_fn(move |ctx: TaskContext| {
            let boot_clone = boot_clone.clone();
            let cancel_clone = cancel_clone.clone();
            let second_clone = second_clone.clone();
            let start_counter_body = start_counter_body.clone();
            async move {
                let invocation = start_counter_body.fetch_add(1, Ordering::SeqCst);
                if invocation == 0 {
                    let _ignored = boot_clone.send(()).await;
                    ctx.cancellation_token().cancelled().await;
                    let _ignored = cancel_clone.send(()).await;
                    tokio::time::sleep(Duration::from_millis(400)).await;
                    TaskResult::Cancelled
                } else {
                    let _ignored = second_clone.send(()).await;
                    ctx.cancellation_token().cancelled().await;
                    TaskResult::Cancelled
                }
            }
        }),
    )]);
    let handle = Supervisor::start(spec).await.expect("start supervisor");
    boot_rx.recv().await.expect("child should boot");

    let restart = handle
        .restart_child(child_id.clone(), "operator", "fence-delivers-cancel")
        .await
        .expect("restart command");
    let outcome = match restart {
        CommandResult::ChildControl { outcome } => outcome,
        other => panic!("unexpected restart result: {other:?}"),
    };
    assert_eq!(
        outcome
            .generation_fence
            .as_ref()
            .expect("fence payload")
            .decision,
        GenerationFenceDecision::QueuedAfterStop
    );
    assert!(outcome.cancel_delivered);

    cancel_ack_rx
        .recv()
        .await
        .expect("cancellation must reach the supervised task");

    let recorder = handle.observability_recorder();
    assert!(recorder.events.iter().any(|event| matches!(
        &event.what,
        What::ChildRestartFenceEntered { child_id: id, .. } if *id == child_id
    )));
    assert!(recorder.events.iter().any(|event| matches!(
        &event.what,
        What::ChildControlCancelDelivered { child_id: id, .. } if *id == child_id
    )));

    let current = expect_current_state(handle.current_state().await.expect("state"));
    let worker = current
        .child_runtime_records
        .iter()
        .find(|record| record.child_id == child_id)
        .expect("worker projection");
    assert_eq!(
        worker.generation.map(|generation| generation.value),
        Some(0)
    );
    assert_eq!(worker.attempt.map(|attempt| attempt.value), Some(1));
    assert_eq!(worker.status, Some(ChildAttemptStatus::Cancelling));

    advance_test_clock(Duration::from_millis(400)).await;
    second_boot_rx
        .recv()
        .await
        .expect("second supervised attempt boots after draining the fenced stop");

    let refreshed =
        expect_current_state(handle.current_state().await.expect("state after respawn"));
    let worker_state = refreshed
        .child_runtime_records
        .iter()
        .find(|record| record.child_id == child_id)
        .expect("worker record after respawn");
    assert_eq!(
        worker_state.generation.map(|generation| generation.value),
        Some(1_u64),
        "{worker_state:?}"
    );

    let _ignored = with_auto_clock_drive(async {
        handle
            .shutdown_tree("operator", "finish fenced restart test")
            .await
    })
    .await;
}

/// Verifies manual restart waits for graceful stop semantics and cites the old identities.
#[tokio::test(start_paused = true)]
async fn restart_child_queues_after_stop_decision_test() {
    let child_id = ChildId::new("worker");
    let baseline = Generation::initial();
    let baseline_attempt = ChildStartCount::first();
    let (started_tx, mut started_rx) = mpsc::channel(1);
    let spec = SupervisorSpec::root(vec![worker_child(
        "worker",
        service_fn(move |ctx: TaskContext| {
            let started_tx = started_tx.clone();
            async move {
                let _ignored = started_tx.send(()).await;
                ctx.cancellation_token().cancelled().await;
                TaskResult::Cancelled
            }
        }),
    )]);
    let handle = Supervisor::start(spec).await.expect("start supervisor");
    started_rx.recv().await.expect("child starts");

    let restart = handle
        .restart_child(child_id.clone(), "operator", "queued restart")
        .await
        .expect("restart command");
    let outcome = match restart {
        CommandResult::ChildControl { outcome } => outcome,
        other => panic!("unexpected restart outcome: {other:?}"),
    };
    assert_eq!(
        outcome
            .generation_fence
            .as_ref()
            .expect("fence payload present")
            .decision,
        GenerationFenceDecision::QueuedAfterStop
    );
    assert_eq!(outcome.generation, Some(baseline));
    assert_eq!(outcome.attempt, Some(baseline_attempt));
    assert_eq!(
        outcome
            .generation_fence
            .as_ref()
            .expect("fence payload holds target generation")
            .target_generation,
        Some(Generation { value: 1 })
    );

    let _ignored = with_auto_clock_drive(async {
        handle
            .shutdown_tree("operator", "finish queued restart test")
            .await
    })
    .await;
}

/// Verifies restart during shutdown returns a deterministic blocked outcome without spawning anew.
#[tokio::test(start_paused = true)]
async fn restart_child_blocked_during_tree_shutdown_test() {
    let child_id = ChildId::new("worker");
    let (started_tx, mut started_rx) = mpsc::channel(1);
    let spec = SupervisorSpec::root(vec![worker_child(
        "worker",
        service_fn(move |ctx: TaskContext| {
            let started_tx = started_tx.clone();
            async move {
                let _ignored = started_tx.send(()).await;
                ctx.cancellation_token().cancelled().await;
                TaskResult::Cancelled
            }
        }),
    )]);
    let handle = Supervisor::start(spec).await.expect("start supervisor");
    started_rx.recv().await.expect("child starts");

    with_auto_clock_drive(async {
        handle
            .shutdown_tree("operator", "blocking restart test")
            .await
    })
    .await
    .expect("shutdown");
    let blocked = handle
        .restart_child(
            child_id.clone(),
            "operator",
            "restart while shutdown completes",
        )
        .await
        .expect("restart should still dispatch");
    let outcome = match blocked {
        CommandResult::ChildControl { outcome } => outcome,
        other => panic!("unexpected blocked restart result {other:?}"),
    };
    assert_eq!(
        outcome
            .generation_fence
            .as_ref()
            .expect("generation fence populated")
            .decision,
        GenerationFenceDecision::BlockedByShutdown
    );

    let recorder_after = handle.observability_recorder();
    assert!(!recorder_after.events.iter().any(|event| matches!(
        &event.what,
        What::ChildRestartFenceEntered { child_id: id, .. } if *id == child_id
    )));
}

/// Validates spawn failures after fencing retain the earlier exit verdict and expose the error.
#[tokio::test(start_paused = true)]
async fn pending_restart_target_spawn_failure_retains_prior_outcomes_test() {
    const SPAWN_HOOK_CASE_CHILD: &str = "worker_spawn_hook_fence_case";
    let child_id = ChildId::new(SPAWN_HOOK_CASE_CHILD);
    let (boot_tx, mut boot_rx) = mpsc::channel(1);
    let (cancel_ack_tx, mut cancel_ack_rx) = mpsc::channel(1);
    let boot_body = boot_tx.clone();
    let cancel_body = cancel_ack_tx.clone();

    let spec = SupervisorSpec::root(vec![worker_child(
        SPAWN_HOOK_CASE_CHILD,
        service_fn(move |ctx: TaskContext| {
            let boot_body = boot_body.clone();
            let cancel_body = cancel_body.clone();
            async move {
                let _ignored = boot_body.send(()).await;
                ctx.cancellation_token().cancelled().await;
                let _ignored = cancel_body.send(()).await;
                tokio::time::sleep(Duration::from_millis(300)).await;
                TaskResult::Cancelled
            }
        }),
    )]);
    let handle = Supervisor::start(spec).await.expect("start supervisor");
    boot_rx.recv().await.expect("child starts");

    handle
        .restart_child(child_id.clone(), "operator", "fence for spawn hook")
        .await
        .expect("restart fences old attempt");

    cancel_ack_rx
        .recv()
        .await
        .expect("supervised task observes cancel before we arm the spawn hook");

    rust_supervisor::test_support::child_spawn::fail_next_child_spawns_for(child_id.clone(), 1);

    let mut stop_completed_seen = false;
    let mut retained_failure_observed = false;
    let mut recorder_capture = handle.observability_recorder();

    for _ in 0..400 {
        advance_test_clock(Duration::from_millis(10)).await;
        // Call current_state first to drive the control loop through any
        // pending child exit messages before reading the recorder.
        let current = expect_current_state(handle.current_state().await.expect("state"));
        recorder_capture = handle.observability_recorder();
        stop_completed_seen |= recorder_capture.events.iter().any(|event| {
            matches!(
                &event.what,
                What::ChildControlStopCompleted { exit_kind, .. }
                    if matches!(exit_kind, TaskExit::Cancelled)
            )
        });

        let Some(worker) = current
            .child_runtime_records
            .iter()
            .find(|record| record.child_id == child_id)
        else {
            continue;
        };
        if let Some(failure) = &worker.failure {
            retained_failure_observed = true;
            assert!(
                failure.reason.contains("test hook"),
                "unexpected diagnostic: {}",
                failure.reason
            );
            assert_eq!(worker.generation, Some(Generation::initial()), "{worker:?}");
            assert_eq!(worker.attempt, Some(ChildStartCount::first()), "{worker:?}");
            break;
        }
    }

    assert!(
        stop_completed_seen,
        "observer should record the graceful cancel exit before failing the next spawn"
    );
    assert!(
        retained_failure_observed,
        "runtime projection should expose the spawn failure alongside the preceding exit verdict"
    );
    assert!(
        !recorder_capture
            .events
            .iter()
            .any(|event| matches!(&event.what, What::ChildRestartFenceReleased { .. }))
    );

    let _ignored = with_auto_clock_drive(async {
        handle
            .shutdown_tree("operator", "finish spawn hook test")
            .await
    })
    .await;
}

/// Verifies duplicate restart commands collapse into [`GenerationFenceDecision::AlreadyPending`].
#[tokio::test(start_paused = true)]
async fn duplicate_restart_child_merges_to_already_pending_test() {
    let child_id = ChildId::new("worker_dup_fence");
    let (boot_tx, mut boot_rx) = mpsc::channel(1);
    let boot_clone = boot_tx.clone();

    let spec = SupervisorSpec::root(vec![worker_child(
        "worker_dup_fence",
        service_fn(move |ctx: TaskContext| {
            let boot_clone = boot_clone.clone();
            async move {
                let _ignored = boot_clone.send(()).await;
                ctx.cancellation_token().cancelled().await;
                tokio::time::sleep(Duration::from_millis(400)).await;
                TaskResult::Cancelled
            }
        }),
    )]);
    let handle = Supervisor::start(spec).await.expect("start supervisor");
    boot_rx.recv().await.expect("child boots");

    let first = handle
        .restart_child(child_id.clone(), "operator", "dup fence first")
        .await
        .expect("first restart");
    let first_outcome = match first {
        CommandResult::ChildControl { outcome } => outcome,
        other => panic!("unexpected first restart result {other:?}"),
    };
    assert_eq!(
        first_outcome
            .generation_fence
            .as_ref()
            .expect("fence payload")
            .decision,
        GenerationFenceDecision::QueuedAfterStop
    );

    let second = handle
        .restart_child(child_id.clone(), "operator", "dup fence second")
        .await
        .expect("second restart");
    let second_outcome = match second {
        CommandResult::ChildControl { outcome } => outcome,
        other => panic!("unexpected second restart result {other:?}"),
    };
    assert_eq!(
        second_outcome
            .generation_fence
            .as_ref()
            .expect("fence payload")
            .decision,
        GenerationFenceDecision::AlreadyPending
    );
    assert!(!second_outcome.cancel_delivered);

    let recorder = handle.observability_recorder();
    assert!(recorder.events.iter().any(|event| matches!(
        &event.what,
        What::ChildRestartConflict {
            decision,
            child_id: id,
            ..
        } if *id == child_id && decision == "already_pending"
    )));

    let _ignored = with_auto_clock_drive(async {
        handle
            .shutdown_tree("operator", "finish dup fence test")
            .await
    })
    .await;
}

/// Verifies automatic restart scope respects manual pending restart gates without duplicate spawns.
#[tokio::test(start_paused = true)]
async fn auto_restart_and_manual_restart_share_fence_gate_test() {
    let beta_id = ChildId::new("scope_beta");
    let beta_invocations = Arc::new(AtomicUsize::new(0));
    let beta_invocations_body = beta_invocations.clone();

    let mut spec = SupervisorSpec::root(vec![
        worker_child(
            "scope_alpha",
            service_fn(move |_ctx: TaskContext| async move {
                tokio::time::sleep(Duration::from_millis(200)).await;
                TaskResult::Failed(TaskFailure::new(
                    TaskFailureKind::Error,
                    "test",
                    "scope alpha triggers restart plan",
                ))
            }),
        ),
        worker_child(
            "scope_beta",
            service_fn(move |ctx: TaskContext| {
                let beta_invocations_body = beta_invocations_body.clone();
                async move {
                    beta_invocations_body.fetch_add(1, Ordering::SeqCst);
                    ctx.cancellation_token().cancelled().await;
                    tokio::time::sleep(Duration::from_millis(400)).await;
                    TaskResult::Cancelled
                }
            }),
        ),
    ]);
    spec.strategy = SupervisionStrategy::OneForAll;

    let handle = Supervisor::start(spec).await.expect("start supervisor");
    advance_test_clock(Duration::from_millis(40)).await;
    assert_eq!(
        beta_invocations.load(Ordering::SeqCst),
        1,
        "beta should boot exactly once before fencing"
    );

    handle
        .restart_child(
            beta_id.clone(),
            "operator",
            "manual fence blocks automatic restart",
        )
        .await
        .expect("restart beta");

    for _step_millis in 0..250 {
        advance_test_clock(Duration::from_millis(1)).await;
        tokio::task::yield_now().await;
    }

    assert_eq!(
        beta_invocations.load(Ordering::SeqCst),
        1,
        "automatic restart path must not spawn a second beta attempt while fencing waits"
    );

    let recorder = handle.observability_recorder();
    let restart_conflicts_for_debug = recorder
        .events
        .iter()
        .filter_map(|event| match &event.what {
            What::ChildRestartConflict {
                child_id,
                command_id,
                decision,
                reason,
                ..
            } => Some((
                child_id.clone(),
                command_id.clone(),
                decision.clone(),
                reason.clone(),
            )),
            _ => None,
        })
        .collect::<Vec<_>>();
    assert!(
        recorder.events.iter().any(|event| matches!(
            &event.what,
            What::ChildRestartConflict {
                child_id: id,
                command_id,
                ..
            } if *id == beta_id && command_id == "runtime-policy"
        )),
        "expected runtime-policy automatic restart suppression on {:?}; conflicts observed {:?}",
        beta_id,
        restart_conflicts_for_debug
    );
    let _ignored = with_auto_clock_drive(async {
        handle
            .shutdown_tree("operator", "finish scope fence test")
            .await
    })
    .await;
}

/// Verifies stale completion triples publish observability facts without overwriting generation truth.
#[tokio::test(start_paused = true)]
async fn stale_exit_report_never_overwrites_current_attempt_test() {
    const STALE_CHILD: &str = "stale_fence_worker";
    let child_id = ChildId::new(STALE_CHILD);
    let start_counter_inner = Arc::new(AtomicUsize::new(0));
    let start_counter_body = start_counter_inner.clone();

    let (boot_tx, mut boot_rx) = mpsc::channel(1);
    let (cancel_ack_tx, mut cancel_ack_rx) = mpsc::channel(1);
    let (second_boot_tx, mut second_boot_rx) = mpsc::channel(1);

    let boot_clone = boot_tx.clone();
    let cancel_clone = cancel_ack_tx.clone();
    let second_clone = second_boot_tx.clone();

    let spec = SupervisorSpec::root(vec![worker_child(
        STALE_CHILD,
        service_fn(move |ctx: TaskContext| {
            let boot_clone = boot_clone.clone();
            let cancel_clone = cancel_clone.clone();
            let second_clone = second_clone.clone();
            let start_counter_body = start_counter_body.clone();
            async move {
                let invocation = start_counter_body.fetch_add(1, Ordering::SeqCst);
                if invocation == 0 {
                    let _ignored = boot_clone.send(()).await;
                    ctx.cancellation_token().cancelled().await;
                    let _ignored = cancel_clone.send(()).await;
                    tokio::time::sleep(Duration::from_millis(350)).await;
                    TaskResult::Cancelled
                } else {
                    let _ignored = second_clone.send(()).await;
                    ctx.cancellation_token().cancelled().await;
                    TaskResult::Cancelled
                }
            }
        }),
    )]);
    let handle = Supervisor::start(spec).await.expect("start supervisor");
    boot_rx.recv().await.expect("child should boot");

    handle
        .restart_child(child_id.clone(), "operator", "stale replay fence")
        .await
        .expect("restart fences old attempt");

    cancel_ack_rx
        .recv()
        .await
        .expect("supervised task observes cancel");

    advance_test_clock(Duration::from_millis(350)).await;
    second_boot_rx
        .recv()
        .await
        .expect("target generation starts");

    let stale_leaf = worker_child(
        STALE_CHILD,
        service_fn(|_ctx: TaskContext| async { TaskResult::Succeeded }),
    );
    let mut stale_runtime = ChildRuntime::new(stale_leaf, SupervisorPath::root().join(STALE_CHILD));
    stale_runtime.generation = Generation::initial();
    stale_runtime.child_start_count = ChildStartCount::first();
    stale_runtime.status = ChildRuntimeStatus::Exited;

    let stale_report = ChildRunReport {
        runtime: stale_runtime,
        exit: TaskExit::Succeeded,
        became_ready: false,
    };

    handle
        .generation_fencing_replay_child_exit_for_test(stale_report)
        .await
        .expect("replay stale completion");

    advance_test_clock(Duration::from_millis(80)).await;

    let current = expect_current_state(handle.current_state().await.expect("state"));
    let worker = current
        .child_runtime_records
        .iter()
        .find(|record| record.child_id == child_id)
        .expect("worker projection");
    assert_eq!(
        worker.generation.map(|generation| generation.value),
        Some(1_u64),
        "{worker:?}"
    );

    let recorder = handle.observability_recorder();
    assert!(recorder.events.iter().any(|event| matches!(
        &event.what,
        What::ChildAttemptStaleReport {
            child_id: id,
            ..
        } if *id == child_id
    )));

    let _ignored = with_auto_clock_drive(async {
        handle
            .shutdown_tree("operator", "finish stale replay test")
            .await
    })
    .await;
}