Skip to content

Commit 78eded7

Browse files
committed
fix(shared): keep the newest shared usage scan
1 parent 0c200c5 commit 78eded7

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
@@ -105,9 +105,9 @@ function fingerprintKey(fingerprint: UsageSourceFingerprint): string {
105105
*
106106
* Several environments on one machine (worktree servers, for instance) resolve
107107
* the same provider home and would otherwise double count every token. The
108-
* first environment in a stable order claims a fingerprint; the rest have that
109-
* provider's buckets dropped. Environments are sorted by id so the winner does
110-
* not change between renders.
108+
* most recently read summary claims a fingerprint; the rest have that provider's
109+
* buckets dropped. Environment ids break ties so the winner is stable when
110+
* summaries have the same read time.
111111
*/
112112
function claimSources(environments: readonly EnvironmentUsage[]): {
113113
readonly ownerByFingerprint: ReadonlyMap<string, EnvironmentId>;
@@ -116,7 +116,11 @@ function claimSources(environments: readonly EnvironmentUsage[]): {
116116
const ownerByFingerprint = new Map<string, EnvironmentId>();
117117
const duplicates: string[] = [];
118118

119-
const ordered = [...environments].sort((a, b) => a.environmentId.localeCompare(b.environmentId));
119+
const ordered = [...environments].sort(
120+
(a, b) =>
121+
(Date.parse(b.summary.readAt) || 0) - (Date.parse(a.summary.readAt) || 0) ||
122+
a.environmentId.localeCompare(b.environmentId),
123+
);
120124

121125
for (const environment of ordered) {
122126
for (const source of environment.summary.sources) {

0 commit comments

Comments
 (0)