Skip to content

Commit ae8fbeb

Browse files
fix: take forkedFrom from the rollout and the usage model from the thread
Third review round: - thread/list answers forkedFromId null and thread/read does not, so a fork's row changed between the two and sent false changes. forkedFrom is now the forked_from_id of the rollout's session_meta, read once with the usage, for list and subscription rows alike; the first line is read in 64 KB steps. - usage.model follows the thread's current model, which can change without a new token count. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0ae1353 commit ae8fbeb

6 files changed

Lines changed: 101 additions & 30 deletions

File tree

‎docs/air-extensions.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ Every row carries these fields in `_meta.jetbrains.air`; all but `archived` only
10441044
| `archived` | `true` for a thread from the archived Codex list, `false` otherwise, whatever the `archived` filter. Always present. |
10451045
| `lastPromptAt` | ISO time of `Thread.recencyAt`: Codex moves it when a turn starts and orders threads by it. |
10461046
| `model` | `Thread.model`. |
1047-
| `forkedFrom` | `Thread.forkedFromId`. |
1047+
| `forkedFrom` | The thread it was forked from: `forked_from_id` of the `session_meta` line of its rollout, which the adapter reads with the usage, see [Token usage](#token-usage). Until then `Thread.forkedFromId`, which `thread/read` answers and `thread/list` does not. |
10481048
| `state` | Only for a thread that this adapter has loaded: `running`, `requires_action` (waiting for an approval or for user input), or `idle`. Omitted otherwise, never `unknown`. |
10491049
| `lastTurnEndedAt` | ISO time of the last `turn/completed` that this adapter saw for a session of this connection. Forgotten when the thread is deleted. |
10501050
| `usage` | The token usage of the thread, see [Token usage](#token-usage). Omitted until the adapter has read it, and for a thread whose rollout has no token count. |
@@ -1065,7 +1065,7 @@ The adapter sends no `cost`: Codex reports none.
10651065
- `inputTokens` is fresh input only: Codex's input minus the cache reads and the cache writes, at least 0, as
10661066
`PromptResponse.usage` counts it. `cachedReadTokens` and `cachedWriteTokens` are the cache reads and writes.
10671067
`outputTokens` includes `reasoningTokens`.
1068-
- `model` is `Thread.model`, the thread's latest model.
1068+
- `model` is `Thread.model`, the thread's latest model, the same as the row's `model`.
10691069
- A fork counts only its own work. A rollout whose first line, `session_meta`, names a `forked_from_id` is a
10701070
fork; `Thread.forkedFromId` does not tell, as `thread/list` leaves it out. The first record of a fork carries
10711071
the total of the history it inherited plus the usage of its own first request, so the adapter subtracts that

‎src/RolloutUsage.ts‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,33 +132,43 @@ export async function readLastTokenTotal(file: string): Promise<RawTokens | null
132132

133133
export const ZERO_TOKENS: Readonly<RawTokens> = Object.freeze({input: 0, cached: 0, cacheWrite: 0, output: 0, reasoning: 0});
134134

135-
/** How much of the start of a rollout is read for its `session_meta` line. */
136-
const SESSION_META_LIMIT = 1024 * 1024;
135+
/** How much of the start of a rollout is read for its `session_meta` line, and in what steps. */
136+
const SESSION_META_LIMIT = 4 * 1024 * 1024;
137+
const SESSION_META_STEP = 64 * 1024;
137138

138139
/**
139-
* `"notForked"` for a rollout whose `session_meta`, its first line, has no `forked_from_id`; otherwise the
140-
* first `token_count` of the fork, see {@link readFirstTokenCount}.
140+
* The thread that a rollout was forked from: `forked_from_id` of its `session_meta`, the first line, or `null`
141+
* for a rollout that is no fork. `undefined` while that line is not complete yet.
141142
*/
142-
export async function readForkStart(file: string): Promise<{total: RawTokens, last: RawTokens | null} | "notForked" | null> {
143+
export async function readForkOrigin(file: string): Promise<string | null | undefined> {
143144
const handle = await fs.open(file, "r");
144-
let firstLine: string;
145+
let firstLine: string | null = null;
145146
try {
146-
const buffer = Buffer.alloc(SESSION_META_LIMIT);
147-
const {bytesRead} = await handle.read(buffer, 0, SESSION_META_LIMIT, 0);
148-
const end = buffer.subarray(0, bytesRead).indexOf(NEWLINE);
149-
firstLine = buffer.toString("utf8", 0, end < 0 ? bytesRead : end);
147+
const chunks: Buffer[] = [];
148+
for (let position = 0; position < SESSION_META_LIMIT;) {
149+
const chunk = Buffer.alloc(SESSION_META_STEP);
150+
const {bytesRead} = await handle.read(chunk, 0, SESSION_META_STEP, position);
151+
if (bytesRead === 0) break;
152+
const end = chunk.subarray(0, bytesRead).indexOf(NEWLINE);
153+
chunks.push(chunk.subarray(0, end < 0 ? bytesRead : end));
154+
if (end >= 0) {
155+
firstLine = Buffer.concat(chunks).toString("utf8");
156+
break;
157+
}
158+
position += bytesRead;
159+
}
150160
} finally {
151161
await handle.close();
152162
}
163+
if (firstLine === null) return undefined;
153164
let meta: unknown;
154165
try {
155166
meta = JSON.parse(firstLine);
156167
} catch {
157-
return "notForked";
168+
return null;
158169
}
159170
const forkedFrom = field(field(meta, "payload"), "forked_from_id");
160-
if (field(meta, "type") !== "session_meta" || typeof forkedFrom !== "string" || forkedFrom === "") return "notForked";
161-
return await readFirstTokenCount(file);
171+
return field(meta, "type") === "session_meta" && typeof forkedFrom === "string" && forkedFrom !== "" ? forkedFrom : null;
162172
}
163173

164174
/** The first `token_count` of the rollout, read from its start, or `null` within {@link HEAD_LIMIT}. */

‎src/SessionIndex.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,22 @@ export function sessionIndexSessionInfo(
458458
};
459459
}
460460

461-
/** A row with another usage, or without one for `null`. The rest of the row stays as it is. */
461+
/** A row with another `forkedFrom`, or without one for `null`. */
462+
export function withSessionIndexForkOrigin(row: acp.SessionInfo, forkedFrom: string | null): acp.SessionInfo {
463+
const jetbrains = asRecord(asRecord(row._meta)[JETBRAINS_META_KEY]);
464+
const air = asRecord(jetbrains[AIR_META_KEY]);
465+
if ((air[AIR_FORKED_FROM_KEY] ?? null) === forkedFrom) return row;
466+
const {[AIR_FORKED_FROM_KEY]: _dropped, ...rest} = air;
467+
const next = forkedFrom === null ? rest : {...rest, [AIR_FORKED_FROM_KEY]: forkedFrom};
468+
return {...row, _meta: {...asRecord(row._meta), [JETBRAINS_META_KEY]: {...jetbrains, [AIR_META_KEY]: next}}};
469+
}
470+
471+
/** A row with another usage, or without one for `null`. The usage `model` is the row's `model`. */
462472
export function withSessionIndexUsage(row: acp.SessionInfo, usage: SessionUsage | null): acp.SessionInfo {
463-
if (usage !== null) return {...row, _meta: withAirMeta(row._meta, AIR_USAGE_KEY, usage)};
473+
if (usage !== null) {
474+
const model = asRecord(asRecord(asRecord(row._meta)[JETBRAINS_META_KEY])[AIR_META_KEY])[AIR_MODEL_KEY];
475+
return {...row, _meta: withAirMeta(row._meta, AIR_USAGE_KEY, {...usage, model: typeof model === "string" ? model : null})};
476+
}
464477
const jetbrains = asRecord(asRecord(row._meta)[JETBRAINS_META_KEY]);
465478
const air = asRecord(jetbrains[AIR_META_KEY]);
466479
if (!(AIR_USAGE_KEY in air)) return row;

‎src/SessionIndexService.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
SESSION_UNARCHIVE_METHOD,
3333
SessionIndexActivity,
3434
sessionIndexSessionInfo,
35+
withSessionIndexForkOrigin,
3536
withSessionIndexUsage,
3637
type SessionArchiveRequest,
3738
type SessionIndexListOptions,
@@ -97,7 +98,9 @@ export class SessionIndexService {
9798
rows: async (entries) => await this.rows(entries),
9899
withLatestUsage: (row) => {
99100
const usage = this.usage.latestUsage(row.sessionId);
100-
return usage === undefined ? row : withSessionIndexUsage(row, usage);
101+
const origin = this.usage.forkOrigin(row.sessionId);
102+
const withOrigin = origin === undefined ? row : withSessionIndexForkOrigin(row, origin);
103+
return usage === undefined ? withOrigin : withSessionIndexUsage(withOrigin, usage);
101104
},
102105
scopeCwds: (cwd) => linkedWorktreeCwds(cwd),
103106
notify: async (changes) => {
@@ -295,12 +298,16 @@ export class SessionIndexService {
295298
* usage known for its thread; one that is not read yet is read in the background, see `SessionUsageIndex`.
296299
*/
297300
private async rows(entries: ThreadEntry[]): Promise<acp.SessionInfo[]> {
298-
return entries.map(entry => this.withActiveAdditionalDirectories(sessionIndexSessionInfo(
299-
entry.thread,
300-
entry.archived,
301-
this.activity.activityOf(entry.thread),
302-
this.usage.usageOf(entry) ?? null,
303-
)));
301+
return entries.map(entry => {
302+
const usage = this.usage.usageOf(entry) ?? null;
303+
// The fork origin comes from the rollout once it is read with the usage, for every row alike:
304+
// `thread/list` answers no `forkedFromId`, `thread/read` does.
305+
const origin = this.usage.forkOrigin(entry.thread.id);
306+
const thread = origin === undefined ? entry.thread : {...entry.thread, forkedFromId: origin};
307+
return this.withActiveAdditionalDirectories(
308+
sessionIndexSessionInfo(thread, entry.archived, this.activity.activityOf(thread), usage),
309+
);
310+
});
304311
}
305312

306313
private withActiveAdditionalDirectories(session: acp.SessionInfo): acp.SessionInfo {

‎src/SessionUsage.ts‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import type {Thread} from "./app-server/v2";
1616
import {logger} from "./Logger";
1717
import {
1818
minus,
19-
readForkStart,
19+
readFirstTokenCount,
20+
readForkOrigin,
2021
readLastTokenTotal,
2122
scanSpawnedThreads,
2223
type RawTokens,
@@ -120,6 +121,8 @@ export class SessionUsageIndex {
120121
private readonly usages = new Map<string, CachedUsage>();
121122
private readonly fileTokens = new Map<string, CachedFileTokens>();
122123
private readonly inherited = new Map<string, RawTokens | null>();
124+
/** The thread each rollout was forked from, or `null` for no fork: `thread/list` leaves `forkedFromId` out. */
125+
private readonly origins = new Map<string, string | null>();
123126
private readonly spawnScans = new Map<string, SpawnScan>();
124127
private readonly subagents = new Map<string, Subagent>();
125128
private readonly pending = new Map<string, UsageSubject>();
@@ -146,7 +149,13 @@ export class SessionUsageIndex {
146149
const current = cached !== undefined && cached.updatedAt === subject.thread.updatedAt
147150
&& cached.path === subject.thread.path;
148151
if (!current || (cached.recheckAt !== null && this.nowSeconds() >= cached.recheckAt)) this.schedule(subject);
149-
return cached?.usage;
152+
// The model is the thread's latest, which can change without a new token count.
153+
return cached?.usage ? {...cached.usage, model: subject.thread.model} : cached?.usage;
154+
}
155+
156+
/** The thread that a thread was forked from, `null` for no fork, `undefined` before its rollout was read. */
157+
forkOrigin(threadId: string): string | null | undefined {
158+
return this.origins.get(threadId);
150159
}
151160

152161
/** The usage of the last read of a thread, whatever thread it was read for; `undefined` before any. */
@@ -239,8 +248,16 @@ export class SessionUsageIndex {
239248
private async read(subject: UsageSubject): Promise<boolean> {
240249
const {thread} = subject;
241250
let usage: SessionUsage | null = null;
251+
let originRead = false;
242252
if (thread.path !== null) {
243253
try {
254+
if (!this.origins.has(thread.id)) {
255+
const origin = await readForkOrigin(thread.path);
256+
if (origin !== undefined) {
257+
remember(this.origins, thread.id, origin);
258+
originRead = origin !== null;
259+
}
260+
}
244261
const own = await this.tokensOf(thread.id, thread.path);
245262
if (own !== null) {
246263
usage = {...own, model: thread.model, subagents: await this.subagentUsages(subject, thread.path)};
@@ -256,7 +273,7 @@ export class SessionUsageIndex {
256273
remember(this.usages, thread.id, {updatedAt: thread.updatedAt, path: thread.path, usage, recheckAt});
257274
// Another record can come within the second of `updatedAt` without moving it: read once more after it.
258275
if (recheckAt !== null) this.readLater(subject, Math.max(0, recheckAt - this.nowSeconds()) * 1000);
259-
return before === undefined ? usage !== null : JSON.stringify(before.usage) !== JSON.stringify(usage);
276+
return originRead || (before === undefined ? usage !== null : JSON.stringify(before.usage) !== JSON.stringify(usage));
260277
}
261278

262279
/**
@@ -339,10 +356,17 @@ export class SessionUsageIndex {
339356
let inherited = this.inherited.get(threadId);
340357
if (inherited === undefined) {
341358
// A fork tells from its own session_meta: `thread/list` leaves out `Thread.forkedFromId`.
342-
const start = await readForkStart(file);
343-
inherited = start === "notForked"
359+
let origin = this.origins.get(threadId);
360+
if (origin === undefined) {
361+
const read = await readForkOrigin(file);
362+
if (read === undefined) return null;
363+
origin = read;
364+
remember(this.origins, threadId, origin);
365+
}
366+
const first = origin === null ? null : await readFirstTokenCount(file);
367+
inherited = origin === null
344368
? ZERO_TOKENS
345-
: start === null ? null : start.last === null ? start.total : minus(start.total, start.last);
369+
: first === null ? null : first.last === null ? first.total : minus(first.total, first.last);
346370
remember(this.inherited, threadId, inherited);
347371
}
348372
// A fork whose first record is beyond the read start of its rollout: its own part is unknown.

‎src/__tests__/SessionUsage.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,23 @@ describe("SessionUsageIndex", () => {
321321
expect(index.usageOf(changed)).toMatchObject({inputTokens: 20, subagents: [{sessionId: CHILD, inputTokens: 40}]});
322322
});
323323

324+
it("reads the fork origin from the rollout and gives the usage the thread's current model", async () => {
325+
const file = write("fork.jsonl", [
326+
JSON.stringify({type: "session_meta", payload: {id: PARENT, forked_from_id: OTHER}}),
327+
tokenCount({input: 10, output: 1}),
328+
]);
329+
const {index, onRead} = createIndex(new Map());
330+
const subject = {thread: thread(PARENT, file), archived: false};
331+
expect(index.forkOrigin(PARENT)).toBeUndefined();
332+
index.usageOf(subject);
333+
await settle(index);
334+
335+
expect(index.forkOrigin(PARENT)).toBe(OTHER);
336+
expect(onRead).toHaveBeenCalledTimes(1);
337+
expect(index.usageOf({thread: thread(PARENT, file, {model: "gpt-6"}), archived: false})).toMatchObject({model: "gpt-6"});
338+
expect(index.busy()).toBe(false);
339+
});
340+
324341
it("reports no change for a read that gives the usage it had", async () => {
325342
const file = write("a.jsonl", [tokenCount({input: 10, output: 1})]);
326343
const {index, onRead} = createIndex(new Map());

0 commit comments

Comments
 (0)