@@ -16,13 +16,14 @@ import type {Thread} from "./app-server/v2";
1616import { logger } from "./Logger" ;
1717import {
1818 minus ,
19- readFirstTokenCount ,
19+ readForkStart ,
2020 readLastTokenTotal ,
2121 scanSpawnedThreads ,
2222 type RawTokens ,
2323 type SessionUsageTokens ,
2424 type SpawnScan ,
2525 usageTokens ,
26+ ZERO_TOKENS ,
2627} from "./RolloutUsage" ;
2728
2829export type { SessionUsageTokens } from "./RolloutUsage" ;
@@ -82,7 +83,6 @@ interface CachedFileTokens {
8283interface Subagent {
8384 path : string ;
8485 model : string | null ;
85- forked : boolean ;
8686 /** The stamp of the rollout when the thread was read: a subagent whose rollout changed is read again. */
8787 stamp : string | null ;
8888}
@@ -241,7 +241,7 @@ export class SessionUsageIndex {
241241 let usage : SessionUsage | null = null ;
242242 if ( thread . path !== null ) {
243243 try {
244- const own = await this . tokensOf ( thread . id , thread . path , thread . forkedFromId !== null ) ;
244+ const own = await this . tokensOf ( thread . id , thread . path ) ;
245245 if ( own !== null ) {
246246 usage = { ...own , model : thread . model , subagents : await this . subagentUsages ( subject , thread . path ) } ;
247247 }
@@ -301,7 +301,7 @@ export class SessionUsageIndex {
301301 child = { ...child , stamp} ;
302302 remember ( this . subagents , childId , child ) ;
303303 }
304- const tokens = await this . tokensOf ( childId , child . path , child . forked ) ;
304+ const tokens = await this . tokensOf ( childId , child . path ) ;
305305 return tokens === null ? null : { sessionId : childId , model : child . model , ...tokens } ;
306306 }
307307
@@ -312,7 +312,7 @@ export class SessionUsageIndex {
312312 this . subagents . delete ( threadId ) ;
313313 return null ;
314314 }
315- const subagent : Subagent = { path : thread . path , model : thread . model , forked : thread . forkedFromId !== null , stamp : null } ;
315+ const subagent : Subagent = { path : thread . path , model : thread . model , stamp : null } ;
316316 remember ( this . subagents , threadId , subagent ) ;
317317 return subagent ;
318318 }
@@ -321,26 +321,28 @@ export class SessionUsageIndex {
321321 * The own usage of a thread from its rollout, or `null` when it has no `token_count`. The rollout is read
322322 * again only when its path, size or modification time changed.
323323 */
324- private async tokensOf ( threadId : string , file : string , forked : boolean ) : Promise < SessionUsageTokens | null > {
324+ private async tokensOf ( threadId : string , file : string ) : Promise < SessionUsageTokens | null > {
325325 const stamp = await stampOf ( file ) ;
326326 const cached = this . fileTokens . get ( threadId ) ;
327327 if ( cached !== undefined && cached . stamp === stamp ) {
328328 remember ( this . fileTokens , threadId , cached ) ;
329329 return cached . tokens ;
330330 }
331- const tokens = await this . readTokens ( threadId , file , forked ) ;
331+ const tokens = await this . readTokens ( threadId , file ) ;
332332 remember ( this . fileTokens , threadId , { stamp, tokens} ) ;
333333 return tokens ;
334334 }
335335
336- private async readTokens ( threadId : string , file : string , forked : boolean ) : Promise < SessionUsageTokens | null > {
336+ private async readTokens ( threadId : string , file : string ) : Promise < SessionUsageTokens | null > {
337337 const total = await readLastTokenTotal ( file ) ;
338338 if ( total === null ) return null ;
339- if ( ! forked ) return usageTokens ( total ) ;
340339 let inherited = this . inherited . get ( threadId ) ;
341340 if ( inherited === undefined ) {
342- const first = await readFirstTokenCount ( file ) ;
343- inherited = first === null ? null : first . last === null ? first . total : minus ( first . total , first . last ) ;
341+ // A fork tells from its own session_meta: `thread/list` leaves out `Thread.forkedFromId`.
342+ const start = await readForkStart ( file ) ;
343+ inherited = start === "notForked"
344+ ? ZERO_TOKENS
345+ : start === null ? null : start . last === null ? start . total : minus ( start . total , start . last ) ;
344346 remember ( this . inherited , threadId , inherited ) ;
345347 }
346348 // A fork whose first record is beyond the read start of its rollout: its own part is unknown.
0 commit comments