@devries-cameron20 / t3code-1 / commits / 0c6f421f3c
Fork 0

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

perf(desktop): stop rebroadcasting preview ownership on every key (#29312)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Lakshmi Tanmay committed Nov 14, 2026 at 20:19 UTC 0c6f421f3c145ad5c7ff75ebbb4d4996ad7537d7
2 files changed +172 -20
apps/desktop/src/preview/Manager.test.ts
+110
@@ -2756,6 +2756,116 @@ describe("PreviewManager", () => {
2756 ),
2757 );
2758
2759 + effectIt.effect(
2760 + "publishes human ownership only on transitions while retaining every recording press",
2761 + () =>
2762 + withManager((manager) =>
2763 + Effect.gen(function* () {
2764 + let humanInput: ((input: unknown) => void) | undefined;
2765 + const contents = makeTestPreviewWebContents(async () => ({
2766 + toJPEG: () => Buffer.from("frame"),
2767 + getSize: () => ({ width: 800, height: 600 }),
2768 + }));
2769 + contents.ipc.on = (channel, callback) => {
2770 + if (channel === "preview:human-input")
2771 + humanInput = (input) => callback({} as Electron.IpcMainEvent, input);
2772 + return contents.ipc;
2773 + };
2774 + fromId.mockReturnValue(contents);
2775 + yield* manager.createTab("tab_human");
2776 + yield* manager.registerWebview("tab_human", 42);
2777 + yield* manager.startRecording("tab_human", {
2778 + showKeyPresses: true,
2779 + showMousePresses: true,
2780 + });
2781 + const controllers: string[] = [];
2782 + const feedback = yield* Queue.unbounded<void>();
2783 + webviewSend.mockImplementation((channel) => {
2784 + if (channel === "preview:recording-controller") Queue.offerUnsafe(feedback, undefined);
2785 + });
2786 + yield* manager.subscribeStateChanges((_tabId, state) =>
2787 + Effect.sync(() => {
2788 + controllers.push(state.controller);
2789 + }),
2790 + );
2791 + for (const press of [
2792 + { kind: "pointer", x: 10, y: 20, button: 0 },
2793 + { kind: "pointer", x: 30, y: 40, button: 0 },
2794 + { kind: "key", key: "a", code: "KeyA" },
2795 + ]) {
2796 + humanInput?.(press);
2797 + yield* Queue.take(feedback);
2798 + }
2799 + expect(controllers).toEqual(["human"]);
2800 + expect(
2801 + webviewSend.mock.calls.filter(
2802 + ([channel]) => channel === "preview:recording-controller",
2803 + ),
2804 + ).toEqual([
2805 + ["preview:recording-controller", "human", { x: 10, y: 20 }],
2806 + ["preview:recording-controller", "human", { x: 30, y: 40 }],
2807 + ["preview:recording-controller", "human", undefined],
2808 + ]);
2809 + yield* TestClock.adjust(750);
2810 + expect(controllers).toEqual(["human", "none"]);
2811 + }),
2812 + ),
2813 + );
2814 +
2815 + effectIt.effect("keeps human ownership until the last input's idle deadline", () =>
2816 + withManager((manager) =>
2817 + Effect.gen(function* () {
2818 + let humanInput: ((input: unknown) => void) | undefined;
2819 + const contents = makeTestPreviewWebContents(async () => ({
2820 + toJPEG: () => Buffer.from("frame"),
2821 + getSize: () => ({ width: 800, height: 600 }),
2822 + }));
2823 + contents.ipc.on = (channel, callback) => {
2824 + if (channel === "preview:human-input")
2825 + humanInput = (input) => callback({} as Electron.IpcMainEvent, input);
2826 + return contents.ipc;
2827 + };
2828 + fromId.mockReturnValue(contents);
2829 + yield* manager.createTab("tab_idle");
2830 + yield* manager.registerWebview("tab_idle", 42);
2831 + const controllers: string[] = [];
2832 + yield* manager.subscribeStateChanges((_tabId, state) =>
2833 + Effect.sync(() => {
2834 + controllers.push(state.controller);
2835 + }),
2836 + );
2837 + const press = () => humanInput?.({ kind: "key", key: "a", code: "KeyA" });
2838 + press();
2839 + yield* TestClock.adjust(500);
2840 + press();
2841 + yield* TestClock.adjust(749);
2842 + expect(controllers).toEqual(["human"]);
2843 + yield* TestClock.adjust(1);
2844 + expect(controllers).toEqual(["human", "none"]);
2845 +
2846 + press();
2847 + yield* TestClock.adjust(500);
2848 + const staleInput = humanInput;
2849 + const replacement = makeTestPreviewWebContents(
2850 + async () => ({
2851 + toJPEG: () => Buffer.from("frame"),
2852 + getSize: () => ({ width: 800, height: 600 }),
2853 + }),
2854 + 43,
2855 + );
2856 + replacement.ipc.on = contents.ipc.on;
2857 + contents.isDestroyed = () => true;
2858 + fromId.mockImplementation((id) => (id === 43 ? replacement : null));
2859 + yield* manager.registerWebview("tab_idle", 43);
2860 + expect(controllers.at(-1)).toBe("none");
2861 + const count = controllers.length;
2862 + staleInput?.({ kind: "key", key: "b", code: "KeyB" });
2863 + yield* TestClock.adjust(750);
2864 + expect(controllers).toHaveLength(count);
2865 + }),
2866 + ),
2867 + );
2868 +
2869 effectIt.effect("gates recording decorations and isolates failed subscribers", () =>
2870 withManager((manager) =>
2871 Effect.gen(function* () {
apps/desktop/src/preview/Manager.ts
+62 -20
@@ -889,7 +889,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
889 const update = Effect.fn("PreviewManager.update")(function* (
890 tabId: string,
891 patch: Partial<PreviewTabState>,
892 - humanPoint?: { readonly x: number; readonly y: number },
892 ) {
893 const updatedAt = yield* currentIso;
894 const next = yield* SynchronizedRef.modify(tabsRef, (tabs) => {
@@ -905,17 +904,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
904 });
905 // syncTabAudible can commit after this update; emitIfCurrent prevents publishing a stale snapshot.
906 if (Option.isSome(next)) {
908 - if (patch.controller !== undefined && next.value.webContentsId != null) {
909 - const capture = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId);
910 - const webContentsId = next.value.webContentsId;
911 - if (capture?.consumers.has("recording")) {
912 - yield* attempt({ operation: "recording.controller", tabId }, () => {
913 - const contents = webContents.fromId(webContentsId);
914 - if (contents && !contents.isDestroyed())
915 - contents.send(RECORDING_CONTROLLER_CHANNEL, patch.controller, humanPoint);
916 - }).pipe(Effect.ignore);
917 - }
918 - }
907 yield* emitIfCurrent(tabId, next.value);
908 }
909 });
@@ -1444,21 +1432,75 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
1432 }),
1433 );
1434 };
1435 + let humanInputWorker = false;
1436 + let humanInputDeadline = 0;
1437 + let listening = true;
1438 + const publishHumanController = Effect.fn("PreviewManager.publishHumanController")(function* (
1439 + controller: "human" | "none",
1440 + humanPoint?: { readonly x: number; readonly y: number },
1441 + ) {
1442 + const now = yield* currentMillis;
1443 + const updatedAt = yield* currentIso;
1444 + const result = yield* SynchronizedRef.modify(tabsRef, (tabs) => {
1445 + const current = tabs.get(tabId);
1446 + if (
1447 + !current ||
1448 + current.webContentsId !== wc.id ||
1449 + (controller === "human"
1450 + ? !listening || webContents.fromId(wc.id) !== wc
1451 + : current.controller !== "human" || humanInputDeadline > now)
1452 + )
1453 + return [undefined, tabs] as const;
1454 + const changed = current.controller !== controller;
1455 + const state = changed ? { ...current, controller, updatedAt } : current;
1456 + return [
1457 + { state, changed },
1458 + changed
1459 + ? replaceMap(tabs, (copy) => {
1460 + copy.set(tabId, state);
1461 + })
1462 + : tabs,
1463 + ] as const;
1464 + });
1465 + if (!result) return;
1466 + const capture = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId);
1467 + if (capture?.consumers.has("recording")) {
1468 + yield* attempt({ operation: "recording.controller", tabId }, () => {
1469 + if (!wc.isDestroyed()) wc.send(RECORDING_CONTROLLER_CHANNEL, controller, humanPoint);
1470 + }).pipe(Effect.ignore);
1471 + }
1472 + if (result.changed) yield* emitIfCurrent(tabId, result.state);
1473 + });
1474 + yield* Scope.addFinalizer(
1475 + scope,
1476 + Effect.gen(function* () {
1477 + listening = false;
1478 + humanInputDeadline = 0;
1479 + yield* publishHumanController("none");
1480 + }),
1481 + );
1482 const handleHumanInput = Effect.fn("PreviewManager.handleHumanInput")(function* (
1483 rawSignal?: unknown,
1484 ) {
1450 - yield* update(
1451 - tabId,
1452 - { controller: "human" },
1485 + if (!listening) return;
1486 + humanInputDeadline = (yield* currentMillis) + 750;
1487 + if (!humanInputWorker) {
1488 + humanInputWorker = true;
1489 + yield* Effect.gen(function* () {
1490 + let remaining: number;
1491 + while ((remaining = humanInputDeadline - (yield* currentMillis)) > 0) {
1492 + yield* Effect.sleep(remaining);
1493 + }
1494 + humanInputWorker = false;
1495 + yield* publishHumanController("none");
1496 + }).pipe(Effect.forkIn(scope));
1497 + }
1498 + yield* publishHumanController(
1499 + "human",
1500 isPreviewInputSignal(rawSignal) && rawSignal.kind === "pointer"
1501 ? { x: rawSignal.x, y: rawSignal.y }
1502 : undefined,
1503 );
1457 - yield* Effect.sleep(750);
1458 - const tabs = yield* SynchronizedRef.get(tabsRef);
1459 - if (tabs.get(tabId)?.controller === "human") {
1460 - yield* update(tabId, { controller: "none" });
1461 - }
1504 });
1505 const recordingInput = (_event: unknown, input: unknown) => {
1506 if (!isRecordingInput(input)) return;