mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 08:18:10 +01:00
/activity threw away its hydrated tree on every viewport, in production only.
This was written off earlier as "intermittent, diminishing returns". It was
neither — it was two faults of the same kind, one of them rare enough to look
like noise.
MEASURED, on ONE page load: 1798 words server, 1798 words client, and every
clock differing by exactly two hours.
server Sent 10:18 Sent 09:54 Partial 04:00
client Sent 12:18 Sent 11:54 Partial 06:00
UTC on the box, Europe/Zurich in the browser. Five formatters in
activity-shared.ts called toLocaleTimeString/toLocaleDateString with no
`timeZone`, so they read the RUNTIME's zone — and their callers
(ActivityEventRow, EventStream, NeedsYouCard, ActivityTimeline) are
"use client", so they render in both processes.
Pinned to DEFAULT_TIMEZONE, which already exists as the app's SSOT. Pinning
beats the mounted-gate the greeting uses: these are PAST events, not "now", so
there is no reader-dependent answer to defer to and deferring would only trade
a wrong first paint for an empty one.
formatDayHeading carried the same fault in a two-hour window: it compared
`new Date(y, m-1, d)` against now.getFullYear()/getMonth()/getDate(), all
runtime-zone, so between 00:00 and 02:00 Zurich the box was still on yesterday
while the browser was already on today. THAT is why the failure presented as
intermittent — one run in three. It now compares calendar-date strings in a
fixed zone, and formats the fallback as UTC midnight rendered in UTC, because
a bucket key names a calendar day rather than an instant.
The first hypothesis was WRONG and is worth recording: the pulse's
"busiest: 20 at 09:21" label looked like the culprit, but ActivityPulse is a
server component and the client never re-renders it. Two measurement attempts
were wrong before one worked — a detached DOMParser document has no layout so
innerText collapsed the page to a single line, and rendering with JS disabled
skipped everything because React streams suspended content into `<div hidden>`
that only a script unhides. Both made the page look like it had no server
render at all.
The gate (scripts/test/activity-time-zone-stable.ts) runs the formatters in
three child processes — UTC, Europe/Zurich, Pacific/Honolulu — and compares
their output. Grepping the source for "timeZone" would pass a formatter pinned
to the WRONG zone and would say nothing about date arithmetic. Its fixture is
22:30 UTC deliberately: that is 00:30 the next day in Zurich, so a noon
fixture would pass on the broken code.
Mutation-proven: remove the pins → 4 of 5 fail, reporting 22:30 / 00:30 /
12:30 — the production symptom exactly; restore → 5/5. Picked up automatically
by test-unit's directory glob.
pnpm run verify green.
Claude-Session: https://claude.ai/code/session_01UvjGNAS9CMfEGNW26tUR4P
Co-authored-by: Mao Nakamoto <41178744+maonakamoto@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
107 lines
4.2 KiB
TypeScript
107 lines
4.2 KiB
TypeScript
/**
|
|
* The activity time formatters must produce the SAME string in every process
|
|
* timezone, because they run in two processes with different ones.
|
|
*
|
|
* WHY THIS EXISTS
|
|
*
|
|
* activity-shared.ts formats with `toLocaleTimeString(APP_LOCALE, …)` and no
|
|
* `timeZone`, which reads the RUNTIME's zone. The components calling them are
|
|
* "use client", so they render on the box (UTC) for the HTML and in the browser
|
|
* (Europe/Zurich) on hydration. The server wrote "Sent 10:18" and the client
|
|
* wrote "Sent 12:18"; React discarded the tree — #418 on /activity at every
|
|
* viewport, production only, invisible in dev because dev server and dev
|
|
* browser share a clock.
|
|
*
|
|
* Measured on one production page load 2026-09-05: 1798 words server, 1798
|
|
* client, every clock differing by exactly two hours.
|
|
*
|
|
* `formatDayHeading` had the same fault in a two-hour window: it compared
|
|
* `new Date(y, m-1, d)` against `now.getFullYear()/getMonth()/getDate()`, all
|
|
* runtime-zone, so between 00:00 and 02:00 Zurich the box was still on
|
|
* yesterday while the browser was on today. That is why the failure looked
|
|
* intermittent before the whole file was pinned.
|
|
*
|
|
* The check runs the formatters in two child processes — TZ=UTC and
|
|
* TZ=Europe/Zurich — and compares. Reading the source for the string
|
|
* "timeZone" would pass on a formatter that pins the WRONG zone, and would say
|
|
* nothing about the date arithmetic in formatDayHeading.
|
|
*
|
|
* Run: npx tsx scripts/test/activity-time-zone-stable.ts
|
|
*/
|
|
import { spawnSync } from "node:child_process";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
// Static, not a top-level `await import`: tsx transforms this to CJS, which
|
|
// cannot require an async module (ERR_REQUIRE_ASYNC_MODULE). The module is
|
|
// pure — no DB, no React — so importing it in both modes costs nothing.
|
|
import {
|
|
formatClockTime,
|
|
formatActivityTime,
|
|
formatPulseBucketLabel,
|
|
formatDayHeading,
|
|
} from "../../src/components/activity/activity-shared";
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
const repoRoot = resolve(here, "../..");
|
|
const SELF = fileURLToPath(import.meta.url);
|
|
|
|
// An instant that lands on DIFFERENT calendar days in the two zones: 22:30 UTC
|
|
// is 00:30 the NEXT day in Zurich. A fixture at noon would pass under a broken
|
|
// implementation, which is the whole failure mode being guarded.
|
|
const INSTANT = "2026-09-05T22:30:00.000Z";
|
|
const DAY_KEY = "2026-09-06";
|
|
|
|
// Child mode: print what the formatters produce in THIS process's zone.
|
|
if (process.env.__ACTIVITY_TZ_PROBE === "1") {
|
|
console.log(
|
|
JSON.stringify({
|
|
clock: formatClockTime(INSTANT),
|
|
full: formatActivityTime(INSTANT),
|
|
bucketDay: formatPulseBucketLabel(INSTANT, "day"),
|
|
bucketMonth: formatPulseBucketLabel(INSTANT, "month"),
|
|
heading: formatDayHeading(DAY_KEY, new Date(INSTANT)),
|
|
}),
|
|
);
|
|
process.exit(0);
|
|
}
|
|
|
|
function runIn(tz: string): Record<string, string> {
|
|
const res = spawnSync(join(repoRoot, "node_modules/.bin/tsx"), [SELF], {
|
|
env: { ...process.env, TZ: tz, __ACTIVITY_TZ_PROBE: "1" },
|
|
encoding: "utf8",
|
|
cwd: repoRoot,
|
|
});
|
|
if (res.status !== 0) {
|
|
console.error(` ✗ probe failed under TZ=${tz}: ${(res.stderr || "").slice(0, 400)}`);
|
|
process.exit(1);
|
|
}
|
|
return JSON.parse(res.stdout.trim().split("\n").pop() ?? "{}");
|
|
}
|
|
|
|
const utc = runIn("UTC");
|
|
const zurich = runIn("Europe/Zurich");
|
|
const honolulu = runIn("Pacific/Honolulu"); // a zone on the other side of the date line
|
|
|
|
let pass = 0;
|
|
let fail = 0;
|
|
for (const key of Object.keys(utc)) {
|
|
const a = utc[key];
|
|
const b = zurich[key];
|
|
const c = honolulu[key];
|
|
if (a === b && b === c) {
|
|
pass++;
|
|
console.log(` ✓ ${key}: "${a}" in every process timezone`);
|
|
} else {
|
|
fail++;
|
|
console.error(` ✗ ${key} depends on the process timezone:`);
|
|
console.error(` TZ=UTC → "${a}"`);
|
|
console.error(` TZ=Europe/Zurich → "${b}"`);
|
|
console.error(` TZ=Pacific/Honolulu→ "${c}"`);
|
|
console.error(` A "use client" component renders in BOTH the server's`);
|
|
console.error(` zone and the reader's, so this is a hydration mismatch.`);
|
|
}
|
|
}
|
|
|
|
console.log(`\n${pass} passed, ${fail} failed`);
|
|
process.exit(fail === 0 ? 0 : 1);
|