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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ The project settings of Claude Code, `.claude/settings.json`, turn off the plugi
```
Validates every check `devops ci` runs concurrently: Python runtime, tests, the coverage floor, linting, formatting, strict typing, dependency audit, security scanning, the wheel and sdist build, workflow actionlint and zizmor, and documentation freshness.

Several agents share one host, and the full test suite starts one pytest worker for each CPU, so gates that run at once make each other several times slower (11189 tests took 247 s alone and 440 s each beside a second gate). A run that selects `test`, `security` or `typecheck`, and the full-suite `devops ci test` and `devops ci coverage`, therefore wait for a free gate slot first and say which process and worktree hold the slots. One slot exists for each 16 CPUs, and at least one; `ci.gate_slots` (or `DEVOPS_CLI_CI_GATE_SLOTS`) sets the number. The pre-push hook, a test run narrowed by paths or by `-n`, a dry run and a cached verdict never wait. The operating system frees a slot when its holder ends, also when it is killed. The wait is not part of the gate time and is recorded as the histogram `devops_cli_ci_gate_slot_wait_seconds`.
Several agents share one host, and the full test suite starts one pytest worker for each CPU, so gates that run at once make each other several times slower (11189 tests took 247 s alone and 440 s each beside a second gate). A run that selects `test`, `security` or `typecheck`, and the full-suite `devops ci test` and `devops ci coverage`, therefore wait for a free gate slot first and say which process and worktree hold the slots. A direct `uv run pytest` that starts its workers from `addopts`' `-n logical`, or from `-n auto`, also waits for a slot, and then starts at most the CPUs of one slot as workers (#4525). One slot exists for each 16 CPUs, and at least one; `ci.gate_slots` (or `DEVOPS_CLI_CI_GATE_SLOTS`) sets the number. The holder of a slot sets `DEVOPS_CLI_GATE_SLOT_HELD=1` for its children, so the pytest that a gate starts takes no second slot. The pre-push hook, a test run narrowed by paths or by `-n`, a pytest run with `--collect-only`, a dry run and a cached verdict never wait. The operating system frees a slot when its holder ends, also when it is killed. The wait is not part of the gate time and is recorded as the histogram `devops_cli_ci_gate_slot_wait_seconds`.

### Shell notes for agents

Expand Down
2 changes: 1 addition & 1 deletion docs/TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Every new test follows these rules. Some of them name a path, fixture or check t

- `devops ci test <paths>` runs only the tests a change reaches, and so does `devops test run --changed`, over the files changed since the branch left `main`, committed or not. A test module is reached when it is named for a changed module, at any depth, or imports it. A test module that only sits in the directory mirroring the module's package is not reached.
- `devops ci test` takes a directory too. A directory of `tests/` runs the test modules (`test_*.py`) inside it at any depth, and not its helper modules. A directory of `src/` selects tests for the Python modules inside it, as for a list of files. It refuses a path that does not exist, a directory that gives no module, and a directory outside `src/` and `tests/`, with exit status 1 and an error that names the path, whatever `--fallback` says. A file that exists and maps to no test, such as `README.md`, still exits 0 with a note, and a trigger file that is gone, such as a deleted `conftest.py`, still forces the full run (#2868).
- Given no `-n`, a narrowed `devops ci test` runs serially for up to `DEFAULT_CI_TEST_SERIAL_MAX_FILES` test modules, where starting xdist workers costs more than it saves, and on `DEFAULT_CI_TEST_NARROWED_WORKERS` workers above that; the full suite, and the fallback to it, run on `addopts`' `-n logical`. Any run with workers distributes its tests with `addopts`' `--dist worksteal`: a worker that runs out of tests takes half the tests another worker has not started, so a block of slow tests handed to one worker late in the run no longer keeps the others waiting for it. It runs pytest in a session of its own, so a timeout, Ctrl-C, SIGTERM or SIGHUP stops pytest and its workers with `uv`: a timeout with SIGTERM, a signal by first forwarding the SIGINT a terminal would have sent, so pytest runs its teardown, then SIGTERM and SIGKILL, which a second signal brings forward and cannot skip. The run takes over all three signals through `loop.add_signal_handler`, so each one wakes the run's event loop wherever it lands, only cancels the run, and raises no KeyboardInterrupt in the middle of the stop. A test that drives `devops ci test` doubles `asyncio.create_subprocess_exec` with `record_spawns` from `tests/support/session_spawns.py`, and asks for `refuse_nested_test_runs`, which fails any real `uv` or pytest it would start.
- Given no `-n`, a narrowed `devops ci test` runs serially for up to `DEFAULT_CI_TEST_SERIAL_MAX_FILES` test modules, where starting xdist workers costs more than it saves, and on `DEFAULT_CI_TEST_NARROWED_WORKERS` workers above that; the full suite, and the fallback to it, run on `addopts`' `-n logical`. A direct `uv run pytest` with `-n logical` or `-n auto` waits for a gate slot in the `pytest_xdist_auto_num_workers` wrapper of the root conftest, and starts at most the CPUs of one slot (#4525). Any run with workers distributes its tests with `addopts`' `--dist worksteal`: a worker that runs out of tests takes half the tests another worker has not started, so a block of slow tests handed to one worker late in the run no longer keeps the others waiting for it. It runs pytest in a session of its own, so a timeout, Ctrl-C, SIGTERM or SIGHUP stops pytest and its workers with `uv`: a timeout with SIGTERM, a signal by first forwarding the SIGINT a terminal would have sent, so pytest runs its teardown, then SIGTERM and SIGKILL, which a second signal brings forward and cannot skip. The run takes over all three signals through `loop.add_signal_handler`, so each one wakes the run's event loop wherever it lands, only cancels the run, and raises no KeyboardInterrupt in the middle of the stop. A test that drives `devops ci test` doubles `asyncio.create_subprocess_exec` with `record_spawns` from `tests/support/session_spawns.py`, and asks for `refuse_nested_test_runs`, which fails any real `uv` or pytest it would start.
- A test module that replays a shared set computes the replay once in a module-scoped fixture and gives each case its own parametrized item, so a narrowed run stays inside the budget and xdist can distribute the cases.
- With a fresh coverage index (`devops ci coverage --build-index`), `devops ci test` selects the test modules that executed each changed module the index holds, in place of name-and-import matching. A module the index does not hold, a missing or stale index, and `devops test run --changed` select by name and import. So a test that should run for a module executes the module's code inside the test, not only on import, and names the module or imports it.
- A change that can alter tests that never import it runs the whole suite: `conftest.py`, `pyproject.toml`, `uv.lock`, `.python-version`, and a helper module under `tests/` that reaches every test, as the next rule says. `devops ci test`, and so the commit hook, runs the whole suite for any `conftest.py` it is given, at any depth, and under `--no-fallback` refuses any of these files instead. `devops test run --changed` narrows a conftest below `tests/`: one that only defines fixtures selects its own directory, and one that binds a `pytest_*` hook, which pytest calls for every test, selects the whole suite.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-11
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Design

## Decisions
- **The slot is taken in a wrapper of `pytest_xdist_auto_num_workers`.** xdist calls the hook only when `-n` is `logical` or `auto`, only in the controller, and before it sets `config.option.tx`. That is the exact set of runs that start one worker for each CPU. The wrapper takes the count of xdist and returns it capped, so the variable `PYTEST_XDIST_AUTO_NUM_WORKERS` and the choice between `logical` and `auto` stay with xdist.
- **The cap is the CPUs of the process over the number of slots, and at least one.** One slot on 16 CPUs gives 16, which `-n logical` gave before. Two slots on 32 CPUs give 16 each. A setting of 4 slots on 16 CPUs gives 4.
- **The holder marks its environment.** `gate_slot` sets `DEVOPS_CLI_GATE_SLOT_HELD=1` while it holds the slot. The pytest of the `devops ci` test row, and a pytest that a test starts inside a direct run, inherit it and take no second slot. Without the mark they would wait for the slot of their own parent until the check timeout.
- **The hook lives in the root `tests/conftest.py`.** It must be registered before `pytest_cmdline_main`. The support modules of `tests/support/` are registered later, in `pytest_configure`. `optionalhook=True` keeps a run with `-p no:xdist` valid.
- **The hook runs before HOME is isolated.** It needs the settings and the data directory of the person, the same ones `devops ci` reads, so that both share one set of slots. The controller of an xdist run runs no test.
- **The logic lives in `src/devops_cli/ci/gate_slots.py`.** A test file may not import a conftest, so `direct_run_slot` holds the behavior that tests check, and the conftest only calls it.

## Rejected alternatives
- **Take the slot in `pytest_configure` (the CI audit plan).** xdist sets the worker count in `pytest_cmdline_main`, before `pytest_configure` (`xdist/plugin.py:305-316`). A `pytest_configure` hook could only rewrite `config.option.tx`, an internal option of xdist.
- **Narrow a direct run to 4 workers while a slot is held (the first design of the issue).** The run still loads the host beside the gate, and it does not wait. The CI audit chose the wait.
- **Set `PYTEST_XDIST_AUTO_NUM_WORKERS` from the hook.** The default of xdist reads it, but a value that the person or a runner pod set would then need its own merge rule. The wrapper gets the result of that default and caps it.
- **Compare the holder process number with the pytest parent.** `devops ci` starts pytest through `uv run`, so the holder is the grandparent, and a test can start pytest deeper. The variable reaches every child.
- **Take a slot for every direct run, also with an explicit `-n`.** `devops ci test -n <count>` takes no slot, and a person who sets the count has chosen the load. A serial run of one file would wait up to the full gate time.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Proposal: #4525

## Why
A direct `uv run pytest` takes no gate slot (#4525). `addopts` give it `-n logical`, so on the shared 16-CPU host it starts 16 xdist workers beside a `devops ci` gate that holds the only slot. On 2026-11-11 such a run took 458% CPU beside the gate. The load average went from 2 to 41, the test row of the gate hit its 1800 s timeout, and mypy and bandit ran 6 to 9 times slower than on the GitHub runner. The slot limits only the runs of `devops ci`.

## What Changes
1. `gate_slot` sets `DEVOPS_CLI_GATE_SLOT_HELD=1` in the environment of its process while it holds a slot, and removes it after. The children of the holder, such as the pytest of the `devops ci` test row, get it. `build_subprocess_env` passes it through its `DEVOPS_CLI_` prefix.
2. The new `direct_run_slot` in `src/devops_cli/ci/gate_slots.py` holds a slot for a pytest run that no holder started. It waits as a gate waits, names the holders, and gives at most the CPUs of the process over the number of slots, and at least one. A run that finds `DEVOPS_CLI_GATE_SLOT_HELD` takes no slot and keeps its worker count.
3. The root `tests/conftest.py` wraps the xdist hook `pytest_xdist_auto_num_workers`. xdist calls this hook only for `-n logical` or `-n auto`, in the controller. The wrapper takes the count of xdist, which already reads `PYTEST_XDIST_AUTO_NUM_WORKERS`, and returns it capped by `direct_run_slot`. A run with `--collect-only`, `--help` or `--version` starts no worker and takes no slot. A run with an explicit `-n` count never calls the hook, so it takes no slot, as `devops ci test -n` takes none.
4. `CONTRIBUTING.md` and `docs/TESTING.md` say which pytest runs take a slot.

A direct pytest run now waits for a slot, which a person can see, so the change carries a spec delta.

## Capabilities
- `ci-gate-admission`: ADDED "A direct pytest run with automatic workers takes a slot" and "A run that a slot holder started takes no second slot".

## Impact
- A direct `uv run pytest` or `devops test run` with the default `-n logical` waits while every slot is held. On the 16-CPU host it then starts 16 workers, as before. `devops ci test <paths>` and a run with an explicit `-n` do not wait.
- The `devops ci` test row, its shards on the GitHub runner and the in-image suite hold the slot in the parent `devops` process. Their pytest sees the variable and keeps `-n logical`.
- A runner pod that sets `PYTEST_XDIST_AUTO_NUM_WORKERS` keeps that bound, capped by the CPUs of one slot.
- The change fits one pull request.

## Feasibility
- pytest-xdist 3.8.0 (`.venv`, `pytest_xdist-3.8.0.dist-info`) resolves `-n logical` and `-n auto` in its `tryfirst` `pytest_cmdline_main` (`xdist/plugin.py:305-316`), which calls the `firstresult` hook `pytest_xdist_auto_num_workers` (`xdist/newhooks.py:100`). Its default reads `PYTEST_XDIST_AUTO_NUM_WORKERS`, then the CPU count (`xdist/plugin.py:16-32`). pytest 9.1.1 calls `pytest_configure` later, in `wrap_session`. So the CI audit plan's `pytest_configure` is too late to set the count; the hook is the site.
- An xdist worker sets `numprocesses = None` before it calls `pytest_cmdline_main` (`xdist/remote.py:398`, `427`), so it never calls the hook.
- `tests/support/bootstrap.py` isolates HOME in a `trylast` `pytest_configure`. The hook runs before that, in the controller, which runs no test. It reads the settings and the data directory of the person, which `devops ci` also reads.
- Run with `DEVOPS_CLI_CI_GATE_SLOTS=4` while another process held all 4 slots on 16 CPUs, `uv run pytest tests/ci/test_ci_gate_slots.py -k number_of_slots` printed the wait line and waited. After the holders ended it started 4 workers. With the slots still held, the same run with `DEVOPS_CLI_GATE_SLOT_HELD=1` and `PYTEST_XDIST_AUTO_NUM_WORKERS=2`, the run with `-n 2`, and the run with `--collect-only` did not wait; the first two started 2 workers. The pull request comment has the commands and their output.

## Existing Tool Considered
pytest-xdist covers the worker count through its documented hook `pytest_xdist_auto_num_workers` and the variable `PYTEST_XDIST_AUTO_NUM_WORKERS`. `filelock` covers the slot, through the existing `gate_slot`. The change adds no dependency and no second lock: one hook wrapper of about 10 lines and one function of about 15 lines over `gate_slot`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## ADDED Requirements

### Requirement: A direct pytest run with automatic workers takes a slot
A pytest run that takes its worker count from `-n logical` or `-n auto`, and that no holder of a slot started, SHALL take a gate slot before it starts its workers, and SHALL wait while every slot is held. It SHALL start at most the CPU count of the process divided by the number of slots, and at least one worker.

#### Scenario: A gate holds the only slot
- **WHEN** the number of slots is 1, a gate in another process holds the slot, and a direct pytest run asks for 16 workers on 16 CPUs
- **THEN** the run waits and names the holder
- **AND** the run starts 16 workers after the gate gives the slot back

#### Scenario: Two slots on 32 CPUs
- **WHEN** `ci.gate_slots` is not set, the process has 32 CPUs, and xdist asks for 32 workers
- **THEN** the run starts 16 workers

### Requirement: A run that a slot holder started takes no second slot
While a process holds a gate slot, its environment, which its children get, SHALL hold `DEVOPS_CLI_GATE_SLOT_HELD=1`. A pytest run that finds this variable SHALL take no slot and SHALL keep the worker count of xdist.

#### Scenario: The pytest of a gate
- **WHEN** a gate holds the only slot and its pytest asks for 32 workers
- **THEN** the pytest run starts 32 workers at once and does not wait

#### Scenario: The mark ends with the slot
- **WHEN** a process gives its slot back
- **THEN** `DEVOPS_CLI_GATE_SLOT_HELD` is not in its environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Tasks

## 1. Behavior

- [x] 1.1 Add tests to `tests/ci/test_ci_gate_slots.py`: a held slot marks the environment of its holder and of the children that it starts, a direct run waits while a gate holds the only slot, a run that a gate started takes no slot and keeps its workers, and a direct run starts at most the CPUs of one slot
- Verification: `$ uv run devops ci test tests/ci/test_ci_gate_slots.py`
- [x] 1.2 Set `DEVOPS_CLI_GATE_SLOT_HELD` in `gate_slot`, add `direct_run_slot` to `src/devops_cli/ci/gate_slots.py`, and call it from a `pytest_xdist_auto_num_workers` wrapper in `tests/conftest.py`
- Verification: `$ uv run devops ci test tests/ci/test_ci_gate_slots.py tests/ci/test_ci_gate_admission.py`

## 2. Documents

- [x] 2.1 Say in `CONTRIBUTING.md` and `docs/TESTING.md` which pytest runs take a gate slot
- Verification: `$ uv run devops ci docs`

- Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD).
23 changes: 23 additions & 0 deletions openspec/specs/ci-gate-admission/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,26 @@ The time a run waits for a slot SHALL NOT count in the elapsed time of the gate,
- **WHEN** a run waits 600 s for a slot and then runs checks that take 30 s
- **THEN** the summary reports an elapsed time of about 30 s and no over-budget warning
- **AND** the histogram `devops_cli_ci_gate_slot_wait_seconds` has one observation of about 600

### Requirement: A direct pytest run with automatic workers takes a slot
A pytest run that takes its worker count from `-n logical` or `-n auto`, and that no holder of a slot started, SHALL take a gate slot before it starts its workers, and SHALL wait while every slot is held. It SHALL start at most the CPU count of the process divided by the number of slots, and at least one worker.

#### Scenario: A gate holds the only slot
- **WHEN** the number of slots is 1, a gate in another process holds the slot, and a direct pytest run asks for 16 workers on 16 CPUs
- **THEN** the run waits and names the holder
- **AND** the run starts 16 workers after the gate gives the slot back

#### Scenario: Two slots on 32 CPUs
- **WHEN** `ci.gate_slots` is not set, the process has 32 CPUs, and xdist asks for 32 workers
- **THEN** the run starts 16 workers

### Requirement: A run that a slot holder started takes no second slot
While a process holds a gate slot, its environment, which its children get, SHALL hold `DEVOPS_CLI_GATE_SLOT_HELD=1`. A pytest run that finds this variable SHALL take no slot and SHALL keep the worker count of xdist.

#### Scenario: The pytest of a gate
- **WHEN** a gate holds the only slot and its pytest asks for 32 workers
- **THEN** the pytest run starts 32 workers at once and does not wait

#### Scenario: The mark ends with the slot
- **WHEN** a process gives its slot back
- **THEN** `DEVOPS_CLI_GATE_SLOT_HELD` is not in its environment
Loading
Loading