Skip to content

Keep tests from writing into the committed publications/ and reactome/ caches - #5025

Merged
cmungall merged 4 commits into
mainfrom
claude/confident-shannon-vfz0tz
Oct 16, 2026
Merged

cmungall merged 4 commits into
mainfrom
claude/confident-shannon-vfz0tz

Conversation

@cmungall

@cmungall cmungall commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Summary

  • test_fetch_pubmed_data (integration) called fetch_pubmed_data(pmid) with the default cache dir. Every just test-full / pytest -m integration run, cassette replay included, rewrote the committed publications/PMID_29727692.md. It now caches to tmp_path.
  • GOAValidator.seed_missing_annotations hard-coded Path("publications") and Path("reactome") for title fetches. As a result, fetch_gene_data(base_path=tmp) still cached PMIDs and Reactome entries into the working directory, which happens when cassettes are recorded.
    • It now takes publications_dir and reactome_dir.
    • fetch_gene_data and fetch_gene_data_ncRNA pass base_path / "publications" and base_path / "reactome".
    • CLI behaviour is unchanged, because base_path defaults to the working directory.
  • A root conftest.py guards the committed data:
    • It snapshots git status of publications/, genes/ and reactome/ (untracked files included) when a session starts.
    • If a passing run changed them, it fails the run and lists the files in the terminal summary.
    • It sits at the root so it also covers pytest --doctest-modules src.
    • It costs about 0.25s per session.
  • .linkml-reference-validator.yaml now sets trust_cached_entries: true. Upstream's CLI loads this file when run without --config, so a bare linkml-reference-validator run from the repo root could still re-fetch and rewrite reviewed cache entries. conf/reference_validator_config.yaml already sets it.

Replaces the remaining part of the closed #4552, whose LRV upgrade and backport removal are already on main.

Validation

n/a (no gene-review changes)

Test plan

  • Full suite at 67cee26: 3785 passed, 73 failed. All 73 also fail on a clean main checkout in this container (just missing, shallow-clone snapshot commits). The protected trees were unchanged afterwards.
  • At 8d437d1, the affected test files (goa_validator, gene_etl, publication_etl, gene_alternative_products_refresh, reference_cache_behavior, validator) give 175 passed.
  • pytest -m integration --vcr-record=none tests/test_publication_etl.py passes and leaves the working tree clean.
  • test_seed_missing_annotations_uses_given_cache_dirs fails without the parameters: it picks up the committed PMID_12345.md and R-CEL-9856612.md titles.
  • test_bare_cli_default_config_trusts_reviewed_cache fails without the config line.
  • Guard probes exit 1 and name the file in two cases: a --doctest-modules run outside tests/ that writes publications/PMID_*.md, and a test outside tests/ that writes reactome/R-*.md. A clean run exits 0.
  • ruff check and mypy clean on changed files.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SfBE3uVPNyHBBofmRvNakd

- test_fetch_pubmed_data cached PMID_29727692.md into the repo's
  publications/ on every integration run (cassette replay included);
  it now caches to tmp_path.
- seed_missing_annotations hard-coded Path("publications") for title
  fetches, so fetch_gene_data(base_path=tmp) still wrote publications
  into the working directory. It takes publications_dir, and
  fetch_gene_data passes base_path / "publications" (unchanged for the
  CLI, where base_path is the working directory).
- conftest snapshots git status of publications/ and genes/ at session
  start and fails the run if any test changed them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SfBE3uVPNyHBBofmRvNakd
The BAG6 refresh test replaces seed_missing_annotations with a stub that
did not accept the new keyword. It now records publications_dir and
asserts fetch_gene_data passes base_path / "publications".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SfBE3uVPNyHBBofmRvNakd
A bare `linkml-reference-validator` run from the repo root (no --config)
loads .linkml-reference-validator.yaml, which did not set
trust_cached_entries, so such a run could still re-fetch and rewrite
committed publications/ entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SfBE3uVPNyHBBofmRvNakd
@ai4c-reviewer

ai4c-reviewer Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Claude finished @cmungall's task in 5m 32s —— View job


Review: PR #5025 — keep tests out of the committed caches

  • Schema validation passes — n/a (no gene-review YAML changed)
  • All PMIDs verified as real — n/a
  • Supporting text matches publication content — n/a
  • GO terms correct and specific — n/a
  • Review actions well-justified — n/a
  • Core functions accurately identified — n/a
  • No over-annotation issues missed — n/a
  • Test-infrastructure correctness reviewed (statically — see note at the end)

This is a test-hygiene / ETL-plumbing PR. The diagnosis is right and the fix is the right shape: a cwd-relative cache default is exactly how base_path=tmp_path leaked into the committed tree. A few things I verified hold up:

  • base_path is normalized to Path.cwd() at src/ai_gene_review/etl/gene.py:455 and :1656, so base_path / "publications" can never TypeError and the "CLI behaviour unchanged" claim is genuinely true.
  • The new test is not vacuous: get_cached_title (etl/publication.py:460-470) parses the first # heading, so the fixture title is really what comes back, and without the parameter the committed publications/PMID_12345.md title would surface. The sorted(publications_dir.iterdir()) == [...] assertion also pins "nothing else was written" — nice touch.
  • git status --untracked-files=all excludes ignored files, so git-ignored build artifacts (.cache/panther/...) can't trip the guard. Good choice.
  • Mutating session.exitstatus inside pytest_sessionfinish does take effect — wrap_session returns session.exitstatus after firing the hook.
  • The monkeypatched seed stub in tests/test_gene_alternative_products_refresh.py was correctly updated for the new kwarg, and asserts the value rather than just tolerating it.

🟡 IMPORTANT — the sibling leak two lines away is still cwd-bound, and the new guard can't see it

src/ai_gene_review/validation/goa_validator.py:1146:

# Use reactome directory for cache
cache_dir = Path("reactome")

This is inside _get_reactome_title, called from goa_validator.py:927 — in the same fetch_titles block whose PMID branch this PR just parameterized. fetch_reactome_data then writes cache_dir / f"{reactome_id}.md" (etl/reactome.py:184-190), creating files in the repo's committed reactome/ tree (8365 tracked files) regardless of base_path.

It's reachable from the exact tests this PR is cleaning up: tests/test_gene_etl.py:36 calls fetch_gene_data(..., base_path=base_path) with fetch_titles defaulting to True, and the recorded cassettes for those cases (test_fetch_gene_data_creates_correct_directory_structure[human-BRCA1-P38398], [human-TP53-P04637], test_fetch_gene_data_different_organisms[mouse-Trp53]) contain Reactome:R- references. Today most of those ids are already cached so get_cached_title short-circuits; the first GOA refresh or cassette re-record that introduces an uncached R-HSA-* writes into reactome/.

And because PROTECTED_TREES = ("publications", "genes") omits reactome, the new tripwire would not report it — so this is the one case where the PR's guard gives false reassurance rather than just incomplete coverage. Two small changes close it: thread a reactome_dir the same way publications_dir was threaded, and add "reactome" to PROTECTED_TREES. (Fix this →)

If you'd rather land this PR as-is and do the Reactome half as a follow-up, adding "reactome" to PROTECTED_TREES now would at least make the leak visible instead of silent — that alone would drop this to a 🔵 for me.


🔵 SUGGESTION — the guard is inert for just doctest

just test = pytest doctest mypy format, and just doctest runs uv run pytest --doctest-modules src. With src as the only arg, tests/conftest.py is not an initial conftest (pytest only auto-loads conftests from the args' ancestors plus their test* subdirs), so pytest_sessionstart never fires and before is None short-circuits the check.

The same mechanism is what makes the guard work for just pytest-integration (pytest -m integration --vcr-record=none, no path arg): the root anchor's test* glob picks up tests/, which is also why --vcr-record resolves there at all. So the behaviour is load-bearing and currently correct for the two suite runs — just absent for the doctest session, which does execute src code with those cwd-relative defaults. Moving pytest_sessionstart/pytest_sessionfinish into a root-level conftest.py would cover every invocation uniformly, since the rootdir conftest is always initial.

🔵 SUGGESTION — don't clobber more specific exit codes

tests/conftest.py:67 sets TESTS_FAILED unconditionally, overwriting INTERRUPTED (Ctrl-C), USAGE_ERROR, and NO_TESTS_COLLECTED. Those are more informative than "tests failed", and the tree change is usually a consequence of the aborted run rather than the headline. Guarding it reads better:

if session.exitstatus == pytest.ExitCode.OK:
    session.exitstatus = pytest.ExitCode.TESTS_FAILED

🔵 SUGGESTION — the red message lands above the summary, so the last line still reads green

TerminalReporter.pytest_sessionfinish is a hookwrapper that prints the summary sections after the inner impls run, so reporter.write_line(...) here is emitted before === 3785 passed ===. In a CI log the final line reads green while the exit code is 1, and the explanation is a scroll away. A pytest_terminal_summary impl (or an entry appended there) would put it at the bottom where a failing run is read from.

Also, it uses exitstatus from the hook argument rather than session.exitstatus, so your mutation won't retroactively add a "short test summary" section — worth knowing if you ever expect one.

🔵 SUGGESTION — note the comparison's blind spot in the docstring

The check diffs sets of porcelain lines, which carry no content hash. Two consequences worth a sentence in _protected_tree_status's docstring:

  • False negative: a test that edits a file already showing as M before the run produces an identical status line, so the write is invisible. (The changed or ["(status changed for already-modified files)"] fallback suggests you already had this in mind.)
  • False positive: an editor save or a concurrent just fetch-gene during a long run attributes the change to the tests.

Neither undermines the guard — it's aimed at new PMID_*.md files, which it catches precisely — but the next person to hit the false negative will want the hint.

🔵 SUGGESTION — seed-goa CLI still takes the cwd default

src/ai_gene_review/cli.py:1534 and :1552 call seed_missing_annotations without publications_dir, so with --output pointing outside the repo, titles still cache into the cwd. Pre-existing and arguably correct for a repo-root invocation, but it's now one keyword away from being explicit (e.g. derived from yaml_file's repo root), which would remove the last cwd dependence from this path.


On the config and dependency coupling

.linkml-reference-validator.yaml gaining trust_cached_entries: true is right and the test is the good kind — asserting the loader's resolved behaviour rather than re-reading the YAML, which is the only version that would have caught the bug. It does couple to linkml_reference_validator.cli.shared.load_validation_config, a fairly internal entry point; that's the correct trade here (it fails loudly on an upstream rename rather than silently passing), just worth knowing it's a deliberate coupling.

Note on verification

I reviewed this statically: there's no uv or virtualenv in this container and sandbox restrictions blocked probing for an interpreter, so I could not execute the suite, the two new tests, or ruff. Everything above is from reading the code, the cassettes, pytest.ini and the justfile; I've tried to state the evidence for each claim so you can check my reasoning rather than my word. Your own test-plan results (and CI) are the authority on pass/fail.

Requesting changes on the 🟡 only — the rest are take-or-leave.
· branch claude/confident-shannon-vfz0tz

@ai4c-reviewer ai4c-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test-hygiene/plumbing PR; no gene-review YAML changed, so the GO/evidence checklist is n/a.

The diagnosis and fix shape are right, and I verified the load-bearing parts: base_path is normalized to Path.cwd() (gene.py:455, :1656) so the CLI is genuinely unchanged; the new seeding test is non-vacuous (get_cached_title parses the first # heading, and the iterdir() assertion pins that nothing extra was written); --untracked-files=all correctly excludes git-ignored artifacts; and mutating session.exitstatus in pytest_sessionfinish does take effect.

One 🟡 to close: goa_validator.py:1146 still hardcodes cache_dir = Path("reactome") inside _get_reactome_title, called from line 927 — the same fetch_titles block whose PMID branch this PR just parameterized. It writes into the committed reactome/ tree regardless of base_path, and it is reachable from the very tests being cleaned up (test_fetch_gene_data_* pass base_path=tmp with fetch_titles defaulting True, and their cassettes carry Reactome:R- refs). Because PROTECTED_TREES omits reactome, the new tripwire would not report it — so the guard currently gives false reassurance there rather than merely incomplete coverage. Threading a reactome_dir the same way plus adding "reactome" to PROTECTED_TREES closes it; adding just the PROTECTED_TREES entry now and doing the plumbing as a follow-up would be enough to make it visible instead of silent.

Five 🔵s in the full comment, all take-or-leave: the guard is inert for just doctest (pytest --doctest-modules src doesn't load tests/conftest.py as an initial conftest, so pytest_sessionstart never fires — a root-level conftest would cover every invocation); the unconditional TESTS_FAILED clobbers INTERRUPTED/USAGE_ERROR/NO_TESTS_COLLECTED; the red message prints above the terminal summary so the last line still reads green on a tripped run; the porcelain set-diff can't see content edits to already-dirty files; and seed-goa in cli.py:1534/1552 still takes the cwd default.

Note: no uv/virtualenv in this container, so this is a static review — I could not run the suite, the new tests, or ruff. Evidence for each claim is cited in the full comment.

See #5025 (comment) for details.

Review follow-ups on the test-isolation change:

- _get_reactome_title hard-coded Path("reactome"), so fetch_gene_data
  with a tmp base_path could still write reactome/R-*.md into the repo.
  seed_missing_annotations takes reactome_dir, and fetch_gene_data
  passes base_path / "reactome" (unchanged for the CLI).
- Move the protected-tree guard to a root conftest.py so it also covers
  invocations that never load tests/conftest.py, such as
  `pytest --doctest-modules src`, and protect reactome/ as well.
- Only turn an otherwise passing run into a failure, keeping
  interrupted / usage-error / no-tests exit codes, and report the
  changed files in the terminal summary.
- Document that the status comparison cannot see further edits to an
  already-modified file and attributes concurrent edits to the tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SfBE3uVPNyHBBofmRvNakd

Copy link
Copy Markdown
Member Author

Addressed in 8d437d1.

🟡 Reactome cache

  • seed_missing_annotations now takes reactome_dir and passes it to _get_reactome_title, which no longer hard-codes Path("reactome").
  • fetch_gene_data and fetch_gene_data_ncRNA pass base_path / "reactome".
  • reactome is now in PROTECTED_TREES.
  • The seeding test now also seeds Reactome:R-CEL-9856612, which exists in the committed reactome/ with a different title. Without reactome_dir the test fails with the committed title ("ATFS-1 translocates…").

🔵 Taken

  • The guard moved to a root conftest.py, and tests/conftest.py is back to main's version. Probe tests confirm it now fails a --doctest-modules run on a module outside tests/ that writes publications/PMID_*.md, and a run outside tests/ that writes reactome/R-*.md. Both exit 1 and name the file. A clean run exits 0.
  • It only changes ·O·K t·o ·T·ESTS_FAILED, so interrupted, usage-error and no-tests exit codes are kept.
  • The file list is printed from pytest_terminal_summary as a red section in the summary. The final N passed line is still pytest's own, so a tripped run ends with that line above an exit code of 1.
  • The docstring now describes the porcelain blind spot (further edits to an already-dirty file) and the false positive (concurrent edits).

🔵 Not taken: seed-goa CLI default. That command runs from the repo root and is meant to cache into the repo's publications/ and reactome/. Deriving the directory from yaml_file or --output would change where real curation runs write, and no test path reaches it. I'm leaving it on the working-directory default.

Checks: the affected test files pass (175 passed); pytest -m integration --vcr-record=none tests/test_publication_etl.py passes and leaves the tree clean; ruff and mypy are clean on the changed files.


Generated by Claude Code

@ai4c-reviewer

ai4c-reviewer Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Claude finished @cmungall's task in 3m 4s —— View job


Review: PR #5025 — follow-up pass on 8d437d1a8

  • 🟡 Reactome leak — closed
  • Root conftest.py move — verified
  • Exit-code / terminal-summary changes — verified
  • Updated tests non-vacuous — verified
  • Scan for sibling leaks of the same shape

The 🟡 is resolved and four of the five 🔵s were taken. Nothing blocking left. What I checked:

Reactome threading is complete. _get_reactome_title now takes cache_dir (goa_validator.py:1131) with no internal default, and the only call site passes it (:927). fetch_gene_data and fetch_gene_data_ncRNA pass base_path / "reactome" (etl/gene.py:630, :1819), and base_path is still normalized to Path.cwd(), so the CLI path is unchanged. I grepped for other callers — cli.py:1534/:1552 are the only ones left on the cwd default, which is the case you deliberately kept.

The new assertion is load-bearing. reactome/R-CEL-9856612.md is committed with # ATFS-1 translocates from the cytosol to the mitochondrial matrix, and get_cached_title (etl/reactome.py:104-112) returns the first # heading — so without reactome_dir the test gets that title instead of Fixture-only pathway title. The sorted(reactome_dir.iterdir()) assertion pins that nothing extra was written, mirroring the publications one. The only fetch_titles=True call in the suite is this test; everything else reaches it through fetch_gene_data, which now threads both dirs — so the three test_fetch_gene_data_* cassettes carrying Reactome:R- refs are covered.

The guard move is right. A rootdir conftest.py is always an initial conftest (pytest walks args' ancestors up to confcutdir, which defaults to rootdir), so pytest --doctest-modules src does load it — your probe result matches the mechanism. tests/conftest.py is byte-identical to main again. session.exitstatus == pytest.ExitCode.OK works on the raw int too (ExitCode is an IntEnum), so interrupted/usage-error/no-tests outcomes survive. And pytest_terminal_summary does fire here: TerminalReporter.pytest_sessionfinish gates on its exitstatus argument being in summary_exit_codes, and OK is in that set — the mutation happening in a non-wrapper pytest_sessionfinish means the stash is populated before the wrapper's post-yield runs. No test writes to the real reactome/, so adding it to PROTECTED_TREES shouldn't false-trip.


🔵 SUGGESTION — gocams/ is the same shape, one line from being covered

etl/gocam.py:37 has DEFAULT_CACHE_DIR = Path("gocams"), and cache_gocam_model writes gocams/<id>/<id>-src.yaml into a 2-tree of committed models. Nothing leaks today — tests/test_gocam_etl.py:191/:201 both pass cache_dir=tmp_path, and the CLI default is an explicit option. But that is exactly the position reactome/ was in before the cassettes grew an uncached id. rules/arba and rules/unirule (etl/arba.py:29, etl/unirule.py:41) are the same. Adding "gocams" to PROTECTED_TREES costs nothing and converts a future regression from silent to named. (Fix this →)

🔵 SUGGESTION — hoist the two cwd fallbacks out of the loop

goa_validator.py:903-907 and :930-934 resolve publications_dir/reactome_dir against their cwd defaults inside the per-reference loop. Resolving both once at the top of seed_missing_annotations would read better and make "the default is the working directory" a single visible statement rather than two inline ternaries fifty lines apart. Behaviour-identical.

Note, pre-existing and tangential

pytest.ini exists and pyproject.toml has [tool.pytest.ini_options] with testpaths = ["tests"]. pytest.ini wins outright, so that testpaths is inert and a bare pytest from the repo root collects the whole tree. Not caused by this PR — and the guard's new location happens to cover that invocation too — but worth folding testpaths into pytest.ini sometime.

Not taken: seed-goa CLI default

Agreed, and your reasoning is the right one: that command's whole purpose is to populate the repo's caches, no test path reaches it, and deriving the dir from --output would silently relocate real curation writes. Leaving it is the conservative choice.

On the declined-by-design placement

Your note that the red section lands above pytest's own N passed line is accurate — summary_stats() runs after the pytest_terminal_summary hook in the same wrapper. Given the named-files section now sits in the summary block rather than before it, that's a fine resting place.

Verification note

Static again: no uv or virtualenv in this container, and the sandbox blocked probing for an interpreter, so I could not run the suite, the new test, ruff or mypy. Everything above comes from reading the diff, the committed reactome/ fixture, pytest.ini, and pytest's hook ordering; I've cited the evidence so you can check the reasoning. Your run (175 passed, clean integration run, clean tree) and CI are the authority on pass/fail.

Approving — only 🔵s remain.
· branch claude/confident-shannon-vfz0tz

@ai4c-reviewer ai4c-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up pass on 8d437d1: the 🟡 Reactome leak is closed and four of five 🔵s were taken.

Verified statically: _get_reactome_title no longer hard-codes a cache dir and its single call site threads reactome_dir; fetch_gene_data/fetch_gene_data_ncRNA pass base_path / "reactome", with base_path still normalized to cwd so the CLI is unchanged. The new test assertion is non-vacuous — the committed reactome/R-CEL-9856612.md carries a different # heading, which get_cached_title would return without the parameter. The rootdir conftest.py really is an initial conftest for every invocation including pytest --doctest-modules src; tests/conftest.py is back to main byte-for-byte; the ExitCode.OK guard preserves interrupted/usage-error/no-tests codes; and pytest_terminal_summary does fire, since TerminalReporter gates on its hook argument and OK is in summary_exit_codes. No test writes to the real reactome/, so the new PROTECTED_TREES entry shouldn't false-trip.

Two take-or-leave 🔵s in the full comment: gocams/ (plus rules/arba, rules/unirule) are the same cwd-default shape reactome/ was in and are one PROTECTED_TREES entry from being covered; and the two cwd fallbacks could be hoisted out of the per-reference loop. Declining the seed-goa CLI default is the right call for the reason you gave.

Note: no uv/virtualenv in this container, so I could not run the suite, the new test, or ruff — your results and CI are the authority on pass/fail.

See #5025 (comment) for details.

@cmungall cmungall changed the title Keep tests from writing into the committed publications/ cache Keep tests from writing into the committed publications/ and reactome/ caches Oct 15, 2026

Copy link
Copy Markdown
Member Author

Thanks. Both new 🔵s will go into the next code push if one is needed: adding gocams/ and rules/ to PROTECTED_TREES, and hoisting the cwd fallbacks out of the loop. Otherwise they stay as they are; neither is a leak today.


Generated by Claude Code

@cmungall
cmungall added this pull request to the merge queue Oct 16, 2026
Merged via the queue into main with commit aff915d Oct 16, 2026
5 checks passed
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.

2 participants