Skip to content
TypeScript

fix(server): keep preview browser connected after operation timeouts (#17693)

Julius Marminge5c715c694118
diff --git a/apps/server/src/mcp/McpHttpServer.test.ts b/apps/server/src/mcp/McpHttpServer.test.ts
index 330fc1840de..e7b5e90fbd8 100644
--- a/apps/server/src/mcp/McpHttpServer.test.ts
+++ b/apps/server/src/mcp/McpHttpServer.test.ts
@@ -234,14 +234,14 @@ it.effect.each([
).pipe(Effect.provide(layerTest)),
);
-it.effect("tells the agent how to fall back when no desktop app can run the snapshot", () =>
+it.effect("tells the agent how to recover when no preview host can run the snapshot", () =>
Effect.gen(function* () {
const snapshot = yield* callSnapshot({});
expect(snapshot.isError).toBe(true);
const [text] = snapshot.content;
expect(text?.type === "text" ? text.text : "").toContain(
- "use a headless browser from the shell",
+ "Retry preview_status, then call preview_open",
);
expect(snapshot.structuredContent).toMatchObject({
error: { _tag: "PreviewAutomationNoAvailableHostError" },
diff --git a/apps/server/src/mcp/PreviewAutomationBroker.test.ts b/apps/server/src/mcp/PreviewAutomationBroker.test.ts
index c8b02aa549e..72d5d4b044d 100644
--- a/apps/server/src/mcp/PreviewAutomationBroker.test.ts
+++ b/apps/server/src/mcp/PreviewAutomationBroker.test.ts
@@ -507,7 +507,6 @@ it.effect.each([
threadId: scope.thread.threadId,
});
expect(error.cause).toBe(remoteError);
- expect(error.message).toContain("remains on the desktop");
expect(error.message).not.toContain("remote recording details");
}),
),
@@ -1352,7 +1351,7 @@ it.effect("discards buffered actions before completing an evicted host stream",
})
.pipe(Effect.flip, Effect.forkScoped);
yield* Deferred.await(actionRouted);
- yield* TestClock.adjust(1_000);
+ yield* TestClock.adjust(2_000);
expect(yield* Fiber.join(timedOut)).toMatchObject({ _tag: "PreviewAutomationTimeoutError" });
expect(yield* Fiber.join(buffered)).toMatchObject({
_tag: "PreviewAutomationClientDisconnectedError",
@@ -1414,7 +1413,7 @@ it.effect("rejects a routed action when its generation is evicted before deliver
Effect.forkScoped,
);
yield* Deferred.await(actionRouted);
- yield* TestClock.adjust(1_000);
+ yield* TestClock.adjust(2_000);
expect(yield* Fiber.join(timedOut)).toMatchObject({ _tag: "PreviewAutomationTimeoutError" });
expect(Exit.isSuccess(yield* Fiber.await(consumer))).toBe(true);
@@ -1428,30 +1427,41 @@ it.effect("rejects a routed action when its generation is evicted before deliver
),
);
-it.effect("keeps a host that responds with an operation timeout", () =>
+it.effect("keeps a host whose operation timeout arrives just after the budget", () =>
Effect.scoped(
Effect.gen(function* () {
const broker = yield* makeBroker;
- const connected = yield* Deferred.make<void>();
- const events = yield* broker.connect(makeHost());
- yield* Stream.runForEach(events, (event) => {
- if (event.type === "connected") return Deferred.succeed(connected, undefined);
- return broker.respond({
- clientId: "client-1",
- connectionId: event.connectionId,
- requestId: event.request.requestId,
- ...(event.request.operation === "waitFor"
- ? {
- ok: false,
- error: { _tag: "PreviewAutomationTimeoutError", message: "Selector timed out" },
- }
- : { ok: true, result: "responsive" }),
- });
- }).pipe(Effect.forkScoped);
- yield* Deferred.await(connected);
- expect(
- yield* broker.invoke<void>({ scope, operation: "waitFor", input: {} }).pipe(Effect.flip),
- ).toMatchObject({ _tag: "PreviewAutomationTimeoutError" });
+ const received = yield* Deferred.make<void>();
+ const requests = requestsFrom(yield* broker.connect(makeHost()));
+ yield* Stream.runForEach(requests, (request) =>
+ Effect.gen(function* () {
+ if (request.operation === "waitFor") {
+ yield* Deferred.succeed(received, undefined);
+ // The browser starts its own timer after delivery, so its timeout lands late.
+ yield* Effect.sleep(request.timeoutMs + 50);
+ }
+ yield* broker.respond({
+ clientId: "client-1",
+ connectionId: request.connectionId,
+ requestId: request.requestId,
+ ...(request.operation === "waitFor"
+ ? {
+ ok: false,
+ error: { _tag: "PreviewAutomationTimeoutError", message: "Selector timed out" },
+ }
+ : { ok: true, result: "responsive" }),
+ });
+ }),
+ ).pipe(Effect.forkScoped);
+ const timedOut = yield* broker
+ .invoke<void>({ scope, operation: "waitFor", input: {}, timeoutMs: 1_000 })
+ .pipe(Effect.flip, Effect.forkScoped);
+ yield* Deferred.await(received);
+ yield* TestClock.adjust(1_050);
+ expect(yield* Fiber.join(timedOut)).toMatchObject({
+ _tag: "PreviewAutomationTimeoutError",
+ remoteTag: "PreviewAutomationTimeoutError",
+ });
expect(yield* broker.invoke({ scope, operation: "status", input: {} })).toBe("responsive");
}),
),
diff --git a/apps/server/src/mcp/PreviewAutomationBroker.ts b/apps/server/src/mcp/PreviewAutomationBroker.ts
index 7be02956eb3..47863242215 100644
--- a/apps/server/src/mcp/PreviewAutomationBroker.ts
+++ b/apps/server/src/mcp/PreviewAutomationBroker.ts
@@ -41,6 +41,8 @@ import * as SynchronizedRef from "effect/SynchronizedRef";
import * as McpInvocationContext from "./McpInvocationContext.ts";
+const HOST_RESPONSE_GRACE_MS = 1_000;
+
export interface PreviewAutomationInvokeInput {
/** Preview tabs belong to a thread, so only thread callers reach the broker. */
readonly scope: McpInvocationContext.McpThreadInvocationScope;
@@ -651,7 +653,11 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
}
return yield* new PreviewAutomationRequestQueueClosedError(requestContext);
}
- const result = yield* Deferred.await(deferred).pipe(Effect.timeoutOption(timeoutMs));
+ // The browser starts its operation timer after delivery. Allow its timeout
+ // response to arrive before treating the entire host as unresponsive.
+ 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* () {
diff --git a/packages/contracts/src/previewAutomation.ts b/packages/contracts/src/previewAutomation.ts
index 6634e73b699..504d28148b2 100644
--- a/packages/contracts/src/previewAutomation.ts
+++ b/packages/contracts/src/previewAutomation.ts
@@ -925,7 +925,7 @@ export class PreviewAutomationNoAvailableHostError extends Schema.TaggedError<Pr
},
) {
override get message(): string {
- return `No preview automation host is available for ${this.operation} in environment ${this.environmentId}. Preview tools run in a T3 Code desktop app that is open and connected to this environment; a headless server has no browser of its own. Do not retry. To check a page, use a headless browser from the shell, such as Playwright, or curl, or ask the user to open this thread in the T3 Code desktop app.`;
+ return `No preview automation host is available for ${this.operation} in environment ${this.environmentId}. The server-owned browser may be starting or reconnecting. Retry preview_status, then call preview_open if no tab is available. If it remains unavailable, report the browser connection failure.`;
}
}
@@ -1129,7 +1129,7 @@ export class PreviewAutomationRecordingTransferError extends Schema.TaggedError<
},
) {
override get message(): string {
- return "Preview recording could not be saved to the agent environment. The saved copy remains on the desktop.";
+ return "Preview recording could not be saved to the agent environment.";
}
}
@@ -1147,7 +1147,7 @@ export class PreviewAutomationRecordingTooLargeError extends Schema.TaggedError<
{ threadId: ThreadId, cause: Schema.optional(Schema.Defect()) },
) {
override get message(): string {
- return "The recording exceeds 50 MiB. The saved copy remains on the desktop.";
+ return "The recording exceeds 50 MiB. Make a shorter recording.";
}
}
@@ -1156,7 +1156,7 @@ export class PreviewAutomationRecordingDeadlineExpiredError extends Schema.Tagge
{ threadId: ThreadId, cause: Schema.optional(Schema.Defect()) },
) {
override get message(): string {
- return "The recording transfer deadline expired. The saved copy remains on the desktop.";
+ return "The recording transfer deadline expired.";
}
}