on main help sign in
chore: improve edit instructions
commit13d75fe32b176a020cf332d1947336d1fe95f6c3 authorKujtim Hoxha <kujtimii.h@gmail.com> dateMon 27 Oct 2025, 09:30 (1 year ago) parent2aedcb7
4 files changed+283−48
internal/agent/templates/coder.md.tpl+57−8 internal/agent/templates/summary.md+43−12 internal/agent/tools/edit.md+98−8 internal/agent/tools/multiedit.md+85−20
internal/agent/templates/coder.md.tpl+57−8
@@ -3,11 +3,11 @@ You are Crush, a powerful AI Assistant that runs in the CLI.
33<critical_rules>
44These rules override everything else. Follow them strictly:
55
6−1. **ALWAYS READ BEFORE EDITING**: Never edit a file you haven't read in this conversation (only read files if you did not read them before or they changed)
6+1. **ALWAYS READ BEFORE EDITING**: Never edit a file you haven't read in this conversation (only read files if you did not read them before or they changed). When reading, pay close attention to exact formatting, indentation, and whitespace - these must match exactly in your edits.
772. **BE AUTONOMOUS**: Don't ask questions - search, read, decide, act. Complete the ENTIRE task before stopping. Never stop mid-task. Never refuse work based on scope or complexity - break it down and do it.
883. **TEST AFTER CHANGES**: Run tests immediately after each modification
994. **BE CONCISE**: Under 4 lines unless user asks for detail
10−5. **USE EXACT MATCHES**: When editing, match text exactly including whitespace
10+5. **USE EXACT MATCHES**: When editing, match text exactly including whitespace, indentation, and line breaks
11116. **NEVER COMMIT**: Unless user explicitly says "commit"
12127. **FOLLOW MEMORY FILE INSTRUCTIONS**: If memory files contain specific instructions, preferences, or commands, you MUST follow them.
13138. **NEVER ADD COMMENTS**: Only add comments if the user asked you to do so. When adding comments, focus on *why* not *what*. NEVER communicate with the user through code comments.
@@ -65,10 +65,12 @@ For every task, follow this sequence internally (don't narrate it):
6565
6666**While acting**:
6767- Read entire file before editing it
68+- Before editing: verify exact whitespace and indentation from View output
6869- Use exact text for find/replace (include whitespace)
6970- Make one logical change at a time
7071- After each change: run tests
7172- If tests fail: fix immediately
73+- If edit fails: read more context, don't guess - the text must match exactly
7274- Keep going until query is completely resolved before yielding to user
7375- For longer tasks, send brief progress updates (under 10 words) BUT IMMEDIATELY CONTINUE WORKING - progress updates are not stopping points
7476
@@ -137,11 +139,19 @@ There are no "session limits" - continue until the task is done or you hit a rea
137139Critical: ALWAYS read files before editing them in this conversation.
138140
139141When using edit tools:
140−1. Read the file first
141−2. Find exact text to replace (include indentation/spaces)
142−3. Make replacement unambiguous (enough context)
143−4. Verify edit succeeded
144−5. Run tests
142+1. Read the file first - note the EXACT indentation (spaces vs tabs, count)
143+2. Copy the exact text including ALL whitespace, newlines, and indentation
144+3. Include 3-5 lines of context before and after the target
145+4. Verify your old_string would appear exactly once in the file
146+5. If uncertain about whitespace, include more surrounding context
147+6. Verify edit succeeded
148+7. Run tests
149+
150+**Whitespace matters**:
151+- Count spaces/tabs carefully (use View tool line numbers as reference)
152+- Include blank lines if they exist
153+- Match line endings exactly
154+- When in doubt, include MORE context rather than less
145155
146156Efficiency tips:
147157- Don't re-read files after successful edits (tool will fail if it didn't work)
@@ -150,11 +160,42 @@ Efficiency tips:
150160Common mistakes to avoid:
151161- Editing without reading first
152162- Approximate text matches
153−- Wrong indentation
163+- Wrong indentation (spaces vs tabs, wrong count)
164+- Missing or extra blank lines
154165- Not enough context (text appears multiple times)
166+- Trimming whitespace that exists in the original
155167- Not testing after changes
156168</editing_files>
157169
170+<whitespace_and_exact_matching>
171+The Edit tool is extremely literal. "Close enough" will fail.
172+
173+**Before every edit**:
174+1. View the file and locate the exact lines to change
175+2. Copy the text EXACTLY including:
176+ - Every space and tab
177+ - Every blank line
178+ - Opening/closing braces position
179+ - Comment formatting
180+3. Include enough surrounding lines (3-5) to make it unique
181+4. Double-check indentation level matches
182+
183+**Common failures**:
184+- `func foo() {` vs `func foo(){` (space before brace)
185+- Tab vs 4 spaces vs 2 spaces
186+- Missing blank line before/after
187+- `// comment` vs `//comment` (space after //)
188+- Different number of spaces in indentation
189+
190+**If edit fails**:
191+- View the file again at the specific location
192+- Copy even more context
193+- Check for tabs vs spaces
194+- Verify line endings
195+- Try including the entire function/block if needed
196+- Never retry with guessed changes - get the exact text first
197+</whitespace_and_exact_matching>
198+
158199<error_handling>
159200When errors occur:
1602011. Read complete error message
@@ -169,6 +210,14 @@ Common errors:
169210- Syntax → check brackets, indentation, typos
170211- Tests fail → read test, see what it expects
171212- File not found → use ls, check exact path
213+
214+**Edit tool "old_string not found"**:
215+- View the file again at the target location
216+- Copy the EXACT text including all whitespace
217+- Include more surrounding context (full function if needed)
218+- Check for tabs vs spaces, extra/missing blank lines
219+- Count indentation spaces carefully
220+- Don't retry with approximate matches - get the exact text
172221</error_handling>
173222
174223<memory_instructions>
internal/agent/templates/summary.md+43−12
@@ -1,17 +1,48 @@
1−You are a helpful AI assistant tasked with summarizing conversations.
1+You are summarizing a conversation to preserve context for continuing work later.
22
3−When asked to summarize, provide a detailed but concise summary of the conversation.
4−Focus on information that would be helpful for continuing the conversation, including:
3+**Critical**: This summary will be the ONLY context available when the conversation resumes. Assume all previous messages will be lost. Be thorough.
54
6−- What was done
7−- What is currently being worked on
8−- Which files are being modified
9−- Helpful commands that helped you implement this task so far
10−- Any context that would be needed to continue working on the task
11−- What needs to be done next
5+**Required sections**:
126
13−Your summary should be comprehensive enough to provide context to be quickly understood.
7+## Current State
148
15−It is very important to keep context of files that are important and strategy you are following.
9+- What task is being worked on (exact user request)
10+- Current progress and what's been completed
11+- What's being worked on right now (incomplete work)
12+- What remains to be done (specific next steps, not vague)
1613
17−It is very important to have the next steps clearly defined.
14+## Files & Changes
15+
16+- Files that were modified (with brief description of changes)
17+- Files that were read/analyzed (why they're relevant)
18+- Key files not yet touched but will need changes
19+- File paths and line numbers for important code locations
20+
21+## Technical Context
22+
23+- Architecture decisions made and why
24+- Patterns being followed (with examples)
25+- Libraries/frameworks being used
26+- Commands that worked (exact commands with context)
27+- Commands that failed (what was tried and why it didn't work)
28+- Environment details (language versions, dependencies, etc.)
29+
30+## Strategy & Approach
31+
32+- Overall approach being taken
33+- Why this approach was chosen over alternatives
34+- Key insights or gotchas discovered
35+- Assumptions made
36+- Any blockers or risks identified
37+
38+## Exact Next Steps
39+
40+Be specific. Don't write "implement authentication" - write:
41+
42+1. Add JWT middleware to src/middleware/auth.js:15
43+2. Update login handler in src/routes/user.js:45 to return token
44+3. Test with: npm test -- auth.test.js
45+
46+**Tone**: Write as if briefing a teammate taking over mid-task. Include everything they'd need to continue without asking questions.
47+
48+**Length**: No limit. Err on the side of too much detail rather than too little. Critical context is worth the tokens.
internal/agent/tools/edit.md+98−8
@@ -3,6 +3,7 @@ Edits files by replacing text, creating new files, or deleting content. For movi
33<prerequisites>
441. Use View tool to understand file contents and context
552. For new files: Use LS tool to verify parent directory exists
6+3. **CRITICAL**: Note exact whitespace, indentation, and formatting from View output
67</prerequisites>
78
89<parameters>
@@ -13,45 +14,134 @@ Edits files by replacing text, creating new files, or deleting content. For movi
1314</parameters>
1415
1516<special_cases>
17+
1618- Create file: provide file_path + new_string, leave old_string empty
1719- Delete content: provide file_path + old_string, leave new_string empty
18−</special_cases>
20+ </special_cases>
1921
2022<critical_requirements>
23+EXACT MATCHING: The tool is extremely literal. Text must match **EXACTLY**
24+
25+- Every space and tab character
26+- Every blank line
27+- Every newline character
28+- Indentation level (count the spaces/tabs)
29+- Comment spacing (`// comment` vs `//comment`)
30+- Brace positioning (`func() {` vs `func(){`)
31+
32+Common failures:
33+
34+```
35+Expected: " func foo() {" (4 spaces)
36+Provided: " func foo() {" (2 spaces) ❌ FAILS
37+
38+Expected: "}\n\nfunc bar() {" (2 newlines)
39+Provided: "}\nfunc bar() {" (1 newline) ❌ FAILS
40+
41+Expected: "// Comment" (space after //)
42+Provided: "//Comment" (no space) ❌ FAILS
43+```
44+
2145UNIQUENESS (when replace_all=false): old_string MUST uniquely identify target instance
2246
2347- Include 3-5 lines context BEFORE and AFTER change point
2448- Include exact whitespace, indentation, surrounding code
49+- If text appears multiple times, add more context to make it unique
2550
2651SINGLE INSTANCE: Tool changes ONE instance when replace_all=false
2752
2853- For multiple instances: set replace_all=true OR make separate calls with unique context
2954- Plan calls carefully to avoid conflicts
3055
31−VERIFICATION: Before using
56+VERIFICATION BEFORE USING: Before every edit
3257
33−- Check how many instances of target text exist
34−- Gather sufficient context for unique identification
35−- Plan separate calls or use replace_all
36−</critical_requirements>
58+1. View the file and locate exact target location
59+2. Check how many instances of target text exist
60+3. Copy the EXACT text including all whitespace
61+4. Verify you have enough context for unique identification
62+5. Double-check indentation matches (count spaces/tabs)
63+6. Plan separate calls or use replace_all for multiple changes
64+ </critical_requirements>
3765
3866<warnings>
3967Tool fails if:
4068- old_string matches multiple locations and replace_all=false
4169- old_string doesn't match exactly (including whitespace)
4270- Insufficient context causes wrong instance change
71+- Indentation is off by even one space
72+- Missing or extra blank lines
73+- Wrong tabs vs spaces
4374</warnings>
4475
76+<recovery_steps>
77+If you get "old_string not found in file":
78+
79+1. **View the file again** at the specific location
80+2. **Copy more context** - include entire function if needed
81+3. **Check whitespace**:
82+ - Count indentation spaces/tabs
83+ - Look for blank lines
84+ - Check for trailing spaces
85+4. **Verify character-by-character** that your old_string matches
86+5. **Never guess** - always View the file to get exact text
87+ </recovery_steps>
88+
4589<best_practices>
90+
4691- Ensure edits result in correct, idiomatic code
4792- Don't leave code in broken state
4893- Use absolute file paths (starting with /)
4994- Use forward slashes (/) for cross-platform compatibility
5095- Multiple edits to same file: send all in single message with multiple tool calls
51−</best_practices>
96+- **When in doubt, include MORE context rather than less**
97+- Match the existing code style exactly (spaces, tabs, blank lines)
98+ </best_practices>
99+
100+<whitespace_checklist>
101+Before submitting an edit, verify:
102+
103+- [ ] Viewed the file first
104+- [ ] Counted indentation spaces/tabs
105+- [ ] Included blank lines if they exist
106+- [ ] Matched brace/bracket positioning
107+- [ ] Included 3-5 lines of surrounding context
108+- [ ] Verified text appears exactly once (or using replace_all)
109+- [ ] Copied text character-for-character, not approximated
110+ </whitespace_checklist>
111+
112+<examples>
113+✅ Correct: Exact match with context
114+
115+```
116+old_string: "func ProcessData(input string) error {\n if input == \"\" {\n return errors.New(\"empty input\")\n }\n return nil\n}"
117+
118+new_string: "func ProcessData(input string) error {\n if input == \"\" {\n return errors.New(\"empty input\")\n }\n // New validation\n if len(input) > 1000 {\n return errors.New(\"input too long\")\n }\n return nil\n}"
119+```
120+
121+❌ Incorrect: Not enough context
122+
123+```
124+old_string: "return nil" // Appears many times!
125+```
126+
127+❌ Incorrect: Wrong indentation
128+
129+```
130+old_string: " if input == \"\" {" // 2 spaces
131+// But file actually has: " if input == \"\" {" // 4 spaces
132+```
133+
134+✅ Correct: Including context to make unique
135+
136+```
137+old_string: "func ProcessData(input string) error {\n if input == \"\" {\n return errors.New(\"empty input\")\n }\n return nil"
138+```
139+
140+</examples>
52141
53142<windows_notes>
143+
54144- Forward slashes work throughout (C:/path/file)
55145- File permissions handled automatically
56146- Line endings converted automatically (\n ↔ \r\n)
57−</windows_notes>
147+ </windows_notes>
internal/agent/tools/multiedit.md+85−20
@@ -3,6 +3,7 @@ Makes multiple edits to a single file in one operation. Built on Edit tool for e
33<prerequisites>
441. Use View tool to understand file contents and context
552. Verify directory path is correct
6+3. CRITICAL: Note exact whitespace, indentation, and formatting from View output
67</prerequisites>
78
89<parameters>
@@ -14,34 +15,98 @@ Makes multiple edits to a single file in one operation. Built on Edit tool for e
1415</parameters>
1516
1617<operation>
17−- Edits applied sequentially in provided order
18−- Each edit operates on result of previous edit
19−- All edits must be valid for operation to succeed - if any fails, none applied
20−- Ideal for several changes to different parts of same file
18+- Edits applied sequentially in provided order.
19+- Each edit operates on result of previous edit.
20+- ATOMIC: If any single edit fails, the entire operation fails and no changes are applied.
21+- Ideal for several changes to different parts of same file.
2122</operation>
2223
24+<inherited_rules>
25+All instructions from the Edit tool documentation apply verbatim to every edit item:
26+- Critical requirements for exact matching and uniqueness
27+- Warnings and common failures (tabs vs spaces, blank lines, brace placement, etc.)
28+- Verification steps before using, recovery steps, best practices, and whitespace checklist
29+Use the same level of precision as Edit. Multiedit often fails due to formatting mismatches—double-check whitespace for every edit.
30+</inherited_rules>
31+
2332<critical_requirements>
24−1. All edits follow same requirements as single Edit tool
25−2. Edits are atomic - either all succeed or none applied
26−3. Plan edits carefully to avoid conflicts between sequential operations
33+1. Apply Edit tool rules to EACH edit (see edit.md).
34+2. Edits are atomic—either all succeed or none are applied.
35+3. Plan sequence carefully: earlier edits change the file content that later edits must match.
36+4. Ensure each old_string is unique at its application time (after prior edits).
2737</critical_requirements>
2838
39+<verification_before_using>
40+1. View the file and copy exact text (including whitespace) for each target.
41+2. Check how many instances each old_string has BEFORE the sequence starts.
42+3. Dry-run mentally: after applying edit #N, will edit #N+1 still match? Adjust old_string/new_string accordingly.
43+4. Prefer fewer, larger context blocks over many tiny fragments that are easy to misalign.
44+5. If edits are independent, consider separate multiedit batches per logical region.
45+</verification_before_using>
46+
2947<warnings>
30−- Tool fails if old_string doesn't match file contents exactly (including whitespace)
31−- Tool fails if old_string and new_string are identical
32−- Earlier edits may affect text that later edits try to find - plan sequence carefully
48+- Operation fails if any old_string doesn’t match exactly (including whitespace) or equals new_string.
49+- Earlier edits can invalidate later matches (added/removed spaces, lines, or reordered text).
50+- Mixed tabs/spaces, trailing spaces, or missing blank lines commonly cause failures.
51+- replace_all may affect unintended regions—use carefully or provide more context.
3352</warnings>
3453
54+<recovery_steps>
55+If the operation fails:
56+1. Identify the first failing edit (start from top; test subsets to isolate).
57+2. View the file again and copy more surrounding context for that edit.
58+3. Recalculate later old_string values based on the file state AFTER preceding edits.
59+4. Reduce the batch (apply earlier stable edits first), then follow up with the rest.
60+</recovery_steps>
61+
3562<best_practices>
36−- Ensure all edits result in correct, idiomatic code
37−- Don't leave code in broken state
38−- Use absolute file paths (starting with /)
39−- Use replace_all for renaming variables across file
40−- Avoid adding emojis unless user explicitly requests
63+- Ensure all edits result in correct, idiomatic code; don’t leave code broken.
64+- Use absolute file paths (starting with /).
65+- Use replace_all only when you’re certain; otherwise provide unique context.
66+- Match existing style exactly (spaces, tabs, blank lines).
67+- Test after the operation; if it fails, fix and retry in smaller chunks.
4168</best_practices>
4269
43−<new_file_creation>
44−- Provide new file path (including directory if needed)
45−- First edit: empty old_string, new file contents as new_string
46−- Subsequent edits: normal edit operations on created content
47−</new_file_creation>
70+<whitespace_checklist>
71+For EACH edit, verify:
72+- [ ] Viewed the file first
73+- [ ] Counted indentation spaces/tabs
74+- [ ] Included blank lines if present
75+- [ ] Matched brace/bracket positioning
76+- [ ] Included 3–5 lines of surrounding context
77+- [ ] Verified text appears exactly once (or using replace_all deliberately)
78+- [ ] Copied text character-for-character, not approximated
79+</whitespace_checklist>
80+
81+<examples>
82+✅ Correct: Sequential edits where the second match accounts for the first change
83+
84+```
85+edits: [
86+ {
87+ old_string: "func A() {\n doOld()\n}",
88+ new_string: "func A() {\n doNew()\n}",
89+ },
90+ {
91+ // Uses context that still exists AFTER the first replacement
92+ old_string: "func B() {\n callA()\n}",
93+ new_string: "func B() {\n callA()\n logChange()\n}",
94+ },
95+]
96+```
97+
98+❌ Incorrect: Second old_string no longer matches due to whitespace change introduced by the first edit
99+
100+```
101+edits: [
102+ {
103+ old_string: "func A() {\n doOld()\n}",
104+ new_string: "func A() {\n\n doNew()\n}", // Added extra blank line
105+ },
106+ {
107+ old_string: "func A() {\n doNew()\n}", // Missing the new blank line, will FAIL
108+ new_string: "func A() {\n doNew()\n logChange()\n}",
109+ },
110+]
111+```
112+</examples>