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>
This commit is contained in:
Daniel Wallace 2026-11-07 14:56:03 -08:00
commit dd4cbd83bb
2 changed files with 64 additions and 28 deletions

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;