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: make the post-delete file probe tri-state, not existsSync (#2738)
[._.]/ Adam Eivyc8c9ea6029ad
diff --git a/server/services/imageGen/deleteImage.test.js b/server/services/imageGen/deleteImage.test.jsindex f1c1da6bd2..9b8711a2f9 100644--- a/server/services/imageGen/deleteImage.test.js+++ b/server/services/imageGen/deleteImage.test.js@@ -34,12 +34,18 @@ 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).+// Real fs/promises, except tests can make unlink fail (simulating an+// EACCES/EBUSY gallery, which deleteImage swallows, leaving the file on disk)+// and make the post-delete stat probe fail (simulating an unreadable dir).let unlinkImpl = null;+let statImpl = null;vi.mock('fs/promises', async () => {const actual = await vi.importActual('fs/promises');- return { ...actual, unlink: async (...args) => (unlinkImpl ? unlinkImpl(...args) : actual.unlink(...args)) };+ return {+ ...actual,+ unlink: async (...args) => (unlinkImpl ? unlinkImpl(...args) : actual.unlink(...args)),+ stat: async (...args) => (statImpl ? statImpl(...args) : actual.stat(...args)),+ };});let tmpRoot;@@ -64,6 +70,7 @@ afterAll(() => {beforeEach(() => {vi.clearAllMocks();unlinkImpl = null;+ statImpl = null;rmSync(imagesDir, { recursive: true, force: true });});@@ -109,7 +116,22 @@ describe('deleteImage → media asset index delete hook', () => {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'));+ expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('not confirmed gone'));+ errSpy.mockRestore();+ });++ it('keeps the index row when the probe itself fails (unknown != absent)', async () => {+ // An unreadable gallery dir must not read as "the file is gone". existsSync+ // would return false here and retire a live row; only a definitive ENOENT+ // may unindex.+ const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});+ unlinkImpl = async () => { throw Object.assign(new Error('EACCES'), { code: 'EACCES' }); };+ statImpl = async () => { throw Object.assign(new Error('EACCES'), { code: 'EACCES' }); };+ seedImage('img-4.png');++ await expect(deleteImage('img-4.png')).resolves.toEqual({ ok: true });+ expect(unindexImage).not.toHaveBeenCalled();+ expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('EACCES'));errSpy.mockRestore();});});diff --git a/server/services/imageGen/local.js b/server/services/imageGen/local.jsindex 65fa52418e..a3f613f3e3 100644--- a/server/services/imageGen/local.js+++ b/server/services/imageGen/local.js@@ -999,17 +999,23 @@ export async function deleteImage(filename) {// 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).//- // Gated on the PNG actually being GONE. The unlink above swallows its error,+ // Gated on the PNG being CONFIRMED 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 {+ // UNDERcount a live image. Failed-to-delete is not deleted.+ //+ // The probe is tri-state, NOT existsSync: that collapses "absent" and "I+ // couldn't tell" (EACCES on the dir, EIO) into the same `false`, which is the+ // absent-vs-failed sentinel trap — an unreadable gallery would read as "every+ // image is gone" and retire live rows. Only a definitive ENOENT retires the+ // row; present-or-unknown leaves it for the reconcile, which reads disk.+ const probeErr = await stat(join(PATHS.images, filename)).then(() => null, (err) => err);+ if (probeErr?.code === 'ENOENT') {await import('../mediaAssetIndex/index.js').then((m) => m.unindexImage(filename)).catch((err) => console.error(`❌ Media index image delete hook: ${err.message}`));+ } else {+ console.error(`❌ Image file not confirmed gone (${probeErr?.code || 'still present'}), keeping its index row: ${filename}`);}console.log(`🗑️ Deleted image: ${filename}`);return { ok: true };