feat(grpc-data): A1 scripted OnInputsEstablished — engine flip queued last in pending, host holds flips during a load until after the re-assert, MarkConditionsKnown on the alarm chain

This commit is contained in:
Joseph Doherty
2026-11-24 09:38:51 -05:00
parent 3ad6d7e8c7
commit a248616761
4 changed files with 311 additions and 6 deletions
@@ -131,6 +131,17 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
private readonly ILoggingAdapter _log = Context.GetLogger();
private readonly CancellationTokenSource _cts = new();
private readonly EventHandler<ScriptedAlarmEvent> _onEngineEvent;
private readonly EventHandler<ScriptedAlarmInputsEventArgs> _onInputsEstablished;
/// <summary>A1 (grpc-data §5.1 T2/F2): an engine input-establishment flip, marshaled onto the actor thread on the
/// SAME mailbox as <see cref="EngineEmission"/> (the engine raises it after that evaluation's emissions).</summary>
private sealed record EngineInputsEstablished(string AlarmId, bool Established);
// Flips that arrive while a load is in flight wait for that load's re-assert (OnAlarmsLoaded): a LoadAsync fires
// its flips before the AlarmsLoaded pipe-back, and marking a re-materialised node known before the re-assert of
// its persisted non-normal state lands would be a falsely-normal known (T2). Latest flip per alarm wins.
private readonly Dictionary<string, bool> _heldEstablishments = new(StringComparer.Ordinal);
private int _loadedGeneration;
private readonly TimeSpan _commandTimeout;
/// <summary>True when THIS incarnation built its engine (factory Props) and therefore disposes it on
@@ -306,6 +317,8 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
var self = Self;
_onEngineEvent = (_, e) => self.Tell(new EngineEmission(e));
_engine.OnEvent += _onEngineEvent;
_onInputsEstablished = (_, e) => self.Tell(new EngineInputsEstablished(e.AlarmId, e.Established));
_engine.OnInputsEstablished += _onInputsEstablished;
Receive<ApplyScriptedAlarms>(OnApply);
Receive<AlarmsLoaded>(OnAlarmsLoaded);
@@ -316,6 +329,7 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
Receive<VirtualTagActor.DependencyValueBatch>(OnDependencyBatch);
Receive<DependencyMuxActor.ProjectedDependencyWake>(wake => wake.Consume(items => OnDependencyBatch(new(items))));
Receive<EngineEmission>(OnEngineEmission);
Receive<EngineInputsEstablished>(OnEngineInputsEstablished);
// Cluster redundancy snapshots (published on the `redundancy-state` topic, subscribed in PreStart)
// cache this node's role so OnEngineEmission can gate the cluster-wide alerts publish to the Primary.
Receive<RedundancyStateChanged>(OnRedundancyStateChanged);
@@ -348,6 +362,9 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
// applies kick off concurrent LoadAsyncs whose continuations may complete out of order; tagging
// each with its generation lets OnAlarmsLoaded discard any but the latest.
var gen = ++_loadGeneration;
// The new load clears the engine's established set and re-raises every flip; any held from the load this one
// supersedes would describe that load, not this one.
_heldEstablishments.Clear();
// Skip disabled plans entirely — the engine has no Enabled flag, so a disabled alarm is simply
// not loaded (no predicate, no upstream subscription, no events).
@@ -412,8 +429,30 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
msg.DepRefs.Count, dropped);
ReassertConditionNodes();
// A1: the re-assert above is queued to the publish actor first, so a held "known" lands after it.
_loadedGeneration = msg.Generation;
foreach (var (alarmId, established) in _heldEstablishments) SendEstablishment(alarmId, established);
_heldEstablishments.Clear();
}
private void OnEngineInputsEstablished(EngineInputsEstablished msg)
{
if (_loadedGeneration != _loadGeneration)
{
_heldEstablishments[msg.AlarmId] = msg.Established;
return;
}
SendEstablishment(msg.AlarmId, msg.Established);
}
/// <summary>Same actor → publish-actor chain as the <see cref="OpcUaPublishActor.AlarmStateUpdate"/>s, so the mark is
/// applied after every projection this host sent before it (F2). Scripted conditions live in the UNS realm.</summary>
private void SendEstablishment(string alarmId, bool established) =>
_publishActor.Tell(new OpcUaPublishActor.MarkConditionsKnown(
OpcUaPublishActor.ScriptedSource, [alarmId], AddressSpaceRealm.Uns, established));
/// <summary>
/// re-project every loaded alarm that is NOT in the Part 9 "no-event" position back onto
/// its condition node, right after a (re)load.
@@ -836,6 +875,7 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
// re-attach to (R02:H1).
_cts.Cancel();
_engine.OnEvent -= _onEngineEvent;
_engine.OnInputsEstablished -= _onInputsEstablished;
if (_ownsEngine || !_restarting) _engine.Dispose();
_cts.Dispose();
base.PostStop();