The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off - Effect has no incremental digest. |
| 2 | import { |
| 3 | ProviderDriverKind, |
| 4 | type ProviderInstallState, |
| 5 | type ServerProviderCompatibilityAdvisory, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { cliArchiveTarCommand, parseChecksums } from "@t3tools/shared/cliRelease"; |
| 8 | import { compareSemverVersions } from "@t3tools/shared/semver"; |
| 9 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 10 | import { resolveCommandPath, resolveSpawnCommand } from "@t3tools/shared/shell"; |
| 11 | import * as Clock from "effect/Clock"; |
| 12 | import * as Cause from "effect/Cause"; |
| 13 | import * as Context from "effect/Context"; |
| 14 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import * as Exit from "effect/Exit"; |
| 17 | import * as Fiber from "effect/Fiber"; |
| 18 | import * as FileSystem from "effect/FileSystem"; |
| 19 | import * as Layer from "effect/Layer"; |
| 20 | import * as Option from "effect/Option"; |
| 21 | import * as Path from "effect/Path"; |
| 22 | import * as Schedule from "effect/Schedule"; |
| 23 | import * as Schema from "effect/Schema"; |
| 24 | import * as Scope from "effect/Scope"; |
| 25 | import * as Semaphore from "effect/Semaphore"; |
| 26 | import * as Stream from "effect/Stream"; |
| 27 | import * as SubscriptionRef from "effect/SubscriptionRef"; |
| 28 | import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; |
| 29 | import { ChildProcess, ChildProcessSpawner } from "effect/process"; |
| 30 | import * as NodeCrypto from "node:crypto"; |
| 31 | import * as ServerConfig from "../config.ts"; |
| 32 | import { selectManagedRelease } from "./managedRelease.ts"; |
| 33 | import * as ModelManifest from "./ModelManifest.ts"; |
| 34 | |
| 35 | const DRIVER = ProviderDriverKind.make("codex"); |
| 36 | const STAGING_STALE_AFTER_MS = 24 * 60 * 60_000; |
| 37 | const UPDATE_CHECK_INTERVAL_MS = 60 * 60_000; |
| 38 | const UPDATE_CHECK_TIMEOUT = "20 seconds"; |
| 39 | const UPSTREAM_LATEST_URL = "https://registry.npmjs.org/@openai/codex/latest"; |
| 40 | const UPSTREAM_RELEASES_URL = "https://github.com/openai/codex/releases/download"; |
| 41 | // An upstream release has no recorded size; current packages are about 160 MB. |
| 42 | const UPSTREAM_MAX_ARCHIVE_BYTES = 1024 * 1024 * 1024; |
| 43 | const Version = Schema.String.check(Schema.isPattern(/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?$/u)); |
| 44 | const ActiveRelease = Schema.Struct({ version: Version }); |
| 45 | const InstalledRelease = Schema.Struct({ |
| 46 | version: Version, |
| 47 | target: Schema.String, |
| 48 | sha256: Schema.String, |
| 49 | }); |
| 50 | const PackageManifest = Schema.Struct({ |
| 51 | layoutVersion: Schema.Literal(1), |
| 52 | version: Version, |
| 53 | target: Schema.String, |
| 54 | entrypoint: Schema.String, |
| 55 | }); |
| 56 | const decodeVersion = Schema.decodeUnknownEffect(Version); |
| 57 | const decodeNpmLatest = Schema.decodeUnknownEffect(Schema.Struct({ version: Version })); |
| 58 | const encodeRecord = Schema.encodeEffect(Schema.fromJsonString(InstalledRelease)); |
| 59 | const encodeActive = Schema.encodeEffect(Schema.fromJsonString(ActiveRelease)); |
| 60 | export interface CodexReleaseAsset { |
| 61 | readonly version: string; |
| 62 | readonly target: string; |
| 63 | readonly url: string; |
| 64 | readonly sha256: string; |
| 65 | /** Null for an upstream release, whose package checksum is published without a size. */ |
| 66 | readonly archiveBytes: number | null; |
| 67 | } |
| 68 | const RELEASES: Readonly<Record<string, CodexReleaseAsset>> = { |
| 69 | "darwin-arm64": { |
| 70 | version: "0.166.0", |
| 71 | target: "aarch64-apple-darwin", |
| 72 | url: "https://github.com/openai/codex/releases/download/rust-v0.166.0/codex-package-aarch64-apple-darwin.tar.gz", |
| 73 | sha256: "55a440cb579f970abe7c79ab181e54b0ea4c419be99f333bbd868eebac9c21f0", |
| 74 | archiveBytes: 165951869, |
| 75 | }, |
| 76 | "darwin-x64": { |
| 77 | version: "0.166.0", |
| 78 | target: "x86_64-apple-darwin", |
| 79 | url: "https://github.com/openai/codex/releases/download/rust-v0.166.0/codex-package-x86_64-apple-darwin.tar.gz", |
| 80 | sha256: "1cb65690230be5982c654ba2655e3a3b594dce9c55b780e78bfdfeff829d7ad9", |
| 81 | archiveBytes: 180076524, |
| 82 | }, |
| 83 | "linux-arm64": { |
| 84 | version: "0.166.0", |
| 85 | target: "aarch64-unknown-linux-musl", |
| 86 | url: "https://github.com/openai/codex/releases/download/rust-v0.166.0/codex-package-aarch64-unknown-linux-musl.tar.gz", |
| 87 | sha256: "7821b77dda2e6283fb1f63f3165177a271da8833e857cf673c6ca1c9db3bfcc6", |
| 88 | archiveBytes: 187370368, |
| 89 | }, |
| 90 | "linux-x64": { |
| 91 | version: "0.166.0", |
| 92 | target: "x86_64-unknown-linux-musl", |
| 93 | url: "https://github.com/openai/codex/releases/download/rust-v0.166.0/codex-package-x86_64-unknown-linux-musl.tar.gz", |
| 94 | sha256: "1d44b28bc4ac9937d7d3532264baa68a507ce364956a0d4ebb856f331cdee254", |
| 95 | archiveBytes: 199694962, |
| 96 | }, |
| 97 | "win32-arm64": { |
| 98 | version: "0.166.0", |
| 99 | target: "aarch64-pc-windows-msvc", |
| 100 | url: "https://github.com/openai/codex/releases/download/rust-v0.166.0/codex-package-aarch64-pc-windows-msvc.tar.gz", |
| 101 | sha256: "9383152a72f97bb5dd148493c8214e4fed38d805aa2c3297cb3739f7f130becf", |
| 102 | archiveBytes: 187024318, |
| 103 | }, |
| 104 | "win32-x64": { |
| 105 | version: "0.166.0", |
| 106 | target: "x86_64-pc-windows-msvc", |
| 107 | url: "https://github.com/openai/codex/releases/download/rust-v0.166.0/codex-package-x86_64-pc-windows-msvc.tar.gz", |
| 108 | sha256: "d559bb88e74d471265857b93b7f19bc250dae527673b9191b33534478093f6bc", |
| 109 | archiveBytes: 202589789, |
| 110 | }, |
| 111 | }; |
| 112 | const resolveCodexReleaseAsset = (platform: NodeJS.Platform, arch: string) => |
| 113 | RELEASES[`${platform}-${arch}`] ?? null; |
| 114 | export class CodexInstallationError extends Schema.TaggedError<CodexInstallationError>()( |
| 115 | "CodexInstallationError", |
| 116 | { |
| 117 | operation: Schema.String, |
| 118 | detail: Schema.String, |
| 119 | cause: Schema.optional(Schema.Defect()), |
| 120 | }, |
| 121 | ) { |
| 122 | override get message() { |
| 123 | return this.detail; |
| 124 | } |
| 125 | } |
| 126 | const isInstallationError = Schema.is(CodexInstallationError); |
| 127 | const installationError = (operation: string, detail: string, cause?: unknown) => |
| 128 | new CodexInstallationError({ operation, detail, ...(cause === undefined ? {} : { cause }) }); |
| 129 | const wrapFailure = (operation: string, detail: string) => (cause: unknown) => |
| 130 | isInstallationError(cause) ? cause : installationError(operation, detail, cause); |
| 131 | export interface CodexExecutable { |
| 132 | readonly executablePath: string; |
| 133 | readonly source: "managed" | "local"; |
| 134 | readonly version: string; |
| 135 | readonly managedVersionDirectory: string | null; |
| 136 | } |
| 137 | interface CodexInstallationService { |
| 138 | readonly managedDirectory: string; |
| 139 | readonly resolve: () => Effect.Effect<CodexExecutable, CodexInstallationError>; |
| 140 | readonly acquire: () => Effect.Effect<CodexExecutable, CodexInstallationError, Scope.Scope>; |
| 141 | readonly compatibility: ( |
| 142 | version: string, |
| 143 | ) => Effect.Effect<ServerProviderCompatibilityAdvisory | undefined>; |
| 144 | readonly start: Effect.Effect<ProviderInstallState, CodexInstallationError>; |
| 145 | readonly cancel: ( |
| 146 | operationId: string, |
| 147 | ) => Effect.Effect<ProviderInstallState, CodexInstallationError>; |
| 148 | readonly state: Effect.Effect<ProviderInstallState>; |
| 149 | readonly changes: Stream.Stream<ProviderInstallState>; |
| 150 | /** |
| 151 | * Looks up OpenAI's newest Codex release, at most hourly, and offers it |
| 152 | * through `state.version` when it is newer than the managed install. |
| 153 | */ |
| 154 | readonly checkForUpdate: Effect.Effect<void>; |
| 155 | readonly remove: ( |
| 156 | protectedBinaryPaths?: ReadonlyArray<string>, |
| 157 | ) => Effect.Effect<void, CodexInstallationError>; |
| 158 | } |
| 159 | export class CodexInstallation extends Context.Service< |
| 160 | CodexInstallation, |
| 161 | CodexInstallationService |
| 162 | >()("t3/provider/CodexInstallation") { |
| 163 | static readonly layer = Layer.effect( |
| 164 | CodexInstallation, |
| 165 | Effect.gen(function* () { |
| 166 | const config = yield* ServerConfig.ServerConfig; |
| 167 | return yield* makeCodexInstallation({ baseDir: config.baseDir }); |
| 168 | }), |
| 169 | ); |
| 170 | } |
| 171 | export interface CodexInstallationOptions { |
| 172 | readonly baseDir: string; |
| 173 | readonly releaseAsset?: CodexReleaseAsset | null; |
| 174 | readonly validate?: ( |
| 175 | executable: CodexExecutable, |
| 176 | expectedVersion: string, |
| 177 | ) => Effect.Effect<void, CodexInstallationError, Scope.Scope>; |
| 178 | } |
| 179 | const isRunning = (state: ProviderInstallState) => |
| 180 | ["downloading", "extracting", "verifying"].includes(state.phase); |
| 181 | export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function* ( |
| 182 | options: CodexInstallationOptions, |
| 183 | ) { |
| 184 | const manifestService = yield* ModelManifest.ModelManifest; |
| 185 | const fs = yield* FileSystem.FileSystem; |
| 186 | const path = yield* Path.Path; |
| 187 | const random = yield* SecureRandom.SecureRandom; |
| 188 | const http = yield* HttpClient.HttpClient; |
| 189 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 190 | const serviceScope = yield* Effect.scope; |
| 191 | const platform = yield* HostProcess.Platform; |
| 192 | const environment = yield* HostProcess.Environment; |
| 193 | const arch = yield* HostProcess.Architecture; |
| 194 | const asset = |
| 195 | options.releaseAsset === undefined |
| 196 | ? resolveCodexReleaseAsset(platform, arch) |
| 197 | : options.releaseAsset; |
| 198 | const managedDirectory = path.join(options.baseDir, "tools", "codex"); |
| 199 | const activePath = path.join(managedDirectory, "active.json"); |
| 200 | const executableName = platform === "win32" ? "codex.exe" : "codex"; |
| 201 | const gate = yield* Semaphore.make(1); |
| 202 | const updateCheckGate = yield* Semaphore.make(1); |
| 203 | const leases = new Map<string, number>(); |
| 204 | let running: { readonly operationId: string; readonly fiber: Fiber.Fiber<void> } | undefined; |
| 205 | let upstream: CodexReleaseAsset | null = null; |
| 206 | let upstreamCheckedAt: number | null = null; |
| 207 | |
| 208 | // A local Codex is used as is, so only a managed install is ever updated. |
| 209 | const managedVersion = (current: ProviderInstallState) => |
| 210 | current.source === "managed" ? current.installedVersion : null; |
| 211 | // What `start` installs next. Clients compare `version` with |
| 212 | // `installedVersion` to offer an update. |
| 213 | const offer = (installedVersion: string | null) => { |
| 214 | const release = selectManagedRelease(installedVersion, asset, upstream); |
| 215 | return { |
| 216 | version: release?.version ?? installedVersion, |
| 217 | totalBytes: release?.archiveBytes ?? null, |
| 218 | }; |
| 219 | }; |
| 220 | |
| 221 | const state = yield* SubscriptionRef.make<ProviderInstallState>({ |
| 222 | driver: DRIVER, |
| 223 | operationId: null, |
| 224 | phase: "idle", |
| 225 | downloadedBytes: 0, |
| 226 | ...offer(null), |
| 227 | installedVersion: null, |
| 228 | executablePath: null, |
| 229 | canRemove: false, |
| 230 | message: null, |
| 231 | }); |
| 232 | const readRecord = Effect.fn("CodexInstallation.readRecord")(function* <A>( |
| 233 | file: string, |
| 234 | schema: Schema.Codec<A>, |
| 235 | ) { |
| 236 | const info = yield* fs.stat(file); |
| 237 | if (info.type !== "File" || Number(info.size) > 8192) |
| 238 | return yield* installationError( |
| 239 | "resolve", |
| 240 | "The managed Codex installation record is invalid. Reinstall Codex.", |
| 241 | ); |
| 242 | return yield* Schema.decodeEffect(Schema.fromJsonString(schema))( |
| 243 | yield* fs.readFileString(file), |
| 244 | ); |
| 245 | }); |
| 246 | const fromDirectory = Effect.fn("CodexInstallation.fromDirectory")(function* ( |
| 247 | directory: string, |
| 248 | version: string, |
| 249 | target: string, |
| 250 | ) { |
| 251 | const manifest = yield* readRecord(path.join(directory, "codex-package.json"), PackageManifest); |
| 252 | if ( |
| 253 | manifest.version !== version || |
| 254 | manifest.target !== target || |
| 255 | manifest.entrypoint !== `bin/${executableName}` |
| 256 | ) |
| 257 | return yield* installationError( |
| 258 | "verify", |
| 259 | "The downloaded Codex package does not match the expected release.", |
| 260 | ); |
| 261 | for (const name of [ |
| 262 | `bin/${executableName}`, |
| 263 | `bin/codex-code-mode-host${platform === "win32" ? ".exe" : ""}`, |
| 264 | `codex-path/rg${platform === "win32" ? ".exe" : ""}`, |
| 265 | ]) { |
| 266 | const info = yield* fs.stat(path.join(directory, name)); |
| 267 | if ( |
| 268 | info.type !== "File" || |
| 269 | Number(info.size) === 0 || |
| 270 | (platform !== "win32" && (info.mode & 0o111) === 0) |
| 271 | ) |
| 272 | return yield* installationError( |
| 273 | "verify", |
| 274 | "The managed Codex package is incomplete. Reinstall Codex.", |
| 275 | ); |
| 276 | } |
| 277 | return { |
| 278 | executablePath: path.join(directory, "bin", executableName), |
| 279 | source: "managed", |
| 280 | version, |
| 281 | managedVersionDirectory: directory, |
| 282 | } satisfies CodexExecutable; |
| 283 | }); |
| 284 | const completedRelease = Effect.fn("CodexInstallation.completedRelease")(function* ( |
| 285 | version: string, |
| 286 | ) { |
| 287 | yield* decodeVersion(version); |
| 288 | const directory = path.join(managedDirectory, version); |
| 289 | const record = yield* readRecord( |
| 290 | path.join(directory, ".install-complete.json"), |
| 291 | InstalledRelease, |
| 292 | ); |
| 293 | if (record.version !== version) |
| 294 | return yield* installationError( |
| 295 | "resolve", |
| 296 | "The managed Codex installation record has the wrong version.", |
| 297 | ); |
| 298 | return yield* fromDirectory(directory, version, record.target); |
| 299 | }); |
| 300 | const compatibility = Effect.fn("CodexInstallation.compatibility")(function* (version: string) { |
| 301 | return ModelManifest.providerCompatibility(yield* manifestService.current, DRIVER, version); |
| 302 | }); |
| 303 | const isSupported = (version: string) => |
| 304 | compatibility(version).pipe(Effect.map((advisory) => advisory?.status === "supported")); |
| 305 | // Forgets what the manifest withdrew after it was seen. Managed resolution |
| 306 | // refuses a withdrawn install, so the pin replaces it rather than a re-check. |
| 307 | const keptInstall = Effect.fn("CodexInstallation.keptInstall")(function* ( |
| 308 | installedVersion: string | null, |
| 309 | ) { |
| 310 | if (upstream && !(yield* isSupported(upstream.version))) upstream = null; |
| 311 | return installedVersion !== null && (yield* isSupported(installedVersion)) |
| 312 | ? installedVersion |
| 313 | : null; |
| 314 | }); |
| 315 | // npm names the latest stable release; its GitHub release lists the package |
| 316 | // checksums, so an upstream update stays SHA-256 verified. |
| 317 | const lookupUpstream = Effect.gen(function* () { |
| 318 | if (!asset) return null; |
| 319 | const latest = yield* http |
| 320 | .execute(HttpClientRequest.get(UPSTREAM_LATEST_URL)) |
| 321 | .pipe(Effect.flatMap(HttpClientResponse.filterStatusOk)); |
| 322 | const { version } = yield* latest.json.pipe(Effect.flatMap(decodeNpmLatest)); |
| 323 | // Managed resolution refuses anything else, so offering it would break sessions. |
| 324 | if (!(yield* isSupported(version))) return null; |
| 325 | const releaseUrl = `${UPSTREAM_RELEASES_URL}/rust-v${version}`; |
| 326 | const checksums = yield* http |
| 327 | .execute(HttpClientRequest.get(`${releaseUrl}/codex-package_SHA256SUMS`)) |
| 328 | .pipe( |
| 329 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 330 | Effect.flatMap((response) => response.text), |
| 331 | ); |
| 332 | const fileName = `codex-package-${asset.target}.tar.gz`; |
| 333 | const sha256 = parseChecksums(checksums).get(fileName); |
| 334 | if (!sha256) return null; |
| 335 | return { |
| 336 | version, |
| 337 | target: asset.target, |
| 338 | url: `${releaseUrl}/${fileName}`, |
| 339 | sha256, |
| 340 | archiveBytes: null, |
| 341 | } satisfies CodexReleaseAsset; |
| 342 | }); |
| 343 | // A check already in flight covers concurrent subscribers, including when it fails. |
| 344 | const checkForUpdate = updateCheckGate |
| 345 | .withPermitsIfAvailable(1)( |
| 346 | Effect.gen(function* () { |
| 347 | const now = yield* Clock.currentTimeMillis; |
| 348 | if (upstreamCheckedAt === null || now - upstreamCheckedAt >= UPDATE_CHECK_INTERVAL_MS) { |
| 349 | // A failed lookup counts too, and keeps whatever release was offered before. |
| 350 | upstreamCheckedAt = now; |
| 351 | upstream = yield* lookupUpstream.pipe( |
| 352 | Effect.timeout(UPDATE_CHECK_TIMEOUT), |
| 353 | Effect.orElseSucceed(() => upstream), |
| 354 | ); |
| 355 | } |
| 356 | // A running install already published the release it is installing. |
| 357 | yield* SubscriptionRef.updateEffect(state, (current) => |
| 358 | isRunning(current) |
| 359 | ? Effect.succeed(current) |
| 360 | : keptInstall(managedVersion(current)).pipe( |
| 361 | Effect.map((kept) => ({ ...current, ...offer(kept) })), |
| 362 | ), |
| 363 | ); |
| 364 | }), |
| 365 | ) |
| 366 | .pipe(Effect.ignore); |
| 367 | const resolveManaged = Effect.fn("CodexInstallation.resolveManaged")( |
| 368 | function* () { |
| 369 | const active = yield* readRecord(activePath, ActiveRelease); |
| 370 | const executable = yield* completedRelease(active.version); |
| 371 | const advisory = yield* compatibility(executable.version); |
| 372 | if (advisory?.status !== "supported") |
| 373 | return yield* installationError( |
| 374 | "resolve", |
| 375 | advisory?.message ?? "Update the managed Codex installation to continue.", |
| 376 | ); |
| 377 | return executable; |
| 378 | }, |
| 379 | Effect.mapError( |
| 380 | wrapFailure("resolve", "Codex is not installed in T3 Code. Install it to continue."), |
| 381 | ), |
| 382 | ); |
| 383 | const acquire = Effect.fn("CodexInstallation.acquire")(function* () { |
| 384 | return yield* Effect.acquireRelease( |
| 385 | gate.withPermit( |
| 386 | resolve().pipe( |
| 387 | Effect.tap((executable) => |
| 388 | Effect.sync(() => { |
| 389 | const key = executable.managedVersionDirectory ?? executable.executablePath; |
| 390 | leases.set(key, (leases.get(key) ?? 0) + 1); |
| 391 | }), |
| 392 | ), |
| 393 | ), |
| 394 | ), |
| 395 | (executable) => |
| 396 | Effect.sync(() => { |
| 397 | const key = executable.managedVersionDirectory ?? executable.executablePath; |
| 398 | const remaining = (leases.get(key) ?? 1) - 1; |
| 399 | if (remaining > 0) leases.set(key, remaining); |
| 400 | else leases.delete(key); |
| 401 | }), |
| 402 | ); |
| 403 | }); |
| 404 | const runCommand = Effect.fn("CodexInstallation.runCommand")(function* ( |
| 405 | command: string, |
| 406 | args: ReadonlyArray<string>, |
| 407 | ) { |
| 408 | const resolved = yield* resolveSpawnCommand(command, args).pipe( |
| 409 | Effect.provideService(HostProcess.Platform, platform), |
| 410 | ); |
| 411 | const child = yield* spawner.spawn( |
| 412 | ChildProcess.make(resolved.command, resolved.args, { shell: resolved.shell }), |
| 413 | ); |
| 414 | const [output, , exitCode] = yield* Effect.all( |
| 415 | [ |
| 416 | child.stdout.pipe(Stream.decodeText(), Stream.mkString), |
| 417 | child.stderr.pipe(Stream.runDrain), |
| 418 | child.exitCode, |
| 419 | ], |
| 420 | { concurrency: "unbounded" }, |
| 421 | ); |
| 422 | if (exitCode !== 0) |
| 423 | return yield* installationError( |
| 424 | "verify", |
| 425 | "Could not unpack or start the downloaded Codex package.", |
| 426 | ); |
| 427 | return output; |
| 428 | }, Effect.scoped); |
| 429 | // Native Codex needs its adjacent host; launchers resolve their own payloads. |
| 430 | const hasCodeModeHost = Effect.fn("CodexInstallation.hasCodeModeHost")( |
| 431 | function* (realExecutablePath: string) { |
| 432 | if (/^(?:volta-shim|mise|aqua-proxy)(?:\.exe)?$/iu.test(path.basename(realExecutablePath))) |
| 433 | return true; |
| 434 | if (platform === "win32") { |
| 435 | if (path.extname(realExecutablePath).toLowerCase() !== ".exe") return true; |
| 436 | for (const extension of [".bunx", ".shim"]) { |
| 437 | const marker = yield* fs |
| 438 | .stat(realExecutablePath.slice(0, -4) + extension) |
| 439 | .pipe(Effect.option); |
| 440 | if ( |
| 441 | Option.isSome(marker) && |
| 442 | marker.value.type === "File" && |
| 443 | Number(marker.value.size) > 0 |
| 444 | ) |
| 445 | return true; |
| 446 | } |
| 447 | } else { |
| 448 | const header = yield* fs.open(realExecutablePath, { flag: "r" }).pipe( |
| 449 | Effect.flatMap((file) => file.readAlloc(2)), |
| 450 | Effect.orElseSucceed(() => Option.none()), |
| 451 | ); |
| 452 | if (Option.isSome(header) && header.value[0] === 0x23 && header.value[1] === 0x21) |
| 453 | return true; |
| 454 | } |
| 455 | const info = yield* fs.stat( |
| 456 | path.join( |
| 457 | path.dirname(realExecutablePath), |
| 458 | `codex-code-mode-host${platform === "win32" ? ".exe" : ""}`, |
| 459 | ), |
| 460 | ); |
| 461 | return ( |
| 462 | info.type === "File" && |
| 463 | Number(info.size) > 0 && |
| 464 | (platform === "win32" || (info.mode & 0o111) !== 0) |
| 465 | ); |
| 466 | }, |
| 467 | Effect.scoped, |
| 468 | Effect.orElseSucceed(() => false), |
| 469 | ); |
| 470 | const localCache = new Map<string, { fingerprint: string; executable: CodexExecutable | null }>(); |
| 471 | const resolveLocal = Effect.fn("CodexInstallation.resolveLocal")( |
| 472 | function* () { |
| 473 | const executablePath = yield* resolveCommandPath("codex", { env: environment }).pipe( |
| 474 | Effect.provideService(HostProcess.Platform, platform), |
| 475 | Effect.provideService(FileSystem.FileSystem, fs), |
| 476 | Effect.provideService(Path.Path, path), |
| 477 | ); |
| 478 | if (executablePath === null) return null; |
| 479 | // Keep launcher symlinks intact: version-manager shims dispatch by their invoked name. |
| 480 | const realExecutablePath = yield* fs.realPath(executablePath); |
| 481 | const realManaged = yield* fs.realPath(managedDirectory).pipe(Effect.option); |
| 482 | if ( |
| 483 | realExecutablePath.startsWith( |
| 484 | `${Option.getOrElse(realManaged, () => managedDirectory)}${path.sep}`, |
| 485 | ) |
| 486 | ) |
| 487 | return null; |
| 488 | if (!(yield* hasCodeModeHost(realExecutablePath))) return null; |
| 489 | const info = yield* fs.stat(executablePath); |
| 490 | const fingerprint = `${realExecutablePath}:${info.size}:${Option.getOrUndefined(info.mtime)?.getTime()}:${Option.getOrUndefined(info.ino)}`; |
| 491 | const cached = localCache.get(executablePath); |
| 492 | const executable = |
| 493 | cached?.fingerprint === fingerprint |
| 494 | ? cached.executable |
| 495 | : yield* Effect.gen(function* () { |
| 496 | const output = yield* runCommand(executablePath, ["--version"]); |
| 497 | const version = /^codex-cli (\d+\.\d+\.\d+)$/u.exec(output.trim())?.[1]; |
| 498 | if (!version) return null; |
| 499 | yield* runCommand(executablePath, ["app-server", "--help"]); |
| 500 | return { |
| 501 | executablePath, |
| 502 | source: "local", |
| 503 | version, |
| 504 | managedVersionDirectory: null, |
| 505 | } satisfies CodexExecutable; |
| 506 | }).pipe( |
| 507 | Effect.timeout("5 seconds"), |
| 508 | Effect.orElseSucceed(() => null), |
| 509 | ); |
| 510 | localCache.set(executablePath, { fingerprint, executable }); |
| 511 | if (!executable) return null; |
| 512 | const advisory = yield* compatibility(executable.version); |
| 513 | return advisory?.status === "supported" ? executable : null; |
| 514 | }, |
| 515 | Effect.orElseSucceed(() => null), |
| 516 | ); |
| 517 | const resolve = Effect.fn("CodexInstallation.resolve")(function* () { |
| 518 | const local = yield* resolveLocal(); |
| 519 | return local ?? (yield* resolveManaged()); |
| 520 | }); |
| 521 | const reuseLocal = Effect.fn("CodexInstallation.reuseLocal")(function* () { |
| 522 | const local = yield* resolveLocal(); |
| 523 | if (!local) return false; |
| 524 | yield* SubscriptionRef.update( |
| 525 | state, |
| 526 | (current) => |
| 527 | ({ |
| 528 | ...current, |
| 529 | phase: "succeeded", |
| 530 | source: "local", |
| 531 | operationId: null, |
| 532 | installedVersion: local.version, |
| 533 | executablePath: local.executablePath, |
| 534 | downloadedBytes: 0, |
| 535 | totalBytes: null, |
| 536 | message: null, |
| 537 | }) satisfies ProviderInstallState, |
| 538 | ); |
| 539 | return true; |
| 540 | }); |
| 541 | const withValidationTimeout = Effect.timeoutOrElse({ |
| 542 | duration: "90 seconds", |
| 543 | orElse: () => |
| 544 | Effect.fail( |
| 545 | installationError( |
| 546 | "verify", |
| 547 | "Codex runtime verification timed out after 90 seconds. Check this environment's server logs for startup errors, then retry installation.", |
| 548 | ), |
| 549 | ), |
| 550 | }); |
| 551 | const validate = |
| 552 | options.validate ?? |
| 553 | Effect.fn("CodexInstallation.validate")( |
| 554 | function* (executable: CodexExecutable, version: string) { |
| 555 | const output = yield* runCommand(executable.executablePath, ["--version"]); |
| 556 | if (output.trim() !== `codex-cli ${version}`) |
| 557 | return yield* installationError( |
| 558 | "verify", |
| 559 | "The downloaded Codex executable has the wrong version.", |
| 560 | ); |
| 561 | }, |
| 562 | Effect.mapError(wrapFailure("verify", "The downloaded Codex runtime could not start.")), |
| 563 | ); |
| 564 | // Each update lands in its own directory. Best effort: a busy gate means a session is |
| 565 | // resolving, so the next start or update finishes the cleanup. |
| 566 | const pruneInactiveReleases = Effect.gen(function* () { |
| 567 | const active = yield* readRecord(activePath, ActiveRelease); |
| 568 | for (const entry of yield* fs.readDirectory(managedDirectory)) { |
| 569 | if ( |
| 570 | entry === active.version || |
| 571 | Option.isNone(yield* decodeVersion(entry).pipe(Effect.option)) |
| 572 | ) |
| 573 | continue; |
| 574 | const directory = path.join(managedDirectory, entry); |
| 575 | if (leases.has(directory)) continue; |
| 576 | yield* fs.remove(directory, { recursive: true, force: true }).pipe(Effect.ignore); |
| 577 | } |
| 578 | }).pipe( |
| 579 | gate.withPermitsIfAvailable(1), |
| 580 | Effect.ignore, |
| 581 | Effect.withSpan("CodexInstallation.pruneInactiveReleases"), |
| 582 | ); |
| 583 | |
| 584 | const install = Effect.fn("CodexInstallation.install")( |
| 585 | function* (release: CodexReleaseAsset) { |
| 586 | const ensureEligible = Effect.fn("CodexInstallation.ensureEligible")(function* () { |
| 587 | if (release.version !== asset?.version && !(yield* isSupported(release.version))) { |
| 588 | return yield* installationError( |
| 589 | "verify", |
| 590 | "This Codex release was withdrawn. Check for updates and try again.", |
| 591 | ); |
| 592 | } |
| 593 | }); |
| 594 | yield* decodeVersion(release.version); |
| 595 | yield* fs.makeDirectory(managedDirectory, { recursive: true }); |
| 596 | yield* SubscriptionRef.update(state, (current) => ({ ...current, canRemove: true })); |
| 597 | const destination = path.join(managedDirectory, release.version); |
| 598 | const activate = Effect.fn("CodexInstallation.activate")( |
| 599 | function* () { |
| 600 | const executable = yield* completedRelease(release.version); |
| 601 | const temporary = yield* fs.makeTempDirectoryScoped({ |
| 602 | directory: managedDirectory, |
| 603 | prefix: ".active-", |
| 604 | }); |
| 605 | const pointer = path.join(temporary, "active.json"); |
| 606 | yield* fs.writeFileString(pointer, yield* encodeActive({ version: release.version }), { |
| 607 | mode: 0o600, |
| 608 | flag: "wx", |
| 609 | }); |
| 610 | yield* ensureEligible(); |
| 611 | yield* fs.rename(pointer, activePath); |
| 612 | yield* pruneInactiveReleases; |
| 613 | yield* SubscriptionRef.update( |
| 614 | state, |
| 615 | (current) => |
| 616 | ({ |
| 617 | ...current, |
| 618 | ...offer(release.version), |
| 619 | phase: "succeeded", |
| 620 | source: "managed", |
| 621 | installedVersion: release.version, |
| 622 | executablePath: executable.executablePath, |
| 623 | message: null, |
| 624 | }) satisfies ProviderInstallState, |
| 625 | ); |
| 626 | }, |
| 627 | Effect.scoped, |
| 628 | Effect.uninterruptible, |
| 629 | ); |
| 630 | if (yield* fs.exists(destination)) { |
| 631 | const existing = yield* completedRelease(release.version); |
| 632 | const record = yield* readRecord( |
| 633 | path.join(destination, ".install-complete.json"), |
| 634 | InstalledRelease, |
| 635 | ); |
| 636 | if (record.sha256 !== release.sha256 || record.target !== release.target) |
| 637 | return yield* installationError( |
| 638 | "verify", |
| 639 | "The existing managed Codex release differs from the official package. Remove it and reinstall.", |
| 640 | ); |
| 641 | yield* ensureEligible(); |
| 642 | yield* validate(existing, release.version).pipe(Effect.scoped, withValidationTimeout); |
| 643 | yield* activate(); |
| 644 | return; |
| 645 | } |
| 646 | const staging = yield* fs.makeTempDirectoryScoped({ |
| 647 | directory: managedDirectory, |
| 648 | prefix: ".install-", |
| 649 | }); |
| 650 | const archivePath = path.join(staging, "download.tar.gz"); |
| 651 | const runtime = path.join(staging, "runtime"); |
| 652 | yield* fs.makeDirectory(runtime); |
| 653 | const hash = NodeCrypto.createHash("sha256"); |
| 654 | const maxArchiveBytes = release.archiveBytes ?? UPSTREAM_MAX_ARCHIVE_BYTES; |
| 655 | let downloadedBytes = 0; |
| 656 | let lastProgressAt = yield* Clock.currentTimeMillis; |
| 657 | const response = yield* http |
| 658 | .execute(HttpClientRequest.get(release.url)) |
| 659 | .pipe(Effect.flatMap(HttpClientResponse.filterStatusOk)); |
| 660 | yield* response.stream.pipe( |
| 661 | Stream.tap((chunk) => |
| 662 | Effect.gen(function* () { |
| 663 | downloadedBytes += chunk.byteLength; |
| 664 | if (downloadedBytes > maxArchiveBytes) |
| 665 | return yield* installationError( |
| 666 | "download", |
| 667 | "The Codex download exceeded the expected release size.", |
| 668 | ); |
| 669 | hash.update(chunk); |
| 670 | const now = yield* Clock.currentTimeMillis; |
| 671 | if (now - lastProgressAt >= 250 || downloadedBytes === release.archiveBytes) { |
| 672 | lastProgressAt = now; |
| 673 | yield* SubscriptionRef.update(state, (current) => ({ ...current, downloadedBytes })); |
| 674 | } |
| 675 | }), |
| 676 | ), |
| 677 | Stream.run(fs.sink(archivePath, { flag: "wx", mode: 0o600 })), |
| 678 | Effect.timeout("45 minutes"), |
| 679 | Effect.catchTags({ |
| 680 | TimeoutError: (cause) => |
| 681 | installationError( |
| 682 | "download", |
| 683 | "The Codex download timed out. Retry installation.", |
| 684 | cause, |
| 685 | ), |
| 686 | }), |
| 687 | ); |
| 688 | if ( |
| 689 | (release.archiveBytes !== null && downloadedBytes !== release.archiveBytes) || |
| 690 | hash.digest("hex") !== release.sha256 |
| 691 | ) |
| 692 | return yield* installationError( |
| 693 | "download", |
| 694 | "The Codex download failed its size or SHA-256 check. Nothing was installed.", |
| 695 | ); |
| 696 | yield* SubscriptionRef.update( |
| 697 | state, |
| 698 | (current) => |
| 699 | ({ |
| 700 | ...current, |
| 701 | phase: "extracting", |
| 702 | message: "Extracting Codex.", |
| 703 | }) satisfies ProviderInstallState, |
| 704 | ); |
| 705 | // Bare `tar` on Windows can resolve to Git's MSYS tar, which reads `C:\...` as a remote host. |
| 706 | const tarCommand = cliArchiveTarCommand(platform, environment); |
| 707 | const entries = (yield* runCommand(tarCommand, ["-tzf", archivePath])).trim().split(/\r?\n/u); |
| 708 | const types = (yield* runCommand(tarCommand, ["-tvzf", archivePath])).trim().split(/\r?\n/u); |
| 709 | if ( |
| 710 | entries.length > 10000 || |
| 711 | entries.length !== types.length || |
| 712 | types.some((line) => !["-", "d"].includes(line[0] ?? "")) || |
| 713 | entries.some((entry) => { |
| 714 | const parts = entry.replace(/\/$/u, "").split("/"); |
| 715 | return ( |
| 716 | !entry || |
| 717 | entry.includes("\\") || |
| 718 | entry.startsWith("/") || |
| 719 | parts.some((part) => part === ".." || part === "." || part === "" || part.includes(":")) |
| 720 | ); |
| 721 | }) |
| 722 | ) |
| 723 | return yield* installationError("extract", "The Codex archive contains unsafe entries."); |
| 724 | yield* runCommand(tarCommand, ["-xzf", archivePath, "-C", runtime]); |
| 725 | yield* SubscriptionRef.update( |
| 726 | state, |
| 727 | (current) => |
| 728 | ({ |
| 729 | ...current, |
| 730 | phase: "verifying", |
| 731 | message: "Checking Codex.", |
| 732 | }) satisfies ProviderInstallState, |
| 733 | ); |
| 734 | const executable = yield* fromDirectory(runtime, release.version, release.target); |
| 735 | yield* ensureEligible(); |
| 736 | yield* validate(executable, release.version).pipe(Effect.scoped, withValidationTimeout); |
| 737 | yield* fs.writeFileString( |
| 738 | path.join(runtime, ".install-complete.json"), |
| 739 | yield* encodeRecord({ |
| 740 | version: release.version, |
| 741 | target: release.target, |
| 742 | sha256: release.sha256, |
| 743 | }), |
| 744 | { flag: "wx", mode: 0o600 }, |
| 745 | ); |
| 746 | // Windows Defender or process rundown may retain codex.exe handles briefly after --version exits. |
| 747 | const renameRuntime = fs.rename(runtime, destination); |
| 748 | yield* platform === "win32" |
| 749 | ? renameRuntime.pipe(Effect.retry({ times: 10, schedule: Schedule.spaced("100 millis") })) |
| 750 | : renameRuntime; |
| 751 | yield* activate(); |
| 752 | }, |
| 753 | Effect.scoped, |
| 754 | Effect.catchTags({ |
| 755 | HttpClientError: (cause) => { |
| 756 | const httpStatus = |
| 757 | cause.reason._tag === "StatusCodeError" ? cause.reason.response.status : undefined; |
| 758 | return new CodexInstallationError({ |
| 759 | operation: "download", |
| 760 | detail: |
| 761 | httpStatus === undefined |
| 762 | ? "The Codex download was interrupted. Check your internet connection, then retry installation." |
| 763 | : `The Codex download server returned HTTP ${httpStatus}. Try again later.`, |
| 764 | cause, |
| 765 | }); |
| 766 | }, |
| 767 | }), |
| 768 | Effect.mapError( |
| 769 | wrapFailure( |
| 770 | "install", |
| 771 | "Could not install Codex. Check disk space and directory access, then try again.", |
| 772 | ), |
| 773 | ), |
| 774 | ); |
| 775 | // Nothing newer is known, so "reinstall" re-checks the active runtime rather |
| 776 | // than replacing it with an older pinned release. |
| 777 | const reverify = Effect.fn("CodexInstallation.reverify")( |
| 778 | function* () { |
| 779 | const active = yield* readRecord(activePath, ActiveRelease); |
| 780 | const executable = yield* completedRelease(active.version); |
| 781 | yield* validate(executable, executable.version).pipe(Effect.scoped, withValidationTimeout); |
| 782 | const kept = yield* keptInstall(executable.version); |
| 783 | if (kept === null) { |
| 784 | yield* SubscriptionRef.update(state, (current) => ({ ...current, ...offer(kept) })); |
| 785 | return yield* installationError( |
| 786 | "verify", |
| 787 | "This Codex release was withdrawn. Check for updates and try again.", |
| 788 | ); |
| 789 | } |
| 790 | yield* SubscriptionRef.update( |
| 791 | state, |
| 792 | (current) => |
| 793 | ({ |
| 794 | ...current, |
| 795 | ...offer(kept), |
| 796 | phase: "succeeded", |
| 797 | source: "managed", |
| 798 | installedVersion: executable.version, |
| 799 | executablePath: executable.executablePath, |
| 800 | message: null, |
| 801 | }) satisfies ProviderInstallState, |
| 802 | ); |
| 803 | }, |
| 804 | Effect.mapError( |
| 805 | wrapFailure( |
| 806 | "verify", |
| 807 | "The installed Codex runtime could not start. Remove it and reinstall.", |
| 808 | ), |
| 809 | ), |
| 810 | ); |
| 811 | const start = gate |
| 812 | .withPermit( |
| 813 | Effect.gen(function* () { |
| 814 | const current = yield* SubscriptionRef.get(state); |
| 815 | if (isRunning(current)) return current; |
| 816 | if (yield* reuseLocal()) return yield* SubscriptionRef.get(state); |
| 817 | const installedVersion = yield* keptInstall(managedVersion(current)); |
| 818 | const release = selectManagedRelease(installedVersion, asset, upstream); |
| 819 | if (!release && installedVersion === null) { |
| 820 | return yield* installationError( |
| 821 | "start", |
| 822 | `OpenAI does not publish a Codex runtime for ${platform}-${arch}. Use a supported remote environment or a custom executable.`, |
| 823 | ); |
| 824 | } |
| 825 | const operationId = yield* random.uuidV4(); |
| 826 | const next: ProviderInstallState = { |
| 827 | ...current, |
| 828 | operationId, |
| 829 | phase: release ? "downloading" : "verifying", |
| 830 | downloadedBytes: 0, |
| 831 | ...offer(installedVersion), |
| 832 | message: release ? "Downloading Codex." : "Checking Codex.", |
| 833 | }; |
| 834 | yield* SubscriptionRef.set(state, next); |
| 835 | const work = (release ? install(release) : reverify()).pipe( |
| 836 | Effect.onExit((exit) => |
| 837 | Exit.isFailure(exit) |
| 838 | ? SubscriptionRef.update(state, (value) => { |
| 839 | if (value.operationId !== operationId || value.phase === "succeeded") |
| 840 | return value; |
| 841 | const error = Cause.findErrorOption(exit.cause); |
| 842 | const cancelled = Cause.hasInterruptsOnly(exit.cause); |
| 843 | return { |
| 844 | ...value, |
| 845 | phase: cancelled ? "cancelled" : "failed", |
| 846 | message: cancelled |
| 847 | ? "Installation cancelled. The previous runtime is unchanged." |
| 848 | : Option.isSome(error) |
| 849 | ? error.value.detail |
| 850 | : "Could not finish the Codex installation. Check disk space and directory access.", |
| 851 | } satisfies ProviderInstallState; |
| 852 | }) |
| 853 | : Effect.void, |
| 854 | ), |
| 855 | Effect.ignoreCause, |
| 856 | Effect.ensuring( |
| 857 | Effect.sync(() => { |
| 858 | if (running?.operationId === operationId) running = undefined; |
| 859 | }), |
| 860 | ), |
| 861 | ); |
| 862 | const fiber = yield* Effect.forkIn(Effect.interruptible(work), serviceScope); |
| 863 | running = { operationId, fiber }; |
| 864 | return next; |
| 865 | }).pipe(Effect.uninterruptible), |
| 866 | ) |
| 867 | .pipe(Effect.mapError(wrapFailure("start", "Could not start the Codex installation."))); |
| 868 | |
| 869 | const cancel = Effect.fn("CodexInstallation.cancel")(function* (operationId: string) { |
| 870 | return yield* gate.withPermit( |
| 871 | Effect.gen(function* () { |
| 872 | const current = yield* SubscriptionRef.get(state); |
| 873 | if (current.operationId !== operationId) { |
| 874 | return yield* installationError( |
| 875 | "cancel", |
| 876 | "This installation is no longer current. Refresh its status before cancelling.", |
| 877 | ); |
| 878 | } |
| 879 | if (running?.operationId === operationId && isRunning(current)) { |
| 880 | yield* Fiber.interrupt(running.fiber); |
| 881 | } |
| 882 | return yield* SubscriptionRef.get(state); |
| 883 | }), |
| 884 | ); |
| 885 | }); |
| 886 | |
| 887 | const remove = Effect.fn("CodexInstallation.remove")( |
| 888 | function* (protectedBinaryPaths: ReadonlyArray<string> = []) { |
| 889 | yield* gate.withPermit( |
| 890 | Effect.gen(function* () { |
| 891 | if (isRunning(yield* SubscriptionRef.get(state)) || leases.size > 0) { |
| 892 | return yield* installationError( |
| 893 | "remove", |
| 894 | "Stop Codex sessions and sign-in flows before removing its managed runtime.", |
| 895 | ); |
| 896 | } |
| 897 | const realManaged = yield* fs.realPath(managedDirectory).pipe(Effect.option); |
| 898 | if (Option.isSome(realManaged)) { |
| 899 | for (const binary of protectedBinaryPaths) { |
| 900 | const resolved = yield* fs.realPath(binary).pipe(Effect.option); |
| 901 | const candidate = Option.getOrElse(resolved, () => path.resolve(binary)); |
| 902 | if (candidate.startsWith(`${realManaged.value}${path.sep}`)) |
| 903 | return yield* installationError( |
| 904 | "remove", |
| 905 | "A provider instance uses a custom path inside managed Codex. Clear that path before removing it.", |
| 906 | ); |
| 907 | } |
| 908 | } |
| 909 | yield* fs.remove(managedDirectory, { recursive: true, force: true }); |
| 910 | yield* SubscriptionRef.update( |
| 911 | state, |
| 912 | (current) => |
| 913 | ({ |
| 914 | ...current, |
| 915 | ...offer(null), |
| 916 | operationId: null, |
| 917 | phase: "idle", |
| 918 | downloadedBytes: 0, |
| 919 | installedVersion: null, |
| 920 | executablePath: null, |
| 921 | source: null, |
| 922 | canRemove: false, |
| 923 | message: null, |
| 924 | }) satisfies ProviderInstallState, |
| 925 | ); |
| 926 | yield* reuseLocal(); |
| 927 | }).pipe(Effect.uninterruptible), |
| 928 | ); |
| 929 | }, |
| 930 | Effect.mapError( |
| 931 | wrapFailure( |
| 932 | "remove", |
| 933 | "Could not remove the managed Codex runtime. Check for open processes and try again.", |
| 934 | ), |
| 935 | ), |
| 936 | ); |
| 937 | |
| 938 | yield* Effect.gen(function* () { |
| 939 | // Shared-home installs do not refresh parent mtimes; remove only staging directories far older than the 45-minute download cap. |
| 940 | let canRemove = false; |
| 941 | if (yield* fs.exists(managedDirectory)) { |
| 942 | const entries = yield* fs |
| 943 | .readDirectory(managedDirectory) |
| 944 | .pipe(Effect.orElseSucceed((): ReadonlyArray<string> => [])); |
| 945 | const now = yield* Clock.currentTimeMillis; |
| 946 | for (const entry of entries) { |
| 947 | if (!entry.startsWith(".install-") && !entry.startsWith(".active-")) continue; |
| 948 | const target = path.join(managedDirectory, entry); |
| 949 | const info = yield* fs.stat(target).pipe(Effect.option); |
| 950 | if (Option.isNone(info)) continue; |
| 951 | const modifiedAt = Option.match(info.value.mtime, { |
| 952 | onNone: () => now, |
| 953 | onSome: (date) => date.getTime(), |
| 954 | }); |
| 955 | if (now - modifiedAt < STAGING_STALE_AFTER_MS) continue; |
| 956 | yield* fs.remove(target, { recursive: true, force: true }).pipe(Effect.ignore); |
| 957 | } |
| 958 | // Keep the empty root to avoid racing new staging directories; unreadable roots remain removable. |
| 959 | const remaining = yield* fs.readDirectory(managedDirectory).pipe(Effect.option); |
| 960 | canRemove = Option.isNone(remaining) || remaining.value.length > 0; |
| 961 | } |
| 962 | yield* SubscriptionRef.update(state, (current) => ({ ...current, canRemove })); |
| 963 | if (yield* reuseLocal()) return; |
| 964 | if (!(yield* fs.exists(activePath))) return; |
| 965 | const active = yield* readRecord(activePath, ActiveRelease); |
| 966 | const installed = yield* completedRelease(active.version); |
| 967 | const kept = yield* keptInstall(installed.version); |
| 968 | yield* SubscriptionRef.update( |
| 969 | state, |
| 970 | (current) => |
| 971 | ({ |
| 972 | ...current, |
| 973 | ...offer(kept), |
| 974 | installedVersion: installed.version, |
| 975 | executablePath: installed.executablePath, |
| 976 | source: "managed", |
| 977 | }) satisfies ProviderInstallState, |
| 978 | ); |
| 979 | // A supported older runtime still resolves, so a raised pin needs its own update. |
| 980 | if (asset && compareSemverVersions(installed.version, asset.version) < 0) |
| 981 | yield* start.pipe(Effect.ignore); |
| 982 | }).pipe( |
| 983 | Effect.catch(() => |
| 984 | SubscriptionRef.update( |
| 985 | state, |
| 986 | (current) => |
| 987 | ({ |
| 988 | ...current, |
| 989 | phase: "failed", |
| 990 | message: "The managed Codex runtime is incomplete. Remove it and reinstall.", |
| 991 | }) satisfies ProviderInstallState, |
| 992 | ), |
| 993 | ), |
| 994 | ); |
| 995 | |
| 996 | // A startup install prunes when it activates; pruning now could delete the release it is verifying. |
| 997 | if (!isRunning(yield* SubscriptionRef.get(state))) yield* pruneInactiveReleases; |
| 998 | |
| 999 | return CodexInstallation.of({ |
| 1000 | managedDirectory, |
| 1001 | resolve, |
| 1002 | acquire, |
| 1003 | compatibility, |
| 1004 | start, |
| 1005 | cancel, |
| 1006 | state: SubscriptionRef.get(state), |
| 1007 | changes: SubscriptionRef.changes(state), |
| 1008 | checkForUpdate, |
| 1009 | remove, |
| 1010 | }); |
| 1011 | }); |