Repository navigation
fix(backend-tests): test.sh runs the files it is given and bounds local workers - #25717
Merged
Merged
Conversation
…al workers `bash test.sh a.py b.py` ignored its arguments and ran the whole unit suite (~2,970 files), and every invocation started one worker per core. With ten agent lanes on one 18-core host that was 48 concurrent pytest processes and a load average near 380. - Positional paths run exactly those files (tests/unit/x.py, backend/tests/unit/x.py, caller-relative, a bare name unique under tests/, and file::node). Unresolvable, ambiguous or directory paths and unknown options exit 2 naming the argument; nothing falls back to the suite. - --changed runs CI's PR selector over the merge-base diff against origin/main plus uncommitted and untracked work. It is the local default with no arguments; CI (CI/GITHUB_ACTIONS=true) still defaults to --all. --list prints a selection without running it. - BACKEND_UNIT_TEST_FILE_LIST still works and is exclusive with the others. - BACKEND_PYTEST_WORKERS=auto keeps every core in CI; locally it is half the cores, 2 to 8 (measured in backend/tests/README.md). Invalid values fail. - bless-python-backend.sh asks for --all explicitly. - New failure class FC-test-runner-ignores-requested-selection. Co-Authored-By: Claude Opus 5.5 <[email protected]>
select_backend_unit_tests.py answers an empty changed-path list with the full suite. Through --changed that meant a bare `bash test.sh` on a clean branch ran all ~2,975 files, the exact case this runner change exists to stop. The diff was computed and is empty, so select nothing and say how to run more. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ving symlinks Review of #25717 (P2): backend_relative_path resolved only the parent directory, so backend/tests/unit/test_link.py -> /tmp/outside_test.py passed the backend/ containment check and pytest collected the external target. - Resolve the complete path, following file symlinks and chains (portable readlink loop; Bash 3.2, no readlink -f), and require both the link's location and its target to be inside backend/. The error names the target. - Bare-name matches go through the same check. - --changed/--all selector output is checked too: a selected path that reaches outside backend/ through a symlinked file or directory exits 2 before pytest starts. Only builtin -L tests run per path component, so the full selection costs ~0.3 s. - An explicit BACKEND_UNIT_TEST_FILE_LIST keeps naming files anywhere; existing callers pass absolute paths outside backend/. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Member
Author
|
Review finding (P2), fixed in f6ddbad: symlink escape in the path containment check. Found. Fixed (
Tests added to
Run locally (m4-air):
🤖 Generated with Claude Code |
This was referenced Nov 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed and why
backend/test.shignored its positional arguments.bash test.sh test_a.py test_b.pyran the whole unit suite (about 2,970 files), and every run started one worker per core. On 2026-11-19, ten agent lanes on one 18-core host did this at the same time: 48 pytest processes, a load average near 380, and one lane's "two-file" check still running after 56 minutes. This PR makes local runs only as large as they need to be. CI selection and CI worker counts do not change.bash test.sh tests/unit/x.py backend/tests/unit/y.py z.py file.py::test_caseruns exactly those files through the same per-file runner and guards. A path can be backend-relative, repo-relative, relative to the current directory, or a bare file name that is unique undertests/. A path that does not exist, a directory, an ambiguous bare name (the error lists every match), a path outsidebackend/, or an unknown option exits 2, names the argument, and starts nothing. Nothing falls back to the full suite.--changed(optional--base REF, defaultorigin/main). It runs CI's PR selector (select_backend_unit_tests.py --changed-files) on the same diff CI uses: merge-base toHEAD, the samescripts/changed-fileshelper, plus staged, unstaged and untracked files. It keeps the contract-selected hermetic E2E files, because CI passes its PR selection totest.shas an explicit list. For the same change it can select more than CI, never less. If the base ref does not resolve, it exits 2 with the fix. An empty diff selects nothing. The selector itself maps an empty path list to the full suite, and through--changedthat would have made a barebash test.shon a clean branch run everything. I hit this while validating; it is the second commit.--changedlocally,--allin CI (CIorGITHUB_ACTIONSset totrue).--allis the explicit opt-in locally and prints a one-line warning with the file count and the cheaper commands before it starts.--listprints a selection without running it.BACKEND_UNIT_TEST_FILE_LISTbehaves as before. Combining it with file arguments,--changedor--allis an error.BACKEND_PYTEST_WORKERS=<n>is honoured as before. A value that is not a positive integer now fails, where0used to spin forever.auto(the default) is every core in CI. Anywhere else it is half the cores, at least 2 and at most 8. The runner prints the count it chose.backend/scripts/bless-python-backend.shnow asks for--all, the only in-tree caller that relied on the old bare default.backend/AGENTS.mdnow opens its Testing block with the narrow command, and describes the full suite as CI's job and an explicit local opt-in. Also updated:backend/tests/README.md(commands, selection rules, the worker measurement) and.cursor/cloud-agent-environment.md. The agent-doc-claims anchors are unchanged.backend/AGENTS.mdstays within its ratchet: 38,978 of 39,000 bytes, after trimming one sentence.Why the no-argument default became
--changedEvery automated caller already passes an explicit
BACKEND_UNIT_TEST_FILE_LIST, so none of them depends on the bare default:scripts/run-unit-ci.sh, which CI uses for both the PR and the main selections.scripts/pre-push, which passes its selection capped at 40 files and calls that cap a "bounded local-feedback gate, not a CI clone" (Make local pre-push and GitHub CI deterministic and parity-driven #9440).scripts/backend-ratchet-preflight.backend/scripts/run_firestore_index_guard.py.The only bare caller was the bless script, which now passes
--all. The bare default therefore only changes what a person or agent typingbash test.shgets.backend/tests/README.mdalready says to "let CI own full-suite validation". Making--changedthe default gives that command CI's PR selection for the branch. In CI the default stays--all, as a safety net for any out-of-tree bare caller.Worker count measurement
Measured 2026-11-19 on m4-air: 10 cores (4 performance, 6 efficiency) and 32 GB. About 6 cores were already busy with other work, and load averages ran 38-113. The sample was every 74th file of
--all: 40 files and about 156 file-seconds of wall time. Each run went throughtest.shwithBACKEND_UNIT_TEST_FILE_LIST, behindomi-heavy run --no-priority, after a warm-up run.CPU stays within one band (it rises a little with oversubscription), because the worker count does not change the work. It only decides how much of the host one run claims. Oversubscribing (18 workers on 10 cores) was slower than 8. From 8 workers up, the sample's wall time is bounded by its slowest file (26 s). Half the cores, at least 2 and at most 8, comes within a few seconds of the best single-run wall time on this laptop and leaves the other half for other lanes. On the 18-core host, ten lanes now hold 80 worker slots instead of 180. Any admission gate or developer that sets
BACKEND_PYTEST_WORKERSoverrides the default.Per-file start-up cost
A trivial one-test file in
tests/unit/throughbash test.sh <file>with one worker:PYTHONPYCACHEPREFIXset to an empty directory): 0.97 s wall, 0.79 s CPU.pytestwith the runner's plugin flags: 0.37-0.45 s.python -c pass: 0.01 s.import pytest: 0.08-0.12 s.-X importtime: about 83 ms of pytest's own imports (24 ms of that is pygments, pulled in by the terminal writer), 18 ms for pytest-asyncio, and about 10 ms for anyio's plugin.-p no:anyiosaved about 0.03 s, which is within noise.-p no:xdistis not possible, because the conftest uses xdist hooks (exit 3). Fixed start-up is about 0.4 s of the roughly 3 CPU-seconds an average sample file costs, so about 12% of a full run (around 20 of 160 CPU-minutes). No plugin saving was big and safe enough to ship here. The remaining cost is each file importing its production modules.Product invariants affected
none
How it was verified
All local runs were on m4-air, in worktree
lane/backend-test-cheap:bash test.sh test_backend_test_runner_selection.py test_backend_test_runner.py test_backend_unit_ci_runner.py test_fast_unit_duration_guard.py, using the new positional path (bare names): all four files pass (5, 16, 21 and 30 tests). The output showsRunning 4 backend unit test file(s).and the chosen worker count.backend/tests/unit/test_action_items_export_clears_sync.py test_app_update_path_id_authority.py backend/tests/unit/test_chat_messages_pagination.py): 3 files, all pass (3, 5 and 5 tests); auto chose 5 workers on this 10-core host./bin/bash3.2 and Homebrew bash 5.3, using--list: every accepted form, plus missing, ambiguous, directory and outside-backend paths, unknown options, conflicting selections, and--basewith no value.bash test.sh --liston this branch, which defaults to--changed: one changed path,backend/test.sh. The selector answers "backend/test.sh requires the full backend unit suite" and lists 2,974 files:--all's 2,973 plus the contract-selected canonical-memory E2E, the same set CI runs for this diff.CI=true bash test.sh --listgives 2,973, the old default.python3 .github/scripts/check_agents_md_lean.py,check_agent_doc_claims.py,shellcheck -S warning backend/test.sh, andbash -nunder bash 3.2: all clean.omi-heavy run -- git push: all steps passed. It covered the shared PR preflight (42 manifest checks, includingfirestore-index-guard, which runstest.shwith an explicit list) and pyright. The selector asked for the full suite becausetest.shchanged (2,975 files), and pre-push ran the 9 affected contracts and changed tests under its 40-file cap. Full coverage is deferred to CI.bash test.sh test_backend_test_runner_selection.py test_backend_test_runner.pypasses (31 and 16 tests).Not run locally, by design: the full backend unit suite and
make preflightin full. Changingtest.shselects the whole suite in CI's PR job, and CI is the authority for it.Tests
backend/tests/unit/test_backend_test_runner_selection.py, new and hermetic. Each test copies the realtest.shandscripts/changed-filesinto a throwaway Git repository, with a recording selector stub and a fake pytest. It covers:--changedcounts branch commits, staged, unstaged and untracked files, ignores commits that exist only on main, and keeps E2E files the way CI's explicit list does;--changedlocally and--allin CI, with E2E still filtered in CI;--all;0is rejected.The existing runner tests (
test_backend_test_runner.py,test_backend_unit_ci_runner.py) pass unchanged.UI evidence
UI-Evidence: none -- backend test tooling only
Failure class (fixes)
Failure-Class: new
Failure-class transition narrative (only when needed)
Adds
.github/failure-classes/FC-test-runner-ignores-requested-selection.json(familyinput-tolerance). Violated contract: a test runner given an explicit selection must run exactly that selection, or exit nonzero naming the argument it could not use.test.shsilently ran its widest default, the full suite with one worker per core, for arguments it never read. Canonical guard: parse every argument, and reject unresolvable paths, ambiguous names, directories, unknown options and conflicting selections before any selector or pytest process starts. It is proven by the hermetic fixture-repository test above. Evidence: the 2026-11-19 shared-host incident described at the top.evidence_prsis empty because this PR has no number yet. None of the 22 scope-matched classes or theinput-tolerancefamily members states this contract.Follow-ups (not in this PR)
TERM keeps running.
test.sh's TERM trap names the files in flight and then the run continues;test_term_trap_names_files_in_flightasserts exit 0. Undertimeout(or an agent's own deadline) a full run therefore keeps launching files after TERM. Making TERM stop scheduling new files would be a small separate change; I did not change it here because the existing test pins the behaviour.Shared import cost. Each file pays about 3 CPU-seconds, mostly importing production modules again in its own process. Finishing
docs/test_isolation.md(import purity, then a shared session) is the only large saving, up to roughly 85% of suite CPU. Size: large.Fork-server runner. A preloaded interpreter that forks per file would amortise pytest, plugin and conftest start-up: about 0.35 CPU-seconds per file, around 17 CPU-minutes per full run. It is only safe once test modules stop mutating
sys.modulesat import time. Size: medium, with risk.pygments import. pytest's terminal writer imports pygments (24 ms per process, about 1.2 CPU-minutes per full run). Not worth a patch on its own.
Selection cap. Locally,
--changedcan still select the whole suite when a diff touches a full-run input (for exampletest.shitself, or a lockfile). It warns above 100 files. Pre-push already caps that case at 40 files using--focused-output. Atest.sh --changed --focusedmode could reuse that ordering for interactive use.🤖 Generated with Claude Code