@devries-cameron20 / t3code-1 / commits / b9d7239dcb
Fork 0

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

fix(server): SSH device host no longer reconnects on a single slow health probe (#30159)

gyurix committed Nov 4, 2026 at 00:46 UTC b9d7239dcb42e1dea14620381c180182de52921c
2 files changed +84 -1
apps/server/src/device/SshDeviceHost.test.ts
+77
@@ -11,6 +11,7 @@ import * as Stream from "effect/Stream";
11 import * as HttpClient from "effect/http/HttpClient";
12 import * as HttpClientResponse from "effect/http/HttpClientResponse";
13 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
14 +import * as TestClock from "effect/testing/TestClock";
15 import * as ServerConfig from "../config.ts";
16 import * as DeviceHost from "./DeviceHost.ts";
17 import * as SshDeviceHost from "./SshDeviceHost.ts";
@@ -309,3 +310,79 @@ it.effect("lists Android devices without probing the host again once it reports
310 yield* host.stop;
311 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
312 );
313 +
314 +it.effect(
315 + "keeps the SSH forward through slow health probes and reconnects once the hub stays down",
316 + () =>
317 + Effect.gen(function* () {
318 + const fs = yield* FileSystem.FileSystem;
319 + const home = yield* fs.makeTempDirectoryScoped();
320 + let forwardSpawns = 0;
321 + let hubAnswers = true;
322 + const spawner = ChildProcessSpawner.make((command) =>
323 + Effect.gen(function* () {
324 + if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
325 + const forwarding = command.args.includes("-L");
326 + // A fresh forward reaches a working hub again.
327 + if (forwarding && ++forwardSpawns > 1) hubAnswers = true;
328 + const output = JSON.stringify({
329 + nodePath: "/node",
330 + platforms: [{ platform: "ios", available: true }],
331 + hubPort: 1234,
332 + helpers: { serveSimAxSettings: null, serveSimCli: null },
333 + });
334 + return ChildProcessSpawner.makeHandle({
335 + pid: ChildProcessSpawner.ProcessId(123),
336 + stdout: Stream.make(new TextEncoder().encode(output)),
337 + stderr: Stream.empty,
338 + all: Stream.empty,
339 + exitCode: forwarding ? Effect.never : Effect.succeed(ChildProcessSpawner.ExitCode(0)),
340 + isRunning: Effect.succeed(forwarding),
341 + kill: () => Effect.void,
342 + stdin: Sink.drain,
343 + getInputFd: () => Sink.drain,
344 + getOutputFd: () => Stream.empty,
345 + unref: Effect.succeed(Effect.void),
346 + });
347 + }),
348 + );
349 + const host = yield* SshDeviceHost.make({
350 + kind: "ssh",
351 + id: "test",
352 + label: "Test",
353 + target: "test.example",
354 + }).pipe(
355 + Effect.provide(Layer.mergeAll(ServerConfig.layerTest(home, home), Net.layer)),
356 + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
357 + Effect.provideService(
358 + HttpClient.HttpClient,
359 + HttpClient.make((request) =>
360 + hubAnswers
361 + ? Effect.succeed(HttpClientResponse.fromWeb(request, new Response("ok")))
362 + : Effect.never,
363 + ),
364 + ),
365 + );
366 + yield* host.ensureReady(() => Effect.void);
367 + expect(forwardSpawns).toBe(1);
368 +
369 + // A loaded host can miss a probe or two without being gone; the forward stays up.
370 + hubAnswers = false;
371 + yield* TestClock.adjust("30 seconds");
372 + hubAnswers = true;
373 + yield* TestClock.adjust("2 minutes");
374 + expect(forwardSpawns).toBe(1);
375 +
376 + // A hub that keeps missing probes is still replaced.
377 + hubAnswers = false;
378 + // Recovery reset the miss count, so two new misses do not reconnect yet.
379 + yield* TestClock.adjust("30 seconds");
380 + expect(forwardSpawns).toBe(1);
381 + yield* TestClock.adjust("30 seconds");
382 + expect(forwardSpawns).toBe(2);
383 + yield* host.stop;
384 + }).pipe(
385 + Effect.scoped,
386 + Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, TestClock.layer())),
387 + ),
388 +);
apps/server/src/device/SshDeviceHost.ts
+7 -1
@@ -27,6 +27,8 @@ import * as DeviceHost from "./DeviceHost.ts";
27 import { makeDeviceHubOperations } from "./DeviceHubOperations.ts";
28 import { quoteRemoteArg, remoteDeviceEnvironment, remoteDeviceScript } from "./sshDeviceScript.ts";
29
30 +const UNHEALTHY_PROBE_LIMIT = 3;
31 +
32 const Probe = Schema.Struct({
33 nodePath: Schema.String,
34 tools: Schema.optional(DeviceToolVersions),
@@ -365,7 +367,10 @@ export const make = Effect.fn("SshDeviceHost.make")(function* (
367 : next;
368 yield* onStatus("ready");
369 // Reconnect also repairs helpers that died while SSH itself stayed connected.
370 + // A loaded host can miss a probe or two while still working, and reconnecting
371 + // then drops the forward in use, so only consecutive misses count.
372 const unhealthy = Effect.gen(function* () {
373 + let misses = 0;
374 while (true) {
375 yield* Effect.sleep("10 seconds");
376 const alive = yield* http.get(`${next.hub.origin}/readyz`).pipe(
@@ -380,7 +385,8 @@ export const make = Effect.fn("SshDeviceHost.make")(function* (
385 Effect.orElseSucceed(() => false),
386 )
387 : true;
383 - if (!alive || !daemonAlive) return;
388 + misses = alive && daemonAlive ? 0 : misses + 1;
389 + if (misses >= UNHEALTHY_PROBE_LIMIT) return;
390 }
391 });
392 yield* Effect.gen(function* () {