@devries-cameron20 / t3code Mirror · read-only / commits / 8f947a313b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 22:00 UTC

fix(server): queued preview actions no longer start after their deadline, except recording stop (#16941)

Co-authored-by: hogeheer499-commits <267467744+hogeheer499-commits@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Jesse van Dijk committed Oct 20, 2026 at 07:08 UTC 8f947a313b29c30c1deb9960ea0450482cb6e19f
4 files changed +367 -51
apps/server/src/preview/ServerBrowser.test.ts
+259
@@ -23,6 +23,7 @@ import * as PubSub from "effect/PubSub";
23 import * as Queue from "effect/Queue";
24 import * as Scope from "effect/Scope";
25 import * as Stream from "effect/Stream";
26 +import * as TestClock from "effect/testing/TestClock";
27 import type { BrowserContext, Page } from "playwright-core";
28 import { beforeEach, expect, vi } from "vite-plus/test";
29
@@ -32,6 +33,7 @@ import * as Broker from "../mcp/PreviewAutomationBroker.ts";
33 import * as DesktopChannel from "./DesktopBrowserChannel.ts";
34 import * as Manager from "./Manager.ts";
35 import * as ServerBrowser from "./ServerBrowser.ts";
36 +import * as ServerBrowserContexts from "./ServerBrowserContexts.ts";
37 import * as PreviewBrowser from "./PreviewBrowser.ts";
38
39 vi.mock("./ServerBrowserContexts.ts", () => ({
@@ -152,6 +154,9 @@ function makeContext(onClose?: (context: BrowserContext) => void) {
154 goto: vi.fn(async (next: string) => {
155 url = next;
156 events.emit("load");
157 + const pending = initialNavigation;
158 + initialNavigation = null;
159 + await pending?.();
160 }),
161 goBack: vi.fn(async () => {}),
162 goForward: vi.fn(async () => {}),
@@ -206,6 +211,7 @@ let pageCopy = "";
211 let keyCopy = "";
212 let focusedFrame: ReturnType<typeof makeFrame> | null = null;
213 let contextFailure: Error | null = null;
214 +let initialNavigation: (() => Promise<void>) | null = null;
215 let desktopRendersNext = false;
216 const desktopDetaches = new NodeEvents.EventEmitter();
217 const desktopTabs = new Set<string>();
@@ -316,6 +322,7 @@ beforeEach(() => {
322 contextRequests.length = 0;
323 contextGate = null;
324 contextFailure = null;
325 + initialNavigation = null;
326 pageCopy = "";
327 keyCopy = "";
328 focusedFrame = null;
@@ -325,6 +332,71 @@ beforeEach(() => {
332 desktopConnections.length = 0;
333 });
334
335 +it.live("an expired open still drains its initial navigation before human input", () =>
336 + Effect.scoped(
337 + Effect.gen(function* () {
338 + const browser = yield* ServerBrowser.ServerBrowser;
339 + const broker = yield* Broker.PreviewAutomationBroker;
340 + const manager = yield* Manager.PreviewManager;
341 + yield* Effect.yieldNow;
342 + const committed = Promise.withResolvers<void>();
343 + const events: string[] = [];
344 + const clock = vi.spyOn(performance, "now").mockReturnValue(0);
345 + yield* Effect.addFinalizer(() => Effect.sync(() => clock.mockRestore()));
346 + yield* Effect.addFinalizer(() => Effect.sync(() => committed.resolve()));
347 + const original = ServerBrowserContexts.ServerBrowserContexts.prototype.contextFor;
348 + const create = vi
349 + .spyOn(ServerBrowserContexts.ServerBrowserContexts.prototype, "contextFor")
350 + .mockImplementation(async function (
351 + this: ServerBrowserContexts.ServerBrowserContexts,
352 + profileId,
353 + isolationKey,
354 + ) {
355 + const context = await original.call(this, profileId, isolationKey);
356 + contexts.at(-1)!.page.goto.mockImplementationOnce(async () => {
357 + clock.mockReturnValue(1000);
358 + await committed.promise;
359 + events.push("navigation committed");
360 + });
361 + return context;
362 + });
363 + yield* Effect.addFinalizer(() => Effect.sync(() => create.mockRestore()));
364 + const expired = yield* broker
365 + .invoke<PreviewAutomationStatus>({
366 + scope,
367 + operation: "open",
368 + input: { url: "http://localhost:5173/slow", reuseExistingTab: false, show: false },
369 + timeoutMs: 1000,
370 + })
371 + .pipe(Effect.flip);
372 + expect(expired).toMatchObject({ _tag: "PreviewAutomationTimeoutError" });
373 + const { sessions } = yield* manager.list({ threadId: scope.thread.threadId });
374 + expect(sessions).toHaveLength(1);
375 + const tabId = PreviewTabId.make(sessions[0]!.tabId);
376 + const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
377 + yield* Queue.clear(viewer.output);
378 + const takeover = yield* viewer.input({ type: "takeControl" }).pipe(Effect.forkScoped);
379 + let control = yield* Queue.take(viewer.output);
380 + while (control._tag !== "control" || control.controller !== "you") {
381 + control = yield* Queue.take(viewer.output);
382 + }
383 + const cdp = contexts[0]!.sessions.at(-1)!;
384 + const send = cdp.send.getMockImplementation()!;
385 + cdp.send.mockImplementation(async (operation, input) => {
386 + if (operation === "Input.insertText") events.push("human typed");
387 + return send(operation, input);
388 + });
389 + const typing = yield* viewer.input({ type: "text", text: "hello" }).pipe(Effect.forkScoped);
390 + yield* broker.invoke({ scope, tabId, operation: "status", input: {} });
391 + expect(events).toEqual([]);
392 + committed.resolve();
393 + yield* Fiber.join(takeover);
394 + yield* Fiber.join(typing);
395 + expect(events).toEqual(["navigation committed", "human typed"]);
396 + }),
397 + ).pipe(Effect.provide(layer)),
398 +);
399 +
400 it.live.each(["desktop" as const, undefined])(
401 "explicit desktop targets ($0 runtime) fail without creating a server tab",
402 (runtime) =>
@@ -595,6 +667,193 @@ it.live.each([
667 ).pipe(Effect.provide(layer)),
668 );
669
670 +it.live("a user-opened tab allows a corrected URL before its initial navigation commits", () =>
671 + Effect.scoped(
672 + Effect.gen(function* () {
673 + const { browser } = yield* ready;
674 + const manager = yield* Manager.PreviewManager;
675 + yield* Effect.yieldNow;
676 + const started = Promise.withResolvers<void>();
677 + const committed = Promise.withResolvers<void>();
678 + yield* Effect.addFinalizer(() => Effect.sync(() => committed.resolve()));
679 + initialNavigation = () => {
680 + started.resolve();
681 + return committed.promise;
682 + };
683 + const opened = yield* manager.open({
684 + threadId: scope.thread.threadId,
685 + url: "http://localhost:5173/stalled",
686 + runtime: "server",
687 + });
688 + yield* Effect.promise(() => started.promise);
689 + const viewer = yield* browser.attachViewer(viewerInput(opened.tabId, true));
690 + const correction = yield* viewer
691 + .input({ type: "navigate", url: "http://localhost:5173/fixed" })
692 + .pipe(Effect.forkScoped);
693 + yield* Effect.promise(() => new Promise<void>((resolve) => setImmediate(resolve)));
694 + const lastNavigationBeforeCommit = contexts[1]!.page.goto.mock.calls.at(-1);
695 + committed.resolve();
696 + yield* Fiber.join(correction);
697 + expect(lastNavigationBeforeCommit).toEqual([
698 + "http://localhost:5173/fixed",
699 + expect.objectContaining({ waitUntil: "commit" }),
700 + ]);
701 + }),
702 + ).pipe(Effect.provide(layer)),
703 +);
704 +
705 +it.live("an expired queued click never reaches the page", () =>
706 + Effect.scoped(
707 + Effect.gen(function* () {
708 + const { broker, tabId } = yield* ready;
709 + const page = contexts[0]!.page;
710 + const started = Promise.withResolvers<void>();
711 + const release = Promise.withResolvers<Awaited<ReturnType<typeof page.evaluate>>>();
712 + const metadata = {
713 + url: "about:blank",
714 + title: "test page",
715 + loading: false,
716 + visibleText: "",
717 + interactiveElements: [],
718 + };
719 + yield* Effect.addFinalizer(() => Effect.sync(() => release.resolve(metadata)));
720 + page.evaluate.mockImplementationOnce(async () => {
721 + started.resolve();
722 + return release.promise;
723 + });
724 + const click = vi.fn(async () => {});
725 + page.locator.mockReturnValue({
726 + scrollIntoViewIfNeeded: async () => {},
727 + boundingBox: async () => ({ x: 0, y: 0, width: 10, height: 10 }),
728 + click,
729 + } as never);
730 + let now = performance.now();
731 + const clock = vi.spyOn(performance, "now").mockImplementation(() => now);
732 + yield* Effect.addFinalizer(() => Effect.sync(() => clock.mockRestore()));
733 + const snapshot = yield* broker
734 + .invoke<PreviewAutomationSnapshot>({
735 + scope,
736 + tabId,
737 + operation: "snapshot",
738 + input: {},
739 + timeoutMs: 5000,
740 + })
741 + .pipe(Effect.forkScoped);
742 + yield* Effect.promise(() => started.promise);
743 + const action = yield* broker
744 + .invoke<void>({
745 + scope,
746 + tabId,
747 + operation: "click",
748 + input: { selector: "button" },
749 + timeoutMs: 1000,
750 + })
751 + .pipe(Effect.exit, Effect.forkScoped);
752 + // The later status reply marks delivery through the same request stream.
753 + yield* broker.invoke({ scope, tabId, operation: "status", input: {} });
754 + now += 1000;
755 + release.resolve(metadata);
756 + yield* Fiber.join(snapshot);
757 + const result = yield* Fiber.join(action);
758 + expect(click).not.toHaveBeenCalled();
759 + const error = yield* Effect.flip(result);
760 + expect(error).toMatchObject({
761 + _tag: "PreviewAutomationTimeoutError",
762 + remoteTag: "PreviewAutomationTimeoutError",
763 + });
764 + }),
765 + ).pipe(Effect.provide(layer)),
766 +);
767 +
768 +it.effect(
769 + "a queued recording stop still runs after its deadline when a snapshot caller times out",
770 + () =>
771 + Effect.scoped(
772 + Effect.gen(function* () {
773 + const { broker, tabId } = yield* ready;
774 + const page = contexts[0]!.page;
775 + const encoder = {
776 + evaluate: vi
777 + .fn()
778 + .mockResolvedValueOnce(undefined)
779 + .mockResolvedValueOnce(true)
780 + .mockResolvedValueOnce({ mimeType: "video/webm", count: 1, bytes: 5 })
781 + .mockResolvedValueOnce("ZnJhbWU="),
782 + close: vi.fn(async () => {}),
783 + };
784 + const scratchPage = vi
785 + .spyOn(ServerBrowserContexts.ServerBrowserContexts.prototype, "scratchPage")
786 + .mockResolvedValue(encoder as unknown as Page);
787 + yield* Effect.addFinalizer(() => Effect.sync(() => scratchPage.mockRestore()));
788 + const recording = yield* broker.invoke({
789 + scope,
790 + tabId,
791 + operation: "recordingStart",
792 + input: {},
793 + });
794 + expect(recording).toMatchObject({ recording: true });
795 + const session = contexts[0]!.sessions.at(-1)!;
796 + expect(session.send).toHaveBeenCalledWith("Page.startScreencast", expect.anything());
797 +
798 + const started = Promise.withResolvers<void>();
799 + const release = Promise.withResolvers<Awaited<ReturnType<typeof page.evaluate>>>();
800 + const metadata = {
801 + url: "about:blank",
802 + title: "test page",
803 + loading: false,
804 + visibleText: "",
805 + interactiveElements: [],
806 + };
807 + yield* Effect.addFinalizer(() => Effect.sync(() => release.resolve(metadata)));
808 + page.evaluate.mockImplementationOnce(async () => {
809 + started.resolve();
810 + return release.promise;
811 + });
812 + let now = performance.now();
813 + const clock = vi.spyOn(performance, "now").mockImplementation(() => now);
814 + yield* Effect.addFinalizer(() => Effect.sync(() => clock.mockRestore()));
815 + const snapshot = yield* broker
816 + .invoke<PreviewAutomationSnapshot>({
817 + scope,
818 + tabId,
819 + operation: "snapshot",
820 + input: {},
821 + timeoutMs: 150,
822 + updateCurrentTab: false,
823 + })
824 + .pipe(Effect.flip, Effect.forkScoped);
825 + yield* Effect.promise(() => started.promise);
826 + const stop = yield* broker
827 + .invoke<void>({
828 + scope,
829 + tabId,
830 + operation: "recordingStop",
831 + input: {},
832 + timeoutMs: 1000,
833 + })
834 + .pipe(Effect.exit, Effect.forkScoped);
835 + // The status reply marks delivery while the snapshot still holds the tab queue.
836 + yield* broker.invoke({ scope, tabId, operation: "status", input: {} });
837 + yield* TestClock.adjust(150);
838 + expect(yield* Fiber.join(snapshot)).toMatchObject({
839 + _tag: "PreviewAutomationTimeoutError",
840 + });
841 + now += 1000;
842 + expect(session.detach).not.toHaveBeenCalled();
843 + release.resolve(metadata);
844 + yield* yield* Fiber.join(stop);
845 + expect(session.detach).toHaveBeenCalledOnce();
846 + const stopped = yield* broker
847 + .invoke<void>({ scope, tabId, operation: "recordingStop", input: {} })
848 + .pipe(Effect.flip);
849 + expect(stopped).toMatchObject({
850 + _tag: "PreviewAutomationExecutionError",
851 + cause: { _tag: "PreviewAutomationRecordingNotActiveError" },
852 + });
853 + }),
854 + ).pipe(Effect.provide(layer)),
855 +);
856 +
857 it.live("enforces provider ownership and explicit targets when a session has multiple tabs", () =>
858 Effect.scoped(
859 Effect.gen(function* () {
apps/server/src/preview/ServerBrowser.ts
+64 -47
@@ -899,7 +899,8 @@ const make = Effect.gen(function* () {
899 : page.goto(snapshot.navStatus.url, options)
900 ).then(constVoid);
901
902 - void tab.initialNavigation.catch(constVoid);
902 + if (tab.control.agentId !== null) tab.control.track(tab.initialNavigation);
903 + else void tab.initialNavigation.catch(constVoid);
904 }
905 return tab;
906 };
@@ -1704,7 +1705,10 @@ const make = Effect.gen(function* () {
1705 }
1706 };
1707
1707 - const runOperation = async (request: PreviewAutomationRequest): Promise<unknown> => {
1708 + const runOperation = async (
1709 + request: PreviewAutomationRequest,
1710 + deadlineMs: number,
1711 + ): Promise<unknown> => {
1712 const input = request.input;
1713 switch (request.operation) {
1714 case "status":
@@ -1790,34 +1794,38 @@ const make = Effect.gen(function* () {
1794 "A browser dialog is pending. Read preview_status and use preview_dialog first.",
1795 "dialogPending",
1796 );
1793 - return tab.control.agent(request.agentSessionId, async () => {
1794 - if (tab.dialog)
1795 - throw new BrowserControlInterrupted(
1796 - "A browser dialog is pending. Read preview_status and use preview_dialog first.",
1797 - "dialogPending",
1798 - );
1799 - if (existing) {
1800 - if (url) await navigate(tab, url, "load", navigationTimeout);
1801 - } else {
1802 - await tab.initialNavigation;
1803 - }
1804 - const reveal = open.open ?? open.show;
1805 - if (reveal !== false) {
1806 - await Effect.runPromise(
1807 - manager.requestReveal({
1808 - threadId: tab.threadId,
1809 - tabId: tab.tabId,
1810 - force: reveal === true,
1811 - }),
1812 - );
1813 - }
1814 - if (!existing && url) {
1815 - await tab.page
1816 - .waitForLoadState("load", { timeout: navigationTimeout })
1817 - .catch(constVoid);
1818 - }
1819 - return statusWithTitle(tab, request.agentSessionId);
1820 - });
1797 + return tab.control.agent(
1798 + request.agentSessionId,
1799 + async () => {
1800 + if (tab.dialog)
1801 + throw new BrowserControlInterrupted(
1802 + "A browser dialog is pending. Read preview_status and use preview_dialog first.",
1803 + "dialogPending",
1804 + );
1805 + if (existing) {
1806 + if (url) await navigate(tab, url, "load", navigationTimeout);
1807 + } else {
1808 + await tab.initialNavigation;
1809 + }
1810 + const reveal = open.open ?? open.show;
1811 + if (reveal !== false) {
1812 + await Effect.runPromise(
1813 + manager.requestReveal({
1814 + threadId: tab.threadId,
1815 + tabId: tab.tabId,
1816 + force: reveal === true,
1817 + }),
1818 + );
1819 + }
1820 + if (!existing && url) {
1821 + await tab.page
1822 + .waitForLoadState("load", { timeout: navigationTimeout })
1823 + .catch(constVoid);
1824 + }
1825 + return statusWithTitle(tab, request.agentSessionId);
1826 + },
1827 + deadlineMs,
1828 + );
1829 }
1830 case "recordingStop": {
1831 if (
@@ -1849,6 +1857,8 @@ const make = Effect.gen(function* () {
1857 "No recording is active for this thread.",
1858 );
1859 }
1860 + // A stop cannot act twice (a retry finds no recording);
1861 + // dropping a late one would leave capture running.
1862 return tab.control.agent(request.agentSessionId ?? "", () => stopRecording(tab));
1863 }
1864 }
@@ -1891,23 +1901,27 @@ const make = Effect.gen(function* () {
1901 return executeTabOperation(tab, request);
1902 });
1903 }
1894 - return tab.control.agent(agentSessionId, async () => {
1895 - if (tab.dialog)
1896 - throw new BrowserControlInterrupted(
1897 - "A browser dialog is pending. Read preview_status and use preview_dialog first.",
1898 - "dialogPending",
1899 - );
1900 - const generation = tab.control.generation;
1901 - tab.agentOperating = !READ_OPERATIONS.has(request.operation);
1902 - updateFileChooserInterception(tab);
1903 - try {
1904 - return await executeTabOperation(tab, request);
1905 - } finally {
1906 - tab.agentOperating = false;
1904 + return tab.control.agent(
1905 + agentSessionId,
1906 + async () => {
1907 + if (tab.dialog)
1908 + throw new BrowserControlInterrupted(
1909 + "A browser dialog is pending. Read preview_status and use preview_dialog first.",
1910 + "dialogPending",
1911 + );
1912 + const generation = tab.control.generation;
1913 + tab.agentOperating = !READ_OPERATIONS.has(request.operation);
1914 updateFileChooserInterception(tab);
1908 - if (generation !== tab.control.generation) ServerBrowserPage.invalidateRefs(tab.page);
1909 - }
1910 - });
1915 + try {
1916 + return await executeTabOperation(tab, request);
1917 + } finally {
1918 + tab.agentOperating = false;
1919 + updateFileChooserInterception(tab);
1920 + if (generation !== tab.control.generation) ServerBrowserPage.invalidateRefs(tab.page);
1921 + }
1922 + },
1923 + deadlineMs,
1924 + );
1925 };
1926
1927 const executeTabOperation = async (tab: ServerTab, request: PreviewAutomationRequest) => {
@@ -2002,7 +2016,10 @@ const make = Effect.gen(function* () {
2016
2017 const handleRequest = (connectionId: string, request: PreviewAutomationRequest) =>
2018 Effect.tryPromise({
2005 - try: () => runOperation(request).finally(() => markUsed(request)),
2019 + try: () =>
2020 + runOperation(request, performance.now() + request.timeoutMs).finally(() =>
2021 + markUsed(request),
2022 + ),
2023 catch: ServerBrowserPage.toOperationError,
2024 }).pipe(
2025 Effect.match({
apps/server/src/preview/SessionControl.test.ts
+35 -1
@@ -1,4 +1,4 @@
1 -import { describe, expect, it } from "vite-plus/test";
1 +import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2
3 import { BrowserControlInterrupted, SessionControl } from "./SessionControl.ts";
4
@@ -179,3 +179,37 @@ describe("SessionControl", () => {
179 await expect(control.agent("agent-b", async () => "acted")).resolves.toBe("acted");
180 });
181 });
182 +
183 +describe("request deadlines at dispatch", () => {
184 + afterEach(() => vi.restoreAllMocks());
185 +
186 + it("finishes running work, skips expired queued actions, and accepts fresh work", async () => {
187 + let now = 0;
188 + vi.spyOn(performance, "now").mockImplementation(() => now);
189 + const control = new SessionControl("agent");
190 + const started = Promise.withResolvers<void>();
191 + const finish = Promise.withResolvers<void>();
192 + const events: string[] = [];
193 + const running = control.agent(
194 + "agent",
195 + async () => {
196 + events.push("started");
197 + started.resolve();
198 + await finish.promise;
199 + events.push("finished");
200 + },
201 + 100,
202 + );
203 + await started.promise;
204 + const expired = expect(
205 + control.agent("agent", async () => events.push("expired"), 100),
206 + ).rejects.toMatchObject({ name: "TimeoutError" });
207 + const next = control.agent("agent", async () => events.push("next"), 200);
208 + // No timer fires: expiry must be checked when the queue resumes.
209 + now = 100;
210 + expect(events).toEqual(["started"]);
211 + finish.resolve();
212 + await Promise.all([running, expired, next]);
213 + expect(events).toEqual(["started", "finished", "next"]);
214 + });
215 +});
apps/server/src/preview/SessionControl.ts
+9 -3
@@ -73,13 +73,19 @@ export class SessionControl {
73 this.onGenerationChange();
74 }
75
76 - private async action<A>(allowed: () => boolean, run: () => Promise<A>) {
76 + private async action<A>(allowed: () => boolean, run: () => Promise<A>, deadlineMs?: number) {
77 this.assertOpen();
78 if (!allowed()) throw new BrowserControlInterrupted("You do not control this browser tab.");
79 const epoch = this.epoch;
80 return this.enqueue(async () => {
81 this.assertOpen();
82 if (epoch !== this.epoch || !allowed()) throw new BrowserControlInterrupted();
83 + if (deadlineMs !== undefined && performance.now() >= deadlineMs) {
84 + throw new DOMException(
85 + "This browser request expired before it could start.",
86 + "TimeoutError",
87 + );
88 + }
89 return run();
90 });
91 }
@@ -88,7 +94,7 @@ export class SessionControl {
94 return this.agentId === null || this.agentId === agentId;
95 }
96
91 - agent<A>(agentId: string, run: () => Promise<A>) {
97 + agent<A>(agentId: string, run: () => Promise<A>, deadlineMs?: number) {
98 if (!this.agentMayAct(agentId))
99 return Promise.reject(
100 new BrowserControlInterrupted("This tab belongs to another agent.", "agentMismatch"),
@@ -97,7 +103,7 @@ export class SessionControl {
103 return Promise.reject(
104 new BrowserControlInterrupted("A human controls this tab.", "humanControl"),
105 );
100 - return this.action(() => this.agentMayAct(agentId) && this.owner === null, run);
106 + return this.action(() => this.agentMayAct(agentId) && this.owner === null, run, deadlineMs);
107 }
108
109 agentCanActNow(agentId: string) {