Validate scale numeric options before actions run#742
Validate scale numeric options before actions run#742rissrice2105-agent wants to merge 1 commit into
Conversation
Greptile SummaryThis PR adds parse-time validation for numeric CLI options in the
Confidence Score: 4/5Safe to merge; the parsers correctly block all targeted bad inputs before actions run, and no existing valid inputs are broken. The core logic is correct and the test coverage is solid. Two action-level guards (min < 0, max < 1) are now dead code that could be cleaned up, and --target-cpu still relies on the action handler for its upper-bound check rather than the new parse-time style — both are minor quality gaps rather than functional defects. The auto-scale action handler in scale.ts (lines 466–481) has checks that are now either unreachable or inconsistent with the parse-time validation pattern introduced here. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[CLI argument string] --> B{Which option?}
B --> |--instances / --max / --cooldown / --ttl / --percent| C[parsePositiveSafeInteger]
B --> |--min| D[parseNonNegativeSafeInteger]
B --> |--max-hourly-price| E[parsePositiveFiniteNumber]
B --> |--target-cpu| F[parsePositiveSafeInteger only lower bound ge 1]
C --> G{Number.isSafeInteger AND value ge 1?}
D --> H{Number.isSafeInteger AND value ge 0?}
E --> I{Number.isFinite AND value gt 0?}
F --> J{Number.isSafeInteger AND value ge 1?}
G -- No --> K[InvalidArgumentError - Commander exits immediately]
H -- No --> K
I -- No --> K
J -- No --> K
G -- Yes --> L[Action handler runs]
H -- Yes --> L
I -- Yes --> L
J -- Yes --> M{targetCpu gt 100?}
M -- Yes --> N[process.exit 1 - action-level check]
M -- No --> L
L --> O[Command executes]
|
| .option('--cooldown <seconds>', 'minimum time between scale events', Number, 300) | ||
| .option('--min <n>', 'minimum instances', parseNonNegativeSafeInteger, 1) | ||
| .option('--max <n>', 'maximum instances', parsePositiveSafeInteger, 10) | ||
| .option('--target-cpu <percent>', 'target CPU utilization to maintain', parsePositiveSafeInteger, 70) |
There was a problem hiding this comment.
--target-cpu upper-bound check inconsistency
parsePositiveSafeInteger accepts any integer >= 1, so values like --target-cpu 200 pass parse-time validation and are only caught in the action handler (line 478) with a process.exit(1) message. All other options in this PR now surface validation errors early via Commander's InvalidArgumentError. Consider using a dedicated parser for percent values (e.g. one that rejects anything outside 1–100) for consistent user experience.
| } | ||
| }); | ||
|
|
||
| it('allows zero only for non-negative safe integer options', () => { | ||
| expect(parseNonNegativeSafeInteger('0')).toBe(0); | ||
| expect(parseNonNegativeSafeInteger('4')).toBe(4); |
There was a problem hiding this comment.
Missing unsafe-integer test for
parseNonNegativeSafeInteger
The parsePositiveSafeInteger suite explicitly tests '9007199254740992' (2^53, the first unsafe integer), but the parseNonNegativeSafeInteger suite omits that boundary case. Adding it would ensure the upper bound of Number.isSafeInteger is uniformly exercised across all three parsers.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Fixes #729.
Changes
--max-hourly-priceVerification
corepack pnpm vitest run packages/cli/src/commands/scale.test.ts(20 passed)corepack pnpm --filter @profullstack/sh1pt typecheckattempted; it still fails on existing workspace/module-resolution errors unrelated to this PR