@devries-cameron20 / t3code-1 / commits / e859e318a0
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(codex): end the session when an incoming message is too long (#19733)

Co-authored-by: Cestercian <183791452+cestercian@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Victor Vogel committed Oct 19, 2026 at 23:10 UTC e859e318a0ed88f0493958c9226e4501c9aaa7e3
2 files changed +100 -16
packages/effect-codex-app-server/src/protocol.test.ts
+74
@@ -371,6 +371,80 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
371 }),
372 );
373
374 + it.effect.each(["newline-terminated", "unfinished"])(
375 + "rejects an oversized %s message using UTF-8 bytes before logging or parsing",
376 + (framing) =>
377 + Effect.gen(function* () {
378 + const { stdio, input, output } = yield* makeInMemoryStdio();
379 + const notifications: Array<CodexProtocol.CodexAppServerIncomingNotification> = [];
380 + const logged: Array<unknown> = [];
381 + const termination = yield* Deferred.make<CodexError.CodexAppServerError>();
382 + let reachedEnd = false;
383 + const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
384 + stdio: Stdio.make({
385 + ...stdio,
386 + stdin: stdio.stdin.pipe(
387 + Stream.concat(
388 + Stream.suspend(() => {
389 + reachedEnd = true;
390 + return Stream.empty;
391 + }),
392 + ),
393 + ),
394 + }),
395 + maxIncomingMessageBytes: 16,
396 + logIncoming: true,
397 + logger: (event) =>
398 + Effect.sync(() => {
399 + logged.push(event.payload);
400 + }),
401 + onNotification: (notification) =>
402 + Effect.sync(() => {
403 + notifications.push(notification);
404 + }),
405 + onTermination: (error) => Deferred.succeed(termination, error).pipe(Effect.asVoid),
406 + });
407 + const pending = yield* Effect.forEach(["thread/read", "thread/list"], (method) =>
408 + transport.request(method, {}).pipe(Effect.forkScoped),
409 + );
410 + yield* Effect.forEach(pending, () => Queue.take(output));
411 +
412 + // Two 16-byte lines must survive; the 17-byte line is only 15 UTF-16 code units.
413 + const firstLine = '{"method":"xé"}';
414 + const secondLine = '{"method":"x/a"}';
415 + const bytes = encoder.encode(
416 + `${firstLine}\n${secondLine}\n{"method":"éé"}${framing === "unfinished" ? "" : "\n"}`,
417 + );
418 + // Split a UTF-8 sequence on both sides of the byte limit.
419 + const firstSplit = bytes.indexOf(0xc3) + 1;
420 + yield* Queue.offer(input, bytes.subarray(0, firstSplit));
421 + if (framing === "unfinished") {
422 + const lastSplit = bytes.lastIndexOf(0xc3) + 1;
423 + yield* Queue.offer(input, bytes.subarray(firstSplit, lastSplit));
424 + yield* Queue.offer(input, bytes.subarray(lastSplit));
425 + } else {
426 + yield* Queue.offer(input, bytes.subarray(firstSplit));
427 + }
428 + yield* Queue.end(input);
429 +
430 + const error = yield* Deferred.await(termination);
431 + assert.instanceOf(error, CodexError.CodexAppServerTransportError);
432 + assert.equal(error.operation, "read-input-stream");
433 + assert.isFalse(reachedEnd, "Must reject the unfinished line before reading EOF");
434 + for (const response of pending) {
435 + const failure = yield* Fiber.join(response).pipe(
436 + Effect.match({
437 + onFailure: (error) => error,
438 + onSuccess: () => assert.fail("Expected the oversized message to fail the request"),
439 + }),
440 + );
441 + assert.strictEqual(failure, error);
442 + }
443 + assert.deepEqual(notifications, [{ method: "xé" }, { method: "x/a" }]);
444 + assert.deepEqual(logged, [firstLine, { method: "xé" }, secondLine, { method: "x/a" }]);
445 + }),
446 + );
447 +
448 it.effect("reports a malformed fragmented final line before input stream termination", () =>
449 Effect.gen(function* () {
450 const { stdio, input, output } = yield* makeInMemoryStdio();
packages/effect-codex-app-server/src/protocol.ts
+26 -16
@@ -16,6 +16,8 @@ const isJsonRpcId = Schema.is(JsonRpcId);
16 const isJsonRpcResponseEnvelope = Schema.is(JsonRpcResponseEnvelope);
17 const isCodexAppServerError = Schema.is(CodexError.CodexAppServerError);
18 const MAX_BUFFERED_RAW_MESSAGES = 32;
19 +// Bound JSONL lines before joining and parsing; the LF delimiter is excluded.
20 +const MAX_INCOMING_MESSAGE_BYTES = 128 * 1024 * 1024;
21
22 export interface CodexAppServerProtocolLogEvent {
23 readonly direction: "incoming" | "outgoing";
@@ -39,6 +41,8 @@ export interface CodexAppServerPatchedProtocolOptions {
41 readonly terminationError?: Effect.Effect<CodexError.CodexAppServerError>;
42 readonly logIncoming?: boolean;
43 readonly logOutgoing?: boolean;
44 + // Test override; production uses MAX_INCOMING_MESSAGE_BYTES.
45 + readonly maxIncomingMessageBytes?: number;
46 readonly logger?: (event: CodexAppServerProtocolLogEvent) => Effect.Effect<void, never>;
47 readonly onNotification?: (
48 notification: CodexAppServerIncomingNotification,
@@ -164,7 +168,9 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
168 yield* Queue.sliding<CodexAppServerIncomingRequest>(MAX_BUFFERED_RAW_MESSAGES);
169 const pending = yield* Ref.make(new Map<string, CodexAppServerPendingRequest>());
170 const nextRequestId = yield* Ref.make(1);
171 + const maxIncomingMessageBytes = options.maxIncomingMessageBytes ?? MAX_INCOMING_MESSAGE_BYTES;
172 const remainder: Array<string> = [];
173 + let remainderBytes = 0;
174 const terminationHandled = yield* Ref.make(false);
175 const terminationFailure = yield* Ref.make(Option.none<CodexError.CodexAppServerError>());
176 const terminationSignal = yield* Deferred.make<void>();
@@ -399,26 +405,30 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
405 yield* options.stdio.stdin.pipe(
406 Stream.interruptWhen(Deferred.await(terminationSignal)),
407 Stream.decodeText(),
402 - Stream.runForEach((chunk) =>
403 - Effect.sync(() => {
404 - const lines: Array<string> = [];
408 + Stream.runForEach(
409 + Effect.fnUntraced(function* (chunk) {
410 let start = 0;
406 - for (
407 - let newline = chunk.indexOf("\n");
408 - newline !== -1;
409 - newline = chunk.indexOf("\n", start)
410 - ) {
411 - remainder.push(chunk.slice(start, newline));
412 - lines.push(remainder.join("").replace(/\r$/, ""));
411 + while (start < chunk.length) {
412 + const newline = chunk.indexOf("\n", start);
413 + const fragment = chunk.slice(start, newline === -1 ? chunk.length : newline);
414 + // decodeText holds back split UTF-8 sequences, so fragment byte sums are exact.
415 + remainderBytes += Buffer.byteLength(fragment, "utf8");
416 + if (remainderBytes > maxIncomingMessageBytes) {
417 + remainder.length = 0;
418 + return yield* new CodexError.CodexAppServerTransportError({
419 + operation: "read-input-stream",
420 + cause: new Error(`Incoming message exceeded ${maxIncomingMessageBytes} bytes.`),
421 + });
422 + }
423 + remainder.push(fragment);
424 + if (newline === -1) break;
425 + const line = remainder.join("").replace(/\r$/, "");
426 remainder.length = 0;
427 + remainderBytes = 0;
428 + yield* handleLine(line);
429 start = newline + 1;
430 }
416 - // Keep unfinished lines in fragments so each chunk is scanned only once.
417 - if (start < chunk.length) {
418 - remainder.push(chunk.slice(start));
419 - }
420 - return lines;
421 - }).pipe(Effect.flatMap((lines) => Effect.forEach(lines, handleLine, { discard: true }))),
431 + }),
432 ),
433 Effect.matchEffect({
434 onFailure: (error) =>