Skip to content

Commit dc5301b

Browse files
fix: stop polling a rollout with a cut last line and keep spawns in file order
Second review round on list row usage: - A spawn scan that ended on a last line without its newline counted as unfinished, so a rollout cut by a crash was read again every 250 ms for the life of the process. Only a scan that the 8 MB budget ended goes on by itself now; a cut line is read when the file changes. - Spawns of both record formats in one chunk are taken in file order. - The limiter of subagent reads hands a finished slot to the next waiting read, so a new caller cannot run beyond the limit. - Document that a usage read reaches a subscription only for rows the agent has for its cwd. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 503a3ce commit dc5301b

4 files changed

Lines changed: 36 additions & 18 deletions

File tree

‎docs/air-extensions.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,8 +1081,10 @@ The adapter sends no `cost`: Codex reports none.
10811081
subagents found so far, and the row is sent again when it finds more.
10821082
- The adapter reads usage in the background, so a list never waits for it. A row carries the usage read for its
10831083
thread; the first list of a thread has none, and a thread that changed keeps the usage of the last read until
1084-
the new one is done. A subscription gets the row again when its usage was read, see
1085-
[Session list subscription](#session-list-subscription). A thread whose `updatedAt` and rollout did not change is
1084+
the new one is done. When the usage of a thread is read, a subscription gets the row again if the agent has
1085+
that row for the subscription's cwd: one of its baseline rows or a row it sent or computed before, see
1086+
[Session list subscription](#session-list-subscription). Another thread, such as one from a later list page,
1087+
shows its usage with its next change or the next list. A thread whose `updatedAt` and rollout did not change is
10861088
not read again, and a rollout is read again only when its size or time changed. A thread read within 2 s of its
10871089
`updatedAt` is read once more after that, since `updatedAt` has whole seconds.
10881090
- The usage of a subagent is read again when its parent thread changes: a subagent that works while its parent

‎src/RolloutUsage.ts‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export interface SpawnScan {
187187
offset: number;
188188
/** The spawned thread ids, in the order of their first record. */
189189
threadIds: string[];
190-
/** The scan reached the end of the file. */
190+
/** The scan reached the end of the file, but for a last line without its newline. */
191191
done: boolean;
192192
}
193193

@@ -228,23 +228,27 @@ export async function scanSpawnedThreads(
228228
}
229229
// Only complete lines: the rest is read with the next chunk.
230230
const view = buffer.subarray(0, lastNewline + 1);
231+
// The lines with a marker, in file order, each once.
232+
const lineStarts = new Set<number>();
231233
for (const marker of SPAWN_MARKERS) {
232-
let at = view.indexOf(marker);
233-
while (at >= 0) {
234-
const lineEnd = view.indexOf(NEWLINE, at);
235-
const line = view.toString("utf8", view.lastIndexOf(NEWLINE, at) + 1, lineEnd);
236-
for (const id of spawnedBy(line, threadId)) {
237-
if (seen.has(id)) continue;
238-
seen.add(id);
239-
scan.threadIds.push(id);
240-
}
241-
at = view.indexOf(marker, lineEnd + 1);
234+
for (let at = view.indexOf(marker); at >= 0; at = view.indexOf(marker, view.indexOf(NEWLINE, at) + 1)) {
235+
lineStarts.add(view.lastIndexOf(NEWLINE, at) + 1);
236+
}
237+
}
238+
for (const lineStart of [...lineStarts].sort((left, right) => left - right)) {
239+
const line = view.toString("utf8", lineStart, view.indexOf(NEWLINE, lineStart));
240+
for (const id of spawnedBy(line, threadId)) {
241+
if (seen.has(id)) continue;
242+
seen.add(id);
243+
scan.threadIds.push(id);
242244
}
243245
}
244246
position += view.length;
245247
scan.offset = position;
246248
}
247-
scan.done = scan.offset >= size;
249+
// Not done only when the budget ended the scan: a last line that Codex is still writing, or that a crash
250+
// cut, is read when the file changes, not by polling it.
251+
scan.done = !(position >= limit && limit < size);
248252
return scan;
249253
} finally {
250254
await handle.close();

‎src/SessionUsage.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,15 @@ function limiter(limit: number): <T>(task: () => Promise<T>) => Promise<T> {
9292
let running = 0;
9393
const waiting: Array<() => void> = [];
9494
return async <T>(task: () => Promise<T>): Promise<T> => {
95+
// A finished task hands its slot to the next waiting one, so a new caller cannot take it meanwhile.
9596
if (running >= limit) await new Promise<void>(resolve => waiting.push(resolve));
96-
running++;
97+
else running++;
9798
try {
9899
return await task();
99100
} finally {
100-
running--;
101-
waiting.shift()?.();
101+
const next = waiting.shift();
102+
if (next !== undefined) next();
103+
else running--;
102104
}
103105
};
104106
}

‎src/__tests__/SessionUsage.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ describe("rollout reads", () => {
122122
expect(scan.threadIds).toEqual([OTHER, CHILD]);
123123
});
124124

125+
it("keeps the file order of spawns of both formats within one chunk", async () => {
126+
const collab = JSON.stringify({type: "event_msg", payload: {type: "item_completed", thread_id: PARENT, item: {
127+
type: "CollabAgentToolCall", tool: "spawn_agent", sender_thread_id: PARENT, receiver_thread_ids: [OTHER],
128+
}}});
129+
const file = write("mixed.jsonl", [collab, spawn(PARENT, CHILD)]);
130+
131+
expect((await scanSpawnedThreads(file, PARENT, null, 1024 * 1024)).threadIds).toEqual([OTHER, CHILD]);
132+
});
133+
125134
it("reads the first token_count of a rollout past long lines", async () => {
126135
const file = write("fork.jsonl", [
127136
filler(700 * 1024),
@@ -150,7 +159,8 @@ describe("rollout reads", () => {
150159
const record = spawn(PARENT, OTHER);
151160
fs.appendFileSync(file, record.slice(0, 50));
152161
const partial = await scanSpawnedThreads(file, PARENT, first, 64 * 1024 * 1024);
153-
expect(partial).toEqual({...first, done: false});
162+
// Done all the same: a cut last line is read when the file changes, not polled.
163+
expect(partial).toEqual(first);
154164
fs.appendFileSync(file, record.slice(50) + "\n");
155165
expect((await scanSpawnedThreads(file, PARENT, partial, 64 * 1024 * 1024)).threadIds).toEqual([CHILD, OTHER]);
156166
});

0 commit comments

Comments
 (0)