Merge pull request #119 from continuedev/full-access-in-headless-mode

Add headless mode permissions with full tool access by default
This commit is contained in:
Nate Sesti 2025-07-31 08:03:16 -07:00 • committed by GitHub
commit edbe02e3b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 215 additions and 13 deletions

View file

@ -82,3 +82,25 @@ exclude:
Except that it's important to understand that this file is _not_ intended to be edited by the user. It is only for persistence, and users should interact with their permissions by using the TUI.
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 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`) - user is prompted
- **Headless mode**: Same default policies, but tools requiring confirmation (`ask`) will cause the process to exit with an error message
To use tools that normally require confirmation in headless mode, you must explicitly allow them:
```bash
# 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 run_terminal_command "Clean up the codebase"
```
This approach ensures that headless mode is secure by default while providing clear guidance on how to enable the needed permissions.

View file

@ -26,3 +26,4 @@ export const DEFAULT_TOOL_POLICIES: ToolPermissionPolicy[] = [
// Default fallback - ask for any unmatched tools
{ tool: "*", permission: "ask" },
];

View file

@ -0,0 +1,117 @@
import { resolvePermissionPrecedence } from "./precedenceResolver.js";
import { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js";
import { checkToolPermission } from "./permissionChecker.js";
describe("Headless Permissions Integration", () => {
describe("precedence resolution", () => {
it("should use default policies", () => {
const policies = resolvePermissionPrecedence({
useDefaults: true,
personalSettings: false, // Disable to avoid file system dependencies
});
expect(policies).toEqual(DEFAULT_TOOL_POLICIES);
});
it("should allow CLI overrides to supersede defaults", () => {
const policies = resolvePermissionPrecedence({
commandLineFlags: {
exclude: ["dangerous_tool"],
},
useDefaults: true,
personalSettings: false,
});
// CLI exclusion should come first
expect(policies[0]).toEqual({ tool: "dangerous_tool", permission: "exclude" });
// Default policies should follow
expect(policies.slice(1)).toEqual(DEFAULT_TOOL_POLICIES);
});
});
describe("permission checking with default policies", () => {
it("should follow default permission rules", () => {
const permissions = { policies: DEFAULT_TOOL_POLICIES };
// 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");
// 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 },
...DEFAULT_TOOL_POLICIES,
];
const permissions = { policies };
// Excluded tool should be blocked
const excludedResult = checkToolPermission(
{ name: "dangerous_tool", arguments: {} },
permissions
);
expect(excludedResult.permission).toBe("exclude");
// 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 with permission overrides", () => {
// Simulate CLI with some overrides
const policies = resolvePermissionPrecedence({
commandLineFlags: {
// Still exclude truly dangerous operations
exclude: ["rm", "sudo"],
// Allow specific tools that would normally require confirmation
allow: ["write_file", "run_terminal_command"],
},
useDefaults: true,
personalSettings: false,
});
const permissions = { policies };
// Excluded tools should be blocked
expect(checkToolPermission({ name: "rm", arguments: {} }, permissions).permission).toBe("exclude");
expect(checkToolPermission({ name: "sudo", arguments: {} }, permissions).permission).toBe("exclude");
// Explicitly allowed tools should be allowed
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,
});
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");
});
});
});

View file

@ -140,5 +140,48 @@ describe("precedenceResolver", () => {
const readPolicy = policies.find(p => p.tool === "read_file");
expect(readPolicy).toBeDefined();
});
describe("precedence rules", () => {
it("should use default policies when no other sources provided", () => {
const policies = resolvePermissionPrecedence({
useDefaults: true,
personalSettings: false,
});
expect(policies).toEqual(DEFAULT_TOOL_POLICIES);
});
it("should allow CLI flags to override default policies", () => {
const policies = resolvePermissionPrecedence({
commandLineFlags: {
exclude: ["write_file"],
},
useDefaults: true,
personalSettings: false,
});
// CLI exclusion should come first
expect(policies[0]).toEqual({ tool: "write_file", permission: "exclude" });
// Default policies should follow
expect(policies.slice(1)).toEqual(DEFAULT_TOOL_POLICIES);
});
it("should allow config policies to override defaults", () => {
const configPolicies: ToolPermissionPolicy[] = [
{ tool: "run_terminal_command", permission: "allow" },
];
const policies = resolvePermissionPrecedence({
configPermissions: configPolicies,
useDefaults: true,
personalSettings: false,
});
// 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);
});
});
});
});

View file

@ -23,7 +23,7 @@ describe("ToolPermissionService unit tests", () => {
expect(state.permissions.policies).toBeDefined();
expect(state.permissions.policies.length).toBeGreaterThan(0);
// Verify the overrides are in the policies
// Verify the overrides are in the policies (they should be at the beginning due to precedence)
const toolNames = state.permissions.policies.map((p) => p.tool);
expect(toolNames).toContain("TestTool3");
expect(toolNames).toContain("TestTool2");
@ -74,4 +74,6 @@ describe("ToolPermissionService unit tests", () => {
expect(state.permissions.policies.length).toBe(2);
});
});
// Headless mode functionality is tested in precedenceResolver.test.ts and integration tests
});

View file

@ -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