@devries-cameron20 / t3code Mirror · read-only / commits / 9b5d9979c8
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): native forks no longer multiply usage (#23120)

Julius Marminge committed Nov 13, 2026 at 09:49 UTC 9b5d9979c85a3ed8ab6ef6241cc876559597c316
3 files changed +404 -8
packages/provider-opencode/src/server/usage.fixture.ts new
+176
@@ -0,0 +1,176 @@
1 +/**
2 + * Rows recorded from an isolated `@opencode/cli@2.1.2` data directory: one imported
3 + * session with a single assistant reply, a fork of it, and a fork of that fork
4 + * moved to another directory. No model request was made after the import, so
5 + * every assistant row here describes the same provider request.
6 + */
7 +export const openCodeForkSchema = [
8 + // Recorded verbatim, except the project foreign key, whose table the reader never reads.
9 + `CREATE TABLE \`session_v2\` (
10 + \`id\` text PRIMARY KEY,
11 + \`project_id\` text NOT NULL,
12 + \`workspace_id\` text,
13 + \`parent_id\` text,
14 + \`fork_session_id\` text,
15 + \`fork_boundary\` text,
16 + \`slug\` text NOT NULL,
17 + \`directory\` text NOT NULL,
18 + \`path\` text,
19 + \`title\` text,
20 + \`version\` text NOT NULL,
21 + \`share_url\` text,
22 + \`summary_additions\` integer,
23 + \`summary_deletions\` integer,
24 + \`summary_files\` integer,
25 + \`summary_diffs\` text,
26 + \`metadata\` text,
27 + \`cost\` real DEFAULT 0 NOT NULL,
28 + \`tokens_input\` integer DEFAULT 0 NOT NULL,
29 + \`tokens_output\` integer DEFAULT 0 NOT NULL,
30 + \`tokens_reasoning\` integer DEFAULT 0 NOT NULL,
31 + \`tokens_cache_read\` integer DEFAULT 0 NOT NULL,
32 + \`tokens_cache_write\` integer DEFAULT 0 NOT NULL,
33 + \`revert\` text,
34 + \`permission\` text,
35 + \`agent\` text,
36 + \`model\` text,
37 + \`time_created\` integer NOT NULL,
38 + \`time_updated\` integer NOT NULL,
39 + \`time_idle\` integer,
40 + \`time_viewed\` integer,
41 + \`idle_outcome\` text,
42 + \`time_compacting\` integer,
43 + \`time_archived\` integer,
44 + \`time_suspended\` integer,
45 + \`resume_attempts\` integer DEFAULT 0 NOT NULL
46 + )`,
47 + `CREATE TABLE \`session_message\` (
48 + \`id\` text PRIMARY KEY,
49 + \`session_id\` text NOT NULL,
50 + \`type\` text NOT NULL,
51 + \`seq\` integer NOT NULL,
52 + \`time_created\` integer NOT NULL,
53 + \`time_updated\` integer NOT NULL,
54 + \`data\` text NOT NULL,
55 + CONSTRAINT \`fk_session_message_session_id_session_v2_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session_v2\`(\`id\`) ON DELETE CASCADE
56 + )`,
57 +] as const;
58 +
59 +export const openCodeForkSourceSessionId = "ses_e9f85baeeffec3vOG8D972DJeK";
60 +export const openCodeForkSourceAssistantId = "msg_usageproofassistant0000001";
61 +export const openCodeForkContinuationSessionId = "ses_e9f847594ffetYJnuLwILjFktF";
62 +
63 +export const openCodeForkSessions = [
64 + {
65 + id: openCodeForkSourceSessionId,
66 + project_id: "66c815350f7a94ca31c5664e5e983b8692210bdc",
67 + fork_session_id: null,
68 + fork_boundary: null,
69 + slug: "mighty-engine",
70 + directory: "/home/ubuntu/wt/issue-23104",
71 + version: "2.1.2",
72 + time_created: 1792619988283,
73 + time_updated: 1792620016405,
74 + },
75 + {
76 + id: "ses_e9f854c73ffezSCt9xY4yA99bP",
77 + project_id: "66c815350f7a94ca31c5664e5e983b8692210bdc",
78 + fork_session_id: openCodeForkSourceSessionId,
79 + fork_boundary: `{"type":"through","messageID":"${openCodeForkSourceAssistantId}"}`,
80 + slug: "silent-meadow",
81 + directory: "/home/ubuntu/wt/issue-23104",
82 + version: "2.1.2",
83 + time_created: 1792620016526,
84 + time_updated: 1792620016526,
85 + },
86 + {
87 + id: openCodeForkContinuationSessionId,
88 + project_id: "98021609e205cae99a3b3545f6e3643a33b807b5",
89 + fork_session_id: "ses_e9f854c73ffezSCt9xY4yA99bP",
90 + fork_boundary: '{"type":"through","messageID":"msg_1607ab38e001GxURO0NU4GY4Az_2"}',
91 + slug: "playful-star",
92 + directory: "/tmp/triage-23104",
93 + version: "2.1.2",
94 + time_created: 1792620071533,
95 + time_updated: 1792620071833,
96 + },
97 +] as const;
98 +
99 +const userData = '{"time":{"created":1792619988283},"text":"Say usage proof"}';
100 +const assistantData =
101 + '{"time":{"created":1792619988284,"completed":1792619988285},"agent":"build","model":{"id":"claude-sonnet-4-5","providerID":"anthropic"},"content":[{"type":"text","text":"usage proof"}],"finish":"stop","cost":0.01,"tokens":{"input":1000,"output":200,"reasoning":0,"cache":{"read":300,"write":0}}}';
102 +
103 +export const openCodeForkMessages = [
104 + {
105 + id: "msg_usageproofuser0000000000001",
106 + session_id: openCodeForkSourceSessionId,
107 + type: "user",
108 + seq: 1,
109 + time_created: 1792619988283,
110 + time_updated: 1792620016408,
111 + data: userData,
112 + },
113 + {
114 + id: openCodeForkSourceAssistantId,
115 + session_id: openCodeForkSourceSessionId,
116 + type: "assistant",
117 + seq: 2,
118 + time_created: 1792619988284,
119 + time_updated: 1792620016408,
120 + data: assistantData,
121 + },
122 + {
123 + id: "msg_1607ab38e001GxURO0NU4GY4Az_1",
124 + session_id: "ses_e9f854c73ffezSCt9xY4yA99bP",
125 + type: "user",
126 + seq: 1,
127 + time_created: 1792619988283,
128 + time_updated: 1792620016408,
129 + data: userData,
130 + },
131 + {
132 + id: "msg_1607ab38e001GxURO0NU4GY4Az_2",
133 + session_id: "ses_e9f854c73ffezSCt9xY4yA99bP",
134 + type: "assistant",
135 + seq: 2,
136 + time_created: 1792619988284,
137 + time_updated: 1792620016408,
138 + data: assistantData,
139 + },
140 + {
141 + id: "msg_1607b8a6d001MA7a5GgmyUywMf_1",
142 + session_id: openCodeForkContinuationSessionId,
143 + type: "user",
144 + seq: 1,
145 + time_created: 1792619988283,
146 + time_updated: 1792620016408,
147 + data: userData,
148 + },
149 + {
150 + id: "msg_1607b8a6d001MA7a5GgmyUywMf_2",
151 + session_id: openCodeForkContinuationSessionId,
152 + type: "assistant",
153 + seq: 2,
154 + time_created: 1792619988284,
155 + time_updated: 1792620016408,
156 + data: assistantData,
157 + },
158 + {
159 + id: "msg_1607b8b990025w0634bKiaIXmD",
160 + session_id: openCodeForkContinuationSessionId,
161 + type: "location-switched",
162 + seq: 6,
163 + time_created: 1792620071833,
164 + time_updated: 1792620071838,
165 + data: '{"time":{"created":1792620071833},"location":{"directory":"/tmp/triage-23104"},"projectID":"98021609e205cae99a3b3545f6e3643a33b807b5","subpath":"","previous":{"location":{"directory":"/home/ubuntu/wt/issue-23104"},"projectID":"66c815350f7a94ca31c5664e5e983b8692210bdc","subpath":""}}',
166 + },
167 + {
168 + id: "msg_1607b8ba5001oCiDSgDjIcR3Nc",
169 + session_id: openCodeForkContinuationSessionId,
170 + type: "idle",
171 + seq: 7,
172 + time_created: 1792620071845,
173 + time_updated: 1792620071846,
174 + data: '{"time":{"created":1792620071845},"outcome":"succeeded"}',
175 + },
176 +] as const;
packages/provider-opencode/src/server/usage.test.ts
+146
@@ -12,6 +12,14 @@ 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 }));
@@ -40,6 +48,57 @@ const openDatabase = (filename: string) =>
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* () {
@@ -308,6 +367,93 @@ describe("readOpenCodeUsage", () => {
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(
458 "projects both SQLite formats without changing malformed or optional field handling",
459 () =>
packages/provider-opencode/src/server/usage.ts
+82 -8
@@ -204,15 +204,75 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
204 )).map((row) => row.name),
205 );
206 if (!tables.has("message") && !tables.has("session_message")) error = true;
207 + const columnsOf = (table: string) =>
208 + sql
209 + .unsafe<{ readonly name: unknown }>(`PRAGMA table_info(${table})`)
210 + .pipe(Effect.map((rows) => new Set(rows.map((row) => row.name))));
211 + // A fork copies its parent's messages through the boundary under new ids, keeping
212 + // seq, creation time and data. Resolve each fork to its last copied seq so a copy
213 + // names its original as (session, seq). A fork whose boundary is gone keeps its usage.
214 + const forks = new Map<
215 + string,
216 + { readonly parent: string; readonly lastCopiedSeq: number; readonly created: number }
217 + >();
218 + if (
219 + tables.has("session_v2") &&
220 + tables.has("session_message") &&
221 + (yield* columnsOf("session_message")).has("seq") &&
222 + (yield* columnsOf("session_v2")).has("fork_boundary")
223 + ) {
224 + const resolved = yield* sql.unsafe<{
225 + readonly id: unknown;
226 + readonly parent: unknown;
227 + readonly created: unknown;
228 + readonly last_copied_seq: unknown;
229 + }>(
230 + `SELECT fork.id, fork.fork_session_id AS parent, fork.time_created AS created,
231 + (SELECT MAX(copied.seq) FROM session_message AS copied
232 + WHERE copied.session_id = fork.fork_session_id
233 + AND CASE json_extract(fork.fork_boundary, '$.type')
234 + WHEN 'through' THEN copied.seq <= boundary.seq
235 + WHEN 'before' THEN copied.seq < boundary.seq
236 + END) AS last_copied_seq
237 + FROM session_v2 AS fork
238 + JOIN session_message AS boundary
239 + ON boundary.session_id = fork.fork_session_id
240 + AND boundary.id = json_extract(fork.fork_boundary, '$.messageID')
241 + WHERE fork.fork_session_id IS NOT NULL AND json_valid(fork.fork_boundary)`,
242 + );
243 + for (const row of resolved) {
244 + if (typeof row.last_copied_seq !== "number" || typeof row.created !== "number") continue;
245 + forks.set(text(row.id), {
246 + parent: text(row.parent),
247 + lastCopiedSeq: row.last_copied_seq,
248 + created: row.created,
249 + });
250 + }
251 + }
252 + const original = (sessionId: string, seq: number, created: number) => {
253 + const visited = new Set<string>();
254 + let session = sessionId;
255 + for (
256 + let fork = forks.get(session);
257 + fork !== undefined &&
258 + seq <= fork.lastCopiedSeq &&
259 + created < fork.created &&
260 + !visited.has(session);
261 + fork = forks.get(session)
262 + ) {
263 + visited.add(session);
264 + session = fork.parent;
265 + }
266 + return `${session}:${seq}`;
267 + };
268 + const originals = new Set<string>();
269 for (const table of ["message", "session_message"] as const) {
270 if (!tables.has(table)) continue;
209 - const columns = new Set(
210 - (yield* sql.unsafe<{ readonly name: unknown }>(`PRAGMA table_info(${table})`)).map(
211 - (row) => row.name,
212 - ),
213 - );
271 + const columns = yield* columnsOf(table);
272 const timestamp = columns.has("time_created") ? "time_created" : "NULL";
273 const updated = columns.has("time_updated") ? "time_updated" : "NULL";
274 + const lineage =
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'"] : [];
@@ -224,9 +284,10 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
284 readonly updated: unknown;
285 readonly size: unknown;
286 readonly created: unknown;
287 + readonly seq: unknown;
288 }>(
289 `SELECT rowid AS row_id, ${updated} AS updated, octet_length(data) AS size,
229 - ${timestamp} AS created
290 + ${timestamp} AS created, ${lineage} AS seq
291 FROM ${table}
292 ${predicates.length ? `WHERE ${predicates.join(" AND ")}` : ""}`,
293 timestamp === "NULL" ? [] : [sinceMs],
@@ -283,11 +344,24 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
344 }
345 // Rows the SQL filter dropped are cached as empty so they are not read again either.
346 for (const row of page) {
286 - cached.set(row.row_id, { ...row, record: parsed.get(row.row_id) ?? null });
347 + cached.set(row.row_id, {
348 + updated: row.updated,
349 + size: row.size,
350 + created: row.created,
351 + record: parsed.get(row.row_id) ?? null,
352 + });
353 }
354 yield* Effect.yieldNow;
355 }
290 - for (const row of listed) append(file.records, cached.get(row.row_id)!.record);
356 + for (const row of listed) {
357 + const record = cached.get(row.row_id)!.record;
358 + if (record !== null && typeof row.seq === "number") {
359 + const copyOf = original(record.sessionId, row.seq, record.timestampMs);
360 + if (originals.has(copyOf)) continue;
361 + originals.add(copyOf);
362 + }
363 + append(file.records, record);
364 + }
365 // Rows before this window were not listed; keep them for a wider window until retention.
366 for (const [rowId, entry] of reusable ?? []) {
367 const created = entry.created;