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: require operator authority for Digital Twin AI actions (#10671)

Adam Eivy31db305036fe
diff --git a/server/lib/hostControlRoutes.js b/server/lib/hostControlRoutes.js
index 25210ecd9e..398fa4ad64 100644
--- a/server/lib/hostControlRoutes.js
+++ b/server/lib/hostControlRoutes.js
@@ -276,6 +276,32 @@ export const HOST_CONTROL_ROUTES = Object.freeze([
'POST /api/digital-twin/personas',
'PUT /api/digital-twin/personas/:id',
// /personas/:id also covers /personas/active.
+
+ // Digital Twin AI actions (#10671) reach the shared CLI/TUI-capable runner,
+ // directly or via imported-data analysis. Gate API-first requests too: their
+ // configured fallback can execute a process. Authorize before private reads,
+ // document writes or behavioral batches; reference CRUD, bounded API-only
+ // taste/vision calls and deterministic recomputation remain open.
+ 'POST /api/digital-twin/enrich/question',
+ 'POST /api/digital-twin/enrich/answer',
+ 'POST /api/digital-twin/enrich/analyze-list',
+ 'POST /api/digital-twin/validate/contradictions',
+ 'POST /api/digital-twin/analyze-writing',
+ 'POST /api/digital-twin/style/spoken-written',
+ 'POST /api/digital-twin/traits/analyze',
+ 'POST /api/digital-twin/confidence/calculate',
+ 'POST /api/digital-twin/interview/analyze',
+ 'POST /api/digital-twin/tests/run',
+ 'POST /api/digital-twin/tests/run-multi',
+ 'POST /api/digital-twin/tests/generate',
+ 'POST /api/digital-twin/values-tests/run',
+ 'POST /api/digital-twin/adversarial-tests/run',
+ 'POST /api/digital-twin/multi-turn-tests/run',
+ 'POST /api/digital-twin/avatar-bio/polish',
+ 'POST /api/digital-twin/import/analyze',
+ 'POST /api/digital-twin/import/spotify/browser/import',
+ 'POST /api/digital-twin/twin-evidence/interpret',
+
'POST /api/tools',
'PUT /api/tools/:id',
diff --git a/server/lib/hostControlRoutes.test.js b/server/lib/hostControlRoutes.test.js
index 7aebfcadb1..784c35ac75 100644
--- a/server/lib/hostControlRoutes.test.js
+++ b/server/lib/hostControlRoutes.test.js
@@ -109,7 +109,7 @@ describe('browser/runtime/database mutation inventory (#8798, #8897)', () => {
});
-describe('prompt-feeding store mutation inventory (#9040)', () => {
+describe('prompt-feeding store and Digital Twin mutation inventory (#9040, #10671)', () => {
// Reviewed reference-data CRUD, fixed-provider inference, previews and stops.
// Twin settings alone are body-gated in their handler; memories and twin
// documents/traits are fenced at prompt assembly. Keep this list explicit:
@@ -144,19 +144,12 @@ describe('prompt-feeding store mutation inventory (#9040)', () => {
"POST /api/cos/mind/recipes/:recipeId/archive",
"POST /api/cos/mind/recipes/validate",
"POST /api/cos/mind/stop",
- "POST /api/digital-twin/adversarial-tests/run",
- "POST /api/digital-twin/analyze-writing",
"POST /api/digital-twin/autobiography/stories",
"POST /api/digital-twin/autobiography/stories/:id/evaluate",
"POST /api/digital-twin/autobiography/stories/:id/follow-ups",
"POST /api/digital-twin/autobiography/stories/:id/weave",
"POST /api/digital-twin/autobiography/trigger",
- "POST /api/digital-twin/avatar-bio/polish",
- "POST /api/digital-twin/confidence/calculate",
"POST /api/digital-twin/documents",
- "POST /api/digital-twin/enrich/analyze-list",
- "POST /api/digital-twin/enrich/answer",
- "POST /api/digital-twin/enrich/question",
"POST /api/digital-twin/enrich/save-list",
"POST /api/digital-twin/export",
"POST /api/digital-twin/feedback",
@@ -180,28 +173,16 @@ describe('prompt-feeding store mutation inventory (#9040)', () => {
"POST /api/digital-twin/identity/image",
"POST /api/digital-twin/identity/image/save",
"POST /api/digital-twin/identity/longevity/derive",
- "POST /api/digital-twin/import/analyze",
"POST /api/digital-twin/import/save",
- "POST /api/digital-twin/import/spotify/browser/import",
"POST /api/digital-twin/import/spotify/browser/open",
- "POST /api/digital-twin/interview/analyze",
- "POST /api/digital-twin/multi-turn-tests/run",
"POST /api/digital-twin/snapshots",
"POST /api/digital-twin/snapshots/compare",
"POST /api/digital-twin/social-accounts",
"POST /api/digital-twin/social-accounts/bulk",
- "POST /api/digital-twin/style/spoken-written",
"POST /api/digital-twin/taste/:section/personalized-question",
"POST /api/digital-twin/taste/answer",
"POST /api/digital-twin/taste/summary",
- "POST /api/digital-twin/tests/generate",
- "POST /api/digital-twin/tests/run",
- "POST /api/digital-twin/tests/run-multi",
- "POST /api/digital-twin/traits/analyze",
- "POST /api/digital-twin/twin-evidence/interpret",
"POST /api/digital-twin/twin-evidence/recompute",
- "POST /api/digital-twin/validate/contradictions",
- "POST /api/digital-twin/values-tests/run",
"POST /api/memory",
"POST /api/memory/:id/approve",
"POST /api/memory/:id/reject",
@@ -229,7 +210,7 @@ describe('prompt-feeding store mutation inventory (#9040)', () => {
"PUT /api/memory/:id"
];
- it('classifies every mounted mutation, including the tool writes from #9014', () => {
+ it('classifies every mounted mutation, including tool writes and process-capable Digital Twin actions', () => {
const mutations = getApiRouteCatalog().routes.filter(({ method, path }) =>
/^(POST|PUT|PATCH|DELETE)$/.test(method)
&& /^\/api\/(?:(tools|prompts|memory|digital-twin)(\/|$)|cos\/(mind|goal-fidelity)(\/|$))/.test(path));
diff --git a/server/routes/hostControlDigitalTwin.test.js b/server/routes/hostControlDigitalTwin.test.js
new file mode 100644
index 0000000000..5127716d55
--- /dev/null
+++ b/server/routes/hostControlDigitalTwin.test.js
@@ -0,0 +1,188 @@
+import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import { EventEmitter } from 'node:events';
+import express from 'express';
+import { request } from '../lib/testHelper.js';
+import { errorMiddleware } from '../lib/errorHandler.js';
+import { DEV_PROXY_CLIENT_ADDRESS_HEADER } from '../../lib/portosAuthCore.js';
+import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../lib/mockPathsDataRoot.js';
+import { derivePeerAuthToken, PEER_AUTH_HEADER, PEER_INSTANCE_HEADER } from '../lib/peerHttpClient.js';
+
+// Real gates, mounted routers and schemas. Stub the first service boundaries:
+// refusal must happen before private reads, document writes or provider dispatch.
+const auth = vi.hoisted(() => ({ enabled: false }));
+const effects = vi.hoisted(() => Object.fromEntries([
+ 'generateEnrichmentQuestion', 'processEnrichmentAnswer', 'analyzeEnrichmentList',
+ 'detectContradictions', 'analyzeWritingSamples', 'compareSpokenWrittenStyle',
+ 'analyzeTraits', 'calculateConfidence', 'analyzeAssessment', 'runTests',
+ 'generateDynamicTests', 'runValuesAlignmentTests', 'runAdversarialTests',
+ 'runMultiTurnTests', 'polishAvatarBio', 'analyzeImportedData', 'getPersonaById',
+ 'saveEnrichmentListDocument', 'createDocument', 'updateTraits',
+ 'saveImportAsDocument', 'analyzeIdentityImage', 'getTestHistory',
+].map(name => [name, vi.fn(async () => ({ id: 'example-record' }))])));
+const evidence = vi.hoisted(() => ({
+ interpretConsumption: vi.fn(async () => ({ text: 'Example interpretation' })),
+ aggregateTwinEvidence: vi.fn(async () => ({})),
+ getObservedEvidence: vi.fn(async () => ({})),
+}));
+const spotify = vi.hoisted(() => ({
+ openSpotifyBrowser: vi.fn(async () => ({})),
+ importSpotifyFromBrowser: vi.fn(async () => ({})),
+}));
+vi.mock('../lib/fileUtils.js', async (original) => ({
+ ...makePathsProxy(await original(), { dataRoot: () => lazyTempDataRoot('portos-twin-authority-') }),
+}));
+afterAll(cleanupTempDataRoots);
+vi.mock('../services/auth.js', async (original) => ({
+ ...await original(),
+ isAuthEnabled: vi.fn(async () => auth.enabled),
+ verifyPassword: vi.fn(async () => true),
+}));
+vi.mock('../services/settings.js', () => ({
+ settingsEvents: new EventEmitter(), getSettings: vi.fn(async () => ({})),
+}));
+vi.mock('../services/instanceIdentity.js', () => ({
+ loadData: vi.fn(async () => ({ peers: [{
+ id: 'example-peer', instanceId: 'example-instance', enabled: true, syncSecret: 'a'.repeat(32),
+ }] })),
+}));
+vi.mock('../services/digital-twin.js', () => effects);
+vi.mock('../services/twinEnrichment.js', () => evidence);
+vi.mock('../services/spotifyBrowserImport.js', () => spotify);
+
+import { authGate, hostControlRouteGate } from '../services/authGate.js';
+import { createSession } from '../services/auth.js';
+import enrichmentRoutes from './digital-twin/enrichment.js';
+import analysisRoutes from './digital-twin/analysis.js';
+import testRoutes from './digital-twin/tests.js';
+import avatarBioRoutes from './digital-twin/avatar-bio.js';
+import importRoutes from './digital-twin/import.js';
+import evidenceRoutes from './digital-twin/evidence.js';
+import documentRoutes from './digital-twin/documents.js';
+
+const picker = { providerId: 'example-cli', model: 'example-model' };
+const personaId = '00000000-0000-4000-8000-000000000001';
+const items = [{ title: 'Example book', note: 'Example note' }];
+const operations = [
+ ['enrich/question', { category: 'favorite_books', providerOverride: picker.providerId }, effects.generateEnrichmentQuestion],
+ ['enrich/answer', { questionId: personaId, category: 'favorite_books', question: 'Example question?', answer: 'Example answer', providerOverride: picker.providerId }, effects.processEnrichmentAnswer],
+ ['enrich/analyze-list', { category: 'favorite_books', items, ...picker }, effects.analyzeEnrichmentList],
+ ['validate/contradictions', picker, effects.detectContradictions],
+ ['analyze-writing', { samples: ['Example writing sample'], ...picker }, effects.analyzeWritingSamples],
+ ['style/spoken-written', { spokenTranscript: 'Example transcript. '.repeat(10), ...picker }, effects.compareSpokenWrittenStyle],
+ ['traits/analyze', picker, effects.analyzeTraits],
+ ['confidence/calculate', picker, effects.calculateConfidence],
+ ['interview/analyze', { content: 'Example assessment. '.repeat(5), ...picker }, effects.analyzeAssessment],
+ ['tests/run', { ...picker, personaId }, effects.runTests],
+ ['tests/run-multi', { providers: [picker], personaId }, effects.runTests],
+ ['tests/generate', picker, effects.generateDynamicTests],
+ ['values-tests/run', { ...picker, personaId }, effects.runValuesAlignmentTests],
+ ['adversarial-tests/run', { ...picker, personaId }, effects.runAdversarialTests],
+ ['multi-turn-tests/run', { ...picker, personaId }, effects.runMultiTurnTests],
+ ['avatar-bio/polish', picker, effects.polishAvatarBio],
+ ['import/analyze', { source: 'goodreads', data: 'Example import data', ...picker }, effects.analyzeImportedData],
+ ['import/spotify/browser/import', picker, spotify.importSpotifyFromBrowser],
+ ['twin-evidence/interpret', picker, evidence.interpretConsumption],
+];
+const appFor = (address = '192.0.2.10') => {
+ const app = express();
+ app.use((req, _res, next) => {
+ Object.defineProperty(req.socket, 'remoteAddress', { value: address });
+ next();
+ });
+ app.use(authGate, hostControlRouteGate, express.json());
+ for (const router of [enrichmentRoutes, analysisRoutes, testRoutes, avatarBioRoutes, importRoutes, evidenceRoutes, documentRoutes]) {
+ app.use('/api/digital-twin', router);
+ }
+ app.use(errorMiddleware);
+ return app;
+};
+const call = (app, path, body, headers = {}, method = 'post') => {
+ const pending = request(app)[method]('/api/digital-twin/' + path);
+ for (const [name, value] of Object.entries(headers)) pending.set(name, value);
+ return pending.send(body);
+};
+const expectNoEffects = () => {
+ for (const effect of [...Object.values(effects), ...Object.values(evidence), ...Object.values(spotify)]) {
+ expect(effect).not.toHaveBeenCalled();
+ }
+};
+beforeEach(() => {
+ vi.clearAllMocks();
+ auth.enabled = false;
+});
+
+describe('Digital Twin process-capable AI authority (#10671)', () => {
+ it('refuses remote anonymous operations before any effect, including API-first fallback and Express path variants', async () => {
+ const app = appFor();
+ for (const [path, body] of operations) {
+ for (const candidate of [body, { ...body, providerId: 'example-api', providerOverride: 'example-api', providers: [{ providerId: 'example-api', model: picker.model }] }]) {
+ const response = await call(app, path, candidate);
+ expect([response.status, response.body.code], path).toEqual([403, 'HOST_CONTROL_FORBIDDEN']);
+ }
+ const variant = await call(app, path.toUpperCase() + '/', body);
+ expect([variant.status, variant.body.code], path).toEqual([403, 'HOST_CONTROL_FORBIDDEN']);
+ }
+ expectNoEffects();
+ });
+
+ it('refuses forwarded local, anonymous auth-on, Basic and scoped-peer requests before any effect', async () => {
+ for (const [enabled, address, headers, status, code] of [
+ [false, '127.0.0.1', { [DEV_PROXY_CLIENT_ADDRESS_HEADER]: '192.0.2.10' }, 403, 'HOST_CONTROL_FORBIDDEN'],
+ [true, '192.0.2.10', {}, 401, 'AUTH_REQUIRED'],
+ [true, '192.0.2.10', { Authorization: 'Basic ' + Buffer.from(':example-password').toString('base64') }, 403, 'HOST_CONTROL_FORBIDDEN'],
+ [true, '192.0.2.10', {
+ [PEER_AUTH_HEADER]: derivePeerAuthToken('a'.repeat(32), 'example-instance'),
+ [PEER_INSTANCE_HEADER]: 'example-instance',
+ }, 403, 'PEER_SCOPE_FORBIDDEN'],
+ ]) {
+ auth.enabled = enabled;
+ const app = appFor(address);
+ for (const [path, body] of operations) {
+ const response = await call(app, path, body, headers);
+ expect([response.status, response.body.code], path).toEqual([status, code]);
+ }
+ }
+ expectNoEffects();
+ });
+
+ it('preserves genuine local, operator-session and delegated-agent actions through real schemas', async () => {
+ const operator = await createSession();
+ const agent = await createSession({ label: 'agent' });
+ for (const [enabled, address, headers] of [
+ [false, '127.0.0.1', {}],
+ [true, '192.0.2.10', { Cookie: `portos_auth=${operator.token}` }],
+ [true, '127.0.0.1', { Authorization: `Bearer ${agent.token}` }],
+ ]) {
+ auth.enabled = enabled;
+ const app = appFor(address);
+ for (const [path, body, effect] of operations) {
+ const before = effect.mock.calls.length;
+ const response = await call(app, path, body, headers);
+ expect(response.status, `${path}: ${JSON.stringify(response.body)}`).toBe(200);
+ expect(effect, path).toHaveBeenCalledTimes(before + 1);
+ }
+ }
+ expect(effects.getPersonaById).toHaveBeenCalledTimes(15);
+ expect(effects.analyzeWritingSamples).toHaveBeenLastCalledWith(['Example writing sample'], picker.providerId, picker.model);
+ });
+
+ it('preserves remote data editing, deterministic evidence recomputation, bounded vision and history', async () => {
+ const app = appFor();
+ for (const [method, path, body, effect, status] of [
+ ['post', 'enrich/save-list', { category: 'favorite_books', content: 'Example content', items }, effects.saveEnrichmentListDocument, 200],
+ ['post', 'documents', { filename: 'EXAMPLE.md', title: 'Example document', category: 'core', content: 'Example reference' }, effects.createDocument, 201],
+ ['put', 'traits', { bigFive: { O: 0.5 } }, effects.updateTraits, 200],
+ ['post', 'import/save', { source: 'goodreads', suggestedDoc: { filename: 'EXAMPLE.md', content: 'Example reference' } }, effects.saveImportAsDocument, 200],
+ ['post', 'twin-evidence/recompute', {}, evidence.aggregateTwinEvidence, 200],
+ ['post', 'identity/image', { imageDataUrl: 'data:image/png;base64,ZXhhbXBsZQ==', ...picker }, effects.analyzeIdentityImage, 200],
+ ['get', 'tests/history', undefined, effects.getTestHistory, 200],
+ ]) {
+ const before = effect.mock.calls.length;
+ const response = await call(app, path, body, {}, method);
+ expect(response.status, path).toBe(status);
+ expect(effect, path).toHaveBeenCalledTimes(before + 1);
+ }
+ for (const [, , effect] of operations) expect(effect).not.toHaveBeenCalled();
+ expect(effects.getPersonaById).not.toHaveBeenCalled();
+ });
+});