Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(agent): a run's end answers only its own approvals, not a background sub-agent's

purple8811219b804c
diff --git a/src/api/agents/sub-agent.ts b/src/api/agents/sub-agent.ts
index 9c99653f..918d2ffe 100644
--- a/src/api/agents/sub-agent.ts
+++ b/src/api/agents/sub-agent.ts
@@ -406,7 +406,7 @@ export async function buildSubAgentGates(run?: AgentRunContext): Promise<SubAgen
status: 'pending_approval',
timestamp: Date.now(),
}
- const entry: ApprovalEntry = { toolCall, resolve }
+ const entry: ApprovalEntry = { toolCall, resolve, owner: abortSignal }
approvals.enqueueApproval(convId, entry)
// Stop has to answer a question nobody clicked, or the delegation (and
// with it the parent turn) waits forever, same lesson as audit A4.
diff --git a/src/hooks/useAgentChat.ts b/src/hooks/useAgentChat.ts
index e2b30e0a..df1d6346 100644
--- a/src/hooks/useAgentChat.ts
+++ b/src/hooks/useAgentChat.ts
@@ -219,7 +219,7 @@ export function useAgentChat() {
resolve(false)
return
}
- const entry: ApprovalEntry = { toolCall, resolve }
+ const entry: ApprovalEntry = { toolCall, resolve, owner: signal }
enqueueApproval(convId, entry)
signal?.addEventListener(
'abort',
@@ -3045,7 +3045,7 @@ export function useAgentChat() {
// but only THIS run's: a replaced slot means a NEW run's approvals are
// waiting under the same convId, and they must survive.
if (stillOwnsSlot) {
- drainApprovals(convId)
+ drainApprovals(convId, abort.signal)
}
// Auto-read the finished response when the user opted in (#77). Default
diff --git a/src/lib/__tests__/a-run-end-leaves-background-approvals-alone.test.ts b/src/lib/__tests__/a-run-end-leaves-background-approvals-alone.test.ts
new file mode 100644
index 00000000..556c488b
--- /dev/null
+++ b/src/lib/__tests__/a-run-end-leaves-background-approvals-alone.test.ts
@@ -0,0 +1,40 @@
+/**
+ * Bug hunt 01.10.2026 (A8). A background sub-agent asks for approval in the
+ * same per-conversation queue as the run that started it, but it outlives that
+ * run. The run's end emptied the whole queue, so the sub-agent was told "User
+ * rejected tool call" for a question the user never saw.
+ */
+import { it, expect, beforeEach } from 'vitest'
+import { readFileSync } from 'node:fs'
+import { resolve } from 'node:path'
+import { enqueueApproval, drainApprovals, headApproval, resetApprovals, type ApprovalEntry } from '../approval-queue'
+import type { AgentToolCall } from '../../types/agent-mode'
+
+const call = (id: string): AgentToolCall => ({ id, toolName: 'shell_execute', args: {}, status: 'pending_approval', timestamp: 0 })
+
+beforeEach(() => resetApprovals())
+
+it('the run end answers its own questions and leaves the sub-agent waiting', () => {
+ const run = new AbortController().signal
+ const background = new AbortController().signal
+ const answers: Record<string, boolean> = {}
+ const entry = (id: string, owner: AbortSignal): ApprovalEntry => ({ toolCall: call(id), owner, resolve: (a) => { answers[id] = a } })
+ enqueueApproval('c1', entry('main', run))
+ enqueueApproval('c1', entry('sub', background))
+
+ drainApprovals('c1', run)
+ expect(answers).toEqual({ main: false })
+ expect(headApproval('c1')?.id).toBe('sub')
+
+ // Stop still clears everything in the conversation.
+ drainApprovals('c1')
+ expect(answers).toEqual({ main: false, sub: false })
+ expect(headApproval('c1')).toBeNull()
+})
+
+it('both askers say who they are, and the run end passes its own signal', () => {
+ const read = (f: string) => readFileSync(resolve(__dirname, '..', '..', f), 'utf8')
+ expect(read('hooks/useAgentChat.ts')).toContain('const entry: ApprovalEntry = { toolCall, resolve, owner: signal }')
+ expect(read('hooks/useAgentChat.ts')).toContain('drainApprovals(convId, abort.signal)')
+ expect(read('api/agents/sub-agent.ts')).toContain('const entry: ApprovalEntry = { toolCall, resolve, owner: abortSignal }')
+})
diff --git a/src/lib/approval-queue.ts b/src/lib/approval-queue.ts
index 3520ffaa..aaaf6cd2 100644
--- a/src/lib/approval-queue.ts
+++ b/src/lib/approval-queue.ts
@@ -21,6 +21,10 @@ import type { AgentToolCall } from '../types/agent-mode'
export interface ApprovalEntry {
toolCall: AgentToolCall
resolve: (approved: boolean) => void
+ /** The run that asked, as its abort signal. A run's end drains only its own
+ * entries: a background sub-agent shares the conversation's queue but
+ * outlives the turn that started it. */
+ owner?: AbortSignal
}
const queues = new Map<string, ApprovalEntry[]>()
@@ -75,12 +79,23 @@ export function removeApproval(convId: string, entry: ApprovalEntry): boolean {
return true
}
-/** Answer every waiting tool with "no" and empty the queue (turn end, Stop). */
-export function drainApprovals(convId: string | null | undefined): void {
+/**
+ * Answer waiting tools with "no". With `owner`, only that run's entries (turn
+ * end); without, the whole conversation's queue (Stop).
+ *
+ * The turn end used to empty the whole queue (bug hunt 01.10.2026, A8), so a
+ * background sub-agent still at work was told "User rejected tool call" for a
+ * question the user never saw.
+ */
+export function drainApprovals(convId: string | null | undefined, owner?: AbortSignal): void {
if (!convId) return
const q = queues.get(convId)
- queues.delete(convId)
- if (q) for (const entry of q) entry.resolve(false)
+ if (!q) return
+ const drained = owner ? q.filter((e) => e.owner === owner) : q
+ const kept = owner ? q.filter((e) => e.owner !== owner) : []
+ if (kept.length) queues.set(convId, kept)
+ else queues.delete(convId)
+ for (const entry of drained) entry.resolve(false)
notify()
}