Repository navigation
feat(expand): start rule walks near the window - #39
Merged
Merged
Conversation
expandOccurrences walked every rule from DTSTART, so an open-ended series cost work in proportion to its age: a yearly birthday from 1960 queried for one week of 2026 spent about 3,000 units, and 5,000 of them 15 million, fifty times the 300,000 a dav-mcp request has (dav-mcp#170 review). With `from`, a yearly rule of fixed days (FREQ=YEARLY with no COUNT and no part but BYMONTH and a BYMONTHDAY every month it takes has) is worked out without ical.js: 45 units per instance in the window, nothing when there is none. That is what ical.js yields for it, since expand_year_days builds each year from those parts alone and next_year carries nothing from one year to the next for such a day. 5,000 birthdays now cost 4,275 units for a week. Any other rule without COUNT is walked from DTSTART moved by whole FREQ x INTERVAL periods to two periods before the one holding `from` (same day of month, weekday, time and INTERVAL phase), and instances before `from` are dropped. ical.js evaluates BY parts and BYSETPOS per period, so the result is the walk from DTSTART filtered; the two spare periods absorb its untested first period and BYWEEKNO weeks that reach into the next year. Such a rule costs about three candidates of its FREQ at least (135 units yearly). A rule whose UNTIL lies before `from` is not walked. Walked from DTSTART in full, where only the whole walk gives ical.js' result or verdict: COUNT; YEARLY with a BYMONTHDAY some month it takes may lack (ical.js reads it by the month of the instance before); MONTHLY with BYDAY and BYMONTHDAY (its 48-month search throws on a gap before `from`); YEARLY with BYDAY and BYYEARDAY or BYWEEKNO (it ends a rule in silence after 28 empty years); a date series with a time part. A call with `from` refuses exactly when the call without it does (fail closed): ical.js' setup is run at DTSTART before any moved walk or arithmetic, a rule it sets up there to yield nothing yields nothing, and a moved start it refuses is walked again from DTSTART. The property test checks both directions for random rules over eight seeds. Selection stays by original start; the property test draws from an exact 32-bit generator, since the LCG in doubles cycles after a few hundred draws. Closes #37 Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI checks that the committed dist is what src builds to. Refs #37 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 18, 2026
The review of #39 found results with `from` that the walk from DTSTART does not give, and one window that cost far more than its budget: - MONTHLY with a fifth or fifth-last weekday (5SU, -5FR) is walked from DTSTART. Where the start's month lacks that weekday, ical.js' setup searches on month by month, advances BYMONTH's shared position (which resetting its start does not undo), and yields what it finds untested. From a moved start that happens next to the window, so occurrences were lost or invented (139 of 3,000 random rules). Every month has a first to fourth of each weekday, so no other BYDAY makes it search. The claim that every period after the first expands as from DTSTART now says where it holds: once a walk is past its setup. - A moved start whose setup ends the rule (YEARLY;BYYEARDAY=366 from 2097: three years on, no 29 February) is walked from DTSTART, as a moved start the setup refuses already was. Before, it yielded nothing. - A yearly rule of fixed days is yielded instance by instance and charged as it comes. It was worked out in full first: 336 days a year to the year 200000 took 14.5 s and 562 MB before the budget was read. - A DTSTART before the year 100 is walked from DTSTART: ical.js compares those times as 1900 to 1999 (Time.toUnixTime goes through Date.UTC), so only its own walk gives its own result. The year start of the worked-out days no longer uses Date.UTC for the same reason. Tests: the three failing rules of the review, fifth weekdays in the property sweep, a random day-366 generator that takes the restart path, a window to the year 200000 bounded by units and heap, and years before 100 with and without `from`. Refs #37, #36 Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI checks that the committed dist is what src builds to. Refs #37 Co-Authored-By: Claude Opus 5.5 <[email protected]>
ical.js ends a YEARLY rule after 28 years without an instance and a MONTHLY one after 336 months (RecurIterator.next), and its next() then returns null as if the rule had ended. A fifth Sunday of February can be 40 years away, so FREQ=YEARLY;BYMONTH=2;BYDAY=5SU from 2058 stopped after 2088 and expandOccurrences reported complete: true, while 2128-02-29 is an occurrence. An iterator that returns null although its COUNT is not reached and it has not passed UNTIL (and its setup found instances) has given up: the result is now complete: false, stoppedBy: 'library', and a write check on such a series fails closed. The setup does not have that cut: it searches on by years without bound. So from a moved start, the same rule found 2128-02-29 where the walk from DTSTART had stopped (32 of 6,000 in the re-review's gap sweep). MONTHLY and YEARLY rules with a fifth or fifth-last weekday or a BYSETPOS of 5 or more, whose years or months may hold no instance, are now walked from DTSTART, so both calls give the same result, and the comment that said YEARLY's setup searches as a walk would is corrected. Refs #37, #36 Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI checks that the committed dist is what src builds to. Refs #37 Co-Authored-By: Claude Opus 5.5 <[email protected]>
A write check that fails because ical.js gave the rule up reused CHECK_LIMIT_EXCEEDED, which reads as the work budget. Error codes are public API, so it gets its own, CHECK_LIBRARY_STOPPED, matching stoppedBy: 'library' from expandOccurrences, with the same remedies. Refs #37, #36 Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI checks that the committed dist is what src builds to. Refs #37 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 18, 2026
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.
What
With
from,expandOccurrencesno longer walks an open-ended rule from DTSTART. Cost now depends on the window, not on how old the series is.FREQ=YEARLYwith no COUNT and no parts exceptBYMONTHand aBYMONTHDAYthat every month the rule takes has (birthdays, anniversaries, includingBYMONTH=5;BYMONTHDAY=30). SeefixedYearlyWallsinsrc/recur.ts.from. Instances beforefromare dropped (iterationStart).fromis not walked.walkedInFull): COUNT; YEARLY with a BYMONTHDAY that some month it takes may lack; MONTHLY with BYDAY+BYMONTHDAY; YEARLY with BYDAY+BYYEARDAY/BYWEEKNO; a date series with a time part.Selection is still by original start, so the contract is unchanged.
Budget per call: N open-ended yearly series, DTSTART 1960, window 2026-06-01..08
Units spent from one shared budget (box run, node:22). "Spread" means each series has its own day of the year.
FREQ=YEARLY, spreadFREQ=YEARLY;BYMONTH=m;BYMONTHDAY=d, spreadFREQ=YEARLY, all 5,000 on 06-03 (all in the week)BYMONTH=6;BYDAY=1SUSo 5,000 birthdays fit in 300,000 units with 1.4% used. Even the worst case, where all 5,000 fall in the window, stays inside the budget: 45 units per occurrence actually returned.
Why a walked rule has a floor of about 90 to 135 units. ical.js returns the first instance from its start position without testing it. Each later candidate passes
check_contracting_rulesonce, at 45 units for YEARLY. A walk from two periods back costs three candidates: the second lead period, the window's period, and the first candidate past the horizon, which is charged before the walk stops. That is 135 units. A series young enough to start at DTSTART costs 90.The two lead periods can't be dropped. ical.js yields the start untested and filters by it within its own period, and YEARLY;BYWEEKNO weeks reach into the next year. That is why only shapes that need no iteration at all reach 0.
Refusal symmetry (fail closed)
A call with
fromrefuses exactly when the same call withoutfromrefuses. Three mechanisms enforce this:MONTHLY;BYDAY=5SU,5FR: the start moves to 1 April 2024, which ical.js refuses, while DTSTART in 2020 is accepted.The property test checks both directions over random rules: if the call without
fromrefuses, every call withfromrefuses with the same message, and otherwise every call withfromcompletes and returns the filtered full result.Tests (box,
--maxWorkers=4)tsc --noEmitis clean.test/expandFrom.test.tswithSERIES_PROPERTY_RUNS=1500, seeds 1–8: 30/30 each. After the last test was added: 31/31.Review fixes (commits 9b429ad, 36c4942)
5SU,-5FR) is now walked from DTSTART. When the start's month lacks that weekday, ical.js' setup searches forward month by month. Each step moves BYMONTH's shared position, which resetting the start does not undo, and the setup then yields what it found untested. From a moved start this happens next to the window. In the reviewer's sweep it lost or invented occurrences in 139 of 3,000 rules; now 0 of 3,000. Ordinals 1–4 and plain weekdays never trigger the search (0 of 3,000 both before and after).Time.toUnixTimegoes throughDate.UTC, which reads those years as 1900–1999. The year start in the computed days no longer usesDate.UTCeither.walls/refusal) mismatches in 12,000 rules. What it still reports are its own cost and brute-force checks below the year 100, where the walk is now used by design.Re-review fixes (commits on top of 36c4942)
RecurIterator.nextends a YEARLY rule after 28 years without an instance (a MONTHLY one after 336 months) and returns null as if the rule had ended. A null while COUNT is not reached and UNTIL is not passed now givescomplete: false, stoppedBy: 'library', and a write check on such a series fails closed. Example:FREQ=YEARLY;BYMONTH=2;BYDAY=5SUfrom 2058, which never yields 2128-02-29.from.Closes #37
🤖 Generated with Claude Code