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 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as ModelManifest from "./ModelManifest.ts"; |
| 5 | import { expect, it } from "@effect/vitest"; |
| 6 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 7 | import * as Digest from "effect/crypto/Digest"; |
| 8 | import * as Deferred from "effect/Deferred"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Hex from "effect/encoding/Hex"; |
| 11 | import * as FileSystem from "effect/FileSystem"; |
| 12 | import * as Option from "effect/Option"; |
| 13 | import * as PlatformError from "effect/PlatformError"; |
| 14 | import * as Exit from "effect/Exit"; |
| 15 | import * as Queue from "effect/Queue"; |
| 16 | import * as Scope from "effect/Scope"; |
| 17 | import * as Stream from "effect/Stream"; |
| 18 | import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http"; |
| 19 | import * as TestClock from "effect/testing/TestClock"; |
| 20 | import { ChildProcess, ChildProcessSpawner } from "effect/process"; |
| 21 | import * as CodexInstallation from "./CodexInstallation.ts"; |
| 22 | |
| 23 | const archive = Buffer.from( |
| 24 | "H4sIAAAAAAAC/+3W0W6DIBQGYB/FcD0sKNSkD7J7aom6tmAQtzXL3n3QZM3qdWVt+n8XoCckJp78wLY3q8bu9Ge2HBbUUp7nYD4H8s9zrNdSsCxnWQLT6JULn8ye09K9h/u2/c0/jSM9xqGzo0+bf3Gdf15WQiD/Kdy61/CA+z8dlO9Wrv2387+c5Z9VZY38p7BY0+Gh8t/sVauLt9Ga9Pnnop7ln1e4/6fxRQ7qZCf/qt3YW0M2OX/JyfvljbCCy3XBSaiGH9VqH4tKuaZbC6qG4aDpTrmP3sQV2nh3Gmxvzqsud0vyjaABAAAAAAAAAAAAAAAk8gOq19rvACgAAA==", |
| 25 | "base64", |
| 26 | ); |
| 27 | const asset = { |
| 28 | version: "0.156.1", |
| 29 | target: "aarch64-apple-darwin", |
| 30 | url: "https://github.com/openai/codex/releases/download/test/package.tar.gz", |
| 31 | archiveBytes: archive.length, |
| 32 | }; |
| 33 | const makeHarness = Effect.fn("test.makeCodexInstallation")(function* ( |
| 34 | input: { |
| 35 | options?: Partial<CodexInstallation.CodexInstallationOptions>; |
| 36 | manifestCurrent?: Effect.Effect<ModelManifest.ModelManifestData>; |
| 37 | body?: Stream.Stream<Uint8Array, HttpClientError.HttpClientError>; |
| 38 | downloadStatus?: number; |
| 39 | baseDir?: string; |
| 40 | local?: { version: string; appServerFails?: boolean; versionFails?: boolean }; |
| 41 | platform?: NodeJS.Platform; |
| 42 | spawner?: ChildProcessSpawner.ChildProcessSpawner["Service"]; |
| 43 | runtimeRenameFailures?: number; |
| 44 | /** Served by URL instead of the pinned package. */ |
| 45 | responses?: Readonly<Record<string, () => Response>>; |
| 46 | } = {}, |
| 47 | ) { |
| 48 | const fs = yield* FileSystem.FileSystem; |
| 49 | const baseDir = |
| 50 | input.baseDir ?? (yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-install-test-" })); |
| 51 | const localDirectory = `${baseDir}/local`; |
| 52 | const localBinaryPath = `${localDirectory}/codex${input.platform === "win32" ? ".exe" : ""}`; |
| 53 | const probeLog = `${baseDir}/local-probes.txt`; |
| 54 | if (input.local) { |
| 55 | yield* fs.makeDirectory(localDirectory, { recursive: true }); |
| 56 | yield* fs.writeFileString( |
| 57 | localBinaryPath, |
| 58 | `#!/bin/sh\nprintf '%s\\n' "$*" >> '${probeLog}'\ncase "$1" in\n--version) ${input.local.versionFails ? "exit 1" : `printf '%s\\n' 'codex-cli ${input.local.version}'`};;\napp-server) exit ${input.local.appServerFails ? "1" : "0"};;\nesac\n`, |
| 59 | { mode: 0o755 }, |
| 60 | ); |
| 61 | } |
| 62 | let downloads = 0; |
| 63 | const digest = yield* Digest.Digest; |
| 64 | const sha256 = Hex.encode(yield* digest.digest(Digest.sha256, archive).pipe(Effect.orDie)); |
| 65 | let runtimeRenameFailures = input.runtimeRenameFailures ?? 0; |
| 66 | const failedRenames = yield* Queue.unbounded<void>(); |
| 67 | const lockingFs: FileSystem.FileSystem = { |
| 68 | ...fs, |
| 69 | // Suspended so each retry of the same effect is a fresh attempt, like the real rename. |
| 70 | rename: (from, to) => |
| 71 | Effect.suspend(() => |
| 72 | /[/\\]runtime$/u.test(from) && runtimeRenameFailures-- > 0 |
| 73 | ? Queue.offer(failedRenames, undefined).pipe( |
| 74 | Effect.andThen( |
| 75 | Effect.fail( |
| 76 | PlatformError.systemError({ |
| 77 | _tag: "PermissionDenied", |
| 78 | module: "FileSystem", |
| 79 | method: "rename", |
| 80 | pathOrDescriptor: from, |
| 81 | }), |
| 82 | ), |
| 83 | ), |
| 84 | ) |
| 85 | : fs.rename(from, to), |
| 86 | ), |
| 87 | }; |
| 88 | const spawner = input.spawner ?? (yield* ChildProcessSpawner.ChildProcessSpawner); |
| 89 | const installation = yield* CodexInstallation.makeCodexInstallation({ |
| 90 | baseDir, |
| 91 | releaseAsset: { ...asset, sha256 }, |
| 92 | validate: () => Effect.void, |
| 93 | ...input.options, |
| 94 | }).pipe( |
| 95 | Effect.provideService(ModelManifest.ModelManifest, { |
| 96 | current: input.manifestCurrent ?? Effect.succeed(ModelManifest.BUNDLED_MODEL_MANIFEST), |
| 97 | refresh: Effect.succeed(ModelManifest.BUNDLED_MODEL_MANIFEST), |
| 98 | forceRefresh: Effect.succeed(ModelManifest.BUNDLED_MODEL_MANIFEST), |
| 99 | refreshOnDemand: Effect.succeed(ModelManifest.BUNDLED_MODEL_MANIFEST), |
| 100 | refreshInBackground: Effect.void, |
| 101 | }), |
| 102 | Effect.provideService(HostProcess.Platform, input.platform ?? "darwin"), |
| 103 | Effect.provideService(HostProcess.Architecture, "arm64"), |
| 104 | Effect.provideService(HostProcess.Environment, { |
| 105 | PATH: input.local ? localDirectory : "", |
| 106 | SystemRoot: "C:\\Windows", |
| 107 | }), |
| 108 | Effect.provideService(FileSystem.FileSystem, lockingFs), |
| 109 | Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 110 | Effect.provideService( |
| 111 | HttpClient.HttpClient, |
| 112 | HttpClient.make((request) => |
| 113 | Effect.sync(() => { |
| 114 | downloads++; |
| 115 | const respond = input.responses?.[request.url]; |
| 116 | if (respond) return HttpClientResponse.fromWeb(request, respond()); |
| 117 | return Object.defineProperty( |
| 118 | HttpClientResponse.fromWeb( |
| 119 | request, |
| 120 | new Response(null, { status: input.downloadStatus ?? 200 }), |
| 121 | ), |
| 122 | "stream", |
| 123 | { |
| 124 | value: input.body ?? Stream.succeed(archive), |
| 125 | }, |
| 126 | ); |
| 127 | }), |
| 128 | ), |
| 129 | ), |
| 130 | ); |
| 131 | return { |
| 132 | installation, |
| 133 | fs, |
| 134 | baseDir, |
| 135 | localBinaryPath, |
| 136 | probeLog, |
| 137 | failedRenames, |
| 138 | downloads: () => downloads, |
| 139 | }; |
| 140 | }); |
| 141 | const terminalState = (installation: CodexInstallation.CodexInstallation["Service"]) => |
| 142 | installation.changes.pipe( |
| 143 | Stream.filter((state) => ["succeeded", "failed", "cancelled"].includes(state.phase)), |
| 144 | Stream.runHead, |
| 145 | Effect.map(Option.getOrThrow), |
| 146 | ); |
| 147 | |
| 148 | it.effect.each(["0.156.0", "0.156.1", "0.156.2", "0.157.0"])( |
| 149 | "reuses installed Codex %s without downloading or taking ownership of it", |
| 150 | (version) => |
| 151 | Effect.gen(function* () { |
| 152 | const h = yield* makeHarness({ local: { version } }); |
| 153 | expect(yield* h.installation.start).toMatchObject({ |
| 154 | source: "local", |
| 155 | phase: "succeeded", |
| 156 | installedVersion: version, |
| 157 | executablePath: h.localBinaryPath, |
| 158 | canRemove: false, |
| 159 | }); |
| 160 | expect(yield* h.installation.resolve()).toMatchObject({ |
| 161 | source: "local", |
| 162 | executablePath: h.localBinaryPath, |
| 163 | managedVersionDirectory: null, |
| 164 | version, |
| 165 | }); |
| 166 | yield* h.installation.acquire().pipe(Effect.scoped); |
| 167 | expect(h.downloads()).toBe(0); |
| 168 | expect(yield* h.fs.exists(h.installation.managedDirectory)).toBe(false); |
| 169 | expect((yield* h.fs.readFileString(h.probeLog)).trim().split("\n")).toEqual([ |
| 170 | "--version", |
| 171 | "app-server --help", |
| 172 | ]); |
| 173 | yield* h.installation.remove(); |
| 174 | expect(yield* h.fs.exists(h.localBinaryPath)).toBe(true); |
| 175 | expect((yield* h.installation.state).source).toBe("local"); |
| 176 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 177 | ); |
| 178 | it.effect.each([ |
| 179 | { version: "0.128.9" }, |
| 180 | { version: "0.145.0" }, |
| 181 | { version: "0.155.1" }, |
| 182 | { version: "0.155.9" }, |
| 183 | { version: "0.156.1-alpha.1" }, |
| 184 | { version: "unknown" }, |
| 185 | { version: "0.156.1", appServerFails: true }, |
| 186 | { version: "0.156.1", versionFails: true }, |
| 187 | ])("downloads the pinned release when the local CLI is unsupported or broken: %j", (local) => |
| 188 | Effect.gen(function* () { |
| 189 | const h = yield* makeHarness({ local }); |
| 190 | expect((yield* h.installation.state).installedVersion).toBeNull(); |
| 191 | yield* h.installation.start; |
| 192 | const installed = yield* terminalState(h.installation); |
| 193 | expect(installed.phase).toBe("succeeded"); |
| 194 | const executable = yield* h.installation.resolve(); |
| 195 | expect(executable.source).toBe("managed"); |
| 196 | expect(installed.executablePath).toBe(executable.executablePath); |
| 197 | expect(h.downloads()).toBe(1); |
| 198 | expect(yield* h.fs.exists(h.localBinaryPath)).toBe(true); |
| 199 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 200 | ); |
| 201 | |
| 202 | const compileNative = Effect.fn("test.compileNative")(function* ( |
| 203 | executablePath: string, |
| 204 | source: string, |
| 205 | ) { |
| 206 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 207 | const compiler = yield* spawner.spawn( |
| 208 | ChildProcess.make("cc", ["-x", "c", "-", "-o", executablePath], { |
| 209 | stdin: Stream.make(new TextEncoder().encode(source)), |
| 210 | stdout: "inherit", |
| 211 | stderr: "inherit", |
| 212 | }), |
| 213 | ); |
| 214 | expect(yield* compiler.exitCode).toBe(0); |
| 215 | }, Effect.scoped); |
| 216 | |
| 217 | it.effect("rechecks compatibility after the local executable is replaced", () => |
| 218 | Effect.gen(function* () { |
| 219 | const h = yield* makeHarness({ local: { version: "0.156.1" } }); |
| 220 | expect((yield* h.installation.resolve()).source).toBe("local"); |
| 221 | yield* h.fs.remove(h.localBinaryPath); |
| 222 | yield* h.fs.writeFileString(h.localBinaryPath, "#!/bin/sh\nprintf 'codex-cli 0.128.9\\n'\n", { |
| 223 | mode: 0o755, |
| 224 | }); |
| 225 | yield* h.installation.start; |
| 226 | expect((yield* terminalState(h.installation)).phase).toBe("succeeded"); |
| 227 | expect((yield* h.installation.resolve()).source).toBe("managed"); |
| 228 | expect(h.downloads()).toBe(1); |
| 229 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 230 | ); |
| 231 | |
| 232 | it.effect("rechecks a cached local executable when the shared manifest policy changes", () => |
| 233 | Effect.gen(function* () { |
| 234 | let manifest = ModelManifest.BUNDLED_MODEL_MANIFEST; |
| 235 | const h = yield* makeHarness({ |
| 236 | local: { version: "0.156.0" }, |
| 237 | manifestCurrent: Effect.sync(() => manifest), |
| 238 | }); |
| 239 | expect((yield* h.installation.resolve()).source).toBe("local"); |
| 240 | manifest = { |
| 241 | ...manifest, |
| 242 | compatibility: [ |
| 243 | { |
| 244 | driver: "codex", |
| 245 | t3CodeRange: ">=0.0.42", |
| 246 | ranges: [ |
| 247 | { range: ">=0.156.1", status: "supported" }, |
| 248 | { range: "<0.156.1", status: "broken" }, |
| 249 | ], |
| 250 | }, |
| 251 | ], |
| 252 | }; |
| 253 | yield* h.installation.start; |
| 254 | expect((yield* terminalState(h.installation)).phase).toBe("succeeded"); |
| 255 | expect((yield* h.installation.resolve()).source).toBe("managed"); |
| 256 | expect(h.downloads()).toBe(1); |
| 257 | expect((yield* h.fs.readFileString(h.probeLog)).trim().split("\n")).toEqual([ |
| 258 | "--version", |
| 259 | "app-server --help", |
| 260 | ]); |
| 261 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 262 | ); |
| 263 | |
| 264 | it.effect("uses bundled Codex compatibility when the remote manifest omits its policy", () => |
| 265 | Effect.gen(function* () { |
| 266 | const h = yield* makeHarness({ |
| 267 | local: { version: "0.156.0" }, |
| 268 | manifestCurrent: Effect.succeed({ |
| 269 | ...ModelManifest.BUNDLED_MODEL_MANIFEST, |
| 270 | compatibility: [], |
| 271 | }), |
| 272 | }); |
| 273 | expect((yield* h.installation.start).source).toBe("local"); |
| 274 | expect(h.downloads()).toBe(0); |
| 275 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 276 | ); |
| 277 | |
| 278 | it.effect.each([ |
| 279 | { kind: "script", name: "volta-shim" }, |
| 280 | { kind: "native", name: "volta-shim" }, |
| 281 | { kind: "native", name: "mise" }, |
| 282 | { kind: "native", name: "aqua-proxy" }, |
| 283 | ])("preserves the invoked name of $kind $name launcher symlinks", ({ kind, name }) => |
| 284 | Effect.gen(function* () { |
| 285 | const h = yield* makeHarness({ local: { version: "0.156.1" } }); |
| 286 | const launcher = `${h.baseDir}/${name}`; |
| 287 | if (kind === "native") { |
| 288 | const payload = `${h.baseDir}/codex.js`; |
| 289 | yield* h.fs.rename(h.localBinaryPath, payload); |
| 290 | yield* compileNative( |
| 291 | launcher, |
| 292 | `#include <string.h> |
| 293 | #include <unistd.h> |
| 294 | int main(int argc, char **argv) { |
| 295 | const char *name = strrchr(argv[0], '/'); |
| 296 | if (strcmp(name ? name + 1 : argv[0], "codex") != 0) return 1; |
| 297 | execv(${JSON.stringify(payload)}, argv); |
| 298 | return 1; |
| 299 | } |
| 300 | `, |
| 301 | ); |
| 302 | } else { |
| 303 | yield* h.fs.writeFileString( |
| 304 | launcher, |
| 305 | '#!/bin/sh\ncase "$0" in */codex) ;; *) exit 1;; esac\ncase "$1" in --version) printf "codex-cli 0.156.1\\n";; app-server) exit 0;; esac\n', |
| 306 | { mode: 0o755 }, |
| 307 | ); |
| 308 | yield* h.fs.remove(h.localBinaryPath); |
| 309 | } |
| 310 | yield* h.fs.symlink(launcher, h.localBinaryPath); |
| 311 | expect(yield* h.installation.start).toMatchObject({ source: "local", phase: "succeeded" }); |
| 312 | expect((yield* h.installation.resolve()).executablePath).toBe(h.localBinaryPath); |
| 313 | expect(h.downloads()).toBe(0); |
| 314 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 315 | ); |
| 316 | |
| 317 | it.effect( |
| 318 | "installs the complete verified package in tools/codex/version and survives a restart", |
| 319 | () => |
| 320 | Effect.gen(function* () { |
| 321 | const { installation, fs, baseDir } = yield* makeHarness(); |
| 322 | yield* installation.start; |
| 323 | expect((yield* terminalState(installation)).phase).toBe("succeeded"); |
| 324 | const executable = yield* installation.resolve(); |
| 325 | expect(executable.executablePath).toBe(`${baseDir}/tools/codex/0.156.1/bin/codex`); |
| 326 | expect( |
| 327 | yield* fs.readFileString(`${executable.managedVersionDirectory}/bin/codex-code-mode-host`), |
| 328 | ).toBe("host"); |
| 329 | expect(yield* fs.readFileString(`${executable.managedVersionDirectory}/codex-path/rg`)).toBe( |
| 330 | "rg", |
| 331 | ); |
| 332 | const restarted = yield* makeHarness({ baseDir }); |
| 333 | expect((yield* restarted.installation.resolve()).version).toBe("0.156.1"); |
| 334 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 335 | ); |
| 336 | |
| 337 | it.effect("requires an update before running an older activated managed installation", () => |
| 338 | Effect.gen(function* () { |
| 339 | const first = yield* makeHarness(); |
| 340 | yield* first.installation.start; |
| 341 | yield* terminalState(first.installation); |
| 342 | const executable = yield* first.installation.resolve(); |
| 343 | const oldDirectory = `${first.installation.managedDirectory}/0.155.1`; |
| 344 | yield* first.fs.rename(executable.managedVersionDirectory!, oldDirectory); |
| 345 | for (const name of ["codex-package.json", ".install-complete.json"]) { |
| 346 | const file = `${oldDirectory}/${name}`; |
| 347 | yield* first.fs.writeFileString( |
| 348 | file, |
| 349 | (yield* first.fs.readFileString(file)).replaceAll("0.156.1", "0.155.1"), |
| 350 | ); |
| 351 | } |
| 352 | yield* first.fs.writeFileString( |
| 353 | `${first.installation.managedDirectory}/active.json`, |
| 354 | '{"version":"0.155.1"}', |
| 355 | ); |
| 356 | const restarted = yield* makeHarness({ baseDir: first.baseDir }); |
| 357 | expect((yield* Effect.flip(restarted.installation.resolve())).detail).toContain( |
| 358 | "outside the supported range", |
| 359 | ); |
| 360 | yield* restarted.installation.start; |
| 361 | expect((yield* terminalState(restarted.installation)).phase).toBe("succeeded"); |
| 362 | expect((yield* restarted.installation.resolve()).version).toBe("0.156.1"); |
| 363 | expect(restarted.downloads()).toBe(1); |
| 364 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 365 | ); |
| 366 | |
| 367 | it.effect("updates an older supported managed installation to the pinned release on startup", () => |
| 368 | Effect.gen(function* () { |
| 369 | const first = yield* makeHarness(); |
| 370 | yield* first.installation.start; |
| 371 | yield* terminalState(first.installation); |
| 372 | const executable = yield* first.installation.resolve(); |
| 373 | const oldDirectory = `${first.installation.managedDirectory}/0.156.0`; |
| 374 | yield* first.fs.rename(executable.managedVersionDirectory!, oldDirectory); |
| 375 | for (const name of ["codex-package.json", ".install-complete.json"]) { |
| 376 | const file = `${oldDirectory}/${name}`; |
| 377 | yield* first.fs.writeFileString( |
| 378 | file, |
| 379 | (yield* first.fs.readFileString(file)).replaceAll("0.156.1", "0.156.0"), |
| 380 | ); |
| 381 | } |
| 382 | yield* first.fs.writeFileString( |
| 383 | `${first.installation.managedDirectory}/active.json`, |
| 384 | '{"version":"0.156.0"}', |
| 385 | ); |
| 386 | const restarted = yield* makeHarness({ baseDir: first.baseDir }); |
| 387 | expect((yield* restarted.installation.state).operationId).not.toBeNull(); |
| 388 | expect((yield* terminalState(restarted.installation)).phase).toBe("succeeded"); |
| 389 | expect((yield* restarted.installation.resolve()).version).toBe("0.156.1"); |
| 390 | expect(restarted.downloads()).toBe(1); |
| 391 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 392 | ); |
| 393 | |
| 394 | it.effect("rejects corrupted downloads without publishing a runtime", () => |
| 395 | Effect.gen(function* () { |
| 396 | const { installation, fs } = yield* makeHarness({ |
| 397 | options: { releaseAsset: { ...asset, sha256: "0".repeat(64) } }, |
| 398 | }); |
| 399 | yield* installation.start; |
| 400 | const state = yield* terminalState(installation); |
| 401 | expect(state.phase).toBe("failed"); |
| 402 | expect(state.message).toContain("SHA-256"); |
| 403 | expect(yield* fs.exists(`${installation.managedDirectory}/active.json`)).toBe(false); |
| 404 | expect(yield* fs.exists(`${installation.managedDirectory}/0.156.1`)).toBe(false); |
| 405 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 406 | ); |
| 407 | |
| 408 | it.effect.each(["interrupted body", "HTTP 404"] as const)( |
| 409 | "reports a download failure for %s without blaming disk access", |
| 410 | (failure) => |
| 411 | Effect.gen(function* () { |
| 412 | const request = HttpClientRequest.get(asset.url); |
| 413 | const response = HttpClientResponse.fromWeb(request, new Response(null)); |
| 414 | const h = yield* makeHarness( |
| 415 | failure === "HTTP 404" |
| 416 | ? { downloadStatus: 404 } |
| 417 | : { |
| 418 | body: Stream.concat( |
| 419 | Stream.succeed(archive.subarray(0, 10)), |
| 420 | Stream.fail( |
| 421 | new HttpClientError.HttpClientError({ |
| 422 | reason: new HttpClientError.DecodeError({ |
| 423 | request, |
| 424 | response, |
| 425 | cause: new TypeError("terminated"), |
| 426 | }), |
| 427 | }), |
| 428 | ), |
| 429 | ), |
| 430 | }, |
| 431 | ); |
| 432 | yield* h.installation.start; |
| 433 | expect(yield* terminalState(h.installation)).toMatchObject({ |
| 434 | phase: "failed", |
| 435 | message: |
| 436 | failure === "HTTP 404" |
| 437 | ? "The Codex download server returned HTTP 404. Try again later." |
| 438 | : "The Codex download was interrupted. Check your internet connection, then retry installation.", |
| 439 | }); |
| 440 | expect(h.downloads()).toBe(1); |
| 441 | expect(yield* h.fs.readDirectory(h.installation.managedDirectory)).toEqual([]); |
| 442 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 443 | ); |
| 444 | |
| 445 | it.effect( |
| 446 | "preserves the filesystem failure message when installation cannot create its directory", |
| 447 | () => |
| 448 | Effect.gen(function* () { |
| 449 | const fs = yield* FileSystem.FileSystem; |
| 450 | const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-install-test-" }); |
| 451 | const baseDir = `${directory}/not-a-directory`; |
| 452 | yield* fs.writeFileString(baseDir, "existing file"); |
| 453 | const h = yield* makeHarness({ baseDir }); |
| 454 | yield* h.installation.start; |
| 455 | expect(yield* terminalState(h.installation)).toMatchObject({ |
| 456 | phase: "failed", |
| 457 | message: "Could not install Codex. Check disk space and directory access, then try again.", |
| 458 | }); |
| 459 | expect(h.downloads()).toBe(0); |
| 460 | expect(yield* fs.readFileString(baseDir)).toBe("existing file"); |
| 461 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 462 | ); |
| 463 | |
| 464 | it.effect.each(["cached update", "reverify"] as const)( |
| 465 | "reports a verification timeout during %s and preserves the managed runtime", |
| 466 | (operation) => |
| 467 | Effect.gen(function* () { |
| 468 | const fs = yield* FileSystem.FileSystem; |
| 469 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-verify-test-" }); |
| 470 | const managed = `${baseDir}/tools/codex`; |
| 471 | const installedVersion = operation === "cached update" ? "0.156.0" : asset.version; |
| 472 | const target = "aarch64-pc-windows-msvc"; |
| 473 | const checksum = "a".repeat(64); |
| 474 | for (const version of new Set([installedVersion, asset.version])) { |
| 475 | const directory = `${managed}/${version}`; |
| 476 | yield* fs.makeDirectory(`${directory}/bin`, { recursive: true }); |
| 477 | yield* fs.makeDirectory(`${directory}/codex-path`, { recursive: true }); |
| 478 | for (const name of ["bin/codex.exe", "bin/codex-code-mode-host.exe", "codex-path/rg.exe"]) { |
| 479 | yield* fs.writeFileString(`${directory}/${name}`, "fixture executable"); |
| 480 | } |
| 481 | yield* fs.writeFileString( |
| 482 | `${directory}/codex-package.json`, |
| 483 | JSON.stringify({ layoutVersion: 1, version, target, entrypoint: "bin/codex.exe" }), |
| 484 | ); |
| 485 | yield* fs.writeFileString( |
| 486 | `${directory}/.install-complete.json`, |
| 487 | JSON.stringify({ version, target, sha256: checksum }), |
| 488 | ); |
| 489 | } |
| 490 | const pointer = JSON.stringify({ version: installedVersion }); |
| 491 | yield* fs.writeFileString(`${managed}/active.json`, pointer); |
| 492 | const entered = yield* Deferred.make<void>(); |
| 493 | const h = yield* makeHarness({ |
| 494 | baseDir, |
| 495 | platform: "win32", |
| 496 | manifestCurrent: Effect.succeed({ |
| 497 | ...ModelManifest.BUNDLED_MODEL_MANIFEST, |
| 498 | compatibility: [ |
| 499 | { |
| 500 | driver: "codex", |
| 501 | t3CodeRange: ">=0.0.0", |
| 502 | ranges: [{ range: ">=0.156.0", status: "supported" }], |
| 503 | }, |
| 504 | ], |
| 505 | }), |
| 506 | options: { |
| 507 | releaseAsset: operation === "reverify" ? null : { ...asset, target, sha256: checksum }, |
| 508 | validate: () => Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)), |
| 509 | }, |
| 510 | }); |
| 511 | yield* h.installation.start; |
| 512 | yield* Deferred.await(entered); |
| 513 | yield* TestClock.adjust("90 seconds"); |
| 514 | expect(yield* terminalState(h.installation)).toMatchObject({ |
| 515 | phase: "failed", |
| 516 | installedVersion, |
| 517 | message: |
| 518 | "Codex runtime verification timed out after 90 seconds. Check this environment's server logs for startup errors, then retry installation.", |
| 519 | }); |
| 520 | expect(yield* fs.readFileString(`${managed}/active.json`)).toBe(pointer); |
| 521 | expect((yield* h.installation.resolve()).version).toBe(installedVersion); |
| 522 | expect(h.downloads()).toBe(0); |
| 523 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 524 | ); |
| 525 | |
| 526 | it.effect("reports a download timeout and cleans the staging directory", () => |
| 527 | Effect.gen(function* () { |
| 528 | const started = yield* Deferred.make<void>(); |
| 529 | const stall = Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)); |
| 530 | const h = yield* makeHarness({ |
| 531 | body: Stream.concat(Stream.succeed(archive.subarray(0, 10)), Stream.fromEffect(stall)), |
| 532 | }); |
| 533 | yield* h.installation.start; |
| 534 | yield* Deferred.await(started); |
| 535 | yield* TestClock.adjust("45 minutes"); |
| 536 | expect(yield* terminalState(h.installation)).toMatchObject({ |
| 537 | phase: "failed", |
| 538 | message: "The Codex download timed out. Retry installation.", |
| 539 | }); |
| 540 | expect(h.downloads()).toBe(1); |
| 541 | expect(yield* h.fs.readDirectory(h.installation.managedDirectory)).toEqual([]); |
| 542 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 543 | ); |
| 544 | |
| 545 | it.effect("cancels an in-flight download and cleans the staging directory", () => |
| 546 | Effect.gen(function* () { |
| 547 | const downloading = yield* Deferred.make<void>(); |
| 548 | const body = Stream.fromEffect( |
| 549 | Deferred.succeed(downloading, undefined).pipe(Effect.andThen(Effect.never)), |
| 550 | ); |
| 551 | const { installation, fs } = yield* makeHarness({ body }); |
| 552 | const started = yield* installation.start; |
| 553 | yield* Deferred.await(downloading); |
| 554 | expect((yield* installation.cancel(started.operationId!)).phase).toBe("cancelled"); |
| 555 | expect( |
| 556 | (yield* fs.readDirectory(installation.managedDirectory)).filter((name) => |
| 557 | name.startsWith(".install-"), |
| 558 | ), |
| 559 | ).toEqual([]); |
| 560 | expect(yield* fs.exists(`${installation.managedDirectory}/active.json`)).toBe(false); |
| 561 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 562 | ); |
| 563 | |
| 564 | it.effect("protects active process leases and removes the runtime after release", () => |
| 565 | Effect.gen(function* () { |
| 566 | const { installation, fs } = yield* makeHarness(); |
| 567 | yield* installation.start; |
| 568 | yield* terminalState(installation); |
| 569 | const leaseScope = yield* Scope.make(); |
| 570 | yield* installation.acquire().pipe(Effect.provideService(Scope.Scope, leaseScope)); |
| 571 | expect((yield* Effect.flip(installation.remove())).detail).toContain("Stop Codex sessions"); |
| 572 | yield* Scope.close(leaseScope, Exit.void); |
| 573 | yield* installation.remove(); |
| 574 | expect(yield* fs.exists(installation.managedDirectory)).toBe(false); |
| 575 | expect((yield* installation.state).installedVersion).toBeNull(); |
| 576 | expect((yield* installation.state).executablePath).toBeNull(); |
| 577 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 578 | ); |
| 579 | |
| 580 | it.effect("keeps an activated runtime when a later update fails verification", () => |
| 581 | Effect.gen(function* () { |
| 582 | const first = yield* makeHarness(); |
| 583 | yield* first.installation.start; |
| 584 | yield* terminalState(first.installation); |
| 585 | const update = yield* makeHarness({ |
| 586 | baseDir: first.baseDir, |
| 587 | options: { releaseAsset: { ...asset, version: "0.156.2", sha256: "0".repeat(64) } }, |
| 588 | }); |
| 589 | expect((yield* update.installation.state).executablePath).toBe( |
| 590 | (yield* first.installation.resolve()).executablePath, |
| 591 | ); |
| 592 | yield* update.installation.start; |
| 593 | expect((yield* terminalState(update.installation)).phase).toBe("failed"); |
| 594 | expect((yield* update.installation.resolve()).version).toBe("0.156.1"); |
| 595 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 596 | ); |
| 597 | |
| 598 | const upstreamVersion = "0.163.0"; |
| 599 | const upstreamArchive = Buffer.from( |
| 600 | "H4sIAAAAAAACA+3WzY7CIBQF4D5Kw1oqtNQmPoh7rKStP9BQdMaYefcBE43T9RQ1nm8B7Q1Jk94cYN3peW026juZDvOqsrzO3nj2yofnUK9KwZKUJREcByet/2TymabuPby29S3/NIz0EIbWDC5u/sXf/PO8EAL5j+G/ew1vuP/TXrp2bpunnf/5KP+syCvkP4bJmg5vlf96JxuVbQej4+efi2qUf17g/h/Hhezl2RzdStmhM5osUz5Lyen+RljGF0XGiK/6H9UoF4pS2rpdCCr7fq/oRtqvTocVSjt77k2nr6vud0vyg6ABAAAAAAAAAAAAAABE8gvVbjvaACgAAA==", |
| 601 | "base64", |
| 602 | ); |
| 603 | const upstreamReleaseUrl = `https://github.com/openai/codex/releases/download/rust-v${upstreamVersion}`; |
| 604 | const upstreamResponses = (checksum: string) => ({ |
| 605 | "https://registry.npmjs.org/@openai/codex/latest": () => |
| 606 | Response.json({ version: upstreamVersion }), |
| 607 | [`${upstreamReleaseUrl}/codex-package_SHA256SUMS`]: () => |
| 608 | new Response( |
| 609 | `${"0".repeat(64)} codex-package-x86_64-apple-darwin.tar.gz\n${checksum} codex-package-aarch64-apple-darwin.tar.gz\n`, |
| 610 | ), |
| 611 | [`${upstreamReleaseUrl}/codex-package-aarch64-apple-darwin.tar.gz`]: () => |
| 612 | new Response(upstreamArchive), |
| 613 | }); |
| 614 | |
| 615 | it.effect("updates a managed install to OpenAI's newest release and never downgrades it", () => |
| 616 | Effect.gen(function* () { |
| 617 | const digest = yield* Digest.Digest; |
| 618 | const checksum = Hex.encode( |
| 619 | yield* digest.digest(Digest.sha256, upstreamArchive).pipe(Effect.orDie), |
| 620 | ); |
| 621 | const h = yield* makeHarness({ responses: upstreamResponses(checksum) }); |
| 622 | yield* h.installation.checkForUpdate; |
| 623 | // A fresh install still gets the pinned release. |
| 624 | expect((yield* h.installation.state).version).toBe("0.156.1"); |
| 625 | yield* h.installation.start; |
| 626 | expect(yield* terminalState(h.installation)).toMatchObject({ |
| 627 | phase: "succeeded", |
| 628 | installedVersion: "0.156.1", |
| 629 | version: upstreamVersion, |
| 630 | }); |
| 631 | |
| 632 | const sessionScope = yield* Scope.make(); |
| 633 | const previous = yield* h.installation.acquire().pipe(Scope.provide(sessionScope)); |
| 634 | yield* h.installation.start; |
| 635 | expect(yield* terminalState(h.installation)).toMatchObject({ |
| 636 | phase: "succeeded", |
| 637 | installedVersion: upstreamVersion, |
| 638 | version: upstreamVersion, |
| 639 | }); |
| 640 | expect((yield* h.installation.resolve()).version).toBe(upstreamVersion); |
| 641 | // The running session keeps its release until the next start prunes it. |
| 642 | expect(yield* h.fs.exists(previous.managedVersionDirectory!)).toBe(true); |
| 643 | yield* Scope.close(sessionScope, Exit.void); |
| 644 | |
| 645 | // Without upstream information the older pin is not offered as a reinstall. |
| 646 | const restarted = yield* makeHarness({ baseDir: h.baseDir }); |
| 647 | expect(yield* h.fs.exists(previous.managedVersionDirectory!)).toBe(false); |
| 648 | expect(yield* restarted.installation.state).toMatchObject({ |
| 649 | installedVersion: upstreamVersion, |
| 650 | version: upstreamVersion, |
| 651 | }); |
| 652 | yield* restarted.installation.start; |
| 653 | expect(yield* terminalState(restarted.installation)).toMatchObject({ |
| 654 | phase: "succeeded", |
| 655 | installedVersion: upstreamVersion, |
| 656 | }); |
| 657 | expect(restarted.downloads()).toBe(0); |
| 658 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 659 | ); |
| 660 | |
| 661 | it.effect("keeps the managed install when an update fails its published checksum", () => |
| 662 | Effect.gen(function* () { |
| 663 | const h = yield* makeHarness({ responses: upstreamResponses("f".repeat(64)) }); |
| 664 | yield* h.installation.start; |
| 665 | yield* terminalState(h.installation); |
| 666 | yield* h.installation.checkForUpdate; |
| 667 | yield* h.installation.start; |
| 668 | expect(yield* terminalState(h.installation)).toMatchObject({ |
| 669 | phase: "failed", |
| 670 | installedVersion: "0.156.1", |
| 671 | message: expect.stringContaining("SHA-256"), |
| 672 | }); |
| 673 | expect((yield* h.installation.resolve()).version).toBe("0.156.1"); |
| 674 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 675 | ); |
| 676 | |
| 677 | it.effect("does not install a cached update the manifest has since withdrawn", () => |
| 678 | Effect.gen(function* () { |
| 679 | const digest = yield* Digest.Digest; |
| 680 | const checksum = Hex.encode( |
| 681 | yield* digest.digest(Digest.sha256, upstreamArchive).pipe(Effect.orDie), |
| 682 | ); |
| 683 | let manifest = ModelManifest.BUNDLED_MODEL_MANIFEST; |
| 684 | const h = yield* makeHarness({ |
| 685 | responses: upstreamResponses(checksum), |
| 686 | manifestCurrent: Effect.sync(() => manifest), |
| 687 | }); |
| 688 | yield* h.installation.start; |
| 689 | yield* terminalState(h.installation); |
| 690 | yield* h.installation.checkForUpdate; |
| 691 | expect((yield* h.installation.state).version).toBe(upstreamVersion); |
| 692 | manifest = { |
| 693 | ...manifest, |
| 694 | compatibility: [ |
| 695 | { |
| 696 | driver: "codex", |
| 697 | t3CodeRange: ">=0.0.42", |
| 698 | ranges: [ |
| 699 | { range: ">=0.160.0", status: "unsupported" }, |
| 700 | { range: ">=0.156.0 <0.160.0", status: "supported" }, |
| 701 | ], |
| 702 | }, |
| 703 | ], |
| 704 | }; |
| 705 | const downloads = h.downloads(); |
| 706 | yield* h.installation.start; |
| 707 | expect(yield* terminalState(h.installation)).toMatchObject({ |
| 708 | phase: "succeeded", |
| 709 | installedVersion: "0.156.1", |
| 710 | version: "0.156.1", |
| 711 | }); |
| 712 | expect(h.downloads()).toBe(downloads); |
| 713 | expect((yield* h.installation.resolve()).version).toBe("0.156.1"); |
| 714 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 715 | ); |
| 716 | |
| 717 | it.effect("returns a managed install the manifest withdraws to the pinned release", () => |
| 718 | Effect.gen(function* () { |
| 719 | const digest = yield* Digest.Digest; |
| 720 | const checksum = Hex.encode( |
| 721 | yield* digest.digest(Digest.sha256, upstreamArchive).pipe(Effect.orDie), |
| 722 | ); |
| 723 | let manifest = ModelManifest.BUNDLED_MODEL_MANIFEST; |
| 724 | const h = yield* makeHarness({ |
| 725 | responses: upstreamResponses(checksum), |
| 726 | manifestCurrent: Effect.sync(() => manifest), |
| 727 | }); |
| 728 | yield* h.installation.checkForUpdate; |
| 729 | yield* h.installation.start; |
| 730 | yield* terminalState(h.installation); |
| 731 | yield* h.installation.start; |
| 732 | expect(yield* terminalState(h.installation)).toMatchObject({ |
| 733 | installedVersion: upstreamVersion, |
| 734 | }); |
| 735 | |
| 736 | manifest = { |
| 737 | ...manifest, |
| 738 | compatibility: [ |
| 739 | { |
| 740 | driver: "codex", |
| 741 | t3CodeRange: ">=0.0.42", |
| 742 | ranges: [ |
| 743 | { range: ">=0.160.0", status: "unsupported" }, |
| 744 | { range: ">=0.156.0 <0.160.0", status: "supported" }, |
| 745 | ], |
| 746 | }, |
| 747 | ], |
| 748 | }; |
| 749 | yield* h.installation.checkForUpdate; |
| 750 | expect(yield* h.installation.state).toMatchObject({ |
| 751 | installedVersion: upstreamVersion, |
| 752 | version: "0.156.1", |
| 753 | }); |
| 754 | yield* h.installation.start; |
| 755 | expect(yield* terminalState(h.installation)).toMatchObject({ |
| 756 | phase: "succeeded", |
| 757 | installedVersion: "0.156.1", |
| 758 | version: "0.156.1", |
| 759 | }); |
| 760 | expect((yield* h.installation.resolve()).version).toBe("0.156.1"); |
| 761 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 762 | ); |
| 763 | |
| 764 | it.effect.each(["update", "reinstall"] as const)( |
| 765 | "rejects withdrawal during $0 validation", |
| 766 | (operation) => |
| 767 | Effect.gen(function* () { |
| 768 | const digest = yield* Digest.Digest; |
| 769 | const checksum = Hex.encode( |
| 770 | yield* digest.digest(Digest.sha256, upstreamArchive).pipe(Effect.orDie), |
| 771 | ); |
| 772 | let manifest = ModelManifest.BUNDLED_MODEL_MANIFEST; |
| 773 | let withdraw = operation === "update"; |
| 774 | const h = yield* makeHarness({ |
| 775 | responses: upstreamResponses(checksum), |
| 776 | manifestCurrent: Effect.sync(() => manifest), |
| 777 | options: { |
| 778 | validate: (_executable, version) => |
| 779 | Effect.sync(() => { |
| 780 | if (withdraw && version === upstreamVersion) |
| 781 | manifest = { |
| 782 | ...ModelManifest.BUNDLED_MODEL_MANIFEST, |
| 783 | compatibility: [ |
| 784 | { |
| 785 | driver: "codex", |
| 786 | t3CodeRange: ">=0.0.42", |
| 787 | ranges: [ |
| 788 | { range: ">=0.160.0", status: "unsupported" }, |
| 789 | { range: ">=0.156.0 <0.160.0", status: "supported" }, |
| 790 | ], |
| 791 | }, |
| 792 | ], |
| 793 | }; |
| 794 | }), |
| 795 | }, |
| 796 | }); |
| 797 | yield* h.installation.start; |
| 798 | yield* terminalState(h.installation); |
| 799 | yield* h.installation.checkForUpdate; |
| 800 | if (operation === "reinstall") { |
| 801 | yield* h.installation.start; |
| 802 | expect((yield* terminalState(h.installation)).phase).toBe("succeeded"); |
| 803 | withdraw = true; |
| 804 | } |
| 805 | yield* h.installation.start; |
| 806 | expect((yield* terminalState(h.installation)).phase).toBe("failed"); |
| 807 | if (operation === "update") { |
| 808 | expect((yield* h.installation.resolve()).version).toBe("0.156.1"); |
| 809 | } else { |
| 810 | expect((yield* h.installation.state).version).toBe("0.156.1"); |
| 811 | yield* Effect.flip(h.installation.resolve()); |
| 812 | } |
| 813 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 814 | ); |