Files
hapi/cli/src/commands/opencode.ts
T
Junmo KimandGitHub e6eaff83c5 fix(hub,cli): forward permissionMode on session resume (#460)
* feat(hub,cli): forward permissionMode on session resume

When a session is resumed, the cached permissionMode is now forwarded
through the Hub → Runner → CLI pipeline via a new --permission-mode
flag. Previously the mode was lost on resume, resetting to 'default'.

Each CLI flavor validates the flag value against its own allowed
permission modes (e.g. CLAUDE_PERMISSION_MODES) and rejects unknown
values. The existing --yolo flag is preserved as a shorthand.

* refactor(cli): extract buildCliArgs from startRunner

Extract the CLI argument construction logic into a standalone
exported function so it can be unit-tested independently.
No behavior change.

* test(cli): add buildCliArgs unit tests for --permission-mode

Verify that the runner correctly forwards valid permission modes
via --permission-mode, rejects invalid values, and falls back to
--yolo when no permission mode is set.

* fix(cli): let --permission-mode take precedence over --yolo

When both flags are present, --permission-mode was silently
overwritten by a later --yolo. Guard legacy flag branches with
a hasExplicitPermissionMode check so the explicit flag wins.
2026-04-15 11:13:21 +08:00

67 lines
2.8 KiB
TypeScript

import chalk from 'chalk'
import { authAndSetupMachineIfNeeded } from '@/ui/auth'
import { initializeToken } from '@/ui/tokenInit'
import { maybeAutoStartServer } from '@/utils/autoStartServer'
import type { CommandDefinition } from './types'
import { OPENCODE_PERMISSION_MODES } from '@hapi/protocol/modes'
import type { OpencodePermissionMode } from '@hapi/protocol/types'
export const opencodeCommand: CommandDefinition = {
name: 'opencode',
requiresRuntimeAssets: true,
run: async ({ commandArgs }) => {
try {
const options: {
startedBy?: 'runner' | 'terminal'
startingMode?: 'local' | 'remote'
permissionMode?: OpencodePermissionMode
resumeSessionId?: string
} = {}
let hasExplicitPermissionMode = false
for (let i = 0; i < commandArgs.length; i++) {
const arg = commandArgs[i]
if (arg === '--started-by') {
options.startedBy = commandArgs[++i] as 'runner' | 'terminal'
} else if (arg === '--hapi-starting-mode') {
const value = commandArgs[++i]
if (value === 'local' || value === 'remote') {
options.startingMode = value
} else {
throw new Error('Invalid --hapi-starting-mode (expected local or remote)')
}
} else if (arg === '--permission-mode') {
const mode = commandArgs[++i]
if (!mode || !(OPENCODE_PERMISSION_MODES as readonly string[]).includes(mode)) {
throw new Error(`Invalid --permission-mode value: ${mode ?? '(missing)'}`)
}
options.permissionMode = mode as OpencodePermissionMode
hasExplicitPermissionMode = true
} else if (arg === '--yolo' && !hasExplicitPermissionMode) {
options.permissionMode = 'yolo'
} else if (arg === '--resume') {
const sessionId = commandArgs[++i]
if (!sessionId) {
throw new Error('Missing --resume value')
}
options.resumeSessionId = sessionId
}
}
await initializeToken()
await maybeAutoStartServer()
await authAndSetupMachineIfNeeded()
const { runOpencode } = await import('@/opencode/runOpencode')
await runOpencode(options)
} catch (error) {
console.error(chalk.red('Error:'), error instanceof Error ? error.message : 'Unknown error')
if (process.env.DEBUG) {
console.error(error)
}
process.exit(1)
}
}
}