xonecas.io
62159
Sy

symb / commit 1b83154

feat: compact past tool results

xonecas2026-02-15 1b83154c3554d41398ef0108758938ff7f495e0f parent 8b1a606

8 files changed

docs/DESIGN.md+3−0
@@ -127,6 +127,9 @@ Needs an undo.
127127 Refactor so it shows two lines as above with text right aligned. Remove existing `---` separator completly
128128 **DONE**
129129
130+- Read tool caps output at 500 lines / 20k chars; tells LLM to use line ranges for larger files.
131+- Old tool results are compacted to their header line after the assistant responds, preventing resend of full payloads on subsequent rounds.
132+
130133 2. Gather baseline data with no context management. Then study:
131134 **INPROGRESS**
132135
internal/constants/constants.go+1−1
@@ -28,4 +28,4 @@ package constants
2828 // Light themes:
2929 // - tokyonight-day #e1e2e7 81 entries
3030 // - catppuccin-latte #eff1f5 81 entries
31-const SyntaxTheme = "aura-theme-dark"
31+const SyntaxTheme = "github-dark"
internal/llm/loop.go+60−0
@@ -5,6 +5,7 @@ import (
55 "context"
66 "encoding/json"
77 "fmt"
8+ "strings"
89 "time"
910
1011 "github.com/xonecas/symb/internal/mcp"
@@ -88,6 +89,13 @@ func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
8889 }
8990
9091 for round := 0; round < opts.MaxToolRounds; round++ {
92+ // After round 0 the history contains tool results that the LLM
93+ // has already responded to. Compact them so we don't resend
94+ // large payloads on every subsequent call.
95+ if round > 0 {
96+ compactOldToolResults(opts.History)
97+ }
98+
9199 resp, err := streamAndCollect(ctx, &opts, providerTools)
92100 if err != nil {
93101 return fmt.Errorf("LLM stream failed: %w", err)
@@ -271,6 +279,58 @@ func executeToolCalls(ctx context.Context, proxy *mcp.Proxy, toolCalls []provide
271279 return toolResults
272280 }
273281
282+// maxToolResultLen is the character threshold above which a tool result is
283+// compacted once the LLM has already processed it.
284+const maxToolResultLen = 200
285+
286+// compactOldToolResults replaces verbose tool-result messages that the LLM
287+// has already responded to with a short summary. A tool result is considered
288+// "processed" when an assistant message appears after it in the history.
289+//
290+// This reduces tokens sent to the provider during multi-round tool-calling
291+// turns. It operates on the local history copy inside ProcessTurn — the
292+// TUI's m.history is unaffected.
293+func compactOldToolResults(history []provider.Message) {
294+ // Find the index of the last assistant message — everything before it
295+ // has been seen by the LLM.
296+ lastAssistant := -1
297+ for i := len(history) - 1; i >= 0; i-- {
298+ if history[i].Role == "assistant" {
299+ lastAssistant = i
300+ break
301+ }
302+ }
303+ if lastAssistant < 0 {
304+ return
305+ }
306+
307+ for i := 0; i < lastAssistant; i++ {
308+ if history[i].Role != "tool" {
309+ continue
310+ }
311+ if len(history[i].Content) <= maxToolResultLen {
312+ continue
313+ }
314+ history[i].Content = trimToolResult(history[i].Content)
315+ }
316+}
317+
318+// trimToolResult keeps the first line (usually a header like "Read file.go (200 lines):")
319+// plus a truncation notice. This preserves enough context for the LLM to know what
320+// tool was called and what it returned, without the full payload.
321+func trimToolResult(content string) string {
322+ firstNL := strings.Index(content, "\n")
323+ if firstNL < 0 || firstNL > maxToolResultLen {
324+ // No newline or very long first line — hard truncate.
325+ r := []rune(content)
326+ if len(r) > maxToolResultLen {
327+ return string(r[:maxToolResultLen]) + "\n[Truncated — already processed by assistant]"
328+ }
329+ return content
330+ }
331+ return content[:firstNL] + "\n[Truncated — already processed by assistant]"
332+}
333+
274334 // extractTextFromContent extracts text from MCP content blocks.
275335 func extractTextFromContent(content []mcp.ContentBlock) string {
276336 var text string
internal/llm/prompts/base.md+2−0
@@ -48,6 +48,8 @@ The 2-char hex hash is a content fingerprint for that line. You need both the li
4848 - `{"file": "main.go"}` — read entire file
4949 - `{"file": "main.go", "start": 50, "end": 100}` — read line range
5050
51+Output is capped at 500 lines / 20k characters. For large files, **always use `start`/`end`** to read the section you need. Reading whole large files wastes context.
52+
5153 **You MUST Read a file before editing it.** Edit will reject changes to files you haven't read.
5254
5355 ### `Grep` — Search files or content
internal/mcptools/open.go+28−1
@@ -14,6 +14,11 @@ import (
1414 "github.com/xonecas/symb/internal/treesitter"
1515 )
1616
17+const (
18+ maxReadLines = 500 // Max lines returned by Read before truncation.
19+ maxReadChars = 20000 // Max characters returned by Read before truncation.
20+)
21+
1722 // ReadArgs represents arguments for the Read tool.
1823 type ReadArgs struct {
1924 File string `json:"file"`
@@ -88,8 +93,25 @@ func (h *ReadHandler) Handle(_ context.Context, arguments json.RawMessage) (*mcp
8893 }
8994
9095 tagged := hashline.TagLines(selectedContent, startLine)
96+
97+ // Cap output to avoid blowing up context. When the file is too large,
98+ // return only the first maxReadLines and tell the LLM to use line ranges.
99+ truncatedRead := false
100+ totalLines := len(tagged)
101+ if len(tagged) > maxReadLines {
102+ tagged = tagged[:maxReadLines]
103+ truncatedRead = true
104+ }
105+
91106 taggedOutput := hashline.FormatTagged(tagged)
92107
108+ // Secondary cap on raw character count (handles very long lines).
109+ // Use []rune to avoid splitting multi-byte UTF-8 sequences.
110+ if runes := []rune(taggedOutput); len(runes) > maxReadChars {
111+ taggedOutput = string(runes[:maxReadChars]) + "\n[Truncated — output exceeded character limit]"
112+ truncatedRead = true
113+ }
114+
93115 rangeInfo := ""
94116 if args.Start > 0 || args.End > 0 {
95117 end := args.End
@@ -99,8 +121,13 @@ func (h *ReadHandler) Handle(_ context.Context, arguments json.RawMessage) (*mcp
99121 rangeInfo = fmt.Sprintf(" (lines %d-%d)", startLine, end)
100122 }
101123
124+ header := fmt.Sprintf("Read %s%s (%d lines):\n\n%s", args.File, rangeInfo, len(tagged), taggedOutput)
125+ if truncatedRead {
126+ header += fmt.Sprintf("\n\n[Showing %d of %d lines. Use start/end parameters to read specific sections.]", len(tagged), totalLines)
127+ }
128+
102129 return &mcp.ToolResult{
103- Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Read %s%s (%d lines):\n\n%s", args.File, rangeInfo, len(tagged), taggedOutput)}},
130+ Content: []mcp.ContentBlock{{Type: "text", Text: header}},
104131 }, nil
105132 }
106133
internal/tui/messages.go+3−2
@@ -153,7 +153,7 @@ func (m Model) processLLM() tea.Cmd {
153153 history := make([]provider.Message, len(m.history))
154154 copy(history, m.history)
155155 ch := m.updateChan
156- ctx := m.ctx
156+ ctx := m.turnCtx
157157 dt := m.deltaTracker
158158
159159 return func() tea.Msg {
@@ -209,7 +209,8 @@ func (m Model) processLLM() tea.Cmd {
209209 })
210210
211211 // Post-turn snapshot: diff against pre to record deltas for undo.
212- if preSnap != nil {
212+ // Skip on cancellation — partial work has no turnBoundary.
213+ if preSnap != nil && err == nil {
213214 postSnap := delta.SnapshotDir(snapRoot)
214215 delta.RecordDeltas(dt, snapRoot, preSnap, postSnap)
215216 }
internal/tui/tui.go+2−0
@@ -200,6 +200,8 @@ type Model struct {
200200 updateChan chan tea.Msg
201201 ctx context.Context
202202 cancel context.CancelFunc
203+ turnCtx context.Context // per-turn child context (nil when idle)
204+ turnCancel context.CancelFunc // cancels current LLM turn only (nil when idle)
203205
204206 // Session persistence
205207 store *store.Cache
internal/tui/update.go+49−2
@@ -1,6 +1,7 @@
11 package tui
22
33 import (
4+ "context"
45 "encoding/json"
56 "fmt"
67 "sort"
@@ -57,7 +58,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5758 // -- LLM user message (sent before streaming begins) ---------------------
5859 case llmUserMsg:
5960 m.llmInFlight = true
60- return m.handleUserMsg(msg), tea.Batch(m.processLLM(), m.waitForLLMUpdate())
61+ turnCtx, turnCancel := context.WithCancel(m.ctx)
62+ m.turnCancel = turnCancel
63+ m.turnCtx = turnCtx
64+ mdl := m.handleUserMsg(msg)
65+ return mdl, tea.Batch(mdl.processLLM(), mdl.waitForLLMUpdate())
6166
6267 case LSPDiagnosticsMsg:
6368 return m.handleLSPDiag(msg), nil
@@ -165,6 +170,10 @@ func (m *Model) handleKeyPress(msg tea.KeyPressMsg) (Model, tea.Cmd, bool) {
165170 case "ctrl+shift+v":
166171 return *m, tea.ReadClipboard, true
167172 case "esc":
173+ if m.llmInFlight {
174+ m.cancelTurn()
175+ return *m, m.waitForLLMUpdate(), true
176+ }
168177 if m.focus == focusInput {
169178 m.agentInput.Blur()
170179 } else {
@@ -172,7 +181,7 @@ func (m *Model) handleKeyPress(msg tea.KeyPressMsg) (Model, tea.Cmd, bool) {
172181 }
173182 return *m, nil, true
174183 case "enter":
175- if m.focus == focusInput && m.agentInput.Value() != "" {
184+ if m.focus == focusInput && m.agentInput.Value() != "" && m.turnCancel == nil {
176185 userMsg := m.agentInput.Value()
177186 m.agentInput.Reset()
178187 return *m, m.sendToLLM(userMsg), true
@@ -232,6 +241,19 @@ func (m *Model) handleUserMsg(msg llmUserMsg) Model {
232241 // Finalization messages (assistant, tool result) flush immediately since
233242 // they clear streaming state.
234243 func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
244+ // Turn was cancelled — drain remaining messages until the goroutine
245+ // sends its terminal message (llmDoneMsg or llmErrorMsg), then stop.
246+ if !m.llmInFlight {
247+ for _, raw := range batch {
248+ switch raw.(type) {
249+ case llmDoneMsg, llmErrorMsg:
250+ m.turnCancel = nil
251+ m.turnCtx = nil
252+ return m, nil
253+ }
254+ }
255+ return m, m.waitForLLMUpdate()
256+ }
235257 for _, raw := range batch {
236258 switch msg := raw.(type) {
237259 case llmReasoningDeltaMsg:
@@ -256,6 +278,11 @@ func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
256278
257279 case llmErrorMsg:
258280 m.llmInFlight = false
281+ if m.turnCancel != nil {
282+ m.turnCancel()
283+ m.turnCancel = nil
284+ }
285+ m.turnCtx = nil
259286 m.lastNetError = msg.err.Error()
260287 m.clearStreaming()
261288 m.appendText("", m.styles.Error.Render("Error: "+msg.err.Error()), "")
@@ -269,6 +296,11 @@ func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
269296
270297 case llmDoneMsg:
271298 m.llmInFlight = false
299+ if m.turnCancel != nil {
300+ m.turnCancel()
301+ m.turnCancel = nil
302+ }
303+ m.turnCtx = nil
272304 m.lastNetError = ""
273305 m.demoteOldUndo()
274306 m.appendText("")
@@ -350,6 +382,21 @@ func (m *Model) applyAssistantMsg(msg llmAssistantMsg) {
350382 }
351383 }
352384
385+// cancelTurn gracefully interrupts the in-flight LLM turn.
386+// History, turn boundaries, file changes, and deltas are preserved
387+// so the user can undo via the message footer if desired.
388+// turnCancel is kept non-nil until the drain loop finishes (see
389+// handleLLMBatch) so the enter guard blocks new submissions.
390+func (m *Model) cancelTurn() {
391+ if m.turnCancel != nil {
392+ m.turnCancel()
393+ }
394+ m.llmInFlight = false
395+ m.clearStreaming()
396+ m.appendText("", m.styles.Dim.Render("(interrupted)"), "")
397+ m.scrollOffset = 0
398+}
399+
353400 // clearStreaming resets active streaming state.
354401 func (m *Model) clearStreaming() {
355402 if !m.streaming {