Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions apps/coder-gateway/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
76 changes: 76 additions & 0 deletions apps/coder-gateway/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
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<number | null> {
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<unknown> {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
Expand Down Expand Up @@ -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 }
: {}),
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/coderEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const CODER_ENVIRONMENT_CAPABILITIES = {
threadPullRequestLinking: true,
serverResolvedCommandContext: true,
threadStop: true,
localServerStop: true,
environmentIcon: true,
projectCloneTracking: true,
threadProjectFromScratch: true,
Expand Down Expand Up @@ -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<Record<keyof ExecutionEnvironmentCapabilities, string>>;

Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/coderServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
layerCheckpointStore,
layerGitWorkflow,
layerIdleCompactionWorker,
layerPortScanner,
layerPullRequestService,
layerRepositoryIdentityResolver,
layerServerSettings,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 17 additions & 3 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -869,6 +870,7 @@ export const layerQueryRunner: Layer.Layer<
| ChildProcessSpawner.ChildProcessSpawner
| Crypto.Crypto
| Path.Path
| PortScanner.PortDiscovery
| ProviderEventLoggers.ProviderEventLoggers
> = Layer.effect(
ClaudeAgentSdkQueryRunner,
Expand All @@ -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<never>());
const hostEnvironment = yield* HostProcess.Environment;
const portDiscovery = yield* PortScanner.PortDiscovery;

return ClaudeAgentSdkQueryRunner.of({
allocateSessionId: crypto
Expand All @@ -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 =
Expand Down Expand Up @@ -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<void>((resolve) => {
if (processExited) resolve();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -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<void>((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<string>((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));
});
});
Loading