Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(agents): pure permission-mode core (flags, resolver)

enescf788b4405ee
diff --git a/src/shared/agents/config.ts b/src/shared/agents/config.ts
index 8dd2e16b9..372c345f8 100644
--- a/src/shared/agents/config.ts
+++ b/src/shared/agents/config.ts
@@ -106,3 +106,71 @@ export function resumeCommand(id: AgentId, sessionId: string): string | null {
return null
}
}
+
+/**
+ * The permission mode an agent session STARTS in. The user can still cycle modes at runtime
+ * with Shift+Tab — this only decides the starting state, which is exactly what the CLI's
+ * `--permission-mode` flag does.
+ *
+ * `dontAsk` is deliberately not exposed: from the user's point of view it overlaps `auto`.
+ */
+export type AgentPermissionMode = 'manual' | 'auto' | 'acceptEdits' | 'plan' | 'bypassPermissions'
+
+export const ALL_PERMISSION_MODES: readonly AgentPermissionMode[] = [
+ 'manual',
+ 'auto',
+ 'acceptEdits',
+ 'plan',
+ 'bypassPermissions'
+]
+
+export const PERMISSION_MODE_LABELS: Record<AgentPermissionMode, string> = {
+ manual: 'Ask each time',
+ auto: 'Auto',
+ acceptEdits: 'Accept edits',
+ plan: 'Plan',
+ bypassPermissions: 'Bypass all'
+}
+
+/** Fallback whenever a persisted mode is missing or unrecognized. */
+export const DEFAULT_PERMISSION_MODE: AgentPermissionMode = 'auto'
+
+const isPermissionMode = (v: unknown): v is AgentPermissionMode =>
+ typeof v === 'string' && (ALL_PERMISSION_MODES as readonly string[]).includes(v)
+
+// Only claude's flag surface is verified. codex (--ask-for-approval) and gemini
+// (--approval-mode) join by being added here with their own flag mapping.
+export const PERMISSION_MODE_CAPABLE = ['claude'] as const
+
+export const hasPermissionMode = (id: AgentId): boolean => includes(PERMISSION_MODE_CAPABLE, id)
+
+/** CLI flags for a mode. `manual` yields NO flags, so the command stays bare — the exact
+ * command nodeterm shipped before this setting existed. */
+export function permissionModeFlag(mode: AgentPermissionMode): string[] {
+ return mode === 'manual' ? [] : ['--permission-mode', mode]
+}
+
+/** Appends the permission-mode flag to a launch command, if the agent supports one. The single
+ * funnel for every CLI launch path (new node, cold-restore resume, branch). */
+export function withPermissionMode(cmd: string, id: AgentId, mode: AgentPermissionMode): string {
+ if (!hasPermissionMode(id)) return cmd
+ const flags = permissionModeFlag(mode)
+ return flags.length ? `${cmd} ${flags.join(' ')}` : cmd
+}
+
+/**
+ * The mode a new session starts in: the project override, else the global setting.
+ * Mirrors resolveNewNodeAccount's shape, including its stale-value guard — an unrecognized
+ * persisted mode must never reach the CLI as a flag value.
+ *
+ * Structurally typed (not `Project`/`Settings`) because src/shared/types.ts imports THIS file;
+ * importing it back would be a cycle.
+ */
+export function resolvePermissionMode(
+ project: { defaultPermissionMode?: AgentPermissionMode } | undefined,
+ settings: { claudePermissionMode: AgentPermissionMode }
+): AgentPermissionMode {
+ if (isPermissionMode(project?.defaultPermissionMode)) return project.defaultPermissionMode
+ if (isPermissionMode(settings.claudePermissionMode)) return settings.claudePermissionMode
+ return DEFAULT_PERMISSION_MODE
+}
diff --git a/src/shared/agents/permission-mode.test.ts b/src/shared/agents/permission-mode.test.ts
new file mode 100644
index 000000000..dbd9a01f9
--- /dev/null
+++ b/src/shared/agents/permission-mode.test.ts
@@ -0,0 +1,90 @@
+import { describe, it, expect } from 'vitest'
+import {
+ hasPermissionMode,
+ permissionModeFlag,
+ withPermissionMode,
+ resolvePermissionMode,
+ PERMISSION_MODE_LABELS,
+ type AgentPermissionMode
+} from './config'
+
+describe('hasPermissionMode', () => {
+ it('is claude-only', () => {
+ expect(hasPermissionMode('claude')).toBe(true)
+ expect(hasPermissionMode('codex')).toBe(false)
+ expect(hasPermissionMode('gemini')).toBe(false)
+ expect(hasPermissionMode('custom:abc')).toBe(false)
+ })
+})
+
+describe('permissionModeFlag', () => {
+ // Load-bearing: "ask each time" must reproduce today's bare command exactly, so the
+ // setting can always be returned to a known-good state.
+ it('returns no flags for manual', () => {
+ expect(permissionModeFlag('manual')).toEqual([])
+ })
+
+ it('returns the CLI flag pair for every other mode', () => {
+ expect(permissionModeFlag('auto')).toEqual(['--permission-mode', 'auto'])
+ expect(permissionModeFlag('acceptEdits')).toEqual(['--permission-mode', 'acceptEdits'])
+ expect(permissionModeFlag('plan')).toEqual(['--permission-mode', 'plan'])
+ expect(permissionModeFlag('bypassPermissions')).toEqual([
+ '--permission-mode',
+ 'bypassPermissions'
+ ])
+ })
+})
+
+describe('withPermissionMode', () => {
+ it('appends the flag for a capable agent', () => {
+ expect(withPermissionMode('claude', 'claude', 'auto')).toBe('claude --permission-mode auto')
+ })
+
+ it('leaves the command bare in manual mode', () => {
+ expect(withPermissionMode('claude', 'claude', 'manual')).toBe('claude')
+ })
+
+ it('appends after existing args (Branch resume)', () => {
+ expect(withPermissionMode('claude -r abc123', 'claude', 'auto')).toBe(
+ 'claude -r abc123 --permission-mode auto'
+ )
+ })
+
+ it('never touches a non-capable agent', () => {
+ expect(withPermissionMode('codex', 'codex', 'auto')).toBe('codex')
+ expect(withPermissionMode('my-agent', 'custom:x', 'bypassPermissions')).toBe('my-agent')
+ })
+})
+
+describe('resolvePermissionMode', () => {
+ const settings = { claudePermissionMode: 'auto' as AgentPermissionMode }
+
+ it('falls back to the global when the project has no override', () => {
+ expect(resolvePermissionMode({}, settings)).toBe('auto')
+ expect(resolvePermissionMode(undefined, settings)).toBe('auto')
+ })
+
+ it('lets the project override the global', () => {
+ expect(resolvePermissionMode({ defaultPermissionMode: 'plan' }, settings)).toBe('plan')
+ })
+
+ it('ignores an unknown persisted value rather than passing it to the CLI', () => {
+ const project = { defaultPermissionMode: 'yolo' as unknown as AgentPermissionMode }
+ expect(resolvePermissionMode(project, settings)).toBe('auto')
+ })
+
+ it('ignores an unknown global value and falls back to auto', () => {
+ const bad = { claudePermissionMode: 'yolo' as unknown as AgentPermissionMode }
+ expect(resolvePermissionMode({}, bad)).toBe('auto')
+ })
+})
+
+describe('PERMISSION_MODE_LABELS', () => {
+ it('labels every mode', () => {
+ expect(PERMISSION_MODE_LABELS.manual).toBe('Ask each time')
+ expect(PERMISSION_MODE_LABELS.auto).toBe('Auto')
+ expect(PERMISSION_MODE_LABELS.acceptEdits).toBe('Accept edits')
+ expect(PERMISSION_MODE_LABELS.plan).toBe('Plan')
+ expect(PERMISSION_MODE_LABELS.bypassPermissions).toBe('Bypass all')
+ })
+})