Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(chat): settle an async delegation card when its sub-agent finishes

M3gA-Mind3a7b2bb69d32
diff --git a/app/src/providers/ChatRuntimeProvider.tsx b/app/src/providers/ChatRuntimeProvider.tsx
index 5a66ce6fd9..a3d12409d6 100644
--- a/app/src/providers/ChatRuntimeProvider.tsx
+++ b/app/src/providers/ChatRuntimeProvider.tsx
@@ -965,6 +965,7 @@ const ChatRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
subagentDone({
threadId: event.thread_id,
rowId: `${event.thread_id}:subagent:${event.skill_id}:${event.tool_name}`,
+ taskId: event.skill_id,
success: event.success,
iterations: event.subagent?.iterations,
elapsedMs: event.subagent?.elapsed_ms,
diff --git a/app/src/providers/__tests__/liveHistoryParity.test.tsx b/app/src/providers/__tests__/liveHistoryParity.test.tsx
index 1e595d6e45..2a10365870 100644
--- a/app/src/providers/__tests__/liveHistoryParity.test.tsx
+++ b/app/src/providers/__tests__/liveHistoryParity.test.tsx
@@ -363,3 +363,131 @@ describe('live ≡ history rendering of one turn', () => {
expect(after.map(partKey)).toEqual(before.map(partKey));
});
});
+
+/**
+ * A detached (`async`) delegation outlives the turn that spawned it. Seen
+ * live: after the child finished and the background delivery posted its
+ * result, a "Delegated to … · async" card kept spinning with "Cancel task"
+ * under that delivery reply. The turn's `chat_done` hydrates the completed
+ * snapshot, whose async row carries the core's id (`subagent:<task>`), so the
+ * late `subagent_completed` (socket row id) missed it; the delivery then
+ * arrived as a bare `chat_done` and froze that stale row as its own trail.
+ */
+describe('async delegation settles after its turn', () => {
+ const spawnedStep = LIVE_TURN_STEPS.find(step => step.listener === 'onSubagentSpawned');
+ const doneStep = LIVE_TURN_STEPS.find(step => step.listener === 'onSubagentDone');
+ if (!spawnedStep || !doneStep) throw new Error('fixture lost its delegation');
+ const asyncSteps = LIVE_TURN_STEPS.filter(step => step !== doneStep).map(step =>
+ step === spawnedStep
+ ? ({
+ ...step,
+ event: { ...step.event, subagent: { mode: 'async', parent_call_id: 'call-spawn' } },
+ } as SocketStep)
+ : step
+ );
+ const DELIVERY_REQUEST = 'bgdeliver-1';
+ // The completed snapshot as the core persists it: same delegation, core ids.
+ const snapshot = (status: 'running' | 'success') => ({
+ threadId: TURN_THREAD,
+ requestId: TURN_REQUEST,
+ lifecycle: 'completed' as const,
+ iteration: 3,
+ maxIterations: 10,
+ streamingText: '',
+ thinking: '',
+ startedAt: '2026-09-28T11:03:05Z',
+ updatedAt: '2026-09-28T11:04:06Z',
+ toolTimeline: [
+ { id: 'call-spawn', name: 'spawn_subagent', status: 'success' as const, round: 2 },
+ {
+ id: 'subagent:sub-1',
+ name: 'subagent:researcher',
+ status,
+ round: 2,
+ subagent: {
+ taskId: 'sub-1',
+ agentId: 'researcher',
+ mode: 'async',
+ parentCallId: 'call-spawn',
+ toolCalls: [],
+ },
+ },
+ ],
+ });
+
+ /** Every delegation card on screen: `[message, settled status | 'running']`. */
+ function delegationCards(): [string | undefined, string][] {
+ return project().flatMap(message =>
+ typeof message.content === 'string'
+ ? []
+ : message.content.flatMap(part =>
+ part.type === 'tool-call' && part.toolName === 'task'
+ ? [
+ [
+ message.id,
+ (part.result as { status?: string } | undefined)?.status ?? 'running',
+ ] as [string | undefined, string],
+ ]
+ : []
+ )
+ );
+ }
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ store.dispatch(clearAllThreads());
+ store.dispatch(clearAllChatRuntime());
+ vi.mocked(threadApi.appendMessage).mockImplementation(async (_tid, message) => message);
+ vi.mocked(threadApi.getThreads).mockResolvedValue({ threads: [], count: 0 });
+ vi.mocked(threadApi.listRuns).mockResolvedValue([]);
+ vi.mocked(threadApi.generateTitleIfNeeded).mockResolvedValue({
+ id: TURN_THREAD,
+ title: 'Today',
+ } as never);
+ });
+
+ afterEach(() => {
+ store.dispatch(setStatusForUser({ userId: '__pending__', status: 'disconnected' }));
+ });
+
+ it('a late subagent_completed settles the card, and the delivery turn adds no spinner', async () => {
+ vi.mocked(threadApi.getTurnState).mockResolvedValue(snapshot('running') as never);
+ const listeners = renderProvider();
+ await store.dispatch(addMessageLocal({ threadId: TURN_THREAD, message: USER_MESSAGE }));
+ store.dispatch(beginInferenceTurn({ threadId: TURN_THREAD }));
+ for (const step of asyncSteps) fire(listeners, step);
+ act(() => listeners.onDone?.(DONE_EVENT));
+ // The completed snapshot (child still running) replaces the live rows.
+ await waitFor(() =>
+ expect(store.getState().chatRuntime.toolTimelineByThread[TURN_THREAD]?.[1]?.id).toBe(
+ 'subagent:sub-1'
+ )
+ );
+ expect(delegationCards()).toEqual([[`agent:${TURN_REQUEST}`, 'running']]);
+
+ // Minutes later the child finishes, then its result is delivered as a
+ // host-authored turn: a bare `chat_done`, no `inference_start`.
+ fire(listeners, doneStep);
+ vi.mocked(threadApi.getTurnState).mockResolvedValue(snapshot('success') as never);
+ act(() =>
+ listeners.onDone?.({
+ ...DONE_EVENT,
+ request_id: DELIVERY_REQUEST,
+ full_response: 'The research finished.',
+ })
+ );
+ await waitFor(() =>
+ expect(
+ store
+ .getState()
+ .thread.messagesByThreadId[
+ TURN_THREAD
+ ]?.some(message => message.id === `agent:${DELIVERY_REQUEST}`)
+ ).toBe(true)
+ );
+
+ const cards = delegationCards();
+ expect(cards).toContainEqual([`agent:${TURN_REQUEST}`, 'success']);
+ expect(cards.filter(([, status]) => status === 'running')).toEqual([]);
+ });
+});
diff --git a/app/src/store/chatRuntimeSlice.ts b/app/src/store/chatRuntimeSlice.ts
index 4ff43974f9..c39776366a 100644
--- a/app/src/store/chatRuntimeSlice.ts
+++ b/app/src/store/chatRuntimeSlice.ts
@@ -1921,6 +1921,8 @@ const chatRuntimeSlice = createSlice({
action: PayloadAction<{
threadId: string;
rowId: string;
+ /** The delegation's task id (`skill_id`); see the match below. */
+ taskId?: string;
success: boolean;
iterations?: number;
elapsedMs?: number;
@@ -1935,6 +1937,7 @@ const chatRuntimeSlice = createSlice({
const {
threadId,
rowId,
+ taskId,
success,
iterations,
elapsedMs,
@@ -1947,10 +1950,20 @@ const chatRuntimeSlice = createSlice({
// Settle a still-in-flight row: `running`, or `awaiting_user` (a subagent
// paused for input that then completes must not stay stuck at
// awaiting_user). Already-terminal rows are left as-is.
+ //
+ // Also matched by task id: a detached (`async`) child outlives its turn,
+ // and that turn's `chat_done` replaces the live timeline with the
+ // completed snapshot, whose row id is the core's `subagent:<task>`, not
+ // this socket `rowId`. Matching the id alone left that row running, and
+ // a background delivery (a `chat_done` with no `inference_start`) then
+ // froze it as its own trail: a card that spun forever under the reply
+ // announcing the child's result.
const entries = subagentRows(
state,
threadId,
- e => e.id === rowId && (e.status === 'running' || e.status === 'awaiting_user')
+ e =>
+ (e.id === rowId || (taskId !== undefined && e.subagent?.taskId === taskId)) &&
+ (e.status === 'running' || e.status === 'awaiting_user')
);
for (const entry of entries) {
entry.status = success ? 'success' : 'error';