From 811330657f892190faf9797686627ef2f23d5264 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 17:22:10 +0000 Subject: [PATCH 1/2] test(server): background Claude subagent asks for approval after its parent turn settles Fails on main: canUseTool denies with "no active turn" once the root result clears the active turn, though the backgrounded subagent is still running. Refs #20339. AI-assisted; verified with the tests and tools listed above. --- .../Adapters/ClaudeAdapterV2.test.ts | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 95432c40e688..d54a23cf2ed0 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -15797,6 +15797,73 @@ describe("ClaudeAdapterV2 background wake turns", () => { return { childThreadId, toolThreadIds, assistantTexts }; }; + // regression: #20339 a background subagent outliving its parent turn must still get approvals + it.effect("asks for a background subagent's approval after the parent turn settles", () => + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: DateTime.makeUnsafe(0), + attemptId: RunAttemptId.make("settled-subagent-permission"), + text: "Run the auditor in the background.", + attachments: [], + runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "approval-required", + interactionMode: "default", + cwd: "/workspace", + }), + }), + ); + yield* harness.offerAndWait( + makeSubagentTaskStartedFrame({ + taskId: "auditor", + toolUseId: "toolu-auditor", + uuid: "00000000-0000-4000-8000-000000020339", + }), + ); + yield* harness.offerAndWait( + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000020340", result: "Launched." }), + ); + yield* Queue.take(harness.terminalReceipts); + + const decision = yield* Effect.promise(() => + harness.getOpenedOptions()!.canUseTool!( + "Read", + { file_path: "/etc/hosts" }, + { + signal: new AbortController().signal, + toolUseID: "toolu-auditor-read", + requestId: "request-auditor-read", + agentID: "auditor", + }, + ), + ).pipe(Effect.forkScoped); + const first = yield* Effect.raceFirst( + Queue.take(harness.runtimeRequestReceipts).pipe( + Effect.map((event) => ({ type: "request", event }) as const), + ), + Fiber.join(decision).pipe(Effect.map((result) => ({ type: "decision", result }) as const)), + ); + assert.equal( + first.type, + "request", + `Expected an approval request, got ${first.type === "decision" ? JSON.stringify(first.result) : ""}`, + ); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect.each([ { label: "an empty one", quotaFailureFirst: false }, { label: "a quota failure and an empty one", quotaFailureFirst: true }, From a29cb50514444e21958844572f3e1f6af2e234fd Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 17:31:58 +0000 Subject: [PATCH 2/2] fix(server): background Claude subagents still ask for approval after their parent turn ends canUseTool denied every request once the root result cleared the active turn, though a backgrounded subagent kept running in the same process. Requests from a still-running session subagent now resolve to the settled turn, as that subagent's frames already do, so approvals and the thread's permission mode apply again. Fixes pingdotgg/t3code#20339. AI-assisted; verified with the tests and tools listed above. --- .../orchestration-v2/Adapters/ClaudeAdapterV2.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 3e10e0990679..9df2746e6aa8 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -11382,13 +11382,26 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( yield* handleRoutedSdkMessage(input); }); + // A background subagent keeps running after its root turn settles; its requests belong to that turn. + const settledTurnForSubagentPermission = Effect.fnUntraced(function* ( + agentId: string | undefined, + ) { + const liveQuery = yield* Ref.get(queryContext); + if (agentId === undefined || liveQuery === null) return null; + const subagent = (yield* Ref.get(sessionSubagentsByTaskId)).get(agentId); + if (subagent?.task.status !== "running") return null; + return settledTurnByNativeThread.get(liveQuery.nativeThreadId) ?? null; + }); + const canUseToolEffect = Effect.fn("ClaudeAdapterV2.canUseTool")(function* ( toolName: Parameters[0], toolInput: Parameters[1], callbackOptions: Parameters[2], options?: { readonly startToolCall?: boolean }, ) { - const context = yield* Ref.get(activeTurn); + const context = + (yield* Ref.get(activeTurn)) ?? + (yield* settledTurnForSubagentPermission(callbackOptions.agentID)); if (context === null) { return { behavior: "deny",