The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 00:30 UTC
fix(claude): Plan mode asks before writes in Auto and Full access (#17963)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Maxwell Young committed
Oct 20, 2026 at 13:28 UTC
b49488759211b2c69a9419b845e1bdff17d23fb3
2 files changed
+176
-1
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+162
@@ -2542,6 +2542,14 @@ describe("ClaudeAdapterV2 background wake turns", () => {
2542
yield* Queue.unbounded<
2543
Extract<ProviderAdapter.ProviderAdapterV2Event, { type: "turn_item.updated" }>
2544
>();
2545
+ const runtimeRequestReceipts =
2546
+ yield* Queue.unbounded<
2547
+ Extract<ProviderAdapter.ProviderAdapterV2Event, { type: "runtime_request.updated" }>
2548
+ >();
2549
+ const planReceipts =
2550
+ yield* Queue.unbounded<
2551
+ Extract<ProviderAdapter.ProviderAdapterV2Event, { type: "plan.updated" }>
2552
+ >();
2553
let openedOptions: ClaudeAdapterV2.ClaudeAgentSdkQueryOptions | undefined;
2554
const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
2555
instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
@@ -2649,6 +2657,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
2657
if (event.type === "turn.terminal") {
2658
yield* Queue.offer(terminalReceipts, event);
2659
}
2660
+ if (event.type === "runtime_request.updated") {
2661
+ yield* Queue.offer(runtimeRequestReceipts, event);
2662
+ }
2663
+ if (event.type === "plan.updated") {
2664
+ yield* Queue.offer(planReceipts, event);
2665
+ }
2666
if (event.type === "turn_item.updated" && event.turnItem.type === "system_notice") {
2667
yield* Queue.offer(systemNoticeReceipts, event);
2668
}
@@ -2688,6 +2702,8 @@ describe("ClaudeAdapterV2 background wake turns", () => {
2702
terminalReceipts,
2703
subagentReceipts,
2704
systemNoticeReceipts,
2705
+ runtimeRequestReceipts,
2706
+ planReceipts,
2707
getOpenedOptions: () => openedOptions,
2708
terminalEvents,
2709
hasPendingBackgroundWork,
@@ -2695,6 +2711,152 @@ describe("ClaudeAdapterV2 background wake turns", () => {
2711
});
2712
const makeWakeHarness = makeWakeHarnessWithOptions();
2713
2714
+ describe("Plan-mode tool approval", () => {
2715
+ const openPlanTurn = Effect.fn("openPlanTurn")(function* (
2716
+ runtimeMode: "auto" | "full-access",
2717
+ approvalPolicy?: "never",
2718
+ ) {
2719
+ const harness = yield* makeWakeHarness;
2720
+ const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
2721
+ runtimeMode,
2722
+ interactionMode: "plan",
2723
+ cwd: "/workspace",
2724
+ ...(approvalPolicy === undefined ? {} : { approvalPolicy }),
2725
+ });
2726
+ const input = makeClaudeTestTurnInput({
2727
+ threadId: harness.threadId,
2728
+ providerThread: harness.providerThread,
2729
+ now: yield* DateTime.now,
2730
+ attemptId: RunAttemptId.make("attempt-claude-plan-write"),
2731
+ text: "Investigate before implementing.",
2732
+ attachments: [],
2733
+ runtimePolicy,
2734
+ });
2735
+ yield* harness.runtime.startTurn({
2736
+ ...input,
2737
+ appThread: { ...input.appThread, runtimeMode, interactionMode: "plan" },
2738
+ });
2739
+ assert.equal(harness.getOpenedOptions()?.permissionMode, "plan");
2740
+ const canUseTool = harness.getOpenedOptions()?.canUseTool;
2741
+ assert.isDefined(canUseTool);
2742
+ return { ...harness, canUseTool: canUseTool! };
2743
+ });
2744
+
2745
+ const requestWrite = Effect.fn("requestPlanWrite")(function* (
2746
+ turn: Effect.Success<ReturnType<typeof openPlanTurn>>,
2747
+ toolName: string,
2748
+ toolInput: Record<string, unknown>,
2749
+ ) {
2750
+ const decision = yield* Effect.promise(() =>
2751
+ turn.canUseTool(toolName, toolInput, {
2752
+ signal: new AbortController().signal,
2753
+ toolUseID: "tool-plan-write",
2754
+ requestId: "request-plan-write",
2755
+ }),
2756
+ ).pipe(Effect.forkScoped);
2757
+ const first = yield* Effect.raceFirst(
2758
+ Queue.take(turn.runtimeRequestReceipts).pipe(
2759
+ Effect.map((event) => ({ type: "request", event }) as const),
2760
+ ),
2761
+ Fiber.join(decision).pipe(Effect.map((result) => ({ type: "decision", result }) as const)),
2762
+ );
2763
+ return { first, decision };
2764
+ });
2765
+
2766
+ it.effect.each([
2767
+ {
2768
+ runtimeMode: "auto",
2769
+ toolName: "Edit",
2770
+ toolInput: {
2771
+ file_path: "/workspace/target.txt",
2772
+ old_string: "before",
2773
+ new_string: "after",
2774
+ },
2775
+ kind: "file-change",
2776
+ decision: "accept",
2777
+ behavior: "allow",
2778
+ },
2779
+ {
2780
+ runtimeMode: "full-access",
2781
+ toolName: "Bash",
2782
+ toolInput: { command: "echo after > target.txt" },
2783
+ kind: "command",
2784
+ decision: "cancel",
2785
+ behavior: "deny",
2786
+ },
2787
+ ] as const)(
2788
+ "asks before $toolName in $runtimeMode Plan, then respects $decision",
2789
+ ({ runtimeMode, toolName, toolInput, kind, decision, behavior }) =>
2790
+ Effect.gen(function* () {
2791
+ const turn = yield* openPlanTurn(runtimeMode);
2792
+ const permission = yield* requestWrite(turn, toolName, toolInput);
2793
+ assert.equal(permission.first.type, "request", "the planned write bypassed approval");
2794
+ if (permission.first.type !== "request") return;
2795
+ const request = permission.first.event.runtimeRequest;
2796
+ assert.equal(request.status, "pending");
2797
+ assert.equal(request.kind, kind);
2798
+ yield* turn.runtime.respondToRuntimeRequest({ requestId: request.id, decision });
2799
+ const result = yield* Fiber.join(permission.decision);
2800
+ assert.equal(result?.behavior, behavior);
2801
+ if (result?.behavior === "allow") {
2802
+ assert.deepEqual(result.updatedInput, toolInput);
2803
+ assert.equal(result.toolUseID, "tool-plan-write");
2804
+ }
2805
+ }).pipe(
2806
+ Effect.scoped,
2807
+ Effect.provide(
2808
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
2809
+ ),
2810
+ ),
2811
+ );
2812
+
2813
+ it.effect("denies Plan writes without asking when approvals are never allowed", () =>
2814
+ Effect.gen(function* () {
2815
+ const turn = yield* openPlanTurn("full-access", "never");
2816
+ const permission = yield* requestWrite(turn, "Bash", {
2817
+ command: "echo after > target.txt",
2818
+ });
2819
+ assert.equal(permission.first.type, "decision", "Never raised an approval request");
2820
+ if (permission.first.type !== "decision") return;
2821
+ assert.equal(permission.first.result?.behavior, "deny");
2822
+ }).pipe(
2823
+ Effect.scoped,
2824
+ Effect.provide(
2825
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
2826
+ ),
2827
+ ),
2828
+ );
2829
+
2830
+ it.effect("still captures ExitPlanMode when Plan approvals are never allowed", () =>
2831
+ Effect.gen(function* () {
2832
+ const turn = yield* openPlanTurn("full-access", "never");
2833
+ const markdown = "# Proposed work\n\nFix the calculation after approval.";
2834
+ const result = yield* Effect.promise(() =>
2835
+ turn.canUseTool(
2836
+ "ExitPlanMode",
2837
+ { plan: markdown },
2838
+ {
2839
+ signal: new AbortController().signal,
2840
+ toolUseID: "tool-plan-capture",
2841
+ requestId: "request-plan-capture",
2842
+ },
2843
+ ),
2844
+ );
2845
+ assert.equal(result?.behavior, "deny");
2846
+ if (result?.behavior === "deny")
2847
+ assert.include(result.message, "captured your proposed plan");
2848
+ const event = yield* Queue.take(turn.planReceipts);
2849
+ assert.equal(event.plan.kind, "proposed_plan");
2850
+ assert.equal(event.plan.kind === "proposed_plan" ? event.plan.markdown : "", markdown);
2851
+ }).pipe(
2852
+ Effect.scoped,
2853
+ Effect.provide(
2854
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
2855
+ ),
2856
+ ),
2857
+ );
2858
+ });
2859
+
2860
it.effect.each([
2861
{ isError: false, title: "Check weather" },
2862
{ isError: true, title: "Check weather" },
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+14
-1
@@ -1952,7 +1952,8 @@ export function claudeRuntimeQueryPolicyForRuntimePolicy(
1952
permissionMode,
1953
...(readOnlyTools === undefined ? {} : { tools: readOnlyTools }),
1954
...(allowedTools === undefined ? {} : { allowedTools }),
1955
- installPermissionCallback,
1955
+ // Claude routes Plan-mode writes to the callback even in Auto or Full access.
1956
+ installPermissionCallback: true,
1957
};
1958
}
1959
@@ -8471,6 +8472,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
8472
} satisfies PermissionResult;
8473
}
8474
8475
+ if (
8476
+ context.input.runtimePolicy.interactionMode === "plan" &&
8477
+ context.input.runtimePolicy.approvalPolicy === "never"
8478
+ ) {
8479
+ return {
8480
+ behavior: "deny",
8481
+ message:
8482
+ "This tool requires approval in Plan mode, but approvals are disabled for this thread.",
8483
+ toolUseID: callbackOptions.toolUseID,
8484
+ } satisfies PermissionResult;
8485
+ }
8486
+
8487
if (!requiresClaudeApproval(context)) {
8488
return {
8489
behavior: "allow",