@@ -37,6 +37,7 @@ import { HttpClient, HttpClientResponse } from "effect/unstable/http";
3737
3838import { ServerConfig } from "../config.ts" ;
3939import { expandHomePath } from "../pathExpansion.ts" ;
40+ import { mergeProviderInstanceEnvironment } from "../provider/ProviderInstanceEnvironment.ts" ;
4041import * as ServerSettings from "../serverSettings.ts" ;
4142import { resolveClaudeHomePath } from "../provider/Drivers/ClaudeHome.ts" ;
4243import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts" ;
@@ -202,7 +203,7 @@ export const make = Effect.gen(function* () {
202203 return nestedExists ? nested : path . join ( homePath , "projects" ) ;
203204 } ) ;
204205
205- /** Resolves the transcript directory for each provider . */
206+ /** Resolves transcript directories and every OpenCode instance environment . */
206207 const resolveTranscriptDirs = Effect . fn ( "UsageService.resolveTranscriptDirs" ) ( function * ( ) {
207208 // A settings failure must surface as an error: swallowing it here would
208209 // present "zero usage from every provider" as a valid answer.
@@ -231,46 +232,71 @@ export const make = Effect.gen(function* () {
231232 ? path . resolve ( expandHomePath ( grokHomeEnv ) )
232233 : path . join ( NodeOS . homedir ( ) , ".grok" ) ;
233234
234- return [
235- { provider : "claude" as const , dir : claudeDir } ,
236- { provider : "codex" as const , dir : path . join ( codexLayout . sharedHomePath , "sessions" ) } ,
237- {
238- provider : "grok" as const ,
239- dir : path . join ( grokHome , "sessions" ) ,
240- fileName : "updates.jsonl" ,
241- } ,
242- ] ;
235+ return {
236+ dirs : [
237+ { provider : "claude" as const , dir : claudeDir } ,
238+ { provider : "codex" as const , dir : path . join ( codexLayout . sharedHomePath , "sessions" ) } ,
239+ {
240+ provider : "grok" as const ,
241+ dir : path . join ( grokHome , "sessions" ) ,
242+ fileName : "updates.jsonl" ,
243+ } ,
244+ ] ,
245+ openCodeEnvironments : [
246+ hostEnvironment ,
247+ ...Object . values ( settings . providerInstances )
248+ . filter ( ( instance ) => instance . driver === "opencode" )
249+ . map ( ( instance ) =>
250+ mergeProviderInstanceEnvironment ( instance . environment , hostEnvironment ) ,
251+ ) ,
252+ ] ,
253+ } ;
243254 } ) ;
244255
245- /** Resolves every persisted database the installed OpenCode channel can use. */
246- const resolveOpenCodeDatabases = Effect . fn ( "UsageService.resolveOpenCodeDatabases" ) ( function * ( ) {
247- const xdgDataHome = hostEnvironment [ "XDG_DATA_HOME" ] ?. trim ( ) ?? "" ;
248- const dataRoot =
249- xdgDataHome . length > 0
250- ? path . resolve ( expandHomePath ( xdgDataHome ) )
251- : path . join ( NodeOS . homedir ( ) , ".local" , "share" ) ;
252- const dataDirectory = path . join ( dataRoot , "opencode" ) ;
253- const directoryEntries = yield * fileSystem
254- . readDirectory ( dataDirectory )
255- . pipe ( Effect . catchCause ( ( ) => Effect . succeed ( [ ] as const ) ) ) ;
256-
257- const databasePaths = resolveOpenCodeDatabasePaths ( {
258- dataDirectory,
259- databaseOverride : hostEnvironment [ "OPENCODE_DB" ] ,
260- disableChannelDatabase : hostEnvironment [ "OPENCODE_DISABLE_CHANNEL_DB" ] ,
261- directoryEntries,
262- path,
263- } ) ;
264- const hasOverride = ( hostEnvironment [ "OPENCODE_DB" ] ?. trim ( ) . length ?? 0 ) > 0 ;
256+ /** Resolves every persisted database the installed OpenCode instances can use. */
257+ const resolveOpenCodeDatabases = Effect . fn ( "UsageService.resolveOpenCodeDatabases" ) ( function * (
258+ environments : readonly NodeJS . ProcessEnv [ ] ,
259+ ) {
260+ const resolved = yield * Effect . forEach ( environments , ( environment ) =>
261+ Effect . gen ( function * ( ) {
262+ const xdgDataHome = environment [ "XDG_DATA_HOME" ] ?. trim ( ) ?? "" ;
263+ const dataRoot =
264+ xdgDataHome . length > 0
265+ ? path . resolve ( expandHomePath ( xdgDataHome ) )
266+ : path . join ( NodeOS . homedir ( ) , ".local" , "share" ) ;
267+ const dataDirectory = path . join ( dataRoot , "opencode" ) ;
268+ const directoryEntries = yield * fileSystem
269+ . readDirectory ( dataDirectory )
270+ . pipe ( Effect . catchCause ( ( ) => Effect . succeed ( [ ] as const ) ) ) ;
271+ const databasePaths = resolveOpenCodeDatabasePaths ( {
272+ dataDirectory,
273+ databaseOverride : environment [ "OPENCODE_DB" ] ,
274+ disableChannelDatabase : environment [ "OPENCODE_DISABLE_CHANNEL_DB" ] ,
275+ directoryEntries,
276+ path,
277+ } ) ;
278+ const hasOverride = ( environment [ "OPENCODE_DB" ] ?. trim ( ) . length ?? 0 ) > 0 ;
279+ return {
280+ databasePaths,
281+ sourcePath :
282+ hasOverride && databasePaths . length === 1
283+ ? ( databasePaths [ 0 ] ?? dataDirectory )
284+ : dataDirectory ,
285+ } ;
286+ } ) ,
287+ ) ;
288+
289+ const databasePaths = [ ...new Set ( resolved . flatMap ( ( entry ) => entry . databasePaths ) ) ] . sort (
290+ ( left , right ) => left . localeCompare ( right ) ,
291+ ) ;
292+ const sourcePaths = [ ...new Set ( resolved . map ( ( entry ) => entry . sourcePath ) ) ] . sort (
293+ ( left , right ) => left . localeCompare ( right ) ,
294+ ) ;
265295 return {
266296 databasePaths,
267- // Buckets are provider-scoped rather than source-scoped. Treat all
268- // channel databases as one source so another environment cannot claim
269- // one database and accidentally merge duplicate buckets from another.
270- sourcePath :
271- hasOverride && databasePaths . length === 1
272- ? ( databasePaths [ 0 ] ?? dataDirectory )
273- : dataDirectory ,
297+ // Usage buckets are provider-scoped, so all instance databases must be
298+ // represented by one deterministic source fingerprint.
299+ sourcePath : sourcePaths . join ( ", " ) ,
274300 } ;
275301 } ) ;
276302
@@ -378,8 +404,11 @@ export const make = Effect.gen(function* () {
378404 const hostId = NodeOS . hostname ( ) ;
379405 // The home resolvers ask for `Path` themselves; satisfy them from the
380406 // instance we already hold so `readSummary` stays context-free.
381- const dirs = yield * resolveTranscriptDirs ( ) . pipe ( Effect . provideService ( Path . Path , path ) ) ;
382- const openCode = yield * resolveOpenCodeDatabases ( ) ;
407+ const resolvedSources = yield * resolveTranscriptDirs ( ) . pipe (
408+ Effect . provideService ( Path . Path , path ) ,
409+ ) ;
410+ const dirs = resolvedSources . dirs ;
411+ const openCode = yield * resolveOpenCodeDatabases ( resolvedSources . openCodeEnvironments ) ;
383412 const windowStart = DateTime . make ( `${ input . sinceDay } T00:00:00Z` ) ;
384413 if ( Option . isNone ( windowStart ) ) {
385414 return yield * new UsageReadError ( {
@@ -461,7 +490,12 @@ export const make = Effect.gen(function* () {
461490 }
462491
463492 if ( openCode . databasePaths . length > 0 ) {
464- const volumeId = yield * Effect . promise ( ( ) => readDirectoryVolumeId ( openCode . sourcePath ) ) ;
493+ const volumeIds = yield * Effect . forEach ( openCode . databasePaths , ( databasePath ) =>
494+ Effect . promise ( ( ) => readDirectoryVolumeId ( databasePath ) ) ,
495+ ) ;
496+ const volumeId = [ ...new Set ( volumeIds . filter ( ( id ) => id . length > 0 ) ) ]
497+ . sort ( ( left , right ) => left . localeCompare ( right ) )
498+ . join ( "|" ) ;
465499 const sessionIds = new Set < string > ( ) ;
466500 let existingDatabases = 0 ;
467501 let scannedFiles = 0 ;
0 commit comments