fix(grpc-data): A1 review S1 — a scripted condition is established only by a completed predicate
NoteInputsEstablishment raised 'established' before AreInputsReady, the evidence check and the predicate, so a pushed-but-Bad input (driver down), a runner that was not up or a predicate timeout produced a known-inactive record though no predicate had completed. The flip is now raised after the first predicate evaluation that completes since the load or a revert; reverting an input to never-pushed still withdraws it.
This commit is contained in:
@@ -322,18 +322,20 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
|
||||
/// <summary>
|
||||
/// A1 (grpc-data plan §5.1 T2): an alarm's read cache went from holding a never-pushed input
|
||||
/// (<see cref="StatusNeverPushed"/>) to holding none — its condition state is now ESTABLISHED — or back
|
||||
/// (<see cref="ScriptedAlarmInputsEventArgs.Established"/> false: an input reverted to never-pushed, e.g. a tag
|
||||
/// re-created by a deploy). Raised the first time after each <see cref="LoadAsync"/> and on every flip.
|
||||
/// (<see cref="StatusNeverPushed"/>) to its FIRST COMPLETED predicate evaluation — its condition state is now
|
||||
/// ESTABLISHED — or back (<see cref="ScriptedAlarmInputsEventArgs.Established"/> false: an input reverted to
|
||||
/// never-pushed, e.g. a tag re-created by a deploy). "Established" is raised only after a predicate actually
|
||||
/// completed (review S1): never while an input is Bad, the runner is not up, or the predicate timed out, was
|
||||
/// refused or threw. Raised the first time after each <see cref="LoadAsync"/> / revert, and on every flip.
|
||||
/// <para><b>Ordering (F2):</b> queued as the LAST item of the evaluation's pending emissions, so it fires only
|
||||
/// after that evaluation's own <see cref="OnEvent"/> emissions were dispatched, on the same thread.</para>
|
||||
/// <para>This is a new predicate: <see cref="WorstInputStatus"/> filters never-pushed values out.</para>
|
||||
/// </summary>
|
||||
public event EventHandler<ScriptedAlarmInputsEventArgs>? OnInputsEstablished;
|
||||
|
||||
// A1: per alarm, whether its inputs were established at its last evaluation. Only touched under _evalGate;
|
||||
// A1: the alarms whose state is established (a predicate completed since the last load / revert). Only touched under _evalGate;
|
||||
// cleared by every LoadAsync so a reload (a rebuild re-materialised the conditions unknown) re-raises it.
|
||||
private readonly Dictionary<string, bool> _inputsEstablished = new(StringComparer.Ordinal);
|
||||
private readonly HashSet<string> _inputsEstablished = new(StringComparer.Ordinal);
|
||||
|
||||
/// <summary>Gets the collection of loaded alarm identifiers.</summary>
|
||||
public IReadOnlyCollection<string> LoadedAlarmIds => _alarms.Keys.ToArray();
|
||||
@@ -909,7 +911,7 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
state.Definition.AlarmId,
|
||||
_ => new AlarmScratch(state.Inputs, state.Logger, _clock));
|
||||
RefillReadCache(scratch.ReadCache, state.Inputs);
|
||||
NoteInputsEstablishment(state.Definition.AlarmId, scratch.ReadCache, pendingEmissions);
|
||||
NoteInputsReverted(state.Definition.AlarmId, scratch.ReadCache, pendingEmissions);
|
||||
var captured = CaptureInputs(state, scratch.ReadCache);
|
||||
scratch.HasProjectedInputs = captured.Evidence is not null;
|
||||
if (captured.Evidence is { IsValid: false }) return seed;
|
||||
@@ -978,6 +980,7 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
if (captured.Evidence is { IsValid: false }) return seed;
|
||||
MirrorPredicate(state, scratch, ScriptRunOutcome.Ok(predicateTrue));
|
||||
scratch.LastNonCompletion = null;
|
||||
NoteEvaluationCompleted(state.Definition.AlarmId, pendingEmissions);
|
||||
|
||||
// Track the bucket only once the predicate has COMPLETED (I3). The tracked bucket is the last
|
||||
// bucket this alarm was annotated with; tracking the input bucket before the attempt flipped it to
|
||||
@@ -1068,7 +1071,7 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
var scratch = scratches[i] = _scratchByAlarmId.GetOrAdd(
|
||||
state.Definition.AlarmId, _ => new AlarmScratch(state.Inputs, state.Logger, _clock));
|
||||
RefillReadCache(scratch.ReadCache, state.Inputs);
|
||||
NoteInputsEstablishment(state.Definition.AlarmId, scratch.ReadCache, pendingEmissions);
|
||||
NoteInputsReverted(state.Definition.AlarmId, scratch.ReadCache, pendingEmissions);
|
||||
var captured = capturedInputs[i] = CaptureInputs(state, scratch.ReadCache);
|
||||
worst[i] = WorstInputStatus(scratch.ReadCache);
|
||||
if (!AreInputsReady(scratch.ReadCache) || captured.Evidence is { IsValid: false })
|
||||
@@ -1157,6 +1160,7 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
return seed;
|
||||
}
|
||||
scratch.LastNonCompletion = null;
|
||||
NoteEvaluationCompleted(state.Definition.AlarmId, pendingEmissions);
|
||||
|
||||
// From here on, identical to the serial path: the bucket is tracked only once the predicate completed (I3).
|
||||
var qualityBucketChanged = TrackQualityBucket(state.Definition.AlarmId, worstStatus);
|
||||
@@ -1385,22 +1389,30 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
/// that re-enters the engine (e.g. calls AcknowledgeAsync) would
|
||||
/// deadlock against the non-reentrant SemaphoreSlim.
|
||||
/// </summary>
|
||||
/// <summary>A1: record an established/reverted flip of <paramref name="alarmId"/>'s inputs into the caller's pending
|
||||
/// list (fired after every emission in it). Only called under <c>_evalGate</c>.</summary>
|
||||
private void NoteInputsEstablishment(string alarmId, IReadOnlyDictionary<string, DataValueSnapshot> cache,
|
||||
/// <summary>A1 (review S1): withdraw "established" from <paramref name="alarmId"/> when one of its inputs reverted to
|
||||
/// never-pushed (e.g. a tag re-created by a deploy). Only called under <c>_evalGate</c>; the flip is fired after every
|
||||
/// emission in the caller's pending list.</summary>
|
||||
private void NoteInputsReverted(string alarmId, IReadOnlyDictionary<string, DataValueSnapshot> cache,
|
||||
List<ScriptedAlarmEvent> pendingEmissions)
|
||||
{
|
||||
var established = true;
|
||||
if (!_inputsEstablished.Contains(alarmId)) return; // not established: an unknown condition is already unknown
|
||||
foreach (var v in cache.Values)
|
||||
{
|
||||
if (v is null || v.StatusCode == StatusNeverPushed) { established = false; break; }
|
||||
if (v is not null && v.StatusCode != StatusNeverPushed) continue;
|
||||
_inputsEstablished.Remove(alarmId);
|
||||
if (pendingEmissions is PendingEmissions pending) pending.Establishments.Add((alarmId, false));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var had = _inputsEstablished.TryGetValue(alarmId, out var prior);
|
||||
_inputsEstablished[alarmId] = established;
|
||||
// First evaluation after a load: raise only "established" (an unknown condition is already unknown).
|
||||
if (had ? prior == established : !established) return;
|
||||
if (pendingEmissions is PendingEmissions pending) pending.Establishments.Add((alarmId, established));
|
||||
/// <summary>A1 (review S1): a predicate evaluation of <paramref name="alarmId"/> COMPLETED — its inputs were ready
|
||||
/// (none never-pushed, none Bad), its evidence valid and its predicate returned a bool — so the condition's state is
|
||||
/// established. Raised once per load / revert, never on a Bad input, a refused or timed-out predicate or a runner
|
||||
/// that was not up. Only called under <c>_evalGate</c>.</summary>
|
||||
private void NoteEvaluationCompleted(string alarmId, List<ScriptedAlarmEvent> pendingEmissions)
|
||||
{
|
||||
if (!_inputsEstablished.Add(alarmId)) return;
|
||||
if (pendingEmissions is PendingEmissions pending) pending.Establishments.Add((alarmId, true));
|
||||
}
|
||||
|
||||
/// <summary>Fire a caller's pending emissions, then (A1 F2) its input-establishment flips — last.</summary>
|
||||
|
||||
Reference in New Issue
Block a user