Skip to content

The agent that grows with you

fix(desktop): clear goals and background status with a deleted session's subagents and todos

kshitijk4poor29757dab7b1c
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
index a8323befec2..23d3cc2ab49 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
@@ -24,12 +24,14 @@ import {
import { createClientSessionState } from '@/lib/chat-runtime'
import { $clarifyRequests, clearClarifyRequest, setClarifyRequest } from '@/store/clarify'
import { clearSessionDraft, stashSessionDraft, takeSessionDraft } from '@/store/composer'
+import { $backgroundStatusBySession, type ComposerStatusItem } from '@/store/composer-status'
import {
activeGatewayConnectionId,
requestGatewayForAgent,
requestGatewayForProfile,
retainGatewayForAgent
} from '@/store/gateway'
+import { $goalsBySession, type SessionGoal } from '@/store/goals'
import { $pinnedSessionIds } from '@/store/layout'
import { $notifications, dismissNotification } from '@/store/notifications'
import {
@@ -554,22 +556,29 @@ describe('connection-qualified session deletion', () => {
expect($sessions.get().some(session => session.id === 'background-session')).toBe(true)
})
- describe('subagent and todo cleanup', () => {
+ describe('subagent, todo, goal, and background cleanup', () => {
// Stored id, foreground runtime, and the stored→runtime mapping are all
- // distinct: subagent/todo stores key on the event's runtime session_id.
+ // distinct: these live stores key on the event's runtime session_id.
const doomedIds = ['stored-doomed', 'runtime-active', 'runtime-mapped']
const seedLiveState = () => {
const subagents: Record<string, SubagentProgress[]> = {}
const todos: Record<string, { content: string; id: string; status: 'pending' }[]> = {}
+ const goals: Record<string, SessionGoal> = {}
+ const background: Record<string, ComposerStatusItem[]> = {}
for (const sid of [...doomedIds, 'runtime-other']) {
subagents[sid] = [{ id: `sub-${sid}` } as SubagentProgress]
todos[sid] = [{ content: 'todo', id: `todo-${sid}`, status: 'pending' }]
+ goals[sid] = { status: 'active', title: 'goal', updatedAt: 1 }
+ // Finished (not running) so resetSessionBackground has nothing to kill.
+ background[sid] = [{ id: `proc-${sid}`, state: 'done', title: 'proc', type: 'background' }]
}
$subagentsBySession.set(subagents)
$todosBySession.set(todos)
+ $goalsBySession.set(goals)
+ $backgroundStatusBySession.set(background)
}
const renderDeleting = async () => {
@@ -597,9 +606,11 @@ describe('connection-qualified session deletion', () => {
afterEach(() => {
$subagentsBySession.set({})
$todosBySession.set({})
+ $goalsBySession.set({})
+ $backgroundStatusBySession.set({})
})
- it('clears subagents and todos under the stored and every runtime id once the delete lands', async () => {
+ it('clears subagents, todos, goals, and background status under the stored and every runtime id once the delete lands', async () => {
seedLiveState()
vi.mocked(deleteSession).mockResolvedValue({ ok: true })
@@ -607,9 +618,11 @@ describe('connection-qualified session deletion', () => {
expect(Object.keys($subagentsBySession.get())).toEqual(['runtime-other'])
expect(Object.keys($todosBySession.get())).toEqual(['runtime-other'])
+ expect(Object.keys($goalsBySession.get())).toEqual(['runtime-other'])
+ expect(Object.keys($backgroundStatusBySession.get())).toEqual(['runtime-other'])
})
- it('keeps subagents and todos when the delete RPC fails and the row is restored', async () => {
+ it('keeps subagents, todos, goals, and background status when the delete RPC fails and the row is restored', async () => {
seedLiveState()
vi.mocked(deleteSession).mockRejectedValue(new Error('delete failed'))
@@ -618,6 +631,8 @@ describe('connection-qualified session deletion', () => {
expect($sessions.get().some(session => session.id === 'stored-doomed')).toBe(true)
expect(Object.keys($subagentsBySession.get()).sort()).toEqual([...doomedIds, 'runtime-other'].sort())
expect(Object.keys($todosBySession.get()).sort()).toEqual([...doomedIds, 'runtime-other'].sort())
+ expect(Object.keys($goalsBySession.get()).sort()).toEqual([...doomedIds, 'runtime-other'].sort())
+ expect(Object.keys($backgroundStatusBySession.get()).sort()).toEqual([...doomedIds, 'runtime-other'].sort())
})
})
})
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index be996a8ae1c..ed4d7f40276 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -35,6 +35,7 @@ import { setSessionYolo } from '@/lib/yolo-session'
import { $clarifyRequests, clearClarifyRequest } from '@/store/clarify'
import { announceGoneSessionDraft, announceNewSessionDraftKey, migrateSessionDraft } from '@/store/composer'
import { clearQueuedPrompts, migrateQueuedPrompts } from '@/store/composer-queue'
+import { resetSessionBackground } from '@/store/composer-status'
import { $connectionRequests } from '@/store/connection-request'
import {
$gateway,
@@ -45,6 +46,7 @@ import {
retainGatewayForAgent
} from '@/store/gateway'
import { $gatewaySwitching } from '@/store/gateway-switch'
+import { clearSessionGoal } from '@/store/goals'
import { $pinnedSessionIds } from '@/store/layout'
import { clearNotifications, notify, notifyError } from '@/store/notifications'
import {
@@ -3290,8 +3292,6 @@ export function useSessionActions({
// back, and a rolled-back row must keep its watermark/marker.
forgetSessionUnread(removedIds, profile)
clearQueuedPrompts(storedSessionId)
- clearSessionSubagents(storedSessionId)
- clearSessionTodos(storedSessionId)
// The journaled in-flight tail holds this session's prompt and tool
// calls in localStorage; a deleted session must not leave that copy
// behind to age out on its own. Purge after the RPC lands (same
@@ -3303,8 +3303,6 @@ export function useSessionActions({
if (closingRuntimeId) {
clearQueuedPrompts(closingRuntimeId)
clearSessionControl(closingRuntimeId)
- clearSessionSubagents(closingRuntimeId)
- clearSessionTodos(closingRuntimeId)
}
// A tiled copy of this session must not outlive it: collapse the pane
@@ -3317,12 +3315,18 @@ export function useSessionActions({
runtimeIdByStoredSessionIdRef.current.delete(storedSessionId)
sessionStateByRuntimeIdRef.current.delete(tiledRuntimeId)
dropSessionState(tiledRuntimeId)
- // Subagent/todo stores key on the gateway event's session_id, i.e.
- // the runtime id. When the deleted row is selected, closingRuntimeId
- // is the foreground runtime, which can differ from the stored→runtime
- // mapping (cached/tiled runtime), so clear the mapped id as well.
- clearSessionSubagents(tiledRuntimeId)
- clearSessionTodos(tiledRuntimeId)
+ }
+
+ // Live per-session stores (the same four the stop paths clear) key on
+ // the gateway event's session_id, i.e. the runtime id. When the deleted
+ // row is selected, closingRuntimeId is the foreground runtime, which can
+ // differ from the stored→runtime mapping (cached/tiled runtime), so
+ // clear the stored id and both runtime ids — once per distinct id.
+ for (const sid of new Set([storedSessionId, closingRuntimeId, tiledRuntimeId].filter(Boolean) as string[])) {
+ clearSessionSubagents(sid)
+ clearSessionTodos(sid)
+ clearSessionGoal(sid)
+ resetSessionBackground(sid)
}
} catch (err) {
if (listed?.session) {