diff --git a/pm-stack/opencode-usage-export/opencode-usage-export.mjs b/pm-stack/opencode-usage-export/opencode-usage-export.mjs index 32935bf434..9ef4d54616 100755 --- a/pm-stack/opencode-usage-export/opencode-usage-export.mjs +++ b/pm-stack/opencode-usage-export/opencode-usage-export.mjs @@ -57,6 +57,7 @@ * OpenCode has been down past the window). Exit 4. * schema-drift a candidate DB lacks the v2 tables. Exit 5. * read-failed any other read error. Exit 6. + * invalid-argument numeric option is not positive and finite. Exit 2. */ import { appendFile, mkdir, readFile, rename, writeFile } from "node:fs/promises"; import { existsSync } from "node:fs"; @@ -314,13 +315,23 @@ function parseArgs(argv) { const a = argv[i]; if (a === "--db") args.db = argv[++i]; else if (a === "--out-dir") args.outDir = argv[++i]; - else if (a === "--window-hours") args.windowHours = Number(argv[++i]); - else if (a === "--fresh-hours") args.freshHours = Number(argv[++i]); - else throw new Error(`unknown argument: ${a}`); + else if (a === "--window-hours" || a === "--fresh-hours") { + const value = argv[i + 1]; + const hours = Number(value?.startsWith("--") ? undefined : value); + if (a === "--window-hours") args.windowHours = hours; + else args.freshHours = hours; + if (value != null && !value.startsWith("--")) i += 1; + } else throw new Error(`unknown argument: ${a}`); } return args; } +function requirePositiveFinite(value, option) { + if (!Number.isFinite(value) || value <= 0) { + throw new ExportError("invalid-argument", 2, `${option} must be a positive finite number`); + } +} + /** One exporter tick. Injectable `now`/`home` keep the tests offline * and deterministic; production uses the defaults. */ export async function run({ @@ -333,6 +344,8 @@ export async function run({ const outDir = args.outDir ?? join(home, "atlas/opencode-usage-export"); const attemptedAt = new Date(now).toISOString(); try { + requirePositiveFinite(args.windowHours, "--window-hours"); + requirePositiveFinite(args.freshHours, "--fresh-hours"); const explicit = args.db ?? env.OPENCODE_USAGE_DB ?? null; const candidates = explicit ? [explicit] : defaultCandidatePaths(home); const state = await readState(outDir); diff --git a/pm-stack/opencode-usage-export/opencode-usage-export.test.mjs b/pm-stack/opencode-usage-export/opencode-usage-export.test.mjs index 8dfe251632..a27f299e2c 100644 --- a/pm-stack/opencode-usage-export/opencode-usage-export.test.mjs +++ b/pm-stack/opencode-usage-export/opencode-usage-export.test.mjs @@ -75,6 +75,52 @@ async function fakeHome() { const defaultDb = (home) => join(home, ".local/share/opencode/opencode.db"); const chamberDb = (home) => join(home, ".local/share/openchamber/opencode.db"); +async function assertInvalidHoursOption(option, valueArgs) { + const { home, outDir } = await fakeHome(); + const { exitCode, doc } = await run({ + home, + now: NOW, + argv: [...valueArgs, "--out-dir", outDir], + }); + assert.notEqual(exitCode, 0); + assert.equal(doc.status, "invalid-argument"); + assert.match(doc.detail, new RegExp(option)); + assert.equal(doc.sessions, undefined); + const latest = JSON.parse(await readFile(join(outDir, "latest.json"), "utf8")); + assert.equal(latest.status, "invalid-argument"); + assert.match(latest.detail, new RegExp(option)); +} + +for (const option of ["--window-hours", "--fresh-hours"]) { + for (const value of ["banana", "NaN", "Infinity", "-Infinity", "0", "-1"]) { + test(`${option} rejects invalid value ${value} before reading a store`, async () => { + await assertInvalidHoursOption(option, [option, value]); + }); + } + test(`${option} rejects a missing value before the next option`, async () => { + await assertInvalidHoursOption(option, [option]); + }); +} + +test("positive finite hour options preserve normal export behavior", async () => { + const { home, outDir } = await fakeHome(); + await makeStore(defaultDb(home), { + sessions: [ + { id: "ses_recent" }, + { id: "ses_old", updated: NOW - 48 * HOUR, created: NOW - 49 * HOUR }, + ], + }); + const { exitCode, doc } = await run({ + home, + now: NOW, + argv: ["--out-dir", outDir, "--window-hours", "12", "--fresh-hours", "24"], + }); + assert.equal(exitCode, 0); + assert.equal(doc.status, "ok"); + assert.equal(doc.windowHours, 12); + assert.deepEqual(doc.sessions.map((session) => session.id), ["ses_recent"]); +}); + test("fresh export covers a known session with model, tokens, cost, parentage", async () => { const { home, outDir } = await fakeHome(); await makeStore(defaultDb(home), {