Repository navigation
Description
Problem — four copies of the same backoff curve
The exponential-backoff reconnect curve (Math.min(1000 * 2^n, cap) shape) is duplicated across four call sites in three packages:
| Location | Surface | Max attempts | Cap | Session-unknown fast-path |
|---|---|---|---|---|
packages/vscode/src/connection-manager.ts:177 |
SSE / health-check | — | 30s | — |
packages/vscode/src/terminal-adapter.ts:208 |
VSCode terminal WS | 6 | 30s | yes (Tower close-code) |
packages/dashboard/src/components/Terminal.tsx:533 |
Web terminal WS | 50 | (varies) | no (no-op onerror) |
packages/codev/src/agent-farm/lib/tunnel-client.ts:71 |
Tunnel control channel | — | — | — |
Each instance reinvents the same primitive with subtly different tuning. Worse, the divergence on max-attempts (6 vs 50) and session-unknown detection (yes vs no recovery affordance) between the VSCode and web terminals means the same user-visible state (a terminal that's stopped reconnecting) has different semantics depending on where it's viewed — and the web side lacks the one-click recovery affordance that #939 ships for VSCode.
The #936 PR added the third copy. This issue pays that debt down.
Why this belongs in @cluesmith/codev-core
Precedent: EscapeBuffer lives at packages/core/src/escape-buffer.ts and is consumed by both vscode/src/terminal-adapter.ts and dashboard/src/components/Terminal.tsx. Reconnect policy is the same shape of thing — pure logic with no vscode or DOM dependency, useful from any host that wraps a flaky socket.
Proposed extract
A pure ReconnectPolicy / BackoffController in packages/core/src/:
- State: attempt counter, current-status (
idle/connecting/connected/giving-up). - Computation:
nextDelayMs()returnsMath.min(base * 2^attempt, cap). - Lifecycle:
recordSuccess()(resets counter),recordFailure(reason)(advances; returns next action:retry/give-up),start()/stop(). - Classification:
classifyUpgradeError(reason: string | { code, message }): 'permanent' | 'transient'— encapsulates the Tower-close-code rule (session-unknown → permanent; transport blip → transient) so all four sites agree on what's worth retrying. - Configurable: base, cap, max-attempts, classifier — defaults bake in the v1 terminal-adapter: WebSocket close-handler spams 'Connection lost' in a tight loop with no backoff, no give-up, no actual reconnect #936 choices (1000ms base, 30s cap, 6 attempts, Tower close-code classifier).
What stays host-side
- The socket itself (Node
wspackage vs browserWebSocketvs Node http2-stream — all incompatible). - The output sink (
writeEmitter+ ANSI sequences in vscode;xterm'sterm.writein dashboard; stdout in CLI). - The recovery affordance (vscode terminal-link from vscode: add manual-reconnect affordance to terminal give-up state #939; xterm link-addon or React button in dashboard; CLI re-prompt).
The policy is the brain; the host is the body. Same split as EscapeBuffer.
Design calls for the plan gate
These are the substantive decisions worth a plan-approval gate before code lands:
- Reconcile the 6-vs-50 max-attempts divergence. VSCode terminal-adapter (post-terminal-adapter: WebSocket close-handler spams 'Connection lost' in a tight loop with no backoff, no give-up, no actual reconnect #936) gives up at 6. Dashboard
Terminal.tsxgives up at 50. They're the same UX surface from the user's POV — should they agree? My read: 6 is the right number (faster surfacing of "this isn't recovering, intervene") but the web terminal's 50 may reflect a legitimate "browsers see more transient closes" pressure. Plan should decide. - Should the web terminal adopt session-unknown detection? Today's
Terminal.tsxonerroris a no-op — it can't fast-path a Tower-side "session forgotten" close. Extracting forces a decision: adopt and gain instant give-up on stale-session, or keep behavior identical to today's blind retry? Adopting is the obviously-right answer but it's a behavior change to flag. - Add the recovery affordance to the web terminal too? If yes, this issue grows by a small dashboard PR. If no, the web terminal still gives up silently — leave that to its own follow-up.
- Do the SSE / tunnel clients want the same policy?
connection-manager(SSE health-check) andtunnel-client(control channel) have different "what counts as transient" rules than a PTY socket. Probably YES they should share the curve and AttemptController, NO they shouldn't share theclassifyUpgradeError(different transport, different close vocabulary). Plan should decide whether to extract the classifier as a per-transport plugin or per-call-site.
Acceptance
-
@cluesmith/codev-coreexportsReconnectPolicy/BackoffController(or whatever names land at plan-approval) with full unit-test coverage in the core package's test suite. - All four call sites adopted:
vscode/connection-manager.ts,vscode/terminal-adapter.ts,dashboard/Terminal.tsx,agent-farm/tunnel-client.ts. Each call site shrinks; per-host behavior preserved per the plan's tuning decision. - The 6-vs-50 divergence resolved per the plan-gate decision and documented in the policy's defaults.
- Web terminal's session-unknown handling is either explicitly adopted or explicitly kept-as-is — no silent change.
- No regression to the VSCode terminal's terminal-adapter: WebSocket close-handler spams 'Connection lost' in a tight loop with no backoff, no give-up, no actual reconnect #936 behavior (backoff curve, give-up threshold, session-unknown fast-path, recovery affordance from vscode: add manual-reconnect affordance to terminal give-up state #939).
- Cross-package smoke test: kill Tower mid-session, confirm VSCode AND dashboard terminals both surface their give-up state with consistent timing.
Out of scope
- The recovery affordance work for vscode: add manual-reconnect affordance to terminal give-up state #939 (already shipped per its issue) and the terminal-adapter: WebSocket close-handler spams 'Connection lost' in a tight loop with no backoff, no give-up, no actual reconnect #936 fixes themselves (already shipped at the call sites).
- Replacing
EscapeBufferwith anything else — it's the precedent, not the target. - Standardising all error-handling across the three transports — only the reconnect-curve + classifier overlap is in scope here.
- Adding a new transport (e.g. WebRTC) — adopters only.
Why PIR
Two genuine design calls (#1 and #2 above) want plan-approval rather than being decided ad-hoc during implementation. Plus the change crosses three packages and the running-app verification needs both VSCode AND dashboard terminals exercised against a forced give-up — dev-approval gate is right for that.
Related
- terminal-adapter: WebSocket close-handler spams 'Connection lost' in a tight loop with no backoff, no give-up, no actual reconnect #936 — the terminal-adapter backoff fix that added the 3rd copy of the curve. This issue is the deferred consolidation.
- vscode: add manual-reconnect affordance to terminal give-up state #939 — the VSCode terminal recovery affordance. Surfaces the divergence-with-web that motivates this extract.
packages/core/src/escape-buffer.ts— the precedent for cross-host pure logic in core.
Activity
amrmelsayed commentedon Jun 2, 2026
On it! Working on this with the PIR protocol (plan + dev-approval gates before PR).
[PIR #961] Plan draft
[PIR #961] Add transport-agnostic ReconnectPolicy to codev-core
[PIR #961] Adopt ReconnectPolicy in vscode terminal + SSE clients
[PIR #961] Adopt ReconnectPolicy in web terminal; unify give-up at 6
[PIR #961] Reimplement tunnel calculateBackoff over shared curve
[PIR #961] Run core unit tests in CI; update thread
[PIR #961] Update thread: implement-phase status
[PIR #961] Align core tsconfig exclude with codev's convention
[PIR #961] Drop dormant web-terminal classifier seam (defer to #971)
[PIR #961] Update thread: dev-approval refinements (#967, #971)
[PIR #961] Review + retrospective
release notes: #961 web dashboard terminal reconnect parity with vscode