fix(scripting): #1100 a script result is what an OPC UA node can carry, in both runners — Guid crosses the wire, unpublishable types are refused alike

Where an in-process result really ends up: VirtualTagActor -> VirtualTagHostActor -> OpcUaPublishActor ->
OtOpcUaNodeManager.WriteValueLocked -> SdkValueBoundary.FromClr (VariantHelper.TryCastFrom). Probed against
SDK 2.0.1: Guid / Guid[] become an OPC UA Guid scalar/array; KeyValuePair<,>[] (a returned Dictionary),
decimal, enum, TimeSpan, char, DateTimeOffset, tuples, Nullable<T>[] and rank>1 arrays are refused, so the
node write threw, OpcUaPublishActor logged a Warning, and the node kept its PREVIOUS value and status (a
silent freeze, possibly under Good) while the worker answered "unsupported type".

Decision, per type, by what a client can see:
- Guid (scalar + 1-D array): representable -> the wire gains an additive GUID tag (15, `bytes g = 10`,
  16 ToByteArray bytes; a wrong length is a protocol fault). Guid INPUTS cross too. protocol_version stays 1:
  the worker ships in the host's script-worker/ folder, and an unknown tag already fails closed.
- Everything FromClr refuses: refused identically in both runners by ScriptResultMaterializer, inside the run,
  with one reason ("script threw: The script returned a value of type 'KeyValuePair<Int32, Double>[]', which
  an OPC UA node cannot carry; return ..."). A lazy unpublishable sequence is refused before it runs.
  ScriptResultMaterializer.PublishableScalarTypes / IsPublishable own the set.

Pins: SdkValueBoundaryScriptResultTests (OpcUaServer.Tests, new Core.Scripting ref) checks IsPublishable ==
"FromClr takes it" over 34 values; ProtocolTests checks the wire carries every publishable type scalar + array;
ParityTests gains guid / guid array / guid list / dictionary / KVP[] / lazy KVPs / KVP / decimal / decimal[] /
enum / TimeSpan / char[] / 2-D rows, a Guid unboxing row, and a positive+negative control. Mutations: with the
type check disabled 11 parity tests fail; with Guid not encoded 9 protocol/parity tests fail.

Behaviour change: a value script returning a Dictionary, decimal, enum, TimeSpan, char or tuple now fails
(Bad + script log) in process too, instead of "succeeding" and leaving its node frozen. Remaining divergence:
a jagged array (publish path carries it as Variant[], worker answers "unsupported type") — follow-up.
This commit is contained in:
Joseph Doherty
2026-12-04 17:27:22 -05:00
parent 50a2acb2be
commit 9dbccf231e
11 changed files with 310 additions and 12 deletions
@@ -79,6 +79,53 @@ public sealed class ScriptResultMaterializerTests
.Message.ShouldContain("no IEnumerable<T> element type");
}
/// <summary>
/// lmxopcua#1100: a result the OPC UA publish path cannot represent is refused inside the run. In process it used
/// to pass and then fail in the node write (the node silently kept its previous value and status).
/// </summary>
[Fact]
public void A_value_no_OPC_UA_node_can_carry_is_refused()
{
object[] refused =
[
1.5m, DayOfWeek.Monday, TimeSpan.FromSeconds(1), 'a', DateTimeOffset.UnixEpoch, new KeyValuePair<int, int>(1, 2),
(1, 2), new[] { new KeyValuePair<int, int>(1, 2) }, new[] { 1.5m }, new double[1, 1], new Guid?[] { null },
new Dictionary<int, double> { [1] = 1.5 },
];
foreach (var value in refused)
Should.Throw<ScriptResultException>(() => ScriptResultMaterializer.Materialize(value, 100), value.GetType().Name)
.Message.ShouldContain("which an OPC UA node cannot carry");
Should.Throw<ScriptResultException>(() => ScriptResultMaterializer.Materialize(new Dictionary<string, int> { ["a"] = 1 }, 100))
.Message.ShouldStartWith("The script returned a value of type 'KeyValuePair<String, Int32>[]'");
}
[Fact]
public void An_unpublishable_lazy_sequence_is_refused_before_it_runs()
{
var ran = 0;
var lazy = Enumerable.Range(0, 3).Select(i => { ran++; return (decimal)i; });
Should.Throw<ScriptResultException>(() => ScriptResultMaterializer.Materialize(lazy, 100));
ran.ShouldBe(0);
}
[Fact]
public void Every_publishable_type_passes_scalar_and_array_positive_controls()
{
var guid = Guid.NewGuid();
ScriptResultMaterializer.Materialize(guid, 100).ShouldBe(guid);
ScriptResultMaterializer.Materialize(new[] { guid }, 100).ShouldBeOfType<Guid[]>();
ScriptResultMaterializer.Materialize(new List<Guid> { guid }, 100).ShouldBeOfType<Guid[]>().ShouldBe([guid]);
// A jagged array is publishable too (the OPC UA boundary carries it as an array of Variants).
ScriptResultMaterializer.Materialize(new[] { new[] { 1 } }, 100).ShouldBeOfType<int[][]>();
foreach (var type in ScriptResultMaterializer.PublishableScalarTypes)
{
var scalar = type == typeof(string) ? "s" : Activator.CreateInstance(type)!;
ScriptResultMaterializer.Materialize(scalar, 100).ShouldBe(scalar);
ScriptResultMaterializer.Materialize(Array.CreateInstance(type, 1), 100)!.GetType().ShouldBe(type.MakeArrayType());
}
}
[Fact]
public void An_oversized_array_or_collection_is_refused_by_the_same_cap()
{