mentra 0.18.0

An agent runtime for tool-using LLM applications
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
use crate::{
    BuiltinProvider, ContentBlock, Role, Runtime, TokenUsage,
    agent::AgentEvent,
    error::RuntimeError,
    provider::{ContentBlockDelta, ContentBlockStart, ProviderEvent},
    runtime::{CancellationToken, EarlyEnd, RunOptions},
};

use super::support::{
    ScriptedProvider, StaticTool, StopTrippingTool, StreamScript, model_info, ok_stream,
};

/// Builds a [`TokenUsage`] reporting only `input_tokens`/`output_tokens`, the two
/// fields [`RunOptions::token_budget`] is evaluated against.
fn usage(input_tokens: u64, output_tokens: u64) -> TokenUsage {
    TokenUsage {
        input_tokens: Some(input_tokens),
        output_tokens: Some(output_tokens),
        ..Default::default()
    }
}

/// Like `support::tool_use_stream`, but also reports `usage` via `MessageDelta`
/// so a round-boundary [`RunOptions::token_budget`] check has something to
/// evaluate.
fn tool_use_stream_with_usage(
    model: &str,
    id: &str,
    name: &str,
    input_json: &str,
    usage: TokenUsage,
) -> StreamScript {
    ok_stream(vec![
        ProviderEvent::MessageStarted {
            id: format!("msg-{id}"),
            model: model.to_string(),
            role: Role::Assistant,
        },
        ProviderEvent::ContentBlockStarted {
            index: 0,
            kind: ContentBlockStart::ToolUse {
                id: id.to_string(),
                name: name.to_string(),
            },
        },
        ProviderEvent::ContentBlockDelta {
            index: 0,
            delta: ContentBlockDelta::ToolUseInputJson(input_json.to_string()),
        },
        ProviderEvent::ContentBlockStopped { index: 0 },
        ProviderEvent::MessageDelta {
            stop_reason: None,
            usage: Some(usage),
        },
        ProviderEvent::MessageStopped,
    ])
}

/// Like `support::text_stream`, but also reports `usage` via `MessageDelta`.
fn text_stream_with_usage(model: &str, text: &str, usage: TokenUsage) -> StreamScript {
    ok_stream(vec![
        ProviderEvent::MessageStarted {
            id: format!("msg-{text}"),
            model: model.to_string(),
            role: Role::Assistant,
        },
        ProviderEvent::ContentBlockStarted {
            index: 0,
            kind: ContentBlockStart::Text,
        },
        ProviderEvent::ContentBlockDelta {
            index: 0,
            delta: ContentBlockDelta::Text(text.to_string()),
        },
        ProviderEvent::ContentBlockStopped { index: 0 },
        ProviderEvent::MessageDelta {
            stop_reason: None,
            usage: Some(usage),
        },
        ProviderEvent::MessageStopped,
    ])
}

#[tokio::test]
async fn token_budget_stops_gracefully_after_the_round_that_crosses_it() {
    // Round 1 reports usage that reaches the budget exactly; round 2 (a text
    // response) must never be requested. Because the run stops before a final
    // assistant message, `Agent::run` reports `EmptyAssistantResponse` — the same
    // honest "stopped before a final answer" outcome `RunOptions::stop` produces
    // at the identical boundary — while the gathered tool round stays committed
    // rather than rolled back.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            tool_use_stream_with_usage(
                &model.id,
                "call-1",
                "probe_tool",
                r#"{"value":"hi"}"#,
                usage(60, 40),
            ),
            // Must never be requested: the budget trips before round 2 starts.
            text_stream_with_usage(&model.id, "must not run", usage(1, 1)),
        ],
    );
    let provider_handle = provider.clone();
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_tool(StaticTool::success("probe_tool", "ok"))
        .build()
        .expect("build runtime");
    let mut agent = runtime.spawn("agent", model).expect("spawn agent");

    let result = agent
        .run(
            vec![ContentBlock::text("go")],
            RunOptions {
                token_budget: Some(100),
                ..Default::default()
            },
        )
        .await;

    assert!(matches!(result, Err(RuntimeError::EmptyAssistantResponse)));
    assert_eq!(
        agent.history().len(),
        3,
        "the round that crossed the budget stays committed, not rolled back"
    );
    assert_eq!(
        provider_handle.recorded_requests().await.len(),
        1,
        "the budget halted the run before a second model request"
    );
}

#[tokio::test]
async fn absent_token_budget_ignores_reported_usage() {
    // With `token_budget: None` (the default), no amount of reported usage stops
    // the run early — the seam is inert, reproducing today's behavior exactly.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            tool_use_stream_with_usage(
                &model.id,
                "call-1",
                "probe_tool",
                r#"{"value":"hi"}"#,
                usage(10_000, 10_000),
            ),
            text_stream_with_usage(&model.id, "done", usage(10_000, 10_000)),
        ],
    );
    let provider_handle = provider.clone();
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_tool(StaticTool::success("probe_tool", "ok"))
        .build()
        .expect("build runtime");
    let mut agent = runtime.spawn("agent", model).expect("spawn agent");

    let message = agent
        .run(vec![ContentBlock::text("go")], RunOptions::default())
        .await
        .expect("run completes normally despite large reported usage");

    assert_eq!(message.text(), "done");
    assert_eq!(provider_handle.recorded_requests().await.len(), 2);
    assert_eq!(agent.history().len(), 4);
}

#[tokio::test]
async fn a_crossed_budget_reports_why_the_turn_ended() {
    // The defect this signal exists for: the run above ends correctly — the
    // round that crossed the bound stays committed, nothing is rolled back —
    // and says nothing about *why* it ended, since a run the model finished
    // returns exactly the same way. `ended_early` is the runner's own answer,
    // recorded at the boundary it refused to start another round at, and read
    // back through a clone of the options the run was given.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            tool_use_stream_with_usage(
                &model.id,
                "call-1",
                "probe_tool",
                r#"{"value":"hi"}"#,
                usage(60, 40),
            ),
            text_stream_with_usage(&model.id, "must not run", usage(1, 1)),
        ],
    );
    let provider_handle = provider.clone();
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_tool(StaticTool::success("probe_tool", "ok"))
        .build()
        .expect("build runtime");
    let mut agent = runtime.spawn("agent", model).expect("spawn agent");

    let options = RunOptions {
        token_budget: Some(100),
        ..Default::default()
    };
    let result = agent
        .run(vec![ContentBlock::text("go")], options.clone())
        .await;

    assert_eq!(
        options.ended_early(),
        Some(EarlyEnd::TokenBudget),
        "the run must report the bound that ended it, not leave it to be inferred"
    );
    // The behavior around the report is unchanged, and pinned here beside it so
    // that adding the signal cannot quietly turn a graceful end into a rollback.
    assert!(matches!(result, Err(RuntimeError::EmptyAssistantResponse)));
    assert_eq!(
        agent.history().len(),
        3,
        "the round that crossed the budget stays committed, not rolled back"
    );
    assert_eq!(provider_handle.recorded_requests().await.len(), 1);
}

#[tokio::test]
async fn a_requested_stop_is_not_reported_as_a_crossed_budget() {
    // Both graceful bounds end a turn at the same boundary in the same way, so
    // the signal is only worth anything if it tells them apart. The budget here
    // is set and nowhere near crossed: what ends the turn is the tool tripping
    // the stop token, and that is what must be reported.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let stop = CancellationToken::default();
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            tool_use_stream_with_usage(
                &model.id,
                "call-1",
                "stop_probe",
                r#"{"value":"enough"}"#,
                usage(10, 10),
            ),
            text_stream_with_usage(&model.id, "must not run", usage(1, 1)),
        ],
    );
    let provider_handle = provider.clone();
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_tool(StopTrippingTool::new("stop_probe", stop.clone()))
        .build()
        .expect("build runtime");
    let mut agent = runtime.spawn("agent", model).expect("spawn agent");

    let options = RunOptions {
        stop: Some(stop),
        token_budget: Some(10_000),
        ..Default::default()
    };
    let result = agent
        .run(vec![ContentBlock::text("go")], options.clone())
        .await;

    assert_eq!(options.ended_early(), Some(EarlyEnd::StopRequested));
    assert!(matches!(result, Err(RuntimeError::EmptyAssistantResponse)));
    assert_eq!(
        options.reported_tokens(),
        20,
        "the bound was never near: a caller recomputing it would have found nothing"
    );
    assert_eq!(provider_handle.recorded_requests().await.len(), 1);
}

#[tokio::test]
async fn a_stop_and_a_crossed_budget_together_report_the_stop() {
    // The one round both spends the whole bound and trips the stop, so both
    // conditions hold at the boundary that ends the turn. The stop wins: it is
    // an instruction the caller issued, and the turn would have ended there
    // with no budget set at all. Reporting the budget would tell a caller its
    // allowance ran out when what happened is that it asked to stop.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let stop = CancellationToken::default();
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            tool_use_stream_with_usage(
                &model.id,
                "call-1",
                "stop_probe",
                r#"{"value":"enough"}"#,
                usage(60, 60),
            ),
            text_stream_with_usage(&model.id, "must not run", usage(1, 1)),
        ],
    );
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .with_tool(StopTrippingTool::new("stop_probe", stop.clone()))
        .build()
        .expect("build runtime");
    let mut agent = runtime.spawn("agent", model).expect("spawn agent");

    let options = RunOptions {
        stop: Some(stop),
        token_budget: Some(100),
        ..Default::default()
    };
    let _ = agent
        .run(vec![ContentBlock::text("go")], options.clone())
        .await;

    assert!(
        options.reported_tokens() >= 100,
        "the budget really is crossed, so the precedence is what decides the report"
    );
    assert_eq!(options.ended_early(), Some(EarlyEnd::StopRequested));
}

#[tokio::test]
async fn a_turn_that_runs_to_completion_reports_nothing() {
    // The default that keeps the signal honest: a bound that was set but never
    // reached leaves nothing behind, so `Some(..)` always means the runner
    // ended the turn rather than the model finishing it.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![text_stream_with_usage(&model.id, "done", usage(10, 10))],
    );
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .build()
        .expect("build runtime");
    let mut agent = runtime.spawn("agent", model).expect("spawn agent");

    let options = RunOptions {
        stop: Some(CancellationToken::default()),
        token_budget: Some(10_000),
        ..Default::default()
    };
    let message = agent
        .run(vec![ContentBlock::text("go")], options.clone())
        .await
        .expect("the run completes under both bounds");

    assert_eq!(message.text(), "done");
    assert_eq!(options.ended_early(), None);
}

#[tokio::test]
async fn a_turn_that_ends_on_the_budget_and_still_answers_reports_why() {
    // The case that makes the signal load-bearing rather than convenient. The
    // model finished its message *and* a steer was queued behind it, so the
    // runner checks whether another request is available, finds the bound
    // crossed, and returns the message it has. The turn is an ordinary `Ok`
    // carrying an ordinary final answer — indistinguishable from a turn that
    // ran to completion except through what the runner recorded, and the still
    // pending steer is the work that got left behind.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            text_stream_with_usage(&model.id, "answered first", usage(60, 40)),
            text_stream_with_usage(&model.id, "must not run", usage(1, 1)),
        ],
    );
    let provider_handle = provider.clone();
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .build()
        .expect("build runtime");
    let mut agent = runtime.spawn("agent", model).expect("spawn agent");
    let steering = agent.steering_handle();
    steering.steer(vec![ContentBlock::text("and then this")]);

    let options = RunOptions {
        token_budget: Some(100),
        ..Default::default()
    };
    let message = agent
        .run(vec![ContentBlock::text("go")], options.clone())
        .await
        .expect("a turn that ends on the budget after a committed message succeeds");

    assert_eq!(message.text(), "answered first");
    assert_eq!(
        options.ended_early(),
        Some(EarlyEnd::TokenBudget),
        "a successful turn is exactly where an unreported bound is invisible"
    );
    assert!(
        steering.has_pending(),
        "the steer no request could carry is kept, not consumed"
    );
    assert_eq!(provider_handle.recorded_requests().await.len(), 1);
}

#[tokio::test]
async fn child_run_shares_cancellation_with_parent() {
    // `RunOptions::child` carries the parent's `cancellation` token forward, so a
    // parent cancel stops a child run threaded with the derived options — even
    // though the two runs are on different agents and never call into each other.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![text_stream_with_usage(
            &model.id,
            "should not complete",
            usage(1, 1),
        )],
    );
    let runtime = Runtime::empty_builder()
        .with_provider_instance(provider)
        .build()
        .expect("build runtime");
    let mut child_agent = runtime.spawn("child", model).expect("spawn child agent");

    let cancellation = CancellationToken::default();
    let parent_options = RunOptions {
        cancellation: Some(cancellation.clone()),
        ..Default::default()
    };
    let child_options = parent_options.child();
    cancellation.cancel();

    let error = child_agent
        .run(vec![ContentBlock::text("go")], child_options)
        .await
        .expect_err("a cancelled parent token must stop the derived child run");

    assert!(matches!(error, RuntimeError::Cancelled));
}

#[tokio::test]
async fn child_usage_counts_toward_shared_token_budget() {
    // Parent and child share one token-accounting handle via `RunOptions::child`:
    // neither run's own usage alone crosses the budget, but their combined total
    // does, so the child's run stops gracefully at the shared bound.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let parent_provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![text_stream_with_usage(
            &model.id,
            "parent done",
            usage(40, 20),
        )],
    );
    let parent_runtime = Runtime::empty_builder()
        .with_provider_instance(parent_provider)
        .build()
        .expect("build runtime");
    let mut parent_agent = parent_runtime
        .spawn("parent", model.clone())
        .expect("spawn parent");

    let parent_options = RunOptions {
        token_budget: Some(100),
        ..Default::default()
    };
    parent_agent
        .run(vec![ContentBlock::text("go")], parent_options.clone())
        .await
        .expect("parent run completes under budget");
    assert_eq!(
        parent_options.reported_tokens(),
        60,
        "parent alone stays under the shared bound"
    );

    let child_options = parent_options.child();
    assert_eq!(
        child_options.reported_tokens(),
        60,
        "the derived child starts from the parent's already-reported usage"
    );

    let child_provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            // parent(60) + child(50) = 110, crossing the shared bound of 100.
            tool_use_stream_with_usage(
                &model.id,
                "call-1",
                "probe_tool",
                r#"{"value":"hi"}"#,
                usage(30, 20),
            ),
            text_stream_with_usage(&model.id, "must not run", usage(1, 1)),
        ],
    );
    let child_provider_handle = child_provider.clone();
    let child_runtime = Runtime::empty_builder()
        .with_provider_instance(child_provider)
        .with_tool(StaticTool::success("probe_tool", "ok"))
        .build()
        .expect("build runtime");
    let mut child_agent = child_runtime.spawn("child", model).expect("spawn child");

    let result = child_agent
        .run(vec![ContentBlock::text("go")], child_options)
        .await;

    assert!(
        matches!(result, Err(RuntimeError::EmptyAssistantResponse)),
        "the child stops gracefully once the combined parent+child usage crosses the bound"
    );
    assert_eq!(
        child_provider_handle.recorded_requests().await.len(),
        1,
        "the shared bound halted the child before its second round"
    );
}

/// Drains the events an agent emitted during a finished run.
fn collect_events(receiver: &mut tokio::sync::broadcast::Receiver<AgentEvent>) -> Vec<AgentEvent> {
    let mut events = Vec::new();
    while let Ok(event) = receiver.try_recv() {
        events.push(event);
    }
    events
}

/// The `input + output` totals of every [`AgentEvent::UsageReport`] in `events`,
/// in the order they were emitted.
fn reported_usage_totals(events: &[AgentEvent]) -> Vec<u64> {
    events
        .iter()
        .filter_map(|event| match event {
            AgentEvent::UsageReport {
                input_tokens,
                output_tokens,
                ..
            } => Some(input_tokens + output_tokens),
            _ => None,
        })
        .collect()
}

#[tokio::test]
async fn delegated_subagent_usage_counts_against_the_parent_token_budget() {
    // The `task` intrinsic is the one child run mentra drives itself: the model
    // asks for it from inside the parent's run. Running it on the parent's
    // `RunOptions::child` is what stops a model from delegating its way past the
    // budget its own run was given — the child reports into the parent's
    // accounting handle, and the parent ends at the next round boundary once the
    // combined total crosses the bound.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            // Parent round 1 delegates, reporting 60 of the 100-token bound.
            tool_use_stream_with_usage(
                &model.id,
                "parent-task",
                "task",
                r#"{"prompt":"delegate"}"#,
                usage(40, 20),
            ),
            // The delegated run spends 50 more, taking the shared total to 110.
            text_stream_with_usage(&model.id, "child summary", usage(30, 20)),
            // Parent round 2 must never be requested.
            text_stream_with_usage(&model.id, "parent done", usage(1, 1)),
        ],
    );
    let provider_handle = provider.clone();
    let runtime = Runtime::builder()
        .with_provider_instance(provider)
        .build()
        .expect("build runtime");
    let mut agent = runtime.spawn("agent", model).expect("spawn agent");

    let options = RunOptions {
        token_budget: Some(100),
        ..Default::default()
    };
    let result = agent
        .run(vec![ContentBlock::text("delegate that")], options.clone())
        .await;

    assert_eq!(
        options.reported_tokens(),
        110,
        "the delegated run must report into the parent's accounting handle, not a fresh one"
    );
    assert!(
        matches!(result, Err(RuntimeError::EmptyAssistantResponse)),
        "the parent stops gracefully at the boundary after delegated spend crossed the bound, \
         reporting the same 'stopped before a final answer' outcome any tripped bound does"
    );
    assert_eq!(
        provider_handle.recorded_requests().await.len(),
        2,
        "one parent round and one delegated round: the parent never got a second round"
    );
}

#[tokio::test]
async fn parent_cancellation_reaches_the_delegated_subagent() {
    // The delegated run shares the parent's cancellation token, so it ends at
    // its own next round boundary rather than running on unreachable while the
    // parent is torn down. `cancel_probe` trips the very token the parent's
    // options hold, so the child honoring it can only mean it inherited that
    // token rather than a default `None`.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let cancellation = CancellationToken::default();
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            tool_use_stream_with_usage(
                &model.id,
                "parent-task",
                "task",
                r#"{"prompt":"delegate"}"#,
                usage(1, 1),
            ),
            tool_use_stream_with_usage(
                &model.id,
                "child-tool",
                "cancel_probe",
                r#"{"value":"trip it"}"#,
                usage(1, 1),
            ),
            // Never requested: the child checks the shared token before its
            // second round.
            text_stream_with_usage(&model.id, "child must not continue", usage(1, 1)),
        ],
    );
    let provider_handle = provider.clone();
    let runtime = Runtime::builder()
        .with_provider_instance(provider)
        .with_tool(StopTrippingTool::new("cancel_probe", cancellation.clone()))
        .build()
        .expect("build runtime");
    let mut agent = runtime.spawn("agent", model).expect("spawn agent");

    let error = agent
        .run(
            vec![ContentBlock::text("delegate that")],
            RunOptions {
                cancellation: Some(cancellation),
                ..Default::default()
            },
        )
        .await
        .expect_err("a cancelled run must fail rather than finish");

    assert!(matches!(error, RuntimeError::Cancelled));
    assert_eq!(
        provider_handle.recorded_requests().await.len(),
        2,
        "the child stopped at its own round boundary; without the shared token it would \
         have run a second round before the parent ever saw the cancellation"
    );
}

#[tokio::test]
async fn delegated_usage_reports_reach_the_parent_event_stream() {
    // The accounting fix alone would leave a parent's observer blind to
    // delegated spend, since a subagent has its own event bus. Relaying the
    // child's `UsageReport` keeps a stream that sums usage agreeing with the
    // shared handle the budget is checked against.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            tool_use_stream_with_usage(
                &model.id,
                "parent-task",
                "task",
                r#"{"prompt":"delegate"}"#,
                usage(40, 20),
            ),
            text_stream_with_usage(&model.id, "child summary", usage(30, 20)),
            text_stream_with_usage(&model.id, "parent done", usage(5, 5)),
        ],
    );
    let runtime = Runtime::builder()
        .with_provider_instance(provider)
        .build()
        .expect("build runtime");
    let mut agent = runtime.spawn("agent", model).expect("spawn agent");
    let mut events = agent.subscribe_events();

    let options = RunOptions::default();
    let message = agent
        .run(vec![ContentBlock::text("delegate that")], options.clone())
        .await
        .expect("the run completes with no bound set");

    assert_eq!(message.text(), "parent done");
    let totals = reported_usage_totals(&collect_events(&mut events));
    assert_eq!(
        totals,
        vec![60, 50, 10],
        "the parent's stream carries the delegated round's usage between its own two rounds"
    );
    assert_eq!(
        totals.iter().sum::<u64>(),
        options.reported_tokens(),
        "what an observer sums from the stream matches what the budget is checked against"
    );
}

#[tokio::test]
async fn delegating_with_the_budget_already_spent_fails_the_delegation() {
    // A round is always allowed to finish, so the round that crosses the bound
    // can still be the one asking to delegate. The child then inherits an
    // already-exceeded budget and does zero rounds. That surfaces as a failed
    // delegation the parent can see, not as a silent empty success — and the
    // provider is never called on the child's behalf.
    let model = model_info("model", BuiltinProvider::Anthropic);
    let provider = ScriptedProvider::new(
        BuiltinProvider::Anthropic,
        vec![model.clone()],
        vec![
            // This single round spends the whole 100-token bound and delegates.
            tool_use_stream_with_usage(
                &model.id,
                "parent-task",
                "task",
                r#"{"prompt":"delegate"}"#,
                usage(60, 60),
            ),
            // Neither the child nor a second parent round may be requested.
            text_stream_with_usage(&model.id, "must not run", usage(1, 1)),
        ],
    );
    let provider_handle = provider.clone();
    let runtime = Runtime::builder()
        .with_provider_instance(provider)
        .build()
        .expect("build runtime");
    let mut agent = runtime.spawn("agent", model).expect("spawn agent");

    let result = agent
        .run(
            vec![ContentBlock::text("delegate that")],
            RunOptions {
                token_budget: Some(100),
                ..Default::default()
            },
        )
        .await;

    assert!(matches!(result, Err(RuntimeError::EmptyAssistantResponse)));
    assert_eq!(
        provider_handle.recorded_requests().await.len(),
        1,
        "the delegated run stopped at its first boundary without a model request"
    );
    let subagents = agent.watch_snapshot().borrow().subagents.clone();
    assert_eq!(subagents.len(), 1);
    assert!(
        matches!(
            &subagents[0].status,
            crate::agent::SpawnedAgentStatus::Failed(message)
                if message == "run completed without a final assistant message"
        ),
        "the exhausted delegation is recorded as failed, not finished: {:?}",
        subagents[0].status
    );
}