fix(alarms): #958 a broadcast copy the local engine refuses reverts the node

When the direct verdict ask expired before the engine reached it, or never started before its
asker's deadline, the host answered OutcomeUnknown and let the broadcast copy decide - but nobody
awaited that copy, so a local refusal left the SDK-applied node change until the next good deploy.

The host now keeps a pending decision per CommandId and returns it as the unknown answer's
LateOutcome; OnAlarmCommand settles it with the copy's local outcome (applied / refused / unknown /
not owned). The node manager's existing #951 chain follows it, so a refused copy reverts the node
under the per-condition mutation-counter guard (one chain, one terminal verdict - no double revert;
a newer change wins). Never-arriving copies settle unknown after 30 s; a stopping host settles its
pending decisions unknown; at most 1024 pending per host.

Tests: Runtime host tests (refused/applied copy settles the late outcome, copy-first answers
directly, window expiry, stop) - red without the LateOutcome wiring; wire test of the production
chain reverting exactly once, and not at all after a newer change (falsified by disabling the
mutation guard). Docs: ScriptedAlarms.md residual + #958 section, rules/alarms.md.
This commit is contained in:
Joseph Doherty
2026-12-01 20:24:15 -05:00
parent 92da56c071
commit 401f9a9685
5 changed files with 303 additions and 17 deletions
@@ -31,7 +31,7 @@ public sealed class ScriptedAlarmCommandVerdictTests : RuntimeActorTestBase
private static EquipmentScriptedAlarmPlan BadPlan(string id) => Plan(id, "return unknownIdentifier;");
private (IActorRef Host, ScriptedAlarmEngine Engine, ScriptedAlarmCommandReadinessView Readiness, TestProbe Mux) Spawn(
IAlarmStateStore? store = null, TimeSpan? commandTimeout = null)
IAlarmStateStore? store = null, TimeSpan? commandTimeout = null, TimeSpan? broadcastDecisionWindow = null)
{
var publish = CreateTestProbe();
var mux = CreateTestProbe();
@@ -40,7 +40,7 @@ public sealed class ScriptedAlarmCommandVerdictTests : RuntimeActorTestBase
var engine = new ScriptedAlarmEngine(upstream, store ?? new InMemoryAlarmStateStore(), new ScriptLoggerFactory(logger), logger);
var readiness = new ScriptedAlarmCommandReadinessView();
var host = Sys.ActorOf(ScriptedAlarmHostActor.Props(publish.Ref, mux.Ref, () => (upstream, engine),
commandTimeout: commandTimeout, readiness: readiness));
commandTimeout: commandTimeout, readiness: readiness, broadcastDecisionWindow: broadcastDecisionWindow));
return (host, engine, readiness, mux);
}
@@ -227,10 +227,105 @@ public sealed class ScriptedAlarmCommandVerdictTests : RuntimeActorTestBase
release.SetResult(null);
mux.ExpectMsg<DependencyMuxActor.RegisterInterest>(Timeout, cancellationToken: Ct);
host.Tell(cmd); // the broadcast copy: not shadowed by a remembered refusal
// #958: the unknown answer carries what the broadcast copy did here.
(await unknown.LateOutcome!.WaitAsync(Timeout, Ct)).Verdict.ShouldBe(AlarmCommandVerdict.Applied);
(await Ask(host, "alm-1", "AddComment", "after", "cmd-after")).Verdict.ShouldBe(AlarmCommandVerdict.Applied);
engine.GetState("alm-1")!.Comments.Select(c => c.Text).ShouldBe(["late", "after"]);
}
/// <summary>
/// #958: an ask answered "the broadcast copy decides" carries, as its <see cref="AlarmCommandOutcome.LateOutcome"/>, what
/// THIS host's engine does with that copy — so when the engine refuses the copy (here: the deploy that landed in between
/// failed) the node manager can revert the method's node change. Before #958 nobody awaited the copy and the node kept
/// the change until the next good deploy. An applied copy settles Applied (the node keeps the change).
/// </summary>
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task The_broadcast_copys_local_outcome_settles_the_expired_asks_late_outcome(bool refused)
{
var (host, engine, readiness, mux) = Spawn();
host.Tell(new ScriptedAlarmHostActor.ApplyScriptedAlarms([Plan("alm-1")]));
mux.ExpectMsg<DependencyMuxActor.RegisterInterest>(Timeout, cancellationToken: Ct);
var cmd = new AlarmCommand("alm-1", "AddComment", "alice", "late", null, "cmd-958");
var unknown = await host.Ask<AlarmCommandOutcome>(
new ScriptedAlarmHostActor.ExecuteAlarmCommand(cmd, DateTime.UtcNow.AddSeconds(-1)), Timeout, Ct);
unknown.Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
var late = unknown.LateOutcome;
(late is not null).ShouldBeTrue("the broadcast copy's local outcome is still coming");
late!.IsCompleted.ShouldBeFalse("nothing decided the command yet");
if (refused)
{
host.Tell(new ScriptedAlarmHostActor.ApplyScriptedAlarms([Plan("alm-1"), BadPlan("bad-1")]));
AwaitCondition(() => readiness.Refusal() is not null, Timeout, cancellationToken: Ct);
}
host.Tell(cmd); // the broadcast copy (same CommandId)
var decided = await late.WaitAsync(Timeout, Ct);
decided.Verdict.ShouldBe(refused ? AlarmCommandVerdict.NotApplied : AlarmCommandVerdict.Applied);
if (refused)
{
decided.Reason.ShouldBe(ScriptedAlarmCommandReadinessView.LoadFailedRefusal);
engine.GetState("alm-1")!.Comments.ShouldBeEmpty();
}
else
{
engine.GetState("alm-1")!.Comments.Select(c => c.Text).ShouldBe(["late"]);
}
}
/// <summary>#958: the copy reaching the host BEFORE the expired ask (remembered) answers the ask with that verdict
/// directly — one terminal outcome, so the node manager reverts at most once.</summary>
[Fact]
public async Task A_broadcast_copy_refused_before_the_expired_ask_answers_it_directly()
{
var (host, _, readiness, _) = Spawn();
host.Tell(new ScriptedAlarmHostActor.ApplyScriptedAlarms([Plan("alm-1"), BadPlan("bad-1")]));
AwaitCondition(() => readiness.Refusal() is not null, Timeout, cancellationToken: Ct);
var cmd = new AlarmCommand("alm-1", "AddComment", "alice", "late", null, "cmd-958-first");
host.Tell(cmd);
var answer = await host.Ask<AlarmCommandOutcome>(
new ScriptedAlarmHostActor.ExecuteAlarmCommand(cmd, DateTime.UtcNow.AddSeconds(-1)), Timeout, Ct);
answer.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
(answer.LateOutcome is null).ShouldBeTrue();
}
/// <summary>#958: a broadcast copy that never arrives (the publish failed) settles the late outcome unknown after the
/// window — never left pending, never a refusal.</summary>
[Fact]
public async Task A_broadcast_copy_that_never_arrives_settles_unknown_after_the_window()
{
var (host, _, _, mux) = Spawn(broadcastDecisionWindow: TimeSpan.FromMilliseconds(200));
host.Tell(new ScriptedAlarmHostActor.ApplyScriptedAlarms([Plan("alm-1")]));
mux.ExpectMsg<DependencyMuxActor.RegisterInterest>(Timeout, cancellationToken: Ct);
var cmd = new AlarmCommand("alm-1", "AddComment", "alice", "lost", null, "cmd-958-lost");
var unknown = await host.Ask<AlarmCommandOutcome>(
new ScriptedAlarmHostActor.ExecuteAlarmCommand(cmd, DateTime.UtcNow.AddSeconds(-1)), Timeout, Ct);
var settled = await unknown.LateOutcome!.WaitAsync(Timeout, Ct);
settled.Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
settled.AlarmId.ShouldBe("alm-1");
(settled.LateOutcome is null).ShouldBeTrue("the chain ends: nothing more is coming");
}
/// <summary>#958: a host that stops with a copy still pending settles it unknown (the late chain always ends).</summary>
[Fact]
public async Task A_stopping_host_settles_a_pending_broadcast_decision_unknown()
{
var (host, _, _, mux) = Spawn();
host.Tell(new ScriptedAlarmHostActor.ApplyScriptedAlarms([Plan("alm-1")]));
mux.ExpectMsg<DependencyMuxActor.RegisterInterest>(Timeout, cancellationToken: Ct);
var cmd = new AlarmCommand("alm-1", "AddComment", "alice", "orphan", null, "cmd-958-stop");
var unknown = await host.Ask<AlarmCommandOutcome>(
new ScriptedAlarmHostActor.ExecuteAlarmCommand(cmd, DateTime.UtcNow.AddSeconds(-1)), Timeout, Ct);
Sys.Stop(host);
(await unknown.LateOutcome!.WaitAsync(Timeout, Ct)).Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
}
/// <summary>
/// #953: a load that fails after a newer apply already started its own load is superseded — its failure must not
/// publish "refuse everything" while the newer load is in flight (the engine queues a command behind it; the view is