diff --git a/changelog.d/ship-handoff-child-gets-its-prompt.md b/changelog.d/ship-handoff-child-gets-its-prompt.md new file mode 100644 index 000000000..f2a6e3a0c --- /dev/null +++ b/changelog.d/ship-handoff-child-gets-its-prompt.md @@ -0,0 +1,4 @@ +--- +important: blocker +--- +- **A `/ship` with a token cap on Claude Code no longer stops at its first handoff.** When the budget handed steps to a fresh child, `gstack-ship-budget launch` started `claude -p` with `--allowedTools ` right before the prompt. Claude Code reads every argument after that option as one more tool name, so the child got no prompt, exited with "Input must be provided either through stdin or as a prompt argument", and the run stopped. The launch now ends the options with `--` before the prompt, which Claude Code 2.1.292 and 2.1.326 both read as the prompt. Thanks to @exGeni for the report and diagnosis (#4448). diff --git a/lib/ship-handoff.ts b/lib/ship-handoff.ts index e19cdb190..786d187dc 100644 --- a/lib/ship-handoff.ts +++ b/lib/ship-handoff.ts @@ -304,7 +304,7 @@ export function cmdLaunch(argv: string[], ctx: Ctx): Outcome { const log = path.join(path.dirname(block.return_file), `child-${session}.log`); joinMeter(block.parent_ledger, session, ctx.env); const out = fs.openSync(log, 'a', 0o600); - const child = spawn(claude.command, [...claude.argsPrefix, '-p', '--session-id', session, ...CHILD_PERMISSION_ARGS, prompt], { cwd: ctx.cwd, env: ctx.env, detached: true, stdio: ['ignore', out, out] }); + const child = spawn(claude.command, [...claude.argsPrefix, '-p', '--session-id', session, ...CHILD_PERMISSION_ARGS, '--', prompt], { cwd: ctx.cwd, env: ctx.env, detached: true, stdio: ['ignore', out, out] }); child.unref(); fs.closeSync(out); return { stdout: [`CHILD: session=${session} pid=${child.pid} log=${log} return=${block.return_file}`], exit: 0 }; diff --git a/test/ship-budget.test.ts b/test/ship-budget.test.ts index 5043da5a1..af8871f95 100644 --- a/test/ship-budget.test.ts +++ b/test/ship-budget.test.ts @@ -301,6 +301,27 @@ describe('delegation', () => { }); describe('Claude Code child launch', () => { + /** + * The argv rules of `claude --help` (2.1.292 pinned in CI, 2.1.326): a `` option takes + * every following argument up to the next one that starts with '-', and '--' ends the options. + */ + function parseLikeClaudeCli(argv: string[]): { options: Record; prompts: string[] } { + const variadic = new Set(['--allowedTools', '--allowed-tools', '--disallowedTools', '--disallowed-tools', '--add-dir', '--tools']); + const valued = new Set(['--session-id', '--permission-mode', '--model', '--settings', '--output-format']); + const options: Record = {}; + const prompts: string[] = []; + for (let i = 0; i < argv.length; i++) { + const arg = argv[i]!; + if (arg === '--') { prompts.push(...argv.slice(i + 1)); break; } + if (variadic.has(arg)) { + const values = options[arg] ??= []; + while (i + 1 < argv.length && !argv[i + 1]!.startsWith('-')) values.push(argv[++i]!); + } else if (valued.has(arg)) options[arg] = [argv[++i]!]; + else if (!arg.startsWith('-')) prompts.push(arg); + } + return { options, prompts }; + } + test('launch runs the child in the background with explicit permission flags and the return file named', async () => { const h = fresh(); const s = started(h, ['--token-cap', '80M', '--meter', 'estimate']); @@ -314,9 +335,12 @@ describe('Claude Code child launch', () => { const session = h.field(r.out, 'CHILD', 'session'); expect(h.field(r.out, 'CHILD', 'return')).toBe(ho.out.match(/^RETURN_FILE: (\S+)$/m)![1]!); for (let i = 0; i < 50 && !fs.existsSync(argsFile); i++) await Bun.sleep(100); - const args = fs.readFileSync(argsFile, 'utf8').split('\n'); + const args = fs.readFileSync(argsFile, 'utf8').split('\n').slice(0, -1); expect(args.slice(0, 7)).toEqual(['-p', '--session-id', session, '--permission-mode', 'acceptEdits', '--allowedTools', 'Bash,Read,Edit,Write,Grep,Glob']); - expect(args[7]).toContain(`verify-handoff --in ${file} --session ${session}`); + const parsed = parseLikeClaudeCli(args); + expect(parsed.options['--allowedTools']).toEqual(['Bash,Read,Edit,Write,Grep,Glob']); + expect(parsed.prompts).toHaveLength(1); + expect(parsed.prompts[0]).toContain(`verify-handoff --in ${file} --session ${session}`); }); });