Skip to content

refactor(studio): describe cron schedules with cronstrue - #2285

Merged
kantord merged 2 commits into
mainfrom
issue-1852-cronstrue
Oct 14, 2026
Merged

kantord merged 2 commits into
mainfrom
issue-1852-cronstrue

Conversation

@kantord

@kantord kantord commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #2194, which has merged.

Summary

Replace the hand-rolled regex describeCron in lib/formatters.ts with cronstrue (3.32.0, MIT). The wrapper keeps the existing contract: anything that is not a five-field expression, or that cronstrue cannot parse, falls back to the raw string (schedule-form.tsx relies on this).

User-facing change

Wording changes, e.g. Daily at 9:00 AM becomes At 09:00 AM, and weekdays read At 05:30 PM, Monday through Friday. Shapes the old matcher printed raw (day lists, pinned months, * * * * *) are now described.

Test plan

  • formatters.test.ts updated for the new wording and the raw-string fallbacks
  • schedule-inventory.test.ts adjusted (search text and sort data depended on the old wording)
  • Web suite, typecheck and biome check pass
  • Playwright e2e not run locally

The lockfile change adds only cronstrue.

Fully or partially written by an AI agent.

🤖 Generated with Claude Code

@kantord
kantord force-pushed the issue-1852-cronstrue branch 2 times, most recently from 50604f7 to 3cfda80 Compare October 13, 2026 15:09
Base automatically changed from issue-1852 to main October 13, 2026 16:35
@kantord
kantord force-pushed the issue-1852-cronstrue branch from 3cfda80 to 23d5806 Compare October 13, 2026 17:00

@peppescg peppescg left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Daniel. Using a library here is the right call, but swapping the describer changed more than the wording, because the form preview, the Schedule sort and the search all run on its output. Reviewed at 23d58066b.

Before merge

  1. Invalid crons now preview as valid (lib/formatters.ts:112). The daemon parses with robfig ParseStandard (engine/adapter/cronparse/cronparse.go:41): weekdays are 0–6, and there's no L, W or #. cronstrue accepts all of those, so 0 9 * * 7 previews as "At 09:00 AM, only on Sunday", 0 9 L * * as "on the last day of the month", and 0 12 * * 1#1 as "first Monday", and then Save fails. The described === outcome.cron check in schedule-form.tsx:79, which showed the "Cron expression" hint, now almost never matches. The 0 9 * * 7 fallback test was the only thing tying the describer to the daemon's range, and it was removed. Keep a guard for the daemon's grammar before calling cronstrue, and restore that test.

  2. Sort and search now run on English prose (schedule-inventory.ts:61, :100).

    • Sort: Schedule sort is localeCompare on the label, so it now orders "At 01:00 PM" before "At 09:00 AM", and midnight after 11 PM. The test changed 0 9 to 0 5 to keep its expected order.
    • Search: searching "daily", "weekly", "monthly" or "weekdays" no longer matches anything, although scheduled-tasks.md says you can search by cadence. The test moved from "weekdays" to "Monday through Friday".

    Sort on a structured key (the next fire time, or the cadence plus minutes since midnight), and add stable cadence words to the search text. Computing the labels once per list also removes the cronstrue parse from inside the comparator.

Small, can go in now

  • Tabs (formatters.ts:110). The guard splits on /\s+/ but passes the original string to cronstrue, which only splits on spaces. A tab-separated cron, which robfig accepts (strings.Fields), shows as raw. Pass parts.join(" ").
  • Long labels (schedules-workspace.tsx:391). The cell is whitespace-nowrap, and 15 9,13,17 * * 1-5 now reads "At 09:15 AM, 01:15 PM and 05:15 PM, only on Monday, Tuesday, Wednesday, Thursday, and Friday", which pushes the Actions column out of view. Truncate, and put the raw cron in title. That also makes the docstring's "shown in a tooltip alongside" (formatters.ts:104) true, which it isn't today in the inventory.
  • Tests (formatters.test.ts:126). Cover the shapes the builder emits: 0 0 * * 0, */15 * * * * and 0 */1 * * *.
  • Bundle (formatters.ts:3). cronstrue comes in through the shared lib/formatters, so chat and knowledge load it too. Move describeCron into features/schedules, or import cronstrue lazily there.
  • Stale comment (schedule-phrase.ts:23-27). It still points at the describeCron Weekly/Sunday shape, which is gone. The real reason for Sunday being 0 is the daemon's 0–6 range.

Can be a follow-up

  • Steps wider than the field read wrong, as they did before the swap. */90 * * * * reads "Every 90 minutes", but robfig only sets minute 0, so it runs hourly; 0 */25 * * * runs once a day.

kantord and others added 2 commits October 14, 2026 11:21
Replace the hand-rolled describeCron regex matcher with cronstrue. The
wrapper keeps the existing contract: anything that is not a five-field
expression, or that cronstrue cannot parse, falls back to the raw string.
Wording changes (e.g. "At 09:00 AM, only on Friday"), and shapes the old
matcher passed through raw (day lists, pinned months) are now described.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
cronstrue accepts weekday 7 and the L, W and # forms that the daemon's
parser rejects, so an invalid cron previewed as a valid one and the
"Cron expression" hint no longer showed. Guard each field against the
daemon's grammar before calling cronstrue, and return the raw expression
for what the daemon would reject, for steps wider than the field, and for
anything that is not five fields. Tab-separated fields now describe too.

The Schedule column now sorts on the cadence and time of day rather than
the English label, and search matches cadence words (daily, weekly,
weekdays, monthly) again. Long labels truncate with the raw cron in the
title. describeCron moves to features/schedules so cronstrue stays out of
the shared formatters bundle.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@kantord

kantord commented Oct 14, 2026 •

Copy link
Copy Markdown
Member Author

Thanks @peppescg, these were real regressions. Pushed e83c61963 on top of the rebase onto current main; all of it is addressed:

Before merge

  1. Invalid crons previewing as valid. describeCron now checks each field against the daemon's grammar (ParseStandard: weekday 0–6, month and weekday names allowed, no L, W or #, ranges in order, positive steps) before calling cronstrue, and returns the raw expression otherwise. That restores the "Cron expression" hint in the form. The 0 9 * * 7 case is back in the tests, with L, W, #, out-of-range values and backwards ranges beside it.
  2. Sort and search.
    • The Schedule column now sorts on a structured key: cadence, then position within it (minutes since midnight for daily, and so on), then the raw expression. One-shots follow, soonest first. 0 13 no longer sorts before 0 9, and midnight comes first.
    • Search text now includes stable cadence words (daily, weekly, weekdays, monthly, interval) alongside the described label and raw cron, so "weekdays" matches again, including 15 9,13,17 * * 1-5. I restored the original test inputs and added tests for both.
    • describeCron memoizes per expression, so the comparator and filter no longer re-parse.

Small items

  • Tabs: fields are re-joined with single spaces before cronstrue, so tab-separated crons describe.
  • Long labels: the cell truncates with the raw cron in title. I checked 15 9,13,17 * * 1-5 in a real browser at 1000px; the Actions column stays visible.
  • Tests: added 0 0 * * 0, */15 * * * * and 0 */1 * * *.
  • Bundle: describeCron moved to features/schedules/describe-cron.ts, so cronstrue is no longer in the shared formatters.
  • Stale comment in schedule-phrase.ts now gives the daemon's 0–6 range as the reason.

Follow-up item (steps wider than the field): I folded it in, since it was one rule in the guard. A step larger than the span it steps over (*/90, 0 */25, */60) returns the raw expression instead of a wrong description.

Fully or partially written by an AI agent.

@peppescg peppescg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Daniel, this is all addressed. The grammar guard and the cadence sort key are exactly what I had in mind, and thanks for folding in the wide-step case too. LGTM.

@kantord kantord mentioned this pull request Oct 14, 2026
41 tasks done
@kantord
kantord merged commit 1a3150f into main Oct 14, 2026
31 checks passed
@kantord
kantord deleted the issue-1852-cronstrue branch October 14, 2026 10:36
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Oct 20, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants