feat(scripting): batch submit, exact per-item deadlines, scripted-alarm seam on the out-of-process runner (lmxopcua#780)
v2-ci / unit-tests (push) Waiting to run

- IScriptRunner.RunBatch: one EvalBatch frame per worker for every evaluation ready to run; per-item, streamed results.
  The default (InProcessScriptRunner) is RunAsync per item. Used by the Calculation pump (dequeue all, split at a
  repeated tag, publish in dequeue order), the VirtualTag seam (ScriptBatchCoalescer, no timer) and the scripted-alarm
  engine (whole dirty set). In process the pump and the engine stay one at a time, unchanged.
- Exact per-item deadlines: the worker hands work to an idle evaluation thread or defers it (EvalResults
  deferred_eval_ids); the host stops that run's clock and restarts T then G when it starts (started_eval_ids). A run
  queued behind a runaway is neither timed out nor struck and is a victim, not the culprit, at the kill.
- Scripted-alarm seam behind Scripting:Runner:Seams; unset Seams now means VirtualTag + Calculation, so alarms run out of
  process only when listed. Load still compiles each predicate (to PE bytes only) so a broken one fails the load; passes
  batch and apply in the serial order under the gate. Script faults freeze the condition BadInternalError; runner faults
  freeze it BadResourceUnavailable and re-evaluate on recovery; a dead worker never clears an alarm. IScriptRunnerProvider
  carries the runner to DriverHostActor.
- Perf harness: batch throughput (RunBatch vs RunAsync x n) and alarm-pass latency (in process / batched / serial);
  design §12.3 records macOS + windev. On windev the serial out-of-process alarm pass misses 250 ms from ~500 alarms;
  batched p99 <= 53 ms at 1,000.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-10 06:37:36 -05:00
parent 11a5c1d4e2
commit 0796928504
33 changed files with 2168 additions and 117 deletions
@@ -640,7 +640,7 @@ per-shard state, restarts in the last hour and the quarantine list. Quarantines
| Key | Default | Meaning |
|---|---|---|
| `Scripting:Runner` | `InProcess` | `InProcess` \| `OutOfProcess` \| `Shadow` |
| `Scripting:Runner:Seams` | all three | Optional per-seam subset for `OutOfProcess` / `Shadow` (e.g. `["Calculation"]`) |
| `Scripting:Runner:Seams` | VirtualTag + Calculation | Optional per-seam subset for `OutOfProcess` / `Shadow` (e.g. `["Calculation"]`). Unset selects VirtualTag and Calculation; `ScriptedAlarm` only when listed (§14 #10, as built in §12.3) |
| `Scripting:Worker:Shards` | `2` | 1–8 |
| `Scripting:Worker:HardGrace` | `max(1s, T)` | §5.1 |
| `Scripting:Worker:HeapLimitBytes` | `536870912` | §5.2 |
@@ -660,6 +660,9 @@ per-shard state, restarts in the last hour and the quarantine list. Quarantines
`VirtualTagActor`), so the dispatcher-thread blocking is gone. The Calc pump and the alarm engine still run one
script at a time; why that was not relaxed in-process is recorded in `docs/VirtualTags.md` §"Upstream-tag feed".
`IScriptRunner` and batch-submit remain to do.
- **Batch submit landed in lmxopcua#780 (fourth P1 slice, §12.3):** `IScriptRunner.RunBatch`, used by the Calc pump, the
VirtualTag seam (a coalescer) and the scripted-alarm engine on the out-of-process path. In process the pump and the
engine stay one at a time, unchanged.
2. **P1, worker + protocol behind the flag.**
- Ship `ScriptWorker`, `OutOfProcessScriptRunner` and the PE cache.
- Default stays `InProcess`. Enable on the docker-dev rig only.
@@ -686,6 +689,11 @@ per-shard state, restarts in the last hour and the quarantine list. Quarantines
§10 gauges and histograms, the degraded-but-ready `script-runner` health check (§14 #12), and the orphan sweep by
launch marker (pid + start time + image, host gone). Still open: self-contained worker publish, installer (account,
pipe ACL, firewall), docker-dev live drill, scripted-alarm seam, the §4.5 batch submit, the §8 perf harness.
- **Fourth P1 slice (lmxopcua#780, record in §12.3):** the §4.5 batch submit (`IScriptRunner.RunBatch`, one `EvalBatch`
frame per worker; Calc pump, VT coalescer, alarm dirty set), exact per-item deadlines for work queued in the worker
(deferral / start notices), and the scripted-alarm seam behind `Scripting:Runner:Seams` — alarms run out of process
only when listed. Still open: installer (account, pipe ACL, firewall, PeCacheDir ACL), #789's remaining misses, then
P2 Shadow.
- **Third P1 slice (lmxopcua#780, record in §12.1):** a self-contained worker publish for self-contained (Windows)
Host publishes, started through its apphost; the docker-dev live drill (`docker-dev/docker-compose.script-runner.yml`);
the §8 perf harness (`Category=Perf`, opt-in), which passes the accepted 5k evals/s/node gate; and two runner fixes
@@ -963,6 +971,133 @@ Additional measurements:
- Tests: `A_shard_that_cannot_start_logs_one_error_then_concise_rate_limited_retries_then_its_recovery`, with two
intervals. Red when every failure is logged at Error with the exception.
### 12.3 Fourth P1 slice: batch submit, exact per-item deadlines, the scripted-alarm seam (2026-11-10)
**Batch submit (§4.5, P0).**
- `IScriptRunner.RunBatch(invocations)` returns one task per item. Results are per item and streamed.
- The default implementation is `RunAsync` per item, started together. `InProcessScriptRunner` uses it.
- `OutOfProcessScriptRunner` sends every item that is ready to run as ONE `EvalBatch` frame per worker. Ready means
admitted, not quarantined, not a suspect, and already loaded in a live worker at this source and slot order.
- Anything else goes through the full `RunAsync` path on its own: a first evaluation that needs a compile, a worker
that is down, or a suspect bound for the one-at-a-time isolation worker. A batch therefore never waits for a load
or a restart as a whole.
- Seams that use it, on the out-of-process path only:
- **Calculation pump.** It dequeues everything pending (each entry's dependency snapshot was taken at enqueue),
submits one batch, and publishes in dequeue order. A tag queued twice ends the batch, so every queued evaluation
still runs, in order, against the snapshot that triggered it. The published sequence is the one-at-a-time pump's.
- **VirtualTag seam.** There is one actor per tag, so an upstream burst makes many tags ready at once from different
threads. `ScriptBatchCoalescer` hands those to one `RunBatch`. It has no timer: an evaluation on an idle seam is
submitted at once by its own caller.
- **Scripted alarms.** See below.
- In process, the Calc pump and the alarm engine stay one at a time, unchanged. The VirtualTag in-process path does
not touch the coalescer.
**Per-item deadlines and blame stay exact under batching.**
- The host arms T and T + G at submit. That is when the run starts, provided the worker has an idle evaluation thread.
- A batch larger than the worker's scheduler, or work queued behind a runaway, used to wait in the worker with its
clock already running. It could then be timed out, take timeout strikes, and even be the key blamed at T + G.
- The worker's scheduler now hands an evaluation straight to an idle thread, or else puts it in a backlog and reports
it in `EvalResults.deferred_eval_ids`.
- The host stops that evaluation's clock.
- When a thread picks the evaluation up, its id rides back in `started_eval_ids` on the result frame of the run that
thread just finished. The host then restarts the full T, then G, from the start.
- The two notices are unordered on the wire, and the host handles either order.
- The deadlines are absolute timestamps that the timer re-checks, so a stale tick never fires early.
- A run queued behind a runaway is therefore neither timed out nor struck. At the kill it is a victim
(`script worker restarted`, `BadResourceUnavailable`, re-evaluated on return), not the culprit.
- Tests:
- `A_runaway_in_a_batch_is_blamed_alone_and_siblings_queued_behind_it_are_not_timed_out_or_struck` runs one evaluation
thread and a batch of a runaway plus three siblings. It is red with the host ignoring the deferral, and red with the
worker never deferring.
- `A_deferred_run_is_timed_and_killed_from_when_it_starts` is the positive control. It is red with a start notice that
does not re-arm the clock.
- This also changes plain `RunAsync` under saturation: a run that waits for a thread no longer times out on its wait.
That matches the in-process guard, whose budget clock starts when the pool picks the run up (§1.2).
**Scripted-alarm seam (★10: alarms last).**
- `Scripting:Runner:Seams` unset now means VirtualTag + Calculation. **`ScriptedAlarm` runs out of process only when it
is listed.**
- `ScriptRunnerSelection` serves it. The Runtime's `DriverHostActor` gets it through the new `IScriptRunnerProvider`
and passes it to the engine (a new last parameter, `scriptedAlarmRunner`).
- Engine changes, all on the runner path only:
- `LoadAsync` compiles each predicate to PE bytes only (`CompileToImage`). A broken predicate still fails the load
exactly as in process, and no assembly is loaded into the host. Deviation: Roslyn still runs in the host at load
for alarms; the worker compiles again for execution.
- Startup recovery and every re-evaluation pass snapshot each dirty alarm's inputs under `_evalGate`, submit all
their predicates as one batch, and then apply the results in the serial order under the gate: Part 9 transition,
persist before update, emissions. A test shows the transition sequence is identical to an in-process engine's
for the same pushes.
- Failure semantics (§9, alarm column):
- Timed out, killed, refused or quarantined: the condition is frozen with a `BadInternalError` annotation, as today.
- The runner's fault (worker restarted, none ready): frozen with **`BadResourceUnavailable`**, consistent with
VT/Calc. The alarm is re-evaluated by itself when the runner's `Recovery` completes.
- A predicate that threw keeps its state with no annotation, as in process.
- No outcome but a completed `bool` reaches the state machine, so a dead worker never clears an alarm.
- Real-worker tests:
- `A_runaway_predicate_is_killed_and_quarantined_its_alarm_frozen_Bad_and_a_sibling_keeps_evaluating`;
- `A_worker_that_dies_under_a_predicate_freezes_it_BadResourceUnavailable_never_clears_it_and_it_is_re_evaluated_on_return`.
It is red when the runner's failure is read as `false`.
- Budget note: since lmxopcua#779 the host passes `Scripting:RunTimeout` (2 s) to the engine. The passes below are
still judged against the stricter 250 ms the design was written for.
**Measured (§8 harness, `Batch_throughput_and_alarm_pass_latency`; macOS Arm64, 14 CPUs, load average about 14 from
other work on the box, 10 s phases):**
| Batch size | RunBatch evals/s | RunAsync ×n evals/s | RunBatch p50 / p99 µs | RunAsync ×n p50 / p99 µs |
|---:|---:|---:|---:|---:|
| 1 | 38,296 | 40,980 | 24 / 41 | 23 / 35 |
| 10 | 147,035 | 118,516 | 57 / 121 | 70 / 142 |
| 100 | 369,304 | 232,967 | 215 / 1,095 | 363 / 1,341 |
| 500 | 463,033 | 256,232 | 818 / 2,541 | 1,701 / 3,178 |
(OutOfProcess K = 2, one batch at a time. The in-process rows show no difference between the two, as expected.)
| Alarms per pass | InProcess p50 / p99 ms | OutOfProcess batched p50 / p99 ms | OutOfProcess serial p50 / p99 ms |
|---:|---:|---:|---:|
| 100 | 0.4 / 1.5 | 0.4 / 2.1 | 3.6 / 4.9 |
| 500 | 2.2 / 4.7 | 1.7 / 4.8 | 16.1 / 18.0 |
| 1,000 | 4.4 / 7.7 | 2.5 / 41.0 | 28.1 / 33.4 |
- A pass is push → the last of the N transitions it emits. "Serial" replays the pre-batch one-predicate-at-a-time pass
over the same runner. That is the N × round-trip cost §8.2 warned about: about 30 µs per alarm, against 2–4 µs
batched.
- Every configuration is far inside 250 ms. The one 41 ms p99 is a single loaded-box outlier.
- The §8.2 harness rerun on the same box after the scheduler change:
- K = 2 closed loop ×64: 366k evals/s;
- paced 10k/s: +5 % host CPU vs InProcess (budget ≤ 25 %);
- idle p50: 25 µs;
- kill latency: 14–19 ms;
- warm-cache restart of 1,000 scripts: 220 ms;
- every result correct.
**windev** (Windows VM, 36 CPUs, .NET 10.0.8; the full `ScriptWorker.Tests` suite also passes there, 157/157):
| Batch size | RunBatch evals/s | RunAsync ×n evals/s | RunBatch host CPU | RunAsync ×n host CPU |
|---:|---:|---:|---:|---:|
| 1 | 1,810 | 1,921 | 63 % | 65 % |
| 10 | 13,013 | 11,220 | 109 % | 157 % |
| 100 | 48,685 | 40,699 | 129 % | 224 % |
| 500 | 86,322 | 83,116 | 143 % | 289 % |
| Alarms per pass | InProcess p50 / p99 ms | OutOfProcess batched p50 / p99 ms | OutOfProcess serial p50 / p99 ms |
|---:|---:|---:|---:|
| 100 | 8.2 / 39.0 | 2.9 / 5.1 | 51.7 / 64.8 |
| 500 | 42.3 / 53.3 | 7.4 / 35.7 | **236.5 / 273.3 (MISS)** |
| 1,000 | 36.6 / 84.2 | 11.7 / 52.7 | **466.8 / 519.4 (MISS)** |
- On the VM, where one round trip costs about 0.5 ms, the pre-batch serial pass **misses the 250 ms budget from about
500 alarms**. The batched pass stays at p99 ≤ 53 ms for 1,000 alarms, below the in-process engine's own figures.
That is the measurement behind §8's "batch submit is a prerequisite, not an optimisation", and the reason ★10 put
alarms last.
- On Windows, batching halves the host CPU of a 100–500 evaluation burst. That is the overlapped-I/O path that is still
#789's main miss.
- No predicate failed to complete in any pass on either host.
- Rerun of the §8.2 harness on windev:
- K = 2 closed loop ×64: 50k evals/s, gate PASS;
- paced 10k/s: +99 % host CPU vs InProcess (#789, unchanged in kind);
- kill latency: 9–19 ms;
- warm-cache restart of 1,000 scripts: 1.8 s.
---
## 13. Test plan