Skip to content

Commit f8c09d0

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. The walk already stats each entry, so use that mtime to skip the read when a legacy message 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 b1ec4b3 commit f8c09d0

2 files changed

Lines changed: 58 additions & 9 deletions

File tree

‎packages/provider-opencode/src/server/usage.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,49 @@ describe("readOpenCodeUsage", () => {
6767
assert.isAbove(Number((yield* fileSystem.stat(path.join(dir, "opencode.db-wal"))).size), 0);
6868
}).pipe(Effect.provide(NodeServices.layer)),
6969
);
70+
71+
it.effect("reads only legacy OpenCode messages written since the window opened", () =>
72+
Effect.gen(function* () {
73+
const fileSystem = yield* FileSystem.FileSystem;
74+
const path = yield* Path.Path;
75+
const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" });
76+
const sinceMs = 1780000000000;
77+
const legacy = path.join(dir, "storage", "message", "session-1");
78+
yield* fileSystem.makeDirectory(legacy, { recursive: true });
79+
const write = Effect.fn(function* (id: string, mtimeMs: number) {
80+
const file = path.join(legacy, `${id}.json`);
81+
yield* fileSystem.writeFileString(
82+
file,
83+
JSON.stringify({
84+
id,
85+
sessionID: "session-1",
86+
role: "assistant",
87+
modelID: "claude-sonnet-4-5",
88+
// In range, so an old file that was read would produce a record.
89+
time: { created: sinceMs + 1000 },
90+
tokens: { input: 100, output: 20 },
91+
}),
92+
);
93+
yield* fileSystem.utimes(file, mtimeMs / 1000, mtimeMs / 1000);
94+
return file;
95+
});
96+
yield* write("msg-old", sinceMs - 60_000);
97+
98+
const stale = yield* readOpenCodeUsage(dir, sinceMs);
99+
assert.deepStrictEqual(stale, { files: [], missing: false, error: false });
100+
101+
const recent = yield* write("msg-new", sinceMs + 60_000);
102+
const result = yield* readOpenCodeUsage(dir, sinceMs);
103+
assert.isFalse(result.missing);
104+
assert.isFalse(result.error);
105+
assert.deepStrictEqual(
106+
result.files.map((file) => file.path),
107+
[recent],
108+
);
109+
assert.deepStrictEqual(
110+
result.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
111+
["opencode:msg-new"],
112+
);
113+
}).pipe(Effect.provide(NodeServices.layer)),
114+
);
70115
});

‎packages/provider-opencode/src/server/usage.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
1717
import * as Effect from "effect/Effect";
1818
import * as Exit from "effect/Exit";
1919
import * as FileSystem from "effect/FileSystem";
20+
import * as Option from "effect/Option";
2021
import * as Path from "effect/Path";
2122
import type * as PlatformError from "effect/PlatformError";
2223
import * as SqlClient from "effect/sql/SqlClient";
@@ -93,12 +94,12 @@ export interface OpenCodeUsageReadResult {
9394

9495
const isNotFound = (cause: PlatformError.PlatformError) => cause.reason._tag === "NotFound";
9596

96-
/** A regular file or directory, never a symlink to one. */
97-
const entryType = Effect.fn("entryType")(function* (path: string) {
97+
/** Stat of a regular file or directory; a symlink reports as itself and is never followed. */
98+
const entryInfo = Effect.fn("entryInfo")(function* (path: string) {
9899
const fileSystem = yield* FileSystem.FileSystem;
99100
const link = yield* Effect.exit(fileSystem.readLink(path));
100-
if (Exit.isSuccess(link)) return "SymbolicLink" as const;
101-
return (yield* fileSystem.stat(path)).type;
101+
if (Exit.isSuccess(link)) return { type: "SymbolicLink" as const, mtime: Option.none<Date>() };
102+
return yield* fileSystem.stat(path);
102103
});
103104

104105
/** Reads current SQLite and pre-migration JSON stores without modifying either. */
@@ -127,8 +128,8 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
127128
names.filter((name) => /^opencode(?:-[a-zA-Z0-9_-]+)?\.db$/.test(name)),
128129
// One database removed between listing and stat must not hide the rest.
129130
(name) =>
130-
entryType(path.join(root, name)).pipe(
131-
Effect.map((type) => type === "File"),
131+
entryInfo(path.join(root, name)).pipe(
132+
Effect.map((info) => info.type === "File"),
132133
Effect.catchTags({
133134
PlatformError: (cause) =>
134135
isNotFound(cause) ? Effect.succeed(false) : Effect.fail(cause),
@@ -214,18 +215,21 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
214215
yield* Effect.gen(function* () {
215216
for (const name of yield* fileSystem.readDirectory(directory)) {
216217
const entry = path.join(directory, name);
217-
const type = yield* entryType(entry).pipe(
218+
const info = yield* entryInfo(entry).pipe(
218219
Effect.catchTags({
219220
PlatformError: (cause) =>
220221
isNotFound(cause) ? Effect.succeed(null) : Effect.fail(cause),
221222
}),
222223
);
223-
if (type === "Directory") {
224+
if (info?.type === "Directory") {
224225
directories.push(entry);
225-
} else if (type === "File" && name.endsWith(".json")) {
226+
} else if (info?.type === "File" && name.endsWith(".json")) {
226227
found = true;
227228
const id = name.slice(0, -5);
228229
if (seen.has(`opencode:${id}`)) continue;
230+
// A message cannot be created after its file was last written, so a
231+
// file untouched since the window opened holds nothing in range.
232+
if (Option.exists(info.mtime, (mtime) => mtime.getTime() < sinceMs)) continue;
229233
const file = { path: entry, records: [] as UsageRecord[] };
230234
files.push(file);
231235
yield* fileSystem.readFileString(entry).pipe(

0 commit comments

Comments
 (0)