fix(scripting): #1063 one compile-refusal authority for editor and deploy gate (ScriptCompileRules + the runtime's own wrapper text) — CS0161 and wrapper injection are now flagged in the editor; #1065 {{equip}} in a Calculation script is refused by the gate and by a new Calculation editor mode

#1063: the Monaco diagnostics dropped every Roslyn error located in the synthesized
wrapper as 'phantom', so CS0161 (not all code paths return a value, pinned on Run)
passed the editor and failed the deploy gate. ScriptCompileRules (Core.Scripting) now
states which diagnostics refuse (every Error, wherever located; LMX001/002 injection)
and which sources skip the compile (blank/passthrough value scripts); ScriptEvaluator
and the editor both use it, and the editor wraps with ScriptEvaluator.WrapperPreamble/
WrapperSuffix instead of its own copy. Wrapper-located refusals are drawn on the
script's last line. EditorGateParityTests runs one corpus through editor and gate in
all three modes.

#1065: a Calculation tag is raw/device-level with no equipment and its seam never
substitutes {{equip}}, so such a script deployed clean and read Bad forever. Refused
rather than substituted: EquipTokenLiteralScanner.FindInCalculation is run by
RoslynDeployScriptChecker for every Calculation use (before the passthrough shortcut,
outside the shared VT/Calc cache) and by the editor in the new 'calculation' mode
(Calculation tag editor; ScriptEdit defaults to it when a Calculation tag uses the
script).
This commit is contained in:
Joseph Doherty
2026-12-03 16:47:14 -05:00
parent a06dc8dd9f
commit 73f49eaf32
18 changed files with 644 additions and 186 deletions
+47 -22
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)
@@ -300,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`.
---
@@ -547,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.
---