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: key database target proofs by process incarnation so reused PIDs can recover

[._.]/ Adam Eivya0c3c922ead9
diff --git a/server/lib/databaseMaintenanceJournal.js b/server/lib/databaseMaintenanceJournal.js
index 000529dbcc..c9d6075a59 100644
--- a/server/lib/databaseMaintenanceJournal.js
+++ b/server/lib/databaseMaintenanceJournal.js
@@ -604,7 +604,8 @@ export function createDatabaseMaintenanceJournal(dataDir = PATHS.data) {
// Operation-scoped archive for detached-writer records proven quiescent.
const reconciledWritersDirectory = join(activeDir, 'reconciled-writers');
- const targetProofPath = pid => join(activeDir, 'target-proof-' + z.number().int().positive().parse(pid) + '.json');
+ const targetProofPath = (pid, startedAt) => join(activeDir, 'target-proof-' + z.number().int().positive().parse(pid)
+ + (startedAt === undefined ? '' : '-' + z.number().int().nullable().parse(startedAt)) + '.json');
// Published by a booting ordinary server AFTER its own pool passed the
// read-only target verification. Only verifying/verified may carry one.
@@ -612,17 +613,20 @@ export function createDatabaseMaintenanceJournal(dataDir = PATHS.data) {
assertNotRealDataWrite(activeDir, 'database maintenance target proof');
const current = read();
if (!current || current.id !== id || !['verifying', 'verified'].includes(current.stage)) throw databaseMaintenanceError();
- publishImmutable(targetProofPath(pid), targetProofSchema.parse({ id, pid, startedAt, target: current.target }));
- return readTargetProof(id, pid);
+ publishImmutable(targetProofPath(pid, startedAt), targetProofSchema.parse({ id, pid, startedAt, target: current.target }));
+ return readTargetProof(id, pid, startedAt);
};
- function readTargetProof(id, pid) {
+ function readTargetProof(id, pid, startedAt) {
const current = read();
if (!current || current.id !== id) throw databaseMaintenanceError();
- const proof = readRecord(targetProofPath(pid), targetProofSchema);
+ // Retain already-written proofs from older installs, but never let a
+ // reused PID inherit evidence belonging to another process incarnation.
+ const proof = readRecord(targetProofPath(pid, startedAt), targetProofSchema)
+ ?? readRecord(targetProofPath(pid), targetProofSchema);
if (proof && (proof.id !== id || proof.pid !== pid
|| JSON.stringify(proof.target) !== JSON.stringify(current.target))) throw databaseMaintenanceError();
- return proof;
+ return proof && startedAt !== undefined && proof.startedAt !== startedAt ? null : proof;
}
/**
diff --git a/server/services/databaseMaintenanceCutover.js b/server/services/databaseMaintenanceCutover.js
index a63ecc5e10..374bf1e4d1 100644
--- a/server/services/databaseMaintenanceCutover.js
+++ b/server/services/databaseMaintenanceCutover.js
@@ -114,11 +114,11 @@ async function commitSavedMode(operation) {
// A proof counts only for the process that wrote it: same pid AND the start
// time the process table reports for that pid now.
-async function proofMatchesProcess(proof, pid) {
- if (!proof) return false;
- if (process.platform === 'win32') return proof.startedAt === null;
+async function proofMatchesProcess(journal, id, pid) {
+ if (process.platform === 'win32') return journal.readTargetProof(id, pid, null)?.startedAt === null;
const row = (await snapshotProcesses().catch(() => [])).find(value => value.pid === pid);
- return Number.isSafeInteger(proof.startedAt) && row?.startedAt === proof.startedAt;
+ if (!Number.isSafeInteger(row?.startedAt)) return false;
+ return journal.readTargetProof(id, pid, row.startedAt)?.startedAt === row.startedAt;
}
/**
@@ -136,7 +136,7 @@ async function awaitRestartedTarget(journal, operation, token, saved, { proofTim
journal.assertEnteredCoordinatorWorker(id, token);
const server = (await readRecordedProducers(saved)).find(row => row.name === 'portos-server');
if (server.status === 'online' && server.pid > 0
- && await proofMatchesProcess(journal.readTargetProof(id, server.pid), server.pid)) return server.pid;
+ && await proofMatchesProcess(journal, id, server.pid)) return server.pid;
if (server.status !== 'online' && !restarted) {
journal.assertEnteredCoordinatorWorker(id, token);
const result = await restartMaintenanceProducer('portos-server', recordedEndpointEnv(operation)).catch(() => null);
diff --git a/server/services/databaseMaintenanceCutover.test.js b/server/services/databaseMaintenanceCutover.test.js
index 178da186ec..8e38ed90d7 100644
--- a/server/services/databaseMaintenanceCutover.test.js
+++ b/server/services/databaseMaintenanceCutover.test.js
@@ -11,6 +11,7 @@ import { spawn } from 'node:child_process';
import { join } from 'node:path';
import { STUB_DUMP_COMPLETE, installDatabaseStubs } from '../test/fixtures/databaseTransferStubs.js';
import { installCutoverStubs, restartedServerEndpoint } from '../test/fixtures/databaseCutoverStubs.js';
+import { snapshotProcesses } from '../lib/detachedSpawn.js';
const context = vi.hoisted(() => ({ root: undefined, list: null, stop: null, restart: null }));
vi.mock('../lib/paths.js', async original => {
@@ -37,6 +38,8 @@ const native = { mode: 'native', host: 'db.example.invalid', port: 5432, databas
const docker = { mode: 'docker', host: 'db.example.invalid', port: 5561, database: 'example_test', user: 'example' };
const fast = { graceMs: 1500, pollMs: 20, proofTimeoutMs: 4000 };
const alive = pid => { try { process.kill(pid, 0); return true; } catch { return false; } };
+const processStart = async pid => process.platform === 'win32' ? null
+ : (await snapshotProcesses()).find(row => row.pid === pid)?.startedAt;
const savedEnv = { ...process.env };
let root;
@@ -197,7 +200,7 @@ describe.skipIf(process.platform === 'win32')('offline database cutover', () =>
// A healthy restarted server proves the target and waits at the fence.
const pid = cutover.launchServer(docker);
rows.find(row => row.name === 'portos-server').pid = pid;
- await vi.waitFor(() => expect(journal.readTargetProof(operation.id, pid)).not.toBeNull(), { timeout: 15_000, interval: 20 });
+ await vi.waitFor(async () => expect(journal.readTargetProof(operation.id, pid, await processStart(pid))).not.toBeNull(), { timeout: 15_000, interval: 20 });
// A worker crashes between verification and release.
successor();
journal.enterCoordinatorWorker(operation.id, token);
@@ -230,6 +233,11 @@ describe.skipIf(process.platform === 'win32')('offline database cutover', () =>
await expect(run()).rejects.toThrow(/did not prove the target backend/);
expect(journal.read().stage).toBe('verifying');
expect(() => journal.assertAdmission()).toThrow();
+ // The new incarnation can publish its own proof without colliding with
+ // immutable evidence left by the earlier holder of this PID.
+ const startedAt = await processStart(impostor.pid);
+ expect(journal.recordTargetProof(operation.id, impostor.pid, startedAt)).toMatchObject({ startedAt });
+ expect(journal.readTargetProof(operation.id, impostor.pid, startedAt)).toMatchObject({ startedAt });
} finally {
impostor.kill('SIGKILL');
}
@@ -242,7 +250,7 @@ describe.skipIf(process.platform === 'win32')('offline database cutover', () =>
cutover.setHealth('healthy');
const pid = cutover.launchServer(docker);
rows.find(row => row.name === 'portos-server').pid = pid;
- await vi.waitFor(() => expect(journal.readTargetProof(operation.id, pid)).not.toBeNull(), { timeout: 15_000, interval: 20 });
+ await vi.waitFor(async () => expect(journal.readTargetProof(operation.id, pid, await processStart(pid))).not.toBeNull(), { timeout: 15_000, interval: 20 });
successor();
journal.enterCoordinatorWorker(operation.id, token);
journal.transition(operation.id, token, 'verifying', 'verified');