Merge scripting follow-ups: one shared compile rule for editor + gate, CS0161/injection now flagged in the editor (#1063), worker applies the result materialisation cap (#1064), {{equip}} in Calculation scripts refused at gate + editor (#1065)

This commit is contained in:
Joseph Doherty
2026-12-03 16:49:40 -05:00
24 changed files with 729 additions and 191 deletions
+50 -23
View File
@@ -100,11 +100,14 @@ debounced (~500 ms) to avoid firing on every keystroke.
The analysis seam is the private `Analyze(userSource)` method, which:
1. Builds the wrapped document — the user's source is appended after the
OtOpcUa evaluator preamble (usings from `ScriptSandbox.Build`, the
runtime's own wrapper text, `ScriptEvaluator<TContext, TResult>.WrapperPreamble`
(usings from `ScriptSandbox.Build`, the
`CompiledScript.Run(ScriptGlobals<VirtualTagContext>)` wrapper, `var ctx =
globals.ctx;`, then `#line 1` so Roslyn reports user-source coordinates for
diagnostics). Return type is `object` so one shared script is valid regardless
of any virtual tag's `DataType`.
diagnostics), and closed with `WrapperSuffix`. Since #1063 the editor holds no
copy of the wrapper: it is the exact text the runtime compile and the deploy
gate parse. Return type is `object` so one shared script is valid regardless
of any virtual tag's `DataType` (`bool` in predicate mode).
2. Parses the wrapped document as a `CSharpSyntaxTree` and creates a
`CSharpCompilation` against the `ScriptSandbox` reference set.
3. Returns the `SemanticModel`, `SyntaxTree`, and the preamble byte offset (used
@@ -145,19 +148,27 @@ The diagnostics endpoint combines three sources and returns **errors only**:
### 1. Roslyn compile diagnostics
`compilation.GetDiagnostics()` filtered to `DiagnosticSeverity.Error`. Warnings
are intentionally excluded: the canonical passthrough pattern
What refuses a script is decided in ONE place, `ScriptCompileRules`
(`Core.Scripting`), which the runtime compile (`ScriptEvaluator`, and so the
deploy gate's `RoslynDeployScriptChecker`) and the editor both call (#1063):
`CompileErrors` — every `DiagnosticSeverity.Error` diagnostic, wherever Roslyn
locates it — and `WrapperInjection` (`LMX001`/`LMX002`, Core.Scripting-013).
`SkipsCompile` is the shared shortcut: a blank or passthrough value script never
reaches the compiler at runtime, so the editor does not compile it either.
Warnings are intentionally excluded: the canonical passthrough pattern
`return ctx.GetTag("X").Value;` triggers nullable warning CS8605 under
`NullableContextOptions.Enable` but compiles and publishes fine — flagging it in
the editor would be misleading noise that does not reflect publish reality.
Line numbers are read from `GetMappedLineSpan()` (not `GetLineSpan()`).
`GetMappedLineSpan` honours the `#line 1` directive in the preamble, so it
returns user-source coordinates directly. Diagnostics whose
`SourceSpan.Start < preambleLength` are dropped — they are phantom wrapper
diagnostics (e.g. `CS0161` "not all code paths return a value" on the synthesized
`Run` method body when the user has not yet typed `return`) and are not the
user's fault.
returns user-source coordinates directly. A refusal located in the wrapper
rather than the user's text — notably `CS0161` "not all code paths return a
value", which Roslyn pins on the synthesized `Run` — is drawn on the script's
**last line** with a plain-language message. Until #1063 such diagnostics were
dropped as "phantom", so a script with a path that falls off the end was clean in
the editor and refused at deploy. `EditorGateParityTests` now runs one corpus
through the editor and the gate in all three modes and requires the same verdict.
### 2. ForbiddenTypeAnalyzer
@@ -192,15 +203,19 @@ editor showed can no longer be deployed silently. Two differences from the edito
to know: the gate compiles each script **as the seam that uses it** (a predicate
must return `bool`), and it only checks scripts something enabled uses. The editor
matches the gate's seam since #1025: the analysis endpoints take an optional `mode`
(`value` default | `alarmPredicate`), the editor wraps the source as
(`value` default | `calculation` | `alarmPredicate`), the editor wraps the source as
`bool Run(ScriptGlobals<AlarmPredicateContext>)` in predicate mode, and the ScriptEdit
page has an **Analyse as** selector that defaults to *predicate* when scripted alarms
are the script's only users (a script also used as a VirtualTag/Calculation defaults to
value; pick whichever seam you are authoring for). The per-equipment VirtualTag and
Calculation editors are always value mode. Known remaining gap: a body that lacks a
`return` on some path (CS0161) is not flagged while you type (a deliberate
no-phantom-marker rule) but is refused at deploy. See `docs/VirtualTags.md`
§"Deploy-time script compile gate" for modes, caching and the upgrade note.
page has an **Analyse as** selector. It defaults to *Calculation* when any Calculation
tag runs the script (#1065 — Calculation mode is the value shape plus the refusal of
`{{equip}}`, so it shows every refusal a value use can meet), to *predicate* when
scripted alarms are the script's only users, and to *value* otherwise; pick whichever
seam you are authoring for. The per-equipment VirtualTag editor is value mode and the
Calculation tag editor is Calculation mode. A body that lacks a `return` on some path
(CS0161) is flagged while you type since #1063 (it used to be filtered as a "phantom"
wrapper diagnostic while the gate refused it). Known remaining divergence: a dynamic
tag path (`OTSCRIPT_DYNPATH`) is an editor error in every mode but a gate refusal only
for a predicate. See `docs/VirtualTags.md` §"Deploy-time script compile gate" for
modes, caching and the upgrade note.
### Runtime safety (what happens after publish)
@@ -251,7 +266,9 @@ scripted-alarm predicates) runs scripts through the shared `ScriptRunGuard`:
Arrays, lists, strings and scalars are unchanged. **Still in-process-only limits** (the boundary is
`Scripting:Runner=OutOfProcess`): memory exhaustion inside a run (the sample is after the fact), a
non-terminating loop (an orphan thread, then quarantine) and the same two inside an enumerated
sequence. See `docs/VirtualTags.md` §"Known resource limits (accepted trade-offs)".
sequence. See `docs/VirtualTags.md` §"Known resource limits (accepted trade-offs)". The out-of-process
worker applies the same rule with the same cap and the same failure text (#1064), so a script behaves
identically under `Scripting:Runner=InProcess` and `OutOfProcess`.
- **Out of process (opt-in, lmxopcua#780).** `Scripting:Runner=OutOfProcess` moves the
virtual-tag and Calculation seams' compiled runs — and the scripted-alarm predicates,
when `Scripting:Runner:Seams` lists `ScriptedAlarm` — into `ScriptWorker` processes, where a
@@ -298,8 +315,10 @@ scripted-alarm predicates) runs scripts through the shared `ScriptRunGuard`:
`ctx.SetVirtualTag` call whose first argument is NOT a string literal (variable,
concatenation, interpolation). Each rejection carries a `TextSpan` in
user-source coordinates, emitted as code `OTSCRIPT_DYNPATH`. This is the exact
same check publish enforces — the editor shows the squiggle where the literal
must appear.
check the deploy gate (and the scripted-alarm load) enforces for a scripted-alarm
predicate — the editor shows the squiggle where the literal must appear. In value
and Calculation mode the editor marks it an error too, but the gate does not refuse
it there: a known editor-stricter divergence, excluded from `EditorGateParityTests`.
---
@@ -545,8 +564,16 @@ interpolated (`$"…"`) or raw (`"""…"""`) literal is **not substituted**, so
instead of silently reading Bad at runtime: the editor flags it (`OTSCRIPT_EQUIPLITERAL`, an error) and
the deploy gate fails the script (`equip token not substituted — …`), both through
`EquipTokenLiteralScanner`. Keep the token in a regular literal. (Only a `ctx.GetTag` /
`ctx.SetVirtualTag` path argument is scanned; a `{{equip}}` token in a Calculation script is not
substituted in any literal form — the seam does no substitution.)
`ctx.SetVirtualTag` path argument is scanned.)
**Calculation scripts may not use `{{equip}}` at all (#1065).** A Calculation tag is a raw,
device-level tag in `/raw` — it belongs to no equipment, and the Calculation seam does no
substitution — so the token in any path literal (regular, verbatim, raw or interpolated, anywhere in
the path) is refused: the deploy gate fails the Calculation use (`equip token in a Calculation script
— …`, the passthrough shape included) and the editor flags it in Calculation mode
(`OTSCRIPT_EQUIPCALC`), both through `EquipTokenLiteralScanner.FindInCalculation`. Use the literal
RawPath, or move an equipment-relative computation to a VirtualTag. A script shared by a VirtualTag
and a Calculation tag is held to the rule for its Calculation use only.
---
+3 -2
View File
File diff suppressed because one or more lines are too long
+5
View File
@@ -31,6 +31,11 @@ They are close cousins and easy to confuse:
If the same script should apply to many machines, you want a virtual tag with `{{equip}}`. If a single
derived signal belongs next to the device tags it is derived from, you want a calc tag.
A Calculation script may **not** use `{{equip}}` (#1065): a Calculation tag has no equipment, so the
token has nothing to resolve to and the Calculation seam never substitutes it. The deploy gate refuses
it (`ScriptCompileFailed` … `equip token in a Calculation script`) and the Calculation tag editor flags
it as you type (`OTSCRIPT_EQUIPCALC`). Read sources by their literal RawPath.
## Authoring a calc tag
Calc tags are authored in `/raw` like any other tag: create a `Calculation` driver, add a device under