Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 132 additions & 122 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1147,134 +1147,144 @@ describe("ClaudeAdapterV2 session permissions", () => {
});
});

describe("ClaudeAdapterV2 Auto-accept edits", () => {
it.effect("asks before a command instead of allowing it", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const idAllocator = yield* IdAllocator.IdAllocatorV2;
const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-claude-accept-edits-",
});
let openedOptions: ClaudeAdapterV2.ClaudeAgentSdkQueryOptions | undefined;
const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
settings: DEFAULT_CLAUDE_SETTINGS,
environment: {},
attachmentsDir,
fileSystem,
path: yield* Path.Path,
random: yield* SecureRandom.SecureRandom,
idAllocator,
queryRunner: {
allocateSessionId: Effect.succeed("native-thread-claude-accept-edits"),
open: (input) =>
Effect.sync(() => {
openedOptions = input.options;
return {
messages: Stream.never,
offer: () => Effect.void,
setModel: () => Effect.void,
applyFlagSettings: () => Effect.void,
setPermissionMode: () => Effect.void,
renameSession: () => Effect.void,
backgroundTasks: () => Effect.succeed(false),
mcpServerStatus: Effect.succeed([]),
readMcpResource: () => Effect.die("unexpected MCP resource read"),
callMcpTool: () => Effect.die("unexpected MCP tool call"),
mcpAuthenticate: () => Effect.die("unexpected MCP sign-in"),
mcpSubmitOAuthCallbackUrl: () => Effect.die("unexpected MCP sign-in"),
reconnectMcpServer: () => Effect.die("unexpected MCP reconnect"),
listPermissionRules: Effect.die("unexpected permission rules read"),
stopTask: () => Effect.void,
interrupt: Effect.void,
close: Effect.void,
};
}),
forkSession: () => Effect.die("unused"),
subagentLaunchToolUseId: () => Effect.succeed(null),
subagentModel: () => Effect.succeed(null),
assertComplete: Effect.void,
},
});
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
runtimeMode: "auto-accept-edits",
interactionMode: "default",
cwd: "/workspace",
});
const threadId = ThreadId.make("thread-claude-accept-edits");
const runtime = yield* adapter.openSession({
threadId,
providerSessionId: ProviderSessionId.make("provider-session-claude-accept-edits"),
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
runtimePolicy,
});
const providerThread = yield* runtime.ensureThread({
threadId,
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
runtimePolicy,
});
const now = yield* DateTime.now;
yield* runtime.startTurn(
makeClaudeTestTurnInput({
describe("ClaudeAdapterV2 escalated permission requests", () => {
// Claude sends only requests it wants a person to decide (ask rules, hooks, classifier fallback) to canUseTool.
it.effect.each([
{ runtimeMode: "auto-accept-edits", permissionMode: "acceptEdits" },
{ runtimeMode: "auto", permissionMode: "auto" },
] as const)(
"$runtimeMode asks before a command Claude escalates instead of allowing it",
({ runtimeMode, permissionMode }) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const idAllocator = yield* IdAllocator.IdAllocatorV2;
const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-claude-accept-edits-",
});
let openedOptions: ClaudeAdapterV2.ClaudeAgentSdkQueryOptions | undefined;
const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
settings: DEFAULT_CLAUDE_SETTINGS,
environment: {},
attachmentsDir,
fileSystem,
path: yield* Path.Path,
random: yield* SecureRandom.SecureRandom,
idAllocator,
queryRunner: {
allocateSessionId: Effect.succeed("native-thread-claude-accept-edits"),
open: (input) =>
Effect.sync(() => {
openedOptions = input.options;
return {
messages: Stream.never,
offer: () => Effect.void,
setModel: () => Effect.void,
applyFlagSettings: () => Effect.void,
setPermissionMode: () => Effect.void,
renameSession: () => Effect.void,
backgroundTasks: () => Effect.succeed(false),
mcpServerStatus: Effect.succeed([]),
readMcpResource: () => Effect.die("unexpected MCP resource read"),
callMcpTool: () => Effect.die("unexpected MCP tool call"),
mcpAuthenticate: () => Effect.die("unexpected MCP sign-in"),
mcpSubmitOAuthCallbackUrl: () => Effect.die("unexpected MCP sign-in"),
reconnectMcpServer: () => Effect.die("unexpected MCP reconnect"),
listPermissionRules: Effect.die("unexpected permission rules read"),
stopTask: () => Effect.void,
interrupt: Effect.void,
close: Effect.void,
};
}),
forkSession: () => Effect.die("unused"),
subagentLaunchToolUseId: () => Effect.succeed(null),
subagentModel: () => Effect.succeed(null),
assertComplete: Effect.void,
},
});
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
runtimeMode,
interactionMode: "default",
cwd: "/workspace",
});
const threadId = ThreadId.make("thread-claude-accept-edits");
const runtime = yield* adapter.openSession({
threadId,
providerThread,
now,
attemptId: RunAttemptId.make("attempt-claude-accept-edits"),
text: "Run node.",
attachments: [],
providerSessionId: ProviderSessionId.make("provider-session-claude-accept-edits"),
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
runtimePolicy,
}),
);
assert.equal(openedOptions?.permissionMode, "acceptEdits");
const canUseTool = openedOptions?.canUseTool;
assert.isFunction(canUseTool);
});
const providerThread = yield* runtime.ensureThread({
threadId,
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
runtimePolicy,
});
const now = yield* DateTime.now;
yield* runtime.startTurn(
makeClaudeTestTurnInput({
threadId,
providerThread,
now,
attemptId: RunAttemptId.make("attempt-claude-accept-edits"),
text: "Run node.",
attachments: [],
runtimePolicy,
}),
);
assert.equal(openedOptions?.permissionMode, permissionMode);
const canUseTool = openedOptions?.canUseTool;
assert.isFunction(canUseTool);

const requestEvent = yield* runtime.events.pipe(
Stream.takeUntil(
(event) =>
event.type === "turn_item.updated" && event.turnItem.type === "approval_request",
),
Stream.runCollect,
Effect.forkScoped,
);
const command = { command: "node -e 'console.log(42)'" };
const decision = yield* Effect.promise(() =>
canUseTool!("Bash", command, {
signal: new AbortController().signal,
toolUseID: "tool-bash-accept-edits",
requestId: "request-bash-accept-edits",
description: "Check the Node runtime",
}),
).pipe(Effect.forkScoped);
const first = yield* Effect.raceFirst(
Fiber.join(requestEvent).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", "the command ran without asking");
if (first.type !== "request") return;
const event = first.event.find((event) => event.type === "runtime_request.updated");
assert.equal(event?.runtimeRequest.kind, "command");
const approval = first.event.findLast((event) => event.type === "turn_item.updated");
assert.equal(
approval?.turnItem.type === "approval_request" ? approval.turnItem.prompt : undefined,
command.command,
);
const requestEvent = yield* runtime.events.pipe(
Stream.takeUntil(
(event) =>
event.type === "turn_item.updated" && event.turnItem.type === "approval_request",
),
Stream.runCollect,
Effect.forkScoped,
);
const command = { command: "node -e 'console.log(42)'" };
const decision = yield* Effect.promise(() =>
canUseTool!("Bash", command, {
signal: new AbortController().signal,
toolUseID: "tool-bash-accept-edits",
requestId: "request-bash-accept-edits",
description: "Check the Node runtime",
}),
).pipe(Effect.forkScoped);
const first = yield* Effect.raceFirst(
Fiber.join(requestEvent).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", "the command ran without asking");
if (first.type !== "request") return;
const event = first.event.find((event) => event.type === "runtime_request.updated");
assert.equal(event?.runtimeRequest.kind, "command");
const approval = first.event.findLast((event) => event.type === "turn_item.updated");
assert.equal(
approval?.turnItem.type === "approval_request" ? approval.turnItem.prompt : undefined,
command.command,
);

yield* runtime.respondToRuntimeRequest({
requestId: event!.runtimeRequest.id,
decision: "accept",
});
assert.equal((yield* Fiber.join(decision))?.behavior, "allow");
}).pipe(
Effect.provide(
Layer.mergeAll(
IdAllocator.layer,
McpProviderSessions.layer,
NodeServices.layer,
NodeCrypto.layer,
yield* runtime.respondToRuntimeRequest({
requestId: event!.runtimeRequest.id,
decision: "accept",
});
assert.equal((yield* Fiber.join(decision))?.behavior, "allow");
}).pipe(
Effect.provide(
Layer.mergeAll(
IdAllocator.layer,
McpProviderSessions.layer,
NodeServices.layer,
NodeCrypto.layer,
),
),
),
),
);

it.effect("keeps an MCP approval prompt on a code point boundary", () =>
Expand Down
10 changes: 7 additions & 3 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3670,10 +3670,14 @@ function claudeBypassAvailability(
}

function requiresClaudeApproval(context: ActiveClaudeTurnContext): boolean {
// CLI-forwarded requests need a person's decision even under bypass (ask rules,
// safety checks, or settings fallback). Do not wait for queued init/status updates.
// CLI-forwarded requests need a person's decision even under auto or bypass (ask rules,
// hooks, safety checks, or classifier fallback). Do not wait for queued init/status updates.
const policy = context.queryPolicy;
return policy.installPermissionCallback || policy.permissionMode === "bypassPermissions";
return (
policy.installPermissionCallback ||
policy.permissionMode === "auto" ||
policy.permissionMode === "bypassPermissions"
);
}

function claudeRuntimeQueryPolicyKey(policy: ClaudeRuntimeQueryPolicy): string {
Expand Down
Loading