Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([dup-detect-collection-count-undercount]): sum collection items per universe

[._.]/ Adam Eivyd0fe842b2a3c
diff --git a/server/services/duplicateDetection.js b/server/services/duplicateDetection.js
index 6dd70c218d..e696754bd9 100644
--- a/server/services/duplicateDetection.js
+++ b/server/services/duplicateDetection.js
@@ -74,7 +74,10 @@ export async function findDuplicateUniverseGroups() {
const collectionItemsByUniverse = new Map();
for (const c of collections) {
if (c.deleted || !c.universeId) continue;
- collectionItemsByUniverse.set(c.universeId, (c.items || []).length);
+ collectionItemsByUniverse.set(
+ c.universeId,
+ (collectionItemsByUniverse.get(c.universeId) || 0) + (c.items || []).length,
+ );
}
const groups = groupDuplicates(live, (u) => normName(u.name));
diff --git a/server/services/duplicateDetection.test.js b/server/services/duplicateDetection.test.js
index c5f2ef3d84..83c97ebee8 100644
--- a/server/services/duplicateDetection.test.js
+++ b/server/services/duplicateDetection.test.js
@@ -19,6 +19,7 @@ vi.mock('crypto', async () => {
const universeSvc = await import('./universeBuilder.js');
const seriesSvc = await import('./pipeline/series.js');
+const mediaCollections = await import('./mediaCollections.js');
const dup = await import('./duplicateDetection.js');
afterAll(() => rmSync(TEST_DATA_ROOT, { recursive: true, force: true }));
@@ -65,6 +66,34 @@ describe('duplicateDetection', () => {
const rec = groups[0].records.find((r) => r.id === u1.id);
expect(rec.linkedSeriesCount).toBe(2);
});
+
+ it('sums linkedCollectionItemCount across all collections sharing a universeId', async () => {
+ // Two universes share a name → a duplicate group. The first owns TWO
+ // collections (the deterministic `uc-` bucket plus a stray same-universeId
+ // record that a sync merge could leave behind). The count must add both
+ // collections' items, not last-wins on whichever the scan reads last.
+ const u1 = await universeSvc.createUniverse({ name: 'WithCollections' });
+ await universeSvc.createUniverse({ name: 'WithCollections' });
+ const now = new Date().toISOString();
+ const seed = (id, itemCount) => mediaCollections.mediaCollectionStore().saveOneNow(id, {
+ id,
+ name: `${id}-name`,
+ description: '',
+ coverKey: null,
+ universeId: u1.id,
+ items: Array.from({ length: itemCount }, (_, i) => ({
+ kind: 'image', ref: `${id}-item-${i}`, key: `${id}-item-${i}`,
+ })),
+ createdAt: now,
+ updatedAt: now,
+ });
+ await seed(`uc-${u1.id}`, 3);
+ await seed('stray-merged-bucket', 2);
+
+ const groups = await dup.findDuplicateUniverseGroups();
+ const rec = groups[0].records.find((r) => r.id === u1.id);
+ expect(rec.linkedCollectionItemCount).toBe(5);
+ });
});
describe('findDuplicateSeriesGroups', () => {