Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(book-mirror): validate --max-turns and --timeout-ms (#5909)

garrytan8341bb063baf
diff --git a/scripts/test-weights.json b/scripts/test-weights.json
index f2d0e9d524..81658e908c 100644
--- a/scripts/test-weights.json
+++ b/scripts/test-weights.json
@@ -153,6 +153,7 @@
"test/bench/correctness-gate.test.ts": 19,
"test/bench/qrels-file.test.ts": 5,
"test/binary-self-update.test.ts": 16,
+ "test/book-mirror-numeric-flags.test.ts": 1000,
"test/book-mirror.test.ts": 618,
"test/bootstrap-atomic-write.test.ts": 12,
"test/bootstrap-codex-door.test.ts": 6,
diff --git a/scripts/ubicloud/weights.json b/scripts/ubicloud/weights.json
index 51730a2ffe..5f4568c34e 100644
--- a/scripts/ubicloud/weights.json
+++ b/scripts/ubicloud/weights.json
@@ -772,6 +772,7 @@
"unit:test/bench/correctness-gate.test.ts": 248,
"unit:test/bench/qrels-file.test.ts": 153,
"unit:test/binary-self-update.test.ts": 167,
+ "unit:test/book-mirror-numeric-flags.test.ts": 1000,
"unit:test/book-mirror.test.ts": 1113,
"unit:test/bootstrap-atomic-write.test.ts": 235,
"unit:test/bootstrap-codex-door.test.ts": 182,
diff --git a/src/commands/book-mirror.ts b/src/commands/book-mirror.ts
index 97aaf11f74..f7b32f0a44 100644
--- a/src/commands/book-mirror.ts
+++ b/src/commands/book-mirror.ts
@@ -51,6 +51,7 @@ import type { MinionJobInput, SubagentHandlerData } from '../core/minions/types.
import { operations } from '../core/operations.ts';
import { loadConfig } from '../core/config.ts';
import { getCliOptions } from '../core/cli-options.ts';
+import { intFlagValue } from '../cli/flag-values.ts';
const COST_PER_CHAPTER_OPUS = 0.30; // rough; depends on chapter length
const COST_PER_CHAPTER_SONNET = 0.06;
@@ -102,8 +103,11 @@ function parseFlags(args: string[]): BookMirrorFlags {
const title = parseFlag(args, '--title');
const author = parseFlag(args, '--author');
const model = parseFlag(args, '--model') ?? 'claude-opus-4-7';
- const maxTurnsStr = parseFlag(args, '--max-turns');
- const timeoutMsStr = parseFlag(args, '--timeout-ms');
+ // #5909 (D4): a present flag needs a positive safe integer (usage error, exit 2), never a parseInt NaN.
+ const positiveInt = (flag: string, example: number) =>
+ hasFlag(args, flag) ? intFlagValue(parseFlag(args, flag), flag, { min: 1, example }) : undefined;
+ const maxTurns = positiveInt('--max-turns', DEFAULT_MAX_TURNS);
+ const timeoutMs = positiveInt('--timeout-ms', 600000);
return {
chaptersDir,
@@ -112,8 +116,8 @@ function parseFlags(args: string[]): BookMirrorFlags {
title,
author,
model,
- maxTurns: maxTurnsStr ? parseInt(maxTurnsStr, 10) : DEFAULT_MAX_TURNS,
- timeoutMs: timeoutMsStr ? parseInt(timeoutMsStr, 10) : undefined,
+ maxTurns: maxTurns ?? DEFAULT_MAX_TURNS,
+ timeoutMs,
noConfirm: hasFlag(args, '--no-confirm') || hasFlag(args, '--yes'),
follow: process.stdout.isTTY === true && !hasFlag(args, '--no-follow'),
dryRun: hasFlag(args, '--dry-run'),
diff --git a/test/book-mirror-numeric-flags.test.ts b/test/book-mirror-numeric-flags.test.ts
new file mode 100644
index 0000000000..c3981c96c6
--- /dev/null
+++ b/test/book-mirror-numeric-flags.test.ts
@@ -0,0 +1,44 @@
+// #5909 (absorbed in agent contract v1 D4): bad values are invalid_params usage errors (exit 2 via renderCliError).
+import { afterAll, beforeAll, expect, test } from 'bun:test';
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { join } from 'node:path';
+import { runBookMirrorCmd } from '../src/commands/book-mirror.ts';
+import type { BrainEngine } from '../src/core/engine.ts';
+
+let chaptersDir: string;
+let engineReads = 0;
+const noSubmitEngine = new Proxy({} as BrainEngine, {
+ get() {
+ engineReads++;
+ throw new Error('engine access would submit a child job');
+ },
+});
+
+beforeAll(() => {
+ chaptersDir = mkdtempSync(join(import.meta.dir, '.book-mirror-numeric-'));
+ mkdirSync(chaptersDir, { recursive: true });
+ writeFileSync(join(chaptersDir, 'chapter-1.txt'), 'Synthetic chapter text.');
+});
+afterAll(() => rmSync(chaptersDir, { recursive: true, force: true }));
+
+function args(flag: string, value?: string): string[] {
+ return ['--chapters-dir', chaptersDir, '--slug', 'synthetic-book', '--dry-run', flag, ...(value === undefined ? [] : [value])];
+}
+
+for (const flag of ['--max-turns', '--timeout-ms']) {
+ for (const value of ['abc', '12abc', '0', '-2', '9007199254740992', undefined]) {
+ test(`${flag} rejects ${value ?? 'missing value'} before engine access`, async () => {
+ const before = engineReads;
+ await expect(runBookMirrorCmd(noSubmitEngine, args(flag, value))).rejects.toMatchObject({ code: 'invalid_params', message: expect.stringContaining(flag) });
+ expect(engineReads).toBe(before);
+ });
+ }
+
+ for (const value of ['5', '05', ' 5 ']) {
+ test(`${flag} accepts ${JSON.stringify(value)} without engine access in dry run`, async () => {
+ const before = engineReads;
+ await expect(runBookMirrorCmd(noSubmitEngine, args(flag, value))).resolves.toBeUndefined();
+ expect(engineReads).toBe(before);
+ });
+ }
+}