@@ -5759,6 +5759,211 @@ describe("ClaudeAdapterV2 background wake turns", () => {
57595759 ),
57605760 );
57615761
5762+ it.effect.each([
5763+ "accept",
5764+ "decline",
5765+ "full-access",
5766+ "without-agent-id",
5767+ "no-subagent",
5768+ "earlier-run",
5769+ "wrong-agent",
5770+ "aborted",
5771+ "stopping",
5772+ "replaced-process",
5773+ "resumed",
5774+ ] as const)("handles a settled subagent permission request: %s", (scenario) =>
5775+ Effect.scoped(
5776+ Effect.gen(function* () {
5777+ const closeStarted = yield* Deferred.make<void>();
5778+ const finishClose = yield* Deferred.make<void>();
5779+ const harness = yield* makeWakeHarnessWithOptions({
5780+ freshQueueOnReopen: true,
5781+ close: (messages) =>
5782+ scenario === "stopping"
5783+ ? Deferred.succeed(closeStarted, undefined).pipe(
5784+ Effect.andThen(Deferred.await(finishClose)),
5785+ Effect.andThen(Queue.shutdown(messages)),
5786+ )
5787+ : Queue.shutdown(messages),
5788+ });
5789+ const now = yield* DateTime.now;
5790+ const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
5791+ runtimeMode: scenario === "full-access" ? "full-access" : "approval-required",
5792+ interactionMode: "default",
5793+ cwd: "/workspace",
5794+ });
5795+ const start = (ordinal: number) =>
5796+ harness.runtime.startTurn(
5797+ makeClaudeTestTurnInput({
5798+ threadId: harness.threadId,
5799+ providerThread: harness.providerThread,
5800+ now,
5801+ attemptId: RunAttemptId.make(`attempt-idle-permission-${ordinal}`),
5802+ providerTurnOrdinal: ordinal,
5803+ text: "Start the subagent, then return.",
5804+ attachments: [],
5805+ runtimePolicy,
5806+ }),
5807+ );
5808+ const taskStarted = (toolUseId: string) =>
5809+ claudeSdkFrame({
5810+ type: "system",
5811+ subtype: "task_started",
5812+ task_id: "idle-permission-agent",
5813+ tool_use_id: toolUseId,
5814+ description: "Background permission probe",
5815+ subagent_type: "general-purpose",
5816+ task_type: "local_agent",
5817+ is_backgrounded: true,
5818+ prompt: "Read a file after the parent returns.",
5819+ uuid: `idle-permission-${toolUseId}`,
5820+ session_id: WAKE_NATIVE_SESSION,
5821+ });
5822+ const resultFrame = (ordinal: number) =>
5823+ makeResultFrame({ uuid: `idle-permission-result-${ordinal}`, result: "Started." });
5824+ yield* start(1);
5825+ const callback = harness.getOpenedOptions()!.canUseTool!;
5826+ if (scenario !== "no-subagent") {
5827+ yield* harness.offerAndWait(taskStarted("idle-launch"));
5828+ }
5829+ if (scenario === "resumed") {
5830+ yield* harness.offerAndWait(
5831+ claudeSdkFrame({
5832+ type: "system",
5833+ subtype: "task_notification",
5834+ task_id: "idle-permission-agent",
5835+ tool_use_id: "idle-launch",
5836+ status: "completed",
5837+ output_file: "/tmp/idle-permission-agent.output",
5838+ summary: "First answer.",
5839+ uuid: "idle-permission-completed",
5840+ session_id: WAKE_NATIVE_SESSION,
5841+ }),
5842+ );
5843+ }
5844+ yield* harness.offerAndWait(resultFrame(1));
5845+ yield* Queue.take(harness.terminalReceipts);
5846+ const stop =
5847+ scenario === "stopping" || scenario === "replaced-process"
5848+ ? yield* harness.runtime
5849+ .interruptTurn({
5850+ providerThread: harness.providerThread,
5851+ providerTurnId: harness.terminalEvents()[0]!.providerTurnId,
5852+ requestRuntimeRestart: true,
5853+ })
5854+ .pipe(Effect.forkScoped)
5855+ : null;
5856+ if (scenario === "stopping") yield* Deferred.await(closeStarted);
5857+ else if (stop !== null) yield* Fiber.join(stop);
5858+ if (
5859+ scenario === "earlier-run" ||
5860+ scenario === "resumed" ||
5861+ scenario === "replaced-process"
5862+ ) {
5863+ yield* start(2);
5864+ if (scenario === "replaced-process") {
5865+ yield* Queue.offer(harness.processQueues[1]!, taskStarted("replacement-launch"));
5866+ yield* Queue.take(harness.subagentReceipts);
5867+ yield* Queue.offer(harness.processQueues[1]!, resultFrame(2));
5868+ } else {
5869+ if (scenario === "resumed") {
5870+ yield* harness.offerAndWait(taskStarted("idle-send-message"));
5871+ }
5872+ yield* harness.offerAndWait(resultFrame(2));
5873+ }
5874+ yield* Queue.take(harness.terminalReceipts);
5875+ }
5876+ const controller = new AbortController();
5877+ if (scenario === "aborted") controller.abort();
5878+ let callbackReturned = false;
5879+ const permission = yield* Effect.promise(async () => {
5880+ const decision = await callback(
5881+ "Read",
5882+ { file_path: "/etc/hosts" },
5883+ {
5884+ signal: controller.signal,
5885+ toolUseID: "idle-read",
5886+ requestId: "idle-permission-request",
5887+ ...(scenario === "without-agent-id"
5888+ ? {}
5889+ : {
5890+ agentID: scenario === "wrong-agent" ? "another-agent" : "idle-permission-agent",
5891+ }),
5892+ },
5893+ );
5894+ callbackReturned = true;
5895+ return decision;
5896+ }).pipe(Effect.forkScoped);
5897+ const needsApproval =
5898+ scenario === "accept" ||
5899+ scenario === "decline" ||
5900+ scenario === "resumed" ||
5901+ scenario === "without-agent-id";
5902+ if (needsApproval) {
5903+ yield* awaitUntil(
5904+ () =>
5905+ callbackReturned ||
5906+ harness.events.some((event) => event.type === "runtime_request.updated"),
5907+ "settled subagent approval",
5908+ );
5909+ const request = harness.events.findLast(
5910+ (event) => event.type === "runtime_request.updated",
5911+ );
5912+ assert.equal(request?.type, "runtime_request.updated");
5913+ if (request?.type !== "runtime_request.updated") return;
5914+ assert.equal(
5915+ request.runtimeRequest.providerTurnId,
5916+ harness.terminalEvents().at(-1)!.providerTurnId,
5917+ );
5918+ const node = harness.events.findLast(
5919+ (event) =>
5920+ event.type === "node.updated" && event.node.id === request.runtimeRequest.nodeId,
5921+ );
5922+ assert.equal(
5923+ node?.type === "node.updated" ? node.node.parentNodeId : null,
5924+ node?.type === "node.updated" ? node.node.rootNodeId : undefined,
5925+ );
5926+ yield* harness.runtime.respondToRuntimeRequest({
5927+ requestId: request.runtimeRequest.id,
5928+ decision: scenario === "decline" ? "decline" : "accept",
5929+ });
5930+ }
5931+ const decision = yield* Fiber.join(permission);
5932+ const allowed =
5933+ scenario === "accept" ||
5934+ scenario === "resumed" ||
5935+ scenario === "full-access" ||
5936+ scenario === "without-agent-id";
5937+ assert.equal(decision?.behavior, allowed ? "allow" : "deny");
5938+ if (!needsApproval) {
5939+ assert.isFalse(harness.events.some((event) => event.type === "runtime_request.updated"));
5940+ if (!allowed) {
5941+ assert.equal(
5942+ decision?.behavior === "deny" ? decision.message : null,
5943+ "Claude V2 adapter has no active turn for this tool request.",
5944+ );
5945+ }
5946+ }
5947+ assert.isFalse(
5948+ harness.events.some(
5949+ (event) =>
5950+ event.type === "turn_item.updated" &&
5951+ event.turnItem.type === "dynamic_tool" &&
5952+ event.turnItem.nativeItemRef?.nativeId === "idle-read",
5953+ ),
5954+ );
5955+ if (scenario === "stopping" && stop !== null) {
5956+ yield* Deferred.succeed(finishClose, undefined);
5957+ yield* Fiber.join(stop);
5958+ }
5959+ }).pipe(
5960+ Effect.provide(
5961+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
5962+ ),
5963+ ),
5964+ ),
5965+ );
5966+
57625967 it.effect("answers an approval a held wake turn raises without waiting for the echo", () =>
57635968 Effect.scoped(
57645969 Effect.gen(function* () {
0 commit comments