From e45012b69d66f4257e7b3cdcc1f7499917359751 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Sat, 28 Nov 2026 11:01:47 -0500 Subject: [PATCH] fix(alarms): a native condition's AddComment is not subject to the scripted verdict (#937) Native AddComment shares HandleAlarmCommand but is applied at the node only; asking the scripted host made it BadInvalidState (live A02 on fu937-1). Co-Authored-By: Claude --- .../grpc-data/conditions_scenarios.py | 3 +- .../OtOpcUaNodeManager.cs | 13 +++++-- .../ScriptedCommandVerdictWireTests.cs | 37 +++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/scripts/validation/grpc-data/conditions_scenarios.py b/scripts/validation/grpc-data/conditions_scenarios.py index f04db1c69..23c8769e7 100644 --- a/scripts/validation/grpc-data/conditions_scenarios.py +++ b/scripts/validation/grpc-data/conditions_scenarios.py @@ -558,7 +558,8 @@ class ConditionScenarios: finally: restored_at = self.utc(-2) self.r.mutate(scripted_predicate=good) - loaded = wait_for(lambda: self.log_lines(source, restored_at, "ScriptedAlarmEngine loaded"), 120) + # The engine's own "loaded" line rides the script-log topic; the host's re-assert of the still-active condition is on stdout. + loaded = wait_for(lambda: self.log_lines(source, restored_at, "ScriptedAlarmHost: re-asserted"), 120) self.check("origin_engine_reloaded", loaded) # Positive control: the first good load after a failed one is a new occurrence (documented); the ack now succeeds. fresh = wait_for(lambda: self.latest(origin, name, lambda b: b.get("State") == "Active, Unacknowledged" diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs index acc50514d..b516a2d3b 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs @@ -2649,7 +2649,9 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage ? new ServiceResult(StatusCodes.BadNotSupported) : HandleAlarmCommand(context, condition, "Confirm", comment, unshelveAt: null); alarm.OnAddComment = (context, condition, _, comment) => - HandleAlarmCommand(context, condition, "AddComment", comment, unshelveAt: null); + // A native AddComment is applied at the node only (no engine owns it): no scripted pre-check or verdict (#937). + HandleAlarmCommand(context, condition, "AddComment", comment, unshelveAt: null, + scriptedEngine: !IsNativeAlarmNode(alarmNodeId, realm)); alarm.OnShelve = (context, condition, shelving, oneShot, shelvingTime) => { if (IsNativeAlarmNode(alarmNodeId, realm)) return new ServiceResult(StatusCodes.BadNotSupported); @@ -3194,10 +3196,13 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage /// The Part 9 operation name (e.g. Acknowledge, TimedShelve). /// The call's comment text, or null when none was supplied. /// For TimedShelve, the computed UTC expiry; otherwise null. + /// #937: false for a NATIVE condition's AddComment (applied at the node; no engine owns it) — + /// no scripted pre-check, no verdict ask; the fire-and-forget router as before. /// ServiceResult.Good when allowed (the SDK then applies state, and the method path /// reports the one condition event);BadUserAccessDenied when the gate vetoes (no route, no state mutation). private ServiceResult HandleAlarmCommand( - ISystemContext context, ConditionState condition, string operation, LocalizedText? comment, DateTime? unshelveAt) + ISystemContext context, ConditionState condition, string operation, LocalizedText? comment, DateTime? unshelveAt, + bool scriptedEngine = true) { // Resolve the principal the SAME way the SDK's own GetCurrentUserId does, then narrow to the // role-carrying identity T17 attached. Anonymous / non-role-carrying identities ⇒ null ⇒ denied. @@ -3212,7 +3217,7 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage // #937: this node's engine holds no complete generation (its last load failed) and would reject the command — // refuse it before the SDK applies anything (no event, nothing routed). One volatile read; safe under Lock. - if (ScriptedAlarmCommandPreflight?.Invoke() is { } notReady) + if (scriptedEngine && ScriptedAlarmCommandPreflight?.Invoke() is { } notReady) return new ServiceResult(StatusCodes.BadInvalidState, new LocalizedText(notReady)); var cmd = new AlarmCommand( @@ -3224,7 +3229,7 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage // #937: inside a Call that awaits the engine's verdict (CallAsync / ExecuteConditionMethodAsync), start the // bounded ask — non-blocking: it only sends — and let the caller await it once Lock is released. - if (TryStartScriptedCommandAsk(cmd)) return ServiceResult.Good; + if (scriptedEngine && TryStartScriptedCommandAsk(cmd)) return ServiceResult.Good; // Non-blocking by contract (host wires a fire-and-forget mediator.Tell); safe to call under Lock. AlarmCommandRouter?.Invoke(cmd); diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ScriptedCommandVerdictWireTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ScriptedCommandVerdictWireTests.cs index 3f628dcda..87d814e93 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ScriptedCommandVerdictWireTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ScriptedCommandVerdictWireTests.cs @@ -189,6 +189,43 @@ public sealed class ScriptedCommandVerdictWireTests finally { await rig.DisposeAsync(); } } + /// + /// A NATIVE condition's AddComment is applied at the node (no scripted engine owns it): neither the scripted pre-check + /// nor the verdict ask applies, even while the scripted engine is blind (caught live by A02 on fu937-1). + /// + [Fact] + public async Task A_native_AddComment_is_not_subject_to_the_scripted_verdict() + { + var ct = TestContext.Current.CancellationToken; + var rig = await StartAsync("Verdict937NativeComment", encrypted: false, ct); + try + { + rig.Enforce(false, [Key()], ClusterGrant(KeyPrincipal, "acl-key", All)); + rig.MaterialiseNative(); + var asked = 0; + rig.Nm.AlarmCommandAsker = (cmd, _) => + { + Interlocked.Increment(ref asked); + return Task.FromResult(new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.NotApplied, "not mine")); + }; + rig.Nm.ScriptedAlarmCommandPreflight = () => "the scripted engine is blind"; + + using var session = await rig.ConnectAsync(anonymous: false, ct); + var events = new RecordedEvents(); + var subscription = await RecordedEvents.SubscribeAsync(session, events, ct); + var item = RecordedEvents.AddEventItem(subscription, ObjectIds.Server); + await subscription.ApplyChangesAsync(ct); + rig.Project(true, ConditionMethodEventTests.PreState("AddComment", native: true) with { Message = "base" }); + await RecordedEvents.WaitUntilAsync(() => events.Has(item, "base")); + + await ConditionMethodEventTests.CallAsync(session, ConditionNodeId(session, true), "AddComment", + events.LatestEventId(item, "base"), ct); + Volatile.Read(ref asked).ShouldBe(0); + await subscription.DeleteAsync(true, ct); + } + finally { await rig.DisposeAsync(); } + } + /// A2b: a remote (gRPC data) condition method on a scripted condition carries the same verdict. [Fact] public async Task A_remote_condition_method_reports_the_engine_refusal_as_dispatched()