Retract the CultLib divergence claim; read each .cc with the right client

I claimed this morning that cultcache-ts could not read a cultcache-rs
store and that the CultLib forks had diverged. That was false, and it was
false in the usual way: generalised from one file.

cultcache-ts reads Idunn's live control store on yggdrasil directly -- 36
records, no complaint. Ordinary CultCache stores use the same framing in
both runtimes, and that covers the control store, the runtime bundle's
expected.cc and activation.cc, and the process write lease. The exception
is narrow and deliberate: service identity private stores are written by
cultnet-rs's atomic_create_private_store as a single bare positional
envelope, because they are a private-key container rather than a record
store.

The consequence was a live bug. Both readers I wrote decoded the
private-store framing, so the write lease and expected-incarnation readers
would have failed on the first real deployment -- with green tests, because
I had built the fixtures the same wrong way. process-write-lease.ts now
uses the CultCache client, provider-health-identity.ts keeps the bare
reader for the fd-passed identity where it belongs, and the lease fixtures
are written with the client so a test can disagree with me.

Also corrected the write-up in Idunn/docs/migration.md and the memory that
recorded the false finding. Three places had it wrong; leaving any of them
would have taught the next session the same mistake.

54 pass, 1 skipped, 12 files; tsc clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-06 19:11:19 +02:00
commit 8086d5155e
4 changed files with 60 additions and 52 deletions

View file

@ -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<Uint8Array> {
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;
}

View file

@ -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<Uint8Array> {
return readIdunnStoreRecord(await readFile(file), label);
return readIdunnStoreRecord(file, label);
}
/**

View file

@ -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"
);

View file

@ -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<void> {
// 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 };