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

CodexInstallation.test.ts

746 lines30.8 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Layer from "effect/Layer";
4import * as ModelManifest from "./ModelManifest.ts";
5import { expect, it } from "@effect/vitest";
6import * as HostProcess from "@t3tools/shared/HostProcess";
7import * as Digest from "effect/crypto/Digest";
8import * as Deferred from "effect/Deferred";
9import * as Effect from "effect/Effect";
10import * as Hex from "effect/encoding/Hex";
11import * as FileSystem from "effect/FileSystem";
12import * as Option from "effect/Option";
13import * as PlatformError from "effect/PlatformError";
14import * as Exit from "effect/Exit";
15import * as Queue from "effect/Queue";
16import * as Scope from "effect/Scope";
17import * as Stream from "effect/Stream";
18import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http";
19import * as TestClock from "effect/testing/TestClock";
20import { ChildProcess, ChildProcessSpawner } from "effect/process";
21import * as CodexInstallation from "./CodexInstallation.ts";
22​
23const archive = Buffer.from(
24 "H4sIAAAAAAAC/+3W0W6DIBQGYB/FcD0sKNSkD7J7aom6tmAQtzXL3n3QZM3qdWVt+n8XoCckJp78wLY3q8bu9Ge2HBbUUp7nYD4H8s9zrNdSsCxnWQLT6JULn8ye09K9h/u2/c0/jSM9xqGzo0+bf3Gdf15WQiD/Kdy61/CA+z8dlO9Wrv2387+c5Z9VZY38p7BY0+Gh8t/sVauLt9Ga9Pnnop7ln1e4/6fxRQ7qZCf/qt3YW0M2OX/JyfvljbCCy3XBSaiGH9VqH4tKuaZbC6qG4aDpTrmP3sQV2nh3Gmxvzqsud0vyjaABAAAAAAAAAAAAAAAk8gOq19rvACgAAA==",
25 "base64",
26);
27const 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};
33const 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});
141const 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​
148it.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);
178it.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​
202const 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​
217it.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​
232it.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​
264it.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​
278it.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>
294int 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;
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​
317it.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​
337it.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​
367it.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​
394it.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​
408it.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​
445it.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​
464it.effect("reports a download timeout and cleans the staging directory", () =>
465 Effect.gen(function* () {
466 const started = yield* Deferred.make<void>();
467 const stall = Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never));
468 const h = yield* makeHarness({
469 body: Stream.concat(Stream.succeed(archive.subarray(0, 10)), Stream.fromEffect(stall)),
470 });
471 yield* h.installation.start;
472 yield* Deferred.await(started);
473 yield* TestClock.adjust("45 minutes");
474 expect(yield* terminalState(h.installation)).toMatchObject({
475 phase: "failed",
476 message: "The Codex download timed out. Retry installation.",
477 });
478 expect(h.downloads()).toBe(1);
479 expect(yield* h.fs.readDirectory(h.installation.managedDirectory)).toEqual([]);
480 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
481);
482​
483it.effect("cancels an in-flight download and cleans the staging directory", () =>
484 Effect.gen(function* () {
485 const downloading = yield* Deferred.make<void>();
486 const body = Stream.fromEffect(
487 Deferred.succeed(downloading, undefined).pipe(Effect.andThen(Effect.never)),
488 );
489 const { installation, fs } = yield* makeHarness({ body });
490 const started = yield* installation.start;
491 yield* Deferred.await(downloading);
492 expect((yield* installation.cancel(started.operationId!)).phase).toBe("cancelled");
493 expect(
494 (yield* fs.readDirectory(installation.managedDirectory)).filter((name) =>
495 name.startsWith(".install-"),
496 ),
497 ).toEqual([]);
498 expect(yield* fs.exists(`${installation.managedDirectory}/active.json`)).toBe(false);
499 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
500);
501​
502it.effect("protects active process leases and removes the runtime after release", () =>
503 Effect.gen(function* () {
504 const { installation, fs } = yield* makeHarness();
505 yield* installation.start;
506 yield* terminalState(installation);
507 const leaseScope = yield* Scope.make();
508 yield* installation.acquire().pipe(Effect.provideService(Scope.Scope, leaseScope));
509 expect((yield* Effect.flip(installation.remove())).detail).toContain("Stop Codex sessions");
510 yield* Scope.close(leaseScope, Exit.void);
511 yield* installation.remove();
512 expect(yield* fs.exists(installation.managedDirectory)).toBe(false);
513 expect((yield* installation.state).installedVersion).toBeNull();
514 expect((yield* installation.state).executablePath).toBeNull();
515 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
516);
517​
518it.effect("keeps an activated runtime when a later update fails verification", () =>
519 Effect.gen(function* () {
520 const first = yield* makeHarness();
521 yield* first.installation.start;
522 yield* terminalState(first.installation);
523 const update = yield* makeHarness({
524 baseDir: first.baseDir,
525 options: { releaseAsset: { ...asset, version: "0.156.2", sha256: "0".repeat(64) } },
526 });
527 expect((yield* update.installation.state).executablePath).toBe(
528 (yield* first.installation.resolve()).executablePath,
529 );
530 yield* update.installation.start;
531 expect((yield* terminalState(update.installation)).phase).toBe("failed");
532 expect((yield* update.installation.resolve()).version).toBe("0.156.1");
533 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
534);
535​
536const upstreamVersion = "0.163.0";
537const upstreamArchive = Buffer.from(
538 "H4sIAAAAAAACA+3WzY7CIBQF4D5Kw1oqtNQmPoh7rKStP9BQdMaYefcBE43T9RQ1nm8B7Q1Jk94cYN3peW026juZDvOqsrzO3nj2yofnUK9KwZKUJREcByet/2TymabuPby29S3/NIz0EIbWDC5u/sXf/PO8EAL5j+G/ew1vuP/TXrp2bpunnf/5KP+syCvkP4bJmg5vlf96JxuVbQej4+efi2qUf17g/h/Hhezl2RzdStmhM5osUz5Lyen+RljGF0XGiK/6H9UoF4pS2rpdCCr7fq/oRtqvTocVSjt77k2nr6vud0vyg6ABAAAAAAAAAAAAAABE8gvVbjvaACgAAA==",
539 "base64",
540);
541const upstreamReleaseUrl = `https://github.com/openai/codex/releases/download/rust-v${upstreamVersion}`;
542const upstreamResponses = (checksum: string) => ({
543 "https://registry.npmjs.org/@openai/codex/latest": () =>
544 Response.json({ version: upstreamVersion }),
545 [`${upstreamReleaseUrl}/codex-package_SHA256SUMS`]: () =>
546 new Response(
547 `${"0".repeat(64)} codex-package-x86_64-apple-darwin.tar.gz\n${checksum} codex-package-aarch64-apple-darwin.tar.gz\n`,
548 ),
549 [`${upstreamReleaseUrl}/codex-package-aarch64-apple-darwin.tar.gz`]: () =>
550 new Response(upstreamArchive),
551});
552​
553it.effect("updates a managed install to OpenAI's newest release and never downgrades it", () =>
554 Effect.gen(function* () {
555 const digest = yield* Digest.Digest;
556 const checksum = Hex.encode(
557 yield* digest.digest(Digest.sha256, upstreamArchive).pipe(Effect.orDie),
558 );
559 const h = yield* makeHarness({ responses: upstreamResponses(checksum) });
560 yield* h.installation.checkForUpdate;
561 // A fresh install still gets the pinned release.
562 expect((yield* h.installation.state).version).toBe("0.156.1");
563 yield* h.installation.start;
564 expect(yield* terminalState(h.installation)).toMatchObject({
565 phase: "succeeded",
566 installedVersion: "0.156.1",
567 version: upstreamVersion,
568 });
569​
570 yield* h.installation.start;
571 expect(yield* terminalState(h.installation)).toMatchObject({
572 phase: "succeeded",
573 installedVersion: upstreamVersion,
574 version: upstreamVersion,
575 });
576 expect((yield* h.installation.resolve()).version).toBe(upstreamVersion);
577​
578 // Without upstream information the older pin is not offered as a reinstall.
579 const restarted = yield* makeHarness({ baseDir: h.baseDir });
580 expect(yield* restarted.installation.state).toMatchObject({
581 installedVersion: upstreamVersion,
582 version: upstreamVersion,
583 });
584 yield* restarted.installation.start;
585 expect(yield* terminalState(restarted.installation)).toMatchObject({
586 phase: "succeeded",
587 installedVersion: upstreamVersion,
588 });
589 expect(restarted.downloads()).toBe(0);
590 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
591);
592​
593it.effect("keeps the managed install when an update fails its published checksum", () =>
594 Effect.gen(function* () {
595 const h = yield* makeHarness({ responses: upstreamResponses("f".repeat(64)) });
596 yield* h.installation.start;
597 yield* terminalState(h.installation);
598 yield* h.installation.checkForUpdate;
599 yield* h.installation.start;
600 expect(yield* terminalState(h.installation)).toMatchObject({
601 phase: "failed",
602 installedVersion: "0.156.1",
603 message: expect.stringContaining("SHA-256"),
604 });
605 expect((yield* h.installation.resolve()).version).toBe("0.156.1");
606 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
607);
608​
609it.effect("does not install a cached update the manifest has since withdrawn", () =>
610 Effect.gen(function* () {
611 const digest = yield* Digest.Digest;
612 const checksum = Hex.encode(
613 yield* digest.digest(Digest.sha256, upstreamArchive).pipe(Effect.orDie),
614 );
615 let manifest = ModelManifest.BUNDLED_MODEL_MANIFEST;
616 const h = yield* makeHarness({
617 responses: upstreamResponses(checksum),
618 manifestCurrent: Effect.sync(() => manifest),
619 });
620 yield* h.installation.start;
621 yield* terminalState(h.installation);
622 yield* h.installation.checkForUpdate;
623 expect((yield* h.installation.state).version).toBe(upstreamVersion);
624 manifest = {
625 ...manifest,
626 compatibility: [
627 {
628 driver: "codex",
629 t3CodeRange: ">=0.0.42",
630 ranges: [
631 { range: ">=0.160.0", status: "unsupported" },
632 { range: ">=0.156.0 <0.160.0", status: "supported" },
633 ],
634 },
635 ],
636 };
637 const downloads = h.downloads();
638 yield* h.installation.start;
639 expect(yield* terminalState(h.installation)).toMatchObject({
640 phase: "succeeded",
641 installedVersion: "0.156.1",
642 version: "0.156.1",
643 });
644 expect(h.downloads()).toBe(downloads);
645 expect((yield* h.installation.resolve()).version).toBe("0.156.1");
646 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
647);
648​
649it.effect("returns a managed install the manifest withdraws to the pinned release", () =>
650 Effect.gen(function* () {
651 const digest = yield* Digest.Digest;
652 const checksum = Hex.encode(
653 yield* digest.digest(Digest.sha256, upstreamArchive).pipe(Effect.orDie),
654 );
655 let manifest = ModelManifest.BUNDLED_MODEL_MANIFEST;
656 const h = yield* makeHarness({
657 responses: upstreamResponses(checksum),
658 manifestCurrent: Effect.sync(() => manifest),
659 });
660 yield* h.installation.checkForUpdate;
661 yield* h.installation.start;
662 yield* terminalState(h.installation);
663 yield* h.installation.start;
664 expect(yield* terminalState(h.installation)).toMatchObject({
665 installedVersion: upstreamVersion,
666 });
667​
668 manifest = {
669 ...manifest,
670 compatibility: [
671 {
672 driver: "codex",
673 t3CodeRange: ">=0.0.42",
674 ranges: [
675 { range: ">=0.160.0", status: "unsupported" },
676 { range: ">=0.156.0 <0.160.0", status: "supported" },
677 ],
678 },
679 ],
680 };
681 yield* h.installation.checkForUpdate;
682 expect(yield* h.installation.state).toMatchObject({
683 installedVersion: upstreamVersion,
684 version: "0.156.1",
685 });
686 yield* h.installation.start;
687 expect(yield* terminalState(h.installation)).toMatchObject({
688 phase: "succeeded",
689 installedVersion: "0.156.1",
690 version: "0.156.1",
691 });
692 expect((yield* h.installation.resolve()).version).toBe("0.156.1");
693 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
694);
695​
696it.effect.each(["update", "reinstall"] as const)(
697 "rejects withdrawal during $0 validation",
698 (operation) =>
699 Effect.gen(function* () {
700 const digest = yield* Digest.Digest;
701 const checksum = Hex.encode(
702 yield* digest.digest(Digest.sha256, upstreamArchive).pipe(Effect.orDie),
703 );
704 let manifest = ModelManifest.BUNDLED_MODEL_MANIFEST;
705 let withdraw = operation === "update";
706 const h = yield* makeHarness({
707 responses: upstreamResponses(checksum),
708 manifestCurrent: Effect.sync(() => manifest),
709 options: {
710 validate: (_executable, version) =>
711 Effect.sync(() => {
712 if (withdraw && version === upstreamVersion)
713 manifest = {
714 ...ModelManifest.BUNDLED_MODEL_MANIFEST,
715 compatibility: [
716 {
717 driver: "codex",
718 t3CodeRange: ">=0.0.42",
719 ranges: [
720 { range: ">=0.160.0", status: "unsupported" },
721 { range: ">=0.156.0 <0.160.0", status: "supported" },
722 ],
723 },
724 ],
725 };
726 }),
727 },
728 });
729 yield* h.installation.start;
730 yield* terminalState(h.installation);
731 yield* h.installation.checkForUpdate;
732 if (operation === "reinstall") {
733 yield* h.installation.start;
734 expect((yield* terminalState(h.installation)).phase).toBe("succeeded");
735 withdraw = true;
736 }
737 yield* h.installation.start;
738 expect((yield* terminalState(h.installation)).phase).toBe("failed");
739 if (operation === "update") {
740 expect((yield* h.installation.resolve()).version).toBe("0.156.1");
741 } else {
742 expect((yield* h.installation.state).version).toBe("0.156.1");
743 yield* Effect.flip(h.installation.resolve());
744 }
745 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
746);