Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(purge): forget --dry-run is registered with consumption evidence; a plain forget refuses it
garrytanb0352fd375a6
diff --git a/scripts/generate-flag-registry.ts b/scripts/generate-flag-registry.tsindex 6e292a1582..85c117eb04 100644--- a/scripts/generate-flag-registry.ts+++ b/scripts/generate-flag-registry.ts@@ -67,6 +67,9 @@ const DELEGATED_MODULES: Record<string, string[]> = {// 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'],+ // `forget --purge` (and forget --help) hand the argv to runForgetPurge, which+ // parses --dry-run itself; a plain forget refuses --dry-run (recall.ts).+ forget: ['src/commands/forget-purge.ts'],};/**diff --git a/src/commands/forget-purge.ts b/src/commands/forget-purge.tsindex b44c165725..7dd61f53fe 100644--- a/src/commands/forget-purge.ts+++ b/src/commands/forget-purge.ts@@ -90,6 +90,11 @@ export function renderPurgeReceipt(r: Record<string, unknown>): string {/** Test seam: the terminal probe and the typed-token reader (defaults: isInteractive and a stderr prompt). */export interface PurgeCliIo { interactive?: () => boolean; readToken?: (prompt: string) => Promise<string | null> }+/** `gbrain forget` argv this module handles: a purge, its help, and a --dry-run (which a plain forget refuses here, since it has none). */+export function routesToForgetPurge(args: readonly string[]): boolean {+ return args.includes('--purge') || args.includes('--dry-run') || args.includes('--help') || args.includes('-h');+}+export async function runForgetPurge(engine: BrainEngine | (() => Promise<BrainEngine>), args: string[], io: PurgeCliIo = {}): Promise<void> {const json = args.includes('--json');const { reportPersistenceCliError } = await import('./persistence-delegate.ts');@@ -98,6 +103,10 @@ export async function runForgetPurge(engine: BrainEngine | (() => Promise<BrainEconst out = (value: Record<string, unknown>, human: string) => process.stdout.write(json ? `${JSON.stringify(value, null, 2)}\n` : human);try {if (args.includes('--help') || args.includes('-h')) { process.stdout.write(USAGE); return; }+ if (!args.includes('--purge')) {+ throw opError('invalid_params', '--dry-run previews a purge only; a plain forget has no dry run, so nothing was forgotten.',+ 'Add --purge to preview a purge (gbrain forget <id> --purge --dry-run), or drop --dry-run to forget the fact.');+ }const idArg = args.find(a => /^\d+$/.test(a));const cfg = loadConfig();if (isThinClient(cfg)) throw purgeHostOnly(idArg);diff --git a/src/commands/recall.ts b/src/commands/recall.tsindex 72c13896fe..f5da8d4c61 100644--- a/src/commands/recall.ts+++ b/src/commands/recall.ts@@ -827,7 +827,8 @@ function factRowToJson(r: FactRow): Record<string, unknown> {}export async function runForget(engine: BrainEngine | (() => Promise<BrainEngine>), args: string[]): Promise<void> {- if (args.includes('--purge') || args.includes('--help') || args.includes('-h')) return (await import('./forget-purge.ts')).runForgetPurge(engine, args);+ const purge = await import('./forget-purge.ts');+ if (purge.routesToForgetPurge(args)) return purge.runForgetPurge(engine, args);const idArg = args.find(a => /^\d+$/.test(a));if (!idArg) {process.stderr.write('Usage: gbrain forget <fact-id> [--reason <text>] [--source <id>] [--request-id <uuid>] [--json]\n');diff --git a/src/core/cli-flag-registry.generated.ts b/src/core/cli-flag-registry.generated.tsindex 801ab9725a..3857fdbcf1 100644--- a/src/core/cli-flag-registry.generated.ts+++ b/src/core/cli-flag-registry.generated.ts@@ -64,7 +64,7 @@ export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {'features': ['--all', '--apply', '--auto', '--auto-fix', '--background', '--batch-size', '--brain', '--by-mention', '--catch-up', '--concurrency', '--dir', '--embedding-model', '--expect', '--explain', '--facts', '--force', '--from-meetings', '--help', '--images', '--include-frontmatter', '--include-null-signature', '--infer-dates', '--json', '--kind', '--max-cost', '--max-usd', '--ner', '--non-interactive', '--only', '--overwrite', '--pace', '--pace-max-concurrency', '--pack', '--page', '--path', '--priority', '--probe', '--prune-orphans', '--rebuild', '--refresh', '--repo', '--save', '--since', '--slugs', '--source', '--source-id', '--stale', '--target', '--trust', '--type', '--verbose', '--workers', '--yes'],'feedback': ['--brain', '--help', '--json', '--page', '--source'],'files': ['--accept-pending', '--background', '--brain', '--dry-run', '--explain', '--follow', '--help', '--json', '--no-accept-pending', '--no-pointer', '--page', '--progress-interval', '--progress-json', '--quiet', '--retry-failed', '--source', '--timeout', '--type', '--wait', '--yes'],- 'forget': ['--all-subjects', '--brain', '--help', '--json', '--match', '--purge', '--reason', '--request-id', '--source', '--status', '--vacuum', '--yes'],+ 'forget': ['--all-subjects', '--brain', '--dry-run', '--help', '--json', '--match', '--purge', '--reason', '--request-id', '--source', '--status', '--vacuum', '--yes'],'founder': ['--brain', '--help', '--json', '--mcp-only', '--since', '--source', '--until'],'friction': ['--agent', '--base', '--brain', '--compare', '--help', '--hint', '--json', '--kind', '--message', '--no-redact', '--phase', '--run-id', '--severity', '--source', '--transcript-path', '--transcripts'],'frontmatter': ['--allow-catch-all', '--batch', '--brain', '--cached', '--diff-filter', '--dry-run', '--exclude-standard', '--fix', '--force', '--full-name', '--get', '--git-common-dir', '--help', '--importable', '--include-ambiguous', '--include-catch-all', '--json', '--literal-pathspecs', '--name-only', '--no-verify', '--others', '--path', '--show-prefix', '--show-toplevel', '--source', '--stage', '--staged', '--stdin', '--type', '--uninstall'],diff --git a/test/forget-purge-cli.test.ts b/test/forget-purge-cli.test.tsindex 46e4c26511..afb22be680 100644--- a/test/forget-purge-cli.test.ts+++ b/test/forget-purge-cli.test.ts@@ -14,7 +14,7 @@ import { PGLiteEngine } from '../src/core/pglite-engine.ts';import { importFromContent } from '../src/core/import-file.ts';import { runExtractFacts } from '../src/core/cycle/extract-facts.ts';import { registerLocalWriter } from '../src/core/persistence/identity.ts';-import { runForgetPurge } from '../src/commands/forget-purge.ts';+import { routesToForgetPurge, runForgetPurge } from '../src/commands/forget-purge.ts';import { _resetCliExitVerdictForTests, currentExitCode } from '../src/core/cli-force-exit.ts';import { withEnv } from './helpers/with-env.ts';@@ -85,4 +85,12 @@ describe('gbrain forget --purge', () => {expect(dry.out).toContain('Purge dry run');expect(await engine.executeRaw('SELECT 1 FROM facts WHERE id=$1', [factId])).toHaveLength(1);}, 60_000);++ test('a plain forget with --dry-run is refused before anything changes (a dry run exists only for --purge)', async () => {+ expect(routesToForgetPurge([String(factId), '--dry-run'])).toBe(true);+ const refused = await capture(() => runForgetPurge(engine, [String(factId), '--dry-run', '--json'], { interactive: () => false }));+ expect(refused.code).not.toBe(0);+ const [row] = await engine.executeRaw<{ expired_at: unknown }>('SELECT expired_at FROM facts WHERE id=$1', [factId]);+ expect(row?.expired_at).toBeNull();+ }, 60_000);});