Files
orca/config/scripts/node-server-change-scope.mjs
T
Neil 1912c90141 Extract subprocess handling into @orca/process-host (#26963)
Extract subprocess execution and process contracts into @orca/process-host with explicit public entry points, independent checks, and one Node 18 compatible CommonJS runtime. Migrate application consumers and integrate package builds, dependency selection, import enforcement, and runtime resource staging.

Preserve daemon startup ownership and profile isolation, and qualify development rebuild recovery and concurrent output publication. Remove unnecessary aliases, test forwarding exports, and a duplicate output scan.

Validation: full typechecks, changed-code quality, package and artifact checks, native Windows builds/types plus 306 runtime tests, and independent native reviews.

Matched full E2E controls on original main and unchanged reviewed head both pass all 17 applicable tests (nine skips). Earlier intermittent E2E failures are documented in the PR; no assertions were weakened. The final combined-tree audit preserves both branches with no import-boundary violations.
2026-10-09 18:32:28 -07:00

201 lines
6.7 KiB
JavaScript

import { appendFileSync, globSync, readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { pathToFileURL } from 'node:url'
import { nodeServerTestPaths } from './node-server-test-paths.mjs'
import { nodeServerQualification } from './node-server-qualification.mjs'
const ROOT = resolve(import.meta.dirname, '../..')
const BUILD_SCRIPTS = [
'config/scripts/build-orcad-node.mjs',
'config/scripts/server-build-target.mjs',
'config/scripts/pinned-node-downloads.mjs',
'config/scripts/build-orcad.mjs',
'config/scripts/build-orcad-prebuilds.mjs',
'config/scripts/orcad-windows-prebuild-cache.mjs',
'config/scripts/orcad-prebuild-smoke-child.cjs',
'config/scripts/build-windows-process-tree-relay-addon.mjs',
'config/scripts/run-node-server-tests.mjs',
'config/vitest.config.ts',
'config/scripts/happy-dom-offscreen-canvas.ts',
'config/scripts/happy-dom-mutation-observer-retention.ts',
'config/scripts/vitest-host-ports-setup.ts',
'config/scripts/vitest-real-agent-home-write-guard.ts'
]
const ALWAYS_FILES = new Set([
'package.json',
'pnpm-lock.yaml',
'pnpm-workspace.yaml',
'.npmrc',
'.pnpmfile.cjs',
'tsconfig.json',
'.github/workflows/node-server-tests.yml',
'config/scripts/node-server-change-scope.mjs',
'config/scripts/node-server-change-scope.test.mjs',
'config/scripts/workspace-source-exports.mjs',
'config/scripts/workspace-source-exports.test.mjs',
'config/scripts/headless-detector-compiler-cache.mjs',
'config/scripts/node-server-qualification.mjs',
'config/scripts/node-server-qualification.test.mjs',
// The source graph sees process-host source, not the build that emits the dist servers load.
'src/packages/process-host/.gitignore',
'src/packages/process-host/scripts/build-dist.mjs',
'src/packages/process-host/tsconfig.json'
])
const ALWAYS_PREFIXES = [
'.github/actions/install-node-dependencies/',
'.github/actions/restore-pnpm-verification/',
'.github/actions/prepare-headless-compiler/',
'.github/actions/prepare-native-runtime/',
'.github/actions/prepare-orcad-prebuilds/',
// These areas also contain worker paths and fixtures opened without an import.
'src/main/persistence/',
'src/main/sqlite/',
'src/main/orcad/',
'src/main/daemon/pty-subprocess/',
'src/main/providers/',
'config/patches/',
'config/tsconfig',
'native/',
'resources/licenses/ripgrep/'
]
export function discoverNodeServerTests(root = ROOT) {
const selectors = nodeServerTestPaths({ artifact: true, crossRuntime: true })
return globSync(
['src/**/*.test.{ts,tsx}', 'config/scripts/**/*.test.{ts,mjs}', 'tests/e2e/**/*.unit.test.ts'],
{ cwd: root }
)
.map((file) => file.replaceAll('\\', '/'))
.filter((file) => selectors.some((selector) => file.includes(selector)))
.sort()
}
export async function collectNodeServerInputs({ root = ROOT, entryPoints } = {}) {
const [
{ build },
{
externalNativeAddons,
ORCAD_CHILD_ENTRY_POINTS,
ORCAD_ENTRY_POINT,
ORCAD_LAUNCHER_ENTRY_POINT
},
{ createWorkspaceSourceResolver }
] = await Promise.all([
import('esbuild'),
import('./orcad-entry-build.mjs'),
import('./workspace-source-exports.mjs')
])
const workspace = createWorkspaceSourceResolver(root)
const manifests = new Set()
const workspaceSources = {
name: 'workspace-source-exports',
setup(pluginBuild) {
pluginBuild.onResolve({ filter: /^[^./]/ }, ({ path }) => {
const source = workspace.resolve(path)
if (!source) {
return null
}
manifests.add(source.manifest)
return { path: source.path }
})
}
}
const entries = entryPoints ?? [
ORCAD_ENTRY_POINT,
ORCAD_LAUNCHER_ENTRY_POINT,
...Object.values(ORCAD_CHILD_ENTRY_POINTS),
...BUILD_SCRIPTS,
...discoverNodeServerTests(root)
]
const result = await build({
absWorkingDir: root,
entryPoints: entries,
bundle: true,
write: false,
outdir: resolve(root, '.node-server-scope'),
platform: 'node',
format: 'esm',
splitting: true,
packages: 'external',
loader: { '.svg': 'empty', '.png': 'empty', '.webp': 'empty', '.css': 'empty' },
plugins: [workspaceSources, externalNativeAddons],
metafile: true,
logLevel: 'silent'
})
if (result.warnings.length > 0) {
throw new Error(result.warnings.map((warning) => warning.text).join('\n'))
}
return new Set([
...manifests,
...Object.keys(result.metafile.inputs).map((file) =>
file.replaceAll('\\', '/').replace(/\?.*$/, '')
)
])
}
function isSourceUnitTest(file) {
return (
/^src\/(?:[^/]+\/)*[^/]+\.test\.(?:ts|tsx)$/.test(file) &&
!file.includes('/../') &&
!file.includes('/./')
)
}
export async function classifyNodeServerChanges(
changedFiles,
collect = collectNodeServerInputs,
{ deferGraph = false } = {}
) {
if (changedFiles.length === 0) {
return { shouldRun: true, reason: 'No complete changed-file evidence' }
}
const selectors = nodeServerTestPaths({ artifact: true, crossRuntime: true })
const forced = changedFiles.find(
(file) =>
ALWAYS_FILES.has(file) ||
(ALWAYS_PREFIXES.some((prefix) => file.startsWith(prefix)) && !isSourceUnitTest(file)) ||
selectors.some((selector) => file.includes(selector))
)
if (forced) {
return { shouldRun: true, reason: `Build or CI input changed: ${forced}` }
}
if (deferGraph) {
return { graphRequired: true, reason: 'Installed dependencies are needed to check imports' }
}
try {
const inputs = await collect()
const matched = changedFiles.find((file) => inputs.has(file))
return {
shouldRun: Boolean(matched),
reason: matched
? `Runtime or test dependency changed: ${matched}`
: 'No headless-server inputs changed'
}
} catch (error) {
return {
shouldRun: true,
graphUnavailable: true,
reason: `Dependency graph unavailable: ${String(error)}`
}
}
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
const changedFiles = readFileSync(process.argv[2], 'utf8').split('\0').filter(Boolean)
const result = await classifyNodeServerChanges(changedFiles, collectNodeServerInputs, {
deferGraph: process.argv.includes('--defer-graph')
})
console.log(result.reason)
const policy = nodeServerQualification(changedFiles, result, {
fullQualification: process.argv.includes('--full-qualification')
})
const output = result.graphRequired
? 'graph_required=true\n'
: `should_run=${result.shouldRun}\nqualification=${policy.qualification}\nrunners=${JSON.stringify(policy.runners)}\n`
if (process.env.GITHUB_OUTPUT) {
appendFileSync(process.env.GITHUB_OUTPUT, output)
} else {
process.stdout.write(output)
}
}