feat(scripting): Scripting:Runner=Shadow — in-process results published, each compiled run mirrored to the ScriptWorker pool and compared (lmxopcua#780 P2)

- ShadowScriptRunner (Core.Scripting): IScriptShadow.Mirror copies the inputs and queues the shadow run on the
  pool; compares value (structural), CLR type, status and failure class (compile / budget / threw+message /
  unsupported_type); runner-fault shadow outcomes (Unavailable, worker busy) are not compared. Bounded:
  Scripting:Shadow:MaxInFlight (256) and one per script; drops counted (behind | key_busy).
- Metrics otopcua.script.shadow.{compared,mismatch{script},dropped,inflight}; one rate-limited Warning per
  script and reason with a diff summary (Scripting:Shadow:MismatchLogsPerMinute, re-armed at the seam's deploy).
- The VirtualTag, Calculation and scripted-alarm seams recognise the shadow and keep their in-process path
  byte-for-byte (no coalescer, one-at-a-time pump, in-process engine), mirroring after each compiled run.
- ScriptRunnerSelection builds the shadow over an OutOfProcessScriptRunner whose lines carry
  ScriptRunnerRole=Shadow; Status() is null under Shadow so readiness never sees the shadow pool; the
  script-runner health entry stays Healthy and reports the shadow counters as data.
- docker-dev/docker-compose.script-runner-shadow.yml; docs/VirtualTags.md "Shadow mode".
- Tests: comparison table, per-script metric + once-per-reason log, rate limit, hung/blocking/throwing shadow
  never reaches the caller, MaxInFlight drop accounting, input copy; real-worker divergent + unsupported-type
  scripts counted against exactly those scripts, a worker-only runaway killed/quarantined there with every
  published value intact, alarm transitions identical to in process across a kill -9 of the shadow worker.
  Mutation-proved (16 mutants, 15 red; the one survivor added an assertion).

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-10 07:18:40 -05:00
parent 0796928504
commit 03686eb5d7
17 changed files with 1477 additions and 52 deletions
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
using ZB.MOM.WW.OtOpcUa.Core.Scripting.OutOfProcess;
using ZB.MOM.WW.OtOpcUa.Host.Configuration;
@@ -16,26 +17,35 @@ public static class ScriptRunnerHealthDecision
/// ServiceLevel to the peer over a script problem. Degraded is still ready (200) but visible.
/// </summary>
/// <param name="status">The out-of-process runner's status, or null when every seam runs in process.</param>
/// <param name="shadow">
/// Under <c>Scripting:Runner=Shadow</c>: the shadow worker pool's status and the comparison counters. Always
/// <b>Healthy</b> — every published value comes from the in-process path, so nothing on the shadow side (a
/// degraded pool, an open circuit, mismatches) may move readiness. It is reported as data only.
/// </param>
/// <returns>The status, a description and the per-shard data.</returns>
public static (HealthStatus Status, string Description, IReadOnlyDictionary<string, object> Data) Evaluate(ScriptRunnerStatus? status)
public static (HealthStatus Status, string Description, IReadOnlyDictionary<string, object> Data) Evaluate(
ScriptRunnerStatus? status, (ScriptRunnerStatus Runner, ScriptShadowStats Stats)? shadow = null)
{
if (shadow is { } sh)
{
var shadowData = RunnerData(sh.Runner, "shadow.");
shadowData["mode"] = "Shadow";
shadowData["shadow.compared"] = sh.Stats.Compared;
shadowData["shadow.mismatched"] = sh.Stats.Mismatched;
shadowData["shadow.unavailable"] = sh.Stats.ShadowUnavailable;
shadowData["shadow.dropped"] = sh.Stats.Dropped;
shadowData["shadow.inFlight"] = sh.Stats.InFlight;
return (HealthStatus.Healthy,
$"Scripts run in process (Scripting:Runner=Shadow); the out-of-process shadow runner is {sh.Runner.State}: " +
$"{sh.Stats.Compared} compared, {sh.Stats.Mismatched} mismatch(es), {sh.Stats.Dropped} dropped. " +
"Shadow results are never published and never affect readiness.",
shadowData);
}
if (status is null)
return (HealthStatus.Healthy, "Scripts run in process (Scripting:Runner=InProcess).", new Dictionary<string, object>());
var data = new Dictionary<string, object>(StringComparer.Ordinal)
{
["state"] = status.State.ToString(),
["suspects"] = status.SuspectCount,
["quarantined"] = status.QuarantinedCount,
["peCache"] = status.PeCacheEnabled,
};
foreach (var shard in status.Shards)
{
data[$"shard.{shard.Label}"] = shard.Up
? $"up pid {shard.Pid}, {shard.InFlight} in flight"
: shard.DownFor is { } downFor ? $"down {downFor.TotalSeconds:F0} s ({shard.ConsecutiveFailures} failure(s))" : "not started";
}
var data = RunnerData(status, string.Empty);
var down = status.Shards.Where(s => s.DownFor is not null).Select(s => s.Label).ToArray();
return status.State switch
{
@@ -48,6 +58,24 @@ public static class ScriptRunnerHealthDecision
data),
};
}
private static Dictionary<string, object> RunnerData(ScriptRunnerStatus status, string prefix)
{
var data = new Dictionary<string, object>(StringComparer.Ordinal)
{
[$"{prefix}state"] = status.State.ToString(),
[$"{prefix}suspects"] = status.SuspectCount,
[$"{prefix}quarantined"] = status.QuarantinedCount,
[$"{prefix}peCache"] = status.PeCacheEnabled,
};
foreach (var shard in status.Shards)
{
data[$"{prefix}shard.{shard.Label}"] = shard.Up
? $"up pid {shard.Pid}, {shard.InFlight} in flight"
: shard.DownFor is { } downFor ? $"down {downFor.TotalSeconds:F0} s ({shard.ConsecutiveFailures} failure(s))" : "not started";
}
return data;
}
}
/// <summary>The script-runner readiness component: Healthy, or Degraded-but-ready (see <see cref="ScriptRunnerHealthDecision"/>).</summary>
@@ -60,7 +88,7 @@ public sealed class ScriptRunnerHealthCheck(ScriptRunnerSelection? selection) :
/// <inheritdoc />
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
var (status, description, data) = ScriptRunnerHealthDecision.Evaluate(selection?.Status());
var (status, description, data) = ScriptRunnerHealthDecision.Evaluate(selection?.Status(), selection?.ShadowStatus());
return Task.FromResult(status == HealthStatus.Healthy
? HealthCheckResult.Healthy(description, data)
: HealthCheckResult.Degraded(description, data: data));