CultNet selection cut1, commit 1: the shape, the evaluator, v1 messages
docs/cultnet-selection-cut.md section 2/4/6/7/8: CultNetSelection (schemas, keys, fields, cites, cited, projection, descending, limit, cursor) with its door validation (S1), and CultNetSelectionEvaluator: the single-row Matches fast path (D6) and the full Select (order, hop, cursor, paging). Neither reflects over a document; both read only through the cache's public surface (commit 0). Three v1 messages beside v0 (cultnet.snapshot_request.v1, cultnet.database_subscribe.v1, cultnet.snapshot_response_raw.v1), their schema files under contracts/cultnet, registry entries and dispatch arms. CultNetSchemaAliasMatching goes internal -> public (D4), the one alias matcher. CultNetDatabase gains LastWriteSequence(schemaId, key), confirmed at the one append site (AppendMutationLogEntry) that every committed write takes a shard-log sequence, including a local PutAsync outside any shard. Three engines deleted, one evaluator wired in their place: - CultNetDocumentRegistry: CreateRawSnapshotResponse's reflection loop over cache.AllEntries and MatchesRequestedSchema gone; cache.AllStoredDocuments replaces the reflection entirely. v0 lowers into a Selection and walks the evaluator's own pages so a v0 caller still sees its whole matching set (D3: v0 has no engine of its own). CreateSelectionResponse/SelectPage/ ToRawRecord/CreateSnapshotRequestV1 added. - CultNetDatabaseServer: CreateChangeMessage/CreateRawDocumentRecord/Matches/ MatchesSchema/InferSchemaName (122 lines) deleted; CreateSubscription lowers v0 into a Selection and matches through the evaluator. - CultNetDatabaseSubscriptionServer: CreateAuthorizedChange/CreateChange/ CreateChangeCore/ResolveWireSchemaId/MatchesRequestedSchema/CreateRawRecord (92 lines) deleted, replaced by CreateMatchedRecord. _projectRecord and CreateProjectedSnapshot's collapse are commit 2's, per the map's own deletion line, and are untouched here. Discrepancy found and fixed (not in the map): CultNetDocumentBinding can carry a schema id different from its descriptor's own (CultNetDocumentBinding.ForDocument's schemaId override), which the old per-engine matchers checked and a descriptor-only evaluator cannot see by design (it stays decoupled from CultNetDocumentRegistry). Fixed at the two call sites and in CultNetDocumentRegistry.SelectPage by resolving the binding's schema id into the effective selection before evaluation (CultNetSchemaAliasMatching.WithBindingSchemaAlias, CultNetDocumentRegistry.ExpandSchemaBindingAliases), not in the evaluator. Found by DatabaseSubscription_FiltersLiveChangesByWireSchemaBinding timing out; both call sites now pass. Second discrepancy, reported not fixed: CultNetDatabase.cs:1707 has a fourth, undocumented copy of schema-matching logic (MatchesRequestedSchema, private, serving CreateShardSnapshotResponse), which the map's section 1 audit did not name among "three separate selector engines". Left untouched this commit: it is out of the map's explicit deletes list and touching it risks shard snapshot behavior with no budgeted verification. Verification: dotnet test tests/GameCult.Networking.Tests - 162 passed (150 existing + 12 new), 0 failed; the seven map-named tests all still pass, two rewritten for the new signatures (CreateChangeMessage now takes CultNetSelection; CreateChange/CreateAuthorizedChange collapsed into CreateMatchedRecord, returning CultNetRawDocumentRecord? directly). tests/GameCult.Mesh.Tests - 252 passed, 1 pre-existing skip, 0 failed (no Mesh source touched this commit). tests/GameCult.Caching.Tests - unaffected (188, unchanged from commit 0). Negative greps: GetProperty|GetField|GetCustomAttribute in GameCult.Networking - only against the generic `object change` dispatch (CultNetDatabaseChange<T>) and CultRecordHandle<T> construction, pre-existing and out of this cut's scope, never against a document's declared values. _projectRecord survives in CultNetDatabaseSubscriptionServer.cs (expected, commit 2). CultNetSchemaAliasMatching is the only schema matcher outside the one reported discrepancy. scripts/mutate-dotnet.ps1: the caching mutation runner generalized into a shared dotnet/NUnit harness (Target/TestProject/Entries/ControlFilter), replacing scripts/mutate-cultnet-selection-caching.ps1 with an entries file of the same six mutations (re-verified: all six still KILL). It is now generic to any NUnit project in this repo, not only this campaign's two targets - a candidate to sit beside tools/eureka-mutations.ps1 as the dotnet-side counterpart. Nine new networking entries (S3, S5 x2, S7 x2, S16 x2, S18) all KILLED. Not yet mutation-run: S1, S2, S6, S23 (pinned by tests, not by a committed mutant); S9-S15 (subscription reconcile lowering, v0 no-engine-of-its-own); S17, S20 (pinned by tests only). S19/S21/S22 are cache-layer, covered in commit 0. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
e420410fe9
commit
17d10e017f
19 changed files with 1976 additions and 477 deletions
|
|
@ -234,8 +234,8 @@ namespace GameCult.Networking
|
|||
}
|
||||
|
||||
var sourceRecordKey = ResolveChangeRecordKey(change);
|
||||
var message = CreateAuthorizedChange(change, request, subscriptionId, peer);
|
||||
var projected = message?.Document;
|
||||
var matched = CreateMatchedRecord(change, request, peer);
|
||||
var projected = matched;
|
||||
if (projected != null && _projectRecord != null)
|
||||
projected = _projectRecord(request, peer, projected);
|
||||
ApplyProjectedChange(
|
||||
|
|
@ -421,97 +421,40 @@ namespace GameCult.Networking
|
|||
_requests.TryRemove(key, out _);
|
||||
}
|
||||
|
||||
private CultNetDatabaseChangeRawMessage? CreateAuthorizedChange(
|
||||
// One evaluator (docs/cultnet-selection-cut.md, D3/D4): a v0 subscribe request lowers into a
|
||||
// Selection with no engine of its own, matched and projected exactly as a v1 request would be.
|
||||
// Record-level authorization (_authorizeRecord) is not selection and stays layered on top.
|
||||
private CultNetRawDocumentRecord? CreateMatchedRecord(
|
||||
object change,
|
||||
CultNetDatabaseSubscribeMessage request,
|
||||
string subscriptionId,
|
||||
ICultNetSchemaServerPeer peer)
|
||||
{
|
||||
return CreateChangeCore(change, request, subscriptionId, (recordKey, schemaId) =>
|
||||
_authorizeRecord?.Invoke(request, peer, recordKey, schemaId) != false);
|
||||
}
|
||||
|
||||
private CultNetDatabaseChangeRawMessage? CreateChange(
|
||||
object change,
|
||||
CultNetDatabaseSubscribeMessage request,
|
||||
string subscriptionId)
|
||||
{
|
||||
return CreateChangeCore(change, request, subscriptionId, (_, _) => true);
|
||||
}
|
||||
|
||||
private CultNetDatabaseChangeRawMessage? CreateChangeCore(
|
||||
object change,
|
||||
CultNetDatabaseSubscribeMessage request,
|
||||
string subscriptionId,
|
||||
Func<string, string, bool> authorizeRecord)
|
||||
{
|
||||
var changeType = change.GetType();
|
||||
var key = (CultRecordKey)(changeType.GetProperty("Key")?.GetValue(change) ?? new CultRecordKey(""));
|
||||
if (request.RecordKeys is { Length: > 0 } && !request.RecordKeys.Contains(key.Value, StringComparer.Ordinal))
|
||||
return null;
|
||||
|
||||
var kind = (CultNetDatabaseChangeKind)(changeType.GetProperty("Kind")?.GetValue(change) ?? CultNetDatabaseChangeKind.Updated);
|
||||
var document = changeType.GetProperty("Document")?.GetValue(change);
|
||||
if (kind == CultNetDatabaseChangeKind.Removed || document == null)
|
||||
{
|
||||
var previous = changeType.GetProperty("PreviousDocument")?.GetValue(change);
|
||||
var schemaId = ResolveWireSchemaId(previous, (string?)changeType.GetProperty("SchemaId")?.GetValue(change) ?? "");
|
||||
if (!MatchesRequestedSchema(request.SchemaIds, previous, schemaId))
|
||||
return null;
|
||||
if (!authorizeRecord(key.Value, schemaId))
|
||||
return null;
|
||||
return new CultNetDatabaseChangeRawMessage
|
||||
{
|
||||
MessageId = Guid.NewGuid().ToString("N"),
|
||||
SubscriptionId = subscriptionId,
|
||||
ChangeKind = "removed",
|
||||
RecordKey = key.Value,
|
||||
SchemaId = schemaId
|
||||
};
|
||||
}
|
||||
var forDescriptor = document ?? changeType.GetProperty("PreviousDocument")?.GetValue(change);
|
||||
if (forDescriptor == null)
|
||||
return null;
|
||||
|
||||
var raw = CreateRawRecord(key, document);
|
||||
if (!MatchesRequestedSchema(request.SchemaIds, document, raw.SchemaId))
|
||||
return null;
|
||||
if (!authorizeRecord(key.Value, raw.SchemaId))
|
||||
return null;
|
||||
return new CultNetDatabaseChangeRawMessage
|
||||
var descriptor = _database.Cache.Registry.GetRequired(forDescriptor.GetType());
|
||||
var selection = new CultNetSelection
|
||||
{
|
||||
MessageId = Guid.NewGuid().ToString("N"),
|
||||
SubscriptionId = subscriptionId,
|
||||
ChangeKind = kind == CultNetDatabaseChangeKind.Added ? "added" : "updated",
|
||||
Document = raw
|
||||
Schemas = request.SchemaIds,
|
||||
Keys = request.RecordKeys,
|
||||
Projection = CultNetSelectionProjections.Document
|
||||
};
|
||||
}
|
||||
var binding = _database.Documents.GetByDocumentType(descriptor.DocumentType);
|
||||
var effectiveSelection = CultNetSchemaAliasMatching.WithBindingSchemaAlias(selection, descriptor, binding?.SchemaId);
|
||||
if (!CultNetSelectionEvaluator.Matches(descriptor, key, document, effectiveSelection))
|
||||
return null;
|
||||
if (_authorizeRecord?.Invoke(request, peer, key.Value, descriptor.SchemaId) == false)
|
||||
return null;
|
||||
|
||||
private string ResolveWireSchemaId(object? document, string fallback)
|
||||
{
|
||||
if (document == null) return fallback;
|
||||
return _database.Documents.GetByDocumentType(document.GetType())?.SchemaId ?? fallback;
|
||||
}
|
||||
if (kind == CultNetDatabaseChangeKind.Removed || document == null)
|
||||
return null;
|
||||
|
||||
private bool MatchesRequestedSchema(string[]? requestedSchemaIds, object? document, string wireSchemaId)
|
||||
{
|
||||
if (requestedSchemaIds is not { Length: > 0 }) return true;
|
||||
if (requestedSchemaIds.Contains(wireSchemaId, StringComparer.Ordinal)) return true;
|
||||
return document != null && CultNetSchemaAliasMatching.MatchesAny(
|
||||
requestedSchemaIds,
|
||||
_database.Cache.Registry.GetRequired(document.GetType()));
|
||||
}
|
||||
|
||||
private CultNetRawDocumentRecord CreateRawRecord(CultRecordKey key, object document)
|
||||
{
|
||||
var method = typeof(CultNetDocumentRegistry)
|
||||
.GetMethods(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Single(candidate => candidate.Name == nameof(CultNetDocumentRegistry.CreateRawDocumentPutMessage) &&
|
||||
candidate.IsGenericMethodDefinition);
|
||||
var documentType = document.GetType();
|
||||
var handle = Activator.CreateInstance(
|
||||
typeof(CultRecordHandle<>).MakeGenericType(documentType),
|
||||
new object[] { key });
|
||||
var put = method.MakeGenericMethod(documentType)
|
||||
.Invoke(_database.Documents, new[] { Guid.NewGuid().ToString("N"), handle, document, null });
|
||||
return ((CultNetDocumentPutRawMessage)put!).Document;
|
||||
return _database.Documents.ToRawRecord(descriptor, key, document, DateTimeOffset.UtcNow.ToString("O"));
|
||||
}
|
||||
|
||||
private sealed class SubscriptionProjectionState
|
||||
|
|
|
|||
Loading…
Reference in a new issue