From 8072314b7c47c20f594d7585250296949299904e Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 29 Sep 2026 03:11:48 +0200 Subject: [PATCH] cultnet-ts: the Idunn signer owns the reported health and warming proofs The signer holds the last reported health, starting at warming, and gains reportHealth(state). sign(detail), answerRouteObservation(request) and the Odin publisher's publish(detail) no longer take a state, so the route answer and the Odin publish cannot disagree and a fresh signer cannot answer active. Every warming presence the signer signs becomes a valid write-lease target; the publisher's recordPublishedWarming is deleted. Tests now assert every committed stateless vector, dead ones included. Co-Authored-By: Claude Sonnet 5 --- .../src/idunn-odin-presence-publisher.ts | 11 +- .../cultnet-ts/src/idunn-runtime-authority.ts | 42 +++++--- .../test/idunn-runtime-authority.test.ts | 100 ++++++++++++------ 3 files changed, 100 insertions(+), 53 deletions(-) diff --git a/packages/cultnet-ts/src/idunn-odin-presence-publisher.ts b/packages/cultnet-ts/src/idunn-odin-presence-publisher.ts index 8e13acb4..2e992658 100644 --- a/packages/cultnet-ts/src/idunn-odin-presence-publisher.ts +++ b/packages/cultnet-ts/src/idunn-odin-presence-publisher.ts @@ -5,7 +5,6 @@ import { encodeCultNetMessageForWire, parseCultNetMessage } from "./contracts"; import type { IdunnRuntimeSigner } from "./idunn-runtime-authority"; import { CultNetRudpSession, decodeRudpPacket, encodeRudpPacket } from "./rudp"; import type { CultNetRudpPacket } from "./rudp"; -import type { RuntimePresenceHealth } from "./runtime-presence-health"; const ODIN_CAPABILITY = "odin.verse-rendezvous"; const MAX_LEASE_WAIT_ROUNDS = 720; @@ -14,7 +13,8 @@ export type IdunnRuntimePresencePublisher = { readonly runtimeId: string; readonly runtimeInstanceId: string; readonly requiresWriteLease: boolean; - publish(state: RuntimePresenceHealth["state"], detail: string): Promise; + /** Publishes the signer's presence in the health the signer reports. */ + publish(detail: string): Promise; waitForWriteLease(options?: { pollIntervalMs?: number; signal?: AbortSignal }): Promise; assertWriteLease(): Promise; }; @@ -44,15 +44,14 @@ export function createIdunnRuntimePresencePublisher( const connectionId = 0x0d1d0002; let messageSequence = 0; - const publish = async (state: RuntimePresenceHealth["state"], detail: string) => { - const signed = signer.sign(state, detail); + const publish = async (detail: string) => { + const signed = signer.sign(detail); messageSequence += 1; await publishDocument(endpoint, connectionId, { schemaVersion: "cultnet.document_put_raw.v0", messageId: `runtime-presence:${signer.authority.expected.target}:${signer.runtimeInstanceId}:${messageSequence}`, document: signed.document, }); - if (state === "warming") signer.recordPublishedWarming(signed.canonicalSha256); return signed.canonicalSha256; }; @@ -65,7 +64,7 @@ export function createIdunnRuntimePresencePublisher( if (!signer.requiresWriteLease) return ""; let warmingSequence = 0; while (!signal?.aborted) { - await publish("warming", "waiting-for-process-write-lease"); + await publish("waiting-for-process-write-lease"); warmingSequence += 1; try { return signer.assertWriteLease(); diff --git a/packages/cultnet-ts/src/idunn-runtime-authority.ts b/packages/cultnet-ts/src/idunn-runtime-authority.ts index 47e132b3..bb217ed0 100644 --- a/packages/cultnet-ts/src/idunn-runtime-authority.ts +++ b/packages/cultnet-ts/src/idunn-runtime-authority.ts @@ -82,25 +82,30 @@ export type SignedRuntimePresence = { /** * Signs this launch's runtime presence and answers Idunn's route challenge. It * owns no network: Odin publication, if a service wants it, is a separate - * consumer of `sign`. + * consumer of `sign`. The signer is the single owner of the reported health: + * it starts `warming`, the app moves it with `reportHealth`, and every + * presence it signs, however it is carried, states that health. */ export type IdunnRuntimeSigner = { readonly authority: IdunnRuntimeAuthority; readonly runtimeId: string; readonly runtimeInstanceId: string; readonly requiresWriteLease: boolean; - /** Signs one presence document. Every call takes the next publisher sequence. */ - sign(state: RuntimePresenceHealth["state"], detail: string): SignedRuntimePresence; + /** Records the app's health. Until the first call the signer reports `warming`. */ + reportHealth(state: RuntimePresenceHealth["state"]): void; + /** + * Signs one presence document in the reported health. Every call takes the + * next publisher sequence; every warming presence signed becomes a valid + * write-lease target. + */ + sign(detail: string): SignedRuntimePresence; /** * Answers an Idunn route challenge from local authority alone: signs a fresh - * presence in the given state, with no network. The caller supplies the - * app's current health (`warming` until first healthy). + * presence in the reported health, with no network. */ - answerRouteObservation(request: CultNetSnapshotRequestMessage, state: RuntimePresenceHealth["state"]): CultNetRawDocumentRecord; - /** Reads Idunn's write lease and checks it names this incarnation and a warming proof this process published. */ + answerRouteObservation(request: CultNetSnapshotRequestMessage): CultNetRawDocumentRecord; + /** Reads Idunn's write lease and checks it names this incarnation and a warming presence this signer signed. */ assertWriteLease(): string; - /** Marks a warming presence as accepted by its consumer, making it a valid lease target. */ - recordPublishedWarming(canonicalSha256: string): void; }; export type IdunnRuntimeSignerOptions = { @@ -219,6 +224,7 @@ export function createIdunnRuntimeSigner(options: IdunnRuntimeSignerOptions): Id const { authority } = options; const expected = authority.expected; let sequence = 0; + let health: RuntimePresenceHealth["state"] = "warming"; const recentWarmingProofs: string[] = []; const assertWriteLease = () => { @@ -231,7 +237,8 @@ export function createIdunnRuntimeSigner(options: IdunnRuntimeSignerOptions): Id return prefixedSha256(Buffer.from(encode(lease))); }; - const sign = (state: RuntimePresenceHealth["state"], detail: string): SignedRuntimePresence => { + const sign = (detail: string): SignedRuntimePresence => { + const state = health; const writeLeaseSha256 = state === "warming" || !expected.writeLeaseRequired ? null : assertWriteLease(); sequence += 1; const signed = signIdunnRuntimePresence(authority, { @@ -242,6 +249,10 @@ export function createIdunnRuntimeSigner(options: IdunnRuntimeSignerOptions): Id publisherSequence: sequence, observedAtUnixMillis: Date.now(), }); + if (state === "warming") { + recentWarmingProofs.push(signed.canonicalSha256); + if (recentWarmingProofs.length > MAX_RECENT_WARMING_PROOFS) recentWarmingProofs.shift(); + } return { canonicalSha256: signed.canonicalSha256, document: { @@ -262,8 +273,11 @@ export function createIdunnRuntimeSigner(options: IdunnRuntimeSignerOptions): Id runtimeId: expected.runtimeId, runtimeInstanceId: authority.activation.runtimeInstanceId, requiresWriteLease: expected.writeLeaseRequired, + reportHealth(state) { + health = state; + }, sign, - answerRouteObservation(request, state) { + answerRouteObservation(request) { if (!request.messageId || request.messageId.trim() !== request.messageId) throw new Error("Idunn route observation requires a message id."); if ((request.schemaIds && !request.schemaIds.includes(GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA)) || (request.recordKeys && !request.recordKeys.includes(expected.target)) @@ -271,13 +285,9 @@ export function createIdunnRuntimeSigner(options: IdunnRuntimeSignerOptions): Id || request.shardEpoch !== undefined) { throw new Error("Idunn route observation does not request this runtime presence document."); } - return sign(state, `route-observation:${request.messageId}`).document; + return sign(`route-observation:${request.messageId}`).document; }, assertWriteLease, - recordPublishedWarming(canonicalSha256) { - recentWarmingProofs.push(canonicalSha256); - if (recentWarmingProofs.length > MAX_RECENT_WARMING_PROOFS) recentWarmingProofs.shift(); - }, }; } diff --git a/packages/cultnet-ts/test/idunn-runtime-authority.test.ts b/packages/cultnet-ts/test/idunn-runtime-authority.test.ts index 1639b9aa..1df6e98f 100644 --- a/packages/cultnet-ts/test/idunn-runtime-authority.test.ts +++ b/packages/cultnet-ts/test/idunn-runtime-authority.test.ts @@ -25,7 +25,7 @@ const CHALLENGE: CultNetSnapshotRequestMessage = { messageId: "route-challenge-1", }; -type FixtureSet = "web" | "service" | "rudp-route"; +type FixtureSet = "web" | "service" | "rudp-route" | "service-stateful"; function fixtureFile(set: FixtureSet, name: string): Buffer { return fs.readFileSync(path.join(FIXTURES, set, name)); @@ -50,7 +50,11 @@ function expectedFacts(set: FixtureSet) { } /** Points the process at one Rust-written bundle and its inherited descriptors, as Idunn's systemd unit would. */ -function openAuthority(context: TestContext, set: FixtureSet, options: { machineId?: "etc" | "dbus" } = {}): IdunnRuntimeAuthority { +function openAuthority( + context: TestContext, + set: FixtureSet, + options: { machineId?: "etc" | "dbus"; lease?: string } = {}, +): IdunnRuntimeAuthority { const facts = expectedFacts(set); const originalEnvironment = { ...process.env }; context.after(() => { @@ -59,7 +63,8 @@ function openAuthority(context: TestContext, set: FixtureSet, options: { machine }); process.env.GAMECULT_IDUNN_RUNTIME_BUNDLE = path.join(FIXTURES, set); process.env.GAMECULT_IDUNN_CANDIDATE_BIND = facts.candidate; - delete process.env.GAMECULT_IDUNN_PROCESS_WRITE_LEASE; + if (options.lease) process.env.GAMECULT_IDUNN_PROCESS_WRITE_LEASE = path.join(FIXTURES, set, options.lease); + else delete process.env.GAMECULT_IDUNN_PROCESS_WRITE_LEASE; process.env.LISTEN_PID = String(process.pid); process.env.LISTEN_FDS = "2"; process.env.LISTEN_FDNAMES = "gamecult-idunn-runtime-activation-key:gamecult-runtime-presence-identity"; @@ -78,8 +83,8 @@ function openAuthority(context: TestContext, set: FixtureSet, options: { machine return loadIdunnRuntimeAuthorityFromEnvironment(facts.target, facts.healthContract); } -function signerFor(context: TestContext, set: FixtureSet, capacityDelta = 0) { - const authority = openAuthority(context, set); +function signerFor(context: TestContext, set: FixtureSet, capacityDelta = 0, lease?: string) { + const authority = openAuthority(context, set, { lease }); const capabilities = expectedFacts(set).capabilities.map((capability) => ({ ...capability, capacity: capability.capacity + capacityDelta })); return createIdunnRuntimeSigner({ authority, capabilities }); } @@ -107,7 +112,7 @@ for (const set of ["web", "service", "rudp-route"] as const) { test("a target that declares no Odin dependency opens and answers Idunn's challenge without one", (context) => { assert.equal(expectedFacts("web").odinEndpoint, undefined); const signer = signerFor(context, "web"); - assert.ok(signer.answerRouteObservation(CHALLENGE, "warming").payload.byteLength > 0); + assert.ok(signer.answerRouteObservation(CHALLENGE).payload.byteLength > 0); }); test("finds the machine-id at the dbus path when /etc/machine-id is absent, as the Rust protector does", (context) => { @@ -127,35 +132,44 @@ test("rejects a credential file holding more than one envelope", (context) => { ); }); -test("answers a route challenge byte for byte as the Rust-verified vectors, in the state the app reports", (context) => { +function presenceSlot(document: { payload: Uint8Array }, slot: number): unknown { + return (decode(document.payload) as unknown[])[slot]; +} + +// Every committed vector is asserted, dead ones included. Stateless sets sign at sequence 1. +for (const set of ["web", "service", "rudp-route"] as const) { + test(`${set}: a fresh signer answers warming, and after reportHealth('active') answers active, byte for byte as the Rust-verified vectors`, (context) => { + freezeClock(context); + assert.deepEqual(Buffer.from(signerFor(context, set).answerRouteObservation(CHALLENGE).payload), fixtureFile(set, "presence-warming.bin")); + const active = signerFor(context, set); + active.reportHealth("active"); + assert.deepEqual(Buffer.from(active.answerRouteObservation(CHALLENGE).payload), fixtureFile(set, "presence-active.bin")); + }); +} + +test("a shortfall signs the vector Rust correlates to the typed capacity disagreement", (context) => { freezeClock(context); - const signer = signerFor(context, "web"); - const warming = signer.answerRouteObservation(CHALLENGE, "warming"); - assert.deepEqual(Buffer.from(warming.payload), fixtureFile("web", "presence-warming.bin")); - assert.equal((decode(warming.payload) as unknown[])[RUNTIME_PRESENCE_SLOT.state], "warming"); - const second = signer.answerRouteObservation(CHALLENGE, "active"); - assert.equal((decode(second.payload) as unknown[])[RUNTIME_PRESENCE_SLOT.publisherSequence], 2); - // Sequence 2 cannot match a vector written at sequence 1; sign the same fields from a fresh signer. - const fresh = signerFor(context, "web"); - assert.deepEqual(Buffer.from(fresh.answerRouteObservation(CHALLENGE, "active").payload), fixtureFile("web", "presence-active.bin")); + const signer = signerFor(context, "service", -1); + signer.reportHealth("active"); + assert.deepEqual(Buffer.from(signer.answerRouteObservation(CHALLENGE).payload), fixtureFile("service", "presence-active-below-minimum.bin")); }); -test("a service with an Odin dependency signs the Rust-verified vectors, including a shortfall Rust correlates to a capacity disagreement", (context) => { +test("the signer owns the reported health: a fresh signer is warming, reportHealth moves it, the sequence keeps counting", (context) => { freezeClock(context); - assert.deepEqual( - Buffer.from(signerFor(context, "service").answerRouteObservation(CHALLENGE, "active").payload), - fixtureFile("service", "presence-active.bin"), - ); - assert.deepEqual( - Buffer.from(signerFor(context, "service", -1).answerRouteObservation(CHALLENGE, "active").payload), - fixtureFile("service", "presence-active-below-minimum.bin"), - ); + const signer = signerFor(context, "web"); + const first = signer.answerRouteObservation(CHALLENGE); + assert.equal(presenceSlot(first, RUNTIME_PRESENCE_SLOT.state), "warming"); + assert.equal(presenceSlot(signer.sign("detail"), RUNTIME_PRESENCE_SLOT.state), "warming"); + signer.reportHealth("active"); + const third = signer.answerRouteObservation(CHALLENGE); + assert.equal(presenceSlot(third, RUNTIME_PRESENCE_SLOT.state), "active"); + assert.equal(presenceSlot(third, RUNTIME_PRESENCE_SLOT.publisherSequence), 3); }); test("answers locally: synchronously, with no socket opened", (context) => { const sockets = context.mock.method(dgram, "createSocket"); const signer = signerFor(context, "web"); - const answer = signer.answerRouteObservation(CHALLENGE, "warming"); + const answer = signer.answerRouteObservation(CHALLENGE); assert.equal(typeof (answer as unknown as { then?: unknown }).then, "undefined"); assert.equal(sockets.mock.callCount(), 0); }); @@ -169,7 +183,7 @@ test("refuses a challenge that does not ask for this runtime's presence", (conte { ...CHALLENGE, schemaIds: ["another.schema"] }, { ...CHALLENGE, shardId: "shard-1" }, ]; - for (const request of cases) assert.throws(() => signer.answerRouteObservation(request, "active"), /route observation/); + for (const request of cases) assert.throws(() => signer.answerRouteObservation(request), /route observation/); }); async function startOdinPeer(context: TestContext, endpoint: string) { @@ -222,7 +236,7 @@ test("the Odin publisher publishes the signer's document to the endpoint Expecte const signer = signerFor(context, "service"); const publisher = createIdunnRuntimePresencePublisher({ signer, endpoint }); - await publisher.publish("warming", "accepted presence"); + await publisher.publish("accepted presence"); assert.equal(peer.received[0]?.schemaVersion, "cultnet.document_put_raw.v0"); const published = decode(peer.received[0]!.document.payload) as unknown[]; assert.equal(published[RUNTIME_PRESENCE_SLOT.state], "warming"); @@ -230,15 +244,18 @@ test("the Odin publisher publishes the signer's document to the endpoint Expecte assert.equal(peer.responseAck, 2, "the response data packet must acknowledge the publisher's reliable schema packet"); peer.reject = true; - await assert.rejects(publisher.publish("active", "denied presence"), /Odin rejected runtime presence: test admission denied/); + signer.reportHealth("active"); + await assert.rejects(publisher.publish("denied presence"), /Odin rejected runtime presence: test admission denied/); assert.equal(peer.failure, undefined, peer.failure?.message); }); test("the Odin publisher leaves a capability shortfall to the Rust correlation instead of refusing to publish", async (context) => { const endpoint = expectedFacts("service").odinEndpoint!; const peer = await startOdinPeer(context, endpoint); - const publisher = createIdunnRuntimePresencePublisher({ signer: signerFor(context, "service", -1), endpoint }); - await publisher.publish("active", "below minimum"); + const signer = signerFor(context, "service", -1); + signer.reportHealth("active"); + const publisher = createIdunnRuntimePresencePublisher({ signer, endpoint }); + await publisher.publish("below minimum"); const capabilities = (decode(peer.received[0]!.document.payload) as unknown[])[RUNTIME_PRESENCE_SLOT.capabilities] as unknown[][]; assert.equal(capabilities[0]![3], expectedFacts("service").capabilities[0]!.capacity - 1); }); @@ -253,3 +270,24 @@ test("the Odin publisher takes only the endpoint Expected names", (context) => { /does not match Idunn Expected dependency authority/, ); }); + +test("the route answer and the Odin publish report the same state, because the signer owns it", async (context) => { + const endpoint = expectedFacts("service").odinEndpoint!; + const peer = await startOdinPeer(context, endpoint); + const signer = signerFor(context, "service"); + const publisher = createIdunnRuntimePresencePublisher({ signer, endpoint }); + + await publisher.publish("first"); + assert.equal(presenceSlot(peer.received[0]!.document, RUNTIME_PRESENCE_SLOT.state), "warming"); + assert.equal(presenceSlot(signer.answerRouteObservation(CHALLENGE), RUNTIME_PRESENCE_SLOT.state), "warming"); + + signer.reportHealth("active"); + await publisher.publish("second"); + assert.equal(presenceSlot(peer.received[1]!.document, RUNTIME_PRESENCE_SLOT.state), "active"); + assert.equal(presenceSlot(signer.answerRouteObservation(CHALLENGE), RUNTIME_PRESENCE_SLOT.state), "active"); + // One sequence across both carriers. + assert.deepEqual( + [1, 2].map((index) => presenceSlot(peer.received[index - 1]!.document, RUNTIME_PRESENCE_SLOT.publisherSequence)), + [1, 3], + ); +});