fix: drop orphan flag when filtering empty-string spawn args (#2049)

Observations were 100% failing on Claude Code 2.1.109+ because the Agent
SDK emits ["--setting-sources", ""] when settingSources defaults to [].
The existing Bun-workaround filter stripped the empty string but left
the orphan --setting-sources flag, which then consumed --permission-mode
as its value, crashing the subprocess with:

  Error processing --setting-sources:
  Invalid setting source: --permission-mode.

Make the filter pair-aware: when an empty arg follows a --flag, drop
both so the SDK default (no setting sources) is preserved by omission.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Alex Newman
2026-04-16 14:30:54 -07:00
parent 70a150db74
commit c76a439491
4 changed files with 247 additions and 236 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+16 -5
View File
@@ -412,11 +412,22 @@ export function createPidCapturingSpawn(sessionDbId: number) {
const useCmdWrapper = process.platform === 'win32' && spawnOptions.command.endsWith('.cmd'); const useCmdWrapper = process.platform === 'win32' && spawnOptions.command.endsWith('.cmd');
const env = sanitizeEnv(spawnOptions.env ?? process.env); const env = sanitizeEnv(spawnOptions.env ?? process.env);
// Filter empty string args: Bun's spawn() silently drops empty strings from argv, // Filter empty string args AND their preceding flag (Issue #2049).
// causing subsequent flags to be consumed as values for the preceding flag. // The Agent SDK emits ["--setting-sources", ""] when settingSources defaults to [].
// The Agent SDK may produce empty-string args (e.g., settingSources defaults to [] // Simply dropping "" leaves an orphan --setting-sources that consumes the next
// which joins to ""). Node preserves these, but Bun drops them, breaking CLI parsing. // flag (e.g. --permission-mode) as its value, crashing Claude Code 2.1.109+ with
const args = spawnOptions.args.filter(arg => arg !== ''); // "Invalid setting source: --permission-mode". Drop the flag too so the SDK
// default (no setting sources) is preserved by omission.
const args: string[] = [];
for (const arg of spawnOptions.args) {
if (arg === '') {
if (args.length > 0 && args[args.length - 1].startsWith('--')) {
args.pop();
}
continue;
}
args.push(arg);
}
const child = useCmdWrapper const child = useCmdWrapper
? spawn('cmd.exe', ['/d', '/c', spawnOptions.command, ...args], { ? spawn('cmd.exe', ['/d', '/c', spawnOptions.command, ...args], {