From 8a8d4b1b08efb771408f62a0ca815873cb646377 Mon Sep 17 00:00:00 2001 From: Nick Roberts <9039456+nickrroberts@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:50:54 -0600 Subject: [PATCH 1/6] fix(browser): a timed-out preview call no longer drops the whole browser host The broker disconnected the automation host on any unanswered request, failing every pending call from every thread and leaving agents with "no automation host" until it re-registered. The 500ms status read sent after each action could trigger it on any busy page. - Wait timeoutMs plus a 1s grace (upstream #17693) so the host's own timeout reply wins the race. - Never evict on background status reads (updateCurrentTab: false). - Evict only when the host went silent since the request was delivered; a host answering other calls keeps its connection. - Forward a sanitized failure reason from the web host so PreviewAutomationExecutionError says why it failed. Co-Authored-By: Claude Opus 5.5 --- .../src/mcp/PreviewAutomationBroker.test.ts | 183 +++++++++++++++++- .../server/src/mcp/PreviewAutomationBroker.ts | 89 ++++++++- .../preview/previewAutomationErrors.ts | 8 + .../previewAutomationRequestConsumer.test.ts | 21 +- packages/contracts/src/previewAutomation.ts | 5 +- packages/shared/package.json | 4 + .../shared/src/previewFailureReason.test.ts | 47 +++++ packages/shared/src/previewFailureReason.ts | 61 ++++++ 8 files changed, 406 insertions(+), 12 deletions(-) create mode 100644 packages/shared/src/previewFailureReason.test.ts create mode 100644 packages/shared/src/previewFailureReason.ts diff --git a/apps/server/src/mcp/PreviewAutomationBroker.test.ts b/apps/server/src/mcp/PreviewAutomationBroker.test.ts index 4d9b40e1cab89..c069b1194a49a 100644 --- a/apps/server/src/mcp/PreviewAutomationBroker.test.ts +++ b/apps/server/src/mcp/PreviewAutomationBroker.test.ts @@ -1356,7 +1356,8 @@ it.effect("evicts an unanswered host and lets later calls use a healthy runtime" Effect.forkScoped, ); yield* Deferred.await(otherReceived); - yield* TestClock.adjust(1_000); + // Budget plus the host-response grace, with no sign of life from the host. + yield* TestClock.adjust(2_000); expect(yield* Fiber.join(timedOut)).toMatchObject({ _tag: "PreviewAutomationTimeoutError" }); expect(yield* Deferred.isDone(otherCompleted)).toBe(true); expect(yield* Fiber.join(other)).toMatchObject({ @@ -1417,3 +1418,183 @@ it.effect("keeps a host that responds with an operation timeout", () => }), ), ); + +const otherThreadScope = { + ...scope, + threadId: ThreadId.make("thread-2"), + providerSessionId: "provider-session-2", +}; + +it.effect("a timed-out request does not evict a host that is still answering other threads", () => + Effect.scoped( + Effect.gen(function* () { + const broker = yield* makeBroker; + const connected = yield* Deferred.make(); + const stuckDelivered = yield* Deferred.make(); + const events = yield* broker.connect(makeHost()); + const consumer = yield* Stream.runForEach(events, (event) => { + if (event.type === "connected") return Deferred.succeed(connected, undefined); + // A page that never settles: the host never answers this one request. + if (event.request.operation === "snapshot") { + return Deferred.succeed(stuckDelivered, undefined); + } + return broker.respond({ + clientId: "client-1", + connectionId: event.connectionId, + requestId: event.request.requestId, + ok: true, + result: `answered ${event.request.operation} for ${event.request.threadId}`, + }); + }).pipe(Effect.forkScoped); + yield* Deferred.await(connected); + + const stuck = yield* broker + .invoke({ scope, operation: "snapshot", input: {}, timeoutMs: 1_000 }) + .pipe(Effect.flip, Effect.forkScoped); + yield* Deferred.await(stuckDelivered); + // Another thread keeps using the same host while the first request hangs. + expect( + yield* broker.invoke({ scope: otherThreadScope, operation: "evaluate", input: {} }), + ).toBe("answered evaluate for thread-2"); + + yield* TestClock.adjust(2_000); + expect(yield* Fiber.join(stuck)).toMatchObject({ _tag: "PreviewAutomationTimeoutError" }); + expect(consumer.pollUnsafe()).toBeUndefined(); + expect( + yield* broker.invoke({ scope: otherThreadScope, operation: "status", input: {} }), + ).toBe("answered status for thread-2"); + expect(yield* broker.invoke({ scope, operation: "status", input: {} })).toBe( + "answered status for thread-1", + ); + }), + ), +); + +it.effect("a slow background status read never drops the host or other pending calls", () => + Effect.scoped( + Effect.gen(function* () { + const broker = yield* makeBroker; + const connected = yield* Deferred.make(); + const evaluateDelivered = yield* Deferred.make(); + const events = yield* broker.connect(makeHost()); + const consumer = yield* Stream.runForEach(events, (event) => { + if (event.type === "connected") return Deferred.succeed(connected, undefined); + // A busy page: nothing is answered until the evaluate finishes. + if (event.request.operation === "evaluate") { + return Deferred.succeed(evaluateDelivered, { + ...event.request, + connectionId: event.connectionId, + }); + } + return Effect.void; + }).pipe(Effect.forkScoped); + yield* Deferred.await(connected); + + const evaluate = yield* broker + .invoke({ scope: otherThreadScope, operation: "evaluate", input: {} }) + .pipe(Effect.forkScoped); + const evaluateRequest = yield* Deferred.await(evaluateDelivered); + const probe = yield* broker + .invoke({ + scope, + operation: "status", + input: {}, + timeoutMs: 500, + updateCurrentTab: false, + }) + .pipe(Effect.flip, Effect.forkScoped); + yield* TestClock.adjust(500); + expect(yield* Fiber.join(probe)).toMatchObject({ _tag: "PreviewAutomationTimeoutError" }); + + // The host was silent the whole time, yet the probe must not have dropped it. + expect(consumer.pollUnsafe()).toBeUndefined(); + yield* broker.respond({ + clientId: "client-1", + connectionId: evaluateRequest.connectionId, + requestId: evaluateRequest.requestId, + ok: true, + result: "evaluated", + }); + expect(yield* Fiber.join(evaluate)).toBe("evaluated"); + }), + ), +); + +it.effect("keeps a host whose own timeout reply arrives within the response grace", () => + Effect.scoped( + Effect.gen(function* () { + const broker = yield* makeBroker; + const connected = yield* Deferred.make(); + const delivered = yield* Deferred.make(); + const events = yield* broker.connect(makeHost()); + const consumer = yield* Stream.runForEach(events, (event) => { + if (event.type === "connected") return Deferred.succeed(connected, undefined); + if (event.request.operation === "waitFor") { + return Deferred.succeed(delivered, { + ...event.request, + connectionId: event.connectionId, + }); + } + return broker.respond({ + clientId: "client-1", + connectionId: event.connectionId, + requestId: event.request.requestId, + ok: true, + result: "responsive", + }); + }).pipe(Effect.forkScoped); + yield* Deferred.await(connected); + + const waitFor = yield* broker + .invoke({ scope, operation: "waitFor", input: {}, timeoutMs: 1_000 }) + .pipe(Effect.flip, Effect.forkScoped); + const request = yield* Deferred.await(delivered); + // The host started its own timer late, so its timeout lands after ours. + yield* TestClock.adjust(1_500); + yield* broker.respond({ + clientId: "client-1", + connectionId: request.connectionId, + requestId: request.requestId, + ok: false, + error: { _tag: "PreviewAutomationTimeoutError", message: "Selector timed out" }, + }); + expect(yield* Fiber.join(waitFor)).toMatchObject({ _tag: "PreviewAutomationTimeoutError" }); + expect(consumer.pollUnsafe()).toBeUndefined(); + expect(yield* broker.invoke({ scope, operation: "status", input: {} })).toBe("responsive"); + }), + ), +); + +it.effect("reports the host's sanitized failure reason instead of a bare execution error", () => + Effect.scoped( + Effect.gen(function* () { + const broker = yield* makeBroker; + const requests = requestsFrom(yield* broker.connect(makeHost())); + yield* Stream.runForEach(requests, (request) => + broker.respond({ + clientId: "client-1", + connectionId: request.connectionId, + requestId: request.requestId, + ok: false, + error: { + _tag: "PreviewAutomationExecutionError", + message: "Preview automation snapshot request failed.", + detail: { + reason: + "Desktop preview operation failed: automationSnapshot.capturePage (artifact /Users/someone/.t3/a.png): TimeoutError: Operation timed out after '1s'", + }, + }, + }), + ).pipe(Effect.forkScoped); + yield* Effect.yieldNow; + + const error = yield* broker + .invoke({ scope, operation: "snapshot", input: {} }) + .pipe(Effect.flip); + expect(error).toMatchObject({ _tag: "PreviewAutomationExecutionError" }); + expect(error.message).toBe( + "Preview automation snapshot failed on client client-1: Desktop preview operation failed: automationSnapshot.capturePage (artifact ): TimeoutError: Operation timed out after '1s'", + ); + }), + ), +); diff --git a/apps/server/src/mcp/PreviewAutomationBroker.ts b/apps/server/src/mcp/PreviewAutomationBroker.ts index 0b03eb1eba2a7..575f0db366ee3 100644 --- a/apps/server/src/mcp/PreviewAutomationBroker.ts +++ b/apps/server/src/mcp/PreviewAutomationBroker.ts @@ -36,8 +36,20 @@ import * as Schema from "effect/Schema"; import * as Stream from "effect/Stream"; import * as SynchronizedRef from "effect/SynchronizedRef"; +import { + previewFailureReason, + sanitizePreviewFailureText, +} from "@t3tools/shared/previewFailureReason"; + import * as McpInvocationContext from "./McpInvocationContext.ts"; +/** + * The browser host starts its own operation timer only after the request is + * delivered, so its timeout reply lands just after ours. Wait this long past + * the request's budget before concluding the host never answered. + */ +const HOST_RESPONSE_GRACE_MS = 1_000; + export interface PreviewAutomationInvokeInput { readonly scope: McpInvocationContext.McpInvocationScope; readonly operation: PreviewAutomationOperation; @@ -75,6 +87,8 @@ interface ClientConnection { readonly liveTabs: NonNullable; readonly focusOrder: number; readonly queue: Queue.Queue; + /** Bumped on every response or focus update, so a timeout can tell a busy host from a dead one. */ + readonly activity: number; } interface PendingRequest { @@ -192,6 +206,22 @@ function remoteDetailKind(detail: unknown): RemoteDetailKind { } } +// The host is a client, so its text is re-sanitized here rather than trusted. +const remoteFailureReason = ( + error: NonNullable, +): string | undefined => { + const detail = error.detail; + if ( + typeof detail === "object" && + detail !== null && + "reason" in detail && + typeof detail.reason === "string" + ) { + return previewFailureReason(sanitizePreviewFailureText(detail.reason)); + } + return undefined; +}; + const classifyResponseError = ( context: PreviewAutomationRequestErrorContext, error: NonNullable, @@ -306,11 +336,14 @@ const classifyResponseError = ( ...context, ...remoteDiagnostics, }); - default: + default: { + const reason = remoteFailureReason(error); return new PreviewAutomationExecutionError({ ...context, ...remoteDiagnostics, + ...(reason === undefined ? {} : { reason }), }); + } } }; @@ -364,6 +397,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() { liveTabs: [], focusOrder: 0, queue, + activity: 0, }; const registration = yield* SynchronizedRef.modify(state, (current) => { const previousConnection = current.clients.get(clientId); @@ -420,6 +454,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() { focused: host.focused, liveTabs: host.liveTabs ?? currentHost.liveTabs, focusOrder: host.focused ? focusSequence : currentHost.focusOrder, + activity: currentHost.activity + 1, }); return { ...current, clients, focusSequence }; }); @@ -429,17 +464,28 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() { "PreviewAutomationBroker.respond", )(function* (response) { const pending = yield* SynchronizedRef.modify(state, (current) => { - const entry = current.pending.get(response.requestId); + const responder = current.clients.get(response.clientId); + const withActivity = + responder?.connectionId === response.connectionId + ? { + ...current, + clients: new Map(current.clients).set(response.clientId, { + ...responder, + activity: responder.activity + 1, + }), + } + : current; + const entry = withActivity.pending.get(response.requestId); if ( !entry || entry.context.clientId !== response.clientId || entry.context.connectionId !== response.connectionId ) { - return [undefined, current] as const; + return [undefined, withActivity] as const; } - const next = new Map(current.pending); + const next = new Map(withActivity.pending); next.delete(response.requestId); - return [entry, { ...current, pending: next }] as const; + return [entry, { ...withActivity, pending: next }] as const; }); if (!pending) return; if (response.ok) { @@ -563,7 +609,14 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() { pending.delete(requestId); return { ...next, pending }; }); + const connectionActivity = SynchronizedRef.get(state).pipe( + Effect.map((current) => { + const live = current.clients.get(connection.clientId); + return live?.queue === connection.queue ? live.activity : undefined; + }), + ); const awaitResponse = Effect.fn("PreviewAutomationBroker.awaitResponse")(function* () { + const activityAtDelivery = yield* connectionActivity; const offered = yield* Queue.offer(connection.queue, { type: "request", connectionId: connection.connectionId, @@ -584,13 +637,31 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() { } return yield* new PreviewAutomationRequestQueueClosedError(requestContext); } - const result = yield* Deferred.await(deferred).pipe(Effect.timeoutOption(timeoutMs)); + const responseTimeoutMs = + input.updateCurrentTab === false ? timeoutMs : timeoutMs + HOST_RESPONSE_GRACE_MS; + const result = yield* Deferred.await(deferred).pipe(Effect.timeoutOption(responseTimeoutMs)); return yield* Option.match(result, { onNone: () => Effect.gen(function* () { - // An unanswered request invalidates this connection. Do not replay - // actions: the client may have applied them before becoming unreachable. - yield* disconnect(connection.clientId, connection.queue); + // Only this request fails. The connection is dropped (failing every + // request on it, from any thread) only when the host has gone silent: + // no response or focus update since this request was delivered. A busy + // page, a slow background status read, or a host that is answering + // other calls keeps its connection. Never replay: the client may have + // applied the action before going quiet. + const activityNow = yield* connectionActivity; + if ( + input.updateCurrentTab !== false && + activityNow !== undefined && + activityNow === activityAtDelivery + ) { + yield* Effect.logWarning("preview automation host went silent; dropping it", { + operation: input.operation, + clientId: connection.clientId, + timeoutMs, + }); + yield* disconnect(connection.clientId, connection.queue); + } return yield* new PreviewAutomationTimeoutError(requestContext); }), onSome: (value) => Effect.succeed(value as A), diff --git a/apps/web/src/components/preview/previewAutomationErrors.ts b/apps/web/src/components/preview/previewAutomationErrors.ts index 5e1378599878a..221e39eb128a2 100644 --- a/apps/web/src/components/preview/previewAutomationErrors.ts +++ b/apps/web/src/components/preview/previewAutomationErrors.ts @@ -12,6 +12,7 @@ import { ThreadId, TrimmedNonEmptyString, } from "@t3tools/contracts"; +import { previewFailureReason } from "@t3tools/shared/previewFailureReason"; import * as Schema from "effect/Schema"; export interface PreviewAutomationOperationContext { @@ -235,6 +236,13 @@ export function serializePreviewAutomationHostError( key !== "_tag" && key !== "cause" && key !== "name" && key !== "message" && key !== "stack", ), ); + // The wrapped cause is not serializable, but its sanitized summary is what + // tells the agent why the operation failed (e.g. a capture timeout). + const reason = + error._tag === "PreviewAutomationOperationError" + ? previewFailureReason(error.cause) + : undefined; + if (reason !== undefined) detail.reason = reason; return { _tag: "responseTag" in error ? error.responseTag : error._tag, message: error.message, diff --git a/apps/web/src/components/preview/previewAutomationRequestConsumer.test.ts b/apps/web/src/components/preview/previewAutomationRequestConsumer.test.ts index af3a95c32c784..25d42df8e1a78 100644 --- a/apps/web/src/components/preview/previewAutomationRequestConsumer.test.ts +++ b/apps/web/src/components/preview/previewAutomationRequestConsumer.test.ts @@ -324,7 +324,7 @@ describe("previewAutomationRequestConsumer", () => { }); }); - it("correlates unexpected failures without exposing cause details", () => { + it("correlates unexpected failures with a redacted reason instead of raw cause details", () => { const cause = new Error("private bridge token: preview-secret"); const context = { requestId: "request-2", @@ -345,11 +345,29 @@ describe("previewAutomationRequestConsumer", () => { environmentId: "environment-1", threadId: "thread-1", tabId: "tab-1", + reason: "private bridge token: ", }, }); expect(JSON.stringify(response)).not.toContain("preview-secret"); }); + it("forwards why a desktop capture failed, without local paths", () => { + const ipcFailure = new Error( + "Error invoking remote method 'desktop:preview:automation-snapshot': Error: Desktop preview operation failed: automationSnapshot.capturePage (artifact /Users/someone/.t3/shot.png): TimeoutError: Operation timed out after '1s'", + ); + const response = serializePreviewAutomationError(ipcFailure, { + requestId: "request-3", + operation: "snapshot" as const, + environmentId, + threadId, + tabId, + }); + const reason = (response.detail as { reason?: string }).reason; + expect(reason).toContain("automationSnapshot.capturePage"); + expect(reason).toContain("Operation timed out after '1s'"); + expect(reason).not.toContain("/Users/someone"); + }); + it("sanitizes unexpected handler failures at the response boundary", async () => { const requestsAtom = Atom.make>( AsyncResult.initial(false), @@ -398,6 +416,7 @@ describe("previewAutomationRequestConsumer", () => { environmentId: "environment-1", threadId: "thread-1", tabId: "tab-1", + reason: "desktop IPC secret: ", }, }, }); diff --git a/packages/contracts/src/previewAutomation.ts b/packages/contracts/src/previewAutomation.ts index c022fed26172b..551166784fced 100644 --- a/packages/contracts/src/previewAutomation.ts +++ b/packages/contracts/src/previewAutomation.ts @@ -784,10 +784,13 @@ export class PreviewAutomationExecutionError extends Schema.TaggedError { + it("joins a wrapped cause chain into one actionable line", () => { + const timeout = Object.assign(new Error("Operation timed out after '1s'"), { + _tag: "TimeoutError", + }); + const wrapper = new Error("Desktop preview operation failed: automationSnapshot.capturePage", { + cause: timeout, + }); + expect(previewFailureReason(wrapper)).toBe( + "Desktop preview operation failed: automationSnapshot.capturePage: TimeoutError: Operation timed out after '1s'", + ); + }); + + it("does not repeat a cause the wrapper already quotes", () => { + const inner = new Error("target closed while handling command"); + const outer = new Error("Runtime.enable failed: target closed while handling command", { + cause: inner, + }); + expect(previewFailureReason(outer)).toBe( + "Runtime.enable failed: target closed while handling command", + ); + }); + + it("strips local paths, URL queries and token-like values", () => { + const text = sanitizePreviewFailureText( + "ERR_ABORTED loading 'https://app.example.com/callback?code=abc123&state=xyz' from file:///Users/nick/app/index.html at /Users/nick/.t3/userdata/x.png Authorization: Bearer-zzz " + + "a".repeat(48), + ); + expect(text).not.toContain("code=abc123"); + expect(text).not.toContain("/Users/nick"); + expect(text).not.toContain("Bearer-zzz"); + expect(text).not.toContain("a".repeat(48)); + expect(text).toContain("https://app.example.com/callback"); + }); + + it("bounds the reason and ignores causes without text", () => { + expect( + previewFailureReason(new Error("x".repeat(20) + " " + "word ".repeat(200))), + ).toHaveLength(300); + expect(previewFailureReason({})).toBeUndefined(); + expect(previewFailureReason(undefined)).toBeUndefined(); + }); +}); diff --git a/packages/shared/src/previewFailureReason.ts b/packages/shared/src/previewFailureReason.ts new file mode 100644 index 0000000000000..3fec5de050979 --- /dev/null +++ b/packages/shared/src/previewFailureReason.ts @@ -0,0 +1,61 @@ +/** + * Turns a preview automation failure cause into a short reason an agent can act + * on, such as "TimeoutError: Operation timed out after '1s'". Causes cross the + * renderer, desktop IPC and server boundaries, so the text is stripped of local + * paths, URL queries and token-like strings before it leaves the machine's logs. + */ +const MAX_REASON_LENGTH = 300; +const MAX_CAUSE_DEPTH = 4; + +const HOME_PATH = /(?:\/Users|\/home|[A-Za-z]:\\Users)[\\/][^\s'"`)\]]+/g; +const FILE_URL = /file:\/\/[^\s'"`)\]]+/g; +const URL_QUERY = /(https?:\/\/[^\s?#'"`)\]]+)[?#][^\s'"`)\]]*/g; +const BEARER = /\b(bearer|token|authorization|cookie|password|secret)([=: ]+)[^\s,;'"`)\]]+/gi; +const OPAQUE_TOKEN = /\b[A-Za-z0-9_-]{40,}\b/g; + +export const sanitizePreviewFailureText = (text: string): string => + text + .replace(FILE_URL, "") + .replace(HOME_PATH, "") + .replace(URL_QUERY, "$1") + .replace(BEARER, "$1$2") + .replace(OPAQUE_TOKEN, "") + .replace(/\s+/g, " ") + .trim(); + +const ownMessage = (value: unknown): string | undefined => { + if (typeof value === "string") return value; + if (typeof value !== "object" || value === null) return undefined; + const message = "message" in value && typeof value.message === "string" ? value.message : ""; + const tag = + "_tag" in value && typeof value._tag === "string" + ? value._tag + : value instanceof Error && value.name !== "Error" + ? value.name + : undefined; + if (!message) return tag; + return tag && !message.startsWith(tag) ? `${tag}: ${message}` : message; +}; + +/** Returns undefined when the cause carries nothing beyond its wrapper. */ +export const previewFailureReason = (cause: unknown): string | undefined => { + const parts: string[] = []; + let current: unknown = cause; + for (let depth = 0; depth < MAX_CAUSE_DEPTH && current !== undefined; depth += 1) { + const message = ownMessage(current); + if (message) { + const firstLine = message.split("\n", 1)[0]?.trim() ?? ""; + if (firstLine && !parts.some((part) => part.includes(firstLine))) parts.push(firstLine); + } + current = + typeof current === "object" && current !== null && "cause" in current + ? current.cause + : undefined; + } + if (parts.length === 0) return undefined; + const reason = sanitizePreviewFailureText(parts.join(": ")); + if (!reason) return undefined; + return reason.length > MAX_REASON_LENGTH + ? `${reason.slice(0, MAX_REASON_LENGTH - 3)}...` + : reason; +}; From f2de483dbd05665e9aad5ab296a8dea12efa3f56 Mon Sep 17 00:00:00 2001 From: Nick Roberts <9039456+nickrroberts@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:50:58 -0600 Subject: [PATCH 2/6] fix(desktop): recover dropped browser debugger sessions and bound recording warmup - Reattach a tab's CDP session when Chromium drops the debugger on its own, instead of failing every later command (upstream #17525). - Fail cleanly when a guest is already destroyed (from upstream #14727). - Bound the recording warmup capture so a stalled capturePage cannot hold the tab lock forever (upstream #20085). - Fold the sanitized cause of a PreviewOperationError into the IPC failure text, so renderer and agent see e.g. a capture timeout. Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/ipc/methods/preview.test.ts | 29 ++++ apps/desktop/src/ipc/methods/preview.ts | 88 +++++++---- apps/desktop/src/preview/Manager.test.ts | 146 +++++++++++++++++++ apps/desktop/src/preview/Manager.ts | 71 +++++++-- 4 files changed, 299 insertions(+), 35 deletions(-) diff --git a/apps/desktop/src/ipc/methods/preview.test.ts b/apps/desktop/src/ipc/methods/preview.test.ts index 18b0b8040e3d4..3216d5c7a27b5 100644 --- a/apps/desktop/src/ipc/methods/preview.test.ts +++ b/apps/desktop/src/ipc/methods/preview.test.ts @@ -170,3 +170,32 @@ describe("preview IPC methods", () => { ).toThrow(); }); }); + +describe("describePreviewIpcFailure", () => { + it("folds a desktop operation's sanitized cause into the text the renderer receives", () => { + const timeout = Object.assign(new Error("Operation timed out after '1s'"), { + _tag: "TimeoutError", + }); + const failure = PreviewIpc.describePreviewIpcFailure( + new PreviewManager.PreviewOperationError({ + operation: "automationSnapshot.capturePage", + tabId: "tab_1", + artifactPath: "/Users/someone/.t3/shot.png", + cause: timeout, + }), + ); + expect(failure.message).toBe( + "Desktop preview operation failed: automationSnapshot.capturePage (tab tab_1, artifact /Users/someone/.t3/shot.png): TimeoutError: Operation timed out after '1s'", + ); + }); + + it("leaves already-descriptive errors and causeless failures unchanged", () => { + const notFound = new PreviewManager.PreviewTabNotFoundError({ tabId: "tab_1" }); + expect(PreviewIpc.describePreviewIpcFailure(notFound)).toBe(notFound); + const bare = new PreviewManager.PreviewOperationError({ + operation: "navigate.loadURL", + cause: {}, + }); + expect(PreviewIpc.describePreviewIpcFailure(bare)).toBe(bare); + }); +}); diff --git a/apps/desktop/src/ipc/methods/preview.ts b/apps/desktop/src/ipc/methods/preview.ts index 5fb7eff99fc67..8b45799eac431 100644 --- a/apps/desktop/src/ipc/methods/preview.ts +++ b/apps/desktop/src/ipc/methods/preview.ts @@ -28,6 +28,7 @@ import { DEFAULT_BROWSER_PROFILE_ID, INCOGNITO_BROWSER_PROFILE_ID, } from "@t3tools/contracts"; +import { previewFailureReason } from "@t3tools/shared/previewFailureReason"; import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; import * as NodeURL from "node:url"; @@ -39,6 +40,41 @@ import { PREVIEW_WEBVIEW_PREFERENCES } from "../../preview/WebviewPreferences.ts import * as IpcChannels from "../channels.ts"; import * as DesktopIpc from "../DesktopIpc.ts"; +/** + * Electron hands the renderer only an IPC failure's text. A `PreviewOperationError` + * names the step that failed but keeps its cause structured, so the cause (a + * capture timeout, a closed debugger target) is folded into the text here, + * sanitized, for the agent to act on. + */ +export class PreviewIpcOperationError extends Schema.TaggedError()( + "PreviewIpcOperationError", + { summary: Schema.String }, +) { + override get message(): string { + return this.summary; + } +} + +const isPreviewOperationError = Schema.is(PreviewManager.PreviewOperationError); + +export const describePreviewIpcFailure = (error: E): E | PreviewIpcOperationError => { + if (!isPreviewOperationError(error)) return error; + const reason = previewFailureReason(error.cause); + return reason === undefined + ? error + : new PreviewIpcOperationError({ summary: `${error.message}: ${reason}` }); +}; + +// The renderer only sees the failure's text, so widening the error type here is safe. +const makePreviewIpcMethod = ((registration) => { + const method = DesktopIpc.makeIpcMethod(registration); + return { + ...method, + handler: (raw, event) => + method.handler(raw, event).pipe(Effect.mapError(describePreviewIpcFailure)), + }; +}) as typeof DesktopIpc.makeIpcMethod; + export const installPreviewEventForwarding = Effect.fn( "desktop.ipc.preview.installEventForwarding", )(function* () { @@ -55,7 +91,7 @@ export const installPreviewEventForwarding = Effect.fn( ); }); -export const createTab = DesktopIpc.makeIpcMethod({ +export const createTab = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_CREATE_TAB_CHANNEL, payload: DesktopPreviewCreateTabInputSchema, result: Schema.Void, @@ -69,7 +105,7 @@ export const createTab = DesktopIpc.makeIpcMethod({ }), }); -export const closeTab = DesktopIpc.makeIpcMethod({ +export const closeTab = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_CLOSE_TAB_CHANNEL, payload: DesktopPreviewTabInputSchema, result: Schema.Void, @@ -79,7 +115,7 @@ export const closeTab = DesktopIpc.makeIpcMethod({ }), }); -export const registerWebview = DesktopIpc.makeIpcMethod({ +export const registerWebview = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_REGISTER_WEBVIEW_CHANNEL, payload: DesktopPreviewRegisterWebviewInputSchema, result: Schema.Void, @@ -89,7 +125,7 @@ export const registerWebview = DesktopIpc.makeIpcMethod({ }), }); -export const navigate = DesktopIpc.makeIpcMethod({ +export const navigate = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_NAVIGATE_CHANNEL, payload: DesktopPreviewNavigateInputSchema, result: Schema.Void, @@ -107,7 +143,7 @@ const tabMethod = ( tabId: string, ) => Effect.Effect, ) => - DesktopIpc.makeIpcMethod({ + makePreviewIpcMethod({ channel, payload: DesktopPreviewTabInputSchema, result: Schema.Void, @@ -152,7 +188,7 @@ export const hardReload = tabMethod( "desktop.ipc.preview.hardReload", (manager, tabId) => manager.hardReload(tabId), ); -export const setColorScheme = DesktopIpc.makeIpcMethod({ +export const setColorScheme = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL, payload: DesktopPreviewSetColorSchemeInputSchema, result: Schema.Void, @@ -161,7 +197,7 @@ export const setColorScheme = DesktopIpc.makeIpcMethod({ yield* manager.setColorScheme(tabId, colorScheme); }), }); -export const setAudioMuted = DesktopIpc.makeIpcMethod({ +export const setAudioMuted = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_SET_AUDIO_MUTED_CHANNEL, payload: DesktopPreviewSetAudioMutedInputSchema, result: Schema.Void, @@ -201,7 +237,7 @@ export const closePictureInPicture = tabMethod( (manager, tabId) => manager.closePictureInPicture(tabId), ); -export const clearCookies = DesktopIpc.makeIpcMethod({ +export const clearCookies = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL, payload: DesktopPreviewClearDataInputSchema, result: Schema.Void, @@ -211,7 +247,7 @@ export const clearCookies = DesktopIpc.makeIpcMethod({ }), }); -export const clearCache = DesktopIpc.makeIpcMethod({ +export const clearCache = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_CLEAR_CACHE_CHANNEL, payload: DesktopPreviewClearDataInputSchema, result: Schema.Void, @@ -269,7 +305,7 @@ const resolveClearPartitions = Effect.fn("desktop.ipc.preview.resolveClearPartit return [yield* manager.getBrowserPartition(scope, persistent, namespace)]; }); -export const getPreviewConfig = DesktopIpc.makeIpcMethod({ +export const getPreviewConfig = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_GET_CONFIG_CHANNEL, payload: DesktopPreviewConfigInputSchema, result: DesktopPreviewWebviewConfigSchema, @@ -293,7 +329,7 @@ export const getPreviewConfig = DesktopIpc.makeIpcMethod({ * context and their own failure type, so they do not unify with the * manager-backed handlers the shared loop iterates. */ -export const listBrowserImportSources = DesktopIpc.makeIpcMethod({ +export const listBrowserImportSources = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_IMPORT_SOURCES_CHANNEL, payload: Schema.Void, result: Schema.Array(BrowserImportSource), @@ -303,7 +339,7 @@ export const listBrowserImportSources = DesktopIpc.makeIpcMethod({ }), }); -export const importBrowserCookies = DesktopIpc.makeIpcMethod({ +export const importBrowserCookies = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_IMPORT_COOKIES_CHANNEL, payload: DesktopPreviewImportCookiesInputSchema, result: BrowserImportResult, @@ -327,7 +363,7 @@ export const importBrowserCookies = DesktopIpc.makeIpcMethod({ }), }); -export const setAnnotationTheme = DesktopIpc.makeIpcMethod({ +export const setAnnotationTheme = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_SET_ANNOTATION_THEME_CHANNEL, payload: DesktopPreviewAnnotationThemeInputSchema, result: Schema.Void, @@ -337,7 +373,7 @@ export const setAnnotationTheme = DesktopIpc.makeIpcMethod({ }), }); -export const pickElement = DesktopIpc.makeIpcMethod({ +export const pickElement = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_PICK_ELEMENT_CHANNEL, payload: DesktopPreviewTabInputSchema, result: Schema.NullOr(PreviewAnnotationSubmissionResultSchema), @@ -347,7 +383,7 @@ export const pickElement = DesktopIpc.makeIpcMethod({ }), }); -export const captureScreenshot = DesktopIpc.makeIpcMethod({ +export const captureScreenshot = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_CAPTURE_SCREENSHOT_CHANNEL, payload: DesktopPreviewTabInputSchema, result: DesktopPreviewScreenshotArtifactSchema, @@ -357,7 +393,7 @@ export const captureScreenshot = DesktopIpc.makeIpcMethod({ }), }); -export const revealArtifact = DesktopIpc.makeIpcMethod({ +export const revealArtifact = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_REVEAL_ARTIFACT_CHANNEL, payload: DesktopPreviewArtifactInputSchema, result: Schema.Void, @@ -367,7 +403,7 @@ export const revealArtifact = DesktopIpc.makeIpcMethod({ }), }); -export const copyArtifactToClipboard = DesktopIpc.makeIpcMethod({ +export const copyArtifactToClipboard = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_COPY_ARTIFACT_CHANNEL, payload: DesktopPreviewArtifactInputSchema, result: Schema.Void, @@ -377,7 +413,7 @@ export const copyArtifactToClipboard = DesktopIpc.makeIpcMethod({ }), }); -export const automationStatus = DesktopIpc.makeIpcMethod({ +export const automationStatus = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_AUTOMATION_STATUS_CHANNEL, payload: DesktopPreviewTabInputSchema, result: DesktopPreviewAutomationStatusSchema, @@ -387,7 +423,7 @@ export const automationStatus = DesktopIpc.makeIpcMethod({ }), }); -export const automationSnapshot = DesktopIpc.makeIpcMethod({ +export const automationSnapshot = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_AUTOMATION_SNAPSHOT_CHANNEL, payload: DesktopPreviewTabInputSchema, result: PreviewAutomationSnapshot, @@ -397,7 +433,7 @@ export const automationSnapshot = DesktopIpc.makeIpcMethod({ }), }); -export const automationClick = DesktopIpc.makeIpcMethod({ +export const automationClick = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_AUTOMATION_CLICK_CHANNEL, payload: DesktopPreviewAutomationClickInputSchema, result: Schema.Void, @@ -407,7 +443,7 @@ export const automationClick = DesktopIpc.makeIpcMethod({ }), }); -export const automationType = DesktopIpc.makeIpcMethod({ +export const automationType = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_AUTOMATION_TYPE_CHANNEL, payload: DesktopPreviewAutomationTypeInputSchema, result: Schema.Void, @@ -417,7 +453,7 @@ export const automationType = DesktopIpc.makeIpcMethod({ }), }); -export const automationPress = DesktopIpc.makeIpcMethod({ +export const automationPress = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_AUTOMATION_PRESS_CHANNEL, payload: DesktopPreviewAutomationPressInputSchema, result: Schema.Void, @@ -427,7 +463,7 @@ export const automationPress = DesktopIpc.makeIpcMethod({ }), }); -export const automationScroll = DesktopIpc.makeIpcMethod({ +export const automationScroll = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_AUTOMATION_SCROLL_CHANNEL, payload: DesktopPreviewAutomationScrollInputSchema, result: Schema.Void, @@ -437,7 +473,7 @@ export const automationScroll = DesktopIpc.makeIpcMethod({ }), }); -export const automationEvaluate = DesktopIpc.makeIpcMethod({ +export const automationEvaluate = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_AUTOMATION_EVALUATE_CHANNEL, payload: DesktopPreviewAutomationEvaluateInputSchema, result: Schema.Unknown, @@ -447,7 +483,7 @@ export const automationEvaluate = DesktopIpc.makeIpcMethod({ }), }); -export const automationWaitFor = DesktopIpc.makeIpcMethod({ +export const automationWaitFor = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_AUTOMATION_WAIT_FOR_CHANNEL, payload: DesktopPreviewAutomationWaitForInputSchema, result: Schema.Void, @@ -457,7 +493,7 @@ export const automationWaitFor = DesktopIpc.makeIpcMethod({ }), }); -export const saveRecording = DesktopIpc.makeIpcMethod({ +export const saveRecording = makePreviewIpcMethod({ channel: IpcChannels.PREVIEW_RECORDING_SAVE_CHANNEL, payload: DesktopPreviewRecordingSaveInputSchema, result: DesktopPreviewRecordingArtifactSchema, diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts index 7b76a1b8003ab..574c176d8affe 100644 --- a/apps/desktop/src/preview/Manager.test.ts +++ b/apps/desktop/src/preview/Manager.test.ts @@ -1730,6 +1730,109 @@ describe("PreviewManager", () => { }; }; + const makeDetachableGuest = (id: number) => { + const detachListeners = new Set<() => void>(); + let chromiumDetaches = 0; + let destroyed = false; + const attach = vi.fn(); + const detach = vi.fn(); + const sendCommand = vi.fn<(method: string, params?: unknown) => Promise>( + async () => undefined, + ); + const wc = { + id, + isDestroyed: () => destroyed, + isDevToolsOpened: () => false, + getType: () => "webview", + getURL: () => "https://example.com", + getTitle: () => "Example", + isLoading: () => false, + getZoomFactor: () => 1, + setZoomFactor: vi.fn(), + setAudioMuted: vi.fn(), + isCurrentlyAudible: () => false, + loadURL: vi.fn(async () => undefined), + on: vi.fn(), + off: vi.fn(), + ipc: { on: vi.fn(), off: vi.fn() }, + send: webviewSend, + navigationHistory: { canGoBack: () => false, canGoForward: () => false }, + setIgnoreMenuShortcuts: vi.fn(), + setWindowOpenHandler: vi.fn(), + debugger: { + isAttached: () => attach.mock.calls.length > detach.mock.calls.length + chromiumDetaches, + attach, + detach, + sendCommand, + on: (event: string, listener: () => void) => { + if (event === "detach") detachListeners.add(listener); + }, + off: (event: string, listener: () => void) => { + if (event === "detach") detachListeners.delete(listener); + }, + }, + }; + return { + wc: wc as unknown as Electron.WebContents, + attach, + detach, + sendCommand, + /** Chromium dropping the debugger (target crash or swap) without a detach() call. */ + chromiumDetach: () => { + chromiumDetaches += 1; + for (const listener of Array.from(detachListeners)) listener(); + }, + destroy: () => { + destroyed = true; + }, + }; + }; + + effectIt.effect("reattaches a debugger that Chromium dropped on its own", () => + withManager((manager) => + Effect.gen(function* () { + const guest = makeDetachableGuest(48); + fromId.mockReturnValue(guest.wc); + yield* manager.createTab("tab_detached"); + yield* manager.registerWebview("tab_detached", 48); + yield* Effect.yieldNow; + expect(guest.attach).toHaveBeenCalledTimes(1); + + guest.chromiumDetach(); + yield* Effect.yieldNow; + yield* Effect.yieldNow; + expect(guest.attach).toHaveBeenCalledTimes(2); + // The dropped session must not detach the shared debugger out from under its successor. + expect(guest.detach).not.toHaveBeenCalled(); + + // A second drop recovers again, so the tab never stays stuck on a dead session. + guest.chromiumDetach(); + yield* Effect.yieldNow; + yield* Effect.yieldNow; + expect(guest.attach).toHaveBeenCalledTimes(3); + }), + ), + ); + + effectIt.effect("does not reattach a debugger whose guest was destroyed", () => + withManager((manager) => + Effect.gen(function* () { + const guest = makeDetachableGuest(49); + fromId.mockReturnValue(guest.wc); + yield* manager.createTab("tab_detached_destroyed"); + yield* manager.registerWebview("tab_detached_destroyed", 49); + yield* Effect.yieldNow; + expect(guest.attach).toHaveBeenCalledTimes(1); + + guest.destroy(); + guest.chromiumDetach(); + yield* Effect.yieldNow; + yield* Effect.yieldNow; + expect(guest.attach).toHaveBeenCalledTimes(1); + }), + ), + ); + effectIt.effect("mutes the guest and re-applies the mute across webview swaps", () => withManager((manager) => Effect.gen(function* () { @@ -2885,6 +2988,49 @@ describe("PreviewManager", () => { ), ); + effectIt.effect("releases the tab when a recording warmup capture never settles", () => + withManager((manager) => + Effect.gen(function* () { + const warmupStarted = Promise.withResolvers(); + const stalled = Promise.withResolvers(); + const capturePage = vi.fn<() => Promise>(() => { + warmupStarted.resolve(); + return stalled.promise; + }); + const host = makeTestHostWebContents(); + const webContents = makeTestPreviewWebContents(capturePage, 42, host); + fromId.mockReturnValue(webContents); + yield* manager.createTab("tab_recording_stalled_warmup"); + yield* manager.registerWebview("tab_recording_stalled_warmup", 42); + try { + let started = false; + const start = yield* manager.startRecording("tab_recording_stalled_warmup").pipe( + Effect.andThen( + Effect.sync(() => { + started = true; + }), + ), + Effect.forkChild({ startImmediately: true }), + ); + yield* Effect.promise(() => warmupStarted.promise); + yield* TestClock.adjust(1_000); + expect(started).toBe(false); + // One retry, each attempt bounded, then recording proceeds without a warm source. + yield* TestClock.adjust(1_000); + yield* Fiber.join(start); + expect(started).toBe(true); + expect(capturePage).toHaveBeenCalledTimes(2); + yield* manager.stopRecording("tab_recording_stalled_warmup"); + } finally { + stalled.resolve({ + toJPEG: () => Buffer.from("late-frame"), + getSize: () => ({ width: 1280, height: 720 }), + }); + } + }), + ), + ); + effectIt.effect("continues native recording when the source warmup fails", () => withManager((manager) => Effect.gen(function* () { diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts index 151ead2c0a4a7..32eb0b88478c5 100644 --- a/apps/desktop/src/preview/Manager.ts +++ b/apps/desktop/src/preview/Manager.ts @@ -1243,6 +1243,15 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function > => { const existing = sessions.get(wc.id); if (existing) return Effect.succeed([existing, sessions] as const); + if (wc.isDestroyed()) { + return Effect.fail( + new PreviewOperationError({ + operation: "ensureControlSession", + webContentsId: wc.id, + cause: new Error("WebContents was destroyed"), + }), + ); + } if (wc.isDevToolsOpened()) { return Effect.fail( new PreviewAutomationDevToolsOpenError({ @@ -1311,6 +1320,14 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function const onMessage: BrowserControlSession["onMessage"] = (_event, method, params) => { runFork(handleDebuggerMessage(method, params)); }; + // Electron emits `detach` when Chromium closes the debugger session on its + // own (target crash or swap). The cached session would otherwise fail every + // later command until the webview is replaced. + let dropped = false; + const onDetach = () => { + dropped = true; + runFork(recoverDetachedControlSession(wc, control)); + }; yield* Scope.addFinalizer( scope, Effect.all( @@ -1320,9 +1337,14 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function copy.delete(wc.id); }), ), + // The listener comes off before detach() so a detach requested here + // never looks like a dropped one. A dropped session has nothing left + // to detach, and `wc.debugger` is shared, so detaching would hit a + // successor session. attempt({ operation: "detachControlSession", webContentsId: wc.id }, () => { wcDebugger.off("message", onMessage); - if (wcDebugger.isAttached()) wcDebugger.detach(); + wcDebugger.off("detach", onDetach); + if (!dropped && wcDebugger.isAttached()) wcDebugger.detach(); }).pipe(Effect.ignore), ], { discard: true }, @@ -1347,6 +1369,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function ); yield* attempt({ operation: "attachDebuggerListeners", webContentsId: wc.id }, () => { wcDebugger.on("message", onMessage); + wcDebugger.on("detach", onDetach); wcDebugger.attach("1.3"); }); yield* Effect.all( @@ -2688,6 +2711,30 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function } }).pipe(Effect.ignore); + // Only the session that saw the detach is replaced: by the time the event is + // handled the guest may already hold a newer one, which must stay. + const recoverDetachedControlSession = Effect.fn("PreviewManager.recoverDetachedControlSession")( + function* (wc: Electron.WebContents, detached: BrowserControlSession) { + const removed = yield* SynchronizedRef.modify(controlSessionsRef, (sessions) => + sessions.get(wc.id) === detached + ? ([ + true, + replaceMap(sessions, (copy) => { + copy.delete(wc.id); + }), + ] as const) + : ([false, sessions] as const), + ); + if (!removed) return; + yield* Scope.close(detached.scope, Exit.void).pipe(Effect.ignore); + if (wc.isDestroyed()) return; + const tabId = yield* tabIdForWebContents(wc.id); + if (tabId === null) return; + yield* restoreControlSession(tabId, wc); + }, + (recover) => Effect.ignore(recover), + ); + const setColorScheme = Effect.fn("PreviewManager.setColorScheme")(function* ( tabId: string, colorScheme: DesktopPreviewColorScheme, @@ -3418,14 +3465,20 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function if (requestWebContents === null) { return yield* new PreviewMainWindowClosedError({ tabId }); } - yield* attemptPromise( - { - operation: "recording.warmSource", - tabId, - webContentsId: wc.id, - }, - () => wc.capturePage().then(() => undefined), - ).pipe(Effect.retry({ times: 1 }), Effect.ignore); + const warmupContext = { operation: "recording.warmSource", tabId, webContentsId: wc.id }; + yield* Effect.tryPromise({ + // Electron cannot cancel capturePage, but a stalled warmup must release the tab lock. + try: (_signal) => wc.capturePage(), + catch: (cause) => new PreviewOperationError({ ...warmupContext, cause }), + }).pipe( + Effect.timeout(CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS), + Effect.catchTags({ + TimeoutError: (cause) => + Effect.fail(new PreviewOperationError({ ...warmupContext, cause })), + }), + Effect.retry({ times: 1 }), + Effect.ignore, + ); const currentWebContents = yield* requireWebContents(tabId); if (currentWebContents !== wc || wc.isDestroyed()) { return yield* new PreviewWebContentsNotFoundError({ From 41a84d2ef34166d101cdc1bb72a78bfa5b40522f Mon Sep 17 00:00:00 2001 From: Nick Roberts <9039456+nickrroberts@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:51:00 -0600 Subject: [PATCH 3/6] fix(web): browser tabs keep a fixed render order so reorders don't destroy them Electron destroys a webview's guest when its element moves, so following the server's list order blanked tabs and dropped their debugger sessions mid-automation (upstream #13799). Co-Authored-By: Claude Opus 5.5 --- apps/web/src/browser/ElectronBrowserHost.tsx | 25 +----- .../src/browser/hostedBrowserSessions.test.ts | 81 +++++++++++++++++++ apps/web/src/browser/hostedBrowserSessions.ts | 43 ++++++++++ 3 files changed, 126 insertions(+), 23 deletions(-) create mode 100644 apps/web/src/browser/hostedBrowserSessions.test.ts create mode 100644 apps/web/src/browser/hostedBrowserSessions.ts diff --git a/apps/web/src/browser/ElectronBrowserHost.tsx b/apps/web/src/browser/ElectronBrowserHost.tsx index de7e236032989..0800ce9944761 100644 --- a/apps/web/src/browser/ElectronBrowserHost.tsx +++ b/apps/web/src/browser/ElectronBrowserHost.tsx @@ -1,6 +1,5 @@ "use client"; -import { parseScopedThreadKey } from "@t3tools/client-runtime/environment"; import { FILL_PREVIEW_VIEWPORT } from "@t3tools/contracts"; import { useEffect, useMemo } from "react"; @@ -11,32 +10,12 @@ import { useActivePreviewSessions } from "~/previewStateStore"; import { readPreviewAnnotationTheme } from "./annotationTheme"; import { useBrowserPointerStore } from "./browserPointerStore"; import { HostedBrowserWebview } from "./HostedBrowserWebview"; -import { previewRuntimeTabId } from "./previewRuntimeTabId"; +import { hostedBrowserSessions } from "./hostedBrowserSessions"; export function ElectronBrowserHost() { const { resolvedTheme } = useTheme(); const previewByThreadKey = useActivePreviewSessions(); - const sessions = useMemo( - () => - Object.entries(previewByThreadKey).flatMap(([threadKey, previewState]) => { - const threadRef = parseScopedThreadKey(threadKey); - return threadRef - ? Object.values(previewState.sessions).map((snapshot) => ({ - threadRef, - snapshot, - runtimeTabId: previewRuntimeTabId( - threadRef, - previewState.serverEpoch, - snapshot.tabId, - ), - pictureInPicture: - previewState.desktopByTabId[snapshot.tabId]?.pictureInPicture ?? false, - zoomFactor: previewState.desktopByTabId[snapshot.tabId]?.zoomFactor ?? 1, - })) - : []; - }), - [previewByThreadKey], - ); + const sessions = useMemo(() => hostedBrowserSessions(previewByThreadKey), [previewByThreadKey]); useEffect(() => { const preview = window.desktopBridge?.preview; diff --git a/apps/web/src/browser/hostedBrowserSessions.test.ts b/apps/web/src/browser/hostedBrowserSessions.test.ts new file mode 100644 index 0000000000000..df7b06831828d --- /dev/null +++ b/apps/web/src/browser/hostedBrowserSessions.test.ts @@ -0,0 +1,81 @@ +import { scopedThreadKey } from "@t3tools/client-runtime/environment"; +import type { PreviewSessionSnapshot, ScopedThreadRef } from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { __testing, type ThreadPreviewState } from "~/previewStateStore"; + +import { hostedBrowserSessions } from "./hostedBrowserSessions"; + +const threadA = { + environmentId: "local" as ScopedThreadRef["environmentId"], + threadId: "thread-a" as ScopedThreadRef["threadId"], +}; +const threadB = { ...threadA, threadId: "thread-b" as ScopedThreadRef["threadId"] }; + +const snapshot = ( + threadRef: ScopedThreadRef, + tabId: string, + updatedAt: string, +): PreviewSessionSnapshot => ({ + threadId: threadRef.threadId, + tabId, + navStatus: { _tag: "Success", url: `http://localhost:3000/${tabId}`, title: tabId }, + canGoBack: false, + canGoForward: false, + updatedAt, +}); + +const threadState = (sessions: PreviewSessionSnapshot[]): ThreadPreviewState => ({ + ...__testing.EMPTY_THREAD_PREVIEW_STATE, + sessions: Object.fromEntries(sessions.map((session) => [session.tabId, session])), + serverEpoch: "epoch-1", +}); + +describe("hostedBrowserSessions", () => { + it("keeps webview order fixed when the server reorders tabs and threads", () => { + const first = hostedBrowserSessions({ + [scopedThreadKey(threadA)]: threadState([ + snapshot(threadA, "tab-1", "2026-10-21T10:00:00.000Z"), + snapshot(threadA, "tab-2", "2026-10-21T10:00:01.000Z"), + ]), + [scopedThreadKey(threadB)]: threadState([ + snapshot(threadB, "tab-3", "2026-10-21T10:00:02.000Z"), + ]), + }); + // A newer update moves tab-1 to the end of the server list and thread B first. + const reordered = hostedBrowserSessions({ + [scopedThreadKey(threadB)]: threadState([ + snapshot(threadB, "tab-3", "2026-10-21T10:00:02.000Z"), + ]), + [scopedThreadKey(threadA)]: threadState([ + snapshot(threadA, "tab-2", "2026-10-21T10:00:01.000Z"), + snapshot(threadA, "tab-1", "2026-10-21T10:00:05.000Z"), + ]), + }); + + expect(reordered.map((session) => session.runtimeTabId)).toEqual( + first.map((session) => session.runtimeTabId), + ); + expect(first.map((session) => session.snapshot.tabId)).toHaveLength(3); + }); + + it("adding a tab does not move the tabs already rendered before it", () => { + const before = hostedBrowserSessions({ + [scopedThreadKey(threadA)]: threadState([ + snapshot(threadA, "tab-1", "2026-10-21T10:00:00.000Z"), + ]), + }); + const after = hostedBrowserSessions({ + [scopedThreadKey(threadA)]: threadState([ + snapshot(threadA, "tab-9", "2026-10-21T10:00:09.000Z"), + snapshot(threadA, "tab-1", "2026-10-21T10:00:00.000Z"), + ]), + }); + const kept = after.filter((session) => + before.some((existing) => existing.runtimeTabId === session.runtimeTabId), + ); + expect(kept.map((session) => session.runtimeTabId)).toEqual( + before.map((session) => session.runtimeTabId), + ); + }); +}); diff --git a/apps/web/src/browser/hostedBrowserSessions.ts b/apps/web/src/browser/hostedBrowserSessions.ts new file mode 100644 index 0000000000000..8824b86e503e7 --- /dev/null +++ b/apps/web/src/browser/hostedBrowserSessions.ts @@ -0,0 +1,43 @@ +import { parseScopedThreadKey } from "@t3tools/client-runtime/environment"; +import type { PreviewSessionSnapshot, ScopedThreadRef } from "@t3tools/contracts"; + +import type { ThreadPreviewState } from "~/previewStateStore"; + +import { previewRuntimeTabId } from "./previewRuntimeTabId"; + +export interface HostedBrowserSession { + readonly threadRef: ScopedThreadRef; + readonly snapshot: PreviewSessionSnapshot; + readonly runtimeTabId: string; + readonly pictureInPicture: boolean; + readonly zoomFactor: number; +} + +/** + * Every live preview tab, in a fixed render order. Electron destroys a + * webview's guest when its element moves in the DOM, so following the server's + * list order (which changes as tabs update) would blank tabs and drop their + * debugger sessions mid-automation. Any stable order works; this is not a + * display order. + */ +export function hostedBrowserSessions( + previewByThreadKey: Readonly>, +): HostedBrowserSession[] { + return Object.entries(previewByThreadKey) + .flatMap(([threadKey, previewState]) => { + const threadRef = parseScopedThreadKey(threadKey); + return threadRef + ? Object.values(previewState.sessions).map((snapshot) => ({ + threadRef, + snapshot, + runtimeTabId: previewRuntimeTabId(threadRef, previewState.serverEpoch, snapshot.tabId), + pictureInPicture: + previewState.desktopByTabId[snapshot.tabId]?.pictureInPicture ?? false, + zoomFactor: previewState.desktopByTabId[snapshot.tabId]?.zoomFactor ?? 1, + })) + : []; + }) + .toSorted((left, right) => + left.runtimeTabId < right.runtimeTabId ? -1 : left.runtimeTabId > right.runtimeTabId ? 1 : 0, + ); +} From 5aafa4e6e3eeb8d0c5d6ede35fbdd2928d7fb574 Mon Sep 17 00:00:00 2001 From: Nick Roberts <9039456+nickrroberts@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:51:03 -0600 Subject: [PATCH 4/6] fix(desktop): reload the main renderer after it is killed The automation host lives in the main renderer, so a killed renderer left agents with no browser host and a white window (upstream #20300). Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/window/DesktopWindow.test.ts | 36 +++++++++++++++++++ apps/desktop/src/window/DesktopWindow.ts | 1 + 2 files changed, 37 insertions(+) diff --git a/apps/desktop/src/window/DesktopWindow.test.ts b/apps/desktop/src/window/DesktopWindow.test.ts index deba1d6a5d76a..3df4a314da67d 100644 --- a/apps/desktop/src/window/DesktopWindow.test.ts +++ b/apps/desktop/src/window/DesktopWindow.test.ts @@ -1300,6 +1300,42 @@ describe("DesktopWindow", () => { }), ); + it.effect("reloads a killed renderer, bounded, and never revives a destroyed window", () => + Effect.gen(function* () { + const fakeWindow = makeFakeBrowserWindow(); + const createCount = yield* Ref.make(0); + const mainWindow = yield* Ref.make>(Option.none()); + const layer = makeTestLayer({ window: fakeWindow.window, createCount, mainWindow }); + + yield* Effect.gen(function* () { + const desktopWindow = yield* DesktopWindow.DesktopWindow; + yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773")); + const onGone = fakeWindow.webContentsListeners.get("render-process-gone"); + if (!onGone) return yield* Effect.die("render-process-gone listener missing"); + const loads = () => fakeWindow.loadURL.mock.calls.length; + const initialLoads = loads(); + + onGone({}, { reason: "clean-exit", exitCode: 0 }); + yield* TestClock.adjust(500); + assert.equal(loads(), initialLoads); + + // The automation host lives in this renderer, so a killed one must come back. + for (let attempt = 0; attempt < 4; attempt += 1) { + onGone({}, { reason: "killed", exitCode: 9 }); + yield* TestClock.adjust(500); + } + assert.equal(loads(), initialLoads + 3); + + yield* TestClock.adjust(60_000); + onGone({}, { reason: "killed", exitCode: 9 }); + fakeWindow.isDestroyed.mockReturnValue(true); + yield* TestClock.adjust(500); + assert.equal(loads(), initialLoads + 3); + assert.equal(yield* Ref.get(createCount), 1); + }).pipe(Effect.provide(layer)); + }), + ); + it.effect("recovers when the development renderer is temporarily unreachable", () => Effect.gen(function* () { const fakeWindow = makeFakeBrowserWindow(); diff --git a/apps/desktop/src/window/DesktopWindow.ts b/apps/desktop/src/window/DesktopWindow.ts index 2cd2fc54e2601..fee0b33f07b15 100644 --- a/apps/desktop/src/window/DesktopWindow.ts +++ b/apps/desktop/src/window/DesktopWindow.ts @@ -777,6 +777,7 @@ export const make = Effect.gen(function* () { const recoverable = details.reason === "crashed" || details.reason === "oom" || + details.reason === "killed" || details.reason === "abnormal-exit"; // Long sessions can OOM the renderer (V8 heap exhaustion from // accumulated thread state). Without a reload the user is left staring From bde4f9c121b6123bc87364577a3dc3c18cc64639 Mon Sep 17 00:00:00 2001 From: Nick Roberts <9039456+nickrroberts@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:14:40 -0600 Subject: [PATCH 5/6] fix(browser): address review of the stability batch - Release a crashed guest's debugger session so in-flight commands reject, fail fast with a reload hint until the page reloads, bound CDP domain enabling to 5s, and cap reattach attempts per guest. - The web host answers every request within its budget, so one slow operation can no longer look like a dead host. - Only responses count as host liveness; focus updates travel on a separate RPC. - Sanitizer covers Bearer/Basic values, JSON and key=value credentials, URL userinfo, spaced macOS paths and Windows forward-slash paths, without redacting ordinary words. Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/preview/Manager.test.ts | 72 +++++++++++++++++- apps/desktop/src/preview/Manager.ts | 75 ++++++++++++++++++- .../src/mcp/PreviewAutomationBroker.test.ts | 36 +++++++++ .../server/src/mcp/PreviewAutomationBroker.ts | 15 ++-- .../previewAutomationRequestConsumer.test.ts | 48 ++++++++++++ .../previewAutomationRequestConsumer.ts | 37 ++++++--- .../shared/src/previewFailureReason.test.ts | 35 +++++++-- packages/shared/src/previewFailureReason.ts | 16 +++- 8 files changed, 305 insertions(+), 29 deletions(-) diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts index 574c176d8affe..69d6c299a3083 100644 --- a/apps/desktop/src/preview/Manager.test.ts +++ b/apps/desktop/src/preview/Manager.test.ts @@ -1732,8 +1732,10 @@ describe("PreviewManager", () => { const makeDetachableGuest = (id: number) => { const detachListeners = new Set<() => void>(); + const goneListeners = new Set<() => void>(); let chromiumDetaches = 0; let destroyed = false; + let crashed = false; const attach = vi.fn(); const detach = vi.fn(); const sendCommand = vi.fn<(method: string, params?: unknown) => Promise>( @@ -1742,6 +1744,7 @@ describe("PreviewManager", () => { const wc = { id, isDestroyed: () => destroyed, + isCrashed: () => crashed, isDevToolsOpened: () => false, getType: () => "webview", getURL: () => "https://example.com", @@ -1752,8 +1755,12 @@ describe("PreviewManager", () => { setAudioMuted: vi.fn(), isCurrentlyAudible: () => false, loadURL: vi.fn(async () => undefined), - on: vi.fn(), - off: vi.fn(), + on: (event: string, listener: () => void) => { + if (event === "render-process-gone") goneListeners.add(listener); + }, + off: (event: string, listener: () => void) => { + if (event === "render-process-gone") goneListeners.delete(listener); + }, ipc: { on: vi.fn(), off: vi.fn() }, send: webviewSend, navigationHistory: { canGoBack: () => false, canGoForward: () => false }, @@ -1785,9 +1792,70 @@ describe("PreviewManager", () => { destroy: () => { destroyed = true; }, + crash: () => { + crashed = true; + for (const listener of Array.from(goneListeners)) listener(); + }, + reload: () => { + crashed = false; + }, }; }; + effectIt.effect("releases a crashed guest's debugger and fails fast until it reloads", () => + withManager((manager) => + Effect.gen(function* () { + const guest = makeDetachableGuest(50); + fromId.mockReturnValue(guest.wc); + yield* manager.createTab("tab_crashed"); + yield* manager.registerWebview("tab_crashed", 50); + yield* Effect.yieldNow; + expect(guest.attach).toHaveBeenCalledTimes(1); + + guest.crash(); + yield* Effect.yieldNow; + yield* Effect.yieldNow; + // Detaching rejects any command still waiting on the dead renderer. + expect(guest.detach).toHaveBeenCalledTimes(1); + guest.chromiumDetach(); + yield* Effect.yieldNow; + expect(guest.attach).toHaveBeenCalledTimes(1); + + const failure = yield* manager + .automationEvaluate("tab_crashed", { expression: "1" }) + .pipe(Effect.flip); + expect(String(failure.cause)).toContain("renderer crashed"); + expect(guest.attach).toHaveBeenCalledTimes(1); + + guest.reload(); + yield* manager.automationEvaluate("tab_crashed", { expression: "1" }).pipe(Effect.exit); + expect(guest.attach).toHaveBeenCalledTimes(2); + }), + ), + ); + + effectIt.effect("stops reattaching a debugger that keeps dropping", () => + withManager((manager) => + Effect.gen(function* () { + const guest = makeDetachableGuest(51); + fromId.mockReturnValue(guest.wc); + yield* manager.createTab("tab_flapping"); + yield* manager.registerWebview("tab_flapping", 51); + yield* Effect.yieldNow; + for (let drop = 0; drop < 5; drop += 1) { + guest.chromiumDetach(); + yield* Effect.yieldNow; + yield* Effect.yieldNow; + } + // One initial attach plus three bounded recoveries, then it stays detached. + expect(guest.attach).toHaveBeenCalledTimes(4); + // The next agent command still reattaches on demand. + yield* manager.automationEvaluate("tab_flapping", { expression: "1" }).pipe(Effect.exit); + expect(guest.attach).toHaveBeenCalledTimes(5); + }), + ), + ); + effectIt.effect("reattaches a debugger that Chromium dropped on its own", () => withManager((manager) => Effect.gen(function* () { diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts index 32eb0b88478c5..83f88e324dcfa 100644 --- a/apps/desktop/src/preview/Manager.ts +++ b/apps/desktop/src/preview/Manager.ts @@ -140,6 +140,16 @@ const PICTURE_IN_PICTURE_JPEG_QUALITY = 80; const CAPTURE_PAGE_RETRY_ATTEMPTS = 3; const CAPTURE_PAGE_RETRY_DELAY_MS = 120; const CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS = 1_000; +// Enabling CDP domains on a healthy guest takes milliseconds. A target that never +// answers must not hold the control-session lock every tab shares. +const CONTROL_SESSION_INIT_TIMEOUT_MS = 5_000; +// A target that drops its debugger again right after each reattach is not recoverable. +const DEBUGGER_RECOVERY_MAX_ATTEMPTS = 3; +const DEBUGGER_RECOVERY_WINDOW_MS = 60_000; + +// Commands sent to a crashed guest never settle. Test doubles may omit isCrashed. +const isGuestCrashed = (wc: Electron.WebContents): boolean => + (wc as { readonly isCrashed?: () => boolean }).isCrashed?.() === true; const PICTURE_IN_PICTURE_INITIAL_WIDTH = 480; const PICTURE_IN_PICTURE_INITIAL_HEIGHT = 320; const PICTURE_IN_PICTURE_MIN_WIDTH = 240; @@ -644,6 +654,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function ReadonlyMap >(new Map()); const diagnosticsRef = yield* Ref.make>(new Map()); + const debuggerRecoveriesRef = yield* Ref.make>>( + new Map(), + ); const expectedAgentInputsRef = yield* Ref.make< ReadonlyMap> >(new Map()); @@ -1241,6 +1254,15 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function readonly [BrowserControlSession, ReadonlyMap], PreviewManagerError > => { + if (!wc.isDestroyed() && isGuestCrashed(wc)) { + return Effect.fail( + new PreviewOperationError({ + operation: "ensureControlSession", + webContentsId: wc.id, + cause: new Error("The page's renderer crashed. Reload the tab to continue."), + }), + ); + } const existing = sessions.get(wc.id); if (existing) return Effect.succeed([existing, sessions] as const); if (wc.isDestroyed()) { @@ -1328,6 +1350,11 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function dropped = true; runFork(recoverDetachedControlSession(wc, control)); }; + // Commands in flight on a crashed guest never settle. Closing the session + // detaches the debugger, which rejects them so the caller gets an error. + const onGuestGone = () => { + runFork(releaseCrashedControlSession(wc.id, control)); + }; yield* Scope.addFinalizer( scope, Effect.all( @@ -1344,6 +1371,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function attempt({ operation: "detachControlSession", webContentsId: wc.id }, () => { wcDebugger.off("message", onMessage); wcDebugger.off("detach", onDetach); + if (!wc.isDestroyed()) wc.off("render-process-gone", onGuestGone); if (!dropped && wcDebugger.isAttached()) wcDebugger.detach(); }).pipe(Effect.ignore), ], @@ -1370,6 +1398,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function yield* attempt({ operation: "attachDebuggerListeners", webContentsId: wc.id }, () => { wcDebugger.on("message", onMessage); wcDebugger.on("detach", onDetach); + wc.on("render-process-gone", onGuestGone); wcDebugger.attach("1.3"); }); yield* Effect.all( @@ -1378,6 +1407,18 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function attemptPromise( { operation: `initializeDebugger.${method}`, webContentsId: wc.id }, () => wcDebugger.sendCommand(method), + ).pipe( + Effect.timeout(CONTROL_SESSION_INIT_TIMEOUT_MS), + Effect.catchTags({ + TimeoutError: (cause) => + Effect.fail( + new PreviewOperationError({ + operation: `initializeDebugger.${method}`, + webContentsId: wc.id, + cause, + }), + ), + }), ), ), { concurrency: "unbounded", discard: true }, @@ -2711,6 +2752,23 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function } }).pipe(Effect.ignore); + const releaseCrashedControlSession = Effect.fn("PreviewManager.releaseCrashedControlSession")( + function* (webContentsId: number, crashed: BrowserControlSession) { + const removed = yield* SynchronizedRef.modify(controlSessionsRef, (sessions) => + sessions.get(webContentsId) === crashed + ? ([ + true, + replaceMap(sessions, (copy) => { + copy.delete(webContentsId); + }), + ] as const) + : ([false, sessions] as const), + ); + if (removed) yield* Scope.close(crashed.scope, Exit.void).pipe(Effect.ignore); + }, + (release) => Effect.ignore(release), + ); + // Only the session that saw the detach is replaced: by the time the event is // handled the guest may already hold a newer one, which must stay. const recoverDetachedControlSession = Effect.fn("PreviewManager.recoverDetachedControlSession")( @@ -2727,7 +2785,22 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function ); if (!removed) return; yield* Scope.close(detached.scope, Exit.void).pipe(Effect.ignore); - if (wc.isDestroyed()) return; + // A crashed guest is reattached on demand after the webview reloads. + if (wc.isDestroyed() || isGuestCrashed(wc)) return; + const now = yield* currentMillis; + const allowed = yield* Ref.modify(debuggerRecoveriesRef, (recoveries) => { + const recent = (recoveries.get(wc.id) ?? []).filter( + (timestamp) => now - timestamp < DEBUGGER_RECOVERY_WINDOW_MS, + ); + const permitted = recent.length < DEBUGGER_RECOVERY_MAX_ATTEMPTS; + return [ + permitted, + replaceMap(recoveries, (copy) => { + copy.set(wc.id, permitted ? [...recent, now] : recent); + }), + ] as const; + }); + if (!allowed) return; const tabId = yield* tabIdForWebContents(wc.id); if (tabId === null) return; yield* restoreControlSession(tabId, wc); diff --git a/apps/server/src/mcp/PreviewAutomationBroker.test.ts b/apps/server/src/mcp/PreviewAutomationBroker.test.ts index c069b1194a49a..c4979d0a35b80 100644 --- a/apps/server/src/mcp/PreviewAutomationBroker.test.ts +++ b/apps/server/src/mcp/PreviewAutomationBroker.test.ts @@ -1598,3 +1598,39 @@ it.effect("reports the host's sanitized failure reason instead of a bare executi }), ), ); + +it.effect("focus updates alone do not keep a host whose request stream went silent", () => + Effect.scoped( + Effect.gen(function* () { + const broker = yield* makeBroker; + const connected = yield* Deferred.make(); + const delivered = yield* Deferred.make(); + const events = yield* broker.connect(makeHost()); + const consumer = yield* Stream.runForEach(events, (event) => + event.type === "connected" + ? Deferred.succeed(connected, event.connectionId) + : Deferred.succeed(delivered, undefined), + ).pipe(Effect.forkScoped); + const connectionId = yield* Deferred.await(connected); + + const stuck = yield* broker + .invoke({ scope, operation: "snapshot", input: {}, timeoutMs: 1_000 }) + .pipe(Effect.flip, Effect.forkScoped); + yield* Deferred.await(delivered); + // The window keeps reporting focus on a separate RPC while requests go unanswered. + yield* broker.focusHost({ + clientId: "client-1", + connectionId, + environmentId: scope.environmentId, + focused: true, + }); + yield* TestClock.adjust(2_000); + expect(yield* Fiber.join(stuck)).toMatchObject({ _tag: "PreviewAutomationTimeoutError" }); + const consumerExit = yield* Fiber.await(consumer); + expect(Exit.isFailure(consumerExit)).toBe(true); + expect( + yield* broker.invoke({ scope, operation: "status", input: {} }).pipe(Effect.flip), + ).toMatchObject({ _tag: "PreviewAutomationNoAvailableHostError" }); + }), + ), +); diff --git a/apps/server/src/mcp/PreviewAutomationBroker.ts b/apps/server/src/mcp/PreviewAutomationBroker.ts index 575f0db366ee3..134a91da23264 100644 --- a/apps/server/src/mcp/PreviewAutomationBroker.ts +++ b/apps/server/src/mcp/PreviewAutomationBroker.ts @@ -44,9 +44,9 @@ import { import * as McpInvocationContext from "./McpInvocationContext.ts"; /** - * The browser host starts its own operation timer only after the request is - * delivered, so its timeout reply lands just after ours. Wait this long past - * the request's budget before concluding the host never answered. + * The host answers every request within the request's own budget, measured from + * when it receives it, so its reply can land just after the broker's clock runs + * out. Wait this long past the budget before concluding the host never answered. */ const HOST_RESPONSE_GRACE_MS = 1_000; @@ -87,7 +87,11 @@ interface ClientConnection { readonly liveTabs: NonNullable; readonly focusOrder: number; readonly queue: Queue.Queue; - /** Bumped on every response or focus update, so a timeout can tell a busy host from a dead one. */ + /** + * Bumped on every response from this connection, so a timeout can tell a busy + * host from one that stopped serving its request stream. Focus updates travel + * on a separate RPC and do not prove the stream is being read. + */ readonly activity: number; } @@ -454,7 +458,6 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() { focused: host.focused, liveTabs: host.liveTabs ?? currentHost.liveTabs, focusOrder: host.focused ? focusSequence : currentHost.focusOrder, - activity: currentHost.activity + 1, }); return { ...current, clients, focusSequence }; }); @@ -645,7 +648,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() { Effect.gen(function* () { // Only this request fails. The connection is dropped (failing every // request on it, from any thread) only when the host has gone silent: - // no response or focus update since this request was delivered. A busy + // no response at all since this request was delivered. A busy // page, a slow background status read, or a host that is answering // other calls keeps its connection. Never replay: the client may have // applied the action before going quiet. diff --git a/apps/web/src/components/preview/previewAutomationRequestConsumer.test.ts b/apps/web/src/components/preview/previewAutomationRequestConsumer.test.ts index 25d42df8e1a78..435496b4036f5 100644 --- a/apps/web/src/components/preview/previewAutomationRequestConsumer.test.ts +++ b/apps/web/src/components/preview/previewAutomationRequestConsumer.test.ts @@ -152,6 +152,54 @@ describe("previewAutomationRequestConsumer", () => { registry.dispose(); }); + it("answers a stalled request with a timeout within its budget and drops the late result", async () => { + vi.useFakeTimers(); + try { + const requestsAtom = Atom.make>( + AsyncResult.initial(false), + ); + const stalled = Promise.withResolvers(); + const responses: PreviewAutomationResponse[] = []; + const state = consumerState(() => stalled.promise); + const consumerAtom = createPreviewAutomationRequestConsumerAtom({ + requestsAtom, + clientId, + connectionAtom: state.connectionAtom, + environmentId, + requestHandlerAtom: state.requestHandlerAtom, + respond: async (response) => { + responses.push(response); + }, + label: "test:preview-automation-deadline", + }); + const registry = AtomRegistry.make(); + registry.mount(consumerAtom); + registry.set( + requestsAtom, + AsyncResult.success( + requestEvent("request-stalled", { operation: "snapshot", timeoutMs: 2_000 }), + ), + ); + + await vi.advanceTimersByTimeAsync(1_999); + expect(responses).toHaveLength(0); + await vi.advanceTimersByTimeAsync(1); + expect(responses).toHaveLength(1); + expect(responses[0]).toMatchObject({ + requestId: "request-stalled", + ok: false, + error: { _tag: "PreviewAutomationTimeoutError" }, + }); + + stalled.resolve("late"); + await vi.advanceTimersByTimeAsync(0); + expect(responses).toHaveLength(1); + registry.dispose(); + } finally { + vi.useRealTimers(); + } + }); + it("uses the latest request handler without rebuilding the stream consumer", async () => { const requestsAtom = Atom.make>( AsyncResult.initial(false), diff --git a/apps/web/src/components/preview/previewAutomationRequestConsumer.ts b/apps/web/src/components/preview/previewAutomationRequestConsumer.ts index 89a9387e4af6d..5d071fada7df2 100644 --- a/apps/web/src/components/preview/previewAutomationRequestConsumer.ts +++ b/apps/web/src/components/preview/previewAutomationRequestConsumer.ts @@ -23,6 +23,8 @@ export function serializePreviewAutomationError( ); } +class PreviewAutomationHostDeadlineError extends Error {} + export function createPreviewAutomationRequestConsumerAtom(options: { readonly requestsAtom: Atom.Atom>; readonly clientId: PreviewAutomationHost["clientId"]; @@ -63,9 +65,18 @@ export function createPreviewAutomationRequestConsumerAtom(options: { return; } const request = event.request; - void get - .once(options.requestHandlerAtom) - .handle(request) + // Answer within the request's own budget. The broker waits only a short grace + // past it, and a host that never answers is treated as gone, which fails every + // other thread's calls on this host. A late result is dropped. + let timer: ReturnType | undefined; + const deadline = new Promise((_resolve, reject) => { + timer = setTimeout( + () => reject(new PreviewAutomationHostDeadlineError()), + request.timeoutMs, + ); + }); + void Promise.race([get.once(options.requestHandlerAtom).handle(request), deadline]) + .finally(() => clearTimeout(timer)) .then( (value) => options.respond({ @@ -81,13 +92,19 @@ export function createPreviewAutomationRequestConsumerAtom(options: { connectionId: event.connectionId, requestId: request.requestId, ok: false, - error: serializePreviewAutomationError(error, { - requestId: request.requestId, - operation: request.operation, - environmentId: options.environmentId, - threadId: request.threadId, - tabId: request.tabId ?? null, - }), + error: + error instanceof PreviewAutomationHostDeadlineError + ? { + _tag: "PreviewAutomationTimeoutError", + message: `Preview automation ${request.operation} did not finish within ${request.timeoutMs}ms on this client; it may still complete.`, + } + : serializePreviewAutomationError(error, { + requestId: request.requestId, + operation: request.operation, + environmentId: options.environmentId, + threadId: request.threadId, + tabId: request.tabId ?? null, + }), }), ); }; diff --git a/packages/shared/src/previewFailureReason.test.ts b/packages/shared/src/previewFailureReason.test.ts index 8362b15bf9464..26148ccbd4f94 100644 --- a/packages/shared/src/previewFailureReason.test.ts +++ b/packages/shared/src/previewFailureReason.test.ts @@ -25,18 +25,41 @@ describe("previewFailureReason", () => { ); }); - it("strips local paths, URL queries and token-like values", () => { + it("strips local paths, URL credentials and queries, and token-like values", () => { const text = sanitizePreviewFailureText( - "ERR_ABORTED loading 'https://app.example.com/callback?code=abc123&state=xyz' from file:///Users/nick/app/index.html at /Users/nick/.t3/userdata/x.png Authorization: Bearer-zzz " + + [ + "ERR_ABORTED loading 'https://user:hunter2@app.example.com/callback?code=abc123&state=xyz'", + "from file:///Users/nick/app/index.html", + "at /Users/nick/Library/Application Support/Control Plane/x.png", + "and C:/Users/nick/a.txt", + "Authorization: Bearer abc123def456", + '{"access_token":"sk-live-1"} api_key=k-2 x-api-key: k-3', "a".repeat(48), + ].join(" "), ); - expect(text).not.toContain("code=abc123"); - expect(text).not.toContain("/Users/nick"); - expect(text).not.toContain("Bearer-zzz"); - expect(text).not.toContain("a".repeat(48)); + for (const leaked of [ + "hunter2", + "code=abc123", + "/Users/nick", + "nick/a.txt", + "Support/Control", + "abc123def456", + "sk-live-1", + "k-2", + "k-3", + "a".repeat(48), + ]) { + expect(text).not.toContain(leaked); + } expect(text).toContain("https://app.example.com/callback"); }); + it("keeps ordinary words that only resemble credential keys", () => { + expect(sanitizePreviewFailureText("token expired while waiting; Cookie import failed")).toBe( + "token expired while waiting; Cookie import failed", + ); + }); + it("bounds the reason and ignores causes without text", () => { expect( previewFailureReason(new Error("x".repeat(20) + " " + "word ".repeat(200))), diff --git a/packages/shared/src/previewFailureReason.ts b/packages/shared/src/previewFailureReason.ts index 3fec5de050979..c11bcb641f262 100644 --- a/packages/shared/src/previewFailureReason.ts +++ b/packages/shared/src/previewFailureReason.ts @@ -7,18 +7,26 @@ const MAX_REASON_LENGTH = 300; const MAX_CAUSE_DEPTH = 4; -const HOME_PATH = /(?:\/Users|\/home|[A-Za-z]:\\Users)[\\/][^\s'"`)\]]+/g; const FILE_URL = /file:\/\/[^\s'"`)\]]+/g; +// Home and temp paths. A space continues the path only when the next word still +// holds a separator, so "Application Support/Control Plane/x" is covered while +// the text after the path is kept. +const LOCAL_PATH = + /(?:\/Users|\/home|\/private\/var|\/var\/folders|[A-Za-z]:[\\/]Users)[\\/][^\s'"`)\]]*(?: [^\s'"`)\]\\/]*[\\/][^\s'"`)\]]*)*/g; +const URL_USERINFO = /(\b[a-z][a-z0-9+.-]*:\/\/)[^\s/@'"`]+@/gi; const URL_QUERY = /(https?:\/\/[^\s?#'"`)\]]+)[?#][^\s'"`)\]]*/g; -const BEARER = /\b(bearer|token|authorization|cookie|password|secret)([=: ]+)[^\s,;'"`)\]]+/gi; +// key=value / key: value / "key":"value" credentials, including "Bearer ". +const CREDENTIAL = + /((?:^|[^A-Za-z0-9])(?:[A-Za-z]+[_-])?(?:authorization|cookie|password|passwd|secret|token|api[_-]?key|session(?:id)?|sid)["']?\s*[=:]\s*["']?)(?:(?:bearer|basic)\s+)?[^\s,;'"`)\]]+/gi; const OPAQUE_TOKEN = /\b[A-Za-z0-9_-]{40,}\b/g; export const sanitizePreviewFailureText = (text: string): string => text .replace(FILE_URL, "") - .replace(HOME_PATH, "") + .replace(LOCAL_PATH, "") + .replace(URL_USERINFO, "$1") .replace(URL_QUERY, "$1") - .replace(BEARER, "$1$2") + .replace(CREDENTIAL, "$1") .replace(OPAQUE_TOKEN, "") .replace(/\s+/g, " ") .trim(); From c55e1116ff35caa1d32707a950cd1e659d8b64ab Mon Sep 17 00:00:00 2001 From: Nick Roberts <9039456+nickrroberts@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:36:49 -0600 Subject: [PATCH 6/6] test(browser): type the new regression tests Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/preview/Manager.test.ts | 6 ++++-- apps/server/src/mcp/PreviewAutomationBroker.test.ts | 2 +- .../preview/previewAutomationRequestConsumer.test.ts | 8 +++++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts index 69d6c299a3083..1af273e8622ab 100644 --- a/apps/desktop/src/preview/Manager.test.ts +++ b/apps/desktop/src/preview/Manager.test.ts @@ -1823,8 +1823,10 @@ describe("PreviewManager", () => { const failure = yield* manager .automationEvaluate("tab_crashed", { expression: "1" }) - .pipe(Effect.flip); - expect(String(failure.cause)).toContain("renderer crashed"); + .pipe(Effect.match({ onFailure: (error) => error, onSuccess: () => undefined })); + expect(String((failure as { readonly cause?: unknown } | undefined)?.cause)).toContain( + "renderer crashed", + ); expect(guest.attach).toHaveBeenCalledTimes(1); guest.reload(); diff --git a/apps/server/src/mcp/PreviewAutomationBroker.test.ts b/apps/server/src/mcp/PreviewAutomationBroker.test.ts index c4979d0a35b80..47e849c2fb736 100644 --- a/apps/server/src/mcp/PreviewAutomationBroker.test.ts +++ b/apps/server/src/mcp/PreviewAutomationBroker.test.ts @@ -1629,7 +1629,7 @@ it.effect("focus updates alone do not keep a host whose request stream went sile const consumerExit = yield* Fiber.await(consumer); expect(Exit.isFailure(consumerExit)).toBe(true); expect( - yield* broker.invoke({ scope, operation: "status", input: {} }).pipe(Effect.flip), + yield* broker.invoke({ scope, operation: "status", input: {} }).pipe(Effect.flip), ).toMatchObject({ _tag: "PreviewAutomationNoAvailableHostError" }); }), ), diff --git a/apps/web/src/components/preview/previewAutomationRequestConsumer.test.ts b/apps/web/src/components/preview/previewAutomationRequestConsumer.test.ts index 435496b4036f5..3ccdf6aba150c 100644 --- a/apps/web/src/components/preview/previewAutomationRequestConsumer.test.ts +++ b/apps/web/src/components/preview/previewAutomationRequestConsumer.test.ts @@ -158,7 +158,13 @@ describe("previewAutomationRequestConsumer", () => { const requestsAtom = Atom.make>( AsyncResult.initial(false), ); - const stalled = Promise.withResolvers(); + let resolveStalled!: (value: unknown) => void; + const stalled = { + promise: new Promise((resolve) => { + resolveStalled = resolve; + }), + resolve: (value: unknown) => resolveStalled(value), + }; const responses: PreviewAutomationResponse[] = []; const state = consumerState(() => stalled.promise); const consumerAtom = createPreviewAutomationRequestConsumerAtom({