Skip to content

Commit 58906da

Browse files
fix(server): pass Codex image attachments by path to avoid oversized requests
1 parent d29c56a commit 58906da

6 files changed

Lines changed: 154 additions & 29 deletions

File tree

‎apps/server/src/provider/Layers/CodexAdapter.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,54 @@ sessionErrorLayer("CodexAdapterLive session errors", (it) => {
448448
}),
449449
);
450450

451+
it.effect("passes image attachments to Codex by path instead of base64", () =>
452+
Effect.gen(function* () {
453+
const adapter = yield* CodexAdapter;
454+
const threadId = asThreadId("thread-image-attachment");
455+
yield* adapter.startSession({
456+
provider: ProviderDriverKind.make("codex"),
457+
threadId,
458+
runtimeMode: "full-access",
459+
});
460+
const runtime = sessionRuntimeFactory.lastRuntime;
461+
NodeAssert.ok(runtime);
462+
runtime.sendTurnImpl.mockClear();
463+
464+
const serverConfig = yield* ServerConfig;
465+
const attachmentsDir = serverConfig.attachmentsDir;
466+
const attachmentId = "attachment-local-image-1";
467+
const attachmentPath = NodePath.join(attachmentsDir, `${attachmentId}.png`);
468+
NodeFS.writeFileSync(attachmentPath, Buffer.alloc(4, 0x89));
469+
470+
try {
471+
yield* adapter.sendTurn({
472+
threadId,
473+
input: "Use this image.",
474+
attachments: [
475+
{
476+
type: "image",
477+
id: attachmentId,
478+
name: "generated.png",
479+
mimeType: "image/png",
480+
sizeBytes: NodeFS.statSync(attachmentPath).size,
481+
},
482+
],
483+
});
484+
485+
const input = runtime.sendTurnImpl.mock.calls[0]?.[0];
486+
NodeAssert.ok(input);
487+
NodeAssert.deepStrictEqual(input.attachments, [
488+
{
489+
type: "localImage",
490+
path: attachmentPath,
491+
},
492+
]);
493+
} finally {
494+
NodeFS.rmSync(attachmentPath, { force: true });
495+
}
496+
}),
497+
);
498+
451499
it.effect("passes configured launch args into the session runtime", () => {
452500
const runtimeFactory = makeRuntimeFactory();
453501
const layer = Layer.effect(

‎apps/server/src/provider/Layers/CodexAdapter.ts‎

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import * as NodeCrypto from "node:crypto";
3636
import * as Crypto from "effect/Crypto";
3737
import * as Exit from "effect/Exit";
3838
import * as Fiber from "effect/Fiber";
39-
import * as FileSystem from "effect/FileSystem";
4039
import * as Queue from "effect/Queue";
4140
import * as Schema from "effect/Schema";
4241
import * as Scope from "effect/Scope";
@@ -67,6 +66,7 @@ import {
6766
makeCodexSessionRuntime,
6867
type CodexSessionRuntimeError,
6968
type CodexSessionRuntimeOptions,
69+
type CodexSessionRuntimeSendTurnInput,
7070
type CodexSessionRuntimeShape,
7171
} from "./CodexSessionRuntime.ts";
7272
import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
@@ -2218,7 +2218,6 @@ export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
22182218
options?: CodexAdapterLiveOptions,
22192219
) {
22202220
const boundInstanceId = options?.instanceId ?? ProviderInstanceId.make("codex");
2221-
const fileSystem = yield* FileSystem.FileSystem;
22222221
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
22232222
const crypto = yield* Crypto.Crypto;
22242223
const serverConfig = yield* Effect.service(ServerConfig);
@@ -2488,27 +2487,18 @@ export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
24882487
detail: `Invalid attachment id '${attachment.id}'.`,
24892488
});
24902489
}
2491-
const bytes = yield* fileSystem.readFile(attachmentPath).pipe(
2492-
Effect.mapError(
2493-
(cause) =>
2494-
new ProviderAdapterRequestError({
2495-
provider: PROVIDER,
2496-
method: "turn/start",
2497-
detail: `Failed to read attachment file: ${cause.message}.`,
2498-
cause,
2499-
}),
2500-
),
2501-
);
25022490
return {
2503-
type: "image" as const,
2504-
url: `data:${attachment.mimeType};base64,${Buffer.from(bytes).toString("base64")}`,
2491+
type: "localImage" as const,
2492+
path: attachmentPath,
25052493
};
25062494
});
25072495

25082496
const sendTurn: CodexAdapterShape["sendTurn"] = Effect.fn("sendTurn")(function* (input) {
2509-
// Codex ingests images only. Anything else would be base64-encoded as an
2510-
// image and rejected or misread; generic files reach the agent through the
2511-
// path line ProviderService puts in the prompt.
2497+
// Codex ingests images only. Anything else would be inlined as an image
2498+
// and rejected or misread; generic files reach the agent through the path
2499+
// line ProviderService puts in the prompt. Images are passed by path
2500+
// instead of base64 so the turn/start request does not scale with file
2501+
// size; the CLI reads the file itself.
25122502
const codexAttachments = yield* Effect.forEach(
25132503
(input.attachments ?? []).filter((attachment) => attachment.type === "image"),
25142504
(attachment) => resolveAttachment(input, attachment),

‎apps/server/src/provider/Layers/CodexCollabRuntime.integration.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,51 @@ describe("CodexSessionRuntime collab integration", () => {
654654
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
655655
);
656656

657+
it.live("compacts and retries a turn/start that fails with HTTP 413", () =>
658+
Effect.gen(function* () {
659+
const script = {
660+
rootThreadId: ROOT,
661+
recordRequests: true,
662+
recordTurnStartRequests: true,
663+
turnStart413BeforeSuccess: true,
664+
notifications: [],
665+
};
666+
// @effect-diagnostics-next-line preferSchemaOverJson:off
667+
NodeFS.writeFileSync(scriptPath, JSON.stringify(script), "utf8");
668+
const requestsPath = `${scriptPath}.requests`;
669+
NodeFS.rmSync(requestsPath, { force: true });
670+
yield* Effect.addFinalizer(() =>
671+
Effect.sync(() => {
672+
NodeFS.rmSync(scriptPath, { force: true });
673+
NodeFS.rmSync(requestsPath, { force: true });
674+
}),
675+
);
676+
677+
const runtime = yield* makeCodexSessionRuntime({
678+
threadId: ThreadId.make("thread-codex-413-retry"),
679+
binaryPath: peerPath,
680+
cwd: NodeOS.tmpdir(),
681+
runtimeMode: "full-access",
682+
environment: { ...process.env, T3_CODEX_COLLAB_SCRIPT: scriptPath },
683+
});
684+
685+
yield* runtime.start();
686+
const result = yield* runtime.sendTurn({ input: "continue" });
687+
assert.ok(result.turnId);
688+
689+
const requests = NodeFS.readFileSync(requestsPath, "utf8")
690+
.trim()
691+
.split("\n")
692+
.map((line) => JSON.parse(line) as { method: string });
693+
assert.deepEqual(
694+
requests.map((request) => request.method),
695+
["turn/start", "thread/compact/start", "turn/start"],
696+
);
697+
698+
yield* runtime.close;
699+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
700+
);
701+
657702
const elicitationCases = [
658703
{
659704
decision: "accept",

‎apps/server/src/provider/Layers/CodexSessionRuntime.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ describe("buildTurnStartParams", () => {
7171
runtimeMode: "full-access",
7272
attachments: [
7373
{
74-
type: "image",
75-
url: { secret } as unknown as string,
74+
type: "localImage",
75+
path: { secret } as unknown as string,
7676
},
7777
],
7878
}).pipe(Effect.flip),
@@ -140,8 +140,8 @@ describe("buildTurnStartParams", () => {
140140
interactionMode: "default",
141141
attachments: [
142142
{
143-
type: "image",
144-
url: "data:image/png;base64,abc",
143+
type: "localImage",
144+
path: "/tmp/generated.png",
145145
},
146146
],
147147
}),
@@ -160,8 +160,8 @@ describe("buildTurnStartParams", () => {
160160
text: "Implement it",
161161
},
162162
{
163-
type: "image",
164-
url: "data:image/png;base64,abc",
163+
type: "localImage",
164+
path: "/tmp/generated.png",
165165
},
166166
],
167167
model: "gpt-5.3-codex",

‎apps/server/src/provider/Layers/CodexSessionRuntime.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ export interface CodexSessionRuntimeOptions {
178178
export interface CodexSessionRuntimeSendTurnInput {
179179
readonly input?: string;
180180
readonly attachments?: ReadonlyArray<{
181-
readonly type: "image";
182-
readonly url: string;
181+
readonly type: "localImage";
182+
readonly path: string;
183183
}>;
184184
readonly model?: string;
185185
readonly serviceTier?: CodexServiceTier | undefined;
@@ -602,8 +602,8 @@ export function buildTurnStartParams(input: {
602602
readonly runtimeMode: RuntimeMode;
603603
readonly prompt?: string;
604604
readonly attachments?: ReadonlyArray<{
605-
readonly type: "image";
606-
readonly url: string;
605+
readonly type: "localImage";
606+
readonly path: string;
607607
}>;
608608
readonly model?: string;
609609
readonly serviceTier?: CodexServiceTier;
@@ -2363,7 +2363,21 @@ export const makeCodexSessionRuntime = (
23632363
hasConfiguredMcpServer(options.appServerArgs) &&
23642364
(options.browserToolsAvailable ?? true),
23652365
});
2366-
const rawResponse = yield* client.raw.request("turn/start", params);
2366+
const rawResponse = yield* client.raw.request("turn/start", params).pipe(
2367+
// The provider rejects oversized serialized turn payloads with HTTP
2368+
// 413 before any model call happens. Compaction shrinks the rollout
2369+
// history on the CLI side, so a single retry after it completes can
2370+
// recover what would otherwise be a dead turn.
2371+
Effect.catchIf(
2372+
(cause) =>
2373+
cause._tag === "CodexAppServerRequestError" && cause.errorMessage.includes("413"),
2374+
() =>
2375+
Effect.gen(function* () {
2376+
yield* client.request("thread/compact/start", { threadId: providerThreadId });
2377+
return yield* client.raw.request("turn/start", params);
2378+
}),
2379+
),
2380+
);
23672381
const response = yield* decodeV2TurnStartResponse(rawResponse).pipe(
23682382
Effect.mapError((error) =>
23692383
CodexErrors.CodexAppServerProtocolParseError.fromSchemaError(

‎apps/server/src/provider/testFixtures/codexCollabMockPeer.mjs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ rl.on("line", (line) => {
6161
write({ id, result: { account: { type: "apiKey" }, requiresOpenaiAuth: false } });
6262
return;
6363
}
64+
if (method === "thread/compact/start") {
65+
if (script.recordRequests) {
66+
NodeFS.appendFileSync(
67+
`${process.env.T3_CODEX_COLLAB_SCRIPT}.requests`,
68+
`${JSON.stringify({ method, params: message.params })}\n`,
69+
);
70+
}
71+
write({ id, result: {} });
72+
return;
73+
}
6474
if (method === "skills/list" || method === "model/list") {
6575
write({ id, result: { data: [] } });
6676
return;
@@ -118,6 +128,24 @@ rl.on("line", (line) => {
118128
return;
119129
}
120130
if (method === "turn/start") {
131+
if (script.recordTurnStartRequests) {
132+
NodeFS.appendFileSync(
133+
`${process.env.T3_CODEX_COLLAB_SCRIPT}.requests`,
134+
`${JSON.stringify({ method, params: message.params })}\n`,
135+
);
136+
}
137+
if (script.turnStart413BeforeSuccess) {
138+
script.turnStart413BeforeSuccess = false;
139+
write({
140+
id,
141+
error: {
142+
code: -32000,
143+
message:
144+
"unexpected status 413 Payload Too Large from http://127.0.0.1:8317/v1/responses",
145+
},
146+
});
147+
return;
148+
}
121149
const turnId = script.turnIds?.[turnStartCount];
122150
const turn = turnId
123151
? { ...fixture.responses.turnStart.turn, id: turnId }

0 commit comments

Comments
 (0)