Skip to content

fix(expand): MONTHLY month days on their grid, RFC-checked candidates - #43

Merged
PhilflowIO merged 17 commits into
mainfrom
fix/from-month-end
Nov 18, 2026
Merged

PhilflowIO merged 17 commits into
mainfrom
fix/from-month-end

Conversation

@PhilflowIO

@PhilflowIO PhilflowIO commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

What

Fixes to expandOccurrences for MONTHLY rules whose months may hold none of the rule's days (#42), for rules ical.js gives up on, and for candidates ical.js yields that RFC 5545 rules out. Every claim below is checked against a brute-force RFC 5545 expansion (test/rfcOracle.ts).

#42. FREQ=MONTHLY;INTERVAL=5;BYMONTHDAY=31,-31 from 30 June 1992 gave July/December 2026 without from and March/August with it. RFC 5545 gives March and August. ical.js' setup, in a start month without a listed day, searched on with the BYMONTHDAY list not yet normalised. If the second listed value is negative and longer than the month (-31 in June), that gives day 0, which rolls into 31 May, so the whole grid runs a month early. A moved start lands in another month and gets another grid. The same search gave up after three months, which lost rules like BYMONTHDAY=31;INTERVAL=5 from a June entirely, reported complete. The iteration is now set up in the first month on its own grid that holds a listed day, at the earliest time of day an instance has. That is ical.js' exact setup branch, so no exclusion list is needed.

Release notes: behaviour changes, also without from

  • MONTHLY;BYMONTHDAY without BYDAY whose DTSTART month lacks every listed day: on DTSTART's grid now (was a month early), and rules ical.js left empty now yield (BYMONTHDAY=31;INTERVAL=5 from a June: 31 July 1994, …).
  • No invented 1st-of-month times: BYMONTHDAY=31;BYHOUR=0,23 no longer yields 1 February and 1 April at 23:00.
  • Every MONTHLY and YEARLY candidate is checked against RFC 5545 and dropped if it is no instance; an instance ical.js skips makes the result stoppedBy: 'library'. Each period the iteration passes is compared with RFC 5545's instances. So a complete result is RFC 5545's, or it says it is not. COUNT counts the kept instances; with a skipped instance, a COUNT result stops at the first one it missed:
    • the day ical.js' setup finds untested: INTERVAL=7;BYDAY=5MO;BYSETPOS=2 from 1993-05-08 no longer yields 1993-05-31, BYDAY=5MO;BYSETPOS=5 from 2001-01-01 no longer yields 2001-01-29, and BYMONTH=6;BYDAY=1MO from a December no longer yields that December;
    • BYMONTH with INTERVAL > 1 follows the INTERVAL grid, which ical.js ignored: INTERVAL=24;BYMONTHDAY=-2,28;BYMONTH=11;COUNT=1 from 2007-05-28T01:59 gives DTSTART only (0.8.0: plus 2007-11-28), and INTERVAL=2;BYMONTH=1,2,3,4 gives January and March, no longer every listed month;
    • BYMONTHDAY with BYSETPOS (no BYDAY) picks positions: BYMONTHDAY=31,30;BYMONTH=7,4,10,1;BYSETPOS=3 gives none (0.8.0: every 30th and 31st).
    • YEARLY days a month lacks are dropped: BYMONTH=2;BYMONTHDAY=30 from 2019-01-30 gives DTSTART only (0.8.0: 2 March 2019, 1 March 2020, …, complete).
    • now 'library' instead of complete, because ical.js skips instances: YEARLY BYMONTHDAY without BYMONTH (RFC 5545 takes every month, ical.js DTSTART's only, e.g. YEARLY;BYMONTHDAY=15), MONTHLY BYDAY with BYMONTHDAY and INTERVAL > 1, YEARLY BYSETPOS (ical.js reads it per month), BYMONTH with a fifth weekday. Such a YEARLY rule is no longer worked out by the fixed-day arithmetic, which followed ical.js.
  • BYDAY with a negative BYSETPOS keeps a month's 1st when BYSETPOS names it from the back: BYDAY=SA;BYSETPOS=-5 now gives 1 August 2020.
  • A rule with nothing left to give is complete, not stoppedBy: 'library'. Example: MONTHLY;INTERVAL=2;BYDAY=5MO;BYSETPOS=5 asks for the fifth of at most one Monday, which never comes. Each period after the last instance, up to the range's end, is counted against RFC 5545 for one full cycle of the rule's own periods: 4,800 / gcd(months per period, 4,800), after which month lengths and weekdays have come round. The count is charged to the budget at one unit a month (measured 1.3–4.6 ms for 4,800 months). Rules that ical.js gives up on while instances exist stay 'library', e.g. YEARLY;INTERVAL=7;BYMONTH=2;BYMONTHDAY=29;BYDAY=MO from 2001, whose next instance after 2596 is in 3408. So do shapes it cannot count (BYWEEKNO; an ordinal weekday under BYMONTHDAY/BYYEARDAY).
  • No more hang in ical.js' MONTHLY BYDAY setup; no series 0.8.0 expanded is refused. ical.js searches month by month, without a bound, for a fifth or fifth-last weekday that DTSTART's month lacks, unless an earlier BYDAY entry (in WKST order) already found a day in that month. That search is now modelled step by step from init. Where it would never end, ical.js is not set up. A rule with no instance at all gives DTSTART alone, complete: INTERVAL=24;BYDAY=5MO from February 2019. A rule with instances that ical.js cannot set up is refused as an invalid rule, with from as without, where 0.8.0 never returned: INTERVAL=24;BYDAY=-1TH,5MO from 2019-02-14, INTERVAL=24;BYDAY=1SA,5TU from 2025-02-23. Rules where the search is skipped are expanded: INTERVAL=24;BYDAY=1MO,5TU from 2019-02-04.

Not checked, as ical.js gives them (tsdav-utils#36): BYWEEKNO, an ordinal weekday under BYMONTHDAY or BYYEARDAY (RFC 5545 leaves its scope open), and COUNT read as ical.js counts it (DTSTART aside). FREQ below MONTHLY is unchanged.

dav-mcp: after the release, its test "a fifth Monday by BYSETPOS=5 … is incomplete, not empty" (PR #174) has to expect complete again.

  • YEARLY with BYMONTH, BYDAY and several hours now reports stoppedBy: 'library'. ical.js drops all but one hour there, e.g. YEARLY;BYMONTH=6;BYDAY=MO;BYHOUR=0,9 from 2021-06-07T00:00 yields only the 00:00 instances (8 of 16). 0.8.0 called that result complete; it is now flagged, so consumers see it as truncated.

Tests

Box, node:22 (workdir w134), on the code of f7259ac (dist built there). CI is green on Node 18–26.

  • Full suite: 647 passed, 3 skipped; tsc clean.
  • Seeds: expandFrom + monthEnd + recurOrder at 1,500 runs, seeds 2–4 green. Seed 1's only failure was an ical.js refusal in the month-end property, which 0.8.0 shares; it is now accepted when from refuses alike (f7259ac, rechecked locally on seeds 1 and 3).
  • Hang sweep (the review's 600 rules, each in its own process with a 4 s limit, plus ical.js run exactly as recurIterator builds it): 0 hangs missed, 0 false refusals. 11 rules really hang: 6 have instances and are refused, 5 have none and give DTSTART alone, complete.
  • YEARLY sweep against the review's oracle (4 seeds × 500 rules: BYMONTH, BYMONTHDAY incl. 29–31 and negatives, BYYEARDAY ±366, ordinal BYDAY up to ±53, BYSETPOS, BYHOUR, COUNT, UNTIL; 3 from each): 1,509 complete and all equal to RFC 5545; 491 'library' with no non-instance in them; from 4,527 of 4,527 match; 227 from calls complete in a window the full walk could not vouch for, all equal to RFC 5545 there.
  • Review sweeps (md 3 × 800, never-recurs 3 × 300): no complete result differs from RFC 5545. Every changed result that 0.8.0 got wrong now matches RFC 5545 or is marked 'library'. complete holds per window: a library result can be complete for a later from, because a miss before the window does not touch it. Errors appear only where ical.js refuses as in 0.8.0.
  • Earlier: month-end sweep 0 of 71,892 from mismatches.
  • Speed: expandFrom 300 runs 40.2 s on 0.8.0, 44.2 s on this PR.

No version bump; dist rebuilt on the box (node:22).

Closes #42

🤖 Generated with Claude Code

PhilflowIO and others added 5 commits November 18, 2026 12:22
A MONTHLY;BYMONTHDAY rule without BYDAY whose start month holds none of
the listed days sent ical.js' setup into a search that reads the list
before normalising it to the month: -31 in a June became day 0, rolled
into May, and every later month came one month early. A call with `from`
moved the start into another month and got another grid, so
INTERVAL=5;BYMONTHDAY=31,-31 from 30 June 1992 gave July and December
of 2026 without `from` and March and August with it. RFC 5545 gives
March and August. The same search gave up after three months, so
BYMONTHDAY=31;INTERVAL=5 from a June had no occurrence at all, reported
complete, and it yielded a start month that BYMONTH leaves out.

recurIterator now sets such a rule up on day 1 of the first month on
ical.js' own grid that the rule takes and that holds a listed day, at
the earliest time of day an instance has, or marks it empty when no
month in a 400-year cycle does. That takes ical.js' exact setup branch,
so a moved start keeps the grid too, and no exclusion list is needed.

The walk had a second defect in such months: with a BYHOUR, BYMINUTE or
BYSECOND list, next_month stepped the next time on day 1 of a month
without the day and reported it (BYMONTHDAY=31;BYHOUR=0,23 gave
1 February at 23:00), so the result depended on the months a walk
passed. A step that lands on an unlisted day now ends the day instead.

Tests check these rules against a brute-force RFC 5545 expansion and,
with `from`, against the filtered result.

Closes #42

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ical.js ends a MONTHLY rule after 336 months without an instance and a
YEARLY one after 28 years. Since 0.8.0 that is reported as
stoppedBy 'library', which is right where instances follow the gap (a
fifth Sunday of February, 40 years apart) but wrong for a rule that never
recurs: FREQ=MONTHLY;INTERVAL=2;BYDAY=5MO;BYSETPOS=5 asks for the fifth
of a set of at most one day, and dav-mcp then marks a correct series
incomplete.

holdsInstance counts, per RFC 5545, whether any period after the last
instance ical.js gave, up to the end of the range, holds one. Month
lengths and weekdays repeat every 400 years, so one cycle of periods
settles it. Only where it proves none is the give-up taken as the rule's
end; BYWEEKNO and an ordinal weekday under BYMONTHDAY or BYYEARDAY are
not counted and stay 'library' (fail closed). A property test checks the
count against the brute-force RFC 5545 expansion.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Built on the box in node:22 from the two commits before; CI checks that
dist equals a fresh build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The never-recurs check looked at 4,800 months of the rule's grid, but
the grid returns to the same calendar position only after
lcm(months per period, 4,800) months. YEARLY;INTERVAL=7;BYMONTH=2;
BYMONTHDAY=29;BYDAY=MO from 2001 has a Monday 29 February in 2596 and
next in 3408, 116 periods later; after 57 periods the check called the
rule ended and the expansion complete. It now counts
4,800 / gcd(months per period, 4,800) periods, the least number after
which every period shape has come round (proof in cyclePeriods).

The count is charged to the expansion's budget, one unit a month, as
measured (4,800 months take 1.3 to 4.6 ms). The RFC 5545 reading of a
period moves into periodModel, which the next commit uses as well.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since the previous fixes call more results complete, the candidates
ical.js yields that are no instance matter more. Its MONTHLY setup
yields the day it finds untested (BYDAY=5MO;BYSETPOS=2 gave the fifth
Monday of DTSTART's month; BYMONTH=6;BYDAY=1MO a December's first
Monday). With BYMONTH it steps every listed month whatever INTERVAL
says, and with BYMONTHDAY and no BYDAY it never reads BYSETPOS. Each
is a superset of the instances, so every MONTHLY candidate is now
checked against the RFC 5545 period model and dropped if it is no
instance. COUNT then counts instances only, and a stretch of a whole
cycle without one ends the rule.

ical.js also tested the 1st of each new month only against BYSETPOS
position 1, so BYDAY=SA;BYSETPOS=-5 never gave 1 August 2020. The 1st
is now kept when BYSETPOS names it from the back.

Where ical.js is no superset, the rule is left as it gives it
(tsdav-utils#36): BYDAY with BYMONTHDAY, BYDAY with BYSETPOS and
several times of day, BYMONTH with a fifth weekday, and YEARLY rules.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PhilflowIO and others added 4 commits November 18, 2026 14:11
ical.js 2.2.1 sets up a MONTHLY rule with a fifth or fifth-last weekday
that DTSTART's month lacks by stepping on month by month until one has
it, without a bound. FREQ=MONTHLY;INTERVAL=24;BYDAY=-1TH,5MO from
February 2019 visits only Februaries of odd years, none with five
Mondays, so the RecurIterator constructor never returns: a calendar
object with such a rule hangs the process outside any budget (0.8.0
too). The widened RFC 5545 property test found it.

recurIterator emulates that search for one 400-year cycle before
constructing the iterator and refuses the rule when no month on the
iteration's steps has the weekday, as ical.js refuses other rules it
cannot set up. Refused with `from` as without it.

The RFC property leaves fifth weekdays out: their setup search also
loses first instances (tsdav-utils#36); fixed cases cover the days it
invents.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Built on the box in node:22 from the commits before; CI checks that dist
equals a fresh build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
holdsInstance proves that a rule has nothing left to give, but that only
settles the result where what ical.js gave is trusted. With BYSETPOS,
ical.js invents days where its candidates are not checked against
RFC 5545: YEARLY;INTERVAL=7;BYDAY=5TU;BYSETPOS=2 gives a fifth Tuesday
every seven years, RFC 5545 none (#36). So holdsInstance now counts a
BYSETPOS rule only where keepInstances checks every candidate (shared
predicate candidatesChecked); elsewhere a give-up stays 'library'.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI checks that dist equals a fresh build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@PhilflowIO PhilflowIO changed the title fix(expand): keep MONTHLY month days on their grid fix(expand): MONTHLY month days on their grid, RFC-checked candidates Nov 18, 2026
PhilflowIO and others added 8 commits November 18, 2026 16:03
The fifth-weekday guard refused rules ical.js expands: in its MONTHLY
BYDAY setup, init skips the forward search for a weekday the start
month lacks once an earlier BYDAY entry found a day in that month
(INTERVAL=24;BYDAY=1MO,5TU from 2019-02-04 is valid). It also refused
a rule with no instance at all, which RFC 5545 reads as DTSTART only.

byDaySetupEndless now follows init step by step: BYDAY sorted by
sort_byday_rules (WKST), nthWeekDay counted from Sunday, the
daysInMonth variable it carries across entries, the skip, and
increment_month with BYMONTH's shared position. A search not done after
4,800 steps never is. Only then is ical.js not set up: a rule
holdsInstance proves empty gets an ended iterator (complete, DTSTART
only), any other is refused as before (1SA,5TU from 2025-02-23 with
INTERVAL=24 hangs ical.js). holdsInstance reads a period's year within
the 400-year cycle near 2000, so a long reach stays inside Date's range.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
YEARLY results were still called complete with days ical.js invents:
BYMONTH=2;BYMONTHDAY=30 from 2019-01-30 gave 2 March 2019, 1 March
2020 and so on, where RFC 5545 has no instance. And where ical.js reads
a rule differently it may skip instances, which no check saw.

keepInstances now checks MONTHLY and YEARLY candidates against the RFC
5545 period model and drops non-instances (COUNT counts instances
only, as for MONTHLY). It also compares each period the iteration
passes with the model's instances from its start; one ical.js never
yielded is recorded, and the walk asks for those in [from, the end it
stands for] and then reports stoppedBy 'library'. So a complete result
is RFC 5545's: a YEARLY BYMONTHDAY without BYMONTH (ical.js reads
DTSTART's month only) or a MONTHLY BYDAY with BYMONTHDAY (stepped
whatever INTERVAL says) now says library instead of complete. Such a
YEARLY rule is no longer worked out by fixedYearlyWalls, which followed
ical.js' reading. With every modelled rule checked, the never-recurs
count no longer needs the BYSETPOS restriction of 9848eaf.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Built on the box in node:22; CI checks that dist equals a fresh build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The YEARLY sweep against the review's RFC 5545 oracle still found
complete results that differ from it, all with COUNT or `from`:
- ical.js counts what it yields, and twice where it retries a candidate
  inside next(), which the drop compensation did not cover: a COUNT
  ended one instance early, reported complete.
- an instance ical.js skipped shifts every later count, but the walk
  only asked for misses from `from` on, and kept the shifted occurrences
  after the miss in a library result.
- a rule ical.js sets up at DTSTART to yield nothing skipped the walk
  with `from`, and with it the check for instances it missed.

keepInstances now switches ical.js' count off and counts the kept
instances itself (countReached ends the rule). The walk asks for misses
from DTSTART where the rule has COUNT, removes what it added after the
first miss, and walks a rule set up empty from DTSTART, which ends at
once and runs the check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Built on the box in node:22; CI checks that dist equals a fresh build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
At 1,500 runs seed 1 draws YEARLY;BYMONTHDAY=30,31,-31,-1;BYMONTH=11,10,3,
two of whose days fall on one date: ical.js refuses it ("Same occurrence
found twice"), as 0.8.0 does. The property now requires the same
refusal with `from` instead of failing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The sweep tolerated any message matching "Invalid", which also matches the
package's own new refusal, so a new false refusal could pass unnoticed.

Refs #42

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Four tests asserted wall time (100 ms to 1 s). On a loaded runner one of
them failed at 202 ms on Node 20 although nothing was slow. They now count
what they guard: Intl lookups for the zone scan, ical.js rule steps for the
hostile VTIMEZONE, and the metered expansion steps against WORK_BUDGET.
Each still fails if the guarded scan or walk comes back unbounded.

Refs #42

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@PhilflowIO
PhilflowIO merged commit 281bc97 into main Nov 18, 2026
5 checks passed
@PhilflowIO
PhilflowIO deleted the fix/from-month-end branch November 18, 2026 15:47
@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 with from shifts MONTHLY BYMONTHDAY=31,-31 grid

1 participant