@@ -182,6 +182,123 @@ describe("parseCodexLine", () => {
182182 }
183183 } ) ;
184184
185+ it . each ( [ "continuation" , "fork" , "subagent" ] ) (
186+ "bills only the latest request when a %s has no initial counter baseline" ,
187+ ( source ) => {
188+ const state = initialCodexScanState ( ) ;
189+ parseCodexLine (
190+ JSON . stringify ( {
191+ type : "session_meta" ,
192+ timestamp : "2026-08-01T05:17:41.000Z" ,
193+ payload : {
194+ id : "child" ,
195+ model_provider : "openai" ,
196+ ...( source === "fork" ? { forked_from_id : "parent" } : { } ) ,
197+ ...( source === "subagent"
198+ ? { source : { subagent : { thread_spawn : { parent_thread_id : "parent" } } } }
199+ : { source : "vscode" } ) ,
200+ } ,
201+ } ) ,
202+ state ,
203+ ) ;
204+ parseCodexLine ( turnContext , state ) ;
205+ const request = {
206+ ...counters ( 100 , 20 ) ,
207+ cached_input_tokens : 30 ,
208+ cache_write_input_tokens : 10 ,
209+ reasoning_output_tokens : 5 ,
210+ } ;
211+ const cumulative = {
212+ ...counters ( 500 , 60 ) ,
213+ cached_input_tokens : 200 ,
214+ cache_write_input_tokens : 20 ,
215+ reasoning_output_tokens : 15 ,
216+ } ;
217+ const first = counted ( request , cumulative ) ;
218+ expect ( parseCodexLine ( first , state ) ) . toMatchObject ( {
219+ sessionId : "child" ,
220+ totals : {
221+ uncachedInputTokens : 60 ,
222+ cachedInputTokens : 30 ,
223+ cacheCreationTokens : 10 ,
224+ outputTokens : 20 ,
225+ reasoningTokens : 5 ,
226+ } ,
227+ } ) ;
228+ expect ( parseCodexLine ( first , state ) ) . toBeNull ( ) ;
229+ expect ( parseCodexLine ( counted ( counters ( 10 , 2 ) , cumulative ) , state ) ) . toBeNull ( ) ;
230+ expect (
231+ parseCodexLine (
232+ counted ( counters ( 10 , 2 ) , {
233+ ...cumulative ,
234+ input_tokens : 510 ,
235+ output_tokens : 62 ,
236+ total_tokens : 572 ,
237+ } ) ,
238+ state ,
239+ ) ?. totals . outputTokens ,
240+ ) . toBe ( 2 ) ;
241+ expect ( codexUsageFormat . readMalformedRecords ?.( state ) ) . toBe ( 0 ) ;
242+ } ,
243+ ) ;
244+
245+ it . each ( [
246+ {
247+ ...counters ( 90 , 40 ) ,
248+ cached_input_tokens : 60 ,
249+ cache_write_input_tokens : 20 ,
250+ reasoning_output_tokens : 10 ,
251+ } ,
252+ {
253+ ...counters ( 200 , 10 ) ,
254+ cached_input_tokens : 60 ,
255+ cache_write_input_tokens : 20 ,
256+ reasoning_output_tokens : 5 ,
257+ } ,
258+ {
259+ ...counters ( 200 , 40 ) ,
260+ cached_input_tokens : 20 ,
261+ cache_write_input_tokens : 20 ,
262+ reasoning_output_tokens : 10 ,
263+ } ,
264+ {
265+ ...counters ( 200 , 40 ) ,
266+ cached_input_tokens : 60 ,
267+ cache_write_input_tokens : 5 ,
268+ reasoning_output_tokens : 10 ,
269+ } ,
270+ {
271+ ...counters ( 200 , 40 ) ,
272+ cached_input_tokens : 60 ,
273+ cache_write_input_tokens : 20 ,
274+ reasoning_output_tokens : 3 ,
275+ } ,
276+ {
277+ ...counters ( 110 , 40 ) ,
278+ cached_input_tokens : 60 ,
279+ cache_write_input_tokens : 20 ,
280+ reasoning_output_tokens : 10 ,
281+ } ,
282+ {
283+ ...counters ( 200 , 30 ) ,
284+ cached_input_tokens : 60 ,
285+ cache_write_input_tokens : 20 ,
286+ reasoning_output_tokens : 20 ,
287+ } ,
288+ ] ) ( "rejects an initial snapshot with an impossible inherited baseline: %j" , ( cumulative ) => {
289+ const state = initialCodexScanState ( ) ;
290+ parseCodexLine ( turnContext , state ) ;
291+ const request = {
292+ ...counters ( 100 , 20 ) ,
293+ cached_input_tokens : 30 ,
294+ cache_write_input_tokens : 10 ,
295+ reasoning_output_tokens : 5 ,
296+ } ;
297+ expect ( parseCodexLine ( counted ( request , cumulative ) , state ) ) . toBeNull ( ) ;
298+ expect ( codexUsageFormat . readMalformedRecords ?.( state ) ) . toBe ( 1 ) ;
299+ expect ( state . lastCumulativeUsage ) . toEqual ( cumulative ) ;
300+ } ) ;
301+
185302 it . each ( [ "request" , "cumulative" ] ) ( "skips malformed %s counters and recovers" , ( malformed ) => {
186303 const state = initialCodexScanState ( ) ;
187304 parseCodexLine ( turnContext , state ) ;
0 commit comments