Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/gap-diff-base-limits-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@tanstack/oracles': minor
---

`oracle-gaps --diff-base <rev>` now judges only the mutants on lines changed since `<rev>`, for fast feedback after a change. Operator and authored mutants both count, and a mutant is on a changed line when any line from its first to its last is changed. Every line of a source file that git does not track is changed. The baseline still runs every test file that the run without the option runs, and each judged mutant gets the outcome it gets in the full run. An authored mutant that does not apply is reported on every run with the option, as it is without it. The run says so on its stream before the baseline, `oracle-gaps: note --diff-base <rev>: <n> of <m> mutants are on changed lines; the others are not judged in this run`, and in the same sentence in `summary.md`; `<m>` counts the run without the option, with the same `--only` or `--sample`. Its `results.json` and `summary.md` inputs say `a partial run (--only, --sample, or --diff-base)`, as an `--only` or `--sample` run's do (their text used to read `(--only or --sample)`). Authored mutants are marked `inDiff` too.

`oracle-gaps --diff-base <rev>` refuses a revision that names no commit of the git repository that holds the package: a name git does not know, a range, a blob, an option such as `--cached`, an empty value, or a package in no repository. It exits 2 with `refused: --diff-base "<rev>" names no commit of the git repository that holds the package` and a `fix:` line, before any test runs. Before, the run went on and marked every mutant `inDiff: false`. `results.json` records the commit as `options.diffBase: { rev, commit }` (it was the revision as given), so the run identity follows the commit and not the name.

Before, `--diff-base` only added `inDiff` to each operator mutant in `results.json`, and every mutant still ran. Run a full check without `--diff-base` to judge the other lines.
9 changes: 7 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,16 @@ jobs:
uses: tanstack/config/.github/setup@e4b48f16568324f76f467aa4c2aac2f05db632c3
- run: pnpm --filter @tanstack/oracles run build
# The same partition of the static oracle that Linux runs, by pattern.
# Vitest runs through node, not `pnpm exec`: on Windows the latter goes
# through cmd.exe, whose command line holds 8191 characters, and a
# shard's pattern of test names passed that with #290's tests ("The
# command line is too long").
- name: Gap report static oracle
shell: bash
working-directory: packages/oracles
env:
SHARD: ${{ matrix.shard }}
run: pnpm exec vitest --run --config oracle-gaps/vitest.config.mjs gap-report-static -t "$(node scripts/pr-shards.mjs pattern oracle-gaps-static-windows "$SHARD")"
run: node node_modules/vitest/vitest.mjs --run --config oracle-gaps/vitest.config.mjs gap-report-static -t "$(node scripts/pr-shards.mjs pattern oracle-gaps-static-windows "$SHARD")"
windows-gap-classify:
name: Windows gap report classify, probe-form, and mutant-validity oracles
# Gated: runs when gaps changed (scripts/classify-changes.sh).
Expand Down Expand Up @@ -426,7 +430,8 @@ jobs:
env:
SHARD: ${{ matrix.shard }}
# The verbose reporter prints each test with its duration, which sizes the shards.
run: pnpm exec vitest --run --reporter=verbose --config oracle-gaps/vitest.config.mjs gap-report-oracle -t "$(node scripts/pr-shards.mjs pattern oracle-gaps-fixed-seed-windows "$SHARD")"
# Through node, not `pnpm exec`, for cmd.exe's command-line length (see the static job).
run: node node_modules/vitest/vitest.mjs --run --reporter=verbose --config oracle-gaps/vitest.config.mjs gap-report-oracle -t "$(node scripts/pr-shards.mjs pattern oracle-gaps-fixed-seed-windows "$SHARD")"
windows-gap-cli:
name: Windows gap CLI on a fixture
# Gated: runs when framework or gaps changed (scripts/classify-changes.sh).
Expand Down
29 changes: 28 additions & 1 deletion packages/oracles/oracle-gaps/args.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// The CLI's arguments, shared by the CLI (index.mjs) and the daemon
// (daemon.mjs), which parses each request's arguments the same way.
import { spawnSync } from 'node:child_process'
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
import { randomBytes } from 'node:crypto'
import { availableParallelism } from 'node:os'
Expand Down Expand Up @@ -185,6 +186,12 @@ function optionsOf(args, cwd) {
// Authored mutants are the author's files: one that cannot be read
// refuses the run before any work.
if (mutants) loadAuthored(mutants)
// The commit `--diff-base` names, resolved once: the run marks lines
// changed since that commit, and a name git does not know would mark none.
const diffBase =
args[`diff-base`] === undefined
? undefined
: commitOf(pkg, `diff-base`, args[`diff-base`])
// Each source as the package names it (packagePath), a file of the
// package.
const sources = args.source.split(`,`).map((source) => {
Expand All @@ -211,7 +218,7 @@ function optionsOf(args, cwd) {
),
sources,
seed: args.seed,
diffBase: args[`diff-base`],
diffBase,
// Each run's output is a directory of its own under the package's state
// in the OS temp directory (`stateDirOf`), so runs of one package and of
// two packages never collide (maintainer decision, 2026-11-10:
Expand Down Expand Up @@ -268,6 +275,26 @@ export function packagePath(pkg, path) {
return posix(named)
}

/**
* The commit that `rev`, the value of `--<option>`, names in the git
* repository that holds `pkg`, as `{ rev, commit }`. Anything else refuses
* the run: a name git does not know, a range, a blob, an option, an empty
* value, or a package in no repository.
*/
function commitOf(pkg, option, rev) {
const parsed = spawnSync(
`git`,
[`rev-parse`, `--verify`, `--quiet`, `--end-of-options`, `${rev}^{commit}`],
{ cwd: pkg, encoding: `utf8` },
)
if (parsed.status !== 0)
throw new Refusal(
`--${option} ${JSON.stringify(rev)} names no commit of the git repository that holds the package`,
`Pass --${option} a commit, branch, or tag of the package's git repository, such as HEAD.`,
)
return { rev, commit: parsed.stdout.trim() }
}

/** A run's directory name: its start time, then random bytes, unique per run. */
function runName() {
return `${new Date().toISOString().replace(/[:.]/g, `-`)}-${randomBytes(4).toString(`hex`)}`
Expand Down
3 changes: 3 additions & 0 deletions packages/oracles/oracle-gaps/authored.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export function applyAuthored(text, mutant) {
text:
text.slice(0, at) + mutant.replace + text.slice(at + mutant.find.length),
line: text.slice(0, at).split(`\n`).length,
// The find's last line, which law 16 compares with the diff too.
endLine:
text.slice(0, at).split(`\n`).length + mutant.find.split(`\n`).length - 1,
position: at,
}
}
132 changes: 101 additions & 31 deletions packages/oracles/oracle-gaps/check.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// One gap check over a package, cold or through the daemon (index.mjs,
// daemon.mjs).
//
// With `--diff-base <rev>`, the mutant phase judges only the mutants on lines
// changed since `<rev>` (law 16 of the end-to-end oracle, which states the
// rule); the run is partial, and says so (report.mjs `diffNote`).
//
// The evidence of a check is a set of units. A unit is one Vitest run of one
// test file under one mutant spec: an unmutated pass, an operator mutant, or
// an authored patch. Its result is plain data, the outcomes of its tests,
Expand Down Expand Up @@ -47,6 +51,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url'
import { applyAuthored, loadAuthored } from './authored.mjs'
import {
authoredKey,
diffNote,
gapsOf,
keyAndSelect,
renderReport,
Expand Down Expand Up @@ -2161,21 +2166,67 @@ export default async (env) => {
const ownersOf = (mutant) => owned.at(mutant.source, mutant.position).owners
// Keys are numbered over every mutant before the selection (report.mjs).
let queue = keyAndSelect(mutants, options)
// Each authored mutant the run selects (every one without --only), with
// its patched copy, or none where its edit does not apply.
let chosen = authored
.filter(
(mutant) => !options.only || options.only.has(`authored:${mutant.id}`),
)
.map((mutant) => {
const path = realpathSync(join(pkg, mutant.source))
return {
mutant,
path,
patched: applyAuthored(readFileSync(path, `utf8`), mutant),
}
})
// With --diff-base, the run judges only the mutants, operator and
// authored, on lines changed since its commit, and says how many of the
// run's mutants those are; the baseline still runs every test file.
const diffs = new Map()
// A mutant is on a changed line when any line from its first to its last
// is in the diff (law 16).
const inDiff = (source, line, endLine = line) => {
if (!options.diffBase) return undefined
if (!diffs.has(source))
diffs.set(source, diffLines(pkg, options.diffBase, source))
const changed = diffs.get(source)
for (let at = line; at <= endLine; at += 1)
if (changed.has(at)) return true
return false
}
const selectedMutants = queue
const selectedAuthored = chosen
let judgedOnly
if (options.diffBase) {
for (const mutant of queue)
mutant.inDiff = inDiff(mutant.source, mutant.line, mutant.endLine)
const all = queue.length + chosen.length
queue = queue.filter((mutant) => mutant.inDiff)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// An authored mutant that does not apply has no line, and every run
// reports it (law 16: it runs nothing, and it is a gap under law 5).
chosen = chosen.filter(
({ mutant, patched }) =>
patched === undefined ||
inDiff(mutant.source, patched.line, patched.endLine),
)
judgedOnly = diffNote(
options.diffBase,
queue.length +
chosen.filter(({ patched }) => patched !== undefined).length,
all,
)
log(streamLine(`note`, judgedOnly))
}
// With --only, the baseline runs only the owners of the selected mutants
// (maintainer disposition G5, 2026-11-11), operator and authored.
const selectedOwners =
options.only &&
new Set([
...queue.flatMap((mutant) => [...ownersOf(mutant)]),
...authored
.filter((mutant) => options.only.has(`authored:${mutant.id}`))
.flatMap((mutant) => {
const path = realpathSync(join(pkg, mutant.source))
const patched = applyAuthored(readFileSync(path, `utf8`), mutant)
return patched
? [...owned.at(mutant.source, patched.position).owners]
: []
}),
...selectedMutants.flatMap((mutant) => [...ownersOf(mutant)]),
...selectedAuthored.flatMap(({ mutant, patched }) =>
patched ? [...owned.at(mutant.source, patched.position).owners] : [],
),
])
// The first pass runs every test file, ordinary ones too, so the report
// leads with each file's result (the v20 freeze's one loop: test results
Expand Down Expand Up @@ -2410,12 +2461,6 @@ export default async (env) => {
)

// 3–5. Mutants.
const diffs = new Map(
options.sources.map((source) => [
source,
diffLines(pkg, options.diffBase, source),
]),
)
const results = []
const started = Date.now()
// Results are appended as they finish, so a stopped survey keeps them.
Expand Down Expand Up @@ -2544,7 +2589,6 @@ export default async (env) => {
async function evaluateGroup(group, slot) {
const evaluated = Date.now()
const states = group.map((mutant) => {
mutant.inDiff = diffs.get(mutant.source)?.has(mutant.line)
const owners = ownersOf(mutant)
const targets = targetsOf(mutant, baseline, owners, oracleFiles)
const decided = owners.size === 0 || targets.oracleTargets.length === 0
Expand Down Expand Up @@ -2641,11 +2685,8 @@ export default async (env) => {
// function its edit lands in, whole files, still stopping at its first
// kill.
const authoredJobs = []
for (const mutant of authored) {
for (const { mutant, path, patched } of chosen) {
const id = `authored:${mutant.id}`
if (options.only && !options.only.has(id)) continue
const path = realpathSync(join(pkg, mutant.source))
const patched = applyAuthored(readFileSync(path, `utf8`), mutant)
const result = {
id,
authored: true,
Expand All @@ -2659,6 +2700,7 @@ export default async (env) => {
original: mutant.find,
replacement: mutant.replace,
line: patched?.line,
inDiff: inDiff(mutant.source, patched?.line, patched?.endLine),
}
results.push(result)
if (!patched) {
Expand Down Expand Up @@ -2779,6 +2821,7 @@ export default async (env) => {
excluded: excluded.map(relative),
baseline: baselineFiles,
config: options.config,
judgedOnly,
}),
)
if (existsSync(testOutput) && statSync(testOutput).size > 0)
Expand Down Expand Up @@ -2841,21 +2884,48 @@ function identityOf(options, authored, testFiles, oracleCount) {
hash.update(JSON.stringify({ ...decisive, only: [...(options.only ?? [])] }))
return {
id: hash.digest(`hex`).slice(0, 12),
inputs: `${options.sources.length} source files, ${oracleCount} oracle files, ${testFiles.length - oracleCount} ordinary test files, ${authored.length} authored mutants, seed ${options.seed ?? `(none)`}, pool ${options.pool}${options.only || options.sample !== undefined ? `; a partial run (--only or --sample)` : ``}`,
inputs: `${options.sources.length} source files, ${oracleCount} oracle files, ${testFiles.length - oracleCount} ordinary test files, ${authored.length} authored mutants, seed ${options.seed ?? `(none)`}, pool ${options.pool}${options.only || options.sample !== undefined || options.diffBase ? `; a partial run (--only, --sample, or --diff-base)` : ``}`,
}
}

// Lines changed relative to the diff base, for diff-scoped summaries.
/**
* The lines of `source` changed since the commit that `--diff-base` names
* (`diffBase.commit`, args.mjs): every line of a file that git does not
* track, since `git diff` leaves such a file out, and otherwise each line
* that `git diff` shows the working tree's file adding or changing. Paths
* are literal, so a name with `*` or `:` names one file.
*/
function diffLines(pkg, diffBase, source) {
if (!diffBase) return undefined
const child = spawnSync(`git`, [`diff`, `-U0`, diffBase, `--`, source], {
cwd: pkg,
encoding: `utf8`,
})
const git = (...args) => {
const child = spawnSync(`git`, [`--literal-pathspecs`, ...args], {
cwd: pkg,
encoding: `utf8`,
maxBuffer: 64 * 1024 * 1024,
})
if (child.status !== 0)
throw new Error(`git ${args.join(` `)} failed: ${child.stderr}`)
return child.stdout
}
const lines = new Set()
for (const match of child.stdout.matchAll(
/^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/gm,
)) {
if (git(`ls-files`, `-z`, `--`, source) === ``) {
const count = readFileSync(join(pkg, source), `utf8`).split(`\n`).length
for (let line = 1; line <= count; line += 1) lines.add(line)
return lines
}
for (const match of git(
`diff`,
`--no-color`,
`--no-ext-diff`,
`--no-textconv`,
`--text`,
`-U0`,
// A repository's diff.interHunkContext would join two near hunks across
// the unchanged lines between them, which -U0 alone does not undo.
`--inter-hunk-context=0`,
diffBase.commit,
`--`,
source,
).matchAll(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/gm)) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const start = Number(match[1])
for (let line = start; line < start + Number(match[2] ?? 1); line += 1)
lines.add(line)
Expand Down
10 changes: 9 additions & 1 deletion packages/oracles/oracle-gaps/constructed-order.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@
"a stop after the run's ending is written, while its exit is held",
"an internal error after the run's ending is answered",
"a supervisor killed while its child, with no process left, awaits its answer",
"an internal error after the run's ending is written, before the supervisor has it"
"an internal error after the run's ending is written, before the supervisor has it",
"a check with --diff-base on changed and unchanged lines",
"a check with --diff-base and an untracked source",
"a check with --diff-base on changed and unchanged lines of one function",
"a check with --diff-base and an authored mutant that does not apply",
"a check with --diff-base and an authored mutant on a changed line",
"a check with --diff-base and a multi-line authored mutant whose later line changed",
"a check with --diff-base through the daemon",
"a check with --diff-base and --only"
]
}
Loading
Loading