fix(scripting): {{equip}} substitution escapes the RawPath it pastes into the string literal

A RawPath segment may contain backslash, quote, braces, line breaks or non-ASCII text.
SubstituteEquipmentToken pasted it raw into a regular C# string literal, so a device named
de\qv failed to compile and x\vy silently read a different tag path (\v became a vertical tab).

The paste is now encoded (EscapeForRegularLiteral) and the <RefName> / dependency refs are
decoded (TryDecodeRegularLiteral), so GetTag receives the exact RawPath at both compose seams
and the mux subscribes to the real key. The path-literal regex is escape-aware. The Monaco
OTSCRIPT_EQUIPREF diagnostic and completion share the same scan/escape. The deploy gate's
SubstitutionPreservesCompilation is now a verified round trip, so its pinned device-name case
flips from rejected to accepted.
This commit is contained in:
Joseph Doherty
2026-12-03 11:40:17 -05:00
parent 84789b33f3
commit ee9aa30ec0
8 changed files with 609 additions and 68 deletions
@@ -238,7 +238,7 @@ public sealed class AdminOperationsActorScriptCompileGateTests : ControlPlaneAct
[Fact]
public void Equip_tokens_resolve_before_compiling_and_a_template_is_sent_once_for_every_equipment()
{
// Three equipments share one VT template: substitution only rewrites escape-free literal text, so the gate
// Three equipments share one VT template: substitution only rewrites literal text (escaped), so the gate
// sends the template once per seam rather than three resolved copies.
var checker = new RecordingChecker();
var reply = Deploy(SeedDraft(new Seed(Equipments: 3)), checker);
@@ -275,20 +275,33 @@ public sealed class AdminOperationsActorScriptCompileGateTests : ControlPlaneAct
}
/// <summary>
/// A RawPath segment may legally contain a backslash, and substitution pastes the RawPath into a C# string
/// literal — so <c>{{equip}}/speed</c> over a device named <c>de\qv</c> becomes an invalid escape the runtime
/// refuses to compile. The template compiles fine; only compiling the RESOLVED source (what the gate does
/// whenever substitution is not provably compile-neutral) catches it.
/// A RawPath segment may legally contain a backslash or a quote, and substitution pastes the RawPath into a C#
/// string literal. Substitution ESCAPES the paste (<c>EquipmentScriptPaths.EscapeForRegularLiteral</c>), so
/// <c>{{equip}}/speed</c> over a device named <c>de\qv</c> still compiles and still reads the exact RawPath
/// (<c>EquipTokenLiteralEscapingTests</c> in Runtime.Tests runs it end to end). It used to paste raw — an
/// invalid escape the runtime refused to compile, which only compiling the resolved source caught. Now the
/// substitution is provably compile-neutral, so the gate compiles the shared template once and the real
/// compiler accepts the draft; the resolved source itself is also compiled to prove it.
/// </summary>
[Fact]
public void An_equip_substitution_that_breaks_the_literal_is_caught_by_compiling_the_resolved_source()
[Theory]
[InlineData(@"de\qv")]
[InlineData("a\"b")]
[InlineData(@"x\vy")]
public void An_equip_substitution_over_a_RawPath_with_escape_characters_compiles_and_is_sent_once(string deviceName)
{
var reply = Deploy(SeedDraft(new Seed(DeviceName: @"de\qv")), new RoslynDeployScriptChecker(), expectDispatch: false);
reply.Outcome.ShouldBe(StartDeploymentOutcome.Rejected);
reply.Message.ShouldNotBeNull();
reply.Message.ShouldContain("script 's-vt' does not compile as a VirtualTag script");
reply.Message.ShouldContain("compile error");
reply.Message.ShouldContain("script 's-pred'"); // the predicate resolves through the same reference
var checker = new RoslynDeployScriptChecker();
var reply = Deploy(SeedDraft(new Seed(DeviceName: deviceName, Equipments: 2)), checker);
reply.Outcome.ShouldBe(StartDeploymentOutcome.Accepted, reply.Message);
(reply.Message ?? string.Empty).ShouldNotContain("does not compile");
// The resolved source (what the seams will run) compiles too — the gate's dedupe is not hiding a failure.
var map = new Dictionary<string, string>(StringComparer.Ordinal) { ["speed"] = $"plant/drv/{deviceName}/speed" };
EquipmentScriptPaths.SubstitutionPreservesCompilation(Legit, map).ShouldBeTrue();
var resolved = EquipmentScriptPaths.SubstituteEquipmentToken(Legit, map);
resolved.ShouldNotBe(Legit);
var verdict = new RoslynDeployScriptChecker()
.Check([new DeployScriptCheckRequest(ScriptContextKind.VirtualTag, resolved)], TestContext.Current.CancellationToken).Single();
verdict.Problems.ShouldBeEmpty();
}
[Fact]