fix(alarms): revert a native ack that reaches no driver; refuse native acks while the host is Stale (lmxopcua#765)

Residuals of #760:

1. An ack routed to a disabled / not-yet-spawned driver, or to a stubbed child
   (driver does not run on this node, or its factory rejected the config), was
   dropped and left the condition Acknowledged. DriverHostActor now reverts it
   through the same NativeAlarmProjector.TryRevertAcknowledge path as a refused
   ack.

2. A native ack arriving while the host is Stale was dropped before the projector
   recorded it, so it could never be reverted. Stale now marks the whole
   NativeAlarmAckPreflightView unavailable (SetHostUnavailable), so the node
   manager refuses every native ack synchronously; BecomeSteady clears it.

3. (keyed sessions without a Galaxy identity) is not addressed.

Tests: DriverHostActorNativeAlarmTests' client-ack test and the FOCAS respawn
theory now start the host's own driver (AckingAlarmDriver; a factory building a
real FocasDriver per spawn over per-instance fake CNCs, with a real config-change
respawn) - both fail against the fix in their old shape. New: ack to a disabled
driver reverts, ack to a stubbed driver reverts, a Stale host refuses through the
pre-check view - all three fail with the fix mutated out. Runtime.Tests 943/0/3.
Docs: AlarmTracking.md, .claude/rules/alarms.md.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 13:29:15 -05:00
parent 7f29a75edb
commit f54922f94c
8 changed files with 312 additions and 87 deletions
@@ -479,20 +479,29 @@ public sealed class DriverHostActorNativeAlarmTests : RuntimeActorTestBase
/// after one defaulted Acked to true and happened to be right for a client-acked alarm; now that the
/// projector survives the deploy, a projector that never heard the ack projects the Clear as UNacked and
/// un-acknowledges the alarm the operator acknowledged.
/// <para>
/// lmxopcua#765 — the host's own driver is started here: an ack routed to a driver that is not running
/// is now reverted (it reached no alarm system), so only a delivered ack may stand.
/// </para>
/// </summary>
[Fact]
public void A_clear_after_a_client_acknowledge_and_a_redeploy_stays_acknowledged()
{
var db = NewInMemoryDbFactory();
var deploymentId = SeedV3AlarmDeployment(db, RevA, Driver: "drv-1", Tag: "temp_hi");
var (actor, publish, coordinator) = SpawnHostAndApplyWithCoordinator(db, deploymentId);
var factory = new AckingAlarmDriverFactory("Modbus");
var deploymentId = SeedV3AlarmDeployment(db, RevA, Driver: "drv-1", Tag: "temp_hi", enabled: true);
var (actor, publish, coordinator) = SpawnHostAndApplyWithCoordinator(db, deploymentId, factory);
// An ack reaching a still-Connecting child is dropped (and now reverted); wait for Connected.
AwaitCondition(() => factory.Driver.AlarmsSubscribed, TimeSpan.FromSeconds(5));
actor.Tell(Transition(AlarmTransitionKind.Raise));
publish.ExpectMsg<OpcUaPublishActor.AlarmStateUpdate>(TimeSpan.FromSeconds(5)).State.Acknowledged.ShouldBeFalse();
publish.FishForMessage<OpcUaPublishActor.AlarmStateUpdate>(_ => true, TimeSpan.FromSeconds(5))
.State.Acknowledged.ShouldBeFalse();
// The client's Acknowledge on the node, routed toward the driver (no transition comes back for it).
// The client's Acknowledge on the node, routed to the driver (no transition comes back for it).
actor.Tell(new DriverHostActor.RouteNativeAlarmAck(AlarmRawPath, "seen", "op"));
Redeploy(db, actor, publish, coordinator);
AwaitCondition(() => factory.Driver.Acks == 1, TimeSpan.FromSeconds(5));
Redeploy(db, actor, publish, coordinator, enabled: true);
actor.Tell(Transition(AlarmTransitionKind.Clear));
var cleared = publish.FishForMessage<OpcUaPublishActor.AlarmStateUpdate>(_ => true, TimeSpan.FromSeconds(5));
@@ -507,10 +516,11 @@ public sealed class DriverHostActorNativeAlarmTests : RuntimeActorTestBase
/// <summary>A second deployment carrying the SAME alarm (a new revision of an identical artifact).</summary>
private void Redeploy(
IDbContextFactory<OtOpcUaConfigDbContext> db, IActorRef actor, TestProbe publish, TestProbe coordinator)
IDbContextFactory<OtOpcUaConfigDbContext> db, IActorRef actor, TestProbe publish, TestProbe coordinator,
bool enabled = false)
{
var revB = RevisionHash.Parse(new string('b', 64));
var second = SeedV3AlarmDeployment(db, revB, Driver: "drv-1", Tag: "temp_hi");
var second = SeedV3AlarmDeployment(db, revB, Driver: "drv-1", Tag: "temp_hi", enabled: enabled);
actor.Tell(new DispatchDeployment(second, revB, CorrelationId.NewId()));
coordinator.FishForMessage<ApplyAck>(a => a.DeploymentId == second, TimeSpan.FromSeconds(5))
.Outcome.ShouldBe(ApplyAckOutcome.Applied);
@@ -553,7 +563,7 @@ public sealed class DriverHostActorNativeAlarmTests : RuntimeActorTestBase
}
private (IActorRef Actor, TestProbe Publish, TestProbe Coordinator) SpawnHostAndApplyWithCoordinator(
IDbContextFactory<OtOpcUaConfigDbContext> db, DeploymentId deploymentId)
IDbContextFactory<OtOpcUaConfigDbContext> db, DeploymentId deploymentId, IDriverFactory? driverFactory = null)
{
var coordinator = CreateTestProbe();
var publish = this.CreateCleanOutcomePublishProbe();
@@ -562,6 +572,7 @@ public sealed class DriverHostActorNativeAlarmTests : RuntimeActorTestBase
var actor = Sys.ActorOf(DriverHostActor.Props(
db, TestNode, coordinator.Ref,
driverFactory: driverFactory,
localRoles: new HashSet<string> { "driver" },
dependencyMux: mux.Ref,
opcUaPublishActor: publish.Ref,
@@ -588,7 +599,7 @@ public sealed class DriverHostActorNativeAlarmTests : RuntimeActorTestBase
/// </summary>
private static DeploymentId SeedV3AlarmDeployment(
IDbContextFactory<OtOpcUaConfigDbContext> db, RevisionHash rev,
string Driver, string Tag, bool? historizeToAveva = null)
string Driver, string Tag, bool? historizeToAveva = null, bool enabled = false)
{
// historizeToAveva absent (null) ⇒ omit the key so the absent ⇒ historize default path is exercised;
// a concrete true/false writes the bool into the alarm object so the native path threads it.
@@ -601,7 +612,7 @@ public sealed class DriverHostActorNativeAlarmTests : RuntimeActorTestBase
RawFolders = new[] { new { RawFolderId = "rf-plant", ParentRawFolderId = (string?)null, Name = "Plant", ClusterId = "c1" } },
DriverInstances = new[]
{
new { DriverInstanceId = Driver, RawFolderId = "rf-plant", Name = "Modbus", DriverType = "Modbus", DriverConfig = "{}", ClusterId = "c1", Enabled = false },
new { DriverInstanceId = Driver, RawFolderId = "rf-plant", Name = "Modbus", DriverType = "Modbus", DriverConfig = "{}", ClusterId = "c1", Enabled = enabled },
},
Devices = new[]
{