@@ -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{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}