Spaces:
Configuration error
Configuration error
File size: 13,357 Bytes
a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 ac98b25 a6e11a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 | import json
import pytest
from agentcache.core import KV, ObservationEvents, ObservationStore, SearchService
from agentcache.search import SearchIndex
def test_observation_events_dataclass():
"""Verify ObservationEvents dataclass has required typed callback lists."""
events = ObservationEvents()
assert isinstance(events.on_added, list)
assert isinstance(events.on_deleted, list)
assert isinstance(events.on_folder_deleted, list)
def test_observation_store_ingest_and_dedup(tmp_db):
"""Verify ObservationStore.ingest happy path, dedup on write, and events firing."""
kv = tmp_db
bm25 = SearchIndex()
search_svc = SearchService(bm25_index=bm25, kv=kv)
added_events = []
events = ObservationEvents(on_added=[lambda ev: added_events.append(ev)])
store = ObservationStore(kv=kv, search_service=search_svc, events=events)
payload1 = {
"folderPath": "src/services",
"agentId": "agent_alpha",
"text": "Extracted ObservationStore module",
"timestamp": "2026-07-24T12:00:00Z",
}
res1 = store.ingest(payload1)
assert "observationId" in res1
assert "deduplicated" not in res1
obs_id1 = res1["observationId"]
# Verify event fired
assert len(added_events) == 1
assert added_events[0]["type"] == "folder_observation"
assert added_events[0]["data"]["id"] == obs_id1
# Ingest distinct text -> deduplicated must NOT be set
payload_distinct = {
"folderPath": "src/services",
"agentId": "agent_alpha",
"text": "Distinct second observation text",
"timestamp": "2026-07-24T12:01:00Z",
}
res_distinct = store.ingest(payload_distinct)
assert "observationId" in res_distinct
assert "deduplicated" not in res_distinct
# Ingest duplicate text on same folderPath + agentId
res2 = store.ingest(payload1)
assert res2.get("observationId") == obs_id1
assert res2.get("deduplicated") is True
# SearchService indexed obs1
search_results = search_svc.search("Extracted ObservationStore", limit=5)
assert len(search_results) == 1
assert search_results[0]["id"] == obs_id1
def test_observation_store_max_cap(tmp_db, monkeypatch):
"""Verify MAX_OBS_PER_FOLDER cap is enforced during ingest."""
monkeypatch.setenv("MAX_OBS_PER_FOLDER", "2")
kv = tmp_db
store = ObservationStore(kv=kv)
store.ingest(
{
"folderPath": "src/cap",
"agentId": "agent_beta",
"text": "First item",
"timestamp": "2026-07-24T12:00:00Z",
}
)
store.ingest(
{
"folderPath": "src/cap",
"agentId": "agent_beta",
"text": "Second item",
"timestamp": "2026-07-24T12:01:00Z",
}
)
with pytest.raises(ValueError, match="Folder observation limit reached"):
store.ingest(
{
"folderPath": "src/cap",
"agentId": "agent_beta",
"text": "Third item",
"timestamp": "2026-07-24T12:02:00Z",
}
)
def test_observation_store_manual_dedup(tmp_db):
"""Verify ObservationStore.dedup removes duplicate observations across pairs."""
kv = tmp_db
store = ObservationStore(kv=kv)
obs1 = {
"id": "fobs_dup_1",
"folderPath": "src/dedup",
"agentId": "agent_gamma",
"text": "Duplicated text sample",
"timestamp": "2026-07-24T10:00:00Z",
}
obs2 = {
"id": "fobs_dup_2",
"folderPath": "src/dedup",
"agentId": "agent_gamma",
"text": "Duplicated text sample",
"timestamp": "2026-07-24T11:00:00Z",
}
kv.set(
KV.folders,
"src/dedup:agent_gamma",
{"folderPath": "src/dedup", "agentId": "agent_gamma"},
)
kv.set(KV.folder_obs("src/dedup", "agent_gamma"), "fobs_dup_1", obs1)
kv.set(KV.folder_obs("src/dedup", "agent_gamma"), "fobs_dup_2", obs2)
kv.set(
KV.obs_lookup,
"fobs_dup_1",
{"folderPath": "src/dedup", "agentId": "agent_gamma"},
)
kv.set(
KV.obs_lookup,
"fobs_dup_2",
{"folderPath": "src/dedup", "agentId": "agent_gamma"},
)
res = store.dedup("src/dedup", "agent_gamma")
assert res["success"] is True
assert res["deduplicated"] == 1
assert res["kept"] == 1
remaining = kv.list(KV.folder_obs("src/dedup", "agent_gamma"))
assert len(remaining) == 1
assert remaining[0]["id"] == "fobs_dup_1"
def test_observation_store_forget_full_and_partial(tmp_db):
"""Verify ObservationStore.forget partial and full pair deletions and events."""
kv = tmp_db
bm25 = SearchIndex()
search_svc = SearchService(bm25_index=bm25, kv=kv)
deleted_obs = []
deleted_folders = []
events = ObservationEvents(
on_deleted=[lambda ids: deleted_obs.extend(ids)],
on_folder_deleted=[lambda fp, aid: deleted_folders.append((fp, aid))],
)
store = ObservationStore(kv=kv, search_service=search_svc, events=events)
obs1_id = store.ingest(
{
"folderPath": "src/forget",
"agentId": "agent_forget",
"text": "First item to forget",
"timestamp": "2026-07-24T10:00:00Z",
}
)["observationId"]
store.ingest(
{
"folderPath": "src/forget",
"agentId": "agent_forget",
"text": "Second item to keep initially",
"timestamp": "2026-07-24T11:00:00Z",
}
)["observationId"]
# Explicitly empty observationIds list -> graceful handling of no-op partial delete
res_noop = store.forget(
{
"folderPath": "src/forget",
"agentId": "agent_forget",
"observationIds": [],
}
)
assert res_noop["success"] is True
assert res_noop["deleted"] == 0
# 1. Partial deletion
res_part = store.forget(
{
"folderPath": "src/forget",
"agentId": "agent_forget",
"observationIds": [obs1_id],
}
)
assert res_part["success"] is True
assert res_part["deleted"] == 1
assert deleted_obs == [obs1_id]
meta = kv.get(KV.folder_meta("src/forget", "agent_forget"), "meta")
assert meta["obsCount"] == 1
# 2. Full folder pair deletion
res_full = store.forget(
{
"folderPath": "src/forget",
"agentId": "agent_forget",
}
)
assert res_full["success"] is True
assert res_full["deleted"] == 1
assert len(deleted_folders) == 1
assert deleted_folders[0] == ("src/forget", "agent_forget")
remaining_folders = kv.list(KV.folders)
assert len(remaining_folders) == 0
def test_observation_store_timeline_sorting_and_filtering(tmp_db):
"""Verify ObservationStore.timeline sorting descending and timestamp filters."""
kv = tmp_db
store = ObservationStore(kv=kv)
store.ingest(
{
"folderPath": "src/t1",
"agentId": "agent_t",
"text": "Oldest observation",
"timestamp": "2026-07-24T10:00:00Z",
}
)
store.ingest(
{
"folderPath": "src/t1",
"agentId": "agent_t",
"text": "Middle observation",
"timestamp": "2026-07-24T12:00:00Z",
}
)
store.ingest(
{
"folderPath": "src/t2",
"agentId": "agent_t",
"text": "Newest observation",
"timestamp": "2026-07-24T14:00:00Z",
}
)
# Timeline all
tl_all = store.timeline(limit=10)
assert len(tl_all) == 3
assert tl_all[0]["text"] == "Newest observation"
assert tl_all[1]["text"] == "Middle observation"
assert tl_all[2]["text"] == "Oldest observation"
# Filter before and after
tl_filtered = store.timeline(
limit=10,
before="2026-07-24T13:00:00Z",
after="2026-07-24T11:00:00Z",
)
assert len(tl_filtered) == 1
assert tl_filtered[0]["text"] == "Middle observation"
# Filter folder_path
tl_folder = store.timeline(folder_path="src/t1")
assert len(tl_folder) == 2
def test_observation_store_rebuild_index_and_backfill_lookup(tmp_db):
"""Verify ObservationStore.rebuild_index and backfill_lookup populate missing entries."""
kv = tmp_db
bm25 = SearchIndex()
search_svc = SearchService(bm25_index=bm25, kv=kv)
store = ObservationStore(kv=kv, search_service=search_svc)
# 1. Backfill test: insert raw observation without lookup entry
kv.set(
KV.folders,
"src/bf:agent_bf",
{"folderPath": "src/bf", "agentId": "agent_bf", "obsCount": 1},
)
kv.set(
KV.folder_obs("src/bf", "agent_bf"),
"fobs_bf1",
{
"id": "fobs_bf1",
"folderPath": "src/bf",
"agentId": "agent_bf",
"text": "Unindexed backfill observation",
"timestamp": "2026-07-24T10:00:00Z",
},
)
kv.delete(KV.obs_lookup, "fobs_bf1")
assert kv.get(KV.obs_lookup, "fobs_bf1") is None
store.backfill_lookup()
lookup = kv.get(KV.obs_lookup, "fobs_bf1")
assert lookup is not None
assert lookup["folderPath"] == "src/bf"
assert lookup["agentId"] == "agent_bf"
# 2. Rebuild index test: search_svc index is currently 0
assert search_svc.bm25_size == 0
count = store.rebuild_index()
assert count == 1
assert search_svc.bm25_size == 1
search_res = search_svc.search("Unindexed backfill", limit=5)
assert len(search_res) == 1
assert search_res[0]["id"] == "fobs_bf1"
def test_http_and_mcp_forget_and_timeline_end_to_end(app_client):
"""Verify Flask HTTP timeline & forget routes and MCP tools work end-to-end."""
client = app_client
# Ingest observation via HTTP
resp1 = client.post(
"/agentcache/agent/observe",
json={
"folderPath": "src/mcp_test",
"agentId": "agent_mcp",
"text": "Testing MCP tool handlers",
"timestamp": "2026-07-24T15:00:00Z",
},
)
assert resp1.status_code == 201
# Timeline via HTTP POST /agentcache/timeline
resp_tl = client.post(
"/agentcache/timeline",
json={"folderPath": "src/mcp_test", "agentId": "agent_mcp"},
)
assert resp_tl.status_code == 200
data_tl = resp_tl.get_json()
assert len(data_tl["observations"]) == 1
# Timeline via MCP tool
resp_mcp_tl = client.post(
"/agentcache/mcp/tools",
json={
"name": "cache_timeline",
"arguments": {"folderPath": "src/mcp_test", "agentId": "agent_mcp"},
},
)
assert resp_mcp_tl.status_code == 200
mcp_out = resp_mcp_tl.get_json()["content"][0]["text"]
assert "Testing MCP tool handlers" in mcp_out
# Forget via MCP tool
resp_mcp_forget = client.post(
"/agentcache/mcp/tools",
json={
"name": "cache_forget",
"arguments": {"folderPath": "src/mcp_test", "agentId": "agent_mcp"},
},
)
assert resp_mcp_forget.status_code == 200
# Post-forget timeline check
resp_tl_post = client.post(
"/agentcache/timeline",
json={"folderPath": "src/mcp_test", "agentId": "agent_mcp"},
)
assert resp_tl_post.status_code == 200
assert len(resp_tl_post.get_json()["observations"]) == 0
def test_full_lifecycle_e2e_pass(app_client):
"""Full integration pass: observe -> search -> timeline -> forget -> rebuild (HTTP & MCP layers)."""
client = app_client
# 1. OBSERVE via MCP
mcp_obs = client.post(
"/agentcache/mcp/tools",
json={
"name": "agent_observe",
"arguments": {
"folderPath": "src/e2e",
"agentId": "agent_e2e",
"text": "Refactored observation store completely",
"timestamp": "2026-07-24T16:00:00Z",
},
},
)
assert mcp_obs.status_code == 200
obs_id = json.loads(mcp_obs.get_json()["content"][0]["text"])["observationId"]
# 2. SEARCH via HTTP
search_resp = client.post(
"/agentcache/search",
json={
"query": "observation store",
"folderPath": "src/e2e",
"agentId": "agent_e2e",
},
)
assert search_resp.status_code == 200
search_data = search_resp.get_json()
assert len(search_data) == 1
assert search_data[0]["id"] == obs_id
# 3. TIMELINE via MCP
timeline_resp = client.post(
"/agentcache/mcp/tools",
json={
"name": "cache_timeline",
"arguments": {"folderPath": "src/e2e", "agentId": "agent_e2e"},
},
)
assert timeline_resp.status_code == 200
assert (
"Refactored observation store" in timeline_resp.get_json()["content"][0]["text"]
)
# 4. REBUILD via ObservationStore
import agentcache.app as app_mod
obs_store = app_mod.observation_store
count = obs_store.rebuild_index()
assert count >= 1
# 5. FORGET via HTTP / MCP
forget_resp = client.post(
"/agentcache/mcp/tools",
json={
"name": "cache_forget",
"arguments": {"folderPath": "src/e2e", "agentId": "agent_e2e"},
},
)
assert forget_resp.status_code == 200
|