Skip to main content

lean_ctx/core/
rules_canonical.rs

1//! Canonical Hybrid Policy — the single source of truth for all lean-ctx rules.
2//!
3//! Every template, injected rule file, SKILL.md, and MCP instructions field
4//! MUST derive its tool-mapping table from this module. No inline tool tables
5//! elsewhere in the codebase.
6
7/// Generates the canonical tool-mapping table for the given mode.
8pub fn tool_table(mode: Mode) -> &'static str {
9    match mode {
10        Mode::Hybrid => HYBRID_TABLE,
11        Mode::Mcp => MCP_TABLE,
12    }
13}
14
15/// Returns the full rules block (LITM-optimized) for dedicated rule files.
16pub fn dedicated_rules(mode: Mode) -> String {
17    let table = tool_table(mode);
18    let shell_note = match mode {
19        Mode::Hybrid => "Shell commands: use `lean-ctx -c \"<cmd>\"` via your Shell tool. NEVER use `ctx_shell` in Hybrid mode.",
20        Mode::Mcp => "Shell commands: use `ctx_shell(command)`. NEVER use raw Shell/bash.",
21    };
22
23    format!(
24        r#"# lean-ctx — Context Engineering Layer
25<!-- lean-ctx-rules-v11 -->
26
27CRITICAL: ALWAYS use lean-ctx tools instead of native equivalents. This is NOT optional.
28
29{table}
30
31{shell_note}
32
33## Workflow: Orient → Locate → Read → Edit → Verify → Record
341. Orient: ctx_overview(task) or ctx_compose(task, path)
352. Locate: ctx_search(pattern, path) or ctx_semantic_search(query)
363. Read: ctx_read(path, mode) — full before edits, signatures for context
374. Edit: ctx_edit(path, old_string, new_string) or native Edit
385. Verify: ctx_read(path, "diff") + ctx_shell("test command")
396. Record: ctx_knowledge(action="remember", content="...")
40
41File editing: use native Edit/StrReplace. Write, Delete, Glob → use normally.
42NEVER loop on Edit failures — switch to ctx_edit immediately.
43
44NEVER use native Read/Grep/Shell when ctx_* equivalents are available.
45<!-- /lean-ctx -->"#
46    )
47}
48
49/// Returns a short instructions string for MCP JSON `instructions` field.
50pub fn mcp_instructions(mode: Mode) -> &'static str {
51    match mode {
52        Mode::Hybrid => MCP_INSTRUCTIONS_HYBRID,
53        Mode::Mcp => MCP_INSTRUCTIONS_MCP,
54    }
55}
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58pub enum Mode {
59    Hybrid,
60    Mcp,
61}
62
63impl Mode {
64    pub fn from_hook_mode(hook: &crate::hooks::HookMode) -> Self {
65        match hook {
66            crate::hooks::HookMode::Hybrid => Mode::Hybrid,
67            crate::hooks::HookMode::Mcp => Mode::Mcp,
68        }
69    }
70}
71
72const HYBRID_TABLE: &str = "\
73| MUST USE | NEVER USE | Why |
74|----------|-----------|-----|
75| `ctx_read(path, mode)` | `Read` / `cat` / `head` / `tail` | Cached, 10 read modes, re-reads ~13 tokens |
76| `ctx_search(pattern, path)` | `Grep` / `rg` | Compact, token-efficient results |
77| `lean-ctx -c \"<cmd>\"` (via Shell) | `ctx_shell` / raw `Shell` | CLI compression, no MCP overhead |
78| `lean-ctx ls [path]` (via Shell) | `ctx_tree` / `ls` / `find` | Compact directory maps |";
79
80const MCP_TABLE: &str = "\
81| MUST USE | NEVER USE | Why |
82|----------|-----------|-----|
83| `ctx_read(path, mode)` | `Read` / `cat` / `head` / `tail` | Cached, 10 read modes, re-reads ~13 tokens |
84| `ctx_search(pattern, path)` | `Grep` / `rg` | Compact, token-efficient results |
85| `ctx_shell(command)` | `Shell` / `bash` / terminal | Pattern compression for git/npm/cargo output |
86| `ctx_tree(path, depth)` | `ls` / `find` | Compact directory maps |";
87
88const MCP_INSTRUCTIONS_HYBRID: &str = "\
89lean-ctx tools replace Read/Grep/Shell/ls. Workflow: Orient(ctx_overview) → Locate(ctx_search) → Read(ctx_read) → Edit(ctx_edit/native) → Verify(ctx_read diff + lean-ctx -c test) → Record(ctx_knowledge). Edit/Write/Glob: native.";
90
91const MCP_INSTRUCTIONS_MCP: &str = "\
92lean-ctx tools replace Read/Grep/Shell/ls. Workflow: Orient(ctx_overview) → Locate(ctx_search) → Read(ctx_read) → Edit(ctx_edit/native) → Verify(ctx_read diff + ctx_shell test) → Record(ctx_knowledge). Edit/Write/Glob: native.";
93
94/// Tool-mapping in bullet format for MCP instructions blocks.
95pub fn tool_mapping_bullets(mode: Mode) -> &'static str {
96    match mode {
97        Mode::Hybrid => HYBRID_BULLETS,
98        Mode::Mcp => MCP_BULLETS,
99    }
100}
101
102const MCP_BULLETS: &str = "\
103lean-ctx MCP — MANDATORY tool mapping:\n\
104• Read/cat/head/tail -> ctx_read(path, mode)  [NEVER use native Read]\n\
105• Shell/bash -> ctx_shell(command)  [NEVER use native Shell]\n\
106• Grep/rg -> ctx_search(pattern, path)  [NEVER use native Grep]\n\
107• ls/find -> ctx_tree(path, depth)\n\
108• Edit/StrReplace -> use native (lean-ctx only replaces READ, not WRITE)\n\
109• Write, Delete, Glob -> use normally";
110
111const HYBRID_BULLETS: &str = "\
112lean-ctx — MANDATORY tool mapping:\n\
113• Read/cat/head/tail -> ctx_read(path, mode)  [NEVER use native Read]\n\
114• Shell commands -> lean-ctx -c \"<cmd>\" (via Shell)  [NEVER use ctx_shell]\n\
115• Grep/rg -> ctx_search(pattern, path)  [NEVER use native Grep]\n\
116• ls/find -> lean-ctx ls [path] (via Shell)\n\
117• Edit/StrReplace -> use native (lean-ctx only replaces READ, not WRITE)\n\
118• Write, Delete, Glob -> use normally";
119
120pub fn compatibility_block() -> &'static str {
121    "COMPATIBILITY: lean-ctx replaces READ operations only. Edit/Write/StrReplace stay native.\n\
122     FILE EDITING: Native Edit/StrReplace preferred. If Edit fails, use ctx_edit immediately."
123}
124
125pub fn ctx_read_modes_block() -> &'static str {
126    "ctx_read modes: full|map|signatures|diff|task|reference|aggressive|entropy|lines:N-M. Auto-selects. Cached re-reads can be ~13 tok when unchanged. Fn refs F1,F2.. persist.\n\
127     Cache auto-validates via file mtime. Use fresh=true (or start_line / lines:N-M) to force a disk re-read."
128}
129
130pub fn automation_block() -> &'static str {
131    "Auto: ctx_overview, ctx_preload, ctx_dedup, ctx_compress run behind the scenes. Checkpoint every 15 calls.\n\
132     Multi-agent: ctx_agent(action=handoff|sync). Diary: ctx_agent(action=diary, category=discovery|decision|blocker|progress|insight).\n\
133     ctx_semantic_search for meaning-based search. ctx_session for memory. ctx_knowledge: remember|recall|timeline|rooms|search|wakeup.\n\
134     ctx_shell raw=true for uncompressed output."
135}
136
137pub fn cep_block() -> &'static str {
138    "CEP v1: 1.ACT FIRST 2.DELTA ONLY (Fn refs) 3.STRUCTURED (+/-/~) 4.ONE LINE PER ACTION 5.QUALITY ANCHOR"
139}
140
141pub fn litm_end_block(mode: Mode) -> &'static str {
142    match mode {
143        Mode::Hybrid => {
144            "--- TOOL PREFERENCE (LITM-END) ---\n\
145             ctx_read>Read ctx_search>Grep lean-ctx_-c>Shell lean-ctx_ls>ls | Edit/Write/Glob=native"
146        }
147        Mode::Mcp => {
148            "--- TOOL PREFERENCE (LITM-END) ---\n\
149             ctx_read>Read ctx_shell>Shell ctx_search>Grep ctx_tree>ls | Edit/Write/Glob=native"
150        }
151    }
152}
153
154pub fn unified_tool_mode_block() -> &'static str {
155    "UNIFIED TOOL MODE (active):\n\
156     Additional tools are accessed via ctx() meta-tool: ctx(tool=\"<name>\", ...params).\n\
157     See the ctx() tool description for available sub-tools."
158}
159
160#[cfg(test)]
161mod tests {
162    use super::*;
163
164    #[test]
165    fn hybrid_table_contains_must() {
166        assert!(HYBRID_TABLE.contains("MUST USE"));
167        assert!(!HYBRID_TABLE.contains("PREFER"));
168    }
169
170    #[test]
171    fn mcp_table_contains_must() {
172        assert!(MCP_TABLE.contains("MUST USE"));
173        assert!(!MCP_TABLE.contains("PREFER"));
174    }
175
176    #[test]
177    fn hybrid_table_uses_cli() {
178        assert!(HYBRID_TABLE.contains("lean-ctx -c"));
179        for line in HYBRID_TABLE.lines() {
180            assert!(
181                !line.starts_with("| `ctx_shell"),
182                "Hybrid table must not list ctx_shell in MUST USE column"
183            );
184        }
185    }
186
187    #[test]
188    fn mcp_table_uses_ctx_shell() {
189        assert!(MCP_TABLE.contains("ctx_shell"));
190        assert!(!MCP_TABLE.contains("lean-ctx -c"));
191    }
192
193    #[test]
194    fn dedicated_rules_have_markers() {
195        let rules = dedicated_rules(Mode::Hybrid);
196        assert!(rules.contains("lean-ctx-rules-v11"));
197        assert!(rules.contains("<!-- /lean-ctx -->"));
198    }
199
200    #[test]
201    fn dedicated_rules_litm_structure() {
202        for mode in [Mode::Hybrid, Mode::Mcp] {
203            let rules = dedicated_rules(mode);
204            let lines: Vec<&str> = rules.lines().collect();
205            let first_5 = lines[..5.min(lines.len())].join("\n");
206            assert!(
207                first_5.contains("CRITICAL") || first_5.contains("MUST"),
208                "LITM: MUST instruction near start for {mode:?}"
209            );
210            let last_3 = lines[lines.len().saturating_sub(3)..].join("\n");
211            assert!(
212                last_3.contains("MUST") || last_3.contains("NEVER"),
213                "LITM: reinforcement near end for {mode:?}"
214            );
215        }
216    }
217
218    #[test]
219    fn no_prefer_in_any_output() {
220        for mode in [Mode::Hybrid, Mode::Mcp] {
221            let rules = dedicated_rules(mode);
222            assert!(
223                !rules.contains("PREFER"),
224                "canonical rules must use MUST, not PREFER for {mode:?}"
225            );
226            let instructions = mcp_instructions(mode);
227            assert!(
228                !instructions.contains("PREFER"),
229                "MCP instructions must use MUST, not PREFER for {mode:?}"
230            );
231        }
232    }
233
234    #[test]
235    fn hybrid_bullets_use_cli() {
236        let bullets = tool_mapping_bullets(Mode::Hybrid);
237        for line in bullets.lines() {
238            if line.starts_with('•') {
239                assert!(
240                    !line.starts_with("• Shell/bash -> ctx_shell"),
241                    "Hybrid bullets must not map Shell to ctx_shell"
242                );
243            }
244        }
245        assert!(bullets.contains("lean-ctx -c"));
246    }
247
248    #[test]
249    fn mcp_bullets_no_lean_ctx_c() {
250        let bullets = tool_mapping_bullets(Mode::Mcp);
251        assert!(
252            !bullets.contains("lean-ctx -c"),
253            "MCP bullets must not reference lean-ctx -c"
254        );
255        assert!(bullets.contains("ctx_shell"));
256    }
257
258    #[test]
259    fn shared_sections_not_empty() {
260        assert!(!compatibility_block().is_empty());
261        assert!(!ctx_read_modes_block().is_empty());
262        assert!(!automation_block().is_empty());
263        assert!(!cep_block().is_empty());
264        assert!(!litm_end_block(Mode::Mcp).is_empty());
265        assert!(!litm_end_block(Mode::Hybrid).is_empty());
266        assert!(!unified_tool_mode_block().is_empty());
267    }
268}