Skip to content

fix(devtools): expose pytest selection during verify - #2107

Merged
Sinity merged 1 commit into
masterfrom
feature/fix/pytest-selection-observability
Jun 18, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/fix/pytest-selection-observability

Conversation

@Sinity

@Sinity Sinity commented Jun 18, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds explicit pytest selection observability to the managed devtools verify / devtools test harness. Slow or stalled runs now leave a current-pytest-selection.json artifact alongside the existing progress, event, and output ledgers.

Problem

The harness could report the active pytest node and terminate stalled child process groups, but it did not preserve the selected/deselected node set or phase-level timing. That made it hard to distinguish a genuinely slow test from a bad testmon selection, expensive collection, or fixture/setup hang.

Solution

The pytest progress plugin now writes .cache/verify/current-pytest-selection.json with selected and deselected node IDs, emits a collection summary event, and records setup/call/teardown reports instead of only call reports and failures. devtools verify and devtools test both pass the selection path through their managed environments and surface the artifact path in metadata/output.

Verification

  • devtools test tests/unit/devtools/test_pytest_progress_plugin.py tests/unit/devtools/test_run_tests.py -n 0
  • devtools test tests/unit/devtools/test_pytest_progress_plugin.py -k records_node -n 0 and read back .cache/verify/current-pytest-selection.json
  • ruff format --check devtools/pytest_progress_plugin.py devtools/verify.py devtools/run_tests.py tests/unit/devtools/test_pytest_progress_plugin.py
  • ruff check devtools/pytest_progress_plugin.py devtools/verify.py devtools/run_tests.py tests/unit/devtools/test_pytest_progress_plugin.py
  • devtools verify --quick

Summary by CodeRabbit

  • New Features
    • Added test selection diagnostics tracking with counts of selected and deselected tests
    • Enhanced test reporting to capture all test phases (setup, call, teardown) with outcomes and timing
    • Selection metadata now included in verification results and notifications

Problem: a slow or stalled selected pytest run exposed only the active node and stream output, leaving agents to infer whether testmon selected the wrong set or whether time was spent in collection, setup, call, or teardown. That made harness slowness hard to diagnose and encouraged ad hoc reruns.\n\nWhat changed: the pytest progress plugin now writes a current-pytest-selection.json artifact with selected and deselected node IDs, records collection summaries in the event stream, and logs setup/call/teardown phase reports. devtools verify and devtools test both pass the selection artifact path through their managed environments and report it next to the existing progress/events/output files.\n\nVerification: devtools test tests/unit/devtools/test_pytest_progress_plugin.py tests/unit/devtools/test_run_tests.py -n 0; ruff format --check devtools/pytest_progress_plugin.py devtools/verify.py devtools/run_tests.py tests/unit/devtools/test_pytest_progress_plugin.py; ruff check devtools/pytest_progress_plugin.py devtools/verify.py devtools/run_tests.py tests/unit/devtools/test_pytest_progress_plugin.py; devtools verify --quick
@coderabbitai

coderabbitai Bot commented Jun 18, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: fcb720a5-24b1-4114-9889-2da7eea1bbb7

📥 Commits

Reviewing files that changed from the base of the PR and between a8cd1c1 and a11465f.

📒 Files selected for processing (4)
  • devtools/pytest_progress_plugin.py
  • devtools/run_tests.py
  • devtools/verify.py
  • tests/unit/devtools/test_pytest_progress_plugin.py

📝 Walkthrough

Walkthrough

The pytest progress plugin gains a selection ledger: new module state tracks deselected node IDs, a pytest_deselected hook accumulates them, and pytest_collection_modifyitems writes a JSON artifact with selected/deselected counts and node lists plus a collection_finished event. Per-test reporting expands to emit test_report events for all three phases. verify.py and run_tests.py are updated to define, clean up, export, read, and surface the artifact path.

Changes

Pytest selection ledger

Layer / File(s) Summary
Plugin state, selection hooks, and report filtering
devtools/pytest_progress_plugin.py
Adds _SELECTION_ENV and _DESELECTED_NODEIDS module state; implements _write_selection; adds pytest_deselected to accumulate deselected node IDs; updates pytest_collection_modifyitems to write the selection JSON artifact and emit a collection_finished event; expands pytest_runtest_logreport to emit test_report events for setup, call, and teardown phases.
Artifact path, env export, cleanup, and metadata
devtools/verify.py, devtools/run_tests.py
Adds PYTEST_SELECTION_PATH constant; deletes stale selection file before pytest runs; exports POLYLOGUE_PYTEST_SELECTION_PATH into subprocess env from both verify.py and run_tests.py; reads selected_count/deselected_count from the artifact into step metadata; includes selection= in the run_tests status line.
Updated and new plugin unit tests
tests/unit/devtools/test_pytest_progress_plugin.py
Refactors existing failure-event assertions to check (nodeid, when, outcome) sequence first; adds _Item/_Session helpers and test_progress_plugin_records_final_selected_nodes to drive deselection/collection-modification flow and assert selection JSON contents and the collection_finished event.

Sequence Diagram(s)

sequenceDiagram
  participant verify as verify.py / run_tests.py
  participant pytest_sub as pytest subprocess
  participant plugin as pytest_progress_plugin
  participant sel_json as current-pytest-selection.json
  participant event_log as event log (JSONL)

  verify->>verify: delete stale current-pytest-selection.json
  verify->>pytest_sub: launch with POLYLOGUE_PYTEST_SELECTION_PATH set
  pytest_sub->>plugin: pytest_deselected(items)
  plugin->>plugin: accumulate _DESELECTED_NODEIDS
  pytest_sub->>plugin: pytest_collection_modifyitems(session, items)
  plugin->>sel_json: write {selected_count, deselected_count, selected_nodeids, deselected_nodeids}
  plugin->>event_log: append collection_finished event
  pytest_sub->>plugin: pytest_runtest_logreport(report) [setup/call/teardown]
  plugin->>event_log: append test_report event
  pytest_sub-->>verify: exit
  verify->>sel_json: read selected_count, deselected_count
  verify->>verify: record counts in step metadata
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • Sinity/polylogue#2100: Modifies devtools/pytest_progress_plugin.py's pytest_runtest_logreport filtering and event-serialization logic, which is the same hook expanded in this PR to cover setup/call/teardown phases.

Poem

🐇 Hop, hop, I count each test with care,
Selected and skipped — I track every hare!
A JSON ledger written fresh and neat,
collection_finished marks the feat.
No test goes unrecorded in my warren sweet! 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 35.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main change: exposing pytest selection diagnostics during the verify workflow.
Description check ✅ Passed The description is comprehensive and follows the template structure with all major sections (Summary, Problem, Solution, Verification) properly filled; Changelog and Risks sections are appropriately omitted as not required.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/fix/pytest-selection-observability

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a11465f4aa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +62 to +63
@pytest.hookimpl(trylast=True)
def pytest_collection_modifyitems(session: Any, config: Any, items: list[Any]) -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Write selection after testmon prunes items

In the default devtools verify path, devtools/verify.py:776 runs pytest with --testmon; pytest-testmon's selector is also a trylast pytest_collection_modifyitems registered during configuration. Because this hook is registered earlier, it can run before testmon mutates items and emits its deselections, so current-pytest-selection.json reports the pre-testmon selected set and misses the deselected tests in exactly the affected-test lane this artifact is meant to explain. Write the artifact from a hookwrapper after yield or a later collection hook so it observes the final item list.

Useful? React with 👍 / 👎.

@Sinity
Sinity merged commit dfe57dc into master Jun 18, 2026
11 checks passed
@Sinity
Sinity deleted the feature/fix/pytest-selection-observability branch June 18, 2026 13:16
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