Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(reindex): apply the --limit validation to --multimodal and --aliases (#5937)
garrytan4767cb7e441e
diff --git a/src/cli/commands/reindex.ts b/src/cli/commands/reindex.tsindex 8c5bea55c4..d4b9a2a152 100644--- a/src/cli/commands/reindex.ts+++ b/src/cli/commands/reindex.ts@@ -5,12 +5,19 @@* src/cli/command-table.ts.*/import { setCliExitVerdict } from '../../core/cli-force-exit.ts';+import { usageError, writeCliError } from '../cli-error.ts';import type { BrainEngine } from '../../core/engine.ts';export async function run(engine: BrainEngine, args: string[]): Promise<void> {const reindex = await import('../../commands/reindex.ts'); args = reindex.normalizeReindexArgs(args);const scopeError = reindex.validateReindexModeScope(args);- if (scopeError) { process.stderr.write(`[reindex] ${scopeError}\n`); setCliExitVerdict(2); return; }+ // Agent contract v1 D1/D4: a scope/value error is a usage error (exit 2) rendered by renderCliError;+ // the --limit refusal keeps its legacy `--json` key (`error: "invalid --limit: …"`).+ if (scopeError) {+ const legacy = scopeError.startsWith('invalid --limit') ? { legacy: { error: scopeError } } : {};+ setCliExitVerdict(writeCliError(usageError(`[reindex] ${scopeError}`, 'Run `gbrain reindex --help` for the modes and their flags, e.g. gbrain reindex --multimodal --limit 100.'), 'reindex', legacy));+ return;+ }if (args.includes('--multimodal')) {const { runReindexMultimodal } = await import('../../commands/reindex-multimodal.ts');const { parseWorkers } = await import('../../core/sync-concurrency.ts');diff --git a/src/commands/reindex.ts b/src/commands/reindex.tsindex b1155ccf6a..aae3ef7895 100644--- a/src/commands/reindex.ts+++ b/src/commands/reindex.ts@@ -147,6 +147,8 @@ function pendingDriftPredicate(noEmbed: boolean): string {export function validateReindexModeScope(args: string[]): string | null {args = normalizeReindexArgs(args);+ // #5937: --limit is validated for every mode (--multimodal/--aliases parsed it with parseInt).+ if (invalidPositiveIntegerFlag(args, '--limit')) return 'invalid --limit: expected a positive integer';if (!args.includes('--type')) return null;if (args.includes('--multimodal')) return '--type is only supported with reindex --markdown, not --multimodal';if (args.includes('--aliases')) return '--type is only supported with reindex --markdown, not --aliases';@@ -157,7 +159,8 @@ function invalidPositiveIntegerFlag(args: string[], flag: string): boolean {return args.some((arg, index) => {if (arg !== flag) return false;const raw = args[index + 1];- return raw == null || raw.startsWith('--') || !/^\d+$/.test(raw) || Number(raw) <= 0;+ return raw == null || raw.startsWith('--') || !/^\d+$/.test(raw) ||+ !Number.isSafeInteger(Number(raw)) || Number(raw) <= 0;});}diff --git a/test/reindex.test.ts b/test/reindex.test.tsindex 3d4eb285c0..84167afbec 100644--- a/test/reindex.test.ts+++ b/test/reindex.test.ts@@ -79,6 +79,18 @@ async function captureOutput<T>(fn: () => Promise<T>): Promise<{ result: T; stdo}describe('gbrain reindex --markdown (v0.32.7)', () => {+ // #5937 (absorbed in agent contract v1 D4)+ test('rejects invalid --limit values before dispatching any reindex mode', () => {+ for (const value of ['NaN', '0', '-1', '1.5', '1oops', '9007199254740992']) {+ expect(validateReindexModeScope(['--multimodal', '--limit', value]), `multimodal ${value}`)+ .toBe('invalid --limit: expected a positive integer');+ expect(validateReindexModeScope(['--aliases', `--limit=${value}`]), `aliases ${value}`)+ .toBe('invalid --limit: expected a positive integer');+ }+ expect(validateReindexModeScope(['--multimodal', '--limit', '3'])).toBeNull();+ expect(validateReindexModeScope(['--aliases', '--limit=3'])).toBeNull();+ });+test('dry-run reports pending count and does not write', async () => {await seedLegacyPage('note-a', 'body a');await seedLegacyPage('note-b', 'body b');