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: restore test temp-root ownership after a watch-mode config restart (#10755)
[._.]/ Adam Eivy7093c6b8b46b
diff --git a/client/src/test/vitestTempRoot.test.js b/client/src/test/vitestTempRoot.test.jsindex c07b70655c..d38445c61d 100644--- a/client/src/test/vitestTempRoot.test.js+++ b/client/src/test/vitestTempRoot.test.js@@ -1,10 +1,11 @@// @vitest-environment nodeimport { describe, expect, it } from 'vitest';import { spawnSync } from 'node:child_process';-import { mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs';+import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, utimesSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';-import { createVitestTempFixture } from '../../../scripts/lib/vitestTempRootFixture.js';+import { createVitestTempFixture, runWatchConfigRestart } from '../../../scripts/lib/vitestTempRootFixture.js';+import { STALE_ROOT_AGE_MS, sweepStaleRunRoots } from '../../../scripts/lib/vitestStaleRunRoots.js';describe('real client runner temp lifecycle', () => {const workspace = 'client';@@ -50,5 +51,23 @@ describe('real client runner temp lifecycle', () => {rmSync(host, { recursive: true, force: true });}}, 25000);-+ it('client watch-config restart re-stamps the live owner so a later sweep keeps the root', async () => {+ const host = mkdtempSync(join(tmpdir(), 'vrt-'));+ try {+ await runWatchConfigRestart(host, 'client', ({ first, restarted, pid }) => {+ // The restart reused the pathname and the main process re-stamped it.+ expect(restarted.root).toBe(first.root);+ expect(restarted.owner.split(' ')[0]).toBe(String(pid));+ expect(existsSync(join(first.root, '.owner.pid'))).toBe(true);+ // Six idle hours later, the next launch's sweep must not reclaim a live watcher.+ const aged = new Date(Date.now() - STALE_ROOT_AGE_MS - 60000);+ utimesSync(first.root, aged, aged);+ sweepStaleRunRoots(host);+ expect(existsSync(first.root)).toBe(true);+ rmSync(mkdtempSync(join(first.root, 'after-sweep-')), { recursive: true });+ });+ } finally {+ rmSync(host, { recursive: true, force: true });+ }+ }, 90000);});diff --git a/scripts/lib/vitestTempRoot.js b/scripts/lib/vitestTempRoot.jsindex 829ff70ff8..1aa4eb96b6 100644--- a/scripts/lib/vitestTempRoot.js+++ b/scripts/lib/vitestTempRoot.js@@ -1,7 +1,26 @@-import { mkdtempSync } from 'node:fs';+import { existsSync, mkdirSync, mkdtempSync } from 'node:fs';import { tmpdir } from 'node:os';-import { join } from 'node:path';-import { sweepStaleRunRoots, writeOwnerFile } from './vitestStaleRunRoots.js';+import { basename, join } from 'node:path';+import { OWNER_FILE, sweepStaleRunRoots, writeOwnerFile } from './vitestStaleRunRoots.js';++// Vitest sets these in every pool worker; the enclosing main process owns the root.+const inVitestWorker = () => Boolean(process.env.VITEST_POOL_ID || process.env.VITEST_WORKER_ID);++/**+ * A watch-mode config restart runs global teardown (which removes the root)+ * and then reloads the config in the SAME main process, with the inherited+ * env still naming the removed pathname. Re-materialize it and re-stamp this+ * process as owner before the framework recreates scratch under it; otherwise+ * the next launch's sweep reads an unknown owner and reclaims a live session+ * after the age threshold (#10755). An existing stamp belongs to the+ * enclosing owner (CI launcher, earlier config load) and is never replaced,+ * and a pool worker re-importing the config never claims the root.+ */+function restoreInheritedRootOwnership(root) {+ if (inVitestWorker() || !basename(root).startsWith('pvt-')) return;+ mkdirSync(root, { recursive: true });+ if (!existsSync(join(root, OWNER_FILE))) writeOwnerFile(root);+}/** Establish containment at config-load time, before Vitest creates its cache or workers. */export function bootstrapVitestTempRoot() {@@ -13,6 +32,8 @@ export function bootstrapVitestTempRoot() {root = mkdtempSync(join(hostRoot, 'pvt-'));writeOwnerFile(root);process.env.PORTOS_TEST_TEMP_ROOT = root;+ } else {+ restoreInheritedRootOwnership(root);}process.env.TMPDIR = root;process.env.TMP = root;diff --git a/scripts/lib/vitestTempRootFixture.js b/scripts/lib/vitestTempRootFixture.jsindex e8689136e2..9424ddeeb1 100644--- a/scripts/lib/vitestTempRootFixture.js+++ b/scripts/lib/vitestTempRootFixture.js@@ -1,5 +1,6 @@// Dependency-free subprocess fixtures: each workspace runs its own lifecycle cases.-import { existsSync, mkdirSync, symlinkSync, watch, writeFileSync } from 'node:fs';+import { spawn, spawnSync } from 'node:child_process';+import { appendFileSync, existsSync, mkdirSync, readFileSync, realpathSync, symlinkSync, watch, writeFileSync } from 'node:fs';import { dirname, join } from 'node:path';import { fileURLToPath } from 'node:url';@@ -34,6 +35,9 @@ export function createVitestTempFixture(host, workspace, body) {const env = { ...process.env, TMPDIR: host, TMP: host, TEMP: host, NODE_DISABLE_COMPILE_CACHE: '1' };delete env.PORTOS_TEST_TEMP_ROOT;delete env.VITEST_FAST;+ // The fixture's main process is not a pool worker of the suite spawning it.+ delete env.VITEST_POOL_ID;+ delete env.VITEST_WORKER_ID;return {args: [join(repo, workspace, 'node_modules/vitest/vitest.mjs'), 'run','--config', join(repo, workspace, 'vitest.config.js'), '--root', root, '--maxWorkers', '1'],@@ -97,3 +101,78 @@ it('controlled sibling failure', async ({ signal }) => {});`;}++const WATCH_READY_MS = 60000;++function killProcessTree(child) {+ if (!child.pid || child.exitCode !== null) return;+ if (process.platform === 'win32') {+ spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' });+ return;+ }+ try {+ process.kill(-child.pid, 'SIGKILL'); // detached: the child leads its own group+ } catch {+ child.kill('SIGKILL');+ }+}++/**+ * Run the real workspace config under `vitest --watch`, edit the config to force+ * a restart ("Restarting due to config changes"), and hand the restarted run's+ * observations to `inspect`. The fixture test records `{ root, owner }` into a+ * numbered marker per run, so readiness is event-driven and bounded by+ * WATCH_READY_MS. The process tree is always killed before returning.+ */+export async function runWatchConfigRestart(host, workspace, inspect) {+ const marks = join(host, 'marks');+ mkdirSync(marks);+ const { args, options } = createVitestTempFixture(host, workspace, `+ const fs = await import('node:fs');+ const marks = process.env.VRT_MARKS;+ const n = fs.readdirSync(marks).filter((name) => name.endsWith('.json')).length + 1;+ const record = JSON.stringify({ root: first, owner: fs.readFileSync(join(first, '.owner.pid'), 'utf8') });+ fs.writeFileSync(join(marks, 'pending'), record);+ fs.renameSync(join(marks, 'pending'), join(marks, 'run-' + n + '.json'));+ `);+ // A wrapper config in the fixture is the file the watcher restarts on; it+ // loads the workspace's actual config, so editing it never touches the repo.+ // Real path: the watcher reports canonical paths (macOS tmpdir is a symlink),+ // and a config file it cannot match is a plain rerun, not a restart.+ const wrapper = join(realpathSync(host), 'fixture', 'vitest.config.mjs');+ writeFileSync(wrapper, `export { default } from ${JSON.stringify(join(repo, workspace, 'vitest.config.js'))};\n`);+ const cfg = args.indexOf('--config') + 1;+ args[cfg] = wrapper;+ args[args.indexOf('--root') + 1] = dirname(wrapper);+ args.splice(args.indexOf('run'), 1, '--watch');+ const { timeout: _bounded, ...spawnOptions } = options; // readiness below bounds the run+ const child = spawn(process.execPath, args, {+ ...spawnOptions,+ env: { ...options.env, VRT_MARKS: marks },+ detached: process.platform !== 'win32',+ stdio: ['ignore', 'pipe', 'pipe'],+ });+ let output = '';+ child.stdout.on('data', (chunk) => { output += chunk; });+ child.stderr.on('data', (chunk) => { output += chunk; });+ const abort = new AbortController();+ const timer = setTimeout(() => abort.abort(new Error('watch fixture readiness timed out')), WATCH_READY_MS);+ child.once('exit', () => abort.abort(new Error('watch fixture exited before readiness')));+ const readMark = async (n) => {+ const file = join(marks, `run-${n}.json`);+ await waitForFixtureReady(file, { signal: abort.signal });+ return JSON.parse(readFileSync(file, 'utf8'));+ };+ try {+ const first = await readMark(1);+ appendFileSync(wrapper, '// force a config restart\n');+ const restarted = await readMark(2);+ return await inspect({ first, restarted, pid: child.pid });+ } catch (error) {+ error.message += `\n${output}`;+ throw error;+ } finally {+ clearTimeout(timer);+ killProcessTree(child);+ }+}diff --git a/scripts/vitestTempRoot.test.js b/scripts/vitestTempRoot.test.jsindex 2fc2fba6e8..8f19805477 100644--- a/scripts/vitestTempRoot.test.js+++ b/scripts/vitestTempRoot.test.js@@ -1,10 +1,10 @@import { describe, expect, it } from 'vitest';import { spawn, spawnSync } from 'node:child_process';-import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs';+import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, utimesSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';-import { createVitestTempFixture } from './lib/vitestTempRootFixture.js';-import { sweepStaleRunRoots, writeOwnerFile } from './lib/vitestStaleRunRoots.js';+import { createVitestTempFixture, runWatchConfigRestart } from './lib/vitestTempRootFixture.js';+import { STALE_ROOT_AGE_MS, sweepStaleRunRoots, writeOwnerFile } from './lib/vitestStaleRunRoots.js';describe('real workspace runner temp lifecycle', () => {const workspace = 'server';@@ -74,4 +74,23 @@ describe('real workspace runner temp lifecycle', () => {rmSync(host, { recursive: true, force: true });}}, 20000);+ it('server watch-config restart re-stamps the live owner so a later sweep keeps the root', async () => {+ const host = mkdtempSync(join(tmpdir(), 'vrt-'));+ try {+ await runWatchConfigRestart(host, 'server', ({ first, restarted, pid }) => {+ // The restart reused the pathname and the main process re-stamped it.+ expect(restarted.root).toBe(first.root);+ expect(restarted.owner.split(' ')[0]).toBe(String(pid));+ expect(existsSync(join(first.root, '.owner.pid'))).toBe(true);+ // Six idle hours later, the next launch's sweep must not reclaim a live watcher.+ const aged = new Date(Date.now() - STALE_ROOT_AGE_MS - 60000);+ utimesSync(first.root, aged, aged);+ sweepStaleRunRoots(host);+ expect(existsSync(first.root)).toBe(true);+ rmSync(mkdtempSync(join(first.root, 'after-sweep-')), { recursive: true });+ });+ } finally {+ rmSync(host, { recursive: true, force: true });+ }+ }, 90000);});