bamboo-engine 2026.10.19

Execution engine and orchestration for the Bamboo agent framework
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
//! Real FileStore + canonical SessionStore + actual activation admission.
//! The scripted provider checks the production decorator, without paid calls.
//! This is not evidence that the full Native Worker has run; its Host test is separate.
use super::*;
use crate::{runtime::config::AgentLoopConfig, ticket_token_budget::NativeWorkTokenBudget};
use bamboo_llm::{provider::ProviderModelInfo, LLMChunk, LLMError, LLMProvider, LLMStream};
use bamboo_tickets::{ExecutionTokenUsage, WorkTokenLimits};
use futures::StreamExt;
use std::sync::atomic::{AtomicUsize, Ordering};

mod terminal;

enum Script {
    Known,
    KnownText,
    DoneError,
    DoneOverCap,
    DonePending,
    RepeatedDone,
    NoDone,
    Partial,
    BootstrapError,
    StreamError,
    Pending,
    OverCap,
    PartialOverCap,
    InconsistentOverCap,
    LegacyCachedInputOverCap,
    LegacyCompositeInputOverCap,
    LegacyOutputOverCap,
    LegacyThinkingOverCap,
    LegacyOverflowOverCap,
    LegacyWithinCap,
    MixedKnown,
    CanonicalCacheOnlyOverCap,
    CanonicalReasoningOnlyOverCap,
    CanonicalTotalOnlyOverCap,
    LegacySplitInputOverCap,
    CanonicalSplitCacheOverCap,
    SplitTotalMismatch,
}
struct Provider {
    script: Script,
    calls: AtomicUsize,
}
impl Provider {
    fn new(script: Script) -> Arc<Self> {
        Arc::new(Self {
            script,
            calls: AtomicUsize::new(0),
        })
    }
}
fn usage(input: Option<u64>, output: Option<u64>) -> LLMChunk {
    LLMChunk::ProviderUsage {
        input_tokens: input,
        output_tokens: output,
        total_tokens: None,
        reasoning_tokens: Some(5),
        cache_creation_input_tokens: None,
        cache_read_input_tokens: Some(10),
        cache_write_input_tokens: None,
    }
}
#[async_trait::async_trait]
impl LLMProvider for Provider {
    async fn vision_support_override(&self, model: &str) -> Option<bool> {
        (model == "auxiliary-model").then_some(true)
    }
    async fn list_model_info(&self) -> bamboo_llm::provider::Result<Vec<ProviderModelInfo>> {
        Ok(vec![
            ProviderModelInfo {
                id: "fixture-model".into(),
                max_context_tokens: Some(100),
                max_output_tokens: Some(40),
            },
            ProviderModelInfo {
                id: "auxiliary-model".into(),
                max_context_tokens: Some(8192),
                max_output_tokens: Some(2048),
            },
        ])
    }
    async fn chat_stream(
        &self,
        _: &[bamboo_domain::Message],
        _: &[bamboo_domain::ToolSchema],
        cap: Option<u32>,
        model: &str,
    ) -> bamboo_llm::provider::Result<LLMStream> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        if model == "auxiliary-model" {
            assert!(cap.is_some_and(|cap| cap > 0 && cap <= 2048));
        } else {
            assert_eq!(cap, Some(40));
        }
        let mut complete = usage(Some(20), Some(u64::from(cap.unwrap_or(40).min(10))));
        if let LLMChunk::ProviderUsage {
            reasoning_tokens,
            cache_read_input_tokens,
            ..
        } = &mut complete
        {
            *reasoning_tokens = None;
            *cache_read_input_tokens = None;
        }
        let chunks = match self.script {
            Script::KnownText => vec![
                Ok(LLMChunk::Token("Fixture result".into())),
                Ok(complete.clone()),
                Ok(LLMChunk::Done),
                Ok(LLMChunk::Token("FORBIDDEN_LATE_TEXT".into())),
                Ok(LLMChunk::ReasoningToken("FORBIDDEN_LATE_REASONING".into())),
                Ok(LLMChunk::ReasoningSignature(
                    "FORBIDDEN_LATE_SIGNATURE".into(),
                )),
                Ok(LLMChunk::ResponseId("FORBIDDEN_LATE_ID".into())),
                Ok(LLMChunk::ResponsesEvent {
                    event_type: "late".into(),
                    data: Box::new(serde_json::json!({"late":true})),
                }),
                Ok(LLMChunk::ToolCalls(vec![])),
                Ok(complete),
                Ok(LLMChunk::Done),
            ],
            Script::DoneError => vec![
                Ok(complete),
                Ok(LLMChunk::Done),
                Err(LLMError::Stream("late failure".into())),
            ],
            Script::DoneOverCap => vec![
                Ok(complete),
                Ok(LLMChunk::Done),
                Ok(usage(Some(101), Some(10))),
            ],
            Script::NoDone => vec![
                Ok(LLMChunk::Token("Incomplete response".into())),
                Ok(complete),
            ],
            Script::DonePending | Script::RepeatedDone => {
                let prefix = futures::stream::iter(vec![
                    Ok(LLMChunk::Token("Fixture result".into())),
                    Ok(complete),
                    Ok(LLMChunk::Done),
                ]);
                return if matches!(self.script, Script::DonePending) {
                    Ok(Box::pin(prefix.chain(futures::stream::pending())))
                } else {
                    Ok(Box::pin(prefix.chain(futures::stream::repeat_with(|| {
                        Ok(LLMChunk::Done)
                    }))))
                };
            }
            Script::BootstrapError => {
                return Err(LLMError::Api("controlled bootstrap error".into()))
            }
            Script::Pending => return std::future::pending().await,
            Script::StreamError => vec![
                Ok(usage(Some(20), Some(10))),
                Err(LLMError::Stream("controlled stream error".into())),
            ],
            Script::Partial => vec![Ok(LLMChunk::Done), Ok(usage(None, Some(10)))],
            Script::OverCap => vec![Ok(usage(Some(101), Some(10)))],
            Script::PartialOverCap => vec![Ok(usage(Some(101), None))],
            Script::LegacySplitInputOverCap => vec![
                Ok(LLMChunk::CacheUsage {
                    input_tokens: 80,
                    cache_creation_input_tokens: 0,
                    cache_read_input_tokens: 10,
                }),
                Ok(LLMChunk::CacheUsage {
                    input_tokens: 0,
                    cache_creation_input_tokens: 0,
                    cache_read_input_tokens: 40,
                }),
            ],
            Script::CanonicalSplitCacheOverCap => {
                let mut first = usage(None, None);
                let mut second = usage(None, None);
                if let LLMChunk::ProviderUsage {
                    cache_creation_input_tokens,
                    ..
                } = &mut first
                {
                    *cache_creation_input_tokens = Some(80);
                }
                if let LLMChunk::ProviderUsage {
                    cache_read_input_tokens,
                    ..
                } = &mut second
                {
                    *cache_read_input_tokens = Some(40);
                }
                vec![Ok(first), Ok(second)]
            }
            Script::SplitTotalMismatch => {
                let mut total = usage(None, None);
                if let LLMChunk::ProviderUsage { total_tokens, .. } = &mut total {
                    *total_tokens = Some(100);
                }
                vec![Ok(usage(Some(20), Some(10))), Ok(LLMChunk::Done), Ok(total)]
            }
            Script::CanonicalCacheOnlyOverCap
            | Script::CanonicalReasoningOnlyOverCap
            | Script::CanonicalTotalOnlyOverCap => {
                let mut chunk = usage(None, None);
                if let LLMChunk::ProviderUsage {
                    cache_read_input_tokens,
                    reasoning_tokens,
                    total_tokens,
                    ..
                } = &mut chunk
                {
                    match self.script {
                        Script::CanonicalCacheOnlyOverCap => *cache_read_input_tokens = Some(101),
                        Script::CanonicalReasoningOnlyOverCap => *reasoning_tokens = Some(41),
                        Script::CanonicalTotalOnlyOverCap => *total_tokens = Some(141),
                        _ => unreachable!(),
                    }
                }
                vec![Ok(chunk)]
            }
            Script::LegacyCachedInputOverCap => vec![Ok(LLMChunk::CacheUsage {
                input_tokens: 0,
                cache_creation_input_tokens: 0,
                cache_read_input_tokens: 101,
            })],
            Script::LegacyCompositeInputOverCap => vec![Ok(LLMChunk::CacheUsage {
                input_tokens: 40,
                cache_creation_input_tokens: 40,
                cache_read_input_tokens: 40,
            })],
            Script::LegacyOverflowOverCap => vec![Ok(LLMChunk::CacheUsage {
                input_tokens: u64::MAX,
                cache_creation_input_tokens: 0,
                cache_read_input_tokens: 1,
            })],
            Script::LegacyOutputOverCap => vec![Ok(LLMChunk::UsageSummary {
                output_tokens: 41,
                thinking_tokens: 0,
            })],
            Script::LegacyThinkingOverCap => vec![Ok(LLMChunk::UsageSummary {
                output_tokens: 0,
                thinking_tokens: 41,
            })],
            Script::LegacyWithinCap => vec![
                Ok(LLMChunk::CacheUsage {
                    input_tokens: 60,
                    cache_creation_input_tokens: 10,
                    cache_read_input_tokens: 30,
                }),
                Ok(LLMChunk::UsageSummary {
                    output_tokens: 40,
                    thinking_tokens: 5,
                }),
                Ok(LLMChunk::Done),
            ],
            Script::MixedKnown => {
                let mut canonical = usage(Some(20), Some(10));
                if let LLMChunk::ProviderUsage {
                    cache_creation_input_tokens,
                    cache_read_input_tokens,
                    ..
                } = &mut canonical
                {
                    *cache_creation_input_tokens = Some(10);
                    *cache_read_input_tokens = Some(5);
                }
                vec![
                    Ok(LLMChunk::CacheUsage {
                        input_tokens: 5,
                        cache_creation_input_tokens: 10,
                        cache_read_input_tokens: 5,
                    }),
                    Ok(LLMChunk::UsageSummary {
                        output_tokens: 10,
                        thinking_tokens: 5,
                    }),
                    Ok(canonical),
                    Ok(LLMChunk::Done),
                ]
            }
            Script::InconsistentOverCap => {
                let mut chunk = usage(Some(101), Some(10));
                if let LLMChunk::ProviderUsage { total_tokens, .. } = &mut chunk {
                    *total_tokens = Some(1);
                }
                vec![Ok(chunk)]
            }
            Script::Known => vec![
                Ok(LLMChunk::Done),
                Ok(usage(Some(20), Some(10))),
                Ok(LLMChunk::Done),
            ],
        };
        Ok(Box::pin(futures::stream::iter(chunks)))
    }
}

async fn admitted() -> (Fixture, crate::session_activation::SessionRunRegistration) {
    let mut f = Fixture::with_project(true).await;
    let registration = f
        .runtime
        .router
        .register_run(&f.child.id, "budget-host-run")
        .await
        .unwrap();
    admit_ticket_run(&f.service, &f.runtime, &mut f.child, "budget-host-run")
        .await
        .unwrap();
    (f, registration)
}
fn configure(f: &Fixture, cap: u64) {
    let user = Authority::from_verified_host(
        f.spec.binding.clone(),
        Principal::User {
            user_id: "budget-owner".into(),
        },
    );
    f.service
        .configure_work_token_budget(
            &user,
            0,
            WorkTokenLimits {
                work_tokens: std::collections::BTreeMap::from([(f.spec.work_id.clone(), cap)]),
                utc_day_tokens: cap,
                total_tokens: cap,
            },
        )
        .unwrap();
}
fn ledger(f: &Fixture) -> bamboo_tickets::WorkTokenBudget {
    f.service
        .work_token_budget(&Authority::from_verified_host(
            f.spec.binding.clone(),
            Principal::Runtime,
        ))
        .unwrap()
        .unwrap()
}
async fn provider(f: &Fixture, inner: Arc<Provider>) -> Arc<dyn LLMProvider> {
    NativeWorkTokenBudget::capture(Some(f.service.clone()), f.storage.clone(), &f.child)
        .await
        .unwrap()
        .unwrap()
        .wrap(
            inner,
            &AgentLoopConfig {
                app_data_dir: Some(f.dir.path().to_path_buf()),
                ..Default::default()
            },
        )
}

#[tokio::test]
async fn token_budget_reserves_before_provider_and_settles_only_after_trailing_usage_eof() {
    let (f, _registration) = admitted().await;
    configure(&f, 140);
    let inner = Provider::new(Script::Known);
    let decorated = provider(&f, inner.clone()).await;
    assert_eq!(
        decorated.resource_allocation_identity(),
        Some(Arc::as_ptr(&inner) as *const () as usize)
    );
    let mut stream = decorated
        .chat_stream(&[], &[], None, "fixture-model")
        .await
        .unwrap();
    assert_eq!(inner.calls.load(Ordering::SeqCst), 1);
    assert_eq!(ledger(&f).total_charged, 140);
    assert!(matches!(
        stream.next().await.unwrap().unwrap(),
        LLMChunk::ProviderUsage { .. }
    ));
    assert_eq!(ledger(&f).total_charged, 140);
    assert!(matches!(
        stream.next().await.unwrap().unwrap(),
        LLMChunk::Done
    ));
    // Existing auxiliary consumers stop here; their charge is already settled.
    let budget = ledger(&f);
    assert_eq!(budget.total_charged, 30);
    let final_seq = f.service.published().unwrap().1.seq;
    assert!(stream.next().await.is_none());
    assert!(stream.next().await.is_none());
    assert_eq!(f.service.published().unwrap().1.seq, final_seq);
    let record = budget.reservations.values().next().unwrap();
    assert_eq!(
        record.request.attempt.as_ref().unwrap().assignment_id,
        f.spec.assignment_id
    );
    assert_eq!(record.request.project_id.as_str(), "budget-fixture-project");
    assert_eq!(
        record.usage,
        Some(ExecutionTokenUsage::Known {
            input_tokens: 20,
            output_tokens: 10
        })
    );
    assert!(decorated
        .chat_stream(&[], &[], None, "fixture-model")
        .await
        .is_err());
    assert_eq!(
        inner.calls.load(Ordering::SeqCst),
        1,
        "exhaustion blocks actual provider entry"
    );
}

#[tokio::test]
async fn token_budget_missing_error_cancel_and_dropped_stream_keep_the_full_charge() {
    for script in [
        Script::Partial,
        Script::BootstrapError,
        Script::StreamError,
        Script::Pending,
        Script::Known,
    ] {
        let (f, _registration) = admitted().await;
        configure(&f, 140);
        let inner = Provider::new(script);
        let decorated = provider(&f, inner.clone()).await;
        let result = tokio::time::timeout(
            std::time::Duration::from_millis(100),
            decorated.chat_stream(&[], &[], None, "fixture-model"),
        )
        .await;
        if let Ok(Ok(mut stream)) = result {
            if matches!(inner.script, Script::Known) {
                drop(stream);
            } else {
                while let Some(chunk) = stream.next().await {
                    if chunk.is_err() {
                        break;
                    }
                }
            }
        }
        assert_eq!(inner.calls.load(Ordering::SeqCst), 1);
        assert_eq!(ledger(&f).total_charged, 140);
        assert!(decorated
            .chat_stream(&[], &[], None, "fixture-model")
            .await
            .is_err());
        assert_eq!(inner.calls.load(Ordering::SeqCst), 1);
    }
}

#[tokio::test]
async fn token_budget_over_cap_report_halts_subsequent_provider_entry() {
    for script in [
        Script::OverCap,
        Script::PartialOverCap,
        Script::InconsistentOverCap,
        Script::LegacyCachedInputOverCap,
        Script::LegacyCompositeInputOverCap,
        Script::LegacyOutputOverCap,
        Script::LegacyThinkingOverCap,
        Script::LegacyOverflowOverCap,
        Script::CanonicalCacheOnlyOverCap,
        Script::CanonicalReasoningOnlyOverCap,
        Script::CanonicalTotalOnlyOverCap,
        Script::LegacySplitInputOverCap,
        Script::CanonicalSplitCacheOverCap,
    ] {
        let (f, _registration) = admitted().await;
        configure(&f, 500);
        let inner = Provider::new(script);
        let decorated = provider(&f, inner.clone()).await;
        let mut stream = decorated
            .chat_stream(&[], &[], None, "fixture-model")
            .await
            .unwrap();
        let mut saw_error = false;
        while let Some(chunk) = stream.next().await {
            if chunk.is_err() {
                saw_error = true;
                break;
            }
        }
        assert!(
            saw_error,
            "overrun must fail while consuming its real stream"
        );
        let budget = ledger(&f);
        assert_eq!(budget.total_charged, 140);
        assert!(budget
            .reservations
            .values()
            .next()
            .unwrap()
            .over_cap_usage
            .is_some());
        assert!(decorated
            .chat_stream(&[], &[], None, "fixture-model")
            .await
            .is_err());
        assert_eq!(inner.calls.load(Ordering::SeqCst), 1);
    }
}

#[tokio::test]
async fn token_budget_incomplete_or_inconsistent_usage_keeps_caps_without_double_counting() {
    for (script, charged, known) in [
        (Script::LegacyWithinCap, 140, false),
        (Script::MixedKnown, 30, true),
        (Script::SplitTotalMismatch, 140, false),
    ] {
        let (f, _registration) = admitted().await;
        configure(&f, 500);
        let decorated = provider(&f, Provider::new(script)).await;
        let mut stream = decorated
            .chat_stream(&[], &[], None, "fixture-model")
            .await
            .unwrap();
        while let Some(chunk) = stream.next().await {
            chunk.unwrap();
        }
        let budget = ledger(&f);
        assert_eq!(budget.total_charged, charged);
        let receipt = budget.reservations.values().next().unwrap();
        assert!(receipt.over_cap_usage.is_none());
        assert_eq!(
            matches!(receipt.usage, Some(ExecutionTokenUsage::Known { .. })),
            known
        );
    }
}

#[tokio::test]
async fn token_budget_dynamic_auxiliary_and_vision_cannot_escape_shared_exhaustion() {
    use crate::runtime::config::AuxiliaryModelConfig;
    let (f, _registration) = admitted().await;
    configure(&f, 140);
    let inner = Provider::new(Script::Known);
    let budget =
        NativeWorkTokenBudget::capture(Some(f.service.clone()), f.storage.clone(), &f.child)
            .await
            .unwrap()
            .unwrap();
    let dynamic: Arc<dyn LLMProvider> = inner.clone();
    let vision: Arc<dyn LLMProvider> = inner.clone();
    let mut config = AgentLoopConfig {
        app_data_dir: Some(f.dir.path().to_path_buf()),
        fast_model_provider: Some(inner.clone()),
        summarization_model_provider: Some(inner.clone()),
        auxiliary_model_resolver: Some(Arc::new(move || AuxiliaryModelConfig {
            background_model_provider: Some(dynamic.clone()),
            ..Default::default()
        })),
        vision_model_resolver: Some(Arc::new(move || {
            let vision = vision.clone();
            Box::pin(async move { Ok(bamboo_llm::ResolvedModel::new(vision, "fixture-model")) })
        })),
        ..Default::default()
    };
    let main = budget.decorate(&mut config, inner.clone());
    drop(
        main.chat_stream(&[], &[], None, "fixture-model")
            .await
            .unwrap(),
    );
    let late_aux = config.auxiliary_model_resolver.as_ref().unwrap()();
    let late_vision = config.vision_model_resolver.as_ref().unwrap()()
        .await
        .unwrap();
    for provider in [
        config.fast_model_provider.unwrap(),
        config.summarization_model_provider.unwrap(),
        late_aux.background_model_provider.unwrap(),
        late_vision.provider,
    ] {
        assert!(provider
            .chat_stream(&[], &[], None, "fixture-model")
            .await
            .is_err());
    }
    assert_eq!(inner.calls.load(Ordering::SeqCst), 1);
    assert_eq!(ledger(&f).total_charged, 140);
}

#[tokio::test]
async fn token_budget_unknown_model_and_revoked_root_block_before_provider() {
    let (f, _registration) = admitted().await;
    configure(&f, 500);
    let inner = Provider::new(Script::Known);
    let decorated = provider(&f, inner.clone()).await;
    assert!(decorated
        .chat_stream(&[], &[], None, "unknown-budget-model")
        .await
        .is_err());
    assert!(decorated
        .chat_stream(&[], &[], Some(41), "fixture-model")
        .await
        .is_err());
    // A valid historical service in another canonical namespace cannot lend
    // its binding to this Native ancestry, even when the Supervisor id matches.
    let foreign_storage = SessionStoreV2::new(f.dir.path().join("foreign-host"))
        .await
        .unwrap();
    let bootstrap = foreign_storage
        .get_or_create_default_supervisor("fixture-model")
        .await
        .unwrap();
    let (mut foreign_binding, _) = verified_scope_binding(&foreign_storage, &bootstrap.session_id)
        .await
        .unwrap();
    foreign_binding.binding_revision = 7;
    assert_eq!(
        foreign_binding.supervisor_session_id,
        f.spec.binding.supervisor_session_id
    );
    assert_ne!(foreign_binding.scope_id, f.spec.binding.scope_id);
    let foreign = Arc::new(
        TicketService::open(f.dir.path().join("foreign-tickets"), foreign_binding).unwrap(),
    );
    match NativeWorkTokenBudget::capture(Some(foreign), f.storage.clone(), &f.child).await {
        Err(Error::ScopeDenied(reason)) => assert_eq!(reason, "Supervisor scope binding changed"),
        _ => panic!("a foreign historical scope cannot authorize Native capture"),
    }
    // Thinking mode is not a Supervisor permission epoch. Genuine loss of
    // canonical Root authority must still close the retained provider.
    f.storage.delete_session(&f.supervisor.id).await.unwrap();
    assert!(decorated
        .chat_stream(&[], &[], None, "fixture-model")
        .await
        .is_err());
    assert_eq!(inner.calls.load(Ordering::SeqCst), 0);
    assert_eq!(ledger(&f).total_charged, 0);
}

#[tokio::test]
async fn token_budget_native_descendant_uses_original_work_and_rejects_lost_ancestry() {
    let (f, _registration) = admitted().await;
    configure(&f, 500);
    let mut descendant = Session::new_child_of(
        "native-grandchild",
        &f.child,
        "fixture-model",
        "nested work",
    );
    f.storage.save_session(&descendant).await.unwrap();
    let budget =
        NativeWorkTokenBudget::capture(Some(f.service.clone()), f.storage.clone(), &descendant)
            .await
            .unwrap()
            .unwrap();
    let inner = Provider::new(Script::Known);
    let decorated = budget.wrap(
        inner.clone(),
        &AgentLoopConfig {
            app_data_dir: Some(f.dir.path().to_path_buf()),
            ..Default::default()
        },
    );
    let mut stream = decorated
        .chat_stream(&[], &[], None, "fixture-model")
        .await
        .unwrap();
    while let Some(chunk) = stream.next().await {
        chunk.unwrap();
    }
    assert_eq!(ledger(&f).total_charged, 30);
    descendant.parent_session_id = Some(f.supervisor.id.clone());
    // Storage already refuses reparenting. Loss of the original canonical
    // child independently closes the provider capability without a bypass.
    assert!(f.storage.save_session(&descendant).await.is_err());
    f.storage.delete_session(&descendant.id).await.unwrap();
    assert!(decorated
        .chat_stream(&[], &[], None, "fixture-model")
        .await
        .is_err());
    assert_eq!(inner.calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn token_budget_unconfigured_ordinary_child_preserves_legacy_and_later_config_fails_closed() {
    let f = Fixture::with_project(true).await;
    let mut root = f
        .storage
        .load_root_authority(&f.supervisor.id)
        .await
        .unwrap()
        .unwrap();
    root.set_root_orchestration_only(false).unwrap();
    f.storage.save_session(&root).await.unwrap();
    let child = Session::new_child_of(
        "ordinary-no-budget",
        &root,
        "fixture-model",
        "ordinary child",
    );
    f.storage.save_session(&child).await.unwrap();
    let capability =
        NativeWorkTokenBudget::capture(Some(f.service.clone()), f.storage.clone(), &child)
            .await
            .unwrap()
            .unwrap();
    let inner = Provider::new(Script::Known);
    let decorated = capability.wrap(
        inner.clone(),
        &AgentLoopConfig {
            app_data_dir: Some(f.dir.path().to_path_buf()),
            ..Default::default()
        },
    );
    let mut stream = decorated
        .chat_stream(&[], &[], Some(40), "fixture-model")
        .await
        .unwrap();
    while let Some(chunk) = stream.next().await {
        chunk.unwrap();
    }
    assert!(f.service.published().unwrap().1.execution_budget.is_none());
    assert_eq!(inner.calls.load(Ordering::SeqCst), 1);
    configure(&f, 500);
    assert!(decorated
        .chat_stream(&[], &[], Some(40), "fixture-model")
        .await
        .is_err());
    assert_eq!(
        inner.calls.load(Ordering::SeqCst),
        1,
        "late configuration cannot escape the retained capability"
    );
    assert_eq!(ledger(&f).total_charged, 0);
}