[Bug]: relay-lost backoff "stabilized" reset measures reconnect duration, not post-ready uptime — give-up ceiling can be unreachable
Operating system
Other / Any (platform-independent; most visible against a remote host on a slow or high-latency link, e.g. a loaded Linux/Windows box over SSH)
Orca version
1.4.108-rc.1 (reading main)
Details
Short summary
The relay-lost reconnect backoff in src/main/ipc/ssh.ts has a give-up ceiling (RELAY_LOST_MAX_ATTEMPTS = 6) that is supposed to stop infinite reconnect attempts and surface an actionable error — "Relay channel kept dropping. Click Reconnect on the SSH target before retrying." But the "stabilized" check that resets the attempt counter measures how long the reconnect took to reach ready, not how long the session stayed ready after recovering. As a result, on a host where each reconnect legitimately takes more than the 5s window, the counter never accumulates and the give-up error is unreachable — the terminal sits in "Reconnecting…" indefinitely.
(For context: I was studying how Orca recovers remote terminals across an SSH host reboot vs a transient blip. The recovery itself is genuinely clean — a stale ptyId is detected and the pane is recreated in place rather than freezing. This report is just about the backoff give-up path.)
What happens (the mechanism)
In the setOnRelayLost handler, lastAttemptStartedAt is stamped inside the reconnect timer, right before s.reconnect() (≈ line 506):
state.pendingTimer = setTimeout(() => {
state.pendingTimer = null
state.lastAttemptStartedAt = Date.now() // stamped when the reconnect STARTS
...
void s.reconnect(liveConn, relayGracePeriodForTarget(t))
}, delay)
Then setOnReady (which fires when the reconnect reaches ready) treats it as stabilized using that same timestamp (≈ line 527):
const stabilized =
state.lastAttemptStartedAt === 0 ||
Date.now() - state.lastAttemptStartedAt >= RELAY_LOST_STABILIZED_MS
if (stabilized) {
relayLostBackoff.delete(tid) // counter reset
}
So Date.now() - lastAttemptStartedAt is the reconnect duration (attempt-start → ready), not the post-ready uptime. That's the opposite of what the constant's own comment intends:
// Why: if a fresh reconnect's mux dies within this window, the new session // never stabilized (a flap, not a real recovery). ... const RELAY_LOST_STABILIZED_MS = 5_000
Consequence (two-directional)
- (a) Slow reconnect → give-up unreachable. When each reconnect takes >5s to reach ready (loaded host, high-latency link, relay redeploy),
stabilizedevaluatestrueon every cycle and deletes the backoff, soattemptsoscillates and never reachesRELAY_LOST_MAX_ATTEMPTS. The actionable "Click Reconnect" error is never shown; the terminal stays in "Reconnecting…" forever. - (b) Fast (<5s) reconnect → counter never clears. A healthy fast reconnect evaluates
stabilized = false, so the backoff is not deleted on that ready and lingers — which also keepsrelayLostBackoff.has(target)true, making the window-reactivation fast-path (the!relayLostBackoff.has(targetId)guard, ≈ line 697) tear down + redeploy instead of reusing the live session — until a much-later qualifying ready or a teardown.
How to reproduce
Conceptually: trigger repeated relay losses where each reconnect reaches ready more than 5s after its attempt starts. The give-up error override never appears even after >6 losses. This is unit-testable in the existing ssh.test.ts fake-timer style (the relay-loss tests around the onDispose('connection_lost') harness): advancing the reconnect→ready gap past RELAY_LOST_STABILIZED_MS each cycle resets the counter every time.
Proposed fix (happy to PR)
Measure stabilization from when the session actually reaches ready and survives the window, rather than from the reconnect duration — e.g. on setOnReady, start a RELAY_LOST_STABILIZED_MS timer that clears the backoff only if no relay loss intervenes, and cancel it in setOnRelayLost. That makes the give-up ceiling reachable on a persistently-flapping host while still resetting on a genuine recovery, and it also cleans up the lingering-state / reactivation interaction in (b).
Before sending a PR I wanted to confirm the intended semantics, since this changes when the "Click Reconnect" error becomes user-visible (a host that previously spun forever would now reach the actionable state). Is the 5s window meant to gate on post-ready uptime, as the comment reads? If so, I'm glad to open a PR with regression tests covering both directions in the existing fake-timer style. Thanks for Orca!