lean-ctx 3.12.0

LeanCTX Engine: the open-source runtime for the Context Gateway for AI Systems. CLI, MCP server, and supported proxy integrations.
Documentation
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! Solution Intelligence auto-capture for native edit operations.
//!
//! Hooks into the PostToolUse `observe` pipeline to record solution decisions
//! and LOC metrics for EVERY edit — not just `ctx_edit`/`ctx_patch` MCP calls.
//! This ensures Solution Intelligence works across all IDEs (Cursor, Claude Code,
//! Codex, Copilot, Windsurf, JetBrains) that emit PostToolUse events.

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::path::Path;
use std::process::Command;
use std::sync::Mutex;

use serde_json::Value;
use sha2::{Digest, Sha256};

use super::payload;

/// Number of recent edit hashes to remember for de-duplication.
const DEDUP_CAPACITY: usize = 32;

/// Edit tool names that carry `old_string` + `new_string` replacements.
const EDIT_TOOLS: &[&str] = &[
    "str_replace_editor",
    "edit_file",
    "StrReplace",
    "MultiEdit",
    "str_replace",
    "replace_in_file",
];

/// Tools that create/overwrite files (LOC-only, no old_string available).
const WRITE_TOOLS: &[&str] = &["Write", "write_to_file", "create_file", "write_file"];

static RECENT_EDITS: Mutex<RecentEdits> = Mutex::new(RecentEdits::new());

struct RecentEdits {
    hashes: [u64; DEDUP_CAPACITY],
    cursor: usize,
    count: usize,
}

impl RecentEdits {
    const fn new() -> Self {
        Self {
            hashes: [0; DEDUP_CAPACITY],
            cursor: 0,
            count: 0,
        }
    }

    fn contains(&self, hash: u64) -> bool {
        let len = self.count.min(DEDUP_CAPACITY);
        self.hashes[..len].contains(&hash)
    }

    fn insert(&mut self, hash: u64) {
        self.hashes[self.cursor] = hash;
        self.cursor = (self.cursor + 1) % DEDUP_CAPACITY;
        if self.count < DEDUP_CAPACITY {
            self.count += 1;
        }
    }
}

/// Entry point called from `observe.rs` after `edit_health::maybe_emit`.
/// Detects native edit tool payloads and triggers solution capture + LOC metering.
pub fn maybe_capture(input: &str) {
    let admission = super::admission::admit_payload(input);
    maybe_capture_admitted(input, admission.derivable(input));
}

/// [`maybe_capture`] with the payload's admission already decided.
///
/// `input` is the original payload and feeds only content-free facts: LOC
/// metering, edit de-duplication and provenance hashes. `admitted` is the
/// payload text-bearing derivations may read — solution decisions and the
/// findings built from them see nothing else. `None` (the original was
/// withheld) derives no decision at all.
pub(super) fn maybe_capture_admitted(input: &str, admitted: Option<&str>) {
    let Ok(v) = serde_json::from_str::<Value>(input) else {
        return;
    };
    let admitted_args = admitted
        .and_then(|payload| serde_json::from_str::<Value>(payload).ok())
        .and_then(|payload| payload::resolve_tool_args(&payload));

    let Some(tool) = payload::resolve_tool_name(&v) else {
        return;
    };

    // Skip lean-ctx MCP tools — they have their own capture path.
    if tool.starts_with("ctx_") || tool.starts_with("mcp__lean-ctx__") {
        return;
    }

    let Some(args) = payload::resolve_tool_args(&v) else {
        return;
    };

    let root = resolve_root(&v);
    if root.is_empty() {
        return;
    }

    // Codex edits through `apply_patch`; its hook payload carries the raw
    // patch text (`tool_input.command`, a string or `["apply_patch", patch]`).
    // Report every file the patch touches to the edit beacon.
    if is_apply_patch_tool(&tool) {
        let mut texts = Vec::new();
        collect_strings(&args, &mut texts);
        for text in texts {
            crate::core::bounce_tracker::note_host_patch_edits(text, &root);
        }
        return;
    }

    let is_edit = is_edit_tool(&tool);
    if (is_edit || is_write_tool(&tool))
        && let Some((_, path)) = payload::resolve_path_field(Some(&args), payload::READ_PATH_FIELDS)
    {
        // The MCP server's bounce tracker never sees native edits: report the
        // edit so a provisional compressed→full bounce of this file is
        // cancelled as read-before-edit (see `bounce_tracker` edit beacon).
        let absolute = std::path::Path::new(&root).join(&path);
        crate::core::bounce_tracker::note_host_edit(&absolute.to_string_lossy());
    }

    let provenance_config = crate::core::config::Config::load().provenance;
    let capture_provenance = provenance_config.enabled && provenance_config.capture_native_edits;

    let call = NativeEdit {
        event: &v,
        args: &args,
        admitted_args: admitted_args.as_ref(),
        tool: &tool,
        root: &root,
        capture_provenance,
    };
    if is_edit {
        handle_edit_tool(&call);
    } else if is_write_tool(&tool) {
        handle_write_tool(&call);
    }
}

/// One native edit or write call as the hook saw it.
struct NativeEdit<'a> {
    event: &'a Value,
    /// Original tool arguments: content-free metrics only.
    args: &'a Value,
    /// Admitted tool arguments: the only input of text-bearing derivations.
    admitted_args: Option<&'a Value>,
    tool: &'a str,
    root: &'a str,
    capture_provenance: bool,
}

/// Derive solution decisions from the ADMITTED form of one edit. A withheld
/// payload has none; a redacted payload yields decisions from redacted text.
fn capture_admitted_decisions(call: &NativeEdit<'_>, old: &str, new: &str) {
    let Some(args) = call.admitted_args else {
        return;
    };
    if let Some((_, path)) = payload::resolve_path_field(Some(args), payload::READ_PATH_FIELDS) {
        crate::core::solution_auto_capture::capture_edit_decisions(call.root, &path, old, new);
    }
}

/// Records a checkpoint for the commit that just completed.
///
/// Intended for a Git `post-commit` hook invoking `lean-ctx hook post-commit`.
/// The tracker resolves and links the latest session for this project before it
/// persists the checkpoint record.
pub fn handle_post_commit() {
    let config = crate::core::config::Config::load();
    if !config.provenance.enabled || !config.provenance.checkpoint_on_commit {
        return;
    }

    let Ok(root) = std::env::current_dir() else {
        return;
    };
    let Ok(output) = Command::new("git")
        .args(["rev-parse", "HEAD"])
        .current_dir(&root)
        .output()
    else {
        return;
    };
    if !output.status.success() {
        return;
    }
    let commit_sha = String::from_utf8_lossy(&output.stdout).trim().to_owned();
    if commit_sha.is_empty() {
        return;
    }

    let root = root.to_string_lossy().into_owned();
    let session_id = crate::core::session::SessionState::load_latest_for_project_root(&root)
        .map(|session| session.id)
        .unwrap_or_else(|| "git-hook".to_owned());
    let _ = crate::core::provenance::ProvenanceTracker::new(&root)
        .and_then(|tracker| tracker.observe_commit(&commit_sha, &session_id));
}

fn is_edit_tool(tool: &str) -> bool {
    EDIT_TOOLS.iter().any(|&t| tool.contains(t))
}

fn is_write_tool(tool: &str) -> bool {
    WRITE_TOOLS.iter().any(|&t| tool.contains(t))
}

fn is_apply_patch_tool(tool: &str) -> bool {
    matches!(tool, "apply_patch" | "ApplyPatch")
}

/// Every string inside `value` (patch text may be nested in an array).
fn collect_strings<'a>(value: &'a Value, out: &mut Vec<&'a str>) {
    match value {
        Value::String(s) => out.push(s),
        Value::Array(items) => items.iter().for_each(|v| collect_strings(v, out)),
        Value::Object(map) => map.values().for_each(|v| collect_strings(v, out)),
        _ => {}
    }
}

/// Handle StrReplace-style edits that carry old_string + new_string.
fn handle_edit_tool(call: &NativeEdit<'_>) {
    let (v, args, tool, root) = (call.event, call.args, call.tool, call.root);
    let capture_provenance = call.capture_provenance;
    let Some((_field, path)) = payload::resolve_path_field(Some(args), payload::READ_PATH_FIELDS)
    else {
        return;
    };

    // Collect all edits (single or MultiEdit array).
    let edits = collect_edits(args);
    if edits.is_empty() {
        return;
    }
    // Same structure as `edits`: admission rewrites strings, never the shape.
    let admitted_edits = call.admitted_args.map(collect_edits).unwrap_or_default();

    for (index, edit) in edits.iter().enumerate() {
        let hash = compute_edit_hash(&path, &edit.old, &edit.new);

        let mut dedup = RECENT_EDITS
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if dedup.contains(hash) {
            continue;
        }
        dedup.insert(hash);
        drop(dedup);

        // Solution decision heuristics: text-bearing, admitted form only.
        if let Some(admitted) = admitted_edits.get(index) {
            capture_admitted_decisions(call, &admitted.old, &admitted.new);
        }

        // LOC metering
        let old_lines = edit.old.lines().count() as u64;
        let new_lines = edit.new.lines().count() as u64;
        let added = new_lines.saturating_sub(old_lines);
        let removed = old_lines.saturating_sub(new_lines);
        if added > 0 || removed > 0 {
            crate::core::edit_metering::record_loc_change(Some(&path), added, removed);
        }

        if capture_provenance {
            observe_native_edit(
                v, root, &path, tool, &edit.old, &edit.new, new_lines, old_lines,
            );
        }
    }

    // Also count via toolResult if available (for Copilot camelCase shape)
    if let Some(result) = v.get("toolResult").and_then(|r| r.get("textResultForLlm")) {
        let _ = result; // Already captured via edits above
    }
}

/// The file body a Write/create tool carries, under any of its field names.
fn written_content(args: &Value) -> Option<&str> {
    args.get("contents")
        .or_else(|| args.get("content"))
        .or_else(|| args.get("file_text"))
        .and_then(Value::as_str)
}

/// Handle Write/create tools — no old_string, so LOC-only from new_content.
fn handle_write_tool(call: &NativeEdit<'_>) {
    let (v, args, tool, root) = (call.event, call.args, call.tool, call.root);
    let capture_provenance = call.capture_provenance;
    let content = written_content(args).unwrap_or("");

    if content.is_empty() {
        return;
    }

    let lines = content.lines().count() as u64;
    if lines > 0 {
        crate::core::edit_metering::record_loc_change(None, lines, 0);
        if let Some(admitted) = call.admitted_args.and_then(written_content) {
            capture_admitted_decisions(call, "", admitted);
        }
        if let Some((_, file_path)) =
            payload::resolve_path_field(Some(args), payload::READ_PATH_FIELDS)
        {
            if capture_provenance {
                crate::core::savings_ledger::record_edit_event(&file_path, lines, 0);
                observe_native_edit(v, root, &file_path, tool, "", content, lines, 0);
            }
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn observe_native_edit(
    event: &Value,
    root: &str,
    path: &str,
    tool: &str,
    before: &str,
    after: &str,
    lines_added: u64,
    lines_removed: u64,
) {
    let tracked_path = Path::new(path)
        .strip_prefix(root)
        .ok()
        .filter(|relative| !relative.as_os_str().is_empty())
        .map(|relative| relative.to_string_lossy().into_owned())
        .unwrap_or_else(|| path.to_owned());
    let session_id = event
        .get("session_id")
        .and_then(Value::as_str)
        .unwrap_or("native-hook");
    let agent_id = event
        .get("agent_id")
        .or_else(|| event.get("client_name"))
        .and_then(Value::as_str)
        .unwrap_or("native");

    let _ = crate::core::provenance::ProvenanceTracker::new(root).and_then(|tracker| {
        tracker.observe_edit(
            tracked_path,
            tool,
            sha256_hex(before),
            sha256_hex(after),
            lines_added,
            lines_removed,
            session_id,
            agent_id,
        )
    });
}

fn sha256_hex(content: &str) -> String {
    let hash = Sha256::digest(content.as_bytes());
    let mut hex = String::with_capacity(64);
    for b in &hash {
        use std::fmt::Write;
        let _ = write!(hex, "{b:02x}");
    }
    hex
}

struct EditReplacement {
    old: String,
    new: String,
}

/// Extract edit replacements from tool args (single or MultiEdit array).
fn collect_edits(args: &Value) -> Vec<EditReplacement> {
    // MultiEdit: `edits` array
    if let Some(arr) = args.get("edits").and_then(Value::as_array) {
        return arr.iter().filter_map(replacement_from).collect();
    }
    // Single replacement
    replacement_from(args).into_iter().collect()
}

fn replacement_from(obj: &Value) -> Option<EditReplacement> {
    let old = obj
        .get("old_string")
        .or_else(|| obj.get("old_str"))
        .and_then(Value::as_str)?;
    let new = obj
        .get("new_string")
        .or_else(|| obj.get("new_str"))
        .or_else(|| obj.get("code_edit"))
        .and_then(Value::as_str)
        .unwrap_or("");
    Some(EditReplacement {
        old: old.to_string(),
        new: new.to_string(),
    })
}

fn compute_edit_hash(path: &str, old: &str, new: &str) -> u64 {
    let mut hasher = DefaultHasher::new();
    path.hash(&mut hasher);
    old.hash(&mut hasher);
    new.hash(&mut hasher);
    hasher.finish()
}

/// Escape raw control characters (\n, \r, \t) inside JSON string values.
/// IDE hooks pipe payloads with literal newlines in `old_string`/`new_string`
/// fields, which `serde_json` rejects as invalid JSON (RFC 8259 §7).
pub(crate) fn sanitize_json_control_chars(input: &str) -> String {
    let mut out = String::with_capacity(input.len() + 32);
    let mut in_string = false;
    let mut prev_backslash = false;
    for ch in input.chars() {
        if in_string {
            if prev_backslash {
                prev_backslash = false;
                out.push(ch);
                continue;
            }
            match ch {
                '\\' => {
                    prev_backslash = true;
                    out.push(ch);
                }
                '"' => {
                    in_string = false;
                    out.push(ch);
                }
                '\n' => out.push_str("\\n"),
                '\r' => out.push_str("\\r"),
                '\t' => out.push_str("\\t"),
                c if c.is_control() => {
                    let _ =
                        std::fmt::Write::write_fmt(&mut out, format_args!("\\u{:04x}", c as u32));
                }
                _ => out.push(ch),
            }
        } else {
            if ch == '"' && !prev_backslash {
                in_string = true;
            }
            prev_backslash = ch == '\\';
            out.push(ch);
        }
    }
    out
}

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()
}

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

    #[test]
    fn skips_ctx_tools() {
        let input = json!({
            "tool_name": "ctx_edit",
            "tool_input": { "path": "foo.rs", "old_string": "a", "new_string": "b" },
            "cwd": "/tmp/proj"
        });
        // Should not panic or capture — just returns early
        maybe_capture(&input.to_string());
    }

    #[test]
    fn skips_mcp_lean_ctx_tools() {
        let input = json!({
            "tool_name": "mcp__lean-ctx__ctx_edit",
            "tool_input": { "path": "foo.rs", "old_string": "a", "new_string": "b" },
            "cwd": "/tmp/proj"
        });
        maybe_capture(&input.to_string());
    }

    #[test]
    fn captures_cursor_str_replace() {
        let input = json!({
            "tool_name": "str_replace_editor",
            "tool_input": {
                "path": "/tmp/proj/src/main.rs",
                "old_string": "use serde;",
                "new_string": "use std::collections::HashMap;"
            },
            "cwd": "/tmp/proj"
        });
        maybe_capture(&input.to_string());
    }

    #[test]
    fn captures_claude_edit_file() {
        let input = json!({
            "tool_name": "edit_file",
            "tool_input": {
                "file_path": "/tmp/proj/lib.rs",
                "old_string": "fn old() {}",
                "new_string": "fn new() {}"
            },
            "cwd": "/tmp/proj"
        });
        maybe_capture(&input.to_string());
    }

    #[test]
    fn captures_codex_str_replace() {
        let input = json!({
            "toolName": "str_replace",
            "toolArgs": {
                "path": "/tmp/proj/mod.rs",
                "old_string": "let x = 1;",
                "new_string": "let x = 2;"
            },
            "cwd": "/tmp/proj"
        });
        maybe_capture(&input.to_string());
    }

    #[test]
    fn captures_write_tool_loc_only() {
        let input = json!({
            "tool_name": "Write",
            "tool_input": {
                "path": "/tmp/proj/new.rs",
                "contents": "fn main() {\n    println!(\"hello\");\n}\n"
            },
            "cwd": "/tmp/proj"
        });
        maybe_capture(&input.to_string());
    }

    #[test]
    fn dedup_prevents_double_count() {
        let input = json!({
            "tool_name": "StrReplace",
            "tool_input": {
                "path": "/tmp/proj/dedup_test.rs",
                "old_string": "UNIQUE_DEDUP_TEST_STRING_XYZ",
                "new_string": "UNIQUE_DEDUP_TEST_REPLACEMENT_XYZ"
            },
            "cwd": "/tmp/proj"
        });
        let s = input.to_string();
        maybe_capture(&s);
        maybe_capture(&s);
        // Second call is a no-op due to dedup — no assertion needed, just no panic
    }

    #[test]
    fn no_crash_on_missing_fields() {
        let inputs = [
            json!({}),
            json!({"tool_name": "StrReplace"}),
            json!({"tool_name": "StrReplace", "tool_input": {}}),
            json!({"tool_name": "StrReplace", "tool_input": {"path": "x.rs"}}),
            json!({"random": "data"}),
        ];
        for input in &inputs {
            maybe_capture(&input.to_string());
        }
    }

    #[test]
    fn multi_edit_extracts_all() {
        let edits = collect_edits(&json!({
            "edits": [
                { "old_string": "a", "new_string": "b" },
                { "old_string": "c", "new_string": "d" }
            ]
        }));
        assert_eq!(edits.len(), 2);
        assert_eq!(edits[0].old, "a");
        assert_eq!(edits[1].old, "c");
    }

    #[test]
    fn apply_patch_payload_strings_are_collected() {
        let args = json!({
            "command": ["apply_patch", "*** Begin Patch\n*** Update File: src/a.rs\n*** End Patch"]
        });
        let mut texts = Vec::new();
        collect_strings(&args, &mut texts);
        let targets: Vec<String> = texts
            .iter()
            .flat_map(|t| crate::core::shell_file_effects::apply_patch_targets(t))
            .collect();
        assert_eq!(targets, ["src/a.rs"]);
        assert!(is_apply_patch_tool("apply_patch"));
        assert!(!is_apply_patch_tool("Bash"));
    }

    #[test]
    fn recent_edits_ring_buffer() {
        let mut ring = RecentEdits::new();
        for i in 0..DEDUP_CAPACITY {
            ring.insert(i as u64 + 100);
        }
        assert!(ring.contains(100));
        assert!(ring.contains(100 + DEDUP_CAPACITY as u64 - 1));
        // Insert one more — evicts the oldest
        ring.insert(999);
        assert!(!ring.contains(100));
        assert!(ring.contains(999));
    }
}