@@ -51,6 +51,8 @@ export interface AcpAgentTerminalState {
5151 readonly command ?: string ;
5252 readonly cwd ?: string ;
5353 readonly output : string ;
54+ /** UTF-8 byte length of `output`, so appending a chunk never re-encodes the buffer. */
55+ readonly outputBytes : number ;
5456 readonly exitStatus ?: EffectAcpSchema . TerminalExitStatus ;
5557 /** Incomplete trailing UTF-8 bytes retained until the next output update. */
5658 readonly pendingOutputBytes ?: Uint8Array ;
@@ -133,27 +135,44 @@ function decodeTerminalOutputChunk(
133135 : { text, pendingOutputBytes : Uint8Array . from ( bytes . subarray ( completeEnd ) ) } ;
134136}
135137
136- function truncateTerminalOutput ( output : string ) : string {
137- const bytes = Buffer . from ( output , "utf8" ) ;
138- if ( bytes . length <= MAX_ACP_TERMINAL_OUTPUT_BYTES ) return output ;
139- let start = bytes . length - MAX_ACP_TERMINAL_OUTPUT_BYTES ;
140- while ( start < bytes . length && ( bytes [ start ] ! & 0xc0 ) === 0x80 ) start += 1 ;
141- return bytes . subarray ( start ) . toString ( "utf8" ) ;
138+ /**
139+ * Appends decoded text to terminal output and keeps the newest
140+ * `MAX_ACP_TERMINAL_OUTPUT_BYTES`, dropping whole leading characters. Only the
141+ * appended text and the dropped prefix are measured, so per-chunk cost does not
142+ * grow with the retained output.
143+ */
144+ function appendTerminalOutput (
145+ output : string ,
146+ outputBytes : number ,
147+ text : string ,
148+ ) : Pick < AcpAgentTerminalState , "output" | "outputBytes" > {
149+ const combined = `${ output } ${ text } ` ;
150+ const combinedBytes = outputBytes + Buffer . byteLength ( text , "utf8" ) ;
151+ const excess = combinedBytes - MAX_ACP_TERMINAL_OUTPUT_BYTES ;
152+ if ( excess <= 0 ) return { output : combined , outputBytes : combinedBytes } ;
153+ let start = 0 ;
154+ let dropped = 0 ;
155+ while ( dropped < excess ) {
156+ const codePoint = combined . codePointAt ( start ) ! ;
157+ dropped += codePoint < 0x80 ? 1 : codePoint < 0x800 ? 2 : codePoint < 0x10000 ? 3 : 4 ;
158+ start += codePoint < 0x10000 ? 1 : 2 ;
159+ }
160+ return { output : combined . slice ( start ) , outputBytes : combinedBytes - dropped } ;
142161}
143162
144163/** Applies an ACP v2 agent-owned terminal snapshot or chunk to display state. */
145164export function applyAcpAgentTerminalUpdate (
146165 previous : AcpAgentTerminalState | undefined ,
147166 update : AcpAgentTerminalUpdate ,
148167) : AcpAgentTerminalState {
149- const current = previous ?? { output : "" } ;
168+ const current = previous ?? { output : "" , outputBytes : 0 } ;
150169 if ( update . sessionUpdate === "terminal_output_chunk" ) {
151170 const decoded = decodeTerminalOutputChunk ( current . pendingOutputBytes , update . data ) ;
152171 if ( decoded === undefined ) return current ;
153172 const { pendingOutputBytes : _pendingOutputBytes , ...withoutPendingOutputBytes } = current ;
154173 return {
155174 ...withoutPendingOutputBytes ,
156- output : truncateTerminalOutput ( ` ${ current . output } ${ decoded . text } ` ) ,
175+ ... appendTerminalOutput ( current . output , current . outputBytes , decoded . text ) ,
157176 ...( decoded . pendingOutputBytes === undefined
158177 ? { }
159178 : { pendingOutputBytes : decoded . pendingOutputBytes } ) ,
@@ -181,7 +200,7 @@ export function applyAcpAgentTerminalUpdate(
181200 const { pendingOutputBytes : _pendingOutputBytes , ...withoutPendingOutputBytes } = next ;
182201 next = {
183202 ...withoutPendingOutputBytes ,
184- output : truncateTerminalOutput ( decoded . text ) ,
203+ ... appendTerminalOutput ( "" , 0 , decoded . text ) ,
185204 ...( decoded . pendingOutputBytes === undefined
186205 ? { }
187206 : { pendingOutputBytes : decoded . pendingOutputBytes } ) ,
@@ -199,7 +218,7 @@ export function applyAcpAgentTerminalUpdate(
199218 const { pendingOutputBytes : _pendingOutputBytes , ...withoutPendingOutputBytes } = next ;
200219 next = {
201220 ...withoutPendingOutputBytes ,
202- output : truncateTerminalOutput ( ` ${ next . output } ${ pendingText } ` ) ,
221+ ... appendTerminalOutput ( next . output , next . outputBytes , pendingText ) ,
203222 exitStatus : update . exitStatus ,
204223 } ;
205224 }
0 commit comments