diff --git a/apps/coder-gateway/src/server.test.ts b/apps/coder-gateway/src/server.test.ts index 8e673e0ef2070..6740f6be910cb 100644 --- a/apps/coder-gateway/src/server.test.ts +++ b/apps/coder-gateway/src/server.test.ts @@ -276,6 +276,68 @@ describe("local Coder gateway", () => { } }); + it("suggests the requested or next loopback port that no forward or process holds", async () => { + const directory = await NodeFS.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-coder-gateway-")); + tempDirectories.push(directory); + const occupant = NodeNet.createServer(); + occupant.listen(0, CODER_GATEWAY_HOST); + await once(occupant, "listening"); + try { + const held = (occupant.address() as NodeNet.AddressInfo).port; + const configPath = NodePath.join(directory, "config.json"); + await NodeFS.writeFile( + configPath, + JSON.stringify({ + version: 1, + deployments: [{ id: "goldman", name: "Goldman", url: "https://coder.example.gs.com" }], + workspaces: [ + { + id: "project-one", + name: "Project One", + deploymentId: "goldman", + workspace: "henry/project-one", + }, + ], + portForwards: [ + { + id: "saved", + workspaceId: "project-one", + protocol: "tcp", + localPort: held + 1, + remotePort: 3000, + }, + ], + }), + ); + const gateway = await startLocalCoderGateway({ + configPath, + // The saved rule's local port stays unbound, so only the config excludes it. + connectPortForward: async () => ({ + closed: new Promise(() => {}), + close: () => {}, + }), + }); + closeGateway = gateway.close; + const freePort = (port: unknown, origin = gateway.url) => + request({ + url: `${gateway.url}/api/port-forwards/free-port`, + method: "POST", + headers: { Origin: origin, "Content-Type": "application/json" }, + body: JSON.stringify({ port }), + }); + + strictEqual((await freePort(held, "https://attacker.example")).statusCode, 403); + strictEqual((await freePort(0)).statusCode, 400); + strictEqual((await freePort(held + 0.5)).statusCode, 400); + const next = await freePort(held); + strictEqual(next.statusCode, 200); + deepStrictEqual(JSON.parse(next.body), { port: held + 2 }); + deepStrictEqual(JSON.parse((await freePort(held + 2)).body), { port: held + 2 }); + } finally { + occupant.close(); + } + }); + it("auto-starts, reports, restarts, and removes configured port forwards", async () => { const directory = await NodeFS.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-coder-gateway-")); tempDirectories.push(directory); diff --git a/apps/coder-gateway/src/server.ts b/apps/coder-gateway/src/server.ts index bcefdb9b7ea96..7f23b190bb67e 100644 --- a/apps/coder-gateway/src/server.ts +++ b/apps/coder-gateway/src/server.ts @@ -198,6 +198,40 @@ async function serveStaticFile( } } +/** Ports past the requested one a server forward tries before reporting that none is free. */ +const MAX_LOCAL_PORT_CANDIDATES = 100; + +/** Whether `port` can be bound on the loopback address `coder port-forward` listens on. */ +function loopbackPortIsFree(port: number): Promise { + return new Promise((resolve) => { + const probe = NodeHttp.createServer(); + probe.once("error", () => resolve(false)); + probe.listen({ port, host: CODER_GATEWAY_HOST, exclusive: true }, () => + probe.close(() => resolve(true)), + ); + }); +} + +/** + * The requested port, or the next one, that no saved TCP forward uses and nothing on this + * computer holds. Null when none of the candidates is free. + */ +async function findFreeForwardPort( + requested: number, + config: CoderProfileConfig, +): Promise { + const saved = new Set( + (config.portForwards ?? []) + .filter((portForward) => portForward.protocol === "tcp") + .map((portForward) => portForward.localPort), + ); + const last = Math.min(65_535, requested + MAX_LOCAL_PORT_CANDIDATES); + for (let port = requested; port <= last; port += 1) { + if (!saved.has(port) && (await loopbackPortIsFree(port))) return port; + } + return null; +} + function readJsonBody(request: NodeHttp.IncomingMessage): Promise { return new Promise((resolve, reject) => { const chunks: Buffer[] = []; @@ -1819,6 +1853,9 @@ export function makeLocalCoderGateway( : state._tag === "Failed" || state._tag === "ShutdownFailed" ? "error" : "starting", + ...(state._tag === "Running" && state.connection.ready?.() === true + ? { ready: true } + : {}), ...(state._tag === "Failed" || state._tag === "ShutdownFailed" ? { error: state.error } : {}), @@ -1924,6 +1961,45 @@ export function makeLocalCoderGateway( } return; } + if (request.method === "POST" && request.url === "/api/port-forwards/free-port") { + if (request.headers.origin !== expectedOrigin) { + sendText(response, 403, "text/plain; charset=utf-8", "Forbidden origin."); + return; + } + if (!request.headers["content-type"]?.startsWith("application/json")) { + sendText(response, 415, "text/plain; charset=utf-8", "JSON content required."); + return; + } + let requested: unknown; + try { + requested = ((await readJsonBody(request)) as { readonly port?: unknown } | null)?.port; + } catch (cause) { + const message = cause instanceof Error ? cause.message : "Invalid request."; + sendText(response, 400, "text/plain; charset=utf-8", message); + return; + } + if ( + typeof requested !== "number" || + !Number.isInteger(requested) || + requested < 1 || + requested > 65_535 + ) { + sendText(response, 400, "text/plain; charset=utf-8", "Invalid port."); + return; + } + const port = await findFreeForwardPort(requested, profileConfig); + if (port === null) { + sendText( + response, + 409, + "text/plain; charset=utf-8", + `No free local port from ${requested} to ${Math.min(65_535, requested + MAX_LOCAL_PORT_CANDIDATES)}.`, + ); + return; + } + sendText(response, 200, "application/json; charset=utf-8", JSON.stringify({ port })); + return; + } const loginRoute = request.url?.match(/^\/api\/deployments\/([^/]+)\/login$/); if (request.method === "POST" && loginRoute !== null && loginRoute !== undefined) { if (request.headers.origin !== expectedOrigin) { diff --git a/apps/server/src/coderEnvironment.ts b/apps/server/src/coderEnvironment.ts index ce59c65c191f6..8cc73c511c93d 100644 --- a/apps/server/src/coderEnvironment.ts +++ b/apps/server/src/coderEnvironment.ts @@ -88,6 +88,7 @@ export const CODER_ENVIRONMENT_CAPABILITIES = { threadPullRequestLinking: true, serverResolvedCommandContext: true, threadStop: true, + localServerStop: true, environmentIcon: true, projectCloneTracking: true, threadProjectFromScratch: true, @@ -116,7 +117,6 @@ export const CODER_OMITTED_CAPABILITIES = { "No Add to another machine: ignored files such as .env would pass through the laptop browser.", mcpModeLimitHeader: "No /mcp HTTP server; the gateway hub carries a linked call's mode limits with the call.", - localServerStop: "No dev-server port discovery: the browser preview is not carried.", triage: "No in-app triage: it clones upstream T3 Code to help file upstream issues.", } satisfies Partial>; diff --git a/apps/server/src/coderServer.ts b/apps/server/src/coderServer.ts index 02eb117c6c825..f1ab3b68d2333 100644 --- a/apps/server/src/coderServer.ts +++ b/apps/server/src/coderServer.ts @@ -67,6 +67,7 @@ import { layerCheckpointStore, layerGitWorkflow, layerIdleCompactionWorker, + layerPortScanner, layerPullRequestService, layerRepositoryIdentityResolver, layerServerSettings, @@ -239,6 +240,9 @@ const CoderRuntimeDependenciesLive = CoderRuntimeCoreLive.pipe( ), Layer.provideMerge(CoderProviderRegistryLive), Layer.provideMerge(CoderProviderInstancesLive), + // Provider hydration builds the Claude runner, which reports agents to the same scanner the + // terminal layer and the RPC handlers share. + Layer.provideMerge(layerPortScanner), Layer.provideMerge(ModelManifest.layerBundled), Layer.provideMerge(SqlitePersistenceLayerLive), // As upstream's runtime: every source control driver is built with an HTTP client, though diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index a35029174b997..665fc9030eab2 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -64,6 +64,7 @@ import { formatClaudeResumeCompactionQuestion } from "@t3tools/shared/claudeComp import * as HostProcess from "@t3tools/shared/HostProcess"; import { getModelSelectionStringOptionValue } from "@t3tools/shared/model"; +import * as PortScanner from "../../preview/PortScanner.ts"; import { attachmentRelativePath } from "../../attachmentStore.ts"; import { WorkflowAgentTranscriptReadObserver } from "../workflowAgentTranscript.ts"; import * as McpProviderSession from "@t3tools/provider-core/server/mcpSession"; @@ -2349,7 +2350,7 @@ describe("ClaudeAdapterV2 MCP credential channel", () => { }, }); }).pipe( - Effect.provide(ClaudeAdapterV2.layerQueryRunner), + Effect.provide(ClaudeAdapterV2.layerQueryRunner.pipe(Layer.provide(PortScanner.layerTest))), Effect.provideService(ProviderEventLoggers.ProviderEventLoggers, { ...ProviderEventLoggers.NoOpProviderEventLoggers, native, diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index f89bd3fbb9722..162946c0a7e6d 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -142,6 +142,7 @@ import { type CompiledClaudeModelSelection, } from "../../claudeModelOptions.ts"; import * as ServerConfig from "../../config.ts"; +import * as PortScanner from "../../preview/PortScanner.ts"; import * as ServerSettings from "../../serverSettings.ts"; import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; import { signalProcessGroup } from "@t3tools/provider-core/server/processGroup"; @@ -869,6 +870,7 @@ export const layerQueryRunner: Layer.Layer< | ChildProcessSpawner.ChildProcessSpawner | Crypto.Crypto | Path.Path + | PortScanner.PortDiscovery | ProviderEventLoggers.ProviderEventLoggers > = Layer.effect( ClaudeAgentSdkQueryRunner, @@ -878,6 +880,8 @@ export const layerQueryRunner: Layer.Layer< const agentScope = yield* AgentScope; const platform = yield* HostProcess.Platform; const runFork = Effect.runForkWith(yield* Effect.context()); + const hostEnvironment = yield* HostProcess.Environment; + const portDiscovery = yield* PortScanner.PortDiscovery; return ClaudeAgentSdkQueryRunner.of({ allocateSessionId: crypto @@ -901,9 +905,15 @@ export const layerQueryRunner: Layer.Layer< Stream.toAsyncIterable, ); // Send MCP credentials over stdin to keep them out of SDK argv and inherited command environments. - // Coder: no dev-server port discovery (#22542) runs here, since the browser preview is not - // carried, so jobs the agent starts get no thread marker and the agent is not tracked. - const { mcpServers, ...queryOptions } = input.options; + const { mcpServers, ...sdkOptions } = input.options; + // Jobs the agent starts inherit the thread, so port discovery can attribute their servers. + const queryOptions = { + ...sdkOptions, + env: { + ...(sdkOptions.env ?? hostEnvironment), + [PortScanner.AGENT_THREAD_ENV]: input.threadId, + }, + }; // The SDK can prepend a scope wrapper to native binaries; script entries run through node. const binaryPath = queryOptions.pathToClaudeCodeExecutable; const launch = @@ -950,6 +960,10 @@ export const layerQueryRunner: Layer.Layer< const spawnClaudeCodeProcess = (spawnOptions: ClaudeSpawnOptions) => { const child = spawnProcess(spawnOptions); spawnedProcess = child; + // A spawn that fails emits only "error". + const releaseAgent = portDiscovery.trackAgent(); + child.once("exit", releaseAgent); + child.on("error", releaseAgent); processExited = child.exitCode !== null || child.signalCode != null; processExit = new Promise((resolve) => { if (processExited) resolve(); diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts index 08b8183a6934b..04021d513776e 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts @@ -21,6 +21,7 @@ import { AgentScope } from "@t3tools/shared/AgentScope"; import { ProviderEventLoggers } from "@t3tools/provider-core/server/ProviderEventLoggers"; import { makeProviderFailure } from "@t3tools/provider-core/server/failure"; import { signalProcessGroup } from "@t3tools/provider-core/server/processGroup"; +import * as PortScanner from "../../preview/PortScanner.ts"; // Coder: the runner queries Claude through the CLI transport's SDK-typed shim, so the test // stubs the shim's query rather than the Agent SDK's. import type * as ClaudeSdk from "@anthropic-ai/claude-agent-sdk"; @@ -36,8 +37,8 @@ vi.mock("../../provider/Drivers/ClaudeAgentSdk.ts", { spy: true }); // Coder: upstream's fork tests copy the transcript through the Agent SDK; the CLI transport // forks natively on the fork's first query (`ClaudeAdapterV2.forkThread`), so they are not carried. -// The runner does no port-discovery agent tracking (#22542), so that test is not carried either. const realQueryRunner = layerQueryRunner.pipe( + Layer.provide(PortScanner.layerTest), Layer.provide( Layer.succeed(ProviderEventLoggers, { native: undefined, @@ -418,4 +419,54 @@ describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("Claude CLI pro yield* Effect.promise(() => turnStopped); }), ); + + it.effect("marks the CLI's jobs with its thread and counts it as an agent until it exits", () => { + let runningAgents = 0; + let releaseAgent!: () => void; + const agentReleased = new Promise((resolve) => (releaseAgent = resolve)); + const runner = layerQueryRunner.pipe( + Layer.provide( + Layer.mock(PortScanner.PortDiscovery)({ + trackAgent: () => { + runningAgents += 1; + return () => { + runningAgents -= 1; + releaseAgent(); + }; + }, + }), + ), + Layer.provide( + Layer.succeed(ProviderEventLoggers, { native: undefined, canonical: undefined }), + ), + Layer.provide(NodeServices.layer), + ); + return Effect.gen(function* () { + const cli = fakeClaudeCli( + [ + 'dir=$(dirname "$0")', + `printf '%s' "$${PortScanner.AGENT_THREAD_ENV}" > "$dir/thread"`, + "cat > /dev/null", + ].join("\n"), + ); + const dir = NodePath.dirname(cli); + NodeChildProcess.execFileSync("mkfifo", [NodePath.join(dir, "thread")]); + yield* Effect.addFinalizer(() => + Effect.sync(() => NodeFS.rmSync(dir, { recursive: true, force: true })), + ); + const threadRead = new Promise((resolve) => { + let thread = ""; + NodeFS.createReadStream(NodePath.join(dir, "thread"), "utf8") + .on("data", (chunk) => (thread += chunk)) + .once("end", () => resolve(thread)); + }); + + const session = yield* openFakeCli(cli); + assert.equal(yield* Effect.promise(() => threadRead), "thread-fake-cli"); + assert.equal(runningAgents, 1); + yield* session.close; + yield* Effect.promise(() => agentReleased); + assert.equal(runningAgents, 0); + }).pipe(Effect.provide(runner)); + }); }); diff --git a/apps/server/src/preview/PortScanner.test.ts b/apps/server/src/preview/PortScanner.test.ts new file mode 100644 index 0000000000000..903e9a7010094 --- /dev/null +++ b/apps/server/src/preview/PortScanner.test.ts @@ -0,0 +1,810 @@ +// @effect-diagnostics nodeBuiltinImport:off -- the stop test drives real server processes. +import * as NodeChildProcess from "node:child_process"; + +import { it as effectIt } from "@effect/vitest"; +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { + CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS, + PREVIEW_URL_MAX_LENGTH, + type DiscoveredLocalServer, +} from "@t3tools/contracts"; +import * as ProcessGroup from "@t3tools/provider-core/server/processGroup"; +import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; +import * as Duration from "effect/Duration"; +import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as PlatformError from "effect/PlatformError"; +import * as Scope from "effect/Scope"; +import * as TestClock from "effect/testing/TestClock"; +import { expect, vi } from "vite-plus/test"; +import { FetchHttpClient } from "effect/http"; + +import * as ProcessRunner from "../processRunner.ts"; +import * as PortScanner from "./PortScanner.ts"; + +vi.mock("@t3tools/provider-core/server/processGroup", { spy: true }); + +const processProbeFailure: ProcessRunner.ProcessRunner["Service"]["run"] = (input) => + Effect.fail( + new ProcessRunner.ProcessSpawnError({ + command: input.command, + argumentCount: input.args.length, + cwd: input.cwd, + cause: PlatformError.systemError({ + _tag: "NotFound", + module: "ChildProcess", + method: "spawn", + description: "PowerShell is not installed in the test environment", + }), + }), + ); + +const layerNoProc = FileSystem.layerNoop({}); + +const layerProbeFailure = ( + run: ProcessRunner.ProcessRunner["Service"]["run"], + fetch: typeof globalThis.fetch = globalThis.fetch, + fileSystem: Layer.Layer = layerNoProc, +) => + PortScanner.layer.pipe( + Layer.provide( + Layer.mergeAll( + fileSystem, + Layer.succeed(ProcessRunner.ProcessRunner, { run }), + Layer.succeed(HostProcess.Platform, "linux"), + FetchHttpClient.layer.pipe(Layer.provide(Layer.succeed(FetchHttpClient.Fetch, fetch))), + ), + ), + ); + +const LSOF_TEST_PORT = 43_123; + +const ownedScanner = (processIds: ReadonlyArray = [1234]) => + Effect.tap(PortScanner.PortDiscovery, (scanner) => + scanner.registerTerminalProcesses({ + threadId: "scanner-thread", + terminalId: "scanner-terminal", + processIds, + }), + ); + +const layerLsofScanner = (input: { + readonly pid: () => number; + readonly fetch: typeof globalThis.fetch; + readonly stdout?: () => string; +}) => + PortScanner.layer.pipe( + Layer.provide( + Layer.mergeAll( + layerNoProc, + Layer.succeed(ProcessRunner.ProcessRunner, { + run: () => + Effect.succeed({ + stdout: input.stdout?.() ?? `p${input.pid()}\ncnode\nn*:${LSOF_TEST_PORT}\n`, + stderr: "", + code: null, + timedOut: false, + stdoutTruncated: false, + stderrTruncated: false, + stdoutInvalidUtf8: false, + stderrInvalidUtf8: false, + }), + }), + Layer.succeed(HostProcess.Platform, "linux"), + FetchHttpClient.layer.pipe( + Layer.provide(Layer.succeed(FetchHttpClient.Fetch, input.fetch)), + ), + ), + ), + ); + +effectIt.effect("keeps a full configured URL when the discovered server root fails", () => { + const requests: string[] = []; + const configuredUrl = `http://localhost:${LSOF_TEST_PORT}/docs`; + const fetchFn = ((input: Parameters[0]) => { + const url = String(input); + requests.push(url); + return Promise.resolve( + url === configuredUrl + ? new Response("docs", { headers: { "content-type": "text/html" } }) + : new Response("not found", { + status: 404, + headers: { "content-type": "text/html" }, + }), + ); + }) as typeof globalThis.fetch; + const layer = layerLsofScanner({ pid: () => 1234, fetch: fetchFn }); + + return Effect.gen(function* () { + const scanner = yield* PortScanner.PortDiscovery; + const servers = yield* scanner.scan([configuredUrl]); + expect(servers).toHaveLength(1); + expect(servers[0]?.url).toBe(configuredUrl); + expect(requests).toContain(configuredUrl); + }).pipe(Effect.provide(layer)); +}); + +effectIt.effect("probes configured custom ports through a canonical loopback host", () => { + const customPort = 43_124; + const configuredUrl = `http://0.0.0.0:${customPort}/docs`; + const expectedUrl = `http://localhost:${customPort}/docs`; + const requests: string[] = []; + const fetchFn = ((input: Parameters[0]) => { + requests.push(String(input)); + return Promise.resolve(new Response("docs", { headers: { "content-type": "text/html" } })); + }) as typeof globalThis.fetch; + const layer = layerProbeFailure(processProbeFailure, fetchFn); + + return Effect.gen(function* () { + const scanner = yield* PortScanner.PortDiscovery; + const servers = yield* scanner.scan([configuredUrl]); + expect(servers).toHaveLength(1); + expect(servers[0]?.host).toBe("localhost"); + expect(servers[0]?.port).toBe(customPort); + expect(servers[0]?.url).toBe(expectedUrl); + expect(requests).toEqual([expectedUrl]); + }).pipe(Effect.provide(layer)); +}); + +effectIt.effect("preserves explicit loopback hosts and bounds wildcard rewrites", () => { + const ipv4Url = "https://127.0.0.1:43125/docs"; + const ipv6Url = "http://[::1]:43126/docs"; + const wildcardPrefix = "http://0.0.0.0/"; + const maximumWildcardUrl = `${wildcardPrefix}${"a".repeat( + PREVIEW_URL_MAX_LENGTH - wildcardPrefix.length, + )}`; + const requests: string[] = []; + const fetchFn = ((input: Parameters[0]) => { + requests.push(String(input)); + return Promise.resolve(new Response("docs", { headers: { "content-type": "text/html" } })); + }) as typeof globalThis.fetch; + const layer = layerProbeFailure(processProbeFailure, fetchFn); + + return Effect.gen(function* () { + const scanner = yield* PortScanner.PortDiscovery; + const servers = yield* scanner.scan([ipv4Url, ipv6Url, maximumWildcardUrl]); + expect(servers.map((server) => server.url)).toEqual([ipv4Url, ipv6Url]); + expect(requests).toEqual([ipv4Url, ipv6Url]); + }).pipe(Effect.provide(layer)); +}); + +effectIt.effect("projects configured paths independently for simultaneous subscribers", () => { + const docsUrl = `http://localhost:${LSOF_TEST_PORT}/docs`; + const adminUrl = `http://localhost:${LSOF_TEST_PORT}/admin`; + const fetchFn = ((input: Parameters[0]) => { + const url = String(input); + return Promise.resolve( + url === docsUrl || url === adminUrl + ? new Response("app", { headers: { "content-type": "text/html" } }) + : new Response("not found", { status: 404 }), + ); + }) as typeof globalThis.fetch; + const layer = layerLsofScanner({ pid: () => 1234, fetch: fetchFn }); + + return Effect.gen(function* () { + const scanner = yield* PortScanner.PortDiscovery; + const docsSnapshots: ReadonlyArray[] = []; + const adminSnapshots: ReadonlyArray[] = []; + yield* scanner.subscribe({ configuredUrls: [docsUrl], initialSnapshot: [] }, (servers) => + Effect.sync(() => docsSnapshots.push(servers)), + ); + yield* scanner.subscribe({ configuredUrls: [adminUrl], initialSnapshot: [] }, (servers) => + Effect.sync(() => adminSnapshots.push(servers)), + ); + yield* scanner.retain; + + expect(docsSnapshots.at(-1)?.[0]?.url).toBe(docsUrl); + expect(adminSnapshots.at(-1)?.[0]?.url).toBe(adminUrl); + }).pipe(Effect.scoped, Effect.provide(layer)); +}); + +effectIt.effect( + "keeps each subscriber's candidates when their combined union exceeds the per-client cap", + () => { + const firstSubscriberUrls = Array.from( + { length: CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS }, + (_, index) => `http://localhost:${LSOF_TEST_PORT}/app-${index}`, + ); + const secondSubscriberUrl = `http://localhost:${LSOF_TEST_PORT}/app-${CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS}`; + const fetchFn = ((input: Parameters[0]) => + Promise.resolve( + String(input) === secondSubscriberUrl + ? new Response("app", { headers: { "content-type": "text/html" } }) + : new Response("not found", { status: 404 }), + )) as typeof globalThis.fetch; + const layer = layerLsofScanner({ pid: () => 1234, fetch: fetchFn }); + + return Effect.gen(function* () { + const scanner = yield* PortScanner.PortDiscovery; + const secondSnapshots: ReadonlyArray[] = []; + yield* scanner.subscribe( + { configuredUrls: firstSubscriberUrls, initialSnapshot: [] }, + () => Effect.void, + ); + yield* scanner.subscribe( + { configuredUrls: [secondSubscriberUrl], initialSnapshot: [] }, + (servers) => Effect.sync(() => secondSnapshots.push(servers)), + ); + yield* scanner.retain; + + expect(secondSnapshots.at(-1)?.[0]?.url).toBe(secondSubscriberUrl); + }).pipe(Effect.scoped, Effect.provide(layer)); + }, +); + +effectIt.effect("stops probing a subscriber's configured paths after its scope closes", () => { + const docsUrl = `http://localhost:${LSOF_TEST_PORT}/docs`; + const adminUrl = `http://localhost:${LSOF_TEST_PORT}/admin`; + const requests: string[] = []; + const fetchFn = ((input: Parameters[0]) => { + const url = String(input); + requests.push(url); + return Promise.resolve( + url === docsUrl || url === adminUrl + ? new Response("app", { headers: { "content-type": "text/html" } }) + : new Response("not found", { status: 404 }), + ); + }) as typeof globalThis.fetch; + const layer = layerLsofScanner({ pid: () => 1234, fetch: fetchFn }); + + return Effect.gen(function* () { + const scanner = yield* PortScanner.PortDiscovery; + const docsScope = yield* Scope.make(); + yield* scanner + .subscribe({ configuredUrls: [docsUrl], initialSnapshot: [] }, () => Effect.void) + .pipe(Effect.provideService(Scope.Scope, docsScope)); + yield* scanner.subscribe( + { configuredUrls: [adminUrl], initialSnapshot: [] }, + () => Effect.void, + ); + yield* scanner.retain; + yield* Scope.close(docsScope, Exit.void); + + requests.length = 0; + yield* TestClock.adjust(Duration.seconds(15)); + expect(requests).toContain(adminUrl); + expect(requests).not.toContain(docsUrl); + }).pipe(Effect.scoped, Effect.provide(layer)); +}); + +effectIt.effect("uses the current configured fragment when readiness comes from cache", () => { + const requests: string[] = []; + const fetchFn = ((input: Parameters[0]) => { + requests.push(String(input)); + return Promise.resolve(new Response("docs", { headers: { "content-type": "text/html" } })); + }) as typeof globalThis.fetch; + const layer = layerLsofScanner({ pid: () => 1234, fetch: fetchFn }); + const oldUrl = `http://localhost:${LSOF_TEST_PORT}/docs#old`; + const newUrl = `http://localhost:${LSOF_TEST_PORT}/docs#new`; + + return Effect.gen(function* () { + const scanner = yield* PortScanner.PortDiscovery; + expect((yield* scanner.scan([oldUrl]))[0]?.url).toBe(oldUrl); + const requestCount = requests.length; + expect((yield* scanner.scan([newUrl]))[0]?.url).toBe(newUrl); + expect(requests).toHaveLength(requestCount); + }).pipe(Effect.provide(layer)); +}); + +effectIt.effect("falls back to HTTPS and does not follow redirects while probing", () => { + const redirects: Array = []; + const fetchFn = (async ( + input: Parameters[0], + init?: Parameters[1], + ) => { + redirects.push(init?.redirect); + if (String(input).startsWith("http:")) throw new TypeError("TLS listener"); + return new Response(null, { status: 302, headers: { location: "https://example.com" } }); + }) as typeof globalThis.fetch; + const layer = layerLsofScanner({ pid: () => 1234, fetch: fetchFn }); + + return Effect.gen(function* () { + const scanner = yield* ownedScanner(); + const servers = yield* scanner.scan(); + expect(servers).toHaveLength(1); + expect(servers[0]?.url).toBe(`https://localhost:${LSOF_TEST_PORT}`); + expect(redirects).toEqual(["manual", "manual"]); + }).pipe(Effect.provide(layer)); +}); + +effectIt.effect("probes a port only while T3 terminals own every listener on it", () => { + const owned = `p1234\ncnode\nn[::1]:${LSOF_TEST_PORT}\n`; + const unowned = `p5678\ncrpc\nn127.0.0.1:${LSOF_TEST_PORT}\n`; + let stdout = unowned; + const requests: string[] = []; + const fetchFn = ((input: Parameters[0]) => { + requests.push(String(input)); + return Promise.resolve(new Response("app", { headers: { "content-type": "text/html" } })); + }) as typeof globalThis.fetch; + const layer = layerLsofScanner({ pid: () => 1234, fetch: fetchFn, stdout: () => stdout }); + + return Effect.gen(function* () { + const scanner = yield* ownedScanner(); + expect(yield* scanner.scan()).toHaveLength(0); + stdout = `${owned}${unowned}`; + expect(yield* scanner.scan()).toHaveLength(0); + expect(requests).toEqual([]); + + stdout = owned; + expect(yield* scanner.scan()).toHaveLength(1); + expect(requests).toEqual([`http://localhost:${LSOF_TEST_PORT}/`]); + + yield* scanner.unregisterTerminal({ + threadId: "scanner-thread", + terminalId: "scanner-terminal", + }); + expect(yield* scanner.scan()).toHaveLength(0); + expect(requests).toHaveLength(1); + }).pipe(Effect.provide(layer)); +}); + +effectIt.effect( + "excludes HTTP errors, non-navigation responses, and successful non-documents", + () => { + let pid = 1; + let makeResponse = () => + new Response("not found", { status: 404, headers: { "content-type": "text/html" } }); + const fetchFn = ((_input: Parameters[0]) => + Promise.resolve(makeResponse())) as typeof globalThis.fetch; + const layer = layerLsofScanner({ pid: () => pid, fetch: fetchFn }); + + return Effect.gen(function* () { + const scanner = yield* ownedScanner([1, 2, 3, 4, 5, 6, 7]); + expect(yield* scanner.scan()).toHaveLength(0); + + pid += 1; + makeResponse = () => + new Response("{}", { status: 200, headers: { "content-type": "application/json" } }); + expect(yield* scanner.scan()).toHaveLength(0); + + pid += 1; + makeResponse = () => + new Response("ready", { status: 200, headers: { "content-type": "text/plain" } }); + expect(yield* scanner.scan()).toHaveLength(0); + + pid += 1; + makeResponse = () => new Response(null, { status: 304, headers: { location: "/cached" } }); + expect(yield* scanner.scan()).toHaveLength(0); + + pid += 1; + makeResponse = () => + new Response(null, { status: 204, headers: { "content-type": "text/html" } }); + expect(yield* scanner.scan()).toHaveLength(0); + + pid += 1; + makeResponse = () => new Response(null, { status: 302 }); + expect(yield* scanner.scan()).toHaveLength(0); + + pid += 1; + makeResponse = () => + new Response("", { + status: 200, + headers: { "content-type": "application/xhtml+xml; charset=utf-8" }, + }); + expect(yield* scanner.scan()).toHaveLength(1); + }).pipe(Effect.provide(layer)); + }, +); + +const PROC_NET_TCP = ` sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode + 0: 0100007F:223D 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 5001 1 0000000000000000 100 0 0 10 0 + 1: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 5002 1 0000000000000000 100 0 0 10 0 + 2: 0100007F:223D 0100007F:C350 01 00000000:00000000 00:00000000 00000000 1000 0 5003 1 0000000000000000 20 4 30 10 -1 + 3: 0A00000F:1F90 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 5005 1 0000000000000000 100 0 0 10 0 +`; +const PROC_NET_TCP6 = ` sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode + 0: 00000000000000000000000000000000:0BB9 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 5004 1 0000000000000000 100 0 0 10 0 +`; + +effectIt.effect( + "reads Linux listeners from /proc when lsof is missing, and stops spawning it", + () => { + let lsofSpawns = 0; + const fdWalks: string[] = []; + const procFiles: Record = { + "/proc/net/tcp": PROC_NET_TCP, + "/proc/net/tcp6": PROC_NET_TCP6, + "/proc/4242/comm": "python3\n", + }; + const procFds: Record> = { + "4242": { "0": "/dev/null", "3": "socket:[5001]" }, + "77": { "5": "socket:[9999]" }, + }; + const missing = FileSystem.makeNoop({}); + const fileSystem = FileSystem.layerNoop({ + readFileString: (path) => { + const content = procFiles[path]; + return content === undefined ? missing.readFileString(path) : Effect.succeed(content); + }, + readDirectory: (path) => { + if (path === "/proc") return Effect.succeed(["self", "77", "4242"]); + const pid = /^\/proc\/(\d+)\/fd$/.exec(path)?.[1]; + const fds = pid === undefined ? undefined : procFds[pid]; + if (pid === undefined || fds === undefined) return missing.readDirectory(path); + fdWalks.push(pid); + return Effect.succeed(Object.keys(fds)); + }, + readLink: (path) => { + const [, pid, fd] = /^\/proc\/(\d+)\/fd\/(\d+)$/.exec(path) ?? []; + const target = pid && fd ? procFds[pid]?.[fd] : undefined; + return target === undefined ? missing.readLink(path) : Effect.succeed(target); + }, + }); + const fetchFn = (() => + Promise.resolve( + new Response("hello", { headers: { "content-type": "text/html" } }), + )) as typeof globalThis.fetch; + const layer = layerProbeFailure( + (input) => { + lsofSpawns += 1; + return processProbeFailure(input); + }, + fetchFn, + fileSystem, + ); + + return Effect.gen(function* () { + const scanner = yield* ownedScanner([4242]); + const first = yield* scanner.scan(); + expect(first.map(({ port, pid, processName }) => ({ port, pid, processName }))).toEqual([ + { port: 8765, pid: 4242, processName: "python3" }, + ]); + yield* scanner.scan(); + expect(lsofSpawns).toBe(1); + expect(fdWalks).toEqual(["77", "4242", "77", "4242"]); + yield* scanner.scan(); + yield* scanner.scan(); + expect(fdWalks).toEqual(["77", "4242", "77", "4242", "77", "4242"]); + }).pipe(Effect.provide(layer)); + }, +); + +effectIt.effect("ignores a partial Windows netstat snapshot when one query exits nonzero", () => { + const fetchFn = (() => + Promise.resolve( + new Response("hello", { headers: { "content-type": "text/html" } }), + )) as typeof globalThis.fetch; + const layer = PortScanner.layer.pipe( + Layer.provide( + Layer.mergeAll( + layerNoProc, + Layer.succeed(ProcessRunner.ProcessRunner, { + run: (input) => { + const isIpv6 = input.args.includes("tcpv6"); + return Effect.succeed({ + stdout: isIpv6 + ? "" + : " TCP 127.0.0.1:5173 0.0.0.0:0 LISTENING 1234\r\n", + stderr: "", + code: isIpv6 ? ChildProcessSpawner.ExitCode(1) : null, + timedOut: false, + stdoutTruncated: false, + stderrTruncated: false, + stdoutInvalidUtf8: false, + stderrInvalidUtf8: false, + }); + }, + }), + Layer.succeed(HostProcess.Platform, "win32"), + FetchHttpClient.layer.pipe(Layer.provide(Layer.succeed(FetchHttpClient.Fetch, fetchFn))), + ), + ), + ); + + return Effect.gen(function* () { + const scanner = yield* PortScanner.PortDiscovery; + yield* scanner.registerTerminalProcesses({ + threadId: "scanner-thread", + terminalId: "scanner-terminal", + processIds: [1234], + }); + expect(yield* scanner.scan()).toEqual([]); + }).pipe(Effect.provide(layer)); +}); + +const processOutput = (stdout: string): ProcessRunner.ProcessRunOutput => ({ + stdout, + stderr: "", + code: ChildProcessSpawner.ExitCode(0), + timedOut: false, + stdoutTruncated: false, + stderrTruncated: false, + stdoutInvalidUtf8: false, + stderrInvalidUtf8: false, +}); + +effectIt.effect("rechecks the socket owner before stopping a Linux server without lsof", () => { + let holder = "4242"; + const missing = FileSystem.makeNoop({}); + const layer = layerProbeFailure( + (input) => + input.command === "lsof" + ? processProbeFailure(input) + : Effect.succeed(processOutput("4242 1 4300\n5000 1 5300\n")), + () => Promise.resolve(new Response("app", { headers: { "content-type": "text/html" } })), + FileSystem.layerNoop({ + readFileString: (path) => { + if (path === "/proc/net/tcp") { + return Effect.succeed(PROC_NET_TCP.split("\n").slice(0, 2).join("\n")); + } + if (/^\/proc\/(4242|5000)\/comm$/.test(path)) return Effect.succeed("node\n"); + if (/^\/proc\/(4242|5000)\/environ$/.test(path)) { + return Effect.succeed(`${PortScanner.AGENT_THREAD_ENV}=thread-agent\0`); + } + return missing.readFileString(path); + }, + readDirectory: (path) => { + if (path === "/proc") return Effect.succeed(["4242", "5000"]); + if (/^\/proc\/(4242|5000)\/fd$/.test(path)) { + return Effect.succeed(path === `/proc/${holder}/fd` ? ["3"] : []); + } + return missing.readDirectory(path); + }, + readLink: (path) => + path === `/proc/${holder}/fd/3` ? Effect.succeed("socket:[5001]") : missing.readLink(path), + }), + ); + + return Effect.gen(function* () { + const signal = vi.spyOn(ProcessGroup, "signalProcessGroup").mockImplementation(() => { + throw Object.assign(new Error("No such process"), { code: "ESRCH" }); + }); + yield* Effect.addFinalizer(() => Effect.sync(() => signal.mockRestore())); + const scanner = yield* PortScanner.PortDiscovery; + expect(yield* scanner.scan()).toMatchObject([{ pid: 4242, port: 8765 }]); + + // The socket outlives its original holder. Its old pid now belongs to another agent job. + holder = "5000"; + const result = yield* scanner + .stopServer({ pid: 4242, port: 8765 }) + .pipe(Effect.match({ onFailure: (error) => error.reason, onSuccess: () => null })); + expect(signal).not.toHaveBeenCalled(); + expect(result).toBe("not-found"); + expect(yield* scanner.scan()).toMatchObject([{ pid: 5000, port: 8765 }]); + yield* scanner.stopServer({ pid: 5000, port: 8765 }); + expect(signal).toHaveBeenCalledExactlyOnceWith(5300, "SIGTERM"); + }).pipe(Effect.provide(layer)); +}); + +effectIt.effect.each(["linux", "darwin"] as const)( + "attributes listeners to the agent thread in their environment on %s, after the agent exits", + (platform) => { + const agentPort = 43_124; + const otherPort = 43_125; + const agentListener = `p4242\ncnode\nn127.0.0.1:${agentPort}\n`; + const otherListener = `p5000\ncpostgres\nn127.0.0.1:${otherPort}\n`; + let lsofStdout = otherListener; + let lsofFails = false; + let environmentFails = false; + let lsofCalls = 0; + let environmentReads = 0; + const requests: string[] = []; + const missing = FileSystem.makeNoop({}); + const procEnviron: Record = { + "/proc/4242/environ": `PATH=/usr/bin\0${PortScanner.AGENT_THREAD_ENV}=thread-agent\0`, + "/proc/5000/environ": "PATH=/usr/bin\0", + }; + const layer = PortScanner.layer.pipe( + Layer.provide( + Layer.mergeAll( + FileSystem.layerNoop({ + readFileString: (path) => { + const content = procEnviron[path]; + if (content === undefined) return missing.readFileString(path); + environmentReads += 1; + return Effect.succeed(content); + }, + }), + Layer.succeed(ProcessRunner.ProcessRunner, { + run: (input) => { + if (input.command === "lsof") { + lsofCalls += 1; + return lsofFails + ? Effect.fail( + new ProcessRunner.ProcessTimeoutError({ + command: input.command, + argumentCount: input.args.length, + cwd: input.cwd, + timeoutMs: 5_000, + }), + ) + : Effect.succeed(processOutput(lsofStdout)); + } + // A direct child of this process, so stop refuses it without signaling anything. + if (input.args[0] === "-A") { + return Effect.succeed(processOutput(`4242 ${process.pid} 4242\n`)); + } + if (environmentFails) { + return Effect.fail( + new ProcessRunner.ProcessTimeoutError({ + command: input.command, + argumentCount: input.args.length, + timeoutMs: 5_000, + }), + ); + } + environmentReads += 1; + return Effect.succeed( + processOutput( + ` 4242 node server.js PATH=/usr/bin ${PortScanner.AGENT_THREAD_ENV}=thread-agent HOME=/Users/dev\n` + + " 5000 postgres -D /data PATH=/usr/bin\n", + ), + ); + }, + }), + Layer.succeed(HostProcess.Platform, platform), + FetchHttpClient.layer.pipe( + Layer.provide( + Layer.succeed(FetchHttpClient.Fetch, (input) => { + requests.push(String(input)); + return Promise.resolve( + new Response("app", { headers: { "content-type": "text/html" } }), + ); + }), + ), + ), + ), + ), + ); + + return Effect.gen(function* () { + const scanner = yield* PortScanner.PortDiscovery; + // Startup reads listeners once. With none marked, later scans stay idle. + expect(yield* scanner.scan()).toEqual([]); + expect(yield* scanner.scan()).toEqual([]); + expect(lsofCalls).toBe(1); + + // The agent exits before any client looks, and the first read fails; its server is still + // found. + lsofStdout = `${agentListener}${otherListener}`; + scanner.trackAgent()(); + lsofFails = true; + expect(yield* scanner.scan()).toEqual([]); + lsofFails = false; + if (platform === "darwin") { + environmentFails = true; + expect(yield* scanner.scan()).toEqual([]); + environmentFails = false; + } + expect(yield* scanner.scan()).toMatchObject([ + { port: agentPort, pid: 4242, terminal: null, agent: { threadId: "thread-agent" } }, + ]); + expect(requests).toEqual([`http://localhost:${agentPort}/`]); + + expect(yield* scanner.scan()).toHaveLength(1); + expect(environmentReads).toBe(2); + + // Stop reads its target's environment again rather than trust the cache, and still finds it. + const stop = yield* Effect.flip(scanner.stopServer({ pid: 4242, port: agentPort })); + expect(stop.reason).toBe("shared-group"); + expect(environmentReads).toBe(3); + + lsofStdout = otherListener; + expect(yield* scanner.scan()).toEqual([]); + const lsofCallsAfterExit = lsofCalls; + expect(yield* scanner.scan()).toEqual([]); + expect(lsofCalls).toBe(lsofCallsAfterExit); + }).pipe(Effect.provide(layer)); + }, +); + +effectIt.effect("stops only a job's own process group", () => + Effect.sync(() => { + const serverPid = 100; + const rows = [ + { pid: 10, ppid: 1, pgid: 10 }, // `vp run dev`, whose job holds this server + { pid: 20, ppid: 10, pgid: 10 }, // the web dev server in the same job + { pid: serverPid, ppid: 10, pgid: 10 }, + { pid: 200, ppid: serverPid, pgid: 200 }, // agent CLI + { pid: 210, ppid: 200, pgid: 200 }, // a server it spawned without a group of its own + { pid: 300, ppid: 200, pgid: 300 }, // agent tool shell + { pid: 400, ppid: 300, pgid: 400 }, // `npm run dev &` + { pid: 401, ppid: 400, pgid: 400 }, + { pid: 500, ppid: serverPid, pgid: 500 }, // terminal shell + { pid: 600, ppid: 500, pgid: 600 }, // terminal job + ]; + const table = new Map(rows.map((row) => [row.pid, row])); + expect(PortScanner.stoppableProcessGroup(table, 401, serverPid)).toBe(400); + expect(PortScanner.stoppableProcessGroup(table, 600, serverPid)).toBe(600); + for (const pid of [20, 210, 500, 999]) { + expect(PortScanner.stoppableProcessGroup(table, pid, serverPid)).toBeNull(); + } + }), +); + +const AGENT_SERVER_SCRIPT = ` +const server = require("node:http").createServer((_, response) => { + response.writeHead(200, { "content-type": "text/html" }); + response.end(""); +}); +server.listen(0, "127.0.0.1", () => { + process.stdout.write(process.pid + " " + server.address().port + "\\n"); +}); +`; + +/** + * A real HTML server with an agent's environment, started the way an agent's tool shell starts a + * job: under a detached shell with job control, in a group of its own. Release kills only the pids + * it started. + */ +const agentServer = () => + Effect.acquireRelease( + Effect.callback<{ + readonly child: NodeChildProcess.ChildProcess; + readonly pid: number; + readonly port: number; + readonly exited: Promise; + }>((resume) => { + const env = { ...process.env, [PortScanner.AGENT_THREAD_ENV]: "thread-live" }; + // dash ignores `set -m` without a terminal; bash, like agent tool shells, honors it. + const child = NodeChildProcess.spawn( + "bash", + ["-c", 'set -m; "$0" -e "$1" & wait', process.execPath, AGENT_SERVER_SCRIPT], + { detached: true, env, stdio: ["ignore", "pipe", "ignore"] }, + ); + const exited = new Promise((resolve) => child.once("exit", () => resolve())); + child.once("error", (error) => resume(Effect.die(error))); + child.stdout?.once("data", (chunk) => { + const [pid, port] = String(chunk).trim().split(" ").map(Number); + resume(Effect.succeed({ child, pid: pid!, port: port!, exited })); + }); + }), + ({ child, pid }) => + Effect.sync(() => { + for (const target of [pid, child.pid]) { + try { + if (target !== undefined) process.kill(target, "SIGKILL"); + } catch {} + } + }), + ); + +const isRunning = (pid: number): boolean => { + try { + process.kill(pid, 0); + return true; + } catch { + return false; + } +}; + +effectIt.live.skipIf(HostProcess.Platform.defaultValue() === "win32")( + "stops an agent's server through its job's group, and only a listed server", + () => + Effect.gen(function* () { + const scanner = yield* PortScanner.PortDiscovery; + yield* Effect.acquireRelease(Effect.sync(scanner.trackAgent), (release) => + Effect.sync(release), + ); + const job = yield* agentServer(); + + expect(yield* scanner.scan()).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + pid: job.pid, + port: job.port, + agent: { threadId: "thread-live" }, + }), + ]), + ); + + const unlisted = yield* Effect.flip(scanner.stopServer({ pid: process.pid, port: job.port })); + expect(unlisted.reason).toBe("not-found"); + + yield* scanner.stopServer({ pid: job.pid, port: job.port }); + // The shell waits on its job, so it exits once the job has. + yield* Effect.promise(() => job.exited); + expect(isRunning(job.pid)).toBe(false); + }).pipe( + Effect.scoped, + Effect.provide( + PortScanner.layer.pipe( + Layer.provide(ProcessRunner.layer), + Layer.provide(Layer.mergeAll(NodeServices.layer, FetchHttpClient.layer)), + ), + ), + ), +); diff --git a/apps/server/src/preview/PortScanner.ts b/apps/server/src/preview/PortScanner.ts new file mode 100644 index 0000000000000..1080624d7508d --- /dev/null +++ b/apps/server/src/preview/PortScanner.ts @@ -0,0 +1,1188 @@ +import { + CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS, + PREVIEW_URL_MAX_LENGTH, + PreviewStopLocalServerError, + ThreadId, + type DiscoveredLocalServer, + type PreviewStopLocalServerInput, +} from "@t3tools/contracts"; +import { signalProcessGroup } from "@t3tools/provider-core/server/processGroup"; +import * as HostProcess from "@t3tools/shared/HostProcess"; +import { isLoopbackHost, LSOF_LOCAL_HOST_TOKENS } from "@t3tools/shared/preview"; +import * as Cause from "effect/Cause"; +import * as Clock from "effect/Clock"; +import * as Context from "effect/Context"; +import * as Duration from "effect/Duration"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as PlatformError from "effect/PlatformError"; +import * as Ref from "effect/Ref"; +import * as Schedule from "effect/Schedule"; +import * as Scope from "effect/Scope"; +import * as Semaphore from "effect/Semaphore"; +import { FetchHttpClient, HttpClient } from "effect/http"; + +import * as ProcessRunner from "../processRunner.ts"; + +export class PortDiscovery extends Context.Service< + PortDiscovery, + { + readonly scan: ( + configuredUrls?: ReadonlyArray, + ) => Effect.Effect>; + readonly subscribe: ( + input: { + readonly configuredUrls: ReadonlyArray; + readonly initialSnapshot: ReadonlyArray; + }, + listener: (servers: ReadonlyArray) => Effect.Effect, + ) => Effect.Effect; + readonly retain: Effect.Effect; + readonly registerTerminalProcesses: (input: { + readonly threadId: string; + readonly terminalId: string; + readonly processIds: ReadonlyArray; + }) => Effect.Effect; + readonly unregisterTerminal: (input: { + readonly threadId: string; + readonly terminalId: string; + }) => Effect.Effect; + /** + * Counts a running agent until the returned function is called. While one runs, any + * listener may be a server it started. Synchronous because provider spawn callbacks run + * outside Effect. + */ + readonly trackAgent: () => () => void; + /** Stops a listed server's process group, never this server, an agent CLI, or a shell. */ + readonly stopServer: ( + input: PreviewStopLocalServerInput, + ) => Effect.Effect; + } +>()("t3/preview/PortScanner/PortDiscovery") {} + +/** + * Agents launch with this set to their thread, and every job they start inherits it, so a + * server stays attributed after its launching shell exits or the agent settles. + */ +export const AGENT_THREAD_ENV = "T3CODE_THREAD_ID"; + +const POLL_INTERVAL = Duration.seconds(3); +const LSOF_TIMEOUT_MS = 5_000; +const PROC_FD_WALK_LIMIT = 50_000; +const SOCKET_OWNER_ATTEMPTS = 3; +const WINDOWS_LISTENER_TIMEOUT_MS = 5_000; +/** `netstat -ano` lists every connection, not just listeners; ~80 bytes per row, sized for ~200k rows. */ +const WINDOWS_NETSTAT_MAX_OUTPUT_BYTES = 16 * 1024 * 1024; +const WEB_PROBE_TIMEOUT = Duration.seconds(1); +const WEB_PROBE_CACHE_TTL_MS = Duration.toMillis(Duration.seconds(15)); +/** Bounds how long a cached Windows image name can outlive its process if Windows reuses the PID. */ +const WINDOWS_PROCESS_NAME_TTL_MS = Duration.toMillis(Duration.seconds(60)); +const WINDOWS_PROCESS_NAME_CONCURRENCY = 4; +const WEB_PROBE_CONCURRENCY = 16; +const NAVIGATION_REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]); +const PS_TIMEOUT_MS = 5_000; +const PS_MAX_OUTPUT_BYTES = 8 * 1024 * 1024; +const STOP_GRACE_MS = 3_000; +const STOP_POLL_INTERVAL = Duration.millis(250); + +type Listener = (servers: ReadonlyArray) => Effect.Effect; + +interface ListenerSubscription { + readonly configuredUrls: ReadonlyArray; + readonly lastSnapshot: ReadonlyArray; +} + +interface ScannerState { + readonly listeners: ReadonlyMap; + readonly terminalProcesses: ReadonlyMap< + string, + { + readonly owner: TerminalProcessOwner; + readonly processIds: ReadonlySet; + } + >; + readonly retainCount: number; +} + +interface TerminalProcessOwner { + readonly threadId: ThreadId; + readonly terminalId: string; +} + +type ServerOwner = Pick; + +const UNOWNED: ServerOwner = { terminal: null, agent: null }; + +const isOwned = (server: DiscoveredLocalServer): boolean => + server.terminal !== null || (server.agent ?? null) !== null; + +interface ProcessTableRow { + readonly pid: number; + readonly ppid: number; + readonly pgid: number; +} + +interface WebProbeCacheEntry { + readonly pid: number | null; + readonly isWeb: boolean; + readonly expiresAtMillis: number; +} + +interface WebProbeGroup { + readonly server: DiscoveredLocalServer; + readonly urls: ReadonlyArray; + readonly configuredKey: string | null; +} + +interface WebProbeSnapshot { + readonly discovered: ReadonlyArray; + readonly configured: ReadonlyMap; +} + +const terminalOwnerKey = (owner: { + readonly threadId: string; + readonly terminalId: string; +}): string => `${owner.threadId}\u0000${owner.terminalId}`; + +const parseConfiguredUrl = (raw: string): URL | null => { + try { + const url = new URL(raw); + if (url.protocol !== "http:" && url.protocol !== "https:") return null; + if (!isLoopbackHost(url.hostname)) return null; + return url; + } catch { + return null; + } +}; + +const localServerKey = (host: string, port: number): string => + `${isLoopbackHost(host) ? "loopback" : host.toLowerCase()}:${port}`; + +const urlPort = (url: URL): number => + url.port.length > 0 ? Number.parseInt(url.port, 10) : url.protocol === "http:" ? 80 : 443; + +const webProbeCacheKey = (raw: string): string => { + const url = new URL(raw); + url.hash = ""; + return url.href; +}; + +const normalizeConfiguredUrls = (urls: ReadonlyArray): ReadonlyArray => [ + ...new Set( + urls + .slice(0, CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS) + .filter((raw) => raw.length <= PREVIEW_URL_MAX_LENGTH) + .map(parseConfiguredUrl) + .filter((url): url is URL => url !== null && url.href.length <= PREVIEW_URL_MAX_LENGTH) + .map((url) => { + if (url.hostname === "0.0.0.0") url.hostname = "localhost"; + return url.href; + }) + .filter((url) => url.length <= PREVIEW_URL_MAX_LENGTH), + ), +]; + +const projectWebProbeSnapshot = ( + snapshot: WebProbeSnapshot, + configuredUrls: ReadonlyArray, +): ReadonlyArray => { + const visibleByServer = new Map(); + for (const raw of normalizeConfiguredUrls(configuredUrls)) { + const url = new URL(raw); + const port = urlPort(url); + const serverKey = localServerKey(url.hostname, port); + if (visibleByServer.has(serverKey)) continue; + const configured = snapshot.configured.get(webProbeCacheKey(raw)); + if (configured) visibleByServer.set(serverKey, { ...configured, url: raw }); + } + for (const server of snapshot.discovered) { + const key = localServerKey(server.host, server.port); + if (!visibleByServer.has(key)) visibleByServer.set(key, server); + } + return [...visibleByServer.values()].toSorted((left, right) => left.port - right.port); +}; + +const parseLsofOutput = ( + raw: string, + owners: ReadonlyMap = new Map(), +): ReadonlyArray => { + const seen = new Map(); + let pid: number | null = null; + let processName: string | null = null; + + for (const line of raw.split("\n")) { + if (line.length === 0) continue; + const tag = line.charAt(0); + const value = line.slice(1); + if (tag === "p") { + const parsed = Number.parseInt(value, 10); + pid = Number.isFinite(parsed) && parsed > 0 ? parsed : null; + processName = null; + continue; + } + if (tag === "c") { + processName = value.trim() || null; + continue; + } + if (tag === "n") { + const portMatch = parsePortFromLsofName(value); + if (portMatch == null) continue; + const url = `http://localhost:${portMatch}`; + const key = `localhost:${portMatch}`; + const owner = pid === null ? undefined : owners.get(pid); + const existing = seen.get(key); + if (existing) { + if (owner === undefined) seen.set(key, { ...existing, ...UNOWNED }); + continue; + } + seen.set(key, { + host: "localhost", + port: portMatch, + url, + processName, + pid, + ...(owner ?? UNOWNED), + }); + } + } + + return Array.from(seen.values()).toSorted((a, b) => a.port - b.port); +}; + +const parsePortFromLsofName = (name: string): number | null => { + const trimmed = name.split(" ", 1)[0]?.trim() ?? ""; + if (trimmed.length === 0) return null; + const lastColon = trimmed.lastIndexOf(":"); + if (lastColon < 0) return null; + const hostPart = trimmed.slice(0, lastColon); + const portPart = trimmed.slice(lastColon + 1); + if (!LSOF_LOCAL_HOST_TOKENS.has(hostPart)) return null; + const port = Number.parseInt(portPart, 10); + if (!Number.isFinite(port) || port <= 0 || port >= 65536) return null; + return port; +}; + +const parseWindowsListenerLine = ( + line: string, +): { + readonly host: string; + readonly port: number; + readonly pid: number | null; + readonly processName: string | null; +} | null => { + const trimmed = line.trim(); + if (trimmed.length === 0) return null; + + if (trimmed.includes("|")) { + const [hostRaw, portRaw, pidRaw, processNameRaw] = trimmed.split("|", 4); + const host = hostRaw?.trim() ?? ""; + if (!LSOF_LOCAL_HOST_TOKENS.has(host) && host !== "::") return null; + const port = Number(portRaw); + const pid = Number(pidRaw); + if (!Number.isInteger(port) || port <= 0 || port >= 65536) return null; + return { + host, + port, + pid: Number.isInteger(pid) && pid > 0 ? pid : null, + processName: processNameRaw?.trim() || null, + }; + } + + // Foreign port `:0` identifies a listening TCP socket regardless of Windows locale. + const fields = trimmed.split(/\s+/); + if (fields.length < 4 || fields[0]?.toUpperCase() !== "TCP") return null; + const localEndpoint = fields[1] ?? ""; + const foreignEndpoint = fields[2] ?? ""; + if (!foreignEndpoint.endsWith(":0")) return null; + const lastColon = localEndpoint.lastIndexOf(":"); + if (lastColon <= 0) return null; + const host = localEndpoint.slice(0, lastColon); + if (!LSOF_LOCAL_HOST_TOKENS.has(host) && host !== "::") return null; + const port = Number(localEndpoint.slice(lastColon + 1)); + if (!Number.isInteger(port) || port <= 0 || port >= 65536) return null; + const pid = Number(fields.at(-1)); + return { + host, + port, + pid: Number.isInteger(pid) && pid > 0 ? pid : null, + processName: null, + }; +}; + +const exitedWithError = (result: ProcessRunner.ProcessRunOutput): boolean => + result.code !== null && result.code !== 0; + +const parseTasklistProcessName = (raw: string, pid: number): string | null => { + for (const line of raw.split(/\r?\n/g)) { + const match = /^"([^"]*)","(\d+)"/.exec(line.trim()); + if (match && Number(match[2]) === pid) return match[1]!.replace(/\.exe$/i, "") || null; + } + return null; +}; + +const parseWindowsListenerOutput = ( + raw: string, + owners: ReadonlyMap = new Map(), +): ReadonlyArray => { + const seen = new Map(); + for (const line of raw.split(/\r?\n/g)) { + const parsed = parseWindowsListenerLine(line); + if (!parsed) continue; + const { port, pid: normalizedPid, processName } = parsed; + const owner = normalizedPid === null ? undefined : owners.get(normalizedPid); + const existing = seen.get(port); + if (existing) { + if (owner === undefined) seen.set(port, { ...existing, ...UNOWNED }); + continue; + } + seen.set(port, { + host: "localhost", + port, + url: `http://localhost:${port}`, + processName, + pid: normalizedPid, + ...(owner ?? UNOWNED), + }); + } + return [...seen.values()].toSorted((left, right) => left.port - right.port); +}; + +const PROC_TCP_LISTEN = "0A"; + +/** Each 32-bit word of the hex address is little-endian. */ +const procLocalAddress = (hex: string): boolean => { + if (hex.length === 8) { + return hex === "00000000" || hex.endsWith("7F"); + } + if (hex.length !== 32) return false; + if (hex === "0".repeat(32) || hex === `${"0".repeat(24)}01000000`) return true; + return hex.startsWith(`${"0".repeat(16)}FFFF0000`) && procLocalAddress(hex.slice(24)); +}; + +const parseProcNetTcp = ( + raw: string, +): ReadonlyArray<{ readonly port: number; readonly inode: string }> => { + const listeners: Array<{ port: number; inode: string }> = []; + for (const line of raw.split("\n").slice(1)) { + const fields = line.trim().split(/\s+/); + const [address, portHex] = fields[1]?.split(":") ?? []; + if (fields[3] !== PROC_TCP_LISTEN || !address || !portHex) continue; + if (!procLocalAddress(address.toUpperCase())) continue; + const port = Number.parseInt(portHex, 16); + const inode = fields[9]; + if (!Number.isInteger(port) || port <= 0 || port >= 65536 || !inode) continue; + listeners.push({ port, inode }); + } + return listeners; +}; + +/** Processes holding a loopback or wildcard listener in `lsof -F pcn` output. */ +const lsofProcessIds = (raw: string): ReadonlyArray => { + const pids = new Set(); + let pid: number | null = null; + for (const line of raw.split("\n")) { + if (line.startsWith("p")) { + const parsed = Number.parseInt(line.slice(1), 10); + pid = Number.isFinite(parsed) && parsed > 0 ? parsed : null; + } else if ( + line.startsWith("n") && + pid !== null && + parsePortFromLsofName(line.slice(1)) !== null + ) { + pids.add(pid); + } + } + return [...pids]; +}; + +const AGENT_THREAD_ASSIGNMENT = new RegExp(`(?:^|\\s)${AGENT_THREAD_ENV}=(\\S+)`); + +/** Reads `/proc//environ`, whose entries are NUL-separated. */ +const parseProcEnvironThread = (raw: string): ThreadId | null => { + const prefix = `${AGENT_THREAD_ENV}=`; + const value = raw + .split("\0") + .find((entry) => entry.startsWith(prefix)) + ?.slice(prefix.length) + .trim(); + return value ? ThreadId.make(value) : null; +}; + +/** + * Reads macOS `ps -E -o pid=,command=`, which appends each process's launch environment to its + * argv with spaces, so an argument that spells the assignment out also matches. + */ +const parsePsEnvironmentThreads = (raw: string): ReadonlyMap => { + const threads = new Map(); + for (const line of raw.split("\n")) { + const match = /^\s*(\d+)\s(.*)$/.exec(line); + if (!match) continue; + const value = AGENT_THREAD_ASSIGNMENT.exec(match[2]!)?.[1]; + threads.set(Number(match[1]), value ? ThreadId.make(value) : null); + } + return threads; +}; + +const parseProcessTable = (raw: string): ReadonlyMap => { + const rows = new Map(); + for (const line of raw.split("\n")) { + const [pid, ppid, pgid] = line.trim().split(/\s+/).map(Number); + if (Number.isInteger(pid) && Number.isInteger(ppid) && Number.isInteger(pgid)) { + rows.set(pid!, { pid: pid!, ppid: ppid!, pgid: pgid! }); + } + } + return rows; +}; + +/** + * The process group stopping `pid` signals. Shells with job control and agent tool shells give + * every job its own group, so the group is the job. Null when the group also holds this server, + * a direct child of it (agent CLIs and terminal shells), or one of its ancestors. + */ +export const stoppableProcessGroup = ( + table: ReadonlyMap, + pid: number, + serverPid: number, +): number | null => { + const group = table.get(pid)?.pgid; + if (group === undefined || group <= 1) return null; + const guarded = new Set([serverPid]); + for ( + let parent = table.get(serverPid)?.ppid; + parent !== undefined && parent > 1 && !guarded.has(parent); + parent = table.get(parent)?.ppid + ) { + guarded.add(parent); + } + for (const row of table.values()) { + if (row.pgid === group && (guarded.has(row.pid) || row.ppid === serverPid)) return null; + } + return group; +}; + +const serversEqual = ( + left: ReadonlyArray, + right: ReadonlyArray, +): boolean => { + if (left.length !== right.length) return false; + for (let i = 0; i < left.length; i += 1) { + const a = left[i]; + const b = right[i]; + if (!a || !b) return false; + if ( + a.host !== b.host || + a.port !== b.port || + a.url !== b.url || + a.processName !== b.processName || + a.pid !== b.pid || + a.terminal?.threadId !== b.terminal?.threadId || + a.terminal?.terminalId !== b.terminal?.terminalId || + a.agent?.threadId !== b.agent?.threadId + ) { + return false; + } + } + return true; +}; + +const isNoSuchProcess = (cause: unknown): boolean => + typeof cause === "object" && cause !== null && "code" in cause && cause.code === "ESRCH"; + +const isCommandNotFound = (error: ProcessRunner.ProcessSpawnError): boolean => + PlatformError.isPlatformError(error.cause) && error.cause.reason._tag === "NotFound"; + +/** @public */ +export const make = Effect.gen(function* PortDiscoveryMake() { + const processRunner = yield* ProcessRunner.ProcessRunner; + const fileSystem = yield* FileSystem.FileSystem; + const hostPlatform = yield* HostProcess.Platform; + const httpClient = (yield* HttpClient.HttpClient).pipe(HttpClient.withScope); + const stateRef = yield* Ref.make({ + listeners: new Map(), + terminalProcesses: new Map(), + retainCount: 0, + }); + const webProbeCacheRef = yield* Ref.make>(new Map()); + const scanSemaphore = yield* Semaphore.make(1); + const lsofMissingRef = yield* Ref.make(false); + const windowsProcessNamesRef = yield* Ref.make< + ReadonlyMap + >(new Map()); + const socketOwnersRef = yield* Ref.make< + ReadonlyMap + >(new Map()); + const socketOwnerMissesRef = yield* Ref.make>(new Map()); + // Each current listener pid's agent thread, or null without one. A process keeps its launch + // environment, so a pid is read once while it listens. Cleared while no client retains the + // scanner, since unscanned pids can be reused. + const agentThreadsRef = yield* Ref.make>(new Map()); + let runningAgents = 0; + // An agent that exited, or a cache cleared with a marked listener, owes one listener read: the + // servers it left may still run. So does startup, for agents that ran before a restart. + let agentScanPending = true; + + const readAgentThreads = Effect.fn("PortDiscovery.readAgentThreads")(function* ( + pids: ReadonlyArray, + ) { + const cached = yield* Ref.get(agentThreadsRef); + const unread = pids.filter((pid) => !cached.has(pid)); + const read: ReadonlyMap = + unread.length === 0 + ? new Map() + : hostPlatform === "linux" + ? new Map( + yield* Effect.forEach( + unread, + (pid) => + fileSystem.readFileString(`/proc/${pid}/environ`).pipe( + Effect.map(parseProcEnvironThread), + Effect.orElseSucceed(() => null), + Effect.map((threadId) => [pid, threadId] as const), + ), + { concurrency: 16 }, + ), + ) + : yield* processRunner + .run({ + command: "ps", + args: ["-E", "-ww", "-o", "pid=,command=", "-p", unread.join(",")], + timeout: Duration.millis(PS_TIMEOUT_MS), + maxOutputBytes: PS_MAX_OUTPUT_BYTES, + }) + .pipe( + Effect.map((result) => parsePsEnvironmentThreads(result.stdout)), + Effect.orElseSucceed(() => { + agentScanPending = true; + return new Map(); + }), + ); + const next = new Map(); + for (const pid of pids) { + const threadId = cached.has(pid) ? cached.get(pid) : read.get(pid); + if (threadId !== undefined) next.set(pid, threadId); + } + yield* Ref.set(agentThreadsRef, next); + return next; + }); + + const withAgentOwners = Effect.fn("PortDiscovery.withAgentOwners")(function* ( + terminalOwners: ReadonlyMap, + pids: ReadonlyArray, + ) { + // Runs only once a listener snapshot exists, so a failed read leaves the scan pending. + agentScanPending = false; + const threads = yield* readAgentThreads(pids.filter((pid) => !terminalOwners.has(pid))); + const owners = new Map(terminalOwners); + for (const [pid, threadId] of threads) { + if (threadId !== null) owners.set(pid, { terminal: null, agent: { threadId } }); + } + return owners; + }); + + const findSocketOwners = Effect.fn("PortDiscovery.findSocketOwners")(function* ( + inodes: ReadonlySet, + ) { + const owners = new Map(); + const pids = (yield* fileSystem + .readDirectory("/proc") + .pipe(Effect.orElseSucceed((): ReadonlyArray => []))).filter((entry) => + /^\d+$/.test(entry), + ); + let budget = PROC_FD_WALK_LIMIT; + for (const pidText of pids) { + if (owners.size === inodes.size || budget <= 0) break; + const fds = yield* fileSystem + .readDirectory(`/proc/${pidText}/fd`) + .pipe(Effect.orElseSucceed((): ReadonlyArray => [])); + const walked = fds.slice(0, budget); + budget -= walked.length; + const links = yield* Effect.forEach( + walked, + (fd) => + fileSystem.readLink(`/proc/${pidText}/fd/${fd}`).pipe(Effect.orElseSucceed(() => "")), + { concurrency: 16 }, + ); + const held = links.flatMap((link) => /^socket:\[(\d+)\]$/.exec(link)?.[1] ?? []); + const matched = held.filter((inode) => inodes.has(inode) && !owners.has(inode)); + if (matched.length === 0) continue; + const processName = yield* fileSystem.readFileString(`/proc/${pidText}/comm`).pipe( + Effect.map((name) => name.trim() || null), + Effect.orElseSucceed(() => null), + ); + for (const inode of matched) owners.set(inode, { pid: Number(pidText), processName }); + } + return owners; + }); + + const scanProcListeners = Effect.fn("PortDiscovery.scanProcListeners")(function* ( + resolveOwners: (pids: ReadonlyArray) => Effect.Effect>, + ) { + if (hostPlatform !== "linux") return null; + const tables = yield* Effect.forEach(["/proc/net/tcp", "/proc/net/tcp6"], (path) => + fileSystem.readFileString(path).pipe(Effect.option), + ); + if (tables.every(Option.isNone)) return null; + const listeners = tables.flatMap((table) => + Option.isSome(table) ? parseProcNetTcp(table.value) : [], + ); + const known = yield* Ref.get(socketOwnersRef); + const knownMisses = yield* Ref.get(socketOwnerMissesRef); + const inodes = new Set(listeners.map((listener) => listener.inode)); + // Walk fds only for unowned sockets; stop after repeated misses for inaccessible or exited processes. + const unseen = new Set( + [...inodes].filter( + (inode) => !known.has(inode) && (knownMisses.get(inode) ?? 0) < SOCKET_OWNER_ATTEMPTS, + ), + ); + const found = unseen.size === 0 ? new Map() : yield* findSocketOwners(unseen); + const owners = new Map([...known, ...found].filter(([inode]) => inodes.has(inode))); + const misses = new Map([...knownMisses].filter(([inode]) => inodes.has(inode))); + for (const inode of unseen) { + if (!found.has(inode)) misses.set(inode, (misses.get(inode) ?? 0) + 1); + } + yield* Ref.set(socketOwnersRef, owners); + yield* Ref.set(socketOwnerMissesRef, misses); + const serverOwners = yield* resolveOwners([ + ...new Set([...owners.values()].map(({ pid }) => pid)), + ]); + const seen = new Map(); + for (const { port, inode } of listeners) { + const socketOwner = owners.get(inode) ?? null; + const owner = socketOwner === null ? undefined : serverOwners.get(socketOwner.pid); + const existing = seen.get(port); + if (existing && (existing.pid !== null || socketOwner === null)) { + if (owner === undefined) seen.set(port, { ...existing, ...UNOWNED }); + continue; + } + seen.set(port, { + host: "localhost", + port, + url: `http://localhost:${port}`, + processName: socketOwner?.processName ?? null, + pid: socketOwner?.pid ?? null, + ...(existing ? UNOWNED : (owner ?? UNOWNED)), + }); + } + return [...seen.values()].toSorted((left, right) => left.port - right.port); + }); + + const probeWebUrl = Effect.fn("PortDiscovery.probeWebUrl")((url: string) => + httpClient.get(url).pipe( + Effect.map((response) => { + const location = response.headers.location?.trim(); + if (NAVIGATION_REDIRECT_STATUSES.has(response.status) && location) return url; + if (response.status < 200 || response.status >= 300) return null; + if (response.status === 204 || response.status === 205) return null; + const contentType = response.headers["content-type"] + ?.split(";", 1)[0] + ?.trim() + .toLowerCase(); + return contentType === "text/html" || contentType === "application/xhtml+xml" ? url : null; + }), + Effect.scoped, + Effect.timeoutOption(WEB_PROBE_TIMEOUT), + Effect.map(Option.getOrNull), + Effect.orElseSucceed(() => null), + Effect.provideService(FetchHttpClient.RequestInit, { redirect: "manual" }), + ), + ); + + const makeWebProbeGroups = ( + servers: ReadonlyArray, + configuredUrls: ReadonlyArray, + ): ReadonlyArray => { + const serversByKey = new Map( + servers.map((server) => [localServerKey(server.host, server.port), server] as const), + ); + const groups: WebProbeGroup[] = []; + const configuredResources = new Set(); + + for (const raw of configuredUrls) { + const url = new URL(raw); + const port = urlPort(url); + const key = localServerKey(url.hostname, port); + const resourceKey = webProbeCacheKey(raw); + if (configuredResources.has(resourceKey)) continue; + configuredResources.add(resourceKey); + groups.push({ + server: serversByKey.get(key) ?? { + host: url.hostname, + port, + url: raw, + processName: null, + pid: null, + terminal: null, + }, + urls: [raw], + configuredKey: resourceKey, + }); + } + + for (const server of servers) { + // Never probe an unidentified listener with HTTP or TLS; binary RPC servers can crash on it. + if (!isOwned(server)) continue; + groups.push({ + server, + urls: [`http://${server.host}:${server.port}`, `https://${server.host}:${server.port}`], + configuredKey: null, + }); + } + + return groups; + }; + + const probeWebServers = Effect.fn("PortDiscovery.probeWebServers")(function* ( + servers: ReadonlyArray, + configuredUrls: ReadonlyArray, + ) { + const nowMillis = yield* Clock.currentTimeMillis; + const cached = yield* Ref.get(webProbeCacheRef); + const groups = makeWebProbeGroups(servers, configuredUrls); + const batchProbes = new Map< + string, + Effect.Effect<{ readonly probe: WebProbeCacheEntry; readonly fresh: boolean }> + >(); + const batchProbeSemaphore = yield* Semaphore.make(1); + const getProbe = (url: string, pid: number | null) => { + const key = webProbeCacheKey(url); + const identity = `${key}\u0000${pid ?? ""}`; + return batchProbeSemaphore + .withPermits(1)( + Effect.gen(function* () { + const existing = batchProbes.get(identity); + if (existing) return [existing] as const; + const cachedProbe = cached.get(key); + const cachedIsCurrent = + cachedProbe?.pid === pid && cachedProbe.expiresAtMillis > nowMillis; + const memoized = yield* Effect.cached( + cachedIsCurrent + ? Effect.succeed({ probe: cachedProbe, fresh: false }) + : probeWebUrl(url).pipe( + Effect.map((result) => ({ + probe: { pid, isWeb: result !== null, expiresAtMillis: 0 }, + fresh: true, + })), + ), + ); + batchProbes.set(identity, memoized); + return [memoized] as const; + }), + ) + .pipe(Effect.flatMap(([probe]) => probe)); + }; + const probed = yield* Effect.forEach( + groups, + (group) => + Effect.gen(function* () { + const probes: Array = []; + let visibleUrl: string | null = null; + for (const url of group.urls) { + const key = webProbeCacheKey(url); + const { probe, fresh } = yield* getProbe(url, group.server.pid); + probes.push([key, probe, fresh]); + if (probe.isWeb) { + visibleUrl = url; + break; + } + } + return { group, probes, visibleUrl }; + }), + { concurrency: WEB_PROBE_CONCURRENCY }, + ); + const completedAtMillis = yield* Clock.currentTimeMillis; + const nextCache = new Map( + [...cached].filter(([, probe]) => probe.expiresAtMillis > completedAtMillis), + ); + const discovered: DiscoveredLocalServer[] = []; + const configured = new Map(); + for (const { group, probes, visibleUrl } of probed) { + for (const [key, probe, fresh] of probes) { + nextCache.set( + key, + fresh ? { ...probe, expiresAtMillis: completedAtMillis + WEB_PROBE_CACHE_TTL_MS } : probe, + ); + } + if (visibleUrl === null) continue; + const server = { ...group.server, url: visibleUrl }; + if (group.configuredKey === null) discovered.push(server); + else configured.set(group.configuredKey, server); + } + yield* Ref.set(webProbeCacheRef, nextCache); + return { discovered, configured } satisfies WebProbeSnapshot; + }); + + // tasklist costs 200-300ms per PID; query visible servers with bounded concurrency and cache names. + const withWindowsProcessNames = Effect.fn("PortDiscovery.withWindowsProcessNames")(function* ( + snapshot: WebProbeSnapshot, + ) { + const servers = [...snapshot.discovered, ...snapshot.configured.values()]; + const pids = new Set(servers.flatMap((server) => (server.pid === null ? [] : [server.pid]))); + const nowMillis = yield* Clock.currentTimeMillis; + const cached = [...(yield* Ref.get(windowsProcessNamesRef))].filter( + ([pid, entry]) => pids.has(pid) && entry.expiresAtMillis > nowMillis, + ); + const cachedPids = new Set(cached.map(([pid]) => pid)); + const expiresAtMillis = nowMillis + WINDOWS_PROCESS_NAME_TTL_MS; + const fetched = yield* Effect.forEach( + [...pids].filter((pid) => !cachedPids.has(pid)), + (pid) => + processRunner + .run({ + command: "tasklist.exe", + args: ["/FO", "CSV", "/NH", "/FI", `PID eq ${pid}`], + timeout: Duration.millis(WINDOWS_LISTENER_TIMEOUT_MS), + maxOutputBytes: 64 * 1024, + }) + .pipe( + Effect.map((result) => + exitedWithError(result) + ? [] + : [ + [ + pid, + { name: parseTasklistProcessName(result.stdout, pid), expiresAtMillis }, + ] as const, + ], + ), + Effect.orElseSucceed(() => []), + ), + { concurrency: WINDOWS_PROCESS_NAME_CONCURRENCY }, + ); + const names = new Map([...cached, ...fetched.flat()]); + yield* Ref.set(windowsProcessNamesRef, names); + const withName = (server: DiscoveredLocalServer): DiscoveredLocalServer => + server.pid === null || server.processName !== null + ? server + : { ...server, processName: names.get(server.pid)?.name ?? null }; + return { + discovered: snapshot.discovered.map(withName), + configured: new Map([...snapshot.configured].map(([key, server]) => [key, withName(server)])), + } satisfies WebProbeSnapshot; + }); + + const recoverProcessProbeFailure = + (probe: "lsof" | "windows-listeners") => (error: ProcessRunner.ProcessRunError) => + Effect.logDebug("preview port process probe failed; probing configured URLs only", { + cause: error, + probe, + platform: hostPlatform, + }).pipe(Effect.as(null)); + + const scanUnlocked = Effect.fn("PortDiscovery.scanUnlocked")(function* ( + configuredUrls: ReadonlyArray, + ) { + const state = yield* Ref.get(stateRef); + const terminalOwners = new Map(); + for (const registration of state.terminalProcesses.values()) { + for (const processId of registration.processIds) { + terminalOwners.set(processId, { terminal: registration.owner, agent: null }); + } + } + // A server an agent started can outlive the agent, so listeners are read while the last scan + // still found one. + const agentsPossible = + hostPlatform !== "win32" && + (runningAgents > 0 || + agentScanPending || + [...(yield* Ref.get(agentThreadsRef)).values()].some((threadId) => threadId !== null)); + if (terminalOwners.size === 0 && configuredUrls.length === 0 && !agentsPossible) { + return { discovered: [], configured: new Map() } satisfies WebProbeSnapshot; + } + const resolveOwners = (pids: ReadonlyArray) => + agentsPossible ? withAgentOwners(terminalOwners, pids) : Effect.succeed(terminalOwners); + if (hostPlatform === "win32") { + const recoverWindowsProbeFailure = recoverProcessProbeFailure("windows-listeners"); + const runNetstat = (protocol: "tcp" | "tcpv6") => + processRunner.run({ + command: "netstat.exe", + args: ["-ano", "-p", protocol], + timeout: Duration.millis(WINDOWS_LISTENER_TIMEOUT_MS), + maxOutputBytes: WINDOWS_NETSTAT_MAX_OUTPUT_BYTES, + }); + const listeners = yield* Effect.all([runNetstat("tcp"), runNetstat("tcpv6")], { + concurrency: 2, + }).pipe( + // Failed queries can hide other IPv4/IPv6 listeners on the port; fall back to configured URLs. + Effect.map(([tcp4, tcp6]) => + exitedWithError(tcp4) || exitedWithError(tcp6) + ? null + : parseWindowsListenerOutput(`${tcp4.stdout}\n${tcp6.stdout}`, terminalOwners), + ), + Effect.catchTags({ + ProcessSpawnError: recoverWindowsProbeFailure, + ProcessStdinError: recoverWindowsProbeFailure, + ProcessOutputLimitError: recoverWindowsProbeFailure, + ProcessReadError: recoverWindowsProbeFailure, + ProcessTimeoutError: recoverWindowsProbeFailure, + }), + ); + return yield* withWindowsProcessNames( + yield* probeWebServers(listeners ?? [], configuredUrls), + ); + } + const recoverLsofProbeFailure = recoverProcessProbeFailure("lsof"); + if (yield* Ref.get(lsofMissingRef)) { + const fromProc = yield* scanProcListeners(resolveOwners); + return yield* probeWebServers(fromProc ?? [], configuredUrls); + } + const lsofResult = yield* processRunner + .run({ + command: "lsof", + args: ["-iTCP", "-sTCP:LISTEN", "-P", "-n", "-F", "pcn"], + timeout: Duration.millis(LSOF_TIMEOUT_MS), + // Partial output could hide another app's listener on an owned port. + maxOutputBytes: 1024 * 1024, + }) + .pipe( + Effect.flatMap((result) => + resolveOwners(lsofProcessIds(result.stdout)).pipe( + Effect.map((owners) => parseLsofOutput(result.stdout, owners)), + ), + ), + Effect.catchTags({ + ProcessSpawnError: (error) => + (isCommandNotFound(error) ? Ref.set(lsofMissingRef, true) : Effect.void).pipe( + Effect.andThen(recoverLsofProbeFailure(error)), + ), + ProcessStdinError: recoverLsofProbeFailure, + ProcessOutputLimitError: recoverLsofProbeFailure, + ProcessReadError: recoverLsofProbeFailure, + ProcessTimeoutError: recoverLsofProbeFailure, + }), + ); + if (lsofResult !== null) return yield* probeWebServers(lsofResult, configuredUrls); + const fromProc = yield* scanProcListeners(resolveOwners); + return yield* probeWebServers(fromProc ?? [], configuredUrls); + }); + + const scanSnapshot = Effect.fn("PortDiscovery.scanSnapshot")( + (configuredUrls: ReadonlyArray) => + scanSemaphore.withPermits(1)(scanUnlocked(configuredUrls)), + ); + + const scanOnce: PortDiscovery["Service"]["scan"] = (configuredUrls = []) => { + const normalized = normalizeConfiguredUrls(configuredUrls); + return scanSnapshot(normalized).pipe( + Effect.map((snapshot) => projectWebProbeSnapshot(snapshot, normalized)), + ); + }; + + const pollTick = Effect.fn("PortDiscovery.pollTick")( + function* () { + const configuredUrls = [ + ...new Set( + [...(yield* Ref.get(stateRef)).listeners.values()].flatMap( + (subscription) => subscription.configuredUrls, + ), + ), + ]; + const snapshot = yield* scanSnapshot(configuredUrls); + const notifications = yield* Ref.modify(stateRef, (state) => { + const listeners = new Map(state.listeners); + const changed: Array]> = []; + for (const [listener, subscription] of listeners) { + const next = projectWebProbeSnapshot(snapshot, subscription.configuredUrls); + if (serversEqual(subscription.lastSnapshot, next)) continue; + listeners.set(listener, { ...subscription, lastSnapshot: next }); + changed.push([listener, next]); + } + return [changed, { ...state, listeners }]; + }); + yield* Effect.forEach(notifications, ([listener, servers]) => listener(servers), { + discard: true, + }); + }, + Effect.catchCause((cause: Cause.Cause) => + Effect.logWarning("preview port scan failed", Cause.pretty(cause)), + ), + ); + + const pollIfRetained = Ref.get(stateRef).pipe( + Effect.flatMap((state) => (state.retainCount > 0 ? pollTick() : Effect.void)), + ); + yield* Effect.forkScoped(pollIfRetained.pipe(Effect.repeat(Schedule.spaced(POLL_INTERVAL)))); + + const acquireRetention = Effect.fn("PortDiscovery.retain")(function* () { + const wasIdle = yield* Ref.modify(stateRef, (state) => [ + state.retainCount === 0, + { ...state, retainCount: state.retainCount + 1 }, + ]); + if (wasIdle) { + yield* pollTick(); + } + }); + + const releaseRetention = Effect.gen(function* () { + const idle = yield* Ref.modify(stateRef, (state) => { + const retainCount = Math.max(0, state.retainCount - 1); + return [retainCount === 0, { ...state, retainCount }]; + }); + if (!idle) return; + const threads = yield* Ref.getAndSet(agentThreadsRef, new Map()); + if ([...threads.values()].some((threadId) => threadId !== null)) agentScanPending = true; + }); + + const retain: PortDiscovery["Service"]["retain"] = Effect.acquireRelease( + acquireRetention(), + () => releaseRetention, + ); + + const subscribe: PortDiscovery["Service"]["subscribe"] = Effect.fn("PortDiscovery.subscribe")( + (input, listener) => + Effect.acquireRelease( + Ref.update(stateRef, (state) => { + const listeners = new Map(state.listeners); + listeners.set(listener, { + configuredUrls: normalizeConfiguredUrls(input.configuredUrls), + lastSnapshot: input.initialSnapshot, + }); + return { ...state, listeners }; + }), + () => + Ref.update(stateRef, (state) => { + const listeners = new Map(state.listeners); + listeners.delete(listener); + return { ...state, listeners }; + }), + ), + ); + + const registerTerminalProcesses: PortDiscovery["Service"]["registerTerminalProcesses"] = + Effect.fn("PortDiscovery.registerTerminalProcesses")(function* (input) { + const owner = { + threadId: ThreadId.make(input.threadId), + terminalId: input.terminalId, + }; + const processIds = new Set( + input.processIds.filter((processId) => Number.isInteger(processId) && processId > 0), + ); + yield* Ref.update(stateRef, (state) => { + const terminalProcesses = new Map(state.terminalProcesses); + const key = terminalOwnerKey(owner); + if (processIds.size === 0) { + terminalProcesses.delete(key); + } else { + terminalProcesses.set(key, { owner, processIds }); + } + return { ...state, terminalProcesses }; + }); + }); + + const unregisterTerminal: PortDiscovery["Service"]["unregisterTerminal"] = Effect.fn( + "PortDiscovery.unregisterTerminal", + )(function* (input) { + yield* Ref.update(stateRef, (state) => { + const terminalProcesses = new Map(state.terminalProcesses); + terminalProcesses.delete(terminalOwnerKey(input)); + return { ...state, terminalProcesses }; + }); + }); + + const trackAgent: PortDiscovery["Service"]["trackAgent"] = () => { + runningAgents += 1; + let released = false; + return () => { + if (released) return; + released = true; + runningAgents -= 1; + agentScanPending = true; + }; + }; + + // Fails when `ps` does, so an empty table never reads as every process having exited. + const readProcessTable = Effect.suspend(() => + processRunner.run({ + command: "ps", + args: ["-A", "-o", "pid=,ppid=,pgid="], + timeout: Duration.millis(PS_TIMEOUT_MS), + maxOutputBytes: PS_MAX_OUTPUT_BYTES, + }), + ).pipe( + Effect.mapError((cause) => new PreviewStopLocalServerError({ reason: "signal-failed", cause })), + Effect.filterOrFail( + (result) => !exitedWithError(result), + () => new PreviewStopLocalServerError({ reason: "signal-failed" }), + ), + Effect.map((result) => parseProcessTable(result.stdout)), + ); + + const signalUntilStopped = Effect.fn("PortDiscovery.signalUntilStopped")(function* ( + input: PreviewStopLocalServerInput, + ) { + if (hostPlatform === "win32") { + return yield* new PreviewStopLocalServerError({ reason: "unsupported" }); + } + // A socket can outlive its cached owner. Wait for any in-flight scan before invalidating + // that owner, so the /proc fallback confirms who holds it before a signal is sent. + yield* scanSemaphore.withPermits(1)( + Ref.update( + socketOwnersRef, + (owners) => new Map([...owners].filter(([, owner]) => owner.pid !== input.pid)), + ), + ); + // The pid comes from a client, so only a server this scan attributes to a thread is signaled, + // from its environment as read now. + yield* Ref.update(agentThreadsRef, (threads) => { + const next = new Map(threads); + next.delete(input.pid); + return next; + }); + agentScanPending = true; + const listed = (yield* scanOnce()).some( + (server) => server.pid === input.pid && server.port === input.port && isOwned(server), + ); + if (!listed) return yield* new PreviewStopLocalServerError({ reason: "not-found" }); + const table = yield* readProcessTable; + // It exited after the scan, so there is nothing left to stop. + if (!table.has(input.pid)) return; + const group = stoppableProcessGroup(table, input.pid, process.pid); + if (group === null) return yield* new PreviewStopLocalServerError({ reason: "shared-group" }); + // False when the whole group already exited. + const signalGroup = (signal: "SIGTERM" | "SIGKILL") => + Effect.suspend(() => { + try { + signalProcessGroup(group, signal); + return Effect.succeed(true); + } catch (cause) { + return isNoSuchProcess(cause) + ? Effect.succeed(false) + : Effect.fail(new PreviewStopLocalServerError({ reason: "signal-failed", cause })); + } + }); + if (!(yield* signalGroup("SIGTERM"))) return; + // While one of the group's original members is still in it, the group has not been reused, + // so SIGKILL goes only to the job that ignored SIGTERM. + const members = [...table.values()].filter((row) => row.pgid === group).map((row) => row.pid); + const jobRunning = readProcessTable.pipe( + Effect.map((current) => members.some((pid) => current.get(pid)?.pgid === group)), + Effect.orElseSucceed(() => false), + ); + const deadline = (yield* Clock.currentTimeMillis) + STOP_GRACE_MS; + while ((yield* Clock.currentTimeMillis) < deadline) { + yield* Effect.sleep(STOP_POLL_INTERVAL); + if (!(yield* jobRunning)) return; + } + yield* signalGroup("SIGKILL"); + }); + + const stopServer: PortDiscovery["Service"]["stopServer"] = (input) => + // Subscribers see the server leave now rather than on the next poll. + signalUntilStopped(input).pipe(Effect.andThen(pollIfRetained)); + + return PortDiscovery.of({ + scan: scanOnce, + subscribe, + retain, + registerTerminalProcesses, + unregisterTerminal, + trackAgent, + stopServer, + }); +}).pipe(Effect.withSpan("PortDiscovery.make")); + +export const layer = Layer.effect(PortDiscovery, make); + +/** A scanner that tracks nothing, for tests that build provider runners without a host. */ +export const layerTest = Layer.mock(PortDiscovery)({ trackAgent: () => () => undefined }); diff --git a/apps/server/src/provider/ProviderInstanceRegistry.test.ts b/apps/server/src/provider/ProviderInstanceRegistry.test.ts index 36dcdf46948b0..288b23954b776 100644 --- a/apps/server/src/provider/ProviderInstanceRegistry.test.ts +++ b/apps/server/src/provider/ProviderInstanceRegistry.test.ts @@ -39,6 +39,7 @@ import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEve import { makeProviderInstanceRegistry } from "./ProviderInstanceRegistry.ts"; import { ProviderDriverError } from "./Errors.ts"; import * as ProviderOrchestrationAdapterInfrastructure from "./ProviderOrchestrationAdapterInfrastructure.ts"; +import * as PortScanner from "../preview/PortScanner.ts"; import * as ProviderHostLive from "./ProviderHostLive.ts"; // Coder: secret providers are not carried; the registry reads through the stand-in resolver. import * as SecretResolver from "../secrets/coderSecretResolver.ts"; @@ -160,6 +161,7 @@ describe("ProviderInstanceRegistry — multi-instance codex slice", () => { ); const layerBase = ProviderHostLive.layer.pipe(Layer.provideMerge(layerBaseDeps)); const layerTest = ProviderOrchestrationAdapterInfrastructure.layer.pipe( + Layer.provide(PortScanner.layerTest), Layer.provideMerge(layerBase), ); diff --git a/apps/server/src/provider/ProviderInstanceRegistryHydration.ts b/apps/server/src/provider/ProviderInstanceRegistryHydration.ts index 39d8755b06b59..8905531885fdd 100644 --- a/apps/server/src/provider/ProviderInstanceRegistryHydration.ts +++ b/apps/server/src/provider/ProviderInstanceRegistryHydration.ts @@ -23,6 +23,7 @@ import * as ProviderHostLive from "./ProviderHostLive.ts"; import type * as ProviderHost from "@t3tools/provider-core/server/ProviderHost"; import type * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; import type * as ServerConfig from "../config.ts"; +import type * as PortScanner from "../preview/PortScanner.ts"; type ProviderInstanceRegistryHydrationEnv = | Exclude< @@ -33,7 +34,9 @@ type ProviderInstanceRegistryHydrationEnv = > | Settings.ServerSettingsService | BackgroundPolicy.BackgroundPolicy - | ServerConfig.ServerConfig; + | ServerConfig.ServerConfig + // The Claude runner counts its CLIs as running agents for port discovery. + | PortScanner.PortDiscovery; export const deriveProviderInstanceConfigMap = ( settings: ServerSettings, @@ -69,8 +72,7 @@ export const deriveProviderInstanceConfigMap = ( return merged as ProviderInstanceConfigMap; }; -// Coder: upstream's dev replay provider (#26207, `seedDevReplayInstance`) is not carried, and the -// Claude runner reports no agents to port discovery (#22542), so hydration needs neither. +// Coder: upstream's dev replay provider (#26207, `seedDevReplayInstance`) is not carried. const layerSettingsWatcher = (settingsChanges: Stream.Stream) => Layer.effectDiscard( Effect.gen(function* () { diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index 95b7c217dbe69..d0a8b1157d74c 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -47,6 +47,7 @@ import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderL import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers"; import * as ProviderInstanceRegistryHydration from "./ProviderInstanceRegistryHydration.ts"; +import * as PortScanner from "../preview/PortScanner.ts"; import * as ServerConfig from "../config.ts"; import * as ServerSettingsModule from "../serverSettings.ts"; import { @@ -2148,7 +2149,9 @@ it.layer( const scope = yield* Scope.make(); yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void)); const layerProviderRegistry = ProviderRegistry.layer.pipe( - Layer.provideMerge(ProviderInstanceRegistryHydration.layer), + Layer.provideMerge( + ProviderInstanceRegistryHydration.layer.pipe(Layer.provide(PortScanner.layerTest)), + ), Layer.provideMerge( Layer.succeed(ServerSettingsModule.ServerSettingsService, serverSettings), ), @@ -2259,7 +2262,9 @@ it.layer( const scope = yield* Scope.make(); yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void)); const layerProviderRegistry = ProviderRegistry.layer.pipe( - Layer.provideMerge(ProviderInstanceRegistryHydration.layer), + Layer.provideMerge( + ProviderInstanceRegistryHydration.layer.pipe(Layer.provide(PortScanner.layerTest)), + ), Layer.provideMerge( Layer.succeed(ServerSettingsModule.ServerSettingsService, serverSettings), ), @@ -2383,9 +2388,9 @@ it.layer( ), ); readSeed = false; - const services = yield* Layer.build(ProviderInstanceRegistryHydration.layer).pipe( - Effect.provide(dependencies), - ); + const services = yield* Layer.build( + ProviderInstanceRegistryHydration.layer.pipe(Layer.provide(PortScanner.layerTest)), + ).pipe(Effect.provide(dependencies)); failCurrentRead = input.failCurrentRead ?? false; yield* PubSub.publish(changes, undefined); yield* Deferred.succeed(consume, undefined); diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index cf9445c4e2549..e5e49c44c7ac5 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -15,6 +15,7 @@ import * as RepositoryIdentityResolver from "./project/RepositoryIdentityResolve import * as TextGeneration from "./textGeneration/TextGeneration.ts"; import * as TerminalManager from "./terminal/Manager.ts"; import * as ProcessRunner from "./processRunner.ts"; +import * as PortScanner from "./preview/PortScanner.ts"; import * as GitManager from "./git/GitManager.ts"; import * as ServerSettings from "./serverSettings.ts"; import * as WorkspaceEntries from "./workspace/WorkspaceEntries.ts"; @@ -154,9 +155,12 @@ const layerVcs = Layer.empty.pipe( const layerCheckpointStore = CheckpointStore.layer.pipe(Layer.provide(layerVcsDriverRegistry)); +const layerPortScanner = PortScanner.layer.pipe(Layer.provide(ProcessRunner.layer)); + const layerTerminal = TerminalManager.layer.pipe( - // Coder: no port scanner or native telemetry; the PTY adapter and process runner are shared. + // Coder: no native telemetry; the PTY adapter and process runner are shared. Layer.provideMerge(layerPtyAdapter), + Layer.provide(layerPortScanner), Layer.provideMerge(ProcessRunner.layer), ); @@ -186,6 +190,7 @@ export { layerCheckpointStore, layerGitWorkflow, layerIdleCompactionWorker, + layerPortScanner, layerPullRequestService, layerRepositoryIdentityResolver, layerServerSettings, diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index 39871e50c6965..e702cf71cd52b 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -67,6 +67,7 @@ import { deriveProviderInstanceConfigMap } from "../provider/ProviderInstanceReg import * as ServerSettings from "../serverSettings.ts"; import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; import * as ProcessRunner from "../processRunner.ts"; +import * as PortScanner from "../preview/PortScanner.ts"; import { terminalOutputStream } from "./OutputProtocol.ts"; import * as PtyAdapter from "@t3tools/shared/PtyAdapter"; @@ -1516,6 +1517,7 @@ export const resolveProviderInstanceTerminalEnvironment = Effect.fn( export const make = Effect.fn("TerminalManager.make")(function* () { const { terminalLogsDir } = yield* ServerConfig.ServerConfig; const ptyAdapter = yield* PtyAdapter.PtyAdapter; + const portDiscovery = yield* PortScanner.PortDiscovery; const serverSettings = yield* ServerSettings.ServerSettingsService; const path = yield* Path.Path; const resolveProviderInstanceEnvironment = Effect.fn( @@ -1531,8 +1533,9 @@ export const make = Effect.fn("TerminalManager.make")(function* () { return yield* makeWithOptions({ logsDir: terminalLogsDir, ptyAdapter, - // Coder: no resource-telemetry process table or preview port discovery; terminals poll - // the process table themselves. + // Coder: no resource-telemetry process table; terminals poll the process table themselves. + registerTerminalProcesses: portDiscovery.registerTerminalProcesses, + unregisterTerminal: portDiscovery.unregisterTerminal, resolveProviderInstanceEnvironment, }); }); diff --git a/apps/server/src/ws.projects.test.ts b/apps/server/src/ws.projects.test.ts index e600b0169eac2..11769e9d42712 100644 --- a/apps/server/src/ws.projects.test.ts +++ b/apps/server/src/ws.projects.test.ts @@ -67,6 +67,7 @@ import * as SourceControlDiscovery from "./sourceControl/SourceControlDiscovery. import * as SourceControlRepositoryService from "./sourceControl/SourceControlRepositoryService.ts"; import * as StorageCleanup from "./storageCleanup.ts"; import * as TerminalManager from "./terminal/Manager.ts"; +import * as PortScanner from "./preview/PortScanner.ts"; import * as VcsProvisioningService from "./vcs/VcsProvisioningService.ts"; import * as VcsStatusBroadcaster from "./vcs/VcsStatusBroadcaster.ts"; import * as WorkspaceEntries from "./workspace/WorkspaceEntries.ts"; @@ -221,6 +222,7 @@ const harness = ( stub(VcsProvisioningService.VcsProvisioningService), stub(ReviewService.ReviewService), stub(TerminalManager.TerminalManager), + stub(PortScanner.PortDiscovery), stub(GitWorkflowService.GitWorkflowService), stub(SecretRequests.SecretRequests), stub(McpAppRequests.McpAppRequests), diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 07a662bc21399..4bead8570542f 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -26,6 +26,7 @@ import { OrchestrationV2GetShellSnapshotError, OrchestrationV2GetThreadProjectionError, OrchestrationV2ThreadLaunchError, + type DiscoveredLocalServerList, type OrchestrationProjectShell, type OrchestrationV2ShellSnapshot, type OrchestrationV2ThreadStreamItem, @@ -57,6 +58,7 @@ import { RpcGroup } from "effect/rpc"; import { isNetworkPath } from "@t3tools/shared/path"; import { issueAssetUrl } from "./assets/AssetAccess.ts"; import { deletePendingAttachment, issueAttachmentUploadUrl } from "./assets/AttachmentUpload.ts"; +import * as PortScanner from "./preview/PortScanner.ts"; import * as CheckpointDiffQuery from "./checkpointing/CheckpointDiffQuery.ts"; import * as ServerConfig from "./config.ts"; import * as EnvironmentTheme from "./environmentTheme.ts"; @@ -915,6 +917,7 @@ const layerWsRpc = () => const vcsProvisioning = yield* VcsProvisioningService.VcsProvisioningService; const vcsStatusBroadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; const terminalManager = yield* TerminalManager.TerminalManager; + const portDiscovery = yield* PortScanner.PortDiscovery; const providerRegistry = yield* ProviderRegistry.ProviderRegistry; const providerInstances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry; const providerLatestVersions = yield* ProviderLatestVersions.ProviderLatestVersions; @@ -1833,6 +1836,33 @@ const layerWsRpc = () => (unsubscribe) => Effect.sync(unsubscribe), ), ), + [WS_METHODS.previewStopLocalServer]: (input) => portDiscovery.stopServer(input), + [WS_METHODS.subscribeDiscoveredLocalServers]: (input) => + Stream.callback((queue) => + Effect.gen(function* () { + const configuredUrls = input.configuredUrls ?? []; + yield* portDiscovery.retain; + const initial = yield* portDiscovery.scan(configuredUrls); + const initialScannedAt = DateTime.formatIso(yield* DateTime.now); + yield* Queue.offer(queue, { + servers: initial, + scannedAt: initialScannedAt, + configuredUrlProbing: true, + }); + yield* portDiscovery.subscribe( + { configuredUrls, initialSnapshot: initial }, + (servers) => + Effect.gen(function* () { + const scannedAt = DateTime.formatIso(yield* DateTime.now); + yield* Queue.offer(queue, { + servers, + scannedAt, + configuredUrlProbing: true, + }); + }), + ); + }), + ), // Coder: preserve bounded helper stdio streams and reconnect snapshots. [WS_METHODS.subscribeServerConfig]: (input) => Stream.unwrap( diff --git a/apps/web/src/coder/api.ts b/apps/web/src/coder/api.ts index ff9a6839aaceb..69be6c5aaef25 100644 --- a/apps/web/src/coder/api.ts +++ b/apps/web/src/coder/api.ts @@ -99,6 +99,16 @@ export async function loadCoderPortForwardStatuses( ).portForwards; } +/** The requested local port, or the next one free for a TCP forward on this computer. */ +export async function findFreeCoderForwardPort(port: number): Promise { + const response = await fetchCoder("/api/port-forwards/free-port", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ port }), + }); + return ((await response.json()) as { readonly port: number }).port; +} + export async function restartCoderPortForward(portForwardId: string): Promise { await fetchCoder(`/api/port-forwards/${encodeURIComponent(portForwardId)}/restart`, { method: "POST", diff --git a/apps/web/src/coder/serverForwards.test.ts b/apps/web/src/coder/serverForwards.test.ts new file mode 100644 index 0000000000000..3914576a78bc9 --- /dev/null +++ b/apps/web/src/coder/serverForwards.test.ts @@ -0,0 +1,112 @@ +import { afterEach, describe, expect, it, vi } from "vite-plus/test"; + +import type { CoderProfileConfig } from "./api"; +import { addServerForward, findServerForward, forwardServer } from "./serverForwards"; + +afterEach(() => { + vi.unstubAllGlobals(); +}); + +const config: CoderProfileConfig = { + version: 1, + deployments: [{ id: "deployment", name: "Deployment", url: "https://coder.example.test" }], + workspaces: [ + { id: "workspace", name: "Workspace", deploymentId: "deployment", workspace: "owner/one" }, + ], + portForwards: [ + { id: "udp", workspaceId: "workspace", protocol: "udp", localPort: 5173, remotePort: 5173 }, + ], +} as CoderProfileConfig; + +/** Answers the gateway's free-port request with 5174 and status reads with `status(rule)`. */ +function stubGateway( + read: () => CoderProfileConfig, + status: (poll: number) => Record, +) { + const calls: string[] = []; + let polls = 0; + vi.stubGlobal( + "fetch", + vi.fn(async (path: string, init?: RequestInit) => { + calls.push(`${init?.method ?? "GET"} ${path}${init?.body ? ` ${String(init.body)}` : ""}`); + if (path === "/api/port-forwards/free-port") return Response.json({ port: 5174 }); + polls += 1; + return Response.json({ + portForwards: (read().portForwards ?? []).map((entry) => ({ + id: entry.id, + ...status(polls), + })), + }); + }), + ); + return calls; +} + +function fakeUpdateConfig() { + let current = config; + const updateConfig = vi.fn(async (update: (value: CoderProfileConfig) => CoderProfileConfig) => { + current = update(current); + }); + return { updateConfig, read: () => current }; +} + +describe("server forwards", () => { + it("finds only a TCP forward of the server's workspace port", () => { + expect(findServerForward(config, "workspace", 5173)).toBeUndefined(); + const added = addServerForward(config, { + id: "tcp", + workspaceId: "workspace", + protocol: "tcp", + localPort: 5174, + remotePort: 5173, + }); + expect(findServerForward(added, "workspace", 5173)?.id).toBe("tcp"); + expect(findServerForward(added, "other", 5173)).toBeUndefined(); + }); + + it("refuses a local port another TCP rule already uses", () => { + const rule = { + id: "a", + workspaceId: "workspace", + protocol: "tcp", + localPort: 5174, + remotePort: 5173, + } as const; + expect(() => addServerForward(addServerForward(config, rule), { ...rule, id: "b" })).toThrow( + /already configured/, + ); + }); + + it("saves a TCP rule at the gateway's free port and resolves once it is ready", async () => { + const { updateConfig, read } = fakeUpdateConfig(); + const calls = stubGateway(read, (poll) => ({ + status: "running", + ...(poll > 1 ? { ready: true } : {}), + })); + + const rule = await forwardServer({ workspaceId: "workspace", remotePort: 5173, updateConfig }); + + expect(rule).toMatchObject({ + workspaceId: "workspace", + protocol: "tcp", + localPort: 5174, + remotePort: 5173, + }); + expect(read().portForwards?.at(-1)).toEqual(rule); + expect(calls).toEqual([ + 'POST /api/port-forwards/free-port {"port":5173}', + "GET /api/port-forwards", + "GET /api/port-forwards", + ]); + }); + + it("removes the rule again when the forward fails to start", async () => { + const { updateConfig, read } = fakeUpdateConfig(); + stubGateway(read, () => ({ status: "error", error: "Local port 5174 is already in use." })); + + await expect( + forwardServer({ workspaceId: "workspace", remotePort: 5173, updateConfig }), + ).rejects.toThrow("Local port 5174 is already in use."); + expect(read().portForwards).toEqual(config.portForwards); + }); +}); diff --git a/apps/web/src/coder/serverForwards.ts b/apps/web/src/coder/serverForwards.ts new file mode 100644 index 0000000000000..e26495c108ada --- /dev/null +++ b/apps/web/src/coder/serverForwards.ts @@ -0,0 +1,117 @@ +// Forwards a dev server the thread's agent or terminals started to this computer as a normal saved +// port-forward rule, the same shape Settings → Port forwarding edits. +import type { EnvironmentId } from "@t3tools/contracts"; +import { useCallback } from "react"; + +import { + useCoderSettingsConfig, + type UpdateCoderSettingsConfig, +} from "../components/settings/useCoderSettingsConfig"; +import { randomUUID } from "../lib/utils"; +import { + findFreeCoderForwardPort, + loadCoderPortForwardStatuses, + type CoderPortForwardProfile, + type CoderProfileConfig, +} from "./api"; +import { useCoder } from "./CoderBootstrap"; +import { coderWorkspaceIdForEnvironment } from "./environmentStore"; + +/** How long Forward waits for Coder to report its listener ready before opening the tab anyway. */ +const FORWARD_READY_TIMEOUT_MS = 20_000; +const FORWARD_READY_POLL_MS = 250; + +/** The saved TCP forward that reaches `remotePort` in the workspace, if any. */ +export function findServerForward( + config: CoderProfileConfig, + workspaceId: string, + remotePort: number, +): CoderPortForwardProfile | undefined { + return config.portForwards?.find( + (rule) => + rule.workspaceId === workspaceId && rule.protocol === "tcp" && rule.remotePort === remotePort, + ); +} + +/** Adds `rule` with the checks the port-forward editor makes before saving. */ +export function addServerForward( + current: CoderProfileConfig, + rule: CoderPortForwardProfile, +): CoderProfileConfig { + const rules = current.portForwards ?? []; + if (!current.workspaces.some((workspace) => workspace.id === rule.workspaceId)) + throw new Error("The workspace connection was removed."); + if (rules.some((entry) => entry.protocol === rule.protocol && entry.localPort === rule.localPort)) + throw new Error(`Local port ${rule.localPort} is already configured. Try again.`); + return { ...current, portForwards: [...rules, rule] }; +} + +export function removeServerForward( + current: CoderProfileConfig, + ruleId: string, +): CoderProfileConfig { + return { + ...current, + portForwards: (current.portForwards ?? []).filter((entry) => entry.id !== ruleId), + }; +} + +export const forwardedServerUrl = (rule: Pick) => + `http://127.0.0.1:${rule.localPort}/`; + +/** Saves and starts a forward of `remotePort`, then resolves once Coder reports it ready. */ +export async function forwardServer(input: { + readonly workspaceId: string; + readonly remotePort: number; + readonly updateConfig: UpdateCoderSettingsConfig; +}): Promise { + const { remotePort, updateConfig } = input; + const rule: CoderPortForwardProfile = { + id: `port-forward-${randomUUID()}`, + workspaceId: input.workspaceId, + protocol: "tcp", + localPort: await findFreeCoderForwardPort(remotePort), + remotePort, + }; + // Saving starts the forward; the gateway answers once its process has spawned. + await updateConfig((current) => addServerForward(current, rule)); + const deadline = Date.now() + FORWARD_READY_TIMEOUT_MS; + for (;;) { + const status = (await loadCoderPortForwardStatuses()).find((entry) => entry.id === rule.id); + const expired = Date.now() >= deadline; + // Coder versions that never print `Ready!` still open once the wait runs out. + if (status?.status === "running" && (status.ready === true || expired)) return rule; + if (status?.status !== "running" && (status?.status !== "starting" || expired)) { + // A forward that never ran is not kept, so Settings shows no rule to explain. + await updateConfig((current) => removeServerForward(current, rule.id)); + throw new Error(status?.error ?? "The port forward did not start."); + } + await new Promise((resolve) => setTimeout(resolve, FORWARD_READY_POLL_MS)); + } +} + +/** Saved forwards for an environment's workspace, and the Forward and Stop forwarding actions. */ +export function useServerForwards(environmentId: EnvironmentId) { + const { config, saveConfig } = useCoder(); + const updateConfig = useCoderSettingsConfig(config, saveConfig); + const workspaceId = coderWorkspaceIdForEnvironment(environmentId); + + const forwardFor = (remotePort: number) => + workspaceId === null ? undefined : findServerForward(config, workspaceId, remotePort); + + const forward = useCallback( + (remotePort: number): Promise => + workspaceId === null + ? Promise.reject(new Error("This thread's workspace is not connected.")) + : forwardServer({ workspaceId, remotePort, updateConfig }), + [updateConfig, workspaceId], + ); + + const stopForwarding = useCallback( + (rule: CoderPortForwardProfile) => + updateConfig((current) => removeServerForward(current, rule.id)), + [updateConfig], + ); + + return { canForward: workspaceId !== null, forwardFor, forward, stopForwarding }; +} diff --git a/apps/web/src/components/chat/ThreadDetailsPanel.tsx b/apps/web/src/components/chat/ThreadDetailsPanel.tsx index aa569c939f413..c0bfdf1e76697 100644 --- a/apps/web/src/components/chat/ThreadDetailsPanel.tsx +++ b/apps/web/src/components/chat/ThreadDetailsPanel.tsx @@ -20,6 +20,7 @@ import { ThreadDetailsCard } from "./ThreadDetailsCard"; import { ThreadDetailsSection } from "./ThreadDetailsSection"; import { ThreadAutomationsPanel } from "./ThreadAutomationsPanel"; import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl"; +import { ThreadServersPanel } from "./ThreadServersPanel"; import { ThreadDetailsItemsMenuItems } from "./ThreadDetailsItemsMenu"; import { resolveThreadDetailsVisibility, THREAD_DETAILS_ITEMS } from "./threadDetailsItems"; import { useClientSettings } from "../../hooks/useSettings"; @@ -34,7 +35,7 @@ import { Menu, MenuPopup } from "../ui/menu"; // Coder: there is no local editor to open, and the gateway installs the helper matching this // client, so there is no version mismatch warning. Repository actions come from the helper's -// validated t3.json read by project. No dev-servers or usage-limits items either. +// validated t3.json read by project. No usage-limits item either. const GitActionsControl = memo(BaseGitActionsControl); interface ThreadDetailsPanelProps extends Pick< @@ -220,7 +221,10 @@ export function ThreadDetailsPanel(props: ThreadDetailsPanelProps) { ) : null} - {/* Coder: no dev-servers section; it needs upstream's preview port discovery. */} + {density === "full" && !props.draftId && visible.servers ? ( + + ) : null} + {density === "full" && !props.draftId && visible.automations ? ( `${server.pid}:${server.port}`; + +/** Web servers the thread's terminals and agent started. Hidden while none run. */ +export function ThreadServersPanel(props: { + readonly environmentId: EnvironmentId; + readonly threadId: ThreadId; +}) { + const servers = useThreadDiscoveredPorts(props); + const canOpen = useEnvironmentScope(props.environmentId, AuthPreviewOperateScope); + const canStop = useAtomValue( + previewEnvironment.stopLocalServer.permissionAtom(props.environmentId), + ); + const supportsStop = + useEnvironment(props.environmentId)?.serverConfig?.environment.capabilities.localServerStop === + true; + const forwards = useServerForwards(props.environmentId); + const [forwardingKey, setForwardingKey] = useState(null); + const [unforwardingKey, setUnforwardingKey] = useState(null); + const stopServer = useAtomCommand(previewEnvironment.stopLocalServer, { + label: "thread server stop", + }); + const [stoppingKey, setStoppingKey] = useState(null); + + if (servers.length === 0) return null; + + const reportFailure = (title: string, error: unknown) => { + toastManager.add( + stackedThreadToast({ + type: "error", + title, + description: error instanceof Error ? error.message : String(error), + }), + ); + }; + + // Coder: a forwarded server opens at its laptop address; any other is forwarded first. + const open = async (server: DiscoveredLocalServer, event: MouseEvent) => { + if (event.button > 1) return; + event.preventDefault(); + const existing = forwards.forwardFor(server.port); + if (existing) { + window.open(forwardedServerUrl(existing), "_blank", "noopener,noreferrer"); + return; + } + if (forwardingKey !== null) return; + // Opened during the click, which a later window.open would lose, and pointed at the forward + // once Coder reports it ready. + const tab = window.open("", "_blank"); + if (tab) tab.opener = null; + setForwardingKey(serverKey(server)); + const forwarded = await forwards.forward(server.port).then( + (rule) => rule, + (error: unknown) => { + tab?.close(); + reportFailure("Could not forward server", error); + return null; + }, + ); + setForwardingKey(null); + if (forwarded === null) return; + const url = forwardedServerUrl(forwarded); + if (tab) tab.location.href = url; + else window.open(url, "_blank", "noopener,noreferrer"); + }; + + const unforward = async (server: DiscoveredLocalServer) => { + const existing = forwards.forwardFor(server.port); + if (!existing || unforwardingKey !== null) return; + setUnforwardingKey(serverKey(server)); + await forwards + .stopForwarding(existing) + .catch((error: unknown) => reportFailure("Could not stop forwarding", error)); + setUnforwardingKey(null); + }; + + const stop = async (server: DiscoveredLocalServer) => { + if (server.pid === null || stoppingKey !== null) return; + setStoppingKey(serverKey(server)); + const result = await stopServer({ + environmentId: props.environmentId, + input: { pid: server.pid, port: server.port }, + }); + setStoppingKey(null); + if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { + reportFailure("Could not stop server", squashAtomCommandFailure(result)); + } + }; + + return ( + +
    + {servers.map((server) => { + const key = serverKey(server); + const stoppable = canStop && supportsStop && server.pid !== null; + const forwarded = forwards.forwardFor(server.port); + return ( +
  • + + void open(server, event)} + onAuxClick={(event) => void open(server, event)} + /> + } + > + {forwardingKey === key ? ( + + ) : ( + + )} + {/* The end padding keeps text clear of the stop button. */} + {/* Coder: and of the Stop forwarding button. */} + + + localhost:{server.port} + + + {/* Coder: a forwarded server leads with its laptop address. */} + {forwarded ? ( + <> + + + 127.0.0.1:{forwarded.localPort} + + {" · "} + + ) : null} + {[server.terminal === null ? "Agent" : "Terminal", server.processName] + .filter(Boolean) + .join(" · ")} + + + + + {forwarded + ? `Open 127.0.0.1:${forwarded.localPort} in a new tab.` + : "Forward to this computer and open it in a new tab."} + + + {stoppable ? ( +
    + + void stop(server)} + /> + } + > + {stoppingKey === key ? ( + + ) : ( + + )} + + Stop server + +
    + ) : null} + {/* Coder: removes the saved forward, left of Stop; the server keeps running. */} + {forwarded ? ( +
    + + void unforward(server)} + /> + } + > + {unforwardingKey === key ? ( + + ) : ( + + )} + + Stop forwarding + +
    + ) : null} +
  • + ); + })} +
+
+ ); +} diff --git a/apps/web/src/components/chat/threadDetailsItems.ts b/apps/web/src/components/chat/threadDetailsItems.ts index 3e4936584cd9c..cb6d3cb463bd9 100644 --- a/apps/web/src/components/chat/threadDetailsItems.ts +++ b/apps/web/src/components/chat/threadDetailsItems.ts @@ -1,14 +1,15 @@ import type { ClientSettings } from "@t3tools/contracts"; // Ids are stored in client settings, so never rename one. -// Coder: no "openIn" (no local editor), "servers" (no dev-servers panel), or "usage" (no plan -// windows) items; the retired Labs usage flag therefore has no item to default. +// Coder: no "openIn" (no local editor) or "usage" (no plan windows) items; the retired Labs usage +// flag therefore has no item to default. export const THREAD_DETAILS_ITEMS = [ { id: "environment", label: "Environment and worktree", defaultVisible: true }, { id: "scripts", label: "Project scripts", defaultVisible: true }, { id: "branch", label: "Branch and pull request", defaultVisible: true }, { id: "gitActions", label: "Git actions", defaultVisible: true }, { id: "changes", label: "Changes", defaultVisible: true }, + { id: "servers", label: "Servers", defaultVisible: true }, { id: "automations", label: "Automations", defaultVisible: true }, { id: "lineage", label: "Lineage", defaultVisible: true }, ] as const; diff --git a/apps/web/src/portDiscoveryState.test.ts b/apps/web/src/portDiscoveryState.test.ts new file mode 100644 index 0000000000000..82ab1828dc63f --- /dev/null +++ b/apps/web/src/portDiscoveryState.test.ts @@ -0,0 +1,35 @@ +import { CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS, PREVIEW_URL_MAX_LENGTH } from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { boundConfiguredLocalServerUrls } from "./portDiscoveryState"; + +describe("boundConfiguredLocalServerUrls", () => { + it("keeps subscription payloads within the discovery RPC bounds", () => { + const urls = Array.from( + { length: CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS + 1 }, + (_, index) => `http://localhost:${3_000 + index}`, + ); + urls.unshift( + "https://example.com", + "not a URL", + `http://localhost/${"a".repeat(PREVIEW_URL_MAX_LENGTH)}`, + ); + + const bounded = boundConfiguredLocalServerUrls(urls); + + expect(bounded).toHaveLength(CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS); + expect(bounded.every((url) => url.startsWith("http://localhost:"))).toBe(true); + }); + + it("does not let fragment-only variants crowd out another server", () => { + const fragments = Array.from( + { length: CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS }, + (_, index) => `http://localhost:3000/docs#section-${index}`, + ); + + expect(boundConfiguredLocalServerUrls([...fragments, "http://localhost:4000/app"])).toEqual([ + "http://localhost:3000/docs#section-0", + "http://localhost:4000/app", + ]); + }); +}); diff --git a/apps/web/src/portDiscoveryState.ts b/apps/web/src/portDiscoveryState.ts new file mode 100644 index 0000000000000..5eb3698eaa224 --- /dev/null +++ b/apps/web/src/portDiscoveryState.ts @@ -0,0 +1,80 @@ +import { + CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS, + PREVIEW_URL_MAX_LENGTH, + type DiscoveredLocalServer, + type EnvironmentId, + type ThreadId, +} from "@t3tools/contracts"; +import { isLoopbackHost } from "@t3tools/shared/preview"; + +import { previewEnvironment } from "./state/preview"; +import { useEnvironmentQuery } from "./state/query"; + +const EMPTY_PORTS: ReadonlyArray = Object.freeze([]); + +interface DiscoveredPortsState { + readonly servers: ReadonlyArray; + readonly configuredUrlProbing: boolean; +} + +export function boundConfiguredLocalServerUrls( + urls: ReadonlyArray | undefined, +): ReadonlyArray { + const bounded: string[] = []; + const seen = new Set(); + for (const raw of urls ?? []) { + if (raw.length === 0 || raw.length > PREVIEW_URL_MAX_LENGTH || raw.trim().length !== raw.length) + continue; + try { + const url = new URL(raw); + if (url.protocol !== "http:" && url.protocol !== "https:") continue; + if (!isLoopbackHost(url.hostname) || url.href.length > PREVIEW_URL_MAX_LENGTH) continue; + const resourceUrl = new URL(url.href); + resourceUrl.hash = ""; + if (seen.has(resourceUrl.href)) continue; + seen.add(resourceUrl.href); + bounded.push(url.href); + if (bounded.length >= CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS) break; + } catch {} + } + return bounded; +} + +function useDiscoveredPorts( + environmentId: EnvironmentId | null, + configuredUrls?: ReadonlyArray, +): ReadonlyArray { + return useDiscoveredPortsState(environmentId, configuredUrls).servers; +} + +export function useDiscoveredPortsState( + environmentId: EnvironmentId | null, + configuredUrls?: ReadonlyArray, +): DiscoveredPortsState { + const boundedConfiguredUrls = boundConfiguredLocalServerUrls(configuredUrls); + const query = useEnvironmentQuery( + environmentId === null + ? null + : previewEnvironment.discoveredServers({ + environmentId, + input: boundedConfiguredUrls.length ? { configuredUrls: boundedConfiguredUrls } : {}, + }), + ); + return { + servers: query.data?.servers ?? EMPTY_PORTS, + configuredUrlProbing: query.data?.configuredUrlProbing === true, + }; +} + +export function useThreadDiscoveredPorts(input: { + readonly environmentId: EnvironmentId | null; + readonly threadId: ThreadId | null; +}): ReadonlyArray { + const ports = useDiscoveredPorts(input.environmentId); + return input.threadId + ? ports.filter( + (port) => + port.terminal?.threadId === input.threadId || port.agent?.threadId === input.threadId, + ) + : EMPTY_PORTS; +} diff --git a/apps/web/src/state/preview.ts b/apps/web/src/state/preview.ts new file mode 100644 index 0000000000000..af15f38ab7739 --- /dev/null +++ b/apps/web/src/state/preview.ts @@ -0,0 +1,5 @@ +import { createPreviewEnvironmentAtoms } from "@t3tools/client-runtime/state/preview"; + +import { connectionAtomRuntime } from "../connection/runtime"; + +export const previewEnvironment = createPreviewEnvironmentAtoms(connectionAtomRuntime); diff --git a/docs/internals/coder-only.md b/docs/internals/coder-only.md index 2bc0d019048f0..6d6183a7f6f38 100644 --- a/docs/internals/coder-only.md +++ b/docs/internals/coder-only.md @@ -174,7 +174,11 @@ Each saved port-forward rule starts a separate foreground `coder port-forward` p contains only a configured workspace, TCP or UDP protocol, and validated local and remote ports. The gateway always supplies `127.0.0.1` as the local bind address. It reports process state to the settings UI, does not loop on failures, and stops the exact captured process when a rule changes, is -removed, or the gateway exits. +removed, or the gateway exits. A running forward's status adds `ready` once the process prints +Coder's `Ready!` line on stderr. The thread details card's Servers section saves the same rules +(see [Web chat, composer, and files](#web-chat-composer-and-files)); its free-port check +(`/api/port-forwards/free-port`, at most `MAX_LOCAL_PORT_CANDIDATES` ports past the requested one) +binds and closes a probe listener on `127.0.0.1` only and opens no connection. ```text browser -> 127.0.0.1 gateway -> coder ssh stdio -> workspace helper -> codex / claude @@ -432,7 +436,8 @@ checks that every name here still exists and that every modified upstream file i `CoderRuntimeStartup`, `StorageCleanup`, `DatabaseStorage`, the T3 tool bridge binding, and the RPC layer. In `server.ts`, settings also provide the secret store, the VCS status broadcaster takes the demand-only background policy, and the terminal shares its PTY adapter and process - runner with no port scanner or native telemetry. + runner and upstream's port scanner (`layerPortScanner`, which the helper also provides to the + Claude runner and the RPC handlers) with no native telemetry. - `CoderRuntimeStartup` completes before the RPC layer is built, replacing upstream's startup command queue, with upstream's recovery order (provider runtimes, then `recoverDelegatedTasks`); `coderServer.ts` starts the delegated-task question reactor with the other workers, and @@ -525,8 +530,7 @@ checks that every name here still exists and that every modified upstream file i `forkHeadMessageId`) is not carried. Forks also carry `claudeAttachmentBudget`. - `ClaudeAdapterV2.ts`: pasted images go through `resolvePastedImageAttachments`, counted toward the PDF and attachment budget by `sizeBytes`; bridge sessions get - `CLAUDE_BRIDGE_BACKGROUND_WAIT_INSTRUCTIONS` (no ToolSearch sentence); the query runner gives CLI - jobs no agent-thread variable and counts no agent for port discovery; no `deviceToolsAvailable` + `CLAUDE_BRIDGE_BACKGROUND_WAIT_INSTRUCTIONS` (no ToolSearch sentence); no `deviceToolsAvailable` input or `SubagentStart` device hook; `resolveBypassAvailability` reads the settings files (cached remote, user, project, local, managed) where upstream calls the SDK's `resolveSettings`, and `resolveSubagentPromptCacheTtl` reads `subagentPromptCacheTtl` from them (last set wins). @@ -562,7 +566,7 @@ checks that every name here still exists and that every modified upstream file i driver list; `settingsSecrets.ts` reads secret fields from Pi's and the ACP Registry's client definitions only; `ProviderOrchestrationAdapterInfrastructure.ts` provides only the Claude query runner and the Codex app-server factory; `ProviderInstanceRegistryHydration.ts` seeds no replay - instance and needs no `PortDiscovery` or `Crypto`; `testkit/DormantProviderCapabilities.ts` is + instance and needs no `Crypto`; `testkit/DormantProviderCapabilities.ts` is kept for orchestrator tests. - Pi: `buildPiRpcLaunch` hands upstream's T3 extension the file bridge (`T3_TOOL_COMMAND`, `T3_TOOL_CATALOG`); it registers the catalog's tools under upstream's names, runs each through the @@ -645,7 +649,8 @@ checks that every name here still exists and that every modified upstream file i check) and adds the helper-only methods and `CoderWsRpcGroup`, the only group the helper serves. Upstream's `WsRpcGroup` stays dormant (`knip.jsonc` ignores `rpc.ts`'s unused exports). `CoderWsRpcGroup` omits `server.getDatabaseStorage`, `server.getNpmRelease`, - `server.prepareTriage`, `preview.stopLocalServer`, guest share creation, the peer-link RPCs, and + `server.prepareTriage`, the browser preview's RPCs (it keeps `preview.stopLocalServer` and + `subscribeDiscoveredLocalServers`), guest share creation, the peer-link RPCs, and the `sourceControl.signIn*`, `sourceControl.appAccess`, and `sourceControl.starred` RPCs. - `server.ts` (contracts) mirrors `ipc.ts`'s `DesktopUpdateChannelSchema` for `ServerSelfUpdateInput`, because `ipc.ts` is not carried. @@ -664,7 +669,7 @@ checks that every name here still exists and that every modified upstream file i - `terminal/Manager.ts` keeps a bounded attach replay window (`DEFAULT_ATTACH_REPLAY_*`) and answers an attach with `afterSequence` by replaying the gap and a `resumed` event; each shell session has an `attachEpoch`, and a cursor naming another epoch gets a snapshot. Terminals poll - the process table themselves, register no ports, record no metrics, and get no managed ACP + the process table themselves, record no metrics, and get no managed ACP directories on `PATH`. - client-runtime adds `TerminalBufferCache`, a bounded memory cache that seeds a reattach and supplies its `afterSequence` and `afterEpoch`; a resumed attach of a closed terminal gets @@ -864,8 +869,18 @@ checks that every name here still exists and that every modified upstream file i is still the partial diff that asked, and lets a contents loader return `null` (`FileDiffContentsLoader` and `LoadedPartialDiffContents` types), which 1.6.1 still lacks. - Project actions: repository actions come from `projects.getConfig`, which drops `iconPath` and - script preview fields; the editor has no preview URL; the thread details card has no Open in, - servers, or usage items, so `threadDetailsUsageEnabled` is never read. + script preview fields; the editor has no preview URL; the thread details card has no Open in or + usage items, so `threadDetailsUsageEnabled` is never read. +- Servers (`chat/ThreadServersPanel.tsx`): selecting an unforwarded server is **Forward** in place + of opening the browser panel (`coder/serverForwards.ts`): the gateway's + `/api/port-forwards/free-port` picks the server's port or the next one free on `127.0.0.1` and + unused by a saved TCP rule, the browser saves an ordinary rule through `useCoderSettingsConfig`, + waits for the forward's `ready` status (Coder's `Ready!` line, up to `FORWARD_READY_TIMEOUT_MS`), + and points a tab it opened during the click at `http://127.0.0.1:/`; a forward that + never runs is removed again. A forwarded row shows its local address, opens it, and adds **Stop + forwarding**, which removes the rule; stopping the server keeps the rule. No modified-click + system-browser open (no desktop shell). Client-runtime's `state/preview.ts` keeps only + `discoveredServers` and `stopLocalServer`, and shared `preview.ts` only the loopback checks. - The worktree setup card says "Use project checkout"; `ChatComposer.tsx`'s `addComposerFolders` always takes the remote-environment branch and its toast. - Command palette (`CommandPalette.tsx` and its split modules): every workspace is remote; Add @@ -1042,7 +1057,7 @@ them, the file is named here. or connection catalog; `settings/AddEnvironmentDialog.tsx` is cut. With no session scopes, scheduled task lists show every webhook URL. - **Browser preview and devices.** No in-app browser, preview panel, mini-player, preview MCP - toolkits, device hosts, or agent device shim; provider-core's device instructions never reach an + toolkits, device hosts, or agent device shim (port discovery stays: `preview/PortScanner.ts`); provider-core's device instructions never reach an agent. Without browser preview, `composerDraftStore.ts` keeps no empty draft alive for an open page, and `mergeDirectPreviewAnnotation` is not carried. - **Telemetry, usage, and diagnostics.** No T3 telemetry, analytics, client-origin attribution, @@ -1102,8 +1117,6 @@ them, the file is named here. reads no reference (an instance whose variable points at one is unavailable with upstream's "Could not read …" reason) and lists no kind (`secretProviders` writes are rejected); `ws.ts` does not advertise `secretReferences`. -- **Dev servers** (#22542). `chat/ThreadServersPanel.tsx`, its `ThreadDetailsPanel.tsx` section and - `thread-sidebar.md` paragraph, and `preview.stopLocalServer`; `localServerStop` is omitted. - **Add to another machine** (#22180, #22254). It copies ignored files such as `.env` between workspaces through the browser: no `listIgnoredFiles`, `projects.listIgnoredFiles` (`ignoredFileListing` omitted), client-runtime `projectCommands.ts` command, `BranchToolbar.tsx` diff --git a/docs/internals/sync.md b/docs/internals/sync.md index b5e4c10a27d78..8dc43dfcc8965 100644 --- a/docs/internals/sync.md +++ b/docs/internals/sync.md @@ -174,7 +174,8 @@ mobile's `microphonePriority.ts` into `packages/shared`) arrives as an added fil (top-level directories in brackets): - Auth and pairing (`apps/server/src/auth`, web `components/auth`, client-runtime `authorization`). -- Preview and browser (`apps/server/src/preview`, web `browser`, `components/preview`). +- Preview and browser (`apps/server/src/preview` except `PortScanner.ts`, web `browser`, + `components/preview`). - Device (`apps/server/src/device`, web `components/device`, client-runtime `device`). - HTTP routes (`http.ts` route files other than the server's signed asset route in `apps/server/src/http.ts`, and the uncarried `assets/` modules). @@ -189,8 +190,8 @@ mobile's `microphonePriority.ts` into `packages/shared`) arrives as an added fil `nativeLogging.ts`). - Desktop (`apps/desktop`, `native`, `packaging`, `assets`); mobile (`apps/mobile`); hosted web (`apps/marketing`, `apps/server/src/cloud`, web `cloud`, `components/clerk`). -- Triage, dev servers, and dev replay (`apps/server/src/triage`, `apps/server/scripts/dev-replay*`, - web `DevReplayHost.tsx`, `chat/ThreadServersPanel.tsx`, `chat/TriageDraftHero.tsx`, +- Triage and dev replay (`apps/server/src/triage`, `apps/server/scripts/dev-replay*`, + web `DevReplayHost.tsx`, `chat/TriageDraftHero.tsx`, `hooks/useStartTriage.ts`, `lib/triageTarget.ts`, `docs/user/triage.md`). - Relay (`apps/server/src/relay`, `infra`, `packages/ssh`, `packages/tailscale`); telemetry (`apps/server/src/{telemetry,resourceTelemetry,usage,diagnostics}`, web `components/usage`). diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.md index d468eabc09576..cf2e30744b030 100644 --- a/docs/user/thread-sidebar.md +++ b/docs/user/thread-sidebar.md @@ -241,6 +241,15 @@ Expand a tool call in the conversation to see its full command and output. Summaries shorten shell wrappers and can still describe the latest call after it finishes; the call's own result shows its status. +The thread details card lists the web servers that the thread's terminals or its Claude agent +started in the workspace, with their ports. A server the agent started stays listed after the +agent's turn ends. Select a server to forward it to `127.0.0.1` on this computer and open it in a +new tab; the forward is saved under **Settings → Environments → Port forwarding** and uses the same +port when it is free, otherwise the next free one. A forwarded server shows its local address, and +selecting it opens that address. **Stop forwarding** removes the forward; the server keeps running. +**Stop** ends the server's job and keeps the agent, the terminal, and T3 Coder running; a saved +forward stays until you remove it. + To choose what the thread details card shows, right-click the card or open **Settings → Appearance → Thread details**. Hidden rows keep their keyboard shortcuts, such as project scripts. **Reset to default** brings back the diff --git a/docs/user/workspaces.md b/docs/user/workspaces.md index d87f162bc7f0d..137124dd043ee 100644 --- a/docs/user/workspaces.md +++ b/docs/user/workspaces.md @@ -64,6 +64,9 @@ network. Each rule runs its own forward process, reports its state (**Starting** **Stopped**), and can be restarted or removed individually. Saved rules start again when the gateway starts; a rule that failed stays stopped until you restart it or change its configuration. +A dev server a thread started can also be forwarded from the thread details card's **Servers** +section, which saves the same kind of TCP rule (see [Thread sidebar](thread-sidebar.md)). + ## Linked workspaces A link lets agents in one workspace delegate tasks to another workspace and work on its threads. diff --git a/packages/client-runtime/package.json b/packages/client-runtime/package.json index 678aafa12d4a9..45f7d8e5b4762 100644 --- a/packages/client-runtime/package.json +++ b/packages/client-runtime/package.json @@ -127,6 +127,10 @@ "types": "./src/state/presentation.ts", "default": "./src/state/presentation.ts" }, + "./state/preview": { + "types": "./src/state/preview.ts", + "default": "./src/state/preview.ts" + }, "./state/projects": { "types": "./src/state/projects.ts", "default": "./src/state/projects.ts" diff --git a/packages/client-runtime/src/rpc/client.ts b/packages/client-runtime/src/rpc/client.ts index 545e578dbe8bb..adf4ade5c1686 100644 --- a/packages/client-runtime/src/rpc/client.ts +++ b/packages/client-runtime/src/rpc/client.ts @@ -60,6 +60,7 @@ export type EnvironmentSubscriptionRpcTag = | typeof WS_METHODS.scheduledTasksSubscribe | typeof WS_METHODS.serverGetStorageCleanupReport | typeof WS_METHODS.subscribeTerminalMetadata + | typeof WS_METHODS.subscribeDiscoveredLocalServers | typeof WS_METHODS.pullRequestsSubscribeRefreshes | typeof WS_METHODS.subscribeVcsStatus | typeof WS_METHODS.subscribeVcsRefStatus diff --git a/packages/client-runtime/src/state/preview.ts b/packages/client-runtime/src/state/preview.ts new file mode 100644 index 0000000000000..ccb8948d50bc9 --- /dev/null +++ b/packages/client-runtime/src/state/preview.ts @@ -0,0 +1,28 @@ +import { WS_METHODS } from "@t3tools/contracts"; +import type { Atom } from "effect/reactivity"; + +import type { EnvironmentRegistry } from "../connection/registry.ts"; +import { + createEnvironmentRpcCommand, + createEnvironmentRpcSubscriptionAtomFamily, +} from "./runtime.ts"; + +// Coder: only port discovery's atoms are carried; the browser preview's list, events, session +// lifecycle, profile, and status commands are not. +export function createPreviewEnvironmentAtoms( + runtime: Atom.AtomRuntime, +) { + return { + discoveredServers: createEnvironmentRpcSubscriptionAtomFamily(runtime, { + label: "environment-data:preview:discovered-servers", + tag: WS_METHODS.subscribeDiscoveredLocalServers, + // Configured URLs are part of this atom's key. Dispose immediately so + // unmounted projects stop contributing probe candidates on the server. + idleTtlMs: 0, + }), + stopLocalServer: createEnvironmentRpcCommand(runtime, { + label: "environment-data:preview:stop-local-server", + tag: WS_METHODS.previewStopLocalServer, + }), + }; +} diff --git a/packages/coder-cli/src/portForward.test.ts b/packages/coder-cli/src/portForward.test.ts index 7340a86bcf5fe..bbdd6c18de670 100644 --- a/packages/coder-cli/src/portForward.test.ts +++ b/packages/coder-cli/src/portForward.test.ts @@ -94,6 +94,35 @@ describe("Coder port forward process", () => { } }); + it("reports ready once Coder prints Ready! after its forwarding lines", async () => { + const child = fakeChild(); + const scope = await Effect.runPromise(Scope.make("sequential")); + try { + const connection = await Effect.runPromise( + connectCoderPortForward( + { executable: "coder", args: ["port-forward", "workspace"] }, + { + spawnProcess: () => { + queueMicrotask(() => child.emit("spawn")); + return child as unknown as ChildProcess; + }, + }, + ).pipe(Scope.provide(scope)), + ); + const flush = () => new Promise((resolve) => setImmediate(resolve)); + child.stderr.write( + "Forwarding 'tcp://127.0.0.1:5173' locally to 'tcp://127.0.0.1:5173' in the workspace\n", + ); + await flush(); + strictEqual(connection.ready?.(), false); + child.stderr.write("Ready!\n"); + await flush(); + strictEqual(connection.ready?.(), true); + } finally { + await Effect.runPromise(Scope.close(scope, Exit.void)); + } + }); + it("force-kills a forward that ignores graceful scope shutdown", async () => { const child = fakeChild(); const signals: NodeJS.Signals[] = []; diff --git a/packages/coder-cli/src/portForward.ts b/packages/coder-cli/src/portForward.ts index a59b576212a03..7de1c0df1fb28 100644 --- a/packages/coder-cli/src/portForward.ts +++ b/packages/coder-cli/src/portForward.ts @@ -30,8 +30,13 @@ export interface CoderPortForwardExit { export interface CoderPortForwardConnection { readonly closed: Effect.Effect; readonly close: Effect.Effect; + /** True once Coder prints `Ready!`: its listeners are bound and the agent is reachable. */ + readonly ready?: () => boolean; } +/** The line `coder port-forward` writes to stderr after binding its listeners. */ +const READY_LINE = /(?:^|\n)Ready!\r?\n/; + type SpawnCoderProcess = ( executable: string, args: readonly string[], @@ -43,6 +48,7 @@ interface ManagedPortForwardProcess { readonly spawned: Deferred.Deferred; readonly exit: Deferred.Deferred; readonly cleanupListeners: () => void; + readonly ready: () => boolean; closeRequested: boolean; } @@ -73,6 +79,7 @@ export function connectCoderPortForward( const spawned = yield* Deferred.make(); const exit = yield* Deferred.make(); let stderr = ""; + let ready = false; const child = yield* Effect.try({ try: () => (options?.spawnProcess ?? spawn)(invocation.executable, invocation.args, { @@ -85,6 +92,7 @@ export function connectCoderPortForward( }); const process: ManagedPortForwardProcess = { child, + ready: () => ready, spawned, exit, closeRequested: false, @@ -118,6 +126,7 @@ export function connectCoderPortForward( const onStderr = (chunk: Buffer) => { if (stderr.length >= MAX_ERROR_BYTES) return; stderr += chunk.toString("utf8").slice(0, MAX_ERROR_BYTES - stderr.length); + ready ||= READY_LINE.test(stderr); }; const onSpawn = () => Deferred.doneUnsafe(spawned, Effect.void); const onError = (cause: Error) => { @@ -154,6 +163,7 @@ export function connectCoderPortForward( return { closed: Deferred.await(process.exit), close: terminatePortForwardProcess(process, terminationGraceMs), + ready: process.ready, }; }); } diff --git a/packages/contracts/src/coderGateway.ts b/packages/contracts/src/coderGateway.ts index 71f1608f12013..e6c8b3c80cfe2 100644 --- a/packages/contracts/src/coderGateway.ts +++ b/packages/contracts/src/coderGateway.ts @@ -47,6 +47,8 @@ export interface CoderProfileConfig { export interface CoderPortForwardRuntimeStatus { readonly id: string; readonly status: "starting" | "running" | "stopped" | "error"; + /** Present once a running forward's listener is bound and the workspace agent reachable. */ + readonly ready?: true; readonly error?: string; } diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.ts index d2adbf897fad5..fee353db3a034 100644 --- a/packages/contracts/src/rpc.ts +++ b/packages/contracts/src/rpc.ts @@ -2190,6 +2190,8 @@ export const CoderWsRpcGroup = RpcGroup.make( WsTerminalClearRpc, WsTerminalCloseRpc, WsSubscribeTerminalMetadataRpc, + WsPreviewStopLocalServerRpc, + WsSubscribeDiscoveredLocalServersRpc, WsSubscribeServerConfigRpc, WsSubscribeServerLifecycleRpc, WsOrchestrationV2DispatchCommandRpc, diff --git a/packages/shared/package.json b/packages/shared/package.json index d8f4181eecd3e..4e9027532f78b 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -240,6 +240,10 @@ "types": "./src/orchestrationV2ThreadError.ts", "import": "./src/orchestrationV2ThreadError.ts" }, + "./preview": { + "types": "./src/preview.ts", + "import": "./src/preview.ts" + }, "./mediaPreview": { "types": "./src/mediaPreview.ts", "import": "./src/mediaPreview.ts" diff --git a/packages/shared/src/preview.ts b/packages/shared/src/preview.ts new file mode 100644 index 0000000000000..242fce315558e --- /dev/null +++ b/packages/shared/src/preview.ts @@ -0,0 +1,16 @@ +// Coder: only the loopback host checks port discovery uses are carried; the browser preview's tab +// ids, PDF asset URLs, and address-bar and URL normalization are not. +const LOOPBACK_HOSTS: ReadonlySet = new Set(["localhost", "127.0.0.1", "0.0.0.0", "::1"]); + +export const LSOF_LOCAL_HOST_TOKENS: ReadonlySet = new Set([ + ...LOOPBACK_HOSTS, + "*", + "[::]", + "[::1]", +]); + +export function isLoopbackHost(host: string): boolean { + if (LOOPBACK_HOSTS.has(host)) return true; + if (host === "[::1]") return true; + return false; +} diff --git a/scripts/sync-resolve-deleted.mjs b/scripts/sync-resolve-deleted.mjs index 04d69fa21fdca..ebb6477958cd8 100644 --- a/scripts/sync-resolve-deleted.mjs +++ b/scripts/sync-resolve-deleted.mjs @@ -39,24 +39,23 @@ const DELETED_PATHS = { ], // Preview and browser: no in-app browser, preview panel, or headless browser automation. // web's browser/ keeps only useOpenLink.ts, and components/preview/ only the panel shell and - // its resize handle. + // its resize handle. The server's preview/ keeps PortScanner.ts for port discovery, and the + // preview state modules keep only its atoms and loopback checks. "preview and browser": [ "apps/server/src/mcp/PreviewAutomationBroker.ts", "apps/server/src/htmlRender/headlessChrome.ts", "apps/server/src/htmlRender/HtmlRender.test.ts", "apps/server/src/mcp/toolkits/preview/", "apps/server/src/mcp/toolkits/previewControls/", - "apps/server/src/preview/", + /^apps\/server\/src\/preview\/(?!PortScanner\.)/, "apps/web/src/browser/", "apps/web/src/components/preview/", "apps/web/src/components/settings/IntegrationsSettings.tsx", "apps/web/src/previewMiniPlayerStore.ts", "apps/web/src/previewStateStore.ts", - "apps/web/src/state/preview.ts", "apps/web/src/state/previewStream.ts", "packages/client-runtime/src/preview/", - "packages/client-runtime/src/state/preview.ts", - "packages/shared/src/preview.ts", + "packages/shared/src/preview.test.ts", "packages/shared/src/previewViewport.ts", ], // Device: no device hosts, device hub, or agent device shim. @@ -315,15 +314,13 @@ const DELETED_PATHS = { "apps/web/src/guestPage.ts", "apps/web/src/routes/guest.tsx", ], - // Triage, dev servers, and dev replay: no in-app triage of upstream issues, no dev-server panel - // (it needs the preview port scanner), and no dev-only replay provider. - "triage, dev servers, and dev replay": [ + // Triage and dev replay: no in-app triage of upstream issues and no dev-only replay provider. + "triage and dev replay": [ "apps/server/src/triage/", "apps/server/scripts/dev-replay/", "apps/server/scripts/dev-replay-agent.ts", "apps/server/scripts/dev-replay-agent.test.ts", "apps/web/src/components/DevReplayHost.tsx", - "apps/web/src/components/chat/ThreadServersPanel.tsx", "apps/web/src/components/chat/TriageDraftHero.tsx", "apps/web/src/hooks/useStartTriage.ts", "apps/web/src/lib/triageTarget.ts",