@devries-cameron20 / t3code-1 / commits / 058d953646
Fork 0

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

fix(ssh): load remote profiles with the account login shell (#23934)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

tatsuaki watanabe committed Nov 13, 2026 at 07:48 UTC 058d953646412c21441dc3454203a7fde9c828ed
2 files changed +129 -15
packages/ssh/src/tunnel.test.ts
+120 -14
@@ -133,7 +133,12 @@ const serve = (tries) => {
133 serve(40);
134 `;
135
136 -// Runs the remote half of an SSH command (`sh -l -s -- <key>`) as a local shell
136 +// The launch's POSIX bootstrap without the account login shell around it, so the
137 +// test machine's login profile cannot move HOME.
138 +const launchStateKey = (command: ChildProcess.Command) =>
139 + /exec sh -s -- (\S+)"/u.exec(commandArgs(command).join(" "))?.[1];
140 +
141 +// Runs the remote half of an SSH launch as a local shell
142 // whose HOME is the given directory. Every launch after the first starts once
143 // a server is running (or after 10s), which lines up the race deterministically.
144 const localRemoteShellLayer = (home: string) =>
@@ -151,7 +156,7 @@ const localRemoteShellLayer = (home: string) =>
156 return spawner.spawn(
157 ChildProcess.make(
158 "sh",
154 - ["-c", `${gate}exec sh -s -- "$1"`, "sh", ...commandArgs(command).slice(-1)],
159 + ["-c", `${gate}exec sh -s -- "$1"`, "sh", launchStateKey(command) ?? ""],
160 {
161 ...command.options,
162 env: { ...command.options.env, HOME: home },
@@ -296,12 +301,8 @@ describe("ssh tunnel scripts", () => {
301 username: "julius",
302 port: 2222,
303 } as const;
299 - const spawnedCommands: Array<ReadonlyArray<string>> = [];
300 - const spawner = ChildProcessSpawner.make((command) =>
301 - Effect.sync(() => {
302 - spawnedCommands.push(commandArgs(command));
303 - return makeSuccessfulProcess('loaded nvm default\n{"remotePort":3774}\n');
304 - }),
304 + const spawner = ChildProcessSpawner.make(() =>
305 + Effect.succeed(makeSuccessfulProcess('loaded nvm default\n{"remotePort":3774}\n')),
306 );
307 const layerSpawner = Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner);
308 const layerProcess = Layer.merge(
@@ -312,10 +313,112 @@ describe("ssh tunnel scripts", () => {
313 return Effect.gen(function* () {
314 const result = yield* SshTunnel.launchOrReuseRemoteServer(target, undefined, ARCHIVE);
315 assert.equal(result.remotePort, 3774);
315 - assert.deepEqual(spawnedCommands[0]?.slice(-5, -1), ["sh", "-l", "-s", "--"]);
316 }).pipe(Effect.provide(layerProcess));
317 });
318
319 + it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
320 + "loads login settings without changing the POSIX bootstrap or its state key",
321 + () =>
322 + Effect.gen(function* () {
323 + const fs = yield* FileSystem.FileSystem;
324 + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-login-" });
325 + const startupFile = `${directory}/bash-env`;
326 + const bash = `${directory}/bash`;
327 + // System profiles can reset HOME. Suppress profile reads while BASH_ENV
328 + // checks that the launcher still requests Bash login mode.
329 + yield* fs.writeFileString(bash, '#!/bin/sh\nexec /bin/bash --noprofile "$@"\n');
330 + yield* fs.chmod(bash, 0o755);
331 + yield* fs.writeFileString(
332 + startupFile,
333 + "if shopt -q login_shell; then export T3_SSH_LOGIN_ENV=loaded; fi\n",
334 + );
335 + const target = { alias: "login-test", hostname: "login-test", username: null, port: null };
336 + let remoteCommand = "";
337 + const capture = ChildProcessSpawner.make((command) =>
338 + Effect.sync(() => {
339 + const args = commandArgs(command);
340 + remoteCommand = args.slice(args.indexOf(target.alias) + 1).join(" ");
341 + return makeSuccessfulProcess('{"remotePort":3774}\n');
342 + }),
343 + );
344 + yield* SshTunnel.launchOrReuseRemoteServer(target, undefined, ARCHIVE).pipe(
345 + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, capture),
346 + );
347 + yield* fs.writeFileString(
348 + `${directory}/.zprofile`,
349 + "if [[ -o login ]]; then export T3_SSH_LOGIN_ENV=loaded; fi\n",
350 + );
351 + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
352 + const cases = [
353 + { shell: bash, outerShell: "/bin/sh", expected: "loaded" },
354 + { shell: "/bin/sh", outerShell: "/bin/sh", expected: "missing" },
355 + { shell: "", outerShell: "/bin/sh", expected: "missing" },
356 + { shell: "/unavailable/csh", outerShell: "/bin/sh", expected: "missing" },
357 + { shell: "/unavailable/tcsh", outerShell: "/bin/sh", expected: "missing" },
358 + { shell: "/unavailable/pwsh", outerShell: "/bin/sh", expected: "missing" },
359 + ];
360 + if (yield* fs.exists("/bin/zsh")) {
361 + cases.push({ shell: "/bin/zsh", outerShell: "/bin/zsh", expected: "loaded" });
362 + }
363 + if (yield* fs.exists("/bin/tcsh")) {
364 + cases.push({ shell: "/bin/tcsh", outerShell: "/bin/tcsh", expected: "missing" });
365 + }
366 + const environment = yield* HostProcess.Environment;
367 + for (const folder of (environment.PATH ?? "").split(":")) {
368 + const nu = `${folder}/nu`;
369 + if (folder && (yield* fs.exists(nu))) {
370 + cases.push({ shell: nu, outerShell: nu, expected: "missing" });
371 + break;
372 + }
373 + }
374 + for (const { shell, outerShell, expected } of cases) {
375 + const child = yield* spawner.spawn(
376 + ChildProcess.make(outerShell, ["-c", remoteCommand], {
377 + env: {
378 + PATH: environment.PATH ?? "/usr/bin:/bin",
379 + HOME: directory,
380 + XDG_CONFIG_HOME: directory,
381 + SHELL: shell,
382 + BASH_ENV: startupFile,
383 + ZDOTDIR: directory,
384 + T3_SSH_LOGIN_ENV: "",
385 + },
386 + stdin: {
387 + stream: Stream.make(
388 + new TextEncoder().encode('printf "%s:%s" "${T3_SSH_LOGIN_ENV:-missing}" "$1"'),
389 + ),
390 + endOnDone: true,
391 + },
392 + }),
393 + );
394 + const [stdout, stderr, exitCode] = yield* Effect.all(
395 + [
396 + child.stdout.pipe(
397 + Stream.decodeText(),
398 + Stream.runFold(
399 + () => "",
400 + (a, b) => a + b,
401 + ),
402 + ),
403 + child.stderr.pipe(
404 + Stream.decodeText(),
405 + Stream.runFold(
406 + () => "",
407 + (a, b) => a + b,
408 + ),
409 + ),
410 + child.exitCode,
411 + ],
412 + { concurrency: "unbounded" },
413 + );
414 + assert.equal(Number(exitCode), 0, stderr);
415 + // First-run shell messages can precede the bootstrap response.
416 + const response = stdout.trim().split(/\r?\n/u).at(-1) ?? "";
417 + assert.match(response, new RegExp(`^${expected}:[a-f0-9]{16}$`), shell || "unset SHELL");
418 + }
419 + }).pipe(Effect.provide(Layer.merge(NodeServices.layer, NodeCrypto.layer))),
420 + );
421 +
422 it.effect("accepts pretty-printed pairing JSON after remote shell startup noise", () => {
423 const target = {
424 alias: "devbox",
@@ -795,7 +898,7 @@ describe("ssh tunnel scripts", () => {
898 }
899 return tunnel;
900 }
798 - if (args.some((arg, index) => arg === "-s" && args[index + 1] === "--")) {
901 + if (args.includes("-c")) {
902 if (isTarget) {
903 launches += 1;
904 remoteRunning = true;
@@ -972,17 +1075,20 @@ server.listen(Number(flag("--port") ?? 0), "127.0.0.1", () => {
1075 const fakeServer = `${root}/fake-t3.cjs`;
1076 yield* fs.makeDirectory(home, { recursive: true });
1077 yield* fs.writeFileString(fakeServer, FAKE_SERVER);
975 - // Runs the remote command locally under the temp HOME, without -l so the test
976 - // machine's login profile cannot move HOME. setsid keeps a launched server alive
1078 + // Runs the remote command locally under the temp HOME. setsid keeps a launched server alive
1079 // when the command's process group goes away, as sshd would.
1080 const sshAsLocalShell = ChildProcessSpawner.make((command) => {
1081 if (command._tag !== "StandardCommand" || command.command !== "ssh") {
1082 return realSpawner.spawn(command);
1083 }
1084 if (command.args.includes("-G")) return Effect.succeed(makeSuccessfulProcess(""));
983 - const remoteArgs = command.args.slice(command.args.indexOf("--") + 2);
1085 + const stateKey = launchStateKey(command);
1086 + const remoteArgs =
1087 + stateKey === undefined
1088 + ? command.args.slice(command.args.indexOf("--") + 2)
1089 + : ["sh", "-s", "--", stateKey];
1090 return realSpawner.spawn(
985 - ChildProcess.make("setsid", ["-w", ...remoteArgs.filter((arg) => arg !== "-l")], {
1091 + ChildProcess.make("setsid", ["-w", ...remoteArgs], {
1092 ...command.options,
1093 env: { PATH: process.env.PATH ?? "", HOME: home },
1094 extendEnv: false,
packages/ssh/src/tunnel.ts
+9 -1
@@ -1005,7 +1005,15 @@ export const launchOrReuseRemoteServer = Effect.fn("ssh/tunnel.launchOrReuseRemo
1005 stateKey,
1006 });
1007 const result = yield* runSshCommand(target, {
1008 - remoteCommandArgs: ["sh", "-l", "-s", "--", stateKey],
1008 + // Shells known to take `-l -c` with a POSIX-style `exec` read their own login
1009 + // profiles first; any other shell (csh, pwsh, ...) keeps the plain `sh -l` launch.
1010 + remoteCommandArgs: [
1011 + "sh",
1012 + "-c",
1013 + shellSingleQuote(
1014 + `case "\${SHELL##*/}" in bash|zsh|ksh|fish|nu) exec "$SHELL" -l -c "exec sh -s -- ${stateKey}" ;; *) exec sh -l -s -- ${stateKey} ;; esac`,
1015 + ),
1016 + ],
1017 stdin: buildRemoteLaunchScript(runner),
1018 timeoutMs: isNodeScriptRunner(runner)
1019 ? REMOTE_LAUNCH_TIMEOUT_MS