Skip to content

Commit 5241591

Browse files
fix: keep a fork origin only with its usage and skip a child rollout that went
Per-commit review findings on list row usage: - A read that failed after reading the fork origin left the origin cached without its usage, beyond the bound of the usage cache; the origin is now kept only with a stored usage, and passed to the token read instead of being read twice. - A subagent rollout that was archived or deleted between its check and its read failed the read of the parent; that subagent is now left out and read again with the next read of the parent. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8662b37 commit 5241591

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

‎src/SessionUsage.ts‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,11 @@ export class SessionUsageIndex {
263263
if (thread.path !== null) {
264264
try {
265265
if (origin === undefined) {
266+
// Kept with the usage below, so a read that fails leaves none behind.
266267
origin = await readForkOrigin(thread.path);
267-
if (origin !== undefined) {
268-
this.origins.set(thread.id, origin);
269-
originRead = origin !== null;
270-
}
268+
originRead = origin !== undefined && origin !== null;
271269
}
272-
const own = await this.tokensOf(thread.id, thread.path);
270+
const own = await this.tokensOf(thread.id, thread.path, origin);
273271
if (own !== null) {
274272
usage = {...own, model: thread.model, subagents: await this.subagentUsages(subject, thread.path)};
275273
}
@@ -337,7 +335,15 @@ export class SessionUsageIndex {
337335
child = {...child, stamp};
338336
remember(this.subagents, childId, child);
339337
}
340-
const tokens = await this.tokensOf(childId, child.path);
338+
let tokens: SessionUsageTokens | null;
339+
try {
340+
tokens = await this.tokensOf(childId, child.path);
341+
} catch (error) {
342+
// Moved or gone between the check and the read: read it with the next read of its parent.
343+
if (!isMissingFile(error)) throw error;
344+
this.subagents.delete(childId);
345+
return null;
346+
}
341347
return tokens === null ? null : {sessionId: childId, model: child.model, ...tokens};
342348
}
343349

@@ -357,25 +363,25 @@ export class SessionUsageIndex {
357363
* The own usage of a thread from its rollout, or `null` when it has no `token_count`. The rollout is read
358364
* again only when its path, size or modification time changed.
359365
*/
360-
private async tokensOf(threadId: string, file: string): Promise<SessionUsageTokens | null> {
366+
private async tokensOf(threadId: string, file: string, knownOrigin?: string | null): Promise<SessionUsageTokens | null> {
361367
const stamp = await stampOf(file);
362368
const cached = this.fileTokens.get(threadId);
363369
if (cached !== undefined && cached.stamp === stamp) {
364370
remember(this.fileTokens, threadId, cached);
365371
return cached.tokens;
366372
}
367-
const tokens = await this.readTokens(threadId, file);
373+
const tokens = await this.readTokens(threadId, file, knownOrigin);
368374
remember(this.fileTokens, threadId, {stamp, tokens});
369375
return tokens;
370376
}
371377

372-
private async readTokens(threadId: string, file: string): Promise<SessionUsageTokens | null> {
378+
private async readTokens(threadId: string, file: string, knownOrigin?: string | null): Promise<SessionUsageTokens | null> {
373379
const total = await readLastTokenTotal(file);
374380
if (total === null) return null;
375381
let inherited = this.inherited.get(threadId);
376382
if (inherited === undefined) {
377383
// A fork tells from its own session_meta: `thread/list` leaves out `Thread.forkedFromId`.
378-
let origin = this.origins.get(threadId);
384+
let origin = knownOrigin !== undefined ? knownOrigin : this.origins.get(threadId);
379385
if (origin === undefined) {
380386
const read = await readForkOrigin(file);
381387
if (read === undefined) return null;

0 commit comments

Comments
 (0)