Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(memory): clamp memory age so a future created_at can't crash or invert ranking (#3139)

Abhay Singh8208a4e759c3
diff --git a/headroom/memory/budget.py b/headroom/memory/budget.py
index db38d8223..7f579fc41 100644
--- a/headroom/memory/budget.py
+++ b/headroom/memory/budget.py
@@ -140,7 +140,11 @@ class MemoryBudgetManager:
result: list[MemoryEntry] = []
for m in memories:
- age_days = (now - m.created_at) / 86400
+ # Clamp to non-negative: a future created_at (clock skew) otherwise
+ # makes exp(-rate × negative) > 1 and inflates importance above 1.0,
+ # so a future-dated memory outranks everything. Treat it as new (no
+ # decay), matching memory_rank_policy.memory_recency_factor.
+ age_days = max(0.0, (now - m.created_at) / 86400)
# Exponential decay: importance × e^(-rate × age)
import math
diff --git a/headroom/memory/writers/base.py b/headroom/memory/writers/base.py
index 367a82787..5e8ed1636 100644
--- a/headroom/memory/writers/base.py
+++ b/headroom/memory/writers/base.py
@@ -49,7 +49,12 @@ class MemoryEntry:
@property
def score(self) -> float:
"""Combined score for ranking: importance × recency × access."""
- age_days = (time.time() - self.created_at) / 86400
+ # Clamp to non-negative: a future created_at (clock skew, a timestamp
+ # set on another machine) otherwise drives the denominator to zero at
+ # exactly +10 days (ZeroDivisionError) and negative past that, inverting
+ # the ranking. A future memory is treated as brand new (no decay), which
+ # matches memory_rank_policy.memory_recency_factor.
+ age_days = max(0.0, (time.time() - self.created_at) / 86400)
recency = 1.0 / (1.0 + age_days * 0.1) # Decay over ~10 days
access_boost = min(1.0, 0.5 + self.access_count * 0.1)
return self.importance * recency * access_boost
diff --git a/tests/test_memory/test_budget.py b/tests/test_memory/test_budget.py
index 583eca3d3..ff2b39484 100644
--- a/tests/test_memory/test_budget.py
+++ b/tests/test_memory/test_budget.py
@@ -67,6 +67,18 @@ class TestBudgetManager:
if len(optimized) >= 2:
assert optimized[0].content == "New memory"
+ def test_decay_does_not_inflate_future_timestamps(self, manager: MemoryBudgetManager):
+ # A future created_at (clock skew) made exp(-rate × negative) > 1, so the
+ # decayed importance rose above the original (and above 1.0) and the
+ # future memory outranked a fresh, genuinely-important one. Age is now
+ # clamped to >= 0, so a future memory decays like a brand-new one.
+ future = _make_entry("Future", importance=0.5, age_days=-30) # created 30d ahead
+ decayed = manager._apply_decay([future])
+
+ assert decayed # survives the min-importance filter
+ assert decayed[0].importance <= 0.5 # never inflated above the original
+ assert decayed[0].importance <= 1.0
+
def test_access_count_boost(self, manager: MemoryBudgetManager):
unused = _make_entry("Unused", importance=0.5, age_days=5, access_count=0)
used = _make_entry("Heavily used", importance=0.5, age_days=5, access_count=10)
diff --git a/tests/test_memory/test_writers.py b/tests/test_memory/test_writers.py
index 9ecc58be8..7cf18591a 100644
--- a/tests/test_memory/test_writers.py
+++ b/tests/test_memory/test_writers.py
@@ -60,6 +60,23 @@ class TestMemoryEntry:
old = MemoryEntry(content="old", importance=0.9, created_at=time.time() - 30 * 86400)
assert recent.score > old.score
+ def test_score_handles_future_timestamp(self):
+ # A future created_at (clock skew, a timestamp written on another
+ # machine) drove recency = 1/(1 + age_days*0.1) to a divide-by-zero at
+ # exactly +10 days and negative past that, inverting the ranking. Age is
+ # now clamped to >= 0, so a future memory scores like a brand-new one.
+ now = time.time()
+ boom = MemoryEntry(content="future", importance=0.9, created_at=now + 10 * 86400)
+ far_future = MemoryEntry(content="far", importance=0.9, created_at=now + 30 * 86400)
+ fresh = MemoryEntry(content="fresh", importance=0.9, created_at=now)
+
+ assert boom.score > 0 # no ZeroDivisionError at exactly +10 days
+ assert far_future.score > 0 # not negative past +10 days
+ # Any future timestamp clamps to age 0, so all future memories score
+ # identically to a brand-new one (never better than a real fresh one).
+ assert far_future.score == boom.score
+ assert far_future.score >= fresh.score
+
def test_content_hash(self):
e1 = MemoryEntry(content="same content")
e2 = MemoryEntry(content="same content")