fix(grpc-data): H4 review 1 — count every projected history answer where its status is set

The consumer reads counter was fed from the gateway's outcomes, so limiter refusals, a spent
deadline, a count mismatch and refusals before the gateway were never counted, and a serve-time
fault after the gateway returned counted Good. The node manager now reports each projected node
once with the status the client sees (IProjectedHistoryAccess.RecordAnswered); a fault that
escapes to the SDK counts every projected node of the request Bad. Variant gains 'modified'.
This commit is contained in:
Joseph Doherty
2026-11-24 08:35:55 -05:00
parent ce648523ab
commit 2f11a9c609
6 changed files with 186 additions and 21 deletions
@@ -214,6 +214,76 @@ public sealed class ProjectedHistoryReadTests : IAsyncDisposable
access.Issued[0].Released.ShouldBe(0, "the store owns the saved cursor");
}
[Fact]
[Trait("Phase", "H4")]
public async Task Every_projected_answer_is_recorded_once_with_the_status_the_client_sees()
{
// H4 review item 1: counted where the status is set — limiter refusals, a spent deadline, a count mismatch and a
// serve-time fault included — never from the gateway's outcomes alone.
var (nm, f) = await BootAsync(aclEnabled: false, NodePermissions.Browse | NodePermissions.Read | NodePermissions.HistoryRead);
nm.HistoryContinuationStore = new InMemoryHistoryContinuationStore();
var a = Project(nm, f, "Speed", historizing: true);
var b = Project(nm, f, "Flow", historizing: true);
var access = new FakeAccess();
nm.ProjectedHistoryAccess = access;
(string Variant, uint[] Statuses) Delta(Action act)
{
var before = access.Answered.Count;
act();
var added = access.Answered.Skip(before).ToArray();
added.Select(x => x.Variant).Distinct().Count().ShouldBe(1);
return (added[0].Variant, added.Select(x => x.Status).ToArray());
}
// Served: Good, once per node.
var served = Delta(() => Raw(nm, f.Identity, [a, b]));
served.Variant.ShouldBe("raw");
served.Statuses.ShouldBe([StatusCodes.Good.Code, StatusCodes.Good.Code]);
// Refused unsent by the consumer: an unknown aggregate, a modified read.
var processed = new ReadProcessedDetails { StartTime = T0, EndTime = T0.AddHours(1), ProcessingInterval = 60_000,
AggregateType = [new NodeId("not-an-aggregate", 2)] };
var unknown = Delta(() => Read(nm, f.Identity, processed, [a]));
unknown.Variant.ShouldBe("processed");
unknown.Statuses.ShouldBe([StatusCodes.BadAggregateNotSupported.Code]);
var modified = new ReadRawModifiedDetails { StartTime = T0, EndTime = T0.AddHours(1), NumValuesPerNode = 5, IsReadModified = true };
var refused = Delta(() => Read(nm, f.Identity, modified, [a]));
refused.Variant.ShouldBe("modified");
refused.Statuses.ShouldBe([StatusCodes.BadHistoryOperationUnsupported.Code]);
// The non-local (projected) slot limiter saturated.
var nonLocal = nm.GetNonLocalHistoryLimiter();
var taken = 0;
while (nonLocal.Wait(0, TestContext.Current.CancellationToken)) taken++;
try { Delta(() => Raw(nm, f.Identity, [a, b])).Statuses.ShouldBe([StatusCodes.BadTooManyOperations.Code, StatusCodes.BadTooManyOperations.Code]); }
finally { nonLocal.Release(taken); }
// The shared batch limiter saturated (its admission wait is capped by the request deadline).
var batch = nm.GetHistoryReadBatchLimiter();
nm.HistoryReadDeadline = TimeSpan.FromMilliseconds(100);
taken = 0;
while (batch.Wait(0, TestContext.Current.CancellationToken)) taken++;
try { Delta(() => Raw(nm, f.Identity, [a])).Statuses.ShouldBe([StatusCodes.BadTooManyOperations.Code]); }
finally { batch.Release(taken); nm.HistoryReadDeadline = TimeSpan.FromSeconds(60); }
// The request deadline spent before the remote call.
nm.HistoryReadDeadline = TimeSpan.FromTicks(1);
try { Delta(() => Raw(nm, f.Identity, [a])).Statuses.ShouldBe([StatusCodes.BadTimeout.Code]); }
finally { nm.HistoryReadDeadline = TimeSpan.FromSeconds(60); }
// The gateway answered the wrong number of outcomes.
access.DropLast = true;
Delta(() => Raw(nm, f.Identity, [a, b])).Statuses.ShouldBe([StatusCodes.BadInternalError.Code, StatusCodes.BadInternalError.Code]);
access.DropLast = false;
// A serve-time fault after the gateway returned: the service fails, so every projected node counts Bad — never Good.
var poisoned = new FakeAccess { FirstValue = new object() };
nm.ProjectedHistoryAccess = poisoned;
Should.Throw<ArgumentException>(() => Raw(nm, f.Identity, [a, b]));
poisoned.Answered.Select(x => x.Status).ShouldBe([StatusCodes.BadInternalError.Code, StatusCodes.BadInternalError.Code]);
}
[Fact]
public void Unset_bounds_travel_absent_and_AtTime_carries_its_times()
{
@@ -470,6 +540,15 @@ public sealed class ProjectedHistoryReadTests : IAsyncDisposable
/// <summary>When set, every call throws it (after claiming nothing — the cursors are the caller's to release).</summary>
public Exception? Throw { get; set; }
public int Entries;
/// <summary>When set, the answer omits the last node (an outcome count mismatch).</summary>
public bool DropLast { get; set; }
/// <summary>H4: what the node manager reported as answered, in order.</summary>
public readonly List<(string Variant, NodeId Node, uint Status)> Answered = [];
public void RecordAnswered(string variant, IReadOnlyList<(NodeId NodeId, uint StatusCode)> answers)
{
lock (Answered) foreach (var (node, status) in answers) Answered.Add((variant, node, status));
}
public async Task<IReadOnlyList<ProjectedHistoryOutcome>> ReadAsync(RoleCarryingUserIdentity caller, DataHistoryDetails details,
IReadOnlyList<ProjectedHistoryNode> nodes, TimeSpan deadline, CancellationToken cancellationToken)
@@ -479,7 +558,7 @@ public sealed class ProjectedHistoryReadTests : IAsyncDisposable
if (Interlocked.Increment(ref Entries) > 0) Entered.TrySetResult();
if (Hold is { } hold) await hold.Task.ConfigureAwait(false);
if (Throw is { } error) throw error;
return (nodes.Select(node =>
return (nodes.Take(DropLast ? nodes.Count - 1 : nodes.Count).Select(node =>
{
FakeCursor? next = null;
if (Paged)