The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Duration from "effect/Duration"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | |
| 5 | import packageJson from "../../package.json" with { type: "json" }; |
| 6 | import { BUILD_MIGRATION_HEAD, recordedMigrations } from "../persistence/databaseMigrationHead.ts"; |
| 7 | import { skippedV2Migrations } from "../persistence/Migrations.ts"; |
| 8 | import { reconciledV2PreviewHistory } from "../persistence/reconcileV2PreviewMigration.ts"; |
| 9 | import type * as ProcessRunner from "../processRunner.ts"; |
| 10 | import { |
| 11 | pinnedRuntimeCommand, |
| 12 | PinnedRuntimeInstallError, |
| 13 | type PinnedRuntimePaths, |
| 14 | PinnedRuntimePreflightBlockedError, |
| 15 | } from "./pinnedRuntime.ts"; |
| 16 | import { |
| 17 | isSupportedLauncherProtocol, |
| 18 | SERVICE_LAUNCHER_PROTOCOL, |
| 19 | type SupportedLauncherProtocol, |
| 20 | } from "./serviceProtocol.ts"; |
| 21 | |
| 22 | // A staged runtime's cold start can take over a minute on a loaded, swapping host. |
| 23 | const PREFLIGHT_TIMEOUT = Duration.minutes(3); |
| 24 | const decodePreflightJson = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown)); |
| 25 | |
| 26 | export type ServicePreflightResult = |
| 27 | | { |
| 28 | readonly status: "ready"; |
| 29 | readonly version: string; |
| 30 | /** The launcher protocol the server named; it accepts no other in reply. */ |
| 31 | readonly launcherProtocol: SupportedLauncherProtocol; |
| 32 | } |
| 33 | | { |
| 34 | readonly status: "blocked"; |
| 35 | readonly version: string; |
| 36 | readonly reason: string; |
| 37 | }; |
| 38 | |
| 39 | export function runServicePreflight(input: { |
| 40 | readonly databasePath: string; |
| 41 | readonly launcherProtocol: number; |
| 42 | readonly version?: string; |
| 43 | }): ServicePreflightResult { |
| 44 | const version = input.version ?? packageJson.version; |
| 45 | const { launcherProtocol } = input; |
| 46 | if (!isSupportedLauncherProtocol(launcherProtocol)) { |
| 47 | return { |
| 48 | status: "blocked", |
| 49 | version, |
| 50 | // A stale `t3` earlier on PATH (an old npm/nvm global) makes `t3 service install` |
| 51 | // reinstall itself and report success, so name the exact install and the path check. |
| 52 | reason: [ |
| 53 | `The installed service launcher speaks protocol ${launcherProtocol}, but t3@${version} requires protocol ${SERVICE_LAUNCHER_PROTOCOL}.`, |
| 54 | `On the server machine, install this exact CLI: \`curl -fsSL https://t3.codes/install.sh | T3CODE_VERSION=${version} sh\`.`, |
| 55 | `Check that \`t3 --version\` prints ${version} and that \`which -a t3\` lists no older t3 (such as an npm or nvm install) first, then run \`~/.local/bin/t3 service install\` (the installer's default path).`, |
| 56 | "Updating the desktop app does not update this CLI, and `t3 service update` only repairs the service with whichever CLI runs it.", |
| 57 | ].join(" "), |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | const recorded = recordedMigrations(input.databasePath); |
| 62 | if (recorded === null) { |
| 63 | return { |
| 64 | status: "blocked", |
| 65 | version, |
| 66 | reason: `Could not read this database to confirm t3@${version} supports it.`, |
| 67 | }; |
| 68 | } |
| 69 | const newer = recorded |
| 70 | .filter(({ id }) => id > BUILD_MIGRATION_HEAD) |
| 71 | .map(({ id, name }) => `${id}_${name}`); |
| 72 | if (newer.length > 0) { |
| 73 | return { |
| 74 | status: "blocked", |
| 75 | version, |
| 76 | reason: `This database has migrations that t3@${version} does not know: ${newer.join(", ")}. A newer build wrote them, and running t3@${version} on this database is not safe. Stay on the newer release, or wait for a release that includes these migrations.`, |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | // Startup reconciles V2 preview ids before it checks the history, so check what it will see. |
| 81 | const reconciled = reconciledV2PreviewHistory(recorded); |
| 82 | const incompatible = |
| 83 | reconciled === null ? ["an unexpected V2 preview history"] : skippedV2Migrations(reconciled); |
| 84 | if (incompatible.length > 0) { |
| 85 | return { |
| 86 | status: "blocked", |
| 87 | version, |
| 88 | reason: `This database has incompatible migration history: ${incompatible.join(", ")}. Start the build that wrote it, or report this migration history so an upgrade path can be added.`, |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | return { status: "ready", version, launcherProtocol }; |
| 93 | } |
| 94 | |
| 95 | function decodeServicePreflightResult( |
| 96 | value: unknown, |
| 97 | launcherProtocol: SupportedLauncherProtocol, |
| 98 | ): ServicePreflightResult | undefined { |
| 99 | if (typeof value !== "object" || value === null) { |
| 100 | return undefined; |
| 101 | } |
| 102 | const record = value as Record<string, unknown>; |
| 103 | if ( |
| 104 | record.status === "ready" && |
| 105 | record.launcherProtocol === launcherProtocol && |
| 106 | typeof record.version === "string" |
| 107 | ) { |
| 108 | return { status: "ready", version: record.version, launcherProtocol }; |
| 109 | } |
| 110 | if ( |
| 111 | record.status === "blocked" && |
| 112 | typeof record.version === "string" && |
| 113 | typeof record.reason === "string" |
| 114 | ) { |
| 115 | return { status: "blocked", version: record.version, reason: record.reason }; |
| 116 | } |
| 117 | return undefined; |
| 118 | } |
| 119 | |
| 120 | /** Asks a staged runtime whether it can run on this database under this launcher protocol. */ |
| 121 | export const runStagedServicePreflight = Effect.fn("cloud.service_preflight.run_staged")( |
| 122 | function* (input: { |
| 123 | readonly runner: ProcessRunner.ProcessRunner["Service"]; |
| 124 | readonly runtime: PinnedRuntimePaths; |
| 125 | readonly targetVersion: string; |
| 126 | readonly databasePath: string; |
| 127 | readonly launcherProtocol: SupportedLauncherProtocol; |
| 128 | }) { |
| 129 | const step = "running the staged service preflight"; |
| 130 | const command = pinnedRuntimeCommand(input.runtime); |
| 131 | const result = yield* input.runner |
| 132 | .run({ |
| 133 | command: command.command, |
| 134 | args: [ |
| 135 | ...command.args, |
| 136 | "__service-preflight", |
| 137 | "--database-path", |
| 138 | input.databasePath, |
| 139 | "--launcher-protocol", |
| 140 | String(input.launcherProtocol), |
| 141 | ], |
| 142 | timeout: PREFLIGHT_TIMEOUT, |
| 143 | }) |
| 144 | .pipe(Effect.mapError((cause) => new PinnedRuntimeInstallError({ step, cause }))); |
| 145 | if (result.code !== 0) { |
| 146 | return yield* new PinnedRuntimeInstallError({ |
| 147 | step, |
| 148 | exitCode: Number(result.code), |
| 149 | stdoutLength: result.stdout.length, |
| 150 | stderrLength: result.stderr.length, |
| 151 | }); |
| 152 | } |
| 153 | const parsed = yield* decodePreflightJson(result.stdout.trim()).pipe( |
| 154 | Effect.mapError( |
| 155 | (cause) => |
| 156 | new PinnedRuntimeInstallError({ step: "decoding the staged service preflight", cause }), |
| 157 | ), |
| 158 | ); |
| 159 | const preflight = decodeServicePreflightResult(parsed, input.launcherProtocol); |
| 160 | if (preflight === undefined || preflight.version !== input.targetVersion) { |
| 161 | return yield* new PinnedRuntimeInstallError({ |
| 162 | step: "verifying the staged service preflight", |
| 163 | }); |
| 164 | } |
| 165 | if (preflight.status === "blocked") { |
| 166 | return yield* new PinnedRuntimePreflightBlockedError({ |
| 167 | version: input.targetVersion, |
| 168 | reason: preflight.reason, |
| 169 | }); |
| 170 | } |
| 171 | }, |
| 172 | ); |