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
stop render bookkeeping from bumping a deck card's federated updated_at (#7364)
[._.]/ Adam Eivyfc9db72c2079
diff --git a/server/services/decks.js b/server/services/decks.jsindex 9d15ad824e..55413fcf61 100644--- a/server/services/decks.js+++ b/server/services/decks.js@@ -172,8 +172,16 @@ async function patchDeck(id, mutate) {* returns false to skip the write. Resolves with the projected card, or null* when the card is gone / the write was skipped and `missingOk` is set (the* completion hook's contract: never throw on a card that vanished).+ *+ * `touch` bumps the DECK's `updatedAt` (the LWW clock the push compares on);+ * `touchCard` bumps the CARD row's. Both default on, and both must be off for+ * a write that only edits machine-local render bookkeeping: the card's+ * `updatedAt` rides the wire and is hashed (only `id`, `deckId` and `render`+ * are stripped — `syncWire.js`), so moving it on a purely local write shifts+ * the deck's content hash and manufactures a phantom conflict on the next+ * push from a peer.*/-async function patchCard(deckId, cardId, mutate, { missingOk = false, touch = true } = {}) {+async function patchCard(deckId, cardId, mutate, { missingOk = false, touch = true, touchCard = true } = {}) {await ensureSchema();return queueRecordWrite(deckId, () => withTransaction(async (client) => {const { rows: deckRows } = await client.query('SELECT deleted FROM decks WHERE id = $1', [deckId]);@@ -189,7 +197,8 @@ async function patchCard(deckId, cardId, mutate, { missingOk = false, touch = trconst d = { ...(rows[0].definition || {}) };if (mutate(d) === false) return null;const { rows: written } = await client.query(- 'UPDATE deck_cards SET definition = $2, updated_at = NOW() WHERE id = $1 RETURNING *', [cardId, d],+ `UPDATE deck_cards SET definition = $2${touchCard ? ', updated_at = NOW()' : ''} WHERE id = $1 RETURNING *`,+ [cardId, d],);if (touch) await client.query('UPDATE decks SET updated_at = NOW() WHERE id = $1', [deckId]);return projectCard(written[0]);@@ -355,6 +364,11 @@ export async function applyCardGenerations(deckId, entries = []) {* `entries` is `[{ cardId, render }]`. A card the completion hook already* filed for the SAME job (a backend that finished before the stamp landed)* keeps its completed record — the queued stamp must not regress it.+ *+ * Deliberately leaves `updated_at` alone on both tables: which job a card is+ * waiting on is machine-local bookkeeping the wire strips, so bumping the+ * card's clock would move the deck's sync content hash for a state that means+ * nothing on a peer (#7364).*/export async function markCardsRenderQueued(deckId, entries = []) {if (!entries.length) return;@@ -362,7 +376,7 @@ export async function markCardsRenderQueued(deckId, entries = []) {const stampedAt = new Date().toISOString();await queueRecordWrite(deckId, () => query(`UPDATE deck_cards c- SET definition = c.definition || jsonb_build_object('render', v.render), updated_at = NOW()+ SET definition = c.definition || jsonb_build_object('render', v.render)FROM unnest($2::uuid[], $3::jsonb[]) AS v(id, render)WHERE c.deck_id = $1 AND c.id = v.idAND COALESCE(c.definition->'render'->>'jobId', '') <> v.render->>'jobId'`,@@ -373,13 +387,15 @@ export async function markCardsRenderQueued(deckId, entries = []) {/*** Terminal state for a render that never produced a file. Only the job the* card is currently waiting on may flip it — a stale failure event for a- * superseded job must not mark a newer queued render as failed.+ * superseded job must not mark a newer queued render as failed. Like the+ * queued stamp above this edits only the wire-stripped `render` record, so+ * neither clock moves (#7364).*/export function markCardRenderTerminal(deckId, cardId, { jobId, status, error = null }) {return patchCard(deckId, cardId, (d) => {if (!d.render || d.render.jobId !== jobId) return false;d.render = { ...d.render, status, error: error || null, updatedAt: new Date().toISOString() };- }, { missingOk: true, touch: false });+ }, { missingOk: true, touch: false, touchCard: false });}/**diff --git a/server/services/decksSync.db.test.js b/server/services/decksSync.db.test.jsindex c45c4e541a..79b487f149 100644--- a/server/services/decksSync.db.test.js+++ b/server/services/decksSync.db.test.js@@ -7,9 +7,11 @@ import { afterAll, describe, expect, it } from 'vitest';import { checkHealth, ensureSchema, query, close } from '../lib/db.js';import { requireDbOrSkip } from '../lib/dbTestGate.js';import { sanitizeRecordForWire } from '../lib/syncWire.js';+import { contentHashForRecord } from '../lib/conflictJournal.js';import {- createDeck, getDeck, updateDeck, updateCard, deleteDeck,+ DECK_KIND, createDeck, getDeck, updateDeck, updateCard, deleteDeck,getDeckForSync, listDecksForSync, listDeckIdsForSync,+ markCardsRenderQueued, markCardRenderTerminal, attachCardRender,mergeDecksFromSync, pruneTombstonedDecks, restoreDeck,} from './decks.js';@@ -198,4 +200,42 @@ describe.skipIf(!ready)('deck federation', () => {it('returns null when restoring a deck the sweep already hard-pruned', async () => {expect(await restoreDeck(crypto.randomUUID(), { name: 'ghost' })).toBeNull();});++ it('leaves the sync content hash untouched while a render queues and then fails', async () => {+ // Which job a card is waiting on is machine-local bookkeeping the wire+ // strips. If it moved the hash, every deck the user had rendered since the+ // last sync would look locally edited and raise a conflict-journal entry+ // against the next peer push — for changes that do not exist (#7364).+ const deck = await make('Render Bookkeeping');+ const card = deck.cards[0];+ const before = contentHashForRecord(DECK_KIND, await getDeckForSync(deck.id));++ await markCardsRenderQueued(deck.id, [{ cardId: card.id, render: { jobId: 'job-1', status: 'queued' } }]);+ const queued = await getDeck(deck.id);+ expect(queued.cards.find((c) => c.id === card.id).render.jobId).toBe('job-1');+ expect(contentHashForRecord(DECK_KIND, await getDeckForSync(deck.id))).toBe(before);++ await markCardRenderTerminal(deck.id, card.id, { jobId: 'job-1', status: 'failed', error: 'boom' });+ expect((await getDeck(deck.id)).cards.find((c) => c.id === card.id).render.status).toBe('failed');+ expect(contentHashForRecord(DECK_KIND, await getDeckForSync(deck.id))).toBe(before);++ // Control: a real content edit MUST still move the hash, or this test would+ // pass just as well against a hash that ignores the card roster entirely.+ await updateCard(deck.id, card.id, { prompt: 'a king of coins, woodcut' });+ expect(contentHashForRecord(DECK_KIND, await getDeckForSync(deck.id))).not.toBe(before);+ });++ it('does move the hash when a completed render attaches its gallery file', async () => {+ // The mirror of the case above: `imageRefs` IS wire-visible (the push asset+ // manifest ships the bytes), so a finished render is a genuine content+ // change peers must receive — the #7364 narrowing must not swallow it.+ const deck = await make('Completed Render');+ const card = deck.cards[0];+ const before = contentHashForRecord(DECK_KIND, await getDeckForSync(deck.id));++ await markCardsRenderQueued(deck.id, [{ cardId: card.id, render: { jobId: 'job-2', status: 'queued' } }]);+ await attachCardRender({ deckId: deck.id, cardId: card.id, filename: 'king.png', jobId: 'job-2' });++ expect(contentHashForRecord(DECK_KIND, await getDeckForSync(deck.id))).not.toBe(before);+ });});