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, 13:30 UTC
fix(opencode): usage totals include what OpenCode 2 spends on compaction (#30565)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
MarcoDotIO committed
Nov 14, 2026 at 18:21 UTC
fed0f743b9ba6b08db7605475c7ca621cf7f4928
2 files changed
+67
-1
packages/provider-opencode/src/server/usage.test.ts
+64
@@ -454,6 +454,70 @@ describe("readOpenCodeUsage", () => {
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
() =>
packages/provider-opencode/src/server/usage.ts
+3
-1
@@ -275,7 +275,9 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
275
table === "session_message" && forks.size > 0 && columns.has("seq") ? "seq" : "NULL";
276
// Without time_updated an in-place edit can keep the length, so any database write counts.
277
const version = updated === "NULL" ? `${identity}/${stamp(wal!)}` : identity;
278
- const predicates = table === "session_message" ? ["type = 'assistant'"] : [];
278
+ // OpenCode 2 records a compaction request's spend on its compaction message.
279
+ const predicates =
280
+ table === "session_message" ? ["type IN ('assistant', 'compaction')"] : [];
281
if (timestamp !== "NULL") predicates.push("time_created >= ?");
282
// List rowids and change markers so SQLite can use time indexes without reading message
283
// bodies; octet_length comes from the record header.