lean-ctx 3.9.19

Context Runtime for AI Agents with CCP. 79 MCP tools, 10 read modes, 95+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use crate::core::gotcha_tracker::{self, GotchaStore, learn};

use crate::core::causal_attribution::{self, Outcome, OutcomeSignal};
use serde_json::Value;
use std::collections::HashMap;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum FailurePatternType {
    Loop,
    StuckOnError,
    WrongTool,
    ExcessiveTokens,
    Timeout,
}

#[derive(Debug, Clone, PartialEq)]
pub struct FailurePattern {
    pub pattern_type: FailurePatternType,
    pub frequency: u32,
    pub session_ids: Vec<String>,
    pub suggested_correction: String,
    pub confidence: f32,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LoopDetection {
    pub tool_name: String,
    pub arguments_hash: String,
    pub repetitions: usize,
    pub turn_range: (usize, usize),
    pub wasted_tokens: usize,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Correction {
    pub trigger: String,
    pub action: String,
    pub priority: u8,
}

pub fn detect_loops(session_log: &[Value]) -> Vec<LoopDetection> {
    #[derive(Default)]
    struct Repetition {
        repetitions: usize,
        first_turn: usize,
        last_turn: usize,
        token_total: usize,
    }

    let mut repetitions: HashMap<(String, String), Repetition> = HashMap::new();
    for (turn, entry) in session_log.iter().enumerate() {
        let Some((tool_name, arguments)) = tool_call(entry) else {
            continue;
        };
        let arguments_hash = stable_hash(arguments);
        let repetition = repetitions
            .entry((tool_name.to_owned(), arguments_hash))
            .or_insert_with(|| Repetition {
                first_turn: turn,
                ..Default::default()
            });
        repetition.repetitions += 1;
        repetition.last_turn = turn;
        repetition.token_total += tokens(entry);
    }

    let mut loops: Vec<_> = repetitions
        .into_iter()
        .filter_map(|((tool_name, arguments_hash), repetition)| {
            (repetition.repetitions >= 3).then(|| LoopDetection {
                tool_name,
                arguments_hash,
                repetitions: repetition.repetitions,
                turn_range: (repetition.first_turn, repetition.last_turn),
                wasted_tokens: repetition.token_total,
            })
        })
        .collect();
    loops.sort_by(|left, right| {
        left.turn_range
            .cmp(&right.turn_range)
            .then_with(|| left.tool_name.cmp(&right.tool_name))
    });
    loops
}

pub fn mine_failures(outcomes: &[OutcomeSignal]) -> Vec<FailurePattern> {
    let mut sessions_by_kind: HashMap<FailurePatternType, Vec<String>> = HashMap::new();
    for outcome in outcomes {
        let evidence = outcome.evidence.to_lowercase();
        let kind = if evidence.contains("timeout") {
            Some(FailurePatternType::Timeout)
        } else if evidence.contains("wrong tool") || evidence.contains("tool mismatch") {
            Some(FailurePatternType::WrongTool)
        } else if evidence.contains("token") || evidence.contains("context limit") {
            Some(FailurePatternType::ExcessiveTokens)
        } else if evidence.contains("error") || matches!(outcome.outcome, Outcome::Failure) {
            Some(FailurePatternType::StuckOnError)
        } else {
            None
        };
        if let Some(kind) = kind {
            sessions_by_kind
                .entry(kind)
                .or_default()
                .push(outcome.session_id.clone());
        }
    }

    let causal_sources = causal_attribution::suggest_removals();
    let mut patterns: Vec<_> = sessions_by_kind
        .into_iter()
        .map(|(pattern_type, mut session_ids)| {
            session_ids.sort();
            session_ids.dedup();
            let frequency = session_ids.len() as u32;
            let suggested_correction = correction_for(pattern_type, &causal_sources);
            FailurePattern {
                pattern_type,
                frequency,
                session_ids,
                suggested_correction,
                confidence: (0.55 + frequency as f32 * 0.1).min(0.95),
            }
        })
        .collect();
    sort_failure_patterns(&mut patterns);
    patterns
}

fn sort_failure_patterns(patterns: &mut [FailurePattern]) {
    patterns.sort_by(|left, right| {
        right
            .frequency
            .cmp(&left.frequency)
            .then_with(|| left.pattern_type.cmp(&right.pattern_type))
    });
}

pub fn generate_corrections(patterns: &[FailurePattern]) -> Vec<Correction> {
    patterns
        .iter()
        .map(|pattern| Correction {
            trigger: format!(
                "{:?} observed in {} session(s)",
                pattern.pattern_type, pattern.frequency
            ),
            action: pattern.suggested_correction.clone(),
            priority: (pattern.confidence * 10.0).round() as u8,
        })
        .collect()
}

fn tool_call(value: &Value) -> Option<(&str, &Value)> {
    let object = value.as_object()?;
    let tool_name = object
        .get("tool_name")
        .or_else(|| object.get("tool"))
        .or_else(|| object.get("name"))?
        .as_str()?;
    let arguments = object
        .get("arguments")
        .or_else(|| object.get("args"))
        .or_else(|| object.get("input"))
        .unwrap_or(&Value::Null);
    Some((tool_name, arguments))
}

fn tokens(value: &Value) -> usize {
    value
        .get("tokens")
        .or_else(|| value.get("token_count"))
        .and_then(Value::as_u64)
        .unwrap_or_default() as usize
}

fn stable_hash(value: &Value) -> String {
    let bytes = serde_json::to_vec(value).unwrap_or_default();
    format!("{:016x}", fxhash(&bytes))
}

fn fxhash(bytes: &[u8]) -> u64 {
    bytes.iter().fold(0xcbf2_9ce4_8422_2325, |hash, byte| {
        (hash ^ u64::from(*byte)).wrapping_mul(0x0000_0100_0000_01b3)
    })
}

fn correction_for(pattern_type: FailurePatternType, causal_sources: &[String]) -> String {
    let attribution = causal_sources
        .first()
        .map(|source| format!(" Remove low-value context from {source}."))
        .unwrap_or_default();
    match pattern_type {
        FailurePatternType::Loop => "After repeating a tool call twice, switch strategy; use ctx_compose before another ctx_read.".to_owned(),
        FailurePatternType::StuckOnError => format!("Read the complete error once, then use the targeted diagnostic tool.{attribution}"),
        FailurePatternType::WrongTool => "Choose the tool by intent: ctx_compose for orientation, ctx_search for symbols, ctx_read for exact files.".to_owned(),
        FailurePatternType::ExcessiveTokens => format!("Prefer focused search and compact reads over broad context collection.{attribution}"),
        FailurePatternType::Timeout => "Reduce scope and split the operation into bounded, independently verifiable steps.".to_owned(),
    }
}

pub fn load_outcome_signals() -> Result<Vec<OutcomeSignal>, String> {
    let path = crate::core::causal_attribution::CausalAttributor::default_path()?;
    if !path.exists() {
        return Ok(Vec::new());
    }
    let content = std::fs::read_to_string(&path)
        .map_err(|error| format!("read {}: {error}", path.display()))?;
    Ok(content
        .lines()
        .filter_map(|line| serde_json::from_str::<Value>(line).ok())
        .filter_map(|event| event.get("outcome").cloned())
        .filter_map(|outcome| serde_json::from_value(outcome).ok())
        .collect())
}

pub fn store_corrections(project_root: &str, corrections: &[Correction]) -> Result<(), String> {
    let path = std::path::Path::new(project_root)
        .join(".lean-ctx")
        .join("shared-context.jsonl");
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)
            .map_err(|error| format!("create {}: {error}", parent.display()))?;
    }
    let mut file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&path)
        .map_err(|error| format!("open {}: {error}", path.display()))?;
    for correction in corrections {
        let fact = serde_json::json!({
            "category": "correction",
            "trigger": correction.trigger,
            "action": correction.action,
            "priority": correction.priority,
        });
        use std::io::Write;
        writeln!(file, "{fact}").map_err(|error| format!("write {}: {error}", path.display()))?;
    }
    Ok(())
}

pub(crate) fn cmd_learn(args: &[String]) {
    if args.is_empty() {
        match load_outcome_signals() {
            Ok(outcomes) => {
                let patterns = mine_failures(&outcomes);
                if patterns.is_empty() {
                    println!(
                        "No learnings yet. lean-ctx needs to detect and resolve errors across sessions first."
                    );
                    println!(
                        "Tip: Use lean-ctx normally — errors are automatically tracked and correlated."
                    );
                } else {
                    let corrections = generate_corrections(&patterns);
                    let project_root = std::env::current_dir()
                        .map_err(|error| error.to_string())
                        .and_then(|path| {
                            path.into_os_string()
                                .into_string()
                                .map_err(|_| "non-UTF-8 project path".to_owned())
                        });
                    match project_root.and_then(|root| store_corrections(&root, &corrections)) {
                        Ok(()) => println!(
                            "Stored {} correction(s) as SharedContext facts.",
                            corrections.len()
                        ),
                        Err(error) => eprintln!("Failed to store corrections: {error}"),
                    }
                    for correction in corrections {
                        println!("- {}", correction.action);
                    }
                }
            }
            Err(error) => eprintln!("Failed to load outcome data: {error}"),
        }
        return;
    }
    // Offline mining mode: `lean-ctx learn --mine <dir>` distills recurring
    // error signatures from a directory of .jsonl transcripts/logs.
    if let Some(pos) = args.iter().position(|a| a == "--mine") {
        let dir = args.get(pos + 1).map(String::as_str);
        cmd_learn_mine(dir);
        return;
    }

    let project_root = super::common::detect_project_root(args);
    let apply = args.iter().any(|a| a == "--apply");

    let mut store = GotchaStore::load(&project_root);
    let universal = gotcha_tracker::load_universal_gotchas();
    for ug in universal {
        store.add_universal(ug);
    }

    let learnings = learn::extract_learnings(&store);

    if learnings.is_empty() {
        println!(
            "No learnings yet. lean-ctx needs to detect and resolve errors across sessions first."
        );
        println!("Tip: Use lean-ctx normally — errors are automatically tracked and correlated.");
        return;
    }

    println!("=== Learned Gotchas ({} total) ===\n", learnings.len());
    for l in &learnings {
        println!("  {l}");
    }

    if apply {
        println!();
        match learn::apply_learnings(&project_root, &learnings) {
            Ok(files) if files.is_empty() => {
                println!("No learnings written (need >=2 occurrences with >=50% confidence).");
            }
            Ok(files) => println!(
                "Wrote {} learnings to {}",
                learnings.len(),
                files.join(" + ")
            ),
            Err(e) => eprintln!("Error: {e}"),
        }
    } else {
        println!(
            "\nUse `lean-ctx learn --apply` to write these to AGENTS.md (and CLAUDE.local.md if present)."
        );
    }
}

/// `lean-ctx learn --mine [dir]`: distill recurring error signatures from a
/// directory of `.jsonl` transcripts/logs. With no `dir`, it auto-discovers the
/// agent-transcripts directory (Claude Code / Cursor), so scanning real subagent
/// transcripts is zero-config. Read-only — it surfaces the project's recurring
/// pain points for review, it never mutates stored state.
fn cmd_learn_mine(dir: Option<&str>) {
    let path = if let Some(d) = dir {
        std::path::PathBuf::from(d)
    } else if let Some(p) = gotcha_tracker::mining::default_transcript_dir() {
        println!("Scanning auto-discovered transcripts: {}\n", p.display());
        p
    } else {
        eprintln!(
            "Usage: lean-ctx learn --mine [dir]  (no agent-transcripts dir found to auto-scan)"
        );
        return;
    };
    if !path.is_dir() {
        eprintln!("Error: '{}' is not a directory", path.display());
        return;
    }
    let mined = gotcha_tracker::mining::mine_jsonl_dir(&path);
    println!(
        "{}",
        gotcha_tracker::mining::format_mining_report(&mined, 2)
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn loop_detection_finds_repeated_tool_calls() {
        let log = vec![
            json!({"tool_name":"ctx_read","arguments":{"path":"src/lib.rs"},"tokens":10}),
            json!({"tool_name":"ctx_read","arguments":{"path":"src/lib.rs"},"tokens":20}),
            json!({"tool_name":"ctx_read","arguments":{"path":"src/lib.rs"},"tokens":30}),
        ];
        let loops = detect_loops(&log);
        assert_eq!(loops.len(), 1);
        assert_eq!(loops[0].repetitions, 3);
        assert_eq!(loops[0].wasted_tokens, 60);
    }

    #[test]
    fn failure_mining_identifies_patterns() {
        let outcomes = vec![
            OutcomeSignal {
                session_id: "one".into(),
                outcome: Outcome::Failure,
                evidence: "timeout while reading".into(),
            },
            OutcomeSignal {
                session_id: "two".into(),
                outcome: Outcome::Failure,
                evidence: "tool mismatch".into(),
            },
        ];
        let patterns = mine_failures(&outcomes);
        assert!(
            patterns
                .iter()
                .any(|pattern| pattern.pattern_type == FailurePatternType::Timeout)
        );
        assert!(
            patterns
                .iter()
                .any(|pattern| pattern.pattern_type == FailurePatternType::WrongTool)
        );
    }

    #[test]
    fn correction_generation_is_actionable() {
        let patterns = vec![FailurePattern {
            pattern_type: FailurePatternType::Loop,
            frequency: 3,
            session_ids: vec!["session".into()],
            suggested_correction: "Switch strategy after two repeats.".into(),
            confidence: 0.85,
        }];
        let corrections = generate_corrections(&patterns);
        assert_eq!(corrections[0].priority, 9);
        assert!(corrections[0].action.contains("Switch strategy"));
    }

    #[test]
    fn empty_session_produces_no_errors() {
        assert!(detect_loops(&[]).is_empty());
        assert!(mine_failures(&[]).is_empty());
        assert!(generate_corrections(&[]).is_empty());
    }
}