diff --git a/src/main/codex/codex-structured-app-server-args.test.ts b/src/main/codex/codex-structured-app-server-args.test.ts index f76305cf7c1e..61eb84cc969e 100644 --- a/src/main/codex/codex-structured-app-server-args.test.ts +++ b/src/main/codex/codex-structured-app-server-args.test.ts @@ -1,7 +1,51 @@ import { describe, expect, it } from 'vitest' import { resolveCodexStructuredAppServerArgs } from './codex-structured-app-server-args' +import { YOLO_TUI_AGENT_ARGS } from '../../shared/tui-agent-permissions' +import type { AgentStartupShell } from '../../shared/tui-agent-startup-shell' + +/** Asserts the configured CLI permission flags become explicit app-server config assignments. */ +function expectPermissionConfig( + configured: string, + shell: AgentStartupShell, + approvalPolicy: string, + sandboxMode: string +): void { + expect(resolveCodexStructuredAppServerArgs(configured, shell)).toEqual([ + '-c', + `approval_policy="${approvalPolicy}"`, + '-c', + `sandbox_mode="${sandboxMode}"` + ]) +} + +/** The YOLO default must reach app-server as config, since it ignores the TUI bypass flag. */ +function expectYoloDefaultTranslated(shell: AgentStartupShell): void { + expectPermissionConfig(YOLO_TUI_AGENT_ARGS.codex!, shell, 'never', 'danger-full-access') +} + +/** Restricted flags must keep their configured values in either short or long spelling. */ +function expectRestrictedPermissionsPreserved(configured: string): void { + expectPermissionConfig(configured, 'posix', 'on-request', 'workspace-write') +} + +/** An empty configuration must not grant bypass permissions implicitly. */ +function expectNoBypassWhenDisabled(): void { + expect(resolveCodexStructuredAppServerArgs('', 'posix')).toEqual([]) +} describe('structured Codex app-server arguments', () => { + it.each(['posix', 'powershell', 'cmd'] as const)( + 'translates the configured YOLO default into app-server permissions on %s', + expectYoloDefaultTranslated + ) + + it.each([ + '-a on-request -s workspace-write', + '--ask-for-approval=on-request --sandbox=workspace-write' + ])('preserves explicit restricted permissions: %s', expectRestrictedPermissionsPreserved) + + it('does not grant bypass permissions when YOLO is disabled', expectNoBypassWhenDisabled) + it('keeps configuration flags and converts effort to the app-server config contract', () => { expect( resolveCodexStructuredAppServerArgs( diff --git a/src/main/codex/codex-structured-app-server-args.ts b/src/main/codex/codex-structured-app-server-args.ts index af83c46c8a2c..43025aa742d7 100644 --- a/src/main/codex/codex-structured-app-server-args.ts +++ b/src/main/codex/codex-structured-app-server-args.ts @@ -32,6 +32,12 @@ const BOOLEAN_FLAGS = new Set([ ]) const EFFORT_FLAGS = new Set(['--effort', '--reasoning-effort']) +const PERMISSION_CONFIG_KEYS = new Map([ + ['-a', 'approval_policy'], + ['--ask-for-approval', 'approval_policy'], + ['-s', 'sandbox_mode'], + ['--sandbox', 'sandbox_mode'] +]) function configuredArgsError(detail: string): Error { return new Error( @@ -63,6 +69,11 @@ export function resolveCodexStructuredAppServerArgs( for (let index = 0; index < parsed.tokens.length; index += 1) { const token = parsed.tokens[index] const { flag, inlineValue } = splitOption(token) + // Codex accepts the TUI bypass flag before app-server but does not apply it. + if (flag === '--dangerously-bypass-approvals-and-sandbox' && inlineValue === undefined) { + result.push('-c', 'approval_policy="never"', '-c', 'sandbox_mode="danger-full-access"') + continue + } if (BOOLEAN_FLAGS.has(flag) && inlineValue === undefined) { result.push(flag) continue @@ -74,7 +85,10 @@ export function resolveCodexStructuredAppServerArgs( if (value === undefined || value.length === 0) { throw configuredArgsError(`${flag} requires a value`) } - if (EFFORT_FLAGS.has(flag)) { + const permissionKey = PERMISSION_CONFIG_KEYS.get(flag) + if (permissionKey) { + result.push('-c', `${permissionKey}=${JSON.stringify(value)}`) + } else if (EFFORT_FLAGS.has(flag)) { result.push('-c', `model_reasoning_effort=${value}`) } else { result.push(flag, value)