Repository navigation
fix(cron): accept week unit in cron once --after durations - #435
Draft
hetaoBackend wants to merge 2 commits into
Draft
hetaoBackend wants to merge 2 commits into
hetaoBackend wants to merge 2 commits into
Conversation
`cron once --after 1w` was rejected with CRON_VALIDATION_ERROR even though DURATION_UNIT_MS defines `w`, because both relative-duration parsers only matched `ms|s|m|h|d`. Add `w` to the token regex in the shared agent-tools parser and the local-runtime parser (keeping `ms` ahead of `m`), and replace the local-runtime nested ternary with a unit table that includes weeks. Single (`1w`) and compound (`1w2d`) week durations now resolve correctly; existing units and invalid-input rejection are unchanged and covered by new unit tests in both packages. Fixes #433 Co-authored-by: smartworldarafath <153785481+smartworldarafath@users.noreply.github.com>
CodeQL flagged the `parseDurationMs` token regex (js/polynomial-redos): matching the unanchored global pattern against `after` retries `\d+` from every offset, so a long unit-less digit run takes quadratic time (about 2s for 50k digits). Consume one start-anchored `<number><unit>` token at a time instead, in both the agent-tools and local-runtime parsers. Accepted and rejected inputs are unchanged; add a regression test for the digit-run case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change
Fixes #433.
cron once --after 1w(and compound forms such as1w2d) failed withCRON_VALIDATION_ERROR: Invalid cron once after duration: "1w", even though the sharedDURATION_UNIT_MStable already defineswandcron self --every 1waccepts it. Both relative-duration parsers used forcron onceonly matchedms|s|m|h|d.packages/agent-tools/src/shared/cron-schedule-input.ts: theparseDurationMstoken regex now matchesms|s|m|h|d|w.msstays ahead ofm, so500msis still read as milliseconds and not as minutes plus a strays.packages/local-runtime/src/cron/once-time.ts: same regex change. The nested unit ternary (which had no week branch) is replaced with aDURATION_UNIT_MStable that includesw: 604_800_000.Both parsers now consume one start-anchored
<number><unit>token at a time (/^(\d+(?:\.\d+)?)(ms|s|m|h|d|w)/plusslice) instead of runningmatchAllwith an unanchored global pattern and comparing the concatenated matches. CodeQL flagged the touched regex asjs/polynomial-redos. The quadratic behavior was already on main: a 50k-digit unit-lessaftertook about 2 s to reject. It now fails in linear time. Accepted and rejected inputs are the same as before.After this change,
1wresolves to 7 days and1w2dto 9 days. Existing units and the rejection of malformed input (1week,1 w,w,0w,1y, …) are unchanged.I grepped for other duration parsers with the same gap.
cron selfintervals (parseIntervalSeconds) andpackages/shared/src/watch-interval.tsalready acceptw. The TUI headless timeout parser and the permissiontimeoutduration matcher leave outwon purpose (it isn't a valid unit there), so I didn't touch them. Theafterfield description (e.g. "10m" or "1h30m") gives examples rather than a list of units, so the model-visible schema is unchanged.Thanks to @smartworldarafath for reporting this, finding the root cause in both parsers and proposing the fix. They are credited as co-author on the commit.
Follow-up, not changed here:
parseIntervalSeconds(cron self --every) in the same file uses the same unanchoredmatchAllpattern, so it scales the same way on long digit runs. I kept this PR to thecron onceparsers.bug,cli.Validation
Before the fix (origin/main
1852771), the new tests fail as expected: 4 week-unit cases fail withInvalid cron once after duration: "1w"/"1w2d", and the other 20 pass.New unit tests, both passing (26/26):
packages/agent-tools/src/shared/cron-schedule-input.test.tspackages/local-runtime/test/unit/cron/once-time.test.tsThey cover a single week (
1w,2W,1.5w), compound week durations (1w2d,1w2d3h4m5s6ms,2d1w), unchanged existing units (250ms,30s,10m,2h,3d,1h30m,1m500ms), rejection of invalid input with the existing error code/status, and a long unit-less digit run ('1'.repeat(50_000) + 'x') rejected in under 500 ms. With the previous unanchored parser, that digit-run test fails in both packages. Both files are registered intest/vitest-suites.json(capability suite), andrelease/public-source.jsonwas regenerated for the two new files.pnpm verify(full profile, Linux, Node 22.23.3) ondaf3050passed 14 gates.test:capabilitiesran 212 files with 5311 passed and 18 skipped. Skipped as not applicable on Linux:test:windows,test:sandbox,test:release-package.Performance: automatic 100-round basic check. This is a parser-only change outside the runtime paths that need
perf:full.NOT RUN: Windows/macOS-only gates; no live-service checks.
Publication and contribution checks
release/public-source.json; new tests are declared intest/vitest-suites.jsonwhere applicable.Maintainer handoff
Publication scope or license changes (if any): none.
Shared-source port: not needed.