bamboo-domain 2026.10.21

Domain models and shared types 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
//! Tool name constants and normalization functions.
//!
//! These are pure data and functions with zero crate dependencies. They are
//! used by both `core::config` and `agent::tools::executor`.

/// List of all built-in tool names.
///
/// This list intentionally includes only tools that are always registered by
/// `BuiltinToolExecutor::new()`. Optional tools (for example integrations that
/// depend on host binaries) should NOT be added here.
pub const BUILTIN_TOOL_NAMES: [&str; 22] = [
    "Bash",
    "BashInput",
    "BashOutput",
    "exec_command",
    "write_stdin",
    "Edit",
    "ExitPlanMode",
    "GetFileInfo",
    "Glob",
    "Grep",
    "KillShell",
    "session_note",
    "Read",
    "request_permissions",
    "Sleep",
    "Task",
    "update_goal",
    "loop_control",
    "ViewImage",
    "WebFetch",
    "Workspace",
    "Write",
];

/// Tool names that are accepted as aliases for built-in/server tools but are not
/// independently listed in `BUILTIN_TOOL_NAMES`/`SERVER_TOOL_NAMES`. Calls to these names are
/// transparently routed to their canonical counterpart.
pub const BUILTIN_TOOL_ALIASES: [(&str, &str); 10] = [
    // apply_patch is a patch-only alias for Edit
    ("apply_patch", "Edit"),
    // FileExists is subsumed by GetFileInfo (returns {exists: false} for missing paths)
    ("FileExists", "GetFileInfo"),
    // GetCurrentDir + SetWorkspace are subsumed by Workspace
    ("GetCurrentDir", "Workspace"),
    ("SetWorkspace", "Workspace"),
    // Session note rename
    ("memory_note", "session_note"),
    // Server tool renames: `recall` was ambiguous with durable-memory recall
    // (RAG-style relevant-memory injection). The canonical name is now
    // `session_history` — strictly a read-only viewer over local SQLite
    // session storage. Older names still route to the canonical tool.
    ("recall", "session_history"),
    ("session_inspector", "session_history"),
    ("schedule_tasks", "scheduler"),
    // SubAgent manager alias
    ("sub_session_manager", "SubAgent"),
    ("SubSession", "SubAgent"),
];

/// Legacy spellings normalized before canonical alias resolution.
///
/// This table is public so classification, compatibility tests, and future
/// admission code can prove that every accepted spelling shares one resolver.
pub const LEGACY_TOOL_NAME_ALIASES: [(&str, &str); 34] = [
    ("execute_command", "Bash"),
    ("file_exists", "FileExists"),
    ("fileExists", "FileExists"),
    ("get_current_dir", "GetCurrentDir"),
    ("getCurrentDir", "GetCurrentDir"),
    ("get_file_info", "GetFileInfo"),
    ("getFileInfo", "GetFileInfo"),
    ("list_directory", "Glob"),
    ("memory_note", "memory_note"),
    ("read_file", "Read"),
    ("set_workspace", "SetWorkspace"),
    ("setWorkspace", "SetWorkspace"),
    ("sleep", "Sleep"),
    ("applyPatch", "apply_patch"),
    ("spawn_session", "SubAgent"),
    ("spawnSession", "SubAgent"),
    ("sub_session", "SubAgent"),
    ("subSession", "SubAgent"),
    ("sub_task", "SubAgent"),
    ("subTask", "SubAgent"),
    ("team_agent", "SubAgent"),
    ("teamAgent", "SubAgent"),
    ("child_session", "SubAgent"),
    ("childSession", "SubAgent"),
    ("sub_session_manager", "SubAgent"),
    ("sub_agent", "SubAgent"),
    ("subAgent", "SubAgent"),
    ("SubSession", "SubAgent"),
    ("subsession", "SubAgent"),
    ("view_image", "ViewImage"),
    ("viewImage", "ViewImage"),
    ("write_file", "Write"),
    ("sessionInspector", "session_inspector"),
    ("scheduleTasks", "schedule_tasks"),
];

pub const SERVER_TOOL_NAMES: [&str; 13] = [
    "Plan",
    "SubAgent",
    "archive_context",
    "compact_context",
    "scheduler",
    "session_history",
    "session_history_current",
    "session_message",
    "session_control",
    "memory",
    "ledger",
    "load_skill",
    "read_skill_resource",
];

/// Canonical capability names supplied by Bamboo's server-side overlays.
///
/// This superset is used by catalogs and discovery without broadening the
/// legacy `normalize_tool_ref`/`is_builtin_tool` acceptance surface above.
pub const SERVER_CAPABILITY_NAMES: [&str; 27] = [
    "Plan",
    "SubAgent",
    "Project",
    "archive_context",
    "ask_agent",
    "browser",
    "browser_eval",
    "cluster",
    "compact_context",
    "deploy_agent",
    "ledger",
    "load_skill",
    "memory",
    "notify",
    "read_skill_resource",
    "scheduler",
    "session_control",
    "session_history",
    "session_history_current",
    "session_message",
    "workflow_run",
    "work_overview",
    "work_search",
    "work_inspect",
    "work_changes",
    "work_update",
    "work_dispatch",
];

/// Resolve any model- or host-facing tool reference to one canonical identity.
///
/// Legacy `namespace::tool` references use the executable name after the last
/// separator. MCP aliases already encode their namespace as
/// `mcp__server__function`, so they remain distinct. Known builtin/server names
/// are also restored to their declared case. Unknown registrations are kept as
/// executable names and are classified fail-closed elsewhere. Registered schema
/// execution identities remain exact and are carried separately by
/// `ClassifiedToolIdentity`; this function must not be used to rename schemas.
pub fn canonical_tool_name(value: &str) -> String {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return String::new();
    }

    let unqualified = trimmed.rsplit("::").next().unwrap_or(trimmed).trim();
    let normalized = normalize_builtin_alias(unqualified);
    let canonical = resolve_alias(normalized).unwrap_or(normalized);

    BUILTIN_TOOL_NAMES
        .iter()
        .chain(SERVER_CAPABILITY_NAMES.iter())
        .find(|known| known.eq_ignore_ascii_case(canonical))
        .map(|known| (*known).to_string())
        .unwrap_or_else(|| canonical.to_string())
}

/// Normalizes a tool reference to a standard tool name.
///
/// Returns None if the tool name is not recognized.
pub fn normalize_tool_ref(value: &str) -> Option<String> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return None;
    }
    let raw_tool_name = trimmed.split("::").last().unwrap_or(trimmed);
    // Keep the ambiguous spelling until the actual Session registry/version
    // selects its owner. Canonical v1 alias resolution itself remains unchanged.
    if crate::independent_root_spawn_reference(raw_tool_name) {
        return Some("spawn_session".into());
    }
    let normalized = normalize_builtin_alias(raw_tool_name);

    // Check canonical tool names first
    if let Some(name) = BUILTIN_TOOL_NAMES
        .iter()
        .chain(SERVER_TOOL_NAMES.iter())
        .find(|name| name.eq_ignore_ascii_case(normalized))
    {
        return Some((*name).to_string());
    }

    // Check aliases – return the *alias* name so callers see what was typed,
    // while the executor routes it to the canonical tool.
    BUILTIN_TOOL_ALIASES
        .iter()
        .find(|(alias, _)| alias.eq_ignore_ascii_case(normalized))
        .map(|(alias, _)| (*alias).to_string())
}

/// Returns the canonical tool name for an alias, or `None` if the name is
/// not an alias.
pub fn resolve_alias(name: &str) -> Option<&'static str> {
    BUILTIN_TOOL_ALIASES
        .iter()
        .find(|(alias, _)| alias.eq_ignore_ascii_case(name))
        .map(|(_, canonical)| *canonical)
}

pub fn normalize_builtin_alias(name: &str) -> &str {
    LEGACY_TOOL_NAME_ALIASES
        .iter()
        .find(|(alias, _)| *alias == name)
        .map(|(_, normalized)| *normalized)
        .unwrap_or(name)
}

/// Preserve a disabled legacy shell capability after the model surface rename.
/// Call only after resolving an exact registered identity; foreign tools with
/// the same suffix must retain their own policies.
pub fn legacy_shell_replacement(execution_name: &str) -> Option<&'static str> {
    match execution_name {
        "Bash" => Some("exec_command"),
        "BashInput" | "BashOutput" | "KillShell" => Some("write_stdin"),
        _ => None,
    }
}

/// Resolve a disabled reference without losing legacy shell denials when a
/// positive catalog omits the old route. Exact registrations keep priority.
/// Only missing unqualified references may use this denial-only fallback;
/// callable resolution and qualified foreign identities remain unchanged.
pub fn resolve_disabled_tool_reference_name(
    reference: &str,
    contains_registered_name: impl FnMut(&str) -> bool,
) -> Option<String> {
    if let Some(name) = crate::resolve_tool_reference_name(reference, contains_registered_name) {
        return Some(name);
    }
    let reference = reference.trim();
    if reference.contains("::") {
        return None;
    }
    let legacy = canonical_tool_name(reference);
    legacy_shell_replacement(&legacy).map(|_| legacy)
}

/// Checks if a tool reference is a built-in tool
pub fn is_builtin_tool(value: &str) -> bool {
    normalize_tool_ref(value).is_some()
}

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

    #[test]
    fn legacy_shell_disabled_policy_only_expands_exact_builtin_names() {
        assert_eq!(legacy_shell_replacement("Bash"), Some("exec_command"));
        assert_eq!(legacy_shell_replacement("BashOutput"), Some("write_stdin"));
        assert_eq!(legacy_shell_replacement("vendor::Bash"), None);
        assert_eq!(legacy_shell_replacement("exec_command"), None);
    }

    #[test]
    fn disabled_shell_fallback_reuses_only_supported_missing_unqualified_aliases() {
        for (reference, legacy) in [
            ("Bash", "Bash"),
            (" bash ", "Bash"),
            ("bAsH", "Bash"),
            ("execute_command", "Bash"),
            ("bashinput", "BashInput"),
            ("bashoutput", "BashOutput"),
            ("kIlLsHeLl", "KillShell"),
        ] {
            assert_eq!(
                crate::resolve_tool_reference_name(reference, |name| name == legacy),
                Some(legacy.to_owned()),
            );
            assert_eq!(
                resolve_disabled_tool_reference_name(reference, |_| false),
                Some(legacy.to_owned()),
                "{reference}",
            );
        }
        for reference in [
            "",
            "EXECUTE_COMMAND",
            "unknown",
            "Read",
            "exec_command",
            "write_stdin",
            "vendor::Bash",
            "default::Bash",
            "vendor::execute_command",
            "default::bashoutput",
        ] {
            assert_eq!(
                resolve_disabled_tool_reference_name(reference, |_| false),
                None,
                "{reference}",
            );
        }
    }

    #[test]
    fn disabled_shell_fallback_keeps_registered_exact_and_alias_resolution_first() {
        let registered = [
            "Bash",
            "bash",
            "execute_command",
            "vendor::Bash",
            "default::Bash",
            "default::execute_command",
            "Read",
        ];
        for reference in registered {
            assert_eq!(
                resolve_disabled_tool_reference_name(reference, |name| registered.contains(&name)),
                Some(reference.to_owned()),
                "{reference}",
            );
        }
        // A real legacy route keeps the shared resolver's qualified-alias
        // behavior; only the additional missing-route fallback is unqualified.
        assert_eq!(
            resolve_disabled_tool_reference_name("default::execute_command", |name| name == "Bash"),
            Some("Bash".to_owned()),
        );
        assert_eq!(
            resolve_disabled_tool_reference_name("read_file", |name| name == "Read"),
            Some("Read".to_owned()),
        );
    }

    #[test]
    fn test_normalize_tool_ref_accepts_claude_style_names() {
        assert_eq!(
            normalize_tool_ref("default::Bash"),
            Some("Bash".to_string())
        );
    }

    #[test]
    fn test_normalize_tool_ref_accepts_legacy_camel_aliases() {
        assert_eq!(
            normalize_tool_ref("default::fileExists"),
            Some("FileExists".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::getCurrentDir"),
            Some("GetCurrentDir".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::getFileInfo"),
            Some("GetFileInfo".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::setWorkspace"),
            Some("SetWorkspace".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::sleep"),
            Some("Sleep".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::viewImage"),
            Some("ViewImage".to_string())
        );
    }

    #[test]
    fn retired_conclusion_tool_and_alias_are_not_recognized() {
        assert_eq!(normalize_tool_ref("conclusion_with_options"), None);
        assert_eq!(normalize_tool_ref("default::conclusionWithOptions"), None);
    }

    #[test]
    fn test_normalize_tool_ref_accepts_legacy_snake_case_aliases() {
        assert_eq!(
            normalize_tool_ref("default::execute_command"),
            Some("Bash".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::file_exists"),
            Some("FileExists".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::get_current_dir"),
            Some("GetCurrentDir".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::get_file_info"),
            Some("GetFileInfo".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::list_directory"),
            Some("Glob".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::memory_note"),
            Some("memory_note".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::read_file"),
            Some("Read".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::set_workspace"),
            Some("SetWorkspace".to_string())
        );
        assert_eq!(
            normalize_tool_ref("default::write_file"),
            Some("Write".to_string())
        );
    }

    #[test]
    fn test_normalize_tool_ref_accepts_spawn_task_aliases() {
        for reference in [
            "spawn_session",
            "default::spawn_session",
            "default::spawnSession",
        ] {
            assert_eq!(normalize_tool_ref(reference), Some("spawn_session".into()));
            assert_eq!(canonical_tool_name(reference), "SubAgent");
        }
        for alias in [
            "default::sub_session",
            "default::sub_task",
            "default::team_agent",
            "default::child_session",
            "default::sub_agent",
            "default::subAgent",
            "default::SubSession",
            "default::subsession",
        ] {
            assert_eq!(normalize_tool_ref(alias), Some("SubAgent".to_string()));
        }
    }

    #[test]
    fn test_normalize_tool_ref_accepts_server_overlay_tools() {
        assert_eq!(normalize_tool_ref("compress_context"), None);
        assert_eq!(
            normalize_tool_ref("default::read_skill_resource"),
            Some("read_skill_resource".to_string())
        );
    }

    #[test]
    fn test_normalize_tool_ref_rejects_unknown_tool() {
        assert_eq!(normalize_tool_ref("default::search"), None);
    }

    #[test]
    fn test_is_builtin_tool() {
        assert!(is_builtin_tool("Bash"));
        assert!(is_builtin_tool("default::Bash"));
        assert!(!is_builtin_tool("unknown_tool"));
        assert!(!is_builtin_tool("compress_context"));
    }

    #[test]
    fn test_resolve_alias() {
        assert_eq!(resolve_alias("apply_patch"), Some("Edit"));
        assert_eq!(resolve_alias("FileExists"), Some("GetFileInfo"));
        assert_eq!(resolve_alias("Bash"), None);
    }

    #[test]
    fn canonical_name_resolves_aliases_namespaces_and_known_case() {
        assert_eq!(canonical_tool_name(" default::applyPatch "), "Edit");
        assert_eq!(canonical_tool_name("DEFAULT::read"), "Read");
        assert_eq!(canonical_tool_name("default::set_workspace"), "Workspace");
        assert_eq!(canonical_tool_name("DEFAULT::WORKFLOW_RUN"), "workflow_run");
        assert_eq!(
            canonical_tool_name("DEFAULT::SESSION_HISTORY_CURRENT"),
            "session_history_current"
        );
        assert_eq!(
            normalize_tool_ref("session_history_current"),
            Some("session_history_current".to_string())
        );
        assert_eq!(resolve_alias("session_history_current"), None);
    }

    #[test]
    fn canonical_reference_name_unqualifies_dynamic_refs_and_preserves_mcp_aliases() {
        assert_eq!(canonical_tool_name("default::custom_tool"), "custom_tool");
        assert_eq!(
            canonical_tool_name("mcp__alpha__read_file"),
            "mcp__alpha__read_file"
        );
        assert_eq!(
            canonical_tool_name("mcp__beta__read_file"),
            "mcp__beta__read_file"
        );
        assert_ne!(
            canonical_tool_name("mcp__alpha__read_file"),
            canonical_tool_name("mcp__beta__read_file")
        );
        assert!(canonical_tool_name("  ").is_empty());
    }

    #[test]
    fn legacy_server_names_are_a_subset_of_the_classified_server_catalog() {
        for name in SERVER_TOOL_NAMES {
            assert!(
                SERVER_CAPABILITY_NAMES.contains(&name),
                "legacy server tool {name} must remain classifiable"
            );
        }
    }
}