// A run's completeness (ADR 0036 §9; architecture, "Scenarios"): the World is the application's internet closure, so // what reached past it is what the World is missing. The relay notes every destination no twin serves (refused, or // passed to the real internet where the World lets it), a twin every request to an operation it does not implement // (answered as the vendor answers an unknown request), and a run every machine fault no fault door carried. All are // appended, at World time, to the active World's completeness file (worldStateFile('completeness')), and a run counts them. import { getActiveWorldStore, worldStateFile } from './world-store.ts'; import clockForm from '../world-clock.cjs'; import { isWorldInternalHost } from '../network-policy.cjs'; /** What a relay or a twin notes; the World's time is added as it is appended. */ type CompletenessNote = | { kind: 'egress'; destination: string; outcome: 'refused' | 'passed'; rule?: string } | { kind: 'unimplemented'; vendor: string; method: string; path: string; operation?: string } | { kind: 'machine'; layer: string; fault: string; target?: string } /** a connection to the World's own proxy address that the address refused (the injector's): what was sent there never * reached the World (a proxy stopped under a running app) */ | { kind: 'proxy'; address: string; outcome: 'refused'; rule: string }; /** One record a run's completeness counted, at its World instant: the grounds of a count, carried beside it. */ export type CompletenessRecord = CompletenessNote & { at: string }; /** The World's time in `env` (its clock file's, else the machine's): world-clock.cjs's one stamp, which the injector's * records carry too. */ function worldTimeIn(env: Record): string { return clockForm.worldInstant({ ...env, TWIN_WORLD_CLOCK_FILE: worldStateFile('clock', env) }, (file) => getActiveWorldStore().read(file)); } /** A destination (`host:port`) on a loopback or private address, as the World's network policy reads one. */ function internalDestination(destination: string): boolean { let host: string; try { host = new URL(`http://${destination}`).hostname; } catch { return false; } return host !== '' && isWorldInternalHost(host); } /** Append one record to the active World's completeness file (the store's own World's, else the one `env` names); a * process outside a World notes nothing. */ export function noteCompleteness(env: Record, record: CompletenessNote): void { const file = worldStateFile('completeness', env); if (!file) return; // what passed to a loopback or private address stayed on the World's own machine and network (the app at its listening // address, a sidecar): it never reached the internet, so it is nothing the World is missing if (record.kind === 'egress' && record.outcome === 'passed' && internalDestination(record.destination)) return; // a record that cannot be appended is not a World that was complete: it fails the request that would have noted it, // where a run counts it, rather than leaving the run's completeness short getActiveWorldStore().append(file, `${JSON.stringify({ at: worldTimeIn(env), ...record })}\n`); } export type Completeness = { /** zero when the World was the whole internet the run reached */ total: number; egress: Record; unimplemented: Record>; /** machine faults the run placed and no fault door carried, by layer and kind (ADR 0036 §4) */ machine: Record; /** connections the World's own proxy address refused, by address */ proxy: Record; /** each record counted, in the order it was appended: what the totals above are read from (ADR 0036 §8) */ records: CompletenessRecord[]; }; /** How many records the completeness file holds: where a run starts counting. */ export function completenessMark(file: string): number { return (getActiveWorldStore().read(file) ?? '').split('\n').filter((line) => line.trim()).length; } /** What reached past the World after the file's `mark`, counted per destination and per vendor's operation. */ export function completenessSince(file: string, mark: number): Completeness { const out: Completeness = { total: 0, egress: {}, unimplemented: {}, machine: {}, proxy: {}, records: [] }; for (const line of (getActiveWorldStore().read(file) ?? '').split('\n').filter((l) => l.trim()).slice(mark)) { const r = JSON.parse(line) as CompletenessRecord; out.total += 1; out.records.push(r); if (r.kind === 'egress') (out.egress[r.destination] ??= { refused: 0, passed: 0 })[r.outcome] += 1; else if (r.kind === 'machine') out.machine[`${r.layer} ${r.fault}`] = (out.machine[`${r.layer} ${r.fault}`] ?? 0) + 1; else if (r.kind === 'proxy') out.proxy[r.address] = (out.proxy[r.address] ?? 0) + 1; else { const ops = out.unimplemented[r.vendor] ??= {}; const key = r.operation ?? `${r.method} ${r.path}`; ops[key] = (ops[key] ?? 0) + 1; } } return out; }