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
@@ -178,6 +178,70 @@ public sealed class AddressSpaceComposerDualNamespaceTests
statusRef.Writable.ShouldBeFalse(); // inherited from the Read raw tag
}
/// <summary>
/// 2026-08-15 surgical-deploys review — a UNS reference also carries its backing tag's HISTORIAN
/// intent + ARRAY shape (<c>Backing{IsHistorized,HistorianTagname,IsArray,ArrayLength}</c>). They are
/// on the record so a raw-side edit to any of them makes the reference record unequal, producing the
/// coincident <c>UnsReferenceDelta</c> that repairs the mirror node; without them the UNS node kept the
/// old historian tagname / scalar shape after a surgical raw edit.
/// </summary>
[Fact]
public void Uns_reference_variables_carry_backing_historian_intent_and_array_shape()
{
var c = ComposeFullFixture();
// Backing tag historized WITH an override → both fields ride the reference verbatim.
var speedRef = c.UnsReferenceVariables.Single(v => v.UnsTagReferenceId == "ref-speed");
speedRef.BackingIsHistorized.ShouldBeTrue();
speedRef.BackingHistorianTagname.ShouldBe("speed_hist");
speedRef.BackingIsArray.ShouldBeFalse();
speedRef.BackingArrayLength.ShouldBeNull();
// Backing tag neither historized nor array → the defaults.
var statusRef = c.UnsReferenceVariables.Single(v => v.UnsTagReferenceId == "ref-status");
statusRef.BackingIsHistorized.ShouldBeFalse();
statusRef.BackingHistorianTagname.ShouldBeNull();
statusRef.BackingIsArray.ShouldBeFalse();
statusRef.BackingArrayLength.ShouldBeNull();
}
/// <summary>The array + historize-without-override branches of the same inheritance: an ARRAY backing tag
/// projects its shape onto the reference (so the UNS mirror is created as an array, and therefore
/// read-only), and a historized tag with a BLANK override leaves <c>BackingHistorianTagname</c> null — the
/// applier resolves that to the BackingRawPath, exactly as the raw side defaults to its NodeId.</summary>
[Fact]
public void Uns_reference_variables_carry_array_shape_and_blank_historian_override()
{
var driver = new DriverInstance
{
DriverInstanceId = "drv-modbus", ClusterId = "c1", RawFolderId = null,
Name = "Modbus1", DriverType = "Modbus", DriverConfig = "{}",
};
var device = new Device { DeviceId = "dev-1", DriverInstanceId = "drv-modbus", Name = "PLC-A", DeviceConfig = "{}" };
var levels = new Tag
{
TagId = "t-levels", DeviceId = "dev-1", Name = "Levels", DataType = "Int32",
AccessLevel = TagAccessLevel.ReadWrite,
TagConfig = "{\"isArray\":true,\"arrayLength\":8,\"isHistorized\":true}",
};
var area = new UnsArea { UnsAreaId = "area-1", ClusterId = "c1", Name = "filling" };
var line = new UnsLine { UnsLineId = "line-1", UnsAreaId = "area-1", Name = "line-1" };
var equip = new Equipment { EquipmentId = "eq-1", UnsLineId = "line-1", Name = "station-1", MachineCode = "STATION_001" };
var reference = new UnsTagReference { UnsTagReferenceId = "ref-levels", EquipmentId = "eq-1", TagId = "t-levels" };
var c = AddressSpaceComposer.Compose(
new[] { area }, new[] { line }, new[] { equip },
new[] { driver }, Array.Empty<ScriptedAlarm>(),
unsTagReferences: new[] { reference }, tags: new[] { levels },
rawFolders: Array.Empty<RawFolder>(), devices: new[] { device });
var levelsRef = c.UnsReferenceVariables.ShouldHaveSingleItem();
levelsRef.BackingIsArray.ShouldBeTrue();
levelsRef.BackingArrayLength.ShouldBe(8u);
levelsRef.BackingIsHistorized.ShouldBeTrue();
levelsRef.BackingHistorianTagname.ShouldBeNull(); // blank override ⇒ resolved to BackingRawPath later
}
/// <summary>A composition with no raw/UNS inputs leaves all three new subtree sets empty (the legacy
/// convenience overloads + earlier callers keep working).</summary>
[Fact]