Skip to content

Python: fix Purview cache accounting when replacing entries - #9052

Closed
Suhas Aggarwal (suhasagg) wants to merge 1 commit into
microsoft:mainfrom
suhasagg:fix-8988-purview-cache-accounting
Closed

Suhas Aggarwal (suhasagg) wants to merge 1 commit into
microsoft:mainfrom
suhasagg:fix-8988-purview-cache-accounting

Conversation

@suhasagg

Copy link
Copy Markdown
Contributor

Summary

Fix Purview in-memory cache byte accounting when replacing an existing
entry, particularly when the replacement requires eviction.

Problem

When an existing cache entry is replaced, its size is subtracted from
_current_size_bytes, but the old entry remains in _cache while
eviction runs.

If eviction encounters that entry, its size can be subtracted again,
causing _current_size_bytes to diverge from the actual size of the
live cache entries.

A regression test reproduced this on the existing implementation:

  • tracked cache size: 154 bytes
  • actual live-entry size: 206 bytes

Changes

  • Remove an existing entry from _cache before adjusting its size.
  • Run eviction against the actual set of live cache entries.
  • Preserve stale expiry-heap handling.
  • Add regression coverage for growing an existing entry when the cache
    is at capacity.
  • Verify that tracked cache size matches the total size of live entries.

Testing

  • Regression test before fix: failed as expected (154 != 206)
  • Regression test after fix: passed
  • Purview cache tests: 18 passed
  • Purview non-integration tests: 200 passed
  • Ruff check: passed
  • Ruff format check: passed
  • git diff --check: passed

Fixes #8988

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.

Copilot review overview

🟢 Approval recommended

The focused fix resolves the double-subtraction bug and includes effective regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes Purview cache byte accounting when replacing entries during eviction.

Changes:

  • Removes replaced entries before eviction.
  • Adds regression coverage validating tracked size against live entries.
File Description
python/​packages/​purview/​agent_framework_purview/​_cache.py Corrects replacement accounting and eviction behavior.
python/​packages/​purview/​tests/​purview/​test_cache.py Tests larger replacements at cache capacity.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@eavanvalkenburg

Copy link
Copy Markdown
Member

Suhas Aggarwal (@suhasagg) Thanks for the contribution. This implements the same change as #8990, so we'll continue the work there and close this duplicate.

This branch was previously deployed

1 inactive deployment
github-app-auth — e3769b18 Deployed Oct 5, 2026 by suhasagg via team_check #6044
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Purview cache undercounts bytes when growing an entry at capacity

3 participants