Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CodexInstallation.ts

992 lines39.1 KB

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