// **The first registrations switch the merge on** (novox/hq design 47 §95, §353): until the catalog half has said its first // `registration-changed`, the seat git's `merge-pull` refuses every merge naming the missing registrations, because a merge // made with no Approve could reach the old assigner, which writes wants from the trunk with no pipeline. The module learns it // two ways, both through the seat `mesh-catalog`: its verb `registrations` asked at the start (a restart after the first // registrations were written), and its event `registration-changed` (the first being written, or heard again from the // seat's kept stream). Neither is the holder's state or a module named. // // **A plain merge needs a view read since the last change heard** (design 47 H2 H-A-471, second addendum): the holder keeps // the time of its last read of the watched branches and of the last registration-changed it heard; the view is current only // when a read asked after that change has answered. A read that fails is said and asked again with a growing wait until it // answers, so an unread view is never taken as "no watched branch". import { seat as CATALOG, verbs as CATALOG_VERBS, events as CATALOG_EVENTS, type RegistrationChangedEvent, type RegistrationsAnswer, type RegistrationsInput, } from "@novox/mesh-delivery-contracts-mesh-catalog"; import { bindWait } from "./bind.js"; /** The seat verb asked, as the manifest's `invokes` names it. */ export const REGISTRATIONS_VERB = `seat:${CATALOG}.${CATALOG_VERBS.registrations}`; /** The seat event heard, as the manifest's `consumes` names it. */ export const REGISTRATION_CHANGED_EVENT = `${CATALOG}.${CATALOG_EVENTS.registrationChanged}`; export class Registrations { private first = false; private byRepository = new Map>(); /** The registration-changed events heard; the count a read was asked at, once it answered (-1: never read). */ private changes = 0; private readAtChange = -1; /** The reads asked, and the latest whose answer is the view (an older answer landing later is not taken). */ private asked = 0; private applied = 0; private lastRead?: Date; private lastChange?: Date; constructor(private readonly ask: (verb: string, input: RegistrationsInput) => Promise, private readonly log: (line: string) => void = () => {}, private readonly sleep: (ms: number) => Promise = (ms) => new Promise((r) => setTimeout(r, ms)), private readonly now: () => Date = () => new Date()) {} /** Whether the first registrations are written: the one fact the merge switches on. */ get written(): boolean { return this.first; } /** The watched branches the registrations name, by their origin repository (`/`) as of the last read. */ get watched(): ReadonlyMap> { return this.byRepository; } /** Whether the watched branches were read since the last registration-changed heard (H-A-471, second addendum). */ get current(): boolean { return this.readAtChange === this.changes; } /** The time the last read that answered was asked, and of the last registration-changed heard. */ get readAt(): Date | undefined { return this.lastRead; } get changedAt(): Date | undefined { return this.lastChange; } /** One `registration-changed`: the first registration written, however many were before this module started. */ hear(ev: RegistrationChangedEvent | undefined): void { if (!ev || typeof ev.module !== "string" || ev.module === "") { throw new Error(`not a ${REGISTRATION_CHANGED_EVENT} event: ${JSON.stringify(ev ?? null).slice(0, 200)}`); } if (!this.first) this.log(`the first registration is written (${ev.module}); merge-pull merges from here`); this.first = true; this.changes++; this.lastChange = this.now(); } /** The start's first view, asked again after each `registration-changed`: any registration held is the first written, and its * watched branches are read. A failed ask is said and asked again with a growing wait until it answers (H-A-471, second * addendum); until then no member is merged plainly. */ async start(): Promise { for (let attempt = 1; ; attempt++) { const at = this.changes, seq = ++this.asked, askedAt = this.now(); try { let a: unknown = await this.ask(REGISTRATIONS_VERB, {}); if (typeof a === "string") a = JSON.parse(a); const r = (a as RegistrationsAnswer | undefined)?.registrations; if (!Array.isArray(r)) throw new Error(`${REGISTRATIONS_VERB} answered no registrations: ${JSON.stringify(a ?? null).slice(0, 200)}`); if (seq < this.applied) return; const by = new Map>(); for (const x of r) for (const b of x.branches ?? []) (by.get(x.origin) ?? by.set(x.origin, new Set()).get(x.origin)!).add(b); this.byRepository = by; this.applied = seq; this.readAtChange = at; this.lastRead = askedAt; if (attempt > 1) this.log(`the registrations are read after ${attempt} attempts`); if (r.length > 0) { this.first = true; this.log(`${r.length} registrations are held; merge-pull merges`); } return; } catch (err) { const wait = bindWait(attempt); this.log(`the registrations could not be read: ${err instanceof Error ? err.message : String(err)}; ` + `no member is merged plainly until they are, asked again in ${wait / 1000}s`); await this.sleep(wait); } } } }