2026-09-29 12:31:12 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
using System.Globalization;
|
2026-09-29 12:31:12 +02:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using MessagePack;
|
|
|
|
|
|
|
|
|
|
namespace GameCult.Caching
|
|
|
|
|
{
|
|
|
|
|
// The identity of one element of an object list. A string member, keyed like any other, on every object type that
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
// sits in a list of a registered document type (union bases included: every concrete subtype carries the member).
|
2026-09-29 15:03:09 +02:00
|
|
|
// [CultElementId(nameof(Offset))] declares the id derived, never random: an unset id becomes the invariant-culture text of
|
|
|
|
|
// that member (a keyed string or number on the same type, unique within its list). Content-addressed elements use it so the
|
|
|
|
|
// same content always encodes to the same bytes.
|
2026-09-29 12:31:12 +02:00
|
|
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
|
|
|
|
|
public sealed class CultElementIdAttribute : Attribute
|
|
|
|
|
{
|
2026-09-29 15:03:09 +02:00
|
|
|
public CultElementIdAttribute(string? derivedFrom = null)
|
|
|
|
|
{
|
|
|
|
|
DerivedFrom = derivedFrom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string? DerivedFrom { get; }
|
2026-09-29 12:31:12 +02:00
|
|
|
}
|
|
|
|
|
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
// A record the id rule refuses: a duplicate id in one list, a random id that is not 12 lowercase hex characters, or a derived
|
|
|
|
|
// id whose source member is null or empty. It names the record, the list (record key, member slot, dictionary key and
|
|
|
|
|
// element index) and, where there is one, the element id and the source member.
|
|
|
|
|
public sealed class CultElementIdException : InvalidOperationException
|
|
|
|
|
{
|
|
|
|
|
public CultElementIdException(string message, string recordKey, string listPath, string? elementId = null, string? member = null)
|
|
|
|
|
: base(message)
|
|
|
|
|
{
|
|
|
|
|
RecordKey = recordKey;
|
|
|
|
|
ListPath = listPath;
|
|
|
|
|
ElementId = elementId;
|
|
|
|
|
Member = member;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string RecordKey { get; }
|
|
|
|
|
public string ListPath { get; }
|
|
|
|
|
public string? ElementId { get; }
|
|
|
|
|
public string? Member { get; }
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-29 12:31:12 +02:00
|
|
|
// The one owner of element identity: which types need an id, and how unset ids are filled.
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
// A random id is 12 lowercase hex characters (48 bits); a derived id is the invariant text of its source and only has to be
|
|
|
|
|
// non-empty. Either need only be unique within one list. A write mints a random id; a load of a store written before ids
|
|
|
|
|
// existed mints from SHA-256(record key, element path), so every reader of the same store mints the same id and a reload is
|
|
|
|
|
// identical to what was read. A write and a load refuse the same things.
|
2026-09-29 12:31:12 +02:00
|
|
|
internal static class CultElementIds
|
|
|
|
|
{
|
|
|
|
|
private const int IdBytes = 6;
|
|
|
|
|
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
// The ids a pass would fill, decided before anything is written: nothing changes until Apply, and Undo restores what
|
|
|
|
|
// Apply replaced.
|
|
|
|
|
internal sealed class IdPlan
|
|
|
|
|
{
|
|
|
|
|
private readonly List<(object Item, Shape Shape, string? Before, string Id)> _fills = new();
|
|
|
|
|
|
|
|
|
|
public int Count => _fills.Count;
|
|
|
|
|
|
2026-09-29 21:54:01 +02:00
|
|
|
// True when some element under the root already holds a non-empty id: one a store persisted, or one the caller set.
|
|
|
|
|
public bool HeldIds { get; internal set; }
|
|
|
|
|
|
|
|
|
|
// True when some element holds a non-empty id once the plan is applied: one already there, or one this plan fills.
|
|
|
|
|
public bool HoldsIds => HeldIds || _fills.Count > 0;
|
2026-09-29 19:40:36 +02:00
|
|
|
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
internal void Add(object item, Shape shape, string? before, string id) => _fills.Add((item, shape, before, id));
|
|
|
|
|
|
|
|
|
|
public void Apply()
|
|
|
|
|
{
|
|
|
|
|
foreach (var (item, shape, _, id) in _fills)
|
|
|
|
|
shape.SetId!(item, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Undo()
|
|
|
|
|
{
|
|
|
|
|
foreach (var (item, shape, before, _) in _fills)
|
|
|
|
|
shape.SetId!(item, before!);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal sealed class Shape
|
2026-09-29 12:31:12 +02:00
|
|
|
{
|
|
|
|
|
public Func<object, string?>? GetId;
|
|
|
|
|
public Action<object, string>? SetId;
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
public Func<object, string?>? Derive;
|
|
|
|
|
public string? DerivedFrom;
|
2026-09-29 12:31:12 +02:00
|
|
|
public (int Slot, Func<object, object?> Get)[] Walk = Array.Empty<(int, Func<object, object?>)>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly ConcurrentDictionary<Type, Shape> Shapes = new();
|
|
|
|
|
|
|
|
|
|
// ---- registration: which element types must carry an id ----
|
|
|
|
|
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
internal sealed class Survey
|
2026-09-29 12:31:12 +02:00
|
|
|
{
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
public List<string> Problems { get; } = new();
|
2026-09-29 12:31:12 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-29 19:40:36 +02:00
|
|
|
// Every reason this document type cannot be registered under the id rule, one per offending concrete element type.
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
internal static Survey Inspect(Type documentType)
|
|
|
|
|
{
|
|
|
|
|
var survey = new Survey();
|
|
|
|
|
VisitObject(documentType, documentType, new HashSet<Type>(), survey);
|
|
|
|
|
return survey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void VisitObject(Type documentType, Type type, HashSet<Type> seen, Survey survey)
|
2026-09-29 12:31:12 +02:00
|
|
|
{
|
|
|
|
|
if (!seen.Add(type))
|
|
|
|
|
return;
|
|
|
|
|
foreach (var (member, memberType) in KeyedMembers(type))
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
VisitMemberType(documentType, type, member, memberType, seen, survey);
|
2026-09-29 12:31:12 +02:00
|
|
|
foreach (var union in type.GetCustomAttributes<UnionAttribute>(true))
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
VisitObject(documentType, union.SubType, seen, survey);
|
2026-09-29 12:31:12 +02:00
|
|
|
}
|
|
|
|
|
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
private static void VisitMemberType(Type documentType, Type owner, MemberInfo member, Type memberType, HashSet<Type> seen, Survey survey)
|
2026-09-29 12:31:12 +02:00
|
|
|
{
|
|
|
|
|
memberType = Nullable.GetUnderlyingType(memberType) ?? memberType;
|
|
|
|
|
if (IsLeaf(memberType))
|
|
|
|
|
return;
|
|
|
|
|
if (DictionaryValueType(memberType) is { } valueType)
|
|
|
|
|
{
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
VisitMemberType(documentType, owner, member, valueType, seen, survey);
|
2026-09-29 12:31:12 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ElementTypeOf(memberType) is { } element)
|
|
|
|
|
{
|
|
|
|
|
if (IsObjectType(element))
|
|
|
|
|
{
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
foreach (var concrete in ConcreteTypes(element))
|
|
|
|
|
{
|
|
|
|
|
var problem = IdProblem(concrete);
|
|
|
|
|
if (problem != null)
|
|
|
|
|
survey.Problems.Add($"Cult document {documentType.Name}: list element type {concrete.Name} (in {owner.Name}.{member.Name}) {problem}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VisitObject(documentType, element, seen, survey);
|
2026-09-29 12:31:12 +02:00
|
|
|
}
|
|
|
|
|
else
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
VisitMemberType(documentType, owner, member, element, seen, survey);
|
2026-09-29 12:31:12 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IsObjectType(memberType))
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
VisitObject(documentType, memberType, seen, survey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The types an element can actually be: the element type itself when it can be instantiated, and every union subtype
|
|
|
|
|
// under it. Shapes are built from these, so registration judges exactly what minting will meet.
|
|
|
|
|
private static IEnumerable<Type> ConcreteTypes(Type element)
|
|
|
|
|
{
|
|
|
|
|
var seen = new HashSet<Type>();
|
|
|
|
|
return Expand(element).ToList();
|
|
|
|
|
|
|
|
|
|
IEnumerable<Type> Expand(Type type)
|
|
|
|
|
{
|
|
|
|
|
if (!seen.Add(type))
|
|
|
|
|
yield break;
|
|
|
|
|
if (!type.IsAbstract && !type.IsInterface)
|
|
|
|
|
yield return type;
|
|
|
|
|
foreach (var union in type.GetCustomAttributes<UnionAttribute>(true))
|
|
|
|
|
{
|
|
|
|
|
foreach (var subtype in Expand(union.SubType))
|
|
|
|
|
yield return subtype;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-29 12:31:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string? IdProblem(Type element)
|
|
|
|
|
{
|
|
|
|
|
var ids = MembersOf(element).Where(member => member.IsDefined(typeof(CultElementIdAttribute), true)).ToArray();
|
|
|
|
|
if (ids.Length == 0)
|
|
|
|
|
return "has no [CultElementId] member; add a public string member with [Key] and [CultElementId] so each element has an identity.";
|
|
|
|
|
if (ids.Length > 1)
|
|
|
|
|
return $"has two [CultElementId] members ({ids[0].Name}, {ids[1].Name}); an element has one identity.";
|
|
|
|
|
var id = ids[0];
|
|
|
|
|
if (TypeOf(id) != typeof(string))
|
|
|
|
|
return $"declares [CultElementId] on {id.Name}, which is not a string.";
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
if (KeyOf(id) is not { } idKey)
|
2026-09-29 12:31:12 +02:00
|
|
|
return $"declares [CultElementId] on {id.Name}, which has no integer [Key], so it is never persisted.";
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
var sharing = KeyedMembers(element).FirstOrDefault(entry => entry.Member != id && KeyOf(entry.Member) == idKey);
|
|
|
|
|
if (sharing.Member != null)
|
|
|
|
|
return $"declares [CultElementId] on {id.Name} with [Key({idKey})], which {sharing.Member.Name} also uses; an id needs a key of its own.";
|
2026-09-29 12:31:12 +02:00
|
|
|
if (id is PropertyInfo { SetMethod: null } or FieldInfo { IsInitOnly: true })
|
|
|
|
|
return $"declares [CultElementId] on {id.Name}, which cannot be assigned; the cache mints into it.";
|
2026-09-29 15:03:09 +02:00
|
|
|
var source = id.GetCustomAttribute<CultElementIdAttribute>(true)!.DerivedFrom;
|
|
|
|
|
if (source != null)
|
|
|
|
|
{
|
|
|
|
|
var from = MembersOf(element).FirstOrDefault(member => member.Name == source);
|
|
|
|
|
if (from == null || KeyOf(from) == null)
|
|
|
|
|
return $"derives its [CultElementId] from {source}, which is not a keyed member of the type.";
|
|
|
|
|
var kind = Nullable.GetUnderlyingType(TypeOf(from)) ?? TypeOf(from);
|
|
|
|
|
if (kind != typeof(string) && !kind.IsPrimitive)
|
|
|
|
|
return $"derives its [CultElementId] from {source}, which is not a string or a number.";
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-29 12:31:12 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- write and load: mint and check ----
|
|
|
|
|
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
// Decides every unset element id under root, and refuses what the rule refuses, without changing anything: a random id
|
|
|
|
|
// that is not 12 lowercase hex characters, a duplicate id in one list, a derived id whose source is null or empty.
|
2026-09-29 19:40:36 +02:00
|
|
|
// rootPath (the record key) seeds deterministic minting and names the record in a refusal. An element object that sits
|
|
|
|
|
// twice in one list is refused too: planning would give it two ids and it would then be a duplicate on load.
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
internal static IdPlan Plan(object? root, string rootPath, bool deterministic)
|
2026-09-29 12:31:12 +02:00
|
|
|
{
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
var plan = new IdPlan();
|
2026-09-29 12:31:12 +02:00
|
|
|
Visit(root, rootPath);
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
return plan;
|
2026-09-29 12:31:12 +02:00
|
|
|
|
|
|
|
|
void Visit(object? value, string path)
|
|
|
|
|
{
|
|
|
|
|
if (value == null)
|
|
|
|
|
return;
|
|
|
|
|
var type = value.GetType();
|
|
|
|
|
if (IsLeaf(type))
|
|
|
|
|
return;
|
|
|
|
|
if (value is IDictionary dictionary)
|
|
|
|
|
{
|
|
|
|
|
foreach (DictionaryEntry entry in dictionary)
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
Visit(entry.Value, path + "{" + Convert.ToString(entry.Key, CultureInfo.InvariantCulture) + "}");
|
2026-09-29 12:31:12 +02:00
|
|
|
}
|
|
|
|
|
else if (value is IEnumerable list)
|
|
|
|
|
Elements(list, path);
|
|
|
|
|
else if (IsObjectType(type))
|
|
|
|
|
{
|
|
|
|
|
foreach (var (slot, get) in ShapeOf(type).Walk)
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
Visit(get(value), path + "." + slot.ToString(CultureInfo.InvariantCulture));
|
2026-09-29 12:31:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Elements(IEnumerable list, string path)
|
|
|
|
|
{
|
|
|
|
|
var items = list.Cast<object?>().ToArray();
|
|
|
|
|
var taken = new HashSet<string>(StringComparer.Ordinal);
|
2026-09-29 19:40:36 +02:00
|
|
|
var objects = new HashSet<object>(ReferenceComparer.Instance);
|
2026-09-29 12:31:12 +02:00
|
|
|
foreach (var item in items)
|
|
|
|
|
{
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
if (item == null || !IsObjectType(item.GetType()) || ShapeOf(item.GetType()) is not { GetId: { } getId } shape)
|
|
|
|
|
continue;
|
2026-09-29 19:40:36 +02:00
|
|
|
if (!objects.Add(item))
|
|
|
|
|
throw new CultElementIdException(
|
|
|
|
|
$"Record {rootPath}: the same {item.GetType().Name} object appears twice in the list at {path}; an element is one object with one id.",
|
|
|
|
|
rootPath, path);
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
var id = getId(item);
|
|
|
|
|
if (string.IsNullOrEmpty(id))
|
|
|
|
|
continue;
|
2026-09-29 21:54:01 +02:00
|
|
|
plan.HeldIds = true;
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
if (shape.Derive == null && !IsRandomId(id!))
|
|
|
|
|
throw new CultElementIdException(
|
|
|
|
|
$"Record {rootPath}: element id '{id}' in the list at {path} is not 12 lowercase hex characters.",
|
|
|
|
|
rootPath, path, id);
|
|
|
|
|
if (!taken.Add(id!))
|
|
|
|
|
throw Duplicate(id!, path);
|
2026-09-29 12:31:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var index = 0; index < items.Length; index++)
|
|
|
|
|
{
|
|
|
|
|
var item = items[index];
|
|
|
|
|
if (item == null)
|
|
|
|
|
continue;
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
var itemPath = path + "/" + index.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
if (IsObjectType(item.GetType()) && ShapeOf(item.GetType()) is { SetId: { } , GetId: { } get } itemShape &&
|
|
|
|
|
string.IsNullOrEmpty(get(item)))
|
2026-09-29 12:31:12 +02:00
|
|
|
{
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
string id;
|
|
|
|
|
if (itemShape.Derive != null)
|
2026-09-29 12:31:12 +02:00
|
|
|
{
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
id = itemShape.Derive(item) ?? string.Empty;
|
|
|
|
|
if (id.Length == 0)
|
|
|
|
|
throw new CultElementIdException(
|
|
|
|
|
$"Record {rootPath}: the element at {itemPath} derives its id from {itemShape.DerivedFrom}, which is null or empty.",
|
|
|
|
|
rootPath, itemPath, member: itemShape.DerivedFrom);
|
|
|
|
|
if (!taken.Add(id))
|
|
|
|
|
throw Duplicate(id, path);
|
2026-09-29 12:31:12 +02:00
|
|
|
}
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
else
|
|
|
|
|
id = Mint(itemPath, deterministic, rootPath, taken);
|
|
|
|
|
plan.Add(item, itemShape, get(item), id);
|
2026-09-29 12:31:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Visit(item, itemPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
|
|
|
|
|
CultElementIdException Duplicate(string id, string path) => new(
|
|
|
|
|
$"Record {rootPath}: element id '{id}' appears twice in the list at {path}; an element id is unique within its list.",
|
|
|
|
|
rootPath, path, id);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-29 19:40:36 +02:00
|
|
|
private sealed class ReferenceComparer : IEqualityComparer<object>
|
|
|
|
|
{
|
|
|
|
|
public static readonly ReferenceComparer Instance = new();
|
|
|
|
|
|
|
|
|
|
bool IEqualityComparer<object>.Equals(object? left, object? right) => ReferenceEquals(left, right);
|
|
|
|
|
|
|
|
|
|
int IEqualityComparer<object>.GetHashCode(object value) => System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(value);
|
|
|
|
|
}
|
|
|
|
|
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
private static bool IsRandomId(string id)
|
|
|
|
|
{
|
|
|
|
|
if (id.Length != IdBytes * 2)
|
|
|
|
|
return false;
|
|
|
|
|
foreach (var character in id)
|
|
|
|
|
{
|
|
|
|
|
if (!(character is >= '0' and <= '9' or >= 'a' and <= 'f'))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2026-09-29 12:31:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string Mint(string itemPath, bool deterministic, string rootPath, HashSet<string> taken)
|
|
|
|
|
{
|
|
|
|
|
for (var attempt = 0; ; attempt++)
|
|
|
|
|
{
|
|
|
|
|
var bytes = new byte[IdBytes];
|
|
|
|
|
if (deterministic)
|
|
|
|
|
{
|
|
|
|
|
using var sha = SHA256.Create();
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
var hash = sha.ComputeHash(Encoding.UTF8.GetBytes(rootPath + "|" + itemPath + "|" + attempt.ToString(CultureInfo.InvariantCulture)));
|
2026-09-29 12:31:12 +02:00
|
|
|
Array.Copy(hash, bytes, IdBytes);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
RandomBytes(bytes);
|
|
|
|
|
var id = Hex(bytes);
|
|
|
|
|
if (taken.Add(id))
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void RandomBytes(byte[] bytes)
|
|
|
|
|
{
|
|
|
|
|
using var random = RandomNumberGenerator.Create();
|
|
|
|
|
random.GetBytes(bytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string Hex(byte[] bytes)
|
|
|
|
|
{
|
|
|
|
|
var text = new StringBuilder(bytes.Length * 2);
|
|
|
|
|
foreach (var value in bytes)
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
text.Append(value.ToString("x2", CultureInfo.InvariantCulture));
|
2026-09-29 12:31:12 +02:00
|
|
|
return text.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- shapes ----
|
|
|
|
|
|
|
|
|
|
private static Shape ShapeOf(Type type) => Shapes.GetOrAdd(type, Build);
|
|
|
|
|
|
|
|
|
|
private static Shape Build(Type type)
|
|
|
|
|
{
|
|
|
|
|
var shape = new Shape();
|
|
|
|
|
var idMember = MembersOf(type).FirstOrDefault(member => member.IsDefined(typeof(CultElementIdAttribute), true));
|
|
|
|
|
if (idMember != null)
|
|
|
|
|
{
|
|
|
|
|
shape.GetId = idMember is FieldInfo field ? o => (string?)field.GetValue(o) : o => (string?)((PropertyInfo)idMember).GetValue(o);
|
|
|
|
|
shape.SetId = idMember is FieldInfo f ? (o, v) => f.SetValue(o, v) : (o, v) => ((PropertyInfo)idMember).SetValue(o, v);
|
2026-09-29 15:03:09 +02:00
|
|
|
if (idMember.GetCustomAttribute<CultElementIdAttribute>(true)!.DerivedFrom is { } source &&
|
|
|
|
|
MembersOf(type).FirstOrDefault(member => member.Name == source) is { } from)
|
CultCache: element id rules, atomic minting, minted override values, the v3 marker
The id rule is one plan, made before anything is written: Plan decides every fill and
refuses a duplicate, a random id that is not 12 lowercase hex characters, and a derived
id whose source is null or empty, naming the record and list (CultElementIdException).
A load refuses what a write refuses. Apply and Undo make minting part of the admission:
a refused or uncommitted write leaves the caller's documents as they were.
Override values mint inside Resolve, into the decoded copy, so the public
CultVariantOverride.Set, C1-era overrides and Override() all get ids on write and on load;
Override() no longer mints. Interface unions are judged on their concrete types, the same
rule registration and minting share, and a subtype reusing the id's key is refused at
registration. Dictionary keys format with the invariant culture.
Records minted at load are marked per entry (IdsInMemoryOnly) instead of listed by key, so
a deleted record leaves the mark with it, and a flush of the whole store clears it. A store
whose types reach an object list declares cultcache.store.v3 (directory: v5), which every
reader older than ids refuses; a marked file stays marked when a writer merges onto it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 18:27:16 +02:00
|
|
|
{
|
|
|
|
|
shape.DerivedFrom = source;
|
|
|
|
|
shape.Derive = o => Convert.ToString(from is FieldInfo ff ? ff.GetValue(o) : ((PropertyInfo)from).GetValue(o), CultureInfo.InvariantCulture);
|
|
|
|
|
}
|
2026-09-29 12:31:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shape.Walk = KeyedMembers(type)
|
|
|
|
|
.Where(entry => !IsLeaf(Nullable.GetUnderlyingType(entry.Type) ?? entry.Type))
|
|
|
|
|
.Select(entry => (Slot: KeyOf(entry.Member)!.Value, Get: entry.Member is FieldInfo fi
|
|
|
|
|
? (Func<object, object?>)(o => fi.GetValue(o))
|
|
|
|
|
: o => ((PropertyInfo)entry.Member).GetValue(o)))
|
|
|
|
|
.ToArray();
|
|
|
|
|
return shape;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<MemberInfo> MembersOf(Type type) =>
|
|
|
|
|
type.GetFields(BindingFlags.Instance | BindingFlags.Public).Cast<MemberInfo>()
|
|
|
|
|
.Concat(type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(property => property.GetIndexParameters().Length == 0 && property.GetMethod != null));
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<(MemberInfo Member, Type Type)> KeyedMembers(Type type) =>
|
|
|
|
|
MembersOf(type).Where(member => KeyOf(member) != null).Select(member => (member, TypeOf(member)));
|
|
|
|
|
|
|
|
|
|
private static int? KeyOf(MemberInfo member) => member.GetCustomAttribute<KeyAttribute>(true)?.IntKey;
|
|
|
|
|
|
|
|
|
|
private static Type TypeOf(MemberInfo member) => member is FieldInfo field ? field.FieldType : ((PropertyInfo)member).PropertyType;
|
|
|
|
|
|
|
|
|
|
// ---- type classification ----
|
|
|
|
|
|
|
|
|
|
// A value MessagePack writes whole and that cannot hold an element list. Structs are values: never elements, never walked.
|
|
|
|
|
private static bool IsLeaf(Type type)
|
|
|
|
|
{
|
|
|
|
|
if (type.IsPrimitive || type.IsEnum || type == typeof(string) || type.IsValueType)
|
|
|
|
|
return true;
|
|
|
|
|
return type.IsArray && IsLeaf(type.GetElementType()!);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// An object a store can hold an element list under, or hold as an element: a class MessagePack serializes by key.
|
|
|
|
|
private static bool IsObjectType(Type type) =>
|
|
|
|
|
(type.IsClass || type.IsInterface) && type != typeof(string) && type != typeof(object) &&
|
|
|
|
|
(type.IsDefined(typeof(MessagePackObjectAttribute), true) || type.IsDefined(typeof(UnionAttribute), true));
|
|
|
|
|
|
|
|
|
|
private static Type? ElementTypeOf(Type type)
|
|
|
|
|
{
|
|
|
|
|
if (type.IsArray)
|
|
|
|
|
return type.GetElementType();
|
|
|
|
|
foreach (var candidate in type.GetInterfaces().Append(type))
|
|
|
|
|
{
|
|
|
|
|
if (candidate.IsGenericType && candidate.GetGenericTypeDefinition() == typeof(IEnumerable<>))
|
|
|
|
|
return candidate.GetGenericArguments()[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Type? DictionaryValueType(Type type)
|
|
|
|
|
{
|
|
|
|
|
foreach (var candidate in type.GetInterfaces().Append(type))
|
|
|
|
|
{
|
|
|
|
|
if (candidate.IsGenericType && candidate.GetGenericTypeDefinition() == typeof(IDictionary<,>))
|
|
|
|
|
return candidate.GetGenericArguments()[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|