cultcache-ts store: an identical rewrite keeps only the stamp, the writer's type and catalog still land; the push result must parse as a stamp; CHANGELOG states the departure from C#
All checks were successful
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Successful in 22m4s

Fixes variants:finding:cut-update-with-retry-ts.s3.identical-drops-writer-catalog and .identical-rule-undeclared.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Libby 2026-11-13 18:49:12 +00:00
commit dd961bc743
4 changed files with 42 additions and 14 deletions

View file

@ -12,7 +12,7 @@ All notable changes to `@gamecult/cultcache-ts` are recorded here, following
### Changed
- A write that replaces a record at its key is stored strictly later than it: a supplied `storedAt` not later than the stored one is written one millisecond after it, so a content version that includes `storedAt` never repeats across writes. `push`, `pushAll` and `commitIf` alike; the single-file store's `push` returns the stamp it wrote (`CacheBackingStore.push` may resolve `{ storedAt }`), and `put`, `putGlobal` and `putEnvelope` install that stamp in the cache, so the cache and the file agree after a put over a record stamped at or after now. A stamp that would pass 9999-12-31T23:59:59.999Z is refused with `RangeError` and nothing is written.
- A write that replaces a record at its key with different content (another schema id or other payload bytes) is stored strictly later than it: a supplied `storedAt` not later than the stored one is written one millisecond after it, so a content version that includes `storedAt` never repeats across writes. A rewrite of identical content (same schema id and payload bytes) keeps the stored stamp and so its version, which is what keeps a periodic flush of an unchanged cache from changing every record's version; C# re-mints every replacing write, so this is a departure from it, and the writer's type and catalog entry are written either way. `push`, `pushAll` and `commitIf` alike; the single-file store's `push` returns the stamp it wrote (`CacheBackingStore.push` may resolve `{ storedAt }`), and `put`, `putGlobal` and `putEnvelope` install that stamp in the cache, so the cache and the file agree after a put over a record stamped at or after now (a push result that is not a parseable stamp is ignored). A stamp that would pass 9999-12-31T23:59:59.999Z is refused with `RangeError` and nothing is written.
- `withStoreWriteLock` looks for a link at the store path again once the lock is held, so a link put at the path while a writer waited on the lock is refused with `StoreLinkedError` and the lock released, instead of being replaced. Pinned by a test that puts the link while another holder owns the lock.
- `CultCache.isWritable` does not look at store paths: a link at a store path is not predicted, and the write refuses it with `StoreLinkedError`. Pinned by a test through a cache attached to a linked store.
@ -29,7 +29,7 @@ All notable changes to `@gamecult/cultcache-ts` are recorded here, following
the many-row selection aliases (never indexed for lookup, same catalog `indexAlias`). The per-key migration rule is
in the README; classify every key before the CultLib your tools load is updated. The refusal verdict is the contract, its text is informational.
- The single-file reader (v1 and the legacy envelope array) refuses a store that holds two records under one key as `StoreUnreadableError`; a store that opened before may now be refused. Repair it by removing the extra record with the runtime that wrote it.
- A `put`, `push` or `pushAll` that would land a second type under a held key (against the file, or within one batch) is refused with `SchemaConflictError`, and nothing is written. The written type may replace the record when its catalog entry carries the stored schema id as its own or lists it compatible (a type renamed under a stable id restamps the record, as C# does); otherwise retyping a key is a delete, then a put. The C# allowance for an undeclared older id of the same schema name that reads whole is not ported: a same-named type keeps passing, including one that drops persisted slots, which C# refuses.
- A `put`, `push` or `pushAll` that would land a second type under a held key (against the file, or within one batch) is refused with `SchemaConflictError`, and nothing is written. The written type may replace the record when its catalog entry carries the stored schema id as its own or lists it compatible (a type renamed under a stable id restamps the record, as C# does, when its payload changes; an identical payload keeps the stamp); otherwise retyping a key is a delete, then a put. The C# allowance for an undeclared older id of the same schema name that reads whole is not ported: a same-named type keeps passing, including one that drops persisted slots, which C# refuses.
- Same-type writes replace by key; in one `pushAll` batch the last entry for a key wins.
- A store whose canonical-schema catalog slot is not a string is refused as `StoreUnreadableError`.
- The cache holds one record per key (variants:ruling:cache-refuses-retype). A `put` or `putEnvelope` that lands a second type under a key the cache holds, or under a key a store carries foreign, is refused with `SchemaConflictError` naming the key, even with no store (a memory-only cache used to hold both types at the key). A type that owns the held record's schema id (as for the store) replaces it, unless the held record is in a store other than the written type's home: that rename is refused with `SchemaConflictError` (remove the record first). Two stores holding one key, registered or foreign, refuse the load and admit nothing. A store's `delete` removes the record whose stored schema id the removal's type owns, whatever the arrived record's `type` string.

View file

@ -879,7 +879,7 @@ export class CultCache {
} else {
// A store that re-stamps a write (storedAt later than the record it replaces) reports the stamp; the cache holds that one.
const pushed = await home?.push(entry);
if (typeof pushed === "object" && pushed !== null && typeof pushed.storedAt === "string" && pushed.storedAt.length > 0) {
if (typeof pushed === "object" && pushed !== null && typeof pushed.storedAt === "string" && !Number.isNaN(Date.parse(pushed.storedAt))) {
hydrated.storedAt = pushed.storedAt;
}
}

View file

@ -166,9 +166,10 @@ export class SingleFileMessagePackBackingStore implements CacheBackingStore {
if (prior === undefined) {
return entry;
}
// A rewrite of the identical record (same schema id and payload bytes) keeps the stamp and so the version it has.
// A rewrite of the identical record (same schema id and payload bytes) keeps the stamp and so the version it has; the
// writer's type and catalog entry still go down with it.
if (sameContent(entry, prior)) {
return prior;
return { ...entry, storedAt: prior.storedAt };
}
return { ...entry, storedAt: laterStoredAt(entry.storedAt, prior.storedAt) };
});

View file

@ -11,7 +11,7 @@ import { z } from "zod";
import { CultCache } from "../src/cult-cache";
import { defineDocumentRegistry, defineDocumentType, documentPayloadBytes, documentPayloadFromBytes } from "../src/document";
import { SingleFileMessagePackBackingStore, readSingleFileStore } from "../src/single-file-messagepack-backing-store";
import { SchemaConflictError } from "../src/store-format";
import { SchemaConflictError, schemaIdForEnvelope } from "../src/store-format";
import type { CacheBackingStore } from "../src/types";
import { envelopeOf, memoryStore, read, surfaceDocument } from "./union-surface-fixture";
@ -562,12 +562,39 @@ test("a changed record is re-stamped later than the one it replaces, and a rever
});
});
test("a push result that is not a stamp is ignored", async () => {
const entries: Parameters<typeof memoryStore>[0] = [];
const store = { ...memoryStore(entries), push: async (entry: (typeof entries)[number]) => { entries.push(entry); return true as never; } };
const cache = CultCache.builder().withRegistry(registry).withGenericStore(store).build();
await cache.put(counterDocument, KEY, { value: 1 });
const held = cache.getEnvelope(counterDocument, KEY)!.storedAt;
assert.equal(typeof held, "string");
assert.ok(held.length > 0);
test("a push result that is not a parseable stamp is ignored", async () => {
for (const result of [true, {}, "", "garbage", { storedAt: "" }, { storedAt: "garbage" }]) {
const entries: Parameters<typeof memoryStore>[0] = [];
const store = { ...memoryStore(entries), push: async (entry: (typeof entries)[number]) => { entries.push(entry); return result as never; } };
const cache = CultCache.builder().withRegistry(registry).withGenericStore(store).build();
await cache.put(counterDocument, KEY, { value: 1 });
const held = cache.getEnvelope(counterDocument, KEY)!.storedAt;
assert.equal(Number.isNaN(Date.parse(held)), false, `held stamp ${held} after a push result of ${JSON.stringify(result)}`);
}
});
test("a rewrite of identical content keeps the stamp but writes the writer's type and catalog entry", async () => {
await withDirectory(async (path) => {
const old = defineDocumentType({ type: "update.rn.old", schemaId: "update.rn.v1", schemaName: "update.rn.old", schemaVersion: "update.rn.v1", schema: z.object({ value: z.number().int() }) });
const renamed = defineDocumentType({ type: "update.rn.new", schemaId: "update.rn.v1", schemaName: "update.rn.new", schemaVersion: "update.rn.v1", schema: z.object({ value: z.number().int() }) });
await (await open(path, defineDocumentRegistry(old))).put(old, KEY, { value: 1 });
const before = readSingleFileStore(path).envelopes[0]!;
await (await open(path, defineDocumentRegistry(renamed))).put(renamed, KEY, { value: 1 });
const after = readSingleFileStore(path).envelopes[0]!;
assert.equal(after.storedAt, before.storedAt, "identical content keeps its stamp");
assert.equal(after.type, "update.rn.new", "the writer's type is what the file says");
});
});
test("a rewrite of the same bytes under another schema id is a changed record: stamped later, written under the new id", async () => {
await withDirectory(async (path) => {
const v1 = defineDocumentType({ type: "update.sid", schemaId: "update.sid.v1", schemaName: "update.sid", schemaVersion: "update.sid.v1", schema: z.object({ value: z.number().int() }) });
const v2 = defineDocumentType({ type: "update.sid", schemaId: "update.sid.v2", schemaName: "update.sid", schemaVersion: "update.sid.v2", compatibleSchemaIds: ["update.sid.v1"], schema: z.object({ value: z.number().int() }) });
await (await open(path, defineDocumentRegistry(v1))).put(v1, KEY, { value: 1 });
const before = readSingleFileStore(path).envelopes[0]!;
await (await open(path, defineDocumentRegistry(v2))).put(v2, KEY, { value: 1 });
const after = readSingleFileStore(path).envelopes[0]!;
assert.ok(Date.parse(after.storedAt) > Date.parse(before.storedAt));
assert.equal(schemaIdForEnvelope(after), "update.sid.v2");
});
});