From 1f5187f0fa676308fa52c1fa7aaf08266fdf4cdf Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 14 Apr 2026 19:29:33 -0700 Subject: [PATCH] Respect desktop release channel for SSH bootstrap Co-authored-by: codex --- apps/desktop/src/main.ts | 8 +++- apps/desktop/src/sshEnvironment.test.ts | 27 +++++++++-- apps/desktop/src/sshEnvironment.ts | 60 ++++++++++++++++++++----- 3 files changed, 79 insertions(+), 16 deletions(-) diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts index 5d0514518a..24e1b5b48a 100644 --- a/apps/desktop/src/main.ts +++ b/apps/desktop/src/main.ts @@ -65,7 +65,7 @@ import { resolveDesktopCoreAdvertisedEndpoints, resolveDesktopServerExposure, } from "./serverExposure.ts"; -import { DesktopSshEnvironmentManager } from "./sshEnvironment.ts"; +import { DesktopSshEnvironmentManager, resolveRemoteT3CliPackageSpec } from "./sshEnvironment.ts"; import { syncShellEnvironment } from "./syncShellEnvironment.ts"; import { waitForBackendStartupReady } from "./backendStartupReadiness.ts"; import { getAutoUpdateDisabledReason, shouldBroadcastDownloadProgress } from "./updateState.ts"; @@ -860,6 +860,12 @@ async function requestSshPasswordFromRenderer(input: { const desktopSshEnvironmentManager = new DesktopSshEnvironmentManager({ passwordProvider: requestSshPasswordFromRenderer, + resolveCliPackageSpec: () => + resolveRemoteT3CliPackageSpec({ + appVersion: app.getVersion(), + updateChannel: desktopSettings.updateChannel, + isDevelopment, + }), }); function resolveUpdaterErrorContext(): DesktopUpdateErrorContext { diff --git a/apps/desktop/src/sshEnvironment.test.ts b/apps/desktop/src/sshEnvironment.test.ts index d0f9c4c2d3..9fd8ab7830 100644 --- a/apps/desktop/src/sshEnvironment.test.ts +++ b/apps/desktop/src/sshEnvironment.test.ts @@ -173,9 +173,24 @@ describe("sshEnvironment", () => { const script = __test.buildRemoteT3RunnerScript(); expect(script).toContain('exec t3 "$@"'); - expect(script).toContain('exec npx --yes t3 "$@"'); - expect(script).toContain('exec npm exec --yes t3 -- "$@"'); - expect(script).toContain("could not find npx or npm on PATH"); + expect(script).toContain('exec npx --yes t3@latest "$@"'); + expect(script).toContain('exec npm exec --yes t3@latest -- "$@"'); + expect(script).toContain("could not install t3@latest"); + }); + + it("resolves the remote t3 package spec from the desktop release channel", () => { + expect( + __test.resolveRemoteT3CliPackageSpec({ + appVersion: "0.0.17", + updateChannel: "latest", + }), + ).toBe("t3@0.0.17"); + expect( + __test.resolveRemoteT3CliPackageSpec({ + appVersion: "0.0.17-nightly.20260415.44", + updateChannel: "nightly", + }), + ).toBe("t3@nightly"); }); it("uses the remote t3 runner for launch and pairing scripts", () => { @@ -190,9 +205,15 @@ describe("sshEnvironment", () => { '[ -n "$REMOTE_PID" ] && [ -n "$REMOTE_PORT" ] && kill -0 "$REMOTE_PID" 2>/dev/null', ); expect(__test.buildRemoteLaunchScript()).toContain('"$RUNNER_FILE" serve --host 127.0.0.1'); + expect(__test.buildRemoteLaunchScript({ packageSpec: "t3@nightly" })).toContain( + 'exec npx --yes t3@nightly "$@"', + ); expect(__test.buildRemotePairingScript(target)).toContain( '"$RUNNER_FILE" auth pairing create --base-dir "$SERVER_HOME" --json', ); + expect(__test.buildRemotePairingScript(target, { packageSpec: "t3@nightly" })).toContain( + 'exec npm exec --yes t3@nightly -- "$@"', + ); }); it("reads the last non-empty ssh output line", () => { diff --git a/apps/desktop/src/sshEnvironment.ts b/apps/desktop/src/sshEnvironment.ts index c62473ba06..7144d8b8bd 100644 --- a/apps/desktop/src/sshEnvironment.ts +++ b/apps/desktop/src/sshEnvironment.ts @@ -9,6 +9,7 @@ import type { DesktopDiscoveredSshHost, DesktopSshEnvironmentBootstrap, DesktopSshEnvironmentTarget, + DesktopUpdateChannel, } from "@t3tools/contracts"; import { waitForHttpReady } from "./backendReadiness"; @@ -18,6 +19,7 @@ const REMOTE_PORT_SCAN_WINDOW = 200; const SSH_ASKPASS_DIR_NAME = "t3code-ssh-askpass"; const TUNNEL_SHUTDOWN_TIMEOUT_MS = 2_000; const SSH_READY_TIMEOUT_MS = 20_000; +const STABLE_T3_VERSION_PATTERN = /^\d+\.\d+\.\d+$/u; interface SshTunnelEntry { readonly key: string; @@ -60,6 +62,7 @@ interface DesktopSshPasswordRequest { interface DesktopSshEnvironmentManagerOptions { readonly passwordProvider?: (request: DesktopSshPasswordRequest) => Promise; + readonly resolveCliPackageSpec?: () => string; } const NO_HOSTS = [] as const; @@ -591,8 +594,8 @@ async function resolveDesktopSshTarget(alias: string): Promise/dev/null 2>&1; then", - ' exec npx --yes t3 "$@"', + ` exec npx --yes ${packageSpec} "$@"`, "fi", "if command -v npm >/dev/null 2>&1; then", - ' exec npm exec --yes t3 -- "$@"', + ` exec npm exec --yes ${packageSpec} -- "$@"`, "fi", - "printf 'Remote host is missing the t3 CLI and could not find npx or npm on PATH.\\n' >&2", + `printf 'Remote host is missing the t3 CLI and could not install ${packageSpec} because npx and npm are unavailable on PATH.\\n' >&2`, "exit 1", ].join("\n"); } -function buildRemotePairingScript(target: DesktopSshEnvironmentTarget): string { - const runnerScript = buildRemoteT3RunnerScript(); +function buildRemotePairingScript( + target: DesktopSshEnvironmentTarget, + input?: { readonly packageSpec?: string }, +): string { + const runnerScript = buildRemoteT3RunnerScript(input); return ` set -eu STATE_DIR="$HOME/.t3/ssh-launch/${remoteStateKey(target)}" @@ -702,10 +726,11 @@ chmod 700 "$RUNNER_FILE" async function launchOrReuseRemoteServer( target: DesktopSshEnvironmentTarget, input?: SshAuthOptions, + runner?: { readonly packageSpec?: string }, ): Promise { const result = await runSshCommand(target, { remoteCommandArgs: ["sh", "-s", "--", remoteStateKey(target)], - stdin: buildRemoteLaunchScript(), + stdin: buildRemoteLaunchScript(runner), ...(input?.authSecret === undefined ? {} : { authSecret: input.authSecret }), ...(input?.batchMode === undefined ? {} : { batchMode: input.batchMode }), ...(input?.interactiveAuth === undefined ? {} : { interactiveAuth: input.interactiveAuth }), @@ -725,10 +750,11 @@ async function launchOrReuseRemoteServer( async function issueRemotePairingToken( target: DesktopSshEnvironmentTarget, input?: SshAuthOptions, + runner?: { readonly packageSpec?: string }, ): Promise { const result = await runSshCommand(target, { remoteCommandArgs: ["sh", "-s"], - stdin: buildRemotePairingScript(target), + stdin: buildRemotePairingScript(target, runner), ...(input?.authSecret === undefined ? {} : { authSecret: input.authSecret }), ...(input?.batchMode === undefined ? {} : { batchMode: input.batchMode }), ...(input?.interactiveAuth === undefined ? {} : { interactiveAuth: input.interactiveAuth }), @@ -898,6 +924,7 @@ export class DesktopSshEnvironmentManager { ): Promise { const resolvedTarget = await resolveDesktopSshTarget(target.alias || target.hostname); const key = targetConnectionKey(resolvedTarget); + const packageSpec = this.options.resolveCliPackageSpec?.(); let entry = this.tunnels.get(key) ?? null; if (entry !== null) { @@ -912,7 +939,11 @@ export class DesktopSshEnvironmentManager { if (entry === null) { const remotePort = await this.runWithSshAuth(key, resolvedTarget, (authOptions) => - launchOrReuseRemoteServer(resolvedTarget, authOptions), + launchOrReuseRemoteServer( + resolvedTarget, + authOptions, + packageSpec === undefined ? undefined : { packageSpec }, + ), ); const localPort = await findAvailableLocalPort(); const httpBaseUrl = `http://127.0.0.1:${localPort}/`; @@ -993,7 +1024,11 @@ export class DesktopSshEnvironmentManager { const pairingToken = options?.issuePairingToken ? await this.runWithSshAuth(key, entry.target, (authOptions) => - issueRemotePairingToken(entry.target, authOptions), + issueRemotePairingToken( + entry.target, + authOptions, + packageSpec === undefined ? undefined : { packageSpec }, + ), ) : null; @@ -1017,6 +1052,7 @@ export const __test = { buildRemoteLaunchScript, buildRemotePairingScript, buildRemoteT3RunnerScript, + resolveRemoteT3CliPackageSpec, buildSshAskpassHelperDescriptor, buildSshChildEnvironment, getLastNonEmptyOutputLine,