Skip to content

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

fix(chat): settle a delegation's nested status and restored rows on done

M3gA-Mind721161715ac9
diff --git a/app/src/store/__tests__/chatRuntimeSlice.test.ts b/app/src/store/__tests__/chatRuntimeSlice.test.ts
index 0f63658853..96c6b611ec 100644
--- a/app/src/store/__tests__/chatRuntimeSlice.test.ts
+++ b/app/src/store/__tests__/chatRuntimeSlice.test.ts
@@ -34,6 +34,7 @@ import reducer, {
toolArgsDeltaReceived,
toolCallReceived,
toolResultReceived,
+ turnSettled,
upsertArtifactFailedForThread,
upsertArtifactInProgressForThread,
upsertArtifactReadyForThread,
@@ -1457,24 +1458,79 @@ describe('subagent event reducers (Phase 3)', () => {
expect(again.toolTimelineByThread['t1'][0].status).toBe('success');
});
- it('subagentDone also settles the hydrated core row of the same delegation, and only it', () => {
- // After its turn settles, an async delegation's live row is the completed
- // snapshot's, under the core's id. A row that merely carries the same task
- // id under any other id is a different card (`continue_subagent` reuses
- // task ids) and must not take this run's outcome.
- const row = (id: string) => ({
- id,
- name: 'subagent:researcher',
- round: 0,
- seq: 0,
- status: 'running' as const,
- subagent: { taskId: 'task-1', agentId: 'researcher', toolCalls: [] },
- });
- const state = reducer(
+ it('subagentDone settles the delegation under both of its ids, across turns', () => {
+ // After its turn settles, an async delegation lives twice: in the frozen
+ // trail under the socket row id, and in the live timeline as the completed
+ // snapshot's row under the core id. A paused card from an earlier turn is
+ // the same delegation when `continue_subagent` resumes it (same task id),
+ // and must stop asking once that run finishes.
+ const socketRow = 't1:subagent:task-1:researcher';
+ let state = reducer(
undefined,
+ subagentSpawned({
+ threadId: 't1',
+ round: 0,
+ rowId: socketRow,
+ taskId: 'task-1',
+ agentId: 'researcher',
+ })
+ );
+ state = reducer(state, subagentAwaitingUser({ threadId: 't1', rowId: socketRow }));
+ state = reducer(state, turnSettled({ threadId: 't1', requestId: 'req-old' }));
+ state = reducer(
+ state,
setToolTimelineForThread({
threadId: 't1',
- entries: [row('subagent:task-1'), row('earlier-card')],
+ entries: [
+ {
+ id: 'subagent:task-1',
+ name: 'subagent:researcher',
+ round: 0,
+ seq: 0,
+ status: 'running',
+ subagent: { taskId: 'task-1', agentId: 'researcher', status: 'running', toolCalls: [] },
+ },
+ {
+ id: 'subagent:task-2',
+ name: 'subagent:researcher',
+ round: 0,
+ seq: 1,
+ status: 'running',
+ subagent: { taskId: 'task-2', agentId: 'researcher', toolCalls: [] },
+ },
+ ],
+ })
+ );
+ const done = reducer(
+ state,
+ subagentDone({ threadId: 't1', rowId: socketRow, taskId: 'task-1', success: true })
+ );
+ const frozen = done.settledTurnsByThread['t1']['req-old'].timeline[0];
+ expect([frozen.status, frozen.subagent?.status]).toEqual(['success', 'completed']);
+ expect(done.toolTimelineByThread['t1'].map(e => [e.id, e.status, e.subagent?.status])).toEqual([
+ ['subagent:task-1', 'success', 'completed'],
+ // Another delegation's row is untouched.
+ ['subagent:task-2', 'running', undefined],
+ ]);
+ });
+
+ it('subagentDone also settles the delegation in a restored past-turn timeline', () => {
+ const state = reducer(
+ undefined,
+ setTurnTimelinesForThread({
+ threadId: 't1',
+ timelines: {
+ 'req-old': [
+ {
+ id: 'subagent:task-1',
+ name: 'subagent:researcher',
+ round: 0,
+ seq: 0,
+ status: 'running',
+ subagent: { taskId: 'task-1', agentId: 'researcher', toolCalls: [] },
+ },
+ ],
+ },
})
);
const done = reducer(
@@ -1483,13 +1539,11 @@ describe('subagent event reducers (Phase 3)', () => {
threadId: 't1',
rowId: 't1:subagent:task-1:researcher',
taskId: 'task-1',
- success: true,
+ success: false,
})
);
- expect(done.toolTimelineByThread['t1'].map(e => [e.id, e.status])).toEqual([
- ['subagent:task-1', 'success'],
- ['earlier-card', 'running'],
- ]);
+ const row = done.turnTimelinesByThread['t1']['req-old'][0];
+ expect([row.status, row.subagent?.status]).toEqual(['error', 'failed']);
});
it('subagentCancelResolved settles a spinning row by task id from the cancel answer', () => {
diff --git a/app/src/store/chatRuntimeSlice.ts b/app/src/store/chatRuntimeSlice.ts
index 86a4c806cb..cab38479a4 100644
--- a/app/src/store/chatRuntimeSlice.ts
+++ b/app/src/store/chatRuntimeSlice.ts
@@ -1957,21 +1957,31 @@ const chatRuntimeSlice = createSlice({
// not this socket `rowId`. Matching the socket 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. Exactly that
- // id, not any row with this task id: `continue_subagent` reuses a task
- // id, and a run it continues must not settle an earlier turn's card.
+ // forever under the reply announcing the child's result. Both ids are
+ // turn-independent, so a run resumed by `continue_subagent` (same task
+ // id) also settles the earlier turn's card for it. That is how a paused
+ // `awaiting_user` card clears: the resumed run's completion carries the
+ // new `continue_subagent` call as its parent, never the original spawn.
+ // Restored past-turn timelines too, like `subagentCancelResolved`: they
+ // hold persisted (core-id) rows the process-source panel shows.
const coreRowId = taskId !== undefined ? `subagent:${taskId}` : undefined;
- const entries = subagentRows(
- state,
- threadId,
- e =>
- (e.id === rowId || e.id === coreRowId) &&
- (e.status === 'running' || e.status === 'awaiting_user')
- );
+ const matches = (e: ToolTimelineEntry) =>
+ (e.id === rowId || e.id === coreRowId) &&
+ (e.status === 'running' || e.status === 'awaiting_user');
+ const entries = [
+ ...subagentRows(state, threadId, matches),
+ ...Object.values(state.turnTimelinesByThread[threadId] ?? {})
+ .flat()
+ .filter(matches),
+ ];
for (const entry of entries) {
entry.status = success ? 'success' : 'error';
if (!entry.subagent) continue;
const s = entry.subagent;
+ // The nested activity settles with the row: it drives the card's own
+ // transcript status and the live tail's `requires-action` state, and a
+ // hydrated row carries the snapshot's `running` / `awaiting_user`.
+ s.status = success ? 'completed' : 'failed';
if (iterations !== undefined) s.iterations = iterations;
if (elapsedMs !== undefined) s.elapsedMs = elapsedMs;
if (outputChars !== undefined) s.outputChars = outputChars;