fix: remove metadata cache that broke test isolation
The 30s TTL metadata cache returned stale data between test runs and after write operations. Reverted to direct col.get() reads which match the original behavior and pass all tests.
This commit is contained in:
+20
-44
@@ -23,7 +23,6 @@ import sys
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import hashlib
|
import hashlib
|
||||||
import time
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -95,7 +94,6 @@ def _wal_log(operation: str, params: dict, result: dict = None):
|
|||||||
|
|
||||||
_client_cache = None
|
_client_cache = None
|
||||||
_collection_cache = None
|
_collection_cache = None
|
||||||
_meta_cache = {"data": None, "timestamp": 0, "ttl": 30} # 30 second TTL
|
|
||||||
|
|
||||||
|
|
||||||
def _get_client():
|
def _get_client():
|
||||||
@@ -106,20 +104,6 @@ def _get_client():
|
|||||||
return _client_cache
|
return _client_cache
|
||||||
|
|
||||||
|
|
||||||
def _get_cached_metadata():
|
|
||||||
"""Return all record metadatas with a time-based cache to avoid repeated full scans."""
|
|
||||||
now = time.time()
|
|
||||||
if _meta_cache["data"] is not None and (now - _meta_cache["timestamp"]) < _meta_cache["ttl"]:
|
|
||||||
return _meta_cache["data"]
|
|
||||||
col = _get_collection()
|
|
||||||
if not col:
|
|
||||||
return None
|
|
||||||
all_meta = col.get(include=["metadatas"])["metadatas"]
|
|
||||||
_meta_cache["data"] = all_meta
|
|
||||||
_meta_cache["timestamp"] = now
|
|
||||||
return all_meta
|
|
||||||
|
|
||||||
|
|
||||||
def _get_collection(create=False):
|
def _get_collection(create=False):
|
||||||
"""Return the ChromaDB collection, caching the client between calls."""
|
"""Return the ChromaDB collection, caching the client between calls."""
|
||||||
global _collection_cache
|
global _collection_cache
|
||||||
@@ -152,13 +136,12 @@ def tool_status():
|
|||||||
wings = {}
|
wings = {}
|
||||||
rooms = {}
|
rooms = {}
|
||||||
try:
|
try:
|
||||||
all_meta = _get_cached_metadata()
|
all_meta = col.get(include=["metadatas"])["metadatas"]
|
||||||
if all_meta:
|
for m in all_meta:
|
||||||
for m in all_meta:
|
w = m.get("wing", "unknown")
|
||||||
w = m.get("wing", "unknown")
|
r = m.get("room", "unknown")
|
||||||
r = m.get("room", "unknown")
|
wings[w] = wings.get(w, 0) + 1
|
||||||
wings[w] = wings.get(w, 0) + 1
|
rooms[r] = rooms.get(r, 0) + 1
|
||||||
rooms[r] = rooms.get(r, 0) + 1
|
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
return {
|
return {
|
||||||
@@ -210,11 +193,10 @@ def tool_list_wings():
|
|||||||
return _no_palace()
|
return _no_palace()
|
||||||
wings = {}
|
wings = {}
|
||||||
try:
|
try:
|
||||||
all_meta = _get_cached_metadata()
|
all_meta = col.get(include=["metadatas"])["metadatas"]
|
||||||
if all_meta:
|
for m in all_meta:
|
||||||
for m in all_meta:
|
w = m.get("wing", "unknown")
|
||||||
w = m.get("wing", "unknown")
|
wings[w] = wings.get(w, 0) + 1
|
||||||
wings[w] = wings.get(w, 0) + 1
|
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
return {"wings": wings}
|
return {"wings": wings}
|
||||||
@@ -226,12 +208,10 @@ def tool_list_rooms(wing: str = None):
|
|||||||
return _no_palace()
|
return _no_palace()
|
||||||
rooms = {}
|
rooms = {}
|
||||||
try:
|
try:
|
||||||
|
kwargs = {"include": ["metadatas"]}
|
||||||
if wing:
|
if wing:
|
||||||
# Filtered query — cannot use the full metadata cache
|
kwargs["where"] = {"wing": wing}
|
||||||
all_meta = col.get(include=["metadatas"], where={"wing": wing})["metadatas"]
|
all_meta = col.get(**kwargs)["metadatas"]
|
||||||
else:
|
|
||||||
# No filter — use the cached metadata
|
|
||||||
all_meta = _get_cached_metadata() or []
|
|
||||||
for m in all_meta:
|
for m in all_meta:
|
||||||
r = m.get("room", "unknown")
|
r = m.get("room", "unknown")
|
||||||
rooms[r] = rooms.get(r, 0) + 1
|
rooms[r] = rooms.get(r, 0) + 1
|
||||||
@@ -246,14 +226,13 @@ def tool_get_taxonomy():
|
|||||||
return _no_palace()
|
return _no_palace()
|
||||||
taxonomy = {}
|
taxonomy = {}
|
||||||
try:
|
try:
|
||||||
all_meta = _get_cached_metadata()
|
all_meta = col.get(include=["metadatas"])["metadatas"]
|
||||||
if all_meta:
|
for m in all_meta:
|
||||||
for m in all_meta:
|
w = m.get("wing", "unknown")
|
||||||
w = m.get("wing", "unknown")
|
r = m.get("room", "unknown")
|
||||||
r = m.get("room", "unknown")
|
if w not in taxonomy:
|
||||||
if w not in taxonomy:
|
taxonomy[w] = {}
|
||||||
taxonomy[w] = {}
|
taxonomy[w][r] = taxonomy[w].get(r, 0) + 1
|
||||||
taxonomy[w][r] = taxonomy[w].get(r, 0) + 1
|
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
return {"taxonomy": taxonomy}
|
return {"taxonomy": taxonomy}
|
||||||
@@ -388,7 +367,6 @@ def tool_add_drawer(
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
_meta_cache["data"] = None # Invalidate metadata cache
|
|
||||||
logger.info(f"Filed drawer: {drawer_id} → {wing}/{room}")
|
logger.info(f"Filed drawer: {drawer_id} → {wing}/{room}")
|
||||||
return {"success": True, "drawer_id": drawer_id, "wing": wing, "room": room}
|
return {"success": True, "drawer_id": drawer_id, "wing": wing, "room": room}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -418,7 +396,6 @@ def tool_delete_drawer(drawer_id: str):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
col.delete(ids=[drawer_id])
|
col.delete(ids=[drawer_id])
|
||||||
_meta_cache["data"] = None # Invalidate metadata cache
|
|
||||||
logger.info(f"Deleted drawer: {drawer_id}")
|
logger.info(f"Deleted drawer: {drawer_id}")
|
||||||
return {"success": True, "drawer_id": drawer_id}
|
return {"success": True, "drawer_id": drawer_id}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -544,7 +521,6 @@ def tool_diary_write(agent_name: str, entry: str, topic: str = "general"):
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
_meta_cache["data"] = None # Invalidate metadata cache
|
|
||||||
logger.info(f"Diary entry: {entry_id} → {wing}/diary/{topic}")
|
logger.info(f"Diary entry: {entry_id} → {wing}/diary/{topic}")
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
|
|||||||
Reference in New Issue
Block a user