fix(desktop): fail closed on unrecognized subagent.complete statuses; surface timeout reason

Follow-up to the #73728 normalization fix (supersedes the event-agnostic
fallback the maintainers flagged as incomplete):

- subagent.complete is terminal by definition — an unrecognized status on
  it now renders as 'failed' instead of falling through to 'running',
  which would recreate the immortal false-active row for any future
  backend status (the keep_open request on #73859).
- Live events keep the lenient 'running' fallback.
- Synthesize a 'Timed out after Xs' summary from duration_seconds when
  the backend completes with status 'timeout' and no summary, so the
  failed row explains itself.
- Tests: timeout reason synthesis + pruning, event-aware fail-closed vs
  lenient live fallback (13 total).
This commit is contained in:
David Metcalfe
2026-08-13 11:17:33 -07:00
committed by Teknium
parent a351c17d4a
commit 09b1726a1d
2 changed files with 81 additions and 7 deletions
+48
View File
@@ -227,4 +227,52 @@ describe('subagent store', () => {
pruneFinishedSessionSubagents('s1')
expect(listFor('s1')).toHaveLength(0)
})
// The backend completes subagents with status "timeout" (hard child timeout,
// delegation.child_timeout_seconds) and no summary — synthesize the reason
// so the failed row explains itself instead of rendering as a bare failure.
it('maps backend timeout status to a terminal failure with a synthesized reason', () => {
upsertSubagent('s1', { goal: 'scan files', status: 'running', subagent_id: 't1', task_index: 0 })
upsertSubagent(
's1',
{ status: 'timeout', subagent_id: 't1', task_index: 0, duration_seconds: 612.3 },
false,
'subagent.complete'
)
const item = listFor('s1')[0]
expect(item?.status).toBe('failed')
expect(item?.durationSeconds).toBe(612.3)
expect(item?.summary).toBe('Timed out after 612.3s')
// A timed-out row must be pruned at the next message.start boundary like
// any other finished row — it must not linger as a live spinner.
pruneFinishedSessionSubagents('s1')
expect(listFor('s1')).toHaveLength(0)
})
// Fail-closed guard: subagent.complete is terminal by definition, so an
// unrecognized status on it must not resurrect a row as 'running'. Live
// events keep the lenient fallback (a status we don't know is still active).
it('fails closed on unrecognized completion statuses but stays lenient for live events', () => {
upsertSubagent('s1', { goal: 'scan files', status: 'running', subagent_id: 'u1', task_index: 0 })
upsertSubagent(
's1',
{ status: 'some_future_terminal_status', subagent_id: 'u1', task_index: 0 },
false,
'subagent.complete'
)
expect(listFor('s1')[0]?.status).toBe('failed')
expect(activeSubagentCount(listFor('s1'))).toBe(0)
upsertSubagent('s1', { goal: 'scan files', status: 'running', subagent_id: 'u2', task_index: 1 })
upsertSubagent(
's1',
{ status: 'some_future_live_status', subagent_id: 'u2', task_index: 1, text: 'still working' },
false,
'subagent.progress'
)
expect(listFor('s1')[1]?.status).toBe('running')
expect(activeSubagentCount(listFor('s1'))).toBe(1)
})
})
+33 -7
View File
@@ -55,10 +55,27 @@ const str = (v: unknown) => (isStr(v) ? v : '')
const num = (v: unknown) => (typeof v === 'number' && Number.isFinite(v) ? v : undefined)
const strList = (v: unknown) => (Array.isArray(v) ? v.filter(isStr) : [])
const asStatus = (v: unknown): SubagentStatus => {
if (v === 'completed' || v === 'failed' || v === 'interrupted' || v === 'queued') return v
if (v === 'timeout' || v === 'error') return 'failed'
if (v === 'cancelled' || v === 'canceled') return 'interrupted'
const asStatus = (v: unknown, terminalEvent = false): SubagentStatus => {
if (v === 'completed' || v === 'failed' || v === 'interrupted' || v === 'queued') {
return v
}
if (v === 'timeout' || v === 'error') {
return 'failed'
}
if (v === 'cancelled' || v === 'canceled') {
return 'interrupted'
}
// Fail closed on completion: a subagent.complete event is terminal by
// definition, so an unrecognized status must render as a failure rather
// than leave a dead row spinning as 'running' forever. Live events keep
// the lenient 'running' fallback.
if (terminalEvent) {
return 'failed'
}
return 'running'
}
@@ -110,6 +127,15 @@ const appendStream = (stream: SubagentStreamEntry[], entry: SubagentStreamEntry)
return [...stream, entry].slice(-MAX_STREAM)
}
// The backend sends no summary on a hard child timeout (only a preview like
// "Timed out after 612.3s" + duration_seconds). Synthesize it so the terminal
// row explains why it failed instead of rendering as a bare failure.
const timeoutSummary = (payload: SubagentPayload): string => {
const seconds = num(payload.duration_seconds)
return str(payload.status) === 'timeout' ? `Timed out after ${seconds ?? '?'}s` : ''
}
function streamFromPayload(
payload: SubagentPayload,
status: SubagentStatus,
@@ -141,7 +167,7 @@ function streamFromPayload(
out.push({ at, kind: 'thinking', text })
}
const summary = compact(str(payload.summary) || str(payload.text))
const summary = compact(str(payload.summary) || str(payload.text) || timeoutSummary(payload))
if (TERMINAL.has(status) && summary) {
out.push({ at, isError: status === 'failed', kind: 'summary', text: summary })
@@ -152,7 +178,7 @@ function streamFromPayload(
function toProgress(payload: SubagentPayload, prev: SubagentProgress | undefined, eventType = ''): SubagentProgress {
const at = Date.now()
const status = asStatus(payload.status)
const status = asStatus(payload.status, eventType === 'subagent.complete')
const tool = str(payload.tool_name)
const stream = streamFromPayload(payload, status, eventType, at).reduce(appendStream, prev?.stream ?? [])
const filesRead = strList(payload.files_read)
@@ -177,7 +203,7 @@ function toProgress(payload: SubagentPayload, prev: SubagentProgress | undefined
filesRead: filesRead.length ? filesRead : (prev?.filesRead ?? []),
filesWritten: filesWritten.length ? filesWritten : (prev?.filesWritten ?? []),
stream,
summary: str(payload.summary) || prev?.summary,
summary: str(payload.summary) || prev?.summary || timeoutSummary(payload) || undefined,
currentTool: TERMINAL.has(status) ? undefined : tool || prev?.currentTool
}
}