@@ -128,6 +128,17 @@ interface AcpPendingRequest {
128128 readonly method : string ;
129129}
130130
131+ /** A decoded wire message and the length of the JSON line it arrived on. */
132+ interface AcpDecodedWireMessage {
133+ readonly message : RpcMessage . FromClientEncoded | RpcMessage . FromServerEncoded ;
134+ readonly lineLength : number ;
135+ }
136+
137+ interface AcpBufferedRawNotification {
138+ readonly notification : AcpIncomingNotification ;
139+ readonly lineLength : number ;
140+ }
141+
131142interface AcpOutgoingWrite {
132143 readonly acknowledgement ?: Deferred . Deferred < void , AcpError . AcpError > ;
133144 readonly payload : string | Uint8Array ;
@@ -153,19 +164,30 @@ const makeStrictNdJsonRpcParser = () => {
153164 const decoder = new TextDecoder ( ) ;
154165 let buffer = "" ;
155166 return {
156- decode : ( bytes : Uint8Array | string ) : ReadonlyArray < unknown > => {
167+ // Every message in a batch line is charged the whole line.
168+ decode : ( bytes : Uint8Array | string ) : ReadonlyArray < AcpDecodedWireMessage > => {
157169 buffer += typeof bytes === "string" ? bytes : decoder . decode ( bytes , { stream : true } ) ;
158170 const lines = buffer . split ( "\n" ) ;
159171 buffer = lines . pop ( ) ?? "" ;
160- return lines . flatMap ( ( line ) => codec . decode ( line ) ) ;
172+ return lines . flatMap ( ( line ) =>
173+ codec . decode ( line ) . map ( ( message ) => ( {
174+ message : normalizeAcpJsonRpcError (
175+ message as RpcMessage . FromClientEncoded | RpcMessage . FromServerEncoded ,
176+ ) ,
177+ lineLength : line . length ,
178+ } ) ) ,
179+ ) ;
161180 } ,
162181 encode : ( response : Parameters < typeof codec . encode > [ 0 ] ) => {
163182 const encoded = codec . encode ( response ) ;
164183 return encoded === undefined ? undefined : `${ encoded } \n` ;
165184 } ,
166185 } ;
167186} ;
187+ // Unread raw notifications are best-effort. The newest is always kept; older
188+ // ones are dropped once the backlog passes either limit.
168189const MAX_BUFFERED_RAW_NOTIFICATIONS = 32 ;
190+ const MAX_BUFFERED_RAW_NOTIFICATION_JSON_LENGTH = 16 * 1024 * 1024 ;
169191// Outbound JSON-RPC notification: no `id`, so peers never treat it as a request.
170192const encodeJsonRpcNotification = Schema . encodeUnknownExit (
171193 Schema . fromJsonString (
@@ -213,9 +235,14 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
213235 const parser = makeStrictNdJsonRpcParser ( ) ;
214236 const serverQueue = yield * Queue . unbounded < RpcMessage . FromClientEncoded > ( ) ;
215237 const clientQueue = yield * Queue . unbounded < RpcMessage . FromServerEncoded > ( ) ;
216- const notificationQueue = yield * Queue . sliding < AcpIncomingNotification > (
217- MAX_BUFFERED_RAW_NOTIFICATIONS ,
218- ) ;
238+ const rawNotifications = yield * Queue . unbounded < AcpBufferedRawNotification , Cause . Done < void > > ( ) ;
239+ // JSON length of the notifications offered since the queue was last empty.
240+ // `incoming` takes the whole backlog on every pull, so an empty queue holds
241+ // nothing, and a read can only make this an overestimate.
242+ let rawNotificationsJsonLength = 0 ;
243+ // Raw readers finish with the connection: termination ends the queue, and so
244+ // does closing the scope, which interrupts the reader before it terminates.
245+ yield * Effect . addFinalizer ( ( ) => Queue . end ( rawNotifications ) ) ;
219246 const disconnects = yield * Queue . unbounded < number > ( ) ;
220247 const outgoing = yield * Queue . unbounded < AcpOutgoingWrite , AcpError . AcpError | Cause . Done < void > > ( ) ;
221248 const outgoingWriterState = yield * Ref . make < AcpOutgoingWriterState > ( {
@@ -384,8 +411,25 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
384411 ) ,
385412 ) ;
386413
387- const dispatchNotification = ( notification : AcpIncomingNotification ) =>
388- Queue . offer ( notificationQueue , notification ) . pipe (
414+ const bufferRawNotification = ( notification : AcpIncomingNotification , lineLength : number ) =>
415+ Effect . sync ( ( ) => {
416+ if ( Queue . sizeUnsafe ( rawNotifications ) === 0 ) rawNotificationsJsonLength = 0 ;
417+ while (
418+ Queue . sizeUnsafe ( rawNotifications ) >= MAX_BUFFERED_RAW_NOTIFICATIONS ||
419+ ( Queue . sizeUnsafe ( rawNotifications ) > 0 &&
420+ rawNotificationsJsonLength + lineLength > MAX_BUFFERED_RAW_NOTIFICATION_JSON_LENGTH )
421+ ) {
422+ const dropped = Queue . takeUnsafe ( rawNotifications ) ;
423+ if ( dropped === undefined || Exit . isFailure ( dropped ) ) break ;
424+ rawNotificationsJsonLength -= dropped . value . lineLength ;
425+ }
426+ if ( Queue . offerUnsafe ( rawNotifications , { notification, lineLength } ) ) {
427+ rawNotificationsJsonLength += lineLength ;
428+ }
429+ } ) ;
430+
431+ const dispatchNotification = ( notification : AcpIncomingNotification , lineLength : number ) =>
432+ bufferRawNotification ( notification , lineLength ) . pipe (
389433 Effect . andThen (
390434 // A failing or dying handler must not stop the reader, or every later
391435 // message on the connection goes unanswered.
@@ -415,6 +459,7 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
415459 return [
416460 Effect . gen ( function * ( ) {
417461 yield * Queue . offer ( disconnects , 0 ) ;
462+ yield * Queue . end ( rawNotifications ) ;
418463 const error = yield * classify ( ) ;
419464 yield * Deferred . fail ( terminationFailure , error ) ;
420465 yield * failAllExtPending ( error ) ;
@@ -499,7 +544,7 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
499544 ) ;
500545 } ;
501546
502- const handleRequestEncoded = ( message : RpcMessage . RequestEncoded ) => {
547+ const handleRequestEncoded = ( message : RpcMessage . RequestEncoded , lineLength : number ) => {
503548 if ( message . id === "" ) {
504549 if ( message . tag === CLIENT_METHODS . session_update ) {
505550 return decodeSessionUpdate ( message . payload ) . pipe (
@@ -518,7 +563,7 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
518563 cause ,
519564 ) ,
520565 ) ,
521- Effect . flatMap ( dispatchNotification ) ,
566+ Effect . flatMap ( ( notification ) => dispatchNotification ( notification , lineLength ) ) ,
522567 ) ;
523568 }
524569 if (
@@ -542,14 +587,13 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
542587 cause ,
543588 ) ,
544589 ) ,
545- Effect . flatMap ( dispatchNotification ) ,
590+ Effect . flatMap ( ( notification ) => dispatchNotification ( notification , lineLength ) ) ,
546591 ) ;
547592 }
548- return dispatchNotification ( {
549- _tag : "ExtNotification" ,
550- method : message . tag ,
551- params : message . payload ,
552- } ) ;
593+ return dispatchNotification (
594+ { _tag : "ExtNotification" , method : message . tag , params : message . payload } ,
595+ lineLength ,
596+ ) ;
553597 }
554598
555599 const observeIncoming =
@@ -625,12 +669,13 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
625669 } ) ,
626670 ) ;
627671
628- const routeDecodedMessage = (
629- message : RpcMessage . FromClientEncoded | RpcMessage . FromServerEncoded ,
630- ) : Effect . Effect < void , AcpError . AcpError > => {
672+ const routeDecodedMessage = ( {
673+ message,
674+ lineLength,
675+ } : AcpDecodedWireMessage ) : Effect . Effect < void , AcpError . AcpError > => {
631676 switch ( message . _tag ) {
632677 case "Request" :
633- return handleRequestEncoded ( message ) ;
678+ return handleRequestEncoded ( message , lineLength ) ;
634679 case "Exit" :
635680 return handleExitEncoded ( message ) ;
636681 case "Chunk" :
@@ -672,25 +717,22 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
672717 ) . pipe (
673718 Effect . flatMap ( ( ) =>
674719 Effect . try ( {
675- try : ( ) =>
676- (
677- parser . decode ( data ) as ReadonlyArray <
678- RpcMessage . FromClientEncoded | RpcMessage . FromServerEncoded
679- >
680- ) . map ( normalizeAcpJsonRpcError ) ,
720+ try : ( ) => parser . decode ( data ) ,
681721 catch : ( cause ) =>
682722 new AcpError . AcpProtocolParseError ( {
683723 operation : "decode-wire-message" ,
684724 cause,
685725 } ) ,
686726 } ) ,
687727 ) ,
688- Effect . tap ( ( messages ) =>
689- logProtocol ( {
690- direction : "incoming" ,
691- stage : "decoded" ,
692- payload : messages ,
693- } ) ,
728+ Effect . tap ( ( decoded ) =>
729+ options . logIncoming
730+ ? logProtocol ( {
731+ direction : "incoming" ,
732+ stage : "decoded" ,
733+ payload : decoded . map ( ( { message } ) => message ) ,
734+ } )
735+ : Effect . void ,
694736 ) ,
695737 Effect . tapErrorTag ( "AcpProtocolParseError" , ( error ) =>
696738 logProtocol ( {
@@ -708,8 +750,8 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
708750 } ,
709751 } ) ,
710752 ) ,
711- Effect . flatMap ( ( messages ) =>
712- Effect . forEach ( messages , routeDecodedMessage , {
753+ Effect . flatMap ( ( decoded ) =>
754+ Effect . forEach ( decoded , routeDecodedMessage , {
713755 discard : true ,
714756 } ) ,
715757 ) ,
@@ -970,7 +1012,9 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
9701012 clientProtocol,
9711013 serverProtocol,
9721014 get incoming ( ) {
973- return Stream . fromQueue ( notificationQueue ) ;
1015+ return Stream . fromQueue ( rawNotifications ) . pipe (
1016+ Stream . map ( ( { notification } ) => notification ) ,
1017+ ) ;
9741018 } ,
9751019 request : sendRequest ,
9761020 notify : sendNotification ,
0 commit comments