@@ -124,51 +124,69 @@ export function handoffBudget(input: {
124124 ) ;
125125}
126126
127- export function historicalMessage (
128- item : OrchestrationV2TurnItem ,
129- ) : OrchestrationV2HistoricalMessage | null {
130- let text : string ;
127+ export const HISTORICAL_ITEM_TYPES = [
128+ "user_message" ,
129+ "assistant_message" ,
130+ "command_execution" ,
131+ "error" ,
132+ "run_interrupt_result" ,
133+ "file_change" ,
134+ "proposed_plan" ,
135+ ] as const satisfies ReadonlyArray < OrchestrationV2TurnItem [ "type" ] > ;
136+ const historicalItemTypes : ReadonlySet < string > = new Set ( HISTORICAL_ITEM_TYPES ) ;
137+
138+ export const isHistoricalItem = ( item : OrchestrationV2TurnItem ) =>
139+ historicalItemTypes . has ( item . type ) ;
140+
141+ export function historicalText ( item : OrchestrationV2TurnItem ) : string | null {
131142 switch ( item . type ) {
132143 case "user_message" :
133144 case "assistant_message" :
134- text = item . text ;
135- break ;
145+ return item . text ;
136146 case "command_execution" :
137- text = [
147+ return [
138148 `Command: ${ item . input } ` ,
139149 `Exit code: ${ item . exitCode ?? "unknown" } ` ,
140150 item . output ?? "" ,
141151 ] . join ( "\n" ) ;
142- break ;
143152 case "error" :
144- text = item . failure . message ;
145- break ;
153+ return item . failure . message ;
146154 case "run_interrupt_result" :
147- text = item . message ;
148- break ;
155+ return item . message ;
149156 case "file_change" :
150- text = `File change: ${ item . changes ?. map ( ( change ) => change . path ) . join ( ", " ) ?? item . fileName } ` ;
151- break ;
157+ return `File change: ${ item . changes ?. map ( ( change ) => change . path ) . join ( ", " ) ?? item . fileName } ` ;
152158 case "proposed_plan" :
153- text = item . markdown ;
154- break ;
159+ return item . markdown ;
155160 default :
156161 return null ;
157162 }
163+ }
164+
165+ export function historicalMessage (
166+ item : OrchestrationV2TurnItem ,
167+ runStatus ?: OrchestrationV2HistoricalMessage [ "runStatus" ] ,
168+ ) : OrchestrationV2HistoricalMessage | null {
169+ const text = historicalText ( item ) ;
170+ if ( text === null ) return null ;
158171 return {
159172 role : item . type === "user_message" ? "user" : "assistant" ,
160173 text,
161174 threadId : item . threadId ,
162175 runId : item . runId ,
163176 itemId : item . id ,
164- providerThreadId : item . providerThreadId ,
177+ providerThreadId : item . providerThreadId ?? null ,
165178 status : item . status ,
166179 kind : item . type ,
180+ ...( runStatus === undefined ? { } : { runStatus } ) ,
167181 } ;
168182}
169183
184+ function historicalMessageHeader ( message : OrchestrationV2HistoricalMessage ) : string {
185+ return `[Historical ${ message . role } ; ${ message . kind } ; thread=${ message . threadId } ; run=${ message . runId ?? "imported" } ; item=${ message . itemId } ; provider-thread=${ message . providerThreadId ?? "none" } ; status=${ message . status } ${ message . runStatus === undefined ? "" : `; run-status=${ message . runStatus } ` } ]` ;
186+ }
187+
170188function renderHistoricalMessage ( message : OrchestrationV2HistoricalMessage ) : string {
171- return `[Historical ${ message . role } ; ${ message . kind } ; thread= ${ message . threadId } ; run= ${ message . runId ?? "imported" } ; item= ${ message . itemId } ; provider-thread= ${ message . providerThreadId ?? "none" } ; status= ${ message . status } ${ message . runStatus === undefined ? "" : `; run-status= ${ message . runStatus } ` } ] \n${ message . text } ` ;
189+ return `${ historicalMessageHeader ( message ) } \n${ message . text } ` ;
172190}
173191
174192export function historyResponseItems (
@@ -199,6 +217,67 @@ export function renderHistory(
199217
200218const jsonByteLength = ( value : unknown ) => Buffer . byteLength ( JSON . stringify ( value ) ) ;
201219
220+ /** UTF-8 bytes of `JSON.stringify(text)` without the quotes, counted without building the string. */
221+ export function jsonEscapedByteLength ( text : string ) : number {
222+ let bytes = 0 ;
223+ for ( let index = 0 ; index < text . length ; index ++ ) {
224+ const code = text . charCodeAt ( index ) ;
225+ if ( code < 0x20 ) {
226+ bytes += code === 8 || code === 9 || code === 10 || code === 12 || code === 13 ? 2 : 6 ;
227+ } else if ( code === 0x22 || code === 0x5c ) {
228+ bytes += 2 ;
229+ } else if ( code < 0x80 ) {
230+ bytes += 1 ;
231+ } else if ( code < 0x800 ) {
232+ bytes += 2 ;
233+ } else if ( code >= 0xd800 && code <= 0xdbff ) {
234+ const next = text . charCodeAt ( index + 1 ) ;
235+ if ( next >= 0xdc00 && next <= 0xdfff ) {
236+ bytes += 4 ;
237+ index ++ ;
238+ } else {
239+ bytes += 6 ;
240+ }
241+ } else if ( code >= 0xdc00 && code <= 0xdfff ) {
242+ bytes += 6 ;
243+ } else {
244+ bytes += 3 ;
245+ }
246+ }
247+ return bytes ;
248+ }
249+
250+ // JSON string escaping is concatenative, so a message's serialized size is its escaped
251+ // header and body plus a constant per-role envelope; selection never builds the strings.
252+ const envelopeBytes = ( role : OrchestrationV2HistoricalMessage [ "role" ] ) => {
253+ const probe = {
254+ role,
255+ text : "" ,
256+ threadId : "" ,
257+ runId : null ,
258+ itemId : "" ,
259+ providerThreadId : null ,
260+ status : "completed" ,
261+ kind : "user_message" ,
262+ } as unknown as OrchestrationV2HistoricalMessage ;
263+ const rendered = renderHistoricalMessage ( probe ) ;
264+ return jsonByteLength ( historyResponseItems ( [ probe ] , "" ) [ 1 ] ) - jsonByteLength ( rendered ) ;
265+ } ;
266+ const RESPONSE_ENVELOPE_BYTES = {
267+ user : envelopeBytes ( "user" ) ,
268+ assistant : envelopeBytes ( "assistant" ) ,
269+ } ;
270+ const MIN_MESSAGE_COST = Math . min ( RESPONSE_ENVELOPE_BYTES . user , RESPONSE_ENVELOPE_BYTES . assistant ) ;
271+
272+ function messageCost ( message : OrchestrationV2HistoricalMessage ) : number {
273+ const rendered =
274+ jsonEscapedByteLength ( historicalMessageHeader ( message ) ) +
275+ 2 +
276+ jsonEscapedByteLength ( message . text ) +
277+ 2 ;
278+ return Math . max ( rendered + RESPONSE_ENVELOPE_BYTES [ message . role ] + 1 , rendered + 4 ) ;
279+ }
280+
202281export function historyCost (
203282 messages : ReadonlyArray < OrchestrationV2HistoricalMessage > ,
204283 context : string ,
@@ -231,23 +310,25 @@ export function selectHistory(input: {
231310 const tryAdd = ( index : number ) => {
232311 const message = messages [ index ] ;
233312 if ( message === undefined || selected . has ( index ) || message . text . length > remaining ) return ;
234- const cost = Math . max (
235- Buffer . byteLength ( JSON . stringify ( historyResponseItems ( [ message ] , "" ) [ 1 ] ) ) + 1 ,
236- Buffer . byteLength ( JSON . stringify ( renderHistoricalMessage ( message ) ) ) + 4 ,
237- ) ;
313+ const cost = messageCost ( message ) ;
238314 if ( cost > remaining ) return ;
239315 selected . add ( index ) ;
240316 remaining -= cost ;
241317 } ;
242318 tryAdd ( messages . findLastIndex ( ( message ) => message . role === "user" ) ) ;
243319 tryAdd ( messages . findLastIndex ( ( message ) => message . role === "assistant" ) ) ;
244320 tryAdd ( messages . findIndex ( ( message ) => message . role === "user" ) ) ;
245- for ( let index = messages . length - 1 ; index >= 0 ; index -- ) tryAdd ( index ) ;
321+ for ( let index = messages . length - 1 ; index >= 0 && remaining >= MIN_MESSAGE_COST ; index -- )
322+ tryAdd ( index ) ;
323+ const selectedMessages : Array < OrchestrationV2HistoricalMessage > = [ ] ;
324+ const omittedItemIds : Array < OrchestrationV2HistoricalMessage [ "itemId" ] > = [ ] ;
325+ for ( const [ index , message ] of messages . entries ( ) ) {
326+ if ( selected . has ( index ) ) selectedMessages . push ( message ) ;
327+ else omittedItemIds . push ( message . itemId ) ;
328+ }
246329 return {
247- messages : messages . filter ( ( _ , index ) => selected . has ( index ) ) ,
248- omittedItemIds : messages
249- . filter ( ( _ , index ) => ! selected . has ( index ) )
250- . map ( ( message ) => message . itemId ) ,
330+ messages : selectedMessages ,
331+ omittedItemIds,
251332 context : contextFor ( selected . size ) ,
252333 omittedItems : ( input . omittedItems ?? 0 ) + messages . length - selected . size ,
253334 } ;
0 commit comments