fix(alarms): #937 review r1 — revert a refused scripted method, broadcast to the peer only after the local verdict, settle a batch per request, in-flight cap

1. A post-dispatch refusal now puts the node back to its captured pre-call state
   with one corrective event (skipped when the node changed since) — the SDK had
   already applied and reported the change. Blank scripted AddComment is refused
   in the handler before the SDK runs.
2. ScriptedAlarmCommandDispatch: ask the local engine first, broadcast to the peer
   only on Applied/OutcomeUnknown — a refusal now reached no engine anywhere.
3. ApplyAsync no longer checks 'loaded' before _evalGate: a command racing the
   first load queues behind it; never loaded => refusal (nothing applied).
4. One Call's requests are settled sequentially (no request runs against a node
   state the engine refused); <= 64 asks in flight node-wide, else
   BadTooManyOperations before anything applies; per-request bound starts at send.
   Measured 300 acks through one host + LocalDb: 59 ms sequential, max 24 ms/ask
   with 64 outstanding.
5. Mixed-batch wire test (scripted refused + native + imported + scripted applied
   + core-manager method): per-item status in order, one scripted ask at a time.
L6: readiness from the engine's HasCompleteGeneration on a load fault.
L7: the verdict slot is closed when its SDK call returns (timer-captured
AsyncLocal can no longer park an ask). L10: remote budget never stretched.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-11-28 11:51:28 -05:00
parent 66b75aed0f
commit 6004a5a565
14 changed files with 670 additions and 125 deletions
@@ -384,6 +384,35 @@ public sealed class ScriptedAlarmEngineReloadRestatementTests
rig.Events.Where(e => e.AlarmId == "HighTemp").Select(e => e.Emission).ShouldBe([EmissionKind.Acknowledged]);
}
/// <summary>
/// #937 review r1: a command racing the engine's FIRST load queues behind it and applies (it used to throw "not loaded"
/// before the gate — an InvalidOperationException the host could only classify as OutcomeUnknown). A command on an
/// engine that never loaded is a definite refusal.
/// </summary>
[Fact]
public async Task A_command_racing_the_first_load_applies_after_it()
{
using var rig = new Rig();
(await Should.ThrowAsync<ScriptedAlarmCommandRejectedException>(rig.Engine.AcknowledgeAsync("HighTemp", "alice", null, Ct)))
.AlarmId.ShouldBe("HighTemp");
rig.Up.Set("Temp", 150);
using var loadReached = new ManualResetEventSlim();
using var release = new ManualResetEventSlim();
rig.Up.OnRead = _ => { loadReached.Set(); release.Wait(TimeSpan.FromSeconds(30)); };
var load = Task.Run(() => rig.Engine.LoadAsync([Alarm()], Ct), Ct);
loadReached.Wait(TimeSpan.FromSeconds(10), Ct).ShouldBeTrue();
rig.Up.OnRead = null;
var ack = rig.Engine.AcknowledgeAsync("HighTemp", "alice", "seen", Ct);
await Task.Delay(100, Ct);
ack.IsCompleted.ShouldBeFalse("the ack waits for the first load's gate");
release.Set();
await load;
await ack;
rig.Engine.GetState("HighTemp")!.Acked.ShouldBe(AlarmAckedState.Acknowledged);
}
/// <summary>#935 review S1: a command that changes nothing (an uncommented re-ack) writes nothing.</summary>
[Fact]
public async Task An_idempotent_command_does_not_write_the_store()