import * as HostProcess from "@t3tools/shared/HostProcess"; import { CLI_RELEASE_BASE_URL_ENV, CLI_RELEASE_CHANNELS, cliReleaseIndexPageUrl, cliReleaseChannelOf, newestCliReleaseVersion, updateFeedFileName, type CliReleaseChannel, } from "@t3tools/shared/cliRelease"; import * as Console from "effect/Console"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; import { Argument, Command, Flag, GlobalFlag, Prompt } from "effect/cli"; import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; import packageJson from "../../package.json" with { type: "json" }; import * as BootService from "../cloud/bootService.ts"; import { ensurePinnedRuntimeInstalled, launcherOwnsVersionsDir, pinnedRuntimeCommand, PinnedRuntimeInstallError, pinnedRuntimePaths, pinnedRuntimeVersionsDir, prunePinnedRuntimes, recordPinnedRuntimeLauncher, windowsShimTarget, } from "../cloud/pinnedRuntime.ts"; import { compareExactServiceVersions, isExactServiceVersion } from "../cloud/serviceProtocol.ts"; import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; import { managedUpdateMirror, managedUpdateRefusal } from "../policy/managedUpdates.ts"; import * as ProcessRunner from "../processRunner.ts"; import { isProcessAlive, readPersistedServerRuntimeState } from "../serverRuntimeState.ts"; import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts"; import { createUpdateProgress } from "./updateProgress.ts"; import * as CliService from "./service.ts"; export class CliUpdateError extends Schema.TaggedError()("CliUpdateError", { reason: Schema.String, }) { override get message(): string { return this.reason; } } const ReleaseIndex = Schema.Array( Schema.Struct({ tag_name: Schema.String, draft: Schema.optional(Schema.Boolean), }), ); const decodeReleaseIndex = Schema.decodeUnknownEffect(Schema.fromJsonString(ReleaseIndex)); const RELEASE_INDEX_TIMEOUT = Duration.seconds(30); const RELEASE_INDEX_MAX_PAGES = 10; export const resolveNewestVersion = Effect.fn("cli.update.resolve_newest")(function* ( channel: CliReleaseChannel, ) { const httpClient = yield* HttpClient.HttpClient; const environment = yield* HostProcess.Environment; const githubToken = environment.GH_TOKEN?.trim() || environment.GITHUB_TOKEN?.trim() || undefined; for (let page = 1; page <= RELEASE_INDEX_MAX_PAGES; page += 1) { const request = HttpClientRequest.get(cliReleaseIndexPageUrl(page)).pipe( HttpClientRequest.setHeader("Accept", "application/vnd.github+json"), ); const authenticatedRequest = githubToken === undefined ? request : HttpClientRequest.setHeader(request, "Authorization", `Bearer ${githubToken}`); const body = yield* httpClient.execute(authenticatedRequest).pipe( Effect.flatMap(HttpClientResponse.filterStatusOk), Effect.flatMap((response) => response.text), Effect.mapError(() => new CliUpdateError({ reason: "Could not list t3 releases." })), Effect.timeoutOrElse({ duration: RELEASE_INDEX_TIMEOUT, orElse: () => Effect.fail(new CliUpdateError({ reason: "Timed out listing t3 releases." })), }), ); const releases = yield* decodeReleaseIndex(body).pipe( Effect.mapError( () => new CliUpdateError({ reason: "The t3 release index had an unexpected shape." }), ), ); const version = newestCliReleaseVersion(releases, channel); if (version !== undefined) return version; if (releases.length === 0) break; } return yield* new CliUpdateError({ reason: `No published ${channel} release was found.` }); }); /** The version an update mirror's feed file for this platform offers on `channel`. */ export const resolveMirrorVersion = Effect.fn("cli.update.resolve_mirror")(function* ( mirror: { readonly organization: string; readonly feedUrl: string }, channel: CliReleaseChannel, ) { if (channel === "preview") { return yield* new CliUpdateError({ reason: `${mirror.organization}'s update mirror has no preview builds.`, }); } const httpClient = yield* HttpClient.HttpClient; const url = `${mirror.feedUrl.replace(/\/+$/, "")}/${updateFeedFileName( channel === "nightly" ? "nightly" : "latest", yield* HostProcess.Platform, yield* HostProcess.Architecture, )}`; const unreadable = new CliUpdateError({ reason: `Could not read ${url} from ${mirror.organization}'s update mirror.`, }); const body = yield* httpClient.get(url).pipe( Effect.flatMap(HttpClientResponse.filterStatusOk), Effect.flatMap((response) => response.text), Effect.mapError(() => unreadable), Effect.timeoutOrElse({ duration: RELEASE_INDEX_TIMEOUT, orElse: () => Effect.fail(unreadable), }), ); const version = /^version:\s*["']?([^"'\s]+)/m.exec(body)?.[1]; if ( version === undefined || !isExactServiceVersion(version) || cliReleaseChannelOf(version) !== channel ) { return yield* unreadable; } return version; }); /** * Only a launcher that already points into this home's `runtime/versions` tree is touched; a * plain copy of the executable, or a launcher for some other install, is left alone. It is * recorded before it moves, so prune never meets a repointed launcher it does not know about. */ export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function* (input: { readonly launchedAs: string | undefined; readonly baseDir: string; readonly targetEntryPath: string; }) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const platform = yield* HostProcess.Platform; if (input.launchedAs === undefined) return Option.none(); const versionsDir = pinnedRuntimeVersionsDir(path, input.baseDir); const ownsTarget = (candidate: string) => launcherOwnsVersionsDir(path, versionsDir, candidate); const remember = (launcher: string) => recordPinnedRuntimeLauncher({ baseDir: input.baseDir, launcher }).pipe( Effect.mapError( () => new CliUpdateError({ reason: `Could not record the t3 launcher at ${launcher}.` }), ), ); if (platform === "win32") { const shimPath = yield* findWindowsShim(input.launchedAs); if (shimPath === undefined) return Option.none(); const current = yield* fs.readFileString(shimPath).pipe(Effect.option); const quoted = Option.isSome(current) ? windowsShimTarget(current.value) : undefined; if (quoted === undefined || !ownsTarget(quoted)) return Option.none(); yield* remember(shimPath); yield* fs .writeFileString(shimPath, `@echo off\r\n"${input.targetEntryPath}" %*`) .pipe( Effect.mapError( () => new CliUpdateError({ reason: `Could not rewrite the t3 launcher at ${shimPath}.` }), ), ); return Option.some(shimPath); } const linkTarget = yield* fs.readLink(input.launchedAs).pipe(Effect.option); if (Option.isNone(linkTarget)) return Option.none(); const resolvedTarget = path.resolve(path.dirname(input.launchedAs), linkTarget.value); if (!ownsTarget(resolvedTarget)) return Option.none(); yield* remember(input.launchedAs); const tempLink = `${input.launchedAs}.${process.pid}.tmp`; yield* fs.symlink(input.targetEntryPath, tempLink).pipe( Effect.andThen(fs.rename(tempLink, input.launchedAs)), Effect.mapError( () => new CliUpdateError({ reason: `Could not repoint the t3 launcher at ${input.launchedAs}.` }), ), ); return Option.some(input.launchedAs); }); /** Node preserves argv0's spelling; resolve bare names on PATH to find the launcher symlink. */ export const resolveLauncherPath = Effect.gen(function* () { const path = yield* Path.Path; const fs = yield* FileSystem.FileSystem; const invokedAs = yield* HostProcess.InvokedAs; const cwd = yield* HostProcess.WorkingDirectory; const environment = yield* HostProcess.Environment; const platform = yield* HostProcess.Platform; if (invokedAs.includes("/") || invokedAs.includes("\\")) { return path.resolve(cwd, invokedAs); } const delimiter = platform === "win32" ? ";" : ":"; for (const directory of (environment["PATH"] ?? "").split(delimiter)) { if (directory.length === 0) continue; const candidate = path.join(directory, invokedAs); if (yield* fs.exists(candidate).pipe(Effect.orElseSucceed(() => false))) { return candidate; } } return undefined; }); /** Windows executables see their own path, so find the installer shim by matching t3.cmd targets on PATH. */ export const findWindowsShim = Effect.fn("cli.update.find_windows_shim")(function* ( executablePath: string, ) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const environment = yield* HostProcess.Environment; const candidates = [ ...(environment["T3CODE_INSTALL_BIN_DIR"] ? [environment["T3CODE_INSTALL_BIN_DIR"]] : []), ...(environment["PATH"] ?? environment["Path"] ?? "").split(";"), ].filter((entry) => entry.trim().length > 0); for (const directory of candidates) { const shimPath = path.join(directory, "t3.cmd"); const contents = yield* fs.readFileString(shimPath).pipe(Effect.option); if (Option.isNone(contents)) continue; const target = windowsShimTarget(contents.value); if ( target !== undefined && path.resolve(target).toLowerCase() === path.resolve(executablePath).toLowerCase() ) { return shimPath; } } return undefined; }); const updateFlags = { ...projectLocationFlags, channel: Flag.Literals("channel", CLI_RELEASE_CHANNELS).pipe( Flag.withDescription( "Release channel to follow. Defaults to the channel this t3 was published on.", ), Flag.optional, ), allowDowngrade: Flag.Boolean("allow-downgrade").pipe( Flag.withDescription("Allow moving to an older version than the one running."), Flag.withDefault(false), ), yes: Flag.Boolean("yes").pipe( Flag.withAlias("y"), Flag.withDescription( "Restart the background service without asking. Required to restart it from a script, where there is no prompt.", ), Flag.withDefault(false), ), }; const versionArgument = Argument.String("version").pipe( Argument.withDescription( "Exact version to install. Defaults to the newest release on the channel.", ), Argument.optional, ); export const updateCommand = Command.make("update", { ...updateFlags, version: versionArgument, }).pipe( Command.withDescription( "Download a newer t3 and switch this machine to it, including the background service when one is installed.", ), Command.withHandler((flags) => Effect.gen(function* () { const logLevel = yield* GlobalFlag.LogLevel; const config = yield* resolveCliAuthConfig(flags, logLevel); return yield* runUpdate({ baseDir: config.baseDir, logsDir: config.logsDir, serverRuntimeStatePath: config.serverRuntimeStatePath, channel: Option.getOrUndefined(flags.channel), requestedVersion: Option.getOrUndefined(flags.version), allowDowngrade: flags.allowDowngrade, assumeYes: flags.yes, }).pipe( Effect.provide( Layer.mergeAll( CliService.layer(config), ProcessRunner.layer, FetchHttpClient.layer, ManagedPolicy.layer, ), ), ); }), ), ); const findForegroundServer = Effect.fn("cli.update.find_foreground_server")(function* (input: { readonly serverRuntimeStatePath: string; readonly serviceInstalled: boolean; }) { const state = yield* readPersistedServerRuntimeState(input.serverRuntimeStatePath); if (Option.isNone(state) || state.value.serviceManaged || !isProcessAlive(state.value.pid)) { return undefined; } if (input.serviceInstalled && (yield* belongsToBootService(state.value.pid))) return undefined; return state.value; }); const belongsToBootService = Effect.fn("cli.update.belongs_to_boot_service")(function* ( pid: number, ) { const platform = yield* HostProcess.Platform; const fs = yield* FileSystem.FileSystem; const runner = yield* ProcessRunner.ProcessRunner; if (platform === "linux") { const cgroup = yield* fs.readFileString(`/proc/${pid}/cgroup`).pipe(Effect.option); return Option.isSome(cgroup) && cgroup.value.includes("/t3code.service"); } if (platform === "darwin") { const parent = yield* runner .run({ command: "ps", args: ["-o", "ppid=", "-p", String(pid)], timeout: Duration.seconds(5), }) .pipe(Effect.option); const ppid = Option.isSome(parent) && parent.value.code === 0 ? parent.value.stdout.trim() : ""; if (!/^\d+$/.test(ppid)) return false; const command = yield* runner .run({ command: "ps", args: ["-o", "command=", "-p", ppid], timeout: Duration.seconds(5) }) .pipe( Effect.map((result) => (result.code === 0 ? result.stdout : "")), Effect.orElseSucceed(() => ""), ); return /__service-launcher/.test(command); } return false; }); export const runUpdate = Effect.fn("cli.update.run")(function* (input: { readonly baseDir: string; readonly logsDir: string; readonly serverRuntimeStatePath: string; readonly channel: CliReleaseChannel | undefined; readonly requestedVersion: string | undefined; readonly allowDowngrade: boolean; readonly assumeYes: boolean; }) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const runner = yield* ProcessRunner.ProcessRunner; const platform = yield* HostProcess.Platform; const arch = yield* HostProcess.Architecture; const environment = yield* HostProcess.Environment; const httpClient = yield* HttpClient.HttpClient; const service = yield* BootService.BootService; const policy = yield* ManagedPolicy.ManagedPolicy; const mirror = managedUpdateMirror(policy.document); const currentVersion = packageJson.version; const channel = input.channel ?? cliReleaseChannelOf(currentVersion); const refusal = managedUpdateRefusal( policy.document, input.requestedVersion === undefined ? channel : cliReleaseChannelOf(input.requestedVersion), ); if (refusal !== undefined) { return yield* new CliUpdateError({ reason: refusal }); } if (input.requestedVersion !== undefined && !isExactServiceVersion(input.requestedVersion)) { return yield* new CliUpdateError({ reason: `'${input.requestedVersion}' is not an exact t3 version.`, }); } const progress = createUpdateProgress(); progress.status("Checking for updates..."); const targetVersion = yield* ( input.requestedVersion !== undefined ? Effect.succeed(input.requestedVersion) : mirror === undefined ? resolveNewestVersion(channel) : resolveMirrorVersion(mirror, channel) ).pipe(Effect.ensuring(Effect.sync(progress.finish))); const targetChannel = cliReleaseChannelOf(targetVersion); const currentChannel = cliReleaseChannelOf(currentVersion); if (targetChannel === "preview" && currentChannel !== "preview") { yield* Console.log( [ `t3@${targetVersion} is a preview build.`, " Preview builds are cut by maintainers from unreleased branches to exercise the release", " pipeline. They can be broken, receive no fixes, and are never offered as updates; you", ` will have to switch back to ${currentChannel} yourself with \`t3 update --channel ${currentChannel} --allow-downgrade\`.`, ].join("\n"), ); if (!(process.stdin.isTTY && process.stdout.isTTY)) { return yield* new CliUpdateError({ reason: "Refusing to install a preview build without confirmation. Run this from a terminal to confirm, or pass --channel preview from an interactive shell.", }); } const confirmed = yield* Prompt.run( Prompt.Confirm({ message: "Install the preview build anyway?", initial: false }), ).pipe(Effect.catchTags({ QuitError: () => Effect.succeed(false) })); if (!confirmed) { yield* Console.log("Left as is."); return; } } const status = yield* service.status; // Service names are per user, so another home's unit belongs to a different install. const servesThisHome = status.installedBaseDir !== undefined && path.resolve(status.installedBaseDir) === path.resolve(input.baseDir); const serviceInstalled = status.supported && status.installed && servesThisHome; const foreground = yield* findForegroundServer({ serverRuntimeStatePath: input.serverRuntimeStatePath, serviceInstalled, }); const serviceVersion = serviceInstalled ? status.installedVersion : undefined; const executableCurrent = targetVersion === currentVersion; const restartPending = status.problems?.includes("restart-pending") === true; const serviceCurrent = !serviceInstalled || (serviceVersion === targetVersion && (executableCurrent ? status.current : (status.problems ?? []).length === 0)); const newestInstalled = serviceVersion !== undefined && compareExactServiceVersions(serviceVersion, currentVersion) > 0 ? serviceVersion : currentVersion; if (executableCurrent && serviceCurrent) { yield* Console.log( serviceVersion !== undefined ? `t3 and its background service are already on ${targetVersion} (${targetChannel}).` : `t3 is already on ${targetVersion} (${targetChannel}).`, ); return; } if (!input.allowDowngrade && compareExactServiceVersions(targetVersion, newestInstalled) < 0) { return yield* new CliUpdateError({ reason: `t3@${targetVersion} is older than the installed ${newestInstalled}. Pass --allow-downgrade to install it anyway.`, }); } const alreadyOnDisk = yield* fs .readFileString(pinnedRuntimePaths(path, input.baseDir, targetVersion, platform).sentinelPath) .pipe( Effect.map((sentinel) => sentinel.trim() === targetVersion), Effect.orElseSucceed(() => false), ); progress.heading( executableCurrent && restartPending ? `The background service is still running the version before ${targetVersion} (${targetChannel}).` : executableCurrent ? `Updating the background service ${serviceVersion ?? "(unknown version)"} -> ${targetVersion} (${targetChannel}).` : alreadyOnDisk ? "Switching T3 Code" : "Updating T3 Code", executableCurrent ? "" : `${currentVersion} → ${targetVersion}${targetChannel === "stable" ? "" : ` (${targetChannel})`}`, ); let restartService = false; if (serviceInstalled && !serviceCurrent) { yield* Console.log( " A background service is installed for this T3 home. Restarting it interrupts anything running in it: agent turns, terminals, remote clients.", ); if (input.assumeYes) { restartService = true; } else if (process.stdin.isTTY && process.stdout.isTTY) { restartService = yield* Prompt.run( Prompt.Confirm({ message: "Restart the background service once the download is verified?", initial: true, }), ).pipe(Effect.catchTags({ QuitError: () => Effect.succeed(false) })); } else { yield* Console.log( " Not a terminal, so the service keeps running its current version. Rerun with --yes to restart it now, or run `t3 service restart` later.", ); } } const runtime = yield* ensurePinnedRuntimeInstalled({ onProgress: progress.report, baseDir: input.baseDir, version: targetVersion, fs, path, runner, httpClient, platform, arch, releaseBaseUrl: mirror?.feedUrl ?? (environment[CLI_RELEASE_BASE_URL_ENV]?.trim() || undefined), requirePublishedRelease: mirror !== undefined, validate: (paths) => runner .run({ command: pinnedRuntimeCommand(paths).command, args: [...pinnedRuntimeCommand(paths).args, "--version"], timeout: Duration.seconds(30), }) .pipe( Effect.mapError( (cause) => new PinnedRuntimeInstallError({ step: "verifying the downloaded t3", cause }), ), Effect.flatMap((result) => result.code === 0 && /\bv(\S+)\s*$/.exec(result.stdout)?.[1] === targetVersion ? Effect.void : Effect.fail( new PinnedRuntimeInstallError({ step: "verifying the downloaded t3", exitCode: Number(result.code), }), ), ), ), }).pipe( Effect.ensuring(Effect.sync(progress.finish)), Effect.catchIf( (error): error is PinnedRuntimeInstallError => error._tag === "PinnedRuntimeInstallError" && error.step.startsWith("downloading the t3 release checksums") && String(error.cause).includes("404"), () => Effect.fail( new CliUpdateError({ reason: mirror === undefined ? `No release archive was published for t3@${targetVersion}.` : `${mirror.organization}'s update mirror has no t3@${targetVersion} for this machine.`, }), ), ), ); const launchedAs = (yield* HostProcess.IsExecutable) ? yield* resolveLauncherPath : undefined; const repointed = yield* repointLauncher({ launchedAs, baseDir: input.baseDir, targetEntryPath: runtime.entryPath, }); // Use today's service manager for the target version. Inside the service it hands off to // the target CLI, so stopping this process cannot interrupt the switch. let servicePlan: BootService.BootServicePlan | undefined; if (serviceInstalled && !serviceCurrent) { yield* Console.log( restartService ? "Restarting the background service..." : "Updating the background service...", ); servicePlan = yield* BootService.BootService.pipe( Effect.flatMap((target) => target.install({ allowDowngrade: input.allowDowngrade, start: restartService }), ), Effect.provide( BootService.layer({ baseDir: input.baseDir, logsDir: input.logsDir, cliVersion: targetVersion, }), ), Effect.mapError( (error) => new CliUpdateError({ reason: `t3@${targetVersion} is installed but the background service could not be ${restartService ? "updated" : "pointed at it"}: ${error.message}`, }), ), ); } // A handed-off service install rewrites the unit later, while the old launcher may be stopped. if (servicePlan?.handedOff !== true) { // A stopped service's unit still names the launcher runtime it will start. const unit = yield* fs.readFileString(status.unitPath).pipe(Effect.option); yield* prunePinnedRuntimes({ baseDir: input.baseDir, keep: [currentVersion, targetVersion], references: Option.toArray(Option.flatMapNullishOr(unit, BootService.bootServiceProgramOf)), }); } progress.success(`Installed T3 Code ${targetVersion}`); if (Option.isSome(repointed)) { yield* Console.log(" Run t3 to get started.\n"); } else { yield* Console.log(` Run ${runtime.entryPath}\n`); } if (servicePlan?.handedOff === true) { yield* Console.log( ` Background service switching to ${targetVersion} from a separate systemd unit, since this session runs inside it and disconnects when it restarts. Result: ${servicePlan.logPath}`, ); } else if (servicePlan !== undefined && restartService) { yield* Console.log(` Background service restarted on ${targetVersion}`); } else if (serviceInstalled && serviceCurrent) { yield* Console.log(` Background service already on ${targetVersion}`); } else if (serviceInstalled) { yield* Console.log( ` Background service still running ${serviceVersion ?? "an unknown version"}. Run \`t3 service restart\` when you are ready to switch it to ${targetVersion}.`, ); } else if (status.installed && !servesThisHome) { yield* Console.log( ` The background service serves ${status.installedBaseDir ?? "another T3 home"} and was left unchanged.`, ); } if (foreground !== undefined) { yield* Console.log( ` A server started by hand is still running at ${foreground.origin} (pid ${foreground.pid}). Stop it and start it again to pick up ${targetVersion}.`, ); } });