Skip to main content

lean_ctx/core/config/
sections.rs

1//! Auxiliary configuration section structs.
2//!
3//! Nested config structs (secret-detection, setup, archive, providers,
4//! autonomy, updates, cloud, gain, loop-detection, embedding, …) split out of
5//! `config/mod.rs` to keep the top-level module focused on `Config` itself.
6//! Re-exported via `pub use sections::*`, so external paths stay stable.
7
8use super::serde_defaults;
9#[allow(clippy::wildcard_imports)]
10use super::*;
11use serde::{Deserialize, Serialize};
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14#[serde(default)]
15pub struct SecretDetectionConfig {
16    pub enabled: bool,
17    pub redact: bool,
18    pub custom_patterns: Vec<String>,
19}
20
21/// Controls what lean-ctx injects during `setup` and `update --rewire`.
22/// Fresh installs default to non-invasive (rules/skills off, MCP on).
23/// Users who ran setup interactively get explicit true/false.
24/// `None` = undecided (legacy: check if rules already exist and preserve behavior).
25#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(default)]
27pub struct SetupConfig {
28    /// Inject agent rule files (CLAUDE.md, .cursor/rules/, etc.).
29    /// None = undecided (legacy compat: inject if rules already present).
30    /// Some(true) = always inject. Some(false) = never inject.
31    pub auto_inject_rules: Option<bool>,
32    /// Install SKILL.md files for supported agents.
33    /// None = undecided. Some(true) = install. Some(false) = skip.
34    pub auto_inject_skills: Option<bool>,
35    /// Register lean-ctx as an MCP server in editor configs.
36    #[serde(default = "serde_defaults::default_true")]
37    pub auto_update_mcp: bool,
38}
39
40impl Default for SetupConfig {
41    fn default() -> Self {
42        Self {
43            auto_inject_rules: None,
44            auto_inject_skills: None,
45            auto_update_mcp: true,
46        }
47    }
48}
49
50impl SetupConfig {
51    /// Returns whether rules should be injected, considering legacy installs.
52    /// If undecided (None), checks if lean-ctx rules markers already exist
53    /// in any agent config — if so, keeps injecting for backward compat.
54    pub fn should_inject_rules(&self) -> bool {
55        match self.auto_inject_rules {
56            Some(v) => v,
57            None => Self::rules_already_present(),
58        }
59    }
60
61    /// Returns whether skills should be installed.
62    pub fn should_inject_skills(&self) -> bool {
63        match self.auto_inject_skills {
64            Some(v) => v,
65            None => Self::rules_already_present(),
66        }
67    }
68
69    /// Returns whether `setup`/`onboard`/`init` may (re)register the lean-ctx
70    /// MCP server in editor configs. Honors `auto_update_mcp` (#281) so locked-
71    /// down environments can keep MCP out of agent settings while still getting
72    /// hooks, rules and skills.
73    pub fn should_update_mcp(&self) -> bool {
74        self.auto_update_mcp
75    }
76
77    /// Check if lean-ctx rules markers exist in any known agent config location.
78    ///
79    /// Delegates the per-agent path catalog to `rules_inject::any_rules_marker_present`
80    /// (derived from the injector's own target list) so this never drifts behind
81    /// newly supported agents again (#442). Claude Code and CodeBuddy have no
82    /// rules *target* (they auto-load an inline block instead), so their legacy
83    /// rule files are checked separately to keep honoring older installs.
84    fn rules_already_present() -> bool {
85        let Some(home) = dirs::home_dir() else {
86            return false;
87        };
88        if crate::rules_inject::any_rules_marker_present(&home) {
89            return true;
90        }
91        let legacy_paths = [
92            crate::core::editor_registry::claude_rules_dir(&home).join("lean-ctx.md"),
93            crate::core::editor_registry::codebuddy_rules_dir(&home).join("lean-ctx.md"),
94        ];
95        legacy_paths.iter().any(|p| {
96            std::fs::read_to_string(p)
97                .is_ok_and(|c| c.contains(crate::core::rules_canonical::START_MARK))
98        })
99    }
100}
101
102impl Default for SecretDetectionConfig {
103    fn default() -> Self {
104        Self {
105            enabled: true,
106            redact: true,
107            custom_patterns: Vec::new(),
108        }
109    }
110}
111
112/// Settings for the zero-loss compression archive (large tool outputs saved to disk).
113#[derive(Debug, Clone, Serialize, Deserialize)]
114#[serde(default)]
115pub struct ArchiveConfig {
116    pub enabled: bool,
117    pub threshold_chars: usize,
118    pub max_age_hours: u64,
119    pub max_disk_mb: u64,
120    pub ephemeral: bool,
121    /// Minimum output tokens before the ephemeral firewall replaces an inline tool
122    /// result with a summary + retrieval ref. Outputs below this stay fully inline.
123    pub ephemeral_min_tokens: usize,
124}
125
126impl Default for ArchiveConfig {
127    fn default() -> Self {
128        Self {
129            enabled: true,
130            threshold_chars: 800,
131            max_age_hours: 48,
132            max_disk_mb: 500,
133            ephemeral: true,
134            ephemeral_min_tokens: 2000,
135        }
136    }
137}
138
139impl ArchiveConfig {
140    pub fn ephemeral_effective(&self) -> bool {
141        if let Ok(v) = std::env::var("LEAN_CTX_EPHEMERAL") {
142            return !matches!(v.trim(), "0" | "false" | "off");
143        }
144        self.ephemeral && self.enabled
145    }
146
147    pub fn ephemeral_min_tokens_effective(&self) -> usize {
148        if let Ok(v) = std::env::var("LEAN_CTX_EPHEMERAL_MIN_TOKENS")
149            && let Ok(n) = v.trim().parse::<usize>()
150        {
151            return n;
152        }
153        self.ephemeral_min_tokens
154    }
155}
156
157/// Configuration for external context providers (GitHub, GitLab, Jira, etc.).
158/// Each provider can be enabled/disabled and configured with auth tokens.
159/// Override individual tokens via env vars (GITHUB_TOKEN, GITLAB_TOKEN, etc.).
160#[derive(Debug, Clone, Serialize, Deserialize)]
161#[serde(default)]
162pub struct ProvidersConfig {
163    /// Master switch for the provider subsystem.
164    pub enabled: bool,
165    /// GitHub provider configuration.
166    pub github: ProviderEntryConfig,
167    /// GitLab provider configuration.
168    pub gitlab: ProviderEntryConfig,
169    /// Auto-ingest provider results into BM25/embedding indexes.
170    pub auto_index: bool,
171    /// Default cache TTL for provider results (seconds).
172    pub cache_ttl_secs: u64,
173    /// MCP Bridge providers: `{ "name" = { url = "...", description = "..." } }`.
174    #[serde(default)]
175    pub mcp_bridges: std::collections::HashMap<String, McpBridgeEntry>,
176}
177
178impl Default for ProvidersConfig {
179    fn default() -> Self {
180        Self {
181            enabled: true,
182            github: ProviderEntryConfig::default(),
183            gitlab: ProviderEntryConfig::default(),
184            auto_index: true,
185            cache_ttl_secs: 120,
186            mcp_bridges: std::collections::HashMap::new(),
187        }
188    }
189}
190
191#[derive(Debug, Clone, Serialize, Deserialize)]
192pub struct McpBridgeEntry {
193    /// HTTP/SSE URL for remote MCP servers.
194    #[serde(default)]
195    pub url: Option<String>,
196    /// Command to spawn a local MCP server (stdio transport).
197    #[serde(default)]
198    pub command: Option<String>,
199    /// Arguments for the command.
200    #[serde(default)]
201    pub args: Vec<String>,
202    /// Human-readable description.
203    #[serde(default)]
204    pub description: Option<String>,
205    /// Environment variable name containing an auth token.
206    #[serde(default)]
207    pub auth_env: Option<String>,
208}
209
210/// Per-provider configuration entry.
211#[derive(Debug, Clone, Serialize, Deserialize)]
212#[serde(default)]
213pub struct ProviderEntryConfig {
214    /// Whether this specific provider is enabled.
215    pub enabled: bool,
216    /// Auth token (prefer env var; only use this for project-local overrides).
217    pub token: Option<String>,
218    /// API base URL override (for GitHub Enterprise, self-hosted GitLab, etc.).
219    pub api_url: Option<String>,
220    /// Default project/repo for this provider (auto-detected from git remote if empty).
221    pub project: Option<String>,
222}
223
224impl Default for ProviderEntryConfig {
225    fn default() -> Self {
226        Self {
227            enabled: true,
228            token: None,
229            api_url: None,
230            project: None,
231        }
232    }
233}
234
235/// Controls autonomous background behaviors (preload, dedup, consolidation).
236#[derive(Debug, Clone, Serialize, Deserialize)]
237#[serde(default)]
238pub struct AutonomyConfig {
239    pub enabled: bool,
240    pub auto_preload: bool,
241    pub auto_dedup: bool,
242    pub auto_related: bool,
243    pub auto_consolidate: bool,
244    pub silent_preload: bool,
245    pub dedup_threshold: usize,
246    pub consolidate_every_calls: u32,
247    pub consolidate_cooldown_secs: u64,
248    #[serde(default = "serde_defaults::default_true")]
249    pub cognition_loop_enabled: bool,
250    #[serde(default = "serde_defaults::default_cognition_loop_interval")]
251    pub cognition_loop_interval_secs: u64,
252    #[serde(default = "serde_defaults::default_cognition_loop_max_steps")]
253    pub cognition_loop_max_steps: u8,
254    /// Minimum facts an entity needs before observation synthesis (#802) writes a
255    /// summary. Synthesis itself is gated by `cognition_loop_max_steps >= 9`.
256    #[serde(default = "serde_defaults::default_cognition_synthesis_min_cluster")]
257    pub cognition_synthesis_min_cluster: usize,
258}
259
260impl Default for AutonomyConfig {
261    fn default() -> Self {
262        Self {
263            enabled: true,
264            auto_preload: true,
265            auto_dedup: true,
266            auto_related: true,
267            auto_consolidate: true,
268            silent_preload: true,
269            dedup_threshold: 8,
270            consolidate_every_calls: 25,
271            consolidate_cooldown_secs: 120,
272            cognition_loop_enabled: true,
273            cognition_loop_interval_secs: 3600,
274            cognition_loop_max_steps: 9,
275            cognition_synthesis_min_cluster: 3,
276        }
277    }
278}
279
280/// Controls automatic update behavior. All defaults are OFF — auto-updates
281/// require explicit opt-in via `lean-ctx setup` or `lean-ctx update --schedule`.
282#[derive(Debug, Clone, Serialize, Deserialize)]
283#[serde(default)]
284pub struct UpdatesConfig {
285    pub auto_update: bool,
286    pub check_interval_hours: u64,
287    pub notify_only: bool,
288}
289
290impl Default for UpdatesConfig {
291    fn default() -> Self {
292        Self {
293            auto_update: false,
294            check_interval_hours: 6,
295            notify_only: false,
296        }
297    }
298}
299
300impl UpdatesConfig {
301    pub fn from_env() -> Self {
302        let mut cfg = Self::default();
303        if let Ok(v) = std::env::var("LEAN_CTX_AUTO_UPDATE") {
304            cfg.auto_update = v == "1" || v.eq_ignore_ascii_case("true");
305        }
306        if let Ok(v) = std::env::var("LEAN_CTX_UPDATE_INTERVAL_HOURS")
307            && let Ok(h) = v.parse::<u64>()
308        {
309            cfg.check_interval_hours = h.clamp(1, 168);
310        }
311        if let Ok(v) = std::env::var("LEAN_CTX_UPDATE_NOTIFY_ONLY") {
312            cfg.notify_only = v == "1" || v.eq_ignore_ascii_case("true");
313        }
314        cfg
315    }
316}
317
318impl AutonomyConfig {
319    /// Creates an autonomy config from env vars, falling back to defaults.
320    pub fn from_env() -> Self {
321        let mut cfg = Self::default();
322        if let Ok(v) = std::env::var("LEAN_CTX_AUTONOMY")
323            && (v == "false" || v == "0")
324        {
325            cfg.enabled = false;
326        }
327        if let Ok(v) = std::env::var("LEAN_CTX_AUTO_PRELOAD") {
328            cfg.auto_preload = v != "false" && v != "0";
329        }
330        if let Ok(v) = std::env::var("LEAN_CTX_AUTO_DEDUP") {
331            cfg.auto_dedup = v != "false" && v != "0";
332        }
333        if let Ok(v) = std::env::var("LEAN_CTX_AUTO_RELATED") {
334            cfg.auto_related = v != "false" && v != "0";
335        }
336        if let Ok(v) = std::env::var("LEAN_CTX_AUTO_CONSOLIDATE") {
337            cfg.auto_consolidate = v != "false" && v != "0";
338        }
339        if let Ok(v) = std::env::var("LEAN_CTX_SILENT_PRELOAD") {
340            cfg.silent_preload = v != "false" && v != "0";
341        }
342        if let Ok(v) = std::env::var("LEAN_CTX_DEDUP_THRESHOLD")
343            && let Ok(n) = v.parse()
344        {
345            cfg.dedup_threshold = n;
346        }
347        if let Ok(v) = std::env::var("LEAN_CTX_CONSOLIDATE_EVERY_CALLS")
348            && let Ok(n) = v.parse()
349        {
350            cfg.consolidate_every_calls = n;
351        }
352        if let Ok(v) = std::env::var("LEAN_CTX_CONSOLIDATE_COOLDOWN_SECS")
353            && let Ok(n) = v.parse()
354        {
355            cfg.consolidate_cooldown_secs = n;
356        }
357        if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_LOOP_ENABLED") {
358            cfg.cognition_loop_enabled = v != "false" && v != "0";
359        }
360        if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_LOOP_INTERVAL_SECS")
361            && let Ok(n) = v.parse()
362        {
363            cfg.cognition_loop_interval_secs = n;
364        }
365        if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_LOOP_MAX_STEPS")
366            && let Ok(n) = v.parse()
367        {
368            cfg.cognition_loop_max_steps = n;
369        }
370        if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_SYNTHESIS_MIN_CLUSTER")
371            && let Ok(n) = v.parse()
372        {
373            cfg.cognition_synthesis_min_cluster = n;
374        }
375        cfg
376    }
377
378    /// Loads autonomy config from disk, with env var overrides applied.
379    pub fn load() -> Self {
380        let file_cfg = Config::load().autonomy;
381        let mut cfg = file_cfg;
382        if let Ok(v) = std::env::var("LEAN_CTX_AUTONOMY")
383            && (v == "false" || v == "0")
384        {
385            cfg.enabled = false;
386        }
387        if let Ok(v) = std::env::var("LEAN_CTX_AUTO_PRELOAD") {
388            cfg.auto_preload = v != "false" && v != "0";
389        }
390        if let Ok(v) = std::env::var("LEAN_CTX_AUTO_DEDUP") {
391            cfg.auto_dedup = v != "false" && v != "0";
392        }
393        if let Ok(v) = std::env::var("LEAN_CTX_AUTO_RELATED") {
394            cfg.auto_related = v != "false" && v != "0";
395        }
396        if let Ok(v) = std::env::var("LEAN_CTX_SILENT_PRELOAD") {
397            cfg.silent_preload = v != "false" && v != "0";
398        }
399        if let Ok(v) = std::env::var("LEAN_CTX_DEDUP_THRESHOLD")
400            && let Ok(n) = v.parse()
401        {
402            cfg.dedup_threshold = n;
403        }
404        if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_LOOP_ENABLED") {
405            cfg.cognition_loop_enabled = v != "false" && v != "0";
406        }
407        if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_LOOP_INTERVAL_SECS")
408            && let Ok(n) = v.parse()
409        {
410            cfg.cognition_loop_interval_secs = n;
411        }
412        if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_LOOP_MAX_STEPS")
413            && let Ok(n) = v.parse()
414        {
415            cfg.cognition_loop_max_steps = n;
416        }
417        if let Ok(v) = std::env::var("LEAN_CTX_COGNITION_SYNTHESIS_MIN_CLUSTER")
418            && let Ok(n) = v.parse()
419        {
420            cfg.cognition_synthesis_min_cluster = n;
421        }
422        cfg
423    }
424}
425
426/// Cloud sync and contribution settings (pattern sharing, model pulls).
427#[derive(Debug, Clone, Serialize, Deserialize, Default)]
428#[serde(default)]
429pub struct CloudConfig {
430    pub contribute_enabled: bool,
431    pub last_contribute: Option<String>,
432    pub last_sync: Option<String>,
433    pub last_gain_sync: Option<String>,
434    pub last_model_pull: Option<String>,
435    /// Auto-push the Pro Personal-Cloud surfaces (knowledge, commands, CEP,
436    /// gotchas, buddy, feedback) from the background task — opt-in, once per
437    /// day, offline-tolerant (GL #384). Toggle: `lean-ctx cloud autosync on`.
438    pub auto_sync: bool,
439    pub last_auto_sync: Option<String>,
440    /// Auto-push the project's encrypted retrieval-index bundle (hosted
441    /// Personal Index, GL #392) alongside the daily auto-sync — separate
442    /// opt-in because index bundles are orders of magnitude larger than the
443    /// other surfaces. Toggle: `lean-ctx cloud autoindex on`.
444    pub auto_index: bool,
445    /// Per-project debounce: `project_hash → YYYY-MM-DD` of the last
446    /// successful background index push.
447    pub last_index_push: std::collections::HashMap<String, String>,
448}
449
450/// Settings for publishing your token-savings recap (`gain --publish` / auto-publish).
451///
452/// Publishing is always opt-in: it sends a small, whitelisted *aggregate* payload (tokens
453/// saved, $ avoided, compression % — never code, paths or counts) to the cloud.
454/// `auto_publish` simply removes the need to re-run `gain --publish` by hand; it stays off
455/// until the user explicitly enables it.
456#[derive(Debug, Clone, Serialize, Deserialize)]
457#[serde(default)]
458pub struct GainConfig {
459    /// When true, `lean-ctx gain` automatically (re)publishes the recap, throttled to
460    /// `auto_publish_interval_hours`. Off by default.
461    pub auto_publish: bool,
462    /// When auto-publishing, also opt into the public leaderboard.
463    pub leaderboard: bool,
464    /// Optional display name for the published card / leaderboard entry.
465    pub display_name: Option<String>,
466    /// Minimum hours between automatic publishes (throttle).
467    pub auto_publish_interval_hours: u64,
468    /// Runtime state — RFC3339 timestamp of the last automatic publish. Managed by the
469    /// tool, not meant to be set by hand.
470    pub last_auto_publish: Option<String>,
471}
472
473impl Default for GainConfig {
474    fn default() -> Self {
475        Self {
476            auto_publish: false,
477            leaderboard: true,
478            display_name: None,
479            auto_publish_interval_hours: 24,
480            last_auto_publish: None,
481        }
482    }
483}
484
485/// Model declaration for **measured-vs-estimated** cost reporting.
486///
487/// Proxy-routed clients (Claude Code, Codex, Pi, Gemini CLI, OpenCode) report
488/// their real model and billed tokens, so lean-ctx prices them *measured* with
489/// no configuration. MCP-only IDEs (Cursor, Copilot, Windsurf, VS Code, Zed)
490/// send their LLM traffic straight to the provider, bypassing lean-ctx — their
491/// real model is invisible. Declaring it here lets those *estimated* turns be
492/// priced with the correct model instead of a blended fallback.
493#[derive(Debug, Clone, Default, Serialize, Deserialize)]
494#[serde(default)]
495pub struct CostConfig {
496    /// Fallback pricing model for any client without a per-client entry.
497    /// Unset/empty → lean-ctx keeps its blended heuristic.
498    #[serde(default, skip_serializing_if = "Option::is_none")]
499    pub default_model: Option<String>,
500    /// Per-client pricing model, keyed by client id (`cursor`, `copilot`,
501    /// `windsurf`, `claude`, `codex`, …). Used for MCP-only IDEs whose real
502    /// model lean-ctx cannot observe. Example:
503    /// `[cost.models]` then `cursor = "claude-opus-4.5"`.
504    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
505    pub models: HashMap<String, String>,
506}
507
508impl CostConfig {
509    /// Configured pricing model for a client id: the per-client entry first, then
510    /// the global default. `None` when neither is set (the caller then falls back
511    /// to the env override / heuristic). Blank entries are ignored.
512    pub fn model_for_client(&self, client: &str) -> Option<String> {
513        self.models
514            .get(client)
515            .or(self.default_model.as_ref())
516            .map(|s| s.trim().to_string())
517            .filter(|s| !s.is_empty())
518    }
519}
520
521/// Settings for the code graph — in particular the *traversal* (co-access) edges
522/// learned from real agent sessions (#289).
523///
524/// The static AST/import graph captures how code is wired structurally; it cannot
525/// see which files an agent actually opens *together* while solving a task.
526/// Traversal edges add that behavioural signal: files surfaced together are
527/// associated with a decaying weight (Hebbian co-access), folded into the graph
528/// as `co_access` edges and mixed into recall. The store is bounded and decays,
529/// so stale associations fade.
530#[derive(Debug, Clone, Serialize, Deserialize)]
531#[serde(default)]
532pub struct GraphConfig {
533    /// Record co-access between files surfaced together in a session, surface them
534    /// as decaying `co_access` edges in the graph, and boost recall by them.
535    /// On by default; set to `false` for a purely static (AST-only) graph.
536    pub traversal_edges: bool,
537}
538
539impl Default for GraphConfig {
540    fn default() -> Self {
541        Self {
542            traversal_edges: true,
543        }
544    }
545}
546
547/// Skillify (#290): mine the project's session diary + knowledge facts into
548/// versioned, git-committable `.cursor/rules/skillify-*.mdc` rule files.
549///
550/// The miner is precision-biased — it only codifies recurring or high-confidence
551/// patterns and never invents content. Runs on demand (`ctx_skillify` /
552/// `lean-ctx skillify`); re-running merges (bumps version) only when the distilled
553/// content actually changes.
554#[derive(Debug, Clone, Serialize, Deserialize)]
555#[serde(default)]
556pub struct SkillifyConfig {
557    /// Master switch for the skillify miner. On by default; the miner only ever
558    /// acts when explicitly invoked, so this never writes files unprompted.
559    pub enabled: bool,
560    /// Where generated rules are written: `project` (`<repo>/.cursor/rules`,
561    /// git-committable, default) or `global` (`~/.cursor/rules`).
562    pub scope: String,
563    /// Minimum confidence for a single curated knowledge fact to be codified even
564    /// without repetition. 0.0..=1.0.
565    pub min_confidence: f32,
566    /// Minimum number of reinforcements (confirmations / repeated mentions) before
567    /// a pattern is codified when its confidence is below `min_confidence`.
568    pub min_recurrence: u32,
569}
570
571impl Default for SkillifyConfig {
572    fn default() -> Self {
573        Self {
574            enabled: true,
575            scope: "project".to_string(),
576            min_confidence: 0.7,
577            min_recurrence: 2,
578        }
579    }
580}
581
582/// AI session summaries (#292): periodically distil the working session into a
583/// compact, *semantically recallable* summary so a future session can answer
584/// "what did I do last time on X?". Deterministic and local-first — recall uses
585/// embeddings when the `embeddings` feature is on, else a lexical fallback.
586#[derive(Debug, Clone, Serialize, Deserialize)]
587#[serde(default)]
588pub struct SummariesConfig {
589    /// Record periodic session summaries. On by default; recording is cheap and
590    /// happens at most once per `every_n_turns` tool calls.
591    pub enabled: bool,
592    /// Tool calls between automatic summaries. The auto-checkpoint cadence still
593    /// gates the check, so the effective minimum is the checkpoint interval.
594    pub every_n_turns: u32,
595    /// Maximum summaries kept per project (oldest pruned first).
596    pub max_kept: u32,
597}
598
599impl Default for SummariesConfig {
600    fn default() -> Self {
601        Self {
602            enabled: true,
603            every_n_turns: 25,
604            max_kept: 100,
605        }
606    }
607}
608
609/// A user-defined command alias mapping for shell compression patterns.
610#[derive(Debug, Clone, Serialize, Deserialize)]
611pub struct AliasEntry {
612    pub command: String,
613    pub alias: String,
614}
615
616/// Thresholds for detecting and throttling repetitive agent tool call loops.
617#[derive(Debug, Clone, Serialize, Deserialize)]
618#[serde(default)]
619pub struct LoopDetectionConfig {
620    pub normal_threshold: u32,
621    pub reduced_threshold: u32,
622    pub blocked_threshold: u32,
623    pub window_secs: u64,
624    pub search_group_limit: u32,
625    pub tool_total_limits: HashMap<String, u32>,
626}
627
628impl Default for LoopDetectionConfig {
629    fn default() -> Self {
630        let mut tool_total_limits = HashMap::new();
631        tool_total_limits.insert("ctx_read".to_string(), 100);
632        tool_total_limits.insert("ctx_search".to_string(), 80);
633        tool_total_limits.insert("ctx_shell".to_string(), 50);
634        tool_total_limits.insert("ctx_semantic_search".to_string(), 60);
635        Self {
636            normal_threshold: 2,
637            reduced_threshold: 4,
638            blocked_threshold: 0,
639            window_secs: 300,
640            search_group_limit: 10,
641            tool_total_limits,
642        }
643    }
644}
645
646/// Semantic-embedding engine settings.
647///
648/// `model` selects which local ONNX embedding model lean-ctx downloads and uses for
649/// `ctx_semantic_search`. Accepts the same aliases as the `LEAN_CTX_EMBEDDING_MODEL` env
650/// var: `minilm` (all-MiniLM-L6-v2, 384d — the default), `nomic` (768d) — or any
651/// HuggingFace repo with an ONNX export via `hf:org/repo[@revision]` (GL #397), e.g.
652/// `hf:jinaai/jina-embeddings-v2-base-code` for code-specialized embeddings. When the
653/// env var is set it takes precedence; an
654/// unset/`None` value uses the default model. Switching models triggers a one-time
655/// re-index on the next semantic search (vector dimensions follow from the model).
656///
657/// `dimensions` is only consulted for `hf:` custom models as the declared fallback
658/// width; the real width is probed from the ONNX graph at load time. Built-ins ignore it.
659#[derive(Debug, Clone, Default, Serialize, Deserialize)]
660#[serde(default)]
661pub struct EmbeddingConfig {
662    #[serde(default, skip_serializing_if = "Option::is_none")]
663    pub model: Option<String>,
664    #[serde(default, skip_serializing_if = "Option::is_none")]
665    pub dimensions: Option<usize>,
666    /// Allow downloading the embedding model on first semantic need (#551).
667    /// `None` (unset) means **allowed** — the soft default that activates the
668    /// semantic features without manual setup. Set `false` for air-gapped
669    /// machines. The `LEAN_CTX_EMBEDDINGS_AUTO_DOWNLOAD` env var, when set,
670    /// overrides this in either direction.
671    #[serde(default, skip_serializing_if = "Option::is_none")]
672    pub auto_download: Option<bool>,
673    /// Pin embedding inference to a single CPU thread (no GPU EP) so vectors are
674    /// bit-identical across machines, not just run-to-run on one host (#895).
675    /// `None`/`false` keeps the multi-threaded GPU-capable path. Extractive prose
676    /// ranking is already deterministic via score quantization + stable tiebreak;
677    /// this flag is the extra hardening for cross-machine reproducibility. The
678    /// `LEAN_CTX_EMBEDDING_DETERMINISTIC` env var overrides this either way.
679    #[serde(default, skip_serializing_if = "Option::is_none")]
680    pub deterministic: Option<bool>,
681}