xonecas.io
62159
Sy

symb / commit 1f659b2

feat: no immediate rendering, use 60fps for render rate

xonecas2026-02-15 1f659b246f75568f8b8680097dda237260f9b30c parent ed28713

7 files changed

TODO.md+20−5
@@ -1,6 +1,21 @@
1-# TODO
1+# Rendering bugs after frame-loop refactor
22
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
3+## Bug 1: Conversation pane — first/last lines of assistant response unstyled
4+
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.
docs/DESIGN.md+16−0
@@ -153,6 +153,22 @@ File search modal:
153153 - `ESC` cancels the file search closing the modal.
154154
155155 Selection is opened in the editor.
156+
157+### Code viewer pane -> Becomes Code editor pane
158+
159+ - Enable the same editting abilities as the agent input box.
160+ - <ctrl + s> Saves the file. (Do we just save it, or we send a message to the LLM to apply the edit?)
161+
162+# TODO (after features)
163+
164+1. Review prompts, refactor prompt system message composition [x]
165+2. Continue to test Edit tool, LLMs make errors with it. [x]
166+3. Context token count: input, output, and total tokens
167+4. Simple statusbar with more info
168+5. Investigate if turn latency has improved
169+6. Perf: debounce/skip highlightMarkdown during streaming, audit all rendering hot paths
170+7. Perf: make SaveMessageSync async (don't block TUI update loop before LLM call)
171+8. Perf: make SSE parser channel buffered (e.g. 64) to let parser get ahead of consumer
156172
157173 ---
158174
internal/tui/conv.go+12−11
@@ -15,21 +15,22 @@ import (
1515 // convWidth returns the usable width of the conversation pane.
1616 func (m Model) convWidth() int { return m.layout.conv.Dx() }
1717
18-// highlightMarkdown highlights markdown text line-by-line via Chroma.
19-// Line-by-line keeps each call tiny and cacheable, so streaming stays fast.
20-// Fenced code blocks lose inner language highlighting but delimiters are styled.
18+// highlightMarkdown highlights a full markdown text block via Chroma.
19+// The entire text is tokenised as one unit so multi-line constructs
20+// (fenced code blocks, block quotes, etc.) maintain correct state.
21+// Rendering cost is bounded by the frame-loop tick (~16ms).
2122 func highlightMarkdown(text string, fallback lipgloss.Style) []string {
22- raw := strings.Split(text, "\n")
23- out := make([]string, len(raw))
24- for i, line := range raw {
25- hl := highlight.CachedHighlight(line, "markdown", constants.SyntaxTheme, "#000000")
26- if hl == line {
23+ hl := highlight.CachedHighlight(text, "markdown", constants.SyntaxTheme, "#000000")
24+ if hl == text {
25+ // Chroma produced no highlighting; apply fallback per line.
26+ raw := strings.Split(text, "\n")
27+ out := make([]string, len(raw))
28+ for i, line := range raw {
2729 out[i] = fallback.Render(line)
28- } else {
29- out[i] = hl
3030 }
31+ return out
3132 }
32- return out
33+ return strings.Split(hl, "\n")
3334 }
3435
3536 // styledLines applies a lipgloss style to each line in a multi-line text.
internal/tui/editor/view.go+40−8
@@ -84,6 +84,9 @@ func (m Model) View() string {
8484 }
8585
8686 // buildVisualRows computes all visible visual rows from the scroll position.
87+// All visible buffer lines are highlighted as a single block so Chroma
88+// maintains cross-line state (important for markdown fenced blocks, but
89+// harmless and slightly fewer calls for other languages too).
8790 func (m Model) buildVisualRows(tw int) []visualRow {
8891 hasSyntax := m.Language != "" && m.SyntaxTheme != ""
8992 startBuf, startRuneOff := m.visualToBuffer(m.scroll)
@@ -92,25 +95,54 @@ func (m Model) buildVisualRows(tw int) []visualRow {
9295 startSubRow = startRuneOff / tw
9396 }
9497
95- var rows []visualRow
96- for bufIdx := startBuf; bufIdx < len(m.lines) && len(rows) < m.height; bufIdx++ {
98+ // First pass: collect visible buffer lines and their segments.
99+ type bufLine struct {
100+ idx int
101+ text string
102+ segments []string
103+ }
104+ var visible []bufLine
105+ rowCount := 0
106+ for bufIdx := startBuf; bufIdx < len(m.lines) && rowCount < m.height; bufIdx++ {
97107 lineStr := expandTabs(string(m.lines[bufIdx]))
98108 segments := wrapPlain(lineStr, tw)
109+ first := 0
110+ if bufIdx == startBuf {
111+ first = startSubRow
112+ }
113+ visible = append(visible, bufLine{idx: bufIdx, text: lineStr, segments: segments})
114+ rowCount += len(segments) - first
115+ }
99116
117+ // Highlight all visible lines as one block.
118+ var hlLines []string
119+ if hasSyntax && len(visible) > 0 {
120+ block := make([]string, len(visible))
121+ for i, vl := range visible {
122+ block[i] = vl.text
123+ }
124+ joined := strings.Join(block, "\n")
125+ hlBlock := highlight.CachedHighlight(joined, m.Language, m.SyntaxTheme, m.bgHexForHighlight())
126+ hlLines = strings.Split(hlBlock, "\n")
127+ }
128+
129+ // Second pass: build visual rows with per-line HL from the block result.
130+ var rows []visualRow
131+ for li, vl := range visible {
100132 var fullHL string
101- if hasSyntax {
102- fullHL = highlight.CachedHighlight(lineStr, m.Language, m.SyntaxTheme, m.bgHexForHighlight())
133+ if li < len(hlLines) {
134+ fullHL = hlLines[li]
103135 }
104136
105137 firstSub := 0
106- if bufIdx == startBuf {
138+ if vl.idx == startBuf {
107139 firstSub = startSubRow
108140 }
109141 runeOff := firstSub * tw
110- for subIdx := firstSub; subIdx < len(segments) && len(rows) < m.height; subIdx++ {
111- segLen := len([]rune(segments[subIdx]))
142+ for subIdx := firstSub; subIdx < len(vl.segments) && len(rows) < m.height; subIdx++ {
143+ segLen := len([]rune(vl.segments[subIdx]))
112144 rows = append(rows, visualRow{
113- bufRow: bufIdx, subRow: subIdx, text: segments[subIdx],
145+ bufRow: vl.idx, subRow: subIdx, text: vl.segments[subIdx],
114146 fullHL: fullHL, segStart: runeOff, segEnd: runeOff + segLen,
115147 })
116148 runeOff += segLen
internal/tui/messages.go+12−0
@@ -46,6 +46,11 @@ type llmReasoningDeltaMsg struct{ content string }
4646 // Exported so main.go can send it via program.Send from the shell handler callback.
4747 type ShellOutputMsg struct{ Content string }
4848
49+// tickMsg drives the 60fps frame loop (~16ms). Rendering work (highlight,
50+// wrap) is deferred to this tick so streaming deltas don't cause per-batch
51+// rebuilds.
52+type tickMsg time.Time
53+
4954 // undoMsg is sent when the user clicks the undo control.
5055 type undoMsg struct{}
5156
@@ -65,6 +70,13 @@ type LSPDiagnosticsMsg struct {
6570 // ELM commands
6671 // ---------------------------------------------------------------------------
6772
73+// frameTick returns a command that fires a tickMsg after ~16ms (~60fps).
74+func frameTick() tea.Cmd {
75+ return tea.Tick(16*time.Millisecond, func(t time.Time) tea.Msg {
76+ return tickMsg(t)
77+ })
78+}
79+
6880 func (m Model) sendToLLM(userInput string) tea.Cmd {
6981 return func() tea.Msg { return llmUserMsg{content: userInput} }
7082 }
internal/tui/tui.go+6−2
@@ -38,6 +38,7 @@ const (
3838 statusRows = 2 // Status separator + status bar
3939 minPaneWidth = 20
4040 maxPreviewLines = 5 // Max lines shown for tool results before truncation
41+ maxDisplayTurns = 5 // Max conversation turns kept in memory; older turns live in DB
4142 )
4243
4344 // generateLayout computes all regions from terminal size and divider position.
@@ -239,6 +240,9 @@ type Model struct {
239240
240241 // Hover state: wrapped line index under cursor (-1 = none)
241242 hoverConvLine int
243+
244+ // Frame loop: dirty flag defers highlight/wrap to the next 16ms tick.
245+ dirty bool
242246 }
243247
244248 // New creates a new TUI model.
@@ -316,7 +320,7 @@ func New(prov provider.Provider, proxy *mcp.Proxy, tools []mcp.Tool, modelID str
316320 }
317321 }
318322
319-// Init starts spinner and cursor blink.
323+// Init starts spinner, cursor blink, and the 60fps frame loop.
320324 func (m Model) Init() tea.Cmd {
321- return tea.Batch(m.spinner.Tick, func() tea.Msg { return editor.Blink() })
325+ return tea.Batch(m.spinner.Tick, func() tea.Msg { return editor.Blink() }, frameTick())
322326 }
internal/tui/update.go+60−26
@@ -42,6 +42,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
4242 return mdl, cmd
4343 }
4444
45+ // -- Frame tick (60fps) — flush deferred highlight/wrap work -------------
46+ case tickMsg:
47+ m.flushDirty()
48+ return m, frameTick()
49+
4550 // -- LLM batch (multiple messages drained from updateChan) ---------------
4651 case llmBatchMsg:
4752 return m.handleLLMBatch(msg)
@@ -51,11 +56,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5156 return m.handleUserMsg(msg), tea.Batch(m.processLLM(), m.waitForLLMUpdate())
5257
5358 case LSPDiagnosticsMsg:
54- if msg.FilePath == m.editorFilePath {
55- m.editor.DiagnosticLines = msg.Lines
56- }
57- return m, nil
58-
59+ return m.handleLSPDiag(msg), nil
5960 case UpdateToolsMsg:
6061 m.mcpTools = msg.Tools
6162 return m, nil
@@ -63,7 +64,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
6364 case ShellOutputMsg:
6465 m.ensureStreaming()
6566 m.streamingContent += msg.Content
66- m.rebuildStreamEntries()
67+ m.dirty = true
6768 return m, nil
6869
6970 case undoMsg:
@@ -87,6 +88,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
8788 return m, tea.Batch(cmds...)
8889 }
8990
91+// handleLSPDiag applies LSP diagnostic markers when the file matches the editor.
92+func (m *Model) handleLSPDiag(msg LSPDiagnosticsMsg) Model {
93+ if msg.FilePath == m.editorFilePath {
94+ m.editor.DiagnosticLines = msg.Lines
95+ }
96+ return *m
97+}
98+
9099 // handleResize applies a window size change and re-derives layout.
91100 func (m *Model) handleResize(msg tea.WindowSizeMsg) {
92101 m.width, m.height = msg.Width, msg.Height
@@ -191,56 +200,50 @@ func (m *Model) handleUserMsg(msg llmUserMsg) Model {
191200 }
192201
193202 // handleLLMBatch processes a batch of messages drained from updateChan.
194-// Delta messages are accumulated first, with a single rebuildStreamEntries
195-// at the end, avoiding per-token re-wraps.
203+// Streaming deltas just mark dirty — the next frame tick does the rebuild.
204+// Finalization messages (assistant, tool result) flush immediately since
205+// they clear streaming state.
196206 func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
197- needRebuild := false
198-
199207 for _, raw := range batch {
200208 switch msg := raw.(type) {
201209 case llmReasoningDeltaMsg:
202210 m.ensureStreaming()
203211 m.streamingReasoning += msg.content
204- needRebuild = true
212+ m.dirty = true
205213
206214 case llmContentDeltaMsg:
207215 m.ensureStreaming()
208216 m.streamingContent += msg.content
209- needRebuild = true
217+ m.dirty = true
210218
211219 case llmHistoryMsg:
212220 m.history = append(m.history, msg.msg)
213221 m.saveMessage(msg.msg)
214222
215223 case llmAssistantMsg:
216- needRebuild = m.flushRebuild(needRebuild)
224+ m.flushDirty()
217225 m.applyAssistantMsg(msg)
218226
219227 case llmToolResultMsg:
220- needRebuild = m.flushRebuild(needRebuild)
228+ m.flushDirty()
221229 m.applyToolResultMsg(msg)
222230
223231 case llmErrorMsg:
224- m.flushRebuild(needRebuild)
232+ m.flushDirty()
225233 m.appendText("", m.styles.Error.Render("Error: "+msg.err.Error()), "")
226234 return m, nil
227235
228236 case llmDoneMsg:
229- m.flushRebuild(needRebuild)
230- // Replace previous turn's undo entry with a normal separator.
237+ m.flushDirty()
231238 m.demoteOldUndo()
232239 m.appendText("")
233- // Append undo control instead of a plain separator for the latest turn.
234- // Store the separator text in full so it can be restored on demote.
235240 sep := m.makeSeparator(msg.duration.Round(time.Second).String(), msg.timestamp)
236241 m.appendConv(m.makeUndoEntry(sep))
242+ m.trimOldTurns()
237243 return m, nil
238244 }
239245 }
240246
241- // Apply one rebuild for all accumulated deltas.
242- m.flushRebuild(needRebuild)
243-
244247 // More messages may follow — keep listening.
245248 return m, m.waitForLLMUpdate()
246249 }
@@ -257,12 +260,13 @@ func (m *Model) ensureStreaming() {
257260 m.streamingContent = ""
258261 }
259262
260-// flushRebuild calls rebuildStreamEntries when needed and returns false.
261-func (m *Model) flushRebuild(needed bool) bool {
262- if needed {
263+// flushDirty rebuilds stream entries if dirty, then clears the flag.
264+// Called before finalization messages that clear streaming state.
265+func (m *Model) flushDirty() {
266+ if m.dirty && m.streaming {
263267 m.rebuildStreamEntries()
264268 }
265- return false
269+ m.dirty = false
266270 }
267271
268272 // applyAssistantMsg finalizes streaming state and appends the assistant's
@@ -491,6 +495,36 @@ func (m *Model) demoteOldUndo() {
491495 }
492496 }
493497
498+// trimOldTurns drops the oldest display turns when we exceed maxDisplayTurns.
499+// Messages are already persisted in the DB — this only trims in-memory state
500+// to bound rendering cost.
501+func (m *Model) trimOldTurns() {
502+ for len(m.turnBoundaries) > maxDisplayTurns {
503+ // The second boundary marks where the oldest visible turn's display ends.
504+ cutConv := m.turnBoundaries[1].convIdx
505+ cutHist := m.turnBoundaries[1].historyIdx
506+
507+ // Shift convEntries and adjust all boundary indices.
508+ m.convEntries = m.convEntries[cutConv:]
509+ m.turnBoundaries = m.turnBoundaries[1:]
510+
511+ // History: keep the system prompt (index 0) + trim old turn messages.
512+ // After: history = [system] + history[cutHist:], so what was at
513+ // cutHist is now at index 1 — offset is cutHist-1.
514+ histOff := cutHist - 1
515+ if cutHist > 1 && cutHist < len(m.history) {
516+ m.history = append(m.history[:1], m.history[cutHist:]...)
517+ }
518+
519+ for i := range m.turnBoundaries {
520+ m.turnBoundaries[i].convIdx -= cutConv
521+ m.turnBoundaries[i].historyIdx -= histOff
522+ }
523+
524+ m.convLines = nil // invalidate wrap cache
525+ }
526+}
527+
494528 // handleUndo reverts the most recent turn: restores files, truncates history
495529 // and convEntries, and cleans up the database.
496530 func (m *Model) handleUndo() Model {