Skip to content

Commit 5e222be

Browse files
pschaubmaria-rcks
andauthored
fix(server): stop reporting Codex HTTP failures as disk errors (#22308)
Co-authored-by: maria-rcks <[email protected]>
1 parent b5a5ab4 commit 5e222be

2 files changed

Lines changed: 105 additions & 3 deletions

File tree

‎apps/server/src/provider/CodexInstallation.test.ts‎

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import * as Exit from "effect/Exit";
1313
import * as Queue from "effect/Queue";
1414
import * as Scope from "effect/Scope";
1515
import * as Stream from "effect/Stream";
16-
import { HttpClient, HttpClientResponse } from "effect/http";
16+
import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http";
17+
import * as TestClock from "effect/testing/TestClock";
1718
import { ChildProcess, ChildProcessSpawner } from "effect/process";
1819
import * as CodexInstallation from "./CodexInstallation.ts";
1920

@@ -31,7 +32,8 @@ const makeHarness = Effect.fn("test.makeCodexInstallation")(function* (
3132
input: {
3233
options?: Partial<CodexInstallation.CodexInstallationOptions>;
3334
manifestCurrent?: Effect.Effect<ModelManifest.ModelManifestData>;
34-
body?: Stream.Stream<Uint8Array>;
35+
body?: Stream.Stream<Uint8Array, HttpClientError.HttpClientError>;
36+
downloadStatus?: number;
3537
baseDir?: string;
3638
local?: { version: string; appServerFails?: boolean; versionFails?: boolean };
3739
platform?: NodeJS.Platform;
@@ -106,7 +108,10 @@ const makeHarness = Effect.fn("test.makeCodexInstallation")(function* (
106108
Effect.sync(() => {
107109
downloads++;
108110
return Object.defineProperty(
109-
HttpClientResponse.fromWeb(request, new Response(null)),
111+
HttpClientResponse.fromWeb(
112+
request,
113+
new Response(null, { status: input.downloadStatus ?? 200 }),
114+
),
110115
"stream",
111116
{
112117
value: input.body ?? Stream.succeed(archive),
@@ -366,6 +371,81 @@ it.effect("rejects corrupted downloads without publishing a runtime", () =>
366371
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
367372
);
368373

374+
it.effect.each(["interrupted body", "HTTP 404"] as const)(
375+
"reports a download failure for %s without blaming disk access",
376+
(failure) =>
377+
Effect.gen(function* () {
378+
const request = HttpClientRequest.get(asset.url);
379+
const response = HttpClientResponse.fromWeb(request, new Response(null));
380+
const h = yield* makeHarness(
381+
failure === "HTTP 404"
382+
? { downloadStatus: 404 }
383+
: {
384+
body: Stream.concat(
385+
Stream.succeed(archive.subarray(0, 10)),
386+
Stream.fail(
387+
new HttpClientError.HttpClientError({
388+
reason: new HttpClientError.DecodeError({
389+
request,
390+
response,
391+
cause: new TypeError("terminated"),
392+
}),
393+
}),
394+
),
395+
),
396+
},
397+
);
398+
yield* h.installation.start;
399+
expect(yield* terminalState(h.installation)).toMatchObject({
400+
phase: "failed",
401+
message:
402+
failure === "HTTP 404"
403+
? "The Codex download server returned HTTP 404. Try again later."
404+
: "The Codex download was interrupted. Check your internet connection, then retry installation.",
405+
});
406+
expect(h.downloads()).toBe(1);
407+
expect(yield* h.fs.readDirectory(h.installation.managedDirectory)).toEqual([]);
408+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
409+
);
410+
411+
it.effect(
412+
"preserves the filesystem failure message when installation cannot create its directory",
413+
() =>
414+
Effect.gen(function* () {
415+
const fs = yield* FileSystem.FileSystem;
416+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-install-test-" });
417+
const baseDir = `${directory}/not-a-directory`;
418+
yield* fs.writeFileString(baseDir, "existing file");
419+
const h = yield* makeHarness({ baseDir });
420+
yield* h.installation.start;
421+
expect(yield* terminalState(h.installation)).toMatchObject({
422+
phase: "failed",
423+
message: "Could not install Codex. Check disk space and directory access, then try again.",
424+
});
425+
expect(h.downloads()).toBe(0);
426+
expect(yield* fs.readFileString(baseDir)).toBe("existing file");
427+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
428+
);
429+
430+
it.effect("reports a download timeout and cleans the staging directory", () =>
431+
Effect.gen(function* () {
432+
const started = yield* Deferred.make<void>();
433+
const stall = Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never));
434+
const h = yield* makeHarness({
435+
body: Stream.concat(Stream.succeed(archive.subarray(0, 10)), Stream.fromEffect(stall)),
436+
});
437+
yield* h.installation.start;
438+
yield* Deferred.await(started);
439+
yield* TestClock.adjust("45 minutes");
440+
expect(yield* terminalState(h.installation)).toMatchObject({
441+
phase: "failed",
442+
message: "The Codex download timed out. Retry installation.",
443+
});
444+
expect(h.downloads()).toBe(1);
445+
expect(yield* h.fs.readDirectory(h.installation.managedDirectory)).toEqual([]);
446+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
447+
);
448+
369449
it.effect("cancels an in-flight download and cleans the staging directory", () =>
370450
Effect.gen(function* () {
371451
const downloading = yield* Deferred.make<void>();

‎apps/server/src/provider/CodexInstallation.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,14 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function
546546
),
547547
Stream.run(fs.sink(archivePath, { flag: "wx", mode: 0o600 })),
548548
Effect.timeout("45 minutes"),
549+
Effect.catchTags({
550+
TimeoutError: (cause) =>
551+
installationError(
552+
"download",
553+
"The Codex download timed out. Retry installation.",
554+
cause,
555+
),
556+
}),
549557
);
550558
if (downloadedBytes !== release.archiveBytes || hash.digest("hex") !== release.sha256)
551559
return yield* installationError(
@@ -612,6 +620,20 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function
612620
yield* activate();
613621
},
614622
Effect.scoped,
623+
Effect.catchTags({
624+
HttpClientError: (cause) => {
625+
const httpStatus =
626+
cause.reason._tag === "StatusCodeError" ? cause.reason.response.status : undefined;
627+
return new CodexInstallationError({
628+
operation: "download",
629+
detail:
630+
httpStatus === undefined
631+
? "The Codex download was interrupted. Check your internet connection, then retry installation."
632+
: `The Codex download server returned HTTP ${httpStatus}. Try again later.`,
633+
cause,
634+
});
635+
},
636+
}),
615637
Effect.mapError(
616638
wrapFailure(
617639
"install",

0 commit comments

Comments
 (0)