// THE WORLD CLOCK — the one piece of physics every twin consults (TWIN-PROGRAMMING-MODEL P3). // Time is a GLOBAL fact requiring simultaneous agreement: TTL/expiry logic reads it at serve // time, so no seed-side workaround exists. The clock is a FILE, not a service — every twin // reads it per request (a tiny local read; the serve-path determinism tooth bans network // egress, not filesystem reads), the world runtime owns writing it, and `volter world clock` // is the operator door (set / advance / show). No file, or no env → real wall-clock time, // exactly the pre-clock behavior; a world only has scripted time when its operator says so. // // File format (world-clock.cjs, the one home of it, shared with the injector that gives an application process the // same time): a FROZEN instant, one ISO-8601 line, every read returning exactly that instant; or a RUNNING clock, // ` from `, the World's time at a moment of the machine's, running on at the machine's pace (a // World serving an application). Moving time is an explicit operator act either way, never a background drift. import { getActiveWorldStore, worldStateFile } from './world-store.ts'; import clockForm from '../world-clock.cjs'; export const WORLD_CLOCK_ENV = 'TWIN_WORLD_CLOCK_FILE'; /** The machine's own time: the real Date.now even where the injector gives the process the World's time (inject.cjs * leaves the original under this symbol), so a running clock's offset is applied once. */ const machineNow = (): number => clockForm.machineNow(); /** The world's "now": the clock file's time when one is configured (its frozen instant, or a running clock's time * now); real wall-clock time otherwise. */ export function worldNow(): string { const clock = heldClock(); return new Date(clock ? clockForm.clockNowMs(clock, machineNow()) : machineNow()).toISOString(); } /** Which clock a twin's client reads, declared by the host that runs the twin (ADR 0036 §8): `world` where the client * follows the World's clock (the World runtime with a fault door, the walk harness), `own` where it keeps its own. */ const APP_CLOCK_ENV = 'TWIN_WORLD_APP_CLOCK'; /** The receiver's "now", in epoch milliseconds: the clock a vendor renders a time on when its receiver checks that time * against its own clock (a webhook signature's timestamp, a short-lived token's `iat`/`nbf`/`exp`, a request * signature's window; ADR 0036 §4). The World's time where the app's processes read the World clock * (`TWIN_WORLD_APP_CLOCK=world`), the machine's own where it is `own` (which world-core's `serveHttp` declares * for a twin served on its own); refused where nothing declares whose clock its client reads. An event's * business times stay `worldNow()`. */ export function receiverNow(): number { const declared = process.env[APP_CLOCK_ENV]; if (declared === 'world') return Date.parse(worldNow()); if (declared === 'own') return machineNow(); throw new Error(`${APP_CLOCK_ENV} ${declared === undefined ? 'unset' : `is ${JSON.stringify(declared)}`}: the host running this twin must declare whose clock its client reads (world or own)`); } /** The World's clock as its file holds it, or null where it has none (the machine's time). One read answers both * whether the clock file is there and what it holds, kept while the file's own facts stand (world-store.ts readKept): * every operation asks the time. A configured-but-corrupt clock is a World defect: parseClock fails loudly, never * drifting back to the wall clock (two services disagreeing about now breaks the World). */ function heldClock(): ReturnType | null { const file = worldStateFile('clock'); const store = getActiveWorldStore(); const held = file ? (store.readKept ? store.readKept(file) : store.read(file)) : null; return held === null ? null : clockForm.parseClock(held); } /** Whether the World's time runs on by itself (a running clock, or no clock: the machine's), rather than standing frozen * until it is moved. */ export function worldClockRunning(): boolean { return heldClock()?.kind !== 'frozen'; }