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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
perf(memory): bound traffic-learner _persisted_ids with the dedup window (#3341)
Abhay Singh7e73438dc5f9
diff --git a/headroom/memory/traffic_learner.py b/headroom/memory/traffic_learner.pyindex 612af996c..8727d3c2d 100644--- a/headroom/memory/traffic_learner.py+++ b/headroom/memory/traffic_learner.py@@ -1275,10 +1275,16 @@ class TrafficLearner:# Ready to savedel self._pattern_counts[h]self._saved_hashes.add(h)- # Trim saved hashes to prevent unbounded growth+ # Trim saved hashes to prevent unbounded growth, and drop the evicted+ # hash's persisted-id entry in lockstep. ``_persisted_ids`` is only+ # ever read behind an ``h in _saved_hashes`` guard (the dedup check+ # above), so an id for a hash no longer tracked is dead weight;+ # without this it grew one entry per distinct persisted pattern for+ # the whole process lifetime while ``_saved_hashes`` stayed bounded.if len(self._saved_hashes) > self._dedup_window:# Remove oldest (arbitrary, set is unordered, but prevents growth)- self._saved_hashes.pop()+ evicted = self._saved_hashes.pop()+ self._persisted_ids.pop(evicted, None)# Persist the real accumulated count, not the dataclass default.pattern.evidence_count = count@@ -1310,9 +1316,13 @@ class TrafficLearner:},)self._patterns_saved += 1- # Track id so future re-sightings bump this row.+ # Track id so future re-sightings bump this row — but only while+ # the hash is still within the dedup window. If it was evicted+ # from ``_saved_hashes`` between enqueue and now, recording its id+ # would re-leak an entry that can never be read again (the dedup+ # read at the top of ``_accumulate`` is gated on ``_saved_hashes``).memory_id = getattr(memory, "id", None)- if memory_id is not None:+ if memory_id is not None and pattern.content_hash in self._saved_hashes:self._persisted_ids[pattern.content_hash] = memory_idlogger.debug(f"Traffic learner saved pattern: {pattern.content[:80]}")@@ -1341,7 +1351,16 @@ class TrafficLearner:try:rows = conn.execute("SELECT id, content, metadata FROM memories "- "WHERE json_extract(metadata, '$.source') = 'traffic_learner'"+ "WHERE json_extract(metadata, '$.source') = 'traffic_learner' "+ # Hydrate at most dedup_window rows so a large persisted+ # history does not start the in-memory dedup maps oversized+ # (they are trimmed to dedup_window in steady state). Keep the+ # most-recently-seen patterns; rows without last_seen_at+ # (legacy) sort last under DESC and are dropped first, with id+ # as a deterministic tie-break.+ "ORDER BY json_extract(metadata, '$.last_seen_at') DESC, id DESC "+ "LIMIT ?",+ (self._dedup_window,),).fetchall()except sqlite3.DatabaseError:return []diff --git a/tests/test_memory/test_traffic_learner.py b/tests/test_memory/test_traffic_learner.pyindex 4faa037b8..4ff1fcc44 100644--- a/tests/test_memory/test_traffic_learner.py+++ b/tests/test_memory/test_traffic_learner.py@@ -412,6 +412,41 @@ class TestTrafficLearner:assert pattern.content_hash not in learner._pattern_counts # removed on promotionassert pattern.content_hash in learner._saved_hashes+ def test_persisted_ids_bounded_in_lockstep_with_saved_hashes(self):+ """``_persisted_ids`` (content_hash -> memory row id) must not outgrow+ ``_saved_hashes``. The dedup read only consults ``_persisted_ids`` behind+ an ``h in _saved_hashes`` guard, so an id whose hash has been evicted from+ the dedup window is dead weight; previously it accumulated one entry per+ distinct persisted pattern for the whole process lifetime while+ ``_saved_hashes`` stayed trimmed to ``dedup_window``.+ """+ import asyncio++ learner = TrafficLearner(backend=None, min_evidence=1, dedup_window=4)++ async def feed() -> None:+ for i in range(200):+ pattern = ExtractedPattern(+ category=PatternCategory.PREFERENCE,+ content=f"distinct preference number {i}",+ importance=0.5,+ )+ await learner._accumulate(pattern) # first sight -> pending+ await learner._accumulate(pattern) # second -> promote to saved+ # Emulate the async save worker recording the row id, using the+ # same "still tracked" guard the worker now applies.+ if pattern.content_hash in learner._saved_hashes:+ learner._persisted_ids[pattern.content_hash] = f"mem-{i}"++ asyncio.run(feed())++ # _saved_hashes stays bounded (existing behavior).+ assert len(learner._saved_hashes) <= 4+ # _persisted_ids no longer leaks: it never holds an id for a hash that is+ # no longer in the dedup window, so it stays bounded too.+ assert set(learner._persisted_ids).issubset(learner._saved_hashes)+ assert len(learner._persisted_ids) <= 4 # not 200+@pytest.mark.asyncioasync def test_dedup(self, learner: TrafficLearner):"""Test that identical patterns are deduplicated."""@@ -1446,6 +1481,59 @@ class TestHydrateEdgeCases:assert learner._saved_hashes == set()assert learner._persisted_ids == {}+ @pytest.mark.asyncio+ async def test_hydration_is_bounded_to_dedup_window(self, tmp_path):+ """A persisted history larger than dedup_window must not start the+ in-memory dedup maps oversized. Hydration keeps at most dedup_window+ rows (the most-recently-seen), and both maps stay bounded with matching+ keys — otherwise a long-lived install boots with an unbounded leak that+ only trims one entry at a time."""+ import json as _json+ import sqlite3 as _sql++ db = tmp_path / "memory.db"+ _init_db(db)++ window = 5+ total = 20+ conn = _sql.connect(db)+ try:+ for i in range(total):+ conn.execute(+ "INSERT INTO memories (id, content, metadata, entity_refs, importance) "+ "VALUES (?,?,?,?,?)",+ (+ f"id-{i:02d}",+ f"Command `cmd{i}` fails; use `alt{i}` instead.",+ _json.dumps(+ {+ "source": "traffic_learner",+ "category": "error_recovery",+ "evidence_count": 2,+ # Higher i == more recently seen; hydration keeps+ # the newest `window` of these.+ "last_seen_at": f"2026-01-01T00:{i:02d}:00+00:00",+ }+ ),+ "[]",+ 0.7,+ ),+ )+ conn.commit()+ finally:+ conn.close()++ backend = _FakeBackend(db)+ learner = TrafficLearner(backend=backend, min_evidence=2, dedup_window=window)+ await learner._hydrate_persisted_state()++ # Both maps are bounded to the window and hold exactly the same keys.+ assert len(learner._saved_hashes) <= window+ assert len(learner._persisted_ids) <= window+ assert set(learner._persisted_ids) == set(learner._saved_hashes)+ # The retained rows are the most-recently-seen ones (ids 15..19).+ assert set(learner._persisted_ids.values()) == {f"id-{i:02d}" for i in range(15, 20)}+class TestBumpEdgeCases:@pytest.mark.asyncio