Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(renderer): throttle retries, drop rung, answer restore, local-close RPCs, SAS-timed wait (R41)

review3d386d35b2ba
diff --git a/src/renderer/bridge/frame-transport.test.ts b/src/renderer/bridge/frame-transport.test.ts
index 3cfbab36a..50126385b 100644
--- a/src/renderer/bridge/frame-transport.test.ts
+++ b/src/renderer/bridge/frame-transport.test.ts
@@ -1,6 +1,8 @@
-import { describe, it, expect } from 'vitest'
+import { describe, it, expect, vi } from 'vitest'
import { encodePtyData, E_DISCONNECTED } from '../../shared/rpc'
-import type { FrameTransport } from './frame-transport'
+import type { RelayClientApi } from '../../shared/types'
+import { RelayFrameTransport, type FrameTransport } from './frame-transport'
+import { emitLocalRelayClose } from './relay-local-close'
import { RpcClient } from './ws-bridge'
/**
@@ -85,3 +87,50 @@ describe('RpcClient over a FrameTransport', () => {
expect(closed).toBe(1)
})
})
+
+// ── R41: a relay connection this renderer closed itself ────────────────────────────────────────────
+
+describe('RelayFrameTransport: a close is a close, whoever made it', () => {
+ function relay() {
+ const mainClose = new Map<string, () => void>()
+ const api = {
+ onApproved: vi.fn(() => () => {}),
+ onClosed: vi.fn((id: string, cb: () => void) => {
+ mainClose.set(id, cb)
+ return () => {}
+ }),
+ send: vi.fn(),
+ onFrame: vi.fn(() => () => {})
+ } as unknown as RelayClientApi
+ return { api, mainClose }
+ }
+
+ it('fires on main\'s close, and on a close this renderer announced locally (main never reports that one)', () => {
+ const r = relay()
+ const onMain = vi.fn()
+ new RelayFrameTransport('conn-main', r.api).onClose(onMain)
+ r.mainClose.get('conn-main')!()
+ expect(onMain).toHaveBeenCalledTimes(1)
+ const onLocal = vi.fn()
+ new RelayFrameTransport('conn-local', r.api).onClose(onLocal)
+ emitLocalRelayClose('conn-local')
+ expect(onLocal).toHaveBeenCalledTimes(1)
+ })
+
+ it('fires once, however many closes arrive (a drop, then the offline teardown\'s own close)', () => {
+ const r = relay()
+ const cb = vi.fn()
+ new RelayFrameTransport('conn-both', r.api).onClose(cb)
+ r.mainClose.get('conn-both')!()
+ emitLocalRelayClose('conn-both')
+ expect(cb).toHaveBeenCalledTimes(1)
+ })
+
+ it('an RpcClient over it fails its in-flight requests on a local close', async () => {
+ const r = relay()
+ const client = new RpcClient(new RelayFrameTransport('conn-rpc', r.api))
+ const pending = client.request('workspace:load')
+ emitLocalRelayClose('conn-rpc')
+ await expect(pending).rejects.toMatchObject({ code: E_DISCONNECTED })
+ })
+})
diff --git a/src/renderer/bridge/frame-transport.ts b/src/renderer/bridge/frame-transport.ts
index a3059eb74..ba7acea58 100644
--- a/src/renderer/bridge/frame-transport.ts
+++ b/src/renderer/bridge/frame-transport.ts
@@ -9,6 +9,7 @@
// the RpcClient routes a string through `parseRpcMessage` and a `Uint8Array` through `decodePtyData`.
import type { RelayClientApi } from '../../shared/types'
+import { onLocalRelayClose } from './relay-local-close'
export interface FrameTransport {
/** Send one outbound frame (always a JSON string — pty-data is inbound only). */
@@ -94,7 +95,19 @@ export class RelayFrameTransport implements FrameTransport {
}
onClose(cb: () => void): void {
- this.relay.onClosed(this.connectionId, () => cb())
+ // Main's close (the socket dropped) or this renderer's own (relay-local-close.ts: main never
+ // reports a close it was asked for). Only hosted paths announce a local close, so a Team Access
+ // connection hears exactly what it always did. Once, whichever comes first.
+ let fired = false
+ let unLocal: () => void = () => {}
+ const fire = (): void => {
+ if (fired) return
+ fired = true
+ unLocal()
+ cb()
+ }
+ unLocal = onLocalRelayClose(this.connectionId, fire)
+ this.relay.onClosed(this.connectionId, () => fire())
}
ready(): Promise<void> {
diff --git a/src/renderer/bridge/hosted-gate.ts b/src/renderer/bridge/hosted-gate.ts
index f6dc85098..c381c8122 100644
--- a/src/renderer/bridge/hosted-gate.ts
+++ b/src/renderer/bridge/hosted-gate.ts
@@ -13,19 +13,33 @@
// wrong guess must err in. Only a relay tab joined by a hosted team's code is built with this; a
// Team Access relay tab keeps the plain RpcClient. See docs/hosted-team-relay.md.
import { hostedMayCall, hostedRoleRefusal } from '../../shared/hosted-access'
+import { E_DISCONNECTED } from '../../shared/rpc'
import type { HostedRole } from '../../shared/types'
import type { FrameTransport } from './frame-transport'
import { RpcClient } from './ws-bridge'
export class RoleGatedRpcClient extends RpcClient {
+ /** The connection is gone: nothing can answer a request any more. */
+ private closed = false
+
constructor(
transport: FrameTransport,
private readonly role: () => HostedRole | null
) {
super(transport)
+ this.onClose(() => {
+ this.closed = true
+ })
}
override request(method: string, ...args: unknown[]): Promise<unknown> {
+ if (this.closed) {
+ // A request sent into a closed hosted connection would wait forever (nothing will answer it,
+ // and nothing fails it later): fail it now, like the in-flight ones were (R41).
+ const gone = Promise.reject(Object.assign(new Error('The connection to the server was lost.'), { code: E_DISCONNECTED }))
+ gone.catch(() => {})
+ return gone
+ }
const role = this.role()
if (!hostedMayCall(role, method)) {
const refused = Promise.reject(hostedRoleRefusal(role))
@@ -38,7 +52,7 @@ export class RoleGatedRpcClient extends RpcClient {
}
override cast(method: string, ...args: unknown[]): void {
- if (!hostedMayCall(this.role(), method)) return
+ if (this.closed || !hostedMayCall(this.role(), method)) return
super.cast(method, ...args)
}
}
diff --git a/src/renderer/bridge/relay-api.test.ts b/src/renderer/bridge/relay-api.test.ts
index b4864d173..06ef50ecc 100644
--- a/src/renderer/bridge/relay-api.test.ts
+++ b/src/renderer/bridge/relay-api.test.ts
@@ -282,6 +282,16 @@ describe('buildRelayApi — hosted team tabs', () => {
expect(legacy).not.toHaveBeenCalled()
})
+ it('R41: once its connection closed, a hosted tab\'s requests fail at once instead of waiting forever', async () => {
+ const t = new FakeTransport()
+ const handle = buildRelayApi('conn-1', t, { hosted: true })
+ handle.setHostedRole!('owner')
+ ;(t as unknown as { closeCb: () => void }).closeCb()
+ await expect(handle.api.workspace.load()).rejects.toMatchObject({ code: 'E_DISCONNECTED' })
+ handle.api.pty.write('s1', 'x')
+ expect(t.sent).toEqual([])
+ })
+
it('a commenter may also chat; an editor sends everything', () => {
const t = new FakeTransport()
const handle = buildRelayApi('conn-1', t, { hosted: true })
diff --git a/src/renderer/bridge/relay-local-close.test.ts b/src/renderer/bridge/relay-local-close.test.ts
index c046ac7a7..b753f0e33 100644
--- a/src/renderer/bridge/relay-local-close.test.ts
+++ b/src/renderer/bridge/relay-local-close.test.ts
@@ -1,4 +1,6 @@
import { describe, it, expect, vi } from 'vitest'
+import { readFileSync, readdirSync, statSync } from 'node:fs'
+import { join } from 'node:path'
import { emitLocalRelayClose, onLocalRelayClose } from './relay-local-close'
describe('local relay close', () => {
@@ -23,3 +25,32 @@ describe('local relay close', () => {
expect(ok).toHaveBeenCalled()
})
})
+
+// Every relay transport now also hears a LOCAL close (frame-transport.ts), so a Team Access relay
+// tab stays byte-identical only because nothing ever announces one for it. That is a claim about
+// every call site, so it is checked here rather than trusted: each emit sits on a hosted-only path.
+
+describe('local relay close: announced only on hosted paths (the Team Access guarantee)', () => {
+ const root = join(__dirname, '..')
+ const files = (dir: string): string[] =>
+ readdirSync(dir).flatMap((n) => {
+ const p = join(dir, n)
+ return statSync(p).isDirectory() ? files(p) : /\.tsx?$/.test(n) && !/\.test\./.test(n) ? [p] : []
+ })
+ const callers = files(root)
+ .map((p) => ({ p: p.slice(root.length + 1).replace(/\\/g, '/'), src: readFileSync(p, 'utf8').replace(/\r\n/g, '\n') }))
+ .filter((f) => f.p !== 'bridge/relay-local-close.ts' && f.src.includes('emitLocalRelayClose('))
+
+ it('is emitted from exactly the hosted relay api close and two hosted-only Canvas paths', () => {
+ expect(callers.map((c) => c.p).sort()).toEqual(['bridge/relay-api.ts', 'canvas/Canvas.tsx'])
+ const relayApi = callers.find((c) => c.p === 'bridge/relay-api.ts')!.src
+ expect(relayApi.match(/emitLocalRelayClose\(/g)).toHaveLength(1)
+ expect(relayApi).toContain('if (hosted) emitLocalRelayClose(connectionId)')
+ const canvas = callers.find((c) => c.p === 'canvas/Canvas.tsx')!.src
+ expect(canvas.match(/emitLocalRelayClose\(/g)).toHaveLength(2)
+ expect(canvas).toContain('if (hosted) emitLocalRelayClose(connectionId)')
+ // The other one is the hosted joiner's own `disconnect` (it only ever holds hosted connections).
+ const joiner = canvas.slice(canvas.indexOf('const joiner = createHostedJoiner({'), canvas.indexOf('}, [confirmAndMount])'))
+ expect(joiner).toContain('emitLocalRelayClose(id)')
+ })
+})
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 62f753323..f4e201302 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -4196,7 +4196,7 @@ export function Canvas() {
label = 'Remote host',
reconnectProjectId?: string,
staleSessionId?: string,
- hosted?: { activate: boolean }
+ hosted?: { activate: boolean; onSasConfirmed?: () => void }
): Promise<HostedMountOutcome | null> => {
if (relayTabsRef.current.has(connectionId)) return Promise.resolve(null)
return openRelayTab(connectionId, label, {
@@ -4273,7 +4273,7 @@ export function Canvas() {
label: string,
reconnectProjectId?: string,
staleSessionId?: string,
- hosted?: { activate: boolean }
+ hosted?: { activate: boolean; onSasConfirmed?: () => void }
): Promise<HostedMountOutcome | null> => {
const unSas = window.nodeTerminal.relayClient.onSas(connectionId, (sas) => {
unSas()
@@ -4284,6 +4284,8 @@ export function Canvas() {
: `Verify this code matches the one shown on the host:\n\n${sas}`
if (sas && window.confirm(question)) {
window.nodeTerminal.relayClient.confirm(connectionId)
+ // Nothing left for this user to do: the owner's wait starts now (the joiner says so).
+ hosted?.onSasConfirmed?.()
} else {
// Deliberate decline: mark it so the bootstrap's close-reject isn't surfaced as an error.
cancelledConnsRef.current.add(connectionId)
@@ -4316,6 +4318,7 @@ export function Canvas() {
}
},
onApproved: (id, listener) => window.nodeTerminal.relayClient.onApproved(id, listener),
+ onSas: (id, listener) => window.nodeTerminal.relayClient.onSas(id, () => listener()),
// A connection the joiner gives up on (its tab closed mid-approval, a cancelled late connect):
// main never reports a close it was asked for, so it is announced here too.
disconnect: (id) => {
@@ -4324,7 +4327,7 @@ export function Canvas() {
},
bookmarks: async () => (await window.nodeTerminal.relayHosted?.bookmarks()) ?? [],
removeBookmark: (hostId) => window.nodeTerminal.relayHosted.removeBookmark(hostId),
- mount: (connectionId, req) => {
+ mount: (connectionId, req, hooks) => {
const bound = req.reconnectProjectId ? sessionForProject(req.reconnectProjectId) : null
return confirmAndMount(
connectionId,
@@ -4332,7 +4335,7 @@ export function Canvas() {
req.reconnectProjectId,
bound?.source === 'relay' ? bound.id : undefined,
// Only a reconnect the user asked for takes the screen (or one with nothing on it).
- { activate: req.manual || !useProjects.getState().activeProjectId }
+ { activate: req.manual || !useProjects.getState().activeProjectId, onSasConfirmed: hooks.sasConfirmed }
).then((o): HostedMountOutcome => o ?? { error: new Error('That connection is already open.'), declined: true })
},
tabOpen: isOpenTab,
diff --git a/src/renderer/canvas/hosted-team.source.test.ts b/src/renderer/canvas/hosted-team.source.test.ts
index 39014e9b8..490f65652 100644
--- a/src/renderer/canvas/hosted-team.source.test.ts
+++ b/src/renderer/canvas/hosted-team.source.test.ts
@@ -97,4 +97,12 @@ describe('hosted team glue in Canvas', () => {
expect(body).toContain('{ begin: q.beginAnswer, finish: q.finishAnswer }')
expect(body).not.toContain('.settle(')
})
+
+ it('R41: the owner\'s wait starts only once the SAS was confirmed (hosted), and the joiner hears the SAS', () => {
+ const body = between('const confirmAndMount = useCallback(', 'return mountRemoteMirror(')
+ expect(body.indexOf('window.nodeTerminal.relayClient.confirm(connectionId)')).toBeLessThan(body.indexOf('hosted?.onSasConfirmed?.()'))
+ const joiner = between('const joiner = createHostedJoiner({', '}, [confirmAndMount])')
+ expect(joiner).toContain('onSas: (id, listener) =>')
+ expect(joiner).toContain('onSasConfirmed: hooks.sasConfirmed')
+ })
})
diff --git a/src/renderer/lib/hostedAttempts.test.ts b/src/renderer/lib/hostedAttempts.test.ts
index 3cf7e566d..685bea8d7 100644
--- a/src/renderer/lib/hostedAttempts.test.ts
+++ b/src/renderer/lib/hostedAttempts.test.ts
@@ -17,9 +17,11 @@ function harness() {
const ended: Array<{ req: HostedAttemptRequest; projectId: string; reason?: RelayClosedReason }> = []
const disconnected: string[] = []
const exhausted: HostedAttemptRequest[] = []
+ const throttledSaid: HostedAttemptRequest[] = []
const wanted = { value: true }
const deps: HostedAttemptDeps = {
exhausted: (req) => exhausted.push(req),
+ throttled: (req) => throttledSaid.push(req),
wanted: () => wanted.value,
connect: (code) => new Promise((resolve, reject) => connects.push({ code, resolve, reject })),
mount: (id, req) => new Promise((resolve) => mounts.push({ id, req, resolve })),
@@ -46,7 +48,7 @@ function harness() {
t.fn()
}
const armed = () => timers.filter((t) => !t.cleared)
- return { deps, connects, mounts, closeCbs, unsubs, timers, armed, fire, stopped, ended, disconnected, exhausted, wanted }
+ return { deps, connects, mounts, closeCbs, unsubs, timers, armed, fire, stopped, ended, disconnected, exhausted, wanted, throttledSaid }
}
const boot = (over: Partial<HostedAttemptRequest> = {}): HostedAttemptRequest => ({
@@ -454,4 +456,92 @@ describe('hosted attempts: one attempt and one live connection per team (R38/R39
await flush()
expect(h.mounts[0].req.reconnectProjectId).toBe('proj-1')
})
+
+ // ── R41 ─────────────────────────────────────────────────────────────────────────────────────────
+ it('R41: a throttle is retried after at least a minute, or the Retry-After when longer, and joins the 60 s tail', async () => {
+ const h = harness()
+ const a = createHostedAttempts(h.deps)
+ a.run(boot())
+ h.connects[0].reject(wrap('[E_JOIN_THROTTLED] limiting'))
+ await flush()
+ expect(a.phase('H1')).toBe('waiting')
+ expect(h.armed()[0].ms).toBe(60_000)
+ h.fire()
+ h.connects[1].reject(wrap('[E_JOIN_THROTTLED] limiting [retry-after:150]'))
+ await flush()
+ expect(h.armed()[0].ms).toBe(150_000)
+ h.fire()
+ h.connects[2].reject(wrap('[E_JOIN_THROTTLED] limiting [retry-after:5]'))
+ await flush()
+ expect(h.armed()[0].ms).toBe(60_000) // never faster than once a minute
+ h.fire()
+ // A network failure right after a throttle stays on the tail: no burst back down to 1 s.
+ h.connects[3].reject(wrap('[E_JOIN_NETWORK] x'))
+ await flush()
+ expect(h.armed()[0].ms).toBe(60_000)
+ expect(h.stopped).toEqual([])
+ })
+
+ it('R41: a throttle streak is announced once; a new streak (after the service answered) again', async () => {
+ const h = harness()
+ const a = createHostedAttempts(h.deps)
+ a.run(boot())
+ for (let i = 0; i < 3; i++) {
+ h.connects[i].reject(wrap('[E_JOIN_THROTTLED] limiting'))
+ await flush()
+ h.fire()
+ }
+ expect(h.throttledSaid).toHaveLength(1)
+ h.connects[3].resolve('c3') // the service answered: the streak is over
+ await flush()
+ h.mounts[0].resolve({ retry: true })
+ await flush()
+ h.fire()
+ h.connects[4].reject(wrap('[E_JOIN_THROTTLED] limiting'))
+ await flush()
+ expect(h.throttledSaid).toHaveLength(2)
+ })
+
+ it('R41: a throttle on an attempt that asked not to retry (a pasted code) is told, not looped', async () => {
+ const h = harness()
+ const a = createHostedAttempts(h.deps)
+ a.run(boot({ manual: true, retry: false }))
+ h.connects[0].reject(wrap('[E_JOIN_THROTTLED] limiting'))
+ await flush()
+ expect(h.armed()).toEqual([])
+ expect(h.stopped[0].f.code).toBe('E_JOIN_THROTTLED')
+ expect(h.throttledSaid).toEqual([])
+ })
+
+ it('R41: a reconnect after a drop waits the 1 s rung first, then spends the rest of a fresh drop budget', async () => {
+ const h = harness()
+ const a = createHostedAttempts(h.deps)
+ a.run(boot({ reconnectProjectId: 'proj-1', afterDrop: true }))
+ expect(h.connects).toHaveLength(0)
+ expect(a.phase('H1')).toBe('waiting')
+ const delays = [h.armed()[0].ms]
+ h.fire()
+ for (let i = 0; i < 5; i++) {
+ h.connects[i].resolve(`c${i}`)
+ await flush()
+ h.mounts[i].resolve({ retry: true })
+ await flush()
+ if (i < 4) {
+ delays.push(h.armed()[0].ms)
+ h.fire()
+ }
+ }
+ expect(delays).toEqual([1000, 2000, 4000, 8000, 15000])
+ expect(h.connects).toHaveLength(5)
+ expect(h.exhausted).toHaveLength(1)
+ })
+
+ it('R41: a click during that first second hurries it (the user asked)', async () => {
+ const h = harness()
+ const a = createHostedAttempts(h.deps)
+ a.run(boot({ reconnectProjectId: 'proj-1', afterDrop: true }))
+ expect(a.run(boot({ manual: true, reconnectProjectId: 'proj-1' }))).toBe('started')
+ expect(h.connects).toHaveLength(1)
+ expect(h.armed()).toEqual([])
+ })
})
diff --git a/src/renderer/lib/hostedAttempts.ts b/src/renderer/lib/hostedAttempts.ts
index bfbc63219..cf49908e2 100644
--- a/src/renderer/lib/hostedAttempts.ts
+++ b/src/renderer/lib/hostedAttempts.ts
@@ -11,14 +11,20 @@
//
// Why only network failures retry (R35): every other code needs a person (a fresh code, an unlocked
// keyring, an owner) or a new day, and a loop on one of them spends the team's damped device mints.
-// A connection that drops before the host answered (a host restarting) is the one other retry, and
-// it is BOUNDED (R40): the 1/2/4/8/15 s steps, then it stops and says so once — every try mints a
-// join token and opens a relay socket. And no attempt outlives its tab: `wanted` is asked before
-// every retry and before every mount, and closing the tab cancels the attempt in any phase.
+// The service's per-network limiter (E_JOIN_THROTTLED, a 429 that clears within a minute) retries
+// too, but never sooner than a minute (or its Retry-After), on the 60 s tail, announced once per
+// streak (R41). A connection that drops before the host answered (a host restarting) is the other
+// retry, and it is BOUNDED (R40): the 1/2/4/8/15 s steps, then it stops and says so once — every
+// try mints a join token and opens a relay socket. A reconnect after a LIVE connection dropped
+// starts on the first rung, never at once (R41). And no attempt outlives its tab: `wanted` is asked
+// before every retry and before every mount, and closing the tab cancels the attempt in any phase.
// Pure orchestration over injected deps, so every rule is testable without React or a relay.
// See docs/hosted-team-relay.md.
import type { RelayClosedReason } from '@shared/types'
-import { classifyJoinFailure, DROP_RETRY_MAX, reconnectDelayMs, type JoinFailure } from './hostedTeam'
+import { classifyJoinFailure, DROP_RETRY_MAX, reconnectDelayMs, THROTTLE_MIN_DELAY_MS, type JoinFailure } from './hostedTeam'
+
+/** The ladder rung of the 60 s tail (1/2/4/8/15 s come before it). */
+const TAIL_RUNG = 5
export interface HostedAttemptRequest {
hostId: string
@@ -33,6 +39,12 @@ export interface HostedAttemptRequest {
retry: boolean
/** Reconnect onto this existing tab instead of opening a new one. */
reconnectProjectId?: string
+ /** This reconnect follows a live connection's drop: it waits the first rung (1 s) before its
+ * first try, which counts against the drop budget — a host that approves and then drops must
+ * never make a tight loop (R41). */
+ afterDrop?: boolean
+ /** A bookmarked reconnect: this side confirms on its own (no SAS prompt is expected). */
+ autoConfirm?: boolean
}
export type HostedAttemptPhase = 'connecting' | 'waiting' | 'mounting' | 'live'
@@ -59,6 +71,9 @@ export interface HostedAttemptDeps {
/** An unattended attempt used up its retries for a drop the host did not explain (R40). Called
* once; the team is released, and the next try is the user's. */
exhausted(req: HostedAttemptRequest): void
+ /** An unattended attempt is being held back by the service's per-network limiter and will retry
+ * in a minute. Called once per streak of throttles (R41). */
+ throttled(req: HostedAttemptRequest): void
/** Is this attempt still wanted? Asked before every retry and before every mount — an attempt
* reconnecting a tab that has since been closed or deleted must stop (R40). Default: yes. */
wanted?(req: HostedAttemptRequest): boolean
@@ -88,6 +103,8 @@ interface Entry {
attempt: number
/** Retries spent on drops the host did not explain (bounded by DROP_RETRY_MAX). */
drops: number
+ /** This streak of throttles has been announced. Ends when the service lets a connect through. */
+ throttleSaid: boolean
timer: unknown
connectionId: string | null
projectId: string | null
@@ -153,6 +170,7 @@ export function createHostedAttempts(deps: HostedAttemptDeps): HostedAttempts {
deps.disconnect(connectionId)
return
}
+ e.throttleSaid = false // the service let this one through
if (!wanted(e)) {
// Its tab went away while it connected: never bind this connection to it.
deps.disconnect(connectionId)
@@ -210,6 +228,17 @@ export function createHostedAttempts(deps: HostedAttemptDeps): HostedAttempts {
(err) => {
if (!current(e)) return
const failure = classifyJoinFailure(err instanceof Error ? err.message : String(err))
+ if (failure.throttled && e.req.retry) {
+ // The per-network limiter clears within a minute: wait at least that long (or its
+ // Retry-After), and stay on the 60 s tail after it — never a burst back down to 1 s.
+ if (!e.throttleSaid) {
+ e.throttleSaid = true
+ deps.throttled(e.req)
+ }
+ e.attempt = Math.max(e.attempt, TAIL_RUNG)
+ retryAfter(e, Math.max(THROTTLE_MIN_DELAY_MS, failure.retryAfterMs ?? 0))
+ return
+ }
if (failure.retry && e.req.retry) {
backOff(e)
return
@@ -239,9 +268,15 @@ export function createHostedAttempts(deps: HostedAttemptDeps): HostedAttempts {
attempt(existing)
return 'started'
}
- const e: Entry = { req, phase: 'connecting', attempt: 0, drops: 0, timer: null, connectionId: null, projectId: null, unClose: null, closedEarly: null }
+ const e: Entry = { req, phase: 'connecting', attempt: 0, drops: 0, throttleSaid: false, timer: null, connectionId: null, projectId: null, unClose: null, closedEarly: null }
entries.set(req.hostId, e)
- attempt(e)
+ if (req.afterDrop) {
+ // Never straight back after a drop: the first rung, and it is the drop budget's first retry.
+ e.drops = 1
+ retryAfter(e, reconnectDelayMs(0))
+ } else {
+ attempt(e)
+ }
return 'started'
},
phase(hostId) {
diff --git a/src/renderer/lib/hostedJoin.test.ts b/src/renderer/lib/hostedJoin.test.ts
index f36808efc..7b433abb9 100644
--- a/src/renderer/lib/hostedJoin.test.ts
+++ b/src/renderer/lib/hostedJoin.test.ts
@@ -3,7 +3,7 @@ import type { RelayClosedReason } from '@shared/types'
import { JOIN_CODE_PREFIX } from '@shared/relay-join-code'
import { createHostedJoiner, type HostedJoinerDeps, type HostedMountOutcome, type HostedNotice } from './hostedJoin'
import type { HostedAttemptRequest } from './hostedAttempts'
-import { RelayApprovalError } from './hostedTeam'
+import { RelayApprovalError, THROTTLED_NOTICE } from './hostedTeam'
import { WAITING_NOTICE_DELAY_MS } from './hostedJoin'
const wrap = (m: string) => new Error(`Error invoking remote method 'relay:client:connect': Error: ${m}`)
@@ -16,7 +16,8 @@ type Bookmark = { hostId: string; label: string; approved: boolean; code: string
function harness(bookmarks: Bookmark[] = []) {
const connects: Array<{ code: string; resolve: (id: string) => void; reject: (e: Error) => void }> = []
- const mounts: Array<{ id: string; req: HostedAttemptRequest; resolve: (o: HostedMountOutcome) => void }> = []
+ const mounts: Array<{ id: string; req: HostedAttemptRequest; resolve: (o: HostedMountOutcome) => void; hooks: { sasConfirmed(): void } }> = []
+ const sasCbs = new Map<string, () => void>()
const closeCbs = new Map<string, (reason?: RelayClosedReason) => void>()
const notices: HostedNotice[] = []
const timers: Array<{ fn: () => void; ms: number; cleared: boolean }> = []
@@ -40,7 +41,11 @@ function harness(bookmarks: Bookmark[] = []) {
removeBookmark: vi.fn(async (hostId: string) => {
list = list.filter((b) => b.hostId !== hostId)
}),
- mount: (id, req) => new Promise((resolve) => mounts.push({ id, req, resolve })),
+ mount: (id, req, hooks) => new Promise((resolve) => mounts.push({ id, req, resolve, hooks })),
+ onSas: (id, cb) => {
+ sasCbs.set(id, cb)
+ return () => sasCbs.delete(id)
+ },
tabOpen: (projectId) => open.has(projectId),
notify: (n) => notices.push(n),
promptForCode: vi.fn(async () => null as string | null),
@@ -53,7 +58,7 @@ function harness(bookmarks: Bookmark[] = []) {
;(t as { cleared: boolean }).cleared = true
}
}
- return { deps, connects, mounts, closeCbs, notices, timers, open, approvedCbs, cleared, setList: (l: Bookmark[]) => { list = l } }
+ return { deps, connects, mounts, closeCbs, notices, timers, open, approvedCbs, cleared, sasCbs, setList: (l: Bookmark[]) => { list = l } }
}
/** Connect + mount one attempt to a live tab. */
@@ -119,6 +124,10 @@ describe('hosted joiner', () => {
await goLive(h, 0, 'proj-1')
h.closeCbs.get('c0')!(undefined)
await flush()
+ // R41: never at once — the first rung (1 s) first, so a host that approves and drops cannot spin.
+ expect(h.connects).toHaveLength(1)
+ expect(h.timers.filter((t) => !t.cleared && t.ms !== WAITING_NOTICE_DELAY_MS).map((t) => t.ms)).toEqual([1000])
+ fireRetry(h)
expect(h.connects).toHaveLength(2)
await goLive(h, 1, 'proj-1')
expect(h.mounts[1].req).toMatchObject({ reconnectProjectId: 'proj-1', manual: false, retry: true })
@@ -301,19 +310,20 @@ describe('hosted joiner', () => {
await goLive(h, 0, 'proj-1')
h.closeCbs.get('c0')!(undefined) // the drop
await flush()
- for (let i = 1; i <= 6; i++) {
+ fireRetry(h) // the first rung (1 s) is the budget's first retry (R41)
+ for (let i = 1; i <= 5; i++) {
h.connects[i].resolve(`c${i}`)
await flush()
h.mounts[i].resolve({ error: new RelayApprovalError('The relay connection closed before it was approved.'), declined: false })
await flush()
- if (i < 6) fireRetry(h)
+ if (i < 5) fireRetry(h)
}
- expect(h.connects).toHaveLength(7) // the drop's reconnect + 5 retries
+ expect(h.connects).toHaveLength(6) // the first connection + 5 tries after the drop (1/2/4/8/15 s)
expect(h.notices).toEqual([{ kind: 'error', text: "Couldn't reconnect to box. Click its tab to try again." }])
expect(h.timers.filter((t) => !t.cleared && t.ms !== WAITING_NOTICE_DELAY_MS)).toEqual([])
// The tab stays greyed and clickable: a click is a fresh try.
expect(j.reconnectTab('proj-1')).toBe(true)
- expect(h.connects).toHaveLength(8)
+ expect(h.connects).toHaveLength(7)
})
it('R40: a boot reconnect that gives up (no tab yet) says how to try again', async () => {
@@ -337,6 +347,7 @@ describe('hosted joiner', () => {
await goLive(h, 0, 'proj-1')
h.closeCbs.get('c0')!(undefined)
await flush()
+ fireRetry(h)
h.connects[1].reject(wrap('[E_JOIN_NETWORK] x'))
await flush()
j.tabClosed('proj-1')
@@ -357,6 +368,7 @@ describe('hosted joiner', () => {
await goLive(h, 0, 'proj-1')
h.closeCbs.get('c0')!(undefined)
await flush()
+ fireRetry(h)
expect(h.connects).toHaveLength(2) // the in-place reconnect is connecting
j.tabClosed('proj-1')
h.open.delete('proj-1')
@@ -375,6 +387,7 @@ describe('hosted joiner', () => {
await goLive(h, 0, 'proj-1')
h.closeCbs.get('c0')!(undefined)
await flush()
+ fireRetry(h)
h.connects[1].resolve('c1')
await flush()
j.tabClosed('proj-1')
@@ -466,9 +479,11 @@ describe('hosted joiner', () => {
h.connects[0].resolve('c0')
await flush()
h.approvedCbs.get('c0')!() // approved; the mount is still loading the workspace
- const wait = h.timers.find((t) => t.ms === WAITING_NOTICE_DELAY_MS && !t.cleared)!
- wait.cleared = true
- wait.fn()
+ // The approval disarmed the clock: nothing is left to fire, however long the load takes…
+ expect(h.timers.filter((t) => t.ms === WAITING_NOTICE_DELAY_MS && !t.cleared)).toEqual([])
+ // …and a confirm that lands after it (a SAS answered late) does not re-arm it.
+ h.mounts[0].hooks.sasConfirmed()
+ expect(h.timers.filter((t) => t.ms === WAITING_NOTICE_DELAY_MS && !t.cleared)).toEqual([])
expect(h.notices).toEqual([])
})
@@ -485,4 +500,50 @@ describe('hosted joiner', () => {
expect(h.notices).toEqual([])
expect(h.cleared).toEqual([])
})
+
+ // ── R41 ─────────────────────────────────────────────────────────────────────────────────────────
+ it('R41: a throttled reconnect says so ONCE ("retrying in a minute") and keeps trying', async () => {
+ const h = harness([{ hostId: 'H1', label: 'box', approved: true, code: codeFor('H1') }])
+ const j = createHostedJoiner(h.deps)
+ await j.bootReconnect()
+ for (let i = 0; i < 3; i++) {
+ h.connects[i].reject(wrap('[E_JOIN_THROTTLED] The nodeterm service is limiting requests from this network.'))
+ await flush()
+ fireRetry(h)
+ }
+ expect(h.notices).toEqual([{ kind: 'info', text: THROTTLED_NOTICE }])
+ expect(h.connects).toHaveLength(4)
+ })
+
+ it('R41: a first join arms the waiting notice only after its SAS was confirmed — never before the prompt', async () => {
+ const h = harness()
+ const j = createHostedJoiner(h.deps)
+ j.joinWithCode(codeFor('H1'))
+ h.connects[0].resolve('c0')
+ await flush()
+ const waits = () => h.timers.filter((t) => t.ms === WAITING_NOTICE_DELAY_MS && !t.cleared)
+ expect(waits()).toEqual([]) // the SAS prompt has not even come up yet
+ h.sasCbs.get('c0')!() // the SAS arrives (the prompt is on screen)
+ expect(waits()).toEqual([])
+ h.mounts[0].hooks.sasConfirmed() // the user compared and confirmed
+ expect(waits()).toHaveLength(1)
+ const t = waits()[0]
+ t.cleared = true
+ t.fn()
+ expect(h.notices).toEqual([{ kind: 'info', text: 'Waiting for an owner of box to approve this device…', sticky: true }])
+ })
+
+ it('R41: a bookmarked reconnect arms it when its connect resolves; a SAS that shows up after all disarms it', async () => {
+ const h = harness([{ hostId: 'H1', label: 'box', approved: true, code: codeFor('H1') }])
+ const j = createHostedJoiner(h.deps)
+ await j.bootReconnect()
+ h.connects[0].resolve('c0')
+ await flush()
+ const waits = () => h.timers.filter((t) => t.ms === WAITING_NOTICE_DELAY_MS && !t.cleared)
+ expect(waits()).toHaveLength(1)
+ h.sasCbs.get('c0')!() // the host asked for a comparison after all (its approval was withdrawn)
+ expect(waits()).toEqual([])
+ h.mounts[0].hooks.sasConfirmed()
+ expect(waits()).toHaveLength(1)
+ })
})
diff --git a/src/renderer/lib/hostedJoin.ts b/src/renderer/lib/hostedJoin.ts
index ea5278a44..4a3e4c4fa 100644
--- a/src/renderer/lib/hostedJoin.ts
+++ b/src/renderer/lib/hostedJoin.ts
@@ -22,6 +22,7 @@ import {
mountFailureMessage,
mountFailureRetries,
stripIpcPrefix,
+ THROTTLED_NOTICE,
waitingForOwnerText
} from './hostedTeam'
@@ -48,13 +49,16 @@ export interface HostedJoinerDeps {
onClosed(connectionId: string, listener: (reason?: RelayClosedReason) => void): () => void
/** Both humans approved this connection (`relayClient.onApproved`). */
onApproved(connectionId: string, listener: () => void): () => void
+ /** This connection's SAS arrived, i.e. a comparison prompt is about to show (`relayClient.onSas`). */
+ onSas(connectionId: string, listener: () => void): () => void
/** Close a connection this joiner no longer wants (it must also count as closed locally). */
disconnect(connectionId: string): void
bookmarks: RelayHostedApi['bookmarks']
removeBookmark: RelayHostedApi['removeBookmark']
/** The SAS (a first join), the owner's approval, the tab. Never rejects on purpose; a rejection
- * is read as a failure with that error. */
- mount(connectionId: string, req: HostedAttemptRequest): Promise<HostedMountOutcome>
+ * is read as a failure with that error. `hooks.sasConfirmed` is called once this user confirmed
+ * the SAS (only a connection that asked for one). */
+ mount(connectionId: string, req: HostedAttemptRequest, hooks: { sasConfirmed(): void }): Promise<HostedMountOutcome>
/** Is this project still an open tab (not closed, not deleted)? */
tabOpen(projectId: string): boolean
notify(notice: HostedNotice): void
@@ -114,6 +118,9 @@ export function createHostedJoiner(deps: HostedJoinerDeps): HostedJoiner {
setTimer: deps.setTimer,
clearTimer: deps.clearTimer,
wanted: tabWanted,
+ throttled() {
+ deps.notify({ kind: 'info', text: THROTTLED_NOTICE })
+ },
exhausted(req) {
deps.notify({
kind: 'error',
@@ -124,31 +131,47 @@ export function createHostedJoiner(deps: HostedJoinerDeps): HostedJoiner {
},
async mount(connectionId, req): Promise<HostedMountResult> {
// Any hosted mount still waiting for approval after a moment says so — a first join and a
- // bookmarked reconnect alike (the host may no longer know this device and ask an owner).
+ // bookmarked reconnect alike (the host may no longer know this device and ask an owner). The
+ // clock starts when there is nothing left for THIS user to do: once they confirmed the SAS
+ // (a first join), or once the connection exists (a bookmarked reconnect, which confirms on
+ // its own). Never before a SAS prompt: a comparison that shows up disarms it (R41).
const waiting = waitingForOwnerText(req.label)
let approved = false
let shown = false
- const unApproved = deps.onApproved(connectionId, () => {
- approved = true
+ let waitTimer: unknown = null
+ const disarm = (): void => {
+ if (waitTimer !== null) deps.clearTimer(waitTimer)
+ waitTimer = null
if (shown) {
shown = false
deps.clearNotice(waiting)
}
- })
- const waitTimer = deps.setTimer(() => {
+ }
+ const arm = (): void => {
+ disarm()
if (approved) return
- shown = true
- deps.notify({ kind: 'info', text: waiting, sticky: true })
- }, WAITING_NOTICE_DELAY_MS)
+ waitTimer = deps.setTimer(() => {
+ waitTimer = null
+ if (approved) return
+ shown = true
+ deps.notify({ kind: 'info', text: waiting, sticky: true })
+ }, WAITING_NOTICE_DELAY_MS)
+ }
+ const unApproved = deps.onApproved(connectionId, () => {
+ approved = true
+ disarm()
+ })
+ const unSas = deps.onSas(connectionId, disarm)
+ if (req.autoConfirm) arm()
let outcome: HostedMountOutcome
try {
- outcome = await deps.mount(connectionId, req)
+ outcome = await deps.mount(connectionId, req, { sasConfirmed: arm })
} catch (error) {
outcome = { error, declined: false }
} finally {
- deps.clearTimer(waitTimer)
unApproved()
- if (shown) deps.clearNotice(waiting)
+ unSas()
+ disarm()
}
if ('projectId' in outcome) {
tabs.set(outcome.projectId, { hostId: req.hostId, code: req.code, label: req.label })
@@ -185,7 +208,7 @@ export function createHostedJoiner(deps: HostedJoinerDeps): HostedJoiner {
(list) => {
const b = list.find((x) => x.hostId === req.hostId)
if (!b?.approved || !deps.tabOpen(projectId)) return
- run({ hostId: b.hostId, code: b.code, label: b.label, manual: false, retry: true, reconnectProjectId: projectId })
+ run({ hostId: b.hostId, code: b.code, label: b.label, manual: false, retry: true, reconnectProjectId: projectId, afterDrop: true, autoConfirm: true })
},
() => {}
)
@@ -240,7 +263,7 @@ export function createHostedJoiner(deps: HostedJoinerDeps): HostedJoiner {
)
return true
}
- if (run({ hostId: t.hostId, code: t.code, label: t.label, manual: true, retry: true, reconnectProjectId: projectId }) === 'busy') {
+ if (run({ hostId: t.hostId, code: t.code, label: t.label, manual: true, retry: true, reconnectProjectId: projectId, autoConfirm: true }) === 'busy') {
deps.notify({ kind: 'info', text: `Already reconnecting to ${team(t.label)}…` })
}
return true
@@ -255,7 +278,7 @@ export function createHostedJoiner(deps: HostedJoinerDeps): HostedJoiner {
async bootReconnect() {
const list = await deps.bookmarks().catch(() => [])
for (const b of list) {
- if (b.approved) run({ hostId: b.hostId, code: b.code, label: b.label, manual: false, retry: true })
+ if (b.approved) run({ hostId: b.hostId, code: b.code, label: b.label, manual: false, retry: true, autoConfirm: true })
}
},
async forget(hostId, label) {
diff --git a/src/renderer/lib/hostedOwner.test.ts b/src/renderer/lib/hostedOwner.test.ts
index bd02e7b0f..1e3d8723d 100644
--- a/src/renderer/lib/hostedOwner.test.ts
+++ b/src/renderer/lib/hostedOwner.test.ts
@@ -37,7 +37,8 @@ const sink = () => {
const added: QueuedRequest[] = []
const closed: Array<[string, string]> = []
const dropped: string[] = []
- return { added, closed, dropped, add: (i: QueuedRequest) => added.push(i), close: (id: string, r: HostedPendingClosedReason) => closed.push([id, r]), drop: (p: string) => dropped.push(p) }
+ const attached: string[] = []
+ return { added, closed, dropped, attached, attach: (p: string) => attached.push(p), add: (i: QueuedRequest) => added.push(i), close: (id: string, r: HostedPendingClosedReason) => closed.push([id, r]), drop: (p: string) => dropped.push(p) }
}
const P = (id: string, since = 1): HostedPending => ({ pendingId: id, sas: '1 2', peerKeyB64: 'K', since })
const flush = () => new Promise((r) => setTimeout(r, 0))
@@ -47,6 +48,7 @@ describe('attachHostedOwner (R25)', () => {
const f = fakeHosted([P('pulled')])
const s = sink()
attachHostedOwner(f.hosted, { projectId: 'p1', teamLabel: 'box' }, s)
+ expect(s.attached).toEqual(['p1']) // its failed answers may go back on screen while attached (R41)
expect(f.calls).toEqual(['sub:pending', 'sub:closed', 'pull'])
f.push(P('pushed'))
await flush()
@@ -86,7 +88,11 @@ describe('answerHostedRequest', () => {
const item = (hosted: HostedSessionApi): QueuedRequest => ({ projectId: 'p1', teamLabel: 'box', pending: P('x'), answerer: hosted })
const ledger = () => {
const log: string[] = []
- return { log, begin: (id: string) => log.push(`begin:${id}`), finish: (id: string, landed: boolean) => log.push(`finish:${id}:${landed}`) }
+ return {
+ log,
+ begin: (id: string) => log.push(`begin:${id}`),
+ finish: (item: QueuedRequest, landed: boolean) => log.push(`finish:${item.pending.pendingId}:${landed}`)
+ }
}
it('R40: settles only once the host ANSWERED (true or false); a rejection leaves the request answerable', async () => {
diff --git a/src/renderer/lib/hostedOwner.ts b/src/renderer/lib/hostedOwner.ts
index dc7ac59f7..233b58dd2 100644
--- a/src/renderer/lib/hostedOwner.ts
+++ b/src/renderer/lib/hostedOwner.ts
@@ -9,6 +9,8 @@ import type { QueuedRequest } from './hostedPendingQueue'
import { stripIpcPrefix } from './hostedTeam'
export interface OwnerQueueSink {
+ /** This owner tab's subscription is live. */
+ attach(projectId: string): void
add(item: QueuedRequest): void
close(pendingId: string, reason: HostedPendingClosedReason): void
/** The tab is going away: drop its requests. */
@@ -22,6 +24,7 @@ export function attachHostedOwner(
sink: OwnerQueueSink
): () => void {
let live = true
+ sink.attach(ctx.projectId)
const add = (pending: unknown): void => {
if (live) sink.add({ projectId: ctx.projectId, teamLabel: ctx.teamLabel, pending: pending as QueuedRequest['pending'], answerer: hosted })
}
@@ -52,7 +55,7 @@ export type HostedAnswer = { kind: 'approve'; role: HostedRole } | { kind: 'deny
export interface AnswerLedger {
begin(pendingId: string): void
/** `landed` = the host answered (true or false); false = it never did. */
- finish(pendingId: string, landed: boolean): void
+ finish(item: QueuedRequest, landed: boolean): void
}
/** Send an owner's answer. The request leaves the screen at once, and is settled only if the host
@@ -67,11 +70,11 @@ export async function answerHostedRequest(
ledger.begin(id)
try {
const ok = answer.kind === 'approve' ? await item.answerer.approve(id, answer.role) : await item.answerer.deny(id)
- ledger.finish(id, true)
+ ledger.finish(item, true)
// False is an answer, not a failure: another owner got there first, or the device left.
return ok ? null : { kind: 'info', text: 'That request was already answered or has gone.' }
} catch (err) {
- ledger.finish(id, false)
+ ledger.finish(item, false)
return { kind: 'error', text: `Could not answer the request: ${stripIpcPrefix(err instanceof Error ? err.message : String(err))}` }
}
}
diff --git a/src/renderer/lib/hostedPendingQueue.test.ts b/src/renderer/lib/hostedPendingQueue.test.ts
index 5fdb301c4..6c2d285f6 100644
--- a/src/renderer/lib/hostedPendingQueue.test.ts
+++ b/src/renderer/lib/hostedPendingQueue.test.ts
@@ -8,6 +8,7 @@ import {
headRequest,
beginAnswer,
finishAnswer,
+ attachProject,
SETTLED_MEMORY,
type QueuedRequest
} from './hostedPendingQueue'
@@ -97,15 +98,42 @@ describe('hosted pending queue (R37)', () => {
q = beginAnswer(q, 'a')
expect(headRequest(q)?.pending.pendingId).toBe('b')
expect(addRequest(q, req('a', 10)).items.map((i) => i.pending.pendingId)).toEqual(['b']) // a replay mid-flight
- q = finishAnswer(q, 'a', true)
+ q = finishAnswer(q, req('a', 10), true)
expect(addRequest(q, req('a', 10)).items.map((i) => i.pending.pendingId)).toEqual(['b'])
})
it('R40: an answer that never landed (the tab dropped) stays answerable — the reconnect\'s pull brings it back', () => {
let q = addRequest(EMPTY_PENDING_QUEUE, req('a', 10))
q = beginAnswer(q, 'a')
- q = finishAnswer(q, 'a', false)
+ q = finishAnswer(q, req('a', 10), false) // its tab is not attached here: nothing to put back
expect(q.settled).not.toContain('a')
+ expect(q.items).toEqual([])
expect(addRequest(q, req('a', 10)).items.map((i) => i.pending.pendingId)).toEqual(['a'])
})
+
+ it('R41: a failed answer on a tab that is still attached goes back on screen, to be answered again', () => {
+ let q = attachProject(EMPTY_PENDING_QUEUE, 'p1')
+ q = addRequest(addRequest(q, req('a', 10)), req('b', 20))
+ q = beginAnswer(q, 'a')
+ q = finishAnswer(q, req('a', 10), false)
+ expect(q.items.map((i) => i.pending.pendingId)).toEqual(['a', 'b'])
+ expect(q.settled).not.toContain('a')
+ })
+
+ it('R41: …once: a second failure leaves it off the screen (a host that keeps refusing must not trap the owner)', () => {
+ let q = addRequest(attachProject(EMPTY_PENDING_QUEUE, 'p1'), req('a', 10))
+ q = finishAnswer(beginAnswer(q, 'a'), req('a', 10), false)
+ q = finishAnswer(beginAnswer(q, 'a'), req('a', 10), false)
+ expect(q.items).toEqual([])
+ expect(q.settled).not.toContain('a') // still answerable if the host offers it again
+ })
+
+ it('R41: a failed answer on a tab that dropped meanwhile is NOT put back (the reconnect\'s pull will)', () => {
+ let q = addRequest(attachProject(EMPTY_PENDING_QUEUE, 'p1'), req('a', 10))
+ q = beginAnswer(q, 'a')
+ q = dropProjectRequests(q, 'p1') // the drop's teardown ran before the rejection landed
+ q = finishAnswer(q, req('a', 10), false)
+ expect(q.items).toEqual([])
+ expect(addRequest(q, req('a', 10)).items).toHaveLength(1)
+ })
})
diff --git a/src/renderer/lib/hostedPendingQueue.ts b/src/renderer/lib/hostedPendingQueue.ts
index 02ccbc79d..d5c264863 100644
--- a/src/renderer/lib/hostedPendingQueue.ts
+++ b/src/renderer/lib/hostedPendingQueue.ts
@@ -28,9 +28,13 @@ export interface PendingQueue {
/** Requests whose answer is on its way: off the screen, and not re-added by a replay meanwhile.
* Settled only once the host answers; an answer that never landed leaves them answerable. */
answering?: readonly string[]
+ /** Owner tabs whose subscription is live: a failed answer on one of these goes back on screen. */
+ attached?: readonly string[]
+ /** Requests whose answer already failed once and went back: a second failure does not (R41). */
+ retried?: readonly string[]
}
-export const EMPTY_PENDING_QUEUE: PendingQueue = Object.freeze({ items: [], settled: [], answering: [] })
+export const EMPTY_PENDING_QUEUE: PendingQueue = Object.freeze({ items: [], settled: [], answering: [], attached: [], retried: [] })
/** How many closed request ids are remembered (4× the host's concurrent cap). */
export const SETTLED_MEMORY = 64
@@ -98,19 +102,33 @@ export function beginAnswer(q: PendingQueue, pendingId: string): PendingQueue {
}
}
-/** The answer came back. `landed` (the host said true or false) settles it; an answer that never
- * landed (the tab dropped, the call failed) leaves it answerable: the reconnect's pull brings the
- * request back if it is still pending (R40). */
-export function finishAnswer(q: PendingQueue, pendingId: string, landed: boolean): PendingQueue {
- if (landed) return settleRequest(q, pendingId)
- return { ...q, answering: (q.answering ?? []).filter((x) => x !== pendingId) }
+/**
+ * The answer came back. `landed` (the host said true or false) settles it. An answer that never
+ * landed is not remembered (R40), and:
+ * - on a tab that is still attached, it goes back on screen to be answered again — ONCE: a host
+ * that keeps refusing must not trap the owner in a dialog that returns after every answer;
+ * - on a tab that dropped meanwhile, it stays off: that tab's reconnect pulls it back if it is
+ * still pending (R41).
+ */
+export function finishAnswer(q: PendingQueue, item: QueuedRequest, landed: boolean): PendingQueue {
+ const id = item.pending.pendingId
+ if (landed) return settleRequest(q, id)
+ const next: PendingQueue = { ...q, answering: (q.answering ?? []).filter((x) => x !== id) }
+ if (!(next.attached ?? []).includes(item.projectId) || (next.retried ?? []).includes(id)) return next
+ return addRequest({ ...next, retried: [...(next.retried ?? []), id].slice(-SETTLED_MEMORY) }, item)
}
/** The owner's tab went away (dropped, closed): its requests leave the queue unanswered — its
* reconnect pulls them again, so they are not marked settled. */
export function dropProjectRequests(q: PendingQueue, projectId: string): PendingQueue {
const items = q.items.filter((i) => i.projectId !== projectId)
- return items.length === q.items.length ? q : { ...q, items }
+ const attached = (q.attached ?? []).filter((p) => p !== projectId)
+ return items.length === q.items.length && attached.length === (q.attached ?? []).length ? q : { ...q, items, attached }
+}
+
+/** An owner tab's subscription is live (its failed answers may go back on screen). */
+export function attachProject(q: PendingQueue, projectId: string): PendingQueue {
+ return (q.attached ?? []).includes(projectId) ? q : { ...q, attached: [...(q.attached ?? []), projectId] }
}
/** The request the dialog shows: the oldest one. */
diff --git a/src/renderer/lib/hostedTeam.test.ts b/src/renderer/lib/hostedTeam.test.ts
index 06b8d66a7..abe8411a7 100644
--- a/src/renderer/lib/hostedTeam.test.ts
+++ b/src/renderer/lib/hostedTeam.test.ts
@@ -14,7 +14,8 @@ import {
waitingForOwnerText,
RelayApprovalError,
mountFailureRetries,
- mountFailureMessage
+ mountFailureMessage,
+ THROTTLED_NOTICE
} from './hostedTeam'
describe('hosted team helpers', () => {
@@ -132,3 +133,18 @@ describe('a hosted tab that never opened', () => {
expect(mountFailureMessage(new Error("Error invoking remote method 'x': Error: boom"), '')).toBe('Could not open the team: boom')
})
})
+
+describe('R41: the per-network throttle', () => {
+ const wrap = (m: string) => `Error invoking remote method 'relay:client:connect': Error: ${m}`
+ it('is retried, carries a Retry-After when main attached one, and is not a BUSY', () => {
+ expect(classifyJoinFailure(wrap('[E_JOIN_THROTTLED] limiting'))).toMatchObject({ code: 'E_JOIN_THROTTLED', retry: true, throttled: true, retryAfterMs: null })
+ expect(classifyJoinFailure(wrap('[E_JOIN_THROTTLED] limiting [retry-after:90]'))).toMatchObject({ throttled: true, retryAfterMs: 90_000 })
+ expect(classifyJoinFailure(wrap('[E_JOIN_NETWORK] x')).throttled).toBe(false)
+ })
+ it('says a minute, never "tomorrow"', () => {
+ expect(joinStopMessage(classifyJoinFailure(wrap('[E_JOIN_THROTTLED] x')), 'box')).toBe(
+ 'The nodeterm service is limiting requests from this network. Try again in a minute.'
+ )
+ expect(THROTTLED_NOTICE).toBe('The nodeterm service is limiting requests from this network — retrying in a minute.')
+ })
+})
diff --git a/src/renderer/lib/hostedTeam.ts b/src/renderer/lib/hostedTeam.ts
index 480631b84..fd92ca5b5 100644
--- a/src/renderer/lib/hostedTeam.ts
+++ b/src/renderer/lib/hostedTeam.ts
@@ -1,7 +1,7 @@
// Pure helpers for a hosted team (a Server Edition hosting over the E2EE relay, joined by a
// `nodeterm://join` code): the unattended reconnect's backoff and retry decision, the sentences a
// refusal is told in, and the role wording. No React, no window — see docs/hosted-team-relay.md.
-import { joinErrorCode, joinErrorRetries, type JoinErrorCode } from '@shared/relay-join-errors'
+import { joinErrorCode, joinErrorRetries, joinRetryAfterMs, type JoinErrorCode } from '@shared/relay-join-errors'
import type { HostedPendingClosedReason, HostedRole, RelayClosedReason } from '@shared/types'
const BACKOFF = [1000, 2000, 4000, 8000, 15000]
@@ -11,6 +11,13 @@ export function reconnectDelayMs(attempt: number): number {
return attempt < BACKOFF.length ? BACKOFF[attempt] : 60_000
}
+/** What an unattended reconnect says, once per streak, while the service's per-network limiter
+ * holds it back (R41). */
+export const THROTTLED_NOTICE = 'The nodeterm service is limiting requests from this network — retrying in a minute.'
+
+/** A throttled retry never comes sooner than this, whatever Retry-After says (R41). */
+export const THROTTLE_MIN_DELAY_MS = 60_000
+
/** How many times an unattended attempt retries a connection that dropped before the host answered
* (a host restarting): the short steps of the ladder, 1/2/4/8/15 s, and then it stops and says so
* (R40). Each try mints a join token and opens a relay socket, so this one is bounded — unlike a
@@ -80,14 +87,28 @@ export interface JoinFailure {
retry: boolean
/** Another attempt of OURS for the same team is running — never a verdict about the team. */
busy: boolean
+ /** The service's per-network limiter: retry, but never faster than once a minute (R41). */
+ throttled: boolean
+ /** The Retry-After the service named, when it named one. */
+ retryAfterMs: number | null
/** Main's own sentence, without Electron's prefix or the code tag. */
detail: string
}
export function classifyJoinFailure(message: string): JoinFailure {
const code = joinErrorCode(String(message))
- const detail = stripIpcPrefix(message).replace(/^\[E_JOIN_[A-Z_]+\]\s*/, '').trim()
- return { code, retry: joinErrorRetries(code), busy: code === 'E_JOIN_BUSY', detail }
+ const detail = stripIpcPrefix(message)
+ .replace(/^\[E_JOIN_[A-Z_]+\]\s*/, '')
+ .replace(/\s*\[retry-after:\d+\]/, '')
+ .trim()
+ return {
+ code,
+ retry: joinErrorRetries(code),
+ busy: code === 'E_JOIN_BUSY',
+ throttled: code === 'E_JOIN_THROTTLED',
+ retryAfterMs: joinRetryAfterMs(String(message)),
+ detail
+ }
}
const teamName = (label: string): string => label.trim() || 'the team'
@@ -107,6 +128,8 @@ export function joinStopMessage(f: JoinFailure, teamLabel: string): string | nul
return `This device's access to ${team} was revoked. Remove the team and join again with a fresh invite code.`
case 'E_JOIN_RATE':
return `Too many join attempts for ${team} today. Try again tomorrow.`
+ case 'E_JOIN_THROTTLED':
+ return 'The nodeterm service is limiting requests from this network. Try again in a minute.'
case 'E_JOIN_BAD_CODE':
return `The invite code for ${team} is not valid. Ask an owner for a fresh code.`
case 'E_JOIN_KEY_LOCKED':
diff --git a/src/renderer/session/relay-tab.local-close.test.ts b/src/renderer/session/relay-tab.local-close.test.ts
new file mode 100644
index 000000000..671942b43
--- /dev/null
+++ b/src/renderer/session/relay-tab.local-close.test.ts
@@ -0,0 +1,100 @@
+// R41: a hosted connection cancelled from THIS side while the tab is still opening — after the
+// approval, while it asks its role (`hosted.self()`) or loads the host's workspace. Main never
+// reports a close it was asked for, so without the local close the in-flight requests hung and
+// `openRelayTab` never settled, leaking the session and its presence subscription. This runs the
+// REAL relay api (RelayFrameTransport + the hosted role gate) over a fake preload.
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
+import { openRelayTab } from './relay-tab'
+import { createSession, resetSessionsForTest, sessionCount, setActiveSession } from './session'
+import { emitLocalRelayClose } from '../bridge/relay-local-close'
+import { useHostedTeams } from '../state/hostedTeams'
+import { IPC } from '../../shared/ipc'
+import type { NodeTerminalApi } from '../../shared/types'
+
+const flush = async (): Promise<void> => {
+ for (let i = 0; i < 5; i++) await new Promise((r) => setTimeout(r, 0))
+}
+
+/** The local preload a relay tab is built over: approval arrives at once, frames are recorded. */
+function fakeLocal() {
+ const sent: Array<{ id: number; method: string }> = []
+ let frameCb: ((json: string) => void) | null = null
+ const disconnect = vi.fn()
+ const relayClient = {
+ onApproved: (_id: string, cb: () => void) => {
+ queueMicrotask(cb)
+ return () => {}
+ },
+ onClosed: () => () => {},
+ onFrame: (_id: string, cb: (json: string) => void) => {
+ frameCb = cb
+ return () => {}
+ },
+ send: (_id: string, json: string) => {
+ const m = JSON.parse(json)
+ if (m.t === 'req') sent.push({ id: m.id, method: m.method })
+ },
+ disconnect
+ }
+ const local = {
+ relayClient,
+ pty: { onData: () => () => {} },
+ claude: { cliCaps: async () => ({}), readTranscript: async () => [] },
+ githubControl: {}
+ } as unknown as NodeTerminalApi
+ const answer = (method: string, result: unknown): void => {
+ const req = sent.find((r) => r.method === method)
+ if (!req) throw new Error(`no ${method} request`)
+ frameCb?.(JSON.stringify({ t: 'res', id: req.id, ok: true, result }))
+ }
+ return { local, sent, disconnect, answer }
+}
+
+let saved: unknown
+beforeEach(() => {
+ saved = (globalThis as Record<string, unknown>).window
+ resetSessionsForTest()
+ useHostedTeams.setState({ bySession: {} })
+ setActiveSession(createSession('local', { marker: 'local' } as unknown as NodeTerminalApi, 'This Mac').id)
+})
+afterEach(() => {
+ ;(globalThis as Record<string, unknown>).window = saved
+})
+
+function open(f: ReturnType<typeof fakeLocal>, id: string) {
+ ;(globalThis as Record<string, unknown>).window = { nodeTerminal: f.local }
+ return openRelayTab(id, 'box', {
+ relayClient: f.local.relayClient,
+ hosted: true,
+ addProject: () => ({ id: 'proj-1' }),
+ setActiveProject: () => {}
+ })
+}
+
+describe('openRelayTab — a hosted connection closed from this side while the tab opens (R41)', () => {
+ it('during the role question: the tab rejects, and nothing was created', async () => {
+ const f = fakeLocal()
+ const opening = open(f, 'conn-self')
+ await flush()
+ expect(f.sent.map((r) => r.method)).toEqual([IPC.relayHostedSelf])
+ emitLocalRelayClose('conn-self')
+ await expect(opening).rejects.toMatchObject({ code: 'E_DISCONNECTED' })
+ expect(sessionCount()).toBe(1) // only the local session
+ expect(f.disconnect).toHaveBeenCalledWith('conn-self')
+ expect(useHostedTeams.getState().bySession).toEqual({})
+ })
+
+ it('during the workspace load: the tab rejects and disposes the session it had created (presence too)', async () => {
+ const f = fakeLocal()
+ const opening = open(f, 'conn-load')
+ await flush()
+ f.answer(IPC.relayHostedSelf, { role: 'owner', label: 'me', hostLabel: 'box' })
+ await flush()
+ expect(f.sent.map((r) => r.method)).toContain(IPC.workspaceLoad)
+ expect(sessionCount()).toBe(2) // the relay session exists, with its presence subscription
+ emitLocalRelayClose('conn-load')
+ await expect(opening).rejects.toMatchObject({ code: 'E_DISCONNECTED' })
+ expect(sessionCount()).toBe(1)
+ expect(useHostedTeams.getState().bySession).toEqual({})
+ })
+})
diff --git a/src/renderer/session/relay-tab.ts b/src/renderer/session/relay-tab.ts
index 8f865fac4..11615a925 100644
--- a/src/renderer/session/relay-tab.ts
+++ b/src/renderer/session/relay-tab.ts
@@ -16,6 +16,7 @@
import type { HostedRole, HostedSessionApi, Project, RelayClientApi } from '@shared/types'
import { buildRelayApi, type RelayApiHandle } from '../bridge/relay-api'
+import { E_DISCONNECTED } from '../../shared/rpc'
import { onLocalRelayClose } from '../bridge/relay-local-close'
import { closedReasonMessage, RelayApprovalError } from '../lib/hostedTeam'
import { attachHostedOwner } from '../lib/hostedOwner'
@@ -79,7 +80,12 @@ const ROLES: readonly HostedRole[] = ['owner', 'editor', 'commenter', 'viewer']
* in is "shows less", never "sends what will be refused".
*/
async function learnHostedRole(hosted: HostedSessionApi, handle: RelayApiHandle, label: string): Promise<HostedTeamInfo> {
- const self = await hosted.self().catch(() => null)
+ const self = await hosted.self().catch((err: unknown) => {
+ // The connection itself is gone (closed from either side): there is no tab to open — never a
+ // Viewer guess over a dead socket (R41).
+ if ((err as { code?: unknown } | null)?.code === E_DISCONNECTED) throw err
+ return null
+ })
const role: HostedRole = self && ROLES.includes(self.role) ? self.role : 'viewer'
handle.setHostedRole?.(role)
const hostLabel = self && typeof self.hostLabel === 'string' ? self.hostLabel.trim() : ''
diff --git a/src/renderer/state/hostedPending.ts b/src/renderer/state/hostedPending.ts
index 8df24680e..72553f42f 100644
--- a/src/renderer/state/hostedPending.ts
+++ b/src/renderer/state/hostedPending.ts
@@ -3,6 +3,7 @@ import type { HostedPendingClosedReason } from '@shared/types'
import {
EMPTY_PENDING_QUEUE,
addRequest,
+ attachProject,
beginAnswer,
closeRequest,
finishAnswer,
@@ -27,8 +28,11 @@ interface HostedPendingStore {
settle: (pendingId: string) => void
/** An answer is on its way (off the screen, not re-added by a replay). */
beginAnswer: (pendingId: string) => void
- /** It came back; `landed` settles it, otherwise it stays answerable. */
- finishAnswer: (pendingId: string, landed: boolean) => void
+ /** It came back; `landed` settles it, otherwise it stays answerable (and goes back on screen
+ * once, while its owner tab is attached). */
+ finishAnswer: (item: QueuedRequest, landed: boolean) => void
+ /** An owner tab's subscription is live. */
+ attach: (projectId: string) => void
drop: (projectId: string) => void
}
@@ -46,7 +50,11 @@ export const useHostedPending = create<HostedPendingStore>((set) => ({
}),
settle: (pendingId) => set((s) => ({ queue: settleRequest(s.queue, pendingId) })),
beginAnswer: (pendingId) => set((s) => ({ queue: beginAnswer(s.queue, pendingId) })),
- finishAnswer: (pendingId, landed) => set((s) => ({ queue: finishAnswer(s.queue, pendingId, landed) })),
+ finishAnswer: (item, landed) => set((s) => ({ queue: finishAnswer(s.queue, item, landed) })),
+ attach: (projectId) => set((s) => {
+ const queue = attachProject(s.queue, projectId)
+ return queue === s.queue ? s : { queue }
+ }),
drop: (projectId) => set((s) => {
const queue = dropProjectRequests(s.queue, projectId)
return queue === s.queue ? s : { queue }
@@ -55,6 +63,7 @@ export const useHostedPending = create<HostedPendingStore>((set) => ({
/** The store as the owner subscription's sink. */
export const hostedPendingSink: OwnerQueueSink = {
+ attach: (projectId) => useHostedPending.getState().attach(projectId),
add: (item) => useHostedPending.getState().add(item),
close: (pendingId, reason) => useHostedPending.getState().close(pendingId, reason),
drop: (projectId) => useHostedPending.getState().drop(projectId)