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

refactor: leave publication admission unchanged under a maintenance fence (#9983)

Adam Eivy9bd2b6a7cdc8
diff --git a/docs/BACKUP.md b/docs/BACKUP.md
index d5d60341e2..5ceb8e750b 100644
--- a/docs/BACKUP.md
+++ b/docs/BACKUP.md
@@ -40,11 +40,13 @@ stops PM2 to obtain it:
restore returns `backup_snapshot_busy` without changing anything.
- **Accepting a backend cutover** takes the cut around publishing the maintenance
fence (it waits up to 30 seconds to drain, and refuses with
- `DATABASE_PREFLIGHT_BUSY` while a backup owns it). Once the fence exists, new
- publications are refused before either store changes (`DATABASE_MAINTENANCE`),
- and a backup refuses to start or to take its cut — a backup that already
- reserved a snapshot marks it failed and preserves older recovery points.
- Cancelling the operation reopens publication; nothing stays held.
+ `DATABASE_PREFLIGHT_BUSY` while a backup owns it). The fence is published
+ before any new publication can be admitted, and every admitted one has already
+ finished. Once it exists, a backup refuses to start or to take its cut
+ (`DATABASE_MAINTENANCE`) — one that already reserved a snapshot marks it failed
+ and preserves older recovery points — and ordinary database writes are
+ refused, so no row can reference a file the fence stranded. Cancelling the
+ operation reopens everything; nothing stays held.
- Every release is owner-scoped: a failed or cancelled operation releases only the
cut it acquired and leaves the maintenance journal and restore-recovery
incomplete guards exactly as they were. The boundary is process-local; the
diff --git a/server/lib/backupSnapshotBoundary.js b/server/lib/backupSnapshotBoundary.js
index 96e6139988..f3c4ef0fba 100644
--- a/server/lib/backupSnapshotBoundary.js
+++ b/server/lib/backupSnapshotBoundary.js
@@ -4,10 +4,11 @@
* filesystem or SQL operations. The backup closes admission before rsync and
* keeps it closed through the database dump and manifest write.
*
- * Database maintenance shares the boundary. A maintenance fence refuses a NEW
- * publication or cut before either store changes, and maintenance that must not
- * observe or replace a half-published pair takes the same cut around its own
- * destructive step. Every refusal that is retryable carries BACKUP_SNAPSHOT_BUSY.
+ * Database maintenance shares the boundary. A cut is refused while a maintenance
+ * fence is up, and maintenance that must not observe or replace a half-published
+ * pair takes the same cut around its own destructive step. Publication itself is
+ * never refused here: under a fence the database already rejects the row write.
+ * Every retryable refusal carries BACKUP_SNAPSHOT_BUSY.
*/
import { AsyncLocalStorage } from 'node:async_hooks';
import { assertDatabaseAdmission } from './databaseMaintenanceJournal.js';
@@ -32,14 +33,10 @@ function releasePublications() {
export async function withBackupAssetPublication(work) {
const scope = publicationScope.getStore();
if (scope?.active) return work();
- // A maintenance fence would fail the row write after the file was published.
- // Refuse before either store changes, and again once a cut has been waited out.
- assertDatabaseAdmission();
// Work spawned by a still-admitted lease joins it: the cut already waits for that lease.
const joinsAdmitted = scope?.spawnedBy?.active === true;
while (!joinsAdmitted && (cutRequested || cutActive)) {
await new Promise(resolve => publicationWaiters.push(resolve));
- assertDatabaseAdmission();
}
admitted += 1;
const lease = { active: true };
diff --git a/server/lib/backupSnapshotBoundary.maintenance.test.js b/server/lib/backupSnapshotBoundary.maintenance.test.js
index e4a202076a..b7e603ac08 100644
--- a/server/lib/backupSnapshotBoundary.maintenance.test.js
+++ b/server/lib/backupSnapshotBoundary.maintenance.test.js
@@ -13,33 +13,6 @@ import { acquireBackupSnapshotCut, withBackupAssetPublication } from './backupSn
const settle = () => new Promise(resolve => setTimeout(resolve, 10));
describe('backup snapshot boundary under database maintenance', () => {
- it('refuses a new publication before either store changes while maintenance is fenced', async () => {
- fence.fenced = true;
- const work = vi.fn();
- try {
- await expect(withBackupAssetPublication(work)).rejects.toMatchObject({ code: 'DATABASE_MAINTENANCE' });
- } finally {
- fence.fenced = false;
- }
- expect(work).not.toHaveBeenCalled();
- await expect(withBackupAssetPublication(() => 'admitted')).resolves.toBe('admitted');
- });
-
- it('refuses a publication that waited out a cut when maintenance fenced meanwhile', async () => {
- const release = await acquireBackupSnapshotCut();
- const work = vi.fn();
- const waiting = withBackupAssetPublication(work).catch(error => error);
- await settle();
- fence.fenced = true;
- try {
- release();
- expect(await waiting).toMatchObject({ code: 'DATABASE_MAINTENANCE' });
- } finally {
- fence.fenced = false;
- }
- expect(work).not.toHaveBeenCalled();
- });
-
it('lets an admitted workflow finish, then refuses the cut and reopens admission when maintenance fenced while draining', async () => {
let finishRow;
let completed = false;
diff --git a/server/routes/databasePreflight.test.js b/server/routes/databasePreflight.test.js
index 0440eacbcb..a617451af7 100644
--- a/server/routes/databasePreflight.test.js
+++ b/server/routes/databasePreflight.test.js
@@ -248,7 +248,7 @@ describe('database cutover acceptance HTTP contract', () => {
expect(spawnDatabaseMaintenanceWorker).not.toHaveBeenCalled();
});
- it('publishes the fence only after an admitted file-plus-row publication drains, then refuses new ones', async () => {
+ it('publishes the fence only after an admitted file-plus-row publication drains, then refuses a backup cut', async () => {
const journal = createDatabaseMaintenanceJournal(PATHS.data);
let finishRow;
const halfPublished = withBackupAssetPublication(() => new Promise(resolve => { finishRow = resolve; }));
@@ -260,9 +260,8 @@ describe('database cutover acceptance HTTP contract', () => {
await halfPublished;
expect((await accepting).status).toBe(202);
expect(journal.read()).toMatchObject({ stage: 'accepted' });
- const work = vi.fn();
- await expect(withBackupAssetPublication(work)).rejects.toMatchObject({ code: 'DATABASE_MAINTENANCE' });
- expect(work).not.toHaveBeenCalled();
+ // The fence also refuses a backup cut, so nothing can capture the stores meanwhile.
+ await expect(acquireBackupSnapshotCut()).rejects.toMatchObject({ code: 'DATABASE_MAINTENANCE' });
});
it('refuses without a fence while a backup owns the snapshot cut, and accepts once it is released', async () => {