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: diagnose nonzero test exits after passing assertions (#11244)
[._.]/ Adam Eivye51c74426a09
diff --git a/scripts/run-ci-tests.js b/scripts/run-ci-tests.jsindex 437f9e0349..dbc4512fde 100644--- a/scripts/run-ci-tests.js+++ b/scripts/run-ci-tests.js@@ -134,12 +134,27 @@ export function recordVitestDuration(scope, label, startedAt) {* Recover diagnostics independently of the terminal reporter. Bound both the* input and rendered output; diagnostic failures never change the exit status.*/-export function structuredFailureDiagnostics(reportPath) {+export function structuredFailureDiagnostics(reportPath, runReportPath) {+ const lines = [];+ if (runReportPath) {+ try {+ if (statSync(runReportPath).size > 100_000) throw new Error('run report exceeds limit');+ const run = JSON.parse(readFileSync(runReportPath, 'utf8'));+ if (!Array.isArray(run.errors) || !run.errors.every(error => typeof error === 'string')) {+ throw new Error('invalid run report schema');+ }+ lines.push(`Vitest run: reason=${run.reason}, exitCode=${run.exitCode}, unhandledErrors=${run.errorCount}`);+ if (run.teardownTimeout) lines.push('Vitest teardown timed out.');+ if (run.errors.length) lines.push(`Vitest unhandled errors (up to 10):\n${run.errors.slice(0, 10).map(error => error.slice(0, 8000)).join('\n')}`);+ } catch {+ lines.push('Vitest run diagnostics unavailable: report missing, malformed, or oversized.');+ }+ }try {if (statSync(reportPath).size > 32 * 1024 * 1024) throw new Error('report exceeds 32 MiB');const report = JSON.parse(readFileSync(reportPath, 'utf8'));if (!Array.isArray(report?.testResults)) throw new Error('invalid report schema');- const lines = [];+ const failures = [];for (const suite of report.testResults) {if (typeof suite?.name !== 'string' || !Array.isArray(suite.assertionResults)) {throw new Error('invalid suite schema');@@ -150,20 +165,21 @@ export function structuredFailureDiagnostics(reportPath) {|| !assertion.failureMessages.every(message => typeof message === 'string')) {throw new Error('invalid assertion schema');}- lines.push(`FAIL ${suite.name} > ${assertion.fullName}\n${assertion.failureMessages.join('\n')}`.slice(0, 8000));- if (lines.length === 10) break;+ failures.push(`FAIL ${suite.name} > ${assertion.fullName}\n${assertion.failureMessages.join('\n')}`.slice(0, 8000));+ if (failures.length === 10) break;}if (suite.status === 'failed' && suite.message) {- lines.push(`FAIL ${suite.name}\n${String(suite.message)}`.slice(0, 8000));+ failures.push(`FAIL ${suite.name}\n${String(suite.message)}`.slice(0, 8000));}- if (lines.length >= 10) break;+ if (failures.length >= 10) break;}- return lines.length- ? `Structured Vitest failure diagnostics (up to 10 failures):\n${lines.slice(0, 10).join('\n')}`- : 'Inconclusive Vitest diagnostics: structured report contains no failed assertion or suite message.';+ lines.push(failures.length+ ? `Structured Vitest failure diagnostics (up to 10 failures):\n${failures.slice(0, 10).join('\n')}`+ : 'Inconclusive Vitest diagnostics: structured report contains no failed assertion or suite message.');} catch {- return 'Inconclusive Vitest diagnostics: structured report is missing, malformed, or exceeds 32 MiB.';+ lines.push('Inconclusive Vitest diagnostics: structured report is missing, malformed, or exceeds 32 MiB.');}+ return lines.join('\n');}/**@@ -187,8 +203,10 @@ export async function runNpm(scope, script, extraArgs) {}function runNpmWithReport(scope, script, extraArgs, reportPath) {+ const runReportPath = join(dirname(reportPath), 'run.json');const args = ['run', script, '--prefix', scope];- args.push('--', ...extraArgs, '--reporter=json', `--outputFile.json=${reportPath}`);+ args.push('--', ...extraArgs, '--reporter=json', `--outputFile.json=${reportPath}`,+ `--reporter=${join(repoRoot, 'scripts', 'vitestRunDiagnostics.js')}`);// Wrapped, not a bare `npm.cmd`: Node refuses to spawn a `.cmd` under// `shell:false` and throws EINVAL, so this script could never run on a// Windows checkout. See server/lib/bufferedSpawn.js.@@ -197,7 +215,9 @@ function runNpmWithReport(scope, script, extraArgs, reportPath) {// npm is a launcher, not the entire test tree. Give this invocation its// own POSIX group; Windows killProcessTree uses taskkill /T instead.const processGroup = process.platform !== 'win32';- const child = spawn(command, spawnArgs, { env: process.env, cwd: repoRoot, detached: processGroup });+ const child = spawn(command, spawnArgs, {+ env: { ...process.env, PORTOS_CI_DIAGNOSTICS_PATH: runReportPath }, cwd: repoRoot, detached: processGroup,+ });let interrupted;let escalation;let spawnError;@@ -222,7 +242,7 @@ function runNpmWithReport(scope, script, extraArgs, reportPath) {child.stderr.on('data', (chunk) => tee(process.stderr, chunk));// A spawn error also emits close. Settle only there, after the pipes close.child.on('error', (error) => { spawnError = error; });- child.on('close', (code) => {+ child.on('close', (code, signal) => {clearTimeout(escalation);if (interrupted) {// npm may close before a descendant that detached its stdio. Finish@@ -232,7 +252,13 @@ function runNpmWithReport(scope, script, extraArgs, reportPath) {process.removeListener('SIGINT', onSigint);process.removeListener('SIGTERM', onSigterm);const status = interrupted ? (interrupted === 'SIGINT' ? 130 : 143) : code ?? 1;- if (status !== 0) console.error(structuredFailureDiagnostics(reportPath));+ if (status !== 0) {+ console.error(`Test launcher exited: code=${code}, signal=${signal ?? 'none'}${interrupted ? `, interrupted=${interrupted}` : ''}`);+ console.error(structuredFailureDiagnostics(reportPath, runReportPath));+ // Also retain teardown/worker output emitted after the reporter's last+ // hook, including exit codes unavailable through the reporter API.+ console.error(`Vitest output tail (last 16000 characters):\n${stripVTControlCharacters(output).slice(-16_000)}`);+ }resolve({ status, output, ...(interrupted ? { interrupted } : {}), ...(spawnError ? { error: spawnError } : {}) });});});diff --git a/scripts/run-ci-tests.test.js b/scripts/run-ci-tests.test.jsindex 8280fd4138..4da1a09af5 100644--- a/scripts/run-ci-tests.test.js+++ b/scripts/run-ci-tests.test.js@@ -280,6 +280,46 @@ describe('relatedInputs', () => {describe('structured failure diagnostics', () => {+ it('reports a run-level rejection and worker cause when every test passed (#11244)', () => {+ const fixture = mkdtempSync(join(tmpdir(), 'portos-diagnostics-'));+ try {+ const host = join(fixture, 'host');+ mkdirSync(host);+ writeFileSync(join(fixture, 'vitest.config.mjs'), FIXTURE_CONFIG);+ writeFileSync(join(fixture, 'fixture.test.js'), `+ test('passing assertion with unhandled rejection', async () => {+ Promise.reject(new Error('synthetic run-level rejection', {+ cause: new Error('Worker exited unexpectedly with exit code 3221226505'),+ }));+ await new Promise(resolve => setImmediate(resolve));+ expect(true).toBe(true);+ });`);+ const runnerUrl = new URL('./run-ci-tests.js', import.meta.url).href;+ const args = ['--config', join(fixture, 'vitest.config.mjs'), '--root', fixture, './fixture.test.js'];+ const env = { ...process.env, NODE_ENV: 'test', TMPDIR: host, TMP: host, TEMP: host, NODE_DISABLE_COMPILE_CACHE: '1' };+ delete env.PORTOS_TEST_TEMP_ROOT;+ const result = spawnSync(process.execPath, ['--input-type=module', '-e',+ `import { runNpm } from ${JSON.stringify(runnerUrl)};+ const result = await runNpm('server', 'test:ci:related', ${JSON.stringify(args)});+ process.exitCode = result.status;`,+ ], { encoding: 'utf8', timeout: 30_000, env });+ expect(result.error).toBeUndefined();+ expect(result.status).toBe(1);+ expect(result.stdout).toMatch(/Test Files\s+1 passed/);+ const diagnostic = result.stderr.slice(result.stderr.indexOf('Test launcher exited:'));+ expect(diagnostic).toContain('code=1, signal=none');+ expect(diagnostic).toContain('unhandledErrors=1');+ const errors = diagnostic.slice(diagnostic.indexOf('Vitest unhandled errors'), diagnostic.indexOf('Inconclusive'));+ expect(errors).toContain('synthetic run-level rejection');+ expect(errors).toContain('fixture.test.js:');+ expect(errors).toContain('Worker exited unexpectedly with exit code 3221226505');+ expect(diagnostic).toContain('Vitest output tail');+ expect(readdirSync(host)).toEqual([]);+ } finally {+ rmSync(fixture, { recursive: true, force: true });+ }+ }, 35_000);+it('recovers a real bail failure with default and GitHub reporters even if their summary is incomplete', () => {const fixture = mkdtempSync(join(tmpdir(), 'portos-diagnostics-'));try {diff --git a/scripts/vitestRunDiagnostics.js b/scripts/vitestRunDiagnostics.jsnew file mode 100644index 0000000000..d3ea064698--- /dev/null+++ b/scripts/vitestRunDiagnostics.js@@ -0,0 +1,38 @@+import { writeFileSync } from 'node:fs';++// The JSON reporter includes test failures, but drops run-level errors such as+// unhandled rejections and worker crashes. Keep those independently of stdout.+function errorDiagnostic(error, depth = 0) {+ if (!error || typeof error !== 'object') return String(error).slice(0, 8000);+ const lines = [error.type, error.stack || `${error.name || 'Error'}: ${error.message || ''}`];+ if (error.code !== undefined) lines.push(`code: ${error.code}`);+ if (error.cause && depth < 3) lines.push(`Caused by: ${errorDiagnostic(error.cause, depth + 1)}`);+ return lines.filter(Boolean).join('\n').slice(0, 8000);+}++export default function vitestRunDiagnostics() {+ let diagnostics = { reason: 'not-finished', errorCount: 0, errors: [] };+ const persist = () => {+ const path = process.env.PORTOS_CI_DIAGNOSTICS_PATH;+ if (!path) return;+ try {+ writeFileSync(path, JSON.stringify({ ...diagnostics, exitCode: process.exitCode ?? null }));+ } catch {+ // Diagnostic I/O must never turn a passing test run into a failure.+ console.error('Unable to persist Vitest run diagnostics.');+ }+ };+ return {+ onInit(vitest) {+ vitest.onClose(persist);+ },+ onTestRunEnd(_modules, errors, reason) {+ diagnostics = { reason, errorCount: errors.length, errors: errors.slice(0, 10).map(error => errorDiagnostic(error)) };+ persist();+ },+ onProcessTimeout() {+ diagnostics = { ...diagnostics, teardownTimeout: true };+ persist();+ },+ };+}