fix(alarms): #999 K-4 a timed shelve expiring on a predicate evaluation emits Unshelved + AutoUnshelve audit

This commit is contained in:
Joseph Doherty
2026-12-03 10:39:17 -05:00
parent b9955f6946
commit 626d0b33c5
5 changed files with 154 additions and 19 deletions
@@ -22,7 +22,8 @@ namespace ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms;
/// (2) Shelved alarms (OneShot / Timed) don't fire active transitions to
/// subscribers, but the state record still advances so that when shelving
/// expires the ActiveState reflects current reality. OneShot expires on the
/// next clear; Timed expires at <see cref="ShelvingState.UnshelveAtUtc"/>.
/// next clear; Timed expires at <see cref="ShelvingState.UnshelveAtUtc"/>, and only through
/// <see cref="ApplyShelvingCheck"/> (#999 / K-4) — never inside <see cref="ApplyPredicate"/>.
/// </para>
/// </remarks>
public static class Part9StateMachine
@@ -44,8 +45,10 @@ public static class Part9StateMachine
if (current.Enabled == AlarmEnabledState.Disabled)
return TransitionResult.NoOp(current, "disabled — predicate result ignored");
// Expire timed shelving if the configured clock has passed.
var shelving = MaybeExpireShelving(current.Shelving, nowUtc);
// #999 / review K-4: a timed shelve is NOT expired here. Expiry belongs to ApplyShelvingCheck alone (it carries
// the AutoUnshelve audit comment and the Unshelved emission); the engine runs it before this call. Expiring it
// here returned EmissionKind.None — the alarm unshelved with no event and no audit.
var shelving = current.Shelving;
var stateWithShelving = current with { Shelving = shelving };
// Shelved alarms still update state but skip event emission.
@@ -84,8 +87,7 @@ public static class Part9StateMachine
return new TransitionResult(next, shelved ? EmissionKind.Suppressed : EmissionKind.Cleared);
}
// Predicate matches current Active — no state change beyond possible shelving
// expiry.
// Predicate matches current Active — no state change.
return new TransitionResult(stateWithShelving, EmissionKind.None);
}
@@ -325,12 +327,6 @@ public static class Part9StateMachine
return TransitionResult.None(current);
}
private static ShelvingState MaybeExpireShelving(ShelvingState s, DateTime nowUtc)
{
if (s.Kind != ShelvingKind.Timed) return s;
return s.UnshelveAtUtc is DateTime t && nowUtc >= t ? ShelvingState.Unshelved : s;
}
private static ImmutableList<AlarmComment> AppendComment(
ImmutableList<AlarmComment> existing, DateTime ts, string user, string kind, string? text)
=> existing.Add(new AlarmComment(ts, user, kind, text ?? string.Empty));
@@ -508,7 +508,7 @@ public sealed class ScriptedAlarmEngine : IDisposable
foreach (var (alarmId, state) in _alarms)
{
var seed = await SeedAsync(alarmId, state, carryOver, ct).ConfigureAwait(false);
var evalSeed = ExpireTimedShelveAtLoad(state, seed.State, pending);
var evalSeed = ExpireTimedShelve(state, seed.State, _clock(), pending);
var afterPredicate = await EvaluatePredicateToStateAsync(state, evalSeed, nowUtc: _clock(), ct, pending)
.ConfigureAwait(false);
await SaveLoadedAsync(seed, afterPredicate, ct).ConfigureAwait(false);
@@ -525,7 +525,7 @@ public sealed class ScriptedAlarmEngine : IDisposable
{
var seed = await SeedAsync(alarmId, state, carryOver, ct).ConfigureAwait(false);
sources.Add(seed);
seeds.Add((state, ExpireTimedShelveAtLoad(state, seed.State, pending)));
seeds.Add((state, ExpireTimedShelve(state, seed.State, _clock(), pending)));
}
var after = await EvaluatePredicateBatchToStatesAsync(seeds, _clock(), ct, pending).ConfigureAwait(false);
for (var i = 0; i < seeds.Count; i++)
@@ -605,10 +605,16 @@ public sealed class ScriptedAlarmEngine : IDisposable
/// predicate runs. Otherwise <see cref="Part9StateMachine.ApplyPredicate"/>'s own expiry silently unshelved it
/// (no audit entry, no event) and the shelving timer found nothing left to expire.
/// </summary>
private AlarmConditionState ExpireTimedShelveAtLoad(
AlarmState state, AlarmConditionState seed, List<ScriptedAlarmEvent> pendingEmissions)
/// <remarks>
/// #999 / review K-4: the same expiry runs before every completed predicate evaluation (serial and batch paths),
/// not only at load — a push that crossed the deadline before the 5 s shelving timer otherwise unshelved the alarm
/// silently and the OPC UA node stayed TimedShelved. <see cref="Part9StateMachine.ApplyPredicate"/> no longer
/// expires a timed shelve itself, so the shelving check is the only place one expires.
/// </remarks>
private AlarmConditionState ExpireTimedShelve(
AlarmState state, AlarmConditionState seed, DateTime nowUtc, List<ScriptedAlarmEvent> pendingEmissions)
{
var check = Part9StateMachine.ApplyShelvingCheck(seed, _clock());
var check = Part9StateMachine.ApplyShelvingCheck(seed, nowUtc);
if (check.Emission == EmissionKind.None) return seed;
var evt = BuildEmission(state, check.State, check.Emission, LastWorstStatus(state.Definition.AlarmId));
if (evt is not null) pendingEmissions.Add(evt);
@@ -1109,7 +1115,10 @@ public sealed class ScriptedAlarmEngine : IDisposable
// QualityChanged on every push instead of once per real bucket change.
var qualityBucketChanged = TrackQualityBucket(state.Definition.AlarmId, worstStatus);
var result = Part9StateMachine.ApplyPredicate(seed, predicateTrue, nowUtc);
// #999 / K-4: an expired timed shelve is unshelved by the shelving check (Unshelved event + AutoUnshelve audit),
// queued BEFORE the predicate's own transition, never silently inside ApplyPredicate.
var result = Part9StateMachine.ApplyPredicate(
ExpireTimedShelve(state, seed, nowUtc, pendingEmissions), predicateTrue, nowUtc);
var transition = result.Emission != EmissionKind.None
? BuildEmission(state, result.State, result.Emission, worstStatus, captured)
: null;
@@ -1285,7 +1294,10 @@ public sealed class ScriptedAlarmEngine : IDisposable
// From here on, identical to the serial path: the bucket is tracked only once the predicate completed (I3).
var qualityBucketChanged = TrackQualityBucket(state.Definition.AlarmId, worstStatus);
var result = Part9StateMachine.ApplyPredicate(seed, predicateTrue, nowUtc);
// #999 / K-4: an expired timed shelve is unshelved by the shelving check (Unshelved event + AutoUnshelve audit),
// queued BEFORE the predicate's own transition, never silently inside ApplyPredicate.
var result = Part9StateMachine.ApplyPredicate(
ExpireTimedShelve(state, seed, nowUtc, pendingEmissions), predicateTrue, nowUtc);
var transition = result.Emission != EmissionKind.None
? BuildEmission(state, result.State, result.Emission, worstStatus, captured)
: null;