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: keep malformed maintenance paths fenced

[._.]/ Adam Eivyf5d9e7e41c2c
diff --git a/docs/BACKUP.md b/docs/BACKUP.md
index 406e8e8c7c..dd8f6714e8 100644
--- a/docs/BACKUP.md
+++ b/docs/BACKUP.md
@@ -223,7 +223,7 @@ node scripts/database-maintenance.mjs begin native docker
node scripts/database-maintenance.mjs begin docker native
```
-The command returns the operation ID and direction. Plan for downtime: new pooled database operations and CoS spawn admission are refused immediately, and both managed server and runner refuse normal boot while fenced. Already-admitted transactions may finish; this boundary alone is **not a drained snapshot boundary**. The offline coordinator must still stop/drain all owned writers and validate live/spawning work before export. A process-local restore callback cannot bypass this persistent fence.
+The command returns the operation ID and direction. Plan for downtime: new pooled database operations and CoS spawn admission are refused immediately, and both managed server and runner refuse normal boot while fenced. Admission is the synchronous fence check: work admitted just before publication may still obtain a connection or spawn afterward. Already-admitted transactions may finish; this boundary alone is **not a drained snapshot boundary**. The offline coordinator must still stop/drain all owned writers and validate live/spawning work before export. A process-local restore callback cannot bypass this persistent fence.
If no transfer has started and saved source configuration is unchanged, cancel using that exact ID:
diff --git a/server/lib/databaseMaintenanceJournal.js b/server/lib/databaseMaintenanceJournal.js
index 81fa086ca4..470f854a13 100644
--- a/server/lib/databaseMaintenanceJournal.js
+++ b/server/lib/databaseMaintenanceJournal.js
@@ -51,7 +51,10 @@ function writeDurableExclusive(path, value) {
/**
* The DIRECTORY is the fence, not a successfully parsed journal. In particular,
* a crash between mkdir and journal publication must never reopen admission.
- * Synchronous checks keep admission before the first pool checkout/await.
+ * Admission linearizes at the synchronous filesystem check, before the first
+ * pool checkout/await. A call admitted just before publication may acquire its
+ * connection or spawn afterward. This is NOT a quiescence/snapshot guarantee:
+ * the coordinator must drain/stop every writer and in-flight spawn before dump.
*/
export function createDatabaseMaintenanceJournal(dataDir = PATHS.data) {
const activeDir = join(dataDir, 'database-maintenance');
@@ -75,6 +78,9 @@ export function createDatabaseMaintenanceJournal(dataDir = PATHS.data) {
const read = () => {
if (!isFenced()) return null;
try {
+ if (!lstatSync(activeDir).isDirectory() || !lstatSync(recordPath).isFile()) {
+ throw databaseMaintenanceError();
+ }
return journalSchema.parse(JSON.parse(readFileSync(recordPath, 'utf8')));
} catch {
throw databaseMaintenanceError();
diff --git a/server/lib/databaseMaintenanceJournal.test.js b/server/lib/databaseMaintenanceJournal.test.js
index 78ee7f98d3..f95dedf44a 100644
--- a/server/lib/databaseMaintenanceJournal.test.js
+++ b/server/lib/databaseMaintenanceJournal.test.js
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { spawn, spawnSync } from 'node:child_process';
-import { copyFileSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
+import { copyFileSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
@@ -90,11 +90,11 @@ describe('persistent database maintenance boundary', () => {
expect(journal.read().id).toBe(operation.id);
});
- it('lets an admitted transaction finish but refuses new work after another process fences it', () => {
+ it('allows admitted work to finish when checkout completes after the fence', () => {
const loader = join(root, 'transaction-loader.mjs');
const stub = `const log=[]; globalThis.sqlLog=log; export default {
types:{setTypeParser(){}},Pool:class {
- on(){} async connect(){return {query:async sql=>{log.push(sql); return {rows:[]}},release(){}}}
+ on(){} async connect(){globalThis.beforeCheckout(); return {query:async sql=>{log.push(sql); return {rows:[]}},release(){}}}
query(){throw Error('NEW_QUERY_REACHED')}
}};`;
writeFileSync(loader, `export async function resolve(s,c,n) {
@@ -102,10 +102,12 @@ describe('persistent database maintenance boundary', () => {
}`);
const code = `import {spawnSync} from 'node:child_process';
import {withTransaction,query} from ${JSON.stringify(new URL('./db.js', import.meta.url).href)};
- await withTransaction(async client=>{
+ globalThis.beforeCheckout=()=>{
const result=spawnSync(process.execPath,['--input-type=module','-e',
${JSON.stringify('import {createDatabaseMaintenanceJournal} from '+JSON.stringify(moduleUrl)+'; createDatabaseMaintenanceJournal('+JSON.stringify(data)+').begin('+JSON.stringify({source,target})+');')}]);
if(result.status!==0) throw Error('FENCE_FAILED');
+ };
+ await withTransaction(async client=>{
await client.query('INSERT INTO example_record VALUES (1)');
try {await query('INSERT INTO example_record VALUES (2)'); throw Error('NEW_WRITE_ACCEPTED')}
catch(e){if(e.code!=='DATABASE_MAINTENANCE') throw e}
@@ -185,6 +187,25 @@ describe('persistent database maintenance boundary', () => {
expect(JSON.parse(run('status').stdout)).toEqual({ stage: 'idle' });
});
+ it.skipIf(process.platform === 'win32')('never releases a symlinked active directory or journal', () => {
+ const operation = journal.begin({ source, target });
+ const active = join(data, 'database-maintenance');
+ const outside = join(root, 'outside.json');
+ writeFileSync(outside, JSON.stringify(operation));
+ rmSync(join(active, 'operation.json'));
+ symlinkSync(outside, join(active, 'operation.json'));
+ expect(() => journal.cancel(operation.id, source)).toThrow();
+ expect(journal.isFenced()).toBe(true);
+ rmSync(active, { recursive: true });
+ const outsideDir = join(root, 'outside');
+ mkdirSync(outsideDir);
+ writeFileSync(join(outsideDir, 'operation.json'), JSON.stringify(operation));
+ symlinkSync(outsideDir, active);
+ expect(() => journal.cancel(operation.id, source)).toThrow();
+ expect(journal.isFenced()).toBe(true);
+ expect(readdirSync(outsideDir)).toEqual(['operation.json']);
+ });
+
it('refuses same-endpoint and credential-bearing journals before fencing', () => {
expect(() => journal.begin({ source, target: { ...target, host: '127.0.0.1', port: source.port } })).toThrow();
expect(() => journal.begin({ source: { ...source, password: 'example-secret' }, target })).toThrow();