Skip to content

Commit 9753875

Browse files
fix: bound usage reads after close and read for the newest listed thread
Per-commit review findings on list row usage: - A connection closed during a batch still started the rest of the batch and the waiting subagent reads; both now stop. - A read whose subagents took long decided on its re-read from when they were done, so a token count of the same second could stay unread; it now decides from when the rollout is read. - A scan continuation scheduled with an older thread could replace a newer pending read of the same thread, such as one after an archive moved its rollout; reads now use the newest thread listed. - Fork origins are kept for listed threads only and dropped with their usage, so the cache is bounded again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9be957f commit 9753875

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

‎src/SessionUsage.ts‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

‎src/__tests__/SessionUsage.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,23 @@ describe("SessionUsageIndex", () => {
349349
expect(onRead).toHaveBeenCalledTimes(1);
350350
});
351351

352+
it("starts no more reads of a batch after dispose", async () => {
353+
const files = Array.from({length: 20}, (_, index) => write(`t${index}.jsonl`, [tokenCount({input: index, output: 1})]));
354+
const {index} = createIndex(new Map());
355+
const realOpen = fs.promises.open.bind(fs.promises);
356+
// The connection closes as the first read starts.
357+
const open = vi.spyOn(fs.promises, "open").mockImplementation(async (...args: Parameters<typeof fs.promises.open>) => {
358+
index.dispose();
359+
return await realOpen(...args);
360+
});
361+
files.forEach((file, position) => index.usageOf({thread: thread(`01a0f439-251d-74a1-b2e7-${String(position).padStart(12, "0")}`, file), archived: false}));
362+
await new Promise(resolve => setTimeout(resolve, 200));
363+
364+
// Fork origin, tail and spawn scan of at most the first eight threads.
365+
expect(open.mock.calls.length).toBeLessThanOrEqual(8 * 3);
366+
open.mockRestore();
367+
});
368+
352369
it("reads nothing after dispose", async () => {
353370
vi.useFakeTimers();
354371
const file = write("a.jsonl", [tokenCount({input: 10, output: 1})]);

0 commit comments

Comments
 (0)