Skip to content

ci(unit): guard recurring bug shapes and run only the tests a PR can reach - #5653

Merged
kovtcharov-amd merged 15 commits into
amd:mainfrom
kovtcharov:ci/coverage-gaps-test-selection
Oct 30, 2026
Merged

kovtcharov-amd merged 15 commits into
amd:mainfrom
kovtcharov:ci/coverage-gaps-test-selection

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Of the last 150 bug fixes, 147 shipped a regression test, yet CI caught only about 7 of those bugs before users or RC testing did. Each test guards the one call site that broke, and the next site with the same flaw ships untested. Meanwhile every PR ran all ~27k unit tests on three operating systems, even for a docs edit, and most main-branch runs were cancelled before they finished. This PR guards the recurring bug shapes everywhere, fixes a real bug the new tests found, and makes a PR run only the tests it can affect.

  • Guards for the shapes behind most recent fixes. A ratchet counts subprocess and HTTP calls with no timeout, kill-by-image-name, and except Exception bodies that only pass or return a placeholder. Existing sites are grandfathered, new ones fail CI, and fixing one lowers the count.
  • Folder grants were lost when several GAIA processes saved at once. A four-process test lost 96–98 of 100: the read-modify-write had no lock, and a torn read "rebuilt" the file from empty. It is now locked and atomic. Six runtime calls that could hang forever now time out, with actionable errors.
  • PRs run only the reachable tests. util/select_tests.py combines a nightly per-test coverage map with static rules for what coverage can't see. Windows and macOS run the OS-touching subset of that. The median PR runs about a quarter of the suite. On ~1,150 recent commits, it selected every bug fix's own regression test from the fix's source changes alone.
  • Full coverage moves to main and nightly. Every push to main runs the full suite on Linux and now finishes instead of being cancelled. The nightly run covers all three operating systems and rebuilds the map. The frozen flagship binary also builds on packaging PRs and weekly, not only at an RC.
  • Duplicates trimmed. The email unit lane no longer re-runs ~90 files test_unit.yml already covers, and the warning-only C++ benchmarks run on main instead of every PR.

Also unbreaks main's TUI build: #5464 documented the desktop app's gaia shim in backticks, and the TUI doc check only exempted it before a space, so every PR's TUI checks went red. The shim is the Python CLI's, so the exemption now accepts any punctuation after it.

Trade-offs to know: a Windows- or macOS-only bug outside the OS-touching subset surfaces at the nightly run, not on the PR, and PRs no longer upload partial coverage to Codecov. To run the full suite on a branch, dispatch test_unit.yml on it.

Test plan

  • pytest tests/unit/test_select_tests.py tests/unit/test_process_safety_ratchet.py tests/unit/test_allowed_paths_concurrency.py: 26 + 33 + 1 passed; the concurrency test fails on the old code (96–98 of 100 grants lost)
  • Full tests/unit locally: 27,501 passed; 26 failures that fail identically on main in this environment (PATH-installed gaia, real wheel installs)
  • python util/lint.py --all passes, including the lane-coverage, path-coverage, trigger and ancestor-skip checks
  • Selector replayed over ~1,150 recent commits with a map built from a real --cov-context=test run
  • After merge: the first nightly run saves unit-test-map-*, and the next PR's "Select unit tests" summary reports a map-based selection
🔍 Technical details

…folder grants

Almost every recent fix shipped a regression test, yet CI caught almost none
of the original bugs: each test guarded its own call site, and the next site
with the same shape shipped untested. A ratchet now counts those shapes across
src/gaia and the hub agents (subprocess and HTTP calls with no timeout,
kill-by-image-name, broad excepts that pass or return a placeholder), so the
counts can only go down.

A four-process test found a real bug: folder grants saved by several GAIA
processes at once lost 96-98 of 100, because the read-modify-write had no lock
and a torn read "rebuilt" the file from empty. It is now locked and atomic.

- Bound six runtime subprocess calls, with actionable errors when taskkill or
  the notification sink hangs.
- test_unit.yml no longer cancels a started run on main: 73 of the last 100
  were cancelled part-way, so main rarely got a result.
- The flagship freeze and smoke test also run on PRs that change packaging or
  dependencies and weekly, so a new upstream wheel that drops data or a
  platform (amd#5420, amd#5481, amd#5546) surfaces before an RC.
… main and nightly

Every PR ran all ~27k unit tests on Linux, Windows and macOS, including for a
docs edit. Now util/select_tests.py picks the test files a change can reach,
and a PR runs those. Windows and macOS run the subset of them that touches the
operating system. The median PR runs about a quarter of the suite. Every push
to main still runs the full suite on Linux, and the nightly run covers all
three operating systems and rebuilds the coverage map the selection reads.

The selection combines a nightly per-test coverage map with static rules for
what coverage can't see: module-level imports, patch-target strings, files
tests read, directory scans, conftest fixtures, and CLIs run in a child process.
Deleted or renamed files and packaging or conftest changes run everything.
Over ~1,150 recent commits it selected every bug fix's own regression test from
the fix's source changes alone.

- The email unit lane no longer re-runs ~90 connector and eval test files that
  need nothing it installs.
- C++ benchmarks, which only warn, run on main instead of every PR.
@github-actions github-actions Bot added documentation Documentation changes devops DevOps/infrastructure changes tests Test changes security Security-sensitive changes cpp daemon Daemon supervisor / sidecar control plane sidecar Agent sidecar contract / harness labels Oct 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

PRs will now run only the unit tests their change can reach; the full suite moves to main and a nightly run. The PR also adds a ratchet that stops new no-timeout calls, kill-by-name commands and swallowed exceptions, and fixes folder grants being lost when several GAIA processes saved at the same time. I found no blocking issues. I spot-checked the riskiest part, whether the selector picks the repo-wide scan tests for an ordinary source change. It does: a change to cli.py, a hub agent or a UI router each selects the new ratchet, the URL-prefix guard and the GAIA_HOME ratchet.

Small follow-ups worth doing:

  • The grants file is still read without the lock. The writer is now safe, but on Windows a process that loads grants while another is replacing the file can hit "access denied", log it, and start with no saved grants for that session.
  • The nightly coverage map can go stale on quiet weeks. The cache key is the commit, so a nightly run with no new commits can't save a fresh map, and PRs then get the "map over 3 days old" warning even though nothing changed.
  • Adding any label to a packaging PR rebuilds the frozen binary. It also cancels the build already in progress.

Real-world evidence

N/A: no evidence bundle was produced for this run. The PR is mostly CI and tests. Its runtime edits only add timeouts and a file lock, and no user-facing surface changes. The verdict rests on static review, plus a local run of the selector against a few sample changes.

🔍 Technical details

🟢 Minor

  1. Unlocked reader of allowed_paths.json (src/gaia/security.py:752). _save_persisted_path now holds file_lock and writes atomically, but _load_persisted_paths opens the file bare. gaia/utils/file_lock.py's module docstring says readers must hold the lock too, because on Windows opening mid-rename fails with PermissionError. That error is caught by the outer except Exception and logged, and the process runs with no persisted grants. This isn't a regression (the old torn read failed the same way), but the fix is one line:

    from gaia.utils.file_lock import file_lock
    
    if self.config_file.exists():
        try:
            with file_lock(self.config_file), open(self.config_file, "r", encoding="utf-8") as f:
  2. Map cache key can't refresh on an unchanged SHA (.github/workflows/test_unit.yml, "Save the test coverage map"). key: unit-test-map-${{ github.sha }} collides when two nightly runs see the same main head. cache/save then refuses to overwrite and keeps the old entry's built_at, so after three quiet days every PR gets the ::warning:: from load_map for a map that is still accurate. restore-keys: unit-test-map- already picks the newest entry, so a unique key costs nothing:

              key: unit-test-map-${{ github.sha }}-${{ github.run_id }}
    
  3. labeled trigger on the freeze (.github/workflows/build_flagship_sidecar.yml). It exists so ready_for_ci can start a draft PR's build. Because cancel-in-progress is on for PRs, every other label added (documentation, cpp, …) cancels and restarts the whole multi-OS freeze matrix. Consider gating the job on github.event.action != 'labeled' || github.event.label.name == 'ready_for_ci'.

  4. Leaked workers on timeout (tests/unit/test_allowed_paths_concurrency.py). If one proc.wait(timeout=120) raises, the remaining workers keep running against tmp_path. A try/finally that kills any live procs would keep a hang from orphaning processes, the same shape the new ratchet guards against.

Strengths

  • Fallbacks fail closed: no base commit, a deleted or renamed file, any conftest.py, packaging config, the selector itself, or a selection over 70% all run the full suite. Merge queue, dispatch and nightly are always full, so the release gate keeps full coverage.
  • The new timeouts surface as actionable errors (TimeoutExpired → RuntimeError naming the remedy) rather than being swallowed. The taskkill one aborts daemon start before instance.json is written, which is the right fail-loud shape.
  • The ratchet only goes down: it fails on growth and on an un-lowered count. Its docstring states its blind spots (Popen.wait, Session methods) instead of implying full coverage.

…ore unrelated labels

A process loading folder grants while another replaced the file could hit a
Windows access-denied and start with none; the read now takes the same lock as
the write. The nightly map's cache key now includes the run id, so a night with
no new commits still saves a fresh map instead of tripping the staleness
warning. Only the ready_for_ci label starts a freeze on a PR, so other labels no
longer cancel and restart it. The grants concurrency test kills its workers if
one hangs.
…tes it

amd#5464 documented the desktop app's `~/.local/bin/gaia` shim in backticks, and
the doc check's exemption for that shim only matched it followed by a space, so
the TUI build went red on main and on every open PR. The shim wraps the Python
CLI, so naming it is correct; the exemption now accepts any non-name character
after it, the same boundary the check already uses for `bin/gaia`.
@github-actions github-actions Bot added the tui Go terminal UI (gaia-tui) label Oct 23, 2026
# Conflicts:
#	tui/test/hub_vocabulary_test.go
After merging main, the process-safety ratchet failed on four new calls with
no timeout: three git calls in the scorecard gate and the build-time tiktoken
download. A hung git now raises an actionable error instead of hanging the
release gate (and is never read as "first adoption"), and a hung download
fails the freeze naming the host to check. A cli_agent.py swallowed-exception
site main removed is dropped from the allowlist so it can't return.
The coverage map is a nightly snapshot, and the selector trusted it even where
it had no record, so a PR could leave out tests it actually reaches: a test
added since the last nightly run, a source file the map never saw, every file
once the map stopped refreshing, and code that GAIA itself runs in a child
process (`python -m gaia.daemon`), which coverage can't see into. Each of
those now falls back to following every import, as with no map at all, and a
spawned module selects the tests that ran the file that spawns it. More tests
run in those cases, never fewer.
@github-actions github-actions Bot added eval Evaluation framework changes performance Performance-critical changes labels Oct 29, 2026
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

The test selection could leave out tests a PR actually reaches wherever the nightly coverage map had no record. It now runs more tests in those cases, never fewer.

  • Tests added since the last nightly run were judged only by their top-level imports, so one that reached the changed code through a function-level import was skipped.
  • Source files the map never saw (a new module, or one no test had run) had the same gap.
  • A map more than three days old only printed a warning. It now also follows every import.
  • Code GAIA starts in a child process (python -m gaia.daemon from the daemon client) was invisible to coverage. Tests that run the spawning file are now selected.

I also merged main, which needed two more fixes. The TUI doc-check conflict resolved to main's version, which already covered the same gaia shim. Main had also added four calls with no timeout that the new ratchet flagged: three git calls in the scorecard gate and the tiktoken download in the freeze. Those calls now time out with actionable errors.

🔍 Technical details
  • affected_files computes a second closure over module-level, function-level and spawn edges, the no-map rule. It adds every test the map never credited, plus that closure from any changed .py the map has no entry for. With a stale map it adds the whole closure.
  • Package code that matches _SPAWN_RE now records the dotted gaia.* modules it names as spawn_deps. The existing subprocess-target pass covers them and adds coverage[spawner].
  • load_map now returns (files, stale).
  • Each new case in test_select_tests.py fails on the previous selector. The case where a mapped test reaches nothing still stays out, so the selection stays tight.
  • The merge queue hasn't run test_unit.yml since March, so the PR selection is the last check before merge. That's why "unsure means run more" matters here.

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Test selection still skips tests that read a skill file by its full path. A PR that edits hub/skills/inbox-triage/SKILL.md today runs the full suite. With this change it skips the two tests that check that exact file, so a wrong tools_required entry would pass the PR and fail on main. The merge queue no longer runs unit tests, so nothing catches it before merge.

Next step: if a test names a non-Python file by any path, that file should select the test even when its basename is common (SKILL.md, README.md, CHANGELOG.md). Failing that, any change to a non-Python file nobody can attribute should run the full suite. Please add a regression case for this file.

🔍 Technical details
  • Repro at c6f9228, no map: python util/select_tests.py select --changed hub/skills/inbox-triage/SKILL.md --explain selects 41 files but leaves out tests/unit/agents/tools/test_email_tools.py (_inbox_triage_skill, line 834, which feeds test_skill_tools_required_match_the_registered_tools) and tests/unit/agents/tools/test_email_phishing_guard.py:271. The map can't help here because coverage only records .py files.
  • Cause: in build_graph, only the last path piece of a string becomes a read_deps edge. "SKILL.md" has more than MAX_BASENAME_FANOUT (25) matches, so it's dropped. "README.md" is in _IGNORED_BASENAMES and is never linked at all. Neither test lists a directory, so the scanner-token rule doesn't apply. Matching on the joined path (hub/skills/inbox-triage/SKILL.md, or the "hub" / "skills" / "inbox-triage" / "SKILL.md" pieces taken together) would resolve to exactly one file.
  • Checked and handled correctly: deleted and renamed files (--no-renames plus the exists() check), every conftest.py, root setup.py/pyproject.toml, the hub agents' pyproject.toml (the selection goes over 70%, so the full suite runs), test_unit.yml, and uv.lock (CI doesn't install from it).
  • The two red checks are infrastructure: C++ STX passed on rerun (the runner was missing cc1.exe), and the TUI "Fetch the desktop app setup" step fails with the same release not found on other PRs.

kovtcharov and others added 4 commits October 30, 2026 08:19
A PR editing hub/skills/inbox-triage/SKILL.md skipped the two tests that check
that exact file, so a wrong tools_required entry would pass the PR and fail on
main. Only the last piece of a path counted, and SKILL.md is too common to mean
anything alone; README.md was ignored outright. Coverage records only .py
files, so the map couldn't help.

Paths built with /, joinpath or os.path.join are now joined and matched whole.
A non-Python name with no folder means every file of that name, however common.
They excused the flagship agent files for a test the email unit lane used to
collect from tests/unit/eval. This PR removed that directory from the lane, so
the entries excuse nothing and the path-coverage check failed.
Matching full paths fixed the inbox-triage case, but any data file a test
reads without naming it (through a variable folder, a bare common name like
SKILL.md, or a directory scan) still got a guessed, partial selection, and
coverage can't help because it records only .py files. A changed non-Python
file now selects precisely only when some string names that one file;
otherwise the whole suite runs.
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

A data file now selects tests only when some test names that exact file. Otherwise the PR runs the full suite. Editing hub/skills/inbox-triage/SKILL.md selects both email tests that read it.

  • The fallback covers a common name like SKILL.md, a folder held in a variable, and a directory scan. Coverage can't help in those cases because it only records .py files.
  • New regression and adversarial cases cover a root README.md, a JSON fixture whose name is shared, a .mdx doc, a YAML scenario, a file under references/, and a renamed data file. Each case selects its reader when a test names the file, and runs the full suite when none does. Each full-suite case fails on the previous selector.

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Test selection still skips a test when it loads a script by file path and the change is to something that script imports or reads. The SKILL.md case is fixed, but editing eval/ci_lanes.json would pass this PR while breaking a test on main.

Next step: treat a module a test loads by path (spec_from_file_location, runpy, python path/to/script.py) like an import, so changes to that script's own imports and data files reach the test. Failing that, run the full suite for any change such a script reaches. Please add regression cases for the examples below.

🔍 Technical details
  • Found by running all of tests/unit at a9c4f4c with an audit hook that logged which tracked non-Python files each test file opened, then running select() on each of those 1,925 files alone. 1,717 ran the full suite and 203 selected every test that opened them. The rest were the cases below, plus a session-fixture artifact (vendor_corpus_seed.jsonl) that isn't a real gap.
  • Cause: a test that runs importlib.util.spec_from_file_location(..., REPO_ROOT / "util" / "run_eval_lane.py") gets only a read_deps edge to that script. read_deps is followed one hop from the changed file and is not part of the deps closure. The script's own imports and reads are therefore never connected to the test. The coverage map can't fill the gap for util/ or tests/fixtures/ scripts, because CI measures only src/gaia (plus hub/agents nightly).
  • Reproduced with select(), both with no map and with a map that records only direct hits:
    • eval/ci_lanes.json selects 13 tests but misses tests/unit/eval/test_run_eval_lane.py. That test asserts lane_categories() for every lane in the file.
    • util/eval_lane_matrix.py and util/eval_gaia_home.py each miss test_run_eval_lane.py. run_eval_lane.plan() calls both.
    • tests/fixtures/email/phishing_fixture.json misses tests/unit/agents/tools/test_email_eval_fixture.py, which loads tests/fixtures/gaia/email/build_mailbox.py by path, and that script reads the JSON.
    • With no map, the same shape also misses drafting_gate_thresholds.json → test_release_scorecard.py and perf_gate_thresholds.json → test_gaia_scorecard_adapter.py. The nightly map may catch these two, because the loaded scripts sit under covered paths.
  • Checked and correct: the inbox-triage SKILL.md (both email tests selected), a root README.md, an eval scenario YAML, a non-test workflow, a renamed JSON fixture, a deleted helper, every conftest and pyproject.toml (all full suite), a webui .tsx, and a module reached only through python -m gaia.cli. CI is green at a9c4f4c, and the earlier bot-review items are fixed.

Path-by-path rules kept missing tests that load a script by file path: editing
eval/ci_lanes.json, util/eval_lane_matrix.py or a fixture that
build_mailbox.py reads skipped the test that loads that script, because
neither imports nor coverage follow what such a script imports or reads.

Narrowing is now an allowlist. A PR runs a subset only when every changed file
is a unit test, or modified .py code under src/gaia (or hub/agents when the
map measured it) that has an entry in the coverage map. Everything else runs
the full suite, as does a missing or stale map. About one in five recent
merges would still get a narrowed run.
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Unit tests are now narrowed only by an allowlist. A PR runs a subset only when every file it changes is a unit test, or modified Python under src/gaia (or a hub agent package, when the map measured it) that the coverage map has an entry for. Every other change runs the full suite. That includes data files, util/ scripts, test helpers and fixtures, eval/, configs, workflows, new source files, and any run with a missing or stale map.

  • About one in five of the last 600 merges to main would still get a narrowed run.
  • The regression tests cover the four cases you reported, plus drafting_gate_thresholds.json, perf_gate_thresholds.json, a util/ script and a non-test helper under tests/. Each runs the full suite.
  • The path matching from earlier rounds stays, but it now only adds tests to a narrowed run. It never decides whether a data-file change narrows.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 30, 2026
Merged via the queue into amd:main with commit 4eb6aab Oct 30, 2026
128 of 131 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the ci/coverage-gaps-test-selection branch October 30, 2026 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cpp daemon Daemon supervisor / sidecar control plane devops DevOps/infrastructure changes documentation Documentation changes eval Evaluation framework changes performance Performance-critical changes security Security-sensitive changes sidecar Agent sidecar contract / harness tests Test changes tui Go terminal UI (gaia-tui)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants