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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(watch-link): refuse a 400 host token, bound Retry-After, tell a reset body from a bad one
review1524b0af804b
diff --git a/src/core/watch-link/api.test.ts b/src/core/watch-link/api.test.tsindex cbdf84189..78a833b5e 100644--- a/src/core/watch-link/api.test.ts+++ b/src/core/watch-link/api.test.ts@@ -1,4 +1,4 @@-import { describe, it, expect } from 'vitest'+import { describe, it, expect, vi } from 'vitest'import { createWatchLinkApi } from './api'const res = (status: number, body: unknown, headers: Record<string, string> = {}) =>@@ -25,6 +25,49 @@ function hungFetch() {return { f, signals }}+/** A 200 whose body sends part of a chunk and then dies the way undici reports a reset (`terminated`). */+function resetMidBody() {+ let pulls = 0+ const body = new ReadableStream<Uint8Array>({+ pull(c) {+ if (pulls++ === 0) c.enqueue(new TextEncoder().encode('{"pairingToken":"P'))+ else c.error(new TypeError('terminated'))+ }+ })+ return new Response(body, { status: 200, headers: { 'content-type': 'application/json' } })+}++/**+ * Tracks the abort timers `createWatchLinkApi` arms. Only timers armed with `delay` are counted, so a+ * timer some other layer arms (undici, vitest) cannot make the count lie either way.+ */+function trackTimers(delay: number) {+ const realSet = globalThis.setTimeout+ const realClear = globalThis.clearTimeout+ const live = new Set<unknown>()+ let armed = 0+ const setSpy = vi.spyOn(globalThis, 'setTimeout').mockImplementation(((fn: () => void, ms?: number, ...args: unknown[]) => {+ const h = realSet(fn, ms, ...args)+ if (ms === delay) {+ armed+++ live.add(h)+ }+ return h+ }) as unknown as typeof setTimeout)+ const clearSpy = vi.spyOn(globalThis, 'clearTimeout').mockImplementation(((h?: Parameters<typeof clearTimeout>[0]) => {+ live.delete(h)+ return realClear(h)+ }) as typeof clearTimeout)+ return {+ live,+ armed: () => armed,+ restore: () => {+ setSpy.mockRestore()+ clearSpy.mockRestore()+ }+ }+}+describe('createWatchLinkApi', () => {it('create posts the hash and corrects expiry to the local clock', async () => {const serverNow = Date.parse('Tue, 29 Sep 2026 10:00:00 GMT')@@ -177,4 +220,111 @@ describe('createWatchLinkApi', () => {`https://api.test/v1/watch-links/${enc}/revoke`])})++ it('create accepts a 200 only when expiresAt is a finite number above zero', async () => {+ const bodies = [+ '{"linkId":"L","expiresAt":0}',+ '{"linkId":"L","expiresAt":-5}',+ // NaN cannot cross JSON (it serializes to null); a string and a non-finite number can.+ '{"linkId":"L","expiresAt":null}',+ '{"linkId":"L","expiresAt":"1790679600"}',+ '{"linkId":"L","expiresAt":1e999}',+ '{"linkId":"L"}'+ ]+ for (const text of bodies) {+ expect(await api(() => new Response(text, { status: 200 })).a.create('e', 'h', 3600)).toEqual({ ok: false, error: 'network' })+ }+ })++ it('create falls back to the local clock when the Date header is missing or unparseable', async () => {+ // Without a server clock to subtract, the instant is taken as-is: expiresAt = the server's+ // instant in ms (local now + (instant - local now)).+ const serverExp = Date.parse('Tue, 29 Sep 2026 11:00:00 GMT') / 1000+ for (const headers of [{}, { date: 'yesterday' }] as Record<string, string>[]) {+ const r = await api(() => res(200, { linkId: 'L', expiresAt: serverExp }, headers), 5_000).a.create('e', 'h', 3600)+ expect(r).toEqual({ ok: true, linkId: 'L', expiresAt: serverExp * 1000 })+ }+ })++ it('hostToken: our own malformed request (400) is refused, never retried as a network failure', async () => {+ expect(await api(() => res(400, { error: 'bad_request' })).a.hostToken('L', 'e')).toEqual({ ok: false, kind: 'refused', status: 400 })+ })++ it('hostToken: a 5xx is a network failure that keeps its status', async () => {+ for (const status of [500, 502, 503]) {+ expect(await api(() => res(status, null)).a.hostToken('L', 'e')).toEqual({ ok: false, kind: 'network', status })+ }+ })++ it('Retry-After counts only as whole seconds, clamped to an hour', async () => {+ const after = async (value: string) => api(() => res(429, {}, { 'retry-after': value })).a.hostToken('L', 'e')+ // Delay-seconds (RFC 9110) only; everything else leaves the wait to the scheduler's own floor.+ for (const value of ['Wed, 21 Oct 2026 07:28:00 GMT', '1e9', 'Infinity', '0x10', '', '0.5', '-5', '0']) {+ expect(await after(value)).toEqual({ ok: false, kind: 'rate-limited', status: 429 })+ }+ expect(await api(() => res(429, {})).a.hostToken('L', 'e')).toEqual({ ok: false, kind: 'rate-limited', status: 429 })+ expect(await after('60')).toEqual({ ok: false, kind: 'rate-limited', status: 429, retryAfterMs: 60_000 })+ expect(await after('3600')).toEqual({ ok: false, kind: 'rate-limited', status: 429, retryAfterMs: 3_600_000 })+ for (const value of ['3601', '86400', '99999999999999999999']) {+ expect(await after(value)).toEqual({ ok: false, kind: 'rate-limited', status: 429, retryAfterMs: 3_600_000 })+ }+ })++ it('a transport error while the body streams is no answer, never a bad response', async () => {+ expect(await api(() => resetMidBody()).a.hostToken('L', 'e')).toEqual({ ok: false, kind: 'network' })+ expect(await api(() => resetMidBody()).a.create('e', 'h', 3600)).toEqual({ ok: false, error: 'network' })+ expect(await api(() => resetMidBody()).a.status('L', 'e')).toBe('unknown')+ })++ it('a completed body that does not parse is the server\'s fault', async () => {+ for (const text of ['{"pairingToken":"P"', '', 'not json']) {+ expect(await api(() => new Response(text, { status: 200 })).a.hostToken('L', 'e')).toEqual({ ok: false, kind: 'bad-response', status: 200 })+ }+ })++ it('every request refuses to follow a redirect', async () => {+ const inits: RequestInit[] = []+ const a = createWatchLinkApi({+ apiBase: 'https://api.test',+ fetch: (async (_url: string, init: RequestInit) => {+ inits.push(init)+ return res(204, null)+ }) as typeof fetch+ })+ await a.create('e', 'h', 3600)+ await a.hostToken('L', 'e')+ await a.status('L', 'e')+ await a.revoke('L', 'e')+ await a.revokeAll('e')+ expect(inits.map((i) => i.redirect)).toEqual(['error', 'error', 'error', 'error', 'error'])+ })++ it('the abort timer is cleared after every outcome', async () => {+ const T = 4_321+ const t = trackTimers(T)+ try {+ const run = (reply: (init: RequestInit) => Promise<Response>) =>+ createWatchLinkApi({ apiBase: 'https://api.test', timeoutMs: T, fetch: ((_u: string, init: RequestInit) => reply(init)) as typeof fetch })+ // success, error status, thrown fetch, and a body that dies mid-stream+ await run(async () => res(200, { pairingToken: 'P', exp: 1_120 })).hostToken('L', 'e')+ await run(async () => res(500, null)).hostToken('L', 'e')+ await run(async () => { throw new Error('offline') }).hostToken('L', 'e')+ await run(async () => resetMidBody()).hostToken('L', 'e')+ expect(t.armed()).toBe(4)+ expect(t.live.size).toBe(0)+ } finally {+ t.restore()+ }+ // and the timeout itself: the timer that fired is cleared too, and nothing else is left armed+ const T2 = 17+ const t2 = trackTimers(T2)+ try {+ const { f } = hungFetch()+ expect(await createWatchLinkApi({ apiBase: 'https://api.test', timeoutMs: T2, fetch: f }).revoke('L', 'e')).toBe(false)+ expect(t2.armed()).toBe(1)+ expect(t2.live.size).toBe(0)+ } finally {+ t2.restore()+ }+ })})diff --git a/src/core/watch-link/api.ts b/src/core/watch-link/api.tsindex 861de2e58..ba061e87e 100644--- a/src/core/watch-link/api.ts+++ b/src/core/watch-link/api.ts@@ -20,25 +20,48 @@ interface Reply {retryAfter: string | null}+/** The longest wait a Retry-After may impose. Our limiters do not count a refused request, so asking+ * again early is cheap, while an unbounded header (a proxy's `86400`) would park a link's host for+ * longer than the link lives. */+const RETRY_AFTER_MAX_S = 3600++/** Retry-After as RFC 9110 delay-seconds only, clamped. Anything else (an HTTP-date, `1e9`,+ * `Infinity`, `0x10`, `0.5`, empty, 0) is no answer: the scheduler's own floor applies. */+function retryAfterMs(header: string | null): number | undefined {+ if (header === null || !/^\d+$/.test(header)) return undefined+ const s = Number(header)+ return s > 0 ? Math.min(s, RETRY_AFTER_MAX_S) * 1000 : undefined+}+export function createWatchLinkApi(o: { apiBase: string; fetch?: typeof fetch; now?: () => number; timeoutMs?: number }): WatchLinkApi {const base = o.apiBase.replace(/\/+$/, '')const now = o.now ?? Date.nowconst f = o.fetch ?? fetch- /** One POST. null = no answer (the request failed, or the timeout fired before the whole body arrived). */+ /** One POST. null = no answer (the request failed, or the body did not arrive whole, timeout included). */async function post(path: string, body: unknown): Promise<Reply | null> {const ctrl = new AbortController()const timer = setTimeout(() => ctrl.abort(), o.timeoutMs ?? 8000)try {- const r = await f(`${base}${path}`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body), signal: ctrl.signal })+ const r = await f(`${base}${path}`, {+ method: 'POST',+ headers: { 'content-type': 'application/json' },+ body: JSON.stringify(body),+ signal: ctrl.signal,+ // A 307/308 would re-send this body, entitlement included, to wherever Location points. None+ // of these routes redirects, so a redirect is an error rather than a hop.+ redirect: 'error'+ })let json: Record<string, unknown> | null = nullif (r.status !== 204) {+ // Reading and parsing are two different failures. A read that throws (a reset mid-body, the+ // timeout's abort) is the network's: the outer catch turns it into "no answer". A body that+ // arrived whole but is not a JSON object is the server's: json stays null.+ const text = await r.text()try {- const v: unknown = await r.json()+ const v: unknown = JSON.parse(text)json = v !== null && typeof v === 'object' && !Array.isArray(v) ? (v as Record<string, unknown>) : null} catch {- // Aborted mid-body is the timeout (no answer); a body that is not JSON is the server's.- if (ctrl.signal.aborted) return nulljson = null}}@@ -55,11 +78,15 @@ export function createWatchLinkApi(o: { apiBase: string; fetch?: typeof fetch; nasync create(entitlement, joinKeyHash, ttlSeconds) {const r = await post('/v1/watch-links', { entitlement, joinKeyHash, ttlSeconds })if (!r) return { ok: false, error: 'network' }- if (r.status === 200 && typeof r.json?.linkId === 'string' && typeof r.json.expiresAt === 'number') {+ const exp = r.json?.expiresAt+ // A link always ends in the future; 0, a negative or a non-finite instant is a malformed reply,+ // and tokenTtlMs would turn it into its two-minute HOST-TOKEN default (a link that "expires" in+ // 2 minutes while its server row stays live and counts against the active-link cap).+ if (r.status === 200 && typeof r.json?.linkId === 'string' && typeof exp === 'number' && Number.isFinite(exp) && exp > 0) {// `expiresAt` is an instant on the SERVER's clock; the Date header turns it into time left,// which is then anchored on this machine's clock (see tokenTtlMs).const t = now()- return { ok: true, linkId: r.json.linkId, expiresAt: t + tokenTtlMs(r.json.expiresAt, r.date, t) }+ return { ok: true, linkId: r.json.linkId, expiresAt: t + tokenTtlMs(exp, r.date, t) }}if (r.status === 402 || r.status === 403) return { ok: false, error: 'not-entitled' }if (r.status === 429) {@@ -81,11 +108,12 @@ export function createWatchLinkApi(o: { apiBase: string; fetch?: typeof fetch; n}if (r.status === 410) return { ok: false, kind: 'gone', reason: r.json?.reason === 'expired' ? 'expired' : 'revoked' }if (r.status === 429) {- // Seconds only. An HTTP-date Retry-After (or none) falls to the scheduler's own floor.- const ra = Number(r.retryAfter)- return { ok: false, kind: 'rate-limited', status: 429, ...(ra > 0 ? { retryAfterMs: ra * 1000 } : {}) }+ const ra = retryAfterMs(r.retryAfter)+ return { ok: false, kind: 'rate-limited', status: 429, ...(ra !== undefined ? { retryAfterMs: ra } : {}) }}- if (r.status === 402 || r.status === 403 || r.status === 404) return { ok: false, kind: 'refused', status: r.status }+ // 400 is our own malformed request (the owner check's body validation): it can never succeed,+ // so it stops minting like the other refusals instead of being retried as a network failure.+ if (r.status === 400 || r.status === 402 || r.status === 403 || r.status === 404) return { ok: false, kind: 'refused', status: r.status }return { ok: false, kind: 'network', status: r.status }},async status(linkId, entitlement) {