fix(codex): avoid quadratic app-server input buffering (#8605)

This commit is contained in:
Julius Marminge authored and GitHub committed 2026-08-28 18:17:06 -07:00
1 parent 2bc9e8ef6a
commit 702a6ade3c
2 files changed
+133 -8

No files matched your search

@@ -290,6 +290,114 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
}),
);
it.effect("routes a large notification fragmented across thousands of input chunks", () =>
Effect.gen(function* () {
const { stdio, input, output } = yield* makeInMemoryStdio();
const notifications: Array<CodexProtocol.CodexAppServerIncomingNotification> = [];
const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
stdio,
onNotification: (notification) =>
Effect.sync(() => {
notifications.push(notification);
}),
});
const response = yield* transport.request("thread/read", {}).pipe(Effect.forkScoped);
yield* Queue.take(output);
const notification = {
method: "turn/diff/updated",
params: {
threadId: "thread-1",
turnId: "turn-1",
diff: "x".repeat(4 * 1024 * 1024),
},
};
const bytes = encoder.encode(
`${encodeUnknownJsonString(notification)}\n${encodeUnknownJsonString({ id: 1, result: { ok: true } })}\n`,
);
for (let offset = 0; offset < bytes.length; offset += 1024) {
yield* Queue.offer(input, bytes.subarray(offset, offset + 1024));
}
assert.deepEqual(yield* Fiber.join(response), { ok: true });
assert.deepEqual(notifications, [notification]);
}),
);
it.effect.each([1, 7, 1024])(
"preserves JSONL framing and UTF-8 across %i-byte input chunks",
(chunkSize) =>
Effect.gen(function* () {
const { stdio, input } = yield* makeInMemoryStdio();
const notifications: Array<CodexProtocol.CodexAppServerIncomingNotification> = [];
const rawLines: Array<unknown> = [];
const termination = yield* Deferred.make<CodexError.CodexAppServerError>();
yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
stdio,
logIncoming: true,
logger: (event) =>
Effect.sync(() => {
if (event.stage === "raw") {
rawLines.push(event.payload);
}
}),
onNotification: (notification) =>
Effect.sync(() => {
notifications.push(notification);
}),
onTermination: (error) => Deferred.succeed(termination, error).pipe(Effect.asVoid),
});
const firstLine = '{"method":"x/first",\r"params":{"text":"hé🙂"}}';
const secondLine = '{"method":"x/second","params":{"value":2}}';
const finalLine = '{"method":"x/final","params":{"text":"最後"}}\r';
const bytes = encoder.encode(`\n \t\r\n${firstLine}\r\n\n${secondLine}\n${finalLine}`);
for (let offset = 0; offset < bytes.length; offset += chunkSize) {
yield* Queue.offer(input, bytes.subarray(offset, offset + chunkSize));
}
yield* Queue.end(input);
assert.instanceOf(
yield* Deferred.await(termination),
CodexError.CodexAppServerInputStreamEndedError,
);
assert.deepEqual(notifications, [
{ method: "x/first", params: { text: "hé🙂" } },
{ method: "x/second", params: { value: 2 } },
{ method: "x/final", params: { text: "最後" } },
]);
assert.deepEqual(rawLines, [firstLine, secondLine, finalLine]);
}),
);
it.effect("reports a malformed fragmented final line before input stream termination", () =>
Effect.gen(function* () {
const { stdio, input, output } = yield* makeInMemoryStdio();
const termination = yield* Deferred.make<CodexError.CodexAppServerError>();
const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
stdio,
onTermination: (error) => Deferred.succeed(termination, error).pipe(Effect.asVoid),
});
const response = yield* transport.request("thread/read", {}).pipe(Effect.forkScoped);
yield* Queue.take(output);
yield* Queue.offer(input, encoder.encode('{"id":1,'));
yield* Queue.offer(input, encoder.encode('"result":'));
yield* Queue.end(input);
const error = yield* Deferred.await(termination);
assert.instanceOf(error, CodexError.CodexAppServerProtocolParseError);
assert.equal(error.operation, "decode-wire-message");
const responseError = yield* Fiber.join(response).pipe(
Effect.match({
onFailure: (failure) => failure,
onSuccess: () => assert.fail("Expected the malformed response to fail the request"),
}),
);
assert.strictEqual(responseError, error);
}),
);
it.effect("surfaces JSON encoding failures as protocol parse errors", () =>
Effect.gen(function* () {
const { stdio } = yield* makeInMemoryStdio();
@@ -157,7 +157,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
const incomingRequests = yield* Queue.unbounded<CodexAppServerIncomingRequest>();
const pending = yield* Ref.make(new Map<string, CodexAppServerPendingRequest>());
const nextRequestId = yield* Ref.make(1);
const remainder = yield* Ref.make("");
const remainder: Array<string> = [];
const terminationHandled = yield* Ref.make(false);
const logProtocol = (event: CodexAppServerProtocolLogEvent) => {
@@ -354,11 +354,24 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
yield* options.stdio.stdin.pipe(
Stream.decodeText(),
Stream.runForEach((chunk) =>
Ref.modify(remainder, (current) => {
const combined = current + chunk;
const lines = combined.split("\n");
const nextRemainder = lines.pop() ?? "";
return [lines.map((line) => line.replace(/\r$/, "")), nextRemainder] as const;
Effect.sync(() => {
const lines: Array<string> = [];
let start = 0;
for (
let newline = chunk.indexOf("\n");
newline !== -1;
newline = chunk.indexOf("\n", start)
) {
remainder.push(chunk.slice(start, newline));
lines.push(remainder.join("").replace(/\r$/, ""));
remainder.length = 0;
start = newline + 1;
}
// Keep unfinished lines in fragments so each chunk is scanned only once.
if (start < chunk.length) {
remainder.push(chunk.slice(start));
}
return lines;
}).pipe(Effect.flatMap((lines) => Effect.forEach(lines, handleLine, { discard: true }))),
),
Effect.matchEffect({
@@ -367,8 +380,12 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
Effect.succeed(normalizeIncomingError(error, "read-input-stream")),
),
onSuccess: () =>
Ref.get(remainder).pipe(
Effect.flatMap((line) => (line.trim().length === 0 ? Effect.void : handleLine(line))),
Effect.sync(() => {
const line = remainder.join("");
remainder.length = 0;
return line;
}).pipe(
Effect.flatMap(handleLine),
Effect.matchEffect({
onFailure: (error) => handleTermination(() => Effect.succeed(error)),
onSuccess: () =>