/** Inert construction of the actual observation -> registry path on one held * configured node lease. Nothing here installs a production external enforcer, * admits scheduler/listener work or certifies a deployment inventory. */ import { isDeepStrictEqual } from "node:util" import type { BandwidthOperatorEnrollmentConfig } from "./bandwidth-operator-enrollment.js" import { createBandwidthOperatorEnrollmentPrerequisite } from "./bandwidth-operator-enrollment.js" import { createBandwidthOperatorRegistry } from "./bandwidth-operator-registry.js" import type { createConfiguredBandwidthNodeSource } from "./bandwidth-node-source.js" import type { BandwidthPeerTrustedSource } from "./bandwidth-peer-observation.js" import { createBandwidthOperatorOriginalAuthorityReader, createBandwidthOperatorExternalFencer, createBandwidthOperatorWriterWindows, BandwidthOperatorWriterFenceError, type BandwidthOperatorExternalStopAdapter, type BandwidthOperatorScope, type BandwidthOperatorWriterWindow, type BandwidthOperatorPeerSourceGuard, } from "./bandwidth-operator-writer-window.js" export interface BandwidthOperatorWriterCompositionDependencies { config: BandwidthOperatorEnrollmentConfig nodeSource: ReturnType observationSource?: BandwidthPeerTrustedSource /** Optional fixed server-constructor assembly, invoked ONLY after the genuine * window is held. Mutually exclusive with legacy observationSource. */ createObservationSource?(guard: BandwidthOperatorPeerSourceGuard): BandwidthPeerTrustedSource & { close?(): void } /** Required trusted service-manager/credential enforcer. There is deliberately * no default, JSON attestor, pg_locks fallback or empty-database shortcut. */ externalStop: BandwidthOperatorExternalStopAdapter } export function createBandwidthOperatorWriterComposition(dependencies: BandwidthOperatorWriterCompositionDependencies) { const config = Object.freeze({ ...dependencies.config }) const originals = createBandwidthOperatorOriginalAuthorityReader(config) const external = createBandwidthOperatorExternalFencer(dependencies.externalStop) const open = dependencies.nodeSource.open.bind(dependencies.nodeSource) const openWithOwnershipFence = dependencies.nodeSource.openWithOwnershipFence.bind(dependencies.nodeSource) const createObservationSource = dependencies.createObservationSource?.bind(dependencies) const read = dependencies.observationSource?.read.bind(dependencies.observationSource) if (!!createObservationSource === !!read) throw new BandwidthOperatorWriterFenceError() const active = new Set<() => void>() let closed = false return Object.freeze({ async register(scope: BandwidthOperatorScope) { if (closed) throw new BandwidthOperatorWriterFenceError() // Original authority exists BEFORE external Q and BEFORE secrets/native // observation. The signed peer is fixed; no caller supplies a new peer. const originalCap = await originals.acquire(scope) if (closed) throw new BandwidthOperatorWriterFenceError() let window: BandwidthOperatorWriterWindow | undefined const windows = createBandwidthOperatorWriterWindows({ originals, external, source: { open, openWithOwnershipFence }, registry: { async register(original, borrowed, heldScope) { if (!window) throw new BandwidthOperatorWriterFenceError() const observationSource = createObservationSource?.(windows.createPeerSourceGuard(window, originalCap)) const readObservation = observationSource ? observationSource.read.bind(observationSource) : read! const checkMetadata = observationSource?.assertMetadataCurrent?.bind(observationSource) const closeObservation = observationSource?.close?.bind(observationSource) const verify = async (grant: unknown, requested: BandwidthOperatorScope) => { if (!window || !isDeepStrictEqual(grant, original.grant)) throw new BandwidthOperatorWriterFenceError() await windows.verify(window, originalCap, requested) } const coordinator = createBandwidthOperatorEnrollmentPrerequisite(config, { async read(peerRef, requested) { // The coordinator invokes Q before reaching this credential source; // keep an additional original/held-Q check at that exact boundary. await verify(original.grant, requested) return readObservation(peerRef, requested) }, ...(checkMetadata ? { assertMetadataCurrent: checkMetadata } : {}), }, { verify }) try { const prerequisite = await coordinator.acquire(original.grant.peerRef, heldScope) const registry = createBandwidthOperatorRegistry({ coordinator, source: { async open(requested, guard) { await verify(original.grant, requested); await guard() // Registry owns BEGIN/COMMIT but borrows the owner's SAME lease. // release(true) is logical; close is completed by the window // after its final original-authority/Q check, even on failure. return Object.freeze({ ...borrowed, close() {} }) }, } }) return await registry.register(prerequisite, heldScope) } finally { coordinator.close(); closeObservation?.() } }, } }) const close = () => windows.close() active.add(close) try { window = await windows.open(originalCap, scope) return await windows.register(window) } finally { close(); active.delete(close) } }, close() { closed = true; for (const stop of active) stop(); originals.close() }, }) }