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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
feat: expose fenced database maintenance status (#8816)
[._.]/ Adam Eivyff0afa7fef12
diff --git a/docs/BACKUP.md b/docs/BACKUP.mdindex 8a1007569e..cfe20058f1 100644--- a/docs/BACKUP.md+++ b/docs/BACKUP.md@@ -235,6 +235,8 @@ outside the CoS state/config scope do not acquire this boundary.## Database maintenance admission+While the server is still running, `GET /api/database/maintenance/status` reads the journal without querying PostgreSQL or launching shell probes. It uses the ordinary instance authentication gate (authentication remains optional) and returns `{ stage: "idle", fenced: false }` when no operation exists, or the operation's `id`, `stage`, source/target mode names, and `fenced: true`. Connection identities and coordinator credentials are never returned. Responses are not cacheable; damaged or unreadable state returns HTTP 503 with `DATABASE_MAINTENANCE`, never idle. A `verified` stage alone still reports fenced: it does not authorize writers or prove admission has reopened. Once the server has stopped or its boot fence refuses startup, use the local status command below; this endpoint does not bypass the boot fence.+The persistent maintenance boundary is a prerequisite for coordinated offline migration (#8805). It does **not** migrate data, change saved mode, stop existing writer processes, or prove a target cutover. In particular, entering maintenance is not permission to invoke an uncoordinated SQL import or backend migration.From the install root, inspect or establish the fence:diff --git a/server/routes/database.js b/server/routes/database.jsindex 3d675253cb..0bebcb0771 100644--- a/server/routes/database.js+++ b/server/routes/database.js@@ -10,6 +10,13 @@ import * as dbAdmin from '../services/dbAdmin.js';const router = Router();+// GET /api/database/maintenance/status — local journal, no pool/shell probes.+// The ordinary instance auth gate applies; this is not a public health route.+router.get('/maintenance/status', asyncHandler(async (_req, res) => {+ res.set('Cache-Control', 'no-store');+ res.json(dbAdmin.getMaintenanceStatus());+}));+// GET /api/database/status — current mode, connectivity, row counts, resource statsrouter.get('/status', asyncHandler(async (_req, res) => {res.json(await dbAdmin.getStatus());diff --git a/server/routes/database.test.js b/server/routes/database.test.jsindex f3ea120661..f2a79f4cc9 100644--- a/server/routes/database.test.js+++ b/server/routes/database.test.js@@ -66,7 +66,7 @@ import { POOL_CONFIG, query, checkHealth } from '../lib/db.js';import { execFile, spawn } from '../lib/childProcess.js';import { EventEmitter } from 'events';import { PassThrough, Readable } from 'stream';-import { writeFileSync, mkdtempSync, readFileSync, createReadStream } from 'fs';+import { writeFileSync, mkdtempSync, readFileSync, createReadStream, rmSync } from 'fs';import { tmpdir } from 'os';import { join as pathJoin } from 'path';import databaseRoutes from './database.js';@@ -167,6 +167,62 @@ describe('database admin maintenance admission', () => {});});+// A fenced operation must remain inspectable without pool/shell admission,+// while endpoint identities and damaged on-disk bytes never reach the API.+describe('database maintenance status', () => {+ afterEach(() => {+ rmSync(pathJoin(PATHS.data, 'database-maintenance'), { recursive: true, force: true });+ });++ it('reports idle and current durable progress without exposing endpoints or reopening admission', async () => {+ vi.clearAllMocks();+ const app = makeApp();+ const idle = await request(app).get('/api/database/maintenance/status');+ expect(idle.status).toBe(200);+ expect(idle.body).toEqual({ stage: 'idle', fenced: false });+ expect(idle.headers['cache-control']).toBe('no-store');++ const journal = createDatabaseMaintenanceJournal(PATHS.data);+ const source = { mode: 'native', host: 'localhost', port: 5432, database: 'example_test', user: 'example_role' };+ const operation = journal.begin({ source, target: { ...source, mode: 'docker', port: 5561 } });+ const accepted = await request(app).get('/api/database/maintenance/status');+ expect(accepted.body).toEqual({+ id: operation.id, stage: 'accepted', source: 'native', target: 'docker', fenced: true,+ });+ const owner = journal.acquireCoordinator(operation.id);+ const stages = ['accepted', 'quiescing', 'exporting', 'importing', 'committing', 'verifying', 'verified'];+ for (let i = 1; i < stages.length; i++) {+ journal.transition(operation.id, owner, stages[i - 1], stages[i]);+ }+ const verified = await request(app).get('/api/database/maintenance/status');+ expect(verified.status).toBe(200);+ expect(verified.body).toEqual({ ...accepted.body, stage: 'verified' });+ expect(() => journal.assertAdmission()).toThrow();+ expect(execFile).not.toHaveBeenCalled();+ expect(spawn).not.toHaveBeenCalled();+ expect(query).not.toHaveBeenCalled();+ expect(checkHealth).not.toHaveBeenCalled();+ });++ it('returns a non-cacheable maintenance error for damaged state instead of reporting idle', async () => {+ vi.clearAllMocks();+ const journal = createDatabaseMaintenanceJournal(PATHS.data);+ const source = { mode: 'docker', host: 'localhost', port: 5561, database: 'example_test', user: 'example_role' };+ journal.begin({ source, target: { ...source, mode: 'native', port: 5432 } });+ writeFileSync(pathJoin(PATHS.data, 'database-maintenance', 'operation.json'), 'example damaged private bytes');+ const response = await request(makeApp()).get('/api/database/maintenance/status');+ expect(response.status).toBe(503);+ expect(response.body.code).toBe('DATABASE_MAINTENANCE');+ expect(response.headers['cache-control']).toBe('no-store');+ expect(response.text).not.toContain('example damaged private bytes');+ expect(() => journal.assertAdmission()).toThrow();+ expect(execFile).not.toHaveBeenCalled();+ expect(spawn).not.toHaveBeenCalled();+ expect(query).not.toHaveBeenCalled();+ expect(checkHealth).not.toHaveBeenCalled();+ });+});+describe('isPg17OnlyDirective (sed-replacement line filter)', () => {it('matches the pg17-only directives the legacy sed stripped', () => {expect(isPg17OnlyDirective('\\restrict abc123')).toBe(true);diff --git a/server/services/authGate.test.js b/server/services/authGate.test.jsindex cbf009d390..5cbe4910ff 100644--- a/server/services/authGate.test.js+++ b/server/services/authGate.test.js@@ -492,6 +492,7 @@ describe('authGate Express path matching', () => {for (const path of ['/api/example/records/ExampleRecord', '/aPi/example/records/ExampleRecord',+ '/api/database/maintenance/status','/data/images/ExampleAsset.txt', '/DaTa/images/ExampleAsset.txt','/sdapi/v1/sd-models', '/SdApI/v1/sd-models',]) {diff --git a/server/services/dbAdmin.js b/server/services/dbAdmin.jsindex 4f104f7adc..4df88b6e6d 100644--- a/server/services/dbAdmin.js+++ b/server/services/dbAdmin.js@@ -8,7 +8,7 @@ import { stripAnsi } from '../lib/ansiStrip.js';import { resolvePgDumpBinary } from '../lib/pgTools.js';import { resolveBashBinary, toBashPath } from '../lib/bashResolver.js';import { resolvePostgresPort } from '../lib/ports.js';-import { assertDatabaseAdmission } from '../lib/databaseMaintenanceJournal.js';+import { assertDatabaseAdmission, createDatabaseMaintenanceJournal } from '../lib/databaseMaintenanceJournal.js';const rootDir = PATHS.root;const dbScript = toBashPath(join(rootDir, 'scripts', 'db.sh'));@@ -35,6 +35,23 @@ async function withDatabaseOperation(operation) {}}+/**+ * Read only the durable operation, including while pool admission is fenced.+ * This is not a health probe or permission to resume writers. Even a verified+ * journal remains fenced until the coordinator explicitly releases admission.+ */+export function getMaintenanceStatus() {+ const operation = createDatabaseMaintenanceJournal().read();+ if (!operation) return { stage: 'idle', fenced: false };+ return {+ id: operation.id,+ stage: operation.stage,+ source: operation.source.mode,+ target: operation.target.mode,+ fenced: true,+ };+}+/*** Run a command and return { stdout, stderr, exitCode }.*/