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

[issue-9513] Require operator authority for media runtime installers (#9528)

[._.]/e007ccc99cec
diff --git a/scripts/vitestCiPool.js b/scripts/vitestCiPool.js
index 0e6ce02774..fea2b53610 100644
--- a/scripts/vitestCiPool.js
+++ b/scripts/vitestCiPool.js
@@ -22,6 +22,15 @@ export const EXCLUSIVE_CAPTURE_TESTS = [
'services/musicVideo/documentRender.browser.test.js',
];
+// Cutover's real subprocess proof has a four-second test budget. A retained
+// concurrent-run failure spent 3.746 seconds on startup/module import alone
+// (#9368). Isolate that resource-sensitive contract without changing its
+// deadline, process-incarnation checks, or fail-closed negative controls.
+const EXCLUSIVE_TESTS = [
+ ...EXCLUSIVE_CAPTURE_TESTS,
+ 'services/databaseMaintenanceCutover.test.js',
+];
+
export function vitestCaptureProjects(test) {
const { include, exclude, ...shared } = test;
// Vite concatenates inherited include arrays: extending the broad root
@@ -36,7 +45,7 @@ export function vitestCaptureProjects(test) {
...shared,
name: 'parallel',
include,
- exclude: [...exclude, ...EXCLUSIVE_CAPTURE_TESTS],
+ exclude: [...exclude, ...EXCLUSIVE_TESTS],
sequence: { groupOrder: 0 },
},
},
@@ -45,7 +54,7 @@ export function vitestCaptureProjects(test) {
test: {
...shared,
name: 'capture',
- include: EXCLUSIVE_CAPTURE_TESTS,
+ include: EXCLUSIVE_TESTS,
exclude,
fileParallelism: false,
sequence: { groupOrder: 1 },
diff --git a/scripts/vitestCiPool.test.js b/scripts/vitestCiPool.test.js
index 7b6d00ad33..bc44ffc060 100644
--- a/scripts/vitestCiPool.test.js
+++ b/scripts/vitestCiPool.test.js
@@ -38,7 +38,7 @@ describe('vitestCiPool', () => {
// Real workers catch inheritance/array-merging mistakes that can duplicate or
// drop suites, or defeat exclusion, despite correct-looking config objects.
-it('runs the full server config with parallel units and exclusive, nonduplicated captures', () => {
+it('runs parallel units before exclusive, nonduplicated captures and cutover proofs', () => {
const scratch = mkdtempSync(join(tmpdir(), 'portos-capture-schedule-'));
const fixture = join(scratch, 'fixture');
const server = fileURLToPath(new URL('../server/', import.meta.url));
@@ -79,6 +79,7 @@ it('runs the full server config with parallel units and exclusive, nonduplicated
for (const [name, path] of [
['html', 'services/htmlComposition/index.test.js'],
['music', 'services/musicVideo/documentRender.browser.test.js'],
+ ['cutover', 'services/databaseMaintenanceCutover.test.js'],
]) {
put(path, common + `test('${name}', async () => {
expect(existsSync(marker('unit-a-done')) && existsSync(marker('unit-b-done'))).toBe(true);
@@ -100,7 +101,7 @@ it('runs the full server config with parallel units and exclusive, nonduplicated
expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0);
const events = readFileSync(join(fixture, 'events'), 'utf8').trim().split('\n');
expect(events.slice(0, 2).sort()).toEqual(['unit-a', 'unit-b']);
- expect(events.slice(2).sort()).toEqual(['html', 'music']);
+ expect(events.slice(2).sort()).toEqual(['cutover', 'html', 'music']);
} finally {
rmSync(scratch, { recursive: true, force: true });
}
diff --git a/server/lib/hostControlRoutes.js b/server/lib/hostControlRoutes.js
index 314963e0b2..ea9c0f823c 100644
--- a/server/lib/hostControlRoutes.js
+++ b/server/lib/hostControlRoutes.js
@@ -280,6 +280,12 @@ export const HOST_CONTROL_ROUTES = Object.freeze([
// Motion toolkit: executes the downloaded installer and writes agent skills user-wide.
'POST /api/html-composition/toolkit/skills/install',
+ // Media runtime setup installs executable dependencies or builds a Docker
+ // image; authorize before opening SSE or probing runtime readiness.
+ 'POST /api/music/setup/runtime-install',
+ 'POST /api/video-gen/setup/runtime-install',
+ 'POST /api/music/supercollider/setup',
+
// Media authoring can launch a coding CLI/TUI agent, including via provider
// fallback. Gate the whole operation even when the initial provider is API;
// runtime-data cwd and later render containment do not sandbox authoring.
diff --git a/server/routes/hostControlMediaInstallers.test.js b/server/routes/hostControlMediaInstallers.test.js
new file mode 100644
index 0000000000..1d92319622
--- /dev/null
+++ b/server/routes/hostControlMediaInstallers.test.js
@@ -0,0 +1,242 @@
+import { 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';
+
+// Real mounted routers, auth policy, SSE and Music/Video installer state
+// machines. Only runtime probes, processes, Docker and store effects are doubled.
+const state = vi.hoisted(() => ({ enabled: false, installed: new Set(), onSpawn: null }));
+vi.mock('../services/auth.js', () => ({
+ isAuthEnabled: vi.fn(async () => state.enabled),
+ verifyRequestSession: vi.fn(async req => req.headers.authorization === 'Bearer example-session'),
+ verifyPassword: vi.fn(async () => true),
+}));
+vi.mock('../services/settings.js', () => ({
+ settingsEvents: new EventEmitter(),
+ getSettings: vi.fn(async () => ({})),
+}));
+vi.mock('../lib/setupScriptRunner.js', async (importOriginal) => ({
+ ...await importOriginal(),
+ stopSetupScript: vi.fn(),
+ spawnSetupScript: vi.fn(env => {
+ const runtime = env.INSTALL_EXAMPLE_MUSIC ? 'music' : 'video';
+ const child = new EventEmitter();
+ child.stdout = new EventEmitter();
+ child.stderr = new EventEmitter();
+ const complete = () => {
+ state.installed.add(runtime);
+ child.emit('close', 0);
+ };
+ // Complete after the real installer has registered its process callbacks,
+ // or let the concurrency test hold the process while a second request arrives.
+ if (state.onSpawn) state.onSpawn(complete);
+ else Promise.resolve().then(complete);
+ return child;
+ }),
+}));
+vi.mock('../services/pipeline/musicGen.js', () => ({
+ ENGINES: { example: {
+ id: 'example', name: 'Example Music', installEnv: 'INSTALL_EXAMPLE_MUSIC',
+ venvDefault: '/example/music', resolvePython: () => '/example/music/bin/python',
+ } },
+ isEnginePlatformSupported: vi.fn(() => true),
+ isEngineHealthy: vi.fn(async () => state.installed.has('music')),
+}));
+vi.mock('../services/videoGen/runtimes.js', () => ({
+ BYOV_RUNTIME_INFO: { example: {
+ id: 'example', label: 'Example Video', installEnvVar: 'INSTALL_EXAMPLE_VIDEO',
+ venvPython: '/example/video/bin/python', repoDir: '/example/video',
+ } },
+ isByovRuntimeInstalled: vi.fn(() => state.installed.has('video')),
+ isByovRuntimeCurrent: vi.fn(async () => true),
+ isByovRuntimeReady: vi.fn(async () => true),
+ invalidateByovReadyCache: vi.fn(),
+ invalidateByovLoraCapabilityCache: vi.fn(),
+ invalidateRuntimeFingerprintCache: vi.fn(),
+}));
+vi.mock('../services/superColliderRuntime.js', () => ({
+ getSuperColliderStatus: vi.fn(async () => ({ ready: true, state: 'ready' })),
+ setupSuperColliderRuntime: vi.fn(async ({ onLine }) => {
+ onLine('Example build completed');
+ return { outcome: 'ready', built: true, probed: true, status: { ready: true } };
+ }),
+}));
+vi.mock('../services/superColliderRender.js', () => ({
+ superColliderSourceHash: () => 'example-source-hash',
+}));
+vi.mock('../services/musicCode.js', () => ({
+ MUSIC_CODE_LANGUAGES: ['strudel', 'tonejs', 'supercollider'], MUSIC_CODE_MAX: 20000,
+}));
+vi.mock('../services/mediaJobQueue/index.js', () => ({
+ enqueueJob: vi.fn(async () => ({ jobId: 'example-render', status: 'queued' })),
+}));
+vi.mock('../services/audioModels.js', () => ({}));
+vi.mock('../services/musicEngineCapabilities.js', () => ({}));
+vi.mock('../services/musicDesigner.js', () => ({}));
+vi.mock('../services/musicWaveform.js', () => ({}));
+vi.mock('../services/hfDownloadStream.js', () => ({}));
+vi.mock('../services/musicGeneration.js', () => ({}));
+vi.mock('../services/videoGen/poster.js', () => ({}));
+vi.mock('../services/userActions.js', () => ({}));
+vi.mock('../services/videoGen/local.js', () => ({ getHistoryItem: vi.fn() }));
+vi.mock('../services/videoGen/upscaleJob.js', () => ({}));
+vi.mock('../services/videoGen/prepareParams.js', () => ({}));
+vi.mock('../services/videoGen/submitJob.js', () => ({}));
+vi.mock('../services/videoGen/reactor.js', () => ({}));
+vi.mock('../services/videoGen/modelCache.js', () => ({}));
+vi.mock('../services/videoUpload.js', () => ({}));
+vi.mock('../services/mediaJobQueue/remoteMediaJob.js', () => ({}));
+vi.mock('../services/videoGen/displayPower.js', () => ({}));
+
+import { authGate, hostControlRouteGate } from '../services/authGate.js';
+import musicRoutes from './music.js';
+import videoRoutes from './videoGen.js';
+import superColliderRoutes from './superCollider.js';
+import { spawnSetupScript } from '../lib/setupScriptRunner.js';
+import { isEngineHealthy, isEnginePlatformSupported } from '../services/pipeline/musicGen.js';
+import { isByovRuntimeInstalled, isByovRuntimeCurrent, isByovRuntimeReady,
+ invalidateByovReadyCache, invalidateByovLoraCapabilityCache, invalidateRuntimeFingerprintCache,
+} from '../services/videoGen/runtimes.js';
+import { getSuperColliderStatus, setupSuperColliderRuntime } from '../services/superColliderRuntime.js';
+import { enqueueJob } from '../services/mediaJobQueue/index.js';
+
+const installers = [
+ ['/api/music/setup/runtime-install?runtime=example', {}],
+ ['/api/video-gen/setup/runtime-install?runtime=example', {}],
+ ['/api/music/supercollider/setup', { rebuild: true }],
+];
+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());
+ app.use('/api/video-gen', videoRoutes);
+ app.use('/api/music/supercollider', superColliderRoutes);
+ app.use('/api/music', musicRoutes);
+ app.use(errorMiddleware);
+ return app;
+};
+const call = (app, [path, body], headers = {}) => {
+ const pending = request(app).post(path);
+ for (const [name, value] of Object.entries(headers)) pending.set(name, value);
+ return pending.send(body);
+};
+const expectNoSetup = () => {
+ for (const effect of [spawnSetupScript, setupSuperColliderRuntime,
+ invalidateByovReadyCache, invalidateByovLoraCapabilityCache, invalidateRuntimeFingerprintCache]) {
+ expect(effect).not.toHaveBeenCalled();
+ }
+};
+const expectNoProbes = () => {
+ for (const probe of [isEngineHealthy, isEnginePlatformSupported,
+ isByovRuntimeInstalled, isByovRuntimeCurrent, isByovRuntimeReady, getSuperColliderStatus]) {
+ expect(probe).not.toHaveBeenCalled();
+ }
+};
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ state.enabled = false;
+ state.installed.clear();
+ state.onSpawn = null;
+});
+
+describe('media installer operator authority (#9513)', () => {
+ it('refuses remote and dev-proxied setup before SSE, readiness probes or installation', async () => {
+ for (const installer of installers) {
+ for (const [address, headers] of [
+ ['192.0.2.10', {}],
+ ['127.0.0.1', { [DEV_PROXY_CLIENT_ADDRESS_HEADER]: '192.0.2.10' }],
+ ['192.0.2.10', { [DEV_PROXY_CLIENT_ADDRESS_HEADER]: '127.0.0.1' }],
+ ]) {
+ const response = await call(appFor(address), installer, headers);
+ expect([response.status, response.body.code], installer[0]).toEqual([403, 'HOST_CONTROL_FORBIDDEN']);
+ expect(response.headers['content-type']).toMatch(/^application\/json/);
+ }
+ }
+ expectNoProbes();
+ expectNoSetup();
+ });
+
+ it('rejects password-enabled anonymous and legacy Basic setup before opening SSE', async () => {
+ state.enabled = true;
+ for (const installer of installers) {
+ for (const [headers, status, code] of [
+ [{}, 401, 'AUTH_REQUIRED'],
+ [{ Authorization: 'Basic ' + Buffer.from(':example-password').toString('base64') }, 403, 'HOST_CONTROL_FORBIDDEN'],
+ ]) {
+ const response = await call(appFor(), installer, headers);
+ expect([response.status, response.body.code], installer[0]).toEqual([status, code]);
+ expect(response.headers['content-type']).toMatch(/^application\/json/);
+ }
+ }
+ expectNoProbes();
+ expectNoSetup();
+ });
+
+ it('preserves local and operator streamed installs, including explicit rebuilds', async () => {
+ 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' }],
+ ]) {
+ state.enabled = enabled;
+ state.installed.clear();
+ for (const installer of installers) {
+ const response = await call(appFor(address), installer, headers);
+ expect(response.status, installer[0]).toBe(200);
+ expect(response.headers['content-type']).toMatch(/^text\/event-stream/);
+ expect(response.text, installer[0]).toContain('"type":"complete"');
+ }
+ }
+ expect(spawnSetupScript).toHaveBeenCalledTimes(6);
+ expect(spawnSetupScript).toHaveBeenCalledWith({ INSTALL_EXAMPLE_MUSIC: '1' });
+ expect(spawnSetupScript).toHaveBeenCalledWith({ INSTALL_EXAMPLE_VIDEO: '1' });
+ expect(setupSuperColliderRuntime).toHaveBeenCalledTimes(3);
+ expect(setupSuperColliderRuntime).toHaveBeenLastCalledWith({ rebuild: true, onLine: expect.any(Function) });
+ });
+
+ it.each(installers.slice(0, 2))('retains single-flight installation for %s', async (path, body) => {
+ const spawned = Promise.withResolvers();
+ state.onSpawn = spawned.resolve;
+ const app = appFor('127.0.0.1');
+ const first = Promise.resolve(call(app, [path, body]));
+ const complete = await spawned.promise;
+ try {
+ const second = await call(app, [path, body]);
+ expect(second.status).toBe(200);
+ expect(second.text).toContain('already running');
+ expect(spawnSetupScript).toHaveBeenCalledTimes(1);
+ } finally {
+ complete();
+ await first;
+ }
+ expect((await first).text).toContain('"type":"complete"');
+ });
+
+ it('keeps status aliases read-only and contained rendering separate from installation', async () => {
+ const app = appFor();
+ for (const path of [
+ '/api/music/setup/runtime-status?runtime=example',
+ '/api/music/setup/runtime-install?runtime=example',
+ '/api/video-gen/setup/runtime-status?runtime=example',
+ '/api/video-gen/setup/runtime-install?runtime=example',
+ '/api/music/supercollider/status',
+ ]) {
+ const response = await request(app).get(path);
+ expect(response.status, path).toBe(200);
+ expect(response.headers['content-type']).toMatch(/^application\/json/);
+ }
+ const render = await request(app).post('/api/music/supercollider/render')
+ .send({ code: 'Pbind()', durationSec: 4, seed: 1 });
+ expect(render.status).toBe(202);
+ expect(enqueueJob).toHaveBeenCalledWith({ kind: 'supercollider', params: {
+ source: 'Pbind()', sourceHash: 'example-source-hash', durationSec: 4, seed: 1,
+ } });
+ expectNoSetup();
+ });
+});