// THE PLACEHOLDER REMOTE (docs/concepts/the-model.md#a-branch-is-a-position-not-a-copy, "default data"): a fresh twin is a clone of the // coherent default data its pack ships, and that seed is a PULL, never a pending write. While a // service's placeholder-pull window is open — a marker file under its state dir, read through the // active world store like the world clock — every local `set` write the twin accepts lands as an // entry of the placeholder's on the parent log (provenance `placeholder`) and no action row // is appended: `main` equals `origin/main` when the seed ends, `diff` is empty, nothing is pending, // and no real remote linked later is ever offered the seed. The placeholder has no origin and no // credential, so it can never be a push target; a pull from it again is "back to the defaults". import { appendParentEntry, type Entry } from './log.ts'; import { join } from 'node:path'; import { worldStateRoot } from './storage.ts'; import { hashFieldValue } from './hash.ts'; import { getActiveWorldStore } from './world-store.ts'; import type { TwinAction } from './actions.ts'; export const PLACEHOLDER_REMOTE = 'placeholder'; /** The window is per WORLD SERVICE ROOT, not per state service: a twin may record under a state * service that is not its world service id (slack records under `chat`), and every state service * of one twin lives under the same root — so the marker sits at the root's state dir. */ export function placeholderPullMarkerPath(_service: string, root?: string): string { return join(worldStateRoot(root), 'placeholder-pull'); } /** Is the service's placeholder-pull window open? */ export function placeholderPullActive(service: string, root?: string): boolean { return getActiveWorldStore().exists(placeholderPullMarkerPath(service, root)); } export function beginPlaceholderPull(service: string, root?: string, at: string = new Date().toISOString()): void { const path = placeholderPullMarkerPath(service, root); const store = getActiveWorldStore(); store.mkdir(join(path, '..')); store.write(path, `${JSON.stringify({ remote: PLACEHOLDER_REMOTE, openedAt: at })}\n`); } export function endPlaceholderPull(service: string, root?: string): void { const path = placeholderPullMarkerPath(service, root); if (getActiveWorldStore().exists(path)) getActiveWorldStore().remove(path); } /** Open the window around `fn` (in-process seeding). */ export async function withPlaceholderPull(service: string, root: string | undefined, fn: () => T | Promise): Promise { beginPlaceholderPull(service, root); try { return await fn(); } finally { endPlaceholderPull(service, root); } } /** The entries a local write becomes when it is a pull from the placeholder: the write's own * subject and fields, plus every resource its projection creates or updates. A projection * DELETE has no placeholder form (the placeholder holds what exists, not what was removed) and is * refused loudly — a seed creates the world, it does not un-create it. */ export function placeholderEntriesFor(action: TwinAction): Entry[] { const observations: Array<{ subject: { type: string; id: string }; fields: Record }> = []; if (action.fields) observations.push({ subject: action.subject, fields: action.fields }); for (const c of action.projection?.creates ?? []) observations.push({ subject: { type: c.type, id: c.id }, fields: c.fields }); for (const u of action.projection?.updates ?? []) observations.push({ subject: { type: u.type, id: u.id }, fields: u.fields }); if (action.projection?.deletes?.length) throw new Error(`placeholder pull: ${action.service} ${action.operation ?? action.op} deletes ${action.projection.deletes.map((d) => `${d.type}:${d.id}`).join(', ')} — a seed creates the placeholder world, it cannot remove from it`); return observations.map((o): Entry => { const contentHash = hashFieldValue({ subject: o.subject, fields: o.fields, operation: action.operation ?? action.op }); return { id: `placeholder:${action.service}:${o.subject.type}:${o.subject.id}:${contentHash}`, service: action.service, op: 'set', subject: o.subject, occurredAt: action.occurredAt, fields: o.fields, provenance: 'placeholder' }; }); } /** Land `action` as the placeholder's entries on the parent log. Caller holds the service locks. */ export function landAsPlaceholder(action: TwinAction, root?: string): { entries: Entry[] } { const entries = placeholderEntriesFor(action); for (const entry of entries) appendParentEntry(entry, root); return { entries }; }