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
8 changes: 8 additions & 0 deletions .changeset/relevance-first-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"e2e": patch
"@e2e-dev/decision": patch
---

A test filter can score the tests it keeps. `relevance` in its result gives each test a number from 0 to 1, and a run with `--changed-since` starts the files of the likeliest first in each target, in bands at 75%, 50%, and 25%, so a failure shows sooner and `--max-failures` stops earlier. Targets go by the band of their best file, ties in config order. Inside a band, a run with more than one worker still starts the file the previous run spent longest in first.

`decisionFilter()` now scores every test it keeps: 100% for a test whose file, setup file, or stored screenshots changed, the model's answer for one it judged, and 50% for one it did not judge. When it fails, as without a key, it puts the scores the change alone gives on the error it throws, so the run still warns and runs every test, with the touched ones first. A filter of your own can do the same: an error it throws may carry `relevance`. `decisionFilter()` requires e2e 0.23.1 or newer, since an older runner refuses the new `relevance` key and drops the whole filter.
8 changes: 6 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,14 @@ suites that consume the built packages the way a user would.
can affect each test, packing test files into calls under the model's
size limit, 8 calls at a time, and leaves out the unlikely ones. It runs
every test on an `e2e.config` or lockfile change, and asks nothing about a
test whose test file, setup file, or stored screenshots changed. A failed
test whose test file, setup file, or stored screenshots changed. It scores
each test it keeps through `relevance` (1 for a touched test, the model's
probability for a judged one, 0.5 for one it did not judge), and the
runner starts each target's likeliest files first. A failed
call leaves its tests in and the rest are still judged; after 3 failed
calls it stops asking. When every call fails, or git or a file read
fails, it throws, and the run warns and runs every test.
fails, it throws, and the run warns and runs every test, the touched
ones first, since the error carries their scores in `relevance`.
- `packages/acp` — the published `@e2e-dev/acp` package: a `StepExecutor`
(`acp()`, with `claudeCode()` and `codex()` presets) that runs
`agent.act` and `agent.assert` on a coding agent over the Agent Client
Expand Down
12 changes: 9 additions & 3 deletions docs/ci.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,15 @@ To keep the checks predictable:
## Run only affected tests

On a pull request, pass `--changed-since` so a [test filter](/filters) can
leave out the tests the change is unlikely to affect. Without the flag, as
on `main`, the whole suite runs. See
[Add the decision filter](/filters#add-the-decision-filter).
leave out the tests the change is unlikely to affect. When the filter scores
tests, as the decision filter does, each target starts the files of the
likeliest to fail first. Without the flag, as on `main`, the whole suite
runs. See [Add the decision filter](/filters#add-the-decision-filter).

With the CI default of one worker, only the relevance bands order files and
collection order breaks ties: e2e reads the
[timings history](#cache-the-timings-history) only with more than one worker.
See [Start the likeliest first](/filters#start-the-likeliest-first).

## Cache the timings history

Expand Down
12 changes: 8 additions & 4 deletions docs/examples/filters/areas-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ export const areas = {
name: 'areas',
filter(tests, { changes }) {
const reached = new Set<string>();
let unmapped: string | undefined;
for (const file of changes.files) {
const area = Object.keys(AREAS).find((prefix) => file.startsWith(prefix));
if (area === undefined) return { exclude: {}, note: `${file} is in no area, so areas leaves nothing out` };
for (const tag of AREAS[area] ?? []) reached.add(tag);
if (area === undefined) unmapped = file;
else for (const tag of AREAS[area] ?? []) reached.add(tag);
}
const exclude: Record<string, string> = {};
const relevance: Record<string, number> = {};
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
for (const test of tests) {
const tags = test.tags.filter((tag) => AREA_TAGS.has(tag));
if (tags.length > 0 && !tags.some((tag) => reached.has(tag))) exclude[test.id] = 'the change reaches none of its areas';
if (tags.some((tag) => reached.has(tag))) relevance[test.id] = 1;
else if (tags.length > 0 && unmapped === undefined) exclude[test.id] = 'the change reaches none of its areas';
}
return { exclude };
if (unmapped === undefined) return { exclude, relevance };
return { exclude, relevance, note: `${unmapped} is in no area, so areas leaves nothing out` };
},
} satisfies TestFilter;
77 changes: 61 additions & 16 deletions docs/filters.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ description: Run only the tests a pull request can affect.
---

With `--changed-since <ref>`, a test filter leaves out the tests the change
since that ref can't affect. The decision filter asks a
[decision model](/decision-models), such as Jev, about each test. Without
the flag, as on `main` and locally, every test runs.
since that ref can't affect, and starts the files of the likeliest to fail first. The
decision filter asks a [decision model](/decision-models), such as Jev, about
each test. Without the flag, as on `main` and locally, every test runs.

## Add the decision filter

Expand Down Expand Up @@ -72,7 +72,8 @@ setup under 20% likely affected. Tests the model can't judge run, and the
filter's note says why: a diff over 60 kB, a test or setup file too large
to send with the diff, or some calls failing. When every call fails, as
without a key on a pull request from a fork, the filter fails: the run
warns and runs every test. When git can't measure the change, because it
warns and runs every test, starting first the ones whose test file, setup
file, or stored screenshots changed. When git can't measure the change, because it
is missing, the project is not in a repository, or the ref shares no
history with `HEAD`, the run fails with `INVALID_CHANGED_SINCE`. The
filter needs `ai` 7.0.134 or later.
Expand Down Expand Up @@ -123,23 +124,53 @@ The decision filter also runs:
`Cargo.lock`. The model judges any other file, and those of a sibling
project.

A filter that throws, returns something other than `{ exclude, note? }`,
or runs past 5 minutes leaves nothing out, and a warning says so. The next
filter still judges every test it was given.
A filter that throws, returns something other than
`{ exclude, relevance?, note? }`, or runs past 5 minutes leaves nothing out,
and a warning says so. The next filter still judges every test it was given.
A failed filter scores nothing either, unless the error it threw carries
`relevance`, as in [Write your own](#write-your-own). A filter that runs past
5 minutes is abandoned, and nothing it throws after that counts.

On another CI service, pass `--changed-since origin/main` on pull request
builds only, and fetch enough history for the merge base. On GitLab, see
[Run only affected tests](/ci/gitlab#run-only-affected-tests). With
`--shard`, the filters on each machine judge only its slice.

## Start the likeliest first

The decision filter scores each test it keeps, and each target starts the
files of the likeliest tests first. A failure then shows early, which saves
the most with `--max-failures` and with one worker, the default in CI.

| Test | Score |
| --- | --- |
| Its test file, setup file, or stored screenshots changed | 100% |
| Every other test | The higher of its rating and its setup's, where a rating the model didn't give counts as 50% |

- Files start in four bands: 75% and up, 50% up to 75%, 25% up to 50%, and
under 25%. A file takes the band of its likeliest test.
- With more than one worker, the longest file by the
[timings history](/ci#cache-the-timings-history) starts first inside a
band. A file with no time starts before the timed ones.
- Targets go by the band of their best file, ties in config order, and each
target starts its own files in that order. A target's setup tests still
finish before its first test file starts.
- A short likely file can start before a long unlikely one, so with several
workers a run that finishes whole can end later. The report keeps its
order, and `e2e list` shows none.

A filter you write scores tests with `relevance`, as in
[Write your own](#write-your-own).

## What you see

- **Terminal**: one line per filter, such as
`decision kept 7 of 77 tests, left out 70 (1 call, 17.6k tokens, $0.0007)`.
The decision filter's note ends with its spend: model calls, tokens, and
the cost when the provider reports it, as the AI Gateway does. A
`Left out` row follows in the summary. When the filters leave nothing to
run, the run passes and starts no app.
the cost when the provider reports it, as the AI Gateway does. A line such
as `ranked 7 of 7 tests by relevance, likeliest first` follows when
the scores put tests in more than one band. A `Left out` row follows in the summary. When the
filters leave nothing to run, the run passes and starts no app.
- **[Pull request comment](/github) and `summary.md`**: the headline
`7 passed, 70 left out by filters`, and a folded table of the tests with
their reasons.
Expand Down Expand Up @@ -188,8 +219,9 @@ on:
## Write your own

A filter is `{ name, filter }`: `filter` gets the tests and the change, and
returns the tests to leave out with a reason for each. This one maps
changed paths to test tags:
returns the tests to leave out with a reason for each. It can also score the
rest from 0 to 1 with `relevance`. This one maps changed paths to test tags,
and starts the tests whose area the change reaches first:

```ts title="areas-filter.ts"
import type { TestFilter } from 'e2e';
Expand All @@ -207,17 +239,21 @@ export const areas = {
name: 'areas',
filter(tests, { changes }) {
const reached = new Set<string>();
let unmapped: string | undefined;
for (const file of changes.files) {
const area = Object.keys(AREAS).find((prefix) => file.startsWith(prefix));
if (area === undefined) return { exclude: {}, note: `${file} is in no area, so areas leaves nothing out` };
for (const tag of AREAS[area] ?? []) reached.add(tag);
if (area === undefined) unmapped = file;
else for (const tag of AREAS[area] ?? []) reached.add(tag);
}
const exclude: Record<string, string> = {};
const relevance: Record<string, number> = {};
for (const test of tests) {
const tags = test.tags.filter((tag) => AREA_TAGS.has(tag));
if (tags.length > 0 && !tags.some((tag) => reached.has(tag))) exclude[test.id] = 'the change reaches none of its areas';
if (tags.some((tag) => reached.has(tag))) relevance[test.id] = 1;
else if (tags.length > 0 && unmapped === undefined) exclude[test.id] = 'the change reaches none of its areas';
}
return { exclude };
if (unmapped === undefined) return { exclude, relevance };
return { exclude, relevance, note: `${unmapped} is in no area, so areas leaves nothing out` };
},
} satisfies TestFilter;
```
Expand All @@ -240,6 +276,15 @@ export default {

- Every test you don't name runs, and so does a test you name in a serial
group another of its tests runs in.
- `relevance` takes test ids and numbers from 0 to 1, a test no filter names
counts as 0, and a test several filters name keeps its highest. Anything
else fails the filter, as for `exclude`.
- A filter that fails can still score. An error it throws with a `relevance`
property, shaped and checked like the result's, scores the tests the same
way, and the failure still warns and leaves nothing out. The decision
filter does this, so a run without a key still starts the tests the change
touches first. Scores that fail the check are dropped, and the warning
says so.
- Each filter judges only the tests the one before it kept, and is not
called when none are left.
- Pass `signal` to every call, and `redact` what you send to a model. It
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ Test files, directories, or globs relative to the project root:
| `--exclude-tag <tags>` | Leaves out tests with any of the tags, comma-separated or repeated, whatever `--tag` or a positional selected. A serial group still runs whole when any member is selected; tag the serial `describe` to exclude the flow. |
| `--grep <pattern>` | Only tests whose title matches one of the regular expressions; repeatable. The title is the describe titles and the test title joined by one space (`checkout pays`), without the file or the tags. A bare pattern is case-sensitive; `'/pattern/i'` adds flags. A pattern that does not compile is a usage error, exit 2. |
| `--grep-invert <pattern>` | Leaves out tests whose title matches one of the regular expressions; repeatable, with the text and forms of `--grep`. With both, the run keeps what `--grep` matches minus what `--grep-invert` matches. |
| `--changed-since <ref>` | Runs the configured [test filters](/filters) on the change since the ref: every file that differs from the merge base of the ref and `HEAD`, uncommitted and untracked files included. Without it, no filter runs. With no filters configured, a warning says every test runs. |
| `--changed-since <ref>` | Runs the configured [test filters](/filters) on the change since the ref: every file that differs from the merge base of the ref and `HEAD`, uncommitted and untracked files included. Each target starts the files of the tests the filters score likeliest first. Without it, no filter runs. With no filters configured, a warning says every test runs. |
| `--last-failed` | Only the tests the previous run did not pass; the list after these tables says which. |
| `--shard <index/total>` | Runs one contiguous slice of the selected tests, such as `2/3`, cut after every other flag. Slices differ by at most one test. A serial group counts as one and is never split. Each shard brings only the setup tests its own tests need. Skipped tests are not cut: the shard that runs the nearest test before one reports it, else the first shard, so only one shard does. Every shard runs the same command with a different index. The order is the report order, so a suite shards the same way on every machine. A shard with no test is `NO_TESTS` unless `--pass-with-no-tests`. See [Sharding](/ci#sharding). |
| `--pass-with-no-tests` | Exits 0 instead of `NO_TESTS` when nothing is selected. A run with nothing to run prepares no engine and starts no app or service command. |
Expand Down
14 changes: 13 additions & 1 deletion docs/reference/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ workers: 4, // default: 1 in CI, else half the cores
- Locally the default is `max(1, floor(availableParallelism() / 2))`.
- An engine may cap it lower: `workers` never exceeds what a target's engine
declares.
- With more than one worker, each target's files start longest first by the
[timings history](/ci#cache-the-timings-history). A file with no time starts
before the timed ones. With `--changed-since`, the likeliest files start
before that, as
[Start the likeliest first](/filters#start-the-likeliest-first) says.

#### `reporters`

Expand Down Expand Up @@ -1748,7 +1753,7 @@ interface TestFilter {
}

interface TestFilterCandidate {
/** The report's `testId`, and the key `exclude` takes. */
/** The report's `testId`, and the key `exclude` and `relevance` take. */
readonly id: string;
/** Project-relative path of the test file. */
readonly file: string;
Expand Down Expand Up @@ -1787,6 +1792,8 @@ interface TestFilterChanges {
interface TestFilterResult {
/** The tests to leave out, by `id`, each with a non-empty reason the report shows. An `id` of a collected test this filter wasn't given is ignored; any other fails the filter. */
readonly exclude: Readonly<Record<string, string>>;
/** How likely each test is to fail on this change, by `id` as for `exclude`, from 0 to 1; any other score fails the filter. Each target starts the files of the likeliest first, in bands at 75%, 50%, and 25%, and targets go by the band of their best file, ties in config order. A test no filter scores counts as 0, and a test several filters score keeps its highest. */
readonly relevance?: Readonly<Record<string, number>>;
/** One line about the whole decision. */
readonly note?: string;
}
Expand All @@ -1795,6 +1802,11 @@ interface TestFilterResult {
`TestFilter`, `TestFilterCandidate`, `TestFilterContext`,
`TestFilterChanges`, and `TestFilterResult` are exported from `e2e`.

A filter that throws, returns an invalid result, or runs past 5 minutes
leaves nothing out, and a warning says so. An error it throws can carry
`relevance`, shaped as in `TestFilterResult`: the run still starts the
likeliest first. See [Test filters](/filters#write-your-own).

### `CredentialConfig`

```ts
Expand Down
5 changes: 3 additions & 2 deletions packages/decision/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ for setup, vision, gates, provider options, and limits.
## Test filter

With `e2e run --changed-since <ref>`, `decisionFilter()` asks a decision
model whether the change since that ref can affect each test, and leaves
out the unlikely ones:
model whether the change since that ref can affect each test, leaves out
the unlikely ones, and scores the rest so each target starts the files
of the likeliest tests first:

```ts
import type { E2EConfig } from 'e2e';
Expand Down
2 changes: 1 addition & 1 deletion packages/decision/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"peerDependencies": {
"ai": "^7.0.134",
"e2e": ">=0.21.3 <1"
"e2e": ">=0.23.1 <1"
},
"devDependencies": {
"@ai-sdk/openai": "4.0.111",
Expand Down
Loading
Loading