apishift-env / tests /test_memory.py
yaswanth169's picture
Initial APIShift env push
3040bf7 verified
"""MemoryAgent and retrieval tests."""
import os
from server.memory import LessonRetriever, MemoryAgent
from server.memory.lesson_schema import parse_lessons_md
def test_memory_agent_creates_lesson(tmp_path):
lessons_path = str(tmp_path / "lessons.md")
agent = MemoryAgent(lessons_path=lessons_path)
written = agent.update(
episode_id="ep_0001",
score=0.85,
n_steps=8,
action_log=[
{"command": "dispatch_diff", "rationale": "first"},
{"command": "dispatch_patch", "rationale": "patch ch_001"},
{"command": "dispatch_test", "rationale": "test"},
{"command": "dispatch_rollback", "rationale": "rb"},
{"command": "submit", "rationale": "submit"},
],
dominant_change_type="auth_scheme_changed",
component_rewards={"backward_compat_preservation": 0.9},
)
assert written is not None
assert os.path.exists(lessons_path)
with open(lessons_path, "r", encoding="utf-8") as f:
text = f.read()
lessons = parse_lessons_md(text)
assert len(lessons) >= 1
def test_memory_agent_skips_low_score(tmp_path):
lessons_path = str(tmp_path / "lessons.md")
agent = MemoryAgent(lessons_path=lessons_path)
written = agent.update(
episode_id="ep_0002",
score=0.20,
n_steps=12,
action_log=[],
dominant_change_type="field_renamed",
component_rewards={},
)
assert written is None
def test_retriever_returns_relevant(tmp_path):
lessons_path = str(tmp_path / "lessons.md")
agent = MemoryAgent(lessons_path=lessons_path)
agent.update(
episode_id="ep_0001", score=0.9, n_steps=6,
action_log=[{"command": "dispatch_diff"}, {"command": "submit"}],
dominant_change_type="auth_scheme_changed",
component_rewards={},
)
retr = LessonRetriever(lessons_path=lessons_path)
hits = retr.retrieve("github authentication scheme bearer change")
assert isinstance(hits, list)