fix(alarms): #937 review r2 — deadline/poison refusals unsent, Call size quota, expired asks skip the engine, mutation-counter revert guard

M1: a scripted method whose request deadline leaves < the dispatch floor, or that
follows an unanswered scripted method in the same Call, is refused BadTimeout in
its handler (unsent, unapplied); OpcUa:Quotas:MaxNodesPerMethodCall = 1000.
M2: ExecuteAlarmCommand carries the asker's deadline; an expired ask skips the
engine and is not remembered (the broadcast copy applies).
L1: revert guarded by a per-condition mutation counter; raw severity restored.
L5 + nits: docs, stale comment. SDK check: no Part 9 handler calls our delegate
before its own validation.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-11-28 12:47:08 -05:00
parent 2d54d9d671
commit e42e666a1d
12 changed files with 249 additions and 25 deletions
@@ -296,6 +296,102 @@ public sealed class ScriptedCommandVerdictWireTests
finally { await rig.DisposeAsync(); }
}
/// <summary>
/// #937 review r2 M1: a scripted method whose request deadline leaves less than the dispatch floor is refused
/// BadTimeout unsent (nothing asked, nothing applied); after the first unanswered scripted method of a batch the rest
/// of that batch's scripted methods are refused unsent — while a native method in the same batch still runs.
/// </summary>
[Fact]
public async Task A_too_close_deadline_or_an_earlier_unanswered_command_refuses_scripted_methods_unsent()
{
var ct = TestContext.Current.CancellationToken;
var rig = await StartAsync("Verdict937Deadline", encrypted: false, ct);
try
{
rig.Enforce(false, [Key()], ClusterGrant(KeyPrincipal, "acl-key", All));
rig.MaterialiseScripted();
rig.MaterialiseNative();
rig.Nm.NativeAlarmAckRouter = _ => { };
rig.Nm.ScriptedCommandTimeout = TimeSpan.FromMilliseconds(400);
var asked = new ConcurrentQueue<string>();
var never = new TaskCompletionSource<AlarmCommandOutcome>();
rig.Nm.AlarmCommandAsker = (cmd, _) => { asked.Enqueue(cmd.Operation); return never.Task; };
var pre = new AlarmConditionSnapshot(Active: true, Acknowledged: false, Confirmed: true, Enabled: true,
Shelving: AlarmShelvingKind.Unshelved, Severity: 700, Message: "pre");
rig.Project(false, pre);
rig.Project(true, pre);
await rig.DrainProjectionsAsync();
using var session = await rig.ConnectAsync(anonymous: false, ct);
ByteString EventIdOf(bool native) => rig.Nm.TryGetAlarmCondition(native ? RawAlarmPath : ScriptedAlarmId,
native ? AddressSpaceRealm.Raw : AddressSpaceRealm.Uns)!.EventId!.Value;
CallMethodRequest Request(bool native, NodeId method, string text) => new()
{
ObjectId = ConditionNodeId(session, native), MethodId = method,
InputArguments = [new Variant(EventIdOf(native)), new Variant(new LocalizedText(text))],
};
// (a) TimeoutHint 200 ms < grace + dispatch floor: refused unsent.
var tight = await session.CallAsync(new RequestHeader { TimeoutHint = 200 },
[Request(false, MethodIds.AcknowledgeableConditionType_Acknowledge, "tight")], ct);
tight.Results[0].StatusCode.Code.ShouldBe(StatusCodes.BadTimeout.Code);
asked.ShouldBeEmpty("nothing was sent");
rig.Nm.TryGetAlarmCondition(ScriptedAlarmId)!.AckedState!.Id!.Value.ShouldBeFalse("nothing was applied");
// (b) one unanswered scripted method poisons the rest of its batch (scripted only).
var started = DateTime.UtcNow;
var batch = await session.CallAsync(null,
[
Request(false, MethodIds.AcknowledgeableConditionType_Acknowledge, "first"),
Request(false, MethodIds.ConditionType_AddComment, "second"),
Request(true, MethodIds.AcknowledgeableConditionType_Acknowledge, "native"),
Request(false, MethodIds.ConditionType_AddComment, "third"),
], ct);
batch.Results.ToArray()!.Select(r => r.StatusCode.Code).ToArray().ShouldBe(
[StatusCodes.BadTimeout.Code, StatusCodes.BadTimeout.Code, StatusCodes.Good.Code, StatusCodes.BadTimeout.Code]);
asked.ToArray().ShouldBe(["Acknowledge"], "only the first scripted method was sent");
(DateTime.UtcNow - started).ShouldBeLessThan(TimeSpan.FromSeconds(3), "one bound for the batch, not one per request");
}
finally { await rig.DisposeAsync(); }
}
/// <summary>
/// #937 review r2 L1: a refusal does not revert a node something else changed since the method — even a change that
/// leaves every compared field equal (here an identical re-projection, the shape of a Timed→Timed re-shelve).
/// </summary>
[Fact]
public async Task A_refusal_does_not_revert_a_node_changed_since_the_method()
{
var ct = TestContext.Current.CancellationToken;
var rig = await StartAsync("Verdict937NoRevert", encrypted: false, ct);
try
{
rig.Enforce(false, [Key()], ClusterGrant(KeyPrincipal, "acl-key", All));
rig.MaterialiseScripted();
var acked = ConditionMethodEventTests.PreState("Acknowledge", native: false) with { Acknowledged = true, Message = "base" };
rig.Nm.AlarmCommandAsker = (cmd, _) => Task.Run(() =>
{
// Another writer touches the node after the method (off Lock — the handler runs under it), leaving every
// field as the method left it.
rig.Project(false, acked);
return new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.NotApplied, "refused");
});
rig.Project(false, acked with { Acknowledged = false });
await rig.DrainProjectionsAsync();
using var session = await rig.ConnectAsync(anonymous: false, ct);
var eventId = rig.Nm.TryGetAlarmCondition(ScriptedAlarmId)!.EventId!.Value;
var refused = await session.CallAsync(null, [new CallMethodRequest
{
ObjectId = ConditionNodeId(session, false), MethodId = MethodIds.AcknowledgeableConditionType_Acknowledge,
InputArguments = [new Variant(eventId), new Variant(new LocalizedText("x"))],
}], ct);
refused.Results[0].StatusCode.Code.ShouldBe(StatusCodes.BadInvalidState.Code);
rig.Nm.TryGetAlarmCondition(ScriptedAlarmId)!.AckedState!.Id!.Value.ShouldBeTrue("the newer write wins over the revert");
}
finally { await rig.DisposeAsync(); }
}
/// <summary>A2b: a remote (gRPC data) condition method on a scripted condition carries the same verdict.</summary>
[Fact]
public async Task A_remote_condition_method_reports_the_engine_refusal_as_dispatched()