CultNet selection cut1, commit 2 fix batch: door, shard-via-evaluator, Mesh v0 refusal, dead Reconcile code, mutation harness crash handling
Soul found commit 2 didn't pass (docs/cultnet-selection-cut.md, 2026-09-22):
empty/blank schemas-keys still reached the evaluator, the shard snapshot ran
its own loop instead of the evaluator, Mesh's v0 wire silently dropped v1-only
selection terms, the Mesh one-pass read could return the wrong schema's
record at a shared key, two decode rules for a foreign schema id disagreed,
dead identity-comparison code from the _projectRecord removal was still live,
and a disposed-socket race could abort a mutation run instead of recording
NO VERDICT. This is Self's fix batch for all of it.
- CultNetSelection.Validate now refuses a blank/whitespace entry inside
schemas or keys, not just an empty array. CultNetSelectionEvaluator no
longer treats [] as "no filter" (the door already refuses it before
evaluation). CultNetV0SelectionLowering is the one place that lowers an
empty or all-blank v0 list to null; CreateRawSnapshotResponse and
CreateShardSnapshotResponse both route through it, and CultMesh's own
Clean() now delegates to it instead of duplicating the rule.
- CreateShardSnapshotResponse no longer carries a private loop or its own
schema/key matching: it builds a CultNetSelection and answers through
CultNetDocumentRegistry.SelectPage (the same evaluator path as the
non-shard snapshot), with the shard's own membership check as a row
filter. Binding-alias expansion now runs on both paths identically.
- ResolveDefaultSelection decides "no schema filter" from the cleaned
recordKeys, not the raw array length. OverlaySelection stops deciding what
a missing override means: any explicit schemaIds/recordKeys override
replaces the default selection wholesale; no override keeps the default
untouched.
- CultMeshSnapshotRequestOptions.Selection is now checked by
EnsureV0Compatible before every send: fields, cites, cited, limit, cursor,
descending, and any non-default projection are refused typed
(CultNetSelectionInvalidException), never silently dropped from the v0
wire message. Mesh sending v1 is FU-Mesh-v1, not this cut.
- CultMeshHotBodySubscription.Selection returns a defensive copy on every
read so its arrays can't be mutated out from under the stored selection.
- ReadDocumentFromSnapshotResponse now searches candidates at one recordKey
in one order - exact schema, then alias, then a foreign schema id decoded
from the payload - instead of one OR'd condition where a case-insensitive
or structurally-lucky candidate could win first. The foreign-id decode
reads the payload's embedded schema version with the registry's shared
CultNetDocumentRegistry.TryReadSchemaVersion and alias-matches it against
the caller's own descriptor directly, rather than resolving to "whichever
registered type owns this schema string" - that resolution is ambiguous
when two document types alias the same schema id (MeshNoteDocument and
MeshNoteAliasDocument in the existing fixtures), which is what broke on
the first pass; TryResolveDescriptorByPayloadSchema stays for its existing
caller (ResolveDescriptorForRawDocument) with a note on when not to use it.
DecodeSnapshotDocuments and the Mesh copy of the payload-schema decode
(RawSnapshotPayloadMatchesSchema/TryReadSchemaVersion) are deleted in
favour of the shared one.
- CultNetDatabaseSubscriptionServer: the dead RecordKey/SchemaId identity
comparisons in Reconcile and ApplyProjectedChange, and the duplicate-key
throw in CreateProjectedSnapshot, are deleted - they can no longer fire
now that a projection's dictionary key is always the record's own key.
A new test flips authorizeRecord per key while the whole request stays
authorized (so DemandActive never toggles) and proves both halves of the
remaining diff: the removal of a now-unauthorized key and the add loop
picking up a newly-authorized one.
- Every NetworkingTests.cs test that starts an IsBackground poll thread now
joins it before its server disposes (about 20 tests; one already did).
- scripts/mutate-dotnet.ps1: a dotnet-test-host crash writing to stderr no
longer throws the whole run away under PowerShell 5.1's
$ErrorActionPreference = 'Stop' (2>&1 on a native command wraps stderr in
a terminating NativeCommandError); Run-Tests catches it and the entry loop
records NO VERDICT instead of aborting every remaining entry.
- Two probe tests: CultNetDatabase_ShardAndNonShardSnapshot_Agree{On
UnfilteredRequest,OnBindingSchemaIdOverride} prove the shard and non-shard
snapshot paths agree, including shard-membership exclusion of a document
outside the shard's key prefix and a binding schema-id override matching
on both paths.
- Mutation entries updated/added to match: the stale NET-D4-FourthMatcher
entry (its anchor was the private loop this batch deleted, not refactored)
is replaced by NET-D4-ShardMembership-Revert; the Mesh entries file's
header miscount ("two of three" loosening entries when there are only
two) is corrected and both previously-surviving loosening mutants
(MESH-DEFAULT-Loosening, MESH-KEY-Loosening) now have killing fixtures;
scripts/mutate-cultnet-selection-fixbatch2.entries.ps1 is new, covering
Reconcile's remaining two mutants.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
b090ca97f9
commit
568e8e356f
14 changed files with 586 additions and 174 deletions
|
|
@ -628,7 +628,10 @@ namespace GameCult.Networking
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a raw document snapshot bounded to one shard.
|
||||
/// Creates a raw document snapshot bounded to one shard. Answers through the one evaluator
|
||||
/// (<see cref="CultNetSelectionEvaluator"/>, via <see cref="CultNetDocumentRegistry.SelectPage"/>),
|
||||
/// binding-alias expansion included, with the shard membership check as the row filter -
|
||||
/// no loop of its own (docs/cultnet-selection-cut.md, Self's rulings 2026-09-22, S2-3).
|
||||
/// </summary>
|
||||
public CultNetSnapshotResponseRawMessage CreateShardSnapshotResponse(
|
||||
CultNetShardDescriptor shard,
|
||||
|
|
@ -638,26 +641,25 @@ namespace GameCult.Networking
|
|||
ThrowIfDisposed();
|
||||
if (shard == null) throw new ArgumentNullException(nameof(shard));
|
||||
|
||||
var requestedSchemaIds = filter?.SchemaIds;
|
||||
var requestedRecordKeys = filter?.RecordKeys != null
|
||||
? new HashSet<string>(filter.RecordKeys, StringComparer.Ordinal)
|
||||
: null;
|
||||
var documents = new List<CultNetRawDocumentRecord>();
|
||||
foreach (var document in _cache.AllEntries)
|
||||
var selection = new CultNetSelection
|
||||
{
|
||||
var documentType = document.GetType();
|
||||
var descriptor = _cache.Registry.GetRequired(documentType);
|
||||
var key = GetTrackedKey(document, documentType);
|
||||
if (string.IsNullOrWhiteSpace(key.Value) ||
|
||||
!shard.Matches(descriptor.SchemaId, key) ||
|
||||
(requestedSchemaIds != null && !CultNetSchemaAliasMatching.MatchesAny(requestedSchemaIds, descriptor)) ||
|
||||
(requestedRecordKeys != null && !requestedRecordKeys.Contains(key.Value)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Schemas = CultNetV0SelectionLowering.Lower(filter?.SchemaIds),
|
||||
Keys = CultNetV0SelectionLowering.Lower(filter?.RecordKeys),
|
||||
Projection = CultNetSelectionProjections.Document,
|
||||
Limit = CultNetSelectionEvaluator.LimitMax
|
||||
};
|
||||
bool RowFilter(CultDocumentDescriptor descriptor, CultRecordKey key) =>
|
||||
!string.IsNullOrWhiteSpace(key.Value) && shard.Matches(descriptor.SchemaId, key);
|
||||
|
||||
documents.Add(CreateRawDocumentRecord(key, document));
|
||||
}
|
||||
var documents = new List<CultNetRawDocumentRecord>();
|
||||
string? cursor = null;
|
||||
do
|
||||
{
|
||||
selection.Cursor = cursor;
|
||||
var page = _documents.SelectPage(_cache, selection, ordinalOf: static (_, _) => 0, asOf: 0, validate: false, options: null, rowFilter: RowFilter);
|
||||
documents.AddRange(page.Documents ?? Array.Empty<CultNetRawDocumentRecord>());
|
||||
cursor = page.Next;
|
||||
} while (cursor != null);
|
||||
|
||||
return new CultNetSnapshotResponseRawMessage
|
||||
{
|
||||
|
|
|
|||
|
|
@ -251,6 +251,9 @@ namespace GameCult.Networking
|
|||
? CreateProjectedSnapshot(request, key.Peer)
|
||||
: ProjectedSnapshot.Empty;
|
||||
|
||||
// With _projectRecord gone (docs/cultnet-selection-cut.md, S2-2), a projection's dictionary
|
||||
// key is always the record's own RecordKey/SchemaId, so the two can never disagree here;
|
||||
// the identity branch that used to catch a re-projected key is dead and deleted.
|
||||
foreach (var previous in projection.DeliveredBySourceRecordKey.ToArray())
|
||||
{
|
||||
if (!next.BySourceRecordKey.TryGetValue(previous.Key, out var current))
|
||||
|
|
@ -258,13 +261,7 @@ namespace GameCult.Networking
|
|||
SendRemoval(key.Peer, key.Id, previous.Value);
|
||||
continue;
|
||||
}
|
||||
if (!string.Equals(previous.Value.RecordKey, current.RecordKey, StringComparison.Ordinal) ||
|
||||
!string.Equals(previous.Value.SchemaId, current.SchemaId, StringComparison.Ordinal))
|
||||
{
|
||||
SendRemoval(key.Peer, key.Id, previous.Value);
|
||||
SendUpsert(key.Peer, key.Id, current, added: true);
|
||||
}
|
||||
else if (!Equivalent(previous.Value, current))
|
||||
if (!Equivalent(previous.Value, current))
|
||||
{
|
||||
SendUpsert(key.Peer, key.Id, current, added: false);
|
||||
}
|
||||
|
|
@ -299,16 +296,15 @@ namespace GameCult.Networking
|
|||
SchemaIds = request.SchemaIds,
|
||||
RecordKeys = request.RecordKeys
|
||||
});
|
||||
// The snapshot is one row per record key by construction, so the duplicate-key throw this
|
||||
// used to guard can no longer fire (docs/cultnet-selection-cut.md, S2-2); deleted rather
|
||||
// than tested around.
|
||||
var bySourceRecordKey = new Dictionary<string, CultNetRawDocumentRecord>(StringComparer.Ordinal);
|
||||
var matchedRecordKeys = new HashSet<string>(StringComparer.Ordinal);
|
||||
foreach (var source in snapshot.Documents)
|
||||
{
|
||||
var sourceRecordKey = source.RecordKey;
|
||||
if (_authorizeRecord?.Invoke(request, peer, sourceRecordKey, source.SchemaId) == false)
|
||||
continue;
|
||||
if (!matchedRecordKeys.Add(source.RecordKey))
|
||||
throw new InvalidOperationException(
|
||||
$"Database subscription snapshot produced duplicate record key '{source.RecordKey}'.");
|
||||
bySourceRecordKey[sourceRecordKey] = source;
|
||||
}
|
||||
return new ProjectedSnapshot(bySourceRecordKey);
|
||||
|
|
@ -337,14 +333,8 @@ namespace GameCult.Networking
|
|||
SendUpsert(peer, subscriptionId, current, added: true);
|
||||
return;
|
||||
}
|
||||
if (!string.Equals(previous.RecordKey, current.RecordKey, StringComparison.Ordinal) ||
|
||||
!string.Equals(previous.SchemaId, current.SchemaId, StringComparison.Ordinal))
|
||||
{
|
||||
SendRemoval(peer, subscriptionId, previous);
|
||||
projection.DeliveredBySourceRecordKey[sourceRecordKey] = current;
|
||||
SendUpsert(peer, subscriptionId, current, added: true);
|
||||
return;
|
||||
}
|
||||
// Same S2-2 fact as Reconcile: previous/current are looked up by the record's own key, so
|
||||
// they can never disagree in RecordKey/SchemaId here either.
|
||||
if (!Equivalent(previous, current))
|
||||
{
|
||||
projection.DeliveredBySourceRecordKey[sourceRecordKey] = current;
|
||||
|
|
|
|||
|
|
@ -285,8 +285,8 @@ namespace GameCult.Networking
|
|||
if (cache == null) throw new ArgumentNullException(nameof(cache));
|
||||
var selection = new CultNetSelection
|
||||
{
|
||||
Schemas = filter?.SchemaIds,
|
||||
Keys = filter?.RecordKeys,
|
||||
Schemas = CultNetV0SelectionLowering.Lower(filter?.SchemaIds),
|
||||
Keys = CultNetV0SelectionLowering.Lower(filter?.RecordKeys),
|
||||
Projection = CultNetSelectionProjections.Document,
|
||||
Limit = CultNetSelectionEvaluator.LimitMax
|
||||
};
|
||||
|
|
@ -350,15 +350,19 @@ namespace GameCult.Networking
|
|||
Func<string, CultRecordKey, long> ordinalOf,
|
||||
ulong asOf,
|
||||
bool validate,
|
||||
CultNetDocumentMessageOptions? options)
|
||||
CultNetDocumentMessageOptions? options,
|
||||
Func<CultDocumentDescriptor, CultRecordKey, bool>? rowFilter = null)
|
||||
{
|
||||
selection = ExpandSchemaBindingAliases(selection);
|
||||
if (validate)
|
||||
selection.Validate(_documents.AllDescriptors.ToArray());
|
||||
|
||||
var rows = cache.AllStoredDocuments
|
||||
.Select(stored => new CultNetSelectionEvaluator.Row(
|
||||
stored.Descriptor, stored.Key, stored.Document, ordinalOf(stored.Descriptor.SchemaId, stored.Key), stored.StoredAt))
|
||||
var stored = cache.AllStoredDocuments;
|
||||
if (rowFilter != null)
|
||||
stored = stored.Where(entry => rowFilter(entry.Descriptor, entry.Key));
|
||||
var rows = stored
|
||||
.Select(entry => new CultNetSelectionEvaluator.Row(
|
||||
entry.Descriptor, entry.Key, entry.Document, ordinalOf(entry.Descriptor.SchemaId, entry.Key), entry.StoredAt))
|
||||
.ToArray();
|
||||
var evaluation = CultNetSelectionEvaluator.Select(_documents, rows, selection, asOf);
|
||||
var records = evaluation.Rows.Select(row => ToRawRecord(row, options)).ToArray();
|
||||
|
|
@ -593,7 +597,19 @@ namespace GameCult.Networking
|
|||
string.Equals(candidate.SchemaName, schemaName, StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
private CultDocumentDescriptor? TryResolveDescriptorByPayloadSchema(byte[] payload)
|
||||
/// <summary>
|
||||
/// Resolves the descriptor this registry's own bindings (or every registered descriptor, when
|
||||
/// none are bound) associate with a raw payload's embedded schema version, when exactly one
|
||||
/// registered type owns that schema string. Used by <see cref="ResolveDescriptorForRawDocument"/>
|
||||
/// to recover a document's type from a foreign/runtime-generated schema id. Ambiguous - and
|
||||
/// so not the right tool - when the caller already knows the target descriptor and two
|
||||
/// registered types alias the same schema id: match <see cref="TryReadSchemaVersion"/>'s
|
||||
/// result against that descriptor with <see cref="CultNetSchemaAliasMatching"/> directly
|
||||
/// instead (docs/cultnet-selection-cut.md, section 4/S2-5; this is what CultMesh's
|
||||
/// foreign-schema decode does). Trusts the payload's own schema stamp; it does not re-run a
|
||||
/// selection or re-check authorization.
|
||||
/// </summary>
|
||||
public CultDocumentDescriptor? TryResolveDescriptorByPayloadSchema(byte[] payload)
|
||||
{
|
||||
var schemaVersion = TryReadSchemaVersion(payload);
|
||||
if (string.IsNullOrWhiteSpace(schemaVersion))
|
||||
|
|
@ -624,7 +640,16 @@ namespace GameCult.Networking
|
|||
.Select(group => group.First());
|
||||
}
|
||||
|
||||
private static string? TryReadSchemaVersion(byte[] payload)
|
||||
/// <summary>
|
||||
/// Reads a raw payload's embedded <c>schemaVersion</c> stamp, when it carries one - the parse
|
||||
/// step of the registry's payload-schema decode rule, shared with
|
||||
/// <see cref="TryResolveDescriptorByPayloadSchema"/> and every "foreign schema id" fallback in
|
||||
/// CultNet and CultMesh (docs/cultnet-selection-cut.md, section 4/S2-5). A caller matching
|
||||
/// against one already-known descriptor (rather than resolving "whichever registered type owns
|
||||
/// this schema string", which is ambiguous when two document types alias the same schema id)
|
||||
/// should alias-match this string with <see cref="CultNetSchemaAliasMatching"/> directly.
|
||||
/// </summary>
|
||||
public static string? TryReadSchemaVersion(byte[] payload)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
|
|||
|
|
@ -195,6 +195,31 @@ namespace GameCult.Networking
|
|||
public bool HasHop => Cites != null || Cited != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lowers a v0 <c>schemaIds</c>/<c>recordKeys</c> filter into cultnet.selection.v1's null-means-
|
||||
/// every vocabulary (docs/cultnet-selection-cut.md, Self's rulings 2026-09-22): v0's old cleaning
|
||||
/// carried over as the one place that decides it. An absent list, an empty one, or one made only
|
||||
/// of blank entries all lower to null; a mixed list keeps its non-blank entries, deduplicated.
|
||||
/// This is a v0-compatibility rule, not a meaning <see cref="CultNetSelectionEvaluator"/> or
|
||||
/// <see cref="CultNetSelectionValidation"/> carries - v1 selections are refused at the door
|
||||
/// instead (S1).
|
||||
/// </summary>
|
||||
public static class CultNetV0SelectionLowering
|
||||
{
|
||||
/// <summary>Lowers one v0 filter list; see the type summary.</summary>
|
||||
public static string[]? Lower(IReadOnlyList<string>? values)
|
||||
{
|
||||
if (values is not { Count: > 0 })
|
||||
return null;
|
||||
|
||||
var filtered = values
|
||||
.Where(value => !string.IsNullOrWhiteSpace(value))
|
||||
.Distinct(StringComparer.Ordinal)
|
||||
.ToArray();
|
||||
return filtered.Length == 0 ? null : filtered;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The two wire values <see cref="CultNetSelection.Projection"/> may carry.</summary>
|
||||
public static class CultNetSelectionProjections
|
||||
{
|
||||
|
|
@ -302,6 +327,22 @@ namespace GameCult.Networking
|
|||
throw new CultNetSelectionInvalidException("schemas", null, "selection.schemas is present and empty; omit it to reach every schema.");
|
||||
if (selection.Keys != null && selection.Keys.Length == 0)
|
||||
throw new CultNetSelectionInvalidException("keys", null, "selection.keys is present and empty; omit it to reach every key.");
|
||||
if (selection.Schemas != null)
|
||||
{
|
||||
foreach (var schema in selection.Schemas)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(schema))
|
||||
throw new CultNetSelectionInvalidException("schemas", schema, "selection.schemas carries an empty or whitespace entry.");
|
||||
}
|
||||
}
|
||||
if (selection.Keys != null)
|
||||
{
|
||||
foreach (var key in selection.Keys)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
throw new CultNetSelectionInvalidException("keys", key, "selection.keys carries an empty or whitespace entry.");
|
||||
}
|
||||
}
|
||||
if (selection.Projection is not (CultNetSelectionProjections.Header or CultNetSelectionProjections.Document))
|
||||
throw new CultNetSelectionInvalidException("projection", selection.Projection, $"selection.projection \"{selection.Projection}\" is neither \"header\" nor \"document\".");
|
||||
|
||||
|
|
|
|||
|
|
@ -79,9 +79,13 @@ namespace GameCult.Networking
|
|||
|
||||
private static bool MatchesSchemaKeysFields(CultDocumentDescriptor descriptor, CultRecordKey key, object? document, CultNetSelection selection)
|
||||
{
|
||||
if (selection.Schemas is { Length: > 0 } && !CultNetSchemaAliasMatching.MatchesAny(selection.Schemas, descriptor))
|
||||
// The door (CultNetSelectionValidation.Validate) refuses an empty schemas/keys list before
|
||||
// a selection ever reaches evaluation, so a non-null array here is always non-empty; the
|
||||
// evaluator does not special-case [] as "no filter" (docs/cultnet-selection-cut.md,
|
||||
// Self's rulings 2026-09-22, S2-3).
|
||||
if (selection.Schemas != null && !CultNetSchemaAliasMatching.MatchesAny(selection.Schemas, descriptor))
|
||||
return false;
|
||||
if (selection.Keys is { Length: > 0 } && !selection.Keys.Contains(key.Value, StringComparer.Ordinal))
|
||||
if (selection.Keys != null && !selection.Keys.Contains(key.Value, StringComparer.Ordinal))
|
||||
return false;
|
||||
if (selection.Fields == null)
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Reference in a new issue