fix(claude): make "Always allow for session" stick, and only for the session (#5041)

Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
Joaquin NavarroandClaude Fable 5 authored and GitHub committed 2026-08-15 12:05:21 +00:00
1 parent 551f4c99c1
commit e9e46972fb
2 files changed
+148 -2

No files matched your search

@@ -3360,6 +3360,118 @@ describe("ClaudeAdapterLive", () => {
);
});
it.effect("acceptForSession returns session-scoped permission updates", () => {
const harness = makeHarness();
return Effect.gen(function* () {
const adapter = yield* ClaudeAdapter;
const session = yield* adapter.startSession({
threadId: THREAD_ID,
provider: ProviderDriverKind.make("claudeAgent"),
runtimeMode: "approval-required",
});
yield* Stream.take(adapter.streamEvents, 3).pipe(Stream.runDrain);
yield* adapter.sendTurn({
threadId: session.threadId,
input: "approve this for the session",
attachments: [],
});
yield* Stream.take(adapter.streamEvents, 1).pipe(Stream.runDrain);
const createInput = harness.getLastCreateQueryInput();
const canUseTool = createInput?.options.canUseTool;
assert.equal(typeof canUseTool, "function");
if (!canUseTool) {
return;
}
const respondToNextRequest = Effect.gen(function* () {
const requested = yield* Stream.runHead(adapter.streamEvents);
assert.equal(requested._tag, "Some");
if (requested._tag !== "Some" || requested.value.type !== "request.opened") {
return;
}
const runtimeRequestId = requested.value.requestId;
assert.equal(typeof runtimeRequestId, "string");
if (runtimeRequestId === undefined) {
return;
}
yield* adapter.respondToRequest(
session.threadId,
ApprovalRequestId.make(runtimeRequestId),
"acceptForSession",
);
yield* Stream.take(adapter.streamEvents, 1).pipe(Stream.runDrain);
});
// MCP tools frequently arrive with no usable suggestion (Claude Code
// sends an empty array); the decision must still stick for the session.
const mcpPermissionPromise = canUseTool(
"mcp__linear__create_issue",
{ title: "hello" },
{
signal: new AbortController().signal,
suggestions: [],
toolUseID: "tool-use-mcp-1",
},
);
yield* respondToNextRequest;
const mcpPermission = (yield* Effect.promise(() => mcpPermissionPromise)) as PermissionResult;
assert.equal(mcpPermission.behavior, "allow");
if (mcpPermission.behavior !== "allow") {
return;
}
assert.deepEqual(mcpPermission.updatedPermissions, [
{
type: "addRules",
rules: [{ toolName: "mcp__linear__create_issue" }],
behavior: "allow",
destination: "session",
},
]);
// Received suggestions are reused but rescoped to the session —
// echoing "localSettings" would persist a session-only choice to disk.
const bashPermissionPromise = canUseTool(
"Bash",
{ command: "git status" },
{
signal: new AbortController().signal,
suggestions: [
{
type: "addRules",
rules: [{ toolName: "Bash", ruleContent: "git status" }],
behavior: "allow",
destination: "localSettings",
},
],
toolUseID: "tool-use-bash-1",
},
);
yield* respondToNextRequest;
const bashPermission = (yield* Effect.promise(
() => bashPermissionPromise,
)) as PermissionResult;
assert.equal(bashPermission.behavior, "allow");
if (bashPermission.behavior !== "allow") {
return;
}
assert.deepEqual(bashPermission.updatedPermissions, [
{
type: "addRules",
rules: [{ toolName: "Bash", ruleContent: "git status" }],
behavior: "allow",
destination: "session",
},
]);
}).pipe(
Effect.provideService(Random.Random, makeDeterministicRandomService()),
Effect.provide(harness.layer),
);
});
it.effect("classifies Agent tools and read-only Claude tools correctly for approvals", () => {
const harness = makeHarness();
return Effect.gen(function* () {
@@ -160,6 +160,37 @@ interface PendingApproval {
readonly decision: Deferred.Deferred<ProviderApprovalDecision>;
}
/**
* Permission updates applied for an "Always allow this session" decision.
*
* Claude Code's suggestions are reused when present but rescoped to
* `destination: "session"` — echoing them verbatim would persist the
* session-only choice as a permanent rule (suggestions typically target
* `localSettings`, i.e. `.claude/settings.local.json`). When Claude Code
* offers no suggestion — common for MCP tools — fall back to a whole-tool
* session allow rule so the decision still sticks for the session instead of
* silently degrading into a one-shot accept.
*/
function toSessionPermissionUpdates(
toolName: string,
suggestions: ReadonlyArray<PermissionUpdate> | undefined,
): Array<PermissionUpdate> {
const sessionScoped = (suggestions ?? []).map(
(suggestion): PermissionUpdate => ({ ...suggestion, destination: "session" }),
);
if (sessionScoped.length > 0) {
return sessionScoped;
}
return [
{
type: "addRules",
rules: [{ toolName }],
behavior: "allow",
destination: "session",
},
];
}
interface PendingUserInput {
readonly questions: ReadonlyArray<UserInputQuestion>;
readonly answers: Deferred.Deferred<ProviderUserInputAnswers>;
@@ -4039,9 +4070,12 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
return {
behavior: "allow",
updatedInput: toolInput,
...(decision === "acceptForSession" && pendingApproval.suggestions
...(decision === "acceptForSession"
? {
updatedPermissions: [...pendingApproval.suggestions],
updatedPermissions: toSessionPermissionUpdates(
toolName,
pendingApproval.suggestions,
),
}
: {}),
} satisfies PermissionResult;