Skip to content

Commit d4d5d12

Browse files
fix(shared): keep the newest shared usage scan (#10315)
Co-authored-by: shivam <[email protected]>
1 parent 1ab2dfb commit d4d5d12

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@ describe("mergeUsage", () => {
146146
).toEqual({ claude: 1, codex: 1 });
147147
});
148148

149+
it("uses the newest scan when environments share the same transcript directory", () => {
150+
const source = { provider: "claude" as const, hostId: "mac", homePath: "/home/theo/.claude" };
151+
const environments = [
152+
environment("env-a", summary([bucket({ costUsd: 4, records: 2 })], [source])),
153+
environment("env-b", {
154+
...summary([bucket()], [source]),
155+
readAt: "2026-08-07T01:00:00.000Z",
156+
}),
157+
];
158+
159+
for (const ordered of [environments, environments.toReversed()]) {
160+
const merged = mergeUsage(ordered, USAGE_CONTRACT_VERSION);
161+
expect(merged.costUsd).toBe(10);
162+
expect(merged.records).toBe(5);
163+
expect(merged.sessions).toBe(1);
164+
expect(merged.contributingEnvironments).toEqual(["env-b"]);
165+
expect(merged.duplicateSources).toEqual(["env-a: /home/theo/.claude"]);
166+
}
167+
});
168+
149169
it("excludes an environment reporting an older contract version", () => {
150170
const merged = mergeUsage(
151171
[

‎packages/shared/src/usageMerge.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ function fingerprintKey(fingerprint: UsageSourceFingerprint): string {
118118
*
119119
* Several environments on one machine (worktree servers, for instance) resolve
120120
* the same provider home and would otherwise double count every token. The
121-
* first environment in a stable order claims a fingerprint; the rest have that
122-
* provider's buckets dropped. Environments are sorted by id so the winner does
123-
* not change between renders.
121+
* most recently read summary claims a fingerprint; the rest have that provider's
122+
* buckets dropped. Environment ids break ties so the winner is stable when
123+
* summaries have the same read time.
124124
*/
125125
function claimSources(environments: readonly EnvironmentUsage[]): {
126126
readonly ownerByFingerprint: ReadonlyMap<string, EnvironmentId>;
@@ -129,7 +129,11 @@ function claimSources(environments: readonly EnvironmentUsage[]): {
129129
const ownerByFingerprint = new Map<string, EnvironmentId>();
130130
const duplicates: string[] = [];
131131

132-
const ordered = [...environments].sort((a, b) => a.environmentId.localeCompare(b.environmentId));
132+
const ordered = [...environments].sort(
133+
(a, b) =>
134+
(Date.parse(b.summary.readAt) || 0) - (Date.parse(a.summary.readAt) || 0) ||
135+
a.environmentId.localeCompare(b.environmentId),
136+
);
133137

134138
for (const environment of ordered) {
135139
for (const source of environment.summary.sources) {

0 commit comments

Comments
 (0)