Stop a folder wipe from restoring the pre-wipe library catalog.

The in-memory catalog could outlive the write, so delete-all came back as thousands of empty tiles and a stale badge.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Towsty
2026-10-24 12:01:25 -05:00
co-authored by Cursor
parent 39aa4b65d0
commit 545a3d749b
12 changed files with 393 additions and 131 deletions
+60 -2
View File
@@ -9,6 +9,8 @@ const names = [
'safeRm',
'removeStillFiles',
'detachDraftStills',
'sameFolder',
'emptyFolderRows',
'pruneMissingStillMasters',
'catalogWithPrunedStills',
'deleteStill',
@@ -24,6 +26,8 @@ const js = ts.transpileModule(functions, { compilerOptions: { target: ts.ScriptT
function harness() {
const files = new Set()
const thrown = new Set()
const invalidated = []
const catalogEpoch = new Map()
let writes = 0
let stored = null
function readCatalog() { return stored }
@@ -31,6 +35,7 @@ function harness() {
stored = catalog === undefined ? _owner : catalog
writes += 1
}
function invalidateCatalogCache(owner) { invalidated.push(owner) }
const api = new Function(
'rmSync',
'existsSync',
@@ -45,7 +50,9 @@ function harness() {
'mutate',
'readCatalog',
'writeCatalog',
`${js}; return { catalogWithPrunedStills, deleteStill, deleteStillFamily, wipeFolderContents };`
'catalogEpoch',
'invalidateCatalogCache',
`${js}; return { catalogWithPrunedStills, deleteStill, deleteStillFamily, wipeFolderContents, emptyFolderRows };`
)(
(path) => {
if ([...thrown].some(id => String(path).includes(id))) throw new Error('EPERM')
@@ -67,12 +74,15 @@ function harness() {
return result
},
readCatalog,
writeCatalog
writeCatalog,
catalogEpoch,
invalidateCatalogCache
)
return {
api,
files,
thrown,
invalidated,
get writes() { return writes },
set writes(value) { writes = value },
load(catalog) { stored = catalog }
@@ -132,4 +142,52 @@ test('wipeFolderContents still clears the folder when a file remove throws', asy
assert.equal(catalog.stills.length, 0)
assert.equal(catalog.clips.length, 0)
assert.equal(h.writes, 1)
assert.deepEqual(h.invalidated, ['owner'])
})
test('wipeFolderContents leaves zero rows for that folder and does not touch another folder', async () => {
const h = harness()
const catalog = {
stills: [
{ id: 'a', folderId: 'inbox' },
{ id: 'b', folderId: 'other' }
],
clips: [{ id: 'c', folderId: 'inbox' }],
tracks: [
{ id: 't', folderId: 'inbox' },
{ id: 'u', folderId: 'other' }
],
drafts: [],
folders: [{ id: 'inbox', name: 'Inbox' }, { id: 'other', name: 'Other' }]
}
h.load(catalog)
h.files.add('owner/stills/a')
h.files.add('owner/stills/b')
const result = await h.api.wipeFolderContents('owner', 'inbox')
assert.equal(result.deleted, 3)
assert.deepEqual(catalog.stills.map(item => item.id), ['b'])
assert.deepEqual(catalog.clips, [])
assert.deepEqual(catalog.tracks.map(item => item.id), ['u'])
assert.deepEqual(h.invalidated, ['owner'])
})
test('pruneMissingStillMasters only drops and a second read does not put the still back', async () => {
const h = harness()
const catalog = {
stills: [{ id: 'gone', folderId: 'inbox' }, { id: 'keep', folderId: 'inbox' }],
drafts: [],
clips: [],
tracks: [],
folders: [{ id: 'inbox' }]
}
h.load(catalog)
h.files.add('owner/stills/keep')
const before = new Set(catalog.stills.map(item => item.id))
const page = h.api.catalogWithPrunedStills('owner')
assert.deepEqual(page.stills.map(item => item.id), ['keep'])
for (const still of page.stills) assert.equal(before.has(still.id), true)
assert.ok(page.stills.length < before.size)
h.files.add('owner/stills/gone')
const again = h.api.catalogWithPrunedStills('owner')
assert.deepEqual(again.stills.map(item => item.id), ['keep'])
})
+39
View File
@@ -0,0 +1,39 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { folderImageCount, libraryAfterWipe, mergeLibraryRows, replaceLibraryRows } from '../shared/studio2/viewer.mjs'
test('folderImageCount trusts a server count of 0', () => {
assert.equal(folderImageCount({ id: 'inbox', imageCount: 0 }, 1762), 0)
assert.equal(folderImageCount({ id: 'inbox', imageCount: 4 }, 1), 4)
assert.equal(folderImageCount(undefined, 3), 3)
})
test('libraryAfterWipe replaces state and does not keep wiped folder rows from a stale list', () => {
const previous = {
folders: [{ id: 'inbox', imageCount: 2 }],
stills: [{ id: 'new-1', folderId: 'inbox' }, { id: 'new-2', folderId: 'inbox' }],
clips: [],
tracks: [],
suppressedIds: ['already-gone']
}
const stale = {
folders: [{ id: 'inbox', imageCount: 0 }, { id: 'other', imageCount: 1 }],
stills: [
{ id: 'ghost', folderId: 'inbox' },
{ id: 'new-1', folderId: 'inbox' },
{ id: 'kept', folderId: 'other' }
],
clips: [{ id: 'clip-ghost', folderId: 'inbox' }],
tracks: []
}
const next = libraryAfterWipe(previous, stale, 'inbox')
assert.deepEqual(next.stills.map(item => item.id), ['kept'])
assert.deepEqual(next.clips, [])
assert.equal(next.folders[0].imageCount, 0)
assert.equal(next.suppressedIds.includes('new-1'), true)
assert.equal(next.suppressedIds.includes('ghost'), true)
assert.equal(next.suppressedIds.includes('already-gone'), true)
const page = mergeLibraryRows(next.stills, stale.stills, next.suppressedIds)
assert.deepEqual(page.map(item => item.id), ['kept'])
assert.deepEqual(replaceLibraryRows(stale.stills, next.suppressedIds).map(item => item.id), ['kept'])
})