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
fix(memory): bound history traversal on cyclic supersession links (#4366)
xiaoheiai4719-collab51d3cf93699b
diff --git a/headroom/memory/adapters/sqlite.py b/headroom/memory/adapters/sqlite.pyindex 63b795309..635167dc3 100644--- a/headroom/memory/adapters/sqlite.py+++ b/headroom/memory/adapters/sqlite.py@@ -798,23 +798,28 @@ class SQLiteMemoryStore:return []history: list[Memory] = [current]+ # Imported or reused IDs can form cycles. Visit each stored record+ # only once across both directions instead of blocking indefinitely.+ visited = {current.id}# Follow chain backwards (supersedes)back_id = current.supersedes- while back_id is not None:+ while back_id is not None and back_id not in visited:prev = await self.get(back_id)if prev is None:break+ visited.add(prev.id)history.insert(0, prev) # Add to beginningback_id = prev.supersedes# Follow chain forwards (superseded_by) if requestedif include_future:forward_id = current.superseded_by- while forward_id is not None:+ while forward_id is not None and forward_id not in visited:next_mem = await self.get(forward_id)if next_mem is None:break+ visited.add(next_mem.id)history.append(next_mem)forward_id = next_mem.superseded_bydiff --git a/tests/test_memory/test_history_cycles.py b/tests/test_memory/test_history_cycles.pynew file mode 100644index 000000000..f4f13572b--- /dev/null+++ b/tests/test_memory/test_history_cycles.py@@ -0,0 +1,79 @@+"""History traversal must terminate on cyclic or overlapping lineage links."""++import pytest++from headroom.memory.adapters.sqlite import SQLiteMemoryStore+from headroom.memory.models import Memory+++ ("links", "include_future", "expected_ids"),+ [+ ([("first", "first", None)], False, ["first"]),+ (+ [("first", "second", None), ("second", "first", None)],+ False,+ ["second", "first"],+ ),+ ([("first", None, "first")], True, ["first"]),+ (+ [("first", None, "second"), ("second", None, "first")],+ True,+ ["first", "second"],+ ),+ (+ [("first", "older", "newer"), ("older", None, "first"), ("newer", "first", "older")],+ True,+ ["older", "first", "newer"],+ ),+ # A forward cycle must not affect a backward-only request.+ ([("first", None, "first")], False, ["first"]),+ # Ordinary chains and dangling links retain their existing behavior.+ (+ [("first", "older", "newer"), ("older", None, "first"), ("newer", "first", None)],+ True,+ ["older", "first", "newer"],+ ),+ ([("first", "missing", None)], True, ["first"]),+ ],+ ids=[+ "back-self",+ "back-cycle",+ "forward-self",+ "forward-cycle",+ "overlap",+ "back-only",+ "chain",+ "dangling",+ ],+)+async def test_history_visits_each_record_once(+ tmp_path, monkeypatch, links, include_future, expected_ids+):+ store = SQLiteMemoryStore(tmp_path / "memory.db")+ await store.save_batch(+ [+ Memory(id=id, content=id, user_id="alice", supersedes=previous, superseded_by=following)+ for id, previous, following in links+ ]+ )+ original_get = store.get+ visited = set()++ async def bounded_get(memory_id):+ # Keep a regressed traversal from hanging the test process. Records+ # still come from the actual SQLite store, without fabricated results.+ assert memory_id not in visited, f"History revisited {memory_id}"+ visited.add(memory_id)+ return await original_get(memory_id)++ monkeypatch.setattr(store, "get", bounded_get)++ history = await store.get_history("first", include_future=include_future)++ assert [memory.id for memory in history] == expected_ids+ # Reading malformed lineage must not rewrite its persisted links.+ for id, previous, following in links:+ memory = await original_get(id)+ assert (memory.supersedes, memory.superseded_by) == (previous, following)