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: bind peer-admin execution spies to the real launch seams (#11873)

Adam Eivy078175b2de1d
diff --git a/server/routes/peerAdministration.test.js b/server/routes/peerAdministration.test.js
index 6a650ce0d5..c5048476e1 100644
--- a/server/routes/peerAdministration.test.js
+++ b/server/routes/peerAdministration.test.js
@@ -26,11 +26,18 @@ vi.mock('../lib/db.js', async () => ({
vi.mock('../lib/peerHttpClient.js', async () => ({
...await vi.importActual('../lib/peerHttpClient.js'), peerFetch: vi.fn(),
}));
-// Any accidental executor integration fails the no-active-interruption contract.
+// Spies sit on the seams production executors really call (see peerExecutionAdapters.js
+// launchUpdate/launchRestart/installCatalog); any accidental executor integration fails
+// the no-active-interruption contract. The restart and install doubles spread the actual
+// module so a removed export fails here instead of passing as a phantom double.
const execution = vi.hoisted(() => ({ update: vi.fn(), restart: vi.fn(), install: vi.fn() }));
vi.mock('../services/portosSelfUpdate.js', () => ({ startPortosSelfUpdate: execution.update }));
-vi.mock('../services/localLlm.js', () => ({ installModel: execution.install }));
-vi.mock('../services/pm2.js', () => ({ restartProcess: execution.restart }));
+vi.mock('../lib/detachedSpawn.js', async () => ({
+ ...await vi.importActual('../lib/detachedSpawn.js'), spawnDetached: execution.restart,
+}));
+vi.mock('../services/peerCatalogInstaller.js', async () => ({
+ ...await vi.importActual('../services/peerCatalogInstaller.js'), installPeerCatalog: execution.install,
+}));
const { authGate, hostControlRouteGate } = await import('../services/authGate.js');
const { default: receiver } = await import('./peerAdministration.js');
diff --git a/server/services/peerExecutionAdapters.production.test.js b/server/services/peerExecutionAdapters.production.test.js
index e1b451153c..84fd78f8fc 100644
--- a/server/services/peerExecutionAdapters.production.test.js
+++ b/server/services/peerExecutionAdapters.production.test.js
@@ -1,5 +1,20 @@
-import { describe, expect, it, vi } from 'vitest';
+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
+import { randomUUID } from 'node:crypto';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { createMaintenanceAdmission } from '../lib/maintenanceAdmission.js';
+const tmp = vi.hoisted(() => ({ data: '' }));
+vi.mock('../lib/paths.js', async () => {
+ const actual = await vi.importActual('../lib/paths.js');
+ // Lazy: the temp root is allocated in beforeAll, after this factory has run.
+ return { ...actual, PATHS: new Proxy(actual.PATHS, { get: (target, key) => key === 'data' ? tmp.data : target[key] }) };
+});
+const detached = vi.hoisted(() => ({ spawn: vi.fn() }));
+vi.mock('../lib/detachedSpawn.js', async () => ({
+ ...await vi.importActual('../lib/detachedSpawn.js'), spawnDetached: detached.spawn,
+}));
vi.mock('./updateChecker.js', () => ({ getUpdateStatus: async () => ({ latestRelease: { tag: 'v1.2.3' },
remoteInfo: { hasOrigin: true, isFork: false }, forkSyncFresh: false }) }));
vi.mock('./updateRepoReadiness.js', () => ({ checkUpdateRepoReadiness: async () => ({ ready: false,
@@ -27,3 +42,33 @@ describe('production preview before graceful draining', () => {
expect(getPersistentMindImageWorkGuard).not.toHaveBeenCalled();
});
});
+
+// Positive control for the peer-admin route test: an authorized restart reaches the
+// same detachedSpawn seam that the refusal-path spies bind to, so those spies observe
+// a real executor path rather than a phantom export.
+describe('authorized production restart reaches the detached launch seam', () => {
+ beforeAll(() => { tmp.data = mkdtempSync(join(tmpdir(), 'peer-restart-')); });
+ afterAll(() => rmSync(tmp.data, { recursive: true, force: true }));
+ it('spawns exactly one pm2 restart under an exclusive capability', async () => {
+ const { peerExecutionEvidenceDigest, preparePeerExecution, runPeerExecution } = await import('./peerExecutionAdapters.js');
+ detached.spawn.mockResolvedValueOnce({ on: vi.fn() });
+ vi.mocked(countActiveCosAgents).mockResolvedValueOnce(0);
+ vi.mocked(getPersistentMindImageWorkGuard).mockResolvedValueOnce({ trusted: true, safe: true });
+ const intent = { action: 'portos.restart' };
+ const evidence = await preparePeerExecution(intent);
+ const coordinator = createMaintenanceAdmission(tmp.data);
+ const hold = coordinator.begin({ reason: 'Fixture drain', owner: 'Fixture operator' }).hold;
+ const claim = coordinator.claimReady({ id: hold.id, revision: hold.revision, operation: {
+ operationId: randomUUID(), requestId: randomUUID(), peerInstanceId: randomUUID(), hostInstanceId: randomUUID(),
+ grantId: randomUUID(), grantGeneration: 1, scope: 'execution-v1', pairBinding: 'd'.repeat(64),
+ intent, receiverVersion: '1.2.3', evidenceDigest: peerExecutionEvidenceDigest(evidence),
+ } }, coordinator.observeIdle());
+ const started = coordinator.transitionExclusive({ id: claim.id, revision: claim.revision, fingerprint: claim.fingerprint },
+ 'in-flight', coordinator.observeIdle());
+ const capability = coordinator.issueExecutionCapability(started);
+ await expect(runPeerExecution(intent, evidence, { capability })).resolves.toEqual({
+ state: 'awaiting-reconnect', code: 'PEER_EXECUTION_RESTART_STARTED' });
+ expect(detached.spawn).toHaveBeenCalledTimes(1);
+ expect(detached.spawn.mock.calls[0][1]).toEqual(expect.arrayContaining(['restart', 'portos-server']));
+ });
+});