Skip to content

The agent that grows with you

fix(desktop): keep session marked in-progress during async delegation

razultull6202fca05915
diff --git a/apps/desktop/src/store/session-dot-state.test.ts b/apps/desktop/src/store/session-dot-state.test.ts
index 5e81c3201b1..5f9120a3915 100644
--- a/apps/desktop/src/store/session-dot-state.test.ts
+++ b/apps/desktop/src/store/session-dot-state.test.ts
@@ -1,6 +1,11 @@
-import { describe, expect, it } from 'vitest'
+import { afterEach, describe, expect, it } from 'vitest'
-import { hasLiveTurn, showsRunningArc } from './session-dot-state'
+import { createClientSessionState } from '@/lib/chat-runtime'
+
+import { $sessions } from './session'
+import { $delegatingSessionIds, hasLiveTurn, showsRunningArc } from './session-dot-state'
+import { clearAllSessionStates, publishSessionState } from './session-states'
+import { $subagentsBySession, type SubagentProgress } from './subagents'
describe('showsRunningArc', () => {
it('keeps the arc when an authoritative turn goes quiet', () => {
@@ -35,3 +40,46 @@ describe('hasLiveTurn', () => {
expect(hasLiveTurn('unread')).toBe(false)
})
})
+
+describe('$delegatingSessionIds', () => {
+ const subagent = (status: SubagentProgress['status']): SubagentProgress => ({
+ id: 'sub-1',
+ parentId: null,
+ goal: 'do a thing',
+ status,
+ taskCount: 1,
+ taskIndex: 0,
+ startedAt: 0,
+ updatedAt: 0,
+ filesRead: [],
+ filesWritten: [],
+ stream: []
+ })
+
+ afterEach(() => {
+ clearAllSessionStates()
+ $subagentsBySession.set({})
+ $sessions.set([])
+ })
+
+ it('claims the stored id while a subagent is running after the parent turn ended', () => {
+ publishSessionState('runtime-1', { ...createClientSessionState('stored-1'), busy: false })
+ $subagentsBySession.set({ 'runtime-1': [subagent('running')] })
+
+ expect($delegatingSessionIds.get()).toContain('stored-1')
+ })
+
+ it('drops the session once every subagent reaches a terminal status', () => {
+ publishSessionState('runtime-1', { ...createClientSessionState('stored-1'), busy: false })
+ $subagentsBySession.set({ 'runtime-1': [subagent('running')] })
+ $subagentsBySession.set({ 'runtime-1': [subagent('completed')] })
+
+ expect($delegatingSessionIds.get()).not.toContain('stored-1')
+ })
+
+ it('falls back to the runtime id for a not-yet-persisted conversation', () => {
+ $subagentsBySession.set({ 'runtime-fresh': [subagent('queued')] })
+
+ expect($delegatingSessionIds.get()).toContain('runtime-fresh')
+ })
+})
diff --git a/apps/desktop/src/store/session-dot-state.ts b/apps/desktop/src/store/session-dot-state.ts
index bf94a1658ed..7a83e7faa33 100644
--- a/apps/desktop/src/store/session-dot-state.ts
+++ b/apps/desktop/src/store/session-dot-state.ts
@@ -19,11 +19,46 @@
import { computed } from 'nanostores'
-import { stableRecord } from '@/lib/stable-array'
+import { stableArray, stableRecord } from '@/lib/stable-array'
import { $backgroundRunningSessionIds } from './composer-status'
import { $sessions, $unreadFinishedSessionIds, lineageAliases } from './session'
-import { $attentionSessionIds, $draftSessionIds, $stalledSessionIds, $workingSessionIds } from './session-states'
+import {
+ $attentionSessionIds,
+ $draftSessionIds,
+ $sessionStates,
+ $stalledSessionIds,
+ $workingSessionIds
+} from './session-states'
+import { $subagentsBySession, activeSubagentCount } from './subagents'
+
+// Sessions parked in async delegation: the parent turn has ended (busy=false —
+// delegate_task(background=true) returns its handle the moment the children
+// are spawned) while those subagents keep working for minutes. Without this
+// input the sidebar row dropped to a plain idle dot mid-delegation, reading as
+// "done" while work was still running in child sessions. Same runtime→stored
+// bridge and fresh-chat fallback as $backgroundRunningSessionIds:
+// $subagentsBySession is keyed by runtime id, surfaces key on stored ids, and
+// lineageAliases covers whichever tip of the conversation a surface holds.
+let delegatingIds: readonly string[] = []
+export const $delegatingSessionIds = computed(
+ [$subagentsBySession, $sessionStates, $sessions],
+ (bySession, states, sessions) => {
+ const ids = new Set<string>()
+
+ for (const [runtimeId, items] of Object.entries(bySession)) {
+ if (activeSubagentCount(items) === 0) {
+ continue
+ }
+
+ for (const alias of lineageAliases(states[runtimeId]?.storedSessionId ?? runtimeId, sessions)) {
+ ids.add(alias)
+ }
+ }
+
+ return (delegatingIds = stableArray(delegatingIds, [...ids]))
+ }
+)
export type SessionDotState = 'background' | 'draft' | 'idle' | 'needs-input' | 'stalled' | 'unread' | 'working'
@@ -63,11 +98,12 @@ export const $sessionDotStateById = computed(
$workingSessionIds,
$stalledSessionIds,
$backgroundRunningSessionIds,
+ $delegatingSessionIds,
$unreadFinishedSessionIds,
$draftSessionIds,
$sessions
],
- (attention, working, stalled, background, unread, draft, sessions) => {
+ (attention, working, stalled, background, delegating, unread, draft, sessions) => {
const next: Record<string, SessionDotState> = {}
const claim = (ids: readonly string[], state: SessionDotState) => {
@@ -87,6 +123,11 @@ export const $sessionDotStateById = computed(
claim(draft, 'draft')
claim(unread, 'unread')
claim(background, 'background')
+ // Async delegation: the parent turn has ended but its subagents are still
+ // running, so the session's work continues in child sessions. Same visual
+ // claim as background processes — and it yields to `working` below the
+ // moment the parent turn itself is live (synchronous orchestrator children).
+ claim(delegating, 'background')
claim(working, 'working')
// Stalled REFINES working rather than rivalling it — the turn is still