From 7003094f56a772b9aff407f35bfe315a5f05adab Mon Sep 17 00:00:00 2001 From: Daniel Wallace Date: Sat, 7 Nov 2026 15:43:49 -0800 Subject: [PATCH] RynthDreamWeave: final-review fixes (malformed code lines never shown, wording) - LaunchWire.IsSuccessShaped: a server line starting "[WEB] Launch code", well formed or not. OnChatLine keeps a malformed one out of chat: a pending request takes it as Refused with the unexpected-reply message; otherwise Stale inside the stale window, NotOurs after. TryParse and its pins are unchanged. - An empty "[WEB] " refusal is an unexpected reply too. - Wording: "an unexpected reply (is RynthDreamWeave up to date?)", "Wait a few seconds between website opens.", "Couldn't open the website." - Tests: the raw-string guard covers only the DreamWeave files the scan parses; the scan test's name says its catch logs through LogTagged; the directory walkers added on this branch use ServerProjectsTests.PluginsDir(). Co-Authored-By: Claude Opus 5.5 --- .../DreamWeave/LaunchWire.cs | 8 ++ .../DreamWeave/WebLauncher.cs | 29 +++++- .../DreamWeaveWebTools.cs | 4 +- .../DreamWeaveWebTests.cs | 98 ++++++++++++++----- 4 files changed, 107 insertions(+), 32 deletions(-) diff --git a/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/LaunchWire.cs b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/LaunchWire.cs index aba5a34..551e481 100644 --- a/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/LaunchWire.cs +++ b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/LaunchWire.cs @@ -26,6 +26,14 @@ internal static class LaunchWire public static bool IsCodeChar(char c) => (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_'; + /// + /// True when is a server line that starts like a success ("[WEB] Launch code"), + /// well formed or not. TryParse rejects a malformed one; the caller must still keep it out of chat, + /// since it may carry most of a code. + /// + public static bool IsSuccessShaped(string? text, int chatType) + => text != null && !ChatSource.IsPlayerAuthored(chatType) && ChatSource.StartsWithMarker(text, SuccessStem); + /// /// True when is a [WEB] reply from the server: then either /// is set (success) or is (a refusal). diff --git a/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs index 0f7bb48..f2b058c 100644 --- a/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs +++ b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebLauncher.cs @@ -23,7 +23,7 @@ internal sealed class WebLauncher { public readonly object Gate = new(); - private const string UnexpectedReply = "DreamWeave sent an unexpected reply."; + private const string UnexpectedReply = "an unexpected reply (is RynthDreamWeave up to date?)"; private WebDestination? _pending; private long _deadlineMs; @@ -72,13 +72,26 @@ internal sealed class WebLauncher /// caller eats it (on Code, message is empty). Otherwise a line TryParse reads as a valid code is /// Stale (eat it, open nothing; dest null, code and message empty) while nowMs is before the /// forward-only stale deadline, and NotOurs after. Any other line outside a request is NotOurs. + /// A success-shaped line TryParse rejects (a malformed code) is treated the same way, never shown: + /// a pending request takes it as Refused with the unexpected-reply message; otherwise Stale inside + /// the window and NotOurs after. An empty refusal, or one holding a code-length run, is unexpected too. /// public LaunchLine OnChatLine(string text, int chatType, long nowMs, out WebDestination? dest, out string code, out string message) { dest = null; code = ""; message = ""; - if (!LaunchWire.TryParse(text, chatType, out string parsedCode, out string refusal)) return LaunchLine.NotOurs; + if (!LaunchWire.TryParse(text, chatType, out string parsedCode, out string refusal)) + { + if (!LaunchWire.IsSuccessShaped(text, chatType)) return LaunchLine.NotOurs; + WebDestination? sp = _pending; + if (sp == null || nowMs >= _deadlineMs) return nowMs < _staleUntilMs ? LaunchLine.Stale : LaunchLine.NotOurs; + _pending = null; + dest = sp; + message = UnexpectedReply; + Status = sp.Label + ": DreamWeave sent " + UnexpectedReply; + return LaunchLine.Refused; + } WebDestination? p = _pending; if (p == null || nowMs >= _deadlineMs) @@ -92,8 +105,16 @@ internal sealed class WebLauncher Status = "Opening " + p.Label + "..."; return LaunchLine.Code; } - message = HasCodeRun(refusal) ? UnexpectedReply : refusal; - Status = p.Label + ": DreamWeave said: " + message; + if (refusal.Trim().Length == 0 || HasCodeRun(refusal)) + { + message = UnexpectedReply; + Status = p.Label + ": DreamWeave sent " + UnexpectedReply; + } + else + { + message = refusal; + Status = p.Label + ": DreamWeave said: " + refusal; + } return LaunchLine.Refused; } diff --git a/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeaveWebTools.cs b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeaveWebTools.cs index 66f55c8..71aec1f 100644 --- a/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeaveWebTools.cs +++ b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeaveWebTools.cs @@ -116,7 +116,7 @@ public sealed partial class ServerPackPlugin } if (begun == LaunchBegin.TooSoon) { - Reply.Fail("Wait a few seconds between web opens."); + Reply.Fail("Wait a few seconds between website opens."); return; } @@ -134,7 +134,7 @@ public sealed partial class ServerPackPlugin try { LogTagged("Starting a web open failed: " + ex.GetType().Name); - Reply.Fail("Couldn't start the web open."); + Reply.Fail("Couldn't open the website."); } catch (Exception) { diff --git a/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs b/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs index 5f09271..05d8914 100644 --- a/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs +++ b/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs @@ -28,8 +28,9 @@ internal static class DreamWeaveWebTests r.Add("dreamweave web: a late code is eaten while it could still be valid", LateCodeIsStale); r.Add("dreamweave web: overlapping requests never leak a code to chat", OverlappingRequests); r.Add("dreamweave web: profile keys separate accounts", ProfileKeys); - r.Add("dreamweave web: ImGui only in WebButtons, no context switching, no host calls from draw", ThreadRules); + r.Add("dreamweave web: ImGui only in WebButtons, no context switching, draw makes no host calls (its catch logs via LogTagged)", ThreadRules); r.Add("dreamweave web: an open's answer lands only on its own request", ResultGeneration); + r.Add("dreamweave web: a malformed launch-code line never reaches chat", MalformedSuccess); r.Add("dreamweave web: the source scan's helpers read code, not comments or strings", ScanHelpers); r.Add("dreamweave web: the plugin's shared hooks hand off to the web tools first", HooksHandOff); } @@ -37,15 +38,6 @@ internal static class DreamWeaveWebTests // -- Host wiring (source scan: the plugin is NativeAOT, so its host code can't run here) -- // The rules are allow-lists: what may appear is named, so a new spelling of a forbidden call fails. - private static string PluginsRoot() - { - string? dir = AppContext.BaseDirectory; - while (dir != null && !Directory.Exists(Path.Combine(dir, "Plugins", "Shared", "ServerPack"))) - dir = Path.GetDirectoryName(dir); - if (dir == null) throw new DirectoryNotFoundException("Plugins not found above " + AppContext.BaseDirectory); - return Path.Combine(dir, "Plugins"); - } - private static IEnumerable SourceFiles(string root) => Directory.GetFiles(root, "*.cs", SearchOption.AllDirectories) .Where(p => !p.Contains(Path.DirectorySeparatorChar + "bin" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) @@ -212,7 +204,7 @@ internal static class DreamWeaveWebTests // panel: each hands off to its Web* method before its own code, so no gate of theirs comes first. private static void HooksHandOff() { - string dw = Path.Combine(PluginsRoot(), "RynthCore.Plugin.RynthDreamWeave"); + string dw = Path.Combine(ServerProjectsTests.PluginsDir(), "RynthCore.Plugin.RynthDreamWeave"); string plugin = StripComments(File.ReadAllText(Path.Combine(dw, "DreamWeavePlugin.cs"))); string skills = StripComments(File.ReadAllText(Path.Combine(dw, "DreamWeaveSkills.cs"))); (string Code, string Head, string Call)[] hooks = @@ -242,9 +234,11 @@ internal static class DreamWeaveWebTests Check.False(web.Contains("partial void " + hook + "(", StringComparison.Ordinal), "DreamWeaveWebTools.cs leaves " + hook + " to the hub"); } + // The draw path makes no host calls. ServerCardActions' catch (render thread) does log through + // LogTagged once per failure, the shared pattern RenderEmbedded's own catch already follows. private static void ThreadRules() { - string plugins = PluginsRoot(); + string plugins = ServerProjectsTests.PluginsDir(); string dw = Path.Combine(plugins, "RynthCore.Plugin.RynthDreamWeave"); string buttonsPath = Path.Combine(dw, "DreamWeave", "WebButtons.cs"); string hostPath = Path.Combine(dw, "DreamWeaveWebTools.cs"); @@ -266,14 +260,15 @@ internal static class DreamWeaveWebTests } } + // StripComments does not model raw string literals: a DreamWeave file with one must fail, not scan + // wrongly. Only the files this scan parses in depth are held to it (the web tools' files and the two hub + // files HooksHandOff reads), so another server, or another DreamWeave feature, can't break it. + foreach (string f in WebFiles(dw).Concat(new[] { Path.Combine(dw, "DreamWeavePlugin.cs"), Path.Combine(dw, "DreamWeaveSkills.cs") })) + Check.False(File.ReadAllText(f).Contains("\"\"\"", StringComparison.Ordinal), Path.GetFileName(f) + ": no raw string literal (the scan does not support them)"); + var processStart = new System.Text.RegularExpressions.Regex(@"\bProcess\s*\.\s*Start\b"); foreach (string f in SourceFiles(plugins)) - { - string raw = File.ReadAllText(f); - // StripComments does not model raw string literals: a file with one must fail, not scan wrongly. - Check.False(raw.Contains("\"\"\"", StringComparison.Ordinal), Path.GetFileName(f) + ": no raw string literal (the scan does not support them)"); - Check.False(processStart.IsMatch(StripComments(raw)), Path.GetFileName(f) + ": no Process.Start under Plugins (ShellExecuteW only)"); - } + Check.False(processStart.IsMatch(StripComments(File.ReadAllText(f))), Path.GetFileName(f) + ": no Process.Start under Plugins (ShellExecuteW only)"); // Monitor would take the launcher's lock without a lock statement, out of the scan's sight. var monitor = new System.Text.RegularExpressions.Regex(@"\bMonitor\s*\."); @@ -371,6 +366,61 @@ internal static class DreamWeaveWebTests } } + private static void MalformedSuccess() + { + string head = "[WEB] Launch code: "; + string long44 = head + Code + "A (single use, expires in 60 seconds)"; + string dotted = head + Code + "."; + foreach (string l in new[] { long44, dotted }) + { + Check.True(!Parse(l, 0x05, out _, out _), "TryParse still rejects it: " + l.Length + " chars"); + Check.True(LaunchWire.IsSuccessShaped(l, 0x05), "success-shaped: " + l.Length + " chars"); + Check.False(LaunchWire.IsSuccessShaped(l, 0x02), "a player's copy is not success-shaped"); + } + Check.True(LaunchWire.IsSuccessShaped(" [WEB] Launch code", 0x05), "the stem alone, past leading space"); + Check.False(LaunchWire.IsSuccessShaped("[WEB] Signing in is turned off.", 0x05), "a refusal is not success-shaped"); + Check.False(LaunchWire.IsSuccessShaped(null, 0x05), "null is not success-shaped"); + + foreach (string l in new[] { long44, dotted }) + { + // Pending: the request takes the line as an unexpected reply; nothing of the code survives. + var w = new WebLauncher(); + w.Begin(Market, 0); + var k = w.OnChatLine(l, 0x05, 100, out var dest, out string code, out string msg); + Check.True(k == LaunchLine.Refused, "pending: a malformed code line is Refused (eaten), not NotOurs"); + Check.True(ReferenceEquals(dest, Market), "pending: the destination is handed back"); + Check.Eq(code, "", "pending: no code"); + Check.Eq(msg, "an unexpected reply (is RynthDreamWeave up to date?)", "pending: the fixed message"); + Check.Eq(w.Status, "Market: DreamWeave sent an unexpected reply (is RynthDreamWeave up to date?)", "pending: the status"); + Check.True(w.Pending == null, "pending: cleared"); + Check.False(w.Status.Contains(Code.Substring(0, 12), StringComparison.Ordinal) || msg.Contains(Code.Substring(0, 12), StringComparison.Ordinal), "pending: no code in status or message"); + + // A player's copy while pending is still not ours. + var p = new WebLauncher(); + p.Begin(Market, 0); + Check.True(p.OnChatLine(l, 0x02, 100, out _, out _, out _) == LaunchLine.NotOurs, "player copy (0x02): not ours"); + Check.True(p.Pending != null, "player copy leaves the request pending"); + + // Not pending: Stale inside the window, NotOurs after it. + var s = new WebLauncher(); + s.Begin(Market, 0); + s.TakeTimedOut(10_000); + Check.True(s.OnChatLine(l, 0x05, 20_000, out dest, out code, out msg) == LaunchLine.Stale, "not pending, inside the window: Stale"); + Check.True(dest == null && code == "" && msg == "", "Stale carries nothing"); + Check.True(s.OnChatLine(l, 0x05, 70_001, out _, out _, out _) == LaunchLine.NotOurs, "after the window: NotOurs"); + Check.True(new WebLauncher().OnChatLine(l, 0x05, 5, out _, out _, out _) == LaunchLine.NotOurs, "no Begin ever: NotOurs"); + } + + // An empty refusal is an unexpected reply too. + foreach (string l in new[] { "[WEB] ", "[WEB] " }) + { + var e = new WebLauncher(); + e.Begin(Market, 0); + Check.True(e.OnChatLine(l, 0x05, 10, out _, out _, out string msg) == LaunchLine.Refused, "empty refusal: Refused"); + Check.Eq(msg, "an unexpected reply (is RynthDreamWeave up to date?)", "empty refusal: the fixed message"); + } + } + private static void ResultGeneration() { var w = new WebLauncher(); @@ -663,15 +713,15 @@ internal static class DreamWeaveWebTests Check.Eq(code, "", "no code"); Check.True(!msg.Contains(Code), "message has no code"); Check.True(!w.Status.Contains(Code), "status has no code"); - Check.Eq(msg, "DreamWeave sent an unexpected reply.", "fixed replacement text"); - Check.Eq(w.Status, "Market: DreamWeave said: DreamWeave sent an unexpected reply.", "status from the replacement"); + Check.Eq(msg, "an unexpected reply (is RynthDreamWeave up to date?)", "fixed replacement text"); + Check.Eq(w.Status, "Market: DreamWeave sent an unexpected reply (is RynthDreamWeave up to date?)", "status from the replacement"); Check.True(w.Pending == null, "pending cleared"); // A long run of code chars anywhere, and a 42-char run (below the limit) left alone. var w2 = new WebLauncher(); w2.Begin(Market, 0); w2.OnChatLine("[WEB] x " + Code + Code + " y", 0x05, 10, out _, out _, out msg); - Check.Eq(msg, "DreamWeave sent an unexpected reply.", "longer run replaced"); + Check.Eq(msg, "an unexpected reply (is RynthDreamWeave up to date?)", "longer run replaced"); var w3 = new WebLauncher(); w3.Begin(Market, 0); w3.OnChatLine("[WEB] id " + Code.Substring(1) + " ok", 0x05, 10, out _, out _, out msg); @@ -825,12 +875,8 @@ internal static class DreamWeaveWebTests private static string[] ServerPluginIds() { - string? dir = AppContext.BaseDirectory; - while (dir != null && !Directory.Exists(Path.Combine(dir, "Plugins", "Shared", "ServerPack"))) - dir = Path.GetDirectoryName(dir); - if (dir == null) throw new DirectoryNotFoundException("Plugins not found above " + AppContext.BaseDirectory); var ids = new List(); - foreach (string p in Directory.GetDirectories(Path.Combine(dir, "Plugins"), "RynthCore.Plugin.Rynth*")) + foreach (string p in Directory.GetDirectories(ServerProjectsTests.PluginsDir(), "RynthCore.Plugin.Rynth*")) { string csproj = Path.Combine(p, Path.GetFileName(p) + ".csproj"); if (!File.Exists(csproj)) continue;