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: don't prune media assets indexed during reconcile (#10106)
Adam Eivycdbf5cd9fa1c
diff --git a/server/services/mediaAssetIndex/db.js b/server/services/mediaAssetIndex/db.jsindex 122fcddfcc..ff687a57fa 100644--- a/server/services/mediaAssetIndex/db.js+++ b/server/services/mediaAssetIndex/db.js@@ -388,6 +388,9 @@ async function readListStrict(fn) {*/export async function reconcileMediaAssets(deps = {}) {const now = new Date().toISOString();+ // Rows indexed after this instant (a generation/upload/peer sync landing+ // mid-reconcile) are absent from the disk snapshot but must not be pruned.+ const startedAt = now;const imageRead = await readListStrict(deps.listGallery || (await import('../imageGen/local.js')).listGallery,@@ -410,8 +413,8 @@ export async function reconcileMediaAssets(deps = {}) {// Per-kind prune, gated on a successful read for that kind. Pruning one kind// never touches the other's rows (an image-read failure can't wipe videos).let pruned = 0;- if (imageRead.ok) pruned += await pruneKind('image', imageRows.map((r) => r.mediaKey));- if (videoRead.ok) pruned += await pruneKind('video', videoRows.map((r) => r.mediaKey));+ if (imageRead.ok) pruned += await pruneKind('image', imageRows.map((r) => r.mediaKey), startedAt);+ if (videoRead.ok) pruned += await pruneKind('video', videoRows.map((r) => r.mediaKey), startedAt);const skipped = [!imageRead.ok && 'images', !videoRead.ok && 'videos'].filter(Boolean);const skipNote = skipped.length ? ` — SKIPPED prune for ${skipped.join('+')} (disk read failed)` : '';@@ -423,12 +426,15 @@ export async function reconcileMediaAssets(deps = {}) {// liveKeys set legitimately means "this kind has no assets on disk" — safe to// prune all of that kind — but the CALLER only reaches here when the read for// that kind succeeded, so empty is trustworthy.-async function pruneKind(kind, liveKeys) {+async function pruneKind(kind, liveKeys, startedAt) {const res = liveKeys.length === 0- ? await query(`DELETE FROM media_assets WHERE kind = $1`, [kind])+ ? await query(+ `DELETE FROM media_assets WHERE kind = $1 AND indexed_at < $2::timestamptz`,+ [kind, startedAt],+ ): await query(- `DELETE FROM media_assets WHERE kind = $1 AND media_key <> ALL($2::text[])`,- [kind, liveKeys],+ `DELETE FROM media_assets WHERE kind = $1 AND indexed_at < $2::timestamptz AND media_key <> ALL($3::text[])`,+ [kind, startedAt, liveKeys],);return res.rowCount || 0;}diff --git a/server/services/mediaAssetIndex/db.test.js b/server/services/mediaAssetIndex/db.test.jsindex c08a932c66..46e830cf5c 100644--- a/server/services/mediaAssetIndex/db.test.js+++ b/server/services/mediaAssetIndex/db.test.js@@ -207,6 +207,7 @@ describe.skipIf(!runDb)('media asset index DB round-trip', () => {// Pre-seed a stale row that won't be in the injected "disk" set.await db.upsertAsset({ mediaKey: `image:${PFX}stale.png`, kind: 'image', ref: `${PFX}stale.png`, data: { filename: `${PFX}stale.png` }, createdAt: '2026-01-01T00:00:00.000Z' });+ await new Promise((r) => setTimeout(r, 10)); // seed's indexed_at must predate reconcile's startconst listGallery = async () => [{ filename: `${PFX}live1.png`, prompt: 'one', createdAt: '2026-03-01T00:00:00.000Z' },{ filename: `${PFX}live2.png`, prompt: 'two', createdAt: '2026-03-02T00:00:00.000Z' },@@ -229,6 +230,18 @@ describe.skipIf(!runDb)('media asset index DB round-trip', () => {expect(vids.some((x) => x.id === `${PFX}vid1`)).toBe(true);});+ it('does not prune a row indexed after reconcile started (concurrent generation)', async () => {+ await new Promise((r) => setTimeout(r, 10));+ const listGallery = async () => {+ // A generation finishing mid-read: indexed, but absent from this snapshot.+ await db.upsertAsset({ mediaKey: `image:${PFX}racing.png`, kind: 'image', ref: `${PFX}racing.png`, data: { filename: `${PFX}racing.png` }, createdAt: '2026-05-01T00:00:00.000Z' });+ return [];+ };+ await db.reconcileMediaAssets({ listGallery, loadHistory: async () => [] });+ const imgs = await db.listAssets({ kind: 'image' });+ expect(imgs.map((x) => x.filename)).toContain(`${PFX}racing.png`);+ });+it('survives duplicate refs on disk (multi-row upsert must not self-conflict)', async () => {// Catches the multi-row upsert self-conflicting on a repeated media_key —// a Postgres-level constraint no unit test can pin. See the dedupe comment