@devries-cameron20 / t3code Mirror · read-only / commits / 17db7082c0
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 21, 12:15 UTC

fix(server): preview port discovery stops polling while no client is in the foreground (#29782)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 14, 2026 at 20:51 UTC 17db7082c0679e51371412ed823c79eac39e4e81
3 files changed +60 -8
apps/server/src/preview/PortScanner.test.ts
+50 -6
@@ -22,6 +22,7 @@ import * as TestClock from "effect/testing/TestClock";
22 import { expect, vi } from "vite-plus/test";
23 import { FetchHttpClient } from "effect/http";
24
25 +import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
26 import * as ProcessRunner from "../processRunner.ts";
27 import * as PortScanner from "./PortScanner.ts";
28
@@ -44,12 +45,18 @@ const processProbeFailure: ProcessRunner.ProcessRunner["Service"]["run"] = (inpu
45
46 const layerNoProc = FileSystem.layerNoop({});
47
48 +const layerForeground = Layer.mock(BackgroundPolicy.BackgroundPolicy)({
49 + shouldRunOpportunisticWork: Effect.succeed(true),
50 +});
51 +
52 +const layerScanner = PortScanner.layer.pipe(Layer.provide(layerForeground));
53 +
54 const layerProbeFailure = (
55 run: ProcessRunner.ProcessRunner["Service"]["run"],
56 fetch: typeof globalThis.fetch = globalThis.fetch,
57 fileSystem: Layer.Layer<FileSystem.FileSystem> = layerNoProc,
58 ) =>
52 - PortScanner.layer.pipe(
59 + layerScanner.pipe(
60 Layer.provide(
61 Layer.mergeAll(
62 fileSystem,
@@ -86,10 +93,12 @@ const layerLsofScanner = (input: {
93 readonly pid: () => number;
94 readonly fetch: typeof globalThis.fetch;
95 readonly stdout?: () => string;
96 + readonly backgroundPolicy?: Layer.Layer<BackgroundPolicy.BackgroundPolicy>;
97 }) =>
98 PortScanner.layer.pipe(
99 Layer.provide(
100 Layer.mergeAll(
101 + input.backgroundPolicy ?? layerForeground,
102 layerNoProc,
103 Layer.succeed(ProcessRunner.ProcessRunner, {
104 run: () =>
@@ -287,6 +296,41 @@ effectIt.effect("stops probing a subscriber's configured paths after its scope c
296 }).pipe(Effect.scoped, Effect.provide(layer));
297 });
298
299 +effectIt.effect("skips scheduled scans while no client is in the foreground", () => {
300 + let lsofRuns = 0;
301 + let foreground = true;
302 + const layer = layerLsofScanner({
303 + pid: () => 1234,
304 + fetch: (() => Promise.resolve(new Response("not found", { status: 404 }))) as typeof fetch,
305 + stdout: () => {
306 + lsofRuns += 1;
307 + return `p1234\ncnode\nn*:${LSOF_TEST_PORT}\n`;
308 + },
309 + backgroundPolicy: Layer.mock(BackgroundPolicy.BackgroundPolicy)({
310 + shouldRunOpportunisticWork: Effect.sync(() => foreground),
311 + }),
312 + });
313 +
314 + return Effect.gen(function* () {
315 + const scanner = yield* PortScanner.PortDiscovery;
316 + yield* scanner.subscribe(
317 + { configuredUrls: [`http://localhost:${LSOF_TEST_PORT}/`], initialSnapshot: [] },
318 + () => Effect.void,
319 + );
320 + yield* scanner.retain;
321 + const afterInitialScan = lsofRuns;
322 + expect(afterInitialScan).toBeGreaterThan(0);
323 +
324 + foreground = false;
325 + yield* TestClock.adjust(Duration.seconds(15));
326 + expect(lsofRuns).toBe(afterInitialScan);
327 +
328 + foreground = true;
329 + yield* TestClock.adjust(Duration.seconds(3));
330 + expect(lsofRuns).toBeGreaterThan(afterInitialScan);
331 + }).pipe(Effect.scoped, Effect.provide(layer));
332 +});
333 +
334 effectIt.effect("uses the current configured fragment when readiness comes from cache", () => {
335 const requests: string[] = [];
336 const fetchFn = ((input: Parameters<typeof globalThis.fetch>[0]) => {
@@ -554,7 +598,7 @@ effectIt.effect.each([
598 let lsofSpawns = 0;
599 let procWalks = 0;
600 const missing = FileSystem.makeNoop({});
557 - const layer = PortScanner.layer.pipe(
601 + const layer = layerScanner.pipe(
602 Layer.provide(
603 Layer.mergeAll(
604 FileSystem.layerNoop({
@@ -635,7 +679,7 @@ effectIt.effect("ignores a partial Windows netstat snapshot when one query exits
679 requests.push(String(input));
680 return Promise.resolve(new Response("hello", { headers: { "content-type": "text/html" } }));
681 }) as typeof globalThis.fetch;
638 - const layer = PortScanner.layer.pipe(
682 + const layer = layerScanner.pipe(
683 Layer.provide(
684 Layer.mergeAll(
685 layerNoProc,
@@ -744,7 +788,7 @@ effectIt.effect.each(["linux", "darwin", "win32"] as const)(
788 const fileReads: string[] = [];
789 const requests: string[] = [];
790 const missing = FileSystem.makeNoop({});
747 - const layer = PortScanner.layer.pipe(
791 + const layer = layerScanner.pipe(
792 Layer.provide(
793 Layer.mergeAll(
794 FileSystem.layerNoop({
@@ -811,7 +855,7 @@ effectIt.effect.each(["linux", "darwin"] as const)(
855 "/proc/4242/environ": `PATH=/usr/bin\0${PortScanner.AGENT_THREAD_ENV}=thread-agent\0`,
856 "/proc/5000/environ": "PATH=/usr/bin\0",
857 };
814 - const layer = PortScanner.layer.pipe(
858 + const layer = layerScanner.pipe(
859 Layer.provide(
860 Layer.mergeAll(
861 FileSystem.layerNoop({
@@ -1051,7 +1095,7 @@ effectIt.live.skipIf(HostProcess.Platform.defaultValue() === "win32")(
1095 }).pipe(
1096 Effect.scoped,
1097 Effect.provide(
1054 - PortScanner.layer.pipe(
1098 + layerScanner.pipe(
1099 Layer.provide(ProcessRunner.layer),
1100 Layer.provide(Layer.mergeAll(NodeServices.layer, FetchHttpClient.layer)),
1101 ),
apps/server/src/preview/PortScanner.ts
+6 -1
@@ -24,6 +24,7 @@ import * as Scope from "effect/Scope";
24 import * as Semaphore from "effect/Semaphore";
25 import { FetchHttpClient, HttpClient } from "effect/http";
26
27 +import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
28 import * as WindowsTcpListeners from "../process/windowsTcpListeners.ts";
29 import * as ProcessRunner from "../processRunner.ts";
30
@@ -508,6 +509,7 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
509 const hostPlatform = yield* HostProcess.Platform;
510 const listenerDiscovery = (yield* HostProcess.Environment)[PORT_DISCOVERY_ENV] !== "0";
511 const httpClient = (yield* HttpClient.HttpClient).pipe(HttpClient.withScope);
512 + const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
513 const stateRef = yield* Ref.make<ScannerState>({
514 listeners: new Map(),
515 terminalProcesses: new Map(),
@@ -973,7 +975,10 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
975 const pollIfRetained = Ref.get(stateRef).pipe(
976 Effect.flatMap((state) => (state.retainCount > 0 ? pollTick() : Effect.void)),
977 );
976 - yield* Effect.forkScoped(pollIfRetained.pipe(Effect.repeat(Schedule.spaced(POLL_INTERVAL))));
978 + const scheduledPoll = backgroundPolicy.shouldRunOpportunisticWork.pipe(
979 + Effect.flatMap((foreground) => (foreground ? pollIfRetained : Effect.void)),
980 + );
981 + yield* Effect.forkScoped(scheduledPoll.pipe(Effect.repeat(Schedule.spaced(POLL_INTERVAL))));
982
983 const acquireRetention = Effect.fn("PortDiscovery.retain")(function* () {
984 const wasIdle = yield* Ref.modify(stateRef, (state) => [
apps/server/src/server.ts
+4 -1
@@ -506,7 +506,10 @@ const layerVcs = Layer.empty.pipe(
506
507 const layerCheckpointStore = CheckpointStore.layer.pipe(Layer.provide(layerVcsDriverRegistry));
508
509 -const layerPortScanner = PortScanner.layer.pipe(Layer.provide(ProcessRunner.layer));
509 +const layerPortScanner = PortScanner.layer.pipe(
510 + Layer.provide(ProcessRunner.layer),
511 + Layer.provide(layerBackground),
512 +);
513
514 const layerTerminal = TerminalManager.layer(
515 ProviderInstanceRegistryHydration.deriveProviderInstanceConfigMap,