From 6bf864d27792aa3007ebf491bbd59bd773787c64 Mon Sep 17 00:00:00 2001 From: Josh Gratton Date: Mon, 18 May 2026 09:53:31 -0400 Subject: [PATCH] fix(web): timeout each WSL register attempt + expose reconciler trace Two issues from a fresh cold-start launch: 1. The first register attempt hit ERR_CONNECTION_REFUSED (WSL not listening yet), surfaced as Failed to fetch, and then no retries fired. Most likely a registration step hung past the descriptor fetch (IPC bearer read, WS open, etc) so the per-instance pending promise never resolved, which kept reconcileOnce awaiting forever and left pendingReconcileRun wedged for every subsequent retry tick. Wrap each attempt in a 25s hard ceiling via Promise.race so a stuck step is force-rejected, the catch wrapper logs it, and the scheduleAutoRetry chain keeps progressing. 2. Production renderers don't have CDP, so when the silent failure above happened there was no way to inspect what state the reconciler was actually in. Expose window.__t3LocalSecondaryDebug with two methods: - getState() returns the most recent bootstraps list, per-instance registration errors, last reconcile timestamp, and attempt count. - retryNow() re-fires the reconcile loop on demand. Wrapped behind exposeDebugGlobal() so non-browser environments (vitest under node) don't poison globalThis. Also wrapped the IPC call to getLocalEnvironmentBootstraps in a try/catch with logging, in case the bridge throws during a startup race. --- apps/web/src/environments/local/index.ts | 84 +++++++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/apps/web/src/environments/local/index.ts b/apps/web/src/environments/local/index.ts index 2869af29eb..3919689b44 100644 --- a/apps/web/src/environments/local/index.ts +++ b/apps/web/src/environments/local/index.ts @@ -67,12 +67,70 @@ const AUTO_RETRY_DELAYS_MS = [2_000, 4_000, 8_000, 16_000, 30_000, 45_000, 60_00 let autoRetryHandle: ReturnType | null = null; let autoRetryAttempt = 0; +interface LocalSecondaryReconcileTrace { + readonly bootstrapsSeen: ReadonlyArray<{ id: string; hasToken: boolean; httpBaseUrl: string }>; + readonly registrationErrors: ReadonlyMap; + readonly lastReconcileAt: string | null; + readonly attempts: number; +} + +const reconcileTrace: { + bootstrapsSeen: ReadonlyArray<{ id: string; hasToken: boolean; httpBaseUrl: string }>; + registrationErrors: Map; + lastReconcileAt: string | null; + attempts: number; +} = { + bootstrapsSeen: [], + registrationErrors: new Map(), + lastReconcileAt: null, + attempts: 0, +}; + +// Surface the reconciler's internal state on `window` for ad-hoc +// debugging. Production renderers don't expose CDP, so this is the +// only way for a user to inspect "what does the local-secondary +// reconciler think happened?" from the dev tools console. +function exposeDebugGlobal(): void { + if (typeof window === "undefined") return; + const w = window as unknown as { + __t3LocalSecondaryDebug?: { + getState: () => LocalSecondaryReconcileTrace; + retryNow: () => Promise; + }; + }; + if (w.__t3LocalSecondaryDebug) return; + w.__t3LocalSecondaryDebug = { + getState: () => ({ + bootstrapsSeen: reconcileTrace.bootstrapsSeen, + registrationErrors: new Map(reconcileTrace.registrationErrors), + lastReconcileAt: reconcileTrace.lastReconcileAt, + attempts: reconcileTrace.attempts, + }), + retryNow: () => reconcileLocalSecondaryEnvironments(), + }; +} + function readBootstraps(): readonly DesktopEnvironmentBootstrap[] { // Guard against test environments that import this module under // Node (no window) but exercise the service entrypoint that boots // the reconciler. if (typeof window === "undefined") return []; - return window.desktopBridge?.getLocalEnvironmentBootstraps() ?? []; + exposeDebugGlobal(); + let list: readonly DesktopEnvironmentBootstrap[] = []; + try { + list = window.desktopBridge?.getLocalEnvironmentBootstraps() ?? []; + } catch (error) { + console.error("[LOCAL_SECONDARY] readBootstraps threw", error); + return []; + } + reconcileTrace.bootstrapsSeen = list.map((entry) => ({ + id: entry.id, + hasToken: Boolean(entry.bootstrapToken), + httpBaseUrl: entry.httpBaseUrl ?? "", + })); + reconcileTrace.lastReconcileAt = new Date().toISOString(); + reconcileTrace.attempts += 1; + return list; } function findRecordByInstanceId(instanceId: string): SavedEnvironmentRecord | null { @@ -238,8 +296,30 @@ async function reconcileOnce(): Promise { return; } - const promise = registerSecondaryLocalEnvironment(bootstrap) + // Hard ceiling per attempt so a hung step (e.g. WebSocket open + // that never resolves, or an IPC call that doesn't return) can't + // wedge `pendingReconcileRun` and silence the entire retry loop. + const attemptTimeoutMs = 25_000; + const timeoutPromise = new Promise((_, reject) => { + setTimeout( + () => + reject(new Error(`Timed out registering ${bootstrap.id} after ${attemptTimeoutMs}ms`)), + attemptTimeoutMs, + ); + }); + const promise = Promise.race([registerSecondaryLocalEnvironment(bootstrap), timeoutPromise]) + .then((record) => { + if (record) { + reconcileTrace.registrationErrors.delete(bootstrap.id); + } + return record; + }) .catch((error) => { + const message = error instanceof Error ? error.message : String(error); + reconcileTrace.registrationErrors.set(bootstrap.id, { + message, + at: new Date().toISOString(), + }); console.error("[LOCAL_SECONDARY] register failed", bootstrap.id, error); return null; })