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: bind recovery worker status to its owner to avoid stale coordinator launches

[._.]/ Adam Eivy2c8a11ad5b9c
diff --git a/server/lib/databaseMaintenanceJournal.js b/server/lib/databaseMaintenanceJournal.js
index 13868d0320..581f6830f8 100644
--- a/server/lib/databaseMaintenanceJournal.js
+++ b/server/lib/databaseMaintenanceJournal.js
@@ -272,8 +272,7 @@ export function createDatabaseMaintenanceJournal(dataDir = PATHS.data) {
return directory;
};
- const coordinatorStatus = (id) => {
- const owner = readCoordinator(id);
+ const coordinatorStatusForOwner = (id, owner) => {
if (!owner) return { state: 'unclaimed' };
const directory = workerDirectory(owner.token);
try { lstatSync(directory); } catch (err) {
@@ -298,6 +297,8 @@ export function createDatabaseMaintenanceJournal(dataDir = PATHS.data) {
return { state: 'exited', exitCode };
};
+ const coordinatorStatus = id => coordinatorStatusForOwner(id, readCoordinator(id));
+
// The fixed maintenance worker checks both ownership and the supervisor's
// one-use reservation. This grants no ordinary database/spawn admission.
const assertCoordinatorWorker = (id, token) => {
@@ -656,7 +657,10 @@ export function createDatabaseMaintenanceJournal(dataDir = PATHS.data) {
if (!current || current.id !== id) throw databaseMaintenanceError();
const owner = readCoordinator(id);
if (!owner) throw databaseMaintenanceError();
- const status = coordinatorStatus(id);
+ // Bind the receipt to the owner we read. A concurrent recovery may publish
+ // a successor before this read; its unregistered state must never authorize
+ // launching our stale predecessor token. Its persisted intent is adopted below.
+ const status = coordinatorStatusForOwner(id, owner);
if (status.state === 'unregistered') return owner.token;
if (status.state !== 'exited') return null;
const intentPath = join(activeDir, 'recovery-after-' + owner.token + '.json');
diff --git a/server/lib/databaseMaintenanceJournal.test.js b/server/lib/databaseMaintenanceJournal.test.js
index ad7f30741a..5dcd3729d9 100644
--- a/server/lib/databaseMaintenanceJournal.test.js
+++ b/server/lib/databaseMaintenanceJournal.test.js
@@ -72,6 +72,31 @@ describe('persistent database maintenance boundary', () => {
expect(journal.read()).toEqual(record);
});
+ it('adopts a successor published between recovery owner and worker-status reads', () => {
+ const operation = journal.begin({ source, target });
+ const token = journal.acquireCoordinator(operation.id);
+ const directory = journal.reserveCoordinatorWorker(operation.id, token);
+ writeFileSync(join(directory, 'exit'), '1\n');
+ let ownerReads = 0;
+ let successor;
+ const interleave = path => {
+ if (path.endsWith('cancel-' + operation.id + '.claim') && ++ownerReads === 2) {
+ successor = journal.prepareRecovery(operation.id);
+ } else {
+ fsHook.beforeRead = interleave;
+ }
+ };
+ fsHook.beforeRead = interleave;
+ const recovered = journal.prepareRecovery(operation.id);
+ fsHook.beforeRead = null;
+ expect(successor).toBeTruthy();
+ expect(recovered).toBe(successor);
+ expect(recovered).not.toBe(token);
+ expect(() => journal.reserveCoordinatorWorker(operation.id, recovered)).not.toThrow();
+ expect(journal.prepareRecovery(operation.id)).toBeNull();
+ expect(() => journal.assertAdmission()).toThrow();
+ });
+
it('fences cancellation and competing coordinators through every durable stage', () => {
const operation = journal.begin({ source, target });
const token = journal.acquireCoordinator(operation.id);