lean_ctx/rules_inject/
content.rs1use std::path::PathBuf;
5
6use super::RulesFormat;
7
8#[must_use]
12pub fn canonical_rules_block() -> &'static str {
13 RULES_SHARED
14}
15
16pub fn rules_dedicated_markdown() -> &'static str {
17 RULES_DEDICATED
18}
19
20pub fn rules_shared_content() -> &'static str {
21 RULES_SHARED
22}
23
24pub fn dedicated_session_summary() -> &'static str {
33 DEDICATED_SESSION_SUMMARY
34}
35
36const DEDICATED_SESSION_SUMMARY: &str =
37 "lean-ctx is active — prefer its tools over native equivalents to save context tokens:
38- ctx_read → instead of Read/cat/head/tail (cached, 10 modes, re-reads ~13 tokens)
39- ctx_shell → instead of bash/Shell (95+ output-compression patterns)
40- ctx_search → instead of Grep/rg/find (compact, token-efficient results)
41- ctx_tree → instead of ls/find (compact directory maps)
42Native Edit/Write/Glob stay as-is. If Edit needs Read and Read is unavailable, use ctx_edit.
43NEVER use native Read/Grep/Shell when a ctx_* equivalent exists.";
44
45pub fn opencode_dedicated_rules_path(home: &std::path::Path) -> PathBuf {
48 home.join(".config/opencode/rules/lean-ctx.md")
49}
50
51pub fn gemini_dedicated_rules_path(home: &std::path::Path) -> PathBuf {
56 home.join(".gemini").join(GEMINI_DEDICATED_CONTEXT_FILENAME)
57}
58
59pub const GEMINI_DEDICATED_CONTEXT_FILENAME: &str = "LEANCTX.md";
61
62pub(super) const RULES_SHARED: &str = r#"# lean-ctx — Context Engineering Layer
68<!-- lean-ctx-rules-v12 -->
69
70## Tool Mapping (MANDATORY — use instead of native equivalents)
71| Instead of | Use | Example |
72|------------|-----|---------|
73| Read/cat/head/tail | `ctx_read(path, mode)` | `ctx_read("src/main.rs")` (omit mode = auto) |
74| Grep/rg/find | `ctx_search(pattern, path)` | `ctx_search("fn handle", "src/")` |
75| Shell/bash | `ctx_shell(command)` | `ctx_shell("cargo test")` |
76| Edit (when Read unavailable) | `ctx_edit(path, old, new)` | `ctx_edit("f.rs", "old", "new")` |
77
78## ctx_read Mode Selection (omit mode to auto-select — recommended)
79| Goal | Mode | When |
80|------|------|------|
81| Exploring / unsure | `auto` (default) | Omit mode; system picks optimal |
82| Edit this file | `full` | Right before an edit |
83| Understand API | `signatures` | Context-only, won't edit |
84| Large file overview | `map` | >500 lines, won't edit |
85| Re-read after edit | `diff` | Post-edit verification |
86| Specific region | `lines:N-M` | Know exact location |
87
88## Workflow (follow this order)
891. **Orient:** `ctx_overview(task)` or `ctx_compose(task, path)` for unfamiliar tasks
902. **Locate:** `ctx_search(pattern, path)` for exact text; `ctx_semantic_search(query)` for concepts
913. **Read:** `ctx_read(path, mode)` with appropriate mode from table above
924. **Edit:** `ctx_edit(path, old_string, new_string)` or native Edit if available
935. **Verify:** `ctx_read(path, "diff")` + `ctx_shell("test command")`
946. **Record:** `ctx_knowledge(action="remember", content="...")` for non-obvious findings
95
96## Session
97- **Start:** `ctx_session(action="status")` + `ctx_knowledge(action="wakeup")`
98- **End:** `ctx_session(action="decision", content="what was done + next steps")`
99- **On [CHECKPOINT]:** `ctx_session(action="task", value="current status")`
100
101NEVER use native Read/Grep/Shell when ctx_* equivalents are available.
102<!-- /lean-ctx -->"#;
103
104pub(super) const RULES_DEDICATED: &str = r#"# lean-ctx — Context Engineering Layer
110<!-- lean-ctx-rules-v12 -->
111
112## Tool Mapping (MANDATORY — use instead of native equivalents)
113| Instead of | Use | Example |
114|------------|-----|---------|
115| Read/cat/head/tail | `ctx_read(path, mode)` | `ctx_read("src/main.rs")` (omit mode = auto) |
116| Grep/rg/find | `ctx_search(pattern, path)` | `ctx_search("fn handle", "src/")` |
117| Shell/bash | `ctx_shell(command)` | `ctx_shell("cargo test")` |
118| Edit (when Read unavailable) | `ctx_edit(path, old, new)` | `ctx_edit("f.rs", "old", "new")` |
119
120## ctx_read Mode Selection (omit mode to auto-select — recommended)
121| Goal | Mode | When |
122|------|------|------|
123| Exploring / unsure | `auto` (default) | Omit mode; system picks optimal |
124| Edit this file | `full` | Right before an edit |
125| Understand API | `signatures` | Context-only, won't edit |
126| Large file overview | `map` | >500 lines, won't edit |
127| Re-read after edit | `diff` | Post-edit verification |
128| Specific region | `lines:N-M` | Know exact location |
129
130## Workflow (follow this order)
1311. **Orient:** `ctx_overview(task)` or `ctx_compose(task, path)` for unfamiliar tasks
1322. **Locate:** `ctx_search(pattern, path)` for exact text; `ctx_semantic_search(query)` for concepts
1333. **Read:** `ctx_read(path, mode)` with appropriate mode from table above
1344. **Edit:** `ctx_edit(path, old_string, new_string)` or native Edit if available
1355. **Verify:** `ctx_read(path, "diff")` + `ctx_shell("test command")`
1366. **Record:** `ctx_knowledge(action="remember", content="...")` for non-obvious findings
137
138## Proactive (use without being asked)
139- `ctx_overview(task)` — at session start for orientation
140- `ctx_compress` — when context grows large (at phase boundaries)
141- `ctx_knowledge(action="wakeup")` — at session start to surface prior findings
142
143## Compression Bypass (only when compressed output hides needed detail)
144`ctx_read(path, "lines:N-M")` → `ctx_read(path, "full")` → `ctx_shell(cmd, raw=true)`
145Return to compressed defaults after one expanded retrieval.
146
147## Risk Gate (before high-impact edits)
148Before editing exported symbols, auth, DB schemas, or 3+ files: run `ctx_impact(action="analyze")`
149and `ctx_callgraph(action="callers")` to confirm blast radius.
150
151## Session
152- **Start:** `ctx_session(action="status")` + `ctx_knowledge(action="wakeup")`
153- **End:** `ctx_session(action="decision", content="what was done + next steps")`
154- **On [CHECKPOINT]:** `ctx_session(action="task", value="current status")`
155
156NEVER use native Read/Grep/Shell when ctx_* equivalents are available.
157<!-- /lean-ctx -->"#;
158
159pub(super) const RULES_CURSOR_MDC: &str = include_str!("../templates/lean-ctx.mdc");
163
164pub(super) fn rules_content(format: &RulesFormat) -> &'static str {
165 match format {
166 RulesFormat::SharedMarkdown => RULES_SHARED,
167 RulesFormat::DedicatedMarkdown => RULES_DEDICATED,
168 RulesFormat::CursorMdc => RULES_CURSOR_MDC,
169 }
170}