@@ -12,6 +12,14 @@ import * as Path from "effect/Path";
1212import * as SqlClient from "effect/sql/SqlClient" ;
1313import { 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" ;
1523import { openCodeUsageReader , readOpenCodeUsage } from "./usage.ts" ;
1624
1725const 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+
43102describe ( "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