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
//! Profile-aware rules section builders (#756).
//!
//! Each function generates the text for one rules section, filtering tool
//! references by the active [`ToolProfile`]. Power profile produces output
//! identical to the previous static constants; Minimal/Standard/Custom omit
//! tools the agent cannot call.
//!
//! Folded-tool alignment (#509): `ctx_symbol` → `ctx_search(action=symbol)`,
//! `ctx_semantic_search` → `ctx_search(action=semantic)`.

use super::tool_profiles::ToolProfile;

fn has(p: &ToolProfile, name: &str) -> bool {
    p.is_tool_enabled(name)
}

/// The memory tools the profile offers, in the wording the rules use. The lazy
/// core offers ctx_session but keeps ctx_knowledge behind ctx_call.
fn memory_tools(p: &ToolProfile) -> Option<&'static str> {
    match (has(p, "ctx_session"), has(p, "ctx_knowledge")) {
        (true, true) => Some("ctx_session / ctx_knowledge"),
        (true, false) => Some("ctx_session"),
        (false, true) => Some("ctx_knowledge"),
        (false, false) => None,
    }
}

/// Intent-to-tool playbook — only advertises tools the profile exposes.
pub(crate) fn intent_section(p: &ToolProfile) -> String {
    let mut lines = vec!["Tool selection by intent:".to_string()];

    if has(p, "ctx_compose") {
        lines.push("• Orient / understand code (call FIRST) -> ctx_compose".into());
    }

    let read_line = if has(p, "ctx_patch") {
        "• Read a file -> ctx_read(path, mode=signatures|map|full); edit after reading -> ctx_patch"
    } else {
        "• Read a file -> ctx_read(path, mode=signatures|map|full)"
    };
    lines.push(read_line.into());

    // #509: ctx_symbol and ctx_semantic_search are folded into ctx_search actions.
    let mut search_parts = vec!["• Exact symbol -> ctx_search(action=symbol)"];
    search_parts.push("pattern -> ctx_search");
    search_parts.push("by meaning -> ctx_search(action=semantic)");
    lines.push(search_parts.join("; "));

    let mut nav = String::from("• Files by glob -> ctx_glob; structure -> ctx_tree");
    if has(p, "ctx_callgraph") {
        nav.push_str("; callers/impact -> ctx_callgraph");
    }
    lines.push(nav);

    let mut verify = String::from("• Verify after edits -> ctx_shell(test/build)");
    if let Some(memory) = memory_tools(p) {
        verify.push_str(&format!("; memory -> {memory}"));
    }
    lines.push(verify);

    lines.push(
        "Semantic questions -> search tools, not whole-file reads: \
         reading more ≠ understanding more."
            .into(),
    );
    lines.join("\n")
}

/// Mandatory tool mapping on hook-covered hosts (Cursor with hooks).
/// v8: strict mode — always prefer ctx_* even when hooks would cover native tools.
pub(crate) fn hook_covered_tools_section(p: &ToolProfile) -> String {
    let mut lines = vec!["MANDATORY MAPPING (always use ctx_* instead of native):".to_string()];

    lines.push(
        "• Read/cat -> ctx_read(path, mode) — structural views, caching, source references".into(),
    );
    lines.push(
        "• Grep/search -> ctx_search(pattern, path) — also action=symbol|semantic \
         for definitions/meaning"
            .into(),
    );
    lines.push("• Shell/bash -> ctx_shell(command) — command-specific output shaping".into());

    if has(p, "ctx_compose") {
        lines.push(
            "• ctx_compose — orient in code FIRST (bundles search + read + symbols) \
             — call before editing/debugging"
                .into(),
        );
    }

    if has(p, "ctx_callgraph") {
        lines.push(
            "• ctx_callgraph — callers, callees, blast radius — use instead of manual \
             file reading"
                .into(),
        );
    }

    if let Some(memory) = memory_tools(p) {
        lines.push(format!(
            "• {memory} — persistent memory — record decisions & progress after milestones"
        ));
    }

    if has(p, "ctx_expand") {
        lines.push("• ctx_expand — recover full text from [Archived]/compressed output".into());
    }

    lines.join("\n")
}

/// Shadow-mode exclusive tools (no native trigger to intercept).
pub(crate) fn shadow_minimal_section(p: &ToolProfile) -> String {
    let mut exclusives = Vec::new();

    if has(p, "ctx_compose") {
        exclusives.push("ctx_compose (understand code, call first)");
    }

    // #509: always ctx_search actions, not standalone tools
    exclusives.push("ctx_search(action=symbol) (exact symbol)");
    exclusives.push("ctx_search(action=semantic) (by meaning)");

    if has(p, "ctx_callgraph") {
        exclusives.push("ctx_callgraph (callers)");
    }
    // Name only what the profile offers (the lazy core has ctx_session but
    // keeps ctx_knowledge behind ctx_call, which `ctx_call_fallback` covers).
    // The full pair keeps its established order (identical to SHADOW_MINIMAL).
    match (has(p, "ctx_session"), has(p, "ctx_knowledge")) {
        (true, true) => exclusives.push("ctx_knowledge / ctx_session (memory)"),
        (true, false) => exclusives.push("ctx_session (memory)"),
        (false, true) => exclusives.push("ctx_knowledge (memory)"),
        (false, false) => {}
    }

    // #1849: the same text goes to every host, and their edit tools differ
    // (Edit, StrReplace, Codex `apply_patch`) — so name none of them.
    let edit_line = if has(p, "ctx_patch") {
        "File editing → the host's native edit tool (lean-ctx only handles reads); if denied, use ctx_patch.\n"
    } else {
        "File editing → the host's native edit tool (lean-ctx only handles reads).\n"
    };

    format!(
        "lean-ctx shadow mode: native read/search/shell calls auto-route to ctx_* \
         — no tool-mapping needed.\n\
         {edit_line}\
         Exclusive tools (no native trigger): {}.",
        exclusives.join(", ")
    )
}

/// Anti-patterns — only references tools the profile exposes.
pub(crate) fn anti_section(p: &ToolProfile) -> String {
    let mut lines = vec!["Anti-patterns — do NOT:".to_string()];

    if has(p, "ctx_compose") {
        lines.push(
            "• Chain ctx_search -> ctx_read -> ctx_search(action=symbol) \
             — one ctx_compose replaces all three"
                .into(),
        );
    }

    lines.push("• Use ctx_read(mode=full) for orientation — use mode=signatures".into());

    // #1849: name only the graph tools the profile keeps — a disabled
    // ctx_callgraph must not reappear through its ctx_graph sibling.
    let graph_tools: Vec<&str> = ["ctx_callgraph", "ctx_graph"]
        .into_iter()
        .filter(|t| has(p, t))
        .collect();
    if !graph_tools.is_empty() {
        let (names, verb) = match graph_tools.as_slice() {
            [one] => (*one, "it tracks"),
            _ => ("ctx_callgraph/ctx_graph", "they track"),
        };
        lines.push(format!(
            "• Use {names} for const/static/variable refs — {verb} \
             call edges and file deps only; use ctx_search instead"
        ));
    }

    lines.join("\n")
}

/// Invocation nudge — "ctx_compose first" only where the profile has it (#1849).
pub(crate) fn must_invoke_section(p: &ToolProfile) -> String {
    if has(p, "ctx_compose") {
        super::rules_canonical::MUST_INVOKE.to_string()
    } else {
        "ACTUALLY EMIT the ctx_* tool call — describing a tool is not calling it.".to_string()
    }
}

/// Parallel-calls nudge — the ctx_compose aside only where the profile has it (#1849).
pub(crate) fn parallel_section(p: &ToolProfile) -> String {
    if has(p, "ctx_compose") {
        super::rules_canonical::PARALLEL.to_string()
    } else {
        "PARALLEL: fire independent tool calls in the SAME turn.".to_string()
    }
}

/// LITM end-of-instructions preference line — only lists enabled tools.
pub(crate) fn litm_end_section(p: &ToolProfile) -> String {
    let mut prefs = Vec::new();

    if has(p, "ctx_compose") {
        prefs.push("ctx_compose>chain");
    }
    prefs.push("ctx_read>Read");
    prefs.push("ctx_shell>Shell");
    prefs.push("ctx_search>Grep");
    prefs.push("ctx_glob>Glob");
    prefs.push("ctx_tree>ls");

    format!(
        "TOOL PREFERENCE (END): {} | Edit/Write/Delete=native",
        prefs.join(" ")
    )
}

/// `ctx_call` gateway fallback — shown when the profile hides tools the agent
/// might need. Returns `None` for Power (all tools visible).
pub(crate) fn ctx_call_fallback(p: &ToolProfile) -> Option<String> {
    if matches!(p, ToolProfile::Power) {
        return None;
    }
    // Kept short: the lean core now carries this line in every session's
    // instructions, which have a fixed token budget.
    Some("Other tools: ctx_call(tool=<name>).".into())
}

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

    #[test]
    fn intent_minimal_omits_compose_and_callgraph() {
        let text = intent_section(&ToolProfile::Minimal);
        assert!(!text.contains("ctx_compose"), "minimal has no ctx_compose");
        assert!(
            !text.contains("ctx_callgraph"),
            "minimal has no ctx_callgraph"
        );
        assert!(!text.contains("ctx_patch"), "minimal has no ctx_patch");
        assert!(text.contains("ctx_read"), "minimal always has ctx_read");
        assert!(text.contains("ctx_search"), "minimal always has ctx_search");
    }

    #[test]
    fn intent_standard_includes_compose_and_callgraph() {
        let text = intent_section(&ToolProfile::Standard);
        assert!(text.contains("ctx_compose"), "standard has ctx_compose");
        assert!(text.contains("ctx_callgraph"), "standard has ctx_callgraph");
        assert!(text.contains("ctx_patch"), "standard has ctx_patch");
    }

    #[test]
    fn intent_power_matches_full_set() {
        let text = intent_section(&ToolProfile::Power);
        assert!(text.contains("ctx_compose"));
        assert!(text.contains("ctx_callgraph"));
        assert!(text.contains("ctx_patch"));
        assert!(text.contains("ctx_session"));
    }

    #[test]
    fn intent_uses_folded_search_actions() {
        for p in [
            ToolProfile::Minimal,
            ToolProfile::Standard,
            ToolProfile::Power,
        ] {
            let text = intent_section(&p);
            assert!(
                !text.contains("ctx_symbol"),
                "must use ctx_search(action=symbol), not ctx_symbol"
            );
            assert!(
                !text.contains("ctx_semantic_search"),
                "must use ctx_search(action=semantic), not ctx_semantic_search"
            );
            assert!(text.contains("ctx_search(action=symbol)"));
            assert!(text.contains("ctx_search(action=semantic)"));
        }
    }

    #[test]
    fn hook_covered_tools_respects_profile() {
        let min = hook_covered_tools_section(&ToolProfile::Minimal);
        assert!(!min.contains("ctx_compose"), "minimal: no compose");
        assert!(!min.contains("ctx_callgraph"), "minimal: no callgraph");
        assert!(
            min.contains("ctx_read"),
            "minimal: always has ctx_read mapping"
        );
        assert!(
            min.contains("ctx_search"),
            "minimal: always has ctx_search mapping"
        );
        assert!(
            min.contains("ctx_shell"),
            "minimal: always has ctx_shell mapping"
        );

        let std = hook_covered_tools_section(&ToolProfile::Standard);
        assert!(std.contains("ctx_compose"), "standard: compose present");
        assert!(std.contains("ctx_callgraph"), "standard: callgraph present");
    }

    #[test]
    fn shadow_minimal_respects_profile() {
        let min = shadow_minimal_section(&ToolProfile::Minimal);
        assert!(!min.contains("ctx_compose"));
        assert!(!min.contains("ctx_callgraph"));
        assert!(min.contains("ctx_search(action=symbol)"));
        assert!(!min.contains("ctx_patch"));

        let power = shadow_minimal_section(&ToolProfile::Power);
        assert!(power.contains("ctx_patch"));
    }

    #[test]
    fn nudges_name_ctx_compose_only_when_the_profile_has_it() {
        // #1849: PARALLEL and MUST_INVOKE were static and said "ctx_compose"
        // on every profile, including Minimal, which does not expose it.
        use crate::core::rules_canonical::{MUST_INVOKE, PARALLEL};
        assert_eq!(parallel_section(&ToolProfile::Power), PARALLEL);
        assert_eq!(must_invoke_section(&ToolProfile::Power), MUST_INVOKE);
        for text in [
            parallel_section(&ToolProfile::Minimal),
            must_invoke_section(&ToolProfile::Minimal),
        ] {
            assert!(text.starts_with("PARALLEL") || text.starts_with("ACTUALLY EMIT"));
            assert!(!text.contains("ctx_compose"), "{text}");
        }
    }

    #[test]
    fn anti_graph_line_names_only_enabled_graph_tools() {
        // #1849: disabling ctx_callgraph left it named through "ctx_callgraph/ctx_graph".
        let graph_only = ToolProfile::Custom(vec!["ctx_read".into(), "ctx_graph".into()]);
        let text = anti_section(&graph_only);
        assert!(text.contains("• Use ctx_graph for const/static/variable refs — it tracks"));
        assert!(!text.contains("ctx_callgraph"), "{text}");
        let power = anti_section(&ToolProfile::Power);
        assert!(
            power.contains("ctx_callgraph/ctx_graph for const/static/variable refs — they track")
        );
    }

    #[test]
    fn shadow_edit_line_names_no_host_tool() {
        // #1849: Codex edits through `apply_patch`; naming Edit/StrReplace sent
        // it looking for tools it does not have.
        for p in [
            ToolProfile::Minimal,
            ToolProfile::Standard,
            ToolProfile::Power,
        ] {
            let text = shadow_minimal_section(&p);
            assert!(text.contains("the host's native edit tool"), "{p}");
            for host_tool in ["StrReplace", "Edit/", "apply_patch"] {
                assert!(!text.contains(host_tool), "{p}: names {host_tool}");
            }
        }
    }

    #[test]
    fn anti_minimal_omits_compose_chain() {
        let text = anti_section(&ToolProfile::Minimal);
        assert!(
            !text.contains("ctx_compose"),
            "minimal: no compose anti-pattern"
        );
        assert!(
            text.contains("ctx_read(mode=full)"),
            "universal anti-pattern stays"
        );
    }

    #[test]
    fn ctx_call_fallback_absent_for_power() {
        assert!(ctx_call_fallback(&ToolProfile::Power).is_none());
    }

    #[test]
    fn ctx_call_fallback_present_for_non_power() {
        assert!(ctx_call_fallback(&ToolProfile::Minimal).is_some());
        assert!(ctx_call_fallback(&ToolProfile::Standard).is_some());
    }

    #[test]
    fn litm_end_always_has_core_tools() {
        for p in [
            ToolProfile::Minimal,
            ToolProfile::Standard,
            ToolProfile::Power,
        ] {
            let text = litm_end_section(&p);
            assert!(text.contains("ctx_read>Read"));
            assert!(text.contains("ctx_shell>Shell"));
            assert!(text.contains("ctx_search>Grep"));
        }
    }

    #[test]
    fn litm_end_compose_only_when_enabled() {
        let min = litm_end_section(&ToolProfile::Minimal);
        assert!(!min.contains("ctx_compose"));
        let std = litm_end_section(&ToolProfile::Standard);
        assert!(std.contains("ctx_compose"));
    }
}