Skip to content

feat(ci): shuffled repeat runs of a pull request's changed test files #852

Closed

Description

@dan-petty

Context & Rationale

AGENTS.md:11 sets zero tolerance for flaky tests, but nothing reorders or repeats them:

  • uv.lock has no pytest-randomly, pytest-repeat or pytest-rerunfailures.
  • addopts is -n logical (pyproject.toml:105).
  • xdist's load scheduling turns order coupling into intermittent failures on later, unrelated runs.

Evidence:

  • Order dependence causes 59% of flaky Python tests (arXiv 2101.09077).
  • Meta's TestGen-LLM rejects a test that does not pass all of 5 runs (arXiv 2402.09171).

A serial run of two files took about 15 s, of which 9 s was setup, so N repeats cannot go into pre-push.

Scope & Deliverables

  • Deliverable:
    • Add pytest-randomly 5.x (pytest-dev) to the dev group, and add -p no:randomly to addopts so existing runs keep their order.
    • Add devops ci test --repeat N --seed S:
      • it runs the selected targets N times serially (-n 0) with -p randomly --randomly-seed=S+i;
      • S defaults to a value derived from HEAD's hash;
      • on the first failure it prints the seed and a reproduction command;
      • the default N lives in config/defaults.py.
    • Add a 'Changed-Test Independence' job to ci.yml, triggered on pull_request. It runs --repeat 3 over the added or modified test files and is skipped when no test file changed.
  • Acceptance criteria:
    • A pytester fixture with two tests that share module state passes in plain order and fails under a seed that reverses them; the output prints that seed.
    • The argv of every repeat run is asserted, and the full-gate argv contains no randomly option.
    • With the plugin installed, uv run pytest --co -q gives the same collection order as before.
    • The job is skipped when no test file changed.
    • The unit tests take at most 1 s.
  • Key questions: none.
  • Constraint:
    • Shuffling the whole suite waits for the thread-leak entry (ROADMAP.md:1070).
    • Timing-ceiling flakes are fixed at the source, by the CLI start-up item (ROADMAP.md:1022).

Strategic Alignment

Activity

  1. added
    type/feature
    scope/cli
    priority/p2-medium
    on Oct 1, 2026
  2. added
    scope/ci
    on Oct 2, 2026
  3. added this to the v0.2.32 milestone on Oct 8, 2026
  4. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Placed in v0.2.32 on 2026-10-08 at the owner's request ("Populate 10 security and ci issues into each v0.2.30, v0.2.31, and v0.2.32 milestone"). This is a person's placement. Release theme: URLs and host keys read by the library that dials, ollama hardening, and pull-request test-quality tooling. Position 8 of 10 in the release's planned merge order. Its board Status is set to Ready, so the release-start fill keeps it.

  5. dan-petty-bot commented on Oct 9, 2026

    @dan-petty-bot
    Collaborator

    What changed

    #1512, merged into release/v0.2.32 as a9d7fd8.

    Summary

    AGENTS.md sets zero tolerance for flaky tests, but nothing reordered or repeated a test. A test that passes only after another one stayed hidden until xdist's load scheduling put the two in a different order. This PR makes order dependence visible in the test files a pull request adds or changes.

    • pytest-randomly 5.0.0 (pytest-dev) joins the dev group. addopts blocks it with -p no:randomly, so pytest, devops ci and the gate keep file order, and uv run pytest --co -q collects the same order as before.
    • devops ci test --repeat N --seed S runs the selected test files N times, one run after another, each as uv run pytest -n 0 [-v] [-k K] [-x] -p randomly --randomly-seed=<S+i> <files>.
      • S defaults to the first eight hex digits of HEAD's commit hash, read as an integer.
      • The first failing run stops the command and prints Run i/N failed with seed S+i with the command that reproduces it.
      • The default N is DEFAULT_CI_TEST_REPEAT_RUNS = 0 in config/defaults.py.
      • A run that no test file narrows is refused, since shuffling the whole suite waits for thread-leak isolation.
    • Changed-Test Independence, an advisory ci.yml job:

    17 file(s) changed.

    How it was verified

    Check runs at 470cf3a:

    • Prune PR DevContainer Images: pass
    • CodeQL: pass
    • Static Analysis: pass
    • Analyze (actions): pass
    • Tests & Coverage: pending
    • Changed-Test Independence: pass
    • Analyze (python): pass
    • DevContainer Image: skipping
    • Service Image: skipping
    • Dependency Freshness: skipping

    Acceptance Criteria of docs/agent/tasks/task-852-shuffled-repeat-runs-of-changed-test-files.md:

    • A pytester project with two tests that share module state passes in plain order and fails under a seed that reverses them, and the output prints that seed. test_repeat_stops_at_the_seed_that_breaks_order_dependent_tests (tests/test_ci_repeat.py) runs devops ci test --repeat 3 --seed 2 tests/test_shared.py with each run going to an in-process pytester.runpytest. Seed 2 keeps file order and passes; seed 3 reverses it and fails; seed 4 never runs. The output holds Run 2/3 failed with seed 3 and uv run pytest -n 0 -p randomly --randomly-seed=3 tests/test_shared.py. The inner project blocks randomly in its own addopts, as the repository does, so the test also shows -p randomly overriding the block.
    • The argv of every repeat run is asserted, and the full-gate argv has no randomly option. test_each_repeat_runs_the_targets_serially_with_the_next_seed asserts the three argvs and their working directory for --repeat 3 --seed 10 -n 4 -v -k unit -x -- tests/test_x.py. test_the_gate_and_a_plain_run_stay_in_file_order asserts that no get_check_specs() command and no run without --repeat names randomly, and that the default is 0.
    • With the plugin installed, uv run pytest --co -q gives the same collection order as before. On this branch's base, 10,277 tests collected; with the change, 10,288. After removing the 11 additions (the 10 tests of tests/test_ci_repeat.py and this task file's own check in tests/test_agent_task_files.py), the two lists are identical (diff prints nothing). test_pytest_randomly_is_installed_and_blocked_by_default pins the pin and the block.
    • The job is skipped when no test file changed: the detect step writes changed=false, and the toolchain and test steps run only if: steps.changed_tests.outputs.changed == 'true'. test_the_changed_test_job_reruns_added_or_modified_test_files pins the job's trigger, permissions, diff filter, pathspec, step conditions, cache inputs and command, and that no run script expands ${{ }}.
    • The unit tests take at most 1 s: measured at 0.65 s to 0.89 s call duration in total on one worker (uv run pytest -n 0 --durations=0 tests/test_ci_repeat.py) in six of seven runs, at load average 12 to 15 on 16 cores; the seventh, during a burst of other worktrees' suites on the same host, measured 1.58 s. The pytester test is 0.39 s to 0.46 s of the total, and each CLI case 0.03 s to 0.06 s. The module builds the ci command group once, as tests/test_k8s_run_job.py does, rather than Typer rebuilding it on every invoke; before that, the same command measured 0.90 s to 0.96 s.
    • Without paths, or when a trigger file such as tests/conftest.py sends the run to the whole suite, --repeat exits 1 and starts nothing (test_repeat_refuses_to_shuffle_the_whole_suite). When HEAD cannot be read it exits 1, names --seed and starts no test run (test_an_unreadable_head_asks_for_a_seed). The HEAD seed is int("deadbeef", 16) for HEAD deadbeef… (test_the_first_seed_defaults_to_heads_commit_hash). --dry-run plans every run and starts no process (test_a_dry_run_plans_every_repeat_and_starts_no_process).
    • Pending a person: on the pull request that delivers this item, which changes tests/test_ci.py and tests/test_ci_repeat.py, uv run devops pr checks <number> lists Changed-Test Independence. With the run ID from uv run devops gh runs list --branch feat/852-shuffled-repeat-changed-test-files, uv run devops gh runs view <run_id> --log | grep '^Changed-Test Independence' | grep -E 'tests/test_ci(_repeat)?\.py$|Using --randomly-seed=' prints both file names from the Detect Changed Test Files step and three Using --randomly-seed= lines whose values are consecutive.
    • Pending a person: on the first pull request into release/v0.2.32 that changes no test file, uv run devops gh runs view <run_id> --log | grep '^Changed-Test Independence', with the run ID from uv run devops gh runs list --branch <head branch>, prints "No test file changed; skipping." and no line from the Set up uv toolchain or Run Changed Tests in Shuffled Order steps, which the run's summary page shows as skipped.
  6. added a commit that references this issue on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p2-mediumscope/ciscope/clitype/feature

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions