Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import { DEFAULT_SERVER_SETTINGS, ProviderInstanceId } from "@t3tools/contracts";
4import * as HostProcess from "@t3tools/shared/HostProcess";
5import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
6import * as Context from "effect/Context";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as Layer from "effect/Layer";
10import * as Path from "effect/Path";
11import * as SqlClient from "effect/sql/SqlClient";
12import { vi } from "vite-plus/test";
13
14import { openCodeUsageReader, readOpenCodeUsage } from "./usage.ts";
15
16const openDatabase = (filename: string) =>
17 Layer.build(NodeSqliteClient.layer({ filename })).pipe(
18 Effect.map(Context.get(SqlClient.SqlClient)),
19 );
20
21describe("readOpenCodeUsage", () => {
22 it.effect("bounds raw history allocations while counting every eligible message once", () =>
23 Effect.gen(function* () {
24 const fileSystem = yield* FileSystem.FileSystem;
25 const path = yield* Path.Path;
26 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-large-test-" });
27 const db = yield* openDatabase(path.join(dir, "opencode.db"));
28 yield* db.unsafe("PRAGMA journal_mode = WAL");
29 yield* db.unsafe(
30 "CREATE TABLE message (id TEXT, session_id TEXT, data TEXT, time_created INTEGER)",
31 );
32 const sinceMs = 1780000000000;
33 const message = {
34 role: "assistant",
35 modelID: "claude-sonnet-4-5",
36 time: { created: sinceMs },
37 tokens: { input: 100, output: 20 },
38 body: "x".repeat(256 * 1024),
39 };
40 const data = JSON.stringify(message);
41 const insert = (rowId: number, id: string, payload: string, created = sinceMs) =>
42 db.unsafe(
43 "INSERT INTO message (rowid, id, session_id, data, time_created) VALUES (?, ?, ?, ?, ?)",
44 [rowId, id, "session-1", payload, created],
45 );
46 for (let index = 0; index < 130; index++) {
47 yield* insert(index * 2 - 10, `msg-${index}`, data);
48 }
49 yield* insert(300, "old", data, sinceMs - 1);
50 yield* insert(301, "user", JSON.stringify({ ...message, role: "user" }));
51 for (let index = 0; index < 32; index++) {
52 yield* insert(
53 400 + index,
54 `user-${index}`,
55 JSON.stringify({
56 role: "user",
57 summary: { diffs: [{ patch: "x".repeat(1024 * 1024) }] },
58 }),
59 );
60 }
61 yield* insert(302, "invalid", "{invalid");
62 yield* insert(303, "msg-0", data);
63
64 const writer = yield* Effect.acquireRelease(
65 Effect.sync(() => new NodeSqlite.DatabaseSync(path.join(dir, "opencode.db"))),
66 (database) => Effect.sync(() => database.close()),
67 );
68 let changed = false;
69 let largestRawResult = 0;
70 const originalAll = NodeSqlite.StatementSync.prototype.all;
71 yield* Effect.acquireRelease(
72 Effect.sync(() =>
73 vi.spyOn(NodeSqlite.StatementSync.prototype, "all").mockImplementation(function (
74 this: NodeSqlite.StatementSync,
75 ...params
76 ) {
77 const rows = originalAll.apply(this, params);
78 const rawSize = rows.reduce(
79 (size, row) => size + (typeof row.data === "string" ? row.data.length : 0),
80 0,
81 );
82 largestRawResult = Math.max(largestRawResult, rawSize);
83 if (!changed && rawSize > 0) {
84 changed = true;
85 writer
86 .prepare("UPDATE message SET data = ? WHERE id = ?")
87 .run(JSON.stringify({ ...message, tokens: { input: 900, output: 20 } }), "msg-129");
88 }
89 return rows;
90 }),
91 ),
92 (spy) => Effect.sync(() => spy.mockRestore()),
93 );
94 const result = yield* readOpenCodeUsage(dir, sinceMs);
95 assert.isFalse(result.error);
96 const records = result.files.flatMap((file) => file.records);
97 assert.deepStrictEqual(
98 records.map((record) => record.dedupeKey),
99 Array.from({ length: 130 }, (_, index) => `opencode:msg-${index}`),
100 );
101 assert.strictEqual(
102 records.reduce((total, record) => total + record.totals.uncachedInputTokens, 0),
103 13000,
104 );
105 assert.strictEqual(
106 (yield* readOpenCodeUsage(dir, sinceMs)).files
107 .flatMap((file) => file.records)
108 .find((record) => record.dedupeKey === "opencode:msg-129")?.totals.uncachedInputTokens,
109 900,
110 );
111 assert.isAbove(largestRawResult, 0);
112 assert.isBelow(largestRawResult, 64 * 1024);
113 }).pipe(Effect.provide(NodeServices.layer)),
114 );
115
116 it.effect("reads statements in proportion to the window despite sparse, unordered rowids", () =>
117 Effect.gen(function* () {
118 const fileSystem = yield* FileSystem.FileSystem;
119 const path = yield* Path.Path;
120 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-window-" });
121 const db = yield* openDatabase(path.join(dir, "opencode.db"));
122 yield* db.unsafe(
123 "CREATE TABLE message (id TEXT, session_id TEXT, data TEXT, time_created INTEGER)",
124 );
125 yield* db.unsafe("CREATE INDEX message_time ON message(time_created)");
126 const sinceMs = 1780000000000;
127 const data = JSON.stringify({ modelID: "m", tokens: { input: 1 } });
128 yield* db.unsafe(
129 `WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 5000)
130 INSERT INTO message SELECT 'old-' || i, 's', ?, ? FROM n`,
131 [data, sinceMs - 1],
132 );
133 for (const [rowid, id] of [
134 [-10, "a"],
135 [10000, "b"],
136 [20000, "c"],
137 ] as const) {
138 yield* db.unsafe(
139 "INSERT INTO message(rowid, id, session_id, data, time_created) VALUES (?, ?, 's', ?, ?)",
140 [rowid, id, data, sinceMs],
141 );
142 }
143 let statements = 0;
144 const originalAll = NodeSqlite.StatementSync.prototype.all;
145 yield* Effect.acquireRelease(
146 Effect.sync(() =>
147 vi.spyOn(NodeSqlite.StatementSync.prototype, "all").mockImplementation(function (
148 this: NodeSqlite.StatementSync,
149 ...params
150 ) {
151 statements++;
152 return originalAll.apply(this, params);
153 }),
154 ),
155 (spy) => Effect.sync(() => spy.mockRestore()),
156 );
157 const result = yield* readOpenCodeUsage(dir, sinceMs);
158 assert.isFalse(result.error);
159 assert.deepStrictEqual(
160 result.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
161 ["opencode:a", "opencode:b", "opencode:c"],
162 );
163 assert.isBelow(statements, 10);
164 }).pipe(Effect.provide(NodeServices.layer)),
165 );
166
167 it.effect("counts migrated OpenCode messages once and sees subsequent WAL writes", () =>
168 Effect.gen(function* () {
169 const fileSystem = yield* FileSystem.FileSystem;
170 const path = yield* Path.Path;
171 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" });
172 const db = yield* openDatabase(path.join(dir, "opencode.db"));
173 yield* db.unsafe("PRAGMA journal_mode = WAL");
174 yield* db.unsafe("PRAGMA wal_autocheckpoint = 0");
175 yield* db.unsafe("CREATE TABLE message (id TEXT, session_id TEXT, data TEXT)");
176 const message = {
177 id: "msg-1",
178 sessionID: "session-1",
179 role: "assistant",
180 modelID: "claude-sonnet-4-5",
181 time: { created: 1780000000000 },
182 cost: 0.25,
183 tokens: { input: 100, output: 20, reasoning: 5, cache: { read: 30, write: 10 } },
184 };
185 const insert = (id: string, sessionId: string, data: string) =>
186 db.unsafe("INSERT INTO message VALUES (?, ?, ?)", [id, sessionId, data]);
187 yield* insert(message.id, message.sessionID, JSON.stringify(message));
188 const legacy = path.join(dir, "storage", "message", message.sessionID);
189 yield* fileSystem.makeDirectory(legacy, { recursive: true });
190 yield* fileSystem.writeFileString(path.join(legacy, "msg-1.json"), JSON.stringify(message));
191 const first = yield* readOpenCodeUsage(dir, 0);
192 assert.isFalse(first.error);
193 const records = first.files.flatMap((file) => file.records);
194 assert.strictEqual(records.length, 1);
195 assert.deepStrictEqual(records[0]?.totals, {
196 uncachedInputTokens: 100,
197 cachedInputTokens: 30,
198 cacheCreationTokens: 10,
199 outputTokens: 25,
200 reasoningTokens: 5,
201 });
202 assert.strictEqual(records[0]?.reportedCostUsd, 0.25);
203 yield* insert(
204 "msg-2",
205 message.sessionID,
206 JSON.stringify({ ...message, id: "msg-2", time: { created: 1780000001000 } }),
207 );
208 const next = yield* readOpenCodeUsage(dir, 1780000001000);
209 assert.isFalse(next.error);
210 assert.deepStrictEqual(
211 next.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
212 ["opencode:msg-2"],
213 );
214 assert.isAbove(Number((yield* fileSystem.stat(path.join(dir, "opencode.db-wal"))).size), 0);
215 }).pipe(Effect.provide(NodeServices.layer)),
216 );
217
218 it.effect(
219 "projects both SQLite formats without changing malformed or optional field handling",
220 () =>
221 Effect.gen(function* () {
222 const fileSystem = yield* FileSystem.FileSystem;
223 const path = yield* Path.Path;
224 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-formats-" });
225 const db = yield* openDatabase(path.join(dir, "opencode.db"));
226 const sinceMs = 1780000000000;
227 const message = { model: { id: "m" }, tokens: { input: 10 }, time: { created: sinceMs } };
228 const cases = [
229 ["assistant", JSON.stringify({ ...message, role: "assistant", cost: 0.25 }), true],
230 ["absent-role", JSON.stringify(message), true],
231 ["null-role", JSON.stringify({ ...message, role: null }), false],
232 ["user", JSON.stringify({ ...message, role: "user" }), false],
233 ["malformed", "{broken", false],
234 ["json5", "{role:'assistant'}", false],
235 ["null", null, false],
236 ["blob", new Uint8Array(Buffer.from(JSON.stringify(message))), false],
237 ["fallback", JSON.stringify({ modelID: "fallback-model", tokens: { output: 5 } }), true],
238 ] as const;
239 for (const table of ["message", "session_message"]) {
240 yield* db.unsafe(
241 `CREATE TABLE ${table} (id TEXT, session_id TEXT, data, type TEXT, time_created INTEGER)`,
242 );
243 for (const [id, data] of cases) {
244 yield* db.unsafe(`INSERT INTO ${table} VALUES (?, 's', ?, 'assistant', ?)`, [
245 `${table}-${id}`,
246 data,
247 sinceMs,
248 ]);
249 }
250 yield* db.unsafe(`INSERT INTO ${table} VALUES (?, 's', ?, 'user', ?)`, [
251 `${table}-type-user`,
252 JSON.stringify(message),
253 sinceMs,
254 ]);
255 }
256 const result = yield* readOpenCodeUsage(dir, sinceMs);
257 assert.isFalse(result.error);
258 const records = result.files.flatMap((file) => file.records);
259 assert.deepStrictEqual(
260 records.map((record) => record.dedupeKey),
261 [
262 ...cases.filter(([, , valid]) => valid).map(([id]) => `opencode:message-${id}`),
263 "opencode:message-type-user",
264 ...cases.filter(([, , valid]) => valid).map(([id]) => `opencode:session_message-${id}`),
265 ],
266 );
267 assert.strictEqual(records[0]?.reportedCostUsd, 0.25);
268 assert.strictEqual(records[2]?.model, "fallback-model");
269 assert.strictEqual(records[2]?.timestampMs, sinceMs);
270 }).pipe(Effect.provide(NodeServices.layer)),
271 );
272
273 it.effect("reads only legacy OpenCode messages written since the window opened", () =>
274 Effect.gen(function* () {
275 const fileSystem = yield* FileSystem.FileSystem;
276 const path = yield* Path.Path;
277 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" });
278 const sinceMs = 1780000000000;
279 const legacy = path.join(dir, "storage", "message", "session-1");
280 yield* fileSystem.makeDirectory(legacy, { recursive: true });
281 const write = Effect.fn(function* (id: string, mtimeMs: number) {
282 const file = path.join(legacy, `${id}.json`);
283 yield* fileSystem.writeFileString(
284 file,
285 JSON.stringify({
286 id,
287 sessionID: "session-1",
288 role: "assistant",
289 modelID: "claude-sonnet-4-5",
290 time: { created: sinceMs + 1000 },
291 tokens: { input: 100, output: 20 },
292 }),
293 );
294 yield* fileSystem.utimes(file, mtimeMs / 1000, mtimeMs / 1000);
295 return file;
296 });
297 yield* write("msg-old", sinceMs - 60_000);
298
299 const stale = yield* readOpenCodeUsage(dir, sinceMs);
300 assert.deepStrictEqual(stale, { files: [], missing: false, error: false });
301
302 const recent = yield* write("msg-new", sinceMs + 60_000);
303 const result = yield* readOpenCodeUsage(dir, sinceMs);
304 assert.isFalse(result.missing);
305 assert.isFalse(result.error);
306 assert.deepStrictEqual(
307 result.files.map((file) => file.path),
308 [recent],
309 );
310 assert.deepStrictEqual(
311 result.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
312 ["opencode:msg-new"],
313 );
314 }).pipe(Effect.provide(NodeServices.layer)),
315 );
316
317 it.effect("keys OpenCode Go to opencode_go rates and keeps free gateway models at zero", () =>
318 Effect.gen(function* () {
319 const fileSystem = yield* FileSystem.FileSystem;
320 const path = yield* Path.Path;
321 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" });
322 const base = {
323 sessionID: "session-1",
324 role: "assistant",
325 time: { created: 1780000000000 },
326 cost: 0,
327 tokens: { input: 100, output: 20 },
328 };
329 const db = yield* openDatabase(path.join(dir, "opencode.db"));
330 yield* db.unsafe("CREATE TABLE message (id TEXT, session_id TEXT, data TEXT)");
331 yield* db.unsafe("INSERT INTO message VALUES ('msg-top', 'session-1', ?)", [
332 JSON.stringify({ ...base, providerID: "opencode-go", modelID: "deepseek-v4.1-flash" }),
333 ]);
334 const legacy = path.join(dir, "storage", "message", "session-1");
335 yield* fileSystem.makeDirectory(legacy, { recursive: true });
336 yield* fileSystem.writeFileString(
337 path.join(legacy, "msg-nested.json"),
338 JSON.stringify({ ...base, model: { providerID: "opencode-go", id: "glm-5.3" } }),
339 );
340 yield* fileSystem.writeFileString(
341 path.join(legacy, "msg-free.json"),
342 JSON.stringify({ ...base, providerID: "opencode", modelID: "deepseek-v4.1-flash-free" }),
343 );
344 const result = yield* readOpenCodeUsage(dir, 0);
345 assert.deepStrictEqual(
346 result.files
347 .flatMap((file) => file.records)
348 .map(({ model, rateModel, reportedCostUsd }) => ({ model, rateModel, reportedCostUsd }))
349 .sort((a, b) => a.model.localeCompare(b.model)),
350 [
351 {
352 model: "deepseek-v4.1-flash",
353 rateModel: "opencode_go/deepseek-v4.1-flash",
354 reportedCostUsd: null,
355 },
356 { model: "deepseek-v4.1-flash-free", rateModel: undefined, reportedCostUsd: 0 },
357 { model: "glm-5.3", rateModel: "opencode_go/glm-5.3", reportedCostUsd: null },
358 ],
359 );
360 }).pipe(Effect.provide(NodeServices.layer)),
361 );
362
363 it.effect("never follows symlinks in the legacy OpenCode store", () =>
364 Effect.gen(function* () {
365 const fileSystem = yield* FileSystem.FileSystem;
366 const path = yield* Path.Path;
367 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" });
368 const outside = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" });
369 const sinceMs = 1780000000000;
370 const store = path.join(dir, "storage", "message");
371 const session = path.join(store, "session-1");
372 yield* fileSystem.makeDirectory(session, { recursive: true });
373 const write = Effect.fn(function* (directory: string, id: string, mtimeMs: number) {
374 const file = path.join(directory, `${id}.json`);
375 yield* fileSystem.writeFileString(
376 file,
377 JSON.stringify({
378 id,
379 sessionID: "session-1",
380 role: "assistant",
381 modelID: "claude-sonnet-4-5",
382 time: { created: sinceMs + 1000 },
383 tokens: { input: 100, output: 20 },
384 }),
385 );
386 yield* fileSystem.utimes(file, mtimeMs / 1000, mtimeMs / 1000);
387 return file;
388 });
389 const target = yield* write(outside, "msg-linked", sinceMs + 60_000);
390 const stale = yield* write(outside, "msg-stale", sinceMs - 60_000);
391 yield* fileSystem.symlink(target, path.join(session, "msg-linked.json"));
392 yield* fileSystem.symlink(stale, path.join(session, "msg-stale.json"));
393 yield* fileSystem.symlink(outside, path.join(store, "session-linked"));
394 yield* fileSystem.symlink(store, path.join(session, "cycle"));
395 yield* fileSystem.symlink(path.join(outside, "gone.json"), path.join(session, "gone.json"));
396 yield* fileSystem.symlink(path.join(session, "loop.json"), path.join(session, "loop.json"));
397
398 const linksOnly = yield* readOpenCodeUsage(dir, sinceMs);
399 assert.deepStrictEqual(linksOnly, { files: [], missing: true, error: false });
400
401 const real = yield* write(session, "msg-real", sinceMs + 60_000);
402 const result = yield* readOpenCodeUsage(dir, sinceMs);
403 assert.isFalse(result.missing);
404 assert.isFalse(result.error);
405 assert.deepStrictEqual(
406 result.files.map((file) => file.path),
407 [real],
408 );
409 assert.deepStrictEqual(
410 result.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
411 ["opencode:msg-real"],
412 );
413 }).pipe(Effect.provide(NodeServices.layer)),
414 );
415});
416import * as NodeSqlite from "node:sqlite";
417
418describe("openCodeUsageReader", () => {
419 it.effect("scans canonical instance roots once, including XDG and home defaults", () =>
420 Effect.gen(function* () {
421 const fileSystem = yield* FileSystem.FileSystem;
422 const path = yield* Path.Path;
423 const temp = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-instances-test-" });
424 const home = yield* fileSystem.realPath(temp);
425 const defaultRoot = path.join(home, ".local", "share", "opencode");
426 const instanceRoot = path.join(home, "instance");
427 const dataHome = path.join(home, "xdg");
428 const xdgRoot = path.join(dataHome, "opencode");
429 const alias = path.join(home, "default-alias");
430 for (const root of [defaultRoot, instanceRoot, xdgRoot]) {
431 yield* fileSystem.makeDirectory(root, { recursive: true });
432 }
433 yield* fileSystem.symlink(defaultRoot, alias);
434 const instances = [
435 { id: "opencode", environment: {}, configured: false },
436 {
437 id: "opencode-list",
438 environment: { OPENCODE_DATA_DIR: ` ${instanceRoot}, ${alias}, ${defaultRoot}, , ` },
439 configured: true,
440 },
441 {
442 id: "opencode-xdg",
443 environment: { XDG_DATA_HOME: dataHome },
444 configured: true,
445 },
446 ];
447 if (openCodeUsageReader.kind !== "scan") throw new Error("Expected an OpenCode scan reader");
448 const sources = yield* openCodeUsageReader
449 .scan({
450 instances: instances.map(({ id, environment, configured }) => ({
451 instanceId: ProviderInstanceId.make(id),
452 config: undefined,
453 environment,
454 configured,
455 })),
456 settings: DEFAULT_SERVER_SETTINGS,
457 windowStartMs: 0,
458 retentionCutoffMs: 0,
459 awaitRefresh: true,
460 })
461 .pipe(
462 Effect.provideService(HostProcess.Environment, {}),
463 Effect.provideService(HostProcess.HomeDirectory, home),
464 );
465 assert.deepStrictEqual(
466 sources.map((source) => source.dir).sort(),
467 [defaultRoot, instanceRoot, xdgRoot].sort(),
468 );
469 }).pipe(Effect.provide(NodeServices.layer)),
470 );
471});