Unverified Commit 543c5ec3 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

[ai-upgrade] Ask user to choose permission (#99291)

Stacked on #99235.

### Why?

Let users choose whether a new upgrade agent uses Auto permissions.

### How?

Ask Yes/No after model and effort. No sets explicit approvals; CLIs
without Auto use a supported approval mode. Existing agents keep their
permissions.

<!-- NEXT_JS_LLM -->

Closes NAR-947
parent 38c16c82
...@@ -72,6 +72,13 @@ function getCodexModels(path: string) { ...@@ -72,6 +72,13 @@ function getCodexModels(path: string) {
} }
} }
const CODEX_APPROVAL_ARGS = [
'--sandbox',
'workspace-write',
'--ask-for-approval',
'on-request',
] as const
type UpgradeHarness = { type UpgradeHarness = {
name: keyof typeof UPGRADE_MODELS name: keyof typeof UPGRADE_MODELS
path: string path: string
...@@ -130,6 +137,38 @@ function getHarnessDisplayName(name: UpgradeHarness['name']): string { ...@@ -130,6 +137,38 @@ function getHarnessDisplayName(name: UpgradeHarness['name']): string {
return name === 'codex' ? 'Codex' : 'Claude Code' return name === 'codex' ? 'Codex' : 'Claude Code'
} }
function supportsCodexAutoReview(path: string): boolean {
const result = spawn.sync(path, ['--help'], {
encoding: 'utf8',
timeout: 5000,
})
return result.status === 0 && /--approve-for-me\b/.test(result.stdout ?? '')
}
function getClaudePermissionSupport(path: string) {
const result = spawn.sync(path, ['--help'], {
encoding: 'utf8',
timeout: 5000,
})
const permissionModeHelp =
result.status === 0
? result.stdout?.match(
/--permission-mode[^\n]*(?:\n[ \t]{8,}[^\n]*){0,4}/
)?.[0]
: null
const choices = permissionModeHelp?.match(/\(choices:\s*([^)]+)\)/)?.[1]
const supportedModes = new Set(choices?.match(/[A-Za-z]+/g) ?? [])
return {
auto: supportedModes.has('auto'),
approvalMode: supportedModes.has('manual')
? 'manual'
: supportedModes.has('default')
? 'default'
: null,
}
}
async function findHarnesses(): Promise<UpgradeHarness[]> { async function findHarnesses(): Promise<UpgradeHarness[]> {
const names: UpgradeHarness['name'][] = ['codex', 'claude'] const names: UpgradeHarness['name'][] = ['codex', 'claude']
const directories = (process.env.PATH ?? '').split(delimiter).filter(Boolean) const directories = (process.env.PATH ?? '').split(delimiter).filter(Boolean)
...@@ -269,7 +308,8 @@ function launchHarness( ...@@ -269,7 +308,8 @@ function launchHarness(
prompt: string, prompt: string,
directory: string, directory: string,
model: string | null, model: string | null,
effort: string effort: string,
permissionArgs: readonly string[]
): Promise<number> { ): Promise<number> {
// Windows shell shims cannot carry literal line breaks in an argument. // Windows shell shims cannot carry literal line breaks in an argument.
if (process.platform === 'win32' && /\.(cmd|bat)$/i.test(harness.path)) { if (process.platform === 'win32' && /\.(cmd|bat)$/i.test(harness.path)) {
...@@ -284,6 +324,7 @@ function launchHarness( ...@@ -284,6 +324,7 @@ function launchHarness(
args.push('--effort', effort) args.push('--effort', effort)
} }
} }
args.push(...permissionArgs)
args.push(prompt) args.push(prompt)
return runChildProcess(harness.path, args, { return runChildProcess(harness.path, args, {
cwd: directory, cwd: directory,
...@@ -315,7 +356,7 @@ export async function handoffUpgrade( ...@@ -315,7 +356,7 @@ export async function handoffUpgrade(
return return
} }
// Let the selected agent take over the terminal with its existing permissions. // Let the selected agent take over the terminal with the chosen permissions.
const harness = await chooseHarness(installed) const harness = await chooseHarness(installed)
if (harness === 'copy') { if (harness === 'copy') {
...@@ -374,6 +415,45 @@ export async function handoffUpgrade( ...@@ -374,6 +415,45 @@ export async function handoffUpgrade(
} }
effort = selectedEffort effort = selectedEffort
} }
let autoPermissionArgs: string[] | null
let approvalPermissionArgs: string[]
if (harness.name === 'codex') {
autoPermissionArgs = supportsCodexAutoReview(harness.path)
? ['--approve-for-me']
: null
approvalPermissionArgs = [...CODEX_APPROVAL_ARGS]
} else {
const { auto, approvalMode } = getClaudePermissionSupport(harness.path)
if (!approvalMode) {
Log.error('Could not determine a supported Claude approval mode.')
process.exitCode = 1
return
}
autoPermissionArgs = auto ? ['--permission-mode', 'auto'] : null
approvalPermissionArgs = ['--permission-mode', approvalMode]
}
let permissionArgs = approvalPermissionArgs
if (autoPermissionArgs) {
const useAuto = await chooseOption(
`Use Auto permission mode for ${getHarnessDisplayName(harness.name)}?`,
{ yes: 'Yes', no: 'No, ask for approval' },
0
)
if (useAuto !== 'yes' && useAuto !== 'no') {
Log.bootstrap(` ${dim('Upgrade cancelled.')}\n`)
process.exitCode = 1
return
}
if (useAuto === 'yes') {
permissionArgs = autoPermissionArgs
}
} else {
Log.info(
dim('Auto permission mode is unavailable; using approval requests.')
)
}
let useWorktree: boolean let useWorktree: boolean
try { try {
useWorktree = await chooseWorktree() useWorktree = await chooseWorktree()
...@@ -394,7 +474,8 @@ export async function handoffUpgrade( ...@@ -394,7 +474,8 @@ export async function handoffUpgrade(
resolvePrompt(prompt, useWorktree), resolvePrompt(prompt, useWorktree),
directory, directory,
model?.id ?? null, model?.id ?? null,
effort effort,
permissionArgs
) )
} catch { } catch {
Log.error(`Could not start ${getHarnessDisplayName(harness.name)}.`) Log.error(`Could not start ${getHarnessDisplayName(harness.name)}.`)
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment