Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(edges-backfill): reject invalid --max-chunks values (#5896)

garrytane704f741ca66
diff --git a/src/commands/edges-backfill.ts b/src/commands/edges-backfill.ts
index 166f3a556b..39b4676590 100644
--- a/src/commands/edges-backfill.ts
+++ b/src/commands/edges-backfill.ts
@@ -40,7 +40,12 @@ function parseFlags(args: string[]): BackfillOpts {
} else if (a === '--all-sources') {
opts.allSources = true;
} else if (a === '--max-chunks') {
- opts.maxChunks = parseInt(args[++i] ?? '', 10);
+ const raw = args[++i];
+ const n = Number(raw);
+ if (!raw || !/^\d+$/.test(raw.trim()) || !Number.isSafeInteger(n) || n < 1) {
+ throw new Error('--max-chunks must be a positive safe integer');
+ }
+ opts.maxChunks = n;
} else if (a === '--json') {
opts.json = true;
} else if (a === '--workers' || a === '--concurrency') {
diff --git a/test/edges-backfill-max-chunks.test.ts b/test/edges-backfill-max-chunks.test.ts
new file mode 100644
index 0000000000..5cedff3172
--- /dev/null
+++ b/test/edges-backfill-max-chunks.test.ts
@@ -0,0 +1,37 @@
+import { expect, test } from 'bun:test';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { runEdgesBackfill } from '../src/commands/edges-backfill.ts';
+
+function emptyEngine() {
+ let dbCalls = 0;
+ const engine = {
+ kind: 'pglite',
+ executeRaw: async () => { dbCalls++; return []; },
+ transaction: async (work: (tx: unknown) => Promise<unknown>) => {
+ dbCalls++;
+ return work({ executeRaw: async () => { dbCalls++; return []; } });
+ },
+ } as unknown as BrainEngine;
+ return { engine, dbCalls: () => dbCalls };
+}
+
+for (const raw of ['abc', '12abc', '0', '-1', undefined]) {
+ test(`edges-backfill rejects invalid --max-chunks ${raw ?? '(missing)'} before DB work`, async () => {
+ const { engine, dbCalls } = emptyEngine();
+ const args = ['--source', 'default', '--json', '--max-chunks', ...(raw === undefined ? [] : [raw])];
+ await expect(runEdgesBackfill(engine, args)).rejects.toThrow('--max-chunks');
+ expect(dbCalls()).toBe(0);
+ });
+}
+
+test('edges-backfill accepts --max-chunks 5 and runs the resolver', async () => {
+ const { engine, dbCalls } = emptyEngine();
+ await runEdgesBackfill(engine, ['--source', 'default', '--max-chunks', '5']);
+ expect(dbCalls()).toBeGreaterThan(0);
+});
+
+test('edges-backfill accepts a leading-zero --max-chunks 05 as 5', async () => {
+ const { engine, dbCalls } = emptyEngine();
+ await runEdgesBackfill(engine, ['--source', 'default', '--max-chunks', '05']);
+ expect(dbCalls()).toBeGreaterThan(0);
+});