From a86aea9c55e05f3f7819986973daebfafe4d5505 Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Sat, 7 Nov 2026 23:23:07 -0600 Subject: [PATCH] fix(aggregator): produce-charge-candidates rejects unknown/duplicated arguments before any connector call or file write (#7301) --- .../scripts/produce-charge-candidates.ts | 50 ++++++++++++++++++- .../test/produce-charge-candidates.test.ts | 49 ++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/pm-stack/aggregator/scripts/produce-charge-candidates.ts b/pm-stack/aggregator/scripts/produce-charge-candidates.ts index 6c9a3a9c8f..3907a4042f 100644 --- a/pm-stack/aggregator/scripts/produce-charge-candidates.ts +++ b/pm-stack/aggregator/scripts/produce-charge-candidates.ts @@ -119,6 +119,49 @@ function argValue(flag: string): string | undefined { return value; } +// Unknown or duplicated arguments are usage errors, never silently +// ignored (#7301): argv was probed token-by-token by argValue() / +// includes("--skip-gmail") only, so a typo'd --strat fell through to +// the default current-month period and overwrote the candidates file +// for the wrong period, a joined --start=... did the same, a typo'd +// --skip-gmal ran the Gmail read anyway, and a duplicate --start +// silently let the first win — all exit 0. Mirrors rejectUnknownArgs +// in post-alert.ts (#7289) / generate-digest.ts (#7097). Runs first +// in main(), before any connector call or file write. Known flags +// stay accepted in staged mode too — their values remain ignored +// there per the staged-manifest contract; only unknown, joined, or +// duplicated tokens are refused. +const VALUE_FLAGS = ["--start", "--end", "--out"]; +const STANDALONE_FLAGS = ["--skip-gmail"]; +function rejectUnknownArgs(): void { + const args = process.argv.slice(2); + const seen = new Set(); + for (let i = 0; i < args.length; i++) { + const token = args[i] as string; + if (VALUE_FLAGS.includes(token)) { + if (seen.has(token)) { + console.error(`producer: duplicate ${token} (usage: [--start YYYY-MM-DD] [--end YYYY-MM-DD] [--out ] [--skip-gmail])`); + process.exit(2); + } + seen.add(token); + i++; // skip the value; argValue() validates it + continue; + } + if (STANDALONE_FLAGS.includes(token)) { + if (seen.has(token)) { + console.error(`producer: duplicate ${token} (usage: [--start YYYY-MM-DD] [--end YYYY-MM-DD] [--out ] [--skip-gmail])`); + process.exit(2); + } + seen.add(token); + continue; + } + console.error( + `producer: unknown argument ${JSON.stringify(token)} (usage: [--start YYYY-MM-DD] [--end YYYY-MM-DD] [--out ] [--skip-gmail])`, + ); + process.exit(2); + } +} + function isoDay(d: Date): string { return d.toISOString().slice(0, 10); } @@ -410,8 +453,11 @@ async function gmailCandidates( } async function main(): Promise { - // Parse values before any connector call or file write. Staged periods - // come from their manifest, so --start/--end remain ignored there. + // Reject unknown arguments before anything is parsed, queried, or + // written (#7301); then parse values before any connector call or + // file write. Staged periods come from their manifest, so + // --start/--end remain ignored there. + rejectUnknownArgs(); const outArg = argValue("--out"); const explicitStart = INPUTS_DIR ? undefined : argValue("--start"); const explicitEnd = INPUTS_DIR ? undefined : argValue("--end"); diff --git a/pm-stack/aggregator/test/produce-charge-candidates.test.ts b/pm-stack/aggregator/test/produce-charge-candidates.test.ts index 52f8c0a498..93464e64fd 100644 --- a/pm-stack/aggregator/test/produce-charge-candidates.test.ts +++ b/pm-stack/aggregator/test/produce-charge-candidates.test.ts @@ -400,3 +400,52 @@ describe("live mode CLI validation (#3333)", () => { expect(readFileSync(h.gmailCalls, "utf8")).toContain("gmail +triage"); }); }); + +// #7301: unknown, misspelled, joined, or duplicated arguments must be +// usage errors before any side effect — argv was probed token-by-token +// only, so a typo'd --strat silently produced the default current-month +// period, a joined --start=... did the same, and a typo'd --skip-gmal +// ran the Gmail read anyway, all exit 0. +describe("live mode unknown arguments (#7301)", () => { + test("an unknown token is a usage error before any side effect", () => { + expectCliUsageError( + (h) => ["--skip-gmail", "--bogus", "--start", "2026-09-01", "--end", "2026-09-30", "--out", h.out], + 'unknown argument "--bogus"', + ); + }); + + test("a typo'd --strat is a usage error, not the default period", () => { + expectCliUsageError( + (h) => ["--skip-gmail", "--strat", "2026-09-01", "--end", "2026-12-31", "--out", h.out], + 'unknown argument "--strat"', + ); + }); + + test("a joined --start=... is a usage error, not the default period", () => { + expectCliUsageError( + (h) => ["--skip-gmail", "--start=2026-09-01", "--end", "2026-12-31", "--out", h.out], + 'unknown argument "--start=2026-09-01"', + ); + }); + + test("a typo'd --skip-gmal is a usage error, not a Gmail read", () => { + expectCliUsageError( + (h) => ["--skip-gmal", "--start", "2026-09-01", "--end", "2026-09-30", "--out", h.out], + 'unknown argument "--skip-gmal"', + ); + }); + + test("a duplicate --start is a usage error, not first-wins", () => { + expectCliUsageError( + (h) => ["--skip-gmail", "--start", "2026-09-01", "--start", "2026-10-01", "--end", "2026-12-31", "--out", h.out], + "duplicate --start", + ); + }); + + test("a duplicate --skip-gmail is a usage error", () => { + expectCliUsageError( + (h) => ["--skip-gmail", "--skip-gmail", "--start", "2026-09-01", "--end", "2026-09-30", "--out", h.out], + "duplicate --skip-gmail", + ); + }); +});