Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(cli): accept --dry-run on auth local-writer and rescope commands

garrytan14336e756ab7
diff --git a/scripts/generate-flag-registry.ts b/scripts/generate-flag-registry.ts
index 22700ef26f..7f36889d95 100644
--- a/scripts/generate-flag-registry.ts
+++ b/scripts/generate-flag-registry.ts
@@ -56,6 +56,18 @@ const EXTRA_FLAGS: Record<string, string[]> = {
forget: ['--reason', '--request-id'],
};
+/**
+ * Command modules a command hands its argv to through a nested handler import,
+ * scanned at module depth so their safety-flag reads count as consumption
+ * evidence. Their own imports are not walked. Keep commented.
+ */
+const DELEGATED_MODULES: Record<string, string[]> = {
+ // `auth local-writer list|register|revoke` passes its argv to
+ // runPersistenceAdminCli (#5595), and `auth rescope-client|rescope-token` to
+ // parseRescopeGrantArgs; both parse --dry-run themselves.
+ auth: ['src/commands/persistence-admin.ts', 'src/core/grants/cli.ts'],
+};
+
/**
* Modules the import scan must SKIP. thin-client-routing.ts is a pure router —
* its flag literals belong to the commands it routes (takes/search/jobs/cache/
@@ -428,6 +440,14 @@ export function buildFlagRegistry(root: string = ROOT): Record<string, string[]>
}
}
+ for (const rel of DELEGATED_MODULES[command] ?? []) {
+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- build-time generator over this repository's own source tree; every joined segment is a constant module path from DELEGATED_MODULES
+ const path = join(root, rel);
+ if (!existsSync(path)) continue;
+ const code = stripComments(readSrc(path));
+ depthZeroText += code;
+ for (const f of flagsInText(code)) { flags.add(f); depthZero.add(f); }
+ }
for (const f of EXTRA_FLAGS[command] ?? []) { flags.add(f); depthZero.add(f); }
for (const f of SAFETY_FLAGS) {
if (flags.has(f) && !consumes(depthZeroText, f)) flags.delete(f);
diff --git a/src/core/cli-flag-registry.generated.ts b/src/core/cli-flag-registry.generated.ts
index 6b9da0a73d..3ec8d1d808 100644
--- a/src/core/cli-flag-registry.generated.ts
+++ b/src/core/cli-flag-registry.generated.ts
@@ -18,7 +18,7 @@ export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {
'agent': ['--allow-old-serve', '--brain', '--detach', '--fanout-manifest', '--federated-read', '--flags', '--follow', '--harness', '--help', '--http', '--json', '--max-turns', '--mcp-only', '--model', '--no-follow', '--path', '--port', '--preset', '--reissue', '--scopes', '--show-token', '--since', '--source', '--subagent-def', '--surface', '--timeout-ms', '--token-ttl', '--tools', '--url', '--word', '--yes'],
'anomalies': ['--brain', '--help', '--json', '--lookback-days', '--mcp-only', '--sigma', '--since', '--source'],
'apply-migrations': ['--acknowledge-no-backup', '--apply', '--backup-confirmed', '--brain', '--break-lock', '--confirm-quiesced', '--content-root', '--dry-run', '--export-db-only', '--export-source', '--force', '--force-all', '--force-orchestrator', '--force-retry', '--force-schema', '--help', '--host-dir', '--json', '--list', '--locks', '--migrate-only', '--migration', '--mode', '--no-autopilot-install', '--non-interactive', '--require-db', '--skip-verify', '--source', '--yes'],
- 'auth': ['--admin-intent', '--allowed-operations', '--before', '--bound-brain', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--budget-usd-per-day', '--cleanup-dead-local-locks', '--confirm-quiesced', '--days', '--delegated-namespace', '--delegated-slug-prefixes', '--enable-dcr-insecure', '--expected-epoch', '--expected-state', '--federated-read', '--fix', '--force', '--grant-types', '--help', '--id', '--if-version', '--json', '--limit', '--manifest', '--max-concurrent', '--no-pull', '--path', '--probe', '--profile', '--redirect-uri', '--repair', '--replace', '--request-id', '--retry-failed', '--scopes', '--self-transfer', '--shared-skills', '--slug-prefixes', '--source', '--source-ids', '--surface', '--takes-holders', '--token', '--token-endpoint-auth-method', '--token-ttl', '--usage', '--yes'],
+ 'auth': ['--admin-intent', '--allowed-operations', '--before', '--bound-brain', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--budget-usd-per-day', '--cleanup-dead-local-locks', '--confirm-quiesced', '--days', '--delegated-namespace', '--delegated-slug-prefixes', '--dry-run', '--enable-dcr-insecure', '--expected-epoch', '--expected-state', '--federated-read', '--fix', '--force', '--grant-types', '--help', '--id', '--if-version', '--json', '--limit', '--manifest', '--max-concurrent', '--no-pull', '--path', '--probe', '--profile', '--redirect-uri', '--repair', '--replace', '--request-id', '--retry-failed', '--scopes', '--self-transfer', '--shared-skills', '--slug-prefixes', '--source', '--source-ids', '--surface', '--takes-holders', '--token', '--token-endpoint-auth-method', '--token-ttl', '--usage', '--yes'],
'autopilot': ['--batch', '--brain', '--check', '--dimensions', '--dir', '--drain', '--embedding-dimensions', '--embedding-model', '--explain', '--ff-only', '--force', '--force-retry', '--from-pages', '--help', '--inject-bootstrap', '--inline', '--install', '--interval', '--json', '--markdown', '--max-rss', '--max-usd', '--migrate-only', '--no-autopilot-install', '--no-inject', '--no-worker', '--non-interactive', '--now', '--once', '--output', '--path', '--pause', '--phase', '--reason', '--refresh-cache', '--remediation-plan', '--repo', '--resume', '--source', '--stale', '--status', '--swap-only', '--target', '--uninstall', '--user', '--version', '--yes'],
'backfill': ['--batch-size', '--brain', '--concurrency', '--dry-run', '--fresh', '--help', '--json', '--keep-index', '--list', '--max-errors', '--max-rows', '--resume', '--source'],
'backup': ['--background', '--brain', '--confirm-authority-reviewed', '--confirm-backup-compatible', '--confirm-quiesced', '--dir', '--explain', '--follow', '--help', '--into', '--json', '--mode', '--output', '--progress-interval', '--progress-json', '--quiet', '--restore-only', '--source', '--timeout'],
diff --git a/test/auth-local-writer-dry-run.test.ts b/test/auth-local-writer-dry-run.test.ts
new file mode 100644
index 0000000000..2ec3129b52
--- /dev/null
+++ b/test/auth-local-writer-dry-run.test.ts
@@ -0,0 +1,40 @@
+/**
+ * #5595 ("also seen"): `gbrain auth local-writer register cli --dry-run`
+ * failed with "unknown flag --dry-run for 'gbrain auth'" although --help
+ * documents it. auth hands that argv to runPersistenceAdminCli
+ * (persistence-admin.ts), one module deeper than the flag-registry scan
+ * takes consumption evidence from, so the generator dropped the safety flag.
+ * The same gap hid `auth rescope-client|rescope-token --dry-run`.
+ */
+import { afterAll, expect, test } from 'bun:test';
+import { spawnSync } from 'node:child_process';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { validateCommandFlags } from '../src/cli.ts';
+
+const home = mkdtempSync(join(tmpdir(), 'gbrain-local-writer-dry-run-'));
+afterAll(() => rmSync(home, { recursive: true, force: true }));
+const CLI = join(import.meta.dir, '..', 'src', 'cli.ts');
+function gbrain(args: string[]) {
+ const env: NodeJS.ProcessEnv = { ...process.env, GBRAIN_HOME: home };
+ for (const key of ['OPENAI_API_KEY', 'ANTHROPIC_API_KEY', 'VOYAGE_API_KEY', 'DATABASE_URL', 'GBRAIN_DATABASE_URL']) delete env[key];
+ return spawnSync(process.execPath, ['run', CLI, ...args], { env, encoding: 'utf8', timeout: 120_000 });
+}
+
+test('the validator accepts --dry-run on every auth subcommand that parses it', () => {
+ expect(validateCommandFlags('auth', ['local-writer', 'register', 'cli', '--dry-run'])).toBeNull();
+ expect(validateCommandFlags('auth', ['local-writer', 'revoke', '00000000-0000-4000-8000-000000000000', '--dry-run'])).toBeNull();
+ expect(validateCommandFlags('auth', ['rescope-client', 'client-example', '--source', 'default', '--dry-run'])).toBeNull();
+ expect(validateCommandFlags('auth', ['local-writer', 'register', 'cli', '--definitely-not-a-flag'])).toBe('--definitely-not-a-flag');
+});
+
+test('auth local-writer register --replace --dry-run previews the regrant and changes nothing', () => {
+ expect(gbrain(['init', '--pglite', '--non-interactive', '--no-embedding']).status).toBe(0);
+ const before = JSON.parse(gbrain(['auth', 'local-writer', 'list', '--json']).stdout);
+ const preview = gbrain(['auth', 'local-writer', 'register', 'cli', '--source-ids', 'default', '--replace', '--dry-run', '--json']);
+ expect(preview.stderr).not.toContain('unknown flag');
+ expect(preview.status).toBe(0);
+ expect(JSON.parse(preview.stdout)).toMatchObject({ dry_run: true, action: 'local_writer_register', lane: 'cli', replace: true, grant: { sourceIds: ['default'] } });
+ expect(JSON.parse(gbrain(['auth', 'local-writer', 'list', '--json']).stdout)).toEqual(before);
+}, 150_000);
diff --git a/test/docs-cli-commands.test.ts b/test/docs-cli-commands.test.ts
index 0768395f3c..c586586de3 100644
--- a/test/docs-cli-commands.test.ts
+++ b/test/docs-cli-commands.test.ts
@@ -60,16 +60,6 @@ const ALLOWLIST: Record<string, { tokens: string[]; reason: string }> = {
tokens: ['rls-exempt'],
reason: 'explains that gbrain deliberately does NOT ship this command',
},
- 'docs/guides/concurrent-writes.md': {
- tokens: ['--dry-run'],
- reason: 'CLI bug: `auth rescope-client` / `auth local-writer` parse --dry-run '
- + '(src/core/grants/cli.ts, src/commands/persistence-admin.ts) but '
- + "CLI_FLAG_REGISTRY['auth'] omits it, so the validator rejects it. Remove once the registry accepts it.",
- },
- 'docs/guides/shared-brain-skills.md': {
- tokens: ['--dry-run'],
- reason: 'same auth --dry-run registry bug as docs/guides/concurrent-writes.md',
- },
};
interface Violation { file: string; line: number; token: string; message: string }