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: refuse process-group writers with no recorded job identity (#8870)

[._.]/ Adam Eivyc71a6c2a93d5
diff --git a/docs/STORAGE.md b/docs/STORAGE.md
index 680305e4a9..2acdde9e4b 100644
--- a/docs/STORAGE.md
+++ b/docs/STORAGE.md
@@ -126,7 +126,7 @@ For local diagnostics at any stage, run `node scripts/database-maintenance.mjs s
`data/database-writers/<launch-id>/` is machine-local, file-primary **detached launch evidence**. `spawnDetached` publishes and fsyncs a reservation before its first asynchronous setup step, then checks admission again. A launch that races fence acceptance is therefore either refused or already represented in the inventory. It checks admission again after setup; a launch refused before any launcher started records `abandoned.json`, the only proof that no child exists. On POSIX the detached outer `sh` is a session and process-group leader, so its PID (`launcher.json`) names the group every supervisor/job descendant inherits; `launch.json` adds the job PID and launch time. Reservations contain only a random launch ID, timestamp, control directory, and process-group mode. Commands, arguments, environment variables, credentials, and application records are excluded. A partial/unreadable record fails inventory closed. On completion, POSIX keeps the identity and retires the entry only after two consecutive process-table snapshots show the job gone and every recorded group empty (a group ID cannot be reused while it has members); a surviving descendant or slow supervisor leaves the record for maintenance reconciliation. Windows has no group proof, so its completion still compacts into the identity-free `unreconciled-exits.json` marker, published from fsynced bytes with its parent directory synced before deletion. Ordinary completed jobs do not accumulate one permanent directory per launch.
-Run `node scripts/database-maintenance.mjs writers` for bounded counts at any stage (`unresolved`, `launching`, `launched`, `exited`, `abandoned`); it always reports `quiescenceVerified: false` and exposes no control paths or process identities. A malformed inventory refuses the command. A `{ state: 'unresolved', kind: 'retired-exits' }` entry represents legacy or Windows compacted completion history. **An inventory read never proves descendant quiescence.** Only the internal `reconcileDetachedWriters(id, token)` stage (`server/services/databaseMaintenanceQuiescence.js`) can, and only for the owning coordinator worker after `stopOwnedDatabaseProducers` recorded producer shutdown at `quiescing`. It classifies each record against a POSIX process-table snapshot: a job still alive with a verified identity (its PGID is recorded and it started inside the launch window) has its recorded groups sent SIGTERM, then SIGKILL after a grace period; a group seen empty is never signalled again. It refuses, leaving the fence and every record in place, when an admitted launch has not recorded a process identity, when a record is legacy/compacted or lacks a launcher group, when a live PID does not match its recorded identity (possible reuse), when an exited job still has surviving group members, when groups do not terminate, when a detached supervisor of this install's data directory is running without a record (for example a launch from code that predates the registry), or on Windows for anything but abandoned launches. Two consecutive quiescent snapshots are required before it archives every record to `data/database-maintenance/reconciled-writers/<launch-id>/` and returns `quiescenceVerified: true, transferReady: false`. It never infers absence from a PID probe or elapsed time and has no force/skip option. It is internal: `quiesceDatabaseWriters(id, token)` runs producer shutdown then reconciliation, and no operator or public entrypoint invokes either; export/import (#8871) must re-run it before relying on its result. Limits: a descendant that deliberately leaves its session and process group (`setsid`) is not observable by group, and orphans of a pre-registry launch whose supervisor already exited cannot be identified. Records are local recovery evidence included in backups, never a federated application collection or reference seed; do not delete them to unblock maintenance. The fixed maintenance worker can launch while fenced, but can only inspect this bounded inventory: it reports direction, stage, counts, `quiescenceVerified: false`, and `transferReady: false`, then exits with code 78 (transfer not integrated). Empty inventory is not a successful cutover. Damaged ownership or inventory exits with code 1 and a redacted refusal. Its stdout/stderr, owner binding, one-use entry marker, and supervisor receipt remain in the reserved directory. It changes neither stage nor saved mode, stops no processes, and starts no child writers. This internal foundation is not wired to an operator launch or recovery command.
+Run `node scripts/database-maintenance.mjs writers` for bounded counts at any stage (`unresolved`, `launching`, `launched`, `exited`, `abandoned`); it always reports `quiescenceVerified: false` and exposes no control paths or process identities. A malformed inventory refuses the command. A `{ state: 'unresolved', kind: 'retired-exits' }` entry represents legacy or Windows compacted completion history. **An inventory read never proves descendant quiescence.** Only the internal `reconcileDetachedWriters(id, token)` stage (`server/services/databaseMaintenanceQuiescence.js`) can, and only for the owning coordinator worker after `stopOwnedDatabaseProducers` recorded producer shutdown at `quiescing`. It classifies each record against a POSIX process-table snapshot: a job still alive with a verified identity (its PGID is recorded and it started inside the launch window) has its recorded groups sent SIGTERM, then SIGKILL after a grace period; a group seen empty is never signalled again. It refuses, leaving the fence and every record in place, when an admitted launch has not recorded a process identity, when a record is legacy/compacted, lacks a launcher group, or is a process-group job whose own PID was never recorded, when a live PID does not match its recorded identity (possible reuse), when an exited job still has surviving group members, when groups do not terminate, when a detached supervisor of this install's data directory is running without a record (for example a launch from code that predates the registry), or on Windows for anything but abandoned launches. Two consecutive quiescent snapshots are required before it archives every record to `data/database-maintenance/reconciled-writers/<launch-id>/` and returns `quiescenceVerified: true, transferReady: false`. It never infers absence from a PID probe or elapsed time and has no force/skip option. It is internal: `quiesceDatabaseWriters(id, token)` runs producer shutdown then reconciliation, and no operator or public entrypoint invokes either; export/import (#8871) must re-run it before relying on its result. Limits: a descendant that deliberately leaves its session and process group (`setsid`) is not observable by group, and orphans of a pre-registry launch whose supervisor already exited cannot be identified. Records are local recovery evidence included in backups, never a federated application collection or reference seed; do not delete them to unblock maintenance. The fixed maintenance worker can launch while fenced, but can only inspect this bounded inventory: it reports direction, stage, counts, `quiescenceVerified: false`, and `transferReady: false`, then exits with code 78 (transfer not integrated). Empty inventory is not a successful cutover. Damaged ownership or inventory exits with code 1 and a redacted refusal. Its stdout/stderr, owner binding, one-use entry marker, and supervisor receipt remain in the reserved directory. It changes neither stage nor saved mode, stops no processes, and starts no child writers. This internal foundation is not wired to an operator launch or recovery command.
A cancelled unowned accepted operation moves to `data/database-maintenance-cancelled/<operation-id>/` as local recovery evidence. These records remain filesystem-backed and included in backups; they are not a growing application collection. Restoring an active journal deliberately restores its fence: do not delete it merely to make startup succeed. See [database maintenance admission](BACKUP.md#database-maintenance-admission) for the operator contract and current limitations.
diff --git a/server/lib/databaseMaintenanceJournal.js b/server/lib/databaseMaintenanceJournal.js
index 23744ae071..c7f1bc5a58 100644
--- a/server/lib/databaseMaintenanceJournal.js
+++ b/server/lib/databaseMaintenanceJournal.js
@@ -435,7 +435,10 @@ export function createDatabaseMaintenanceJournal(dataDir = PATHS.data) {
return { id, stage: 'cancelled' };
};
- return { isFenced, assertAdmission, read, begin, cancel, acquireCoordinator, transition,
+ // Operation-scoped archive for detached-writer records proven quiescent.
+ const reconciledWritersDirectory = join(activeDir, 'reconciled-writers');
+
+ return { isFenced, assertAdmission, read, begin, cancel, acquireCoordinator, transition, reconciledWritersDirectory,
reserveCoordinatorWorker, coordinatorStatus, recoverCoordinator, assertCoordinatorWorker, enterCoordinatorWorker,
readProducerSnapshot, recordProducerSnapshot };
}
diff --git a/server/lib/databaseWriterRegistry.js b/server/lib/databaseWriterRegistry.js
index 173607599b..727cb7aa62 100644
--- a/server/lib/databaseWriterRegistry.js
+++ b/server/lib/databaseWriterRegistry.js
@@ -175,14 +175,18 @@ const START_SLACK_MS = 2000;
* recorded groups reaches only this launch's tree.
* - orphaned: the job is gone but its groups still hold survivors.
* - pending: an admitted launch that recorded no process identity yet.
- * - ambiguous: legacy/compacted history, no launcher group, or an identity
- * mismatch (possible PID reuse). Never safe to signal or to ignore.
+ * - ambiguous: legacy/compacted history, no launcher group, a process-group
+ * job whose own group was never recorded, or an identity mismatch (possible
+ * PID reuse). Never safe to signal or to ignore.
*/
export function classifyWriterQuiescence(row, processes) {
if (row.kind === 'retired-exits') return { verdict: 'ambiguous', groups: [] };
if (row.state === 'abandoned') return { verdict: 'quiescent', groups: [] };
if (row.state === 'unresolved') return { verdict: 'pending', groups: [] };
if (!row.launcherPid || (row.pid && !row.launchedAt)) return { verdict: 'ambiguous', groups: [] };
+ // A process-group job leaves the launcher group for its own; without its
+ // recorded PID that group is unknown, so an empty launcher group proves nothing.
+ if (row.processGroup && !row.pid) return { verdict: 'ambiguous', groups: [row.launcherPid] };
const groups = [...new Set([row.launcherPid, ...(row.processGroup && row.pid ? [row.pid] : [])])];
const earliest = Math.floor(Date.parse(row.createdAt) / 1000) * 1000 - START_SLACK_MS;
const latest = (row.launchedAt ? Date.parse(row.launchedAt) : Date.now()) + START_SLACK_MS;
diff --git a/server/services/databaseMaintenanceQuiescence.js b/server/services/databaseMaintenanceQuiescence.js
index 936c5a6f1a..8ccc78503b 100644
--- a/server/services/databaseMaintenanceQuiescence.js
+++ b/server/services/databaseMaintenanceQuiescence.js
@@ -1,4 +1,4 @@
-import { join, sep } from 'node:path';
+import { sep } from 'node:path';
import { PATHS } from '../lib/paths.js';
import { createDatabaseMaintenanceJournal } from '../lib/databaseMaintenanceJournal.js';
import { classifyWriterQuiescence, createDatabaseWriterRegistry } from '../lib/databaseWriterRegistry.js';
@@ -92,8 +92,7 @@ export async function reconcileDetachedWriters(id, token, { graceMs = TERMINATE_
assertNoUnknownSupervisors(processes, new Set());
}
journal.assertCoordinatorWorker(id, token);
- const archive = join(PATHS.data, 'database-maintenance', 'reconciled-writers');
- for (const { row } of results) registry.retire(row.id, archive);
+ for (const { row } of results) registry.retire(row.id, journal.reconciledWritersDirectory);
if (registry.read().length !== 0) throw refused('the writer inventory is not empty after retirement');
return { id, stage: 'quiescing', writersReconciled: rows.length, writersTerminated: terminated,
quiescenceVerified: true, transferReady: false };
diff --git a/server/services/databaseMaintenanceQuiescence.test.js b/server/services/databaseMaintenanceQuiescence.test.js
index aa29b1fdc9..934857ef5e 100644
--- a/server/services/databaseMaintenanceQuiescence.test.js
+++ b/server/services/databaseMaintenanceQuiescence.test.js
@@ -135,9 +135,14 @@ describe.skipIf(process.platform === 'win32')('detached writer quiescence reconc
const legacyLaunch = registry.reserve(join(root, 'old-control'));
const writers = join(context.data, 'database-writers');
writeFileSync(join(writers, legacyLaunch.id, 'launch.json'), JSON.stringify({ pid: 999999 }) + '\n');
+ // A process-group job that crashed its supervisor before recording its PID
+ // may live on in its own group; an empty launcher group proves nothing.
+ const groupLaunch = registry.reserve(join(root, 'group-control'), true);
+ groupLaunch.launcher(999998);
const { id, token } = quiescingOperation();
- await expect(reconcileDetachedWriters(id, token, fast)).rejects.toThrow(/legacy, compacted/);
+ await expect(reconcileDetachedWriters(id, token, fast)).rejects.toThrow(/2 writer record\(s\) are legacy, compacted/);
rmSync(join(writers, legacyLaunch.id), { recursive: true });
+ rmSync(join(writers, groupLaunch.id), { recursive: true });
writeFileSync(join(writers, 'unreconciled-exits.json'), JSON.stringify({ version: 1 }) + '\n');
await expect(reconcileDetachedWriters(id, token, fast)).rejects.toThrow(/legacy, compacted/);