@devries-cameron20 / t3code Mirror · read-only / commits / 148612e181
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 17:15 UTC

fix(opencode): usage scans reuse unchanged rows and run off the event loop (#34897)

Julius Marminge committed Nov 13, 2026 at 08:50 UTC 148612e181446ed6b1e87dd013f5ad7f488ba01d
2 files changed +179 -12
packages/provider-opencode/src/server/usage.test.ts
+93
@@ -1,3 +1,4 @@
1 +import type * as NodeWorkerThreads from "node:worker_threads";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import { assert, describe, it } from "@effect/vitest";
4 import { DEFAULT_SERVER_SETTINGS, ProviderInstanceId } from "@t3tools/contracts";
@@ -13,6 +14,27 @@ import { vi } from "vite-plus/test";
14
15 import { openCodeUsageReader, readOpenCodeUsage } from "./usage.ts";
16
17 +const readerWorkers = vi.hoisted(() => ({ started: 0, enabled: false }));
18 +
19 +// Statement spies only see the main thread, so reader workers fail to start unless a test enables them.
20 +vi.mock("node:worker_threads", async (importOriginal) => {
21 + const actual = await importOriginal<typeof NodeWorkerThreads>();
22 + class ReaderWorker extends actual.Worker {
23 + constructor(...args: ConstructorParameters<typeof actual.Worker>) {
24 + super(
25 + ...((readerWorkers.enabled
26 + ? args
27 + : [
28 + "throw new Error('reader workers disabled');",
29 + { eval: true },
30 + ]) as ConstructorParameters<typeof actual.Worker>),
31 + );
32 + readerWorkers.started++;
33 + }
34 + }
35 + return { ...actual, Worker: ReaderWorker };
36 +});
37 +
38 const openDatabase = (filename: string) =>
39 Layer.build(NodeSqliteClient.layer({ filename })).pipe(
40 Effect.map(Context.get(SqlClient.SqlClient)),
@@ -141,6 +163,7 @@ describe("readOpenCodeUsage", () => {
163 );
164 }
165 let statements = 0;
166 + let bodyReads = 0;
167 const originalAll = NodeSqlite.StatementSync.prototype.all;
168 yield* Effect.acquireRelease(
169 Effect.sync(() =>
@@ -149,6 +172,7 @@ describe("readOpenCodeUsage", () => {
172 ...params
173 ) {
174 statements++;
175 + if (this.sourceSQL.includes("data ->")) bodyReads++;
176 return originalAll.apply(this, params);
177 }),
178 ),
@@ -161,6 +185,51 @@ describe("readOpenCodeUsage", () => {
185 ["opencode:a", "opencode:b", "opencode:c"],
186 );
187 assert.isBelow(statements, 10);
188 +
189 + bodyReads = 0;
190 + const repeated = yield* readOpenCodeUsage(dir, sinceMs);
191 + assert.deepStrictEqual(
192 + repeated.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
193 + ["opencode:a", "opencode:b", "opencode:c"],
194 + );
195 + // An unchanged database must not have its message bodies read and parsed again.
196 + assert.strictEqual(bodyReads, 0);
197 + }).pipe(Effect.provide(NodeServices.layer)),
198 + );
199 +
200 + it.effect("does not reuse rows from a replaced or restored database file", () =>
201 + Effect.gen(function* () {
202 + const fileSystem = yield* FileSystem.FileSystem;
203 + const path = yield* Path.Path;
204 + const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-replaced-" });
205 + const write = (name: string, input: number) =>
206 + Effect.scoped(
207 + Effect.gen(function* () {
208 + const db = yield* openDatabase(path.join(dir, name));
209 + yield* db.unsafe("PRAGMA journal_mode = WAL");
210 + yield* db.unsafe(
211 + "CREATE TABLE message (id TEXT, session_id TEXT, time_created INTEGER, time_updated INTEGER, data TEXT)",
212 + );
213 + yield* db.unsafe(
214 + "INSERT INTO message(rowid, id, session_id, time_created, time_updated, data) VALUES (1, 'a', 's', 1, 1, ?)",
215 + [JSON.stringify({ modelID: "m", tokens: { input } })],
216 + );
217 + }),
218 + );
219 + const input = Effect.map(
220 + readOpenCodeUsage(dir, 0),
221 + (result) => result.files[0]?.records[0]?.totals.uncachedInputTokens,
222 + );
223 + yield* write("opencode.db", 100);
224 + assert.strictEqual(yield* input, 100);
225 + yield* write("replacement.db", 900);
226 + yield* fileSystem.rename(path.join(dir, "replacement.db"), path.join(dir, "opencode.db"));
227 + assert.strictEqual(yield* input, 900);
228 + // A backup restored in place keeps the inode and birth time, like `cp -p`.
229 + yield* write("backup.db", 500);
230 + yield* fileSystem.copyFile(path.join(dir, "backup.db"), path.join(dir, "opencode.db"));
231 + yield* fileSystem.utimes(path.join(dir, "opencode.db"), 0, 0);
232 + assert.strictEqual(yield* input, 500);
233 }).pipe(Effect.provide(NodeServices.layer)),
234 );
235
@@ -188,6 +257,27 @@ describe("readOpenCodeUsage", () => {
257 const legacy = path.join(dir, "storage", "message", message.sessionID);
258 yield* fileSystem.makeDirectory(legacy, { recursive: true });
259 yield* fileSystem.writeFileString(path.join(legacy, "msg-1.json"), JSON.stringify(message));
260 + yield* Effect.acquireRelease(
261 + Effect.sync(() => {
262 + readerWorkers.enabled = true;
263 + readerWorkers.started = 0;
264 + }),
265 + () => Effect.sync(() => (readerWorkers.enabled = false)),
266 + );
267 + let mainThreadBodyReads = 0;
268 + const originalAll = NodeSqlite.StatementSync.prototype.all;
269 + yield* Effect.acquireRelease(
270 + Effect.sync(() =>
271 + vi.spyOn(NodeSqlite.StatementSync.prototype, "all").mockImplementation(function (
272 + this: NodeSqlite.StatementSync,
273 + ...params
274 + ) {
275 + if (this.sourceSQL.includes("data ->")) mainThreadBodyReads++;
276 + return originalAll.apply(this, params);
277 + }),
278 + ),
279 + (spy) => Effect.sync(() => spy.mockRestore()),
280 + );
281 const first = yield* readOpenCodeUsage(dir, 0);
282 assert.isFalse(first.error);
283 const records = first.files.flatMap((file) => file.records);
@@ -212,6 +302,9 @@ describe("readOpenCodeUsage", () => {
302 ["opencode:msg-2"],
303 );
304 assert.isAbove(Number((yield* fileSystem.stat(path.join(dir, "opencode.db-wal"))).size), 0);
305 + // A large database must not be read on the server's event loop.
306 + assert.isAbove(readerWorkers.started, 0);
307 + assert.strictEqual(mainThreadBodyReads, 0);
308 }).pipe(Effect.provide(NodeServices.layer)),
309 );
310
packages/provider-opencode/src/server/usage.ts
+86 -12
@@ -11,6 +11,7 @@ import * as FileSystem from "effect/FileSystem";
11 import * as Option from "effect/Option";
12 import * as Path from "effect/Path";
13 import type * as PlatformError from "effect/PlatformError";
14 +import * as Semaphore from "effect/Semaphore";
15 import * as SqlClient from "effect/sql/SqlClient";
16
17 import type { OpenCodeSettings } from "../settings.ts";
@@ -93,6 +94,18 @@ export interface OpenCodeUsageReadResult {
94 readonly error: boolean;
95 }
96
97 +interface CachedRow {
98 + readonly updated: unknown;
99 + readonly size: unknown;
100 + readonly created: unknown;
101 + readonly record: UsageRecord | null;
102 +}
103 +
104 +// Parsed rows by `${databasePath}\0${table}`, then rowid, so unchanged rows are not read again.
105 +const rowCache = new Map<string, { version: string; rows: Map<number, CachedRow> }>();
106 +// Concurrent scans of the same database would each read every uncached row.
107 +const scanLock = Semaphore.makeUnsafe(1);
108 +
109 const isNotFound = (cause: PlatformError.PlatformError) => cause.reason._tag === "NotFound";
110 const MESSAGE_PAGE_SIZE = 64;
111
@@ -116,6 +129,7 @@ const LEGACY_STAT_BATCH = 256;
129 export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
130 root: string,
131 sinceMs: number,
132 + retentionCutoffMs = sinceMs,
133 ): Effect.fn.Return<OpenCodeUsageReadResult, never, FileSystem.FileSystem | Path.Path> {
134 const fileSystem = yield* FileSystem.FileSystem;
135 const path = yield* Path.Path;
@@ -164,6 +178,22 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
178 found = true;
179 const file = { path: path.join(root, name), records: [] as UsageRecord[] };
180 files.push(file);
181 + // Taken before the snapshot opens, so a write that races the scan invalidates its rows.
182 + const [main, wal] = yield* Effect.forEach([file.path, `${file.path}-wal`], (target) =>
183 + Effect.option(fileSystem.stat(target)),
184 + );
185 + const stamp = (info: Option.Option<FileSystem.File.Info>) =>
186 + Option.match(info, {
187 + onNone: () => "none",
188 + onSome: (stat) => `${stat.size}:${Option.getOrNull(stat.mtime)?.getTime()}`,
189 + });
190 + // A replaced or restored database can repeat rowids and change markers, so every key includes
191 + // the main file; WAL commits leave it alone until a checkpoint.
192 + const identity = Option.match(main!, {
193 + onNone: () => "none",
194 + onSome: (stat) =>
195 + `${stat.dev}:${Option.getOrNull(stat.ino)}:${Option.getOrNull(stat.birthtime)?.getTime()}/${stamp(main!)}`,
196 + });
197 yield* Effect.gen(function* () {
198 const sql = yield* SqlClient.SqlClient;
199 // Fail promptly on live-writer locks instead of stalling the server.
@@ -182,30 +212,53 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
212 ),
213 );
214 const timestamp = columns.has("time_created") ? "time_created" : "NULL";
215 + const updated = columns.has("time_updated") ? "time_updated" : "NULL";
216 + // Without time_updated an in-place edit can keep the length, so any database write counts.
217 + const version = updated === "NULL" ? `${identity}/${stamp(wal!)}` : identity;
218 const predicates = table === "session_message" ? ["type = 'assistant'"] : [];
219 if (timestamp !== "NULL") predicates.push("time_created >= ?");
187 - // List only rowids so SQLite can use time indexes without reading message bodies.
188 - const listed = (yield* sql.unsafe<{ readonly row_id: number }>(
189 - `SELECT rowid AS row_id FROM ${table}
220 + // List rowids and change markers so SQLite can use time indexes without reading message
221 + // bodies; octet_length comes from the record header.
222 + const listed = (yield* sql.unsafe<{
223 + readonly row_id: number;
224 + readonly updated: unknown;
225 + readonly size: unknown;
226 + readonly created: unknown;
227 + }>(
228 + `SELECT rowid AS row_id, ${updated} AS updated, octet_length(data) AS size,
229 + ${timestamp} AS created
230 + FROM ${table}
231 ${predicates.length ? `WHERE ${predicates.join(" AND ")}` : ""}`,
232 timestamp === "NULL" ? [] : [sinceMs],
233 )).toSorted((a, b) => a.row_id - b.row_id);
234 databaseRows += listed.length;
235 + const key = `${file.path}\0${table}`;
236 + const previous = rowCache.get(key);
237 + const reusable = previous?.version === version ? previous.rows : undefined;
238 + const cached = new Map<number, CachedRow>();
239 + const misses: typeof listed = [];
240 + for (const row of listed) {
241 + const hit = reusable?.get(row.row_id);
242 + if (hit && hit.updated === row.updated && hit.size === row.size)
243 + cached.set(row.row_id, hit);
244 + else misses.push(row);
245 + }
246 // Filter and project in SQLite: user diffs and assistant bodies can be hundreds of MB.
247 const validAssistant = `CASE WHEN typeof(data) = 'text' AND json_valid(data)
248 THEN json_type(data, '$.role') IS NULL OR data ->> '$.role' = 'assistant' END`;
249 // Omit per-page spans; the enclosing scan records row and page counts.
198 - for (let start = 0; start < listed.length; start += MESSAGE_PAGE_SIZE) {
199 - const page = listed.slice(start, start + MESSAGE_PAGE_SIZE);
250 + for (let start = 0; start < misses.length; start += MESSAGE_PAGE_SIZE) {
251 + const page = misses.slice(start, start + MESSAGE_PAGE_SIZE);
252 databasePages++;
253 const rows = yield* sql
254 .unsafe<{
255 + readonly row_id: number;
256 readonly id: unknown;
257 readonly session_id: unknown;
258 readonly data: unknown;
259 readonly created: unknown;
260 }>(
208 - `SELECT id, session_id, ${timestamp} AS created, json_object(
261 + `SELECT rowid AS row_id, id, session_id, ${timestamp} AS created, json_object(
262 'tokens', data -> '$.tokens', 'cost', data -> '$.cost',
263 'model', data -> '$.model', 'modelID', data -> '$.modelID',
264 'providerID', data -> '$.providerID',
@@ -217,9 +270,10 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
270 page.map((row) => row.row_id),
271 )
272 .pipe(Effect.withTracerEnabled(false));
273 + const parsed = new Map<number, UsageRecord | null>();
274 for (const row of rows) {
221 - append(
222 - file.records,
275 + parsed.set(
276 + row.row_id,
277 parseOpenCodeMessage(text(row.data), {
278 id: text(row.id),
279 sessionId: text(row.session_id),
@@ -227,12 +281,28 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
281 }),
282 );
283 }
284 + // Rows the SQL filter dropped are cached as empty so they are not read again either.
285 + for (const row of page) {
286 + cached.set(row.row_id, { ...row, record: parsed.get(row.row_id) ?? null });
287 + }
288 yield* Effect.yieldNow;
289 }
290 + for (const row of listed) append(file.records, cached.get(row.row_id)!.record);
291 + // Rows before this window were not listed; keep them for a wider window until retention.
292 + for (const [rowId, entry] of reusable ?? []) {
293 + const created = entry.created;
294 + if (typeof created === "number" && created < sinceMs && created >= retentionCutoffMs)
295 + cached.set(rowId, entry);
296 + }
297 + rowCache.set(key, { version, rows: cached });
298 }
299 }).pipe(
300 (read) => Effect.flatMap(SqlClient.SqlClient, (sql) => sql.withTransaction(read)),
235 - Effect.provide(NodeSqliteClient.layer({ filename: file.path, readonly: true })),
301 + // Run the scan on a reader worker so large databases do not block the server's event loop.
302 + NodeSqliteClient.readOnly,
303 + Effect.provide(
304 + NodeSqliteClient.layer({ filename: file.path, readonly: true, readerWorker: true }),
305 + ),
306 Effect.catchTags({
307 SqlError: () => {
308 error = true;
@@ -312,7 +382,7 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
382 "usage.database_pages": databasePages,
383 });
384 return { files, missing: !found && !error, error };
315 -});
385 +}, scanLock.withPermit);
386
387 const resolveOpenCodeDataDirs = Effect.fn("resolveOpenCodeDataDirs")(function* (
388 environment: NodeJS.ProcessEnv,
@@ -346,7 +416,11 @@ export type OpenCodeUsageReaderEnv = FileSystem.FileSystem | Path.Path;
416 export const openCodeUsageReader: ProviderUsageReader<OpenCodeSettings, OpenCodeUsageReaderEnv> = {
417 kind: "scan",
418 provider: "opencode",
349 - scan: Effect.fn("openCodeUsageReader.scan")(function* ({ instances, windowStartMs }) {
419 + scan: Effect.fn("openCodeUsageReader.scan")(function* ({
420 + instances,
421 + windowStartMs,
422 + retentionCutoffMs,
423 + }) {
424 const roots = new Set<string>();
425 for (const instance of instances) {
426 for (const root of yield* resolveOpenCodeDataDirs(instance.environment)) roots.add(root);
@@ -354,7 +428,7 @@ export const openCodeUsageReader: ProviderUsageReader<OpenCodeSettings, OpenCode
428 return yield* Effect.forEach(
429 roots,
430 (dir) =>
357 - readOpenCodeUsage(dir, windowStartMs).pipe(
431 + readOpenCodeUsage(dir, windowStartMs, retentionCutoffMs).pipe(
432 Effect.map((result) => ({
433 dir,
434 files: result.missing && !result.error ? null : result.files,