bamboo-engine 2026.10.14

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
mod background_outcomes {
    use super::*;
    use crate::runtime::runner::state_bridge::refresh_turn_boundary_with_inbox_for_run;
    use bamboo_domain::RuntimeSessionPersistence;

    async fn active_parent(
        store: &bamboo_storage::SessionStoreV2,
        supervisor: bool,
        ultra: bool,
    ) -> Session {
        let mut parent = if supervisor {
            let reference = store
                .get_or_create_default_supervisor("model")
                .await
                .unwrap();
            store
                .load_session(&reference.session_id)
                .await
                .unwrap()
                .unwrap()
        } else {
            Session::new("background-parent", "model")
        };
        parent.set_root_orchestration_only(ultra).unwrap();
        parent.add_message(Message::user(
            "Read the assigned fixture and report its evidence",
        ));
        let mut runtime = AgentRuntimeState::new("active-parent-run");
        runtime.status = AgentStatusState::Running;
        write_runtime_state(&mut parent, &runtime);
        store.save_session(&parent).await.unwrap();
        parent
    }

    async fn admit_at_safe_point(
        coordinator: &ChildCompletionCoordinator,
        inbox: &Arc<dyn SessionInboxPort>,
        parent: &mut Session,
        run_id: &str,
    ) -> u64 {
        let refresh = refresh_turn_boundary_with_inbox_for_run(
            parent,
            Some(coordinator.agent.storage()),
            Some(coordinator.agent.persistence()),
            Some(inbox),
            Some(run_id),
        )
        .await;
        assert!(refresh.admission_error.is_none(), "{refresh:?}");
        assert_eq!(
            refresh.merged, 1,
            "the next safe point must receive this outcome"
        );
        parent
            .session_inbox_admission()
            .unwrap()
            .last_admitted_sequence
    }

    #[tokio::test]
    async fn background_completion_is_consumed_before_summary_without_a_successor() {
        for supervisor in [false, true] {
            for ultra in [false, true] {
                for prose in [false, true] {
                    let (_home, store, inbox, coordinator, reservations, launches) =
                        completion_inbox_fixture().await;
                    let mut parent = active_parent(&store, supervisor, ultra).await;
                    let report = if prose {
                        "The fixture names apple and pear.".to_string()
                    } else {
                        typed_test_report().to_string()
                    };
                    let child = typed_test_child(&parent, "background-child", &report);
                    store.save_session(&child).await.unwrap();
                    let completion = typed_test_completion(&child);
                    let router = coordinator.agent.activation_router().unwrap();
                    router.set_inbox(inbox.clone());
                    let mut owner = router
                        .register_run(&parent.id, "parent-turn")
                        .await
                        .unwrap();

                    coordinator.on_child_completed(completion.clone()).await;
                    let pending = inbox
                        .pending_child_outcomes(&parent.id, &child.id)
                        .await
                        .unwrap();
                    assert_eq!(pending.len(), 1);
                    let envelope = pending[0].clone();
                    assert!(!envelope.child_outcome_waits_for_run("parent-turn"));
                    let generation =
                        admit_at_safe_point(&coordinator, &inbox, &mut parent, "parent-turn").await;
                    let notice = parent.messages.last().unwrap();
                    assert!(bamboo_domain::is_matching_session_message(
                        notice, &envelope
                    ));
                    assert_eq!(notice.content.contains("report_malformed"), prose);
                    assert!(notice.content.contains("Child typed result:"));
                    assert!(inbox.was_admitted(&parent.id, &envelope.id).await.unwrap());
                    parent.add_message(Message::assistant("Summary after the child outcome", None));
                    coordinator
                        .persistence
                        .save_runtime_session(&mut parent)
                        .await
                        .unwrap();

                    // A repeated callback after durable admission cannot recreate
                    // the notification or schedule an otherwise empty successor.
                    coordinator.on_child_completed(completion.clone()).await;
                    owner.begin_finalization().await;
                    assert!(owner.finish(generation).await.unwrap().is_none());
                    assert!(router.current_run_id(&parent.id).await.is_none());
                    coordinator.on_child_completed(completion).await;
                    assert_eq!(inbox.inspect(&parent.id).await.unwrap().pending, 0);
                    assert_eq!(reservations.load(Ordering::SeqCst), 0);
                    assert_eq!(launches.load(Ordering::SeqCst), 0);
                    let saved = store.load_session(&parent.id).await.unwrap().unwrap();
                    assert_eq!(
                        saved
                            .messages
                            .iter()
                            .filter(|message| bamboo_domain::is_matching_session_message(
                                message, &envelope
                            ))
                            .count(),
                        1
                    );
                    assert_eq!(
                        saved.messages.last().unwrap().content,
                        "Summary after the child outcome"
                    );
                    assert_eq!(saved.supervisor_incarnation_id().is_some(), supervisor);
                    assert_eq!(saved.root_orchestration_only_enabled(), ultra);
                }
            }
        }
    }

    #[tokio::test]
    async fn background_completion_remains_deliverable_after_inbox_reopen() {
        let (home, store, inbox, coordinator, reservations, launches) =
            completion_inbox_fixture().await;
        let mut parent = active_parent(&store, false, false).await;
        let completion =
            save_completion_test_child(&store, &parent.id, "reopened-child", "completed").await;
        let router = coordinator.agent.activation_router().unwrap();
        router.set_inbox(inbox.clone());
        let mut owner = router
            .register_run(&parent.id, "parent-turn")
            .await
            .unwrap();
        coordinator.on_child_completed(completion.clone()).await;

        let reopened_store = Arc::new(
            bamboo_storage::SessionStoreV2::new(home.path().to_path_buf())
                .await
                .unwrap(),
        );
        let reopened: Arc<dyn SessionInboxPort> = Arc::new(bamboo_storage::FileSessionInbox::new(
            reopened_store,
            Default::default(),
        ));
        let generation =
            admit_at_safe_point(&coordinator, &reopened, &mut parent, "parent-turn").await;
        coordinator.on_child_completed(completion).await;
        owner.begin_finalization().await;
        assert!(owner.finish(generation).await.unwrap().is_none());
        assert_eq!(reopened.inspect(&parent.id).await.unwrap().pending, 0);
        assert_eq!(reservations.load(Ordering::SeqCst), 0);
        assert_eq!(launches.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn background_completion_after_last_safe_point_still_gets_one_successor() {
        let (_home, store, inbox, coordinator, reservations, launches) =
            completion_inbox_fixture().await;
        let mut parent = active_parent(&store, false, false).await;
        parent.add_message(Message::assistant("Earlier work is complete", None));
        store.save_session(&parent).await.unwrap();
        let completion =
            save_completion_test_child(&store, &parent.id, "late-background-child", "completed")
                .await;
        let router = coordinator.agent.activation_router().unwrap();
        router.set_inbox(inbox.clone());
        let mut owner = router
            .register_run(&parent.id, "parent-turn")
            .await
            .unwrap();
        owner.begin_finalization().await;

        coordinator.on_child_completed(completion.clone()).await;
        assert_eq!(reservations.load(Ordering::SeqCst), 0);
        owner.finish(0).await.unwrap();
        assert_eq!(reservations.load(Ordering::SeqCst), 1);
        assert_eq!(launches.load(Ordering::SeqCst), 1);
        let successor = router.current_run_id(&parent.id).await.unwrap();
        let generation = admit_at_safe_point(&coordinator, &inbox, &mut parent, &successor).await;
        assert!(parent
            .messages
            .last()
            .unwrap()
            .content
            .contains("late-background-child"));
        coordinator.on_child_completed(completion).await;
        router.begin_finalization(&parent.id, &successor).await;
        assert!(router
            .finish_finalization(&parent.id, &successor, generation)
            .await
            .unwrap()
            .is_none());
        assert_eq!(reservations.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn background_outcome_does_not_steal_an_unrelated_child_wait() {
        for policy in [
            ChildWaitPolicy::All,
            ChildWaitPolicy::Any,
            ChildWaitPolicy::FirstError,
        ] {
            let (_home, store, inbox, coordinator, reservations, launches) =
                completion_inbox_fixture().await;
            let parent_id = "unrelated-wait-parent";
            save_waiting_test_parent(
                &store,
                parent_id,
                vec!["still-running-child".into()],
                policy,
            )
            .await;
            save_completion_test_child(&store, parent_id, "still-running-child", "running").await;
            let completion =
                save_completion_test_child(&store, parent_id, "background-child", "completed")
                    .await;
            let before = store.load_session(parent_id).await.unwrap().unwrap();
            let router = coordinator.agent.activation_router().unwrap();
            router.set_inbox(inbox.clone());
            let mut owner = router.register_run(parent_id, "parent-turn").await.unwrap();

            coordinator.on_child_completed(completion).await;
            assert!(inbox
                .claim_for_turn(parent_id, 8, Some("parent-turn"))
                .await
                .unwrap()
                .is_empty());
            let after = store.load_session(parent_id).await.unwrap().unwrap();
            assert_eq!(read_runtime_state(&before), read_runtime_state(&after));
            assert_eq!(after.updated_at, before.updated_at);
            owner.begin_finalization().await;
            owner.finish(0).await.unwrap();
            assert_eq!(reservations.load(Ordering::SeqCst), 1);
            assert_eq!(launches.load(Ordering::SeqCst), 1);
            let successor = router.current_run_id(parent_id).await.unwrap();
            let claims = inbox
                .claim_for_turn(parent_id, 8, Some(&successor))
                .await
                .unwrap();
            assert_eq!(claims.len(), 1);
            assert_eq!(
                read_runtime_state(&store.load_session(parent_id).await.unwrap().unwrap()),
                read_runtime_state(&before)
            );
        }
    }

    #[tokio::test]
    async fn waited_completion_keeps_its_successor_fence_after_clearing_wait() {
        for (policy, status) in [
            (ChildWaitPolicy::All, "completed"),
            (ChildWaitPolicy::Any, "completed"),
            (ChildWaitPolicy::FirstError, "error"),
        ] {
            let (_home, store, inbox, coordinator, reservations, launches) =
                completion_inbox_fixture().await;
            let parent_id = "waited-parent";
            save_waiting_test_parent(&store, parent_id, vec!["waited-child".into()], policy).await;
            let completion =
                save_completion_test_child(&store, parent_id, "waited-child", status).await;
            let router = coordinator.agent.activation_router().unwrap();
            router.set_inbox(inbox.clone());
            let mut owner = router.register_run(parent_id, "parent-turn").await.unwrap();

            coordinator.on_child_completed(completion.clone()).await;
            // Retrying after the wait was cleared reuses the original durable
            // envelope; the new no-wait observation cannot remove its fence.
            coordinator.on_child_completed(completion).await;
            assert!(
                read_runtime_state(&store.load_session(parent_id).await.unwrap().unwrap())
                    .waiting_for_children
                    .is_none()
            );
            assert!(inbox
                .claim_for_turn(parent_id, 8, Some("parent-turn"))
                .await
                .unwrap()
                .is_empty());
            owner.begin_finalization().await;
            owner.finish(0).await.unwrap();
            assert_eq!(reservations.load(Ordering::SeqCst), 1);
            assert_eq!(launches.load(Ordering::SeqCst), 1);
            let successor = router.current_run_id(parent_id).await.unwrap();
            let claims = inbox
                .claim_for_turn(parent_id, 8, Some(&successor))
                .await
                .unwrap();
            assert_eq!(claims.len(), 1);
            assert!(claims[0]
                .envelope
                .child_outcome_waits_for_run("parent-turn"));
        }
    }

    #[tokio::test]
    async fn released_wait_keeps_late_siblings_on_the_same_owners_successor() {
        for (policy, first_status) in [
            (ChildWaitPolicy::Any, "completed"),
            (ChildWaitPolicy::FirstError, "error"),
        ] {
            let (_home, store, inbox, coordinator, reservations, launches) =
                completion_inbox_fixture().await;
            let parent_id = "released-wait-parent";
            save_waiting_test_parent(
                &store,
                parent_id,
                vec!["first-child".into(), "late-sibling".into()],
                policy,
            )
            .await;
            save_completion_test_child(&store, parent_id, "late-sibling", "running").await;
            let first =
                save_completion_test_child(&store, parent_id, "first-child", first_status).await;
            let router = coordinator.agent.activation_router().unwrap();
            router.set_inbox(inbox.clone());
            let mut owner = router.register_run(parent_id, "parent-turn").await.unwrap();

            coordinator.on_child_completed(first).await;
            assert!(
                read_runtime_state(&store.load_session(parent_id).await.unwrap().unwrap())
                    .waiting_for_children
                    .is_none()
            );
            // The durable wait is gone, but the current reasoning owner still
            // predates its release. A later sibling must not overtake the first
            // outcome merely because a fresh control-plane read now says None.
            let late =
                save_completion_test_child(&store, parent_id, "late-sibling", "completed").await;
            coordinator.on_child_completed(late).await;
            assert!(inbox
                .claim_for_turn(parent_id, 8, Some("parent-turn"))
                .await
                .unwrap()
                .is_empty());
            owner.begin_finalization().await;
            owner.finish(0).await.unwrap();
            assert_eq!(reservations.load(Ordering::SeqCst), 1);
            assert_eq!(launches.load(Ordering::SeqCst), 1);
            let successor = router.current_run_id(parent_id).await.unwrap();
            let claims = inbox
                .claim_for_turn(parent_id, 8, Some(&successor))
                .await
                .unwrap();
            assert_eq!(claims.len(), 2);
            assert!(claims
                .iter()
                .all(|claim| claim.envelope.child_outcome_waits_for_run("parent-turn")));
        }
    }

    #[tokio::test]
    async fn guardian_control_without_a_child_wait_still_waits_for_successor() {
        let (_home, store, inbox, coordinator, reservations, launches) =
            completion_inbox_fixture().await;
        let mut parent = active_parent(&store, false, false).await;
        let mut guardian = crate::runtime::guardian_state::ensure_guardian_state(&parent);
        guardian.record_spawn("guardian-child");
        write_guardian_state(&mut parent, guardian);
        store.save_session(&parent).await.unwrap();
        let mut child = Session::new_child("guardian-child", &parent.id, "model", "Guardian");
        child
            .metadata
            .insert("subagent_type".into(), "guardian".into());
        child.add_message(Message::assistant(
            r#"{"approve":true,"findings":[]}"#,
            None,
        ));
        child.set_last_run_status("completed");
        save_sourced_test_child(&store, &child).await;
        let completion = ChildCompletion {
            source: test_child_source(&store, &child.id).await,
            ..typed_test_completion(&child)
        };
        let router = coordinator.agent.activation_router().unwrap();
        router.set_inbox(inbox.clone());
        let mut owner = router
            .register_run(&parent.id, "parent-turn")
            .await
            .unwrap();

        coordinator.on_child_completed(completion).await;
        assert!(
            read_guardian_state(&store.load_session(&parent.id).await.unwrap().unwrap())
                .unwrap()
                .phase
                .is_reviewed()
        );
        assert!(inbox
            .claim_for_turn(&parent.id, 8, Some("parent-turn"))
            .await
            .unwrap()
            .is_empty());
        let background =
            save_completion_test_child(&store, &parent.id, "after-guardian-child", "completed")
                .await;
        coordinator.on_child_completed(background).await;
        assert!(inbox
            .claim_for_turn(&parent.id, 8, Some("parent-turn"))
            .await
            .unwrap()
            .is_empty());
        owner.begin_finalization().await;
        owner.finish(0).await.unwrap();
        assert_eq!(reservations.load(Ordering::SeqCst), 1);
        assert_eq!(launches.load(Ordering::SeqCst), 1);
        let successor = router.current_run_id(&parent.id).await.unwrap();
        let claims = inbox
            .claim_for_turn(&parent.id, 8, Some(&successor))
            .await
            .unwrap();
        assert_eq!(claims.len(), 2);
        assert!(claims
            .iter()
            .all(|claim| claim.envelope.child_outcome_waits_for_run("parent-turn")));
    }

    #[tokio::test]
    async fn background_source_rejection_has_no_delivery_or_parent_state_effect() {
        for foreign in [false, true] {
            let (_home, store, inbox, coordinator, reservations, launches) =
                completion_inbox_fixture().await;
            let parent = active_parent(&store, false, false).await;
            let child_parent = if foreign {
                "foreign-parent"
            } else {
                &parent.id
            };
            if foreign {
                store
                    .save_session(&Session::new(child_parent, "model"))
                    .await
                    .unwrap();
            }
            let mut completion =
                save_completion_test_child(&store, child_parent, "rejected-child", "completed")
                    .await;
            completion.parent_session_id = parent.id.clone();
            if !foreign {
                let mut successor = store.load_session("rejected-child").await.unwrap().unwrap();
                successor.add_message(Message::assistant("Newer child result", None));
                ChildCompletionSource::prepare(
                    &mut successor,
                    "newer-child-run",
                    &Default::default(),
                );
                store.save_session(&successor).await.unwrap();
            }
            let before =
                serde_json::to_value(store.load_session(&parent.id).await.unwrap().unwrap())
                    .unwrap();
            let router = coordinator.agent.activation_router().unwrap();
            router.set_inbox(inbox.clone());
            let mut owner = router
                .register_run(&parent.id, "parent-turn")
                .await
                .unwrap();

            coordinator.on_child_completed(completion).await;
            assert_eq!(inbox.inspect(&parent.id).await.unwrap().pending, 0);
            assert_eq!(
                serde_json::to_value(store.load_session(&parent.id).await.unwrap().unwrap())
                    .unwrap(),
                before
            );
            owner.begin_finalization().await;
            assert!(owner.finish(0).await.unwrap().is_none());
            assert_eq!(reservations.load(Ordering::SeqCst), 0);
            assert_eq!(launches.load(Ordering::SeqCst), 0);
        }
    }
}