From bac423c117c0a01adb6d0ddae3ef4da91d2d9640 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 30 Jul 2025 09:28:24 -0700 Subject: [PATCH 1/3] Add headless mode permissions with full tool access by default --- spec/permissions.md | 19 +++ src/permissions/defaultPolicies.ts | 9 ++ .../headlessPermissions.integration.test.ts | 129 ++++++++++++++++++ src/permissions/index.ts | 2 +- src/permissions/precedenceResolver.test.ts | 67 ++++++++- src/permissions/precedenceResolver.ts | 9 +- src/services/ToolPermissionService.ts | 12 +- .../ToolPermissionService.unit.test.ts | 4 +- src/services/index.ts | 4 +- 9 files changed, 242 insertions(+), 13 deletions(-) create mode 100644 src/permissions/headlessPermissions.integration.test.ts diff --git a/spec/permissions.md b/spec/permissions.md index 380c7bfba..51b828359 100644 --- a/spec/permissions.md +++ b/spec/permissions.md @@ -82,3 +82,22 @@ 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 different default policies designed for non-interactive usage: + +- **Normal mode**: Write operations and terminal commands require confirmation (`ask`) +- **Headless mode**: All tools are allowed by default (`*: allow`) + +This behavior can still be overridden using the standard precedence rules. For example: + +```bash +# Headless mode with full access (default) +cn -p "Write a hello world script" + +# Headless mode with specific restrictions +cn -p --exclude Write "Clean up the codebase" +``` + +The headless default policies are defined in [`src/permissions/defaultPolicies.ts`](../src/permissions/defaultPolicies.ts) as `HEADLESS_TOOL_POLICIES`. diff --git a/src/permissions/defaultPolicies.ts b/src/permissions/defaultPolicies.ts index d08c5f375..4af884496 100644 --- a/src/permissions/defaultPolicies.ts +++ b/src/permissions/defaultPolicies.ts @@ -26,3 +26,12 @@ export const DEFAULT_TOOL_POLICIES: ToolPermissionPolicy[] = [ // Default fallback - ask for any unmatched tools { 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" }, +]; diff --git a/src/permissions/headlessPermissions.integration.test.ts b/src/permissions/headlessPermissions.integration.test.ts new file mode 100644 index 000000000..31ba6da71 --- /dev/null +++ b/src/permissions/headlessPermissions.integration.test.ts @@ -0,0 +1,129 @@ +import { resolvePermissionPrecedence } from "./precedenceResolver.js"; +import { HEADLESS_TOOL_POLICIES, 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({ + 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({ + 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"); + }); + + it("should allow CLI overrides to supersede headless defaults", () => { + const policies = resolvePermissionPrecedence({ + commandLineFlags: { + exclude: ["dangerous_tool"], + }, + useDefaults: true, + personalSettings: false, + headless: true, + }); + + // 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" }); + }); + }); + + describe("permission checking with headless policies", () => { + it("should allow all tools with headless policies", () => { + const permissions = { policies: HEADLESS_TOOL_POLICIES }; + + // Test various tool types + const toolsToTest = [ + "write_file", + "run_terminal_command", + "read_file", + "search_code", + "some_unknown_tool", + "mcp__custom_tool" + ]; + + for (const toolName of toolsToTest) { + const result = checkToolPermission( + { name: toolName, arguments: {} }, + permissions + ); + + expect(result.permission).toBe("allow"); + expect(result.matchedPolicy).toEqual({ tool: "*", permission: "allow" }); + } + }); + + it("should respect CLI overrides even in headless mode", () => { + const policies = [ + { tool: "dangerous_tool", permission: "exclude" as const }, + { tool: "*", permission: "allow" as const }, + ]; + const permissions = { policies }; + + // Excluded tool should be blocked + const excludedResult = checkToolPermission( + { name: "dangerous_tool", arguments: {} }, + permissions + ); + expect(excludedResult.permission).toBe("exclude"); + + // Other tools should be allowed + const allowedResult = checkToolPermission( + { name: "safe_tool", arguments: {} }, + permissions + ); + expect(allowedResult.permission).toBe("allow"); + }); + }); + + describe("real-world headless scenarios", () => { + it("should handle typical CLI workflow in headless mode", () => { + // 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"], + }, + useDefaults: true, + personalSettings: false, + headless: true, + }); + + 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: "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: "unknown_tool", arguments: {} }, permissions).permission).toBe("allow"); + }); + }); +}); \ No newline at end of file diff --git a/src/permissions/index.ts b/src/permissions/index.ts index 7e5fca074..9327dafb9 100644 --- a/src/permissions/index.ts +++ b/src/permissions/index.ts @@ -6,7 +6,7 @@ export type { ToolPermissions, } from "./types.js"; -export { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js"; +export { DEFAULT_TOOL_POLICIES, HEADLESS_TOOL_POLICIES } from "./defaultPolicies.js"; export { checkToolPermission, filterExcludedTools, diff --git a/src/permissions/precedenceResolver.test.ts b/src/permissions/precedenceResolver.test.ts index 7e69810fe..7c4bb9dcf 100644 --- a/src/permissions/precedenceResolver.test.ts +++ b/src/permissions/precedenceResolver.test.ts @@ -1,5 +1,5 @@ import { jest } from "@jest/globals"; -import { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js"; +import { DEFAULT_TOOL_POLICIES, HEADLESS_TOOL_POLICIES } from "./defaultPolicies.js"; import { resolvePermissionPrecedence } from "./precedenceResolver.js"; import { ToolPermissionPolicy } from "./types.js"; @@ -140,5 +140,70 @@ describe("precedenceResolver", () => { const readPolicy = policies.find(p => p.tool === "read_file"); expect(readPolicy).toBeDefined(); }); + + describe("headless mode", () => { + it("should use headless policies when headless: true", () => { + const policies = resolvePermissionPrecedence({ + useDefaults: true, + personalSettings: false, + headless: true, + }); + + expect(policies).toEqual(HEADLESS_TOOL_POLICIES); + }); + + it("should use normal policies when headless: false", () => { + const policies = resolvePermissionPrecedence({ + useDefaults: true, + personalSettings: false, + headless: false, + }); + + expect(policies).toEqual(DEFAULT_TOOL_POLICIES); + }); + + it("should use normal policies when headless is undefined", () => { + const policies = resolvePermissionPrecedence({ + useDefaults: true, + personalSettings: false, + }); + + expect(policies).toEqual(DEFAULT_TOOL_POLICIES); + }); + + it("should allow CLI flags to override headless policies", () => { + const policies = resolvePermissionPrecedence({ + commandLineFlags: { + exclude: ["write_file"], + }, + useDefaults: true, + personalSettings: false, + headless: true, + }); + + // CLI exclusion should come first, overriding headless allow-all + expect(policies[0]).toEqual({ tool: "write_file", permission: "exclude" }); + // Headless wildcard should still be present + expect(policies[1]).toEqual({ tool: "*", permission: "allow" }); + }); + + it("should allow config policies to override headless defaults", () => { + const configPolicies: ToolPermissionPolicy[] = [ + { tool: "run_terminal_command", permission: "ask" }, + ]; + + const policies = resolvePermissionPrecedence({ + configPermissions: configPolicies, + useDefaults: true, + personalSettings: false, + 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" }); + }); + }); }); }); \ No newline at end of file diff --git a/src/permissions/precedenceResolver.ts b/src/permissions/precedenceResolver.ts index b3a6da6e9..b7a4dc967 100644 --- a/src/permissions/precedenceResolver.ts +++ b/src/permissions/precedenceResolver.ts @@ -1,4 +1,4 @@ -import { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js"; +import { DEFAULT_TOOL_POLICIES, HEADLESS_TOOL_POLICIES } from "./defaultPolicies.js"; import { loadPermissionsYaml, yamlConfigToPolicies } from "./permissionsYamlLoader.js"; import { normalizeToolName } from "./toolNameMapping.js"; import { ToolPermissionPolicy } from "./types.js"; @@ -17,6 +17,8 @@ export interface PermissionSources { personalSettings?: boolean; // Whether to load from permissions.yaml /** Default policies - lowest precedence */ useDefaults?: boolean; + /** Whether running in headless mode - affects default policies */ + headless?: boolean; } /** @@ -38,9 +40,10 @@ 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: "defaults", - policies: [...DEFAULT_TOOL_POLICIES] + name: sources.headless ? "headless-defaults" : "defaults", + policies: [...defaultPolicies] }); } diff --git a/src/services/ToolPermissionService.ts b/src/services/ToolPermissionService.ts index b599fcc60..a80d1b03c 100644 --- a/src/services/ToolPermissionService.ts +++ b/src/services/ToolPermissionService.ts @@ -23,16 +23,18 @@ export class ToolPermissionService { allow?: string[]; ask?: string[]; exclude?: string[]; - }): ToolPermissionServiceState { + }, headless?: boolean): ToolPermissionServiceState { logger.debug("Synchronously initializing ToolPermissionService", { - hasOverrides: !!runtimeOverrides + hasOverrides: !!runtimeOverrides, + headless: !!headless }); // Use the precedence resolver to get properly ordered policies const compiledPolicies = resolvePermissionPrecedence({ commandLineFlags: runtimeOverrides, personalSettings: true, // Enable loading from ~/.continue/permissions.yaml - useDefaults: true + useDefaults: true, + headless: headless }); this.state = { @@ -49,12 +51,12 @@ export class ToolPermissionService { allow?: string[]; ask?: string[]; exclude?: string[]; - }): Promise { + }, headless?: boolean): Promise { // Ensure permissions.yaml exists before loading await ensurePermissionsYamlExists(); // Use the synchronous version after ensuring the file exists - return this.initializeSync(runtimeOverrides); + return this.initializeSync(runtimeOverrides, headless); } /** diff --git a/src/services/ToolPermissionService.unit.test.ts b/src/services/ToolPermissionService.unit.test.ts index 71793ca81..dbb48b61b 100644 --- a/src/services/ToolPermissionService.unit.test.ts +++ b/src/services/ToolPermissionService.unit.test.ts @@ -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 }); diff --git a/src/services/index.ts b/src/services/index.ts index f9b03ed0d..210c56dee 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -31,13 +31,13 @@ export async function initializeServices(options: ServiceInitOptions = {}) { // Handle tool permissions specially for immediate availability if (options.toolPermissionOverrides) { // Initialize synchronously and register the value immediately - const permissionState = toolPermissionService.initializeSync(options.toolPermissionOverrides); + const permissionState = toolPermissionService.initializeSync(options.toolPermissionOverrides, options.headless); serviceContainer.registerValue(SERVICE_NAMES.TOOL_PERMISSIONS, permissionState); } else { // Register normal factory for lazy initialization serviceContainer.register( SERVICE_NAMES.TOOL_PERMISSIONS, - () => toolPermissionService.initialize(), + () => toolPermissionService.initialize(undefined, options.headless), [] ); } From 328ff12e65e6a6022528b4091b951d6cfc3fdae8 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 30 Jul 2025 22:34:20 -0700 Subject: [PATCH 2/3] headless tool call permissions error msg --- spec/permissions.md | 19 +-- src/permissions/defaultPolicies.ts | 8 -- .../headlessPermissions.integration.test.ts | 108 +++++++++--------- src/permissions/index.ts | 2 +- src/permissions/precedenceResolver.test.ts | 38 +++--- src/permissions/precedenceResolver.ts | 7 +- src/streamChatResponse.ts | 41 +++++-- 7 files changed, 116 insertions(+), 107 deletions(-) diff --git a/spec/permissions.md b/spec/permissions.md index 51b828359..0e47c056c 100644 --- a/spec/permissions.md +++ b/spec/permissions.md @@ -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. diff --git a/src/permissions/defaultPolicies.ts b/src/permissions/defaultPolicies.ts index 4af884496..6b5693e5b 100644 --- a/src/permissions/defaultPolicies.ts +++ b/src/permissions/defaultPolicies.ts @@ -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" }, -]; diff --git a/src/permissions/headlessPermissions.integration.test.ts b/src/permissions/headlessPermissions.integration.test.ts index 31ba6da71..7ca1e7dbf 100644 --- a/src/permissions/headlessPermissions.integration.test.ts +++ b/src/permissions/headlessPermissions.integration.test.ts @@ -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"); }); }); diff --git a/src/permissions/index.ts b/src/permissions/index.ts index 9327dafb9..7e5fca074 100644 --- a/src/permissions/index.ts +++ b/src/permissions/index.ts @@ -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, diff --git a/src/permissions/precedenceResolver.test.ts b/src/permissions/precedenceResolver.test.ts index 7c4bb9dcf..de0ab10f6 100644 --- a/src/permissions/precedenceResolver.test.ts +++ b/src/permissions/precedenceResolver.test.ts @@ -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); }); }); }); diff --git a/src/permissions/precedenceResolver.ts b/src/permissions/precedenceResolver.ts index b7a4dc967..22ad131c2 100644 --- a/src/permissions/precedenceResolver.ts +++ b/src/permissions/precedenceResolver.ts @@ -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] }); } diff --git a/src/streamChatResponse.ts b/src/streamChatResponse.ts index 242e30d83..4b379241a 100644 --- a/src/streamChatResponse.ts +++ b/src/streamChatResponse.ts @@ -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 From e614792d31cc06cdc98c53a88774137cbb924d70 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 30 Jul 2025 23:02:51 -0700 Subject: [PATCH 3/3] fix feedback --- .../headlessPermissions.integration.test.ts | 24 ++++------------ src/permissions/precedenceResolver.test.ts | 28 +++---------------- src/permissions/precedenceResolver.ts | 2 -- src/services/ToolPermissionService.ts | 12 ++++---- src/services/index.ts | 4 +-- 5 files changed, 17 insertions(+), 53 deletions(-) diff --git a/src/permissions/headlessPermissions.integration.test.ts b/src/permissions/headlessPermissions.integration.test.ts index 7ca1e7dbf..791c6cb25 100644 --- a/src/permissions/headlessPermissions.integration.test.ts +++ b/src/permissions/headlessPermissions.integration.test.ts @@ -3,33 +3,23 @@ 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 same default policies for both headless and normal modes", () => { - const headlessPolicies = resolvePermissionPrecedence({ + describe("precedence resolution", () => { + it("should use default policies", () => { + const policies = resolvePermissionPrecedence({ useDefaults: true, personalSettings: false, // Disable to avoid file system dependencies - headless: true, }); - const normalPolicies = resolvePermissionPrecedence({ - useDefaults: true, - personalSettings: false, - headless: false, - }); - - expect(headlessPolicies).toEqual(DEFAULT_TOOL_POLICIES); - expect(normalPolicies).toEqual(DEFAULT_TOOL_POLICIES); - expect(headlessPolicies).toEqual(normalPolicies); + expect(policies).toEqual(DEFAULT_TOOL_POLICIES); }); - it("should allow CLI overrides to supersede defaults in headless mode", () => { + it("should allow CLI overrides to supersede defaults", () => { const policies = resolvePermissionPrecedence({ commandLineFlags: { exclude: ["dangerous_tool"], }, useDefaults: true, personalSettings: false, - headless: true, }); // CLI exclusion should come first @@ -78,7 +68,7 @@ describe("Headless Permissions Integration", () => { describe("real-world headless scenarios", () => { it("should handle typical CLI workflow with permission overrides", () => { - // Simulate headless mode with some CLI overrides + // Simulate CLI with some overrides const policies = resolvePermissionPrecedence({ commandLineFlags: { // Still exclude truly dangerous operations @@ -88,7 +78,6 @@ describe("Headless Permissions Integration", () => { }, useDefaults: true, personalSettings: false, - headless: true, }); const permissions = { policies }; @@ -115,7 +104,6 @@ describe("Headless Permissions Integration", () => { }, useDefaults: true, personalSettings: false, - headless: true, }); const permissions = { policies }; diff --git a/src/permissions/precedenceResolver.test.ts b/src/permissions/precedenceResolver.test.ts index de0ab10f6..b16317587 100644 --- a/src/permissions/precedenceResolver.test.ts +++ b/src/permissions/precedenceResolver.test.ts @@ -141,26 +141,8 @@ describe("precedenceResolver", () => { expect(readPolicy).toBeDefined(); }); - describe("headless mode", () => { - it("should use same policies for headless and normal modes", () => { - const headlessPolicies = resolvePermissionPrecedence({ - useDefaults: true, - personalSettings: false, - headless: true, - }); - - const normalPolicies = resolvePermissionPrecedence({ - useDefaults: true, - personalSettings: false, - headless: false, - }); - - expect(headlessPolicies).toEqual(DEFAULT_TOOL_POLICIES); - expect(normalPolicies).toEqual(DEFAULT_TOOL_POLICIES); - expect(headlessPolicies).toEqual(normalPolicies); - }); - - it("should use default policies when headless is undefined", () => { + describe("precedence rules", () => { + it("should use default policies when no other sources provided", () => { const policies = resolvePermissionPrecedence({ useDefaults: true, personalSettings: false, @@ -169,14 +151,13 @@ describe("precedenceResolver", () => { expect(policies).toEqual(DEFAULT_TOOL_POLICIES); }); - it("should allow CLI flags to override default policies in headless mode", () => { + it("should allow CLI flags to override default policies", () => { const policies = resolvePermissionPrecedence({ commandLineFlags: { exclude: ["write_file"], }, useDefaults: true, personalSettings: false, - headless: true, }); // CLI exclusion should come first @@ -185,7 +166,7 @@ describe("precedenceResolver", () => { expect(policies.slice(1)).toEqual(DEFAULT_TOOL_POLICIES); }); - it("should allow config policies to override defaults in headless mode", () => { + it("should allow config policies to override defaults", () => { const configPolicies: ToolPermissionPolicy[] = [ { tool: "run_terminal_command", permission: "allow" }, ]; @@ -194,7 +175,6 @@ describe("precedenceResolver", () => { configPermissions: configPolicies, useDefaults: true, personalSettings: false, - headless: true, }); // Config policy should override default diff --git a/src/permissions/precedenceResolver.ts b/src/permissions/precedenceResolver.ts index 22ad131c2..b3a6da6e9 100644 --- a/src/permissions/precedenceResolver.ts +++ b/src/permissions/precedenceResolver.ts @@ -17,8 +17,6 @@ export interface PermissionSources { personalSettings?: boolean; // Whether to load from permissions.yaml /** Default policies - lowest precedence */ useDefaults?: boolean; - /** Whether running in headless mode - affects default policies */ - headless?: boolean; } /** diff --git a/src/services/ToolPermissionService.ts b/src/services/ToolPermissionService.ts index a80d1b03c..b599fcc60 100644 --- a/src/services/ToolPermissionService.ts +++ b/src/services/ToolPermissionService.ts @@ -23,18 +23,16 @@ export class ToolPermissionService { allow?: string[]; ask?: string[]; exclude?: string[]; - }, headless?: boolean): ToolPermissionServiceState { + }): ToolPermissionServiceState { logger.debug("Synchronously initializing ToolPermissionService", { - hasOverrides: !!runtimeOverrides, - headless: !!headless + hasOverrides: !!runtimeOverrides }); // Use the precedence resolver to get properly ordered policies const compiledPolicies = resolvePermissionPrecedence({ commandLineFlags: runtimeOverrides, personalSettings: true, // Enable loading from ~/.continue/permissions.yaml - useDefaults: true, - headless: headless + useDefaults: true }); this.state = { @@ -51,12 +49,12 @@ export class ToolPermissionService { allow?: string[]; ask?: string[]; exclude?: string[]; - }, headless?: boolean): Promise { + }): Promise { // Ensure permissions.yaml exists before loading await ensurePermissionsYamlExists(); // Use the synchronous version after ensuring the file exists - return this.initializeSync(runtimeOverrides, headless); + return this.initializeSync(runtimeOverrides); } /** diff --git a/src/services/index.ts b/src/services/index.ts index 210c56dee..f9b03ed0d 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -31,13 +31,13 @@ export async function initializeServices(options: ServiceInitOptions = {}) { // Handle tool permissions specially for immediate availability if (options.toolPermissionOverrides) { // Initialize synchronously and register the value immediately - const permissionState = toolPermissionService.initializeSync(options.toolPermissionOverrides, options.headless); + const permissionState = toolPermissionService.initializeSync(options.toolPermissionOverrides); serviceContainer.registerValue(SERVICE_NAMES.TOOL_PERMISSIONS, permissionState); } else { // Register normal factory for lazy initialization serviceContainer.register( SERVICE_NAMES.TOOL_PERMISSIONS, - () => toolPermissionService.initialize(undefined, options.headless), + () => toolPermissionService.initialize(), [] ); }