fix(alarms): #951 #952 #953 — late refusal reverts, never-started command is NotApplied, superseded load failure ignored

#951: the hosted-service Ask lives LateVerdictWindow (30 s) past the caller's bound
(ExecuteAlarmCommand deadline unchanged); the OutcomeUnknown it returns carries the
still-running ask as AlarmCommandOutcome.LateOutcome (process-local). The node manager
watches it (or the asker task itself when its backstop expired, following unknown
chains) and reverts the node on a late NotApplied, under the existing per-condition
mutation-counter guard. Both CallAsync and remote ExecuteConditionMethodAsync paths.

#952: ScriptedAlarmEngine throws ScriptedAlarmCommandNotStartedException (an OCE) when a
command is cancelled before it acquires _evalGate; a cancellation inside SaveAsync stays
a plain OCE (unknown). The host maps not-started to NotApplied and remembers it, except on
the direct ask once its asker's deadline passed (asker already answered unknown and
broadcast): then unknown + not remembered, so the broadcast copy decides (as review r2 M2).

#953: a faulted load pipes back AlarmsLoadFailed(cause, generation); a failure from a
generation a newer apply superseded publishes no readiness (it would refuse every command
while the newer load is in flight).

Red/green: each fix reverted individually turns its tests red (Core 1, Runtime 3,
Host 1, OpcUaServer.IntegrationTests 4).

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-12-01 19:42:24 -05:00
parent 6b60845931
commit 068124d1ae
14 changed files with 596 additions and 33 deletions
@@ -50,11 +50,14 @@ public sealed class ScriptedAlarmEngineReloadRestatementTests
public Task<IReadOnlyList<AlarmConditionState>> LoadAllAsync(CancellationToken ct)
=> Task.FromResult<IReadOnlyList<AlarmConditionState>>(_rows.Values.ToArray());
public Task SaveAsync(AlarmConditionState state, CancellationToken ct)
/// <summary>Optional hook run by <see cref="SaveAsync"/> before it writes — block it (#952).</summary>
public Func<AlarmConditionState, CancellationToken, Task>? OnSave { get; set; }
public async Task SaveAsync(AlarmConditionState state, CancellationToken ct)
{
if (OnSave is { } hook) await hook(state, ct);
Saves.Enqueue(state);
_rows[state.AlarmId] = state;
return Task.CompletedTask;
}
public Task RemoveAsync(string alarmId, CancellationToken ct)
@@ -413,6 +416,74 @@ public sealed class ScriptedAlarmEngineReloadRestatementTests
rig.Engine.GetState("HighTemp")!.Acked.ShouldBe(AlarmAckedState.Acknowledged);
}
/// <summary>
/// #952: a command cancelled while it still waits for the gate (here: behind a reload) never started — it throws
/// <see cref="ScriptedAlarmCommandNotStartedException"/> (still an <see cref="OperationCanceledException"/>) and
/// applied nothing, so its host can answer a definite "not applied".
/// </summary>
[Fact]
public async Task A_command_cancelled_before_it_gets_the_gate_is_reported_not_started()
{
using var rig = new Rig();
await ActiveAndAcked(rig);
rig.Store.Saves.Clear();
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;
using var cts = CancellationTokenSource.CreateLinkedTokenSource(Ct);
var comment = rig.Engine.AddCommentAsync("HighTemp", "alice", "queued", cts.Token);
comment.IsCompleted.ShouldBeFalse("the reload holds the gate");
cts.Cancel();
// Awaited directly, as the host does: a cancellation-derived exception leaves the task Canceled, and only a direct
// await rethrows the stored exception (a ContinueWith-based assertion would see a fresh TaskCanceledException).
var notStarted = (await CaughtAsync(comment)).ShouldBeOfType<ScriptedAlarmCommandNotStartedException>();
notStarted.AlarmId.ShouldBe("HighTemp");
notStarted.ShouldBeAssignableTo<OperationCanceledException>();
release.Set();
await load;
rig.Engine.GetState("HighTemp")!.Comments.Select(c => c.Text).ShouldNotContain("queued");
rig.Store.Saves.ShouldBeEmpty();
}
/// <summary>#952 counterpart: a cancellation once the command HAS the gate (inside the store save, which may already
/// have committed) stays a plain <see cref="OperationCanceledException"/> — its outcome is unknown, never "not started".</summary>
[Fact]
public async Task A_command_cancelled_inside_its_save_is_not_reported_not_started()
{
using var rig = new Rig();
await ActiveAndAcked(rig);
var saveReached = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
rig.Store.OnSave = async (_, ct) => { saveReached.TrySetResult(); await Task.Delay(Timeout.Infinite, ct); };
using var cts = CancellationTokenSource.CreateLinkedTokenSource(Ct);
var comment = rig.Engine.AddCommentAsync("HighTemp", "alice", "saving", cts.Token);
await saveReached.Task.WaitAsync(TimeSpan.FromSeconds(10), Ct);
cts.Cancel();
var cancelled = (await CaughtAsync(comment)).ShouldBeAssignableTo<OperationCanceledException>();
cancelled.ShouldNotBeOfType<ScriptedAlarmCommandNotStartedException>();
rig.Store.OnSave = null;
}
private static async Task<Exception?> CaughtAsync(Task task)
{
try
{
await task;
return null;
}
catch (Exception ex)
{
return ex;
}
}
/// <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()