calendar: occurrence engine v2 — real rank thresholds, transfers, collisions
Deploy / deploy (push) Successful in 39s

Corrects and completes the occurrence rules, based on a design discussion
plus one concrete data point: St. Anthony Abbot (plain Duplex) was found
outright winning against an ordinary Sunday in the real Monastic 1617
engine, which the old duplex-1-classis-only threshold got wrong.

- FeastClass gains `vigil`, inserted between `simplex` and `semiduplex` —
  one ordering that correctly serves both "does this win against a Sunday"
  (vigil behaves like simplex there) and "which of two saints wins a
  landing-day collision" (vigil beats simplex, loses to semiduplex).
- LiturgicalDay.occurring (a flat OccurringFeast[] that could only ever
  express a losing *sanctoral* candidate) is replaced by `winner:
  DayWinner` + `commemorations: Commemoration[]` — a discriminated list
  that can hold the temporal day itself, one or more sanctoral entries, or
  (not built yet, but the shape already accommodates it) a future octave
  kind.
- commemorations.ts: ordinary Sundays let Duplex+ win outright (Sunday
  commemorated in return), Semiduplex/Vigil transfer elsewhere (too
  substantial a feast to cheapen with a bare commemoration), Simplex stays
  and is commemorated. Privileged Sundays never displace; Duplex-majus+
  commemorated, everything else transfers.
- collision.ts (new): resolves two sanctoral candidates wanting the same
  day (a transfer landing on an already-occupied day, or two native
  saints sharing a date) — duplex > semiduplex > vigil > simplex, loser
  always commemorated, ties favor the native occupant.
- temporal-id.ts (new): maps any date to one of the 52 real Sunday-collect
  ids from the previous commit, so a temporal winner/commemoration can
  actually be looked up, not just labeled "temporal" in the abstract.
- index.ts's resolveDay orchestrates all of it, including the actual
  Monday/Saturday transfer mechanism. Landing on a privileged feria (the
  concrete case: Holy Week, right after Palm Sunday) is explicitly
  deferred rather than guessed at — it needs its own Easter-keyed lookup
  table, the same way the reference engine handles it.

Added the Vigil of St. Lawrence (Aug 9) as real content specifically to
exercise the backward-transfer rule end-to-end: Aug 9, 2026 is a Sunday,
so the vigil transfers cleanly back to Saturday, verified by a new
integration test alongside the unit-level rule and collision tests.
This commit is contained in:
2026-08-10 12:00:01 -04:00
parent 8bb5d0167d
commit 37ac31c3f2
23 changed files with 573 additions and 198 deletions
+93 -35
View File
@@ -1,57 +1,115 @@
import type { LiturgicalDay, OccurringFeast } from './types';
import type { Commemoration, DayWinner, LiturgicalDay, SanctoralIdentity, TemporalCategory } from './types';
import { weekdayOf } from './weekday';
import { resolveSeason, resolveTemporalCategory } from './temporal';
import { resolveTemporalId } from './temporal-id';
import { getSanctoralCandidatesFor } from './feasts';
import { decideOccurrence, compareFeastClass } from './commemorations';
import { decideOccurrence, type OccurrenceResult } from './commemorations';
import { resolveCollision } from './collision';
import { addDays } from './date-math';
/**
* A day's occurrence considered on its own — no awareness of what an
* adjacent day might be trying to transfer in. `resolveDay` calls this for
* the date itself *and* for yesterday/tomorrow (to check for an inbound
* transfer) without ever recursing into their own adjacent-day checks —
* that's what keeps this from being mutually recursive.
*/
function resolveNativeOccurrence(isoDate: string): { temporalCategory: TemporalCategory; result: OccurrenceResult } {
const weekday = weekdayOf(isoDate);
const season = resolveSeason(isoDate);
const temporalCategory = resolveTemporalCategory(isoDate, season, weekday);
const temporalId = resolveTemporalId(isoDate);
// Multiple saints sharing a date is its own small occurrence contest,
// resolved the same way a transfer landing is — see calendar/collision.ts.
const candidates = getSanctoralCandidatesFor(isoDate);
let topCandidate: SanctoralIdentity | null = null;
const clashCommemorations: Commemoration[] = [];
for (const candidate of candidates) {
if (!topCandidate) {
topCandidate = candidate;
continue;
}
const collision = resolveCollision(candidate, topCandidate);
clashCommemorations.push(...collision.commemorations);
if (collision.winner.kind === 'sanctoral') {
topCandidate = { id: collision.winner.id, name: collision.winner.name, rank: collision.winner.rank };
}
}
const result = decideOccurrence(temporalCategory, temporalId, topCandidate);
result.commemorations.push(...clashCommemorations);
return { temporalCategory, result };
}
function applyIncomingTransfer(
candidate: SanctoralIdentity,
temporalCategory: TemporalCategory,
winner: DayWinner,
commemorations: Commemoration[],
): DayWinner {
if (temporalCategory === 'privileged-feria') {
// Deferred: a transfer landing on a privileged feria (the concrete
// case is Holy Week, right after Palm Sunday) needs its own
// Easter-date-keyed lookup table, the same way the reference engine
// handles it — not modeled yet. The transfer is simply not delivered
// here rather than guessed at.
return winner;
}
if (winner.kind === 'temporal') {
return { kind: 'sanctoral', id: candidate.id, name: candidate.name, rank: candidate.rank };
}
const collision = resolveCollision(candidate, winner);
commemorations.push(...collision.commemorations);
return collision.winner;
}
/**
* Resolves everything about a given day *except* hour content — weekday,
* season, temporal precedence category, and any occurring feast. All real
* now: `occurring` combines calendar/feasts.ts's sanctoral candidates (if
* more than one shares a date, the highest-ranked wins that contest too —
* clashes *among* saints aren't otherwise modeled) with
* calendar/commemorations.ts's occurrence decision. A feast that's fully
* superseded (not even commemorated) doesn't appear in `occurring` at all.
* season, temporal precedence category, and the day's real winner plus
* whatever's commemorated alongside it (calendar/commemorations.ts,
* calendar/collision.ts, calendar/transfer signaling all feed into this).
*/
export function resolveDay(isoDate: string): LiturgicalDay {
const weekday = weekdayOf(isoDate);
const season = resolveSeason(isoDate);
const temporalCategory = resolveTemporalCategory(isoDate, season, weekday);
const { temporalCategory, result } = resolveNativeOccurrence(isoDate);
const candidates = getSanctoralCandidatesFor(isoDate);
let topCandidate = candidates[0] ?? null;
for (const candidate of candidates) {
if (compareFeastClass(candidate.rank, topCandidate!.rank) > 0) {
topCandidate = candidate;
}
let winner = result.winner;
const commemorations = [...result.commemorations];
const yesterday = resolveNativeOccurrence(addDays(isoDate, -1));
if (yesterday.result.transfer?.direction === 'forward') {
winner = applyIncomingTransfer(yesterday.result.transfer.candidate, temporalCategory, winner, commemorations);
}
const tomorrow = resolveNativeOccurrence(addDays(isoDate, 1));
if (tomorrow.result.transfer?.direction === 'backward') {
winner = applyIncomingTransfer(tomorrow.result.transfer.candidate, temporalCategory, winner, commemorations);
}
const occurring: OccurringFeast[] = [];
if (topCandidate) {
const { winner, commemorated } = decideOccurrence(temporalCategory, topCandidate.rank);
if (winner === 'sanctoral' || commemorated) {
occurring.push({
id: topCandidate.id,
name: topCandidate.name,
rank: topCandidate.rank,
commemorated: winner === 'temporal',
});
}
}
return { date: isoDate, weekday, season, temporalCategory, occurring };
return { date: isoDate, weekday, season, temporalCategory, winner, commemorations };
}
/**
* The Sunday/feast-vs-ferial split that several hours' propers key off of
* (Prime's capitulum and Preces, so far): a plain weekday with nothing else
* going on gets the "ferial" form, Sunday or an occurring feast gets the
* fuller "Sunday/feast" form. `occurring` is always [] until milestone 4, so
* today this reduces to "is it Sunday" — but the feast-override branch is
* real, not a stub, and will start firing the moment feasts do.
* going on gets the "ferial" form, Sunday or a winning feast gets the
* fuller "Sunday/feast" form.
*/
export function isSundayOrFeast(day: LiturgicalDay): boolean {
return day.weekday === 'sunday' || day.occurring.length > 0;
return day.weekday === 'sunday' || day.winner.kind === 'sanctoral';
}
export type { LiturgicalDay, OccurringFeast, Season, Weekday, FeastClass, TemporalCategory } from './types';
export { compareFeastClass, isAtLeast, decideOccurrence } from './commemorations';
export { resolveCollision } from './collision';
export { resolveTemporalId } from './temporal-id';
export type {
LiturgicalDay,
DayWinner,
Commemoration,
SanctoralIdentity,
Season,
Weekday,
FeastClass,
TemporalCategory,
} from './types';