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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Native-edit code-health notice (#1085).
//!
//! The Phase-4 edit-gate guards lean-ctx's own `ctx_edit`/`ctx_patch`. When an
//! agent edits code with the host's NATIVE Edit/MultiEdit tools the gate is
//! bypassed — this closes that gap. It runs inside the PostToolUse `observe`
//! handler (the only edit-covering hook registered for every host, matcher
//! `.*`) and emits an advisory code-health notice through the model-visible
//! `additionalContext` channel when an edit pushes a function over the
//! navigability threshold.
//!
//! PostToolUse fires AFTER the write, so this is advisory only (it cannot block).
//! The pre-image is reconstructed by reversing the payload's `old_string`→
//! `new_string` diff on the post-edit file — no subprocess (hooks must never
//! spawn children) and no git dependency. Write/create tools are skipped: at
//! PostToolUse their pre-image is gone, so there is no reliable per-edit delta
//! (the background index + session-start block cover those instead).

use super::payload;
use crate::core::code_health::GateMode;
use crate::core::code_health::gate::{self, GateOutcome};
use crate::core::config::Config;
use serde_json::Value;

/// Largest post-edit file we will parse inside a hook. Above this the tree-sitter
/// parse isn't worth the hook's latency budget; the background index covers it.
const MAX_EDIT_BYTES: usize = 1_000_000;

/// A single textual replacement from an edit payload.
struct Replacement {
    old: String,
    new: String,
    replace_all: bool,
}

/// Parse, evaluate, and print a PostToolUse code-health notice for a native edit.
/// No-op for non-edit events, non-source files, or sub-threshold edits.
///
/// #778: By default, notices route to `ctx_knowledge` + dashboard (non-destructive)
/// instead of `additionalContext` stdout (which causes prompt-cache invalidation).
/// Set `[code_health] inject_context = true` to opt into the old stdout behavior.
pub(super) fn maybe_emit(input: &str) {
    let Ok(v) = serde_json::from_str::<Value>(input) else {
        return;
    };
    let root = resolve_root(&v);

    // Guard: detect and auto-restore files corrupted by compression markers.
    // Shadow-mode can compress content that StrReplace/Write then writes back.
    check_and_restore_corrupted(&v, &root);

    if let Some(notice) = edit_health_notice(&v, &root) {
        // Always persist to non-destructive channels (#778)
        persist_notice_to_knowledge(&notice, &v, &root);

        // Only inject additionalContext when explicitly opted in (cache-destructive)
        if inject_context_enabled() {
            emit_post_tool_use_context(&notice);
        }
    }
}

/// Check if `additionalContext` injection is enabled (opt-in, default off).
/// Env `LEAN_CTX_INJECT_CONTEXT=1` force-enables for debugging.
fn inject_context_enabled() -> bool {
    std::env::var("LEAN_CTX_INJECT_CONTEXT").is_ok() || Config::load().code_health.inject_context
}

/// Persist a health notice to `ctx_knowledge` so the agent sees it on the next
/// `ctx_compose` call, and emit a ContextBus event so the dashboard shows the
/// alert in real-time. Fire-and-forget: never blocks the hook, never panics.
fn persist_notice_to_knowledge(notice: &str, payload: &Value, root: &str) {
    // Skip noise paths (IDE caches, Library bundles, terminal artifacts) — these
    // produce meaningless code_health facts from bundled vendor JS.
    let file_check = super::payload::resolve_path_field(
        super::payload::resolve_tool_args(payload).as_ref(),
        super::payload::READ_PATH_FIELDS,
    )
    .map(|(_, p)| p)
    .unwrap_or_default();
    if crate::core::auto_findings::is_noise_path(&file_check) {
        return;
    }
    let session_id = payload
        .get("session_id")
        .and_then(|s| s.as_str())
        .unwrap_or("unknown");

    let file_path = super::payload::resolve_path_field(
        super::payload::resolve_tool_args(payload).as_ref(),
        super::payload::READ_PATH_FIELDS,
    )
    .map(|(_, p)| p)
    .unwrap_or_default();

    let key = if file_path.is_empty() {
        "edit_regression".to_string()
    } else {
        format!("edit_regression:{file_path}")
    };

    let policy = crate::core::memory_policy::MemoryPolicy::default();
    let _ = crate::core::knowledge::ProjectKnowledge::mutate_locked(root, |pk| {
        pk.remember("code_health", &key, notice, session_id, 0.9, &policy);
    });

    // Emit ContextBus event so dashboard + subscribers see it in real-time
    crate::core::context_os::emit_event(
        root,
        "code_health",
        &crate::core::context_os::ContextEventKindV1::KnowledgeRemembered,
        Some("edit_health_hook"),
        serde_json::json!({
            "category": "code_health",
            "key": key,
            "notice": notice,
            "file": file_path,
        }),
    );
}

/// Project root for path resolution: the payload `cwd` (every Claude/Cursor hook
/// carries it), falling back to the process working directory.
fn resolve_root(v: &Value) -> String {
    v.get("cwd")
        .and_then(Value::as_str)
        .filter(|s| !s.is_empty())
        .map(String::from)
        .or_else(|| {
            std::env::current_dir()
                .ok()
                .map(|p| p.to_string_lossy().into_owned())
        })
        .unwrap_or_default()
}

/// The advisory notice for a native edit, or `None` when not applicable. Loads
/// the `[code_health]` config for mode + threshold.
pub(super) fn edit_health_notice(v: &Value, root: &str) -> Option<String> {
    let cfg = Config::load();
    notice_with(
        v,
        root,
        GateMode::parse(&cfg.code_health.gate),
        cfg.code_health.cognitive_threshold,
    )
}

/// Pure-by-injection core: explicit `mode`/`threshold` so it is unit-testable
/// without touching the on-disk config.
fn notice_with(v: &Value, root: &str, mode: GateMode, threshold: u32) -> Option<String> {
    if matches!(mode, GateMode::Off) {
        return None;
    }

    let tool = payload::resolve_tool_name(v)?;
    // ctx_edit / ctx_patch already run the in-tool gate; never double-notice.
    if tool.starts_with("ctx_") || tool.starts_with("mcp__lean-ctx__") {
        return None;
    }

    let args = payload::resolve_tool_args(v)?;
    let (_field, file) = payload::resolve_path_field(Some(&args), payload::READ_PATH_FIELDS)?;
    let edits = collect_edits(&args)?;

    let after = read_jailed(&file, root)?;
    if after.len() > MAX_EDIT_BYTES {
        return None;
    }
    let before = reverse_edits(&after, &edits)?;
    if before == after {
        return None;
    }

    let ext = std::path::Path::new(&file)
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("");

    match gate::evaluate_with(&before, &after, ext, mode, threshold) {
        GateOutcome::Allow(Some(notice)) | GateOutcome::Block(notice) => Some(notice),
        GateOutcome::Allow(None) => None,
    }
}

/// Extract the edit replacements from a tool-args object. Handles the single-edit
/// shape (`old_string` + `new_string`) and the MultiEdit shape (`edits: [...]`).
/// `None` when the payload carries no recognizable edit (e.g. a Write/create).
fn collect_edits(args: &Value) -> Option<Vec<Replacement>> {
    if let Some(arr) = args.get("edits").and_then(Value::as_array) {
        let edits: Vec<Replacement> = arr.iter().filter_map(replacement_from).collect();
        return (!edits.is_empty()).then_some(edits);
    }
    replacement_from(args).map(|r| vec![r])
}

fn replacement_from(obj: &Value) -> Option<Replacement> {
    let old = obj.get("old_string").and_then(Value::as_str)?.to_string();
    let new = obj.get("new_string").and_then(Value::as_str)?.to_string();
    let replace_all = obj
        .get("replace_all")
        .and_then(Value::as_bool)
        .unwrap_or(false);
    Some(Replacement {
        old,
        new,
        replace_all,
    })
}

/// Reconstruct the pre-edit content by reversing each replacement (new→old) on
/// `after`, in reverse application order. Returns `None` when a replacement
/// cannot be reversed reliably — a deletion (`new_string` empty) whose position
/// is unknown, or a `new_string` no longer present (the host normalized it) — so
/// an unreliable delta never produces a false notice.
fn reverse_edits(after: &str, edits: &[Replacement]) -> Option<String> {
    let mut content = after.to_string();
    for e in edits.iter().rev() {
        if e.new.is_empty() {
            return None; // pure deletion: original position is unrecoverable.
        }
        if !content.contains(&e.new) {
            return None; // can't locate the inserted text → bail, don't guess.
        }
        content = if e.replace_all {
            content.replace(&e.new, &e.old)
        } else {
            content.replacen(&e.new, &e.old, 1)
        };
    }
    Some(content)
}

/// Read `file` (resolved against `root`) only if it stays inside the project
/// jail. Returns `None` on any path/IO/jail failure (best-effort hook).
fn read_jailed(file: &str, root: &str) -> Option<String> {
    let p = std::path::Path::new(file);
    let abs = if p.is_absolute() {
        p.to_path_buf()
    } else {
        std::path::Path::new(root).join(file)
    };
    crate::core::pathjail::jail_path(&abs, std::path::Path::new(root)).ok()?;
    std::fs::read_to_string(&abs).ok()
}

/// Emit a PostToolUse notice on the model-visible `additionalContext` channel
/// (honored by Claude Code / Codex; ignored harmlessly by other hosts).
fn emit_post_tool_use_context(notice: &str) {
    let payload = serde_json::json!({
        "hookSpecificOutput": {
            "hookEventName": "PostToolUse",
            "additionalContext": notice,
        }
    });
    println!("{payload}");
}

/// Compression markers that indicate a file was corrupted by shadow-mode
/// content being written back to disk by an edit tool.
const CORRUPTION_MARKERS: &[&str] = &[
    "§ function",
    "§ block",
    "§ impl",
    "§ struct",
    "§ enum",
    "§ trait",
    "§ mod",
    "[lean-ctx: omitted",
    "… truncated at ~",
    "use ctx_read with lines= parameter to see specific sections]",
];

/// After a native edit, check if the written file contains lean-ctx compression
/// markers. If so, restore the file from git HEAD and log a warning.
/// This prevents accidental corruption when shadow-mode compressed content
/// leaks through edit tools (StrReplace, Write).
fn check_and_restore_corrupted(v: &Value, root: &str) {
    let Some(args) = payload::resolve_tool_args(v) else {
        return;
    };
    let Some((_field, file)) = payload::resolve_path_field(Some(&args), payload::READ_PATH_FIELDS)
    else {
        return;
    };
    let Some(contents) = read_jailed(&file, root) else {
        return;
    };

    let has_marker = CORRUPTION_MARKERS.iter().any(|m| contents.contains(m));
    if !has_marker {
        return;
    }

    eprintln!(
        "[lean-ctx] CORRUPTION DETECTED in {file}: compression markers found after edit. Restoring from git HEAD."
    );

    let abs_path = if std::path::Path::new(&file).is_absolute() {
        file.clone()
    } else {
        format!("{root}/{file}")
    };

    let restore = std::process::Command::new("git")
        .args(["checkout", "HEAD", "--", &abs_path])
        .current_dir(root)
        .output();

    match restore {
        Ok(out) if out.status.success() => {
            eprintln!("[lean-ctx] Restored {file} from git HEAD.");
        }
        Ok(out) => {
            let stderr = String::from_utf8_lossy(&out.stderr);
            eprintln!("[lean-ctx] CRITICAL: could not restore {file}: {stderr}");
        }
        Err(e) => {
            eprintln!("[lean-ctx] CRITICAL: git restore failed for {file}: {e}");
        }
    }
}

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

    const FLAT: &str = "fn f(a: bool) { if a {} }";
    // 1+2+3+4+5+6 = 21 cognitive → over the default threshold of 15.
    const DEEP: &str = "fn f(a: bool) { if a { if a { if a { if a { if a { if a {} } } } } } }";

    #[test]
    fn reverse_single_edit_reconstructs_before() {
        let after = format!("{DEEP}\n");
        let edits = vec![Replacement {
            old: FLAT.into(),
            new: DEEP.into(),
            replace_all: false,
        }];
        assert_eq!(reverse_edits(&after, &edits).unwrap(), format!("{FLAT}\n"));
    }

    #[test]
    fn reverse_insertion_removes_new_text() {
        // old empty (pure insertion): reversing removes the inserted text.
        let edits = vec![Replacement {
            old: String::new(),
            new: "fn extra() {}\n".into(),
            replace_all: false,
        }];
        let after = "fn extra() {}\nfn keep() {}\n";
        assert_eq!(reverse_edits(after, &edits).unwrap(), "fn keep() {}\n");
    }

    #[test]
    fn reverse_deletion_bails() {
        // new empty (deletion): original position is unrecoverable → None.
        let edits = vec![Replacement {
            old: "fn gone() {}\n".into(),
            new: String::new(),
            replace_all: false,
        }];
        assert!(reverse_edits("fn keep() {}\n", &edits).is_none());
    }

    #[test]
    fn reverse_missing_new_text_bails() {
        let edits = vec![Replacement {
            old: "a".into(),
            new: "NOT_PRESENT".into(),
            replace_all: false,
        }];
        assert!(reverse_edits("some other content", &edits).is_none());
    }

    #[test]
    fn collect_edits_single_and_multi() {
        let single = json!({ "old_string": "a", "new_string": "b" });
        assert_eq!(collect_edits(&single).unwrap().len(), 1);

        let multi = json!({ "edits": [
            { "old_string": "a", "new_string": "b" },
            { "old_string": "c", "new_string": "d", "replace_all": true },
        ]});
        let edits = collect_edits(&multi).unwrap();
        assert_eq!(edits.len(), 2);
        assert!(edits[1].replace_all);

        // A Write payload (content only, no old/new) is not an edit.
        let write = json!({ "content": "whole file" });
        assert!(collect_edits(&write).is_none());
    }

    #[test]
    fn notice_for_native_edit_that_regresses() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path().to_str().unwrap();
        std::fs::write(dir.path().join("f.rs"), format!("{DEEP}\n")).unwrap();

        let v = json!({
            "tool_name": "Edit",
            "cwd": root,
            "tool_input": {
                "file_path": "f.rs",
                "old_string": FLAT,
                "new_string": DEEP,
            }
        });
        let notice = notice_with(&v, root, GateMode::Warn, 15).expect("notice");
        assert!(notice.contains("[CODE HEALTH]"));
    }

    #[test]
    fn no_notice_for_ctx_edit_tool() {
        // ctx_edit goes through the in-tool gate; the hook must not double-notice.
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path().to_str().unwrap();
        std::fs::write(dir.path().join("f.rs"), format!("{DEEP}\n")).unwrap();
        let v = json!({
            "tool_name": "ctx_edit",
            "cwd": root,
            "tool_input": { "file_path": "f.rs", "old_string": FLAT, "new_string": DEEP }
        });
        assert!(notice_with(&v, root, GateMode::Warn, 15).is_none());
    }

    #[test]
    fn no_notice_in_off_mode() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path().to_str().unwrap();
        std::fs::write(dir.path().join("f.rs"), format!("{DEEP}\n")).unwrap();
        let v = json!({
            "tool_name": "Edit",
            "cwd": root,
            "tool_input": { "file_path": "f.rs", "old_string": FLAT, "new_string": DEEP }
        });
        assert!(notice_with(&v, root, GateMode::Off, 15).is_none());
    }

    #[test]
    fn no_notice_for_non_edit_event() {
        let v = json!({ "tool_name": "Read", "tool_input": { "file_path": "f.rs" } });
        assert!(notice_with(&v, "/tmp", GateMode::Warn, 15).is_none());
    }

    #[test]
    fn inject_context_disabled_by_default() {
        // #778: with default config, inject_context is false → no stdout emission
        let _lock = crate::core::data_dir::test_env_lock();
        crate::test_env::remove_var("LEAN_CTX_INJECT_CONTEXT");
        assert!(!inject_context_enabled());
    }

    #[test]
    fn inject_context_enabled_via_env() {
        // #778: env override forces injection (for debugging/opt-in)
        let _lock = crate::core::data_dir::test_env_lock();
        crate::test_env::set_var("LEAN_CTX_INJECT_CONTEXT", "1");
        assert!(inject_context_enabled());
        crate::test_env::remove_var("LEAN_CTX_INJECT_CONTEXT");
    }
}