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-v11 -->
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", "full")` |
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
79| Goal | Mode | When |
80|------|------|------|
81| Edit this file | `full` | Before any edit |
82| Understand API | `signatures` | Context-only, won't edit |
83| Re-read after edit | `diff` | Post-edit verification |
84| Large file overview | `map` | >500 lines, won't edit |
85| Specific region | `lines:N-M` | Know exact location |
86
87## Workflow (follow this order)
881. **Orient:** `ctx_overview(task)` or `ctx_compose(task, path)` for unfamiliar tasks
892. **Locate:** `ctx_search(pattern, path)` for exact text; `ctx_semantic_search(query)` for concepts
903. **Read:** `ctx_read(path, mode)` with appropriate mode from table above
914. **Edit:** `ctx_edit(path, old_string, new_string)` or native Edit if available
925. **Verify:** `ctx_read(path, "diff")` + `ctx_shell("test command")`
936. **Record:** `ctx_knowledge(action="remember", content="...")` for non-obvious findings
94
95## Session
96- **Start:** `ctx_session(action="status")` + `ctx_knowledge(action="wakeup")`
97- **End:** `ctx_session(action="decision", content="what was done + next steps")`
98- **On [CHECKPOINT]:** `ctx_session(action="task", value="current status")`
99
100NEVER use native Read/Grep/Shell when ctx_* equivalents are available.
101<!-- /lean-ctx -->"#;
102
103pub(super) const RULES_DEDICATED: &str = r#"# lean-ctx — Context Engineering Layer
109<!-- lean-ctx-rules-v11 -->
110
111## Tool Mapping (MANDATORY — use instead of native equivalents)
112| Instead of | Use | Example |
113|------------|-----|---------|
114| Read/cat/head/tail | `ctx_read(path, mode)` | `ctx_read("src/main.rs", "full")` |
115| Grep/rg/find | `ctx_search(pattern, path)` | `ctx_search("fn handle", "src/")` |
116| Shell/bash | `ctx_shell(command)` | `ctx_shell("cargo test")` |
117| Edit (when Read unavailable) | `ctx_edit(path, old, new)` | `ctx_edit("f.rs", "old", "new")` |
118
119## ctx_read Mode Selection
120| Goal | Mode | When |
121|------|------|------|
122| Edit this file | `full` | Before any edit |
123| Understand API | `signatures` | Context-only, won't edit |
124| Re-read after edit | `diff` | Post-edit verification |
125| Large file overview | `map` | >500 lines, won't edit |
126| Specific region | `lines:N-M` | Know exact location |
127| Unsure | `auto` | System selects optimal mode |
128
129## Workflow (follow this order)
1301. **Orient:** `ctx_overview(task)` or `ctx_compose(task, path)` for unfamiliar tasks
1312. **Locate:** `ctx_search(pattern, path)` for exact text; `ctx_semantic_search(query)` for concepts
1323. **Read:** `ctx_read(path, mode)` with appropriate mode from table above
1334. **Edit:** `ctx_edit(path, old_string, new_string)` or native Edit if available
1345. **Verify:** `ctx_read(path, "diff")` + `ctx_shell("test command")`
1356. **Record:** `ctx_knowledge(action="remember", content="...")` for non-obvious findings
136
137## Proactive (use without being asked)
138- `ctx_overview(task)` — at session start for orientation
139- `ctx_compress` — when context grows large (at phase boundaries)
140- `ctx_knowledge(action="wakeup")` — at session start to surface prior findings
141
142## Compression Bypass (only when compressed output hides needed detail)
143`ctx_read(path, "lines:N-M")` → `ctx_read(path, "full")` → `ctx_shell(cmd, raw=true)`
144Return to compressed defaults after one expanded retrieval.
145
146## Risk Gate (before high-impact edits)
147Before editing exported symbols, auth, DB schemas, or 3+ files: run `ctx_impact(action="analyze")`
148and `ctx_callgraph(action="callers")` to confirm blast radius.
149
150## Session
151- **Start:** `ctx_session(action="status")` + `ctx_knowledge(action="wakeup")`
152- **End:** `ctx_session(action="decision", content="what was done + next steps")`
153- **On [CHECKPOINT]:** `ctx_session(action="task", value="current status")`
154
155NEVER use native Read/Grep/Shell when ctx_* equivalents are available.
156<!-- /lean-ctx -->"#;
157
158pub(super) const RULES_CURSOR_MDC: &str = include_str!("../templates/lean-ctx.mdc");
162
163pub(super) fn rules_content(format: &RulesFormat) -> &'static str {
164 match format {
165 RulesFormat::SharedMarkdown => RULES_SHARED,
166 RulesFormat::DedicatedMarkdown => RULES_DEDICATED,
167 RulesFormat::CursorMdc => RULES_CURSOR_MDC,
168 }
169}