Skip to content

Commit a618bcf

Browse files
committed
fix(preview): contain stalled requests within their browser deadline
1 parent 3b95e78 commit a618bcf

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

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

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as Path from "effect/Path";
2222
import * as PubSub from "effect/PubSub";
2323
import * as Queue from "effect/Queue";
2424
import * as Stream from "effect/Stream";
25+
import * as TestClock from "effect/testing/TestClock";
2526
import type { BrowserContext, Page } from "playwright-core";
2627
import { beforeEach, expect, vi } from "vite-plus/test";
2728

@@ -333,6 +334,136 @@ beforeEach(() => {
333334
desktopConnections.length = 0;
334335
});
335336

337+
it.effect("a stalled tab request times out without disconnecting another agent", () =>
338+
Effect.scoped(
339+
Effect.gen(function* () {
340+
const { broker, tabId } = yield* ready;
341+
const otherScope = asSession("agent-b");
342+
const other = yield* broker.invoke<PreviewAutomationStatus>({
343+
scope: otherScope,
344+
operation: "open",
345+
input: { reuseExistingTab: false, open: false, url: "https://example.com/peer" },
346+
});
347+
const started = Promise.withResolvers<void>();
348+
const finish = Promise.withResolvers<void>();
349+
const page = contexts[0]!.page;
350+
page.goto.mockImplementationOnce(() => {
351+
started.resolve();
352+
return finish.promise;
353+
});
354+
const pending = yield* broker
355+
.invoke<PreviewAutomationStatus>({
356+
scope,
357+
tabId,
358+
operation: "navigate",
359+
input: { url: "https://example.com/stalled" },
360+
timeoutMs: 2_000,
361+
})
362+
.pipe(Effect.flip, Effect.forkScoped);
363+
yield* Effect.promise(() => started.promise);
364+
const reading = Promise.withResolvers<void>();
365+
const title = Promise.withResolvers<string>();
366+
contexts[1]!.page.title.mockImplementationOnce(() => {
367+
reading.resolve();
368+
return title.promise;
369+
});
370+
const peer = yield* broker
371+
.invoke<PreviewAutomationStatus>({
372+
scope: otherScope,
373+
tabId: PreviewTabId.make(other.tabId!),
374+
operation: "status",
375+
input: {},
376+
timeoutMs: 10_000,
377+
})
378+
.pipe(Effect.forkScoped);
379+
yield* Effect.promise(() => reading.promise);
380+
try {
381+
yield* TestClock.adjust(3_001);
382+
expect(yield* Fiber.join(pending)).toMatchObject({
383+
_tag: "PreviewAutomationTimeoutError",
384+
});
385+
title.resolve("other agent's page");
386+
expect(yield* Fiber.join(peer)).toMatchObject({
387+
available: true,
388+
title: "other agent's page",
389+
tabId: other.tabId,
390+
});
391+
const status = yield* broker.invoke<PreviewAutomationStatus>({
392+
scope: otherScope,
393+
tabId: PreviewTabId.make(other.tabId!),
394+
operation: "status",
395+
input: {},
396+
});
397+
expect(status).toMatchObject({ available: true, tabId: other.tabId });
398+
} finally {
399+
finish.resolve();
400+
title.resolve("other agent's page");
401+
}
402+
expect(page.goto).toHaveBeenCalledTimes(1);
403+
}),
404+
).pipe(Effect.provide(layer)),
405+
);
406+
407+
it.effect("an expired queued navigation is not replayed after the blocked action finishes", () =>
408+
Effect.scoped(
409+
Effect.gen(function* () {
410+
const { broker, tabId } = yield* ready;
411+
let now = 0;
412+
const clock = vi.spyOn(performance, "now").mockImplementation(() => now);
413+
const started = Promise.withResolvers<void>();
414+
const finish = Promise.withResolvers<void>();
415+
const page = contexts[0]!.page;
416+
page.goto.mockImplementationOnce(() => {
417+
started.resolve();
418+
return finish.promise;
419+
});
420+
try {
421+
const first = yield* broker
422+
.invoke<PreviewAutomationStatus>({
423+
scope,
424+
tabId,
425+
operation: "navigate",
426+
input: { url: "https://example.com/first" },
427+
timeoutMs: 10_000,
428+
})
429+
.pipe(Effect.forkScoped);
430+
yield* Effect.promise(() => started.promise);
431+
const queued = yield* broker
432+
.invoke<PreviewAutomationStatus>({
433+
scope,
434+
tabId,
435+
operation: "navigate",
436+
input: { url: "https://example.com/expired" },
437+
timeoutMs: 1_000,
438+
})
439+
.pipe(Effect.flip, Effect.forkScoped);
440+
yield* Effect.yieldNow;
441+
yield* TestClock.adjust(2_001);
442+
expect(yield* Fiber.join(queued)).toMatchObject({
443+
_tag: "PreviewAutomationTimeoutError",
444+
});
445+
now += 2_000;
446+
finish.resolve();
447+
yield* Fiber.join(first);
448+
const corrected = yield* broker.invoke<PreviewAutomationStatus>({
449+
scope,
450+
tabId,
451+
operation: "navigate",
452+
input: { url: "https://example.com/corrected" },
453+
});
454+
expect(corrected.url).toBe("https://example.com/corrected");
455+
expect(page.goto.mock.calls.map(([url]) => url)).toEqual([
456+
"https://example.com/first",
457+
"https://example.com/corrected",
458+
]);
459+
} finally {
460+
finish.resolve();
461+
clock.mockRestore();
462+
}
463+
}),
464+
).pipe(Effect.provide(layer)),
465+
);
466+
336467
it.live.each(["desktop" as const, undefined])(
337468
"explicit desktop targets ($0 runtime) fail without creating a server tab",
338469
(runtime) =>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,17 @@ const make = Effect.gen(function* () {
20332033
),
20342034
catch: ServerBrowserPage.toOperationError,
20352035
}).pipe(
2036+
// A stalled tab must not make the broker evict the shared browser host.
2037+
Effect.timeoutOrElse({
2038+
duration: request.timeoutMs,
2039+
orElse: () =>
2040+
Effect.fail(
2041+
new ServerBrowserPage.ServerBrowserOperationError(
2042+
"PreviewAutomationTimeoutError",
2043+
"The browser request exceeded its deadline. It may still complete; check the tab before retrying an action.",
2044+
),
2045+
),
2046+
}),
20362047
Effect.match({
20372048
onSuccess: (result) => ({ ok: true as const, result }),
20382049
onFailure: (error) => ({

0 commit comments

Comments
 (0)