The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 12:15 UTC
fix(web): desktop window recovers when its backend restarts during a renderer reload (#26612)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 14, 2026 at 20:50 UTC
32a6a7f70b6a204098507764abe209e75a56015d
3 files changed
+58
-3
apps/web/src/authBootstrap.test.ts
+49
@@ -8,6 +8,7 @@ import {
8
import { createBrowserHistory } from "@tanstack/react-router";
9
import * as DateTime from "effect/DateTime";
10
import * as Effect from "effect/Effect";
11
+import { HttpClientError, HttpClientRequest } from "effect/http";
12
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
13
14
import { installEnvironmentHttpTest } from "../test/environmentHttpTest";
@@ -165,6 +166,54 @@ describe("resolveInitialServerAuthGateState", () => {
166
expect(testApi.calls.browserSession).toEqual([{ credential: "desktop-bootstrap-token" }]);
167
});
168
169
+ it("retries the desktop bootstrap while the backend is still unreachable", async () => {
170
+ const nextSession = sequence(
171
+ unauthenticatedSession(DESKTOP_AUTH),
172
+ authenticatedSession(DESKTOP_AUTH),
173
+ );
174
+ const testApi = await installAuthApi({
175
+ session: nextSession,
176
+ browserSession: () => Effect.succeed(browserSession(["orchestration:read", "access:write"])),
177
+ });
178
+ // What FetchHttpClient yields when nothing listens on the backend port.
179
+ const connectionRefused = new HttpClientError.HttpClientError({
180
+ reason: new HttpClientError.TransportError({
181
+ request: HttpClientRequest.get("http://localhost:3773/api/auth/session"),
182
+ cause: new TypeError("Failed to fetch"),
183
+ }),
184
+ });
185
+ let unreachableAttempts = 1;
186
+ __setPrimaryHttpRunnerForTests((effect) => {
187
+ if (unreachableAttempts > 0) {
188
+ unreachableAttempts -= 1;
189
+ return Promise.reject(connectionRefused);
190
+ }
191
+ return testApi.run(effect);
192
+ });
193
+
194
+ const testWindow = installTestBrowser("http://localhost/");
195
+ testWindow.desktopBridge = {
196
+ getLocalEnvironmentBootstraps: () => [
197
+ {
198
+ id: "primary",
199
+ label: "Local",
200
+ httpBaseUrl: "http://localhost:3773",
201
+ wsBaseUrl: "ws://localhost:3773",
202
+ bootstrapToken: "desktop-bootstrap-token",
203
+ },
204
+ ],
205
+ } as unknown as DesktopBridge;
206
+
207
+ const { resolveInitialServerAuthGateState } = await import("./environments/primary");
208
+
209
+ vi.useFakeTimers({ toFake: ["setTimeout", "Date"] });
210
+ await Promise.all([
211
+ expect(resolveInitialServerAuthGateState()).resolves.toEqual({ status: "authenticated" }),
212
+ vi.advanceTimersByTimeAsync(1_000),
213
+ ]);
214
+ expect(testApi.calls.browserSession).toEqual([{ credential: "desktop-bootstrap-token" }]);
215
+ });
216
+
217
it("takes a pairing token from the location hash and strips it immediately", async () => {
218
const testWindow = installTestBrowser("http://localhost/#token=pairing-token");
219
const { takePairingTokenFromUrl } = await import("./environments/primary");
apps/web/src/environments/primary/auth.ts
+5
-1
@@ -294,7 +294,7 @@ function waitForBootstrapRetry(delayMs: number): Promise<void> {
294
295
function isTransientBootstrapError(error: unknown): boolean {
296
if (isPrimaryEnvironmentRequestError(error)) {
297
- return TRANSIENT_BOOTSTRAP_STATUS_CODES.has(error.status);
297
+ return TRANSIENT_BOOTSTRAP_STATUS_CODES.has(error.status) || isTransportError(error.cause);
298
}
299
300
if (error instanceof TypeError) {
@@ -304,6 +304,10 @@ function isTransientBootstrapError(error: unknown): boolean {
304
return error instanceof DOMException && error.name === "AbortError";
305
}
306
307
+function isTransportError(error: unknown): boolean {
308
+ return HttpClientError.isHttpClientError(error) && error.reason._tag === "TransportError";
309
+}
310
+
311
async function bootstrapServerAuth(urlCredential: string | null): Promise<ServerAuthGateState> {
312
const currentSession = await fetchSessionState();
313
if (currentSession.authenticated && !urlCredential) {
apps/web/test/environmentHttpTest.ts
+4
-2
@@ -23,7 +23,7 @@ import { HttpApiTest } from "effect/http-api";
23
import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
24
25
import { PrimaryEnvironmentHttpClient } from "../src/environments/primary/httpClient";
26
-import { __setPrimaryHttpRunnerForTests } from "../src/lib/runtime";
26
+import { __setPrimaryHttpRunnerForTests, type PrimaryHttpEffectRunner } from "../src/lib/runtime";
27
28
type BrowserSessionHandler = (
29
payload: AuthBrowserSessionRequest,
@@ -127,10 +127,12 @@ export async function installEnvironmentHttpTest(scenario: EnvironmentHttpTestSc
127
);
128
129
const runtime = ManagedRuntime.make(Layer.succeed(PrimaryEnvironmentHttpClient, client));
130
- __setPrimaryHttpRunnerForTests((effect) => runtime.runPromise(effect));
130
+ const run: PrimaryHttpEffectRunner = (effect) => runtime.runPromise(effect);
131
+ __setPrimaryHttpRunnerForTests(run);
132
133
return {
134
calls,
135
+ run,
136
async dispose() {
137
__setPrimaryHttpRunnerForTests();
138
await runtime.dispose();