xonecas.io
62159
Sy

symb / commit a131b27

fix: lint and tests

xonecas2026-02-16 a131b277aaef00bdc5fc9896a7346489b8e587a7 parent f86c804

7 files changed

Makefile+2−1
@@ -12,8 +12,9 @@ run:
1212 test:
1313 go test -v -race -coverprofile=coverage.out ./cmd/... ./internal/...
1414
15-# Run linter
15+# Run linter (auto-formats first)
1616 lint:
17+ gofmt -w ./cmd/ ./internal/
1718 $$(go env GOPATH)/bin/golangci-lint run ./cmd/... ./internal/...
1819
1920 # Clean build artifacts
TODO.md+5−19
@@ -1,21 +1,7 @@
1-# Rendering bugs after frame-loop refactor
21
3-## Bug 1: Conversation pane — first/last lines of assistant response unstyled
2+# TODO write cancels assistant message
43
5-**Symptom:** The cache connections with the incremental additions is visible
6-in the output, breaks the continuous highlight, most noticeable with markdown
7-but happens to all filetypes.
8-
9-- Fix the rendering pipeline from ground up.
10-- Remove caching/buffering etc from rendering all together.
11-- with the tick tracking framerate we should render the TUI state in full each frame.
12- - Only renders what is displayed in the TUI (cull scroll history no visible), keep memory and db cache only. (5 ish turns in memory, rest in db)
13- - Aim for 60fps - we will adjust and see what our render times are and optimize with this info.
14-
15-## Bug 2: Gray background on syntax highlighted areas (editor + conversation)
16-
17-**Symptom:** Syntax highlighted regions show a explicitly unset background instead
18-of pure black (#000000) with some themes, more noticeable with "pure black" themes.
19-
20-- Background fallback to our default might not be working - fix.
21-- Audit that background is being inherited correctly, this should be free, provided by our libraries.
4+- Session: 017549ac3301e35884ed78aa7063ead8
5+- Assistant completed work, called the todo message without content to clear and got an error
6+- session is currupted after resume.
7+- Expected: Assistant reply after calling todo tool, even if it errored. Session to not be corrupted after resume
internal/tui/conv.go+1−1
@@ -180,7 +180,7 @@ func historyConvEntries(msgs []provider.Message) []convEntry {
180180 continue
181181 }
182182 entries = append(entries, textEntries(highlightMarkdown(msg.Content, sty.Text)...)...)
183- case "assistant":
183+ case roleAssistant:
184184 if msg.Content != "" {
185185 entries = append(entries, textEntries(highlightMarkdown(msg.Content, sty.Text)...)...)
186186 }
internal/tui/editor/editor.go+1−0
@@ -173,6 +173,7 @@ func (m *Model) GotoLine(line int) {
173173 m.col = 0
174174 m.clampScroll()
175175 }
176+
176177 // ---------------------------------------------------------------------------
177178 // Selection API (called by parent)
178179 // ---------------------------------------------------------------------------
internal/tui/messages.go+1−1
@@ -197,7 +197,7 @@ func (m Model) processLLM() tea.Cmd {
197197 OnMessage: func(msg provider.Message) {
198198 ch <- llmHistoryMsg{msg: msg}
199199 switch msg.Role {
200- case "assistant":
200+ case roleAssistant:
201201 ch <- llmAssistantMsg{
202202 reasoning: msg.Reasoning,
203203 content: msg.Content,
internal/tui/tui.go+2−0
@@ -40,6 +40,8 @@ const (
4040 minPaneWidth = 20
4141 maxPreviewLines = 5 // Max lines shown for tool results before truncation
4242 maxDisplayTurns = 5 // Max conversation turns kept in memory; older turns live in DB
43+
44+ roleAssistant = "assistant"
4345 )
4446
4547 // generateLayout computes all regions from terminal size and divider position.
internal/tui/update.go+49−45
@@ -269,40 +269,8 @@ func (m *Model) handleUserMsg(msg llmUserMsg) Model {
269269 // Finalization messages (assistant, tool result) flush immediately since
270270 // they clear streaming state.
271271 func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
272- // Turn was cancelled — drain remaining messages until the goroutine
273- // sends its terminal message (llmDoneMsg or llmErrorMsg), then stop.
274- // We still persist llmHistoryMsg so the DB stays consistent (an
275- // assistant's tool_use blocks must be present for its tool results).
276272 if !m.llmInFlight {
277- for _, raw := range batch {
278- switch msg := raw.(type) {
279- case llmHistoryMsg:
280- m.history = append(m.history, msg.msg)
281- m.saveMessage(msg.msg)
282- case llmDoneMsg, llmErrorMsg:
283- // If the interrupted turn left history in an invalid
284- // API state (trailing tool_use without result, or
285- // trailing tool result with no assistant follow-up),
286- // close it with a synthetic assistant message so
287- // resume won't trigger a 400.
288- if n := len(m.history); n > 0 {
289- last := m.history[n-1]
290- if last.Role != "assistant" || len(last.ToolCalls) > 0 {
291- interruptMsg := provider.Message{
292- Role: "assistant",
293- Content: "The user interrupted me.",
294- CreatedAt: time.Now(),
295- }
296- m.history = append(m.history, interruptMsg)
297- m.saveMessage(interruptMsg)
298- }
299- }
300- m.turnCancel = nil
301- m.turnCtx = nil
302- return m, nil
303- }
304- }
305- return m, m.waitForLLMUpdate()
273+ return m.drainCancelled(batch)
306274 }
307275 for _, raw := range batch {
308276 switch msg := raw.(type) {
@@ -327,12 +295,7 @@ func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
327295 m.applyToolResultMsg(msg)
328296
329297 case llmErrorMsg:
330- m.llmInFlight = false
331- if m.turnCancel != nil {
332- m.turnCancel()
333- m.turnCancel = nil
334- }
335- m.turnCtx = nil
298+ m.finishTurn()
336299 m.lastNetError = msg.err.Error()
337300 m.clearStreaming()
338301 m.appendText("", m.styles.Error.Render("Error: "+msg.err.Error()), "")
@@ -345,12 +308,7 @@ func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
345308 m.totalOutputTokens += msg.outputTokens
346309
347310 case llmDoneMsg:
348- m.llmInFlight = false
349- if m.turnCancel != nil {
350- m.turnCancel()
351- m.turnCancel = nil
352- }
353- m.turnCtx = nil
311+ m.finishTurn()
354312 m.lastNetError = ""
355313 m.demoteOldUndo()
356314 m.appendText("")
@@ -367,6 +325,52 @@ func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
367325 return m, m.waitForLLMUpdate()
368326 }
369327
328+// drainCancelled handles a batch after the turn was cancelled.
329+// It persists history messages and patches invalid API state on termination.
330+func (m Model) drainCancelled(batch llmBatchMsg) (tea.Model, tea.Cmd) {
331+ for _, raw := range batch {
332+ switch msg := raw.(type) {
333+ case llmHistoryMsg:
334+ m.history = append(m.history, msg.msg)
335+ m.saveMessage(msg.msg)
336+ case llmDoneMsg, llmErrorMsg:
337+ m.patchInterruptedHistory()
338+ m.turnCancel = nil
339+ m.turnCtx = nil
340+ return m, nil
341+ }
342+ }
343+ return m, m.waitForLLMUpdate()
344+}
345+
346+// finishTurn clears in-flight state and cancels the turn context.
347+func (m *Model) finishTurn() {
348+ m.llmInFlight = false
349+ if m.turnCancel != nil {
350+ m.turnCancel()
351+ m.turnCancel = nil
352+ }
353+ m.turnCtx = nil
354+}
355+
356+// patchInterruptedHistory appends a synthetic assistant message if the
357+// interrupted turn left history in an invalid API state (trailing tool_use
358+// without result, or trailing tool result with no assistant follow-up).
359+func (m *Model) patchInterruptedHistory() {
360+ if n := len(m.history); n > 0 {
361+ last := m.history[n-1]
362+ if last.Role != roleAssistant || len(last.ToolCalls) > 0 {
363+ interruptMsg := provider.Message{
364+ Role: roleAssistant,
365+ Content: "The user interrupted me.",
366+ CreatedAt: time.Now(),
367+ }
368+ m.history = append(m.history, interruptMsg)
369+ m.saveMessage(interruptMsg)
370+ }
371+ }
372+}
373+
370374 // brailleFrames is the spinner animation sequence.
371375 var brailleFrames = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}