Skip to content

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

fix(app): never settle a detached sub-agent row from its parent's snapshot

M3gA-Mind119ff1925f41
diff --git a/app/src/store/__tests__/chatRuntimeSlice.test.ts b/app/src/store/__tests__/chatRuntimeSlice.test.ts
index acbc4fc2ea..1fcd996a8e 100644
--- a/app/src/store/__tests__/chatRuntimeSlice.test.ts
+++ b/app/src/store/__tests__/chatRuntimeSlice.test.ts
@@ -261,11 +261,15 @@ describe('chatRuntimeSlice', () => {
});
/**
- * The counterpart: an `interrupted` snapshot means the core process driving
- * the child is gone, so even a detached child has no driver left and must
- * settle rather than pulse forever.
+ * `interrupted` does NOT mean the core is gone. It is also stamped when the
+ * parent's agent loop merely errored with the core still running
+ * (`TurnStateMirror::finish`), and a detached child spawned earlier in that
+ * turn is a separate task that keeps working. So the snapshot must not
+ * settle a detached row on `interrupted` either — the ledger does that after
+ * a real restart. An ordinary (non-detached) row in the same snapshot has no
+ * driver left and must still settle.
*/
- it('settles a detached async subagent when the turn was interrupted', () => {
+ it('keeps a detached async subagent running when its parent turn was interrupted', () => {
const snapshot: PersistedTurnState = {
threadId: 'thread-async-int',
requestId: 'req-async-int',
@@ -288,16 +292,31 @@ describe('chatRuntimeSlice', () => {
toolCalls: [],
},
},
+ {
+ id: 'subagent:sub-typed-int',
+ name: 'subagent:writer',
+ round: 1,
+ status: 'running' as const,
+ subagent: {
+ taskId: 'sub-typed-int',
+ agentId: 'writer',
+ status: 'running' as const,
+ mode: 'typed',
+ toolCalls: [],
+ },
+ },
],
startedAt: '2026-09-22T00:00:00Z',
updatedAt: '2026-09-22T00:00:09Z',
};
const next = reducer(undefined, hydrateRuntimeFromSnapshot({ snapshot }));
- const row = next.toolTimelineByThread['thread-async-int'][0];
+ const [detached, typed] = next.toolTimelineByThread['thread-async-int'];
- expect(row.status).toBe('cancelled');
- expect(row.subagent?.status).toBe('cancelled');
+ expect(detached.status).toBe('running');
+ expect(detached.subagent?.status).toBe('running');
+ expect(typed.status).toBe('cancelled');
+ expect(typed.subagent?.status).toBe('cancelled');
});
it('rehydrates historical subagent rows without live streamed prose', () => {
@@ -421,14 +440,14 @@ describe('chatRuntimeSlice', () => {
* settle a detached row still shown running, and a `running` one must not.
*/
describe('ledger reconciliation of a detached async row kept alive past its parent', () => {
- const keptAlive = () =>
+ const keptAlive = (lifecycle: 'completed' | 'interrupted' = 'completed') =>
reducer(
undefined,
hydrateRuntimeFromSnapshot({
snapshot: {
threadId: 'thread-detached',
requestId: 'req-detached',
- lifecycle: 'completed',
+ lifecycle,
iteration: 1,
maxIterations: 25,
streamingText: '',
@@ -483,6 +502,32 @@ describe('chatRuntimeSlice', () => {
expect(rows[0].subagent?.status).toBe('interrupted');
});
+ /**
+ * A crash mid-turn leaves the parent snapshot `interrupted`, and the
+ * snapshot no longer settles detached rows. The ledger must, or a child
+ * that died with the core would read "Running" forever.
+ */
+ it('settles the row after a crash mid-turn, via the ledger', () => {
+ const before = keptAlive('interrupted');
+ expect(before.toolTimelineByThread['thread-detached'][0].status).toBe('running');
+
+ const rows = reducer(before, ledgerSays('interrupted')).toolTimelineByThread[
+ 'thread-detached'
+ ];
+
+ expect(rows[0].status).toBe('cancelled');
+ expect(rows[0].subagent?.status).toBe('interrupted');
+ });
+
+ /** The reviewer's scenario: the parent errored, the core and child live on. */
+ it('leaves the row running after a parent error while the child lives', () => {
+ const rows = reducer(keptAlive('interrupted'), ledgerSays('running')).toolTimelineByThread[
+ 'thread-detached'
+ ];
+
+ expect(rows[0].status).toBe('running');
+ });
+
it('leaves the row running while the ledger says the child is still alive', () => {
const rows = reducer(keptAlive(), ledgerSays('running')).toolTimelineByThread[
'thread-detached'
diff --git a/app/src/store/chatRuntimeSlice.ts b/app/src/store/chatRuntimeSlice.ts
index 63ea6d3e4f..511aedf78c 100644
--- a/app/src/store/chatRuntimeSlice.ts
+++ b/app/src/store/chatRuntimeSlice.ts
@@ -1032,22 +1032,23 @@ function toolTimelineFromPersisted(
* intact preserves the truthful "was waiting for the user" history — and the
* pulse is already stopped by the row-level `cancelled` above.
*
- * `turnStillOwnsDetachedChildren` is the one case where a `running` row is not
- * orphaned: a detached sub-agent (`spawn_async_subagent`, `mode === 'async'`)
- * is a fire-and-forget task that deliberately outlives the turn that spawned
- * it, and the core is still driving it. Settling those on the parent's
- * `completed` snapshot is what made the Background tasks panel report
- * "none running" — and the row read "Cancelled" — while the sub-agent was
- * visibly still making tool calls. Its `subagent_completed` event settles it
- * for real. An `interrupted` snapshot passes `false`: there the core process
- * itself is gone, so even a detached child has no driver left.
+ * A detached sub-agent (`spawn_async_subagent`, `mode === 'async'`) is the
+ * exception, and the parent's snapshot is never allowed to settle it. It is a
+ * fire-and-forget task that deliberately outlives the turn that spawned it, so
+ * the parent's lifecycle says nothing about whether the child is alive — and
+ * `interrupted` is ambiguous on exactly that point: it is stamped both when the
+ * core died (child dead too) and when the parent's agent loop merely errored
+ * with the core still running (`TurnStateMirror::finish`, child very possibly
+ * still working). Settling on the snapshot made the Background tasks panel
+ * read "none running" / "Cancelled" while the sub-agent was visibly still
+ * making tool calls. Its liveness is owned by sources that actually track it:
+ * its own `subagent_completed` event while the core lives, and the run ledger
+ * after a restart (startup stamps orphaned runs `interrupted`, which
+ * `hydrateRuntimeFromRunLedger` then applies to the row).
*/
-function settleOrphanedTimelineEntry(
- entry: ToolTimelineEntry,
- turnStillOwnsDetachedChildren = false
-): ToolTimelineEntry {
+function settleOrphanedTimelineEntry(entry: ToolTimelineEntry): ToolTimelineEntry {
if (entry.status !== 'running') return entry;
- if (turnStillOwnsDetachedChildren && entry.subagent?.mode === 'async') return entry;
+ if (entry.subagent?.mode === 'async') return entry;
return {
...entry,
status: 'cancelled',
@@ -2272,7 +2273,7 @@ const chatRuntimeSlice = createSlice({
state.toolTimelineByThread[threadId],
snapshot.toolTimeline
.map((e, seq) => toolTimelineFromPersisted(e, seq))
- .map(e => settleOrphanedTimelineEntry(e, snapshot.lifecycle === 'completed'))
+ .map(settleOrphanedTimelineEntry)
);
// Persisted order is issue order — seed the live counter with the
// row count so events arriving after this hydration keep counting
@@ -2369,15 +2370,14 @@ const chatRuntimeSlice = createSlice({
const seq = state.toolTimelineSeqByThread[threadId] ?? 0;
const entry = timelineEntryFromRun(run, seq);
if (liveTaskIds.has(run.id)) {
- // A detached `async` row is kept `running` past its parent's
- // `completed` snapshot (see `settleOrphanedTimelineEntry`), which
- // is only truthful while the child is alive. If the core died
- // first, no `subagent_completed` is ever coming, and the snapshot
- // stays `completed` — so without this the row would read
- // "Running" forever. The ledger is the independent authority on
- // the child: startup stamps orphaned runs `interrupted`
- // (`interrupt_orphaned_agent_runs`). Let a terminal ledger status
- // settle a row that is still shown running.
+ // The parent's snapshot never settles a detached `async` row (see
+ // `settleOrphanedTimelineEntry`): its lifecycle cannot say whether
+ // the child is alive. That is only truthful while the child is. If
+ // the core died, no `subagent_completed` is ever coming, so without
+ // this the row would read "Running" forever. The ledger is the
+ // independent authority on the child: startup stamps orphaned runs
+ // `interrupted` (`interrupt_orphaned_agent_runs`). Let a terminal
+ // ledger status settle a row that is still shown running.
const live = existing.find(e => e.subagent?.taskId === run.id);
const settled = timelineStatusFromRun(run.status);
if (