@@ -32,9 +32,9 @@ function claudeLine(overrides: {
3232
3333describe ( "parseClaudeLine" , ( ) => {
3434 it ( "extracts token totals and a dedupe key" , ( ) => {
35- const record = parseClaudeLine ( claudeLine ( { messageId : "msg_1" , contentType : "text" } ) ) ;
35+ const [ record ] = parseClaudeLine ( claudeLine ( { messageId : "msg_1" , contentType : "text" } ) ) ;
3636
37- expect ( record ) . not . toBeNull ( ) ;
37+ expect ( record ) . toBeDefined ( ) ;
3838 expect ( record ?. provider ) . toBe ( "claude" ) ;
3939 expect ( record ?. model ) . toBe ( "claude-fable-5" ) ;
4040 expect ( record ?. totals ) . toEqual ( {
@@ -50,8 +50,8 @@ describe("parseClaudeLine", () => {
5050
5151 it ( "gives every content block of one message the same dedupe key" , ( ) => {
5252 // Content-block records repeat full parent usage; summing them overcounts.
53- const text = parseClaudeLine ( claudeLine ( { messageId : "msg_2" , contentType : "text" } ) ) ;
54- const toolUse = parseClaudeLine ( claudeLine ( { messageId : "msg_2" , contentType : "tool_use" } ) ) ;
53+ const [ text ] = parseClaudeLine ( claudeLine ( { messageId : "msg_2" , contentType : "text" } ) ) ;
54+ const [ toolUse ] = parseClaudeLine ( claudeLine ( { messageId : "msg_2" , contentType : "tool_use" } ) ) ;
5555
5656 expect ( text ?. dedupeKey ) . toBe ( toolUse ?. dedupeKey ) ;
5757 expect ( text ?. totals ) . toEqual ( toolUse ?. totals ) ;
@@ -86,3 +86,133 @@ describe("parseClaudeLine", () => {
8686 expect ( claudeUsageFormat . parseProjected ( synthetic , undefined ) ) . toEqual ( [ ] ) ;
8787 } ) ;
8888} ) ;
89+
90+ describe ( "Claude advisor iterations" , ( ) => {
91+ const advisorTurn = {
92+ type : "assistant" ,
93+ timestamp : "2026-10-22T01:31:11.984Z" ,
94+ sessionId : "5a128faa-8253-489e-b935-6c08e8e670c0" ,
95+ requestId : "req_1" ,
96+ message : {
97+ id : "msg_1" ,
98+ model : "claude-opus-5-5" ,
99+ usage : {
100+ input_tokens : 4 ,
101+ cache_read_input_tokens : 118320 ,
102+ cache_creation_input_tokens : 2676 ,
103+ output_tokens : 4506 ,
104+ speed : "standard" ,
105+ iterations : [
106+ {
107+ type : "message" ,
108+ input_tokens : 2 ,
109+ cache_read_input_tokens : 58352 ,
110+ cache_creation_input_tokens : 1616 ,
111+ output_tokens : 141 ,
112+ } ,
113+ {
114+ type : "advisor_message" ,
115+ model : "claude-fable-5-1" ,
116+ input_tokens : 54204 ,
117+ cache_read_input_tokens : 0 ,
118+ cache_creation_input_tokens : 0 ,
119+ output_tokens : 4229 ,
120+ } ,
121+ {
122+ type : "message" ,
123+ input_tokens : 2 ,
124+ cache_read_input_tokens : 59968 ,
125+ cache_creation_input_tokens : 1060 ,
126+ output_tokens : 4365 ,
127+ } ,
128+ ] ,
129+ } ,
130+ } ,
131+ } ;
132+ const withIterations = ( iterations : readonly unknown [ ] ) => ( {
133+ ...advisorTurn ,
134+ message : { ...advisorTurn . message , usage : { ...advisorTurn . message . usage , iterations } } ,
135+ } ) ;
136+ const parse = ( turn : unknown ) => claudeUsageFormat . parseLine ( JSON . stringify ( turn ) , undefined ) ;
137+
138+ it ( "reports the advisor model's tokens as their own record beside the unchanged executor record" , ( ) => {
139+ const executor = {
140+ provider : "claude" ,
141+ timestampMs : Date . parse ( "2026-10-22T01:31:11.984Z" ) ,
142+ model : "claude-opus-5-5" ,
143+ sessionId : "5a128faa-8253-489e-b935-6c08e8e670c0" ,
144+ totals : {
145+ uncachedInputTokens : 4 ,
146+ cachedInputTokens : 118320 ,
147+ cacheCreationTokens : 2676 ,
148+ outputTokens : 4506 ,
149+ reasoningTokens : 0 ,
150+ } ,
151+ reportedCostUsd : null ,
152+ speed : "standard" ,
153+ dedupeKey : "msg_1:req_1" ,
154+ } ;
155+ const expected = [
156+ executor ,
157+ {
158+ ...executor ,
159+ model : "claude-fable-5-1" ,
160+ totals : {
161+ uncachedInputTokens : 54204 ,
162+ cachedInputTokens : 0 ,
163+ cacheCreationTokens : 0 ,
164+ outputTokens : 4229 ,
165+ reasoningTokens : 0 ,
166+ } ,
167+ dedupeKey : expect . any ( String ) ,
168+ } ,
169+ ] ;
170+
171+ expect ( parse ( advisorTurn ) ) . toEqual ( expected ) ;
172+ expect ( claudeUsageFormat . parseProjected ( advisorTurn , undefined ) ) . toEqual ( expected ) ;
173+ } ) ;
174+
175+ it ( "keys the advisor record apart from the executor and the same across a message's content blocks" , ( ) => {
176+ const block = ( type : string ) =>
177+ parse ( { ...advisorTurn , message : { ...advisorTurn . message , content : [ { type } ] } } ) ;
178+ const [ executorKey , advisorKey ] = block ( "text" ) . map ( ( record ) => record . dedupeKey ) ;
179+
180+ expect ( advisorKey ) . toEqual ( expect . any ( String ) ) ;
181+ expect ( advisorKey ) . not . toBe ( executorKey ) ;
182+ expect ( block ( "tool_use" ) . map ( ( record ) => record . dedupeKey ) ) . toEqual ( [ executorKey , advisorKey ] ) ;
183+
184+ const unkeyed = parse ( {
185+ ...advisorTurn ,
186+ requestId : undefined ,
187+ message : { ...advisorTurn . message , id : undefined } ,
188+ } ) ;
189+ expect ( unkeyed . map ( ( record ) => record . dedupeKey ) ) . toEqual ( [ null , null ] ) ;
190+ } ) ;
191+
192+ it ( "emits only the executor record without an advisor iteration" , ( ) => {
193+ const executorIterations = advisorTurn . message . usage . iterations . filter (
194+ ( iteration ) => iteration . type === "message" ,
195+ ) ;
196+
197+ expect ( parse ( withIterations ( executorIterations ) ) ) . toHaveLength ( 1 ) ;
198+ expect ( claudeUsageFormat . parseProjected ( withIterations ( [ ] ) , undefined ) ) . toHaveLength ( 1 ) ;
199+ expect (
200+ claudeUsageFormat . parseLine (
201+ claudeLine ( { messageId : "msg_3" , contentType : "text" } ) ,
202+ undefined ,
203+ ) ,
204+ ) . toHaveLength ( 1 ) ;
205+ } ) ;
206+
207+ it ( "ignores advisor iterations without a usable model and malformed entries" , ( ) => {
208+ const unusable = withIterations ( [
209+ null ,
210+ "advisor_message" ,
211+ { type : "advisor_message" , input_tokens : 5 } ,
212+ { type : "advisor_message" , model : "" , input_tokens : 5 } ,
213+ { type : "advisor_message" , model : "<synthetic>" , input_tokens : 5 } ,
214+ ] ) ;
215+
216+ expect ( parse ( unusable ) . map ( ( record ) => record . model ) ) . toEqual ( [ "claude-opus-5-5" ] ) ;
217+ } ) ;
218+ } ) ;
0 commit comments