Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions pm-stack/aggregator/scripts/produce-charge-candidates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
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 <path>] [--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 <path>] [--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 <path>] [--skip-gmail])`,
);
process.exit(2);
}
}

function isoDay(d: Date): string {
return d.toISOString().slice(0, 10);
}
Expand Down Expand Up @@ -410,8 +453,11 @@ async function gmailCandidates(
}

async function main(): Promise<void> {
// 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");
Expand Down
49 changes: 49 additions & 0 deletions pm-stack/aggregator/test/produce-charge-candidates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
});
});