import { writeFile } from "node:fs/promises"; import { resolve } from "node:path"; import { root, run, requireThat } from "../scripts/runtime"; import { containerID } from "../scripts/runtime-inspect"; import { waitReady } from "./wait"; // Caller stops its explicit preflight heartbeat before this function. No live provider is stopped here. export async function freshnessExpiry() { await waitReady(); const started = Date.now(), since = new Date(started).toISOString(), baseline: any = {}, observed: any = {}; for (const service of ["api1", "api2", "worker"]) { const id = await containerID(service), state = JSON.parse((await run(["docker", "inspect", id])).out)[0]; requireThat(state.State.Running, "FRESHNESS_BASELINE_NOT_RUNNING"); baseline[service] = { id, restarts: state.RestartCount }; } while (Date.now() - started < 145_000) { for (const service of ["api1", "api2", "worker"]) { if (observed[service]) continue; const base = baseline[service], state = JSON.parse((await run(["docker", "inspect", base.id])).out)[0], logs = await run(["docker", "logs", "--since", since, base.id]); const cause = (logs.out + "\n" + logs.err).split("\n").some((line) => { try { return JSON.parse(line).event === "secret_freshness_failed"; } catch { return false; } }); if (cause && (!state.State.Running || state.RestartCount > base.restarts)) observed[service] = { event: "secret_freshness_failed", seconds: (Date.now() - started) / 1000, }; } if (Object.keys(observed).length === 3) break; await Bun.sleep(1000); } requireThat( Object.keys(observed).length === 3, "FRESHNESS_EXPIRY_DID_NOT_STOP_ALL_PROCESSES", ); await writeFile( resolve(root, "evidence/preflight/freshness-expiry.json"), JSON.stringify( { provider: "explicit operator heartbeat stopped; not OpenBao seal", max_age_seconds: 120, poll_seconds: 5, observed, limit: "measured local filesystem and scheduling only; no hung-filesystem wall-clock guarantee", }, null, 2, ) + "\n", ); console.log("PASS_ACTUAL_FRESHNESS_EXPIRY_WITH_FIXED_CAUSE_EVENT"); }