| import pytest
|
| import pytest_asyncio
|
| import os
|
| import asyncio
|
| from mnemocore.core.config import get_config, reset_config
|
| from mnemocore.core.engine import HAIMEngine
|
|
|
| @pytest.fixture
|
| def test_engine(tmp_path):
|
| from mnemocore.core.hnsw_index import HNSWIndexManager
|
| HNSWIndexManager._instance = None
|
| reset_config()
|
|
|
| data_dir = tmp_path / "data"
|
| data_dir.mkdir()
|
|
|
| os.environ["HAIM_DATA_DIR"] = str(data_dir)
|
| os.environ["HAIM_ENCODING_MODE"] = "binary"
|
| os.environ["HAIM_DIMENSIONALITY"] = "1024"
|
| os.environ["HAIM_ANTICIPATORY_ENABLED"] = "True"
|
| os.environ["HAIM_ANTICIPATORY_PREDICTIVE_DEPTH"] = "1"
|
| os.environ["HAIM_MEMORY_FILE"] = str(tmp_path / "memory.jsonl")
|
| os.environ["HAIM_LTP_INITIAL_IMPORTANCE"] = "1.0"
|
|
|
| reset_config()
|
| engine = HAIMEngine()
|
| yield engine
|
|
|
| del os.environ["HAIM_DATA_DIR"]
|
| del os.environ["HAIM_ENCODING_MODE"]
|
| del os.environ["HAIM_DIMENSIONALITY"]
|
| del os.environ["HAIM_ANTICIPATORY_ENABLED"]
|
| del os.environ["HAIM_ANTICIPATORY_PREDICTIVE_DEPTH"]
|
| del os.environ["HAIM_MEMORY_FILE"]
|
| del os.environ["HAIM_LTP_INITIAL_IMPORTANCE"]
|
| reset_config()
|
|
|
| @pytest.mark.asyncio
|
| async def test_anticipatory_memory(test_engine):
|
| await test_engine.initialize()
|
|
|
| node_a = await test_engine.store("I love learning about Machine Learning and AI algorithms.")
|
| node_b = await test_engine.store("Neural networks and deep learning models are very powerful.")
|
|
|
|
|
|
|
|
|
| await test_engine.bind_memories(node_a, node_b, weight=10.0)
|
|
|
|
|
| mem_b_obj = await test_engine.tier_manager.get_memory(node_b)
|
| assert mem_b_obj is not None
|
|
|
| mem_b_obj.ltp_strength = -1.0
|
| mem_b_obj.tier = "hot"
|
|
|
| async with test_engine.tier_manager.lock:
|
| if node_b in test_engine.tier_manager.hot:
|
| del test_engine.tier_manager.hot[node_b]
|
| test_engine.tier_manager._remove_from_faiss(node_b)
|
| await test_engine.tier_manager._save_to_warm(mem_b_obj)
|
|
|
|
|
| assert node_b not in test_engine.tier_manager.hot
|
|
|
|
|
| results = await test_engine.query("I love learning about Machine Learning and AI algorithms.", top_k=2)
|
|
|
| assert len(results) > 0
|
| assert results[0][0] == node_a
|
|
|
|
|
| await asyncio.sleep(0.1)
|
|
|
|
|
| assert node_b in test_engine.tier_manager.hot, "Anticipatory engine failed to preload node_b."
|
|
|
| await test_engine.close()
|
|
|