Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(memory): skip already archived entries in the compression pass

Lanre Shittua3b656c3e502
diff --git a/agent/src/memory/lifecycle.py b/agent/src/memory/lifecycle.py
index f31714b59..417a42049 100644
--- a/agent/src/memory/lifecycle.py
+++ b/agent/src/memory/lifecycle.py
@@ -205,6 +205,7 @@ class MemoryLifecycle:
now = time.time()
actions: list[dict] = []
+ gc_acted_ids: set[str] = set()
for entry in entries:
age_days = (now - entry.created_at) / 86400.0
@@ -237,12 +238,17 @@ class MemoryLifecycle:
# Tier 1: force archive even if classified as delete
effective = "archive" if not self.ENABLE_DELETE else action
self._execute_gc_action(entry, effective)
+ gc_acted_ids.add(entry.id)
self._append_gc_log(actions, dry_run)
# Tier 2: Trigger compression for aged entries. Skipped on dry_run:
# compression rewrites entry bodies and frontmatter in place, and a
- # dry run must not mutate the files it is only auditing.
+ # dry run must not mutate the files it is only auditing. Entries
+ # already archived/deleted above are skipped too: _execute_gc_action
+ # already renamed or unlinked entry.path on disk, so re-using this
+ # same pre-Tier-1 snapshot for them would try to compress a file
+ # that no longer exists at that location.
from src.config.accessor import get_env_config
if not dry_run and get_env_config().memory.compression_enabled:
try:
@@ -250,6 +256,8 @@ class MemoryLifecycle:
pipeline = CompressionPipeline(self._memory._dir)
now_ts = time.time()
for entry in entries:
+ if entry.id in gc_acted_ids:
+ continue
target = pipeline.should_compress(
compression_level=entry.compression_level,
last_accessed=entry.last_accessed,
diff --git a/agent/tests/test_memory_gc.py b/agent/tests/test_memory_gc.py
index eea7613b6..441ed0a0c 100644
--- a/agent/tests/test_memory_gc.py
+++ b/agent/tests/test_memory_gc.py
@@ -227,6 +227,39 @@ class TestRunGC:
assert after != before
assert "compression_level: daily" in after
+ def test_gc_archived_entry_is_not_also_compressed(
+ self, tmp_path: Path, monkeypatch, caplog
+ ) -> None:
+ """An entry archived by Tier 1 must not be handed to Tier 2 compression.
+
+ Old + low quality makes Tier 1 archive it; also stale-since-access
+ makes it Tier-2-compression-eligible. Tier 2 must skip it instead of
+ trying to compress a file Tier 1 already moved away.
+ """
+ monkeypatch.setenv("VT_MEMORY_GC", "1")
+ monkeypatch.setenv("VT_MEMORY_DECAY", "1")
+ monkeypatch.setenv("VT_MEMORY_COMPRESSION", "1")
+ now = time.time()
+ created_iso = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(now - 30 * 86400))
+ accessed_iso = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(now - 30 * 86400))
+ _create_memory_file(
+ tmp_path,
+ "old-and-stale",
+ quality_score=0.01,
+ access_count=0,
+ created_at=created_iso,
+ last_accessed=accessed_iso,
+ )
+ pm = PersistentMemory(memory_dir=tmp_path)
+ lc = MemoryLifecycle(pm)
+ with caplog.at_level("WARNING"):
+ actions = lc.run_gc(dry_run=False)
+ assert actions[0]["action"] == "archive"
+ assert (tmp_path / "archive" / "project_old-and-stale.md").exists()
+ assert not (tmp_path / "project_old-and-stale.md").exists()
+ assert "Cannot archive non-existent file" not in caplog.text
+ assert "_write_compressed" not in caplog.text
+
# ---------------------------------------------------------------------------
# 2. find_relevant importance weighting