DreamWeave: website links, logged in by launch code #2

Open
AwfulWaffle wants to merge 19 commits from AwfulWaffle/RynthServers:dreamweave-web-panel into main
2 changed files with 64 additions and 28 deletions
Showing only changes of commit dd4cbd83bb - Show all commits

RynthDreamWeave: forward-only stale window for late codes

Replace the _expired flag with _staleUntilMs, raised by every started Begin and never
lowered, so an earlier request's late code is eaten after overlap, Cancel or Reset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Daniel Wallace 2026-11-07 14:56:03 -08:00

View file

@ -26,9 +26,10 @@ internal sealed class WebLauncher
private long _deadlineMs; private long _deadlineMs;
private long _lastBeginMs; private long _lastBeginMs;
private bool _everBegun; private bool _everBegun;
// A request that expired: its late code is eaten (Stale) until the code itself would have expired. // Forward-only: the moment after which no code from any request begun so far can still be valid.
private bool _expired; // Nothing lowers it (not Begin, Cancel, Reset, Code, Refused or Expire), so a late code from any
private long _expiredDeadlineMs; // earlier request is still eaten as Stale, whatever happened to the requests since.
private long _staleUntilMs = long.MinValue;
public WebDestination? Pending => _pending; public WebDestination? Pending => _pending;
public string Status { get; private set; } = ""; public string Status { get; private set; } = "";
@ -46,7 +47,7 @@ internal sealed class WebLauncher
_everBegun = true; _everBegun = true;
_lastBeginMs = nowMs; _lastBeginMs = nowMs;
_deadlineMs = nowMs + LaunchWire.ReplyTimeoutMs; _deadlineMs = nowMs + LaunchWire.ReplyTimeoutMs;
_expired = false; _staleUntilMs = Math.Max(_staleUntilMs, nowMs + LaunchWire.ReplyTimeoutMs + LaunchWire.CodeLifetimeMs);
Status = "Asking DreamWeave for a login code for " + d.Label + "..."; Status = "Asking DreamWeave for a login code for " + d.Label + "...";
return LaunchBegin.Started; return LaunchBegin.Started;
} }
@ -59,30 +60,22 @@ internal sealed class WebLauncher
} }
/// <summary> /// <summary>
/// NotOurs unless a request is pending, or expired within the code's lifetime. Before the /// A pending request before its deadline takes the line: Code or Refused clears Pending and the
/// deadline, Code and Refused clear Pending and the caller eats the line. After the deadline, /// caller eats it (on Code, message is empty). Otherwise a line TryParse reads as a valid code is
/// a valid code is Stale (eat it, open nothing; dest null, code empty) until CodeLifetimeMs /// Stale (eat it, open nothing; dest null, code and message empty) while nowMs is before the
/// past the deadline; any other line is NotOurs. On Code, message is empty. /// forward-only stale deadline, and NotOurs after. Any other line outside a request is NotOurs.
/// </summary> /// </summary>
public LaunchLine OnChatLine(string text, int chatType, long nowMs, out WebDestination? dest, out string code, out string message) public LaunchLine OnChatLine(string text, int chatType, long nowMs, out WebDestination? dest, out string code, out string message)
{ {
dest = null; dest = null;
code = ""; code = "";
message = ""; message = "";
long deadline;
if (_pending != null) deadline = _deadlineMs;
else if (_expired) deadline = _expiredDeadlineMs;
else return LaunchLine.NotOurs;
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)) return LaunchLine.NotOurs;
if (nowMs >= deadline) WebDestination? p = _pending;
{ if (p == null || nowMs >= _deadlineMs)
if (parsedCode.Length > 0 && nowMs < deadline + LaunchWire.CodeLifetimeMs) return LaunchLine.Stale; return parsedCode.Length > 0 && nowMs < _staleUntilMs ? LaunchLine.Stale : LaunchLine.NotOurs;
return LaunchLine.NotOurs;
}
WebDestination p = _pending!;
_pending = null; _pending = null;
dest = p; dest = p;
if (parsedCode.Length > 0) if (parsedCode.Length > 0)
@ -95,7 +88,6 @@ internal sealed class WebLauncher
Status = p.Label + ": DreamWeave said: " + message; Status = p.Label + ": DreamWeave said: " + message;
return LaunchLine.Refused; return LaunchLine.Refused;
} }
/// <summary>The pending destination once, at or after Begin + ReplyTimeoutMs; null otherwise.</summary> /// <summary>The pending destination once, at or after Begin + ReplyTimeoutMs; null otherwise.</summary>
public WebDestination? TakeTimedOut(long nowMs) public WebDestination? TakeTimedOut(long nowMs)
{ {
@ -115,8 +107,6 @@ internal sealed class WebLauncher
private void Expire() private void Expire()
{ {
_pending = null; _pending = null;
_expired = true;
_expiredDeadlineMs = _deadlineMs;
Status = "No reply from DreamWeave to " + LaunchWire.RequestCommand + " within " Status = "No reply from DreamWeave to " + LaunchWire.RequestCommand + " within "
+ (LaunchWire.ReplyTimeoutMs / 1000) + " s, so nothing was opened."; + (LaunchWire.ReplyTimeoutMs / 1000) + " s, so nothing was opened.";
} }

View file

@ -26,6 +26,7 @@ internal static class DreamWeaveWebTests
r.Add("dreamweave web: an expired request frees itself", SelfExpiry); r.Add("dreamweave web: an expired request frees itself", SelfExpiry);
r.Add("dreamweave web: reset keeps the throttle", ResetKeepsThrottle); r.Add("dreamweave web: reset keeps the throttle", ResetKeepsThrottle);
r.Add("dreamweave web: a late code is eaten while it could still be valid", LateCodeIsStale); 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);
} }
private const string Code = "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789-_AbCde"; // 43 chars private const string Code = "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789-_AbCde"; // 43 chars
@ -172,8 +173,8 @@ internal static class DreamWeaveWebTests
Check.Eq(msg, "", "no message on a code"); Check.Eq(msg, "", "no message on a code");
Check.True(ReferenceEquals(dest, Market), "destination handed back"); Check.True(ReferenceEquals(dest, Market), "destination handed back");
Check.True(w.Pending == null, "code clears pending"); Check.True(w.Pending == null, "code clears pending");
Check.True(w.OnChatLine(Line, 0x05, 1000 + 9_999, out dest, out code, out msg) == LaunchLine.NotOurs, "an identical second line is not ours"); Check.True(w.OnChatLine(Line, 0x05, 1000 + 9_999, out dest, out code, out msg) == LaunchLine.Stale, "an identical second line is stale: eaten, opens nothing");
Check.True(dest == null && code == "" && msg == "", "no outputs on NotOurs"); Check.True(dest == null && code == "" && msg == "", "no outputs on Stale");
// Timeout at exactly +10,000. // Timeout at exactly +10,000.
var t = new WebLauncher(); var t = new WebLauncher();
@ -206,7 +207,7 @@ internal static class DreamWeaveWebTests
p.Reset(); p.Reset();
Check.True(p.Pending == null, "reset clears pending"); Check.True(p.Pending == null, "reset clears pending");
Check.Eq(p.Status, "", "reset clears status"); Check.Eq(p.Status, "", "reset clears status");
Check.True(p.OnChatLine(Line, 0x05, 5, out _, out _, out _) == LaunchLine.NotOurs, "after reset: not ours"); Check.True(p.OnChatLine(Line, 0x05, 5, out _, out _, out _) == LaunchLine.Stale, "after reset: a code is stale, still eaten");
} }
private static void OneAtATime() private static void OneAtATime()
@ -276,6 +277,7 @@ internal static class DreamWeaveWebTests
w3.OnChatLine("[WEB] id " + Code.Substring(1) + " ok", 0x05, 10, out _, out _, out msg); w3.OnChatLine("[WEB] id " + Code.Substring(1) + " ok", 0x05, 10, out _, out _, out msg);
Check.Eq(msg, "id " + Code.Substring(1) + " ok", "42-char run is ordinary text"); Check.Eq(msg, "id " + Code.Substring(1) + " ok", "42-char run is ordinary text");
} }
private static void SelfExpiry() private static void SelfExpiry()
{ {
var w = new WebLauncher(); var w = new WebLauncher();
@ -290,6 +292,7 @@ internal static class DreamWeaveWebTests
Check.True(x.TakeTimedOut(10_001) == null, "the new request is not timed out yet"); Check.True(x.TakeTimedOut(10_001) == null, "the new request is not timed out yet");
Check.Eq(x.TakeTimedOut(20_001)?.Id ?? "none", "suit", "and it times out at its own deadline"); Check.Eq(x.TakeTimedOut(20_001)?.Id ?? "none", "suit", "and it times out at its own deadline");
} }
private static void ResetKeepsThrottle() private static void ResetKeepsThrottle()
{ {
var w = new WebLauncher(); var w = new WebLauncher();
@ -325,15 +328,58 @@ internal static class DreamWeaveWebTests
var c = new WebLauncher(); var c = new WebLauncher();
c.Begin(Market, 0); c.Begin(Market, 0);
c.OnChatLine(Line, 0x05, 100, out _, out _, out _); c.OnChatLine(Line, 0x05, 100, out _, out _, out _);
Check.True(c.OnChatLine(Line, 0x05, 10_500, out _, out _, out _) == LaunchLine.NotOurs, "after a code: not ours"); Check.True(c.OnChatLine(Line, 0x05, 10_500, out _, out _, out _) == LaunchLine.Stale, "after a consumed code: a repeat is stale");
// A new Begin ends the old stale window. // A new Begin never ends the old stale window: the new request takes a code while pending,
// and the window still covers the old code once the new request is gone.
var b = new WebLauncher(); var b = new WebLauncher();
b.Begin(Market, 0); b.Begin(Market, 0);
b.TakeTimedOut(10_000); b.TakeTimedOut(10_000);
b.Begin(Market, 20_000); b.Begin(Market, 20_000);
Check.True(b.OnChatLine(Line, 0x05, 20_100, out _, out _, out _) == LaunchLine.Code, "new request takes the code"); Check.True(b.OnChatLine(Line, 0x05, 20_100, out _, out _, out _) == LaunchLine.Code, "new request takes the code while pending");
Check.True(b.OnChatLine(Line, 0x05, 20_200, out _, out _, out _) == LaunchLine.Stale, "then a further valid code is stale");
// Window upper boundary.
Check.Eq(LaunchWire.CodeLifetimeMs, 60_000L, "code lifetime");
var u = new WebLauncher();
u.Begin(Market, 0);
Check.True(u.OnChatLine(Line, 0x05, 69_999, out _, out _, out _) == LaunchLine.Stale, "+69,999 stale");
Check.True(u.OnChatLine(Line, 0x05, 70_000, out _, out _, out _) == LaunchLine.NotOurs, "+70,000 not ours");
} }
private static void OverlappingRequests()
{
// Scenario 1: request 1 times out, request 2 begins, request 1's late code arrives.
var w = new WebLauncher();
w.Begin(Market, 0);
w.TakeTimedOut(10_000);
Check.True(w.Begin(Market, 13_000) == LaunchBegin.Started, "request 2");
Check.True(w.OnChatLine(Line, 0x05, 14_000, out _, out _, out _) == LaunchLine.Code, "the late code is taken by request 2 (indistinguishable)");
Check.True(w.OnChatLine(Line, 0x05, 15_000, out var dest, out string code, out string msg) == LaunchLine.Stale, "request 2's own reply is stale, never chat");
Check.True(dest == null && code == "" && msg == "", "stale carries nothing");
// Scenario 2: request 1 expires, request 2 begins and is cancelled, the late code arrives.
var c = new WebLauncher();
c.Begin(Market, 0);
c.TakeTimedOut(10_000);
c.Begin(Market, 13_000);
c.Cancel("Could not send /weblogin.");
Check.True(c.OnChatLine(Line, 0x05, 14_000, out _, out _, out _) == LaunchLine.Stale, "after cancel: stale");
// Reset while pending, then the code.
var r = new WebLauncher();
r.Begin(Market, 0);
r.Reset();
Check.True(r.OnChatLine(Line, 0x05, 5_000, out _, out _, out _) == LaunchLine.Stale, "after reset while pending: stale");
Check.True(r.OnChatLine("[WEB] some refusal", 0x05, 5_000, out _, out _, out _) == LaunchLine.NotOurs, "refusals outside a request stay not ours");
// Cancel right after Begin: the ask may have been sent anyway.
var k = new WebLauncher();
k.Begin(Market, 0);
k.Cancel("x");
Check.True(k.OnChatLine(Line, 0x05, 2_000, out _, out _, out _) == LaunchLine.Stale, "cancelled request's code is stale");
}
private static string[] ServerPluginIds() private static string[] ServerPluginIds()
{ {
string? dir = AppContext.BaseDirectory; string? dir = AppContext.BaseDirectory;