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: report unreadable backup retention inventories

Adam Eivy8894b02f28fc
diff --git a/server/services/backup.js b/server/services/backup.js
index e817486231..c0de1accda 100644
--- a/server/services/backup.js
+++ b/server/services/backup.js
@@ -1132,7 +1132,11 @@ async function pruneOldSnapshots(destPath, retentionCount) {
if (retentionCount === null || retentionCount === undefined) return { pruned: 0 };
const sourceRoot = join(destPath, 'snapshots', MACHINE_HOST);
- const entries = await readdir(sourceRoot, { withFileTypes: true }).catch(() => []);
+ // A completed snapshot was just written here; even ENOENT is a failed
+ // inventory, not proof that there is nothing to prune. Keep paths private.
+ const entries = await readdir(sourceRoot, { withFileTypes: true }).catch(cause => {
+ throw inventoryReadError(cause, 'read-retention-namespace');
+ });
const candidateIds = entries
.filter(entry => entry.isDirectory() && !entry.name.startsWith('.') && SNAPSHOT_ID_PATTERN.test(entry.name))
.map(entry => entry.name);
diff --git a/server/services/backup.test.js b/server/services/backup.test.js
index 6b48834316..5a57fdad59 100644
--- a/server/services/backup.test.js
+++ b/server/services/backup.test.js
@@ -142,6 +142,7 @@ vi.mock('fs/promises', async (importOriginal) => {
return {
...actual,
stat: vi.fn(actual.stat),
+ readdir: vi.fn(actual.readdir),
readFile: vi.fn(actual.readFile),
access: vi.fn(actual.access),
unlink: vi.fn(actual.unlink),
@@ -181,6 +182,7 @@ import { EXCLUDE_PATTERN_MAX_LENGTH, anchorUserExclude, anchorUserExcludes, isSa
// mockResolvedValue would otherwise make every later test see a marker and 409.
beforeEach(async () => {
const actual = await vi.importActual('fs/promises');
+ fs.readdir.mockImplementation(actual.readdir);
fs.access.mockImplementation(actual.access);
fs.unlink.mockImplementation(actual.unlink);
fs.writeFile.mockImplementation(actual.writeFile);
@@ -4782,6 +4784,34 @@ describe('runBackup lifecycle', () => {
await expect(fsp.stat(otherMachine)).resolves.toBeDefined();
});
+ it('reports an unreadable retention inventory without failing the completed backup or deleting snapshots', async () => {
+ const fsp = await actualFs();
+ const old = await seedSnapshot(machineHost, '2020-01-01T00-00-00');
+ const namespace = joinPath(destRoot, 'snapshots', machineHost);
+ fs.readdir.mockImplementation((path, ...args) => path === namespace
+ ? Promise.reject(Object.assign(new Error('private destination detail'), { code: 'EACCES' }))
+ : fsp.readdir(path, ...args));
+ const errorLog = vi.spyOn(console, 'error').mockImplementation(() => {});
+ try {
+ const rsync = fakeProc();
+ spawn.mockReturnValue(rsync);
+ const pending = runBackup(destRoot, null, { retentionCount: 1 });
+ await waitFor(() => spawn.mock.calls.length === 1, 'rsync spawn');
+ rsync.emit('close', 0);
+
+ const result = await pending;
+ expect(result).toMatchObject({ status: 'ok', prunedSnapshots: 0 });
+ await expect(fsp.stat(old)).resolves.toBeDefined();
+ await expect(fsp.stat(joinPath(namespace, result.snapshotId))).resolves.toBeDefined();
+ expect(errorLog).toHaveBeenCalledWith(
+ '❌ Backup retention prune failed: Backup inventory unavailable: read-retention-namespace (EACCES)'
+ );
+ expect(JSON.stringify(errorLog.mock.calls)).not.toContain('private destination detail');
+ } finally {
+ errorLog.mockRestore();
+ }
+ });
+
it('preserves older snapshots and reports zero pruned when the database dump fails', async () => {
const fsp = await actualFs();
const old1 = await seedSnapshot(machineHost, '2020-01-01T00-00-00', { generatedAt: '2020-01-01T00:00:00.000Z' });