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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix(catalog): track intra-batch write timestamps to prevent stale duplicate ingredient overwrite (#4006)
[._.]/ Adam Eivyd69567087186
diff --git a/server/services/catalogCanonProjection.batch.test.js b/server/services/catalogCanonProjection.batch.test.jsindex 500e4fca2d..7ca7a3dc88 100644--- a/server/services/catalogCanonProjection.batch.test.js+++ b/server/services/catalogCanonProjection.batch.test.js@@ -165,4 +165,51 @@ describe('projectToCatalog batching', () => {expect(await projectToCatalog('universe-1', null)).toEqual({ written: 0, skipped: 0 });expect(catalogDB.getIngredientTimestamps).not.toHaveBeenCalled();});++ it('prevents older duplicate ingredientId from overwriting newer entry in same batch (#4006)', async () => {+ catalogDB.getIngredientTimestamps.mockResolvedValue(timestampMap([['ing-dup', ROW_AT]]));++ const stats = await projectToCatalog('universe-1', {+ characters: [+ entry('ing-dup', { name: 'Newer Entry', updatedAt: NEWER }),+ entry('ing-dup', { name: 'Stale Duplicate', updatedAt: OLDER }),+ ],+ });++ // Newer entry writes first; older duplicate entry is evaluated against appliedTimestamps (NEWER) and skipped+ expect(catalogDB.updateIngredient).toHaveBeenCalledTimes(1);+ expect(catalogDB.updateIngredient).toHaveBeenCalledWith(+ 'ing-dup',+ { name: 'Newer Entry', payload: expect.anything() },+ expect.anything(),+ );+ expect(stats).toEqual({ written: 1, skipped: 1 });+ });++ it('allows newer duplicate ingredientId to update older entry written earlier in same batch (#4006)', async () => {+ catalogDB.getIngredientTimestamps.mockResolvedValue(timestampMap([['ing-dup', ROW_AT]]));++ const stats = await projectToCatalog('universe-1', {+ characters: [+ entry('ing-dup', { name: 'Older First', updatedAt: ROW_AT }),+ entry('ing-dup', { name: 'Newer Second', updatedAt: NEWER }),+ ],+ });++ expect(catalogDB.updateIngredient).toHaveBeenCalledTimes(2);+ expect(catalogDB.updateIngredient).toHaveBeenNthCalledWith(+ 1,+ 'ing-dup',+ { name: 'Older First', payload: expect.anything() },+ expect.anything(),+ );+ expect(catalogDB.updateIngredient).toHaveBeenNthCalledWith(+ 2,+ 'ing-dup',+ { name: 'Newer Second', payload: expect.anything() },+ expect.anything(),+ );+ expect(stats).toEqual({ written: 2, skipped: 0 });+ });});+diff --git a/server/services/catalogCanonProjection.js b/server/services/catalogCanonProjection.jsindex 57db85e82d..af001b9ccc 100644--- a/server/services/catalogCanonProjection.js+++ b/server/services/catalogCanonProjection.js@@ -197,8 +197,13 @@ export async function projectToCatalog(universeId, canonArrays, { guardToken = n// by design: each updateIngredient is a multi-statement write (sanitize +// update + revision), and firing N of them at once would trade the read// stampede this fix removed for a write one.+ //+ // Track writes applied within this call in `appliedTimestamps` so if multiple+ // candidates reference the same ingredientId in one batch, an older entry+ // cannot overwrite a newer entry written earlier in the same pass (#4006).+ const appliedTimestamps = new Map(timestamps);for (const { ingredientId, entry } of candidates) {- const rowUpdatedAt = timestamps.get(ingredientId);+ const rowUpdatedAt = appliedTimestamps.get(ingredientId);if (rowUpdatedAt === undefined) { stats.skipped++; continue; } // no live row// LWW on updatedAt — only overwrite the catalog row when the embedded entry// is at-least-as-fresh. An older embedded snapshot (e.g. a concurrent@@ -212,6 +217,7 @@ export async function projectToCatalog(universeId, canonArrays, { guardToken = n{ name: entry.name, payload: entryToPayload(entry) },{ source: 'sync', actor: 'canon-projection' },);+ appliedTimestamps.set(ingredientId, entry.updatedAt);stats.written++;} catch (err) {console.error(`🔁 projectToCatalog: ingredient ${ingredientId} update failed: ${err.message}`);