@@ -122,8 +122,8 @@ export class SessionUsageIndex {
122122 private readonly fileTokens = new Map < string , CachedFileTokens > ( ) ;
123123 private readonly inherited = new Map < string , RawTokens | null > ( ) ;
124124 /**
125- * The thread each rollout was forked from, or `null` for no fork: `thread/list` leaves `forkedFromId` out. Not
126- * evicted, unlike the other caches: a row must not lose its `forkedFrom` while its usage is current.
125+ * The thread each listed thread was forked from, or `null` for no fork: `thread/list` leaves `forkedFromId` out.
126+ * Dropped with the usage of the thread, so a row does not lose its `forkedFrom` while its usage is current.
127127 */
128128 private readonly origins = new Map < string , string | null > ( ) ;
129129 private readonly spawnScans = new Map < string , SpawnScan > ( ) ;
@@ -138,6 +138,8 @@ export class SessionUsageIndex {
138138 private disposed = false ;
139139 private readonly nowSeconds : ( ) => number ;
140140 private readonly subagentReads = limiter ( READ_CONCURRENCY ) ;
141+ /** The newest thread that each thread was listed as: a later read uses it, not the one it was scheduled for. */
142+ private readonly latest = new Map < string , UsageSubject > ( ) ;
141143
142144 constructor ( private readonly deps : SessionUsageIndexDeps ) {
143145 this . nowSeconds = deps . nowSeconds ?? ( ( ) => Date . now ( ) / 1000 ) ;
@@ -151,6 +153,7 @@ export class SessionUsageIndex {
151153 const cached = this . usages . get ( subject . thread . id ) ;
152154 const current = cached !== undefined && cached . updatedAt === subject . thread . updatedAt
153155 && cached . path === subject . thread . path ;
156+ remember ( this . latest , subject . thread . id , subject ) ;
154157 if ( ! current || ( cached . recheckAt !== null && this . nowSeconds ( ) >= cached . recheckAt ) ) this . schedule ( subject ) ;
155158 // The model is the thread's latest, which can change without a new token count.
156159 return cached ?. usage ? { ...cached . usage , model : subject . thread . model } : cached ?. usage ;
@@ -214,8 +217,9 @@ export class SessionUsageIndex {
214217 this . laterTimer . unref ?.( ) ;
215218 }
216219
217- private schedule ( subject : UsageSubject ) : void {
220+ private schedule ( scheduled : UsageSubject ) : void {
218221 if ( this . disposed ) return ;
222+ const subject = this . latest . get ( scheduled . thread . id ) ?? scheduled ;
219223 if ( this . later . delete ( subject . thread . id ) ) this . armLater ( ) ;
220224 this . pending . set ( subject . thread . id , subject ) ;
221225 if ( this . timer !== null || this . running ) return ;
@@ -233,7 +237,7 @@ export class SessionUsageIndex {
233237 const batch = [ ...this . pending . values ( ) ] ;
234238 this . pending . clear ( ) ;
235239 const changed : UsageSubject [ ] = [ ] ;
236- for ( let start = 0 ; start < batch . length ; start += READ_CONCURRENCY ) {
240+ for ( let start = 0 ; start < batch . length && ! this . disposed ; start += READ_CONCURRENCY ) {
237241 await Promise . all ( batch . slice ( start , start + READ_CONCURRENCY ) . map ( async ( subject ) => {
238242 if ( await this . read ( subject ) ) changed . push ( subject ) ;
239243 } ) ) ;
@@ -250,6 +254,9 @@ export class SessionUsageIndex {
250254 /** Reads the usage of a thread; true when it differs from the usage known before. */
251255 private async read ( subject : UsageSubject ) : Promise < boolean > {
252256 const { thread} = subject ;
257+ // Whether a record can still come within the second of `updatedAt` is up to when the rollout is read,
258+ // not when the subagents are done.
259+ const recheckAt = this . recheckTime ( thread ) ;
253260 let usage : SessionUsage | null = null ;
254261 let originRead = false ;
255262 if ( thread . path !== null ) {
@@ -272,8 +279,9 @@ export class SessionUsageIndex {
272279 }
273280 }
274281 const before = this . usages . get ( thread . id ) ;
275- const recheckAt = this . recheckTime ( thread ) ;
276- remember ( this . usages , thread . id , { updatedAt : thread . updatedAt , path : thread . path , usage, recheckAt} ) ;
282+ const evicted = remember ( this . usages , thread . id , { updatedAt : thread . updatedAt , path : thread . path , usage, recheckAt} ) ;
283+ // The fork origin of a row lives as long as its usage.
284+ if ( evicted !== undefined ) this . origins . delete ( evicted ) ;
277285 // Another record can come within the second of `updatedAt` without moving it: read once more after it.
278286 if ( recheckAt !== null ) this . readLater ( subject , Math . max ( 0 , recheckAt - this . nowSeconds ( ) ) * 1000 ) ;
279287 return originRead || ( before === undefined ? usage !== null : JSON . stringify ( before . usage ) !== JSON . stringify ( usage ) ) ;
@@ -288,7 +296,10 @@ export class SessionUsageIndex {
288296 const scan = await scanSpawnedThreads ( file , threadId , this . spawnScans . get ( threadId ) ?? null , SPAWN_SCAN_BYTES ) ;
289297 remember ( this . spawnScans , threadId , scan ) ;
290298 if ( ! scan . done ) this . readLater ( subject , SPAWN_SCAN_PAUSE_MS ) ;
291- const usages = await Promise . all ( scan . threadIds . map ( childId => this . subagentReads ( ( ) => this . subagentUsage ( childId ) ) ) ) ;
299+ // A read that waits for its turn after the connection closed does nothing.
300+ const usages = await Promise . all ( scan . threadIds . map ( childId => this . subagentReads (
301+ async ( ) => this . disposed ? null : await this . subagentUsage ( childId ) ,
302+ ) ) ) ;
292303 return usages . filter ( ( usage ) : usage is SubagentUsage => usage !== null ) ;
293304 }
294305
@@ -364,7 +375,6 @@ export class SessionUsageIndex {
364375 const read = await readForkOrigin ( file ) ;
365376 if ( read === undefined ) return null ;
366377 origin = read ;
367- this . origins . set ( threadId , origin ) ;
368378 }
369379 const first = origin === null ? null : await readFirstTokenCount ( file ) ;
370380 inherited = origin === null
@@ -393,12 +403,12 @@ async function stampOf(file: string): Promise<string> {
393403 return `${ file } :${ stats . size } :${ stats . mtimeMs } ` ;
394404}
395405
396- /** Keeps a value, most recent last, and drops the oldest beyond {@link MAX_CACHED}. */
397- function remember < T > ( cache : Map < string , T > , key : string , value : T ) : void {
406+ /** Keeps a value, most recent last, and drops the oldest beyond {@link MAX_CACHED}; returns the dropped key . */
407+ function remember < T > ( cache : Map < string , T > , key : string , value : T ) : string | undefined {
398408 cache . delete ( key ) ;
399409 cache . set ( key , value ) ;
400- if ( cache . size > MAX_CACHED ) {
401- const oldest = cache . keys ( ) . next ( ) . value ;
402- if ( oldest !== undefined ) cache . delete ( oldest ) ;
403- }
410+ if ( cache . size <= MAX_CACHED ) return undefined ;
411+ const oldest = cache . keys ( ) . next ( ) . value ;
412+ if ( oldest !== undefined ) cache . delete ( oldest ) ;
413+ return oldest ;
404414}
0 commit comments