Skip to content

Commit 566a471

Browse files
committed
test(codex): pin the transport boundary at the adjacent byte
The sizing helper already had unit tests, but nothing proved the real serialized frame routes correctly one byte on each side of the limit. That gap matters because the request body is not the frame: `stream` is deleted and `type` is added before sending, so padding sized against the body sits eleven bytes away from what is actually transmitted. An off-by-one would live exactly there and pass every existing test. These two build padding so the serialized frame is exactly limit-1 and exactly limit, then assert the whole path: one socket and one send of the expected byte length under, zero sockets and one SSE call at it. Flipping the gate from >= to > fails the second one, so it catches a real off-by-one at the transport level rather than only in the helper. Two comment corrections while here. The close-code comment claimed the named 1009 message makes the failure diagnosable from the logs; it does not. The eager relay turns any stream error into a generic `upstream_reset` synthetic terminal without feeding it back through the inspector, so `/api/logs` retains only `streamAborted`. The message reaches the client and stops there, and the comment now says so rather than promising observability the code does not deliver. The margin comment described 64 KiB as absorbing a future append. There is no append. It is a conservative cushion, and the useful thing to record is what it actually covers: RFC 6455 framing is 14 bytes at this payload size — an 8-byte extended length plus a 4-byte client mask — so even a backend counting frame headers has ~65.5 KiB of room. Tests: 59 pass, 1 skip across ws-upstream, sse-failed-tail, and upstream-http-version. tsc --noEmit clean.
1 parent f093bf0 commit 566a471

2 files changed

Lines changed: 78 additions & 5 deletions

File tree

‎src/server/responses/ws-upstream.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ export const MIN_BOUNDED_CODEX_WS_BUN_VERSION = "1.4.0";
3737
// screenshots, and then never recovers, because each retry resends the frame.
3838
export const MAX_CODEX_WS_CREATE_FRAME_BYTES = 16 * 1024 * 1024;
3939
// Bun frames the payload it is handed, so the send-side budget is the JSON text
40-
// itself. The margin only absorbs the difference between the measurement here
41-
// and what a future caller might append after it.
40+
// itself, and nothing is appended between the check and the send. The margin is
41+
// a conservative cushion, not a computed requirement: it covers RFC 6455 frame
42+
// overhead in case the backend counts it (14 bytes at this payload size — an
43+
// 8-byte extended length plus a 4-byte client mask, leaving ~65.5 KiB spare),
44+
// and it leaves room for a future caller that appends to the frame.
4245
const CODEX_WS_CREATE_FRAME_MARGIN_BYTES = 64 * 1024;
4346
export const CODEX_WS_CREATE_FRAME_LIMIT_BYTES =
4447
MAX_CODEX_WS_CREATE_FRAME_BYTES - CODEX_WS_CREATE_FRAME_MARGIN_BYTES;
@@ -122,9 +125,16 @@ const CLOSED_BEFORE_TERMINAL = "codex websocket closed before a Responses termin
122125

123126
/**
124127
* The close code is the only thing that separates "the backend refused this
125-
* payload" from "the network dropped", and both used to reach the logs as the
126-
* same bare 502. Naming the oversized case here is what makes it diagnosable
127-
* without reading the client's own rollout files.
128+
* payload" from "the network dropped", and both used to reach the caller as the
129+
* same bare 502. Naming the oversized case here puts that distinction in the
130+
* message the client receives.
131+
*
132+
* It does NOT reach the request log as a typed code. The eager relay turns any
133+
* stream error into a generic `upstream_reset` synthetic terminal
134+
* (`relay.ts`, `relay-eager.ts`) without feeding that frame back through the
135+
* inspector, so `/api/logs` keeps neither this message nor a specific code —
136+
* only `streamAborted`. Machine-readable typing would mean changing the error
137+
* taxonomy, which is deliberately out of scope for this transport fix.
128138
*/
129139
function closedBeforeTerminalMessage(event: unknown): string {
130140
const detail = event as { code?: unknown; reason?: unknown } | null | undefined;

‎tests/ws-upstream.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,69 @@ describe("oversized Codex create frames", () => {
748748
expect(FakeWebSocket.instances).toHaveLength(1);
749749
});
750750

751+
// The unit tests above measure the helper; these two measure the REAL serialized
752+
// frame, one byte on each side of the limit. That distinction matters because the
753+
// request body is not the frame: `stream` is deleted and `type` is added before
754+
// sending, so padding sized against the body would sit at a different offset than
755+
// the bytes actually transmitted. An off-by-one lives exactly here and nowhere else.
756+
describe("the adjacent-byte transport boundary", () => {
757+
// Build padding such that the serialized frame is EXACTLY `target` bytes.
758+
function initForFrameBytes(target: number): RequestInit {
759+
const probe = frameTextFor(0);
760+
const padding = "x".repeat(target - Buffer.byteLength(probe, "utf8"));
761+
const init = streamingInit({ padding });
762+
const actual = Buffer.byteLength(frameTextFor(padding.length), "utf8");
763+
if (actual !== target) throw new Error(`frame sizing is wrong: wanted ${target}, built ${actual}`);
764+
return init;
765+
}
766+
767+
function frameTextFor(paddingLength: number): string {
768+
const body = JSON.parse(streamingInit({ padding: "x".repeat(paddingLength) }).body as string) as Record<string, unknown>;
769+
delete body.stream;
770+
return JSON.stringify({ ...body, type: "response.create" });
771+
}
772+
773+
test("a frame one byte under the limit goes over WS", async () => {
774+
installFake(ws => {
775+
ws.emit("open", {});
776+
ws.emit("message", { data: JSON.stringify({ type: "response.completed", response: {} }) });
777+
});
778+
const fallback = (() => {
779+
throw new Error("fallback must not run one byte under the limit");
780+
}) as unknown as typeof fetch;
781+
782+
const response = await codexWsUpstreamFetch(
783+
CODEX_URL,
784+
initForFrameBytes(CODEX_WS_CREATE_FRAME_LIMIT_BYTES - 1),
785+
fallback,
786+
);
787+
expect(isCodexWsUpstreamResponse(response)).toBe(true);
788+
expect(FakeWebSocket.instances).toHaveLength(1);
789+
expect(FakeWebSocket.instances[0]!.sent).toHaveLength(1);
790+
// Byte length, not code units: the limit is a byte budget, and this
791+
// assertion should keep meaning the same thing if the fixture ever
792+
// carries non-ASCII text.
793+
expect(Buffer.byteLength(FakeWebSocket.instances[0]!.sent[0]!, "utf8"))
794+
.toBe(CODEX_WS_CREATE_FRAME_LIMIT_BYTES - 1);
795+
});
796+
797+
test("the very next byte takes SSE without dialing", async () => {
798+
installFake(() => { throw new Error("WS must not be dialed at the limit"); });
799+
const sentinel = new Response("sse");
800+
let fallbackCalls = 0;
801+
const fallback = (async () => { fallbackCalls += 1; return sentinel; }) as unknown as typeof fetch;
802+
803+
const response = await codexWsUpstreamFetch(
804+
CODEX_URL,
805+
initForFrameBytes(CODEX_WS_CREATE_FRAME_LIMIT_BYTES),
806+
fallback,
807+
);
808+
expect(response).toBe(sentinel);
809+
expect(fallbackCalls).toBe(1);
810+
expect(FakeWebSocket.instances).toHaveLength(0);
811+
});
812+
});
813+
751814
test("names the oversized close instead of reporting a bare drop", async () => {
752815
installFake(ws => {
753816
ws.emit("open", {});

0 commit comments

Comments
 (0)