@@ -29,7 +29,7 @@ import { buildCodexMessagesSnapshot } from "./event-projector-snapshot.js";
2929import { CodexToolProgressProjection } from "./event-projector-tool-progress.js" ;
3030import { CodexToolTranscriptProjection } from "./event-projector-tool-transcript.js" ;
3131import {
32- normalizeCodexResponseTokenUsage ,
32+ CodexResponseCompletionProjection ,
3333 normalizeCodexThreadTokenUsage ,
3434 projectCodexThreadUsageUpdate ,
3535} from "./event-projector-usage.js" ;
@@ -106,7 +106,7 @@ export class CodexAppServerEventProjector {
106106 private synthesizedMissingToolResultError : string | null = null ;
107107 private aborted = false ;
108108 private tokenUsage : ReturnType < typeof normalizeCodexThreadTokenUsage > ;
109- private responseUsage : ReturnType < typeof normalizeCodexResponseTokenUsage > ;
109+ private readonly responseCompletions = new CodexResponseCompletionProjection ( ) ;
110110 private completedCompactionCount = 0 ;
111111 private lastTranscriptTimestamp = 0 ;
112112
@@ -288,13 +288,13 @@ export class CodexAppServerEventProjector {
288288 await this . handleTurnCompleted ( params ) ;
289289 break ;
290290 case "rawResponse/completed" :
291- this . handleRawResponseCompleted ( params ) ;
291+ this . responseCompletions . record ( params ) ;
292292 break ;
293293 case "rawResponseItem/completed" :
294294 await this . handleRawResponseItemCompleted ( params ) ;
295295 break ;
296296 case "error" :
297- this . responseUsage = undefined ;
297+ this . responseCompletions . clear ( ) ;
298298 if ( params . willRetry === true ) {
299299 break ;
300300 }
@@ -334,7 +334,9 @@ export class CodexAppServerEventProjector {
334334 // A terminal timeout must not publish exact usage, but the timeout watcher
335335 // can still recover a completed assistant. Keep the snapshot masked until
336336 // recovery clears the abort instead of destroying it in markTimedOut().
337- const projectedUsage = this . aborted ? this . tokenUsage : ( this . responseUsage ?? this . tokenUsage ) ;
337+ const projectedUsage = this . aborted
338+ ? this . tokenUsage
339+ : ( this . responseCompletions . usage ?? this . tokenUsage ) ;
338340 const hasAssistantItemText = this . assistantProjection . hasAssistantItemTextForSynthesis ( ) ;
339341 const legacyFailClosed =
340342 ! this . completedTurn || this . completedTurn . status !== "completed" || hasAssistantItemText ;
@@ -416,6 +418,9 @@ export class CodexAppServerEventProjector {
416418 ...( agentHarnessResultClassification ? { agentHarnessResultClassification } : { } ) ,
417419 bootstrapPromptWarningSignaturesSeen : this . params . bootstrapPromptWarningSignaturesSeen ,
418420 bootstrapPromptWarningSignature : this . params . bootstrapPromptWarningSignature ,
421+ ...( this . responseCompletions . modelIterations > 0
422+ ? { modelIterations : this . responseCompletions . modelIterations }
423+ : { } ) ,
419424 messagesSnapshot,
420425 assistantTexts,
421426 toolMetas,
@@ -483,7 +488,7 @@ export class CodexAppServerEventProjector {
483488
484489 markAborted ( ) : void {
485490 this . aborted = true ;
486- this . responseUsage = undefined ;
491+ this . responseCompletions . clear ( ) ;
487492 }
488493
489494 isCompacting ( ) : boolean {
@@ -594,21 +599,14 @@ export class CodexAppServerEventProjector {
594599 } ) ;
595600 }
596601
597- private handleRawResponseCompleted ( params : JsonObject ) : void {
598- const usage = isJsonObject ( params . usage ) ? params . usage : undefined ;
599- // Every provider completion replaces the prior response snapshot. A final
600- // response with missing or malformed usage must leave freshness unknown.
601- this . responseUsage = usage ? normalizeCodexResponseTokenUsage ( usage ) : undefined ;
602- }
603-
604602 private async handleTurnCompleted ( params : JsonObject ) : Promise < void > {
605603 const turn = readCodexTurn ( params . turn ) ;
606604 if ( ! turn || turn . id !== this . turnId ) {
607605 return ;
608606 }
609607 this . completedTurn = turn ;
610608 if ( turn . status !== "completed" ) {
611- this . responseUsage = undefined ;
609+ this . responseCompletions . clear ( ) ;
612610 }
613611 if ( turn . status === "failed" ) {
614612 const usageLimitMessage = formatCodexUsageLimitErrorMessage ( {
0 commit comments