Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

perf(runtime): adopt provider-neutral upstream fixes (#16)

Leo53ea87f6909a
diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts
index 3bf6d6305..b3bf9e622 100644
--- a/apps/desktop/src/preview/Manager.test.ts
+++ b/apps/desktop/src/preview/Manager.test.ts
@@ -288,8 +288,18 @@ const settle = function* (until: () => boolean) {
const makeTestPictureInPictureWindow = (loadURL: () => Promise<void> = async () => undefined) => {
const listeners = new Map<string, () => void>();
+ const webContentsListeners = new Map<string, () => void>();
const send = vi.fn();
let destroyed = false;
+ const webContents = {
+ on: vi.fn((event: string, listener: () => void) => {
+ webContentsListeners.set(event, listener);
+ }),
+ off: vi.fn((event: string) => {
+ webContentsListeners.delete(event);
+ }),
+ send,
+ };
const pictureInPictureWindow = {
isDestroyed: vi.fn(() => destroyed),
once: vi.fn((event: string, listener: () => void) => {
@@ -309,11 +319,12 @@ const makeTestPictureInPictureWindow = (loadURL: () => Promise<void> = async ()
destroyed = true;
listeners.get("closed")?.();
}),
- webContents: {
- send,
+ get webContents() {
+ if (destroyed) throw new Error("Picture-in-picture window is closed.");
+ return webContents;
},
};
- return { pictureInPictureWindow, send };
+ return { pictureInPictureWindow, send, webContentsListeners };
};
describe("PreviewManager", () => {
@@ -2369,6 +2380,8 @@ describe("PreviewManager", () => {
pictureInPictureListeners.get("closed")?.();
}),
webContents: {
+ on: vi.fn(),
+ off: vi.fn(),
send: pictureInPictureSend,
},
};
@@ -2434,6 +2447,7 @@ describe("PreviewManager", () => {
);
expect(states.at(-1)?.pictureInPicture).toBe(true);
expect(capturePage).toHaveBeenCalledOnce();
+ const pictureInPictureFramesBeforeRecording = pictureInPictureSend.mock.calls.length;
yield* manager.startRecording("tab_pip");
expect(capturePage).toHaveBeenCalledOnce();
@@ -2441,6 +2455,7 @@ describe("PreviewManager", () => {
yield* TestClock.adjust(100);
expect(capturePage).toHaveBeenCalledTimes(2);
+ expect(pictureInPictureSend).toHaveBeenCalledTimes(pictureInPictureFramesBeforeRecording);
expect(recordingFrames).toHaveLength(1);
yield* manager.stopRecording("tab_pip");
@@ -2448,9 +2463,7 @@ describe("PreviewManager", () => {
const framesBeforePictureInPictureOnlyTick = pictureInPictureSend.mock.calls.length;
yield* TestClock.adjust(100);
expect(capturePage).toHaveBeenCalledTimes(3);
- expect(pictureInPictureSend.mock.calls.length).toBeGreaterThan(
- framesBeforePictureInPictureOnlyTick,
- );
+ expect(pictureInPictureSend.mock.calls.length).toBe(framesBeforePictureInPictureOnlyTick);
expect(recordingFrames).toHaveLength(1);
setBackgroundThrottling.mockImplementationOnce(() => {
@@ -2467,6 +2480,213 @@ describe("PreviewManager", () => {
),
);
+ effectIt.effect("starts picture-in-picture without an extra recording capture", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ const jpeg = Buffer.from("shared-preview-frame");
+ const capturePage = vi.fn(async () => ({
+ toJPEG: () => jpeg,
+ getSize: () => ({ width: 1280, height: 720 }),
+ }));
+ fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
+ const { pictureInPictureWindow, send } = makeTestPictureInPictureWindow();
+ browserWindowConstructor.mockImplementation(function () {
+ return pictureInPictureWindow;
+ });
+ const recordingFrames: DesktopPreviewRecordingFrame[] = [];
+
+ yield* manager.subscribeRecordingFrames((frame) =>
+ Effect.sync(() => {
+ recordingFrames.push(frame);
+ }),
+ );
+ yield* manager.createTab("tab_recording_then_pip");
+ yield* manager.registerWebview("tab_recording_then_pip", 42);
+ yield* manager.startRecording("tab_recording_then_pip");
+
+ expect(recordingFrames).toHaveLength(1);
+ yield* manager.openPictureInPicture("tab_recording_then_pip");
+ expect(capturePage).toHaveBeenCalledOnce();
+ expect(send).not.toHaveBeenCalled();
+
+ yield* TestClock.adjust(100);
+
+ expect(capturePage).toHaveBeenCalledTimes(2);
+ expect(recordingFrames).toHaveLength(2);
+ expect(send).toHaveBeenCalledOnce();
+ yield* manager.closePictureInPicture("tab_recording_then_pip");
+ yield* manager.stopRecording("tab_recording_then_pip");
+ }),
+ ),
+ );
+
+ effectIt.effect("stops frame capture when the native picture-in-picture window closes", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ const capturePage = vi.fn(async () => ({
+ toJPEG: () => Buffer.from("native-close-preview-frame"),
+ getSize: () => ({ width: 1280, height: 720 }),
+ }));
+ fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
+ const { pictureInPictureWindow } = makeTestPictureInPictureWindow();
+ browserWindowConstructor.mockImplementation(function () {
+ return pictureInPictureWindow;
+ });
+ const states: PreviewManager.PreviewTabState[] = [];
+ yield* manager.subscribeStateChanges((_tabId, state) =>
+ Effect.sync(() => {
+ states.push(state);
+ }),
+ );
+
+ yield* manager.createTab("tab_native_pip_close");
+ yield* manager.registerWebview("tab_native_pip_close", 42);
+ yield* manager.openPictureInPicture("tab_native_pip_close");
+
+ pictureInPictureWindow.close();
+ yield* settle(() => states.at(-1)?.pictureInPicture === false);
+
+ expect(states.at(-1)?.pictureInPicture).toBe(false);
+ const capturesAfterClose = capturePage.mock.calls.length;
+ yield* TestClock.adjust(200);
+ expect(capturePage).toHaveBeenCalledTimes(capturesAfterClose);
+ }),
+ ),
+ );
+
+ effectIt.effect("retries an unchanged picture-in-picture frame after delivery fails", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ const jpeg = Buffer.from("retry-preview-frame");
+ const capturePage = vi.fn(async () => ({
+ toJPEG: () => jpeg,
+ getSize: () => ({ width: 1280, height: 720 }),
+ }));
+ fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
+ const { pictureInPictureWindow, send } = makeTestPictureInPictureWindow();
+ send.mockImplementationOnce(() => {
+ throw new Error("picture-in-picture delivery failed");
+ });
+ browserWindowConstructor.mockImplementation(function () {
+ return pictureInPictureWindow;
+ });
+
+ yield* manager.createTab("tab_pip_delivery_retry");
+ yield* manager.registerWebview("tab_pip_delivery_retry", 42);
+ yield* manager.openPictureInPicture("tab_pip_delivery_retry");
+ expect(send).toHaveBeenCalledOnce();
+
+ yield* TestClock.adjust(100);
+
+ expect(capturePage).toHaveBeenCalledTimes(2);
+ expect(send).toHaveBeenCalledTimes(2);
+ yield* manager.closePictureInPicture("tab_pip_delivery_retry");
+ }),
+ ),
+ );
+
+ effectIt.effect("replays an unchanged picture-in-picture frame after its renderer reloads", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ const jpeg = Buffer.from("reloaded-preview-frame");
+ const capturePage = vi.fn(async () => ({
+ toJPEG: () => jpeg,
+ getSize: () => ({ width: 1280, height: 720 }),
+ }));
+ fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
+ const { pictureInPictureWindow, send, webContentsListeners } =
+ makeTestPictureInPictureWindow();
+ browserWindowConstructor.mockImplementation(function () {
+ return pictureInPictureWindow;
+ });
+
+ yield* manager.createTab("tab_pip_reload");
+ yield* manager.registerWebview("tab_pip_reload", 42);
+ yield* manager.openPictureInPicture("tab_pip_reload");
+ expect(send).toHaveBeenCalledOnce();
+
+ webContentsListeners.get("did-finish-load")?.();
+ yield* TestClock.adjust(100);
+
+ expect(send).toHaveBeenCalledTimes(2);
+ yield* manager.closePictureInPicture("tab_pip_reload");
+ }),
+ ),
+ );
+
+ effectIt.effect("keeps recording cadence when pixels remain unchanged", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ const unchanged = Buffer.from("unchanged-preview-frame");
+ const capturePage = vi.fn(async () => ({
+ toJPEG: () => unchanged,
+ getSize: () => ({ width: 1280, height: 720 }),
+ }));
+ fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
+ const frames: DesktopPreviewRecordingFrame[] = [];
+
+ yield* manager.subscribeRecordingFrames((frame) =>
+ Effect.sync(() => {
+ frames.push(frame);
+ }),
+ );
+ yield* manager.createTab("tab_unchanged_frame");
+ yield* manager.registerWebview("tab_unchanged_frame", 42);
+ yield* manager.startRecording("tab_unchanged_frame");
+
+ expect(frames.map((frame) => frame.data)).toEqual([unchanged.toString("base64")]);
+
+ yield* TestClock.adjust(100);
+
+ expect(capturePage).toHaveBeenCalledTimes(2);
+ expect(frames.map((frame) => frame.data)).toEqual([
+ unchanged.toString("base64"),
+ unchanged.toString("base64"),
+ ]);
+
+ yield* TestClock.adjust(100);
+
+ expect(capturePage).toHaveBeenCalledTimes(3);
+ expect(frames.map((frame) => frame.data)).toEqual([
+ unchanged.toString("base64"),
+ unchanged.toString("base64"),
+ unchanged.toString("base64"),
+ ]);
+ yield* manager.stopRecording("tab_unchanged_frame");
+ }),
+ ),
+ );
+
+ effectIt.effect("retries recording delivery when pixels remain unchanged", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ const jpeg = Buffer.from("retry-recording-frame");
+ const capturePage = vi.fn(async () => ({
+ toJPEG: () => jpeg,
+ getSize: () => ({ width: 1280, height: 720 }),
+ }));
+ fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
+ let deliveries = 0;
+
+ yield* manager.subscribeRecordingFrames(() =>
+ Effect.sync(() => {
+ deliveries += 1;
+ if (deliveries === 1) throw new Error("recording delivery failed");
+ }),
+ );
+ yield* manager.createTab("tab_recording_delivery_retry");
+ yield* manager.registerWebview("tab_recording_delivery_retry", 42);
+ yield* manager.startRecording("tab_recording_delivery_retry");
+ expect(deliveries).toBe(1);
+
+ yield* TestClock.adjust(100);
+
+ expect(deliveries).toBe(2);
+ yield* manager.stopRecording("tab_recording_delivery_retry");
+ }),
+ ),
+ );
+
effectIt.effect("retries a cold hidden-tab capture without dropping recording", () =>
withManager((manager) =>
Effect.gen(function* () {
diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts
index 0d90e0175..3a0234584 100644
--- a/apps/desktop/src/preview/Manager.ts
+++ b/apps/desktop/src/preview/Manager.ts
@@ -381,6 +381,7 @@ type FrameCaptureConsumer = "picture-in-picture" | "recording";
interface FrameCaptureSession {
readonly scope: Scope.Closeable;
readonly consumers: ReadonlySet<FrameCaptureConsumer>;
+ readonly lastPictureInPictureFrame: Buffer | null;
}
interface PictureInPictureSession {
@@ -616,7 +617,12 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
return [
undefined,
replaceMap(sessions, (copy) => {
- copy.set(tabId, { ...current, consumers });
+ copy.set(tabId, {
+ ...current,
+ consumers,
+ lastPictureInPictureFrame:
+ consumer === "picture-in-picture" ? null : current.lastPictureInPictureFrame,
+ });
}),
] as const;
}
@@ -2549,18 +2555,25 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
tabId,
webContentsId: wc.id,
},
- () => image.toJPEG(RECORDING_JPEG_QUALITY).toString("base64"),
+ () => image.toJPEG(RECORDING_JPEG_QUALITY),
);
+ const frameSession = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId);
+ if (frameSession?.scope !== captureSession.scope) return;
+ const recording = frameSession.consumers.has("recording");
+ const pictureInPicture =
+ frameSession.consumers.has("picture-in-picture") &&
+ frameSession.lastPictureInPictureFrame?.equals(encoded) !== true;
+ if (!recording && !pictureInPicture) return;
const receivedAt = yield* currentIso;
const frame: DesktopPreviewRecordingFrame = {
tabId,
- data: encoded,
+ data: encoded.toString("base64"),
width: size.width,
height: size.height,
receivedAt,
};
const deliveries: Array<Effect.Effect<void>> = [];
- if (currentCaptureSession.consumers.has("recording")) {
+ if (recording) {
const listeners = yield* Ref.get(recordingFrameListenersRef);
deliveries.push(
Effect.forEach(
@@ -2570,7 +2583,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
),
);
}
- if (currentCaptureSession.consumers.has("picture-in-picture")) {
+ if (pictureInPicture) {
const pictureInPictureWindow = (yield* SynchronizedRef.get(pictureInPictureSessionsRef)).get(
tabId,
)?.window;
@@ -2620,6 +2633,15 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
);
},
);
+ yield* SynchronizedRef.update(frameCaptureSessionsRef, (sessions) => {
+ if (sessions.get(tabId) !== frameSession) return sessions;
+ return replaceMap(sessions, (copy) => {
+ copy.set(tabId, {
+ ...frameSession,
+ lastPictureInPictureFrame: encoded,
+ });
+ });
+ });
}).pipe(
Effect.catch((error) =>
Effect.logWarning("Picture-in-picture frame delivery failed.", {
@@ -2687,6 +2709,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
copy.set(tabId, {
scope,
consumers: new Set([consumer]),
+ lastPictureInPictureFrame: null,
});
}),
] as const;
@@ -2841,6 +2864,18 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
),
);
};
+ const onDidFinishLoad = () => {
+ runFork(
+ SynchronizedRef.update(frameCaptureSessionsRef, (sessions) => {
+ const current = sessions.get(tabId);
+ if (!current?.consumers.has("picture-in-picture")) return sessions;
+ return replaceMap(sessions, (copy) => {
+ copy.set(tabId, { ...current, lastPictureInPictureFrame: null });
+ });
+ }),
+ );
+ };
+ const pipWebContents = pictureInPictureWindow.webContents;
yield* attempt(
{
operation: "pictureInPicture.configure",
@@ -2861,6 +2896,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
skipTransformProcessType: true,
});
}
+ pipWebContents.on("did-finish-load", onDidFinishLoad);
},
).pipe(
Effect.onError(() =>
@@ -2875,6 +2911,12 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
),
),
);
+ yield* Scope.addFinalizer(
+ initializationScope,
+ Effect.sync(() => {
+ pipWebContents.off("did-finish-load", onDidFinishLoad);
+ }).pipe(Effect.ignore),
+ );
yield* SynchronizedRef.update(pictureInPictureSessionsRef, (sessions) =>
replaceMap(sessions, (copy) => {
copy.set(tabId, session);
diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
index b15d2f679..d27caed4a 100644
--- a/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
@@ -1255,6 +1255,77 @@ it.layer(Layer.fresh(makeProjectionPipelinePrefixedTestLayer("t3-projection-atta
);
it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
+ it.effect("replays a bootstrap backlog larger than the event store default limit", () =>
+ Effect.gen(function* () {
+ const projectionPipeline = yield* OrchestrationProjectionPipeline;
+ const eventStore = yield* OrchestrationEventStore;
+ const sql = yield* SqlClient.SqlClient;
+ const now = "2026-01-01T00:00:00.000Z";
+ const projectId = ProjectId.make("project-bootstrap-backlog");
+
+ const sequenceRows = yield* sql<{ readonly maxSequence: number | null }>`
+ SELECT MAX(sequence) AS "maxSequence" FROM orchestration_events
+ `;
+ const sequenceBeforeBacklog = sequenceRows[0]?.maxSequence ?? 0;
+ const appendedEvents = yield* Effect.forEach(
+ Array.from({ length: 1_001 }, (_, index) => index),
+ (index) => {
+ const eventId = EventId.make(`evt-bootstrap-backlog-${index}`);
+ const commandId = CommandId.make(`cmd-bootstrap-backlog-${index}`);
+ return eventStore.append({
+ type: "project.created",
+ eventId,
+ aggregateKind: "project",
+ aggregateId: projectId,
+ occurredAt: now,
+ commandId,
+ causationEventId: null,
+ correlationId: CorrelationId.make(commandId),
+ metadata: {},
+ payload: {
+ projectId,
+ title: `Bootstrap backlog ${index}`,
+ workspaceRoot: "/tmp/project-bootstrap-backlog",
+ defaultModelSelection: null,
+ scripts: [],
+ createdAt: now,
+ updatedAt: now,
+ },
+ });
+ },
+ );
+ const lastSequence = appendedEvents[appendedEvents.length - 1]!.sequence;
+
+ yield* Effect.forEach(
+ Object.values(ORCHESTRATION_PROJECTOR_NAMES),
+ (projector) => {
+ const lastAppliedSequence =
+ projector === ORCHESTRATION_PROJECTOR_NAMES.projects
+ ? sequenceBeforeBacklog
+ : lastSequence;
+ return sql`
+ INSERT INTO projection_state (projector, last_applied_sequence, updated_at)
+ VALUES (${projector}, ${lastAppliedSequence}, ${now})
+ ON CONFLICT (projector)
+ DO UPDATE SET
+ last_applied_sequence = excluded.last_applied_sequence,
+ updated_at = excluded.updated_at
+ `;
+ },
+ { discard: true },
+ );
+
+ yield* projectionPipeline.bootstrap;
+
+ const stateRows = yield* sql<{ readonly lastAppliedSequence: number }>`
+ SELECT last_applied_sequence AS "lastAppliedSequence"
+ FROM projection_state
+ WHERE projector = ${ORCHESTRATION_PROJECTOR_NAMES.projects}
+ `;
+ assert.deepEqual(stateRows, [{ lastAppliedSequence: lastSequence }]);
+ }),
+ );
+
it.effect("resumes from projector last_applied_sequence without replaying older events", () =>
Effect.gen(function* () {
const projectionPipeline = yield* OrchestrationProjectionPipeline;
diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
index cc76e5a13..7c880ef41 100644
--- a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
@@ -1976,6 +1976,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
Stream.runForEach(
eventStore.readFromSequence(
Option.isSome(stateRow) ? stateRow.value.lastAppliedSequence : 0,
+ Number.MAX_SAFE_INTEGER,
),
(event) => runProjectorForEvent(projector, event),
),
diff --git a/apps/web/src/browser/ElectronBrowserHost.tsx b/apps/web/src/browser/ElectronBrowserHost.tsx
index fbf7c14b7..5425bca0b 100644
--- a/apps/web/src/browser/ElectronBrowserHost.tsx
+++ b/apps/web/src/browser/ElectronBrowserHost.tsx
@@ -29,6 +29,8 @@ export function ElectronBrowserHost() {
previewState.serverEpoch,
snapshot.tabId,
),
+ pictureInPicture:
+ previewState.desktopByTabId[snapshot.tabId]?.pictureInPicture ?? false,
zoomFactor: previewState.desktopByTabId[snapshot.tabId]?.zoomFactor ?? 1,
}))
: [];
@@ -80,7 +82,7 @@ export function ElectronBrowserHost() {
if (!isElectron) return null;
return (
<div className="contents" data-electron-browser-host>
- {sessions.map(({ threadRef, snapshot, runtimeTabId, zoomFactor }) => {
+ {sessions.map(({ threadRef, snapshot, runtimeTabId, pictureInPicture, zoomFactor }) => {
const url = snapshot.navStatus._tag === "Idle" ? null : snapshot.navStatus.url;
return (
<HostedBrowserWebview
@@ -90,6 +92,7 @@ export function ElectronBrowserHost() {
runtimeTabId={runtimeTabId}
initialUrl={url}
viewport={snapshot.viewport ?? FILL_PREVIEW_VIEWPORT}
+ pictureInPicture={pictureInPicture}
zoomFactor={zoomFactor}
/>
);
diff --git a/apps/web/src/browser/HostedBrowserWebview.tsx b/apps/web/src/browser/HostedBrowserWebview.tsx
index ae0526abb..998f564a5 100644
--- a/apps/web/src/browser/HostedBrowserWebview.tsx
+++ b/apps/web/src/browser/HostedBrowserWebview.tsx
@@ -9,6 +9,7 @@ import { usePreviewBridge } from "~/components/preview/usePreviewBridge";
import { cn } from "~/lib/utils";
import { resolveBrowserSurfacePanelRect, useBrowserSurfaceStore } from "./browserSurfaceStore";
+import { useActiveBrowserRecordingTabIds } from "./browserRecording";
import {
browserViewportSettingKey,
resolveBrowserViewportLayout,
@@ -47,9 +48,11 @@ export function HostedBrowserWebview(props: {
readonly runtimeTabId: string;
readonly initialUrl: string | null;
readonly viewport: PreviewViewportSetting;
+ readonly pictureInPicture: boolean;
readonly zoomFactor: number;
}) {
- const { threadRef, tabId, runtimeTabId, initialUrl, viewport, zoomFactor } = props;
+ const { threadRef, tabId, runtimeTabId, initialUrl, viewport, pictureInPicture, zoomFactor } =
+ props;
const config = usePreviewWebviewConfig(threadRef.environmentId);
const [initialSrc] = useState(() => initialUrl ?? "about:blank");
const tabLeaseRef = useRef<AcquiredDesktopTab | null>(null);
@@ -70,6 +73,10 @@ export function HostedBrowserWebview(props: {
};
}),
);
+ const backgroundActivity = useBrowserSurfaceStore(
+ (state) => (state.activityByTabId[runtimeTabId] ?? 0) > 0,
+ );
+ const recordingActive = useActiveBrowserRecordingTabIds().has(runtimeTabId);
usePreviewBridge({ threadRef, tabId, runtimeTabId });
useEffect(() => {
@@ -231,8 +238,10 @@ export function HostedBrowserWebview(props: {
if (!config) return null;
+ const renderingActive = active || backgroundActivity || pictureInPicture || recordingActive;
const wrapperStyle = resolveHostedBrowserWebviewWrapperStyle({
active,
+ renderingActive,
cornerRadius: presentation.cornerRadius,
rect: lastRect,
hiddenSize,
@@ -244,6 +253,7 @@ export function HostedBrowserWebview(props: {
className="fixed overflow-hidden bg-muted/35"
style={{ ...wrapperStyle, overscrollBehavior: "contain" }}
onScroll={syncContentPresentation}
+ data-preview-rendering={renderingActive ? "active" : "suspended"}
data-preview-viewport={runtimeTabId}
>
<div className="relative" style={{ width: layout.canvasWidth, height: layout.canvasHeight }}>
diff --git a/apps/web/src/browser/browserRecording.test.ts b/apps/web/src/browser/browserRecording.test.ts
index b5ef77dd2..ed954942a 100644
--- a/apps/web/src/browser/browserRecording.test.ts
+++ b/apps/web/src/browser/browserRecording.test.ts
@@ -179,10 +179,10 @@ describe("browser recording", () => {
it("starts recording for a visible tab", async () => {
await startBrowserRecording("recording-tab");
-
- expect(events).toEqual(["start-screencast", "publish:recording-tab"]);
+ const startupEvents = [...events];
await stopBrowserRecording("recording-tab");
+ expect(startupEvents).toEqual(["publish:recording-tab", "start-screencast"]);
});
it("records a hidden tab without requiring it to become visible", async () => {
@@ -195,11 +195,11 @@ describe("browser recording", () => {
};
await startBrowserRecording("recording-tab");
+ const startupEvents = [...events];
expect(startScreencast).toHaveBeenCalledWith("recording-tab");
- expect(events).toEqual(["start-screencast", "publish:recording-tab"]);
-
await stopBrowserRecording("recording-tab");
+ expect(startupEvents).toEqual(["publish:recording-tab", "start-screencast"]);
});
it("fails startup instead of locking a fallback size when no frame arrives", async () => {
diff --git a/apps/web/src/browser/browserRecording.ts b/apps/web/src/browser/browserRecording.ts
index 69297cfdb..33d4610da 100644
--- a/apps/web/src/browser/browserRecording.ts
+++ b/apps/web/src/browser/browserRecording.ts
@@ -122,6 +122,12 @@ export function useActiveBrowserRecordingTabIds(): ReadonlySet<string> {
const activeRecordings = new Map<string, ActiveRecording>();
let unsubscribeFrames: (() => void) | null = null;
+const publishActiveRecordingTabIds = (): void => {
+ appAtomRegistry.set(activeBrowserRecordingTabIdsAtom, {
+ tabIds: new Set(activeRecordings.keys()),
+ });
+};
+
export const BROWSER_RECORDING_STARTUP_SETTLE_TIMEOUT_MS = 5_000;
export const BROWSER_RECORDING_FIRST_FRAME_SIZE_TIMEOUT_MS = 5_000;
@@ -228,9 +234,7 @@ const clearActiveRecording = (recording: ActiveRecording): void => {
unsubscribeFrames?.();
unsubscribeFrames = null;
}
- appAtomRegistry.set(activeBrowserRecordingTabIdsAtom, {
- tabIds: new Set(activeRecordings.keys()),
- });
+ publishActiveRecordingTabIds();
};
const cleanupFailedRecordingStart = async (
@@ -377,6 +381,7 @@ export async function startBrowserRecording(
lifecycle: { phase: "starting" },
};
activeRecordings.set(tabId, recording);
+ publishActiveRecordingTabIds();
try {
try {
unsubscribeFrames ??= bridge.recording.onFrame(drawFrame);
@@ -487,9 +492,6 @@ export async function startBrowserRecording(
if (recording.lifecycle.phase === "starting") {
recording.lifecycle = { phase: "recording" };
}
- appAtomRegistry.set(activeBrowserRecordingTabIdsAtom, {
- tabIds: new Set(activeRecordings.keys()),
- });
return startedAt;
} finally {
settleStartup?.();
diff --git a/apps/web/src/browser/browserSurfaceStore.test.ts b/apps/web/src/browser/browserSurfaceStore.test.ts
index 12b34dd4b..249d3dcb2 100644
--- a/apps/web/src/browser/browserSurfaceStore.test.ts
+++ b/apps/web/src/browser/browserSurfaceStore.test.ts
@@ -2,13 +2,25 @@ import { beforeEach, describe, expect, it } from "vite-plus/test";
import {
acquireBrowserSurface,
+ acquireBrowserSurfaceActivity,
resolveBrowserSurfacePanelRect,
useBrowserSurfaceStore,
} from "./browserSurfaceStore";
describe("browserSurfaceStore", () => {
beforeEach(() => {
- useBrowserSurfaceStore.setState({ byTabId: {} });
+ useBrowserSurfaceStore.setState({ activityByTabId: {}, byTabId: {} });
+ });
+
+ it("keeps concurrent background work active until every lease is released", () => {
+ const first = acquireBrowserSurfaceActivity("background-browser");
+ const second = acquireBrowserSurfaceActivity("background-browser");
+
+ first();
+ expect(useBrowserSurfaceStore.getState().activityByTabId["background-browser"]).toBe(1);
+
+ second();
+ expect(useBrowserSurfaceStore.getState().activityByTabId["background-browser"]).toBeUndefined();
});
it("freezes the source content dimensions for a fitted presentation", () => {
diff --git a/apps/web/src/browser/browserSurfaceStore.ts b/apps/web/src/browser/browserSurfaceStore.ts
index 43ae0037c..fe85c9e38 100644
--- a/apps/web/src/browser/browserSurfaceStore.ts
+++ b/apps/web/src/browser/browserSurfaceStore.ts
@@ -29,7 +29,9 @@ export interface BrowserSurfaceContentPresentation {
}
interface BrowserSurfaceStoreState {
+ readonly activityByTabId: Record<string, number>;
readonly byTabId: Record<string, BrowserSurfacePresentation>;
+ readonly acquireActivity: (tabId: string) => () => void;
readonly claim: (tabId: string, owner: symbol, fitSourceContent: boolean) => void;
readonly present: (
tabId: string,
@@ -63,7 +65,28 @@ const rectEquals = (left: BrowserSurfaceRect | null, right: BrowserSurfaceRect):
left.height === right.height;
export const useBrowserSurfaceStore = create<BrowserSurfaceStoreState>()((set) => ({
+ activityByTabId: {},
byTabId: {},
+ acquireActivity: (tabId) => {
+ let released = false;
+ set((state) => ({
+ activityByTabId: {
+ ...state.activityByTabId,
+ [tabId]: (state.activityByTabId[tabId] ?? 0) + 1,
+ },
+ }));
+ return () => {
+ if (released) return;
+ released = true;
+ set((state) => {
+ const count = state.activityByTabId[tabId] ?? 0;
+ const activityByTabId = { ...state.activityByTabId };
+ if (count <= 1) delete activityByTabId[tabId];
+ else activityByTabId[tabId] = count - 1;
+ return { activityByTabId };
+ });
+ };
+ },
claim: (tabId, owner, fitSourceContent) =>
set((state) => {
const current = state.byTabId[tabId];
@@ -171,6 +194,9 @@ export const useBrowserSurfaceStore = create<BrowserSurfaceStoreState>()((set) =
}),
}));
+export const acquireBrowserSurfaceActivity = (tabId: string): (() => void) =>
+ useBrowserSurfaceStore.getState().acquireActivity(tabId);
+
export function acquireBrowserSurface(
tabId: string,
fitSourceContent = false,
diff --git a/apps/web/src/browser/hostedBrowserWebviewStyle.test.ts b/apps/web/src/browser/hostedBrowserWebviewStyle.test.ts
index d0298dcde..c2c78f4ac 100644
--- a/apps/web/src/browser/hostedBrowserWebviewStyle.test.ts
+++ b/apps/web/src/browser/hostedBrowserWebviewStyle.test.ts
@@ -10,6 +10,7 @@ describe("resolveHostedBrowserWebviewWrapperStyle", () => {
expect(
resolveHostedBrowserWebviewWrapperStyle({
active: true,
+ renderingActive: true,
rect: { x: 12, y: 34, width: 800, height: 600 },
hiddenSize: { width: 1280, height: 800 },
}),
@@ -27,6 +28,7 @@ describe("resolveHostedBrowserWebviewWrapperStyle", () => {
expect(
resolveHostedBrowserWebviewWrapperStyle({
active: true,
+ renderingActive: true,
cornerRadius: 12,
rect: { x: 12, y: 34, width: 360, height: 203 },
hiddenSize: { width: 1280, height: 800 },
@@ -40,9 +42,10 @@ describe("resolveHostedBrowserWebviewWrapperStyle", () => {
});
});
- it("keeps an inactive webview paintable while moving it offscreen", () => {
+ it("suspends painting for an inactive webview", () => {
const style = resolveHostedBrowserWebviewWrapperStyle({
active: false,
+ renderingActive: false,
rect: { x: 12, y: 34, width: 800, height: 600 },
hiddenSize: { width: 393, height: 852 },
});
@@ -54,6 +57,25 @@ describe("resolveHostedBrowserWebviewWrapperStyle", () => {
height: 852,
zIndex: -1,
pointerEvents: "none",
+ visibility: "hidden",
+ });
+ });
+
+ it("keeps an active background task paintable offscreen", () => {
+ const style = resolveHostedBrowserWebviewWrapperStyle({
+ active: false,
+ renderingActive: true,
+ rect: null,
+ hiddenSize: { width: 1280, height: 800 },
+ });
+
+ expect(style).toEqual({
+ left: HIDDEN_BROWSER_WEBVIEW_OFFSET,
+ top: HIDDEN_BROWSER_WEBVIEW_OFFSET,
+ width: 1280,
+ height: 800,
+ zIndex: -1,
+ pointerEvents: "none",
visibility: "visible",
});
});
diff --git a/apps/web/src/browser/hostedBrowserWebviewStyle.ts b/apps/web/src/browser/hostedBrowserWebviewStyle.ts
index f96f4af04..1e6ff0bea 100644
--- a/apps/web/src/browser/hostedBrowserWebviewStyle.ts
+++ b/apps/web/src/browser/hostedBrowserWebviewStyle.ts
@@ -13,18 +13,19 @@ export interface HostedBrowserWebviewWrapperStyle {
readonly zIndex: number;
readonly pointerEvents: "auto" | "none";
readonly borderRadius?: number;
- readonly visibility?: "visible";
+ readonly visibility?: "hidden" | "visible";
}
export const HIDDEN_BROWSER_WEBVIEW_OFFSET = -100_000;
export function resolveHostedBrowserWebviewWrapperStyle(input: {
readonly active: boolean;
+ readonly renderingActive: boolean;
readonly cornerRadius?: number;
readonly rect: BrowserSurfaceRect | null;
readonly hiddenSize: HostedBrowserWebviewSize;
}): HostedBrowserWebviewWrapperStyle {
- const { active, cornerRadius = 0, hiddenSize, rect } = input;
+ const { active, cornerRadius = 0, hiddenSize, rect, renderingActive } = input;
if (active && rect) {
return {
left: rect.x,
@@ -44,9 +45,6 @@ export function resolveHostedBrowserWebviewWrapperStyle(input: {
height: hiddenSize.height,
zIndex: -1,
pointerEvents: "none",
- // Keep the guest CSS-visible even while physically offscreen. Electron
- // webviews can keep metadata/status alive under `visibility:hidden` while
- // CDP Runtime/Input commands stall, which breaks offscreen automation.
- visibility: "visible",
+ visibility: renderingActive ? "visible" : "hidden",
};
}
diff --git a/apps/web/src/components/preview/PreviewAutomationHosts.tsx b/apps/web/src/components/preview/PreviewAutomationHosts.tsx
index acf7e52e3..1faf928b1 100644
--- a/apps/web/src/components/preview/PreviewAutomationHosts.tsx
+++ b/apps/web/src/components/preview/PreviewAutomationHosts.tsx
@@ -37,7 +37,10 @@ import {
stopBrowserRecording,
} from "~/browser/browserRecording";
import { resolveBrowserRecordingStopTarget } from "~/browser/browserRecordingScope";
-import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore";
+import {
+ acquireBrowserSurfaceActivity,
+ useBrowserSurfaceStore,
+} from "~/browser/browserSurfaceStore";
import { browserDefaultOpenViewport, resolveBrowserDefaults } from "~/browser/browserDefaults";
import { runBrowserViewportMutation } from "~/browser/browserViewportActions";
import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId";
@@ -98,7 +101,7 @@ const waitForDesktopOverlay = async (
operation,
requestId,
});
- if (state.desktopByTabId[tabId] && previewBridge) {
+ if (state.desktopByTabId[tabId] && previewBridge && isPreviewWebviewRendering(runtimeTabId)) {
const status = await previewBridge.automation.status(runtimeTabId);
if (status.available) return;
}
@@ -121,6 +124,11 @@ const findPreviewWebview = (tabId: string): ExecutablePreviewWebview | null =>
(candidate) => candidate.getAttribute("data-preview-tab") === tabId,
) ?? null;
+const isPreviewWebviewRendering = (runtimeTabId: string): boolean => {
+ const wrapper = findPreviewWebview(runtimeTabId)?.closest<HTMLElement>("[data-preview-viewport]");
+ return wrapper?.getAttribute("data-preview-rendering") === "active";
+};
+
const readWebviewViewport = async (
webview: ExecutablePreviewWebview,
): Promise<PreviewRenderedViewportSize | null> => {
@@ -212,8 +220,12 @@ const currentStatus = async (
const visible = runtimeTabId
? (useBrowserSurfaceStore.getState().byTabId[runtimeTabId]?.visible ?? false)
: false;
+ const renderingActive = runtimeTabId ? isPreviewWebviewRendering(runtimeTabId) : false;
const viewportSetting = snapshot ? (snapshot.viewport ?? FILL_PREVIEW_VIEWPORT) : undefined;
- const viewport = runtimeTabId ? await readRenderedViewport(runtimeTabId).catch(() => null) : null;
+ const viewport =
+ runtimeTabId && renderingActive
+ ? await readRenderedViewport(runtimeTabId).catch(() => null)
+ : null;
const viewportStatus = {
...(viewportSetting === undefined ? {} : { viewportSetting }),
...(viewport === null ? {} : { viewport }),
@@ -307,6 +319,7 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
threadId: request.threadId,
};
let tabId = request.tabId ?? null;
+ const browserActivity = { release: null as (() => void) | null };
try {
let state = readThreadPreviewState(threadRef);
const needsSessionSync = needsPreviewAutomationSessionSync(state, request.tabId);
@@ -340,6 +353,7 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
}
const readyState = readThreadPreviewState(threadRef);
const runtimeTabId = previewRuntimeTabId(threadRef, readyState.serverEpoch, readyTabId);
+ browserActivity.release ??= acquireBrowserSurfaceActivity(runtimeTabId);
await waitForDesktopOverlay(
threadRef,
request.requestId,
@@ -440,14 +454,7 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
usePreviewMiniPlayerStore.getState().open(threadRef, activeTabId);
}
if (activeSnapshot && previewAutomationOpenNeedsOverlay(input, activeSnapshot)) {
- await waitForDesktopOverlay(
- threadRef,
- request.requestId,
- activeTabId,
- activeRuntimeTabId,
- request.operation,
- request.timeoutMs,
- );
+ await requireReadyTab();
}
if (shouldPresentPreview) {
// React commits the thread-bound surface asynchronously. Settle
@@ -678,6 +685,8 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
tabId,
cause,
});
+ } finally {
+ browserActivity.release?.();
}
},
[environmentId, listPreviews, open, registry, resize],
diff --git a/apps/web/src/previewStateStore.test.ts b/apps/web/src/previewStateStore.test.ts
index bfe5d46b1..321dd68c0 100644
--- a/apps/web/src/previewStateStore.test.ts
+++ b/apps/web/src/previewStateStore.test.ts
@@ -20,6 +20,7 @@ import {
rememberPreviewUrl,
removePreviewThread,
resetPreviewStateForTests,
+ subscribeThreadPreviewState,
setActivePreviewTab,
updatePreviewServerSnapshot,
} from "./previewStateStore";
@@ -331,6 +332,38 @@ describe("previewStateStore (single-tab)", () => {
expect(state.snapshot?.canGoBack).toBe(false);
});
+ it("does not publish duplicate desktop browser state", () => {
+ const snapshot = makeSnapshot();
+ applyPreviewServerSnapshot(ref, snapshot);
+ const overlay = {
+ hasWebContents: true,
+ canGoBack: true,
+ canGoForward: false,
+ loading: false,
+ zoomFactor: 1,
+ pictureInPicture: false,
+ colorScheme: "system" as const,
+ audioMuted: false,
+ audible: false,
+ controller: "none" as const,
+ favicon: {
+ dataUrl: "data:image/png;base64,AA==",
+ pageUrl: "https://example.com",
+ capturedAt: 1,
+ },
+ };
+ let updateCount = 0;
+ const unsubscribe = subscribeThreadPreviewState(ref, () => {
+ updateCount += 1;
+ });
+
+ applyPreviewDesktopState(ref, snapshot.tabId, overlay);
+ applyPreviewDesktopState(ref, snapshot.tabId, { ...overlay, favicon: { ...overlay.favicon } });
+ unsubscribe();
+
+ expect(updateCount).toBe(1);
+ });
+
it("retains multiple tabs and switches active desktop state", () => {
const first = makeSnapshot();
const second = { ...makeSnapshot(), tabId: "tab_2", updatedAt: "2026-01-02T00:00:00.000Z" };
diff --git a/apps/web/src/previewStateStore.ts b/apps/web/src/previewStateStore.ts
index a40e65fbc..eb1052fee 100644
--- a/apps/web/src/previewStateStore.ts
+++ b/apps/web/src/previewStateStore.ts
@@ -368,12 +368,39 @@ export function reconcilePreviewServerSessions(
});
}
+function isPreviewStateEqual(
+ previous: DesktopPreviewOverlay | null,
+ next: DesktopPreviewOverlay | null,
+) {
+ return (
+ previous === next ||
+ (previous !== null &&
+ next !== null &&
+ previous.hasWebContents === next.hasWebContents &&
+ previous.canGoBack === next.canGoBack &&
+ previous.canGoForward === next.canGoForward &&
+ previous.loading === next.loading &&
+ previous.zoomFactor === next.zoomFactor &&
+ previous.pictureInPicture === next.pictureInPicture &&
+ previous.colorScheme === next.colorScheme &&
+ previous.audioMuted === next.audioMuted &&
+ previous.audible === next.audible &&
+ previous.controller === next.controller &&
+ previous.favicon?.dataUrl === next.favicon?.dataUrl &&
+ previous.favicon?.pageUrl === next.favicon?.pageUrl &&
+ previous.favicon?.capturedAt === next.favicon?.capturedAt)
+ );
+}
+
export function applyPreviewDesktopState(
ref: ScopedThreadRef,
tabId: string,
overlay: DesktopPreviewOverlay | null,
): void {
updateThreadPreviewState(ref, (current) => {
+ if (isPreviewStateEqual(current.desktopByTabId[tabId] ?? null, overlay)) {
+ return current;
+ }
const desktopByTabId = { ...current.desktopByTabId };
if (overlay) desktopByTabId[tabId] = overlay;
else delete desktopByTabId[tabId];