Skip to content

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

feat(providers): project approval state into assistant UI messages

Steven Enamakel25cf1b008cca
diff --git a/app/src/providers/__tests__/assistantUiMessages.approvalProjection.test.ts b/app/src/providers/__tests__/assistantUiMessages.approvalProjection.test.ts
index 8ca000f848..54ae28cd97 100644
--- a/app/src/providers/__tests__/assistantUiMessages.approvalProjection.test.ts
+++ b/app/src/providers/__tests__/assistantUiMessages.approvalProjection.test.ts
@@ -128,4 +128,34 @@ describe('a parked approval in the runtime projection', () => {
expect(ids).toEqual([...new Set(ids)]);
expect(ids).toContain('call-parked');
});
+
+ it('shows a detached approval on its own after the parent turn settled', () => {
+ // An async sub-agent asked for approval and the parent turn has since
+ // finished. The card must still render (its gate is parked) — and only the
+ // card: the parent's trail belongs to its settled message, not the tail.
+ const detached: PendingApproval = {
+ requestId: 'appr-sub',
+ toolName: 'media_generate_image',
+ message: 'Run media_generate_image — a red fox',
+ toolCallId: 'sub-call-1',
+ detached: true,
+ };
+ const messages = buildRuntimeMessages([msg({ id: 'a1', sender: 'agent' })], null, {
+ isRunning: false,
+ pendingApproval: detached,
+ liveTimeline: [tool({ id: 'call-parent', name: 'spawn_async_subagent', status: 'success' })],
+ });
+
+ const tail = messages.find(message => message.id === STREAMING_TAIL_ID);
+ expect(tail).toBeDefined();
+ expect(tail?.status).toEqual({ type: 'requires-action', reason: 'interrupt' });
+ const tailParts = toolParts(tail!);
+ expect(tailParts).toHaveLength(1);
+ expect(tailParts[0].approval?.id).toBe('appr-sub');
+ expect(tailParts[0].toolName).toBe('media_generate_image');
+
+ const ids = allToolCallIds(messages);
+ expect(ids).toEqual([...new Set(ids)]);
+ expect(ids).toContain('call-parent');
+ });
});
diff --git a/app/src/providers/assistantUiMessages.ts b/app/src/providers/assistantUiMessages.ts
index 8a07f06316..567424ad78 100644
--- a/app/src/providers/assistantUiMessages.ts
+++ b/app/src/providers/assistantUiMessages.ts
@@ -1181,7 +1181,9 @@ export function buildRuntimeMessages(
projection.liveTimelineRequestId !== requestId;
const useSettledLiveFallback =
projection.isRunning === false &&
- !pendingApproval &&
+ // A detached approval's tail carries none of these rows (see
+ // `detachedAfterTurn`), so the settled message keeps them.
+ (!pendingApproval || detachedAfterTurn) &&
msg.id === lastVisibleAgentId &&
!persistedTimeline &&
!persistedTranscript &&
diff --git a/app/src/store/chatRuntimeSlice.test.ts b/app/src/store/chatRuntimeSlice.test.ts
index 56552edd14..dd2552711c 100644
--- a/app/src/store/chatRuntimeSlice.test.ts
+++ b/app/src/store/chatRuntimeSlice.test.ts
@@ -7,7 +7,9 @@ import chatRuntimeReducer, {
beginInferenceTurn,
clearAllChatRuntime,
clearQueueStatusForThread,
+ clearPendingApprovalIfRequest,
clearRuntimeForThread,
+ clearTurnApprovalForThread,
hydrateRuntimeFromRunLedger,
hydrateRuntimeFromSnapshot,
hydrateThreadUsage,
@@ -20,6 +22,7 @@ import chatRuntimeReducer, {
setStreamingAssistantForThread,
setToolTimelineForThread,
setWorkflowProposalForThread,
+ turnSettled,
} from './chatRuntimeSlice';
function makeRun(id: string, status: AgentRunStatus): AgentRun {
@@ -993,3 +996,47 @@ describe('hydrateRuntimeFromSnapshot — sub-agent transcript fallback (fix 4)',
expect(transcript[0].kind).toBe('tool');
});
});
+
+// Regression: an async sub-agent's approval (`detached`) was routed to the
+// parent thread while the parent turn ran, then wiped by that turn's
+// `chat_done` — "a parked gate cannot outlive its turn" — though its gate was
+// still parked. Every async `image_agent` approval then expired unseen at
+// 600s. A detached card survives turn-end clears and goes on its decision.
+describe('detached approvals', () => {
+ const detached = {
+ requestId: 'req-sub',
+ toolName: 'media_generate_image',
+ message: 'Run media_generate_image',
+ detached: true,
+ };
+
+ it('survives the parent turn ending', () => {
+ const store = makeStore();
+ store.dispatch(setPendingApprovalForThread({ threadId: 't1', approval: detached }));
+ store.dispatch(clearTurnApprovalForThread({ threadId: 't1' }));
+ store.dispatch(turnSettled({ threadId: 't1' }));
+ store.dispatch(clearRuntimeForThread({ threadId: 't1' }));
+ expect(store.getState().chatRuntime.pendingApprovalByThread['t1']?.requestId).toBe('req-sub');
+ });
+
+ it('still clears an in-turn approval at the turn end', () => {
+ const store = makeStore();
+ store.dispatch(
+ setPendingApprovalForThread({
+ threadId: 't1',
+ approval: { requestId: 'req-main', toolName: 'shell', message: 'Run ls' },
+ })
+ );
+ store.dispatch(clearTurnApprovalForThread({ threadId: 't1' }));
+ expect(store.getState().chatRuntime.pendingApprovalByThread['t1']).toBeUndefined();
+ });
+
+ it('clears on its own decision but not on another request', () => {
+ const store = makeStore();
+ store.dispatch(setPendingApprovalForThread({ threadId: 't1', approval: detached }));
+ store.dispatch(clearPendingApprovalIfRequest({ threadId: 't1', requestId: 'req-other' }));
+ expect(store.getState().chatRuntime.pendingApprovalByThread['t1']).toBeDefined();
+ store.dispatch(clearPendingApprovalIfRequest({ threadId: 't1', requestId: 'req-sub' }));
+ expect(store.getState().chatRuntime.pendingApprovalByThread['t1']).toBeUndefined();
+ });
+});