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