From bb7b485f99cf9acad9dabc0e9908dacdce63993b Mon Sep 17 00:00:00 2001 From: dawn <90008@klbr.net> Date: Fri, 9 Oct 2026 23:00:31 +0300 Subject: [PATCH] tui: clear stopping state when session becomes idle Signed-off-by: dawn <90008@klbr.net> --- cli/internal/tui/chat.go | 1 + cli/internal/tui/chat_input.go | 4 +- cli/internal/tui/chat_operations.go | 14 +++ cli/internal/tui/chat_stopping_test.go | 131 +++++++++++++++++++++++++ cli/internal/tui/chat_stream.go | 1 + 5 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 cli/internal/tui/chat_stopping_test.go diff --git a/cli/internal/tui/chat.go b/cli/internal/tui/chat.go index 6e1632f9d..1cc2501e2 100644 --- a/cli/internal/tui/chat.go +++ b/cli/internal/tui/chat.go @@ -365,6 +365,7 @@ func (m ChatModel) update(msg tea.Msg) (ChatModel, tea.Cmd) { } m.refreshViewportContent() } + m.reconcileStopped() } home := m.hostHomeCmd() return m, tea.Batch(m.startAnimation(), m.statusPollCmd(), home) diff --git a/cli/internal/tui/chat_input.go b/cli/internal/tui/chat_input.go index e51b7945a..5e5342ab2 100644 --- a/cli/internal/tui/chat_input.go +++ b/cli/internal/tui/chat_input.go @@ -78,11 +78,11 @@ func (m *ChatModel) handleInput(msg tea.Msg) (tea.Cmd, bool) { if m.isSending { m.interruptDeferred = true m.Stopping = true - return nil, true + return m.startAnimation(), true } if m.Status.Running || len(m.pendingUsers) > 0 { m.Stopping = true - cmds = append(cmds, m.interruptCmd()) + cmds = append(cmds, m.interruptCmd(), m.startAnimation()) return tea.Batch(cmds...), true } case "ctrl+v": diff --git a/cli/internal/tui/chat_operations.go b/cli/internal/tui/chat_operations.go index 64219ab67..acaa068e2 100644 --- a/cli/internal/tui/chat_operations.go +++ b/cli/internal/tui/chat_operations.go @@ -114,6 +114,14 @@ func (m ChatModel) operationRecoverable(id string) bool { return exists && !pending.Expired } +func (m *ChatModel) reconcileStopped() { + if m.Stopping && !m.isSending && !m.interruptDeferred && m.pendingSendCount() == 0 && (!m.Status.Running || m.Status.Idle) { + m.Stopping = false + m.Stopped = true + m.clearAction() + } +} + func (m *ChatModel) applyInputReceipt(receipt daemon.InputReceipt) { m.Status.InputOrder = max(m.Status.InputOrder, receipt.InputOrder()) for i := range m.pendingUsers { @@ -122,12 +130,14 @@ func (m *ChatModel) applyInputReceipt(receipt daemon.InputReceipt) { if receipt.Settled() { m.pendingUsers = slices.Delete(m.pendingUsers, i, i+1) } + m.reconcileStopped() return } } if receipt.Settled() { delete(m.pendingContinuations, receipt.ID) } + m.reconcileStopped() } func (m *ChatModel) handleOperationResults(msg tea.Msg) (tea.Cmd, bool) { @@ -168,6 +178,7 @@ func (m *ChatModel) handleOperationResults(msg tea.Msg) (tea.Cmd, bool) { m.pendingUsers = slices.Delete(m.pendingUsers, index, index+1) } m.refreshViewportContent() + m.reconcileStopped() return nil, true } if msg.Err == nil { @@ -249,6 +260,8 @@ func (m *ChatModel) handleOperationResults(msg tea.Msg) (tea.Cmd, bool) { m.interruptDeferred = false m.Stopping = true cmds = append(cmds, m.interruptCmd()) + } else { + m.reconcileStopped() } return tea.Batch(cmds...), true case ChatInterruptMsg: @@ -261,6 +274,7 @@ func (m *ChatModel) handleOperationResults(msg tea.Msg) (tea.Cmd, bool) { m.appendSettledEntry(HistoryEntry{Kind: EntryError, Text: "Could not stop the current reply: " + msg.Err.Error()}) } } + m.reconcileStopped() return nil, true } return nil, false diff --git a/cli/internal/tui/chat_stopping_test.go b/cli/internal/tui/chat_stopping_test.go new file mode 100644 index 000000000..1d817e72e --- /dev/null +++ b/cli/internal/tui/chat_stopping_test.go @@ -0,0 +1,131 @@ +package tui + +import ( + "strings" + "testing" + "time" + + "albedo/cli/internal/daemon" + tea "charm.land/bubbletea/v2" +) + +func TestEscapeStoppingCompactionReconcilesWhenIdle(t *testing.T) { + m := newTestChatModel(t, &daemon.Session{ID: "s"}) + m.SetSize(80, 30) + + phaseCompacting := daemon.PhaseCompacting + m.Status.Running, m.Status.Idle, m.Status.Phase = true, false, &phaseCompacting + + // Press esc to stop compaction + var cmd tea.Cmd + m, cmd = m.Update(tea.KeyPressMsg{Code: tea.KeyEscape}) + if cmd == nil { + t.Fatal("expected interrupt command on esc") + } + if !m.Stopping { + t.Fatal("expected Stopping to be true after pressing esc") + } + if !strings.Contains(m.statusLine(), "stopping…") { + t.Fatalf("expected statusLine to show stopping…, got %q", m.statusLine()) + } + + // Daemon finishes interrupting compaction and emits status idle + phaseIdle := daemon.PhaseIdle + m.handleStreamEvent(daemon.StreamEvent{ + Type: "status", + Status: &daemon.AgentStatus{ + Running: false, + Idle: true, + Phase: &phaseIdle, + }, + }) + + if m.Stopping { + t.Fatal("expected Stopping to be false after status became idle") + } + if !m.Stopped { + t.Fatal("expected Stopped to be true after stopping was reconciled") + } + if strings.Contains(m.statusLine(), "stopping…") { + t.Fatalf("statusLine still shows stopping… after idle: %q", m.statusLine()) + } + if m.animating() { + t.Fatal("expected animating to be false after stopping completed and session is idle") + } +} + +func TestEscapeStoppingPendingUsersReconcilesWhenSettled(t *testing.T) { + m := newTestChatModel(t, &daemon.Session{ID: "s"}) + m.SetSize(80, 30) + m.Status.Running, m.Status.Idle = false, true + m.pendingUsers = []PendingUserTurn{ + {OperationID: "op-1", Text: "queued message", At: time.Now().UnixMilli()}, + } + + // Press esc to cancel queued input + m, _ = m.Update(tea.KeyPressMsg{Code: tea.KeyEscape}) + if !m.Stopping { + t.Fatal("expected Stopping to be true after pressing esc with pending users") + } + + // Status event arrives while input is still pending: Stopping remains true + phaseIdle := daemon.PhaseIdle + m.handleStreamEvent(daemon.StreamEvent{ + Type: "status", + Status: &daemon.AgentStatus{ + Running: false, + Idle: true, + Phase: &phaseIdle, + }, + }) + if !m.Stopping { + t.Fatal("expected Stopping to stay true while pendingUsers still contains items") + } + + // Receipt settles the input turn + cancelled := "cancelled" + m.handleStreamEvent(daemon.StreamEvent{ + Type: "input", + Receipt: &daemon.InputReceipt{ + ID: "op-1", + Delivery: &cancelled, + }, + }) + + if m.Stopping { + t.Fatal("expected Stopping to be false once pending input is settled") + } + if !m.Stopped { + t.Fatal("expected Stopped to be true after pending input is settled") + } + if m.animating() { + t.Fatal("expected animating to be false") + } +} + +func TestChatStatusPollReconcilesStopping(t *testing.T) { + m := newTestChatModel(t, &daemon.Session{ID: "s"}) + m.SetSize(80, 30) + m.Stopping = true + m.Status.Running, m.Status.Idle = true, false + + phaseIdle := daemon.PhaseIdle + status := daemon.AgentStatus{ + Running: false, + Idle: true, + Phase: &phaseIdle, + } + m, _ = m.Update(ChatStatusMsg{ + SessionID: m.SessionID, + Generation: m.Generation, + Revision: m.statusRevision, + Status: &status, + }) + + if m.Stopping { + t.Fatal("expected Stopping to be false after ChatStatusMsg reported idle") + } + if !m.Stopped { + t.Fatal("expected Stopped to be true") + } +} diff --git a/cli/internal/tui/chat_stream.go b/cli/internal/tui/chat_stream.go index c9a86ff01..8d8ec7a35 100644 --- a/cli/internal/tui/chat_stream.go +++ b/cli/internal/tui/chat_stream.go @@ -166,6 +166,7 @@ func (m *ChatModel) handleStreamEvent(evt daemon.StreamEvent) { m.Status.Running = evt.Status.Running m.Status.Idle = evt.Status.Idle m.Status.RunID = evt.Status.RunID + m.reconcileStopped() } case "input", daemon.EventTurnMembership: if evt.Receipt != nil { -- knotmirror 0.0.1