Skip to content

Commit 02bee71

Browse files
fix(shared): conflicting physical sources no longer merge through a shared legacy fingerprint
Two installations whose legacy fingerprints collide but whose physical source ids differ were joined through the legacy key, undercounting usage. Physical ids now take precedence; a legacy-only reading adopts a physical id only when its legacy key maps to exactly one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 993914e commit 02bee71

2 files changed

Lines changed: 59 additions & 20 deletions

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,45 @@ describe("mergeUsage", () => {
692692
expect(merged.duplicateSources).toHaveLength(1);
693693
});
694694

695+
it("keeps two directories apart when their Windows fingerprints collide but physical sources differ", () => {
696+
const pairs = ["a", "b"].map((name) => ({
697+
windows: { ...windows, physicalSourceId: `physical-${name}` },
698+
wsl: { ...wsl, physicalSourceId: `physical-${name}` },
699+
}));
700+
const environments = pairs.flatMap(({ windows: win, wsl: linux }, index) => [
701+
environment(`windows-${index}`, summary([bucket({ sourcePath: win.homePath })], [win])),
702+
environment(`wsl-${index}`, summary([bucket({ sourcePath: linux.homePath })], [linux])),
703+
]);
704+
705+
for (const ordered of [environments, environments.toReversed()]) {
706+
const merged = mergeUsage(ordered, USAGE_CONTRACT_VERSION);
707+
expect(merged.costUsd).toBe(20);
708+
expect(merged.sessions).toBe(284);
709+
expect(merged.duplicateSources).toHaveLength(2);
710+
}
711+
});
712+
713+
it("keeps a legacy-only reading apart when its fingerprint matches two physical sources", () => {
714+
const { physicalSourceId: _omitted, ...legacy } = windows;
715+
const merged = mergeUsage(
716+
[
717+
environment("pc-a", summary([bucket({ sourcePath: windows.homePath })], [windows])),
718+
environment(
719+
"pc-b",
720+
summary(
721+
[bucket({ sourcePath: windows.homePath })],
722+
[{ ...windows, physicalSourceId: "physical-other-pc" }],
723+
),
724+
),
725+
environment("standalone", summary([bucket({ sourcePath: legacy.homePath })], [legacy])),
726+
],
727+
USAGE_CONTRACT_VERSION,
728+
);
729+
730+
expect(merged.costUsd).toBe(30);
731+
expect(merged.duplicateSources).toHaveLength(0);
732+
});
733+
695734
it("joins a legacy-only reading to the WSL one through the Windows reading", () => {
696735
const { physicalSourceId: _omitted, ...legacy } = windows;
697736
const environments = [

‎packages/shared/src/usageMerge.ts‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -127,35 +127,35 @@ function fingerprintKey(fingerprint: UsageSourceFingerprint): string {
127127
/**
128128
* Maps each fingerprint to one key per physical directory. The legacy key cannot
129129
* match one directory read natively by Windows and through a WSL mount, since
130-
* path and `dev:ino` both differ, so `physicalSourceId` joins the two. It only
131-
* adds aliases: a server that omits it still matches on the legacy key, and
132-
* union-find keeps chains such as legacy-only, Windows, WSL in one group
133-
* whatever order they arrive in.
130+
* path and `dev:ino` both differ, so `physicalSourceId` joins the two and wins
131+
* over the legacy key: two installations whose legacy keys collide stay apart
132+
* when their physical sources differ. A server that omits it adopts the one
133+
* physical source seen at its legacy key. When that key carries several, the
134+
* reading cannot be attributed and keeps its legacy key, counting separately
135+
* rather than silently dropping one directory's usage.
134136
*/
135137
function sourceIdentity(
136138
environments: readonly EnvironmentUsage[],
137139
): (fingerprint: UsageSourceFingerprint) => string {
138-
const parent = new Map<string, string>();
139-
const find = (key: string): string => {
140-
let root = key;
141-
for (let next = parent.get(root); next !== undefined; next = parent.get(root)) root = next;
142-
// Path compression keeps long alias chains from making lookups quadratic.
143-
for (let node = key; node !== root;) {
144-
const next = parent.get(node)!;
145-
parent.set(node, root);
146-
node = next;
147-
}
148-
return root;
149-
};
140+
const physicalKey = (fingerprint: UsageSourceFingerprint) =>
141+
`\u0000${fingerprint.provider}\u0000${fingerprint.physicalSourceId}`;
142+
// null marks a legacy key seen with more than one physical source.
143+
const physicalByLegacy = new Map<string, string | null>();
150144
for (const environment of environments) {
151145
for (const { fingerprint } of environment.summary.sources) {
152146
if (fingerprint.physicalSourceId === undefined) continue;
153-
const legacy = find(fingerprintKey(fingerprint));
154-
const physical = find(`\u0000${fingerprint.provider}\u0000${fingerprint.physicalSourceId}`);
155-
if (legacy !== physical) parent.set(physical, legacy);
147+
const legacy = fingerprintKey(fingerprint);
148+
const physical = physicalKey(fingerprint);
149+
const seen = physicalByLegacy.get(legacy);
150+
if (seen === undefined) physicalByLegacy.set(legacy, physical);
151+
else if (seen !== physical) physicalByLegacy.set(legacy, null);
156152
}
157153
}
158-
return (fingerprint) => find(fingerprintKey(fingerprint));
154+
return (fingerprint) => {
155+
if (fingerprint.physicalSourceId !== undefined) return physicalKey(fingerprint);
156+
const legacy = fingerprintKey(fingerprint);
157+
return physicalByLegacy.get(legacy) ?? legacy;
158+
};
159159
}
160160

161161
function bucketsForSource(summary: UsageSummary, source: UsageSource): readonly UsageBucket[] {

0 commit comments

Comments
 (0)