fix(memory): take the most recent lessons by decision date (#1603)

- get_past_context orders a ticker's settled decisions by their date, so a backfill of older dates no longer pushes out the latest lesson
- rotation drops the oldest-dated settled entries, not the first written
This commit is contained in:
Yijia-Xiao
2026-10-27 07:59:51 +00:00
parent ee06dcb961
commit caf6792bba
2 changed files with 35 additions and 14 deletions
+20
View File
@@ -300,6 +300,16 @@ class TestTradingMemoryLogCore:
assert "Buy entry 0" not in ctx
assert "Buy entry 5" in ctx
def test_the_most_recent_decisions_are_the_latest_dated_not_the_latest_written(self, tmp_path):
"""A backfill writes January after June; June is still the most recent lesson."""
log = make_log(tmp_path)
_seed_completed(tmp_path, "NVDA", "2026-06-01", "Buy entry June.", "Correct.")
for i in range(5):
_seed_completed(tmp_path, "NVDA", f"2026-01-{i+1:02d}", f"Buy entry {i}.", "Correct.")
ctx = log.get_past_context("NVDA", n_same=5)
assert "Buy entry June" in ctx and "Buy entry 0" not in ctx
assert ctx.index("Buy entry June") < ctx.index("Buy entry 4")
def test_n_cross_limit_respected(self, tmp_path):
"""Only the n_cross most recent cross-ticker entries are included."""
log = make_log(tmp_path)
@@ -341,6 +351,16 @@ class TestTradingMemoryLogCore:
dates = [e["date"] for e in entries]
assert dates == ["2026-01-03", "2026-01-04", "2026-01-05"]
def test_rotation_prunes_the_oldest_dated_not_the_first_written(self, tmp_path):
"""A backfill writes older dates last; rotation still keeps the newest decisions."""
log = TradingMemoryLog({
"memory_log_path": str(tmp_path / "trading_memory.md"),
"memory_log_max_entries": 3,
})
for date in ("2026-06-01", "2026-06-02", "2026-01-01", "2026-01-02", "2026-01-03"):
_resolve_entry(log, "NVDA", date, DECISION_BUY, f"Lesson {date}.")
assert sorted(e["date"] for e in log.load_entries()) == ["2026-01-03", "2026-06-01", "2026-06-02"]
def test_rotation_never_prunes_pending(self, tmp_path):
"""Pending entries (unresolved) are kept regardless of the cap."""
log = TradingMemoryLog({