From 12f6bf1db78cae52623e9c1e3b9726eae184b178 Mon Sep 17 00:00:00 2001 From: Coding-Dev-Tools Date: Thu, 3 Sep 2026 09:37:10 -0400 Subject: [PATCH] test(decay): deflake reinforced-memory guard against coarse clocks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test simulated reinforcement by setting last_access = now_ts(), then asserted the next decay pass leaves stability bit-identical (1e-9). On hosts with coarse clock resolution (observed on Windows: consecutive time.time() calls can return the same value), last_access can equal the just-written last_decay anchor, so the strict 'last_access > anchor' guard misses and the pass legitimately decays one tick's interval (~2.1e-9 stability drift) — a race, not the compounding regression this test pins. Set last_access one second past the pass anchor so the guard condition holds deterministically on every host, and extend coverage to 50 rapid passes to pin the guard, not just the first one after the update. Reproduced: full-suite runs failed this test intermittently (stability drift 2.13e-9 vs 1e-9 threshold) while it passed in isolation; 20/20 same-tick stress trials now stable across 8 consecutive test runs. --- tests/test_decay_idempotent.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_decay_idempotent.py b/tests/test_decay_idempotent.py index ac76feb0..4ab77a9b 100644 --- a/tests/test_decay_idempotent.py +++ b/tests/test_decay_idempotent.py @@ -67,10 +67,18 @@ def test_reinforced_memory_is_not_decayed_that_interval(monkeypatch, tmp_path): decayed = _stability("ns", "hot") assert decayed < 5.0 # Simulate reinforcement: the memory is accessed now (last_access moves past the anchor). + # Bump one second past the pass's anchor: time.time() can return the SAME + # value as the anchor on coarse-clock hosts (observed on Windows), and a + # last_access equal to the anchor makes the subsequent pass legitimately + # decay one tick's interval — a race, not the regression this pins. conn = get_conn() conn.execute("UPDATE memories SET last_access=? WHERE namespace=? AND document_id=?", - (now_ts(), "ns", "hot")) + (now + 1.0, "ns", "hot")) conn.commit() # A subsequent pass must NOT decay it further — it was just accessed. mem_store.apply_decay_to_all("ns", 7.0) assert abs(_stability("ns", "hot") - decayed) < 1e-9 + # And the guard must hold across many rapid passes, not just one. + for _ in range(50): + mem_store.apply_decay_to_all("ns", 7.0) + assert abs(_stability("ns", "hot") - decayed) < 1e-9