@@ -50,6 +50,8 @@ export interface AcpAgentTerminalState {
5050 readonly command ?: string ;
5151 readonly cwd ?: string ;
5252 readonly output : string ;
53+ /** UTF-8 byte length of `output`, so appending a chunk never re-encodes the buffer. */
54+ readonly outputBytes : number ;
5355 readonly exitStatus ?: EffectAcpSchema . TerminalExitStatus ;
5456 readonly pendingOutputBytes ?: Uint8Array ;
5557}
@@ -126,26 +128,43 @@ function decodeTerminalOutputChunk(
126128 : { text, pendingOutputBytes : Uint8Array . from ( bytes . subarray ( completeEnd ) ) } ;
127129}
128130
129- function truncateTerminalOutput ( output : string ) : string {
130- const bytes = Buffer . from ( output , "utf8" ) ;
131- if ( bytes . length <= MAX_ACP_TERMINAL_OUTPUT_BYTES ) return output ;
132- let start = bytes . length - MAX_ACP_TERMINAL_OUTPUT_BYTES ;
133- while ( start < bytes . length && ( bytes [ start ] ! & 0xc0 ) === 0x80 ) start += 1 ;
134- return bytes . subarray ( start ) . toString ( "utf8" ) ;
131+ /**
132+ * Appends decoded text to terminal output and keeps the newest
133+ * `MAX_ACP_TERMINAL_OUTPUT_BYTES`, dropping whole leading characters. Only the
134+ * appended text and the dropped prefix are measured, so per-chunk cost does not
135+ * grow with the retained output.
136+ */
137+ function appendTerminalOutput (
138+ output : string ,
139+ outputBytes : number ,
140+ text : string ,
141+ ) : Pick < AcpAgentTerminalState , "output" | "outputBytes" > {
142+ const combined = `${ output } ${ text } ` ;
143+ const combinedBytes = outputBytes + Buffer . byteLength ( text , "utf8" ) ;
144+ const excess = combinedBytes - MAX_ACP_TERMINAL_OUTPUT_BYTES ;
145+ if ( excess <= 0 ) return { output : combined , outputBytes : combinedBytes } ;
146+ let start = 0 ;
147+ let dropped = 0 ;
148+ while ( dropped < excess ) {
149+ const codePoint = combined . codePointAt ( start ) ! ;
150+ dropped += codePoint < 0x80 ? 1 : codePoint < 0x800 ? 2 : codePoint < 0x10000 ? 3 : 4 ;
151+ start += codePoint < 0x10000 ? 1 : 2 ;
152+ }
153+ return { output : combined . slice ( start ) , outputBytes : combinedBytes - dropped } ;
135154}
136155
137156export function applyAcpAgentTerminalUpdate (
138157 previous : AcpAgentTerminalState | undefined ,
139158 update : AcpAgentTerminalUpdate ,
140159) : AcpAgentTerminalState {
141- const current = previous ?? { output : "" } ;
160+ const current = previous ?? { output : "" , outputBytes : 0 } ;
142161 if ( update . sessionUpdate === "terminal_output_chunk" ) {
143162 const decoded = decodeTerminalOutputChunk ( current . pendingOutputBytes , update . data ) ;
144163 if ( decoded === undefined ) return current ;
145164 const { pendingOutputBytes : _pendingOutputBytes , ...withoutPendingOutputBytes } = current ;
146165 return {
147166 ...withoutPendingOutputBytes ,
148- output : truncateTerminalOutput ( ` ${ current . output } ${ decoded . text } ` ) ,
167+ ... appendTerminalOutput ( current . output , current . outputBytes , decoded . text ) ,
149168 ...( decoded . pendingOutputBytes === undefined
150169 ? { }
151170 : { pendingOutputBytes : decoded . pendingOutputBytes } ) ,
@@ -173,7 +192,7 @@ export function applyAcpAgentTerminalUpdate(
173192 const { pendingOutputBytes : _pendingOutputBytes , ...withoutPendingOutputBytes } = next ;
174193 next = {
175194 ...withoutPendingOutputBytes ,
176- output : truncateTerminalOutput ( decoded . text ) ,
195+ ... appendTerminalOutput ( "" , 0 , decoded . text ) ,
177196 ...( decoded . pendingOutputBytes === undefined
178197 ? { }
179198 : { pendingOutputBytes : decoded . pendingOutputBytes } ) ,
@@ -191,7 +210,7 @@ export function applyAcpAgentTerminalUpdate(
191210 const { pendingOutputBytes : _pendingOutputBytes , ...withoutPendingOutputBytes } = next ;
192211 next = {
193212 ...withoutPendingOutputBytes ,
194- output : truncateTerminalOutput ( ` ${ next . output } ${ pendingText } ` ) ,
213+ ... appendTerminalOutput ( next . output , next . outputBytes , pendingText ) ,
195214 exitStatus : update . exitStatus ,
196215 } ;
197216 }
0 commit comments