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,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.";
}