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([issue-11695]): drain fixture-owned Chrome groups (#11700)

[._.]/15a80e91bd87
diff --git a/scripts/vitestTempRootSetup.js b/scripts/vitestTempRootSetup.js
index 21229f534a..52f28af9e6 100644
--- a/scripts/vitestTempRootSetup.js
+++ b/scripts/vitestTempRootSetup.js
@@ -5,7 +5,7 @@
*/
import { lstatSync, readdirSync, rmSync } from 'node:fs';
import { join } from 'node:path';
-import { describeOwnedBrowserObservation, readOwnedBrowserObservations } from '../server/test/ownedBrowserDiagnostics.js';
+import { describeOwnedBrowserObservation, describeStageFixtureRoot, readOwnedBrowserObservations } from '../server/test/ownedBrowserDiagnostics.js';
import { OWNER_FILE, killTestBrowsersUnder } from './lib/vitestStaleRunRoots.js';
export function setup() {}
@@ -103,6 +103,11 @@ export function teardown() {
console.warn(`⚠️ composition profile ${index + 1}: ${describeOwnedBrowserObservation(observations.get(name), state)}`);
}
if (composition.length > 8) console.warn(`⚠️ composition profile provenance capped: ${composition.length - 8} additional entries`);
+ const stages = entries.filter(name => name.startsWith('portos-code-animation-stages-'));
+ for (const [index, name] of stages.slice(0, 8).entries()) {
+ console.warn(`⚠️ stage fixture ${index + 1}: ${describeStageFixtureRoot(join(root, name), observations.get(name), state)}`);
+ }
+ if (stages.length > 8) console.warn(`⚠️ stage fixture provenance capped: ${stages.length - 8} additional entries`);
if (entries.length) process.exitCode = 1;
try {
diff --git a/scripts/vitestTempRootSetup.test.js b/scripts/vitestTempRootSetup.test.js
index 130f4acd09..7c7743f297 100644
--- a/scripts/vitestTempRootSetup.test.js
+++ b/scripts/vitestTempRootSetup.test.js
@@ -4,6 +4,7 @@ import { tmpdir } from 'node:os';
import { channel } from 'node:diagnostics_channel';
import { EventEmitter } from 'node:events';
import { fileURLToPath } from 'node:url';
+import { cleanupTempDataRoots, lazyTempDataRoot } from '../server/lib/mockPathsDataRoot.js';
import { observeOwnedBrowsers } from '../server/test/ownedBrowserDiagnostics.js';
import { join } from 'node:path';
import { afterEach, describe, expect, it, vi } from 'vitest';
@@ -129,6 +130,51 @@ describe('vitestTempRootSetup teardown', () => {
expect(report).not.toContain('Abc123');
});
+ it('distinguishes a removed stage root recreated by a writer from a later lazy allocation', () => {
+ const leaked = runTeardown(root => {
+ const tempKeys = ['TMPDIR', 'TMP', 'TEMP'];
+ const prior = Object.fromEntries(tempKeys.map(key => [key, process.env[key]]));
+ for (const key of tempKeys) process.env[key] = root;
+ const dispose = observeOwnedBrowsers(() => fileURLToPath(import.meta.url));
+ try {
+ const first = lazyTempDataRoot('portos-code-animation-stages-');
+ const child = new EventEmitter();
+ child.exitCode = null;
+ child.signalCode = null;
+ channel('portos.composition-browser.lifecycle').publish({ event: 'child', profile: first, child });
+ child.exitCode = 0;
+ child.emit('exit'); // Inherited stderr can remain open; do not fabricate close.
+ cleanupTempDataRoots();
+ expect(existsSync(first)).toBe(false);
+ mkdirSync(join(first, 'chrome-test-profile'), { recursive: true });
+ writeFileSync(join(first, 'chrome-test-profile', 'private-profile-sentinel'), 'private content');
+ const second = lazyTempDataRoot('portos-code-animation-stages-');
+ mkdirSync(join(second, 'code-animations'));
+ writeFileSync(join(second, 'code-animations', 'private-project-sentinel'), 'private content');
+ dispose();
+ } finally {
+ dispose();
+ for (const key of tempKeys) {
+ if (prior[key] === undefined) delete process.env[key];
+ else process.env[key] = prior[key];
+ }
+ }
+ });
+ cleanupTempDataRoots();
+ expect(leaked.exitCode).toBe(1);
+ const reports = leaked.warnings.filter(line => line.startsWith('⚠️ stage fixture'));
+ expect(reports).toHaveLength(2);
+ const first = reports.find(line => line.includes('generation=1'));
+ expect(first).toContain('exitObserved=true closeObserved=false exitCode=0');
+ expect(first).toContain('removal=resolved presentAfterRemoval=false presentAtGlobalGuard=true');
+ expect(first).toContain('profileEntries=2 applicationEntries=0');
+ const second = reports.find(line => line.includes('generation=2'));
+ expect(second).toContain('removal=not-started');
+ expect(second).toContain('profileEntries=0 applicationEntries=2');
+ expect(reports.join('\n')).not.toContain('private-');
+ expect(reports.join('\n')).not.toContain('private content');
+ });
+
it('caps records, event history and public projection without copying private child or error values', () => {
const observations = channel('portos.composition-browser.lifecycle');
const leaked = runTeardown(root => {
diff --git a/server/lib/mockPathsDataRoot.js b/server/lib/mockPathsDataRoot.js
index 981be1af70..deaeae224f 100644
--- a/server/lib/mockPathsDataRoot.js
+++ b/server/lib/mockPathsDataRoot.js
@@ -45,6 +45,7 @@
* target the default re-rooting wouldn't produce — it merges last.
*/
+import { channel } from 'node:diagnostics_channel';
import { appendFileSync, mkdtempSync, readdirSync, rmSync, statSync } from 'fs';
import { tmpdir } from 'os';
import { basename, isAbsolute, join, relative, sep } from 'path';
@@ -110,11 +111,13 @@ export function recordTempRootOwner(dir, { owner, observation } = {}) {
* @returns {string} absolute path to this prefix's temp root
*/
const lazyRoots = new Map();
+const rootLifecycle = channel('portos.composition-browser.lifecycle');
export function lazyTempDataRoot(prefix = 'portos-test-') {
let root = lazyRoots.get(prefix);
if (!root) {
root = createTempDataRoot(prefix);
lazyRoots.set(prefix, root);
+ rootLifecycle.publish({ event: 'allocated', profile: root });
}
return root;
}
@@ -123,7 +126,16 @@ export function lazyTempDataRoot(prefix = 'portos-test-') {
export function cleanupTempDataRoots() {
// Retries cover ENOTEMPTY/EBUSY from a just-killed child (a test Chrome's
// helpers) still flushing into the root while it is being removed.
- for (const root of lazyRoots.values()) rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
+ for (const root of lazyRoots.values()) {
+ rootLifecycle.publish({ event: 'remove-start', profile: root });
+ try {
+ rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
+ rootLifecycle.publish({ event: 'remove-resolved', profile: root });
+ } catch (error) {
+ rootLifecycle.publish({ event: 'remove-rejected', profile: root, code: error.code });
+ throw error;
+ }
+ }
lazyRoots.clear();
}
diff --git a/server/routes/musicVideoProductionReview.browser.test.js b/server/routes/musicVideoProductionReview.browser.test.js
index c8a288320e..53198dfcc5 100644
--- a/server/routes/musicVideoProductionReview.browser.test.js
+++ b/server/routes/musicVideoProductionReview.browser.test.js
@@ -7,7 +7,7 @@ import { join, resolve } from 'node:path';
import { execFileSync, spawn } from 'node:child_process';
import { chromium } from 'playwright-core';
import { createRequire } from 'node:module';
-import { makePathsProxy, lazyTempDataRoot, cleanupTempDataRoots, sweepStrayTempRoots } from '../lib/mockPathsDataRoot.js';
+import { makePathsProxy, lazyTempDataRoot, cleanupTempDataRoots } from '../lib/mockPathsDataRoot.js';
import { errorMiddleware } from '../lib/errorHandler.js';
import { browserSuiteCanRun } from '../lib/browserSuiteGate.js';
@@ -18,7 +18,7 @@ vi.mock('../services/auth.js', () => ({ isAuthEnabled: async () => true, verifyP
let endpoint;
vi.mock('../services/browserService.js', () => ({ loadConfig: async () => ({ chromePath: chrome }), cdpRequest: path => fetch(`${endpoint}${path}`) }));
const { findFfmpeg } = await import('../lib/ffmpeg.js');
-const { _testChromeCaptureArgs, _waitForTestChrome, _cleanupTestBrowser, _createTestArtifactsRoot } = await import('../services/htmlComposition/testBrowserCleanup.js');
+const { _testChromeCaptureArgs, _waitForTestChrome, _cleanupTestBrowser, _testBrowserCleanupSettled, _createTestArtifactsRoot } = await import('../services/htmlComposition/testBrowserCleanup.js');
const chrome = [process.env.CHROME_PATH, chromium.executablePath(), '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '/usr/bin/google-chrome'].find(p => p && existsSync(p));
const ffmpeg = await findFfmpeg();
// Server-only CI intentionally does not install the client workspace. This
@@ -37,13 +37,19 @@ try {
const canRun = browserSuiteCanRun('musicVideo browser suite', { Chrome: chrome, ffmpeg, 'client workspace dependencies': clientBundler }, { onUnavailable: cleanupTempDataRoots });
let browser, artifacts, proc, server;
+const detachedChromeProcessGroupId = () => process.platform === 'win32' ? undefined : proc?.pid;
afterAll(async () => {
- await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: () => {} });
- if (server) await new Promise(resolve => server.close(resolve));
- cleanupTempDataRoots();
- // A just-killed Chrome helper can recreate the data root after removal; CI's Linux run left one (#10312).
- await sweepStrayTempRoots('mv-review-browser-');
-});
+ let cleanupError;
+ const recordCleanupError = error => { cleanupError = cleanupError ? new AggregateError([cleanupError, error], 'Music-video review fixture cleanup failed') : error; };
+ try { await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: () => {}, detachedProcessGroupId: detachedChromeProcessGroupId() }); }
+ catch (error) { recordCleanupError(error); }
+ try { if (server) await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())); }
+ catch (error) { recordCleanupError(error); }
+ if (!cleanupError && _testBrowserCleanupSettled(proc)) {
+ try { cleanupTempDataRoots(); } catch (error) { recordCleanupError(error); }
+ }
+ if (cleanupError) throw cleanupError;
+}, 25000);
describe.skipIf(!canRun)('production review in a real browser (Chrome, ffmpeg and client dependencies required)', () => {
it('reviews, requests targeted revisions, renders and watches real animated evidence before approval', async () => {
@@ -93,7 +99,7 @@ describe.skipIf(!canRun)('production review in a real browser (Chrome, ffmpeg an
await build({ configFile: false, root: ui, plugins: [react()], css: { postcss: { plugins: [tailwind({ base: client })] } }, build: { outDir: join(ui, 'dist'), emptyOutDir: true }, logLevel: 'warn' });
const app = express(); app.use(express.json()); app.use('/api/music-video', router); app.use('/data/videos', express.static(PATHS.videos)); app.use('/fonts', express.static(join(client, 'public/fonts'))); app.use(express.static(join(ui, 'dist'))); app.use(errorMiddleware);
server = await new Promise(resolve => { const listener = app.listen(0, '127.0.0.1', () => resolve(listener)); });
- proc = spawn(chrome, _testChromeCaptureArgs(join(PATHS.data, 'test-chrome')), { stdio: ['ignore', 'ignore', 'pipe'] });
+ proc = spawn(chrome, _testChromeCaptureArgs(join(PATHS.data, 'test-chrome')), { stdio: ['ignore', 'ignore', 'pipe'], detached: process.platform !== 'win32' });
endpoint = new URL(await _waitForTestChrome(proc)).origin.replace('ws:', 'http:');
artifacts = _createTestArtifactsRoot();
browser = await chromium.connectOverCDP(endpoint, { artifactsDir: artifacts.path });
diff --git a/server/routes/musicVideoRichAuthoring.browser.test.js b/server/routes/musicVideoRichAuthoring.browser.test.js
index 3a61a43311..c6d19ad714 100644
--- a/server/routes/musicVideoRichAuthoring.browser.test.js
+++ b/server/routes/musicVideoRichAuthoring.browser.test.js
@@ -28,7 +28,7 @@ vi.mock('../services/auth.js', () => ({ isAuthEnabled: async () => true, verifyR
let endpoint;
vi.mock('../services/browserService.js', () => ({ loadConfig: async () => ({ chromePath: chrome }), cdpRequest: path => fetch(`${endpoint}${path}`) }));
const { findFfmpeg } = await import('../lib/ffmpeg.js');
-const { _testChromeCaptureArgs, _waitForTestChrome, _cleanupTestBrowser, _createTestArtifactsRoot } = await import('../services/htmlComposition/testBrowserCleanup.js');
+const { _testChromeCaptureArgs, _waitForTestChrome, _cleanupTestBrowser, _testBrowserCleanupSettled, _createTestArtifactsRoot } = await import('../services/htmlComposition/testBrowserCleanup.js');
const chrome = [process.env.CHROME_PATH, chromium.executablePath(), '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '/usr/bin/google-chrome'].find(p => p && existsSync(p));
const ffmpeg = await findFfmpeg();
// Server-only CI intentionally does not install the client workspace. This
@@ -47,11 +47,19 @@ try {
const canRun = browserSuiteCanRun('musicVideo browser suite', { Chrome: chrome, ffmpeg, 'client workspace dependencies': clientBundler }, { onUnavailable: cleanupTempDataRoots });
let browser, artifacts, proc, server;
+const detachedChromeProcessGroupId = () => process.platform === 'win32' ? undefined : proc?.pid;
afterAll(async () => {
- await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: () => {} });
- if (server) await new Promise(resolve => server.close(resolve));
- cleanupTempDataRoots();
-});
+ let cleanupError;
+ const recordCleanupError = error => { cleanupError = cleanupError ? new AggregateError([cleanupError, error], 'Music-video authoring fixture cleanup failed') : error; };
+ try { await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: () => {}, detachedProcessGroupId: detachedChromeProcessGroupId() }); }
+ catch (error) { recordCleanupError(error); }
+ try { if (server) await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())); }
+ catch (error) { recordCleanupError(error); }
+ if (!cleanupError && _testBrowserCleanupSettled(proc)) {
+ try { cleanupTempDataRoots(); } catch (error) { recordCleanupError(error); }
+ }
+ if (cleanupError) throw cleanupError;
+}, 25000);
describe.skipIf(!canRun)('rich document authoring in a real browser (Chrome, ffmpeg and client dependencies required)', () => {
it('[fidelity] authors and accepts a Three.js world through the real UI and renders its reviewed proof', async () => {
@@ -87,7 +95,7 @@ describe.skipIf(!canRun)('rich document authoring in a real browser (Chrome, ffm
await build({ configFile: false, root: ui, plugins: [react()], css: { postcss: { plugins: [tailwind({ base: client })] } }, build: { outDir: join(ui, 'dist'), emptyOutDir: true }, logLevel: 'warn' });
const app = express(); app.use(express.json()); app.get('/api/providers', (req,res)=>res.json({activeProvider:'stub-provider',providers:[{id:'stub-provider',name:'Synthetic author',type:'api',enabled:true,models:['fixture-model'],defaultModel:'fixture-model',toolFreeOneShot:true}]})); app.use('/api/music-video', router); app.use('/data/videos', express.static(PATHS.videos)); app.use('/fonts', express.static(join(client, 'public/fonts'))); app.use(express.static(join(ui, 'dist'))); app.use(errorMiddleware);
server = await new Promise(resolve => { const listener = app.listen(0, '127.0.0.1', () => resolve(listener)); });
- proc = spawn(chrome, _testChromeCaptureArgs(join(PATHS.data, 'test-chrome')), { stdio: ['ignore', 'ignore', 'pipe'] });
+ proc = spawn(chrome, _testChromeCaptureArgs(join(PATHS.data, 'test-chrome')), { stdio: ['ignore', 'ignore', 'pipe'], detached: process.platform !== 'win32' });
endpoint = new URL(await _waitForTestChrome(proc)).origin.replace('ws:', 'http:');
artifacts = _createTestArtifactsRoot();
browser = await chromium.connectOverCDP(endpoint, { artifactsDir: artifacts.path });
diff --git a/server/routes/musicVideoSongRevision.browser.test.js b/server/routes/musicVideoSongRevision.browser.test.js
index e6cb7dbf01..e9b9d19696 100644
--- a/server/routes/musicVideoSongRevision.browser.test.js
+++ b/server/routes/musicVideoSongRevision.browser.test.js
@@ -8,13 +8,13 @@ import { join, resolve } from 'node:path';
import { spawn } from 'node:child_process';
import { createRequire } from 'node:module';
import { chromium } from 'playwright-core';
-import { makePathsProxy, lazyTempDataRoot, cleanupTempDataRoots, sweepStrayTempRoots } from '../lib/mockPathsDataRoot.js';
+import { makePathsProxy, lazyTempDataRoot, cleanupTempDataRoots } from '../lib/mockPathsDataRoot.js';
import { waitForAbort } from '../lib/testHelper.js';
import { errorMiddleware } from '../lib/errorHandler.js';
import { browserSuiteCanRun } from '../lib/browserSuiteGate.js';
vi.mock('../lib/paths.js', async original => makePathsProxy(await original(), { dataRoot: () => lazyTempDataRoot('mv-song-browser-') }));
vi.mock('../services/settings.js', () => ({ getSettings: async () => ({}) }));
-const { _testChromeCaptureArgs, _waitForTestChrome, _cleanupTestBrowser, _createTestArtifactsRoot } = await import('../services/htmlComposition/testBrowserCleanup.js');
+const { _testChromeCaptureArgs, _waitForTestChrome, _cleanupTestBrowser, _testBrowserCleanupSettled, _createTestArtifactsRoot } = await import('../services/htmlComposition/testBrowserCleanup.js');
const chrome = [process.env.CHROME_PATH, chromium.executablePath(), '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '/usr/bin/google-chrome'].find(p => p && existsSync(p));
const client = resolve(import.meta.dirname, '../../client');
const requireClient = createRequire(join(client, 'package.json'));
@@ -24,27 +24,27 @@ catch (err) { if (err.code !== 'MODULE_NOT_FOUND') throw err; }
const canRun = browserSuiteCanRun('song revision browser suite', { Chrome: chrome, 'client workspace dependencies': bundler }, { onUnavailable: cleanupTempDataRoots });
// Prerequisite discovery may initialize mocked paths; skipped suites have no cleanup hooks.
let browser, artifacts, proc, server, io, songs, musicVideoEvents;
+const detachedChromeProcessGroupId = () => process.platform === 'win32' ? undefined : proc?.pid;
const fixtureCleanup = new AbortController();
const broadcast = event => io.emit('music-video:song-revision', event);
afterAll(async () => {
+ let cleanupError;
+ const recordCleanupError = error => { cleanupError = cleanupError ? new AggregateError([cleanupError, error], 'Song revision fixture cleanup failed') : error; };
musicVideoEvents?.off('song-revision', broadcast);
fixtureCleanup.abort();
- await songs?.__testing.settle(); songs?.__setSongRevisionDepsForTests();
+ try { await songs?.__testing.settle(); } catch (error) { recordCleanupError(error); }
+ try { songs?.__setSongRevisionDepsForTests(); } catch (error) { recordCleanupError(error); }
+ try { await _cleanupTestBrowser({ browser, proc, artifacts, detachedProcessGroupId: detachedChromeProcessGroupId(), cleanup: () => {} }); }
+ catch (error) { recordCleanupError(error); }
try {
- await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: async () => {
- try {
- if (io) await new Promise(resolve => io.close(resolve));
- else if (server) await new Promise(resolve => server.close(resolve));
- } finally {
- cleanupTempDataRoots();
- }
- } });
- } finally {
- // A SIGKILLed Chrome leaves helper processes that can still flush into the
- // profile (<root>/chrome) after the root was removed, recreating it (#10404).
- await sweepStrayTempRoots('mv-song-browser-');
+ if (io) await new Promise((resolve, reject) => io.close(error => error ? reject(error) : resolve()));
+ else if (server) await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve()));
+ } catch (error) { recordCleanupError(error); }
+ if (!cleanupError && _testBrowserCleanupSettled(proc)) {
+ try { cleanupTempDataRoots(); } catch (error) { recordCleanupError(error); }
}
-});
+ if (cleanupError) throw cleanupError;
+}, 30000);
describe.skipIf(!canRun)('song revision in Chrome (client dependencies required)', () => {
it('forks, edits, generates, listens and selects without altering the previous version', async () => {
@@ -92,7 +92,7 @@ describe.skipIf(!canRun)('song revision in Chrome (client dependencies required)
const app = express(); app.use(express.json()); app.use('/api/music-video', router); app.use('/data/music', express.static(PATHS.music)); app.use(express.static(join(ui, 'dist'))); app.use(errorMiddleware);
server = await new Promise(resolve => { const listener = app.listen(0, '127.0.0.1', () => resolve(listener)); });
io = new Server(server); musicVideoEvents.on('song-revision', broadcast);
- proc = spawn(chrome, _testChromeCaptureArgs(join(PATHS.data, 'chrome')), { stdio: ['ignore', 'ignore', 'pipe'] });
+ proc = spawn(chrome, _testChromeCaptureArgs(join(PATHS.data, 'chrome')), { stdio: ['ignore', 'ignore', 'pipe'], detached: process.platform !== 'win32' });
artifacts = _createTestArtifactsRoot();
browser = await chromium.connectOverCDP(await _waitForTestChrome(proc), { artifactsDir: artifacts.path });
const page = await browser.newPage({ viewport: { width: 1000, height: 900 } });
diff --git a/server/services/codeAnimation/export.realBrowser.test.js b/server/services/codeAnimation/export.realBrowser.test.js
index 5a21670921..51e9e478a2 100644
--- a/server/services/codeAnimation/export.realBrowser.test.js
+++ b/server/services/codeAnimation/export.realBrowser.test.js
@@ -105,6 +105,7 @@ window.renderFrame = (t) => {
let proc;
let browser;
let artifacts;
+const detachedChromeProcessGroupId = () => process.platform === 'win32' ? undefined : proc?.pid;
const decodeRaw = (input, extra = []) => execFileSync(ffmpeg, ['-v', 'error', '-i', input, ...extra, '-f', 'rawvideo', '-pix_fmt', 'rgb24', '-'], { maxBuffer: 80 * 1024 * 1024 });
const meanAbsDiff = (a, b) => {
let sum = 0;
@@ -115,7 +116,7 @@ const meanAbsDiff = (a, b) => {
describe.skipIf(!chrome || !ffmpeg || !ffprobe)('Code Animation frame-exact export with real Chrome and ffmpeg', () => {
beforeAll(async () => {
const profile = join(lazyTempDataRoot('portos-code-animation-export-'), 'chrome-test-profile');
- proc = spawn(chrome, ['--headless=new', '--mute-audio', '--no-sandbox', '--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--ignore-gpu-blocklist', '--no-first-run', '--disable-background-networking', '--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank'], { stdio: ['ignore', 'ignore', 'pipe'] });
+ proc = spawn(chrome, ['--headless=new', '--mute-audio', '--no-sandbox', '--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--ignore-gpu-blocklist', '--no-first-run', '--disable-background-networking', '--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank'], { stdio: ['ignore', 'ignore', 'pipe'], detached: process.platform !== 'win32' });
try {
const ws = await _waitForTestChrome(proc);
endpoint = new URL(ws).origin.replace('ws:', 'http:');
@@ -123,13 +124,13 @@ describe.skipIf(!chrome || !ffmpeg || !ffprobe)('Code Animation frame-exact expo
browser = await chromium.connectOverCDP(endpoint, { artifactsDir: artifacts.path });
} catch (error) {
// Retain the owned handle for afterAll if termination itself fails.
- await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: cleanupTempDataRoots, startupError: error });
+ await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: cleanupTempDataRoots, startupError: error, detachedProcessGroupId: detachedChromeProcessGroupId() });
}
// Enclose the existing startup (20s), disconnect (5s) and owned-child
// termination (10s) budgets without cutting off evidence.
- }, 40000);
+ }, 45000);
- afterAll(() => _cleanupTestBrowser({ browser, proc, artifacts, cleanup: cleanupTempDataRoots }), 20000);
+ afterAll(() => _cleanupTestBrowser({ browser, proc, artifacts, cleanup: cleanupTempDataRoots, detachedProcessGroupId: detachedChromeProcessGroupId() }), 25000);
it('exports duration × fps unique frames from a slow renderFrame, matching direct screenshots, as BT.709 H.264 with the controls hidden', async () => {
state.html = FIXTURE;
diff --git a/server/services/codeAnimation/stages.realBrowser.db.test.js b/server/services/codeAnimation/stages.realBrowser.db.test.js
index 832858082b..2c00019375 100644
--- a/server/services/codeAnimation/stages.realBrowser.db.test.js
+++ b/server/services/codeAnimation/stages.realBrowser.db.test.js
@@ -3,6 +3,7 @@
// new revision, and finally encoded by the real renderer and ffmpeg. PostgreSQL
// (portos_test) holds the project and run records; no provider is called.
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
+import { channel } from 'node:diagnostics_channel';
import { createHash } from 'node:crypto';
import { readFile, mkdir, writeFile } from 'node:fs/promises';
import { execFileSync, spawn } from 'node:child_process';
@@ -71,11 +72,14 @@ let proc;
let browser;
let artifacts;
let projectId;
+const detachedChromeProcessGroupId = () => process.platform === 'win32' ? undefined : proc?.pid;
describe.skipIf(!dbReady || !chrome || !ffmpeg || !ffprobe)('Production stages with real Chrome and ffmpeg', () => {
beforeAll(async () => {
- const profile = join(lazyTempDataRoot('portos-code-animation-stages-'), 'chrome-test-profile');
- proc = spawn(chrome, _testChromeCaptureArgs(profile), { stdio: ['ignore', 'ignore', 'pipe'] });
+ const root = lazyTempDataRoot('portos-code-animation-stages-');
+ const profile = join(root, 'chrome-test-profile');
+ proc = spawn(chrome, _testChromeCaptureArgs(profile), { stdio: ['ignore', 'ignore', 'pipe'], detached: process.platform !== 'win32' });
+ channel('portos.composition-browser.lifecycle').publish({ event: 'child', profile: root, child: proc });
try {
const ws = await _waitForTestChrome(proc, 20000, { source: selected.source, executable: chrome, profile });
endpoint = new URL(ws).origin.replace('ws:', 'http:');
@@ -83,17 +87,17 @@ describe.skipIf(!dbReady || !chrome || !ffmpeg || !ffprobe)('Production stages w
browser = await chromium.connectOverCDP(endpoint, { artifactsDir: artifacts.path });
} catch (error) {
// Retain the owned handle for afterAll if termination itself fails.
- await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: cleanupTempDataRoots, startupError: error });
+ await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: cleanupTempDataRoots, startupError: error, detachedProcessGroupId: detachedChromeProcessGroupId() });
}
- // Enclose the existing startup (20s), version probe (up to 3s), disconnect
- // (5s) and owned-child termination (10s) budgets without cutting off evidence.
- }, 40000);
+ // Enclose startup (20s), version probe (up to 3s), disconnect (5s), direct
+ // child termination (10s), and detached-group drain (5s) budgets.
+ }, 50000);
afterAll(async () => {
if (projectId) await query('DELETE FROM code_animation_projects WHERE id = $1', [projectId]);
- await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: cleanupTempDataRoots });
+ await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: cleanupTempDataRoots, detachedProcessGroupId: detachedChromeProcessGroupId() });
await close();
- }, 20000);
+ }, 25000);
it('turns a frozen original into measured findings, a repaired revision and a real MP4, with a real style frame and pilot', async () => {
projectId = (await createProductionProject({ manifest })).id;
diff --git a/server/services/htmlComposition/browserProcessGroup.js b/server/services/htmlComposition/browserProcessGroup.js
index b29fbdd592..124de7ff0c 100644
--- a/server/services/htmlComposition/browserProcessGroup.js
+++ b/server/services/htmlComposition/browserProcessGroup.js
@@ -37,8 +37,10 @@ export function signalBrowserProcessGroup(pgid, signal, state, phase) {
}
}
-// Only the retained, closed child's detached group is eligible. Zombies cannot
-// write; a live leader at the departed PID means reuse and must not be signaled.
+// Only a group tied to the retained direct child's observed exit is eligible.
+// Zombies cannot write; a live leader at the departed PID means reuse and must
+// not be signaled. The caller may still hold an open stdio handle whose `close`
+// event is delayed by descendants, so `exit` is the required reaping boundary.
export async function drainBrowserProcessGroup(pgid, deadline, graceMs, state = { error: 'none' }) {
const failure = message => {
const code = state.error === 'none' ? 'none' : safeCode(state.error);
diff --git a/server/services/htmlComposition/index.test.js b/server/services/htmlComposition/index.test.js
index f6d6d62694..e3b44f72ff 100644
--- a/server/services/htmlComposition/index.test.js
+++ b/server/services/htmlComposition/index.test.js
@@ -38,6 +38,7 @@ let proc;
let browser;
let artifacts;
let browserSession;
+const detachedChromeProcessGroupId = () => process.platform === 'win32' ? undefined : proc?.pid;
// The delayed paint is intentional test input: a screenshot before the seek
// promise settles MUST see the previous position, even on a fast machine.
@@ -58,7 +59,7 @@ async function composition(html = fixture(), directory = `compositions/${randomU
describe.skipIf(!chrome || !ffmpeg)('HTML composition with real Chrome and ffmpeg', () => {
beforeAll(async () => {
const profile = join(PATHS.data, 'chrome-test-profile');
- proc = spawn(chrome, _testChromeCaptureArgs(profile), { stdio: ['ignore', 'ignore', 'pipe'] });
+ proc = spawn(chrome, _testChromeCaptureArgs(profile), { stdio: ['ignore', 'ignore', 'pipe'], detached: process.platform !== 'win32' });
const ws = await _waitForTestChrome(proc);
endpoint = new URL(ws).origin.replace('ws:', 'http:');
artifacts = _createTestArtifactsRoot();
@@ -82,7 +83,7 @@ describe.skipIf(!chrome || !ffmpeg)('HTML composition with real Chrome and ffmpe
}
});
- afterAll(() => _cleanupTestBrowser({ browser, proc, artifacts, cleanup: cleanupTempDataRoots }));
+ afterAll(() => _cleanupTestBrowser({ browser, proc, artifacts, cleanup: cleanupTempDataRoots, detachedProcessGroupId: detachedChromeProcessGroupId() }), 25000);
it('awaits every seek, encodes exactly 12 frames, disposes its private target and registers a thumbnail', async () => {
const input = await composition();
diff --git a/server/services/htmlComposition/testBrowserCleanup.js b/server/services/htmlComposition/testBrowserCleanup.js
index 5bdf00ca11..5eb9f88e38 100644
--- a/server/services/htmlComposition/testBrowserCleanup.js
+++ b/server/services/htmlComposition/testBrowserCleanup.js
@@ -4,6 +4,7 @@ import { availableParallelism, tmpdir } from 'node:os';
import { basename, join } from 'node:path';
import { killWithEscalation } from '../../lib/killWithEscalation.js';
import { recordTempRootOwner } from '../../lib/mockPathsDataRoot.js';
+import { drainBrowserProcessGroup } from './browserProcessGroup.js';
// These owned browsers render on explicit seek(t), not wall/display time.
// Chromium's display scheduler must not pace a song-length offline capture.
@@ -400,6 +401,19 @@ export function _createTestArtifactsRoot({ parent = tmpdir() } = {}) {
return Object.freeze({ path, dev, ino });
}
+// A successful explicit group drain settles this retained handle permanently.
+// Startup failure and afterAll can both enter cleanup; never probe or signal
+// the departed group's numeric identity a second time.
+const settledDetachedGroups = new WeakSet();
+const settledTestChromeProcesses = new WeakSet();
+
+// A fixture with a secondary teardown hook can guard root cleanup after the
+// browser helper throws. No retained process means discovery was skipped and
+// there is no live owned Chrome to block that cleanup.
+export function _testBrowserCleanupSettled(proc) {
+ return !proc || settledTestChromeProcesses.has(proc);
+}
+
// The owned Chrome may still write downloads into the root until it exits.
// Remove only the directory created above, and keep it as evidence when exit
// is unconfirmed or the path no longer names that directory. No path in text.
@@ -416,28 +430,58 @@ function removeTestArtifactsRoot({ path, dev, ino }, childSettled) {
}
// Test-only lifecycle boundary. Disconnect failure must not strand the owned
-// child, and neither failure may prevent removal of temporary test data.
-// A startup failure remains primary even if teardown also fails.
-export async function _cleanupTestBrowser({ browser, proc, artifacts, cleanup, startupError, observeProcess = _testChromeProcessFacts }) {
+// child. An explicitly detached POSIX group must quiesce before its temp roots
+// are removed; an uncertain group keeps those roots as evidence. A startup
+// failure remains primary even if teardown also fails.
+export async function _cleanupTestBrowser({ browser, proc, artifacts, cleanup, startupError, detachedProcessGroupId, observeProcess = _testChromeProcessFacts }) {
const errors = startupError ? [startupError] : [];
const cleanupFailure = (stage, error) => {
const code = /^[A-Z0-9_]{1,32}$/.test(error?.code) ? error.code : 'unknown';
errors.push(new Error(`Test Chrome ${stage} cleanup failed (${code})`));
};
let childSettled = false;
+ let detachedGroupSettled = detachedProcessGroupId === undefined
+ || (proc && settledDetachedGroups.has(proc));
try {
await withinDeadline(() => browser?.close(), 5000, 'browser disconnect',
() => `; process: ${describeProcess(proc, observeProcess)}`).catch(error => errors.push(error));
- await terminateOwnedChrome(proc, observeProcess).then(() => { childSettled = true; }, error => errors.push(error));
+ await terminateOwnedChrome(proc, observeProcess).then(async () => {
+ childSettled = true;
+ if (detachedProcessGroupId === undefined || detachedGroupSettled) return;
+ if (process.platform === 'win32') {
+ errors.push(new Error('Test Chrome detached process groups are unsupported on Windows'));
+ return;
+ }
+ if (!Number.isSafeInteger(detachedProcessGroupId) || detachedProcessGroupId !== proc?.pid) {
+ errors.push(new Error('Test Chrome detached process group ownership unavailable'));
+ return;
+ }
+ try {
+ // terminateOwnedChrome waits for the retained direct child's `exit`
+ // event (Node has reaped it). `close` can wait on descendants that
+ // inherited stderr, so drain the exact detached group next; its live
+ // leader identity check refuses a reused PID before any signal.
+ await drainBrowserProcessGroup(detachedProcessGroupId, performance.now() + 5000, 1000);
+ detachedGroupSettled = true;
+ settledDetachedGroups.add(proc);
+ } catch (error) {
+ errors.push(error);
+ }
+ }, error => errors.push(error));
} finally {
try { proc?.stderr?.destroy(); } catch (error) { cleanupFailure('stderr', error); }
if (artifacts) {
try {
- const failure = removeTestArtifactsRoot(artifacts, childSettled);
+ const failure = removeTestArtifactsRoot(artifacts, childSettled && detachedGroupSettled);
if (failure) errors.push(new Error(`Test Chrome artifacts cleanup ${failure}`));
} catch (error) { cleanupFailure('artifacts', error); }
}
- try { await cleanup(); } catch (error) { cleanupFailure('temporary data', error); }
+ if (detachedProcessGroupId !== undefined && (!childSettled || !detachedGroupSettled)) {
+ errors.push(new Error('Test Chrome temporary data cleanup skipped; detached process group unsettled, root preserved'));
+ } else {
+ try { await cleanup(); } catch (error) { cleanupFailure('temporary data', error); }
+ }
+ if (proc && childSettled && detachedGroupSettled) settledTestChromeProcesses.add(proc);
}
if (startupError && errors.length === 1) throw startupError;
if (errors.length) throw new AggregateError(errors, errors.map(error => error.message).join('; '),
diff --git a/server/services/htmlComposition/testBrowserCleanup.test.js b/server/services/htmlComposition/testBrowserCleanup.test.js
index 739d70e370..49ae012daa 100644
--- a/server/services/htmlComposition/testBrowserCleanup.test.js
+++ b/server/services/htmlComposition/testBrowserCleanup.test.js
@@ -1,11 +1,17 @@
import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
-import { spawnSync } from 'node:child_process';
+import { spawn, spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { EventEmitter } from 'node:events';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import { _cleanupTestBrowser, _createTestArtifactsRoot, _describeTestChromeStartup, _testChromeProcessFacts, _waitForTestChrome, _withTestCaptureDiagnostics } from './testBrowserCleanup.js';
+import { _cleanupTestBrowser, _createTestArtifactsRoot, _describeTestChromeStartup, _testBrowserCleanupSettled, _testChromeProcessFacts, _waitForTestChrome, _withTestCaptureDiagnostics } from './testBrowserCleanup.js';
+
+async function waitForFile(path, timeoutMs = 2000) {
+ const deadline = Date.now() + timeoutMs;
+ while (!existsSync(path) && Date.now() < deadline) await new Promise(resolve => setTimeout(resolve, 10));
+ if (!existsSync(path)) throw new Error('Timed out waiting for synthetic browser descendant marker');
+}
function child() {
const proc = new EventEmitter();
@@ -641,7 +647,9 @@ describe('owned test Chrome cleanup', () => {
describe('fixture-owned CDP artifacts root', () => {
const originalRoot = process.env.PORTOS_TEST_TEMP_ROOT;
let runRoot;
+ let preserveRunRoot = false;
beforeEach(() => {
+ preserveRunRoot = false;
runRoot = mkdtempSync(join(tmpdir(), 'cdp-artifacts-run-'));
process.env.PORTOS_TEST_TEMP_ROOT = runRoot;
vi.spyOn(console, 'log').mockImplementation(() => {});
@@ -652,7 +660,7 @@ describe('fixture-owned CDP artifacts root', () => {
vi.restoreAllMocks();
if (originalRoot === undefined) delete process.env.PORTOS_TEST_TEMP_ROOT;
else process.env.PORTOS_TEST_TEMP_ROOT = originalRoot;
- rmSync(runRoot, { recursive: true, force: true });
+ if (!preserveRunRoot) rmSync(runRoot, { recursive: true, force: true });
});
const exited = () => {
const proc = child();
@@ -702,6 +710,97 @@ describe('fixture-owned CDP artifacts root', () => {
expect(cleanup).toHaveBeenCalledOnce();
});
+ it.skipIf(process.platform === 'win32')('preserves artifacts and data roots when detached-group ownership cannot be verified', async () => {
+ const artifacts = _createTestArtifactsRoot({ parent: runRoot });
+ const dataRoot = mkdtempSync(join(runRoot, 'owned-data-'));
+ const cleanup = vi.fn(() => rmSync(dataRoot, { recursive: true, force: true }));
+ const proc = exited();
+ // This deliberately names our own process group. The helper must fail
+ // closed before signaling it or deleting any root.
+ proc.pid = process.pid;
+
+ const error = await _cleanupTestBrowser({
+ proc, artifacts, cleanup, detachedProcessGroupId: process.pid,
+ }).catch(caught => caught);
+
+ expect(error.message).toContain('Composition browser group identity unavailable');
+ expect(error.message).toContain('Test Chrome temporary data cleanup skipped; detached process group unsettled, root preserved');
+ expect(existsSync(artifacts.path)).toBe(true);
+ expect(existsSync(dataRoot)).toBe(true);
+ expect(cleanup).not.toHaveBeenCalled();
+ expect(_testBrowserCleanupSettled(proc)).toBe(false);
+ expect(_testBrowserCleanupSettled(undefined)).toBe(true);
+ });
+
+ it.skipIf(process.platform === 'win32')('waits for an owned detached descendant before removing roots', async () => {
+ const artifacts = _createTestArtifactsRoot({ parent: runRoot });
+ const dataRoot = mkdtempSync(join(runRoot, 'owned-data-'));
+ const ready = join(runRoot, 'descendant-ready');
+ const release = join(runRoot, 'descendant-release');
+ const wrote = join(runRoot, 'descendant-wrote');
+ const descendant = `
+ const fs = require('node:fs');
+ const path = require('node:path');
+ const [artifactsPath, dataPath, releasePath, wrotePath] = process.argv.slice(1);
+ const poll = setInterval(() => {
+ if (!fs.existsSync(releasePath)) return;
+ clearInterval(poll);
+ fs.mkdirSync(artifactsPath, { recursive: true });
+ fs.mkdirSync(dataPath, { recursive: true });
+ fs.writeFileSync(path.join(artifactsPath, 'late-write'), 'artifact');
+ fs.writeFileSync(path.join(dataPath, 'late-write'), 'data');
+ fs.writeFileSync(wrotePath, 'done');
+ }, 5);
+ `;
+ const parent = `
+ const fs = require('node:fs');
+ const { spawn } = require('node:child_process');
+ const descendant = spawn(process.execPath, ['-e', ${JSON.stringify(descendant)}, ${JSON.stringify(artifacts.path)}, ${JSON.stringify(dataRoot)}, ${JSON.stringify(release)}, ${JSON.stringify(wrote)}], { stdio: ['ignore', 'ignore', 'inherit'] });
+ descendant.unref();
+ fs.writeFileSync(${JSON.stringify(ready)}, 'ready');
+ `;
+ const proc = spawn(process.execPath, ['-e', parent], { detached: true, stdio: ['ignore', 'ignore', 'pipe'] });
+ let exited = false;
+ let closed = false;
+ const directExit = new Promise(resolve => proc.once('exit', () => { exited = true; resolve(); }));
+ const directClose = new Promise(resolve => proc.once('close', () => { closed = true; resolve(); }));
+ let groupSettled = false;
+
+ try {
+ await waitForFile(ready);
+ await directExit;
+ expect(exited).toBe(true);
+ expect(closed).toBe(false);
+
+ await _cleanupTestBrowser({
+ proc, artifacts,
+ cleanup: () => rmSync(dataRoot, { recursive: true, force: true }),
+ detachedProcessGroupId: proc.pid,
+ });
+ groupSettled = true;
+ expect(_testBrowserCleanupSettled(proc)).toBe(true);
+ await directClose;
+ expect(closed).toBe(true);
+
+ // The writer is blocked on this marker. A direct-child-only cleanup
+ // would return, then this release would recreate both removed roots.
+ writeFileSync(release, 'release');
+ await waitForFile(wrote, 1000).then(() => true, () => false);
+ expect(existsSync(wrote)).toBe(false);
+ expect(existsSync(artifacts.path)).toBe(false);
+ expect(existsSync(dataRoot)).toBe(false);
+ } finally {
+ if (!groupSettled) {
+ const finalCleanup = await _cleanupTestBrowser({
+ proc, artifacts,
+ cleanup: () => rmSync(dataRoot, { recursive: true, force: true }),
+ detachedProcessGroupId: proc.pid,
+ }).then(() => true, () => false);
+ preserveRunRoot = !finalCleanup;
+ }
+ }
+ });
+
it('refuses to remove a directory that replaced the created root', async () => {
const artifacts = _createTestArtifactsRoot({ parent: runRoot });
renameSync(artifacts.path, `${artifacts.path}-moved`);
diff --git a/server/services/localLlm.test.js b/server/services/localLlm.test.js
index f265b15374..b3008ca375 100644
--- a/server/services/localLlm.test.js
+++ b/server/services/localLlm.test.js
@@ -3,8 +3,18 @@ import os from 'os';
import path from 'path';
import fs from 'fs';
import { EventEmitter } from 'events';
+import { Writable } from 'node:stream';
import { pinPlatform } from '../lib/testHelper.js';
+// Only the stall-guard cases opt in: fake network time must not race a real
+// filesystem sink. The macOS swap workflow below keeps real download writes.
+const downloadSink = vi.hoisted(() => ({ create: null }));
+vi.mock('fs', async original => {
+ const actual = await original();
+ return { ...actual, createWriteStream: (...args) => downloadSink.create
+ ? downloadSink.create(...args) : actual.createWriteStream(...args) };
+});
+
// vi.mock factories are hoisted above the module body, so the mutable holder
// and the mock objects must come from vi.hoisted (which runs first). The
// service captures ENV_PATH = join(PATHS.root, '.env') at import time, so each
@@ -860,29 +870,47 @@ describe('localLlm', () => {
for (const n of tmpDirs()) fs.rmSync(path.join(os.tmpdir(), n), { recursive: true, force: true });
});
- const run = async (downloadFetch, advanceMs) => {
- // setImmediate stays real: the loop below yields to real file I/O with it.
+ const run = async (downloadFetch, drive) => {
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
const restorePlatform = pinPlatform('darwin');
+ const started = Promise.withResolvers();
+ const sinkReady = Promise.withResolvers();
+ const bytes = [];
+ const progress = [];
+ const waiting = [];
+ downloadSink.create = () => {
+ const sink = new Writable({ write(chunk, _encoding, callback) {
+ bytes.push(...chunk);
+ for (const waiter of waiting) if (bytes.length >= waiter.count) waiter.resolve();
+ callback();
+ } });
+ sinkReady.resolve();
+ return sink;
+ };
pathExistsImpl.fn = async (p) => p === '/Applications/Ollama.app';
vi.stubGlobal('fetch', vi.fn(async (url, init) => {
if (String(url).includes('api.github.com')) {
return { ok: true, json: async () => ({ tag_name: 'v0.34.0', assets: [{ name: 'Ollama-darwin.zip', size: 1024, browser_download_url: 'https://example.com/Ollama-darwin.zip' }] }) };
}
+ started.resolve();
return downloadFetch(init);
}));
mocks.ollama.getStatus.mockResolvedValueOnce({ version: '0.33.3' });
try {
- let settled = false;
- const p = svc.upgradeBackend('ollama').finally(() => { settled = true; });
- // Step in small increments so each chunk's idle-timer reset lands before the clock
- // moves on; keep stepping past the budget so post-download sleeps also fire.
- for (let elapsed = 0; !settled && elapsed < advanceMs * 4; elapsed += 5_000) {
- await vi.advanceTimersByTimeAsync(5_000);
- for (let y = 0; y < 5; y++) await new Promise((r) => setImmediate(r));
- }
- return await p;
+ const p = svc.upgradeBackend('ollama', event => progress.push(event.message));
+ await started.promise;
+ await drive({ sinkReady: sinkReady.promise, waitForBytes: count => {
+ if (bytes.length >= count) return Promise.resolve();
+ const receipt = Promise.withResolvers();
+ waiting.push({ count, resolve: receipt.resolve });
+ return Promise.race([receipt.promise, p.then(result => {
+ throw new Error(`Upgrade ended before receiving ${count} bytes: ${result.error || 'completed'}`);
+ })]);
+ } });
+ return { result: await p, bytes, progress };
} finally {
+ downloadSink.create = null;
+ vi.useRealTimers();
mocks.ollama.getStatus.mockReset();
vi.unstubAllGlobals();
restorePlatform();
@@ -890,23 +918,27 @@ describe('localLlm', () => {
};
it('aborts a body that stops emitting, removes the temp dir and returns the stall error', async () => {
- const r = await run(({ signal }) => ({
+ const { result: r } = await run(({ signal }) => ({
ok: true,
body: new ReadableStream({ start(c) { c.enqueue(new Uint8Array([1])); signal.addEventListener('abort', () => c.error(new Error('aborted'))); } })
- }), 60_001);
+ }), async ({ waitForBytes }) => {
+ await waitForBytes(1);
+ await vi.advanceTimersByTimeAsync(60_001);
+ });
expect(r).toEqual({ success: false, error: expect.stringMatching(/no data for 60 s/) });
expect(tmpDirs()).toEqual([]);
});
it('aborts when no response headers arrive in time', async () => {
- const r = await run(({ signal }) => new Promise((_res, rej) => signal.addEventListener('abort', () => rej(new Error('aborted')))), 30_001);
+ const { result: r } = await run(({ signal }) => new Promise((_res, rej) => signal.addEventListener('abort', () => rej(new Error('aborted')))),
+ () => vi.advanceTimersByTimeAsync(30_001));
expect(r).toEqual({ success: false, error: expect.stringMatching(/no response from the server within 30 s/) });
expect(tmpDirs()).toEqual([]);
});
it('does not abort a slow stream that keeps making progress', async () => {
let n = 0;
- const r = await run(() => ({
+ const { result, bytes, progress } = await run(() => ({
ok: true,
body: new ReadableStream({
async pull(c) {
@@ -915,9 +947,18 @@ describe('localLlm', () => {
c.enqueue(new Uint8Array([n]));
}
})
- }), 200_000);
- // Past the download the flow proceeds to the swap; whatever it fails on, it is not a stall.
- expect(r.error || '').not.toMatch(/stalled/);
+ }), async ({ sinkReady, waitForBytes }) => {
+ await sinkReady;
+ for (let count = 1; count <= 30; count++) {
+ await vi.advanceTimersByTimeAsync(5_000);
+ await waitForBytes(count);
+ }
+ // Stop advancing virtual time at EOF; pipeline completion is owned by
+ // the sink, not an arbitrary number of real event-loop turns.
+ });
+ expect(bytes).toEqual(Array.from({ length: 30 }, (_, i) => i + 1));
+ expect(progress).toContain('Stopping running Ollama…');
+ expect(result).toEqual({ success: false, error: 'Extracted archive did not contain Ollama.app — release layout may have changed.' });
});
});
diff --git a/server/services/musicVideo/backupAdmission.test.js b/server/services/musicVideo/backupAdmission.test.js
index 7f26b1368f..945137af97 100644
--- a/server/services/musicVideo/backupAdmission.test.js
+++ b/server/services/musicVideo/backupAdmission.test.js
@@ -16,14 +16,16 @@ vi.mock('../../lib/paths.js', async (importOriginal) => makePathsProxy(await imp
}));
vi.mock('../../lib/databaseMaintenanceJournal.js', () => ({ assertDatabaseAdmission: () => {} }));
vi.mock('./productionReview.js', async (load) => ({ ...await load(), assertProductionApproval: vi.fn() }));
-// The publishing kit's encodes and thumbnails are written as placeholders; the
-// final render's poster is a fixed name.
+// Video encodes stay placeholders; thumbnails reach the REAL image scorer, so
+// their bytes are a valid 2x2 RGB PNG. The final render's poster is a fixed name.
+const thumbnailPng = vi.hoisted(() => Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAAEklEQVQImWPQqDihUXGCAUIBACTuBaFpkxOkAAAAAElFTkSuQmCC', 'base64'));
vi.mock('../../lib/ffmpeg.js', async (importOriginal) => ({
...(await importOriginal()),
generateThumbnail: vi.fn(async () => 'thumb.jpg'),
findFfmpeg: vi.fn(async () => 'ffmpeg'),
runFfmpegProcess: vi.fn(async ({ args }) => {
- await writeFile(args.at(-1), 'synthetic encode');
+ const output = args.at(-1);
+ await writeFile(output, output.endsWith('.jpg') ? thumbnailPng : 'synthetic encode');
return { ok: true };
}),
}));
@@ -179,11 +181,19 @@ describe('publishing kit build', () => {
const encoded = runFfmpegProcess.mock.calls.length;
expect(encoded).toBeGreaterThan(frames);
expect((await projects.getProject(id)).publishKit?.exports).toBeUndefined();
+ expect((await projects.getProject(id)).publishKit?.thumbnails).toBeUndefined();
release();
await waitFor(async () => expect((await projects.getProject(id)).publishKit?.exports?.length).toBe(encoded - frames));
- for (const { filename } of (await projects.getProject(id)).publishKit.exports) {
+ const { publishKit } = await projects.getProject(id);
+ for (const { filename } of publishKit.exports) {
expect(existsSync(join(PATHS.videos, filename))).toBe(true);
}
+ // The real scorer drops undecodable frames: a nonempty published list
+ // proves these synthetic bytes passed its decoder instead of becoming NaN.
+ expect(publishKit.thumbnails.length).toBeGreaterThan(0);
+ for (const filename of publishKit.thumbnails) {
+ expect(existsSync(join(PATHS.videoThumbnails, filename))).toBe(true);
+ }
} finally {
release();
}
diff --git a/server/services/musicVideo/compositionRender.test.js b/server/services/musicVideo/compositionRender.test.js
index 3674999981..e3bf821c5d 100644
--- a/server/services/musicVideo/compositionRender.test.js
+++ b/server/services/musicVideo/compositionRender.test.js
@@ -4,7 +4,7 @@ import { mkdir, readdir, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { execFileSync, spawn } from 'node:child_process';
import { chromium } from 'playwright-core';
-import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy, sweepStrayTempRoots } from '../../lib/mockPathsDataRoot.js';
+import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../../lib/mockPathsDataRoot.js';
import { findFfmpeg, probeVideoDuration } from '../../lib/ffmpeg.js';
import { PATHS } from '../../lib/fileUtils.js';
import { _cleanupTestBrowser, _createTestArtifactsRoot } from '../htmlComposition/testBrowserCleanup.js';
@@ -30,6 +30,7 @@ if (!chrome || !ffmpeg) cleanupTempDataRoots();
let proc;
let browser;
let artifacts;
+const detachedChromeProcessGroupId = () => process.platform === 'win32' ? undefined : proc?.pid;
const FPS = 24;
const run = (args) => execFileSync(ffmpeg, ['-v', 'error', ...args], { maxBuffer: 256 * 1024 * 1024 });
@@ -57,7 +58,7 @@ function assertInsideSafeArea(path, width, height) {
describe.skipIf(!chrome || !ffmpeg)('music-video typography overlay with real Chrome and ffmpeg (#8984)', () => {
beforeAll(async () => {
const profile = join(PATHS.data, 'chrome-test-profile');
- proc = spawn(chrome, ['--headless=new', '--mute-audio', '--no-sandbox', '--no-first-run', '--disable-background-networking', '--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank'], { stdio: ['ignore', 'ignore', 'pipe'] });
+ proc = spawn(chrome, ['--headless=new', '--mute-audio', '--no-sandbox', '--no-first-run', '--disable-background-networking', '--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank'], { stdio: ['ignore', 'ignore', 'pipe'], detached: process.platform !== 'win32' });
const ws = await new Promise((resolve, reject) => {
const timer = setTimeout(() => reject(new Error('Test Chrome did not start')), 20000);
proc.once('error', reject);
@@ -72,10 +73,8 @@ describe.skipIf(!chrome || !ffmpeg)('music-video typography overlay with real Ch
}, 30000);
afterAll(async () => {
- await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: cleanupTempDataRoots });
- // Real ffprobe/ffmpeg subprocess work (#9032) — see sweepStrayTempRoots's doc.
- await sweepStrayTempRoots('portos-mv-composition-');
- });
+ await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: cleanupTempDataRoots, detachedProcessGroupId: detachedChromeProcessGroupId() });
+ }, 25000);
it('lays timed text over the cut on one continuous audio master, covering the edit within a frame', async () => {
const dir = join(PATHS.data, 'fixture');
diff --git a/server/services/musicVideo/documentRender.browser.test.js b/server/services/musicVideo/documentRender.browser.test.js
index cf10dd5a54..9d5831dbcc 100644
--- a/server/services/musicVideo/documentRender.browser.test.js
+++ b/server/services/musicVideo/documentRender.browser.test.js
@@ -19,7 +19,7 @@ import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy, ownTestBodies }
import { browserSuiteCanRun, chromeWithH264 } from '../../lib/browserSuiteGate.js';
const { it, drain } = ownTestBodies(vitestIt);
-const lifecycle = vi.hoisted(() => ({ diagnostics: null }));
+const lifecycle = vi.hoisted(() => ({ diagnostics: null, chromeProcess: null, fixtureWritersSettled: false }));
vi.mock('node:fs/promises', async original => {
const actual = await original();
return { ...actual, rm: (...args) => lifecycle.diagnostics
@@ -68,9 +68,12 @@ vi.mock('../../lib/paths.js', async (importOriginal) => makePathsProxy(await imp
}));
const { findFfmpeg } = await import('../../lib/ffmpeg.js');
-const { _cleanupTestBrowser, _createTestArtifactsRoot, _waitForTestChrome, _testChromeCaptureArgs } = await import('../htmlComposition/testBrowserCleanup.js');
+const { _cleanupTestBrowser, _createTestArtifactsRoot, _waitForTestChrome, _testBrowserCleanupSettled, _testChromeCaptureArgs } = await import('../htmlComposition/testBrowserCleanup.js');
-afterAll(() => { lifecycle.diagnostics.dispose(); cleanupTempDataRoots(); });
+afterAll(() => {
+ lifecycle.diagnostics.dispose();
+ if (!lifecycle.chromeProcess || (lifecycle.fixtureWritersSettled && _testBrowserCleanupSettled(lifecycle.chromeProcess))) cleanupTempDataRoots();
+});
const chrome = [process.env.CHROME_PATH, chromium.executablePath(),
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
@@ -84,6 +87,7 @@ describe.skipIf(!canRun)('layered template with real Chrome and ffmpeg', () => {
let proc;
let browser;
let artifacts;
+ const detachedChromeProcessGroupId = () => process.platform === 'win32' ? undefined : proc?.pid;
let PATHS, encodeDocumentComposition, prepareDocumentRender, documentRenderClock, importDocumentTemplate;
let generateMixedMediaDocument, regenerateMixedMediaSection, acceptMixedMediaDocument, buildDocumentPreview, PREVIEW_MEDIA_BUDGET_BYTES, projects;
beforeAll(async () => {
@@ -101,7 +105,9 @@ describe.skipIf(!canRun)('layered template with real Chrome and ffmpeg', () => {
({ buildDocumentPreview, PREVIEW_MEDIA_BUDGET_BYTES } = await import('./documentPreview.js'));
projects = await import('./projects.js');
const profile = join(PATHS.data, 'chrome-test-profile');
- proc = spawn(chrome, _testChromeCaptureArgs(profile), { stdio: ['ignore', 'ignore', 'pipe'] });
+ proc = spawn(chrome, _testChromeCaptureArgs(profile), { stdio: ['ignore', 'ignore', 'pipe'], detached: process.platform !== 'win32' });
+ lifecycle.chromeProcess = proc;
+ lifecycle.fixtureWritersSettled = false;
const ws = await _waitForTestChrome(proc);
endpoint = new URL(ws).origin.replace('ws:', 'http:');
artifacts = _createTestArtifactsRoot();
@@ -114,8 +120,9 @@ describe.skipIf(!canRun)('layered template with real Chrome and ffmpeg', () => {
expect(owned.closed, `owned capture child closed before fixture teardown; ${lifecycle.diagnostics.describe(owned)}`).toBe(true);
expect(existsSync(owned.profile), `owned capture profile removed before fixture teardown; ${lifecycle.diagnostics.describe(owned)}`).toBe(false);
}
- } finally { await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: () => {} }); }
- });
+ lifecycle.fixtureWritersSettled = true;
+ } finally { await _cleanupTestBrowser({ browser, proc, artifacts, cleanup: () => {}, detachedProcessGroupId: detachedChromeProcessGroupId() }); }
+ }, 35000);
// Uniquely catches dropped effect data, missing staged modules and preview/export curve drift.
it('develops a still from white in the encoded document render and live preview', async () => {
diff --git a/server/services/musicVideo/render.document.test.js b/server/services/musicVideo/render.document.test.js
index 3e0dd00219..3355e89fd2 100644
--- a/server/services/musicVideo/render.document.test.js
+++ b/server/services/musicVideo/render.document.test.js
@@ -35,6 +35,7 @@ const { startExcerptRender } = await import('./excerptRender.js');
const { importDocumentTemplate } = await import('./compositionDocument.js');
const { encodeDocumentComposition } = await import('./documentRender.js');
const { mutateVideoHistory } = await import('../videoGen/local.js');
+const { musicVideoEvents } = await import('./events.js');
afterAll(() => cleanupTempDataRoots());
@@ -160,14 +161,32 @@ it('rejects a changed film between preparing its reviewed plan and starting the
it('persists a document failure for returning operators and clears it after a successful retry', async () => {
const id = await documentProject();
await importDocumentTemplate(id);
+ const renderAndWait = async () => {
+ let jobId;
+ const early = new Map();
+ const terminal = Promise.withResolvers();
+ const onRender = event => {
+ if (event.projectId !== id) return;
+ // A fast encoder can settle before dispatch returns its job ID.
+ if (!jobId) early.set(event.jobId, event);
+ else if (event.jobId === jobId) terminal.resolve(event);
+ };
+ musicVideoEvents.on('render', onRender);
+ try {
+ ({ jobId } = await renderMusicVideo(id));
+ return early.get(jobId) || await terminal.promise;
+ } finally {
+ musicVideoEvents.off('render', onRender);
+ }
+ };
const message = 'Edit portosComposition.durationSec to a whole-frame duration and re-import the document.';
encodeDocumentComposition.mockRejectedValueOnce(new Error(message));
- await renderMusicVideo(id);
- await vi.waitFor(async () => expect(await projects.getProject(id)).toMatchObject({
+ expect(await renderAndWait()).toMatchObject({ projectId: id, status: 'failed', error: `Composition document render failed: ${message}` });
+ expect(await projects.getProject(id)).toMatchObject({
status: 'failed', renderError: `Composition document render failed: ${message}`,
- }));
- await renderMusicVideo(id);
- await vi.waitFor(async () => expect(await projects.getProject(id)).toMatchObject({ status: 'complete', renderError: null }));
+ });
+ expect(await renderAndWait()).toMatchObject({ projectId: id, status: 'completed', error: null });
+ expect(await projects.getProject(id)).toMatchObject({ status: 'complete', renderError: null });
});
it.each(['error', 'canceled'])('persists document excerpt %s before notifying subscribers to refresh', async (status) => {
diff --git a/server/services/musicVideo/songRevision.js b/server/services/musicVideo/songRevision.js
index 0c74f2b92f..da93be2e70 100644
--- a/server/services/musicVideo/songRevision.js
+++ b/server/services/musicVideo/songRevision.js
@@ -316,4 +316,9 @@ export async function resolveSongRevisionScenes(projectId, { revisionId, action,
});
}
-export const __testing = { settle: async () => { await Promise.all([...inflight.values()].map((entry) => entry.promise)); } };
+export const __testing = { settle: async () => {
+ const results = await Promise.allSettled([...inflight.values()].map((entry) => entry.promise));
+ const failures = results.filter(result => result.status === 'rejected').map(result => result.reason);
+ if (failures.length === 1) throw failures[0];
+ if (failures.length > 1) throw new AggregateError(failures, 'Song revision jobs failed during fixture settlement');
+} };
diff --git a/server/test/ownedBrowserDiagnostics.js b/server/test/ownedBrowserDiagnostics.js
index e3598323d0..e8115dcfe2 100644
--- a/server/test/ownedBrowserDiagnostics.js
+++ b/server/test/ownedBrowserDiagnostics.js
@@ -1,5 +1,5 @@
import { channel } from 'node:diagnostics_channel';
-import { closeSync, existsSync, openSync, readSync } from 'node:fs';
+import { closeSync, existsSync, lstatSync, openSync, opendirSync, readSync } from 'node:fs';
import { basename, dirname, relative, resolve, sep } from 'node:path';
import { constants as osConstants } from 'node:os';
import { fileURLToPath } from 'node:url';
@@ -139,8 +139,9 @@ export function observeOwnedBrowsers(getOwner) {
let ended = false;
let capped = false;
const owners = new Map();
+ const roots = new Map();
const diagnostics = createOwnedBrowserDiagnostics({ changed(owned) {
- const observation = JSON.stringify({ v: 1, exitCode: owned.exitCode, signalCode: owned.signalCode, exit: owned.exited, close: owned.closed,
+ const observation = JSON.stringify({ v: 1, ...roots.get(owned.profile), exitCode: owned.exitCode, signalCode: owned.signalCode, exit: owned.exited, close: owned.closed,
removal: owned.removal, presentAfterRemoval: owned.presentAfterRemoval,
observer: ended ? 'ended' : 'active', events: owned.events, truncated: owned.truncated });
recordTempRootOwner(owned.profile, { owner: owners.get(owned.profile) || 'unavailable', observation });
@@ -150,7 +151,7 @@ export function observeOwnedBrowsers(getOwner) {
try {
if (ended || !runRoot || !message || typeof message.profile !== 'string'
|| dirname(resolve(message.profile)) !== resolve(runRoot)
- || !/^portos-composition-browser-[A-Za-z0-9]+$/.test(basename(message.profile))) return;
+ || !/^portos-(?:composition-browser|code-animation-stages)-[A-Za-z0-9]+$/.test(basename(message.profile))) return;
let owned = diagnostics.children.find(item => item.profile === message.profile);
if (message.event === 'allocated' && !owned) {
if (diagnostics.children.length >= 64) {
@@ -159,6 +160,10 @@ export function observeOwnedBrowsers(getOwner) {
return;
}
owners.set(message.profile, ownerFor(getOwner()));
+ if (basename(message.profile).startsWith('portos-code-animation-stages-')) {
+ const { dev, ino } = lstatSync(message.profile);
+ roots.set(message.profile, { v: 2, generation: roots.size + 1, dev, ino });
+ }
owned = diagnostics.allocate(message.profile);
}
if (!owned) return;
@@ -212,7 +217,10 @@ export function readOwnedBrowserObservations(root) {
let value;
try { value = JSON.parse(encoded); } catch { observations.delete(dir); continue; }
if (dir === '.composition-observer' && value?.v === 1 && value.state === 'capped') { state = 'capped'; continue; }
- const valid = value?.v === 1
+ const stageRoot = value?.v === 2 && /^portos-code-animation-stages-[A-Za-z0-9]+$/.test(dir)
+ && ['generation', 'dev', 'ino'].every(key => Number.isSafeInteger(value[key]) && value[key] >= 0)
+ && value.generation >= 1 && value.generation <= 64;
+ const valid = (value?.v === 1 || stageRoot)
&& safeExit(value.exitCode === 'none' ? null : value.exitCode) === value.exitCode
&& safeSignal(value.signalCode === 'none' ? null : value.signalCode) === value.signalCode
&& typeof value.exit === 'boolean' && typeof value.close === 'boolean'
@@ -221,7 +229,9 @@ export function readOwnedBrowserObservations(root) {
&& Array.isArray(value.events) && value.events.length <= 16
&& value.events.every(event => typeof event === 'string' && /^(.*)@(\d{1,8})ms$/.test(event)
&& events.has(event.split('@')[0]) && Number(event.match(/@(\d+)ms$/)[1]) <= 86400000)
- && Object.keys(value).sort().join(',') === 'close,events,exit,exitCode,observer,presentAfterRemoval,removal,signalCode,truncated,v';
+ && Object.keys(value).sort().join(',') === (stageRoot
+ ? 'close,dev,events,exit,exitCode,generation,ino,observer,presentAfterRemoval,removal,signalCode,truncated,v'
+ : 'close,events,exit,exitCode,observer,presentAfterRemoval,removal,signalCode,truncated,v');
if (!valid) { observations.delete(dir); continue; }
observations.set(dir, { ...value, owner: safeOwner(owner) ? owner : 'unavailable' });
}
@@ -237,3 +247,34 @@ export function describeOwnedBrowserObservation(value, state) {
+ ` removal=${value.removal} presentAfterRemoval=${value.presentAfterRemoval} presentAtGlobalGuard=true`
+ ` observer=${value.observer} events=${value.events.join(',')} truncated=${value.truncated}`;
}
+
+// Inspect only the retained root, without following symlinks or printing names.
+// Bound both traversal and output; 'other' includes unknown application entries.
+export function describeStageFixtureRoot(path, value, state) {
+ let identity = 'unavailable';
+ const counts = { profile: 0, application: 0, other: 0 };
+ let visited = 0;
+ let capped = false;
+ let unreadable = false;
+ try {
+ const current = lstatSync(path);
+ identity = value?.v === 2 ? (current.dev === value.dev && current.ino === value.ino ? 'matches' : 'changed') : 'unavailable';
+ const walk = (directory, category, depth) => {
+ if (depth > 12 || visited >= 256) { capped = true; return; }
+ const dir = opendirSync(directory);
+ try {
+ let entry;
+ while ((entry = dir.readSync())) {
+ if (visited++ >= 256) { capped = true; break; }
+ const group = category || (entry.name === 'chrome-test-profile' ? 'profile'
+ : ['code-animations', 'videos', 'video-thumbnails', 'video-history.json', 'uploads', 'music'].includes(entry.name) ? 'application' : 'other');
+ counts[group]++;
+ if (entry.isDirectory() && !entry.isSymbolicLink()) walk(resolve(directory, entry.name), group, depth + 1);
+ }
+ } finally { dir.closeSync(); }
+ };
+ if (current.isDirectory() && !current.isSymbolicLink()) walk(path, null, 0);
+ } catch { unreadable = true; }
+ return `${describeOwnedBrowserObservation(value, state)} generation=${value?.v === 2 ? value.generation : 'unavailable'}`
+ + ` rootIdentity=${identity} profileEntries=${counts.profile} applicationEntries=${counts.application} otherEntries=${counts.other} contentsCapped=${capped} contentsUnreadable=${unreadable}`;
+}