headless tool call permissions error msg
This commit is contained in:
parent
bac423c117
commit
328ff12e65
7 changed files with 116 additions and 107 deletions
|
|
@ -85,19 +85,22 @@ This file should be created the first time that the CLI starts.
|
|||
|
||||
## Headless mode permissions
|
||||
|
||||
When running in headless mode (using the `-p` or `--print` flag), the CLI uses different default policies designed for non-interactive usage:
|
||||
When running in headless mode (using the `-p` or `--print` flag), the CLI uses the same default policies as normal mode, but with different behavior for tools that require confirmation:
|
||||
|
||||
- **Normal mode**: Write operations and terminal commands require confirmation (`ask`)
|
||||
- **Headless mode**: All tools are allowed by default (`*: allow`)
|
||||
- **Normal mode**: Write operations and terminal commands require confirmation (`ask`) - user is prompted
|
||||
- **Headless mode**: Same default policies, but tools requiring confirmation (`ask`) will cause the process to exit with an error message
|
||||
|
||||
This behavior can still be overridden using the standard precedence rules. For example:
|
||||
To use tools that normally require confirmation in headless mode, you must explicitly allow them:
|
||||
|
||||
```bash
|
||||
# Headless mode with full access (default)
|
||||
cn -p "Write a hello world script"
|
||||
# Headless mode with explicit permissions for write operations
|
||||
cn -p --allow write_file "Write a hello world script"
|
||||
|
||||
# Headless mode with wildcard permission (allow all tools)
|
||||
cn -p --allow "*" "Write and run a script"
|
||||
|
||||
# Headless mode with specific restrictions
|
||||
cn -p --exclude Write "Clean up the codebase"
|
||||
cn -p --exclude run_terminal_command "Clean up the codebase"
|
||||
```
|
||||
|
||||
The headless default policies are defined in [`src/permissions/defaultPolicies.ts`](../src/permissions/defaultPolicies.ts) as `HEADLESS_TOOL_POLICIES`.
|
||||
This approach ensures that headless mode is secure by default while providing clear guidance on how to enable the needed permissions.
|
||||
|
|
|
|||
|
|
@ -27,11 +27,3 @@ export const DEFAULT_TOOL_POLICIES: ToolPermissionPolicy[] = [
|
|||
{ tool: "*", permission: "ask" },
|
||||
];
|
||||
|
||||
/**
|
||||
* Headless mode permission policies where all tools are allowed by default.
|
||||
* Used when running with the "-p" flag for full access without prompts.
|
||||
*/
|
||||
export const HEADLESS_TOOL_POLICIES: ToolPermissionPolicy[] = [
|
||||
// Allow all tools by default in headless mode
|
||||
{ tool: "*", permission: "allow" },
|
||||
];
|
||||
|
|
|
|||
|
|
@ -1,37 +1,28 @@
|
|||
import { resolvePermissionPrecedence } from "./precedenceResolver.js";
|
||||
import { HEADLESS_TOOL_POLICIES, DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
import { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
import { checkToolPermission } from "./permissionChecker.js";
|
||||
|
||||
describe("Headless Permissions Integration", () => {
|
||||
describe("precedence resolution with headless mode", () => {
|
||||
it("should use headless policies when headless: true", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
it("should use same default policies for both headless and normal modes", () => {
|
||||
const headlessPolicies = resolvePermissionPrecedence({
|
||||
useDefaults: true,
|
||||
personalSettings: false, // Disable to avoid file system dependencies
|
||||
headless: true,
|
||||
});
|
||||
|
||||
expect(policies).toEqual(HEADLESS_TOOL_POLICIES);
|
||||
expect(policies.length).toBe(1);
|
||||
expect(policies[0]).toEqual({ tool: "*", permission: "allow" });
|
||||
});
|
||||
|
||||
it("should use normal policies when headless: false", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
const normalPolicies = resolvePermissionPrecedence({
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
headless: false,
|
||||
});
|
||||
|
||||
expect(policies).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
expect(policies.length).toBeGreaterThan(1);
|
||||
|
||||
// Check that write operations require confirmation in normal mode
|
||||
const writePolicy = policies.find(p => p.tool === "write_file");
|
||||
expect(writePolicy?.permission).toBe("ask");
|
||||
expect(headlessPolicies).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
expect(normalPolicies).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
expect(headlessPolicies).toEqual(normalPolicies);
|
||||
});
|
||||
|
||||
it("should allow CLI overrides to supersede headless defaults", () => {
|
||||
it("should allow CLI overrides to supersede defaults in headless mode", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
commandLineFlags: {
|
||||
exclude: ["dangerous_tool"],
|
||||
|
|
@ -43,40 +34,32 @@ describe("Headless Permissions Integration", () => {
|
|||
|
||||
// CLI exclusion should come first
|
||||
expect(policies[0]).toEqual({ tool: "dangerous_tool", permission: "exclude" });
|
||||
// Headless wildcard should still be present
|
||||
expect(policies[1]).toEqual({ tool: "*", permission: "allow" });
|
||||
// Default policies should follow
|
||||
expect(policies.slice(1)).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
});
|
||||
});
|
||||
|
||||
describe("permission checking with headless policies", () => {
|
||||
it("should allow all tools with headless policies", () => {
|
||||
const permissions = { policies: HEADLESS_TOOL_POLICIES };
|
||||
describe("permission checking with default policies", () => {
|
||||
it("should follow default permission rules", () => {
|
||||
const permissions = { policies: DEFAULT_TOOL_POLICIES };
|
||||
|
||||
// Test various tool types
|
||||
const toolsToTest = [
|
||||
"write_file",
|
||||
"run_terminal_command",
|
||||
"read_file",
|
||||
"search_code",
|
||||
"some_unknown_tool",
|
||||
"mcp__custom_tool"
|
||||
];
|
||||
// Read-only tools should be allowed
|
||||
expect(checkToolPermission({ name: "read_file", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "list_files", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "search_code", arguments: {} }, permissions).permission).toBe("allow");
|
||||
|
||||
for (const toolName of toolsToTest) {
|
||||
const result = checkToolPermission(
|
||||
{ name: toolName, arguments: {} },
|
||||
permissions
|
||||
);
|
||||
|
||||
expect(result.permission).toBe("allow");
|
||||
expect(result.matchedPolicy).toEqual({ tool: "*", permission: "allow" });
|
||||
}
|
||||
// Write operations should require permission
|
||||
expect(checkToolPermission({ name: "write_file", arguments: {} }, permissions).permission).toBe("ask");
|
||||
expect(checkToolPermission({ name: "run_terminal_command", arguments: {} }, permissions).permission).toBe("ask");
|
||||
|
||||
// Unknown tools should default to ask
|
||||
expect(checkToolPermission({ name: "unknown_tool", arguments: {} }, permissions).permission).toBe("ask");
|
||||
});
|
||||
|
||||
it("should respect CLI overrides even in headless mode", () => {
|
||||
const policies = [
|
||||
{ tool: "dangerous_tool", permission: "exclude" as const },
|
||||
{ tool: "*", permission: "allow" as const },
|
||||
...DEFAULT_TOOL_POLICIES,
|
||||
];
|
||||
const permissions = { policies };
|
||||
|
||||
|
|
@ -87,24 +70,21 @@ describe("Headless Permissions Integration", () => {
|
|||
);
|
||||
expect(excludedResult.permission).toBe("exclude");
|
||||
|
||||
// Other tools should be allowed
|
||||
const allowedResult = checkToolPermission(
|
||||
{ name: "safe_tool", arguments: {} },
|
||||
permissions
|
||||
);
|
||||
expect(allowedResult.permission).toBe("allow");
|
||||
// Default tools should follow their normal rules
|
||||
expect(checkToolPermission({ name: "read_file", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "write_file", arguments: {} }, permissions).permission).toBe("ask");
|
||||
});
|
||||
});
|
||||
|
||||
describe("real-world headless scenarios", () => {
|
||||
it("should handle typical CLI workflow in headless mode", () => {
|
||||
it("should handle typical CLI workflow with permission overrides", () => {
|
||||
// Simulate headless mode with some CLI overrides
|
||||
const policies = resolvePermissionPrecedence({
|
||||
commandLineFlags: {
|
||||
// Still exclude truly dangerous operations
|
||||
exclude: ["rm", "sudo"],
|
||||
// Allow specific tools that might normally require confirmation
|
||||
allow: ["git", "npm"],
|
||||
// Allow specific tools that would normally require confirmation
|
||||
allow: ["write_file", "run_terminal_command"],
|
||||
},
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
|
|
@ -118,11 +98,31 @@ describe("Headless Permissions Integration", () => {
|
|||
expect(checkToolPermission({ name: "sudo", arguments: {} }, permissions).permission).toBe("exclude");
|
||||
|
||||
// Explicitly allowed tools should be allowed
|
||||
expect(checkToolPermission({ name: "git", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "npm", arguments: {} }, permissions).permission).toBe("allow");
|
||||
|
||||
// Other tools should fall back to headless wildcard (allow)
|
||||
expect(checkToolPermission({ name: "write_file", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "run_terminal_command", arguments: {} }, permissions).permission).toBe("allow");
|
||||
|
||||
// Read-only tools should still be allowed by default
|
||||
expect(checkToolPermission({ name: "read_file", arguments: {} }, permissions).permission).toBe("allow");
|
||||
|
||||
// Unknown tools should default to ask (since no wildcard allow override was provided)
|
||||
expect(checkToolPermission({ name: "unknown_tool", arguments: {} }, permissions).permission).toBe("ask");
|
||||
});
|
||||
|
||||
it("should handle workflow with wildcard allow override", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
commandLineFlags: {
|
||||
allow: ["*"], // Allow all tools
|
||||
},
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
headless: true,
|
||||
});
|
||||
|
||||
const permissions = { policies };
|
||||
|
||||
// All tools should be allowed due to wildcard override
|
||||
expect(checkToolPermission({ name: "write_file", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "run_terminal_command", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "unknown_tool", arguments: {} }, permissions).permission).toBe("allow");
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ export type {
|
|||
ToolPermissions,
|
||||
} from "./types.js";
|
||||
|
||||
export { DEFAULT_TOOL_POLICIES, HEADLESS_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
export { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
export {
|
||||
checkToolPermission,
|
||||
filterExcludedTools,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { jest } from "@jest/globals";
|
||||
import { DEFAULT_TOOL_POLICIES, HEADLESS_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
import { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
import { resolvePermissionPrecedence } from "./precedenceResolver.js";
|
||||
import { ToolPermissionPolicy } from "./types.js";
|
||||
|
||||
|
|
@ -142,27 +142,25 @@ describe("precedenceResolver", () => {
|
|||
});
|
||||
|
||||
describe("headless mode", () => {
|
||||
it("should use headless policies when headless: true", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
it("should use same policies for headless and normal modes", () => {
|
||||
const headlessPolicies = resolvePermissionPrecedence({
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
headless: true,
|
||||
});
|
||||
|
||||
expect(policies).toEqual(HEADLESS_TOOL_POLICIES);
|
||||
});
|
||||
|
||||
it("should use normal policies when headless: false", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
const normalPolicies = resolvePermissionPrecedence({
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
headless: false,
|
||||
});
|
||||
|
||||
expect(policies).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
expect(headlessPolicies).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
expect(normalPolicies).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
expect(headlessPolicies).toEqual(normalPolicies);
|
||||
});
|
||||
|
||||
it("should use normal policies when headless is undefined", () => {
|
||||
it("should use default policies when headless is undefined", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
|
|
@ -171,7 +169,7 @@ describe("precedenceResolver", () => {
|
|||
expect(policies).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
});
|
||||
|
||||
it("should allow CLI flags to override headless policies", () => {
|
||||
it("should allow CLI flags to override default policies in headless mode", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
commandLineFlags: {
|
||||
exclude: ["write_file"],
|
||||
|
|
@ -181,15 +179,15 @@ describe("precedenceResolver", () => {
|
|||
headless: true,
|
||||
});
|
||||
|
||||
// CLI exclusion should come first, overriding headless allow-all
|
||||
// CLI exclusion should come first
|
||||
expect(policies[0]).toEqual({ tool: "write_file", permission: "exclude" });
|
||||
// Headless wildcard should still be present
|
||||
expect(policies[1]).toEqual({ tool: "*", permission: "allow" });
|
||||
// Default policies should follow
|
||||
expect(policies.slice(1)).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
});
|
||||
|
||||
it("should allow config policies to override headless defaults", () => {
|
||||
it("should allow config policies to override defaults in headless mode", () => {
|
||||
const configPolicies: ToolPermissionPolicy[] = [
|
||||
{ tool: "run_terminal_command", permission: "ask" },
|
||||
{ tool: "run_terminal_command", permission: "allow" },
|
||||
];
|
||||
|
||||
const policies = resolvePermissionPrecedence({
|
||||
|
|
@ -199,10 +197,10 @@ describe("precedenceResolver", () => {
|
|||
headless: true,
|
||||
});
|
||||
|
||||
// Config policy should override headless default
|
||||
expect(policies[0]).toEqual({ tool: "run_terminal_command", permission: "ask" });
|
||||
// Headless wildcard should still be present
|
||||
expect(policies[1]).toEqual({ tool: "*", permission: "allow" });
|
||||
// Config policy should override default
|
||||
expect(policies[0]).toEqual({ tool: "run_terminal_command", permission: "allow" });
|
||||
// Default policies should follow
|
||||
expect(policies.slice(1)).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { DEFAULT_TOOL_POLICIES, HEADLESS_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
import { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
import { loadPermissionsYaml, yamlConfigToPolicies } from "./permissionsYamlLoader.js";
|
||||
import { normalizeToolName } from "./toolNameMapping.js";
|
||||
import { ToolPermissionPolicy } from "./types.js";
|
||||
|
|
@ -40,10 +40,9 @@ export function resolvePermissionPrecedence(sources: PermissionSources): ToolPer
|
|||
|
||||
// Layer 4: Default policies (lowest precedence)
|
||||
if (sources.useDefaults !== false) {
|
||||
const defaultPolicies = sources.headless ? HEADLESS_TOOL_POLICIES : DEFAULT_TOOL_POLICIES;
|
||||
layers.push({
|
||||
name: sources.headless ? "headless-defaults" : "defaults",
|
||||
policies: [...defaultPolicies]
|
||||
name: "defaults",
|
||||
policies: [...DEFAULT_TOOL_POLICIES]
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -239,7 +239,7 @@ async function processStreamingResponse(
|
|||
hasToolCalls = true;
|
||||
for (const toolCallDelta of choice.delta.tool_calls) {
|
||||
let toolCallId: string | undefined;
|
||||
|
||||
|
||||
// If we have an ID, use it and map the index
|
||||
if (toolCallDelta.id) {
|
||||
toolCallId = toolCallDelta.id;
|
||||
|
|
@ -250,12 +250,14 @@ async function processStreamingResponse(
|
|||
// No ID, but we have an index - look up the ID from our map
|
||||
toolCallId = indexToIdMap.get(toolCallDelta.index);
|
||||
}
|
||||
|
||||
|
||||
if (!toolCallId) {
|
||||
logger.warn("Tool call delta without ID or valid index mapping", { toolCallDelta });
|
||||
logger.warn("Tool call delta without ID or valid index mapping", {
|
||||
toolCallDelta,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Create tool call entry if it doesn't exist
|
||||
if (!toolCallsMap.has(toolCallId)) {
|
||||
toolCallsMap.set(toolCallId, {
|
||||
|
|
@ -478,7 +480,6 @@ export async function streamChatResponse(
|
|||
arguments: toolCall.arguments,
|
||||
});
|
||||
|
||||
|
||||
// Notify tool start immediately - before permission check
|
||||
// This ensures the UI shows the tool call even if it's rejected
|
||||
if (callbacks?.onToolStart) {
|
||||
|
|
@ -490,6 +491,23 @@ export async function streamChatResponse(
|
|||
if (permissionCheck.permission === "allow") {
|
||||
approved = true;
|
||||
} else if (permissionCheck.permission === "ask") {
|
||||
// In headless mode, exit immediately with instructions
|
||||
const tool = BUILTIN_TOOLS.find((t) => t.name === toolCall.name);
|
||||
const toolName = tool?.displayName || toolCall.name;
|
||||
if (isHeadless) {
|
||||
console.error(
|
||||
`Error: Tool '${toolName}' requires permission but cn is running in headless mode.`
|
||||
);
|
||||
console.error(
|
||||
`If you want to allow this tool, use --allow ${toolName}.`
|
||||
);
|
||||
console.error(
|
||||
`If you don't want the tool to be included, use --exclude ${toolName}.`
|
||||
);
|
||||
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Request permission from user
|
||||
if (callbacks?.onToolPermissionRequest) {
|
||||
// Use the proper toolPermissionManager API
|
||||
|
|
@ -497,7 +515,7 @@ export async function streamChatResponse(
|
|||
name: toolCall.name,
|
||||
arguments: toolCall.arguments,
|
||||
};
|
||||
|
||||
|
||||
// Set up listener for permissionRequested event
|
||||
const handlePermissionRequested = (event: {
|
||||
requestId: string;
|
||||
|
|
@ -516,17 +534,16 @@ export async function streamChatResponse(
|
|||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
toolPermissionManager.on(
|
||||
"permissionRequested",
|
||||
handlePermissionRequested
|
||||
);
|
||||
|
||||
|
||||
// Request permission using the proper API
|
||||
const permissionResult = await toolPermissionManager.requestPermission(
|
||||
toolCallRequest
|
||||
);
|
||||
|
||||
const permissionResult =
|
||||
await toolPermissionManager.requestPermission(toolCallRequest);
|
||||
|
||||
approved = permissionResult.approved;
|
||||
} else {
|
||||
// Fallback: deny if no UI callback available
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue