Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(chat): deleting a chat aborts its in-flight generation/agent (Bug C)

purple2bf6b280fb31
diff --git a/src/hooks/useAgentChat.ts b/src/hooks/useAgentChat.ts
index c8c5de19..d228dfd9 100644
--- a/src/hooks/useAgentChat.ts
+++ b/src/hooks/useAgentChat.ts
@@ -458,6 +458,8 @@ export function useAgentChat() {
// Bind the generating flag to THIS conversation so the typing indicator
// shows only in the chat whose turn is in flight (David 2026-06-12).
useGenerationStore.getState().setGenerating(convId, true)
+ // Register so deleting/closing this chat stops the agent loop (Bug C).
+ useGenerationStore.getState().registerAborter(convId, () => { runningRef.current = false; abort.abort() })
contentRef.current = ''
thinkingRef.current = ''
blocksRef.current = []
@@ -1294,6 +1296,7 @@ export function useAgentChat() {
} finally {
setIsAgentRunning(false)
useGenerationStore.getState().setGenerating(convId, false)
+ useGenerationStore.getState().clearAborter(convId)
runningRef.current = false
abortRef.current = null
// Chat-tools artifact mode: attach any files the model "wrote" (captured
diff --git a/src/hooks/useChat.ts b/src/hooks/useChat.ts
index c1a682d6..7fed663f 100644
--- a/src/hooks/useChat.ts
+++ b/src/hooks/useChat.ts
@@ -190,6 +190,8 @@ export function useChat() {
const abort = new AbortController()
abortRef.current = abort
+ // Register so deleting/closing this chat aborts the in-flight stream (Bug C).
+ useGenerationStore.getState().registerAborter(convId, () => abort.abort())
setIsGenerating(true)
// Bind the generating flag to THIS conversation so the typing indicator
// only shows in the chat whose turn is in flight — not in every other chat
@@ -427,6 +429,7 @@ export function useChat() {
} finally {
setIsGenerating(false)
useGenerationStore.getState().setGenerating(convId, false)
+ useGenerationStore.getState().clearAborter(convId)
setIsLoadingModel(false)
useModelStore.getState().setIsModelLoading(false)
abortRef.current = null
diff --git a/src/stores/chatStore.ts b/src/stores/chatStore.ts
index db09dbd8..58646010 100644
--- a/src/stores/chatStore.ts
+++ b/src/stores/chatStore.ts
@@ -5,6 +5,7 @@ import type { Conversation, Message, ChatArtifact } from '../types/chat'
import type { AgentBlock } from '../types/agent-mode'
import { idbStorage } from '../lib/idbStorage'
import { migrateBlockInPlace } from '../api/agents/block-helpers'
+import { useGenerationStore } from './generationStore'
/**
* Rehydration migration for Phase 1 (v2.4.0) — wraps legacy
@@ -92,12 +93,18 @@ export const useChatStore = create<ChatState>()(
return id
},
- deleteConversation: (id) =>
+ deleteConversation: (id) => {
+ // Stop any in-flight turn (chat stream OR agent loop) for this chat
+ // BEFORE dropping it, so deleting/closing a chat halts its activity
+ // completely — no orphaned stream burning tokens / GPU after the chat
+ // is gone (David 2026-06-15).
+ try { useGenerationStore.getState().abortConversation(id) } catch { /* best-effort */ }
set((state) => ({
conversations: state.conversations.filter((c) => c.id !== id),
activeConversationId:
state.activeConversationId === id ? null : state.activeConversationId,
- })),
+ }))
+ },
renameConversation: (id, title) =>
set((state) => ({
diff --git a/src/stores/generationStore.ts b/src/stores/generationStore.ts
index 1ed917b5..323e2fda 100644
--- a/src/stores/generationStore.ts
+++ b/src/stores/generationStore.ts
@@ -22,10 +22,25 @@ interface GenerationState {
/** conversationId → true while its turn is generating. Absent = idle. */
generating: Record<string, boolean>
setGenerating: (conversationId: string | null | undefined, on: boolean) => void
+ /**
+ * conversationId → abort callback for the in-flight turn (chat stream OR
+ * agent loop). Lets a non-hook caller (deleting/closing a chat) stop the
+ * work that the owning hook started.
+ */
+ aborters: Record<string, () => void>
+ registerAborter: (conversationId: string | null | undefined, fn: () => void) => void
+ clearAborter: (conversationId: string | null | undefined) => void
+ /**
+ * Abort the in-flight turn for a conversation and clear its flags. Called
+ * when a chat is deleted/closed so its activity stops completely instead of
+ * running on in the background (David 2026-06-15).
+ */
+ abortConversation: (conversationId: string | null | undefined) => void
}
-export const useGenerationStore = create<GenerationState>((set) => ({
+export const useGenerationStore = create<GenerationState>((set, get) => ({
generating: {},
+ aborters: {},
setGenerating: (conversationId, on) =>
set((state) => {
if (!conversationId) return state
@@ -37,4 +52,33 @@ export const useGenerationStore = create<GenerationState>((set) => ({
else delete next[conversationId]
return { generating: next }
}),
+
+ registerAborter: (conversationId, fn) =>
+ set((state) => {
+ if (!conversationId) return state
+ return { aborters: { ...state.aborters, [conversationId]: fn } }
+ }),
+
+ clearAborter: (conversationId) =>
+ set((state) => {
+ if (!conversationId || !state.aborters[conversationId]) return state
+ const next = { ...state.aborters }
+ delete next[conversationId]
+ return { aborters: next }
+ }),
+
+ abortConversation: (conversationId) => {
+ if (!conversationId) return
+ const fn = get().aborters[conversationId]
+ if (fn) {
+ try { fn() } catch { /* best-effort — the turn is going away anyway */ }
+ }
+ set((state) => {
+ const nextAborters = { ...state.aborters }
+ delete nextAborters[conversationId]
+ const nextGenerating = { ...state.generating }
+ delete nextGenerating[conversationId]
+ return { aborters: nextAborters, generating: nextGenerating }
+ })
+ },
}))