Skip to content

fix(recur): iterate rules in time order for any listed BY order - #35

Merged
PhilflowIO merged 6 commits into
mainfrom
fix/monotonic-expand
Nov 17, 2026
Merged

PhilflowIO merged 6 commits into
mainfrom
fix/monotonic-expand

Conversation

@PhilflowIO

@PhilflowIO PhilflowIO commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Closes #31

Problem

ical.js' RecurIterator steps through BYSECOND, BYMINUTE, BYHOUR and BYMONTH by their position in the list, and it starts every position at 0. That causes two defects:

  • A list out of order comes out of order. DAILY;BYHOUR=17,9 gives 17:00 before 09:00. HOURLY;INTERVAL=4;BYMINUTE=45,15 does the same with minutes. MONTHLY;BYMONTH=6,1,3 jumps back and forth across years and skips June of the first year completely.
  • DTSTART's own month or hour is not the first listed. The first step then goes backwards: MONTHLY;BYMONTH=9,3,6;BYDAY=FR starting in September walks June and then that September a second time.

expandWalls stops at the first instance past until. So does ical.js' own UNTIL, and COUNT counts the first N instances it yields. As a result, occurrences inside the range were dropped while expandOccurrences reported complete: true. That includes overrides of those occurrences. cancelOccurrences and EXDATE checks also refused real occurrences (for example 2026-06-15 of MONTHLY;BYMONTH=6,1,3).

Fix

All iteration now goes through one shared iterator, recurIterator in src/recur.ts. It is used by expandWalls (src/series.ts) and by the VTIMEZONE observance expansion (src/zone.ts). It does two things:

  • it sorts and de-duplicates the four lists ical.js steps through by position;
  • it starts each position at DTSTART's value, using the by_indices option that ical.js' constructor accepts for resuming an iteration.

ical.js already sorts BYDAY, BYMONTHDAY and the days of a YEARLY rule itself.

Fail closed: the by_indices option is not documented. Every ical.js 2.x (2.0.0 to 2.2.1) reads it and keeps the object it is given, so the ^2.0.1 range stays. After construction the iterator is checked for holding that object. If it does not, expansion throws BoundUnavailable, the same library-failure error the budget hook uses, and never drops instances silently.

BYSETPOS: ical.js reads it only over the days of a month, in day order (next_month, expand_year_days). It never reads the order or position of these four lists, so the days it selects do not change. A test checks this.

Rejected alternative: keep iterating to the end of the current period before stopping. This cannot repair UNTIL and COUNT, which ical.js applies inside the iterator.

The work budget is unchanged: the same hook, the same costs.

Tests

test/recurOrder.test.ts, 10 tests. 9 fail against main's source; only the BYSETPOS comparison passes there.

  • Concrete shapes from the issue: BYHOUR, BYMINUTE and BYMONTH listed out of order, RRULE UNTIL and COUNT, an override behind the cut, a cancel, a DTSTART that is not the first listed value, DAILY;BYMONTH=3,9 from 2026-06-15 (main jumps to 2027-03-01), and the fail-closed check.
  • Property test against an independent brute-force expansion, for YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY and SECONDLY with BY lists in random order (BYSECOND included), plus COUNT and UNTIL. About a third of the cases put DTSTART off the rule. It also checks that the iteration ascends strictly.
  • BYSETPOS test: both the sorted and the listed-order iteration are compared with every expected instant, meaning each selected day at every listed hour.

Full suite on the box: 589 passed, 3 skipped. Deep runs with SERIES_PROPERTY_RUNS=3000 and seeds 1 to 4: green. The sample is shuffled with Fisher-Yates on the seeded generator, so every engine draws the same sample.

Behaviour change for existing series

YEARLY with several BYHOUR (or BYMINUTE, BYSECOND) values: ical.js uses only one of them (quirk 1 below). Before this PR that was the first one listed; now it is the smallest. An existing YEARLY;BYHOUR=17,13 series therefore moves from 17:00 to 13:00. Both are wrong per RFC 5545, which wants both hours, but the visible time changes.

Other ical.js quirks found, not fixed here

The property test keeps these out:

  1. YEARLY uses only the first BYHOUR and BYMINUTE value. Since this PR that is the smallest value, before it was the first one listed. Both are wrong.
  2. MONTHLY with BYDAY and more than one time of day adds a phantom instance on the 1st of the next month.
  3. HOURLY with BYHOUR ignores INTERVAL, the same way MONTHLY with BYMONTH ignores INTERVAL.
  4. BYDAY with BYMONTHDAY: ical.js stops searching after 48 steps and throws "Malformed values", also on valid rules such as MONTHLY;INTERVAL=3;BYMONTHDAY=13,5,20;BYDAY=TU. This fails closed: expandOccurrences throws instead of dropping occurrences. Unrelated to order; the plain iterator throws too.
  5. With DTSTART off the rule, ical.js yields its starting candidate without testing it against the rule. MONTHLY;BYMONTH=1,6,2;BYMONTHDAY=20 from 1 November gives 20 November, a month the rule does not list. This happens with or without this PR.

🤖 Generated with Claude Code

PhilflowIO and others added 3 commits November 17, 2026 23:29
ical.js steps through BYSECOND, BYMINUTE, BYHOUR and BYMONTH by
position in the list and starts each position at 0. A rule listing them
out of order (DAILY;BYHOUR=17,9, HOURLY;BYMINUTE=45,15,
MONTHLY;BYMONTH=6,1,3) comes out of order, and a rule whose DTSTART
value is not the first listed walks back first
(MONTHLY;BYMONTH=9,3,6;BYDAY=FR from September walks that September
twice). Every expansion stops at the first instance past its end, and
ical.js' own UNTIL and COUNT assume the same order, so occurrences in
range were dropped while the result said complete, and an EXDATE or
cancel on such an occurrence was refused.

All iteration now goes through one iterator that sorts those four lists
and starts each position at DTSTART's value. This changes no set: each
BY part is a set, and ical.js reads BYSETPOS only over a month's days in
day order. Continuing to the end of the current period instead was
rejected: it cannot repair UNTIL and COUNT inside ical.js.

Closes #31

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The BY lists were shuffled with sort() and a random comparator, so
the sample depended on how often V8's sort calls it; Node 26 drew a
rule (MONTHLY;INTERVAL=3;BYMONTHDAY=13,5,20;BYDAY=TU) on which ical.js
throws past the range, plain iterator or not: its BYDAY+BYMONTHDAY
search gives up after 48 steps. Fisher-Yates on the seeded generator
makes the sample engine-independent, and the order check ends at that
throw instead of failing on a quirk this change does not touch.

Refs #31

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PhilflowIO and others added 3 commits November 18, 2026 00:34
The start positions are passed through an option ical.js reads to
resume an iteration but does not document. A version that ignored it
would start at position 0 again and drop instances in silence
(MONTHLY;BYMONTH=6,1,3 from January loses that June). Every ical.js
2.x (2.0.0 to 2.2.1) keeps the object it is given, so the iterator is
checked for holding that object and refuses as a failure of the
library (BoundUnavailable, now in recur.ts beside the check) when it
does not. The ^2.0.1 range needs no raise.

Refs #31

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The sample put DTSTART on the rule's first instance only and left out
WEEKLY, MINUTELY, SECONDLY and BYSECOND. It now covers them, puts
DTSTART off the rule in about a third of the cases, and fixes the case
DAILY;BYMONTH=3,9 from 15 June (main jumps to the next March). With
DTSTART off the rule ical.js yields the candidate it starts on without
testing it; that one instance is allowed, nothing else.

The BYSETPOS test compared the two iterations with each other after
filtering by day; it now compares both with every expected instant,
each selected day at every listed hour.

Refs #31

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@PhilflowIO
PhilflowIO merged commit 71669f5 into main Nov 17, 2026
5 checks passed
@PhilflowIO
PhilflowIO deleted the fix/monotonic-expand branch November 17, 2026 23:37
@PhilflowIO PhilflowIO mentioned this pull request Nov 18, 2026
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.

expandOccurrences can drop occurrences of MONTHLY rules with BYMONTH

1 participant