Heimdall/src/verse-state.ts
Meta Vens 726dd56e7d Honour the Idunn process write lease, and stop claiming active while warming
During a deployment two Heimdall processes exist at once under distinct
UIDs and namespaces, and both are configured with the same CultCache store
path. Exactly one may write it. Idunn decides which by publishing a lease
record naming an incarnation; nothing in Heimdall read it, so both
generations would have written heimdall.service.cc.

src/process-write-lease.ts answers one question -- may this process write
state right now -- by reading the lease and the runtime bundle's
expected.cc, and comparing incarnation identity. It grants nothing and
throws nothing outward: every failure is a decision of mayWrite:false with
a reason, because for a warming candidate "not yet my turn" is the
expected condition and crashing would turn it into a failed deployment.
Records are decoded as the canonical positional MessagePack Idunn writes
(14 fields for the lease, schema-checked), and the tests build the same
envelope bytes rather than convenient objects.

publishHeimdallVerseState is the single writer, so that is the only place
gated. Outside Idunn no lease path is configured and writes proceed
unconditionally, which keeps developer runs and the current plain unit
working.

Second defect found while wiring it: health publication hardcoded
state "active". A candidate would have claimed active from its first
pulse, which is both untrue and structurally broken -- Idunn must observe
`warming` presence before it fences the incumbent, so a candidate that
never reports warming cannot be promoted. State is now derived from the
lease: warming until held, active after, with the reason carried in the
detail string.

49 tests across 11 files; tsc clean under exactOptionalPropertyTypes.

The recipe's status banner now records the contract as implemented, with
the honest caveat that none of it has been through a live `idunn up` --
the daemon on yggdrasil is still the previous generation, so the first
deployment remains the real verification.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-06 02:25:38 +02:00

260 lines
10 KiB
TypeScript

import { resolveWriteLease, WriteLeaseNotHeldError } from "./process-write-lease.js";
import { mkdir, rename, rm, writeFile } from "node:fs/promises";
import path from "node:path";
import { encode } from "@msgpack/msgpack";
import { appSlugs, providers } from "./contracts.js";
import { buildHeimdallAccessPluginAdvertisement } from "./access-plugin.js";
import type { HeimdallConfig } from "./config.js";
import { buildHeimdallProviderAdvertisement } from "./verse-witness.js";
const STORE_FORMAT_VERSION = "cultcache.store.v1";
export type CultCacheRecord = {
key: string;
schemaId: string;
schemaName: string;
schemaVersion: string;
payload: unknown;
storedAt: string;
};
export type HeimdallRuntimePulse = {
updatedAt: string;
configuredProviders: string[];
storageBackend: string;
appProfileCount: number;
cultCachePath: string;
};
export function createHeimdallRuntimePulse(config: HeimdallConfig, updatedAt = new Date().toISOString()): HeimdallRuntimePulse {
return {
updatedAt,
configuredProviders: providers.filter((provider) => {
const entry = config.providers[provider];
return Boolean(entry?.clientId && entry.clientSecret);
}),
storageBackend: config.storage.backend,
appProfileCount: appSlugs.length,
cultCachePath: config.cultCachePath,
};
}
export async function publishHeimdallVerseState(config: HeimdallConfig, pulse: HeimdallRuntimePulse): Promise<void> {
// Under Idunn a warming candidate shares this store's path with the running
// incumbent. Writing without the lease would let two generations overwrite
// one another's snapshot, so a candidate stays quiet until it is granted the
// lease — which is also the state it is in for most of its warming window.
const lease = await resolveWriteLease({
leasePath: config.idunnWriteLeasePath,
runtimeBundlePath: config.idunnRuntimeBundlePath,
});
if (!lease.mayWrite) {
throw new WriteLeaseNotHeldError(lease.reason);
}
const records: CultCacheRecord[] = [
...buildHeimdallVerseRecords(config, pulse),
{
key: config.daemonId,
schemaId: "idunn.daemon_health",
schemaName: "idunn.daemon_health",
schemaVersion: "idunn.daemon_health.v1",
payload: buildDaemonHealthRecord(config, pulse),
storedAt: pulse.updatedAt,
},
];
await writeCultCacheSnapshot(config.cultCachePath, records);
}
export function buildHeimdallVerseRecords(config: HeimdallConfig, pulse: HeimdallRuntimePulse): CultCacheRecord[] {
return [
{
key: "heimdall",
schemaId: "gamecult.eve.provider_advertisement.v1",
schemaName: "gamecult.eve.provider_advertisement",
schemaVersion: "gamecult.eve.provider_advertisement.v1",
payload: buildRuntimeProviderAdvertisement(config, pulse),
storedAt: pulse.updatedAt,
},
{
key: "heimdall",
schemaId: "heimdall.command_boundary.v1",
schemaName: "heimdall.command_boundary",
schemaVersion: "heimdall.command_boundary.v1",
payload: buildCommandBoundary(config, pulse),
storedAt: pulse.updatedAt,
},
{
key: "gamecult.heimdall.access",
schemaId: "gamecult.eve.plugin_advertisement.v1",
schemaName: "gamecult.eve.plugin_advertisement",
schemaVersion: "gamecult.eve.plugin_advertisement.v1",
payload: buildHeimdallAccessPluginAdvertisement(),
storedAt: pulse.updatedAt,
},
{
key: "heimdall",
schemaId: "heimdall.transport_profile.v1",
schemaName: "heimdall.transport_profile",
schemaVersion: "heimdall.transport_profile.v1",
payload: buildTransportProfile(config, pulse),
storedAt: pulse.updatedAt,
},
];
}
export function buildHeimdallHealthDetail(config: HeimdallConfig, pulse: HeimdallRuntimePulse): string {
const healthTransport = config.idunnRudpHealth ? "CultNet/RUDP" : "CultCache witness only";
return `Heimdall auth runtime active; storage=${pulse.storageBackend}; providers=${pulse.configuredProviders.length}/${providers.length}; apps=${pulse.appProfileCount}; healthTransport=${healthTransport}`;
}
function buildRuntimeProviderAdvertisement(config: HeimdallConfig, pulse: HeimdallRuntimePulse) {
const advertisement = buildHeimdallProviderAdvertisement({ updatedAt: pulse.updatedAt });
const witnessPath = path.relative(config.workspaceRoot, config.cultCachePath).replace(/\\/g, "/");
return {
...advertisement,
status: "daemon_live" as const,
version: "daemon-live-v1",
provider: {
...advertisement.provider,
transport: config.idunnRudpHealth
? "CultCache witness store + daemon-published Idunn health over CultNet/RUDP."
: "CultCache witness store; Idunn health still falls back to local witness publication.",
},
controlSurface: {
...advertisement.controlSurface,
controls: {
...advertisement.controlSurface.controls,
reason: "This runtime publication advertises provider, plugin, and lifecycle boundaries. Auth mutation remains behind Heimdall's authenticated private command plane or retained HTTP compatibility APIs.",
},
},
runtime: {
storageBackend: pulse.storageBackend,
configuredProviders: pulse.configuredProviders,
appProfileCount: pulse.appProfileCount,
cultCachePath: witnessPath,
idunnHealthContract: config.idunnHealthContract,
idunnRudpHealth: config.idunnRudpHealth ?? null,
privateCommandRoute: `rudp://${config.privateCommandHost ?? "127.0.0.1"}:${config.privateCommandPort ?? 4101}`,
},
};
}
function buildCommandBoundary(config: HeimdallConfig, pulse: HeimdallRuntimePulse) {
return {
schema: "heimdall.command_boundary.v1",
boundaryId: "heimdall",
daemonId: config.daemonId,
providerId: "heimdall",
updatedAt: pulse.updatedAt,
owner: "Heimdall auth runtime",
lifecycleAuthority: "idunn.yggdrasil-source-app.deploy",
healthPublication: {
contract: config.idunnHealthContract,
transport: config.idunnRudpHealth ? "cultnet.transport.rudp.v0" : "cultcache-store",
publicationSource: config.idunnRudpHealth ? "daemon-published" : "daemon-published-cultcache",
stateOwner: "Heimdall auth runtime",
},
commands: [
{
operation: "heimdall.auth.begin",
requestSchema: "heimdall.private_command_envelope.v1",
responseSchema: "heimdall.auth_begin_receipt.v1",
},
{
operation: "heimdall.auth.complete",
requestSchema: "heimdall.private_command_envelope.v1",
responseSchema: "heimdall.auth_completion_receipt.v1",
},
{
operation: "heimdall.auth.refresh",
requestSchema: "heimdall.private_command_envelope.v1",
responseSchema: "heimdall.auth_refresh_receipt.v1",
},
{
operation: "heimdall.auth.logout",
requestSchema: "heimdall.private_command_envelope.v1",
responseSchema: "heimdall.auth_logout_receipt.v1",
},
],
privateRoute: {
endpoint: `rudp://${config.privateCommandHost ?? "127.0.0.1"}:${config.privateCommandPort ?? 4101}`,
exposure: "loopback-only",
authentication: "app-bound HMAC + AES-256-GCM envelope",
secretBearing: false,
},
forbiddenWriters: [
"Odin and Idunn may observe Heimdall boundary state but do not mutate auth/control-plane truth.",
"HTTP /healthz, JWKS, discovery, systemd, and nginx routing are compatibility witnesses, not daemon truth.",
],
compatibility: {
httpHealth: "/healthz",
jwks: "/.well-known/jwks.json",
discovery: "/.well-known/heimdall-configuration",
yggdrasilDeployLane: "E:/Projects/Odin/scripts/deploy-yggdrasil-heimdall.cmd",
status: "fallback-witness-only",
},
};
}
function buildTransportProfile(config: HeimdallConfig, pulse: HeimdallRuntimePulse) {
return {
schema: "heimdall.transport_profile.v1",
profileId: "heimdall",
daemonId: config.daemonId,
providerId: "heimdall",
updatedAt: pulse.updatedAt,
targetTransport: "cultnet.transport.rudp.v0",
currentTransport: config.idunnRudpHealth
? "cultcache-redacted-witness + daemon-published-rudp-health + compatibility-http"
: "cultcache-redacted-witness + compatibility.ssh-systemd-http",
healthTransport: config.idunnRudpHealth ? "cultnet.transport.rudp.v0" : "cultcache-store",
stateTransport: "cultcache-store",
rendererTransport: "browser-http-jwks-discovery-lowering",
storageBackend: pulse.storageBackend,
cutLine: config.idunnRudpHealth
? "Heimdall now publishes daemon-owned witness state and Idunn health; systemd, /healthz, JWKS, discovery, and nginx routing can be demoted to deployment/debug witnesses once Odin consumes the typed store."
: "Heimdall now publishes a daemon-owned witness store; the remaining transport debt is Idunn health publication over CultNet/RUDP and Odin ingestion of the typed witness surface.",
};
}
function buildDaemonHealthRecord(config: HeimdallConfig, pulse: HeimdallRuntimePulse) {
return [
config.daemonId,
"active",
buildHeimdallHealthDetail(config, pulse),
pulse.updatedAt,
config.idunnHealthContract,
config.idunnRudpHealth ? "daemon-published" : "daemon-published-cultcache",
config.idunnRudpHealth ? "cultnet.transport.rudp.v0" : "cultcache-store",
];
}
async function writeCultCacheSnapshot(filePath: string, records: CultCacheRecord[]): Promise<void> {
await mkdir(path.dirname(filePath), { recursive: true });
const catalog = records.map((record) => [
record.schemaId,
record.schemaName,
record.schemaVersion,
record.schemaId,
JSON.stringify({
schemaName: record.schemaName,
schemaVersion: record.schemaVersion,
members: [],
}),
[record.schemaId],
[],
]);
const entries = records.map((record) => [record.key, record.schemaId, record.storedAt, encode(record.payload)]);
const tempPath = `${filePath}.tmp-${process.pid}-${Date.now()}`;
try {
await writeFile(tempPath, encode([STORE_FORMAT_VERSION, catalog, entries]));
await rename(tempPath, filePath);
} catch (error) {
await rm(tempPath, { force: true }).catch(() => undefined);
throw error;
}
}