The best way to control Claude Code, Codex and more from any computer or phone
| 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"; |
| 5 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 6 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 7 | import * as Context from "effect/Context"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as FileSystem from "effect/FileSystem"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Path from "effect/Path"; |
| 12 | import * as SqlClient from "effect/sql/SqlClient"; |
| 13 | import { vi } from "vite-plus/test"; |
| 14 | |
| 15 | import { |
| 16 | openCodeForkContinuationSessionId, |
| 17 | openCodeForkMessages, |
| 18 | openCodeForkSchema, |
| 19 | openCodeForkSessions, |
| 20 | openCodeForkSourceAssistantId, |
| 21 | openCodeForkSourceSessionId, |
| 22 | } from "./usage.fixture.ts"; |
| 23 | import { openCodeUsageReader, readOpenCodeUsage } from "./usage.ts"; |
| 24 | |
| 25 | const readerWorkers = vi.hoisted(() => ({ started: 0, enabled: false })); |
| 26 | |
| 27 | // Statement spies only see the main thread, so reader workers fail to start unless a test enables them. |
| 28 | vi.mock("node:worker_threads", async (importOriginal) => { |
| 29 | const actual = await importOriginal<typeof NodeWorkerThreads>(); |
| 30 | class ReaderWorker extends actual.Worker { |
| 31 | constructor(...args: ConstructorParameters<typeof actual.Worker>) { |
| 32 | super( |
| 33 | ...((readerWorkers.enabled |
| 34 | ? args |
| 35 | : [ |
| 36 | "throw new Error('reader workers disabled');", |
| 37 | { eval: true }, |
| 38 | ]) as ConstructorParameters<typeof actual.Worker>), |
| 39 | ); |
| 40 | readerWorkers.started++; |
| 41 | } |
| 42 | } |
| 43 | return { ...actual, Worker: ReaderWorker }; |
| 44 | }); |
| 45 | |
| 46 | const openDatabase = (filename: string) => |
| 47 | Layer.build(NodeSqliteClient.layer({ filename })).pipe( |
| 48 | Effect.map(Context.get(SqlClient.SqlClient)), |
| 49 | ); |
| 50 | |
| 51 | const recordedForkDatabase = Effect.fn(function* ( |
| 52 | sessions: ReadonlyArray<{ |
| 53 | readonly id: string; |
| 54 | readonly project_id: string; |
| 55 | readonly fork_session_id: string | null; |
| 56 | readonly fork_boundary: string | null; |
| 57 | readonly slug: string; |
| 58 | readonly directory: string; |
| 59 | readonly version: string; |
| 60 | readonly time_created: number; |
| 61 | readonly time_updated: number; |
| 62 | }>, |
| 63 | ) { |
| 64 | const fileSystem = yield* FileSystem.FileSystem; |
| 65 | const path = yield* Path.Path; |
| 66 | const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-fork-test-" }); |
| 67 | const db = yield* openDatabase(path.join(dir, "opencode.db")); |
| 68 | for (const statement of openCodeForkSchema) yield* db.unsafe(statement); |
| 69 | for (const session of sessions) { |
| 70 | yield* db.unsafe( |
| 71 | "INSERT INTO session_v2 (id, project_id, fork_session_id, fork_boundary, slug, directory, version, time_created, time_updated) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 72 | [ |
| 73 | session.id, |
| 74 | session.project_id, |
| 75 | session.fork_session_id, |
| 76 | session.fork_boundary, |
| 77 | session.slug, |
| 78 | session.directory, |
| 79 | session.version, |
| 80 | session.time_created, |
| 81 | session.time_updated, |
| 82 | ], |
| 83 | ); |
| 84 | } |
| 85 | for (const message of openCodeForkMessages) { |
| 86 | yield* db.unsafe( |
| 87 | "INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data) VALUES (?, ?, ?, ?, ?, ?, ?)", |
| 88 | [ |
| 89 | message.id, |
| 90 | message.session_id, |
| 91 | message.type, |
| 92 | message.seq, |
| 93 | message.time_created, |
| 94 | message.time_updated, |
| 95 | message.data, |
| 96 | ], |
| 97 | ); |
| 98 | } |
| 99 | return { dir, db }; |
| 100 | }); |
| 101 | |
| 102 | describe("readOpenCodeUsage", () => { |
| 103 | it.effect("bounds raw history allocations while counting every eligible message once", () => |
| 104 | Effect.gen(function* () { |
| 105 | const fileSystem = yield* FileSystem.FileSystem; |
| 106 | const path = yield* Path.Path; |
| 107 | const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-large-test-" }); |
| 108 | const db = yield* openDatabase(path.join(dir, "opencode.db")); |
| 109 | yield* db.unsafe("PRAGMA journal_mode = WAL"); |
| 110 | yield* db.unsafe( |
| 111 | "CREATE TABLE message (id TEXT, session_id TEXT, data TEXT, time_created INTEGER)", |
| 112 | ); |
| 113 | const sinceMs = 1780000000000; |
| 114 | const message = { |
| 115 | role: "assistant", |
| 116 | modelID: "claude-sonnet-4-5", |
| 117 | time: { created: sinceMs }, |
| 118 | tokens: { input: 100, output: 20 }, |
| 119 | body: "x".repeat(256 * 1024), |
| 120 | }; |
| 121 | const data = JSON.stringify(message); |
| 122 | const insert = (rowId: number, id: string, payload: string, created = sinceMs) => |
| 123 | db.unsafe( |
| 124 | "INSERT INTO message (rowid, id, session_id, data, time_created) VALUES (?, ?, ?, ?, ?)", |
| 125 | [rowId, id, "session-1", payload, created], |
| 126 | ); |
| 127 | for (let index = 0; index < 130; index++) { |
| 128 | yield* insert(index * 2 - 10, `msg-${index}`, data); |
| 129 | } |
| 130 | yield* insert(300, "old", data, sinceMs - 1); |
| 131 | yield* insert(301, "user", JSON.stringify({ ...message, role: "user" })); |
| 132 | for (let index = 0; index < 32; index++) { |
| 133 | yield* insert( |
| 134 | 400 + index, |
| 135 | `user-${index}`, |
| 136 | JSON.stringify({ |
| 137 | role: "user", |
| 138 | summary: { diffs: [{ patch: "x".repeat(1024 * 1024) }] }, |
| 139 | }), |
| 140 | ); |
| 141 | } |
| 142 | yield* insert(302, "invalid", "{invalid"); |
| 143 | yield* insert(303, "msg-0", data); |
| 144 | |
| 145 | const writer = yield* Effect.acquireRelease( |
| 146 | Effect.sync(() => new NodeSqlite.DatabaseSync(path.join(dir, "opencode.db"))), |
| 147 | (database) => Effect.sync(() => database.close()), |
| 148 | ); |
| 149 | let changed = false; |
| 150 | let largestRawResult = 0; |
| 151 | const originalAll = NodeSqlite.StatementSync.prototype.all; |
| 152 | yield* Effect.acquireRelease( |
| 153 | Effect.sync(() => |
| 154 | vi.spyOn(NodeSqlite.StatementSync.prototype, "all").mockImplementation(function ( |
| 155 | this: NodeSqlite.StatementSync, |
| 156 | ...params |
| 157 | ) { |
| 158 | const rows = originalAll.apply(this, params); |
| 159 | const rawSize = rows.reduce( |
| 160 | (size, row) => size + (typeof row.data === "string" ? row.data.length : 0), |
| 161 | 0, |
| 162 | ); |
| 163 | largestRawResult = Math.max(largestRawResult, rawSize); |
| 164 | if (!changed && rawSize > 0) { |
| 165 | changed = true; |
| 166 | writer |
| 167 | .prepare("UPDATE message SET data = ? WHERE id = ?") |
| 168 | .run(JSON.stringify({ ...message, tokens: { input: 900, output: 20 } }), "msg-129"); |
| 169 | } |
| 170 | return rows; |
| 171 | }), |
| 172 | ), |
| 173 | (spy) => Effect.sync(() => spy.mockRestore()), |
| 174 | ); |
| 175 | const result = yield* readOpenCodeUsage(dir, sinceMs); |
| 176 | assert.isFalse(result.error); |
| 177 | const records = result.files.flatMap((file) => file.records); |
| 178 | assert.deepStrictEqual( |
| 179 | records.map((record) => record.dedupeKey), |
| 180 | Array.from({ length: 130 }, (_, index) => `opencode:msg-${index}`), |
| 181 | ); |
| 182 | assert.strictEqual( |
| 183 | records.reduce((total, record) => total + record.totals.uncachedInputTokens, 0), |
| 184 | 13000, |
| 185 | ); |
| 186 | assert.strictEqual( |
| 187 | (yield* readOpenCodeUsage(dir, sinceMs)).files |
| 188 | .flatMap((file) => file.records) |
| 189 | .find((record) => record.dedupeKey === "opencode:msg-129")?.totals.uncachedInputTokens, |
| 190 | 900, |
| 191 | ); |
| 192 | assert.isAbove(largestRawResult, 0); |
| 193 | assert.isBelow(largestRawResult, 64 * 1024); |
| 194 | }).pipe(Effect.provide(NodeServices.layer)), |
| 195 | ); |
| 196 | |
| 197 | it.effect("reads statements in proportion to the window despite sparse, unordered rowids", () => |
| 198 | Effect.gen(function* () { |
| 199 | const fileSystem = yield* FileSystem.FileSystem; |
| 200 | const path = yield* Path.Path; |
| 201 | const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-window-" }); |
| 202 | const db = yield* openDatabase(path.join(dir, "opencode.db")); |
| 203 | yield* db.unsafe( |
| 204 | "CREATE TABLE message (id TEXT, session_id TEXT, data TEXT, time_created INTEGER)", |
| 205 | ); |
| 206 | yield* db.unsafe("CREATE INDEX message_time ON message(time_created)"); |
| 207 | const sinceMs = 1780000000000; |
| 208 | const data = JSON.stringify({ modelID: "m", tokens: { input: 1 } }); |
| 209 | yield* db.unsafe( |
| 210 | `WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 5000) |
| 211 | INSERT INTO message SELECT 'old-' || i, 's', ?, ? FROM n`, |
| 212 | [data, sinceMs - 1], |
| 213 | ); |
| 214 | for (const [rowid, id] of [ |
| 215 | [-10, "a"], |
| 216 | [10000, "b"], |
| 217 | [20000, "c"], |
| 218 | ] as const) { |
| 219 | yield* db.unsafe( |
| 220 | "INSERT INTO message(rowid, id, session_id, data, time_created) VALUES (?, ?, 's', ?, ?)", |
| 221 | [rowid, id, data, sinceMs], |
| 222 | ); |
| 223 | } |
| 224 | let statements = 0; |
| 225 | let bodyReads = 0; |
| 226 | const originalAll = NodeSqlite.StatementSync.prototype.all; |
| 227 | yield* Effect.acquireRelease( |
| 228 | Effect.sync(() => |
| 229 | vi.spyOn(NodeSqlite.StatementSync.prototype, "all").mockImplementation(function ( |
| 230 | this: NodeSqlite.StatementSync, |
| 231 | ...params |
| 232 | ) { |
| 233 | statements++; |
| 234 | if (this.sourceSQL.includes("data ->")) bodyReads++; |
| 235 | return originalAll.apply(this, params); |
| 236 | }), |
| 237 | ), |
| 238 | (spy) => Effect.sync(() => spy.mockRestore()), |
| 239 | ); |
| 240 | const result = yield* readOpenCodeUsage(dir, sinceMs); |
| 241 | assert.isFalse(result.error); |
| 242 | assert.deepStrictEqual( |
| 243 | result.files.flatMap((file) => file.records).map((record) => record.dedupeKey), |
| 244 | ["opencode:a", "opencode:b", "opencode:c"], |
| 245 | ); |
| 246 | assert.isBelow(statements, 10); |
| 247 | |
| 248 | bodyReads = 0; |
| 249 | const repeated = yield* readOpenCodeUsage(dir, sinceMs); |
| 250 | assert.deepStrictEqual( |
| 251 | repeated.files.flatMap((file) => file.records).map((record) => record.dedupeKey), |
| 252 | ["opencode:a", "opencode:b", "opencode:c"], |
| 253 | ); |
| 254 | // An unchanged database must not have its message bodies read and parsed again. |
| 255 | assert.strictEqual(bodyReads, 0); |
| 256 | }).pipe(Effect.provide(NodeServices.layer)), |
| 257 | ); |
| 258 | |
| 259 | it.effect("does not reuse rows from a replaced or restored database file", () => |
| 260 | Effect.gen(function* () { |
| 261 | const fileSystem = yield* FileSystem.FileSystem; |
| 262 | const path = yield* Path.Path; |
| 263 | const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-replaced-" }); |
| 264 | const write = (name: string, input: number) => |
| 265 | Effect.scoped( |
| 266 | Effect.gen(function* () { |
| 267 | const db = yield* openDatabase(path.join(dir, name)); |
| 268 | yield* db.unsafe("PRAGMA journal_mode = WAL"); |
| 269 | yield* db.unsafe( |
| 270 | "CREATE TABLE message (id TEXT, session_id TEXT, time_created INTEGER, time_updated INTEGER, data TEXT)", |
| 271 | ); |
| 272 | yield* db.unsafe( |
| 273 | "INSERT INTO message(rowid, id, session_id, time_created, time_updated, data) VALUES (1, 'a', 's', 1, 1, ?)", |
| 274 | [JSON.stringify({ modelID: "m", tokens: { input } })], |
| 275 | ); |
| 276 | }), |
| 277 | ); |
| 278 | const input = Effect.map( |
| 279 | readOpenCodeUsage(dir, 0), |
| 280 | (result) => result.files[0]?.records[0]?.totals.uncachedInputTokens, |
| 281 | ); |
| 282 | yield* write("opencode.db", 100); |
| 283 | assert.strictEqual(yield* input, 100); |
| 284 | yield* write("replacement.db", 900); |
| 285 | yield* fileSystem.rename(path.join(dir, "replacement.db"), path.join(dir, "opencode.db")); |
| 286 | assert.strictEqual(yield* input, 900); |
| 287 | // A backup restored in place keeps the inode and birth time, like `cp -p`. |
| 288 | yield* write("backup.db", 500); |
| 289 | yield* fileSystem.copyFile(path.join(dir, "backup.db"), path.join(dir, "opencode.db")); |
| 290 | yield* fileSystem.utimes(path.join(dir, "opencode.db"), 0, 0); |
| 291 | assert.strictEqual(yield* input, 500); |
| 292 | }).pipe(Effect.provide(NodeServices.layer)), |
| 293 | ); |
| 294 | |
| 295 | it.effect("counts migrated OpenCode messages once and sees subsequent WAL writes", () => |
| 296 | Effect.gen(function* () { |
| 297 | const fileSystem = yield* FileSystem.FileSystem; |
| 298 | const path = yield* Path.Path; |
| 299 | const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" }); |
| 300 | const db = yield* openDatabase(path.join(dir, "opencode.db")); |
| 301 | yield* db.unsafe("PRAGMA journal_mode = WAL"); |
| 302 | yield* db.unsafe("PRAGMA wal_autocheckpoint = 0"); |
| 303 | yield* db.unsafe("CREATE TABLE message (id TEXT, session_id TEXT, data TEXT)"); |
| 304 | const message = { |
| 305 | id: "msg-1", |
| 306 | sessionID: "session-1", |
| 307 | role: "assistant", |
| 308 | modelID: "claude-sonnet-4-5", |
| 309 | time: { created: 1780000000000 }, |
| 310 | cost: 0.25, |
| 311 | tokens: { input: 100, output: 20, reasoning: 5, cache: { read: 30, write: 10 } }, |
| 312 | }; |
| 313 | const insert = (id: string, sessionId: string, data: string) => |
| 314 | db.unsafe("INSERT INTO message VALUES (?, ?, ?)", [id, sessionId, data]); |
| 315 | yield* insert(message.id, message.sessionID, JSON.stringify(message)); |
| 316 | const legacy = path.join(dir, "storage", "message", message.sessionID); |
| 317 | yield* fileSystem.makeDirectory(legacy, { recursive: true }); |
| 318 | yield* fileSystem.writeFileString(path.join(legacy, "msg-1.json"), JSON.stringify(message)); |
| 319 | yield* Effect.acquireRelease( |
| 320 | Effect.sync(() => { |
| 321 | readerWorkers.enabled = true; |
| 322 | readerWorkers.started = 0; |
| 323 | }), |
| 324 | () => Effect.sync(() => (readerWorkers.enabled = false)), |
| 325 | ); |
| 326 | let mainThreadBodyReads = 0; |
| 327 | const originalAll = NodeSqlite.StatementSync.prototype.all; |
| 328 | yield* Effect.acquireRelease( |
| 329 | Effect.sync(() => |
| 330 | vi.spyOn(NodeSqlite.StatementSync.prototype, "all").mockImplementation(function ( |
| 331 | this: NodeSqlite.StatementSync, |
| 332 | ...params |
| 333 | ) { |
| 334 | if (this.sourceSQL.includes("data ->")) mainThreadBodyReads++; |
| 335 | return originalAll.apply(this, params); |
| 336 | }), |
| 337 | ), |
| 338 | (spy) => Effect.sync(() => spy.mockRestore()), |
| 339 | ); |
| 340 | const first = yield* readOpenCodeUsage(dir, 0); |
| 341 | assert.isFalse(first.error); |
| 342 | const records = first.files.flatMap((file) => file.records); |
| 343 | assert.strictEqual(records.length, 1); |
| 344 | assert.deepStrictEqual(records[0]?.totals, { |
| 345 | uncachedInputTokens: 100, |
| 346 | cachedInputTokens: 30, |
| 347 | cacheCreationTokens: 10, |
| 348 | outputTokens: 25, |
| 349 | reasoningTokens: 5, |
| 350 | }); |
| 351 | assert.strictEqual(records[0]?.reportedCostUsd, 0.25); |
| 352 | yield* insert( |
| 353 | "msg-2", |
| 354 | message.sessionID, |
| 355 | JSON.stringify({ ...message, id: "msg-2", time: { created: 1780000001000 } }), |
| 356 | ); |
| 357 | const next = yield* readOpenCodeUsage(dir, 1780000001000); |
| 358 | assert.isFalse(next.error); |
| 359 | assert.deepStrictEqual( |
| 360 | next.files.flatMap((file) => file.records).map((record) => record.dedupeKey), |
| 361 | ["opencode:msg-2"], |
| 362 | ); |
| 363 | assert.isAbove(Number((yield* fileSystem.stat(path.join(dir, "opencode.db-wal"))).size), 0); |
| 364 | // A large database must not be read on the server's event loop. |
| 365 | assert.isAbove(readerWorkers.started, 0); |
| 366 | assert.strictEqual(mainThreadBodyReads, 0); |
| 367 | }).pipe(Effect.provide(NodeServices.layer)), |
| 368 | ); |
| 369 | |
| 370 | it.effect("counts an OpenCode reply once across the sessions forked from it", () => |
| 371 | Effect.gen(function* () { |
| 372 | const { dir, db } = yield* recordedForkDatabase(openCodeForkSessions); |
| 373 | // A reply sent after the second fork is a new provider request. |
| 374 | yield* db.unsafe( |
| 375 | "INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data) VALUES (?, ?, ?, ?, ?, ?, ?)", |
| 376 | [ |
| 377 | "msg_continuationassistant00001", |
| 378 | openCodeForkContinuationSessionId, |
| 379 | "assistant", |
| 380 | 8, |
| 381 | 1792620090000, |
| 382 | 1792620090500, |
| 383 | JSON.stringify({ |
| 384 | time: { created: 1792620090000, completed: 1792620090500 }, |
| 385 | agent: "build", |
| 386 | model: { id: "claude-sonnet-4-5", providerID: "anthropic" }, |
| 387 | content: [{ type: "text", text: "continued" }], |
| 388 | finish: "stop", |
| 389 | cost: 0.02, |
| 390 | tokens: { input: 50, output: 10, reasoning: 0, cache: { read: 1500, write: 0 } }, |
| 391 | }), |
| 392 | ], |
| 393 | ); |
| 394 | |
| 395 | const result = yield* readOpenCodeUsage(dir, 0); |
| 396 | assert.isFalse(result.error); |
| 397 | const records = result.files.flatMap((file) => file.records); |
| 398 | assert.deepStrictEqual( |
| 399 | records.map((record) => [record.dedupeKey, record.sessionId]), |
| 400 | [ |
| 401 | [`opencode:${openCodeForkSourceAssistantId}`, openCodeForkSourceSessionId], |
| 402 | ["opencode:msg_continuationassistant00001", openCodeForkContinuationSessionId], |
| 403 | ], |
| 404 | ); |
| 405 | assert.deepStrictEqual(records[0]?.totals, { |
| 406 | uncachedInputTokens: 1000, |
| 407 | cachedInputTokens: 300, |
| 408 | cacheCreationTokens: 0, |
| 409 | outputTokens: 200, |
| 410 | reasoningTokens: 0, |
| 411 | }); |
| 412 | assert.strictEqual(records[0]?.reportedCostUsd, 0.01); |
| 413 | }).pipe(Effect.provide(NodeServices.layer)), |
| 414 | ); |
| 415 | |
| 416 | it.effect("counts each OpenCode fork's own replies after its boundary", () => |
| 417 | Effect.gen(function* () { |
| 418 | const [source, fork] = openCodeForkSessions; |
| 419 | const sibling = { ...fork, id: "ses_siblingfork00000000000000", slug: "quiet-river" }; |
| 420 | const { dir, db } = yield* recordedForkDatabase([...openCodeForkSessions, sibling]); |
| 421 | const insert = (id: string, sessionId: string, seq: number, created: number, data: string) => |
| 422 | db.unsafe( |
| 423 | "INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data) VALUES (?, ?, 'assistant', ?, ?, ?, ?)", |
| 424 | [id, sessionId, seq, created, created, data], |
| 425 | ); |
| 426 | const copied = openCodeForkMessages.find( |
| 427 | (message) => message.id === openCodeForkSourceAssistantId, |
| 428 | )!; |
| 429 | yield* insert("msg_siblingcopy_2", sibling.id, copied.seq, copied.time_created, copied.data); |
| 430 | // Sibling forks send identical replies at the same seq and millisecond. |
| 431 | const created = 1792620090000; |
| 432 | const reply = JSON.stringify({ ...JSON.parse(copied.data), time: { created } }); |
| 433 | yield* insert("msg_forkreply000000000000003", fork.id, 3, created, reply); |
| 434 | yield* insert("msg_siblingreply0000000000003", sibling.id, 3, created, reply); |
| 435 | |
| 436 | const records = (yield* readOpenCodeUsage(dir, 0)).files.flatMap((file) => file.records); |
| 437 | assert.deepStrictEqual( |
| 438 | records.map((record) => record.sessionId), |
| 439 | [source.id, fork.id, sibling.id], |
| 440 | ); |
| 441 | |
| 442 | // Deleting the source leaves the first fork holding the only copy. |
| 443 | yield* db.unsafe("DELETE FROM session_message WHERE session_id = ?", [source.id]); |
| 444 | yield* db.unsafe("DELETE FROM session_v2 WHERE id = ?", [source.id]); |
| 445 | const remaining = (yield* readOpenCodeUsage(dir, 0)).files.flatMap((file) => file.records); |
| 446 | assert.includeMembers( |
| 447 | remaining.map((record) => record.dedupeKey), |
| 448 | ["opencode:msg_1607ab38e001GxURO0NU4GY4Az_2", "opencode:msg_forkreply000000000000003"], |
| 449 | ); |
| 450 | assert.notInclude( |
| 451 | remaining.map((record) => record.dedupeKey), |
| 452 | "opencode:msg_1607b8a6d001MA7a5GgmyUywMf_2", |
| 453 | ); |
| 454 | }).pipe(Effect.provide(NodeServices.layer)), |
| 455 | ); |
| 456 | |
| 457 | it.effect("counts OpenCode 2 compaction requests alongside assistant replies", () => |
| 458 | Effect.gen(function* () { |
| 459 | const fileSystem = yield* FileSystem.FileSystem; |
| 460 | const path = yield* Path.Path; |
| 461 | const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" }); |
| 462 | const db = yield* openDatabase(path.join(dir, "opencode.db")); |
| 463 | yield* db.unsafe( |
| 464 | "CREATE TABLE session_message (id TEXT, session_id TEXT, type TEXT, seq INTEGER, time_created INTEGER, data TEXT)", |
| 465 | ); |
| 466 | const created = 1780000000000; |
| 467 | const model = { id: "big-pickle", providerID: "opencode" }; |
| 468 | const insert = (seq: number, id: string, type: string, data: object) => |
| 469 | db.unsafe("INSERT INTO session_message VALUES (?, ?, ?, ?, ?, ?)", [ |
| 470 | id, |
| 471 | "session-1", |
| 472 | type, |
| 473 | seq, |
| 474 | created + seq, |
| 475 | JSON.stringify({ time: { created: created + seq }, ...data }), |
| 476 | ]); |
| 477 | yield* insert(1, "user-1", "user", { text: "hello" }); |
| 478 | yield* insert(2, "assistant-1", "assistant", { |
| 479 | agent: "build", |
| 480 | model, |
| 481 | content: [], |
| 482 | cost: 0.02, |
| 483 | tokens: { input: 100, output: 20, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 484 | }); |
| 485 | yield* insert(3, "compaction-1", "compaction", { |
| 486 | status: "completed", |
| 487 | reason: "auto", |
| 488 | model, |
| 489 | summary: "Summary", |
| 490 | recent: "", |
| 491 | cost: 0.5, |
| 492 | tokens: { input: 40_000, output: 1_500, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 493 | }); |
| 494 | yield* insert(4, "compaction-2", "compaction", { |
| 495 | status: "running", |
| 496 | reason: "manual", |
| 497 | summary: "", |
| 498 | recent: "", |
| 499 | }); |
| 500 | |
| 501 | const result = yield* readOpenCodeUsage(dir, 0); |
| 502 | assert.isFalse(result.error); |
| 503 | const records = result.files.flatMap((file) => file.records); |
| 504 | assert.deepStrictEqual( |
| 505 | records.map((record) => [record.dedupeKey, record.model, record.reportedCostUsd]), |
| 506 | [ |
| 507 | ["opencode:assistant-1", "big-pickle", 0.02], |
| 508 | ["opencode:compaction-1", "big-pickle", 0.5], |
| 509 | ], |
| 510 | ); |
| 511 | assert.deepStrictEqual(records[1]?.totals, { |
| 512 | uncachedInputTokens: 40_000, |
| 513 | cachedInputTokens: 0, |
| 514 | cacheCreationTokens: 0, |
| 515 | outputTokens: 1_500, |
| 516 | reasoningTokens: 0, |
| 517 | }); |
| 518 | }).pipe(Effect.provide(NodeServices.layer)), |
| 519 | ); |
| 520 | |
| 521 | it.effect( |
| 522 | "projects both SQLite formats without changing malformed or optional field handling", |
| 523 | () => |
| 524 | Effect.gen(function* () { |
| 525 | const fileSystem = yield* FileSystem.FileSystem; |
| 526 | const path = yield* Path.Path; |
| 527 | const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-formats-" }); |
| 528 | const db = yield* openDatabase(path.join(dir, "opencode.db")); |
| 529 | const sinceMs = 1780000000000; |
| 530 | const message = { model: { id: "m" }, tokens: { input: 10 }, time: { created: sinceMs } }; |
| 531 | const cases = [ |
| 532 | ["assistant", JSON.stringify({ ...message, role: "assistant", cost: 0.25 }), true], |
| 533 | ["absent-role", JSON.stringify(message), true], |
| 534 | ["null-role", JSON.stringify({ ...message, role: null }), false], |
| 535 | ["user", JSON.stringify({ ...message, role: "user" }), false], |
| 536 | ["malformed", "{broken", false], |
| 537 | ["json5", "{role:'assistant'}", false], |
| 538 | ["null", null, false], |
| 539 | ["blob", new Uint8Array(Buffer.from(JSON.stringify(message))), false], |
| 540 | ["fallback", JSON.stringify({ modelID: "fallback-model", tokens: { output: 5 } }), true], |
| 541 | ] as const; |
| 542 | for (const table of ["message", "session_message"]) { |
| 543 | yield* db.unsafe( |
| 544 | `CREATE TABLE ${table} (id TEXT, session_id TEXT, data, type TEXT, time_created INTEGER)`, |
| 545 | ); |
| 546 | for (const [id, data] of cases) { |
| 547 | yield* db.unsafe(`INSERT INTO ${table} VALUES (?, 's', ?, 'assistant', ?)`, [ |
| 548 | `${table}-${id}`, |
| 549 | data, |
| 550 | sinceMs, |
| 551 | ]); |
| 552 | } |
| 553 | yield* db.unsafe(`INSERT INTO ${table} VALUES (?, 's', ?, 'user', ?)`, [ |
| 554 | `${table}-type-user`, |
| 555 | JSON.stringify(message), |
| 556 | sinceMs, |
| 557 | ]); |
| 558 | } |
| 559 | const result = yield* readOpenCodeUsage(dir, sinceMs); |
| 560 | assert.isFalse(result.error); |
| 561 | const records = result.files.flatMap((file) => file.records); |
| 562 | assert.deepStrictEqual( |
| 563 | records.map((record) => record.dedupeKey), |
| 564 | [ |
| 565 | ...cases.filter(([, , valid]) => valid).map(([id]) => `opencode:message-${id}`), |
| 566 | "opencode:message-type-user", |
| 567 | ...cases.filter(([, , valid]) => valid).map(([id]) => `opencode:session_message-${id}`), |
| 568 | ], |
| 569 | ); |
| 570 | assert.strictEqual(records[0]?.reportedCostUsd, 0.25); |
| 571 | assert.strictEqual(records[2]?.model, "fallback-model"); |
| 572 | assert.strictEqual(records[2]?.timestampMs, sinceMs); |
| 573 | }).pipe(Effect.provide(NodeServices.layer)), |
| 574 | ); |
| 575 | |
| 576 | it.effect("reads only legacy OpenCode messages written since the window opened", () => |
| 577 | Effect.gen(function* () { |
| 578 | const fileSystem = yield* FileSystem.FileSystem; |
| 579 | const path = yield* Path.Path; |
| 580 | const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" }); |
| 581 | const sinceMs = 1780000000000; |
| 582 | const legacy = path.join(dir, "storage", "message", "session-1"); |
| 583 | yield* fileSystem.makeDirectory(legacy, { recursive: true }); |
| 584 | const write = Effect.fn(function* (id: string, mtimeMs: number) { |
| 585 | const file = path.join(legacy, `${id}.json`); |
| 586 | yield* fileSystem.writeFileString( |
| 587 | file, |
| 588 | JSON.stringify({ |
| 589 | id, |
| 590 | sessionID: "session-1", |
| 591 | role: "assistant", |
| 592 | modelID: "claude-sonnet-4-5", |
| 593 | time: { created: sinceMs + 1000 }, |
| 594 | tokens: { input: 100, output: 20 }, |
| 595 | }), |
| 596 | ); |
| 597 | yield* fileSystem.utimes(file, mtimeMs / 1000, mtimeMs / 1000); |
| 598 | return file; |
| 599 | }); |
| 600 | yield* write("msg-old", sinceMs - 60_000); |
| 601 | |
| 602 | const stale = yield* readOpenCodeUsage(dir, sinceMs); |
| 603 | assert.deepStrictEqual(stale, { files: [], missing: false, error: false }); |
| 604 | |
| 605 | const recent = yield* write("msg-new", sinceMs + 60_000); |
| 606 | const result = yield* readOpenCodeUsage(dir, sinceMs); |
| 607 | assert.isFalse(result.missing); |
| 608 | assert.isFalse(result.error); |
| 609 | assert.deepStrictEqual( |
| 610 | result.files.map((file) => file.path), |
| 611 | [recent], |
| 612 | ); |
| 613 | assert.deepStrictEqual( |
| 614 | result.files.flatMap((file) => file.records).map((record) => record.dedupeKey), |
| 615 | ["opencode:msg-new"], |
| 616 | ); |
| 617 | }).pipe(Effect.provide(NodeServices.layer)), |
| 618 | ); |
| 619 | |
| 620 | it.effect("keys OpenCode Go to opencode_go rates and keeps free gateway models at zero", () => |
| 621 | Effect.gen(function* () { |
| 622 | const fileSystem = yield* FileSystem.FileSystem; |
| 623 | const path = yield* Path.Path; |
| 624 | const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" }); |
| 625 | const base = { |
| 626 | sessionID: "session-1", |
| 627 | role: "assistant", |
| 628 | time: { created: 1780000000000 }, |
| 629 | cost: 0, |
| 630 | tokens: { input: 100, output: 20 }, |
| 631 | }; |
| 632 | const db = yield* openDatabase(path.join(dir, "opencode.db")); |
| 633 | yield* db.unsafe("CREATE TABLE message (id TEXT, session_id TEXT, data TEXT)"); |
| 634 | yield* db.unsafe("INSERT INTO message VALUES ('msg-top', 'session-1', ?)", [ |
| 635 | JSON.stringify({ ...base, providerID: "opencode-go", modelID: "deepseek-v4.1-flash" }), |
| 636 | ]); |
| 637 | const legacy = path.join(dir, "storage", "message", "session-1"); |
| 638 | yield* fileSystem.makeDirectory(legacy, { recursive: true }); |
| 639 | yield* fileSystem.writeFileString( |
| 640 | path.join(legacy, "msg-nested.json"), |
| 641 | JSON.stringify({ ...base, model: { providerID: "opencode-go", id: "glm-5.3" } }), |
| 642 | ); |
| 643 | yield* fileSystem.writeFileString( |
| 644 | path.join(legacy, "msg-free.json"), |
| 645 | JSON.stringify({ ...base, providerID: "opencode", modelID: "deepseek-v4.1-flash-free" }), |
| 646 | ); |
| 647 | const result = yield* readOpenCodeUsage(dir, 0); |
| 648 | assert.deepStrictEqual( |
| 649 | result.files |
| 650 | .flatMap((file) => file.records) |
| 651 | .map(({ model, rateModel, reportedCostUsd }) => ({ model, rateModel, reportedCostUsd })) |
| 652 | .sort((a, b) => a.model.localeCompare(b.model)), |
| 653 | [ |
| 654 | { |
| 655 | model: "deepseek-v4.1-flash", |
| 656 | rateModel: "opencode_go/deepseek-v4.1-flash", |
| 657 | reportedCostUsd: null, |
| 658 | }, |
| 659 | { model: "deepseek-v4.1-flash-free", rateModel: undefined, reportedCostUsd: 0 }, |
| 660 | { model: "glm-5.3", rateModel: "opencode_go/glm-5.3", reportedCostUsd: null }, |
| 661 | ], |
| 662 | ); |
| 663 | }).pipe(Effect.provide(NodeServices.layer)), |
| 664 | ); |
| 665 | |
| 666 | it.effect("never follows symlinks in the legacy OpenCode store", () => |
| 667 | Effect.gen(function* () { |
| 668 | const fileSystem = yield* FileSystem.FileSystem; |
| 669 | const path = yield* Path.Path; |
| 670 | const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" }); |
| 671 | const outside = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" }); |
| 672 | const sinceMs = 1780000000000; |
| 673 | const store = path.join(dir, "storage", "message"); |
| 674 | const session = path.join(store, "session-1"); |
| 675 | yield* fileSystem.makeDirectory(session, { recursive: true }); |
| 676 | const write = Effect.fn(function* (directory: string, id: string, mtimeMs: number) { |
| 677 | const file = path.join(directory, `${id}.json`); |
| 678 | yield* fileSystem.writeFileString( |
| 679 | file, |
| 680 | JSON.stringify({ |
| 681 | id, |
| 682 | sessionID: "session-1", |
| 683 | role: "assistant", |
| 684 | modelID: "claude-sonnet-4-5", |
| 685 | time: { created: sinceMs + 1000 }, |
| 686 | tokens: { input: 100, output: 20 }, |
| 687 | }), |
| 688 | ); |
| 689 | yield* fileSystem.utimes(file, mtimeMs / 1000, mtimeMs / 1000); |
| 690 | return file; |
| 691 | }); |
| 692 | const target = yield* write(outside, "msg-linked", sinceMs + 60_000); |
| 693 | const stale = yield* write(outside, "msg-stale", sinceMs - 60_000); |
| 694 | yield* fileSystem.symlink(target, path.join(session, "msg-linked.json")); |
| 695 | yield* fileSystem.symlink(stale, path.join(session, "msg-stale.json")); |
| 696 | yield* fileSystem.symlink(outside, path.join(store, "session-linked")); |
| 697 | yield* fileSystem.symlink(store, path.join(session, "cycle")); |
| 698 | yield* fileSystem.symlink(path.join(outside, "gone.json"), path.join(session, "gone.json")); |
| 699 | yield* fileSystem.symlink(path.join(session, "loop.json"), path.join(session, "loop.json")); |
| 700 | |
| 701 | const linksOnly = yield* readOpenCodeUsage(dir, sinceMs); |
| 702 | assert.deepStrictEqual(linksOnly, { files: [], missing: true, error: false }); |
| 703 | |
| 704 | const real = yield* write(session, "msg-real", sinceMs + 60_000); |
| 705 | const result = yield* readOpenCodeUsage(dir, sinceMs); |
| 706 | assert.isFalse(result.missing); |
| 707 | assert.isFalse(result.error); |
| 708 | assert.deepStrictEqual( |
| 709 | result.files.map((file) => file.path), |
| 710 | [real], |
| 711 | ); |
| 712 | assert.deepStrictEqual( |
| 713 | result.files.flatMap((file) => file.records).map((record) => record.dedupeKey), |
| 714 | ["opencode:msg-real"], |
| 715 | ); |
| 716 | }).pipe(Effect.provide(NodeServices.layer)), |
| 717 | ); |
| 718 | }); |
| 719 | import * as NodeSqlite from "node:sqlite"; |
| 720 | |
| 721 | describe("openCodeUsageReader", () => { |
| 722 | it.effect("scans canonical instance roots once, including XDG and home defaults", () => |
| 723 | Effect.gen(function* () { |
| 724 | const fileSystem = yield* FileSystem.FileSystem; |
| 725 | const path = yield* Path.Path; |
| 726 | const temp = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-instances-test-" }); |
| 727 | const home = yield* fileSystem.realPath(temp); |
| 728 | const defaultRoot = path.join(home, ".local", "share", "opencode"); |
| 729 | const instanceRoot = path.join(home, "instance"); |
| 730 | const dataHome = path.join(home, "xdg"); |
| 731 | const xdgRoot = path.join(dataHome, "opencode"); |
| 732 | const alias = path.join(home, "default-alias"); |
| 733 | for (const root of [defaultRoot, instanceRoot, xdgRoot]) { |
| 734 | yield* fileSystem.makeDirectory(root, { recursive: true }); |
| 735 | } |
| 736 | yield* fileSystem.symlink(defaultRoot, alias); |
| 737 | const instances = [ |
| 738 | { id: "opencode", environment: {}, configured: false }, |
| 739 | { |
| 740 | id: "opencode-list", |
| 741 | environment: { OPENCODE_DATA_DIR: ` ${instanceRoot}, ${alias}, ${defaultRoot}, , ` }, |
| 742 | configured: true, |
| 743 | }, |
| 744 | { |
| 745 | id: "opencode-xdg", |
| 746 | environment: { XDG_DATA_HOME: dataHome }, |
| 747 | configured: true, |
| 748 | }, |
| 749 | ]; |
| 750 | if (openCodeUsageReader.kind !== "scan") throw new Error("Expected an OpenCode scan reader"); |
| 751 | const sources = yield* openCodeUsageReader |
| 752 | .scan({ |
| 753 | instances: instances.map(({ id, environment, configured }) => ({ |
| 754 | instanceId: ProviderInstanceId.make(id), |
| 755 | config: undefined, |
| 756 | environment, |
| 757 | configured, |
| 758 | })), |
| 759 | settings: DEFAULT_SERVER_SETTINGS, |
| 760 | windowStartMs: 0, |
| 761 | retentionCutoffMs: 0, |
| 762 | awaitRefresh: true, |
| 763 | }) |
| 764 | .pipe( |
| 765 | Effect.provideService(HostProcess.Environment, {}), |
| 766 | Effect.provideService(HostProcess.HomeDirectory, home), |
| 767 | ); |
| 768 | assert.deepStrictEqual( |
| 769 | sources.map((source) => source.dir).sort(), |
| 770 | [defaultRoot, instanceRoot, xdgRoot].sort(), |
| 771 | ); |
| 772 | }).pipe(Effect.provide(NodeServices.layer)), |
| 773 | ); |
| 774 | }); |