fix(opcuaserver): UNS reference nodes repair on backing-tag historian/shape edits

Adversarial review of 9f68f272 (classifier: non-alarm raw-tag deltas became
surgical) found stale UNS inheritance after a surgical raw edit. Editing a raw
tag's IsHistorized / HistorianTagname / IsArray / ArrayLength left every
referencing UNS node STALE: none of those fields were on UnsReferenceVariable,
so no coincident UnsReferenceDelta fired; the applier updated the raw node in
place; and EnsureVariable early-returns on an existing node. Consequences:
HistoryRead through the UNS NodeId served a different series than through the
raw NodeId (breaking the documented dual-registration invariant), a
de-historized tag kept serving history via UNS, and a scalar->array flip left
the UNS mirror scalar AND writable (bypassing the array read-only write gate).

UnsReferenceVariable now carries Backing{IsHistorized,HistorianTagname,IsArray,
ArrayLength}, copied from the backing tag's TagConfigIntent at compose time, so
record equality makes the reference Changed in the SAME plan. Both deltas are
non-rebuild kinds, so the existing machinery repairs the mirror surgically:
remove-previous in Apply, recreate in MaterialiseUnsReferences. That pass now
reads the four values from the RECORD (the single truth the planner diffed)
instead of a rawByPath back-lookup, preserving historian resolution exactly
(blank override => BackingRawPath, which is the backing tag's NodeId).

The artifact-decode seam needs no change: DeploymentArtifact.BuildRawAndUnsSubtrees
drives AddressSpaceComposer.Compose verbatim, which is what guarantees byte-parity.
The classifier needs no change either (a UnsReferenceDelta never forces Rebuild);
its comment now records why.

Also from the same review: a three-place-contract comment on RawTagPlan's
hand-written Equals naming its two downstream consumers, plus classifier pins for
the .Any(!eligible) quantifier, an ArrayLength-only delta, and an
alarm-content-only delta.
This commit is contained in:
Joseph Doherty
2026-08-15 17:11:03 -04:00
parent 9dbd5e96f7
commit 875ec9fe47
9 changed files with 416 additions and 30 deletions
@@ -20,7 +20,7 @@ public sealed class AddressSpacePlannerDualNamespaceTests
// Compose a fixture with one raw tag (given name) under Plant/Modbus1/PLC-A and one UNS reference to it
// (with an optional display-name override so the effective name can be held stable across a raw rename).
private static AddressSpaceComposition Compose(string tagName, string? displayOverride)
private static AddressSpaceComposition Compose(string tagName, string? displayOverride, string tagConfig = "{}")
{
var folder = new RawFolder { RawFolderId = "raw-plant", ClusterId = "c1", Name = RawFolderName };
var driver = new DriverInstance
@@ -32,7 +32,7 @@ public sealed class AddressSpacePlannerDualNamespaceTests
var tag = new Tag
{
TagId = "t-speed", DeviceId = "dev-1", Name = tagName,
DataType = "Float", AccessLevel = TagAccessLevel.ReadWrite, TagConfig = "{}",
DataType = "Float", AccessLevel = TagAccessLevel.ReadWrite, TagConfig = tagConfig,
};
var area = new UnsArea { UnsAreaId = "area-1", ClusterId = "c1", Name = "filling" };
var line = new UnsLine { UnsLineId = "line-1", UnsAreaId = "area-1", Name = "line-1" };
@@ -219,6 +219,55 @@ public sealed class AddressSpacePlannerDualNamespaceTests
_ = prev;
}
/// <summary>
/// <b>THE PIN (2026-08-15 surgical-deploys review):</b> a RAW-side edit to the backing tag's historian
/// intent or array shape must produce a <c>ChangedRawTags</c> delta AND a <b>coincident</b>
/// <c>ChangedUnsReferenceVariables</c> delta on every reference to it. The coincident delta is the ONLY
/// repair trigger for the UNS mirror node: the raw node is updated surgically in place and
/// <c>EnsureVariable</c> early-returns on the existing UNS node, so without this delta the mirror keeps
/// the OLD historian tagname (HistoryRead via the UNS NodeId serves a different series than via the raw
/// NodeId) and the OLD shape (a scalar→array flip leaves it scalar AND writable). Nothing about the
/// reference ROW changed here — only the backing tag's TagConfig — so the delta exists purely because
/// the four <c>Backing*</c> fields are on <see cref="UnsReferenceVariable"/>.
/// </summary>
[Theory]
[InlineData("{\"isHistorized\":true,\"historianTagname\":\"WW.Old\"}", "{\"isHistorized\":true,\"historianTagname\":\"WW.New\"}")]
[InlineData("{\"isHistorized\":true}", "{}")]
[InlineData("{}", "{\"isArray\":true,\"arrayLength\":8}")]
[InlineData("{\"isArray\":true,\"arrayLength\":8}", "{\"isArray\":true,\"arrayLength\":16}")]
public void Backing_tag_historian_or_shape_edit_produces_a_coincident_uns_reference_delta(string before, string after)
{
var prev = Compose("Speed", displayOverride: null, tagConfig: before);
var next = Compose("Speed", displayOverride: null, tagConfig: after);
var plan = AddressSpacePlanner.Compute(prev, next);
// Raw side: an attribute edit on a stable RawPath ⇒ a Changed delta (not remove+add).
var rawDelta = plan.ChangedRawTags.ShouldHaveSingleItem();
rawDelta.Previous.NodeId.ShouldBe(rawDelta.Current.NodeId);
// UNS side: the SAME reference row, same NodeId + BackingRawPath — flagged Changed ONLY because the
// inherited backing historian/shape moved. This is what drives the applier's remove+recreate repair.
var unsDelta = plan.ChangedUnsReferenceVariables.ShouldHaveSingleItem();
unsDelta.Previous.UnsTagReferenceId.ShouldBe("ref-1");
unsDelta.Previous.NodeId.ShouldBe(unsDelta.Current.NodeId);
unsDelta.Previous.BackingRawPath.ShouldBe(unsDelta.Current.BackingRawPath);
unsDelta.Previous.ShouldNotBe(unsDelta.Current);
// And the inherited values on the CURRENT side match the backing tag's new intent, so the recreate
// materialises the mirror from the same truth the raw node was updated with.
unsDelta.Current.BackingIsHistorized.ShouldBe(rawDelta.Current.IsHistorized);
unsDelta.Current.BackingHistorianTagname.ShouldBe(rawDelta.Current.HistorianTagname);
unsDelta.Current.BackingIsArray.ShouldBe(rawDelta.Current.IsArray);
unsDelta.Current.BackingArrayLength.ShouldBe(rawDelta.Current.ArrayLength);
// Neither realm sees a structural add/remove — this stays a surgical, subscription-preserving deploy.
plan.AddedRawTags.ShouldBeEmpty();
plan.RemovedRawTags.ShouldBeEmpty();
plan.AddedUnsReferenceVariables.ShouldBeEmpty();
plan.RemovedUnsReferenceVariables.ShouldBeEmpty();
}
/// <summary>Identical compositions diff to an empty plan across both realms (fresh list instances must
/// not spuriously flag RawTags/UnsReferenceVariables as changed).</summary>
[Fact]