Skip to content

Commit 42cdcfe

Browse files
committed
fix(server): move OpenCode usage scans off event loop
1 parent e13bc62 commit 42cdcfe

2 files changed

Lines changed: 121 additions & 42 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as NodeOS from "node:os";
44
import * as NodePath from "node:path";
55
import * as NodePerfHooks from "node:perf_hooks";
66
import * as NodeSqlite from "node:sqlite";
7+
import * as NodeTimersPromises from "node:timers/promises";
78

89
import { describe, expect, it } from "@effect/vitest";
910

@@ -128,13 +129,20 @@ describe("readOpenCodeRecords", () => {
128129
}
129130
});
130131

131-
it("waits briefly for a writer before reporting a locked database", async () => {
132+
it("waits for a writer off the event loop before reporting a locked database", async () => {
132133
const dbPath = createDatabase();
133134
const writer = new NodeSqlite.DatabaseSync(dbPath);
134135
writer.exec("BEGIN EXCLUSIVE");
135136
const startedAt = NodePerfHooks.performance.now();
136137
try {
137-
expect(await readOpenCodeRecords(dbPath, 2_000)).toBeNull();
138+
const read = readOpenCodeRecords(dbPath, 2_000);
139+
const first = await Promise.race([
140+
read.then(() => "read" as const),
141+
NodeTimersPromises.setTimeout(50, "timer" as const),
142+
]);
143+
144+
expect(first).toBe("timer");
145+
expect(await read).toBeNull();
138146
expect(NodePerfHooks.performance.now() - startedAt).toBeGreaterThanOrEqual(500);
139147
} finally {
140148
writer.exec("ROLLBACK");

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

Lines changed: 111 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import * as NodeFS from "node:fs";
1717
import * as NodeFSP from "node:fs/promises";
1818
import * as NodePath from "node:path";
1919
import * as NodeReadline from "node:readline";
20-
import * as NodeSqlite from "node:sqlite";
20+
import * as NodeWorkerThreads from "node:worker_threads";
2121

2222
import type { UsageProviderKind } from "@t3tools/contracts";
2323

@@ -237,6 +237,99 @@ const OPEN_CODE_TABLE_QUERIES = {
237237

238238
type OpenCodeMessageTable = keyof typeof OPEN_CODE_TABLE_QUERIES;
239239

240+
interface OpenCodeWorkerRows {
241+
readonly table: OpenCodeMessageTable;
242+
readonly rows: readonly OpenCodeUsageRow[];
243+
}
244+
245+
type OpenCodeWorkerResult =
246+
| { readonly status: "ok"; readonly groups: readonly OpenCodeWorkerRows[] }
247+
| { readonly status: "failed" };
248+
249+
/**
250+
* Kept inline so server bundles do not need a second worker entrypoint. Only
251+
* usage scalars cross back to the main thread; message content stays in SQLite.
252+
*/
253+
const OPEN_CODE_WORKER_SOURCE = String.raw`
254+
const { parentPort, workerData } = require("node:worker_threads");
255+
const { DatabaseSync } = require("node:sqlite");
256+
257+
let database;
258+
let result = { status: "failed" };
259+
try {
260+
database = new DatabaseSync(workerData.dbPath, {
261+
readOnly: true,
262+
timeout: workerData.busyTimeoutMs,
263+
});
264+
265+
const tables = new Set();
266+
for (const row of database.prepare(workerData.tablesQuery).all()) {
267+
if (row.name === "session_message" || row.name === "message") tables.add(row.name);
268+
}
269+
if (tables.size > 0) {
270+
const groups = [];
271+
for (const table of ["session_message", "message"]) {
272+
if (!tables.has(table)) continue;
273+
const rows = database
274+
.prepare(workerData.tableQueries[table])
275+
.all(workerData.sinceMs)
276+
.map((row) => ({ ...row }));
277+
groups.push({ table, rows });
278+
}
279+
result = { status: "ok", groups };
280+
}
281+
} catch {
282+
result = { status: "failed" };
283+
} finally {
284+
try {
285+
database?.close();
286+
} catch {}
287+
}
288+
289+
parentPort.postMessage(result);
290+
`;
291+
292+
function readOpenCodeRows(
293+
dbPath: string,
294+
sinceMs: number,
295+
): Promise<readonly OpenCodeWorkerRows[] | null> {
296+
return new Promise((resolve) => {
297+
const worker = (() => {
298+
try {
299+
return new NodeWorkerThreads.Worker(OPEN_CODE_WORKER_SOURCE, {
300+
eval: true,
301+
workerData: {
302+
dbPath,
303+
sinceMs,
304+
busyTimeoutMs: OPEN_CODE_SQLITE_BUSY_TIMEOUT_MS,
305+
tablesQuery: OPEN_CODE_MESSAGE_TABLES_QUERY,
306+
tableQueries: OPEN_CODE_TABLE_QUERIES,
307+
},
308+
});
309+
} catch {
310+
return null;
311+
}
312+
})();
313+
if (worker === null) {
314+
resolve(null);
315+
return;
316+
}
317+
318+
let settled = false;
319+
const finish = (value: readonly OpenCodeWorkerRows[] | null) => {
320+
if (settled) return;
321+
settled = true;
322+
resolve(value);
323+
};
324+
325+
worker.once("message", (message: OpenCodeWorkerResult) => {
326+
finish(message.status === "ok" ? message.groups : null);
327+
});
328+
worker.once("error", () => finish(null));
329+
worker.once("exit", () => finish(null));
330+
});
331+
}
332+
240333
/**
241334
* Reads usage records from OpenCode's SQLite transcript store.
242335
*
@@ -254,47 +347,25 @@ export async function readOpenCodeRecords(
254347
dbPath: string,
255348
sinceMs: number,
256349
): Promise<readonly UsageRecord[] | null> {
257-
let database: NodeSqlite.DatabaseSync;
258-
try {
259-
database = new NodeSqlite.DatabaseSync(dbPath, {
260-
readOnly: true,
261-
timeout: OPEN_CODE_SQLITE_BUSY_TIMEOUT_MS,
262-
});
263-
} catch {
264-
return null;
265-
}
266-
267-
try {
268-
const tables = new Set<OpenCodeMessageTable>();
269-
for (const row of database.prepare(OPEN_CODE_MESSAGE_TABLES_QUERY).all()) {
270-
const name = (row as Record<string, unknown>)["name"];
271-
if (name === "session_message" || name === "message") tables.add(name);
272-
}
273-
if (tables.size === 0) return null;
350+
const groups = await readOpenCodeRows(dbPath, sinceMs);
351+
if (groups === null) return null;
274352

275-
const recordsByKey = new Map<string, UsageRecord>();
276-
let anonymous = 0;
277-
for (const table of ["session_message", "message"] as const) {
278-
if (!tables.has(table)) continue;
279-
const rows = database.prepare(OPEN_CODE_TABLE_QUERIES[table]).all(sinceMs);
280-
for (const row of rows) {
281-
const record = parseOpenCodeUsageRow(row as OpenCodeUsageRow);
282-
if (record === null) continue;
283-
if (record.dedupeKey === null) {
284-
// An anonymous record can still be unique; it just cannot dedupe
285-
// across the two projections.
286-
recordsByKey.set(`${table}#${anonymous++}`, record);
287-
continue;
288-
}
289-
if (!recordsByKey.has(record.dedupeKey)) {
290-
recordsByKey.set(record.dedupeKey, record);
291-
}
353+
const recordsByKey = new Map<string, UsageRecord>();
354+
let anonymous = 0;
355+
for (const { table, rows } of groups) {
356+
for (const row of rows) {
357+
const record = parseOpenCodeUsageRow(row);
358+
if (record === null) continue;
359+
if (record.dedupeKey === null) {
360+
// An anonymous record can still be unique; it just cannot dedupe
361+
// across the two projections.
362+
recordsByKey.set(`${table}#${anonymous++}`, record);
363+
continue;
364+
}
365+
if (!recordsByKey.has(record.dedupeKey)) {
366+
recordsByKey.set(record.dedupeKey, record);
292367
}
293368
}
294-
return [...recordsByKey.values()];
295-
} catch {
296-
return null;
297-
} finally {
298-
database.close();
299369
}
370+
return [...recordsByKey.values()];
300371
}

0 commit comments

Comments
 (0)