Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/main/codex/codex-structured-app-server-args.test.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
16 changes: 15 additions & 1 deletion src/main/codex/codex-structured-app-server-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down