# Monaco Script Editor (Roslyn-backed IntelliSense) > **Status lives in Gitea.** Open follow-ups and known gaps noted in this doc are tracked as issues on > gitea.dohertylan.com/dohertj2/lmxopcua (label `migrated`, 2026-11-06). This doc describes the system — do not track status here; file new work as an issue. The script editor gives AdminUI users a first-class C# authoring experience for virtual-tag scripts: completions, diagnostics, hover, signature help, document formatting, and context-aware tag-path completions — all backed by the same Roslyn compiler the runtime publish gate uses. See the design doc for the original rationale and brainstorming record: [`docs/plans/2026-06-09-monaco-script-editor-design.md`](plans/2026-06-09-monaco-script-editor-design.md). --- ## Overview Virtual-tag scripts are C# expression bodies (see [VirtualTags.md](VirtualTags.md)) compiled at publish time by `ScriptEvaluator` against a restricted `ScriptSandbox`. Before this feature the AdminUI's script page used a CDN Monaco instance wired over a hidden textarea via an `eval` + `Task.Delay(50)` race — no IntelliSense, fragile in air-gapped environments, and the editor accepted code that publish would reject. The replacement: - **Vendored Monaco** served locally — no CDN, air-gap safe. - **Reusable `MonacoEditor.razor`** component wired into both the ScriptEdit page (`/scripts/{id}`) and the virtual-tag inline script panel in the UNS TagModal. - **Full IntelliSense** via a Roslyn backend that analyses the user's source inside the *exact* evaluator wrapper `ScriptEvaluator` compiles, so what the editor accepts/rejects matches publish exactly. --- ## Architecture ``` ┌──────────────────────────── AdminUI (Blazor Server) ───────────────────────────┐ │ │ │ Components/Shared/MonacoEditor.razor ◄── Value/ValueChanged, Height, │ │ │ ReadOnly, ShowToolbar, theme/Format/wrap/minimap toolbar │ │ │ (DotNetObjectReference both ways) │ │ ▼ │ │ wwwroot/js/monaco-init.js ──registers──► completion / hover / signatureHelp / │ │ │ diagnostics / format │ │ │ fetch POST /api/script-analysis/* │ │ ▼ │ │ ScriptAnalysis/ScriptAnalysisEndpoints.cs (minimal-API group, ConfigEditor) │ │ ▼ │ │ ScriptAnalysis/ScriptAnalysisService.cs ── Roslyn over OtOpcUa's real wrapper │ │ ├─ ScriptSandbox.Build(typeof(VirtualTagContext)) (imports + refs) │ │ ├─ CompiledScript.Run(ScriptGlobals) wrapper │ │ ├─ ForbiddenTypeAnalyzer (sandbox-escape squiggles) │ │ ├─ DependencyExtractor (dynamic-path squiggles) │ │ └─ IScriptTagCatalog (tag-path string-literal completions) │ │ ▼ │ │ wwwroot/lib/monaco/vs/… (vendored Monaco, loaded by monaco-init.js) │ └─────────────────────────────────────────────────────────────────────────────────┘ │ references ▼ Core.Scripting + Scripting.Abstractions (ScriptSandbox, VirtualTagContext, ScriptGlobals<>, ForbiddenTypeAnalyzer, DependencyExtractor) Configuration (OtOpcUaConfigDbContext → tag / virtual-tag paths for catalog) ``` ### MonacoEditor.razor `src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Shared/MonacoEditor.razor` A reusable Blazor Server component. Key parameters: | Parameter | Type | Notes | |-----------|------|-------| | `Value` | `string` | Two-way bound script source | | `ValueChanged` | `EventCallback` | Fires on every model change | | `Height` | `string` | CSS height (default `400px`) | | `ReadOnly` | `bool` | Disables editing; IntelliSense still works | | `ShowToolbar` | `bool` | Shows the theme/Format/wrap/minimap toolbar | The component creates the Monaco editor in `OnAfterRenderAsync`, registers a `DotNetObjectReference` so JS can call back into Blazor on value change, and uses a GUID-keyed container so multiple instances on the same page coexist safely. External `Value` changes are pushed to the editor via `window.MonacoBlazor.setValue`. ### monaco-init.js `src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/wwwroot/js/monaco-init.js` Exposes `window.MonacoBlazor` with `createEditor / setValue / getValue / setMarkers / setEditorOption / format / dispose`. Registers the four Monaco language providers (completion, hover, signature help, document formatting) for the `csharp` language at load time; each provider POSTs to the corresponding `/api/script-analysis/*` endpoint. Diagnostics are debounced (~500 ms) to avoid firing on every keystroke. ### ScriptAnalysisService `src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/ScriptAnalysis/ScriptAnalysisService.cs` The analysis seam is the private `Analyze(userSource)` method, which: 1. Builds the wrapped document — the user's source is appended after the runtime's own wrapper text, `ScriptEvaluator.WrapperPreamble` (usings from `ScriptSandbox.Build`, the `CompiledScript.Run(ScriptGlobals)` wrapper, `var ctx = globals.ctx;`, then `#line 1` so Roslyn reports user-source coordinates for 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 by every capability to map editor coordinates to wrapped-document offsets). --- ## HTTP Endpoints All five endpoints live under the `/api/script-analysis` group, registered by `ScriptAnalysisEndpoints.MapScriptAnalysisEndpoints()` (called from `EndpointRouteBuilderExtensions.AddAdminUI` / `Host/Program.cs`) and gated by the `ConfigEditor` authorization policy (`AdminUiPolicies.ConfigEditor`: Administrator or Designer). | Route | Request DTO | Response DTO | Purpose | |-------|-------------|--------------|---------| | `POST /api/script-analysis/diagnostics` | `DiagnoseRequest(Code)` | `DiagnoseResponse(Markers)` | Roslyn + ForbiddenType + dynamic-path markers (error for a predicate, warning otherwise — #1083) | | `POST /api/script-analysis/completions` | `CompletionsRequest(CodeText, Line, Column)` | `CompletionsResponse(Items)` | Scope, dot-member, and tag-path completions | | `POST /api/script-analysis/hover` | `HoverRequest(CodeText, Line, Column)` | `HoverResponse(Markdown?)` | Type + XML doc markdown | | `POST /api/script-analysis/signature-help` | `SignatureHelpRequest(CodeText, Line, Column)` | `SignatureHelpResponse(Label?, Parameters?, ActiveParameter)` | Parameter help | | `POST /api/script-analysis/format` | `FormatRequest(Code)` | `FormatResponse(Code)` | `NormalizeWhitespace()` formatting | DTO definitions live in `ScriptAnalysis/ScriptAnalysisContracts.cs`. `DiagnosticMarker` fields: `Severity` (Monaco values: 8 = error, 4 = warning, 2 = info, 1 = hint), `StartLineNumber`, `StartColumn`, `EndLineNumber`, `EndColumn`, `Message`, `Code`. `CompletionItem` fields: `Label`, `InsertText`, `Detail`, `Kind` (Monaco kind string: `"Method"`, `"Property"`, `"Field"`, `"Variable"`, `"Class"`, etc.), `InsertTextRules` (0 for plain text, 4 for snippet). --- ## Diagnostics Composition The diagnostics endpoint combines three sources and returns **errors only**: ### 1. Roslyn compile diagnostics 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. 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 `ForbiddenTypeAnalyzer.Analyze(compilation)` walks the wrapped syntax tree and checks every referenced symbol, expression type and invoked member against the sandbox **allow-list** (lmxopcua#971): the `ctx` API, primitive / time / maths value types, the collection namespaces, LINQ-to-objects (`Enumerable`), string / `StringBuilder` / `Regex`, the formatting types of `System.Globalization`, and `System.Type` / `CancellationToken` as values with a member allow-list. Anything else is refused — see `docs/VirtualTags.md` §"The sandbox is an allow-list" for the full list and the rules. Because the reference set (`ScriptSandbox.BclAssemblyNames`) is narrowed too, an API whose assembly is not referenced (XML, archives, networking, expression trees, the console, processes) shows as an ordinary **compiler** error rather than `OTSCRIPT_FORBIDDEN`, and is no longer offered by completion. The runtime publish gate uses the same analyzer and the same reference set, so an allow-list change needs a `DiagnoseTests` case to keep editor ⇔ publish parity (`Editor_and_publish_gate_agree`). Each violation is mapped via `tree.GetMappedLineSpan(span)` to user-source coordinates and emitted as an error marker with code `OTSCRIPT_FORBIDDEN`. ### The deploy gate compiles too (lmxopcua#971 follow-up) Saving a script is not compile-gated — the editor's diagnostics are advisory while you type. The **deploy** is: before a deployment is sealed, every script it will run (enabled VirtualTags, enabled scripted-alarm predicates, Calculation tags) is compiled through the same `ScriptEvaluator` pipeline the runtime uses, with `{{equip}}/` already resolved per equipment exactly as the compose seams resolve it. Under the default `Deployment:ScriptCompileGateMode=Error`, a script the runtime would refuse rejects the deploy with `ScriptCompileFailed`, naming the script, the seam it was compiled for and the entities using it — so an error the editor showed can no longer be deployed silently. Two differences from the editor 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 | `calculation` | `alarmPredicate`), the editor wraps the source as `bool Run(ScriptGlobals)` in predicate mode, and the ScriptEdit 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). A dynamic tag path (`OTSCRIPT_DYNPATH`) is an editor **error** only in predicate mode, where the gate refuses it, and a **warning** in value and Calculation mode, where it deploys (#1083 — it used to be an error in every mode). See `docs/VirtualTags.md` §"Deploy-time script compile gate" for modes, caching and the upgrade note. ### Runtime safety (what happens after publish) The editor cannot catch a script that compiles cleanly but never returns or allocates without bound, so every runtime seam (virtual tags, Calculation tags, scripted-alarm predicates) runs scripts through the shared `ScriptRunGuard`: - **Timeout + one orphan.** A run that exceeds its budget is abandoned (it cannot be pre-empted in-process) and the tag/alarm reads Bad. While that abandoned run is still executing, every further trigger is **refused** — Bad `"previous evaluation still running"`, counted on `otopcua.script.run_refused` — so a `while(true)` costs one pool thread, not one per trigger. - **Quarantine after 3 consecutive timeouts** (`Scripting:QuarantineAfterConsecutiveTimeouts`). Only a timeout is a strike: a refusal neither adds to nor resets the streak, and a run that returns (value or throw) resets it. A quarantined script is never dispatched again; it reads Bad `"script quarantined: …"`, logs one Error and increments `otopcua_script_quarantined`. - **Stuck-orphan quarantine** (in-process runner only: `Scripting:Runner=InProcess`, the default and the rollback; under `OutOfProcess` a never-returning script is killed at `T + G` and quarantined after two kills, so this rule never fires there — lmxopcua#780 P4). A never-returning script (`while(true)`) times out once and is then only refused, so the timeout streak alone never reaches 3. Instead, a refusal behind an orphan that has been running longer than `Scripting:StuckOrphanQuarantineFactor` × the timeout (default 10 × — 20 s at the 2 s default) quarantines immediately, through the same path (Error log, metric `reason=stuck`, Bad). A short overrun (a young orphan) stays a plain refusal and the script resumes once the orphan completes. - **Cooperative cancellation (`ctx.CancellationToken`).** The guard cancels the run token when a run times out, and scripts see it as `ctx.CancellationToken`. A long-running loop that polls `ctx.CancellationToken.IsCancellationRequested` (or calls `ctx.CancellationToken.ThrowIfCancellationRequested()`) ends its own abandoned run and frees the slot, instead of holding a pool thread until it returns. A script that ignores the token is still an orphan. The editor completes the member, and `CancellationToken` is on the sandbox allow-list — for polling only: `Register` and `WaitHandle` are refused (a callback or a wait outlives the evaluation). - **Allocation budget.** A single run allocating more than `Scripting:MaxAllocBytesPerRun` (64 MB) is quarantined. This is sampled after the run returns, so a run that OOMs the process first is not stopped — that needs an out-of-process runner (→ #750). - **A returned sequence is materialised inside the run, bounded (#1059).** A script can return an iterator (`yield`) or an unfinished LINQ query: nothing has run when `Run` returns, so the host used to enumerate it on its own thread — outside the timeout and the allocation sample, with no element bound. The guard now enumerates a lazy sequence into a typed array (`double[]` for an `IEnumerable`) on the run's own thread, so the work is charged to the timeout, the orphan slot and the allocation budget, and refuses one past `Scripting:MaxResultElements` (default 100 000; also caps a returned array / collection) with a failed run (Bad, script log) — not a timeout strike. A lazy sequence whose element type is `object`, an interface or another collection is refused: the host copies and compares a returned array recursively, so an open-slot array can nest or contain itself. A concrete collection (`List`, `HashSet`, `Queue`, …) gets the same treatment since #1092 — it is held to the cap by its `Count`, then copied into the same typed array (`double[]`, …), and refused for an open element type (`List`) — because the out-of-process worker's wire carries arrays but no collection type (it used to answer `unsupported type` while the in-process runner returned the list itself). Arrays, strings and scalars are unchanged, provided their type is one an OPC UA node can carry. **The result's type is checked too (#1100):** a value script may return null, a `bool`, an integer type, `float`, `double`, `string`, `DateTime` or `Guid`, or a one-dimensional array of one of them (`ScriptResultMaterializer.PublishableScalarTypes` — exactly what the server's OPC UA value boundary turns into a built-in Variant). Anything else — a `Dictionary` (it becomes `KeyValuePair[]`), a `decimal`, an enum, a `TimeSpan`, a `char`, a tuple, a 2-D array — fails the run (Bad, script log) in both runners with `script threw: The script returned a value of type '…', which an OPC UA node cannot carry; …`. ⚠️ Behaviour change: in process such a value used to "succeed" and then fail inside the node write, so the node silently kept its previous value and status (only a server-log warning); convert first (`Convert.ToDouble(x)`, or a dictionary's keys and values as two arrays). **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)". 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 run past `T + Scripting:Worker:HardGrace` is stopped by killing its worker and an OOM kills a worker instead of the server. A run whose worker died under it reads `BadResourceUnavailable` (an alarm is frozen with that annotation, never cleared). See `docs/VirtualTags.md` §"Out-of-process runner". - **Recursion.** Unbounded recursion fails the script with `ScriptStackExhaustedException` (Bad, script log) instead of overflowing the stack, which would terminate the whole server. The runtime compile inserts a stack check at the entry of the script and of every lambda, anonymous method and local function (lmxopcua#779), and guards every `catch` / `finally` so a handler that throws on every frame cannot overflow the stack while unwinding (lmxopcua#971 review); the editor does not show it and does not flag recursion, and bounded recursion still works. - **Configuration keys and their ranges.** The numeric knobs are node-wide (the `Scripting` appsettings section, shared by virtual tags, Calculation tags and scripted alarms) and are clamped when bound, with one startup Warning per clamped value: | Key | Default | Range after the clamp | |---|---|---| | `Scripting:RunTimeout` | `00:00:02` (2 s) | zero or negative becomes the default; above `00:01:00` becomes 60 s. The per-run budget of virtual tags and scripted-alarm predicates (lmxopcua#779 — alarms used to fall back to 250 ms). Calculation tags keep their per-driver `runTimeoutMs`. | | `Scripting:MaxAllocBytesPerRun` | `67108864` (64 MB) | `0` (or negative) disables the budget; a positive value below `1048576` (1 MB) becomes 1 MB | | `Scripting:MaxResultElements` | `100000` | most elements a returned array / collection / materialised sequence may hold (#1059); zero or negative becomes the default | | `Scripting:QuarantineAfterConsecutiveTimeouts` | `3` | `1`–`1000` | | `Scripting:StuckOrphanQuarantineFactor` | `10` | `0` (or negative) disables the rule; above `1000` becomes `1000`; `NaN` becomes the default. In-process runner only (lmxopcua#780 P4) | A Calculation driver may override `maxAllocBytesPerRun` and `quarantineAfterConsecutiveTimeouts` in its own driver config. Those keys are layered on the node values and clamped to the same ranges, and an out-of-range value logs a Warning naming the driver. The stuck factor has no per-driver key. - **Lifting.** A quarantine lifts when the script source changes or on redeploy, never by itself. - **Script-log rate limit.** The cluster-wide script-log fan-out admits 20 entries per source per minute; the rest are counted and replaced by one "N suppressed" summary. The summary is emitted on that source's **next** entry after the window rolls, so a source that bursts and then goes quiet shows its summary only when it logs again (the full stream is always in `scripts-*.log`). ### 3. DependencyExtractor `DependencyExtractor.Extract(code).Rejections` enumerates every `ctx.GetTag` / `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`. Which seams it refuses is decided in `ScriptCompileRules` (#1083): `RefusesNonLiteralTagPaths(kind)` is true for a scripted-alarm predicate only (the alarm load runs the extractor and refuses the alarm), and `TagPathFindings(kind, source)` returns each rejection with a `Refuses` flag. The deploy gate refuses on exactly those findings, and the editor draws them as an **error** (severity 8) where `Refuses` and a **warning** (severity 4) otherwise — so the two cannot drift. In value and Calculation mode the warning says what the path does at runtime: those seams read only their *declared* inputs (the literal `ctx.GetTag` paths, which are what gets subscribed and what re-triggers the script), so a dynamic read returns Bad (`BadNodeIdUnknown`, null value) unless the same path is also read with a literal elsewhere in the script; an empty literal path always reads Bad (and an empty `SetVirtualTag` path throws). Making the gate refuse these in value/Calculation scripts was declined — it would reject configurations that deploy today. `EditorGateParityTests` carries dynamic, concatenated, interpolated and empty paths in all three modes. --- ## Tag-Path Completion When the caret is inside the first string-literal argument of a `GetTag("…")` or `SetVirtualTag("…")` invocation, the completion provider delegates to `IScriptTagCatalog` instead of the semantic model. ### IScriptTagCatalog contract `ScriptAnalysis/IScriptTagCatalog.cs` `GetPathsAsync(string? filter, CancellationToken)` returns the distinct **runtime-resolvable keys** a script may pass to `ctx.GetTag` / `ctx.SetVirtualTag`, optionally filtered by a case-insensitive `StartsWith` prefix. The concrete implementation `ScriptTagCatalog` reads `Tag` + `VirtualTag` rows from `OtOpcUaConfigDbContext` and projects them as follows: | Tag category | Resolvable key emitted | |---|---| | Equipment driver tag (`EquipmentId != null`) | Driver `FullName` extracted from `Tag.TagConfig` JSON — the verified `DependencyMux` key | | SystemPlatform / Galaxy tag (`EquipmentId == null`) | MXAccess dot-ref: `FolderPath.Name` when a folder is set, else `Name` | | Virtual tag | Leaf `Name` (best-effort) | **Why only resolvable keys?** The live runtime resolves a `ctx.GetTag("X")` literal against `DriverInstanceActor.AttributeValueItem.FullReference`, which is the `FullName` field from `Tag.TagConfig` (see `ScriptTagCatalog.ExtractFullNameFromTagConfig` + `EquipmentNodeWalker.ExtractFullName`). The former UNS-path cascade engine (`Core.VirtualTags.VirtualTagEngine`, keyed by the slash-joined `Enterprise/Site/Area/Line/Equipment/TagName` browse path) was retired (#603) — it was never wired into the host — so UNS browse paths never resolve at runtime and are intentionally NOT offered as completions. The catalog is scoped per Blazor circuit; each call creates and disposes its own `DbContext` via the pooled factory (same pattern as `UnsTreeService`). Results are bounded to 200 entries to keep the completion list responsive on large fleets. > **v3 Batch 4 (dual namespace) — unchanged for scripts.** A `ctx.GetTag(...)` literal > still resolves by the tag's `FullName` / **RawPath** — which is precisely the identity > of the Raw-namespace node (`ns=Raw, s=`). A `{{equip}}/` token resolves > through the equipment's `UnsTagReference` to that same backing RawPath (see below), so > scripts key off the single value source regardless of the UNS projection. The dual > namespace changes only the OPC UA address-space surface, not script tag-path semantics. ### Literal gate `TryGetTagPathLiteral` identifies the tag-path context by climbing from the syntax token under the caret to: `LiteralExpressionSyntax` → `ArgumentSyntax` (first argument only) → `ArgumentListSyntax` → `InvocationExpressionSyntax` → `MemberAccessExpressionSyntax`. It then requires (a) the method name is `GetTag` or `SetVirtualTag`, and (b) the **receiver is the identifier `ctx`** — a purely syntactic check that deliberately mirrors the runtime dependency harvest (`EquipmentScriptPaths.GetTagRefRegex` is `ctx`-anchored), so the editor offers tag-path completion/hover for exactly what `AddressSpaceComposer` harvests at deploy time. An unrelated `anything.GetTag("…")` no longer triggers it. **`SetVirtualTag` is a no-op in production.** The live single-tag evaluator (`RoslynVirtualTagEvaluator`) drops cross-tag `ctx.SetVirtualTag(...)` writes (logged); the cascade engine that would have honoured them (`VirtualTagEngine`) was retired unwired (#603). So hover on a `ctx.SetVirtualTag("…")` path literal appends a note that the write is dropped in single-tag mode and will not take effect at runtime — the path completions still help author the literal, but the author is not misled into relying on a no-op. Making `SetVirtualTag` functional would require wiring the cascade engine into the host (out of scope). --- ## Vendoring / Air-gap Safety Monaco is served from `_content/ZB.MOM.WW.OtOpcUa.AdminUI/lib/monaco/vs/` (the Blazor static-asset path for the AdminUI RCL). The loader script is at `_content/ZB.MOM.WW.OtOpcUa.AdminUI/js/monaco-init.js`. No CDN is contacted at runtime. The legacy CDN loader (`monaco-loader.js`) and the `eval`/`Task.Delay` injection previously in `ScriptEdit.razor` have been removed. --- ## Wire-in Points ### ScriptEdit page (`/scripts/{id}`) `src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Pages/ScriptEdit.razor` Hosts ``. On save, SHA-256 is recomputed from the editor value and stored in `Script.SourceHash` — unchanged from the pre-Monaco save path. ### VirtualTagModal (`/uns` TagModal for virtual tags) `src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Shared/Uns/VirtualTagModal.razor` When the selected script has a `ScriptId`, an inline "Script source" panel renders `` bound to the script source. The panel shows a **"Used by N virtual tag(s)"** notice (populated from `IUnsTreeService.CountVirtualTagsUsingScriptAsync`). A dedicated **Save script** button calls `IUnsTreeService.UpdateScriptSourceAsync`, which performs a `RowVersion`-guarded update of the `Script` row — a separate concurrency unit from the virtual-tag Create/Save operation. Creating a brand-new script inline from the modal is a follow-up (not v1). Three `IUnsTreeService` methods back the inline panel: `GetScriptSourceAsync` / `CountVirtualTagsUsingScriptAsync` / `UpdateScriptSourceAsync`. --- ## How to Extend ### Adding a new completion source 1. Implement a new case in `ScriptAnalysisService.CompleteAsync`. The `Analyze(code)` seam gives you the `SemanticModel`, `SyntaxTree`, and preamble offset. 2. Add the corresponding request/response DTOs to `ScriptAnalysisContracts.cs` if a new endpoint is needed, or fold into an existing endpoint with an extra field. 3. Register the new Monaco provider in `monaco-init.js` using the same `fetch('/api/script-analysis/…', { method: 'POST', … })` pattern. ### Adding a new analysis check (diagnostics) Extend `Diagnose` in `ScriptAnalysisService`. The existing three-source pattern (Roslyn → `ForbiddenTypeAnalyzer` → `DependencyExtractor`) is additive: add a fourth source and append to `markers`. Keep severity at 8 (Monaco error) for anything that blocks publish; use 4 (warning) for advisory-only hints. ### Changing the tag-path catalog source Replace or extend the `ScriptTagCatalog` implementation of `IScriptTagCatalog`. Register the replacement in `EndpointRouteBuilderExtensions.AddAdminUI` (or inject via the DI container as the `IScriptTagCatalog` scoped service). --- ## Equipment-relative tag paths (`{{equip}}`) ### Why Each VirtualTag bound to a script would otherwise need its own near-duplicate script because tag paths are hard-coded absolute RawPaths (e.g. `Cell1/Modbus/Dev1/Speed`). The `{{equip}}/` token breaks this coupling: point many VirtualTags' `ScriptId` at a single template script, and each resolves the token through **its own equipment's tag references** at deploy time. No schema change is required — sharing a `Script` record across VirtualTags already works; `{{equip}}/` is what makes the shared script resolve per-equipment. ### Before / after **Before — one script per machine (hard-coded RawPath):** ```csharp // Script "Calc_TestMachine_001" — hard-coded, cannot reuse return ctx.GetTag("Cell1/Modbus/Dev1/Speed").Value; ``` **After — one shared template:** ```csharp // Script "Calc_Speed" — works for any machine return ctx.GetTag("{{equip}}/Speed").Value; ``` `TestMachine_001` and `TestMachine_002` both bind `ScriptId = "Calc_Speed"`, and each has a **tag reference** whose effective name is `Speed`. At deploy, each VirtualTag receives its own expanded copy — `{{equip}}/Speed` resolves to that equipment's referenced raw tag's RawPath. ### Token rules (v3) - The token joint is a **slash**: `{{equip}}/` (double-brace stem, lowercase). `` is a **reference effective name** — the reference's `DisplayNameOverride`, else the backing raw tag's `Name`. - It is substituted **only inside `ctx.GetTag(…)` / `ctx.SetVirtualTag(…)` first-argument string literals** — comments, logger strings, and other code are untouched. - The whole post-prefix literal content is the reference name, so effective names with internal spaces are supported: `ctx.GetTag("{{equip}}/Motor Speed")`. - `{{equip}}/` **resolves through the owning equipment's `UnsTagReference` rows** (by effective name) to the backing raw tag's `RawPath`. Example: a reference named `Speed` backing `Cell1/Modbus/Dev1/Speed` → the token expands to `Cell1/Modbus/Dev1/Speed`. - Scripted-alarm predicate scripts resolve `{{equip}}/` the same way; alarm **message-template** tokens follow the same resolution rule. ### Validation requirement Saving a VirtualTag (or ScriptedAlarm) whose script uses `{{equip}}/` is rejected in the AdminUI when `` is not one of the owning equipment's reference effective names. The same rule runs at the deploy gate (`DraftValidator.ValidateEquipReferenceResolution`, error code `EquipReferenceUnresolved`), which also catches a **rename-induced** breakage the authoring check never saw. The rejection names the script/alarm, the equipment, and the missing ref — so an unresolved token can never reach the deployed artifact. ### Editor support In the Monaco script editor (ScriptEdit page and the `/uns` virtual-tag modal's inline script panel): - **Hover** — hovering a `{{equip}}/` path literal shows an *"Equipment-relative path — resolved through the equipment's tag reference at deploy"* note. In **Calculation** mode it instead says the token is refused there and why — a Calculation tag is raw/device-level with no owning equipment, so nothing substitutes it and the deploy gate refuses the script (#1093, matching the `OTSCRIPT_EQUIPCALC` marker on the same span). This hover needs no tag catalog. - **Completions** — typing `{{equip}}/` inside a `GetTag`/`SetVirtualTag` literal offers the owning equipment's **reference effective names** (needs an equipment context; inert on the shared ScriptEdit page). - **Diagnostics** — an unresolved `{{equip}}/` is flagged (`OTSCRIPT_EQUIPREF`) identically to the deploy gate, preserving *editor accepts ⇔ publish accepts*. ### Maintainer note Substitution runs at the two compose seams — `AddressSpaceComposer.Compose` and `DeploymentArtifact.ParseComposition` — via the shared `ZB.MOM.WW.OtOpcUa.Commons.Types.EquipmentScriptPaths.SubstituteEquipmentToken` helper, fed the equipment's reference map (effective name → RawPath) built by `EquipmentReferenceMap` over the shared `RawPathResolver`. Substitution runs **before** dependency extraction, so the runtime, the static change-trigger dependency graph, and the literal-only path rule are unchanged: by the time they see the script, `{{equip}}/` has been replaced with a concrete RawPath and the path is a normal string literal. An unresolved ref is left un-substituted (the validator rejects it first — substitution never throws). The two seams build the reference map identically, so their plans stay byte-parity. **The paste is escaped (string-literal rule).** The path literal is a C# *regular* string literal, so its content is source text, not the string value, and a RawPath segment may legally hold `\`, `"`, braces, line breaks or non-ASCII text (`RawPaths.ValidateSegment` only forbids `/` and edge whitespace). Substitution therefore treats the literal exactly as the compiler does: - the `` is **decoded** (C# escapes resolved) before the map lookup, so a reference named `a\b` is written `{{equip}}/a\\b`, and one named `q"t` is written `{{equip}}/q\"t`; - the resolved RawPath is **encoded** (`EquipmentScriptPaths.EscapeForRegularLiteral`: `\` and `"` backslash-escaped; CR/LF/U+0085/U+2028/U+2029, other control characters and unpaired surrogates as `\uXXXX`; braces and non-ASCII untouched) when it is pasted back, so `GetTag` receives the exact RawPath — a device named `de\qv` neither breaks compilation nor, for `x\vy`, silently turns `\v` into a vertical tab; - `ExtractDependencyRefs` (the mux subscription keys) decodes the same way, so the dependency ref is the runtime string value, not the escaped source text; - the editor's `OTSCRIPT_EQUIPREF` diagnostic and completion use the same scan (`EquipmentScriptPaths.FindEquipReferenceLiterals`), and completion inserts the escaped form. A `` with a malformed escape (`\e`) is left as written for the compiler to report. Only regular `"…"` literals are path literals: a `{{equip}}/…` token inside a verbatim (`@"…"`), interpolated (`$"…"`) or raw (`"""…"""`) literal is **not substituted**, so since #1056 it is refused 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.) **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. --- ## Input quality: what a script sees, and when it runs at all The rule is stated once, in [`docs/DriverLifecycle.md` §"The quality contract"](DriverLifecycle.md#the-quality-contract), and it applies identically to virtual-tag scripts and to `Calculation` driver scripts — they share `VirtualTagContext` and this editor, so one policy keeps this page true for every `ctx.GetTag` call site. - **`ctx.GetTag(path).StatusCode` and `.SourceTimestampUtc` are real.** The read cache used to stamp every dependency `Good` with an evaluation-time timestamp, so a quality-aware script could not be written at all — it was told Good even for an input whose driver had stopped communicating. - **A script is NOT run while any of its declared inputs is Bad.** The tag instead reports that input's own status (`BadNoCommunication`, `BadWaitingForInitialData`, …) carrying its last known value. So the `(double)ctx.GetTag("x").Value` idiom is safe against a dead input: the script simply does not run, rather than throwing on every trigger and filling `/script-log`. - **An Uncertain input IS passed to the script**, and the output inherits that Uncertain sub-code. An Uncertain value is present and a script can make a qualified decision from it; that is the alarm engine's long-standing rule, now shared. - **The output never reports better than its worst input.** A Good result over an Uncertain input is published Uncertain; nothing is published Good while an input is Bad. - ⚠️ **If you wrote a script that deliberately handled `null` for a dead input, it no longer runs while that input is Bad.** Assume your inputs are usable — the runtime has already decided they are. ## Known Limitations / Follow-ups - **Tag-path completion shows resolvable keys only.** Surfacing the UNS browse path as a completion *detail* (a non-inserted hint shown alongside the resolvable key) for discoverability is an open, optional enhancement. - **Language providers register globally at Monaco load.** The four providers (completion, hover, signature help, document formatting) are registered for the `csharp` language on the global Monaco instance (loaded once per page in `App.razor`). Only pages gated by the `ConfigEditor` policy host a `MonacoEditor` today, but if a read-only surface ever embeds the editor the providers will fire (harmlessly: they get 401/403 responses from the gated endpoints and return empty results). Items removed from this list because they shipped or were dropped: inlay hints (provider and `/inlay-hints` endpoint dropped in Lane D, #745); creating a new script from the VirtualTagModal inline panel (shipped, `CreateNewScriptAsync` in `VirtualTagModal.razor`); the method-name-only literal gate (fixed by the `ctx`-receiver guard, see [Literal gate](#literal-gate)); and the synchronous Roslyn analysis (every analysis now derives from one process-wide base compilation and honours the request `CancellationToken`). --- ## Testing Unit tests live in `tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/ScriptAnalysis/`: | File | Covers | |------|--------| | `DiagnoseTests.cs` | Clean script (no markers), Roslyn error, ForbiddenTypeAnalyzer violation, dynamic-path rejection, `#line` mapping | | `CompletionTests.cs` | Scope completions, dot-member completions after `ctx.` | | `TagPathCompletionTests.cs` | Tag-path literal completions inside `GetTag`/`SetVirtualTag` via a fake `IScriptTagCatalog` | | `HoverSignatureTests.cs` | Hover markdown, signature-help parameter tracking | | `FormatTests.cs` | `NormalizeWhitespace` round-trip | | `ScriptTagCatalogTests.cs` | Equipment-tag `FullName`, Galaxy dot-ref, virtual-tag leaf `Name`, filter prefix, MaxResults bound | `tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Uns/ScriptSourceServiceTests.cs` covers `GetScriptSourceAsync` / `CountVirtualTagsUsingScriptAsync` / `UpdateScriptSourceAsync` via the in-memory EF pattern. No bUnit tests exist for `MonacoEditor.razor` or `VirtualTagModal.razor` — the AdminUI has no bUnit dependency. Razor/JS correctness was proven by live docker-dev `/run` (ScriptEdit + UNS TagModal with Monaco, diagnostics, hover, tag-path completions, Format, inline script save).