// WORLD STORE — the transport seam for the control-plane's persistence. // // The kernel's on-disk surface is a CLOSED SET of synchronous primitives: JSONL // append-logs (read-all-rows / append-row), whole-file JSON sidecars // (read/write/exists/remove), directory listing, and atomic-write + cross-process // file-lock. `WorldStore` names exactly that set, and nothing more. Route the whole // kernel through it and the twin persists through ANY backend — the local // filesystem today (`FsWorldStore`, the default), an in-memory Map for tests and // serverless proofs (`MemoryWorldStore`), a Durable Object / KV / redis tomorrow — // without touching a line of twin business logic. // // The interface is SYNCHRONOUS on purpose. The kernel's internals (append-then- // project, read-then-append critical sections) are synchronous, and a pack's // request handler is a pure sync function keyed by `root`. Making the store async // would ripple `await` through every one of those call sites and change the handler's // shape — precisely the blast radius this seam exists to avoid. An async backend is // handled at a SEPARATE persistence boundary: `hydrate()` the durable source into a // sync store, run the sync kernel, `flush()` back out. See `hydrateInto`/`flushFrom`. // // NOT part of this store, deliberately: `rateBudget.ts`'s ledger. That is a // client-side guard on REAL vendor calls, keyed by CREDENTIAL and living in the user's // home dir (NOT under the project state root) — it is not twin persistence and never // runs on the serve path. Its bespoke inode-identity lock and reservation semantics // cannot be expressed through this generic seam without weakening them, so it stays on // `node:fs`. See its module header. import { appendFileSync, chmodSync, closeSync, existsSync, fsyncSync, fstatSync, linkSync, ftruncateSync, lstatSync, mkdirSync, openSync, readdirSync, readSync, realpathSync, readFileSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync, writeSync, } from 'node:fs'; import type { Dirent, Stats } from 'node:fs'; import { hostname } from 'node:os'; import { randomUUID } from 'node:crypto'; import { refuseReadOnlyWrite } from './request-scope.ts'; import clockForm from '../world-clock.cjs'; import { basename, dirname, join, resolve } from 'node:path'; // WHAT A LOCK HOLD ASKS (architecture, "What a hold asks is counted"): the file // store's questions counted inside an outermost lock hold and outside one, said at each outermost release where // VOLTER_TWIN_LOG=1 (storage.ts twinLog's gate and line), and not counted otherwise. Each counter counts the calls of // its own method: a readKept that misses its kept text, or meets a link, also counts the read it makes. A hold's // `heldMs` is the time of the hold's body, run once the lock is taken and before it is released, as the lock's release // line times its hold; the wait before the take is the wait line's `waitedMs`. const QUESTION_KINDS = ['generation', 'read', 'readKept', 'stat', 'exists', 'list', 'append'] as const; type QuestionCounts = Record<(typeof QUESTION_KINDS)[number], number>; // Read once, where a process is: a realm with none counts nothing. const questionsCounted = typeof process !== 'undefined' && process.env?.VOLTER_TWIN_LOG === '1'; const noQuestions = (): QuestionCounts => ({ generation: 0, read: 0, readKept: 0, stat: 0, exists: 0, list: 0, append: 0 }); let lockHoldDepth = 0; let questionsHeld = noQuestions(); let questionsUnheld = noQuestions(); function asked(kind: (typeof QUESTION_KINDS)[number]): void { if (questionsCounted) (lockHoldDepth > 0 ? questionsHeld : questionsUnheld)[kind] += 1; } function countedHold(operation: string, fn: () => T): () => T { if (!questionsCounted) return fn; return () => { const began = performance.now(); lockHoldDepth += 1; try { return fn(); } finally { lockHoldDepth -= 1; if (lockHoldDepth === 0) { const held = questionsHeld, before = questionsUnheld; questionsHeld = noQuestions(); questionsUnheld = noQuestions(); try { console.error('[twin:store-lock]', JSON.stringify({ operation, heldMs: performance.now() - began, held, before })); } catch { /* an instrument */ } } } }; } /** Metadata a caller needs about a stored path. `isDirectory` distinguishes a JSON * sidecar from a per-service subdirectory; `size`+`mtimeMs` * drive the append-dedupe index's cache-validity check in storage.ts. */ export type WorldStat = { size: number; mtimeMs: number; isDirectory: boolean; isSymbolicLink?: boolean }; /** Diagnostic context only; it does not change lock acquisition or stale-owner policy. */ export type WorldLockOptions = { retainLiveOwner?: boolean; operation?: string; caller?: string }; /** * Capture at the outermost lock helper, then carry the same caller through nested helpers. * LIMIT THE CAPTURE TO THE FRAMES READ: V8's third frame supplies the caller, * while new Error().stack normally collects up to Error.stackTraceLimit frames (ten by default). * The limit is lowered to three only for Error's own writable numeric data property above three, and restored * only when lowered. Absent, accessor, non-writable and lower limits receive no writes. Existing constructor * and user stack-formatter failure paths remain; this diagnostic optimization changes no lock ownership rule. */ export function storageLockScope(options: WorldLockOptions = {}): WorldLockOptions & { operation: string; caller: string } { let frame: string | undefined; if (!options.caller) { const held = Object.getOwnPropertyDescriptor(Error, 'stackTraceLimit'); const lowered = held !== undefined && 'value' in held && held.writable === true && typeof held.value === 'number' && held.value > 3; if (lowered) Error.stackTraceLimit = 3; try { frame = new Error().stack?.split('\n')[3]; } finally { if (lowered) Error.stackTraceLimit = held!.value as number; } } const location = frame?.slice(frame.lastIndexOf('(') + 1).trim().replace(/^at\s+/, ''); const site = location?.match(/^(.*):(\d+):\d+\)?$/); const caller = options.caller ?? (site ? `${site[1]!.replace(/^file:\/\//, '')}:${site[2]}` : 'unknown:0'); return { ...options, caller, operation: options.operation ?? caller }; } /** * The synchronous persistence seam. Every path is an ABSOLUTE key the caller already * computed (via `worldPaths`/`worldStateRoot`); the store treats it as an opaque key, * so a single active store partitions cleanly by `root` (the root is embedded in the * key) and no per-root wiring is needed. */ export interface WorldStore { /** Whole-file read; `null` when the path does not exist. */ read(path: string): string | null; /** File content split on `\n` (blank lines and the trailing empty included, so a * caller can report 1-based row numbers); `[]` when the path does not exist. */ readLines(path: string): string[]; /** Raw append of exactly `data` (the caller owns any trailing newline). On the fs * backend this is the VOLTER_DURABLE-aware durable append. Does NOT create parents — * call `mkdir` first, matching the historical `appendDurable` contract. */ append(path: string, data: string): void; /** Non-atomic whole-file write, creating parent dirs. Matches the plain * `writeFileSync` sidecar writers (cursors, refs, leases, plans, fork-meta). `secret` makes the * file owner-only (0600 in a 0700 directory) where the backend has permissions. */ write(path: string, data: string, options?: { secret?: boolean }): void; /** Atomic whole-file write (write-temp-then-rename on fs), creating parent dirs. * Used where a reader must never observe a half-written document (state.json). `secret` makes * the file owner-only where the backend has permissions. */ writeAtomic(path: string, data: string, options?: { secret?: boolean }): void; /** Does the path exist? */ exists(path: string): boolean; /** Recursively remove the path (force; a missing path is not an error). */ remove(path: string): void; /** Ensure a directory (and parents) exists. A no-op on backends with implicit dirs. */ mkdir(dirPath: string): void; /** Immediate child names of a directory; `[]` when it does not exist. */ list(dirPath: string): string[]; /** Metadata for a path, or `null` when it does not exist. */ stat(path: string): WorldStat | null; /** The file from byte `start` to its end, decoded as UTF-8 (`start` must fall on a character boundary: a line's * start in a log); `null` when the path does not exist. Optional: a reader of an append-only log uses it to read * only what was appended, and reads the whole file on a store without it. */ readRange?(path: string, start: number): string | null; /** Resolve filesystem aliases for durable ownership; absent on opaque-key stores. */ canonicalPath?(path: string): string; /** `read`, for a small file every operation needs and few change (a branch's pointer, the World's clock): the text * kept from the last read while the path's own facts stand, so an unchanged file costs one check and no read. * Optional: a store without it is read. */ readKept?(path: string): string | null; /** The facts of the generation kept beside a tree's files in `dirPath` (TREE_FILES): one string that differs after * any write of one of them through a store, by this process or another; `null` where the directory has none yet, * which says nothing. Optional: a store without it is asked for each file's own facts. */ generation?(dirPath: string): string | null; /** The generation in `dirPath` now, when exactly one write of a tree's file through a store has moved it since * `before` (a generation this store answered for it); `null` when it cannot say so. A writer that read `before` just * ahead of its own write, holding the directory's history lock, learns that its write was the only one (log.ts * appendToBranchLog). Optional, beside `generation`. */ generationAfterOwnWrite?(dirPath: string, before: string): string | null; /** Run `fn` holding an exclusive lock on `lockPath`, releasing it afterwards. The fs * backend uses the historical cross-process reclaimable file lock; an in-memory, * single-instance backend is a synchronous pass-through. */ withLock(lockPath: string, fn: () => T, options?: WorldLockOptions): T; /** Where this store's World keeps each piece of its own state (worldStateFile): set on a store that is a World of its * own (every `MemoryWorldStore`, so an in-memory walk inside a World reads nothing of the World around it); absent on * the process's own World's store (`FsWorldStore`), whose locations are the ones the runtime gave the process. */ readonly world?: WorldOwnState; } /** A World's own state that the kernel reads, each piece by name (architecture, "A World's own state is located through * the active World"): its clock, its seed identity, its scenario and facts, its completeness record, its instance record * and its data directory. */ export type WorldStateName = 'clock' | 'seed' | 'scenario' | 'facts' | 'completeness' | 'instance' | 'data'; export type WorldOwnState = { [name in WorldStateName]?: string }; /** The variable each piece is named by in a process the runtime starts for its World (runtime.ts sets them all). */ export const WORLD_STATE_ENV: Readonly> = { clock: 'TWIN_WORLD_CLOCK_FILE', seed: 'TWIN_WORLD_SEED_FILE', scenario: 'TWIN_WORLD_SCENARIO_FILE', facts: 'TWIN_WORLD_FACTS_FILE', completeness: 'TWIN_WORLD_COMPLETENESS_FILE', instance: 'VOLTER_WORLD_INSTANCE', data: 'VOLTER_WORLD_DATA', }; /** Where the active World keeps `name`, or undefined where it keeps none: the active store's own World's location where * the store is a World of its own (never the process's environment, which names another World's files), else the * process's own World's, from `env` (the process's environment unless a caller holds its World's env). */ export function worldStateFile(name: WorldStateName, env: Record | undefined = typeof process === 'undefined' ? undefined : process.env): string | undefined { const own = getActiveWorldStore().world; if (own) return own[name] || undefined; return env?.[WORLD_STATE_ENV[name]] || undefined; } /** The files a service's tree is folded from, by name in its state directory (log.ts: the parent log, the branch log, * the branch's pointer), and the generation a store keeps beside them. */ export const TREE_FILES = { parent: 'events.jsonl', branch: 'actions.jsonl', pointer: 'branch.json' } as const; export const TREE_GENERATION = 'tree.generation'; const TREE_FILE_NAMES: ReadonlySet = new Set(Object.values(TREE_FILES)); // ── FsWorldStore — the DEFAULT, byte-identical to the historical node:fs behavior ──── let fsAtomicSequence = 0; /** How long a lock may live before an acquirer treats it as abandoned. Above the 10s * wait timeout so a live-but-slow holder is never reclaimed out from under itself. */ const LOCK_STALE_MS = 60_000; /** * How long an EMPTY lock file is a taker's, between its exclusive create and its holder's record. A taker * writes the record in the call after the create (in a tab's file step, in the same request), and one whose record is * not written gives the lock up and takes it again (withLock), so no live holder holds a lock with an empty record past * that moment. An empty file older than this is a taker that ended between the two calls. */ // Five seconds: in a tab without the kernel's file step the create and the record are two requests of the kernel, // and Twenty's Redis-twin handoffs have exceeded 1 s; and under the 10 s a // waiter waits before it gives up, so a waiter reclaims such a file rather than timing out on it. const EMPTY_RECORD_GRACE_MS = 5_000; /** How many takes whose record could not be written are made before the failure is the caller's. */ const RECORD_ATTEMPTS = 3; type LockHolder = { pid: number; hostname: string; at: string; token?: string; operation?: string; caller?: string; waitingFor?: { path: string; operation: string; since: number }; }; /** The locks this process holds now, by path, and the token each was recorded with (withLock). */ const ownLocks = new Map(); type LockReading = { /** The take's descriptor; null where a kernel's file step took it (fileSteps) and kept none. */ path: string; fd: number | null; holder: LockHolder; /** The bytes of the record the descriptor last wrote: a shorter one is padded to it (recordLockReading). */ recordBytes?: number; nestedWaits: Map; }; const lockReadings: LockReading[] = []; /** * A KERNEL'S FILE STEPS, where this process runs in a tab: browser-substrate's KernelVirtualFS offers them * on the run's tree (`process[Symbol.for('tabnode.run.vfs')]`, under `Symbol.for('browser-substrate.kernel-file-steps')`), * each one request of the kernel's worker that makes, by its own calls in the same order, what this store made as * requests of their own: a lock's take (an exclusive create, its holder's record, a close), its release (the record * read, its token compared, the unlink: SYS_unlink's, with the parent's permission and sticky-bit checks the path * unlink it replaces did not make) and a tree file's append between its generation's two bytes. A write of a * twin took three locks and an append, about thirty requests; these make it seven. Each step answers undefined where * it is not taken (a path the tab keeps outside the kernel, parts too large for one request), and this store then * makes its own calls. Absent on a host's filesystem, which is asked as before. */ type KernelFileSteps = { createExclusive(path: string, data: string, mode: number): boolean | undefined; changeHolding(path: string, needle: string, replacement: string | undefined): { changed: true } | { changed: false; observed: string | null } | undefined; appendBetween(markerPath: string, marker: string, path: string, data: string): boolean; }; function fileSteps(): KernelFileSteps | undefined { const tree = (process as unknown as Record)[Symbol.for('tabnode.run.vfs')] as Record | null | undefined; const offered = tree?.[Symbol.for('browser-substrate.kernel-file-steps')]; return offered && typeof offered === 'object' ? offered as KernelFileSteps : undefined; } /** The bytes a holder's record holds for its token, as JSON.stringify writes them: a key and a string, no space. */ const tokenNeedle = (token: string): string => `"token":${JSON.stringify(token)}`; /** A two-call take whose path did not hold its own record after it: another reclaimed it between the calls. */ const RECLAIMED_BEFORE_RECORD = 'VOLTER_LOCK_RECLAIMED'; /** A take whose record could not be written whole: nothing of it is held. */ const RECORD_NOT_WRITTEN = 'VOLTER_LOCK_UNRECORDED'; /** Where link(2) is not an exclusive create the store can use: the take falls back to two calls. */ const LINK_UNSUPPORTED = new Set(['EPERM', 'ENOTSUP', 'EOPNOTSUPP', 'ENOSYS', 'EXDEV', 'EMLINK', 'EINVAL']); /** * A FILE MADE EXCLUSIVELY WITH ITS RECORD ALREADY IN IT: a lock, or a lock's reclaim record, is never seen * empty while its taker lives, so an empty one is always a dead taker's and is reclaimed (lockIsStale, recoveryIsStale). * - In a tab: the kernel's file step makes it and writes the record in one request (fileSteps). * - On a host: the record is written whole to a private file of the same directory, which link(2) puts at the path, * an exclusive create that fails EEXIST; the private name is then removed. The lock is opened for its descriptor. * Never in a tab: tabnode's link copies over the path (no EEXIST), so it is no exclusive create there. * - Neither (a tab whose kernel offers no step for the path, a host filesystem without link): two calls, the * exclusive create and the record, and the path read back: held only if it holds this take's token. A record that * is not written leaves the file removed only where it is still empty or this take's (read before it is removed). * EEXIST is thrown as the create's; RECLAIMED_BEFORE_RECORD and RECORD_NOT_WRITTEN say a take not held. */ function createRecorded(path: string, text: string, token: string, operation: string | undefined): { fd: number | null; bytes: number } { const bytes = Buffer.byteLength(text); const steps = fileSteps(); const stepped = steps?.createExclusive(path, text, 0o666); if (stepped === true) return { fd: null, bytes }; if (stepped === false) { // made in that one request and its record short. Removed only while the path still holds that short record (a // prefix of this take's own text) or nothing: a stall past the grace may have let another reclaim it and take it lockRecordLine('publish-failed', path, { operation }); let held: string | undefined; try { held = readFileSync(path, 'utf8'); } catch { held = undefined; } if (held !== undefined && (held === '' || text.startsWith(held))) { try { unlinkSync(path); } catch { /* gone */ } } throw Object.assign(new Error(`${path}: its record was not written whole`), { code: RECORD_NOT_WRITTEN }); } const inTab = (process as unknown as Record)[Symbol.for('tabnode.run.vfs')] !== undefined; if (!inTab) { const temporary = `${path}.take.${process.pid}.${randomUUID()}`; let linked = false; try { writeFileSync(temporary, text, { flag: 'wx' }); try { linkSync(temporary, path); linked = true; } catch (error) { if ((error as NodeJS.ErrnoException).code === 'EEXIST') sweepTakeFiles(path); if (!LINK_UNSUPPORTED.has((error as NodeJS.ErrnoException).code ?? '')) throw error; } } finally { try { unlinkSync(temporary); } catch { /* not made, or gone */ } } if (linked) { let fd: number | null = null; try { fd = openSync(path, 'r+'); } catch { fd = null; /* held; its record is then not rewritten */ } return { fd, bytes }; } } const fd = openSync(path, 'wx'); try { writeFileSync(fd, text); } catch (error) { lockRecordLine('publish-failed', path, { fd, operation }, error); try { closeSync(fd); } catch { /* not held */ } let held = ''; try { held = readFileSync(path, 'utf8'); } catch { held = 'gone'; } if (held === '' || held.includes(tokenNeedle(token))) { try { unlinkSync(path); } catch { /* gone */ } } throw Object.assign(new Error(`${path}: its record could not be written`), { code: RECORD_NOT_WRITTEN, cause: error }); } if (readLockHolder(path)?.token !== token) { lockRecordLine('take-reclaimed-before-record', path, { fd, operation }); try { closeSync(fd); } catch { /* not held */ } throw Object.assign(new Error(`${path}: reclaimed before its record`), { code: RECLAIMED_BEFORE_RECORD }); } return { fd, bytes }; } /** * A take's private file a process left at a crash between its link and its removal (`.take..`): * removed where older than the grace, once a process per lock, when its take meets the lock held. It holds nothing. */ const sweptTakes = new Set(); function sweepTakeFiles(path: string): void { if (sweptTakes.has(path)) return; sweptTakes.add(path); const prefix = `${basename(path)}.take.`; try { for (const name of readdirSync(dirname(path))) { if (!name.startsWith(prefix)) continue; const at = join(dirname(path), name); try { if (clockForm.machineNow() - statSync(at).mtimeMs > EMPTY_RECORD_GRACE_MS) unlinkSync(at); } catch { /* gone, or another's to remove */ } } } catch { /* an instrument of tidiness: nothing waits on it */ } } /** A file createRecorded made, let go: its descriptor closed, and the path removed only while it holds the token. */ function releaseRecorded(path: string, token: string, fd: number | null): void { if (fd !== null) { try { closeSync(fd); } catch { /* released below */ } } try { const released = fd === null ? fileSteps()?.changeHolding(path, tokenNeedle(token), undefined) : undefined; if (released?.changed) return; if (released === undefined && readLockHolder(path)?.token === token) unlinkSync(path); } catch (error) { lockRecordLine('release-failed', path, {}, error); } } /** * A leftover reclaim record (`.recovery`) no reclaimer is behind: empty, or recorded by a process of * this host that is not alive, and older than EMPTY_RECORD_GRACE_MS. It held every waiter of its lock until each * timed out, as an empty lock file did. */ function recoveryIsStale(recoveryPath: string): boolean { let file: Stats; try { file = statSync(recoveryPath); } catch { return false; } if (clockForm.machineNow() - file.mtimeMs <= EMPTY_RECORD_GRACE_MS) return false; if (file.size === 0) return true; const holder = readLockHolder(recoveryPath); return holder !== null && holder.hostname === ownHostname() && Number.isInteger(holder.pid) && !pidAlive(holder.pid); } /** * A WORLD'S CARRIED LOCKS, REMOVED AT ITS OPEN. A World booted over retained state may carry lock files * whose takers have ended. An unreadable holder record is reclaimable under the ownership rules below. * * A file is carried only when all of these hold: it is a world-core lock or its reclaim record (`*.lock`, * `*.lock.recovery`) in a service's state directory (`///world`, never a yarn.lock or any * project file); its text is empty or a holder record's beginning (`{"pid"`) that does not parse; and its file time is * before `before`, the start of this boot's claim. A restore sets the time before the claim; a taker that is live now * (a CLI's changeset rebase, a branch World keeping this one as its parent's storage, a two-call taker between its * create and its record) made its file after it. Each removal is still a reclaimer's: a lock's under its `.recovery`, * taken with its record (createRecorded), a held `.recovery` skipped; the file read again, renamed aside, the aside copy * read again and linked back where it now holds a record (EEXIST there is the path taken since, left to its holder). * A record that parses is kept, under its rules (a dead holder's, our own pid's, the 60 s age, which a restore resets: * not this rule's). Answers the paths removed. */ export function removeCarriedLocks(roots: readonly string[], options: { stateDir: string; before: number }): string[] { const removed: string[] = []; const isHolder = (text: string): boolean => Number.isInteger(lockHolderOf('', text)?.pid); const carriedText = (text: string): boolean => { const trimmed = text.trim(); return trimmed === '' || (trimmed.startsWith('{"pid"') && !isHolder(trimmed)); }; /** The file's text where it is carried now (small, older than the claim, unreadable as a record); else undefined. */ const carriedNow = (path: string): string | undefined => { try { const file = statSync(path); if (file.size > 1024 || !(file.mtimeMs < options.before)) return undefined; const text = readFileSync(path, 'utf8'); return carriedText(text) ? text : undefined; } catch { return undefined; } }; /** Renamed aside, read again, put back where a taker recorded itself meanwhile; true when it is gone. */ const setAside = (path: string, text: string): boolean => { const aside = `${path}.carried.${process.pid}.${randomUUID()}`; try { renameSync(path, aside); } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') lockRecordLine('carried-rename-failed', path, {}, error); return false; } let after: string | undefined; try { after = readFileSync(aside, 'utf8'); } catch { after = undefined; } if (after !== undefined && isHolder(after)) { try { linkSync(aside, path); lockRecordLine('carried-restored', path, {}); } catch (error) { lockRecordLine((error as NodeJS.ErrnoException).code === 'EEXIST' ? 'carried-retaken' : 'carried-restore-failed', path, {}, error); } try { unlinkSync(aside); } catch (error) { lockRecordLine('carried-unlink-failed', aside, {}, error); } return false; } storageLockLine(`[world] carried storage lock renamed aside at open path=${path} bytes=${Buffer.byteLength(text)}`); try { unlinkSync(aside); } catch (error) { lockRecordLine('carried-unlink-failed', aside, {}, error); } return true; }; const removeLock = (path: string): void => { const recoveryPath = `${path}.recovery`, recoveryToken = randomUUID(); let recovery: { fd: number | null }; try { recovery = createRecorded(recoveryPath, `${JSON.stringify({ pid: process.pid, hostname: ownHostname(), at: new Date(clockForm.machineNow()).toISOString(), token: recoveryToken })}\n`, recoveryToken, 'carried lock removal'); } catch { return; } // a reclaimer is at work, or the recovery is carried too and goes first (visit) try { const text = carriedNow(path); if (text !== undefined && setAside(path, text)) removed.push(path); } finally { releaseRecorded(recoveryPath, recoveryToken, recovery.fd); } }; const visit = (directory: string, depth: number): void => { let entries: Dirent[]; try { entries = readdirSync(directory, { withFileTypes: true }); } catch { return; } // A directory's carried reclaim records go first: a lock's removal is made under its `.recovery`. const files = entries.filter((entry) => entry.isFile()).map((entry) => entry.name); for (const name of files.filter((name) => name.endsWith('.lock.recovery'))) { const path = join(directory, name), text = carriedNow(path); if (text !== undefined && setAside(path, text)) removed.push(path); } for (const name of files.filter((name) => name.endsWith('.lock'))) { const path = join(directory, name); if (carriedNow(path) !== undefined) removeLock(path); } for (const entry of entries) { if (entry.isDirectory() && depth < 8 && entry.name !== 'node_modules' && entry.name !== '.git') visit(join(directory, entry.name), depth + 1); } }; // Only the services' state directories (`///world`, and `//world`): never a // service's own data beside them (a PostgreSQL cluster, a project tree). for (const root of roots) { const states = [join(root, options.stateDir, 'world')]; try { for (const entry of readdirSync(root, { withFileTypes: true })) if (entry.isDirectory()) states.push(join(root, entry.name, options.stateDir, 'world')); } catch { continue; } for (const state of states) if (existsSync(state)) visit(state, 0); } return removed; } /** This host's name. In a tab (where the kernel's file steps are offered) it is read once per process: every take * records it, and each read was a request of the kernel (its UTS); a tab's process renamed while it runs keeps * recording, and comparing, the name it started with. On a host it is read live, as before: there a rename * (name.local -> name-2.local on macOS) must reach lockIsStale's comparison, or a dead holder recorded under the new * name is never reclaimed, and the read costs no request. */ let hostnameRead: string | undefined; const ownHostname = (): string => (fileSteps() ? (hostnameRead ??= hostname()) : hostname()); function recordLockReading(reading: LockReading): void { try { const text = `${JSON.stringify(reading.holder)}\n`; // a take with no descriptor rewrites its record by path, and only while the file still holds its token: a file a // stale sweep reclaimed and another made is never written over (the descriptor wrote the reclaimed file's own) if (reading.fd === null) { fileSteps()?.changeHolding(reading.path, tokenNeedle(reading.holder.token!), text); return; } // Padded with spaces to the record it replaces, which JSON allows after a value: a write that ends early leaves the // new record's text over the old one's, never a shorter file with the old one's tail (malformed, and a malformed // record keeps the 60 s rule, which a coordinator's live-owner lock never reaches). const bytes = Buffer.byteLength(text), padded = reading.recordBytes !== undefined && bytes < reading.recordBytes ? `${text.slice(0, -1)}${' '.repeat(reading.recordBytes - bytes)}\n` : text; writeSync(reading.fd, padded, 0, 'utf8'); ftruncateSync(reading.fd, Buffer.byteLength(padded)); reading.recordBytes = Buffer.byteLength(padded); } catch (error) { lockRecordLine('rewrite-failed', reading.path, { fd: reading.fd, operation: reading.holder.operation }, error); } } function storageLockLine(line: string): void { try { console.warn(`${line.replace(/[\r\n]/g, ' ')} at=${Date.now()}`); } catch { /* an instrument */ } } /** Failure-only ownership evidence, on the same channel as the existing hold and reclaim warnings. */ function lockRecordLine(stage: string, path: string, fields: Record, error?: unknown): void { try { const cause = error as { name?: unknown; code?: unknown } | null | undefined; console.warn('[world-storage-lock]', JSON.stringify({ at: Date.now(), stage, path, pid: process.pid, hostname: ownHostname(), ...fields, ...(typeof cause?.name === 'string' ? { error: cause.name } : {}), ...(typeof cause?.code === 'string' ? { code: cause.code } : {}), })); } catch { /* an instrument must not change the lock's result */ } } /** Name a bookkeeping phase within an existing acquisition; never acquire another lock. */ export function withStorageLockOperation(path: string, operation: string, fn: () => T): T { let reading: LockReading | undefined; for (let i = lockReadings.length - 1; i >= 0; i -= 1) { if (lockReadings[i].path === path) { reading = lockReadings[i]; break; } } if (!reading) return fn(); const previous = reading.holder.operation; reading.holder.operation = operation; recordLockReading(reading); try { return fn(); } finally { reading.holder.operation = previous; recordLockReading(reading); } } /** A path that is not there: no such entry, or a part of it that is not a directory. */ const absent = (error: unknown): boolean => { const code = (error as NodeJS.ErrnoException | null)?.code; return code === 'ENOENT' || code === 'ENOTDIR'; }; function readLockHolder(lockPath: string, diagnostic?: 'timeout' | 'release'): LockHolder | null { let text: string; try { text = readFileSync(lockPath, 'utf8'); } catch (error) { if (diagnostic) lockRecordLine('read-failed', lockPath, { during: diagnostic }, error); return null; } return lockHolderOf(lockPath, text, diagnostic); } /** A record's text as its holder (readLockHolder's parse); `null` text is no file, read as readLockHolder reads one. */ function lockHolderOf(lockPath: string, text: string | null, diagnostic?: 'timeout' | 'release'): LockHolder | null { if (text === null) { if (diagnostic) lockRecordLine('read-failed', lockPath, { during: diagnostic, code: 'ENOENT' }); return null; } try { return JSON.parse(text) as LockHolder; } catch (error) { if (diagnostic) lockRecordLine('parse-failed', lockPath, { during: diagnostic, bytes: Buffer.byteLength(text) }, error); return null; // missing, empty (mid-write), or malformed } } /** The same ownership account reaches a timeout's client as the storage trace. */ function lockHolderMessage(holder: LockHolder | null): string { if (!holder) return ' (holder record unreadable)'; const since = typeof holder.at === 'string' ? Date.parse(holder.at) : NaN; const wait = holder.waitingFor; return ` (held by pid ${holder.pid} on ${holder.hostname}${Number.isFinite(since) ? ` for ${clockForm.machineNow() - since}ms` : ''}; token=${holder.token ?? '?'} operation=${holder.operation ?? 'unknown'} caller=${holder.caller ?? 'unknown'}${wait ? ` waitingFor=${wait.path} waitingOperation=${wait.operation} waitingSince=${wait.since}` : ''})`; } function pidAlive(pid: number): boolean { try { process.kill(pid, 0); return true; } catch (error) { // ESRCH → no such process (dead); EPERM → exists but not ours to signal (alive) return (error as NodeJS.ErrnoException).code === 'EPERM'; } } /** A lock is stale when its holder is provably gone (same host + dead pid), when its file is empty past * EMPTY_RECORD_GRACE_MS (a taker that ended before recording itself; a live holder never holds an empty record), or * when it has outlived LOCK_STALE_MS. The age falls back to the lockfile's own mtime when the `at` record is * unreadable, while a freshly-created (recent mtime) empty lock is left alone, avoiding a race with the live writer. */ function lockIsStale(lockPath: string, retainLiveOwner = false): boolean { const holder = readLockHolder(lockPath); if (holder && holder.hostname === ownHostname() && Number.isInteger(holder.pid) && !pidAlive(holder.pid)) { return true; } // A lock recorded under this process that it does not hold is left by a failed release or a reused pid. if (holder && holder.hostname === ownHostname() && holder.pid === process.pid && ownLocks.get(lockPath) !== holder.token) { return true; } // An empty record past its grace names no holder, alive or not (EMPTY_RECORD_GRACE_MS): asked before the live // owner's rule, which keeps a lock whose record says who holds it, and an empty one says no one. if (!holder) { try { const file = statSync(lockPath); if (file.size === 0 && clockForm.machineNow() - file.mtimeMs > EMPTY_RECORD_GRACE_MS) return true; } catch { return false; } } if (retainLiveOwner) return false; const recordedAt = holder && typeof holder.at === 'string' ? Date.parse(holder.at) : NaN; let stamp = recordedAt; if (!Number.isFinite(stamp)) { try { stamp = statSync(lockPath).mtimeMs; } catch { return false; // lock vanished — let the next openSync settle it } } return clockForm.machineNow() - stamp > LOCK_STALE_MS; } function sleepSync(ms: number): void { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); } /** The filesystem store: durable append, atomic replacement and reclaimable cross-process locks. */ export class FsWorldStore implements WorldStore { // One filesystem call per read; absent paths return null. read(path: string): string | null { asked('read'); try { return readFileSync(path, 'utf8'); } catch (error) { if (absent(error)) return null; throw error; } } /** * A small file's text, kept while its own facts stand. THE GUARANTEE: the key is the path's own lstat (device, * inode, size, modification and change times); this process's own writes through the store drop what is kept at * once; another process's write is seen when any of those facts moves. That is the guarantee a tree's stamp * already rests on for its logs (their size and modification time), with the inode and change time added: a * replaced file (writeAtomic) is another inode, a file written in place has another time. What it cannot see is a * rewrite to the same size on the same or a reused inode within one tick of the file system's clock, with no check * between; the logs' stamp has the same limit. A link is read through, and not kept. * A text read after a write that came between the lstat and the read is kept under the OLDER key, so the next * check finds the facts moved and reads again: what is kept is never newer than its key, and never served past it. */ private readonly kept = new Map(); readKept(path: string): string | null { asked('readKept'); // A missing file is asked for without an error being made of it: a World with no parent has no branch.json, and // every stamp asks. let own: Stats | undefined; try { own = lstatSync(path, { throwIfNoEntry: false }); } catch (error) { if (!absent(error)) throw error; } if (!own) { this.kept.delete(path); return null; } if (own.isSymbolicLink()) return this.read(path); const key = `${own.dev}:${own.ino}:${own.size}:${own.mtimeMs}:${own.ctimeMs}`; const held = this.kept.get(path); if (held && held.key === key) return held.text; const text = this.read(path); if (text === null) { this.kept.delete(path); return null; } this.kept.set(path, { key, text }); return text; } /** * A TREE'S GENERATION: one file beside a tree's three files, a byte longer after every write of any of them, so a * reader that has the tree's stamp asks ONE question to know it still stands (log.ts treeFacts), where it asked * three, two of them of files a World with no parent does not have. * * WHAT MAKES IT SOUND. A writer appends a byte BEFORE its write and another AFTER it; a reader takes the * generation's facts BEFORE it reads the files' own. The file only grows, so its size never repeats on one inode, * and a replaced or removed file is another inode or none. So for any write W and any reader that kept a stamp S * under generation G: if W's first byte is after G was read, the kept G is behind and S is not served; if it is * before, then either W itself was before S's own reads (S holds it), or W's second byte is after G was read, and * again G is behind. An append is one call the system orders among all writers: each byte is one append of its * own, whole or not at all, on a POSIX filesystem and in a tab's one kernel (browser-substrate * packages/node/src/kernel-virtual-filesystem.ts appendFileSync, an O_APPEND open and writeAll, which sends a write * of at most DATA_BYTES as one SYS_write request the kernel serves whole; or, by a file step, its table's own * O_APPEND open and write of the one byte, filesystem.ts appendFileBytes). The proof rests on that; log.ts * appendToBranchLog's (`generationAfterOwnWrite`: one write is exactly two bytes) as well. * * WHERE THAT STOPS. (1) A writer that dies between its write and its second byte, with a reader's whole check * between its first byte and its write: that reader keeps the older stamp until the next write of the tree. (2) A * writer of these files that does not come through a store of this version (another package's, a copy made over a * live directory by another program): the generation does not move. A directory made or removed whole has no * generation or another's, which serves nothing kept. (3) A stamp that names files outside its own directory (a * World with a parent: its view and its ancestors' logs) is never kept under a generation (log.ts). */ generation(dirPath: string): string | null { asked('generation'); let own: Stats | undefined; try { own = lstatSync(join(dirPath, TREE_GENERATION), { throwIfNoEntry: false }); } catch (error) { if (!absent(error)) throw error; } if (!own || !own.isFile()) return null; return `${own.dev}:${own.ino}:${own.size}:${own.mtimeMs}:${own.ctimeMs}`; } /** One write is `moved` twice, a byte each: the same file, those two bytes longer. */ generationAfterOwnWrite(dirPath: string, before: string): string | null { const now = this.generation(dirPath); if (now === null) return null; const [dev, ino, size] = before.split(':'), [nowDev, nowIno, nowSize] = now.split(':'); return nowDev === dev && nowIno === ino && Number(nowSize) === Number(size) + 2 ? now : null; } /** One more byte on the generation beside `path`, where `path` is one of a tree's files. A directory that is not * there has no reader to tell: the write that makes it is followed by the byte that makes its generation. */ private moved(path: string): void { if (!TREE_FILE_NAMES.has(basename(path))) return; try { appendFileSync(join(dirname(path), TREE_GENERATION), '.'); } catch (error) { if (!absent(error)) throw error; } } /** A write of `path`, with the generation beside it moved before and after where it is one of a tree's files. */ private writing(path: string, write: () => void): void { this.moved(path); try { write(); } finally { this.moved(path); } } readLines(path: string): string[] { try { return readFileSync(path, 'utf8').split('\n'); } catch (error) { if (absent(error)) return []; throw error; } } readRange(path: string, start: number): string | null { let fd: number; try { fd = openSync(path, 'r'); } catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') return null; throw error; } try { const size = fstatSync(fd).size; if (start >= size) return ''; const buffer = Buffer.allocUnsafe(size - start); let read = 0; while (read < buffer.length) { const n = readSync(fd, buffer, read, buffer.length - read, start + read); if (n === 0) break; read += n; } return buffer.toString('utf8', 0, read); } finally { closeSync(fd); } } append(path: string, data: string): void { asked('append'); refuseReadOnlyWrite('the World'); // a read-only request writes nothing, whatever path it takes to the store this.kept.delete(path); // A tree's file in a tab: the generation's byte, the append and the byte again as one request of the kernel // (fileSteps), by its own opens, writes and closes in this order. Not where every append is to be fsynced. if (TREE_FILE_NAMES.has(basename(path)) && process.env.VOLTER_DURABLE !== '1' && fileSteps()?.appendBetween(join(dirname(path), TREE_GENERATION), '.', path, data)) return; // Historical appendDurable: a completed append syscall survives a process crash; // fsync additionally survives kernel-panic/power-loss when VOLTER_DURABLE=1. this.writing(path, () => { const fd = openSync(path, 'a'); try { appendFileSync(fd, data); if (process.env.VOLTER_DURABLE === '1') fsyncSync(fd); } finally { closeSync(fd); } }); } write(path: string, data: string, options: { secret?: boolean } = {}): void { refuseReadOnlyWrite('the World'); // a read-only request writes nothing, whatever path it takes to the store this.kept.delete(path); this.writing(path, () => { if (!options.secret) { mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, data); return; } mkdirSync(dirname(path), { recursive: true, mode: 0o700 }); writeFileSync(path, data, { mode: 0o600 }); chmodSync(path, 0o600); }); } writeAtomic(path: string, data: string, options: { secret?: boolean } = {}): void { refuseReadOnlyWrite('the World'); // a read-only request writes nothing, whatever path it takes to the store this.kept.delete(path); mkdirSync(dirname(path), { recursive: true }); const tmp = `${path}.${process.pid}.${Date.now()}.${fsAtomicSequence++}.tmp`; this.writing(path, () => { try { writeFileSync(tmp, data, options.secret ? { mode: 0o600 } : undefined); renameSync(tmp, path); if (options.secret) chmodSync(path, 0o600); } finally { rmSync(tmp, { force: true }); } }); } exists(path: string): boolean { asked('exists'); return existsSync(path); } remove(path: string): void { refuseReadOnlyWrite('the World'); // a read-only request writes nothing, whatever path it takes to the store for (const held of this.kept.keys()) if (held === path || held.startsWith(`${path}/`)) this.kept.delete(held); this.writing(path, () => rmSync(path, { recursive: true, force: true })); } mkdir(dirPath: string): void { mkdirSync(dirPath, { recursive: true }); } list(dirPath: string): string[] { asked('list'); try { return readdirSync(dirPath); } catch (error) { // No such directory is an empty one, as before; a path that IS there and is not a directory still fails. const code = (error as NodeJS.ErrnoException).code; if (code === 'ENOENT' || (code === 'ENOTDIR' && !existsSync(dirPath))) return []; throw error; } } /** The real path of each path already resolved: a state directory's does not move under a running process, and * every history lock asked for it again. A path that does not exist yet is not kept. */ private readonly canonical = new Map(); canonicalPath(path: string): string { const absolute = resolve(path); const known = this.canonical.get(absolute); if (known !== undefined) return known; try { const real = realpathSync(absolute); this.canonical.set(absolute, real); return real; } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT' || dirname(absolute) === absolute) throw error; return join(this.canonicalPath(dirname(absolute)), basename(absolute)); } } stat(path: string): WorldStat | null { asked('stat'); // The path itself first: where it is no link, that is the whole answer. A link is followed by a second call, // and one that leads nowhere is absent, as it was. // Asked without an error being made of a missing path (a tree's parent log is absent in a World with no parent). let own: Stats | undefined; try { own = lstatSync(path, { throwIfNoEntry: false }); } catch (error) { if (!absent(error)) throw error; } if (!own) return null; if (!own.isSymbolicLink()) return { size: own.size, mtimeMs: own.mtimeMs, isDirectory: own.isDirectory() }; try { const s = statSync(path); return { size: s.size, mtimeMs: s.mtimeMs, isDirectory: s.isDirectory(), isSymbolicLink: true }; } catch (error) { if (absent(error) || (error as NodeJS.ErrnoException).code === 'ELOOP') return null; throw error; } } /** Run `fn` holding an exclusive cross-process file lock. The lockfile records * identity, operation, caller and any nested wait; a contender that finds a stale lock (dead pid or age > * LOCK_STALE_MS) reclaims it by atomically renaming it aside — so a crashed holder * can't wedge the world forever. Reclaim is race-safe: only the process that wins * the rename clears the stale inode, and the exclusive `wx` create still decides the * winner. (Moved verbatim from storage.ts `withFileLock`.) */ withLock(lockPath: string, fn: () => T, options: WorldLockOptions = {}): T { const started = performance.now(); const scope = storageLockScope(options); fn = countedHold(scope.operation, fn); let fd: number | null = null; // A kernel's file step takes the lock with its record in one request and keeps no descriptor (fileSteps). const steps = fileSteps(); // The token is the take's own: its release removes the file only while it still holds this token, so a file a // stale-lock sweep reclaimed and another process re-created is never removed by the first holder. const token = randomUUID(); const holderNow = (): LockHolder => ({ pid: process.pid, hostname: ownHostname(), at: new Date(clockForm.machineNow()).toISOString(), token, operation: scope.operation, caller: scope.caller }); let record: LockHolder | undefined; let taken = false; let unrecordedTakes = 0; let recordBytes: number | undefined; let made = false; let reported = false; let attempts = 0; let sleeps = 0; let sleepMs = 0; let waitedOn: LockHolder | null = null; let nestedWait: { path: string; since: number; holders: LockReading[] } | undefined; const finishNestedWait = (): void => { if (!nestedWait) return; const waitedMs = clockForm.machineNow() - nestedWait.since; for (const reading of nestedWait.holders) { const key = `${nestedWait.path}\0${scope.operation}`; const previous = reading.nestedWaits.get(key); reading.nestedWaits.set(key, { path: nestedWait.path, operation: scope.operation, waitedMs: (previous?.waitedMs ?? 0) + waitedMs }); delete reading.holder.waitingFor; recordLockReading(reading); } nestedWait = undefined; }; const reportWait = (): void => { if (reported || !nestedWait || performance.now() - started < 2_000) return; reported = true; const holder = readLockHolder(nestedWait.path); const since = holder && typeof holder.at === 'string' ? Date.parse(holder.at) : NaN; storageLockLine(`[world] storage lock still held path=${nestedWait.path} holder=${holder?.pid ?? '?'}@${holder?.hostname ?? '?'} token=${holder?.token ?? '?'} operation=${holder?.operation ?? holder?.caller ?? 'unknown:0'} holderMs=${Number.isFinite(since) ? clockForm.machineNow() - since : '?'} waiter=${process.pid} waitingOperation=${scope.operation} caller=${scope.caller}`); }; const slept = (path = lockPath): void => { if (sleeps === 0) waitedOn = readLockHolder(path); if (nestedWait?.path !== path) { finishNestedWait(); nestedWait = { path, since: clockForm.machineNow(), holders: [...lockReadings] }; for (const reading of nestedWait.holders) { reading.holder.waitingFor = { path, operation: scope.operation, since: nestedWait.since }; recordLockReading(reading); } } reportWait(); const sleeping = performance.now(); sleeps += 1; sleepSync(25); sleepMs += performance.now() - sleeping; }; try { while (!taken) { // This runs in the synchronous loop: a blocked holder cannot service a timer. reportWait(); try { attempts += 1; // The holder is recorded WITH the take (createRecorded): no other process sees this lock without its record. record = holderNow(); const taking = createRecorded(lockPath, `${JSON.stringify(record)}\n`, token, scope.operation); fd = taking.fd; recordBytes = taking.bytes; taken = true; } catch (error) { const code = (error as NodeJS.ErrnoException).code; // The lock's directory is made when it is found missing, once, not before every take: it is there for // every take after a state directory's first. if (code === 'ENOENT' && !made) { made = true; mkdirSync(dirname(lockPath), { recursive: true }); continue; } // A two-call take whose path did not hold its record after it (another reclaimed it between): taken again. if (code === RECLAIMED_BEFORE_RECORD) continue; // A take whose record could not be written: made again, a few times, before the failure is the caller's. if (code === RECORD_NOT_WRITTEN) { unrecordedTakes += 1; if (unrecordedTakes >= RECORD_ATTEMPTS) throw Object.assign(new Error(`World storage lock ${lockPath}: its holder's record could not be written`), { code: 'EIO', cause: error }); sleepSync(25 * unrecordedTakes); continue; } if (code !== 'EEXIST') throw error; if (lockIsStale(lockPath, options.retainLiveOwner)) { // Serialize reclaimers, then re-read: a contender's earlier stale observation // must never rename another contender's newly acquired, live lock. The reclaim record is made as a lock // is, with its record and a token of its own, and released by that token. const recoveryPath = `${lockPath}.recovery`; const recoveryToken = randomUUID(); let recovery: { fd: number | null }; try { recovery = createRecorded(recoveryPath, `${JSON.stringify({ pid: process.pid, hostname: ownHostname(), at: new Date(clockForm.machineNow()).toISOString(), token: recoveryToken })}\n`, recoveryToken, 'lock recovery'); } catch (error) { const recoveryCode = (error as NodeJS.ErrnoException).code; if (recoveryCode === RECLAIMED_BEFORE_RECORD || recoveryCode === RECORD_NOT_WRITTEN) continue; if (recoveryCode !== 'EEXIST') throw error; // A reclaim record no reclaimer is behind is reclaimed as an empty lock file is (recoveryIsStale). if (recoveryIsStale(recoveryPath)) { const aside = `${recoveryPath}.stale.${process.pid}.${randomUUID()}`; try { renameSync(recoveryPath, aside); unlinkSync(aside); lockRecordLine('recovery-reclaimed', recoveryPath, {}); } catch (cause) { if ((cause as NodeJS.ErrnoException).code !== 'ENOENT') throw cause; } continue; } if (performance.now() - started > 10_000) throw new Error(`Lock recovery is active or interrupted: ${recoveryPath}${lockHolderMessage(readLockHolder(recoveryPath, 'timeout'))}. Inspect its owner before removing the recovery record.`); slept(recoveryPath); continue; } try { if (lockIsStale(lockPath, options.retainLiveOwner)) { const holder = readLockHolder(lockPath); const salvage = `${lockPath}.stale.${process.pid}.${Date.now()}`; try { renameSync(lockPath, salvage); console.warn(`[world] reclaiming stale storage lock ${lockPath} (held by pid ${holder?.pid ?? '?'} on ${holder?.hostname ?? '?'}) (reclaimer pid ${process.pid} on ${ownHostname()})`); unlinkSync(salvage); } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; } } } finally { releaseRecorded(recoveryPath, recoveryToken, recovery.fd); } continue; } if (performance.now() - started > 10_000) { const holder = readLockHolder(lockPath, 'timeout'); throw new Error(`Timed out waiting for world storage lock: ${lockPath}${lockHolderMessage(holder)}`); } slept(); } } } finally { finishNestedWait(); if (sleeps > 0) { const waitedMs = performance.now() - started; const first = waitedOn as LockHolder | null; storageLockLine(`[world] storage lock wait path=${lockPath} outcome=${taken ? 'acquired' : 'failed'} waiter=${process.pid}@${ownHostname()} operation=${scope.operation} attempts=${attempts} sleeps=${sleeps} requestedSleepMs=${sleeps * 25} sleepMs=${sleepMs} outsideSleepMs=${waitedMs - sleepMs} waitedMs=${waitedMs} firstHolder=${first?.pid ?? '?'}@${first?.hostname ?? '?'} token=${first?.token ?? '?'}`); } } // The holder's record was written with the take, in the loop above: a take whose record could not be written is // never held (EMPTY_RECORD_GRACE_MS). ownLocks.set(lockPath, token); const acquired = performance.now(); const reading: LockReading = { path: lockPath, fd, holder: record!, nestedWaits: new Map(), ...(recordBytes !== undefined ? { recordBytes } : {}) }; lockReadings.push(reading); try { return fn(); } finally { // Every World on the machine waits on some of these locks; a long hold stalls them all. const heldMs = performance.now() - acquired; if (heldMs > 2_000) storageLockLine(`[world] releasing storage lock path=${lockPath} operation=${scope.operation} heldMs=${heldMs} nestedWaits=${JSON.stringify([...reading.nestedWaits.values()])}`); if (heldMs > 2_000) console.warn(`[world] held storage lock ${lockPath} for ${heldMs}ms (pid ${process.pid})\n${new Error().stack?.split('\n').slice(2, 8).join('\n') ?? ''}`); lockReadings.pop(); ownLocks.delete(lockPath); if (fd !== null) { try { closeSync(fd); } catch (error) { lockRecordLine('close-failed', lockPath, { fd }, error); throw error; } } // By the token the take wrote, not by inode: a filesystem may answer a descriptor's stat and the path's with // different identities (a tab's, where a committed path is the store's entry), and a lock released by inode // was then never removed, left recorded as this live process's (above). A kernel's file step reads the record, // compares its token and unlinks in one request; its answer is what the read below would have found. try { const released = fd === null ? steps?.changeHolding(lockPath, tokenNeedle(token), undefined) : undefined; if (!released?.changed) { const observed = released === undefined ? readLockHolder(lockPath, 'release') : lockHolderOf(lockPath, released.observed, 'release'); if (observed?.token === token) unlinkSync(lockPath); else if (observed) lockRecordLine('release-token-mismatch', lockPath, { token, recordedToken: observed.token, recordedPid: observed.pid, recordedHostname: observed.hostname, }); } } catch (error) { lockRecordLine('release-failed', lockPath, { fd }, error); } } } } // ── MemoryWorldStore — a Map-backed store; the proof a non-fs transport works ───────── // A process-global, strictly-increasing stamp source shared by every MemoryWorldStore, so // no two writes (across instances or requests) ever collide on `stat().mtimeMs`. See `bump`. let memoryStoreClock = 0; const byteLength = (text: string): number => new TextEncoder().encode(text).length; /** * An in-memory store. Files are entries in a `Map`; directories are * synthesized from the key set (a path is a directory when it is a strict prefix of a * file key). `withLock` is a synchronous pass-through — a single in-process instance has * no concurrent writers to serialize, so the read-then-append critical sections the * kernel guards are already atomic under the single-threaded event loop. * * This is the portability proof: point the active store here and the entire kernel — and * the clerk twin on top of it — runs with the filesystem untouched. */ export class MemoryWorldStore implements WorldStore { /** A memory store is a World of its own: its state is where `world` says, and what it does not name it has none of * (worldStateFile never falls back to the process's environment for it). The object is the World's: a walk that plays * a scenario names it here. */ readonly world: WorldOwnState; constructor(world: WorldOwnState = {}) { this.world = world; } /** path → content, and path → version stamp driving `stat().mtimeMs`. */ private readonly files = new Map(); private readonly versions = new Map(); /** path → UTF-8 byte size, kept as content changes so `stat` never re-encodes a growing log. */ private readonly sizes = new Map(); /** The stamp of this store's most recent mutation (0 = never mutated). A serverless * entry snapshots this before running the sync kernel and flushes ONLY when it moved — * read-only requests (the common case) then skip the durable write entirely. */ private lastMutation = 0; mutationStamp(): number { return this.lastMutation; } /** Paths whose content changed strictly after `stamp` — the DELTA-FLUSH primitive * for a serverless entry: it records `mutationStamp()` at hydration, * runs the sync kernel, then persists ONLY these paths (plus removals, which the caller * detects by diffing its hydrated path set against `entries()` — a removed path has no * version to report). O(paths) scan, O(delta) write. */ changedSince(stamp: number): string[] { const changed: string[] = []; for (const [path, version] of this.versions) { if (version > stamp && this.files.has(path)) changed.push(path); } return changed.sort(); } private bump(path: string): void { // Draw the stamp from a PROCESS-GLOBAL monotonic counter, not a per-instance one. // storage.ts's append-dedupe cache is keyed by path and validated by (size, mtimeMs), // and it persists across requests in a Worker isolate while a fresh MemoryWorldStore is // created per request (the hydrate→sync-kernel→flush shape). A per-instance counter // resets to 0 each request, so two instances could present the SAME (size, mtimeMs) for // one path with DIFFERENT content — serving a stale index (dedupe bypass) or a false // conflict. A global counter makes every write across every instance a distinct, // strictly-increasing stamp, so a cross-instance collision is impossible. this.lastMutation = memoryStoreClock += 1; this.versions.set(path, this.lastMutation); } read(path: string): string | null { return this.files.has(path) ? this.files.get(path)! : null; } readLines(path: string): string[] { const content = this.files.get(path); return content === undefined ? [] : content.split('\n'); } append(path: string, data: string): void { refuseReadOnlyWrite('the World'); // a read-only request writes nothing, whatever path it takes to the store this.files.set(path, (this.files.get(path) ?? '') + data); this.sizes.set(path, (this.sizes.get(path) ?? 0) + byteLength(data)); this.bump(path); } write(path: string, data: string): void { refuseReadOnlyWrite('the World'); // a read-only request writes nothing, whatever path it takes to the store this.files.set(path, data); this.sizes.set(path, byteLength(data)); this.bump(path); } writeAtomic(path: string, data: string): void { refuseReadOnlyWrite('the World'); // a read-only request writes nothing, whatever path it takes to the store // Atomic by nature in a single process: the assignment is indivisible, so no reader // ever observes a partial document. this.files.set(path, data); this.sizes.set(path, byteLength(data)); this.bump(path); } exists(path: string): boolean { if (this.files.has(path)) return true; // A directory "exists" when some file lives under it. const prefix = path.endsWith('/') ? path : `${path}/`; for (const key of this.files.keys()) if (key.startsWith(prefix)) return true; return false; } remove(path: string): void { refuseReadOnlyWrite('the World'); // a read-only request writes nothing, whatever path it takes to the store let removedAny = false; if (this.files.delete(path)) { this.versions.delete(path); this.sizes.delete(path); removedAny = true; } const prefix = path.endsWith('/') ? path : `${path}/`; for (const key of [...this.files.keys()]) { if (key.startsWith(prefix)) { this.files.delete(key); this.versions.delete(key); this.sizes.delete(key); removedAny = true; } } // A pure removal is a mutation too: without this, a request that only deletes state // would read as clean and the delta-flush would never persist the removal. if (removedAny) this.lastMutation = memoryStoreClock += 1; } mkdir(_dirPath: string): void { // Directories are implicit in the key set; nothing to do. } list(dirPath: string): string[] { const prefix = dirPath.endsWith('/') ? dirPath : `${dirPath}/`; const children = new Set(); for (const key of this.files.keys()) { if (!key.startsWith(prefix)) continue; const rest = key.slice(prefix.length); const slash = rest.indexOf('/'); children.add(slash === -1 ? rest : rest.slice(0, slash)); } return [...children]; } stat(path: string): WorldStat | null { const content = this.files.get(path); if (content !== undefined) { return { size: this.sizes.get(path) ?? byteLength(content), mtimeMs: this.versions.get(path) ?? 0, isDirectory: false }; } if (this.exists(path)) { return { size: 0, mtimeMs: 0, isDirectory: true }; } return null; } withLock(_lockPath: string, fn: () => T): T { return fn(); } /** Test/inspection helper: a plain snapshot of every stored file. Not part of the * `WorldStore` contract — used by `flushFrom` and by tests asserting no fs was touched. */ entries(): Record { return Object.fromEntries(this.files); } } // ── the active store (the injection point) ─────────────────────────────────────────── let activeStore: WorldStore = new FsWorldStore(); /** The request-scoped store: `withWorldStore` runs its callback inside an AsyncLocalStorage * context where the runtime has one (Node, Bun, workerd with nodejs_compat), so concurrent * requests for different worlds in one isolate each see their own store across awaits. Reached * through `process.getBuiltinModule`, never a static import, so a browser bundle stays clean and * falls back to the module-global swap. */ type StoreScope = { getStore(): WorldStore | undefined; run(store: WorldStore, fn: () => R): R }; const scope: StoreScope | null = (() => { try { const hooks = (globalThis as { process?: { getBuiltinModule?: (id: string) => unknown } }).process?.getBuiltinModule?.('node:async_hooks') as { AsyncLocalStorage?: new () => StoreScope } | undefined; return hooks?.AsyncLocalStorage ? new hooks.AsyncLocalStorage() : null; } catch { return null; } })(); /** The store the kernel currently persists through: the request's scoped store, else the * module-global one (`FsWorldStore` by default). */ export function getActiveWorldStore(): WorldStore { return scope?.getStore() ?? activeStore; } /** Swap the active store. A serverless/DO/test entry sets a `MemoryWorldStore` (or a * backend-backed store) here before running the sync kernel, and restores the previous * one afterwards. Returns the store that was active, so callers can restore it. */ export function setActiveWorldStore(store: WorldStore): WorldStore { const previous = activeStore; activeStore = store; return previous; } /** Run `fn` with `store` active, restoring it after a synchronous result or after its promise settles. * * CONCURRENCY: where the runtime has AsyncLocalStorage the scope is per async context, so * unserialized requests for different worlds interleave without bleeding stores. Only a runtime * without it (a browser bundle) falls back to the module-global swap, which is safe only when * store-scoped work is serialized. */ export function withWorldStore(store: WorldStore, fn: () => Promise): Promise; export function withWorldStore(store: WorldStore, fn: () => T): T; export function withWorldStore(store: WorldStore, fn: () => T | Promise): T | Promise { if (scope) return scope.run(store, fn); const previous = setActiveWorldStore(store); let result: T | Promise; try { result = fn(); } catch (error) { setActiveWorldStore(previous); throw error; } if (result instanceof Promise) { return result.finally(() => setActiveWorldStore(previous)); } setActiveWorldStore(previous); return result; } // ── async persistence boundary ─────────────────────────────────────────────────────── // // The kernel is synchronous; a durable backend (Durable Object storage, KV, redis, S3) // is asynchronous. These two helpers are the ONLY async in the persistence story: a // serverless entry `await hydrate()`s the durable snapshot into a sync store, runs the // sync kernel (which never awaits), then `await flush()`es the mutated snapshot back. // The shapes are proven against `MemoryWorldStore` (see world-store.test.ts); a real DO // adapter implements `HydrationSource`/`HydrationSink` over its own async storage. /** A durable, async source of a world snapshot (path → content). */ export interface HydrationSource { load(): Promise>; } /** A durable, async sink for a world snapshot (path → content). */ export interface HydrationSink { save(snapshot: Record): Promise; } /** Fill `store` from an async durable `source` before the sync kernel runs. */ export async function hydrateInto(store: MemoryWorldStore, source: HydrationSource): Promise { const snapshot = await source.load(); for (const [path, content] of Object.entries(snapshot)) store.write(path, content); } /** Persist `store`'s current contents back to an async durable `sink` after the sync * kernel has run. */ export async function flushFrom(store: MemoryWorldStore, sink: HydrationSink): Promise { await sink.save(store.entries()); }