xonecas.io
62159
Sy

symb / commit d587e7f

feat: attempt self correcting edits based on hash

xonecas2026-02-15 d587e7ff5b2245db9e0883ed6a61ff889ae7e5c0 parent 84ef25d

7 files changed

internal/hashline/hashline.go+37−10
@@ -86,25 +86,52 @@ type Anchor struct {
8686
8787 // Validate checks that the anchor matches the actual file lines.
8888 // lines is 0-indexed; anchor.Num is 1-indexed.
89-func (a Anchor) Validate(lines []string) error {
89+// On hash mismatch, it attempts to relocate by scanning for a unique
90+// line with the expected hash. If found, a.Num is updated in place.
91+func (a *Anchor) Validate(lines []string) error {
9092 idx := a.Num - 1
93+ if idx >= 0 && idx < len(lines) && LineHash(lines[idx]) == a.Hash {
94+ return nil
95+ }
9196 if idx < 0 || idx >= len(lines) {
97+ // Out of range — still try relocation before giving up.
98+ if n, ok := relocate(lines, a.Hash); ok {
99+ a.Num = n + 1
100+ return nil
101+ }
92102 return fmt.Errorf("line %d out of range (file has %d lines)", a.Num, len(lines))
93103 }
94- actual := LineHash(lines[idx])
95- if actual != a.Hash {
96- return &HashMismatchError{
97- Line: a.Num,
98- Expected: a.Hash,
99- Got: actual,
100- Content: lines[idx],
104+ // Hash mismatch — try relocation.
105+ if n, ok := relocate(lines, a.Hash); ok {
106+ a.Num = n + 1
107+ return nil
108+ }
109+ return &HashMismatchError{
110+ Line: a.Num,
111+ Expected: a.Hash,
112+ Got: LineHash(lines[idx]),
113+ Content: lines[idx],
114+ }
115+}
116+
117+// relocate scans lines for a unique match of hash.
118+// Returns the 0-indexed line number if exactly one match is found.
119+func relocate(lines []string, hash string) (int, bool) {
120+ found := -1
121+ for i, l := range lines {
122+ if LineHash(l) == hash {
123+ if found >= 0 {
124+ return 0, false // duplicate — ambiguous
125+ }
126+ found = i
101127 }
102128 }
103- return nil
129+ return found, found >= 0
104130 }
105131
106132 // ValidateRange checks that start and end anchors are valid and ordered.
107-func ValidateRange(start, end Anchor, lines []string) error {
133+// Anchors may be relocated in place on hash mismatch.
134+func ValidateRange(lines []string, start, end *Anchor) error {
108135 if err := start.Validate(lines); err != nil {
109136 return fmt.Errorf("start anchor: %w", err)
110137 }
internal/hashline/hashline_test.go+64−5
@@ -129,27 +129,32 @@ func TestValidateRange(t *testing.T) {
129129 h3 := LineHash(lines[2])
130130
131131 // Valid range
132- if err := ValidateRange(Anchor{1, h1}, Anchor{3, h3}, lines); err != nil {
132+ s, e := Anchor{1, h1}, Anchor{3, h3}
133+ if err := ValidateRange(lines, &s, &e); err != nil {
133134 t.Errorf("valid range failed: %v", err)
134135 }
135136
136137 // Single line range
137- if err := ValidateRange(Anchor{2, h2}, Anchor{2, h2}, lines); err != nil {
138+ s2, e2 := Anchor{2, h2}, Anchor{2, h2}
139+ if err := ValidateRange(lines, &s2, &e2); err != nil {
138140 t.Errorf("single line range failed: %v", err)
139141 }
140142
141143 // Inverted range
142- if err := ValidateRange(Anchor{3, h3}, Anchor{1, h1}, lines); err == nil {
144+ s3, e3 := Anchor{3, h3}, Anchor{1, h1}
145+ if err := ValidateRange(lines, &s3, &e3); err == nil {
143146 t.Error("inverted range should fail")
144147 }
145148
146149 // Bad start hash
147- if err := ValidateRange(Anchor{1, "ff"}, Anchor{3, h3}, lines); err == nil {
150+ s4, e4 := Anchor{1, "ff"}, Anchor{3, h3}
151+ if err := ValidateRange(lines, &s4, &e4); err == nil {
148152 t.Error("bad start hash should fail")
149153 }
150154
151155 // Bad end hash
152- if err := ValidateRange(Anchor{1, h1}, Anchor{3, "ff"}, lines); err == nil {
156+ s5, e5 := Anchor{1, h1}, Anchor{3, "ff"}
157+ if err := ValidateRange(lines, &s5, &e5); err == nil {
153158 t.Error("bad end hash should fail")
154159 }
155160 }
@@ -189,3 +194,57 @@ func TestParseAnchor(t *testing.T) {
189194 }
190195 }
191196 }
197+
198+func TestAnchorRelocate(t *testing.T) {
199+ lines := []string{"alpha", "beta", "gamma", "delta"}
200+ betaHash := LineHash("beta")
201+
202+ // Wrong line number, unique hash → relocate succeeds.
203+ a := Anchor{Num: 4, Hash: betaHash}
204+ if err := a.Validate(lines); err != nil {
205+ t.Fatalf("expected relocation, got error: %v", err)
206+ }
207+ if a.Num != 2 {
208+ t.Errorf("expected relocated to line 2, got %d", a.Num)
209+ }
210+
211+ // Duplicate hash → relocation fails.
212+ dupes := []string{"same", "other", "same"}
213+ sameHash := LineHash("same")
214+ a2 := Anchor{Num: 2, Hash: sameHash}
215+ if err := a2.Validate(dupes); err == nil {
216+ t.Error("expected error for ambiguous hash")
217+ }
218+
219+ // Out of range but relocatable.
220+ a3 := Anchor{Num: 99, Hash: betaHash}
221+ if err := a3.Validate(lines); err != nil {
222+ t.Fatalf("expected relocation for out-of-range, got: %v", err)
223+ }
224+ if a3.Num != 2 {
225+ t.Errorf("expected relocated to line 2, got %d", a3.Num)
226+ }
227+}
228+
229+func TestValidateRangeRelocate(t *testing.T) {
230+ lines := []string{"alpha", "beta", "gamma", "delta"}
231+ betaHash := LineHash("beta")
232+ gammaHash := LineHash("gamma")
233+
234+ // Both anchors have wrong line numbers but unique hashes.
235+ start := Anchor{Num: 10, Hash: betaHash}
236+ end := Anchor{Num: 11, Hash: gammaHash}
237+ if err := ValidateRange(lines, &start, &end); err != nil {
238+ t.Fatalf("expected range relocation, got: %v", err)
239+ }
240+ if start.Num != 2 || end.Num != 3 {
241+ t.Errorf("expected 2..3, got %d..%d", start.Num, end.Num)
242+ }
243+
244+ // Relocated range inverted → error.
245+ s2 := Anchor{Num: 10, Hash: gammaHash}
246+ e2 := Anchor{Num: 11, Hash: betaHash}
247+ if err := ValidateRange(lines, &s2, &e2); err == nil {
248+ t.Error("expected error for inverted relocated range")
249+ }
250+}
internal/mcptools/edit.go+2−2
@@ -226,7 +226,7 @@ func applyReplace(lines []string, args EditArgs) (string, editRegion, error) {
226226 if err != nil {
227227 return "", editRegion{}, fmt.Errorf("replace end: %w", err)
228228 }
229- if err := hashline.ValidateRange(start, end, lines); err != nil {
229+ if err := hashline.ValidateRange(lines, &start, &end); err != nil {
230230 return "", editRegion{}, fmt.Errorf("replace: %w", err)
231231 }
232232
@@ -274,7 +274,7 @@ func applyDelete(lines []string, args EditArgs) (string, editRegion, error) {
274274 if err != nil {
275275 return "", editRegion{}, fmt.Errorf("delete end: %w", err)
276276 }
277- if err := hashline.ValidateRange(start, end, lines); err != nil {
277+ if err := hashline.ValidateRange(lines, &start, &end); err != nil {
278278 return "", editRegion{}, fmt.Errorf("delete: %w", err)
279279 }
280280
internal/tui/editor/editor.go+1−6
@@ -9,7 +9,6 @@ import (
99 "strings"
1010
1111 "charm.land/bubbles/v2/cursor"
12- tea "charm.land/bubbletea/v2"
1312 "charm.land/lipgloss/v2"
1413 "github.com/xonecas/symb/internal/highlight"
1514 )
@@ -102,7 +101,7 @@ func (s selection) empty() bool {
102101 // New creates a new editor with sensible defaults.
103102 func New() Model {
104103 c := cursor.New()
105- c.SetMode(cursor.CursorBlink)
104+ c.SetMode(cursor.CursorStatic)
106105 return Model{
107106 lines: [][]rune{{}},
108107 cursor: c,
@@ -174,10 +173,6 @@ func (m *Model) GotoLine(line int) {
174173 m.col = 0
175174 m.clampScroll()
176175 }
177-
178-// Blink returns the initial cursor blink message. Call from Init().
179-func Blink() tea.Msg { return cursor.Blink() }
180-
181176 // ---------------------------------------------------------------------------
182177 // Selection API (called by parent)
183178 // ---------------------------------------------------------------------------
internal/tui/editor/update.go+0−1
@@ -15,7 +15,6 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
1515 if moved := m.handleKeyPress(msg); moved {
1616 m.clampCursor()
1717 m.clampScroll()
18- cmds = append(cmds, m.cursor.Blink())
1918 }
2019 }
2120 case tea.MouseClickMsg, tea.MouseMotionMsg, tea.MouseReleaseMsg, tea.MouseWheelMsg:
internal/tui/editor/view.go+1−0
@@ -410,6 +410,7 @@ func (m Model) placeholderView() string {
410410 phRunes := []rune(m.Placeholder)
411411 m.cursor.SetChar(string(phRunes[0]))
412412 m.cursor.TextStyle = m.PlaceholderSty
413+ m.cursor.Style = m.CursorStyle
413414 b.WriteString(m.cursor.View())
414415 rest := m.PlaceholderSty.Render(string(phRunes[1:]))
415416 rw := lipgloss.Width(m.cursor.View()) + lipgloss.Width(rest)
internal/tui/tui.go+3−3
@@ -296,7 +296,7 @@ func New(prov provider.Provider, proxy *mcp.Proxy, tools []mcp.Tool, modelID str
296296 ed.DiagWarnStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#e5c07b"))
297297
298298 ai := editor.New()
299- ai.Placeholder = "Type a message..."
299+ ai.Placeholder = "What's our task?"
300300 ai.SubmitOnEnter = true
301301 ai.Language = "markdown"
302302 ai.SyntaxTheme = constants.SyntaxTheme
@@ -356,7 +356,7 @@ func newSearcherOrNil(root string) *filesearch.Searcher {
356356 return s
357357 }
358358
359-// Init starts cursor blink, the 60fps frame loop, and periodic git branch polling.
359+// Init starts the 60fps frame loop and periodic git branch polling.
360360 func (m Model) Init() tea.Cmd {
361- return tea.Batch(func() tea.Msg { return editor.Blink() }, frameTick(), gitBranchCmd())
361+ return tea.Batch(frameTick(), gitBranchCmd())
362362 }