Skip to content

Commit d6ef065

Browse files
fix(usage): count sessions only for surviving records
1 parent f8b6a36 commit d6ef065

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,17 @@ describe("UsageAggregator", () => {
184184
const original = new Map(
185185
[
186186
...new Map<string, readonly UsageRecord[]>([
187-
["/partial.jsonl", early],
187+
["/partial.jsonl", early.map((entry) => ({ ...entry, sessionId: "session-fork" }))],
188188
["/complete.jsonl", dedupeWithinFile([...early, ...complete, ...complete])],
189189
[
190190
"/deleted.jsonl",
191-
[{ ...early[0]!, totals: { ...early[0]!.totals, cacheCreationTokens: 30000 } }],
191+
[
192+
{
193+
...early[0]!,
194+
sessionId: "session-deleted",
195+
totals: { ...early[0]!.totals, cacheCreationTokens: 30000 },
196+
},
197+
],
192198
],
193199
]),
194200
].map(
@@ -233,6 +239,7 @@ describe("UsageAggregator", () => {
233239
bucket.totals.cacheCreationTokens,
234240
bucket.totals.outputTokens,
235241
bucket.records,
242+
bucket.sessions,
236243
]),
237244
).toEqual(
238245
expected
@@ -242,6 +249,7 @@ describe("UsageAggregator", () => {
242249
entry.cache_creation_input_tokens,
243250
entry.output_tokens,
244251
1,
252+
1,
245253
]),
246254
);
247255
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ export class UsageAggregator {
233233
}
234234
// Keyed usage is counted in finish(), once all its copies are merged.
235235
if (record.dedupeKey === null) this.#accumulate(bucket, record);
236-
if (record.sessionId.length > 0) bucket.sessions.add(record.sessionId);
237236
return true;
238237
}
239238

@@ -262,6 +261,7 @@ export class UsageAggregator {
262261
}
263262

264263
#accumulate(bucket: MutableBucket, record: UsageRecord): void {
264+
if (record.sessionId.length > 0) bucket.sessions.add(record.sessionId);
265265
const priced = priceUsage(this.#options.rates, record, this.#options.priceOverrides);
266266

267267
const totals = bucket.totals;

0 commit comments

Comments
 (0)