feat(cli): add t3 update for self-contained installs #11451
Mergedjuliusmarmingemerged 7 commits into
sea/remote-runners fromrefs/pull/11451/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -60,3 +60,19 @@6060 export function isArchiveDistributedVersion(version: string): boolean {6161 return /-preview\.\d{8}\.\d+$/.test(version);6262 }63+64+export type CliReleaseChannel = "stable" | "nightly" | "preview";65+export const CLI_RELEASE_CHANNELS: ReadonlyArray<CliReleaseChannel> = [66+ "stable",67+ "nightly",68+ "preview",69+];70+71+/** The release train a version was published on, derived from its prerelease tag. */72+export function cliReleaseChannelOf(version: string): CliReleaseChannel {73+ const channel = /^[^-+]+-(nightly|preview)\.\d{8}\.\d+$/.exec(version)?.[1];74+ return channel === "nightly" || channel === "preview" ? channel : "stable";75+}76+77+/** GitHub's list-releases endpoint; newest first, so the first match wins. */78+export const CLI_RELEASE_INDEX_URL = `https://api.github.com/repos/${CLI_RELEASE_REPOSITORY}/releases?per_page=50`;@@ -0,0 +117,21 @@117+ ? input.launchedAs118+ : path.join(path.dirname(input.launchedAs), "t3.cmd");119+ if (!(yield* fs.exists(shimPath).pipe(Effect.orElseSucceed(() => false)))) {120+ return Option.none<string>();121+ }122+ yield* fs123+ .writeFileString(shimPath, `@echo off\r\n"${input.targetEntryPath}" %*`)124+ .pipe(125+ Effect.mapError(126+ () => new CliUpdateError({ reason: `Could not rewrite the t3 launcher at ${shimPath}.` }),127+ ),128+ );129+ return Option.some(shimPath);130+ }131+132+ const linkTarget = yield* fs.readLink(input.launchedAs).pipe(Effect.option);133+ // Only a launcher we own gets repointed. A plain copy of the executable, or134+ // a symlink to somewhere else, is left alone.135+ if (Option.isNone(linkTarget)) return Option.none<string>();136+ const resolvedTarget = path.resolve(path.dirname(input.launchedAs), linkTarget.value);137+ if (path.basename(path.dirname(path.dirname(resolvedTarget))) !== "versions") {apps/server/src/cli/update.ts line 116OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +96,21 @@96+ return version;97+});98+99+/**100+ * The launcher the install scripts leave behind: a symlink at101+ * `<bin>/t3` on POSIX, a `t3.cmd` shim on Windows. `t3 update` repoints it so102+ * the next `t3` invocation is the new version. The bin directory is whatever103+ * the running executable was launched through; a `t3` that was run by its104+ * full path inside the versions directory has no launcher to repoint.105+ */106+export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function* (input: {107+ readonly launchedAs: string | undefined;108+ readonly targetEntryPath: string;109+}) {110+ const fs = yield* FileSystem.FileSystem;111+ const path = yield* Path.Path;112+ const platform = yield* HostProcessPlatform;113+ if (input.launchedAs === undefined) return Option.none<string>();114+115+ if (platform === "win32") {116+ const shimPath = /\.cmd$/i.test(input.launchedAs)
- @@ -0,0 +103,21 @@103+ * the running executable was launched through; a `t3` that was run by its104+ * full path inside the versions directory has no launcher to repoint.105+ */106+export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function* (input: {107+ readonly launchedAs: string | undefined;108+ readonly targetEntryPath: string;109+}) {110+ const fs = yield* FileSystem.FileSystem;111+ const path = yield* Path.Path;112+ const platform = yield* HostProcessPlatform;113+ if (input.launchedAs === undefined) return Option.none<string>();114+115+ if (platform === "win32") {116+ const shimPath = /\.cmd$/i.test(input.launchedAs)117+ ? input.launchedAs118+ : path.join(path.dirname(input.launchedAs), "t3.cmd");119+ if (!(yield* fs.exists(shimPath).pipe(Effect.orElseSucceed(() => false)))) {120+ return Option.none<string>();121+ }122+ yield* fs123+ .writeFileString(shimPath, `@echo off\r\n"${input.targetEntryPath}" %*`)apps/server/src/cli/update.ts lines 231 to 233OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +213,21 @@213+ const currentVersion = packageJson.version;214+ const channel = input.channel ?? cliReleaseChannelOf(currentVersion);215+ if (input.requestedVersion !== undefined && !isExactServiceVersion(input.requestedVersion)) {216+ return yield* new CliUpdateError({217+ reason: `'${input.requestedVersion}' is not an exact t3 version.`,218+ });219+ }220+ const targetVersion = input.requestedVersion ?? (yield* resolveNewestVersion(channel));221+ const targetChannel = cliReleaseChannelOf(targetVersion);222+223+ // Only archive-distributed versions install without Node and npm on the224+ // machine. Until nightly and stable ship archives, updating onto them from225+ // here would reintroduce the dependency this command exists to remove.226+ if (!isArchiveDistributedVersion(targetVersion)) {227+ return yield* new CliUpdateError({228+ reason: `t3@${targetVersion} is published on npm only. Install it with \`npm install -g t3@${targetVersion}\`, or pick a version from the preview channel.`,229+ });230+ }231+ if (targetVersion === currentVersion) {232+ yield* Console.log(`t3 is already on ${currentVersion} (${targetChannel}).`);233+ return;@@ -60,3 +60,19 @@6060 export function isArchiveDistributedVersion(version: string): boolean {6161 return /-preview\.\d{8}\.\d+$/.test(version);6262 }63+64+export type CliReleaseChannel = "stable" | "nightly" | "preview";65+export const CLI_RELEASE_CHANNELS: ReadonlyArray<CliReleaseChannel> = [66+ "stable",67+ "nightly",68+ "preview",69+];70+71+/** The release train a version was published on, derived from its prerelease tag. */72+export function cliReleaseChannelOf(version: string): CliReleaseChannel {73+ const channel = /^[^-+]+-(nightly|preview)\.\d{8}\.\d+$/.exec(version)?.[1];74+ return channel === "nightly" || channel === "preview" ? channel : "stable";75+}76+77+/** GitHub's list-releases endpoint; newest first, so the first match wins. */78+export const CLI_RELEASE_INDEX_URL = `https://api.github.com/repos/${CLI_RELEASE_REPOSITORY}/releases?per_page=50`;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/cli/update.ts lines 84 to 84OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +64,21 @@64+const RELEASE_INDEX_TIMEOUT = Duration.seconds(30);65+66+/** Asks GitHub for the newest published version on a channel. */67+const resolveNewestVersion = Effect.fn("cli.update.resolve_newest")(function* (68+ channel: CliReleaseChannel,69+) {70+ const httpClient = yield* HttpClient.HttpClient;71+ const body = yield* httpClient72+ .execute(73+ HttpClientRequest.get(CLI_RELEASE_INDEX_URL).pipe(74+ HttpClientRequest.setHeader("Accept", "application/vnd.github+json"),75+ ),76+ )77+ .pipe(78+ Effect.flatMap(HttpClientResponse.filterStatusOk),79+ Effect.flatMap((response) => response.text),80+ Effect.timeoutOrElse({81+ duration: RELEASE_INDEX_TIMEOUT,82+ orElse: () => Effect.fail(new CliUpdateError({ reason: "Timed out listing t3 releases." })),83+ }),84+ Effect.mapError(() => new CliUpdateError({ reason: "Could not list t3 releases." })),
- @@ -0,0 +298,21 @@298+ const serviceInstalled = status.supported && status.installed && servesThisHome;299+ const foreground = yield* findForegroundServer({300+ serverRuntimeStatePath: input.serverRuntimeStatePath,301+ serviceInstalled,302+ });303+ // "Already on" is about what this machine runs, not what this executable304+ // is: a newer t3 downloaded by hand and pointed at a home whose service305+ // still runs the old version has an update to do, and the service's306+ // version is the one the downgrade check has to protect.307+ const serviceVersion = serviceInstalled ? status.installedVersion : undefined;308+ const installedVersion = serviceVersion ?? currentVersion;309+310+ // Only archive-distributed versions install without Node and npm on the311+ // machine. Until nightly and stable ship archives, updating onto them from312+ // here would reintroduce the dependency this command exists to remove.313+ if (!isArchiveDistributedVersion(targetVersion)) {314+ return yield* new CliUpdateError({315+ reason: `t3@${targetVersion} is published on npm only. Install it with \`npm install -g t3@${targetVersion}\`, or pick a version from the preview channel.`,316+ });317+ }318+ if (targetVersion === installedVersion) {
- @@ -62,3 +63,4 @@6263 authCommand,6364 projectCommand,6465 serviceCommand,66+ updateCommand,
- @@ -0,0 +328,21 @@328+ // Work out everything that will be touched before touching anything, so the329+ // user sees one plan and one question rather than a surprise restart.330+ const status = yield* service.status;331+ // The unit name is per user, not per T3 home. Only touch the service when it332+ // serves the home this update targets; otherwise it belongs to another333+ // install on this machine and restarting it would take that server down.334+ const servesThisHome =335+ status.installedBaseDir !== undefined &&336+ path.resolve(status.installedBaseDir) === path.resolve(input.baseDir);337+ const serviceInstalled = status.supported && status.installed && servesThisHome;338+ const foreground = yield* findForegroundServer({339+ serverRuntimeStatePath: input.serverRuntimeStatePath,340+ serviceInstalled,341+ });342+ // What this machine runs is the executable behind the launcher and, when a343+ // service is installed for this home, the version that service runs. Either344+ // being stale is an update to do, and the newest of the two is what the345+ // downgrade check protects.346+ const serviceVersion = serviceInstalled ? status.installedVersion : undefined;347+ const executableCurrent = targetVersion === currentVersion;348+ const serviceCurrent = serviceVersion === undefined || serviceVersion === targetVersion;
- apps/marketing/public/install.ps1 lines 79 to 85Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -57,4 +74,8 @@5774 New-Item -ItemType Directory -Path $staging | Out-Null5875 try {5976 Write-Host "Downloading $archive..."60- Invoke-WebRequest -Uri "$baseUrl/v$version/SHA256SUMS" -OutFile (Join-Path $staging "SHA256SUMS") -UseBasicParsing77+ try {78+ Invoke-WebRequest -Uri "$baseUrl/v$version/SHA256SUMS" -OutFile (Join-Path $staging "SHA256SUMS") -UseBasicParsing79+ } catch {80+ Fail "t3 $version has no self-contained archive; install it with 'npm install -g t3@$version' instead"81+ }apps/server/src/cli/update.ts line 492OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +472,21 @@472+ ),473+ }).pipe(474+ Effect.catchIf(475+ (error): error is PinnedRuntimeInstallError =>476+ error._tag === "PinnedRuntimeInstallError" &&477+ error.step.startsWith("downloading the t3 release checksums") &&478+ String(error.cause).includes("404"),479+ () =>480+ Effect.fail(481+ new CliUpdateError({482+ reason: `No release archive was published for t3@${targetVersion}.`,483+ }),484+ ),485+ ),486+ );487+488+ // Node rewrites argv[0] and execPath to the resolved binary, but argv0 is489+ // the string the shell actually passed, which is the launcher symlink when490+ // the executable was started through one. A relative argv0 (`./t3`) is491+ // resolved against the working directory the process started in.492+ const launchedAs = NodeSea.isSea() ? path.resolve(process.argv0) : undefined;
- @@ -0,0 +161,21 @@161+162+/**163+ * The path the executable was started through. Node keeps the shell's164+ * spelling in argv0: a launcher symlink or `./t3` resolves against the165+ * working directory, while a bare `t3` was found on PATH and has to be166+ * looked up there again, or the launcher symlink is never seen.167+ */168+export const resolveLauncherPath = Effect.gen(function* () {169+ const path = yield* Path.Path;170+ const fs = yield* FileSystem.FileSystem;171+ const invokedAs = yield* HostProcessInvokedAs;172+ const cwd = yield* HostProcessWorkingDirectory;173+ const environment = yield* HostProcessEnvironment;174+ const platform = yield* HostProcessPlatform;175+ if (invokedAs.includes("/") || invokedAs.includes("\\")) {176+ return path.resolve(cwd, invokedAs);177+ }178+ const delimiter = platform === "win32" ? ";" : ":";179+ for (const directory of (environment["PATH"] ?? "").split(delimiter)) {180+ if (directory.length === 0) continue;181+ const candidate = path.join(directory, invokedAs);@@ -37,5 +37,4 @@3737 On Windows, run `irm https://t3.codes/install.ps1 | iex` in PowerShell instead.38383939 It places `t3` in `~/.local/bin` and reuses the same download when you later40-run `t3 service install`. Set `T3CODE_VERSION` to pin an exact version, or41-`T3CODE_RELEASE_BASE_URL` to download from a mirror.40+run `t3 service install`. It follows the stable train by default; set@@ -54,6 +64,12 @@5464 fail "sha256sum or shasum is required"5565 fi566667+channel="${T3CODE_CHANNEL:-stable}"5768 version="${T3CODE_VERSION:-}"5869 if [ -z "$version" ]; then59- # Preview is the only train shipping archives while they are being dogfooded.70+ # Tags are v<semver>; the channel is the prerelease identifier, or none for71+ # stable. Only tags of the requested train are considered, so a stable72+ # install can never pick up a nightly or preview build by accident.73+ case "$channel" in74+ stable) tag_pattern='v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)' ;;75+ nightly | preview) tag_pattern="v\([0-9][^\"]*-${channel}\.[0-9]*\.[0-9]*\)" ;;
juliusmarminge merged thisc7f23c4
juliusmarminge closed this
Sign in to join this conversation.
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Baseline: unavailable · PR result:
f2f60de· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.