Repository navigation
fix(recur): iterate rules in time order for any listed BY order - #35
Merged
Merged
Conversation
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]>
Refs #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]>
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]>
Refs #31 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merged
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.
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:
DAILY;BYHOUR=17,9gives 17:00 before 09:00.HOURLY;INTERVAL=4;BYMINUTE=45,15does the same with minutes.MONTHLY;BYMONTH=6,1,3jumps back and forth across years and skips June of the first year completely.MONTHLY;BYMONTH=9,3,6;BYDAY=FRstarting in September walks June and then that September a second time.expandWallsstops at the first instance pastuntil. So does ical.js' own UNTIL, and COUNT counts the first N instances it yields. As a result, occurrences inside the range were dropped whileexpandOccurrencesreportedcomplete: true. That includes overrides of those occurrences.cancelOccurrencesand EXDATE checks also refused real occurrences (for example 2026-06-15 ofMONTHLY;BYMONTH=6,1,3).Fix
All iteration now goes through one shared iterator,
recurIteratorinsrc/recur.ts. It is used byexpandWalls(src/series.ts) and by the VTIMEZONE observance expansion (src/zone.ts). It does two things:by_indicesoption 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_indicesoption 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.1range stays. After construction the iterator is checked for holding that object. If it does not, expansion throwsBoundUnavailable, 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.DAILY;BYMONTH=3,9from 2026-06-15 (main jumps to 2027-03-01), and the fail-closed check.Full suite on the box: 589 passed, 3 skipped. Deep runs with
SERIES_PROPERTY_RUNS=3000and 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,13series 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:
MONTHLY;INTERVAL=3;BYMONTHDAY=13,5,20;BYDAY=TU. This fails closed:expandOccurrencesthrows instead of dropping occurrences. Unrelated to order; the plain iterator throws too.MONTHLY;BYMONTH=1,6,2;BYMONTHDAY=20from 1 November gives 20 November, a month the rule does not list. This happens with or without this PR.🤖 Generated with Claude Code