bamboo_engine/runtime/config.rs
1use std::collections::BTreeSet;
2use std::path::PathBuf;
3use std::sync::Arc;
4
5use bamboo_agent_core::storage::AttachmentReader;
6use bamboo_agent_core::storage::Storage;
7use bamboo_agent_core::tools::ToolSchema;
8use bamboo_agent_core::GoldConfidence;
9use bamboo_compression::TokenBudget;
10use bamboo_config::MemoryConfig;
11use bamboo_config::PermissionMode;
12use bamboo_domain::ReasoningEffort;
13use bamboo_domain::RuntimeSessionPersistence;
14use bamboo_llm::LLMProvider;
15use bamboo_metrics::MetricsCollector;
16use bamboo_skills::SkillManager;
17use bamboo_tools::ToolRegistry;
18use serde::{Deserialize, Serialize};
19
20use super::hooks::HookRunner;
21
22#[derive(Clone, Default)]
23pub struct AuxiliaryModelConfig {
24 pub fast_model_name: Option<String>,
25 pub fast_model_provider: Option<Arc<dyn LLMProvider>>,
26 pub background_model_name: Option<String>,
27 pub planning_model_name: Option<String>,
28 pub search_model_name: Option<String>,
29 pub summarization_model_name: Option<String>,
30 pub background_model_provider: Option<Arc<dyn LLMProvider>>,
31 pub summarization_model_provider: Option<Arc<dyn LLMProvider>>,
32}
33
34fn default_gold_max_output_tokens() -> u32 {
35 1024
36}
37
38fn default_gold_max_auto_continuations() -> u32 {
39 3
40}
41
42fn default_gold_min_confidence() -> GoldConfidence {
43 GoldConfidence::Medium
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
47#[serde(default)]
48pub struct GoldConfig {
49 /// Master switch for Gold observe-only evaluation.
50 #[serde(default)]
51 pub enabled: bool,
52 /// Independent switch for Phase 2 low-risk auto-answer.
53 ///
54 /// Kept separate from `enabled` so Phase 1 observe-only users do not
55 /// implicitly opt into automatic clarification responses.
56 #[serde(default)]
57 pub auto_answer_enabled: bool,
58 /// Independent switch for Phase 3 server-side auto-continue.
59 ///
60 /// Kept separate from both `enabled` and `auto_answer_enabled` so users can
61 /// opt into terminal auto-resume explicitly without enabling other Gold
62 /// automation behaviors.
63 #[serde(default)]
64 pub auto_continue_enabled: bool,
65 /// Optional dedicated model for Gold evaluation. Falls back to fast model,
66 /// then the main chat model when absent.
67 #[serde(default, skip_serializing_if = "Option::is_none")]
68 pub model_name: Option<String>,
69 /// The user's goal for this session.
70 ///
71 /// Unlike `evaluation_prompt` (which only tunes the *judge*), the goal is
72 /// surfaced to the *main* executing agent as a persistent system-prompt
73 /// block so it actively works toward it. The Gold evaluator also measures
74 /// progress against this text.
75 #[serde(default, skip_serializing_if = "Option::is_none")]
76 pub goal: Option<String>,
77 /// Optional custom prompt suffix appended to the built-in Gold evaluator
78 /// prompt. This tunes the judge only; it does not set the goal.
79 #[serde(default, skip_serializing_if = "Option::is_none")]
80 pub evaluation_prompt: Option<String>,
81 /// Output token limit for the Gold evaluator call.
82 #[serde(default = "default_gold_max_output_tokens")]
83 pub max_output_tokens: u32,
84 /// Maximum number of automatic Gold continuations allowed per session.
85 #[serde(default = "default_gold_max_auto_continuations")]
86 pub max_auto_continuations: u32,
87 /// Minimum evaluator confidence required before Gold auto-continues or
88 /// auto-answers. Defaults to `medium` so the loop fires on reasonably
89 /// confident verdicts rather than only `high`.
90 #[serde(default = "default_gold_min_confidence")]
91 pub min_auto_continue_confidence: GoldConfidence,
92}
93
94impl Default for GoldConfig {
95 fn default() -> Self {
96 Self {
97 enabled: false,
98 auto_answer_enabled: false,
99 auto_continue_enabled: false,
100 model_name: None,
101 goal: None,
102 evaluation_prompt: None,
103 max_output_tokens: default_gold_max_output_tokens(),
104 max_auto_continuations: default_gold_max_auto_continuations(),
105 min_auto_continue_confidence: default_gold_min_confidence(),
106 }
107 }
108}
109
110impl GoldConfig {
111 /// The session goal text, falling back to the legacy `evaluation_prompt`
112 /// for sessions created before the dedicated `goal` field existed.
113 ///
114 /// Returns `None` when neither field holds non-empty text.
115 pub fn effective_goal(&self) -> Option<&str> {
116 self.goal
117 .as_deref()
118 .or(self.evaluation_prompt.as_deref())
119 .map(str::trim)
120 .filter(|value| !value.is_empty())
121 }
122}
123
124fn default_guardian_max_reviews() -> u32 {
125 2
126}
127
128/// Configuration for the guardian adversarial-review terminal gate.
129///
130/// Mirrors [`GoldConfig`]: a plain, serde-defaulting struct surfaced per run.
131/// When `enabled` is false (the default) the guardian gate is inactive and the
132/// terminal completion path is unchanged.
133#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
134#[serde(default)]
135pub struct GuardianConfig {
136 /// Master switch for the guardian review gate.
137 #[serde(default)]
138 pub enabled: bool,
139 /// Optional dedicated reviewer model. Falls back to the run's main model.
140 #[serde(default, skip_serializing_if = "Option::is_none")]
141 pub model_name: Option<String>,
142 /// Maximum guardian review passes per run (budget; mirrors
143 /// [`GoldConfig::max_auto_continuations`]).
144 #[serde(default = "default_guardian_max_reviews")]
145 pub max_reviews: u32,
146}
147
148impl Default for GuardianConfig {
149 fn default() -> Self {
150 Self {
151 enabled: false,
152 model_name: None,
153 max_reviews: default_guardian_max_reviews(),
154 }
155 }
156}
157
158/// Late-bound spawner for the guardian reviewer child.
159///
160/// The runner cannot construct a child directly: the `SpawnScheduler` is built
161/// *after* the `Agent` that drives the runner (a construction-order cycle), so
162/// the terminal gate spawns the reviewer through this trait object, injected
163/// per-request on [`AgentLoopConfig`] exactly like `auxiliary_model_resolver`.
164/// The implementation lives in the server (it captures the already-built
165/// scheduler + child-session adapter); the engine holds only the trait, keeping
166/// the engine free of any dependency on server/AppState types.
167#[async_trait::async_trait]
168pub trait GuardianSpawner: Send + Sync {
169 /// Create a read-only reviewer child for `parent_session_id`, seeded with
170 /// `review_prompt`, enqueue it to run, and return its session id so the
171 /// caller can register a wait on it.
172 async fn spawn_guardian_review(
173 &self,
174 parent_session: &bamboo_agent_core::Session,
175 review_prompt: String,
176 model: String,
177 disabled_tools: Option<BTreeSet<String>>,
178 ) -> Result<String, String>;
179}
180
181/// Hidden resume-message `runtime_kind` metadata value for a bash-completion
182/// self-resume (issue #84 Phase 2b). Shared by the producer (the self-resume
183/// task that appends the resume message) and the consumer (the suspend-
184/// finalization discriminant arm that preserves it), so a typo in one cannot
185/// desync from the other and silently drop the resume trigger.
186pub const BASH_COMPLETION_RESUME_KIND: &str = "bash_completion_resume";
187
188/// Re-exported so peers that already `use crate::runtime::config::{BashResumeHook, …}`
189/// (the runtime/spawn threading) can name the completion sink the same way,
190/// rather than reaching into `bamboo_agent_core` separately.
191pub use bamboo_agent_core::BashCompletionSink;
192
193/// Late-bound hook that arranges a self-resume for a session suspended waiting
194/// on background Bash shells (issue #84 Phase 2b). Injected per-request on
195/// [`AgentLoopConfig`] exactly like [`GuardianSpawner`]; the implementation
196/// lives in the session-app layer (on the completion coordinator) where the
197/// resume port ([`crate::session_app::resume::ResumeExecutionPort`]) is
198/// reachable.
199///
200/// The hook spawns a detached task that **polls the live background-shell
201/// registry** until every captured shell is no longer running, then clears the
202/// wait and resumes the session. Polling — not the one-shot `BashCompleted`
203/// event — is the liveness guarantee: even if a shell completes between the
204/// suspend snapshot and the hook's first poll, or before any event subscriber
205/// exists, the registry will report it as not-running and the session resumes.
206pub trait BashResumeHook: Send + Sync {
207 /// Arrange a detached self-resume for `session_id`, which has just been
208 /// durably suspended waiting on the background shells in `bash_ids`.
209 fn arrange_bash_self_resume(&self, session_id: String, bash_ids: Vec<String>);
210}
211
212/// A child sub-agent's request to have a gated tool approved by its parent.
213///
214/// A non-bypassed child cannot answer its own permission prompt (no human is
215/// attached to a child session), so the request is delegated up to the parent.
216#[derive(Debug, Clone)]
217pub struct ChildApprovalRequest {
218 pub child_session_id: String,
219 pub parent_session_id: String,
220 /// The gated tool call on the child to re-execute once approved.
221 pub child_tool_call_id: String,
222 pub tool_name: String,
223 /// Permission type as a string (e.g. "WriteFile", "ExecuteCommand").
224 pub permission_type: String,
225 /// The concrete resource the permission applies to (path, command, …).
226 pub resource: String,
227 /// Human-facing approval question to surface on the parent.
228 pub question: String,
229 /// The raw `awaiting_permission_approval` payload the child's executor built,
230 /// so the parent can reuse the existing grant-extraction path verbatim.
231 pub approval_payload: serde_json::Value,
232}
233
234/// What the executor should do after delegating a child's approval upward.
235#[derive(Debug, Clone, Copy, PartialEq, Eq)]
236pub enum ChildApprovalOutcome {
237 /// Registered on the parent; the child must SUSPEND and await the decision.
238 Delegated,
239 /// Parent policy auto-approved (bypass / existing grant); proceed to execute.
240 AutoApproved,
241 /// Parent policy auto-denied; the executor must deny the tool.
242 AutoDenied,
243}
244
245/// Late-bound delegate that routes a child's approval request up to its parent.
246///
247/// Injected per-request on [`AgentLoopConfig`] exactly like [`GuardianSpawner`];
248/// the trait lives in the engine, the implementation in the server (it owns the
249/// parent session store + pending-question + notification machinery).
250#[async_trait::async_trait]
251pub trait ApprovalDelegate: Send + Sync {
252 /// Register `request` on its parent (or auto-resolve by policy) and report
253 /// what the child's executor should do next.
254 async fn delegate_child_approval(
255 &self,
256 request: ChildApprovalRequest,
257 ) -> Result<ChildApprovalOutcome, String>;
258}
259
260#[derive(Debug, Clone, Copy, PartialEq, Eq)]
261pub enum ImageFallbackMode {
262 Placeholder,
263 Error,
264 Ocr,
265 /// Use a vision-capable LLM to describe the image, then replace the image
266 /// with the textual description so that text-only models can understand
267 /// the content.
268 Vision,
269}
270
271#[derive(Debug, Clone, PartialEq, Eq)]
272pub struct ImageFallbackConfig {
273 pub mode: ImageFallbackMode,
274 /// Vision model name for `Vision` mode. Falls back to the session's main model
275 /// when `None`.
276 pub vision_model: Option<String>,
277}
278
279#[derive(Debug, Clone, Copy, PartialEq, Eq)]
280pub struct PromptMemoryFlags {
281 pub project_prompt_injection: bool,
282 pub relevant_recall: bool,
283 pub relevant_recall_rerank: bool,
284 pub project_first_dream: bool,
285 pub ledger_agenda: bool,
286}
287
288impl Default for PromptMemoryFlags {
289 fn default() -> Self {
290 Self {
291 project_prompt_injection: true,
292 relevant_recall: true,
293 relevant_recall_rerank: false,
294 project_first_dream: true,
295 ledger_agenda: true,
296 }
297 }
298}
299
300impl From<&MemoryConfig> for PromptMemoryFlags {
301 fn from(value: &MemoryConfig) -> Self {
302 Self {
303 project_prompt_injection: value.project_prompt_injection,
304 relevant_recall: value.relevant_recall,
305 relevant_recall_rerank: value.relevant_recall_rerank,
306 project_first_dream: value.project_first_dream,
307 ledger_agenda: value.ledger_agenda_injection,
308 }
309 }
310}
311
312/// Configuration for the agent loop.
313///
314/// # One-config-per-run invariant (#44)
315///
316/// These values are SNAPSHOTTED once, from the live `Config` under a brief read
317/// lock, at the start of `AgentRuntime::execute()`. The entire multi-round run —
318/// which can last minutes — then uses this frozen snapshot. Changing config
319/// (model names, provider, `disabled_tools`/`disabled_skills`, memory flags,
320/// token budget, …) while a run is in flight does NOT affect that run; the new
321/// values are picked up on the NEXT execution (i.e. the next user turn / session
322/// restart), not the next round of the current run.
323///
324/// This is intentional: a run sees a stable configuration, so its behavior can't
325/// shift underneath it mid-execution. The deliberate exceptions are the
326/// late-bound, per-request trait objects that resolve LIVE each time they're
327/// used rather than being snapshotted — `auxiliary_model_resolver` (auxiliary
328/// model selection) and `guardian_spawner` (the reviewer child). If a frozen
329/// field ever needs to become live-per-round, follow that resolver pattern
330/// rather than widening the snapshot.
331#[non_exhaustive]
332pub struct AgentLoopConfig {
333 pub(crate) max_rounds: usize,
334 pub(crate) system_prompt: Option<String>,
335 /// Skill IDs that are disabled globally for this execution.
336 pub(crate) disabled_skill_ids: BTreeSet<String>,
337 /// Optional explicit skill selection for this execution.
338 /// When set, only these skill IDs are considered for skill context and allowlists.
339 pub(crate) selected_skill_ids: Option<Vec<String>>,
340 /// Optional active skill mode for this execution.
341 ///
342 /// When set, skill discovery prefers `skills-<mode>` directories over generic
343 /// directories for the same skill id.
344 pub(crate) selected_skill_mode: Option<String>,
345 pub(crate) additional_tool_schemas: Vec<ToolSchema>,
346 pub(crate) tool_registry: Arc<ToolRegistry>,
347 pub(crate) skill_manager: Option<Arc<SkillManager>>,
348 /// Stable Project identity/resource resolver. The server wires the
349 /// authoritative registry adapter once on `AgentRuntimeBuilder`.
350 pub(crate) project_context_resolver:
351 Option<Arc<crate::project_context::ProjectContextResolver>>,
352 /// If true, skip appending the initial user message (already present in session).
353 pub(crate) skip_initial_user_message: bool,
354 /// Optional storage for persisting session changes
355 pub(crate) storage: Option<Arc<dyn Storage>>,
356 /// Optional runtime persistence for non-authoritative session saves.
357 /// When set, engine save sites use this instead of `storage` for writes.
358 pub(crate) persistence: Option<Arc<dyn RuntimeSessionPersistence>>,
359 /// Optional attachment reader for resolving `bamboo-attachment://...` references
360 /// into `data:` URLs for upstream providers. This must not mutate session storage.
361 pub(crate) attachment_reader: Option<Arc<dyn AttachmentReader>>,
362 /// Optional asynchronous metrics collector
363 pub(crate) metrics_collector: Option<MetricsCollector>,
364 /// Model name used for metrics attribution
365 pub(crate) model_name: Option<String>,
366 /// Fast/cheap model for lightweight tasks (task evaluation, search, etc.).
367 ///
368 /// Call sites may fall back to `model_name` when this is unset.
369 pub(crate) fast_model_name: Option<String>,
370 /// Optional provider override for lightweight fast-model LLM calls.
371 pub(crate) fast_model_provider: Option<Arc<dyn LLMProvider>>,
372 /// Fast/cheap model for memory/background tasks.
373 ///
374 /// This must not silently fall back to the main interaction model.
375 pub(crate) background_model_name: Option<String>,
376
377 /// Model for planning/coordination tasks (task decomposition, architecture).
378 /// Falls back to `model_name` when unset.
379 pub(crate) planning_model_name: Option<String>,
380 /// Model for search/navigation tasks (grep, file listing, symbol resolution).
381 /// Falls back to `fast_model_name` when unset.
382 pub(crate) search_model_name: Option<String>,
383 /// Custom instructions for conversation summarization, injected into the
384 /// LLM summary prompt. Lets users control what the summary focuses on.
385 ///
386 /// Resolution order: session-level > config-level > built-in defaults.
387 pub(crate) compression_instructions: Option<String>,
388 /// Dedicated model for summarization. Falls back to `background_model_name`.
389 pub(crate) summarization_model_name: Option<String>,
390 /// Optional provider override for memory/background model LLM calls.
391 ///
392 /// When set, memory recall rerank and other memory/background tasks use this
393 /// provider instead of the shared agent loop provider.
394 pub(crate) background_model_provider: Option<Arc<dyn LLMProvider>>,
395 /// Optional provider override for summarization / context compression calls.
396 ///
397 /// When set, conversation/task summarization uses this provider instead of
398 /// the shared agent loop provider.
399 pub(crate) summarization_model_provider: Option<Arc<dyn LLMProvider>>,
400 /// Provider routing key used for provider-specific request behavior.
401 ///
402 /// In multi-instance mode this may be the instance id.
403 pub(crate) provider_name: Option<String>,
404 /// Underlying provider type (for example `openai`, `anthropic`, `copilot`).
405 ///
406 /// This is distinct from `provider_name` so provider-specific behavior can
407 /// remain correct when routing keys are instance ids.
408 pub(crate) provider_type: Option<String>,
409 /// Optional request-time reasoning effort override.
410 pub(crate) reasoning_effort: Option<ReasoningEffort>,
411 /// Bamboo application data directory (typically `~/.bamboo`).
412 ///
413 /// Used by runtime features that persist auxiliary artifacts outside the
414 /// session store, such as durable plan mode files under `~/.bamboo/plan`.
415 pub(crate) app_data_dir: Option<PathBuf>,
416 /// Tool names that should be excluded from schemas sent to the LLM.
417 pub(crate) disabled_tools: BTreeSet<String>,
418 /// Token budget for context management (optional, defaults to model's limits)
419 pub(crate) token_budget: Option<TokenBudget>,
420 /// Legacy `config.json` `model_limits` value, snapshotted from the live
421 /// in-memory Config when this loop config is built. Consulted only by
422 /// `resolve_token_budget` as a last-resort fallback when `model_limits.json`
423 /// fails to load — so the engine never does a fresh disk-reading
424 /// `Config::new()` (which would also clobber the global env-var cache). #38.
425 pub(crate) legacy_model_limits: Option<serde_json::Value>,
426 /// Optional image fallback behavior applied to *LLM requests only* (never persisted).
427 ///
428 /// This is intended for text-only provider paths where image parts must be degraded
429 /// (placeholder / OCR / error) without leaking into stored session history or UI.
430 pub(crate) image_fallback: Option<ImageFallbackConfig>,
431 /// Feature flags controlling prompt-time memory injection behavior.
432 pub(crate) prompt_memory_flags: PromptMemoryFlags,
433 /// Maximum tool calls allowed per round (default: 80).
434 pub(crate) max_tool_calls_per_round: usize,
435 /// Maximum consecutive failures per tool before circuit breaker (default: 3).
436 pub(crate) max_consecutive_failures_per_tool: usize,
437 /// Per-tool execution timeout in seconds (default: 120).
438 pub(crate) per_tool_timeout_secs: u64,
439 /// Parallel batch execution timeout in seconds (default: 300).
440 pub(crate) parallel_batch_timeout_secs: u64,
441 /// Resolved LLM stream transport/semantic watchdog policy. The same value
442 /// is passed to main response streams and auxiliary silent streams.
443 pub(crate) stream_timeout: bamboo_config::StreamTimeoutConfig,
444 /// Permission mode for this execution (default: None = use PermissionConfig's mode).
445 pub(crate) permission_mode: Option<PermissionMode>,
446 /// Optional Gold observe-only evaluator configuration.
447 ///
448 /// When `None` or `enabled == false`, Gold evaluation is disabled and the
449 /// existing execute/respond/resume loop remains unchanged.
450 pub(crate) gold_config: Option<GoldConfig>,
451 /// Optional guardian adversarial-review gate configuration. When `None` or
452 /// `enabled == false`, the guardian terminal gate is inactive.
453 pub(crate) guardian_config: Option<GuardianConfig>,
454 /// Late-bound spawner for the guardian reviewer child. `None` (the default)
455 /// leaves the guardian gate inert even when `guardian_config.enabled` is set,
456 /// since the runner cannot create a child without it. Wired by the server.
457 pub(crate) guardian_spawner: Option<Arc<dyn GuardianSpawner>>,
458 /// Late-bound hook that arranges a self-resume for a session suspended
459 /// waiting on background Bash shells (issue #84 Phase 2b). `None` (the
460 /// default) leaves the bash suspend gate inert: the gate refuses to suspend
461 /// without a wired hook, so a session can never strand itself without a
462 /// resume path. Wired by the server (the completion coordinator impl).
463 pub(crate) bash_resume_hook: Option<Arc<dyn BashResumeHook>>,
464 /// Late-bound sink that pushes a completed background Bash shell's result
465 /// into this session's loop (issue #84 Phase 2b follow-up) — injected at the
466 /// next round boundary while the loop is actively iterating, or delivered via
467 /// resume when it is idle. Threaded onto the tool dispatch context (like
468 /// `can_async_resume`) so the Bash tool can hand it to the shell's
469 /// completion-poll task. `None` (the default) leaves the push inert; the
470 /// durable end-of-turn suspend/poll backstop (`bash_resume_hook`) still runs.
471 /// Wired by the server (the completion coordinator impl).
472 pub(crate) bash_completion_sink: Option<Arc<dyn bamboo_agent_core::BashCompletionSink>>,
473 /// Late-bound delegate that routes a child's gated-tool approval request up
474 /// to its parent (Phase 2). `None` (the default) leaves child gating on its
475 /// legacy path. Wired by the server.
476 pub(crate) approval_delegate: Option<Arc<dyn ApprovalDelegate>>,
477 /// Frozen lifecycle-hook registry for this run. The default registry is
478 /// empty, and every seam checks `has_hooks_for` before constructing payloads.
479 pub(crate) hook_runner: Arc<HookRunner>,
480 /// Enable dynamic per-round model routing based on task complexity.
481 /// When true, the pipeline classifies complexity at each round end and
482 /// stores the result in session metadata.
483 pub(crate) features_dynamic_model_routing: bool,
484 /// Optional per-round resolver for auxiliary model settings that should
485 /// follow live global config rather than stay frozen for the whole run.
486 ///
487 /// The main chat model remains session/request scoped; this hook is only
488 /// for fast/background/planning/search/summarization helpers.
489 pub(crate) auxiliary_model_resolver:
490 Option<Arc<dyn Fn() -> AuxiliaryModelConfig + Send + Sync>>,
491 /// Optional per-round resolver for the disabled tool/skill sets so they follow
492 /// LIVE global config instead of staying frozen for the whole run. Returns the
493 /// current `(disabled_tools, disabled_skill_ids)`. When `None`, the snapshotted
494 /// `disabled_tools` / `disabled_skill_ids` fields below are used (#44 behavior).
495 /// Re-resolved each round at the tool-schema filter, so disabling/re-enabling a
496 /// tool mid-run takes effect on the next round. #136.
497 pub(crate) disabled_filter_resolver:
498 Option<Arc<dyn Fn() -> (BTreeSet<String>, BTreeSet<String>) + Send + Sync>>,
499 /// Server-level usage guidance contributed by the run's tool executor —
500 /// chiefly the `instructions` connected MCP servers return from `initialize`.
501 /// Captured once at config construction (from `ToolExecutor::tool_guidance`)
502 /// and appended to the tool-guide section of the system prompt, so a server's
503 /// own how-to-use notes appear only while that server is loaded for the run.
504 pub(crate) mcp_tool_guidance: Option<String>,
505 /// Per-run resource guardrails (issue #221): already resolved — the
506 /// per-request override merged over the config-level default (see
507 /// [`AgentRuntime::execute`](crate::runtime::runtime::AgentRuntime::execute)).
508 /// Checked after every round; exceeding a configured limit gracefully
509 /// stops the run (mirrors the `max_rounds` exhaustion path).
510 pub(crate) run_budget: bamboo_config::RunBudgetConfig,
511}
512
513impl Default for AgentLoopConfig {
514 fn default() -> Self {
515 Self {
516 max_rounds: 200,
517 system_prompt: None,
518 disabled_skill_ids: BTreeSet::new(),
519 selected_skill_ids: None,
520 selected_skill_mode: None,
521 additional_tool_schemas: Vec::new(),
522 tool_registry: Arc::new(ToolRegistry::new()),
523 skill_manager: None,
524 project_context_resolver: None,
525 skip_initial_user_message: false,
526 storage: None,
527 persistence: None,
528 attachment_reader: None,
529 metrics_collector: None,
530 model_name: None,
531 fast_model_name: None,
532 fast_model_provider: None,
533 background_model_name: None,
534 planning_model_name: None,
535 search_model_name: None,
536 compression_instructions: None,
537 summarization_model_name: None,
538 background_model_provider: None,
539 summarization_model_provider: None,
540 provider_name: None,
541 provider_type: None,
542 reasoning_effort: None,
543 app_data_dir: None,
544 disabled_tools: BTreeSet::new(),
545 token_budget: None,
546 legacy_model_limits: None,
547 image_fallback: None,
548 prompt_memory_flags: PromptMemoryFlags::default(),
549 max_tool_calls_per_round: 80,
550 max_consecutive_failures_per_tool: 3,
551 per_tool_timeout_secs: 120,
552 parallel_batch_timeout_secs: 300,
553 stream_timeout: bamboo_config::StreamTimeoutConfig::default(),
554 permission_mode: None,
555 gold_config: None,
556 guardian_config: None,
557 guardian_spawner: None,
558 bash_resume_hook: None,
559 bash_completion_sink: None,
560 approval_delegate: None,
561 hook_runner: Arc::new(HookRunner::new()),
562 features_dynamic_model_routing: false,
563 auxiliary_model_resolver: None,
564 disabled_filter_resolver: None,
565 mcp_tool_guidance: None,
566 run_budget: bamboo_config::RunBudgetConfig::default(),
567 }
568 }
569}
570
571impl AgentLoopConfig {
572 /// Live `(disabled_tools, disabled_skill_ids)` for the current round: the
573 /// resolver if one is wired (#136 — follows live global config between
574 /// rounds), else the per-run snapshot (#44 frozen behavior). `Cow` avoids
575 /// cloning the snapshot in the common no-resolver path (SDK / tests).
576 pub(crate) fn resolve_disabled_filters(
577 &self,
578 ) -> (
579 std::borrow::Cow<'_, BTreeSet<String>>,
580 std::borrow::Cow<'_, BTreeSet<String>>,
581 ) {
582 match &self.disabled_filter_resolver {
583 Some(resolver) => {
584 let (tools, skills) = resolver();
585 (
586 std::borrow::Cow::Owned(tools),
587 std::borrow::Cow::Owned(skills),
588 )
589 }
590 None => (
591 std::borrow::Cow::Borrowed(&self.disabled_tools),
592 std::borrow::Cow::Borrowed(&self.disabled_skill_ids),
593 ),
594 }
595 }
596
597 /// The active session goal to surface to the main agent, or `None` when
598 /// Gold is disabled or no goal is set. Falls back to the legacy
599 /// `evaluation_prompt` for back-compat via [`GoldConfig::effective_goal`].
600 pub fn active_goal(&self) -> Option<&str> {
601 self.gold_config
602 .as_ref()
603 .filter(|cfg| cfg.enabled)
604 .and_then(GoldConfig::effective_goal)
605 }
606
607 /// Whether the Codex-style autonomous goal loop is active for this run.
608 ///
609 /// This requires Gold to be enabled, a goal to be set, AND auto-continue to
610 /// be on. Only then is the `update_goal` self-report tool surfaced to the
611 /// model and the terminal double-check allowed to veto a premature stop.
612 /// When Gold is enabled without auto-continue, the evaluator stays purely
613 /// observational (legacy behavior).
614 pub fn goal_loop_active(&self) -> bool {
615 self.gold_config.as_ref().is_some_and(|cfg| {
616 cfg.enabled && cfg.auto_continue_enabled && cfg.effective_goal().is_some()
617 })
618 }
619
620 /// Whether the guardian review gate is active for this run: a spawner is
621 /// wired (so the runner can actually create the reviewer child) AND the
622 /// config is present and enabled.
623 pub fn guardian_active(&self) -> bool {
624 self.guardian_spawner.is_some()
625 && self.guardian_config.as_ref().is_some_and(|cfg| cfg.enabled)
626 }
627
628 /// Maximum guardian review passes for this run (the budget). `0` when no
629 /// guardian config is set.
630 pub fn guardian_max_reviews(&self) -> u32 {
631 self.guardian_config
632 .as_ref()
633 .map_or(0, |cfg| cfg.max_reviews)
634 }
635
636 /// The reviewer model override, if a guardian config sets one.
637 pub fn guardian_model(&self) -> Option<&str> {
638 self.guardian_config
639 .as_ref()
640 .and_then(|cfg| cfg.model_name.as_deref())
641 }
642
643 /// Whether child→parent approval delegation is wired for this run.
644 pub fn delegation_active(&self) -> bool {
645 self.approval_delegate.is_some()
646 }
647}
648
649#[cfg(test)]
650mod tests;