Repository navigation
fix(cli): reject invalid check numeric flags as usage errors - #7673
Conversation
--samples, --max-issues, --max-transition-samples and --tolerance fell back to their defaults on any value they could not parse, and parseInt read 5abc as 5. A bad value now exits 2 with a usage_error that names the flag and the value. Absent flags keep their defaults.
miguel-heygen
left a comment
There was a problem hiding this comment.
Review at 5e5104a6. There were no other reviews when I posted. I reviewed fc4047b7 in full. 5e5104a6 only renames numericFlag to wholeStringNumber and drops the added comment (git diff fc4047b7 5e5104a6), so everything below still holds.
What's right
- Strict parsing.
check.ts:741:wholeStringNumberparses withNumber()after a trim and maps an empty string to NaN. So5abc,2pxandtenare rejected, where they used to be read as 5, 2 and the default. - Integer check.
positiveInteger(:721) requiresNumber.isInteger(parsed) && parsed >= 1, so--samples 2.5fails instead of being read as 2. - Error path. The thrown
CliUsageErrorgoes through the existingreportCheckFailurepath, so the exit code (2) andusage_errormatchInvalid --range. - Defaults are safe.
samples,max-issuesandtolerancedeclare string defaults ("9","80","2",:189-233), so an absent flag still parses to the default.max-transition-sampleshas no default, so absent is stillundefined, which means no cap.- The body's "
--max-transition-samples 0is now an error; absent means no cap" matches:480,487.
- Tests. The agent-mode rows (
cli.agentMode.test.ts:236-237) pin exit 2 and theusage_errorenvelope through a spawned CLI.
Tests and mutation checks (check.test.ts: 191 pass at both heads, matching the body; mutations run at fc4047b7)
- The number parser →
parseFloat→ 2 fail (5abc,2px). parsed < 1→parsed < 0→ the--samples 0case fails.Number.isInteger→Number.isFinite→ the--samples 2.5case fails.--tolerance:< 0→<= 0→ the "--tolerance 0stays valid" case fails.- Two mutations survive (nits below):
- dropping the empty-string guard in
wholeStringNumber; Number.isFinite→!Number.isNaNfor--tolerance.
- dropping the empty-string guard in
Nits (non-blocking)
- Two untested edges on
--tolerance.--tolerance=(empty) and--tolerance Infinityaren't covered. Without thetext === ""guard,Number("")is 0 and--tolerance=would silently mean zero tolerance. WithoutisFinite,Infinitywould be accepted.- Both are handled today. Two more rows in the invalid-values table would pin them.
- The warm-check daemon can't catch this throw (by reading the code, not tested).
warmCheck.ts:137callsvoid prelaunchBrowserForCheck(checkArgs).catch(() => undefined). ButprelaunchBrowserForCheck(check.ts:510) isn'tasync, so a throw fromparseCheckOptionsescapes before.catchis attached.- So the detached warm process that
bin/hyperframes.mjs:25spawns after an eligible run with bad flags exits, and the user's corrected next run starts cold. - Nothing user-visible breaks (stdio is ignored), and
Invalid --rangealready takes this path. This PR only widens it. MakingprelaunchBrowserForCheckasyncwould close it.
CI
- This is a fork PR, so every workflow was waiting on maintainer approval. I approved the runs for both heads after reading the diff: three files, nothing under
.github/or build scripts. - On
5e5104a6, every required check that ran is green:Build,Typecheck,Test,Test reachability,Test: runtime contract,regression, the Windows tests and render, captures andSemantic PR title.Skills: manifest in syncandStudio: edit accuracy gatewere skipped because they aren't affected. Commentswas red atfc4047b7because of the one added comment line.5e5104a6drops it, so the check is green at this head.
Verdict: APPROVE
Reasoning: Each invalid value now fails as a usage error through the existing envelope, the defaults are unchanged, and the tests catch each mutation of the parsing rules except two minor edges. Required CI is green on this head.
Make prelaunchBrowserForCheck async so a usage error from parseCheckOptions becomes a rejection that warmCheck's .catch handles, instead of a synchronous throw that ends the warm process. Pin --tolerance= and --tolerance Infinity as usage errors.
|
Thanks for the careful review and the mutation runs. I took both nits in a18b9f7:
|
What
hyperframes checknow rejects a bad--samples,--max-issues,--max-transition-samplesor--tolerancevalue as a usage error (exit 2,usage_errorunder--json) instead of quietly running with the default.Why
An invalid value was silently replaced with the default, so the run looked fine but used other settings than the user asked for.
parseIntalso read--samples 5abcas 5 andparseFloatread--tolerance 2pxas 2.Related work
Fixes #7672
How
positiveIntegerandnonNegativeNumberinpackages/cli/src/commands/check.tsnow take the args and the flag name:Number()(empty string → NaN), so trailing junk is rejected--tolerance) →CliUsageError("Invalid --<flag>: \"<value>\"; use a positive integer"), same style asInvalid --rangeThe thrown error goes through the existing
reportCheckFailurepath, so--jsongets theusage_errorenvelope and the exit code is 2.Behavior notes:
--samples 2.5is now an error (it was read as 2).--max-transition-samples 0is now an error. Before, 0 fell into the "no cap" branch by accident; leaving the flag out still means no cap.--tolerance 0is still valid.Test plan
check.test.ts: 9 cases (--samples 0|ten|5abc|2.5,--max-issues -5,--max-transition-samples abc,--tolerance -1|px|2px) expect exit 2,usage_error, a message naming flag and value, and that the pipeline never runs. All 9 fail onmain.check.test.ts: valid values (--samples 3 --max-transition-samples 4 --tolerance 0.5,--tolerance 0) reach the pipeline, and absent flags keep their defaults.cli.agentMode.test.ts:check --samples tenandcheck --tolerance=-1added to the "flag checks a command makes itself" table (spawned CLI, exit 2 +usage_error). Both fail onmain.vitest run src/commands/check.test.ts: 191 passed. Agent-modeflag checksgroup: 54 passed.oxlintandoxfmt --checkclean on the changed files;tsc --noEmitshows no errors in them.