Skip to content

The agent that grows with you

fix(desktop): Stop keeps the rows of background subagents that outlive the turn

Teknium5c1484f75922
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
index df297c8e4ae..ab4848c148b 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
@@ -31,6 +31,7 @@ import {
setSessions
} from '@/store/session'
import { dropSessionState, publishSessionState } from '@/store/session-states'
+import { $subagentsBySession, upsertSubagent } from '@/store/subagents'
import { $wakeWord, resetWakeWordState } from '@/store/wake-word'
import { actRender, Harness, type HarnessHandle, RUNTIME_SESSION_ID, sessionInfo } from './index-test-utils'
@@ -5155,12 +5156,18 @@ describe('usePromptActions background processes: interrupt vs rewind', () => {
return handle!
}
- it('cancelRun interrupts the turn but leaves running background processes alone', async () => {
+ it('cancelRun interrupts the turn but leaves running background processes and subagents alone', async () => {
const requestGateway = vi.fn(async () => ({}) as never)
const handle = await mount(requestGateway)
+ upsertSubagent(RUNTIME_SESSION_ID, { goal: 'review', status: 'running', subagent_id: 'bg-review', task_index: 0 })
+ upsertSubagent(RUNTIME_SESSION_ID, { goal: 'old', status: 'completed', subagent_id: 'done-1', task_index: 1 })
await handle.cancelRun()
+ expect(($subagentsBySession.get()[RUNTIME_SESSION_ID] ?? []).map(row => [row.id, row.status])).toEqual([
+ ['bg-review', 'running']
+ ])
+
expect(requestGateway).toHaveBeenCalledWith('session.interrupt', { session_id: RUNTIME_SESSION_ID })
expect(requestGateway).not.toHaveBeenCalledWith('process.kill', expect.anything())
expect(killed()).toBe(false)
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
index 85b4366f857..ed79cc74fb2 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
@@ -42,7 +42,7 @@ import {
} from '@/store/session'
import { $sessionStates, isLiveTurnAwaitingEvents, isSessionRemote } from '@/store/session-states'
import { flushStreamDeltas } from '@/store/stream-delta-flush'
-import { clearSessionSubagents } from '@/store/subagents'
+import { pruneFinishedSessionSubagents } from '@/store/subagents'
import { runGatewayRestart } from '@/store/system-actions'
import { clearSessionTodos } from '@/store/todos'
import { setSessionDraftingTool } from '@/store/tool-drafting'
@@ -800,7 +800,9 @@ export function usePromptActions({
})
clearSessionTodos(sessionId)
- clearSessionSubagents(sessionId)
+ // Stop ends this turn only: background subagents keep running and keep their rows; the
+ // stopped turn's own children drop out of the next roster snapshot.
+ pruneFinishedSessionSubagents(sessionId)
// No resetSessionBackground() here. A plain interrupt (Stop, or "send now"
// on a queued message while busy) keeps the timeline, and the gateway's
// session.interrupt deliberately leaves terminal(background=true) processes
diff --git a/apps/desktop/src/store/subagents.ts b/apps/desktop/src/store/subagents.ts
index 9ae2d4c38ca..3f3bdf5d55f 100644
--- a/apps/desktop/src/store/subagents.ts
+++ b/apps/desktop/src/store/subagents.ts
@@ -336,8 +336,8 @@ export function clearSessionSubagents(sid: string) {
* from the display while background subagents that outlived the spawning turn
* remain visible (and still accept late progress/complete events).
*
- * Distinct from `clearSessionSubagents` (used by the Stop action, which
- * genuinely cancels running subagents and so should drop them all) and from
+ * Also what the Stop action uses: Stop ends the turn, not the background
+ * subagents it spawned. Distinct from `clearSessionSubagents` (drops every row) and from
* `pruneDelegateFallbackSubagents` (which filters by id prefix to remove
* placeholder rows once the real native event arrives).
*/