@@ -217,14 +217,12 @@ func (m *Model) handleKeyPress(msg tea.KeyPressMsg) (Model, tea.Cmd, bool) {
217217 return Model{}, nil, false
218218 }
219219
220-// handleUserMsg records a user message in history and conversation display.
220+// handleUserMsg records a user message in the DB and conversation display.
221221 func (m *Model) handleUserMsg(msg llmUserMsg) Model {
222222 now := time.Now()
223223 userMsg := provider.Message{Role: "user", Content: msg.content, CreatedAt: now}
224224
225- historyIdx := len(m.history)
226225 convIdx := len(m.convEntries)
227- m.history = append(m.history, userMsg)
228226
229227 // Save synchronously to get DB row ID for undo tracking.
230228 var dbMsgID int64
@@ -243,7 +241,6 @@ func (m *Model) handleUserMsg(msg llmUserMsg) Model {
243241 }
244242
245243 m.turnBoundaries = append(m.turnBoundaries, turnBoundary{
246- historyIdx: historyIdx,
247244 convIdx: convIdx,
248245 dbMsgID: dbMsgID,
249246 inputTokens: m.totalInputTokens,
@@ -285,7 +282,6 @@ func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
285282 m.streamDirty = true
286283
287284 case llmHistoryMsg:
288- m.history = append(m.history, msg.msg)
289285 m.saveMessage(msg.msg)
290286
291287 case llmAssistantMsg:
@@ -331,7 +327,6 @@ func (m Model) drainCancelled(batch llmBatchMsg) (tea.Model, tea.Cmd) {
331327 for _, raw := range batch {
332328 switch msg := raw.(type) {
333329 case llmHistoryMsg:
334- m.history = append(m.history, msg.msg)
335330 m.saveMessage(msg.msg)
336331 case llmDoneMsg, llmErrorMsg:
337332 m.patchInterruptedHistory()
@@ -357,17 +352,22 @@ func (m *Model) finishTurn() {
357352 // interrupted turn left history in an invalid API state (trailing tool_use
358353 // without result, or trailing tool result with no assistant follow-up).
359354 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)
355+ if m.store == nil {
356+ return
357+ }
358+ last, err := m.store.LoadLastMessage(m.sessionID)
359+ if err != nil || last == nil {
360+ return
361+ }
362+ hasToolCalls := len(last.ToolCalls) > 0 && string(last.ToolCalls) != "[]"
363+ needsPatch := last.Role != roleAssistant || hasToolCalls
364+ if needsPatch {
365+ interruptMsg := provider.Message{
366+ Role: roleAssistant,
367+ Content: "The user interrupted me.",
368+ CreatedAt: time.Now(),
370369 }
370+ m.saveMessage(interruptMsg)
371371 }
372372 }
373373
@@ -643,31 +643,17 @@ func (m *Model) demoteOldUndo() {
643643 }
644644
645645 // trimOldTurns drops the oldest display turns when we exceed maxDisplayTurns.
646-// Messages are already persisted in the DB — this only trims in-memory state
647-// to bound rendering cost.
646+// Messages live in the DB — this only trims display entries to bound rendering cost.
648647 func (m *Model) trimOldTurns() {
649648 for len(m.turnBoundaries) > maxDisplayTurns {
650- // The second boundary marks where the oldest visible turn's display ends.
651649 cutConv := m.turnBoundaries[1].convIdx
652- cutHist := m.turnBoundaries[1].historyIdx
653650
654- // Shift convEntries and adjust all boundary indices.
655651 m.convEntries = m.convEntries[cutConv:]
656652 m.turnBoundaries = m.turnBoundaries[1:]
657653
658- // History: keep the system prompt (index 0) + trim old turn messages.
659- // After: history = [system] + history[cutHist:], so what was at
660- // cutHist is now at index 1 — offset is cutHist-1.
661- histOff := cutHist - 1
662- if cutHist > 1 && cutHist < len(m.history) {
663- m.history = append(m.history[:1], m.history[cutHist:]...)
664- }
665-
666654 for i := range m.turnBoundaries {
667655 m.turnBoundaries[i].convIdx -= cutConv
668- m.turnBoundaries[i].historyIdx -= histOff
669656 }
670-
671657 }
672658 }
673659
@@ -695,8 +681,7 @@ func (m *Model) handleUndo() Model {
695681 m.deltaTracker.DeleteTurn(m.sessionID, tb.dbMsgID)
696682 }
697683
698- // 2. Truncate in-memory history and display.
699- m.history = m.history[:tb.historyIdx]
684+ // 2. Truncate display entries.
700685 m.convEntries = m.convEntries[:tb.convIdx]
701686
702687 // 2b. Show error after truncation so it's visible.
@@ -704,9 +689,8 @@ func (m *Model) handleUndo() Model {
704689 m.appendText("", m.styles.Error.Render("undo file restore failed: "+undoErr.Error()), "")
705690 }
706691
707- // 3. Flush async saves then delete messages from DB.
692+ // 3. Delete messages from DB.
708693 if m.store != nil && tb.dbMsgID > 0 {
709- m.store.Flush()
710694 if err := m.store.DeleteMessagesFrom(m.sessionID, tb.dbMsgID); err != nil {
711695 log.Warn().Err(err).Msg("undo: failed to delete messages")
712696 }