Skip to content

Commit db3361c

Browse files
fix(shared): usage sources match on either legacy key or physical source
Two desktop installs on one PC persist different host scopes, so one directory they both read carries two physical IDs but one legacy key. Join sources when either key matches instead of letting the physical ID override the legacy match. AI-assisted; verified with the tests and tools listed above.
1 parent 25b306f commit db3361c

2 files changed

Lines changed: 22 additions & 44 deletions

File tree

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

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -661,43 +661,24 @@ describe("mergeUsage", () => {
661661
expect(merged.duplicateSources).toHaveLength(1);
662662
});
663663

664-
it("keeps two directories apart when their Windows fingerprints collide but physical sources differ", () => {
665-
const pairs = ["a", "b"].map((name) => ({
666-
windows: { ...windows, physicalSourceId: `physical-${name}` },
667-
wsl: { ...wsl, physicalSourceId: `physical-${name}` },
668-
}));
669-
const environments = pairs.flatMap(({ windows: win, wsl: linux }, index) => [
670-
environment(`windows-${index}`, summary([bucket({ sourcePath: win.homePath })], [win])),
671-
environment(`wsl-${index}`, summary([bucket({ sourcePath: linux.homePath })], [linux])),
672-
]);
673-
674-
for (const ordered of [environments, environments.toReversed()]) {
675-
const merged = mergeUsage(ordered, USAGE_CONTRACT_VERSION);
676-
expect(merged.costUsd).toBe(20);
677-
expect(merged.sessions).toBe(284);
678-
expect(merged.duplicateSources).toHaveLength(2);
679-
}
680-
});
681-
682-
it("keeps a legacy-only reading apart when its fingerprint matches two physical sources", () => {
683-
const { physicalSourceId: _omitted, ...legacy } = windows;
664+
it("counts it once when two desktop installs with different scopes read it", () => {
684665
const merged = mergeUsage(
685666
[
686-
environment("pc-a", summary([bucket({ sourcePath: windows.homePath })], [windows])),
667+
environment("packaged", summary([bucket({ sourcePath: windows.homePath })], [windows])),
687668
environment(
688-
"pc-b",
669+
"dev-build",
689670
summary(
690671
[bucket({ sourcePath: windows.homePath })],
691-
[{ ...windows, physicalSourceId: "physical-other-pc" }],
672+
[{ ...windows, physicalSourceId: "physical-other-scope" }],
692673
),
693674
),
694-
environment("standalone", summary([bucket({ sourcePath: legacy.homePath })], [legacy])),
695675
],
696676
USAGE_CONTRACT_VERSION,
697677
);
698678

699-
expect(merged.costUsd).toBe(30);
700-
expect(merged.duplicateSources).toHaveLength(0);
679+
expect(merged.costUsd).toBe(10);
680+
expect(merged.sessions).toBe(142);
681+
expect(merged.duplicateSources).toHaveLength(1);
701682
});
702683
});
703684

‎packages/shared/src/usageMerge.ts‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,32 +118,29 @@ function fingerprintKey(fingerprint: UsageSourceFingerprint): string {
118118
].join(" ");
119119
}
120120

121-
// physicalSourceId joins one directory read natively by Windows and through a WSL mount
122-
// (path and dev:ino both differ) and wins over the legacy key. A fingerprint without one
123-
// adopts the single physical source seen at its legacy key; when that key carries several,
124-
// it keeps its legacy key and counts separately rather than dropping a directory's usage.
121+
// Sources are one directory when either their legacy key or their physicalSourceId matches.
122+
// physicalSourceId joins a directory read natively by Windows and through a WSL mount (path
123+
// and dev:ino both differ); the legacy key still joins installs whose host scopes differ.
125124
function sourceIdentity(
126125
environments: readonly EnvironmentUsage[],
127126
): (fingerprint: UsageSourceFingerprint) => string {
128-
const physicalKey = (fingerprint: UsageSourceFingerprint) =>
129-
`\u0000${fingerprint.provider}\u0000${fingerprint.physicalSourceId}`;
130-
// null marks a legacy key seen with more than one physical source.
131-
const physicalByLegacy = new Map<string, string | null>();
127+
const parent = new Map<string, string>();
128+
const find = (key: string): string => {
129+
const next = parent.get(key);
130+
if (next === undefined || next === key) return key;
131+
const root = find(next);
132+
parent.set(key, root);
133+
return root;
134+
};
132135
for (const environment of environments) {
133136
for (const { fingerprint } of environment.summary.sources) {
134137
if (fingerprint.physicalSourceId === undefined) continue;
135-
const legacy = fingerprintKey(fingerprint);
136-
const physical = physicalKey(fingerprint);
137-
const seen = physicalByLegacy.get(legacy);
138-
if (seen === undefined) physicalByLegacy.set(legacy, physical);
139-
else if (seen !== physical) physicalByLegacy.set(legacy, null);
138+
const legacy = find(fingerprintKey(fingerprint));
139+
const physical = find(`\u0000${fingerprint.provider}\u0000${fingerprint.physicalSourceId}`);
140+
if (legacy !== physical) parent.set(legacy, physical);
140141
}
141142
}
142-
return (fingerprint) => {
143-
if (fingerprint.physicalSourceId !== undefined) return physicalKey(fingerprint);
144-
const legacy = fingerprintKey(fingerprint);
145-
return physicalByLegacy.get(legacy) ?? legacy;
146-
};
143+
return (fingerprint) => find(fingerprintKey(fingerprint));
147144
}
148145

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

0 commit comments

Comments
 (0)