GitNexus/gitnexus/tools/scenario-map/mutation/row-deletion.ts
Gergő Magyar bad2422aa1
feat: add the test scenario map and a Vitest 5 mutation runner (#3686)
* feat(ci): add advisory duplicate-test scan to the PR report

Adds `npm run test:dupes` (gitnexus/scripts/test-dupes.ts) and a
comment-only CI job that reports, for the lines a PR changes:

- same-test: same body and same setup as an existing test
- assertions-pinned: every assertion is a literal fn(...) -> matcher
  that another test already makes
- same-fixture-setup: a before* hook builds a fixture another describe
  already builds
- copied-helper: a top-level test helper identical to one elsewhere
- orphan-bench-gate: a bench script with --check that nothing runs

A case's identity includes its hooks, describe-level statements,
wrapper arguments, loop heads, each tables, referenced file-local
declarations and resolved imports, with fixture paths compared by
value. Extra context can only split a group, so identical bodies that
run against different fixtures, mocks or env are not reported.

The ci.yml job is continue-on-error and outside the CI Gate.
ci-report.yml renders its JSON into the sticky CI comment, keeping
only whitelisted kinds and strict path/line shapes because the
artifact comes from PR code. `// dupe-ok: <reason>` silences an
intentional repeat.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(mutation): record Vitest 5 and Stryker 10 probe findings

Go/no-go probe for the Stryker Vitest 5 runner: name-filter separator, per-spec task ids, static mutant activation, TypeScript plugin loading in child processes, setup-file meta and hook ordering.

* feat(mutation): prepare a mutable copy and a Vitest mutation config

Prepared copy blanks TypeScript as-assertions (Stryker's instrumenter skips every TSAsExpression) with equal-length spaces, checked by AST equality, and relocates gitnexus-shared/src inside the package so it can be mutated. The mutation Vitest config resolves gitnexus-shared to the relocated sources and takes its test list from a scope file.

* feat(mutation): add a Stryker test runner for Vitest 5

Stryker's vitest-runner 10.0.0 runs zero tests per mutant under Vitest 5 because Vitest 5 matches testNamePattern against ' > '-joined names. This runner selects tests by Vitest task id per file, keeps the configured fork pool, shares the dry-run manifest across Stryker worker processes, attributes hook-time hits to the suite's tests, and records a per-test outcome class in sidecar files. Adds exact devDependencies @stryker-mutator/core and @stryker-mutator/api 10.0.0.

* feat(mutation): add global mutant identities and a span-map check

A global mutant identity (original repo path, original content hash, span, mutator, replacement) makes mutants comparable across Stryker runs. The span-map check proves every mutant of a prepared copy sits on the same span and text in the original source, apart from blanked as-assertions.

* feat(mutation): add a scope driver for Stryker runs

run-scope turns a scope file into a prepared copy, a generated Stryker config (vitest5 or command runner), a Stryker run and normalised results: global mutant ids, runner test ids, sidecar outcome classes, consistency flags and the span-map check.

* fix(mutation): count assertion kills only for killed runs

A run classified timeout, inconclusive, invalid or infrastructure can still carry assertion rows (a test failing after a swallowed hit-limit error, or under a retry). Those kills are not trustworthy, so assertionKilledBy is now empty unless the run's status is killed. Found by the conformance suite.

* feat(mutation): add the runner conformance suite (gate 2)

A fixture project under test/fixtures/mutation-conformance pins 23 runner behaviours (static and collection-time mutants, beforeAll attribution, disableBail, it.each row ids, outcome classes for runtime, hook, collection, timeout, infrastructure, retry and zero-test runs, NoCoverage, environment isolation, runner reuse and manifest sharing) in two scheduling profiles. The path map and scope driver accept an explicit gitnexus/test/fixtures/mutation-* root for it.

* feat(mutation): classify pure-JS unit tests for mutation runs

Walks each unit test's import closure (static imports plus statically resolvable dynamic imports, require and createRequire) and rejects tests that reach native packages, worker threads or child processes, read source or fixture files a prepared copy would break, or contain unresolvable dynamic imports. 247 of 911 unit test files are pure JS.

* feat(mutation): add gate 1 execution-path fidelity and the envelope record

Gate 1 compares a normal Vitest 5 run of the pure-JS category with the runner's unmutated Stryker run: test identities with locations, outcomes, effective config against an allowed-difference list, resolved module paths (everything inside the active sandbox) and a runtime load check for native bindings, worker construction and child processes. It passes on 246 files / 4541 tests and writes test/fixtures/mutation-oracle/envelope.json. The runtime check found commander loading child_process, so the classifier now rejects tests importing commander.

* feat(mutation): add the oracle's command-runner run and classifier

The Vitest 5 cross-check oracle runs its own Stryker run with the command runner (cleanTempDir false). A wrapper runs the scope's tests per mutant with an oracle reporter and writes a per-mutant record, and the oracle classifies every mutant into the KTD17 outcome classes with its own classifier. Oracle code imports no runner code.

* feat(mutation): add the oracle single-test launcher and cost benchmark

launch-single runs one test with one mutant in a fresh process inside the oracle's kept sandbox, selected by file:line plus an anchored name pattern, with the mutant installed before any import, an oracle setup file that proves the installed id and buckets hits (load and collection, hook, per-test), a wall-clock limit and a process-group kill. A launch is valid only when exactly the intended test ran. The benchmark projects the three-scope oracle sample's cost on 4 vCPU. A boundary test keeps oracle code from importing runner code.

* feat(mutation): add the oracle freeze and gate 3 compare

freeze samples mutants per scope with a recorded seed, tops up strata, counts stratum populations from the oracle's own coverage probe, launches every sampled mutant against every scope test (resumable), and writes a manifest keyed by content hashes of the scope's import closure, Vitest configs and prepare tool. compare checks freshness, inventories (tests and global mutant ids) before outcomes, every mutant's class and sampled per-test outcomes, allowing only the four KTD13 normalisations.

* feat(mutation): add gate 4 positive controls and determinism

Nine planted controls, selected from the scopes' own Stryker mutants by global id (three in relocated gitnexus-shared as-const tables), must be killed by exactly their expected killers. Two scheduling profiles (concurrency 1 with unlimited runner reuse, concurrency 3 with one mutant per runner process) must give identical outcome classes and kill sets.

* ci(mutation): add an advisory mutation-runner validation workflow

Runs the runner conformance suite (gate 2) on pull requests that touch the mutation tooling, its fixtures, the gitnexus manifest, lockfile or Vitest config. workflow_dispatch also runs gate 1, the gate 3 oracle compare (or a per-scope re-freeze whose manifests are uploaded, never committed), gate 4 and the oracle cost benchmark. All jobs are advisory. Documents the tooling in TESTING.md and the residual deltas against upstream StrykerJS.

* test(mutation): freeze the gate 3 oracle manifests and record Phase 0 results

Frozen Vitest 5 cross-check oracle manifests for the three pure-JS scopes (seed u7-gate3-1): shared.ts, schema-constants.ts and middleware.ts. Gate 3 passes: all 1,606 mutant classes and all 20,455 sampled per-test outcomes agree with the runner, 0 differences. Adds the approved Test Scenario Map design (updated with Phase 0 findings and gate results) and the Phase 0 implementation plan.

* refactor(mutation): simplify the Phase 0 tooling

Shared CLI/fs helpers for the non-oracle tools, an oracle-local util for isObject and atomic writes, formatMutateEntry and formatDuration reused, a typed isUnusableClass, and hot-path fixes: per-sidecar index in the gate 3 compare, lazy suite-chain in the runner's dry-run hit attribution, one Classifier per freeze, memoised display paths and Vitest lookups. Behaviour unchanged: unit tests, gate 3 compare (byte-identical report) and the conformance suite (23/23) pass.

* fix(review): apply review findings

- Move the duplicate-test scanner out of the published scripts/ directory to gitnexus/tools/test-dupes/ so it no longer ships in the npm package.
- Scope the orphan bench gate to touched lines, like every other scanner finding.
- Let the conformance environment case reach Stryker through an explicit childEnv override, so it exercises the runner's own __STRYKER_ACTIVE_MUTANT__ deletion (a negative check now fails it).
- Do not raise the zero-tests flag when a timeout outside-test row explains the zero.
- Run Stryker in its own process group and signal the group on cancellation.
- Fingerprint conformance inputs and refuse stale artefacts in --compare-only.
- Path-gate the mutation workflow on gitnexus-shared/package.json and the setup action; anchor the PR-report path filter with \A...\z.

* chore(security): mark the runner's provide/inject key as a gitleaks false positive

gitleaks' generic-api-key rule matched PROTOCOL_KEY = 'gitnexusStrykerVitest5' (a Vitest provide/inject key name). Allow the line inline and ignore the fingerprint in the commit that introduced it.

* test(mutation): re-freeze the schema-constants oracle manifest

Merging main changed the scope's import closure (template-arguments.ts),
so gate 3 reported re-freeze required. Re-frozen with seed u7-gate3-2:
72 sampled mutants, 3024 valid observations, 0 invalid. Gate 3 now
passes on all three scopes (shared 1232, schema-constants 72,
middleware 302 mutants) with 0 differences.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(plans): add the Test Scenario Map Phase 1 plan

* chore(security): stop the gitleaks allowlist comment from quoting the key

The previous comment quoted the provide/inject key assignment and gitleaks
flagged the comment line itself (generic-api-key). Reword it and allowlist
that historical finding by fingerprint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(mutation): select single tests in the vitest5 runner and keep per-test classes

Phase 1 U1 (KTD2, KTD9, KTD16):
- driver scope accepts optional testIds, passed as the vitest5.testIds
  runner option; the dry run collects, maps ids to task ids, runs only
  those tests and fails before any mutant run on unknown ids
- the dry-run sidecar (schema 2) carries the hook marker: per mutant,
  the tests a suite hook attributed it to
- results.json (schema 2) keeps every sidecar attempt's per-test rows
  (perTest), attemptsDisagree and hookAttributedTests
- gate 2 gains a selection scope and hook-marker cases (26 cases hold)

Gate 3 hashed inputs are untouched.

* refactor(test-dupes): export Layer 0's case extraction for the scenario map

Phase 1 U2 (KTD6): export extract, extractCases, buildFrame, closure,
staticValue, suppressed and dupeOkReason, plus a per-case record with
the describe path, title, context hash, body, each-table node and
enclosing loop head. Layer 0's --all scan output is byte-identical
before and after (1464 files, 114 findings).

* test(mutation): refresh the gate 1 envelope after the runner extensions

Gate 1 re-ran over the current pure-JS category after U1 and passed with
0 differences: 251 files, 4667 tests. The classifier now rejects
conformance-compare.test.ts (imports the child_process-spawning
conformance runner) and php-namespace-extraction.test.ts (now reaches
tree-sitter through main), and accepts the three oracle unit tests.

* feat(scenario-map): add the eligible-test inventory, identity join and anchors

Phase 1 U3 (R1, R2, R15; KTD3, KTD7):
- eligibility = gate 1 envelope intersect a fresh classifier run, with the
  delta; controls mode enrols a fixture's *.case.ts files
- static expansion of it.each/test.each/test.for tables and literal for-of
  loops into per-row instances; anything not statically evaluable is
  unresolved (fail closed)
- a pure join of static instances to runtime records through Phase 0
  test ids; any count or name mismatch makes the tests unsupported
- continuity anchors (no line; row and body fingerprints) and the one
  production-file predicate shared by every later stage
- test-dupes: additive exports (shapeOf, roleOf, eachTableOf, ASTs, an
  isTest option); Layer 0 output byte-identical

Over the repo: 4667 runtime tests, 4597 joined one-to-one, 70 unsupported
(5 tables built at runtime), 0 mismatches.

* test(scenario-map): add the controls fixture project with expected verdicts

Phase 1 U17: one pair per design §8 pure-JS adversarial case (Math.round,
parseInt radix, localeCompare, normalize, Date boundary, regex
alternatives, numeric boundary with a constants-only module, an
expected-only difference, a derived value reaching toFixed, an
enum-member-only difference), an order-dependent pair, an object-key row,
and positive controls (verbatim duplicate, repo arithmetic, Set table
reads). expected.json was written from the design's stated outcomes.
The fixture has its own Vitest project and tsconfig; no repo project
collects it.

* feat(scenario-map): extract per-instance checks and context

Phase 1 U5 (R5, KTD6):
- checks: the shape of each expect(...).matcher(...) with literals
  abstracted and selector literals in the subject kept exact; snapshots
  hashed from inline text or the .snap entry; a documented supported
  matcher list, anything else unsupported; per-row for it.each/test.for
- context: Layer 0's context hash plus body-level mocks/env writes and
  the test call's own modifiers and options
- checksContextKey for the second candidate source

Over the 251 eligible files: 4600 instances in 2.9 s, 183 with an
unsupported form, 37 with an unresolved selector.

* feat(scenario-map): add Layer 1 per-test V8 coverage and candidate retrieval

Phase 1 U4 (R3, R4; KTD4, KTD5):
- a Vitest setup file records per-test V8 block coverage in a prepared copy
  under the mutation config (gitnexus-shared runs from source), with design
  §5.2 attribution: module load discarded, a suite's beforeAll delta taken
  at its first beforeEach, concurrent tests flagged unattributable
- provenance per test: executed files, native-package entries and worker or
  child-process spawn call sites
- candidates: exact Jaccard >= 0.99 (size-ratio and prefix filters) plus
  identical checks-and-context buckets; connected components schedule
  Layer 3 only

Over the 251 eligible files: 4667 tests in 1:55 (82.8 s Vitest), 6281
candidate pairs in 611 groups, largest group 36 tests. Controls: 16/16
pairs are candidates, 0 cross-pair candidates. The directory is
path-coverage/ because coverage/ is ignored by git, prettier and eslint.

* feat(scenario-map): compute the per-instance input facet

Phase 1 U6 (R5, R9; KTD6), porting the input-facet spike without its
hard-coded inputs:
- rules 1-8: tree-sitter shapes of inline source, the COBOL profile,
  best-fit production tables (executed path, 2-hop import closure,
  property columns; never pooled), selector literals (reusing the checks
  predicate), object keys, source order, shared fixtures, fixture files
- numeric literals with 0/1/-1/negatives exact, and the +-1 rule against
  production numeric constants from executed files and the static import
  closure (folded initialisers included)
- discarded values per position with source locations for value flow
- the residual-difference guard: a body difference the facet misses makes
  the pair facet-unequal and both instances unresolved

Controls: all 16 pairs match expected.json. The spike's untouched
holdout-4 still scores 23/23 distinct. Eligible run: 4667 instances in
about 33 s, 245 unresolved.

* feat(scenario-map): run Layer 3 per candidate group and merge raw observations

Phase 1 U7 (R6, R7, R11; KTD7, KTD8, KTD14, KTD17):
- whole-file mutate scope per group from Layer 1 provenance and the
  static closure; executed-but-unmutated repo files mark tests excluded
- a run manifest of the prepared inputs that every shard verifies
- two plain Vitest 5 baselines (fresh instance per run) plus the Stryker
  dry run must agree; the generated config must carry perTest coverage,
  disableBail and static mutants before mutating
- raw observations from every sidecar attempt, keyed by group, run kind,
  lineage, attempt, test and mutant; class disagreement is inconclusive
  and a missing result is unknown, never a non-kill
- deterministic shard planning that never splits a group, with
  group-over-budget and start-only-if-it-fits rules

Controls: all 16 pairs ran as real groups; every kill set matches
expected.json.

* feat(scenario-map): add the full isolation pass for repeat-stage pairs

Phase 1 U8 (R7; KTD8, KTD9):
- select repeat-stage pairs (same coveredBy path, checks, context, facet
  without a residual difference, and the same non-empty kill set)
- per pair one together run and one alone run per test (shared across
  pairs), mutate restricted to the covered universe, a fresh runner
  process per mutant, all behind the same KTD8 baseline and config checks
  (runGroup's steps moved into runMutationRun)
- coverage and outcome class per test and mutant must agree across main,
  together and alone; a missing rerun result is unknown; hook-attributed
  mutants cap the pair at weak

Controls: order-dependent ends disagrees; verbatim-duplicate,
repo-arithmetic and set-non-member agree, matching expected.json.
Measured fresh-process cost: about 5.5 s per mutant.

* feat(scenario-map): add the row-deletion operator

Phase 1 U9 (R8, KTD10):
- find the one supported production table (object literal with plain
  keys, literal Set/Map) holding both differing values; spreads, computed
  keys, Object.assign, writes, getters, proxies, duplicate keys or an
  ambiguous table make the outcome inconclusive
- splice one key's text in a throwaway prepared copy, run the pair with a
  fresh Vitest instance per run after a control run that proves both pass
  and the edited file loaded
- symmetric / asymmetric (Row) / not applicable / inconclusive, with
  RowDeletion global mutant ids
- vitest-run: an opt-in load probe (probeFiles, loadedFiles)

Controls: all 16 pairs match expected.json (object-key-row and
set-membership asymmetric).

* feat(scenario-map): add the static value-flow check

Phase 1 U10 (R9, KTD11): for facet-equal pairs whose discarded values
differ, all sources must come back clean or the pair is vetoed to Review:
- Babel binds each differing value to its entry (call argument, fixture
  field, table key); an expected-only difference is unknown
- the TypeScript 7.0.2 unstable checker resolves entries and callees
  through aliases; a version mismatch degrades the whole run
- a walk stitched across functions over GitNexus PDG REACHING_DEF and
  CALL_SUMMARY edges (pdg_query flows are intra-procedural): opaque
  sinks (lib/@types/node_modules, any regex) veto, truncation, dynamic
  dispatch, a depth cap or a missing PDG layer are unknown or degraded;
  repo code is clean only inside the pair's mutated files
- row-deletion exports scanSupportedTables so table reads mean exactly
  the tables row deletion supports

Controls: 0 mismatches against expected.json. Whole-repo analyze --pdg:
6:49 wall, 8.4 GB peak RSS.

* feat(scenario-map): add the value-flow trace run

Phase 1 U11 (R9, KTD11): one traced Vitest 5 run over the tests of every
facet-equal candidate pair, with a setup file that wraps value-sensitive
built-ins (Math, string transforms and comparisons, RegExp, number
parsing and formatting, JSON, Intl, Date, sort, plus U10's opaque
callees) and records, per test and production call site, the receiver,
arguments and count. Storage and table reads stay unwrapped.
A shared site with a different receiver or argument, or asymmetric
sites or counts, vetoes the pair; a test whose outcome changes under
tracing makes it inconclusive.

Controls: all 13 pairs with a trace expectation match expected.json,
including receivers 24 and 25 reaching toFixed. Overhead on 40 eligible
files: 1.19x pooled, no outcome changes.

* feat(scenario-map): add the verdict engine

Phase 1 U12 (R10, R11, R15; KTD5, KTD8, KTD9, KTD12): a pure function
over merged observations and every stage's evidence. Design §2
precedence as an ordered rule list (Inconclusive, Out of scope, Weak,
Repeat strong, Repeat weak, Row, Review, Distinct), with the value-flow
and trace vetoes as a gate before any repeat category and the residual
difference ending in Review. One verdict per pair with its deciding rule
and every weak disqualifier; per-test Weak, Inconclusive and Subsumed;
per-file gaps recomputed from all observations with the §5.4 wording;
dupe-ok pairs kept with their evidence. Output is deterministic.

Controls: all 16 verdicts and rules match expected.json.

* feat(scenario-map): build the report, issue body and week-over-week diff

Phase 1 U13 (R12, R13; KTD13, KTD15):
- the JSON report: run provenance (the design §7 cache-key inputs),
  completeness and degraded flags, the eligibility delta, Layer 0
  findings, every verdict with its evidence, per-test properties, gaps,
  a seeded spot audit of 10 strong candidates, N of M evaluated and the
  precision-not-measured statement
- continuity: mutant fingerprints without revision or span, universe ids
  from the pair's common universe, evidence hashes per design §5.5
- a markdown rendering and the tracking-issue body (strong repeats, weak
  tests, gaps; truncated at 60,000 characters; a run-id marker so a
  stale run never overwrites a newer body)
- baseline comparison: new, changed, unchanged, resolved, with first-run
  and incompatible-baseline banners

* feat(scenario-map): add the CLI and the end-to-end controls run

Phase 1 U14 (R14, R16; KTD1, KTD14, KTD17): npm run test:scenario-map
with plan, shard, value-flow, merge, all and controls stages exchanging
JSON in a work directory, so CI jobs and a local run produce the same
report. Shards batch groups into one mutation run when their combined
estimate fits, and persist row-deletion and isolation results as they
finish. Exit codes are set explicitly.

controls: all 16 fixture pairs match expected.json (exit 0, 76 min).

* feat(scenario-map): fit the scheduled run into the CI budget

Phase 1 U14 follow-up (KTD14, KTD17), measured on a seeded slice of the
real category; no evidence rule is relaxed:
- value flow runs before the shards and re-plans them, so isolation is
  spent only on pairs that can still be strong (a value-flow veto or
  unknown ends Review, a trace inconclusive ends Inconclusive); a
  missing, stale or degraded value-flow result falls back to the
  provisional plan
- a shards stage prints the final shard count for the CI matrix
- --concurrency and --speed-factor feed the estimate; the main-run cost
  form is fixed + mutated bytes (measured; it does not grow with tests)
- a group whose isolation does not fit still runs its main run and row
  deletion; its pairs get isolation not-run (weak cap), and a shard
  isolates deferred pairs only when their measured universe fits
- groups that execute no production file are not run; their pairs are
  out of scope

Predicted CI run: 9 shards at concurrency 4, 1658-1829 min in total,
largest shard 196-253 min (budget 300), 0 groups over budget.
controls: 16/16 pairs match, 28 min.

* ci(scenario-map): add the weekly Test Scenario Map workflow and a PR controls job

Phase 1 U15 (R13, R14; KTD13, KTD14, KTD17):
- test-scenario-map.yml (weekly + dispatch, advisory): plan, value-flow,
  shards (sizes the matrix from the value-flow re-plan), a shard matrix
  (fail-fast off, max-parallel 10, 300-minute deadline), merge (always,
  against the newest scenario-map-baseline artifact) and upsert-issue
  (default branch only; the only job with issues: write; replaces the
  tracking issue's body only for a complete run that is not older than
  the body's run marker, comments on partial runs, never closes it)
- mutation-runner-validation.yml: a path-gated scenario-map-controls job
  that runs the controls fixture end to end on PRs touching the tooling

actionlint (with shellcheck), zizmor 1.24.1 and the concurrency check
pass. Neither workflow can run before the merge.

* docs(scenario-map): record Phase 1 in the design and the testing guide

Phase 1 U16: design corrections (the stitched PDG walk, the trace's
receivers and scope, row deletion and baselines on plain Vitest 5, the
isolation pass on testIds after value flow), the demote-only additions,
the refreshed gates, the measured Phase 1 costs and the CI topology
change; TESTING.md gains a Test Scenario Map section (stages, controls
fixture, supported matchers, wrapped built-ins, weekly workflow,
dupe-ok).

* fix(scenario-map): store observations compactly so large batches fit in memory

The first full run crashed a shard with "Invalid string length":
observations were materialised for every collected test x every mutant,
including a valid not-covered survival for each pair (1.6M objects for a
batch of 73 selected tests over 21,965 mutants; the batch file needed
about 700 MB of JSON).

- only a run's selected tests produce observations
- each run records its universe (selected tests, mutants with their
  status, reasons and hook marker); only rows, covered or static
  mutants without a row, and hook-named tests are stored explicitly; a
  not-covered survival is derived when read
- every consumer (pair comparison, isolation, verdict engine, report,
  batch split) reads through one index; group observation and isolation
  run files move to schema 2, and old files are rejected with a message
- merge lists an unreadable group result as incomplete instead of
  dropping it; repeat-stage decisions keep comparison summaries only

On the crashing batch: 141,308 explicit observations instead of 1.6M,
12.1 MB instead of an overflow, and a byte-identical verdict report.
controls: 16/16 pairs match.

* docs(scenario-map): record the Phase 1 measured slice; give the real-repo classify test headroom

- design §9: the full-category report comes from the first weekly
  workflow run; the local full run was stopped because it saturated the
  shared machine. Records the measured slice and the whole-category plan
  and value-flow figures.
- classify.test.ts: classify the real repo once in beforeAll with a
  120 s timeout; it takes about 30 s on a busy host and timed out at
  the default 30 s.

* fix(review): apply review findings and simplify the scenario-map tool

Review (ce-code-review, validated findings):
- trace: a baseline or traced run that is invalid, inconclusive, retried
  or has outside-test failures, unhandled errors, unwrapped entries or
  recorder errors makes every pair inconclusive (trace-run-invalid), never
  clean; the value-flow stage reports it and merge marks the run partial
- verdict: tests pin that a not-required static check for differing
  values reads as unknown, plus the worker-spawn, missing-provenance,
  unresolved-input and unknown-hook-marker weak caps
- workflow: the baseline comes only from this workflow's own scheduled or
  dispatched default-branch runs of this repository; an unreadable
  baseline shows the incompatible-baseline banner instead of failing merge
- controls: the oracle fails on an empty expected list, on any unlisted
  repeat verdict and on a case file without an expected pair
- value flow: the PDG analyze budget (25 min) sits below the 50-minute
  CI step; the TypeScript checker server is closed when opening fails
- --concurrency now also bounds every in-process Vitest run (maxWorkers),
  so local runs stay within the requested cores
- merge reads isolation runs tolerantly (an unreadable file makes the run
  partial); a duplicate test title is renamed; a stale TESTING.md heading
  reference is updated

Simplification (behavior-preserving; merge and plan outputs byte-identical
on fixed inputs): one util.ts for shared helpers, a run-level row-deletion
cache, one classify() per plan, memoised universe ids and single-pass
pair/subsumption/report loops.

* fix(test-dupes): keep beforeAll and beforeEach fixture builds apart

A same-fixture-setup key now includes the hook name. A beforeAll fixture is
built once per suite and a beforeEach one per test, so sharing one result
between them would drop per-test isolation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(scenario-map): temporary label-gated pull_request run for PR #3686

schedule and workflow_dispatch only run from the default branch, so the
Test Scenario Map workflow cannot run before this PR merges. Adding the
run-ci label to the PR starts one full run. A pull_request run writes no
tracking issue and no baseline. Remove the trigger before merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(scenario-map): remove the temporary pull_request trigger

The one full run on PR #3686 finished (run 40183982111, complete, 9 of 9
shards). This reverts 5d06d731c.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(scenario-map): decide pairs past unknown mutants on a kill witness

A mutant both tests ran cleanly, killed by assertion by exactly one, proves
the kill sets differ whatever the unknown and runtime-class mutants hold.
kills-unknown and non-assertion-outcome now leave such a pair Inconclusive
no longer; it goes on to Row or Distinct. A pair's empty kill set only
decides Weak on a fully known run. Verdict engine 2.

Re-merged from the artifacts of run 40183982111: Inconclusive 4532 -> 1469;
every pair engine 1 decided keeps its verdict, rule and details, and the
repeat candidates are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(scenario-map): record a test file's import failure instead of a missing result

A static mutant that breaks a module at import makes its test files fail to
collect: the runner writes one file-level killed-outside-test:collection row
and no per-test rows, so every test of the file was a missing result. Those
were every missing-result observation of run 40183982111 (597 mutants, 675
same-file pairs left Inconclusive by them alone).

Extraction now records each test of a failed file with that class. When both
tests of a pair failed to import, neither ran, so the mutant cannot tell them
apart: comparePair counts it as a non-kill for both rather than another
class. Verdict engine 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(scenario-map): temporary label-gated pull_request run for PR #3686

schedule and workflow_dispatch only run from the default branch, so the
Test Scenario Map workflow cannot run before this PR merges. Adding the
run-ci label to the PR starts one full run. A pull_request run writes no
tracking issue and no baseline. Remove the trigger before merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(scenario-map): remove the temporary pull_request trigger again

The engine 3 run on PR #3686 finished (run 40208819019, complete, 9 of 9
shards). This reverts 0e76acc09.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(scenario-map): record the engine 3 run's results

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): scenario fingerprint verdicts (same path, input, expectation)

Two tests are duplicates when they run the same execution path on the same
input data and check the same expectation. The fingerprint engine decides
that from Layer 1 block sets, Layer 2 facets and discarded values, the
checks-and-context key and the trace run, with no mutation: duplicate,
possible-duplicate (anything unresolved, fail-closed), table-row (other
input values), different-checks, distinct.

On run 40208819019 the fully resolved triple names 26 pairs: the run's 13
strong repeats and 13 more mutation could not decide, none contradicted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): lead the report with the scenario fingerprint

The report (schema 2) carries every pair's fingerprint verdict with its
continuity key and the run's mutation scope. The markdown and the tracking
issue lead with duplicates and same-scenario-different-checks pairs; the
mutation sections (strong repeats, weak tests, gaps) render only when the
run gathered mutation evidence. The spot audit samples duplicates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): mutation scope (none, possible-duplicates, all)

plan and all take --mutation. The plan computes every pair's fingerprint
before the trace and packs only the scope's target: no group (none), every
group (all), or the groups with a possible duplicate, trimmed to the tests
of those pairs and the files they executed. Value flow re-plans a narrower
scope after the trace (a usable trace only narrows it) and builds the PDG
index for scope all only. Merge reports every pair's fingerprint and the
mutation verdicts of the pairs whose tests ran in a mutated group. A plan
with nothing to mutate has no shard; all skips the shard stage then.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): check fingerprint verdicts in the controls oracle

expected.json gives every control its fingerprint verdict and rule, written
from the fingerprint rules before any run: verbatim-duplicate is the one
duplicate, enum-member a possible duplicate (residual difference),
numeric-boundary distinct, the other 13 table rows. The controls stage
compares both verdicts per pair and fails on an unlisted fingerprint
duplicate. Local controls run: all 16 pairs match on both.

value-flow checks for dist/ only when it builds the PDG index (scope
all); controls, and all with scope all, still check before any stage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(scenario-map): fingerprint weekly, mutation on dispatch or label

The weekly run is the fingerprint only (mutation scope none: no build, no
shards). Workflow dispatch takes a mutation input (none, possible-duplicates,
all). Adding the scenario-map label to a pull request starts the deep run
over the possible duplicates; it writes no issue and no baseline, and every
run puts the issue-body view in the job summary. Pull request runs get their
own concurrency slot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(scenario-map): the scenario fingerprint as the primary verdict

The design defines a duplicate as the same execution path, input data and
expectation, records the maintainer decisions of 2026-10-25 (fingerprint
weekly, mutation on dispatch or label) and the local measurement: the
fingerprint-only run took 8.2 min over all 4,632 eligible tests and named
the 26 duplicates the evidence predicted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ci): place a single shard artifact in the scenario-map merge

download-artifact unpacks a pattern that matches one artifact into the
path itself, not into a directory named after it. With a one-shard plan
(the scenario-map label's possible-duplicates run) the placement step found
no shard directory, so merge reported the shard missing and every mutated
pair Inconclusive (run 40268120818). The step now uses the download path
when no artifact directory exists. A local re-merge of that run's artifacts
gives the complete result, recorded in the design doc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): fingerprint every unit and integration test

The fingerprint needs no Stryker run, so its scope is no longer the
mutation runner's validated envelope. Eligibility now carries
fingerprintFiles (every *.test.ts under test/unit and test/integration)
next to the mutation-eligible files; a verdict input carries eligible
(mutation), fingerprintEligible and passed.

- Layer 1 and the trace run the base config's lbug-db files in a
  partition of their own with one worker (partitions.ts), the rest
  with --concurrency workers, and merge both runs.
- New fingerprint rule not-passing: a test that did not pass in
  Layer 1 makes its pairs Inconclusive.
- The mutation target keeps only pairs whose two tests are
  mutation-eligible.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): run Layer 1 in CI slices; load vendor and dist in integration runs

A sample of the integration tests showed two problems with fingerprinting
every unit and integration test:

- Half the sampled files failed to load: the prepare step (gate-3 hashed)
  leaves vendor/ and dist/ out of the prepared copy, and integration tests
  load leiden, the parse worker and the built CLI. Layer 1 and the trace now
  copy both from the checkout in normal mode, and every stage that runs tests
  (coverage, plan without --coverage, value-flow, all, controls) requires a
  built dist/.
- One plan job cannot hold Layer 1: 44 of 435 integration files ran past
  35 minutes. A new coverage stage (--index i --of n) runs one slice, split
  like the CI test shards (test/helpers/shard-balance.ts), and
  plan --coverage <dir> merges the slice files: one block table, indices
  remapped, files and tests in file order. A slice that did not report leaves
  its files unrecorded. The workflow runs 12 coverage slices before the plan.

The sequential partition now reads every fileParallelism: false project of
the base config (lbug-db and cli-e2e), not lbug-db alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(scenario-map): order-dependent controls pair is Inconclusive on the fingerprint

The not-passing fingerprint rule (a test that did not pass in Layer 1 holds
its pairs Inconclusive) came after expected.json was written. Layer 1 runs
every test twice in a row (repeats 1, design §5.2); the order-dependent
fixture's shared counter makes each test's second attempt fail, and Vitest
reports a test failed when any repetition fails. So rule 2, not-passing,
decides the pair before input-values-differ. The expectation follows from
the rule and the fixture's code; the CI controls run that reported the
difference is run 40278026899.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(scenario-map): skip Layer 1 for files no fingerprint finding can come from

Past the path rule, a pair is a duplicate, a possible duplicate or a table
row only when both checks-and-context keys are equal or one is unknown, and
the key is static (facet/checks.ts over the static inventory). Layer 1 now
runs only the files that hold a test whose key another test shares or that
has none, plus every file the static view cannot vouch for: a call whose
instance count is not static, a file snapshot read, a call at registration
level that is not the test API (a helper such as defineRenameFixtureTests or
describeGrammarPresence, or an it alias), or a parse failure. Mutation scope
all and the controls still run every file; the coverage jobs get the run's
--mutation so they slice the same files as the plan.

On the current checkout this skips 449 of 1,399 test files, about 24% of the
weighted Layer 1 cost. On the 250 unit files of the last full run, the static
keys reproduce all 3,313 runtime keys and select the same 172 files; runtime
collection (vitest list --no-static-parse) of the skipped files finds no test
the static inventory misses. Given up: different-checks pairs with a test of
a skipped file (0 on the last full unit run). The report says how many files
Layer 1 ran.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(scenario-map): diff-scope the labelled pull request run

A labelled pull request asks whether its own tests duplicate anything, yet
the run fingerprinted the whole suite (12 coverage slices, hours of runner
time). plan --changed-since <ref> now scopes it to the diff:

- the changed test files are the fingerprint's files the diff since the
  merge base of ref and HEAD added or modified;
- Layer 1 runs the changed files the prefilter keeps plus every file with
  a test whose checks-and-context key a changed test shares;
- candidate pairs are kept only when they have a test of a changed file,
  before the groups form, so the mutation target, the trace and the report
  follow;
- a diff that changes no test file writes an empty work file and an empty,
  complete report.

The workflow skips the coverage matrix for pull requests and passes the
merge commit's first parent (fetch-depth 2); the plan job builds dist/ and
runs Layer 1 itself. The report names the changed files. Locally, over the
two test files of the previous commit, the run took 4 min 41 s end to end
(Layer 1 over 2 files 1.3 min, trace 1.1 min) on a loaded shared host.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(scenario-map): size Layer 1 at 4 coverage slices, not 12

Twelve slices came from an estimate of three times the CI coverage time,
built on a local integration sample that other suites on the shared host
had slowed down. The test workflow runs the whole unit and integration
suite with coverage in three 7-8 minute shards (ci.yml run 30014110469),
and an instrumented Layer 1 run shows its overhead is small: an
integration file's work sits in beforeAll, which Layer 1 neither repeats
nor snapshots per test (resolvers/rust: 10.1 s of coverage snapshots
against about 190 s for the file). With each slice paying about five
minutes of setup, twelve slices spent more on setup than on tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): report the Layer 1 tests that got no fingerprint

Tests that Layer 1 saw but did not run dropped out of the candidates
without a word: skipped ones (skip, skipIf, todo; the GITNEXUS_BENCH
benchmark suites, which run only in the test workflow's benchmarks job)
and unfinished ones (pending, because their file's run ended first). The
report's eligibility section now counts both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(scenario-map): close gaps a code review found in the Layer 1 work

- The registration guard let a suite whose callback is not written
  inline (describe('x', defineCases)) through, so a file whose tests the
  inventory cannot see could be skipped. Such a suite now keeps its file.
- An empty coverage slice exited 2 and uploaded nothing; it now writes
  an empty slice file.
- --changed-since was accepted with the controls fixture, whose run must
  cover every file; that is now a usage error.
- The report counted the files of a coverage slice that did not report
  as run. It now says how many files wrote no Layer 1 record.
- A diff that only changes a snapshot file scoped nothing; a changed
  __snapshots__/<name>.snap now counts as a change of its test file.
- The diff-scope report claimed no other file can hold a changed test's
  duplicate. Files whose tests have no static key are not run, so it now
  says a possible duplicate there is not looked for.

Not changed: mutation shards never get integration tests, because
mutationTarget keeps only pairs of two mutation-eligible tests; a slice
that disagrees with the plan stops the merge on purpose (the slices
header now says so).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(scenario-map): cut setup cost and duplication in the Layer 1 work

A four-angle cleanup review (reuse, simplification, efficiency,
altitude) of the Layer 1 slices, prefilter and diff scope.

Fixed along the way: a shard's row-deletion batch ran every planned pair
of a complete group, including pairs whose tests the mutation target had
trimmed away (an integration test, or a pair outside the scope); it now
runs only pairs whose two tests the trimmed group holds (shardRowPlans).

Setup cost:
- Coverage jobs ran the Phase 0 classifier twice (about 33 s each) just
  to list the fingerprint's files; fingerprintScope lists them directly.
- The plan recomputed the prefilter (about 30 s) to rebuild the slice
  split. Slice files now carry Layer 1's file list, the mutation scope
  and the revision; the plan checks those against its own run.
- The prefilter re-parsed every test file. The unseen-registration check
  now lives in the static inventory (StaticInventory.incomplete), on the
  ASTs it holds and test-dupes' roleOf, which also catches
  test.describe with a named callback and it('x', namedFn).

Simplification: runCoverage writes the empty work file itself; reuse
partitionFiles, codeList, writeJson and path.matchesGlob (the hand-made
glob matcher read ** differently from Vitest); sameList moves to util;
copyBuildOutput owns the controls rule; slices are read before the work
directory is emptied, so --coverage no longer has to lie outside --work;
a dead sort is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(scenario-map): record the Layer 1 cleanup review in the fingerprint plan

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(scenario-map): full recall for diff-scoped pull request runs

A labelled pull request's run left out unchanged files whose tests have
no static checks-and-context key (helper-registered tests, dynamic it.each
counts, file snapshots, no assertion key), so a changed test's possible
duplicate in one was never looked for. Such a test can pair with any
changed test, so the diff scope now runs every one of those files too.

That floor is 230 of 1,407 test files, about 59% of the suite's weighted
Layer 1 cost with 59 sequential files: too much for the plan job alone.
A labelled pull request therefore runs the coverage slices as well:
coverage --changed-since records the changed files in each slice, and the
plan takes the diff scope from the slices (plan --changed-since stays for
a local all run). The plan job no longer builds dist/ or fetches history.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(scenario-map): temporary owner-only test run on PR #3686

TEMPORARY, remove before merge. The scenario-map-test label starts the
whole-suite run (no diff scope) with mutation possible-duplicates, so
every stage (coverage slices, plan, value flow, mutation shards, merge)
can be tested on CI before the workflow reaches the default branch, where
workflow_dispatch would serve. It runs only when magyargergo adds the
label on PR #3686; the label on any other pull request, or by anyone
else, leaves every job skipped. The scenario-map label keeps its
diff-scoped run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: stabilize scenario-map test selection

* fix: exclude retry tests from scenario traces

* fix: keep captured loop inputs unresolved

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:46:58 +00:00

1080 lines
42 KiB
TypeScript

/**
* Row-deletion operator of the scenario map (U9, R8, KTD10, design §5.4 "Row-deletion operator").
* Stryker never mutates object keys, so two tests that read two rows of one production table can
* kill the same mutants and still pin different rows. Deleting each row in turn tells them apart.
*
* Plan ({@link planRowDeletion}, pure). The pair's differing discarded values are the U6 facet
* entries at the same position with the same kind and another value. For each such position the
* two values (strings trimmed, numbers as numbers: the facet's table keys) must both be keys or
* entries of ONE supported production table:
* - the search goes tier by tier like the facet's (design §5.3 rule 3; property columns never
* count): the production files either test executed, then their two-hop import closure
* ({@link pairTableSearch}); the first tier in which U6's table index (tables.ts `factsOf`)
* has a table holding both values decides;
* - in that tier's files, the supported constructs whose key set equals such an index table:
* an object literal (plain identifier or string keys), `new Set([...])` (literal entries) or
* `new Map([...])` (`[key, value]` entries with literal keys), possibly under `as`,
* `satisfies`, `!` or `Object.freeze(...)`;
* - none: the position is not applicable (`no-common-table`: no table holds both values;
* `no-supported-table`: only an array, switch or regex table does); more than one:
* `ambiguous-table` (deleting from the wrong one would read as "neither fails");
* - the one table is unsupported (`unsupported-table: <construct>`) when it holds a spread, a
* computed key, a getter or setter, a duplicate key or a non-literal entry, or is wrapped in
* `new Proxy(...)` or passed to `Object.assign(...)`, or when the declaring module (only that
* module) writes into it through its binding: an assignment, `++`/`--` or `delete` on a
* member, a member in a destructuring or `for…in/of` target, a call of `add`, `set`,
* `delete` or `clear`, `Object.assign/defineProperty/defineProperties/setPrototypeOf` or
* `Reflect.set/defineProperty/deleteProperty/setPrototypeOf` on it, `new Proxy(<it>, …)`, or
* a reassignment of the binding. A table that is neither bound to a declaration nor read in
* place (`{…}[k]`) is `untracked`: its writes cannot be checked. A module that does not parse
* is `unparseable`. Not seen (accepted limits of the one-module scan): writes from another
* module, through an alias of the binding, or by a function the table is passed to.
* Each planned position deletes its two keys (deduplicated across positions): the entry's text
* and its trailing comma, if any, are spliced out of the source, never regenerated, and the result
* must parse (`splice-unparseable`). The global id is prepare/mutant-identity.ts `formatGlobalId`
* over the original file's repo path and content hash, the entry's span (1-based lines and
* columns, end exclusive), mutator {@link ROW_DELETION_MUTATOR} and an empty replacement.
*
* Runs ({@link runRowDeletion}). One throwaway copy per batch: `<work>/row-deletion/prepared`,
* prepared like a shard's copy (tools/mutation/prepare, nothing blanked) and verified against the
* run manifest (run-group.ts `verifyPreparedCopy`, else `manifest-mismatch` and nothing runs);
* each table file's copy must have the planned content hash (`copy-mismatch`). Per pair, through
* vitest-run.ts `runVitest` with the pair's two test ids, each call a fresh Vitest instance (a
* reused one would serve cached transforms and turn a deletion into a false "neither fails"):
* 1. a control run of the unedited copy: both tests pass (`control-failed`), and the run's Vite
* module graph shows every table file transformed (`probeFiles`/`loadedFiles`, else
* `edit-not-loaded`); either problem makes the pair inconclusive and runs no deletion;
* 2. one run per deletion, with the spliced text written into the copy and the original text
* restored after the run, whatever happens; a run that throws is inconclusive (`run-error`).
* Scratch: `<work>/row-deletion/pairs/<pair digest>/{control,deletion-<n>}/`.
*
* Outcomes. A deletion run ({@link classifyDeletionRun}) is inconclusive when the run is invalid,
* errored or timed out, has an outside-test row, a test without a pass or an assertion failure (a
* runtime error, a timeout, a hook: runner/classify-outcome.ts classes), or did not load the
* edited file; else it is asymmetric when exactly one test fails by assertion (Row) and symmetric
* when both or neither do. The pair ({@link combineRowDeletion}) is asymmetric if any deletion is,
* else inconclusive if any deletion or position is, else symmetric if any deletion ran, else not
* applicable. Design §2: symmetric and not applicable allow strong evidence, inconclusive caps it
* at weak, asymmetric is Row.
*
* Library only; the scenario-map CLI (U14) plans the pairs and calls {@link runRowDeletion}. The
* runner and the copy are injected ({@link RowDeletionDeps}), so unit tests never spawn Vitest.
*/
import { readFileSync, rmSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { parse } from '@babel/parser';
import traverse, { type NodePath } from '@babel/traverse';
import * as t from '@babel/types';
import {
contentHash,
formatGlobalId,
type MutantIdentity,
type ReportLocation,
} from '../../mutation/prepare/mutant-identity.js';
import { PACKAGE_DIR, buildPathMap, toPreparedPath } from '../../mutation/prepare/path-map.js';
import {
prepare,
type PrepareOptions,
type PrepareReport,
} from '../../mutation/prepare/prepare.js';
import type { OutcomeClass } from '../../mutation/runner/classify-outcome.js';
import { errorMessage } from '../../mutation/shared/cli.js';
import { overlaps } from '../../mutation/shared/fs.js';
import type {
DiscardedValue,
FacetContext,
FacetSubject,
InputFacet,
} from '../facet/input-facet.js';
import {
numberKey,
stringKey,
type LiteralKey,
type TableIndex,
type TableSearch,
} from '../facet/tables.js';
import { extraRootsOf } from '../types.js';
import { byText, sha256, sortedUnique } from '../util.js';
import { verifyPreparedCopy, type ScenarioRunManifest } from './run-group.js';
import {
runVitest,
vitestSelectionOf,
type VitestRunOptions,
type VitestRunResult,
} from './vitest-run.js';
/** The mutator name of a row deletion's global id. */
export const ROW_DELETION_MUTATOR = 'RowDeletion';
export type RowDeletionOutcome = 'symmetric' | 'asymmetric' | 'not-applicable' | 'inconclusive';
export type DeletionOutcome = 'symmetric' | 'asymmetric' | 'inconclusive';
/** A construct that makes a table unsupported (design §5.4); see the module comment. */
export type UnsupportedConstruct =
| 'spread'
| 'computed-key'
| 'getter'
| 'setter'
| 'duplicate-key'
| 'non-literal-entry'
| 'object-assign'
| 'proxy'
| 'write'
| 'untracked';
export type TableKind = 'object' | 'Set' | 'Map';
export interface RowTable {
/** Repo path of the declaring production file. */
file: string;
kind: TableKind;
/** The declared binding; null for a table read in place. */
binding: string | null;
/** 1-based line of the literal. */
line: number;
}
export interface PlannedDeletion {
globalId: string;
identity: MutantIdentity;
/** The deleted key or entry (tables.ts `LiteralKey`). */
key: LiteralKey;
table: RowTable;
/** Offsets in the original text, end exclusive: the entry and its trailing comma. */
splice: { start: number; end: number };
}
export interface PositionPlan {
/** Facet slot index (U6 `DiscardedValue.position`). */
position: number;
/** The two tests' values at the position. */
values: [string, string];
status: 'planned' | 'not-applicable' | 'inconclusive';
/** Why the position is not planned; empty when it is. */
reasons: string[];
table: RowTable | null;
}
export interface RowDeletionPlan {
/** Phase 0 ids of the pair's tests. */
a: string;
b: string;
/** `planned` when at least one deletion is planned. */
status: 'planned' | 'not-applicable' | 'inconclusive';
/** Reasons of the unplanned positions, sorted and distinct. */
reasons: string[];
positions: PositionPlan[];
/** Distinct global ids, in position order. */
deletions: PlannedDeletion[];
}
export interface RowDeletionPairInput {
a: string;
b: string;
/** The U6 facets of `a` and `b`. */
facets: readonly [InputFacet, InputFacet];
/** Where the table is searched ({@link pairTableSearch}). */
search: TableSearch;
}
export interface RowDeletionEnv {
/** The run's table index (FacetContext `tables`). */
tables: TableIndex;
/** Text of a repo path, null when it cannot be read; the index's sources. */
read: (repoPath: string) => string | null;
}
const PLUGINS: ['typescript', 'decorators-legacy'] = ['typescript', 'decorators-legacy'];
/** Nodes between a table literal and its use that keep its value. */
const WRAPPERS: ReadonlySet<string> = new Set([
'TSAsExpression',
'TSSatisfiesExpression',
'TSTypeAssertion',
'TSNonNullExpression',
'ParenthesizedExpression',
]);
const MUTATING_METHODS: ReadonlySet<string> = new Set(['add', 'set', 'delete', 'clear']);
const WRITING_CALLS: ReadonlySet<string> = new Set([
'Object.defineProperty',
'Object.defineProperties',
'Object.setPrototypeOf',
'Reflect.set',
'Reflect.defineProperty',
'Reflect.deleteProperty',
'Reflect.setPrototypeOf',
]);
// ── Table search ───────────────────────────────────────────────────────────
/**
* The tiers a pair's table is searched in: the production files either test executed (Layer 1
* provenance), then the production files within two import hops of either test file (the facet's
* second tier). Both sorted and distinct.
*/
export function pairTableSearch(ctx: FacetContext, subjects: readonly FacetSubject[]): TableSearch {
const executed = subjects.flatMap((subject) =>
(subject.executedFiles ?? []).filter((file) => ctx.isProduction(file)),
);
const closure = subjects.flatMap((subject) => ctx.closureOf(subject)?.near ?? []);
return { executed: sortedUnique(executed), closure: sortedUnique(closure) };
}
/** A discarded value as the table index holds it; null for values no table holds. */
function tableKeyOf(entry: DiscardedValue): LiteralKey | null {
if (entry.kind === 'string') return stringKey(entry.value.trim());
if (entry.kind !== 'number') return null;
const value = Number(entry.value);
return Number.isFinite(value) ? numberKey(value) : null;
}
// ── Table constructs of a module ───────────────────────────────────────────
interface ScannedTable extends RowTable {
values: ReadonlySet<LiteralKey>;
/** The node of each key's first entry. */
entries: ReadonlyMap<LiteralKey, t.Node>;
unsupported: ReadonlySet<UnsupportedConstruct>;
}
/** A string literal or a plain template's text, else null (tables.ts `stringOf`). */
function stringOf(node: t.Node | null | undefined): string | null {
if (node === null || node === undefined) return null;
if (t.isStringLiteral(node)) return node.value;
if (t.isTemplateLiteral(node) && node.expressions.length === 0) {
return node.quasis[0]?.value.cooked ?? null;
}
return null;
}
/** A Set entry or Map key as the table index holds it (tables.ts `entryKey`), else null. */
function entryKeyOf(node: t.Node | null | undefined): LiteralKey | null {
if (node === null || node === undefined) return null;
if (t.isNumericLiteral(node)) return numberKey(node.value);
if (t.isUnaryExpression(node, { operator: '-' }) && t.isNumericLiteral(node.argument)) {
return numberKey(-node.argument.value);
}
const text = stringOf(node);
return text !== null && text.length > 0 ? stringKey(text) : null;
}
/** An object property's key as the table index holds it (tables.ts `objectKeyOf`), else null. */
function objectKeyOf(property: t.ObjectProperty | t.ObjectMethod): LiteralKey | null {
const text = property.computed
? stringOf(property.key)
: t.isIdentifier(property.key)
? property.key.name
: t.isStringLiteral(property.key)
? property.key.value
: null;
return text !== null && text.length > 0 ? stringKey(text) : null;
}
/** `Object.assign` for `Object.assign(...)`, `Proxy` for `new Proxy(...)`: the callee's name. */
function calleeName(callee: t.Node): string | null {
if (t.isIdentifier(callee)) return callee.name;
if (t.isMemberExpression(callee) && !callee.computed && t.isIdentifier(callee.object)) {
return t.isIdentifier(callee.property) ? `${callee.object.name}.${callee.property.name}` : null;
}
return null;
}
function memberName(member: t.MemberExpression | t.OptionalMemberExpression): string | null {
if (!member.computed && t.isIdentifier(member.property)) return member.property.name;
return stringOf(member.property);
}
/** The outermost path above `path` that only wraps its value. */
function unwrap(path: NodePath): NodePath {
let current = path;
for (;;) {
const parent = current.parentPath;
if (parent === null) return current;
const frozen =
t.isCallExpression(parent.node) &&
calleeName(parent.node.callee) === 'Object.freeze' &&
parent.node.arguments.length === 1 &&
parent.node.arguments[0] === current.node;
if (!WRAPPERS.has(parent.node.type) && !frozen) return current;
current = parent;
}
}
/** True when a member expression is the target of a destructuring or `for…in/of` assignment. */
function isPatternTarget(member: NodePath): boolean {
const parent = member.parentPath;
if (parent === null) return false;
const node = parent.node;
if (t.isArrayPattern(node) || t.isRestElement(node)) return true;
if (t.isAssignmentPattern(node)) return node.left === member.node;
if (t.isObjectProperty(node)) {
return t.isObjectPattern(parent.parent) && node.value === member.node;
}
if (t.isForOfStatement(node) || t.isForInStatement(node)) return node.left === member.node;
return false;
}
/** What one reference to the table's binding does to it; empty for a read. */
function referenceUse(reference: NodePath): UnsupportedConstruct[] {
const use = unwrap(reference);
const parent = use.parentPath;
if (parent === null) return [];
const node = parent.node;
if (
(t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) &&
node.object === use.node
) {
const outer = parent.parent;
if (t.isAssignmentExpression(outer) && outer.left === node) return ['write'];
if (t.isUpdateExpression(outer)) return ['write'];
if (t.isUnaryExpression(outer, { operator: 'delete' })) return ['write'];
const called =
(t.isCallExpression(outer) || t.isOptionalCallExpression(outer)) && outer.callee === node;
if (called && MUTATING_METHODS.has(memberName(node) ?? '')) return ['write'];
return isPatternTarget(parent) ? ['write'] : [];
}
if ((t.isCallExpression(node) || t.isNewExpression(node)) && node.arguments[0] === use.node) {
const callee = calleeName(node.callee) ?? '';
if (t.isNewExpression(node)) return callee === 'Proxy' ? ['proxy'] : [];
if (callee === 'Object.assign') return ['object-assign'];
if (WRITING_CALLS.has(callee)) return ['write'];
}
return [];
}
/** How a table literal is held: its binding and what the module does to it. */
function usageOf(literal: NodePath): {
binding: string | null;
unsupported: UnsupportedConstruct[];
} {
const root = unwrap(literal);
const parent = root.parentPath;
const node = parent?.node;
if (
parent &&
t.isVariableDeclarator(node) &&
node.init === root.node &&
t.isIdentifier(node.id)
) {
const name = node.id.name;
const binding = parent.scope.getBinding(name);
if (binding === undefined) return { binding: name, unsupported: ['untracked'] };
const unsupported: UnsupportedConstruct[] =
binding.constantViolations.length > 0 ? ['write'] : [];
for (const reference of binding.referencePaths) unsupported.push(...referenceUse(reference));
return { binding: name, unsupported };
}
if (
(t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) &&
node.object === root.node
) {
return { binding: null, unsupported: [] };
}
if (t.isNewExpression(node) && calleeName(node.callee) === 'Proxy') {
return { binding: null, unsupported: ['proxy'] };
}
if (t.isCallExpression(node) && calleeName(node.callee) === 'Object.assign') {
return { binding: null, unsupported: ['object-assign'] };
}
return { binding: null, unsupported: ['untracked'] };
}
interface EntryScan {
entries: Map<LiteralKey, t.Node>;
unsupported: UnsupportedConstruct[];
}
function addEntry(scan: EntryScan, key: LiteralKey, node: t.Node): void {
if (scan.entries.has(key)) scan.unsupported.push('duplicate-key');
else scan.entries.set(key, node);
}
function objectEntries(node: t.ObjectExpression): EntryScan {
const scan: EntryScan = { entries: new Map(), unsupported: [] };
for (const property of node.properties) {
if (t.isSpreadElement(property)) {
scan.unsupported.push('spread');
continue;
}
if (property.computed) scan.unsupported.push('computed-key');
if (t.isObjectMethod(property) && property.kind === 'get') scan.unsupported.push('getter');
if (t.isObjectMethod(property) && property.kind === 'set') scan.unsupported.push('setter');
const key = objectKeyOf(property);
if (key !== null) addEntry(scan, key, property);
}
return scan;
}
function collectionEntries(kind: 'Set' | 'Map', array: t.ArrayExpression): EntryScan {
const scan: EntryScan = { entries: new Map(), unsupported: [] };
for (const element of array.elements) {
const keyNode = kind === 'Set' || !t.isArrayExpression(element) ? element : element.elements[0];
if (t.isSpreadElement(element) || t.isSpreadElement(keyNode)) {
scan.unsupported.push('spread');
continue;
}
const key = entryKeyOf(keyNode);
if (key === null || element === null) scan.unsupported.push('non-literal-entry');
else addEntry(scan, key, element);
}
return scan;
}
const parseModule = (source: string): t.File =>
parse(source, { sourceType: 'module', plugins: PLUGINS });
/** Every object literal, `new Set([...])` and `new Map([...])` of a module; null when it does not parse. */
function scanTables(file: string, source: string): ScannedTable[] | null {
let ast: t.File;
try {
ast = parseModule(source);
} catch {
return null;
}
return tablesIn(file, ast);
}
/** {@link scanTables} over a parsed module; null when the traversal throws. */
function tablesIn(file: string, ast: t.File): ScannedTable[] | null {
const tables: ScannedTable[] = [];
const record = (kind: TableKind, literal: NodePath, scan: EntryScan): void => {
if (scan.entries.size < 2) return;
const usage = usageOf(literal);
tables.push({
file,
kind,
binding: usage.binding,
line: literal.node.loc?.start.line ?? 0,
values: new Set(scan.entries.keys()),
entries: scan.entries,
unsupported: new Set([...scan.unsupported, ...usage.unsupported]),
});
};
try {
traverse(ast, {
ObjectExpression(path) {
record('object', path, objectEntries(path.node));
},
NewExpression(path) {
const { callee, arguments: args } = path.node;
const array = args[0];
if (!t.isIdentifier(callee) || !t.isArrayExpression(array)) return;
if (callee.name === 'Set' || callee.name === 'Map') {
record(callee.name, path, collectionEntries(callee.name, array));
}
},
});
} catch {
return null;
}
return tables;
}
/**
* The module-level table bindings of a module (a top-level `const` holding an object literal,
* `new Set([...])` or `new Map([...])`), each with the constructs that make row deletion refuse
* it; an empty list is a table row deletion supports. The value-flow check (U10, value-flow/
* sinks.ts) classifies a key read of exactly these tables as a supported table read. A name bound
* to more than one table literal is `ambiguous`. Null when the module does not parse.
*/
export function scanSupportedTables(
file: string,
source: string,
): Map<string, (UnsupportedConstruct | 'ambiguous')[]> | null {
let ast: t.File;
try {
ast = parseModule(source);
} catch {
return null;
}
return scanSupportedTablesIn(file, ast);
}
/**
* {@link scanSupportedTables} over a module already parsed with the same options (`module`,
* `typescript` and `decorators-legacy`), so a caller that parsed it does not parse it again.
*/
export function scanSupportedTablesIn(
file: string,
ast: t.File,
): Map<string, (UnsupportedConstruct | 'ambiguous')[]> | null {
const tables = tablesIn(file, ast);
if (tables === null) return null;
const topLevel = new Set(
ast.program.body.flatMap((statement) => {
const declaration =
t.isExportNamedDeclaration(statement) && statement.declaration !== null
? statement.declaration
: statement;
if (!t.isVariableDeclaration(declaration) || declaration.kind !== 'const') return [];
return declaration.declarations.flatMap((declarator) =>
t.isIdentifier(declarator.id) ? [declarator.id.name] : [],
);
}),
);
const out = new Map<string, (UnsupportedConstruct | 'ambiguous')[]>();
for (const table of tables) {
if (table.binding === null || !topLevel.has(table.binding)) continue;
const constructs = [...table.unsupported].sort(byText);
out.set(table.binding, out.has(table.binding) ? ['ambiguous'] : constructs);
}
return out;
}
// ── Plan ───────────────────────────────────────────────────────────────────
/** The end of an entry's splice: after its trailing comma, past whitespace and comments. */
function spliceEnd(source: string, end: number): number {
let index = end;
while (index < source.length) {
if (/\s/.test(source[index])) index += 1;
else if (source.startsWith('//', index)) {
const newline = source.indexOf('\n', index);
index = newline === -1 ? source.length : newline;
} else if (source.startsWith('/*', index)) {
const close = source.indexOf('*/', index + 2);
if (close === -1) return end;
index = close + 2;
} else return source[index] === ',' ? index + 1 : end;
}
return end;
}
const parses = (source: string): boolean => {
try {
parseModule(source);
return true;
} catch {
return false;
}
};
/** A Babel location (1-based lines, 0-based columns) as a report span (1-based, end exclusive). */
const reportLocation = (loc: t.SourceLocation): ReportLocation => ({
start: { line: loc.start.line, column: loc.start.column + 1 },
end: { line: loc.end.line, column: loc.end.column + 1 },
});
type TableChoice =
| { kind: 'none'; reason: 'no-common-table' | 'no-supported-table' }
| { kind: 'problem'; reasons: string[] }
| { kind: 'table'; table: ScannedTable };
const sameSet = (a: ReadonlySet<LiteralKey>, b: ReadonlySet<LiteralKey>): boolean =>
a.size === b.size && [...a].every((value) => b.has(value));
/** What planning reads more than once across the pairs of one env. */
interface PlannerCache {
sources: Map<string, string | null>;
scans: Map<string, ScannedTable[] | null>;
/** `contentHash` of each file's planning text. */
fileHashes: Map<string, string>;
/** Whether a file's planning text parses without a splice, by `<file>\0<start>\0<end>`. */
splices: Map<string, boolean>;
}
/**
* One cache per env: a run plans every pair over one env, so each file is read, scanned and
* hashed once and each splice checked once. `env.read` returns the same text for a file
* throughout (the run's checkout).
*/
const plannerCaches = new WeakMap<RowDeletionEnv, PlannerCache>();
class Planner {
private readonly cache: PlannerCache;
constructor(private readonly env: RowDeletionEnv) {
let cache = plannerCaches.get(env);
if (cache === undefined) {
cache = { sources: new Map(), scans: new Map(), fileHashes: new Map(), splices: new Map() };
plannerCaches.set(env, cache);
}
this.cache = cache;
}
source(file: string): string | null {
const { sources } = this.cache;
if (!sources.has(file)) sources.set(file, this.env.read(file));
return sources.get(file) ?? null;
}
/** The text a file's deletions are planned on: its source, empty when it cannot be read. */
text(file: string): string {
return this.source(file) ?? '';
}
fileHash(file: string): string {
const { fileHashes } = this.cache;
let hash = fileHashes.get(file);
if (hash === undefined) {
hash = contentHash(this.text(file));
fileHashes.set(file, hash);
}
return hash;
}
/** Whether the file's text still parses with `[start, end)` cut out. */
spliceParses(file: string, start: number, end: number): boolean {
const { splices } = this.cache;
const key = `${file}\0${start}\0${end}`;
let parsesCut = splices.get(key);
if (parsesCut === undefined) {
const text = this.text(file);
parsesCut = parses(text.slice(0, start) + text.slice(end));
splices.set(key, parsesCut);
}
return parsesCut;
}
private scan(file: string): ScannedTable[] | null {
const { scans } = this.cache;
if (!scans.has(file)) {
const source = this.source(file);
scans.set(file, source === null ? null : scanTables(file, source));
}
return scans.get(file) ?? null;
}
/** The one supported table holding both keys; see the module comment. */
choose(search: TableSearch, a: LiteralKey, b: LiteralKey): TableChoice {
for (const tier of [search.executed, search.closure]) {
const matching = sortedUnique(tier).flatMap((file) => {
const tables = this.env.tables
.factsOf(file)
.tables.filter((table) => table.values.has(a) && table.values.has(b));
return tables.length === 0 ? [] : [{ file, tables }];
});
if (matching.length === 0) continue;
const unparseable = matching.filter(({ file }) => this.scan(file) === null);
if (unparseable.length > 0) return { kind: 'problem', reasons: ['unparseable'] };
const candidates = matching.flatMap(({ file, tables }) =>
(this.scan(file) ?? []).filter(
(table) =>
table.values.has(a) &&
table.values.has(b) &&
tables.some((indexed) => sameSet(indexed.values, table.values)),
),
);
if (candidates.length === 0) return { kind: 'none', reason: 'no-supported-table' };
if (candidates.length > 1) return { kind: 'problem', reasons: ['ambiguous-table'] };
const [table] = candidates;
if (table.unsupported.size > 0) {
return {
kind: 'problem',
reasons: sortedUnique([...table.unsupported].map((c) => `unsupported-table: ${c}`)),
};
}
return { kind: 'table', table };
}
return { kind: 'none', reason: 'no-common-table' };
}
}
const rowTableOf = (table: ScannedTable): RowTable => ({
file: table.file,
kind: table.kind,
binding: table.binding,
line: table.line,
});
/** The deletion of one key of a chosen table; null when its splice does not parse. */
function deletionOf(
planner: Planner,
table: ScannedTable,
key: LiteralKey,
): PlannedDeletion | null {
const node = table.entries.get(key);
if (node === undefined || typeof node.start !== 'number' || typeof node.end !== 'number') {
return null;
}
if (node.loc === null || node.loc === undefined) return null;
const splice = { start: node.start, end: spliceEnd(planner.text(table.file), node.end) };
if (!planner.spliceParses(table.file, splice.start, splice.end)) return null;
const identity: MutantIdentity = {
file: table.file,
fileHash: planner.fileHash(table.file),
location: reportLocation(node.loc),
mutatorName: ROW_DELETION_MUTATOR,
replacement: '',
};
return { globalId: formatGlobalId(identity), identity, key, table: rowTableOf(table), splice };
}
/** Plans the row deletions of one pair; see the module comment. Pure over `env`. */
export function planRowDeletion(input: RowDeletionPairInput, env: RowDeletionEnv): RowDeletionPlan {
const planner = new Planner(env);
const [facetA, facetB] = input.facets;
const others = new Map(facetB.discarded.map((entry) => [entry.position, entry]));
const positions: PositionPlan[] = [];
const deletions = new Map<string, PlannedDeletion>();
for (const entry of facetA.discarded) {
const other = others.get(entry.position);
if (other === undefined || other.kind !== entry.kind || other.value === entry.value) continue;
const base = {
position: entry.position,
values: [entry.value, other.value] as [string, string],
};
const [keyA, keyB] = [tableKeyOf(entry), tableKeyOf(other)];
const choice: TableChoice =
keyA === null || keyB === null || keyA === keyB
? { kind: 'none', reason: 'no-common-table' }
: planner.choose(input.search, keyA, keyB);
if (choice.kind === 'none') {
positions.push({ ...base, status: 'not-applicable', reasons: [choice.reason], table: null });
continue;
}
if (choice.kind === 'problem') {
positions.push({ ...base, status: 'inconclusive', reasons: choice.reasons, table: null });
continue;
}
const { table } = choice;
const planned = [keyA, keyB].map((key) =>
key === null ? null : deletionOf(planner, table, key),
);
if (planned.some((deletion) => deletion === null)) {
positions.push({
...base,
status: 'inconclusive',
reasons: ['splice-unparseable'],
table: rowTableOf(table),
});
continue;
}
for (const deletion of planned)
if (deletion !== null) deletions.set(deletion.globalId, deletion);
positions.push({ ...base, status: 'planned', reasons: [], table: rowTableOf(table) });
}
const reasons = sortedUnique(positions.flatMap((position) => position.reasons));
const status =
deletions.size > 0
? 'planned'
: positions.some((position) => position.status === 'inconclusive')
? 'inconclusive'
: 'not-applicable';
return {
a: input.a,
b: input.b,
status,
reasons: positions.length === 0 ? ['no-differing-values'] : reasons,
positions,
deletions: [...deletions.values()],
};
}
// ── Classification ─────────────────────────────────────────────────────────
const USABLE: ReadonlySet<OutcomeClass> = new Set(['survived', 'killed-by-test:assertion']);
const allLoaded = (run: VitestRunResult, probeFiles: readonly string[]): boolean =>
run.loadedFiles !== undefined && probeFiles.every((file) => run.loadedFiles?.includes(file));
/** The control run's problem: `control-failed`, `edit-not-loaded`, or null when it is usable. */
export function classifyControlRun(
run: VitestRunResult,
tests: readonly [string, string],
probeFiles: readonly string[],
): 'control-failed' | 'edit-not-loaded' | null {
const passed = tests.every(
(id) => run.tests.find((test) => test.id === id)?.outcome === 'survived',
);
const clean =
run.problems.length === 0 &&
run.unhandledErrors.length === 0 &&
run.status === 'survived' &&
run.outsideTest.length === 0;
if (!passed || !clean) return 'control-failed';
return allLoaded(run, probeFiles) ? null : 'edit-not-loaded';
}
/** One deletion run's outcome; `reason` names why it is inconclusive. */
export function classifyDeletionRun(
run: VitestRunResult,
tests: readonly [string, string],
probeFiles: readonly string[],
): { outcome: DeletionOutcome; reason: string | null } {
const inconclusive = (reason: string) => ({ outcome: 'inconclusive' as const, reason });
if (run.problems.length > 0 || run.status === 'error' || run.status === 'timeout') {
return inconclusive(run.outcome);
}
const outside = run.outsideTest[0];
if (outside !== undefined) return inconclusive(outside.outcome);
const outcomes = tests.map((id) => run.tests.find((test) => test.id === id)?.outcome ?? null);
const unusable = outcomes.find((outcome) => outcome === null || !USABLE.has(outcome));
if (unusable !== undefined) return inconclusive(unusable ?? 'missing');
if (!allLoaded(run, probeFiles)) return inconclusive('edit-not-loaded');
const failing = outcomes.filter((outcome) => outcome === 'killed-by-test:assertion').length;
return { outcome: failing === 1 ? 'asymmetric' : 'symmetric', reason: null };
}
/** The pair outcome over its deletions and unplanned positions; see the module comment. */
export function combineRowDeletion(outcomes: readonly RowDeletionOutcome[]): RowDeletionOutcome {
if (outcomes.includes('asymmetric')) return 'asymmetric';
if (outcomes.includes('inconclusive')) return 'inconclusive';
if (outcomes.includes('symmetric')) return 'symmetric';
return 'not-applicable';
}
// ── Runs ───────────────────────────────────────────────────────────────────
export interface RunSummary {
outcome: OutcomeClass;
/** Each test's outcome class, in pair order; null when it has no row. */
tests: { id: string; outcome: OutcomeClass | null; message: string | null }[];
loadedFiles: string[] | null;
problems: string[];
wallClockMs: number;
}
export interface DeletionResult {
globalId: string;
key: LiteralKey;
table: RowTable;
outcome: DeletionOutcome;
/** Why the deletion is inconclusive; null otherwise. */
reason: string | null;
run: RunSummary | null;
}
export interface RowDeletionResult {
a: string;
b: string;
outcome: RowDeletionOutcome;
/** Why the pair is inconclusive or not applicable, sorted and distinct. */
reasons: string[];
/** Error text of a run that threw or of a mismatching copy; null otherwise. */
detail: string | null;
positions: PositionPlan[];
control: RunSummary | null;
deletions: DeletionResult[];
wallClockMs: number;
}
export interface RowDeletionDeps {
runVitest: (options: VitestRunOptions) => Promise<VitestRunResult>;
prepare: (options: PrepareOptions) => PrepareReport;
/** Milliseconds on a monotonic clock. */
now: () => number;
}
export const defaultRowDeletionDeps: RowDeletionDeps = {
runVitest,
prepare,
now: () => performance.now(),
};
export interface RowDeletionRunOptions {
/** Absolute repo checkout. */
repoRoot: string;
/** Absolute work directory, outside the checkout. */
workDir: string;
/** The run manifest the copy is verified against. */
manifest: ScenarioRunManifest;
plans: readonly RowDeletionPlan[];
/** Upper bound of each Vitest run's worker processes (the CLI's `--concurrency`). */
maxWorkers?: number;
}
const summarize = (run: VitestRunResult, tests: readonly string[]): RunSummary => ({
outcome: run.outcome,
tests: tests.map((id) => {
const test = run.tests.find((entry) => entry.id === id);
return { id, outcome: test?.outcome ?? null, message: test?.message ?? null };
}),
loadedFiles: run.loadedFiles ?? null,
problems: [...run.problems, ...run.unhandledErrors],
wallClockMs: run.wallClockMs,
});
/** `<work>/row-deletion/pairs/<16 hex digits of sha256 over the two ids>`. */
const pairDir = (workDir: string, plan: RowDeletionPlan): string =>
path.join(workDir, 'row-deletion', 'pairs', sha256(`${plan.a}\n${plan.b}`).slice(0, 16));
interface Copy {
preparedRoot: string;
packageDir: string;
/** Prepared-root path of each repo path. */
preparedPathOf: (repoPath: string) => string;
/** The manifest check's mismatches; empty when the copy is verified. */
mismatches: string[];
}
interface Finish {
reasons: string[];
detail: string | null;
control: RunSummary | null;
deletions: DeletionResult[];
}
function resultOf(plan: RowDeletionPlan, finish: Finish, wallClockMs: number): RowDeletionResult {
const outcomes: RowDeletionOutcome[] = [
...plan.positions.map(
(position): RowDeletionOutcome =>
position.status === 'planned' ? 'not-applicable' : position.status,
),
...finish.deletions.map((deletion) => deletion.outcome),
];
const outcome = plan.positions.length === 0 ? 'not-applicable' : combineRowDeletion(outcomes);
const reasons = sortedUnique([
...plan.reasons,
...finish.reasons,
...finish.deletions.flatMap((deletion) =>
deletion.reason === null || finish.reasons.includes(deletion.reason)
? []
: [`deletion: ${deletion.reason}`],
),
]);
return {
a: plan.a,
b: plan.b,
outcome,
reasons: outcome === 'asymmetric' ? [] : reasons,
detail: finish.detail,
positions: plan.positions,
control: finish.control,
deletions: finish.deletions,
wallClockMs,
};
}
/** Every planned deletion inconclusive for one reason, none run. */
const skipped = (
plan: RowDeletionPlan,
reason: string,
detail: string | null,
control: RunSummary | null = null,
): Finish => ({
reasons: [reason],
detail,
control,
deletions: plan.deletions.map((deletion) => ({
globalId: deletion.globalId,
key: deletion.key,
table: deletion.table,
outcome: 'inconclusive',
reason,
run: null,
})),
});
/** The control run and one run per deletion of one planned pair, in the verified copy. */
async function runPlan(
plan: RowDeletionPlan,
copy: Copy,
options: Pick<RowDeletionRunOptions, 'manifest' | 'workDir' | 'maxWorkers'>,
deps: RowDeletionDeps,
): Promise<Finish> {
const { manifest, workDir, maxWorkers } = options;
if (copy.mismatches.length > 0) {
return skipped(plan, 'manifest-mismatch', copy.mismatches.join('; '));
}
const tests: [string, string] = [plan.a, plan.b];
const listed = new Set(manifest.tests.map((test) => test.id));
const unlisted = tests.filter((test) => !listed.has(test));
if (unlisted.length > 0) {
return skipped(plan, 'manifest-mismatch', `not in the run manifest: ${unlisted.join(', ')}`);
}
const files = new Map<string, { absolute: string; packagePath: string; original: string }>();
for (const deletion of plan.deletions) {
const file = deletion.identity.file;
if (files.has(file)) continue;
const prepared = copy.preparedPathOf(file);
const absolute = path.join(copy.preparedRoot, prepared);
let original: string;
try {
original = readFileSync(absolute, 'utf8');
} catch (error) {
return skipped(plan, 'copy-mismatch', `${prepared}: ${errorMessage(error)}`);
}
if (contentHash(original) !== deletion.identity.fileHash) {
return skipped(plan, 'copy-mismatch', `${prepared} differs from the planned ${file}`);
}
files.set(file, { absolute, packagePath: prepared.slice(PACKAGE_DIR.length + 1), original });
}
const dir = pairDir(workDir, plan);
// Scratch of an earlier run (a Vite cache among it) would leak into this one.
rmSync(dir, { recursive: true, force: true });
const vitest = vitestSelectionOf(manifest.mode);
const run = (name: string, probeFiles: string[]): Promise<VitestRunResult> =>
deps.runVitest({
packageDir: copy.packageDir,
testIds: tests,
vitest,
workDir: path.join(dir, name),
probeFiles,
maxWorkers,
});
const probeFiles = [...files.values()].map((file) => file.packagePath);
let control: VitestRunResult;
try {
control = await run('control', probeFiles);
} catch (error) {
return skipped(plan, 'control-failed', errorMessage(error));
}
const problem = classifyControlRun(control, tests, probeFiles);
if (problem !== null) {
return skipped(plan, problem, control.reason, summarize(control, tests));
}
const deletions: DeletionResult[] = [];
let detail: string | null = null;
for (const [index, deletion] of plan.deletions.entries()) {
const file = files.get(deletion.identity.file);
if (file === undefined) throw new Error(`row deletion: ${deletion.identity.file} not read`);
const { start, end } = deletion.splice;
const base = { globalId: deletion.globalId, key: deletion.key, table: deletion.table };
writeFileSync(file.absolute, file.original.slice(0, start) + file.original.slice(end));
try {
const result = await run(`deletion-${index + 1}`, [file.packagePath]);
deletions.push({
...base,
...classifyDeletionRun(result, tests, [file.packagePath]),
run: summarize(result, tests),
});
} catch (error) {
deletions.push({ ...base, outcome: 'inconclusive', reason: 'run-error', run: null });
detail ??= errorMessage(error);
} finally {
writeFileSync(file.absolute, file.original);
}
}
return { reasons: [], detail, control: summarize(control, tests), deletions };
}
/**
* Runs the planned row deletions of a batch of pairs in one throwaway copy; see the module
* comment. Plans without deletions come back as they are, and no copy is made for them.
*/
export async function runRowDeletion(
options: RowDeletionRunOptions,
deps: RowDeletionDeps = defaultRowDeletionDeps,
): Promise<RowDeletionResult[]> {
const { repoRoot, workDir, manifest, plans } = options;
if (overlaps(repoRoot, workDir)) {
throw new Error(`row deletion: the work directory ${workDir} must lie outside ${repoRoot}`);
}
let copy: Copy | null = null;
const results: RowDeletionResult[] = [];
for (const plan of plans) {
const started = deps.now();
if (plan.deletions.length === 0) {
results.push(resultOf(plan, { reasons: [], detail: null, control: null, deletions: [] }, 0));
continue;
}
if (copy === null) {
const preparedRoot = path.join(workDir, 'row-deletion', 'prepared');
const pathMap = buildPathMap(extraRootsOf(manifest.mode));
deps.prepare({ repoRoot, outDir: preparedRoot, mutate: [], pathMap });
copy = {
preparedRoot,
packageDir: path.join(preparedRoot, PACKAGE_DIR),
preparedPathOf: (repoPath) => toPreparedPath(repoPath, pathMap),
mismatches: verifyPreparedCopy(manifest, preparedRoot).mismatches,
};
}
const finish = await runPlan(plan, copy, options, deps);
results.push(resultOf(plan, finish, Math.round(deps.now() - started)));
}
return results;
}