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

Drain asset publications before live file restore queues

[._.]/ Adam Eivyc131ce450e31
diff --git a/server/services/backup.js b/server/services/backup.js
index 1a2c3e9990..74fc0c4dc4 100644
--- a/server/services/backup.js
+++ b/server/services/backup.js
@@ -1626,13 +1626,20 @@ export async function restoreSnapshot(destPath, snapshotId, { dryRun = true, sub
}
return restoreWithMediaBoundary();
};
- // Fixed acquisition order: settings -> CoS config -> CoS runtime -> media registry. Settings
- // writers drain before any CoS queue is held, and remain fenced until both
- // the transfer and all cache reconciliation (including CoS) have settled.
- if (!dryRun && (!scope || scope === 'settings.json')) {
- return withLiveSettingsRestore(restoreWithCosBoundary);
+ // Fixed acquisition order: snapshot cut -> settings -> CoS config -> CoS
+ // runtime -> media registry. Drain publications BEFORE holding any domain
+ // queue they may need to finish, and keep admission closed through transfer
+ // and all cache reconciliation, including a partially failed transfer.
+ const restoreWithSettingsBoundary = () => !dryRun && (!scope || scope === 'settings.json')
+ ? withLiveSettingsRestore(restoreWithCosBoundary)
+ : restoreWithCosBoundary();
+ if (dryRun) return restoreWithSettingsBoundary();
+ const releaseSnapshotCut = await acquireBackupSnapshotCut();
+ try {
+ return await restoreWithSettingsBoundary();
+ } finally {
+ releaseSnapshotCut();
}
- return restoreWithCosBoundary();
}
const DUMP_UNREADABLE = Object.freeze({ status: 'failed', reason: 'dump_unreadable', error: 'The snapshot database dump could not be read or staged for restore. Restore was refused without changing data.' });
diff --git a/server/services/backup.test.js b/server/services/backup.test.js
index 2673b3a7a1..ffbfbb6049 100644
--- a/server/services/backup.test.js
+++ b/server/services/backup.test.js
@@ -3135,6 +3135,108 @@ describe('restoreSnapshot snapshotId, filter flags, and settings re-sync', () =>
});
});
+ describe('live file restore publication boundary', () => {
+ afterEach(() => { snapshotCutSeam.wrap = null; });
+
+ it.each([false, true])('drains file publications before domain queues and holds reconciliation (bypass=%s)', async bypass => {
+ const { withBackupAssetPublication } = await import('../lib/backupSnapshotBoundary.js');
+ const marker = join(testDataRoot(), 'restore-publication.txt');
+ const admitted = Promise.withResolvers();
+ const finishPublication = Promise.withResolvers();
+ const requested = Promise.withResolvers();
+ const transferring = Promise.withResolvers();
+ const reconciling = Promise.withResolvers();
+ const finishReconciliation = Promise.withResolvers();
+ snapshotCutSeam.wrap = acquire => {
+ requested.resolve();
+ return bypass ? () => {} : acquire();
+ };
+ const publication = withBackupAssetPublication(async () => {
+ await fs.writeFile(marker, 'old publication started');
+ admitted.resolve();
+ await finishPublication.promise;
+ await fs.writeFile(marker, 'old publication committed');
+ });
+ await admitted.promise;
+ const proc = fakeProc();
+ spawn.mockImplementationOnce(() => {
+ // Real destination bytes at the rsync process boundary, no live data.
+ writeFileSync(marker, 'restored snapshot');
+ transferring.resolve();
+ setImmediate(() => proc.emit('close', 0));
+ return proc;
+ });
+ reloadSettings.mockImplementationOnce(async () => {
+ reconciling.resolve();
+ await finishReconciliation.promise;
+ });
+ const restore = restoreSnapshot('/dest', 'snap-1', { dryRun: false });
+ let later;
+ try {
+ await requested.promise;
+ if (bypass) {
+ await transferring.promise;
+ expect(withLiveSettingsRestore).toHaveBeenCalledTimes(1);
+ } else {
+ await new Promise(resolve => setImmediate(resolve));
+ expect(withLiveSettingsRestore).not.toHaveBeenCalled();
+ expect(withLiveCosRestore).not.toHaveBeenCalled();
+ expect(spawn).not.toHaveBeenCalled();
+ }
+ finishPublication.resolve();
+ await publication;
+ await reconciling.promise;
+ // Without the outer cut, the older in-flight write overwrites restored
+ // bytes. With it, rsync starts only after that publication is durable.
+ expect(await fs.readFile(marker, 'utf8')).toBe(bypass ? 'old publication committed' : 'restored snapshot');
+ let laterRan = false;
+ later = withBackupAssetPublication(async () => {
+ laterRan = true;
+ await fs.writeFile(marker, 'later publication');
+ });
+ await new Promise(resolve => setImmediate(resolve));
+ expect(laterRan).toBe(bypass);
+ } finally {
+ finishPublication.resolve();
+ finishReconciliation.resolve();
+ await Promise.all([publication, restore, later]);
+ }
+ expect(await fs.readFile(marker, 'utf8')).toBe('later publication');
+ });
+
+ it('refuses an owned cut before domain queues or transfer, while preview remains available', async () => {
+ const { acquireBackupSnapshotCut } = await import('../lib/backupSnapshotBoundary.js');
+ const release = await acquireBackupSnapshotCut();
+ try {
+ await expect(restoreSnapshot('/dest', 'snap-1', { dryRun: false }))
+ .rejects.toMatchObject({ code: 'BACKUP_SNAPSHOT_BUSY' });
+ expect(withLiveSettingsRestore).not.toHaveBeenCalled();
+ expect(withLiveCosRestore).not.toHaveBeenCalled();
+ expect(spawn).not.toHaveBeenCalled();
+ await expect(runRestore('/dest', 'snap-1', { dryRun: true })).resolves.toMatchObject({ dryRun: true });
+ // A refused restore must not release the other owner's cut.
+ await expect(acquireBackupSnapshotCut()).rejects.toMatchObject({ code: 'BACKUP_SNAPSHOT_BUSY' });
+ } finally { release(); }
+ });
+
+ it.each(['domain refusal', 'transfer failure', 'reconciliation failure'])('releases its cut after %s', async failure => {
+ const { acquireBackupSnapshotCut } = await import('../lib/backupSnapshotBoundary.js');
+ if (failure === 'domain refusal') {
+ withLiveCosRestore.mockRejectedValueOnce(new Error('restore refused'));
+ } else {
+ const proc = fakeProc();
+ spawn.mockImplementationOnce(() => {
+ setImmediate(() => proc.emit('close', failure === 'transfer failure' ? 1 : 0));
+ return proc;
+ });
+ if (failure === 'reconciliation failure') reloadSettings.mockRejectedValueOnce(new Error('reload failed'));
+ }
+ await expect(restoreSnapshot('/dest', 'snap-1', { dryRun: false })).rejects.toThrow();
+ const release = await acquireBackupSnapshotCut();
+ release();
+ });
+ });
+
describe('media registry restore ownership boundary', () => {
it.each([undefined, 'media-models.json'])('holds the boundary for affected live scope %s', async subdirFilter => {
await runRestore('/dest', 'snap-1', { dryRun: false, subdirFilter });