@devries-cameron20 / t3code Mirror · read-only / commits / 4d9caf14ee
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 00:45 UTC

fix(ssh): tunnel readiness no longer counts ssh's connection time (#33554)

Julius Marminge committed Nov 9, 2026 at 23:59 UTC 4d9caf14eeadcbbf7247177077e87feed0ad6f26
2 files changed +169 -7
packages/ssh/src/tunnel.test.ts
+133 -3
@@ -2,6 +2,7 @@ import { assert, describe, it } from "@effect/vitest";
2 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
3 import * as NodeServices from "@effect/platform-node/NodeServices";
4 import * as NetService from "@t3tools/shared/Net";
5 +import * as Clock from "effect/Clock";
6 import * as Deferred from "effect/Deferred";
7 import * as Duration from "effect/Duration";
8 import * as Effect from "effect/Effect";
@@ -13,11 +14,11 @@ import * as Result from "effect/Result";
14 import * as Sink from "effect/Sink";
15 import * as Stream from "effect/Stream";
16 import { TestClock } from "effect/testing";
16 -import { HttpClient, HttpClientResponse } from "effect/http";
17 +import { HttpClient, HttpClientError, HttpClientResponse } from "effect/http";
18 import { ChildProcess, ChildProcessSpawner } from "effect/process";
19
20 import * as SshAuth from "./auth.ts";
20 -import { SshCommandError, SshPasswordPromptError } from "./errors.ts";
21 +import { SshCommandError, SshPasswordPromptError, SshReadinessError } from "./errors.ts";
22 import * as SshTunnel from "./tunnel.ts";
23
24 const isSshCommandError = Schema.is(SshCommandError);
@@ -74,7 +75,7 @@ const hangingHttpClient = HttpClient.make(() => Effect.never);
75 const testNetService = NetService.NetService.of({
76 canListenOnHost: () => Effect.succeed(true),
77 isPortAvailableOnLoopback: () => Effect.succeed(true),
77 - hasListenerOnHost: () => Effect.succeed(false),
78 + hasListenerOnHost: () => Effect.succeed(true),
79 reserveLoopbackPort: () => Effect.succeed(41_773),
80 findAvailablePort: (preferred) => Effect.succeed(preferred),
81 });
@@ -272,6 +273,135 @@ describe("ssh tunnel scripts", () => {
273 }),
274 );
275
276 + it.effect("waits for a tunnel whose ssh connection outlasts the readiness timeout", () =>
277 + Effect.gen(function* () {
278 + const LOCAL_PORT = 41_773;
279 + // ssh spends ConnectTimeout on each unreachable address before the forward listens.
280 + const SSH_CONNECT_MS = 25_000;
281 + let forwardListensAt = Number.POSITIVE_INFINITY;
282 + const tunnelSpawned = yield* Deferred.make<void>();
283 + const forwardListening = (port: number) =>
284 + Clock.currentTimeMillis.pipe(
285 + Effect.map((now) => port === LOCAL_PORT && now >= forwardListensAt),
286 + );
287 + const spawner = ChildProcessSpawner.make((command) =>
288 + commandArgs(command).includes("-N")
289 + ? Effect.gen(function* () {
290 + forwardListensAt = (yield* Clock.currentTimeMillis) + SSH_CONNECT_MS;
291 + yield* Deferred.succeed(tunnelSpawned, undefined);
292 + return makeRunningProcess(() => undefined);
293 + })
294 + : Effect.succeed(makeSuccessfulProcess('{"remotePort":3773}\n')),
295 + );
296 + const httpClient = HttpClient.make((request) =>
297 + forwardListening(LOCAL_PORT).pipe(
298 + Effect.flatMap((listening) =>
299 + listening
300 + ? Effect.succeed(
301 + HttpClientResponse.fromWeb(request, new Response("", { status: 200 })),
302 + )
303 + : Effect.fail(
304 + new HttpClientError.HttpClientError({
305 + reason: new HttpClientError.TransportError({
306 + request,
307 + description: `connect ECONNREFUSED 127.0.0.1:${LOCAL_PORT}`,
308 + }),
309 + }),
310 + ),
311 + ),
312 + ),
313 + );
314 + const net = NetService.NetService.of({
315 + ...testNetService,
316 + reserveLoopbackPort: () => Effect.succeed(LOCAL_PORT),
317 + canListenOnHost: (port) =>
318 + forwardListening(port).pipe(Effect.map((listening) => !listening)),
319 + isPortAvailableOnLoopback: (port) =>
320 + forwardListening(port).pipe(Effect.map((listening) => !listening)),
321 + hasListenerOnHost: (port) => forwardListening(port),
322 + });
323 + const layer = Layer.mergeAll(
324 + NodeServices.layer,
325 + NodeCrypto.layer,
326 + Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
327 + Layer.succeed(HttpClient.HttpClient, httpClient),
328 + Layer.succeed(NetService.NetService, net),
329 + SshAuth.SshPasswordPrompt.disabledLayer,
330 + SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
331 + );
332 + yield* Effect.gen(function* () {
333 + const manager = yield* SshTunnel.SshEnvironmentManager;
334 + const ensure = yield* Effect.forkChild(
335 + manager.ensureEnvironment({
336 + alias: "devbox",
337 + hostname: "devbox",
338 + username: null,
339 + port: null,
340 + }),
341 + );
342 + yield* Deferred.await(tunnelSpawned);
343 + yield* TestClock.adjust(Duration.seconds(60));
344 + const environment = yield* Fiber.join(ensure);
345 + assert.equal(environment.httpBaseUrl, `http://127.0.0.1:${LOCAL_PORT}/`);
346 + }).pipe(Effect.provide(layer), Effect.scoped);
347 + }),
348 + );
349 +
350 + it.effect("fails with a readiness error when ssh never connects", () =>
351 + Effect.gen(function* () {
352 + const tunnelSpawned = yield* Deferred.make<void>();
353 + let tunnelKilled = false;
354 + const spawner = ChildProcessSpawner.make((command) =>
355 + commandArgs(command).includes("-N")
356 + ? Deferred.succeed(tunnelSpawned, undefined).pipe(
357 + Effect.as(
358 + makeRunningProcess(() => {
359 + tunnelKilled = true;
360 + }),
361 + ),
362 + )
363 + : Effect.succeed(makeSuccessfulProcess('{"remotePort":3773}\n')),
364 + );
365 + const layer = Layer.mergeAll(
366 + NodeServices.layer,
367 + NodeCrypto.layer,
368 + Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
369 + Layer.succeed(HttpClient.HttpClient, hangingHttpClient),
370 + Layer.succeed(
371 + NetService.NetService,
372 + NetService.NetService.of({
373 + ...testNetService,
374 + hasListenerOnHost: () => Effect.succeed(false),
375 + }),
376 + ),
377 + SshAuth.SshPasswordPrompt.disabledLayer,
378 + SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
379 + );
380 + yield* Effect.gen(function* () {
381 + const manager = yield* SshTunnel.SshEnvironmentManager;
382 + const ensure = yield* Effect.forkChild(
383 + Effect.result(
384 + manager.ensureEnvironment({
385 + alias: "devbox",
386 + hostname: "devbox",
387 + username: null,
388 + port: null,
389 + }),
390 + ),
391 + );
392 + yield* Deferred.await(tunnelSpawned);
393 + yield* TestClock.adjust(Duration.minutes(2));
394 + const result = yield* Fiber.join(ensure);
395 + assert.isTrue(Result.isFailure(result));
396 + if (Result.isFailure(result)) {
397 + assert.instanceOf(result.failure, SshReadinessError);
398 + assert.include(result.failure.message, "SSH connection to devbox did not come up");
399 + }
400 + assert.isTrue(tunnelKilled);
401 + }).pipe(Effect.provide(layer), Effect.scoped);
402 + }),
403 + );
404 +
405 it.effect.each([
406 {
407 name: "does not ask for a password when the agent cannot sign an accepted key",
packages/ssh/src/tunnel.ts
+36 -4
@@ -10,12 +10,14 @@ import { extractJsonObject, fromLenientJson } from "@t3tools/shared/schemaJson";
10 import { satisfiesSemverRange } from "@t3tools/shared/semver";
11 import * as Context from "effect/Context";
12 import * as Digest from "effect/crypto/Digest";
13 +import * as Duration from "effect/Duration";
14 import * as Effect from "effect/Effect";
15 import * as Exit from "effect/Exit";
16 import * as Fiber from "effect/Fiber";
17 import * as FileSystem from "effect/FileSystem";
18 import * as Layer from "effect/Layer";
19 import * as Path from "effect/Path";
20 +import * as Schedule from "effect/Schedule";
21 import * as Schema from "effect/Schema";
22 import * as Scope from "effect/Scope";
23 import * as Stream from "effect/Stream";
@@ -52,6 +54,9 @@ const TUNNEL_SHUTDOWN_TIMEOUT_MS = 2_000;
54 const TUNNEL_STDERR_TAIL_CHARS = 16_384;
55 const REMOTE_READY_TIMEOUT_MS = 60_000;
56 const REMOTE_LAUNCH_TIMEOUT_MS = 90_000;
57 +// Matches the node-script launch command's whole ssh run against the same host.
58 +const SSH_TUNNEL_CONNECT_TIMEOUT_MS = REMOTE_LAUNCH_TIMEOUT_MS;
59 +const SSH_TUNNEL_FORWARD_PROBE_INTERVAL_MS = 100;
60 // Cold archive launches download and extract ~70 MB and may wait on an install lock.
61 // Lock waits must outlast both downloads and extraction; SSH must outlast install or wait plus readiness.
62 // Leave slack for verification and extraction, which have no timeout.
@@ -1096,6 +1101,29 @@ const reserveLocalTunnelPort = Effect.fn("ssh/tunnel.reserveLocalTunnelPort")(fu
1101 return yield* net.reserveLoopbackPort();
1102 });
1103
1104 +// ssh only binds the forward after it connects, which can take ConnectTimeout per dead address.
1105 +const waitForTunnelForward = Effect.fn("ssh/tunnel.waitForTunnelForward")(function* (input: {
1106 + readonly target: DesktopSshEnvironmentTarget;
1107 + readonly localPort: number;
1108 +}) {
1109 + const net = yield* NetService.NetService;
1110 + yield* net.hasListenerOnHost(input.localPort, "127.0.0.1").pipe(
1111 + Effect.repeat({
1112 + schedule: Schedule.spaced(Duration.millis(SSH_TUNNEL_FORWARD_PROBE_INTERVAL_MS)),
1113 + until: (listening) => listening,
1114 + }),
1115 + Effect.timeoutOrElse({
1116 + duration: Duration.millis(SSH_TUNNEL_CONNECT_TIMEOUT_MS),
1117 + orElse: () =>
1118 + Effect.fail(
1119 + new SshReadinessError({
1120 + message: `SSH connection to ${input.target.alias} did not come up within ${SSH_TUNNEL_CONNECT_TIMEOUT_MS}ms.`,
1121 + }),
1122 + ),
1123 + }),
1124 + );
1125 +});
1126 +
1127 const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input: {
1128 readonly key: string;
1129 readonly resolvedTarget: DesktopSshEnvironmentTarget;
@@ -1271,10 +1299,14 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
1299 }),
1300 );
1301 yield* Effect.raceFirst(
1274 - waitForHttpReady({
1275 - baseUrl: input.httpBaseUrl,
1276 - timeoutMs: SSH_READY_TIMEOUT_MS,
1277 - }),
1302 + waitForTunnelForward({ target: input.resolvedTarget, localPort: input.localPort }).pipe(
1303 + Effect.andThen(
1304 + waitForHttpReady({
1305 + baseUrl: input.httpBaseUrl,
1306 + timeoutMs: SSH_READY_TIMEOUT_MS,
1307 + }),
1308 + ),
1309 + ),
1310 exitFailure,
1311 ).pipe(
1312 Effect.tap(() =>