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
@@ -38,11 +38,13 @@ internal sealed record MTConnectConditionState(
/// <summary>
/// Whether two states are the SAME alarm statement — the dedup key the alarm path diffs on. Two active
/// states match only when level, <c>nativeCode</c>, <c>nativeSeverity</c>, <c>qualifier</c> and message
/// text all agree (a new code, an escalation, or a changed message is a new statement); two inactive
/// states match on the level alone, so a re-reported Normal never re-clears. The timestamp and the
/// active count are deliberately NOT part of it: a replayed <c>/current</c> after a reconnect carries the
/// same condition under a new sequence, and re-raising it would un-acknowledge an operator's ack.
/// states match when level, <c>nativeCode</c>, <c>nativeSeverity</c> and <c>qualifier</c> agree (a new
/// code or an escalation is a new statement); two inactive states match on the level alone, so a
/// re-reported Normal never re-clears. NOT part of it: the timestamp and the active count (a replayed
/// <c>/current</c> after a reconnect carries the same condition under a new sequence, and re-raising it
/// would un-acknowledge an operator's ack) and the message TEXT (an adapter that embeds a live value in
/// the text, e.g. "Spindle temp 82C", would otherwise re-raise and un-ack on every tick — the message
/// shown stays the statement's first text).
/// </summary>
/// <param name="other">The state to compare against.</param>
/// <returns>True when the alarm path must emit nothing to move from one to the other.</returns>
@@ -53,8 +55,7 @@ internal sealed record MTConnectConditionState(
return string.Equals(Detail.NativeCode, other.Detail.NativeCode, StringComparison.Ordinal)
&& string.Equals(Detail.NativeSeverity, other.Detail.NativeSeverity, StringComparison.Ordinal)
&& string.Equals(Detail.Qualifier, other.Detail.Qualifier, StringComparison.Ordinal)
&& string.Equals(Detail.Text, other.Detail.Text, StringComparison.Ordinal);
&& string.Equals(Detail.Qualifier, other.Detail.Qualifier, StringComparison.Ordinal);
}
}
@@ -75,7 +76,8 @@ internal sealed record MTConnectConditionState(
/// <b>Rules, per DataItem, applied in document order.</b> <c>Warning</c>/<c>Fault</c> activates (or
/// replaces) the entry keyed by its <c>nativeCode</c> (blank = one unnamed entry). <c>Normal</c> with a
/// <c>nativeCode</c> clears that code; <c>Normal</c> without one clears every code. <c>Unavailable</c>
/// clears every code and marks the item unavailable until any other level arrives. A <b>baseline</b>
/// clears every code and marks the item unavailable until any other level arrives (the alarm path does
/// NOT treat that as a clear — see <see cref="MTConnectConditionAlarmMapper"/>). A <b>baseline</b>
/// (<c>/current</c>, which lists every currently-active condition) first resets each DataItem it
/// mentions, so an entry cleared while the driver was not listening cannot survive a re-baseline. An
/// unrecognised (vendor) state word is treated as an active <c>Warning</c> — surfacing an unknown
@@ -92,7 +94,6 @@ internal sealed class MTConnectConditionTracker
private readonly Lock _lock = new();
private readonly Dictionary<string, ItemState> _items = new(StringComparer.Ordinal);
private long _order;
/// <summary>
/// Folds every CONDITION observation of <paramref name="document"/> into the tracked state.
@@ -144,11 +145,13 @@ internal sealed class MTConnectConditionTracker
if (item.Active.Count > 0)
{
// Most severe wins; among equals, the most recently activated — the newest statement the
// operator has not yet seen.
// Most severe wins; among equals, a DETERMINISTIC tie-break (ordinal nativeCode, then type) —
// never document order, which an Agent may present differently on a re-baseline and which would
// then re-raise (and un-ack) an unchanged condition on every reconnect.
var winner = item.Active.Values
.OrderByDescending(e => e.Level)
.ThenByDescending(e => e.Order)
.ThenBy(e => e.Detail.NativeCode ?? string.Empty, StringComparer.Ordinal)
.ThenBy(e => e.Detail.ConditionType ?? string.Empty, StringComparer.Ordinal)
.First();
return new MTConnectConditionState(winner.Level, winner.Detail, winner.TimestampUtc, item.Active.Count);
@@ -185,7 +188,7 @@ internal sealed class MTConnectConditionTracker
private void ApplyOne(ItemState item, MTConnectObservation observation)
{
var detail = observation.Condition!;
var entry = new Entry(LevelOf(observation.Value), detail, observation.TimestampUtc, ++_order);
var entry = new Entry(LevelOf(observation.Value), detail, observation.TimestampUtc);
switch (entry.Level)
{
@@ -209,7 +212,7 @@ internal sealed class MTConnectConditionTracker
}
}
private sealed record Entry(MTConnectConditionLevel Level, MTConnectConditionDetail Detail, DateTime TimestampUtc, long Order);
private sealed record Entry(MTConnectConditionLevel Level, MTConnectConditionDetail Detail, DateTime TimestampUtc);
private sealed class ItemState
{