Skip to main content

lean_ctx/tools/
autonomy.rs

1use std::collections::{HashMap, HashSet};
2use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
3use std::sync::Mutex;
4use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
5
6use crate::core::autonomy_drivers::{
7    AutonomyDriverDecisionV1, AutonomyDriverEventV1, AutonomyDriverKindV1, AutonomyPhaseV1,
8    AutonomyVerdictV1,
9};
10use crate::core::cache::SessionCache;
11use crate::core::config::AutonomyConfig;
12use crate::core::graph_index::ProjectIndex;
13use crate::core::protocol;
14use crate::core::tokens::count_tokens;
15use crate::tools::CrpMode;
16
17#[cfg(test)]
18const SEARCH_REPEAT_IDLE_RESET: Duration = Duration::from_millis(500);
19#[cfg(not(test))]
20const SEARCH_REPEAT_IDLE_RESET: Duration = Duration::from_mins(5);
21
22/// Per-key stats for progressive search hints (`ctx_search` / `ctx_semantic_search`).
23#[derive(Debug, Clone)]
24pub struct SearchHistory {
25    pub call_count: u32,
26    pub last_call: Instant,
27}
28
29/// Tracks autonomous action state: session init, dedup, and consolidation timing.
30pub struct AutonomyState {
31    pub session_initialized: AtomicBool,
32    pub dedup_applied: AtomicBool,
33    pub last_consolidation_unix: AtomicU64,
34    pub config: AutonomyConfig,
35    /// Repeated `pattern|path` keys for search tools (see [`AutonomyState::track_search`]).
36    pub search_repetition: Mutex<HashMap<String, SearchHistory>>,
37    /// One-shot keys for large-output hints (`ctx_shell` bytes, `ctx_read` full tokens).
38    pub large_output_hints_shown: Mutex<HashSet<String>>,
39}
40
41impl Default for AutonomyState {
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47impl AutonomyState {
48    /// Creates a new autonomy state with config loaded from disk.
49    pub fn new() -> Self {
50        Self {
51            session_initialized: AtomicBool::new(false),
52            dedup_applied: AtomicBool::new(false),
53            last_consolidation_unix: AtomicU64::new(0),
54            config: AutonomyConfig::load(),
55            search_repetition: Mutex::new(HashMap::new()),
56            large_output_hints_shown: Mutex::new(HashSet::new()),
57        }
58    }
59
60    /// Returns true if autonomous actions are enabled in configuration.
61    pub fn is_enabled(&self) -> bool {
62        self.config.enabled
63    }
64
65    /// Records a search (`pattern` + `path` key) and returns a progressive hint after repeated calls.
66    ///
67    /// Uses interior mutability so this can be called on `Arc<AutonomyState>`. Counters reset when
68    /// the idle gap since the last call for that key is at least five minutes (50ms in unit tests).
69    pub fn track_search(&self, pattern: &str, path: &str) -> Option<String> {
70        if !autonomy_enabled_effective(self) {
71            return None;
72        }
73        let key = format!("{pattern}|{path}");
74        let now = Instant::now();
75        let mut map = self
76            .search_repetition
77            .lock()
78            .unwrap_or_else(std::sync::PoisonError::into_inner);
79        let hist = map.entry(key).or_insert(SearchHistory {
80            call_count: 0,
81            last_call: now,
82        });
83        if hist.last_call.elapsed() >= SEARCH_REPEAT_IDLE_RESET {
84            hist.call_count = 0;
85        }
86        hist.call_count = hist.call_count.saturating_add(1);
87        hist.last_call = now;
88        let n = hist.call_count;
89
90        match n {
91            1..=3 => None,
92            4..=6 => Some(format!(
93                "[hint: repeated search ({n}/6). Consider ctx_knowledge remember to store findings]"
94            )),
95            _ => Some(format!(
96                "[throttle: search repeated {n} times on same pattern. Use ctx_pack or ctx_knowledge to consolidate]"
97            )),
98        }
99    }
100}
101
102fn profile_autonomy() -> crate::core::profiles::ProfileAutonomy {
103    crate::core::profiles::active_profile().autonomy
104}
105
106fn autonomy_enabled_effective(state: &AutonomyState) -> bool {
107    state.is_enabled() && profile_autonomy().enabled_effective()
108}
109
110fn policy_allows(tool: &str) -> Result<(), (String, String)> {
111    let policy = crate::core::degradation_policy::evaluate_v1_for_tool(tool, None);
112    match policy.decision.verdict {
113        crate::core::degradation_policy::DegradationVerdictV1::Ok
114        | crate::core::degradation_policy::DegradationVerdictV1::Warn => Ok(()),
115        crate::core::degradation_policy::DegradationVerdictV1::Throttle
116        | crate::core::degradation_policy::DegradationVerdictV1::Block => {
117            Err((policy.decision.reason_code, policy.decision.reason))
118        }
119    }
120}
121
122fn record_event(
123    phase: AutonomyPhaseV1,
124    tool: &str,
125    action: Option<&str>,
126    decisions: Vec<AutonomyDriverDecisionV1>,
127) {
128    let mut store = crate::core::autonomy_drivers::AutonomyDriversV1::load();
129    let ev = AutonomyDriverEventV1 {
130        seq: 0,
131        created_at: chrono::Utc::now().to_rfc3339(),
132        phase,
133        role: crate::core::roles::active_role_name(),
134        profile: crate::core::profiles::active_profile_name(),
135        tool: tool.to_string(),
136        action: action.map(std::string::ToString::to_string),
137        decisions,
138    };
139    store.record(ev);
140    let _ = store.save();
141}
142
143/// Auto-preloads project context on the first tool call of a session.
144pub fn session_lifecycle_pre_hook(
145    state: &AutonomyState,
146    tool_name: &str,
147    cache: &mut SessionCache,
148    task: Option<&str>,
149    project_root: Option<&str>,
150    crp_mode: CrpMode,
151) -> Option<String> {
152    if !autonomy_enabled_effective(state) {
153        return None;
154    }
155
156    if tool_name == "ctx_overview" || tool_name == "ctx_preload" {
157        return None;
158    }
159
160    let prof = profile_autonomy();
161    let root = match project_root {
162        Some(r) if !r.is_empty() && r != "." => r.to_string(),
163        _ => return None,
164    };
165
166    if state
167        .session_initialized
168        .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
169        .is_err()
170    {
171        return None;
172    }
173
174    let mut decisions = Vec::new();
175
176    if !state.config.auto_preload || !prof.auto_preload_effective() {
177        decisions.push(AutonomyDriverDecisionV1 {
178            driver: AutonomyDriverKindV1::Preload,
179            verdict: AutonomyVerdictV1::Skip,
180            reason_code: "disabled".to_string(),
181            reason: "auto_preload disabled by config/profile".to_string(),
182            detail: None,
183        });
184        record_event(AutonomyPhaseV1::PreCall, tool_name, None, decisions);
185        return None;
186    }
187
188    let chosen_tool = if task.is_some() {
189        "ctx_preload"
190    } else {
191        "ctx_overview"
192    };
193    if let Err((code, reason)) = policy_allows(chosen_tool) {
194        decisions.push(AutonomyDriverDecisionV1 {
195            driver: AutonomyDriverKindV1::Preload,
196            verdict: AutonomyVerdictV1::Skip,
197            reason_code: code,
198            reason,
199            detail: Some("policy guard (budget/slo)".to_string()),
200        });
201        record_event(AutonomyPhaseV1::PreCall, tool_name, None, decisions);
202        return None;
203    }
204
205    let result = if let Some(task_desc) = task {
206        crate::tools::ctx_preload::handle(cache, task_desc, Some(&root), crp_mode)
207    } else {
208        let cache_readonly = &*cache;
209        crate::tools::ctx_overview::handle(cache_readonly, None, Some(&root), crp_mode)
210    };
211
212    let empty = result.trim().is_empty()
213        || result.contains("No directly relevant files")
214        || result.contains("INDEXING IN PROGRESS");
215    decisions.push(AutonomyDriverDecisionV1 {
216        driver: AutonomyDriverKindV1::Preload,
217        verdict: AutonomyVerdictV1::Run,
218        reason_code: "session_start".to_string(),
219        reason: "first tool call in session".to_string(),
220        detail: Some(format!("tool={chosen_tool} empty={empty}")),
221    });
222    record_event(AutonomyPhaseV1::PreCall, tool_name, None, decisions);
223
224    if empty {
225        return None;
226    }
227
228    Some(format!(
229        "--- AUTO CONTEXT ---\n{result}\n--- END AUTO CONTEXT ---"
230    ))
231}
232
233/// Appends related-file hints and silently preloads imports after a file read.
234pub fn enrich_after_read(
235    state: &AutonomyState,
236    cache: &mut SessionCache,
237    file_path: &str,
238    project_root: Option<&str>,
239    task: Option<&str>,
240    crp_mode: CrpMode,
241    minimal_overhead: bool,
242) -> EnrichResult {
243    let mut result = EnrichResult::default();
244
245    if !autonomy_enabled_effective(state) {
246        return result;
247    }
248
249    let prof = profile_autonomy();
250    let root = match project_root {
251        Some(r) if !r.is_empty() && r != "." => r.to_string(),
252        _ => return result,
253    };
254
255    let index = crate::core::graph_index::load_or_build(&root);
256    if index.files.is_empty() {
257        return result;
258    }
259
260    if state.config.auto_related && prof.auto_related_effective() {
261        result.related_hint = build_related_hints(cache, file_path, &index);
262    }
263
264    if state.config.silent_preload && prof.silent_preload_effective() {
265        silent_preload_imports(cache, file_path, &index, &root);
266    }
267
268    if !minimal_overhead && prof.auto_prefetch_effective() {
269        let mut decisions = Vec::new();
270        if let Err((code, reason)) = policy_allows("ctx_prefetch") {
271            decisions.push(AutonomyDriverDecisionV1 {
272                driver: AutonomyDriverKindV1::Prefetch,
273                verdict: AutonomyVerdictV1::Skip,
274                reason_code: code,
275                reason,
276                detail: Some("policy guard (budget/slo)".to_string()),
277            });
278            record_event(AutonomyPhaseV1::PostRead, "ctx_read", None, decisions);
279        } else {
280            let changed = vec![file_path.to_string()];
281            let out = crate::tools::ctx_prefetch::handle(
282                cache,
283                &root,
284                task,
285                Some(&changed),
286                prof.prefetch_budget_tokens_effective(),
287                Some(prof.prefetch_max_files_effective()),
288                crp_mode,
289            );
290            let summary = out.lines().next().unwrap_or("").trim().to_string();
291            decisions.push(AutonomyDriverDecisionV1 {
292                driver: AutonomyDriverKindV1::Prefetch,
293                verdict: AutonomyVerdictV1::Run,
294                reason_code: "after_read".to_string(),
295                reason: "bounded prefetch after ctx_read".to_string(),
296                detail: if summary.is_empty() {
297                    None
298                } else {
299                    Some(summary.clone())
300                },
301            });
302            record_event(AutonomyPhaseV1::PostRead, "ctx_read", None, decisions);
303            let _ = summary;
304        }
305    }
306
307    result
308}
309
310/// Output from post-read enrichment: optional related-file hints.
311#[derive(Default)]
312pub struct EnrichResult {
313    pub related_hint: Option<String>,
314}
315
316fn build_related_hints(
317    cache: &SessionCache,
318    file_path: &str,
319    index: &ProjectIndex,
320) -> Option<String> {
321    let related: Vec<_> = index
322        .edges
323        .iter()
324        .filter(|e| e.from == file_path || e.to == file_path)
325        .map(|e| if e.from == file_path { &e.to } else { &e.from })
326        .filter(|path| cache.get(path).is_none())
327        .take(3)
328        .collect();
329
330    if related.is_empty() {
331        return None;
332    }
333
334    let hints: Vec<String> = related.iter().map(|p| protocol::shorten_path(p)).collect();
335
336    Some(format!("[related: {}]", hints.join(", ")))
337}
338
339fn silent_preload_imports(
340    cache: &mut SessionCache,
341    file_path: &str,
342    index: &ProjectIndex,
343    project_root: &str,
344) {
345    let imports: Vec<String> = index
346        .edges
347        .iter()
348        .filter(|e| e.from == file_path)
349        .map(|e| e.to.clone())
350        .take(2)
351        .collect();
352
353    let jail_root = std::path::Path::new(project_root);
354    for path in imports {
355        let candidate = std::path::Path::new(&path);
356        let candidate = if candidate.is_absolute() {
357            candidate.to_path_buf()
358        } else {
359            jail_root.join(&path)
360        };
361        let Ok((jailed, warning)) = crate::core::io_boundary::jail_and_check_path(
362            "autonomy:silent_preload",
363            &candidate,
364            jail_root,
365        ) else {
366            continue;
367        };
368        if warning.is_some() {
369            continue;
370        }
371        let jailed_s = jailed.to_string_lossy().to_string();
372        if cache.get(&jailed_s).is_some() {
373            continue;
374        }
375        // Don't hydrate cloud placeholders during automatic import preload (#363).
376        if crate::core::cloud_files::is_cloud_placeholder(&jailed) {
377            continue;
378        }
379
380        if let Ok(content) = std::fs::read_to_string(&jailed) {
381            let tokens = count_tokens(&content);
382            if tokens < 5000 {
383                cache.store(&jailed_s, &content);
384            }
385        }
386    }
387}
388
389/// Runs cache deduplication once the entry count exceeds the configured threshold.
390pub fn maybe_auto_dedup(state: &AutonomyState, cache: &mut SessionCache, trigger_tool: &str) {
391    if !autonomy_enabled_effective(state) {
392        return;
393    }
394
395    let prof = profile_autonomy();
396    if !state.config.auto_dedup || !prof.auto_dedup_effective() {
397        return;
398    }
399
400    if state
401        .dedup_applied
402        .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
403        .is_err()
404    {
405        return;
406    }
407
408    let entries = cache.get_all_entries();
409    let threshold = state
410        .config
411        .dedup_threshold
412        .max(prof.dedup_threshold_effective())
413        .max(1);
414    if entries.len() < threshold {
415        state.dedup_applied.store(false, Ordering::SeqCst);
416        return;
417    }
418
419    let mut decisions = Vec::new();
420    if let Err((code, reason)) = policy_allows("ctx_dedup") {
421        decisions.push(AutonomyDriverDecisionV1 {
422            driver: AutonomyDriverKindV1::Dedup,
423            verdict: AutonomyVerdictV1::Skip,
424            reason_code: code,
425            reason,
426            detail: Some("policy guard (budget/slo)".to_string()),
427        });
428        record_event(AutonomyPhaseV1::PostRead, trigger_tool, None, decisions);
429        state.dedup_applied.store(false, Ordering::SeqCst);
430        return;
431    }
432
433    let out = crate::tools::ctx_dedup::handle_action(cache, "apply");
434    let summary = out.lines().next().unwrap_or("").trim().to_string();
435    decisions.push(AutonomyDriverDecisionV1 {
436        driver: AutonomyDriverKindV1::Dedup,
437        verdict: AutonomyVerdictV1::Run,
438        reason_code: "threshold_reached".to_string(),
439        reason: format!("cache entries >= {threshold}"),
440        detail: if summary.is_empty() {
441            None
442        } else {
443            Some(summary)
444        },
445    });
446    record_event(AutonomyPhaseV1::PostRead, trigger_tool, None, decisions);
447}
448
449/// Returns true if enough tool calls have elapsed to trigger auto-consolidation.
450pub fn should_auto_consolidate(state: &AutonomyState, tool_calls: u32) -> bool {
451    if !state.is_enabled() || !state.config.auto_consolidate {
452        return false;
453    }
454    let every = state.config.consolidate_every_calls.max(1);
455    if !tool_calls.is_multiple_of(every) {
456        return false;
457    }
458
459    let now = SystemTime::now()
460        .duration_since(UNIX_EPOCH)
461        .map_or(0, |d| d.as_secs());
462    let last = state.last_consolidation_unix.load(Ordering::SeqCst);
463    if now.saturating_sub(last) < state.config.consolidate_cooldown_secs {
464        return false;
465    }
466    state.last_consolidation_unix.store(now, Ordering::SeqCst);
467    true
468}
469
470fn take_large_output_hint_once(state: &AutonomyState, key: &str) -> bool {
471    if !autonomy_enabled_effective(state) {
472        return false;
473    }
474    let mut set = state
475        .large_output_hints_shown
476        .lock()
477        .unwrap_or_else(std::sync::PoisonError::into_inner);
478    set.insert(key.to_string())
479}
480
481/// `ctx_shell`: suggest sandbox / read modes when final output is large (bytes).
482pub fn large_ctx_shell_output_hint(
483    state: &AutonomyState,
484    command: &str,
485    output_bytes: usize,
486) -> Option<String> {
487    const THRESHOLD_BYTES: usize = 5000;
488    if output_bytes <= THRESHOLD_BYTES {
489        return None;
490    }
491    if !take_large_output_hint_once(state, "ctx_shell_large_bytes") {
492        return None;
493    }
494    let n = output_bytes;
495    if shell_command_looks_structured(command) {
496        Some(format!(
497            "[hint: large output ({n} bytes). For structured output (e.g. cargo test, npm test, grep), use ctx_execute for automatic compression; for file contents use ctx_read(mode=\"aggressive\")]"
498        ))
499    } else {
500        Some(format!(
501            "[hint: large output ({n} bytes). Consider piping through ctx_execute for automatic compression, or use ctx_read(mode=\"aggressive\") for file contents]"
502        ))
503    }
504}
505
506fn shell_command_looks_structured(cmd: &str) -> bool {
507    let t = cmd.trim();
508    let lower = t.to_lowercase();
509    lower.contains("cargo test")
510        || lower.contains("npm test")
511        || t.starts_with("grep ")
512        || t.starts_with("rg ")
513}
514
515/// `ctx_read` full mode: suggest compressed read modes when output is very large (tokens).
516pub fn large_ctx_read_full_hint(
517    state: &AutonomyState,
518    mode: Option<&str>,
519    output: &str,
520) -> Option<String> {
521    const THRESHOLD_TOKENS: usize = 10_000;
522    let m = mode.unwrap_or("").trim();
523    if m != "full" {
524        return None;
525    }
526    let n = count_tokens(output);
527    if n <= THRESHOLD_TOKENS {
528        return None;
529    }
530    if !take_large_output_hint_once(state, "ctx_read_full_large_tokens") {
531        return None;
532    }
533    Some(format!(
534        "[hint: large file ({n} tokens). Consider mode=\"map\" or mode=\"aggressive\" for compressed view]"
535    ))
536}
537
538/// Suggests a more token-efficient lean-ctx tool when shell compression is low.
539pub fn shell_efficiency_hint(
540    state: &AutonomyState,
541    command: &str,
542    input_tokens: usize,
543    output_tokens: usize,
544) -> Option<String> {
545    if !autonomy_enabled_effective(state) {
546        return None;
547    }
548
549    if input_tokens == 0 {
550        return None;
551    }
552
553    let savings_pct =
554        (input_tokens.saturating_sub(output_tokens) as f64 / input_tokens as f64) * 100.0;
555    if savings_pct >= 20.0 {
556        return None;
557    }
558
559    let cmd_lower = command.to_lowercase();
560    if cmd_lower.starts_with("grep ")
561        || cmd_lower.starts_with("rg ")
562        || cmd_lower.starts_with("find ")
563        || cmd_lower.starts_with("ag ")
564    {
565        return Some("[hint: ctx_search is more token-efficient for code search]".to_string());
566    }
567
568    if cmd_lower.starts_with("cat ") || cmd_lower.starts_with("head ") {
569        return Some("[hint: ctx_read provides cached, compressed file access]".to_string());
570    }
571
572    None
573}
574
575fn looks_like_json(text: &str) -> bool {
576    let t = text.trim();
577    if !(t.starts_with('{') || t.starts_with('[')) {
578        return false;
579    }
580    serde_json::from_str::<serde_json::Value>(t).is_ok()
581}
582
583/// Applies `ctx_response` automatically for large outputs (guarded + bounded).
584/// Never runs on JSON outputs to avoid breaking machine-readable responses.
585pub fn maybe_auto_response(
586    state: &AutonomyState,
587    tool_name: &str,
588    action: Option<&str>,
589    output: &str,
590    crp_mode: CrpMode,
591    minimal_overhead: bool,
592) -> String {
593    if minimal_overhead || !autonomy_enabled_effective(state) {
594        return output.to_string();
595    }
596
597    let prof = profile_autonomy();
598    if !prof.auto_response_effective() {
599        return output.to_string();
600    }
601    if tool_name == "ctx_response" {
602        return output.to_string();
603    }
604
605    let input_tokens = count_tokens(output);
606    if input_tokens < prof.response_min_tokens_effective() {
607        return output.to_string();
608    }
609    if looks_like_json(output) {
610        record_event(
611            AutonomyPhaseV1::PostCall,
612            tool_name,
613            action,
614            vec![AutonomyDriverDecisionV1 {
615                driver: AutonomyDriverKindV1::Response,
616                verdict: AutonomyVerdictV1::Skip,
617                reason_code: "json_output".to_string(),
618                reason: "skip response shaping for JSON outputs".to_string(),
619                detail: None,
620            }],
621        );
622        return output.to_string();
623    }
624
625    if let Err((code, reason)) = policy_allows("ctx_response") {
626        record_event(
627            AutonomyPhaseV1::PostCall,
628            tool_name,
629            action,
630            vec![AutonomyDriverDecisionV1 {
631                driver: AutonomyDriverKindV1::Response,
632                verdict: AutonomyVerdictV1::Skip,
633                reason_code: code,
634                reason,
635                detail: Some("policy guard (budget/slo)".to_string()),
636            }],
637        );
638        return output.to_string();
639    }
640
641    let start = std::time::Instant::now();
642    let compressed = crate::tools::ctx_response::handle(output, crp_mode);
643    let duration = start.elapsed();
644    let output_tokens = count_tokens(&compressed);
645
646    let (verdict, reason_code, reason) = if compressed == output {
647        (
648            AutonomyVerdictV1::Skip,
649            "no_savings".to_string(),
650            "ctx_response made no changes".to_string(),
651        )
652    } else {
653        (
654            AutonomyVerdictV1::Run,
655            "output_large".to_string(),
656            "response shaping applied".to_string(),
657        )
658    };
659
660    record_event(
661        AutonomyPhaseV1::PostCall,
662        tool_name,
663        action,
664        vec![AutonomyDriverDecisionV1 {
665            driver: AutonomyDriverKindV1::Response,
666            verdict,
667            reason_code,
668            reason,
669            detail: Some(format!(
670                "tokens {}→{} in {:.1}ms",
671                input_tokens,
672                output_tokens,
673                duration.as_micros() as f64 / 1000.0
674            )),
675        }],
676    );
677
678    compressed
679}
680
681#[cfg(test)]
682mod tests {
683    use super::*;
684
685    #[test]
686    fn autonomy_state_starts_uninitialized() {
687        let state = AutonomyState::new();
688        assert!(!state.session_initialized.load(Ordering::SeqCst));
689        assert!(!state.dedup_applied.load(Ordering::SeqCst));
690    }
691
692    #[test]
693    fn session_initialized_fires_once() {
694        let state = AutonomyState::new();
695        let first = state.session_initialized.compare_exchange(
696            false,
697            true,
698            Ordering::SeqCst,
699            Ordering::SeqCst,
700        );
701        assert!(first.is_ok());
702        let second = state.session_initialized.compare_exchange(
703            false,
704            true,
705            Ordering::SeqCst,
706            Ordering::SeqCst,
707        );
708        assert!(second.is_err());
709    }
710
711    #[test]
712    fn shell_hint_for_grep() {
713        let state = AutonomyState::new();
714        let hint = shell_efficiency_hint(&state, "grep -rn foo .", 100, 95);
715        assert!(hint.is_some());
716        assert!(hint.unwrap().contains("ctx_search"));
717    }
718
719    #[test]
720    fn shell_hint_none_when_good_savings() {
721        let state = AutonomyState::new();
722        let hint = shell_efficiency_hint(&state, "grep -rn foo .", 100, 50);
723        assert!(hint.is_none());
724    }
725
726    #[test]
727    fn shell_hint_none_for_unknown_command() {
728        let state = AutonomyState::new();
729        let hint = shell_efficiency_hint(&state, "cargo build", 100, 95);
730        assert!(hint.is_none());
731    }
732
733    #[test]
734    fn large_shell_hint_once_per_session() {
735        let state = AutonomyState::new();
736        let h1 = large_ctx_shell_output_hint(&state, "ls -la", 5001).expect("first");
737        assert!(h1.contains("5001 bytes"));
738        assert!(h1.contains("ctx_execute"));
739        assert!(large_ctx_shell_output_hint(&state, "ls -la", 5001).is_none());
740    }
741
742    #[test]
743    fn large_shell_structured_hint_mentions_execute() {
744        let state = AutonomyState::new();
745        let h = large_ctx_shell_output_hint(&state, "cargo test", 6000).expect("hint");
746        assert!(h.contains("structured"));
747        assert!(h.contains("ctx_execute"));
748    }
749
750    #[test]
751    fn large_read_full_hint_respects_mode() {
752        let state = AutonomyState::new();
753        let big = "word ".repeat(20_000);
754        assert!(large_ctx_read_full_hint(&state, Some("map"), &big).is_none());
755        let h = large_ctx_read_full_hint(&state, Some("full"), &big).expect("hint");
756        assert!(h.contains("tokens"));
757        assert!(h.contains("aggressive"));
758        assert!(large_ctx_read_full_hint(&state, Some("full"), &big).is_none());
759    }
760
761    #[test]
762    fn large_hints_disabled_when_autonomy_off() {
763        let mut state = AutonomyState::new();
764        state.config.enabled = false;
765        let big = "word ".repeat(20_000);
766        assert!(large_ctx_shell_output_hint(&state, "cargo test", 6000).is_none());
767        assert!(large_ctx_read_full_hint(&state, Some("full"), &big).is_none());
768    }
769
770    #[test]
771    fn disabled_state_blocks_all() {
772        let mut state = AutonomyState::new();
773        state.config.enabled = false;
774        assert!(!state.is_enabled());
775        let hint = shell_efficiency_hint(&state, "grep foo", 100, 95);
776        assert!(hint.is_none());
777    }
778
779    #[test]
780    fn track_search_none_first_three() {
781        let _lock = crate::core::data_dir::test_env_lock();
782        let state = AutonomyState::new();
783        assert!(state.track_search("foo", "src").is_none());
784        assert!(state.track_search("foo", "src").is_none());
785        assert!(state.track_search("foo", "src").is_none());
786    }
787
788    #[test]
789    fn track_search_hint_band() {
790        let _lock = crate::core::data_dir::test_env_lock();
791        let state = AutonomyState::new();
792        for _ in 0..3 {
793            assert!(state.track_search("bar", ".").is_none());
794        }
795        let h = state.track_search("bar", ".").expect("hint on 4th");
796        assert!(h.starts_with("[hint: repeated search (4/6)."));
797        assert!(h.contains("ctx_knowledge"));
798    }
799
800    #[test]
801    fn track_search_throttle_seventh() {
802        let _lock = crate::core::data_dir::test_env_lock();
803        let state = AutonomyState::new();
804        for _ in 0..6 {
805            let _ = state.track_search("baz", "p");
806        }
807        let h = state.track_search("baz", "p").expect("throttle on 7th");
808        assert!(h.starts_with("[throttle: search repeated 7 times"));
809        assert!(h.contains("ctx_pack"));
810    }
811
812    #[test]
813    fn track_search_resets_after_idle() {
814        let _lock = crate::core::data_dir::test_env_lock();
815        let state = AutonomyState::new();
816        for _ in 0..3 {
817            assert!(state.track_search("idle", "x").is_none());
818        }
819        std::thread::sleep(std::time::Duration::from_millis(600));
820        assert!(
821            state.track_search("idle", "x").is_none(),
822            "count should reset after idle window"
823        );
824    }
825
826    #[test]
827    fn track_search_disabled_no_tracking_messages() {
828        let _lock = crate::core::data_dir::test_env_lock();
829        let mut state = AutonomyState::new();
830        state.config.enabled = false;
831        for _ in 0..8 {
832            assert!(state.track_search("q", "/").is_none());
833        }
834    }
835
836    #[test]
837    fn track_search_distinct_keys() {
838        let _lock = crate::core::data_dir::test_env_lock();
839        let state = AutonomyState::new();
840        assert!(state.track_search("pat", "a").is_none());
841        assert!(state.track_search("pat", "a").is_none());
842        assert!(state.track_search("pat", "a").is_none());
843        assert!(state.track_search("pat", "a").is_some());
844        assert!(state.track_search("pat", "b").is_none());
845    }
846}