fix(runtime): #869 review round 3 — key a native-ack revert to the ack that failed

A pending local ack can outlive its own delivery (sources that send no
Acknowledge back; a Clear now only carries it through), so a later failed
re-ack — or a stale Galaxy confirmation timing out after the alarm re-occurred
and was acked again — could revert an ack it does not own. MarkAcknowledged
now issues a sequence (0 when the ack changed nothing), carried on
RouteAlarmAck and echoed on AlarmAckFailed; TryRevertAcknowledge reverts only
on a matching sequence.

Claude-Session: https://claude.ai/code/session_01GT67AD99zQQqpLCVm3p7xk
This commit is contained in:
Joseph Doherty
2026-11-11 13:25:58 -05:00
parent 5001e40690
commit b421c7ca91
7 changed files with 110 additions and 19 deletions
@@ -18,9 +18,13 @@ public sealed class NativeAlarmProjector
/// <summary>
/// Conditions a client acknowledged locally (<see cref="MarkAcknowledged"/>) whose acknowledge has not yet
/// been confirmed or superseded by a driver transition — the only ones <see cref="TryRevertAcknowledge"/>
/// may un-acknowledge (lmxopcua#760).
/// may un-acknowledge (lmxopcua#760) — keyed to the sequence <see cref="MarkAcknowledged"/> issued, so only a
/// failure of THAT acknowledge reverts it (lmxopcua#869).
/// </summary>
private readonly HashSet<string> _pendingLocalAck = new(StringComparer.Ordinal);
private readonly Dictionary<string, long> _pendingLocalAck = new(StringComparer.Ordinal);
/// <summary>Last sequence issued by <see cref="MarkAcknowledged"/>; 0 is never issued.</summary>
private long _ackSequence;
/// <summary>Project an alarm transition onto the full condition snapshot for <paramref name="nodeId"/>.</summary>
/// <param name="nodeId">The materialised condition node's id (the projection's state key).</param>
@@ -180,12 +184,23 @@ public sealed class NativeAlarmProjector
/// stale unacknowledged prior. An untracked condition already defaults to acknowledged.
/// </summary>
/// <param name="nodeId">The acknowledged condition node id.</param>
public void MarkAcknowledged(string nodeId)
/// <returns>
/// The sequence identifying this pending acknowledge — pass it back to
/// <see cref="TryRevertAcknowledge(string, long, out AlarmConditionSnapshot?)"/> with its failure — or 0 when the
/// ack changed nothing (untracked, or already acknowledged), so its failure has nothing to undo.
/// </returns>
public long MarkAcknowledged(string nodeId)
{
if (!_prior.TryGetValue(nodeId, out var p)) return;
if (!_prior.TryGetValue(nodeId, out var p)) return 0;
// Only an ack that CHANGED the projected state is pending; re-acking an acked condition has nothing to undo.
if (!p.Acked) _pendingLocalAck.Add(nodeId);
long sequence = 0;
if (!p.Acked)
{
sequence = ++_ackSequence;
_pendingLocalAck[nodeId] = sequence;
}
_prior[nodeId] = p with { Acked = true };
return sequence;
}
/// <summary>
@@ -198,10 +213,31 @@ public sealed class NativeAlarmProjector
/// <param name="nodeId">The condition node id whose acknowledge failed.</param>
/// <param name="snapshot">The unacknowledged snapshot to project; null when nothing was reverted.</param>
/// <returns>True when the condition was reverted to unacknowledged.</returns>
public bool TryRevertAcknowledge(string nodeId, out AlarmConditionSnapshot? snapshot)
public bool TryRevertAcknowledge(string nodeId, out AlarmConditionSnapshot? snapshot) =>
TryRevert(nodeId, ackSequence: null, out snapshot);
/// <summary>
/// Like <see cref="TryRevertAcknowledge(string, out AlarmConditionSnapshot?)"/>, but only when the pending
/// acknowledge is the one <see cref="MarkAcknowledged"/> issued <paramref name="ackSequence"/> for
/// (lmxopcua#869). A pending ack can outlive its own delivery — a source that never sends an Acknowledge
/// transition back (FOCAS, MTConnect), across a Clear that only carried it through — so a failure of a LATER,
/// unrelated acknowledge (a re-ack while the driver is down) or of an EARLIER one (a Galaxy confirmation timing
/// out after the alarm re-occurred and was acked again) must not revert it.
/// </summary>
/// <param name="nodeId">The condition node id whose acknowledge failed.</param>
/// <param name="ackSequence">The sequence of the failed acknowledge; 0 never matches.</param>
/// <param name="snapshot">The unacknowledged snapshot to project; null when nothing was reverted.</param>
/// <returns>True when the condition was reverted to unacknowledged.</returns>
public bool TryRevertAcknowledge(string nodeId, long ackSequence, out AlarmConditionSnapshot? snapshot) =>
TryRevert(nodeId, ackSequence, out snapshot);
private bool TryRevert(string nodeId, long? ackSequence, out AlarmConditionSnapshot? snapshot)
{
snapshot = null;
if (!_pendingLocalAck.Remove(nodeId) || !_prior.TryGetValue(nodeId, out var p)) return false;
if (!_pendingLocalAck.TryGetValue(nodeId, out var pending)
|| (ackSequence is { } wanted && wanted != pending)
|| !_prior.TryGetValue(nodeId, out var p)) return false;
_pendingLocalAck.Remove(nodeId);
_prior[nodeId] = p with { Acked = false };
snapshot = new AlarmConditionSnapshot(
Active: p.Active, Acknowledged: false, Confirmed: true, Enabled: true,