fix(scripting): #1104 a jagged-array script result is refused identically in both runners

int[][] / byte[][] used to be the one result divergence left after #1100: in process
ScriptResultMaterializer passed it (SdkValueBoundary.FromClr carries it as an array of Variants),
while the worker's wire has no nested-array tag and answered "unsupported type". A script-fed node
has a scalar DataType, so a Variant-of-arrays value is not what it declares either; not documented as
a feature anywhere (only noted as the known divergence). Decision: refuse, via the shared rule.

- IsPublishable: a 1-D array's element must be a PublishableScalarTypes scalar (no recursion).
- EnsurePublishable: a nested array gets its own reason, still "... which an OPC UA node cannot
  carry: an array of arrays ...", with a flatten hint (SelectMany). Same "script threw: ..." text in
  both runners.
- SdkValueBoundaryScriptResultTests: int[][] leaves the rule==FromClr theory; a separate fact pins
  the one deliberate difference (FromClr takes it, the rule refuses).
- ParityTests: "jagged array" + "jagged byte array" rows. ShadowModeTests census: the jagged script
  now MATCHES; the unsupported_type mismatch is exercised by a decimal INPUT (design §14 #11), the
  real remaining worker-only refusal.
- ContainmentTests.Unsupported_input_and_result_types: jagged now expects the materializer reason.

Mutation: restoring the recursive element check fails 4 tests (2 parity rows, shadow census,
containment unsupported-type).

Behaviour change: a value script returning a jagged array now fails (Bad + script log) in process
too, instead of publishing a Variant array onto a scalar-typed node.
This commit is contained in:
Joseph Doherty
2026-12-04 20:31:25 -05:00
parent ce0133b03c
commit d81990bcbf
7 changed files with 76 additions and 17 deletions
@@ -21,7 +21,7 @@ public sealed class SdkValueBoundaryScriptResultTests
{ "int", 1 }, { "uint", 1u }, { "long", 1L }, { "ulong", 1UL }, { "float", 1f }, { "double", 1.0 },
{ "string", "s" }, { "DateTime", DateTime.UtcNow }, { "Guid", Guid.NewGuid() },
{ "int[]", new[] { 1 } }, { "byte[]", new byte[] { 1 } }, { "string[]", new[] { "a", null! } },
{ "DateTime[]", new[] { DateTime.UtcNow } }, { "Guid[]", new[] { Guid.NewGuid() } }, { "int[][]", new[] { new[] { 1 } } },
{ "DateTime[]", new[] { DateTime.UtcNow } }, { "Guid[]", new[] { Guid.NewGuid() } },
// not publishable
{ "decimal", 1.5m }, { "decimal[]", new[] { 1.5m } }, { "enum", DayOfWeek.Monday }, { "enum[]", new[] { DayOfWeek.Monday } },
{ "TimeSpan", TimeSpan.FromSeconds(1) }, { "char", 'a' }, { "char[]", new[] { 'a' } },
@@ -48,6 +48,20 @@ public sealed class SdkValueBoundaryScriptResultTests
publishable.ShouldBe(taken, name);
}
/// <summary>
/// lmxopcua#1104: the one deliberate difference — the rule is STRICTER than <see cref="SdkValueBoundary.FromClr"/>
/// for an array of arrays. FromClr takes it (an array of Variants), but a script-fed node's DataType is a scalar
/// type, so that value is not what the node declares, and the worker's wire cannot carry it; both runners refuse
/// it. Pinned here so a FromClr change that stopped taking it shows this exception can go.
/// </summary>
[Fact]
public void A_jagged_array_is_refused_by_the_rule_although_the_boundary_would_take_it()
{
var jagged = new[] { new[] { 1 } };
Should.NotThrow(() => SdkValueBoundary.FromClr(jagged));
ScriptResultMaterializer.IsPublishable(jagged.GetType()).ShouldBeFalse();
}
[Fact]
public void Every_publishable_scalar_type_and_its_array_reach_the_publish_path()
{
@@ -123,6 +123,10 @@ public sealed class ParityTests
{ "time span", "return System.TimeSpan.FromSeconds(1);" },
{ "char array", "return new[] { 'a' };" },
{ "two-dimensional array", "return new double[1, 1];" },
// lmxopcua#1104: an array of arrays — in process it used to publish (as an array of Variants) while the worker
// answered "unsupported type"; both now refuse it with the same flatten hint.
{ "jagged array", "return new[] { new[] { 1, 2 }, new[] { 3 } };" },
{ "jagged byte array", "return new[] { new byte[] { 1 } };" },
};
[Theory]
@@ -102,18 +102,28 @@ public sealed class ShadowModeTests
["quality"] = "return ctx.GetTag(\"i\").StatusCode;",
["throws"] = "if ((int)ctx.GetTag(\"i\").Value >= 0) throw new InvalidOperationException(\"boom \" + ctx.GetTag(\"i\").Value); return 0;",
["divergent"] = $"return {InWorker} ? 2 : 1;",
// lmxopcua#1100: a decimal is refused by both runners alike now (no OPC UA node can carry it), so it MATCHES;
// the one result shape left that only the worker refuses is a jagged array (no nested-array wire tag).
// lmxopcua#1100: a decimal RESULT is refused by both runners alike (no OPC UA node can carry it), so it MATCHES;
// lmxopcua#1104: so is a jagged array now (it used to be the one result only the worker refused).
["decimal"] = "return 1.5m;",
["jagged"] = "return new[] { new[] { (int)ctx.GetTag(\"i\").Value } };",
// What only the worker refuses is an INPUT with no wire tag (design §14 #11): in process a decimal input is
// read and converted, the worker answers "unsupported type". Fed only to this script (see InputsFor).
["decimalInput"] = "return Convert.ToDouble(ctx.GetTag(\"m\").Value);",
};
Dictionary<string, DataValueSnapshot> InputsFor(string key, int i)
{
var inputs = Inputs(i);
if (key == "decimalInput")
inputs["m"] = new DataValueSnapshot(1.5m + i, 0u, DateTime.UtcNow, DateTime.UtcNow);
return inputs;
}
for (var i = 0; i < 3; i++)
{
foreach (var (key, source) in scripts)
{
var published = await shadowed.EvaluateAsync(key, source, Inputs(i), Ct);
var expected = await reference.EvaluateAsync(key, source, Inputs(i), Ct);
var published = await shadowed.EvaluateAsync(key, source, InputsFor(key, i), Ct);
var expected = await reference.EvaluateAsync(key, source, InputsFor(key, i), Ct);
Same(published, expected).ShouldBeTrue($"{key}: Shadow publishes exactly what InProcess publishes ({published} vs {expected})");
}
(await shadow.WaitIdleAsync(TimeSpan.FromSeconds(60))).ShouldBeTrue();
@@ -125,12 +135,12 @@ public sealed class ShadowModeTests
stats.Compared.ShouldBe(scripts.Count * 3);
stats.Mismatched.ShouldBe(6, string.Join(Environment.NewLine, shadow.Mismatches.Select(m => $"{m.Key}: {m.LastReason} — {m.LastSummary}")));
var mismatches = shadow.Mismatches.ToDictionary(m => m.Key);
mismatches.Keys.ShouldBe(["jagged", "divergent"], ignoreOrder: true);
mismatches.Keys.ShouldBe(["decimalInput", "divergent"], ignoreOrder: true);
mismatches["divergent"].LastReason.ShouldBe("value");
mismatches["divergent"].LastSummary.ShouldBe("published Ok Int32 1; shadow Ok Int32 2");
mismatches["divergent"].Count.ShouldBe(3);
mismatches["jagged"].LastReason.ShouldBe("unsupported_type");
mismatches["jagged"].LastSummary.ShouldContain("System.Int32[][]");
mismatches["decimalInput"].LastReason.ShouldBe("unsupported_type");
mismatches["decimalInput"].LastSummary.ShouldContain("System.Decimal");
var lines = log.Events.Where(e => e.MessageTemplate.Text.StartsWith("Script shadow mismatch", StringComparison.Ordinal)).ToArray();
lines.Length.ShouldBe(2, "one line per script and reason");