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