@@ -16,7 +16,6 @@ import {
1616} from "./messages"
1717import { renderSystemPrompt , type PromptStore } from "./prompts"
1818import { buildProtectedToolsExtension } from "./prompts/extensions/system"
19- import { attachCompressionDuration } from "./compress/state"
2019import {
2120 applyPendingManualTrigger ,
2221 handleContextCommand ,
@@ -30,7 +29,14 @@ import {
3029} from "./commands"
3130import { type HostPermissionSnapshot } from "./host-permissions"
3231import { compressPermission , syncCompressPermissionState } from "./compress-permission"
33- import { checkSession , ensureSessionInitialized , saveSessionState , syncToolCache } from "./state"
32+ import {
33+ checkSession ,
34+ ensureSessionInitialized ,
35+ applyPendingCompressionDurations ,
36+ queueCompressionDuration ,
37+ saveSessionState ,
38+ syncToolCache ,
39+ } from "./state"
3440import { cacheSystemPromptTokens } from "./ui/utils"
3541
3642const INTERNAL_AGENT_SIGNATURES = [
@@ -269,6 +275,12 @@ export function createTextCompleteHandler() {
269275
270276export function createEventHandler ( state : SessionState , logger : Logger ) {
271277 return async ( input : { event : any } ) => {
278+ const eventSessionId =
279+ typeof input . event ?. properties ?. sessionID === "string"
280+ ? input . event . properties . sessionID
281+ : typeof input . event ?. properties ?. part ?. sessionID === "string"
282+ ? input . event . properties . part . sessionID
283+ : undefined
272284 const eventTime =
273285 typeof input . event ?. time === "number" && Number . isFinite ( input . event . time )
274286 ? input . event . time
@@ -287,20 +299,26 @@ export function createEventHandler(state: SessionState, logger: Logger) {
287299 }
288300
289301 if ( part . state . status === "pending" ) {
290- if ( typeof part . callID !== "string" || typeof part . messageID !== "string" ) {
302+ if (
303+ typeof part . callID !== "string" ||
304+ typeof part . messageID !== "string" ||
305+ typeof eventSessionId !== "string"
306+ ) {
291307 return
292308 }
293309
294- if ( state . compressionStarts . has ( part . callID ) ) {
310+ if ( state . compressionTiming . startsByCallId . has ( part . callID ) ) {
295311 return
296312 }
297313
298314 const startedAt = eventTime ?? Date . now ( )
299- state . compressionStarts . set ( part . callID , {
315+ state . compressionTiming . startsByCallId . set ( part . callID , {
316+ sessionId : eventSessionId ,
300317 messageId : part . messageID ,
301318 startedAt,
302319 } )
303320 logger . debug ( "Recorded compression start" , {
321+ sessionID : eventSessionId ,
304322 callID : part . callID ,
305323 messageID : part . messageID ,
306324 startedAt,
@@ -309,12 +327,16 @@ export function createEventHandler(state: SessionState, logger: Logger) {
309327 }
310328
311329 if ( part . state . status === "completed" ) {
312- if ( typeof part . callID !== "string" || typeof part . messageID !== "string" ) {
330+ if (
331+ typeof part . callID !== "string" ||
332+ typeof part . messageID !== "string" ||
333+ typeof eventSessionId !== "string"
334+ ) {
313335 return
314336 }
315337
316- const start = state . compressionStarts . get ( part . callID )
317- state . compressionStarts . delete ( part . callID )
338+ const start = state . compressionTiming . startsByCallId . get ( part . callID )
339+ state . compressionTiming . startsByCallId . delete ( part . callID )
318340
319341 const runningAt =
320342 typeof part . state . time ?. start === "number" && Number . isFinite ( part . state . time . start )
@@ -341,28 +363,25 @@ export function createEventHandler(state: SessionState, logger: Logger) {
341363 return
342364 }
343365
344- const updates = attachCompressionDuration (
345- state ,
346- part . callID ,
347- part . messageID ,
348- durationMs ,
349- )
366+ queueCompressionDuration ( state , eventSessionId , part . callID , part . messageID , durationMs )
367+
368+ const updates =
369+ state . sessionId === eventSessionId
370+ ? applyPendingCompressionDurations ( state , eventSessionId )
371+ : 0
350372 if ( updates === 0 ) {
351373 return
352374 }
353375
376+ await saveSessionState ( state , logger )
377+
354378 logger . info ( "Attached compression time to blocks" , {
379+ sessionID : eventSessionId ,
355380 callID : part . callID ,
356381 messageID : part . messageID ,
357382 blocks : updates ,
358383 durationMs,
359384 } )
360-
361- saveSessionState ( state , logger ) . catch ( ( error ) => {
362- logger . warn ( "Failed to persist compression time update" , {
363- error : error instanceof Error ? error . message : String ( error ) ,
364- } )
365- } )
366385 return
367386 }
368387
@@ -371,7 +390,7 @@ export function createEventHandler(state: SessionState, logger: Logger) {
371390 }
372391
373392 if ( typeof part . callID === "string" ) {
374- state . compressionStarts . delete ( part . callID )
393+ state . compressionTiming . startsByCallId . delete ( part . callID )
375394 }
376395 }
377396}
0 commit comments