Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(cli): agent run scans the prompt up to its first bare --

garrytandcda02785dcf
diff --git a/src/cli/flag-equals.ts b/src/cli/flag-equals.ts
index ccb63c9b9e..5bc6513770 100644
--- a/src/cli/flag-equals.ts
+++ b/src/cli/flag-equals.ts
@@ -41,9 +41,10 @@
* 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, 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
+ * text, except that up to its first bare `--` one of its value flags written
+ * with `=` (any value) or a switch written with `=` and no whitespace refuses
+ * (`agent run --json --tools=read ...` would hand the job every tool), with
+ * advice to write it before the prompt or put `--` before the prompt. 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
@@ -274,11 +275,10 @@ 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--;
- 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.`) };
- }
+ const dashAt = args.indexOf('--', promptAt);
+ if (dashAt < 0) while (tailAt > promptAt && BOOLEAN_TAIL_FLAGS.has(args[tailAt - 1]!.split('=')[0]!) && !/\s/.test(args[tailAt - 1]!)) tailAt--;
+ const stray = args.slice(promptAt, dashAt < 0 ? tailAt : dashAt).map(t => /^(--[a-z0-9][a-z0-9-]*)=([\s\S]*)$/.exec(t)).find(m => m !== null && (RUN_VALUE_FLAGS.has(m[1]!) || BOOLEAN_TAIL_FLAGS.has(m[1]!) && !/\s/.test(m[2]!)))?.[1];
+ 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 and ahead of any word agent run does not read (such as --json or --yes), or put -- before the prompt 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.ts
index 87954e4be6..8e1f4c413b 100644
--- a/test/cli-flag-equals.test.ts
+++ b/test/cli-flag-equals.test.ts
@@ -546,12 +546,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) {
+ 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'],
+ [['run', '--json', '--tools=read', '--detach', '--', 'do X'], '--tools'], [['run', '--yes', '--source=team-b', 'go', '--', 'x'], '--source'], [['run', '--json', '--tools=read, bash', 'fix'], '--tools']] 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 --');
+ expect('refusal' in r && r.refusal.suggestion).toContain(flag === '--detach' ? '--detach before the prompt' : `${flag} <value> before the prompt`);
+ expect('refusal' in r && r.refusal.suggestion).toContain('or put -- before the prompt');
+ }
+ for (const args of [['run', 'fix', '--', '--tools=read'], ['run', '--json', '--', '--tools=read', 'text'], ['run', 'summarize', '--follow=up items in the thread', '--', 'x']]) {
+ expect(await argvFor('agent', args)).toEqual(args);
}
});
diff --git a/test/serve-flag-equals.test.ts b/test/serve-flag-equals.test.ts
index 6f4533c5a3..5cfc9752ca 100644
--- a/test/serve-flag-equals.test.ts
+++ b/test/serve-flag-equals.test.ts
@@ -1,5 +1,6 @@
/**
- * #7619 (real failure path): `gbrain serve` safety flags in the `--flag=value`
+ * #7619 (real failure path): `gbrain serve` safety flags (and the other
+ * commands below: auth, consent, schema, agent run) in the `--flag=value`
* form over a real CLI process on a hermetic PGLite brain. Before the fix the
* entry accepted these tokens and serve never read them: `--access=read-only`
* and `--surface=verbs` served every write tool, `--token-ttl=59` and
@@ -197,9 +198,11 @@ describe('serve --flag=value', () => {
// 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');
+ for (const args of [['agent', 'run', '--json', '--tools=read', '--detach', 'do X'], ['agent', 'run', '--json', '--tools=read', '--detach', '--', 'do X']]) {
+ const r = cli(args);
+ 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');