Skip to content

Personal AI, On Personal Devices

Python

fix(memory): reject non-positive max_facts instead of disabling the cap (#757) (#802)

havolline11550bea53def
diff --git a/src/openjarvis/memory/store.py b/src/openjarvis/memory/store.py
index fbad20e5..f1dae706 100644
--- a/src/openjarvis/memory/store.py
+++ b/src/openjarvis/memory/store.py
@@ -163,10 +163,15 @@ class LocalFactStore(FactStore):
*,
max_facts: int = 1000,
) -> None:
+ max_facts = int(max_facts)
+ if max_facts <= 0:
+ raise ValueError(
+ f"max_facts must be a positive integer, got {max_facts}"
+ )
self._path = (
Path(path).expanduser() if path is not None else _default_fact_path()
)
- self._max_facts = max(0, int(max_facts))
+ self._max_facts = max_facts
self._lock = threading.Lock()
self._facts: List[Fact] = self._load()
@@ -244,8 +249,11 @@ class LocalFactStore(FactStore):
if any(f.text.lower() == lowered for f in self._facts):
return False # dedupe
self._facts.append(Fact(text=text, source=source, created_at=time.time()))
- # Enforce the cap by evicting the oldest entries.
- if self._max_facts and len(self._facts) > self._max_facts:
+ # Enforce the cap by evicting the oldest entries. max_facts is
+ # always positive (validated in __init__), so this slice can't
+ # hit Python's `list[-0:]` footgun, which returns the whole
+ # list instead of emptying it.
+ if len(self._facts) > self._max_facts:
self._facts = self._facts[-self._max_facts :]
self._flush()
return True
diff --git a/tests/memory/test_fact_store.py b/tests/memory/test_fact_store.py
index fc054732..85f9c607 100644
--- a/tests/memory/test_fact_store.py
+++ b/tests/memory/test_fact_store.py
@@ -87,6 +87,24 @@ def test_max_facts_evicts_oldest(tmp_path):
assert facts == ["second", "third"] # oldest dropped
+def test_max_facts_zero_rejected(tmp_path):
+ """Regression for #757: max_facts=0 must not silently disable the cap.
+
+ ``self._max_facts and len(self._facts) > self._max_facts`` short-
+ circuits on 0 (falsy), so eviction never ran and the store grew
+ without bound -- the opposite of what a "cap on stored facts" should
+ do for its minimum value. Reject non-positive values outright instead
+ of accepting one that means the opposite of what it says.
+ """
+ with pytest.raises(ValueError):
+ LocalFactStore(tmp_path / "facts.jsonl", max_facts=0)
+
+
+def test_max_facts_negative_rejected(tmp_path):
+ with pytest.raises(ValueError):
+ LocalFactStore(tmp_path / "facts.jsonl", max_facts=-5)
+
+
def test_persistence_across_instances(tmp_path):
path = tmp_path / "facts.jsonl"
store = LocalFactStore(path)