diff --git a/src/idunn-store.ts b/src/idunn-store.ts index ed94853..c51cd0a 100644 --- a/src/idunn-store.ts +++ b/src/idunn-store.ts @@ -1,59 +1,64 @@ /** - * Reading the `.cc` stores Idunn writes. + * Reading the two `.cc` shapes Idunn hands a target. * - * Idunn writes with `cultcache-rs`, which lays a single-file store out as a - * bare MessagePack array of positional envelopes: + * Most Idunn-written stores — the control store, the runtime bundle's + * `expected.cc`, the process write lease — are ordinary CultCache stores in the + * standard framing, and `cultcache-ts` reads them directly. Verified against + * Idunn's live control store on yggdrasil: 36 records, no complaint. * - * [ [key, type, payload, stored_at, schema_id], ... ] + * Service identity *private* stores are the exception. `cultnet-rs` writes + * those with `atomic_create_private_store`, a deliberately minimal container + * holding one bare positional envelope: * - * `cultcache-ts` writes a different shape — `[formatVersion, catalog, records]` - * with envelopes as named objects — and **cannot read the Rust layout at all**; - * its schema rejects it with "expected object, received array". So Heimdall - * cannot use its own CultCache client to read an Idunn-written store, and this - * module exists to read exactly that one layout. + * [ [key, type, payload, stored_at, schema_id] ] * - * That divergence is a defect in CultLib, not a property of the format, and - * this module is a consumer-side reader for as long as it stands. If the two - * forks are reconciled, delete this and use the client. + * That is not a fork divergence, it is a different writer for a different job — + * a private key container rather than a record store — and `cultcache-ts` + * cannot parse it. `readPrivateStoreRecord` exists for that one shape. */ import { decode } from "@msgpack/msgpack"; +import { SingleFileMessagePackBackingStore } from "cultcache-ts"; const ENVELOPE_FIELD_COUNT = 5; const ENVELOPE_PAYLOAD = 2; /** - * Decode the payloads of every envelope in an Idunn-written store, in order. - * - * Accepts bytes rather than a path so callers can read from a file, or from a - * file descriptor systemd passed them. + * Read the single record from a service identity private store, given its + * bytes. Takes bytes rather than a path because this store is handed over as + * an open file descriptor, not a filename. */ -export function readIdunnStorePayloads(bytes: Uint8Array, label: string): Uint8Array[] { +export function readPrivateStoreRecord(bytes: Uint8Array, label: string): Uint8Array { const store = decode(bytes); - if (!Array.isArray(store)) { - throw new Error(`${label} is not a cultcache-rs single-file store`); + if (!Array.isArray(store) || store.length !== 1) { + throw new Error(`${label} is not a single-record private store`); } - return store.map((envelope, index) => { - if (!Array.isArray(envelope) || envelope.length !== ENVELOPE_FIELD_COUNT) { - throw new Error( - `${label} envelope ${index} is not the ${ENVELOPE_FIELD_COUNT}-field positional contract` - ); - } - - const payload = envelope[ENVELOPE_PAYLOAD]; - if (!(payload instanceof Uint8Array)) { - throw new Error(`${label} envelope ${index} has no binary payload`); - } - - return payload; - }); -} - -/** The single record an Idunn-written store is expected to hold. */ -export function readIdunnStoreRecord(bytes: Uint8Array, label: string): Uint8Array { - const payloads = readIdunnStorePayloads(bytes, label); - if (payloads.length !== 1) { - throw new Error(`${label} must contain exactly one record, found ${payloads.length}`); + const envelope = store[0]; + if (!Array.isArray(envelope) || envelope.length !== ENVELOPE_FIELD_COUNT) { + throw new Error( + `${label} envelope is not the ${ENVELOPE_FIELD_COUNT}-field positional contract` + ); } - return payloads[0]!; + + const payload = envelope[ENVELOPE_PAYLOAD]; + if (!(payload instanceof Uint8Array)) { + throw new Error(`${label} envelope has no binary payload`); + } + + return payload; +} + +/** + * Read the single record from an ordinary Idunn-written CultCache store. + * + * Uses the CultCache client rather than decoding by hand: this is the standard + * framing, and hand-decoding it is how a reader ends up agreeing with its own + * fixtures instead of with the writer. + */ +export async function readIdunnStoreRecord(file: string, label: string): Promise { + const records = await new SingleFileMessagePackBackingStore(file).pullAll(); + if (records.length !== 1) { + throw new Error(`${label} must contain exactly one record, found ${records.length}`); + } + return records[0]!.payload; } diff --git a/src/process-write-lease.ts b/src/process-write-lease.ts index 3d4f19f..c22aa05 100644 --- a/src/process-write-lease.ts +++ b/src/process-write-lease.ts @@ -16,7 +16,7 @@ * configured and the answer is an unconditional yes, because there is no second * generation to collide with. */ -import { readFile } from "node:fs/promises"; + import path from "node:path"; import { decode } from "@msgpack/msgpack"; @@ -86,7 +86,7 @@ function stringField(fields: unknown[], index: number, label: string): string { * `idunn-store.ts`. */ async function readEnvelopePayload(file: string, label: string): Promise { - return readIdunnStoreRecord(await readFile(file), label); + return readIdunnStoreRecord(file, label); } /** diff --git a/src/provider-health-identity.ts b/src/provider-health-identity.ts index 608013d..a4a4c64 100644 --- a/src/provider-health-identity.ts +++ b/src/provider-health-identity.ts @@ -10,7 +10,7 @@ import { import { readFileSync } from "node:fs"; import { chmod, readFile } from "node:fs/promises"; import { SingleFileMessagePackBackingStore, type CultCacheEnvelope } from "cultcache-ts"; -import { readIdunnStoreRecord } from "./idunn-store.js"; +import { readPrivateStoreRecord } from "./idunn-store.js"; const PRIVATE_SCHEMA = "gamecult.provider_health_identity.private.v1"; const PRIVATE_KEY = "gamecult-provider-health-identity"; @@ -130,7 +130,7 @@ export async function openProviderHealthIdentity( return openOrEnrollProviderHealthIdentity(path); } - const payload = readIdunnStoreRecord( + const payload = readPrivateStoreRecord( readFileSync(descriptor), "Idunn runtime presence identity" ); diff --git a/tests/process-write-lease.test.ts b/tests/process-write-lease.test.ts index 4d38461..4c40431 100644 --- a/tests/process-write-lease.test.ts +++ b/tests/process-write-lease.test.ts @@ -1,8 +1,9 @@ -import { mkdtemp, mkdir, writeFile } from "node:fs/promises"; +import { mkdtemp, mkdir } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { encode } from "@msgpack/msgpack"; +import { SingleFileMessagePackBackingStore } from "cultcache-ts"; import { describe, expect, it } from "vitest"; import { resolveWriteLease } from "../src/process-write-lease.js"; @@ -13,11 +14,16 @@ import { resolveWriteLease } from "../src/process-write-lease.js"; * the tests exercise the same bytes the daemon will actually be handed rather * than a convenient object. */ -function envelope(key: string, type: string, payload: Uint8Array): Uint8Array { - // cultcache-rs writes positional envelopes: [key, type, payload, stored_at, - // schema_id]. Building the fixture the way Idunn actually writes it is the - // point -- an object-shaped fixture agrees with a wrong reader. - return encode([[key, type, payload, "2026-09-06T00:00:00.000Z", type]]); +async function writeStore(file: string, type: string, payload: Uint8Array): Promise { + // Written with the CultCache client, the same way Idunn writes these. A + // hand-built fixture only proves the reader agrees with the fixture. + await new SingleFileMessagePackBackingStore(file).push({ + key: "heimdall", + type, + schemaId: type, + storedAt: "2026-09-06T00:00:00.000Z", + payload, + }); } function expectedIncarnation(target: string, incarnationId: string): Uint8Array { @@ -48,15 +54,12 @@ async function bundleWith( await mkdir(bundle, { recursive: true }); if (expectedBytes) { - await writeFile( - path.join(bundle, "expected.cc"), - envelope("heimdall", "idunn.expected_incarnation", expectedBytes) - ); + await writeStore(path.join(bundle, "expected.cc"), "idunn.expected_incarnation", expectedBytes); } const lease = path.join(root, "process-write-lease.cc"); if (leaseBytes) { - await writeFile(lease, envelope("heimdall", "idunn.process_write_lease", leaseBytes)); + await writeStore(lease, "idunn.process_write_lease", leaseBytes); } return { bundle, lease };