DreamWeave: website links in the bar icon's dropdown #4

Open
AwfulWaffle wants to merge 29 commits from AwfulWaffle/RynthServers:dreamweave-bar-menu 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 _lastBeginMs;
private bool _everBegun;
// A request that expired: its late code is eaten (Stale) until the code itself would have expired.
private bool _expired;
private long _expiredDeadlineMs;
// Forward-only: the moment after which no code from any request begun so far can still be valid.
// Nothing lowers it (not Begin, Cancel, Reset, Code, Refused or Expire), so a late code from any
// earlier request is still eaten as Stale, whatever happened to the requests since.
private long _staleUntilMs = long.MinValue;
public WebDestination? Pending => _pending;
public string Status { get; private set; } = "";
@ -46,7 +47,7 @@ internal sealed class WebLauncher
_everBegun = true;
_lastBeginMs = nowMs;
_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 + "...";
return LaunchBegin.Started;
}
@ -59,30 +60,22 @@ internal sealed class WebLauncher
}
/// <summary>
/// NotOurs unless a request is pending, or expired within the code's lifetime. Before the
/// deadline, Code and Refused clear Pending and the caller eats the line. After the deadline,
/// a valid code is Stale (eat it, open nothing; dest null, code empty) until CodeLifetimeMs
/// past the deadline; any other line is NotOurs. On Code, message is empty.
/// A pending request before its deadline takes the line: Code or Refused clears Pending and the
/// 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.
/// </summary>
public LaunchLine OnChatLine(string text, int chatType, long nowMs, out WebDestination? dest, out string code, out string message)
{
dest = null;
code = "";
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 (nowMs >= deadline)
{
if (parsedCode.Length > 0 && nowMs < deadline + LaunchWire.CodeLifetimeMs) return LaunchLine.Stale;
return LaunchLine.NotOurs;
}
WebDestination? p = _pending;
if (p == null || nowMs >= _deadlineMs)
return parsedCode.Length > 0 && nowMs < _staleUntilMs ? LaunchLine.Stale : LaunchLine.NotOurs;
WebDestination p = _pending!;
_pending = null;
dest = p;
if (parsedCode.Length > 0)
@ -95,7 +88,6 @@ internal sealed class WebLauncher
Status = p.Label + ": DreamWeave said: " + message;
return LaunchLine.Refused;
}
/// <summary>The pending destination once, at or after Begin + ReplyTimeoutMs; null otherwise.</summary>
public WebDestination? TakeTimedOut(long nowMs)
{
@ -115,8 +107,6 @@ internal sealed class WebLauncher
private void Expire()
{
_pending = null;
_expired = true;
_expiredDeadlineMs = _deadlineMs;
Status = "No reply from DreamWeave to " + LaunchWire.RequestCommand + " within "
+ (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: 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: overlapping requests never leak a code to chat", OverlappingRequests);
}
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.True(ReferenceEquals(dest, Market), "destination handed back");
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(dest == null && code == "" && msg == "", "no outputs on NotOurs");
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 Stale");
// Timeout at exactly +10,000.
var t = new WebLauncher();
@ -206,7 +207,7 @@ internal static class DreamWeaveWebTests
p.Reset();
Check.True(p.Pending == null, "reset clears pending");
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()
@ -276,6 +277,7 @@ internal static class DreamWeaveWebTests
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");
}
private static void SelfExpiry()
{
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.Eq(x.TakeTimedOut(20_001)?.Id ?? "none", "suit", "and it times out at its own deadline");
}
private static void ResetKeepsThrottle()
{
var w = new WebLauncher();
@ -325,15 +328,58 @@ internal static class DreamWeaveWebTests
var c = new WebLauncher();
c.Begin(Market, 0);
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();
b.Begin(Market, 0);
b.TakeTimedOut(10_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()
{
string? dir = AppContext.BaseDirectory;