xonecas.io
62159
Sy

symb / commit 1b3bca6

feat: cleaner rendering pipeline, removes stupid caching

xonecas2026-02-15 1b3bca654315d00e13fefe5a8100b631084b39f8 parent 1f659b2

6 files changed

docs/DESIGN.md+12−11
@@ -114,11 +114,19 @@ Needs an undo.
114114
115115 ### Context management?
116116
117-https://manus.im/blog/Context-Engineering-for-AI-Agents-Lessons-from-Building-Manus
117+1. Context token count: input, output, and total tokens. Store them in db. Shown each message with the timestamp:
118+
119+```
120+<time elapsed> <timestamp> <toekns in current count>/<tokens out current count> (<total tokens in context at current count).
121+<undo if most recent agent message>
122+```
123+
124+Gather baseline data with no context management. Then study:
118125
119-### improvements:
126+https://manus.im/blog/Context-Engineering-for-AI-Agents-Lessons-from-Building-Manus
127+(Search for more resources like the one above, build a document with concise knowledge base (500 lines max)
120128
121-- Improve markdown syntax hl overall
129+Implement findings?
122130
123131 ### Statusbar implementation
124132
@@ -161,14 +169,7 @@ File search modal:
161169
162170 # TODO (after features)
163171
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
172+2. Investigate if turn latency has improved
172173
173174 ---
174175
internal/highlight/highlight.go+45−29
@@ -5,7 +5,6 @@ package highlight
55 import (
66 "fmt"
77 "strings"
8- "sync"
98
109 "github.com/alecthomas/chroma/v2"
1110 "github.com/alecthomas/chroma/v2/formatters"
@@ -13,27 +12,10 @@ import (
1312 "github.com/alecthomas/chroma/v2/styles"
1413 )
1514
16-// ---------------------------------------------------------------------------
17-// Highlight cache (global, shared across callers)
18-// ---------------------------------------------------------------------------
19-
20-var (
21- hlCache = make(map[string]string)
22- hlCacheMu sync.RWMutex
23-)
24-
25-// CachedHighlight returns an ANSI-highlighted version of text using the given
15+// Highlight returns an ANSI-highlighted version of text using the given
2616 // Chroma language and theme. bgHex ("#rrggbb") is injected after every ANSI
2717 // reset so the background color is never lost.
28-func CachedHighlight(text, language, theme, bgHex string) string {
29- cacheKey := language + ":" + theme + ":" + bgHex + ":" + text
30- hlCacheMu.RLock()
31- if v, ok := hlCache[cacheKey]; ok {
32- hlCacheMu.RUnlock()
33- return v
34- }
35- hlCacheMu.RUnlock()
36-
18+func Highlight(text, language, theme, bgHex string) string {
3719 lex := lexers.Get(language)
3820 if lex == nil {
3921 return text
@@ -58,15 +40,7 @@ func CachedHighlight(text, language, theme, bgHex string) string {
5840 // the Background entry, and every \x1b[0m reset clears bg. Fix by
5941 // replacing resets with reset+bg so the background is always active.
6042 bgSeq := hexToBgSeq(bgHex)
61- result := bgSeq + strings.ReplaceAll(raw, "\x1b[0m", "\x1b[0m"+bgSeq)
62-
63- hlCacheMu.Lock()
64- if len(hlCache) > 2000 {
65- hlCache = make(map[string]string)
66- }
67- hlCache[cacheKey] = result
68- hlCacheMu.Unlock()
69- return result
43+ return bgSeq + strings.ReplaceAll(raw, "\x1b[0m", "\x1b[0m"+bgSeq)
7044 }
7145
7246 // hexToBgSeq converts "#rrggbb" to an ANSI 24-bit background escape sequence.
@@ -96,6 +70,48 @@ func hexNibble(c byte) int {
9670 return 0
9771 }
9872
73+// SplitLines splits a highlighted block into per-line strings, propagating
74+// ANSI style state across lines so each is independently renderable.
75+func SplitLines(block string) []string {
76+ lines := strings.Split(block, "\n")
77+ if len(lines) <= 1 {
78+ return lines
79+ }
80+ var active []string
81+ for i, line := range lines {
82+ if i > 0 && len(active) > 0 {
83+ lines[i] = strings.Join(active, "") + line
84+ }
85+ active = scanSGR(line, active)
86+ }
87+ return lines
88+}
89+
90+// scanSGR scans a line for SGR escape sequences and updates the active
91+// sequence list. Resets clear the list; other SGRs are appended.
92+func scanSGR(line string, active []string) []string {
93+ for j := 0; j < len(line); j++ {
94+ if line[j] != '\x1b' || j+1 >= len(line) || line[j+1] != '[' {
95+ continue
96+ }
97+ k := j + 2
98+ for k < len(line) && line[k] != 'm' && line[k] != '\x1b' {
99+ k++
100+ }
101+ if k >= len(line) || line[k] != 'm' {
102+ continue
103+ }
104+ params := line[j+2 : k]
105+ if params == "" || params == "0" {
106+ active = active[:0]
107+ } else {
108+ active = append(active, line[j:k+1])
109+ }
110+ j = k
111+ }
112+ return active
113+}
114+
99115 // ThemeBg extracts the background hex color from a Chroma style.
100116 // Returns "" if no background is set.
101117 func ThemeBg(theme string) string {
internal/tui/conv.go+12−39
@@ -20,7 +20,7 @@ func (m Model) convWidth() int { return m.layout.conv.Dx() }
2020 // (fenced code blocks, block quotes, etc.) maintain correct state.
2121 // Rendering cost is bounded by the frame-loop tick (~16ms).
2222 func highlightMarkdown(text string, fallback lipgloss.Style) []string {
23- hl := highlight.CachedHighlight(text, "markdown", constants.SyntaxTheme, "#000000")
23+ hl := highlight.Highlight(text, "markdown", constants.SyntaxTheme, "#000000")
2424 if hl == text {
2525 // Chroma produced no highlighting; apply fallback per line.
2626 raw := strings.Split(text, "\n")
@@ -30,7 +30,7 @@ func highlightMarkdown(text string, fallback lipgloss.Style) []string {
3030 }
3131 return out
3232 }
33- return strings.Split(hl, "\n")
33+ return highlight.SplitLines(hl)
3434 }
3535
3636 // styledLines applies a lipgloss style to each line in a multi-line text.
@@ -54,11 +54,10 @@ func textEntries(lines ...string) []convEntry {
5454 }
5555
5656 // appendConv appends entries and returns whether we were at bottom
57-// (for sticky scroll). Invalidates the wrapped-lines cache.
57+// (for sticky scroll).
5858 func (m *Model) appendConv(entries ...convEntry) bool {
5959 atBottom := m.scrollOffset == 0
6060 m.convEntries = append(m.convEntries, entries...)
61- m.convLines = nil // invalidate cache
6261 return atBottom
6362 }
6463
@@ -69,9 +68,9 @@ func (m *Model) appendText(lines ...string) bool {
6968
7069 // rebuildStreamEntries replaces any existing streaming entries with fresh
7170 // styled entries from the current streamingReasoning and streamingContent.
72-// Only re-wraps the streaming portion — the stable prefix is preserved.
71+// Wrapping is deferred to View() — this only updates convEntries.
7372 func (m *Model) rebuildStreamEntries() {
74- // Remove old streaming entries
73+ // Remove old streaming entries.
7574 if m.streamEntryStart >= 0 && m.streamEntryStart <= len(m.convEntries) {
7675 m.convEntries = m.convEntries[:m.streamEntryStart]
7776 }
@@ -82,41 +81,15 @@ func (m *Model) rebuildStreamEntries() {
8281 if m.streamingContent != "" {
8382 m.convEntries = append(m.convEntries, textEntries(highlightMarkdown(m.streamingContent, m.styles.Text)...)...)
8483 }
85-
86- // Incrementally re-wrap only the streaming entries.
87- w := m.convWidth()
88- if m.convLines != nil && m.convCachedW == w && m.streamWrapStart <= len(m.convLines) {
89- // Truncate cached lines/source to the stable prefix.
90- m.convLines = m.convLines[:m.streamWrapStart]
91- m.convLineSource = m.convLineSource[:m.streamWrapStart]
92- // Wrap only new streaming entries and append.
93- for i := m.streamEntryStart; i < len(m.convEntries); i++ {
94- entry := m.convEntries[i]
95- if entry.display == "" {
96- m.convLines = append(m.convLines, "")
97- m.convLineSource = append(m.convLineSource, i)
98- } else {
99- wrapped := wrapANSI(entry.display, w)
100- for range wrapped {
101- m.convLineSource = append(m.convLineSource, i)
102- }
103- m.convLines = append(m.convLines, wrapped...)
104- }
105- }
106- } else {
107- // Cache invalid or width changed — full rebuild.
108- m.convLines = nil
109- }
11084 }
11185
112-// wrappedConvLines returns the conversation wrapped to the current convWidth.
113-// Cached — only recomputed when entries change (nil) or width changes.
86+// wrappedConvLines wraps all conversation entries to the current convWidth.
87+// Cached for the current frame — cleared at the start of each Update cycle.
11488 func (m *Model) wrappedConvLines() []string {
115- w := m.convWidth()
116- if m.convLines != nil && m.convCachedW == w {
117- return m.convLines
89+ if m.frameLines != nil {
90+ return m.frameLines
11891 }
119- m.convCachedW = w
92+ w := m.convWidth()
12093 lines := make([]string, 0, len(m.convEntries))
12194 source := make([]int, 0, len(m.convEntries))
12295 for i, entry := range m.convEntries {
@@ -131,9 +104,9 @@ func (m *Model) wrappedConvLines() []string {
131104 lines = append(lines, wrapped...)
132105 }
133106 }
134- m.convLines = lines
135107 m.convLineSource = source
136- return m.convLines
108+ m.frameLines = lines
109+ return lines
137110 }
138111
139112 // makeSeparator builds a timestamp separator line.
internal/tui/editor/view.go+2−2
@@ -122,8 +122,8 @@ func (m Model) buildVisualRows(tw int) []visualRow {
122122 block[i] = vl.text
123123 }
124124 joined := strings.Join(block, "\n")
125- hlBlock := highlight.CachedHighlight(joined, m.Language, m.SyntaxTheme, m.bgHexForHighlight())
126- hlLines = strings.Split(hlBlock, "\n")
125+ hlBlock := highlight.Highlight(joined, m.Language, m.SyntaxTheme, m.bgHexForHighlight())
126+ hlLines = highlight.SplitLines(hlBlock)
127127 }
128128
129129 // Second pass: build visual rows with per-line HL from the block result.
internal/tui/tui.go+4−6
@@ -207,9 +207,7 @@ type Model struct {
207207
208208 // Conversation
209209 convEntries []convEntry // Conversation entries (not wrapped)
210- convLines []string // Wrapped visual lines (cache, rebuilt on width change)
211- convLineSource []int // Maps each wrapped line -> index in convEntries
212- convCachedW int // Width used for current convLines cache
210+ convLineSource []int // Maps each wrapped line -> index in convEntries (recomputed each frame)
213211 scrollOffset int // Lines from bottom (0 = pinned)
214212
215213 // Streaming state: raw text accumulated during streaming, styled at render time
@@ -217,7 +215,6 @@ type Model struct {
217215 streamingContent string // In-progress content text
218216 streaming bool // Whether we're currently streaming
219217 streamEntryStart int // Index in convEntries where streaming entries begin (-1 = none)
220- streamWrapStart int // Number of cached wrapped lines before streaming entries
221218
222219 // Undo
223220 deltaTracker *delta.Tracker
@@ -241,8 +238,9 @@ type Model struct {
241238 // Hover state: wrapped line index under cursor (-1 = none)
242239 hoverConvLine int
243240
244- // Frame loop: dirty flag defers highlight/wrap to the next 16ms tick.
245- dirty bool
241+ // Frame loop
242+ streamDirty bool // New streaming content arrived since last rebuild
243+ frameLines []string // Per-frame cache of wrapped conv lines (cleared each Update)
246244 }
247245
248246 // New creates a new TUI model.
internal/tui/update.go+15−24
@@ -18,6 +18,8 @@ import (
1818 // ---------------------------------------------------------------------------
1919
2020 func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
21+ m.frameLines = nil // invalidate per-frame wrap cache
22+
2123 switch msg := msg.(type) {
2224
2325 // -- Window resize -------------------------------------------------------
@@ -42,9 +44,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
4244 return mdl, cmd
4345 }
4446
45- // -- Frame tick (60fps) — flush deferred highlight/wrap work -------------
47+ // -- Frame tick (60fps) — rebuild streaming entries for live updates ------
4648 case tickMsg:
47- m.flushDirty()
49+ m.tickStreaming()
4850 return m, frameTick()
4951
5052 // -- LLM batch (multiple messages drained from updateChan) ---------------
@@ -64,7 +66,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
6466 case ShellOutputMsg:
6567 m.ensureStreaming()
6668 m.streamingContent += msg.Content
67- m.dirty = true
69+ m.streamDirty = true
6870 return m, nil
6971
7072 case undoMsg:
@@ -209,32 +211,28 @@ func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
209211 case llmReasoningDeltaMsg:
210212 m.ensureStreaming()
211213 m.streamingReasoning += msg.content
212- m.dirty = true
214+ m.streamDirty = true
213215
214216 case llmContentDeltaMsg:
215217 m.ensureStreaming()
216218 m.streamingContent += msg.content
217- m.dirty = true
219+ m.streamDirty = true
218220
219221 case llmHistoryMsg:
220222 m.history = append(m.history, msg.msg)
221223 m.saveMessage(msg.msg)
222224
223225 case llmAssistantMsg:
224- m.flushDirty()
225226 m.applyAssistantMsg(msg)
226227
227228 case llmToolResultMsg:
228- m.flushDirty()
229229 m.applyToolResultMsg(msg)
230230
231231 case llmErrorMsg:
232- m.flushDirty()
233232 m.appendText("", m.styles.Error.Render("Error: "+msg.err.Error()), "")
234233 return m, nil
235234
236235 case llmDoneMsg:
237- m.flushDirty()
238236 m.demoteOldUndo()
239237 m.appendText("")
240238 sep := m.makeSeparator(msg.duration.Round(time.Second).String(), msg.timestamp)
@@ -248,6 +246,14 @@ func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
248246 return m, m.waitForLLMUpdate()
249247 }
250248
249+// tickStreaming rebuilds streaming entries when new content has arrived.
250+func (m *Model) tickStreaming() {
251+ if m.streamDirty {
252+ m.rebuildStreamEntries()
253+ m.streamDirty = false
254+ }
255+}
256+
251257 // ensureStreaming initialises stream state on the first delta of a turn.
252258 func (m *Model) ensureStreaming() {
253259 if m.streaming {
@@ -255,20 +261,10 @@ func (m *Model) ensureStreaming() {
255261 }
256262 m.streaming = true
257263 m.streamEntryStart = len(m.convEntries)
258- m.streamWrapStart = len(m.wrappedConvLines())
259264 m.streamingReasoning = ""
260265 m.streamingContent = ""
261266 }
262267
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 {
267- m.rebuildStreamEntries()
268- }
269- m.dirty = false
270-}
271-
272268 // applyAssistantMsg finalizes streaming state and appends the assistant's
273269 // response entries. Extracted so handleLLMBatch can reuse the logic.
274270 func (m *Model) applyAssistantMsg(msg llmAssistantMsg) {
@@ -312,7 +308,6 @@ func (m *Model) clearStreaming() {
312308 m.streamEntryStart = -1
313309 m.streamingReasoning = ""
314310 m.streamingContent = ""
315- m.convLines = nil
316311 }
317312
318313 // applyToolResultMsg appends tool result display entries.
@@ -489,7 +484,6 @@ func (m *Model) demoteOldUndo() {
489484 m.convEntries[i].kind = entrySeparator
490485 // full field carries the original separator display text.
491486 m.convEntries[i].display = m.convEntries[i].full
492- m.convLines = nil // invalidate cache
493487 return
494488 }
495489 }
@@ -521,7 +515,6 @@ func (m *Model) trimOldTurns() {
521515 m.turnBoundaries[i].historyIdx -= histOff
522516 }
523517
524- m.convLines = nil // invalidate wrap cache
525518 }
526519 }
527520
@@ -546,7 +539,6 @@ func (m *Model) handleUndo() Model {
546539 // 2. Truncate in-memory history and display.
547540 m.history = m.history[:tb.historyIdx]
548541 m.convEntries = m.convEntries[:tb.convIdx]
549- m.convLines = nil // invalidate cache
550542
551543 // 2b. Show error after truncation so it's visible.
552544 if undoErr != nil {
@@ -579,7 +571,6 @@ func (m *Model) handleUndo() Model {
579571 if m.convEntries[i].kind == entrySeparator {
580572 // full carries the original separator display text.
581573 m.convEntries[i] = m.makeUndoEntry(m.convEntries[i].full)
582- m.convLines = nil
583574 break
584575 }
585576 }