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: keep the index row when a gallery image survives its delete (#2738)

[._.]/ Adam Eivy252828f77af6
diff --git a/server/services/imageGen/deleteImage.test.js b/server/services/imageGen/deleteImage.test.js
index 0dedb74c2b..f1c1da6bd2 100644
--- a/server/services/imageGen/deleteImage.test.js
+++ b/server/services/imageGen/deleteImage.test.js
@@ -34,6 +34,14 @@ vi.mock('../../lib/pythonSetup.js', () => ({
const unindexImage = vi.fn(async () => {});
vi.mock('../mediaAssetIndex/index.js', () => ({ unindexImage }));
+// Real fs/promises, except tests can make unlink fail to simulate an
+// EACCES/EBUSY gallery (which deleteImage swallows, leaving the file on disk).
+let unlinkImpl = null;
+vi.mock('fs/promises', async () => {
+ const actual = await vi.importActual('fs/promises');
+ return { ...actual, unlink: async (...args) => (unlinkImpl ? unlinkImpl(...args) : actual.unlink(...args)) };
+});
+
let tmpRoot;
let priorRegistryEnv;
let deleteImage;
@@ -55,6 +63,7 @@ afterAll(() => {
beforeEach(() => {
vi.clearAllMocks();
+ unlinkImpl = null;
rmSync(imagesDir, { recursive: true, force: true });
});
@@ -88,4 +97,19 @@ describe('deleteImage → media asset index delete hook', () => {
expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('db down'));
errSpy.mockRestore();
});
+
+ it('keeps the index row when the file survives the delete (no undercount)', async () => {
+ // deleteImage swallows unlink errors, so an EACCES/EBUSY gallery leaves the
+ // image on disk and still listed by listGallery(). Unindexing it here would
+ // swap the overcount this issue fixed for an undercount of a LIVE image.
+ const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
+ unlinkImpl = async () => { throw Object.assign(new Error('EACCES: permission denied'), { code: 'EACCES' }); };
+ seedImage('img-3.png');
+
+ await expect(deleteImage('img-3.png')).resolves.toEqual({ ok: true });
+ expect(existsSync(join(imagesDir, 'img-3.png'))).toBe(true);
+ expect(unindexImage).not.toHaveBeenCalled();
+ expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('survived delete'));
+ errSpy.mockRestore();
+ });
});
diff --git a/server/services/imageGen/local.js b/server/services/imageGen/local.js
index ee2d3de06b..65fa52418e 100644
--- a/server/services/imageGen/local.js
+++ b/server/services/imageGen/local.js
@@ -998,9 +998,19 @@ export async function deleteImage(filename) {
// between. Non-fatal + dynamically imported: a broken index must never fail
// the user's delete, and this keeps the pg stack out of this module's static
// graph (the mirror of how the index dynamically imports the media stack).
- await import('../mediaAssetIndex/index.js')
- .then((m) => m.unindexImage(filename))
- .catch((err) => console.error(`❌ Media index image delete hook: ${err.message}`));
+ //
+ // Gated on the PNG actually being GONE. The unlink above swallows its error,
+ // so an EACCES/EBUSY/EIO failure leaves the image on disk and still listed by
+ // listGallery() — unindexing it there would swap this bug for its mirror and
+ // UNDERcount a live image. Failed-to-delete is not deleted (the absent-vs-empty
+ // sentinel rule): leave the row for the next reconcile, which reads disk.
+ if (existsSync(join(PATHS.images, filename))) {
+ console.error(`❌ Image file survived delete, keeping its index row: ${filename}`);
+ } else {
+ await import('../mediaAssetIndex/index.js')
+ .then((m) => m.unindexImage(filename))
+ .catch((err) => console.error(`❌ Media index image delete hook: ${err.message}`));
+ }
console.log(`🗑️ Deleted image: ${filename}`);
return { ok: true };
}