Skip to content

Commit c5e0804

Browse files
committed
perf(usage): usage scans skip OpenCode legacy messages older than the window
Every usage scan read and parsed every file in OpenCode's pre-migration JSON store, even though records older than the window are dropped afterwards. Stat each legacy message first and skip the read when it was last written before the window opened, as the transcript reader already does. Skipped files are no longer returned, so they stop counting towards the source's skippedFiles. A store holding only old legacy files is still reported as present.
1 parent a6ec88f commit c5e0804

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎apps/server/src/usage/opencodeUsageReader.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ export async function readOpenCodeUsage(
168168
found = true;
169169
const id = entry.name.slice(0, -5);
170170
if (seen.has(`opencode:${id}`)) continue;
171+
// A message cannot be created after its file was last written, so a
172+
// file untouched since the window opened holds nothing in range.
173+
try {
174+
if ((await NodeFSP.stat(path)).mtimeMs < sinceMs) continue;
175+
} catch (cause) {
176+
if (object(cause).code !== "ENOENT") error = true;
177+
continue;
178+
}
171179
const file = { path, records: [] as UsageRecord[] };
172180
files.push(file);
173181
try {

‎apps/server/src/usage/usageTranscriptReader.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,46 @@ describe("SQLite usage readers", () => {
528528
}
529529
});
530530

531+
it("reads only legacy OpenCode messages written since the window opened", async () => {
532+
const sinceMs = 1780000000000;
533+
const legacy = NodePath.join(dir, "storage", "message", "session-1");
534+
await NodeFSP.mkdir(legacy, { recursive: true });
535+
const write = async (id: string, mtimeMs: number) => {
536+
const path = NodePath.join(legacy, `${id}.json`);
537+
await NodeFSP.writeFile(
538+
path,
539+
JSON.stringify({
540+
id,
541+
sessionID: "session-1",
542+
role: "assistant",
543+
modelID: "claude-sonnet-4-5",
544+
// In range, so an old file that was read would produce a record.
545+
time: { created: sinceMs + 1000 },
546+
tokens: { input: 100, output: 20 },
547+
}),
548+
);
549+
await NodeFSP.utimes(path, mtimeMs / 1000, mtimeMs / 1000);
550+
return path;
551+
};
552+
await write("msg-old", sinceMs - 60_000);
553+
554+
const stale = await readOpenCodeUsage(dir, sinceMs);
555+
assert.deepStrictEqual(stale, { files: [], missing: false, error: false });
556+
557+
const recent = await write("msg-new", sinceMs + 60_000);
558+
const result = await readOpenCodeUsage(dir, sinceMs);
559+
assert.isFalse(result.missing);
560+
assert.isFalse(result.error);
561+
assert.deepStrictEqual(
562+
result.files.map((file) => file.path),
563+
[recent],
564+
);
565+
assert.deepStrictEqual(
566+
result.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
567+
["opencode:msg-new"],
568+
);
569+
});
570+
531571
it("deduplicates Antigravity generation and step usage while preserving retry model and token buckets", async () => {
532572
const db = new NodeSqlite.DatabaseSync(NodePath.join(dir, "session-1.db"));
533573
const stamp = protoNumber(1, 1780000000);

0 commit comments

Comments
 (0)