@@ -180,15 +180,26 @@ describe("native compact usage reporting", () => {
180180 } as unknown as OcxConfig ;
181181 globalThis . fetch = ( async ( ) => jsonResponse ( completedPayload ( "native summary" ) ) ) as typeof fetch ;
182182 const logCtx : RequestLogContext = { model : "" , provider : "" } ;
183- const response = await handleResponsesCompact (
184- compactionRequest ( baseCompactionBody ( { model : "openai-apikey/gpt-5.5" } ) ) ,
185- config ,
186- logCtx ,
187- ) ;
183+ const previousUsageDebug = process . env . OPENCODEX_USAGE_DEBUG ;
184+ process . env . OPENCODEX_USAGE_DEBUG = "1" ;
185+ let response : Response ;
186+ try {
187+ response = await handleResponsesCompact (
188+ compactionRequest ( baseCompactionBody ( { model : "openai-apikey/gpt-5.5" } ) ) ,
189+ config ,
190+ logCtx ,
191+ ) ;
192+ } finally {
193+ if ( previousUsageDebug === undefined ) delete process . env . OPENCODEX_USAGE_DEBUG ;
194+ else process . env . OPENCODEX_USAGE_DEBUG = previousUsageDebug ;
195+ }
188196 expect ( response . status ) . toBe ( 200 ) ;
189- const body = await response . json ( ) as { usage ?: Record < string , unknown > } ;
190- expect ( body . usage ) . toMatchObject ( { input_tokens : 10 , output_tokens : 5 , total_tokens : 15 } ) ;
197+ expect ( await response . json ( ) ) . toEqual ( completedPayload ( "native summary" ) ) ;
191198 expect ( logCtx . usage ) . toMatchObject ( { inputTokens : 10 , outputTokens : 5 , totalTokens : 15 } ) ;
199+ // The compact body is replacement history; even with usage debug on it must
200+ // never be sampled into the debug log.
201+ expect ( logCtx . usageDebugBodyKind ) . toBeUndefined ( ) ;
202+ expect ( logCtx . usageDebugBodySample ) . toBeUndefined ( ) ;
192203 } ) ;
193204} ) ;
194205
0 commit comments