|
| 1 | +"""Agent-written memory against a real .lbug store: write, replace, forget, and |
| 2 | +the line between an agent's facts and the source system's.""" |
| 3 | +import shutil |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +lb = pytest.importorskip("ladybug", reason="LadybugDB driver not installed") |
| 8 | + |
| 9 | +from tracerag import config # noqa: E402 |
| 10 | +from tracerag.db import TraceDB # noqa: E402 |
| 11 | +from tracerag.memory import AgentMemory # noqa: E402 |
| 12 | +from tracerag.router import TraceRouter # noqa: E402 |
| 13 | + |
| 14 | +TEAM_A = [1.0] + [0.0] * (config.EMBED_DIM - 1) |
| 15 | + |
| 16 | + |
| 17 | +def embed(text: str) -> list[float]: |
| 18 | + """Deterministic stand-in for the real embedder: one hot slot per name.""" |
| 19 | + v = [0.0] * config.EMBED_DIM |
| 20 | + v[sum(map(ord, text)) % config.EMBED_DIM] = 1.0 |
| 21 | + return v |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def db(tmp_path): |
| 26 | + store = tmp_path / "m.lbug" |
| 27 | + d = TraceDB(store, pool_size=2) |
| 28 | + d.init_schema() |
| 29 | + d.upsert_node("service:checkout", "checkout-service", "Service", embed("checkout-service")) |
| 30 | + d.upsert_node("pr:a/b#7", "PR #7", "PR", embed("PR #7")) |
| 31 | + d.add_relationship("pr:a/b#7", "service:checkout", relation=config.RELATION_TOUCHES, ts=1) |
| 32 | + d.build_vector_index() # memory must work with the index live, as it is in serving |
| 33 | + yield d |
| 34 | + d.close() |
| 35 | + shutil.rmtree(store, ignore_errors=True) |
| 36 | + |
| 37 | + |
| 38 | +def router(db: TraceDB) -> TraceRouter: |
| 39 | + r = TraceRouter(db) |
| 40 | + r._encode = embed |
| 41 | + r._get_extractor = lambda: type("E", (), {"extract": lambda self, q: []})() |
| 42 | + r._classify_intent = lambda q: (0.15, 0.85, "relational") # no live model in tests |
| 43 | + return r |
| 44 | + |
| 45 | + |
| 46 | +def has_fact(found: list[str], a: str, b: str, relation: str, remembered: bool) -> bool: |
| 47 | + """Facts are undirected, so either end can print first.""" |
| 48 | + return any(a in f and b in f and f": {relation}" in f and ("remembered" in f) == remembered |
| 49 | + for f in found) |
| 50 | + |
| 51 | + |
| 52 | +def facts(db: TraceDB, question: str) -> list[str]: |
| 53 | + return router(db).route(question, top_k=10).facts |
| 54 | + |
| 55 | + |
| 56 | +def agent_edges(db: TraceDB) -> list[dict]: |
| 57 | + return db._fetch( |
| 58 | + f"MATCH (a)-[r:{config.REL_TABLE}]->(b) WHERE r.origin = 'agent' " |
| 59 | + f"RETURN a.label AS a, r.relation AS rel, b.label AS b, r.ended AS ended, " |
| 60 | + f"r.source AS source ORDER BY b.label;") |
| 61 | + |
| 62 | + |
| 63 | +def test_a_remembered_fact_is_retrieved_and_labelled(db): |
| 64 | + AgentMemory(db, embed).remember("checkout-service", "owned by", "payments team", source="run-1") |
| 65 | + found = facts(db, "checkout-service") |
| 66 | + assert any(f.endswith(": OWNED_BY (remembered, source: run-1)") for f in found) |
| 67 | + # the GitHub fact on the same node is still there, and unlabelled |
| 68 | + assert has_fact(found, "PR #7", "checkout-service", "TOUCHES", remembered=False) |
| 69 | + |
| 70 | + |
| 71 | +def test_a_new_name_becomes_a_searchable_node(db): |
| 72 | + AgentMemory(db, embed).remember("checkout-service", "owned by", "payments team", source="run-1") |
| 73 | + hits = [h["id"] for h in db.vector_search(embed("payments team"), k=3)] |
| 74 | + assert "mem:payments-team" in hits |
| 75 | + |
| 76 | + |
| 77 | +def test_replacing_ends_the_old_fact_but_keeps_its_history(db): |
| 78 | + memory = AgentMemory(db, embed) |
| 79 | + memory.remember("checkout-service", "owned by", "payments team", source="run-1") |
| 80 | + result = memory.remember("checkout-service", "owned by", "billing team", |
| 81 | + source="run-2", replace=True) |
| 82 | + assert result["replaced"] == 1 |
| 83 | + found = " ".join(facts(db, "checkout-service")) |
| 84 | + assert "billing team" in found and "payments team" not in found |
| 85 | + history = {e["b"]: e["ended"] for e in agent_edges(db)} |
| 86 | + assert history["payments team"] > 0 and history["billing team"] == 0 |
| 87 | + # walking from the node whose only fact ended must not crash or resurrect it |
| 88 | + assert not any("payments team" in f for f in facts(db, "payments team")) |
| 89 | + |
| 90 | + |
| 91 | +def test_without_replace_both_facts_stand(db): |
| 92 | + memory = AgentMemory(db, embed) |
| 93 | + memory.remember("checkout-service", "owned by", "payments team", source="run-1") |
| 94 | + memory.remember("checkout-service", "owned by", "billing team", source="run-2") |
| 95 | + assert all(e["ended"] == 0 for e in agent_edges(db)) |
| 96 | + |
| 97 | + |
| 98 | +def test_forget_removes_the_fact_and_the_node_it_created(db): |
| 99 | + memory = AgentMemory(db, embed) |
| 100 | + memory.remember("checkout-service", "owned by", "payments team", source="run-1") |
| 101 | + assert memory.forget("checkout-service", "OWNED_BY", "payments team") == 1 |
| 102 | + assert agent_edges(db) == [] |
| 103 | + assert "payments team" not in " ".join(facts(db, "checkout-service")) |
| 104 | + assert "mem:payments-team" not in [h["id"] for h in db.vector_search(embed("payments team"), k=3)] |
| 105 | + |
| 106 | + |
| 107 | +def test_forget_cannot_touch_a_github_fact(db): |
| 108 | + assert AgentMemory(db, embed).forget("PR #7", "TOUCHES", "checkout-service") == 0 |
| 109 | + assert has_fact(facts(db, "checkout-service"), "PR #7", "checkout-service", "TOUCHES", remembered=False) |
| 110 | + |
| 111 | + |
| 112 | +def test_ingest_never_rewrites_an_agent_fact_on_the_same_pair(db): |
| 113 | + AgentMemory(db, embed).remember("PR #7", "broke", "checkout-service", source="run-1") |
| 114 | + # a stronger API relation on the same pair must not "upgrade" the agent's edge |
| 115 | + db.add_relationship("pr:a/b#7", "service:checkout", relation=config.RELATION_AUTHORED, ts=2) |
| 116 | + assert [(e["rel"], e["source"]) for e in agent_edges(db)] == [("BROKE", "run-1")] |
| 117 | + |
| 118 | + |
| 119 | +def test_an_agent_fact_ranks_below_a_github_fact(db): |
| 120 | + AgentMemory(db, embed).remember("PR #7", "broke", "checkout-service", source="run-1") |
| 121 | + neighbours = db.expand_frontier(["pr:a/b#7"], k=5, max_degree=10)["pr:a/b#7"] |
| 122 | + assert [n["origin"] for n in neighbours] == ["api", "agent"] |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.parametrize("subject,relation,obj", [ |
| 126 | + ("", "OWNED_BY", "x"), |
| 127 | + ("x", "; --", "y"), |
| 128 | + ("x", "9 lives", "y"), |
| 129 | + ("x", "", "y"), |
| 130 | + ("x" * 201, "OWNED_BY", "y"), |
| 131 | +]) |
| 132 | +def test_bad_input_is_refused(db, subject, relation, obj): |
| 133 | + with pytest.raises(ValueError): |
| 134 | + AgentMemory(db, embed).remember(subject, relation, obj, source="t") |
| 135 | + |
| 136 | + |
| 137 | +def test_mcp_writes_need_the_configured_token(db, monkeypatch): |
| 138 | + import mcp_server |
| 139 | + |
| 140 | + r = router(db) |
| 141 | + monkeypatch.setattr(mcp_server, "get_current_tenant", lambda: mcp_server.Tenant(router=r, db=db)) |
| 142 | + monkeypatch.setattr(config, "MCP_WRITE_TOKEN", None) |
| 143 | + assert not mcp_server._remember_impl("anything", "a", "OWNS", "b", False)["ok"] |
| 144 | + |
| 145 | + monkeypatch.setattr(config, "MCP_WRITE_TOKEN", "s3cret") |
| 146 | + assert not mcp_server._remember_impl("wrong", "a", "OWNS", "b", False)["ok"] |
| 147 | + assert not mcp_server._forget_impl("", "a", "OWNS", "b")["ok"] |
| 148 | + assert agent_edges(db) == [] |
| 149 | + |
| 150 | + assert mcp_server._remember_impl("s3cret", "checkout-service", "OWNED_BY", "payments team", False)["ok"] |
| 151 | + assert agent_edges(db)[0]["source"] == "mcp" |
| 152 | + assert mcp_server._forget_impl("s3cret", "checkout-service", "OWNED_BY", "payments team")["removed"] == 1 |
| 153 | + assert mcp_server._remember_impl("s3cret", "", "OWNED_BY", "x", False)["ok"] is False |
0 commit comments