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

test: distinguish delayed cutover startup from missing proof (#9368)

[._.]/ Adam Eivy700ef87b4d87
diff --git a/server/services/databaseMaintenanceCutover.test.js b/server/services/databaseMaintenanceCutover.test.js
index ff9fe31cf8..e3e40d562d 100644
--- a/server/services/databaseMaintenanceCutover.test.js
+++ b/server/services/databaseMaintenanceCutover.test.js
@@ -116,7 +116,7 @@ const run = async () => {
try {
return await runDatabaseCutover(operation.id, token, fast);
} catch (err) {
- err.message += `\n--- cutover evidence ---\n${evidence()}`;
+ err.message += `\n--- cutover evidence ---\n${await evidence()}`;
throw err;
}
};
@@ -128,14 +128,25 @@ const serverEvents = () => stubs.events().filter(line => line.startsWith('server
// server's liveness, and the tail of their timestamped stderr trace — enough
// to tell a refused boot from one still waiting (or never started).
const bounded = read => { try { return read(); } catch (err) { return 'unreadable (' + (err.code ?? 'error') + ')'; } };
-function evidence() {
+async function evidence() {
+ // A signal-0 probe cannot distinguish a booting process from one suspended
+ // before its first line. Keep only fixture-owned identities, never commands
+ // or the rest of the host's process table.
+ const processes = await snapshotProcesses().catch(() => null);
+ const surrogates = (cutover?.surrogatePids() ?? []).slice(-20).map(pid => {
+ const row = processes?.find(value => value.pid === pid);
+ return { pid, alive: alive(pid), state: processes === null ? 'unavailable' : row?.state ?? 'gone',
+ startedAt: row?.startedAt ?? null,
+ targetProof: processes === null ? 'unavailable'
+ : row ? bounded(() => journal.readTargetProof(operation.id, pid, row.startedAt) !== null) : 'process-gone' };
+ });
const log = join(stubs.dir, 'surrogate-stderr.log');
return [
`events (last 80): ${JSON.stringify(stubs.events().slice(-80))}`,
`fenced: ${bounded(() => journal.isFenced())}; stage: ${bounded(() => journal.read()?.stage ?? 'none')}`,
`coordinator: ${bounded(() => JSON.stringify(journal.coordinatorStatus(operation.id)))}`,
`authority for this operation: ${bounded(() => createDatabaseAuthority(join(root, 'data')).read()?.operationId === operation.id)}`,
- `surrogates: ${JSON.stringify((cutover?.surrogatePids() ?? []).map(pid => ({ pid, alive: alive(pid) })))}`,
+ `surrogates: ${JSON.stringify(surrogates)}`,
`surrogate stderr: ${existsSync(log) ? readFileSync(log, 'utf8').slice(-3_000) : ''}`,
].join('\n');
}
@@ -144,7 +155,7 @@ async function waitWithEvidence(assertion) {
try {
await vi.waitFor(assertion, { timeout: 15_000, interval: 20 });
} catch (err) {
- err.message += `\n--- cutover evidence ---\n${evidence()}`;
+ err.message += `\n--- cutover evidence ---\n${await evidence()}`;
throw err;
}
}
@@ -209,6 +220,35 @@ describe.skipIf(process.platform === 'win32')('offline database cutover', () =>
expect(journal.read()).toBeNull();
}, 60_000);
+ it('diagnoses a suspended server before startup, stays fenced, then recovers the same operation', async () => {
+ begin();
+ const restart = context.restart.getMockImplementation();
+ context.restart.mockImplementation(async (...args) => {
+ const result = await restart(...args);
+ if (args[0] === 'portos-server') {
+ // Delay the actual process, not a mock handshake or a longer deadline.
+ process.kill(rows.find(row => row.name === 'portos-server').pid, 'SIGSTOP');
+ }
+ return result;
+ });
+ await expect(run()).rejects.toThrow(/did not prove the target backend[\s\S]*"state":"T[^"]*"[\s\S]*"targetProof":false/);
+ expect(journal.read().stage).toBe('verifying');
+ expect(() => journal.assertAdmission()).toThrow();
+ expect(context.restart).not.toHaveBeenCalledWith('portos-cos');
+
+ // The already-started incarnation publishes its own proof after the
+ // operator unblocks startup; recovery must neither restart nor re-import.
+ const pid = rows.find(row => row.name === 'portos-server').pid;
+ process.kill(pid, 'SIGCONT');
+ await waitForProof(pid);
+ successor();
+ expect(await run()).toMatchObject({ stage: 'released', restartVerified: true });
+ await waitForEvent(`server booted ${docker.port}`);
+ expect(context.restart.mock.calls.filter(([name]) => name === 'portos-server')).toHaveLength(1);
+ expect(stubs.invocations('pg_dump')).toHaveLength(1);
+ expect(stubs.invocations('psql')).toHaveLength(1);
+ }, 60_000);
+
it.each(['before', 'after', 'reverted'])('recovers a mode commit interrupted %s the .env write without guessing or reversing', async (point) => {
begin();
await runDatabaseTransfer(operation.id, token, fast);
diff --git a/server/test/fixtures/databaseCutoverStubs.js b/server/test/fixtures/databaseCutoverStubs.js
index 7b7a23d6bf..08008be0a6 100644
--- a/server/test/fixtures/databaseCutoverStubs.js
+++ b/server/test/fixtures/databaseCutoverStubs.js
@@ -70,7 +70,9 @@ if (process.argv.includes('--owner-bound')) {
process.stdin.resume();
}
try {
+ trace('handshake import started');
const { awaitDatabaseCutoverRelease } = await import(${JSON.stringify(handshakeUrl)});
+ trace('handshake imported');
const released = await awaitDatabaseCutoverRelease({ pollMs: 20, releaseTimeoutMs: 20000 });
trace('handshake returned released=' + released.released);
await import(${JSON.stringify(bootFenceUrl)});
@@ -133,6 +135,7 @@ export function launchSurrogateServer(root, dir, endpoint, onOwnedChild) {
const args = [join(dir, 'server-surrogate.mjs'), ...(onOwnedChild ? ['--owner-bound'] : [])];
const child = spawn(process.execPath, args, { env, stdio: [onOwnedChild ? 'pipe' : 'ignore', 'ignore', stderr], detached: true });
closeSync(stderr);
+ appendFileSync(join(dir, 'surrogate-stderr.log'), new Date().toISOString() + ' launched surrogate ' + child.pid + '\n');
child.once('error', err => console.error('Cutover surrogate launch failed: ' + (err.code ?? 'error')));
onOwnedChild?.(child);
child.stdin?.unref();