Skip to main content

zeph_config/
agent.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use std::path::PathBuf;
5
6use serde::{Deserialize, Deserializer, Serialize, Serializer};
7
8use crate::subagent::{HookDef, MemoryScope, PermissionMode};
9
10/// Specifies which LLM provider a sub-agent should use.
11///
12/// Used in `SubAgentDef.model` frontmatter field.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum ModelSpec {
15    /// Use the parent agent's active provider at spawn time.
16    Inherit,
17    /// Use a specific named provider from `[[llm.providers]]`.
18    Named(String),
19}
20
21impl ModelSpec {
22    /// Return the string representation: `"inherit"` or the provider name.
23    #[must_use]
24    pub fn as_str(&self) -> &str {
25        match self {
26            ModelSpec::Inherit => "inherit",
27            ModelSpec::Named(s) => s.as_str(),
28        }
29    }
30}
31
32impl Serialize for ModelSpec {
33    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
34        match self {
35            ModelSpec::Inherit => serializer.serialize_str("inherit"),
36            ModelSpec::Named(s) => serializer.serialize_str(s),
37        }
38    }
39}
40
41impl<'de> Deserialize<'de> for ModelSpec {
42    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
43        let s = String::deserialize(deserializer)?;
44        if s == "inherit" {
45            Ok(ModelSpec::Inherit)
46        } else {
47            Ok(ModelSpec::Named(s))
48        }
49    }
50}
51
52/// Controls how parent agent context is injected into a spawned sub-agent's task prompt.
53#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
54#[serde(rename_all = "snake_case")]
55pub enum ContextInjectionMode {
56    /// No parent context injected.
57    None,
58    /// Prepend the last assistant turn from parent history as a preamble.
59    #[default]
60    LastAssistantTurn,
61    /// LLM-generated summary of parent context (not yet implemented in Phase 1).
62    Summary,
63}
64
65fn default_max_tool_iterations() -> usize {
66    10
67}
68
69fn default_auto_update_check() -> bool {
70    true
71}
72
73fn default_focus_compression_interval() -> usize {
74    12
75}
76
77fn default_focus_reminder_interval() -> usize {
78    15
79}
80
81fn default_focus_min_messages_per_focus() -> usize {
82    8
83}
84
85fn default_focus_max_knowledge_tokens() -> usize {
86    4096
87}
88
89fn default_focus_auto_consolidate_min_window() -> usize {
90    6
91}
92
93fn default_max_tool_retries() -> usize {
94    2
95}
96
97fn default_max_retry_duration_secs() -> u64 {
98    30
99}
100
101fn default_tool_repeat_threshold() -> usize {
102    2
103}
104
105fn default_tool_filter_top_k() -> usize {
106    6
107}
108
109fn default_tool_filter_min_description_words() -> usize {
110    5
111}
112
113fn default_tool_filter_always_on() -> Vec<String> {
114    vec![
115        "memory_search".into(),
116        "memory_save".into(),
117        "load_skill".into(),
118        "invoke_skill".into(),
119        "bash".into(),
120        "read".into(),
121        "edit".into(),
122    ]
123}
124
125fn default_instruction_auto_detect() -> bool {
126    true
127}
128
129fn default_max_concurrent() -> usize {
130    5
131}
132
133fn default_context_window_turns() -> usize {
134    10
135}
136
137fn default_max_spawn_depth() -> u32 {
138    3
139}
140
141fn default_transcript_enabled() -> bool {
142    true
143}
144
145fn default_transcript_max_files() -> usize {
146    50
147}
148
149/// Configuration for focus-based active context compression (#1850).
150#[derive(Debug, Clone, Deserialize, Serialize)]
151#[serde(default)]
152pub struct FocusConfig {
153    /// Enable focus tools (`start_focus` / `complete_focus`). Default: `false`.
154    pub enabled: bool,
155    /// Suggest focus after this many turns without one. Default: `12`.
156    #[serde(default = "default_focus_compression_interval")]
157    pub compression_interval: usize,
158    /// Remind the agent every N turns when focus is overdue. Default: `15`.
159    #[serde(default = "default_focus_reminder_interval")]
160    pub reminder_interval: usize,
161    /// Minimum messages required before suggesting a focus. Default: `8`.
162    #[serde(default = "default_focus_min_messages_per_focus")]
163    pub min_messages_per_focus: usize,
164    /// Maximum tokens the Knowledge block may grow to before old entries are trimmed.
165    /// Default: `4096`.
166    #[serde(default = "default_focus_max_knowledge_tokens")]
167    pub max_knowledge_tokens: usize,
168    /// Minimum turns since the last auto-consolidation before the next one fires.
169    ///
170    /// Must be >= 1. `Config::validate()` rejects `0` at startup. Default: `6`.
171    #[serde(default = "default_focus_auto_consolidate_min_window")]
172    pub auto_consolidate_min_window: usize,
173}
174
175impl Default for FocusConfig {
176    fn default() -> Self {
177        Self {
178            enabled: false,
179            compression_interval: default_focus_compression_interval(),
180            reminder_interval: default_focus_reminder_interval(),
181            min_messages_per_focus: default_focus_min_messages_per_focus(),
182            max_knowledge_tokens: default_focus_max_knowledge_tokens(),
183            auto_consolidate_min_window: default_focus_auto_consolidate_min_window(),
184        }
185    }
186}
187
188/// Dynamic tool schema filtering configuration (#2020).
189///
190/// When enabled, only a subset of tool definitions is sent to the LLM on each turn,
191/// selected by embedding similarity between the user query and tool descriptions.
192#[derive(Debug, Clone, Deserialize, Serialize)]
193#[serde(default)]
194pub struct ToolFilterConfig {
195    /// Enable dynamic tool schema filtering. Default: `false` (opt-in).
196    pub enabled: bool,
197    /// Number of top-scoring filterable tools to include per turn.
198    /// Set to `0` to include all filterable tools.
199    #[serde(default = "default_tool_filter_top_k")]
200    pub top_k: usize,
201    /// Tool IDs that are never filtered out.
202    #[serde(default = "default_tool_filter_always_on")]
203    pub always_on: Vec<String>,
204    /// MCP tools with fewer description words than this are auto-included.
205    #[serde(default = "default_tool_filter_min_description_words")]
206    pub min_description_words: usize,
207}
208
209impl Default for ToolFilterConfig {
210    fn default() -> Self {
211        Self {
212            enabled: false,
213            top_k: default_tool_filter_top_k(),
214            always_on: default_tool_filter_always_on(),
215            min_description_words: default_tool_filter_min_description_words(),
216        }
217    }
218}
219
220/// Core agent behavior configuration, nested under `[agent]` in TOML.
221///
222/// Controls the agent's name, tool-loop limits, instruction loading, and retry
223/// behavior. All fields have sensible defaults; only `name` is typically changed
224/// by end users.
225///
226/// # Example (TOML)
227///
228/// ```toml
229/// [agent]
230/// name = "Zeph"
231/// max_tool_iterations = 15
232/// max_tool_retries = 3
233/// ```
234#[derive(Debug, Deserialize, Serialize)]
235pub struct AgentConfig {
236    /// Human-readable agent name surfaced in the TUI and Telegram header. Default: `"Zeph"`.
237    pub name: String,
238    /// Maximum number of tool-call iterations per agent turn before the loop is aborted.
239    /// Must be `<= 100`. Default: `10`.
240    #[serde(default = "default_max_tool_iterations")]
241    pub max_tool_iterations: usize,
242    /// Check for new Zeph releases at startup. Default: `true`.
243    #[serde(default = "default_auto_update_check")]
244    pub auto_update_check: bool,
245    /// Additional instruction files to always load, regardless of provider.
246    #[serde(default)]
247    pub instruction_files: Vec<std::path::PathBuf>,
248    /// When true, automatically detect provider-specific instruction files
249    /// (e.g. `CLAUDE.md` for Claude, `AGENTS.md` for `OpenAI`).
250    #[serde(default = "default_instruction_auto_detect")]
251    pub instruction_auto_detect: bool,
252    /// Maximum retry attempts for transient tool errors (0 to disable).
253    #[serde(default = "default_max_tool_retries")]
254    pub max_tool_retries: usize,
255    /// Number of identical tool+args calls within the recent window to trigger repeat-detection
256    /// abort (0 to disable).
257    #[serde(default = "default_tool_repeat_threshold")]
258    pub tool_repeat_threshold: usize,
259    /// Maximum total wall-clock time (seconds) to spend on retries for a single tool call.
260    #[serde(default = "default_max_retry_duration_secs")]
261    pub max_retry_duration_secs: u64,
262    /// Focus-based active context compression configuration (#1850).
263    #[serde(default)]
264    pub focus: FocusConfig,
265    /// Dynamic tool schema filtering configuration (#2020).
266    #[serde(default)]
267    pub tool_filter: ToolFilterConfig,
268    /// Inject a `<budget>` XML block into the volatile system prompt section so the LLM
269    /// can self-regulate tool calls and cost. Self-suppresses when no budget data is
270    /// available (#2267).
271    #[serde(default = "default_budget_hint_enabled")]
272    pub budget_hint_enabled: bool,
273    /// Background task supervisor tuning. Controls concurrency limits and turn-boundary abort.
274    #[serde(default)]
275    pub supervisor: TaskSupervisorConfig,
276}
277
278fn default_budget_hint_enabled() -> bool {
279    true
280}
281
282fn default_enrichment_limit() -> usize {
283    4
284}
285
286fn default_telemetry_limit() -> usize {
287    8
288}
289
290fn default_background_shell_limit() -> usize {
291    8
292}
293
294/// Background task supervisor configuration, nested under `[agent.supervisor]` in TOML.
295///
296/// Controls per-class concurrency limits and turn-boundary behaviour for the
297/// `BackgroundSupervisor` in `zeph-core`.
298/// All fields have sensible defaults that match the Phase 1 hardcoded values; only change
299/// these if you observe excessive background task drops under load.
300///
301/// # Example (TOML)
302///
303/// ```toml
304/// [agent.supervisor]
305/// enrichment_limit = 4
306/// telemetry_limit = 8
307/// abort_enrichment_on_turn = false
308/// ```
309#[derive(Debug, Clone, Deserialize, Serialize)]
310#[serde(default)]
311pub struct TaskSupervisorConfig {
312    /// Maximum concurrent enrichment tasks (summarization, graph/persona/trajectory extraction).
313    /// Default: `4`.
314    #[serde(default = "default_enrichment_limit")]
315    pub enrichment_limit: usize,
316    /// Maximum concurrent telemetry tasks (audit log writes, graph count sync).
317    /// Default: `8`.
318    #[serde(default = "default_telemetry_limit")]
319    pub telemetry_limit: usize,
320    /// Abort all inflight enrichment tasks at turn boundary to prevent backlog buildup.
321    /// Default: `false`.
322    #[serde(default)]
323    pub abort_enrichment_on_turn: bool,
324    /// Maximum concurrent background shell runs tracked by the supervisor.
325    ///
326    /// Should match `tools.shell.max_background_runs` so both layers agree on capacity.
327    /// Default: `8`.
328    #[serde(default = "default_background_shell_limit")]
329    pub background_shell_limit: usize,
330}
331
332impl Default for TaskSupervisorConfig {
333    fn default() -> Self {
334        Self {
335            enrichment_limit: default_enrichment_limit(),
336            telemetry_limit: default_telemetry_limit(),
337            abort_enrichment_on_turn: false,
338            background_shell_limit: default_background_shell_limit(),
339        }
340    }
341}
342
343/// Sub-agent pool configuration, nested under `[agents]` in TOML.
344///
345/// When `enabled = true`, the agent can spawn isolated sub-agent sessions from
346/// SKILL.md-based agent definitions. Sub-agents inherit the parent's provider pool
347/// unless overridden by `model` in their definition frontmatter.
348///
349/// # Example (TOML)
350///
351/// ```toml
352/// [agents]
353/// enabled = true
354/// max_concurrent = 3
355/// max_spawn_depth = 2
356/// ```
357#[derive(Debug, Clone, Deserialize, Serialize)]
358#[serde(default)]
359pub struct SubAgentConfig {
360    /// Enable the sub-agent subsystem. Default: `false`.
361    pub enabled: bool,
362    /// Maximum number of sub-agents that can run concurrently.
363    #[serde(default = "default_max_concurrent")]
364    pub max_concurrent: usize,
365    /// Additional directories to search for `.agent.md` definition files.
366    pub extra_dirs: Vec<PathBuf>,
367    /// User-level agents directory.
368    #[serde(default)]
369    pub user_agents_dir: Option<PathBuf>,
370    /// Default permission mode applied to sub-agents that do not specify one.
371    pub default_permission_mode: Option<PermissionMode>,
372    /// Global denylist applied to all sub-agents in addition to per-agent `tools.except`.
373    #[serde(default)]
374    pub default_disallowed_tools: Vec<String>,
375    /// Allow sub-agents to use `bypass_permissions` mode.
376    #[serde(default)]
377    pub allow_bypass_permissions: bool,
378    /// Default memory scope applied to sub-agents that do not set `memory` in their definition.
379    #[serde(default)]
380    pub default_memory_scope: Option<MemoryScope>,
381    /// Lifecycle hooks executed when any sub-agent starts or stops.
382    #[serde(default)]
383    pub hooks: SubAgentLifecycleHooks,
384    /// Directory where transcript JSONL files and meta sidecars are stored.
385    #[serde(default)]
386    pub transcript_dir: Option<PathBuf>,
387    /// Enable writing JSONL transcripts for sub-agent sessions.
388    #[serde(default = "default_transcript_enabled")]
389    pub transcript_enabled: bool,
390    /// Maximum number of `.jsonl` transcript files to keep.
391    #[serde(default = "default_transcript_max_files")]
392    pub transcript_max_files: usize,
393    /// Number of recent parent conversation turns to pass to spawned sub-agents.
394    /// Set to 0 to disable history propagation.
395    #[serde(default = "default_context_window_turns")]
396    pub context_window_turns: usize,
397    /// Maximum nesting depth for sub-agent spawns.
398    #[serde(default = "default_max_spawn_depth")]
399    pub max_spawn_depth: u32,
400    /// How parent context is injected into the sub-agent's task prompt.
401    #[serde(default)]
402    pub context_injection_mode: ContextInjectionMode,
403}
404
405impl Default for SubAgentConfig {
406    fn default() -> Self {
407        Self {
408            enabled: false,
409            max_concurrent: default_max_concurrent(),
410            extra_dirs: Vec::new(),
411            user_agents_dir: None,
412            default_permission_mode: None,
413            default_disallowed_tools: Vec::new(),
414            allow_bypass_permissions: false,
415            default_memory_scope: None,
416            hooks: SubAgentLifecycleHooks::default(),
417            transcript_dir: None,
418            transcript_enabled: default_transcript_enabled(),
419            transcript_max_files: default_transcript_max_files(),
420            context_window_turns: default_context_window_turns(),
421            max_spawn_depth: default_max_spawn_depth(),
422            context_injection_mode: ContextInjectionMode::default(),
423        }
424    }
425}
426
427/// Config-level lifecycle hooks fired when any sub-agent starts or stops.
428#[derive(Debug, Clone, Default, Deserialize, Serialize)]
429#[serde(default)]
430pub struct SubAgentLifecycleHooks {
431    /// Hooks run after a sub-agent is spawned (fire-and-forget).
432    pub start: Vec<HookDef>,
433    /// Hooks run after a sub-agent finishes or is cancelled (fire-and-forget).
434    pub stop: Vec<HookDef>,
435}
436
437#[cfg(test)]
438mod tests {
439    use super::*;
440
441    #[test]
442    fn subagent_config_defaults() {
443        let cfg = SubAgentConfig::default();
444        assert_eq!(cfg.context_window_turns, 10);
445        assert_eq!(cfg.max_spawn_depth, 3);
446        assert_eq!(
447            cfg.context_injection_mode,
448            ContextInjectionMode::LastAssistantTurn
449        );
450    }
451
452    #[test]
453    fn subagent_config_deserialize_new_fields() {
454        let toml_str = r#"
455            enabled = true
456            context_window_turns = 5
457            max_spawn_depth = 2
458            context_injection_mode = "none"
459        "#;
460        let cfg: SubAgentConfig = toml::from_str(toml_str).unwrap();
461        assert_eq!(cfg.context_window_turns, 5);
462        assert_eq!(cfg.max_spawn_depth, 2);
463        assert_eq!(cfg.context_injection_mode, ContextInjectionMode::None);
464    }
465
466    #[test]
467    fn model_spec_deserialize_inherit() {
468        let spec: ModelSpec = serde_json::from_str("\"inherit\"").unwrap();
469        assert_eq!(spec, ModelSpec::Inherit);
470    }
471
472    #[test]
473    fn model_spec_deserialize_named() {
474        let spec: ModelSpec = serde_json::from_str("\"fast\"").unwrap();
475        assert_eq!(spec, ModelSpec::Named("fast".to_owned()));
476    }
477
478    #[test]
479    fn model_spec_as_str() {
480        assert_eq!(ModelSpec::Inherit.as_str(), "inherit");
481        assert_eq!(ModelSpec::Named("x".to_owned()).as_str(), "x");
482    }
483
484    #[test]
485    fn focus_config_auto_consolidate_min_window_default_is_six() {
486        let cfg = FocusConfig::default();
487        assert_eq!(cfg.auto_consolidate_min_window, 6);
488    }
489
490    #[test]
491    fn focus_config_auto_consolidate_min_window_deserializes() {
492        let toml_str = "auto_consolidate_min_window = 10";
493        let cfg: FocusConfig = toml::from_str(toml_str).unwrap();
494        assert_eq!(cfg.auto_consolidate_min_window, 10);
495    }
496}