@@ -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 {