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
@@ -24,13 +24,23 @@ namespace ZB.MOM.WW.OtOpcUa.Host.OpcUa;
/// </remarks>
internal static class ScriptedAlarmCommandDispatch
{
/// <summary>
/// #951: how long past the caller's bound the local host's answer is still collected. The host bounds one engine op by
/// its command timeout (10 s) and skips an ask whose deadline passed before it started, so an answer that is coming at
/// all comes inside this window.
/// </summary>
internal static readonly TimeSpan LateVerdictWindow = TimeSpan.FromSeconds(30);
/// <summary>
/// Asks the local engine, then broadcasts unless it refused. Starts the ask synchronously (safe under the SDK's Lock);
/// never faults — no answer within <paramref name="timeout"/> is an OutcomeUnknown outcome.
/// </summary>
/// <param name="cmd">The command (with its <c>CommandId</c>, which de-duplicates the broadcast copy on the local host).</param>
/// <param name="timeout">The caller's bound.</param>
/// <param name="askLocal">The ask to this node's scripted-alarm host; null ⇒ none on this node.</param>
/// <param name="askLocal">The ask to this node's scripted-alarm host; null ⇒ none on this node. Its second argument is the
/// caller's bound (the host skips the command once it has passed); the returned task may complete later than that — up to
/// <see cref="LateVerdictWindow"/> past it — and that late answer rides the timeout outcome's
/// <see cref="AlarmCommandOutcome.LateOutcome"/> (#951).</param>
/// <param name="broadcast">Publishes the command on the <c>alarm-commands</c> topic.</param>
/// <param name="logger">Logger.</param>
/// <returns>The local verdict.</returns>
@@ -44,15 +54,24 @@ internal static class ScriptedAlarmCommandDispatch
}
AlarmCommandOutcome outcome;
Task<AlarmCommandOutcome>? ask = null;
try
{
outcome = await askLocal(cmd, timeout).ConfigureAwait(false);
ask = askLocal(cmd, timeout);
outcome = await ask.WaitAsync(timeout).ConfigureAwait(false);
}
catch (Exception ex) when (ex is AskTimeoutException or TaskCanceledException or TimeoutException)
{
// #951: the caller stops waiting, but the host's answer is still collected — a refusal arriving now lets the
// node manager put the node back. Only for the caller's bound expiring (an ask that faulted in time has no
// later answer); an ask completing in the same instant is still handed over, already complete.
var late = ex is TimeoutException ? ask : null;
// The late ask usually ends in an AskTimeoutException nobody may observe: observe it here.
late?.ContinueWith(static t => _ = t.Exception, CancellationToken.None,
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
outcome = new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.OutcomeUnknown,
$"OutcomeUnknown: the scripted-alarm engine did not answer within {timeout.TotalSeconds:0.#} s; the command may still apply.",
TimedOut: true);
TimedOut: true) { LateOutcome = late };
}
catch (Exception ex)
{