Skip to content

test: pre-load install.audit_replay so run_replay patches cannot leak (MAIN-RED FIX, #4876) - #4884

Merged
Daniel Meppiel (danielmeppiel) merged 1 commit into
mainfrom
cloud-4876-audit-replay-preload
Nov 23, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 1 commit into
mainfrom
cloud-4876-audit-replay-preload

Conversation

@danielmeppiel

@danielmeppiel Daniel Meppiel (danielmeppiel) commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Main is red on Windows unit tests: the conftest mock-leak guard reports apm_cli.install.audit_replay.run_replay left bound to a unittest.mock object. apm audit --ci imports audit_replay lazily. That module binds run_replay from drift at import, so a test that patches drift.run_replay and then runs the verb can first-import audit_replay inside the patch.

This adds apm_cli.install.audit_replay to _LAZY_CONSUMERS in tests/conftest.py. The module now loads at session start, so every later patch stays transient. It is the same one-line preload as commit b9f7fd0 on #4827. OPS VP chose this over the patch-where-used approach (#4881), which fails the AQ004 test-assertion ratchet (see the PR comment).

Issue and approved scope

Issue: Fixes #4876

Human scope-approval comment: #4876 (comment) (approach revised by OPS VP in the reopen comment on this PR: conftest preload instead of patch-where-used, which fails AQ004)

Does this PR complete the issue, or what remains? Completes it.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

Validation:

  • uv run --extra dev pytest -q -p no:randomly <every tests/unit file from git grep -l run_replay tests/> (9 files, one process, guard active at sessionfinish): 281 passed, 1 skipped.
  • uv run --extra dev ruff check tests/conftest.py: passed.
  • uv run --extra dev ruff format --check tests/conftest.py: passed.
  • The leak depends on Windows test ordering and did not reproduce on Linux with or without the fix; Windows CI on this PR confirms it.
  • CI Lint currently fails on main too: src/apm_cli/core/deployment_ledger.py is 2101 lines (limit 2100) after the docs(policy): user-scope org policy is still absent in 0.38; #3703 targets 0.39 #4806 merge. This PR does not touch it.

Changelog

  • One fragment per section, changelog.d/<pr>.<section>.md, holding
    one line: user-visible effect first, 25 words or fewer, ending with
    (#PR); breaking changes only under upgrade notes; no Highlights; no
    new lines under ## [Unreleased]. See changelog.d/README.md and
    .github/instructions/changelog.instructions.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • N/A -- this PR does not change OpenAPM-observable behaviour.

apm audit --ci imports audit_replay on first use, so a test that patches
drift.run_replay and then runs the verb bound the module to the mock.

Fixes #4876

Co-authored-by: copilot-swe-agent[bot] <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The change only modifies the leak guard’s preload list, leaving the approved test-local patches and required real-function regression check unimplemented.

1 open issue
What changed in this PR

This test-only change aims to prevent an order-dependent mock leak reported by the session-end guard, particularly in Windows unit-test shards.

Changes:

  • Adds apm_cli.install.audit_replay to the session-start preload list so it imports before tests patch drift.run_replay.
  • Leaves production code unchanged.
File Description
tests/​conftest.py Preloads audit_replay to prevent its module globals from retaining a patched function.

Review effort: Balanced

Comment thread tests/conftest.py Resolved
@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

Closing as a duplicate: #4881 (OPS VP lane) is THE fix for #4876; one fix, one lander.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The reviewed diff still shows the conftest preload that the author reports reverting, so a refreshed diff is needed to assess the unavailable test-local replacement.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

Reopened by OPS VP: this is THE fix for #4876 after all. #4881's patch-where-used approach fails the AQ004 test-assertion ratchet (Test Architecture Ratchets + Architecture Ratchet Contracts), while this conftest preload does not. #4881 closes as superseded.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The available snapshot still contains the conftest preload reportedly reverted in 7ea41dc, while the replacement test-local fix and regression fixture are unavailable for review.

No open issues 🎉

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the test-only audit replay preload change.

No open issues 🎉

Review effort: Balanced

Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 23, 2026
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit c4de3ba into main Nov 23, 2026
152 of 210 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the cloud-4876-audit-replay-preload branch November 23, 2026 06:17
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 23, 2026
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.

[BUG] Flaky unit shard: test_audit_ci_command patches drift.run_replay and leaks a Mock into audit_replay

2 participants