From 23133f18241487e2d0596fca7165e6b0ecb12c6e Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Thu, 12 Nov 2026 03:45:19 -0600 Subject: [PATCH] fix(pm-stack): reject duplicate export options --- .../opencode-usage-export.mjs | 27 +++++++++++++------ .../opencode-usage-export.test.mjs | 21 +++++++++++++++ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/pm-stack/opencode-usage-export/opencode-usage-export.mjs b/pm-stack/opencode-usage-export/opencode-usage-export.mjs index 4d400db681..d421e00da5 100755 --- a/pm-stack/opencode-usage-export/opencode-usage-export.mjs +++ b/pm-stack/opencode-usage-export/opencode-usage-export.mjs @@ -362,12 +362,21 @@ function parsePathFlag(flag, raw, args) { function parseArgs(argv) { const args = { db: null, outDir: null, windowHours: DEFAULT_WINDOW_HOURS, freshHours: DEFAULT_FRESH_HOURS }; + const seen = new Set(); for (let i = 0; i < argv.length; i += 1) { const a = argv[i]; - if (a === "--db") args.db = parsePathFlag("--db", argv[++i], args); - else if (a === "--out-dir") args.outDir = parsePathFlag("--out-dir", argv[++i], args); - else if (a === "--window-hours") args.windowHours = parseHourFlag("--window-hours", argv[++i], args); - else if (a === "--fresh-hours") args.freshHours = parseHourFlag("--fresh-hours", argv[++i], args); + if (["--db", "--out-dir", "--window-hours", "--fresh-hours"].includes(a)) { + if (seen.has(a)) { + const err = new ExportError("invalid-argument", 2, `opencode usage export: duplicate option ${a}`); + err.suppressFailureDocument = true; + throw err; + } + seen.add(a); + if (a === "--db") args.db = parsePathFlag(a, argv[++i], args); + else if (a === "--out-dir") args.outDir = parsePathFlag(a, argv[++i], args); + else if (a === "--window-hours") args.windowHours = parseHourFlag(a, argv[++i], args); + else args.freshHours = parseHourFlag(a, argv[++i], args); + } else throw new Error(`unknown argument: ${a}`); } return args; @@ -394,10 +403,12 @@ export async function run({ ? err : new ExportError("invalid-argument", 2, `opencode usage export: ${err?.message ?? err}`); const doc = failureDocument(e.status, e.message, attemptedAt); - try { - await writeDocument(err?.args?.outDir ?? join(home, "atlas/opencode-usage-export"), doc); - } catch { - /* nowhere to write the failure either; exit code carries it */ + if (!e.suppressFailureDocument) { + try { + await writeDocument(err?.args?.outDir ?? join(home, "atlas/opencode-usage-export"), doc); + } catch { + /* nowhere to write the failure either; exit code carries it */ + } } return { exitCode: e.exitCode, doc }; } 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 0ef23712b7..827f821da4 100644 --- a/pm-stack/opencode-usage-export/opencode-usage-export.test.mjs +++ b/pm-stack/opencode-usage-export/opencode-usage-export.test.mjs @@ -301,6 +301,27 @@ test("trailing --out-dir: invalid-argument failure document, exit 2 (#6934)", as assert.equal(latest.status, "invalid-argument"); }); +test("duplicate scalar value options fail before probing or writing", async () => { + const { home } = await fakeHome(); + const first = join(home, "first-output"); + const second = join(home, "second-output"); + const cases = [ + ["--db", "first.db", "--db", "second.db"], + ["--out-dir", first, "--out-dir", second], + ["--window-hours", "6", "--window-hours", "12"], + ["--fresh-hours", "6", "--fresh-hours", "12"], + ]; + for (const argv of cases) { + const { exitCode, doc } = await run({ home, now: NOW, argv }); + assert.equal(exitCode, 2); + assert.equal(doc.status, "invalid-argument"); + assert.match(doc.detail, new RegExp(`duplicate option ${argv[0]}`)); + } + assert.equal(fs.existsSync(first), false); + assert.equal(fs.existsSync(second), false); + assert.equal(fs.existsSync(join(home, "atlas/opencode-usage-export")), false); +}); + test("valid --db / --out-dir behave as before (#6934 control)", async () => { const { home, outDir } = await fakeHome(); const dbPath = join(home, "explicit.db");