diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 71ed3ea..68b61b8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -125,7 +125,8 @@ jobs: with: bun-version: 1.3.14 - name: Install ZIP tools - run: sudo apt-get update && sudo apt-get install -y zip unzip + # Hosted images already carry both tools; install only if one is missing. + run: command -v zip >/dev/null && command -v unzip >/dev/null || (sudo apt-get update && sudo apt-get install -y zip unzip) - name: Install project dependencies run: bun install --frozen-lockfile --ignore-scripts - name: Run Linux tests, builds and package checks @@ -172,7 +173,8 @@ jobs: with: bun-version: 1.3.14 - name: Install ZIP tools - run: sudo apt-get update && sudo apt-get install -y zip unzip + # Hosted images already carry both tools; install only if one is missing. + run: command -v zip >/dev/null && command -v unzip >/dev/null || (sudo apt-get update && sudo apt-get install -y zip unzip) - name: Install project dependencies run: bun install --frozen-lockfile --ignore-scripts - name: Run Linux tests, builds and package checks @@ -216,9 +218,23 @@ jobs: # ownership semantics without relaxing the product's SID policy. $name = 'progress' + [Guid]::NewGuid().ToString('N').Substring(0, 10) $password = ConvertTo-SecureString ('Aa1!' + [Guid]::NewGuid().ToString('N')) -AsPlainText -Force - $output = Join-Path $env:RUNNER_TEMP ($name + '.stdout') - $errors = Join-Path $env:RUNNER_TEMP ($name + '.stderr') $trace = Join-Path $env:RUNNER_TEMP 'windows-migration-trace.jsonl' + # The two native files run side by side, each in its own serial test + # runner. Only the service file starts a server and request workers, + # which are what take Setup coordination ports; the platform file + # drives the setup controller directly. Each runs under Start-Process + # -Wait inside a thread job, so it is waited for with everything it + # started, exactly as when they ran one after the other. Starting the + # two at the same instant can be refused once, so the start is retried. + $runs = @('windows-platform', 'windows-service') | ForEach-Object { + [pscustomobject]@{ + File = $_ + Output = Join-Path $env:RUNNER_TEMP ($name + '.' + $_ + '.stdout') + Errors = Join-Path $env:RUNNER_TEMP ($name + '.' + $_ + '.stderr') + Job = $null + ExitCode = $null + } + } # Leave a fixed diagnostic even if principal/process creation fails. '{"request":"runner-start","completed":false}' | Set-Content -LiteralPath $trace -Encoding utf8 $created = $false @@ -227,23 +243,45 @@ jobs: $created = $true Add-LocalGroupMember -SID 'S-1-5-32-545' -Member $user $credential = [PSCredential]::new(($env:COMPUTERNAME + '\' + $name), $password) - $process = Start-Process -FilePath (Get-Command node).Source -ArgumentList '--experimental-strip-types --test --test-concurrency=1 tests/backend/windows-platform.test.ts tests/backend/windows-service.test.ts' -WorkingDirectory $env:GITHUB_WORKSPACE -Credential $credential -LoadUserProfile -WindowStyle Hidden -Wait -PassThru -RedirectStandardOutput $output -RedirectStandardError $errors - if (Test-Path $output) { - Get-Content -LiteralPath $output + foreach ($run in $runs) { + $run.Job = Start-ThreadJob -ArgumentList $run, $credential, $env:GITHUB_WORKSPACE, (Get-Command node).Source -ScriptBlock { + param($run, $credential, $workspace, $node) + # The secondary logon service creates one process at a time and + # refuses a second request while it is busy; retry only that refusal. + $process = $null + for ($attempt = 1; -not $process; $attempt++) { + try { + $process = Start-Process -FilePath $node -ArgumentList "--experimental-strip-types --test --test-concurrency=1 tests/backend/$($run.File).test.ts" -WorkingDirectory $workspace -Credential $credential -LoadUserProfile -WindowStyle Hidden -Wait -PassThru -RedirectStandardOutput $run.Output -RedirectStandardError $run.Errors -ErrorAction Stop + } catch { + if ($attempt -ge 30 -or $_.Exception.Message -notmatch 'already running') { throw } + Start-Sleep -Seconds 1 + } + } + $process.ExitCode + } + } + $null = $runs.Job | Wait-Job + foreach ($run in $runs) { $run.ExitCode = @(Receive-Job -Job $run.Job -ErrorAction Stop)[-1] } + foreach ($run in $runs) { + if (Test-Path $run.Output) { + Get-Content -LiteralPath $run.Output + } + if (Test-Path $run.Errors) { Get-Content -LiteralPath $run.Errors } } - if (Test-Path $errors) { Get-Content -LiteralPath $errors } - if ($process.ExitCode -ne 0) { throw 'Native Windows tests failed.' } + if (@($runs | Where-Object { $_.ExitCode -ne 0 }).Count -gt 0) { throw 'Native Windows tests failed.' } } finally { # Preserve per-request evidence even on an assertion/process failure. # Never upload raw stdout/stderr, local principal names or paths. - if (Test-Path $output) { - $traces = @(Get-Content -LiteralPath $output | ForEach-Object { - if ($_ -match '^\s*(?:#\s*)?WINDOWS_MIGRATION_TRACE (.+)$') { $Matches[1] } - }) - if ($traces.Count -gt 0) { $traces | Add-Content -LiteralPath $trace -Encoding utf8 } + foreach ($run in $runs) { + if (Test-Path $run.Output) { + $traces = @(Get-Content -LiteralPath $run.Output | ForEach-Object { + if ($_ -match '^\s*(?:#\s*)?WINDOWS_MIGRATION_TRACE (.+)$') { $Matches[1] } + }) + if ($traces.Count -gt 0) { $traces | Add-Content -LiteralPath $trace -Encoding utf8 } + } } if ($created) { Remove-LocalUser -Name $name } - Remove-Item -LiteralPath $output, $errors -Force -ErrorAction SilentlyContinue + Remove-Item -LiteralPath @($runs | ForEach-Object { $_.Output; $_.Errors }) -Force -ErrorAction SilentlyContinue } $requests = @(Get-Content -LiteralPath $trace | ForEach-Object { $_ | ConvertFrom-Json } | Where-Object { $_.request -and $_.request -notin @('runner-start','summary') }) if ($requests.Count -eq 0) { throw 'Native Windows request trace is missing.' } diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 38a891f..47d9a95 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -40,6 +40,20 @@ Released-writer delegation fixtures require the local `v1.3.0` tag. Run skip with a prerequisite diagnostic when the tag is unavailable. Full portable CI requires full history and tags, so these cases run there. +Two scripts run the portable groups side by side after building every entry, and print each step's duration at the end: + +```sh +# Edit-and-check loop: both typechecks, the parallel backend group, browser-model +# and automation tests. It skips the serial Setup-port backend group (below), +# which holds most of the full run's time, so it is not what CI runs. +bun run test:fast + +# Everything portable CI tests, minus packaging and the rebuild comparison +bun run test:all +``` + +Both leave out the five native-only backend tests (see [Portable CI](#portable-ci--fresh-disposable-checkout-only)) and say so. Run `bun run test:all` or the commands below before a pull request when you touched backend, setup or service code; `test:fast` can pass where the serial group would fail. They are not replacements for `node --experimental-strip-types scripts/ci.ts` in a fresh checkout. + Run the checks relevant to your change: ```sh @@ -89,6 +103,56 @@ concurrent writer. Keep new fenced HTTP/worker test files in the list in This isolates tests, not product requests; no port override or relaxed refusal is used. +That serial group is the longest part of the run. It is one request after another, +each starting a worker thread, plus a few deliberate deadline waits, so it is bound by +latency rather than by cores; no other group acquires Setup ports (checked with a probe +on the acquisition call). `scripts/ci.ts` +and `bun run test:all` therefore run it in one lane beside a second lane that +holds both typechecks, the parallel backend group and the browser-model and +automation tests (`scripts/test-plan.ts`, `scripts/test-lanes.ts`). The second +lane is capped at the machine's cores minus two and runs under `nice`, so the +timing assertions in the serial group keep their headroom (the cap never goes below +one worker, so on one to three cores the lane runs one worker at a time). A file that takes +a Setup port belongs in the serial group; never add one to the other lane. A listen probe +(a preload on `net.Server.prototype.listen`, covering worker threads, child processes and the +bundled `build/service/main.js`) found that no file in the parallel group or the browser and +automation tests takes one in the CI configuration. One file does in one configuration CI does +not use: `plan-cleanup-service.test.ts` runs the built service when `CP_BUILT_SERVICE=1`, and the +plan then moves it into the serial group (`fencedWhenSet` in `scripts/ci-backend.ts`). + +The runner (`scripts/test-lanes.ts`) is POSIX-first: it prints each step's output whole under its +own heading (the last 2 MB if a step is noisier, saying what was dropped), names the steps still +running about every minute, and ends with a per-step duration list. The first failure, SIGINT, +SIGTERM, SIGHUP or a closed output pipe (`test:all | head`) stops both lanes and prints what the +running steps had written. Each step runs in its own process group, so the runner stops the groups +itself: a repeated signal, or a step that has not stopped ten seconds after the first, is killed +outright and abandoned even if a leftover process still holds its output pipes, and every group +the runner started is swept as soon as its leader has exited and its pipes have closed (SIGTERM, a short +bound, SIGKILL, a short bound, then a probe that finds the group empty), whether the step passed or not, +because a leader that exits 0 can leave descendants holding a port. A group leaves the record the moment a +probe finds it empty and is never signalled afterwards, since its id may then belong to an unrelated +process; every signal is preceded by a fresh probe, and the closing sweep and the exit hook touch only +groups not yet confirmed empty. Group ids are checked before use (a whole number above 1 that is neither +the runner nor its parent), and a crash of the runner kills what is still recorded. On Windows `taskkill` +goes only to a child whose `exit` has not fired. An +interrupted run exits 128 plus the signal number (130, 143, 129, 141), a failed one 1. A step +the runner stopped is reported as stopped; one that fails on its own is reported as failed with +its output, whenever it does so. On Windows steps are not detached and are stopped with +`taskkill /T /F`; that path is covered by a unit test of the stop command but has not been run on +Windows, and the typecheck step's `./node_modules/.bin/tsc` is a POSIX path, so on Windows run the +individual commands listed under Local development by hand. The process-group and signal cases of +`tests/automation/test-lanes.test.ts` are skipped on Windows (its stop-command case runs), and every case +stops the processes it started even when it fails. + +Both runners also turn on Node's compile cache for every step they start +(`NODE_COMPILE_CACHE`, a folder under the ignored `node_modules/.cache/`). It keeps +V8's compiled code per source file and checks it against the source's hash, so a changed +file is recompiled rather than served stale, and each test process and each request +worker the managed-step tests start compiles a shared module once. It changes no test +input and is emptied at the start of every run (fixtures run from fresh temporary folders, so a +kept cache only grows); a standalone `node --test` run does not use it unless you export the +variable yourself. + This is **not native GUI/OpenCode or host-installer certification**. The external host-installer gate is separate, as described below. Full `bun run check` retains the native backend prerequisites; it is not the portable CI command. The [workflow](.github/workflows/ci.yml) runs on pushes to `main` and `dev`, on every pull request whatever branch it targets, weekly on Tuesday at 04:17 UTC, and on manual dispatch. A PR stacked on another feature branch gets the same selection as one targeting `dev`. It retains the Linux portable checks on exactly Node 24.19.0, repeats them on the newest Node 24 release, and adds a targeted Windows native-filesystem suite. A newer push to a pull request cancels that pull request's earlier run. Versioned publication also waits for the full Windows job on its `main` push. Neither job substitutes for actual OpenCode/OpenChamber integration acceptance. @@ -114,7 +178,12 @@ dispatch run the full set, even for UI/docs-only changes. The workflow sets `AP_WINDOWS_FULL=1` only for those full runs and fetches `v1.1.1` only then. Without that opt-in, the three journeys explicitly skip; no per-request assertion, budget or deadline is relaxed. Local full native runs must also set -`AP_WINDOWS_FULL=1`. Weekly and manual CI runs target Windows only; the Linux +`AP_WINDOWS_FULL=1`. The Windows job starts the platform file and the service file +as two processes at once, each in its own serial test runner and as the same +unprivileged principal, each started with `Start-Process -Wait` inside a PowerShell thread job so +it is waited for together with everything it started, and reads both exit codes after both end; only the +service file starts a server and request workers, which take the Setup coordination +ports. Weekly and manual CI runs target Windows only; the Linux jobs retain their PR/push coverage. See [native Windows verification](docs/compatibility.md#native-windows-verification). Only `main` pushes can publish releases. A `dev` push runs portable checks but is never release-eligible; its publish job may appear as skipped. Accept the source on `dev` first, then prepare a promotion commit containing that source and its built outputs. Run the required host gates on the promotion checkout before its PR into `main` merges. diff --git a/docs/compatibility.md b/docs/compatibility.md index cf86bc5..1f8554f 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -493,7 +493,8 @@ permits cleanup/permission repair. See The `Windows setup, storage and capture` CI job runs the native platform suite and `tests/backend/windows-service.test.ts` in the same provisioned non-admin -account. Ordinary PRs run it when backend/build/test inputs change (unknown +account, as two processes at once, each in its own serial test runner and each waited for +with everything it started (`Start-Process -Wait` inside a thread job). Ordinary PRs run it when backend/build/test inputs change (unknown paths conservatively run it); UI, documentation, OpenSpec and browser-test-only PRs skip it, as do duplicate same-repository main/dev heads outside promotion. Every `dev` push runs the fast set. This retains every native test except three diff --git a/openspec/changes/release-1-5-2/tasks.md b/openspec/changes/release-1-5-2/tasks.md index bb1dabb..a090f5b 100644 --- a/openspec/changes/release-1-5-2/tasks.md +++ b/openspec/changes/release-1-5-2/tasks.md @@ -64,6 +64,16 @@ Each numbered group is one pull request into the development branch. It carries - [ ] 7.5 In the same pull request, reconcile the task lists of the 1.5.0 change, the 1.5.1 change and this one: tick each task whose work shipped, and leave a task that did not happen unticked with a note saying whether it is deferred, superseded or out of scope. - [ ] 7.6 In the same pull request, archive the 1.5.0 change, then the 1.5.1 change, then this one, in that order, because each later change modifies capabilities the earlier ones introduced. Never use `--skip-specs` or `--no-validate`. A rehearsal showed what this pull request must correct in the 1.5.1 change's deltas before archiving that change. First, restore the two scenario names that the 1.5.0 specs hold and two of its modified requirements dropped, either by putting the old name on the new content or by keeping the old scenario beside the renamed one. Second, shorten or split the four requirement texts that would exceed 500 characters once in the specs, keeping their meaning and their scenarios. That includes the result requirement at 628 characters, although this change replaces it in the third archive: strict validation runs after the second archive too. After each archive, write the purpose of every capability it created, ten in all, and validate the specs strictly. That validation reports nothing after each of the three archives. If an archive refuses any other delta, correct the delta. +## 9. Faster test suites + +- [x] 9.1 Measure where the portable run spends its time before changing anything: the serial backend group that acquires Setup coordination ports is about two thirds of the Linux step, and one file in it is most of that group. Record the per-step durations of recent CI runs and the per-file durations of that group. +- [x] 9.2 Run the serial Setup-port backend group beside the typechecks, the parallel backend group and the browser-model and automation tests instead of before them, in portable CI and in a new local runner that shares one plan with it. Keep the serial group as one `--test-concurrency=1` invocation that nothing else joins, cap the other lane at the machine's cores minus two, print each step's output whole under its own heading, stop every lane at the first failure or on SIGINT, SIGTERM, SIGHUP or a closed output pipe, keeping the output of steps still running and sweeping each step's group as soon as its leader has exited and its pipes closed, passed or not, retiring a group the moment it is empty and never signalling it afterwards, and sweeping only unconfirmed groups at the end (and killing them after a crash or a repeated signal), exit with 128 plus the signal number when interrupted, tail-limit and heartbeat the step output, mark a step the runner stopped as stopped and one that fails by itself as failed, and keep the second build comparison and the package checks after all tests. Show with a preload probe on the listen call, covering worker threads, child processes and the bundled service, that the parallel backend group and the browser and automation tests take no Setup port, and fence the one file that does when `CP_BUILT_SERVICE=1` is set. Give the plan a core count so one to three cores are tested. +- [x] 9.3 Add `bun run test:fast` (both typechecks, the parallel backend group, browser-model and automation tests) and `bun run test:all` (everything portable CI tests except packaging and the rebuild comparison), document them in the contributor guide, and cover the plan and the runner with automation tests: every portable file runs once, the serial group is alone and serial, and a failure stops the other lane. +- [x] 9.4 Turn on Node's compile cache, in a folder under the ignored dependency folder, for every step of the CI and local runners. It checks each cached file against the source's hash, so no test input changes. Record its effect from CI step timings and from the heaviest managed-step test run alone, and say that the typecheck times did not move. +- [x] 9.5 Skip installing the ZIP tools in the two Linux CI jobs when the runner image already has them. Keep every job and check name, the secrets scan and the second build unchanged. +- [x] 9.6 Start the two native Windows test files as two processes at once in the Windows job, each in its own `--test-concurrency=1` runner and as the same unprivileged principal, each waited for with everything it started, and read both exit codes and both request traces after both end. Only the service file starts a server and request workers, so only it takes Setup coordination ports. The same 26 tests run, with the same pass and skip counts as before. +- [x] 9.7 Remove or merge no test. Record what was considered and rejected as unsafe or unproven in the pull request. + ## Deferred acceptance and out-of-scope follow-up - [ ] Promotion to the distribution branch and its host gates, as the contributor guide describes. It follows the release pull request, and so the archive of this change. The nine generated outputs are built and committed by the promotion only. diff --git a/package.json b/package.json index 56f46f9..5922356 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,8 @@ "scripts": { "typecheck": "tsc --noEmit", "test:backend": "bun run build:all && node --experimental-strip-types --test tests/backend/*.test.ts", + "test:all": "bun run build:all && node --experimental-strip-types scripts/test.ts", + "test:fast": "bun run build:all && node --experimental-strip-types scripts/test.ts --fast", "build:backend": "bun scripts/build.ts", "build:all": "node --experimental-strip-types scripts/build-all.ts", "build": "bun run build:all", diff --git a/scripts/ci-backend.ts b/scripts/ci-backend.ts index f3d52ff..d9c74e0 100644 --- a/scripts/ci-backend.ts +++ b/scripts/ci-backend.ts @@ -16,8 +16,28 @@ const fenceTests = new Set([ 'windows-service.test.ts', ]); -export function backendTestCommands(files: string[]): string[][] { - const fenced = (file: string) => fenceTests.has(file.split('/').at(-1)!); +/** Backend files that need an independent OpenCode binary and Linux isolation + * tools. Portable CI and the local runners leave them out and say so. */ +export const nativeOnlyTests = [ + 'production-runtime.test.ts', + 'plugin-failure-runtime.test.ts', + 'progress-activation-runtime.test.ts', + 'subagent-activity-runtime.test.ts', + 'subagent-upgrade-runtime.test.ts', +]; + +/** Files that take Setup ports only when an environment variable opts in, mapped to + * that variable. `plan-cleanup-service.test.ts` runs the checked-in built service, + * whose readiness probe takes writer slots, when `CP_BUILT_SERVICE=1` (found with a + * listen probe, bundled service included). Where the opt-in is set the file is + * fenced; where it is not, the file takes no port and stays in the parallel group. */ +export const fencedWhenSet: Record = { 'plan-cleanup-service.test.ts': 'CP_BUILT_SERVICE' }; + +export function backendTestCommands(files: string[], environment: NodeJS.ProcessEnv = process.env): string[][] { + const fenced = (file: string) => { + const name = file.split('/').at(-1)!; + return fenceTests.has(name) || (name in fencedWhenSet && environment[fencedWhenSet[name]] === '1'); + }; return [ ['--experimental-strip-types', '--test', ...files.filter(file => !fenced(file))], ['--experimental-strip-types', '--test', '--test-concurrency=1', ...files.filter(fenced)], diff --git a/scripts/ci.ts b/scripts/ci.ts index 128c41c..02dcc82 100644 --- a/scripts/ci.ts +++ b/scripts/ci.ts @@ -4,7 +4,9 @@ import { readFileSync, readdirSync, mkdirSync, existsSync, copyFileSync, writeFi import { createHash } from 'node:crypto'; import { eligibility, assetNames, validateReleaseNotes } from './release.ts'; import { verifyCompanionEntry } from './companion-entry.ts'; -import { backendTestCommands } from './ci-backend.ts'; +import { nativeOnlyTests } from './ci-backend.ts'; +import { LanesError, runLanes } from './test-lanes.ts'; +import { freshCompileCache, portableLanes } from './test-plan.ts'; import { buildAll, outputHashes, compareOutputs } from './build-all.ts'; import { revisionKind, trackedOutputs } from './revision-kind.ts'; @@ -14,7 +16,6 @@ if (existsSync('build/artifacts') && readdirSync('build/artifacts').length > 0) process.exit(1); } -const excluded = ['production-runtime.test.ts', 'plugin-failure-runtime.test.ts', 'progress-activation-runtime.test.ts', 'subagent-activity-runtime.test.ts', 'subagent-upgrade-runtime.test.ts']; function run(command:string, args:string[]) { if (spawnSync(command,args,{stdio:'inherit'}).status !== 0) throw new Error('Portable check failed'); } @@ -33,12 +34,12 @@ if (kind === 'source') buildAll(); const before = outputHashes(); verifyCompanionEntry(process.cwd()); mkdirSync('/tmp/opencode',{recursive:true}); -console.log(`Native-only tests NOT RUN: ${excluded.join(', ')}`); -run('bun',['run','typecheck']); -run('./node_modules/.bin/tsc',['-p','ui/tsconfig.json','--noEmit']); -const tests = (dir:string) => readdirSync(dir).filter(f=>f.endsWith('.test.ts')).sort().map(f=>`${dir}/${f}`); -for (const args of backendTestCommands(tests('tests/backend').filter(f=>!excluded.includes(f.split('/').at(-1)!)))) run('node',args); -run('node',['--experimental-strip-types','--test',...tests('tests/browser'),...tests('tests/automation')]); +console.log(`Native-only tests NOT RUN: ${nativeOnlyTests.join(', ')}`); +// Root and UI typechecks, the parallel backend group, and browser-model and +// automation tests run beside the serial backend group that binds Setup ports. +// Output is grouped per step and the first failure stops every lane. +try { await runLanes(portableLanes({ fenced: true, env: freshCompileCache() })); } +catch (error) { console.error(error instanceof Error ? error.message : error); process.exit(error instanceof LanesError ? error.exitCode : 1); } buildAll(); compareOutputs(before); // The SDK's own readiness check: manifest schema, page scripts and service entry. diff --git a/scripts/test-lanes.ts b/scripts/test-lanes.ts new file mode 100644 index 0000000..261a233 --- /dev/null +++ b/scripts/test-lanes.ts @@ -0,0 +1,255 @@ +/** Runs independent groups of checks side by side. A lane is an ordered list of + * steps that stop at the first failure; lanes share nothing but the checkout. + * Output is captured per step and written whole when the step ends, so lines + * from different lanes never interleave. + * + * POSIX first. Each step runs in its own process group, so a signal to this + * process would not reach it; the runner therefore stops every step itself, on the + * first failure and on SIGINT, SIGTERM, SIGHUP or a closed output pipe, and a step + * left running would keep holding the Setup ports the next run needs. A repeated + * signal, or a step that ignores the first one for too long, is killed outright + * and abandoned even if its output pipes are still held open; every group the + * runner started is swept as soon as its leader has exited and its pipes have + * closed, passing or not, since a leader that exits 0 can leave descendants + * holding ports. A group is retired the moment a probe finds it empty and never + * signalled afterwards (its id may then belong to someone else); every signal is + * preceded by a probe, and the exit hook and closing sweep touch only groups not + * yet confirmed empty. On Windows steps are not detached and are stopped with + * `taskkill /T /F`; that path is untested on Windows. */ +import { spawn, spawnSync, type ChildProcess } from 'node:child_process'; + +/** `lowPriority` runs the step under `nice`, so a lane that must keep its timing + * headroom wins the cores when the machine is short of them. */ +export type Step = { label: string; command: string; args: string[]; lowPriority?: boolean; env?: Record }; +export type Lane = { name: string; steps: Step[] }; +export type StepResult = { lane: string; label: string; seconds: number; status: 'passed' | 'failed' | 'stopped' }; +export type RunOptions = { + /** How often to name the steps still running. */ + heartbeatMs?: number; + /** How long a step may take to stop before it is killed and abandoned. */ + killAfterMs?: number; + /** How long a step that has exited may keep its output pipes open. */ + graceMs?: number; + /** The most output kept per step; the earliest is dropped first. */ + maxOutputBytes?: number; + /** How long a group sweep waits for the group to leave after SIGTERM, and again after SIGKILL. */ + sweepMs?: number; + /** Replaces process.kill for every probe and signal sent to a process group; for tests. */ + kill?: Kill; +}; + +export type Kill = (pid: number, signal: NodeJS.Signals | 0) => void; + +const SIGNAL_NUMBER: Record = { SIGHUP: 1, SIGINT: 2, SIGPIPE: 13, SIGTERM: 15 }; + +/** `exitCode` is 1 for a failed run and 128 plus the signal number for an interrupted one. */ +export class LanesError extends Error { + readonly exitCode: number; + constructor(message: string, exitCode: number) { super(message); this.exitCode = exitCode; } +} + +export type StopPlan = + | { kind: 'group'; group: number; signal: 'SIGTERM' | 'SIGKILL' } + | { kind: 'command'; command: string; args: string[] }; + +/** How to stop a step and everything it started on a platform. */ +export function stopPlan(platform: NodeJS.Platform, pid: number, force: boolean): StopPlan { + if (platform === 'win32') return { kind: 'command', command: 'taskkill', args: ['/T', '/F', '/PID', String(pid)] }; + return { kind: 'group', group: -pid, signal: force ? 'SIGKILL' : 'SIGTERM' }; +} + +/** Steps get their own process group everywhere but Windows, which has none to use. */ +export const detachesSteps = (platform: NodeJS.Platform) => platform !== 'win32'; + +const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); + +/** A pid or group id the runner may signal: a whole number above 1 that is neither this process nor its + * parent. Zero would be this process's own group, a negative number another group, and 1 is init. */ +export function validPid(pid: unknown): pid is number { + return typeof pid === 'number' && Number.isInteger(pid) && pid > 1 && pid !== process.pid && pid !== process.ppid; +} + +/** What to run to stop a step, if anything. On Windows `taskkill /T /F` goes only to a child whose `exit` has + * not fired: its PID may have been reused since. On POSIX a group is signalled only after a fresh probe. */ +export function stopAction(platform: NodeJS.Platform, exited: boolean, pid: number, force: boolean): StopPlan | undefined { + if (!validPid(pid)) return undefined; + if (platform === 'win32' && exited) return undefined; + return stopPlan(platform, pid, force); +} + +/** The last `max` bytes written, with a note of what was dropped. */ +class Tail { + private chunks: Buffer[] = []; + private size = 0; + private dropped = 0; + private readonly max: number; + constructor(max: number) { this.max = max; } + push(chunk: Buffer) { + this.chunks.push(chunk); this.size += chunk.length; + while (this.size > this.max && this.chunks.length > 1) { + const first = this.chunks.shift()!; this.size -= first.length; this.dropped += first.length; + } + if (this.size > this.max) { + const cut = this.size - this.max; + this.chunks[0] = this.chunks[0].subarray(cut); this.size -= cut; this.dropped += cut; + } + } + text() { + const text = Buffer.concat(this.chunks).toString('utf8'); + return this.dropped ? `[${this.dropped} bytes of earlier output dropped]\n${text}` : text; + } +} + +const github = process.env.GITHUB_ACTIONS === 'true'; +const stamp = (seconds: number) => `${seconds.toFixed(1)}s`; + +function print(label: string, text: string, ok: boolean) { + // One write per block keeps another lane's output out of the middle of it. + const body = text.endsWith('\n') || text === '' ? text : `${text}\n`; + process.stdout.write(github ? `::group::${label}\n${body}::endgroup::\n` : `\n=== ${label} ${ok ? '' : '(failed) '}===\n${body}`); +} + +type Live = { label: string; started: number; child: ChildProcess; exited: boolean; signalled: boolean; printed: boolean; dump: () => void; force: (note: string) => void }; + +export async function runLanes(lanes: Lane[], options: RunOptions = {}): Promise { + const { heartbeatMs = 60_000, killAfterMs = 10_000, graceMs = 3_000, maxOutputBytes = 2 * 1024 * 1024, sweepMs = 2_000 } = options; + const kill: Kill = options.kill ?? ((pid, signal) => { process.kill(pid, signal); }); + const results: StepResult[] = []; + // Groups not yet known to be empty. A group leaves as soon as a probe finds it empty, because its id may + // then be reused by an unrelated process; nothing is ever signalled once it has left. + const groups = new Set(); + const live = new Map(); + let failed = false; + let interrupted: { signal: string; code: number } | undefined; + let killTimer: NodeJS.Timeout | undefined; + + const groupAlive = (group: number) => { + try { kill(-group, 0); return true; } catch (error) { return (error as NodeJS.ErrnoException).code === 'EPERM'; } + }; + /** Signal a recorded group, probing it first; an empty group is retired instead. */ + const signalGroup = (group: number, signal: 'SIGTERM' | 'SIGKILL') => { + if (!groups.has(group) || !validPid(group)) return; + if (!groupAlive(group)) { groups.delete(group); return; } + try { kill(-group, signal); } catch { /* gone between probe and signal */ } + }; + const stopStep = (pid: number, step: Live, force: boolean) => { + const plan = stopAction(process.platform, step.exited, pid, force); + if (plan?.kind === 'group') signalGroup(pid, plan.signal); + else if (plan?.kind === 'command') spawnSync(plan.command, plan.args, { stdio: 'ignore', windowsHide: true }); + }; + /** TERM, a short bound, KILL, a short bound; retire the group once it is confirmed empty. */ + const sweepGroup = async (group: number) => { + signalGroup(group, 'SIGTERM'); + for (const end = Date.now() + sweepMs; groups.has(group) && Date.now() < end;) { if (groupAlive(group)) await sleep(25); else groups.delete(group); } + signalGroup(group, 'SIGKILL'); + for (const end = Date.now() + sweepMs; groups.has(group) && Date.now() < end;) { if (groupAlive(group)) await sleep(25); else groups.delete(group); } + }; + const escalate = () => { + clearTimeout(killTimer); killTimer = undefined; + for (const [pid, step] of live) { step.signalled = true; stopStep(pid, step, true); } + for (const step of [...live.values()]) step.force('Did not stop in time; killed and abandoned.\n'); + }; + const beginStop = () => { + for (const [pid, step] of live) { + // A leader that has exited on its own is not ours to call stopped, but its group may still be running. + if (!step.exited && step.child.exitCode === null && step.child.signalCode === null) step.signalled = true; + stopStep(pid, step, false); + } + // Not unref'd: steps are still running, and the run must outlast them. + killTimer ??= setTimeout(escalate, killAfterMs); + }; + const onSignal = (signal: string) => { + if (interrupted) { escalate(); return; } + interrupted = { signal, code: 128 + (SIGNAL_NUMBER[signal] ?? 0) }; failed = true; + for (const step of live.values()) step.dump(); + beginStop(); + }; + const handlers = (['SIGINT', 'SIGTERM', 'SIGHUP'] as const).map(signal => [signal, () => onSignal(signal)] as const); + for (const [signal, handler] of handlers) process.on(signal, handler); + // `test:all | head` closes our output: stop rather than die with groups running. + const onStdoutError = (error: NodeJS.ErrnoException) => { if (error.code === 'EPIPE') onSignal('SIGPIPE'); }; + process.stdout.on('error', onStdoutError); + // A crash or process.exit must not orphan a group either. + const onExit = () => { + for (const pid of [...groups]) signalGroup(pid, 'SIGKILL'); + for (const [pid, step] of live) stopStep(pid, step, true); + }; + process.on('exit', onExit); + const heartbeat = setInterval(() => { + const names = [...live.values()].map(step => `${step.label} (${stamp((performance.now() - step.started) / 1000)})`); + if (names.length) console.log(`still running: ${names.join('; ')}`); + }, heartbeatMs); + + const runStep = (lane: Lane, step: Step) => new Promise(resolve => { + const started = performance.now(); + const label = `${lane.name}: ${step.label}`; + console.log(`start ${label}`); + const tail = new Tail(maxOutputBytes); + const niced = step.lowPriority && process.platform !== 'win32'; + const child = spawn(niced ? 'nice' : step.command, niced ? ['-n', '10', step.command, ...step.args] : step.args, { + stdio: ['ignore', 'pipe', 'pipe'], detached: detachesSteps(process.platform), windowsHide: true, env: { ...process.env, ...step.env }, + }); + const pid = child.pid; + let settled = false; + let exit: { code: number | null; signal: NodeJS.Signals | null } | undefined; + let grace: NodeJS.Timeout | undefined; + const entry: Live = { + label, started, child, exited: false, signalled: false, printed: false, + dump: () => { entry.printed = true; print(label, `${tail.text()}Interrupted before it ended.\n`, false); }, + force: note => { child.stdout?.destroy(); child.stderr?.destroy(); finish(exit?.code === 0, note); }, + }; + if (pid) { live.set(pid, entry); if (detachesSteps(process.platform)) groups.add(pid); } + child.stdout?.on('data', chunk => tail.push(chunk)); + child.stderr?.on('data', chunk => tail.push(chunk)); + child.stdout?.on('error', () => {}); + child.stderr?.on('error', () => {}); + // ENOENT emits error and then close; the first word wins. + const finish = (ok: boolean, note = '') => { + if (settled) return; + settled = true; clearTimeout(grace); + if (pid) { + live.delete(pid); + if (!live.size) { clearTimeout(killTimer); killTimer = undefined; } + } + const seconds = (performance.now() - started) / 1000; + // Only a step the runner stopped is "stopped"; one that fails on its own is a failure, whenever. + const status = ok ? 'passed' : entry.signalled ? 'stopped' : 'failed'; + if (status === 'failed' && !failed) { failed = true; beginStop(); } + if ((status === 'passed' || status === 'failed') && !entry.printed) print(label, tail.text() + note, ok); + console.log(`${status} ${label} (${stamp(seconds)})`); + results.push({ lane: lane.name, label: step.label, seconds, status }); + // Sweep the group now that its leader is gone and the pipes are closed, so a descendant (and a Setup + // port it holds) cannot outlive the step, and the group id is retired while it is still ours. + void (pid ? sweepGroup(pid) : Promise.resolve()).then(() => resolve(ok)); + }; + child.on('error', error => finish(false, `Could not start: ${error.message}\n`)); + child.on('exit', (code, signal) => { + exit = { code, signal }; entry.exited = true; + // A leftover process holding our pipes must not keep the step open for ever. + grace = setTimeout(() => entry.force('A leftover process held its output open; abandoned.\n'), graceMs); + }); + child.on('close', (code, signal) => finish(code === 0, code === 0 ? '' : `Exited with ${signal ?? `code ${code}`}\n`)); + }); + + try { + await Promise.all(lanes.map(async lane => { + for (const step of lane.steps) { + if (failed) { results.push({ lane: lane.name, label: step.label, seconds: 0, status: 'stopped' }); continue; } + if (!await runStep(lane, step)) break; + } + })); + } finally { + clearInterval(heartbeat); clearTimeout(killTimer); + // Only groups not yet confirmed empty are left: each step's own group was swept when it ended. + await Promise.all([...groups].map(sweepGroup)); + for (const [signal, handler] of handlers) process.off(signal, handler); + process.stdout.off('error', onStdoutError); + process.stdout.on('error', () => {}); // a late EPIPE while exiting is not a crash + process.off('exit', onExit); + } + console.log('\nStep timings'); + for (const r of results) console.log(` ${r.status.padEnd(7)} ${stamp(r.seconds).padStart(7)} ${r.lane}: ${r.label}`); + if (interrupted) throw new LanesError(`Portable check interrupted by ${interrupted.signal}`, interrupted.code); + if (failed) throw new LanesError('Portable check failed', 1); + return results; +} diff --git a/scripts/test-plan.ts b/scripts/test-plan.ts new file mode 100644 index 0000000..040e1b4 --- /dev/null +++ b/scripts/test-plan.ts @@ -0,0 +1,69 @@ +/** The portable check groups, shared by CI (`scripts/ci.ts`) and the local + * runner (`scripts/test.ts`) so they cannot drift apart. Nothing here weakens a + * check: it only decides which groups may run side by side. + * + * The backend files that acquire target-derived Setup coordination ports stay in + * one serial `--test-concurrency=1` invocation (see `scripts/ci-backend.ts`). It is + * a chain of requests, each starting a worker thread, with a few deliberate + * deadline waits, so it is bound by latency rather than by the number of cores. No + * other group acquires those ports (checked with a probe on the acquisition call), + * so the other groups run beside it rather than before it. */ +import { mkdirSync, readdirSync, rmSync } from 'node:fs'; +import { resolve } from 'node:path'; +import { availableParallelism } from 'node:os'; +import { backendTestCommands, nativeOnlyTests } from './ci-backend.ts'; +import type { Lane } from './test-lanes.ts'; + +export const testFiles = (dir: string) => readdirSync(dir).filter(f => f.endsWith('.test.ts')).sort().map(f => `${dir}/${f}`); + +/** Node's compile cache keeps V8's compiled code for each source file it loads and + * checks it against the source's hash, so a changed file is recompiled, never + * served stale. Every test file runs in its own process and the managed-step tests + * start a worker per request, all loading the same modules: with the cache they + * compile each module once. Entries are keyed by file name and the fixtures run + * from fresh temporary folders, so a cache kept across runs only grows (about + * 30 MB a run); each run starts from an empty one instead. It sits in the ignored + * dependency folder, never in tracked or packaged paths. A missing or unwritable + * folder only turns the cache off. */ +export function freshCompileCache(root = process.cwd()): Record { + const directory = resolve(root, 'node_modules/.cache/agent-progress-compile'); + try { + rmSync(directory, { recursive: true, force: true }); + mkdirSync(directory, { recursive: true }); + } catch { + console.log('Compile cache disabled: node_modules/.cache/agent-progress-compile could not be prepared; tests run without it.'); + return {}; + } + return { NODE_COMPILE_CACHE: directory }; +} + +export function portableLanes(options: { fenced: boolean; env?: Record; cpus?: number; environment?: NodeJS.ProcessEnv }): Lane[] { + const env = options.env ?? {}; + const backend = testFiles('tests/backend').filter(f => !nativeOnlyTests.includes(f.split('/').at(-1)!)); + const [parallel, serial] = backendTestCommands(backend, options.environment ?? process.env); + // The serial group's chain keeps about two cores busy (its main thread and the + // workers it starts). Leave them free so its timing assertions keep their + // headroom, and run this lane under `nice` as well. + const workers = `--test-concurrency=${Math.max(1, (options.cpus ?? availableParallelism()) - 2)}`; + const withWorkers = (args: string[]) => [...args.slice(0, 2), workers, ...args.slice(2)]; + const node = (label: string, args: string[]) => ({ label, command: 'node', args, env }); + const types = [ + { label: 'root typecheck', command: 'bun', args: ['run', 'typecheck'], env }, + { label: 'UI typecheck', command: './node_modules/.bin/tsc', args: ['-p', 'ui/tsconfig.json', '--noEmit'], env }, + ]; + const rest = [ + node('backend tests (parallel group)', withWorkers(parallel)), + node('browser-model and automation tests', withWorkers(['--experimental-strip-types', '--test', ...testFiles('tests/browser'), ...testFiles('tests/automation')])), + ]; + // Beside the serial group, the other lane yields cores to it. + const yielding = [...types, ...rest].map(step => ({ ...step, lowPriority: true })); + return options.fenced + ? [ + { name: 'port-bound backend', steps: [node('backend tests (serial group, Setup ports)', serial)] }, + { name: 'everything else', steps: yielding }, + ] + : [ + { name: 'types', steps: types }, + { name: 'tests', steps: rest }, + ]; +} diff --git a/scripts/test.ts b/scripts/test.ts new file mode 100644 index 0000000..3e1e2ea --- /dev/null +++ b/scripts/test.ts @@ -0,0 +1,23 @@ +/** Local portable test runner. Build the generated entries first: the package + * scripts `test:all` and `test:fast` do. It never packages or starts a native + * runtime, and it does not replace `scripts/ci.ts` for a pull request. + * + * node --experimental-strip-types scripts/test.ts every portable group + * node --experimental-strip-types scripts/test.ts --fast the same without the serial Setup-port group */ +import { mkdirSync } from 'node:fs'; +import { nativeOnlyTests } from './ci-backend.ts'; +import { LanesError, runLanes } from './test-lanes.ts'; +import { freshCompileCache, portableLanes } from './test-plan.ts'; + +const fast = process.argv.includes('--fast'); +mkdirSync('/tmp/opencode', { recursive: true }); +console.log(`Native-only tests NOT RUN: ${nativeOnlyTests.join(', ')}`); +if (fast) console.log('Fast loop: the serial Setup-port backend group is skipped. Run `bun run test:all` or CI before a pull request.'); +const started = performance.now(); +try { + await runLanes(portableLanes({ fenced: !fast, env: freshCompileCache() })); + console.log(`\nAll portable groups passed in ${((performance.now() - started) / 1000).toFixed(1)}s${fast ? ' (fast loop, not the full set)' : ''}.`); +} catch (error) { + console.error(error instanceof Error ? error.message : error); + process.exit(error instanceof LanesError ? error.exitCode : 1); +} diff --git a/tests/automation/ci.test.ts b/tests/automation/ci.test.ts index ad0266c..dc719e7 100644 --- a/tests/automation/ci.test.ts +++ b/tests/automation/ci.test.ts @@ -1,15 +1,16 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, readdirSync, rmSync } from 'node:fs'; -import { tmpdir } from 'node:os'; +import { tmpdir, availableParallelism } from 'node:os'; import { join, resolve } from 'node:path'; import { spawnSync } from 'node:child_process'; -import { backendTestCommands } from '../../scripts/ci-backend.ts'; +import { backendTestCommands, nativeOnlyTests } from '../../scripts/ci-backend.ts'; +import { freshCompileCache, portableLanes } from '../../scripts/test-plan.ts'; import { outputs } from '../../scripts/build-all.ts'; test('package scripts that consume runtime entries build before invoking their readers',()=>{ const {scripts}=JSON.parse(readFileSync('package.json','utf8')); - for(const name of ['test:backend','package','verify:package','prepare:probe','setup','check']) + for(const name of ['test:backend','test:all','test:fast','package','verify:package','prepare:probe','setup','check']) assert.equal(scripts[name].split('&&')[0].trim(),'bun run build:all',`${name} must build before its readers`); }); @@ -44,7 +45,7 @@ test('Windows service tests have an explicit same-job backend build prerequisite test('portable backend groups isolate coordination ports without omitting or repeating tests',()=>{ const files=readdirSync('tests/backend').filter(f=>f.endsWith('.test.ts')).map(f=>`tests/backend/${f}`); - const [parallel,serial]=backendTestCommands(files); + const [parallel,serial]=backendTestCommands(files,{}); assert.deepEqual(parallel.slice(0,2),['--experimental-strip-types','--test']); assert.deepEqual(serial.slice(0,3),['--experimental-strip-types','--test','--test-concurrency=1']); assert.deepEqual([...parallel.slice(2),...serial.slice(3)].sort(),files.sort()); @@ -55,7 +56,77 @@ test('portable backend groups isolate coordination ports without omitting or rep 'setup-suggestions.test.ts','windows-service.test.ts', ]); const source=readFileSync('scripts/ci.ts','utf8'); - assert.match(source,/for \(const args of backendTestCommands\([^\n]+\)\) run\('node',args\)/); + assert.match(source,/await runLanes\(portableLanes\(\{ fenced: true, env: freshCompileCache\(\) \}\)\)/); +}); + +test('portable lanes run every portable file once, keep the port-bound group serial and alone, and the fast loop drops only that group, on any core count',()=>{ + const names=(dir:string)=>readdirSync(dir).filter(f=>f.endsWith('.test.ts')).map(f=>`${dir}/${f}`).sort(); + const native=new Set(nativeOnlyTests); + const backend=names('tests/backend').filter(f=>!native.has(f.split('/').at(-1)!)); + const every=(lanes:ReturnType)=>lanes.flatMap(l=>l.steps).filter(s=>s.command==='node'); + const files=(steps:{args:string[]}[])=>steps.flatMap(s=>s.args.filter(a=>a.endsWith('.test.ts'))).sort(); + // On one to three cores the other lane's worker count is also 1, so the serial group + // cannot be told apart by its --test-concurrency=1; it is the sole step of its own lane. + for(const cpus of [1,2,3,4,24]){ + const at=`on ${cpus} cpus`; + const full=portableLanes({fenced:true,cpus,environment:{}}),fast=portableLanes({fenced:false,cpus,environment:{}}); + assert.deepEqual(files(every(full)),[...backend,...names('tests/browser'),...names('tests/automation')].sort(),at); + assert.equal(new Set(files(every(full))).size,files(every(full)).length,`no file runs twice ${at}`); + const serialLanes=full.filter(l=>l.name==='port-bound backend'); + assert.equal(serialLanes.length,1,at);assert.equal(serialLanes[0].steps.length,1,`the port-bound group shares its lane with nothing that could take a port ${at}`); + const serial=serialLanes[0].steps[0]; + assert.ok(serial.args.includes('--test-concurrency=1'),at); + assert.deepEqual(serial.args.slice(0,3),backendTestCommands(backend,{})[1].slice(0,3),at); + assert.deepEqual(files([serial]).map(f=>f.split('/').at(-1)),backendTestCommands(backend,{})[1].slice(3).map(f=>f.split('/').at(-1)).sort(),at); + assert.ok(!serial.lowPriority,`the serial group never yields cores ${at}`); + const others=full.filter(l=>l!==serialLanes[0]).flatMap(l=>l.steps); + assert.ok(others.every(s=>s.lowPriority),`the lane beside it yields cores to it ${at}`); + // Every other test step is bounded by an explicit worker count: the cores minus two, never below one. + const workers=Math.max(1,cpus-2); + for(const step of others.filter(s=>s.command==='node'))assert.ok(step.args.includes(`--test-concurrency=${workers}`),`${step.label} ${at}`); + assert.deepEqual(files(every(fast)),files(every(full)).filter(f=>!files([serial]).includes(f)),at); + assert.equal(fast.some(l=>l.steps.some(s=>s.args.includes('--test-concurrency=1')&&files([s]).some(f=>files([serial]).includes(f)))),false,`the fast loop has no serial group ${at}`); + for(const lanes of [full,fast]){ + const labels=lanes.flatMap(l=>l.steps).map(s=>s.label); + assert.ok(labels.includes('root typecheck')&&labels.includes('UI typecheck'),`both typechecks stay in every run ${at}`); + } + } + // Without a core count the host's is used. + assert.deepEqual(portableLanes({fenced:true,environment:{}}),portableLanes({fenced:true,cpus:availableParallelism(),environment:{}})); +}); + +test('a file that takes Setup ports only when CP_BUILT_SERVICE=1 runs in the serial group in that configuration and only there',()=>{ + const names=readdirSync('tests/backend').filter(f=>f.endsWith('.test.ts')).map(f=>`tests/backend/${f}`); + const opted='tests/backend/plan-cleanup-service.test.ts'; + const [parallelOff,serialOff]=backendTestCommands(names,{}); + assert.ok(parallelOff.includes(opted)&&!serialOff.includes(opted),'without the opt-in it stays parallel: it takes no port'); + const [parallelOn,serialOn]=backendTestCommands(names,{CP_BUILT_SERVICE:'1'}); + assert.ok(serialOn.includes(opted)&&!parallelOn.includes(opted),'with the opt-in its built service takes writer slots'); + assert.deepEqual([...parallelOn.slice(2),...serialOn.slice(3)].sort(),names.sort(),'still no file omitted or repeated'); + assert.ok(!backendTestCommands(names,{CP_BUILT_SERVICE:'0'})[1].includes(opted),'only 1 opts in'); + const plan=(environment:NodeJS.ProcessEnv)=>portableLanes({fenced:true,cpus:4,environment}).find(l=>l.name==='port-bound backend')!.steps[0].args; + assert.ok(plan({CP_BUILT_SERVICE:'1'}).includes(opted)&&!plan({}).includes(opted)); + const rest=(environment:NodeJS.ProcessEnv)=>portableLanes({fenced:true,cpus:4,environment}).filter(l=>l.name!=='port-bound backend').flatMap(l=>l.steps).flatMap(s=>s.args); + assert.ok(!rest({CP_BUILT_SERVICE:'1'}).includes(opted),'the lane beside the serial group never holds it'); +}); + +test('every step shares one compile cache folder that starts each run empty and stays out of tracked paths',()=>{ + const root=mkdtempSync(join(tmpdir(),'portable-cache-')); + try { + const stale=join(root,'node_modules','.cache','agent-progress-compile','stale-entry'); + mkdirSync(join(root,'node_modules','.cache','agent-progress-compile'),{recursive:true});writeFileSync(stale,'left by an earlier run'); + const env=freshCompileCache(root); + assert.equal(env.NODE_COMPILE_CACHE,join(root,'node_modules','.cache','agent-progress-compile')); + assert.deepEqual(readdirSync(env.NODE_COMPILE_CACHE),[],'an earlier run leaves nothing behind'); + for(const lanes of [portableLanes({fenced:true,env}),portableLanes({fenced:false,env})]) + assert.deepEqual(new Set(lanes.flatMap(l=>l.steps).map(s=>s.env?.NODE_COMPILE_CACHE)),new Set([env.NODE_COMPILE_CACHE]),'every step, typechecks included, gets the folder'); + // Without a cache folder, plans carry none: the plan tests above never touch a live cache. + assert.ok(portableLanes({fenced:true}).flatMap(l=>l.steps).every(s=>!s.env?.NODE_COMPILE_CACHE)); + assert.equal(spawnSync('git',['check-ignore','-q',join('node_modules','.cache','agent-progress-compile','x')]).status,0,'git ignores the folder'); + // An unusable location turns the cache off instead of failing the run. + writeFileSync(join(root,'blocked'),'a file where a folder is needed'); + assert.deepEqual(freshCompileCache(join(root,'blocked')),{}); + } finally {rmSync(root,{recursive:true,force:true});} }); test('actual portable CLI refuses nonfresh artifacts before entry reads or checks, preserving archives',()=>{ diff --git a/tests/automation/release.test.ts b/tests/automation/release.test.ts index 3b9d706..4a1223a 100644 --- a/tests/automation/release.test.ts +++ b/tests/automation/release.test.ts @@ -314,7 +314,9 @@ test('workflow keeps PR read-only, exact same-run artifact join and version seri assert(!text.includes('pull_request_target'));assert(!text.includes('secrets.')); assert.match(text,/permissions:\n contents: read/);assert.match(text,/publish:\n(?: name: [^\n]+\n)? needs: \[portable, windows-native\]/); assert.match(text,/windows-native:\n(?: name: [^\n]+\n)? runs-on: windows-2025/); - assert.match(text,/--experimental-strip-types --test --test-concurrency=1 tests\/backend\/windows-platform\.test\.ts tests\/backend\/windows-service\.test\.ts/); + // Both native files still run, each in its own serial runner, as the same unprivileged principal. + assert.match(text,/\$runs = @\('windows-platform', 'windows-service'\)/); + assert.match(text,/--experimental-strip-types --test --test-concurrency=1 tests\/backend\/\$\(\$run\.File\)\.test\.ts/); assert.match(text,/git fetch --no-tags origin tag v1\.1\.1/); assert.match(text,/name: Upload sanitized Windows migration trace\n if: always\(\)/); assert.match(text,/name: windows-migration-trace\n path: \$\{\{ runner.temp \}\}\/windows-migration-trace\.jsonl/); diff --git a/tests/automation/test-lanes.test.ts b/tests/automation/test-lanes.test.ts new file mode 100644 index 0000000..696e53c --- /dev/null +++ b/tests/automation/test-lanes.test.ts @@ -0,0 +1,410 @@ +import { test, after } from 'node:test'; +import assert from 'node:assert/strict'; +import { mkdtempSync, writeFileSync, readFileSync, readdirSync, existsSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join, resolve } from 'node:path'; +import { pathToFileURL } from 'node:url'; +import { spawn, spawnSync, type ChildProcess } from 'node:child_process'; +import * as lanes from '../../scripts/test-lanes.ts'; + +// The runner is POSIX-first: its process groups and the catchable SIGINT, SIGHUP and SIGTERM these tests send +// do not exist on Windows, where ChildProcess.kill() cannot deliver them. The stop-command test below runs everywhere. +const posixOnly = { skip: process.platform === 'win32' && 'process groups and catchable signals are POSIX-only' }; +const moduleUrl = JSON.stringify(pathToFileURL(resolve('scripts/test-lanes.ts')).href); +const sleep = (ms: number) => new Promise(r => setTimeout(r, ms)); +// The harness's own process group, found once: a recorded pid must never lead to a signal for it. If `ps` cannot +// run (no binary, no permission) the result has no stdout, and the group is simply unknown. +function lookupGroup(run: typeof spawnSync = spawnSync, platform: NodeJS.Platform = process.platform): number | undefined { + if (platform === 'win32') return undefined; + const group = Number(run('ps', ['-o', 'pgid=', '-p', String(process.pid)], { encoding: 'utf8' })?.stdout?.trim()); + return Number.isSafeInteger(group) && group > 1 ? group : undefined; +} +const ownGroup = lookupGroup(); +/** A pid read back from a fixture file, or undefined for anything that is not a whole number above 1 that is + * neither the harness, its parent nor its group: an empty file is Number('') === 0, which is the whole group. */ +function safePid(text: string): number | undefined { + if (!/^[0-9]+$/.test(text.trim())) return undefined; + const pid = Number(text.trim()); + return Number.isSafeInteger(pid) && pid > 1 && pid !== process.pid && pid !== process.ppid && pid !== ownGroup ? pid : undefined; +} +type Send = (pid: number, signal: NodeJS.Signals) => void; +type Command = (command: string, args: string[]) => void; +const runCommand: Command = (command, args) => { spawnSync(command, args, { stdio: 'ignore' }); }; +/** Stop a fixture process and, on POSIX, the group it leads. With the harness's own group unknown no group signal is + * sent at all, only one to the validated pid itself: it could otherwise be the harness's group. */ +function killTree(pid: number | undefined, harness: { group: number | undefined } = { group: ownGroup }, send: Send = (target, signal) => { process.kill(target, signal); }, platform: NodeJS.Platform = process.platform, command: Command = runCommand) { + if (pid === undefined || safePid(String(pid)) === undefined) return; + if (platform === 'win32') { command('taskkill', ['/T', '/F', '/PID', String(pid)]); return; } + if (harness.group !== undefined && pid !== harness.group) { try { send(-pid, 'SIGKILL'); } catch { /* not a group leader */ } } + try { send(pid, 'SIGKILL'); } catch { /* gone */ } +} +const launched = new Set(); +after(() => { for (const child of launched) if (child.pid) killTree(child.pid); }); +const alive = (pid: number) => { try { process.kill(pid, 0); return true; } catch { return false; } }; +async function until(condition: () => boolean, ms = 10_000) { + for (const end = Date.now() + ms; Date.now() < end;) { if (condition()) return true; await sleep(25); } + return condition(); +} + +/** One disposable folder holding a step program whose behaviour comes from its environment: + * `ignore` ignores SIGTERM, `hang` runs for ever, `life=N` runs N ms, `code=N` exits with N, + * `grandchild` starts a same-group child that ignores SIGTERM and hangs, `session=...` starts a + * descendant in its own session (outside the step's group) that holds the step's pipes with the + * given modes, `print` and `flood` write output. Each process records its pid under ROOT. */ +function fixture() { + const root = mkdtempSync(join(tmpdir(), 'portable-lanes-')); + writeFileSync(join(root, 'child.cjs'), ` +const fs = require('node:fs'), cp = require('node:child_process'); +const { ROOT, NAME, MODE = '' } = process.env; +const modes = MODE.split(','); +const value = key => (modes.find(m => m.startsWith(key + '=')) ?? '').split('=').slice(1).join('=') || undefined; +if (modes.includes('ignore')) process.on('SIGTERM', () => {}); +fs.writeFileSync(ROOT + '/' + NAME + '.pid.tmp', String(process.pid)); +fs.renameSync(ROOT + '/' + NAME + '.pid.tmp', ROOT + '/' + NAME + '.pid'); +const again = (suffix, mode, options = {}) => cp.spawn(process.execPath, [__filename], { env: { ...process.env, NAME: NAME + '-' + suffix, MODE: mode }, stdio: ['ignore', 'inherit', 'inherit'], ...options }); +if (modes.includes('grandchild')) again('grandchild', 'ignore,hang'); +if (value('session')) again('session', value('session').replaceAll('+', ','), { detached: true }); +if (modes.includes('print')) console.log('PRINTED-BY-' + NAME); +if (modes.includes('flood')) console.log('x'.repeat(5000) + 'END'); +if (modes.includes('hang')) setInterval(() => {}, 1000); +if (value('life')) setTimeout(() => process.exit(Number(value('code') ?? 0)), Number(value('life'))); +else if (!modes.includes('hang')) process.exit(Number(value('code') ?? 0)); +`); + const step = (label: string, mode: string) => + `{label:${JSON.stringify(label)},command:process.execPath,args:[${JSON.stringify(join(root, 'child.cjs'))}],env:{ROOT:${JSON.stringify(root)},NAME:${JSON.stringify(label)},MODE:${JSON.stringify(mode)}}}`; + const pid = (name: string) => (existsSync(join(root, name + '.pid')) ? safePid(readFileSync(join(root, name + '.pid'), 'utf8')) : undefined); + // Every fixture process records its pid here: stop whatever a failed test left running, then delete the files. + const dispose = () => { + if (!existsSync(root)) return; + for (const name of readdirSync(root)) if (name.endsWith('.pid')) killTree(safePid(readFileSync(join(root, name), 'utf8'))); + rmSync(root, { recursive: true, force: true }); + }; + return { root, step, pid, dispose }; +} + +function launch(script: string, env: Record = {}) { + const child = spawn(process.execPath, ['--experimental-strip-types', script], { env: { ...process.env, GITHUB_ACTIONS: '', ...env }, stdio: ['ignore', 'pipe', 'pipe'] }); + launched.add(child); + let out = ''; + child.stdout.on('data', c => { out += c; }); + child.stderr.on('data', c => { out += c; }); + const exited = new Promise(done => child.on('close', code => done(code))); + return { child, output: () => out, exited }; +} + +/** A runner script. `${steps}` is written as real code so each test lists its lanes plainly. */ +function program(f: ReturnType, lanesCode: (s: (label: string, mode: string) => string) => string, options = '{}') { + const file = join(f.root, 'run.ts'); + writeFileSync(file, `import * as lanes from ${moduleUrl}; +try { await lanes.runLanes(${lanesCode(f.step)}, ${options}); console.log('RUN-OK'); } +catch (error) { console.log('REJECTED ' + (error as Error).message); process.exit((error as { exitCode?: number }).exitCode ?? 1); }`); + return file; +} +const bounded = async (promise: Promise, ms: number) => Promise.race([promise, sleep(ms).then(() => 'hung' as const)]); + +for (const [signal, code] of [['SIGINT', 130], ['SIGTERM', 143], ['SIGHUP', 129]] as const) { + test(`${signal} stops every step, keeps what they wrote and exits ${code}`, posixOnly, async () => { + const f = fixture(); + try { + const run = launch(program(f, s => `[{name:'a',steps:[${s('one', 'print,hang')}]},{name:'b',steps:[${s('two', 'print,hang')}]}]`)); + assert.ok(await until(() => f.pid('one') !== undefined && f.pid('two') !== undefined), 'both steps started'); + await sleep(300); + const pids = [f.pid('one')!, f.pid('two')!]; + run.child.kill(signal); + assert.equal(await bounded(run.exited, 15_000), code, run.output()); + assert.match(run.output(), new RegExp(`REJECTED Portable check interrupted by ${signal}`)); + assert.match(run.output(), /PRINTED-BY-one/); assert.match(run.output(), /PRINTED-BY-two/); + assert.match(run.output(), /Interrupted before it ended/); + for (const pid of pids) assert.ok(await until(() => !alive(pid), 5000), `step ${pid} outlives the runner`); + } finally { f.dispose(); } + }); +} + +test('a grandchild that ignores SIGTERM and a descendant that holds the pipes from its own session do not hold the runner', posixOnly, async () => { + const f = fixture(); + let session: number | undefined; + try { + const run = launch(program(f, s => `[{name:'a',steps:[${s('one', 'ignore,hang,grandchild,session=hang')}]}]`, '{killAfterMs:1500}')); + assert.ok(await until(() => ['one', 'one-grandchild', 'one-session'].every(n => f.pid(n) !== undefined)), 'processes started'); + await sleep(300); + session = f.pid('one-session'); + const group = [f.pid('one')!, f.pid('one-grandchild')!]; + const started = Date.now(); + run.child.kill('SIGTERM'); + assert.equal(await bounded(run.exited, 15_000), 143, run.output()); + assert.ok(Date.now() - started < 12_000, 'bounded exit'); + for (const pid of group) assert.ok(await until(() => !alive(pid), 5000), `${pid} outlives the runner`); + } finally { if (session && alive(session)) process.kill(session, 'SIGKILL'); f.dispose(); } +}); + +test('a leader that exits 0 leaving a same-group descendant on the pipes does not leave the descendant running when the runner returns', posixOnly, async () => { + const f = fixture(); + try { + // The leader exits at once; its grandchild (same group, ignores SIGTERM) holds the pipes. The grace period + // abandons the pipes, and the step passes, but the group is still alive. + const run = launch(program(f, s => `[{name:'a',steps:[${s('one', 'grandchild')}]}]`, '{graceMs:400,sweepMs:600}')); + assert.ok(await until(() => f.pid('one-grandchild') !== undefined)); + const descendant = f.pid('one-grandchild')!; + assert.equal(await bounded(run.exited, 15_000), 0, run.output()); + assert.match(run.output(), /^passed a: one/m); + assert.ok(await until(() => !alive(descendant), 3000), 'the descendant outlives a passing run'); + } finally { f.dispose(); } +}); + +test('an interrupt during the grace period, which ends before the kill timer, still stops the descendant of a leader that already exited', posixOnly, async () => { + const f = fixture(); + try { + const run = launch(program(f, s => `[{name:'a',steps:[${s('one', 'grandchild')}]}]`, '{graceMs:1500,killAfterMs:30000,sweepMs:600}')); + assert.ok(await until(() => f.pid('one-grandchild') !== undefined)); + const descendant = f.pid('one-grandchild')!; + await sleep(300); + assert.ok(alive(descendant), 'the grace period is still running'); + run.child.kill('SIGTERM'); + assert.equal(await bounded(run.exited, 15_000), 143, run.output()); + assert.ok(await until(() => !alive(descendant), 3000), 'the descendant outlives the interrupted run'); + } finally { f.dispose(); } +}); + +test('a repeated SIGINT is not swallowed: it kills steps that ignore the first one', posixOnly, async () => { + const f = fixture(); + try { + const run = launch(program(f, s => `[{name:'a',steps:[${s('one', 'ignore,hang,grandchild')}]}]`, '{killAfterMs:60000}')); + assert.ok(await until(() => f.pid('one') !== undefined && f.pid('one-grandchild') !== undefined)); + await sleep(300); + const pids = [f.pid('one')!, f.pid('one-grandchild')!]; + run.child.kill('SIGINT'); + await sleep(400); + assert.ok(alive(pids[0]), 'the first signal alone does not stop a step that ignores it'); + run.child.kill('SIGINT'); + assert.equal(await bounded(run.exited, 10_000), 130, run.output()); + for (const pid of pids) assert.ok(await until(() => !alive(pid), 5000), `${pid} outlives the runner`); + } finally { f.dispose(); } +}); + +test('a closed output pipe stops the steps instead of orphaning them', posixOnly, async () => { + const f = fixture(); + try { + const run = launch(program(f, s => `[{name:'a',steps:[${s('one', 'ignore,hang')}]}]`, '{heartbeatMs:100,killAfterMs:1000}')); + assert.ok(await until(() => f.pid('one') !== undefined)); + const pid = f.pid('one')!; + run.child.stdout!.destroy(); + assert.notEqual(await bounded(run.exited, 15_000), 'hung', 'the runner exits'); + assert.ok(await until(() => !alive(pid), 5000), 'the step outlives the runner'); + } finally { f.dispose(); } +}); + +test('a crash of the runner still kills every group it started', posixOnly, async () => { + const f = fixture(); + try { + const file = join(f.root, 'crash.ts'); + writeFileSync(file, `import {runLanes} from ${moduleUrl}; +const pending = runLanes([{name:'a',steps:[${f.step('one', 'ignore,hang')}]}]).catch(() => {}); +setTimeout(() => { throw new Error('boom'); }, 600); +await pending;`); + const run = launch(file); + assert.ok(await until(() => f.pid('one') !== undefined)); + const pid = f.pid('one')!; + assert.notEqual(await bounded(run.exited, 15_000), 0); + assert.ok(await until(() => !alive(pid), 5000), 'the step outlives the crashed runner'); + } finally { f.dispose(); } +}); + +test('a step that cannot start is reported once', async () => { + const f = fixture(); + try { + const run = launch(program(f, () => `[{name:'a',steps:[{label:'missing',command:'definitely-not-a-command-xyz',args:[]}]}]`)); + assert.equal(await bounded(run.exited, 15_000), 1, run.output()); + const rows = run.output().split('\n').filter(l => /^(passed|failed|stopped) a: missing/.test(l)); + assert.equal(rows.length, 1, run.output()); + assert.match(rows[0], /^failed/); + assert.equal(run.output().split('\n').filter(l => /^\s+(passed|failed|stopped)\s+[\d.]+s\s+a: missing/.test(l)).length, 1); + assert.match(run.output(), /Could not start/); + } finally { f.dispose(); } +}); + +test('a step that fails on its own after the first failure is a failure with its output, not a stopped step', async () => { + const f = fixture(); + try { + // "early" has exited with 4 but a descendant in another session holds its pipes for 1.5 s, + // so its close arrives after "first" has failed. The runner never signalled it. + const run = launch(program(f, s => `[{name:'a',steps:[${s('first', 'life=700,code=3')}]},{name:'b',steps:[${s('early', 'print,code=4,session=life=1500')}]}]`)); + assert.equal(await bounded(run.exited, 15_000), 1, run.output()); + assert.match(run.output(), /^failed a: first/m); + assert.match(run.output(), /^failed b: early/m); + assert.match(run.output(), /PRINTED-BY-early/); + assert.match(run.output(), /^\s+failed\s+[\d.]+s\s+b: early/m); + } finally { f.dispose(); } +}); + +test('a step the runner stopped is "stopped" and prints nothing, even if it exits non-zero in reply', async () => { + const f = fixture(); + try { + const run = launch(program(f, s => `[{name:'a',steps:[${s('first', 'life=500,code=3')}]},{name:'b',steps:[${s('victim', 'print,hang')}]}]`)); + assert.equal(await bounded(run.exited, 15_000), 1, run.output()); + assert.match(run.output(), /^failed a: first/m); + assert.match(run.output(), /^stopped b: victim/m); + assert.doesNotMatch(run.output(), /PRINTED-BY-victim/); + } finally { f.dispose(); } +}); + +test('the runner names the steps still running about every heartbeat', async () => { + const f = fixture(); + try { + const run = launch(program(f, s => `[{name:'a',steps:[${s('slow', 'life=900')}]}]`, '{heartbeatMs:150}')); + assert.equal(await bounded(run.exited, 15_000), 0, run.output()); + assert.match(run.output(), /still running: a: slow \(\d+\.\ds\)/); + } finally { f.dispose(); } +}); + +test('a step keeps only the tail of its output and says what was dropped', async () => { + const f = fixture(); + try { + const run = launch(program(f, s => `[{name:'a',steps:[${s('noisy', 'flood,code=1')}]}]`, '{maxOutputBytes:1000}')); + assert.equal(await bounded(run.exited, 15_000), 1, run.output()); + assert.match(run.output(), /\[\d+ bytes of earlier output dropped\]/); + assert.match(run.output(), /xEND/); + assert.ok((run.output().match(/x/g) ?? []).length < 1500, 'the dropped bytes are not printed'); + } finally { f.dispose(); } +}); + +test('a pass whose leftover process holds the pipes is abandoned after the grace period, not waited for', posixOnly, async () => { + const f = fixture(); + let session: number | undefined; + try { + const run = launch(program(f, s => `[{name:'a',steps:[${s('one', 'print,session=hang')}]}]`, '{graceMs:500}')); + assert.ok(await until(() => f.pid('one-session') !== undefined)); + session = f.pid('one-session'); + assert.equal(await bounded(run.exited, 10_000), 0, run.output()); + assert.match(run.output(), /^passed a: one/m); + assert.match(run.output(), /held its output open/); + } finally { if (session && alive(session)) process.kill(session, 'SIGKILL'); f.dispose(); } +}); + +test('a failed lookup of the harness group leaves it unknown, the module still loads, and cleanup then sends no group signal', () => { + const failed = (() => ({ pid: 0, output: null, stdout: null, stderr: null, status: null, signal: null, error: Object.assign(new Error('spawn ps ENOENT'), { code: 'ENOENT' }) })) as unknown as typeof spawnSync; + assert.equal(lookupGroup(failed, 'linux'), undefined); + assert.equal(lookupGroup((() => undefined) as unknown as typeof spawnSync, 'linux'), undefined); + assert.equal(lookupGroup((() => ({ stdout: '' })) as unknown as typeof spawnSync, 'linux'), undefined, 'an empty answer is not group 0'); + assert.equal(lookupGroup((() => ({ stdout: ' 4321\n' })) as unknown as typeof spawnSync, 'linux'), 4321); + assert.equal(lookupGroup(failed, 'win32'), undefined); + // Every call below names its platform and injects both the signal and the command: none can reach a real kill or taskkill. + const noCommand: Command = (name) => { assert.fail(`${name} must not run on this platform`); }; + const noSignal: Send = () => { assert.fail('no signal may be sent on this platform'); }; + const sent: number[] = []; + killTree(4242, { group: undefined }, pid => { sent.push(pid); }, 'linux', noCommand); + assert.deepEqual(sent, [4242], 'only the validated pid itself, never -4242'); + killTree(0, { group: undefined }, pid => { sent.push(pid); }, 'linux', noCommand); killTree(process.pid, { group: undefined }, pid => { sent.push(pid); }, 'linux', noCommand); + assert.deepEqual(sent, [4242], 'an unsafe pid is not signalled at all'); + const known: number[] = []; + killTree(4242, { group: 777 }, pid => { known.push(pid); }, 'linux', noCommand); + assert.deepEqual(known, [-4242, 4242], 'with the group known a leader is stopped as a group too'); + killTree(777, { group: 777 }, pid => { known.push(pid); }, 'linux', noCommand); + assert.deepEqual(known, [-4242, 4242, 777], 'but never the harness group itself'); + const commands: [string, string[]][] = []; + killTree(4242, { group: 777 }, noSignal, 'win32', (name, args) => { commands.push([name, args]); }); + assert.deepEqual(commands, [['taskkill', ['/T', '/F', '/PID', '4242']]], 'on Windows a safe pid goes to taskkill, through the injected runner only'); + killTree(0, { group: undefined }, noSignal, 'win32', (name, args) => { commands.push([name, args]); }); + assert.equal(commands.length, 1, 'and an unsafe pid to nothing'); +}); + +test('an empty or garbage pid file is ignored by the fixture cleanup and the harness survives', async () => { + const f = fixture(); + const sleeper = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore', detached: process.platform !== 'win32' }); + try { + for (const [name, text] of Object.entries({ empty: '', blank: ' \n', zero: '0', init: '1', word: 'abc', negative: '-5', fraction: '2.5', harness: String(process.pid), parent: String(process.ppid) })) + writeFileSync(join(f.root, `${name}.pid`), text); + writeFileSync(join(f.root, 'real.pid'), String(sleeper.pid)); + for (const text of ['', ' ', '0', '1', 'abc', '-5', '2.5', String(process.pid), String(process.ppid)]) assert.equal(safePid(text), undefined, JSON.stringify(text)); + assert.equal(f.pid('empty'), undefined, 'a half-written file is not pid 0'); + f.dispose(); + assert.ok(await until(() => !alive(sleeper.pid!), 5000), 'the recorded fixture process is stopped'); + assert.ok(alive(process.pid), 'the harness is still here'); + assert.ok(alive(process.ppid), 'so is its parent'); + } finally { killTree(sleeper.pid); f.dispose(); } +}); + +test('the runner signals only a whole number above 1 that is not itself or its parent, and Windows stops only a child that has not exited', () => { + for (const bad of [0, 1, -1, -3, 1.5, NaN, Infinity, '12', null, undefined, process.pid, process.ppid]) assert.equal(lanes.validPid(bad), false, String(bad)); + assert.equal(lanes.validPid(4242), true); + assert.equal(lanes.stopAction('win32', true, 4242, false), undefined, 'an exited PID may belong to someone else now'); + assert.deepEqual(lanes.stopAction('win32', false, 4242, true), { kind: 'command', command: 'taskkill', args: ['/T', '/F', '/PID', '4242'] }); + assert.deepEqual(lanes.stopAction('linux', true, 4242, false), { kind: 'group', group: -4242, signal: 'SIGTERM' }, 'POSIX groups are probed before any signal, not skipped here'); + assert.equal(lanes.stopAction('linux', false, 0, false), undefined); + assert.equal(lanes.stopAction('win32', false, process.pid, true), undefined); +}); + +test('a group is retired as soon as its step is swept and nothing is sent to it afterwards, even if its id is reused', posixOnly, async () => { + const f = fixture(); + try { + const file = join(f.root, 'retire.ts'); + writeFileSync(file, `import * as lanes from ${moduleUrl}; +const calls: string[] = []; +const retired = new Set(); +// Once a group answered ESRCH it is retired; any later call to that id stands for an unrelated process that reused it. +const kill = (pid: number, signal: NodeJS.Signals | 0) => { + const group = Math.abs(pid); + if (retired.has(group)) { calls.push(group + ' AFTER-RETIRED ' + signal); return; } + try { process.kill(pid, signal); calls.push(group + ' ' + signal + ' ok'); } + catch (error) { const code = (error as NodeJS.ErrnoException).code; calls.push(group + ' ' + signal + ' ' + code); if (code === 'ESRCH') retired.add(group); throw error; } +}; +await lanes.runLanes([{name:'a',steps:[${f.step('one', 'code=0')},${f.step('two', 'grandchild')}]}], {graceMs:300,sweepMs:800,kill}); +console.log('CALLS ' + JSON.stringify(calls));`); + const run = launch(file); + assert.equal(await bounded(run.exited, 20_000), 0, run.output()); + const calls: string[] = JSON.parse(/CALLS (.*)/.exec(run.output())?.[1] ?? '[]'); + const one = f.pid('one')!, two = f.pid('two')!; + const of = (group: number) => calls.filter(c => c.startsWith(group + ' ')); + assert.ok(calls.length > 0, 'the runner reports its probes and signals through the injected function'); + assert.equal(calls.filter(c => c.includes('AFTER-RETIRED')).length, 0, calls.join('; ')); + assert.deepEqual(of(one), [`${one} 0 ESRCH`], 'an empty group is probed once and retired without a signal'); + assert.ok(of(two).some(c => c.endsWith('SIGTERM ok')) && of(two).some(c => c.endsWith('SIGKILL ok')), 'a stubborn descendant gets TERM then KILL'); + assert.equal(of(two).at(-1), `${two} 0 ESRCH`, 'and the group is confirmed empty'); + assert.ok(calls.findIndex(c => c.startsWith(two + ' ')) > calls.findIndex(c => c.startsWith(one + ' ')) , 'the first step is swept before the next one starts'); + for (const c of calls) assert.ok(Number(c.split(' ')[0]) > 1, c); + } finally { f.dispose(); } +}); + +test('steps are stopped with a process group on POSIX and with taskkill /T /F on Windows, and only POSIX detaches them', () => { + assert.deepEqual(lanes.stopPlan('linux', 4242, false), { kind: 'group', group: -4242, signal: 'SIGTERM' }); + assert.deepEqual(lanes.stopPlan('darwin', 4242, true), { kind: 'group', group: -4242, signal: 'SIGKILL' }); + for (const force of [false, true]) assert.deepEqual(lanes.stopPlan('win32', 4242, force), { kind: 'command', command: 'taskkill', args: ['/T', '/F', '/PID', '4242'] }); + assert.equal(lanes.detachesSteps('linux'), true); + assert.equal(lanes.detachesSteps('darwin'), true); + assert.equal(lanes.detachesSteps('win32'), false); +}); + +test('lane runner starts a yielding step at lower priority and an ordinary one at the usual priority', posixOnly, async () => { + const f = fixture(); + try { + const file = join(f.root, 'priority.ts'); + writeFileSync(file, `import {runLanes} from ${moduleUrl}; +const show=(label:string,lowPriority?:boolean)=>({label,command:process.execPath,args:['-e',"console.log('PRIORITY '+require('node:os').getPriority())"],lowPriority}); +await runLanes([{name:'a',steps:[show('usual')]},{name:'b',steps:[show('yielding',true)]}]);`); + const run = launch(file); + assert.equal(await bounded(run.exited, 20_000), 0, run.output()); + const seen = (label: string) => Number(new RegExp(`=== ${label} ===\\nPRIORITY (-?\\d+)`).exec(run.output())?.[1]); + assert.ok(seen('b: yielding') > seen('a: usual'), 'the yielding step has the larger niceness'); + } finally { f.dispose(); } +}); + +test('lane runner stops every lane at the first failure and reports it without interleaving', async () => { + const f = fixture(); + try { + const file = join(f.root, 'first-failure.ts'); + writeFileSync(file, `import {runLanes} from ${moduleUrl}; +const node=(label:string,code:string)=>({label,command:process.execPath,args:['-e',code]}); +try{await runLanes([ + {name:'slow',steps:[node('hangs',"setTimeout(()=>{},60000)"),node('never starts',"console.log('LATE-STEP')")]}, + {name:'fails',steps:[node('breaks',"console.log('FAIL-OUTPUT');process.exit(3)")]}, + {name:'ok',steps:[node('passes',"console.log('PASS-OUTPUT')"),node('skipped after failure',"setTimeout(()=>console.log('LATE-STEP'),5000)")]}, +]);}catch(error){console.log('REJECTED '+(error as Error).message);process.exit(1);}`); + const started = performance.now(); + const run = launch(file); + assert.equal(await bounded(run.exited, 20_000), 1); + assert.ok(performance.now() - started < 15_000, 'the hanging lane is stopped, not waited for'); + assert.match(run.output(), /FAIL-OUTPUT/); assert.match(run.output(), /Exited with code 3/); assert.match(run.output(), /REJECTED Portable check failed/); + assert.doesNotMatch(run.output(), /LATE-STEP/); + assert.match(run.output(), /stopped\s+[\d.]+s\s+slow: hangs/); + assert.match(run.output(), /failed\s+[\d.]+s\s+fails: breaks/); + } finally { f.dispose(); } +}); diff --git a/tests/automation/workflow-triggers.test.ts b/tests/automation/workflow-triggers.test.ts index ca4fd91..6756787 100644 --- a/tests/automation/workflow-triggers.test.ts +++ b/tests/automation/workflow-triggers.test.ts @@ -197,7 +197,18 @@ test('Windows selects fast PR/push checks and full release, weekly and manual jo const windows = source.split('\n windows-native:\n')[1].split('\n publish:\n')[0]; assert.ok(windows.includes("AP_WINDOWS_FULL: ${{ needs.gate.outputs.windows_full == 'true' && '1' || '0' }}")); assert.match(windows, /name: Fetch released Windows migration fixture\n if: env.AP_WINDOWS_FULL == '1'\n run: git fetch --no-tags origin tag v1\.1\.1/); - assert.match(windows, /--test-concurrency=1 tests\/backend\/windows-platform\.test\.ts tests\/backend\/windows-service\.test\.ts/); + assert.match(windows, /\$runs = @\('windows-platform', 'windows-service'\)/); + assert.match(windows, /--test-concurrency=1 tests\/backend\/\$\(\$run\.File\)\.test\.ts/); + // Each file runs under Start-Process -Wait, which waits for the whole process tree, inside its own thread job. + assert.match(windows, /Start-ThreadJob[^\n]*-ScriptBlock \{/); + assert.match(windows, /-WindowStyle Hidden -Wait -PassThru -RedirectStandardOutput \$run\.Output -RedirectStandardError \$run\.Errors -ErrorAction Stop/); + // The secondary logon service refuses a second simultaneous start once; only that refusal is retried. + assert.match(windows, /\$_\.Exception\.Message -notmatch 'already running'\) \{ throw \}/); + assert.match(windows, /\$attempt -ge 30/); + assert.ok(!windows.includes('WaitForExit'), 'a bare process wait would not wait for what the process started'); + assert.match(windows, /\$null = \$runs\.Job \| Wait-Job/); + assert.match(windows, /Receive-Job -Job \$run\.Job -ErrorAction Stop/); + assert.match(windows, /Where-Object \{ \$_\.ExitCode -ne 0 \}/); const root = mkdtempSync(join(tmpdir(), 'workflow-windows-')); const git = (...args: string[]) => { diff --git a/tsconfig.json b/tsconfig.json index a1645ee..8e75031 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,5 +10,5 @@ "types": ["node"], "lib": ["ES2023", "DOM"] }, - "include": ["shared/**/*.ts", "plugin/**/*.ts", "service/**/*.ts", "setup/**/*.ts", "tests/backend/**/*.ts", "tests/fixtures/**/*.ts", "scripts/cli-probe.ts", "scripts/backend-evidence.ts", "scripts/package*.ts", "scripts/verify*package.ts", "scripts/ci.ts", "scripts/release.ts", "tests/automation/**/*.ts"] + "include": ["shared/**/*.ts", "plugin/**/*.ts", "service/**/*.ts", "setup/**/*.ts", "tests/backend/**/*.ts", "tests/fixtures/**/*.ts", "scripts/cli-probe.ts", "scripts/backend-evidence.ts", "scripts/package*.ts", "scripts/verify*package.ts", "scripts/ci.ts", "scripts/test*.ts", "scripts/release.ts", "tests/automation/**/*.ts"] }