test: de-flake test_skills_stats_cache mtime race

test_skills_stats_cache asserted that adding a skill bumps the skills-dir
mtime so the cheap per-call probe recomputes counts on the next call. Two
_write_skill() calls inside the same coarse filesystem mtime tick could leave
the skills-dir mtime byte-identical to the cached probe value, so the probe
saw no change and served the stale count of 1 (assert enabled==2 failed). This
is an order-dependent flake: passes in isolation, fails intermittently under
full-suite ordering (observed across multiple unrelated PRs).

Force the skills-dir mtime strictly forward after the second write so the
probe is guaranteed to detect a genuine mtime change. The assertion stays
honest (it still proves recompute-on-mtime-change); only the race is removed.
This commit is contained in:
nesquena-hermes
2026-06-29 01:59:22 +00:00
parent 1ae168aa20
commit 359eae93de
+11
View File
@@ -144,6 +144,17 @@ def test_skills_stats_cache(tmp_path):
# Adding a skill bumps the skills-dir mtime; the cheap probe detects it on
# the very next call and the counts update immediately (no stale TTL window).
_write_skill(tmp_path, "beta")
# Guarantee the skills-dir mtime is STRICTLY newer than the cached probe value.
# A real FS-backed skill-add always advances the dir mtime, but two writes inside
# the same coarse mtime tick can leave it byte-identical under full-suite timing —
# the source of this test's intermittent flake (order-dependent: passes in
# isolation, fails ~intermittently under full-suite ordering). Forcing it forward
# keeps the assertion honest (the probe must recompute on a genuine mtime change)
# without racing the filesystem's mtime granularity.
import os as _os, time as _time
_skills_dir = tmp_path / "skills"
_future = _time.time() + 5
_os.utime(_skills_dir, (_future, _future))
enabled, compat = profiles._get_profile_skills_stats(tmp_path)
assert enabled == 2 and compat == 2