Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(extract-conversation-facts): reject invalid --limit, --segment-limit and --sleep values (#5934)

garrytane548b7483e66
diff --git a/scripts/test-weights.json b/scripts/test-weights.json
index 67236bccb4..e7e1f0e9ed 100644
--- a/scripts/test-weights.json
+++ b/scripts/test-weights.json
@@ -751,6 +751,7 @@
"test/extract-by-mention-resume.test.ts": 323,
"test/extract-by-mention.test.ts": 485,
"test/extract-conversation-facts-entity-resolution.test.ts": 769,
+ "test/extract-conversation-facts-flags.test.ts": 2000,
"test/extract-conversation-facts.test.ts": 3681,
"test/extract-cross-source-links.test.ts": 4,
"test/extract-cross-source-resolution-observability.test.ts": 6,
diff --git a/scripts/ubicloud/weights.json b/scripts/ubicloud/weights.json
index 52ba2ad6ee..f53d3a8d7a 100644
--- a/scripts/ubicloud/weights.json
+++ b/scripts/ubicloud/weights.json
@@ -1336,6 +1336,7 @@
"unit:test/extract-by-mention-resume.test.ts": 1897,
"unit:test/extract-by-mention.test.ts": 1721,
"unit:test/extract-conversation-facts-entity-resolution.test.ts": 2323,
+ "unit:test/extract-conversation-facts-flags.test.ts": 2000,
"unit:test/extract-conversation-facts.test.ts": 10046,
"unit:test/extract-cross-source-links.test.ts": 333,
"unit:test/extract-cross-source-resolution-observability.test.ts": 208,
diff --git a/src/commands/extract-conversation-facts.ts b/src/commands/extract-conversation-facts.ts
index 989885366b..11ff404098 100644
--- a/src/commands/extract-conversation-facts.ts
+++ b/src/commands/extract-conversation-facts.ts
@@ -382,6 +382,10 @@ import { readConversationBodyForParsing } from '../core/conversation-parser/body
import { runLlmFallback } from '../core/conversation-parser/llm-fallback.ts';
import { resolveModel, resolveTierDefault } from '../core/model-config.ts';
import { FAILED_EXIT_CODE } from '../core/exit-codes.ts';
+import { usageError } from '../cli/cli-error.ts';
+import { intFlagValue } from '../cli/flag-values.ts';
+
+const ECF_HELP_HINT = 'Run `gbrain extract-conversation-facts --help` for the accepted flags and examples.';
/**
* v0.41.13.0 — back-compat shape for direct callers + the existing
@@ -1797,21 +1801,10 @@ function parseArgs(args: string[]): ParsedArgs {
out.types = parts as AllowedType[];
continue;
}
- if (a === '--limit') {
- const n = parseInt(args[++i] ?? '', 10);
- if (Number.isFinite(n) && n > 0) out.limit = n;
- continue;
- }
- if (a === '--sleep') {
- const n = parseInt(args[++i] ?? '', 10);
- if (Number.isFinite(n) && n >= 0) out.sleepMs = n;
- continue;
- }
- if (a === '--segment-limit') {
- const n = parseInt(args[++i] ?? '', 10);
- if (Number.isFinite(n) && n >= 0) out.segmentLimit = n;
- continue;
- }
+ // #5934 (D4): strict values; a bad one is a usage error (exit 2), never silently ignored.
+ if (a === '--limit') { out.limit = intFlagValue(args[++i], '--limit', { min: 1, example: 100 }); continue; }
+ if (a === '--sleep') { out.sleepMs = intFlagValue(args[++i], '--sleep', { min: 0, example: 500 }); continue; }
+ if (a === '--segment-limit') { out.segmentLimit = intFlagValue(args[++i], '--segment-limit', { min: 0, example: 50 }); continue; }
if (a === '--max-cost-usd') {
const n = Number(args[++i]);
if (!Number.isFinite(n) || n <= 0) {
@@ -1895,7 +1888,7 @@ conversation_facts_backlog check counts pages without this row.
function buildJobParams(args: string[]): Record<string, unknown> {
const parsed = parseArgs(args);
- if (parsed.error) throw new Error(parsed.error);
+ if (parsed.error) throw usageError(parsed.error, ECF_HELP_HINT);
return {
sourceId: parsed.sourceId,
types: parsed.types,
@@ -1936,11 +1929,7 @@ export async function runExtractConversationFacts(
if (backgrounded) return;
const parsed = parseArgs(args);
- if (parsed.error) {
- console.error(parsed.error);
- console.error(HELP);
- process.exit(1);
- }
+ if (parsed.error) throw usageError(parsed.error, ECF_HELP_HINT);
// Chat gateway is required for non-dry-run. Recover a cold singleton before
// reporting an availability error (#2590).
diff --git a/test/extract-conversation-facts-diagnostics.serial.test.ts b/test/extract-conversation-facts-diagnostics.serial.test.ts
index 503774b7df..ebf0937cd4 100644
--- a/test/extract-conversation-facts-diagnostics.serial.test.ts
+++ b/test/extract-conversation-facts-diagnostics.serial.test.ts
@@ -92,18 +92,12 @@ describe('#5364 diagnostics across workers, sources, CLI, and cycle', () => {
});
test.each(['0', '-1', 'invalid', '1junk', 'Infinity', undefined])('invalid explicit cap %s is rejected before extraction or background submission', async value => {
- const error = spyOn(console, 'error').mockImplementation(() => {});
- const exit = spyOn(process, 'exit').mockImplementation(((code: number) => { throw new Error(`exit:${code}`); }) as never);
+ // Agent contract v1 D4: a usage error (invalid_params, exit 2 via renderCliError), not print + exit(1).
const args = ['--source-id', 'speaker-a', '--max-cost-usd', ...(value === undefined ? [] : [value])];
- try {
- await expect(runExtractConversationFacts(engine, args)).rejects.toThrow('exit:1');
- await expect(runExtractConversationFacts(engine, ['--background', ...args])).rejects.toThrow('--max-cost-usd requires a positive finite number');
- expect(error.mock.calls[0]?.[0]).toContain('--max-cost-usd requires a positive finite number');
- expect(calls).toBe(0);
- } finally {
- exit.mockRestore();
- error.mockRestore();
- }
+ await expect(runExtractConversationFacts(engine, args))
+ .rejects.toMatchObject({ code: 'invalid_params', message: expect.stringContaining('--max-cost-usd requires a positive finite number') });
+ await expect(runExtractConversationFacts(engine, ['--background', ...args])).rejects.toThrow('--max-cost-usd requires a positive finite number');
+ expect(calls).toBe(0);
});
test('dry-run help promises segmentation without model calls', async () => {
diff --git a/test/extract-conversation-facts-flags.test.ts b/test/extract-conversation-facts-flags.test.ts
new file mode 100644
index 0000000000..fc3bbf3c89
--- /dev/null
+++ b/test/extract-conversation-facts-flags.test.ts
@@ -0,0 +1,59 @@
+/**
+ * #5934 (absorbed in agent contract v1 D4): extract-conversation-facts
+ * rejects malformed --limit / --segment-limit / --sleep values with an
+ * invalid_params usage error (exit 2 via renderCliError) before any work.
+ */
+import { afterAll, beforeAll, describe, expect, spyOn, test } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { runExtractConversationFacts } from '../src/commands/extract-conversation-facts.ts';
+
+let engine: PGLiteEngine;
+
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+});
+
+afterAll(async () => {
+ await engine.disconnect();
+});
+
+describe('extract-conversation-facts numeric flags', () => {
+ test.each([
+ ['--limit', 'foo', 'a positive integer'],
+ ['--limit', '0', 'a positive integer'],
+ ['--limit', '-1', 'a positive integer'],
+ ['--limit', '1.5', 'a positive integer'],
+ ['--limit', '1junk', 'a positive integer'],
+ ['--limit', '9007199254740992', 'a positive integer'],
+ ['--segment-limit', 'foo', 'a non-negative integer'],
+ ['--segment-limit', '-1', 'a non-negative integer'],
+ ['--segment-limit', '1.5', 'a non-negative integer'],
+ ['--segment-limit', '1junk', 'a non-negative integer'],
+ ['--segment-limit', '9007199254740992', 'a non-negative integer'],
+ ['--sleep', 'foo', 'a non-negative integer'],
+ ['--sleep', '-1', 'a non-negative integer'],
+ ['--sleep', '1.5', 'a non-negative integer'],
+ ['--sleep', '1junk', 'a non-negative integer'],
+ ['--sleep', '9007199254740992', 'a non-negative integer'],
+ ])('%s %s is rejected before dry-run work', async (flag, value, expected) => {
+ await expect(runExtractConversationFacts(engine, ['--dry-run', '--source-id', 'default', flag, value]))
+ .rejects.toMatchObject({ code: 'invalid_params', message: expect.stringContaining(`${flag} must be ${expected}`) });
+ });
+
+ test.each(['--limit', '--segment-limit', '--sleep'])('%s without a value is rejected', async (flag) => {
+ await expect(runExtractConversationFacts(engine, ['--dry-run', '--source-id', 'default', flag]))
+ .rejects.toMatchObject({ code: 'invalid_params', message: expect.stringContaining(`${flag} requires a value`) });
+ });
+
+ test('zero remains valid only for segment-limit and sleep', async () => {
+ const log = spyOn(console, 'log').mockImplementation(() => {});
+ try {
+ await runExtractConversationFacts(engine, ['--dry-run', '--source-id', 'default', '--segment-limit', '0', '--sleep', '0']);
+ expect(log.mock.calls.map(call => call.join(' ')).join('\n')).toContain('Done: (dry run)');
+ } finally {
+ log.mockRestore();
+ }
+ });
+});