Skip to content

The agent that grows with you

fix(desktop): clear subagents/todos under the mapped runtime id on delete

kshitijk4poor5edd4ddf38f9
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 8d1e4522bce..a8323befec2 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
@@ -101,7 +101,8 @@ import {
sessionTileOwnerRoute
} from '@/store/session-states'
import { $sessionSeenCounts, $unreadFinishedMarkers } from '@/store/session-unread'
-import { $retainedTodosBySession, clearSessionTodos } from '@/store/todos'
+import { $subagentsBySession, type SubagentProgress } from '@/store/subagents'
+import { $retainedTodosBySession, $todosBySession, clearSessionTodos } from '@/store/todos'
import { loadTranscriptTail, saveTranscriptTail } from '@/store/transcript-tail-cache'
import sessionResumeActiveTurn from '../../../../../../tests/fixtures/session-resume-active-turn.json'
@@ -552,6 +553,73 @@ describe('connection-qualified session deletion', () => {
expect(updateSessionState.mock.results.at(-1)?.value).toMatchObject({ interrupted: false, needsInput: true })
expect($sessions.get().some(session => session.id === 'background-session')).toBe(true)
})
+
+ describe('subagent and todo cleanup', () => {
+ // Stored id, foreground runtime, and the stored→runtime mapping are all
+ // distinct: subagent/todo 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' }[]> = {}
+
+ for (const sid of [...doomedIds, 'runtime-other']) {
+ subagents[sid] = [{ id: `sub-${sid}` } as SubagentProgress]
+ todos[sid] = [{ content: 'todo', id: `todo-${sid}`, status: 'pending' }]
+ }
+
+ $subagentsBySession.set(subagents)
+ $todosBySession.set(todos)
+ }
+
+ const renderDeleting = async () => {
+ let actions: HarnessHandle | null = null
+
+ setSessions([storedSession({ id: 'stored-doomed' })])
+ render(
+ <Harness
+ activeSessionId="runtime-active"
+ onReady={value => {
+ actions = value
+ }}
+ requestGateway={vi.fn().mockResolvedValue({})}
+ runtimeIdByStoredSessionIdRef={{ current: new Map([['stored-doomed', 'runtime-mapped']]) }}
+ selectedStoredSessionId="stored-doomed"
+ />
+ )
+ await waitFor(() => expect(actions).not.toBeNull())
+
+ await act(async () => {
+ await actions?.removeSession('stored-doomed')
+ })
+ }
+
+ afterEach(() => {
+ $subagentsBySession.set({})
+ $todosBySession.set({})
+ })
+
+ it('clears subagents and todos under the stored and every runtime id once the delete lands', async () => {
+ seedLiveState()
+ vi.mocked(deleteSession).mockResolvedValue({ ok: true })
+
+ await renderDeleting()
+
+ expect(Object.keys($subagentsBySession.get())).toEqual(['runtime-other'])
+ expect(Object.keys($todosBySession.get())).toEqual(['runtime-other'])
+ })
+
+ it('keeps subagents and todos when the delete RPC fails and the row is restored', async () => {
+ seedLiveState()
+ vi.mocked(deleteSession).mockRejectedValue(new Error('delete failed'))
+
+ await renderDeleting()
+
+ 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())
+ })
+ })
})
function StoredIdRotationHarness({
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 664070bce75..be996a8ae1c 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
@@ -3317,6 +3317,12 @@ 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)
}
} catch (err) {
if (listed?.session) {