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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: require operator authority for Music Video agents (#9869)
Adam Eivy6de5f6b434b0
diff --git a/server/lib/hostControlRoutes.js b/server/lib/hostControlRoutes.jsindex 49e79d3990..edf1a73da9 100644--- a/server/lib/hostControlRoutes.js+++ b/server/lib/hostControlRoutes.js@@ -308,6 +308,26 @@ export const HOST_CONTROL_ROUTES = Object.freeze(['POST /api/tracks/:id/waveform/draw','POST /api/music-video/autonomous','POST /api/music-video/:id/autonomous/resume',+ // Music Video planning, authoring and review may dispatch CLI/TUI agents+ // directly, by fallback or after a checkpoint (#9869). Gate each operation+ // uniformly, including deterministic plan/compile options. Creative approval+ // and toolFree authoring flags do not grant network execution authority.+ 'POST /api/music-video/:id/plan',+ 'POST /api/music-video/:id/treatment/compile',+ 'POST /api/music-video/:id/publish-kit/copy',+ 'POST /api/music-video/:id/cast-and-sets',+ 'POST /api/music-video/:id/cast-and-sets/regenerate',+ 'PATCH /api/music-video/:id/cast-and-sets/direction',+ 'POST /api/music-video/:id/cast-and-sets/resume',+ 'POST /api/music-video/:id/code/generate',+ 'POST /api/music-video/:id/code/sections/:sectionId/regenerate',+ 'POST /api/music-video/:id/composition/document/generate',+ 'POST /api/music-video/:id/composition/document/events/revise',+ 'POST /api/music-video/:id/composition/document/sections/:sectionId/regenerate',+ 'POST /api/music-video/:id/production-runs',+ 'POST /api/music-video/:id/production-runs/:runId/resume',+ 'POST /api/music-video/:id/auto-reviews',+ 'POST /api/music-video/:id/auto-reviews/:runId/resume',// Media agents retain host tools; scratch cwd and output validation are not// execution containment (#9667). Gate before uploads, stores or queue writes,diff --git a/server/lib/hostControlRoutes.test.js b/server/lib/hostControlRoutes.test.jsindex 1eed1ef8e8..09ead2842e 100644--- a/server/lib/hostControlRoutes.test.js+++ b/server/lib/hostControlRoutes.test.js@@ -267,3 +267,51 @@ describe('auxiliary media mutation inventory (#9672)', () => {expect([...new Set(open)].sort()).toEqual([...recordOrContained].sort());});});+++describe('Music Video agent workflow policy (#9869)', () => {+ it('gates mounted agent workflows and keeps record, cancellation and contained rendering contracts', () => {+ const protectedRoutes = [+ 'POST /api/music-video/:id/plan',+ 'POST /api/music-video/:id/treatment/compile',+ 'POST /api/music-video/:id/publish-kit/copy',+ 'POST /api/music-video/:id/cast-and-sets',+ 'POST /api/music-video/:id/cast-and-sets/regenerate',+ 'PATCH /api/music-video/:id/cast-and-sets/direction',+ 'POST /api/music-video/:id/cast-and-sets/resume',+ 'POST /api/music-video/:id/code/generate',+ 'POST /api/music-video/:id/code/sections/:sectionId/regenerate',+ 'POST /api/music-video/:id/composition/document/generate',+ 'POST /api/music-video/:id/composition/document/events/revise',+ 'POST /api/music-video/:id/composition/document/sections/:sectionId/regenerate',+ 'POST /api/music-video/:id/production-runs',+ 'POST /api/music-video/:id/production-runs/:runId/resume',+ 'POST /api/music-video/:id/auto-reviews',+ 'POST /api/music-video/:id/auto-reviews/:runId/resume',+ ];+ const mounted = new Set(getApiRouteCatalog().routes.map(({ method, path }) => method + ' ' + path));+ for (const route of protectedRoutes) {+ const [method, path] = route.split(' ');+ expect(mounted.has(route), route).toBe(true);+ expect(hostControlRouteFor(method, path), route).toBe(route);+ }+ for (const route of [+ 'GET /api/music-video/:id',+ 'PATCH /api/music-video/:id',+ 'POST /api/music-video/:id/production-runs/:runId/stop',+ 'POST /api/music-video/:id/production-runs/:runId/cancel',+ 'POST /api/music-video/:id/auto-reviews/:runId/stop',+ 'POST /api/music-video/:id/auto-reviews/:runId/cancel',+ 'POST /api/music-video/:id/composition/document/directory',+ 'GET /api/music-video/:id/composition/document',+ 'POST /api/music-video/:id/render',+ 'POST /api/music-video/:id/publish-kit/build',+ 'POST /api/music-video/:id/publish/:target/prepare',+ 'POST /api/music-video/:id/publish/drafts/:draftId/submit',+ ]) {+ const [method, path] = route.split(' ');+ expect(mounted.has(route), route).toBe(true);+ expect(isHostControlRoute(method, path), route).toBe(false);+ }+ });+});diff --git a/server/routes/hostControlMediaAuthoring.test.js b/server/routes/hostControlMediaAuthoring.test.jsindex ca1a1351c7..0a0cc3c67b 100644--- a/server/routes/hostControlMediaAuthoring.test.js+++ b/server/routes/hostControlMediaAuthoring.test.js@@ -1,16 +1,28 @@-import { beforeEach, describe, expect, it, vi } from 'vitest';+import { afterAll, afterEach, 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 { DEV_PROXY_CLIENT_ADDRESS_HEADER, SESSION_TTL_MS } from '../../lib/portosAuthCore.js';+import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../lib/mockPathsDataRoot.js';+import { derivePeerAuthToken, PEER_AUTH_HEADER, PEER_INSTANCE_HEADER } from '../lib/peerHttpClient.js';++vi.mock('../lib/fileUtils.js', async (importOriginal) =>+ makePathsProxy(await importOriginal(), { dataRoot: () => lazyTempDataRoot('portos-media-authority-') }));+afterAll(cleanupTempDataRoots);+afterEach(() => vi.restoreAllMocks());+vi.mock('../services/instanceIdentity.js', () => ({+ loadData: vi.fn(async () => ({ peers: [{+ id: 'example-peer', instanceId: 'example-instance', enabled: true, syncSecret: 'a'.repeat(32),+ }] })),+}));// Mount the production routers behind the real gates. Service boundaries are// doubled so authoring, persistence, background jobs and processes cannot run.const auth = vi.hoisted(() => ({ enabled: false }));-vi.mock('../services/auth.js', () => ({+vi.mock('../services/auth.js', async (importOriginal) => ({+ ...await importOriginal(),isAuthEnabled: vi.fn(async () => auth.enabled),- verifyRequestSession: vi.fn(async req => req.headers.authorization === 'Bearer example-session'),verifyPassword: vi.fn(async () => true),}));vi.mock('../services/settings.js', () => ({@@ -75,27 +87,51 @@ vi.mock('../services/musicVideo/audioAnalysis.js', () => ({}));vi.mock('../services/musicVideo/projectAudio.js', () => ({}));vi.mock('../services/musicVideo/render.js', () => ({}));vi.mock('../services/musicVideo/codeRender.js', () => ({}));-vi.mock('../services/musicVideo/codeGeneration.js', () => ({}));+vi.mock('../services/musicVideo/codeGeneration.js', () => ({+ generateMusicVideoCode: vi.fn(async () => ({ code: 'example' })),+ regenerateMusicVideoCodeSection: vi.fn(async () => ({ code: 'example' })),+}));vi.mock('../services/musicVideo/excerptRender.js', () => ({}));vi.mock('../services/musicVideo/excerptService.js', () => ({}));vi.mock('../services/musicVideo/socialCuts.js', () => ({}));-vi.mock('../services/musicVideo/publishKit.js', () => ({}));+vi.mock('../services/musicVideo/publishKit.js', () => ({+ draftPublishKitCopy: vi.fn(async () => ({ project: {} })),+}));vi.mock('../services/musicVideo/publish/index.js', () => ({}));vi.mock('../services/musicVideo/publish/platforms.js', () => ({}));vi.mock('../services/musicVideo/revisionService.js', () => ({}));-vi.mock('../services/musicVideo/autoReviewService.js', () => ({}));-vi.mock('../services/musicVideo/productionService.js', () => ({}));-vi.mock('../services/musicVideo/planner.js', () => ({}));+vi.mock('../services/musicVideo/autoReviewService.js', () => ({+ startAutoReview: vi.fn(async () => ({ status: 'running' })),+ resumeAutoReview: vi.fn(async () => ({ status: 'running' })),+}));+vi.mock('../services/musicVideo/productionService.js', () => ({+ startProduction: vi.fn(async () => ({ status: 'running' })),+ resumeProduction: vi.fn(async () => ({ status: 'running' })),+}));+vi.mock('../services/musicVideo/planner.js', () => ({+ planProject: vi.fn(async () => ({ project: {}, promptsSeeded: false })),+}));vi.mock('../services/musicVideo/documentPreview.js', () => ({}));-vi.mock('../services/musicVideo/documentGeneration.js', () => ({}));+vi.mock('../services/musicVideo/documentGeneration.js', () => ({+ generateMixedMediaDocument: vi.fn(async () => ({ document: {} })),+ reviseMixedMediaEvents: vi.fn(async () => ({ document: {} })),+ regenerateMixedMediaSection: vi.fn(async () => ({ document: {} })),+}));vi.mock('../services/musicVideo/timedText.js', () => ({}));vi.mock('../services/musicVideo/lyricAlign.js', () => ({}));vi.mock('../services/musicVideo/trackLyrics.js', () => ({}));vi.mock('../services/musicVideo/lyricMarkers.js', () => ({}));-vi.mock('../services/musicVideo/treatmentService.js', () => ({}));+vi.mock('../services/musicVideo/treatmentService.js', () => ({+ compileTreatment: vi.fn(async () => ({ treatment: {} })),+}));vi.mock('../services/musicVideo/devArtifactService.js', () => ({}));vi.mock('../services/musicVideo/devArtifacts.js', () => ({}));-vi.mock('../services/musicVideo/castAndSetsService.js', () => ({}));+vi.mock('../services/musicVideo/castAndSetsService.js', () => ({+ startCastAndSets: vi.fn(async () => ({ status: 'running' })),+ regenerateCastAndSets: vi.fn(async () => ({ status: 'running' })),+ editCastAndSetsDirection: vi.fn(async () => ({ status: 'running' })),+ resumeCastAndSets: vi.fn(async () => ({ status: 'running' })),+}));import { authGate, hostControlRouteGate } from '../services/authGate.js';import codeAnimationRoutes from './codeAnimation.js';@@ -109,6 +145,17 @@ import { drawWaveSketch, drawWaveSketchForTrack } from '../services/musicWaveforimport { writeMusicCode } from '../services/musicCode.js';import { startAutonomousVideo, resumeAutonomousVideo } from '../services/musicVideo/autonomousService.js';+import { createSession, revokeSessionById } from '../services/auth.js';+import { planProject } from '../services/musicVideo/planner.js';+import { compileTreatment } from '../services/musicVideo/treatmentService.js';+import { draftPublishKitCopy } from '../services/musicVideo/publishKit.js';+import { generateMusicVideoCode, regenerateMusicVideoCodeSection } from '../services/musicVideo/codeGeneration.js';+import { generateMixedMediaDocument, reviseMixedMediaEvents, regenerateMixedMediaSection } from '../services/musicVideo/documentGeneration.js';+import { startAutoReview, resumeAutoReview } from '../services/musicVideo/autoReviewService.js';+import { startProduction, resumeProduction } from '../services/musicVideo/productionService.js';+import { startCastAndSets, regenerateCastAndSets, editCastAndSetsDirection, resumeCastAndSets } from '../services/musicVideo/castAndSetsService.js';++let ownerSession;const id = '00000000-0000-4000-8000-000000000001';const picker = { providerId: 'example-provider', model: 'example-model' };const authoring = [@@ -123,6 +170,25 @@ const authoring = [['/api/music-video/autonomous', { prompt: 'Example video', ...picker }, startAutonomousVideo, 202],[`/api/music-video/${id}/autonomous/resume`, {}, resumeAutonomousVideo, 200],];+const musicVideoAuthoring = [+ [`/api/music-video/${id}/plan`, { ...picker }, planProject, 200, 'post'],+ [`/api/music-video/${id}/treatment/compile`, { baseRevision: 0, ...picker }, compileTreatment, 200, 'post'],+ [`/api/music-video/${id}/publish-kit/copy`, { notes: 'Example copy', ...picker }, draftPublishKitCopy, 200, 'post'],+ [`/api/music-video/${id}/cast-and-sets`, { ...picker }, startCastAndSets, 202, 'post'],+ [`/api/music-video/${id}/cast-and-sets/regenerate`, { ...picker }, regenerateCastAndSets, 202, 'post'],+ [`/api/music-video/${id}/cast-and-sets/direction`, { protagonist: { movement: 'Example movement' } }, editCastAndSetsDirection, 202, 'patch'],+ [`/api/music-video/${id}/cast-and-sets/resume`, { ...picker }, resumeCastAndSets, 202, 'post'],+ [`/api/music-video/${id}/code/generate`, { ...picker }, generateMusicVideoCode, 200, 'post'],+ [`/api/music-video/${id}/code/sections/example-section/regenerate`, { ...picker }, regenerateMusicVideoCodeSection, 200, 'post'],+ [`/api/music-video/${id}/composition/document/generate`, { ...picker }, generateMixedMediaDocument, 201, 'post'],+ [`/api/music-video/${id}/composition/document/events/revise`, { expectedDraft: 'music-video/example/composition/example-draft', ...picker }, reviseMixedMediaEvents, 201, 'post'],+ [`/api/music-video/${id}/composition/document/sections/example-section/regenerate`, { expectedDraft: 'music-video/example/composition/example-draft', ...picker }, regenerateMixedMediaSection, 201, 'post'],+ [`/api/music-video/${id}/production-runs`, { pool: [], limits: { maxGenerations: 2, maxReviewAttempts: 1 }, ...picker }, startProduction, 201, 'post'],+ [`/api/music-video/${id}/production-runs/example-run/resume`, {}, resumeProduction, 200, 'post'],+ [`/api/music-video/${id}/auto-reviews`, { startSec: 0, endSec: 2, limits: { maxAttempts: 1, maxGenerations: 2 }, ...picker }, startAutoReview, 201, 'post'],+ [`/api/music-video/${id}/auto-reviews/example-run/resume`, {}, resumeAutoReview, 200, 'post'],+];+const allAuthoring = [...authoring, ...musicVideoAuthoring];const appFor = (address = '192.0.2.10') => {const app = express();app.use((req, _res, next) => {@@ -137,23 +203,24 @@ const appFor = (address = '192.0.2.10') => {app.use(errorMiddleware);return app;};-const call = (app, [path, body], headers = {}) => {- const pending = request(app).post(path);+const call = (app, [path, body, , , method = 'post'], headers = {}) => {+ const pending = request(app)[method](path);for (const [name, value] of Object.entries(headers)) pending.set(name, value);return pending.send(body);};const expectNoAuthoring = () => {- for (const [, , service] of authoring) expect(service).not.toHaveBeenCalled();+ for (const [, , service] of allAuthoring) expect(service).not.toHaveBeenCalled();};-beforeEach(() => {+beforeEach(async () => {vi.clearAllMocks();auth.enabled = false;+ ownerSession = await createSession();});-describe('media authoring operator authority (#9512)', () => {+describe('media authoring operator authority (#9512, #9869)', () => {it('refuses every direct and deferred authoring entry point before any service effects', async () => {- for (const operation of authoring) {+ for (const operation of allAuthoring) {for (const [address, headers] of [['192.0.2.10', {}],['127.0.0.1', { [DEV_PROXY_CLIENT_ADDRESS_HEADER]: '192.0.2.10' }],@@ -168,7 +235,7 @@ describe('media authoring operator authority (#9512)', () => {it('requires authentication with a password and denies legacy Basic authoring', async () => {auth.enabled = true;- for (const operation of authoring) {+ for (const operation of allAuthoring) {for (const [headers, status, code] of [[{}, 401, 'AUTH_REQUIRED'],[{ Authorization: 'Basic ' + Buffer.from(':example-password').toString('base64') }, 403, 'HOST_CONTROL_FORBIDDEN'],@@ -181,20 +248,98 @@ describe('media authoring operator authority (#9512)', () => {});it('retains local and operator authoring without changing provider/model selections', async () => {+ const agentSession = await createSession({ label: 'agent' });for (const [enabled, address, headers] of [[false, '127.0.0.1', {}],[false, '127.0.0.1', { [DEV_PROXY_CLIENT_ADDRESS_HEADER]: '::ffff:127.0.0.1' }],- [true, '192.0.2.10', { Authorization: 'Bearer example-session' }],+ [true, '192.0.2.10', { Authorization: `Bearer ${ownerSession.token}` }],+ [true, '192.0.2.10', { Cookie: `portos_auth=${ownerSession.token}` }],+ [true, '192.0.2.10', { Authorization: `Bearer ${agentSession.token}` }],+ [true, '192.0.2.10', { Cookie: `portos_auth=${agentSession.token}` }],]) {auth.enabled = enabled;- for (const operation of authoring) {+ for (const operation of allAuthoring) {const response = await call(appFor(address), operation, headers);expect(response.status, `${operation[0]}: ${JSON.stringify(response.body)}`).toBe(operation[3]);}}- for (const [, body, service] of authoring) {- expect(service).toHaveBeenCalledTimes(3);- if (body.providerId) expect(service.mock.lastCall[0]).toEqual(expect.objectContaining(picker));+ for (const [, body, service] of allAuthoring) {+ expect(service).toHaveBeenCalledTimes(6);+ if (body.providerId) {+ const args = service.mock.lastCall;+ const options = args.find(value => value && typeof value === 'object' && (value.providerId || value.reviewer));+ expect(options.reviewer || options).toEqual(expect.objectContaining(picker));+ }+ }+ });++ it('preserves pinned and default authoring/reviewer choices for delegated agents', async () => {+ auth.enabled = true;+ const session = await createSession({ label: 'agent' });+ const headers = { Authorization: `Bearer ${session.token}` };+ for (const provider of [{ providerId: 'example-tui', model: 'example-model' }, {}]) {+ for (const operation of musicVideoAuthoring.filter(([, body]) => body.providerId)) {+ const selected = [...operation];+ const { providerId: _provider, model: _model, ...body } = selected[1];+ selected[1] = { ...body, ...provider };+ expect((await call(appFor(), selected, headers)).status, selected[0]).toBe(selected[3]);+ const options = selected[2].mock.lastCall.find(value => value && typeof value === 'object');+ const choice = options.reviewer || options;+ expect(choice.providerId).toBe(provider.providerId);+ expect(choice.model).toBe(provider.model);+ }+ }+ });++ it('fails closed for invalid, expired, revoked and scoped-peer credentials', async () => {+ auth.enabled = true;+ const expired = await createSession({ label: 'agent' });+ const revoked = await createSession({ label: 'agent' });+ await revokeSessionById(revoked.id);+ // Advance the verifier's clock without timers or production sleeps.+ const clock = vi.spyOn(Date, 'now').mockReturnValue(Date.now() + SESSION_TTL_MS + 1);+ for (const operation of musicVideoAuthoring) {+ for (const [headers, status, code] of [+ [{ Authorization: 'Bearer invalid-session' }, 401, 'AUTH_REQUIRED'],+ [{ Authorization: `Bearer ${expired.token}` }, 401, 'AUTH_REQUIRED'],+ [{ Cookie: `portos_auth=${revoked.token}` }, 401, 'AUTH_REQUIRED'],+ [{+ [PEER_AUTH_HEADER]: derivePeerAuthToken('a'.repeat(32), 'example-instance'),+ [PEER_INSTANCE_HEADER]: 'example-instance',+ }, 403, 'PEER_SCOPE_FORBIDDEN'],+ ]) {+ const response = await call(appFor(), operation, headers);+ expect([response.status, response.body.code], operation[0]).toEqual([status, code]);+ }+ }+ clock.mockRestore();+ expectNoAuthoring();+ });++ it('matches case and trailing-slash variants before any service effects', async () => {+ for (const operation of musicVideoAuthoring) {+ const variant = [...operation];+ variant[0] = operation[0].toUpperCase() + '/';+ const response = await call(appFor(), variant);+ expect([response.status, response.body.code], operation[0]).toEqual([403, 'HOST_CONTROL_FORBIDDEN']);+ }+ expectNoAuthoring();+ });++ it('keeps deterministic plan/compile under the same operation authority', async () => {+ const operations = [+ [`/api/music-video/${id}/plan`, { seedPrompts: false }, planProject, 200],+ [`/api/music-video/${id}/treatment/compile`, { baseRevision: 0, useAi: false }, compileTreatment, 200],+ ];+ for (const operation of operations) {+ expect((await call(appFor(), operation)).status).toBe(403);+ expect(operation[2]).not.toHaveBeenCalled();+ expect((await call(appFor('127.0.0.1'), operation)).status).toBe(200);+ auth.enabled = true;+ const session = await createSession({ label: 'agent' });+ expect((await call(appFor(), operation, { Authorization: `Bearer ${session.token}` })).status).toBe(200);+ expect(operation[2].mock.lastCall).toEqual([id, expect.objectContaining(operation[1])]);+ auth.enabled = false;}});