feat(alarms): thread an additive AckTime through the native-alarm mirror

MES alarm-status API §6.4 (docs/plans/2026-06-30-mes-alarm-status-api.md,
Phase 1 task 1). MES needs a real AckDT for a triggered alarm, and the mirror
carried acked-vs-unacked but never WHEN. AckTime now rides the whole path:
DCL transition -> AlarmStateChanged -> gRPC AlarmStateUpdate -> site SQLite.

Stamping rule, identical on both protocols: non-null ONLY while the condition
is active AND acknowledged. That single predicate yields all three required
behaviours -- null while unacked, cleared on re-raise (a re-raise arrives
unacknowledged), and no phantom ack on a return-to-normal. The last one is
load-bearing for MxGateway, which maps INACTIVE to Acknowledged = true; without
the active check every clear would claim an ack the system never observed.

Provenance is honest, never fabricated:
  - OPC UA A&C supplies a TRUE ack instant, so we now select it:
    AcknowledgeableConditionType/AckedState/TransitionTime at SelectClause
    index 18, APPENDED so the positional reads at 0-17 keep their meaning.
    Servers that omit the field fall back to the event's own Time.
  - MxAccess Gateway supplies none, so the ack transition's own timestamp is
    used -- accurate to when the system SAW the ack. An ACTIVE_ACKED
    re-subscribe snapshot restores one from LastTransitionTimestamp rather
    than dropping it.
The decision lives in pure mappers (Opc/Mx AlarmMapper.DeriveAckTime), so it is
unit-tested with no live server or gateway.

Additive-only throughout: init-only property on AlarmStateChanged, trailing
optional positional on NativeAlarmTransition (all 14-arg call sites untouched),
proto field 24 (never reusing a number) regenerated via docker/regen-proto.sh
sitestream with the csproj diff verified empty.

Persistence rides native_alarm_state's existing metadata_json blob rather than
a new column -- deliberately. That table is RegisterReplicated in
SiteLocalDbSetup and LocalDb builds its CDC triggers from the column list at
registration time, so an additive JSON property changes no schema, no triggers
and no replication contract; metadata_json is exactly the extension point UA4
introduced for this. Rows written before the field deserialize it as null.

Tests: 4 OPC UA + 6 MxGateway mapper cases, 3 NativeAlarmActor (emit,
failover rehydrate, pre-AckTime row), 1 proto round-trip incl. the null case,
4 Commons additive/back-compat. The OPC UA SelectClause count lock-in moves
18 -> 19 with an index-18 assertion -- intended, the clause is appended, which
is precisely what that guard exists to make visible.
This commit is contained in:
Joseph Doherty
2026-08-01 13:12:04 -04:00
parent 6dc5d94cb9
commit 01bcca992c
19 changed files with 775 additions and 113 deletions
@@ -25,4 +25,34 @@ public class AlarmStateChangedEnrichmentTests
Assert.True(c.Acknowledged);
Assert.Equal(250, c.Severity);
}
// ── MES alarm-status API §6.4: additive AckTime ──
[Fact]
public void AckTime_DefaultsToNull_OnThePositionalConstructor()
{
// Additive-only evolution: every existing positional construction stays valid and
// reports no ack time. A computed alarm is auto-acked but has no operator ack
// event, so null (not the timestamp) is the honest value.
var m = new AlarmStateChanged("inst", "HiAlarm", AlarmState.Active, 700, DateTimeOffset.UnixEpoch);
Assert.True(m.Condition.Acknowledged); // computed = auto-acked…
Assert.Null(m.AckTime); // …yet still no ack instant
}
[Fact]
public void AckTime_RoundTripsThroughTheInitProperty()
{
var ackedAt = new DateTimeOffset(2026, 8, 1, 12, 0, 0, TimeSpan.Zero);
var m = new AlarmStateChanged("inst", "Tank01.Level.HiHi", AlarmState.Active, 900, DateTimeOffset.UnixEpoch)
{
Kind = AlarmKind.NativeOpcUa,
AckTime = ackedAt
};
Assert.Equal(ackedAt, m.AckTime);
// `with` (used by the mirror to synthesise a return-to-normal) preserves it.
Assert.Equal(ackedAt, (m with { State = AlarmState.Normal }).AckTime);
}
}
@@ -247,6 +247,44 @@ public class CompatibilityTests
Assert.Equal("HighTemp", deserialized.AlarmName);
}
[Fact]
public void RoundTrip_AlarmStateChanged_PreservesAckTime()
{
// MES alarm-status API §6.4: the additive AckTime must survive the wire, or the
// central live view would show every mirrored alarm as never acknowledged.
var ackedAt = new DateTimeOffset(2026, 8, 1, 12, 0, 0, TimeSpan.Zero);
var msg = new AlarmStateChanged("inst-1", "Tank01.Level.HiHi", AlarmState.Active, 900, DateTimeOffset.UtcNow)
{
AckTime = ackedAt
};
var deserialized = JsonSerializer.Deserialize<AlarmStateChanged>(
JsonSerializer.Serialize(msg), Options);
Assert.Equal(ackedAt, deserialized!.AckTime);
}
[Fact]
public void BackwardCompat_AlarmStateChanged_MissingAckTime_DefaultsToNull()
{
// A frame minted by a node predating the AckTime enrichment omits the property
// entirely; it must deserialize to "no ack time", not fail.
var json = """
{
"InstanceUniqueName": "inst-1",
"AlarmName": "Tank01.Level.HiHi",
"State": 1,
"Priority": 900,
"Timestamp": "2026-08-01T00:00:00+00:00"
}
""";
var deserialized = JsonSerializer.Deserialize<AlarmStateChanged>(json, Options);
Assert.NotNull(deserialized);
Assert.Null(deserialized!.AckTime);
}
[Fact]
public void RoundTrip_HeartbeatMessage_Succeeds()
{
@@ -24,4 +24,22 @@ public class NativeAlarmMessagesTests
Assert.Equal("PlantOpcUa", u.ConnectionName);
Assert.Equal("Tank01", u.Transition.SourceObjectReference);
}
[Fact]
public void NativeAlarmTransition_AckTime_IsAnAdditiveTrailingParameter()
{
// MES alarm-status API §6.4. The 14-argument positional form (every pre-existing
// call site) must still compile and report no ack time; the 15th argument is the
// only way to set one.
var withoutAck = new NativeAlarmTransition("Tank01.Hi", "Tank01", "x", AlarmTransitionKind.Raise,
new AlarmConditionState(true, false, null, AlarmShelveState.Unshelved, false, 500),
"", "", "", "", "", null, DateTimeOffset.UnixEpoch, "", "");
Assert.Null(withoutAck.AckTime);
var ackedAt = DateTimeOffset.UnixEpoch.AddMinutes(5);
var withAck = new NativeAlarmTransition("Tank01.Hi", "Tank01", "x", AlarmTransitionKind.Acknowledge,
new AlarmConditionState(true, true, null, AlarmShelveState.Unshelved, false, 500),
"", "", "", "", "", null, DateTimeOffset.UnixEpoch, "", "", ackedAt);
Assert.Equal(ackedAt, withAck.AckTime);
}
}