agents-kit 0.0.2

Rust agent control Kit for Codex, OpenCode, and provider-backed Custom runtimes
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
mod native_cleanup;
mod native_process;
mod native_stream;

use std::panic::AssertUnwindSafe;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use agents_kit::KitError;
use agents_kit::error::{ErrorDetail, ErrorPhase, Operation};
use agents_kit::prompt::{PromptEvent, PromptResponse, PromptStream};
use futures_util::{StreamExt, stream};
use native_cleanup::NativeCleanup;
use tokio::sync::oneshot;
use tokio::time::Instant;

#[tokio::test]
async fn cleanup_settles_later_owners_and_retains_observation_panic() {
    let observed = NativeCleanup::observe(Instant::now() + Duration::from_secs(1), async {
        std::panic::panic_any(17_u32);
    })
    .await;
    let settled = AtomicUsize::new(0);
    let mut cleanup = NativeCleanup::new(Duration::from_millis(20));
    cleanup.settle(async { Err("child close failed") }).await;
    cleanup
        .settle::<&str>(async { std::panic::panic_any(29_u32) })
        .await;
    cleanup
        .settle(std::future::pending::<Result<(), &str>>())
        .await;
    cleanup
        .settle(async {
            settled.fetch_add(1, Ordering::SeqCst);
            Ok::<(), &str>(())
        })
        .await;
    assert_eq!(
        cleanup.diagnostics(),
        [
            "native cleanup failed: \"child close failed\"",
            "native cleanup panicked: non-string panic payload",
            "native cleanup deadline elapsed",
        ]
    );
    let failure =
        std::panic::catch_unwind(AssertUnwindSafe(|| cleanup.finish(observed))).unwrap_err();
    assert_eq!(failure.downcast_ref::<u32>(), Some(&17));
    assert_eq!(settled.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn cleanup_retains_observation_timeout_until_parent_settles() {
    let observed = NativeCleanup::observe(Instant::now(), std::future::pending::<()>()).await;
    let settled = AtomicUsize::new(0);
    let mut cleanup = NativeCleanup::new(Duration::from_secs(1));
    cleanup
        .settle(async {
            settled.fetch_add(1, Ordering::SeqCst);
            Err("cleanup secondary")
        })
        .await;
    let failure =
        std::panic::catch_unwind(AssertUnwindSafe(|| cleanup.finish(observed))).unwrap_err();
    let message = failure
        .downcast_ref::<&str>()
        .copied()
        .or_else(|| failure.downcast_ref::<String>().map(String::as_str))
        .unwrap();
    assert_eq!(message, "native observation deadline elapsed");
    assert_eq!(settled.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn cleanup_reports_first_failure_only_after_all_owners_settle() {
    let observed =
        NativeCleanup::observe(Instant::now() + Duration::from_secs(1), async { 7 }).await;
    let settled = AtomicUsize::new(0);
    let mut cleanup = NativeCleanup::new(Duration::from_secs(1));
    cleanup.settle(async { Err("first child failure") }).await;
    cleanup
        .settle(async {
            settled.fetch_add(1, Ordering::SeqCst);
            Err("later parent failure")
        })
        .await;
    let failure =
        std::panic::catch_unwind(AssertUnwindSafe(|| cleanup.finish(observed))).unwrap_err();
    assert_eq!(
        failure.downcast_ref::<String>().map(String::as_str),
        Some("native cleanup failed: \"first child failure\"")
    );
    assert_eq!(settled.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn cleanup_preserves_success_after_settling_every_owner() {
    let observed =
        NativeCleanup::observe(Instant::now() + Duration::from_secs(1), async { 7 }).await;
    let mut cleanup = NativeCleanup::new(Duration::from_secs(1));
    cleanup.settle(async { Ok::<(), &str>(()) }).await;
    assert_eq!(cleanup.finish(observed), 7);
}

#[tokio::test]
async fn cleanup_reaps_a_real_child_before_restoring_observation_failure() {
    let mut child = tokio::process::Command::new("sh")
        .args(["-c", "exec sleep 60"])
        .kill_on_drop(true)
        .spawn()
        .expect("real cleanup child must start");
    let observed = NativeCleanup::observe(Instant::now() + Duration::from_secs(1), async {
        std::panic::panic_any(43_u32);
    })
    .await;
    let mut cleanup = NativeCleanup::new(Duration::from_secs(5));
    cleanup.reap(&mut child).await;
    assert!(
        child
            .try_wait()
            .expect("child status must remain observable")
            .is_some()
    );
    assert!(cleanup.diagnostics().is_empty());
    let failure = std::panic::catch_unwind(AssertUnwindSafe(|| cleanup.finish(observed)))
        .expect_err("the original observation must remain failed");
    assert_eq!(failure.downcast_ref::<u32>(), Some(&43));
}

#[tokio::test]
async fn cleanup_reaps_an_already_exited_child_without_changing_observation() {
    let mut child = tokio::process::Command::new("sh")
        .args(["-c", "exit 7"])
        .kill_on_drop(true)
        .spawn()
        .expect("real cleanup child must start");
    let status = tokio::time::timeout(Duration::from_secs(5), child.wait())
        .await
        .expect("child exit must be bounded")
        .expect("child must exit");
    let observed =
        NativeCleanup::observe(Instant::now() + Duration::from_secs(1), async { 17 }).await;
    let mut cleanup = NativeCleanup::new(Duration::from_secs(5));
    cleanup.reap(&mut child).await;
    assert_eq!(status.code(), Some(7));
    assert_eq!(child.try_wait().unwrap().unwrap().code(), Some(7));
    assert_eq!(cleanup.finish(observed), 17);
}

#[tokio::test]
async fn cleanup_consumes_a_completed_task_failure() {
    let mut task = tokio::spawn(async { Err::<(), _>("shutdown failed") });
    let mut cleanup = NativeCleanup::new(Duration::from_secs(1));
    cleanup.join(&mut task).await;
    assert!(task.is_finished());
    assert_eq!(cleanup.diagnostics().len(), 1);
    assert!(cleanup.diagnostics()[0].contains("shutdown failed"));
}

#[tokio::test]
async fn cleanup_joins_task_cancellation_after_observation_timeout() {
    let released = std::sync::Arc::new(AtomicUsize::new(0));
    let mut task = tokio::spawn({
        let released = std::sync::Arc::clone(&released);
        async move {
            let _owned = released;
            std::future::pending::<Result<(), &str>>().await
        }
    });
    let mut cleanup = NativeCleanup::new(Duration::from_millis(20));
    cleanup.join(&mut task).await;
    assert!(task.is_finished());
    assert_eq!(std::sync::Arc::strong_count(&released), 1);
    assert_eq!(cleanup.diagnostics(), ["native cleanup deadline elapsed"]);
}

#[derive(Clone, Copy)]
enum InlineCleanupCase {
    ObservationPanic,
    SettlementTimeout,
}

struct InlineCleanupFuture<'a> {
    payload: [u8; 256 * 1024],
    polls: &'a AtomicUsize,
    drops: &'a AtomicUsize,
    case: InlineCleanupCase,
}

impl<'a> InlineCleanupFuture<'a> {
    fn new(polls: &'a AtomicUsize, drops: &'a AtomicUsize, case: InlineCleanupCase) -> Self {
        Self {
            payload: [37; 256 * 1024],
            polls,
            drops,
            case,
        }
    }

    async fn observation() {
        let polls = AtomicUsize::new(0);
        let drops = AtomicUsize::new(0);
        let observed = NativeCleanup::observe(
            Instant::now() + Duration::from_secs(1),
            InlineCleanupFuture::new(&polls, &drops, InlineCleanupCase::ObservationPanic),
        )
        .await;
        assert_eq!(polls.load(Ordering::SeqCst), 1);
        assert_eq!(drops.load(Ordering::SeqCst), 1);
        let mut cleanup = NativeCleanup::new(Duration::from_secs(1));
        let later = AtomicUsize::new(0);
        cleanup
            .settle(async {
                later.fetch_add(1, Ordering::SeqCst);
                Err("cleanup secondary")
            })
            .await;
        let primary = std::panic::catch_unwind(AssertUnwindSafe(|| cleanup.finish(observed)))
            .expect_err("observation panic must survive cleanup");
        assert_eq!(primary.downcast_ref::<u32>(), Some(&71));
        assert_eq!(later.load(Ordering::SeqCst), 1);
    }

    async fn settlement() {
        let polls = AtomicUsize::new(0);
        let drops = AtomicUsize::new(0);
        let observed = NativeCleanup::observe(Instant::now() + Duration::from_secs(1), async {
            std::panic::panic_any(73_u32);
        })
        .await;
        let mut cleanup = NativeCleanup::new(Duration::from_millis(20));
        cleanup
            .settle(InlineCleanupFuture::new(
                &polls,
                &drops,
                InlineCleanupCase::SettlementTimeout,
            ))
            .await;
        assert!(polls.load(Ordering::SeqCst) > 0);
        assert_eq!(drops.load(Ordering::SeqCst), 1);
        assert_eq!(cleanup.diagnostics(), ["native cleanup deadline elapsed"]);
        let later = AtomicUsize::new(0);
        cleanup
            .settle(async {
                later.fetch_add(1, Ordering::SeqCst);
                Ok::<(), &str>(())
            })
            .await;
        let primary = std::panic::catch_unwind(AssertUnwindSafe(|| cleanup.finish(observed)))
            .expect_err("the original panic must survive the cleanup timeout");
        assert_eq!(primary.downcast_ref::<u32>(), Some(&73));
        assert_eq!(later.load(Ordering::SeqCst), 1);
    }
}

impl std::future::Future for InlineCleanupFuture<'_> {
    type Output = Result<(), &'static str>;

    fn poll(
        self: std::pin::Pin<&mut Self>,
        _context: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        assert!(self.payload.iter().all(|byte| *byte == 37));
        self.polls.fetch_add(1, Ordering::SeqCst);
        match self.case {
            InlineCleanupCase::ObservationPanic => std::panic::panic_any(71_u32),
            InlineCleanupCase::SettlementTimeout => std::task::Poll::Pending,
        }
    }
}

impl Drop for InlineCleanupFuture<'_> {
    fn drop(&mut self) {
        self.drops.fetch_add(1, Ordering::SeqCst);
    }
}

#[tokio::test]
async fn cleanup_large_inline_futures_preserve_outcomes_and_drop_before_return() {
    const CHILD_MODE: &str = "AI_KIT_NATIVE_CLEANUP_INLINE_CHILD";
    match std::env::var(CHILD_MODE).as_deref() {
        Ok("observation") => return Box::pin(InlineCleanupFuture::observation()).await,
        Ok("settlement") => return Box::pin(InlineCleanupFuture::settlement()).await,
        Err(std::env::VarError::NotPresent) => {}
        other => panic!("unexpected inline cleanup child mode: {other:?}"),
    }
    // Stack overflow aborts cannot unwind. Keep each regression in an owned
    // child using the ordinary test-thread stack, so the parent can report red
    // evidence and still join the child without aborting the entire target.
    let mut outcomes = Vec::new();
    for mode in ["observation", "settlement"] {
        let mut child = tokio::process::Command::new(std::env::current_exe().unwrap())
            .args([
                "--exact",
                "cleanup_large_inline_futures_preserve_outcomes_and_drop_before_return",
                "--nocapture",
                "--test-threads=1",
            ])
            .env(CHILD_MODE, mode)
            .kill_on_drop(true)
            .spawn()
            .expect("owned inline cleanup child must start");
        let observed = NativeCleanup::observe(Instant::now() + Duration::from_secs(15), async {
            child.wait().await
        })
        .await;
        let mut cleanup = NativeCleanup::new(Duration::from_secs(5));
        cleanup.reap(&mut child).await;
        let status = cleanup
            .finish(observed)
            .expect("owned child wait must succeed");
        eprintln!("inline cleanup {mode}: child joined with {status}");
        outcomes.push((mode, status));
    }
    assert!(
        outcomes.iter().all(|(_, status)| status.success()),
        "inline cleanup outcomes: {outcomes:?}"
    );
}

#[tokio::test]
async fn abort_stream_drains_while_abort_is_pending() {
    let (entered, entry) = oneshot::channel();
    let (drained, drainage) = oneshot::channel();
    let (settled, settlement) = oneshot::channel();
    let fragments = Arc::new(AtomicUsize::new(0));
    let observed_fragments = Arc::clone(&fragments);
    let aborts = AtomicUsize::new(0);
    let chunks = stream::once(async move {
        entry
            .await
            .expect("abort must enter before output progresses");
        observed_fragments.fetch_add(1, Ordering::SeqCst);
        drained.send(()).unwrap();
        Ok(PromptEvent::OutputDelta {
            text: "chunk".to_owned(),
        })
    });
    let mut output = PromptStream::from_stream(chunks.chain(stream::once(async move {
        settlement
            .await
            .expect("abort must settle before its terminal");
        Err(KitError::cancelled("accepted prompt cancelled"))
    })));
    let abort = async {
        entered.send(()).unwrap();
        drainage
            .await
            .expect("observation must progress while abort is pending");
        aborts.fetch_add(1, Ordering::SeqCst);
        settled.send(()).unwrap();
        Ok(())
    };
    // No task is spawned: the two owned futures and their signal endpoints are
    // dropped before assertions, including when this watchdog diagnoses a stall.
    let observed = NativeCleanup::observe(
        Instant::now() + Duration::from_secs(1),
        NativeCleanup::abort_stream(&mut output, abort),
    )
    .await;
    drop(output);
    observed
        .expect("drain/abort coordination must not panic")
        .expect("receiver must progress before abort can settle");
    assert_eq!(fragments.load(Ordering::SeqCst), 1);
    assert_eq!(aborts.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn abort_stream_preserves_overflow_and_settles_abort() {
    let (observed, observation) = oneshot::channel();
    let aborts = AtomicUsize::new(0);
    let mut output = PromptStream::from_stream(stream::once(async move {
        observed.send(()).unwrap();
        Err(KitError::integration("prompt output observation overflow")
            .with_detail(ErrorDetail::ResponseLimit { limit: 64 })
            .with_context(Operation::Prompt, ErrorPhase::Publication))
    }));
    let abort = async {
        observation
            .await
            .expect("terminal observation must run while abort is pending");
        aborts.fetch_add(1, Ordering::SeqCst);
        Ok(())
    };
    let observed = NativeCleanup::observe(
        Instant::now() + Duration::from_secs(1),
        NativeCleanup::abort_stream(&mut output, abort),
    )
    .await;
    drop(output);
    assert_eq!(
        aborts.load(Ordering::SeqCst),
        1,
        "observation failure must not discard abort"
    );
    let panic = observed.expect_err("overflow remains a failed live observation");
    let message = panic
        .downcast_ref::<String>()
        .expect("retained formatted panic");
    assert!(message.contains("prompt output observation overflow"));
    assert!(message.contains("ResponseLimit { limit: 64 }"));
}

#[tokio::test]
async fn abort_stream_drains_before_reporting_abort_failure() {
    let items = Arc::new(AtomicUsize::new(0));
    let observed_items = Arc::clone(&items);
    let mut output = PromptStream::from_stream(
        stream::iter([
            Ok(PromptEvent::OutputDelta {
                text: "partial".to_owned(),
            }),
            Err(KitError::cancelled("prompt terminal")),
        ])
        .inspect(move |_| {
            observed_items.fetch_add(1, Ordering::SeqCst);
        }),
    );
    let aborts = AtomicUsize::new(0);
    let abort = async {
        aborts.fetch_add(1, Ordering::SeqCst);
        Err(KitError::integration("abort receipt failed"))
    };
    let observed = NativeCleanup::observe(
        Instant::now() + Duration::from_secs(1),
        NativeCleanup::abort_stream(&mut output, abort),
    )
    .await;
    drop(output);
    assert_eq!(aborts.load(Ordering::SeqCst), 1);
    assert_eq!(
        items.load(Ordering::SeqCst),
        2,
        "abort failure must not discard stream observation"
    );
    let panic = observed.expect_err("abort failure remains primary");
    let message = panic
        .downcast_ref::<String>()
        .expect("retained formatted panic");
    assert!(message.contains("explicit abort joins the current stream generation"));
    assert!(message.contains("abort receipt failed"));
}

#[tokio::test]
async fn abort_stream_accepts_cancellation_and_fuses() {
    let items = Arc::new(AtomicUsize::new(0));
    let observed_items = Arc::clone(&items);
    let mut output = PromptStream::from_stream(
        stream::iter([
            Ok(PromptEvent::OutputDelta {
                text: "partial".to_owned(),
            }),
            Err(KitError::cancelled("prompt terminal")),
            Ok(PromptEvent::OutputDelta {
                text: "must never be observed".to_owned(),
            }),
        ])
        .inspect(move |_| {
            observed_items.fetch_add(1, Ordering::SeqCst);
        }),
    );
    let observed = NativeCleanup::observe(
        Instant::now() + Duration::from_secs(1),
        NativeCleanup::abort_stream(&mut output, async { Ok(()) }),
    )
    .await;
    let fused = output.recv().await.is_none();
    drop(output);
    observed
        .expect("cancellation observation must not panic")
        .expect("bounded cancellation");
    assert!(fused);
    assert_eq!(items.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn abort_stream_accepts_natural_completion_and_fuses() {
    let items = Arc::new(AtomicUsize::new(0));
    let observed_items = Arc::clone(&items);
    let mut output = PromptStream::from_stream(
        stream::iter([
            Ok(PromptEvent::OutputDelta {
                text: "partial".to_owned(),
            }),
            Ok(PromptEvent::Completed(PromptResponse::new(
                "native completion".to_owned(),
                None,
            ))),
            Err(KitError::integration("must never be observed")),
        ])
        .inspect(move |_| {
            observed_items.fetch_add(1, Ordering::SeqCst);
        }),
    );
    let observed = NativeCleanup::observe(
        Instant::now() + Duration::from_secs(1),
        NativeCleanup::abort_stream(&mut output, async { Ok(()) }),
    )
    .await;
    let fused = output.recv().await.is_none();
    drop(output);
    observed
        .expect("natural completion must not panic")
        .expect("bounded completion");
    assert!(fused);
    assert_eq!(items.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn abort_stream_rejects_empty_natural_completion() {
    let mut output = PromptStream::from_stream(stream::iter([Ok(PromptEvent::Completed(
        PromptResponse::new(String::new(), None),
    ))]));
    let observed = NativeCleanup::observe(
        Instant::now() + Duration::from_secs(1),
        NativeCleanup::abort_stream(&mut output, async { Ok(()) }),
    )
    .await;
    drop(output);
    assert!(
        observed.is_err(),
        "empty natural completion must remain a failed assertion"
    );
}

#[tokio::test]
async fn abort_stream_rejects_premature_eof() {
    let mut output = PromptStream::from_stream(stream::empty());
    let observed = NativeCleanup::observe(
        Instant::now() + Duration::from_secs(1),
        NativeCleanup::abort_stream(&mut output, async { Ok(()) }),
    )
    .await;
    drop(output);
    let panic = observed.expect_err("missing terminal cannot pass");
    let message = panic
        .downcast_ref::<String>()
        .expect("retained formatted panic");
    assert!(message.contains("prompt output ended before its terminal response"));
}

#[tokio::test]
async fn stream_observation_rejects_terminal_only_with_bounded_counts() {
    for terminal in [
        String::new(),
        "nonempty".to_owned(),
        "secret\n\0😀".repeat(16_384),
    ] {
        let bytes = terminal.len();
        let empty = terminal.is_empty();
        let output = PromptStream::from_stream(stream::iter([Ok(PromptEvent::Completed(
            PromptResponse::new(terminal, None),
        ))]));
        let observed = NativeCleanup::observe(
            Instant::now() + Duration::from_secs(1),
            NativeCleanup::observe_stream(output),
        )
        .await;
        let panic = observed.expect_err("terminal-only output must remain a failed assertion");
        let message = panic
            .downcast_ref::<&str>()
            .copied()
            .or_else(|| panic.downcast_ref::<String>().map(String::as_str))
            .expect("stream assertion must retain its diagnostic");
        assert_eq!(
            message,
            format!(
                "real source must emit text before its terminal event; fragments=0; terminal_output_bytes={bytes}; terminal_output_empty={empty}"
            )
        );
        assert!(message.len() <= 256);
        assert!(message.is_ascii());
        assert!(!message.chars().any(char::is_control));
        assert!(!message.contains("secret"));
    }
}

#[tokio::test]
async fn stream_observation_preserves_terminal_bytes_and_fusion() {
    let items = Arc::new(AtomicUsize::new(0));
    let observed_items = Arc::clone(&items);
    let terminal = "untouched Ω\n terminal";
    let output = PromptStream::from_stream(
        stream::iter([
            Ok(PromptEvent::OutputDelta {
                text: "one".to_owned(),
            }),
            Ok(PromptEvent::OutputDelta {
                text: "two".to_owned(),
            }),
            Ok(PromptEvent::Completed(PromptResponse::new(
                terminal.to_owned(),
                None,
            ))),
            Err(KitError::integration("must never be observed")),
        ])
        .inspect(move |_| {
            observed_items.fetch_add(1, Ordering::SeqCst);
        }),
    );
    let response = NativeCleanup::observe(
        Instant::now() + Duration::from_secs(1),
        NativeCleanup::observe_stream(output),
    )
    .await
    .expect("valid stream must not panic")
    .expect("bounded stream observation");
    assert_eq!(response.output(), terminal);
    assert_eq!(items.load(Ordering::SeqCst), 3);
}

#[tokio::test]
async fn stream_observation_preserves_empty_fragment_and_terminal_failures() {
    for (fragment, terminal, expected) in [
        ("", "terminal", "native output fragment must contain text"),
        ("fragment", "", "terminal response remains authoritative"),
    ] {
        let output = PromptStream::from_stream(stream::iter([
            Ok(PromptEvent::OutputDelta {
                text: fragment.to_owned(),
            }),
            Ok(PromptEvent::Completed(PromptResponse::new(
                terminal.to_owned(),
                None,
            ))),
        ]));
        let observed = NativeCleanup::observe(
            Instant::now() + Duration::from_secs(1),
            NativeCleanup::observe_stream(output),
        )
        .await;
        let panic = observed.expect_err("original empty-output assertions must still fail");
        let message = panic
            .downcast_ref::<&str>()
            .copied()
            .or_else(|| panic.downcast_ref::<String>().map(String::as_str))
            .expect("original assertion must remain visible");
        assert_eq!(message, expected);
    }
}