xonecas.io
62159
Sy

symb / commit 2f2cde9

fix: edit tool usage/error messages

xonecas2026-02-15 2f2cde9b116f788cd20843b333b648cccc5ba12c parent 2bb4172

10 files changed

TODO.md+6−0
@@ -0,0 +1,6 @@
1+# TODO
2+
3+1. Review prompts, refactor prompt system message composition
4+2. Continue to test Edit tool, LLMs make errors with it.
5+3. Context token count: input, output, and total tokens
6+4. Simple statusbar with more info
docs/DESIGN.md+3−2
@@ -35,7 +35,8 @@ viewer. Input pane uses same component with `ReadOnly=false`.
3535 Streaming multi-turn. Up to 20 tool rounds per turn. Calls
3636 `Provider.ChatStream()` — returns `<-chan StreamEvent`. Forwards deltas via
3737 `OnDelta` callback for incremental TUI rendering, emits complete messages via
38-`OnMessage` callback.
38+`OnMessage` callback. Tool call limit is handled by forcing the llm to give a status
39+report.
3940
4041 Prompt system: model-specific base prompts (Claude, Gemini, Qwen, GPT).
4142 `AGENTS.md` support: walks directory tree upward collecting all AGENTS.md files,
@@ -46,7 +47,7 @@ prepends to system prompt. Checks `~/.config/symb/AGENTS.md` too.
4647 - <model name>-<segment name>.md: model specific instructions
4748 - base: common model instructions. and template tags to insert symb and model specific segments.
4849 - Ensure proper ordering of prompt
49- - Fix language, system prompt should be presented as model afnostic, the LLM shouldn't know anythign about other promots.
50+ - Fix language, system prompt should be presented as model afnostic, the LLM shouldn't know anythign about other promots. Should be software language agnostic (not Go focused, use pseudo-code or simple examples)
5051 - Consider token count and optimize where possible
5152
5253 ### Providers (`internal/provider`)
internal/hashline/hashline.go+18−1
@@ -65,6 +65,18 @@ func FormatTagged(tagged []TaggedLine) string {
6565 return b.String()
6666 }
6767
68+// HashMismatchError is returned when an anchor's hash doesn't match the actual file content.
69+type HashMismatchError struct {
70+ Line int
71+ Expected string
72+ Got string
73+ Content string
74+}
75+
76+func (e *HashMismatchError) Error() string {
77+ return fmt.Sprintf("hash mismatch at line %d: expected %s, got %s — actual: %q (re-Read the file to get fresh hashes)", e.Line, e.Expected, e.Got, e.Content)
78+}
79+
6880 // Anchor identifies a line by number and hash.
6981 type Anchor struct {
7082 Num int `json:"line"`
@@ -80,7 +92,12 @@ func (a Anchor) Validate(lines []string) error {
8092 }
8193 actual := LineHash(lines[idx])
8294 if actual != a.Hash {
83- return fmt.Errorf("hash mismatch at line %d: expected %s, got %s (file changed since last read?)", a.Num, a.Hash, actual)
95+ return &HashMismatchError{
96+ Line: a.Num,
97+ Expected: a.Hash,
98+ Got: actual,
99+ Content: lines[idx],
100+ }
84101 }
85102 return nil
86103 }
internal/hashline/hashline_test.go+14−2
@@ -1,6 +1,7 @@
11 package hashline
22
33 import (
4+ "strings"
45 "testing"
56 )
67
@@ -103,11 +104,22 @@ func TestAnchorValidate(t *testing.T) {
103104 t.Error("line 4 should be out of range")
104105 }
105106
106- // Wrong hash (file changed)
107+ // Wrong hash (file changed) — error should include actual line content
107108 a4 := Anchor{Num: 1, Hash: "ff"}
108- if err := a4.Validate(lines); err == nil {
109+ err := a4.Validate(lines)
110+ if err == nil {
109111 t.Error("wrong hash should fail validation")
110112 }
113+ errMsg := err.Error()
114+ if !strings.Contains(errMsg, "actual:") {
115+ t.Errorf("error should contain actual line content: %s", errMsg)
116+ }
117+ if !strings.Contains(errMsg, "func hello()") {
118+ t.Errorf("error should contain the line text: %s", errMsg)
119+ }
120+ if !strings.Contains(errMsg, "re-Read") {
121+ t.Errorf("error should suggest re-reading: %s", errMsg)
122+ }
111123 }
112124
113125 func TestValidateRange(t *testing.T) {
internal/llm/anthropic.md+1−1
@@ -102,7 +102,7 @@ One operation per call. After each edit, you get back the updated file with fres
102102 {"file": "f.go", "delete": {"start": {"line": 5, "hash": "9f"}, "end": {"line": 7, "hash": "d4"}}}
103103 ```
104104
105-**Create** a new file (fails if it exists):
105+**Create** a new file (fails if it exists). Note: `create` is an object with a `content` key, not a bare string:
106106 ```json
107107 {"file": "new.go", "create": {"content": "package main\n"}}
108108 ```
internal/llm/gemini.md+1−1
@@ -106,7 +106,7 @@ One operation per call. Returns updated file with fresh hashes after each edit.
106106 - **replace**: `{"file": "f.go", "replace": {"start": {"line": 5, "hash": "9f"}, "end": {"line": 7, "hash": "d4"}, "content": "new code"}}`
107107 - **insert**: `{"file": "f.go", "insert": {"after": {"line": 3, "hash": "b2"}, "content": "new line"}}`
108108 - **delete**: `{"file": "f.go", "delete": {"start": {"line": 5, "hash": "9f"}, "end": {"line": 7, "hash": "d4"}}}`
109-- **create**: `{"file": "new.go", "create": {"content": "package main\n"}}`
109+- **create** (note: object with `content` key, not a bare string): `{"file": "new.go", "create": {"content": "package main\n"}}`
110110
111111 **Critical rules:**
112112 - If a hash doesn't match, the file changed — re-Read and retry
internal/llm/gpt.md+1−1
@@ -119,7 +119,7 @@ One operation per call. Returns updated file with fresh hashes after each edit.
119119 {"file": "f.go", "delete": {"start": {"line": 5, "hash": "9f"}, "end": {"line": 7, "hash": "d4"}}}
120120 ```
121121
122-**Create** a new file:
122+**Create** a new file. Note: `create` is an object with a `content` key, not a bare string:
123123 ```json
124124 {"file": "new.go", "create": {"content": "package main\n"}}
125125 ```
internal/llm/qwen.md+2−1
@@ -36,6 +36,7 @@ Returns **hashline-tagged** content. Each line as `linenum:hash|content`:
3636 1:e3|package main
3737 2:6a|
3838 3:b2|import "fmt"
39+4:6a|
3940 5:9f|func main() {
4041 6:c1| fmt.Println("hello")
4142 7:d4|}
@@ -80,7 +81,7 @@ Use for: builds, tests, linters, git, file inspection. Default timeout: 60s.
8081 - **replace**: `{"file": "f.go", "replace": {"start": {"line": 5, "hash": "9f"}, "end": {"line": 7, "hash": "d4"}, "content": "new code"}}`
8182 - **insert**: `{"file": "f.go", "insert": {"after": {"line": 3, "hash": "b2"}, "content": "new line"}}`
8283 - **delete**: `{"file": "f.go", "delete": {"start": {"line": 5, "hash": "9f"}, "end": {"line": 7, "hash": "d4"}}}`
83-- **create**: `{"file": "new.go", "create": {"content": "package main\n"}}`
84+- **create** (note: object with `content` key, not a bare string): `{"file": "new.go", "create": {"content": "package main\n"}}`
8485
8586 Hash mismatch = file changed since read → re-Read and retry. Use fresh hashes for chained edits.
8687
internal/mcptools/edit.go+94−17
@@ -16,6 +16,19 @@ import (
1616 "github.com/xonecas/symb/internal/treesitter"
1717 )
1818
19+const (
20+ // windowThreshold is the minimum line count to trigger windowed output.
21+ windowThreshold = 50
22+ // windowContext is the number of lines shown above/below the edit region.
23+ windowContext = 20
24+)
25+
26+// editRegion describes which lines (1-indexed, inclusive) were affected in the new file.
27+type editRegion struct {
28+ start int // first affected line
29+ end int // last affected line
30+}
31+
1932 // EditArgs represents arguments for the Edit tool.
2033 // Exactly one of the operation fields (Replace, Insert, Delete) must be set.
2134 type EditArgs struct {
@@ -128,7 +141,7 @@ func (h *EditHandler) SetTSIndex(idx *treesitter.Index) { h.tsIndex = idx }
128141 func (h *EditHandler) Handle(ctx context.Context, arguments json.RawMessage) (*mcp.ToolResult, error) {
129142 var args EditArgs
130143 if err := json.Unmarshal(arguments, &args); err != nil {
131- return toolError("Invalid arguments: %v", err), nil
144+ return toolError("%s", editUnmarshalHint(arguments, err)), nil
132145 }
133146 if args.File == "" {
134147 return toolError("File path cannot be empty"), nil
@@ -174,6 +187,28 @@ func validateEditOps(args EditArgs) error {
174187 return nil
175188 }
176189
190+// editUnmarshalHint returns an actionable error when JSON unmarshalling fails.
191+// It detects common mistakes like passing a string for create instead of an object.
192+func editUnmarshalHint(raw json.RawMessage, original error) string {
193+ var probe map[string]json.RawMessage
194+ if json.Unmarshal(raw, &probe) == nil {
195+ for _, key := range []string{"create", "replace", "insert", "delete"} {
196+ v, ok := probe[key]
197+ if !ok || len(v) == 0 {
198+ continue
199+ }
200+ // If the value starts with '"' it's a string, but the schema expects an object.
201+ if v[0] == '"' {
202+ return fmt.Sprintf(
203+ `invalid %s: expected an object, got a string. Use {"file":"path", "%s":{"content":"..."}}`,
204+ key, key,
205+ )
206+ }
207+ }
208+ }
209+ return fmt.Sprintf("invalid arguments: %v", original)
210+}
211+
177212 // applyEdit reads the file, applies the edit operation, writes it back, and returns fresh hashes.
178213 func (h *EditHandler) applyEdit(ctx context.Context, absPath string, args EditArgs) (*mcp.ToolResult, error) {
179214 content, err := os.ReadFile(absPath)
@@ -183,13 +218,14 @@ func (h *EditHandler) applyEdit(ctx context.Context, absPath string, args EditAr
183218 lines := strings.Split(string(content), "\n")
184219
185220 var result string
221+ var region editRegion
186222 switch {
187223 case args.Replace != nil:
188- result, err = applyReplace(lines, args.Replace)
224+ result, region, err = applyReplace(lines, args.Replace)
189225 case args.Insert != nil:
190- result, err = applyInsert(lines, args.Insert)
226+ result, region, err = applyInsert(lines, args.Insert)
191227 case args.Delete != nil:
192- result, err = applyDelete(lines, args.Delete)
228+ result, region, err = applyDelete(lines, args.Delete)
193229 }
194230 if err != nil {
195231 return toolError("%v", err), nil
@@ -204,7 +240,7 @@ func (h *EditHandler) applyEdit(ctx context.Context, absPath string, args EditAr
204240 }
205241
206242 tagged := hashline.TagLines(result, 1)
207- text := fmt.Sprintf("Edited %s (%d lines):\n\n%s", args.File, len(tagged), hashline.FormatTagged(tagged))
243+ text := formatEditResponse(args.File, tagged, region)
208244
209245 if h.lspManager != nil {
210246 diags := h.lspManager.NotifyAndWait(ctx, absPath, 5*time.Second)
@@ -258,40 +294,81 @@ func (h *EditHandler) handleCreate(ctx context.Context, absPath, displayPath str
258294 }, nil
259295 }
260296
261-func applyReplace(lines []string, op *ReplaceOp) (string, error) {
297+// formatEditResponse builds the response text, using windowed output for large files.
298+func formatEditResponse(displayPath string, tagged []hashline.TaggedLine, region editRegion) string {
299+ total := len(tagged)
300+ if total <= windowThreshold {
301+ return fmt.Sprintf("Edited %s (%d lines):\n\n%s", displayPath, total, hashline.FormatTagged(tagged))
302+ }
303+
304+ // Clamp window bounds.
305+ winStart := region.start - windowContext
306+ if winStart < 1 {
307+ winStart = 1
308+ }
309+ winEnd := region.end + windowContext
310+ if winEnd > total {
311+ winEnd = total
312+ }
313+
314+ window := tagged[winStart-1 : winEnd] // tagged is 0-indexed, line nums are 1-indexed
315+ return fmt.Sprintf("Edited %s (%d lines, showing %d–%d):\n\n%s",
316+ displayPath, total, winStart, winEnd, hashline.FormatTagged(window))
317+}
318+
319+func applyReplace(lines []string, op *ReplaceOp) (string, editRegion, error) {
262320 if err := hashline.ValidateRange(op.Start, op.End, lines); err != nil {
263- return "", fmt.Errorf("replace: %w", err)
321+ return "", editRegion{}, fmt.Errorf("replace: %w", err)
264322 }
265323
324+ inserted := strings.Split(op.Content, "\n")
266325 newLines := make([]string, 0, len(lines))
267326 newLines = append(newLines, lines[:op.Start.Num-1]...)
268- newLines = append(newLines, strings.Split(op.Content, "\n")...)
327+ newLines = append(newLines, inserted...)
269328 newLines = append(newLines, lines[op.End.Num:]...)
270329
271- return strings.Join(newLines, "\n"), nil
330+ region := editRegion{
331+ start: op.Start.Num,
332+ end: op.Start.Num + len(inserted) - 1,
333+ }
334+ return strings.Join(newLines, "\n"), region, nil
272335 }
273336
274-func applyInsert(lines []string, op *InsertOp) (string, error) {
337+func applyInsert(lines []string, op *InsertOp) (string, editRegion, error) {
275338 if err := op.After.Validate(lines); err != nil {
276- return "", fmt.Errorf("insert: after anchor: %w", err)
339+ return "", editRegion{}, fmt.Errorf("insert: after anchor: %w", err)
277340 }
278341
279- newLines := make([]string, 0, len(lines)+1)
342+ inserted := strings.Split(op.Content, "\n")
343+ newLines := make([]string, 0, len(lines)+len(inserted))
280344 newLines = append(newLines, lines[:op.After.Num]...)
281- newLines = append(newLines, strings.Split(op.Content, "\n")...)
345+ newLines = append(newLines, inserted...)
282346 newLines = append(newLines, lines[op.After.Num:]...)
283347
284- return strings.Join(newLines, "\n"), nil
348+ region := editRegion{
349+ start: op.After.Num + 1,
350+ end: op.After.Num + len(inserted),
351+ }
352+ return strings.Join(newLines, "\n"), region, nil
285353 }
286354
287-func applyDelete(lines []string, op *DeleteOp) (string, error) {
355+func applyDelete(lines []string, op *DeleteOp) (string, editRegion, error) {
288356 if err := hashline.ValidateRange(op.Start, op.End, lines); err != nil {
289- return "", fmt.Errorf("delete: %w", err)
357+ return "", editRegion{}, fmt.Errorf("delete: %w", err)
290358 }
291359
292360 newLines := make([]string, 0, len(lines))
293361 newLines = append(newLines, lines[:op.Start.Num-1]...)
294362 newLines = append(newLines, lines[op.End.Num:]...)
295363
296- return strings.Join(newLines, "\n"), nil
364+ // The "region" is where the deletion happened — show surrounding context.
365+ regionLine := op.Start.Num - 1
366+ if regionLine < 1 {
367+ regionLine = 1
368+ }
369+ region := editRegion{
370+ start: regionLine,
371+ end: regionLine,
372+ }
373+ return strings.Join(newLines, "\n"), region, nil
297374 }
internal/mcptools/edit_test.go+120−2
@@ -3,6 +3,7 @@ package mcptools
33 import (
44 "context"
55 "encoding/json"
6+ "fmt"
67 "os"
78 "path/filepath"
89 "strings"
@@ -11,6 +12,8 @@ import (
1112 "github.com/xonecas/symb/internal/hashline"
1213 )
1314
15+const threeLineContent = "line one\nline two\nline three"
16+
1417 // setupTestFile creates a temp file with the given content and returns its path and cleanup func.
1518 func setupTestFile(t *testing.T, content string) (string, func()) {
1619 t.Helper()
@@ -94,7 +97,7 @@ func TestEditReplace(t *testing.T) {
9497 }
9598
9699 func TestEditInsert(t *testing.T) {
97- content := "line one\nline two\nline three"
100+ content := threeLineContent
98101 path, cleanup := setupTestFile(t, content)
99102 defer cleanup()
100103
@@ -196,7 +199,7 @@ func TestEditCreateFailsIfExists(t *testing.T) {
196199 }
197200
198201 func TestEditHashMismatch(t *testing.T) {
199- content := "line one\nline two\nline three"
202+ content := threeLineContent
200203 path, cleanup := setupTestFile(t, content)
201204 defer cleanup()
202205
@@ -395,3 +398,118 @@ func TestEditCreateBypassesReadCheck(t *testing.T) {
395398 t.Fatal("create should not require prior read")
396399 }
397400 }
401+
402+func TestEditWindowedResponse(t *testing.T) {
403+ // Build a file with >50 lines so windowed output kicks in.
404+ var b strings.Builder
405+ totalLines := 80
406+ for i := 1; i <= totalLines; i++ {
407+ if i > 1 {
408+ b.WriteByte('\n')
409+ }
410+ fmt.Fprintf(&b, "line %d", i)
411+ }
412+ content := b.String()
413+
414+ path, cleanup := setupTestFile(t, content)
415+ defer cleanup()
416+
417+ lines := strings.Split(content, "\n")
418+ // Replace line 40
419+ h40 := hashline.LineHash(lines[39])
420+
421+ handler := newTrackedHandler(t, path)
422+ text, isErr := callEdit(t, handler, EditArgs{
423+ File: filepath.Base(path),
424+ Replace: &ReplaceOp{
425+ Start: hashline.Anchor{Num: 40, Hash: h40},
426+ End: hashline.Anchor{Num: 40, Hash: h40},
427+ Content: "REPLACED LINE 40",
428+ },
429+ })
430+
431+ if isErr {
432+ t.Fatalf("windowed replace failed: %s", text)
433+ }
434+
435+ // Should show windowed output (±20 around line 40 → lines 20–60)
436+ if !strings.Contains(text, "showing") {
437+ t.Errorf("large file should trigger windowed output: %s", text)
438+ }
439+
440+ // Should NOT contain line 1 (outside window)
441+ if strings.Contains(text, "|line 1\n") {
442+ t.Errorf("windowed output should not contain line 1")
443+ }
444+
445+ // Should contain the replaced line
446+ if !strings.Contains(text, "REPLACED LINE 40") {
447+ t.Errorf("windowed output should contain replaced content")
448+ }
449+
450+ // File on disk should be complete
451+ got, _ := os.ReadFile(path)
452+ if !strings.Contains(string(got), "line 1") {
453+ t.Error("full file should still have line 1")
454+ }
455+ if !strings.Contains(string(got), "REPLACED LINE 40") {
456+ t.Error("full file should have replaced content")
457+ }
458+}
459+
460+func TestEditSmallFileFullResponse(t *testing.T) {
461+ // Files with <=50 lines should get full output (no "showing" header).
462+ content := threeLineContent
463+ path, cleanup := setupTestFile(t, content)
464+ defer cleanup()
465+
466+ lines := strings.Split(content, "\n")
467+ h1 := hashline.LineHash(lines[0])
468+
469+ handler := newTrackedHandler(t, path)
470+ text, isErr := callEdit(t, handler, EditArgs{
471+ File: filepath.Base(path),
472+ Replace: &ReplaceOp{
473+ Start: hashline.Anchor{Num: 1, Hash: h1},
474+ End: hashline.Anchor{Num: 1, Hash: h1},
475+ Content: "REPLACED",
476+ },
477+ })
478+
479+ if isErr {
480+ t.Fatalf("small file replace failed: %s", text)
481+ }
482+
483+ if strings.Contains(text, "showing") {
484+ t.Errorf("small file should NOT trigger windowed output: %s", text)
485+ }
486+}
487+
488+func TestEditCreateStringGivesHint(t *testing.T) {
489+ dir := t.TempDir()
490+ origDir, _ := os.Getwd()
491+ if err := os.Chdir(dir); err != nil {
492+ t.Fatalf("chdir: %v", err)
493+ }
494+ defer os.Chdir(origDir) //nolint:errcheck
495+
496+ tracker := NewFileReadTracker()
497+ handler := NewEditHandler(tracker, nil, nil)
498+
499+ // Simulate the LLM mistake: {"file":"TODO.md", "create":"some content"}
500+ raw := json.RawMessage(`{"file":"TODO.md","create":"some content"}`)
501+ result, err := handler.Handle(context.Background(), raw)
502+ if err != nil {
503+ t.Fatalf("handler error: %v", err)
504+ }
505+ if !result.IsError {
506+ t.Fatal("should fail with string create")
507+ }
508+ text := result.Content[0].Text
509+ if !strings.Contains(text, "expected an object") {
510+ t.Errorf("should hint about object vs string: %s", text)
511+ }
512+ if !strings.Contains(text, `"create":{"content"`) {
513+ t.Errorf("should show correct format: %s", text)
514+ }
515+}