Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(cli): agent run flag zone reads only parseRunFlags' tokens

garrytan6cc84ad89071
diff --git a/scripts/structural-suites.tsv b/scripts/structural-suites.tsv
index 24d800bb0b..07c3f6b92c 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) 2
test/claude-cli-recipe.test.ts claude-cli LanguageModel — text-only round trip 4 readFileSync
test/claude-cli-recipe.test.ts claude-cli LanguageModel — tool use 17 readFileSync
test/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 24 readFileSync,source-surface-helper
+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 split rules 21 readFileSync,source-surface-helper
test/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-helper
test/cli.test.ts BigInt-safe output normalization (#2450) 5 source-surface-helper
diff --git a/src/cli/flag-equals.ts b/src/cli/flag-equals.ts
index 322023b481..9d9da19746 100644
--- a/src/cli/flag-equals.ts
+++ b/src/cli/flag-equals.ts
@@ -36,10 +36,12 @@
* which may be a credential. A short flag never takes `=` (`-y=n` is refused,
* never taken as consent); a quoted argument with whitespace that only starts
* like one (`capture "-n=3 is the answer"`) is text. `agent run` scans only
- * where parseRunFlags reads flags: the leading flag zone (up to the first word
- * that is not one of its flags or a value flag's value) and, when the prompt
- * holds no bare `--`, the trailing switches it hoists (`--detach=true` there
- * refuses); the prompt between them is text. OWN_EQUALS_PARSERS pass their argv through. On a
+ * where parseRunFlags reads flags: the leading flag zone (its switches, its value
+ * flags with their value, which passes untouched, and flag-shaped `--x=`
+ * 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
* 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
@@ -224,6 +226,7 @@ export const OWN_EQUALS_PARSERS: ReadonlySet<string> = new Set([
export const COMMAND_SWITCHES: Readonly<Record<string, readonly string[]>> = {
sync: ['--detail'],
schema: ['--fields'],
+ agent: ['--follow', '--detach'],
dream: ['--unsafe-bypass-dream-guard'],
embed: ['--stale'],
};
@@ -261,15 +264,20 @@ export async function splitFlagEquals(command: string, args: readonly string[]):
let pastDashDash = false;
let promptAt = -1;
let tailAt = args.length;
+ const values = new Set<number>();
if (command === 'agent' && args[0] === 'run') {
const { RUN_VALUE_FLAGS, BOOLEAN_TAIL_FLAGS } = await import('../commands/agent.ts');
- promptAt = 1;
- while (promptAt < args.length && args[promptAt] !== '--' && (/^--[^=]+=/.test(args[promptAt]!) || RUN_VALUE_FLAGS.has(args[promptAt]!) || BOOLEAN_TAIL_FLAGS.has(args[promptAt]!))) promptAt += RUN_VALUE_FLAGS.has(args[promptAt]!) ? 2 : 1;
- if (!args.slice(promptAt).includes('--')) while (tailAt > promptAt && BOOLEAN_TAIL_FLAGS.has(args[tailAt - 1]!.split('=')[0]!)) tailAt--;
+ for (promptAt = 1; promptAt < args.length; promptAt++) {
+ const t = args[promptAt]!;
+ 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--;
}
for (let i = 0; i < args.length; i++) {
const token = args[i]!;
if (token === '--' && END_OF_OPTIONS.has(`${command} ${args[0]}`)) return { args: [...out, ...args.slice(i)] };
+ if (values.has(i)) { out.push(token); continue; }
if (i === promptAt && promptAt < tailAt) { out.push(...args.slice(i, tailAt)); i = tailAt - 1; continue; }
pastDashDash ||= token === '--';
const m = EQUALS_TOKEN.exec(token);
diff --git a/test/cli-flag-equals.test.ts b/test/cli-flag-equals.test.ts
index 10537adaa3..35d5361005 100644
--- a/test/cli-flag-equals.test.ts
+++ b/test/cli-flag-equals.test.ts
@@ -535,13 +535,24 @@ describe('#7619 refusals that keep consent, scope and targets right', () => {
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']]) {
+ 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']]) {
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']] as const) {
+ 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) {
const message = await refusalFor('agent', [...args]);
expect(message).toContain(`${flag}=…`);
- expect(message).toMatch(new RegExp(`${flag} is a switch|write ${flag} on its own`));
+ expect(message).toMatch(new RegExp(`${flag} is a switch|${flag} <value> as two arguments`));
+ }
+ });
+
+ // 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 subcommands
+ test('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');
+ for (const flag of CLI_FLAG_REGISTRY.agent!) {
+ const { rest } = __testing.parseRunFlags([flag, '5', 'prompt']);
+ const kind = rest.length === 1 ? 'value' : rest[0] === '5' ? 'switch' : 'prompt';
+ expect([flag, kind]).toEqual([flag, RUN_VALUE_FLAGS.has(flag) ? 'value' : BOOLEAN_TAIL_FLAGS.has(flag) ? 'switch' : 'prompt']);
}
});