Skip to content
Merged
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
7 changes: 7 additions & 0 deletions packages/agent/src/adapters/claude/claude-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,9 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
}
const previousMode = this.session.permissionMode;
this.session.permissionMode = modeId as CodeExecutionMode;
if (modeId === "plan" && previousMode !== "plan") {
this.session.modeBeforePlan = previousMode;
}
try {
await this.session.query.setPermissionMode(modeId as CodeExecutionMode);
} catch (error) {
Expand Down Expand Up @@ -1343,7 +1346,11 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
private createOnModeChange() {
return async (newMode: CodeExecutionMode) => {
if (this.session) {
const previousMode = this.session.permissionMode;
this.session.permissionMode = newMode;
if (newMode === "plan" && previousMode !== "plan") {
this.session.modeBeforePlan = previousMode;
}
}
await this.updateConfigOption("mode", newMode);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ async function requestPlanApproval(
context: ToolHandlerContext,
updatedInput: Record<string, unknown>,
): Promise<RequestPermissionResponse> {
const { client, sessionId, toolUseID } = context;
const { client, sessionId, toolUseID, session } = context;

const toolInfo = toolInfoFromToolUse({
name: context.toolName,
input: updatedInput,
});

return await client.requestPermission({
options: buildExitPlanModePermissionOptions(),
options: buildExitPlanModePermissionOptions(session.modeBeforePlan),
sessionId,
toolCall: {
toolCallId: toolUseID,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, it } from "vitest";
import { buildExitPlanModePermissionOptions } from "./permission-options";

describe("buildExitPlanModePermissionOptions", () => {
it("does not relabel any option when no previous mode is provided", () => {
const options = buildExitPlanModePermissionOptions();
for (const opt of options) {
expect(opt.name).not.toMatch(/^Yes, continue/);
}
expect(options[options.length - 1].optionId).toBe("reject_with_feedback");
});

it("promotes the previous mode to the first position with a continue label", () => {
const options = buildExitPlanModePermissionOptions("default");
expect(options[0]).toMatchObject({
optionId: "default",
name: "Yes, continue manually approving edits",
});
expect(options[options.length - 1].optionId).toBe("reject_with_feedback");
});

it("relabels the auto option when it is the previous mode", () => {
const options = buildExitPlanModePermissionOptions("auto");
expect(options[0]).toMatchObject({
optionId: "auto",
name: 'Yes, continue in "auto" mode',
});
});

it("relabels the acceptEdits option when it is the previous mode", () => {
const options = buildExitPlanModePermissionOptions("acceptEdits");
expect(options[0]).toMatchObject({
optionId: "acceptEdits",
name: "Yes, continue auto-accepting edits",
});
});

it("ignores an unknown previous mode", () => {
const options = buildExitPlanModePermissionOptions("plan");
expect(options[0].name).toMatch(/^Yes, /);
expect(options[0].name).not.toMatch(/^Yes, continue/);
expect(options[options.length - 1].optionId).toBe("reject_with_feedback");
});
Comment on lines +15 to +43
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Prefer a parameterised test for mode relabeling cases

The three tests for default, auto, and acceptEdits are structurally identical — call with a previousMode, assert options[0] has the expected optionId and name. Per the team's rule of always preferring parameterised tests, these should be collapsed into one table-driven test:

it.each([
  ["default", "default", "Yes, continue manually approving edits"],
  ["auto", "auto", 'Yes, continue in "auto" mode'],
  ["acceptEdits", "acceptEdits", "Yes, continue auto-accepting edits"],
])(
  "promotes %s to the first position with the correct continue label",
  (previousMode, expectedId, expectedName) => {
    const options = buildExitPlanModePermissionOptions(previousMode);
    expect(options[0]).toMatchObject({ optionId: expectedId, name: expectedName });
    expect(options[options.length - 1].optionId).toBe("reject_with_feedback");
  },
);

This also folds in the "reject option is always last" assertion, making the coverage obvious at a glance.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/agent/src/adapters/claude/permissions/permission-options.test.ts
Line: 15-43

Comment:
**Prefer a parameterised test for mode relabeling cases**

The three tests for `default`, `auto`, and `acceptEdits` are structurally identical — call with a `previousMode`, assert `options[0]` has the expected `optionId` and `name`. Per the team's rule of always preferring parameterised tests, these should be collapsed into one table-driven test:

```typescript
it.each([
  ["default", "default", "Yes, continue manually approving edits"],
  ["auto", "auto", 'Yes, continue in "auto" mode'],
  ["acceptEdits", "acceptEdits", "Yes, continue auto-accepting edits"],
])(
  "promotes %s to the first position with the correct continue label",
  (previousMode, expectedId, expectedName) => {
    const options = buildExitPlanModePermissionOptions(previousMode);
    expect(options[0]).toMatchObject({ optionId: expectedId, name: expectedName });
    expect(options[options.length - 1].optionId).toBe("reject_with_feedback");
  },
);
```

This also folds in the "reject option is always last" assertion, making the coverage obvious at a glance.

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1


it("always keeps the reject option last", () => {
for (const previousMode of ["auto", "acceptEdits", "default", undefined]) {
const options = buildExitPlanModePermissionOptions(previousMode);
expect(options[options.length - 1].optionId).toBe("reject_with_feedback");
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@ export function buildPermissionOptions(
return permissionOptions("Yes, always allow");
}

export function buildExitPlanModePermissionOptions(): PermissionOption[] {
const CONTINUE_LABELS: Record<string, string> = {
auto: 'Yes, continue in "auto" mode',
acceptEdits: "Yes, continue auto-accepting edits",
default: "Yes, continue manually approving edits",
bypassPermissions: "Yes, continue bypassing all permissions",
};

export function buildExitPlanModePermissionOptions(
previousMode?: string,
): PermissionOption[] {
const options: PermissionOption[] = [];

if (ALLOW_BYPASS) {
Expand All @@ -119,13 +128,30 @@ export function buildExitPlanModePermissionOptions(): PermissionOption[] {
name: "Yes, and manually approve edits",
optionId: "default",
},
{
kind: "reject_once",
name: "No, and tell the agent what to do differently",
optionId: "reject_with_feedback",
_meta: { customInput: true },
},
);

const previousIndex = previousMode
? options.findIndex((opt) => opt.optionId === previousMode)
: -1;
if (previousIndex > 0) {
const [previous] = options.splice(previousIndex, 1);
const continueLabel = CONTINUE_LABELS[previous.optionId];
options.unshift(
continueLabel ? { ...previous, name: continueLabel } : previous,
);
} else if (previousIndex === 0) {
const continueLabel = CONTINUE_LABELS[options[0].optionId];
if (continueLabel) {
options[0] = { ...options[0], name: continueLabel };
}
}

options.push({
kind: "reject_once",
name: "No, and tell the agent what to do differently",
optionId: "reject_with_feedback",
_meta: { customInput: true },
});

return options;
}
1 change: 1 addition & 0 deletions packages/agent/src/adapters/claude/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type Session = BaseSession & {
input: Pushable<SDKUserMessage>;
settingsManager: SettingsManager;
permissionMode: CodeExecutionMode;
modeBeforePlan?: CodeExecutionMode;
modelId?: string;
cwd: string;
taskRunId?: string;
Expand Down
Loading