@@ -16,6 +16,8 @@ const isJsonRpcId = Schema.is(JsonRpcId);
1616const isJsonRpcResponseEnvelope = Schema . is ( JsonRpcResponseEnvelope ) ;
1717const isCodexAppServerError = Schema . is ( CodexError . CodexAppServerError ) ;
1818const 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 ;
1921
2022export interface CodexAppServerProtocolLogEvent {
2123 readonly direction : "incoming" | "outgoing" ;
@@ -39,6 +41,8 @@ export interface CodexAppServerPatchedProtocolOptions {
3941 readonly terminationError ?: Effect . Effect < CodexError . CodexAppServerError > ;
4042 readonly logIncoming ?: boolean ;
4143 readonly logOutgoing ?: boolean ;
44+ // Test override; production uses MAX_INCOMING_MESSAGE_BYTES.
45+ readonly maxIncomingMessageBytes ?: number ;
4246 readonly logger ?: ( event : CodexAppServerProtocolLogEvent ) => Effect . Effect < void , never > ;
4347 readonly onNotification ?: (
4448 notification : CodexAppServerIncomingNotification ,
@@ -164,7 +168,9 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
164168 yield * Queue . sliding < CodexAppServerIncomingRequest > ( MAX_BUFFERED_RAW_MESSAGES ) ;
165169 const pending = yield * Ref . make ( new Map < string , CodexAppServerPendingRequest > ( ) ) ;
166170 const nextRequestId = yield * Ref . make ( 1 ) ;
171+ const maxIncomingMessageBytes = options . maxIncomingMessageBytes ?? MAX_INCOMING_MESSAGE_BYTES ;
167172 const remainder : Array < string > = [ ] ;
173+ let remainderBytes = 0 ;
168174 const terminationHandled = yield * Ref . make ( false ) ;
169175 const terminationFailure = yield * Ref . make ( Option . none < CodexError . CodexAppServerError > ( ) ) ;
170176 const terminationSignal = yield * Deferred . make < void > ( ) ;
@@ -399,26 +405,30 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
399405 yield * options . stdio . stdin . pipe (
400406 Stream . interruptWhen ( Deferred . await ( terminationSignal ) ) ,
401407 Stream . decodeText ( ) ,
402- Stream . runForEach ( ( chunk ) =>
403- Effect . sync ( ( ) => {
404- const lines : Array < string > = [ ] ;
408+ Stream . runForEach (
409+ Effect . fnUntraced ( function * ( chunk ) {
405410 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 $ / , "" ) ;
413426 remainder . length = 0 ;
427+ remainderBytes = 0 ;
428+ yield * handleLine ( line ) ;
414429 start = newline + 1 ;
415430 }
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+ } ) ,
422432 ) ,
423433 Effect . matchEffect ( {
424434 onFailure : ( error ) =>
0 commit comments