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: backup retention no longer deletes snapshots with unreadable manifests (#11859)

Adam Eivy99dacd6e5cf3
diff --git a/server/services/backup.js b/server/services/backup.js
index ead8cb5af0..2aad7e36a6 100644
--- a/server/services/backup.js
+++ b/server/services/backup.js
@@ -1147,21 +1147,26 @@ async function pruneOldSnapshots(destPath, retentionCount) {
const snapshotDir = join(sourceRoot, id);
const { incomplete, failed } = await snapshotState(snapshotDir, id, true);
if (incomplete || failed) return null;
- // Read-only listing metadata; matches listSnapshots' own read.
- const manifest = await readJSONFile(join(snapshotDir, 'manifest.json'), null, { logError: false });
- return { id, snapshotDir, createdAt: manifest?.generatedAt ?? null };
+ // A manifest that exists but cannot be read must not date or delete the
+ // snapshot: fail toward keeping data (#11859).
+ const manifestRead = await readJSONFileStrict(join(snapshotDir, 'manifest.json'), null, { logError: false });
+ return { id, snapshotDir, createdAt: manifestRead.value?.generatedAt ?? null, protectedFromPrune: !manifestRead.ok };
}))).filter(Boolean);
- // Newest-first, same tiebreak as listSnapshots: dated entries by date, then
- // undated (pre-manifest legacy-in-namespace) entries by id.
- descriptors.sort((a, b) => {
- if (a.createdAt && b.createdAt) return b.createdAt.localeCompare(a.createdAt);
- if (a.createdAt) return -1;
- if (b.createdAt) return 1;
- return b.id.localeCompare(a.id);
- });
+ // Newest-first by snapshot id (an ISO-style timestamp that never depends on
+ // reading the manifest); manifest.generatedAt is only a tiebreak.
+ descriptors.sort((a, b) => b.id.localeCompare(a.id)
+ || (b.createdAt ?? '').localeCompare(a.createdAt ?? ''));
- const toDelete = descriptors.slice(retentionCount);
+ // Unreadable-manifest snapshots still count toward retention but are never deleted.
+ const toDelete = [];
+ descriptors.slice(retentionCount).forEach(d => {
+ if (d.protectedFromPrune) {
+ console.warn(`⚠️ Backup retention: kept snapshot ${d.id} (manifest unreadable)`);
+ } else {
+ toDelete.push(d);
+ }
+ });
let pruned = 0;
for (const { snapshotDir, id } of toDelete) {
// A snapshot a restore or download is reading stays; a later run prunes it
diff --git a/server/services/backup.test.js b/server/services/backup.test.js
index b745b684fb..520372923d 100644
--- a/server/services/backup.test.js
+++ b/server/services/backup.test.js
@@ -4909,6 +4909,28 @@ describe('runBackup lifecycle', () => {
await expect(fsp.stat(old)).rejects.toThrow();
});
+ it('keeps the newest snapshot and prunes the oldest when the newest manifest is corrupt, and never deletes an unreadable-manifest snapshot (#11859)', async () => {
+ const fsp = await actualFs();
+ const oldest = await seedSnapshot(machineHost, '2020-01-01T00-00-00', { generatedAt: '2020-01-01T00:00:00.000Z' });
+ const corrupt = await seedSnapshot(machineHost, '2020-01-02T00-00-00');
+ await fsp.writeFile(joinPath(corrupt, 'manifest.json'), '{truncated');
+ const unreadable = await seedSnapshot(machineHost, '2019-12-31T00-00-00');
+ await fsp.writeFile(joinPath(unreadable, 'manifest.json'), '{truncated');
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+
+ const rsync = fakeProc();
+ spawn.mockReturnValue(rsync);
+ const pending = runBackup(destRoot, { emit: vi.fn() }, { retentionCount: 2 });
+ await waitFor(() => spawn.mock.calls.length === 1, 'rsync spawn');
+ rsync.emit('close', 0);
+ await expect(pending).resolves.toMatchObject({ status: 'ok', prunedSnapshots: 1 });
+
+ await expect(fsp.stat(oldest)).rejects.toThrow();
+ await expect(fsp.stat(corrupt)).resolves.toBeDefined();
+ await expect(fsp.stat(unreadable)).resolves.toBeDefined();
+ expect(warn).toHaveBeenCalledWith('⚠️ Backup retention: kept snapshot 2019-12-31T00-00-00 (manifest unreadable)');
+ });
+
it('does not prune anything when retentionCount is null (unlimited — the legacy/unset default)', async () => {
const fsp = await actualFs();
const old1 = await seedSnapshot(machineHost, '2020-01-01T00-00-00', { generatedAt: '2020-01-01T00:00:00.000Z' });