Skip to content

Commit 9b5d997

Browse files
fix(opencode): native forks no longer multiply usage (pingdotgg#23120)
1 parent daed1dd commit 9b5d997

3 files changed

Lines changed: 404 additions & 8 deletions

File tree

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/**
2+
* Rows recorded from an isolated `@opencode/[email protected]` 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‎

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import * as Path from "effect/Path";
1212
import * as SqlClient from "effect/sql/SqlClient";
1313
import { vi } from "vite-plus/test";
1414

15+
import {
16+
openCodeForkContinuationSessionId,
17+
openCodeForkMessages,
18+
openCodeForkSchema,
19+
openCodeForkSessions,
20+
openCodeForkSourceAssistantId,
21+
openCodeForkSourceSessionId,
22+
} from "./usage.fixture.ts";
1523
import { openCodeUsageReader, readOpenCodeUsage } from "./usage.ts";
1624

1725
const readerWorkers = vi.hoisted(() => ({ started: 0, enabled: false }));
@@ -40,6 +48,57 @@ const openDatabase = (filename: string) =>
4048
Effect.map(Context.get(SqlClient.SqlClient)),
4149
);
4250

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+
43102
describe("readOpenCodeUsage", () => {
44103
it.effect("bounds raw history allocations while counting every eligible message once", () =>
45104
Effect.gen(function* () {
@@ -308,6 +367,93 @@ describe("readOpenCodeUsage", () => {
308367
}).pipe(Effect.provide(NodeServices.layer)),
309368
);
310369

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+
311457
it.effect(
312458
"projects both SQLite formats without changing malformed or optional field handling",
313459
() =>

0 commit comments

Comments
 (0)