@@ -8,6 +8,9 @@ import * as NodePath from "node:path";
88import { afterEach , assert , beforeEach , describe , it } from "@effect/vitest" ;
99
1010import { readTranscriptRecords } from "./usageTranscriptReader.ts" ;
11+ import { UsageAggregator } from "./usageAggregation.ts" ;
12+ import { dedupeWithinFile } from "./usageScanCache.ts" ;
13+ import { parseRateTable } from "./usagePricing.ts" ;
1114
1215let dir : string ;
1316
@@ -61,6 +64,65 @@ function codexUsageLine(outputTokens: number, secondsOffset: number): string {
6164}
6265
6366describe ( "readTranscriptRecords resume" , ( ) => {
67+ it ( "counts and prices an advisor arriving after the executor, once across replays" , async ( ) => {
68+ const path = NodePath . join ( dir , "advisor.jsonl" ) ;
69+ const executor = JSON . parse ( claudeLine ( 1 , 5 ) ) ;
70+ executor . message . usage . iterations = [
71+ { type : "message" , input_tokens : 10 , output_tokens : 5 } ,
72+ { type : "advisor_message" , model : "claude-opus-5" } ,
73+ ] ;
74+ await NodeFSP . writeFile ( path , JSON . stringify ( executor ) + "\n" ) ;
75+ const first = await readTranscriptRecords ( path , "claude" ) ;
76+ assert . isNotNull ( first ) ;
77+ assert . strictEqual ( first . records . length , 1 ) ;
78+
79+ executor . message . usage . iterations = [
80+ { type : "message" , input_tokens : 10 , output_tokens : 5 } ,
81+ { type : "advisor_message" , model : "claude-opus-5" , input_tokens : 100 , output_tokens : 20 } ,
82+ ] ;
83+ const advisorLine = JSON . stringify ( executor ) + "\n" ;
84+ executor . message . usage . iterations . push ( {
85+ type : "advisor_message" ,
86+ model : "claude-opus-5" ,
87+ input_tokens : 200 ,
88+ output_tokens : 40 ,
89+ } ) ;
90+ const secondAdvisorLine = JSON . stringify ( executor ) + "\n" ;
91+ await NodeFSP . appendFile ( path , advisorLine + secondAdvisorLine + secondAdvisorLine ) ;
92+ const appended = await readTranscriptRecords ( path , "claude" , first . position ) ;
93+ assert . isNotNull ( appended ) ;
94+ assert . isTrue ( appended . resumed ) ;
95+ const records = dedupeWithinFile ( [ ...first . records , ...appended . records ] ) ;
96+ assert . strictEqual ( records . length , 3 ) ;
97+
98+ const replayPath = NodePath . join ( dir , "replayed.jsonl" ) ;
99+ await NodeFSP . writeFile ( replayPath , secondAdvisorLine ) ;
100+ const replay = await readTranscriptRecords ( replayPath , "claude" ) ;
101+ assert . isNotNull ( replay ) ;
102+ const aggregator = new UsageAggregator ( {
103+ timeZone : "UTC" ,
104+ sinceDay : "2026-08-01" ,
105+ untilDay : "2026-08-01" ,
106+ rates : parseRateTable ( {
107+ "claude-fable-5" : { input_cost_per_token : 1e-5 , output_cost_per_token : 5e-5 } ,
108+ "claude-opus-5" : { input_cost_per_token : 5e-6 , output_cost_per_token : 2.5e-5 } ,
109+ } ) ,
110+ } ) ;
111+ for ( const record of [ ...records , ...replay . records ] ) aggregator . add ( record ) ;
112+ const buckets = aggregator . finish ( ) . buckets ;
113+ assert . strictEqual ( buckets . length , 2 ) ;
114+ const advisor = buckets . find ( ( bucket ) => bucket . model === "claude-opus-5" ) ;
115+ assert . isDefined ( advisor ) ;
116+ assert . strictEqual ( advisor . records , 2 ) ;
117+ assert . strictEqual ( advisor . totals . uncachedInputTokens , 300 ) ;
118+ assert . strictEqual ( advisor . totals . outputTokens , 60 ) ;
119+ assert . approximately ( advisor . costUsd , 0.003 , 1e-10 ) ;
120+ assert . strictEqual (
121+ buckets . find ( ( bucket ) => bucket . model === "claude-fable-5" ) ?. totals . outputTokens ,
122+ 5 ,
123+ ) ;
124+ } ) ;
125+
64126 it ( "parses only appended lines when resuming a grown file" , async ( ) => {
65127 const path = NodePath . join ( dir , "claude.jsonl" ) ;
66128 await NodeFSP . writeFile ( path , claudeLine ( 1 , 5 ) + claudeLine ( 2 , 7 ) ) ;
0 commit comments