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 <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-11-28 11:01:47 -05:00
parent 4d41e61858
commit e45012b69d
3 changed files with 48 additions and 5 deletions
@@ -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
/// <param name="operation">The Part 9 operation name (e.g. <c>Acknowledge</c>, <c>TimedShelve</c>).</param>
/// <param name="comment">The call's comment text, or <c>null</c> when none was supplied.</param>
/// <param name="unshelveAt">For <c>TimedShelve</c>, the computed UTC expiry; otherwise <c>null</c>.</param>
/// <param name="scriptedEngine">#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.</param>
/// <returns><c>ServiceResult.Good</c> when allowed (the SDK then applies state, and the method path
/// reports the one condition event);<c>BadUserAccessDenied</c> when the gate vetoes (no route, no state mutation).</returns>
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);