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

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

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

Synced Nov 21, 03:37 UTC

main
ts 2,012 lines 82.7 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off - Runs the generated runner in a real shell.
2 import * as NodeChildProcess from "node:child_process";
3 import * as NodeFS from "node:fs";
4 import * as NodeNet from "node:net";
5 import * as NodeOS from "node:os";
6 import * as NodePath from "node:path";
7
8 import { assert, describe, it } from "@effect/vitest";
9 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
10 import * as NodeServices from "@effect/platform-node/NodeServices";
11 import * as HostProcess from "@t3tools/shared/HostProcess";
12 import * as NetService from "@t3tools/shared/Net";
13 import * as Clock from "effect/Clock";
14 import * as Deferred from "effect/Deferred";
15 import * as Duration from "effect/Duration";
16 import * as Effect from "effect/Effect";
17 import * as Fiber from "effect/Fiber";
18 import * as FileSystem from "effect/FileSystem";
19 import * as Layer from "effect/Layer";
20 import * as Option from "effect/Option";
21 import * as Path from "effect/Path";
22 import * as PlatformError from "effect/PlatformError";
23 import * as Schema from "effect/Schema";
24 import * as Result from "effect/Result";
25 import * as Sink from "effect/Sink";
26 import * as Stream from "effect/Stream";
27 import { TestClock } from "effect/testing";
28 import { HttpClient, HttpClientError, HttpClientResponse } from "effect/http";
29 import * as FetchHttpClient from "effect/http/FetchHttpClient";
30 import { ChildProcess, ChildProcessSpawner } from "effect/process";
31
32 import { captureLauncher, captureLauncherFor, runLauncher } from "./launcherTestHarness.ts";
33 import * as SshAuth from "./auth.ts";
34 import { remoteStateKey } from "./command.ts";
35 import { SshCommandError, SshPasswordPromptError, SshReadinessError } from "./errors.ts";
36 import * as SshTunnel from "./tunnel.ts";
37
38 const isSshCommandError = Schema.is(SshCommandError);
39
40 const makeSuccessfulProcess = (stdout: string) => {
41 const stdoutStream = Stream.make(new TextEncoder().encode(stdout));
42 return ChildProcessSpawner.makeHandle({
43 pid: ChildProcessSpawner.ProcessId(123),
44 stdout: stdoutStream,
45 stderr: Stream.empty,
46 all: stdoutStream,
47 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
48 isRunning: Effect.succeed(false),
49 kill: () => Effect.void,
50 stdin: Sink.drain,
51 getInputFd: () => Sink.drain,
52 getOutputFd: () => Stream.empty,
53 unref: Effect.succeed(Effect.void),
54 });
55 };
56
57 const makeRunningProcess = (onKill: () => void) => {
58 let finish: ((exitCode: ChildProcessSpawner.ExitCode) => void) | null = null;
59 return ChildProcessSpawner.makeHandle({
60 pid: ChildProcessSpawner.ProcessId(123),
61 stdout: Stream.empty,
62 stderr: Stream.empty,
63 all: Stream.empty,
64 exitCode: Effect.callback<ChildProcessSpawner.ExitCode>((resume) => {
65 finish = (exitCode) => resume(Effect.succeed(exitCode));
66 return Effect.sync(() => {
67 finish = null;
68 });
69 }),
70 isRunning: Effect.succeed(true),
71 kill: () =>
72 Effect.sync(() => {
73 onKill();
74 finish?.(ChildProcessSpawner.ExitCode(143));
75 }),
76 stdin: Sink.drain,
77 getInputFd: () => Sink.drain,
78 getOutputFd: () => Stream.empty,
79 unref: Effect.succeed(Effect.void),
80 });
81 };
82
83 const testHttpClient = HttpClient.make((request) =>
84 Effect.succeed(HttpClientResponse.fromWeb(request, new Response("", { status: 200 }))),
85 );
86
87 const hangingHttpClient = HttpClient.make(() => Effect.never);
88
89 const testNetService = NetService.NetService.of({
90 canListenOnHost: () => Effect.succeed(true),
91 isPortAvailableOnLoopback: () => Effect.succeed(true),
92 hasListenerOnHost: () => Effect.succeed(true),
93 reserveLoopbackPort: () => Effect.succeed(41_773),
94 findAvailablePort: (preferred) => Effect.succeed(preferred),
95 });
96
97 function commandArgs(command: ChildProcess.Command): ReadonlyArray<string> {
98 return command._tag === "StandardCommand" ? command.args.map(String) : [];
99 }
100
101 const ARCHIVE = { archiveVersion: "1.2.3-preview.20260911.4" } as const;
102
103 // Emulates `t3 serve`: it takes the home owner lock at startup, then binds
104 // once a second server has tried the same home (or after 2s), so a concurrent
105 // launch always sees the home still unclaimed. Like the real server, it
106 // publishes server-runtime.json after it starts answering HTTP, if at all.
107 const fakeT3Serve = (publishDelayMs: number | null) => `const fs = require("node:fs");
108 const http = require("node:http");
109 const path = require("node:path");
110 const args = process.argv.slice(2);
111 const port = Number(args[args.indexOf("--port") + 1]);
112 const userdata = path.join(args[args.indexOf("--base-dir") + 1], "userdata");
113 const servers = path.join(process.env.HOME, "servers");
114 fs.mkdirSync(userdata, { recursive: true });
115 fs.appendFileSync(servers, process.pid + "\\n");
116 try {
117 fs.writeFileSync(path.join(userdata, "server.lock"), String(process.pid), { flag: "wx" });
118 } catch {
119 console.error("ServerAlreadyRunningError: another server owns " + userdata);
120 process.exit(1);
121 }
122 setTimeout(() => process.exit(0), 60_000);
123 const serve = (tries) => {
124 if (tries > 0 && fs.readFileSync(servers, "utf8").trim().split("\\n").length < 2) {
125 setTimeout(() => serve(tries - 1), 50);
126 return;
127 }
128 http.createServer((_, response) => response.end()).listen(port, "127.0.0.1", () => {
129 const origin = "http://127.0.0.1:" + port;
130 ${publishDelayMs === null ? "return;" : ""}
131 setTimeout(() => {
132 fs.writeFileSync(path.join(userdata, "server-runtime.json"), JSON.stringify({ pid: process.pid, port, origin }));
133 }, ${publishDelayMs});
134 });
135 };
136 serve(40);
137 `;
138
139 // The launch's POSIX bootstrap without the account login shell around it, so the
140 // test machine's login profile cannot move HOME.
141 const launchStateKey = (command: ChildProcess.Command) =>
142 /exec sh -s -- ([^"]+)"/u.exec(commandArgs(command).join(" "))?.[1];
143
144 // Runs the remote half of an SSH launch as a local shell
145 // whose HOME is the given directory. Every launch after the first starts once
146 // a server is running (or after 10s), which lines up the race deterministically.
147 const localRemoteShellLayer = (home: string) =>
148 Layer.effect(
149 ChildProcessSpawner.ChildProcessSpawner,
150 Effect.gen(function* () {
151 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
152 let launches = 0;
153 return ChildProcessSpawner.make((command) => {
154 if (command._tag !== "StandardCommand") return spawner.spawn(command);
155 const gate =
156 launches++ === 0
157 ? ""
158 : 'i=0; while [ ! -s "$HOME/servers" ] && [ "$i" -lt 200 ]; do sleep 0.05; i=$((i+1)); done; ';
159 return spawner.spawn(
160 ChildProcess.make(
161 "sh",
162 ["-c", `${gate}exec sh -s -- "$1"`, "sh", launchStateKey(command) ?? ""],
163 {
164 ...command.options,
165 env: { ...command.options.env, HOME: home },
166 // Like an SSH session ending, the shell's exit leaves its nohup'd server running.
167 detached: false,
168 },
169 ),
170 );
171 });
172 }),
173 );
174
175 describe("ssh tunnel scripts", () => {
176 it.live("distinguishes an incompatible remote Node from a missing executable", () =>
177 Effect.gen(function* () {
178 const fs = yield* FileSystem.FileSystem;
179 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-node-" });
180 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
181 for (const missing of [false, true]) {
182 const nodeFixture = missing
183 ? "command() { return 1; }; "
184 : 'node() { cat >/dev/null; printf "Remote node 18.20.8 does not satisfy required range >=999.0.0.\\n" >&2; return 1; }; ';
185 const remoteShell = ChildProcessSpawner.make((command) => {
186 if (command._tag !== "StandardCommand") return spawner.spawn(command);
187 return spawner.spawn(
188 ChildProcess.make(
189 "sh",
190 ["-c", `${nodeFixture}eval "$(cat)"`, "sh", ...commandArgs(command).slice(-1)],
191 {
192 ...command.options,
193 env: {
194 ...command.options.env,
195 HOME: home,
196 NVM_DIR: NodePath.join(home, ".nvm"),
197 FNM_DIR: NodePath.join(home, ".fnm"),
198 VOLTA_HOME: NodePath.join(home, ".volta"),
199 },
200 },
201 ),
202 );
203 });
204 const result = yield* SshTunnel.launchOrReuseRemoteServer(
205 { alias: "devbox", hostname: "devbox", username: null, port: null },
206 undefined,
207 { nodeScriptPath: "/unused/server.ts", nodeEngineRange: ">=999.0.0" },
208 ).pipe(
209 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, remoteShell),
210 Effect.result,
211 );
212 assert.isTrue(Result.isFailure(result));
213 if (!Result.isFailure(result)) return;
214 assert.isTrue(isSshCommandError(result.failure));
215 if (!isSshCommandError(result.failure)) return;
216 assert.equal(result.failure.exitCode, 1);
217 assert.equal(result.failure.stderr.includes("missing node on PATH"), missing);
218 assert.equal(
219 result.failure.stderr.includes("does not satisfy required range >=999.0.0"),
220 !missing,
221 );
222 }
223 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
224 );
225
226 it.live("reports only the failed launch's own output, not an earlier launch's log", () =>
227 Effect.gen(function* () {
228 const fs = yield* FileSystem.FileSystem;
229 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-launch-log-" });
230 const target = { alias: "devbox", hostname: "devbox", username: null, port: null };
231 const stateDir = NodePath.join(home, ".t3", "ssh-launch", yield* remoteStateKey(target));
232 const bin = NodePath.join(home, "bin");
233 yield* fs.makeDirectory(stateDir, { recursive: true });
234 yield* fs.makeDirectory(bin);
235 yield* fs.writeFileString(NodePath.join(stateDir, "server.log"), "EARLIER_LAUNCH\n");
236 // The new server exits without output. Readiness then fails after a process from the
237 // earlier launch writes through the log descriptor it still holds (fd 3).
238 yield* fs.writeFileString(
239 NodePath.join(bin, "node"),
240 '#!/bin/sh\n[ "$1" = - ] || exit 0\ncat >/dev/null\ncase "$2" in\n */port) echo 37731 ;;\n 37731) { printf "EARLIER_PROCESS\\n" >&3; } 2>/dev/null; exit 1 ;;\n *) exit 1 ;;\nesac\n',
241 { mode: 0o755 },
242 );
243 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
244 const remoteShell = ChildProcessSpawner.make((command) => {
245 if (command._tag !== "StandardCommand") return spawner.spawn(command);
246 return spawner.spawn(
247 ChildProcess.make(
248 "sh",
249 [
250 "-c",
251 'exec 3>>"$1/server.log"; exec sh -s -- "$2"',
252 "sh",
253 stateDir,
254 launchStateKey(command) ?? "",
255 ],
256 {
257 ...command.options,
258 env: { HOME: home, PATH: `${bin}:${process.env.PATH ?? ""}` },
259 extendEnv: false,
260 },
261 ),
262 );
263 });
264 const result = yield* SshTunnel.launchOrReuseRemoteServer(target, undefined, {
265 nodeScriptPath: "/unused/server.ts",
266 }).pipe(
267 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, remoteShell),
268 Effect.result,
269 );
270 assert.isTrue(Result.isFailure(result));
271 if (!Result.isFailure(result)) return;
272 assert.isTrue(isSshCommandError(result.failure));
273 if (!isSshCommandError(result.failure)) return;
274 assert.include(result.failure.stderr, "It wrote nothing to");
275 assert.notInclude(result.failure.stderr, "EARLIER_LAUNCH");
276 assert.notInclude(result.failure.stderr, "EARLIER_PROCESS");
277 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
278 );
279
280 it("rejects archive versions that are not a single exact version segment", () => {
281 for (const archiveVersion of [
282 "../other",
283 "1.2.3/evil",
284 "1.2.3\\evil",
285 "1.2.3-preview.1 x",
286 "1.2.3-preview.1\nrm -rf /",
287 "v1.2.3",
288 ]) {
289 assert.throws(
290 () => SshTunnel.buildRemoteT3RunnerScript({ archiveVersion }),
291 SshTunnel.SshInvalidArchiveVersionError,
292 undefined,
293 archiveVersion,
294 );
295 }
296 assert.include(
297 SshTunnel.buildRemoteT3RunnerScript(ARCHIVE),
298 "T3_ARCHIVE_VERSION='1.2.3-preview.20260911.4'",
299 );
300 });
301
302 it.each([
303 { host: "Darwin x86_64", system: "Darwin", machine: "x86_64", downloads: false },
304 { host: "Darwin arm64", system: "Darwin", machine: "arm64", downloads: true },
305 ])(
306 "only downloads a release archive the host can run: $host",
307 ({ system, machine, downloads }) => {
308 const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-ssh-runner-"));
309 try {
310 const bin = NodePath.join(root, "bin");
311 NodeFS.mkdirSync(bin);
312 NodeFS.mkdirSync(NodePath.join(root, "home"));
313 NodeFS.writeFileSync(
314 NodePath.join(bin, "uname"),
315 `#!/bin/sh\ncase "$1" in -s) echo ${system} ;; -m) echo ${machine} ;; esac\n`,
316 { mode: 0o755 },
317 );
318 NodeFS.writeFileSync(
319 NodePath.join(bin, "curl"),
320 "#!/bin/sh\necho 'download attempt' >&2\necho 'curl: (6) Could not resolve host: github.com' >&2\nexit 6\n",
321 { mode: 0o755 },
322 );
323 const runner = NodePath.join(root, "run-t3.sh");
324 NodeFS.writeFileSync(runner, SshTunnel.buildRemoteT3RunnerScript(ARCHIVE));
325 const result = NodeChildProcess.spawnSync("sh", [runner, "--version"], {
326 encoding: "utf8",
327 env: { HOME: NodePath.join(root, "home"), PATH: `${bin}:${process.env.PATH ?? ""}` },
328 });
329 assert.notEqual(result.status, 0);
330 if (downloads) {
331 assert.include(result.stderr, "download attempt");
332 // A bare curl error does not tell the user that t3 itself could not be downloaded.
333 assert.include(result.stderr, ARCHIVE.archiveVersion);
334 assert.include(result.stderr, "https://github.com/pingdotgg/t3code/releases/download");
335 } else {
336 assert.notInclude(result.stderr, "download attempt");
337 assert.include(result.stderr, "has no t3 release archive.");
338 }
339 } finally {
340 NodeFS.rmSync(root, { recursive: true, force: true });
341 }
342 },
343 );
344
345 it.each([
346 { installed: ARCHIVE.archiveVersion, status: 0, reuses: true },
347 { installed: ARCHIVE.archiveVersion, status: 1, reuses: false },
348 { installed: "9.9.9", status: 0, reuses: false },
349 ])(
350 "reuses a host t3 only at exactly the requested version: installed $installed, status $status",
351 ({ installed, status, reuses }) => {
352 const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-ssh-runner-"));
353 try {
354 const bin = NodePath.join(root, "bin");
355 NodeFS.mkdirSync(bin);
356 NodeFS.mkdirSync(NodePath.join(root, "home"));
357 // A host the release archive cannot run on, so only reuse can succeed.
358 NodeFS.writeFileSync(
359 NodePath.join(bin, "uname"),
360 `#!/bin/sh\ncase "$1" in -s) echo Darwin ;; -m) echo x86_64 ;; esac\n`,
361 { mode: 0o755 },
362 );
363 NodeFS.writeFileSync(
364 NodePath.join(bin, "t3"),
365 `#!/bin/sh\ncase "$1" in --version) echo "t3 v${installed}"; exit ${status} ;; *) echo "host t3 $*" ;; esac\n`,
366 { mode: 0o755 },
367 );
368 const runner = NodePath.join(root, "run-t3.sh");
369 NodeFS.writeFileSync(runner, SshTunnel.buildRemoteT3RunnerScript(ARCHIVE));
370 const result = NodeChildProcess.spawnSync("sh", [runner, "serve"], {
371 encoding: "utf8",
372 env: { HOME: NodePath.join(root, "home"), PATH: `${bin}:${process.env.PATH ?? ""}` },
373 });
374 if (reuses) {
375 assert.equal(result.status, 0);
376 assert.equal(result.stdout, "host t3 serve\n");
377 } else {
378 assert.notEqual(result.status, 0);
379 assert.include(result.stderr, "has no t3 release archive.");
380 }
381 } finally {
382 NodeFS.rmSync(root, { recursive: true, force: true });
383 }
384 },
385 );
386
387 // sha256sum and shasum escape a newline in a file name they print, so the home path has one.
388 const checksumTools =
389 NodeChildProcess.spawnSync("sh", ["-c", 'command -v "$1"', "sh", "shasum"]).status === 0
390 ? ["sha256sum", "shasum"]
391 : ["sha256sum"];
392 it.each(checksumTools)("names missing gzip after verifying with %s", (checksum) => {
393 const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-ssh-runner-"));
394 try {
395 const bin = NodePath.join(root, "bin");
396 const release = NodePath.join(root, "release");
397 const home = NodePath.join(root, "home\ncache");
398 const archive = `t3-${ARCHIVE.archiveVersion}-linux-x64.tar.gz`;
399 NodeFS.mkdirSync(bin);
400 NodeFS.mkdirSync(home);
401 NodeFS.mkdirSync(NodePath.join(release, "t3"), { recursive: true });
402 NodeFS.writeFileSync(NodePath.join(release, "t3", "t3"), "#!/bin/sh\necho ok\n", {
403 mode: 0o755,
404 });
405 NodeChildProcess.execFileSync("tar", ["-czf", archive, "t3"], { cwd: release });
406 NodeChildProcess.execFileSync("sh", ["-c", `sha256sum ${archive} > SHA256SUMS`], {
407 cwd: release,
408 });
409 for (const tool of [
410 "sh",
411 "mkdir",
412 "mktemp",
413 "cat",
414 "rm",
415 "grep",
416 "cut",
417 checksum,
418 "tar",
419 "mv",
420 ]) {
421 NodeFS.symlinkSync(
422 NodeChildProcess.execFileSync("sh", ["-c", `command -v ${tool}`], {
423 encoding: "utf8",
424 }).trim(),
425 NodePath.join(bin, tool),
426 );
427 }
428 NodeFS.writeFileSync(
429 NodePath.join(bin, "uname"),
430 '#!/bin/sh\ncase "$1" in -s) echo Linux ;; -m) echo x86_64 ;; esac\n',
431 { mode: 0o755 },
432 );
433 NodeFS.writeFileSync(
434 NodePath.join(bin, "curl"),
435 `#!/bin/sh\nfor last; do :; done\nfor a; do case "$a" in http*) src="${release}/\${a##*/}" ;; esac; done\nexec cat "$src" > "$last"\n`,
436 { mode: 0o755 },
437 );
438 const runner = NodePath.join(root, "run-t3.sh");
439 NodeFS.writeFileSync(runner, SshTunnel.buildRemoteT3RunnerScript(ARCHIVE));
440 const result = NodeChildProcess.spawnSync(NodePath.join(bin, "sh"), [runner, "--version"], {
441 encoding: "utf8",
442 env: { HOME: home, PATH: bin },
443 });
444 assert.notEqual(result.status, 0);
445 assert.match(result.stderr, /(missing|needs) gzip/);
446 assert.notInclude(result.stderr, "Cannot exec");
447 } finally {
448 NodeFS.rmSync(root, { recursive: true, force: true });
449 }
450 });
451
452 it("names missing tar before downloading the release archive", () => {
453 const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-ssh-runner-"));
454 try {
455 const bin = NodePath.join(root, "bin");
456 const home = NodePath.join(root, "home");
457 NodeFS.mkdirSync(bin);
458 NodeFS.mkdirSync(home);
459 for (const tool of ["sh", "mkdir", "mktemp", "cat", "rm"]) {
460 NodeFS.symlinkSync(
461 NodeChildProcess.execFileSync("sh", ["-c", `command -v ${tool}`], {
462 encoding: "utf8",
463 }).trim(),
464 NodePath.join(bin, tool),
465 );
466 }
467 NodeFS.writeFileSync(
468 NodePath.join(bin, "uname"),
469 '#!/bin/sh\ncase "$1" in -s) echo Linux ;; -m) echo x86_64 ;; esac\n',
470 { mode: 0o755 },
471 );
472 NodeFS.writeFileSync(
473 NodePath.join(bin, "curl"),
474 '#!/bin/sh\necho "download attempt" >&2\nexit 1\n',
475 { mode: 0o755 },
476 );
477 const runner = NodePath.join(root, "run-t3.sh");
478 NodeFS.writeFileSync(runner, SshTunnel.buildRemoteT3RunnerScript(ARCHIVE));
479 const result = NodeChildProcess.spawnSync(NodePath.join(bin, "sh"), [runner, "--version"], {
480 encoding: "utf8",
481 env: { HOME: home, PATH: bin },
482 });
483 assert.notEqual(result.status, 0);
484 assert.include(result.stderr, "needs tar");
485 assert.notInclude(result.stderr, "download attempt");
486 } finally {
487 NodeFS.rmSync(root, { recursive: true, force: true });
488 }
489 });
490
491 it.effect("accepts launch JSON after remote shell startup noise", () => {
492 const target = {
493 alias: "devbox",
494 hostname: "devbox.example.com",
495 username: "julius",
496 port: 2222,
497 } as const;
498 const spawner = ChildProcessSpawner.make(() =>
499 Effect.succeed(makeSuccessfulProcess('loaded nvm default\n{"remotePort":3774}\n')),
500 );
501 const layerSpawner = Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner);
502 const layerProcess = Layer.merge(
503 Layer.mergeAll(NodeServices.layer, NodeCrypto.layer),
504 layerSpawner,
505 );
506
507 return Effect.gen(function* () {
508 const result = yield* SshTunnel.launchOrReuseRemoteServer(target, undefined, ARCHIVE);
509 assert.equal(result.remotePort, 3774);
510 }).pipe(Effect.provide(layerProcess));
511 });
512
513 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
514 "loads login settings without changing the POSIX bootstrap or its state key",
515 () =>
516 Effect.gen(function* () {
517 const fs = yield* FileSystem.FileSystem;
518 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-login-" });
519 const startupFile = `${directory}/bash-env`;
520 const bash = `${directory}/bash`;
521 // System profiles can reset HOME. Suppress profile reads while BASH_ENV
522 // checks that the launcher still requests Bash login mode.
523 yield* fs.writeFileString(bash, '#!/bin/sh\nexec /bin/bash --noprofile "$@"\n');
524 yield* fs.chmod(bash, 0o755);
525 yield* fs.writeFileString(
526 startupFile,
527 "if shopt -q login_shell; then export T3_SSH_LOGIN_ENV=loaded; fi\n",
528 );
529 const target = { alias: "login-test", hostname: "login-test", username: null, port: null };
530 const remoteCommands: string[] = [];
531 const capture = ChildProcessSpawner.make((command) =>
532 Effect.sync(() => {
533 const args = commandArgs(command);
534 remoteCommands.push(args.slice(args.indexOf(target.alias) + 1).join(" "));
535 return makeSuccessfulProcess('{"remotePort":3774,"credential":"test"}\n');
536 }),
537 );
538 yield* SshTunnel.launchOrReuseRemoteServer(target, undefined, ARCHIVE).pipe(
539 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, capture),
540 );
541 yield* SshTunnel.issueRemotePairingToken(target, undefined, ARCHIVE).pipe(
542 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, capture),
543 );
544 yield* fs.writeFileString(
545 `${directory}/.zprofile`,
546 "if [[ -o login ]]; then export T3_SSH_LOGIN_ENV=loaded; fi\n",
547 );
548 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
549 const cases = [
550 { shell: bash, outerShell: "/bin/sh", expected: "loaded" },
551 { shell: "/bin/sh", outerShell: "/bin/sh", expected: "missing" },
552 { shell: "", outerShell: "/bin/sh", expected: "missing" },
553 { shell: "/unavailable/csh", outerShell: "/bin/sh", expected: "missing" },
554 { shell: "/unavailable/tcsh", outerShell: "/bin/sh", expected: "missing" },
555 { shell: "/unavailable/pwsh", outerShell: "/bin/sh", expected: "missing" },
556 ];
557 if (yield* fs.exists("/bin/zsh")) {
558 cases.push({ shell: "/bin/zsh", outerShell: "/bin/zsh", expected: "loaded" });
559 }
560 if (yield* fs.exists("/bin/tcsh")) {
561 cases.push({ shell: "/bin/tcsh", outerShell: "/bin/tcsh", expected: "missing" });
562 }
563 const environment = yield* HostProcess.Environment;
564 for (const folder of (environment.PATH ?? "").split(":")) {
565 const nu = `${folder}/nu`;
566 if (folder && (yield* fs.exists(nu))) {
567 cases.push({ shell: nu, outerShell: nu, expected: "missing" });
568 break;
569 }
570 }
571 for (const { shell, outerShell, expected, index, remoteCommand } of cases.flatMap((entry) =>
572 remoteCommands.map((remoteCommand, index) => ({ ...entry, remoteCommand, index })),
573 )) {
574 const child = yield* spawner.spawn(
575 ChildProcess.make(outerShell, ["-c", remoteCommand], {
576 env: {
577 PATH: environment.PATH ?? "/usr/bin:/bin",
578 HOME: directory,
579 XDG_CONFIG_HOME: directory,
580 SHELL: shell,
581 BASH_ENV: startupFile,
582 ZDOTDIR: directory,
583 T3_SSH_LOGIN_ENV: "",
584 },
585 stdin: {
586 stream: Stream.make(
587 new TextEncoder().encode('printf "%s:%s" "${T3_SSH_LOGIN_ENV:-missing}" "$1"'),
588 ),
589 endOnDone: true,
590 },
591 }),
592 );
593 const [stdout, stderr, exitCode] = yield* Effect.all(
594 [
595 child.stdout.pipe(
596 Stream.decodeText(),
597 Stream.runFold(
598 () => "",
599 (a, b) => a + b,
600 ),
601 ),
602 child.stderr.pipe(
603 Stream.decodeText(),
604 Stream.runFold(
605 () => "",
606 (a, b) => a + b,
607 ),
608 ),
609 child.exitCode,
610 ],
611 { concurrency: "unbounded" },
612 );
613 assert.equal(Number(exitCode), 0, stderr);
614 // First-run shell messages can precede the bootstrap response.
615 const response = stdout.trim().split(/\r?\n/u).at(-1) ?? "";
616 assert.match(
617 response,
618 new RegExp(index === 0 ? `^${expected}:[a-f0-9]{16}$` : `^${expected}:$`),
619 shell || "unset SHELL",
620 );
621 }
622 }).pipe(Effect.provide(Layer.merge(NodeServices.layer, NodeCrypto.layer))),
623 );
624
625 it.effect("accepts pretty-printed pairing JSON after remote shell startup noise", () => {
626 const target = {
627 alias: "devbox",
628 hostname: "devbox.example.com",
629 username: "julius",
630 port: 2222,
631 } as const;
632 const spawner = ChildProcessSpawner.make(() =>
633 Effect.succeed(
634 makeSuccessfulProcess(`loaded nvm default
635 {
636 "id": "88941235-6ed5-4184-a2ff-5339e2075958",
637 "credential": "LCL4R2TPHDKQ",
638 "scopes": ["orchestration:read"],
639 "expiresAt": "2026-04-29T01:01:20.994Z"
640 }
641
642 `),
643 ),
644 );
645 const layerSpawner = Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner);
646 const layerProcess = Layer.merge(
647 Layer.mergeAll(NodeServices.layer, NodeCrypto.layer),
648 layerSpawner,
649 );
650 return Effect.gen(function* () {
651 const result = yield* SshTunnel.issueRemotePairingToken(target, undefined, ARCHIVE);
652 assert.equal(result.credential, "LCL4R2TPHDKQ");
653 }).pipe(Effect.provide(layerProcess));
654 });
655
656 it.effect("retains a bounded diagnostic tail when the tunnel exits before readiness", () =>
657 Effect.gen(function* () {
658 const diagnostic = "Connection refused: café";
659 const output = new TextEncoder().encode(
660 "old diagnostics\n" + "x".repeat(32_768) + "\n" + diagnostic,
661 );
662 const spawner = ChildProcessSpawner.make((command) =>
663 Effect.succeed(
664 commandArgs(command).includes("-N")
665 ? {
666 ...makeSuccessfulProcess(""),
667 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),
668 stderr: Stream.make(output.subarray(0, output.length - 1), output.subarray(-1)),
669 }
670 : makeSuccessfulProcess('{"remotePort":3773}\n'),
671 ),
672 );
673 const layer = Layer.mergeAll(
674 NodeServices.layer,
675 NodeCrypto.layer,
676 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
677 Layer.succeed(HttpClient.HttpClient, hangingHttpClient),
678 Layer.succeed(NetService.NetService, testNetService),
679 SshAuth.SshPasswordPrompt.disabledLayer,
680 SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
681 );
682 yield* Effect.gen(function* () {
683 const manager = yield* SshTunnel.SshEnvironmentManager;
684 const result = yield* Effect.result(
685 manager.ensureEnvironment({
686 alias: "devbox",
687 hostname: "devbox",
688 username: null,
689 port: null,
690 }),
691 );
692 assert.isTrue(Result.isFailure(result));
693 if (Result.isFailure(result)) {
694 assert.instanceOf(result.failure, SshCommandError);
695 if (isSshCommandError(result.failure)) {
696 assert.equal(result.failure.exitCode, 1);
697 assert.equal(result.failure.stderr.length, 16_384);
698 assert.isTrue(result.failure.stderr.endsWith(diagnostic));
699 assert.equal(result.failure.message, result.failure.stderr.trim());
700 }
701 }
702 }).pipe(Effect.provide(layer), Effect.scoped);
703 }),
704 );
705
706 it.effect("reports the real exit when reading tunnel diagnostics fails", () =>
707 Effect.gen(function* () {
708 const diagnostic = "Permission denied (publickey)";
709 const spawner = ChildProcessSpawner.make((command) =>
710 Effect.succeed(
711 commandArgs(command).includes("-N")
712 ? {
713 ...makeSuccessfulProcess(""),
714 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(255)),
715 stderr: Stream.make(new TextEncoder().encode(diagnostic)).pipe(
716 Stream.concat(
717 Stream.fail(
718 PlatformError.systemError({
719 _tag: "Unknown",
720 module: "Stream",
721 method: "read",
722 description: "stderr read failed",
723 }),
724 ),
725 ),
726 ),
727 }
728 : makeSuccessfulProcess('{"remotePort":3773}\n'),
729 ),
730 );
731 const layer = Layer.mergeAll(
732 NodeServices.layer,
733 NodeCrypto.layer,
734 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
735 Layer.succeed(HttpClient.HttpClient, hangingHttpClient),
736 Layer.succeed(NetService.NetService, testNetService),
737 SshAuth.SshPasswordPrompt.disabledLayer,
738 SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
739 );
740 yield* Effect.gen(function* () {
741 const manager = yield* SshTunnel.SshEnvironmentManager;
742 const result = yield* Effect.result(
743 manager.ensureEnvironment({
744 alias: "devbox",
745 hostname: "devbox",
746 username: null,
747 port: null,
748 }),
749 );
750 assert.isTrue(Result.isFailure(result));
751 if (Result.isFailure(result)) {
752 assert.instanceOf(result.failure, SshCommandError);
753 if (isSshCommandError(result.failure)) {
754 assert.equal(result.failure.exitCode, 255);
755 assert.equal(result.failure.stderr, diagnostic);
756 }
757 }
758 }).pipe(Effect.provide(layer), Effect.scoped);
759 }),
760 );
761
762 it.effect("waits for a tunnel whose ssh connection outlasts the readiness timeout", () =>
763 Effect.gen(function* () {
764 const LOCAL_PORT = 41_773;
765 // ssh spends ConnectTimeout on each unreachable address before the forward listens.
766 const SSH_CONNECT_MS = 25_000;
767 let forwardListensAt = Number.POSITIVE_INFINITY;
768 const tunnelSpawned = yield* Deferred.make<void>();
769 const forwardListening = (port: number) =>
770 Clock.currentTimeMillis.pipe(
771 Effect.map((now) => port === LOCAL_PORT && now >= forwardListensAt),
772 );
773 const spawner = ChildProcessSpawner.make((command) =>
774 commandArgs(command).includes("-N")
775 ? Effect.gen(function* () {
776 forwardListensAt = (yield* Clock.currentTimeMillis) + SSH_CONNECT_MS;
777 yield* Deferred.succeed(tunnelSpawned, undefined);
778 return makeRunningProcess(() => undefined);
779 })
780 : Effect.succeed(makeSuccessfulProcess('{"remotePort":3773}\n')),
781 );
782 const httpClient = HttpClient.make((request) =>
783 forwardListening(LOCAL_PORT).pipe(
784 Effect.flatMap((listening) =>
785 listening
786 ? Effect.succeed(
787 HttpClientResponse.fromWeb(request, new Response("", { status: 200 })),
788 )
789 : Effect.fail(
790 new HttpClientError.HttpClientError({
791 reason: new HttpClientError.TransportError({
792 request,
793 description: `connect ECONNREFUSED 127.0.0.1:${LOCAL_PORT}`,
794 }),
795 }),
796 ),
797 ),
798 ),
799 );
800 const net = NetService.NetService.of({
801 ...testNetService,
802 reserveLoopbackPort: () => Effect.succeed(LOCAL_PORT),
803 canListenOnHost: (port) =>
804 forwardListening(port).pipe(Effect.map((listening) => !listening)),
805 isPortAvailableOnLoopback: (port) =>
806 forwardListening(port).pipe(Effect.map((listening) => !listening)),
807 hasListenerOnHost: (port) => forwardListening(port),
808 });
809 const layer = Layer.mergeAll(
810 NodeServices.layer,
811 NodeCrypto.layer,
812 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
813 Layer.succeed(HttpClient.HttpClient, httpClient),
814 Layer.succeed(NetService.NetService, net),
815 SshAuth.SshPasswordPrompt.disabledLayer,
816 SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
817 );
818 yield* Effect.gen(function* () {
819 const manager = yield* SshTunnel.SshEnvironmentManager;
820 const ensure = yield* Effect.forkChild(
821 manager.ensureEnvironment({
822 alias: "devbox",
823 hostname: "devbox",
824 username: null,
825 port: null,
826 }),
827 );
828 yield* Deferred.await(tunnelSpawned);
829 yield* TestClock.adjust(Duration.seconds(60));
830 const environment = yield* Fiber.join(ensure);
831 assert.equal(environment.httpBaseUrl, `http://127.0.0.1:${LOCAL_PORT}/`);
832 }).pipe(Effect.provide(layer), Effect.scoped);
833 }),
834 );
835
836 it.effect.each([
837 // Over a ~1s RTT link each request through the forward takes a few round trips.
838 { condition: "responses take over a second", reconnectResponseMs: 2_500 },
839 // An alive server whose event loop stalls (e.g. on a blocked fsync) answers late.
840 { condition: "server stalls for 20 seconds", reconnectResponseMs: 20_000 },
841 // ssh closes the forwarded connection when nothing listens on the remote port.
842 { condition: "server is gone", reconnectResponseMs: 0, serverGone: true },
843 ])("connects and reuses a tunnel whose $condition", ({ reconnectResponseMs, serverGone }) =>
844 Effect.gen(function* () {
845 const RESPONSE_MS = 2_500;
846 const tunnelSpawned = yield* Deferred.make<void>();
847 const reconnectProbed = yield* Deferred.make<void>();
848 const forwardReplaced = yield* Deferred.make<void>();
849 let forwards = 0;
850 let connected = false;
851 const spawner = ChildProcessSpawner.make((command) =>
852 commandArgs(command).includes("-N")
853 ? Effect.sync(() => ++forwards).pipe(
854 Effect.flatMap((count) =>
855 Deferred.succeed(count === 1 ? tunnelSpawned : forwardReplaced, undefined),
856 ),
857 Effect.as(makeRunningProcess(() => undefined)),
858 )
859 : Effect.succeed(makeSuccessfulProcess('{"remotePort":3773}\n')),
860 );
861 const httpClient = HttpClient.make((request) =>
862 Effect.gen(function* () {
863 if (connected) yield* Deferred.succeed(reconnectProbed, undefined);
864 yield* Effect.sleep(Duration.millis(connected ? reconnectResponseMs : RESPONSE_MS));
865 if (connected && serverGone) {
866 return yield* new HttpClientError.HttpClientError({
867 reason: new HttpClientError.TransportError({
868 request,
869 description: "socket hang up",
870 }),
871 });
872 }
873 return HttpClientResponse.fromWeb(request, new Response("", { status: 200 }));
874 }),
875 );
876 const layer = Layer.mergeAll(
877 NodeServices.layer,
878 NodeCrypto.layer,
879 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
880 Layer.succeed(HttpClient.HttpClient, httpClient),
881 Layer.succeed(NetService.NetService, testNetService),
882 SshAuth.SshPasswordPrompt.disabledLayer,
883 SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
884 );
885 yield* Effect.gen(function* () {
886 const manager = yield* SshTunnel.SshEnvironmentManager;
887 const ensure = yield* Effect.forkChild(
888 Effect.result(
889 manager.ensureEnvironment({
890 alias: "devbox",
891 hostname: "devbox",
892 username: null,
893 port: null,
894 }),
895 ),
896 );
897 yield* Deferred.await(tunnelSpawned);
898 yield* TestClock.adjust(Duration.seconds(60));
899 const result = yield* Fiber.join(ensure);
900 assert.isTrue(
901 Result.isSuccess(result),
902 Result.isFailure(result) ? result.failure.message : undefined,
903 );
904 if (Result.isFailure(result)) return;
905
906 connected = true;
907 const reconnect = yield* Effect.forkChild(manager.ensureEnvironment(result.success.target));
908 yield* Deferred.await(reconnectProbed);
909 yield* TestClock.adjust(Duration.seconds(60));
910 const outcome = yield* Effect.raceFirst(
911 Fiber.join(reconnect).pipe(Effect.map((environment) => environment.httpBaseUrl)),
912 Deferred.await(forwardReplaced).pipe(Effect.as("replaced the forward")),
913 );
914 assert.equal(outcome, serverGone ? "replaced the forward" : result.success.httpBaseUrl);
915 }).pipe(Effect.provide(layer), Effect.scoped);
916 }),
917 );
918
919 it.effect("reconnects a saved alias through the port its ssh config names now", () =>
920 Effect.gen(function* () {
921 let configPort = 53_791;
922 const tunnelPorts: Array<string | undefined> = [];
923 const spawner = ChildProcessSpawner.make((command) => {
924 const args = commandArgs(command);
925 if (args.includes("-G")) {
926 return Effect.succeed(
927 makeSuccessfulProcess(`hostname 127.0.0.1\nuser developer\nport ${configPort}\n`),
928 );
929 }
930 if (args.includes("-N")) {
931 tunnelPorts.push(args[args.indexOf("-p") + 1]);
932 return Effect.succeed(makeRunningProcess(() => undefined));
933 }
934 return Effect.succeed(makeSuccessfulProcess('{"remotePort":3773}\n'));
935 });
936 const layer = Layer.mergeAll(
937 NodeServices.layer,
938 NodeCrypto.layer,
939 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
940 Layer.succeed(HttpClient.HttpClient, testHttpClient),
941 Layer.succeed(NetService.NetService, testNetService),
942 SshAuth.SshPasswordPrompt.disabledLayer,
943 SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
944 );
945 yield* Effect.gen(function* () {
946 const manager = yield* SshTunnel.SshEnvironmentManager;
947 const added = yield* manager.ensureEnvironment({
948 alias: "lima-sandbox",
949 hostname: "lima-sandbox",
950 username: null,
951 port: null,
952 });
953 // The VM restarts on a new port; the client reconnects with the target it saved.
954 yield* manager.disconnectEnvironment(added.target);
955 configPort = 53_812;
956 yield* manager.ensureEnvironment(added.target);
957 assert.deepStrictEqual(tunnelPorts, ["53791", "53812"]);
958 }).pipe(Effect.provide(layer), Effect.scoped);
959 }),
960 );
961
962 it.effect("fails with a readiness error when ssh never connects", () =>
963 Effect.gen(function* () {
964 const tunnelSpawned = yield* Deferred.make<void>();
965 let tunnelKilled = false;
966 const spawner = ChildProcessSpawner.make((command) =>
967 commandArgs(command).includes("-N")
968 ? Deferred.succeed(tunnelSpawned, undefined).pipe(
969 Effect.as(
970 makeRunningProcess(() => {
971 tunnelKilled = true;
972 }),
973 ),
974 )
975 : Effect.succeed(makeSuccessfulProcess('{"remotePort":3773}\n')),
976 );
977 const layer = Layer.mergeAll(
978 NodeServices.layer,
979 NodeCrypto.layer,
980 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
981 Layer.succeed(HttpClient.HttpClient, hangingHttpClient),
982 Layer.succeed(
983 NetService.NetService,
984 NetService.NetService.of({
985 ...testNetService,
986 hasListenerOnHost: () => Effect.succeed(false),
987 }),
988 ),
989 SshAuth.SshPasswordPrompt.disabledLayer,
990 SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
991 );
992 yield* Effect.gen(function* () {
993 const manager = yield* SshTunnel.SshEnvironmentManager;
994 const ensure = yield* Effect.forkChild(
995 Effect.result(
996 manager.ensureEnvironment({
997 alias: "devbox",
998 hostname: "devbox",
999 username: null,
1000 port: null,
1001 }),
1002 ),
1003 );
1004 yield* Deferred.await(tunnelSpawned);
1005 yield* TestClock.adjust(Duration.minutes(2));
1006 const result = yield* Fiber.join(ensure);
1007 assert.isTrue(Result.isFailure(result));
1008 if (Result.isFailure(result)) {
1009 assert.instanceOf(result.failure, SshReadinessError);
1010 assert.include(result.failure.message, "SSH connection to devbox did not come up");
1011 }
1012 assert.isTrue(tunnelKilled);
1013 }).pipe(Effect.provide(layer), Effect.scoped);
1014 }),
1015 );
1016
1017 it.effect.each([
1018 {
1019 name: "does not ask for a password when the agent cannot sign an accepted key",
1020 stderr:
1021 'sign_and_send_pubkey: signing failed for ED25519 "/home/julius/.ssh/id_ed25519" from agent: communication with agent failed\r\n' +
1022 "julius@devbox: Permission denied (publickey,password).\r\n",
1023 expectedPromptAttempts: [],
1024 },
1025 {
1026 name: "asks for a password when signing failure has no agent provenance",
1027 stderr:
1028 'sign_and_send_pubkey: signing failed for ED25519-CERT "key-cert.pub": agent refused operation\n' +
1029 "julius@devbox: Permission denied (publickey,password).\n",
1030 expectedPromptAttempts: [1],
1031 },
1032 {
1033 name: "does not ask for a password when the host key cannot be verified",
1034 stderr: "Host key verification failed.\r\n",
1035 expectedPromptAttempts: [],
1036 },
1037 {
1038 name: "asks for a password when the server rejects every key",
1039 stderr: "julius@devbox: Permission denied (publickey,password).\r\n",
1040 expectedPromptAttempts: [1],
1041 },
1042 ])("$name", ({ stderr, expectedPromptAttempts }) =>
1043 Effect.gen(function* () {
1044 const promptAttempts: Array<number> = [];
1045 const spawner = ChildProcessSpawner.make((command) =>
1046 Effect.succeed(
1047 commandArgs(command).includes("-G")
1048 ? makeSuccessfulProcess("")
1049 : {
1050 ...makeSuccessfulProcess(""),
1051 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(255)),
1052 stderr: Stream.make(new TextEncoder().encode(stderr)),
1053 },
1054 ),
1055 );
1056 const layer = Layer.mergeAll(
1057 NodeServices.layer,
1058 NodeCrypto.layer,
1059 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
1060 Layer.succeed(HttpClient.HttpClient, testHttpClient),
1061 Layer.succeed(NetService.NetService, testNetService),
1062 Layer.succeed(
1063 SshAuth.SshPasswordPrompt,
1064 SshAuth.SshPasswordPrompt.of({
1065 isAvailable: true,
1066 request: (request) =>
1067 Effect.sync(() => {
1068 promptAttempts.push(request.attempt);
1069 return null;
1070 }),
1071 }),
1072 ),
1073 SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
1074 );
1075 yield* Effect.gen(function* () {
1076 const manager = yield* SshTunnel.SshEnvironmentManager;
1077 const result = yield* Effect.result(
1078 manager.ensureEnvironment({
1079 alias: "devbox",
1080 hostname: "devbox",
1081 username: null,
1082 port: null,
1083 }),
1084 );
1085 assert.isTrue(Result.isFailure(result));
1086 assert.deepEqual(promptAttempts, expectedPromptAttempts);
1087 if (Result.isFailure(result)) {
1088 if (expectedPromptAttempts.length === 0) {
1089 assert.instanceOf(result.failure, SshCommandError);
1090 if (isSshCommandError(result.failure)) {
1091 assert.equal(result.failure.stderr, stderr);
1092 }
1093 } else {
1094 assert.instanceOf(result.failure, SshPasswordPromptError);
1095 }
1096 }
1097 }).pipe(Effect.provide(layer), Effect.scoped);
1098 }),
1099 );
1100
1101 it.effect.each(["local tunnel", "remote server"] as const)(
1102 "waits for %s shutdown before reconnecting the same target",
1103 (stalledStep) =>
1104 Effect.gen(function* () {
1105 const shutdownStarted = yield* Deferred.make<void>();
1106 const finishShutdown = yield* Deferred.make<void>();
1107 const reconnectsStarted = yield* Deferred.make<void>();
1108 const pauseShutdown = Deferred.succeed(shutdownStarted, undefined).pipe(
1109 Effect.andThen(Deferred.await(finishShutdown)),
1110 );
1111 let resolutions = 0;
1112 let launches = 0;
1113 let tunnels = 0;
1114 let stops = 0;
1115 let remoteRunning = false;
1116 const target = { alias: "devbox", hostname: "devbox", username: null, port: null };
1117 const spawner = ChildProcessSpawner.make((command) =>
1118 Effect.gen(function* () {
1119 const args = commandArgs(command);
1120 const isTarget = args.includes(target.alias);
1121 if (args.includes("-G")) {
1122 if (isTarget && ++resolutions === 4) {
1123 yield* Deferred.succeed(reconnectsStarted, undefined);
1124 }
1125 return makeSuccessfulProcess("");
1126 }
1127 if (args.includes("-N")) {
1128 const tunnel = makeRunningProcess(() => undefined);
1129 if (isTarget && ++tunnels === 1 && stalledStep === "local tunnel") {
1130 return {
1131 ...tunnel,
1132 kill: (options?: ChildProcess.KillOptions) =>
1133 pauseShutdown.pipe(Effect.andThen(tunnel.kill(options))),
1134 };
1135 }
1136 return tunnel;
1137 }
1138 if (args.includes("-c")) {
1139 if (isTarget) {
1140 launches += 1;
1141 remoteRunning = true;
1142 }
1143 return makeSuccessfulProcess('{"remotePort":3773}\n');
1144 }
1145 const stop = makeSuccessfulProcess('{"stopped":true}\n');
1146 if (!isTarget) return stop;
1147 const pause = ++stops === 1 && stalledStep === "remote server";
1148 return {
1149 ...stop,
1150 exitCode: (pause ? pauseShutdown : Effect.void).pipe(
1151 Effect.andThen(
1152 Effect.sync(() => {
1153 remoteRunning = false;
1154 return ChildProcessSpawner.ExitCode(0);
1155 }),
1156 ),
1157 ),
1158 };
1159 }),
1160 );
1161 const layer = Layer.mergeAll(
1162 NodeServices.layer,
1163 NodeCrypto.layer,
1164 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
1165 Layer.succeed(HttpClient.HttpClient, testHttpClient),
1166 Layer.succeed(NetService.NetService, testNetService),
1167 SshAuth.SshPasswordPrompt.disabledLayer,
1168 SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
1169 );
1170 yield* Effect.gen(function* () {
1171 const manager = yield* SshTunnel.SshEnvironmentManager;
1172 yield* manager.ensureEnvironment(target);
1173 const disconnect = yield* Effect.forkChild(manager.disconnectEnvironment(target));
1174 yield* Deferred.await(shutdownStarted);
1175 const firstReconnect = yield* Effect.forkChild(manager.ensureEnvironment(target));
1176 const secondReconnect = yield* Effect.forkChild(manager.ensureEnvironment(target));
1177 yield* Deferred.await(reconnectsStarted);
1178
1179 yield* manager.ensureEnvironment({
1180 alias: "other",
1181 hostname: "other",
1182 username: null,
1183 port: null,
1184 });
1185 yield* TestClock.adjust(Duration.zero);
1186 const launchesBeforeShutdown = launches;
1187 yield* Deferred.succeed(finishShutdown, undefined);
1188 yield* Fiber.join(disconnect);
1189 const first = yield* Fiber.join(firstReconnect);
1190 const second = yield* Fiber.join(secondReconnect);
1191
1192 assert.equal(launchesBeforeShutdown, 1);
1193 assert.equal(launches, 2);
1194 assert.equal(tunnels, 2);
1195 assert.isTrue(remoteRunning);
1196 assert.equal(first.httpBaseUrl, second.httpBaseUrl);
1197 }).pipe(
1198 Effect.ensuring(Deferred.succeed(finishShutdown, undefined)),
1199 Effect.provide(layer),
1200 Effect.scoped,
1201 );
1202 }),
1203 );
1204
1205 it.effect.each([
1206 { name: "rides a live ControlMaster", masterLive: true },
1207 { name: "keeps the auth failure without a live ControlMaster", masterLive: false },
1208 ])("when the tunnel cannot sign in again, $name", ({ masterLive }) =>
1209 Effect.gen(function* () {
1210 const denied = "julius@devbox: Permission denied (publickey,keyboard-interactive).\n";
1211 const openedForwards: Array<string | undefined> = [];
1212 const cancelledForwards: Array<string | undefined> = [];
1213 const spawner = ChildProcessSpawner.make((command) =>
1214 Effect.sync(() => {
1215 const args = commandArgs(command);
1216 const forward = args.includes("-L") ? args[args.indexOf("-L") + 1] : undefined;
1217 const muxCommand = args.includes("-O") ? args[args.indexOf("-O") + 1] : undefined;
1218 const failed = (stderr: string) => ({
1219 ...makeSuccessfulProcess(""),
1220 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(255)),
1221 stderr: Stream.make(new TextEncoder().encode(stderr)),
1222 });
1223 if (args.includes("-G")) {
1224 return makeSuccessfulProcess("hostname devbox\ncontrolpath /tmp/t3-cm\n");
1225 }
1226 if (muxCommand === "check") {
1227 return masterLive
1228 ? makeSuccessfulProcess("")
1229 : failed("Control socket connect: ENOENT\n");
1230 }
1231 if (muxCommand === "cancel") {
1232 cancelledForwards.push(forward);
1233 return makeSuccessfulProcess("");
1234 }
1235 if (forward !== undefined) {
1236 // Only the signed-in master can pass the host's one-time-code prompt.
1237 if (!masterLive || args.includes("ControlPath=none")) return failed(denied);
1238 openedForwards.push(forward);
1239 return muxCommand === "forward"
1240 ? makeSuccessfulProcess("")
1241 : makeRunningProcess(() => undefined);
1242 }
1243 return makeSuccessfulProcess('{"remotePort":3773}\n');
1244 }),
1245 );
1246 const layer = Layer.mergeAll(
1247 NodeServices.layer,
1248 NodeCrypto.layer,
1249 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
1250 Layer.succeed(
1251 HttpClient.HttpClient,
1252 HttpClient.make((request) =>
1253 openedForwards.length > 0 ? testHttpClient.execute(request) : Effect.never,
1254 ),
1255 ),
1256 Layer.succeed(NetService.NetService, testNetService),
1257 SshAuth.SshPasswordPrompt.disabledLayer,
1258 SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
1259 );
1260 const target = { alias: "devbox", hostname: "devbox", username: null, port: null };
1261 yield* Effect.gen(function* () {
1262 const manager = yield* SshTunnel.SshEnvironmentManager;
1263 const result = yield* Effect.result(manager.ensureEnvironment(target));
1264 if (!masterLive) {
1265 assert.isTrue(Result.isFailure(result));
1266 if (Result.isFailure(result)) {
1267 assert.instanceOf(result.failure, SshCommandError);
1268 assert.include(result.failure.message, "Permission denied");
1269 }
1270 assert.deepEqual(openedForwards, []);
1271 return;
1272 }
1273 assert.equal(Result.isFailure(result) ? result.failure.message : "connected", "connected");
1274 assert.lengthOf(openedForwards, 1);
1275 yield* manager.disconnectEnvironment(target);
1276 // The master keeps a forward after the client that asked for it exits.
1277 assert.deepEqual(cancelledForwards, openedForwards);
1278 }).pipe(Effect.provide(layer), Effect.scoped);
1279 }),
1280 );
1281
1282 it.live("concurrent launches on one remote home record the server that owns it", () =>
1283 // The second row's server never publishes server-runtime.json.
1284 Effect.forEach([1_500, null, "wrapper"] as const, (variant) =>
1285 Effect.gen(function* () {
1286 const publishDelayMs = variant === "wrapper" ? 0 : variant;
1287 const fs = yield* FileSystem.FileSystem;
1288 const path = yield* Path.Path;
1289 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-launch-" });
1290 const serversFile = path.join(home, "servers");
1291 yield* Effect.addFinalizer(() =>
1292 fs.readFileString(serversFile).pipe(
1293 Effect.map((pids) => {
1294 for (const pid of pids.split("\n").filter(Boolean)) {
1295 try {
1296 process.kill(Number(pid), "SIGKILL");
1297 } catch {}
1298 }
1299 }),
1300 Effect.ignore,
1301 ),
1302 );
1303 const serverScript = path.join(home, "fake-t3.cjs");
1304 yield* fs.writeFileString(serverScript, fakeT3Serve(publishDelayMs));
1305 const wrapperScript = path.join(home, "wrapper.cjs");
1306 yield* fs.writeFileString(
1307 wrapperScript,
1308 `const child = require("node:child_process").spawn(process.execPath, [${JSON.stringify(serverScript)}, ...process.argv.slice(2)], { stdio: "inherit" });
1309 child.on("exit", (code) => process.exit(code ?? 1));`,
1310 );
1311 // A launch that died holding the lock must not block or double-admit later ones.
1312 const launchLock = path.join(home, ".t3", "ssh-launch", ".launch.lock");
1313 yield* fs.makeDirectory(path.dirname(launchLock), { recursive: true });
1314 yield* fs.writeFileString(launchLock, "999999999\n");
1315 const targets = [
1316 { alias: "devbox", hostname: "devbox", username: null, port: null },
1317 { alias: "devbox-tailnet", hostname: "devbox-tailnet", username: null, port: null },
1318 ];
1319
1320 const launches = yield* Effect.forEach(
1321 targets,
1322 (target) =>
1323 SshTunnel.launchOrReuseRemoteServer(target, undefined, {
1324 nodeScriptPath: variant === "wrapper" ? wrapperScript : serverScript,
1325 }),
1326 { concurrency: "unbounded" },
1327 ).pipe(Effect.provide(localRemoteShellLayer(home)));
1328
1329 const ownerPid = Number(
1330 yield* fs.readFileString(path.join(home, ".t3", "userdata", "server.lock")),
1331 );
1332 assert.sameMembers(
1333 launches.map((launch) => launch.remoteServerKind),
1334 ["managed", "external"],
1335 );
1336 assert.equal(launches[0]!.remotePort, launches[1]!.remotePort);
1337 assert.deepEqual((yield* fs.readFileString(serversFile)).trim().split("\n"), [
1338 String(ownerPid),
1339 ]);
1340 for (const [index, launch] of launches.entries()) {
1341 if (launch.remoteServerKind !== "managed") continue;
1342 const stateKey = yield* remoteStateKey(targets[index]!);
1343 const recordedPid = yield* fs.readFileString(
1344 path.join(home, ".t3", "ssh-launch", stateKey, "pid"),
1345 );
1346 assert.equal(
1347 Number(recordedPid.trim()),
1348 ownerPid,
1349 `${targets[index]!.alias} reported a managed server, but its recorded PID is not the server that owns the remote home`,
1350 );
1351 }
1352 assert.isFalse(yield* fs.exists(launchLock));
1353 }).pipe(Effect.scoped),
1354 ).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
1355 );
1356 });
1357
1358 // On Linux kill -0 accepts a thread ID and kill <tid> signals its whole process,
1359 // but ps lists only thread-group leaders. After a reboot the managed pid file can
1360 // name a thread or a whole process that is not our server.
1361 describe.runIf(HostProcess.Platform.defaultValue() === "linux")("stale managed pid file", () => {
1362 const FAKE_SERVER = `const fs = require("node:fs");
1363 const http = require("node:http");
1364 const path = require("node:path");
1365 const args = process.argv.slice(2);
1366 const flag = (name) => args[args.indexOf(name) + 1];
1367 const server = http.createServer((request, response) => {
1368 // Closes the listener but stays up, like a server stuck in its teardown.
1369 if (request.url === "/stop") {
1370 server.close();
1371 setInterval(() => {}, 1 << 30);
1372 response.setHeader("connection", "close");
1373 }
1374 response.end("ok");
1375 });
1376 server.listen(Number(flag("--port") ?? 0), "127.0.0.1", () => {
1377 const port = server.address().port;
1378 const base = flag("--base-dir");
1379 if (base) {
1380 fs.mkdirSync(path.join(base, "userdata"), { recursive: true });
1381 fs.writeFileSync(
1382 path.join(base, "userdata", "server-runtime.json"),
1383 JSON.stringify({ version: 1, pid: process.pid, port, origin: "http://127.0.0.1:" + port }),
1384 );
1385 }
1386 console.log(port);
1387 });
1388 `;
1389 const target = { alias: "devbox", hostname: "devbox", username: null, port: null } as const;
1390 const staleIds = ["a thread of an unrelated process", "an unrelated process"] as const;
1391
1392 const isRunning = (pid: number) =>
1393 Effect.flatMap(FileSystem.FileSystem, (fs) => fs.readFileString(`/proc/${pid}/stat`)).pipe(
1394 Effect.map((stat) => !/^\d+ \(.*\) Z/u.test(stat)),
1395 Effect.orElseSucceed(() => false),
1396 );
1397
1398 const prepare = (staleId: (typeof staleIds)[number]) =>
1399 Effect.gen(function* () {
1400 const fs = yield* FileSystem.FileSystem;
1401 const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
1402 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-stale-pid-" });
1403 const home = `${root}/home`;
1404 const fakeServer = `${root}/fake-t3.cjs`;
1405 yield* fs.makeDirectory(home, { recursive: true });
1406 yield* fs.writeFileString(fakeServer, FAKE_SERVER);
1407 // Runs the remote command locally under the temp HOME. setsid keeps a launched server alive
1408 // when the command's process group goes away, as sshd would.
1409 const sshAsLocalShell = ChildProcessSpawner.make((command) => {
1410 if (command._tag !== "StandardCommand" || command.command !== "ssh") {
1411 return realSpawner.spawn(command);
1412 }
1413 if (command.args.includes("-G")) return Effect.succeed(makeSuccessfulProcess(""));
1414 const stateKey = launchStateKey(command);
1415 const remoteArgs =
1416 stateKey === undefined
1417 ? command.args.slice(command.args.indexOf("--") + 2)
1418 : ["sh", "-s", "--", ...stateKey.split(" ")];
1419 return realSpawner.spawn(
1420 ChildProcess.make("setsid", ["-w", ...remoteArgs], {
1421 ...command.options,
1422 env: { PATH: process.env.PATH ?? "", HOME: home },
1423 extendEnv: false,
1424 }),
1425 );
1426 });
1427 const holder = yield* realSpawner.spawn(ChildProcess.make("node", [fakeServer]));
1428 const holderPort = Number(
1429 Option.getOrThrow(
1430 yield* holder.stdout.pipe(Stream.decodeText(), Stream.splitLines, Stream.runHead),
1431 ),
1432 );
1433 const tid = Number(
1434 (yield* fs.readDirectory(`/proc/${holder.pid}/task`)).find(
1435 (task) => task !== `${holder.pid}`,
1436 ),
1437 );
1438 assert.isTrue(Number.isInteger(tid));
1439 return {
1440 fs,
1441 home,
1442 fakeServer,
1443 holder,
1444 holderPort,
1445 sshAsLocalShell,
1446 staleId: staleId === "an unrelated process" ? holder.pid : tid,
1447 stateDir: `${home}/.t3/ssh-launch/${yield* remoteStateKey(target)}`,
1448 };
1449 });
1450
1451 it.live.each(staleIds)("launches a fresh server when the managed pid file names %s", (kind) =>
1452 Effect.gen(function* () {
1453 const { fs, home, fakeServer, holderPort, sshAsLocalShell, staleId, stateDir } =
1454 yield* prepare(kind);
1455 const launch = SshTunnel.launchOrReuseRemoteServer(target, undefined, {
1456 nodeScriptPath: fakeServer,
1457 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, sshAsLocalShell));
1458 const readPid = fs
1459 .readFileString(`${stateDir}/pid`)
1460 .pipe(Effect.map((pid) => Number(pid.trim())));
1461 const launchedPids: Array<number> = [];
1462 yield* Effect.addFinalizer(() =>
1463 Effect.forEach(launchedPids, (pid) =>
1464 Effect.sync(() => {
1465 try {
1466 process.kill(pid, "SIGKILL");
1467 } catch {}
1468 }),
1469 ),
1470 );
1471
1472 const first = yield* launch;
1473 const firstPid = yield* readPid;
1474 launchedPids.push(firstPid);
1475 assert.deepEqual(yield* launch, first);
1476 assert.equal(yield* readPid, firstPid);
1477
1478 // Reboot: our server is gone and its recorded PID now names something that
1479 // answers on the recorded port.
1480 process.kill(firstPid, "SIGKILL");
1481 yield* fs.remove(`${home}/.t3/userdata/server-runtime.json`);
1482 yield* fs.writeFileString(`${stateDir}/pid`, `${staleId}\n`);
1483 yield* fs.writeFileString(`${stateDir}/port`, `${holderPort}\n`);
1484
1485 const relaunched = yield* launch;
1486 const pid = yield* readPid;
1487 if (pid !== staleId) launchedPids.push(pid);
1488 assert.notEqual(
1489 relaunched.remotePort,
1490 holderPort,
1491 `the launcher kept ${kind} as its running server`,
1492 );
1493 assert.isTrue(yield* isRunning(pid));
1494 }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
1495 );
1496
1497 it.live("a reconnect names the previous server while it is still shutting down", () =>
1498 Effect.gen(function* () {
1499 const { fs, fakeServer, sshAsLocalShell, stateDir } = yield* prepare("an unrelated process");
1500 const launch = SshTunnel.launchOrReuseRemoteServer(target, undefined, {
1501 nodeScriptPath: fakeServer,
1502 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, sshAsLocalShell));
1503
1504 const first = yield* launch;
1505 const pid = Number((yield* fs.readFileString(`${stateDir}/pid`)).trim());
1506 yield* Effect.addFinalizer(() =>
1507 Effect.sync(() => {
1508 try {
1509 process.kill(pid, "SIGKILL");
1510 } catch {}
1511 }),
1512 );
1513 yield* HttpClient.get(`http://127.0.0.1:${first.remotePort}/stop`).pipe(
1514 Effect.flatMap((response) => response.text),
1515 Effect.provide(FetchHttpClient.layer),
1516 );
1517
1518 const error = yield* Effect.flip(launch);
1519 assert.include(
1520 error.message,
1521 `The previous remote T3 server (PID ${pid}) is still shutting down.`,
1522 );
1523 assert.isTrue(yield* isRunning(pid), "the launcher signalled the stopping server");
1524 }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
1525 );
1526
1527 it.live("an update replaces the managed server after its connection is renamed", () =>
1528 Effect.gen(function* () {
1529 const { fs, home, fakeServer, sshAsLocalShell, stateDir } =
1530 yield* prepare("an unrelated process");
1531 const updatedServer = `${home}/fake-t3-next.cjs`;
1532 yield* fs.writeFileString(updatedServer, FAKE_SERVER);
1533 const renamed = { ...target, alias: "devbox-renamed" };
1534 const launch = (
1535 launchTarget: typeof renamed,
1536 nodeScriptPath: string,
1537 launcherId: string | null = "a".repeat(32),
1538 ) =>
1539 SshTunnel.launchOrReuseRemoteServer(
1540 launchTarget,
1541 undefined,
1542 { nodeScriptPath },
1543 launcherId,
1544 ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, sshAsLocalShell));
1545 const runtimePid = fs
1546 .readFileString(`${home}/.t3/userdata/server-runtime.json`)
1547 .pipe(Effect.map((runtime) => Number(JSON.parse(runtime).pid)));
1548 const launchedPids: Array<number> = [];
1549 yield* Effect.addFinalizer(() =>
1550 Effect.forEach(launchedPids, (pid) =>
1551 Effect.sync(() => {
1552 try {
1553 process.kill(pid, "SIGKILL");
1554 } catch {}
1555 }),
1556 ),
1557 );
1558
1559 assert.equal((yield* launch(target, fakeServer)).remoteServerKind, "managed");
1560 const originalPid = yield* runtimePid;
1561 launchedPids.push(originalPid);
1562
1563 // Another desktop on another version shares the server; it must not take it over.
1564 const otherDesktop = yield* launch(
1565 { ...target, alias: "devbox-other" },
1566 updatedServer,
1567 "b".repeat(32),
1568 );
1569 assert.equal(otherDesktop.remoteServerKind, "external");
1570 assert.isTrue(yield* isRunning(originalPid), "another desktop replaced the server");
1571
1572 const afterRename = yield* launch(renamed, fakeServer);
1573 assert.equal(yield* runtimePid, originalPid, "renaming alone restarted the server");
1574 assert.equal(
1575 yield* fs.readFileString(`${stateDir}/managed`),
1576 "external\n",
1577 "the old alias still owns the server, so its disconnect would stop it",
1578 );
1579 const afterUpdate = yield* launch(renamed, updatedServer);
1580 launchedPids.push(yield* runtimePid);
1581 assert.deepEqual(
1582 [afterRename.remoteServerKind, afterUpdate.remoteServerKind],
1583 ["managed", "managed"],
1584 );
1585 assert.isFalse(
1586 yield* isRunning(originalPid),
1587 "the update left the server launched under the old alias running",
1588 );
1589
1590 // A rename and an update in one reconnect still replace the server.
1591 const updatedPid = yield* runtimePid;
1592 const renamedAndUpdated = yield* launch({ ...target, alias: "devbox-again" }, fakeServer);
1593 launchedPids.push(yield* runtimePid);
1594 assert.equal(renamedAndUpdated.remoteServerKind, "managed");
1595 assert.isFalse(yield* isRunning(updatedPid), "a rename with an update kept the old server");
1596
1597 // A server that a launch without an ID started under a reused key is not this desktop's.
1598 const again = { ...target, alias: "devbox-again" };
1599 process.kill(yield* runtimePid, "SIGKILL");
1600 assert.equal((yield* launch(again, fakeServer, null)).remoteServerKind, "managed");
1601 const unownedPid = yield* runtimePid;
1602 launchedPids.push(unownedPid);
1603 const final = yield* launch({ ...target, alias: "devbox-final" }, updatedServer);
1604 assert.equal(final.remoteServerKind, "external");
1605 assert.isTrue(yield* isRunning(unownedPid), "a stale launcher ID claimed another's server");
1606 }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
1607 );
1608
1609 it.live.each(staleIds)("disconnecting does not signal %s named by the managed pid file", (kind) =>
1610 Effect.gen(function* () {
1611 const { fs, holder, sshAsLocalShell, staleId, stateDir } = yield* prepare(kind);
1612 yield* fs.makeDirectory(stateDir, { recursive: true });
1613 yield* fs.writeFileString(`${stateDir}/pid`, `${staleId}\n`);
1614 yield* fs.writeFileString(`${stateDir}/managed`, "managed\n");
1615 const layer = Layer.mergeAll(
1616 NodeServices.layer,
1617 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, sshAsLocalShell),
1618 Layer.succeed(HttpClient.HttpClient, testHttpClient),
1619 Layer.succeed(NetService.NetService, testNetService),
1620 SshAuth.SshPasswordPrompt.disabledLayer,
1621 SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
1622 );
1623
1624 yield* Effect.flatMap(SshTunnel.SshEnvironmentManager, (manager) =>
1625 manager.disconnectEnvironment(target),
1626 ).pipe(Effect.provide(layer));
1627 assert.isTrue(yield* isRunning(holder.pid), `disconnecting signalled ${kind}`);
1628 }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
1629 );
1630 });
1631
1632 // On macOS, Node can bind 127.0.0.1:<port> while another process (such as the
1633 // desktop app with network access on) listens on *:<port>. Linux refuses that
1634 // bind unless both sockets set SO_REUSEPORT, so on Linux the preload adds it to
1635 // the picker's probe and the holder sets it too.
1636
1637 // Reuse decisions live in generated shell, so drive the real launch script in
1638 // a temporary HOME. Each archive version gets a pre-installed fake runtime
1639 // whose server writes the default runtime file the way `t3 serve` does.
1640 describe("archive launch script", () => {
1641 const windowsHost = HostProcess.Platform.defaultValue() === "win32";
1642 const FAKE_T3 = `const fs = require("node:fs");
1643 const http = require("node:http");
1644 const net = require("node:net");
1645 const path = require("node:path");
1646 const [version, ...args] = process.argv.slice(2);
1647 const flag = (name) => args[args.indexOf(name) + 1];
1648 const probe = (port) =>
1649 new Promise((resolve) => {
1650 http
1651 .get({ hostname: "127.0.0.1", port, path: "/", timeout: 1000 }, (response) => {
1652 response.resume();
1653 resolve(response.statusCode === 200);
1654 })
1655 .once("error", () => resolve(false));
1656 });
1657 const canBind = (port) =>
1658 new Promise((resolve) => {
1659 const server = net.createServer();
1660 server.once("error", () => resolve(false));
1661 server.listen(port, "127.0.0.1", () => server.close(() => resolve(true)));
1662 });
1663 (async () => {
1664 if (args[0] === "--version") return console.log(version);
1665 if (args[0] === "serve") {
1666 const server = http.createServer((_request, response) => response.end("ok"));
1667 server.listen(Number(flag("--port")), "127.0.0.1", () => {
1668 const port = server.address().port;
1669 const userdata = path.join(flag("--base-dir"), "userdata");
1670 fs.mkdirSync(userdata, { recursive: true });
1671 fs.writeFileSync(
1672 path.join(userdata, "server-runtime.json"),
1673 JSON.stringify({
1674 version: 1,
1675 pid: process.pid,
1676 port,
1677 origin: "http://127.0.0.1:" + port,
1678 startedAt: new Date().toISOString(),
1679 serverVersion: version,
1680 }),
1681 );
1682 });
1683 return;
1684 }
1685 const [helper, a, b, c] = args.slice(1);
1686 if (helper === "pick-port") {
1687 const start = Number(fs.existsSync(a) ? fs.readFileSync(a, "utf8") : b);
1688 for (let port = start; port < start + Number(c); port += 1) {
1689 if (await canBind(port)) return process.stdout.write(String(port));
1690 }
1691 } else if (helper === "wait-ready") {
1692 const deadline = Date.now() + Number(b);
1693 while (Date.now() < deadline) {
1694 if (await probe(Number(a))) return;
1695 await new Promise((resolve) => setTimeout(resolve, 100));
1696 }
1697 } else if (helper === "runtime-port") {
1698 try {
1699 const runtime = JSON.parse(fs.readFileSync(a, "utf8"));
1700 process.kill(runtime.pid, 0);
1701 return process.stdout.write(runtime.pid + " " + runtime.port);
1702 } catch {}
1703 } else if (helper === "keep-server") {
1704 try {
1705 const runtime = JSON.parse(fs.readFileSync(a, "utf8"));
1706 const order = runtime.serverVersion.localeCompare(version, "en", { numeric: true });
1707 if (String(runtime.pid) === b && order >= 0) return;
1708 } catch {}
1709 }
1710 process.exitCode = 1;
1711 })();
1712 `;
1713
1714 const isAlive = (pid: number) => {
1715 try {
1716 process.kill(pid, 0);
1717 return true;
1718 } catch {
1719 return false;
1720 }
1721 };
1722
1723 const freeLoopbackPort = Effect.callback<number>((resume) => {
1724 const server = NodeNet.createServer();
1725 server.listen(0, "127.0.0.1", () => {
1726 const address = server.address();
1727 server.close(() =>
1728 resume(Effect.succeed(typeof address === "object" && address ? address.port : 0)),
1729 );
1730 });
1731 });
1732
1733 const runScript = (home: string, args: ReadonlyArray<string>) =>
1734 Effect.gen(function* () {
1735 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
1736 const child = yield* spawner.spawn(ChildProcess.make("sh", args, { env: { HOME: home } }));
1737 const collect = (stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>) =>
1738 stream.pipe(Stream.decodeText(), Stream.mkString);
1739 const [stdout, stderr, exitCode] = yield* Effect.all(
1740 [collect(child.stdout), collect(child.stderr), child.exitCode.pipe(Effect.map(Number))],
1741 { concurrency: "unbounded" },
1742 );
1743 return { stdout, stderr, exitCode };
1744 });
1745
1746 // Installs a fake runtime per version so the runner never downloads. The
1747 // seeded remembered port keeps the test off the default 3773, which a live
1748 // T3 on this machine may own.
1749 const prepareLaunch = (versions: ReadonlyArray<string>) =>
1750 Effect.gen(function* () {
1751 const fs = yield* FileSystem.FileSystem;
1752 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-archive-launch-" });
1753 const home = `${root}/home`;
1754 const stateDir = `${home}/.t3/ssh-launch/test-state`;
1755 const fakeT3 = `${root}/fake-t3.cjs`;
1756 yield* fs.makeDirectory(stateDir, { recursive: true });
1757 yield* fs.writeFileString(`${stateDir}/port`, `${yield* freeLoopbackPort}\n`);
1758 yield* fs.writeFileString(fakeT3, FAKE_T3);
1759 for (const version of versions) {
1760 const runtimeDir = `${home}/.t3/runtime/versions/${version}`;
1761 yield* fs.makeDirectory(runtimeDir, { recursive: true });
1762 yield* fs.writeFileString(
1763 `${runtimeDir}/t3`,
1764 `#!/bin/sh\nexec node '${fakeT3}' '${version}' "$@"\n`,
1765 );
1766 yield* fs.chmod(`${runtimeDir}/t3`, 0o755);
1767 yield* fs.writeFileString(`${runtimeDir}/.install-complete`, `${version}\n`);
1768 }
1769 const readManagedPid = fs
1770 .readFileString(`${stateDir}/pid`)
1771 .pipe(Effect.map((pid) => Number(pid.trim())));
1772 const readServerVersion = fs
1773 .readFileString(`${home}/.t3/userdata/server-runtime.json`)
1774 .pipe(Effect.map((raw) => (JSON.parse(raw) as { serverVersion: string }).serverVersion));
1775 // Launched servers outlive the script, so stop each PID it recorded.
1776 const launchedPids = new Set<number>();
1777 yield* Effect.addFinalizer(() =>
1778 readManagedPid.pipe(
1779 Effect.map((pid) => [...launchedPids, pid]),
1780 Effect.orElseSucceed(() => [...launchedPids]),
1781 Effect.map((pids) => {
1782 for (const pid of pids) {
1783 if (isAlive(pid)) process.kill(pid, "SIGKILL");
1784 }
1785 }),
1786 ),
1787 );
1788 const connect = (archiveVersion: string) =>
1789 Effect.gen(function* () {
1790 const launch = `${root}/launch-${archiveVersion}.sh`;
1791 yield* fs.writeFileString(launch, SshTunnel.buildRemoteLaunchScript({ archiveVersion }));
1792 const result = yield* runScript(home, [launch, "test-state"]);
1793 assert.equal(result.exitCode, 0, result.stderr);
1794 const pid = yield* readManagedPid;
1795 launchedPids.add(pid);
1796 return pid;
1797 });
1798 return { connect, readManagedPid, readServerVersion };
1799 });
1800
1801 it.live.skipIf(windowsHost)(
1802 "a reconnect from an older client keeps a newer managed server running",
1803 () =>
1804 Effect.gen(function* () {
1805 const { connect, readServerVersion } = yield* prepareLaunch(["0.1.5", "0.1.6"]);
1806 const newerPid = yield* connect("0.1.6");
1807 assert.equal(yield* readServerVersion, "0.1.6");
1808
1809 const pidAfterOlderClient = yield* connect("0.1.5");
1810 assert.isTrue(
1811 isAlive(newerPid),
1812 "the 0.1.6 managed server was killed by a 0.1.5 reconnect",
1813 );
1814 assert.equal(pidAfterOlderClient, newerPid);
1815 assert.equal(yield* readServerVersion, "0.1.6");
1816 }).pipe(Effect.provide(NodeServices.layer)),
1817 );
1818
1819 // An NFS home can carry a ~/.t3 symlink whose target exists only on another host.
1820 it.live.skipIf(windowsHost)("launches through a ~/.t3 symlink whose target is missing", () =>
1821 Effect.gen(function* () {
1822 const fs = yield* FileSystem.FileSystem;
1823 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-dangling-home-" });
1824 const home = `${root}/home`;
1825 const target = `${root}/shared/t3`;
1826 yield* fs.makeDirectory(home, { recursive: true });
1827 yield* fs.symlink(target, `${home}/.t3`);
1828 const launch = `${root}/launch.sh`;
1829 yield* fs.writeFileString(
1830 launch,
1831 SshTunnel.buildRemoteLaunchScript({
1832 archiveVersion: "0.1.6",
1833 releaseBaseUrl: "http://127.0.0.1:9",
1834 }),
1835 );
1836 const result = yield* runScript(home, [launch, "test-state"]);
1837 assert.notInclude(result.stderr, "File exists");
1838 assert.isTrue(yield* fs.exists(`${target}/ssh-launch/test-state`), result.stderr);
1839 }).pipe(Effect.provide(NodeServices.layer)),
1840 );
1841
1842 it.live.skipIf(windowsHost)("a newer client still replaces an older managed server", () =>
1843 Effect.gen(function* () {
1844 const { connect, readServerVersion } = yield* prepareLaunch(["0.1.5", "0.1.6"]);
1845 const olderPid = yield* connect("0.1.5");
1846
1847 const newerPid = yield* connect("0.1.6");
1848 assert.notEqual(newerPid, olderPid);
1849 assert.isFalse(isAlive(olderPid));
1850 assert.equal(yield* readServerVersion, "0.1.6");
1851 }).pipe(Effect.provide(NodeServices.layer)),
1852 );
1853 });
1854
1855 describe("SSH launch during desktop update", () => {
1856 it.effect("archive launcher reuses the replacement without host Node", () =>
1857 Effect.gen(function* () {
1858 const script = yield* captureLauncherFor({ archiveVersion: "1.2.3" });
1859 const result = runLauncher(script, {
1860 archive: true,
1861 marker: { startedAt: 0, expiresAt: 60_000 },
1862 readyAt: 2_000,
1863 });
1864 assert.deepEqual(result.output, { remotePort: 4888, serverKind: "external" });
1865 assert.equal(result.now, 2_000);
1866 assert.isFalse(result.events.some((event) => event.startsWith("start ")));
1867 }),
1868 );
1869
1870 it.effect.each(["managed", "external"] as const)(
1871 "reuses recorded %s server without runtime metadata or an update wait",
1872 (recordedKind) =>
1873 Effect.gen(function* () {
1874 const result = runLauncher(yield* captureLauncher, {
1875 marker: { startedAt: 0, expiresAt: 60_000 },
1876 recordedKind,
1877 });
1878 assert.deepEqual(result.output, { remotePort: 5999, serverKind: recordedKind });
1879 assert.equal(result.now, 0);
1880 assert.isFalse(result.events.some((event) => event.startsWith("kill ")));
1881 assert.isFalse(result.events.some((event) => event.startsWith("start ")));
1882 }),
1883 );
1884
1885 it.effect.each([
1886 ["malformed", "broken"],
1887 ["future", { startedAt: 1_000, expiresAt: 61_000 }],
1888 ["expired", { startedAt: -60_000, expiresAt: 0 }],
1889 ] as const)("preserves %s markers without waiting", ([, marker]) =>
1890 Effect.gen(function* () {
1891 const result = runLauncher(yield* captureLauncher, { marker });
1892 assert.equal(result.now, 0);
1893 assert.isTrue(result.markerExists);
1894 assert.deepEqual(result.output, { remotePort: 5999, serverKind: "managed" });
1895 }),
1896 );
1897
1898 it.effect("bounds the wait when the host clock jumps back", () =>
1899 Effect.gen(function* () {
1900 const result = runLauncher(yield* captureLauncher, {
1901 marker: { startedAt: 0, expiresAt: 60_000 },
1902 clockJump: -30_000,
1903 });
1904 assert.deepEqual(result.output, { remotePort: 5999, serverKind: "managed" });
1905 assert.isBelow(result.events.filter((event) => event.startsWith("runtime ")).length, 62);
1906 }),
1907 );
1908
1909 it.effect("refreshes runtime after readiness removes the marker between polls", () =>
1910 Effect.gen(function* () {
1911 const result = runLauncher(yield* captureLauncher, {
1912 marker: { startedAt: 0, expiresAt: 60_000 },
1913 readyAt: 8_000,
1914 clearMarkerOnReady: true,
1915 });
1916 assert.deepEqual(result.output, { remotePort: 4888, serverKind: "external" });
1917 assert.equal(result.now, 8_000);
1918 assert.isFalse(result.markerExists);
1919 assert.isFalse(result.events.some((event) => event.startsWith("start ")));
1920 assert.isFalse(result.events.some((event) => event.startsWith("kill ")));
1921 }),
1922 );
1923
1924 it.effect("reuses a runtime published before the first marker read", () =>
1925 Effect.gen(function* () {
1926 const result = runLauncher(yield* captureLauncher, {
1927 marker: { startedAt: 0, expiresAt: 60_000 },
1928 publishAfterFirstRuntimeRead: true,
1929 });
1930 assert.deepEqual(result.output, { remotePort: 4888, serverKind: "external" });
1931 assert.equal(result.now, 0);
1932 assert.isFalse(result.events.some((event) => event.startsWith("start ")));
1933 }),
1934 );
1935
1936 it.effect("waits across the eight-second installer gap and reuses the replacement server", () =>
1937 Effect.gen(function* () {
1938 const result = runLauncher(yield* captureLauncher, {
1939 marker: { startedAt: 0, expiresAt: 60_000 },
1940 readyAt: 8_000,
1941 });
1942 assert.deepEqual(result.output, { remotePort: 4888, serverKind: "external" });
1943 assert.equal(result.now, 8_000);
1944 assert.isFalse(result.events.some((event) => event.startsWith("start ")));
1945 assert.isAtLeast(result.events.filter((event) => event.startsWith("runtime ")).length, 2);
1946 }),
1947 );
1948
1949 it.effect("keeps waiting while a stale runtime record points at a dead port", () =>
1950 Effect.gen(function* () {
1951 const result = runLauncher(yield* captureLauncher, {
1952 marker: { startedAt: 0, expiresAt: 60_000 },
1953 initialPort: 3773,
1954 readyAt: 3_000,
1955 });
1956 assert.deepEqual(result.output, { remotePort: 4888, serverKind: "external" });
1957 assert.equal(result.now, 3_000);
1958 assert.isFalse(result.events.some((event) => event.startsWith("start ")));
1959 }),
1960 );
1961
1962 it.effect("starts a server when the marker expires without a replacement becoming ready", () =>
1963 Effect.gen(function* () {
1964 const result = runLauncher(yield* captureLauncher, {
1965 marker: { startedAt: 0, expiresAt: 60_000 },
1966 });
1967 assert.deepEqual(result.output, { remotePort: 5999, serverKind: "managed" });
1968 assert.isAtLeast(result.now, 60_000);
1969 assert.isAtMost(result.now, 61_000);
1970 assert.isTrue(result.markerExists, "an expired marker is ignored, not deleted");
1971 assert.equal(result.events.filter((event) => event.startsWith("start ")).length, 1);
1972 }),
1973 );
1974
1975 it.effect.each([false, true])(
1976 "waits for a boot service between two servers (archive: %s)",
1977 (archive) =>
1978 Effect.gen(function* () {
1979 const script = yield* archive
1980 ? captureLauncherFor({ archiveVersion: "1.2.3" })
1981 : captureLauncher;
1982 const result = runLauncher(script, { archive, serviceServing: true, readyAt: 5_000 });
1983 assert.deepEqual(result.output, { remotePort: 4888, serverKind: "external" });
1984 assert.equal(result.now, 5_000);
1985 assert.isFalse(result.events.some((event) => event.startsWith("start ")));
1986 }),
1987 );
1988
1989 it.effect("fails without starting a second server when the home's owner never answers", () =>
1990 Effect.gen(function* () {
1991 const result = runLauncher(yield* captureLauncher, {
1992 serviceServing: true,
1993 allowFailure: true,
1994 });
1995 assert.equal(result.status, 1);
1996 assert.include(result.stderr, "no second server was started");
1997 assert.equal(result.now, 60_000);
1998 assert.isFalse(result.events.some((event) => event.startsWith("start ")));
1999 }),
2000 );
2001
2002 it.effect("starts immediately without a marker", () =>
2003 Effect.gen(function* () {
2004 const result = runLauncher(yield* captureLauncher, {});
2005 assert.deepEqual(result.output, { remotePort: 5999, serverKind: "managed" });
2006 assert.equal(result.now, 0);
2007 assert.equal(result.events.filter((event) => event.startsWith("start ")).length, 1);
2008 assert.equal(result.events.filter((event) => event.startsWith("marker ")).length, 1);
2009 assert.equal(result.events.filter((event) => event.startsWith("runtime ")).length, 1);
2010 }),
2011 );
2012 });