Skip to content

Commit 4b1a1a7

Browse files
committed
fix(tests): harden top-5 guarantee test and clear git caches in fixture
Seed 15173041 exposed that read_04's count-2 margin was too slim: stale git-related module-level caches from a prior test on the same xdist worker shaved 1 off each observed read_count, tying read_04 with read_05-07 at count=1 and letting recency break the tie in favour of read_07. Two-pronged fix: - Increase read counts so read_04 gets count=3 (formula max(3,7-i)); even with off-by-1 contamination it lands at 2 vs 1 for read_05-07. - Clear five additional compact.py module-level dicts in tmp_data_dir (_diff_stat_summary_cache, _uncommitted_changes_cache, _is_git_repo_cache, _whole_diff_cache, _blocker_preview_cache) so git-related state from other test modules cannot leak between tests.
1 parent ca1c71b commit 4b1a1a7

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

‎tests/conftest.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,27 @@ def tmp_data_dir(tmp_path):
191191
- compact._manifest_sha_written_this_process: set that gates sidecar
192192
re-reads; if a test writes a manifest for "sid-1" the entry persists
193193
into the next test's context, corrupting the sidecar cache-hit check.
194+
- compact git-related caches (keyed by cwd/project_root): stale entries
195+
from tests in other modules running on the same xdist worker can
196+
populate these with data from a different tmp_path, corrupting
197+
uncommitted-changes or diff-stat sections of the manifest.
194198
"""
195199
from token_goat import compact as _compact_mod
196200
from token_goat import session as _session_mod
197201

198-
_session_mod._proc_load_cache.clear()
199-
_compact_mod._manifest_sha_written_this_process.clear()
202+
def _clear_caches() -> None:
203+
_session_mod._proc_load_cache.clear()
204+
_compact_mod._manifest_sha_written_this_process.clear()
205+
_compact_mod._diff_stat_summary_cache.clear()
206+
_compact_mod._uncommitted_changes_cache.clear()
207+
_compact_mod._is_git_repo_cache.clear()
208+
_compact_mod._whole_diff_cache.clear()
209+
_compact_mod._blocker_preview_cache.clear()
210+
211+
_clear_caches()
200212
with patch.object(paths, 'data_dir', return_value=tmp_path):
201213
yield tmp_path
202-
_session_mod._proc_load_cache.clear()
203-
_compact_mod._manifest_sha_written_this_process.clear()
214+
_clear_caches()
204215

205216

206217
@pytest.fixture(autouse=True)

‎tests/test_compact.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5393,13 +5393,13 @@ def test_top5_guaranteed_with_many_edited_files(self, tmp_data_dir):
53935393
for i in range(10):
53945394
cache = session.mark_file_edited(sid, f"/proj/src/edit_{i:02d}.py", cache=cache)
53955395
# 8 read files; first 5 should always appear.
5396-
# Read counts must be STRICTLY DECREASING for the first 5 so that
5397-
# tie-breaking (by recency) cannot push read_04 out of the top 5.
5398-
# read_05–07 share count=1 which is below read_04's count=2; the top
5399-
# 5 are therefore read_00(5), read_01(4), read_02(3), read_03(2),
5400-
# read_04(2) — unambiguously above the tied trio at count=1.
5396+
# Read counts are strictly decreasing: read_00(7), read_01(6),
5397+
# read_02(5), read_03(4), read_04(3) vs read_05-07(1). The gap
5398+
# between read_04 and the tied trio is 2, so even if cross-test
5399+
# module-level state shaves 1 off every count (observed with seed
5400+
# 15173041), read_04 lands at 2 vs 1 and still holds the top 5.
54015401
for i in range(8):
5402-
read_count = max(2, 6 - i) if i < 5 else 1
5402+
read_count = max(3, 7 - i) if i < 5 else 1
54035403
for _ in range(read_count):
54045404
cache = session.mark_file_read(
54055405
sid, f"/proj/src/read_{i:02d}.py", offset=0, limit=50, cache=cache

0 commit comments

Comments
 (0)