Skip to content

feat(expand): start rule walks near the window - #39

Merged
PhilflowIO merged 8 commits into
mainfrom
feat/expand-from
Nov 18, 2026
Merged

PhilflowIO merged 8 commits into
mainfrom
feat/expand-from

Conversation

@PhilflowIO

@PhilflowIO PhilflowIO commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

What

With from, expandOccurrences no longer walks an open-ended rule from DTSTART. Cost now depends on the window, not on how old the series is.

  • Yearly rules of fixed days are computed without ical.js: 45 units per instance in the window, 0 when there is none. That means FREQ=YEARLY with no COUNT and no parts except BYMONTH and a BYMONTHDAY that every month the rule takes has (birthdays, anniversaries, including BYMONTH=5;BYMONTHDAY=30). See fixedYearlyWalls in src/recur.ts.
  • Any other rule without COUNT starts its walk at DTSTART moved forward by whole FREQ×INTERVAL periods, landing two periods before the one holding from. Instances before from are dropped (iterationStart).
  • A rule whose UNTIL lies before from is not walked.
  • Walked from DTSTART in full (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.

Shape N=1 300 2,500 5,000
before (origin/main), any shape 3,015 904,500 7,537,500 15,075,000
FREQ=YEARLY, spread 0 225 2,115 4,275
FREQ=YEARLY;BYMONTH=m;BYMONTHDAY=d, spread 0 225 2,115 4,275
FREQ=YEARLY, all 5,000 on 06-03 (all in the week) 45 13,500 112,500 225,000
walked shape BYMONTH=6;BYDAY=1SU 135 40,500 337,500 675,000

So 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_rules once, 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 from refuses exactly when the same call without from refuses. Three mechanisms enforce this:

  • ical.js' setup always runs at DTSTART before a moved walk or the arithmetic. A rule it refuses there is refused; a rule it sets up to yield nothing yields nothing.
  • A moved start that ical.js refuses is walked again from DTSTART, which then decides. Tested with MONTHLY;BYDAY=5SU,5FR: the start moves to 1 April 2024, which ical.js refuses, while DTSTART in 2020 is accepted.
  • Rules where ical.js can throw or stop partway through a walk, depending on where the walk began, are walked in full.

The property test checks both directions over random rules: if the call without from refuses, every call with from refuses with the same message, and otherwise every call with from completes and returns the filtered full result.

Tests (box, --maxWorkers=4)

  • Full suite: 619 passed, 3 skipped. tsc --noEmit is clean.
  • test/expandFrom.test.ts with SERIES_PROPERTY_RUNS=1500, seeds 1–8: 30/30 each. After the last test was added: 31/31.
  • New: a property test of the fixed-days arithmetic against the full walk (all four shapes, INTERVAL, UNTIL, four time forms; the cost must be exactly 45 × instances), a 5,000-birthday budget test, the refusal cases above, and a deterministic test of the retry path.

Review fixes (commits 9b429ad, 36c4942)

  • MONTHLY with a fifth or fifth-last weekday (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).
  • A moved start whose setup ends the rule is walked from DTSTART. Example: YEARLY;BYYEARDAY=366 starting in 2097 finds no 29 February within the three years ical.js checks. This is covered by a fixed case and by a random day-366 generator that goes through the restart.
  • The worked-out yearly days are now yielded and charged one at a time. Before, a window of 336 days a year up to the year 200000 took 14.5 s and 562 MB before the budget was checked. A test now bounds this by units and heap.
  • A DTSTART before the year 100 is walked from DTSTART. ical.js' Time.toUnixTime goes through Date.UTC, which reads those years as 1900–1999. The year start in the computed days no longer uses Date.UTC either.
  • Reviewer's oracle, re-run:
    • walk mode: 0 of 16,000 rules differ;
    • MONTHLY/YEARLY ordinal dimensions: 0 of 18,000;
    • fixed mode: 0 contract (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.
  • Full suite: 626 passed, 3 skipped. Seeds 1–8 at 1,500 runs: 37/37 each.
  • ical.js defects are noted in Stop ical.js rule quirks from yielding wrong occurrences #36.

Re-review fixes (commits on top of 36c4942)

  • ical.js' silent give-up is now reported. RecurIterator.next ends 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 gives complete: false, stoppedBy: 'library', and a write check on such a series fails closed. Example: FREQ=YEARLY;BYMONTH=2;BYDAY=5SU from 2058, which never yields 2128-02-29.
  • YEARLY (and MONTHLY) with a ±5 weekday or a BYSETPOS of 5 or more is walked from DTSTART. ical.js' setup searches years without that cut, so a moved start found what the walk from DTSTART never reaches.
  • Re-run on the box (detached):
    • gap sweep: 0 of 6,000 differ (was 32);
    • ±5 MONTHLY and YEARLY: 0 of 6,000;
    • walk seeds 1 and 3: 0;
    • year-200000 test: about 300,000 units and ≤8 MB heap, with or without from.
  • Full suite: 627 passed. Seeds 1–8 at 1,500 runs: 38/38 each. Noted in Stop ical.js rule quirks from yielding wrong occurrences #36.

Closes #37

🤖 Generated with Claude Code

PhilflowIO and others added 2 commits November 18, 2026 09:12
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]>
PhilflowIO and others added 6 commits November 18, 2026 10:33
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]>
@PhilflowIO
PhilflowIO merged commit 9735bc3 into main Nov 18, 2026
5 checks passed
@PhilflowIO
PhilflowIO deleted the feat/expand-from branch November 18, 2026 10:13
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.

Start expansion near the window for open-ended series

1 participant