Skip to content

Commit 1115032

Browse files
fix(server): a stalled preview capture no longer times out snapshots and drops the agent's tab
1 parent 4cb29a5 commit 1115032

7 files changed

Lines changed: 312 additions & 46 deletions

File tree

‎apps/server/src/mcp/PreviewAutomationBroker.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as Deferred from "effect/Deferred";
1818
import * as Fiber from "effect/Fiber";
1919
import * as Result from "effect/Result";
2020
import * as Stream from "effect/Stream";
21+
import * as TestClock from "effect/testing/TestClock";
2122

2223
import * as PreviewAutomationBroker from "./PreviewAutomationBroker.ts";
2324

@@ -925,6 +926,61 @@ it.effect("does not carry a tab id across a replacement automation stream", () =
925926
),
926927
);
927928

929+
it.effect.each([
930+
{ reconnectAfter: "4 seconds", carried: true },
931+
{ reconnectAfter: "6 seconds", carried: false },
932+
] as const)(
933+
"keeps the current tab when a timed-out host re-registers within the grace period (%o)",
934+
({ reconnectAfter, carried }) =>
935+
Effect.scoped(
936+
Effect.gen(function* () {
937+
const broker = yield* makeBroker;
938+
const openedTabId = PreviewTabId.make("tab-before-timeout");
939+
const firstRequests = requestsFrom(yield* broker.connect(makeHost()));
940+
yield* Stream.runForEach(firstRequests, (request) =>
941+
// The host stalls on the snapshot, as when a capture never gets a frame.
942+
request.operation === "snapshot"
943+
? Effect.void
944+
: broker.respond({
945+
clientId: "client-1",
946+
connectionId: request.connectionId,
947+
requestId: request.requestId,
948+
ok: true,
949+
result: { tabId: openedTabId },
950+
}),
951+
).pipe(Effect.forkScoped);
952+
yield* Effect.yieldNow;
953+
954+
yield* broker.invoke({ scope, operation: "open", input: {} });
955+
const stalled = yield* broker
956+
.invoke<void>({ scope, operation: "snapshot", input: {} })
957+
.pipe(Effect.flip, Effect.forkScoped);
958+
yield* Effect.yieldNow;
959+
yield* TestClock.adjust("16 seconds");
960+
expect((yield* Fiber.join(stalled))._tag).toBe("PreviewAutomationTimeoutError");
961+
962+
yield* TestClock.adjust(reconnectAfter);
963+
const routedRequests: RoutedRequest[] = [];
964+
const replacementRequests = requestsFrom(yield* broker.connect(makeHost()));
965+
yield* Stream.runForEach(replacementRequests, (request) => {
966+
routedRequests.push(request);
967+
return broker.respond({
968+
clientId: "client-1",
969+
connectionId: request.connectionId,
970+
requestId: request.requestId,
971+
ok: true,
972+
result: {},
973+
});
974+
}).pipe(Effect.forkScoped);
975+
yield* Effect.yieldNow;
976+
977+
yield* broker.invoke({ scope, operation: "status", input: {} });
978+
expect(routedRequests.at(-1)?.tabId).toBe(carried ? openedTabId : undefined);
979+
expect(routedRequests.at(-1)?.tabIdExplicit).toBe(false);
980+
}),
981+
),
982+
);
983+
928984
it.effect("fails requests assigned to the stream that is replaced", () =>
929985
Effect.scoped(
930986
Effect.gen(function* () {

‎apps/server/src/mcp/PreviewAutomationBroker.ts‎

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
} from "@t3tools/contracts";
3030
import * as Context from "effect/Context";
3131
import type * as Cause from "effect/Cause";
32+
import * as Clock from "effect/Clock";
3233
import * as Crypto from "effect/Crypto";
3334
import * as Deferred from "effect/Deferred";
3435
import * as Effect from "effect/Effect";
@@ -42,6 +43,11 @@ import * as SynchronizedRef from "effect/SynchronizedRef";
4243
import * as McpInvocationContext from "./McpInvocationContext.ts";
4344

4445
const HOST_RESPONSE_GRACE_MS = 1_000;
46+
/**
47+
* A host evicted for a slow response is still running and re-registers within
48+
* about a second. Its tabs survive, so its agents keep their current tab.
49+
*/
50+
const EVICTED_LEASE_GRACE_MS = 5_000;
4551

4652
export interface PreviewAutomationInvokeInput {
4753
readonly scope: McpInvocationContext.McpThreadInvocationScope;
@@ -93,7 +99,10 @@ interface PendingRequest {
9399
readonly context: PreviewAutomationRequestErrorContext;
94100
}
95101

96-
/** A lease belongs to this exact connectionId/queue; replacing the host invalidates it. */
102+
/**
103+
* A lease lives as long as the connection it names; a host evicted for a timeout
104+
* hands its leases to its own re-registration (see `EVICTED_LEASE_GRACE_MS`).
105+
*/
97106
interface HostAssignment {
98107
readonly clientId: ClientConnection["clientId"];
99108
readonly connectionId: ClientConnection["connectionId"];
@@ -102,6 +111,13 @@ interface HostAssignment {
102111
readonly tabSequence?: number;
103112
}
104113

114+
interface ReleasedLease {
115+
readonly clientId: ClientConnection["clientId"];
116+
readonly tabId?: PreviewTabId;
117+
readonly tabSequence?: number;
118+
readonly releasedAt: number;
119+
}
120+
105121
interface PreviewAutomationRequestErrorContext {
106122
readonly operation: PreviewAutomationOperation;
107123
readonly environmentId: McpInvocationContext.McpInvocationScope["environmentId"];
@@ -120,6 +136,8 @@ interface PreviewAutomationRequestErrorContext {
120136
interface BrokerState {
121137
readonly clients: ReadonlyMap<string, ClientConnection>;
122138
readonly assignments: ReadonlyMap<string, HostAssignment>;
139+
/** Leases of hosts evicted for a timeout, kept until they re-register or the grace ends. */
140+
readonly released: ReadonlyMap<string, ReleasedLease>;
123141
readonly pending: ReadonlyMap<string, PendingRequest>;
124142
readonly requestSequence: number;
125143
readonly focusSequence: number;
@@ -129,22 +147,32 @@ const removeConnectionFromState = (
129147
current: BrokerState,
130148
clientId: string,
131149
queue: ClientConnection["queue"],
150+
releasedAt?: number,
132151
): { readonly state: BrokerState; readonly disconnected: ReadonlyArray<PendingRequest> } => {
133152
const clients = new Map(current.clients);
134153
const assignments = new Map(current.assignments);
154+
const released = new Map(current.released);
135155
const pending = new Map(current.pending);
136156
const disconnected: PendingRequest[] = [];
137157
if (current.clients.get(clientId)?.queue === queue) clients.delete(clientId);
138158
for (const [assignmentKey, assignment] of assignments) {
139-
if (assignment.queue === queue) assignments.delete(assignmentKey);
159+
if (assignment.queue !== queue) continue;
160+
assignments.delete(assignmentKey);
161+
if (releasedAt === undefined) continue;
162+
released.set(assignmentKey, {
163+
clientId: assignment.clientId,
164+
...(assignment.tabId === undefined ? {} : { tabId: assignment.tabId }),
165+
...(assignment.tabSequence === undefined ? {} : { tabSequence: assignment.tabSequence }),
166+
releasedAt,
167+
});
140168
}
141169
for (const [requestId, entry] of pending) {
142170
if (entry.queue !== queue) continue;
143171
pending.delete(requestId);
144172
disconnected.push(entry);
145173
}
146174
return {
147-
state: { ...current, clients, assignments, pending },
175+
state: { ...current, clients, assignments, released, pending },
148176
disconnected,
149177
};
150178
};
@@ -351,6 +379,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
351379
const state = yield* SynchronizedRef.make<BrokerState>({
352380
clients: new Map(),
353381
assignments: new Map(),
382+
released: new Map(),
354383
pending: new Map(),
355384
requestSequence: 0,
356385
focusSequence: 0,
@@ -382,11 +411,14 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
382411
queue: ClientConnection["queue"],
383412
completeStream = false,
384413
) {
414+
// Only a timeout keeps the host's leases: a closed or replaced stream may
415+
// belong to a runtime whose tabs are gone.
416+
const releasedAt = completeStream ? yield* Clock.currentTimeMillis : undefined;
385417
yield* SynchronizedRef.modifyEffect(state, (current) => {
386418
if (current.clients.get(clientId)?.queue !== queue) {
387419
return Effect.succeed([undefined, current] as const);
388420
}
389-
const removed = removeConnectionFromState(current, clientId, queue);
421+
const removed = removeConnectionFromState(current, clientId, queue, releasedAt);
390422
return closeConnection(queue, removed.disconnected, completeStream).pipe(
391423
Effect.as([undefined, removed.state] as const),
392424
);
@@ -412,12 +444,28 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
412444
focusOrder: 0,
413445
queue,
414446
};
447+
const now = yield* Clock.currentTimeMillis;
415448
const registration = yield* SynchronizedRef.modify(state, (current) => {
416449
const previousConnection = current.clients.get(clientId);
417450
const removed = previousConnection
418451
? removeConnectionFromState(current, clientId, previousConnection.queue)
419452
: { state: current, disconnected: [] };
420453
const clients = new Map(removed.state.clients);
454+
const assignments = new Map(removed.state.assignments);
455+
const released = new Map(removed.state.released);
456+
for (const [assignmentKey, lease] of released) {
457+
const expired = now - lease.releasedAt > EVICTED_LEASE_GRACE_MS;
458+
if (!expired && lease.clientId !== clientId) continue;
459+
released.delete(assignmentKey);
460+
if (expired || assignments.has(assignmentKey)) continue;
461+
assignments.set(assignmentKey, {
462+
clientId,
463+
connectionId,
464+
queue,
465+
...(lease.tabId === undefined ? {} : { tabId: lease.tabId }),
466+
...(lease.tabSequence === undefined ? {} : { tabSequence: lease.tabSequence }),
467+
});
468+
}
421469
const focusSequence = removed.state.focusSequence + 1;
422470
const registeredConnection = { ...connection, focusOrder: focusSequence };
423471
clients.set(clientId, registeredConnection);
@@ -427,7 +475,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
427475
disconnected: removed.disconnected,
428476
registeredConnection,
429477
},
430-
{ ...removed.state, clients, focusSequence },
478+
{ ...removed.state, clients, assignments, released, focusSequence },
431479
] as const;
432480
});
433481
if (registration.previousConnection) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { CaptureLock } from "./CaptureLock.ts";
4+
5+
// oxlint-disable-next-line t3code/no-real-time-wait-in-tests -- a zero-delay macrotask boundary, not a wait for time
6+
const settle = () => new Promise<void>((resolve) => setImmediate(resolve));
7+
8+
describe("CaptureLock", () => {
9+
it("keeps the capture ahead exclusive when a waiting capture gives up", async () => {
10+
const lock = new CaptureLock();
11+
const ahead = Promise.withResolvers<void>();
12+
const events: string[] = [];
13+
const first = lock.run(async () => {
14+
events.push("first started");
15+
await ahead.promise;
16+
events.push("first finished");
17+
});
18+
const giveUp = new AbortController();
19+
const expired = lock.run(async () => {
20+
events.push("expired started");
21+
}, giveUp.signal);
22+
const reason = new Error("expired");
23+
giveUp.abort(reason);
24+
await expect(expired).rejects.toBe(reason);
25+
26+
const next = lock.run(async () => {
27+
events.push("next started");
28+
});
29+
await settle();
30+
expect(events).toEqual(["first started"]);
31+
32+
ahead.resolve();
33+
await Promise.all([first, next]);
34+
expect(events).toEqual(["first started", "first finished", "next started"]);
35+
});
36+
37+
it("frees the lock when a running capture that never settles gives up", async () => {
38+
const lock = new CaptureLock();
39+
const giveUp = new AbortController();
40+
const stalled = lock.run(() => new Promise<never>(() => {}), giveUp.signal);
41+
await settle();
42+
const reason = new Error("expired");
43+
giveUp.abort(reason);
44+
45+
await expect(stalled).rejects.toBe(reason);
46+
await expect(lock.run(async () => "next")).resolves.toBe("next");
47+
});
48+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { constVoid } from "effect/Function";
2+
3+
const abandonOnAbort = <A>(promise: Promise<A>, signal: AbortSignal): Promise<A> =>
4+
new Promise<A>((resolve, reject) => {
5+
const abort = () => reject(signal.reason);
6+
if (signal.aborted) abort();
7+
signal.addEventListener("abort", abort, { once: true });
8+
promise.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
9+
});
10+
11+
/**
12+
* Runs a tab's captures one at a time. A capture given a signal gives up when
13+
* it aborts, waiting included, so one that never gets a frame cannot stall
14+
* every capture behind it. One that gives up while still waiting keeps its
15+
* place, so the capture ahead of it never overlaps the next.
16+
*/
17+
export class CaptureLock {
18+
private tail: Promise<void> = Promise.resolve();
19+
20+
run<A>(operation: () => Promise<A>, signal?: AbortSignal): Promise<A> {
21+
const previous = this.tail;
22+
const run = previous.then(() => {
23+
signal?.throwIfAborted();
24+
return operation();
25+
});
26+
const result = signal ? abandonOnAbort(run, signal) : run;
27+
this.tail = Promise.all([previous, result.then(constVoid, constVoid)]).then(constVoid);
28+
return result;
29+
}
30+
}

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
type PreviewAutomationSnapshot,
1616
type PreviewAutomationStatus,
1717
} from "@t3tools/contracts";
18+
import * as Deferred from "effect/Deferred";
1819
import * as Effect from "effect/Effect";
1920
import * as Fiber from "effect/Fiber";
2021
import * as FileSystem from "effect/FileSystem";
@@ -93,6 +94,11 @@ function makeSession(frame?: ReturnType<typeof makeFrame>) {
9394
if (method === "Page.getNavigationHistory") return { currentIndex: 0, entries: [{}] };
9495
if (method === "Page.getLayoutMetrics") return { cssVisualViewport: { pageX: 0, pageY: 0 } };
9596
if (method === "Page.captureScreenshot") {
97+
// A guest that is hidden mid-transition never paints the frame a still waits for.
98+
if ((input as { format: string }).format === "jpeg" && stallStills) {
99+
onStalledStill?.();
100+
return new Promise<never>(() => {});
101+
}
96102
// A snapshot sizes its screenshot from the PNG header; viewers get JPEG frames as-is.
97103
return { data: (input as { format: string }).format === "png" ? PNG_HEADER : "ZnJhbWU=" };
98104
}
@@ -268,6 +274,8 @@ let focusedFrame: ReturnType<typeof makeFrame> | null = null;
268274
let contextFailure: Error | null = null;
269275
let initialNavigation: (() => Promise<void>) | null = null;
270276
let desktopRendersNext = false;
277+
let stallStills = false;
278+
let onStalledStill: (() => void) | undefined;
271279
const desktopDetaches = new NodeEvents.EventEmitter();
272280
const desktopTabs = new Set<string>();
273281
const desktopRenders = (tabId: string) => {
@@ -380,6 +388,8 @@ beforeEach(() => {
380388
focusedFrame = null;
381389
desktopTabs.clear();
382390
desktopRendersNext = false;
391+
stallStills = false;
392+
onStalledStill = undefined;
383393
releasedDesktopTabs.length = 0;
384394
desktopConnections.length = 0;
385395
});
@@ -627,6 +637,44 @@ it.live("enforces provider ownership and explicit targets when a session has mul
627637
).pipe(Effect.provide(layer)),
628638
);
629639

640+
// The viewer still's deadline is a signal the test aborts; later deadlines never fire.
641+
const viewerStill = new AbortController();
642+
const neverAborts = new AbortController().signal;
643+
644+
it.effect("a viewer still that never paints does not hold up the agent's snapshot", () =>
645+
Effect.scoped(
646+
Effect.gen(function* () {
647+
const { browser, broker, tabId } = yield* ready;
648+
const timeout = vi
649+
.spyOn(AbortSignal, "timeout")
650+
.mockImplementationOnce(() => viewerStill.signal)
651+
.mockImplementation(() => neverAborts);
652+
yield* Effect.addFinalizer(() => Effect.sync(() => timeout.mockRestore()));
653+
const stillStarted = yield* Deferred.make<void>();
654+
onStalledStill = () => Deferred.doneUnsafe(stillStarted, Effect.void);
655+
stallStills = true;
656+
const attaching = yield* browser
657+
.attachViewer(viewerInput(tabId, false))
658+
.pipe(Effect.forkScoped);
659+
yield* Deferred.await(stillStarted);
660+
661+
const snapshotting = yield* broker
662+
.invoke<PreviewAutomationSnapshot>({
663+
scope,
664+
operation: "snapshot",
665+
input: {},
666+
tabId,
667+
})
668+
.pipe(Effect.forkScoped);
669+
viewerStill.abort(new DOMException("timed out", "TimeoutError"));
670+
const snapshot = yield* Fiber.join(snapshotting);
671+
672+
expect(snapshot.accessibilityTree).toContain('button "delete"');
673+
yield* Fiber.join(attaching);
674+
}),
675+
).pipe(Effect.provide(layer)),
676+
);
677+
630678
it.live("streams to a read-only viewer without allowing takeover, input, or viewport changes", () =>
631679
Effect.scoped(
632680
Effect.gen(function* () {

0 commit comments

Comments
 (0)