Skip to content

fix(backend-tests): test.sh runs the files it is given and bounds local workers - #25717

Merged
Git-on-my-level merged 3 commits into
mainfrom
lane/backend-test-cheap
Nov 19, 2026
Merged

Git-on-my-level merged 3 commits into
mainfrom
lane/backend-test-cheap

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

What changed and why

backend/test.sh ignored its positional arguments. bash test.sh test_a.py test_b.py ran 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.

  • Named files. bash test.sh tests/unit/x.py backend/tests/unit/y.py z.py file.py::test_case runs 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 under tests/. A path that does not exist, a directory, an ambiguous bare name (the error lists every match), a path outside backend/, or an unknown option exits 2, names the argument, and starts nothing. Nothing falls back to the full suite.
  • --changed (optional --base REF, default origin/main). It runs CI's PR selector (select_backend_unit_tests.py --changed-files) on the same diff CI uses: merge-base to HEAD, the same scripts/changed-files helper, plus staged, unstaged and untracked files. It keeps the contract-selected hermetic E2E files, because CI passes its PR selection to test.sh as 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 --changed that would have made a bare bash test.sh on a clean branch run everything. I hit this while validating; it is the second commit.
  • Default with no arguments: --changed locally, --all in CI (CI or GITHUB_ACTIONS set to true). --all is the explicit opt-in locally and prints a one-line warning with the file count and the cheaper commands before it starts. --list prints a selection without running it.
  • BACKEND_UNIT_TEST_FILE_LIST behaves as before. Combining it with file arguments, --changed or --all is an error.
  • Workers. BACKEND_PYTEST_WORKERS=<n> is honoured as before. A value that is not a positive integer now fails, where 0 used 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.sh now asks for --all, the only in-tree caller that relied on the old bare default.
  • Docs: backend/AGENTS.md now 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.md stays within its ratchet: 38,978 of 39,000 bytes, after trimming one sentence.

Why the no-argument default became --changed

Every 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 typing bash test.sh gets. backend/tests/README.md already says to "let CI own full-suite validation". Making --changed the 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 through test.sh with BACKEND_UNIT_TEST_FILE_LIST, behind omi-heavy run --no-priority, after a warm-up run.

workers wall (s), per round user + sys CPU (s)
4 48.4, 34.6, 33.9, 35.9 145, 119, 117, 122
5 (half of 10) 30.7, 32.5 128, 133
8 29.9, 26.2, 30.1, 28.4 143, 136, 141, 142
18 (old default on an 18-core host) 35.4, 30.0, 35.5, 30.5 147, 140, 148, 143

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_WORKERS overrides the default.

Per-file start-up cost

A trivial one-test file in tests/unit/ through bash test.sh <file> with one worker:

  • Cold bytecode (PYTHONPYCACHEPREFIX set to an empty directory): 0.97 s wall, 0.79 s CPU.
  • Warm, five runs: 0.38-0.53 s wall, 0.35-0.49 s CPU.
  • Bare pytest with 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:anyio saved about 0.03 s, which is within noise. -p no:xdist is 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 shows Running 4 backend unit test file(s). and the chosen worker count.
  • Real product unit files from the repository root via mixed path forms (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.
  • Argument edge cases checked by hand under /bin/bash 3.2 and Homebrew bash 5.3, using --list: every accepted form, plus missing, ambiguous, directory and outside-backend paths, unknown options, conflicting selections, and --base with no value.
  • bash test.sh --list on 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 --list gives 2,973, the old default.
  • python3 .github/scripts/check_agents_md_lean.py, check_agent_doc_claims.py, shellcheck -S warning backend/test.sh, and bash -n under bash 3.2: all clean.
  • Pre-push hook (bounded; not the full suite) on the first commit, via omi-heavy run -- git push: all steps passed. It covered the shared PR preflight (42 manifest checks, including firestore-index-guard, which runs test.sh with an explicit list) and pyright. The selector asked for the full suite because test.sh changed (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.
  • After the empty-diff fix: bash test.sh test_backend_test_runner_selection.py test_backend_test_runner.py passes (31 and 16 tests).

Not run locally, by design: the full backend unit suite and make preflight in full. Changing test.sh selects 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 real test.sh and scripts/changed-files into a throwaway Git repository, with a recording selector stub and a fake pytest. It covers:

  • every accepted path form, with named files never consulting the selector;
  • every rejected argument exits 2 and starts nothing (this is the regression test for the ignored arguments);
  • --changed counts 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;
  • an unresolvable base fails instead of widening, and an empty diff selects nothing;
  • the default is --changed locally and --all in CI, with E2E still filtered in CI;
  • the warning on a local --all;
  • worker resolution: half the cores, at least 2 and at most 8 locally, every core in CI, explicit values win, 0 is 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 (family input-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.sh silently 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_prs is empty because this PR has no number yet. None of the 22 scope-matched classes or the input-tolerance family 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_flight asserts exit 0. Under timeout (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.modules at 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, --changed can still select the whole suite when a diff touches a full-run input (for example test.sh itself, or a lockfile). It warns above 100 files. Pre-push already caps that case at 40 files using --focused-output. A test.sh --changed --focused mode could reuse that ordering for interactive use.

🤖 Generated with Claude Code

View guided diff Turn on auto-fix

Git-on-my-level and others added 3 commits November 19, 2026 02:41
…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]>
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Review finding (P2), fixed in f6ddbad: symlink escape in the path containment check.

Found. backend_relative_path resolved only the parent directory with pwd -P, then appended the file name. A symlinked test file such as backend/tests/unit/test_link.py -> /tmp/outside_test.py passed the backend/ check, and pytest then collected the external target.

Fixed (backend/test.sh).

  • Named paths: the full path is now resolved before the containment check, following file symlinks and symlink chains. This uses a readlink loop with a 40-hop limit, because Bash 3.2 and macOS have no readlink -f. Both the link's location and its final target must be inside backend/, and the error names the resolved target.
  • Bare names: a bare-name match goes through the same check. find -type f already skipped symlinks, so this is defence in depth.
  • --changed / --all: the selector's output had the same gap, through a symlinked file or a symlinked directory. Any selected path that resolves outside backend/ now makes the run exit 2 before pytest starts. Each path component gets only the builtin -L test, and a path is resolved only when a link is found. Checking the full 2,974-file selection costs about 0.3 s.
  • BACKEND_UNIT_TEST_FILE_LIST is unchanged: an explicit list may still name files anywhere. Existing callers and tests pass absolute paths outside backend/, and a new test pins that behaviour.
  • No tracked symlinks exist under backend/ (git ls-files -s backend, mode 120000), so CI's selection is unchanged.

Tests added to backend/tests/unit/test_backend_test_runner_selection.py, all hermetic. Each case asserts exit 2 and that pytest never starts, except where noted:

  • An outside file symlink rejected in three forms: tests/unit/..., backend/tests/unit/..., and via a symlinked directory.
  • A two-hop chain, where the error names the resolved target.
  • A bare name pointing at an outside symlink.
  • A symlink that stays inside backend/ still runs.
  • --changed and --all selector output that escapes through a symlinked file or directory.
  • An explicit list naming an outside file still works.

Run locally (m4-air):

  • New file directly: 42 passed under Homebrew bash 5.3, and again with bash resolved to /bin/bash 3.2.
  • bash test.sh test_backend_test_runner_selection.py test_backend_test_runner.py test_backend_unit_ci_runner.py: 42, 16 and 21 passed.
  • shellcheck -S warning, bash -n (3.2), agent-doc-claims and agents-md-lean: all clean.
  • Bounded pre-push through omi-heavy run -- git push, with no escape hatch: passed. Pyright ran, the manifest checks ran, and 9 affected/changed test files ran under the 40-file cap; full-suite coverage is CI's.

🤖 Generated with Claude Code

@Git-on-my-level
Git-on-my-level merged commit 7437adc into main Nov 19, 2026
63 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/backend-test-cheap branch November 19, 2026 04:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant