Skip to content

Commit 48e6245

Browse files
kakismashclaude
andcommitted
fix(claude): make Always allow for session stick, and only for the session
acceptForSession echoed Claude Code's permission suggestions verbatim as updatedPermissions. That breaks in both directions: - MCP tools frequently arrive with empty or absent suggestions, so the echo produced nothing and the decision silently degraded into a one-shot accept - the same tool prompted again on every call (#4512). - When suggestions were present they carried destination localSettings, so a session-only choice was persisted to .claude/settings.local.json as a permanent allow rule. Rescope received suggestions to destination session, and synthesize a whole-tool session allow rule when Claude Code offered none. The rule uses the fully qualified tool name (e.g. mcp__server__tool), which is exactly what Claude Code's own rule matcher expects. Fixes #4512 Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent e482960 commit 48e6245

2 files changed

Lines changed: 148 additions & 2 deletions

File tree

‎apps/server/src/provider/Layers/ClaudeAdapter.test.ts‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,6 +2882,118 @@ describe("ClaudeAdapterLive", () => {
28822882
);
28832883
});
28842884

2885+
it.effect("acceptForSession returns session-scoped permission updates", () => {
2886+
const harness = makeHarness();
2887+
return Effect.gen(function* () {
2888+
const adapter = yield* ClaudeAdapter;
2889+
2890+
const session = yield* adapter.startSession({
2891+
threadId: THREAD_ID,
2892+
provider: ProviderDriverKind.make("claudeAgent"),
2893+
runtimeMode: "approval-required",
2894+
});
2895+
2896+
yield* Stream.take(adapter.streamEvents, 3).pipe(Stream.runDrain);
2897+
2898+
yield* adapter.sendTurn({
2899+
threadId: session.threadId,
2900+
input: "approve this for the session",
2901+
attachments: [],
2902+
});
2903+
yield* Stream.take(adapter.streamEvents, 1).pipe(Stream.runDrain);
2904+
2905+
const createInput = harness.getLastCreateQueryInput();
2906+
const canUseTool = createInput?.options.canUseTool;
2907+
assert.equal(typeof canUseTool, "function");
2908+
if (!canUseTool) {
2909+
return;
2910+
}
2911+
2912+
const respondToNextRequest = Effect.gen(function* () {
2913+
const requested = yield* Stream.runHead(adapter.streamEvents);
2914+
assert.equal(requested._tag, "Some");
2915+
if (requested._tag !== "Some" || requested.value.type !== "request.opened") {
2916+
return;
2917+
}
2918+
const runtimeRequestId = requested.value.requestId;
2919+
assert.equal(typeof runtimeRequestId, "string");
2920+
if (runtimeRequestId === undefined) {
2921+
return;
2922+
}
2923+
yield* adapter.respondToRequest(
2924+
session.threadId,
2925+
ApprovalRequestId.make(runtimeRequestId),
2926+
"acceptForSession",
2927+
);
2928+
yield* Stream.take(adapter.streamEvents, 1).pipe(Stream.runDrain);
2929+
});
2930+
2931+
// MCP tools frequently arrive with no usable suggestion (Claude Code
2932+
// sends an empty array); the decision must still stick for the session.
2933+
const mcpPermissionPromise = canUseTool(
2934+
"mcp__linear__create_issue",
2935+
{ title: "hello" },
2936+
{
2937+
signal: new AbortController().signal,
2938+
suggestions: [],
2939+
toolUseID: "tool-use-mcp-1",
2940+
},
2941+
);
2942+
yield* respondToNextRequest;
2943+
const mcpPermission = (yield* Effect.promise(() => mcpPermissionPromise)) as PermissionResult;
2944+
assert.equal(mcpPermission.behavior, "allow");
2945+
if (mcpPermission.behavior !== "allow") {
2946+
return;
2947+
}
2948+
assert.deepEqual(mcpPermission.updatedPermissions, [
2949+
{
2950+
type: "addRules",
2951+
rules: [{ toolName: "mcp__linear__create_issue" }],
2952+
behavior: "allow",
2953+
destination: "session",
2954+
},
2955+
]);
2956+
2957+
// Received suggestions are reused but rescoped to the session —
2958+
// echoing "localSettings" would persist a session-only choice to disk.
2959+
const bashPermissionPromise = canUseTool(
2960+
"Bash",
2961+
{ command: "git status" },
2962+
{
2963+
signal: new AbortController().signal,
2964+
suggestions: [
2965+
{
2966+
type: "addRules",
2967+
rules: [{ toolName: "Bash", ruleContent: "git status" }],
2968+
behavior: "allow",
2969+
destination: "localSettings",
2970+
},
2971+
],
2972+
toolUseID: "tool-use-bash-1",
2973+
},
2974+
);
2975+
yield* respondToNextRequest;
2976+
const bashPermission = (yield* Effect.promise(
2977+
() => bashPermissionPromise,
2978+
)) as PermissionResult;
2979+
assert.equal(bashPermission.behavior, "allow");
2980+
if (bashPermission.behavior !== "allow") {
2981+
return;
2982+
}
2983+
assert.deepEqual(bashPermission.updatedPermissions, [
2984+
{
2985+
type: "addRules",
2986+
rules: [{ toolName: "Bash", ruleContent: "git status" }],
2987+
behavior: "allow",
2988+
destination: "session",
2989+
},
2990+
]);
2991+
}).pipe(
2992+
Effect.provideService(Random.Random, makeDeterministicRandomService()),
2993+
Effect.provide(harness.layer),
2994+
);
2995+
});
2996+
28852997
it.effect("classifies Agent tools and read-only Claude tools correctly for approvals", () => {
28862998
const harness = makeHarness();
28872999
return Effect.gen(function* () {

‎apps/server/src/provider/Layers/ClaudeAdapter.ts‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,37 @@ interface PendingApproval {
155155
readonly decision: Deferred.Deferred<ProviderApprovalDecision>;
156156
}
157157

158+
/**
159+
* Permission updates applied for an "Always allow this session" decision.
160+
*
161+
* Claude Code's suggestions are reused when present but rescoped to
162+
* `destination: "session"` — echoing them verbatim would persist the
163+
* session-only choice as a permanent rule (suggestions typically target
164+
* `localSettings`, i.e. `.claude/settings.local.json`). When Claude Code
165+
* offers no suggestion — common for MCP tools — fall back to a whole-tool
166+
* session allow rule so the decision still sticks for the session instead of
167+
* silently degrading into a one-shot accept.
168+
*/
169+
function toSessionPermissionUpdates(
170+
toolName: string,
171+
suggestions: ReadonlyArray<PermissionUpdate> | undefined,
172+
): Array<PermissionUpdate> {
173+
const sessionScoped = (suggestions ?? []).map(
174+
(suggestion): PermissionUpdate => ({ ...suggestion, destination: "session" }),
175+
);
176+
if (sessionScoped.length > 0) {
177+
return sessionScoped;
178+
}
179+
return [
180+
{
181+
type: "addRules",
182+
rules: [{ toolName }],
183+
behavior: "allow",
184+
destination: "session",
185+
},
186+
];
187+
}
188+
158189
interface PendingUserInput {
159190
readonly questions: ReadonlyArray<UserInputQuestion>;
160191
readonly answers: Deferred.Deferred<ProviderUserInputAnswers>;
@@ -3465,9 +3496,12 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
34653496
return {
34663497
behavior: "allow",
34673498
updatedInput: toolInput,
3468-
...(decision === "acceptForSession" && pendingApproval.suggestions
3499+
...(decision === "acceptForSession"
34693500
? {
3470-
updatedPermissions: [...pendingApproval.suggestions],
3501+
updatedPermissions: toSessionPermissionUpdates(
3502+
toolName,
3503+
pendingApproval.suggestions,
3504+
),
34713505
}
34723506
: {}),
34733507
} satisfies PermissionResult;

0 commit comments

Comments
 (0)