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.

fix(agents): never append a permission flag the launch command already carries

enes856ce7916ff6
diff --git a/src/renderer/components/settings/sections/AgentsSection.tsx b/src/renderer/components/settings/sections/AgentsSection.tsx
index db877137f..2bf875e54 100644
--- a/src/renderer/components/settings/sections/AgentsSection.tsx
+++ b/src/renderer/components/settings/sections/AgentsSection.tsx
@@ -330,6 +330,7 @@ export function AgentsSection({ isActive }: { isActive: boolean }): React.JSX.El
'Launch an agent with your own command — e.g. a wrapper script that switches accounts or sets env vars. ' +
'Used everywhere the agent is launched (new sessions, resumes, restarts), with flags like --resume appended after it, ' +
'so the command must pass its arguments through — a shell script should end with `exec claude "$@"`. ' +
+ 'A flag your command already spells is left alone: write the permission mode here and it wins over the start-up mode below. ' +
'Leave empty for the default. SSH projects run the same command on the remote host.'
}
control={null}
diff --git a/src/shared/agents/approval-mode.test.ts b/src/shared/agents/approval-mode.test.ts
index c749816fb..ff17a5021 100644
--- a/src/shared/agents/approval-mode.test.ts
+++ b/src/shared/agents/approval-mode.test.ts
@@ -210,6 +210,69 @@ describe('UI copy derived from the mapping', () => {
* one that matters — a plain-object lookup table answers `'constructor' in table` with true and
* hands back a Function, which would have been stringified onto a command line.
*/
+/**
+ * Issue #601 — a launch-command override that already spells the approval flag.
+ *
+ * `settings.agentLaunchCommands` replaces the program part with the user's own wrapper, and the
+ * wrapper is entitled to carry the flag itself. Appending regardless produced
+ * `claude --permission-mode bypassPermissions --permission-mode auto`: a duplicate the settings
+ * field still displayed as exactly what was typed, so whichever occurrence the CLI honoured, the
+ * user could not tell which one was in force from the UI.
+ */
+describe('withPermissionMode — a flag the command already carries (issue #601)', () => {
+ it('does not append a second --permission-mode', () => {
+ expect(withPermissionMode('claude --permission-mode bypassPermissions', 'claude', 'auto')).toBe(
+ 'claude --permission-mode bypassPermissions'
+ )
+ })
+
+ it('reads the `--flag=value` spelling too', () => {
+ expect(withPermissionMode('claude --permission-mode=plan', 'claude', 'auto')).toBe(
+ 'claude --permission-mode=plan'
+ )
+ })
+
+ it('still appends when the override spells a DIFFERENT flag', () => {
+ // The reporter's own benign case: the two flags are about different things, so nodeterm keeps
+ // ownership of the one the user said nothing about.
+ expect(withPermissionMode('claude --allow-dangerously-skip-permissions', 'claude', 'auto')).toBe(
+ 'claude --allow-dangerously-skip-permissions --permission-mode auto'
+ )
+ })
+
+ it('answers per agent — a gemini wrapper carrying claude’s spelling is not a match', () => {
+ // The dialects differ (`--approval-mode` vs `--permission-mode`), so the suppression has to be
+ // keyed on the flag THIS agent would actually emit, not on a fixed string.
+ expect(withPermissionMode('gemini --approval-mode yolo', 'gemini', 'plan')).toBe(
+ 'gemini --approval-mode yolo'
+ )
+ expect(withPermissionMode('gemini --permission-mode plan', 'gemini', 'plan')).toBe(
+ 'gemini --permission-mode plan --approval-mode plan'
+ )
+ })
+
+ it('does not match the flag NAMED inside a quoted argument', () => {
+ // Why this is tokenized rather than a substring search: a wrapper may talk ABOUT the flag, and
+ // suppressing nodeterm's real flag over a sentence would silently change the mode every node
+ // launches in.
+ expect(
+ withPermissionMode(
+ "claude --append-system-prompt 'never suggest --permission-mode'",
+ 'claude',
+ 'auto'
+ )
+ ).toBe("claude --append-system-prompt 'never suggest --permission-mode' --permission-mode auto")
+ })
+
+ it('leaves every non-override command byte-identical', () => {
+ // The regression guard for everyone who has not set an override: nodeterm builds those lines
+ // and never puts the flag in twice, so the new branch is unreachable for them.
+ expect(withPermissionMode('claude', 'claude', 'auto')).toBe('claude --permission-mode auto')
+ expect(withPermissionMode('codex', 'codex', 'manual')).toBe('codex --ask-for-approval untrusted')
+ expect(withPermissionMode('claude', 'claude', 'manual')).toBe('claude')
+ })
+})
+
describe('approvalFlags — a forged mode yields the bare command', () => {
const FORGED = ['yolo', 'on-request', 'constructor', '__proto__', 'toString', '', undefined, null]
it('emits no flag for any agent', () => {
diff --git a/src/shared/agents/approval-mode.ts b/src/shared/agents/approval-mode.ts
index d48f12af4..937e8f7c2 100644
--- a/src/shared/agents/approval-mode.ts
+++ b/src/shared/agents/approval-mode.ts
@@ -23,6 +23,7 @@ import {
type AgentPermissionMode,
type BuiltinAgentId
} from './config'
+import { argvHasFlag } from '../shell-quote'
/** One agent's approval dialect: the flag it spells, and the values it accepts. ONE fact per agent —
* a flag and a table maintained separately is how a third agent added to the table silently emits
@@ -130,10 +131,33 @@ export function approvalFlags(agentId: AgentId, mode: AgentPermissionMode): stri
* WHERE the flag lands is decided one layer up, by `createAgentNode`: with no `argvPromptSeparator`
* (claude, gemini, codex) it goes LAST, keeping those command lines byte-identical; with one
* (grok's `--`) it must go BEFORE the separator, because `--` is end-of-options.
+ *
+ * **A flag the command already carries is left alone (issue #601).** `cmd` is not always ours:
+ * `settings.agentLaunchCommands` lets the user replace the program part with a wrapper, and a
+ * wrapper may spell the approval flag itself. Appending regardless produced
+ * `claude --permission-mode bypassPermissions --permission-mode auto` — a duplicate the settings
+ * field still displayed as exactly what the user typed, so whichever occurrence the CLI honoured,
+ * they had no way to tell which one was in force.
+ *
+ * The contract this settles is "program only, minus what you spelled yourself", not "the override
+ * owns the whole command". The whole-command reading cannot work: the settings copy already
+ * promises that `--resume` and friends are appended, and a wrapper has no way to know the session id
+ * a cold restore is resuming — so nodeterm has to keep appending. What it must not do is append a
+ * SECOND opinion about a flag the user has already stated one about; theirs is the more specific and
+ * the more explicit, and it wins.
+ *
+ * Deliberately not extended to `withAgentModel`: a model switch is a per-node action the user just
+ * took, and letting a global launch-command override veto it would silently strand that node on the
+ * wrapper's model. Different specificity, opposite answer.
+ *
+ * A command with no override cannot reach the suppression — nodeterm builds it and never puts the
+ * flag in twice — so every existing launch line is byte-identical.
*/
export function withPermissionMode(cmd: string, id: AgentId, mode: AgentPermissionMode): string {
const flags = approvalFlags(id, mode)
- return flags.length ? `${cmd} ${flags.join(' ')}` : cmd
+ if (!flags.length) return cmd
+ if (argvHasFlag(cmd, flags[0])) return cmd
+ return `${cmd} ${flags.join(' ')}`
}
// ---------------------------------------------------------------------------------------------
diff --git a/src/shared/agents/launch.test.ts b/src/shared/agents/launch.test.ts
index c1b6d048e..56ffac454 100644
--- a/src/shared/agents/launch.test.ts
+++ b/src/shared/agents/launch.test.ts
@@ -370,6 +370,51 @@ describe('assembleLaunchCommand — promptFile (issue #520)', () => {
})
})
+/**
+ * Issue #601, at the layer the reporter actually read it off — the assembled command line, the one
+ * that shows up in the process table. `withPermissionMode` has its own unit tests; these two pin
+ * that the composition does not put the flag back.
+ */
+describe('assembleLaunchCommand / assembleResumeCommand — override already carries the flag (#601)', () => {
+ it('does not duplicate --permission-mode on a fresh launch', () => {
+ expect(
+ assembleLaunchCommand(
+ {
+ agentId: 'claude',
+ launchCmdOverride: 'claude --permission-mode bypassPermissions',
+ permissionMode: 'auto'
+ },
+ ENV
+ ).command
+ ).toBe('claude --permission-mode bypassPermissions')
+ })
+
+ it('does not duplicate it on the resume path either', () => {
+ // The cold-restore / restart leg. It resumes through the same wrapper, so it inherits the same
+ // duplicate if this is fixed in only one assembler.
+ expect(
+ assembleResumeCommand(
+ {
+ agentId: 'claude',
+ launchCmdOverride: 'claude --permission-mode=plan',
+ sessionId: 'abc-123',
+ permissionMode: 'auto'
+ },
+ ENV
+ ).command
+ ).toBe('claude --permission-mode=plan --resume abc-123')
+ })
+
+ it('still appends to an override that says nothing about permissions', () => {
+ expect(
+ assembleLaunchCommand(
+ { agentId: 'claude', launchCmdOverride: 'my-wrapper', permissionMode: 'auto' },
+ ENV
+ ).command
+ ).toBe('my-wrapper --permission-mode auto')
+ })
+})
+
describe('promptFilePathError', () => {
it('accepts an absolute POSIX path', () => {
expect(promptFilePathError('/tmp/brief.md')).toBeNull()
diff --git a/src/shared/shell-quote.test.ts b/src/shared/shell-quote.test.ts
index a2998760d..7f940daab 100644
--- a/src/shared/shell-quote.test.ts
+++ b/src/shared/shell-quote.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
-import { shellSingleQuote, shellSplit } from './shell-quote'
+import { argvHasFlag, shellSingleQuote, shellSplit } from './shell-quote'
describe('shellSingleQuote', () => {
it('wraps a plain string in single quotes', () => {
@@ -31,3 +31,26 @@ describe('shellSplit', () => {
expect(shellSplit('$HOME/x')).toEqual(['$HOME/x'])
})
})
+
+describe('argvHasFlag', () => {
+ it('finds both spellings of an option', () => {
+ expect(argvHasFlag('claude --permission-mode plan', '--permission-mode')).toBe(true)
+ expect(argvHasFlag('claude --permission-mode=plan', '--permission-mode')).toBe(true)
+ })
+ it('is false when the flag is absent', () => {
+ expect(argvHasFlag('claude --resume abc', '--permission-mode')).toBe(false)
+ expect(argvHasFlag('', '--permission-mode')).toBe(false)
+ })
+ it('does not match a PREFIX of a longer option', () => {
+ // `--permission-mode-file` is a different flag; matching it would suppress the real one.
+ expect(argvHasFlag('claude --permission-mode-file /x', '--permission-mode')).toBe(false)
+ })
+ it('does not match the flag inside a quoted argument', () => {
+ expect(argvHasFlag("claude -p 'use --permission-mode'", '--permission-mode')).toBe(false)
+ expect(argvHasFlag('claude -p "use --permission-mode"', '--permission-mode')).toBe(false)
+ })
+ it('stops at a bare `--` — past end-of-options a word is data, not an option', () => {
+ expect(argvHasFlag('grok -- --permission-mode', '--permission-mode')).toBe(false)
+ expect(argvHasFlag('grok --permission-mode plan -- prompt', '--permission-mode')).toBe(true)
+ })
+})
diff --git a/src/shared/shell-quote.ts b/src/shared/shell-quote.ts
index 8ad42fb29..0d889d825 100644
--- a/src/shared/shell-quote.ts
+++ b/src/shared/shell-quote.ts
@@ -35,6 +35,32 @@ export function shellQuoteIfNeeded(s: string): string {
* (see launch.ts), so a resolved value containing spaces or metacharacters stays one argument by
* construction — the token boundary is fixed before any environment value enters the string.
*/
+/**
+ * Does this command line already carry `flag` as an OPTION of its own?
+ *
+ * Issue #601: nodeterm appends its managed flags to a launch command it did not necessarily write —
+ * `settings.agentLaunchCommands` lets the user replace the program part with a wrapper, and a
+ * wrapper is entitled to spell an option nodeterm also spells. Appending blindly produced
+ * `claude --permission-mode bypassPermissions --permission-mode auto`: a duplicate the field still
+ * displayed as what the user typed, so whichever occurrence the CLI honoured, they had no way to
+ * tell which.
+ *
+ * Both spellings count (`--flag value` and `--flag=value`), and the answer is computed from TOKENS
+ * rather than from a substring search, which is the whole reason this lives beside `shellSplit`: an
+ * override like `claude --append-system-prompt 'mind --permission-mode'` mentions the flag inside a
+ * quoted argument, and a substring match there would suppress nodeterm's real flag over a sentence.
+ *
+ * Scanning stops at a bare `--`, because everything after end-of-options is a positional argument —
+ * a word there is the CLI's data, not an option it will act on.
+ */
+export function argvHasFlag(cmd: string, flag: string): boolean {
+ for (const token of shellSplit(cmd)) {
+ if (token === '--') return false
+ if (token === flag || token.startsWith(`${flag}=`)) return true
+ }
+ return false
+}
+
export function shellSplit(input: string): string[] {
const tokens: string[] = []
let buf = ''