From c7e182e4f494dae31d84852f51802d79359974c6 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 05:11:20 +0000 Subject: [PATCH] fix(insight): reject a non-positive --hours instead of reporting all-time cost `pr-insight cost --hours abc` turned into NaN, which cost.ts treats as "no window", so all-time totals were printed as if they were the last N hours; 0 behaved the same and negative values silently returned nothing. The CLI now exits 2 with a usage message, like its other usage errors. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Cn6JQLtmA8NV75CtdV373W --- ts/src/cli/insight.ts | 3 ++- ts/tests/insight.test.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ts/src/cli/insight.ts b/ts/src/cli/insight.ts index d2ddf5f4..473fd3b3 100644 --- a/ts/src/cli/insight.ts +++ b/ts/src/cli/insight.ts @@ -9,7 +9,8 @@ const { values, positionals } = parseArgs({ allowPositionals: true, options: { p const cmd = positionals[0]; const out = (o: unknown) => console.log(JSON.stringify(o, (_, v) => v, 2)); if (cmd === "cost") { - const log = process.env.AI_USAGE_LOG ?? "", hours = values.hours ? Number(values.hours) : undefined; + const log = process.env.AI_USAGE_LOG ?? "", hours = values.hours === undefined ? undefined : Number(values.hours); + if (hours !== undefined && !(Number.isFinite(hours) && hours > 0)) { console.error("--hours must be a positive number"); process.exit(2); } const rows = summarize(log, hours), skills = summarizeSkills(log, hours), tokenix = summarizeTokenix(log, hours); if (values.json) out({ cost: rows, skills, tokenix }); else { diff --git a/ts/tests/insight.test.ts b/ts/tests/insight.test.ts index c7aed245..ac862fec 100644 --- a/ts/tests/insight.test.ts +++ b/ts/tests/insight.test.ts @@ -16,3 +16,15 @@ test("explain: invalid ref, unavailable PR, live decision", async () => { expect(r2.pipeline_state).toBe("success"); expect(r2.reasons).toContain("merge_conflicts"); expect(r2.reasons).toContain("autonomy_mode:observe"); expect(r2.decision).toBe("blocked"); expect(jobHistory(s, "o/r#1")).toEqual([]); }); + +// NaN or a non-positive window used to be read as "no window" and printed all-time totals as if they were the last N hours. +test("pr-insight cost rejects a --hours value that is not a positive number", () => { + const cli = join(import.meta.dir, "../src/cli/insight.ts"); + const run = (h: string) => Bun.spawnSync(["bun", cli, "cost", `--hours=${h}`], { env: { ...process.env, AI_USAGE_LOG: "/nonexistent" } }); + for (const bad of ["abc", "0", "-5"]) { + const r = run(bad); + expect(r.exitCode).toBe(2); + expect(r.stderr.toString()).toContain("--hours"); + } + expect(run("24").exitCode).toBe(0); +});