bamboo-engine 2026.10.9

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
use std::sync::{
    atomic::{AtomicBool, AtomicUsize, Ordering},
    Arc, Mutex,
};

use async_trait::async_trait;
use bamboo_agent_core::tools::{
    FunctionCall, FunctionSchema, ToolCall, ToolExecutor, ToolResult, ToolSchema,
};
use bamboo_agent_core::{Message, Role, Session};
use bamboo_llm::{LLMChunk, LLMError, LLMProvider, LLMRequestOptions, LLMStream, PromptIR};
use futures::stream;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

use crate::runtime::config::{AgentLoopConfig, PromptMemoryFlags};
use crate::session_app::child_session::{
    apply_child_session_update, run_child_action, update_child_action, ChildRunnerInfo,
    ChildSessionEntry, ChildSessionError, ChildSessionPort, ChildSessionUpdate, DeleteChildResult,
};

fn hint_count(messages: &[Message]) -> usize {
    messages
        .iter()
        .filter(|message| {
            message
                .metadata
                .as_ref()
                .is_some_and(|metadata| metadata["runtime_kind"] == "observation_progress_hint")
        })
        .count()
}

fn provider_hint_count(messages: &[Message]) -> usize {
    // Advisory User context is model-only. Check actual guidance text.
    messages
        .iter()
        .filter(|message| message.role == Role::User)
        .map(|message| message.content.matches(HINT_TEXT).count())
        .sum()
}

const HINT_TEXT: &str = "The same successful file observations returned unchanged information for three consecutive rounds.";

struct ObservationProvider {
    calls: AtomicUsize,
    rounds: usize,
    tool_name: &'static str,
    call_namespace: usize,
    hint_counts: Mutex<Vec<usize>>,
    fail_on_hint_once: bool,
    hint_retry_failed: AtomicBool,
}

#[async_trait]
impl LLMProvider for ObservationProvider {
    async fn chat_stream_ir(
        &self,
        ir: &PromptIR,
        tools: &[ToolSchema],
        max_output_tokens: Option<u32>,
        model: &str,
        _: Option<&LLMRequestOptions>,
    ) -> Result<LLMStream, LLMError> {
        // Exercise the real Anthropic structured-system lowering, which ignores
        // ordinary System body messages when these system blocks are present.
        assert!(!ir.system_blocks.is_empty());
        let body = bamboo_llm::providers::anthropic::build_anthropic_request_with_cache_blocks(
            &ir.body_chat(),
            &ir.system_blocks,
            tools,
            model,
            max_output_tokens.unwrap_or(1024),
            true,
            None,
            None,
            Some(&ir.cache),
            false,
        );
        let wire_blocks = body["messages"]
            .as_array()
            .unwrap()
            .iter()
            .flat_map(|message| message["content"].as_array().unwrap())
            .collect::<Vec<_>>();
        let wire_count = wire_blocks
            .iter()
            .filter_map(|block| block["text"].as_str())
            .map(|text| text.matches(HINT_TEXT).count())
            .sum::<usize>();
        for (index, block) in wire_blocks.iter().enumerate() {
            if block["text"]
                .as_str()
                .is_some_and(|text| text.contains(HINT_TEXT))
            {
                assert!(index >= 2);
                for offset in [2, 1] {
                    assert_eq!(wire_blocks[index - offset]["type"], "tool_result");
                }
                let second_result_id = wire_blocks[index - 1]["tool_use_id"].as_str().unwrap();
                let pair_prefix = second_result_id.strip_suffix("-1").unwrap();
                assert_eq!(
                    wire_blocks[index - 2]["tool_use_id"],
                    format!("{pair_prefix}-0")
                );
            }
        }
        let flat = ir.flatten();
        assert_eq!(wire_count, provider_hint_count(&flat));
        self.chat_stream(&flat, tools, max_output_tokens, model)
            .await
    }

    async fn chat_stream(
        &self,
        messages: &[Message],
        _: &[ToolSchema],
        _: Option<u32>,
        _: &str,
    ) -> Result<LLMStream, LLMError> {
        let hints = provider_hint_count(messages);
        self.hint_counts.lock().unwrap().push(hints);
        if self.fail_on_hint_once
            && hints == 1
            && !self.hint_retry_failed.swap(true, Ordering::SeqCst)
        {
            return Err(LLMError::Api(
                "temporary provider failure before output".into(),
            ));
        }
        let round = self.calls.fetch_add(1, Ordering::SeqCst);
        let chunks = if round < self.rounds {
            vec![
                Ok(LLMChunk::ToolCalls(
                    (0..2)
                        .map(|offset| ToolCall {
                            id: format!("observation-{}-{round}-{offset}", self.call_namespace),
                            tool_type: "function".into(),
                            function: FunctionCall {
                                name: self.tool_name.into(),
                                arguments: if round % 2 == 0 {
                                    r#"{"path":"file","offset":1}"#.into()
                                } else {
                                    r#"{"offset":1,"path":"file"}"#.into()
                                },
                            },
                        })
                        .collect(),
                )),
                Ok(LLMChunk::Done),
            ]
        } else {
            vec![
                Ok(LLMChunk::Token("Done using the collected evidence.".into())),
                Ok(LLMChunk::Done),
            ]
        };
        Ok(Box::pin(stream::iter(chunks)))
    }
}

struct ObservationExecutor {
    calls: AtomicUsize,
    tool_name: &'static str,
    changed_round: Option<usize>,
}

#[async_trait]
impl ToolExecutor for ObservationExecutor {
    async fn execute(
        &self,
        _: &ToolCall,
    ) -> bamboo_agent_core::tools::executor::Result<ToolResult> {
        let call = self.calls.fetch_add(1, Ordering::SeqCst);
        let output = if self.changed_round == Some(call / 2) {
            "new evidence"
        } else {
            "same evidence"
        };
        Ok(ToolResult::text(true, output))
    }

    fn list_tools(&self) -> Vec<ToolSchema> {
        vec![ToolSchema {
            schema_type: "function".into(),
            function: FunctionSchema {
                name: self.tool_name.into(),
                description: "Observation progress probe".into(),
                parameters: serde_json::json!({"type":"object","properties":{}}),
            },
        }]
    }
}

async fn run_observation_loop(
    session: &mut Session,
    tool_name: &'static str,
    changed_round: Option<usize>,
) -> Vec<usize> {
    run_observation_loop_with_retry(session, tool_name, changed_round, false).await
}

async fn run_observation_loop_with_retry(
    session: &mut Session,
    tool_name: &'static str,
    changed_round: Option<usize>,
    fail_on_hint_once: bool,
) -> Vec<usize> {
    let provider = Arc::new(ObservationProvider {
        calls: AtomicUsize::new(0),
        rounds: 6,
        tool_name,
        call_namespace: session.messages.len(),
        hint_counts: Mutex::new(Vec::new()),
        fail_on_hint_once,
        hint_retry_failed: AtomicBool::new(false),
    });
    let executor = Arc::new(ObservationExecutor {
        calls: AtomicUsize::new(0),
        tool_name,
        changed_round,
    });
    let config = AgentLoopConfig {
        system_prompt: Some("Complete the bounded task using collected evidence.".into()),
        model_name: Some("model".into()),
        prompt_memory_flags: PromptMemoryFlags {
            project_prompt_injection: false,
            relevant_recall: false,
            relevant_recall_rerank: false,
            project_first_dream: false,
            ledger_agenda: false,
        },
        run_budget: bamboo_config::RunBudgetConfig {
            max_rounds: Some(10),
            ..Default::default()
        },
        ..Default::default()
    };
    let (tx, mut rx) = mpsc::channel(256);
    crate::runtime::runner::run_agent_loop_with_config(
        session,
        "Inspect the file and finish the task.".into(),
        tx,
        provider.clone(),
        executor.clone(),
        CancellationToken::new(),
        config,
    )
    .await
    .unwrap();
    assert_eq!(
        executor.calls.load(Ordering::SeqCst),
        12,
        "the hint never skips a real call"
    );
    let mut completes = 0;
    while let Ok(event) = rx.try_recv() {
        if matches!(event, bamboo_agent_core::AgentEvent::Complete { .. }) {
            completes += 1;
        }
    }
    assert_eq!(
        completes, 1,
        "the advisory leaves completion behavior unchanged"
    );
    let hints = provider.hint_counts.lock().unwrap().clone();
    hints
}

#[tokio::test]
async fn observation_progress_request_retry_keeps_the_same_one_shot_hint() {
    let mut session = Session::new("observation-progress-retry", "model");
    assert_eq!(
        run_observation_loop_with_retry(&mut session, "Read", None, true).await,
        [0, 0, 0, 1, 1, 0, 0, 0]
    );
    assert!(session
        .messages
        .iter()
        .all(|message| !message.content.contains(HINT_TEXT)));
    assert_eq!(hint_count(&session.messages), 0);
}

#[tokio::test]
async fn observation_progress_real_loop_hints_one_request_after_paired_results_and_resets_on_new_run(
) {
    let mut session = Session::new("observation-progress", "model");
    assert_eq!(
        run_observation_loop(&mut session, "Read", None).await,
        [0, 0, 0, 1, 0, 0, 0]
    );
    assert_eq!(hint_count(&session.messages), 0);
    assert!(session
        .messages
        .iter()
        .all(|message| !message.content.contains(HINT_TEXT)));
    assert_eq!(
        run_observation_loop(&mut session, "Read", None).await,
        [0, 0, 0, 1, 0, 0, 0]
    );
    assert_eq!(hint_count(&session.messages), 0);
    assert!(session
        .messages
        .iter()
        .all(|message| !message.content.contains(HINT_TEXT)));
}

/// Storage seam only; retry/update logic uses the production child actions.
struct ObservationChildPort(Mutex<Session>);

#[async_trait]
impl ChildSessionPort for ObservationChildPort {
    async fn load_root_session(&self, _: &str) -> Result<Session, ChildSessionError> {
        unreachable!("retry/update receives its verified parent")
    }

    async fn load_child_for_parent(
        &self,
        parent_id: &str,
        child_id: &str,
    ) -> Result<Session, ChildSessionError> {
        let child = self.0.lock().unwrap();
        assert_eq!(child.parent_session_id.as_deref(), Some(parent_id));
        assert_eq!(child.id, child_id);
        Ok(child.clone())
    }

    async fn save_child_session(&self, child: &mut Session) -> Result<(), ChildSessionError> {
        *self.0.lock().unwrap() = child.clone();
        Ok(())
    }

    async fn update_child_session(
        &self,
        parent_id: &str,
        child_id: &str,
        update: ChildSessionUpdate,
    ) -> Result<(Session, usize), ChildSessionError> {
        let mut child = self.0.lock().unwrap();
        assert_eq!(child.parent_session_id.as_deref(), Some(parent_id));
        assert_eq!(child.id, child_id);
        let removed = apply_child_session_update(&mut child, update)?;
        Ok((child.clone(), removed))
    }

    async fn save_child_session_authoritative_flags(
        &self,
        _: &mut Session,
    ) -> Result<(), ChildSessionError> {
        unreachable!("no resident reuse")
    }

    async fn is_child_running(&self, _: &str) -> bool {
        false
    }

    async fn list_children(&self, _: &str) -> Vec<ChildSessionEntry> {
        unreachable!("no listing")
    }

    async fn enqueue_child_run(&self, _: &Session, _: &Session) -> Result<(), ChildSessionError> {
        unreachable!("retry only prepares the next run")
    }

    async fn cancel_child_run_and_wait(&self, _: &str) -> Result<(), ChildSessionError> {
        unreachable!("no cancellation")
    }

    async fn delete_child_session(
        &self,
        _: &str,
        _: &str,
    ) -> Result<DeleteChildResult, ChildSessionError> {
        unreachable!("no deletion")
    }

    async fn get_child_runner_info(&self, _: &str) -> Option<ChildRunnerInfo> {
        unreachable!("no inspection")
    }

    async fn register_parent_wait_for_child(
        &self,
        _: &str,
        _: &str,
        _: Option<&str>,
    ) -> Result<(), ChildSessionError> {
        unreachable!("no parent wait")
    }

    async fn register_parent_wait_for_children(
        &self,
        _: &str,
        _: &[String],
        _: bamboo_domain::session::runtime_state::ChildWaitPolicy,
    ) -> Result<usize, ChildSessionError> {
        unreachable!("no parent wait")
    }

    async fn active_child_ids(&self, _: &str) -> Vec<String> {
        unreachable!("no child selection")
    }

    async fn find_resident_child(&self, _: &str, _: &str) -> Option<String> {
        unreachable!("no resident reuse")
    }

    async fn ensure_child_indexed(&self, _: &str) {
        unreachable!("no child creation")
    }
}

#[tokio::test]
async fn observation_progress_child_retry_and_update_keep_the_assignment_anchor() {
    let parent = Session::new("observation-parent", "model");
    let mut child = Session::new_child("observation-child", &parent.id, "model", "Inspect file");
    child
        .metadata
        .insert("responsibility".into(), "Inspect a bounded file".into());
    child
        .metadata
        .insert("subagent_type".into(), "worker".into());
    assert_eq!(
        run_observation_loop(&mut child, "Read", None).await,
        [0, 0, 0, 1, 0, 0, 0]
    );
    assert_eq!(hint_count(&child.messages), 0);
    assert!(child
        .messages
        .iter()
        .all(|message| !message.content.contains(HINT_TEXT)));
    let assignment_index = child
        .messages
        .iter()
        .rposition(|message| message.role == Role::User)
        .unwrap();
    let assignment = child.messages[assignment_index].clone();
    assert_eq!(assignment.content, "Inspect the file and finish the task.");
    child
        .metadata
        .insert("assignment_prompt".into(), assignment.content.clone());
    let tail_len = child.messages.len() - assignment_index - 1;
    assert!(
        tail_len > 1,
        "the real loop emitted tool history while giving the model a hint"
    );

    let retry_port = ObservationChildPort(Mutex::new(child.clone()));
    // SubAgent.run defaults to resetting after the latest user assignment.
    let result = run_child_action(&retry_port, &parent, child.id.clone(), None)
        .await
        .unwrap();
    assert_eq!(result["messages_removed"], tail_len);
    let retried = retry_port.0.lock().unwrap().clone();
    assert_eq!(retried.messages.len(), assignment_index + 1);
    assert_eq!(retried.messages.last().unwrap().id, assignment.id);
    assert_eq!(retried.messages.last().unwrap().content, assignment.content);
    assert_eq!(hint_count(&retried.messages), 0);

    let update_port = ObservationChildPort(Mutex::new(child.clone()));
    let result = update_child_action(
        &update_port,
        &parent.id,
        child.id.clone(),
        None,
        None,
        Some("Inspect the other file; preserve the bounded scope.".into()),
        None,
        None,
        None,
        None,
        false,
    )
    .await
    .unwrap();
    assert_eq!(result["messages_removed"], tail_len);
    let updated = update_port.0.lock().unwrap();
    assert_eq!(updated.messages.len(), assignment_index + 1);
    let updated_assignment = updated.messages.last().unwrap();
    assert_eq!(updated_assignment.id, assignment.id);
    assert_eq!(updated_assignment.role, Role::User);
    assert!(updated_assignment
        .content
        .contains("Inspect the other file; preserve the bounded scope."));
    assert_eq!(hint_count(&updated.messages), 0);
    assert!(updated
        .messages
        .iter()
        .all(|message| message.role != Role::Tool));
}

#[tokio::test]
async fn observation_progress_real_loop_resets_when_output_changes_and_leaves_polling_alone() {
    let mut session = Session::new("observation-progress-change", "model");
    assert_eq!(
        run_observation_loop(&mut session, "Read", Some(2)).await,
        [0, 0, 0, 0, 0, 0, 1]
    );
    let mut polling = Session::new("observation-progress-polling", "model");
    assert_eq!(
        run_observation_loop(&mut polling, "BashOutput", None).await,
        [0; 7]
    );
    let mut mutation = Session::new("observation-progress-mutation", "model");
    assert_eq!(
        run_observation_loop(&mut mutation, "Write", None).await,
        [0; 7]
    );
}