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
Capture bounded owned-browser profile cleanup diagnostics (#11556)
[._.]/bea8a155e7e3
diff --git a/server/services/htmlComposition/ownedBrowser.test.js b/server/services/htmlComposition/ownedBrowser.test.jsindex 21d00bcf7b..09ebd4494b 100644--- a/server/services/htmlComposition/ownedBrowser.test.js+++ b/server/services/htmlComposition/ownedBrowser.test.js@@ -1,7 +1,8 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { EventEmitter } from 'node:events';import { PassThrough } from 'node:stream';-import { access, rm } from 'node:fs/promises';+import { access, mkdir, rm } from 'node:fs/promises';+import { createOwnedBrowserDiagnostics } from '../../test/ownedBrowserDiagnostics.js';const launch = vi.hoisted(() => ({ spawn: vi.fn() }));vi.mock('node:fs/promises', async original => {@@ -15,8 +16,10 @@ const { launchCompositionBrowser } = await import('./ownedBrowser.js');let child;let profile;let startup;+let diagnostics;const endpoint = 'ws://127.0.0.1:12345/devtools/browser/synthetic-browser';beforeEach(() => {+ diagnostics = createOwnedBrowserDiagnostics();child = new EventEmitter();Object.assign(child, { pid: 12345, exitCode: null, signalCode: null, stderr: new PassThrough() });child.kill = vi.fn(signal => {@@ -32,6 +35,7 @@ beforeEach(() => {};launch.spawn.mockReset().mockImplementation((_, args) => {profile = args.find(arg => arg.startsWith('--user-data-dir=')).slice('--user-data-dir='.length);+ diagnostics.track(child, args);queueMicrotask(startup);return child;});@@ -49,6 +53,56 @@ afterEach(() => {});describe('owned composition capture browser lifecycle', () => {+ it('keeps close pending until removal settles and distinguishes later profile recreation in diagnostics', async () => {+ const owner = await launchCompositionBrowser();+ const remove = rm.getMockImplementation();+ let release;+ let started;+ const removing = new Promise(resolve => { started = resolve; });+ const pending = new Promise(resolve => { release = resolve; });+ vi.mocked(rm).mockImplementationOnce((...args) => diagnostics.remove(async (...values) => {+ started();+ await pending;+ return remove(...values);+ }, ...args));+ let settled = false;+ const closing = owner.close().finally(() => { settled = true; });+ try {+ await removing;+ const owned = diagnostics.children[0];+ expect(settled).toBe(false);+ expect(diagnostics.describe(owned)).toContain('exit=true close=true removal=pending presentAfterRemoval=unknown presentNow=true');+ release();+ await closing;+ expect(diagnostics.describe(owned)).toContain('removal=resolved presentAfterRemoval=false presentNow=false');+ // A later writer is a negative control, not evidence about Chrome helpers.+ await mkdir(profile);+ const report = diagnostics.describe(owned);+ expect(report).toContain('removal=resolved presentAfterRemoval=false presentNow=true');+ expect(report).not.toContain(profile);+ } finally {+ release();+ await closing;+ await remove(profile, { recursive: true, force: true });+ }+ });++ it('retains the exact removal rejection while diagnostics distinguish it from recreation', async () => {+ const owner = await launchCompositionBrowser();+ const remove = rm.getMockImplementation();+ const error = Object.assign(new Error('synthetic private profile path'), { code: 'EACCES' });+ vi.mocked(rm).mockImplementationOnce((...args) => diagnostics.remove(() => Promise.reject(error), ...args));+ const log = vi.spyOn(console, 'error').mockImplementation(() => {});+ try {+ await expect(owner.close()).rejects.toBe(error);+ const report = diagnostics.describe(diagnostics.children[0]);+ expect(report).toContain('removal=rejected:EACCES presentAfterRemoval=true presentNow=true');+ expect(report).not.toContain(profile);+ expect(report).not.toContain(error.message);+ expect(log).toHaveBeenCalledWith('❌ Composition browser profile cleanup failed (other)');+ } finally { await remove(profile, { recursive: true, force: true }); }+ });+it('uses the configured executable with a fresh sandboxed profile and disposes only its child', async () => {setUid(1000);const owner = await launchCompositionBrowser();diff --git a/server/services/musicVideo/documentRender.browser.test.js b/server/services/musicVideo/documentRender.browser.test.jsindex a6aa9bd28e..ccd49629b3 100644--- a/server/services/musicVideo/documentRender.browser.test.js+++ b/server/services/musicVideo/documentRender.browser.test.js@@ -19,20 +19,23 @@ import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy, ownTestBodies }import { browserSuiteCanRun } from '../../lib/browserSuiteGate.js';const { it, drain } = ownTestBodies(vitestIt);-const ownedChildren = vi.hoisted(() => []);+const lifecycle = vi.hoisted(() => ({ diagnostics: null }));+vi.mock('node:fs/promises', async original => {+ const actual = await original();+ return { ...actual, rm: (...args) => lifecycle.diagnostics+ ? lifecycle.diagnostics.remove(actual.rm, ...args) : actual.rm(...args) };+});vi.mock('../../lib/childProcess.js', async original => {const actual = await original();return { ...actual, spawn(...args) {const child = actual.spawn(...args);- const profileArg = args[1]?.find(arg => arg.startsWith('--user-data-dir=') && arg.includes('portos-composition-browser-'));- if (profileArg) {- const owned = { profile: profileArg.slice('--user-data-dir='.length), closed: false };- child.once('close', () => { owned.closed = true; });- ownedChildren.push(owned);- }+ lifecycle.diagnostics?.track(child, args[1]);return child;} };});+const { createOwnedBrowserDiagnostics } = await import('../../test/ownedBrowserDiagnostics.js');+lifecycle.diagnostics = createOwnedBrowserDiagnostics();+const ownedChildren = lifecycle.diagnostics.children;// This suite measures encoder pixels; the actual operator boundary is exercised in musicVideoProductionReview.browser.test.js.vi.mock('./productionReview.js', async original => ({ ...await original(), assertProductionApproval: () => {} }));@@ -106,8 +109,8 @@ describe.skipIf(!canRun)('layered template with real Chrome and ffmpeg', () => {try {await drain();for (const owned of ownedChildren) {- expect(owned.closed, 'owned capture child closed before fixture teardown').toBe(true);- expect(existsSync(owned.profile), 'owned capture profile removed before fixture teardown').toBe(false);+ 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, cleanup: () => {} }); }});@@ -794,8 +797,8 @@ describe.skipIf(!canRun)('layered template with real Chrome and ffmpeg', () => {} finally { testSignal = originalSignal; }expect(ownedChildren).toHaveLength(before + 1);const owned = ownedChildren.at(-1);- expect(owned.closed).toBe(true);- expect(existsSync(owned.profile)).toBe(false);+ expect(owned.closed, lifecycle.diagnostics.describe(owned)).toBe(true);+ expect(existsSync(owned.profile), lifecycle.diagnostics.describe(owned)).toBe(false);expect(existsSync(`${outputPath}.silent.mp4`)).toBe(false);expect(browser.isConnected()).toBe(true);}, 30000);diff --git a/server/test/ownedBrowserDiagnostics.js b/server/test/ownedBrowserDiagnostics.jsnew file mode 100644index 0000000000..6eb6661449--- /dev/null+++ b/server/test/ownedBrowserDiagnostics.js@@ -0,0 +1,47 @@+import { existsSync } from 'node:fs';++// Test-owned lifecycle observations only: never retain browser output or print+// the profile path. A close event does not establish that every helper exited.+export function createOwnedBrowserDiagnostics() {+ const children = [];+ const observe = (owned, event) => {+ if (owned.events.length < 16) owned.events.push(`${event}@${Math.round(performance.now() - owned.started)}ms`);+ else owned.truncated = true;+ };+ return {+ children,+ track(child, args) {+ const arg = args?.find(value => value.startsWith('--user-data-dir=') && value.includes('portos-composition-browser-'));+ if (!arg) return;+ const owned = { profile: arg.slice('--user-data-dir='.length), exited: false, closed: false,+ removal: 'not-started', presentAfterRemoval: 'unknown', events: [], started: performance.now(), truncated: false };+ children.push(owned);+ child.once('exit', () => { owned.exited = true; observe(owned, 'exit'); });+ child.once('close', () => { owned.closed = true; observe(owned, 'close'); });+ },+ async remove(remove, path, ...args) {+ const owned = children.find(item => item.profile === path);+ if (!owned) return remove(path, ...args);+ owned.removal = 'pending';+ observe(owned, 'remove-start');+ try {+ const result = await remove(path, ...args);+ owned.removal = 'resolved';+ owned.presentAfterRemoval = existsSync(path);+ observe(owned, 'remove-resolved');+ return result;+ } catch (error) {+ const code = ['ENOTEMPTY', 'EBUSY', 'EPERM', 'EMFILE', 'EACCES', 'ENOENT'].includes(error?.code) ? error.code : 'other';+ owned.removal = `rejected:${code}`;+ owned.presentAfterRemoval = existsSync(path);+ observe(owned, 'remove-rejected');+ throw error;+ }+ },+ describe(owned) {+ return `owned lifecycle: exit=${owned.exited} close=${owned.closed} removal=${owned.removal}`+ + ` presentAfterRemoval=${owned.presentAfterRemoval} presentNow=${existsSync(owned.profile)}`+ + ` events=${owned.events.join(',')} truncated=${owned.truncated}`;+ },+ };+}