Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(cli): agent run refuses its own = flag inside the prompt
garrytan9349c47e89cb
diff --git a/scripts/structural-suites.tsv b/scripts/structural-suites.tsvindex 07c3f6b92c..dedcc9275c 100644--- a/scripts/structural-suites.tsv+++ b/scripts/structural-suites.tsv@@ -76,7 +76,7 @@ test/claude-cli-recipe.test.ts claude-cli LanguageModel — output cap (#6424) 2test/claude-cli-recipe.test.ts claude-cli LanguageModel — text-only round trip 4 readFileSynctest/claude-cli-recipe.test.ts claude-cli LanguageModel — tool use 17 readFileSynctest/claude-cli-recipe.test.ts claude-cli recipe registration 2 readFileSync-test/cli-flag-equals.test.ts #7619 refusals that keep consent, scope and targets right 25 readFileSync,source-surface-helper+test/cli-flag-equals.test.ts #7619 refusals that keep consent, scope and targets right 26 readFileSync,source-surface-helpertest/cli-flag-equals.test.ts #7619 split rules 21 readFileSync,source-surface-helpertest/cli-force-exit-teardown-arming.test.ts cli.ts — disconnect hard-deadline armed at teardown entry, not before the op body 1 readFileSync,source-surface-helpertest/cli.test.ts BigInt-safe output normalization (#2450) 5 source-surface-helperdiff --git a/src/cli/flag-equals.ts b/src/cli/flag-equals.tsindex 9d9da19746..ccb63c9b9e 100644--- a/src/cli/flag-equals.ts+++ b/src/cli/flag-equals.ts@@ -41,7 +41,9 @@* tokens, so `--tools=bash, read` still refuses) and, when the prompt holds no* bare `--`, the trailing switches it hoists, exact or with an `=` value and* no whitespace (`--detach=true` there refuses); the prompt between them is- * text. OWN_EQUALS_PARSERS pass their argv through. On a+ * text, except that one of its own flags written with `=` and no whitespace+ * refuses there (`agent run --json --tools=read ...` would hand the job every+ * tool) with advice to write it before the prompt or put the prompt after `--`. OWN_EQUALS_PARSERS pass their argv through. On a* command with a registry row, unregistered flags keep the token for the* validator to name; on a path with none (the hand-parsed search* subcommands), every `=` token but `--json=true` refuses with a neutral@@ -272,7 +274,11 @@ export async function splitFlagEquals(command: string, args: readonly string[]):if (RUN_VALUE_FLAGS.has(t)) values.add(++promptAt);else if (!BOOLEAN_TAIL_FLAGS.has(t) && !(t.startsWith('--') && EQUALS_TOKEN.test(t))) break;}- if (!args.slice(promptAt).includes('--')) while (tailAt > promptAt && BOOLEAN_TAIL_FLAGS.has(args[tailAt - 1]!.split('=')[0]!) && !/\s/.test(args[tailAt - 1]!)) tailAt--;+ if (!args.slice(promptAt).includes('--')) {+ while (tailAt > promptAt && BOOLEAN_TAIL_FLAGS.has(args[tailAt - 1]!.split('=')[0]!) && !/\s/.test(args[tailAt - 1]!)) tailAt--;+ const stray = args.slice(promptAt, tailAt).map(t => /^(--[a-z0-9][a-z0-9-]*)=\S*$/.exec(t)?.[1]).find(f => f !== undefined && (RUN_VALUE_FLAGS.has(f) || BOOLEAN_TAIL_FLAGS.has(f)));+ if (stray) return { refusal: usageError(`gbrain agent: \`${stray}=…\`: agent run reads its flags before the prompt, so this would be prompt text.`, `Write ${RUN_VALUE_FLAGS.has(stray) ? `${stray} <value>` : stray} before the prompt, or put the prompt after -- to keep it as text.`) };+ }}for (let i = 0; i < args.length; i++) {const token = args[i]!;diff --git a/test/cli-flag-equals.test.ts b/test/cli-flag-equals.test.tsindex 35d5361005..87954e4be6 100644--- a/test/cli-flag-equals.test.ts+++ b/test/cli-flag-equals.test.ts@@ -528,15 +528,13 @@ describe('#7619 refusals that keep consent, scope and targets right', () => {}});- // Value: protects=free text that only looks like an = flag reaches the handler (capture text, the agent run prompt), while the flag zone and the trailing switches agent run hoists still refuse; fails_when=the scan reads quoted text or the prompt as flags, or skips the flag zone or the hoisted trailing switches; why_new=the short-flag and prompt rows only covered refusals; seam=none; no_claim=agent run's own parse after the gate (argv-level)+ // Value: protects=free text that only looks like an = flag reaches the handler (capture text, agent run prompt words that are not its flags), while the flag zone and the trailing switches agent run hoists still refuse; fails_when=the scan reads quoted text or the prompt as flags, or skips the flag zone or the hoisted trailing switches; why_new=the short-flag and prompt rows only covered refusals; seam=none; no_claim=agent run's own parse after the gate (argv-level)test('free text that only looks like an = flag is not scanned', async () => {expect(await argvFor('capture', ['-n=3 is the answer'])).toEqual(['-n=3 is the answer']);- expect(await argvFor('agent', ['run', 'fix', 'the', '--model=opus', 'bug'])).toEqual(['run', 'fix', 'the', '--model=opus', 'bug']);- expect(await argvFor('agent', ['run', '--model', 'opus', 'fix', '--tools=x'])).toEqual(['run', '--model', 'opus', 'fix', '--tools=x']);expect(await refusalFor('agent', ['run', '--model=opus', 'fix'])).toContain('--model=…');expect(await refusalFor('agent', ['run', '--detach', '--tools=x', 'fix'])).toContain('--tools=…');- for (const args of [['run', '--detach', 'fix', '--model=opus'], ['run', '--follow', 'summarize', '--tools=bash'], ['run', '--detach', '-n=3'], ['run', '--note', '--tools=x'], ['run', 'fix', '--', '--detach=true'], ['run', '--', '--model=opus', 'fix'],- ['run', '--model', '-n=3', 'do X'], ['run', '--note: a=b', '--model=x'], ['run', '--re: x=1', '--detach=true', 'and', 'more'], ['run', 'summarize', '--follow=up items in the thread'], ['run', 'fix the bug', '--detach=now please']]) {+ for (const args of [['run', '--detach', '-n=3'], ['run', 'fix', '--', '--detach=true'], ['run', '--', 'fix', 'the', '--model=opus', 'bug'],+ ['run', '--model', '-n=3', 'do X'], ['run', 'summarize', '--follow=up items in the thread'], ['run', 'fix the bug', '--detach=now please'], ['run', 'fix', 'the', '--model opus', 'bug']]) {expect(await argvFor('agent', args)).toEqual(args);}for (const [args, flag] of [[['run', 'summarize the page', '--detach=true'], '--detach'], [['run', 'fix', '--detach', '--no-follow=1'], '--no-follow'], [['run', 'fix', '--follow=false', '--detach'], '--follow'], [['run', '--tools=bash, read', 'fix'], '--tools'], [['run', 'fix', '--follow=up'], '--follow']] as const) {@@ -546,6 +544,17 @@ describe('#7619 refusals that keep consent, scope and targets right', () => {}});+ // Value: protects=an agent run flag written with = inside the prompt (after a token parseRunFlags does not read, such as --json or --yes, or after a prompt word) refuses instead of reaching the job as prompt text with the restriction dropped; fails_when=agent run --json --tools=read submits a job with every tool; why_new=the flag zone ends at the first token parseRunFlags does not read, and everything after it was passed through; seam=none; no_claim=two-argument flags after the prompt (parseRunFlags design, unchanged)+ test('agent run refuses its own = flag inside the prompt and names both escapes', async () => {+ for (const [args, flag] of [[['run', '--json', '--tools=read', '--detach', 'do X'], '--tools'], [['run', '--yes', '--source=team-b', 'go'], '--source'], [['run', 'fix', 'the', '--model=opus', 'bug'], '--model'], [['run', '--model', 'opus', 'fix', '--tools=x'], '--tools'], [['run', '--detach', 'fix', '--model=opus'], '--model'], [['run', '--note', '--max-turns=2'], '--max-turns'], [['run', '--re: x=1', '--detach=true', 'and', 'more'], '--detach']] as const) {+ const r = await cliOnlyFlagArgs('agent', [...args]);+ expect('refusal' in r && r.refusal.code).toBe('invalid_params');+ expect('refusal' in r && r.refusal.message).toContain(`${flag}=…`);+ expect('refusal' in r && r.refusal.message).toContain('reads its flags before the prompt');+ expect('refusal' in r && r.refusal.suggestion).toContain('before the prompt, or put the prompt after --');+ }+ });+// Value: protects=the agent run = scan reads exactly the flags parseRunFlags reads; fails_when=a flag added to parseRunFlags but not to RUN_VALUE_FLAGS or BOOLEAN_TAIL_FLAGS ends the scanned zone early, so a later --model=x reaches the prompt unrefused; why_new=the two lists are kept by hand in agent.ts; seam=none; no_claim=flags of other agent subcommandstest('every agent flag parseRunFlags reads is in the scan tables', async () => {const { RUN_VALUE_FLAGS, BOOLEAN_TAIL_FLAGS, __testing } = await import('../src/commands/agent.ts');diff --git a/test/serve-flag-equals.test.ts b/test/serve-flag-equals.test.tsindex 882d6f4c25..6f4533c5a3 100644--- a/test/serve-flag-equals.test.ts+++ b/test/serve-flag-equals.test.ts@@ -194,4 +194,14 @@ describe('serve --flag=value', () => {expect(r.status).toBe(0);expect(JSON.parse(r.stdout)).toMatchObject({ pack: 'pack-example' });});++ // Value: protects=agent run never submits a job whose tool restriction was written with = after a token its parser does not read; fails_when=the --tools=read token reaches the prompt and the subagent job runs with every tool; why_new=in-process rows never checked the queue; seam=none; no_claim=the job's execution (it stays queued)+ test('agent run --json --tools=read refuses and enqueues no job', () => {+ const r = cli(['agent', 'run', '--json', '--tools=read', '--detach', 'do X']);+ expect(r.status).toBe(2);+ expect(r.stdout + r.stderr).toContain('reads its flags before the prompt');+ const jobs = cli(['jobs', 'list', '--json']);+ expect(jobs.status).toBe(0);+ expect(jobs.stdout).not.toContain('subagent');+ });});