@devries-cameron20 / t3code Mirror · read-only / commits / a31f1f2086
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 23, 04:15 UTC

fix(desktop): bound preview recording warmup (#20085)

Luís Miguel committed Oct 18, 2026 at 20:19 UTC a31f1f208639756b68a6dc530cb5f663f3b2478c
2 files changed +93 -8
apps/desktop/src/preview/Manager.test.ts
+79
@@ -3322,6 +3322,85 @@ describe("PreviewManager", () => {
3322 ),
3323 );
3324
3325 + effectIt.effect(
3326 + "releases queued STOP after stalled recording warmups and ignores late completion",
3327 + () =>
3328 + withManager((manager) =>
3329 + Effect.gen(function* () {
3330 + const warmupStarted = Promise.withResolvers<void>();
3331 + const firstWarmup = Promise.withResolvers<TestCapturedPreviewImage>();
3332 + const lateWarmup = Promise.withResolvers<TestCapturedPreviewImage>();
3333 + const capturePage = vi
3334 + .fn<() => Promise<TestCapturedPreviewImage>>()
3335 + .mockImplementationOnce(() => {
3336 + warmupStarted.resolve();
3337 + return firstWarmup.promise;
3338 + })
3339 + .mockImplementation(() => lateWarmup.promise);
3340 + const host = makeTestHostWebContents();
3341 + const webContents = makeTestPreviewWebContents(capturePage, 42, host);
3342 + const windowThrottling = vi.fn();
3343 + fromId.mockReturnValue(webContents);
3344 + yield* manager.createTab("tab_recording_stalled_warmup");
3345 + yield* manager.registerWebview("tab_recording_stalled_warmup", 42);
3346 + yield* manager.setMainWindow({
3347 + isDestroyed: () => false,
3348 + once: vi.fn(),
3349 + webContents: { setBackgroundThrottling: windowThrottling },
3350 + } as never);
3351 +
3352 + const image = {
3353 + toJPEG: () => Buffer.from("late-recording-frame"),
3354 + getSize: () => ({ width: 1280, height: 720 }),
3355 + };
3356 + try {
3357 + const start = yield* manager
3358 + .startRecording("tab_recording_stalled_warmup")
3359 + .pipe(Effect.forkChild({ startImmediately: true }));
3360 + yield* Effect.promise(() => warmupStarted.promise);
3361 + let stopped = false;
3362 + const stop = yield* manager.stopRecording("tab_recording_stalled_warmup").pipe(
3363 + Effect.andThen(
3364 + Effect.sync(() => {
3365 + stopped = true;
3366 + }),
3367 + ),
3368 + Effect.forkChild({ startImmediately: true }),
3369 + );
3370 + expect(webContents.setBackgroundThrottling.mock.calls).toEqual([[false]]);
3371 + expect(windowThrottling.mock.calls).toEqual([[false]]);
3372 + yield* TestClock.adjust(1_000);
3373 + expect(stopped).toBe(false);
3374 + expect(host.executeJavaScript).not.toHaveBeenCalled();
3375 + yield* TestClock.adjust(1_000);
3376 + expect(stopped).toBe(true);
3377 + yield* Fiber.join(start);
3378 + yield* Fiber.join(stop);
3379 + expect(capturePage).toHaveBeenCalledTimes(2);
3380 + expect(host.executeJavaScript).toHaveBeenCalledOnce();
3381 + expect(webContents.setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
3382 + expect(windowThrottling.mock.calls).toEqual([[false], [true]]);
3383 + expect(webviewSend).toHaveBeenLastCalledWith("preview:recording-cursor", false);
3384 +
3385 + lateWarmup.resolve(image);
3386 + yield* TestClock.adjust(0);
3387 + const streams: Array<{ video?: unknown }> = [];
3388 + host.displayMediaHandler()?.({ frame: host.mainFrame }, (value) => streams.push(value));
3389 + expect(streams).toEqual([{}]);
3390 + expect(capturePage).toHaveBeenCalledTimes(2);
3391 + expect(host.executeJavaScript).toHaveBeenCalledOnce();
3392 + expect(webContents.setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
3393 + expect(windowThrottling.mock.calls).toEqual([[false], [true]]);
3394 + expect(webviewSend).toHaveBeenLastCalledWith("preview:recording-cursor", false);
3395 + } finally {
3396 + // Release stalled native calls after a failing assertion against unchanged base.
3397 + firstWarmup.resolve(image);
3398 + lateWarmup.resolve(image);
3399 + }
3400 + }),
3401 + ),
3402 + );
3403 +
3404 effectIt.effect("continues native recording when the source warmup fails", () =>
3405 withManager((manager) =>
3406 Effect.gen(function* () {
apps/desktop/src/preview/Manager.ts
+14 -8
@@ -3095,14 +3095,20 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
3095 yield* attempt({ operation: "recording.cursor", tabId, webContentsId: wc.id }, () =>
3096 wc.send(RECORDING_CURSOR_CHANNEL, true, options, tab?.controller),
3097 );
3098 - yield* attemptPromise(
3099 - {
3100 - operation: "recording.warmSource",
3101 - tabId,
3102 - webContentsId: wc.id,
3103 - },
3104 - () => wc.capturePage().then(() => undefined),
3105 - ).pipe(Effect.retry({ times: 1 }), Effect.ignore);
3098 + const warmupContext = { operation: "recording.warmSource", tabId, webContentsId: wc.id };
3099 + yield* Effect.tryPromise({
3100 + // Electron cannot cancel capturePage, but a stalled warmup must release the tab lock.
3101 + try: (_signal) => wc.capturePage(),
3102 + catch: (cause) => new PreviewOperationError({ ...warmupContext, cause }),
3103 + }).pipe(
3104 + Effect.timeout(CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS),
3105 + Effect.catchTags({
3106 + TimeoutError: (cause) =>
3107 + Effect.fail(new PreviewOperationError({ ...warmupContext, cause })),
3108 + }),
3109 + Effect.retry({ times: 1 }),
3110 + Effect.ignore,
3111 + );
3112 const currentWebContents = yield* requireWebContents(tabId);
3113 if (currentWebContents !== wc || wc.isDestroyed()) {
3114 return yield* new PreviewWebContentsNotFoundError({