aft-base 0.67.1

Shared foundation for Agent Foundational Toolkit
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
//! Best-effort attribution of agent shell roots over the supervised connection.

use std::collections::{BTreeSet, VecDeque};
use std::future::Future;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;

use subc_client_rs::{AgentCommandFinished, AgentCommandRoot, AgentCommandsError};
use tokio::sync::Notify;
use tokio_util::sync::CancellationToken;

#[derive(Clone, Debug, Default)]
pub struct Batch {
    pub started: Vec<AgentCommandRoot>,
    pub finished: Vec<AgentCommandFinished>,
    pub known: Option<Vec<AgentCommandRoot>>,
    through: u64,
}

enum Event {
    Started(AgentCommandRoot),
    Finished(AgentCommandFinished),
    Restored,
}

#[derive(Default)]
struct State {
    live: BTreeSet<AgentCommandRoot>,
    adopted: BTreeSet<AgentCommandRoot>,
    events: VecDeque<(u64, Event)>,
    sequence: u64,
    connection: u64,
    cancel: Option<CancellationToken>,
    storage: Option<PathBuf>,
    unsupported: bool,
}

#[derive(Default)]
struct Declarations {
    state: Mutex<State>,
    changed: Notify,
    persisting: tokio::sync::Mutex<()>,
}

impl State {
    fn push(&mut self, event: Event) {
        self.sequence += 1;
        if !self.unsupported {
            self.events.push_back((self.sequence, event));
        }
    }
}

impl Declarations {
    fn load(&self, storage: &Path) {
        let path = storage.join("agent-command-roots.json");
        let roots = std::fs::read(&path)
            .ok()
            .and_then(|bytes| serde_json::from_slice::<Vec<AgentCommandRoot>>(&bytes).ok())
            .unwrap_or_default();
        self.state.lock().unwrap_or_else(|e| e.into_inner()).storage = Some(path);
        for root in roots {
            if subc_os::process_start_time_ms(root.pid) == Some(root.start_time_ms) {
                self.restored(root);
            }
        }
    }

    fn started(&self, root: AgentCommandRoot) {
        let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
        state.live.insert(root);
        state.adopted.remove(&root);
        state.push(Event::Started(root));
        drop(state);
        self.changed.notify_one();
    }

    fn restored(&self, root: AgentCommandRoot) {
        let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
        if !state.live.insert(root) {
            return;
        }
        state.adopted.insert(root);
        state.push(Event::Restored);
        drop(state);
        self.changed.notify_one();
    }

    fn finished(&self, root: AgentCommandRoot, cpu_ms: Option<u64>) {
        let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
        if !state.live.remove(&root) {
            return;
        }
        state.adopted.remove(&root);
        state.push(Event::Finished(AgentCommandFinished {
            pid: root.pid,
            start_time_ms: root.start_time_ms,
            cpu_ms,
        }));
        drop(state);
        self.changed.notify_one();
    }

    fn batch(&self, initial: bool) -> Batch {
        let state = self.state.lock().unwrap_or_else(|e| e.into_inner());
        let mut batch = Batch {
            through: state.sequence,
            ..Batch::default()
        };
        let mut known = initial;
        for (_, event) in &state.events {
            match event {
                Event::Started(root) => batch.started.push(*root),
                Event::Finished(root) => batch.finished.push(*root),
                Event::Restored => known = true,
            }
        }
        if known {
            batch.known = Some(state.live.iter().copied().collect());
        }
        batch
    }

    fn connect<F, Fut>(self: &Arc<Self>, send: F) -> Connection
    where
        F: Fn(Batch) -> Fut + Send + 'static,
        Fut: Future<Output = Result<(), AgentCommandsError>> + Send,
    {
        let cancel = CancellationToken::new();
        let generation = {
            let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
            if let Some(old) = state.cancel.replace(cancel.clone()) {
                old.cancel();
            }
            state.connection += 1;
            state.unsupported = false;
            state.connection
        };
        let declarations = self.clone();
        let stopped = cancel.clone();
        let task = tokio::spawn(async move {
            declarations.drive(generation, stopped, send).await;
        });
        Connection {
            cancel,
            task: Some(task),
        }
    }

    async fn drive<F, Fut>(&self, generation: u64, cancel: CancellationToken, send: F)
    where
        F: Fn(Batch) -> Fut,
        Fut: Future<Output = Result<(), AgentCommandsError>>,
    {
        let mut initial = true;
        loop {
            self.prune_exited();
            let changed = self.changed.notified();
            let batch = self.batch(initial);
            if !initial
                && batch.started.is_empty()
                && batch.finished.is_empty()
                && batch.known.is_none()
            {
                tokio::select! {
                    _ = cancel.cancelled() => return,
                    _ = changed => continue,
                    _ = tokio::time::sleep(Duration::from_secs(1)) => continue,
                }
            }
            self.persist().await;
            let mut backoff = Duration::from_millis(50);
            loop {
                let result = tokio::select! {
                    biased;
                    _ = cancel.cancelled() => return,
                    result = send(batch.clone()) => result,
                };
                match result {
                    Ok(()) => break,
                    Err(AgentCommandsError::Unsupported) => {
                        static LOGGED: OnceLock<()> = OnceLock::new();
                        LOGGED.get_or_init(|| {
                            crate::slog_debug!("daemon does not support agent command declarations")
                        });
                        let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
                        state.unsupported = true;
                        state.events.clear();
                        return;
                    }
                    Err(error) if error.is_retryable() => {
                        tokio::select! {
                            _ = cancel.cancelled() => return,
                            _ = tokio::time::sleep(backoff) => {}
                        }
                        backoff = (backoff * 2).min(Duration::from_secs(2));
                    }
                    Err(error) => {
                        crate::slog_warn!("agent command declaration refused: {error}");
                        break;
                    }
                }
            }
            let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
            if state.connection != generation {
                return;
            }
            state
                .events
                .retain(|(sequence, _)| *sequence > batch.through);
            initial = false;
        }
    }

    fn prune_exited(&self) {
        // Only adopted roots need liveness polling. An owned child's waiter must
        // publish its CPU before a missing process-table entry can erase it.
        let roots: Vec<_> = self
            .state
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .adopted
            .iter()
            .copied()
            .collect();
        for root in roots {
            if subc_os::process_start_time_ms(root.pid) != Some(root.start_time_ms) {
                self.finished(root, None);
            }
        }
    }

    async fn persist(&self) {
        // A replaced connection can still be finishing an atomic write. Serialize
        // snapshots and writes so it cannot overwrite the new connection's roots.
        let _order = self.persisting.lock().await;
        let (path, roots) = {
            let state = self.state.lock().unwrap_or_else(|e| e.into_inner());
            (
                state.storage.clone(),
                state.live.iter().copied().collect::<Vec<_>>(),
            )
        };
        if let Some(path) = path {
            let result = tokio::task::spawn_blocking(move || -> std::io::Result<()> {
                if let Some(parent) = path.parent() {
                    std::fs::create_dir_all(parent)?;
                }
                let bytes = serde_json::to_vec(&roots)?;
                crate::durability::atomic_write(
                    &path,
                    &bytes,
                    crate::durability::AtomicWriteDurability::FileAndDirectory,
                )
            })
            .await;
            if !matches!(result, Ok(Ok(()))) {
                crate::slog_warn!("failed to retain live agent command identities: {result:?}");
            }
        }
    }
}

fn declarations() -> &'static Arc<Declarations> {
    static DECLARATIONS: OnceLock<Arc<Declarations>> = OnceLock::new();
    DECLARATIONS.get_or_init(|| Arc::new(Declarations::default()))
}

/// Dropping the connection stops retries before another supervised launch takes over.
pub struct Connection {
    cancel: CancellationToken,
    task: Option<tokio::task::JoinHandle<()>>,
}

impl Connection {
    pub async fn stop(mut self) {
        self.cancel.cancel();
        if let Some(task) = self.task.take() {
            let _ = task.await;
        }
    }
}

impl Drop for Connection {
    fn drop(&mut self) {
        self.cancel.cancel();
    }
}

pub fn connect<F, Fut>(send: F) -> Connection
where
    F: Fn(Batch) -> Fut + Send + 'static,
    Fut: Future<Output = Result<(), AgentCommandsError>> + Send,
{
    declarations().connect(send)
}

pub fn enable(storage: &Path) {
    declarations().load(storage);
}

pub fn started(pid: u32) -> Option<AgentCommandRoot> {
    if declarations()
        .state
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .storage
        .is_none()
    {
        return None;
    }
    let root = AgentCommandRoot {
        pid,
        start_time_ms: subc_os::process_start_time_ms(pid)?,
    };
    declarations().started(root);
    Some(root)
}

pub(crate) fn finished(root: Option<AgentCommandRoot>, cpu_ms: Option<u64>) {
    if let Some(root) = root {
        declarations().finished(root, cpu_ms);
    }
}

#[cfg(unix)]
pub(crate) fn wait4(
    pid: u32,
    flags: libc::c_int,
    root: Option<AgentCommandRoot>,
) -> std::io::Result<Option<std::process::ExitStatus>> {
    use std::os::unix::process::ExitStatusExt;
    loop {
        let mut status = 0;
        let mut usage = std::mem::MaybeUninit::<libc::rusage>::zeroed();
        // The PID is an owned direct child; wait4 initializes usage only on a successful reap.
        let result =
            unsafe { libc::wait4(pid as libc::pid_t, &mut status, flags, usage.as_mut_ptr()) };
        if result > 0 {
            let usage = unsafe { usage.assume_init() };
            let micros = |time: libc::timeval| {
                (time.tv_sec as u64)
                    .saturating_mul(1_000_000)
                    .saturating_add(time.tv_usec as u64)
            };
            let cpu_ms = micros(usage.ru_utime).saturating_add(micros(usage.ru_stime)) / 1_000;
            finished(root, Some(cpu_ms));
            return Ok(Some(std::process::ExitStatus::from_raw(status)));
        }
        if result == 0 {
            return Ok(None);
        }
        let error = std::io::Error::last_os_error();
        if error.kind() != std::io::ErrorKind::Interrupted {
            return Err(error);
        }
    }
}

#[cfg(windows)]
pub(crate) fn windows_cpu_ms(handle: std::os::windows::io::RawHandle) -> Option<u64> {
    use windows_sys::Win32::Foundation::FILETIME;
    use windows_sys::Win32::System::Threading::GetProcessTimes;
    let mut created = FILETIME::default();
    let mut exited = FILETIME::default();
    let mut kernel = FILETIME::default();
    let mut user = FILETIME::default();
    // The caller retains the process handle until its wait and this query finish.
    if unsafe { GetProcessTimes(handle, &mut created, &mut exited, &mut kernel, &mut user) } == 0 {
        return None;
    }
    let ticks =
        |time: FILETIME| (u64::from(time.dwHighDateTime) << 32) | u64::from(time.dwLowDateTime);
    Some(ticks(kernel).saturating_add(ticks(user)) / 10_000)
}

#[cfg(windows)]
#[derive(Debug)]
pub struct WindowsChild {
    child: std::process::Child,
    root: Option<AgentCommandRoot>,
}

#[cfg(windows)]
impl From<std::process::Child> for WindowsChild {
    fn from(child: std::process::Child) -> Self {
        Self { child, root: None }
    }
}

#[cfg(windows)]
impl std::ops::Deref for WindowsChild {
    type Target = std::process::Child;
    fn deref(&self) -> &Self::Target {
        &self.child
    }
}

#[cfg(windows)]
impl std::ops::DerefMut for WindowsChild {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.child
    }
}

#[cfg(windows)]
impl WindowsChild {
    pub fn declare_agent(&mut self) {
        self.root = started(self.child.id());
    }
    // Preserve the std::process::Child-compatible wait API without exposing unused methods publicly.
    #[allow(dead_code)]
    pub(crate) fn try_wait(&mut self) -> std::io::Result<Option<std::process::ExitStatus>> {
        let status = self.child.try_wait()?;
        if status.is_some() {
            self.finished();
        }
        Ok(status)
    }
    #[allow(dead_code)]
    pub(crate) fn wait(&mut self) -> std::io::Result<std::process::ExitStatus> {
        let status = self.child.wait()?;
        self.finished();
        Ok(status)
    }
    #[allow(dead_code)]
    fn finished(&self) {
        use std::os::windows::io::AsRawHandle;
        finished(self.root, windows_cpu_ms(self.child.as_raw_handle()));
    }
}

pub fn wait_pty(
    child: &mut dyn portable_pty::Child,
    root: Option<AgentCommandRoot>,
) -> std::io::Result<portable_pty::ExitStatus> {
    #[cfg(unix)]
    if let Some(pid) = child.process_id() {
        return wait4(pid, 0, root)?
            .map(Into::into)
            .ok_or_else(|| std::io::Error::other("wait4 returned no status"));
    }
    let status = child.wait()?;
    if root.is_some() {
        #[cfg(windows)]
        let cpu = child.as_raw_handle().and_then(windows_cpu_ms);
        #[cfg(not(windows))]
        let cpu = None;
        finished(root, cpu);
    }
    Ok(status)
}

pub fn try_wait_pty(
    child: &mut dyn portable_pty::Child,
    root: Option<AgentCommandRoot>,
) -> std::io::Result<Option<portable_pty::ExitStatus>> {
    #[cfg(unix)]
    if let Some(pid) = child.process_id() {
        return wait4(pid, libc::WNOHANG, root).map(|status| status.map(Into::into));
    }
    let status = child.try_wait()?;
    if status.is_some() {
        if root.is_some() {
            #[cfg(windows)]
            let cpu = child.as_raw_handle().and_then(windows_cpu_ms);
            #[cfg(not(windows))]
            let cpu = None;
            finished(root, cpu);
        }
    }
    Ok(status)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio::sync::{mpsc, oneshot};

    type Reply = oneshot::Sender<Result<(), AgentCommandsError>>;

    fn sender(hub: &Arc<Declarations>) -> (Connection, mpsc::UnboundedReceiver<(Batch, Reply)>) {
        let (tx, rx) = mpsc::unbounded_channel();
        let connection = hub.connect(move |batch| {
            let (reply, result) = oneshot::channel();
            tx.send((batch, reply)).unwrap();
            async {
                result
                    .await
                    .unwrap_or(Err(AgentCommandsError::ConnectionClosed))
            }
        });
        (connection, rx)
    }

    async fn receive(rx: &mut mpsc::UnboundedReceiver<(Batch, Reply)>) -> (Batch, Reply) {
        tokio::time::timeout(Duration::from_secs(2), rx.recv())
            .await
            .unwrap()
            .unwrap()
    }

    fn root(pid: u32) -> AgentCommandRoot {
        AgentCommandRoot {
            pid,
            start_time_ms: 1_700_000_000_000 + u64::from(pid),
        }
    }

    #[test]
    fn agent_commands_reused_pid_finishes_only_the_original_kernel_identity() {
        let hub = Declarations::default();
        let old = root(42);
        let new = AgentCommandRoot {
            start_time_ms: old.start_time_ms + 1,
            ..old
        };
        hub.started(old);
        hub.started(new);
        hub.finished(old, Some(250));
        let batch = hub.batch(true);
        assert_eq!(batch.known, Some(vec![new]));
        assert_eq!(
            batch.finished,
            [AgentCommandFinished {
                pid: old.pid,
                start_time_ms: old.start_time_ms,
                cpu_ms: Some(250)
            }]
        );
        hub.finished(old, Some(250));
        assert_eq!(
            hub.batch(true).finished.len(),
            1,
            "cached waits must not finish twice or settle the reused PID"
        );
    }

    #[tokio::test]
    async fn agent_commands_start_finish_cpu_and_known_after_reconnect() {
        let hub = Arc::new(Declarations::default());
        let live = root(42);
        let ended = root(43);
        hub.started(live);
        hub.started(ended);
        hub.finished(ended, Some(250));
        let (connection, mut rx) = sender(&hub);
        let (batch, reply) = receive(&mut rx).await;
        assert_eq!(batch.started, [live, ended]);
        assert_eq!(
            batch.finished,
            [AgentCommandFinished {
                pid: ended.pid,
                start_time_ms: ended.start_time_ms,
                cpu_ms: Some(250)
            }]
        );
        assert_eq!(batch.known, Some(vec![live]));
        reply.send(Ok(())).unwrap();
        tokio::task::yield_now().await;
        drop(connection);
        let (_connection, mut reconnected) = sender(&hub);
        let (batch, reply) = receive(&mut reconnected).await;
        assert!(batch.started.is_empty());
        assert!(batch.finished.is_empty());
        assert_eq!(batch.known, Some(vec![live]));
        reply.send(Ok(())).unwrap();
        hub.finished(live, None);
        let (batch, reply) = receive(&mut reconnected).await;
        assert_eq!(batch.finished[0].cpu_ms, None);
        let wire = serde_json::to_value(batch.finished[0]).unwrap();
        assert!(wire.get("cpu_ms").is_none());
        reply.send(Ok(())).unwrap();
    }

    #[tokio::test]
    async fn agent_commands_busy_retries_the_same_roots_before_later_finishes() {
        let hub = Arc::new(Declarations::default());
        let live = root(42);
        hub.started(live);
        let (_connection, mut rx) = sender(&hub);
        let (first, reply) = receive(&mut rx).await;
        reply
            .send(Err(AgentCommandsError::Refused(Box::new(
                subc_protocol::ErrorBody::new("busy", "try again"),
            ))))
            .unwrap();
        hub.finished(live, Some(19));
        let (retry, reply) = receive(&mut rx).await;
        assert_eq!(retry.started, first.started);
        assert_eq!(retry.finished, first.finished);
        assert_eq!(retry.known, first.known);
        reply.send(Ok(())).unwrap();
        let (next, reply) = receive(&mut rx).await;
        assert!(next.started.is_empty());
        assert_eq!(next.finished[0].cpu_ms, Some(19));
        assert!(next.known.is_none());
        reply.send(Ok(())).unwrap();
    }

    #[tokio::test]
    async fn agent_commands_unsupported_is_quiet_and_stops_retrying() {
        let hub = Arc::new(Declarations::default());
        let (_connection, mut rx) = sender(&hub);
        let (_, reply) = receive(&mut rx).await;
        reply.send(Err(AgentCommandsError::Unsupported)).unwrap();
        tokio::task::yield_now().await;
        hub.started(root(42));
        assert!(rx.recv().await.is_none());
        assert!(hub.state.lock().unwrap().events.is_empty());
    }

    #[tokio::test]
    async fn agent_commands_reconnect_cancels_the_stale_connection_and_keeps_unacked_roots() {
        let hub = Arc::new(Declarations::default());
        hub.started(root(42));
        let (_old, mut first) = sender(&hub);
        let (_, old_reply) = receive(&mut first).await;
        let (_current, mut current) = sender(&hub);
        let (batch, reply) = receive(&mut current).await;
        assert_eq!(batch.started, [root(42)]);
        assert_eq!(batch.known, Some(vec![root(42)]));
        tokio::task::yield_now().await;
        assert!(old_reply.is_closed());
        reply.send(Ok(())).unwrap();
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn agent_commands_persists_before_send_and_recovers_without_a_project_bind() {
        let storage = tempfile::tempdir().unwrap();
        let mut child = std::process::Command::new("sleep")
            .arg("30")
            .spawn()
            .unwrap();
        let root = AgentCommandRoot {
            pid: child.id(),
            start_time_ms: subc_os::process_start_time_ms(child.id()).unwrap(),
        };
        let first = Arc::new(Declarations::default());
        first.load(storage.path());
        first.started(root);
        let (connection, mut rx) = sender(&first);
        let (_, reply) = receive(&mut rx).await;
        let saved: Vec<AgentCommandRoot> = serde_json::from_slice(
            &std::fs::read(storage.path().join("agent-command-roots.json")).unwrap(),
        )
        .unwrap();
        assert_eq!(saved, [root]);
        reply.send(Ok(())).unwrap();
        connection.stop().await;

        let recovered = Arc::new(Declarations::default());
        recovered.load(storage.path());
        let (connection, mut rx) = sender(&recovered);
        let (batch, reply) = receive(&mut rx).await;
        assert_eq!(batch.known, Some(vec![root]));
        assert!(batch.started.is_empty());
        reply.send(Ok(())).unwrap();
        connection.stop().await;
        child.kill().unwrap();
        child.wait().unwrap();
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn agent_commands_restored_roots_use_kernel_identity_and_prune_when_gone() {
        let mut child = std::process::Command::new("/bin/sh")
            .args(["-c", "sleep 30"])
            .spawn()
            .unwrap();
        let root = AgentCommandRoot {
            pid: child.id(),
            start_time_ms: subc_os::process_start_time_ms(child.id()).unwrap(),
        };
        let hub = Arc::new(Declarations::default());
        hub.restored(root);
        let (_connection, mut rx) = sender(&hub);
        let (batch, reply) = receive(&mut rx).await;
        assert_eq!(batch.known, Some(vec![root]));
        assert!(
            batch.started.is_empty(),
            "known must not introduce a new start after restart"
        );
        reply.send(Ok(())).unwrap();
        child.kill().unwrap();
        child.wait().unwrap();
        hub.prune_exited();
        let (batch, reply) = receive(&mut rx).await;
        assert_eq!(
            batch.finished,
            [AgentCommandFinished {
                pid: root.pid,
                start_time_ms: root.start_time_ms,
                cpu_ms: None
            }]
        );
        reply.send(Ok(())).unwrap();
    }
}