fix(mtconnect): review rulings for CONDITION alarms (#626)

- Unavailable keeps an ACTIVE condition active: quality-only BadNoCommunication; on data
  return same statement => quality-only Good, Normal => Clear, different => Retrigger.
- A changed alarm reference set (alarm-bearing delta = full rebuild) clears the whole
  last-told record so every active condition is re-asserted; the same set keeps it.
- Quality-only events go through the host's AlarmQualityUpdate path, never a projected
  snapshot (which could ack a retained unacked condition or rewrite message/severity).
- Deterministic tie-break between equal-level conditions (ordinal nativeCode, then type).
- Dedup key is level+nativeCode+nativeSeverity+qualifier; a text-only change updates nothing.

Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
This commit is contained in:
Joseph Doherty
2026-10-08 15:38:52 -04:00
parent 6a5f5c5a28
commit 9c8e3da5da
7 changed files with 382 additions and 112 deletions
@@ -48,11 +48,19 @@ public sealed partial class MTConnectDriver : IAlarmSource
private readonly Lock _alarmLock = new();
/// <summary>
/// ConditionId (RawPath) → the state the server was last told. Guarded by <see cref="_alarmLock"/>.
/// Survives re-subscribes and lifecycle restarts on purpose — it describes the SERVER's condition node,
/// which outlives both.
/// ConditionId (RawPath) → what the server was last told. Guarded by <see cref="_alarmLock"/>. Survives a
/// re-subscribe with the SAME reference set (a reconnect) and lifecycle restarts — the server's condition
/// nodes outlive both, and re-raising there would un-acknowledge. Cleared WHOLE when the subscribed set
/// changes: only an alarm-bearing raw-tag delta changes it, that delta is a full address-space rebuild,
/// and a rebuild recreates every condition node inactive — so everything must be told again.
/// </summary>
private readonly Dictionary<string, MTConnectConditionState> _deliveredConditions = new(StringComparer.Ordinal);
private readonly Dictionary<string, MTConnectToldCondition> _deliveredConditions = new(StringComparer.Ordinal);
/// <summary>
/// The reference set of the last <see cref="SubscribeAlarmsAsync"/>, kept across an unsubscribe so a
/// release-then-resubscribe of an identical set is still recognised. Guarded by <see cref="_alarmLock"/>.
/// </summary>
private HashSet<string>? _lastAlarmReferences;
/// <summary>The CONDITION tracker for the current session; replaced alongside the observation index.</summary>
private MTConnectConditionTracker _conditions = new();
@@ -100,12 +108,16 @@ public sealed partial class MTConnectDriver : IAlarmSource
lock (_alarmLock)
{
// A condition that left the set has its node torn down by the server; if it comes back it is a NEW
// node that has been told nothing, so forget what its predecessor was told.
foreach (var stale in _deliveredConditions.Keys.Where(k => !references.Contains(k)).ToList())
// A CHANGED set means an alarm-bearing delta, which the server applies as a full rebuild: every
// condition node — including ones still in the set — was recreated inactive and has been told
// nothing. Re-tell all of them. The SAME set is a reconnect: keep the record, or every still-active
// alarm would be re-raised and lose its acknowledgement.
if (_lastAlarmReferences is null || !_lastAlarmReferences.SetEquals(references))
{
_deliveredConditions.Remove(stale);
_deliveredConditions.Clear();
}
_lastAlarmReferences = references;
}
EmitConditionTransitions(touchedDataItemIds: null, resync: true);
@@ -192,10 +204,10 @@ public sealed partial class MTConnectDriver : IAlarmSource
var current = tracker.Get(dataItemId);
if (current is null) continue;
_deliveredConditions.TryGetValue(conditionId, out var delivered);
var evt = MTConnectConditionAlarmMapper.Transition(
delivered, current, conditionId, dataItemId, subscription.Handle, resync);
_deliveredConditions[conditionId] = current;
_deliveredConditions.TryGetValue(conditionId, out var told);
var (evt, nowTold) = MTConnectConditionAlarmMapper.Transition(
told, current, conditionId, dataItemId, subscription.Handle, resync);
_deliveredConditions[conditionId] = nowTold;
if (evt is null) continue;