Skip to content

Commit e9fb098

Browse files
test(scenarios): walk the working-day calendar once in the run-date oracle (#34104)
<!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9KP2AnyfX9ZJYh48bUsyqbo)_ Fixes #34081 Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:只改 `packages/qa/scenarios/test/runner/run-date.test.ts` 的工作日 oracle。它仍然逐日走日历(独立于实现的 `workingDayNumber` 算术),但只走一次,走成 1960–2040 的工作日序号表;`walkShift`/`walkCount`/`onOrBefore`/`onOrAfter` 改为查表。 - 为什么改:原 oracle 每张卡、每个日期叶子、每个运行日都从头逐日走,2028 三个运行日离卡的今天约 380 个工作日,在 CI 上每例 5.4–7.0 s,超过 vitest 默认 5000 ms,把合并队列里的 PR 踢出去。 - 风险与回滚:只动测试文件,产品代码不变。超时、跳过、RUN_DATES、卡片数量都没动。回滚即 revert 本 PR。 - 先看:测试文件第 48 行起的 oracle 段(逐日走的 `step*` 保留,下面是一次建表的 `calendar()` 和查表函数),以及新加的自检用例 `the calendar walked once reads the same as stepping through it…`。 - 两分钟验证:`pnpm --filter @objectstack/scenarios exec vitest run --reporter=verbose test/runner/run-date.test.ts`,35 项全过,2028 三例本地约 330–360 ms(修前约 1 s)。 - 需要你做的:无,席位落地。 ## What changes The oracle of `every dated card, rebased onto each run date` read each moved date by stepping through the calendar one day at a time, from scratch, for every date leaf of every card on every run date. The step count grows with the distance from the card's `asOf` to the run date, so the three 2028 run dates (about 380 working days out) were the slowest: about 1 s each locally and 5.4–7.0 s on CI runners, past vitest's 5000 ms default. The fix keeps the same reading and takes the walk once: - `calendar()` steps from 1960-01-01 to 2040-12-31 a day at a time with `shiftDate(d, 1)` and the test's own `weekday`/`isWorking`, the same primitives the stepping oracle used. It numbers the working days in order and records, for every calendar day, the number of the working day on or before it. It is built lazily on first use. - `onOrBefore`, `onOrAfter`, `walkCount` and `walkShift` read that table. They keep the stepping semantics, including the edges: a weekend `d` moved forward counts from the Friday before it and moved back from the Monday after it, `n = 0` returns `d` as given, and a time suffix is carried through. A date or result outside the walked range throws instead of reading `undefined`. - The stepping walkers stay, renamed `stepOnOrBefore`, `stepOnOrAfter`, `stepCount` and `stepShift`. They are now the reference for a new self-check. - The implementation's `workingDayNumber` arithmetic is still not used by the oracle, and run-date.ts's functions are not swapped in for it. Nothing else changed. The per-test timeout, RUN_DATES (all nine), the cards and the assertions of every existing case are the same. ## Tests - **New:** `whole working days > the calendar walked once reads the same as stepping through it, across the epoch week, a year end and a leap day`. It compares table against stepping walk on: - every day within ±7 of 1970-01-01, 2026-12-31 and 2028-02-29, weekends included, plus two dates with a time suffix: `onOrBefore`/`onOrAfter`, and `walkShift` by −6, −1, 0, 1 and 6; - long hops: 1969-12-31 +7, 2026-09-30 +380, 2027-01-01 −380, 2028-02-26 (a Saturday) −260, 2028-02-29 +260; - `walkCount` both ways over the epoch week, 2026-09-30 ↔ 2028-03-31, 2028-03-04 ↔ 2026-12-26 and 2028-02-26 ↔ 2028-03-04, with weekend endpoints; - the two ends of the walked range, which throw. - **Changed by the fix:** none. Every existing case reads the four helpers by the same names and gets the same answers. ## Verification Per-case times of `every dated card, rebased onto each run date`, `--reporter=verbose`, local, `--maxWorkers=2`, through `os-verify-lock`: | run date | before (main `63db7240a`) | after (this head) | ablation: stepping walk put back | |---|---|---|---| | 2026-11-15 | 433 ms | 394 ms | 455 ms | | 2026-11-30 | 432 ms | 395 ms | 426 ms | | 2026-12-31 | 451 ms | 362 ms | 571 ms | | 2027-01-01 | 475 ms | 374 ms | 474 ms | | 2027-02-28 | 554 ms | 348 ms | 585 ms | | 2027-03-01 | 612 ms | 333 ms | 528 ms | | **2028-02-28** | **971 ms** | **334 ms** | **1027 ms** | | **2028-02-29** | **1001 ms** | **357 ms** | **966 ms** | | **2028-03-31** | **1119 ms** | **327 ms** | **1050 ms** | | file `tests` total | 6.40 s | 3.62 s | 6.52 s | - After the fix the nine cases are flat at 327–395 ms; the cost no longer grows with the run date's distance. The rest is the rebase and seed reads themselves. At the CI-to-local ratio the card's numbers imply (5.4–7.0 s against about 1.0 s, so roughly 5.5–6.3×), the 2028 cases come to about 2.0–2.3 s against the 5000 ms default. - The calendar table is built inside the first working-day case, which takes 61 ms in total. - **Ablation**, through `scripts/ablation-replace.mjs`: `walkCount` pointed back at `stepCount` and `walkShift` at `stepShift`, both mutations landed (anchor 1 → 0, blob changed). The 2028 cases go back to 1027/966/1050 ms. The one red is the new self-check's out-of-range assertion, which the unbounded stepping walk cannot meet, as expected. All other 34 pass. Restored, `git diff HEAD` empty. Local subset at head `8dd3edfc0a`, derived by `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack` on this diff: - `dispatch-gates --ran`: 48 derived, 48 run, 0 NOT-MEASURED. One first answered exit 3 (prerequisite not met) and was re-run after the prerequisite was built: `pnpm check:dual-build-cjs-loads` after a full `pnpm build` (119/119 tasks), exit 0. All others exit 0 on the first run. - `packages/qa/scenarios`: `pnpm build` exit 0; `vitest run` 34 files, 884 tests, exit 0; `tsc --noEmit -p tsconfig.json` (includes `test/**`) exit 0. - `pnpm exec eslint packages/qa/scenarios/test/runner/run-date.test.ts`: exit 0. - CI families are read off this PR's check runs by the seat. Full record: `/mnt/project-files/devx-seat1/34081-gates-ran.txt`. ## Human Minimum - [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic. (not touched) - [x] 2. Every name I added uses the casing for its kind. (`calendar`, `numberOf`, `dayOf`, `stepShift`, `stepCount`, `stepOnOrBefore`, `stepOnOrAfter`: camelCase; `CALENDAR`: constant) - [x] 3. No formatter ran; `pnpm lint` passes. (eslint on the one touched file: exit 0; the repo-wide run is CI's) - [x] 4. One issue, one branch off an up-to-date `main`, one pull request. - [x] 5. A change that publishes carries a changeset. (test-only, nothing publishes: `skip-changeset`) - [x] 6. `packages/spec` touched: its generated artifacts are regenerated and committed. (not touched) - [x] 7. No hand edits to `content/docs/references/`, `packages/*/CHANGELOG.md` or `content/docs/releases/`. - [x] 8. Title, body and comments are in English. (the 维护者速读 block is the seat's required Chinese summary) - [x] The *Before Submitting* list in CONTRIBUTING.md is done. --- 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01ADHkmRWUrXgua6hAwuKweu <!-- signed: domain:devx#2 · os-steve · session_01ADHkmQTVjryTSkqydKVS6z --> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 86350e1 commit e9fb098

1 file changed

Lines changed: 69 additions & 9 deletions

File tree

‎packages/qa/scenarios/test/runner/run-date.test.ts‎

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,24 @@ const RUN_DATES = ['2026-11-15', '2026-11-30', '2026-12-31', '2027-01-01', '2027
4545

4646
const ISO = /^\d{4}-\d{2}-\d{2}$/;
4747

48-
// ── an independent working-day reading: one calendar day at a time, skipping Saturday and Sunday ──
48+
// ── an independent working-day reading: the calendar walked one day at a time, skipping Saturday and Sunday ──
4949
const weekday = (d: string) => new Date(`${d.slice(0, 10)}T00:00:00Z`).getUTCDay();
5050
const isWorking = (d: string) => weekday(d) !== 0 && weekday(d) !== 6;
51-
/** The working day on or before `d`. */
52-
const onOrBefore = (d: string): string => (isWorking(d) ? d : onOrBefore(shiftDate(d, -1)));
53-
/** The working day on or after `d`. */
54-
const onOrAfter = (d: string): string => (isWorking(d) ? d : onOrAfter(shiftDate(d, 1)));
55-
/** Working days from `from` to `to`, both working days, counted by walking the calendar. */
56-
function walkCount(from: string, to: string): number {
51+
52+
// The walk, step by step: the reading itself, kept to check the calendar table below against.
53+
/** The working day on or before `d`, stepping back a day at a time. */
54+
const stepOnOrBefore = (d: string): string => (isWorking(d) ? d : stepOnOrBefore(shiftDate(d, -1)));
55+
/** The working day on or after `d`, stepping on a day at a time. */
56+
const stepOnOrAfter = (d: string): string => (isWorking(d) ? d : stepOnOrAfter(shiftDate(d, 1)));
57+
/** Working days from `from` to `to`, counted by stepping through the calendar. */
58+
function stepCount(from: string, to: string): number {
5759
let n = 0;
5860
for (let d = from; d < to; d = shiftDate(d, 1)) if (isWorking(shiftDate(d, 1))) n++;
5961
for (let d = from; d > to; d = shiftDate(d, -1)) if (isWorking(d)) n--;
6062
return n;
6163
}
62-
/** `d`, a working day, `n` working days on, by walking the calendar. */
63-
function walkShift(d: string, n: number): string {
64+
/** `d` moved `n` working days, by stepping through the calendar. */
65+
function stepShift(d: string, n: number): string {
6466
let t = d.slice(0, 10);
6567
for (let k = n; k !== 0; ) {
6668
t = shiftDate(t, Math.sign(k));
@@ -69,6 +71,45 @@ function walkShift(d: string, n: number): string {
6971
return t + d.slice(10);
7072
}
7173

74+
// The same walk taken once: every calendar day from CALENDAR[0] to CALENDAR[1], stepped a day at a
75+
// time, numbering the working days in order. Every card date of every run date falls well inside it.
76+
const CALENDAR = ['1960-01-01', '2040-12-31'] as const;
77+
let table: { days: string[]; atOrBefore: Map<string, number> } | undefined;
78+
function calendar() {
79+
if (table) return table;
80+
const days: string[] = [];
81+
const atOrBefore = new Map<string, number>();
82+
for (let d: string = CALENDAR[0]; d <= CALENDAR[1]; d = shiftDate(d, 1)) {
83+
if (isWorking(d)) days.push(d);
84+
atOrBefore.set(d, days.length - 1);
85+
}
86+
return (table = { days, atOrBefore });
87+
}
88+
/** The number of the working day on or before `d`. */
89+
function numberOf(d: string): number {
90+
const i = calendar().atOrBefore.get(d.slice(0, 10));
91+
if (i === undefined || i < 0) throw new Error(`${d} is outside the walked calendar ${CALENDAR.join('..')}`);
92+
return i;
93+
}
94+
/** Working day number `i`. */
95+
function dayOf(i: number): string {
96+
const d = calendar().days[i];
97+
if (d === undefined) throw new Error(`working day ${i} is outside the walked calendar ${CALENDAR.join('..')}`);
98+
return d;
99+
}
100+
/** The working day on or before `d`. */
101+
const onOrBefore = (d: string): string => dayOf(numberOf(d)) + d.slice(10);
102+
/** The working day on or after `d`. */
103+
const onOrAfter = (d: string): string => (isWorking(d) ? d : dayOf(numberOf(d) + 1) + d.slice(10));
104+
/** Working days from `from` to `to`, read off the walked calendar. */
105+
const walkCount = (from: string, to: string): number => numberOf(to) - numberOf(from);
106+
/** `d` moved `n` working days, read off the walked calendar: a weekend `d` counts from the next working day back, the last one on. */
107+
function walkShift(d: string, n: number): string {
108+
if (n === 0) return d;
109+
const i = numberOf(d);
110+
return dayOf((isWorking(d) || n > 0 ? i : i + 1) + n) + d.slice(10);
111+
}
112+
72113
/**
73114
* How the rebase must move `value`, a date of card `s` run on `runDate`, read independently of the
74115
* implementation: whole days, or on a `workingDays` card `asOf` onto `runDate` and every other date by
@@ -156,6 +197,25 @@ describe('whole working days', () => {
156197
for (const d of RUN_DATES) expect(workingDaysBetween('2026-09-30', d), d).toBe(walkCount('2026-09-30', onOrBefore(d)));
157198
expect([onWorkingDay('2026-11-13'), onWorkingDay('2026-11-14'), onWorkingDay('2026-11-15T09:00:00Z')]).toEqual([true, false, false]);
158199
});
200+
201+
it('the calendar walked once reads the same as stepping through it, across the epoch week, a year end and a leap day', () => {
202+
const around = (d: string) => Array.from({ length: 15 }, (_, k) => shiftDate(d, k - 7));
203+
const days = ['1970-01-01', '2026-12-31', '2028-02-29'].flatMap(around);
204+
days.push('2026-12-31T08:30:00Z', '2028-02-26T09:00:00Z');
205+
for (const d of days) {
206+
expect([onOrBefore(d), onOrAfter(d)], d).toEqual([stepOnOrBefore(d), stepOnOrAfter(d)]);
207+
for (const n of [-6, -1, 0, 1, 6]) expect(walkShift(d, n), `${d} + ${n}`).toBe(stepShift(d, n));
208+
}
209+
for (const [d, n] of [['1969-12-31', 7], ['2026-09-30', 380], ['2027-01-01', -380], ['2028-02-26', -260], ['2028-02-29', 260]] as const) {
210+
expect(walkShift(d, n), `${d} + ${n}`).toBe(stepShift(d, n));
211+
}
212+
for (const [from, to] of [['1969-12-27', '1970-01-05'], ['2026-09-30', '2028-03-31'], ['2028-03-04', '2026-12-26'], ['2028-02-26', '2028-03-04']] as const) {
213+
expect(walkCount(from, to), `${from} → ${to}`).toBe(stepCount(from, to));
214+
expect(walkCount(to, from), `${to} → ${from}`).toBe(stepCount(to, from));
215+
}
216+
expect(() => walkShift('1959-12-31', 1)).toThrow(/outside the walked calendar/);
217+
expect(() => walkShift('2040-12-31', 1)).toThrow(/outside the walked calendar/);
218+
});
159219
});
160220

161221
describe('the dates an ask writes in its text move with the card', () => {

0 commit comments

Comments
 (0)