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: pin peer-execution receiver re-validation and capability binding refusals (#11874)

Adam Eivyf27462b32ebe
diff --git a/server/services/peerExecution.test.js b/server/services/peerExecution.test.js
index fe464f2657..a36b3bc4e7 100644
--- a/server/services/peerExecution.test.js
+++ b/server/services/peerExecution.test.js
@@ -9,14 +9,14 @@ import { createPeerExecutionGrants } from './peerExecutionGrants.js';
import { peerExecutionPreflightPayloadSchema, peerExecutionDispatchSchema, peerExecutionGrantSchema } from '../lib/peerAdminValidation.js';
import { peerAdminPairBinding } from './peerAdministration.js';
-let directory, pair, ledger, grants, receiver, adapters, coordinator, store, epoch, operations, floors, proofs;
+let clock, directory, pair, ledger, grants, receiver, adapters, coordinator, store, epoch, operations, floors, proofs;
const hash = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
const intent = { action: 'portos.restart' };
const terminal = state => ['succeeded', 'failed'].includes(state);
beforeEach(async () => {
directory = mkdtempSync(join(tmpdir(), 'peer-execution-fixture-'));
pair = { self: { instanceId: randomUUID() }, peer: { id: 'fixture-peer', instanceId: randomUUID(), syncSecret: 'fixture-pair-secret'.repeat(3) } };
- epoch = randomUUID(); operations = new Map(); floors = new Map(); proofs = new Map();
+ clock = 1_000_000; epoch = randomUUID(); operations = new Map(); floors = new Map(); proofs = new Map();
store = { version: 1, grants: [] };
ledger = {
authority: { read: () => ({ epoch, phase: 'ready' }), requireReady: value => { if (value !== epoch) throw Object.assign(new Error('stale epoch'), { code: 'STALE_EPOCH' }); } },
@@ -58,7 +58,7 @@ beforeEach(async () => {
receiver = makeReceiver();
});
afterEach(() => rmSync(directory, { recursive: true, force: true }));
-const makeReceiver = () => createPeerExecutionReceiver({ ledger, grants, coordinator, adapters, terminalProofs: proofs,
+const makeReceiver = () => createPeerExecutionReceiver({ ledger, grants, coordinator, adapters, terminalProofs: proofs, now: () => clock,
caller: async req => { if (req !== 'paired') throw Object.assign(new Error('pair required'), { status: 403 }); return pair; }, version: '1.0.0' });
const grantInput = (extra = {}) => peerExecutionGrantSchema.parse({ peerId: pair.peer.id, action: intent.action,
confirmedHostInstanceId: pair.self.instanceId, confirmedPeerInstanceId: pair.peer.instanceId,
@@ -146,6 +146,35 @@ describe('receiver execution public workflow with fixture-only side effects', ()
await active.finish(); await receiver.drain(); await idle();
expect(adapters.run).not.toHaveBeenCalled();
});
+ it('refuses a re-granted authority at drain time with the authority-changed code', async () => {
+ const active = coordinator.admit('render', 'Fixture render');
+ await grant(); const input = await prepare(); await receiver.dispatch('paired', input);
+ await vi.waitFor(() => expect(coordinator.status().state).toBe('draining'));
+ await grant();
+ await active.finish(); await receiver.drain(); await idle();
+ expect((await receiver.status('paired', input)).payload).toMatchObject({ state: 'failed', code: 'PEER_EXECUTION_AUTHORITY_CHANGED' });
+ expect(adapters.run).not.toHaveBeenCalled();
+ });
+ it('refuses a dispatch whose evidence digest differs from the signed preflight', async () => {
+ await grant(); const input = await prepare();
+ await expect(receiver.dispatch('paired', { ...input, evidenceDigest: '0'.repeat(64) })).rejects.toMatchObject({ code: 'PEER_EXECUTION_PREFLIGHT_STALE' });
+ expect(operations.size).toBe(0);
+ expect(adapters.run).not.toHaveBeenCalled();
+ });
+ it('refuses a dispatch after the signed preflight expired', async () => {
+ await grant(); const input = await prepare();
+ clock += 61_000;
+ await expect(receiver.dispatch('paired', input)).rejects.toMatchObject({ code: 'PEER_EXECUTION_PREFLIGHT_STALE' });
+ expect(operations.size).toBe(0);
+ expect(adapters.run).not.toHaveBeenCalled();
+ });
+ it('rechecks authority at dispatch when the grant is revoked after preflight', async () => {
+ await grant(); const input = await prepare();
+ await grant({ allowExecution: false });
+ await expect(receiver.dispatch('paired', input)).rejects.toMatchObject({ code: 'PEER_EXECUTION_GRANT_REQUIRED' });
+ expect(operations.size).toBe(0);
+ expect(adapters.run).not.toHaveBeenCalled();
+ });
it('fences resume and never relaunches after an ambiguous launch or process restart', async () => {
adapters.run.mockRejectedValue(new Error('fixture transport lost after launch'));
await grant(); const input = await prepare(); await receiver.dispatch('paired', input); await idle();
diff --git a/server/services/peerExecutionAdapters.test.js b/server/services/peerExecutionAdapters.test.js
index 14c464e737..2d5d737c0f 100644
--- a/server/services/peerExecutionAdapters.test.js
+++ b/server/services/peerExecutionAdapters.test.js
@@ -4,6 +4,7 @@ import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { createMaintenanceAdmission } from '../lib/maintenanceAdmission.js';
+import { assertPeerExecutionCapability } from '../lib/maintenanceExclusive.js';
import { _createPeerExecutionAdapters, peerExecutionEvidenceDigest, _verifyPeerExecutionCompletion } from './peerExecutionAdapters.js';
const roots = [];
@@ -93,6 +94,34 @@ describe('fixed peer actions under exclusive receiver ownership', () => {
});
});
+describe('execution capability binding', () => {
+ const restartEvidence = { version: 1, intent: { action: 'portos.restart' }, target: { processes: [{ name: 'portos-server', pid: 1, scriptDigest: 'a'.repeat(64) }] } };
+
+ it('refuses a restart capability presented as an update or against a different evidence digest', () => {
+ const { capability } = owner(restartEvidence);
+ const digest = peerExecutionEvidenceDigest(restartEvidence);
+ expect(assertPeerExecutionCapability(capability, restartEvidence.intent, digest).intent).toEqual(restartEvidence.intent);
+ expect(() => assertPeerExecutionCapability(capability, { action: 'portos.update' }, digest)).toThrow(expect.objectContaining({ code: 'PEER_EXECUTION_CAPABILITY_REQUIRED' }));
+ expect(() => assertPeerExecutionCapability(capability, restartEvidence.intent, 'f'.repeat(64))).toThrow(expect.objectContaining({ code: 'PEER_EXECUTION_CAPABILITY_REQUIRED' }));
+ });
+
+ it('refuses the update executor a restart capability without launching', async () => {
+ const launchUpdate = vi.fn();
+ const adapters = _createPeerExecutionAdapters({ probeUpdate: async () => update(), launchUpdate });
+ const evidence = await adapters.prepare(intent);
+ const { capability } = owner(restartEvidence);
+ await expect(adapters.run(intent, evidence, { capability })).rejects.toMatchObject({ code: 'PEER_EXECUTION_CAPABILITY_REQUIRED' });
+ expect(launchUpdate).not.toHaveBeenCalled();
+ });
+
+ it('refuses to claim a hold while admitted work is still running', () => {
+ const root = mkdtempSync(join(tmpdir(), 'peer-adapters-')); roots.push(root);
+ const coordinator = createMaintenanceAdmission(root);
+ coordinator.admit('render', 'Fixture render');
+ coordinator.begin({ reason: 'Fixture drain', owner: 'Fixture operator' });
+ expect(() => coordinator.observeIdle()).toThrow(expect.objectContaining({ code: 'MAINTENANCE_STALE' }));
+ });
+});
describe('post-restart completion proof', () => {
it('requires the matching durable launch, successful exit and every intended process to have restarted', () => {