Skip to content

Commit 84d43e4

Browse files
fix(usage): dedupe OpenCode databases by source
1 parent daf4731 commit 84d43e4

8 files changed

Lines changed: 129 additions & 70 deletions

File tree

‎apps/server/src/usage/UsageService.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ it.effect("includes an OpenCode instance database override in the summary", () =
115115
});
116116

117117
expect(
118-
summary.sources.find((source) => source.fingerprint.provider === "opencode"),
118+
summary.sources.find(
119+
(source) =>
120+
source.fingerprint.provider === "opencode" &&
121+
source.fingerprint.resolvedHomePath === databasePath,
122+
),
119123
).toMatchObject({
120124
status: "ok",
121125
scannedFiles: 1,
@@ -125,6 +129,7 @@ it.effect("includes an OpenCode instance database override in the summary", () =
125129
});
126130
expect(summary.buckets.find((bucket) => bucket.provider === "opencode")).toMatchObject({
127131
day: "2026-08-27",
132+
sourcePath: databasePath,
128133
model: "openai/gpt-5.4",
129134
totals: {
130135
uncachedInputTokens: 5,

‎apps/server/src/usage/UsageService.ts‎

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -268,36 +268,17 @@ export const make = Effect.gen(function* () {
268268
const directoryEntries = yield* fileSystem
269269
.readDirectory(dataDirectory)
270270
.pipe(Effect.catchCause(() => Effect.succeed([] as const)));
271-
const databasePaths = resolveOpenCodeDatabasePaths({
271+
return resolveOpenCodeDatabasePaths({
272272
dataDirectory,
273273
databaseOverride: environment["OPENCODE_DB"],
274274
disableChannelDatabase: environment["OPENCODE_DISABLE_CHANNEL_DB"],
275275
directoryEntries,
276276
path,
277277
});
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-
};
286278
}),
287279
);
288280

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-
);
295-
return {
296-
databasePaths,
297-
// Usage buckets are provider-scoped, so all instance databases must be
298-
// represented by one deterministic source fingerprint.
299-
sourcePath: sourcePaths.join(", "),
300-
};
281+
return [...new Set(resolved.flat())].sort((left, right) => left.localeCompare(right));
301282
});
302283

303284
/**
@@ -408,7 +389,9 @@ export const make = Effect.gen(function* () {
408389
Effect.provideService(Path.Path, path),
409390
);
410391
const dirs = resolvedSources.dirs;
411-
const openCode = yield* resolveOpenCodeDatabases(resolvedSources.openCodeEnvironments);
392+
const openCodeDatabasePaths = yield* resolveOpenCodeDatabases(
393+
resolvedSources.openCodeEnvironments,
394+
);
412395
const windowStart = DateTime.make(`${input.sinceDay}T00:00:00Z`);
413396
if (Option.isNone(windowStart)) {
414397
return yield* new UsageReadError({
@@ -489,62 +472,47 @@ export const make = Effect.gen(function* () {
489472
});
490473
}
491474

492-
if (openCode.databasePaths.length > 0) {
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("|");
475+
for (const databasePath of openCodeDatabasePaths) {
476+
const volumeId = yield* Effect.promise(() => readDirectoryVolumeId(databasePath));
499477
const sessionIds = new Set<string>();
500-
let existingDatabases = 0;
501-
let scannedFiles = 0;
502-
let skippedFiles = 0;
503-
let malformedRecords = 0;
504-
505-
for (const databasePath of openCode.databasePaths) {
506-
const exists = yield* fileSystem
507-
.exists(databasePath)
508-
.pipe(Effect.catchCause(() => Effect.succeed(false)));
509-
if (!exists) continue;
510-
existingDatabases += 1;
511-
512-
const result = yield* Effect.promise(() => readOpenCodeUsage(databasePath, windowStartMs));
513-
if (result === null) {
514-
skippedFiles += 1;
515-
continue;
516-
}
478+
const exists = yield* fileSystem
479+
.exists(databasePath)
480+
.pipe(Effect.catchCause(() => Effect.succeed(false)));
481+
const result = exists
482+
? yield* Effect.promise(() => readOpenCodeUsage(databasePath, windowStartMs))
483+
: null;
484+
const failed = exists && result === null;
485+
const malformedRecords = result?.malformedRecords ?? 0;
517486

518-
scannedFiles += 1;
519-
malformedRecords += result.malformedRecords;
487+
if (result !== null) {
520488
for (const record of result.records) {
521-
if (aggregator.add(record) && record.sessionId.length > 0) {
489+
if (
490+
aggregator.add({ ...record, sourcePath: databasePath }) &&
491+
record.sessionId.length > 0
492+
) {
522493
sessionIds.add(record.sessionId);
523494
}
524495
}
525496
}
526497

527-
const missing = existingDatabases === 0;
528-
const failed = existingDatabases > 0 && scannedFiles === 0;
529-
const partial = !missing && !failed && (skippedFiles > 0 || malformedRecords > 0);
530498
sources.push({
531499
fingerprint: {
532500
hostId,
533501
provider: "opencode",
534-
resolvedHomePath: openCode.sourcePath,
502+
resolvedHomePath: databasePath,
535503
volumeId,
536504
},
537-
status: missing ? "missing" : failed ? "failed" : partial ? "partial" : "ok",
538-
scannedFiles,
539-
skippedFiles,
505+
status: !exists ? "missing" : failed ? "failed" : malformedRecords > 0 ? "partial" : "ok",
506+
scannedFiles: result === null ? 0 : 1,
507+
skippedFiles: failed ? 1 : 0,
540508
malformedRecords,
541509
distinctSessions: sessionIds.size,
542-
message: missing
510+
message: !exists
543511
? "No usage database on this environment."
544512
: failed
545513
? "The usage database could not be read."
546-
: partial
547-
? "Some OpenCode usage databases or rows were skipped."
514+
: malformedRecords > 0
515+
? "Some OpenCode usage rows were skipped."
548516
: null,
549517
});
550518
}

‎apps/server/src/usage/usageAggregation.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,16 @@ describe("UsageAggregator", () => {
201201

202202
expect(result.buckets).toHaveLength(3);
203203
});
204+
205+
it("keeps independently persisted sources in separate buckets", () => {
206+
const result = aggregate([
207+
record({ provider: "opencode", sourcePath: "/data/opencode.db" }),
208+
record({ provider: "opencode", sourcePath: "/work/opencode.db" }),
209+
]);
210+
211+
expect(result.buckets.map((bucket) => bucket.sourcePath)).toEqual([
212+
"/data/opencode.db",
213+
"/work/opencode.db",
214+
]);
215+
});
204216
});

‎apps/server/src/usage/usageAggregation.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @effect-diagnostics globalDate:off
22
/**
3-
* Folds parsed transcript records into `(day, hourStart?, provider, model)`
4-
* buckets.
3+
* Folds parsed transcript records into
4+
* `(day, hourStart?, provider, sourcePath?, model)` buckets.
55
*
66
* `Intl.DateTimeFormat` is the only reliable way to resolve a wall-clock day in
77
* an arbitrary IANA zone, and it takes a `Date`. That is why the raw `Date`
@@ -145,7 +145,7 @@ export class UsageAggregator {
145145
this.#hourlyWindow.sinceTimeMs +
146146
Math.floor((record.timestampMs - this.#hourlyWindow.sinceTimeMs) / HOUR_MS) * HOUR_MS,
147147
).toISOString();
148-
const key = `${day}\u0000${hourStart}\u0000${record.provider}\u0000${record.model}`;
148+
const key = `${day}\u0000${hourStart}\u0000${record.provider}\u0000${record.sourcePath ?? ""}\u0000${record.model}`;
149149
let bucket = this.#buckets.get(key);
150150
if (bucket === undefined) {
151151
bucket = {
@@ -180,11 +180,13 @@ export class UsageAggregator {
180180
finish(): AggregateResult {
181181
const buckets: UsageBucket[] = [];
182182
for (const [key, bucket] of this.#buckets) {
183-
const [day = "", hourStart = "", provider = "", model = ""] = key.split("\u0000");
183+
const [day = "", hourStart = "", provider = "", sourcePath = "", model = ""] =
184+
key.split("\u0000");
184185
buckets.push({
185186
day: day as UsageDay,
186187
...(hourStart === "" ? {} : { hourStart }),
187188
provider: provider as UsageBucket["provider"],
189+
...(sourcePath === "" ? {} : { sourcePath }),
188190
model,
189191
totals: bucket.totals,
190192
costUsd: bucket.costUsd,
@@ -201,6 +203,7 @@ export class UsageAggregator {
201203
a.day.localeCompare(b.day) ||
202204
(a.hourStart ?? "").localeCompare(b.hourStart ?? "") ||
203205
a.provider.localeCompare(b.provider) ||
206+
(a.sourcePath ?? "").localeCompare(b.sourcePath ?? "") ||
204207
a.model.localeCompare(b.model),
205208
);
206209

‎apps/server/src/usage/usageTranscripts.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export type UsageTranscriptProviderKind = Exclude<UsageProviderKind, "opencode">
1212

1313
export interface UsageRecord {
1414
readonly provider: UsageProviderKind;
15+
/** Source provenance for providers that can read several independent stores. */
16+
readonly sourcePath?: string;
1517
readonly timestampMs: number;
1618
readonly model: string;
1719
readonly sessionId: string;

‎packages/contracts/src/usage.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
* stays complete even for turns that were never driven through T3 Code. This
99
* mirrors the approach `ccusage` takes.
1010
*
11-
* Environments return pre-aggregated `(day, hourStart?, provider, model)`
12-
* buckets. Raw transcript records never cross the wire.
11+
* Environments return pre-aggregated
12+
* `(day, hourStart?, provider, sourcePath?, model)` buckets. Raw transcript
13+
* records never cross the wire.
1314
*
1415
* @module usage
1516
*/
@@ -27,7 +28,8 @@ export const USAGE_CONTRACT_VERSION = 6 as const;
2728
/**
2829
* Oldest {@link UsageSummary} version a current client will still merge.
2930
*
30-
* v5 only adds `grok` to {@link UsageProviderKind}; v6 adds `opencode`.
31+
* v5 only adds `grok` to {@link UsageProviderKind}; v6 adds `opencode` and
32+
* optional per-source bucket provenance.
3133
* v4 Claude/Codex buckets remain valid, so mixed-version environments keep
3234
* those totals instead of treating every older server as stale.
3335
*/
@@ -82,8 +84,10 @@ export const UsageTokenTotals = Schema.Struct({
8284
export type UsageTokenTotals = typeof UsageTokenTotals.Type;
8385

8486
/**
85-
* One `(day, hourStart?, provider, model)` cell. `hourStart` is the UTC start
86-
* instant of a rolling bucket and is present only for hourly requests.
87+
* One `(day, hourStart?, provider, sourcePath?, model)` cell. `hourStart` is
88+
* the UTC start instant of a rolling bucket and is present only for hourly
89+
* requests. `sourcePath` links a bucket to one source when a provider can use
90+
* several stores; older summaries and single-source providers omit it.
8791
*
8892
* `costUsd` is the raw API-equivalent cost of these tokens. It is not money
8993
* spent: subscription plans bill separately. `unpricedRecords` counts records
@@ -94,6 +98,7 @@ export const UsageBucket = Schema.Struct({
9498
day: UsageDay,
9599
hourStart: Schema.optional(TrimmedNonEmptyString),
96100
provider: UsageProviderKind,
101+
sourcePath: Schema.optional(TrimmedNonEmptyString),
97102
model: TrimmedNonEmptyString,
98103
totals: UsageTokenTotals,
99104
costUsd: Schema.Number,

‎packages/shared/src/usageMerge.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,62 @@ describe("mergeUsage", () => {
257257
expect(merged.duplicateSources).toHaveLength(1);
258258
});
259259

260+
it("deduplicates shared provider storage while keeping unique sources", () => {
261+
const shared = {
262+
provider: "opencode" as const,
263+
hostId: "mac",
264+
homePath: "/data/opencode.db",
265+
volumeId: "16777220:1234",
266+
};
267+
const merged = mergeUsage(
268+
[
269+
environment(
270+
"env-a",
271+
summary(
272+
[
273+
bucket({ provider: "opencode", sourcePath: shared.homePath, costUsd: 10 }),
274+
bucket({ provider: "opencode", sourcePath: "/work/a.db", costUsd: 3 }),
275+
],
276+
[
277+
shared,
278+
{
279+
provider: "opencode",
280+
hostId: "mac",
281+
homePath: "/work/a.db",
282+
volumeId: "16777220:2000",
283+
},
284+
],
285+
),
286+
),
287+
environment(
288+
"env-b",
289+
summary(
290+
[
291+
bucket({ provider: "opencode", sourcePath: shared.homePath, costUsd: 10 }),
292+
bucket({ provider: "opencode", sourcePath: "/work/b.db", costUsd: 4 }),
293+
],
294+
[
295+
shared,
296+
{
297+
provider: "opencode",
298+
hostId: "mac",
299+
homePath: "/work/b.db",
300+
volumeId: "16777220:3000",
301+
},
302+
],
303+
),
304+
),
305+
],
306+
USAGE_CONTRACT_VERSION,
307+
);
308+
309+
expect(merged.costUsd).toBe(17);
310+
expect(merged.records).toBe(15);
311+
expect(merged.sessions).toBe(3);
312+
expect(merged.duplicateSources).toEqual(["env-b: /data/opencode.db"]);
313+
expect(merged.contributingEnvironments).toEqual(["env-a", "env-b"]);
314+
});
315+
260316
it("totals sessions from per-directory distinct counts, not per-bucket sums", () => {
261317
// One session that spans two days appears in two buckets. Summing bucket
262318
// sessions would say 2; the source's distinct count says 1.

‎packages/shared/src/usageMerge.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,17 @@ function ownedContribution(
142142
readonly sessionsByProvider: ReadonlyMap<UsageProviderKind, number>;
143143
} {
144144
const ownedProviders = new Set<UsageProviderKind>();
145+
const ownedSourcePaths = new Map<UsageProviderKind, Set<string>>();
145146
const sessionsByProvider = new Map<UsageProviderKind, number>();
146147
for (const source of environment.summary.sources) {
147148
if (source.status === "missing") continue;
148149
const key = fingerprintKey(source.fingerprint);
149150
if (ownerByFingerprint.get(key) === environment.environmentId) {
150151
const provider = source.fingerprint.provider;
151152
ownedProviders.add(provider);
153+
const providerSourcePaths = ownedSourcePaths.get(provider) ?? new Set<string>();
154+
providerSourcePaths.add(source.fingerprint.resolvedHomePath);
155+
ownedSourcePaths.set(provider, providerSourcePaths);
152156
// Distinct within a directory. Summing per-bucket session counts instead
153157
// would count a session once per day and model it spans.
154158
sessionsByProvider.set(
@@ -158,7 +162,11 @@ function ownedContribution(
158162
}
159163
}
160164
return {
161-
buckets: environment.summary.buckets.filter((bucket) => ownedProviders.has(bucket.provider)),
165+
buckets: environment.summary.buckets.filter((bucket) =>
166+
bucket.sourcePath === undefined
167+
? ownedProviders.has(bucket.provider)
168+
: (ownedSourcePaths.get(bucket.provider)?.has(bucket.sourcePath) ?? false),
169+
),
162170
sessionsByProvider,
163171
};
164172
}

0 commit comments

Comments
 (0)