fix(omp): allowing a tool approval approves it when OMP offers session or grant choices (#10126)
* fix(omp): map tool approvals that offer session or grant choices OMP 18.x adds "Approve <tool> for this session" and directory/file grant choices to its rpc-ui approval select. The mapper only recognised the exact ["Approve", "Deny"] options, so these prompts fell through to a generic "OMP select" question; allowing it from the CLI or a notification answered with no value and OMP treated the call as denied. Recognise approval selects whose one-call choice is "Approve" or "Approve Once", whose last choice is "Deny", and whose other choices are approval labels. Surface the extra choices as allow actions and answer with the selected label. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> * fix(omp): allow one call when a grant is labeled Approve OMP names the one-call choice "Approve Once" whenever it also offers grants. A grant for a target that is not a file is then labeled "Approve", so the select arrives as ["Approve", "Always Approve", "Approve Once", "Deny"]. Taking the first "Approve" as the one-call choice made a plain allow pick the grant, which remembers more than the call. Prefer "Approve Once" when it is offered. Co-authored-by: Dhruv Singal <dhruvsingalabc@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(omp): read the picked approval choice from its action Each extra approval choice's action label is the select value OMP offered, so the response reads it from request.actions instead of a second copy in metadata. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Dhruv Singal <dhruvsingalabc@gmail.com> Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2 files changed
+155
-13
No files matched your search
@@ -136,6 +136,94 @@ describe("OMP rpc-ui permission mapper", () => {
|
||||
).toEqual({ kind: "passthrough" });
|
||||
});
|
||||
|
||||
test("maps approvals that offer session and grant choices beside the one-call approval", () => {
|
||||
const session = classifyOmpRpcUiPermissionRequest({
|
||||
type: "extension_ui_request",
|
||||
id: "session-write",
|
||||
method: "select",
|
||||
title: "Allow tool: write\nPath: cap.txt\nContent:\nhello",
|
||||
options: ["Approve", "Approve write for this session", "Deny"],
|
||||
});
|
||||
if (session.kind !== "tool") throw new Error("Expected session approval to map as a tool");
|
||||
expect(
|
||||
session.request.actions?.map((action) => [action.id, action.label, action.behavior]),
|
||||
).toEqual([
|
||||
["deny", "Deny", "deny"],
|
||||
["approve", "Approve", "allow"],
|
||||
["approve-option-0", "Approve write for this session", "allow"],
|
||||
]);
|
||||
expect(buildOmpRpcUiPermissionResponse(session.request, { behavior: "allow" })).toEqual({
|
||||
value: "Approve",
|
||||
});
|
||||
expect(
|
||||
buildOmpRpcUiPermissionResponse(session.request, {
|
||||
behavior: "allow",
|
||||
selectedActionId: "approve-option-0",
|
||||
}),
|
||||
).toEqual({ value: "Approve write for this session" });
|
||||
expect(
|
||||
buildOmpRpcUiPermissionResponse(session.request, {
|
||||
behavior: "deny",
|
||||
selectedActionId: "approve-option-0",
|
||||
}),
|
||||
).toEqual({ value: "Deny" });
|
||||
|
||||
const grants = classifyOmpRpcUiPermissionRequest({
|
||||
type: "extension_ui_request",
|
||||
id: "grant-edit",
|
||||
method: "select",
|
||||
title: "Allow tool: edit\nFile: src/a.ts",
|
||||
options: ["Approve File", "Always Approve File", "Approve Once", "Deny"],
|
||||
});
|
||||
if (grants.kind !== "tool") throw new Error("Expected grant approval to map as a tool");
|
||||
expect(buildOmpRpcUiPermissionResponse(grants.request, { behavior: "allow" })).toEqual({
|
||||
value: "Approve Once",
|
||||
});
|
||||
expect(
|
||||
buildOmpRpcUiPermissionResponse(grants.request, {
|
||||
behavior: "allow",
|
||||
selectedActionId: "approve-option-1",
|
||||
}),
|
||||
).toEqual({ value: "Always Approve File" });
|
||||
});
|
||||
|
||||
test("allows one call when a grant is labeled Approve beside Approve Once", () => {
|
||||
// OMP names the one-call choice "Approve Once" whenever it offers grants. A grant for a
|
||||
// target that is not a file is then labeled "Approve", which remembers more than one call.
|
||||
const grants = classifyOmpRpcUiPermissionRequest({
|
||||
type: "extension_ui_request",
|
||||
id: "grant-target",
|
||||
method: "select",
|
||||
title: "Allow tool: edit\nFile: src/a.ts",
|
||||
options: ["Approve", "Always Approve", "Approve Once", "Deny"],
|
||||
});
|
||||
if (grants.kind !== "tool") throw new Error("Expected grant approval to map as a tool");
|
||||
expect(buildOmpRpcUiPermissionResponse(grants.request, { behavior: "allow" })).toEqual({
|
||||
value: "Approve Once",
|
||||
});
|
||||
});
|
||||
|
||||
test("passes through selects whose extra choices are not approvals", () => {
|
||||
expect(
|
||||
classifyOmpRpcUiPermissionRequest({
|
||||
type: "extension_ui_request",
|
||||
id: "odd-choice",
|
||||
method: "select",
|
||||
title: "Allow tool: bash\nCommand: echo hi",
|
||||
options: ["Approve", "Edit command", "Deny"],
|
||||
}),
|
||||
).toEqual({ kind: "passthrough" });
|
||||
expect(
|
||||
classifyOmpRpcUiPermissionRequest({
|
||||
type: "extension_ui_request",
|
||||
id: "deny-first",
|
||||
method: "select",
|
||||
title: "Allow tool: bash\nCommand: echo hi",
|
||||
options: ["Deny", "Approve"],
|
||||
}),
|
||||
).toEqual({ kind: "passthrough" });
|
||||
});
|
||||
|
||||
test("responds to tool approvals with exact select values", () => {
|
||||
const request = toolApproval("bash");
|
||||
|
||||
|
||||
@@ -10,8 +10,13 @@ import type { OmpRuntimeEvent } from "./rpc-types.js";
|
||||
const OMP_PROVIDER = "omp";
|
||||
const OMP_RPC_UI_TOOL_APPROVAL_METADATA = "omp_rpc_ui_tool_approval";
|
||||
const TOOL_APPROVAL_APPROVE_VALUE = "Approve";
|
||||
// OMP labels the approve-this-call choice "Approve Once" when it also offers grants that remember
|
||||
// more. A grant can then be labeled "Approve", so "Approve" is the one-call choice only without it.
|
||||
const TOOL_APPROVAL_ONCE_VALUE = "Approve Once";
|
||||
const TOOL_APPROVAL_DENY_VALUE = "Deny";
|
||||
const TOOL_APPROVAL_OPTIONS = [TOOL_APPROVAL_APPROVE_VALUE, TOOL_APPROVAL_DENY_VALUE] as const;
|
||||
// Session and grant choices OMP offers beside the one-call approval, e.g. "Approve write for this session".
|
||||
const TOOL_APPROVAL_EXTRA_CHOICE = /^(?:Always )?Approve\b/;
|
||||
const TOOL_APPROVAL_EXTRA_ACTION_PREFIX = "approve-option-";
|
||||
const TOOL_TITLE_PREFIX = "Allow tool: ";
|
||||
|
||||
export type OmpRpcUiPermissionClassification =
|
||||
@@ -25,6 +30,12 @@ interface ToolApprovalDescriptor {
|
||||
args: AgentMetadata;
|
||||
detail: ToolCallDetail;
|
||||
description?: string;
|
||||
choices: ToolApprovalChoices;
|
||||
}
|
||||
|
||||
interface ToolApprovalChoices {
|
||||
approveValue: string;
|
||||
extraValues: string[];
|
||||
}
|
||||
|
||||
export function classifyOmpRpcUiPermissionRequest(
|
||||
@@ -37,6 +48,13 @@ export function classifyOmpRpcUiPermissionRequest(
|
||||
}
|
||||
|
||||
const provider = options.provider ?? OMP_PROVIDER;
|
||||
const { approveValue, extraValues } = descriptor.choices;
|
||||
const extraActions = extraValues.map((value, index) => ({
|
||||
id: `${TOOL_APPROVAL_EXTRA_ACTION_PREFIX}${index}`,
|
||||
label: value,
|
||||
behavior: "allow" as const,
|
||||
variant: "secondary" as const,
|
||||
}));
|
||||
return {
|
||||
kind: "tool",
|
||||
request: {
|
||||
@@ -57,17 +75,18 @@ export function classifyOmpRpcUiPermissionRequest(
|
||||
},
|
||||
{
|
||||
id: "approve",
|
||||
label: TOOL_APPROVAL_APPROVE_VALUE,
|
||||
label: approveValue,
|
||||
behavior: "allow",
|
||||
variant: "primary",
|
||||
},
|
||||
...extraActions,
|
||||
],
|
||||
metadata: {
|
||||
extensionUiMethod: "select",
|
||||
toolApproval: OMP_RPC_UI_TOOL_APPROVAL_METADATA,
|
||||
toolName: descriptor.toolName,
|
||||
toolArgs: descriptor.args,
|
||||
approveValue: TOOL_APPROVAL_APPROVE_VALUE,
|
||||
approveValue,
|
||||
denyValue: TOOL_APPROVAL_DENY_VALUE,
|
||||
},
|
||||
},
|
||||
@@ -95,13 +114,23 @@ export function buildOmpRpcUiPermissionResponse(
|
||||
|
||||
const approveValue = readString(request.metadata.approveValue) ?? TOOL_APPROVAL_APPROVE_VALUE;
|
||||
const denyValue = readString(request.metadata.denyValue) ?? TOOL_APPROVAL_DENY_VALUE;
|
||||
return { value: response.behavior === "allow" ? approveValue : denyValue };
|
||||
if (response.behavior !== "allow") {
|
||||
return { value: denyValue };
|
||||
}
|
||||
// Each extra choice's action label is the exact select value OMP offered.
|
||||
const selectedExtraChoice = request.actions?.find(
|
||||
(action) =>
|
||||
action.id === response.selectedActionId &&
|
||||
action.id.startsWith(TOOL_APPROVAL_EXTRA_ACTION_PREFIX),
|
||||
);
|
||||
return { value: selectedExtraChoice?.label ?? approveValue };
|
||||
}
|
||||
|
||||
function parseToolApprovalDescriptor(
|
||||
event: ExtensionUiRequestEvent,
|
||||
): ToolApprovalDescriptor | null {
|
||||
if (event.method !== "select" || !hasExactToolApprovalOptions(event.options)) {
|
||||
const choices = event.method === "select" ? readToolApprovalChoices(event.options) : null;
|
||||
if (!choices) {
|
||||
return null;
|
||||
}
|
||||
const title = readString(event.title);
|
||||
@@ -118,6 +147,15 @@ function parseToolApprovalDescriptor(
|
||||
const body = firstLineBreak < 0 ? "" : title.slice(firstLineBreak).replace(/^\r?\n/, "");
|
||||
const bodyLines = body.split(/\r?\n/);
|
||||
|
||||
const parsed = parseToolApprovalBody(rawToolName, body, bodyLines);
|
||||
return parsed ? { ...parsed, choices } : null;
|
||||
}
|
||||
|
||||
function parseToolApprovalBody(
|
||||
rawToolName: string,
|
||||
body: string,
|
||||
bodyLines: string[],
|
||||
): Omit<ToolApprovalDescriptor, "choices"> | null {
|
||||
switch (rawToolName) {
|
||||
case "bash":
|
||||
return parseBashApproval(body);
|
||||
@@ -130,7 +168,7 @@ function parseToolApprovalDescriptor(
|
||||
}
|
||||
}
|
||||
|
||||
function parseBashApproval(body: string): ToolApprovalDescriptor | null {
|
||||
function parseBashApproval(body: string): Omit<ToolApprovalDescriptor, "choices"> | null {
|
||||
const match = /(?:^|\r?\n)[\t ]*Command:[\t ]?(.*)$/s.exec(body);
|
||||
const command = match?.[1];
|
||||
if (!command) {
|
||||
@@ -144,7 +182,7 @@ function parseBashApproval(body: string): ToolApprovalDescriptor | null {
|
||||
};
|
||||
}
|
||||
|
||||
function parseEditApproval(lines: string[]): ToolApprovalDescriptor | null {
|
||||
function parseEditApproval(lines: string[]): Omit<ToolApprovalDescriptor, "choices"> | null {
|
||||
const filePath = readPrefixedValue(lines, "File:");
|
||||
if (!filePath) {
|
||||
return null;
|
||||
@@ -157,7 +195,7 @@ function parseEditApproval(lines: string[]): ToolApprovalDescriptor | null {
|
||||
};
|
||||
}
|
||||
|
||||
function parseWriteApproval(lines: string[]): ToolApprovalDescriptor | null {
|
||||
function parseWriteApproval(lines: string[]): Omit<ToolApprovalDescriptor, "choices"> | null {
|
||||
const pathLineIndex = lines.findIndex((line) => line.trim().startsWith("Path:"));
|
||||
if (pathLineIndex < 0) {
|
||||
return null;
|
||||
@@ -182,12 +220,28 @@ function parseWriteApproval(lines: string[]): ToolApprovalDescriptor | null {
|
||||
};
|
||||
}
|
||||
|
||||
function hasExactToolApprovalOptions(options: unknown): boolean {
|
||||
return (
|
||||
Array.isArray(options) &&
|
||||
options.length === TOOL_APPROVAL_OPTIONS.length &&
|
||||
options.every((option, index) => option === TOOL_APPROVAL_OPTIONS[index])
|
||||
function readToolApprovalChoices(options: unknown): ToolApprovalChoices | null {
|
||||
if (!Array.isArray(options) || !options.every((option) => typeof option === "string")) {
|
||||
return null;
|
||||
}
|
||||
const labels = options as string[];
|
||||
if (
|
||||
labels.at(-1) !== TOOL_APPROVAL_DENY_VALUE ||
|
||||
labels.indexOf(TOOL_APPROVAL_DENY_VALUE) !== labels.length - 1
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const approveValue = [TOOL_APPROVAL_ONCE_VALUE, TOOL_APPROVAL_APPROVE_VALUE].find((value) =>
|
||||
labels.includes(value),
|
||||
);
|
||||
if (!approveValue) {
|
||||
return null;
|
||||
}
|
||||
const extraValues = labels.slice(0, -1).filter((label) => label !== approveValue);
|
||||
if (!extraValues.every((label) => TOOL_APPROVAL_EXTRA_CHOICE.test(label))) {
|
||||
return null;
|
||||
}
|
||||
return { approveValue, extraValues };
|
||||
}
|
||||
|
||||
function readPrefixedValue(lines: readonly string[], prefix: string): string | null {
|
||||
|
||||
Reference in new issue
Block a user