bamboo-tools 2026.10.13

Tool execution and integrations 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
//! Typed, stateless commands for the current session's bounded loop.
//!
//! This tool validates the command only. The owning serial runner checks
//! authority, host ceilings and compare-and-swap state, then persists the
//! operation before replacing this placeholder with a durable receipt.

use async_trait::async_trait;
use bamboo_agent_core::{
    Tool, ToolClass, ToolCtx, ToolError, ToolMutability, ToolOutcome, ToolResult,
};
use serde_json::json;

pub const LOOP_CONTROL_TOOL_NAME: &str = "loop_control";
use bamboo_domain::session::loop_control_request::{
    MAX_ITERATIONS, MAX_OBJECTIVE_BYTES, MAX_TOTAL_ROUNDS,
};
pub use bamboo_domain::{parse_loop_control_request, LoopControlRequest};

/// Validation-only front end; the session owner applies every command serially.
pub struct LoopControlTool;

impl LoopControlTool {
    pub fn new() -> Self {
        Self
    }
}

impl Default for LoopControlTool {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Tool for LoopControlTool {
    fn name(&self) -> &str {
        LOOP_CONTROL_TOOL_NAME
    }

    fn description(&self) -> &str {
        "Read or control this session's bounded loop only when the host explicitly enabled ExecuteRequest.loop_control(config). \
         status requires only action. start requires operation_id (UUID), expected_revision, objective, max_iterations and max_total_rounds; expected_generation is optional. \
         update requires operation_id, generation (UUID), expected_revision and at least one of objective/max_iterations/max_total_rounds. stop requires operation_id, generation and expected_revision. \
         Mutations use generation/control-revision compare-and-swap and an idempotent operation_id; read status before constructing a command. Receipts are bounded to 64 operations in the current generation; retired generations are stale. \
         Host ceilings, consumed budgets and user cancellation always take priority: a command cannot grant authority, overlap execution, restore a stopped run or increase the host's finite limits. \
         Updates may only tighten current limits without dropping below consumption. A new objective applies no earlier than the next iteration admission; only one pending objective is allowed. A pending objective needs one future iteration and at least two remaining rounds during an active iteration (its terminal answer, then the new objective), or one before the first iteration. max_total_rounds is the absolute parent-round cap for this entire activation, including rounds already consumed before start, and grants no new rounds. Start is only for work that can complete without human questions, new approvals, asynchronous child work or background Bash; incompatible waits are refused without approval or a waiter. Unexpected detached tool execution aborts the generation with stop unconfirmed; a previously observed Running in this activation also refuses Start. Interactive work requires a later ordinary activation outside this generation. This tool alone validates a command; only a durable owner receipt confirms application. No pause, resume, cadence or other-session target is supported."
    }

    fn classify(&self, args: &serde_json::Value) -> ToolClass {
        match parse_loop_control_request(&args.to_string()) {
            Ok(LoopControlRequest::Status) => ToolClass {
                mutability: ToolMutability::ReadOnly,
                parallel_safe: false,
                promotable: false,
            },
            _ => ToolClass::MUTATING_SERIAL,
        }
    }

    fn parameters_schema(&self) -> serde_json::Value {
        json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["status", "start", "update", "stop"],
                    "description": "status: no other fields. start: operation_id, expected_revision, objective, max_iterations, max_total_rounds; optional expected_generation. update: operation_id, generation, expected_revision, and at least one patch field. stop: operation_id, generation, expected_revision."
                },
                "operation_id": {
                    "type": "string",
                    "format": "uuid",
                    "description": "Required for start/update/stop. Reuse only for an identical command in the current generation; the owner checks idempotency before CAS. At most 64 operation receipts; retired generations are stale."
                },
                "expected_generation": {
                    "type": ["string", "null"],
                    "format": "uuid",
                    "description": "start only: expected prior generation UUID, or absent/null if none. The owner verifies current generation and revision."
                },
                "generation": {
                    "type": "string",
                    "format": "uuid",
                    "description": "Required for update/stop: exact current generation from status. Retired generations are stale; stop cannot be undone by start."
                },
                "expected_revision": {
                    "type": "integer",
                    "minimum": 0,
                    "description": "Required for start/update/stop: exact current control revision from status, independent of ordinary checkpoint state revisions. Stale commands are refused."
                },
                "objective": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": MAX_OBJECTIVE_BYTES,
                    "description": "Required for start; optional update patch. Non-whitespace text, at most 16384 UTF-8 bytes. An edit applies no earlier than the next iteration admission; at most one objective edit may be pending."
                },
                "max_iterations": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": MAX_ITERATIONS,
                    "description": "Required for start; optional update patch. Total iteration limit within the immutable host ceiling. Updates may only tighten the current limit and cannot drop below consumed iterations."
                },
                "max_total_rounds": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": MAX_TOTAL_ROUNDS,
                    "description": "Required for start; optional update patch. Absolute parent-round cap for the entire current activation, including rounds consumed before start; never an additional allowance. Within immutable host ceilings; updates may only tighten and cannot drop below consumption."
                }
            },
            "required": ["action"],
            "additionalProperties": false
        })
    }

    async fn invoke(
        &self,
        args: serde_json::Value,
        ctx: ToolCtx,
    ) -> Result<ToolOutcome, ToolError> {
        let request =
            parse_loop_control_request(&args.to_string()).map_err(ToolError::InvalidArguments)?;
        let session_id = ctx
            .session_id()
            .filter(|value| !value.trim().is_empty())
            .ok_or_else(|| {
                ToolError::Execution("loop_control requires the current session context".into())
            })?;
        if ctx.plan_read_only && !matches!(request, LoopControlRequest::Status) {
            return Err(ToolError::Execution(
                "Plan mode: loop_control mutation blocked".into(),
            ));
        }
        Ok(ToolOutcome::Completed(ToolResult {
            success: true,
            result: json!({
                "action": request.action(),
                "session_id": session_id,
                "status": "validated",
                "applied": false,
                "requires_owner_apply": true,
                "message": "loop_control command validated only; the current session runner must apply it and return a durable receipt before ToolComplete."
            })
            .to_string(),
            display_preference: Some("json".into()),
            images: Vec::new(),
        }))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    const OPERATION_ID: &str = "A4B85B5B-BA44-488D-9900-20DD7E016E88";
    const GENERATION: &str = "b23bc011-5424-4b5e-8fc9-3c861b89874b";

    fn start() -> serde_json::Value {
        json!({
            "action": "start",
            "operation_id": OPERATION_ID,
            "expected_revision": 0,
            "objective": "  Verify the bounded loop  ",
            "max_iterations": 2,
            "max_total_rounds": 4
        })
    }

    fn update() -> serde_json::Value {
        json!({
            "action": "update",
            "operation_id": OPERATION_ID,
            "generation": GENERATION,
            "expected_revision": 1,
            "objective": "Use the updated objective"
        })
    }

    fn stop() -> serde_json::Value {
        json!({
            "action": "stop",
            "operation_id": OPERATION_ID,
            "generation": GENERATION,
            "expected_revision": 2
        })
    }

    fn context() -> ToolCtx {
        let mut context = ToolCtx::none("loop-control-test");
        context.session_id = Some(Arc::from("current-session"));
        context
    }

    #[test]
    fn parses_all_actions_and_preserves_fingerprint_inputs() {
        assert_eq!(
            parse_loop_control_request(r#"{"action":"status"}"#).unwrap(),
            LoopControlRequest::Status
        );
        let parsed = parse_loop_control_request(&start().to_string()).unwrap();
        assert!(matches!(
            &parsed,
            LoopControlRequest::Start {
                operation_id,
                expected_generation: None,
                objective,
                ..
            } if operation_id == OPERATION_ID && objective == "  Verify the bounded loop  "
        ));
        assert_eq!(serde_json::to_value(&parsed).unwrap()["action"], "start");
        assert_eq!(
            serde_json::from_str::<LoopControlRequest>(&serde_json::to_string(&parsed).unwrap())
                .unwrap(),
            parsed
        );
        assert!(matches!(
            parse_loop_control_request(&update().to_string()).unwrap(),
            LoopControlRequest::Update {
                max_iterations: None,
                max_total_rounds: None,
                ..
            }
        ));
        assert!(matches!(
            parse_loop_control_request(&stop().to_string()).unwrap(),
            LoopControlRequest::Stop { .. }
        ));
    }

    #[test]
    fn rejects_unknown_fields_for_every_action_including_direct_deserialization() {
        for mut args in [json!({"action": "status"}), start(), update(), stop()] {
            args["session_id"] = json!("another-session");
            assert!(parse_loop_control_request(&args.to_string()).is_err());
            assert!(serde_json::from_value::<LoopControlRequest>(args).is_err());
        }
        let mut args = start();
        args["generation"] = json!(GENERATION);
        assert!(parse_loop_control_request(&args.to_string()).is_err());
        let mut args = stop();
        args["objective"] = json!("not a stop field");
        assert!(parse_loop_control_request(&args.to_string()).is_err());
    }

    #[test]
    fn rejects_missing_fields_unknown_actions_and_wrong_types() {
        for field in [
            "operation_id",
            "expected_revision",
            "objective",
            "max_iterations",
            "max_total_rounds",
        ] {
            let mut args = start();
            args.as_object_mut().unwrap().remove(field);
            assert!(
                parse_loop_control_request(&args.to_string()).is_err(),
                "{field}"
            );
        }
        for args in [
            json!({}),
            json!({"action": "Status"}),
            json!({"action": "pause"}),
            json!({"action": "resume"}),
            json!({"action": 1}),
            json!(null),
            json!([]),
        ] {
            assert!(parse_loop_control_request(&args.to_string()).is_err());
        }
        for value in [json!(-1), json!(1.5), json!("1"), json!(null)] {
            let mut args = start();
            args["expected_revision"] = value;
            assert!(parse_loop_control_request(&args.to_string()).is_err());
        }
    }

    #[test]
    fn rejects_duplicate_fields_and_trailing_input() {
        for arguments in [
            r#"{"action":"status","action":"status"}"#,
            r#"{"action":"status"} {}"#,
            r#"{"action":"stop","operation_id":"A4B85B5B-BA44-488D-9900-20DD7E016E88","generation":"b23bc011-5424-4b5e-8fc9-3c861b89874b","expected_revision":1,"expected_revision":2}"#,
        ] {
            assert!(parse_loop_control_request(arguments).is_err());
        }
    }

    #[test]
    fn validates_uuid_fields_without_normalizing_them() {
        for (mut args, field) in [
            (start(), "operation_id"),
            (start(), "expected_generation"),
            (update(), "generation"),
            (stop(), "generation"),
        ] {
            args[field] = json!("not-a-uuid");
            assert!(
                parse_loop_control_request(&args.to_string()).is_err(),
                "{field}"
            );
        }
        let mut args = start();
        args["expected_generation"] = json!(GENERATION);
        let parsed = parse_loop_control_request(&args.to_string()).unwrap();
        assert_eq!(
            serde_json::to_value(parsed).unwrap()["expected_generation"],
            GENERATION
        );
    }

    #[test]
    fn enforces_objective_utf8_byte_limit_and_non_whitespace() {
        for mut args in [start(), update()] {
            for objective in [
                "".into(),
                " \n\t".into(),
                "界".repeat(MAX_OBJECTIVE_BYTES / 3 + 1),
            ] {
                args["objective"] = json!(objective);
                assert!(parse_loop_control_request(&args.to_string()).is_err());
            }
            args["objective"] = json!("x".repeat(MAX_OBJECTIVE_BYTES));
            assert!(parse_loop_control_request(&args.to_string()).is_ok());
        }
    }

    #[test]
    fn enforces_positive_iteration_and_round_bounds_for_start_and_update() {
        for mut args in [start(), update()] {
            for (field, ceiling) in [
                ("max_iterations", MAX_ITERATIONS),
                ("max_total_rounds", MAX_TOTAL_ROUNDS),
            ] {
                for value in [json!(0), json!(ceiling + 1), json!(-1), json!(1.5)] {
                    args[field] = value;
                    assert!(
                        parse_loop_control_request(&args.to_string()).is_err(),
                        "{field}"
                    );
                }
                args[field] = json!(ceiling);
                assert!(parse_loop_control_request(&args.to_string()).is_ok());
            }
        }
    }

    #[test]
    fn update_requires_a_non_null_patch() {
        let mut args = update();
        args.as_object_mut().unwrap().remove("objective");
        assert!(parse_loop_control_request(&args.to_string()).is_err());
        args["objective"] = json!(null);
        args["max_iterations"] = json!(null);
        args["max_total_rounds"] = json!(null);
        assert!(parse_loop_control_request(&args.to_string()).is_err());
        args["max_total_rounds"] = json!(8);
        assert!(parse_loop_control_request(&args.to_string()).is_ok());
    }

    #[test]
    fn status_is_readonly_but_all_calls_are_serial_and_nonpromotable() {
        let tool = LoopControlTool::new();
        assert_eq!(
            tool.classify(&json!({"action": "status"})),
            ToolClass {
                mutability: ToolMutability::ReadOnly,
                parallel_safe: false,
                promotable: false,
            }
        );
        for args in [
            start(),
            update(),
            stop(),
            json!({"action": "status", "session_id": "foreign"}),
            json!({"action": "update"}),
            json!({"action": "unknown"}),
            json!(null),
        ] {
            assert_eq!(tool.classify(&args), ToolClass::MUTATING_SERIAL);
        }
    }

    #[test]
    fn schema_is_flat_bounded_and_requires_action() {
        let schema = LoopControlTool::new().parameters_schema();
        assert_eq!(schema["additionalProperties"], false);
        assert_eq!(schema["required"], json!(["action"]));
        assert_eq!(
            schema["properties"]["max_iterations"]["maximum"],
            MAX_ITERATIONS
        );
        assert_eq!(
            schema["properties"]["max_total_rounds"]["maximum"],
            MAX_TOTAL_ROUNDS
        );
        assert!(schema.get("oneOf").is_none());
        assert!(schema["properties"].get("session_id").is_none());
        assert!(schema["properties"].get("target_session_id").is_none());
    }

    #[tokio::test]
    async fn invocation_is_validation_only_for_every_action() {
        let tool = LoopControlTool::new();
        for args in [json!({"action": "status"}), start(), update(), stop()] {
            let ToolOutcome::Completed(result) = tool.invoke(args, context()).await.unwrap() else {
                panic!("expected a validation-only completion")
            };
            assert!(result.success);
            let receipt: serde_json::Value = serde_json::from_str(&result.result).unwrap();
            assert_eq!(receipt["status"], "validated");
            assert_eq!(receipt["applied"], false);
            assert_eq!(receipt["requires_owner_apply"], true);
            assert_eq!(receipt["session_id"], "current-session");
            assert!(receipt.get("generation").is_none());
            assert!(receipt.get("revision").is_none());
            assert!(receipt.get("objective").is_none());
            assert!(receipt["message"]
                .as_str()
                .unwrap()
                .contains("validated only"));
        }
    }

    #[tokio::test]
    async fn invocation_requires_current_session_and_rejects_invalid_arguments() {
        let tool = LoopControlTool::new();
        assert!(matches!(
            tool.invoke(json!({"action": "status"}), ToolCtx::none("missing"))
                .await,
            Err(ToolError::Execution(_))
        ));
        let mut empty_context = context();
        empty_context.session_id = Some(Arc::from(" "));
        assert!(matches!(
            tool.invoke(start(), empty_context).await,
            Err(ToolError::Execution(_))
        ));
        assert!(matches!(
            tool.invoke(json!({"action": "start"}), context()).await,
            Err(ToolError::InvalidArguments(_))
        ));
    }

    #[tokio::test]
    async fn plan_posture_allows_status_and_blocks_all_mutations_even_with_bypass() {
        let tool = LoopControlTool::new();
        let mut context = context();
        context.plan_read_only = true;
        context.bypass_permissions = true;
        context.auto_approve_permissions = true;
        assert!(tool
            .invoke(json!({"action": "status"}), context.clone())
            .await
            .is_ok());
        for args in [start(), update(), stop()] {
            assert!(matches!(
                tool.invoke(args, context.clone()).await,
                Err(ToolError::Execution(message)) if message.contains("Plan mode")
            ));
        }
    }
}