/** * The file-backed store for one repository's session bus. * * Every session writes its own record, every message is its own file, and every * claim is an exclusive-create file. That keeps concurrent Pi processes off one * shared lock while still making the common operations atomic. The store knows * nothing about models or UI; it only reads and writes validated JSON. */ import { rm, stat } from "node:fs/promises"; import { join } from "node:path"; import { asRecord } from "../../lib/json.ts"; import { busPaths, dropIfEmpty, dropStaleTemps, ensureDir, listJson, publishNew, readJsonAs, storedName, writeJsonAtomic, } from "../../lib/session-files.ts"; import { type BusMessage, holdsMessage } from "../../lib/session-mail.ts"; import type { MailStore } from "./mail-store.ts"; import { claimsConflict, keepsOverlap } from "./overlap.ts"; import type { ClaimConflict, ClaimRecord, DeliveryFailure, DropResult, InboxPeek, InboxRead, SessionRecord, } from "./types.ts"; export interface BusStoreOptions { /** The repository bus directory, e.g. `/session-bus/`. */ dir: string; } export interface BusStore { readonly dir: string; ensure(): Promise; writeSession(record: SessionRecord): Promise; sessions(): Promise; /** One session's record, or undefined when it is not on disk. */ session(id: string): Promise; removeSession(id: string): Promise; writeMessage(target: string, message: BusMessage): Promise; /** * Write one message for each recipient, and answer with those written and those a * file could not be made for. * * A send is one file per inbox, not one transaction over several, so a failure part-way * through leaves the recipients before it holding the message. Nothing here can make the * set atomic — splitting the report from the write is the point — so the caller decides * what a partial delivery means. */ writeTo( recipients: readonly { id: string; label: string }[], fields: Omit, ): Promise<{ delivered: string[]; failed: DeliveryFailure[] }>; /** * Read up to `limit` messages from the front of a recipient's inbox without consuming * them, skipping the ids the caller already has, with every id still unread. */ peek(id: string, options: { limit: number; known?: ReadonlySet }): Promise; /** * Read and consume up to `limit` messages from the front of a recipient's inbox, naming the * files it could not validate instead of deleting them. A limit of zero consumes nothing. */ inbox(id: string, limit: number): Promise; /** * Remove the mail `ids` name, from the inboxes no record holds, and name what stayed. * * Only the unaccounted copies go: an id a live record's inbox also carries stays there, which * is why one id can be both dropped and kept. */ dropUnaccounted(ids: readonly string[]): Promise; claims(): Promise; claimAll( paths: readonly string[], owner: string, ownerLabel: string, at: number, /** Test seams: see {@link ClaimSeams}. */ seams?: ClaimSeams, ): Promise<{ claimed: string[]; conflicts: ClaimConflict[] }>; release(path: string, owner: string): Promise; releaseAll(owner: string): Promise; /** * Remove the records the TTL has taken, the claims they held, and the temp files a crashed * writer left. * * Cheap by design — two small directory reads — because an action runs it: a peer that died * must lose the paths it held at the moment a claimant looks, without the claimant paying for * every message on the bus first. */ pruneRecords(input: { now: number; /** The sessions known to be live when the caller read the bus. */ activeIds: ReadonlySet; /** * Records whose process the caller found gone: they keep their row until their * heartbeat ages out, and own no path and no mail in the meantime. */ goneIds?: ReadonlySet; sessionTtlMs: number; }): Promise; /** * Remove the mail past its TTL from this session's inbox and from every inbox no record holds. * * The one call here whose cost grows with the mail on the bus, so it belongs to the clock the * TTL is measured in — the heartbeat — rather than to every action. A day-old message reading * as one for another ten seconds costs nobody. An inbox a live record holds is that session's * to sweep, which is the rule the ledger states as "unheld": one session no longer walks a * peer's mail on every tick, and mail nobody owns still finds its way out. */ pruneMail(input: { now: number; messageTtlMs: number; own: string }): Promise; } /** The seam a claim batch offers a suite: where inside the batch a rival can be put. */ export interface ClaimSeams { /** * Runs once, after the pre-check and before the first publish. * * The one place a suite can put a rival batch in the window the pre-check cannot see: a batch * that is still checking has published nothing, so nothing tells the rival to stop. */ afterCheck?: () => Promise; /** Runs after each path is published: the window between two creates of one batch. */ afterClaim?: (path: string) => Promise; } /** * Release each path through `release`, reporting the ones this session did not * hold. One copy, because `release` and `leave` answer the same two lists, and a * second loop is how the two answers drift. */ export async function releaseClaimed( paths: readonly string[], release: (path: string) => Promise, ): Promise<{ released: string[]; notOwned: string[] }> { const released: string[] = []; const notOwned: string[] = []; for (const path of paths) { if (await release(path)) released.push(path); else notOwned.push(path); } return { released, notOwned }; } /** * Whether a file is one record the bus wrote. * * Every field the bus writes is checked, and a field of the wrong shape skips the whole * record — the convention `startToken` and `watching` already follow. That is stricter than * the roster's read (`lib/session-roster.ts`), which drops only the bad field and keeps the * row: a page must still draw a teammate. Here the record is the bus's own, and one it cannot * read is one it cannot trust to say who owns a path or a name. */ function isSession(value: unknown): value is SessionRecord { const entry = asRecord(value); if (!entry) return false; return ( typeof entry.id === "string" && typeof entry.label === "string" && typeof entry.pid === "number" && typeof entry.cwd === "string" && typeof entry.repoRoot === "string" && typeof entry.mode === "string" && typeof entry.startedAt === "number" && typeof entry.lastSeen === "number" && (entry.startToken === undefined || typeof entry.startToken === "string") && (entry.turnAt === undefined || typeof entry.turnAt === "number") && (entry.watching === undefined || isWatchingState(entry.watching)) && (entry.seenMail === undefined || (Array.isArray(entry.seenMail) && entry.seenMail.every((id) => typeof id === "string"))) ); } /** The optional listening field, validated only when it is there. */ function isWatchingState(value: unknown): boolean { const entry = asRecord(value); return entry !== undefined && typeof entry.armed === "boolean" && typeof entry.wake === "boolean"; } function isClaim(value: unknown): value is ClaimRecord { const entry = asRecord(value); if (!entry) return false; return ( typeof entry.path === "string" && typeof entry.owner === "string" && typeof entry.ownerLabel === "string" && typeof entry.at === "number" ); } /** Create a store rooted at one repository's bus directory. */ export function createBusStore(options: BusStoreOptions): BusStore { const paths = busPaths(options.dir); const { dir, sessions: sessionsDir, claims: claimsDir } = paths; const sessionFile = paths.recordOf; const inboxDir = paths.inboxOf; const claimFile = paths.claimOf; /** * One message file in one inbox, created where it is not there yet. * * A local function rather than a method, because two entries of the store write through * it and `this` is not a receiver a destructured caller keeps. */ async function writeMessageFile(target: string, message: BusMessage): Promise { const directory = inboxDir(target); if (await holdsMessage(directory, message)) return; await writeJsonAtomic(paths.messageOf(target, message.id), message); } async function readSessions(): Promise { const sessions: SessionRecord[] = []; for (const name of await listJson(sessionsDir)) { const record = readJsonAs(join(sessionsDir, name), isSession); if (record) sessions.push(record); } return sessions.sort((a, b) => b.lastSeen - a.lastSeen || a.label.localeCompare(b.label)); } async function readClaims(): Promise { const claims: ClaimRecord[] = []; for (const name of await listJson(claimsDir)) { const claim = readJsonAs(join(claimsDir, name), isClaim); if (claim) claims.push(claim); } return claims.sort((a, b) => a.path.localeCompare(b.path)); } async function claimOne( path: string, owner: string, ownerLabel: string, at: number, ): Promise<{ claim: ClaimRecord; conflict?: ClaimRecord; created: boolean }> { const file = claimFile(path); const claim: ClaimRecord = { path, owner, ownerLabel, at }; // Publish a written temp file instead of creating the claim and filling it // afterwards: the link is the exclusive create, so no reader can observe a // half-written claim and mistake it for a crashed owner's. if (await publishNew(file, claim)) return { claim, created: true }; const existing = readJsonAs(file, isClaim); if (existing) { return existing.owner === owner ? { claim: existing, created: false } : { claim, conflict: existing, created: false }; } // A claim file that does not parse is debris from before claims were // published whole; clear it once so a stale path cannot block forever. await rm(file, { force: true }); if (!(await publishNew(file, claim))) throw new Error(`the claim on ${path} kept being recreated under it`); return { claim, created: true }; } async function releaseOne(path: string, owner: string): Promise { const file = claimFile(path); const existing = readJsonAs(file, isClaim); if (!existing || existing.owner !== owner) return false; await rm(file, { force: true }); return true; } /** * Claim every path for one owner, or nothing. * * The pre-check reads the claims once and reports every blocking owner, so a * batch that cannot be satisfied touches no file. One owner blocking one * requested path is one conflict, however many of its claims reach under that * path: the sentence a caller prints is per conflict, and a second copy of it is * not a second blocker. A create that loses a race after the pre-check gives back * only what this batch published: a path the owner already held carries an older * `at` and is not this batch's to release. * * The pre-check cannot decide *overlapping* paths, because those are two different files: `link` * is exclusive per name, so `src` and `src/api` claimed at once both publish. So a batch that * published re-reads once and settles — {@link keepsOverlap} picks the keeper, and the loser gives * back the whole batch and names the winner. A pair may hold overlapping paths for the length of * one batch, and never keep them. */ async function claimAll( paths: readonly string[], owner: string, ownerLabel: string, at: number, seams: ClaimSeams = {}, ): Promise<{ claimed: string[]; conflicts: ClaimConflict[] }> { const existing = await readClaims(); const conflicts: ClaimConflict[] = []; for (const path of paths) { for (const held of existing) { if (held.owner === owner || !claimsConflict(path, held.path)) continue; if (conflicts.some((entry) => entry.path === path && entry.owner === held.owner)) continue; conflicts.push({ path, owner: held.owner, ownerLabel: held.ownerLabel }); } } if (conflicts.length > 0) return { claimed: [], conflicts }; if (seams.afterCheck) await seams.afterCheck(); const claimed: string[] = []; const created: string[] = []; for (const path of paths) { const result = await claimOne(path, owner, ownerLabel, at); if (result.conflict) { for (const done of created) await releaseOne(done, owner); const { owner: holder, ownerLabel: holderLabel } = result.conflict; return { claimed: [], conflicts: [{ path, owner: holder, ownerLabel: holderLabel }] }; } claimed.push(path); if (result.created) created.push(path); if (seams.afterClaim) await seams.afterClaim(path); } const settled = await settleOverlaps(claimed, owner); if (settled.length > 0) { // What this batch published goes, and nothing else: a claim is all-or-nothing, so half a // batch would report a success nobody asked for — but a path the owner already held is not // this batch's to give back, which is the rule the create-race rollback above keeps too. for (const path of created) await releaseOne(path, owner); return { claimed: [], conflicts: settled }; } return { claimed, conflicts: [] }; } /** * Which of this batch's paths a rival published first — the settle step's one question. * * Bounded by the batch: it stats the claim this batch published and each rival claim that * overlaps one of its paths, and nothing else. `readClaims` parses every claim file and is what * the board performs once a second, so it must not start paying a stat per claim in the * repository; that is also why `ClaimRecord` does not grow an mtime field. */ async function settleOverlaps(mine: readonly string[], owner: string): Promise { const conflicts: ClaimConflict[] = []; if (mine.length === 0) return conflicts; const held = await readClaims(); const times = new Map(); const timeOf = async (path: string): Promise => { if (times.has(path)) return times.get(path); const info = await stat(claimFile(path)).catch(() => undefined); times.set(path, info?.mtimeMs); return info?.mtimeMs; }; for (const path of mine) { const mineAt = await timeOf(path); // The file is gone: something else settled this batch already, and there is nothing to defend. if (mineAt === undefined) continue; for (const rival of held) { if (rival.owner === owner || !claimsConflict(path, rival.path)) continue; const rivalAt = await timeOf(rival.path); // A rival that is gone released it — it lost to somebody in the same window. if (rivalAt === undefined) continue; if (keepsOverlap({ mtimeMs: mineAt, owner }, { mtimeMs: rivalAt, owner: rival.owner })) continue; if (conflicts.some((entry) => entry.path === path && entry.owner === rival.owner)) continue; conflicts.push({ path, owner: rival.owner, ownerLabel: rival.ownerLabel }); } } return conflicts; } /** * The mail family, loaded on the first call that needs it. * * A deferral rather than a preference: this entry's static module budget is exactly full, and one * more evaluation fails `test/dependencies.test.ts`. `lib/lazy.ts` is the house idiom and is itself * a module this entry does not otherwise load, so the one-load promise is kept here instead. */ let mail: Promise | undefined; const mailStore = (): Promise => (mail ??= import("./mail-store.ts").then((module) => module.createMailStore(dir))); return { dir, async ensure() { await Promise.all([ensureDir(sessionsDir), ensureDir(paths.inbox), ensureDir(claimsDir)]); }, async writeSession(record) { await writeJsonAtomic(sessionFile(record.id), record); }, sessions: readSessions, session: async (id) => readJsonAs(sessionFile(id), isSession), async removeSession(id) { await rm(sessionFile(id), { force: true }); await dropIfEmpty(inboxDir(id)); }, writeMessage: writeMessageFile, async writeTo(recipients, fields) { const delivered: string[] = []; const failed: DeliveryFailure[] = []; for (const recipient of recipients) { try { await writeMessageFile(recipient.id, { ...fields, to: recipient.id, toLabel: recipient.label }); delivered.push(recipient.label); } catch (error) { // The recipient's own sentence, kept as it came: it is the reason a caller // prints, and a filesystem refusal means nothing once it is rewritten. failed.push({ label: recipient.label, error: error instanceof Error ? error.message : String(error) }); } } return { delivered, failed }; }, /** * Remove the mail `ids` name from the inboxes no record holds. * * Attribution is the store's own: an inbox is filed under the hash of its recipient's id, so * hashing each record's id is the whole answer to "does anybody hold this mail" — no field a * message could lie about, and no need to invert the hash of a directory's name. It is read * here, at the delete, rather than by the caller that listed the mail: a session that resumed * in between holds its mail again, and a listing is stale the moment its person reads it. * * An id a held inbox carries is refused and handed back with its holder's label. That is the * one guard this door needs, because a mistaken id would otherwise destroy mail its session * can still read; nothing else on the bus is this destructive and irreversible. */ // The mail family, delegated to the module that owns it: loaded on the first call that needs // it, because this entry's static module budget is exactly full (see `mailStore`). async peek(id, options) { return (await mailStore()).peek(id, options); }, async inbox(id, limit) { return (await mailStore()).inbox(id, limit); }, async dropUnaccounted(ids) { // The holder of every inbox, read now rather than when the caller listed the mail: a // session that resumed in between holds its inbox again. const held = new Map((await readSessions()).map((session) => [session.id, session.label])); return (await mailStore()).dropUnaccounted(ids, held); }, claims: readClaims, claimAll, release: releaseOne, async releaseAll(owner) { const released: string[] = []; for (const claim of await readClaims()) { if (claim.owner !== owner) continue; await rm(claimFile(claim.path), { force: true }); released.push(claim.path); } return released; }, async pruneRecords({ now: at, activeIds, goneIds, sessionTtlMs }) { // The caller's set can lag: a peer that joined and claimed since that read is // live on disk, and its claims are its own. This read is the answer, so the // set only adds to what it found rather than deciding who survives — except for // a record whose process is gone, which the caller knows and the file cannot. // That record keeps its row until the heartbeat ages out and owns nothing. const live = new Set(activeIds); const gone = new Set(goneIds ?? []); for (const session of await readSessions()) { if (at - session.lastSeen > sessionTtlMs) { await rm(sessionFile(session.id), { force: true }); await dropIfEmpty(inboxDir(session.id)); live.delete(session.id); } else if (gone.has(session.id)) { live.delete(session.id); } else { live.add(session.id); } } for (const claim of await readClaims()) { if (!live.has(claim.owner)) await rm(claimFile(claim.path), { force: true }); } await dropStaleTemps(sessionsDir); await dropStaleTemps(claimsDir); }, async pruneMail(input) { // Who holds an inbox, read at the sweep: a record pruned a moment ago leaves its mail // unheld, and an inbox nobody holds is what this session may end. const held = new Set((await readSessions()).map((session) => storedName(session.id))); return (await mailStore()).pruneMail(input, held); }, }; }