Merge #978: one UNS leaf-name authority (modal = service = deploy gate), script-existence gate + in-use delete guard, VirtualTagHost skips invalid tags

This commit is contained in:
Joseph Doherty
2026-12-03 10:28:50 -05:00
23 changed files with 814 additions and 21 deletions
@@ -67,6 +67,18 @@ public static class V3NodeIds
// ----- Uns realm -----
/// <summary>
/// The ONE rule for a UNS NodeId segment — exactly what <see cref="Uns(string[])"/> and
/// <see cref="UnsChild"/> enforce (non-empty, no <see cref="RawPaths.Separator"/>, no leading/trailing
/// whitespace; delegated to <see cref="RawPaths.ValidateSegment"/>). Every authoring surface and deploy-gate
/// rule for a name that becomes a UNS leaf (a VirtualTag <c>Name</c>, a reference's effective name /
/// <c>DisplayNameOverride</c>) calls this, so the editor accepts exactly what the identity authority builds
/// and no name can pass authoring and the gate only to throw at apply time (lmxopcua#978, review C-1/C-4).
/// </summary>
/// <param name="segment">The candidate segment.</param>
/// <returns>A human-readable error when invalid, or <see langword="null"/> when the segment is valid.</returns>
public static string? ValidateSegment(string? segment) => RawPaths.ValidateSegment(segment);
/// <summary>
/// The UNS-namespace <c>s=</c> identifier for a folder or variable, slash-joined from
/// ordered segments (Area, then Line, then Equipment, then an optional EffectiveName).
@@ -94,7 +106,7 @@ public static class V3NodeIds
public static string UnsChild(string parentUnsPath, string childSegment)
{
ArgumentException.ThrowIfNullOrEmpty(parentUnsPath);
var error = RawPaths.ValidateSegment(childSegment);
var error = ValidateSegment(childSegment);
if (error is not null)
throw new ArgumentException($"Invalid UNS NodeId segment ('{childSegment}'): {error}", nameof(childSegment));
@@ -110,7 +122,7 @@ public static class V3NodeIds
for (var i = 0; i < list.Count; i++)
{
var error = RawPaths.ValidateSegment(list[i]);
var error = ValidateSegment(list[i]);
if (error is not null)
throw new ArgumentException($"Invalid UNS NodeId segment at index {i} ('{list[i]}'): {error}", nameof(segments));
}
@@ -30,6 +30,18 @@ public static class EquipmentReferenceMap
/// <param name="TagGroupId">The tag's owning tag-group id, or <see langword="null"/> when directly under the device.</param>
public readonly record struct TagRow(string Name, string DeviceId, string? TagGroupId);
/// <summary>
/// The effective-name rule, stated once: the <paramref name="displayNameOverride"/> when it is non-empty,
/// else the backing raw tag's <paramref name="tagName"/>. An EMPTY override counts as "no override" — the
/// rule both compose seams apply; the deploy gate used <c>??</c> and so skipped an empty-string override
/// the composer materialised under the tag name (lmxopcua#978, review C-4).
/// </summary>
/// <param name="displayNameOverride">The reference's optional override.</param>
/// <param name="tagName">The backing raw tag's Name, or <see langword="null"/> when the tag is unknown.</param>
/// <returns>The effective name, or <see langword="null"/> when neither is available.</returns>
public static string? EffectiveName(string? displayNameOverride, string? tagName) =>
string.IsNullOrEmpty(displayNameOverride) ? tagName : displayNameOverride;
/// <summary>
/// Build the reference map. For each reference (ordered by <c>UnsTagReferenceId</c>), resolve the
/// backing tag's RawPath via <paramref name="resolver"/> and key it under the effective name within
@@ -55,7 +67,7 @@ public static class EquipmentReferenceMap
{
if (string.IsNullOrEmpty(r.EquipmentId) || string.IsNullOrEmpty(r.TagId)) continue;
if (!tagsById.TryGetValue(r.TagId, out var tag)) continue;
var effectiveName = string.IsNullOrEmpty(r.DisplayNameOverride) ? tag.Name : r.DisplayNameOverride!;
var effectiveName = EffectiveName(r.DisplayNameOverride, tag.Name);
if (string.IsNullOrEmpty(effectiveName)) continue;
var rawPath = resolver.TryBuildTagPath(tag.DeviceId, tag.TagGroupId, tag.Name);
if (rawPath is null) continue; // broken chain — dropped (deploy gate rejects invalid names)
@@ -38,8 +38,10 @@ public static class DraftValidator
ValidateRawNameCharset(draft, errors);
ValidateHistorizedTagnameLength(draft, errors);
ValidateHistorianReadOnly(draft, errors);
ValidateUnsLeafNames(draft, errors);
ValidateUnsEffectiveLeafUniqueness(draft, errors);
ValidateEquipReferenceResolution(draft, errors);
ValidateScriptReferences(draft, errors);
ValidateCalculationTags(draft, errors);
ValidateVirtualTagTriggers(draft, errors);
ValidateVirtualTagDependencyCycles(draft, errors);
@@ -505,7 +507,8 @@ public static class DraftValidator
if (!v.Enabled) continue;
if (v.TimerIntervalMs is > 0) continue;
var source = scriptSourceById.TryGetValue(v.ScriptId, out var s) ? s : null;
// An unresolved script is reported elsewhere; with no source there are no reads to count.
// An unresolved script is VirtualTagScriptNotFound (ValidateScriptReferences); with no source there
// are no reads to count.
if (v.ChangeTriggered && source is null) continue;
if (v.ChangeTriggered && EquipmentScriptPaths.ExtractDependencyRefs(source).Count > 0) continue;
errors.Add(new("VirtualTagNoEffectiveTrigger",
@@ -644,8 +647,11 @@ public static class DraftValidator
}
/// <summary>Extract the <c>scriptId</c> string from a calc tag's schemaless <c>TagConfig</c> JSON. Never
/// throws — malformed/blank/non-object JSON or an absent/non-string <c>scriptId</c> yields null.</summary>
private static string? ParseScriptId(string? tagConfig)
/// throws — malformed/blank/non-object JSON or an absent/non-string <c>scriptId</c> yields null. Public so the
/// AdminUI script-delete usage check reads the same key the gate does (lmxopcua#978).</summary>
/// <param name="tagConfig">The tag's TagConfig JSON.</param>
/// <returns>The non-blank <c>scriptId</c>, or <see langword="null"/>.</returns>
public static string? ParseScriptId(string? tagConfig)
{
if (string.IsNullOrWhiteSpace(tagConfig)) return null;
try
@@ -740,6 +746,86 @@ public static class DraftValidator
}
}
/// <summary>The <c>VirtualTagNameInvalid</c> error code (lmxopcua#978).</summary>
public const string VirtualTagNameInvalidCode = "VirtualTagNameInvalid";
/// <summary>The <c>UnsEffectiveNameInvalid</c> error code (lmxopcua#978).</summary>
public const string UnsEffectiveNameInvalidCode = "UnsEffectiveNameInvalid";
/// <summary>The <c>VirtualTagScriptNotFound</c> error code (lmxopcua#978).</summary>
public const string VirtualTagScriptNotFoundCode = "VirtualTagScriptNotFound";
/// <summary>The <c>ScriptedAlarmScriptNotFound</c> error code (lmxopcua#978).</summary>
public const string ScriptedAlarmScriptNotFoundCode = "ScriptedAlarmScriptNotFound";
/// <summary>
/// lmxopcua#978 (review C-1 ≡ R-5, C-4): every name that becomes a UNS leaf NodeId segment must pass
/// <see cref="V3NodeIds.ValidateSegment"/> — the exact rule <see cref="V3NodeIds.Uns(string[])"/> throws on.
/// <list type="bullet">
/// <item>A VirtualTag's <c>Name</c> (its NodeId is <c>{EquipmentId}/{Name}</c>). Unchecked, a
/// <c>Flow/Rate</c> or <c>"Speed "</c> sealed, then threw in the address-space apply and in
/// <c>VirtualTagHostActor</c> on every node, on every later deploy.</item>
/// <item>A reference's non-empty <c>DisplayNameOverride</c> (its effective name). Unchecked, the composer
/// silently dropped the UNS variable. An empty override falls back to the raw tag name, which
/// <c>RawNameInvalid</c> already covers.</item>
/// </list>
/// The authoring surfaces (VirtualTag modal + service, the override editor) call the same method.
/// </summary>
private static void ValidateUnsLeafNames(DraftSnapshot draft, List<ValidationError> errors)
{
foreach (var v in draft.VirtualTags)
{
var error = V3NodeIds.ValidateSegment(v.Name);
if (error is not null)
errors.Add(new(VirtualTagNameInvalidCode,
$"VirtualTag '{v.VirtualTagId}' name '{v.Name}' is not a legal UNS NodeId segment: {error}",
v.VirtualTagId));
}
foreach (var r in draft.UnsTagReferences)
{
if (string.IsNullOrEmpty(r.DisplayNameOverride)) continue;
var error = V3NodeIds.ValidateSegment(r.DisplayNameOverride);
if (error is not null)
errors.Add(new(UnsEffectiveNameInvalidCode,
$"reference '{r.UnsTagReferenceId}' display-name override '{r.DisplayNameOverride}' is not a legal " +
$"UNS NodeId segment: {error}",
r.UnsTagReferenceId));
}
}
/// <summary>
/// lmxopcua#978 (review C-2): every VirtualTag's <c>ScriptId</c> and every ScriptedAlarm's
/// <c>PredicateScriptId</c> must name a script in the draft. The two columns carry no FK, a script delete
/// used to have no usage check, and each downstream layer assumed another enforced it: the composer
/// silently skipped a scripted alarm with a missing predicate (the condition vanished on a green deploy)
/// and deployed a VirtualTag with an empty expression (Bad forever). Disabled rows are included — a
/// dangling reference is a broken row whether or not it is evaluated today, and re-enabling it later must
/// not be the first time anyone hears of it.
/// </summary>
private static void ValidateScriptReferences(DraftSnapshot draft, List<ValidationError> errors)
{
var scriptIds = draft.Scripts.Select(s => s.ScriptId).ToHashSet(StringComparer.Ordinal);
foreach (var v in draft.VirtualTags)
{
if (scriptIds.Contains(v.ScriptId)) continue;
errors.Add(new(VirtualTagScriptNotFoundCode,
$"VirtualTag '{v.VirtualTagId}' references script '{v.ScriptId}', which does not exist in the draft; " +
"pick an existing script or restore the deleted one",
v.VirtualTagId));
}
foreach (var a in draft.ScriptedAlarms)
{
if (scriptIds.Contains(a.PredicateScriptId)) continue;
errors.Add(new(ScriptedAlarmScriptNotFoundCode,
$"ScriptedAlarm '{a.ScriptedAlarmId}' references predicate script '{a.PredicateScriptId}', which does " +
"not exist in the draft; pick an existing script or restore the deleted one",
a.ScriptedAlarmId));
}
}
/// <summary>v3: within an equipment, the EFFECTIVE leaf name must be unique across its UnsTagReferences
/// (DisplayNameOverride else the backing raw tag's Name), its VirtualTags (Name), and its ScriptedAlarms
/// (Name) — the three share the equipment's UNS NodeId space (<c>{EquipmentId}/{EffectiveName}</c>).
@@ -762,7 +848,8 @@ public static class DraftValidator
foreach (var r in draft.UnsTagReferences)
{
var effective = r.DisplayNameOverride ?? (tagNameById.TryGetValue(r.TagId, out var n) ? n : null);
var effective = EquipmentReferenceMap.EffectiveName(
r.DisplayNameOverride, tagNameById.TryGetValue(r.TagId, out var n) ? n : null);
Add(r.EquipmentId, effective, $"reference '{r.UnsTagReferenceId}'");
}
foreach (var v in draft.VirtualTags)
@@ -797,7 +884,8 @@ public static class DraftValidator
var refNamesByEquip = new Dictionary<string, HashSet<string>>(StringComparer.Ordinal);
foreach (var r in draft.UnsTagReferences)
{
var effective = r.DisplayNameOverride ?? (tagNameById.TryGetValue(r.TagId, out var n) ? n : null);
var effective = EquipmentReferenceMap.EffectiveName(
r.DisplayNameOverride, tagNameById.TryGetValue(r.TagId, out var n) ? n : null);
if (string.IsNullOrEmpty(effective)) continue;
if (!refNamesByEquip.TryGetValue(r.EquipmentId, out var set))
refNamesByEquip[r.EquipmentId] = set = new HashSet<string>(StringComparer.Ordinal);
@@ -38,15 +38,18 @@ public static class UnsSegment
/// ⚠️ This does <b>not</b> admit <see cref="DefaultSegment"/> — an attribute cannot express the
/// alternation and stay readable. <see cref="AttributePattern"/> is the one to put on a form;
/// <see cref="IsValid"/> is the one to call in code.
/// <para>⚠️ Anchored with <c>\A</c>/<c>\z</c>, never <c>^</c>/<c>$</c>: in .NET <c>$</c> also matches before a
/// final <c>\n</c>, so <c>"press-1\n"</c> passed — through equipment CSV import, then the deploy gate —
/// and the applier dropped the equipment folder after a green deploy (lmxopcua#978, review C-7).</para>
/// </remarks>
public const string Pattern = "^[a-z0-9-]{1,32}$";
public const string Pattern = @"\A[a-z0-9-]{1,32}\z";
/// <summary>
/// <see cref="Pattern"/> widened to admit <see cref="DefaultSegment"/> — the form of the rule to
/// put in a <c>[RegularExpression]</c> attribute, so the UI accepts exactly what the deploy gate
/// accepts and no less.
/// </summary>
public const string AttributePattern = "^([a-z0-9-]{1,32}|_default)$";
public const string AttributePattern = @"\A(?:[a-z0-9-]{1,32}|_default)\z";
/// <summary>The message shown for a rejected segment, in the UI and from the service guards.</summary>
public const string Message =