Skip to main content

zeph_core/agent/
session_config.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use std::sync::Arc;
5
6use crate::config::{
7    Config, DebugConfig, DocumentConfig, GraphConfig, LearningConfig, OrchestrationConfig,
8    SecurityConfig, TimeoutConfig,
9};
10use crate::vault::Secret;
11
12/// Reserve ratio for `with_context_budget`: fraction of budget reserved for LLM reply.
13///
14/// Extracted from the hardcoded `0.20` literal used in both `spawn_acp_agent` and `runner.rs`.
15pub const CONTEXT_BUDGET_RESERVE_RATIO: f32 = 0.20;
16
17/// All config-derived fields needed to configure an `Agent` session.
18///
19/// This is the single source of truth for config → agent wiring.
20/// Adding a new config field requires exactly three changes:
21///
22/// 1. Add the field here.
23/// 2. Map it in [`AgentSessionConfig::from_config`].
24/// 3. Apply it in [`super::Agent::apply_session_config`] (destructure triggers a compile error if
25///    you forget step 3 — see the S4 note in the critic handoff).
26///
27/// ## What is NOT here
28///
29/// - **Shared runtime objects** (`provider`, `registry`, `memory`, `mcp_manager`, etc.) — these
30///   are expensive to create and shared across sessions; they stay in `SharedAgentDeps`.
31/// - **ACP-specific fields** (`acp_max_sessions`, bearer token, etc.) — transport-level, not
32///   agent-level.
33/// - **Optional runtime providers** (`summary_provider`, `judge_provider`,
34///   `quarantine_provider`) — these contain HTTP client pools (`AnyProvider`) that carry runtime
35///   state; callers wire them separately via `with_summary_provider` / `with_judge_provider` /
36///   `apply_quarantine_provider`.
37/// - **`mcp_config`** — passed alongside runtime MCP objects in `with_mcp()`; separating it
38///   from `mcp_tools` / `mcp_manager` would make the call site awkward.
39/// - **Runner-only fields** (`compression`, `routing`, `autosave`, `hybrid_search`, `trust_config`,
40///   `disambiguation_threshold`, `logging_config`, `subagent`, `experiment`, `instruction`,
41///   `lsp_hooks`, `response_cache`, `cost_tracker`) — not used in ACP sessions; keeping them out
42///   avoids unused-field noise and prevents inadvertent ACP behavior changes.
43/// - **Scheduler runtime objects** (`scheduler_executor`, broadcast senders) — runtime state,
44///   not config-derived values.
45#[derive(Clone)]
46#[allow(clippy::struct_excessive_bools)]
47pub struct AgentSessionConfig {
48    // Tool behavior
49    pub max_tool_iterations: usize,
50    pub max_tool_retries: usize,
51    pub max_retry_duration_secs: u64,
52    pub retry_base_ms: u64,
53    pub retry_max_ms: u64,
54    pub parameter_reformat_provider: String,
55    pub tool_repeat_threshold: usize,
56    pub tool_summarization: bool,
57    pub tool_call_cutoff: usize,
58    pub overflow_config: zeph_tools::OverflowConfig,
59    pub permission_policy: zeph_tools::PermissionPolicy,
60
61    // Model
62    pub model_name: String,
63    pub embed_model: String,
64
65    // Semantic cache
66    pub semantic_cache_enabled: bool,
67    pub semantic_cache_threshold: f32,
68    pub semantic_cache_max_candidates: u32,
69
70    // Memory / compaction
71    pub budget_tokens: usize,
72    pub soft_compaction_threshold: f32,
73    pub hard_compaction_threshold: f32,
74    pub compaction_preserve_tail: usize,
75    pub compaction_cooldown_turns: u8,
76    pub prune_protect_tokens: usize,
77    pub redact_credentials: bool,
78
79    // Security
80    pub security: SecurityConfig,
81    pub timeouts: TimeoutConfig,
82
83    // Feature configs
84    pub learning: LearningConfig,
85    pub document_config: DocumentConfig,
86    pub graph_config: GraphConfig,
87    pub anomaly_config: zeph_tools::AnomalyConfig,
88    pub result_cache_config: zeph_tools::ResultCacheConfig,
89    pub utility_config: zeph_tools::UtilityScoringConfig,
90    pub orchestration_config: OrchestrationConfig,
91    pub debug_config: DebugConfig,
92    pub server_compaction: bool,
93
94    /// Inject `<budget>` XML into the volatile system prompt section (#2267).
95    pub budget_hint_enabled: bool,
96
97    /// Custom secrets from config.
98    ///
99    /// Stored as `Arc` because `Secret` intentionally does not implement `Clone` —
100    /// the wrapper prevents accidental duplication. Iteration produces new `Secret`
101    /// values via `Secret::new(v.expose())` on the consumption side.
102    pub secrets: Arc<[(String, Secret)]>,
103}
104
105impl AgentSessionConfig {
106    /// Build from a resolved [`Config`] snapshot and a pre-computed `budget_tokens`.
107    ///
108    /// `budget_tokens` is passed as a parameter because its computation (`auto_budget_tokens`)
109    /// depends on the active provider and must happen before `AgentSessionConfig` is constructed.
110    #[must_use]
111    pub fn from_config(config: &Config, budget_tokens: usize) -> Self {
112        Self {
113            max_tool_iterations: config.agent.max_tool_iterations,
114            max_tool_retries: config.tools.retry.max_attempts,
115            max_retry_duration_secs: config.tools.retry.budget_secs,
116            retry_base_ms: config.tools.retry.base_ms,
117            retry_max_ms: config.tools.retry.max_ms,
118            parameter_reformat_provider: config.tools.retry.parameter_reformat_provider.clone(),
119            tool_repeat_threshold: config.agent.tool_repeat_threshold,
120            tool_summarization: config.tools.summarize_output,
121            tool_call_cutoff: config.memory.tool_call_cutoff,
122            overflow_config: config.tools.overflow.clone(),
123            permission_policy: config
124                .tools
125                .permission_policy(config.security.autonomy_level),
126            model_name: config.llm.effective_model().to_owned(),
127            embed_model: crate::bootstrap::effective_embedding_model(config),
128            semantic_cache_enabled: config.llm.semantic_cache_enabled,
129            semantic_cache_threshold: config.llm.semantic_cache_threshold,
130            semantic_cache_max_candidates: config.llm.semantic_cache_max_candidates,
131            budget_tokens,
132            soft_compaction_threshold: config.memory.soft_compaction_threshold,
133            hard_compaction_threshold: config.memory.hard_compaction_threshold,
134            compaction_preserve_tail: config.memory.compaction_preserve_tail,
135            compaction_cooldown_turns: config.memory.compaction_cooldown_turns,
136            prune_protect_tokens: config.memory.prune_protect_tokens,
137            redact_credentials: config.memory.redact_credentials,
138            security: config.security.clone(),
139            timeouts: config.timeouts,
140            learning: config.skills.learning.clone(),
141            document_config: config.memory.documents.clone(),
142            graph_config: config.memory.graph.clone(),
143            anomaly_config: config.tools.anomaly.clone(),
144            result_cache_config: config.tools.result_cache.clone(),
145            utility_config: config.tools.utility.clone(),
146            orchestration_config: config.orchestration.clone(),
147            debug_config: config.debug.clone(),
148            server_compaction: config.llm.providers.iter().any(|e| e.server_compaction),
149            budget_hint_enabled: config.agent.budget_hint_enabled,
150            secrets: config
151                .secrets
152                .custom
153                .iter()
154                .map(|(k, v)| (k.clone(), Secret::new(v.expose().to_owned())))
155                .collect::<Vec<_>>()
156                .into(),
157        }
158    }
159}
160
161#[cfg(test)]
162mod tests {
163    use super::*;
164
165    #[test]
166    fn from_config_maps_all_fields() {
167        let config = Config::default();
168        let budget = 100_000;
169        let sc = AgentSessionConfig::from_config(&config, budget);
170
171        assert_eq!(sc.max_tool_iterations, config.agent.max_tool_iterations);
172        assert_eq!(sc.max_tool_retries, config.tools.retry.max_attempts);
173        assert_eq!(sc.max_retry_duration_secs, config.tools.retry.budget_secs);
174        assert_eq!(sc.retry_base_ms, config.tools.retry.base_ms);
175        assert_eq!(sc.retry_max_ms, config.tools.retry.max_ms);
176        assert_eq!(
177            sc.parameter_reformat_provider,
178            config.tools.retry.parameter_reformat_provider
179        );
180        assert_eq!(sc.tool_repeat_threshold, config.agent.tool_repeat_threshold);
181        assert_eq!(sc.tool_summarization, config.tools.summarize_output);
182        assert_eq!(sc.tool_call_cutoff, config.memory.tool_call_cutoff);
183        assert_eq!(sc.model_name, config.llm.effective_model());
184        assert_eq!(
185            sc.embed_model,
186            crate::bootstrap::effective_embedding_model(&config)
187        );
188        assert_eq!(sc.semantic_cache_enabled, config.llm.semantic_cache_enabled);
189        assert!(
190            (sc.semantic_cache_threshold - config.llm.semantic_cache_threshold).abs()
191                < f32::EPSILON
192        );
193        assert_eq!(
194            sc.semantic_cache_max_candidates,
195            config.llm.semantic_cache_max_candidates
196        );
197        assert_eq!(sc.budget_tokens, budget);
198        assert!(
199            (sc.soft_compaction_threshold - config.memory.soft_compaction_threshold).abs()
200                < f32::EPSILON
201        );
202        assert!(
203            (sc.hard_compaction_threshold - config.memory.hard_compaction_threshold).abs()
204                < f32::EPSILON
205        );
206        assert_eq!(
207            sc.compaction_preserve_tail,
208            config.memory.compaction_preserve_tail
209        );
210        assert_eq!(
211            sc.compaction_cooldown_turns,
212            config.memory.compaction_cooldown_turns
213        );
214        assert_eq!(sc.prune_protect_tokens, config.memory.prune_protect_tokens);
215        assert_eq!(sc.redact_credentials, config.memory.redact_credentials);
216        assert_eq!(sc.graph_config.enabled, config.memory.graph.enabled);
217        assert_eq!(
218            sc.orchestration_config.enabled,
219            config.orchestration.enabled
220        );
221        assert_eq!(
222            sc.orchestration_config.max_tasks,
223            config.orchestration.max_tasks
224        );
225        assert_eq!(sc.anomaly_config.enabled, config.tools.anomaly.enabled);
226        assert_eq!(
227            sc.result_cache_config.enabled,
228            config.tools.result_cache.enabled
229        );
230        assert_eq!(
231            sc.result_cache_config.ttl_secs,
232            config.tools.result_cache.ttl_secs
233        );
234        assert_eq!(sc.debug_config.enabled, config.debug.enabled);
235        assert_eq!(
236            sc.document_config.rag_enabled,
237            config.memory.documents.rag_enabled
238        );
239        assert_eq!(
240            sc.overflow_config.threshold,
241            config.tools.overflow.threshold
242        );
243        assert_eq!(
244            sc.permission_policy.autonomy_level(),
245            config.security.autonomy_level
246        );
247        assert_eq!(sc.security.autonomy_level, config.security.autonomy_level);
248        assert_eq!(sc.timeouts.llm_seconds, config.timeouts.llm_seconds);
249        assert_eq!(sc.learning.enabled, config.skills.learning.enabled);
250        assert_eq!(
251            sc.server_compaction,
252            config.llm.providers.iter().any(|e| e.server_compaction)
253        );
254        assert_eq!(sc.secrets.len(), config.secrets.custom.len());
255    }
256}