Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(recall): refuse a malformed --limit instead of silently using another value

masashiono06119a8ce8902ef0
diff --git a/src/commands/recall.ts b/src/commands/recall.ts
index 831da37e78..aad1a80a57 100644
--- a/src/commands/recall.ts
+++ b/src/commands/recall.ts
@@ -125,7 +125,14 @@ function parseFlags(args: string[]): ParsedFlags {
if (a === '--source') { out.source = args[++i] ?? 'default'; out.sourceExplicit = true; continue; }
if (a === '--source-id') { out.source = args[++i] ?? ''; out.sourceExplicit = true; continue; }
if (a.startsWith('--source-id=')) { out.source = a.slice('--source-id='.length); out.sourceExplicit = true; continue; }
- if (a === '--limit') { out.limit = parseInt(args[++i] ?? '50', 10) || 50; continue; }
+ if (a === '--limit') {
+ const raw = args[++i] ?? '';
+ if (!/^\d+$/.test(raw) || !Number.isSafeInteger(Number(raw)) || Number(raw) < 1) {
+ process.stderr.write(`Error: --limit must be a positive safe integer (got "${raw}").\n`);
+ process.exit(2);
+ }
+ out.limit = Number(raw); continue;
+ }
if (a === '--query') { out.query = args[++i] ?? null; continue; }
if (a === '--budget-tokens') { rawBudget = args[++i]; continue; }
if (a === '--budget-policy') {
diff --git a/test/recall-grep-local-cli.test.ts b/test/recall-grep-local-cli.test.ts
index 5656cc4911..16f9a963f7 100644
--- a/test/recall-grep-local-cli.test.ts
+++ b/test/recall-grep-local-cli.test.ts
@@ -65,6 +65,31 @@ beforeEach(() => {
});
describe('gbrain#5607 — local recall --grep filters in SQL before LIMIT', () => {
+ test('rejects malformed --limit values and accepts a positive integer', async () => {
+ const originalExit = process.exit;
+ const originalStderr = process.stderr.write;
+ const refused: string[] = [];
+ process.exit = ((code?: number) => { refused.push(String(code)); throw new Error('exit intercepted'); }) as typeof process.exit;
+ process.stderr.write = ((chunk: string | Uint8Array) => { captured += typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString(); return true; }) as typeof process.stderr.write;
+ try {
+ for (const value of ['3x', 'abc', '0']) {
+ let exited = false;
+ try { await runRecall(engine, ['--limit', value]); } catch (error) { exited = (error as Error).message === 'exit intercepted'; }
+ expect(exited).toBe(true);
+ expect(refused.at(-1)).toBe('2');
+ expect(captured).toContain(`got "${value}"`);
+ captured = '';
+ }
+ } finally {
+ process.exit = originalExit;
+ process.stderr.write = originalStderr;
+ }
+ const needle = await seed({ entity: 'g5607-limit-valid' });
+ const out = await recallJson(['--grep', needle, '--limit', '3']);
+ expect(out.facts.map(f => f.fact)).toEqual([needle]);
+ await engine.executeRaw(`DELETE FROM facts WHERE entity_slug='g5607-limit-valid'`);
+ });
+
test('needle older than the newest-N window is still found (no-filter arm)', async () => {
const needle = await seed({ entity: 'g5607-a' });
const out = await recallJson(['--grep', 'g5607 needle', '--limit', String(LIMIT)]);