@@ -1,6 +1,7 @@
11 package tui
22
33 import (
4+ "context"
45 "encoding/json"
56 "fmt"
67 "sort"
@@ -57,7 +58,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5758 // -- LLM user message (sent before streaming begins) ---------------------
5859 case llmUserMsg:
5960 m.llmInFlight = true
60- return m.handleUserMsg(msg), tea.Batch(m.processLLM(), m.waitForLLMUpdate())
61+ turnCtx, turnCancel := context.WithCancel(m.ctx)
62+ m.turnCancel = turnCancel
63+ m.turnCtx = turnCtx
64+ mdl := m.handleUserMsg(msg)
65+ return mdl, tea.Batch(mdl.processLLM(), mdl.waitForLLMUpdate())
6166
6267 case LSPDiagnosticsMsg:
6368 return m.handleLSPDiag(msg), nil
@@ -165,6 +170,10 @@ func (m *Model) handleKeyPress(msg tea.KeyPressMsg) (Model, tea.Cmd, bool) {
165170 case "ctrl+shift+v":
166171 return *m, tea.ReadClipboard, true
167172 case "esc":
173+ if m.llmInFlight {
174+ m.cancelTurn()
175+ return *m, m.waitForLLMUpdate(), true
176+ }
168177 if m.focus == focusInput {
169178 m.agentInput.Blur()
170179 } else {
@@ -172,7 +181,7 @@ func (m *Model) handleKeyPress(msg tea.KeyPressMsg) (Model, tea.Cmd, bool) {
172181 }
173182 return *m, nil, true
174183 case "enter":
175- if m.focus == focusInput && m.agentInput.Value() != "" {
184+ if m.focus == focusInput && m.agentInput.Value() != "" && m.turnCancel == nil {
176185 userMsg := m.agentInput.Value()
177186 m.agentInput.Reset()
178187 return *m, m.sendToLLM(userMsg), true
@@ -232,6 +241,19 @@ func (m *Model) handleUserMsg(msg llmUserMsg) Model {
232241 // Finalization messages (assistant, tool result) flush immediately since
233242 // they clear streaming state.
234243 func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
244+ // Turn was cancelled — drain remaining messages until the goroutine
245+ // sends its terminal message (llmDoneMsg or llmErrorMsg), then stop.
246+ if !m.llmInFlight {
247+ for _, raw := range batch {
248+ switch raw.(type) {
249+ case llmDoneMsg, llmErrorMsg:
250+ m.turnCancel = nil
251+ m.turnCtx = nil
252+ return m, nil
253+ }
254+ }
255+ return m, m.waitForLLMUpdate()
256+ }
235257 for _, raw := range batch {
236258 switch msg := raw.(type) {
237259 case llmReasoningDeltaMsg:
@@ -256,6 +278,11 @@ func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
256278
257279 case llmErrorMsg:
258280 m.llmInFlight = false
281+ if m.turnCancel != nil {
282+ m.turnCancel()
283+ m.turnCancel = nil
284+ }
285+ m.turnCtx = nil
259286 m.lastNetError = msg.err.Error()
260287 m.clearStreaming()
261288 m.appendText("", m.styles.Error.Render("Error: "+msg.err.Error()), "")
@@ -269,6 +296,11 @@ func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
269296
270297 case llmDoneMsg:
271298 m.llmInFlight = false
299+ if m.turnCancel != nil {
300+ m.turnCancel()
301+ m.turnCancel = nil
302+ }
303+ m.turnCtx = nil
272304 m.lastNetError = ""
273305 m.demoteOldUndo()
274306 m.appendText("")
@@ -350,6 +382,21 @@ func (m *Model) applyAssistantMsg(msg llmAssistantMsg) {
350382 }
351383 }
352384
385+// cancelTurn gracefully interrupts the in-flight LLM turn.
386+// History, turn boundaries, file changes, and deltas are preserved
387+// so the user can undo via the message footer if desired.
388+// turnCancel is kept non-nil until the drain loop finishes (see
389+// handleLLMBatch) so the enter guard blocks new submissions.
390+func (m *Model) cancelTurn() {
391+ if m.turnCancel != nil {
392+ m.turnCancel()
393+ }
394+ m.llmInFlight = false
395+ m.clearStreaming()
396+ m.appendText("", m.styles.Dim.Render("(interrupted)"), "")
397+ m.scrollOffset = 0
398+}
399+
353400 // clearStreaming resets active streaming state.
354401 func (m *Model) clearStreaming() {
355402 if !m.streaming {