mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-21 02:54:25 +00:00
Compare commits
51 Commits
fix/codeql
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
01856b3b10 | ||
|
|
8dd7157b8c | ||
|
|
bbf2ced076 | ||
|
|
c68f193075 | ||
|
|
3833e42097 | ||
|
|
cf8baca2eb | ||
|
|
24e8074019 | ||
|
|
ded34718eb | ||
|
|
c8c3c306b5 | ||
|
|
fe99346fd5 | ||
|
|
786f6b2318 | ||
|
|
2c886cdf05 | ||
|
|
a9e2ba3459 | ||
|
|
8ce4563356 | ||
|
|
cd90930aab | ||
|
|
1b5dc9b650 | ||
|
|
f8ba9806fc | ||
|
|
a6a6f0ca34 | ||
|
|
442a7c8da2 | ||
|
|
72e9012e92 | ||
|
|
e051b9fd66 | ||
|
|
29dcc9115a | ||
|
|
e64c56e67a | ||
|
|
0fac8c6c7a | ||
|
|
89986ee091 | ||
|
|
700fcb22f2 | ||
|
|
66da83b5af | ||
|
|
51714c011f | ||
|
|
50ad62a9ad | ||
|
|
5270afefcd | ||
|
|
04264cea50 | ||
|
|
7dc9e0c806 | ||
|
|
2a8675603b | ||
|
|
6911667c26 | ||
|
|
9b4ca56549 | ||
|
|
1068297dc2 | ||
|
|
761ca3ec7a | ||
|
|
bdda4c2e0f | ||
|
|
ef66a01c52 | ||
|
|
a1d3298bb8 | ||
|
|
1339e147b2 | ||
|
|
e97f95598d | ||
|
|
e66081afa8 | ||
|
|
fed5dbe257 | ||
|
|
251aa07308 | ||
|
|
98023ecaf9 | ||
|
|
a86a33f7fb | ||
|
|
f51e8cbdb3 | ||
|
|
d28c7ccd0a | ||
|
|
0c02884f2a | ||
|
|
fa5449e69d |
2
.github/workflows/api-route-typecheck.yml
vendored
2
.github/workflows/api-route-typecheck.yml
vendored
@@ -25,7 +25,7 @@ jobs:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
|
||||
with:
|
||||
node-version: "24"
|
||||
cache: npm
|
||||
|
||||
74
.github/workflows/ci.yml
vendored
74
.github/workflows/ci.yml
vendored
@@ -38,6 +38,9 @@ env:
|
||||
# system TLS). Belt-and-suspenders with tests/_setup/isolateDataDir.ts.
|
||||
OMNIROUTE_SKIP_SYSTEM_TRUST: "1"
|
||||
CI_NODE_VERSION: "24"
|
||||
# The V8 manifests require the producer and aggregator to have the same Node
|
||||
# patch. A floating major can resolve to different patch versions on runner caches.
|
||||
CI_V8_NODE_VERSION: "24.22.3"
|
||||
CI_NODE_24_VERSION: "24"
|
||||
CI_NODE_26_VERSION: "26"
|
||||
|
||||
@@ -1172,7 +1175,7 @@ jobs:
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
|
||||
with:
|
||||
package-manager-cache: false
|
||||
node-version: ${{ env.CI_NODE_VERSION }}
|
||||
node-version: ${{ env.CI_V8_NODE_VERSION }}
|
||||
- uses: ./.github/actions/prepare-unit-storage
|
||||
with:
|
||||
cache: ${{ github.event_name == 'pull_request' }}
|
||||
@@ -1192,6 +1195,7 @@ jobs:
|
||||
- name: Unit tests (shard ${{ matrix.shard }}/8) with V8 coverage
|
||||
env:
|
||||
TEST_SHARD: ${{ matrix.shard }}/8
|
||||
TMPDIR: ${{ runner.temp }}/v8-tsx-${{ github.run_id }}-${{ github.run_attempt }}-${{ matrix.shard }}
|
||||
# NODE_OPTIONS (nao so o flag em test:unit:ci:shard) porque quem estoura o
|
||||
# heap e o processo `c8` que embrulha a suite — ele agrega ~577 MB de JSON
|
||||
# de cobertura bruta. Subir o teto so no node filho deixa o pai no default
|
||||
@@ -1201,6 +1205,12 @@ jobs:
|
||||
run: |
|
||||
trap 'status=$?; { df -h "$GITHUB_WORKSPACE" "$RUNNER_TEMP"; du -sh coverage-shard; } || :; exit "$status"' EXIT
|
||||
rm -rf coverage-shard coverage-shard-report
|
||||
# Capture the transforms this producer actually executes; a reused or
|
||||
# shared TSX cache cannot establish generated-layout provenance.
|
||||
node scripts/ci/prepare-v8-coverage.mjs init-cache "$TMPDIR" "$TEST_SHARD" "$GITHUB_SHA" "$GITHUB_RUN_ID" "$GITHUB_RUN_ATTEMPT" "$GITHUB_WORKSPACE"
|
||||
V8_CACHE_PRELOAD="$(node -p 'require("node:url").pathToFileURL(process.argv[1]).href' "$GITHUB_WORKSPACE/scripts/ci/atomic-tsx-cache.mjs")"
|
||||
V8_NATIVE_PRELOAD="$(node -p 'require("node:url").pathToFileURL(process.argv[1]).href' "$GITHUB_WORKSPACE/scripts/ci/native-worker-v8-layout.mjs")"
|
||||
OMNIROUTE_CI_SYNC_TSX_CACHE="$TMPDIR" NODE_OPTIONS="$NODE_OPTIONS --import=$V8_CACHE_PRELOAD --import=$V8_NATIVE_PRELOAD" \
|
||||
npx c8 --merge-async \
|
||||
--temp-directory=coverage-shard \
|
||||
--reports-dir=coverage-shard-report \
|
||||
@@ -1208,11 +1218,23 @@ jobs:
|
||||
--exclude=tests/** \
|
||||
--exclude=**/*.test.* \
|
||||
npm run test:unit:ci:shard
|
||||
- name: Upload raw shard coverage
|
||||
if: always()
|
||||
- name: Package successful shard coverage
|
||||
env:
|
||||
TEST_SHARD: ${{ matrix.shard }}/8
|
||||
TMPDIR: ${{ runner.temp }}/v8-tsx-${{ github.run_id }}-${{ github.run_attempt }}-${{ matrix.shard }}
|
||||
run: node scripts/ci/prepare-v8-coverage.mjs pack coverage-shard coverage-shard-upload "$TEST_SHARD" "$GITHUB_SHA" "$GITHUB_RUN_ID" "$GITHUB_RUN_ATTEMPT" "$GITHUB_WORKSPACE" "$TMPDIR"
|
||||
- name: Upload complete shard coverage
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
|
||||
with:
|
||||
name: coverage-shard-${{ matrix.shard }}
|
||||
name: coverage-shard-${{ github.run_attempt }}-${{ matrix.shard }}
|
||||
path: coverage-shard-upload/
|
||||
compression-level: 0
|
||||
if-no-files-found: error
|
||||
- name: Preserve raw coverage from failed shards
|
||||
if: failure()
|
||||
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
|
||||
with:
|
||||
name: failed-unit-coverage-${{ github.run_attempt }}-${{ matrix.shard }}
|
||||
path: coverage-shard/*.json
|
||||
if-no-files-found: error
|
||||
|
||||
@@ -1315,6 +1337,9 @@ jobs:
|
||||
# the release gate can still exercise them via workflow_dispatch when needed).
|
||||
test-coverage:
|
||||
name: Coverage
|
||||
permissions:
|
||||
contents: read
|
||||
actions: read
|
||||
runs-on: ubuntu-latest
|
||||
# 10min was sized before #7114 added the lcov reporter (Codecov/Sonar need it);
|
||||
# merging 8 shard JSONs + text+json+lcov now takes ~10-12min — three consecutive
|
||||
@@ -1354,21 +1379,27 @@ jobs:
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
|
||||
with:
|
||||
package-manager-cache: false
|
||||
node-version: ${{ env.CI_NODE_VERSION }}
|
||||
node-version: ${{ env.CI_V8_NODE_VERSION }}
|
||||
- uses: ./.github/actions/npm-ci-retry
|
||||
with:
|
||||
# PR acceleration is not release-candidate provenance.
|
||||
cache: ${{ github.event_name == 'pull_request' }}
|
||||
- name: Select coverage producers for this attempt
|
||||
id: coverage_selection
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ github.token }}
|
||||
run: |
|
||||
mkdir -p coverage
|
||||
node scripts/ci/select-v8-coverage.mjs coverage/coverage-selection.json 8
|
||||
- name: Download all shard coverage
|
||||
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8
|
||||
with:
|
||||
pattern: coverage-shard-*
|
||||
artifact-ids: ${{ steps.coverage_selection.outputs.artifact_ids }}
|
||||
path: coverage-shards/
|
||||
merge-multiple: true
|
||||
merge-multiple: false
|
||||
- name: Merge + report + gate
|
||||
# Merging 8 shards of raw v8 coverage is memory-heavy. `--merge-async`
|
||||
# keeps the V8 coverage merge incremental instead of loading every raw
|
||||
# JSON blob into one in-memory merge, which avoids Node heap OOMs.
|
||||
# Verify and merge each raw report in order without storing a second
|
||||
# raw copy. c8 reads the aggregate for source mapping and reporting.
|
||||
# lcov on a PR only when SonarQube runs (#20725). #18336 (2026-10-26) dropped it on
|
||||
# pull_request because its only PR consumer was the informational Codecov upload while
|
||||
# SonarQube was off; once vars.SONARQUBE_ENABLED came back the `sonarqube` job scanned
|
||||
@@ -1378,21 +1409,17 @@ jobs:
|
||||
NODE_OPTIONS: --max-old-space-size=8192
|
||||
EVENT_NAME: ${{ github.event_name }}
|
||||
SONARQUBE_ENABLED: ${{ vars.SONARQUBE_ENABLED }}
|
||||
RUN_HEAD_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
|
||||
run: |
|
||||
LCOV_REPORTER=()
|
||||
if [ "$EVENT_NAME" != "pull_request" ] || [ "$SONARQUBE_ENABLED" = "true" ]; then
|
||||
if [ "$EVENT_NAME" != "pull_request" ] || [ "${SONARQUBE_ENABLED:-}" = "true" ]; then
|
||||
LCOV_REPORTER=(--reporter=lcov)
|
||||
fi
|
||||
mkdir -p coverage
|
||||
first_coverage_file=""
|
||||
if [ -d coverage-shards ]; then
|
||||
first_coverage_file="$(find coverage-shards -maxdepth 1 -type f -name '*.json' -print -quit)"
|
||||
fi
|
||||
if [ -z "$first_coverage_file" ]; then
|
||||
echo "::error::No raw coverage shard data was downloaded."
|
||||
find . -maxdepth 3 -type f | sort
|
||||
exit 1
|
||||
fi
|
||||
# Retained producers keep their original attempts. The receipt binds all
|
||||
# eight job/artifact identities and the aggregate sent to c8.
|
||||
node scripts/ci/prepare-v8-coverage.mjs prepare coverage-shards coverage-shards-verified 8 "$GITHUB_SHA" "$GITHUB_RUN_ID" "$GITHUB_RUN_ATTEMPT" "$GITHUB_WORKSPACE" coverage/coverage-selection.json "$RUN_HEAD_SHA" "$GITHUB_REPOSITORY" > coverage/coverage-preparation.json
|
||||
cat coverage/coverage-preparation.json
|
||||
# Gate aligned to the project's local coverage bar: `npm run test:coverage`
|
||||
# gates at 60/60/60/60, so CI must match it (the previous CI floor of 40
|
||||
# silently undershot the local bar — a real drift). Real merged coverage is
|
||||
@@ -1400,7 +1427,7 @@ jobs:
|
||||
# coverage ratchet (quality-baseline.json) layers "must not drop vs baseline"
|
||||
# on top of this floor.
|
||||
npx c8 report \
|
||||
--temp-directory coverage-shards \
|
||||
--temp-directory coverage-shards-verified \
|
||||
--reports-dir coverage \
|
||||
--merge-async \
|
||||
--reporter=text-summary \
|
||||
@@ -1410,6 +1437,7 @@ jobs:
|
||||
--exclude=**/*.test.* \
|
||||
--check-coverage \
|
||||
--statements 60 --lines 60 --functions 60 --branches 60
|
||||
node scripts/ci/prepare-v8-coverage.mjs verify-report coverage
|
||||
- name: Build coverage summary
|
||||
if: always()
|
||||
run: |
|
||||
@@ -1460,6 +1488,8 @@ jobs:
|
||||
path: |
|
||||
coverage/coverage-summary.json
|
||||
coverage/coverage-report.md
|
||||
coverage/coverage-selection.json
|
||||
coverage/coverage-preparation.json
|
||||
coverage/lcov.info
|
||||
if-no-files-found: warn
|
||||
|
||||
@@ -1867,7 +1897,7 @@ jobs:
|
||||
with:
|
||||
ref: ${{ github.sha }}
|
||||
persist-credentials: false
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
|
||||
with:
|
||||
package-manager-cache: false
|
||||
node-version: ${{ env.CI_NODE_VERSION }}
|
||||
|
||||
2
.github/workflows/dast-smoke.yml
vendored
2
.github/workflows/dast-smoke.yml
vendored
@@ -41,7 +41,7 @@ jobs:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
with:
|
||||
node-version: "24"
|
||||
cache: npm
|
||||
|
||||
2
.github/workflows/i18n-mirror-refresh.yml
vendored
2
.github/workflows/i18n-mirror-refresh.yml
vendored
@@ -168,7 +168,7 @@ jobs:
|
||||
echo "refresh=true" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
if: steps.drift.outputs.refresh == 'true'
|
||||
with:
|
||||
package-manager-cache: false
|
||||
|
||||
2
.github/workflows/nightly-basepath-build.yml
vendored
2
.github/workflows/nightly-basepath-build.yml
vendored
@@ -76,7 +76,7 @@ jobs:
|
||||
with:
|
||||
ref: ${{ needs.resolve-branch.outputs.target }}
|
||||
persist-credentials: false
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
with:
|
||||
node-version: "24"
|
||||
package-manager-cache: false
|
||||
|
||||
2
.github/workflows/nightly-resilience.yml
vendored
2
.github/workflows/nightly-resilience.yml
vendored
@@ -133,7 +133,7 @@ jobs:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
with:
|
||||
node-version: "24"
|
||||
cache: npm
|
||||
|
||||
4
.github/workflows/nightly-v4-build.yml
vendored
4
.github/workflows/nightly-v4-build.yml
vendored
@@ -72,7 +72,7 @@ jobs:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
with:
|
||||
node-version: ${{ env.NIGHTLY_NODE_VERSION }}
|
||||
package-manager-cache: false
|
||||
@@ -146,7 +146,7 @@ jobs:
|
||||
path: .
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
with:
|
||||
node-version: ${{ env.NIGHTLY_NODE_VERSION }}
|
||||
registry-url: https://registry.npmjs.org
|
||||
|
||||
2
.github/workflows/omni-queue-watchdog.yml
vendored
2
.github/workflows/omni-queue-watchdog.yml
vendored
@@ -73,7 +73,7 @@ jobs:
|
||||
sparse-checkout: |
|
||||
scripts/merge-queue
|
||||
config/merge-queue
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
with:
|
||||
package-manager-cache: false
|
||||
node-version: "24"
|
||||
|
||||
4
.github/workflows/omni-queue.yml
vendored
4
.github/workflows/omni-queue.yml
vendored
@@ -126,7 +126,7 @@ jobs:
|
||||
sparse-checkout: |
|
||||
scripts/merge-queue
|
||||
config/merge-queue
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
with:
|
||||
package-manager-cache: false
|
||||
node-version: "24"
|
||||
@@ -210,7 +210,7 @@ jobs:
|
||||
sparse-checkout: |
|
||||
scripts/merge-queue
|
||||
config/merge-queue
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
with:
|
||||
package-manager-cache: false
|
||||
node-version: "24"
|
||||
|
||||
2
.github/workflows/preview-artifact.yml
vendored
2
.github/workflows/preview-artifact.yml
vendored
@@ -120,7 +120,7 @@ jobs:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
||||
with:
|
||||
node-version: ${{ env.PREVIEW_NODE_VERSION }}
|
||||
package-manager-cache: false
|
||||
|
||||
4
.github/workflows/quality.yml
vendored
4
.github/workflows/quality.yml
vendored
@@ -573,7 +573,7 @@ jobs:
|
||||
with:
|
||||
fetch-depth: 0
|
||||
persist-credentials: false
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v6
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v6
|
||||
with:
|
||||
package-manager-cache: false
|
||||
node-version: ${{ env.CI_NODE_VERSION }}
|
||||
@@ -613,7 +613,7 @@ jobs:
|
||||
with:
|
||||
ref: ${{ github.sha }}
|
||||
persist-credentials: false
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
|
||||
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
|
||||
with:
|
||||
package-manager-cache: false
|
||||
node-version: ${{ env.CI_NODE_VERSION }}
|
||||
|
||||
1
changelog.d/fixes/0000-v8-coverage-consumer-disk.md
Normal file
1
changelog.d/fixes/0000-v8-coverage-consumer-disk.md
Normal file
@@ -0,0 +1 @@
|
||||
- Merge verified coverage shards in their original reporting order and write a single aggregate for c8, reducing temporary disk use on CI runners.
|
||||
1
changelog.d/fixes/pending-v8-shard-evidence.md
Normal file
1
changelog.d/fixes/pending-v8-shard-evidence.md
Normal file
@@ -0,0 +1 @@
|
||||
- **fix(ci):** Preserve all eight V8 coverage shards with lossless transport, verified manifests and explicit checkout-path relocation before the native c8 report. Bind generated TSX code and source maps from exclusive producer caches before projecting a proven filename layout, and archive the preparation receipt. Support partial reruns by selecting successful producer jobs and immutable artifact IDs while retaining their original attempts. Reject incomplete, duplicate, corrupted or unproven evidence and empty coverage without changing coverage thresholds.
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"_comment": "Allowlist anti-slopsquatting (check-deps.mjs). Toda dep nova exige adicao EXPLICITA aqui apos verificar que e legitima.",
|
||||
"_justifications": {
|
||||
"@bcoe/v8-coverage": "V8 coverage report utilities (bcoe/v8-coverage), pinned at 1.0.2 for the development-only shard aggregation in PR #20515. The npm tarball and SHA-512 integrity match the existing transitive c8 dependency on both base branches.",
|
||||
"@msgpack/msgpack": "Official MessagePack JavaScript implementation (msgpack/msgpack-javascript, ISC), required by Fal's documented specialized realtime protocol. Version 3.1.3 name, repository, license and SHA-512 tarball integrity verified against the npm registry on 2026-11-03 for PR #18849; no additional runtime dependency is introduced by this correction.",
|
||||
"@agentclientprotocol/sdk": "Official Agent Client Protocol TypeScript SDK (agentclientprotocol/typescript-sdk, Apache-2.0). Exact 1.4.0 registry tarball and integrity verified for the registered native Gemini ACP launcher.",
|
||||
"@fontsource/ibm-plex-mono": "Self-hosted Keak type ramp for the vendored CI UI Kit (fontsource, OFL fonts; same no-CDN reasoning as Material Symbols #3695). KEA-7886 — CI UI Kit runtime dependency.",
|
||||
@@ -34,6 +35,7 @@
|
||||
"@atjsh/llmlingua-2",
|
||||
"@aws-sdk/client-bedrock-runtime",
|
||||
"@axe-core/playwright",
|
||||
"@bcoe/v8-coverage",
|
||||
"@cyclonedx/cyclonedx-npm",
|
||||
"@dnd-kit/core",
|
||||
"@dnd-kit/sortable",
|
||||
|
||||
1
package-lock.json
generated
1
package-lock.json
generated
@@ -127,6 +127,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@axe-core/playwright": "^4.13.0",
|
||||
"@bcoe/v8-coverage": "1.0.2",
|
||||
"@cyclonedx/cyclonedx-npm": "6.0.1",
|
||||
"@eslint/compat": "^2.1.1",
|
||||
"@playwright/test": "1.62.1",
|
||||
|
||||
@@ -460,6 +460,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@axe-core/playwright": "^4.13.0",
|
||||
"@bcoe/v8-coverage": "1.0.2",
|
||||
"@cyclonedx/cyclonedx-npm": "6.0.1",
|
||||
"@eslint/compat": "^2.1.1",
|
||||
"@playwright/test": "1.62.1",
|
||||
|
||||
@@ -383,6 +383,20 @@ export const CODE_SCAN_ROOTS = [
|
||||
|
||||
const CODE_ENV_REF = /process\.env\.([A-Z][A-Z0-9_]+)/g;
|
||||
|
||||
// Coverage inputs are scoped to the files that implement the producer protocol.
|
||||
const CI_COVERAGE_INPUTS = new Map([
|
||||
[
|
||||
path.join("scripts", "ci", "atomic-tsx-cache.mjs"),
|
||||
new Set(["OMNIROUTE_CI_SYNC_TSX_CACHE", "TMPDIR"]),
|
||||
],
|
||||
[path.join("scripts", "ci", "native-worker-v8-layout.mjs"), new Set(["NODE_V8_COVERAGE"])],
|
||||
[
|
||||
path.join("scripts", "check", "migration-commutation.ts"),
|
||||
new Set(["NODE_V8_COVERAGE", "OMNIROUTE_CI_SYNC_TSX_CACHE"]),
|
||||
],
|
||||
[path.join("scripts", "perf", "messages-route-memory-profile.ts"), new Set(["NODE_V8_COVERAGE"])],
|
||||
]);
|
||||
|
||||
/**
|
||||
* Collect environment variable references in source code.
|
||||
*
|
||||
@@ -409,7 +423,11 @@ export function scanCodeVars({ cwd, roots = CODE_SCAN_ROOTS } = {}) {
|
||||
return;
|
||||
}
|
||||
if (text.includes("\0")) return;
|
||||
for (const m of text.matchAll(CODE_ENV_REF)) vars.add(m[1]);
|
||||
const coverageInputs = CI_COVERAGE_INPUTS.get(path.relative(repoRoot, file));
|
||||
for (const match of text.matchAll(CODE_ENV_REF)) {
|
||||
const name = match[1];
|
||||
if (!coverageInputs?.has(name)) vars.add(name);
|
||||
}
|
||||
};
|
||||
const walk = (dir) => {
|
||||
let entries;
|
||||
|
||||
@@ -438,7 +438,13 @@ function bootOnce(
|
||||
outputFile: string
|
||||
): Promise<BootResult> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(process.execPath, ["--import", "tsx/esm", BOOT_SCRIPT, outputFile], {
|
||||
const args = ["--import", "tsx/esm", BOOT_SCRIPT, outputFile];
|
||||
// Node propagates V8 coverage to child processes. An instrumented CI boot
|
||||
// needs the repository's layout preload in its otherwise isolated setup.
|
||||
if (process.env.NODE_V8_COVERAGE && process.env.OMNIROUTE_CI_SYNC_TSX_CACHE) {
|
||||
args.unshift("--import", new URL("../ci/native-worker-v8-layout.mjs", import.meta.url).href);
|
||||
}
|
||||
const child = spawn(process.execPath, args, {
|
||||
cwd: REPO_ROOT,
|
||||
env: bootEnv(dataDir, homeDir, migrationsDir),
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
|
||||
45
scripts/ci/atomic-tsx-cache.mjs
Normal file
45
scripts/ci/atomic-tsx-cache.mjs
Normal file
@@ -0,0 +1,45 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import fs from "node:fs";
|
||||
import { register } from "node:module";
|
||||
import { userInfo } from "node:os";
|
||||
import { basename, dirname, join, resolve } from "node:path";
|
||||
import { isInternalThread } from "node:worker_threads";
|
||||
|
||||
const tsxImport = ["tsx/esm", "tsx"].some((specifier) =>
|
||||
process.execArgv.some(
|
||||
(arg, index, args) =>
|
||||
arg === `--import=${specifier}` || (arg === "--import" && args[index + 1] === specifier)
|
||||
)
|
||||
);
|
||||
const root = process.env.OMNIROUTE_CI_SYNC_TSX_CACHE;
|
||||
if (root && root === process.env.TMPDIR && (isInternalThread || tsxImport)) {
|
||||
const uid = process.geteuid ? process.geteuid() : userInfo().username;
|
||||
const cache = join(resolve(root), `tsx-${uid}`);
|
||||
const original = fs.promises.writeFile;
|
||||
|
||||
// The tsx disk cache is optional for execution, but here it is a coverage proof.
|
||||
// Commit only complete transforms before the test process can force its exit.
|
||||
fs.promises.writeFile = async function (file, data, ...options) {
|
||||
if (
|
||||
typeof file !== "string" ||
|
||||
dirname(file) !== cache ||
|
||||
!/^\d+-[a-f0-9]{40}$/.test(basename(file)) ||
|
||||
typeof data !== "string" ||
|
||||
options.length !== 0
|
||||
) {
|
||||
return Reflect.apply(original, this, [file, data, ...options]);
|
||||
}
|
||||
const partial = join(cache, `.${basename(file)}.${randomUUID()}.partial`);
|
||||
fs.writeFileSync(partial, data, { flag: "wx", mode: 0o600 });
|
||||
try {
|
||||
fs.renameSync(partial, file);
|
||||
} catch (error) {
|
||||
fs.unlinkSync(partial);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// Load this same bounded hook in the dedicated ESM loader before registering
|
||||
// tsx. The caller thread also needs the hook for tsx's CJS transform path.
|
||||
if (!isInternalThread) register(import.meta.url);
|
||||
}
|
||||
4
scripts/ci/native-cli-v8-tsx.mjs
Normal file
4
scripts/ci/native-cli-v8-tsx.mjs
Normal file
@@ -0,0 +1,4 @@
|
||||
import "tsx/esm";
|
||||
import { registerNativeCliCoverage } from "./native-worker-v8-layout.mjs";
|
||||
|
||||
registerNativeCliCoverage();
|
||||
108
scripts/ci/native-worker-v8-layout.mjs
Normal file
108
scripts/ci/native-worker-v8-layout.mjs
Normal file
@@ -0,0 +1,108 @@
|
||||
import { readFileSync, realpathSync } from "node:fs";
|
||||
import { createRequire, registerHooks } from "node:module";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { isInternalThread } from "node:worker_threads";
|
||||
|
||||
const cliEntry = new URL("../../bin/omniroute.mjs", import.meta.url);
|
||||
const cliTsxBridge = new URL("native-cli-v8-tsx.mjs", import.meta.url).href;
|
||||
const nativeModuleRoots = ["bin", "scripts"].map((directory) =>
|
||||
fileURLToPath(new URL(`../../${directory}/`, import.meta.url))
|
||||
);
|
||||
// Node loads these modules natively in workers, CLI diagnostics, MCP and catalog checks.
|
||||
const nativeSources = new Set(
|
||||
[
|
||||
"src/lib/usage/callLogArtifactWorker.ts",
|
||||
"src/lib/usage/callLogArtifacts.ts",
|
||||
"src/lib/dataPaths.ts",
|
||||
"src/lib/logEnv.ts",
|
||||
"src/lib/db/booleanSetting.ts",
|
||||
"src/shared/constants/callLogSizeLimitMarkers.ts",
|
||||
"bin/cli/runtime/trayRuntime.ts",
|
||||
"src/shared/platform/windowsProcess.ts",
|
||||
"src/lib/cli-helper/doctor/checks.ts",
|
||||
"open-sse/config/freeModelCatalog.data.ts",
|
||||
"open-sse/utils/errorSanitization.ts",
|
||||
"open-sse/utils/errorPathRedaction.ts",
|
||||
"open-sse/utils/credentialPatterns.ts",
|
||||
"open-sse/utils/providerSafety.ts",
|
||||
"open-sse/utils/registeredFeatureFlag.ts",
|
||||
].map((file) => fileURLToPath(new URL(`../../${file}`, import.meta.url)))
|
||||
);
|
||||
|
||||
function isCliEntry(parentURL) {
|
||||
if (parentURL === cliEntry.href) return true;
|
||||
const entry = process.argv[1];
|
||||
if (!entry || !parentURL?.endsWith("/bin/omniroute.mjs")) return false;
|
||||
// The installed-layout fixture copies the executable and shares the CLI modules.
|
||||
// Check the bytes before substituting the bridge for a CLI outside the checkout.
|
||||
try {
|
||||
// Node can resolve symlinks for the module while argv keeps the supplied path.
|
||||
if (
|
||||
parentURL !== pathToFileURL(entry).href &&
|
||||
parentURL !== pathToFileURL(realpathSync(entry)).href
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return readFileSync(entry).equals(readFileSync(cliEntry));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function loadNativeCoverageSource(url, context, nextLoad) {
|
||||
if (url.startsWith("file:")) {
|
||||
const filename = fileURLToPath(url);
|
||||
if (nativeSources.has(filename)) {
|
||||
return {
|
||||
format: "module-typescript",
|
||||
source: readFileSync(filename),
|
||||
shortCircuit: true,
|
||||
};
|
||||
}
|
||||
// tsx rewrites dynamic imports even in JavaScript, giving these Node entry
|
||||
// points different V8 coordinates than their bare CLI or gate invocations.
|
||||
if (filename.endsWith(".mjs") && nativeModuleRoots.some((root) => filename.startsWith(root))) {
|
||||
return { format: "module", source: readFileSync(filename), shortCircuit: true };
|
||||
}
|
||||
}
|
||||
return nextLoad(url, context);
|
||||
}
|
||||
|
||||
export function registerNativeCliCoverage() {
|
||||
registerHooks({ load: loadNativeCoverageSource });
|
||||
}
|
||||
|
||||
const tsxImport = ["tsx/esm", "tsx"].find((name) =>
|
||||
process.execArgv.some(
|
||||
(arg, index, args) =>
|
||||
arg === `--import=${name}` || (arg === "--import" && args[index + 1] === name)
|
||||
)
|
||||
);
|
||||
|
||||
if (process.env.NODE_V8_COVERAGE && !isInternalThread) {
|
||||
const { extensions } = createRequire(import.meta.url);
|
||||
const nativeTypeScript = extensions[".ts"] ?? extensions[".js"];
|
||||
|
||||
// NODE_OPTIONS preloads run before the existing --import tsx flag. Register
|
||||
// tsx first so this hook takes precedence. Its later CLI import is cached.
|
||||
if (tsxImport) await import(tsxImport);
|
||||
registerHooks({
|
||||
resolve(specifier, context, nextResolve) {
|
||||
// The CLI installs tsx after it has configured stdout. Apply the native
|
||||
// loader again at that import so it takes precedence over tsx.
|
||||
if (specifier === "tsx/esm" && isCliEntry(context.parentURL)) {
|
||||
return { url: cliTsxBridge, format: "module", shortCircuit: true };
|
||||
}
|
||||
return nextResolve(specifier, context);
|
||||
},
|
||||
load: loadNativeCoverageSource,
|
||||
});
|
||||
|
||||
if (tsxImport === "tsx") {
|
||||
// The full tsx loader installs its own loader for TypeScript require().
|
||||
// Delegate these explicit files back to the original Node extension.
|
||||
const tsxTypeScript = extensions[".ts"];
|
||||
extensions[".ts"] = (module, filename) =>
|
||||
(nativeSources.has(filename) ? nativeTypeScript : tsxTypeScript)(module, filename);
|
||||
}
|
||||
}
|
||||
794
scripts/ci/prepare-v8-coverage.mjs
Normal file
794
scripts/ci/prepare-v8-coverage.mjs
Normal file
@@ -0,0 +1,794 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { lstat, mkdir, mkdtemp, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
|
||||
import { createRequire } from "node:module";
|
||||
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { isDeepStrictEqual, promisify } from "node:util";
|
||||
import { gzip, gunzip } from "node:zlib";
|
||||
import { verifyCoverageSelection } from "./select-v8-coverage.mjs";
|
||||
import { createSourceMapLayoutProjector } from "./v8-source-map-layouts.mjs";
|
||||
import {
|
||||
initializeTransformCache,
|
||||
matchingTransformProofs,
|
||||
packTransformEvidence,
|
||||
readTransformEvidence,
|
||||
transformProducerIdentity,
|
||||
} from "./v8-transform-evidence.mjs";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const { mergeProcessCovs, mergeScriptCovs } = require("@bcoe/v8-coverage");
|
||||
const requireFromC8 = createRequire(require.resolve("c8/package.json"));
|
||||
const TestExclude = requireFromC8("test-exclude");
|
||||
const { buildYargs } = require("c8/lib/parse-args");
|
||||
const c8Version = require("c8/package.json").version;
|
||||
const compress = promisify(gzip);
|
||||
const decompress = promisify(gunzip);
|
||||
const hash = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
||||
const PATH_POLICY = "git-tracked-file-urls-v1";
|
||||
const MAX_REPORTED_LAYOUT_SOURCES = 32;
|
||||
const RAW_NAME = /^[a-zA-Z0-9_.-]+\.json$/;
|
||||
const SHA256 = /^[a-f0-9]{64}$/;
|
||||
const SHA = /^[a-f0-9]{40}$/;
|
||||
|
||||
function identity({ candidateSha, runId, runAttempt }) {
|
||||
if (
|
||||
!SHA.test(candidateSha || "") ||
|
||||
!/^[1-9][0-9]*$/.test(runId || "") ||
|
||||
!/^[1-9][0-9]*$/.test(runAttempt || "")
|
||||
) {
|
||||
throw new Error("Coverage requires candidate SHA, run ID and run attempt");
|
||||
}
|
||||
return { candidateSha, runId: String(runId), runAttempt: String(runAttempt) };
|
||||
}
|
||||
|
||||
function shardIdentity(shard) {
|
||||
const match = /^([1-9][0-9]*)\/([1-9][0-9]*)$/.exec(shard || "");
|
||||
if (!match || +match[1] > +match[2] || +match[2] > 256) throw new Error("Invalid coverage shard");
|
||||
return { index: +match[1], total: +match[2] };
|
||||
}
|
||||
|
||||
function safePath(path) {
|
||||
return (
|
||||
typeof path === "string" &&
|
||||
path.length > 0 &&
|
||||
!isAbsolute(path) &&
|
||||
!/[\\\x00-\x1f]/.test(path) &&
|
||||
!path.split("/").some((part) => !part || part === "." || part === "..")
|
||||
);
|
||||
}
|
||||
|
||||
function inside(root, absolute) {
|
||||
const path = relative(root, absolute);
|
||||
return path &&
|
||||
!isAbsolute(path) &&
|
||||
path !== ".." &&
|
||||
!path.startsWith(`..${process.platform === "win32" ? "\\" : "/"}`)
|
||||
? path.split(process.platform === "win32" ? "\\" : "/").join("/")
|
||||
: null;
|
||||
}
|
||||
|
||||
function gitTree(root, sha) {
|
||||
const git = (args) =>
|
||||
execFileSync("git", ["-C", root, ...args], { encoding: "utf8", maxBuffer: 32 * 1024 * 1024 });
|
||||
if (git(["rev-parse", "--verify", `${sha}^{commit}`]).trim() !== sha)
|
||||
throw new Error("Candidate commit mismatch");
|
||||
const entries = git(["ls-tree", "-rz", "--full-tree", sha]).split("\0").filter(Boolean);
|
||||
return new Map(
|
||||
entries.map((entry) => {
|
||||
const tab = entry.indexOf("\t");
|
||||
const match = /^(\d{6}) (blob|commit) ([a-f0-9]{40})$/.exec(entry.slice(0, tab));
|
||||
const path = entry.slice(tab + 1);
|
||||
if (!match || !safePath(path)) throw new Error("Invalid candidate source path");
|
||||
return [path, { mode: match[1], type: match[2], blob: match[3] }];
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
async function regular(path) {
|
||||
if (!(await lstat(path)).isFile()) throw new Error("Coverage evidence must be a regular file");
|
||||
}
|
||||
|
||||
async function sourceIdentity(root, path, tree) {
|
||||
if (!safePath(path)) throw new Error("Unsafe coverage source path");
|
||||
const entry = tree.get(path);
|
||||
if (!entry || entry.type !== "blob" || !["100644", "100755"].includes(entry.mode))
|
||||
throw new Error("Coverage source is not a regular tracked file");
|
||||
let cursor = root;
|
||||
const parts = path.split("/");
|
||||
for (const part of parts) {
|
||||
cursor = join(cursor, part);
|
||||
if ((await lstat(cursor)).isSymbolicLink())
|
||||
throw new Error("Coverage source cannot traverse a symlink");
|
||||
}
|
||||
await regular(cursor);
|
||||
const bytes = await readFile(cursor);
|
||||
const blob = createHash("sha1").update(`blob ${bytes.length}\0`).update(bytes).digest("hex");
|
||||
if (blob !== entry.blob) throw new Error("Coverage source differs from its candidate blob");
|
||||
return { path, blob, bytes: bytes.length, sha256: hash(bytes) };
|
||||
}
|
||||
|
||||
function absolutePath(value, base) {
|
||||
if (typeof value !== "string") throw new Error("Invalid coverage path");
|
||||
if (value.startsWith("file:")) return fileURLToPath(value);
|
||||
if (isAbsolute(value)) return value;
|
||||
if (base && !/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(value)) return resolve(base, value);
|
||||
return null;
|
||||
}
|
||||
|
||||
function c8CoveragePolicy(cwd) {
|
||||
const policy = buildYargs().parse(["report", "--exclude=tests/**", "--exclude=**/*.test.*"]);
|
||||
const exclude = new TestExclude({
|
||||
cwd,
|
||||
exclude: policy.exclude,
|
||||
include: policy.include,
|
||||
extension: policy.extension,
|
||||
excludeNodeModules: policy.excludeNodeModules,
|
||||
relativePath: !policy.allowExternal,
|
||||
});
|
||||
return { policy, exclude };
|
||||
}
|
||||
|
||||
function verifyEmbeddedSources(data, origin, target, sources, exclude) {
|
||||
for (const [url, cache] of Object.entries(data["source-map-cache"] || {})) {
|
||||
const absolute = absolutePath(url);
|
||||
const path = absolute && inside(origin, absolute);
|
||||
const map = cache?.data;
|
||||
if (!path || !sources.has(path) || !exclude.shouldInstrument(resolve(target, path))) continue;
|
||||
if (map?.sourcesContent == null) continue;
|
||||
if (
|
||||
!Array.isArray(map.sourcesContent) ||
|
||||
!Array.isArray(map.sources) ||
|
||||
map.sourcesContent.length > map.sources.length
|
||||
)
|
||||
throw new Error("V8_EVIDENCE_INCOMPLETE:generated-evidence-embedded-source");
|
||||
const root = map.sourceRoot
|
||||
? absolutePath(map.sourceRoot, dirname(absolute))
|
||||
: dirname(absolute);
|
||||
if (!root) throw new Error("V8_EVIDENCE_INCOMPLETE:generated-evidence-embedded-source");
|
||||
for (const [index, text] of map.sourcesContent.entries()) {
|
||||
if (text == null) continue;
|
||||
const source = absolutePath(map.sources[index], root);
|
||||
const expected = source && sources.get(inside(origin, source));
|
||||
if (typeof text !== "string" || !expected || hash(text) !== expected.sha256)
|
||||
throw new Error(`V8_EVIDENCE_INCOMPLETE:generated-evidence-embedded-source (${path})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parseCoverage(bytes, shard, rawInput) {
|
||||
try {
|
||||
return JSON.parse(bytes.toString("utf8"));
|
||||
} catch (cause) {
|
||||
const detail = JSON.stringify({ shard, rawInput, bytes: bytes.length });
|
||||
throw new Error(`V8_EVIDENCE_INCOMPLETE:invalid-raw-json ${detail}`, { cause });
|
||||
}
|
||||
}
|
||||
|
||||
function validateCoverage(data) {
|
||||
if (!data || !Array.isArray(data.result)) throw new Error("Invalid V8 coverage result");
|
||||
for (const script of data.result) {
|
||||
if (typeof script.url !== "string" || !Array.isArray(script.functions))
|
||||
throw new Error("Invalid V8 script");
|
||||
for (const fn of script.functions) {
|
||||
if (!Array.isArray(fn.ranges) || !fn.ranges.length) throw new Error("Missing V8 ranges");
|
||||
for (const range of fn.ranges) {
|
||||
if (
|
||||
![range.startOffset, range.endOffset, range.count].every(
|
||||
(value) => Number.isSafeInteger(value) && value >= 0
|
||||
) ||
|
||||
range.endOffset < range.startOffset
|
||||
)
|
||||
throw new Error("Invalid V8 range");
|
||||
}
|
||||
}
|
||||
}
|
||||
const maps = data["source-map-cache"];
|
||||
if (maps !== undefined && (!maps || typeof maps !== "object" || Array.isArray(maps)))
|
||||
throw new Error("Invalid V8 source-map-cache");
|
||||
}
|
||||
|
||||
function isNodeTestMockWrapper(script, maps) {
|
||||
if (
|
||||
!script.url.startsWith("file:") ||
|
||||
!script.url.includes("?node-test-mock=") ||
|
||||
Object.hasOwn(maps, script.url) ||
|
||||
script.functions.length !== 1
|
||||
)
|
||||
return false;
|
||||
const url = new URL(script.url);
|
||||
if (url.hash || !/^\?node-test-mock=(?:0|[1-9][0-9]*)$/.test(url.search)) return false;
|
||||
url.search = "";
|
||||
const [fn] = script.functions;
|
||||
if (fn.functionName !== "" || fn.isBlockCoverage !== true || fn.ranges.length !== 2) return false;
|
||||
|
||||
// Node's ESM mock loader emits a single wrapper with an unexecuted error
|
||||
// branch. Its exact offsets depend on the base URL embedded twice by Node.
|
||||
// The reserved query alone can also identify a genuine import.
|
||||
const quoted = JSON.stringify(url.href);
|
||||
const prefix = `import $__test from 'node:test';\nif (!$__test.mock._mockExports.has(${quoted})) `;
|
||||
const missing = JSON.stringify(`mock exports not found for "${url.href}"`);
|
||||
const branch = `{\n throw new Error(${missing});\n}`;
|
||||
const suffix = `\n\nconst $__exports = $__test.mock._mockExports.get(${quoted});\n\n\n`;
|
||||
const [root, failure] = fn.ranges;
|
||||
return (
|
||||
root.startOffset === 0 &&
|
||||
root.count === 1 &&
|
||||
root.endOffset >= prefix.length + branch.length + suffix.length &&
|
||||
failure.startOffset === prefix.length &&
|
||||
failure.endOffset === prefix.length + branch.length &&
|
||||
failure.count === 0
|
||||
);
|
||||
}
|
||||
|
||||
/** Only declared candidate paths move. Counters, ranges and mappings never change. */
|
||||
function visitPaths(data, origin, target, tree, visit) {
|
||||
const transform = (value, base, requireTracked = false) => {
|
||||
const absolute = absolutePath(value, base);
|
||||
if (!absolute) return value;
|
||||
const path = inside(origin, absolute);
|
||||
if (!path || !tree.has(path)) {
|
||||
if (requireTracked || (origin !== target && inside(target, absolute)))
|
||||
throw new Error("Coverage source-map path escapes its candidate");
|
||||
return value;
|
||||
}
|
||||
visit(path);
|
||||
if (origin === target) return value;
|
||||
const moved = resolve(target, path);
|
||||
if (value.startsWith("file:")) {
|
||||
const url = new URL(value);
|
||||
const result = pathToFileURL(moved);
|
||||
result.search = url.search;
|
||||
result.hash = url.hash;
|
||||
return result.href;
|
||||
}
|
||||
return isAbsolute(value) ? moved : value;
|
||||
};
|
||||
for (const script of data.result) script.url = transform(script.url);
|
||||
if (data["source-map-cache"]) {
|
||||
const normalized = {};
|
||||
for (const [url, entry] of Object.entries(data["source-map-cache"])) {
|
||||
const original = absolutePath(url);
|
||||
const path = original && inside(origin, original);
|
||||
const tracked = path && tree.has(path);
|
||||
if (!entry || typeof entry !== "object") throw new Error("Invalid source map entry");
|
||||
const map = entry.data;
|
||||
if (tracked && map) {
|
||||
if (!Array.isArray(map.sources)) throw new Error("Invalid source map sources");
|
||||
const mapRoot = map.sourceRoot
|
||||
? absolutePath(map.sourceRoot, dirname(original))
|
||||
: dirname(original);
|
||||
if (!mapRoot) throw new Error("Invalid source map root");
|
||||
for (const source of map.sources) transform(source, mapRoot, true);
|
||||
map.sources = map.sources.map((source) => {
|
||||
const absolute = absolutePath(source, mapRoot);
|
||||
const sourcePath = inside(origin, absolute);
|
||||
return origin === target ? source : pathToFileURL(resolve(target, sourcePath)).href;
|
||||
});
|
||||
// Absolute source URLs above make the relocation explicit; no old root
|
||||
// may be prepended by source-map consumers after moving the script.
|
||||
if (origin !== target) map.sourceRoot = "";
|
||||
}
|
||||
const key = transform(url);
|
||||
if (Object.hasOwn(normalized, key)) throw new Error("Duplicate relocated source map");
|
||||
normalized[key] = entry;
|
||||
}
|
||||
data["source-map-cache"] = normalized;
|
||||
}
|
||||
}
|
||||
|
||||
async function freshStage(outputDirectory) {
|
||||
const output = resolve(outputDirectory);
|
||||
if (
|
||||
await lstat(output).catch((error) => {
|
||||
if (error.code !== "ENOENT") throw error;
|
||||
return null;
|
||||
})
|
||||
)
|
||||
throw new Error("Coverage output already exists");
|
||||
await mkdir(dirname(output), { recursive: true });
|
||||
return { output, stage: await mkdtemp(join(dirname(output), ".v8-evidence-")) };
|
||||
}
|
||||
|
||||
export async function initializeCoverageTransformCache(options) {
|
||||
const binding = identity(options);
|
||||
const shard = shardIdentity(options.shard);
|
||||
const sourceRoot = resolve(options.sourceRoot || process.cwd());
|
||||
gitTree(sourceRoot, binding.candidateSha);
|
||||
const producer = transformProducerIdentity({
|
||||
...binding,
|
||||
shard: `${shard.index}/${shard.total}`,
|
||||
sourceRoot,
|
||||
});
|
||||
return initializeTransformCache({ directory: options.directory, producer });
|
||||
}
|
||||
|
||||
export async function packCoverageShard(options) {
|
||||
const binding = identity(options);
|
||||
const shard = shardIdentity(options.shard);
|
||||
const sourceRoot = resolve(options.sourceRoot || process.cwd());
|
||||
const tree = gitTree(sourceRoot, binding.candidateSha);
|
||||
const { exclude } = c8CoveragePolicy(sourceRoot);
|
||||
const input = resolve(options.inputDirectory);
|
||||
const files = (await readdir(input)).sort();
|
||||
if (!files.length) throw new Error("No V8 coverage inputs");
|
||||
const { output, stage } = await freshStage(options.outputDirectory);
|
||||
const sources = new Map();
|
||||
const inputs = [];
|
||||
try {
|
||||
await mkdir(join(stage, "raw"));
|
||||
for (const name of files) {
|
||||
if (!RAW_NAME.test(name)) throw new Error("Unexpected V8 input name");
|
||||
const path = join(input, name);
|
||||
await regular(path);
|
||||
const bytes = await readFile(path);
|
||||
const data = parseCoverage(bytes, options.shard, name);
|
||||
validateCoverage(data);
|
||||
const referenced = new Set();
|
||||
visitPaths(data, sourceRoot, sourceRoot, tree, (path) => referenced.add(path));
|
||||
for (const path of referenced) {
|
||||
if (!sources.has(path)) sources.set(path, await sourceIdentity(sourceRoot, path, tree));
|
||||
}
|
||||
// c8 may use embedded source text instead of reading the checked-out file.
|
||||
verifyEmbeddedSources(data, sourceRoot, sourceRoot, sources, exclude);
|
||||
const packed = await compress(bytes, { level: 1 });
|
||||
await writeFile(join(stage, "raw", `${name}.gz`), packed, { flag: "wx" });
|
||||
inputs.push({
|
||||
name,
|
||||
bytes: bytes.length,
|
||||
sha256: hash(bytes),
|
||||
packedBytes: packed.length,
|
||||
packedSha256: hash(packed),
|
||||
});
|
||||
}
|
||||
if (JSON.stringify(files) !== JSON.stringify((await readdir(input)).sort()))
|
||||
throw new Error("Coverage inputs changed during packaging");
|
||||
const transformEvidence = await packTransformEvidence({
|
||||
directory: options.transformCacheDirectory,
|
||||
artifactDirectory: stage,
|
||||
sourceFiles: sources,
|
||||
producer: transformProducerIdentity({
|
||||
...binding,
|
||||
shard: `${shard.index}/${shard.total}`,
|
||||
sourceRoot,
|
||||
}),
|
||||
});
|
||||
const manifest = {
|
||||
schemaVersion: 1,
|
||||
...binding,
|
||||
shard: `${shard.index}/${shard.total}`,
|
||||
sourceRoot,
|
||||
pathPolicy: PATH_POLICY,
|
||||
nodeVersion: process.version,
|
||||
c8Version,
|
||||
sourceFiles: [...sources.values()].sort((a, b) => a.path.localeCompare(b.path)),
|
||||
inputFiles: inputs.length,
|
||||
inputBytes: inputs.reduce((sum, item) => sum + item.bytes, 0),
|
||||
packedBytes: inputs.reduce((sum, item) => sum + item.packedBytes, 0),
|
||||
inputs,
|
||||
transformEvidence,
|
||||
};
|
||||
await writeFile(join(stage, "manifest.json"), JSON.stringify(manifest, null, 2) + "\n", {
|
||||
flag: "wx",
|
||||
});
|
||||
await rename(stage, output);
|
||||
return manifest;
|
||||
} finally {
|
||||
await rm(stage, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
export async function prepareCoverageShards(options) {
|
||||
const binding = identity(options);
|
||||
const total = options.total;
|
||||
if (!Number.isSafeInteger(total) || total < 1 || total > 256)
|
||||
throw new Error("Invalid shard total");
|
||||
const sourceRoot = resolve(options.sourceRoot || process.cwd());
|
||||
const tree = gitTree(sourceRoot, binding.candidateSha);
|
||||
const directory = resolve(options.directory);
|
||||
const producers = options.selection
|
||||
? verifyCoverageSelection(options.selection, options)
|
||||
: Array.from({ length: total }, (_, i) => ({
|
||||
index: i + 1,
|
||||
producerAttempt: binding.runAttempt,
|
||||
artifactName: `coverage-shard-${binding.runAttempt}-${i + 1}`,
|
||||
}));
|
||||
const expected = producers.map((producer) => producer.artifactName).sort();
|
||||
if (JSON.stringify((await readdir(directory)).sort()) !== JSON.stringify(expected))
|
||||
throw new Error("V8_EVIDENCE_INCOMPLETE:downloaded-shard-inventory-mismatch");
|
||||
const inputs = [];
|
||||
const verifiedSources = new Map();
|
||||
const manifestHashes = [];
|
||||
let generatedEvidenceBytes = 0;
|
||||
for (let index = 1; index <= total; index++) {
|
||||
const producer = producers[index - 1];
|
||||
const producerBinding = { ...binding, runAttempt: producer.producerAttempt };
|
||||
const artifact = join(directory, producer.artifactName);
|
||||
if (!(await lstat(artifact)).isDirectory()) throw new Error("Invalid coverage artifact layout");
|
||||
await regular(join(artifact, "manifest.json"));
|
||||
const manifestBytes = await readFile(join(artifact, "manifest.json"));
|
||||
const manifest = JSON.parse(manifestBytes.toString("utf8"));
|
||||
manifestHashes.push(hash(manifestBytes));
|
||||
const artifactNames = manifest.transformEvidence?.entries?.length
|
||||
? ["generated", "manifest.json", "raw"]
|
||||
: ["manifest.json", "raw"];
|
||||
if (JSON.stringify((await readdir(artifact)).sort()) !== JSON.stringify(artifactNames))
|
||||
throw new Error("Invalid coverage artifact layout");
|
||||
if (
|
||||
manifest.schemaVersion !== 1 ||
|
||||
Object.entries(producerBinding).some(([key, value]) => manifest[key] !== value) ||
|
||||
manifest.shard !== `${index}/${total}` ||
|
||||
manifest.pathPolicy !== PATH_POLICY ||
|
||||
typeof manifest.sourceRoot !== "string" ||
|
||||
!isAbsolute(manifest.sourceRoot) ||
|
||||
resolve(manifest.sourceRoot) !== manifest.sourceRoot ||
|
||||
manifest.nodeVersion !== process.version ||
|
||||
manifest.c8Version !== c8Version ||
|
||||
!Array.isArray(manifest.inputs) ||
|
||||
!manifest.inputs.length ||
|
||||
manifest.inputFiles !== manifest.inputs.length ||
|
||||
!Array.isArray(manifest.sourceFiles)
|
||||
)
|
||||
throw new Error("Incompatible coverage shard identity");
|
||||
const declared = new Set();
|
||||
for (const source of manifest.sourceFiles) {
|
||||
if (!safePath(source.path) || declared.has(source.path))
|
||||
throw new Error("Invalid or duplicate source identity");
|
||||
declared.add(source.path);
|
||||
if (!verifiedSources.has(source.path))
|
||||
verifiedSources.set(source.path, await sourceIdentity(sourceRoot, source.path, tree));
|
||||
const expectedSource = verifiedSources.get(source.path);
|
||||
if (["blob", "bytes", "sha256"].some((key) => source[key] !== expectedSource[key]))
|
||||
throw new Error("Coverage source blob mismatch");
|
||||
}
|
||||
const generated = await readTransformEvidence({
|
||||
artifactDirectory: artifact,
|
||||
evidence: manifest.transformEvidence,
|
||||
producer: transformProducerIdentity({
|
||||
...producerBinding,
|
||||
shard: `${index}/${total}`,
|
||||
sourceRoot: manifest.sourceRoot,
|
||||
}),
|
||||
sourceFiles: new Map([...declared].map((path) => [path, verifiedSources.get(path)])),
|
||||
});
|
||||
generatedEvidenceBytes += manifest.transformEvidence?.payloadBytes || 0;
|
||||
const rawDirectory = join(artifact, "raw");
|
||||
if (!(await lstat(rawDirectory)).isDirectory())
|
||||
throw new Error("Invalid raw coverage directory");
|
||||
const names = new Set();
|
||||
for (const item of manifest.inputs) {
|
||||
if (
|
||||
!RAW_NAME.test(item.name || "") ||
|
||||
names.has(item.name) ||
|
||||
!SHA256.test(item.sha256 || "") ||
|
||||
!SHA256.test(item.packedSha256 || "") ||
|
||||
!Number.isSafeInteger(item.bytes) ||
|
||||
item.bytes <= 0 ||
|
||||
!Number.isSafeInteger(item.packedBytes) ||
|
||||
item.packedBytes <= 0
|
||||
)
|
||||
throw new Error("Invalid or duplicate V8 coverage input");
|
||||
names.add(item.name);
|
||||
inputs.push({
|
||||
...item,
|
||||
file: join(rawDirectory, `${item.name}.gz`),
|
||||
index,
|
||||
origin: manifest.sourceRoot,
|
||||
declared,
|
||||
generated,
|
||||
});
|
||||
}
|
||||
if (
|
||||
JSON.stringify((await readdir(rawDirectory)).sort()) !==
|
||||
JSON.stringify([...names].map((name) => `${name}.gz`).sort()) ||
|
||||
manifest.inputBytes !== manifest.inputs.reduce((sum, item) => sum + item.bytes, 0) ||
|
||||
manifest.packedBytes !== manifest.inputs.reduce((sum, item) => sum + item.packedBytes, 0)
|
||||
)
|
||||
throw new Error("Incomplete coverage shard inventory");
|
||||
}
|
||||
inputs.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : a.index - b.index));
|
||||
const { policy, exclude } = c8CoveragePolicy(sourceRoot);
|
||||
const { output, stage } = await freshStage(options.outputDirectory);
|
||||
const instrumentedShards = new Set();
|
||||
const layouts = createSourceMapLayoutProjector();
|
||||
const sourceMapCache = Object.create(null);
|
||||
const mergeOrder = createHash("sha256");
|
||||
const mergedScripts = new Map();
|
||||
let relocatedFiles = 0;
|
||||
let projectedFiles = 0;
|
||||
let ignoredNodeTestMockScripts = 0;
|
||||
let firstLayoutError;
|
||||
const incompatibleLayouts = new Map();
|
||||
try {
|
||||
for (const item of inputs) {
|
||||
await regular(item.file);
|
||||
const packed = await readFile(item.file);
|
||||
if (packed.length !== item.packedBytes || hash(packed) !== item.packedSha256)
|
||||
throw new Error("Packed coverage hash mismatch");
|
||||
const bytes = await decompress(packed, { maxOutputLength: item.bytes });
|
||||
if (bytes.length !== item.bytes || hash(bytes) !== item.sha256)
|
||||
throw new Error("Raw coverage hash mismatch");
|
||||
const data = parseCoverage(bytes, `${item.index}/${total}`, item.name);
|
||||
validateCoverage(data);
|
||||
verifyEmbeddedSources(data, item.origin, sourceRoot, verifiedSources, exclude);
|
||||
const maps = new Map();
|
||||
for (const [url, cache] of Object.entries(data["source-map-cache"] || {})) {
|
||||
const absolute = absolutePath(url);
|
||||
const path = absolute && inside(item.origin, absolute);
|
||||
if (!path || !item.declared.has(path)) continue;
|
||||
const aliases = maps.get(path);
|
||||
if (!aliases) {
|
||||
maps.set(path, [cache]);
|
||||
continue;
|
||||
}
|
||||
// Node records each query-string import, while c8 combines them by path.
|
||||
// Only identical raw maps and layouts may share that canonical path.
|
||||
if (!isDeepStrictEqual(aliases[0], cache)) {
|
||||
const detail = JSON.stringify({ source: path, shard: item.index, rawInput: item.name });
|
||||
throw new Error(`V8_EVIDENCE_INCOMPLETE:ambiguous-source-map ${detail}`);
|
||||
}
|
||||
aliases.push(cache);
|
||||
}
|
||||
const actualScripts = data.result.filter(
|
||||
(script) => !isNodeTestMockWrapper(script, data["source-map-cache"] || {})
|
||||
);
|
||||
ignoredNodeTestMockScripts += data.result.length - actualScripts.length;
|
||||
const originals = actualScripts.map((script) => {
|
||||
const absolute = absolutePath(script.url);
|
||||
const path = absolute && inside(item.origin, absolute);
|
||||
return {
|
||||
url: script.url,
|
||||
generatedProofs: matchingTransformProofs(
|
||||
item.generated,
|
||||
maps.get(path)?.[0],
|
||||
path,
|
||||
script.url
|
||||
),
|
||||
};
|
||||
});
|
||||
let moved = false;
|
||||
visitPaths(data, item.origin, sourceRoot, tree, (path) => {
|
||||
if (!item.declared.has(path)) throw new Error("Undeclared coverage source identity");
|
||||
if (item.origin !== sourceRoot) moved = true;
|
||||
});
|
||||
data.result = actualScripts;
|
||||
const cacheUpdates = new Map();
|
||||
for (const [index, script] of data.result.entries()) {
|
||||
const absolute = absolutePath(script.url);
|
||||
const path = absolute && inside(sourceRoot, absolute);
|
||||
if (
|
||||
!path ||
|
||||
!item.declared.has(path) ||
|
||||
!script.functions.length ||
|
||||
!exclude.shouldInstrument(absolute)
|
||||
)
|
||||
continue;
|
||||
instrumentedShards.add(item.index);
|
||||
const cache = maps.get(path)?.[0];
|
||||
try {
|
||||
const lineLengths = layouts.project({
|
||||
script,
|
||||
cache,
|
||||
originUrl: originals[index].url,
|
||||
sourcePath: path,
|
||||
sourceIdentity: verifiedSources.get(path),
|
||||
generatedProofs: originals[index].generatedProofs,
|
||||
shard: item.index,
|
||||
rawInput: item.name,
|
||||
});
|
||||
if (lineLengths) cacheUpdates.set(path, lineLengths);
|
||||
} catch (error) {
|
||||
if (!error.message?.startsWith("V8_EVIDENCE_INCOMPLETE:source-map-layout-")) throw error;
|
||||
const previous = incompatibleLayouts.get(path);
|
||||
if (previous) {
|
||||
previous.occurrences++;
|
||||
continue;
|
||||
}
|
||||
const detail = {
|
||||
source: path,
|
||||
shard: item.index,
|
||||
rawInput: item.name,
|
||||
producerProofs: originals[index].generatedProofs.length,
|
||||
firstObservation: layouts.firstObservation(path),
|
||||
};
|
||||
firstLayoutError ??= new Error(`${error.message} ${JSON.stringify(detail)}`, {
|
||||
cause: error,
|
||||
});
|
||||
const observation = { ...detail, reason: error.message };
|
||||
incompatibleLayouts.set(path, { ...observation, occurrences: 1 });
|
||||
if (incompatibleLayouts.size <= MAX_REPORTED_LAYOUT_SOURCES)
|
||||
options.onLayoutConflict?.(observation);
|
||||
}
|
||||
}
|
||||
// Keep checking authenticated inputs for independent layout failures.
|
||||
// An incompatible sample makes the aggregate inadmissible.
|
||||
if (firstLayoutError) continue;
|
||||
|
||||
// All aliases read the original layout before every equivalent cache
|
||||
// receives the projection. c8 may retain any one of those aliases.
|
||||
for (const [path, lineLengths] of cacheUpdates)
|
||||
for (const cache of maps.get(path)) cache.lineLengths = lineLengths;
|
||||
if (moved) relocatedFiles++;
|
||||
if (cacheUpdates.size) projectedFiles++;
|
||||
// Match c8's incremental report order. Merging batches or individual
|
||||
// scripts first can change how function and block coverage are combined.
|
||||
// c8 normalizes script and cache keys separately before each merge.
|
||||
for (const [url, entry] of Object.entries(data["source-map-cache"] || {})) {
|
||||
sourceMapCache[pathToFileURL(fileURLToPath(url)).href] = entry;
|
||||
}
|
||||
const scripts = data.result.filter((script) => {
|
||||
if (script.url.startsWith("node:")) script.url = script.url.slice(5) + ".js";
|
||||
if (script.url.startsWith("file://")) {
|
||||
try {
|
||||
script.url = fileURLToPath(script.url);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return (
|
||||
(!policy.omitRelative || isAbsolute(script.url)) &&
|
||||
(policy.excludeAfterRemap || exclude.shouldInstrument(script.url))
|
||||
);
|
||||
});
|
||||
const sampleGroups = new Map();
|
||||
for (const script of scripts) {
|
||||
const group = sampleGroups.get(script.url);
|
||||
if (group) {
|
||||
group.push(script);
|
||||
} else {
|
||||
const prior = mergedScripts.get(script.url);
|
||||
sampleGroups.set(script.url, prior ? [prior, script] : [script]);
|
||||
}
|
||||
}
|
||||
// c8 groups each complete sample by URL before merging. Retain that
|
||||
// order and avoid re-normalizing scripts absent from this sample.
|
||||
for (const [url, group] of sampleGroups) {
|
||||
mergedScripts.set(url, mergeScriptCovs(group));
|
||||
}
|
||||
mergeOrder.update(JSON.stringify([item.name, item.index, item.sha256]) + "\n");
|
||||
}
|
||||
if (firstLayoutError) {
|
||||
firstLayoutError.layoutDiagnostics = {
|
||||
sourceCount: incompatibleLayouts.size,
|
||||
sources: [...incompatibleLayouts.values()].slice(0, MAX_REPORTED_LAYOUT_SOURCES),
|
||||
};
|
||||
throw firstLayoutError;
|
||||
}
|
||||
if (instrumentedShards.size !== total)
|
||||
throw new Error("A shard has no instrumented candidate coverage");
|
||||
if (!mergedScripts.size) throw new Error("Empty merged candidate coverage");
|
||||
// The final process merge also sorts the URLs and assigns the script IDs.
|
||||
const merged = mergeProcessCovs([{ result: [...mergedScripts.values()] }]);
|
||||
// c8 still performs source mapping and produces the reports. Only this
|
||||
// aggregate is written, so disk no longer holds all normalized raw inputs.
|
||||
for (const script of merged.result) {
|
||||
if (isAbsolute(script.url)) script.url = pathToFileURL(script.url).href;
|
||||
}
|
||||
const aggregate = JSON.stringify({ ...merged, "source-map-cache": sourceMapCache });
|
||||
await writeFile(join(stage, "coverage-merged.json"), aggregate, { flag: "wx" });
|
||||
await rename(stage, output);
|
||||
return {
|
||||
...binding,
|
||||
producerAttempts: producers.map((producer) => producer.producerAttempt),
|
||||
shards: total,
|
||||
inputFiles: inputs.length,
|
||||
inputBytes: inputs.reduce((sum, item) => sum + item.bytes, 0),
|
||||
packedBytes: inputs.reduce((sum, item) => sum + item.packedBytes, 0),
|
||||
generatedEvidenceBytes,
|
||||
manifestHashes,
|
||||
mergeOrderSha256: mergeOrder.digest("hex"),
|
||||
aggregateBytes: Buffer.byteLength(aggregate),
|
||||
aggregateSha256: hash(aggregate),
|
||||
relocatedFiles,
|
||||
projectedFiles,
|
||||
ignoredNodeTestMockScripts,
|
||||
layoutProjection: layouts.summary(),
|
||||
pathPolicy: PATH_POLICY,
|
||||
status: "COMPLETE",
|
||||
};
|
||||
} finally {
|
||||
await rm(stage, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
export async function verifyCoverageReport({ reportDirectory }) {
|
||||
const report = JSON.parse(await readFile(join(reportDirectory, "coverage-summary.json"), "utf8"));
|
||||
for (const name of ["statements", "lines", "functions", "branches"]) {
|
||||
const metric = report.total?.[name];
|
||||
if (
|
||||
!metric ||
|
||||
![metric.total, metric.covered, metric.skipped].every(
|
||||
(value) => Number.isSafeInteger(value) && value >= 0
|
||||
) ||
|
||||
metric.covered > metric.total ||
|
||||
metric.skipped > metric.total
|
||||
)
|
||||
throw new Error("Invalid coverage report counters");
|
||||
}
|
||||
if (!report.total.statements.total || !report.total.lines.total)
|
||||
throw new Error("Empty coverage report is incomplete");
|
||||
return {
|
||||
status: "MEASURED",
|
||||
statements: report.total.statements.total,
|
||||
lines: report.total.lines.total,
|
||||
};
|
||||
}
|
||||
|
||||
async function main() {
|
||||
if (process.argv[2] === "init-cache") {
|
||||
const [directory, shard, candidateSha, runId, runAttempt, sourceRoot] = process.argv.slice(3);
|
||||
return initializeCoverageTransformCache({
|
||||
directory,
|
||||
shard,
|
||||
candidateSha,
|
||||
runId,
|
||||
runAttempt,
|
||||
sourceRoot,
|
||||
});
|
||||
}
|
||||
const [
|
||||
operation,
|
||||
input,
|
||||
output,
|
||||
shard,
|
||||
candidateSha,
|
||||
runId,
|
||||
runAttempt,
|
||||
sourceRoot,
|
||||
selectionPath,
|
||||
runHeadSha,
|
||||
repository,
|
||||
] = process.argv.slice(2);
|
||||
const selection =
|
||||
operation === "prepare" && selectionPath
|
||||
? JSON.parse(await readFile(selectionPath, "utf8"))
|
||||
: undefined;
|
||||
return operation === "pack"
|
||||
? packCoverageShard({
|
||||
inputDirectory: input,
|
||||
outputDirectory: output,
|
||||
shard,
|
||||
candidateSha,
|
||||
runId,
|
||||
runAttempt,
|
||||
sourceRoot,
|
||||
transformCacheDirectory: selectionPath,
|
||||
})
|
||||
: operation === "prepare"
|
||||
? prepareCoverageShards({
|
||||
directory: input,
|
||||
outputDirectory: output,
|
||||
total: Number(shard),
|
||||
candidateSha,
|
||||
runId,
|
||||
runAttempt,
|
||||
sourceRoot,
|
||||
selection,
|
||||
runHeadSha,
|
||||
repository,
|
||||
onLayoutConflict: (observation) =>
|
||||
console.error(`[coverage-evidence] Layout conflict: ${JSON.stringify(observation)}`),
|
||||
})
|
||||
: operation === "verify-report"
|
||||
? verifyCoverageReport({ reportDirectory: input })
|
||||
: Promise.reject(
|
||||
new Error(
|
||||
"Usage: init-cache <directory> <shard> <sha> <run-id> <attempt> <root> | pack/prepare <input> <output> <shard-or-total> <sha> <run-id> <attempt> <root> [cache-directory or selection head-sha repository] | verify-report <dir>"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
|
||||
main().then(
|
||||
({ inputs: _inputs, sourceFiles: _sources, ...summary }) =>
|
||||
console.log(JSON.stringify(summary)),
|
||||
(error) => {
|
||||
console.error(`[coverage-evidence] ${error.message}`);
|
||||
if (error.layoutDiagnostics)
|
||||
console.error(
|
||||
`[coverage-evidence] Incompatible source layouts: ${JSON.stringify(error.layoutDiagnostics)}`
|
||||
);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
);
|
||||
}
|
||||
388
scripts/ci/select-v8-coverage.mjs
Normal file
388
scripts/ci/select-v8-coverage.mjs
Normal file
@@ -0,0 +1,388 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { appendFile, readFile, writeFile } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
|
||||
const POLICY = "v8-shard-provenance-v1";
|
||||
const SHA = /^[a-f0-9]{40}$/;
|
||||
const DIGEST = /^sha256:[a-f0-9]{64}$/;
|
||||
const MAX_ATTEMPTS = 50;
|
||||
const PAGE_SIZE = 100;
|
||||
const MAX_RECORDS = 1000;
|
||||
|
||||
function requireEvidence(condition, reason) {
|
||||
if (!condition) throw new Error(`V8_EVIDENCE_INCOMPLETE:${reason}`);
|
||||
}
|
||||
|
||||
function id(value) {
|
||||
const text = String(value ?? "");
|
||||
requireEvidence(/^[1-9][0-9]*$/.test(text), "identity-invalid");
|
||||
requireEvidence(BigInt(text) <= BigInt(Number.MAX_SAFE_INTEGER), "identity-out-of-range");
|
||||
return text;
|
||||
}
|
||||
|
||||
function binding(options) {
|
||||
const { repository, candidateSha, runHeadSha, total } = options;
|
||||
requireEvidence(
|
||||
typeof repository === "string" &&
|
||||
/^[\w.-]+\/[\w.-]+$/.test(repository) &&
|
||||
!repository.split("/").some((part) => part === "." || part === ".."),
|
||||
"repository-invalid"
|
||||
);
|
||||
requireEvidence(SHA.test(candidateSha ?? "") && SHA.test(runHeadSha ?? ""), "sha-invalid");
|
||||
requireEvidence(Number.isSafeInteger(total) && total > 0 && total <= 256, "matrix-invalid");
|
||||
const runId = id(options.runId);
|
||||
const consumerAttempt = id(options.runAttempt);
|
||||
requireEvidence(Number(consumerAttempt) <= MAX_ATTEMPTS, "attempt-limit");
|
||||
return { repository, candidateSha, runHeadSha, runId, consumerAttempt, total };
|
||||
}
|
||||
|
||||
function time(value) {
|
||||
requireEvidence(
|
||||
typeof value === "string" && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/.test(value),
|
||||
"timestamp-invalid"
|
||||
);
|
||||
const parsed = Date.parse(value);
|
||||
requireEvidence(Number.isFinite(parsed) && parsed >= 0, "timestamp-invalid");
|
||||
return parsed;
|
||||
}
|
||||
|
||||
export function resolveCoverageContext(env, event, checkoutSha) {
|
||||
const candidateSha = env.GITHUB_SHA;
|
||||
requireEvidence(
|
||||
SHA.test(candidateSha ?? "") && checkoutSha === candidateSha,
|
||||
"checkout-sha-mismatch"
|
||||
);
|
||||
requireEvidence(
|
||||
event?.repository?.full_name === env.GITHUB_REPOSITORY,
|
||||
"event-repository-mismatch"
|
||||
);
|
||||
const name = env.GITHUB_EVENT_NAME;
|
||||
requireEvidence(
|
||||
["pull_request", "push", "workflow_dispatch", "merge_group"].includes(name),
|
||||
"event-unsupported"
|
||||
);
|
||||
let runHeadSha = candidateSha;
|
||||
if (name === "pull_request") {
|
||||
const pr = event.pull_request;
|
||||
const number = id(pr?.number ?? event.number);
|
||||
requireEvidence(env.GITHUB_REF === `refs/pull/${number}/merge`, "pr-ref-mismatch");
|
||||
requireEvidence(pr?.base?.repo?.full_name === env.GITHUB_REPOSITORY, "pr-repository-mismatch");
|
||||
runHeadSha = pr?.head?.sha;
|
||||
requireEvidence(SHA.test(runHeadSha ?? ""), "pr-head-sha-missing");
|
||||
} else if (name === "push") {
|
||||
requireEvidence(
|
||||
event.after === candidateSha && event.ref === env.GITHUB_REF,
|
||||
"push-sha-mismatch"
|
||||
);
|
||||
} else if (name === "merge_group") {
|
||||
requireEvidence(event.merge_group?.head_sha === candidateSha, "merge-group-sha-mismatch");
|
||||
}
|
||||
return {
|
||||
repository: env.GITHUB_REPOSITORY,
|
||||
candidateSha,
|
||||
runHeadSha,
|
||||
runId: id(env.GITHUB_RUN_ID),
|
||||
runAttempt: id(env.GITHUB_RUN_ATTEMPT),
|
||||
};
|
||||
}
|
||||
|
||||
/** The receipt keeps producer attempts intact; the consumer attempt is separate. */
|
||||
export function verifyCoverageSelection(receipt, options) {
|
||||
const expected = binding(options);
|
||||
requireEvidence(
|
||||
receipt?.schemaVersion === 1 && receipt.policy === POLICY,
|
||||
"receipt-policy-invalid"
|
||||
);
|
||||
requireEvidence(
|
||||
Object.entries(expected).every(([key, value]) => receipt[key] === value),
|
||||
"receipt-identity-mismatch"
|
||||
);
|
||||
requireEvidence(
|
||||
Array.isArray(receipt.producers) && receipt.producers.length === expected.total,
|
||||
"receipt-matrix-incomplete"
|
||||
);
|
||||
requireEvidence(
|
||||
receipt.producers.every((producer) => producer && typeof producer === "object"),
|
||||
"receipt-producer-invalid"
|
||||
);
|
||||
const producers = [...receipt.producers].sort((a, b) => a.index - b.index);
|
||||
const jobs = new Set();
|
||||
const artifacts = new Set();
|
||||
for (const [offset, producer] of producers.entries()) {
|
||||
requireEvidence(producer?.index === offset + 1, "receipt-shard-duplicate-or-missing");
|
||||
const attempt = id(producer.producerAttempt);
|
||||
const jobId = id(producer.jobId);
|
||||
const artifactId = id(producer.artifactId);
|
||||
requireEvidence(
|
||||
producer.producerAttempt === attempt && Number(attempt) <= Number(expected.consumerAttempt),
|
||||
"receipt-attempt-invalid"
|
||||
);
|
||||
requireEvidence(!jobs.has(jobId) && !artifacts.has(artifactId), "receipt-producer-duplicate");
|
||||
jobs.add(jobId);
|
||||
artifacts.add(artifactId);
|
||||
requireEvidence(
|
||||
producer.artifactName === `coverage-shard-${attempt}-${producer.index}` &&
|
||||
DIGEST.test(producer.artifactDigest ?? ""),
|
||||
"receipt-artifact-invalid"
|
||||
);
|
||||
const start = time(producer.jobStartedAt);
|
||||
const end = time(producer.jobCompletedAt);
|
||||
const created = time(producer.artifactCreatedAt);
|
||||
const attemptStart = time(producer.attemptStartedAt);
|
||||
const nextStart =
|
||||
producer.nextAttemptStartedAt === null ? Infinity : time(producer.nextAttemptStartedAt);
|
||||
requireEvidence(
|
||||
attemptStart <= start &&
|
||||
start <= created &&
|
||||
created <= end &&
|
||||
end <= nextStart &&
|
||||
attemptStart < nextStart,
|
||||
"receipt-artifact-outside-job"
|
||||
);
|
||||
}
|
||||
return producers;
|
||||
}
|
||||
|
||||
async function get(getJson, route) {
|
||||
try {
|
||||
return await getJson(route);
|
||||
} catch {
|
||||
// HTTP bodies and thrown messages may contain authorization or environment data.
|
||||
throw new Error("V8_EVIDENCE_INCOMPLETE:github-api-unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
async function list(getJson, route, field) {
|
||||
const records = [];
|
||||
const seen = new Set();
|
||||
let total;
|
||||
for (let page = 1; page <= MAX_RECORDS / PAGE_SIZE; page++) {
|
||||
const body = await get(getJson, `${route}?per_page=${PAGE_SIZE}&page=${page}`);
|
||||
requireEvidence(
|
||||
Number.isSafeInteger(body?.total_count) &&
|
||||
body.total_count >= 0 &&
|
||||
body.total_count <= MAX_RECORDS,
|
||||
"api-inventory-limit"
|
||||
);
|
||||
if (total === undefined) total = body.total_count;
|
||||
requireEvidence(
|
||||
body.total_count === total && Array.isArray(body[field]) && body[field].length <= PAGE_SIZE,
|
||||
"api-inventory-drift"
|
||||
);
|
||||
requireEvidence(body[field].length > 0 || records.length === total, "api-inventory-incomplete");
|
||||
for (const record of body[field]) {
|
||||
const key = id(record?.id);
|
||||
requireEvidence(!seen.has(key), "api-inventory-duplicate");
|
||||
seen.add(key);
|
||||
records.push(record);
|
||||
}
|
||||
requireEvidence(records.length <= total, "api-inventory-drift");
|
||||
if (records.length === total) return records;
|
||||
}
|
||||
throw new Error("V8_EVIDENCE_INCOMPLETE:api-page-limit");
|
||||
}
|
||||
|
||||
function attemptStart(run, expected, attempt) {
|
||||
requireEvidence(
|
||||
id(run?.id) === expected.runId &&
|
||||
id(run.run_attempt) === String(attempt) &&
|
||||
run.head_sha === expected.runHeadSha &&
|
||||
run.repository?.full_name === expected.repository,
|
||||
"workflow-run-mismatch"
|
||||
);
|
||||
return time(run.run_started_at);
|
||||
}
|
||||
|
||||
function executionFingerprint(job) {
|
||||
return JSON.stringify({
|
||||
name: job.name,
|
||||
headSha: job.head_sha,
|
||||
status: job.status,
|
||||
conclusion: job.conclusion,
|
||||
startedAt: job.started_at,
|
||||
completedAt: job.completed_at,
|
||||
steps: job.steps.map((step) => ({
|
||||
number: step.number ?? null,
|
||||
name: step.name,
|
||||
status: step.status,
|
||||
conclusion: step.conclusion,
|
||||
startedAt: step.started_at,
|
||||
completedAt: step.completed_at,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
export async function selectCoverageArtifacts(options, { getJson }) {
|
||||
const expected = binding(options);
|
||||
const prefix = `/repos/${expected.repository}/actions/runs/${expected.runId}`;
|
||||
const run = await get(getJson, prefix);
|
||||
const selected = new Map();
|
||||
const retained = new Map();
|
||||
let laterAttemptStart = Infinity;
|
||||
for (
|
||||
let attempt = Number(expected.consumerAttempt);
|
||||
attempt >= 1 && selected.size < expected.total;
|
||||
attempt--
|
||||
) {
|
||||
const metadata =
|
||||
attempt === Number(expected.consumerAttempt)
|
||||
? run
|
||||
: await get(getJson, `${prefix}/attempts/${attempt}`);
|
||||
const started = attemptStart(metadata, expected, attempt);
|
||||
requireEvidence(started < laterAttemptStart, "attempt-time-ambiguous");
|
||||
const jobs = await list(getJson, `${prefix}/attempts/${attempt}/jobs`, "jobs");
|
||||
const attemptIndices = new Set();
|
||||
for (const job of jobs) {
|
||||
const match = /^Unit Tests \(([1-9][0-9]*)\/([1-9][0-9]*)\)$/.exec(job.name ?? "");
|
||||
if (!match) continue;
|
||||
const index = Number(match[1]);
|
||||
requireEvidence(
|
||||
Number(match[2]) === expected.total && index <= expected.total,
|
||||
"producer-matrix-mismatch"
|
||||
);
|
||||
requireEvidence(!attemptIndices.has(index), "producer-job-ambiguous");
|
||||
attemptIndices.add(index);
|
||||
requireEvidence(
|
||||
id(job.run_id) === expected.runId &&
|
||||
job.head_sha === expected.runHeadSha &&
|
||||
(job.run_attempt === undefined || id(job.run_attempt) === String(attempt)),
|
||||
"producer-job-identity-mismatch"
|
||||
);
|
||||
if (selected.has(index)) continue;
|
||||
// A later failure must never be replaced by an earlier successful producer.
|
||||
requireEvidence(
|
||||
job.status === "completed" && job.conclusion === "success",
|
||||
"latest-producer-not-successful"
|
||||
);
|
||||
requireEvidence(
|
||||
Array.isArray(job.steps) && job.steps.every((step) => step && typeof step === "object"),
|
||||
"producer-steps-invalid"
|
||||
);
|
||||
for (const name of ["Package successful shard coverage", "Upload complete shard coverage"]) {
|
||||
const steps = job.steps.filter((step) => step.name === name);
|
||||
requireEvidence(
|
||||
steps.length === 1 &&
|
||||
steps[0].status === "completed" &&
|
||||
steps[0].conclusion === "success",
|
||||
"producer-upload-incomplete"
|
||||
);
|
||||
}
|
||||
const jobStart = time(job.started_at);
|
||||
const jobEnd = time(job.completed_at);
|
||||
requireEvidence(jobStart <= jobEnd && jobEnd <= laterAttemptStart, "producer-time-invalid");
|
||||
const fingerprint = executionFingerprint(job);
|
||||
requireEvidence(
|
||||
!retained.has(index) || retained.get(index) === fingerprint,
|
||||
"retained-producer-history-mismatch"
|
||||
);
|
||||
if (jobStart < started) {
|
||||
// A partial rerun clones successful jobs under new IDs/run_attempt values.
|
||||
// Match their execution details against the original attempt before use.
|
||||
requireEvidence(attempt > 1 && jobEnd <= started, "retained-producer-time-ambiguous");
|
||||
retained.set(index, fingerprint);
|
||||
continue;
|
||||
}
|
||||
selected.set(index, {
|
||||
job,
|
||||
attempt,
|
||||
attemptStartedAt: metadata.run_started_at,
|
||||
nextAttemptStartedAt: Number.isFinite(laterAttemptStart)
|
||||
? new Date(laterAttemptStart).toISOString()
|
||||
: null,
|
||||
});
|
||||
}
|
||||
laterAttemptStart = started;
|
||||
}
|
||||
requireEvidence(selected.size === expected.total, "producer-matrix-incomplete");
|
||||
const artifacts = await list(getJson, `${prefix}/artifacts`, "artifacts");
|
||||
const producers = [];
|
||||
for (let index = 1; index <= expected.total; index++) {
|
||||
const { job, attempt, attemptStartedAt, nextAttemptStartedAt } = selected.get(index);
|
||||
const name = `coverage-shard-${attempt}-${index}`;
|
||||
const matches = artifacts.filter((artifact) => artifact.name === name);
|
||||
requireEvidence(matches.length === 1, "producer-artifact-missing-or-ambiguous");
|
||||
const artifact = matches[0];
|
||||
requireEvidence(
|
||||
artifact.expired === false &&
|
||||
Number.isSafeInteger(artifact.size_in_bytes) &&
|
||||
artifact.size_in_bytes > 0 &&
|
||||
DIGEST.test(artifact.digest ?? ""),
|
||||
"producer-artifact-unavailable"
|
||||
);
|
||||
requireEvidence(
|
||||
id(artifact.workflow_run?.id) === expected.runId &&
|
||||
artifact.workflow_run.head_sha === expected.runHeadSha,
|
||||
"producer-artifact-identity-mismatch"
|
||||
);
|
||||
const created = time(artifact.created_at);
|
||||
requireEvidence(
|
||||
time(job.started_at) <= created && created <= time(job.completed_at),
|
||||
"producer-artifact-outside-job"
|
||||
);
|
||||
producers.push({
|
||||
index,
|
||||
producerAttempt: String(attempt),
|
||||
jobId: id(job.id),
|
||||
artifactId: id(artifact.id),
|
||||
artifactName: name,
|
||||
artifactDigest: artifact.digest,
|
||||
jobStartedAt: job.started_at,
|
||||
jobCompletedAt: job.completed_at,
|
||||
artifactCreatedAt: artifact.created_at,
|
||||
attemptStartedAt,
|
||||
nextAttemptStartedAt,
|
||||
});
|
||||
}
|
||||
const receipt = { schemaVersion: 1, policy: POLICY, ...expected, producers };
|
||||
verifyCoverageSelection(receipt, options);
|
||||
return receipt;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const [output, total] = process.argv.slice(2);
|
||||
requireEvidence(process.argv.length === 4 && output, "usage-select-output-total");
|
||||
const env = process.env;
|
||||
const event = JSON.parse(await readFile(env.GITHUB_EVENT_PATH, "utf8"));
|
||||
const checkoutSha = execFileSync("git", ["rev-parse", "HEAD"], { encoding: "utf8" }).trim();
|
||||
const context = resolveCoverageContext(env, event, checkoutSha);
|
||||
requireEvidence(
|
||||
typeof env.GITHUB_TOKEN === "string" && env.GITHUB_TOKEN.length > 0,
|
||||
"github-token-missing"
|
||||
);
|
||||
const getJson = async (route) => {
|
||||
const response = await fetch(new URL(route, "https://api.github.com"), {
|
||||
headers: {
|
||||
Accept: "application/vnd.github+json",
|
||||
Authorization: `Bearer ${env.GITHUB_TOKEN}`,
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
redirect: "error",
|
||||
});
|
||||
requireEvidence(response.ok, "github-api-status");
|
||||
return response.json();
|
||||
};
|
||||
const receipt = await selectCoverageArtifacts({ ...context, total: Number(total) }, { getJson });
|
||||
await writeFile(output, JSON.stringify(receipt, null, 2) + "\n", { flag: "wx" });
|
||||
const artifactIds = receipt.producers.map((producer) => producer.artifactId).join(",");
|
||||
await appendFile(env.GITHUB_OUTPUT, `artifact_ids=${artifactIds}\n`);
|
||||
console.log(
|
||||
JSON.stringify({
|
||||
status: "COMPLETE",
|
||||
...context,
|
||||
producerAttempts: receipt.producers.map((producer) => producer.producerAttempt),
|
||||
artifactIds,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
|
||||
main().catch((error) => {
|
||||
const reason = /^V8_EVIDENCE_INCOMPLETE:[a-z-]+$/.test(error?.message ?? "")
|
||||
? error.message
|
||||
: "V8_EVIDENCE_INCOMPLETE:local-evidence-unavailable";
|
||||
console.error(reason);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
}
|
||||
232
scripts/ci/v8-source-map-layouts.mjs
Normal file
232
scripts/ci/v8-source-map-layouts.mjs
Normal file
@@ -0,0 +1,232 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { isAbsolute } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
|
||||
export const SOURCE_MAP_LAYOUT_POLICY = "tsx-generated-layout-v1";
|
||||
|
||||
function incomplete(reason) {
|
||||
throw new Error(`V8_EVIDENCE_INCOMPLETE:source-map-layout-${reason}`);
|
||||
}
|
||||
|
||||
function lineStarts(lengths) {
|
||||
if (!Array.isArray(lengths) || !lengths.length) incomplete("invalid-lines");
|
||||
const starts = [];
|
||||
let next = 0;
|
||||
for (const length of lengths) {
|
||||
if (!Number.isSafeInteger(length) || length < 0) incomplete("invalid-lines");
|
||||
starts.push(next);
|
||||
next += length + 1;
|
||||
if (!Number.isSafeInteger(next)) incomplete("invalid-lines");
|
||||
}
|
||||
return { starts, eof: next - 1 };
|
||||
}
|
||||
|
||||
function roots(functions) {
|
||||
return functions
|
||||
.map((fn) => [fn.functionName || "", fn.ranges[0].startOffset, fn.ranges[0].endOffset])
|
||||
.sort((a, b) => a[1] - b[1] || a[2] - b[2] || a[0].localeCompare(b[0]));
|
||||
}
|
||||
|
||||
function layoutDigest(cache) {
|
||||
return createHash("sha256").update(JSON.stringify(cache)).digest("hex");
|
||||
}
|
||||
|
||||
function verifyGeneratedProofs(previous, current, required) {
|
||||
if (previous.length > 1 || current.length > 1) incomplete("generated-evidence-ambiguous");
|
||||
if (required && (!previous.length || !current.length)) incomplete("generated-evidence-missing");
|
||||
if (!previous.length || !current.length) return;
|
||||
const [prior] = previous;
|
||||
const [next] = current;
|
||||
if (required && (prior.kind === "unrecognized-banner" || next.kind === "unrecognized-banner"))
|
||||
incomplete("unrecognized-banner");
|
||||
if (prior.kind !== next.kind || prior.normalizedBodySha256 !== next.normalizedBodySha256)
|
||||
incomplete("generated-body-differs");
|
||||
if (required && prior.suffixKind !== next.suffixKind) incomplete("unproven-layout");
|
||||
}
|
||||
|
||||
function bannerLength(url) {
|
||||
let path;
|
||||
try {
|
||||
path = url.startsWith("file:") ? fileURLToPath(url) : isAbsolute(url) ? url : null;
|
||||
} catch {
|
||||
incomplete("unrecognized-banner");
|
||||
}
|
||||
if (!path) incomplete("unrecognized-banner");
|
||||
return `__filename=${JSON.stringify(path)};(()=>{`.length;
|
||||
}
|
||||
|
||||
function projectFunctions(script, cache, originUrl, canonical, proof) {
|
||||
const from = cache.lineLengths;
|
||||
const to = canonical.cache.lineLengths;
|
||||
const trailer = proof.trailerLineIndex;
|
||||
const withSourceUrl = proof.suffixKind === "inline-source-map-and-source-url";
|
||||
if (
|
||||
from.length !== to.length ||
|
||||
!Number.isSafeInteger(trailer) ||
|
||||
trailer < 1 ||
|
||||
trailer !== canonical.generatedProofs[0].trailerLineIndex ||
|
||||
trailer !== from.length - (withSourceUrl ? 3 : 1)
|
||||
)
|
||||
incomplete("unproven-layout");
|
||||
// The exact suffix was reconstructed from the executed cache. Only those
|
||||
// unmapped trailers may change length when the checkout path changes.
|
||||
if (
|
||||
from[trailer - 1] !== 0 ||
|
||||
to[trailer - 1] !== 0 ||
|
||||
(withSourceUrl && (from[trailer + 1] !== 0 || to[trailer + 1] !== 0))
|
||||
)
|
||||
incomplete("unproven-layout");
|
||||
for (let index = 1; index < trailer; index++) {
|
||||
if (from[index] !== to[index]) incomplete("body-differs");
|
||||
}
|
||||
const bannerChanged = from[0] !== to[0];
|
||||
// A corrupted banner can accidentally equal the canonical length. Validate
|
||||
// the executed filename binding independently of whether its length changes.
|
||||
if (
|
||||
(bannerChanged || proof.kind === "tsx-cjs-filename") &&
|
||||
(!cache.data?.mappings?.startsWith(";") ||
|
||||
from[0] !== bannerLength(originUrl) ||
|
||||
to[0] !== bannerLength(canonical.originUrl))
|
||||
)
|
||||
incomplete("unrecognized-banner");
|
||||
// Both source-map trailers must be unmapped. Changing mapped code is outside
|
||||
// this projection; it needs independent instrumentation evidence instead.
|
||||
const mappingLines = cache.data?.mappings?.split(";");
|
||||
if (!mappingLines || mappingLines.slice(trailer).some((line) => line !== ""))
|
||||
incomplete("mapped-trailer");
|
||||
const source = lineStarts(from);
|
||||
const target = lineStarts(to);
|
||||
const coordinate = (offset) => {
|
||||
if (!Number.isSafeInteger(offset) || offset < 0 || offset > source.eof)
|
||||
incomplete("invalid-range");
|
||||
if (offset === 0) return 0;
|
||||
if (offset === source.eof) return target.eof;
|
||||
if (offset >= source.starts[trailer]) incomplete("range-in-trailer");
|
||||
let line = 0;
|
||||
while (line + 1 < source.starts.length && source.starts[line + 1] <= offset) line++;
|
||||
let column = offset - source.starts[line];
|
||||
if (line === 0 && bannerChanged) {
|
||||
// The only instrumented position inside the known filename banner is
|
||||
// the wrapper's `()=>{` start. Never shift an arbitrary banner range.
|
||||
if (column !== from[0] - 5) incomplete("range-in-banner");
|
||||
column = to[0] - 5;
|
||||
}
|
||||
if (column > to[line]) incomplete("invalid-range");
|
||||
return target.starts[line] + column;
|
||||
};
|
||||
const projected = script.functions.map((fn) => ({
|
||||
...fn,
|
||||
ranges: fn.ranges.map((range) => ({
|
||||
...range,
|
||||
startOffset: coordinate(range.startOffset),
|
||||
endOffset: coordinate(range.endOffset),
|
||||
})),
|
||||
}));
|
||||
if (!isDeepStrictEqual(roots(projected), canonical.roots))
|
||||
incomplete("function-identity-differs");
|
||||
for (const fn of projected) {
|
||||
for (const range of fn.ranges)
|
||||
if (range.endOffset < range.startOffset) incomplete("invalid-range");
|
||||
}
|
||||
return projected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Preserve producer bytes and project only a demonstrated tsx path layout into
|
||||
* the first admitted layout. The c8 CLI still performs the original V8 merge.
|
||||
* Source map mappings, body columns, counts and block/function flags stay intact.
|
||||
*/
|
||||
export function createSourceMapLayoutProjector() {
|
||||
const canonical = new Map();
|
||||
const transformations = new Map();
|
||||
let projectedScriptSamples = 0;
|
||||
return {
|
||||
project({
|
||||
script,
|
||||
cache,
|
||||
originUrl,
|
||||
sourcePath,
|
||||
sourceIdentity,
|
||||
generatedProofs = [],
|
||||
shard,
|
||||
rawInput,
|
||||
}) {
|
||||
if (
|
||||
typeof sourcePath !== "string" ||
|
||||
!sourcePath ||
|
||||
!/^[a-f0-9]{40}$/.test(sourceIdentity?.blob || "") ||
|
||||
!/^[a-f0-9]{64}$/.test(sourceIdentity?.sha256 || "")
|
||||
)
|
||||
incomplete("source-identity-missing");
|
||||
if (cache?.lineLengths) lineStarts(cache.lineLengths);
|
||||
const current = cache ?? null;
|
||||
const prior = canonical.get(sourcePath);
|
||||
if (!prior) {
|
||||
canonical.set(sourcePath, {
|
||||
cache: structuredClone(current),
|
||||
originUrl,
|
||||
identity: structuredClone(sourceIdentity),
|
||||
roots: roots(script.functions),
|
||||
generatedProofs: structuredClone(generatedProofs),
|
||||
firstObservation: { shard, rawInput, producerProofs: generatedProofs.length },
|
||||
});
|
||||
return null;
|
||||
}
|
||||
if (!isDeepStrictEqual(prior.identity, sourceIdentity)) incomplete("source-identity-differs");
|
||||
const sameLayout = isDeepStrictEqual(prior.cache, current);
|
||||
verifyGeneratedProofs(prior.generatedProofs, generatedProofs, !sameLayout);
|
||||
if (sameLayout) return null;
|
||||
if (
|
||||
!prior.cache?.data ||
|
||||
!current?.data ||
|
||||
!Array.isArray(prior.cache.lineLengths) ||
|
||||
!Array.isArray(current.lineLengths)
|
||||
)
|
||||
incomplete("unproven-layout");
|
||||
const { lineLengths: _priorLines, ...priorMap } = prior.cache;
|
||||
const { lineLengths: _currentLines, ...currentMap } = current;
|
||||
if (!isDeepStrictEqual(priorMap, currentMap)) incomplete("map-differs");
|
||||
const projected = projectFunctions(script, current, originUrl, prior, generatedProofs[0]);
|
||||
const fromLayoutSha256 = layoutDigest(current);
|
||||
const toLayoutSha256 = layoutDigest(prior.cache);
|
||||
const key = `${sourcePath}:${fromLayoutSha256}:${toLayoutSha256}`;
|
||||
const entry = transformations.get(key) || {
|
||||
sourcePath,
|
||||
sourceBlob: sourceIdentity.blob,
|
||||
sourceSha256: sourceIdentity.sha256,
|
||||
originalSourceUrl: originUrl,
|
||||
canonicalSourceUrl: prior.originUrl,
|
||||
fromLayoutSha256,
|
||||
toLayoutSha256,
|
||||
samples: 0,
|
||||
originalGeneratedPayloadSha256: generatedProofs[0].payloadSha256,
|
||||
canonicalGeneratedPayloadSha256: prior.generatedProofs[0].payloadSha256,
|
||||
originalCodeSha256: generatedProofs[0].codeSha256,
|
||||
canonicalCodeSha256: prior.generatedProofs[0].codeSha256,
|
||||
originalMapSha256: generatedProofs[0].mapSha256,
|
||||
canonicalMapSha256: prior.generatedProofs[0].mapSha256,
|
||||
normalizedBodySha256: generatedProofs[0].normalizedBodySha256,
|
||||
generatedCodeKind: generatedProofs[0].kind,
|
||||
suffixKind: generatedProofs[0].suffixKind,
|
||||
};
|
||||
entry.samples++;
|
||||
transformations.set(key, entry);
|
||||
projectedScriptSamples++;
|
||||
script.functions = projected;
|
||||
// The caller applies this only after all scripts in the raw sample have
|
||||
// read their original cache. Multiple script IDs can share the same URL.
|
||||
return [...prior.cache.lineLengths];
|
||||
},
|
||||
firstObservation(sourcePath) {
|
||||
return canonical.get(sourcePath)?.firstObservation;
|
||||
},
|
||||
summary() {
|
||||
return {
|
||||
policy: SOURCE_MAP_LAYOUT_POLICY,
|
||||
projectedScriptSamples,
|
||||
transformations: [...transformations.values()],
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
337
scripts/ci/v8-transform-evidence.mjs
Normal file
337
scripts/ci/v8-transform-evidence.mjs
Normal file
@@ -0,0 +1,337 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { lstat, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
||||
import { createRequire } from "node:module";
|
||||
import { userInfo } from "node:os";
|
||||
import { isAbsolute, join, relative, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const hash = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
||||
const POLICY = "exclusive-tsx-cache-v1";
|
||||
const MAX_ENTRY_BYTES = 64 * 1024 * 1024;
|
||||
const MAX_ENTRIES = 50_000;
|
||||
const CACHE_NAME = /^\d+-[a-f0-9]{40}$/;
|
||||
const HASH = /^[a-f0-9]{64}$/;
|
||||
|
||||
function incomplete(reason) {
|
||||
throw new Error(`V8_EVIDENCE_INCOMPLETE:generated-evidence-${reason}`);
|
||||
}
|
||||
|
||||
function stable(value) {
|
||||
if (Array.isArray(value)) return value.map(stable);
|
||||
if (value && typeof value === "object") {
|
||||
return Object.fromEntries(
|
||||
Object.keys(value)
|
||||
.sort()
|
||||
.map((key) => [key, stable(value[key])])
|
||||
);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
const mapDigest = (value) => hash(JSON.stringify(stable(value)));
|
||||
|
||||
async function bytesOf(path) {
|
||||
let stat;
|
||||
let bytes;
|
||||
try {
|
||||
stat = await lstat(path);
|
||||
} catch (error) {
|
||||
incomplete(error.code === "ENOENT" ? "missing-file" : "unreadable-file");
|
||||
}
|
||||
if (!stat.isFile() || stat.size > MAX_ENTRY_BYTES) incomplete("invalid-file");
|
||||
try {
|
||||
bytes = await readFile(path);
|
||||
} catch {
|
||||
incomplete("unreadable-file");
|
||||
}
|
||||
if (bytes.length > MAX_ENTRY_BYTES) incomplete("invalid-file");
|
||||
return bytes;
|
||||
}
|
||||
|
||||
function parsePayload(bytes, { allowInvalidJson = false } = {}) {
|
||||
let payload;
|
||||
let map;
|
||||
try {
|
||||
payload = JSON.parse(bytes.toString("utf8"));
|
||||
map = typeof payload?.map === "string" ? JSON.parse(payload.map) : payload?.map;
|
||||
} catch (error) {
|
||||
if (allowInvalidJson && error instanceof SyntaxError) return null;
|
||||
incomplete("invalid-json");
|
||||
}
|
||||
if (
|
||||
typeof payload?.code !== "string" ||
|
||||
!map ||
|
||||
map.version !== 3 ||
|
||||
typeof map.mappings !== "string" ||
|
||||
!Array.isArray(map.sources)
|
||||
) {
|
||||
incomplete("invalid-payload");
|
||||
}
|
||||
const mapText = typeof payload.map === "string" ? payload.map : JSON.stringify(map);
|
||||
return { code: payload.code, map, mapText };
|
||||
}
|
||||
|
||||
function mappedSource(map, sourceRoot) {
|
||||
// The demonstrated TSX transform has one original source. Other map layouts
|
||||
// are preserved as raw coverage, but cannot prove this coordinate projection.
|
||||
if (map.sources.length !== 1 || typeof map.sources[0] !== "string") return null;
|
||||
const source = map.sources[0];
|
||||
let absolute;
|
||||
try {
|
||||
absolute = source.startsWith("file:")
|
||||
? fileURLToPath(source)
|
||||
: isAbsolute(source)
|
||||
? source
|
||||
: null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (!absolute || (map.sourceRoot && map.sourceRoot !== "")) return null;
|
||||
const path = relative(sourceRoot, absolute);
|
||||
if (
|
||||
!path ||
|
||||
isAbsolute(path) ||
|
||||
path === ".." ||
|
||||
path.startsWith(`..${process.platform === "win32" ? "\\" : "/"}`)
|
||||
)
|
||||
return null;
|
||||
return path.split(process.platform === "win32" ? "\\" : "/").join("/");
|
||||
}
|
||||
|
||||
function matchesSourceContents(payload, source) {
|
||||
return (
|
||||
payload.map.sourcesContent?.length === 1 &&
|
||||
typeof payload.map.sourcesContent[0] === "string" &&
|
||||
hash(payload.map.sourcesContent[0]) === source.sha256
|
||||
);
|
||||
}
|
||||
|
||||
export function transformProducerIdentity(binding) {
|
||||
return {
|
||||
schemaVersion: 1,
|
||||
policy: POLICY,
|
||||
candidateSha: binding.candidateSha,
|
||||
runId: binding.runId,
|
||||
runAttempt: binding.runAttempt,
|
||||
shard: binding.shard,
|
||||
sourceRoot: binding.sourceRoot,
|
||||
nodeVersion: process.version,
|
||||
c8Version: require("c8/package.json").version,
|
||||
tsxVersion: require("tsx/package.json").version,
|
||||
};
|
||||
}
|
||||
|
||||
export async function initializeTransformCache({ directory, producer }) {
|
||||
const path = resolve(directory);
|
||||
// mkdir without recursive/reuse is the exclusivity check: a previous run's
|
||||
// cache cannot be adopted merely because its directory name looks familiar.
|
||||
await mkdir(path);
|
||||
await writeFile(join(path, "producer.json"), JSON.stringify(producer) + "\n", { flag: "wx" });
|
||||
return { directory: path, producer };
|
||||
}
|
||||
|
||||
export async function packTransformEvidence({
|
||||
directory,
|
||||
artifactDirectory,
|
||||
producer,
|
||||
sourceFiles,
|
||||
}) {
|
||||
if (!directory) return undefined;
|
||||
const root = resolve(directory);
|
||||
const rootStat = await lstat(root).catch(() => null);
|
||||
if (!rootStat?.isDirectory()) incomplete("invalid-directory");
|
||||
let stamp;
|
||||
try {
|
||||
stamp = JSON.parse((await bytesOf(join(root, "producer.json"))).toString("utf8"));
|
||||
} catch {
|
||||
incomplete("binding");
|
||||
}
|
||||
if (!isDeepStrictEqual(stamp, producer)) incomplete("binding");
|
||||
const uid = process.geteuid ? process.geteuid() : userInfo().username;
|
||||
const cacheDirectory = join(root, `tsx-${uid}`);
|
||||
const stat = await lstat(cacheDirectory).catch((error) => {
|
||||
if (error.code !== "ENOENT") throw error;
|
||||
return null;
|
||||
});
|
||||
if (stat && !stat.isDirectory()) incomplete("invalid-directory");
|
||||
const names = stat
|
||||
? (await readdir(cacheDirectory)).filter((name) => CACHE_NAME.test(name)).sort()
|
||||
: [];
|
||||
if (names.length > MAX_ENTRIES) incomplete("entry-limit");
|
||||
const entries = [];
|
||||
const seen = new Set();
|
||||
let scannedBytes = 0;
|
||||
let invalidJsonEntries = 0;
|
||||
let mismatchedSourceEntries = 0;
|
||||
for (const name of names) {
|
||||
const bytes = await bytesOf(join(cacheDirectory, name));
|
||||
scannedBytes += bytes.length;
|
||||
// tsx writes its cache asynchronously. An interrupted local write is a
|
||||
// cache miss. Projection still requires verified proof for every layout it changes.
|
||||
const value = parsePayload(bytes, { allowInvalidJson: true });
|
||||
if (!value) {
|
||||
invalidJsonEntries++;
|
||||
continue;
|
||||
}
|
||||
const sourcePath = mappedSource(value.map, producer.sourceRoot);
|
||||
const source = sourceFiles.get(sourcePath);
|
||||
if (!source) continue;
|
||||
// The raw V8 maps have their own source check. Local cache entries that
|
||||
// cannot prove a candidate transform are omitted from transport.
|
||||
if (!matchesSourceContents(value, source)) {
|
||||
mismatchedSourceEntries++;
|
||||
continue;
|
||||
}
|
||||
const payloadSha256 = hash(bytes);
|
||||
if (seen.has(payloadSha256)) continue;
|
||||
seen.add(payloadSha256);
|
||||
if (!entries.length) await mkdir(join(artifactDirectory, "generated"));
|
||||
const payload = `generated/${payloadSha256}.json`;
|
||||
await writeFile(join(artifactDirectory, payload), bytes, { flag: "wx" });
|
||||
entries.push({
|
||||
sourcePath,
|
||||
sourceBlob: source.blob,
|
||||
sourceSha256: source.sha256,
|
||||
cacheEntry: name,
|
||||
payload,
|
||||
payloadBytes: bytes.length,
|
||||
payloadSha256,
|
||||
codeSha256: hash(value.code),
|
||||
mapSha256: hash(value.mapText),
|
||||
});
|
||||
}
|
||||
return {
|
||||
policy: POLICY,
|
||||
producer,
|
||||
entries,
|
||||
payloadBytes: entries.reduce((sum, entry) => sum + entry.payloadBytes, 0),
|
||||
scannedEntries: names.length,
|
||||
scannedBytes,
|
||||
invalidJsonEntries,
|
||||
mismatchedSourceEntries,
|
||||
limits: { maxEntryBytes: MAX_ENTRY_BYTES, maxEntries: MAX_ENTRIES },
|
||||
};
|
||||
}
|
||||
|
||||
function descriptor(value, entry, sourceRoot) {
|
||||
const path = resolve(sourceRoot, entry.sourcePath);
|
||||
const url = pathToFileURL(path).href;
|
||||
const banner = `__filename=${JSON.stringify(path)};(()=>{`;
|
||||
const knownBanner = value.code.startsWith(`${banner}\n`) && value.code.endsWith("\n})()\n");
|
||||
const kind = knownBanner
|
||||
? "tsx-cjs-filename"
|
||||
: value.code.startsWith("__filename=")
|
||||
? "unrecognized-banner"
|
||||
: "identical-code";
|
||||
const body = knownBanner ? value.code.slice(banner.length) : value.code;
|
||||
const sourceMap = { ...value.map, sources: [url], sourceRoot: "" };
|
||||
const inline =
|
||||
value.code +
|
||||
"\n//# sourceMappingURL=data:application/json;base64," +
|
||||
Buffer.from(JSON.stringify(value.map)).toString("base64");
|
||||
const trailerLineIndex = value.code.split("\n").length;
|
||||
return {
|
||||
sourcePath: entry.sourcePath,
|
||||
sourceUrl: url,
|
||||
kind,
|
||||
normalizedBodySha256: hash(body),
|
||||
normalizedMapSha256: mapDigest(sourceMap),
|
||||
layouts: [
|
||||
{
|
||||
suffixKind: "inline-source-map",
|
||||
trailerLineIndex,
|
||||
lineLengths: inline.split("\n").map((line) => line.length),
|
||||
},
|
||||
{
|
||||
suffixKind: "inline-source-map-and-source-url",
|
||||
trailerLineIndex,
|
||||
lineLengths: `${inline}\n\n//# sourceURL=${url}`.split("\n").map((line) => line.length),
|
||||
},
|
||||
],
|
||||
payloadSha256: entry.payloadSha256,
|
||||
codeSha256: entry.codeSha256,
|
||||
mapSha256: entry.mapSha256,
|
||||
};
|
||||
}
|
||||
|
||||
export async function readTransformEvidence({
|
||||
artifactDirectory,
|
||||
evidence,
|
||||
producer,
|
||||
sourceFiles,
|
||||
}) {
|
||||
const index = new Map();
|
||||
if (!evidence) return index;
|
||||
if (
|
||||
evidence.policy !== POLICY ||
|
||||
!isDeepStrictEqual(evidence.producer, producer) ||
|
||||
!Array.isArray(evidence.entries) ||
|
||||
evidence.entries.length > MAX_ENTRIES
|
||||
)
|
||||
incomplete("binding");
|
||||
if (evidence.entries.length && !(await lstat(join(artifactDirectory, "generated"))).isDirectory())
|
||||
incomplete("invalid-directory");
|
||||
const names = new Set();
|
||||
let payloadBytes = 0;
|
||||
for (const entry of evidence.entries) {
|
||||
const source = sourceFiles.get(entry.sourcePath);
|
||||
if (
|
||||
!source ||
|
||||
source.blob !== entry.sourceBlob ||
|
||||
source.sha256 !== entry.sourceSha256 ||
|
||||
!HASH.test(entry.payloadSha256 || "") ||
|
||||
!HASH.test(entry.codeSha256 || "") ||
|
||||
!HASH.test(entry.mapSha256 || "") ||
|
||||
entry.payload !== `generated/${entry.payloadSha256}.json` ||
|
||||
!Number.isSafeInteger(entry.payloadBytes) ||
|
||||
entry.payloadBytes <= 0 ||
|
||||
entry.payloadBytes > MAX_ENTRY_BYTES ||
|
||||
names.has(entry.payload)
|
||||
)
|
||||
incomplete("binding");
|
||||
names.add(entry.payload);
|
||||
const bytes = await bytesOf(join(artifactDirectory, entry.payload));
|
||||
if (bytes.length !== entry.payloadBytes || hash(bytes) !== entry.payloadSha256)
|
||||
incomplete("hash");
|
||||
payloadBytes += bytes.length;
|
||||
const value = parsePayload(bytes);
|
||||
if (hash(value.code) !== entry.codeSha256 || hash(value.mapText) !== entry.mapSha256)
|
||||
incomplete("hash");
|
||||
if (
|
||||
mappedSource(value.map, producer.sourceRoot) !== entry.sourcePath ||
|
||||
!matchesSourceContents(value, source)
|
||||
)
|
||||
incomplete("source");
|
||||
const descriptors = index.get(entry.sourcePath) || [];
|
||||
descriptors.push(descriptor(value, entry, producer.sourceRoot));
|
||||
index.set(entry.sourcePath, descriptors);
|
||||
}
|
||||
if (payloadBytes !== evidence.payloadBytes) incomplete("hash");
|
||||
if (evidence.entries.length) {
|
||||
const generated = join(artifactDirectory, "generated");
|
||||
if (!(await lstat(generated)).isDirectory()) incomplete("invalid-directory");
|
||||
const actual = (await readdir(generated)).map((name) => `generated/${name}`).sort();
|
||||
if (!isDeepStrictEqual(actual, [...names].sort())) incomplete("inventory");
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
export function matchingTransformProofs(index, cache, sourcePath, originUrl) {
|
||||
if (!cache?.data || !Array.isArray(cache.lineLengths)) return [];
|
||||
const digest = mapDigest(cache.data);
|
||||
const matches = (index.get(sourcePath) || []).flatMap(({ layouts, ...entry }) => {
|
||||
if (entry.sourceUrl !== originUrl || entry.normalizedMapSha256 !== digest) return [];
|
||||
return layouts
|
||||
.filter((layout) => isDeepStrictEqual(layout.lineLengths, cache.lineLengths))
|
||||
.map((layout) => ({ ...entry, ...layout }));
|
||||
});
|
||||
// Repeated writes of the same code/map are the same transform. Distinct
|
||||
// generated bodies with an indistinguishable map/layout remain ambiguous.
|
||||
return [
|
||||
...new Map(
|
||||
matches.map((entry) => [`${entry.codeSha256}:${entry.mapSha256}:${entry.suffixKind}`, entry])
|
||||
).values(),
|
||||
];
|
||||
}
|
||||
@@ -936,6 +936,15 @@ async function runDriver(): Promise<void> {
|
||||
|
||||
const cases: unknown[] = [];
|
||||
const scriptFile = fileURLToPath(import.meta.url);
|
||||
// Carry the CI test's explicit repository preload through the filtered launch.
|
||||
const coveragePreload = new URL("../ci/native-worker-v8-layout.mjs", import.meta.url).href;
|
||||
const coverageArgs =
|
||||
process.env.NODE_V8_COVERAGE &&
|
||||
process.execArgv.some(
|
||||
(arg, index) => arg === "--import" && process.execArgv[index + 1] === coveragePreload
|
||||
)
|
||||
? ["--import", coveragePreload]
|
||||
: [];
|
||||
for (const tokenEquivalent of tokenCases) {
|
||||
const caseDir = path.join(outputDir, `context-${tokenEquivalent}`);
|
||||
privateDirectory(caseDir);
|
||||
@@ -944,6 +953,7 @@ async function runDriver(): Promise<void> {
|
||||
const worker = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
...coverageArgs,
|
||||
"--expose-gc",
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
|
||||
@@ -37,7 +37,8 @@ export function resolveLogExportChild(context: ResolutionContext = {}) {
|
||||
return { childFile: entryJs ?? cwdJs, execArgv: [] as string[] };
|
||||
}
|
||||
interface TestOptions {
|
||||
modulePath: string;
|
||||
modulePath?: string;
|
||||
coveragePreload?: string;
|
||||
beforeAck?: () => Promise<void>;
|
||||
onSpawn?: (child: ChildProcess) => void;
|
||||
config?: Record<string, unknown>;
|
||||
@@ -122,11 +123,13 @@ function buildExportChildEnv(): NodeJS.ProcessEnv {
|
||||
function spawnExportChildProcess(): ChildProcess {
|
||||
const { childFile, execArgv } = resolveLogExportChild();
|
||||
const heapMb = positiveInteger(process.env.OMNIROUTE_LOG_EXPORT_CHILD_MAX_OLD_SPACE_MB, 1024);
|
||||
const coveragePreload = testOptions?.coveragePreload;
|
||||
return spawn(
|
||||
process.execPath,
|
||||
[
|
||||
`--max-old-space-size=${heapMb}`,
|
||||
"--disable-warning=ExperimentalWarning",
|
||||
...(coveragePreload ? ["--import", coveragePreload] : []),
|
||||
...execArgv,
|
||||
childFile,
|
||||
],
|
||||
|
||||
@@ -10,6 +10,7 @@ import net from "node:net";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { nativeCoverageArgs } from "./nativeCoverageArgs.ts";
|
||||
|
||||
export const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "..");
|
||||
export const BIN = path.join(ROOT, "bin", "omniroute.mjs");
|
||||
@@ -74,9 +75,23 @@ export function cleanEnv(home: string, extra: Record<string, string> = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
export function cliCoverageNodeArgs(bin: string) {
|
||||
const args = nativeCoverageArgs();
|
||||
if (!args.length || bin === BIN) return args;
|
||||
try {
|
||||
return fs.readFileSync(bin).equals(fs.readFileSync(BIN)) ? args : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function runCli(bin: string, args: string[], cwd: string, env: Record<string, string>) {
|
||||
return new Promise<{ code: number | null; stdout: string; stderr: string }>((resolve) => {
|
||||
const child = spawn(process.execPath, [bin, ...args], { cwd, env, detached: true });
|
||||
const child = spawn(process.execPath, [...cliCoverageNodeArgs(bin), bin, ...args], {
|
||||
cwd,
|
||||
env,
|
||||
detached: true,
|
||||
});
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
child.stdout.on("data", (d) => (stdout += d));
|
||||
|
||||
7
tests/_helpers/nativeCoverageArgs.ts
Normal file
7
tests/_helpers/nativeCoverageArgs.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export function nativeCoverageArgs(): string[] {
|
||||
// Node propagates V8 coverage even when a child receives a filtered environment.
|
||||
// Restore the repository preload with an explicit argument in the shard run.
|
||||
return process.env.NODE_V8_COVERAGE && process.env.OMNIROUTE_CI_SYNC_TSX_CACHE
|
||||
? ["--import", new URL("../../scripts/ci/native-worker-v8-layout.mjs", import.meta.url).href]
|
||||
: [];
|
||||
}
|
||||
@@ -11,25 +11,34 @@ function isColdBuildTimeout(response: Response): boolean {
|
||||
return response.status === 503 && response.headers.get("x-omniroute-catalog") === "build-timeout";
|
||||
}
|
||||
|
||||
export async function getSettledModelCatalogResponse(
|
||||
export function getSettledModelCatalogResponse(
|
||||
catalog: Catalog,
|
||||
request = new Request("http://localhost/api/v1/models")
|
||||
): Promise<Response> {
|
||||
const response = await catalog.getUnifiedModelsResponse(request);
|
||||
return getSettledModelCatalogRouteResponse(catalog, () =>
|
||||
catalog.getUnifiedModelsResponse(request)
|
||||
);
|
||||
}
|
||||
|
||||
export async function getSettledModelCatalogRouteResponse(
|
||||
catalog: Catalog,
|
||||
read: () => Promise<Response>
|
||||
): Promise<Response> {
|
||||
const response = await read();
|
||||
if (!isColdBuildTimeout(response)) return response;
|
||||
|
||||
// These fixtures validate catalog contents. Let the cold build finish
|
||||
// after a loaded runner reaches the endpoint's response deadline.
|
||||
let runs = catalog.__getCatalogBuilderRunsForTest();
|
||||
await catalog.__flushCatalogBackgroundRefreshForTest();
|
||||
let settled = await catalog.getUnifiedModelsResponse(request);
|
||||
let settled = await read();
|
||||
const rereadRuns = catalog.__getCatalogBuilderRunsForTest();
|
||||
if (isColdBuildTimeout(settled) && rereadRuns === runs + 1) {
|
||||
// Invalidation can make the first result uncacheable. Settle the single
|
||||
// replacement the reread already started, then require a cache hit.
|
||||
runs = rereadRuns;
|
||||
await catalog.__flushCatalogBackgroundRefreshForTest();
|
||||
settled = await catalog.getUnifiedModelsResponse(request);
|
||||
settled = await read();
|
||||
}
|
||||
assert.equal(
|
||||
catalog.__getCatalogBuilderRunsForTest(),
|
||||
|
||||
@@ -160,7 +160,10 @@ function assertFailureWasPersisted(result: PipelineResult): void {
|
||||
]);
|
||||
}
|
||||
|
||||
test("a pre-content Z.ai error fails stream readiness with a sanitized 502", async () => {
|
||||
test("a pre-content Z.ai error fails stream readiness with a sanitized 502", async (t) => {
|
||||
// The in-memory upstream is already ready to send its error. Keep unrelated
|
||||
// wall-clock scheduling delays from expiring the deadline during this test.
|
||||
t.mock.timers.enable({ apis: ["Date", "setTimeout"], now: Date.now() });
|
||||
const rawFailure =
|
||||
'signature invalid at /srv/omniroute/open-sse/auth.ts:17:9 api_key="sk-private"\n' +
|
||||
" at verify (/srv/omniroute/open-sse/auth.ts:17:9)";
|
||||
|
||||
@@ -161,7 +161,9 @@ test.after(async () => {
|
||||
fs.rmSync(TEST_PLUGINS_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
||||
});
|
||||
|
||||
test("zed-hosted pre-content status.failed becomes a sanitized 502 readiness failure", async () => {
|
||||
test("zed-hosted pre-content status.failed becomes a sanitized 502 readiness failure", async (t) => {
|
||||
// The in-memory stream already has its failure. This test exercises readiness classification.
|
||||
t.mock.timers.enable({ apis: ["Date", "setTimeout"], now: Date.now() });
|
||||
const readiness = await ensureStreamReadiness(wrapOpenFailedNdjson(), {
|
||||
timeoutMs: 100,
|
||||
provider: "zed-hosted",
|
||||
|
||||
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import test from "node:test";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const REPO_ROOT = fileURLToPath(new URL("../..", import.meta.url));
|
||||
const FIXTURE = fileURLToPath(
|
||||
@@ -43,6 +44,7 @@ test("Adapta Web non-stream error boundaries pass in an isolated process", () =>
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
...nativeCoverageArgs(),
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
"--import",
|
||||
|
||||
@@ -27,13 +27,14 @@ const posixModes = {
|
||||
};
|
||||
|
||||
test("secure file helpers request restrictive modes on every platform", async (t) => {
|
||||
// Finish any loader cache writes before spying on calls made by the helpers.
|
||||
const { ensureSecureDir, writeSecureFile } =
|
||||
await import("../../open-sse/utils/secureFileWrite.ts");
|
||||
const mkdir = t.mock.method(fs, "mkdirSync", () => undefined);
|
||||
const chmod = t.mock.method(fs, "chmodSync", () => undefined);
|
||||
const write = t.mock.method(fs, "writeFileSync", () => undefined);
|
||||
syncBuiltinESMExports();
|
||||
try {
|
||||
const { ensureSecureDir, writeSecureFile } =
|
||||
await import("../../open-sse/utils/secureFileWrite.ts");
|
||||
const dir = join(probeDataDir, "mode-contract");
|
||||
const file = join(dir, "probe.json");
|
||||
ensureSecureDir(dir);
|
||||
|
||||
@@ -4,6 +4,7 @@ import { spawnSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
import { getDbInstance, resetDbInstance } from "../../src/lib/db/core.ts";
|
||||
import { startCleanupScheduler, stopCleanupScheduler } from "../../src/lib/db/cleanup.ts";
|
||||
@@ -164,7 +165,7 @@ function runCredentialHealthInit(flag: string | undefined): string {
|
||||
try {
|
||||
const child = spawnSync(
|
||||
process.execPath,
|
||||
["--import", "tsx/esm", "--input-type=module", "-e", code],
|
||||
[...nativeCoverageArgs(), "--import", "tsx/esm", "--input-type=module", "-e", code],
|
||||
{ cwd: process.cwd(), env, encoding: "utf8", timeout: 180_000 }
|
||||
);
|
||||
assert.equal(child.status, 0, child.stderr);
|
||||
|
||||
53
tests/unit/build/env-doc-v8-context.test.mjs
Normal file
53
tests/unit/build/env-doc-v8-context.test.mjs
Normal file
@@ -0,0 +1,53 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import test from "node:test";
|
||||
import { scanCodeVars } from "../../../scripts/check/check-env-doc-sync.mjs";
|
||||
|
||||
const coverageHelpers = {
|
||||
"scripts/ci/atomic-tsx-cache.mjs": ["OMNIROUTE_CI_SYNC_TSX_CACHE", "TMPDIR"],
|
||||
"scripts/ci/native-worker-v8-layout.mjs": ["NODE_V8_COVERAGE"],
|
||||
"scripts/check/migration-commutation.ts": ["NODE_V8_COVERAGE", "OMNIROUTE_CI_SYNC_TSX_CACHE"],
|
||||
"scripts/perf/messages-route-memory-profile.ts": ["NODE_V8_COVERAGE"],
|
||||
};
|
||||
|
||||
function scanFixture(t, inputs) {
|
||||
const cwd = mkdtempSync(join(tmpdir(), "omniroute-env-v8-"));
|
||||
t.after(() => rmSync(cwd, { recursive: true, force: true }));
|
||||
for (const [file, names] of Object.entries(inputs)) {
|
||||
const path = join(cwd, file);
|
||||
mkdirSync(dirname(path), { recursive: true });
|
||||
writeFileSync(path, names.map((name) => `void process.env.${name};\n`).join(""));
|
||||
}
|
||||
return scanCodeVars({ cwd });
|
||||
}
|
||||
|
||||
test("coverage protocol inputs are scoped to the helper that uses them", (t) => {
|
||||
assert.deepEqual(scanFixture(t, coverageHelpers), new Set());
|
||||
});
|
||||
|
||||
test("the same coverage protocol names remain visible in other code", (t) => {
|
||||
const names = new Set(Object.values(coverageHelpers).flat());
|
||||
assert.deepEqual(
|
||||
scanFixture(t, {
|
||||
...coverageHelpers,
|
||||
"scripts/check/other.mjs": [...names],
|
||||
"src/other.ts": [...names],
|
||||
}),
|
||||
names
|
||||
);
|
||||
});
|
||||
|
||||
test("coverage helpers still report unknown inputs and names scoped to another helper", (t) => {
|
||||
const native = "scripts/ci/native-worker-v8-layout.mjs";
|
||||
const profile = "scripts/perf/messages-route-memory-profile.ts";
|
||||
assert.deepEqual(
|
||||
scanFixture(t, {
|
||||
...coverageHelpers,
|
||||
[native]: [...coverageHelpers[native], "TMPDIR", "UNKNOWN_COVERAGE_HELPER_INPUT"],
|
||||
[profile]: [...coverageHelpers[profile], "OMNIROUTE_CI_SYNC_TSX_CACHE"],
|
||||
}),
|
||||
new Set(["TMPDIR", "UNKNOWN_COVERAGE_HELPER_INPUT", "OMNIROUTE_CI_SYNC_TSX_CACHE"])
|
||||
);
|
||||
});
|
||||
111
tests/unit/build/fixtures/v8-disk-quota-driver.mjs
Normal file
111
tests/unit/build/fixtures/v8-disk-quota-driver.mjs
Normal file
@@ -0,0 +1,111 @@
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
createWriteStream,
|
||||
existsSync,
|
||||
readFileSync,
|
||||
renameSync,
|
||||
rmSync,
|
||||
unlink,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { mkdir, rename, rm, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { quotaObserver } from "./v8-disk-quota-preload.mjs";
|
||||
|
||||
const input = JSON.parse(readFileSync(process.argv[2], "utf8"));
|
||||
const root = process.env.OMNI_V8_QUOTA_ROOT;
|
||||
const quota = Number(process.env.OMNI_V8_QUOTA_BYTES);
|
||||
const report = {
|
||||
schemaVersion: 1,
|
||||
calibration: null,
|
||||
subject: null,
|
||||
restored: false,
|
||||
namedExportsRestored: false,
|
||||
};
|
||||
let phase = "calibration";
|
||||
try {
|
||||
quotaObserver.start("calibration");
|
||||
const calibration = join(root, "calibration");
|
||||
const unit = Math.floor(quota / 2);
|
||||
await mkdir(calibration);
|
||||
await writeFile(join(calibration, "one"), Buffer.alloc(unit, 1), { flag: "wx" });
|
||||
writeFileSync(join(calibration, "two"), Buffer.alloc(unit, 2), { flag: "wx" });
|
||||
let rejected;
|
||||
try {
|
||||
await writeFile(join(calibration, "three"), Buffer.alloc(quota - unit * 2 + 1), { flag: "wx" });
|
||||
} catch (error) {
|
||||
rejected = error.code;
|
||||
}
|
||||
assert.equal(rejected, "ENOSPC");
|
||||
assert.equal(existsSync(join(calibration, "three")), false);
|
||||
await rename(join(calibration, "one"), join(calibration, "renamed"));
|
||||
await rm(join(calibration, "renamed"));
|
||||
writeFileSync(join(calibration, "replacement"), Buffer.alloc(unit, 3));
|
||||
rmSync(join(calibration, "two"));
|
||||
renameSync(calibration, join(root, "calibration-moved"));
|
||||
const cleanup = rm(join(root, "calibration-moved"), { recursive: true });
|
||||
// The async cleanup context belongs to Node's rm descendants. An unrelated
|
||||
// caller must not borrow its permission while the native callbacks are pending.
|
||||
let callbackMutationRejected;
|
||||
try {
|
||||
unlink(join(root, "calibration-moved", "replacement"), () => {});
|
||||
} catch (error) {
|
||||
callbackMutationRejected = error.code;
|
||||
}
|
||||
const concurrentWrite = writeFile(join(root, "concurrent-write"), Buffer.alloc(1)).then(
|
||||
() => null,
|
||||
(error) => error.code
|
||||
);
|
||||
await cleanup;
|
||||
const concurrentMutationRejected = await concurrentWrite;
|
||||
assert.equal(callbackMutationRejected, "ERR_V8_QUOTA_UNOBSERVED_IO");
|
||||
assert.equal(concurrentMutationRejected, "ERR_V8_QUOTA_CONCURRENT_IO");
|
||||
assert.equal(existsSync(join(root, "concurrent-write")), false);
|
||||
// A future stream/FileHandle writer must not silently evade the observation.
|
||||
assert.throws(() => createWriteStream(join(root, "unsupported")), {
|
||||
code: "ERR_V8_QUOTA_UNOBSERVED_IO",
|
||||
});
|
||||
report.calibration = quotaObserver.stop();
|
||||
Object.assign(report.calibration, { callbackMutationRejected, concurrentMutationRejected });
|
||||
assert.equal(report.calibration.peakBytes, unit * 2);
|
||||
assert.equal(report.calibration.finalBytes, 0);
|
||||
const refused = report.calibration.events.filter((event) => event.rejected);
|
||||
assert.equal(refused.length, 1);
|
||||
assert.equal(refused[0].writeOrdinal, 3);
|
||||
assert.equal(refused[0].beforeBytes, unit * 2);
|
||||
assert.equal(refused[0].afterBytes, unit * 2);
|
||||
assert.ok(refused[0].projectedBytes > quota);
|
||||
for (const event of report.calibration.events.filter((item) => item.operation === "rename")) {
|
||||
assert.equal(event.beforeBytes, event.afterBytes, "rename cannot duplicate measured bytes");
|
||||
}
|
||||
if (input.options) {
|
||||
phase = "import";
|
||||
const { prepareCoverageShards } =
|
||||
await import("../../../../scripts/ci/prepare-v8-coverage.mjs");
|
||||
phase = "subject";
|
||||
quotaObserver.start("subject");
|
||||
try {
|
||||
report.prepared = await prepareCoverageShards(input.options);
|
||||
} catch (error) {
|
||||
report.subjectError = {
|
||||
code: /^[A-Z][A-Z0-9_]+$/.test(error?.code || "") ? error.code : "SUBJECT_ERROR",
|
||||
reason: error?.message === "Packed coverage hash mismatch" ? "packed-hash-mismatch" : null,
|
||||
};
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
report.subject = quotaObserver.stop();
|
||||
report.outputExists = existsSync(input.options.outputDirectory);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
report.harnessError = {
|
||||
phase,
|
||||
code: /^[A-Z][A-Z0-9_]+$/.test(error?.code || "") ? error.code : "HARNESS_ERROR",
|
||||
};
|
||||
process.exitCode = 2;
|
||||
} finally {
|
||||
report.restored = quotaObserver.restore();
|
||||
report.namedExportsRestored = quotaObserver.isOriginal(writeFile, writeFileSync, rename, rm);
|
||||
// The observer writes its report only after restoration, outside measured payloads.
|
||||
writeFileSync(input.reportPath, JSON.stringify(report, null, 2) + "\n");
|
||||
}
|
||||
166
tests/unit/build/fixtures/v8-disk-quota-harness.mjs
Normal file
166
tests/unit/build/fixtures/v8-disk-quota-harness.mjs
Normal file
@@ -0,0 +1,166 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { readFile, readdir } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
export const monitoredPromises = [
|
||||
"writeFile",
|
||||
"rename",
|
||||
"rm",
|
||||
"unlink",
|
||||
"rmdir",
|
||||
"mkdir",
|
||||
"mkdtemp",
|
||||
];
|
||||
const digest = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
||||
|
||||
export async function snapshotProducerInputs(options) {
|
||||
const files = [];
|
||||
for (const [offset, manifest] of options.manifests.entries()) {
|
||||
const artifact = join(options.directory, `coverage-shard-${options.runAttempt}-${offset + 1}`);
|
||||
for (const path of [
|
||||
join(artifact, "manifest.json"),
|
||||
...manifest.inputs.map((input) => join(artifact, "raw", `${input.name}.gz`)),
|
||||
...(manifest.transformEvidence?.entries || []).map((entry) => join(artifact, entry.payload)),
|
||||
])
|
||||
files.push({ path, sha256: digest(await readFile(path)) });
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
export async function assertProducerInputsUnchanged(files) {
|
||||
for (const file of files)
|
||||
assert.equal(
|
||||
digest(await readFile(file.path)),
|
||||
file.sha256,
|
||||
"original manifests, gzip and generated proof bytes stay immutable"
|
||||
);
|
||||
}
|
||||
|
||||
export function runWithIndependentDiskQuota({ directory, quotaBytes, options = null }) {
|
||||
const id = randomUUID();
|
||||
const quotaRoot = join(directory, `quota-consumer-${id}`);
|
||||
mkdirSync(quotaRoot);
|
||||
const outputDirectory = join(quotaRoot, "prepared");
|
||||
const config = join(directory, `quota-config-${id}.json`);
|
||||
const reportPath = join(directory, `quota-report-${id}.json`);
|
||||
writeFileSync(
|
||||
config,
|
||||
JSON.stringify({ options: options ? { ...options, outputDirectory } : null, reportPath })
|
||||
);
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
"--import",
|
||||
fileURLToPath(new URL("./v8-disk-quota-preload.mjs", import.meta.url)),
|
||||
fileURLToPath(new URL("./v8-disk-quota-driver.mjs", import.meta.url)),
|
||||
config,
|
||||
],
|
||||
{
|
||||
cwd: directory,
|
||||
env: {
|
||||
...process.env,
|
||||
OMNI_V8_QUOTA_ROOT: quotaRoot,
|
||||
OMNI_V8_QUOTA_BYTES: String(quotaBytes),
|
||||
},
|
||||
encoding: "utf8",
|
||||
timeout: 30000,
|
||||
maxBuffer: 2 * 1024 * 1024,
|
||||
}
|
||||
);
|
||||
assert.equal(result.error, undefined, "quota subprocess must complete without a harness timeout");
|
||||
assert.equal(result.signal, null);
|
||||
assert.ok(
|
||||
existsSync(reportPath),
|
||||
`preload/driver must write independent evidence even on ENOSPC: ${JSON.stringify({
|
||||
status: result.status,
|
||||
signal: result.signal,
|
||||
stderr: String(result.stderr || "").slice(-8192),
|
||||
})}`
|
||||
);
|
||||
const report = JSON.parse(readFileSync(reportPath, "utf8"));
|
||||
assert.equal(report.harnessError, undefined, JSON.stringify(report.harnessError));
|
||||
assert.equal(report.restored, true);
|
||||
assert.equal(
|
||||
report.namedExportsRestored,
|
||||
true,
|
||||
"syncBuiltinESMExports must restore named bindings too"
|
||||
);
|
||||
assert.equal(report.calibration.finalBytes, 0);
|
||||
assert.equal(report.calibration.events.filter((event) => event.rejected === "ENOSPC").length, 1);
|
||||
if (report.subject) {
|
||||
assert.ok(report.subject.peakBytes <= quotaBytes);
|
||||
assert.ok(report.subject.events.every((event) => event.afterBytes <= quotaBytes));
|
||||
}
|
||||
return { ...result, report, outputDirectory, quotaRoot, reportPath };
|
||||
}
|
||||
|
||||
export async function assertBoundedPreparation(options, { expectedCount, generated = false }) {
|
||||
const originals = await snapshotProducerInputs(options);
|
||||
const largest = Math.max(
|
||||
...options.manifests.flatMap((manifest) => manifest.inputs.map((input) => input.bytes))
|
||||
);
|
||||
const result = runWithIndependentDiskQuota({
|
||||
directory: options.dir,
|
||||
options,
|
||||
quotaBytes: largest * 2,
|
||||
});
|
||||
await assertProducerInputsUnchanged(originals);
|
||||
if (result.status !== 0) {
|
||||
assert.equal(
|
||||
result.report.subjectError?.code,
|
||||
"ENOSPC",
|
||||
"RED must be real quota exhaustion, not admission/import failure"
|
||||
);
|
||||
const denied = result.report.subject.events.filter((event) => event.rejected === "ENOSPC");
|
||||
assert.equal(denied.length, 1);
|
||||
assert.equal(denied[0].operation, "writeFile");
|
||||
assert.equal(
|
||||
denied[0].writeOrdinal,
|
||||
3,
|
||||
"the third full materialization exceeds this fixture quota"
|
||||
);
|
||||
assert.equal(
|
||||
denied[0].beforeBytes,
|
||||
denied[0].afterBytes,
|
||||
"ENOSPC is injected before filesystem growth"
|
||||
);
|
||||
assert.equal(result.report.subject.finalBytes, 0, "the failed owned staging is removed");
|
||||
assert.equal(result.report.outputExists, false, "failed input is never published");
|
||||
}
|
||||
assert.equal(
|
||||
result.status,
|
||||
0,
|
||||
`consumer must fit its independent peak quota: ${JSON.stringify({
|
||||
subjectError: result.report.subjectError,
|
||||
peak: result.report.subject?.peakBytes,
|
||||
quota: largest * 2,
|
||||
rejected: result.report.subject?.events.filter((event) => event.rejected),
|
||||
})}`
|
||||
);
|
||||
assert.equal(result.report.prepared.inputFiles, 8);
|
||||
assert.equal(result.report.prepared.shards, 8);
|
||||
assert.equal(
|
||||
result.report.subject.events.some((event) => event.operation === "rename"),
|
||||
true
|
||||
);
|
||||
let contributed = 0;
|
||||
for (const name of await readdir(result.outputDirectory)) {
|
||||
const sample = JSON.parse(await readFile(join(result.outputDirectory, name), "utf8"));
|
||||
assert.ok(Array.isArray(sample.result), "only native V8 derivatives reach c8");
|
||||
for (const script of sample.result) contributed += script.functions[0].ranges[0].count;
|
||||
}
|
||||
assert.equal(contributed, expectedCount, "all eight original producer contributions survive");
|
||||
if (generated) {
|
||||
const proofBytes = options.manifests.reduce(
|
||||
(sum, manifest) => sum + manifest.transformEvidence.payloadBytes,
|
||||
0
|
||||
);
|
||||
assert.ok(proofBytes > 0);
|
||||
assert.equal(result.report.prepared.generatedEvidenceBytes, proofBytes);
|
||||
assert.ok(result.report.prepared.layoutProjection.projectedScriptSamples > 0);
|
||||
}
|
||||
}
|
||||
369
tests/unit/build/fixtures/v8-disk-quota-preload.mjs
Normal file
369
tests/unit/build/fixtures/v8-disk-quota-preload.mjs
Normal file
@@ -0,0 +1,369 @@
|
||||
import { AsyncLocalStorage } from "node:async_hooks";
|
||||
import fs from "node:fs";
|
||||
import promises from "node:fs/promises";
|
||||
import { syncBuiltinESMExports } from "node:module";
|
||||
import { isAbsolute, relative, resolve, sep } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
// Test-only preload. It observes real filesystem sizes; no producer telemetry is
|
||||
// used. The root is a fresh consumer-only directory, outside producer artifacts.
|
||||
const root = resolve(process.env.OMNI_V8_QUOTA_ROOT || ".");
|
||||
const quota = Number(process.env.OMNI_V8_QUOTA_BYTES);
|
||||
if (
|
||||
!isAbsolute(process.env.OMNI_V8_QUOTA_ROOT || "") ||
|
||||
!Number.isSafeInteger(quota) ||
|
||||
quota < 2
|
||||
) {
|
||||
throw new Error("Invalid isolated V8 quota fixture");
|
||||
}
|
||||
const original = { fs: { ...fs }, promises: { ...promises } };
|
||||
const replacements = [];
|
||||
const untouchedAccessors = [fs, promises].flatMap((owner) =>
|
||||
Object.entries(Object.getOwnPropertyDescriptors(owner))
|
||||
.filter(([, descriptor]) => "get" in descriptor || "set" in descriptor)
|
||||
.map(([name, descriptor]) => ({ owner, name, descriptor }))
|
||||
);
|
||||
const nativeCleanup = new AsyncLocalStorage();
|
||||
let active = false;
|
||||
let phase = "idle";
|
||||
let inFlight = false;
|
||||
let nativeDepth = 0;
|
||||
let events = [];
|
||||
let peakBytes = 0;
|
||||
let peakAllocatedBytes = 0;
|
||||
let writeOrdinal = 0;
|
||||
let restored = false;
|
||||
|
||||
function fail(code, syscall) {
|
||||
const error = new Error(code);
|
||||
error.code = code;
|
||||
error.syscall = syscall;
|
||||
if (code === "ENOSPC") error.errno = -28;
|
||||
return error;
|
||||
}
|
||||
|
||||
function replace(owner, name, value) {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(owner, name);
|
||||
if (!descriptor || !("value" in descriptor)) {
|
||||
throw fail("ERR_V8_QUOTA_UNSUPPORTED_DESCRIPTOR", name);
|
||||
}
|
||||
if (!replacements.some((entry) => entry.owner === owner && entry.name === name)) {
|
||||
replacements.push({ owner, name, descriptor });
|
||||
}
|
||||
Object.defineProperty(owner, name, { ...descriptor, value });
|
||||
}
|
||||
|
||||
function descriptorMatches({ owner, name, descriptor }) {
|
||||
const actual = Object.getOwnPropertyDescriptor(owner, name);
|
||||
return (
|
||||
actual &&
|
||||
Object.keys(actual).length === Object.keys(descriptor).length &&
|
||||
Object.keys(descriptor).every(
|
||||
(key) => Object.hasOwn(actual, key) && actual[key] === descriptor[key]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function pathOf(value) {
|
||||
if (value instanceof URL) return resolve(fileURLToPath(value));
|
||||
if (typeof value === "string" || Buffer.isBuffer(value)) return resolve(String(value));
|
||||
throw fail("ERR_V8_QUOTA_UNOBSERVED_IO", "path");
|
||||
}
|
||||
|
||||
function owned(value) {
|
||||
const path = pathOf(value);
|
||||
const part = relative(root, path);
|
||||
if (part === ".." || part.startsWith(`..${sep}`) || isAbsolute(part)) {
|
||||
throw fail("ERR_V8_QUOTA_OUTSIDE_ROOT", "path");
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
function measured(path = root) {
|
||||
let stat;
|
||||
try {
|
||||
stat = original.fs.lstatSync(path);
|
||||
} catch (error) {
|
||||
if (error.code === "ENOENT") return { bytes: 0, allocatedBytes: 0, files: 0 };
|
||||
throw error;
|
||||
}
|
||||
if (stat.isSymbolicLink() || (!stat.isFile() && !stat.isDirectory())) {
|
||||
throw fail("ERR_V8_QUOTA_SPECIAL_FILE", "measure");
|
||||
}
|
||||
if (stat.isFile())
|
||||
return {
|
||||
bytes: stat.size,
|
||||
allocatedBytes: Number.isSafeInteger(stat.blocks) ? stat.blocks * 512 : null,
|
||||
files: 1,
|
||||
};
|
||||
const result = { bytes: 0, allocatedBytes: 0, files: 0 };
|
||||
for (const name of original.fs.readdirSync(path)) {
|
||||
const child = measured(resolve(path, name));
|
||||
result.bytes += child.bytes;
|
||||
result.files += child.files;
|
||||
result.allocatedBytes =
|
||||
result.allocatedBytes === null || child.allocatedBytes === null
|
||||
? null
|
||||
: result.allocatedBytes + child.allocatedBytes;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function dataBytes(value, options) {
|
||||
if (typeof value === "string")
|
||||
return Buffer.byteLength(
|
||||
value,
|
||||
typeof options === "string" ? options : options?.encoding || "utf8"
|
||||
);
|
||||
if (Buffer.isBuffer(value) || ArrayBuffer.isView(value)) return value.byteLength;
|
||||
throw fail("ERR_V8_QUOTA_UNOBSERVED_IO", "writeFile-data");
|
||||
}
|
||||
|
||||
function projectedBytes(name, args, before) {
|
||||
const path = owned(args[0]);
|
||||
if (name === "writeFile") {
|
||||
const previous = measured(path).bytes;
|
||||
const flag = typeof args[2] === "object" ? args[2]?.flag || "w" : "w";
|
||||
if (typeof flag !== "string" || !/^(?:w|wx|a|ax|as|as\+|a\+|ax\+|w\+|wx\+|r\+)$/.test(flag)) {
|
||||
throw fail("ERR_V8_QUOTA_UNOBSERVED_IO", "writeFile-flag");
|
||||
}
|
||||
const incoming = dataBytes(args[1], args[2]);
|
||||
const next = flag.startsWith("a")
|
||||
? previous + incoming
|
||||
: flag.startsWith("w")
|
||||
? incoming
|
||||
: Math.max(previous, incoming);
|
||||
return before.bytes - previous + next;
|
||||
}
|
||||
if (name === "rename") {
|
||||
const destination = owned(args[1]);
|
||||
// Both source and destination are in the same monitored tree. A rename does
|
||||
// not duplicate its contents; replacing an existing target can only shrink it.
|
||||
return path === destination ? before.bytes : before.bytes - measured(destination).bytes;
|
||||
}
|
||||
if (["rm", "unlink", "rmdir"].includes(name)) return before.bytes - measured(path).bytes;
|
||||
if (["mkdir", "mkdtemp"].includes(name)) return before.bytes;
|
||||
throw fail("ERR_V8_QUOTA_UNOBSERVED_IO", name);
|
||||
}
|
||||
|
||||
function beforeOperation(name, args) {
|
||||
if (inFlight) throw fail("ERR_V8_QUOTA_CONCURRENT_IO", name);
|
||||
const before = measured();
|
||||
const next = projectedBytes(name, args, before);
|
||||
const event = {
|
||||
phase,
|
||||
ordinal: events.length + 1,
|
||||
operation: name,
|
||||
writeOrdinal: name === "writeFile" ? ++writeOrdinal : null,
|
||||
beforeBytes: before.bytes,
|
||||
projectedBytes: next,
|
||||
quotaBytes: quota,
|
||||
};
|
||||
events.push(event);
|
||||
if (next > quota) {
|
||||
const after = measured();
|
||||
Object.assign(event, { rejected: "ENOSPC", afterBytes: after.bytes, afterFiles: after.files });
|
||||
throw fail("ENOSPC", name);
|
||||
}
|
||||
inFlight = event;
|
||||
return event;
|
||||
}
|
||||
|
||||
function afterOperation(event, error, release = true) {
|
||||
try {
|
||||
const after = measured();
|
||||
Object.assign(event, {
|
||||
afterBytes: after.bytes,
|
||||
afterFiles: after.files,
|
||||
afterAllocatedBytes: after.allocatedBytes,
|
||||
...(error ? { error: error.code || "IO_ERROR" } : {}),
|
||||
});
|
||||
peakBytes = Math.max(peakBytes, after.bytes);
|
||||
if (after.allocatedBytes !== null)
|
||||
peakAllocatedBytes = Math.max(peakAllocatedBytes, after.allocatedBytes);
|
||||
if (after.bytes > quota) throw fail("ERR_V8_QUOTA_OVERSHOOT", event.operation);
|
||||
} finally {
|
||||
if (release) inFlight = false;
|
||||
}
|
||||
}
|
||||
|
||||
function invokeNativeCleanup(name, args) {
|
||||
const scope = nativeCleanup.getStore();
|
||||
// Only Node's callback unlink/rmdir descended from the observed promise rm
|
||||
// may join that operation. The token is never exposed to caller callbacks.
|
||||
if (!scope?.open || scope.event !== inFlight || !["unlink", "rmdir"].includes(name)) {
|
||||
throw fail("ERR_V8_QUOTA_UNOBSERVED_IO", name);
|
||||
}
|
||||
const path = owned(args[0]);
|
||||
const member = relative(scope.path, path);
|
||||
if (member === ".." || member.startsWith(`..${sep}`) || isAbsolute(member)) {
|
||||
throw fail("ERR_V8_QUOTA_OUTSIDE_CLEANUP", name);
|
||||
}
|
||||
const callback = args.at(-1);
|
||||
if (typeof callback !== "function") throw fail("ERR_V8_QUOTA_UNOBSERVED_IO", name);
|
||||
const before = measured();
|
||||
const event = {
|
||||
phase,
|
||||
ordinal: events.length + 1,
|
||||
operation: name,
|
||||
nativeCleanup: true,
|
||||
parentOrdinal: scope.event.ordinal,
|
||||
cleanupRoot: relative(root, scope.path),
|
||||
member,
|
||||
beforeBytes: before.bytes,
|
||||
projectedBytes: before.bytes - measured(path).bytes,
|
||||
quotaBytes: quota,
|
||||
writeOrdinal: null,
|
||||
};
|
||||
events.push(event);
|
||||
const observe = (error) => {
|
||||
afterOperation(event, error, false);
|
||||
if (event.afterBytes > event.beforeBytes) throw fail("ERR_V8_QUOTA_CLEANUP_GROWTH", name);
|
||||
};
|
||||
try {
|
||||
return original.fs[name](...args.slice(0, -1), function (...values) {
|
||||
observe(values[0]);
|
||||
return callback.apply(this, values);
|
||||
});
|
||||
} catch (error) {
|
||||
observe(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
const supported = ["writeFile", "rename", "rm", "unlink", "rmdir", "mkdir", "mkdtemp"];
|
||||
for (const name of supported) {
|
||||
if (typeof original.promises[name] === "function")
|
||||
replace(promises, name, async (...args) => {
|
||||
if (!active || nativeDepth) return original.promises[name](...args);
|
||||
const event = beforeOperation(name, args);
|
||||
const cleanup = name === "rm" ? { event, path: owned(args[0]), open: true } : null;
|
||||
let error;
|
||||
try {
|
||||
if (cleanup)
|
||||
return await nativeCleanup.run(cleanup, () => original.promises[name](...args));
|
||||
return await original.promises[name](...args);
|
||||
} catch (failure) {
|
||||
error = failure;
|
||||
throw failure;
|
||||
} finally {
|
||||
if (cleanup) cleanup.open = false;
|
||||
afterOperation(event, error);
|
||||
}
|
||||
});
|
||||
const syncName = `${name}Sync`;
|
||||
if (typeof original.fs[syncName] === "function")
|
||||
replace(fs, syncName, (...args) => {
|
||||
if (!active || nativeDepth) return original.fs[syncName](...args);
|
||||
const event = beforeOperation(name, args);
|
||||
let error;
|
||||
try {
|
||||
// writeFileSync internally calls public openSync/writeSync; do not count
|
||||
// the same real write twice or mistake that implementation for a bypass.
|
||||
nativeDepth++;
|
||||
return original.fs[syncName](...args);
|
||||
} catch (failure) {
|
||||
error = failure;
|
||||
throw failure;
|
||||
} finally {
|
||||
nativeDepth--;
|
||||
afterOperation(event, error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Unknown mutation mechanisms fail closed instead of producing a false zero.
|
||||
// Add a real-I/O calibration before adopting one in the production consumer.
|
||||
const unsupported = ["appendFile", "copyFile", "cp", "truncate", "ftruncate", "link", "symlink"];
|
||||
for (const name of [...supported, ...unsupported]) {
|
||||
if (typeof original.fs[name] === "function")
|
||||
replace(fs, name, (...args) => {
|
||||
if (active && !nativeDepth) return invokeNativeCleanup(name, args);
|
||||
return original.fs[name](...args);
|
||||
});
|
||||
}
|
||||
for (const name of unsupported) {
|
||||
if (typeof original.promises[name] === "function")
|
||||
replace(promises, name, async (...args) => {
|
||||
if (active && !nativeDepth) throw fail("ERR_V8_QUOTA_UNOBSERVED_IO", name);
|
||||
return original.promises[name](...args);
|
||||
});
|
||||
const syncName = `${name}Sync`;
|
||||
if (typeof original.fs[syncName] === "function")
|
||||
replace(fs, syncName, (...args) => {
|
||||
if (active && !nativeDepth) throw fail("ERR_V8_QUOTA_UNOBSERVED_IO", syncName);
|
||||
return original.fs[syncName](...args);
|
||||
});
|
||||
}
|
||||
for (const [owner, saved, name] of [
|
||||
[fs, original.fs, "open"],
|
||||
[fs, original.fs, "openSync"],
|
||||
[promises, original.promises, "open"],
|
||||
]) {
|
||||
replace(owner, name, (...args) => {
|
||||
if (active && !nativeDepth && !["r", "rs", "sr", fs.constants.O_RDONLY].includes(args[1])) {
|
||||
throw fail("ERR_V8_QUOTA_UNOBSERVED_IO", name);
|
||||
}
|
||||
return saved[name](...args);
|
||||
});
|
||||
}
|
||||
for (const name of ["createWriteStream", "write", "writeSync", "writev", "writevSync"]) {
|
||||
replace(fs, name, (...args) => {
|
||||
if (active && !nativeDepth) throw fail("ERR_V8_QUOTA_UNOBSERVED_IO", name);
|
||||
return original.fs[name](...args);
|
||||
});
|
||||
}
|
||||
syncBuiltinESMExports();
|
||||
|
||||
export const quotaObserver = {
|
||||
start(name) {
|
||||
if (active || inFlight || measured().bytes !== 0)
|
||||
throw fail("ERR_V8_QUOTA_DIRTY_START", "start");
|
||||
active = true;
|
||||
phase = name;
|
||||
events = [];
|
||||
peakBytes = 0;
|
||||
peakAllocatedBytes = 0;
|
||||
writeOrdinal = 0;
|
||||
},
|
||||
stop() {
|
||||
const final = measured();
|
||||
active = false;
|
||||
return {
|
||||
phase,
|
||||
quotaBytes: quota,
|
||||
peakBytes,
|
||||
peakAllocatedBytes,
|
||||
finalBytes: final.bytes,
|
||||
finalFiles: final.files,
|
||||
events: structuredClone(events),
|
||||
};
|
||||
},
|
||||
restore() {
|
||||
active = false;
|
||||
// Restore exactly the descriptors replaced above. Other Node exports may be
|
||||
// getter-only functions (for example Utf8Stream); they were never patched.
|
||||
for (const { owner, name, descriptor } of replacements) {
|
||||
Object.defineProperty(owner, name, descriptor);
|
||||
}
|
||||
syncBuiltinESMExports();
|
||||
restored =
|
||||
replacements.every(descriptorMatches) &&
|
||||
untouchedAccessors.every(descriptorMatches) &&
|
||||
supported.every(
|
||||
(name) =>
|
||||
promises[name] === original.promises[name] &&
|
||||
fs[`${name}Sync`] === original.fs[`${name}Sync`]
|
||||
);
|
||||
return restored;
|
||||
},
|
||||
isOriginal(asyncWrite, syncWrite, asyncRename, asyncRm) {
|
||||
return (
|
||||
restored &&
|
||||
asyncWrite === original.promises.writeFile &&
|
||||
syncWrite === original.fs.writeFileSync &&
|
||||
asyncRename === original.promises.rename &&
|
||||
asyncRm === original.promises.rm
|
||||
);
|
||||
},
|
||||
};
|
||||
@@ -1,9 +1,8 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { buildSync } from "esbuild";
|
||||
|
||||
// Regression guard for the Node 24 MCP startup crash caused by an ESM init cycle.
|
||||
@@ -37,9 +36,12 @@ test("MCP server bundle imports successfully on Node 24", () => {
|
||||
"MCP bundle must not contain the auth ↔ googApiKeyAuth circular initializer"
|
||||
);
|
||||
|
||||
const importScript = `await import(${JSON.stringify(pathToFileURL(outFile).href)});`;
|
||||
// A file-based entry lets the bundle's workers inherit valid Node arguments
|
||||
// while the child still runs under the same CI coverage preloads.
|
||||
const importFile = join(outDir, "verify-import.mjs");
|
||||
writeFileSync(importFile, 'import "./server.js";\n');
|
||||
assert.doesNotThrow(() => {
|
||||
execFileSync(process.execPath, ["--input-type=module", "-e", importScript], {
|
||||
execFileSync(process.execPath, [importFile], {
|
||||
cwd: repoRoot,
|
||||
env: {
|
||||
...process.env,
|
||||
|
||||
@@ -54,10 +54,20 @@ test(
|
||||
assert.match(collect.run!, /--temp-directory=coverage-shard/);
|
||||
assert.match(collect.run!, /npm run test:unit:ci:shard/);
|
||||
assert.doesNotMatch(collect.run!, /\|\| true|exit 0|--exclude=src/);
|
||||
const upload = steps.find((step) => step.name === "Upload raw shard coverage")!;
|
||||
assert.equal(upload.if, "always()");
|
||||
assert.equal(upload.with?.path, "coverage-shard/*.json");
|
||||
assert.equal(upload.with?.["if-no-files-found"], "error");
|
||||
const packageIndex = steps.findIndex(
|
||||
(step) => step.name === "Package successful shard coverage"
|
||||
);
|
||||
const complete = steps[packageIndex + 1];
|
||||
assert.ok(packageIndex > steps.indexOf(collect));
|
||||
assert.equal(complete.name, "Upload complete shard coverage");
|
||||
assert.equal(complete.if, undefined, "complete evidence is published only on success");
|
||||
assert.equal(complete.with?.path, "coverage-shard-upload/");
|
||||
assert.equal(complete.with?.["if-no-files-found"], "error");
|
||||
const failed = steps.find((step) => step.name === "Preserve raw coverage from failed shards")!;
|
||||
assert.equal(failed.if, "failure()");
|
||||
assert.equal(failed.with?.path, "coverage-shard/*.json");
|
||||
assert.equal(failed.with?.["if-no-files-found"], "error");
|
||||
assert.match(String(failed.with?.name), /^failed-unit-coverage-/);
|
||||
const source = read(".github/workflows/ci.yml");
|
||||
assert.match(source, /shard: \[1, 2, 3, 4, 5, 6, 7, 8\]/);
|
||||
assert.match(source, /--check-coverage/);
|
||||
|
||||
66
tests/unit/build/v8-disk-quota.test.mjs
Normal file
66
tests/unit/build/v8-disk-quota.test.mjs
Normal file
@@ -0,0 +1,66 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import test from "node:test";
|
||||
import {
|
||||
monitoredPromises,
|
||||
runWithIndependentDiskQuota,
|
||||
} from "./fixtures/v8-disk-quota-harness.mjs";
|
||||
|
||||
test("V8 disk quota calibration observes promise and sync writes before the third write", async (t) => {
|
||||
const directory = await mkdtemp(join(tmpdir(), "v8-quota-calibration-"));
|
||||
t.after(() => rm(directory, { recursive: true, force: true }));
|
||||
const result = runWithIndependentDiskQuota({ directory, quotaBytes: 8192 });
|
||||
assert.equal(result.status, 0);
|
||||
assert.equal(result.report.calibration.peakBytes, 8192);
|
||||
assert.equal(result.report.calibration.finalBytes, 0);
|
||||
const denied = result.report.calibration.events.find((event) => event.rejected === "ENOSPC");
|
||||
assert.equal(denied.operation, "writeFile");
|
||||
assert.equal(denied.writeOrdinal, 3);
|
||||
assert.equal(denied.beforeBytes, 8192);
|
||||
assert.equal(denied.afterBytes, 8192);
|
||||
assert.equal(denied.projectedBytes, 8193);
|
||||
assert.equal(result.report.calibration.callbackMutationRejected, "ERR_V8_QUOTA_UNOBSERVED_IO");
|
||||
assert.equal(result.report.calibration.concurrentMutationRejected, "ERR_V8_QUOTA_CONCURRENT_IO");
|
||||
const native = result.report.calibration.events.filter((event) => event.nativeCleanup);
|
||||
assert.ok(native.length > 0, "the platform's native rm cleanup was observed");
|
||||
for (const event of native) {
|
||||
const parent = result.report.calibration.events.find(
|
||||
(item) => item.ordinal === event.parentOrdinal
|
||||
);
|
||||
assert.equal(parent.operation, "rm");
|
||||
assert.ok(["unlink", "rmdir"].includes(event.operation));
|
||||
assert.ok(event.afterBytes <= event.beforeBytes);
|
||||
assert.ok(event.afterBytes <= result.report.calibration.quotaBytes);
|
||||
assert.ok(event.member !== ".." && !event.member.startsWith("../"));
|
||||
}
|
||||
});
|
||||
|
||||
test("V8 disk quota seam covers the actual preparer's fs/promises imports", async () => {
|
||||
const source = await readFile(
|
||||
new URL("../../../scripts/ci/prepare-v8-coverage.mjs", import.meta.url),
|
||||
"utf8"
|
||||
);
|
||||
const match = source.match(/import\s*\{([^}]+)\}\s*from\s*"node:fs\/promises"/);
|
||||
assert.ok(match, "a new I/O import style requires an explicit quota calibration");
|
||||
const readOnly = new Set([
|
||||
"access",
|
||||
"lstat",
|
||||
"stat",
|
||||
"statfs",
|
||||
"readFile",
|
||||
"readdir",
|
||||
"realpath",
|
||||
]);
|
||||
const covered = new Set(monitoredPromises);
|
||||
for (const declaration of match[1].split(",")) {
|
||||
const name = declaration.trim().split(/\s+as\s+/)[0];
|
||||
assert.ok(readOnly.has(name) || covered.has(name), `uncalibrated filesystem API ${name}`);
|
||||
}
|
||||
assert.doesNotMatch(
|
||||
source,
|
||||
/import\s+\w+\s+from\s+["']node:fs(?:\/promises)?["']/,
|
||||
"a new namespace I/O strategy needs its own coverage contract"
|
||||
);
|
||||
});
|
||||
589
tests/unit/build/v8-native-worker-layout.test.mjs
Normal file
589
tests/unit/build/v8-native-worker-layout.test.mjs
Normal file
@@ -0,0 +1,589 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import test from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const root = new URL("../../../", import.meta.url);
|
||||
const preload = new URL("scripts/ci/native-worker-v8-layout.mjs", root);
|
||||
const worker = "src/lib/usage/callLogArtifactWorker.ts";
|
||||
const writer = "src/lib/usage/callLogArtifactWriter.ts";
|
||||
const artifact = "src/lib/usage/callLogArtifacts.ts";
|
||||
|
||||
async function probe(t, tsx, usePreload) {
|
||||
const coverageDirectory = await mkdtemp(join(tmpdir(), "native-worker-coverage-"));
|
||||
t.after(() => rm(coverageDirectory, { recursive: true, force: true }));
|
||||
execFileSync(
|
||||
process.execPath,
|
||||
[
|
||||
"--import",
|
||||
tsx,
|
||||
"--input-type=module",
|
||||
"--eval",
|
||||
`
|
||||
import { once } from "node:events";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { createRequire } from "node:module";
|
||||
import { join } from "node:path";
|
||||
import { Worker } from "node:worker_threads";
|
||||
const entry = "./${artifact}";
|
||||
const common = process.execArgv.includes("tsx") ? createRequire(import.meta.url)(entry) : null;
|
||||
const library = await import(entry);
|
||||
if (common && ${usePreload} && common.getCallLogsDir !== library.getCallLogsDir) {
|
||||
throw new Error("Require and import loaded distinct copies");
|
||||
}
|
||||
const directory = library.getCallLogsDir();
|
||||
await import("./${writer}");
|
||||
const worker = new Worker(new URL("./${worker}", import.meta.url), { execArgv: [] });
|
||||
const reply = once(worker, "message");
|
||||
const artifact = {
|
||||
schemaVersion: 5,
|
||||
summary: { timestamp: "2026-11-19T12:00:00.000Z", id: "coverage-fixture" },
|
||||
requestBody: { message: "native worker" },
|
||||
};
|
||||
worker.postMessage({ id: 1, artifact, environment: { dataDir: process.env.DATA_DIR } });
|
||||
try {
|
||||
const [{ id, result }] = await reply;
|
||||
if (id !== 1 || !result) throw new Error("Unexpected worker reply");
|
||||
const text = await readFile(join(directory, result.relPath), "utf8");
|
||||
if (text !== JSON.stringify(artifact)) throw new Error("Unexpected worker output");
|
||||
} finally {
|
||||
await worker.terminate();
|
||||
}
|
||||
`,
|
||||
],
|
||||
{
|
||||
cwd: root,
|
||||
env: {
|
||||
...process.env,
|
||||
DATA_DIR: coverageDirectory,
|
||||
NODE_OPTIONS: usePreload ? `--import=${preload.href}` : "",
|
||||
NODE_V8_COVERAGE: coverageDirectory,
|
||||
},
|
||||
stdio: "pipe",
|
||||
timeout: 60_000,
|
||||
}
|
||||
);
|
||||
|
||||
const reports = [];
|
||||
for (const file of await readdir(coverageDirectory)) {
|
||||
if (file.startsWith("coverage-") && file.endsWith(".json")) {
|
||||
reports.push(JSON.parse(await readFile(join(coverageDirectory, file), "utf8")));
|
||||
}
|
||||
}
|
||||
const find = (reports, path) => {
|
||||
const url = new URL(path, root).href;
|
||||
const filename = fileURLToPath(url);
|
||||
return reports.flatMap((report) =>
|
||||
report.result
|
||||
.filter((script) => script.url === url || script.url === filename)
|
||||
.map((script) => ({
|
||||
url: script.url,
|
||||
mapped: Boolean(
|
||||
report["source-map-cache"]?.[url] ?? report["source-map-cache"]?.[filename]
|
||||
),
|
||||
functions: script.functions,
|
||||
}))
|
||||
);
|
||||
};
|
||||
const workerReport = reports.find((report) => find([report], worker).length);
|
||||
assert.ok(workerReport, "the real worker must supply its own coverage");
|
||||
const parentReports = reports.filter((report) => report !== workerReport);
|
||||
return { find, workerReport, parentReports };
|
||||
}
|
||||
|
||||
for (const tsx of ["tsx/esm", "tsx"]) {
|
||||
test(`${tsx}: coverage includes native worker and caller offsets in the same layout`, async (t) => {
|
||||
const { find, workerReport, parentReports } = await probe(t, tsx, true);
|
||||
const runtimeSources = workerReport.result.filter((script) =>
|
||||
script.url.startsWith(new URL("src/", root).href)
|
||||
);
|
||||
assert.ok(runtimeSources.length > 1, "native worker dependency graph must be exercised");
|
||||
for (const { url, functions } of runtimeSources) {
|
||||
assert.equal(workerReport["source-map-cache"]?.[url], undefined, url);
|
||||
if (url === new URL(worker, root).href) continue;
|
||||
const parent = find(parentReports, url);
|
||||
assert.equal(parent.length, 1, url);
|
||||
assert.equal(parent[0].mapped, false, url);
|
||||
const sourceLength = (await readFile(new URL(url), "utf8")).length;
|
||||
for (const sample of [{ url, functions }, parent[0]]) {
|
||||
const runtimeLength = sourceLength + `\n\n//# sourceURL=${sample.url}`.length;
|
||||
const [{ startOffset, endOffset }] = sample.functions[0].ranges;
|
||||
assert.deepEqual([startOffset, endOffset], [0, runtimeLength], sample.url);
|
||||
for (const fn of sample.functions.slice(1)) {
|
||||
assert.ok(
|
||||
fn.ranges.every((range) => range.endOffset <= sourceLength),
|
||||
url
|
||||
);
|
||||
}
|
||||
}
|
||||
if (url === new URL(artifact, root).href) {
|
||||
const getter = (sample) => sample.find((fn) => fn.functionName === "getCallLogsDir");
|
||||
const [workerCall] = getter(functions).ranges;
|
||||
const [parentCall] = getter(parent[0].functions).ranges;
|
||||
assert.deepEqual(parentCall, workerCall, "both call sites count at the same coordinates");
|
||||
assert.equal(parentCall.count, 1);
|
||||
}
|
||||
}
|
||||
assert.equal(find(parentReports, writer)[0].mapped, true, "other files still use tsx");
|
||||
});
|
||||
}
|
||||
|
||||
test("without the preload, tsx and the worker still exhibit distinct layouts", async (t) => {
|
||||
const { find, workerReport, parentReports } = await probe(t, "tsx/esm", false);
|
||||
const [parent] = find(parentReports, artifact);
|
||||
const [native] = find([workerReport], artifact);
|
||||
assert.equal(parent.mapped, true);
|
||||
assert.equal(native.mapped, false);
|
||||
assert.notEqual(parent.functions[0].ranges[0].endOffset, native.functions[0].ranges[0].endOffset);
|
||||
});
|
||||
|
||||
test("CLI and maintenance scripts keep the Node coverage layout through tsx registration", async (t) => {
|
||||
const directory = await mkdtemp(join(tmpdir(), "native-cli-coverage-"));
|
||||
t.after(() => rm(directory, { recursive: true, force: true }));
|
||||
const entry = new URL("bin/cli/utils/cliToken.mjs", root).href;
|
||||
const aliasResolver = new URL("bin/aliasResolver.mjs", root).href;
|
||||
const translation = new URL("scripts/i18n/run-translation.mjs", root).href;
|
||||
const lifecycle = new URL("scripts/check/check-model-lifecycle.mjs", root).href;
|
||||
const dependency = new URL("bin/cli/data-dir.mjs", root).href;
|
||||
const tray = new URL("bin/cli/runtime/trayRuntime.ts", root).href;
|
||||
const trayEntry = new URL("bin/cli/tray/traySystray.mjs", root).href;
|
||||
const windows = new URL("src/shared/platform/windowsProcess.ts", root).href;
|
||||
const cliTsx = new URL("scripts/ci/native-cli-v8-tsx.mjs", root).href;
|
||||
const query = `${entry}?coverage-fixture=1`;
|
||||
const required = new Set([
|
||||
entry,
|
||||
query,
|
||||
dependency,
|
||||
tray,
|
||||
trayEntry,
|
||||
windows,
|
||||
aliasResolver,
|
||||
translation,
|
||||
lifecycle,
|
||||
]);
|
||||
const imports = [entry, query, tray, trayEntry, aliasResolver, translation, lifecycle]
|
||||
.map((url) => `await import(${JSON.stringify(url)});`)
|
||||
.join("\n");
|
||||
|
||||
async function record(mode, args, code) {
|
||||
const coverage = join(directory, mode);
|
||||
await mkdir(coverage);
|
||||
execFileSync(process.execPath, [...args, "--input-type=module", "--eval", code], {
|
||||
cwd: root,
|
||||
env: {
|
||||
PATH: process.env.PATH,
|
||||
DATA_DIR: directory,
|
||||
NODE_ENV: "test",
|
||||
NODE_OPTIONS: mode === "native" ? "" : `--import=${preload.href}`,
|
||||
NODE_V8_COVERAGE: coverage,
|
||||
},
|
||||
stdio: "pipe",
|
||||
timeout: 60_000,
|
||||
});
|
||||
const samples = new Map();
|
||||
for (const file of await readdir(coverage)) {
|
||||
if (!file.endsWith(".json")) continue;
|
||||
const report = JSON.parse(await readFile(join(coverage, file), "utf8"));
|
||||
for (const script of report.result) {
|
||||
const projectTs =
|
||||
script.url.startsWith(root.href) &&
|
||||
script.url.endsWith(".ts") &&
|
||||
!script.url.includes("/node_modules/");
|
||||
if (!required.has(script.url) && !projectTs) continue;
|
||||
assert.equal(Boolean(report["source-map-cache"]?.[script.url]), false, script.url);
|
||||
assert.equal(samples.has(script.url), false, script.url);
|
||||
samples.set(script.url, script.functions);
|
||||
}
|
||||
}
|
||||
for (const url of required) assert.ok(samples.has(url), `${mode} did not cover ${url}`);
|
||||
return samples;
|
||||
}
|
||||
|
||||
const native = await record("native", [], imports);
|
||||
assert.deepEqual(await record("native-preload", [], imports), native);
|
||||
assert.deepEqual(await record("early", ["--import", "tsx/esm"], imports), native);
|
||||
assert.deepEqual(
|
||||
await record("late", [], `await import(${JSON.stringify(cliTsx)});\n${imports}`),
|
||||
native
|
||||
);
|
||||
});
|
||||
|
||||
test("the global-install alias fixtures keep native coverage after late tsx registration", async (t) => {
|
||||
const coverage = await mkdtemp(join(tmpdir(), "late-tsx-alias-coverage-"));
|
||||
t.after(() => rm(coverage, { recursive: true, force: true }));
|
||||
const driver = "tests/unit/cli/alias-resolver-12073.test.ts";
|
||||
const source = new URL("bin/aliasResolver.mjs", root);
|
||||
const sourceLength = (await readFile(source, "utf8")).length;
|
||||
const env = {
|
||||
...process.env,
|
||||
NODE_OPTIONS: `--import=${preload.href}`,
|
||||
NODE_V8_COVERAGE: coverage,
|
||||
OMNIROUTE_CI_SYNC_TSX_CACHE: coverage,
|
||||
};
|
||||
delete env.NODE_TEST_CONTEXT;
|
||||
execFileSync(
|
||||
process.execPath,
|
||||
[
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
"--test",
|
||||
"--test-name-pattern",
|
||||
"keeps global-install-style alias imports working|retains module.register",
|
||||
driver,
|
||||
],
|
||||
{ cwd: root, env, stdio: "pipe", timeout: 60_000 }
|
||||
);
|
||||
|
||||
let evaluated = 0;
|
||||
for (const file of await readdir(coverage)) {
|
||||
if (!file.endsWith(".json")) continue;
|
||||
const report = JSON.parse(await readFile(join(coverage, file), "utf8"));
|
||||
const sample = report.result.find(({ url }) => url === source.href);
|
||||
if (!sample || !report.result.some(({ url }) => url.endsWith("/[eval1]"))) continue;
|
||||
evaluated++;
|
||||
assert.equal(Boolean(report["source-map-cache"]?.[source.href]), false, source.href);
|
||||
const [{ startOffset, endOffset }] = sample.functions[0].ranges;
|
||||
assert.deepEqual([startOffset, endOffset], [0, sourceLength]);
|
||||
}
|
||||
assert.equal(evaluated, 2, "both actual late-TSX launchers must emit coverage");
|
||||
});
|
||||
|
||||
test("the CLI doctor and native tray diagnostic share coverage coordinates", async (t) => {
|
||||
const coverage = await mkdtemp(join(tmpdir(), "doctor-native-coverage-"));
|
||||
t.after(() => rm(coverage, { recursive: true, force: true }));
|
||||
const atomicPreload = new URL("scripts/ci/atomic-tsx-cache.mjs", root);
|
||||
const tsxDriver = new URL("tests/unit/cli-doctor-prebuilt-native-binary-10083.test.ts", root);
|
||||
const trayDriver = new URL("tests/unit/cli/tray-failure-diagnostics-17315.test.ts", root);
|
||||
const tray = new URL("bin/cli/tray/traySystray.mjs", root);
|
||||
const source = new URL("src/lib/cli-helper/doctor/checks.ts", root);
|
||||
const sourceSize = (await readFile(source, "utf8")).length;
|
||||
const env = {
|
||||
...process.env,
|
||||
TMPDIR: coverage,
|
||||
NODE_OPTIONS: `--import=${atomicPreload.href} --import=${preload.href}`,
|
||||
NODE_V8_COVERAGE: coverage,
|
||||
OMNIROUTE_CI_SYNC_TSX_CACHE: coverage,
|
||||
};
|
||||
delete env.NODE_TEST_CONTEXT;
|
||||
execFileSync(
|
||||
process.execPath,
|
||||
[
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
"--test",
|
||||
"--test-name-pattern",
|
||||
"doctor finds a prebuilt|doctor surfaces the persisted tray failure",
|
||||
fileURLToPath(tsxDriver),
|
||||
fileURLToPath(trayDriver),
|
||||
],
|
||||
{ cwd: root, env, stdio: "pipe", timeout: 90_000 }
|
||||
);
|
||||
|
||||
const producers = new Set();
|
||||
for (const file of await readdir(coverage)) {
|
||||
if (!file.endsWith(".json")) continue;
|
||||
const report = JSON.parse(await readFile(join(coverage, file), "utf8"));
|
||||
const script = report.result.find(({ url }) => url === source.href);
|
||||
if (!script) continue;
|
||||
const urls = new Set(report.result.map(({ url }) => url));
|
||||
if (urls.has(tsxDriver.href)) {
|
||||
assert.ok(urls.has(preload.href));
|
||||
producers.add("tsx doctor");
|
||||
} else if (urls.has(tray.href)) {
|
||||
assert.equal(urls.has(preload.href), false);
|
||||
producers.add("native tray doctor");
|
||||
}
|
||||
assert.equal(Boolean(report["source-map-cache"]?.[source.href]), false, source.href);
|
||||
assert.equal(script.functions[0].ranges[0].startOffset, 0);
|
||||
assert.equal(
|
||||
script.functions[0].ranges[0].endOffset,
|
||||
sourceSize + `\n\n//# sourceURL=${source.href}`.length
|
||||
);
|
||||
}
|
||||
assert.deepEqual(producers, new Set(["tsx doctor", "native tray doctor"]));
|
||||
});
|
||||
|
||||
test("the real CLI keeps native coverage through the clean-environment launcher", async (t) => {
|
||||
const coverage = await mkdtemp(join(tmpdir(), "clean-cli-coverage-"));
|
||||
t.after(() => rm(coverage, { recursive: true, force: true }));
|
||||
execFileSync(
|
||||
process.execPath,
|
||||
[
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
"--input-type=module",
|
||||
"--eval",
|
||||
`
|
||||
import assert from "node:assert/strict";
|
||||
import { BIN, cleanEnv, layout, rm, runCli } from "./tests/_helpers/cliCwdEnvHarness.ts";
|
||||
const fixture = layout();
|
||||
try {
|
||||
const env = cleanEnv(fixture.home);
|
||||
assert.equal(env.NODE_OPTIONS, undefined);
|
||||
const result = await runCli(BIN, ["--help"], fixture.cwd, env);
|
||||
assert.equal(result.code, 0, result.stderr);
|
||||
} finally {
|
||||
rm(fixture.tmp);
|
||||
}
|
||||
`,
|
||||
],
|
||||
{
|
||||
cwd: root,
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_OPTIONS: `--import=${preload.href}`,
|
||||
NODE_V8_COVERAGE: coverage,
|
||||
OMNIROUTE_CI_SYNC_TSX_CACHE: coverage,
|
||||
},
|
||||
stdio: "pipe",
|
||||
timeout: 90_000,
|
||||
}
|
||||
);
|
||||
const cli = new URL("bin/omniroute.mjs", root).href;
|
||||
const token = new URL("bin/cli/utils/cliToken.mjs", root);
|
||||
let launches = 0;
|
||||
for (const file of await readdir(coverage)) {
|
||||
if (!file.endsWith(".json")) continue;
|
||||
const report = JSON.parse(await readFile(join(coverage, file), "utf8"));
|
||||
if (!report.result.some((script) => script.url === cli)) continue;
|
||||
launches++;
|
||||
assert.ok(report.result.some((script) => script.url === preload.href));
|
||||
assert.ok(
|
||||
report.result.some(
|
||||
(script) => script.url === new URL("scripts/ci/native-cli-v8-tsx.mjs", root).href
|
||||
)
|
||||
);
|
||||
const script = report.result.find((sample) => sample.url === token.href);
|
||||
assert.ok(script, "the CLI's imports contribute native coverage");
|
||||
assert.equal(Boolean(report["source-map-cache"]?.[token.href]), false);
|
||||
assert.equal(script.functions[0].ranges[0].endOffset, (await readFile(token, "utf8")).length);
|
||||
}
|
||||
assert.equal(launches, 1);
|
||||
});
|
||||
|
||||
test("the installed-layout CLI records native coverage for checkout-backed modules", async (t) => {
|
||||
const fixture = await mkdtemp(join(tmpdir(), "installed-cli-coverage-"));
|
||||
t.after(() => rm(fixture, { recursive: true, force: true }));
|
||||
const coverage = join(fixture, "coverage");
|
||||
await mkdir(coverage);
|
||||
const env = {
|
||||
...process.env,
|
||||
TMPDIR: fixture,
|
||||
NODE_OPTIONS: `--import=${preload.href}`,
|
||||
NODE_V8_COVERAGE: coverage,
|
||||
};
|
||||
delete env.NODE_TEST_CONTEXT;
|
||||
execFileSync(
|
||||
process.execPath,
|
||||
["--import", "tsx/esm", "--test", "tests/unit/cli-installed-default-env-16763.test.ts"],
|
||||
{
|
||||
cwd: root,
|
||||
env,
|
||||
stdio: "pipe",
|
||||
timeout: 120_000,
|
||||
}
|
||||
);
|
||||
|
||||
const token = new URL("bin/cli/utils/cliToken.mjs", root);
|
||||
const tokenSourceLength = (await readFile(token, "utf8")).length;
|
||||
const installedEntry = "/node_modules/omniroute/bin/omniroute.mjs";
|
||||
let invocations = 0;
|
||||
for (const file of await readdir(coverage)) {
|
||||
if (!file.endsWith(".json")) continue;
|
||||
const report = JSON.parse(await readFile(join(coverage, file), "utf8"));
|
||||
if (!report.result.some(({ url }) => url.endsWith(installedEntry))) continue;
|
||||
invocations++;
|
||||
const sample = report.result.find(({ url }) => url === token.href);
|
||||
assert.ok(sample, "the installed CLI loaded the checkout-backed token module");
|
||||
assert.equal(Boolean(report["source-map-cache"]?.[token.href]), false, token.href);
|
||||
assert.equal(sample.functions[0].ranges[0].endOffset, tokenSourceLength);
|
||||
}
|
||||
assert.equal(invocations, 2, "include the default and edited installed .env launches");
|
||||
});
|
||||
|
||||
async function assertIsolatedBoundaryCoverage(t, driver, fixturePath) {
|
||||
const coverage = await mkdtemp(join(tmpdir(), "isolated-boundary-v8-coverage-"));
|
||||
t.after(() => rm(coverage, { recursive: true, force: true }));
|
||||
const env = {
|
||||
...process.env,
|
||||
TMPDIR: coverage,
|
||||
NODE_OPTIONS: `--import=${preload.href}`,
|
||||
NODE_V8_COVERAGE: coverage,
|
||||
OMNIROUTE_CI_SYNC_TSX_CACHE: coverage,
|
||||
};
|
||||
delete env.NODE_TEST_CONTEXT;
|
||||
execFileSync(process.execPath, ["--import", "tsx/esm", "--test", driver], {
|
||||
cwd: root,
|
||||
env,
|
||||
stdio: "pipe",
|
||||
timeout: 120_000,
|
||||
});
|
||||
|
||||
const fixture = new URL(fixturePath, root).href;
|
||||
const source = new URL("src/lib/dataPaths.ts", root);
|
||||
const sourceLength = (await readFile(source, "utf8")).length;
|
||||
let children = 0;
|
||||
for (const file of await readdir(coverage)) {
|
||||
if (!file.endsWith(".json")) continue;
|
||||
const report = JSON.parse(await readFile(join(coverage, file), "utf8"));
|
||||
if (!report.result.some(({ url }) => url === fixture)) continue;
|
||||
children++;
|
||||
const sample = report.result.find(({ url }) => url === source.href);
|
||||
assert.ok(sample, "the boundary fixture must exercise dataPaths");
|
||||
assert.equal(Boolean(report["source-map-cache"]?.[source.href]), false, source.href);
|
||||
assert.ok(report.result.some(({ url }) => url === preload.href));
|
||||
assert.equal(
|
||||
sample.functions[0].ranges[0].endOffset,
|
||||
sourceLength + `\n\n//# sourceURL=${source.href}`.length
|
||||
);
|
||||
}
|
||||
assert.equal(children, 1, "check coverage emitted by the real isolated fixture subprocess");
|
||||
}
|
||||
|
||||
test("the isolated Codex boundary child records native dataPaths coverage", async (t) => {
|
||||
await assertIsolatedBoundaryCoverage(
|
||||
t,
|
||||
"tests/unit/codex-response-failed-boundary.test.ts",
|
||||
"tests/fixtures/codex-response-failed-boundary.fixture.ts"
|
||||
);
|
||||
});
|
||||
|
||||
test("the shared error boundary child records native dataPaths coverage", async (t) => {
|
||||
await assertIsolatedBoundaryCoverage(
|
||||
t,
|
||||
"tests/unit/provider-last-error-sanitization.test.ts",
|
||||
"tests/unit/fixtures/provider-last-error-sanitization.fixture.ts"
|
||||
);
|
||||
});
|
||||
|
||||
test("the standalone media MCP and tsx share native coverage coordinates", async (t) => {
|
||||
const fixture = await mkdtemp(join(tmpdir(), "media-mcp-native-coverage-"));
|
||||
t.after(() => rm(fixture, { recursive: true, force: true }));
|
||||
const mediaEntry = "scripts/cli/omniroute-media-mcp.mjs";
|
||||
const media = new URL(mediaEntry, root).href;
|
||||
const sanitizer = new URL("open-sse/utils/errorSanitization.ts", root).href;
|
||||
|
||||
async function record(mode, args) {
|
||||
const coverage = join(fixture, mode);
|
||||
await mkdir(coverage);
|
||||
execFileSync(process.execPath, args, {
|
||||
cwd: root,
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_OPTIONS: mode === "native" ? "" : `--import=${preload.href}`,
|
||||
NODE_V8_COVERAGE: coverage,
|
||||
},
|
||||
input: "",
|
||||
stdio: "pipe",
|
||||
timeout: 90_000,
|
||||
});
|
||||
const files = (await readdir(coverage)).filter((file) => file.endsWith(".json"));
|
||||
return Promise.all(
|
||||
files.map(async (file) => JSON.parse(await readFile(join(coverage, file), "utf8")))
|
||||
);
|
||||
}
|
||||
|
||||
const nativeReports = await record("native", [mediaEntry]);
|
||||
const nativeProcess = nativeReports.find((report) =>
|
||||
report.result.some(({ url }) => url === media)
|
||||
);
|
||||
assert.ok(nativeProcess, "the standalone media process must emit coverage on stdin EOF");
|
||||
const sources = nativeProcess.result.filter(
|
||||
({ url }) => url.startsWith(root.href) && url.endsWith(".ts") && !url.includes("/node_modules/")
|
||||
);
|
||||
assert.ok(sources.length > 1 && sources.some(({ url }) => url === sanitizer));
|
||||
|
||||
const imports = sources.map(({ url }) => `await import(${JSON.stringify(url)});`).join("\n");
|
||||
const tsxReports = await record("tsx", [
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
"--input-type=module",
|
||||
"--eval",
|
||||
imports,
|
||||
]);
|
||||
const tsxProcess = tsxReports.find((report) =>
|
||||
report.result.some(({ url }) => url === sanitizer)
|
||||
);
|
||||
assert.ok(tsxProcess, "the tsx caller must exercise the same sanitizer");
|
||||
|
||||
const coordinates = ({ functions }) =>
|
||||
functions.map(({ functionName, ranges: [{ startOffset, endOffset }] }) => [
|
||||
functionName,
|
||||
startOffset,
|
||||
endOffset,
|
||||
]);
|
||||
for (const native of sources) {
|
||||
const caller = tsxProcess.result.find(({ url }) => url === native.url);
|
||||
assert.ok(caller, native.url);
|
||||
assert.equal(Boolean(nativeProcess["source-map-cache"]?.[native.url]), false, native.url);
|
||||
assert.equal(Boolean(tsxProcess["source-map-cache"]?.[native.url]), false, native.url);
|
||||
assert.deepEqual(coordinates(caller), coordinates(native), native.url);
|
||||
}
|
||||
});
|
||||
|
||||
test("migration schema boots retain the native layout for the shared data paths module", async (t) => {
|
||||
const fixture = await mkdtemp(join(tmpdir(), "migration-native-coverage-"));
|
||||
t.after(() => rm(fixture, { recursive: true, force: true }));
|
||||
const base = join(fixture, "base");
|
||||
const head = join(fixture, "head");
|
||||
const coverage = join(fixture, "coverage");
|
||||
await Promise.all([base, head, coverage].map((directory) => mkdir(directory)));
|
||||
const original = "20261119010000_create_coverage_layout_fixture.sql";
|
||||
const createTable = "CREATE TABLE coverage_layout_fixture (id INTEGER PRIMARY KEY);";
|
||||
await Promise.all([
|
||||
writeFile(join(base, original), createTable),
|
||||
writeFile(join(head, original), createTable),
|
||||
writeFile(
|
||||
join(head, "20261119020000_extend_coverage_layout_fixture.sql"),
|
||||
"ALTER TABLE coverage_layout_fixture ADD COLUMN label TEXT;"
|
||||
),
|
||||
]);
|
||||
execFileSync(
|
||||
process.execPath,
|
||||
[
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
"scripts/check/migration-commutation.ts",
|
||||
"--base-dir",
|
||||
base,
|
||||
"--head-dir",
|
||||
head,
|
||||
],
|
||||
{
|
||||
cwd: root,
|
||||
env: {
|
||||
...process.env,
|
||||
TMPDIR: fixture,
|
||||
NODE_OPTIONS: `--import=${preload.href}`,
|
||||
NODE_V8_COVERAGE: coverage,
|
||||
OMNIROUTE_CI_SYNC_TSX_CACHE: fixture,
|
||||
},
|
||||
stdio: "pipe",
|
||||
timeout: 240_000,
|
||||
}
|
||||
);
|
||||
const boot = new URL("scripts/check/lib/migrationSchemaBoot.ts", root).href;
|
||||
const dbCore = new URL("src/lib/db/core.ts", root).href;
|
||||
const dataPaths = new URL("src/lib/dataPaths.ts", root);
|
||||
const sourceLength = (await readFile(dataPaths, "utf8")).length;
|
||||
let boots = 0;
|
||||
for (const file of await readdir(coverage)) {
|
||||
if (!file.endsWith(".json")) continue;
|
||||
const report = JSON.parse(await readFile(join(coverage, file), "utf8"));
|
||||
const urls = new Set(report.result.map((script) => script.url));
|
||||
if (!urls.has(boot) || !urls.has(dbCore)) continue;
|
||||
boots++;
|
||||
const script = report.result.find((script) => script.url === dataPaths.href);
|
||||
assert.ok(script, "the boot exercised the data paths module");
|
||||
assert.equal(report["source-map-cache"]?.[dataPaths.href], undefined);
|
||||
const [{ startOffset, endOffset }] = script.functions[0].ranges;
|
||||
assert.deepEqual(
|
||||
[startOffset, endOffset],
|
||||
[0, sourceLength + `\n\n//# sourceURL=${dataPaths.href}`.length]
|
||||
);
|
||||
}
|
||||
assert.ok(boots > 0, "the migration boot supplied coverage");
|
||||
});
|
||||
1091
tests/unit/build/v8-shard-evidence.test.mjs
Normal file
1091
tests/unit/build/v8-shard-evidence.test.mjs
Normal file
File diff suppressed because it is too large
Load Diff
529
tests/unit/build/v8-shard-rerun-provenance.test.mjs
Normal file
529
tests/unit/build/v8-shard-rerun-provenance.test.mjs
Normal file
@@ -0,0 +1,529 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import test from "node:test";
|
||||
import { load } from "js-yaml";
|
||||
import {
|
||||
packCoverageShard,
|
||||
prepareCoverageShards,
|
||||
} from "../../../scripts/ci/prepare-v8-coverage.mjs";
|
||||
|
||||
const root = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
||||
const workflow = load(readFileSync(join(root, ".github/workflows/ci.yml"), "utf8"));
|
||||
const selector = () => import("../../../scripts/ci/select-v8-coverage.mjs");
|
||||
const candidateSha = spawnSync("git", ["rev-parse", "HEAD"], {
|
||||
cwd: root,
|
||||
encoding: "utf8",
|
||||
}).stdout.trim();
|
||||
const options = {
|
||||
repository: "cheaper-inference/OmniRoute",
|
||||
candidateSha,
|
||||
// pull_request tests a synthetic merge commit, not the API's PR head SHA.
|
||||
runHeadSha: "a".repeat(40),
|
||||
runId: "987654321",
|
||||
runAttempt: "2",
|
||||
total: 8,
|
||||
};
|
||||
|
||||
function timestamp(attempt, offset) {
|
||||
return `2026-11-18T12:${String(attempt * 10 + offset).padStart(2, "0")}:00.000Z`;
|
||||
}
|
||||
|
||||
function job(index, attempt, conclusion = "success") {
|
||||
return {
|
||||
id: attempt * 100 + index,
|
||||
run_id: Number(options.runId),
|
||||
run_attempt: attempt,
|
||||
head_sha: options.runHeadSha,
|
||||
name: `Unit Tests (${index}/8)`,
|
||||
status: "completed",
|
||||
conclusion,
|
||||
started_at: timestamp(attempt, 0),
|
||||
completed_at: timestamp(attempt, 5),
|
||||
steps: ["Package successful shard coverage", "Upload complete shard coverage"].map((name) => ({
|
||||
name,
|
||||
status: "completed",
|
||||
conclusion: "success",
|
||||
started_at: timestamp(attempt, 1),
|
||||
completed_at: timestamp(attempt, 4),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
function artifact(index, attempt) {
|
||||
return {
|
||||
id: attempt * 1000 + index,
|
||||
name: `coverage-shard-${attempt}-${index}`,
|
||||
size_in_bytes: 100,
|
||||
expired: false,
|
||||
digest: `sha256:${String(index).repeat(64)}`,
|
||||
created_at: timestamp(attempt, 3),
|
||||
workflow_run: { id: Number(options.runId), head_sha: options.runHeadSha },
|
||||
};
|
||||
}
|
||||
|
||||
function receipt(producerAttempts) {
|
||||
return {
|
||||
schemaVersion: 1,
|
||||
policy: "v8-shard-provenance-v1",
|
||||
repository: options.repository,
|
||||
candidateSha,
|
||||
runHeadSha: options.runHeadSha,
|
||||
runId: options.runId,
|
||||
consumerAttempt: options.runAttempt,
|
||||
total: 8,
|
||||
producers: producerAttempts.map((attempt, offset) => {
|
||||
const index = offset + 1;
|
||||
const producer = job(index, attempt);
|
||||
const evidence = artifact(index, attempt);
|
||||
return {
|
||||
index,
|
||||
producerAttempt: String(attempt),
|
||||
jobId: String(producer.id),
|
||||
artifactId: String(evidence.id),
|
||||
artifactName: evidence.name,
|
||||
artifactDigest: evidence.digest,
|
||||
jobStartedAt: producer.started_at,
|
||||
jobCompletedAt: producer.completed_at,
|
||||
artifactCreatedAt: evidence.created_at,
|
||||
attemptStartedAt: timestamp(attempt, 0),
|
||||
nextAttemptStartedAt: attempt < 2 ? timestamp(attempt + 1, 0) : null,
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
async function downloaded(t, producerAttempts) {
|
||||
const dir = await mkdtemp(join(tmpdir(), "v8-rerun-provenance-"));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const directory = join(dir, "downloaded");
|
||||
await mkdir(directory);
|
||||
for (const [offset, attempt] of producerAttempts.entries()) {
|
||||
const index = offset + 1;
|
||||
const inputDirectory = join(dir, `input-${index}`);
|
||||
await mkdir(inputDirectory);
|
||||
await writeFile(
|
||||
join(inputDirectory, "coverage-collision.json"),
|
||||
JSON.stringify({
|
||||
result: [
|
||||
{
|
||||
scriptId: "1",
|
||||
url: pathToFileURL(join(root, "scripts/quality/audit-dependencies.mjs")).href,
|
||||
functions: [
|
||||
{
|
||||
functionName: "fixture",
|
||||
isBlockCoverage: true,
|
||||
ranges: [{ startOffset: 0, endOffset: 80, count: index }],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
);
|
||||
await packCoverageShard({
|
||||
...options,
|
||||
runAttempt: String(attempt),
|
||||
shard: `${index}/8`,
|
||||
sourceRoot: root,
|
||||
inputDirectory,
|
||||
outputDirectory: join(directory, `coverage-shard-${attempt}-${index}`),
|
||||
});
|
||||
}
|
||||
return {
|
||||
...options,
|
||||
directory,
|
||||
outputDirectory: join(dir, "prepared"),
|
||||
sourceRoot: root,
|
||||
selection: receipt(producerAttempts),
|
||||
};
|
||||
}
|
||||
|
||||
for (const [name, attempts] of [
|
||||
["seven retained successes and one rerun shard", [1, 1, 1, 1, 1, 1, 1, 2]],
|
||||
["coverage-only rerun with all producers retained", [1, 1, 1, 1, 1, 1, 1, 1]],
|
||||
]) {
|
||||
test(`a legitimate partial rerun admits ${name} without retagging manifests`, async (t) => {
|
||||
const input = await downloaded(t, attempts);
|
||||
const before = await Promise.all(
|
||||
input.selection.producers.map((producer) =>
|
||||
readFile(join(input.directory, producer.artifactName, "manifest.json"), "utf8")
|
||||
)
|
||||
);
|
||||
const result = await prepareCoverageShards(input);
|
||||
assert.equal(result.shards, 8);
|
||||
assert.equal(result.inputFiles, 8);
|
||||
assert.deepEqual(await readdir(input.outputDirectory), ["coverage-merged.json"]);
|
||||
const merged = JSON.parse(
|
||||
await readFile(join(input.outputDirectory, "coverage-merged.json"), "utf8")
|
||||
);
|
||||
assert.equal(merged.result[0].functions[0].ranges[0].count, 36);
|
||||
assert.deepEqual(result.producerAttempts, attempts.map(String));
|
||||
const after = await Promise.all(
|
||||
input.selection.producers.map((producer) =>
|
||||
readFile(join(input.directory, producer.artifactName, "manifest.json"), "utf8")
|
||||
)
|
||||
);
|
||||
assert.deepEqual(after, before, "producer manifests must retain their real attempt");
|
||||
});
|
||||
}
|
||||
|
||||
test("mixed attempts still fail without an external selection receipt", async (t) => {
|
||||
const input = await downloaded(t, [1, 1, 1, 1, 1, 1, 1, 2]);
|
||||
delete input.selection;
|
||||
await assert.rejects(prepareCoverageShards(input));
|
||||
});
|
||||
|
||||
for (const mutate of [
|
||||
(value) => {
|
||||
value.runId = "123";
|
||||
},
|
||||
(value) => {
|
||||
value.runHeadSha = "b".repeat(40);
|
||||
},
|
||||
(value) => {
|
||||
value.consumerAttempt = "1";
|
||||
},
|
||||
(value) => {
|
||||
value.producers[0] = { ...value.producers[1] };
|
||||
},
|
||||
(value) => {
|
||||
value.producers[0].producerAttempt = "3";
|
||||
},
|
||||
]) {
|
||||
test(`admission validates the supplied receipt: ${mutate.toString()}`, async (t) => {
|
||||
const input = await downloaded(t, [2, 2, 2, 2, 2, 2, 2, 2]);
|
||||
mutate(input.selection);
|
||||
await assert.rejects(prepareCoverageShards(input));
|
||||
});
|
||||
}
|
||||
|
||||
function apiFixture({ producerAttempts = [1, 1, 1, 1, 1, 1, 1, 2], extras = 0 } = {}) {
|
||||
const jobs = new Map([
|
||||
[1, []],
|
||||
[2, []],
|
||||
]);
|
||||
const artifacts = [];
|
||||
for (let index = 1; index <= 8; index++) {
|
||||
jobs.get(1).push(job(index, 1, producerAttempts[index - 1] === 1 ? "success" : "failure"));
|
||||
if (producerAttempts[index - 1] === 2) jobs.get(2).push(job(index, 2));
|
||||
artifacts.push(artifact(index, 1));
|
||||
if (producerAttempts[index - 1] === 2) artifacts.push(artifact(index, 2));
|
||||
}
|
||||
for (let index = 0; index < extras; index++) {
|
||||
jobs.get(2).unshift({ ...job(1, 2), id: 5000 + index, name: `Unrelated ${index}` });
|
||||
}
|
||||
const run = {
|
||||
id: Number(options.runId),
|
||||
run_attempt: 2,
|
||||
head_sha: options.runHeadSha,
|
||||
run_started_at: timestamp(2, 0),
|
||||
event: "pull_request",
|
||||
repository: { full_name: options.repository },
|
||||
};
|
||||
const calls = [];
|
||||
const getJson = async (route) => {
|
||||
calls.push(route);
|
||||
const url = new URL(route, "https://api.github.com");
|
||||
const prefix = `/repos/${options.repository}/actions/runs/${options.runId}`;
|
||||
if (url.pathname === prefix) return structuredClone(run);
|
||||
if (url.pathname === `${prefix}/attempts/1`)
|
||||
return structuredClone({ ...run, run_attempt: 1, run_started_at: timestamp(1, 0) });
|
||||
const match = new RegExp(`^${prefix}/attempts/([12])/jobs$`).exec(url.pathname);
|
||||
const records = match
|
||||
? jobs.get(Number(match[1]))
|
||||
: url.pathname === `${prefix}/artifacts`
|
||||
? artifacts
|
||||
: null;
|
||||
assert.ok(records, `unexpected API route ${route}`);
|
||||
const page = Number(url.searchParams.get("page") || 1);
|
||||
const perPage = Number(url.searchParams.get("per_page") || 100);
|
||||
return structuredClone({
|
||||
total_count: records.length,
|
||||
[match ? "jobs" : "artifacts"]: records.slice((page - 1) * perPage, page * perPage),
|
||||
});
|
||||
};
|
||||
return { jobs, artifacts, run, calls, getJson };
|
||||
}
|
||||
|
||||
test("the CLI context derives PR head from the event and tested SHA from the checked-out merge", async () => {
|
||||
const { resolveCoverageContext } = await selector();
|
||||
const env = {
|
||||
GITHUB_REPOSITORY: options.repository,
|
||||
GITHUB_EVENT_NAME: "pull_request",
|
||||
GITHUB_REF: "refs/pull/42/merge",
|
||||
GITHUB_SHA: candidateSha,
|
||||
GITHUB_RUN_ID: options.runId,
|
||||
GITHUB_RUN_ATTEMPT: options.runAttempt,
|
||||
};
|
||||
const event = {
|
||||
number: 42,
|
||||
repository: { full_name: options.repository },
|
||||
pull_request: {
|
||||
number: 42,
|
||||
head: { sha: options.runHeadSha },
|
||||
base: { repo: { full_name: options.repository } },
|
||||
},
|
||||
};
|
||||
const resolved = resolveCoverageContext(env, event, candidateSha);
|
||||
assert.equal(resolved.candidateSha, candidateSha);
|
||||
assert.equal(resolved.runHeadSha, options.runHeadSha);
|
||||
assert.throws(() => resolveCoverageContext(env, event, options.runHeadSha), /INCOMPLETE/);
|
||||
assert.throws(
|
||||
() =>
|
||||
resolveCoverageContext(
|
||||
env,
|
||||
{ ...event, pull_request: { ...event.pull_request, head: {} } },
|
||||
candidateSha
|
||||
),
|
||||
/INCOMPLETE/
|
||||
);
|
||||
});
|
||||
|
||||
for (const eventName of ["push", "workflow_dispatch", "merge_group"]) {
|
||||
test(`the CLI context keeps a single tested identity for ${eventName}`, async () => {
|
||||
const { resolveCoverageContext } = await selector();
|
||||
const env = {
|
||||
GITHUB_REPOSITORY: options.repository,
|
||||
GITHUB_EVENT_NAME: eventName,
|
||||
GITHUB_REF: "refs/heads/main",
|
||||
GITHUB_SHA: candidateSha,
|
||||
GITHUB_RUN_ID: options.runId,
|
||||
GITHUB_RUN_ATTEMPT: options.runAttempt,
|
||||
};
|
||||
const event = {
|
||||
repository: { full_name: options.repository },
|
||||
ref: env.GITHUB_REF,
|
||||
after: candidateSha,
|
||||
merge_group: { head_sha: candidateSha },
|
||||
};
|
||||
const resolved = resolveCoverageContext(env, event, candidateSha);
|
||||
assert.equal(resolved.candidateSha, candidateSha);
|
||||
assert.equal(resolved.runHeadSha, candidateSha);
|
||||
});
|
||||
}
|
||||
|
||||
test("the selector binds PR head separately from the tested merge and retains seven successes", async () => {
|
||||
const { selectCoverageArtifacts } = await selector();
|
||||
const api = apiFixture();
|
||||
const selected = await selectCoverageArtifacts(options, { getJson: api.getJson });
|
||||
assert.equal(selected.candidateSha, candidateSha);
|
||||
assert.equal(selected.runHeadSha, options.runHeadSha);
|
||||
assert.notEqual(selected.candidateSha, selected.runHeadSha);
|
||||
assert.deepEqual(
|
||||
selected.producers.map((item) => item.producerAttempt),
|
||||
["1", "1", "1", "1", "1", "1", "1", "2"]
|
||||
);
|
||||
assert.deepEqual(
|
||||
selected.producers.map((item) => item.artifactId),
|
||||
["1001", "1002", "1003", "1004", "1005", "1006", "1007", "2008"]
|
||||
);
|
||||
});
|
||||
|
||||
test("GitHub retained job clones do not relabel the original coverage producers", async () => {
|
||||
const { selectCoverageArtifacts } = await selector();
|
||||
const api = apiFixture();
|
||||
for (let index = 1; index <= 7; index++) {
|
||||
// GitHub returns a new ID/run_attempt but retains the original execution times.
|
||||
api.jobs
|
||||
.get(2)
|
||||
.push({ ...structuredClone(api.jobs.get(1)[index - 1]), id: 200 + index, run_attempt: 2 });
|
||||
}
|
||||
const selected = await selectCoverageArtifacts(options, { getJson: api.getJson });
|
||||
assert.deepEqual(
|
||||
selected.producers.map((item) => item.producerAttempt),
|
||||
["1", "1", "1", "1", "1", "1", "1", "2"]
|
||||
);
|
||||
assert.deepEqual(
|
||||
selected.producers.map((item) => item.jobId),
|
||||
["101", "102", "103", "104", "105", "106", "107", "208"]
|
||||
);
|
||||
assert.deepEqual(
|
||||
selected.producers.map((item) => item.artifactId),
|
||||
["1001", "1002", "1003", "1004", "1005", "1006", "1007", "2008"]
|
||||
);
|
||||
});
|
||||
|
||||
for (const scenario of ["clone-history-mismatch", "clone-crosses-attempt-start", "clone-failed"]) {
|
||||
test(`a retained clone is INCOMPLETE for ${scenario}`, async () => {
|
||||
const { selectCoverageArtifacts } = await selector();
|
||||
const api = apiFixture();
|
||||
const clone = { ...structuredClone(api.jobs.get(1)[0]), id: 201, run_attempt: 2 };
|
||||
api.jobs.get(2).push(clone);
|
||||
if (scenario === "clone-history-mismatch") clone.steps[0].completed_at = timestamp(1, 2);
|
||||
if (scenario === "clone-crosses-attempt-start") clone.completed_at = timestamp(2, 1);
|
||||
if (scenario === "clone-failed") clone.conclusion = "failure";
|
||||
await assert.rejects(selectCoverageArtifacts(options, { getJson: api.getJson }), /INCOMPLETE/);
|
||||
});
|
||||
}
|
||||
|
||||
for (const attempts of [
|
||||
[1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[2, 2, 2, 2, 2, 2, 2, 2],
|
||||
]) {
|
||||
test(`the selector handles producer attempts ${attempts.join(",")}`, async () => {
|
||||
const { selectCoverageArtifacts } = await selector();
|
||||
const api = apiFixture({ producerAttempts: attempts });
|
||||
const selected = await selectCoverageArtifacts(options, { getJson: api.getJson });
|
||||
assert.deepEqual(
|
||||
selected.producers.map((item) => item.producerAttempt),
|
||||
attempts.map(String)
|
||||
);
|
||||
assert.equal(new Set(selected.producers.map((item) => item.artifactId)).size, 8);
|
||||
if (attempts[0] === 2)
|
||||
assert.equal(
|
||||
api.calls.some((route) => route.includes("attempts/1/jobs")),
|
||||
false
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
for (const scenario of [
|
||||
"latest-failed",
|
||||
"latest-cancelled",
|
||||
"upload-incomplete",
|
||||
"duplicate-job",
|
||||
"duplicate-artifact",
|
||||
"expired-artifact",
|
||||
"missing-artifact",
|
||||
"wrong-run-head",
|
||||
"wrong-job-head",
|
||||
"wrong-artifact-run",
|
||||
"artifact-outside-job",
|
||||
"digest-missing",
|
||||
]) {
|
||||
test(`provenance is INCOMPLETE for ${scenario} without falling back to an older success`, async () => {
|
||||
const { selectCoverageArtifacts } = await selector();
|
||||
const api = apiFixture();
|
||||
const latest = api.jobs.get(2)[0];
|
||||
const latestArtifact = api.artifacts.find((item) => item.name === "coverage-shard-2-8");
|
||||
if (scenario === "latest-failed") latest.conclusion = "failure";
|
||||
if (scenario === "latest-cancelled") latest.conclusion = "cancelled";
|
||||
if (scenario === "upload-incomplete") latest.steps[1].conclusion = "skipped";
|
||||
if (scenario === "duplicate-job") api.jobs.get(2).push({ ...latest, id: 299 });
|
||||
if (scenario === "duplicate-artifact") api.artifacts.push({ ...latestArtifact, id: 2999 });
|
||||
if (scenario === "expired-artifact") latestArtifact.expired = true;
|
||||
if (scenario === "missing-artifact")
|
||||
api.artifacts.splice(api.artifacts.indexOf(latestArtifact), 1);
|
||||
if (scenario === "wrong-run-head") api.run.head_sha = candidateSha;
|
||||
if (scenario === "wrong-job-head") latest.head_sha = candidateSha;
|
||||
if (scenario === "wrong-artifact-run") latestArtifact.workflow_run.id = 123;
|
||||
if (scenario === "artifact-outside-job") latestArtifact.created_at = timestamp(1, 3);
|
||||
if (scenario === "digest-missing") delete latestArtifact.digest;
|
||||
await assert.rejects(selectCoverageArtifacts(options, { getJson: api.getJson }), /INCOMPLETE/);
|
||||
});
|
||||
}
|
||||
|
||||
test("job enumeration is paginated to completion before choosing a producer", async () => {
|
||||
const { selectCoverageArtifacts } = await selector();
|
||||
const api = apiFixture({ extras: 120 });
|
||||
const selected = await selectCoverageArtifacts(options, { getJson: api.getJson });
|
||||
assert.equal(selected.producers.length, 8);
|
||||
assert.ok(
|
||||
api.calls.some((route) => route.includes("attempts/2/jobs") && route.includes("page=2"))
|
||||
);
|
||||
});
|
||||
|
||||
for (const scenario of ["empty-next-page", "count-drift", "too-many-records", "api-error"]) {
|
||||
test(`bounded API enumeration rejects ${scenario}`, async () => {
|
||||
const { selectCoverageArtifacts } = await selector();
|
||||
const api = apiFixture({ extras: 120 });
|
||||
const getJson = async (route) => {
|
||||
const value = await api.getJson(route);
|
||||
if (!route.includes("attempts/2/jobs")) return value;
|
||||
if (scenario === "api-error")
|
||||
throw new Error("authorization secret must never appear in diagnostics");
|
||||
if (scenario === "too-many-records") value.total_count = 10001;
|
||||
if (route.includes("page=2")) {
|
||||
if (scenario === "empty-next-page") value.jobs = [];
|
||||
if (scenario === "count-drift") value.total_count++;
|
||||
}
|
||||
return value;
|
||||
};
|
||||
await assert.rejects(selectCoverageArtifacts(options, { getJson }), (error) => {
|
||||
assert.match(error.message, /INCOMPLETE/);
|
||||
assert.doesNotMatch(error.message, /authorization secret/);
|
||||
return true;
|
||||
});
|
||||
assert.ok(api.calls.length <= 5, "incomplete API data must stop bounded collection");
|
||||
});
|
||||
}
|
||||
|
||||
test("coverage downloads selected immutable IDs and keeps both required read permissions", () => {
|
||||
const coverage = workflow.jobs["test-coverage"];
|
||||
assert.deepEqual(coverage.permissions, { contents: "read", actions: "read" });
|
||||
const selection = coverage.steps.find((step) => step.id === "coverage_selection");
|
||||
const download = coverage.steps.find((step) => step.name === "Download all shard coverage");
|
||||
assert.match(selection?.run || "", /select-v8-coverage\.mjs/);
|
||||
assert.equal(
|
||||
download.with["artifact-ids"],
|
||||
"${{ steps.coverage_selection.outputs.artifact_ids }}"
|
||||
);
|
||||
assert.equal(download.with.pattern, undefined);
|
||||
assert.equal(download.with["merge-multiple"], false);
|
||||
assert.ok(coverage.steps.indexOf(selection) < coverage.steps.indexOf(download));
|
||||
});
|
||||
|
||||
test("coverage producers and aggregator use one exact Node version across reruns", () => {
|
||||
const versionRef = "${{ env.CI_V8_NODE_VERSION }}";
|
||||
assert.match(workflow.env.CI_V8_NODE_VERSION, /^\d+\.\d+\.\d+$/);
|
||||
for (const job of ["test-unit", "test-coverage"]) {
|
||||
const setup = workflow.jobs[job].steps.find((step) =>
|
||||
step.uses?.startsWith("actions/setup-node@")
|
||||
);
|
||||
assert.equal(setup?.with["node-version"], versionRef, job);
|
||||
}
|
||||
});
|
||||
|
||||
test("the selection receipt is produced, consumed and archived under the coverage prefix", () => {
|
||||
const coverage = workflow.jobs["test-coverage"];
|
||||
const selection = coverage.steps.find((step) => step.id === "coverage_selection");
|
||||
const merge = coverage.steps.find((step) => step.name === "Merge + report + gate");
|
||||
const upload = coverage.steps.find((step) => step.name === "Upload coverage artifacts");
|
||||
const receiptPath = "coverage/coverage-selection.json";
|
||||
assert.match(
|
||||
selection?.run || "",
|
||||
/mkdir -p coverage[\s\S]*node scripts\/ci\/select-v8-coverage\.mjs coverage\/coverage-selection\.json 8/
|
||||
);
|
||||
assert.ok(merge.run.includes(receiptPath), "prepare must consume the archived receipt");
|
||||
const paths = upload.with.path
|
||||
.split(/\r?\n/)
|
||||
.map((path) => path.trim())
|
||||
.filter(Boolean);
|
||||
assert.ok(paths.includes(receiptPath));
|
||||
assert.ok(paths.includes("coverage/coverage-preparation.json"));
|
||||
assert.match(merge.run, /> coverage\/coverage-preparation\.json/);
|
||||
assert.ok(paths.includes("coverage/lcov.info"));
|
||||
assert.ok(
|
||||
paths.every((path) => dirname(path) === "coverage"),
|
||||
"the artifact must retain its common coverage prefix"
|
||||
);
|
||||
const sonarDownload = workflow.jobs.sonarqube.steps.find(
|
||||
(step) => step.with?.name === upload.with.name
|
||||
);
|
||||
assert.equal(sonarDownload.with.path, "coverage/", "lcov.info remains at the artifact root");
|
||||
});
|
||||
|
||||
test("the producer initializes and packages one exclusive runtime cache around the unchanged c8 collection", () => {
|
||||
const unit = workflow.jobs["test-unit"];
|
||||
const collect = unit.steps.find((step) => step.run?.includes("npm run test:unit:ci:shard"));
|
||||
const pack = unit.steps.find((step) => step.name === "Package successful shard coverage");
|
||||
assert.equal(
|
||||
collect.env.TMPDIR,
|
||||
"${{ runner.temp }}/v8-tsx-${{ github.run_id }}-${{ github.run_attempt }}-${{ matrix.shard }}"
|
||||
);
|
||||
assert.equal(pack.env.TMPDIR, collect.env.TMPDIR);
|
||||
assert.ok(
|
||||
collect.run.indexOf("prepare-v8-coverage.mjs init-cache") <
|
||||
collect.run.indexOf("npx c8 --merge-async")
|
||||
);
|
||||
assert.match(
|
||||
collect.run,
|
||||
/init-cache "\$TMPDIR" "\$TEST_SHARD" "\$GITHUB_SHA" "\$GITHUB_RUN_ID" "\$GITHUB_RUN_ATTEMPT"/
|
||||
);
|
||||
assert.match(pack.run, /"\$GITHUB_WORKSPACE" "\$TMPDIR"$/);
|
||||
assert.deepEqual(unit.strategy.matrix.shard, [1, 2, 3, 4, 5, 6, 7, 8]);
|
||||
});
|
||||
276
tests/unit/build/v8-source-map-layouts.test.mjs
Normal file
276
tests/unit/build/v8-source-map-layouts.test.mjs
Normal file
@@ -0,0 +1,276 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import test from "node:test";
|
||||
import { createSourceMapLayoutProjector } from "../../../scripts/ci/v8-source-map-layouts.mjs";
|
||||
|
||||
const sourcePath = "src/sample.ts";
|
||||
const sourceIdentity = { blob: "a".repeat(40), sha256: "b".repeat(64) };
|
||||
const root = join(tmpdir(), "v8-layout-fixture");
|
||||
|
||||
function sample(checkout, count = 1) {
|
||||
const path = join(root, checkout, sourcePath);
|
||||
const originUrl = pathToFileURL(path).href;
|
||||
const banner = `__filename=${JSON.stringify(path)};(()=>{`;
|
||||
const body = "function branch(value){if(value)return 1;return 0;}";
|
||||
const code = `${banner}\n${body}\n})()\n`;
|
||||
const generated = `${code}\n//# sourceMappingURL=${originUrl}\n\n//# sourceURL=${originUrl}`;
|
||||
const start = banner.length + 1;
|
||||
return {
|
||||
sourcePath,
|
||||
sourceIdentity: { ...sourceIdentity },
|
||||
originUrl,
|
||||
cache: {
|
||||
lineLengths: generated.split("\n").map((line) => line.length),
|
||||
// prepare already relocates source paths before this coordinate-only module.
|
||||
data: {
|
||||
version: 3,
|
||||
mappings: ";AAAA",
|
||||
sources: ["file:///candidate/src/sample.ts"],
|
||||
sourcesContent: ["candidate source"],
|
||||
},
|
||||
url: null,
|
||||
},
|
||||
generatedProofs: [
|
||||
{
|
||||
kind: "tsx-cjs-filename",
|
||||
normalizedBodySha256: "c".repeat(64),
|
||||
suffixKind: "inline-source-map-and-source-url",
|
||||
trailerLineIndex: 4,
|
||||
payloadSha256: "d".repeat(64),
|
||||
codeSha256: "e".repeat(64),
|
||||
mapSha256: "f".repeat(64),
|
||||
},
|
||||
],
|
||||
script: {
|
||||
scriptId: "42",
|
||||
url: "file:///candidate/src/sample.ts",
|
||||
functions: [
|
||||
{
|
||||
functionName: "",
|
||||
ranges: [{ startOffset: 0, endOffset: generated.length, count }],
|
||||
isBlockCoverage: true,
|
||||
},
|
||||
{
|
||||
functionName: "",
|
||||
ranges: [{ startOffset: banner.length - 5, endOffset: code.length - 4, count }],
|
||||
isBlockCoverage: false,
|
||||
},
|
||||
{
|
||||
functionName: "branch",
|
||||
ranges: [
|
||||
{ startOffset: start, endOffset: start + body.length, count },
|
||||
{ startOffset: start + 30, endOffset: start + 36, count: 0 },
|
||||
],
|
||||
isBlockCoverage: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function pair() {
|
||||
return { canonical: sample("omni-ci-checkout"), current: sample("hosted-checkout", 7) };
|
||||
}
|
||||
|
||||
test("verified TSX layout projection preserves every counter and flag without mutating the original cache", () => {
|
||||
const { canonical, current } = pair();
|
||||
const projector = createSourceMapLayoutProjector();
|
||||
assert.equal(projector.project(canonical), null);
|
||||
const before = structuredClone(current);
|
||||
const projectedLines = projector.project(current);
|
||||
assert.deepEqual(projectedLines, canonical.cache.lineLengths);
|
||||
assert.deepEqual(
|
||||
current.cache,
|
||||
before.cache,
|
||||
"cache updates are deferred until all script IDs are projected"
|
||||
);
|
||||
assert.equal(current.script.scriptId, before.script.scriptId);
|
||||
assert.equal(current.script.url, before.script.url);
|
||||
for (const [index, fn] of current.script.functions.entries()) {
|
||||
assert.equal(fn.isBlockCoverage, before.script.functions[index].isBlockCoverage);
|
||||
assert.deepEqual(
|
||||
fn.ranges.map((range) => range.count),
|
||||
before.script.functions[index].ranges.map((range) => range.count)
|
||||
);
|
||||
assert.deepEqual(
|
||||
fn.ranges.map(({ count: _count, ...range }) => range),
|
||||
canonical.script.functions[index].ranges.map(({ count: _count, ...range }) => range)
|
||||
);
|
||||
}
|
||||
const receipt = projector.summary();
|
||||
assert.equal(receipt.policy, "tsx-generated-layout-v1");
|
||||
assert.equal(receipt.projectedScriptSamples, 1);
|
||||
assert.equal(receipt.transformations[0].sourceBlob, sourceIdentity.blob);
|
||||
assert.equal(receipt.transformations[0].normalizedBodySha256, "c".repeat(64));
|
||||
assert.equal(receipt.transformations[0].originalSourceUrl, before.originUrl);
|
||||
assert.equal(receipt.transformations[0].canonicalSourceUrl, canonical.originUrl);
|
||||
assert.notEqual(
|
||||
receipt.transformations[0].fromLayoutSha256,
|
||||
receipt.transformations[0].toLayoutSha256
|
||||
);
|
||||
});
|
||||
|
||||
test("multiple script IDs sharing a cache each read the original layout", () => {
|
||||
const { canonical, current } = pair();
|
||||
const projector = createSourceMapLayoutProjector();
|
||||
projector.project(canonical);
|
||||
const second = { ...current, script: structuredClone(current.script) };
|
||||
second.script.scriptId = "43";
|
||||
const firstLines = projector.project(current);
|
||||
const secondLines = projector.project(second);
|
||||
assert.deepEqual(firstLines, secondLines);
|
||||
assert.deepEqual(current.script.functions, second.script.functions);
|
||||
assert.equal(projector.summary().projectedScriptSamples, 2);
|
||||
});
|
||||
|
||||
test("an unchanged layout does not require projection evidence", () => {
|
||||
const canonical = sample("same-checkout");
|
||||
const current = structuredClone(canonical);
|
||||
canonical.generatedProofs = [];
|
||||
current.generatedProofs = [];
|
||||
current.script.functions[0].ranges[0].count = 9;
|
||||
const projector = createSourceMapLayoutProjector();
|
||||
projector.project(canonical);
|
||||
const before = structuredClone(current);
|
||||
assert.equal(projector.project(current), null);
|
||||
assert.deepEqual(current, before);
|
||||
assert.equal(projector.summary().projectedScriptSamples, 0);
|
||||
});
|
||||
|
||||
const negatives = [
|
||||
["body line length", "body-differs", ({ current }) => current.cache.lineLengths[1]++],
|
||||
[
|
||||
"map",
|
||||
"map-differs",
|
||||
({ current }) => {
|
||||
current.cache.data.mappings += ",CAAC";
|
||||
},
|
||||
],
|
||||
[
|
||||
"mapped source",
|
||||
"map-differs",
|
||||
({ current }) => {
|
||||
current.cache.data.sourcesContent[0] = "different source";
|
||||
},
|
||||
],
|
||||
[
|
||||
"source blob",
|
||||
"source-identity-differs",
|
||||
({ current }) => {
|
||||
current.sourceIdentity.blob = "0".repeat(40);
|
||||
},
|
||||
],
|
||||
[
|
||||
"source hash",
|
||||
"source-identity-differs",
|
||||
({ current }) => {
|
||||
current.sourceIdentity.sha256 = "0".repeat(64);
|
||||
},
|
||||
],
|
||||
[
|
||||
"function identity",
|
||||
"function-identity-differs",
|
||||
({ current }) => {
|
||||
current.script.functions[2].functionName = "different";
|
||||
},
|
||||
],
|
||||
[
|
||||
"unrecognized banner",
|
||||
"unrecognized-banner",
|
||||
({ current }) => {
|
||||
current.generatedProofs[0].kind = "unrecognized-banner";
|
||||
},
|
||||
],
|
||||
["banner length", "unrecognized-banner", ({ current }) => current.cache.lineLengths[0]++],
|
||||
[
|
||||
"range in filename",
|
||||
"range-in-banner",
|
||||
({ current }) => {
|
||||
current.script.functions[1].ranges.push({ startOffset: 2, endOffset: 3, count: 0 });
|
||||
},
|
||||
],
|
||||
[
|
||||
"invalid offset",
|
||||
"invalid-range",
|
||||
({ current }) => {
|
||||
current.script.functions[2].ranges[1].endOffset = Number.MAX_SAFE_INTEGER;
|
||||
},
|
||||
],
|
||||
[
|
||||
"invalid line length",
|
||||
"invalid-lines",
|
||||
({ current }) => {
|
||||
current.cache.lineLengths[1] = -1;
|
||||
},
|
||||
],
|
||||
[
|
||||
"missing generated evidence",
|
||||
"generated-evidence-missing",
|
||||
({ current }) => {
|
||||
current.generatedProofs = [];
|
||||
},
|
||||
],
|
||||
[
|
||||
"different generated body",
|
||||
"generated-body-differs",
|
||||
({ current }) => {
|
||||
current.generatedProofs[0].normalizedBodySha256 = "0".repeat(64);
|
||||
},
|
||||
],
|
||||
[
|
||||
"mapped trailer",
|
||||
"mapped-trailer",
|
||||
({ canonical, current }) => {
|
||||
canonical.cache.data.mappings = ";AAAA;;;AAAA";
|
||||
current.cache.data.mappings = canonical.cache.data.mappings;
|
||||
},
|
||||
],
|
||||
];
|
||||
|
||||
for (const trailer of [4, 6]) {
|
||||
negatives.push([
|
||||
`range in trailer ${trailer}`,
|
||||
"range-in-trailer",
|
||||
({ current }) => {
|
||||
const startOffset =
|
||||
current.cache.lineLengths.slice(0, trailer).reduce((sum, length) => sum + length + 1, 0) +
|
||||
1;
|
||||
current.script.functions[0].ranges.push({
|
||||
startOffset,
|
||||
endOffset: startOffset + 1,
|
||||
count: 0,
|
||||
});
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
for (const [name, reason, mutate] of negatives) {
|
||||
test(`layout projection rejects ${name} without publishing a transformation`, () => {
|
||||
const values = pair();
|
||||
mutate(values);
|
||||
const projector = createSourceMapLayoutProjector();
|
||||
projector.project(values.canonical);
|
||||
assert.throws(
|
||||
() => projector.project(values.current),
|
||||
(error) => {
|
||||
assert.ok(
|
||||
error.message.includes(`V8_EVIDENCE_INCOMPLETE:source-map-layout-${reason}`),
|
||||
error.message
|
||||
);
|
||||
return true;
|
||||
}
|
||||
);
|
||||
assert.equal(projector.summary().projectedScriptSamples, 0);
|
||||
});
|
||||
}
|
||||
|
||||
test("same-size generated body changes cannot hide behind an identical map and layout", () => {
|
||||
const canonical = sample("same-checkout");
|
||||
const current = structuredClone(canonical);
|
||||
current.generatedProofs[0].normalizedBodySha256 = "0".repeat(64);
|
||||
const projector = createSourceMapLayoutProjector();
|
||||
projector.project(canonical);
|
||||
assert.throws(() => projector.project(current), /generated-body-differs/);
|
||||
});
|
||||
451
tests/unit/build/v8-transform-evidence.test.mjs
Normal file
451
tests/unit/build/v8-transform-evidence.test.mjs
Normal file
@@ -0,0 +1,451 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync } from "node:fs";
|
||||
import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { createRequire } from "node:module";
|
||||
import { tmpdir, userInfo } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import test from "node:test";
|
||||
import {
|
||||
initializeCoverageTransformCache,
|
||||
packCoverageShard,
|
||||
prepareCoverageShards,
|
||||
} from "../../../scripts/ci/prepare-v8-coverage.mjs";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const digest = (value) => createHash("sha256").update(value).digest("hex");
|
||||
const runId = "987654321";
|
||||
const runAttempt = "2";
|
||||
const total = 8;
|
||||
const sourcePath = "src/branch.ts";
|
||||
const source = "export const value = true;\n";
|
||||
|
||||
async function fixture(t) {
|
||||
const dir = await mkdtemp(join(tmpdir(), "v8-transform-proof-"));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const firstRoot = join(dir, "omni-ci-checkout");
|
||||
const secondRoot = join(dir, "hosted-checkout");
|
||||
await mkdir(join(firstRoot, "src"), { recursive: true });
|
||||
await writeFile(join(firstRoot, sourcePath), source);
|
||||
const env = {
|
||||
...process.env,
|
||||
GIT_AUTHOR_NAME: "Coverage Fixture",
|
||||
GIT_AUTHOR_EMAIL: "coverage@example.test",
|
||||
GIT_COMMITTER_NAME: "Coverage Fixture",
|
||||
GIT_COMMITTER_EMAIL: "coverage@example.test",
|
||||
};
|
||||
const git = (args, input) =>
|
||||
execFileSync("git", args, {
|
||||
cwd: firstRoot,
|
||||
env,
|
||||
input,
|
||||
encoding: "utf8",
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
}).trim();
|
||||
git(["init", "--quiet"]);
|
||||
const blob = git(["hash-object", "-w", "--stdin"], source);
|
||||
const subtree = git(["mktree"], `100644 blob ${blob}\tbranch.ts\n`);
|
||||
const tree = git(["mktree"], `040000 tree ${subtree}\tsrc\n`);
|
||||
const candidateSha = git(["commit-tree", tree, "-m", "coverage fixture"]);
|
||||
git(["update-ref", "HEAD", candidateSha]);
|
||||
git(["clone", "--quiet", "--local", firstRoot, secondRoot]);
|
||||
const directory = join(dir, "downloads");
|
||||
await mkdir(directory);
|
||||
return { dir, firstRoot, secondRoot, candidateSha, directory, manifests: [] };
|
||||
}
|
||||
|
||||
function generated(sourceRoot, { differentBody = false, unrecognizedBanner = false } = {}) {
|
||||
const path = join(sourceRoot, sourcePath);
|
||||
const url = pathToFileURL(path).href;
|
||||
const banner = `__filename=${JSON.stringify(path)};${unrecognizedBanner ? "(function(){" : "(()=>{"}`;
|
||||
const code = `${banner}\nconst value=${differentBody ? "null" : "true"};\n})()\n`;
|
||||
const map = {
|
||||
version: 3,
|
||||
mappings: ";AAAA",
|
||||
names: [],
|
||||
ignoreList: [],
|
||||
sources: [path],
|
||||
sourcesContent: [source],
|
||||
};
|
||||
const executable =
|
||||
code +
|
||||
"\n//# sourceMappingURL=data:application/json;base64," +
|
||||
Buffer.from(JSON.stringify(map)).toString("base64") +
|
||||
`\n\n//# sourceURL=${url}`;
|
||||
const raw = {
|
||||
result: [
|
||||
{
|
||||
scriptId: "1",
|
||||
url,
|
||||
functions: [
|
||||
{
|
||||
functionName: "",
|
||||
ranges: [{ startOffset: 0, endOffset: executable.length, count: 1 }],
|
||||
isBlockCoverage: true,
|
||||
},
|
||||
{
|
||||
functionName: "",
|
||||
ranges: [{ startOffset: banner.length - 5, endOffset: code.length - 4, count: 1 }],
|
||||
isBlockCoverage: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
"source-map-cache": {
|
||||
[url]: {
|
||||
lineLengths: executable.split("\n").map((line) => line.length),
|
||||
data: { ...map, sources: [url], sourceRoot: "" },
|
||||
url: null,
|
||||
},
|
||||
},
|
||||
};
|
||||
return { code, map, raw };
|
||||
}
|
||||
|
||||
async function packageMatrix(t, scenario = "valid") {
|
||||
const options = await fixture(t);
|
||||
for (let index = 1; index <= total; index++) {
|
||||
const sourceRoot = index % 2 ? options.firstRoot : options.secondRoot;
|
||||
const shard = `${index}/${total}`;
|
||||
const inputDirectory = join(options.dir, `raw-${index}`);
|
||||
const cacheDirectory = join(options.dir, `exclusive-cache-${index}`);
|
||||
const uid = process.geteuid ? process.geteuid() : userInfo().username;
|
||||
const cache = join(cacheDirectory, `tsx-${uid}`);
|
||||
await mkdir(inputDirectory);
|
||||
await mkdir(cache, { recursive: true });
|
||||
const value = generated(sourceRoot, {
|
||||
differentBody: scenario === "same-size-body" && index === 2,
|
||||
unrecognizedBanner: scenario === "unrecognized-banner" && index === 2,
|
||||
});
|
||||
if (scenario === "wrong-raw-source" && index === 2) {
|
||||
const rawMap = Object.values(value.raw["source-map-cache"])[0].data;
|
||||
rawMap.sourcesContent = ["export const value = null;\n"];
|
||||
}
|
||||
await writeFile(join(inputDirectory, "coverage-1.json"), JSON.stringify(value.raw));
|
||||
const binding = {
|
||||
schemaVersion: 1,
|
||||
policy: "exclusive-tsx-cache-v1",
|
||||
candidateSha: options.candidateSha,
|
||||
runId,
|
||||
runAttempt,
|
||||
shard,
|
||||
sourceRoot,
|
||||
nodeVersion: process.version,
|
||||
c8Version: require("c8/package.json").version,
|
||||
tsxVersion: require("tsx/package.json").version,
|
||||
};
|
||||
if (scenario === "wrong-cache-binding" && index === 2) binding.candidateSha = "b".repeat(40);
|
||||
await writeFile(join(cacheDirectory, "producer.json"), JSON.stringify(binding));
|
||||
const payload = { code: value.code, map: JSON.stringify(value.map) };
|
||||
const interrupted = '{"code":';
|
||||
const neededEntry = scenario === "interrupted-required-cache" && index === 2;
|
||||
await writeFile(
|
||||
join(cache, `17950-${"a".repeat(40)}`),
|
||||
neededEntry ? interrupted : JSON.stringify(payload)
|
||||
);
|
||||
if (scenario === "interrupted-unrelated-cache" && index === 2) {
|
||||
await writeFile(join(cache, `17950-${"b".repeat(40)}`), interrupted);
|
||||
}
|
||||
if (scenario === "wrong-unused-cached-source" && index === 2) {
|
||||
const staleMap = { ...value.map, sourcesContent: ["export const value = null;\n"] };
|
||||
await writeFile(
|
||||
join(cache, `17950-${"b".repeat(40)}`),
|
||||
JSON.stringify({ code: value.code, map: JSON.stringify(staleMap) })
|
||||
);
|
||||
}
|
||||
if (scenario === "ambiguous-cache" && index === 2) {
|
||||
const different = generated(sourceRoot, { differentBody: true });
|
||||
await writeFile(
|
||||
join(cache, `17950-${"b".repeat(40)}`),
|
||||
JSON.stringify({
|
||||
code: different.code,
|
||||
map: JSON.stringify(different.map),
|
||||
})
|
||||
);
|
||||
}
|
||||
const outputDirectory = join(options.directory, `coverage-shard-${runAttempt}-${index}`);
|
||||
options.manifests.push(
|
||||
await packCoverageShard({
|
||||
inputDirectory,
|
||||
outputDirectory,
|
||||
shard,
|
||||
candidateSha: options.candidateSha,
|
||||
runId,
|
||||
runAttempt,
|
||||
sourceRoot,
|
||||
transformCacheDirectory: scenario === "missing-cache" ? undefined : cacheDirectory,
|
||||
})
|
||||
);
|
||||
}
|
||||
return {
|
||||
...options,
|
||||
total,
|
||||
runId,
|
||||
runAttempt,
|
||||
sourceRoot: options.firstRoot,
|
||||
outputDirectory: join(options.dir, "prepared"),
|
||||
};
|
||||
}
|
||||
|
||||
test("tsx generated evidence binds original code and map bytes to the producer and candidate", async (t) => {
|
||||
const options = await packageMatrix(t);
|
||||
const evidence = options.manifests[1].transformEvidence;
|
||||
assert.ok(evidence, "the original executed transform must travel with its shard");
|
||||
assert.equal(evidence.policy, "exclusive-tsx-cache-v1");
|
||||
assert.equal(evidence.producer.candidateSha, options.candidateSha);
|
||||
assert.equal(evidence.producer.runId, runId);
|
||||
assert.equal(evidence.producer.runAttempt, runAttempt);
|
||||
assert.equal(evidence.producer.shard, "2/8");
|
||||
assert.equal(evidence.producer.nodeVersion, process.version);
|
||||
assert.equal(evidence.producer.tsxVersion, require("tsx/package.json").version);
|
||||
assert.equal(evidence.entries.length, 1);
|
||||
const entry = evidence.entries[0];
|
||||
assert.equal(entry.sourcePath, sourcePath);
|
||||
assert.equal(entry.sourceSha256, digest(source));
|
||||
const bytes = await readFile(join(options.directory, "coverage-shard-2-2", entry.payload));
|
||||
assert.equal(digest(bytes), entry.payloadSha256);
|
||||
const payload = JSON.parse(bytes);
|
||||
assert.equal(digest(payload.code), entry.codeSha256);
|
||||
assert.equal(digest(payload.map), entry.mapSha256);
|
||||
const prepared = await prepareCoverageShards(options);
|
||||
assert.equal(prepared.shards, 8);
|
||||
assert.ok(prepared.layoutProjection.projectedScriptSamples > 0);
|
||||
assert.deepEqual(
|
||||
await readFile(join(options.directory, "coverage-shard-2-2", entry.payload)),
|
||||
bytes
|
||||
);
|
||||
assert.deepEqual(
|
||||
JSON.parse(await readFile(join(options.directory, "coverage-shard-2-2/manifest.json"), "utf8")),
|
||||
options.manifests[1]
|
||||
);
|
||||
});
|
||||
|
||||
test("an interrupted tsx cache write cannot block complete generated coverage evidence", async (t) => {
|
||||
const options = await packageMatrix(t, "interrupted-unrelated-cache");
|
||||
const evidence = options.manifests[1].transformEvidence;
|
||||
assert.equal(evidence.scannedEntries, 2);
|
||||
assert.equal(evidence.invalidJsonEntries, 1);
|
||||
assert.equal(evidence.entries.length, 1);
|
||||
const prepared = await prepareCoverageShards(options);
|
||||
assert.ok(prepared.layoutProjection.projectedScriptSamples > 0);
|
||||
});
|
||||
|
||||
test("an unused transform with a different source cannot replace the measured proof", async (t) => {
|
||||
const options = await packageMatrix(t, "wrong-unused-cached-source");
|
||||
const evidence = options.manifests[1].transformEvidence;
|
||||
assert.equal(evidence.mismatchedSourceEntries, 1);
|
||||
assert.equal(evidence.entries.length, 1);
|
||||
const prepared = await prepareCoverageShards(options);
|
||||
assert.ok(prepared.layoutProjection.projectedScriptSamples > 0);
|
||||
});
|
||||
|
||||
test("V8 evidence refuses an embedded source different from the candidate Git blob", async (t) => {
|
||||
await assert.rejects(
|
||||
packageMatrix(t, "wrong-raw-source"),
|
||||
/V8_EVIDENCE_INCOMPLETE:generated-evidence-embedded-source \(src\/branch.ts\)/
|
||||
);
|
||||
});
|
||||
|
||||
for (const [scenario, reason] of [
|
||||
["missing-cache", "generated-evidence-missing"],
|
||||
["interrupted-required-cache", "generated-evidence-missing"],
|
||||
["same-size-body", "generated-body-differs"],
|
||||
["ambiguous-cache", "generated-evidence-ambiguous"],
|
||||
["unrecognized-banner", "unrecognized-banner"],
|
||||
]) {
|
||||
test(`tsx projection rejects ${scenario} before publishing derived coverage`, async (t) => {
|
||||
const options = await packageMatrix(t, scenario);
|
||||
await assert.rejects(prepareCoverageShards(options), (error) => {
|
||||
assert.match(error.message, /V8_EVIDENCE_INCOMPLETE:/);
|
||||
assert.ok(error.message.includes(reason), error.message);
|
||||
assert.match(error.message, /"source":"src\/branch.ts"/);
|
||||
assert.match(error.message, /"shard":[1-8]/);
|
||||
return true;
|
||||
});
|
||||
assert.equal(existsSync(options.outputDirectory), false);
|
||||
});
|
||||
}
|
||||
|
||||
test("tsx evidence rejects an altered transported cache payload", async (t) => {
|
||||
const options = await packageMatrix(t);
|
||||
const entry = options.manifests[1].transformEvidence?.entries[0];
|
||||
assert.ok(entry, "transport must retain a hash-bound original cache payload");
|
||||
await writeFile(join(options.directory, "coverage-shard-2-2", entry.payload), "altered cache");
|
||||
await assert.rejects(
|
||||
prepareCoverageShards(options),
|
||||
/V8_EVIDENCE_INCOMPLETE:.*generated-evidence-hash/
|
||||
);
|
||||
assert.equal(existsSync(options.outputDirectory), false);
|
||||
});
|
||||
|
||||
test("tsx evidence rejects a cache initialized for a different candidate", async (t) => {
|
||||
await assert.rejects(
|
||||
packageMatrix(t, "wrong-cache-binding"),
|
||||
/V8_EVIDENCE_INCOMPLETE:.*generated-evidence-binding/
|
||||
);
|
||||
});
|
||||
|
||||
test("tsx producer initialization creates an exclusive cache and never adopts an existing directory", async (t) => {
|
||||
const options = await fixture(t);
|
||||
const directory = join(options.dir, "exclusive-producer");
|
||||
const binding = {
|
||||
directory,
|
||||
candidateSha: options.candidateSha,
|
||||
runId,
|
||||
runAttempt,
|
||||
shard: "1/8",
|
||||
sourceRoot: options.firstRoot,
|
||||
};
|
||||
const initialized = await initializeCoverageTransformCache(binding);
|
||||
assert.equal(initialized.producer.candidateSha, options.candidateSha);
|
||||
assert.equal(initialized.producer.shard, "1/8");
|
||||
const original = await readFile(join(directory, "producer.json"));
|
||||
await assert.rejects(initializeCoverageTransformCache(binding), { code: "EEXIST" });
|
||||
assert.deepEqual(await readFile(join(directory, "producer.json")), original);
|
||||
});
|
||||
|
||||
for (const [field, value] of [
|
||||
["runId", "123"],
|
||||
["runAttempt", "1"],
|
||||
["shard", "1/8"],
|
||||
["nodeVersion", "v0.0.0"],
|
||||
["c8Version", "0.0.0"],
|
||||
["tsxVersion", "0.0.0"],
|
||||
["sourceRoot", "/unrelated-checkout"],
|
||||
]) {
|
||||
test(`tsx consumer rejects a different ${field} in the transform binding`, async (t) => {
|
||||
const options = await packageMatrix(t);
|
||||
const manifestPath = join(options.directory, "coverage-shard-2-2/manifest.json");
|
||||
const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
|
||||
manifest.transformEvidence.producer[field] = value;
|
||||
await writeFile(manifestPath, JSON.stringify(manifest));
|
||||
await assert.rejects(
|
||||
prepareCoverageShards(options),
|
||||
/V8_EVIDENCE_INCOMPLETE:.*generated-evidence-binding/
|
||||
);
|
||||
assert.equal(existsSync(options.outputDirectory), false);
|
||||
});
|
||||
}
|
||||
|
||||
test("coverage consumer bounds retained raw after generated proof and portable layout projection", async (t) => {
|
||||
const options = await packageMatrix(t);
|
||||
const originals = [];
|
||||
let generatedEvidenceBytes = 0;
|
||||
for (const [offset, manifest] of options.manifests.entries()) {
|
||||
const artifact = join(options.directory, `coverage-shard-${runAttempt}-${offset + 1}`);
|
||||
generatedEvidenceBytes += manifest.transformEvidence.payloadBytes;
|
||||
for (const path of [
|
||||
join(artifact, "manifest.json"),
|
||||
...manifest.inputs.map((input) => join(artifact, "raw", `${input.name}.gz`)),
|
||||
...manifest.transformEvidence.entries.map((entry) => join(artifact, entry.payload)),
|
||||
]) {
|
||||
originals.push({ path, sha256: digest(await readFile(path)) });
|
||||
}
|
||||
}
|
||||
const prepared = await prepareCoverageShards(options);
|
||||
let materializedBytes = 0;
|
||||
let contributedCount = 0;
|
||||
// readFile is already used by this suite; enumerate only the owned derivative directory.
|
||||
const { readdir } = await import("node:fs/promises");
|
||||
for (const name of await readdir(options.outputDirectory)) {
|
||||
const bytes = await readFile(join(options.outputDirectory, name));
|
||||
materializedBytes += bytes.length;
|
||||
const sample = JSON.parse(bytes.toString("utf8"));
|
||||
assert.ok(Array.isArray(sample.result), "the c8 input directory contains only V8 derivatives");
|
||||
for (const script of sample.result) contributedCount += script.functions[0].ranges[0].count;
|
||||
}
|
||||
assert.equal(prepared.inputFiles, 8);
|
||||
assert.equal(prepared.shards, 8);
|
||||
assert.equal(contributedCount, 8, "the projection retains each producer contribution");
|
||||
assert.ok(generatedEvidenceBytes > 0);
|
||||
assert.equal(prepared.generatedEvidenceBytes, generatedEvidenceBytes);
|
||||
assert.ok(prepared.layoutProjection.projectedScriptSamples > 0);
|
||||
for (const original of originals) {
|
||||
assert.equal(
|
||||
digest(await readFile(original.path)),
|
||||
original.sha256,
|
||||
"raw, manifests and generated proof stay immutable"
|
||||
);
|
||||
}
|
||||
const largestInputBytes = Math.max(
|
||||
...options.manifests.flatMap((manifest) => manifest.inputs.map((input) => input.bytes))
|
||||
);
|
||||
assert.ok(
|
||||
materializedBytes <= largestInputBytes * 2,
|
||||
`proved repeated-source corpus retains ${materializedBytes} raw bytes for largest input ${largestInputBytes}`
|
||||
);
|
||||
});
|
||||
|
||||
test(
|
||||
"coverage consumer enforces peak quota with portable generated-code evidence",
|
||||
{ skip: process.platform === "win32" },
|
||||
async (t) => {
|
||||
const { assertBoundedPreparation } = await import("./fixtures/v8-disk-quota-harness.mjs");
|
||||
await assertBoundedPreparation(await packageMatrix(t), { expectedCount: 8, generated: true });
|
||||
}
|
||||
);
|
||||
|
||||
test("tsx coverage retains the original generated bytes when either loader forces exit", async (t) => {
|
||||
const directory = await mkdtemp(join(tmpdir(), "v8-tsx-forced-exit-"));
|
||||
t.after(() => rm(directory, { recursive: true, force: true }));
|
||||
const atomicPreload = new URL("../../../scripts/ci/atomic-tsx-cache.mjs", import.meta.url).href;
|
||||
const nativePreload = new URL("../../../scripts/ci/native-worker-v8-layout.mjs", import.meta.url)
|
||||
.href;
|
||||
const uid = process.geteuid ? process.geteuid() : userInfo().username;
|
||||
for (const [loader, extension] of [
|
||||
["tsx/esm", "mts"],
|
||||
["tsx/esm", "cts"],
|
||||
["tsx", "mts"],
|
||||
["tsx", "cts"],
|
||||
]) {
|
||||
const mode = `${loader.replace("/", "-")}-${extension}`;
|
||||
const fixture = join(directory, `${mode}.${extension}`);
|
||||
await writeFile(
|
||||
fixture,
|
||||
[
|
||||
'const value: string = "fixture-ok";',
|
||||
"console.log(value);",
|
||||
'if (process.env.V8_FIXTURE_FORCE_EXIT === "1") process.exit(0);',
|
||||
"setTimeout(() => {}, 100);",
|
||||
].join("\n")
|
||||
);
|
||||
const snapshots = [];
|
||||
for (const forced of [false, true]) {
|
||||
const scratch = join(directory, `${mode}-${Number(forced)}`);
|
||||
await mkdir(scratch);
|
||||
const env = { ...process.env, TMPDIR: scratch };
|
||||
delete env.OMNIROUTE_CI_SYNC_TSX_CACHE;
|
||||
delete env.TSX_DISABLE_CACHE;
|
||||
env.NODE_V8_COVERAGE = join(scratch, "raw");
|
||||
env.NODE_OPTIONS = `--import=${nativePreload}`;
|
||||
if (forced) {
|
||||
env.V8_FIXTURE_FORCE_EXIT = "1";
|
||||
env.OMNIROUTE_CI_SYNC_TSX_CACHE = scratch;
|
||||
env.NODE_OPTIONS = `--import=${atomicPreload} --import=${nativePreload}`;
|
||||
} else {
|
||||
delete env.V8_FIXTURE_FORCE_EXIT;
|
||||
}
|
||||
const output = execFileSync(process.execPath, ["--import", loader, fixture], {
|
||||
env,
|
||||
encoding: "utf8",
|
||||
timeout: 15000,
|
||||
});
|
||||
assert.equal(output.trim(), "fixture-ok");
|
||||
const cache = join(scratch, `tsx-${uid}`);
|
||||
const files = (await readdir(cache)).sort();
|
||||
assert.ok(files.length > 0, `${mode} produced no cache`);
|
||||
assert.ok(files.every((file) => /^\d+-[a-f0-9]{40}$/.test(file)));
|
||||
const retained = [];
|
||||
for (const file of files) {
|
||||
const bytes = await readFile(join(cache, file));
|
||||
const value = JSON.parse(bytes.toString("utf8"));
|
||||
assert.equal(typeof value.code, "string");
|
||||
retained.push([file, digest(bytes)]);
|
||||
}
|
||||
snapshots.push(retained);
|
||||
}
|
||||
assert.deepEqual(snapshots[1], snapshots[0], mode);
|
||||
}
|
||||
});
|
||||
@@ -9,7 +9,16 @@ fs.mkdirSync(path.resolve("_artifacts"), { recursive: true });
|
||||
const fixture = fs.mkdtempSync(path.resolve("_artifacts/cached-migration-"));
|
||||
const originalEnv = { ...process.env };
|
||||
for (const key of Object.keys(process.env)) delete process.env[key];
|
||||
for (const key of ["PATH", "SystemRoot", "WINDIR", "COMSPEC", "PATHEXT", "NODE_TEST_CONTEXT"]) {
|
||||
// Keep Node source maps hydrated during coverage of the dynamic imports.
|
||||
for (const key of [
|
||||
"PATH",
|
||||
"SystemRoot",
|
||||
"WINDIR",
|
||||
"COMSPEC",
|
||||
"PATHEXT",
|
||||
"NODE_V8_COVERAGE",
|
||||
"NODE_TEST_CONTEXT",
|
||||
]) {
|
||||
if (originalEnv[key]) process.env[key] = originalEnv[key];
|
||||
}
|
||||
for (const key of ["DATA_DIR", "HOME", "USERPROFILE", "APPDATA", "LOCALAPPDATA", "TEMP", "TMP"]) {
|
||||
|
||||
@@ -807,6 +807,7 @@ function makeSelfContainedRepo(): { dir: string; mig: string; gatePath: string }
|
||||
"open-sse",
|
||||
"config",
|
||||
"scripts/check",
|
||||
"scripts/ci/native-worker-v8-layout.mjs",
|
||||
"package.json",
|
||||
"tsconfig.json",
|
||||
]) {
|
||||
|
||||
@@ -14,6 +14,7 @@ import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { cliCoverageNodeArgs } from "../_helpers/cliCwdEnvHarness.ts";
|
||||
|
||||
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "..");
|
||||
const BIN = path.join(ROOT, "bin", "omniroute.mjs");
|
||||
@@ -68,7 +69,11 @@ function cleanEnv(home: string) {
|
||||
|
||||
function runCli(args: string[], cwd: string, env: Record<string, string>) {
|
||||
return new Promise<{ code: number | null; stdout: string; stderr: string }>((resolve) => {
|
||||
const child = spawn(process.execPath, [BIN, ...args], { cwd, env, detached: true });
|
||||
const child = spawn(process.execPath, [...cliCoverageNodeArgs(BIN), BIN, ...args], {
|
||||
cwd,
|
||||
env,
|
||||
detached: true,
|
||||
});
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
child.stdout.on("data", (d) => (stdout += d));
|
||||
|
||||
@@ -13,6 +13,7 @@ import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { parseNodeVersion } from "../../../src/shared/utils/nodeRuntimeSupport.ts";
|
||||
|
||||
import { registerAliasResolver, resolveAlias } from "../../../bin/aliasResolver.mjs";
|
||||
import { nativeCoverageArgs } from "../../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
||||
const REPO_ROOT = join(__dirname, "..", "..", "..");
|
||||
@@ -24,6 +25,9 @@ const NODE_26_SKIP_REASON =
|
||||
// Child import() specifiers must be real file URLs. A Windows drive letter in
|
||||
// a bare path would otherwise be parsed as a URL scheme.
|
||||
const repoFileUrl = (relPath: string) => pathToFileURL(join(REPO_ROOT, relPath)).href;
|
||||
const coverageTsxImport = nativeCoverageArgs().length
|
||||
? repoFileUrl("scripts/ci/native-cli-v8-tsx.mjs")
|
||||
: undefined;
|
||||
|
||||
function runChild(script: string, cwd = REPO_ROOT) {
|
||||
const result = spawnSync(process.execPath, ["--input-type=module", "-e", script], {
|
||||
@@ -44,7 +48,7 @@ describe("aliasResolver Node 26 registration (#12073)", () => {
|
||||
{ skip: NODE_VERSION.major >= 26 ? false : NODE_26_SKIP_REASON },
|
||||
() => {
|
||||
const script = `
|
||||
await import("tsx/esm");
|
||||
await import(${JSON.stringify(coverageTsxImport ?? "tsx/esm")});
|
||||
const { registerAliasResolver } = await import(${JSON.stringify(repoFileUrl("bin/aliasResolver.mjs"))});
|
||||
const ok = await registerAliasResolver(${JSON.stringify(REPO_ROOT)});
|
||||
if (!ok) { console.error("FAIL: registerAliasResolver returned false"); process.exit(2); }
|
||||
@@ -71,7 +75,7 @@ describe("aliasResolver Node 26 registration (#12073)", () => {
|
||||
const globalInstallCwd = mkdtempSync(join(tmpdir(), "alias-resolver-global-install-"));
|
||||
try {
|
||||
const script = `
|
||||
await import(${JSON.stringify(repoFileUrl("node_modules/tsx/dist/esm/index.mjs"))});
|
||||
await import(${JSON.stringify(coverageTsxImport ?? repoFileUrl("node_modules/tsx/dist/esm/index.mjs"))});
|
||||
const { registerAliasResolver } = await import(${JSON.stringify(repoFileUrl("bin/aliasResolver.mjs"))});
|
||||
const ok = await registerAliasResolver(${JSON.stringify(REPO_ROOT)});
|
||||
if (!ok) { console.error("FAIL: registerAliasResolver returned false"); process.exit(2); }
|
||||
@@ -101,7 +105,7 @@ describe("aliasResolver Node 26 registration (#12073)", () => {
|
||||
|
||||
it("retains module.register() for simulated legacy runtimes", () => {
|
||||
const script = `
|
||||
await import("tsx/esm");
|
||||
await import(${JSON.stringify(coverageTsxImport ?? "tsx/esm")});
|
||||
const { createRequire, syncBuiltinESMExports } = await import("node:module");
|
||||
const require = createRequire(import.meta.url);
|
||||
const cjsModule = require("node:module");
|
||||
|
||||
@@ -5,6 +5,7 @@ import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import test from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
test("Codex app-server lifecycle survives retries and settles disconnects (#16029)", () => {
|
||||
const scratch = mkdtempSync(join(tmpdir(), "codex-lifecycle-16029-"));
|
||||
@@ -12,6 +13,7 @@ test("Codex app-server lifecycle survives retries and settles disconnects (#1602
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
...nativeCoverageArgs(),
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
"--import",
|
||||
|
||||
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import test from "node:test";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const REPO_ROOT = fileURLToPath(new URL("../..", import.meta.url));
|
||||
const FIXTURE = fileURLToPath(
|
||||
@@ -43,6 +44,7 @@ test("Codex public failure boundaries pass in an isolated process", () => {
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
...nativeCoverageArgs(),
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
"--import",
|
||||
|
||||
@@ -8,6 +8,7 @@ import { fileURLToPath } from "node:url";
|
||||
|
||||
import type { RequestFailedPayload } from "../../src/lib/events/types.ts";
|
||||
import { DEFAULT_CODEX_CLIENT_VERSION } from "../../src/shared/constants/codexClient.ts";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const RESULT_PREFIX = "DASHBOARD_FAILURE_PROBE_RESULT=";
|
||||
const repoRoot = fileURLToPath(new URL("../../", import.meta.url));
|
||||
@@ -26,7 +27,7 @@ function runProbe(env: NodeJS.ProcessEnv): Promise<{ stdout: string; stderr: str
|
||||
return new Promise((resolve, reject) => {
|
||||
execFile(
|
||||
process.execPath,
|
||||
["--import", "tsx/esm", probePath],
|
||||
[...nativeCoverageArgs(), "--import", "tsx/esm", probePath],
|
||||
{
|
||||
cwd: repoRoot,
|
||||
env,
|
||||
|
||||
@@ -125,17 +125,21 @@ test("independent SQLite connections race on encrypted duplicate plaintext: exac
|
||||
stdio: ["ignore", "ignore", "pipe", "ipc"],
|
||||
})
|
||||
);
|
||||
const signal = AbortSignal.timeout(90_000);
|
||||
const closed = Promise.all(children.map((child) => once(child, "close", { signal })));
|
||||
// A worker can fail while the parent is still waiting for IPC messages.
|
||||
void closed.catch(() => undefined);
|
||||
try {
|
||||
await Promise.all(
|
||||
children.map(async (child) => {
|
||||
const [message] = await once(child, "message");
|
||||
const [message] = await once(child, "message", { signal });
|
||||
assert.equal(message, "ready");
|
||||
})
|
||||
);
|
||||
const key = secret();
|
||||
const results = await Promise.all(
|
||||
children.map(async (child) => {
|
||||
const message = once(child, "message");
|
||||
const message = once(child, "message", { signal });
|
||||
child.send({ id: randomUUID(), key });
|
||||
return (await message)[0] as { status: number | string };
|
||||
})
|
||||
@@ -150,7 +154,21 @@ test("independent SQLite connections race on encrypted duplicate plaintext: exac
|
||||
assert.equal(rows[0].usage_limit_enabled, 1);
|
||||
assert.equal(rows[0].daily_usage_limit_usd, 5);
|
||||
assert.equal(rows[0].chaos_mode_enabled, 1);
|
||||
// Let workers close SQLite and finish writing V8 coverage after the IPC reply.
|
||||
for (const [code, exitSignal] of await closed) {
|
||||
assert.deepEqual([code, exitSignal], [0, null], "import worker must exit cleanly");
|
||||
}
|
||||
} finally {
|
||||
for (const child of children) child.kill();
|
||||
const cleanupSignal = AbortSignal.timeout(5_000);
|
||||
const running = children.filter(
|
||||
(child) => child.pid !== undefined && child.exitCode === null && child.signalCode === null
|
||||
);
|
||||
await Promise.all(
|
||||
running.map(async (child) => {
|
||||
const reaped = once(child, "close", { signal: cleanupSignal });
|
||||
child.kill("SIGKILL");
|
||||
await reaped;
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -12,7 +12,16 @@ fs.mkdirSync(path.resolve("_artifacts"), { recursive: true });
|
||||
const fixture = fs.mkdtempSync(path.resolve("_artifacts/key-templates-migration-"));
|
||||
const originalEnv = { ...process.env };
|
||||
for (const key of Object.keys(process.env)) delete process.env[key];
|
||||
for (const key of ["PATH", "SystemRoot", "WINDIR", "COMSPEC", "PATHEXT", "NODE_TEST_CONTEXT"]) {
|
||||
// Keep Node source maps hydrated during coverage of the dynamic imports.
|
||||
for (const key of [
|
||||
"PATH",
|
||||
"SystemRoot",
|
||||
"WINDIR",
|
||||
"COMSPEC",
|
||||
"PATHEXT",
|
||||
"NODE_V8_COVERAGE",
|
||||
"NODE_TEST_CONTEXT",
|
||||
]) {
|
||||
if (originalEnv[key]) process.env[key] = originalEnv[key];
|
||||
}
|
||||
for (const key of ["DATA_DIR", "HOME", "USERPROFILE", "APPDATA", "LOCALAPPDATA", "TEMP", "TMP"]) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import test from "node:test";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const FIXTURE = fileURLToPath(
|
||||
new URL("../fixtures/adapta-web-stream-error-boundary.fixture.ts", import.meta.url)
|
||||
@@ -42,12 +43,16 @@ function runIsolatedFixture(testRoot: string): Promise<FixtureResult> {
|
||||
delete childEnv.NODE_TEST_CONTEXT;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(process.execPath, ["--import", "tsx/esm", "--test", FIXTURE], {
|
||||
cwd: REPO_ROOT,
|
||||
env: childEnv,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
timeout: 90_000,
|
||||
});
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[...nativeCoverageArgs(), "--import", "tsx/esm", "--test", FIXTURE],
|
||||
{
|
||||
cwd: REPO_ROOT,
|
||||
env: childEnv,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
timeout: 90_000,
|
||||
}
|
||||
);
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
child.stdout.setEncoding("utf8").on("data", (chunk: string) => {
|
||||
|
||||
@@ -9,7 +9,16 @@ fs.mkdirSync(path.resolve("_artifacts"), { recursive: true });
|
||||
const fixture = fs.mkdtempSync(path.resolve("_artifacts/feature-team-migration-"));
|
||||
const originalEnv = { ...process.env };
|
||||
for (const key of Object.keys(process.env)) delete process.env[key];
|
||||
for (const key of ["PATH", "SystemRoot", "WINDIR", "COMSPEC", "PATHEXT", "NODE_TEST_CONTEXT"]) {
|
||||
// Keep Node source maps hydrated during coverage of the dynamic imports.
|
||||
for (const key of [
|
||||
"PATH",
|
||||
"SystemRoot",
|
||||
"WINDIR",
|
||||
"COMSPEC",
|
||||
"PATHEXT",
|
||||
"NODE_V8_COVERAGE",
|
||||
"NODE_TEST_CONTEXT",
|
||||
]) {
|
||||
if (originalEnv[key]) process.env[key] = originalEnv[key];
|
||||
}
|
||||
for (const key of ["DATA_DIR", "HOME", "USERPROFILE", "APPDATA", "LOCALAPPDATA", "TEMP", "TMP"]) {
|
||||
|
||||
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
|
||||
import { spawn } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import test from "node:test";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const repoRoot = fileURLToPath(new URL("../../", import.meta.url));
|
||||
const fixturePath = fileURLToPath(
|
||||
@@ -36,11 +37,15 @@ function runFixture(): Promise<FixtureResult> {
|
||||
delete childEnv.NODE_TEST_CONTEXT;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(process.execPath, ["--import", "tsx/esm", "--test", fixturePath], {
|
||||
cwd: repoRoot,
|
||||
env: childEnv,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[...nativeCoverageArgs(), "--import", "tsx/esm", "--test", fixturePath],
|
||||
{
|
||||
cwd: repoRoot,
|
||||
env: childEnv,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
}
|
||||
);
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
let timedOut = false;
|
||||
|
||||
@@ -4,6 +4,7 @@ import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { nativeCoverageArgs } from "../../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const REPO_ROOT = fileURLToPath(new URL("../../..", import.meta.url));
|
||||
const CHILD_PATH = "/usr/local/bin:/usr/bin:/bin";
|
||||
@@ -31,7 +32,14 @@ export function runIsolatedBoundaryFixture({
|
||||
try {
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
["--import", "tsx/esm", "--test", "--test-reporter=tap", fileURLToPath(fixtureUrl)],
|
||||
[
|
||||
...nativeCoverageArgs(),
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
"--test",
|
||||
"--test-reporter=tap",
|
||||
fileURLToPath(fixtureUrl),
|
||||
],
|
||||
{
|
||||
cwd: REPO_ROOT,
|
||||
encoding: "utf8",
|
||||
|
||||
@@ -5,6 +5,7 @@ import { tmpdir } from "node:os";
|
||||
import { basename, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import test from "node:test";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const REPO_ROOT = fileURLToPath(new URL("../..", import.meta.url));
|
||||
const FIXTURE = fileURLToPath(
|
||||
@@ -46,11 +47,15 @@ function runFixture(testRoot: string): Promise<ChildResult> {
|
||||
if (process.env.PATH) childEnv.PATH = process.env.PATH;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(process.execPath, ["--import", "tsx/esm", FIXTURE], {
|
||||
cwd: REPO_ROOT,
|
||||
env: childEnv,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[...nativeCoverageArgs(), "--import", "tsx/esm", FIXTURE],
|
||||
{
|
||||
cwd: REPO_ROOT,
|
||||
env: childEnv,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
}
|
||||
);
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
child.stdout.setEncoding("utf8").on("data", (chunk) => (stdout += chunk));
|
||||
|
||||
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { test } from "node:test";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const CHILD_RESULT_PREFIX = "HUGGINGCHAT_TRANSPORT_RESULT=";
|
||||
const childFixture = fileURLToPath(
|
||||
@@ -25,16 +26,20 @@ type TransportFailureResult = {
|
||||
};
|
||||
|
||||
function runTransportScenario(scenario: TransportScenario): TransportFailureResult {
|
||||
const result = spawnSync(process.execPath, ["--import", "tsx/esm", childFixture, scenario], {
|
||||
cwd: process.cwd(),
|
||||
encoding: "utf8",
|
||||
timeout: 60_000,
|
||||
env: {
|
||||
NODE_ENV: "test",
|
||||
NO_COLOR: "1",
|
||||
DISABLE_SQLITE_AUTO_BACKUP: "true",
|
||||
},
|
||||
});
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[...nativeCoverageArgs(), "--import", "tsx/esm", childFixture, scenario],
|
||||
{
|
||||
cwd: process.cwd(),
|
||||
encoding: "utf8",
|
||||
timeout: 60_000,
|
||||
env: {
|
||||
NODE_ENV: "test",
|
||||
NO_COLOR: "1",
|
||||
DISABLE_SQLITE_AUTO_BACKUP: "true",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
result.status,
|
||||
|
||||
@@ -4,6 +4,7 @@ import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import dns from "node:dns";
|
||||
import { getSettledModelCatalogRouteResponse } from "../_setup/modelCatalogFixture.ts";
|
||||
|
||||
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-image-route-"));
|
||||
process.env.DATA_DIR = TEST_DATA_DIR;
|
||||
@@ -209,7 +210,7 @@ test("v1 image models GET exposes image-only modalities for credential-backed im
|
||||
await seedConnection("topaz", { apiKey: "topaz-key" });
|
||||
await seedConnection("stability-ai", { apiKey: "stability-key" });
|
||||
|
||||
const response = await imageRoute.GET();
|
||||
const response = await getSettledModelCatalogRouteResponse(v1ModelsCatalog, imageRoute.GET);
|
||||
const body = (await response.json()) as { data: ImageModelRow[] };
|
||||
const byId = new Map(body.data.map((item) => [item.id, item]));
|
||||
|
||||
@@ -222,7 +223,7 @@ test("v1 image models GET exposes image-only modalities for credential-backed im
|
||||
test("v1 image models GET exposes current Codex image models and hides inactive providers", async () => {
|
||||
await seedConnection("codex", { apiKey: "codex-key" });
|
||||
|
||||
const response = await imageRoute.GET();
|
||||
const response = await getSettledModelCatalogRouteResponse(v1ModelsCatalog, imageRoute.GET);
|
||||
const body = (await response.json()) as { data: Array<{ id: string }> };
|
||||
const ids = body.data.map((item) => item.id);
|
||||
|
||||
@@ -1066,7 +1067,10 @@ for (const [provider, alias] of [
|
||||
assert.deepEqual(((await response.json()) as ImageResponseBody).data, [
|
||||
{ b64_json: "ZmFrZQ==" },
|
||||
]);
|
||||
const catalog = await imageRoute.GET(new Request("http://localhost/v1/images/generations"));
|
||||
const catalog = await getSettledModelCatalogRouteResponse(v1ModelsCatalog, () =>
|
||||
imageRoute.GET(new Request("http://localhost/v1/images/generations"))
|
||||
);
|
||||
assert.equal(catalog.status, 200);
|
||||
const payload = (await catalog.json()) as { data: ImageModelRow[] };
|
||||
assert.ok(payload.data.some((row) => row.id === `${provider}/grok-imagine-image-2.0`));
|
||||
});
|
||||
|
||||
@@ -11,6 +11,7 @@ import net from "node:net";
|
||||
import { generateKeyPairSync } from "node:crypto";
|
||||
import { build } from "esbuild";
|
||||
import { setTimeout as delay } from "node:timers/promises";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "omni-export-child-"));
|
||||
process.env.DATA_DIR = dir;
|
||||
@@ -26,9 +27,11 @@ const fixture = await import("./fixtures/logExportTestDestination.ts");
|
||||
const output = path.join(dir, "received.jsonl");
|
||||
const modulePath = path.join(import.meta.dirname, "fixtures/logExportTestDestination.ts");
|
||||
const secret = "synthetic-secret-not-for-argv-or-env";
|
||||
const [, coveragePreload] = nativeCoverageArgs();
|
||||
let child: ChildProcess | undefined;
|
||||
type TestOptions = {
|
||||
modulePath: string;
|
||||
coveragePreload?: string;
|
||||
beforeAck?: () => Promise<void>;
|
||||
onSpawn?: (child: ChildProcess) => void;
|
||||
config?: Record<string, unknown>;
|
||||
@@ -40,6 +43,7 @@ const configure = (
|
||||
function options(beforeAck?: () => Promise<void>) {
|
||||
configure?.({
|
||||
modulePath,
|
||||
coveragePreload,
|
||||
beforeAck,
|
||||
onSpawn: (spawned) => {
|
||||
child = spawned;
|
||||
@@ -617,6 +621,7 @@ test("R1 real child routes public HTTP and HTTPS through env proxy, NO_PROXY and
|
||||
direct.length = 0;
|
||||
configure?.({
|
||||
modulePath,
|
||||
coveragePreload,
|
||||
config: { network: true },
|
||||
onSpawn: (spawned) => {
|
||||
child = spawned;
|
||||
@@ -839,6 +844,7 @@ test("R3 default 500-row maximum artifacts batch passes with heap margin through
|
||||
console.log = () => {}; // Capture raw GC output instead of flooding test logs.
|
||||
configure?.({
|
||||
modulePath,
|
||||
coveragePreload,
|
||||
config: { memoryOutput: `${output}.memory` },
|
||||
onSpawn: (spawned) => {
|
||||
child = spawned;
|
||||
|
||||
@@ -10,6 +10,7 @@ import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { z } from "zod";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omni-log-export-ext-"));
|
||||
process.env.DATA_DIR = TEST_DATA_DIR;
|
||||
@@ -104,8 +105,10 @@ test("runner_ThirdPartyDestination_DrivesTheWholePipelineWithNoBigQueryCode", as
|
||||
}),
|
||||
});
|
||||
|
||||
const [, coveragePreload] = nativeCoverageArgs();
|
||||
runner.__setLogExportChildOptionsForTest({
|
||||
modulePath: path.join(import.meta.dirname, "fixtures/logExportExtensibilityDestination.ts"),
|
||||
coveragePreload,
|
||||
});
|
||||
const result = await runner.runDestinationExport(destination);
|
||||
const childOutput = JSON.parse(
|
||||
|
||||
@@ -8,6 +8,7 @@ import { createHmac } from "node:crypto";
|
||||
import { once } from "node:events";
|
||||
import { getLogExportDestinationType } from "../../src/lib/logExport/registry.ts";
|
||||
import type { LogExportRecord } from "../../src/lib/logExport/types.ts";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
import { createManagementSessionToken } from "../helpers/managementSession.ts";
|
||||
|
||||
const packageVersion: string = JSON.parse(
|
||||
@@ -34,6 +35,7 @@ const source = await import("../../src/lib/usage/callLogExportSource.ts");
|
||||
const route = await import("../../src/app/api/log-export/destinations/route.ts");
|
||||
const oneRoute = await import("../../src/app/api/log-export/destinations/[id]/route.ts");
|
||||
const otlp = await import("../../src/lib/logExport/destinations/otlpHttp.ts");
|
||||
const [, coveragePreload] = nativeCoverageArgs();
|
||||
|
||||
type Payload = ReturnType<typeof otlp.buildOtlpPayload>;
|
||||
type Span = Payload["resourceSpans"][number]["scopeSpans"][number]["spans"][number];
|
||||
@@ -102,6 +104,15 @@ function attributes(span: Span) {
|
||||
const str = (stringValue: string) => ({ stringValue });
|
||||
const int = (value: number) => ({ intValue: String(value) });
|
||||
|
||||
if (coveragePreload) {
|
||||
test.beforeEach(() => {
|
||||
runner.__setLogExportChildOptionsForTest({ coveragePreload });
|
||||
});
|
||||
test.afterEach(() => {
|
||||
runner.__setLogExportChildOptionsForTest(null);
|
||||
});
|
||||
}
|
||||
|
||||
test.after(() => {
|
||||
core.resetDbInstance();
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
@@ -572,7 +583,7 @@ test("400 permanent excerpt redacts echoed header values; redirects never forwar
|
||||
}
|
||||
});
|
||||
|
||||
test("429 respects Retry-After then succeeds; 5xx and timeout retries bounded and throw", async () => {
|
||||
test("429 respects Retry-After then succeeds; 5xx and network retries are bounded", async () => {
|
||||
const s = await server((_q, r, _b, i) => {
|
||||
r.writeHead(i === 1 ? 429 : 200, { "Retry-After": "1" }).end();
|
||||
});
|
||||
@@ -584,7 +595,7 @@ test("429 respects Retry-After then succeeds; 5xx and timeout retries bounded an
|
||||
} finally {
|
||||
await s.close();
|
||||
}
|
||||
for (const mode of ["5xx", "timeout", "network"]) {
|
||||
for (const mode of ["5xx", "network"]) {
|
||||
const s = await server((q, r) => {
|
||||
if (mode === "5xx") r.writeHead(503).end("unavailable");
|
||||
if (mode === "network") q.socket.destroy();
|
||||
@@ -592,17 +603,13 @@ test("429 respects Retry-After then succeeds; 5xx and timeout retries bounded an
|
||||
try {
|
||||
await assert.rejects(
|
||||
otlp.otlpHttpDestination
|
||||
.createClient(
|
||||
config({ endpoint: s.endpoint, timeoutMs: mode === "timeout" ? 30 : 5_000 })
|
||||
)
|
||||
.createClient(config({ endpoint: s.endpoint, timeoutMs: 5_000 }))
|
||||
.send([record]),
|
||||
{
|
||||
message:
|
||||
mode === "5xx"
|
||||
? "OTLP/HTTP retryable status 503 (3 attempts, 1 spans not acknowledged)"
|
||||
: mode === "timeout"
|
||||
? "OTLP/HTTP request timed out"
|
||||
: "OTLP/HTTP network request failed",
|
||||
: "OTLP/HTTP network request failed",
|
||||
}
|
||||
);
|
||||
assert.equal(s.calls.length, 3);
|
||||
@@ -612,6 +619,41 @@ test("429 respects Retry-After then succeeds; 5xx and timeout retries bounded an
|
||||
}
|
||||
});
|
||||
|
||||
test(
|
||||
"OTLP request timeouts are classified and retried three times",
|
||||
{ timeout: 10_000 },
|
||||
async (t) => {
|
||||
const nativeTimeout = AbortSignal.timeout.bind(AbortSignal);
|
||||
const pending: AbortController[] = [];
|
||||
const ms = 30;
|
||||
const timeoutCalls = t.mock.method(AbortSignal, "timeout", (duration: number) => {
|
||||
if (duration !== ms) return nativeTimeout(duration);
|
||||
const controller = new AbortController();
|
||||
pending.push(controller);
|
||||
return controller.signal;
|
||||
});
|
||||
const s = await server((_req, _res, _body, request) => {
|
||||
pending[request - 1]?.abort(new DOMException("fixture timeout", "TimeoutError"));
|
||||
});
|
||||
t.after(s.close);
|
||||
await assert.rejects(
|
||||
otlp.otlpHttpDestination
|
||||
.createClient(config({ endpoint: s.endpoint, timeoutMs: ms }))
|
||||
.send([record]),
|
||||
{ message: "OTLP/HTTP request timed out" }
|
||||
);
|
||||
assert.equal(s.calls.length, 3);
|
||||
assert.deepEqual(
|
||||
timeoutCalls.mock.calls
|
||||
.filter((call) => call.arguments[0] === ms)
|
||||
.map((call) => call.arguments),
|
||||
[[ms], [ms], [ms]]
|
||||
);
|
||||
assert.equal(new Set(pending.map((controller) => controller.signal)).size, 3);
|
||||
assert.ok(pending.every((controller) => controller.signal.aborted));
|
||||
}
|
||||
);
|
||||
|
||||
test("chunk requests obey byte and row caps; a single oversize span fails without sending", async () => {
|
||||
const s = await server((_q, r) => {
|
||||
r.writeHead(200).end();
|
||||
@@ -728,6 +770,7 @@ test("child runner end-to-end: cursor advances filtered rows, ttft mapped, secre
|
||||
const originalError = console.error;
|
||||
console.error = (...args: unknown[]) => output.push(args.join(" "));
|
||||
runner.__setLogExportChildOptionsForTest({
|
||||
coveragePreload,
|
||||
onSpawn: (child) => {
|
||||
assert.equal(JSON.stringify(child.spawnargs).includes(secret), false);
|
||||
child.stdout?.on("data", (c: Buffer) => output.push(c.toString()));
|
||||
@@ -845,6 +888,7 @@ test("real runner marks failure, retains pending rows and resends identical span
|
||||
let spawned = 0;
|
||||
runner.__setLogExportChildOptionsForTest({
|
||||
modulePath: "",
|
||||
coveragePreload,
|
||||
onSpawn(child) {
|
||||
spawned++;
|
||||
child.stdout?.on("data", (c: Buffer) => output.push(c.toString()));
|
||||
|
||||
@@ -4,6 +4,7 @@ import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { generateKeyPairSync } from "node:crypto";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omni-log-export-"));
|
||||
process.env.DATA_DIR = TEST_DATA_DIR;
|
||||
@@ -48,8 +49,10 @@ function installFetchStub(
|
||||
) {
|
||||
const output = path.join(TEST_DATA_DIR, "bigquery-batches.jsonl");
|
||||
fs.rmSync(output, { force: true });
|
||||
const [, coveragePreload] = nativeCoverageArgs();
|
||||
runner.__setLogExportChildOptionsForTest({
|
||||
modulePath: path.join(import.meta.dirname, "fixtures/logExportTestDestination.ts"),
|
||||
coveragePreload,
|
||||
config: {
|
||||
fixtureFailInsert: options.failInsert,
|
||||
fixtureInsertDelayMs: options.insertDelayMs,
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
summarizeSettledGrowth,
|
||||
withRawSnapshotCleanup,
|
||||
} from "../../scripts/perf/messages-route-memory-profile.ts";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const SCRIPT = new URL("../../scripts/perf/messages-route-memory-profile.ts", import.meta.url);
|
||||
|
||||
@@ -62,6 +63,8 @@ test("JON-562 worker environment is an allowlist and drops credential-shaped var
|
||||
LINEAR_API_KEY: "must-not-cross",
|
||||
OPENAI_API_KEY: "must-not-cross",
|
||||
OMNIROUTE_MANAGEMENT_TOKEN: "must-not-cross",
|
||||
NODE_OPTIONS: "--no-warnings",
|
||||
OMNIROUTE_CI_SYNC_TSX_CACHE: "/tmp/operator-tsx-cache",
|
||||
});
|
||||
|
||||
assert.deepEqual(env, {
|
||||
@@ -280,6 +283,7 @@ test(
|
||||
child = spawn(
|
||||
process.execPath,
|
||||
[
|
||||
...nativeCoverageArgs(),
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
// fileURLToPath, not SCRIPT.pathname: URL pathname is not a spawnable entry path on
|
||||
@@ -346,6 +350,7 @@ test(
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
...nativeCoverageArgs(),
|
||||
"--expose-gc",
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
|
||||
@@ -6,6 +6,7 @@ import { join } from "node:path";
|
||||
import test from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { promisify } from "node:util";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
const fixturePath = fileURLToPath(
|
||||
@@ -51,7 +52,14 @@ test(
|
||||
try {
|
||||
const child = await execFileAsync(
|
||||
process.execPath,
|
||||
["--import", "tsx/esm", "--test", "--test-concurrency=1", fixturePath],
|
||||
[
|
||||
...nativeCoverageArgs(),
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
"--test",
|
||||
"--test-concurrency=1",
|
||||
fixturePath,
|
||||
],
|
||||
{
|
||||
cwd: process.cwd(),
|
||||
encoding: "utf8",
|
||||
|
||||
@@ -5,6 +5,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import test from "node:test";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const FIXTURE = fileURLToPath(
|
||||
new URL("../fixtures/perplexity-web-stream-error-boundary.fixture.ts", import.meta.url)
|
||||
@@ -41,7 +42,7 @@ function runIsolatedFixture(testRoot: string): Promise<FixtureResult> {
|
||||
// Pin the reporter: the assertions read spec output, and Node 22 defaults to tap when stdout is a pipe.
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
["--import", "tsx/esm", "--test", "--test-reporter=spec", FIXTURE],
|
||||
[...nativeCoverageArgs(), "--import", "tsx/esm", "--test", "--test-reporter=spec", FIXTURE],
|
||||
{
|
||||
cwd: process.cwd(),
|
||||
env: childEnv,
|
||||
|
||||
@@ -9,7 +9,8 @@ fs.mkdirSync(path.resolve("_artifacts"), { recursive: true });
|
||||
const fixtureDir = fs.mkdtempSync(path.resolve("_artifacts/traffic-db-"));
|
||||
const originalEnv = { ...process.env };
|
||||
for (const key of Object.keys(process.env)) delete process.env[key];
|
||||
for (const key of ["PATH", "SystemRoot", "WINDIR", "COMSPEC", "PATHEXT"]) {
|
||||
// Keep Node source maps hydrated during coverage of the dynamic imports.
|
||||
for (const key of ["PATH", "SystemRoot", "WINDIR", "COMSPEC", "PATHEXT", "NODE_V8_COVERAGE"]) {
|
||||
if (originalEnv[key]) process.env[key] = originalEnv[key];
|
||||
}
|
||||
for (const key of ["DATA_DIR", "HOME", "USERPROFILE", "APPDATA", "LOCALAPPDATA", "TEMP", "TMP"]) {
|
||||
|
||||
@@ -9,7 +9,16 @@ fs.mkdirSync(path.resolve("_artifacts"), { recursive: true });
|
||||
const fixtureDir = fs.mkdtempSync(path.resolve("_artifacts/traffic-migration-"));
|
||||
const originalEnv = { ...process.env };
|
||||
for (const key of Object.keys(process.env)) delete process.env[key];
|
||||
for (const key of ["PATH", "SystemRoot", "WINDIR", "COMSPEC", "PATHEXT", "NODE_TEST_CONTEXT"]) {
|
||||
// Keep Node source maps hydrated during coverage of the dynamic imports.
|
||||
for (const key of [
|
||||
"PATH",
|
||||
"SystemRoot",
|
||||
"WINDIR",
|
||||
"COMSPEC",
|
||||
"PATHEXT",
|
||||
"NODE_V8_COVERAGE",
|
||||
"NODE_TEST_CONTEXT",
|
||||
]) {
|
||||
if (originalEnv[key]) process.env[key] = originalEnv[key];
|
||||
}
|
||||
for (const key of ["DATA_DIR", "HOME", "USERPROFILE", "APPDATA", "LOCALAPPDATA", "TEMP", "TMP"]) {
|
||||
|
||||
@@ -9,7 +9,16 @@ fs.mkdirSync(path.resolve("_artifacts"), { recursive: true });
|
||||
const fixtureDir = fs.mkdtempSync(path.resolve("_artifacts/traffic-transport-"));
|
||||
const originalEnv = { ...process.env };
|
||||
for (const key of Object.keys(process.env)) delete process.env[key];
|
||||
for (const key of ["PATH", "SystemRoot", "WINDIR", "COMSPEC", "PATHEXT", "NODE_TEST_CONTEXT"]) {
|
||||
// Keep Node source maps hydrated during coverage of the dynamic imports.
|
||||
for (const key of [
|
||||
"PATH",
|
||||
"SystemRoot",
|
||||
"WINDIR",
|
||||
"COMSPEC",
|
||||
"PATHEXT",
|
||||
"NODE_V8_COVERAGE",
|
||||
"NODE_TEST_CONTEXT",
|
||||
]) {
|
||||
if (originalEnv[key]) process.env[key] = originalEnv[key];
|
||||
}
|
||||
for (const key of ["DATA_DIR", "HOME", "USERPROFILE", "APPDATA", "LOCALAPPDATA", "TEMP", "TMP"]) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
// Gera o export estável do catálogo (scripts/release/radar-export.mjs) e valida
|
||||
// o contrato consumido pelo OmniRoute Radar + a proveniência (D16: desconhecido
|
||||
@@ -17,7 +18,7 @@ const SCRIPT = path.join(REPO, "scripts/release/radar-export.mjs");
|
||||
function runExport(extraEnv = {}) {
|
||||
const outDir = fs.mkdtempSync(path.join(os.tmpdir(), "radar-export-"));
|
||||
const outPath = path.join(outDir, "export-omniroute.json");
|
||||
execFileSync("node", ["--import", "tsx/esm", SCRIPT, outPath], {
|
||||
execFileSync("node", [...nativeCoverageArgs(), "--import", "tsx/esm", SCRIPT, outPath], {
|
||||
cwd: REPO,
|
||||
stdio: ["ignore", "ignore", "inherit"],
|
||||
// Base limpa: sem herdar GITHUB_* do ambiente do CI que roda os testes.
|
||||
|
||||
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import test from "node:test";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const REPO_ROOT = fileURLToPath(new URL("../..", import.meta.url));
|
||||
const FIXTURE = fileURLToPath(
|
||||
@@ -43,7 +44,15 @@ function buildFixtureEnv(): NodeJS.ProcessEnv {
|
||||
test("generic stream public error boundaries pass in an isolated process", () => {
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
["--import", "tsx/esm", "--import", "./open-sse/utils/setupPolyfill.ts", "--test", FIXTURE],
|
||||
[
|
||||
...nativeCoverageArgs(),
|
||||
"--import",
|
||||
"tsx/esm",
|
||||
"--import",
|
||||
"./open-sse/utils/setupPolyfill.ts",
|
||||
"--test",
|
||||
FIXTURE,
|
||||
],
|
||||
{
|
||||
cwd: REPO_ROOT,
|
||||
encoding: "utf8",
|
||||
|
||||
@@ -3,6 +3,7 @@ import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { getSettledModelCatalogRouteResponse } from "../_setup/modelCatalogFixture.ts";
|
||||
|
||||
const TEST_DATA_DIR = fs.mkdtempSync(
|
||||
path.join(os.tmpdir(), "omniroute-vscode-token-routes-gpt56-")
|
||||
@@ -14,6 +15,7 @@ const core = await import("../../src/lib/db/core.ts");
|
||||
const providersDb = await import("../../src/lib/db/providers.ts");
|
||||
const settingsDb = await import("../../src/lib/db/settings.ts");
|
||||
const apiKeysDb = await import("../../src/lib/db/apiKeys.ts");
|
||||
const v1ModelsCatalog = await import("../../src/app/api/v1/models/catalog.ts");
|
||||
const vscodeRawModelsRoute =
|
||||
await import("../../src/app/api/v1/vscode/raw/[token]/models/route.ts");
|
||||
|
||||
@@ -60,8 +62,10 @@ test("vscode models route preserves gateway-owned Ollama Cloud effort tiers", as
|
||||
);
|
||||
const vscodeModelsRoute = await import("../../src/app/api/v1/vscode/[token]/models/route.ts");
|
||||
|
||||
const response = await vscodeModelsRoute.GET(
|
||||
new Request(`http://localhost/api/v1/vscode/${encodeURIComponent(key.key)}/models`)
|
||||
const response = await getSettledModelCatalogRouteResponse(v1ModelsCatalog, () =>
|
||||
vscodeModelsRoute.GET(
|
||||
new Request(`http://localhost/api/v1/vscode/${encodeURIComponent(key.key)}/models`)
|
||||
)
|
||||
);
|
||||
const body = (await response.json()) as {
|
||||
data?: Array<{
|
||||
@@ -105,8 +109,10 @@ test("vscode raw models route exposes native GPT-5.6 IDs and effort tiers", asyn
|
||||
"machine-vscode-raw-models-codex"
|
||||
);
|
||||
|
||||
const response = await vscodeRawModelsRoute.GET(
|
||||
new Request(`http://localhost/api/v1/vscode/raw/${encodeURIComponent(key.key)}/models`)
|
||||
const response = await getSettledModelCatalogRouteResponse(v1ModelsCatalog, () =>
|
||||
vscodeRawModelsRoute.GET(
|
||||
new Request(`http://localhost/api/v1/vscode/raw/${encodeURIComponent(key.key)}/models`)
|
||||
)
|
||||
);
|
||||
const body = (await response.json()) as { data?: RawModel[] };
|
||||
const models = body.data ?? [];
|
||||
|
||||
@@ -5,6 +5,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const fixture = fileURLToPath(
|
||||
new URL("../fixtures/zai-web-stream-error-boundary.fixture.ts", import.meta.url)
|
||||
@@ -23,12 +24,16 @@ test("Z.ai stream error boundaries pass in a process-isolated fixture", () => {
|
||||
delete childEnv.NODE_TEST_CONTEXT;
|
||||
|
||||
try {
|
||||
const result = spawnSync(process.execPath, ["--import", "tsx/esm", "--test", fixture], {
|
||||
cwd: fileURLToPath(new URL("../..", import.meta.url)),
|
||||
encoding: "utf8",
|
||||
env: childEnv,
|
||||
timeout: 60_000,
|
||||
});
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[...nativeCoverageArgs(), "--import", "tsx/esm", "--test", fixture],
|
||||
{
|
||||
cwd: fileURLToPath(new URL("../..", import.meta.url)),
|
||||
encoding: "utf8",
|
||||
env: childEnv,
|
||||
timeout: 60_000,
|
||||
}
|
||||
);
|
||||
const diagnostics = [result.stdout, result.stderr].filter(Boolean).join("\n");
|
||||
|
||||
assert.equal(result.error, undefined, diagnostics);
|
||||
|
||||
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
|
||||
import { spawn } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import test from "node:test";
|
||||
import { nativeCoverageArgs } from "../_helpers/nativeCoverageArgs.ts";
|
||||
|
||||
const repoRoot = fileURLToPath(new URL("../../", import.meta.url));
|
||||
const fixturePath = fileURLToPath(
|
||||
@@ -34,11 +35,15 @@ function runFixture(): Promise<FixtureResult> {
|
||||
delete childEnv.NODE_TEST_CONTEXT;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(process.execPath, ["--import", "tsx/esm", "--test", fixturePath], {
|
||||
cwd: repoRoot,
|
||||
env: childEnv,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[...nativeCoverageArgs(), "--import", "tsx/esm", "--test", fixturePath],
|
||||
{
|
||||
cwd: repoRoot,
|
||||
env: childEnv,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
}
|
||||
);
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
let timedOut = false;
|
||||
|
||||
Reference in New Issue
Block a user