AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/copilot): one recall policy so a forgotten fact is never recalled again (#14972)
Nicholas Tindle6ab436bf0ed0
diff --git a/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes.py b/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes.pyindex 739a5e2148..e31bb41940 100644--- a/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes.py+++ b/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes.py@@ -35,6 +35,7 @@ from backend.copilot.dream.nightly_batch import NightlyBatchResultfrom backend.copilot.dream.ratification import RatificationResultfrom backend.copilot.dream.schemas import DreamPassResultfrom backend.copilot.graphiti.falkordb_driver import open_driver+from backend.copilot.graphiti.recall import live_fact_predicatefrom backend.copilot.graphiti.scope import MemoryScopefrom backend.util.clients import get_scheduler_clientfrom backend.util.url_redaction import redact_url@@ -122,6 +123,9 @@ class GraphNode(BaseModel):type: str | None = Nonename: str | None = Nonesummary: str | None = None+ # Set on the tombstone a hard forget leaves of an episode; its text is+ # gone, and only its title and this stamp are shown.+ hard_deleted_at: str | None = Noneclass GraphEdge(BaseModel):@@ -534,10 +538,13 @@ async def _list_facts_impl()group_id = memory_scope.group_id- # Build optional filters+ # Build optional filters. "active" means live and active under the recall+ # policy, so a forgotten or expired fact never shows as active.where_clauses = ["e.group_id = $g"]params: dict[str, Any] = {"g": group_id, "limit": limit}- if status != "any":+ if status == "active":+ where_clauses.append(live_fact_predicate("e", include_tentative=False))+ elif status != "any":where_clauses.append("e.status = $status")params["status"] = statusif scope:@@ -554,8 +561,8 @@ async def _list_facts_impl(RETURN e.uuid AS uuid,src.name AS source,tgt.name AS target,- e.name AS name,- e.fact AS fact,+ coalesce(e.name_redacted, e.name) AS name,+ coalesce(e.fact_redacted, e.fact) AS fact,e.status AS status,e.scope AS scope,e.confidence AS confidence,@@ -793,6 +800,7 @@ async def _get_graph_impl(RETURN n.uuid AS uuid,n.name AS name,n.summary AS summary,+ toString(n.hard_deleted_at) AS hard_deleted_at,labels(n) AS all_labelsLIMIT $limit""",@@ -817,6 +825,7 @@ async def _get_graph_impl(type=custom_type,name=r.get("name"),summary=r.get("summary"),+ hard_deleted_at=r.get("hard_deleted_at"),))if len(nodes) >= node_limit:@@ -847,8 +856,8 @@ async def _get_graph_impl(type(e) AS label,src.uuid AS source,tgt.uuid AS target,- e.name AS name,- e.fact AS fact,+ coalesce(e.name_redacted, e.name) AS name,+ coalesce(e.fact_redacted, e.fact) AS fact,e.status AS status,e.scope AS scopeLIMIT $limitdiff --git a/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes_test.py b/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes_test.pyindex d3cbd2912a..b640c5663a 100644--- a/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes_test.py+++ b/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes_test.py@@ -11,6 +11,7 @@ from redis.exceptions import ResponseErrorfrom backend.api.features.experts.models import PROTECTED_SOUL_RULES, Expertfrom backend.copilot.graphiti.client import derive_memory_group_id+from backend.copilot.graphiti.recall import live_fact_predicatefrom .memory_admin_routes import router as memory_admin_router@@ -582,6 +583,30 @@ class TestListFacts:resp = client.get("/admin/memory/abc/facts?status=garbage")assert resp.status_code == 422+ def test_active_filter_uses_the_recall_live_test(self) -> None:+ """The "active" filter lists what recall would return, tentatives+ aside: an expired edge still stamped active, or a legacy edge with no+ status, is judged the way recall judges it."""+ driver = _driver_returning([])+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):+ resp = client.get("/admin/memory/abc/facts?status=active")+ assert resp.status_code == 200+ query = driver.execute_query.call_args.args[0]+ assert live_fact_predicate("e", include_tentative=False) in query+ assert "status" not in driver.execute_query.call_args.kwargs++ def test_a_forgotten_fact_lists_the_text_its_forget_kept(self) -> None:+ """A forget leaves a placeholder where graphiti reads the sentence+ and keeps the text in ``fact_redacted`` / ``name_redacted`` for this+ audit list."""+ driver = _driver_returning([])+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):+ resp = client.get("/admin/memory/abc/facts")+ assert resp.status_code == 200+ query = driver.execute_query.call_args.args[0]+ assert "coalesce(e.name_redacted, e.name) AS name" in query+ assert "coalesce(e.fact_redacted, e.fact) AS fact" in query+class TestListCommunities:def test_returns_community_summaries(self) -> None:@@ -634,6 +659,39 @@ class TestGraph:bare_client.get("/admin/memory/abc/graph")driver.close.assert_awaited_once()+ def test_shows_audit_text_and_a_tombstones_stamp(self) -> None:+ """A forgotten edge shows the text its forget kept; an episode a hard+ forget emptied shows only when it was emptied."""+ stamp = "2026-09-26T12:00:00+00:00"+ driver = _driver_returning(+ [{"uuid": "alice", "name": "Alice", "summary": "", "all_labels": []}],+ [+ {+ "uuid": "ep1",+ "name": "conversation_s1",+ "summary": None,+ "hard_deleted_at": stamp,+ "all_labels": ["Episodic"],+ }+ ],+ [],+ )+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):+ resp = client.get(+ "/admin/memory/abc/graph?include_episodes=true"+ "&include_communities=false"+ )+ assert resp.status_code == 200+ nodes = {node["uuid"]: node for node in resp.json()["nodes"]}+ assert nodes["ep1"]["hard_deleted_at"] == stamp+ assert nodes["alice"]["hard_deleted_at"] is None+ node_query = driver.execute_query.await_args_list[1].args[0]+ assert "toString(n.hard_deleted_at) AS hard_deleted_at" in node_query+ assert "content" not in node_query, "no episode text in the graph view"+ edge_query = driver.execute_query.await_args_list[2].args[0]+ assert "coalesce(e.name_redacted, e.name) AS name" in edge_query+ assert "coalesce(e.fact_redacted, e.fact) AS fact" in edge_query+class TestRebuildCommunitiesPolling:"""POST /communities/rebuild is now fire-and-forget.diff --git a/autogpt_platform/backend/backend/api/features/memory/routes.py b/autogpt_platform/backend/backend/api/features/memory/routes.pyindex f6eca31b75..486bc618ae 100644--- a/autogpt_platform/backend/backend/api/features/memory/routes.py+++ b/autogpt_platform/backend/backend/api/features/memory/routes.py@@ -6,14 +6,16 @@ expert routes at ``/memory/experts/{expert_id}/...``. Every route resolves thescope from the *authenticated caller* — there is no target-user path segment,so these routes can never read or delete another user's memory by construction.-Deletion semantics match the chat forget tool: single-fact forget is a-bi-temporal system retraction (sets only ``expired_at``; the edge stops being-served but stays for audit), while the scope erase hard-deletes every node and-edge in the scope's graph, raw episode text included.+Facts are listed and counted with the recall policy's live-fact test+(``graphiti/recall.py``), and a single-fact forget is the chat forget tool's+soft ``retract`` (``graphiti/recall_forget.py``): the edge is marked+``retracted`` and stops being served but stays for audit, and every episode+it came from stops being recalled. A forget whose clean-up failed answers 500,+not success, and one that found memory busy answers 409; both retry. The+scope erase hard-deletes every node and edge in the scope's graph."""import logging-from datetime import datetime, timezonefrom typing import Annotatedimport autogpt_libs.auth as autogpt_auth_lib@@ -30,6 +32,9 @@ from backend.api.features.memory.models import ()from backend.copilot.graphiti.config import is_enabled_for_userfrom backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver, open_driver+from backend.copilot.graphiti.memory_model import MemoryForgetFailureCode+from backend.copilot.graphiti.recall import live_fact_predicate+from backend.copilot.graphiti.recall_forget import retractfrom backend.copilot.graphiti.scope import MemoryScopelogger = logging.getLogger(__name__)@@ -49,11 +54,6 @@ _LIMIT_QUERY = Query(ge=1, le=500)_MISSING_GRAPH_MARKERS = ("no such graph", "does not exist", "invalid graph")-def _now_iso() -> str:- """FalkorDB has no Cypher ``datetime()``; timestamps are bound in Python."""- return datetime.now(timezone.utc).isoformat()--def _is_missing_graph_error(exc: BaseException) -> bool:if not isinstance(exc, ResponseError):return False@@ -108,11 +108,17 @@ async def _get_overview_impl(try:facts = await _count(driver,- "MATCH ()-[e:RELATES_TO]->() WHERE e.expired_at IS NULL "+ f"MATCH ()-[e:RELATES_TO]->() WHERE {live_fact_predicate('e')} ""RETURN count(e) AS c",)entities = await _count(driver, "MATCH (n:Entity) RETURN count(n) AS c")- episodes = await _count(driver, "MATCH (n:Episodic) RETURN count(n) AS c")+ # A hard forget's tombstone is a record that a conversation+ # happened, not an episode the user still has.+ episodes = await _count(+ driver,+ "MATCH (n:Episodic) WHERE n.hard_deleted_at IS NULL "+ "RETURN count(n) AS c",+ )finally:await driver.close()return MemoryScopeOverview(@@ -149,9 +155,9 @@ async def _list_facts_impl(driver = open_driver(scope)try:result = await driver.execute_query(- """+ f"""MATCH (src:Entity)-[e:RELATES_TO]->(tgt:Entity)- WHERE e.group_id = $g AND e.expired_at IS NULL+ WHERE e.group_id = $g AND {live_fact_predicate("e")}RETURN e.uuid AS uuid,e.fact AS fact,e.name AS name,@@ -209,33 +215,24 @@ async def _forget_fact_impl(user_id: str, expert_id: str | None, fact_uuid: str) -> ForgetFactResponse:scope = await _resolve_scope(user_id, expert_id)- driver = open_driver(scope)- try:- # Same retraction the chat forget tool performs (``expired_at`` only —- # a system retraction, not a world change), plus a ``group_id``- # predicate as defense-in-depth on top of the per-group database.- result = await driver.execute_query(- """- MATCH ()-[e:MENTIONS|RELATES_TO|HAS_MEMBER- {uuid: $uuid, group_id: $g}]->()- SET e.expired_at = $now- RETURN e.uuid AS uuid- """,- uuid=fact_uuid,- g=scope.group_id,- now=_now_iso(),+ # The chat forget tool's soft retraction (a system retraction, not a+ # world change), so the fact and its episode text stop being recalled.+ result = await retract(scope, [fact_uuid])+ codes = {failure.code for failure in result.failures}+ if MemoryForgetFailureCode.BUSY in codes: # every uuid busy, nothing written+ raise HTTPException(status_code=409, detail=result.failures[0].reason)+ if MemoryForgetFailureCode.CLEANUP_ERROR in codes:+ # Recall already hides the fact and its text, but the redaction did+ # not land; a retry finishes it, so this is not reported as done.+ raise HTTPException(+ status_code=500,+ detail="Forgot this memory but could not finish cleaning up; try again",)- records = result[0] if result else []- except ResponseError as exc:- if not _is_missing_graph_error(exc):- raise- records = []- finally:- await driver.close()-- if not records:- raise HTTPException(status_code=404, detail="Memory not found")- return ForgetFactResponse(uuid=fact_uuid, forgotten=True)+ if result.deleted:+ return ForgetFactResponse(uuid=fact_uuid, forgotten=True)+ if MemoryForgetFailureCode.QUERY_ERROR in codes:+ raise HTTPException(status_code=500, detail="Could not forget this memory")+ raise HTTPException(status_code=404, detail="Memory not found")@router.delete("/facts/{fact_uuid}", operation_id="forget_my_memory_fact")diff --git a/autogpt_platform/backend/backend/api/features/memory/routes_test.py b/autogpt_platform/backend/backend/api/features/memory/routes_test.pyindex eb7d724308..3bad82f918 100644--- a/autogpt_platform/backend/backend/api/features/memory/routes_test.py+++ b/autogpt_platform/backend/backend/api/features/memory/routes_test.py@@ -10,6 +10,13 @@ from redis.exceptions import ResponseErrorfrom backend.api.features.experts.models import PROTECTED_SOUL_RULES, Expertfrom backend.copilot.graphiti.client import derive_memory_group_id+from backend.copilot.graphiti.memory_model import (+ FORGET_BUSY_REASON,+ ForgetResult,+ MemoryForgetFailure,+)+from backend.copilot.graphiti.recall import live_fact_predicate+from backend.copilot.graphiti.scope import MemoryScopefrom .routes import router as memory_router@@ -84,11 +91,20 @@ class TestOverview:assert resp.status_code == 403def test_fact_count_only_counts_live_edges(self) -> None:+ """Counted with recall's own live-fact test, so the number shown is+ the number of facts the assistant can still recall."""driver = _driver_returning([{"c": 0}], [{"c": 0}], [{"c": 0}])with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):client.get("/memory/overview")fact_query = driver.execute_query.await_args_list[0].args[0]- assert "expired_at IS NULL" in fact_query+ assert live_fact_predicate("e") in fact_query++ def test_episode_count_leaves_out_hard_forget_tombstones(self) -> None:+ driver = _driver_returning([{"c": 0}], [{"c": 0}], [{"c": 0}])+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):+ client.get("/memory/overview")+ episode_query = driver.execute_query.await_args_list[2].args[0]+ assert "MATCH (n:Episodic) WHERE n.hard_deleted_at IS NULL" in episode_queryclass TestListFacts:@@ -113,7 +129,7 @@ class TestListFacts:assert body["items"][0]["uuid"] == "edge-1"assert body["items"][0]["fact"] == "Prefers Monday summaries"query = driver.execute_query.await_args_list[0].args[0]- assert "expired_at IS NULL" in query+ assert live_fact_predicate("e") in queryassert "ORDER BY e.created_at DESC" in querydef test_missing_graph_is_empty_list(self) -> None:@@ -158,23 +174,65 @@ class TestListFacts:class TestForgetFact:- def test_retracts_matching_edge(self) -> None:- driver = _driver_returning([{"uuid": "edge-1"}])- with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):+ """The settings forget is the chat tool's soft ``retract`` (its Cypher is+ pinned in ``graphiti/recall_forget_test.py``)."""++ def test_retracts_matching_edge(self, test_user_id) -> None:+ retract = AsyncMock(return_value=ForgetResult(deleted=["edge-1"]))+ with patch(f"{_MOCK_MODULE}.retract", retract):resp = client.delete("/memory/facts/edge-1")assert resp.status_code == 200assert resp.json() == {"uuid": "edge-1", "forgotten": True}- query = driver.execute_query.await_args_list[0].args[0]- assert "SET e.expired_at = $now" in query- assert "group_id: $g" in query- assert "DELETE" not in query+ retract.assert_awaited_once_with(MemoryScope.for_user(test_user_id), ["edge-1"])def test_no_match_is_404(self) -> None:- driver = _driver_returning([])- with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):+ result = ForgetResult(failures=[MemoryForgetFailure.no_match("edge-unknown")])+ with patch(f"{_MOCK_MODULE}.retract", AsyncMock(return_value=result)):resp = client.delete("/memory/facts/edge-unknown")assert resp.status_code == 404+ def test_query_error_is_500_not_404(self) -> None:+ failure = MemoryForgetFailure.query_error("edge-1", RuntimeError("down"))+ result = ForgetResult(failures=[failure])+ with patch(f"{_MOCK_MODULE}.retract", AsyncMock(return_value=result)):+ resp = client.delete("/memory/facts/edge-1")+ assert resp.status_code == 500++ def test_failed_clean_up_is_500_not_success(self) -> None:+ """Retracted, but the episode redaction did not land. Recall hides+ the text regardless; the page still must not show a finished forget,+ and a retry finishes it."""+ failure = MemoryForgetFailure.cleanup_error("edge-1", RuntimeError("down"))+ result = ForgetResult(deleted=["edge-1"], failures=[failure])+ with patch(f"{_MOCK_MODULE}.retract", AsyncMock(return_value=result)):+ resp = client.delete("/memory/facts/edge-1")+ assert resp.status_code == 500+ assert "try again" in resp.json()["detail"]++ def test_memory_being_written_is_409_and_nothing_forgotten(self) -> None:+ """An ingestion held the graph's write lock for the whole wait: the+ forget wrote nothing, and the page can try again."""+ result = ForgetResult(failures=[MemoryForgetFailure.busy("edge-1")])+ with patch(f"{_MOCK_MODULE}.retract", AsyncMock(return_value=result)):+ resp = client.delete("/memory/facts/edge-1")+ assert resp.status_code == 409+ assert resp.json()["detail"] == FORGET_BUSY_REASON++ def test_expert_forget_uses_expert_scope(self, test_user_id) -> None:+ retract = AsyncMock(return_value=ForgetResult(deleted=["edge-1"]))+ with (+ patch(f"{_MOCK_MODULE}.retract", retract),+ patch(+ f"{_MOCK_MODULE}.experts_db.get_expert",+ new=AsyncMock(return_value=_expert()),+ ),+ ):+ resp = client.delete(f"/memory/experts/{_EXPERT_ID}/facts/edge-1")+ assert resp.status_code == 200+ retract.assert_awaited_once_with(+ MemoryScope.for_expert(test_user_id, _EXPERT_ID), ["edge-1"]+ )+class TestEraseScope:def test_erases_all_nodes(self) -> None:diff --git a/autogpt_platform/backend/backend/copilot/dream/apply.py b/autogpt_platform/backend/backend/copilot/dream/apply.pyindex ee0b68e033..b480a57424 100644--- a/autogpt_platform/backend/backend/copilot/dream/apply.py+++ b/autogpt_platform/backend/backend/copilot/dream/apply.py@@ -19,6 +19,7 @@ guard at the top of ``apply_operations``.from __future__ import annotations+import asyncioimport loggingimport uuid as uuidlibfrom collections.abc import Mapping@@ -36,6 +37,7 @@ from backend.copilot.graphiti.memory_model import (MemoryStatus,SourceKind,)+from backend.copilot.graphiti.recall_citations import Citationsfrom backend.copilot.graphiti.scope import MemoryScopefrom backend.copilot.tools.graphiti_forget import (invalidate_entity_direct_neighbors,@@ -158,6 +160,26 @@ def _edge_metadata(envelope: MemoryEnvelope) -> dict:}+def _citations(+ statement: str,+ fact_uuids: list[str],+ episode_uuids: list[str],+ read: Citations | None,+) -> Citations:+ """What a dream write rests on, which the ingestion worker checks against+ forgets right before writing it (``graphiti/recall_citations.py``): what+ it cites or, citing nothing, everything its pass read (``read``) and its+ own statement, since an uncited claim may come from any of it."""+ if fact_uuids or episode_uuids:+ return Citations(fact_uuids=list(fact_uuids), episode_uuids=list(episode_uuids))+ everything = read or Citations()+ return Citations(+ fact_uuids=everything.fact_uuids,+ episode_uuids=everything.episode_uuids,+ statement=statement,+ )++async def _write_consolidated_fact(scope: MemoryScope,pass_id: str,@@ -165,6 +187,8 @@ async def _write_consolidated_fact(fact: ConsolidatedFact,session_id: str,completion: IngestionCompletion,+ *,+ read: Citations | None = None,) -> bool:envelope = MemoryEnvelope(content=fact.content,@@ -187,6 +211,7 @@ async def _write_consolidated_fact(is_json=True,edge_metadata=_edge_metadata(envelope),completion=completion,+ citations=_citations(fact.content, [], fact.source_episode_uuids, read),)@@ -197,6 +222,8 @@ async def _write_proposed_finding(finding: ProposedFinding,session_id: str,completion: IngestionCompletion,+ *,+ read: Citations | None = None,) -> bool:envelope = MemoryEnvelope(content=finding.content,@@ -221,6 +248,12 @@ async def _write_proposed_finding(is_json=True,edge_metadata=_edge_metadata(envelope),completion=completion,+ citations=_citations(+ finding.content,+ finding.source_fact_uuids,+ finding.source_episode_uuids,+ read,+ ),)@@ -386,8 +419,8 @@ async def _create_dream_session(scope: MemoryScope, pass_id: str) -> str:Written up front (before the memory ops) because the fact/proposal``MemoryEnvelope`` provenance references this ``session_id``. The- user-facing narrative is written separately, AFTER the ops land- (``_write_dream_summary_message``), so a partway failure leaves an+ user-facing narrative is written separately, AFTER the ops are+ attempted (``_write_dream_summary_message``), so a partway failure leaves anempty dream rather than a 'completed' narrative with no memory.We use a fresh uuid rather than the pass_id so re-runs of the same@@ -473,9 +506,13 @@ async def _write_dream_summary_message(async def _drain_ingestion(- pass_id: str, completion: IngestionCompletion, timeout_seconds: float+ pass_id: str,+ trackers: tuple[IngestionCompletion, ...],+ timeout_seconds: float,) -> IngestionDrainStatus:- """Wait for the dream's OWN enqueued episodes to land in the graph.+ """Wait for the worker to finish the dream's OWN enqueued episodes, on+ each of *trackers* at once: each written, dropped for resting on a+ forget, or failed.``enqueue_episode`` returning True only proves the episode reached thein-process asyncio queue; the real write (LLM extraction + embedding in@@ -485,40 +522,45 @@ async def _drain_ingestion(silently discards queued writes while the pass stays recordedsuccessful.- Scoped to ``completion`` — only the episodes THIS pass enqueued. The+ Scoped to *trackers* — only the episodes THIS pass enqueued. Theper-user queue is shared with live-chat ingestion, so a whole-queuebarrier would let a user's concurrent chat activity extend the in-lockhold up to the full timeout (and items enqueued after the drain startswould never let it resolve). Tracking the pass's own episodes makes the- drain resolve the instant they land, regardless of other queue traffic.+ drain resolve the instant they are finished, regardless of other queue+ traffic.Returns:- * ``drained`` — nothing was enqueued (vacuous) or all of the pass's- episodes landed within the timeout.+ * ``drained`` — nothing was enqueued (vacuous) or the worker finished+ all of the pass's episodes within the timeout: written, dropped for+ resting on a forget, or failed with a logged error.* ``skipped`` — ``timeout_seconds <= 0``; the batch path uses this toavoid stalling the shared, serial ``BatchExecutor.walk_once`` loop(see ``BATCH_INGESTION_DRAIN_TIMEOUT_SECONDS``). The episodes still- process fire-and-forget in the executor process.- * ``timed_out`` — the episodes did not all land within the timeout.- They keep processing fire-and-forget; apply warns rather than- failing the pass — partial visibility beats a failed pass.+ process fire-and-forget in the executor process, counted pending.+ * ``timed_out`` — the worker did not finish all the episodes within+ the timeout.+ They keep processing fire-and-forget, counted pending, not written;+ apply warns rather than failing the pass — partial visibility beats+ a failed pass."""- if not completion.registered:+ waiting = [tracker for tracker in trackers if tracker.registered]+ if not waiting:return IngestionDrainStatus.drainedif timeout_seconds <= 0:logger.info("Dream pass %s: ingestion drain skipped (no wait) — %d episode(s) ""processing fire-and-forget; reporting drain status=skipped",pass_id,- completion.registered,+ sum(tracker.registered for tracker in waiting),)return IngestionDrainStatus.skipped- drained = await wait_for_ingestion(completion, timeout_seconds)- if drained:+ waits = [wait_for_ingestion(tracker, timeout_seconds) for tracker in waiting]+ if all(await asyncio.gather(*waits)):return IngestionDrainStatus.drainedlogger.warning("Dream pass %s: own ingestion episodes did not drain within %.0fs — "- "reported write/proposal counts include episodes still queued "+ "those not written yet are reported pending; they stay queued ""in-process (lost if this pod restarts)",pass_id,timeout_seconds,@@ -532,6 +574,7 @@ async def apply_operations(ops: DreamOperations,*,known_fact_uuids: set[str] | None = None,+ known_episode_uuids: set[str] | None = None,ingestion_drain_timeout: float = INGESTION_DRAIN_TIMEOUT_SECONDS,lock_handle: DreamLockHandle | None = None,) -> dict[str, int | str | IngestionDrainStatus | DreamOperationsSnapshot]:@@ -542,10 +585,17 @@ async def apply_operations(detailed ``DreamOperationsSnapshot`` payload for consumers thatneed per-operation rollups (eval, admin UI, future P9 SSE event),and an ``ingestion_drain_status`` (``IngestionDrainStatus``) —- ``timed_out`` means the write/proposal counts were reported while- episodes were still queued in-process, ``skipped`` is the by-design- batch skip, ``drained`` is a fully-landed pass (see- ``_drain_ingestion``). Read it back via ``drain_status_from_stats``.+ ``timed_out`` means episodes were still queued in-process when the pass+ was reported, ``skipped`` is the by-design batch skip, ``drained`` is a+ pass whose writes the worker all finished (see ``_drain_ingestion``).+ Read it back via ``drain_status_from_stats``.++ ``consolidated_count`` and ``proposal_count`` count the writes and+ proposals graphiti saved by the time apply returned, never one merely+ queued: those not written yet are ``pending_writes`` (every one on+ the batch path, which does not wait), those whose write raised+ ``failed_writes``, those dropped for resting on a forget+ ``dropped_forgotten``.An empty pass — no writes, proposals, demotions, or entityinvalidations — returns zero counts and an empty snapshot WITHOUT@@ -553,8 +603,8 @@ async def apply_operations(``session_id`` key is absent so ``apply_stats.get("session_id")``reads as ``None`` for both the orchestrator and the batch callback.A pass WITH operations but an empty ``summary_for_user`` still- creates the session and writes the fallback narrative (the ops- landed; only the narrative is missing).+ creates the session and writes the fallback narrative (the ops were+ attempted; only the narrative is missing).``known_fact_uuids`` is the set of edge uuids the dream passactually fetched (``DreamInput.known_fact_uuids``); demotions@@ -563,15 +613,23 @@ async def apply_operations(up the persisted input bundle by pass_id" — the batch path'scallbacks rely on that fallback.- ``ingestion_drain_timeout`` bounds the in-line wait for the enqueued- episodes to land (see ``_drain_ingestion``). The sync path keeps the+ Each write and proposal is queued with what it cites; one that cites+ nothing is taken to rest on everything the pass read, its+ ``known_fact_uuids`` and ``known_episode_uuids``. The ingestion worker+ drops, unwritten, any whose citations a forget reached after the pass+ read the graph (``graphiti/recall_citations.py``). ``dropped_forgotten``+ counts those dropped before apply returned: all of them on a drained+ pass, possibly fewer when the drain was skipped or timed out.++ ``ingestion_drain_timeout`` bounds the in-line wait for the worker to+ finish the enqueued episodes (see ``_drain_ingestion``). The sync path keeps thefull ``INGESTION_DRAIN_TIMEOUT_SECONDS``; the batch path passes``BATCH_INGESTION_DRAIN_TIMEOUT_SECONDS`` (0) so it never stalls theshared, serial ``BatchExecutor.walk_once`` loop.``lock_handle`` is the sync path's held dream lock. It is renewed to afresh ``LOCK_DRAIN_RENEWAL_SECONDS`` budget right before the drain so- the lock cannot expire while the writes are still landing (which would+ the lock cannot expire while the writes are still being processed (which wouldadmit a concurrent pass onto the same graph). ``None`` on the batchpath — it already disowned the lock to its callback with a 24h TTL.@@ -605,6 +663,9 @@ async def apply_operations("demotion_count": 0,"demotion_failed_count": 0,"entity_invalidation_count": 0,+ "dropped_forgotten": 0,+ "pending_writes": 0,+ "failed_writes": 0,# Vacuously drained — the pass enqueued nothing."ingestion_drain_status": IngestionDrainStatus.drained,"snapshot": DreamOperationsSnapshot(),@@ -616,13 +677,18 @@ async def apply_operations(# empty dream rather than a 'completed' narrative with no memory.session_id = await _create_dream_session(scope, pass_id)- # Tracks completion of only the episodes THIS pass enqueues, so the- # drain below waits on the dream's own writes and not on unrelated- # live-chat ingestion sharing the same per-user queue. Registered once- # per successful enqueue; the worker signals each as it lands.- completion = IngestionCompletion()+ # Track only the episodes THIS pass enqueues, its writes on one tracker+ # and its proposals on another: the drain below waits on the dream's own+ # episodes, not on unrelated live-chat ingestion sharing the same+ # per-user queue, and the pass reports as written only what graphiti+ # saved of each. Registered once per successful enqueue; the worker+ # records what became of each episode as it finishes it.+ writes_done, proposals_done = IngestionCompletion(), IngestionCompletion()+ read = Citations(+ fact_uuids=sorted(known_fact_uuids or ()),+ episode_uuids=sorted(known_episode_uuids or ()),+ )- written = 0write_summaries: list[WriteSummary] = []for i, fact in enumerate(ops.writes):if await _write_consolidated_fact(@@ -631,10 +697,10 @@ async def apply_operations(i,fact,session_id=session_id,- completion=completion,+ completion=writes_done,+ read=read,):- completion.register()- written += 1+ writes_done.register()write_summaries.append(WriteSummary(content=fact.content,@@ -645,7 +711,6 @@ async def apply_operations())- proposed = 0proposal_summaries: list[WriteSummary] = []for i, prop in enumerate(ops.proposals):if await _write_proposed_finding(@@ -654,10 +719,10 @@ async def apply_operations(i,prop,session_id=session_id,- completion=completion,+ completion=proposals_done,+ read=read,):- completion.register()- proposed += 1+ proposals_done.register()proposal_summaries.append(WriteSummary(content=prop.content,@@ -669,22 +734,6 @@ async def apply_operations())- # One episode was registered per successful enqueue, so the tracker's- # count is exactly the writes + proposals we report — the drain waits on- # precisely those and nothing else. A mismatch does not endanger the- # writes (the drain would just resolve early or wait out its cap), so- # log loudly instead of failing a pass that has already written.- if completion.registered != written + proposed:- logger.error(- "Dream pass %s: ingestion tracker registered %d episode(s) but "- "reported %d write(s) + %d proposal(s) — drain barrier is not "- "scoped to exactly the reported writes",- pass_id,- completion.registered,- written,- proposed,- )-# Renew the dream lock right before the longest non-LLM tail (the# ingestion drain plus the demotions / summary write that follow) so the# lock cannot expire mid-write and let a second pass touch the same@@ -697,17 +746,18 @@ async def apply_operations(# drain and the destructive writes below. The episodes already enqueued# above keep processing fire-and-forget (they cannot be recalled), but# the pass is reported errored instead of pretending exclusive ownership.+ queued = writes_done.registered + proposals_done.registeredif lock_handle is not None and (- completion.registered or ops.demotions or ops.entity_invalidations+ queued or ops.demotions or ops.entity_invalidations):if not await lock_handle.extend(LOCK_DRAIN_RENEWAL_SECONDS):raise DreamLockLostError(user_id)# Drain the in-process ingestion queue before anything downstream- # treats the writes as landed (and before we return and the caller+ # treats the writes as finished (and before we return and the caller# releases the dream lock). See ``_drain_ingestion``.ingestion_drain_status = await _drain_ingestion(- pass_id, completion, ingestion_drain_timeout+ pass_id, (writes_done, proposals_done), ingestion_drain_timeout)demotions = await _filter_demotions_to_known_facts(@@ -734,14 +784,21 @@ async def apply_operations(# once the memory ops above have been attempted.await _write_dream_summary_message(session_id, pass_id, ops.summary_for_user)+ trackers = (writes_done, proposals_done)+ dropped = sum(tracker.dropped_forgotten for tracker in trackers)+ pending = sum(tracker.pending for tracker in trackers)+ failed = sum(tracker.failed for tracker in trackers)logger.info("Dream pass %s applied for user %s: "- "writes=%d proposals=%d demoted=%d (failed=%d) entity_edges=%d "- "ingestion_drain_status=%s",+ "writes=%d proposals=%d dropped_forgotten=%d pending=%d failed=%d "+ "demoted=%d (failed=%d) entity_edges=%d ingestion_drain_status=%s",pass_id,user_id[:12],- written,- proposed,+ writes_done.written,+ proposals_done.written,+ dropped,+ pending,+ failed,demoted_ok,demoted_fail,entity_edges_demoted,@@ -757,11 +814,19 @@ async def apply_operations(return {"session_id": session_id,- "consolidated_count": written,- "proposal_count": proposed,+ # The writes and proposals graphiti saved by the time apply returned.+ "consolidated_count": writes_done.written,+ "proposal_count": proposals_done.written,"demotion_count": demoted_ok,"demotion_failed_count": demoted_fail,"entity_invalidation_count": entity_edges_demoted,+ # Writes and proposals dropped unwritten: a forget reached what they+ # rest on after the pass read the graph.+ "dropped_forgotten": dropped,+ # Writes and proposals not written yet (queued, waiting for the+ # graph's lock, or being written), and those whose write failed.+ "pending_writes": pending,+ "failed_writes": failed,"ingestion_drain_status": ingestion_drain_status,"snapshot": snapshot,}diff --git a/autogpt_platform/backend/backend/copilot/dream/apply_citations_test.py b/autogpt_platform/backend/backend/copilot/dream/apply_citations_test.pynew file mode 100644index 0000000000..f77aea93c1--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/apply_citations_test.py@@ -0,0 +1,136 @@+"""apply hands every dream write what it rests on, for the ingestion worker+to check against forgets right before writing it+(``graphiti/recall_citations.py``), and reports the writes it dropped.++The live runs, a forget between the dream's read and its writes, are+``graphiti/recall_dream_citations_integration_test.py``.+"""++from unittest.mock import AsyncMock++import pytest++from backend.copilot.graphiti.ingest import IngestionCompletion+from backend.copilot.graphiti.recall_citations import Citations+from backend.copilot.graphiti.scope import MemoryScope++from . import apply as apply_mod+from .schemas import ConsolidatedFact, DreamOperations, ProposedFinding++_SCOPE = MemoryScope.for_user("u-1234567890ab")++[email protected](autouse=True)+def enqueue(mocker) -> AsyncMock:+ queued = AsyncMock(return_value=True)+ mocker.patch.object(apply_mod, "enqueue_episode", queued)+ mocker.patch.object(apply_mod, "wait_for_ingestion", AsyncMock(return_value=True))+ mocker.patch.object(apply_mod, "_create_dream_session", AsyncMock(return_value="s"))+ mocker.patch.object(apply_mod, "_write_dream_summary_message", AsyncMock())+ return queued+++def _cited(enqueue: AsyncMock) -> list[Citations]:+ return [call.kwargs["citations"] for call in enqueue.await_args_list]+++async def test_each_write_carries_what_it_cites(enqueue: AsyncMock) -> None:+ ops = DreamOperations(+ writes=[+ ConsolidatedFact(+ content="Alice works on Atlas",+ confidence=0.9,+ source_episode_uuids=["ep1"],+ )+ ],+ proposals=[+ ProposedFinding(+ content="Alice may lead Atlas",+ confidence=0.5,+ rationale="implied",+ source_fact_uuids=["f1"],+ source_episode_uuids=["ep2"],+ )+ ],+ )++ await apply_mod.apply_operations(+ _SCOPE, "p1", ops, known_fact_uuids={"f1", "f9"}, known_episode_uuids={"ep1"}+ )++ assert _cited(enqueue) == [+ Citations(episode_uuids=["ep1"]),+ Citations(fact_uuids=["f1"], episode_uuids=["ep2"]),+ ]+++async def test_a_write_citing_nothing_rests_on_everything_the_pass_read(+ enqueue: AsyncMock,+) -> None:+ ops = DreamOperations(+ writes=[ConsolidatedFact(content="Alice works on Atlas", confidence=0.9)],+ proposals=[+ ProposedFinding(content="Alice leads", confidence=0.5, rationale="r")+ ],+ )++ await apply_mod.apply_operations(+ _SCOPE, "p1", ops, known_fact_uuids={"f2", "f1"}, known_episode_uuids={"ep1"}+ )++ assert _cited(enqueue) == [+ Citations(+ fact_uuids=["f1", "f2"],+ episode_uuids=["ep1"],+ statement="Alice works on Atlas",+ ),+ Citations(+ fact_uuids=["f1", "f2"], episode_uuids=["ep1"], statement="Alice leads"+ ),+ ]+++async def test_without_what_the_pass_read_it_is_checked_by_its_statement(+ enqueue: AsyncMock,+) -> None:+ fact = ConsolidatedFact(content="Alice works on Atlas", confidence=0.9)++ await apply_mod._write_consolidated_fact(+ _SCOPE, "p1", 0, fact, "s", IngestionCompletion()+ )++ assert _cited(enqueue) == [Citations(statement="Alice works on Atlas")]+++async def test_the_writes_the_worker_dropped_are_reported(mocker) -> None:+ """A dropped write is reported dropped, not consolidated."""++ async def worker_dropped_one(completion: IngestionCompletion, _: float) -> bool:+ completion.dropped_forgotten += 1+ completion.written = completion.registered - 1+ return True++ mocker.patch.object(apply_mod, "wait_for_ingestion", worker_dropped_one)+ ops = DreamOperations(+ writes=[+ ConsolidatedFact(content="one", confidence=0.9),+ ConsolidatedFact(content="two", confidence=0.9),+ ]+ )++ stats = await apply_mod.apply_operations(_SCOPE, "p1", ops)++ assert stats["consolidated_count"] == 1+ assert stats["dropped_forgotten"] == 1+ assert stats["pending_writes"] == 0+++async def test_an_empty_pass_drops_nothing() -> None:+ stats = await apply_mod.apply_operations(_SCOPE, "p1", DreamOperations())++ assert stats["dropped_forgotten"] == 0diff --git a/autogpt_platform/backend/backend/copilot/dream/apply_test.py b/autogpt_platform/backend/backend/copilot/dream/apply_test.pyindex f46872acce..cf57046cea 100644--- a/autogpt_platform/backend/backend/copilot/dream/apply_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/apply_test.py@@ -43,6 +43,11 @@ def _bundle_with_known_facts(*uuids: str) -> DreamInput:)+async def _worker_wrote_them(completion: IngestionCompletion, _: float) -> bool:+ completion.written = completion.registered+ return True++@pytest.fixture(autouse=True)def _stub_boundaries(mocker):"""Wire up the apply.py side-effects with AsyncMocks once per test."""@@ -51,8 +56,11 @@ def _stub_boundaries(mocker):# on the pass's IngestionCompletion — but no worker runs in tests, so an# unstubbed drain would block the full INGESTION_DRAIN_TIMEOUT_SECONDS# (300s) per test and time out the CI job. Default the drain to an- # instant success; drain-behavior tests re-patch this explicitly.- mocker.patch.object(apply_mod, "wait_for_ingestion", AsyncMock(return_value=True))+ # instant success, every episode written, as the worker would report it;+ # drain-behavior tests re-patch this explicitly.+ mocker.patch.object(+ apply_mod, "wait_for_ingestion", AsyncMock(side_effect=_worker_wrote_them)+ )# The driver factory + close — apply.py opens a FalkorDB driver for# demotions and entity invalidations. Patch where it's used.driver = mocker.MagicMock()@@ -605,10 +613,19 @@ async def test_drain_timeout_reports_partial_visibility_not_failure(mocker, capl"""When the pass's episodes don't drain inside the cap, the pass muststill succeed (partial visibility beats a failed pass): counts arereturned, ingestion_drain_status=timed_out flags the overflow, and a- WARNING records the revert to fire-and-forget behavior."""- mocker.patch.object(apply_mod, "wait_for_ingestion", AsyncMock(return_value=False))+ WARNING records the revert to fire-and-forget behavior. What the worker+ has not written yet is reported pending, never consolidated."""++ async def wrote_one_of_two(completion: IngestionCompletion, _: float) -> bool:+ completion.written = 1+ return False++ mocker.patch.object(apply_mod, "wait_for_ingestion", wrote_one_of_two)ops = DreamOperations(- writes=[ConsolidatedFact(content="A likes B", confidence=0.8)],+ writes=[+ ConsolidatedFact(content="A likes B", confidence=0.8),+ ConsolidatedFact(content="B likes C", confidence=0.8),+ ],summary_for_user="ok",)with caplog.at_level(logging.WARNING, logger=apply_mod.logger.name):@@ -618,6 +635,7 @@ async def test_drain_timeout_reports_partial_visibility_not_failure(mocker, caplassert stats["ingestion_drain_status"] is IngestionDrainStatus.timed_outassert stats["consolidated_count"] == 1+ assert (stats["pending_writes"], stats["failed_writes"]) == (1, 0)assert any("did not drain" in record.getMessage()for record in caplog.records@@ -625,6 +643,40 @@ async def test_drain_timeout_reports_partial_visibility_not_failure(mocker, capl)+async def test_counts_only_what_the_worker_wrote_of_each_kind(mocker):+ """Writes and proposals are tracked apart, and each counted only once+ graphiti saved it: of two writes one failed, and the proposal was still+ queued when the drain ran out, so one write is consolidated, one failed+ and one pending, and no proposal counted."""++ async def worker(completion: IngestionCompletion, _: float) -> bool:+ if completion.registered == 2:+ completion.written, completion.failed = 1, 1+ return True+ return False++ mocker.patch.object(apply_mod, "wait_for_ingestion", worker)+ ops = DreamOperations(+ writes=[+ ConsolidatedFact(content="A likes B", confidence=0.8),+ ConsolidatedFact(content="B likes C", confidence=0.8),+ ],+ proposals=[+ ProposedFinding(content="A may like C", confidence=0.5, rationale="r")+ ],+ summary_for_user="ok",+ )++ stats = await apply_mod.apply_operations(+ scope=MemoryScope.for_user("u-kinds"), pass_id="p-kinds", ops=ops+ )++ assert stats["ingestion_drain_status"] is IngestionDrainStatus.timed_out+ assert (stats["consolidated_count"], stats["proposal_count"]) == (1, 0)+ assert (stats["failed_writes"], stats["pending_writes"]) == (1, 1)++@pytest.mark.asyncioasync def test_zero_drain_timeout_skips_wait_and_reports_skipped(mocker, caplog):"""The batch path passes ``ingestion_drain_timeout=0`` so apply never@@ -648,7 +700,8 @@ async def test_zero_drain_timeout_skips_wait_and_reports_skipped(mocker, caplog)drain.assert_not_awaited()assert stats["ingestion_drain_status"] is IngestionDrainStatus.skipped- assert stats["consolidated_count"] == 1+ # Not waited for, so not known written: counted pending, not consolidated.+ assert (stats["consolidated_count"], stats["pending_writes"]) == (0, 1)assert any("drain skipped" in record.getMessage() for record in caplog.records)diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.pyindex 7893ca58e8..ffa3ca2012 100644--- a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py+++ b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py@@ -202,12 +202,14 @@ async def _finalize_stuck_duplicate(existing.state,)# The first delivery's per-edge outcomes (and dream session id)- # died with it, so the counts here are the clamped *attempted*- # ops — annotate the summary so the admin UI doesn't present- # them as confirmed apply results.+ # died with it: the writes and proposals it queued are counted+ # pending, none written, and the demotion counts are the clamped+ # *attempted* ops — annotate the summary so the admin UI doesn't+ # present them as confirmed apply results.note = (- "[finalized after duplicate delivery — counts reflect attempted "- "operations; writes landed with the original delivery] "+ "[finalized after duplicate delivery — writes and proposals were "+ "queued by the original delivery, counted pending; demotion counts "+ "reflect attempted operations] ")await mark_complete(kind="dream_pass",@@ -216,8 +218,7 @@ async def _finalize_stuck_duplicate(user_id=user_id,pass_id=pass_id,execution_path="anthropic_batch",- consolidated_count=len(ops.writes),- proposal_count=len(ops.proposals),+ pending_writes=len(ops.writes) + len(ops.proposals),demotion_count=len(ops.demotions),entity_invalidation_count=len(ops.entity_invalidations),# Batch apply never drains in-line by design; the first@@ -814,6 +815,7 @@ async def _finalize_complete(pass_id,ops,known_fact_uuids=input_bundle.known_fact_uuids,+ known_episode_uuids=input_bundle.known_episode_uuids,ingestion_drain_timeout=BATCH_INGESTION_DRAIN_TIMEOUT_SECONDS,)except Exception as exc:@@ -881,6 +883,9 @@ async def _finalize_complete(proposal_count=_count("proposal_count"),demotion_count=_count("demotion_count"),entity_invalidation_count=_count("entity_invalidation_count"),+ dropped_forgotten=_count("dropped_forgotten"),+ pending_writes=_count("pending_writes"),+ failed_writes=_count("failed_writes"),dream_session_id=session_id,ingestion_drain_status=ingestion_drain_status,operations=snapshot,diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.pyindex d225e415ae..e51964e447 100644--- a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py@@ -424,6 +424,7 @@ class TestPhaseChaining:window_start=now,window_end=now,known_fact_uuids={"fact-1"},+ known_episode_uuids={"episode-1"},),)await _write_phase_to_state(@@ -467,6 +468,7 @@ class TestPhaseChaining:# The demotion allowlist is threaded from the bundle already loaded# for the clamp — apply must not re-read the bundle from Redis.assert apply.call_args.kwargs["known_fact_uuids"] == {"fact-1"}+ assert apply.call_args.kwargs["known_episode_uuids"] == {"episode-1"}# The batch path must NOT run the 300s in-line ingestion drain: apply# executes inside this handler, which BatchExecutor.walk_once awaits# serially — a long drain would stall every other user's batch poll.diff --git a/autogpt_platform/backend/backend/copilot/dream/fake_redis_test.py b/autogpt_platform/backend/backend/copilot/dream/fake_redis_test.pyindex 3fe9656b64..357d8ba675 100644--- a/autogpt_platform/backend/backend/copilot/dream/fake_redis_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/fake_redis_test.py@@ -10,7 +10,7 @@ each call.import pytestfrom .conftest import FakeAsyncRedis-from .locks import _EXTEND_SCRIPT, _UNLOCK_SCRIPT+from .locks import EXTEND_SCRIPT, UNLOCK_SCRIPT@pytest.mark.asyncio@@ -62,11 +62,11 @@ async def test_hash_commands():async def test_eval_models_the_lock_scripts():redis = FakeAsyncRedis()await redis.set("dream:inflight:u1", "token-a", nx=True, ex=1800)- assert await redis.eval(_EXTEND_SCRIPT, 1, "dream:inflight:u1", "token-b", 60) == 0- assert await redis.eval(_EXTEND_SCRIPT, 1, "dream:inflight:u1", "token-a", 60) == 1+ assert await redis.eval(EXTEND_SCRIPT, 1, "dream:inflight:u1", "token-b", 60) == 0+ assert await redis.eval(EXTEND_SCRIPT, 1, "dream:inflight:u1", "token-a", 60) == 1assert await redis.ttl("dream:inflight:u1") == 60- assert await redis.eval(_UNLOCK_SCRIPT, 1, "dream:inflight:u1", "token-b") == 0- assert await redis.eval(_UNLOCK_SCRIPT, 1, "dream:inflight:u1", "token-a") == 1+ assert await redis.eval(UNLOCK_SCRIPT, 1, "dream:inflight:u1", "token-b") == 0+ assert await redis.eval(UNLOCK_SCRIPT, 1, "dream:inflight:u1", "token-a") == 1assert await redis.get("dream:inflight:u1") is Nonewith pytest.raises(NotImplementedError):await redis.eval("return 1", 2, "a", "b", "c")diff --git a/autogpt_platform/backend/backend/copilot/dream/fetch.py b/autogpt_platform/backend/backend/copilot/dream/fetch.pyindex 669fe760aa..e6386398fd 100644--- a/autogpt_platform/backend/backend/copilot/dream/fetch.py+++ b/autogpt_platform/backend/backend/copilot/dream/fetch.py@@ -14,15 +14,24 @@ Caps (copied here so they live next to the code that enforces them):from __future__ import annotationsimport logging+from collections.abc import Collectionfrom datetime import datetime, timedelta, timezonefrom pydantic import BaseModel, Fieldfrom backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver, open_driver+from backend.copilot.graphiti.recall import (+ forgotten_facts_clause,+ live_fact_predicate,+ recallable_episode_predicate,+)from backend.copilot.graphiti.scope import MemoryScopefrom backend.copilot.hidden_rows import visible_to_model+from backend.copilot.model import ChatSessionInfofrom backend.data.db_accessors import chat_db+from .hidden_sessions import hidden_session_ids+logger = logging.getLogger(__name__)DEFAULT_WINDOW_DAYS = 14@@ -150,11 +159,17 @@ async def _fetch_recent_episodes(# which exceeds typical 14-day activity for any one user). The# ``window_start`` argument is kept for caller bookkeeping but the# Cypher itself just relies on the ORDER BY + LIMIT clamp.+ #+ # An episode the recall policy hides (redacted by a forget, or citing a+ # forgotten fact) is skipped like a retracted fact: consolidating its+ # text would re-derive the fact the user forgot._ = window_starttry:result = await driver.execute_query(- """- MATCH (n:Episodic {group_id: $g})+ forgotten_facts_clause()+ + f"""+ MATCH (n:Episodic {{group_id: $g}})+ WHERE {recallable_episode_predicate("n")}RETURN n.uuid AS uuid,n.name AS name,n.content AS content,@@ -193,12 +208,13 @@ async def _fetch_active_facts(group_id: str,limit: int,) -> list[FactRow]:+ # Active facts only, by recall's own live test: tentative, superseded,+ # contradicted, retracted and forgotten edges all fail it.try:result = await driver.execute_query(- """- MATCH (src:Entity)-[e:RELATES_TO {group_id: $g}]->(tgt:Entity)- WHERE (e.status IS NULL OR e.status = 'active')- AND (e.expired_at IS NULL)+ f"""+ MATCH (src:Entity)-[e:RELATES_TO {{group_id: $g}}]->(tgt:Entity)+ WHERE {live_fact_predicate("e", include_tentative=False)}RETURN e.uuid AS uuid,src.name AS source,tgt.name AS target,@@ -242,9 +258,15 @@ async def _fetch_recent_sessions(scope: MemoryScope,window_start: datetime,limit: int,+ *,+ hidden: Collection[str] | None = (),) -> list[SessionRow]:"""Pull the most recent N chat sessions and their first chunk of content.+ A ``hidden`` session (``hidden_sessions.py``) is left out. ``None``+ means that list could not be read; chat history is raw text a forget+ never reaches, so then no session is read at all.+Routes through ``chat_db()`` so the dream pass — which runs in theScheduler subprocess — uses the DatabaseManager RPC when Prismaisn't directly connected, and the in-process Postgres path when@@ -259,6 +281,8 @@ async def _fetch_recent_sessions(clamp does the bounding."""_ = window_start+ if hidden is None:+ return []user_id = scope.owner_user_idtry:if scope.expert_id is None:@@ -276,48 +300,55 @@ async def _fetch_recent_sessions(exc_info=True,)return []+ return [+ await _session_row(session, user_id)+ for session in sessions+ if session.session_id not in hidden+ ]- out: list[SessionRow] = []- for s in sessions:- # ``ChatSessionInfo`` exposes ``session_id`` (not ``id``) and- # carries no messages (it's a summary). Fetch the body lazily- # per-session via the paginated reader so the prompt has- # actual content. One round-trip per session is acceptable at- # limit=10; a bulk variant can replace this when chat_db- # gains one.- sid = s.session_id- messages: list = []- try:- paginated = await chat_db().get_chat_messages_paginated(- session_id=sid, limit=20, user_id=user_id- )- if paginated is not None:- messages = visible_to_model(getattr(paginated, "messages", []) or [])- except Exception:- logger.debug(- "Failed to fetch messages for session %s — body left empty",- sid[:12],- exc_info=True,- )- body_parts: list[str] = []- running_len = 0- for m in messages:- line = f"{m.role}: {m.content or ''}"- running_len += len(line) + 1- body_parts.append(line)- if running_len >= MAX_SESSION_BODY_BYTES:- break- body = "\n".join(body_parts)[:MAX_SESSION_BODY_BYTES]- out.append(- SessionRow(- session_id=sid,- title=s.title,- created_at=getattr(s, "createdAt", None)- or getattr(s, "created_at", None),- body=body,- )++async def _session_row(session: ChatSessionInfo, user_id: str) -> SessionRow:+ """One session's title and the start of its messages.++ ``ChatSessionInfo`` exposes ``session_id`` (not ``id``) and carries no+ messages (it's a summary). Fetch the body lazily per-session via the+ paginated reader so the prompt has actual content. One round-trip per+ session is acceptable at limit=10; a bulk variant can replace this when+ chat_db gains one.+ """+ sid = session.session_id+ messages: list = []+ try:+ paginated = await chat_db().get_chat_messages_paginated(+ session_id=sid, limit=20, user_id=user_id+ )+ if paginated is not None:+ messages = visible_to_model(paginated.messages)+ except Exception:+ logger.debug(+ "Failed to fetch messages for session %s — body left empty",+ sid[:12],+ exc_info=True,)- return out+ return SessionRow(+ session_id=sid,+ title=session.title,+ created_at=session.started_at,+ body=_session_body(messages),+ )+++def _session_body(messages: list) -> str:+ """``role: content`` lines, cut to ``MAX_SESSION_BODY_BYTES``."""+ body_parts: list[str] = []+ running_len = 0+ for m in messages:+ line = f"{m.role}: {m.content or ''}"+ running_len += len(line) + 1+ body_parts.append(line)+ if running_len >= MAX_SESSION_BODY_BYTES:+ break+ return "\n".join(body_parts)[:MAX_SESSION_BODY_BYTES]async def gather_dream_input(@@ -344,10 +375,13 @@ async def gather_dream_input(driver, group_id, window_start, max_episodes)facts = await _fetch_active_facts(driver, group_id, max_facts)+ hidden = await hidden_session_ids(driver, group_id)finally:await driver.close()- sessions = await _fetch_recent_sessions(scope, window_start, max_sessions)+ sessions = await _fetch_recent_sessions(+ scope, window_start, max_sessions, hidden=hidden+ )return DreamInput(user_id=scope.owner_user_id,diff --git a/autogpt_platform/backend/backend/copilot/dream/fetch_test.py b/autogpt_platform/backend/backend/copilot/dream/fetch_test.pyindex aa49afa92e..97d97abe8d 100644--- a/autogpt_platform/backend/backend/copilot/dream/fetch_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/fetch_test.py@@ -4,10 +4,16 @@ from unittest.mock import AsyncMockimport pytest+from backend.copilot.graphiti.recall import (+ forgotten_facts_clause,+ live_fact_predicate,+ recallable_episode_predicate,+)from backend.copilot.graphiti.scope import MemoryScope-from backend.copilot.model import ChatMessage+from backend.copilot.model import ChatMessage, ChatSessionInfofrom . import fetch as fetch_mod+from . import hidden_sessions@pytest.mark.asyncio@@ -38,11 +44,130 @@ async def test_expert_dream_fetches_only_that_experts_sessions(mocker):)+async def test_episode_gather_reads_only_recallable_episodes():+ driver = AsyncMock()+ driver.execute_query.return_value = ([], [], None)++ await fetch_mod._fetch_recent_episodes(+ driver, "user_g", datetime.now(timezone.utc), 50+ )++ query = driver.execute_query.await_args.args[0]+ assert query.startswith(forgotten_facts_clause())+ assert f"WHERE {recallable_episode_predicate('n')}" in query+++async def test_fact_gather_reads_live_facts_by_recalls_own_test():+ """A forgotten fact (``forgotten_at`` set) never reaches the dream, even+ one some writer left unexpired."""+ driver = AsyncMock()+ driver.execute_query.return_value = ([], [], None)++ await fetch_mod._fetch_active_facts(driver, "user_g", 50)++ query = driver.execute_query.await_args.args[0]+ assert f"WHERE {live_fact_predicate('e', include_tentative=False)}" in query+++def _chat_store(*session_ids: str) -> SimpleNamespace:+ message = ChatMessage(role="user", content="something the user said")+ return SimpleNamespace(+ get_user_chat_sessions=AsyncMock(+ return_value=[+ SimpleNamespace(+ session_id=sid,+ title=sid,+ started_at=datetime(2026, 10, 1, tzinfo=timezone.utc),+ )+ for sid in session_ids+ ]+ ),+ get_chat_messages_paginated=AsyncMock(+ return_value=SimpleNamespace(messages=[message])+ ),+ )+++def _graph(hidden_rows: list[dict] | Exception) -> AsyncMock:+ """A driver whose hidden-episode read returns (or raises) ``hidden_rows``;+ the episode and fact reads find nothing."""++ async def execute(query: str, **params: object):+ if query != hidden_sessions._HIDDEN_EPISODES_QUERY:+ return ([], [], None)+ if isinstance(hidden_rows, Exception):+ raise hidden_rows+ return (hidden_rows, [], None)++ driver = AsyncMock()+ driver.execute_query.side_effect = execute+ return driver+++async def test_the_dream_reads_no_session_a_forget_hid(mocker):+ forgotten = {+ "name": "conversation_s-gone",+ "source_description": "",+ "content": "",+ "provenance": None,+ }+ mocker.patch.object(fetch_mod, "open_driver", return_value=_graph([forgotten]))+ chat_store = _chat_store("s-gone", "s-kept")+ mocker.patch.object(fetch_mod, "chat_db", return_value=chat_store)++ bundle = await fetch_mod.gather_dream_input(MemoryScope.for_user("user-1"))++ assert [session.session_id for session in bundle.recent_sessions] == ["s-kept"]+ chat_store.get_chat_messages_paginated.assert_awaited_once_with(+ session_id="s-kept", limit=20, user_id="user-1"+ )+++async def test_the_dream_reads_no_session_when_it_cannot_tell_which_are_hidden(+ mocker,+):+ graph = _graph(RuntimeError("falkordb down"))+ mocker.patch.object(fetch_mod, "open_driver", return_value=graph)+ chat_store = _chat_store("s-1")+ mocker.patch.object(fetch_mod, "chat_db", return_value=chat_store)++ bundle = await fetch_mod.gather_dream_input(MemoryScope.for_user("user-1"))++ assert bundle.recent_sessions == []+ chat_store.get_user_chat_sessions.assert_not_awaited()+++async def test_session_row_preserves_typed_creation_time(mocker):+ started = datetime(2026, 10, 1, tzinfo=timezone.utc)+ session = ChatSessionInfo(+ session_id="s",+ user_id="u",+ title="title",+ usage=[],+ started_at=started,+ updated_at=started,+ )+ database = _chat_store("s")+ mocker.patch.object(fetch_mod, "chat_db", return_value=database)+ row = await fetch_mod._session_row(session, "u")+ assert row.created_at == started+ assert row.body == "user: something the user said"++@pytest.mark.asyncioasync def test_a_note_for_the_user_is_not_dreamt_about(mocker):"""A skipped-run card is the platform talking to the user; a dream wouldstore "usage limit reached" as a fact about them."""- session = SimpleNamespace(session_id="session-1", title="Briefing")+ session = SimpleNamespace(+ session_id="session-1",+ title="Briefing",+ started_at=datetime(2026, 10, 1, tzinfo=timezone.utc),+ )page = SimpleNamespace(messages=[ChatMessage(role="user", content="brief me"),diff --git a/autogpt_platform/backend/backend/copilot/dream/hidden_sessions.py b/autogpt_platform/backend/backend/copilot/dream/hidden_sessions.pynew file mode 100644index 0000000000..872a3f4fe4--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/hidden_sessions.py@@ -0,0 +1,91 @@+"""The chat sessions a dream must not read, because a forget hid their memory.++A dream reads recent chat history straight from the chat store, which a+forget never touches: the forgotten fact still sits in the message that said+it. So every session that a hidden episode came from (one the recall policy+will not return, see ``recall.recallable_episode_predicate``) is left out of+the dream's input whole. Coarse on purpose: a forgotten sentence cannot be+cut out of a transcript reliably, and a session is only background there.++An episode names its session in up to three places, all written at+ingestion: a chat turn is named ``conversation_<session>`` and a derived+finding ``finding_<session>``, both with a ``source_description`` ending+``in session <session>``; a stored memory's envelope has+``provenance='session:<session>#msg:<n>'``. A hard forget empties an episode+into a tombstone but keeps all three (the provenance in its own property,+since the envelope went with the text), so its session stays excluded.+"""++import logging+import re++from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver+from backend.copilot.graphiti.memory_model import envelope_provenance+from backend.copilot.graphiti.recall import (+ forgotten_facts_clause,+ recallable_episode_predicate,+)++logger = logging.getLogger(__name__)++_EPISODE_NAME = re.compile(r"^(?:conversation|finding)_(?P<session>.+)$")+_SOURCE_DESCRIPTION = re.compile(r" in session (?P<session>\S+)$")+_PROVENANCE = re.compile(r"^session:(?P<session>[^#]+)")+++async def hidden_session_ids(+ driver: AutoGPTFalkorDriver, group_id: str+) -> set[str] | None:+ """Every session a hidden episode in the graph came from.++ ``None`` when the graph could not be read: the caller cannot tell which+ sessions are safe and must read none.+ """+ try:+ result = await driver.execute_query(_HIDDEN_EPISODES_QUERY, g=group_id)+ except Exception:+ logger.warning(+ f"Hidden-episode read failed for group {group_id[:12]}; "+ "the dream reads no chat sessions",+ exc_info=True,+ )+ return None+ rows = result[0] if result else []+ return {+ session+ for row in rows+ for session in episode_session_ids(+ row["name"], row["source_description"], row["content"], row["provenance"]+ )+ }+++def episode_session_ids(+ name: str | None,+ source_description: str | None,+ content: str | None,+ provenance: str | None = None,+) -> set[str]:+ """The chat sessions an episode says it came from (usually one).++ ``provenance`` is what a tombstone kept of its envelope; a live episode+ has it in ``content`` instead.+ """+ kept = provenance or envelope_provenance(content) or ""+ matches = [+ _EPISODE_NAME.search(name or ""),+ _SOURCE_DESCRIPTION.search(source_description or ""),+ _PROVENANCE.match(kept),+ ]+ return {match.group("session") for match in matches if match}+++_HIDDEN_EPISODES_QUERY = (+ forgotten_facts_clause()+ + f"""+MATCH (n:Episodic {{group_id: $g}})+WHERE NOT ({recallable_episode_predicate("n")})+RETURN n.name AS name, n.source_description AS source_description,+ n.content AS content, n.provenance AS provenance+"""+)diff --git a/autogpt_platform/backend/backend/copilot/dream/hidden_sessions_test.py b/autogpt_platform/backend/backend/copilot/dream/hidden_sessions_test.pynew file mode 100644index 0000000000..c76d22031e--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/hidden_sessions_test.py@@ -0,0 +1,124 @@+"""Tests for the chat sessions a dream leaves out after a forget.++The live counterpart is+``graphiti/recall_integration_test.py::test_the_dream_leaves_out_the_sessions_a_forget_hid``.+"""++from unittest.mock import AsyncMock, patch++import pytest++from backend.copilot.graphiti import ingest+from backend.copilot.graphiti.memory_model import MemoryEnvelope+from backend.copilot.graphiti.recall import (+ forgotten_facts_clause,+ recallable_episode_predicate,+)++from . import hidden_sessions+++def _envelope(provenance: str) -> str:+ return MemoryEnvelope(content="a fact", provenance=provenance).model_dump_json()+++def _hidden(name: str, content: str, provenance: str | None = None) -> dict:+ """A row of the hidden-episodes read."""+ return {+ "name": name,+ "source_description": "",+ "content": content,+ "provenance": provenance,+ }+++class TestEpisodeSessionIds:+ @pytest.mark.parametrize(+ ("name", "description", "content", "sessions"),+ [+ ("conversation_s1", "User message in session s1", "Alice: hi", {"s1"}),+ (+ "finding_s2",+ "Assistant-derived finding in session s2",+ _envelope("session:s2"),+ {"s2"},+ ),+ ("my note", "Conversation memory", _envelope("session:s3#msg:4"), {"s3"}),+ ("dream_p1_consolidate_0", "dream-pass", _envelope("dream:p1"), set()),+ ("episode-1", "a test episode", "not an envelope", set()),+ (None, None, None, set()),+ ],+ )+ def test_reads_the_session_from_name_description_or_provenance(+ self,+ name: str | None,+ description: str | None,+ content: str | None,+ sessions: set[str],+ ) -> None:+ assert hidden_sessions.episode_session_ids(name, description, content) == (+ sessions+ )++ def test_a_tombstone_keeps_its_session_in_the_provenance_it_kept(self) -> None:+ """A hard forget empties a stored memory's envelope but keeps its+ provenance on the episode, so its session stays hidden."""+ assert hidden_sessions.episode_session_ids(+ "my note", "Conversation memory", "", "session:s3#msg:4"+ ) == {"s3"}++ @pytest.mark.asyncio+ async def test_reads_the_session_of_everything_ingestion_writes(self) -> None:+ """Drift guard: whatever a chat turn and its derived finding are+ named, the parser must still find their session in them."""+ payloads: list[dict] = []++ async def enqueue(_scope: object, payload: dict) -> bool:+ payloads.append(payload)+ return True++ with (+ patch.object(ingest, "_enqueue_payload", enqueue),+ patch.object(ingest, "resolve_user_name", AsyncMock(return_value="Al")),+ ):+ await ingest.enqueue_conversation_turn(+ "user-1", "sess-42", "hello", "The analysis shows " + "x" * 200+ )++ assert len(payloads) == 2, "a turn and its derived finding"+ for payload in payloads:+ found = hidden_sessions.episode_session_ids(+ payload["name"], payload["source_description"], payload["episode_body"]+ )+ assert found == {"sess-42"}+++class TestHiddenSessionIds:+ @pytest.mark.asyncio+ async def test_asks_for_every_episode_the_policy_hides(self) -> None:+ driver = AsyncMock()+ driver.execute_query.return_value = (+ [+ _hidden("conversation_s1", ""),+ _hidden("a memory", "text"),+ _hidden("a hard-forgotten memory", "", "session:s9#msg:2"),+ ],+ [],+ None,+ )++ found = await hidden_sessions.hidden_session_ids(driver, "user_g")++ assert found == {"s1", "s9"}+ query = driver.execute_query.await_args.args[0]+ assert query.startswith(forgotten_facts_clause())+ assert f"NOT ({recallable_episode_predicate('n')})" in query+ assert "n.provenance AS provenance" in query+ assert driver.execute_query.await_args.kwargs == {"g": "user_g"}++ @pytest.mark.asyncio+ async def test_an_unreadable_graph_is_none_not_an_empty_set(self) -> None:+ driver = AsyncMock()+ driver.execute_query.side_effect = RuntimeError("falkordb down")++ assert await hidden_sessions.hidden_session_ids(driver, "user_g") is Nonediff --git a/autogpt_platform/backend/backend/copilot/dream/locks.py b/autogpt_platform/backend/backend/copilot/dream/locks.pyindex 8335a72dd7..0380a01e98 100644--- a/autogpt_platform/backend/backend/copilot/dream/locks.py+++ b/autogpt_platform/backend/backend/copilot/dream/locks.py@@ -44,17 +44,19 @@ DEFAULT_LOCK_TTL_SECONDS = 1800BATCH_LOCK_TTL_SECONDS = 24 * 60 * 60 + 600# Compare-and-delete: only the holder whose token still matches the stored-# value may delete the key. Single-key Lua routes on Redis Cluster.-_UNLOCK_SCRIPT = (+# value may delete the key. Single-key Lua routes on Redis Cluster. Graphiti's+# per-graph write lock releases and renews with these two scripts as well+# (``graphiti/scope_lock.py``).+UNLOCK_SCRIPT = ('if redis.call("get", KEYS[1]) == ARGV[1] then ''return redis.call("del", KEYS[1]) else return 0 end')# Compare-and-extend: only the holder whose token still matches the stored# value may stretch the TTL. Same single-key Lua pattern as-# ``_UNLOCK_SCRIPT`` — a blind ``SET XX`` would overwrite a *newer* pass's+# ``UNLOCK_SCRIPT`` — a blind ``SET XX`` would overwrite a *newer* pass's# token (and TTL) when our lock expired and was re-acquired mid-pass.-_EXTEND_SCRIPT = (+EXTEND_SCRIPT = ('if redis.call("get", KEYS[1]) == ARGV[1] then ''return redis.call("expire", KEYS[1], ARGV[2]) else return 0 end')@@ -106,7 +108,7 @@ class DreamLockHandle:async def extend(self, ttl_seconds: int) -> bool:"""Stretch the lock TTL only while the key still holds our token.- Single-key Lua compare-and-extend (mirrors ``_UNLOCK_SCRIPT``): a+ Single-key Lua compare-and-extend (mirrors ``UNLOCK_SCRIPT``): ablind ``SET XX`` succeeds against ANY existing value, so if ourlock expired and a newer pass re-acquired the key, it wouldhijack that pass's token and stretch its TTL. The compare also@@ -122,9 +124,7 @@ class DreamLockHandle:# ``str`` — same workaround as the release paths below.extended = await cast("Any",- self._redis.eval(- _EXTEND_SCRIPT, 1, self._key, self.token, str(ttl_seconds)- ),+ self._redis.eval(EXTEND_SCRIPT, 1, self._key, self.token, str(ttl_seconds)),)if not extended:logger.warning(@@ -173,7 +173,7 @@ async def dream_lock(try:# ``cast`` because redis-py's stubs type ``eval`` as a bare# ``str`` — same workaround as ``data/redis_helpers.py``.- deleted = await cast("Any", redis.eval(_UNLOCK_SCRIPT, 1, key, token))+ deleted = await cast("Any", redis.eval(UNLOCK_SCRIPT, 1, key, token))if deleted:logger.debug("Released dream lock for user %s", user_id[:12])else:@@ -239,7 +239,7 @@ async def release_dream_lock(scope: MemoryScope, token: str | None) -> None:deleted = await cast("Any",redis.eval(- _UNLOCK_SCRIPT,+ UNLOCK_SCRIPT,1,scope.redis_key("dream_lock"),token,diff --git a/autogpt_platform/backend/backend/copilot/dream/orchestrator.py b/autogpt_platform/backend/backend/copilot/dream/orchestrator.pyindex d39092216f..f1896fa035 100644--- a/autogpt_platform/backend/backend/copilot/dream/orchestrator.py+++ b/autogpt_platform/backend/backend/copilot/dream/orchestrator.py@@ -958,6 +958,7 @@ async def _execute_dream_pass_async(pass_id,ops,known_fact_uuids=input_bundle.known_fact_uuids,+ known_episode_uuids=input_bundle.known_episode_uuids,lock_handle=dream_lock_handle,)# Apply succeeded (even as a no-op) — stamp the marker so the@@ -991,6 +992,9 @@ async def _execute_dream_pass_async(proposal_count=_as_int("proposal_count"),demotion_count=_as_int("demotion_count"),entity_invalidation_count=_as_int("entity_invalidation_count"),+ dropped_forgotten=_as_int("dropped_forgotten"),+ pending_writes=_as_int("pending_writes"),+ failed_writes=_as_int("failed_writes"),summary_for_user=ops.summary_for_user,ingestion_drain_status=ingestion_drain_status,# ``None`` (key absent) on an empty pass — apply skipped thediff --git a/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py b/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.pyindex a033014d58..4cc1cc6815 100644--- a/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py@@ -595,6 +595,7 @@ async def test_clamps_oversized_sanitizer_output(mocker):ops,*,known_fact_uuids=None,+ known_episode_uuids=None,lock_handle=None,):captured["ops"] = ops@@ -663,6 +664,7 @@ async def test_demotions_capped_at_five_percent_of_active_facts(mocker):ops,*,known_fact_uuids=None,+ known_episode_uuids=None,lock_handle=None,):captured["ops"] = ops@@ -780,6 +782,7 @@ async def test_sync_path_filters_hallucinated_demotion_before_cap(mocker):ops,*,known_fact_uuids=None,+ known_episode_uuids=None,lock_handle=None,):captured["ops"] = ops@@ -865,6 +868,11 @@ async def test_sync_path_passes_known_fact_uuids_to_apply(mocker):apply_mock.await_args.kwargs["known_fact_uuids"]== input_bundle.known_fact_uuids)+ # What an uncited write rests on (``graphiti/recall_citations.py``).+ assert (+ apply_mock.await_args.kwargs["known_episode_uuids"]+ == input_bundle.known_episode_uuids+ )@pytest.mark.asynciodiff --git a/autogpt_platform/backend/backend/copilot/dream/ratification.py b/autogpt_platform/backend/backend/copilot/dream/ratification.pyindex 653dfe5bfc..c4728911d5 100644--- a/autogpt_platform/backend/backend/copilot/dream/ratification.py+++ b/autogpt_platform/backend/backend/copilot/dream/ratification.py@@ -171,6 +171,9 @@ async def _process_edge(* hits >= 1 → promote to ``status='active'``* hits == 0 and past grace → supersede with ``reason='unratified'``* hits == 0 within grace → no-op (still earning its keep)++ Both writes apply only while the edge is still an unexpired tentative+ one: a user's forget that lands after the listing is never overwritten."""edge_uuid = edge.get("uuid")if not edge_uuid:@@ -180,7 +183,7 @@ async def _process_edge(hits = await _get_hit_count(scope, edge_uuid)if hits >= 1:- promoted = await _promote_edge(driver, edge_uuid)+ promoted = await _promote_if_tentative(driver, edge_uuid)if promoted:result.ratified_count += 1return@@ -202,6 +205,7 @@ async def _process_edge(new_status="superseded",user_id=scope.owner_user_id,group_id=scope.group_id,+ expected_status="tentative",)if succeeded:result.superseded_count += 1@@ -230,26 +234,6 @@ async def _list_tentative_edges(return [{"uuid": r["uuid"], "created_at": r["created_at"]} for r in records]-async def _promote_edge(driver: AutoGPTFalkorDriver, edge_uuid: str) -> bool:- """Flip a tentative edge to ``status='active'`` with a ratified_at stamp.-- Returns True iff Cypher actually touched a row. Sub-10ms per spec §5.-- The ratified_at timestamp is generated in Python because FalkorDB- does not implement Cypher's no-arg ``datetime()`` function.- """- query = """- MATCH ()-[e:RELATES_TO {uuid: $uuid}]->()- SET e.status = 'active', e.ratified_at = $now- RETURN e.uuid AS uuid- """- result = await driver.execute_query(- query, uuid=edge_uuid, now=datetime.now(timezone.utc).isoformat()- )- records = result[0] if result else []- return bool(records)--async def try_ratify_on_hit(scope: MemoryScope, edge_uuids: list[str]) -> int:"""Record warm-context hits and promote any tentative edges inline.@@ -318,16 +302,20 @@ async def try_ratify_on_hit(scope: MemoryScope, edge_uuids: list[str]) -> int:async def _promote_if_tentative(driver: AutoGPTFalkorDriver, edge_uuid: str) -> bool:- """``_promote_edge`` with a ``status='tentative'`` guard.-- Distinct from ``_promote_edge`` because the hit-hook fires on- EVERY retrieved edge, including already-active ones — we don't- want to overwrite an active edge's ``ratified_at`` with a fresh- timestamp on every retrieval. The guard makes the call idempotent.+ """Flip a still-tentative, unexpired edge to ``status='active'`` with a+ ``ratified_at`` stamp; True iff Cypher touched a row.++ The one promotion write, for the hit hook and the nightly sweep alike.+ The guard makes a repeat hit on an active edge a no-op (its first+ ``ratified_at`` stays) and keeps a forget that lands between the sweep's+ listing and this write: a retracted or forgotten edge is never made+ active again.+ ``now`` comes from Python: FalkorDB has no no-arg ``datetime()``."""query = """MATCH ()-[e:RELATES_TO {uuid: $uuid}]->()WHERE e.status = 'tentative' AND e.expired_at IS NULL+ AND e.forgotten_at IS NULLSET e.status = 'active', e.ratified_at = $nowRETURN e.uuid AS uuid"""diff --git a/autogpt_platform/backend/backend/copilot/dream/ratification_hits.py b/autogpt_platform/backend/backend/copilot/dream/ratification_hits.pyindex 0eb6c22c14..5531b0490b 100644--- a/autogpt_platform/backend/backend/copilot/dream/ratification_hits.py+++ b/autogpt_platform/backend/backend/copilot/dream/ratification_hits.py@@ -8,9 +8,10 @@ fits the file-length budget.Wiring note: warm-context retrieval (``graphiti/context.py``) fires``ratification.try_ratify_on_hit`` for every retrieved edge, whichboth bumps the counter here (via ``record_memory_hit``) and promotes-tentative edges inline. The nightly ratification sweep therefore-rarely promotes — it primarily owns grace-period supersession of-tentatives that never earned a hit.+tentative edges inline. ``memory_search`` counts its returned edges+through ``recall.record_hit`` without promoting; the nightly sweep+promotes those. The sweep otherwise rarely promotes — it primarily+owns grace-period supersession of tentatives that never earned a hit."""from __future__ import annotationsdiff --git a/autogpt_platform/backend/backend/copilot/dream/ratification_test.py b/autogpt_platform/backend/backend/copilot/dream/ratification_test.pyindex 95cd82a30a..d5ea32acc0 100644--- a/autogpt_platform/backend/backend/copilot/dream/ratification_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/ratification_test.py@@ -30,17 +30,16 @@ def _make_driver(records_for_list: list[dict], records_for_promote=None):"""Build a MagicMock driver whose execute_query returns the rows we want.The ratification module issues two Cypher shapes:+ * the PROMOTE query (SET ratified_at) → return ``records_for_promote``* the LIST query (status='tentative') → return ``records_for_list``- * the PROMOTE query (SET status=active) → return ``records_for_promote``- We dispatch by inspecting the first arg of execute_query.+ We dispatch by inspecting the first arg of execute_query; the promote+ query is checked first because its guard also names 'tentative'."""driver = MagicMock()driver.close = AsyncMock(return_value=None)async def fake_execute(query: str, **kwargs):- if "status = 'tentative'" in query:- return (list(records_for_list), None, None)if "ratified_at" in query:# Promote query — default to one-row response unless caller said otherwise.rows = (@@ -49,6 +48,8 @@ def _make_driver(records_for_list: list[dict], records_for_promote=None):else [{"uuid": kwargs.get("uuid")}])return (list(rows), None, None)+ if "status = 'tentative'" in query:+ return (list(records_for_list), None, None)return ([], None, None)driver.execute_query = AsyncMock(side_effect=fake_execute)@@ -170,6 +171,33 @@ async def test_tentative_edge_without_hits_past_grace_is_superseded_as_unratifieassert call.args[1] == ["edge-stale"]assert call.kwargs["reason"] == "unratified"assert call.kwargs["new_status"] == "superseded"+ # Only while it is still an unexpired tentative edge: a forget that+ # landed after the listing is not overwritten.+ assert call.kwargs["expected_status"] == "tentative"+++async def test_sweep_promotion_keeps_a_forget_that_landed_after_the_listing(+ mocker, fake_redis, stub_mark_superseded+):+ """Hits say promote, but the user retracted the edge after the sweep+ listed it: the guarded write matches nothing, so the retraction stays."""+ edge = {"uuid": "edge-forgotten", "created_at": _hours_ago(2)}+ driver = _make_driver(records_for_list=[edge], records_for_promote=[])+ mocker.patch.object(ratification_mod, "open_driver", MagicMock(return_value=driver))+ fake_redis.hits["edge-forgotten"] = 2++ result = await run_ratification_pass("u-raced")++ assert result.ratified_count == 0+ [promote] = [+ call+ for call in driver.execute_query.await_args_list+ if "ratified_at" in call.args[0]+ ]+ assert "WHERE e.status = 'tentative' AND e.expired_at IS NULL" in promote.args[0]+ assert "AND e.forgotten_at IS NULL" in promote.args[0]+ stub_mark_superseded.assert_not_awaited()@pytest.mark.asynciodiff --git a/autogpt_platform/backend/backend/copilot/dream/schemas.py b/autogpt_platform/backend/backend/copilot/dream/schemas.pyindex 065c403e15..15d3de58d8 100644--- a/autogpt_platform/backend/backend/copilot/dream/schemas.py+++ b/autogpt_platform/backend/backend/copilot/dream/schemas.py@@ -31,16 +31,19 @@ class IngestionDrainStatus(str, Enum):Three distinct states, so consumers (and a future Memory Visualizer)can tell a healthy by-design skip from a real failure:- * ``drained`` — the pass's own episodes were confirmed landed in the- graph before the pass returned (sync path), or the pass enqueued- nothing to drain. Healthy.+ * ``drained`` — the worker finished every one of the pass's own+ episodes before the pass returned (sync path): each was written,+ dropped for resting on a forget (``dropped_forgotten``), or failed+ with a logged error; or the pass enqueued nothing to drain. Healthy.* ``skipped`` — the drain was intentionally not run to avoid stallinga shared serial loop (batch path). The episodes process- fire-and-forget; healthy by design, NOT a failure signal.+ fire-and-forget, counted pending; healthy by design, NOT a failure+ signal.* ``timed_out`` — the sync-path drain was attempted but did not finish- within its budget. The reported write/proposal counts include- episodes still queued in-process, which are lost if the worker's pod- restarts. This is the only value that warrants an operator warning.+ within its budget. The episodes not written yet are counted pending,+ not written; they stay queued in-process, and are lost if the+ worker's pod restarts. This is the only value that warrants an+ operator warning."""drained = "drained"@@ -327,22 +330,36 @@ class DreamPassResult(BaseModel):elapsed_seconds: float | None = Noneexecution_path: ExecutionPath = "sync_baseline"- # Per-phase telemetry — null when the phase did not run.+ # Per-phase telemetry — null when the phase did not run. The writes and+ # proposals count only those graphiti saved before the pass was+ # reported, never one merely queued.consolidated_count: int = 0proposal_count: int = 0demotion_count: int = 0entity_invalidation_count: int = 0+ # Of the pass's writes and proposals, the ones dropped unwritten because+ # a forget reached what they rest on after the pass read the graph; only+ # those dropped before the pass was reported (see ingestion_drain_status).+ dropped_forgotten: int = 0+ # The ones not written yet when the pass was reported: still queued+ # in-process (lost if the process stops), waiting for the graph's write+ # lock, or being written. Every one on the batch path, which does not+ # wait for them.+ pending_writes: int = 0+ # The ones whose write raised: dropped, logged, not retried.+ failed_writes: int = 0summary_for_user: str = ""dream_session_id: str | None = None# Fate of the dream's enqueued graph writes when the pass was reported- # complete — see ``IngestionDrainStatus``. ``drained`` is a fully-landed- # (or nothing-to-drain) pass, ``skipped`` is the by-design batch skip+ # complete — see ``IngestionDrainStatus``. ``drained`` is a pass whose+ # writes the worker all finished (written, dropped or failed; or nothing+ # to drain), ``skipped`` is the by-design batch skip# (healthy), ``timed_out`` is a sync-path drain that overran its budget# with writes still queued in-process (at risk on pod restart). Defaults# to ``drained`` so a payload that omits the field still validates as a- # fully-landed pass. Note this default is deliberately the opposite of+ # finished pass. Note this default is deliberately the opposite of# ``apply.drain_status_from_stats``, which fails closed to ``timed_out``# for a missing LIVE stats key: an absent key on a fresh pass means lost# observability, while an absent field on a stored record predates thediff --git a/autogpt_platform/backend/backend/copilot/graphiti/AGENTS.md b/autogpt_platform/backend/backend/copilot/graphiti/AGENTS.mdindex 9e3436d806..fdace7d448 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/AGENTS.md+++ b/autogpt_platform/backend/backend/copilot/graphiti/AGENTS.md@@ -20,6 +20,14 @@ Runtime prompt instructions live in `prompting.py:get_graphiti_supplement()`.- Build a `MemoryScope` (`scope.py`) once where a request enters the memory code and pass it down; read `group_id` / `scope_key` / `redis_key(...)` off it rather than re-deriving them from a `(user_id, expert_id)` pair, and open drivers with `open_driver(scope)`.- Be careful about memory pollution from assistant/tool phrasing; extraction quality matters as much as ingestion success.- Keep warm-context and tool-driven recall resilient: failures should degrade gracefully rather than break chat execution.+- Recall goes through `recall.py` (what may be read back) and `recall_render.py` (how it is written out). Read facts with `search_facts` and episodes with `recent_episodes`; what the assistant is shown is read again by uuid as the last graph read before rendering (`search_facts` ends with `live_now`, and warm context and `memory_search`, which hold both lists while the slower read finishes, call `recall_recheck.recheck`). Ingestion hands graphiti `recall_ingest.previous_episode_uuids`. Code with its own Cypher uses the predicates there: `live_fact_predicate` for facts (the settings and admin fact lists, the dream's demotion writers), `forgotten_facts_clause` with `recallable_episode_predicate` for episodes (the dream gather and its session filter, the forget's redaction).+ - A fact is live while `expired_at` and `forgotten_at` are unset and `status` is `active`, `tentative` or unset. It is forgotten once a forget has stamped `forgotten_at`. The marker is written only by a forget (`recall_forget.py`, and the legacy-forget backfill for older forgets), and no ingestion, dream demotion or other status change can make a forgotten fact look remembered, nor a remembered one look forgotten: a `status` of `retracted` or an `expiration_reason` of `user_signal` is no forget without the marker, since graphiti's attribute extraction fills both from an episode's text and a dream demotion picks its reason. The extraction schema (`types.MemoryFact`) leaves both out, and an extracted `retracted` is put back to `active` as the episode is written (`ingest._undo_extracted_retractions`). Nor can extraction write the marker: graphiti-core 0.30.2 saves every key its attribute model answers, so `recall_ingest.ForgetAwareLLMClient` keeps only what the edge or entity type declares (`types.extracted_attributes`), and no extraction model declares the marker or the audit copies (`types.POLICY_EDGE_FIELDS`). Forgets made before the marker are recognised by the pre-policy shape (`expired_at` set with no `invalid_at` and no reason). `migrations/backfill_legacy_forgets.py` restamps that shape, after which its clause can go. It restamps each graph holding the graph's write lock (below), so an ingestion running meanwhile cannot save an older copy of an edge over the restamp; a graph whose lock another writer keeps past 60 s, or that cannot be locked because Redis is unreachable, is skipped with nothing written and counted busy, a graph whose backfill raises is counted failed, and either makes the script exit 1 so it is run again. Drop the clause only after an `--apply` run that exits 0 everywhere (a dry run exits 0 too, having written nothing). An episode is recallable while it has no `redacted_at` and none of its `entity_edges` is forgotten.+ - What a forget guarantees: the fact and every episode it came from are hidden from warm context, `memory_search`, `memory_forget_search`, the settings fact list and counts, the dream's input (its episodes and facts, and every chat session a hidden episode came from, left out whole) and ingestion's extraction context (the earlier episodes graphiti shows its prompts). A read already under way when the forget answers does not show it either, once its last check has run: right before rendering, warm context and `memory_search` check every fact and episode they are about to show again, by uuid, in one statement (`recall_recheck.recheck`), and `memory_forget_search` its facts (`search_facts` ends with `live_now`). Every item shown passed that check, its own last graph read. A forget writes each fact's marker before anything else, so a check that began after the marker committed drops the fact and every episode citing it; a response whose check began earlier can still carry them while it is delivered (rendering, scheduling, network), and no later read will. The sentence also leaves what graphiti keeps for its own prompts (`recall_hide.py`). The edge's `fact` and `name` read `[forgotten]` (`recall.FORGOTTEN_FACT`), the originals moved to `fact_redacted` / `name_redacted`; no edge type has that name, so graphiti never runs its attribute prompt, which lists an edge's stored properties, on a forgotten edge. Every entity the fact joins, and every entity an episode citing it mentions, loses its `summary` and every property but its identity (uuid, name, group_id, labels, created_at, name_embedding): typed attributes such as `Person.role` and any `attributes` map go. The `summary` of each community one of them belongs to is blanked too. They are blanked, not rewritten: graphiti extracts them again from the next episode that mentions the entity, and the weekly rebuild restores the communities.+ - What it does not: admin audit surfaces (status `any`, the graph view) keep showing retracted facts, with the text from `fact_redacted`, and redacted episodes, on purpose, and show a tombstone's title and `hard_deleted_at` (never its content). `memory_forget_search` lists forgotten facts too when the chat asks for them (`include_forgotten`), so a soft forget can be made permanent with a hard one, but never what they said: each is `[forgotten]` with its uuid and when it was stored and forgotten (each read as a timestamp and written out anew, else `unknown`), without its text, audit copy, scope or valid dates. `recall.search_forgotten_facts` finds them by the embedding a forget keeps, ranking the forgotten facts alone, so no live fact takes their slots. Not scrubbed: community names, the fact's embedding vector, and a redacted episode's name and source description. A hard forget keeps, redacted, the text of an episode another edge still cites, and a tombstone keeps its name (for a stored memory, the model's short title). Two windows have no bound. When Redis cannot be reached, forgets and ingestion go ahead without the write lock (below); and a holder that loses its lease (its renewals fail, or its event loop stalls past the five-minute expiry) keeps writing while another writer takes the lock, since the renewal only warns and nothing fences the graph write. Either way an ingestion that read a fact before a forget of it can save its older copy over the forget, and that fact then stays live until a later forget of it: Redis coming back repairs nothing. And order is by when an episode is written, not when it was said, by design: an episode still queued when a forget lands is written after it, and if it states the fact again it teaches it again, as a message sent after the forget would. The weekly community rebuild does not take the lock either, so one running while a forget lands can write a community summary from entity summaries the forget then blanks. Nor does the settings page's erase-all, which deletes the whole graph: an ingestion running meanwhile can save into the emptied graph what it read before the erase.+ - Every forget goes through `recall_forget.retract`, which holds the graph's write lock (`scope_lock.py`) for the whole forget: if an ingestion holds it for all of `FORGET_LOCK_WAIT_SECONDS` (20 s), every uuid fails as `busy` and nothing is written; the chat tool waits five seconds and tries once more, and the settings page answers 409. Soft stamps `forgotten_at`, sets `status='retracted'` and `expiration_reason`, keeps an earlier `expired_at`, leaves `invalid_at` alone, then scrubs the sentence, entities and communities and stamps `redacted_at` on every episode citing the fact (`recall_hide.py`). An audit copy, once written, is never replaced by the placeholder. Hard does all of that first, then (`recall_orphans.py`) turns each episode no other edge of any status cites into a tombstone (`content` emptied, `hard_deleted_at` and `redacted_at` set, its name, source description and envelope `provenance` kept, so the dream still leaves its chat session out), and last, in one query per edge, deletes the edge, drops the tombstones' mentions and deletes each entity left with no fact and no mention. Every step before the delete is idempotent and the delete finds the edge again, so repeating a forget whose clean-up failed finishes it. A step that fails is reported per edge as `cleanup_error`; recall hides the text regardless. A tombstone is never recalled, counted on the settings page or used as extraction context.+ - While Redis answers and the holder's lease holds, a forget and an ingestion never overlap. graphiti's `add_episode` saves the edges and entities it read when it began, so a forget landing mid-episode would be written over. The ingestion worker therefore holds the graph's write lock around `add_episode` and the dream metadata stamp after it (`ingest._write_locked`), waiting up to `INGEST_LOCK_WAIT_SECONDS` (60 s) for the writer holding it (a forget, or an ingestion in another process) to finish; if it cannot, the episode goes to the back of the queue after a pause (one second, doubling with each wait up to 30 s) and waits again, as often as it takes: contention never drops an episode while the process runs, and every third wait a warning names the episode still unwritten. The queue lives in the process: what it holds when the process stops is lost. Waiters take turns: a writer that finds the lock held claims the next turn (the lock's key plus `:next`, renewed at every poll, lapsing two seconds after its waiter stops), and while it holds that claim nobody else takes the lock, the writer that just let it go included, so a worker writing a backlog hands the lock over between two episodes. The lock (`scope_lock.py`) is one Redis key per graph (`MemoryScope.redis_key("write_lock")`), set NX with a token and a five-minute expiry that the holder renews, and released by the dream lock's compare-and-delete script, so a holder whose key expired never frees a newer holder's. When Redis cannot be reached both writers go ahead without it and log a warning (the unbounded windows above). graphiti also resolves each new statement against every edge between the same entities, forgotten ones included: if its model named a forgotten edge a duplicate, graphiti would append the episode to it and clear every attribute on it, the forget's marker and audit copies among them, and a contradiction would stamp its `invalid_at`. So every graphiti client is built with `recall_ingest.ForgetAwareLLMClient` (`client._build_graphiti`), which keeps a forgotten edge (its text is `[forgotten]`) out of graphiti's edge-dedup answers: a statement said again after a forget is saved by graphiti as a new live edge in the same `add_episode`, one merged into a live fact stays merged, and graphiti's model-decided dedup never rewrites a forgotten edge. There is no second extraction and nothing to repair afterwards. One path runs before the model and so past the guard: graphiti's exact-text match reuses an edge whose text equals the new statement's (lower-cased, whitespace collapsed), so a statement that reads `[forgotten]` appends its episode's uuid to the forgotten edge's `episodes`. The edge keeps its marker and audit copies and stays out of recall, and the episode, citing it, is hidden. The guard reads graphiti's prompt, so it fails closed: a candidate printed with the placeholder is forgotten wherever it appears, in the two candidate lists or not, and when the prompt shows the placeholder, anything short of both lists, once each and readable, makes the answer name no edge at all, so the statement is saved as new (at worst a duplicate of a live fact). A prompt whose lists it cannot read is logged at error once per process for each shape, so a graphiti upgrade that changes the prompt shows up before a forgotten edge meets it.+ - The dream keeps its own Cypher. Its gather and session filter (`dream/fetch.py`, `dream/hidden_sessions.py`) build on those predicates, and so does its fact gather (`live_fact_predicate`). Its writers only write over live facts: supersession (`mark_edges_superseded`) and single-hop neighbour invalidation (`invalidate_entity_direct_neighbors`) match `live_fact_predicate` unless a caller passes `expected_status`, and ratification (`dream/ratification.py`) passes `expected_status='tentative'` and promotes only a still-tentative, unexpired, unforgotten edge, so none of them can overwrite a forget, and none writes a new fact. Its consolidated facts and proposals are new statements, written minutes or hours after its read, so each goes to the ingestion worker with what it cites (`recall_citations.Citations`; one citing nothing is taken to rest on everything the pass read, and its statement is compared, lower-cased and whitespace-collapsed, with the sentences forgotten facts keep). Under the write lock, right before `add_episode`, a write resting on a fact forgotten or deleted, or on an episode no longer recallable, since the pass read the graph is dropped unwritten, logged at info, and counted in the pass's `dropped_forgotten` (it counts the drops the worker made before apply returned, which is all of them on a drained pass; the batch path does not wait, so its count covers only the drops made before it returned). The pass counts a write consolidated, or a proposal proposed, only once graphiti has saved it: those still queued or being written when apply returns are reported as `pending_writes` (every one on the batch path), and those whose write raised as `failed_writes`. The check trusts the model's citations: a write that cites one source but restates another, forgotten, in other words is not recognised, and an uncited write drawn only from a chat session whose episode and fact the pass did not read (beyond its 50 newest episodes and 500 facts) is recognised only by an exact restatement of a softly forgotten sentence.+ - A live fact whose valid time has ended (a past `invalid_at`, never expired) is still recalled, as history: it renders as `fact (valid: X — Y)`. Only a retired fact gets a retirement label, and "present" appears only while no end is known.## Dream Schedulesdiff --git a/autogpt_platform/backend/backend/copilot/graphiti/_format.py b/autogpt_platform/backend/backend/copilot/graphiti/_format.pydeleted file mode 100644index c6975c5c39..0000000000--- a/autogpt_platform/backend/backend/copilot/graphiti/_format.py+++ /dev/null@@ -1,43 +0,0 @@-"""Shared attribute-resolution helpers for Graphiti edge/episode objects.--graphiti-core edge and episode objects have varying attribute names across-versions. These helpers centralise the fallback chains so there's one place-to update when upstream changes an attribute name.-"""---def extract_fact(edge) -> str:- """Extract the human-readable fact from an edge object."""- return getattr(edge, "fact", None) or getattr(edge, "name", "") or ""---def extract_temporal_validity(edge) -> tuple[str, str]:- """Return ``(valid_from, valid_to)`` for an edge."""- valid_from = getattr(edge, "valid_at", None) or "unknown"- valid_to = getattr(edge, "invalid_at", None) or "present"- return str(valid_from), str(valid_to)---def extract_episode_body_raw(episode) -> str:- """Extract the full body text from an episode object (no truncation).-- Use this when the body needs to be parsed as JSON (e.g. scope filtering- on MemoryEnvelope payloads). For display purposes, use- ``extract_episode_body()`` which truncates.- """- return str(- getattr(episode, "content", None)- or getattr(episode, "body", None)- or getattr(episode, "episode_body", None)- or ""- )---def extract_episode_body(episode, max_len: int = 500) -> str:- """Extract the body text from an episode object, truncated to *max_len*."""- return extract_episode_body_raw(episode)[:max_len]---def extract_episode_timestamp(episode) -> str:- """Extract the created_at timestamp from an episode object."""- return str(getattr(episode, "created_at", None) or "")diff --git a/autogpt_platform/backend/backend/copilot/graphiti/_format_test.py b/autogpt_platform/backend/backend/copilot/graphiti/_format_test.pydeleted file mode 100644index 836ef14bbc..0000000000--- a/autogpt_platform/backend/backend/copilot/graphiti/_format_test.py+++ /dev/null@@ -1,90 +0,0 @@-"""Tests for shared attribute-resolution helpers."""--from types import SimpleNamespace--from backend.copilot.graphiti._format import (- extract_episode_body,- extract_episode_timestamp,- extract_fact,- extract_temporal_validity,-)---def test_extract_fact_prefers_fact_attribute() -> None:- edge = SimpleNamespace(fact="user likes python", name="preference")- assert extract_fact(edge) == "user likes python"---def test_extract_fact_falls_back_to_name() -> None:- edge = SimpleNamespace(name="preference")- assert extract_fact(edge) == "preference"---def test_extract_fact_handles_none_fact() -> None:- edge = SimpleNamespace(fact=None, name="fallback")- assert extract_fact(edge) == "fallback"---def test_extract_fact_handles_missing_both() -> None:- edge = SimpleNamespace()- assert extract_fact(edge) == ""---def test_extract_temporal_validity_with_values() -> None:- edge = SimpleNamespace(valid_at="2025-01-01", invalid_at="2025-12-31")- assert extract_temporal_validity(edge) == ("2025-01-01", "2025-12-31")---def test_extract_temporal_validity_defaults() -> None:- edge = SimpleNamespace()- assert extract_temporal_validity(edge) == ("unknown", "present")---def test_extract_temporal_validity_none_values() -> None:- edge = SimpleNamespace(valid_at=None, invalid_at=None)- assert extract_temporal_validity(edge) == ("unknown", "present")---def test_extract_episode_body_prefers_content() -> None:- ep = SimpleNamespace(content="hello world", body="alt", episode_body="alt2")- assert extract_episode_body(ep) == "hello world"---def test_extract_episode_body_falls_back_to_body() -> None:- ep = SimpleNamespace(body="fallback body")- assert extract_episode_body(ep) == "fallback body"---def test_extract_episode_body_falls_back_to_episode_body() -> None:- ep = SimpleNamespace(episode_body="last resort")- assert extract_episode_body(ep) == "last resort"---def test_extract_episode_body_handles_none_all() -> None:- ep = SimpleNamespace(content=None, body=None, episode_body=None)- assert extract_episode_body(ep) == ""---def test_extract_episode_body_truncates() -> None:- ep = SimpleNamespace(content="x" * 1000)- assert len(extract_episode_body(ep)) == 500---def test_extract_episode_body_custom_max_len() -> None:- ep = SimpleNamespace(content="x" * 100)- assert len(extract_episode_body(ep, max_len=10)) == 10---def test_extract_episode_timestamp_with_value() -> None:- ep = SimpleNamespace(created_at="2025-01-01T00:00:00Z")- assert extract_episode_timestamp(ep) == "2025-01-01T00:00:00Z"---def test_extract_episode_timestamp_missing() -> None:- ep = SimpleNamespace()- assert extract_episode_timestamp(ep) == ""---def test_extract_episode_timestamp_none() -> None:- ep = SimpleNamespace(created_at=None)- assert extract_episode_timestamp(ep) == ""diff --git a/autogpt_platform/backend/backend/copilot/graphiti/client.py b/autogpt_platform/backend/backend/copilot/graphiti/client.pyindex 8795be813a..1dca78f66c 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/client.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/client.py@@ -203,13 +203,15 @@ def _build_graphiti(function. One construction site means a kwarg added here reaches thetests too, instead of silently drifting from a hand-mirrored copy.Production passes none of them; each defaults to the real component.- ``group_id`` is only consulted when ``graph_driver`` is not supplied.+ ``group_id`` is only consulted when ``graph_driver`` is not supplied;+ ``llm_client`` is wrapped in ``recall_ingest.ForgetAwareLLMClient``."""from graphiti_core import Graphitifrom graphiti_core.embedder import OpenAIEmbedder, OpenAIEmbedderConfigfrom graphiti_core.llm_client import LLMConfigfrom .falkordb_driver import AutoGPTFalkorDriver+ from .recall_ingest import ForgetAwareLLMClientfrom .reranker import CompatOpenAIRerankerClientif embedder is None:@@ -243,7 +245,7 @@ def _build_graphiti(database=group_id,)return Graphiti(- llm_client=llm_client,+ llm_client=ForgetAwareLLMClient(llm_client),embedder=embedder,cross_encoder=cross_encoder,graph_driver=graph_driver,diff --git a/autogpt_platform/backend/backend/copilot/graphiti/conftest.py b/autogpt_platform/backend/backend/copilot/graphiti/conftest.pyindex ccaa1ce4c2..f46d4c7640 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/conftest.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/conftest.py@@ -32,10 +32,10 @@ The fixtures connect using ``GraphitiConfig`` defaultsimport socketimport uuid-from collections.abc import Iterable+from collections.abc import Awaitable, Iterablefrom datetime import datetime, timezonefrom pathlib import Path-from typing import AsyncIterator+from typing import AsyncIterator, castimport pytestimport pytest_asyncio@@ -46,10 +46,15 @@ from graphiti_core.llm_client.client import LLMClientfrom graphiti_core.llm_client.config import ModelSizefrom graphiti_core.prompts.models import Messagefrom pydantic import BaseModel+from pytest_mock import MockerFixture+from redis.asyncio import Redis+from . import scope_lockfrom .client import _build_graphitifrom .config import graphiti_configfrom .falkordb_driver import AutoGPTFalkorDriver+from .recall_fake_redis import FakeRedis+from .scope import MemoryScopedef pytest_collection_modifyitems(items: list[pytest.Item]) -> None:@@ -133,6 +138,39 @@ def stub_graphiti_client():return _build[email protected](autouse=True)+def lock_redis(mocker: MockerFixture) -> FakeRedis:+ """Every test's own in-memory Redis for the graph write lock+ (``scope_lock.py``), so no unit test reaches for a real one; the live+ tests' ``scope_graph`` swaps in FalkorDB's own (``live_lock``)."""+ redis = FakeRedis()+ mocker.patch.object(+ scope_lock, "get_redis_async", mocker.AsyncMock(return_value=redis)+ )+ return redis+++@pytest_asyncio.fixture(loop_scope="function")+async def live_lock(+ falkordb_available: bool, mocker: MockerFixture+) -> AsyncIterator[Redis]:+ """The FalkorDB server's own Redis as the graph write lock's backend,+ so the live tests take a real ``SET NX`` lock and run its scripts."""+ redis = Redis(+ host=graphiti_config.falkordb_host,+ port=graphiti_config.falkordb_port,+ password=graphiti_config.falkordb_password or None,+ decode_responses=True,+ )+ mocker.patch.object(+ scope_lock, "get_redis_async", mocker.AsyncMock(return_value=redis)+ )+ try:+ yield redis+ finally:+ await redis.aclose()++@pytest_asyncio.fixture(scope="session", loop_scope="session")async def server(): # type: ignore[override]"""No-op server stub — graphiti tests don't need the full backend."""@@ -222,6 +260,45 @@ async def clean_graph(await driver.close()+@pytest_asyncio.fixture(loop_scope="function")+async def scope_graph(+ falkordb_available: bool,+ live_lock: Redis,+) -> AsyncIterator[tuple[AutoGPTFalkorDriver, MemoryScope]]:+ """A fresh account scope and a driver on that scope's own database.++ For code that opens its own driver from a ``MemoryScope`` (the recall+ layer, ratification): ``clean_graph``'s standalone ``test_*`` database is+ not derivable from any scope, so such code would read an empty graph.+ Indices are built first, as the ingestion write path does, so graphiti's+ searches and ``add_episode`` work. The graph is dropped afterwards with a+ real ``GRAPH.DELETE``; ``DETACH DELETE`` would leave an empty+ ``user_test-*`` graph behind on a shared instance. Its writers lock+ through FalkorDB's own Redis (``live_lock``).+ """+ scope = MemoryScope.for_user(f"test-{uuid.uuid4().hex[:16]}")+ driver = AutoGPTFalkorDriver(+ host=graphiti_config.falkordb_host,+ port=graphiti_config.falkordb_port,+ password=graphiti_config.falkordb_password or None,+ database=scope.group_id,+ build_indices=False,+ )+ try:+ await driver.ensure_indices()+ yield driver, scope+ finally:+ try:+ # falkordb's async ``Graph.delete`` is typed as its sync twin;+ # the same cast ``falkordb_driver.py`` uses for ``query``.+ await cast(Awaitable[None], driver._get_graph(None).delete())+ except Exception as exc:+ if "empty key" not in str(exc).lower():+ raise+ finally:+ await driver.close()++@pytest_asyncio.fixture(loop_scope="function")async def seeded_graph(clean_graph: tuple[AutoGPTFalkorDriver, str],diff --git a/autogpt_platform/backend/backend/copilot/graphiti/context.py b/autogpt_platform/backend/backend/copilot/graphiti/context.pyindex d098fb1fbe..4053dcd610 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/context.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/context.py@@ -2,21 +2,22 @@import asyncioimport logging-from datetime import datetime, timezone--from ._format import (- extract_episode_body,- extract_episode_body_raw,- extract_episode_timestamp,- extract_fact,- extract_temporal_validity,-)-from .client import get_graphiti_client++from graphiti_core.edges import EntityEdge+from graphiti_core.nodes import EpisodicNode+from graphiti_core.search.search_config_recipes import EDGE_HYBRID_SEARCH_CROSS_ENCODER+from .config import graphiti_config+from .recall import recent_episodes, search_facts+from .recall_recheck import recheck+from .recall_render import GLOBAL_SCOPE, episode_scope, render, render_episodefrom .scope import MemoryScopelogger = logging.getLogger(__name__)+# Recent raw episodes shown next to the facts.+_RECENT_EPISODES = 5+async def fetch_warm_context(user_id: str, message: str, expert_id: str | None = None@@ -51,39 +52,27 @@ async def fetch_warm_context(async def _fetch(scope: MemoryScope, message: str) -> str | None:- # Imported lazily so the module can be imported without graphiti-core- # installed (matches the pattern in client.py).- from graphiti_core.search.search_config_recipes import (- EDGE_HYBRID_SEARCH_CROSS_ENCODER,- )-- group_id = scope.group_id- client = await get_graphiti_client(group_id)-# P-1.4: warm context is the single most-impactful retrieval per# session — the one place where the cross-encoder rerank earns its# ~10–15% precision lift (per the audit) at the cost of one extra# batch of boolean-classifier prompts. The EDGE_HYBRID_SEARCH_CROSS_ENCODER# recipe combines BM25 + cosine + BFS edge search with cross-encoder- # reranking. The recipe defaults ``limit=10``; we override to our- # configured ``context_max_facts`` so existing operator tuning still- # applies.- search_config = EDGE_HYBRID_SEARCH_CROSS_ENCODER.model_copy(- update={"limit": graphiti_config.context_max_facts}- )- edge_results, episodes = await asyncio.gather(- client.search_(- query=message,- config=search_config,- group_ids=[group_id],- ),- client.retrieve_episodes(- reference_time=datetime.now(timezone.utc),- group_ids=[group_id],- last_n=5,+ # reranking; ``context_max_facts`` replaces its default ``limit=10`` so+ # existing operator tuning still applies. Both reads go through the+ # recall policy, so forgotten facts and their episodes stay out, and both+ # lists are read again right before rendering (``recall_recheck.py``):+ # the episodes wait here for the slower search, a forget can answer+ # meanwhile, and what it hid is then not shown.+ edges, episodes = await asyncio.gather(+ search_facts(+ scope,+ message,+ limit=graphiti_config.context_max_facts,+ recipe=EDGE_HYBRID_SEARCH_CROSS_ENCODER,),+ recent_episodes(scope, _RECENT_EPISODES),)- edges = edge_results.edges if edge_results is not None else []+ edges, episodes = await recheck(scope, edges, episodes)# Ratification sync hit-hook (P0.4 layer-2): every retrieved edge# that's currently ``status='tentative'`` gets promoted to@@ -115,7 +104,7 @@ def _on_hit_task_done(task: asyncio.Task) -> None:logger.warning("Ratification hit task %s failed", task.get_name(), exc_info=exc)-def _spawn_ratification_hits(scope: MemoryScope, edges) -> None:+def _spawn_ratification_hits(scope: MemoryScope, edges: list[EntityEdge]) -> None:"""Fire-and-forget the ratification hit-hook for retrieved edges.Imports lazily so the dream/ratification module isn't pulled into@@ -123,7 +112,7 @@ def _spawn_ratification_hits(scope: MemoryScope, edges) -> None:users on the rare GRAPHITI_MEMORY=on / DREAM_PASS_ENABLED=offcombination."""- edge_uuids = [uuid for uuid in (getattr(e, "uuid", None) for e in edges) if uuid]+ edge_uuids = [edge.uuid for edge in edges]if not edge_uuids:return@@ -137,49 +126,29 @@ def _spawn_ratification_hits(scope: MemoryScope, edges) -> None:task.add_done_callback(_on_hit_task_done)-def _format_context(edges, episodes) -> str | None:+def _format_context(+ edges: list[EntityEdge], episodes: list[EpisodicNode]+) -> str | None:sections: list[str] = []if edges:- fact_lines = []- for e in edges:- valid_from, valid_to = extract_temporal_validity(e)- fact = extract_fact(e)- fact_lines.append(f" - {fact} ({valid_from} — {valid_to})")+ fact_lines = [f" - {render(edge)}" for edge in edges]sections.append("<FACTS>\n" + "\n".join(fact_lines) + "\n</FACTS>")- if episodes:- ep_lines = []- for ep in episodes:- # Use raw body (no truncation) for scope parsing — truncated- # JSON from extract_episode_body() would fail json.loads().- raw_body = extract_episode_body_raw(ep)- if _is_non_global_scope(raw_body):- continue- display_body = extract_episode_body(ep)- ts = extract_episode_timestamp(ep)- ep_lines.append(f" - [{ts}] {display_body}")- if ep_lines:- sections.append(- "<RECENT_EPISODES>\n" + "\n".join(ep_lines) + "\n</RECENT_EPISODES>"- )+ # Warm context is scope-agnostic, so a project- or book-scoped memory+ # stays out of it.+ ep_lines = [+ f" - {render_episode(ep)}"+ for ep in episodes+ if episode_scope(ep) == GLOBAL_SCOPE+ ]+ if ep_lines:+ sections.append(+ "<RECENT_EPISODES>\n" + "\n".join(ep_lines) + "\n</RECENT_EPISODES>"+ )if not sections:return Nonebody = "\n\n".join(sections)return f"<temporal_context>\n{body}\n</temporal_context>"---def _is_non_global_scope(body: str) -> bool:- """Check if an episode body is a MemoryEnvelope with a non-global scope."""- import json-- try:- data = json.loads(body)- if not isinstance(data, dict):- return False- scope = data.get("scope", "real:global")- return scope != "real:global"- except (json.JSONDecodeError, TypeError):- return Falsediff --git a/autogpt_platform/backend/backend/copilot/graphiti/context_test.py b/autogpt_platform/backend/backend/copilot/graphiti/context_test.pyindex fa02a6c832..778576ed3d 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/context_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/context_test.py@@ -2,17 +2,47 @@import asyncioimport logging-from types import SimpleNamespace+from datetime import datetime, timezonefrom unittest.mock import AsyncMock, patchimport pytest+from graphiti_core.edges import EntityEdge+from graphiti_core.nodes import EpisodeType, EpisodicNode+from graphiti_core.search.search_config_recipes import EDGE_HYBRID_SEARCH_CROSS_ENCODERfrom . import context-from ._format import extract_episode_body-from .context import _format_context, _is_non_global_scope, fetch_warm_context-from .memory_model import MemoryEnvelope, MemoryKind, SourceKind+from .context import _format_context, fetch_warm_context+from .memory_model import MemoryEnvelopefrom .scope import MemoryScope+_NOW = datetime(2025, 6, 1, tzinfo=timezone.utc)+++def _edge(uuid: str = "edge-a", fact: str = "user likes python") -> EntityEdge:+ return EntityEdge(+ uuid=uuid,+ group_id="user_abc",+ source_node_uuid="user",+ target_node_uuid="python",+ created_at=_NOW,+ name="preference",+ fact=fact,+ valid_at=datetime(2025, 1, 1, tzinfo=timezone.utc),+ attributes={"status": "active"},+ )+++def _episode(content: str) -> EpisodicNode:+ return EpisodicNode(+ name="ep",+ group_id="user_abc",+ source=EpisodeType.text,+ source_description="chat",+ content=content,+ created_at=_NOW,+ valid_at=_NOW,+ )+class TestFetchWarmContextEmptyUserId:@pytest.mark.asyncio@@ -41,261 +71,181 @@ class TestFetchWarmContextTimeout:class TestFetchWarmContextGeneralError:@pytest.mark.asyncioasync def test_returns_none_on_unexpected_error(self) -> None:- with patch.object(- context,- "get_graphiti_client",- new_callable=AsyncMock,- side_effect=RuntimeError("connection lost"),+ with (+ patch.object(+ context,+ "search_facts",+ new_callable=AsyncMock,+ side_effect=RuntimeError("connection lost"),+ ),+ patch.object(context, "recent_episodes", new_callable=AsyncMock),):result = await fetch_warm_context("abc", "hello")assert result is None-# ----------------------------------------------------------------------------# Bug: extract_episode_body() truncation breaks scope filtering-# ------------------------------------------------------------------------------def _search_results(edges: list[object]) -> SimpleNamespace:- """Stand-in for graphiti_core.search.search_config.SearchResults — only- the ``.edges`` attribute is exercised by ``_fetch``."""- return SimpleNamespace(edges=edges)--class TestFetchInternal:- """Test the internal _fetch function with mocked graphiti client.+ """``_fetch`` reads through the recall policy (``recall.py``).- After P-1.4, ``_fetch`` calls ``client.search_()`` (note trailing- underscore) with the ``EDGE_HYBRID_SEARCH_CROSS_ENCODER`` recipe and- expects a ``SearchResults`` object whose ``.edges`` attribute carries- the candidate list. The mocks below reflect that shape.+ Both reads and their recheck are mocked at their use site (the recheck+ keeps everything unless a test says otherwise); the hit hook is replaced+ so no test here reaches Redis or FalkorDB."""+ @pytest.fixture(autouse=True)+ def spawn_hits(self):+ with patch.object(context, "_spawn_ratification_hits") as spawn:+ yield spawn++ @pytest.fixture(autouse=True)+ def recheck(self):+ async def everything(scope, facts, episodes):+ return facts, episodes++ with patch.object(context, "recheck", side_effect=everything) as recheck:+ yield recheck++ @staticmethod+ def _reads(edges: list[EntityEdge], episodes: list[EpisodicNode]):+ return (+ patch.object(+ context, "search_facts", new_callable=AsyncMock, return_value=edges+ ),+ patch.object(+ context,+ "recent_episodes",+ new_callable=AsyncMock,+ return_value=episodes,+ ),+ )+@pytest.mark.asyncio- async def test_returns_none_when_no_edges_or_episodes(self) -> None:- mock_client = AsyncMock()- mock_client.search_.return_value = _search_results([])- mock_client.retrieve_episodes.return_value = []-- with patch.object(- context,- "get_graphiti_client",- new_callable=AsyncMock,- return_value=mock_client,- ):+ async def test_returns_none_when_no_edges_or_episodes(self, spawn_hits) -> None:+ search, recent = self._reads([], [])+ with search, recent:result = await context._fetch(MemoryScope.for_user("abc"), "hello")assert result is None+ spawn_hits.assert_not_called()@pytest.mark.asyncioasync def test_expert_scope_is_used_for_all_retrievals(self) -> None:- mock_client = AsyncMock()- mock_client.search_.return_value = _search_results([])- mock_client.retrieve_episodes.return_value = []-- with patch.object(- context,- "get_graphiti_client",- new_callable=AsyncMock,- return_value=mock_client,- ) as get_client_mock:+ search, recent = self._reads([], [])+ with search as search_mock, recent as recent_mock:await fetch_warm_context("user-1", "hello", expert_id="expert-1")- expert_group = MemoryScope.for_expert("user-1", "expert-1").group_id- get_client_mock.assert_awaited_once_with(expert_group)- assert mock_client.search_.await_args.kwargs["group_ids"] == [expert_group]- assert mock_client.retrieve_episodes.await_args.kwargs["group_ids"] == [- expert_group- ]+ expert_scope = MemoryScope.for_expert("user-1", "expert-1")+ for read in (search_mock, recent_mock):+ assert read.await_args is not None+ assert read.await_args.args[0] == expert_scope@pytest.mark.asyncio- async def test_returns_context_with_edges(self) -> None:- edge = SimpleNamespace(- fact="user likes python",- name="preference",- valid_at="2025-01-01",- invalid_at=None,- )- mock_client = AsyncMock()- mock_client.search_.return_value = _search_results([edge])- mock_client.retrieve_episodes.return_value = []-- with patch.object(- context,- "get_graphiti_client",- new_callable=AsyncMock,- return_value=mock_client,- ):+ async def test_returns_context_with_edges(self, spawn_hits) -> None:+ edge = _edge()+ search, recent = self._reads([edge], [])+ with search, recent:result = await context._fetch(MemoryScope.for_user("abc"), "hello")assert result is not Noneassert "<temporal_context>" in resultassert "user likes python" in result+ spawn_hits.assert_called_once_with(MemoryScope.for_user("abc"), [edge])@pytest.mark.asyncio- async def test_returns_context_with_episodes(self) -> None:- ep = SimpleNamespace(- content="talked about coffee",- created_at="2025-06-01T00:00:00Z",+ async def test_renders_only_what_the_recheck_still_finds(+ self, spawn_hits, recheck+ ) -> None:+ """A forget that answered while the search ran (a cross-encoder+ rerank can take seconds): the last read before rendering no longer+ finds the fact or its episode, so neither is shown or counted."""+ forgotten, kept = _edge("e1", "Alice works on Atlas"), _edge("e2", "Bob")+ episode = _episode("Alice works on Atlas")+ recheck.side_effect = None+ recheck.return_value = ([kept], [])+ search, recent = self._reads([forgotten, kept], [episode])+ with search, recent:+ result = await context._fetch(MemoryScope.for_user("abc"), "hello")++ recheck.assert_awaited_once_with(+ MemoryScope.for_user("abc"), [forgotten, kept], [episode])- mock_client = AsyncMock()- mock_client.search_.return_value = _search_results([])- mock_client.retrieve_episodes.return_value = [ep]-- with patch.object(- context,- "get_graphiti_client",- new_callable=AsyncMock,- return_value=mock_client,- ):+ assert result is not None and "Alice" not in result and "Bob" in result+ spawn_hits.assert_called_once_with(MemoryScope.for_user("abc"), [kept])++ @pytest.mark.asyncio+ async def test_returns_context_with_episodes(self) -> None:+ search, recent = self._reads([], [_episode("talked about coffee")])+ with search, recent:result = await context._fetch(MemoryScope.for_user("abc"), "hello")assert result is not Noneassert "talked about coffee" in result@pytest.mark.asyncio- async def test_search_call_uses_cross_encoder_recipe(self) -> None:- """P-1.4 contract: warm context must use the cross-encoder recipe.-- Pins both the method (``search_`` not ``search``) and the recipe- passed as ``config=``. If a future refactor swaps in a different- recipe, this test fires.- """- mock_client = AsyncMock()- mock_client.search_.return_value = _search_results([])- mock_client.retrieve_episodes.return_value = []-- with patch.object(- context,- "get_graphiti_client",- new_callable=AsyncMock,- return_value=mock_client,- ):+ async def test_search_uses_cross_encoder_recipe(self) -> None:+ """P-1.4 contract: warm context must use the cross-encoder recipe,+ limited to ``context_max_facts``, and five recent episodes."""+ search, recent = self._reads([], [])+ with search as search_mock, recent as recent_mock:await context._fetch(MemoryScope.for_user("abc"), "hello world")- mock_client.search_.assert_awaited_once()- kwargs = mock_client.search_.await_args.kwargs- assert kwargs["query"] == "hello world"- assert kwargs["group_ids"] == ["user_abc"]- # The config is a copy of EDGE_HYBRID_SEARCH_CROSS_ENCODER with the- # limit overridden to context_max_facts. Verify the edge-config- # reranker is still ``cross_encoder`` so the contract is locked.- from graphiti_core.search.search_config import EdgeReranker-- cfg = kwargs["config"]- assert cfg.edge_config is not None- assert cfg.edge_config.reranker == EdgeReranker.cross_encoder- assert cfg.limit == context.graphiti_config.context_max_facts+ search_mock.assert_awaited_once()+ search = search_mock.await_args+ assert search is not None+ assert search.args == (MemoryScope.for_user("abc"), "hello world")+ kwargs = search.kwargs+ assert kwargs["recipe"] is EDGE_HYBRID_SEARCH_CROSS_ENCODER+ assert kwargs["limit"] == context.graphiti_config.context_max_facts+ recent_mock.assert_awaited_once_with(MemoryScope.for_user("abc"), 5)class TestFormatContextWithContent:"""Test _format_context with actual edges and episodes."""def test_with_edges_only(self) -> None:- edge = SimpleNamespace(- fact="user likes coffee",- name="preference",- valid_at="2025-01-01",- invalid_at="present",- )- result = _format_context(edges=[edge], episodes=[])+ result = _format_context(edges=[_edge(fact="user likes coffee")], episodes=[])assert result is not Noneassert "<FACTS>" in result- assert "user likes coffee" in result+ assert (+ " - user likes coffee (valid: 2025-01-01 00:00:00+00:00 — present)"+ in result+ )assert "<temporal_context>" in resultdef test_with_episodes_only(self) -> None:- ep = SimpleNamespace(- content="plain conversation text",- created_at="2025-01-01T00:00:00Z",+ result = _format_context(+ edges=[], episodes=[_episode("plain conversation text")])- result = _format_context(edges=[], episodes=[ep])assert result is not Noneassert "<RECENT_EPISODES>" in resultassert "plain conversation text" in resultdef test_with_both_edges_and_episodes(self) -> None:- edge = SimpleNamespace(- fact="user likes coffee",- valid_at="2025-01-01",- invalid_at=None,+ result = _format_context(+ edges=[_edge(fact="user likes coffee")],+ episodes=[_episode("talked about coffee")],)- ep = SimpleNamespace(- content="talked about coffee",- created_at="2025-06-01T00:00:00Z",- )- result = _format_context(edges=[edge], episodes=[ep])assert result is not Noneassert "<FACTS>" in resultassert "<RECENT_EPISODES>" in resultdef test_global_scope_episode_included(self) -> None:envelope = MemoryEnvelope(content="global note", scope="real:global")- ep = SimpleNamespace(- content=envelope.model_dump_json(),- created_at="2025-01-01T00:00:00Z",+ result = _format_context(+ edges=[], episodes=[_episode(envelope.model_dump_json())])- result = _format_context(edges=[], episodes=[ep])assert result is not Noneassert "<RECENT_EPISODES>" in resultdef test_non_global_scope_episode_excluded(self) -> None:envelope = MemoryEnvelope(content="project note", scope="project:crm")- ep = SimpleNamespace(- content=envelope.model_dump_json(),- created_at="2025-01-01T00:00:00Z",+ result = _format_context(+ edges=[], episodes=[_episode(envelope.model_dump_json())])- result = _format_context(edges=[], episodes=[ep])assert result is None-class TestIsNonGlobalScopeEdgeCases:- """Verify _is_non_global_scope handles non-dict JSON without crashing."""-- def test_list_json_treated_as_global(self) -> None:- assert _is_non_global_scope("[1, 2, 3]") is False-- def test_string_json_treated_as_global(self) -> None:- assert _is_non_global_scope('"just a string"') is False-- def test_null_json_treated_as_global(self) -> None:- assert _is_non_global_scope("null") is False-- def test_plain_text_treated_as_global(self) -> None:- assert _is_non_global_scope("plain conversation text") is False---class TestIsNonGlobalScopeTruncation:- """Verify _is_non_global_scope handles long MemoryEnvelope JSON.-- extract_episode_body() truncates to 500 chars. A MemoryEnvelope with- a long content field serializes to >500 chars, so the truncated string- is invalid JSON. The except clause falls through to return False,- incorrectly treating a project-scoped episode as global.- """-- def test_long_envelope_with_non_global_scope_detected(self) -> None:- """Long MemoryEnvelope JSON should be parsed with raw (untruncated) body."""- envelope = MemoryEnvelope(- content="x" * 600,- source_kind=SourceKind.user_asserted,- scope="project:crm",- memory_kind=MemoryKind.fact,- )- full_json = envelope.model_dump_json()- assert len(full_json) > 500, "precondition: JSON must exceed truncation limit"-- # With the fix: _is_non_global_scope on the raw (untruncated) body- # correctly detects the non-global scope.- assert _is_non_global_scope(full_json) is True-- # Truncated body still fails — that's expected; callers must use raw body.- ep = SimpleNamespace(content=full_json)- truncated = extract_episode_body(ep)- assert _is_non_global_scope(truncated) is False # truncated JSON → parse fails--# ---------------------------------------------------------------------------# Bug: empty <temporal_context> wrapper when all episodes are non-global# ---------------------------------------------------------------------------@@ -311,11 +261,9 @@ class TestFormatContextEmptyWrapper:content="project-only note",scope="project:crm",)- ep = SimpleNamespace(- content=envelope.model_dump_json(),- created_at="2025-01-01T00:00:00Z",+ result = _format_context(+ edges=[], episodes=[_episode(envelope.model_dump_json())])- result = _format_context(edges=[], episodes=[ep])assert result is None@@ -347,9 +295,8 @@ class TestRatificationHitHookFiresFireAndForget:def test_spawn_helper_creates_task_with_retrieved_uuids(self, monkeypatch: pytest.MonkeyPatch) -> None:- """Edges with uuid attrs → fire-and-forget task scheduled with- all of their uuids. Edges missing a uuid are filtered out so- the hook never passes ``None`` to the ratification module."""+ """Retrieved edges → one fire-and-forget task carrying all of their+ uuids."""captured_calls: list[tuple[MemoryScope, list[str]]] = []async def fake_try_ratify(scope: MemoryScope, edge_uuids: list[str]):@@ -362,12 +309,7 @@ class TestRatificationHitHookFiresFireAndForget:# asyncio.create_task needs an event loop — exercise via# run_until_complete instead of an actual task spawn.async def driver():- edges = [- SimpleNamespace(uuid="edge-a"),- SimpleNamespace(uuid="edge-b"),- SimpleNamespace(uuid=None), # filtered- SimpleNamespace(), # no uuid attr at all → filtered- ]+ edges = [_edge("edge-a"), _edge("edge-b")]context._spawn_ratification_hits(MemoryScope.for_expert("user-xyz", "expert-1"), edges=edges)@@ -400,7 +342,7 @@ class TestRatificationHitTaskRetention:monkeypatch.setattr(ratification_mod, "try_ratify_on_hit", fake_try_ratify)context._spawn_ratification_hits(- MemoryScope.for_user("user-xyz"), edges=[SimpleNamespace(uuid="edge-a")]+ MemoryScope.for_user("user-xyz"), edges=[_edge("edge-a")])# Strong ref held while the task is in flight.assert len(context._pending_hit_tasks) == 1@@ -429,7 +371,7 @@ class TestRatificationHitTaskRetention:monkeypatch.setattr(ratification_mod, "try_ratify_on_hit", fake_try_ratify)context._spawn_ratification_hits(- MemoryScope.for_user("user-xyz"), edges=[SimpleNamespace(uuid="edge-a")]+ MemoryScope.for_user("user-xyz"), edges=[_edge("edge-a")])task = next(iter(context._pending_hit_tasks))await asyncio.gather(task, return_exceptions=True)diff --git a/autogpt_platform/backend/backend/copilot/graphiti/dream_ratification_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/dream_ratification_integration_test.pyindex 4213badf02..fadf55b2ff 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/dream_ratification_integration_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/dream_ratification_integration_test.py@@ -385,7 +385,7 @@ async def test_sweep_promotes_a_tentative_edge_that_earned_a_hit(dream_graph) ->"""Promote leg of the SWEEP: distinct from the warm-context hook.``try_ratify_on_hit`` promotes via ``_promote_if_tentative``; the sweep- reaches ``_promote_edge`` only through ``_process_edge``'s ``hits >= 1``+ reaches that same write only through ``_process_edge``'s ``hits >= 1``branch, keyed off the Redis counter — this is the test that makes the``_FakeRedis`` counter load-bearing."""diff --git a/autogpt_platform/backend/backend/copilot/graphiti/falkordb_driver.py b/autogpt_platform/backend/backend/copilot/graphiti/falkordb_driver.pyindex 70fb6f2159..355b00358a 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/falkordb_driver.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/falkordb_driver.py@@ -148,6 +148,13 @@ class AutoGPTFalkorDriver(FalkorDriver):self._build_indices_at_init = build_indicessuper().__init__(*args, **kwargs)+ @property+ def graph_name(self) -> str:+ """The graph this driver reads and writes: a memory scope's+ ``group_id``, which also names the graph's write lock+ (``scope_lock.py``)."""+ return self._database+async def build_indices_and_constraints(self) -> None: # type: ignore[override]if not self._build_indices_at_init:# Default path. Suppresses graphiti-core's init-time task so adiff --git a/autogpt_platform/backend/backend/copilot/graphiti/graphiti_forget_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/graphiti_forget_integration_test.pyindex 9b94974ead..be31f845a2 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/graphiti_forget_integration_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/graphiti_forget_integration_test.py@@ -20,8 +20,6 @@ runaway-demotion footgun.import pytestfrom backend.copilot.tools.graphiti_forget import (- _retract_edges,- _soft_delete_edges,invalidate_entity_direct_neighbors,mark_edges_superseded,)@@ -42,38 +40,8 @@ async def _select_edge(driver, uuid: str) -> dict | None:return records[0] if records else None-async def test_retract_edges_sets_only_expired_at(seeded_graph) -> None:- """Per Snodgrass — system retraction sets expired_at only."""- driver, group_id = seeded_graph-- deleted, failed = await _retract_edges(driver, ["e1"], "test-user")- assert deleted == ["e1"]- assert failed == []-- row = await _select_edge(driver, "e1")- assert row is not None- assert row["expired_at"] is not None, "_retract_edges must set expired_at"- assert row["invalid_at"] is None, (- "_retract_edges must NOT set invalid_at — that's the contradiction-detector "- "path. Conflating the two breaks the bi-temporal model."- )---async def test_soft_delete_edges_sets_both(seeded_graph) -> None:- """Contradiction-detector path keeps the original both-timestamps behavior."""- driver, group_id = seeded_graph-- deleted, _ = await _soft_delete_edges(driver, ["e1"], "test-user")- assert deleted == ["e1"]-- row = await _select_edge(driver, "e1")- assert row is not None- assert row["expired_at"] is not None- assert row["invalid_at"] is not None+# The user-forget retraction (``expired_at`` + ``status='retracted'``,+# never ``invalid_at``) is pinned live in ``recall_integration_test.py``.@pytest.mark.integrationdiff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest.pyindex 89cb4a110c..fb0b955585 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/ingest.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/ingest.py@@ -9,7 +9,10 @@ import asyncioimport loggingimport weakreffrom datetime import datetime, timezone+from typing import Any+from graphiti_core import Graphiti+from graphiti_core.graphiti import AddEpisodeResultsfrom graphiti_core.nodes import EpisodeTypefrom backend.copilot.dream.registry import ensure_scope_scheduled@@ -17,7 +20,10 @@ from backend.util.background import spawn_background_taskfrom .client import ensure_indices_once, get_graphiti_clientfrom .memory_model import MemoryEnvelope, MemoryKind, MemoryStatus, SourceKind+from .recall_citations import Citations, rests_on_a_forget+from .recall_ingest import previous_episode_uuidsfrom .scope import MemoryScope+from .scope_lock import INGEST_LOCK_WAIT_SECONDS, LockState, graph_write_lockfrom .types import EDGE_TYPE_MAP, EDGE_TYPES, ENTITY_TYPESlogger = logging.getLogger(__name__)@@ -58,6 +64,15 @@ def _get_loop_state() -> _LoopIngestState:# Idle workers are cleaned up after this many seconds of inactivity._WORKER_IDLE_TIMEOUT = 60+# An episode that found its graph's write lock held for a whole+# ``INGEST_LOCK_WAIT_SECONDS`` goes to the back of the queue after a pause,+# doubling from this with each wait up to the cap, and is tried again as often+# as it takes (``_write_or_requeue``).+INGEST_RETRY_PAUSE_SECONDS = 1.0+INGEST_RETRY_PAUSE_MAX_SECONDS = 30.0+# Every this many waits, a warning names the episode still unwritten.+INGEST_LOCK_WARN_EVERY = 3+class MemoryScopeViolationError(ValueError):"""An ingestion payload targeted a different memory group than its worker.@@ -73,7 +88,7 @@ class IngestionCompletion:A memory group's ingestion queue is shared between live-chat ingestion anddream-pass writes. A caller that must wait for only its own episodes to- land (dream-pass apply) creates one of these, passes it to each+ be finished (dream-pass apply) creates one of these, passes it to each``enqueue_episode`` it makes, and awaits it. Unrelated activity on thesame queue — chat episodes enqueued before, during, or after — neverregisters on this tracker, so it cannot extend the wait.@@ -83,20 +98,47 @@ class IngestionCompletion:the counters need no locking. Counts are monotonic (registered /completed) rather than a single decrementing balance, so a transientinterleave can never drive the outstanding count negative.++ It also counts what became of each episode, apart from the drain: so a+ caller reports as written only what graphiti saved, and the rest as+ pending or failed."""- __slots__ = ("_registered", "_completed", "_event")+ __slots__ = (+ "_registered",+ "_completed",+ "_event",+ "written",+ "dropped_forgotten",+ "failed",+ )def __init__(self) -> None:self._registered = 0self._completed = 0self._event = asyncio.Event()self._event.set() # nothing outstanding yet+ # Episodes graphiti saved (``add_episode`` returned).+ self.written = 0+ # Episodes the worker dropped, unwritten, for resting on a forget+ # made since they were queued (``recall_citations.py``).+ self.dropped_forgotten = 0+ # Episodes whose write raised: dropped, logged, not retried.+ self.failed = 0@propertydef registered(self) -> int:return self._registered+ @property+ def pending(self) -> int:+ """Episodes neither written, dropped nor failed yet: still queued,+ waiting for their graph's write lock, or being written. The queue+ lives in the process, so these are lost if it stops."""+ return max(+ 0, self._registered - self.written - self.dropped_forgotten - self.failed+ )+def register(self) -> None:"""Record one more episode this tracker is waiting on."""self._registered += 1@@ -148,10 +190,11 @@ CUSTOM_EXTRACTION_INSTRUCTIONS = """# Cypher that overwrites exactly the five envelope-sourced MemoryFact# props on a known set of edge uuids. group_id predicate is tenant-# defense-in-depth (mirrors apply._apply_demotions).+# defense-in-depth (mirrors apply._apply_demotions); the forgotten test+# keeps it off a fact a forget reached first._STAMP_EDGE_METADATA_QUERY = """MATCH ()-[e:RELATES_TO]->()-WHERE e.uuid IN $uuids AND e.group_id = $gid+WHERE e.uuid IN $uuids AND e.group_id = $gid AND e.forgotten_at IS NULLSET e.status = $status,e.source_kind = $source_kind,e.scope = $scope,@@ -293,41 +336,18 @@ async def _ingestion_worker(user_id: str, group_id: str, queue: asyncio.Queue) -# up front so it is signalled in the finally below even if the# graph write raises. See ``IngestionCompletion``.completion: IngestionCompletion | None = payload.pop("_completion", None)+ waits = int(payload.pop("_lock_waits", 0))+ requeued = Falsetry:if payload.get("group_id") != group_id:raise MemoryScopeViolationError("Ingestion payload memory group mismatch")- client = await get_graphiti_client(group_id)- # This is the write path, so materializing the graph is- # intended here — unlike driver construction, which must- # never create one. Once per group per loop.- await ensure_indices_once(group_id, client)- # ``_edge_metadata`` is a sidecar (not an add_episode kwarg) —- # pop it before the **payload spread. Present only for dream- # writes; None for conversation turns / memory-store calls.- edge_metadata = payload.pop("_edge_metadata", None)- # Pass custom entity + edge types so MemoryEnvelope metadata- # (status, confidence, source_kind, scope, provenance) lives- # on :RELATES_TO edges and not only inside :Episodic.content.- # Single point of wire-in for every caller of this worker.- result = await client.add_episode(- **payload,- entity_types=ENTITY_TYPES,- edge_types=EDGE_TYPES,- edge_type_map=EDGE_TYPE_MAP,+ requeued = await _write_or_requeue(+ user_id, group_id, queue, payload, completion, waits)- # graphiti's attribute extraction fills MemoryFact fields from- # the episode text, not the envelope, so dream metadata- # (source_kind/provenance/exact status) doesn't survive. Stamp- # it deterministically onto the edges THIS episode newly- # created — see ``_stamp_edge_metadata`` for the dedup-safety- # invariant that prevents clobbering user-authored edges.- if edge_metadata:- await _stamp_edge_metadata(- client, group_id, result, edge_metadata, user_id- )except MemoryScopeViolationError:+ _count_failed(completion)logger.error("MEMORY ISOLATION VIOLATION: ingestion payload for user %s ""targeted group %r but the worker owns group %r — "@@ -337,6 +357,7 @@ async def _ingestion_worker(user_id: str, group_id: str, queue: asyncio.Queue) -group_id,)except Exception:+ _count_failed(completion)logger.warning("Graphiti ingestion failed for user %s",user_id[:12],@@ -346,8 +367,9 @@ async def _ingestion_worker(user_id: str, group_id: str, queue: asyncio.Queue) -queue.task_done()# Signal completion for the enqueuer's drain barrier even on# failure — a failed write is still "no longer pending", and- # leaving it outstanding would hang the caller's wait.- if completion is not None:+ # leaving it outstanding would hang the caller's wait. A+ # requeued episode completes when its retry is processed.+ if completion is not None and not requeued:completion.complete_one()except asyncio.CancelledError:logger.debug("Ingestion worker cancelled for user %s", user_id[:12])@@ -371,6 +393,179 @@ async def _ingestion_worker(user_id: str, group_id: str, queue: asyncio.Queue) -)+async def _write_locked(+ user_id: str,+ group_id: str,+ payload: dict[str, Any],+ completion: IngestionCompletion | None,+) -> bool:+ """Write one episode holding the graph's write lock (``scope_lock.py``),+ so no forget lands between what graphiti reads and what it saves over+ it; False, writing nothing, when another writer kept it for the wait.+ A dream write resting on a forget is dropped under the same lock."""+ wait = INGEST_LOCK_WAIT_SECONDS+ async with graph_write_lock(group_id, wait_seconds=wait) as lock:+ if lock is LockState.BUSY:+ return False+ client = await get_graphiti_client(group_id)+ if await _dropped_as_forgotten(client, payload, completion):+ return True+ # This is the write path, so materializing the graph is intended+ # here — unlike driver construction, which must never create one.+ # Once per group per loop.+ await ensure_indices_once(group_id, client)+ # ``_edge_metadata`` is a sidecar (not an add_episode kwarg) — pop it+ # before the **payload spread. Present only for dream writes.+ edge_metadata = payload.pop("_edge_metadata", None)+ result = await _add_episode(client, group_id, payload)+ await _undo_extracted_retractions(client, result)+ # graphiti's attribute extraction fills MemoryFact fields from the+ # episode text, not the envelope, so dream metadata doesn't survive:+ # stamp it onto the edges THIS episode newly created (see+ # ``_stamp_edge_metadata`` for the dedup-safety invariant).+ if edge_metadata:+ await _stamp_edge_metadata(client, group_id, result, edge_metadata, user_id)+ if completion is not None:+ completion.written += 1+ return True+++async def _undo_extracted_retractions(+ client: Graphiti, result: AddEpisodeResults+) -> None:+ """Put back to ``active`` an edge this episode wrote whose status+ graphiti's attribute extraction set to ``retracted``. Its schema+ (``types.MemoryFact``) leaves the value out, but graphiti does not hold+ the model to it, and only a forget retracts: it also stamps+ ``forgotten_at``, and an edge carrying that is never touched. Run under+ the graph's write lock, like the write itself. Best-effort: the episode+ is written either way."""+ try:+ uuids = [edge.uuid for edge in result.edges]+ if uuids:+ await client.driver.execute_query(+ _UNDO_EXTRACTED_RETRACTIONS_QUERY,+ uuids=uuids,+ retracted=MemoryStatus.retracted.value,+ active=MemoryStatus.active.value,+ )+ except Exception:+ logger.warning(+ "Could not check the statuses an episode's edges were given",+ exc_info=True,+ )+++_UNDO_EXTRACTED_RETRACTIONS_QUERY = """+MATCH ()-[e:RELATES_TO]->()+WHERE e.uuid IN $uuids AND e.forgotten_at IS NULL AND e.status = $retracted+SET e.status = $active+"""+++async def _dropped_as_forgotten(+ client: Graphiti,+ payload: dict[str, Any],+ completion: IngestionCompletion | None,+) -> bool:+ """True, counting it on ``completion``, when ``payload`` is a dream write+ that rests on a forget made since the dream read the graph."""+ reason = await rests_on_a_forget(client.driver, payload.pop("_citations", None))+ if reason is None:+ return False+ logger.info(f"Dropped dream write {payload.get('name')!r}: it {reason}")+ if completion is not None:+ completion.dropped_forgotten += 1+ return True+++async def _write_or_requeue(+ user_id: str,+ group_id: str,+ queue: asyncio.Queue,+ payload: dict[str, Any],+ completion: IngestionCompletion | None,+ waits: int,+) -> bool:+ """Write the episode holding its graph's lock (``_write_locked``); True+ when, the lock held by another writer for a whole wait, the episode went+ to the back of the queue instead, after a pause (``_pause_before_retry``).+ Waiters take turns at the lock (``scope_lock.py``), so contention delays+ an episode but never drops it while the process runs. With no room in+ the queue behind the others, it is tried again here, holding no slot."""+ while not await _write_locked(user_id, group_id, payload, completion):+ waits += 1+ _note_lock_wait(payload, waits)+ await _pause_before_retry(waits)+ try:+ queue.put_nowait(+ {**payload, "_lock_waits": waits, "_completion": completion}+ )+ except asyncio.QueueFull:+ continue+ return True+ return False+++async def _pause_before_retry(waits: int) -> None:+ await asyncio.sleep(_retry_pause(waits))+++def _retry_pause(waits: int) -> float:+ """The pause before the *waits*-th retry: ``INGEST_RETRY_PAUSE_SECONDS``,+ doubling with each wait up to ``INGEST_RETRY_PAUSE_MAX_SECONDS``."""+ doubled = INGEST_RETRY_PAUSE_SECONDS * 2.0 ** min(waits - 1, 16)+ return min(INGEST_RETRY_PAUSE_MAX_SECONDS, doubled)+++def _note_lock_wait(payload: dict[str, Any], waits: int) -> None:+ """Log a lock wait; every ``INGEST_LOCK_WARN_EVERY`` waits a warning that+ names the episode still unwritten."""+ group = str(payload.get("group_id"))[:20]+ if waits % INGEST_LOCK_WARN_EVERY:+ logger.info(f"Graph {group} locked by another writer; episode requeued")+ return+ logger.warning(+ f"Graph {group} locked by another writer for {waits} waits; the episode "+ f"{payload.get('name')!r} is still queued, unwritten"+ )+++def _count_failed(completion: IngestionCompletion | None) -> None:+ if completion is not None:+ completion.failed += 1+++async def _add_episode(+ client: Graphiti, group_id: str, payload: dict[str, Any]+) -> AddEpisodeResults:+ """graphiti's ``add_episode`` with our types, under the recall policy.++ The custom entity and edge types keep MemoryEnvelope metadata (status,+ confidence, source_kind, scope, provenance) on ``RELATES_TO`` edges, not+ only inside ``Episodic.content``; this is the single wire-in for every+ caller of the worker. The earlier episodes are the ones graphiti would+ show its extraction prompts, minus those a forget hid: left to pick+ them itself it would show a forgotten episode again. The caller holds+ the graph's write lock, and graphiti's model client never lets its model+ merge a new statement into a forgotten edge (``recall_ingest.py``), so+ the write leaves every forget's marker, audit copies and recall as it+ found them. (graphiti's exact-text match, which runs before the model,+ can still list a new episode on a forgotten edge whose ``[forgotten]``+ text the statement repeats word for word.)+ """+ previous = await previous_episode_uuids(+ client.driver, group_id, payload["reference_time"], payload["source"]+ )+ return await client.add_episode(+ **payload,+ previous_episode_uuids=previous,+ entity_types=ENTITY_TYPES,+ edge_types=EDGE_TYPES,+ edge_type_map=EDGE_TYPE_MAP,+ )++async def enqueue_conversation_turn(user_id: str,session_id: str,@@ -466,6 +661,7 @@ async def enqueue_episode(is_json: bool = False,edge_metadata: dict | None = None,completion: IngestionCompletion | None = None,+ citations: Citations | None = None,) -> bool:"""Enqueue an arbitrary episode for background ingestion in ``scope``.@@ -488,6 +684,11 @@ async def enqueue_episode(await ONLY its own episodes (scoped drain), not everything on theshared memory-group queue. ``None`` for chat / memory-store writesthat are pure fire-and-forget.+ citations: What a dream write rests on. The worker checks them under+ the graph's write lock right before writing and drops the+ episode, counting it on ``completion``, when a forget made since+ the dream read the graph reached them (``recall_citations.py``).+ ``None`` for chat / memory-store writes.Returns ``True`` if the episode was queued, ``False`` if it was dropped.The caller registers the episode on ``completion`` iff this returns@@ -524,6 +725,8 @@ async def enqueue_episode(# Sidecar — the worker calls ``complete_one`` on it after# processing so a scoped-drain caller can await this episode."_completion": completion,+ # Sidecar — popped and checked by the worker under the lock.+ "_citations": citations,},)if not queued:@@ -537,7 +740,8 @@ async def enqueue_episode(async def wait_for_ingestion(completion: IngestionCompletion, timeout_seconds: float) -> bool:- """Block until a specific set of enqueued episodes have all landed.+ """Block until the worker has finished a specific set of enqueued+ episodes: each written, dropped for resting on a forget, or failed.``enqueue_episode`` returning ``True`` only proves the episode reachedthe in-process queue; the real graph write (LLM extraction + embeddingdiff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest_recall_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest_recall_integration_test.pynew file mode 100644index 0000000000..417a251e6a--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/ingest_recall_integration_test.py@@ -0,0 +1,269 @@+"""Ingestion under the recall policy, against a live FalkorDB: what graphiti's+own prompts are shown after a forget.++graphiti's ``add_episode`` shows its extraction prompts the newest earlier+episodes as context. Left to pick them itself it cannot see a forget, so a+forgotten episode's text went back to the extractor with every later one.+Its entity resolution also reads the summaries of the entities a new episode+may be about, which graphiti wrote from fact sentences, and its edge+resolution offers every existing edge, the retracted one included, as a+duplicate or contradiction candidate. A forget therefore leaves a placeholder+where the sentence was and blanks those summaries (``recall_hide.py``).++These drive the production worker (``ingest.enqueue_episode``) with only the+LLM boundary scripted and read every message the model was sent. A status of+``retracted`` the extraction model gives a fact is no forget: the worker puts+it back to ``active``. Nor is a forget's marker it invents: none is saved.+``recall_ingest_integration_test.py`` covers a forgotten fact stated again.++Run with FalkorDB reachable (see ``conftest.py``)::++ poetry run pytest -m integration backend/copilot/graphiti/ingest_recall_integration_test.py+"""++from collections.abc import AsyncIterator+from datetime import datetime+from unittest.mock import AsyncMock++import pytest+import pytest_asyncio+from graphiti_core.edges import EntityEdge+from pytest_mock import MockerFixture++from backend.copilot.model import ChatSession+from backend.copilot.tools import graphiti_forget+from backend.copilot.tools.models import (+ MemoryForgetCandidatesResponse,+ MemoryForgetConfirmResponse,+)++from . import ingest+from .falkordb_driver import AutoGPTFalkorDriver+from .migrations.backfill_legacy_forgets import ForgetMarkers, scan_markers+from .recall import FORGOTTEN_FACT, is_forgotten, is_live+from .recall_forget import retract+from .recall_integration_fixtures import (+ ALICE,+ BOB,+ CAROL,+ edge_row,+ episode_row,+ ingest_facts,+ ingest_through_the_worker,+ live_facts,+ model_of,+ patch_recall_boundaries,+ rows,+ scripted_responses,+ stop_ingestion_workers,+)+from .scope import MemoryScope+from .types import POLICY_EDGE_FIELDS++_INGEST_TIMEOUT_SECONDS = 25.0+# Text only an episode body holds: no fact, entity or summary repeats it, so+# a prompt can only have it from the episode itself.+_FORGOTTEN_NOTE = "She said so over lunch on the twelfth."+_KEPT_NOTE = "Carol mentioned it during the Monday stand-up."+++@pytest_asyncio.fixture(loop_scope="function")+async def ingest_worker_cleanup(mocker: MockerFixture) -> AsyncIterator[None]:+ """No dream registration on a first write; no idle worker left behind."""+ mocker.patch(+ "backend.copilot.graphiti.ingest.ensure_scope_scheduled",+ AsyncMock(return_value=None),+ )+ yield+ await stop_ingestion_workers()+++async def _ingest_through_the_worker(scope: MemoryScope, body: str) -> None:+ completion = ingest.IngestionCompletion()+ queued = await ingest.enqueue_episode(+ scope, "session-bob", name="bob", episode_body=body, completion=completion+ )+ assert queued+ completion.register()+ assert await ingest.wait_for_ingestion(completion, _INGEST_TIMEOUT_SECONDS)+++async def _summaries(driver: AutoGPTFalkorDriver) -> dict[str, str]:+ found = await rows(+ driver, "MATCH (n:Entity) RETURN n.name AS name, n.summary AS summary"+ )+ return {row["name"]: row["summary"] or "" for row in found}+++async def test_extraction_is_never_shown_a_forgotten_episode(+ scope_graph, stub_graphiti_client, mocker, ingest_worker_cleanup+) -> None:+ driver, scope = scope_graph+ forgotten, edges = await ingest_facts(+ driver,+ scope,+ stub_graphiti_client,+ [ALICE],+ body=f"{ALICE[2]}. {_FORGOTTEN_NOTE}",+ )+ kept, _ = await ingest_facts(+ driver, scope, stub_graphiti_client, [CAROL], body=f"{CAROL[2]}. {_KEPT_NOTE}"+ )+ await retract(scope, [edges[ALICE[2]]])+ client = stub_graphiti_client(driver, scripted_responses([BOB]))+ generate = mocker.spy(model_of(client), "_generate_response")+ add_episode = mocker.spy(client, "add_episode")+ mocker.patch.object(ingest, "get_graphiti_client", AsyncMock(return_value=client))++ await _ingest_through_the_worker(scope, BOB[2])++ prompts = [m.content for call in generate.call_args_list for m in call.args[0]]+ assert any(_KEPT_NOTE in prompt for prompt in prompts), "no earlier episodes"+ leaked = [prompt for prompt in prompts if _FORGOTTEN_NOTE in prompt]+ assert leaked == [], "the extractor was shown a forgotten episode"+ context = add_episode.call_args.kwargs["previous_episode_uuids"]+ assert kept in context and forgotten not in context+++async def test_no_later_prompt_carries_a_forgotten_sentence(+ scope_graph, stub_graphiti_client, mocker, ingest_worker_cleanup+) -> None:+ """Ingest Alice, forget her, ingest Bob: not one message graphiti sends+ the model for Bob holds the Alice sentence, although its entity and edge+ resolution both ran over what the forget left behind."""+ driver, scope = scope_graph+ first = stub_graphiti_client(driver, scripted_responses([ALICE]))+ await ingest_through_the_worker(driver, scope, first, [ALICE], session_id="s-alice")+ [alice] = await live_facts(driver)+ assert any(ALICE[2] in summary for summary in (await _summaries(driver)).values())++ await retract(scope, [alice])++ assert set((await _summaries(driver)).values()) == {""}, "summaries blanked"+ client = stub_graphiti_client(driver, scripted_responses([BOB]))+ generate = mocker.spy(model_of(client), "_generate_response")+ await ingest_through_the_worker(driver, scope, client, [BOB], session_id="s-bob")++ calls = generate.call_args_list+ asked = {call.args[1].__name__ for call in calls if call.args[1] is not None}+ assert {"NodeResolutions", "EdgeDuplicate"} <= asked, "both resolutions ran"+ prompts = [m.content for call in calls for m in call.args[0]]+ assert any(FORGOTTEN_FACT in prompt for prompt in prompts), "edge offered"+ assert [prompt for prompt in prompts if ALICE[2] in prompt] == []+ assert list((await live_facts(driver)).values()) == [BOB[2]]+++async def test_a_retraction_the_extraction_gives_is_no_forget(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup+) -> None:+ """ "I retracted my application at Acme": graphiti does not hold its+ extraction model to ``MemoryFact``'s statuses, and this one answers+ ``retracted``. Only a forget retracts, so the worker puts the fact back+ to active: it stays live, and its episode is not redacted."""+ driver, scope = scope_graph+ client = stub_graphiti_client(+ driver, scripted_responses([ALICE], status="retracted")+ )++ episode = await ingest_through_the_worker(+ driver, scope, client, [ALICE], session_id="s-alice"+ )++ [alice] = await live_facts(driver)+ row = await edge_row(driver, alice)+ assert (row["status"], row["forgotten_at"], row["fact"]) == (+ "active",+ None,+ ALICE[2],+ )+ assert (await episode_row(driver, episode))["redacted_at"] is None+++async def test_a_forget_marker_the_extraction_invents_is_never_saved(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup, mocker: MockerFixture+) -> None:+ """Codex's probe: graphiti-core 0.30.2 saves every key its extraction+ model answers, and this one answers a ``forgotten_at`` and audit copies+ holding a sentence nobody stated. The worker saves none of them: the fact+ stays live, and nothing ``memory_forget_search`` or+ ``memory_forget_confirm`` answers holds the sentence, before the fact is+ forgotten or after."""+ invented = "Alice works on the secret Atlas plan"+ driver, scope = scope_graph+ responses = scripted_responses([BOB])+ responses["MemoryFact"] |= dict.fromkeys(POLICY_EDGE_FIELDS, invented)+ client = stub_graphiti_client(driver, responses)++ await ingest_through_the_worker(driver, scope, client, [BOB], session_id="s-bob")++ [bob] = await live_facts(driver)+ stored = await rows(+ driver,+ "MATCH ()-[e:RELATES_TO {uuid: $uuid}]->() RETURN properties(e) AS p",+ uuid=bob,+ )+ assert POLICY_EDGE_FIELDS.isdisjoint(stored[0]["p"])+ [edge] = await EntityEdge.get_by_uuids(driver, [bob])+ assert is_live(edge) and not is_forgotten(edge)++ patch_recall_boundaries(mocker, driver, stub_graphiti_client)+ mocker.patch.object(+ graphiti_forget, "is_enabled_for_user", AsyncMock(return_value=True)+ )+ user, session = scope.owner_user_id, ChatSession.new(+ scope.owner_user_id, dry_run=False+ )+ search = graphiti_forget.MemoryForgetSearchTool()+ confirm = graphiti_forget.MemoryForgetConfirmTool()+ live = await search._execute(user, session, query="Atlas", include_forgotten=True)+ soft = await confirm._execute(user, session, uuids=[bob])+ gone = await search._execute(user, session, query="Atlas", include_forgotten=True)+ hard = await confirm._execute(user, session, uuids=[bob], hard_delete=True)++ assert isinstance(live, MemoryForgetCandidatesResponse)+ assert [(c["uuid"], c.get("forgotten")) for c in live.candidates] == [(bob, None)]+ assert isinstance(gone, MemoryForgetCandidatesResponse)+ [candidate] = gone.candidates+ assert (candidate["uuid"], candidate["forgotten"]) == (bob, "true")+ assert datetime.fromisoformat(candidate["forgotten_at"]).tzinfo is not None+ for done in (soft, hard):+ assert isinstance(done, MemoryForgetConfirmResponse)+ assert done.deleted_uuids == [bob]+ for answer in (live, soft, gone, hard):+ assert invented not in answer.model_dump_json()+ assert await edge_row(driver, bob) == {}+++async def test_the_marker_scan_tells_an_invented_marker_from_a_forget(+ scope_graph, stub_graphiti_client+) -> None:+ """The pre-deploy scan (``backfill_legacy_forgets --scan-markers``) lists+ both marked facts by uuid, and as unscrubbed only the one whose marker a+ past extraction invented: a forget scrubs the sentence it marks."""+ driver, scope = scope_graph+ _, edges = await ingest_facts(driver, scope, stub_graphiti_client, [ALICE, BOB])+ alice, bob = edges[ALICE[2]], edges[BOB[2]]+ await retract(scope, [alice])+ await driver.execute_query(+ "MATCH ()-[e:RELATES_TO {uuid: $uuid}]->() SET e.forgotten_at = $invented",+ uuid=bob,+ invented="2026-01-01T00:00:00+00:00",+ )++ found = await scan_markers(scope.group_id)++ assert found == [+ ForgetMarkers(+ graph=scope.group_id, marked=sorted([alice, bob]), unscrubbed=[bob]+ )+ ]diff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest_recall_test.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest_recall_test.pynew file mode 100644index 0000000000..304fd73088--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/ingest_recall_test.py@@ -0,0 +1,330 @@+"""The ingestion worker hands graphiti's extraction only the earlier episodes+the recall policy allows, writes each episode holding the graph's write lock,+and puts back to ``active`` any edge the extraction gave ``retracted``,+mocked at the graphiti boundary and on the in-memory Redis.++The live runs are ``ingest_recall_integration_test.py`` and+``recall_inflight_integration_test.py``.+"""++import asyncio+from datetime import datetime, timezone+from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from graphiti_core.nodes import EpisodeType+from graphiti_core.search.search_utils import RELEVANT_SCHEMA_LIMIT++from . import ingest, scope_lock+from .recall import is_recallable_episode, recallable_episode_predicate+from .recall_fake_redis import FakeRedis+from .scope import write_lock_key++_NOW = datetime(2026, 9, 26, 12, 0, tzinfo=timezone.utc)+_LOCK = write_lock_key("user_test")+# What the episode store holds, newest first as the query orders them.+_STORED = [+ {"uuid": "ep-newest", "redacted": False, "entity_edges": ["kept"]},+ {"uuid": "ep-redacted", "redacted": True, "entity_edges": []},+ {"uuid": "ep-cites-a-forgotten-fact", "redacted": False, "entity_edges": ["gone"]},+ {"uuid": "ep-oldest", "redacted": False, "entity_edges": []},+]+++async def _policy_filtered_store(query: str, **params: object):+ """The graph's answer to a recallable-episodes read, filtered with the+ predicate's Python twin; any other read would be unfiltered."""+ assert recallable_episode_predicate("e") in query, "an unfiltered episode read"+ rows = [+ {"uuid": episode["uuid"]}+ for episode in _STORED+ if is_recallable_episode(+ episode["entity_edges"], {"gone"}, redacted=episode["redacted"]+ )+ ]+ return rows, [], None+++def _payload(**extra: object) -> dict:+ return {+ "name": "conversation_s1",+ "episode_body": "User: Bob leads Atlas",+ "source": EpisodeType.message,+ "source_description": "User message in session s1",+ "reference_time": _NOW,+ "group_id": "user_test",+ **extra,+ }+++async def _run_worker(+ client: MagicMock, monkeypatch: pytest.MonkeyPatch, *payloads: dict+) -> None:+ queue: asyncio.Queue = asyncio.Queue(maxsize=10)+ for payload in payloads or (_payload(),):+ queue.put_nowait(payload)+ monkeypatch.setattr(ingest, "_WORKER_IDLE_TIMEOUT", 0.05)+ with (+ patch.object(ingest, "get_graphiti_client", AsyncMock(return_value=client)),+ patch.object(ingest, "ensure_indices_once", AsyncMock()),+ ):+ worker = ingest._ingestion_worker("test-user", "user_test", queue)+ await asyncio.wait_for(worker, 10)+++def _graphiti_client() -> MagicMock:+ client = MagicMock()+ client.add_episode = AsyncMock()+ client.driver.execute_query = AsyncMock(side_effect=_policy_filtered_store)+ return client+++def _undo_calls(client: MagicMock) -> list:+ return [+ call+ for call in client.driver.execute_query.await_args_list+ if call.args[0] == ingest._UNDO_EXTRACTED_RETRACTIONS_QUERY+ ]+++class TestExtractionContext:+ @pytest.mark.asyncio+ async def test_add_episode_gets_only_recallable_earlier_episodes(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ client = _graphiti_client()++ await _run_worker(client, monkeypatch)++ client.add_episode.assert_awaited_once()+ assert client.add_episode.await_args is not None+ previous = client.add_episode.await_args.kwargs["previous_episode_uuids"]+ assert previous == ["ep-oldest", "ep-newest"], "oldest first, as graphiti"+ assert "ep-redacted" not in previous+ [read] = client.driver.execute_query.await_args_list+ assert read.kwargs == {+ "group_id": "user_test",+ "reference_time": _NOW,+ "source": "message",+ "limit": RELEVANT_SCHEMA_LIMIT,+ }++ @pytest.mark.asyncio+ async def test_a_failed_read_still_writes_without_earlier_episodes(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """``None`` would let graphiti pick for itself, forgotten episodes+ included; the episode is still written."""+ client = _graphiti_client()+ client.driver.execute_query = AsyncMock(side_effect=RuntimeError("down"))++ await _run_worker(client, monkeypatch)++ assert client.add_episode.await_args is not None+ assert client.add_episode.await_args.kwargs["previous_episode_uuids"] == []+++class TestAnExtractedRetraction:+ @pytest.mark.asyncio+ async def test_is_put_back_to_active_while_the_lock_is_held(+ self, lock_redis: FakeRedis, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """graphiti does not hold its extraction model to ``MemoryFact``'s+ statuses, so the worker puts any edge the episode wrote back from+ ``retracted`` to ``active``, under the lock the write held; the query+ never touches an edge a forget marked."""+ client = _graphiti_client()+ written = [SimpleNamespace(uuid="e1"), SimpleNamespace(uuid="e2")]+ client.add_episode.return_value = SimpleNamespace(edges=written)+ held: list[bool] = []++ async def store(query: str, **params: object):+ if query == ingest._UNDO_EXTRACTED_RETRACTIONS_QUERY:+ held.append(_LOCK in lock_redis.values)+ return [], [], None+ return await _policy_filtered_store(query, **params)++ client.driver.execute_query = AsyncMock(side_effect=store)++ await _run_worker(client, monkeypatch)++ [undo] = _undo_calls(client)+ assert undo.kwargs == {+ "uuids": ["e1", "e2"],+ "retracted": "retracted",+ "active": "active",+ }+ assert held == [True]+ assert "e.forgotten_at IS NULL" in ingest._UNDO_EXTRACTED_RETRACTIONS_QUERY++ @pytest.mark.asyncio+ async def test_that_cannot_be_checked_leaves_the_episode_written(+ self,+ lock_redis: FakeRedis,+ monkeypatch: pytest.MonkeyPatch,+ caplog: pytest.LogCaptureFixture,+ ) -> None:+ client = _graphiti_client()+ client.add_episode.return_value = SimpleNamespace(+ edges=[SimpleNamespace(uuid="e1")]+ )++ async def store(query: str, **params: object):+ if query == ingest._UNDO_EXTRACTED_RETRACTIONS_QUERY:+ raise ConnectionError("falkordb down")+ return await _policy_filtered_store(query, **params)++ client.driver.execute_query = AsyncMock(side_effect=store)++ await _run_worker(client, monkeypatch)++ client.add_episode.assert_awaited_once()+ assert len(_undo_calls(client)) == 1+ assert "Could not check the statuses" in caplog.text+ assert "Graphiti ingestion failed" not in caplog.text+++class TestWriteLock:+ @pytest.mark.asyncio+ async def test_the_episode_is_written_holding_the_graphs_lock(+ self, lock_redis: FakeRedis, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ client = _graphiti_client()+ held: list[bool] = []+ client.add_episode.side_effect = lambda **_: held.append(+ _LOCK in lock_redis.values+ )++ await _run_worker(client, monkeypatch)++ assert held == [True]+ assert _LOCK not in lock_redis.values, "released afterwards"++ @pytest.mark.asyncio+ async def test_a_graph_a_forget_keeps_locked_is_tried_again(+ self, lock_redis: FakeRedis, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """The first attempt writes nothing; the forget finishes while the+ episode pauses, and the episode, back at the end of the queue, is+ written, and only then does the enqueuer's completion count it."""+ client = _graphiti_client()+ lock_redis.values[_LOCK] = "a forget's token"+ completion = ingest.IngestionCompletion()+ completion.register()++ async def forget_finishes(waits: int) -> None:+ assert completion.pending == 1 and not client.add_episode.called+ del lock_redis.values[_LOCK]++ monkeypatch.setattr(ingest, "INGEST_LOCK_WAIT_SECONDS", 0)+ monkeypatch.setattr(ingest, "_pause_before_retry", forget_finishes)+ await _run_worker(client, monkeypatch, _payload(_completion=completion))++ client.add_episode.assert_awaited_once()+ assert "_lock_waits" not in client.add_episode.await_args.kwargs+ assert await completion.wait(0)+ assert (completion.written, completion.pending) == (1, 0)++ @pytest.mark.asyncio+ async def test_a_graph_locked_for_many_waits_keeps_the_episode(+ self,+ lock_redis: FakeRedis,+ monkeypatch: pytest.MonkeyPatch,+ caplog: pytest.LogCaptureFixture,+ ) -> None:+ """Codex's probe: an episode the graph's lock kept out wait after wait+ is never dropped. It goes back on the queue each time, after a pause+ that doubles up to its cap; every third wait a warning names it; it+ is written once the lock frees."""+ client = _graphiti_client()+ lock_redis.values[_LOCK] = "a stuck forget's token"+ completion = ingest.IngestionCompletion()+ completion.register()+ pauses: list[int] = []++ async def pause(waits: int) -> None:+ pauses.append(waits)+ if waits == 7:+ del lock_redis.values[_LOCK]++ monkeypatch.setattr(ingest, "INGEST_LOCK_WAIT_SECONDS", 0)+ monkeypatch.setattr(ingest, "_pause_before_retry", pause)+ payload = _payload(name="remember-me", _completion=completion)+ await _run_worker(client, monkeypatch, payload)++ assert pauses == [1, 2, 3, 4, 5, 6, 7]+ client.add_episode.assert_awaited_once()+ assert (completion.written, completion.pending) == (1, 0)+ warned = [r.getMessage() for r in caplog.records if r.levelname == "WARNING"]+ assert [m for m in warned if "'remember-me' is still queued" in m] == [+ f"Graph user_test locked by another writer for {waits} waits; the "+ "episode 'remember-me' is still queued, unwritten"+ for waits in (3, 6)+ ]+ assert not [r for r in caplog.records if r.levelname == "ERROR"]++ @pytest.mark.asyncio+ async def test_with_no_room_in_the_queue_the_episode_is_tried_again_in_place(+ self, lock_redis: FakeRedis, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """The queue filled up while the episode waited: it keeps its turn,+ is tried again where it stands, written first once the lock frees,+ and the episode behind it after."""+ client = _graphiti_client()+ lock_redis.values[_LOCK] = "a forget's token"+ queue: asyncio.Queue = asyncio.Queue(maxsize=1)+ queue.put_nowait(_payload(name="first"))+ behind: list[str] = []++ async def pause(waits: int) -> None:+ if not behind:+ behind.append("second")+ queue.put_nowait(_payload(name="second"))+ else:+ lock_redis.values.pop(_LOCK, None)++ monkeypatch.setattr(ingest, "INGEST_LOCK_WAIT_SECONDS", 0)+ monkeypatch.setattr(ingest, "_pause_before_retry", pause)+ monkeypatch.setattr(ingest, "_WORKER_IDLE_TIMEOUT", 0.05)+ with (+ patch.object(ingest, "get_graphiti_client", AsyncMock(return_value=client)),+ patch.object(ingest, "ensure_indices_once", AsyncMock()),+ ):+ worker = ingest._ingestion_worker("test-user", "user_test", queue)+ await asyncio.wait_for(worker, 10)++ written = [call.kwargs["name"] for call in client.add_episode.await_args_list]+ assert written == ["first", "second"]++ def test_the_pause_doubles_up_to_its_cap(self) -> None:+ pauses = [ingest._retry_pause(waits) for waits in (1, 2, 3, 5, 6, 10_000)]++ assert pauses == [1.0, 2.0, 4.0, 16.0, 30.0, 30.0]++ @pytest.mark.asyncio+ async def test_a_write_that_raises_is_counted_failed(+ self, lock_redis: FakeRedis, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ client = _graphiti_client()+ client.add_episode.side_effect = RuntimeError("extraction failed")+ completion = ingest.IngestionCompletion()+ completion.register()++ await _run_worker(client, monkeypatch, _payload(_completion=completion))++ assert await completion.wait(0)+ assert (completion.written, completion.failed, completion.pending) == (0, 1, 0)++ @pytest.mark.asyncio+ async def test_without_redis_the_episode_is_written_anyway(+ self, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture+ ) -> None:+ client = _graphiti_client()+ down = AsyncMock(side_effect=ConnectionError("redis down"))++ with patch.object(scope_lock, "get_redis_async", down):+ await _run_worker(client, monkeypatch)++ client.add_episode.assert_awaited_once()+ assert "write lock unavailable" in caplog.textdiff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.pyindex 3c0fa7026d..5953198eef 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py@@ -214,6 +214,16 @@ class TestIngestionCompletion:c.complete_one()assert await c.wait(0.01) is True+ def test_pending_is_what_was_neither_written_dropped_nor_failed(self) -> None:+ """Apart from the drain: an episode finished by the worker is not+ written unless graphiti saved it."""+ c = ingest.IngestionCompletion()+ for _ in range(4):+ c.register()+ c.written, c.dropped_forgotten, c.failed = 1, 1, 1++ assert c.pending == 1+class TestWaitForIngestion:"""``wait_for_ingestion`` — the drain barrier dream-pass apply uses sodiff --git a/autogpt_platform/backend/backend/copilot/graphiti/memory_model.py b/autogpt_platform/backend/backend/copilot/graphiti/memory_model.pyindex 30eeb9a5ae..58212b0219 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/memory_model.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/memory_model.py@@ -4,11 +4,14 @@ Domain-agnostic envelope that works across business, fiction, research,personal life, and arbitrary knowledge domains. Designed so retrievalcan distinguish user-asserted facts from assistant-derived findingsand filter by scope.++Also holds the fact-status and forget vocabulary shared by the recall+layer (``recall.py``) and the chat tools that report on it."""from enum import Enum-from pydantic import BaseModel, Field+from pydantic import BaseModel, Field, ValidationErrorclass SourceKind(str, Enum):@@ -28,12 +31,157 @@ class MemoryKind(str, Enum):class MemoryStatus(str, Enum):+ # Only ``active`` and ``tentative`` facts are recalled (``recall.py``);+ # a fact in any other state stays on the graph for audit.+ active = "active"+ tentative = "tentative"+ superseded = "superseded"+ contradicted = "contradicted"+ retracted = "retracted"+ """Set by an explicit user or API forget (``recall_forget.retract``). A+ retracted fact is never recalled; the edge is kept for audit."""+++# Every ``MemoryStatus`` but the one only a forget writes. The docstring is+# part of the schema graphiti's extraction model is shown, so the omitted+# value is named only here.+class ExtractedStatus(str, Enum):+ """The lifecycle states graphiti's attribute extraction may give a fact+ (``types.MemoryFact.status``)."""+active = "active"tentative = "tentative"superseded = "superseded"contradicted = "contradicted"+class MemoryForgetFailureCode(str, Enum):+ """Stable, machine-switchable reason a forget delete failed.++ The frontend/model can branch on this code (retry vs. give up) without+ parsing the free-text ``reason``. New codes may be added over time, so+ consumers must tolerate unknown values.+ """++ NO_MATCH = "no_match"+ QUERY_ERROR = "query_error"+ CLEANUP_ERROR = "cleanup_error"+ """The fact was forgotten, but the clean-up after it (redacting or+ deleting what it came from) failed. Recall keeps the text hidden anyway,+ and forgetting it again is safe."""+ BUSY = "busy"+ """Memory was being written for the whole wait (another writer, usually an+ ingestion, held the graph's write lock, ``graphiti/scope_lock.py``):+ nothing was forgotten, and trying again shortly is safe."""+++# Reason given when a forget matched no edge: the UUID is stale, already+# deleted, or not a forgettable edge type.+FORGET_NO_MATCH_REASON = (+ "No matching edge found — it may already be deleted, or the UUID is not a "+ "forgettable edge (RELATES_TO, MENTIONS, HAS_MEMBER)."+)+++# Reason given when a forget found memory busy for its whole wait.+FORGET_BUSY_REASON = (+ "Memory is being updated right now, so nothing was forgotten. "+ "Try again in a moment."+)+++class MemoryForgetFailure(BaseModel):+ """One edge that could not be deleted, with an actionable reason.++ Surfaced so the assistant (and user) can tell *why* a delete failed —+ e.g. the edge was not found vs. the query itself errored — instead of a+ bare "N failed" count that gives the model nothing to act on.+ """++ uuid: str+ code: MemoryForgetFailureCode+ reason: str++ @classmethod+ def no_match(cls, uuid: str) -> "MemoryForgetFailure":+ return cls(+ uuid=uuid,+ code=MemoryForgetFailureCode.NO_MATCH,+ reason=FORGET_NO_MATCH_REASON,+ )++ @classmethod+ def busy(cls, uuid: str) -> "MemoryForgetFailure":+ return cls(+ uuid=uuid,+ code=MemoryForgetFailureCode.BUSY,+ reason=FORGET_BUSY_REASON,+ )++ @classmethod+ def query_error(cls, uuid: str, exc: Exception) -> "MemoryForgetFailure":+ """The reason names the exception type and its first argument (e.g.+ FalkorDB's ``Unknown function 'datetime'``), so the model can tell a+ real query error from a plain no-match; never the full ``repr``,+ which can carry connection details. Log the exception where caught.+ """+ return cls(+ uuid=uuid,+ code=MemoryForgetFailureCode.QUERY_ERROR,+ reason=f"Deletion query failed: {_describe(exc)}",+ )++ @classmethod+ def cleanup_error(cls, uuid: str, exc: Exception) -> "MemoryForgetFailure":+ """The fact is forgotten but its clean-up failed; the reason says so,+ so the model neither reports a plain success nor a lost forget."""+ return cls(+ uuid=uuid,+ code=MemoryForgetFailureCode.CLEANUP_ERROR,+ reason=(+ "Forgotten and no longer recalled, but the clean-up after it "+ f"failed: {_describe(exc)}. Forgetting it again is safe."+ ),+ )+++def _describe(exc: Exception) -> str:+ detail = exc.args[0] if exc.args else type(exc).__name__+ return f"{type(exc).__name__}: {detail}"+++class ForgetResult(BaseModel):+ """What one ``recall_forget.retract`` call did.++ ``deleted`` lists the edges retracted (soft) or removed (hard);+ ``failures`` holds one entry per requested uuid that was not, in the+ shape ``memory_forget_confirm`` reports, plus a ``cleanup_error`` for an+ edge whose clean-up failed, which is in ``deleted`` too when its own write+ landed. The episode and entity lists record the clean-up done: a hard+ forget empties an episode nothing else cites into a tombstone rather than+ deleting it, so the chat session it came from stays known.+ """++ deleted: list[str] = Field(default_factory=list)+ failures: list[MemoryForgetFailure] = Field(default_factory=list)+ redacted_episodes: list[str] = Field(default_factory=list)+ tombstoned_episodes: list[str] = Field(default_factory=list)+ deleted_entities: list[str] = Field(default_factory=list)+++def envelope_provenance(content: str | None) -> str | None:+ """The ``provenance`` an episode body records when it is a+ ``MemoryEnvelope`` (``session:<id>#msg:<n>`` for a stored memory)."""+ try:+ return _EnvelopeProvenance.model_validate_json(content or "").provenance+ except ValidationError:+ return None+++class _EnvelopeProvenance(BaseModel):+ provenance: str | None = None++class RuleMemory(BaseModel):"""Structured representation of a standing instruction or rule.diff --git a/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_legacy_forgets.py b/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_legacy_forgets.pynew file mode 100644index 0000000000..025a88ecfa--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_legacy_forgets.py@@ -0,0 +1,293 @@+"""Restamp the user forgets made before the recall policy.++A forget used to set ``expired_at`` on the fact and nothing else, and left+its episodes as they were. The recall policy still treats that shape as a+forget (``recall.legacy_forget_predicate``); this script makes those records+look like today's forgets, so that clause can be dropped once it has run+everywhere. It hides what a forget hides with the forget's own code+(``recall_hide.py``): the fact's sentence moves to ``fact_redacted``, the+summaries and attributes of the entities it joins or its episodes mention+are cleared, and so are their communities' summaries, and every episode+naming the edge is stamped ``redacted_at``. Then it gives the edge what+``recall_forget.retract`` writes: ``forgotten_at`` (the old forget's+``expired_at``), ``status='retracted'`` and ``expiration_reason='user_signal'``.+The restamp goes last because a restamped edge no longer has the legacy+shape, so a run cut short before it could not find the edge again.++It walks every memory graph on the FalkorDB server, account (``user_*``) and+expert (``expert_*``) graphs alike, and is idempotent. Dry run by default: it+only counts. Pass ``--apply`` to write.++With ``--apply`` each graph is rewritten holding its write lock+(``scope_lock.py``), the lock the ingestion worker and every forget take, from+the count to the last restamp: an ingestion running meanwhile cannot save its+older copy of an edge over the restamp. A graph whose lock another writer+keeps past ``BACKFILL_LOCK_WAIT_SECONDS`` (60 s), or that cannot be locked+because Redis is unreachable, is skipped with nothing written and counted+busy; a graph whose backfill raises is counted failed (every step is+idempotent). If any graph was skipped either way the script exits 1: run it+again to pick those graphs up.++Usage:++ poetry run python -m \\+ backend.copilot.graphiti.migrations.backfill_legacy_forgets [--apply]++Pass ``--graph <name>`` to handle one graph (useful for a canary).++``--scan-markers`` instead reads, and writes nothing: per graph, the uuids of+the facts carrying a forget's marker (``forgotten_at``), never their text, and+among them those whose sentence no forget scrubbed (it still reads otherwise+than ``recall.FORGOTTEN_FACT``). The backfill scrubs before it stamps; a+forget stamps first, and scrubs after, so only a forget whose clean-up failed+leaves its marker on an unscrubbed sentence (forgetting again finishes it).+Every other unscrubbed marker was invented by graphiti's attribute extraction+before it was held to its declared fields (``recall_ingest.py``), and would+hide a fact nobody forgot. Before the recall policy is deployed no forget has+written a marker, so every marker found then was invented. It exits 1 when+any marker is unscrubbed.+"""++import argparse+import asyncio+import logging+import sys+from datetime import datetime, timezone++from pydantic import BaseModel++from backend.copilot.graphiti.config import graphiti_config+from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver+from backend.copilot.graphiti.memory_model import MemoryStatus+from backend.copilot.graphiti.recall import (+ FORGOTTEN_FACT,+ USER_FORGET_REASON,+ legacy_forget_predicate,+)+from backend.copilot.graphiti.recall_hide import REDACT_EPISODES_QUERY, scrub+from backend.copilot.graphiti.scope_lock import LockState, graph_write_lock++logger = logging.getLogger(__name__)++# How long ``--apply`` waits for a graph's write lock, as long as the+# ingestion worker waits, before counting the graph busy.+BACKFILL_LOCK_WAIT_SECONDS = 60++# Memory graphs are named for their scope (``client.derive_memory_group_id``).+MEMORY_GRAPH_PREFIXES = ("user_", "expert_")+++class LegacyForgets(BaseModel):+ """Legacy-forgotten edges and the unredacted episodes naming them, and+ the graphs to run again: ``busy`` (write lock not taken, nothing+ written) and ``failed``."""++ edges: int = 0+ episodes: int = 0+ busy: int = 0+ failed: int = 0+++class _GraphForgets(LegacyForgets):+ uuids: list[str] = []+++class ForgetMarkers(BaseModel):+ """One graph's facts carrying a forget's marker, and those among them+ whose sentence no forget scrubbed (``--scan-markers``)."""++ graph: str+ marked: list[str] = []+ unscrubbed: list[str] = []+++async def backfill_graph(driver: AutoGPTFalkorDriver, *, apply: bool) -> LegacyForgets:+ """Count one graph's legacy forgets and, with ``apply``, restamp them+ holding the graph's write lock from the count to the last restamp;+ ``busy``, with nothing written, when the lock cannot be taken."""+ if not apply:+ return await _legacy_forgets(driver)+ graph, wait = driver.graph_name, BACKFILL_LOCK_WAIT_SECONDS+ async with graph_write_lock(graph, wait_seconds=wait) as lock:+ if lock is not LockState.HELD:+ logger.warning(f"Skipped graph {graph[:20]}: write lock {lock.value}")+ return LegacyForgets(busy=1)+ found = await _legacy_forgets(driver)+ if found.edges:+ await _restamp(driver, found.uuids)+ return found+++async def _legacy_forgets(driver: AutoGPTFalkorDriver) -> _GraphForgets:+ result = await driver.execute_query(COUNT_QUERY)+ records = result[0] if result else []+ return _GraphForgets.model_validate(records[0]) if records else _GraphForgets()+++async def _restamp(driver: AutoGPTFalkorDriver, uuids: list[str]) -> None:+ """Hide what a forget hides, then give the edges a forget's marker."""+ await scrub(driver, uuids)+ await driver.execute_query(+ REDACT_EPISODES_QUERY,+ uuids=uuids,+ now=datetime.now(timezone.utc).isoformat(),+ )+ await driver.execute_query(+ RETRACT_EDGES_QUERY,+ uuids=uuids,+ status=MemoryStatus.retracted.value,+ reason=USER_FORGET_REASON,+ )+++async def backfill_all_graphs(+ *, apply: bool, graph: str | None = None+) -> LegacyForgets:+ """Backfill every memory graph on the server, or just ``graph``.++ A graph that is busy or fails is logged, skipped and counted; a re-run+ picks it up.+ """+ names = [graph] if graph else await _memory_graph_names()+ totals = LegacyForgets()+ for name in names:+ driver = _graph_driver(name)+ try:+ found = await backfill_graph(driver, apply=apply)+ except Exception:+ logger.warning(f"Backfill failed for graph {name[:20]}", exc_info=True)+ totals.failed += 1+ continue+ finally:+ await driver.close()+ if found.edges:+ logger.info(f"{name[:20]}: {found.edges} edges, {found.episodes} episodes")+ totals.edges += found.edges+ totals.episodes += found.episodes+ totals.busy += found.busy+ return totals+++async def scan_markers(graph: str | None = None) -> list[ForgetMarkers]:+ """Every memory graph's facts carrying a forget's marker, or just+ ``graph``'s; reads only."""+ names = [graph] if graph else await _memory_graph_names()+ found: list[ForgetMarkers] = []+ for name in names:+ driver = _graph_driver(name)+ try:+ result = await driver.execute_query(+ MARKERS_QUERY, placeholder=FORGOTTEN_FACT+ )+ finally:+ await driver.close()+ rows = result[0] if result else []+ found.append(+ ForgetMarkers(+ graph=name,+ marked=[row["uuid"] for row in rows],+ unscrubbed=[row["uuid"] for row in rows if not row["scrubbed"]],+ )+ )+ return found+++async def _memory_graph_names() -> list[str]:+ driver = _graph_driver("default_db")+ try:+ names = await driver.client.list_graphs()+ finally:+ await driver.close()+ return sorted(name for name in names if name.startswith(MEMORY_GRAPH_PREFIXES))+++def _graph_driver(database: str) -> AutoGPTFalkorDriver:+ """A driver on a graph known only by its name. ``open_driver`` needs a+ ``MemoryScope``, and an expert graph's name is a digest that no scope can+ be rebuilt from. Opening one creates no graph (see the driver)."""+ return AutoGPTFalkorDriver(+ host=graphiti_config.falkordb_host,+ port=graphiti_config.falkordb_port,+ password=graphiti_config.falkordb_password or None,+ database=database,+ )+++_LEGACY = legacy_forget_predicate("e")++# Reads only, so a dry run never writes (nor creates) a graph.+COUNT_QUERY = f"""+OPTIONAL MATCH ()-[e:RELATES_TO]->()+WHERE {_LEGACY}+WITH collect(e.uuid) AS legacy+OPTIONAL MATCH (ep:Episodic)+WHERE ep.redacted_at IS NULL+ AND any(x IN coalesce(ep.entity_edges, []) WHERE x IN legacy)+RETURN legacy AS uuids, size(legacy) AS edges, count(ep) AS episodes+"""++# The facts carrying a forget's marker, and whether their sentence was+# scrubbed. Reads only.+MARKERS_QUERY = """+MATCH ()-[e:RELATES_TO]->()+WHERE e.forgotten_at IS NOT NULL+RETURN e.uuid AS uuid, coalesce(e.fact = $placeholder, false) AS scrubbed+ORDER BY uuid+"""++# A legacy forget set ``expired_at`` when the user forgot, so that is when+# the restamped edge was forgotten.+RETRACT_EDGES_QUERY = f"""+MATCH ()-[e:RELATES_TO]->()+WHERE e.uuid IN $uuids AND {_LEGACY}+SET e.forgotten_at = coalesce(e.forgotten_at, e.expired_at),+ e.status = $status,+ e.expiration_reason = $reason+"""+++async def main(args: argparse.Namespace) -> int:+ logging.basicConfig(+ level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s"+ )+ if args.scan_markers:+ return _report(await scan_markers(args.graph))+ totals = await backfill_all_graphs(apply=args.apply, graph=args.graph)+ verb = "restamped" if args.apply else "would restamp (dry run)"+ print(f"{verb} {totals.edges} edges and {totals.episodes} episodes")+ if not (totals.busy or totals.failed):+ return 0+ print(f"skipped {totals.busy} busy and {totals.failed} failed graphs: run again")+ return 1+++def _report(found: list[ForgetMarkers]) -> int:+ """Print each graph's marked and unscrubbed facts by uuid; 1 when any+ marker is unscrubbed."""+ for graph in found:+ if graph.marked:+ print(+ f"{graph.graph}: {len(graph.marked)} marked, "+ f"{len(graph.unscrubbed)} unscrubbed {graph.unscrubbed}"+ )+ unscrubbed = sum(len(graph.unscrubbed) for graph in found)+ marked = sum(len(graph.marked) for graph in found)+ print(f"{marked} facts marked forgotten, {unscrubbed} of them unscrubbed")+ return 1 if unscrubbed else 0+++if __name__ == "__main__":+ parser = argparse.ArgumentParser(description=__doc__)+ mode = parser.add_mutually_exclusive_group()+ mode.add_argument(+ "--apply",+ action="store_true",+ help="Write the changes, each graph holding its write lock.",+ )+ mode.add_argument(+ "--scan-markers",+ action="store_true",+ help="Only report the facts carrying a forget's marker, by uuid.",+ )+ parser.add_argument("--graph", help="Backfill one graph instead of all of them.")+ sys.exit(asyncio.run(main(parser.parse_args())))diff --git a/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_legacy_forgets_test.py b/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_legacy_forgets_test.pynew file mode 100644index 0000000000..fb7effc741--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_legacy_forgets_test.py@@ -0,0 +1,276 @@+"""Unit tests for the legacy-forget backfill: the Cypher it sends, in what+order, and under the graph's write lock. It is a one-off migration, so+nothing here runs it on a graph; the live run against an ingestion under way+is in ``recall_inflight_integration_test.py``.+"""++import argparse+from unittest.mock import AsyncMock, patch++import pytest++from backend.copilot.graphiti import scope_lock+from backend.copilot.graphiti.recall import FORGOTTEN_FACT, legacy_forget_predicate+from backend.copilot.graphiti.recall_fake_redis import FakeRedis+from backend.copilot.graphiti.recall_hide import REDACT_EPISODES_QUERY+from backend.copilot.graphiti.scope import write_lock_key++from . import backfill_legacy_forgets as backfill++_LOCK = write_lock_key("user_a")+++def _driver(*results: list[dict]) -> AsyncMock:+ driver = AsyncMock()+ driver.graph_name = "user_a"+ driver.execute_query.side_effect = [(rows, [], None) for rows in results]+ return driver+++class TestBackfillGraph:+ @pytest.mark.asyncio+ async def test_a_dry_run_only_counts(self) -> None:+ driver = _driver([{"edges": 3, "episodes": 2}])++ found = await backfill.backfill_graph(driver, apply=False)++ assert (found.edges, found.episodes) == (3, 2)+ assert driver.execute_query.await_count == 1+ assert "SET" not in driver.execute_query.await_args.args[0]++ @pytest.mark.asyncio+ async def test_apply_hides_like_a_forget_before_restamping_edges(self) -> None:+ """The forget's own scrub and redaction run on the edges the count+ found; the restamp goes last, since a restamped edge no longer has+ the legacy shape and a re-run could not find it again."""+ driver = _driver([{"uuids": ["e1", "e2"], "edges": 2, "episodes": 1}], [], [])+ scrub = AsyncMock()++ with patch.object(backfill, "scrub", scrub):+ await backfill.backfill_graph(driver, apply=True)++ scrub.assert_awaited_once_with(driver, ["e1", "e2"])+ count, redact, restamp = driver.execute_query.await_args_list+ assert count.args[0] == backfill.COUNT_QUERY+ assert redact.args[0] == REDACT_EPISODES_QUERY+ assert redact.kwargs["uuids"] == ["e1", "e2"]+ assert set(redact.kwargs) == {"uuids", "now"}+ assert restamp.args[0] == backfill.RETRACT_EDGES_QUERY+ assert restamp.kwargs == {+ "uuids": ["e1", "e2"],+ "status": "retracted",+ "reason": "user_signal",+ }++ def test_the_restamp_gives_a_legacy_forget_the_forget_marker(self) -> None:+ """``forgotten_at`` is the old forget's ``expired_at``, and a marker a+ forget already wrote is kept."""+ query = backfill.RETRACT_EDGES_QUERY+ assert "e.forgotten_at = coalesce(e.forgotten_at, e.expired_at)" in query+ assert "e.status = $status" in query+ assert "e.expiration_reason = $reason" in query++ @pytest.mark.asyncio+ async def test_apply_writes_nothing_where_there_is_nothing_to_restamp(+ self,+ ) -> None:+ driver = _driver([{"edges": 0, "episodes": 0}])++ await backfill.backfill_graph(driver, apply=True)++ assert driver.execute_query.await_count == 1++ def test_the_count_finds_edges_by_the_policys_legacy_clause(self) -> None:+ """The writes touch only the edges it found, and the restamp only+ those still in the legacy shape."""+ assert f"WHERE {legacy_forget_predicate('e')}" in backfill.COUNT_QUERY+ assert "legacy AS uuids" in backfill.COUNT_QUERY+ assert "ep.redacted_at IS NULL" in backfill.COUNT_QUERY+ assert "SET" not in backfill.COUNT_QUERY+ assert (+ f"WHERE e.uuid IN $uuids AND {legacy_forget_predicate('e')}"+ in backfill.RETRACT_EDGES_QUERY+ )+++class TestBackfillAllGraphs:+ @pytest.mark.asyncio+ async def test_walks_every_memory_graph_and_skips_a_failing_one(self) -> None:+ lister = AsyncMock()+ lister.client.list_graphs = AsyncMock(+ return_value=["user_a", "expert_b", "user_broken", "other", "default_db"]+ )+ broken = AsyncMock()+ broken.execute_query.side_effect = RuntimeError("down")+ drivers = {+ "default_db": lister,+ "user_a": _driver([{"edges": 1, "episodes": 2}]),+ "expert_b": _driver([{"edges": 2, "episodes": 0}]),+ "user_broken": broken,+ }++ with patch.object(backfill, "_graph_driver", side_effect=drivers.__getitem__):+ totals = await backfill.backfill_all_graphs(apply=False)++ assert (totals.edges, totals.episodes, totals.failed) == (3, 2, 1)+ for driver in drivers.values():+ driver.close.assert_awaited_once()++ @pytest.mark.asyncio+ async def test_one_named_graph_is_all_it_touches(self) -> None:+ driver = _driver([{"edges": 0, "episodes": 0}])+ opened: list[str] = []++ def open_graph(name: str) -> AsyncMock:+ opened.append(name)+ return driver++ with patch.object(backfill, "_graph_driver", side_effect=open_graph):+ await backfill.backfill_all_graphs(apply=True, graph="expert_x")++ assert opened == ["expert_x"]+++class TestTheWriteLock:+ """Reproduced first by an independent validation+ (``r6-migration-concurrency.py``): without the lock, an ingestion paused+ at graphiti's save wrote its older copy over a finished backfill."""++ @pytest.mark.asyncio+ async def test_apply_holds_the_graphs_write_lock_from_count_to_restamp(+ self, lock_redis: FakeRedis+ ) -> None:+ held: list[bool] = []+ driver = _driver()+ answers = iter([[{"uuids": ["e1"], "edges": 1, "episodes": 1}], [], []])++ async def query(*args: object, **kwargs: object):+ held.append(_LOCK in lock_redis.values)+ return next(answers), [], None++ driver.execute_query.side_effect = query+ with patch.object(backfill, "scrub", AsyncMock()):+ found = await backfill.backfill_graph(driver, apply=True)++ assert (found.edges, found.busy) == (1, 0)+ assert held == [True, True, True], "count, redaction and restamp"+ assert _LOCK not in lock_redis.values, "released afterwards"++ @pytest.mark.asyncio+ async def test_a_graph_another_writer_keeps_locked_is_skipped_as_busy(+ self, lock_redis: FakeRedis, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ lock_redis.values[_LOCK] = "an ingestion's token"+ monkeypatch.setattr(backfill, "BACKFILL_LOCK_WAIT_SECONDS", 0)+ driver = _driver()++ found = await backfill.backfill_graph(driver, apply=True)++ assert (found.edges, found.busy) == (0, 1)+ driver.execute_query.assert_not_awaited()+ assert lock_redis.values[_LOCK] == "an ingestion's token"++ @pytest.mark.asyncio+ async def test_without_redis_a_graph_is_skipped_not_written_unlocked(+ self,+ ) -> None:+ driver = _driver()+ down = AsyncMock(side_effect=ConnectionError("redis down"))++ with patch.object(scope_lock, "get_redis_async", down):+ found = await backfill.backfill_graph(driver, apply=True)++ assert found.busy == 1+ driver.execute_query.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_a_dry_run_only_reads_so_takes_no_lock(+ self, lock_redis: FakeRedis+ ) -> None:+ lock_redis.values[_LOCK] = "an ingestion's token"++ found = await backfill.backfill_graph(+ _driver([{"edges": 2, "episodes": 1}]), apply=False+ )++ assert (found.edges, found.busy) == (2, 0)+++class TestMain:+ @pytest.mark.parametrize(+ "totals, code",+ [+ (backfill.LegacyForgets(edges=2), 0),+ (backfill.LegacyForgets(edges=2, busy=1), 1),+ (backfill.LegacyForgets(failed=1), 1),+ ],+ ids=["all done", "a graph busy", "a graph failed"],+ )+ @pytest.mark.asyncio+ async def test_exits_non_zero_while_any_graph_is_left_to_run_again(+ self,+ totals: backfill.LegacyForgets,+ code: int,+ capsys: pytest.CaptureFixture[str],+ ) -> None:+ args = argparse.Namespace(apply=True, graph=None, scan_markers=False)+ with patch.object(+ backfill, "backfill_all_graphs", AsyncMock(return_value=totals)+ ):+ assert await backfill.main(args) == code++ assert ("run again" in capsys.readouterr().out) is bool(code)+++class TestScanMarkers:+ """``--scan-markers``: the facts carrying a forget's marker, by uuid, and+ those a forget never scrubbed, which before the recall policy deploys+ can only have been invented by the extraction."""++ @pytest.mark.asyncio+ async def test_reports_every_marked_fact_and_the_unscrubbed_apart(self) -> None:+ driver = _driver(+ [{"uuid": "a", "scrubbed": True}, {"uuid": "b", "scrubbed": False}]+ )+ with (+ patch.object(backfill, "_graph_driver", return_value=driver),+ patch.object(+ backfill, "_memory_graph_names", AsyncMock(return_value=["user_a"])+ ),+ ):+ found = await backfill.scan_markers()++ assert found == [+ backfill.ForgetMarkers(graph="user_a", marked=["a", "b"], unscrubbed=["b"])+ ]+ query = driver.execute_query.await_args+ assert query.args == (backfill.MARKERS_QUERY,)+ assert query.kwargs == {"placeholder": FORGOTTEN_FACT}+ assert "SET" not in backfill.MARKERS_QUERY.upper(), "it only reads"+ driver.close.assert_awaited_once()++ @pytest.mark.parametrize(+ "unscrubbed, code", [([], 0), (["b"], 1)], ids=["scrubbed", "invented"]+ )+ @pytest.mark.asyncio+ async def test_exits_non_zero_on_an_unscrubbed_marker_naming_only_uuids(+ self, unscrubbed: list[str], code: int, capsys: pytest.CaptureFixture[str]+ ) -> None:+ found = [+ backfill.ForgetMarkers(+ graph="user_a", marked=["a", *unscrubbed], unscrubbed=unscrubbed+ )+ ]+ args = argparse.Namespace(apply=False, graph=None, scan_markers=True)+ backfill_all = AsyncMock()+ with (+ patch.object(backfill, "scan_markers", AsyncMock(return_value=found)),+ patch.object(backfill, "backfill_all_graphs", backfill_all),+ ):+ assert await backfill.main(args) == code++ backfill_all.assert_not_awaited()+ out = capsys.readouterr().out+ assert (+ f"user_a: {1 + len(unscrubbed)} marked, {len(unscrubbed)} unscrubbed" in out+ )diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall.py b/autogpt_platform/backend/backend/copilot/graphiti/recall.pynew file mode 100644index 0000000000..b7905ff5ca--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall.py@@ -0,0 +1,318 @@+"""The recall policy: what memory may be read back, for the assistant, the+dream and ingestion.++Warm context, ``memory_search``, ``memory_forget_search``, the settings fact+list, the dream gather and ingestion's extraction context read through this+module's functions or predicates, and what the assistant is shown is read+again by uuid right before it is rendered (``live_now``,+``recall_recheck.py``). Every forget goes through ``retract``+(``recall_forget.py``); ``graphiti/AGENTS.md`` lists what a forget reaches+and what it does not. ``recall_render.py`` writes out what recall returns.++A fact (a ``RELATES_TO`` edge) is live while ``expired_at`` and+``forgotten_at`` are unset and its ``status`` is ``active`` or ``tentative``;+an edge with no ``status`` predates the ``MemoryFact`` edge type and counts+as ``active``. A forgotten fact (``forgotten_fact_predicate``) is never live:+a forget stamps ``forgotten_at``, which only a forget writes+(``recall_forget.py``, or the legacy-forget backfill for older ones), so no+other writer's status or reason can make it look remembered again, nor make+a fact look forgotten. An episode is recallable while no forget has stamped+``redacted_at`` on it and none of the facts extracted from it is forgotten:+one forgotten fact hides the whole text, since nothing records which+sentence it came from. The episode's other facts stay live.+"""++import asyncio+from datetime import datetime, timezone+from typing import Any++from graphiti_core.driver.driver import GraphDriver+from graphiti_core.edges import EntityEdge, get_entity_edge_from_record+from graphiti_core.graph_queries import get_vector_cosine_func_query+from graphiti_core.models.edges.edge_db_queries import get_entity_edge_return_query+from graphiti_core.nodes import EpisodicNode, get_episodic_node_from_record+from graphiti_core.search.search_config import SearchConfig+from graphiti_core.search.search_config_recipes import EDGE_HYBRID_SEARCH_RRF+from graphiti_core.search.search_filters import (+ ComparisonOperator,+ DateFilter,+ SearchFilters,+)+from graphiti_core.search.search_utils import DEFAULT_MIN_SCORE++from backend.copilot.dream.ratification_hits import record_memory_hit++from .client import get_graphiti_client+from .falkordb_driver import open_driver+from .memory_model import MemoryStatus+from .scope import MemoryScope++# The ``expiration_reason`` a user's forget records (``recall_forget.retract``).+USER_FORGET_REASON = "user_signal"+# What a forgotten fact's ``fact`` and ``name`` read once a forget has moved+# them to ``fact_redacted`` / ``name_redacted`` (kept for audit), so graphiti's+# own prompts never see the sentence (``recall_hide.py``).+FORGOTTEN_FACT = "[forgotten]"++_LIVE_STATUSES = (MemoryStatus.active, MemoryStatus.tentative)++# The ``expired_at`` half of the live test, applied inside every graphiti+# search method so a retired fact never takes one of the ``limit`` slots. The+# ``status`` half runs in Python (``is_live``): graphiti 0.30 builds no Cypher+# from ``SearchFilters.property_filters``.+_LIVE_SEARCH_FILTER = SearchFilters(+ expired_at=[[DateFilter(comparison_operator=ComparisonOperator.is_null)]]+)+++def live_fact_predicate(alias: str = "e", *, include_tentative: bool = True) -> str:+ """The live-fact test as a Cypher ``WHERE`` fragment on edge ``alias``.++ For code that lists or counts facts with its own Cypher, so its idea of a+ live fact stays the one recall uses.+ """+ statuses = ", ".join(f"'{s.value}'" for s in _statuses(include_tentative))+ return (+ f"{alias}.expired_at IS NULL AND {alias}.forgotten_at IS NULL"+ f" AND ({alias}.status IS NULL OR {alias}.status IN [{statuses}])"+ )+++def is_live(fact: EntityEdge, *, include_tentative: bool = True) -> bool:+ """The live-fact test for an edge graphiti returned."""+ status = fact_status(fact)+ allowed = {s.value for s in _statuses(include_tentative)}+ unmarked = fact.expired_at is None and fact.attributes.get("forgotten_at") is None+ return unmarked and (status is None or status in allowed)+++def fact_status(fact: EntityEdge) -> str | None:+ """The edge's ``MemoryFact.status``; ``None`` on edges that predate it."""+ status = fact.attributes.get("status")+ return None if status is None else str(status)+++def forgotten_fact_predicate(alias: str = "e") -> str:+ """The forgotten-fact test as a Cypher ``WHERE`` fragment on edge ``alias``.++ A user forgot the fact: a forget stamped ``forgotten_at`` (the marker+ only a forget writes), or, from before that marker, it has the+ ``legacy_forget_predicate`` shape. Never its ``status`` or+ ``expiration_reason``: graphiti's attribute extraction fills both from an+ episode's text, and a dream demotion picks its reason, so neither may+ make a fact (and every episode and session it came from) look forgotten.+ """+ return f"({alias}.forgotten_at IS NOT NULL OR ({legacy_forget_predicate(alias)}))"+++def legacy_forget_predicate(alias: str = "e") -> str:+ """What a forget left before this policy: ``expired_at`` and nothing else.++ graphiti sets ``invalid_at`` whenever it expires an edge and a dream+ demotion always records a reason, so no other writer leaves this shape.+ ``migrations/backfill_legacy_forgets.py`` restamps these edges as+ retractions, after which this clause can go.+ """+ return (+ f"{alias}.expired_at IS NOT NULL AND {alias}.invalid_at IS NULL"+ f" AND {alias}.expiration_reason IS NULL"+ )+++def is_forgotten(fact: EntityEdge) -> bool:+ """The forgotten-fact test for an edge graphiti returned."""+ reason = fact.attributes.get("expiration_reason")+ legacy = fact.expired_at is not None and fact.invalid_at is None+ return fact.attributes.get("forgotten_at") is not None or (+ legacy and reason is None+ )+++def forgotten_facts_clause(var: str = "forgotten") -> str:+ """Cypher that opens a query by binding ``var`` to every forgotten fact's+ uuid, for ``recallable_episode_predicate`` to test episodes against.++ One pass over the facts, not one per episode.+ """+ return (+ "OPTIONAL MATCH ()-[forgotten_fact:RELATES_TO]->()\n"+ f"WHERE {forgotten_fact_predicate('forgotten_fact')}\n"+ f"WITH collect(forgotten_fact.uuid) AS {var}\n"+ )+++def recallable_episode_predicate(alias: str = "e", forgotten: str = "forgotten") -> str:+ """The recallable-episode test as a Cypher ``WHERE`` fragment on episode+ ``alias``; the query must open with ``forgotten_facts_clause(forgotten)``."""+ return (+ f"{alias}.redacted_at IS NULL"+ f" AND none(x IN coalesce({alias}.entity_edges, []) WHERE x IN {forgotten})"+ )+++def is_recallable_episode(+ entity_edges: list[str], forgotten: set[str], *, redacted: bool+) -> bool:+ """The recallable-episode test in Python: not ``redacted`` and citing no+ uuid in ``forgotten`` among its ``entity_edges``."""+ return not redacted and forgotten.isdisjoint(entity_edges)+++async def search_facts(+ scope: MemoryScope,+ query: str,+ *,+ limit: int,+ recipe: SearchConfig | None = None,+ include_tentative: bool = True,+) -> list[EntityEdge]:+ """Live facts matching ``query``, best first, at most ``limit``.++ ``recipe`` defaults to the hybrid RRF config ``Graphiti.search`` uses. Its+ ``limit`` is replaced on a copy: ``Graphiti.search`` sets it on the shared+ recipe object, which graphiti's own ingestion dedup also reads. The search+ can take seconds (a cross-encoder rerank), so its last step reads the+ facts again by uuid (``live_now``): a forget that answered meanwhile is+ not returned.+ """+ client = await get_graphiti_client(scope.group_id)+ config = (recipe or EDGE_HYBRID_SEARCH_RRF).model_copy(update={"limit": limit})+ results = await client.search_(+ query=query,+ config=config,+ group_ids=[scope.group_id],+ search_filter=_LIVE_SEARCH_FILTER,+ )+ tentative = include_tentative+ found = [f for f in results.edges if is_live(f, include_tentative=tentative)]+ return await live_now(client.driver, found, include_tentative=tentative)+++async def search_forgotten_facts(+ scope: MemoryScope, query: str, *, limit: int+) -> list[EntityEdge]:+ """Forgotten facts matching ``query`` by meaning, best first, at most+ ``limit``: for making a soft forget permanent+ (``recall_forget.retract(hard=True)``).++ A forget leaves a fact's embedding but makes its sentence read+ ``FORGOTTEN_FACT``, so in graphiti's hybrid search, which ranks every+ fact by both, live facts would take the slots ahead of it. This ranks+ the forgotten facts alone, by the similarity graphiti's own search uses,+ above the same threshold: none is crowded out by a live one.+ """+ client = await get_graphiti_client(scope.group_id)+ vector = await client.embedder.create(input_data=[query.replace("\n", " ")])+ return await _forgotten_by_meaning(client.driver, scope.group_id, vector, limit)+++async def _forgotten_by_meaning(+ driver: GraphDriver, group_id: str, vector: list[float], limit: int+) -> list[EntityEdge]:+ provider = driver.provider+ score = get_vector_cosine_func_query("e.fact_embedding", "$search_vector", provider)+ records, _, _ = await driver.execute_query(+ "MATCH (n:Entity)-[e:RELATES_TO]->(m:Entity)"+ f" WHERE e.group_id = $group_id AND {forgotten_fact_predicate('e')}"+ f" WITH DISTINCT e, n, m, {score} AS score WHERE score > $min_score"+ f" RETURN {get_entity_edge_return_query(provider)}"+ " ORDER BY score DESC LIMIT $limit",+ group_id=group_id,+ search_vector=vector,+ min_score=DEFAULT_MIN_SCORE,+ limit=limit,+ )+ return [get_entity_edge_from_record(row, provider) for row in records]+++async def live_now(+ driver: GraphDriver, facts: list[EntityEdge], *, include_tentative: bool = True+) -> list[EntityEdge]:+ """``facts`` still live, in order, read again by uuid in one query. As+ the last graph read before they are shown, it bounds the stale window to+ the time between this read and the response."""+ if not facts:+ return facts+ live = live_fact_predicate("e", include_tentative=include_tentative)+ result = await driver.execute_query(+ f"MATCH ()-[e:RELATES_TO]->() WHERE e.uuid IN $uuids AND {live}"+ " RETURN e.uuid AS uuid",+ uuids=[fact.uuid for fact in facts],+ )+ kept = {row["uuid"] for row in (result[0] if result else [])}+ return [fact for fact in facts if fact.uuid in kept]+++async def recent_episodes(scope: MemoryScope, n: int) -> list[EpisodicNode]:+ """The ``n`` newest recallable episodes, oldest first.++ graphiti's ``retrieve_episodes`` plus the recallable-episode test it has+ no way to express.+ """+ driver = open_driver(scope)+ try:+ records = await recallable_episodes(+ driver, scope.group_id, datetime.now(timezone.utc), n+ )+ finally:+ await driver.close()+ return [get_episodic_node_from_record(record) for record in reversed(records)]+++async def recallable_episodes(+ driver: GraphDriver,+ group_id: str,+ reference_time: datetime,+ limit: int,+ source: str | None = None,+) -> list[dict[str, Any]]:+ """The ``limit`` newest recallable episodes up to ``reference_time``+ (of ``source`` when given), newest first, as records."""+ result = await driver.execute_query(+ _RECALLABLE_EPISODES_QUERY,+ group_id=group_id,+ reference_time=reference_time,+ source=source,+ limit=limit,+ )+ return result[0] if result else []+++async def record_hit(scope: MemoryScope, edge_uuids: list[str]) -> None:+ """Count one recall hit on each edge, for the ratification sweep.++ Best-effort like ``record_memory_hit``, which never raises: a lost hit+ only delays the promotion of a tentative fact.+ """+ await asyncio.gather(+ *(+ record_memory_hit(scope, edge_uuid)+ for edge_uuid in dict.fromkeys(edge_uuids)+ )+ )+++def _statuses(include_tentative: bool) -> tuple[MemoryStatus, ...]:+ return _LIVE_STATUSES if include_tentative else (MemoryStatus.active,)+++# graphiti's ``retrieve_episodes`` query (group, ``reference_time`` cut-off,+# optional source, newest first) with the recallable-episode test added.+# ``valid_at`` is stored as an ISO string, so the comparison is lexical, as+# in graphiti.+_RECALLABLE_EPISODES_QUERY = (+ forgotten_facts_clause()+ + f"""+MATCH (e:Episodic)+WHERE e.group_id = $group_id+ AND e.valid_at <= $reference_time+ AND ($source IS NULL OR e.source = $source)+ AND {recallable_episode_predicate("e")}+RETURN e.uuid AS uuid, e.name AS name, e.group_id AS group_id,+ e.created_at AS created_at, e.source AS source,+ e.source_description AS source_description, e.content AS content,+ e.valid_at AS valid_at, e.entity_edges AS entity_edges+ORDER BY e.valid_at DESC+LIMIT $limit+"""+)diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_citations.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_citations.pynew file mode 100644index 0000000000..86bbb41225--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_citations.py@@ -0,0 +1,111 @@+"""A dream's writes, checked against forgets right before they are written.++A dream pass reads the graph, asks its model for consolidated facts and+proposals, and queues them as episodes minutes or hours later+(``dream/apply.py``). A forget can answer in between, and graphiti, whose+model-decided dedup never merges a statement into a forgotten edge+(``recall_ingest.py``; its exact-text match only lists an episode on one+when the statement itself reads ``[forgotten]``), would then save such a+write as a new live fact: the forget undone without a word from the user.+So each dream write carries what it rests on (``Citations``) and the+ingestion worker checks it under the graph's write lock, right before+``add_episode`` (``ingest._write_locked``). A forget that answered before+the check drops the write; one that comes later waits for the lock and then+reaches the facts it names, as after any write.++A write rests on a forget when a fact it cites is forgotten or gone (a hard+forget deletes it), or an episode it cites is no longer recallable or gone.+A write that cites nothing is taken to cite everything its pass read+(``apply._citations``), and its statement is also compared with the+sentence every forgotten fact keeps for audit, lower-cased and with its+whitespace collapsed, as graphiti's own exact-match dedup compares. It has+no endpoints until graphiti extracts it, so that comparison spans the+graph: it drops more than a same-endpoints one would, never less.+"""++from graphiti_core.driver.driver import GraphDriver+from pydantic import BaseModel, Field++from .recall import (+ forgotten_fact_predicate,+ forgotten_facts_clause,+ recallable_episode_predicate,+)+++class Citations(BaseModel):+ """What a dream write rests on, for ``rests_on_a_forget``."""++ fact_uuids: list[str] = Field(default_factory=list)+ episode_uuids: list[str] = Field(default_factory=list)+ # Set on a write that cites nothing itself: compared with the sentences+ # forgotten facts keep for audit.+ statement: str | None = None+++async def rests_on_a_forget(+ driver: GraphDriver, citations: Citations | None+) -> str | None:+ """Why the write must not be made, or None when nothing it rests on has+ been forgotten (or it is not a dream write: no ``citations``)."""+ if citations is None:+ return None+ reason = await _cites_a_forget(driver, citations)+ if reason is None and citations.statement is not None:+ reason = await _restates_a_forget(driver, citations.statement)+ return reason+++async def _cites_a_forget(driver: GraphDriver, citations: Citations) -> str | None:+ if not citations.fact_uuids and not citations.episode_uuids:+ return None+ result = await driver.execute_query(+ _CITED_QUERY,+ fact_uuids=citations.fact_uuids,+ episode_uuids=citations.episode_uuids,+ )+ rows = result[0] if result else []+ # No row reads as nothing found: every citation counts as forgotten.+ row = rows[0] if rows else {"facts": [], "episodes": []}+ if set(citations.fact_uuids) - set(row["facts"]):+ return "cites a fact that is forgotten or gone"+ if set(citations.episode_uuids) - set(row["episodes"]):+ return "cites an episode that is hidden or gone"+ return None+++async def _restates_a_forget(driver: GraphDriver, statement: str) -> str | None:+ result = await driver.execute_query(+ _RESTATED_QUERY, statement=statement, space=r"\s+"+ )+ return "restates a forgotten fact" if result and result[0] else None+++def _normalized(text: str) -> str:+ """``text`` lower-cased, whitespace collapsed and trimmed, in Cypher."""+ return f"trim(string.replaceRegEx(toLower({text}), $space, ' '))"+++# Every cited fact still there and not forgotten, and every cited episode+# still recallable; one row.+_CITED_QUERY = (+ forgotten_facts_clause()+ + f"""+OPTIONAL MATCH ()-[fact:RELATES_TO]->()+WHERE fact.uuid IN $fact_uuids AND NOT (fact.uuid IN forgotten)+WITH forgotten, collect(DISTINCT fact.uuid) AS facts+OPTIONAL MATCH (episode:Episodic)+WHERE episode.uuid IN $episode_uuids+ AND {recallable_episode_predicate("episode")}+RETURN facts, collect(DISTINCT episode.uuid) AS episodes+"""+)++# A forgotten fact whose kept sentence reads as ``$statement``.+_RESTATED_QUERY = f"""+MATCH ()-[e:RELATES_TO]->()+WHERE {forgotten_fact_predicate("e")}+ AND {_normalized("coalesce(e.fact_redacted, e.fact)")} = {_normalized("$statement")}+RETURN e.uuid AS uuid+LIMIT 1+"""diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_citations_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_citations_test.pynew file mode 100644index 0000000000..fea581d720--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_citations_test.py@@ -0,0 +1,193 @@+"""Unit tests for ``recall_citations``: a dream write is checked against+forgets, by what it cites or, citing nothing, by its statement, and the+ingestion worker drops one that rests on a forget.++The live runs, through the real dream write helpers and the production+worker, are in ``recall_dream_citations_integration_test.py``.+"""++import asyncio+from datetime import datetime, timezone+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from graphiti_core.nodes import EpisodeType++from . import ingest, recall+from .recall_citations import Citations, rests_on_a_forget+from .recall_fake_redis import FakeRedis+from .scope import write_lock_key++_NOW = datetime(2026, 9, 26, 12, 0, tzinfo=timezone.utc)+_STATEMENT = "Alice works on Atlas"+++def _driver(*answers: list[dict]) -> MagicMock:+ """A driver answering each query in turn with ``answers``' rows."""+ driver = MagicMock()+ driver.execute_query = AsyncMock(side_effect=[(rows, [], None) for rows in answers])+ return driver+++def _still(facts: list[str], episodes: list[str]) -> list[dict]:+ return [{"facts": facts, "episodes": episodes}]+++class TestRestsOnAForget:+ @pytest.mark.asyncio+ async def test_a_write_that_carries_no_citations_is_not_checked(self) -> None:+ driver = _driver()++ assert await rests_on_a_forget(driver, None) is None+ driver.execute_query.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_everything_it_cites_still_there_lets_it_through(self) -> None:+ driver = _driver(_still(["f1", "f2"], ["ep1"]))+ cited = Citations(fact_uuids=["f1", "f2"], episode_uuids=["ep1"])++ assert await rests_on_a_forget(driver, cited) is None+ [call] = driver.execute_query.await_args_list+ query = call.args[0]+ assert query.startswith(recall.forgotten_facts_clause())+ assert "NOT (fact.uuid IN forgotten)" in query+ assert recall.recallable_episode_predicate("episode") in query+ assert call.kwargs == {"fact_uuids": ["f1", "f2"], "episode_uuids": ["ep1"]}++ @pytest.mark.parametrize(+ "still, reason",+ [+ (_still(["f1"], ["ep1"]), "cites a fact that is forgotten"),+ (_still(["f1", "f2"], []), "cites an episode that is hidden"),+ ([], "cites a fact that is forgotten"),+ ],+ ids=["a fact forgotten or deleted", "an episode hidden", "no answer"],+ )+ @pytest.mark.asyncio+ async def test_anything_it_cites_forgotten_or_gone_drops_it(+ self, still: list[dict], reason: str+ ) -> None:+ cited = Citations(fact_uuids=["f1", "f2"], episode_uuids=["ep1"])++ found = await rests_on_a_forget(_driver(still), cited)++ assert found is not None and found.startswith(reason)++ @pytest.mark.asyncio+ async def test_citing_nothing_its_statement_is_compared_with_forgotten_ones(+ self,+ ) -> None:+ driver = _driver([{"uuid": "forgotten-edge"}])++ found = await rests_on_a_forget(driver, Citations(statement=_STATEMENT))++ assert found == "restates a forgotten fact"+ [call] = driver.execute_query.await_args_list+ assert recall.forgotten_fact_predicate("e") in call.args[0]+ assert "coalesce(e.fact_redacted, e.fact)" in call.args[0]+ assert "LIMIT 1" in call.args[0]+ assert call.kwargs == {"statement": _STATEMENT, "space": r"\s+"}++ @pytest.mark.asyncio+ async def test_citing_nothing_it_also_rests_on_everything_the_pass_read(+ self,+ ) -> None:+ uncited = Citations(fact_uuids=["f1"], statement=_STATEMENT)++ forgotten_read = _driver(_still([], []))+ assert await rests_on_a_forget(forgotten_read, uncited) is not None+ assert forgotten_read.execute_query.await_count == 1++ all_live = _driver(_still(["f1"], []), [])+ assert await rests_on_a_forget(all_live, uncited) is None+ assert all_live.execute_query.await_count == 2+++def _payload(citations: Citations, completion: ingest.IngestionCompletion) -> dict:+ return {+ "name": "dream_p1_consolidate_000",+ "episode_body": '{"content": "Alice works on Atlas"}',+ "source": EpisodeType.json,+ "source_description": "dream-pass consolidation",+ "reference_time": _NOW,+ "group_id": "user_test",+ "_citations": citations,+ "_completion": completion,+ }+++def _client(still: list[dict]) -> MagicMock:+ client = MagicMock()+ client.add_episode = AsyncMock()+ client.driver.execute_query = AsyncMock(return_value=(still, [], None))+ return client+++async def _work(client: MagicMock, monkeypatch: pytest.MonkeyPatch, payload: dict):+ queue: asyncio.Queue = asyncio.Queue(maxsize=10)+ queue.put_nowait(payload)+ monkeypatch.setattr(ingest, "_WORKER_IDLE_TIMEOUT", 0.05)+ with (+ patch.object(ingest, "get_graphiti_client", AsyncMock(return_value=client)),+ patch.object(ingest, "ensure_indices_once", AsyncMock()),+ patch.object(ingest, "previous_episode_uuids", AsyncMock(return_value=[])),+ ):+ await ingest._ingestion_worker("test-user", "user_test", queue)+++class TestTheWorker:+ @pytest.mark.asyncio+ async def test_drops_a_dream_write_resting_on_a_forget_and_counts_it(+ self, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture+ ) -> None:+ client = _client(_still([], []))+ completion = ingest.IngestionCompletion()+ completion.register()++ with caplog.at_level("INFO"):+ payload = _payload(Citations(fact_uuids=["f1"]), completion)+ await _work(client, monkeypatch, payload)++ client.add_episode.assert_not_called()+ assert completion.dropped_forgotten == 1+ assert await completion.wait(0), "a dropped write is no longer pending"+ assert "Dropped dream write 'dream_p1_consolidate_000'" in caplog.text++ @pytest.mark.asyncio+ async def test_writes_one_whose_citations_are_all_live(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ client = _client(_still(["f1"], []))+ completion = ingest.IngestionCompletion()+ completion.register()++ payload = _payload(Citations(fact_uuids=["f1"]), completion)+ await _work(client, monkeypatch, payload)++ client.add_episode.assert_awaited_once()+ assert "_citations" not in client.add_episode.await_args.kwargs+ assert completion.dropped_forgotten == 0++ @pytest.mark.asyncio+ async def test_a_write_that_found_the_graph_locked_is_checked_on_its_retry(+ self, lock_redis: FakeRedis, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """Its citations go back on the queue with it: the forget that held+ the lock reached them, so the second attempt drops it."""+ lock = write_lock_key("user_test")+ lock_redis.values[lock] = "the forget's token"+ client = _client(_still([], []))+ completion = ingest.IngestionCompletion()+ completion.register()++ async def forget_finishes(waits: int) -> None:+ client.driver.execute_query.assert_not_awaited()+ del lock_redis.values[lock]++ monkeypatch.setattr(ingest, "INGEST_LOCK_WAIT_SECONDS", 0)+ monkeypatch.setattr(ingest, "_pause_before_retry", forget_finishes)+ payload = _payload(Citations(fact_uuids=["f1"]), completion)+ await _work(client, monkeypatch, payload)++ client.add_episode.assert_not_called()+ assert completion.dropped_forgotten == 1diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_dream_citations_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_dream_citations_integration_test.pynew file mode 100644index 0000000000..9606d73f66--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_dream_citations_integration_test.py@@ -0,0 +1,246 @@+"""A dream's writes after a forget that answered once the dream had read the+graph, through ``dream/apply.py`` and the production ingestion worker on a+live FalkorDB.++The dream reads Alice's fact and its episode, the user forgets the fact+(soft or hard), then the dream writes a consolidation or a proposal resting+on what it read: the worker drops it under the graph's write lock+(``recall_citations.py``), no edge is added, the fact stays out of recall+and the pass reports the drop. A write resting only on live memory still+lands. Reproduced first by an independent validation+(``r5-quality-delayed-operations.py``, the four stale-dream cases).++Run with FalkorDB reachable (see ``conftest.py``)::++ poetry run pytest -m integration backend/copilot/graphiti/recall_dream_citations_integration_test.py+"""++from collections.abc import AsyncIterator+from types import SimpleNamespace+from unittest.mock import AsyncMock, patch++import pytest+import pytest_asyncio+from pytest_mock import MockerFixture++from backend.copilot.dream import apply, fetch+from backend.copilot.dream.fetch import DreamInput+from backend.copilot.dream.schemas import (+ ConsolidatedFact,+ DreamOperations,+ ProposedFinding,+)++from . import ingest, recall+from .falkordb_driver import AutoGPTFalkorDriver+from .recall_forget import retract+from .recall_integration_fixtures import (+ ALICE,+ BOB,+ BuildClient,+ Fact,+ ingest_facts,+ live_facts,+ patch_recall_boundaries,+ rows,+ scripted_responses,+ stop_ingestion_workers,+)+from .scope import MemoryScope++_BOREALIS: Fact = ("Bob", "Borealis", "Bob will lead Borealis")++[email protected](autouse=True)+def boundaries(mocker, scope_graph, stub_graphiti_client):+ patch_recall_boundaries(mocker, scope_graph[0], stub_graphiti_client)+++@pytest_asyncio.fixture(loop_scope="function")+async def dream_apply(mocker: MockerFixture) -> AsyncIterator[None]:+ """apply's Postgres side (the dream session and its summary) stubbed."""+ mocker.patch.object(apply, "_create_dream_session", AsyncMock(return_value="s"))+ mocker.patch.object(apply, "_write_dream_summary_message", AsyncMock())+ mocker.patch(+ "backend.copilot.graphiti.ingest.ensure_scope_scheduled",+ AsyncMock(return_value=None),+ )+ yield+ await stop_ingestion_workers()+++async def _gather(scope: MemoryScope) -> DreamInput:+ sessions = SimpleNamespace(get_user_chat_sessions=AsyncMock(return_value=[]))+ with patch.object(fetch, "chat_db", return_value=sessions):+ return await fetch.gather_dream_input(scope)+++async def _apply(+ driver: AutoGPTFalkorDriver,+ scope: MemoryScope,+ build: BuildClient,+ ops: DreamOperations,+ read: DreamInput | None,+ *,+ extracted: Fact = ALICE,+ pass_id: str = "pass-1",+) -> dict:+ """``ops`` through ``apply_operations`` and the worker, graphiti's model+ extracting ``extracted`` from what it writes; the pass's stats."""+ client = build(driver, scripted_responses([extracted]))+ with patch.object(ingest, "get_graphiti_client", AsyncMock(return_value=client)):+ return await apply.apply_operations(+ scope,+ pass_id,+ ops,+ known_fact_uuids=read.known_fact_uuids if read else None,+ known_episode_uuids=read.known_episode_uuids if read else None,+ ingestion_drain_timeout=25,+ )+++def _resting_on(operation: str, read: DreamInput, content: str) -> DreamOperations:+ """A write of ``content`` citing what the pass read, as its model would."""+ facts, episodes = sorted(read.known_fact_uuids), sorted(read.known_episode_uuids)+ if operation == "consolidate":+ fact = ConsolidatedFact(+ content=content, confidence=0.9, source_episode_uuids=episodes+ )+ return DreamOperations(writes=[fact])+ finding = ProposedFinding(+ content=content,+ confidence=0.9,+ rationale="read before the forget",+ source_fact_uuids=facts,+ source_episode_uuids=episodes,+ )+ return DreamOperations(proposals=[finding])+++def _only_bob(read: DreamInput) -> DreamInput:+ """What ``read`` holds of Bob's fact and his chat turn, and nothing else."""+ facts = {fact.uuid for fact in read.facts if fact.fact == BOB[2]}+ episodes = {ep.uuid for ep in read.episodes if BOB[2] in (ep.content or "")}+ return read.model_copy(+ update={"known_fact_uuids": facts, "known_episode_uuids": episodes}+ )+++async def _edges(driver: AutoGPTFalkorDriver) -> set[str]:+ found = await rows(driver, "MATCH ()-[e:RELATES_TO]->() RETURN e.uuid AS uuid")+ return {row["uuid"] for row in found}+++async def _recalled(scope: MemoryScope) -> set[str]:+ return {fact.fact for fact in await recall.search_facts(scope, "Atlas", limit=20)}+++async def _dream_episodes(driver: AutoGPTFalkorDriver) -> int:+ found = await rows(+ driver,+ "MATCH (ep:Episodic) WHERE ep.name STARTS WITH 'dream_' "+ "RETURN count(ep) AS c",+ )+ return int(found[0]["c"])++[email protected]("hard", [False, True], ids=["soft", "hard"])[email protected]("operation", ["consolidate", "propose"])+async def test_a_dream_write_resting_on_a_fact_forgotten_since_it_read_is_dropped(+ scope_graph, stub_graphiti_client, dream_apply, operation: str, hard: bool+) -> None:+ driver, scope = scope_graph+ _, edges = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE], session_id="s-1"+ )+ read = await _gather(scope)+ assert {fact.fact for fact in read.facts} == {ALICE[2]}, "read before"+ await retract(scope, list(edges.values()), hard=hard)+ before = await _edges(driver)++ stats = await _apply(+ driver,+ scope,+ stub_graphiti_client,+ _resting_on(operation, read, ALICE[2]),+ read,+ )++ assert stats["dropped_forgotten"] == 1+ assert await _edges(driver) == before, "the dream added an edge"+ assert await _dream_episodes(driver) == 0+ assert ALICE[2] not in await _recalled(scope)++[email protected]("hard", [False, True], ids=["soft", "hard"])+async def test_an_uncited_dream_write_after_a_forget_is_dropped(+ scope_graph, stub_graphiti_client, dream_apply, hard: bool+) -> None:+ """Citing nothing, it rests on everything the pass read, and its+ statement is compared with the sentences forgotten facts keep. The soft+ case applies with nothing read, so only the statement can drop it; a+ hard forget keeps no sentence, so there the pass's read drops it."""+ driver, scope = scope_graph+ _, edges = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE], session_id="s-1"+ )+ read = await _gather(scope)+ await retract(scope, list(edges.values()), hard=hard)+ before = await _edges(driver)+ restated = ConsolidatedFact(content=" alice WORKS on\tAtlas ", confidence=0.9)++ stats = await _apply(+ driver,+ scope,+ stub_graphiti_client,+ DreamOperations(writes=[restated]),+ read if hard else None,+ )++ assert stats["dropped_forgotten"] == 1+ assert await _edges(driver) == before, "the dream added an edge"+ assert await _dream_episodes(driver) == 0+ assert ALICE[2] not in await _recalled(scope)++[email protected]("operation", ["consolidate", "propose"])+async def test_a_dream_write_resting_only_on_live_memory_still_lands(+ scope_graph, stub_graphiti_client, dream_apply, operation: str+) -> None:+ """Alice is forgotten after the pass read; a write citing only Bob's+ fact and episode is written, like one citing nothing from a pass that+ read after the forget."""+ driver, scope = scope_graph+ _, alice = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE], session_id="s-1"+ )+ await ingest_facts(driver, scope, stub_graphiti_client, [BOB], session_id="s-2")+ read = await _gather(scope)+ await retract(scope, list(alice.values()))++ ops = _resting_on(operation, _only_bob(read), _BOREALIS[2])+ cited = await _apply(+ driver, scope, stub_graphiti_client, ops, read, extracted=_BOREALIS+ )+ uncited = ConsolidatedFact(content="Bob plans Borealis", confidence=0.9)+ ops, read = DreamOperations(writes=[uncited]), await _gather(scope)+ fresh = await _apply(+ driver,+ scope,+ stub_graphiti_client,+ ops,+ read,+ extracted=_BOREALIS,+ pass_id="pass-2",+ )++ assert (cited["dropped_forgotten"], fresh["dropped_forgotten"]) == (0, 0)+ assert await _dream_episodes(driver) == 2, "both written"+ assert _BOREALIS[2] in (await live_facts(driver)).values()+ assert ALICE[2] not in await _recalled(scope)diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_dream_writers_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_dream_writers_integration_test.pynew file mode 100644index 0000000000..dcefdc599b--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_dream_writers_integration_test.py@@ -0,0 +1,171 @@+"""The dream's writers against a user's forget, on a live FalkorDB.++A dream reads the graph, asks a model, then writes: supersessions+(``mark_edges_superseded``) and single-hop neighbour invalidations+(``invalidate_entity_direct_neighbors``). A user can forget a fact in+between. Both writers only write over a live fact, and a forget's+``forgotten_at`` is written by nothing but ``recall_forget``, so wherever the+dream's write lands (between the forget's edge write and the rest of the+forget, after a whole forget, after a forget whose clean-up failed, or on a+forget from before the recall policy) the fact stays forgotten, its episode+and chat session stay hidden, and the forget's audit fields stay as it left+them. Each case was reproduced against the previous commit by an independent+validation (``r2-dream-atomicity.py``).++Run with FalkorDB reachable (see ``conftest.py``)::++ poetry run pytest -m integration backend/copilot/graphiti/recall_dream_writers_integration_test.py+"""++from collections.abc import Awaitable, Callable+from datetime import datetime, timedelta, timezone+from unittest.mock import patch++import pytest++from backend.copilot.dream import apply+from backend.copilot.dream.fetch import _fetch_recent_episodes+from backend.copilot.dream.hidden_sessions import hidden_session_ids+from backend.copilot.dream.schemas import DreamDemotion, EntityInvalidation++from . import recall_hide+from .falkordb_driver import AutoGPTFalkorDriver+from .recall_forget import retract+from .recall_integration_fixtures import (+ ALICE,+ edge_row,+ ingest_facts,+ patch_recall_boundaries,+ recalled_episodes,+ rows,+)+from .scope import MemoryScope++_LONG_AGO = "2025-01-01T00:00:00+00:00"+_WRITERS = ["supersede", "invalidate-neighbours"]+_TIMINGS = ["mid-forget", "after-forget", "after-failed-clean-up", "legacy-forget"]++[email protected](autouse=True)+def boundaries(mocker, scope_graph, stub_graphiti_client):+ patch_recall_boundaries(mocker, scope_graph[0], stub_graphiti_client)+++async def _dream_write(+ scope: MemoryScope, writer: str, edge_uuid: str, entity_uuid: str+) -> int:+ """The dream's write, as ``apply`` makes it; how many edges it changed."""+ if writer == "supersede":+ succeeded, _, _ = await apply._apply_demotions(+ scope, [DreamDemotion(edge_uuid=edge_uuid, reason="stale_fact")]+ )+ return succeeded+ touched, _ = await apply._apply_entity_invalidations(+ scope, [EntityInvalidation(entity_uuid=entity_uuid, reason="dead_client")]+ )+ return touched+++async def _forget(+ driver: AutoGPTFalkorDriver,+ scope: MemoryScope,+ edge_uuid: str,+ timing: str,+ dream_write: Callable[[], Awaitable[None]],+) -> None:+ """Forget ``edge_uuid`` with the dream's write landing at ``timing``."""+ if timing == "legacy-forget":+ await driver.execute_query(+ "MATCH ()-[e:RELATES_TO {uuid: $uuid}]->() SET e.expired_at = $old",+ uuid=edge_uuid,+ old=_LONG_AGO,+ )+ elif timing == "after-forget":+ await retract(scope, [edge_uuid])+ else:+ await _forget_with_the_write_inside(scope, edge_uuid, timing, dream_write)+ if timing != "mid-forget":+ await dream_write()+++async def _forget_with_the_write_inside(+ scope: MemoryScope,+ edge_uuid: str,+ timing: str,+ dream_write: Callable[[], Awaitable[None]],+) -> None:+ """Right after the forget's edge write, before any of its clean-up: the+ dream writes there, or the clean-up fails there."""+ original = AutoGPTFalkorDriver.execute_query++ async def interleave(self, cypher_query_, **params):+ if cypher_query_ == recall_hide.SCRUB_FACTS_QUERY:+ if timing == "after-failed-clean-up":+ raise RuntimeError("clean-up lost")+ await dream_write()+ return await original(self, cypher_query_, **params)++ with patch.object(AutoGPTFalkorDriver, "execute_query", interleave):+ await retract(scope, [edge_uuid])++[email protected]("timing", _TIMINGS)[email protected]("writer", _WRITERS)+async def test_a_dream_write_never_undoes_a_forget(+ scope_graph, stub_graphiti_client, writer: str, timing: str+) -> None:+ driver, scope = scope_graph+ episode, edges = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE], session_id="s-forgotten"+ )+ alice = edges[ALICE[2]]+ [source] = await rows(+ driver,+ "MATCH (a)-[e:RELATES_TO {uuid: $uuid}]->() RETURN a.uuid AS uuid",+ uuid=alice,+ )+ written: list[int] = []++ async def dream_write() -> None:+ written.append(await _dream_write(scope, writer, alice, source["uuid"]))++ await _forget(driver, scope, alice, timing, dream_write)++ assert written == [0], "the dream wrote over a forgotten fact"+ row = await edge_row(driver, alice)+ if timing == "legacy-forget":+ assert (row["status"], row["reason"]) == ("active", None)+ assert (row["expired_at"], row["forgotten_at"]) == (_LONG_AGO, None)+ else:+ assert (row["status"], row["reason"]) == ("retracted", "user_signal")+ assert row["forgotten_at"] is not None+ assert row["invalid_at"] is None, "a forget is not a world change"+ window_start = datetime.now(timezone.utc) - timedelta(days=14)+ dream = await _fetch_recent_episodes(driver, scope.group_id, window_start, 50)+ assert episode not in {e.uuid for e in dream}, "the dream reads it again"+ assert episode not in await recalled_episodes(scope)+ assert await hidden_session_ids(driver, scope.group_id) == {"s-forgotten"}++[email protected]("writer", _WRITERS)+async def test_the_dream_still_retires_a_live_fact(+ scope_graph, stub_graphiti_client, writer: str+) -> None:+ driver, scope = scope_graph+ _, edges = await ingest_facts(driver, scope, stub_graphiti_client, [ALICE])+ alice = edges[ALICE[2]]+ [source] = await rows(+ driver,+ "MATCH (a)-[e:RELATES_TO {uuid: $uuid}]->() RETURN a.uuid AS uuid",+ uuid=alice,+ )++ assert await _dream_write(scope, writer, alice, source["uuid"]) == 1++ row = await edge_row(driver, alice)+ assert row["status"] == "superseded" and row["expired_at"] is not None+ assert row["forgotten_at"] is None, "only a forget marks a fact forgotten"diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_episodes_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_episodes_test.pynew file mode 100644index 0000000000..f1c7b6e05c--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_episodes_test.py@@ -0,0 +1,131 @@+"""Unit tests for the recall policy's episode reads: ``recent_episodes``+for the assistant, ``previous_episode_uuids`` for ingestion's extraction+context (``recall_ingest.py``), and ``record_hit``.++The predicates and the fact search are pinned in ``recall_test.py``; the+live runs are ``recall_integration_test.py`` and+``ingest_recall_integration_test.py``.+"""++from datetime import datetime, timezone+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from graphiti_core.nodes import EpisodeType+from graphiti_core.search.search_utils import RELEVANT_SCHEMA_LIMIT++from . import recall, recall_ingest+from .scope import MemoryScope++_NOW = datetime(2026, 9, 26, 12, 0, tzinfo=timezone.utc)+_SCOPE = MemoryScope.for_user("user-abc")+++def _episode_record(uuid: str, content: str) -> dict:+ return {+ "uuid": uuid,+ "name": uuid,+ "group_id": _SCOPE.group_id,+ "created_at": _NOW.isoformat(),+ "source": "text",+ "source_description": "chat",+ "content": content,+ "valid_at": _NOW.isoformat(),+ "entity_edges": [],+ }+++def _driver_returning(records: list[dict]) -> AsyncMock:+ driver = AsyncMock()+ driver.execute_query.return_value = (records, [], None)+ return driver+++class TestRecentEpisodes:+ @pytest.mark.asyncio+ async def test_asks_for_recallable_episodes_and_returns_oldest_first(+ self,+ ) -> None:+ driver = _driver_returning(+ [_episode_record("newest", "b"), _episode_record("older", "a")]+ )+ open_driver = MagicMock(return_value=driver)+ with patch.object(recall, "open_driver", open_driver):+ episodes = await recall.recent_episodes(_SCOPE, 5)++ open_driver.assert_called_once_with(_SCOPE)+ query = driver.execute_query.await_args.args[0]+ kwargs = driver.execute_query.await_args.kwargs+ assert query.startswith(recall.forgotten_facts_clause())+ assert recall.recallable_episode_predicate("e") in query+ assert "ORDER BY e.valid_at DESC" in query+ assert kwargs["group_id"] == _SCOPE.group_id+ assert kwargs["limit"] == 5+ assert kwargs["source"] is None, "recall reads every source"+ assert [ep.uuid for ep in episodes] == ["older", "newest"]+ driver.close.assert_awaited_once()++ @pytest.mark.asyncio+ async def test_closes_driver_when_the_read_fails(self) -> None:+ driver = AsyncMock()+ driver.execute_query.side_effect = RuntimeError("falkordb down")+ with patch.object(recall, "open_driver", MagicMock(return_value=driver)):+ with pytest.raises(RuntimeError):+ await recall.recent_episodes(_SCOPE, 5)++ driver.close.assert_awaited_once()+++class TestPreviousEpisodeUuids:+ @pytest.mark.asyncio+ async def test_graphitis_own_pick_through_the_policy_oldest_first(+ self,+ ) -> None:+ """graphiti's ``retrieve_episodes`` window (same source, up to the+ new episode's time, ``RELEVANT_SCHEMA_LIMIT`` newest) with the+ recallable-episode test added."""+ driver = _driver_returning(+ [_episode_record("newest", "b"), _episode_record("older", "a")]+ )++ uuids = await recall_ingest.previous_episode_uuids(+ driver, _SCOPE.group_id, _NOW, EpisodeType.message+ )++ assert uuids == ["older", "newest"]+ query = driver.execute_query.await_args.args[0]+ assert recall.recallable_episode_predicate("e") in query+ assert "($source IS NULL OR e.source = $source)" in query+ assert driver.execute_query.await_args.kwargs == {+ "group_id": _SCOPE.group_id,+ "reference_time": _NOW,+ "source": "message",+ "limit": RELEVANT_SCHEMA_LIMIT,+ }++ @pytest.mark.asyncio+ async def test_a_failed_read_means_no_earlier_episodes_not_graphitis(+ self, caplog: pytest.LogCaptureFixture+ ) -> None:+ driver = AsyncMock()+ driver.execute_query.side_effect = RuntimeError("falkordb down")++ uuids = await recall_ingest.previous_episode_uuids(+ driver, _SCOPE.group_id, _NOW, EpisodeType.text+ )++ assert uuids == [], "None would let graphiti pick, forgotten text included"+ assert "extracting without earlier episodes" in caplog.text+++class TestRecordHit:+ @pytest.mark.asyncio+ async def test_counts_each_edge_once(self) -> None:+ record_memory_hit = AsyncMock()+ with patch.object(recall, "record_memory_hit", record_memory_hit):+ await recall.record_hit(_SCOPE, ["e1", "e2", "e1"])++ assert [c.args for c in record_memory_hit.await_args_list] == [+ (_SCOPE, "e1"),+ (_SCOPE, "e2"),+ ]diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_fake_redis.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_fake_redis.pynew file mode 100644index 0000000000..bee90e9776--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_fake_redis.py@@ -0,0 +1,53 @@+"""The recall tests' in-memory Redis. Not collected by pytest."""++from backend.copilot.dream.locks import EXTEND_SCRIPT, UNLOCK_SCRIPT+++class FakeRedis:+ """Honours SET NX like Redis, for the memory-hit counter+ (``record_memory_hit``) and the graph write lock (``scope_lock.py``),+ and runs the lock's compare-and-delete and compare-and-extend scripts.+ Values are stored as strings, as the ``decode_responses`` client returns+ them; TTLs are recorded, not enforced."""++ def __init__(self) -> None:+ self.values: dict[str, str] = {}+ self.ttls: dict[str, int] = {}++ async def get(self, key: str) -> str | None:+ return self.values.get(key)++ async def set(+ self,+ key: str,+ value: object,+ *,+ nx: bool = False,+ ex: int | None = None,+ px: int | None = None,+ ) -> bool | None:+ if nx and key in self.values:+ return None+ self.values[key] = str(value)+ self.ttls[key] = ex if ex is not None else (px or 0) // 1000+ return True++ async def incr(self, key: str) -> int:+ self.values[key] = str(int(self.values.get(key, "0")) + 1)+ return int(self.values[key])++ async def expire(self, key: str, ttl_seconds: int) -> bool:+ self.ttls[key] = int(ttl_seconds)+ return key in self.values++ async def eval(self, script: str, numkeys: int, key: str, *args: object) -> int:+ token, *rest = (str(arg) for arg in args)+ if self.values.get(key) != token:+ return 0+ if script == UNLOCK_SCRIPT:+ del self.values[key]+ return 1+ if script == EXTEND_SCRIPT:+ self.ttls[key] = int(rest[0])+ return 1+ raise NotImplementedError("FakeRedis runs only the lock scripts")diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_forget.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_forget.pynew file mode 100644index 0000000000..362061abee--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_forget.py@@ -0,0 +1,181 @@+"""Forgetting under the recall policy.++After ``retract`` the recall paths in ``recall.py`` return neither the+forgotten facts nor the episode text they came from, the sentence is gone+from the fact and from what graphiti read out of it onto entities, and the+audit record stays; ``graphiti/AGENTS.md`` lists the limits. The chat forget+tool and the settings page both forget through here, so a forget means the+same thing wherever it starts. ``forgotten_at``, the policy's marker for a+forgotten fact, is written here and, for forgets made before it existed, by+``migrations/backfill_legacy_forgets.py``; nothing else writes it.+"""++import logging+from datetime import datetime, timezone+from typing import Any++from graphiti_core.driver.driver import GraphDriver++from .falkordb_driver import open_driver+from .memory_model import ForgetResult, MemoryForgetFailure, MemoryStatus+from .recall import USER_FORGET_REASON+from .recall_hide import hide+from .recall_orphans import purge+from .scope import MemoryScope+from .scope_lock import FORGET_LOCK_WAIT_SECONDS, LockState, graph_write_lock++logger = logging.getLogger(__name__)+++async def retract(+ scope: MemoryScope,+ uuids: list[str],+ *,+ hard: bool = False,+ reason: str = USER_FORGET_REASON,+) -> ForgetResult:+ """Forget edges: recall stops returning them and the text they came from.++ The forget holds the graph's write lock (``scope_lock.py``), so it never+ lands while an ingestion is between reading the graph and saving over+ it; when the lock stays busy for ``FORGET_LOCK_WAIT_SECONDS`` every uuid+ fails as ``busy`` and nothing is written.++ Soft (the default) stamps ``forgotten_at``, sets ``status='retracted'``+ and ``expiration_reason``, keeps an earlier ``expired_at`` and leaves+ ``invalid_at`` alone: a forget retracts our record of a fact, it does+ not say the world changed (Snodgrass). ``recall_hide.hide`` then moves+ the sentence out of what graphiti reads and redacts every episode citing+ the fact; edges and episodes stay for audit. Hard does the same first,+ then empties and deletes what only those edges kept, the edges last+ (``recall_orphans.purge``), so forgetting again after any failure+ finishes the job. A failed step after the edge write is a+ ``cleanup_error`` on each edge it concerned; recall hides the fact and+ its text regardless.+ """+ requested = list(dict.fromkeys(uuids))+ if not requested:+ return ForgetResult()+ wait = FORGET_LOCK_WAIT_SECONDS+ async with graph_write_lock(scope.group_id, wait_seconds=wait) as lock:+ if lock is LockState.BUSY:+ busy = [MemoryForgetFailure.busy(uuid) for uuid in requested]+ return ForgetResult(failures=busy)+ driver = open_driver(scope)+ try:+ return await _forget(driver, scope.group_id, requested, hard, reason)+ finally:+ await driver.close()+++async def _forget(+ driver: GraphDriver, group_id: str, uuids: list[str], hard: bool, reason: str+) -> ForgetResult:+ result = ForgetResult()+ now = datetime.now(timezone.utc).isoformat()+ found = await _existing_edges(driver, group_id, uuids, result)+ retracted = await _retract_edges(driver, group_id, found, reason, now, result)+ hidden = await hide(driver, group_id, retracted, now, result)+ if not hard:+ result.deleted = retracted+ elif hidden:+ await purge(driver, group_id, retracted, now, result)+ return result+++async def _existing_edges(+ driver: GraphDriver,+ group_id: str,+ uuids: list[str],+ result: ForgetResult,+) -> list[str]:+ """The requested uuids that name a forgettable edge in the graph.++ A read, so forgetting in a scope that has no graph never creates one (a+ write would). Every other uuid is recorded on ``result`` as a failure.+ """+ try:+ rows = _rows(+ await driver.execute_query(+ _EXISTING_EDGES_QUERY, uuids=uuids, group_id=group_id+ )+ )+ except Exception as exc:+ logger.warning(f"Forget lookup failed in graph {group_id[:20]}", exc_info=True)+ result.failures.extend(+ MemoryForgetFailure.query_error(edge_uuid, exc) for edge_uuid in uuids+ )+ return []+ found = {row["uuid"] for row in rows}+ result.failures.extend(+ MemoryForgetFailure.no_match(edge_uuid)+ for edge_uuid in uuids+ if edge_uuid not in found+ )+ return [edge_uuid for edge_uuid in uuids if edge_uuid in found]+++async def _retract_edges(+ driver: GraphDriver,+ group_id: str,+ uuids: list[str],+ reason: str,+ now: str,+ result: ForgetResult,+) -> list[str]:+ """Mark each edge forgotten and retracted; the uuids that were.++ One query per uuid, so one bad edge cannot hide what happened to the+ others (SECRT-2371); failures are recorded on ``result``.+ """+ retracted: list[str] = []+ for edge_uuid in uuids:+ try:+ rows = _rows(+ await driver.execute_query(+ _RETRACT_EDGE_QUERY,+ uuid=edge_uuid,+ group_id=group_id,+ now=now,+ status=MemoryStatus.retracted.value,+ reason=reason,+ )+ )+ except Exception as exc:+ logger.warning(+ f"Forget failed for edge {edge_uuid} in graph {group_id[:20]}",+ exc_info=True,+ )+ result.failures.append(MemoryForgetFailure.query_error(edge_uuid, exc))+ continue+ if rows:+ retracted.append(edge_uuid)+ else:+ result.failures.append(MemoryForgetFailure.no_match(edge_uuid))+ return retracted+++def _rows(result: Any) -> list[dict[str, Any]]:+ return result[0] if result else []+++# The ``group_id`` test is defence in depth on top of the per-scope database;+# an edge with no ``group_id`` (a legacy write) is still forgettable.+_EXISTING_EDGES_QUERY = """+MATCH ()-[e:MENTIONS|RELATES_TO|HAS_MEMBER]->()+WHERE e.uuid IN $uuids AND (e.group_id = $group_id OR e.group_id IS NULL)+RETURN DISTINCT e.uuid AS uuid+"""++# ``coalesce`` keeps the first forget's ``forgotten_at`` and the first+# retirement time on an edge that already had one (a graphiti expiry, a+# dream demotion or an earlier forget), so a repeat is harmless.+_RETRACT_EDGE_QUERY = """+MATCH ()-[e:MENTIONS|RELATES_TO|HAS_MEMBER {uuid: $uuid}]->()+WHERE e.group_id = $group_id OR e.group_id IS NULL+SET e.forgotten_at = coalesce(e.forgotten_at, $now),+ e.expired_at = coalesce(e.expired_at, $now),+ e.status = $status,+ e.expiration_reason = $reason+RETURN e.uuid AS uuid+"""diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_forget_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_forget_integration_test.pynew file mode 100644index 0000000000..ea082ca719--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_forget_integration_test.py@@ -0,0 +1,226 @@+"""The forget contract against a live FalkorDB.++A forget hides the fact and the text it came from from every read the+assistant or a dream makes, while the audit record stays: a hard forget never+deletes an episode a retained edge still cites. Each case was reproduced+against the first recall-policy commit by an independent validation; the+unit siblings (``recall_forget_test.py``, ``recall_hide_test.py``,+``recall_test.py``) pin the Cypher. The hard forget's own cases are in+``recall_hard_forget_integration_test.py``, the dream's writers in+``recall_dream_writers_integration_test.py``.++Run with FalkorDB reachable (see ``conftest.py``)::++ poetry run pytest -m integration backend/copilot/graphiti/recall_forget_integration_test.py+"""++import asyncio+from datetime import datetime, timedelta, timezone+from unittest.mock import patch++import pytest++from backend.copilot.dream import ratification+from backend.copilot.dream.fetch import _fetch_recent_episodes+from backend.copilot.model import ChatSession+from backend.copilot.tools import graphiti_search+from backend.copilot.tools.models import MemorySearchResponse++from . import context, recall+from .falkordb_driver import AutoGPTFalkorDriver+from .memory_model import MemoryForgetFailureCode+from .recall_forget import retract+from .recall_integration_fixtures import (+ ALICE,+ BOB,+ CAROL,+ capture_spawned_tasks,+ edge_row,+ episode_row,+ ingest_facts,+ patch_recall_boundaries,+ recalled_facts,+)+from .scope import MemoryScope++_LONG_AGO = "2025-01-01T00:00:00+00:00"++[email protected](autouse=True)+def boundaries(mocker, scope_graph, stub_graphiti_client):+ patch_recall_boundaries(mocker, scope_graph[0], stub_graphiti_client)+++def hit_tasks(mocker) -> list[asyncio.Task]:+ return capture_spawned_tasks(mocker)+++async def _what_recall_shows(+ driver: AutoGPTFalkorDriver, scope: MemoryScope, hit_tasks: list[asyncio.Task]+) -> list[str]:+ """Every episode text a reader gets: recent episodes, warm context,+ ``memory_search`` and the dream's episode gather."""+ recent = [episode.content for episode in await recall.recent_episodes(scope, 5)]+ warm = await context._fetch(scope, "Atlas")+ await asyncio.gather(*context._pending_hit_tasks)+ session = ChatSession.new(scope.owner_user_id, dry_run=False)+ found = await graphiti_search.MemorySearchTool()._execute(+ scope.owner_user_id, session, query="Atlas"+ )+ await asyncio.gather(*hit_tasks)+ assert isinstance(found, MemorySearchResponse)+ window_start = datetime.now(timezone.utc) - timedelta(days=14)+ dream = await _fetch_recent_episodes(driver, scope.group_id, window_start, 50)+ return [+ *recent,+ warm or "",+ *found.recent_episodes,+ *(episode.content or "" for episode in dream),+ ]++[email protected]("hard", [False, True], ids=["soft", "hard"])+async def test_forgetting_a_fact_hides_the_episode_it_shares(+ scope_graph, stub_graphiti_client, hit_tasks, hard: bool+) -> None:+ driver, scope = scope_graph+ episode, edges = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE, BOB]+ )++ result = await retract(scope, [edges[ALICE[2]]], hard=hard)++ assert result.deleted == [edges[ALICE[2]]] and result.failures == []+ shown = await _what_recall_shows(driver, scope, hit_tasks)+ assert not any(ALICE[2] in text for text in shown), "forgotten text recalled"+ assert await recalled_facts(scope) == {edges[BOB[2]]}, "Bob's fact stays"+ kept = await episode_row(driver, episode)+ assert kept["redacted_at"] is not None+ assert ALICE[2] in kept["content"], "the stored text is kept for audit"+++async def test_a_forget_from_before_the_policy_still_hides_its_episode(+ scope_graph, stub_graphiti_client, hit_tasks+) -> None:+ """The pre-policy forget set ``expired_at`` and nothing else, and no+ episode was redacted; the read side must recognise that shape."""+ driver, scope = scope_graph+ _, edges = await ingest_facts(driver, scope, stub_graphiti_client, [ALICE])+ await driver.execute_query(+ "MATCH ()-[e:RELATES_TO {uuid: $uuid}]->() SET e.expired_at = $old",+ uuid=edges[ALICE[2]],+ old=_LONG_AGO,+ )++ shown = await _what_recall_shows(driver, scope, hit_tasks)++ assert not any(ALICE[2] in text for text in shown), "forgotten text recalled"+++async def test_a_failed_redaction_is_reported_and_the_text_stays_hidden(+ scope_graph, stub_graphiti_client, hit_tasks+) -> None:+ driver, scope = scope_graph+ episode, edges = await ingest_facts(driver, scope, stub_graphiti_client, [ALICE])+ write = AutoGPTFalkorDriver.execute_query++ async def lose_the_redaction(self, cypher_query_, **params):+ if "SET ep.redacted_at" in cypher_query_:+ raise RuntimeError("redaction write lost")+ return await write(self, cypher_query_, **params)++ with patch.object(AutoGPTFalkorDriver, "execute_query", lose_the_redaction):+ result = await retract(scope, [edges[ALICE[2]]])++ shown = await _what_recall_shows(driver, scope, hit_tasks)+ assert not any(ALICE[2] in text for text in shown), "forgotten text recalled"+ assert (await episode_row(driver, episode))["redacted_at"] is None+ assert result.deleted == [edges[ALICE[2]]], "the fact itself was forgotten"+ assert [(f.uuid, f.code) for f in result.failures] == [+ (edges[ALICE[2]], MemoryForgetFailureCode.CLEANUP_ERROR)+ ]+++async def test_hard_forget_keeps_an_episode_a_retracted_fact_still_cites(+ scope_graph, stub_graphiti_client+) -> None:+ driver, scope = scope_graph+ episode, edges = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE, BOB]+ )+ alice, bob = edges[ALICE[2]], edges[BOB[2]]+ await retract(scope, [bob])++ result = await retract(scope, [alice], hard=True)++ assert result.deleted == [alice] and result.failures == []+ assert result.tombstoned_episodes == [], "Bob's retained edge still cites it"+ assert await edge_row(driver, alice) == {}+ retained = await edge_row(driver, bob)+ assert retained["status"] == "retracted"+ assert episode in retained["episodes"]+ assert (await episode_row(driver, episode))["entity_edges"] == [bob]+++async def test_a_forget_keeps_the_expiry_and_valid_time_already_set(+ scope_graph, stub_graphiti_client+) -> None:+ driver, scope = scope_graph+ _, edges = await ingest_facts(driver, scope, stub_graphiti_client, [ALICE])+ await driver.execute_query(+ "MATCH ()-[e:RELATES_TO {uuid: $uuid}]->() "+ "SET e.expired_at = $old, e.invalid_at = $old",+ uuid=edges[ALICE[2]],+ old=_LONG_AGO,+ )++ await retract(scope, [edges[ALICE[2]]])++ row = await edge_row(driver, edges[ALICE[2]])+ assert row["status"] == "retracted"+ assert row["expired_at"] == _LONG_AGO, "the first retirement time is kept"+ assert row["invalid_at"] == _LONG_AGO++[email protected]("hits", [2, 0], ids=["promote", "supersede"])+async def test_the_ratification_sweep_never_overwrites_a_forget(+ scope_graph, stub_graphiti_client, mocker, hits: int+) -> None:+ """The user forgets a tentative fact after the nightly sweep listed it+ and before the sweep writes: neither the promotion nor the unratified+ supersession may replace the retraction."""+ driver, scope = scope_graph+ _, edges = await ingest_facts(+ driver, scope, stub_graphiti_client, [CAROL], status="tentative"+ )+ carol = edges[CAROL[2]]+ await driver.execute_query(+ "MATCH ()-[e:RELATES_TO {uuid: $uuid}]->() SET e.created_at = $old",+ uuid=carol,+ old=_LONG_AGO, # past the grace period, so no hits means supersede+ )++ async def forget_then_count(_scope: MemoryScope, edge_uuid: str) -> int:+ await retract(scope, [edge_uuid])+ return hits++ mocker.patch.object(ratification, "_get_hit_count", forget_then_count)+ result = await ratification.run_ratification_pass(scope.owner_user_id)++ assert result.examined_count == 1+ assert (result.ratified_count, result.superseded_count) == (0, 0)+ row = await edge_row(driver, carol)+ assert (row["status"], row["reason"]) == ("retracted", "user_signal")diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_forget_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_forget_test.pynew file mode 100644index 0000000000..4a7df16c38--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_forget_test.py@@ -0,0 +1,272 @@+"""Unit tests for ``recall_forget.retract`` against a mock driver and the+in-memory write lock (``conftest.lock_redis``).++Pin the Cypher each forget mode issues, its order, the write lock it holds+around every write, and the per-uuid failure reporting. What hiding and+purging send is pinned in ``recall_hide_test.py`` and+``recall_orphans_test.py``; the live runs are+``recall_forget_integration_test.py``, ``recall_hard_forget_integration_test.py``,+``recall_inflight_integration_test.py`` and ``recall_integration_test.py``.+"""++from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from . import recall_forget, recall_hide, recall_orphans, scope_lock+from .memory_model import (+ FORGET_BUSY_REASON,+ FORGET_NO_MATCH_REASON,+ MemoryForgetFailureCode,+)+from .recall import FORGOTTEN_FACT+from .recall_fake_redis import FakeRedis+from .scope import MemoryScope, write_lock_key++_SCOPE = MemoryScope.for_user("user-abc")+_LOCK = write_lock_key(_SCOPE.group_id)+_CLEANUP = MemoryForgetFailureCode.CLEANUP_ERROR+++def _driver(*results) -> AsyncMock:+ """A driver whose queries return (or raise) ``results`` in order."""+ driver = AsyncMock()+ driver.execute_query.side_effect = [+ r if isinstance(r, Exception) else (r, [], None) for r in results+ ]+ return driver+++async def _retract(driver: AsyncMock, uuids: list[str], **kwargs):+ with patch.object(recall_forget, "open_driver", MagicMock(return_value=driver)):+ return await recall_forget.retract(_SCOPE, uuids, **kwargs)+++def _queries(driver: AsyncMock) -> list[str]:+ return [call.args[0] for call in driver.execute_query.await_args_list]+++def _call(driver: AsyncMock, index: int) -> tuple[str, dict]:+ call = driver.execute_query.await_args_list[index]+ return call.args[0], call.kwargs+++# lookup, retract, scrub facts, entity keys (none), redact+_SOFT = ([{"uuid": "u1"}], [{"uuid": "u1"}], [{"ends": []}], [], [{"uuid": "ep1"}])+++class TestSoftRetract:+ @pytest.mark.asyncio+ async def test_marks_the_edge_forgotten_then_hides_its_text(self) -> None:+ driver = _driver(*_SOFT)++ result = await _retract(driver, ["u1"])++ assert (result.deleted, result.failures) == (["u1"], [])+ assert result.redacted_episodes == ["ep1"]+ driver.close.assert_awaited_once()+ lookup, lookup_kwargs = _call(driver, 0)+ assert "SET" not in lookup, "the lookup must be a read"+ assert lookup_kwargs == {"uuids": ["u1"], "group_id": _SCOPE.group_id}+ write, kwargs = _call(driver, 1)+ assert "SET e.forgotten_at = coalesce(e.forgotten_at, $now)," in write+ assert "e.expired_at = coalesce(e.expired_at, $now)," in write+ assert "invalid_at" not in write, "a forget is not a world change"+ assert (kwargs["status"], kwargs["reason"]) == ("retracted", "user_signal")+ assert _call(driver, 2) == (+ recall_hide.SCRUB_FACTS_QUERY,+ {"uuids": ["u1"], "placeholder": FORGOTTEN_FACT},+ )+ assert _call(driver, 4) == (+ recall_hide.REDACT_EPISODES_QUERY,+ {"uuids": ["u1"], "now": kwargs["now"]},+ )++ @pytest.mark.asyncio+ async def test_reason_is_recorded(self) -> None:+ driver = _driver(*_SOFT)++ await _retract(driver, ["u1"], reason="settings_page")++ assert _call(driver, 1)[1]["reason"] == "settings_page"++ @pytest.mark.asyncio+ async def test_unknown_uuid_is_a_no_match_and_nothing_is_written(self) -> None:+ driver = _driver([])++ result = await _retract(driver, ["missing"])++ assert result.deleted == []+ assert [(f.uuid, f.code) for f in result.failures] == [+ ("missing", MemoryForgetFailureCode.NO_MATCH)+ ]+ assert result.failures[0].reason == FORGET_NO_MATCH_REASON+ assert driver.execute_query.await_count == 1++ @pytest.mark.asyncio+ async def test_lookup_error_fails_every_uuid_with_its_reason(self) -> None:+ driver = _driver(RuntimeError("Unknown function 'datetime'"))++ result = await _retract(driver, ["u1", "u2"])++ assert result.deleted == []+ assert [f.uuid for f in result.failures] == ["u1", "u2"]+ for failure in result.failures:+ assert failure.code == MemoryForgetFailureCode.QUERY_ERROR+ assert failure.reason == (+ "Deletion query failed: RuntimeError: Unknown function 'datetime'"+ )+ driver.close.assert_awaited_once()++ @pytest.mark.asyncio+ async def test_write_error_and_vanished_edge_are_told_apart(self) -> None:+ driver = _driver(+ [{"uuid": "errored"}, {"uuid": "vanished"}],+ RuntimeError("boom"),+ [], # deleted between the lookup and the write+ )++ result = await _retract(driver, ["errored", "vanished"])++ by_uuid = {f.uuid: f for f in result.failures}+ assert by_uuid["errored"].code == MemoryForgetFailureCode.QUERY_ERROR+ assert "boom" in by_uuid["errored"].reason+ assert by_uuid["vanished"].code == MemoryForgetFailureCode.NO_MATCH+ assert driver.execute_query.await_count == 3, "nothing to hide"++ @pytest.mark.asyncio+ async def test_a_failed_hide_is_a_cleanup_error_on_each_edge(self) -> None:+ driver = _driver(+ [{"uuid": "u1"}, {"uuid": "u2"}],+ [{"uuid": "u1"}],+ [{"uuid": "u2"}],+ RuntimeError("down"),+ )++ result = await _retract(driver, ["u1", "u2"])++ assert result.deleted == ["u1", "u2"], "the facts themselves are forgotten"+ assert [(f.uuid, f.code) for f in result.failures] == [+ ("u1", _CLEANUP),+ ("u2", _CLEANUP),+ ]+ assert "RuntimeError: down" in result.failures[0].reason++ @pytest.mark.asyncio+ async def test_repeated_uuids_are_forgotten_once(self) -> None:+ driver = _driver(*_SOFT)++ result = await _retract(driver, ["u1", "u1"])++ assert result.deleted == ["u1"] and result.failures == []+ assert _call(driver, 0)[1]["uuids"] == ["u1"]++ @pytest.mark.asyncio+ async def test_empty_request_opens_no_driver(self) -> None:+ open_driver = MagicMock()+ with patch.object(recall_forget, "open_driver", open_driver):+ result = await recall_forget.retract(_SCOPE, [])++ assert result.deleted == [] and result.failures == []+ open_driver.assert_not_called()+++class TestWriteLock:+ @pytest.mark.asyncio+ async def test_every_write_happens_under_the_graphs_lock(+ self, lock_redis: FakeRedis+ ) -> None:+ held: list[bool] = []+ results = list(_SOFT)++ async def query(cypher: str, **params: object):+ held.append(_LOCK in lock_redis.values)+ return results.pop(0), [], None++ driver = AsyncMock()+ driver.execute_query.side_effect = query++ await _retract(driver, ["u1"])++ assert held == [True] * len(_SOFT)+ assert _LOCK not in lock_redis.values, "released afterwards"++ @pytest.mark.asyncio+ async def test_a_lock_held_for_the_whole_wait_fails_as_busy_and_writes_nothing(+ self, lock_redis: FakeRedis+ ) -> None:+ lock_redis.values[_LOCK] = "an ingestion's token"+ open_driver = MagicMock()++ with (+ patch.object(recall_forget, "FORGET_LOCK_WAIT_SECONDS", 0),+ patch.object(recall_forget, "open_driver", open_driver),+ ):+ result = await recall_forget.retract(_SCOPE, ["u1", "u2"])++ assert result.deleted == []+ assert [(f.uuid, f.code) for f in result.failures] == [+ ("u1", MemoryForgetFailureCode.BUSY),+ ("u2", MemoryForgetFailureCode.BUSY),+ ]+ assert result.failures[0].reason == FORGET_BUSY_REASON+ open_driver.assert_not_called()+ assert lock_redis.values[_LOCK] == "an ingestion's token", "left alone"++ @pytest.mark.asyncio+ async def test_redis_unreachable_forgets_without_the_lock(+ self, caplog: pytest.LogCaptureFixture+ ) -> None:+ driver = _driver(*_SOFT)+ down = AsyncMock(side_effect=ConnectionError("redis down"))++ with patch.object(scope_lock, "get_redis_async", down):+ result = await _retract(driver, ["u1"])++ assert (result.deleted, result.failures) == (["u1"], [])+ assert "write lock unavailable" in caplog.text+++class TestHardRetract:+ @pytest.mark.asyncio+ async def test_hides_everything_before_it_purges_anything(self) -> None:+ driver = _driver(+ *_SOFT[:4],+ [{"uuid": "ep1"}, {"uuid": "ep2"}], # redact+ [{"uuid": "ep1", "content": "Alice works on Atlas"}], # citing+ [{"uuid": "ep1"}], # tombstone+ [{"uuid": "u1", "deleted_entities": ["alice", "carol"]}], # delete+ )++ result = await _retract(driver, ["u1"], hard=True)++ assert result.deleted == ["u1"] and result.failures == []+ assert result.tombstoned_episodes == ["ep1"]+ assert result.redacted_episodes == ["ep2"], "kept for another edge, hidden"+ assert result.deleted_entities == ["alice", "carol"]+ assert _queries(driver)[4:] == [+ recall_hide.REDACT_EPISODES_QUERY,+ recall_orphans._CITING_EPISODES_QUERY,+ recall_orphans._TOMBSTONE_QUERY,+ recall_orphans._DELETE_EDGE_QUERY,+ ]++ @pytest.mark.asyncio+ async def test_a_failed_hide_stops_it_before_any_purge(self) -> None:+ driver = _driver([{"uuid": "u1"}], [{"uuid": "u1"}], RuntimeError("down"))++ result = await _retract(driver, ["u1"], hard=True)++ assert result.deleted == [], "a deleted edge could no longer hide its text"+ assert [(f.uuid, f.code) for f in result.failures] == [("u1", _CLEANUP)]+ assert driver.execute_query.await_count == 3++ @pytest.mark.asyncio+ async def test_unmatched_delete_is_a_no_match(self) -> None:+ driver = _driver(*_SOFT[:4], [], [], [], [])++ result = await _retract(driver, ["u1"], hard=True)++ assert result.deleted == []+ assert [f.code for f in result.failures] == [MemoryForgetFailureCode.NO_MATCH]+ assert driver.execute_query.await_count == 8diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_hard_forget_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_hard_forget_integration_test.pynew file mode 100644index 0000000000..c435e30790--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_hard_forget_integration_test.py@@ -0,0 +1,333 @@+"""A hard forget against a live FalkorDB: what it deletes, what it keeps as a+tombstone, and that a failed clean-up finishes when the forget is repeated.++An episode no remaining edge cites is emptied into a tombstone, never+deleted: its name, source description and envelope provenance say which+chat session it came from, and the dream leaves that session out. The edge+itself is deleted last, in one query with what it alone kept, so a forget+that failed part-way finds it again. A fact forgotten softly can still be+found from chat and made permanent. Each case was reproduced against the+previous commit by an independent validation (``r2-dream-atomicity.py``,+``r2-failure.py``).++Run with FalkorDB reachable (see ``conftest.py``)::++ poetry run pytest -m integration backend/copilot/graphiti/recall_hard_forget_integration_test.py+"""++from datetime import datetime, timezone+from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from pytest_mock import MockerFixture++from backend.api.features.admin import memory_admin_routes+from backend.api.features.admin.memory_admin_routes import (+ FactListResponse,+ GraphResponse,+)+from backend.copilot.dream import fetch+from backend.copilot.dream.hidden_sessions import hidden_session_ids+from backend.copilot.dream.prompts import build_consolidate_prompt+from backend.copilot.model import ChatSession+from backend.copilot.tools import graphiti_forget+from backend.copilot.tools.models import (+ MemoryForgetCandidatesResponse,+ MemoryForgetConfirmResponse,+)++from . import recall_hide, recall_orphans+from .falkordb_driver import AutoGPTFalkorDriver+from .memory_model import MemoryEnvelope, MemoryForgetFailureCode+from .recall import FORGOTTEN_FACT+from .recall_forget import retract+from .recall_integration_fixtures import (+ ALICE,+ BOB,+ CAROL,+ count,+ edge_row,+ episode_row,+ ingest_facts,+ patch_recall_boundaries,+ recalled_episodes,+ recalled_facts,+ rows,+)+from .scope import MemoryScope++_STEPS = {+ "scrub": recall_hide.SCRUB_FACTS_QUERY,+ "entity-scrub": recall_hide._SCRUB_ENTITIES_QUERY,+ "redaction": recall_hide.REDACT_EPISODES_QUERY,+ "citing-read": recall_orphans._CITING_EPISODES_QUERY,+ "tombstone": recall_orphans._TOMBSTONE_QUERY,+ "edge-delete": recall_orphans._DELETE_EDGE_QUERY,+}++[email protected](autouse=True)+def boundaries(mocker, scope_graph, stub_graphiti_client):+ patch_recall_boundaries(mocker, scope_graph[0], stub_graphiti_client)+++async def _dangling(driver: AutoGPTFalkorDriver) -> list[str]:+ """Episode references to a deleted edge, and edge references to a+ deleted episode."""+ to_edges = await rows(+ driver,+ """+ MATCH (ep:Episodic)+ UNWIND coalesce(ep.entity_edges, []) AS ref+ OPTIONAL MATCH ()-[e:RELATES_TO {uuid: ref}]->()+ WITH ep, ref, e WHERE e IS NULL+ RETURN ep.uuid + ' -> ' + ref AS dangling+ """,+ )+ to_episodes = await rows(+ driver,+ """+ MATCH ()-[e:RELATES_TO]->()+ UNWIND coalesce(e.episodes, []) AS ref+ OPTIONAL MATCH (ep:Episodic {uuid: ref})+ WITH e, ref, ep WHERE ep IS NULL+ RETURN e.uuid + ' -> ' + ref AS dangling+ """,+ )+ return [row["dangling"] for row in to_edges + to_episodes]+++async def _admin_views(+ mocker: MockerFixture, scope: MemoryScope+) -> tuple[FactListResponse, GraphResponse]:+ """The admin facts list (any status) and graph view (episodes shown)."""+ mocker.patch.object(+ memory_admin_routes,+ "_resolve_and_audit_memory_scope",+ AsyncMock(return_value=scope),+ )+ common = {+ "request": MagicMock(),+ "user_id": scope.owner_user_id,+ "caller_id": "admin",+ "jwt_payload": {},+ "expert_id": None,+ }+ facts = await memory_admin_routes._list_facts_impl(+ **common, limit=50, status="any", scope=None+ )+ graph = await memory_admin_routes._get_graph_impl(+ **common,+ node_limit=100,+ edge_limit=100,+ include_episodes=True,+ include_communities=False,+ )+ return facts, graph+++def _chat_store(*session_ids: str) -> SimpleNamespace:+ message = SimpleNamespace(role="user", content="Whole session text")+ return SimpleNamespace(+ get_user_chat_sessions=AsyncMock(+ return_value=[+ SimpleNamespace(+ session_id=s,+ title=s,+ started_at=datetime(2026, 1, 1, tzinfo=timezone.utc),+ )+ for s in session_ids+ ]+ ),+ get_chat_messages_paginated=AsyncMock(+ return_value=SimpleNamespace(messages=[message])+ ),+ )+++async def test_hard_forget_tombstones_the_last_episode_and_deletes_orphans(+ scope_graph, stub_graphiti_client+) -> None:+ driver, scope = scope_graph+ episode, edges = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE, BOB]+ )+ alice, bob = edges[ALICE[2]], edges[BOB[2]]++ first = await retract(scope, [alice], hard=True)++ # Bob's fact keeps the episode, hidden because it holds Alice's sentence,+ # and the episode's mentions keep every entity it names.+ assert first.deleted == [alice] and first.failures == []+ assert first.tombstoned_episodes == [] and first.deleted_entities == []+ assert first.redacted_episodes == [episode]+ assert await edge_row(driver, alice) == {}+ assert (await episode_row(driver, episode))["entity_edges"] == [bob]++ second = await retract(scope, [bob], hard=True)++ assert second.tombstoned_episodes == [episode]+ assert len(second.deleted_entities) == 3 # Alice, Bob, Atlas+ tombstone = await episode_row(driver, episode)+ assert (tombstone["content"], tombstone["entity_edges"]) == ("", [])+ assert tombstone["hard_deleted_at"] and tombstone["redacted_at"]+ assert tombstone["name"], "a tombstone keeps its name"+ assert await count(driver, "MATCH (n:Episodic) RETURN count(n) AS c") == 1+ assert await count(driver, "MATCH (n:Entity) RETURN count(n) AS c") == 0+ assert await count(driver, "MATCH ()-[e]->() RETURN count(e) AS c") == 0+ assert await recalled_episodes(scope) == set()++[email protected]("named_by", ["chat-turn", "stored-memory"])+async def test_hard_forget_keeps_the_session_out_of_the_dream(+ scope_graph, stub_graphiti_client, mocker, named_by: str+) -> None:+ """The episode's name says its session, or only its stored memory's+ envelope does: either way the tombstone still says it."""+ driver, scope = scope_graph+ if named_by == "chat-turn":+ forgotten, edges = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE], session_id="s-forgotten"+ )+ else:+ envelope = MemoryEnvelope(+ content=ALICE[2], provenance="session:s-forgotten#msg:7"+ )+ forgotten, edges = await ingest_facts(+ driver,+ scope,+ stub_graphiti_client,+ [ALICE],+ body=envelope.model_dump_json(),+ )+ await ingest_facts(+ driver, scope, stub_graphiti_client, [CAROL], session_id="s-kept"+ )++ result = await retract(scope, [edges[ALICE[2]]], hard=True)++ assert result.tombstoned_episodes == [forgotten]+ assert await hidden_session_ids(driver, scope.group_id) == {"s-forgotten"}+ store = _chat_store("s-forgotten", "s-kept")+ mocker.patch.object(fetch, "chat_db", return_value=store)+ bundle = await fetch.gather_dream_input(scope)+ assert [session.session_id for session in bundle.recent_sessions] == ["s-kept"]+ read = store.get_chat_messages_paginated.await_args_list+ assert [call.kwargs["session_id"] for call in read] == ["s-kept"]+ prompt = str(build_consolidate_prompt(bundle))+ assert CAROL[2] in prompt and ALICE[2] not in prompt++[email protected]("step", list(_STEPS))+async def test_a_failed_hard_forget_finishes_when_repeated(+ scope_graph, stub_graphiti_client, step: str+) -> None:+ driver, scope = scope_graph+ episode, edges = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE], session_id="s-forgotten"+ )+ alice = edges[ALICE[2]]+ original = AutoGPTFalkorDriver.execute_query++ async def lose_one_step(self, cypher_query_, **params):+ if cypher_query_ == _STEPS[step]:+ raise RuntimeError(f"{step} lost")+ return await original(self, cypher_query_, **params)++ with patch.object(AutoGPTFalkorDriver, "execute_query", lose_one_step):+ failed = await retract(scope, [alice], hard=True)++ assert failed.deleted == []+ assert [(f.uuid, f.code) for f in failed.failures] == [+ (alice, MemoryForgetFailureCode.CLEANUP_ERROR)+ ]+ assert await edge_row(driver, alice) != {}, "kept, so a retry finds it"+ assert alice not in await recalled_facts(scope)+ assert episode not in await recalled_episodes(scope)++ retry = await retract(scope, [alice], hard=True)++ assert (retry.deleted, retry.failures) == ([alice], [])+ assert await edge_row(driver, alice) == {}+ tombstone = await episode_row(driver, episode)+ assert (tombstone["content"], tombstone["entity_edges"]) == ("", [])+ assert tombstone["hard_deleted_at"] is not None+ assert await hidden_session_ids(driver, scope.group_id) == {"s-forgotten"}+ assert await _dangling(driver) == []+ assert await count(driver, "MATCH (n:Entity) RETURN count(n) AS c") == 0+++async def test_the_admin_audit_views_keep_what_a_forget_hid(+ scope_graph, stub_graphiti_client, mocker+) -> None:+ """The facts list and the graph view show a forgotten fact's own text and+ a tombstone's stamp, never its text."""+ driver, scope = scope_graph+ kept_episode, kept = await ingest_facts(+ driver, scope, stub_graphiti_client, [CAROL], session_id="s-soft"+ )+ gone_episode, gone = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE], session_id="s-hard"+ )+ await retract(scope, [kept[CAROL[2]]])+ await retract(scope, [gone[ALICE[2]]], hard=True)++ facts, graph = await _admin_views(mocker, scope)++ [fact] = facts.items+ assert (fact.uuid, fact.status) == (kept[CAROL[2]], "retracted")+ assert (fact.name, fact.fact) == ("MemoryFact", CAROL[2])+ episodes = {node.uuid: node for node in graph.nodes if node.label == "Episodic"}+ assert episodes[gone_episode].hard_deleted_at is not None+ assert episodes[kept_episode].hard_deleted_at is None+ [edge] = [edge for edge in graph.edges if edge.label == "RELATES_TO"]+ assert edge.fact == CAROL[2]+++async def test_a_soft_forget_can_still_be_made_permanent_from_chat(+ scope_graph, stub_graphiti_client, mocker: MockerFixture+) -> None:+ """``memory_forget_search`` offers a softly forgotten fact again only when+ asked for forgotten ones, by its uuid and dates and never its sentence,+ and the hard delete that follows removes the edge."""+ driver, scope = scope_graph+ _, edges = await ingest_facts(driver, scope, stub_graphiti_client, [ALICE, BOB])+ alice, bob = edges[ALICE[2]], edges[BOB[2]]+ await retract(scope, [alice])+ assert (await edge_row(driver, alice))["fact"] == FORGOTTEN_FACT+ enabled = AsyncMock(return_value=True)+ mocker.patch.object(graphiti_forget, "is_enabled_for_user", enabled)+ session = ChatSession.new(scope.owner_user_id, dry_run=False)+ search = graphiti_forget.MemoryForgetSearchTool()++ found = await search._execute(scope.owner_user_id, session, query="Atlas")+ assert isinstance(found, MemoryForgetCandidatesResponse)+ assert [c["uuid"] for c in found.candidates] == [bob]++ found = await search._execute(+ scope.owner_user_id, session, query="Atlas", include_forgotten=True+ )+ assert isinstance(found, MemoryForgetCandidatesResponse)+ offered = [(c["uuid"], c["fact"], c.get("forgotten")) for c in found.candidates]+ assert offered == [(bob, BOB[2], None), (alice, FORGOTTEN_FACT, "true")]+ assert ALICE[2] not in found.model_dump_json()++ confirmed = await graphiti_forget.MemoryForgetConfirmTool()._execute(+ scope.owner_user_id, session, uuids=[alice], hard_delete=True+ )++ assert isinstance(confirmed, MemoryForgetConfirmResponse)+ assert confirmed.deleted_uuids == [alice] and confirmed.failures == []+ assert await edge_row(driver, alice) == {}+ assert (await edge_row(driver, bob))["fact"] == BOB[2]diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_hide.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_hide.pynew file mode 100644index 0000000000..2f72e8ff05--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_hide.py@@ -0,0 +1,163 @@+"""What a forget hides beyond the edge: the fact's sentence, wherever graphiti+would read it back, and the episodes it came from.++The edge's ``fact`` and ``name`` move to ``fact_redacted`` /+``name_redacted`` for audit and both read ``recall.FORGOTTEN_FACT``:+graphiti offers every edge, forgotten ones included, as a duplicate or+contradiction candidate in its own prompts. The ``name`` goes too because+graphiti picks an edge's attribute prompt by it, and that prompt lists+every stored property of the edge, ``fact_redacted`` included; no edge type+is named ``[forgotten]``, so the prompt never runs for a forgotten edge. An+audit copy, once written, is never replaced by the placeholder.++graphiti also keeps what it read out of the sentence on entities: a summary+built from fact sentences, and typed attributes (``Person.role``) it sends+to its attribute, summary and entity resolution prompts. So every entity+the fact joins, and every entity an episode citing it mentions, loses its+summary and every property but its identity (``_CORE_ENTITY_FIELDS``), and+so does the summary of every community one of them belongs to. They are+blanked, not rewritten: graphiti extracts them again from the next episode+that mentions the entity, and the weekly rebuild restores the communities.+Every episode citing the fact is stamped ``redacted_at``.++Each write is idempotent, so forgetting again finishes a forget whose+clean-up failed. The legacy-forget backfill hides through ``scrub`` too.+"""++import logging+from typing import Any++from graphiti_core.driver.driver import GraphDriver++from .memory_model import ForgetResult, MemoryForgetFailure+from .recall import FORGOTTEN_FACT, forgotten_facts_clause, recallable_episode_predicate++logger = logging.getLogger(__name__)++# What an entity keeps when a forget scrubs it: its identity. Everything+# else graphiti stored on it (typed attributes, an ``attributes`` map) goes.+_CORE_ENTITY_FIELDS = frozenset(+ {"uuid", "name", "group_id", "labels", "created_at", "name_embedding", "summary"}+)+++async def hide(+ driver: GraphDriver,+ group_id: str,+ uuids: list[str],+ now: str,+ result: ForgetResult,+) -> bool:+ """Scrub the retracted facts' text, then redact every episode citing+ one; False when a write failed, each edge then carrying a+ ``cleanup_error`` (recall hides the text regardless)."""+ if not uuids:+ return True+ try:+ await scrub(driver, uuids)+ records = await driver.execute_query(+ REDACT_EPISODES_QUERY, uuids=uuids, now=now+ )+ except Exception as exc:+ logger.warning(+ f"Edges retracted but hiding their text failed in graph {group_id[:20]}",+ exc_info=True,+ )+ result.failures.extend(+ MemoryForgetFailure.cleanup_error(uuid, exc) for uuid in uuids+ )+ return False+ result.redacted_episodes = [row["uuid"] for row in _rows(records)]+ return True+++async def scrub(driver: GraphDriver, uuids: list[str]) -> None:+ """Move the facts' text to their audit copies, then clear what graphiti+ read out of it onto entities and communities."""+ rows = _rows(+ await driver.execute_query(+ SCRUB_FACTS_QUERY, uuids=uuids, placeholder=FORGOTTEN_FACT+ )+ )+ await scrub_entities(driver, uuids, rows[0]["ends"] if rows else [])+++async def scrub_entities(+ driver: GraphDriver, uuids: list[str], entities: list[str]+) -> None:+ """Clear the summary and attributes of ``entities`` and of every entity+ an episode citing one of ``uuids`` mentions, and blank their+ communities' summaries. A read picks the properties to drop, since+ Cypher cannot name them from data."""+ found = _rows(+ await driver.execute_query(_ENTITY_KEYS_QUERY, uuids=uuids, entities=entities)+ )+ cleared = [+ {+ "uuid": row["uuid"],+ "cleared": {k: None for k in row["keys"] if k not in _CORE_ENTITY_FIELDS},+ }+ for row in found+ ]+ if cleared:+ await driver.execute_query(_SCRUB_ENTITIES_QUERY, entities=cleared)+++def _rows(result: Any) -> list[dict[str, Any]]:+ return result[0] if result else []+++# ``sentence`` and ``relation`` are the audit copy already written, else the+# edge's own text unless it already reads the placeholder: a repeat changes+# nothing, and never files the placeholder as the original.+SCRUB_FACTS_QUERY = """+MATCH (source)-[e:RELATES_TO]->(target)+WHERE e.uuid IN $uuids+WITH e, source, target,+ coalesce(e.fact_redacted,+ CASE WHEN e.fact <> $placeholder THEN e.fact END) AS sentence,+ coalesce(e.name_redacted,+ CASE WHEN e.name <> $placeholder THEN e.name END) AS relation+SET e.fact_redacted = sentence,+ e.name_redacted = relation,+ e.fact = $placeholder,+ e.name = $placeholder+RETURN collect(DISTINCT source.uuid) + collect(DISTINCT target.uuid) AS ends+"""++# The endpoints passed in, plus every entity an episode citing a forgotten+# fact mentions (the episodes the redaction below hides).+_ENTITY_KEYS_QUERY = """+OPTIONAL MATCH (ep:Episodic)-[:MENTIONS]->(mentioned:Entity)+WHERE any(x IN coalesce(ep.entity_edges, []) WHERE x IN $uuids)+WITH collect(DISTINCT mentioned.uuid) + $entities AS targets+MATCH (n:Entity)+WHERE n.uuid IN targets+RETURN n.uuid AS uuid, keys(n) AS keys+"""++# ``+=`` with a null value removes that property. A community is matched+# through its ``HAS_MEMBER`` edge to a scrubbed entity.+_SCRUB_ENTITIES_QUERY = """+UNWIND $entities AS entity+MATCH (n:Entity {uuid: entity.uuid})+SET n += entity.cleared, n.summary = ''+WITH collect(n) AS scrubbed+OPTIONAL MATCH (c:Community)-[:HAS_MEMBER]->(member)+WHERE member IN scrubbed+SET c.summary = ''+"""++# Every episode naming one of ``$uuids`` that the recall policy now hides,+# which after the retraction is all of them: the stamp and the read-side test+# cannot disagree. ``coalesce`` keeps the time of the first redaction.+REDACT_EPISODES_QUERY = (+ forgotten_facts_clause()+ + f"""+MATCH (ep:Episodic)+WHERE any(x IN coalesce(ep.entity_edges, []) WHERE x IN $uuids)+ AND NOT ({recallable_episode_predicate("ep")})+SET ep.redacted_at = coalesce(ep.redacted_at, $now)+RETURN ep.uuid AS uuid+"""+)diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_hide_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_hide_test.pynew file mode 100644index 0000000000..19230db11b--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_hide_test.py@@ -0,0 +1,157 @@+"""Unit tests for ``recall_hide``: what a forget scrubs and redacts beyond+the edge, against a mock driver.++The live runs are ``recall_forget_integration_test.py`` (the episodes) and+``recall_scrub_integration_test.py`` / ``ingest_recall_integration_test.py``+(graphiti's own prompts).+"""++from unittest.mock import AsyncMock++import pytest++from . import recall_hide+from .memory_model import ForgetResult, MemoryForgetFailureCode+from .recall import FORGOTTEN_FACT, forgotten_facts_clause, recallable_episode_predicate++_GROUP = "user_abc"+_NOW = "2026-09-26T12:00:00+00:00"+_ALICE_KEYS = [+ "uuid",+ "name",+ "group_id",+ "summary",+ "created_at",+ "role",+ "attributes",+]+++def _driver(*results) -> AsyncMock:+ driver = AsyncMock()+ driver.execute_query.side_effect = [+ r if isinstance(r, Exception) else (r, [], None) for r in results+ ]+ return driver+++class TestHide:+ @pytest.mark.asyncio+ async def test_scrubs_facts_then_entities_then_redacts(self) -> None:+ driver = _driver(+ [{"ends": ["alice", "atlas"]}],+ [{"uuid": "alice", "keys": _ALICE_KEYS}],+ [],+ [{"uuid": "ep1"}],+ )+ result = ForgetResult()++ assert await recall_hide.hide(driver, _GROUP, ["u1"], _NOW, result)++ assert result.redacted_episodes == ["ep1"] and result.failures == []+ facts, keys, entities, redact = driver.execute_query.await_args_list+ assert facts.args == (recall_hide.SCRUB_FACTS_QUERY,)+ assert facts.kwargs == {"uuids": ["u1"], "placeholder": FORGOTTEN_FACT}+ assert keys.kwargs == {"uuids": ["u1"], "entities": ["alice", "atlas"]}+ assert entities.kwargs == {+ "entities": [+ {"uuid": "alice", "cleared": {"role": None, "attributes": None}}+ ]+ }+ assert redact.args == (recall_hide.REDACT_EPISODES_QUERY,)+ assert redact.kwargs == {"uuids": ["u1"], "now": _NOW}++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "failing", [0, 1, 2], ids=["facts", "entities", "redaction"]+ )+ async def test_a_failed_write_is_each_edges_cleanup_error(+ self, failing: int+ ) -> None:+ results: list = [[{"ends": []}], [], []]+ results[failing] = RuntimeError("down")+ driver = _driver(*results)+ result = ForgetResult()++ hidden = await recall_hide.hide(driver, _GROUP, ["u1", "u2"], _NOW, result)++ assert hidden is False+ assert [(f.uuid, f.code) for f in result.failures] == [+ ("u1", MemoryForgetFailureCode.CLEANUP_ERROR),+ ("u2", MemoryForgetFailureCode.CLEANUP_ERROR),+ ]+ assert "Forgetting it again is safe" in result.failures[0].reason++ @pytest.mark.asyncio+ async def test_nothing_retracted_means_nothing_to_hide(self) -> None:+ driver = _driver()++ assert await recall_hide.hide(driver, _GROUP, [], _NOW, ForgetResult())+ driver.execute_query.assert_not_awaited()+++class TestScrubQuery:+ def test_an_audit_copy_is_never_replaced_by_the_placeholder(self) -> None:+ """The audit copy already written wins, then the edge's own text+ unless it already reads the placeholder."""+ query = recall_hide.SCRUB_FACTS_QUERY+ assert (+ "coalesce(e.fact_redacted,\n"+ " CASE WHEN e.fact <> $placeholder THEN e.fact END) AS sentence"+ in query+ )+ assert "CASE WHEN e.name <> $placeholder THEN e.name END" in query+ assert "SET e.fact_redacted = sentence," in query+ assert "e.fact = $placeholder," in query+ assert "e.name = $placeholder" in query++ def test_writes_no_status_or_marker(self) -> None:+ """The retraction owns those; the scrub only moves text."""+ for query in (recall_hide.SCRUB_FACTS_QUERY, recall_hide._SCRUB_ENTITIES_QUERY):+ for field in ("status", "forgotten_at", "expired_at", "invalid_at"):+ assert field not in query+++class TestEntityScrub:+ def test_reaches_every_entity_a_hidden_episode_mentions(self) -> None:+ query = recall_hide._ENTITY_KEYS_QUERY+ assert "(ep:Episodic)-[:MENTIONS]->(mentioned:Entity)" in query+ assert "any(x IN coalesce(ep.entity_edges, []) WHERE x IN $uuids)" in query+ assert "collect(DISTINCT mentioned.uuid) + $entities AS targets" in query+ assert "keys(n) AS keys" in query++ def test_clears_attributes_and_summaries_and_their_communities(self) -> None:+ query = recall_hide._SCRUB_ENTITIES_QUERY+ assert "SET n += entity.cleared, n.summary = ''" in query+ assert "OPTIONAL MATCH (c:Community)-[:HAS_MEMBER]->(member)" in query+ assert "SET c.summary = ''" in query++ @pytest.mark.asyncio+ async def test_an_entity_keeps_only_its_identity(self) -> None:+ keys = [*_ALICE_KEYS, "labels", "name_embedding", "email"]+ driver = _driver([{"uuid": "alice", "keys": keys}], [])++ await recall_hide.scrub_entities(driver, ["u1"], [])++ cleared = driver.execute_query.await_args_list[1].kwargs["entities"][0]+ assert cleared["cleared"] == {"role": None, "attributes": None, "email": None}++ @pytest.mark.asyncio+ async def test_no_entity_found_writes_nothing(self) -> None:+ driver = _driver([])++ await recall_hide.scrub_entities(driver, ["u1"], [])++ assert driver.execute_query.await_count == 1+++class TestRedactQuery:+ def test_redacts_every_episode_the_policy_now_hides(self) -> None:+ """Any episode naming a forgotten fact, not only one left with no+ live fact: the redaction and the read side share one predicate."""+ query = recall_hide.REDACT_EPISODES_QUERY+ assert query.startswith(forgotten_facts_clause())+ assert "any(x IN coalesce(ep.entity_edges, []) WHERE x IN $uuids)" in query+ assert f"NOT ({recallable_episode_predicate('ep')})" in query+ assert "SET ep.redacted_at = coalesce(ep.redacted_at, $now)" in query+ assert "content" not in query, "the text stays for audit"diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_inflight_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_inflight_integration_test.pynew file mode 100644index 0000000000..1bc46240cb--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_inflight_integration_test.py@@ -0,0 +1,285 @@+"""A forget and an ingestion at the same time, through the production worker+against a live FalkorDB whose own Redis holds the graph's write lock.++graphiti's ``add_episode`` saves the edges and entities it read when it+began (``SET r = edge``, ``SET n = node``), so a forget landing mid-episode+used to be written over. Now the two never overlap (``scope_lock.py``): a+forget that arrives while an episode is being written waits for it, then+applies to what it wrote, and search never shows the forgotten sentence once+the forget has answered (``scope_lock_integration_test.py`` covers a lock+held past a writer's wait). The legacy-forget backfill takes the same lock,+so it too waits for an episode being written. Reproduced first by an+independent validation (``r3-ingestion-independent.py``, ``mid_add_race``;+``r4-stash-probes.py``, ``healthy_redis_pre_repair_read``;+``r6-migration-concurrency.py``, the backfill).++Run with FalkorDB reachable (see ``conftest.py``)::++ poetry run pytest -m integration backend/copilot/graphiti/recall_inflight_integration_test.py+"""++import asyncio+from collections.abc import AsyncIterator, Awaitable, Callable, Iterator+from contextlib import contextmanager+from typing import Any+from unittest.mock import AsyncMock, patch++import pytest+import pytest_asyncio+from graphiti_core.driver.falkordb_driver import FalkorDriverSession+from pytest_mock import MockerFixture+from redis.asyncio import Redis++from backend.copilot.dream.hidden_sessions import hidden_session_ids++from . import recall, scope_lock+from .config import graphiti_config+from .falkordb_driver import AutoGPTFalkorDriver+from .memory_model import ForgetResult+from .migrations.backfill_legacy_forgets import LegacyForgets, backfill_graph+from .recall import FORGOTTEN_FACT+from .recall_forget import retract+from .recall_integration_fixtures import (+ ALICE,+ BOB,+ BuildClient,+ count,+ edge_row,+ ingest_through_the_worker,+ live_facts,+ patch_recall_boundaries,+ recalled_episodes,+ rows,+ scripted_responses,+ sentence_properties,+ stop_ingestion_workers,+)+from .scope import MemoryScope++# A forget, or the legacy-forget backfill's restamp.+Writer = Callable[[], Awaitable[ForgetResult | LegacyForgets]]++[email protected](autouse=True)+def boundaries(mocker, scope_graph, stub_graphiti_client):+ patch_recall_boundaries(mocker, scope_graph[0], stub_graphiti_client)+++@pytest_asyncio.fixture(loop_scope="function")+async def ingest_worker_cleanup(mocker: MockerFixture) -> AsyncIterator[None]:+ mocker.patch(+ "backend.copilot.graphiti.ingest.ensure_scope_scheduled",+ AsyncMock(return_value=None),+ )+ yield+ await stop_ingestion_workers()+++async def _learn(driver: AutoGPTFalkorDriver, scope: MemoryScope, build: BuildClient):+ client = build(driver, scripted_responses([ALICE]))+ await ingest_through_the_worker(driver, scope, client, [ALICE], session_id="s-1")+ [alice] = await live_facts(driver)+ return alice+++async def _say(driver: AutoGPTFalkorDriver, scope: MemoryScope, build: BuildClient):+ client = build(driver, scripted_responses([ALICE]))+ await ingest_through_the_worker(driver, scope, client, [ALICE], session_id="s-2")+++@contextmanager+def _forget_arrives_at(+ marker: str, forget: Writer, answered: asyncio.Event | None = None+) -> Iterator[tuple[list[asyncio.Future], list[bool]]]:+ """Start ``forget`` right before graphiti's first query containing+ ``marker``, note whether it is still waiting for the lock 0.6 s later,+ then let the query go (a failed check here would abort graphiti's save+ and hide the overwrite it is meant to catch)."""+ original = FalkorDriverSession.run+ started: list[asyncio.Future] = []+ waited: list[bool] = []++ async def run(session: FalkorDriverSession, query: Any, **params: Any) -> Any:+ text = query if isinstance(query, str) else " ".join(q for q, _ in query)+ if not started and marker in text:+ started.append(asyncio.ensure_future(forget()))+ if answered is not None:+ event = answered+ started[0].add_done_callback(lambda _: event.set())+ await asyncio.sleep(0.6)+ waited.append(not started[0].done())+ return await original(session, query, **params)++ with patch.object(FalkorDriverSession, "run", run):+ yield started, waited+++async def _watch(scope: MemoryScope, answered: asyncio.Event, seen: list[bool]):+ """Search over and over; after the forget answered, note each time+ whether the sentence came back."""+ while True:+ after = answered.is_set()+ facts = await recall.search_facts(scope, "Alice Atlas", limit=10)+ if after:+ seen.append(any(ALICE[2] in fact.fact for fact in facts))+ await asyncio.sleep(0.05)+++async def _searched(seen: list[bool], times: int) -> None:+ """Wait until the watcher has searched ``times`` times since the+ forget answered."""+ async with scope_lock.timeout(20):+ while len(seen) < times:+ await asyncio.sleep(0.05)+++async def _assert_forgotten(driver: AutoGPTFalkorDriver, scope: MemoryScope, alice):+ row = await edge_row(driver, alice)+ assert row["forgotten_at"] is not None+ assert (row["status"], row["reason"]) == ("retracted", "user_signal")+ assert (row["fact"], row["fact_redacted"]) == (FORGOTTEN_FACT, ALICE[2])+ assert ALICE[2] not in (await live_facts(driver)).values()+ said = await rows(+ driver,+ "MATCH (ep:Episodic) WHERE ep.name IN $names RETURN ep.uuid AS uuid",+ names=["conversation_s-1", "conversation_s-2"],+ )+ assert len(said) == 2+ hidden = {row["uuid"] for row in said}+ assert not hidden & await recalled_episodes(scope), "both episodes hidden"+ assert await hidden_session_ids(driver, scope.group_id) == {"s-1", "s-2"}+ assert await sentence_properties(driver, ALICE[2]) == {+ "Episodic.content",+ "RELATES_TO.fact_redacted",+ }++[email protected]("marker", ["SET r = edge", "SET n = node"])+async def test_a_forget_arriving_mid_ingestion_waits_then_applies(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup, marker: str+) -> None:+ """Before the forget answers it has not been applied; from the moment+ it answers, through a later ingestion, search never shows the sentence."""+ driver, scope = scope_graph+ alice = await _learn(driver, scope, stub_graphiti_client)+ answered, seen = asyncio.Event(), []+ watcher = asyncio.ensure_future(_watch(scope, answered, seen))++ forget = _forget_arrives_at(marker, lambda: retract(scope, [alice]), answered)+ with forget as (started, waited):+ await _say(driver, scope, stub_graphiti_client)+ result = await asyncio.wait_for(started[0], 30)+ await _searched(seen, 3)+ later = stub_graphiti_client(driver, scripted_responses([BOB]))+ await ingest_through_the_worker(driver, scope, later, [BOB], session_id="s-3")+ await _searched(seen, len(seen) + 3)+ watcher.cancel()++ assert (result.deleted, result.failures) == ([alice], [])+ assert not any(seen), "search showed the sentence after the forget"+ await _assert_forgotten(driver, scope, alice)+ assert waited == [True], "the forget did not wait for the ingestion"+++async def test_a_hard_forget_arriving_mid_ingestion_deletes_what_it_wrote(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup+) -> None:+ driver, scope = scope_graph+ alice = await _learn(driver, scope, stub_graphiti_client)++ async def forget() -> ForgetResult:+ return await retract(scope, [alice], hard=True)++ with _forget_arrives_at("SET r = edge", forget) as (started, waited):+ await _say(driver, scope, stub_graphiti_client)+ result = await asyncio.wait_for(started[0], 30)++ assert (result.deleted, result.failures) == ([alice], [])+ assert await count(driver, "MATCH ()-[e:RELATES_TO]->() RETURN count(e) AS c") == 0+ assert await recalled_episodes(scope) == set()+ assert await hidden_session_ids(driver, scope.group_id) == {"s-1", "s-2"}+ assert waited == [True], "the forget did not wait for the ingestion"+++async def test_a_forget_on_another_event_loop_waits_for_the_ingestion(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup, mocker+) -> None:+ """The forget runs on another thread's event loop with its own Redis+ connection, sharing only Redis and the graph, as a second process would+ (not a process: an independent validation ran that, ``r5-lock-probes.py``)."""+ driver, scope = scope_graph+ clients: dict[asyncio.AbstractEventLoop, Redis] = {}++ async def per_loop() -> Redis:+ return clients.setdefault(asyncio.get_running_loop(), _server_redis())++ mocker.patch.object(scope_lock, "get_redis_async", per_loop)+ alice = await _learn(driver, scope, stub_graphiti_client)++ async def elsewhere() -> ForgetResult:+ try:+ return await retract(scope, [alice])+ finally:+ await _close(clients.pop(asyncio.get_running_loop(), None))++ def forget() -> Awaitable[ForgetResult]:+ return asyncio.to_thread(asyncio.run, elsewhere())++ with _forget_arrives_at("SET r = edge", forget) as (started, waited):+ await _say(driver, scope, stub_graphiti_client)+ result = await asyncio.wait_for(started[0], 30)+ await _close(clients.pop(asyncio.get_running_loop(), None))++ assert (result.deleted, result.failures) == ([alice], [])+ await _assert_forgotten(driver, scope, alice)+ assert waited == [True], "the forget did not wait for the ingestion"+++async def test_the_legacy_forget_backfill_waits_for_the_ingestion(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup+) -> None:+ """A forget from before the recall policy (``expired_at`` and nothing+ else) is restamped while an episode stating the fact again is being+ saved: the backfill waits for the write lock, then restamps what the+ episode wrote, so nothing writes over its scrub."""+ driver, scope = scope_graph+ alice = await _learn(driver, scope, stub_graphiti_client)+ await driver.execute_query(+ "MATCH ()-[e:RELATES_TO {uuid: $uuid}]->() SET e.expired_at = $then"+ " REMOVE e.invalid_at, e.expiration_reason, e.forgotten_at",+ uuid=alice,+ then="2026-01-01T00:00:00+00:00",+ )++ async def backfill() -> LegacyForgets:+ return await backfill_graph(driver, apply=True)++ with _forget_arrives_at("SET r = edge", backfill) as (started, waited):+ await _say(driver, scope, stub_graphiti_client)+ result = await asyncio.wait_for(started[0], 30)++ assert (result.edges, result.busy) == (1, 0)+ await _assert_forgotten(driver, scope, alice)+ assert waited == [True], "the backfill did not wait for the ingestion"+++async def _close(redis: Redis | None) -> None:+ if redis is not None:+ await redis.aclose()+++def _server_redis() -> Redis:+ return Redis(+ host=graphiti_config.falkordb_host,+ port=graphiti_config.falkordb_port,+ password=graphiti_config.falkordb_password or None,+ decode_responses=True,+ )diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_inflight_read_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_inflight_read_integration_test.pynew file mode 100644index 0000000000..4ed7b27b79--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_inflight_read_integration_test.py@@ -0,0 +1,225 @@+"""A read already under way when a forget answers, on a live FalkorDB.++Warm context is paused at graphiti's cross-encoder, after its edge search+has read the fact and its episode read has returned; ``memory_search`` is+paused as graphiti's search returns. A forget answers meanwhile. When the+read resumes, its last graph read before rendering (``recall.live_now``,+``recall_recheck.recheck``) no longer finds the fact or its episode, so the+response shows neither, while a live fact and its episode still show.+Reproduced first by an independent validation+(``r5-quality-delayed-operations.py``, ``stale-warm-crossencoder`` and+``stale-memory-search``). A forget that answers once warm context's fact+check has passed is caught too: the recheck checks facts and episodes again+in one statement, so no forget lands between two halves of it+(``r6-read-split-check.py``).++Run with FalkorDB reachable (see ``conftest.py``)::++ poetry run pytest -m integration backend/copilot/graphiti/recall_inflight_read_integration_test.py+"""++import asyncio+from collections.abc import Callable, Coroutine+from typing import Any+from unittest.mock import patch++import pytest+from graphiti_core.driver.driver import GraphDriver+from graphiti_core.edges import EntityEdge+from graphiti_core.nodes import EpisodicNode++from backend.copilot.model import ChatSession+from backend.copilot.tools import graphiti_forget+from backend.copilot.tools.graphiti_search import MemorySearchTool+from backend.copilot.tools.models import MemoryForgetCandidatesResponse++from . import context, recall+from .falkordb_driver import AutoGPTFalkorDriver+from .recall_forget import retract+from .recall_integration_fixtures import (+ ALICE,+ BOB,+ BuildClient,+ capture_spawned_tasks,+ ingest_facts,+ patch_recall_boundaries,+)+from .scope import MemoryScope++[email protected](autouse=True)+def boundaries(mocker, scope_graph, stub_graphiti_client):+ patch_recall_boundaries(mocker, scope_graph[0], stub_graphiti_client)+ mocker.patch.object(context, "_spawn_ratification_hits")+ capture_spawned_tasks(mocker)+++async def _alice_and_bob(+ driver: AutoGPTFalkorDriver, scope: MemoryScope, build: BuildClient+) -> str:+ """Alice's and Bob's facts, each from its own chat turn: Alice's uuid."""+ _, alice = await ingest_facts(driver, scope, build, [ALICE], session_id="s-1")+ await ingest_facts(driver, scope, build, [BOB], session_id="s-2")+ return alice[ALICE[2]]+++async def _forget_while_paused(+ scope: MemoryScope,+ alice: str,+ read: Callable[[], Coroutine[Any, Any, Any]],+ pause: Callable[[asyncio.Event, asyncio.Event], Any],+) -> Any:+ """Start ``read``, forget Alice once it reaches ``pause``, then let it+ finish: what it returns."""+ reached, release = asyncio.Event(), asyncio.Event()+ with pause(reached, release):+ task = asyncio.create_task(read())+ await asyncio.wait_for(reached.wait(), 20)+ forgot = await retract(scope, [alice])+ release.set()+ returned = await asyncio.wait_for(task, 20)+ assert (forgot.deleted, forgot.failures) == ([alice], [])+ return returned+++async def test_warm_context_paused_at_its_reranker_shows_nothing_forgotten(+ scope_graph, stub_graphiti_client+) -> None:+ driver, scope = scope_graph+ alice = await _alice_and_bob(driver, scope, stub_graphiti_client)+ client = await recall.get_graphiti_client(scope.group_id)+ rank, recent = client.cross_encoder.rank, context.recent_episodes+ reranked: list[str] = []+ read_back: list[EpisodicNode] = []++ async def episodes(scope: MemoryScope, n: int) -> list[EpisodicNode]:+ read_back.extend(await recent(scope, n))+ return read_back++ def pause(reached: asyncio.Event, release: asyncio.Event):+ async def paused(query: str, passages: list[str]) -> list[tuple[str, float]]:+ reranked.extend(passages)+ reached.set()+ await release.wait()+ return await rank(query, passages)++ return patch.object(client.cross_encoder, "rank", paused)++ with patch.object(context, "recent_episodes", episodes):+ shown = await _forget_while_paused(+ scope, alice, lambda: context._fetch(scope, "Alice Bob Atlas"), pause+ )++ assert ALICE[2] in reranked, "the search read the fact before the forget"+ assert any(ALICE[2] in (ep.content or "") for ep in read_back)+ assert shown is not None and ALICE[2] not in shown+ assert BOB[2] in shown.split("<RECENT_EPISODES>")[0], "the live fact"+ assert BOB[2] in shown.split("<RECENT_EPISODES>")[1], "its episode"+++async def test_warm_context_after_its_fact_check_passed_shows_nothing_forgotten(+ scope_graph, stub_graphiti_client+) -> None:+ """The fact check has kept Alice's fact, a forget answers, then the rest+ of the read runs: its one last statement finds neither her fact nor her+ episode."""+ driver, scope = scope_graph+ alice = await _alice_and_bob(driver, scope, stub_graphiti_client)+ live_now = recall.live_now+ passed: list[str] = []++ def pause(reached: asyncio.Event, release: asyncio.Event):+ async def checked(+ driver: GraphDriver, facts: list[EntityEdge], **kwargs: Any+ ) -> list[EntityEdge]:+ kept = await live_now(driver, facts, **kwargs)+ passed.extend(fact.fact for fact in kept)+ reached.set()+ await release.wait()+ return kept++ return patch.object(recall, "live_now", checked)++ shown = await _forget_while_paused(+ scope, alice, lambda: context._fetch(scope, "Alice Bob Atlas"), pause+ )++ assert ALICE[2] in passed, "the fact check passed before the forget"+ assert shown is not None and ALICE[2] not in shown+ assert BOB[2] in shown.split("<RECENT_EPISODES>")[0], "the live fact"+ assert BOB[2] in shown.split("<RECENT_EPISODES>")[1], "its episode"+++async def test_memory_search_paused_as_its_search_returns_shows_nothing_forgotten(+ scope_graph, stub_graphiti_client+) -> None:+ driver, scope = scope_graph+ alice = await _alice_and_bob(driver, scope, stub_graphiti_client)+ client = await recall.get_graphiti_client(scope.group_id)+ search = client.search_+ searched: list[str] = []++ def pause(reached: asyncio.Event, release: asyncio.Event):+ async def paused(*args: Any, **kwargs: Any):+ results = await search(*args, **kwargs)+ searched.extend(edge.fact for edge in results.edges)+ reached.set()+ await release.wait()+ return results++ return patch.object(client, "search_", paused)++ async def read():+ session = ChatSession.new(scope.owner_user_id, dry_run=False)+ tool = MemorySearchTool()+ return await tool._execute(scope.owner_user_id, session, query="Atlas")++ shown = (await _forget_while_paused(scope, alice, read, pause)).model_dump_json()++ assert ALICE[2] in searched, "the search read the fact before the forget"+ assert ALICE[2] not in shown+ assert BOB[2] in shown+++async def test_forget_search_rechecks_live_candidates_after_forgotten_search(+ scope_graph, stub_graphiti_client, mocker+) -> None:+ driver, scope = scope_graph+ alice = await _alice_and_bob(driver, scope, stub_graphiti_client)+ mocker.patch.object(+ graphiti_forget, "is_enabled_for_user", mocker.AsyncMock(return_value=True)+ )+ search = graphiti_forget.search_forgotten_facts++ def pause(reached: asyncio.Event, release: asyncio.Event):+ async def paused(*args: Any, **kwargs: Any):+ reached.set()+ await release.wait()+ return await search(*args, **kwargs)++ return patch.object(graphiti_forget, "search_forgotten_facts", paused)++ async def read():+ session = ChatSession.new(scope.owner_user_id, dry_run=False)+ return await graphiti_forget.MemoryForgetSearchTool()._execute(+ scope.owner_user_id, session, query="Atlas", include_forgotten=True+ )++ shown = await _forget_while_paused(scope, alice, read, pause)++ assert isinstance(shown, MemoryForgetCandidatesResponse)+ assert ALICE[2] not in shown.model_dump_json()+ assert BOB[2] in shown.model_dump_json()+ candidates = [+ candidate for candidate in shown.candidates if candidate["uuid"] == alice+ ]+ assert len(candidates) == 1+ assert candidates[0]["fact"] == recall.FORGOTTEN_FACTdiff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_ingest.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_ingest.pynew file mode 100644index 0000000000..184ecc2f2d--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_ingest.py@@ -0,0 +1,326 @@+"""How ingestion keeps graphiti from writing over a forget, and which+earlier episodes it shows graphiti's extraction (``previous_episode_uuids``).++Two things could write over a forget. An ingestion that read a fact before+a forget landed saves its older copy after it (graphiti saves what it read:+``SET r = edge``); the graph's write lock rules that out while Redis answers+and the holder keeps its lease, since a forget and an ingestion then never+overlap (``scope_lock.py``; ``graphiti/AGENTS.md`` has the windows with no+bound). And a later ingestion resolves each new statement against the edges+between the same entities, forgotten ones included: if its model calls the+statement a duplicate of a forgotten edge, graphiti appends the episode to+that edge and, because no edge type is named ``[forgotten]``, clears every+attribute on it, the forget's marker and audit copies among them+(``edge_operations.resolve_extracted_edge``, then its bulk save's+``SET r = edge``); if it calls it a contradiction, it stamps the forgotten+edge's ``invalid_at``.++So every graphiti client is built with ``ForgetAwareLLMClient``+(``client._build_graphiti``): whatever graphiti's model answers, an edge+whose text is ``recall.FORGOTTEN_FACT`` is neither a duplicate nor a+contradiction. graphiti then saves a restated fact as a new live edge in the+same ``add_episode``, as it would any new fact, and its model's dedup never+touches the forgotten one: no second extraction, and nothing to repair+afterwards. One path runs before the model and so past this guard:+graphiti's exact-text match reuses an edge whose text equals the new+statement's, lower-cased and whitespace-collapsed, so a statement that reads+``[forgotten]`` appends its episode's uuid to the forgotten edge's+``episodes``. The edge keeps its marker and audit copies and stays out of+recall, and the episode, citing it, is hidden.++The guard reads graphiti's prompt, so it fails closed. A candidate printed+with the placeholder as its fact or name is forgotten wherever it appears,+inside the two candidate lists or not, so renaming a tag cannot hide one.+And when the prompt shows the placeholder anywhere, both lists must be+there, once each, and parse, and every placeholder must belong to a+candidate the guard can read; otherwise the answer names no edge at all and+the statement is saved as new (a duplicate at worst, never a merge into a+forgotten edge). So does anything else that goes wrong while it reads the+prompt: the error is never let into graphiti's ``add_episode``. Every prompt+it cannot read in full is logged at error once per process for each shape+and reason, so a graphiti upgrade that changes the prompt is noticed even+before a forgotten edge shows up in it.++Its attribute answers keep only what their edge or entity type declares+(``types.extracted_attributes``), inside ``add_episode`` and so under the+graph's write lock: graphiti-core 0.30.2 saves every key its model answers,+so an invented ``forgotten_at`` would hide a live fact. A forget's marker and+audit copies are never extracted.+"""++import ast+import logging+import re+from datetime import datetime+from typing import Any++from graphiti_core.driver.driver import GraphDriver+from graphiti_core.llm_client.client import LLMClient+from graphiti_core.llm_client.config import DEFAULT_MAX_TOKENS, ModelSize+from graphiti_core.nodes import EpisodeType, EpisodicNode+from graphiti_core.prompts.dedupe_edges import EdgeDuplicate+from graphiti_core.prompts.models import Message+from graphiti_core.search.search_utils import RELEVANT_SCHEMA_LIMIT+from graphiti_core.tracer import Tracer+from pydantic import BaseModel, TypeAdapter, ValidationError++from .recall import FORGOTTEN_FACT, recallable_episodes+from .types import extracted_attributes++logger = logging.getLogger(__name__)++# The two candidate lists graphiti's edge dedup prompt shows+# (``prompts/dedupe_edges.resolve_edge``), each a Python list of+# ``{'idx': n, 'fact': ...}`` with one idx numbering across both.+_LISTS = ("EXISTING FACTS", "FACT INVALIDATION CANDIDATES")+_LIST = re.compile(+ r"<(EXISTING FACTS|FACT INVALIDATION CANDIDATES)>\s*(.*?)\s*</\1>", re.DOTALL+)+# One candidate as graphiti prints it, wherever it is: a flat dict literal.+_ENTRY = re.compile(r"\{[^{}]*\}")+# A prompt's tags, in order: its shape, for the once-per-shape error log.+_TAG = re.compile(r"</?([A-Z][A-Z _]*)>")+_REPORTED_SHAPES: set[tuple[str, ...]] = set()+# What ``ast.literal_eval`` and the validation raise on text they cannot read+# (pydantic's ``ValidationError`` is a ``ValueError``; named for the reader).+_UNREADABLE = (+ ValidationError,+ ValueError,+ TypeError,+ SyntaxError,+ MemoryError,+ RecursionError,+)+++class _Candidate(BaseModel):+ """One candidate edge as graphiti's dedup prompt prints it."""++ idx: int+ fact: str | None = None+ name: str | None = None++ @property+ def forgotten(self) -> bool:+ return FORGOTTEN_FACT in (self.fact, self.name)+++_CANDIDATE_LIST = TypeAdapter(list[_Candidate])+++class ForgetAwareLLMClient(LLMClient):+ """``inner``, except that a forgotten edge is never a duplicate or a+ contradiction in graphiti's edge dedup answers, and an attribute answer+ keeps only what its type declares."""++ def __init__(self, inner: LLMClient) -> None:+ super().__init__(inner.config)+ self.inner = inner+ self.token_tracker = inner.token_tracker++ def set_tracer(self, tracer: Tracer) -> None:+ super().set_tracer(tracer)+ self.inner.set_tracer(tracer)++ async def generate_response(+ self,+ messages: list[Message],+ response_model: type[BaseModel] | None = None,+ max_tokens: int | None = None,+ model_size: ModelSize = ModelSize.medium,+ group_id: str | None = None,+ prompt_name: str | None = None,+ *,+ attribute_extraction: bool = False,+ ) -> dict[str, Any]:+ forgotten = (+ _forgotten_or_none(messages) if response_model is EdgeDuplicate else set()+ )+ answer = await self.inner.generate_response(+ messages,+ response_model,+ max_tokens,+ model_size,+ group_id,+ prompt_name,+ attribute_extraction=attribute_extraction,+ )+ if attribute_extraction and response_model is not None:+ answer = extracted_attributes(answer, response_model)+ return answer if forgotten == set() else off_forgotten(answer, forgotten)++ async def _generate_response(+ self,+ messages: list[Message],+ response_model: type[BaseModel] | None = None,+ max_tokens: int = DEFAULT_MAX_TOKENS,+ model_size: ModelSize = ModelSize.medium,+ ) -> dict[str, Any]:+ """Unused: ``generate_response`` hands every call to ``inner``."""+ return await self.inner._generate_response(+ messages, response_model, max_tokens, model_size+ )+++async def previous_episode_uuids(+ driver: GraphDriver,+ group_id: str,+ reference_time: datetime,+ source: EpisodeType,+) -> list[str]:+ """The earlier episodes ``add_episode`` may show its extraction prompts.++ graphiti's own pick (``retrieve_episodes``: the ``RELEVANT_SCHEMA_LIMIT``+ newest of the same source up to ``reference_time``) cannot see a forget,+ so ingestion passes this one: the same pick of recallable episodes,+ oldest first. Never raises: on a failed read extraction gets no earlier+ episodes, not graphiti's unfiltered pick, and the write still happens.++ Inherited limitation: on FalkorDB 4.x the indexed ``valid_at <=+ $reference_time`` range can admit an episode dated just after the+ cut-off (a direct comparison of the same values says it should not),+ so, exactly like graphiti's own ``retrieve_episodes``, the newest+ episode here may postdate ``reference_time``. Not fixed here.+ """+ try:+ records = await recallable_episodes(+ driver, group_id, reference_time, RELEVANT_SCHEMA_LIMIT, source.value+ )+ except Exception:+ logger.warning(+ f"Prior-episode read failed for group {group_id[:12]}; "+ "extracting without earlier episodes",+ exc_info=True,+ )+ return []+ return [str(record["uuid"]) for record in reversed(records)]+++def _in_the_order_asked(+ episodes: list[EpisodicNode], uuids: list[str]+) -> list[EpisodicNode]:+ rank = {uuid: index for index, uuid in enumerate(uuids)}+ return sorted(episodes, key=lambda episode: rank.get(episode.uuid, len(rank)))+++def _patch_upstream_episode_order() -> None:+ """``add_episode`` reads the earlier episodes it was given with+ graphiti's ``EpisodicNode.get_by_uuids``, which returns them in whatever+ order the graph yields, so its extraction prompts could see a+ conversation out of order. Hand them back in the order asked for:+ oldest first from ``previous_episode_uuids``, as graphiti's own pick+ (``retrieve_episodes``) already is. graphiti's other callers choose+ their lists' order too."""+ if getattr(EpisodicNode.get_by_uuids, "_autogpt_ordered", False):+ return # idempotent+ upstream = EpisodicNode.get_by_uuids.__func__++ async def get_by_uuids(cls, driver: GraphDriver, uuids: list[str]):+ return _in_the_order_asked(await upstream(cls, driver, uuids), uuids)++ get_by_uuids._autogpt_ordered = True # type: ignore[attr-defined]+ setattr(EpisodicNode, "get_by_uuids", classmethod(get_by_uuids))+++_patch_upstream_episode_order()+++def _forgotten_or_none(messages: list[Message]) -> set[int] | None:+ """``forgotten_candidates``, failing closed on anything it did not+ expect: the answer then names no edge, the prompt is reported once per+ shape, and graphiti's ``add_episode`` goes on."""+ try:+ return forgotten_candidates(messages)+ except Exception as exc:+ text = "\n".join(str(message.content) for message in messages)+ _report_unreadable(text, f"reading it raised {type(exc).__name__}", exc)+ return None+++def forgotten_candidates(messages: list[Message]) -> set[int] | None:+ """The idx of every forgotten edge an edge dedup prompt shows; None, so+ that the answer names no edge, when the prompt shows the placeholder but+ cannot be read in full."""+ text = "\n".join(message.content for message in messages)+ listed = _listed(text)+ if listed is None:+ _report_unreadable(text, "its candidate lists cannot be read")+ if FORGOTTEN_FACT not in text:+ return set()+ shown = _shown(text)+ if shown is None:+ _report_unreadable(text, "it shows the placeholder outside a candidate")+ if listed is None or shown is None:+ return None+ return {c.idx for c in listed if c.forgotten} | shown+++def _listed(text: str) -> list[_Candidate] | None:+ """Every candidate in the two lists; None unless each list is there+ once and reads as candidates with a fact each."""+ lists = _LIST.findall(text)+ if sorted(tag for tag, _ in lists) != sorted(_LISTS):+ return None+ try:+ listed = [+ candidate+ for _, body in lists+ for candidate in _CANDIDATE_LIST.validate_python(ast.literal_eval(body))+ ]+ except _UNREADABLE:+ return None+ return None if any(c.fact is None for c in listed) else listed+++def _shown(text: str) -> set[int] | None:+ """The idx of every candidate anywhere in ``text``, in a list or not,+ whose fact or name is the placeholder; None when the placeholder also+ appears anywhere else."""+ entries = [entry for entry in _ENTRY.findall(text) if FORGOTTEN_FACT in entry]+ placed = sum(entry.count(FORGOTTEN_FACT) for entry in entries)+ if placed != text.count(FORGOTTEN_FACT):+ return None+ try:+ shown = [_Candidate.model_validate(ast.literal_eval(e)) for e in entries]+ except _UNREADABLE:+ return None+ return {c.idx for c in shown} if all(c.forgotten for c in shown) else None+++def _report_unreadable(text: str, why: str, error: Exception | None = None) -> None:+ """Log, at error and once per process for each reason and shape (the+ prompt's tags), a dedup prompt the guard cannot read in full, with+ ``error``'s traceback when there is one."""+ tags = _TAG.findall(text)+ shape = (why, *tags)+ if shape in _REPORTED_SHAPES:+ return+ _REPORTED_SHAPES.add(shape)+ logger.error(+ "graphiti's edge dedup prompt no longer reads as recall_ingest expects: "+ f"{why} (tags {tags}); while it does not, an answer that may name a "+ "forgotten edge names none, so restated facts are saved as new",+ exc_info=error,+ )+++def off_forgotten(answer: dict[str, Any], forgotten: set[int] | None) -> dict[str, Any]:+ """``answer`` naming no forgotten edge; naming no edge at all when which+ ones are forgotten could not be read, so the fact is saved as new."""+ try:+ decided = EdgeDuplicate.model_validate(answer)+ except ValidationError:+ return answer+ if forgotten is None:+ logger.warning("Edge dedup prompt unreadable; the new fact is kept as new")+ kept = EdgeDuplicate(+ duplicate_facts=_keep(decided.duplicate_facts, forgotten),+ contradicted_facts=_keep(decided.contradicted_facts, forgotten),+ )+ if kept != decided:+ logger.info("graphiti's model named a forgotten fact in edge dedup; ignored")+ return {**answer, **kept.model_dump()}+++def _keep(idxs: list[int], forgotten: set[int] | None) -> list[int]:+ return [i for i in idxs if forgotten is not None and i not in forgotten]diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_ingest_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_ingest_integration_test.pynew file mode 100644index 0000000000..e772197616--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_ingest_integration_test.py@@ -0,0 +1,193 @@+"""A forgotten fact stated again, through the production ingestion worker+against a live FalkorDB.++graphiti resolves a newly extracted fact against every edge between the same+entities, the forgotten one included. Before a forget scrubbed the edge's+sentence, its exact-text match merged the new statement into the retracted+edge: the user got no live fact and the new episode was hidden with the old+one. The scrub stops that match, and whatever graphiti's model answers,+a forgotten edge is neither a duplicate nor a contradiction of the new+fact (``recall_ingest.ForgetAwareLLMClient``): graphiti saves it as a new+live edge in the same ``add_episode``, with no second extraction. The unit+sibling is ``recall_ingest_test.py``; ``recall_reteach_integration_test.py``+states several facts at once.++Run with FalkorDB reachable (see ``conftest.py``)::++ poetry run pytest -m integration backend/copilot/graphiti/recall_ingest_integration_test.py+"""++from collections.abc import AsyncIterator+from typing import Any+from unittest.mock import AsyncMock, patch++import pytest+import pytest_asyncio+from pytest_mock import MockerFixture++from .falkordb_driver import AutoGPTFalkorDriver+from .recall import FORGOTTEN_FACT+from .recall_forget import retract+from .recall_integration_fixtures import (+ ALICE,+ BuildClient,+ Fact,+ edge_row,+ episode_row,+ ingest_through_the_worker,+ live_facts,+ model_of,+ patch_recall_boundaries,+ recalled_episodes,+ recalled_facts,+ scripted_responses,+ stop_ingestion_workers,+)+from .scope import MemoryScope++_REWORDED: Fact = ("Alice", "Atlas", "Alice leads the Atlas project")+# What graphiti's model may answer about the one existing edge between Alice+# and Atlas (index 0): the same fact, or one the new fact contradicts.+_RESOLUTIONS = {+ "duplicate": {"duplicate_facts": [0], "contradicted_facts": []},+ "contradiction": {"duplicate_facts": [], "contradicted_facts": [0]},+}+# When each statement says its fact became true: graphiti only lets a newer+# fact invalidate an older one.+_FIRST_VALID_AT = "2026-01-01T00:00:00Z"+_AGAIN_VALID_AT = "2026-06-01T00:00:00Z"+++def _responses(+ fact: Fact, valid_at: str, resolution: dict[str, list[int]] | None = None+) -> dict[str, dict]:+ responses = scripted_responses([fact])+ responses["ExtractedEdges"]["edges"][0]["valid_at"] = valid_at+ if resolution is not None:+ responses["EdgeDuplicate"] = resolution+ return responses++[email protected](autouse=True)+def boundaries(mocker, scope_graph, stub_graphiti_client):+ patch_recall_boundaries(mocker, scope_graph[0], stub_graphiti_client)+++@pytest_asyncio.fixture(loop_scope="function")+async def ingest_worker_cleanup(mocker: MockerFixture) -> AsyncIterator[None]:+ """No dream registration on a first write; no idle worker left behind."""+ mocker.patch(+ "backend.copilot.graphiti.ingest.ensure_scope_scheduled",+ AsyncMock(return_value=None),+ )+ yield+ await stop_ingestion_workers()+++async def _learn_then_forget(+ driver: AutoGPTFalkorDriver, scope: MemoryScope, build: BuildClient+) -> tuple[str, dict[str, Any]]:+ """Alice's fact through the worker, forgotten: its uuid and audit row."""+ client = build(driver, _responses(ALICE, _FIRST_VALID_AT))+ await ingest_through_the_worker(+ driver, scope, client, [ALICE], session_id="s-first"+ )+ [alice] = await live_facts(driver)+ await retract(scope, [alice])+ return alice, await edge_row(driver, alice)+++async def _state_again(+ driver: AutoGPTFalkorDriver,+ scope: MemoryScope,+ build: BuildClient,+ fact: Fact,+ resolution: dict[str, list[int]] | None = None,+) -> tuple[str, list[str], list[str]]:+ """``fact`` stated in a new chat turn: the new episode's uuid, every+ message graphiti sent its model for it, and the answer each call asked+ for."""+ client = build(driver, _responses(fact, _AGAIN_VALID_AT, resolution))+ model = model_of(client)+ answer = model._generate_response+ with patch.object(model, "_generate_response", side_effect=answer) as generate:+ episode = await ingest_through_the_worker(+ driver, scope, client, [fact], session_id="s-again"+ )+ calls = generate.await_args_list+ sent = [m.content for call in calls for m in call.args[0]]+ return episode, sent, [call.args[1].__name__ for call in calls]+++async def _assert_live_again(+ driver: AutoGPTFalkorDriver,+ scope: MemoryScope,+ forgotten: tuple[str, dict[str, Any]],+ episode: str,+ sentence: str,+) -> None:+ """Exactly one live fact, the new one; the forget's audit edge as the+ forget left it; the new episode recallable and citing only the new fact."""+ alice, audit = forgotten+ live = await live_facts(driver)+ assert list(live.values()) == [sentence], "exactly one live fact"+ [restated] = live+ assert restated != alice, "the forgotten edge must stay forgotten"+ assert await edge_row(driver, alice) == audit, "the audit edge changed"+ assert await recalled_facts(scope) == {restated}+ assert episode in await recalled_episodes(scope)+ assert (await episode_row(driver, episode))["entity_edges"] == [restated]+ assert (await edge_row(driver, restated))["episodes"] == [episode]+++async def test_a_fact_taught_again_after_a_forget_is_live_again(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup+) -> None:+ driver, scope = scope_graph+ forgotten = await _learn_then_forget(driver, scope, stub_graphiti_client)++ episode, _, asked = await _state_again(driver, scope, stub_graphiti_client, ALICE)++ await _assert_live_again(driver, scope, forgotten, episode, ALICE[2])+ assert asked.count("ExtractedEdges") == 1, "no second extraction"++[email protected]("resolution", list(_RESOLUTIONS))+async def test_a_fact_resolved_into_a_forgotten_one_gets_its_own_live_edge(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup, resolution: str+) -> None:+ driver, scope = scope_graph+ forgotten = await _learn_then_forget(driver, scope, stub_graphiti_client)++ episode, sent, asked = await _state_again(+ driver, scope, stub_graphiti_client, ALICE, _RESOLUTIONS[resolution]+ )++ assert any(FORGOTTEN_FACT in message for message in sent), "not offered"+ await _assert_live_again(driver, scope, forgotten, episode, ALICE[2])+ assert asked.count("ExtractedEdges") == 1, "no second extraction"+++async def test_a_new_fact_merged_into_a_forgotten_one_never_shows_its_sentence(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup+) -> None:+ """graphiti's model calls a reworded fact a duplicate of the forgotten+ edge: the old sentence reaches none of the prompts that follow, the+ edge's attribute prompt included, and the new wording is the live fact."""+ driver, scope = scope_graph+ forgotten = await _learn_then_forget(driver, scope, stub_graphiti_client)++ episode, sent, asked = await _state_again(+ driver, scope, stub_graphiti_client, _REWORDED, _RESOLUTIONS["duplicate"]+ )++ assert any(FORGOTTEN_FACT in message for message in sent), "not offered"+ assert [message for message in sent if ALICE[2] in message] == []+ await _assert_live_again(driver, scope, forgotten, episode, _REWORDED[2])+ assert asked.count("ExtractedEdges") == 1, "no second extraction"diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_ingest_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_ingest_test.pynew file mode 100644index 0000000000..0db1ebfe01--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_ingest_test.py@@ -0,0 +1,463 @@+"""Unit tests for ``recall_ingest``: graphiti's edge dedup never names a+forgotten edge, checked against graphiti's own dedup prompt, and a prompt the+guard cannot read in full names no edge at all; an attribute answer keeps+only what its type declares.++The live runs, through the production worker, are+``recall_ingest_integration_test.py`` and ``recall_reteach_integration_test.py``.+"""++from collections.abc import Callable+from datetime import datetime, timezone+from typing import Any+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from graphiti_core.driver.driver import GraphProvider+from graphiti_core.edges import EntityEdge+from graphiti_core.llm_client.client import LLMClient+from graphiti_core.llm_client.config import LLMConfig, ModelSize+from graphiti_core.llm_client.token_tracker import TokenUsageTracker+from graphiti_core.nodes import EpisodeType, EpisodicNode+from graphiti_core.prompts import prompt_library+from graphiti_core.prompts.dedupe_edges import EdgeDuplicate+from graphiti_core.prompts.extract_edges import ExtractedEdges+from graphiti_core.prompts.models import Message+from graphiti_core.utils.maintenance.attribute_utils import apply_capped_attributes+from graphiti_core.utils.maintenance.edge_operations import resolve_extracted_edge++from . import recall_ingest+from .client import _build_graphiti+from .recall import FORGOTTEN_FACT+from .recall_ingest import ForgetAwareLLMClient, forgotten_candidates, off_forgotten+from .types import MemoryFact, Person++_EXISTING = "EXISTING FACTS"+_CANDIDATES = "FACT INVALIDATION CANDIDATES"+# A forgotten and a live existing fact, then one invalidation candidate; the+# model names the live ones, which only an unreadable prompt takes away.+_NAMES_LIVE_EDGES = {"duplicate_facts": [1], "contradicted_facts": [2]}+_NAMES_NOTHING = {"duplicate_facts": [], "contradicted_facts": []}++[email protected](autouse=True)+def reported_shapes(monkeypatch: pytest.MonkeyPatch) -> None:+ """Each test starts with no prompt shape logged yet."""+ monkeypatch.setattr(recall_ingest, "_REPORTED_SHAPES", set())+++def _dedup_prompt(existing: list[str], candidates: list[str]) -> list[Message]:+ """graphiti's edge dedup prompt, one idx numbering across both lists."""+ return prompt_library.dedupe_edges.resolve_edge(+ {+ "existing_edges": [{"idx": i, "fact": f} for i, f in enumerate(existing)],+ "new_edge": "Alice is assigned to work on the Atlas project",+ "edge_invalidation_candidates": [+ {"idx": len(existing) + i, "fact": f} for i, f in enumerate(candidates)+ ],+ }+ )+++def _mixed_prompt() -> list[Message]:+ return _dedup_prompt([FORGOTTEN_FACT, "Alice owns the Atlas budget"], ["Bob"])+++def _edited(prompt: list[Message], old: str, new: str) -> list[Message]:+ return [+ m.model_copy(update={"content": m.content.replace(old, new)}) for m in prompt+ ]+++def _retagged(prompt: list[Message], tag: str, new: str) -> list[Message]:+ """``prompt`` with the ``tag`` pair printed as ``new`` (gone when empty)."""+ opened = _edited(prompt, f"<{tag}>", f"<{new}>" if new else "")+ return _edited(opened, f"</{tag}>", f"</{new}>" if new else "")+++def _inner(answer: dict[str, Any]) -> MagicMock:+ inner = MagicMock(spec=LLMClient)+ inner.config = LLMConfig()+ inner.token_tracker = TokenUsageTracker()+ inner.generate_response = AsyncMock(return_value=answer)+ return inner+++class TestForgottenCandidates:+ def test_finds_every_forgotten_edge_in_both_lists(self) -> None:+ prompt = _dedup_prompt(+ [FORGOTTEN_FACT, "Alice owns the Atlas budget"],+ ["Bob leads Atlas", FORGOTTEN_FACT],+ )++ assert forgotten_candidates(prompt) == {0, 3}++ def test_a_prompt_without_one_names_none(self) -> None:+ assert (+ forgotten_candidates(_dedup_prompt(["Alice works on Atlas"], [])) == set()+ )++ def test_quotes_in_other_facts_do_not_confuse_it(self) -> None:+ prompt = _dedup_prompt(['Alice\'s "big" budget', FORGOTTEN_FACT], [])++ assert forgotten_candidates(prompt) == {1}++ def test_lists_it_cannot_read_are_reported_as_such(self) -> None:+ prompt = [Message(role="user", content=f"<EXISTING FACTS>\n{FORGOTTEN_FACT}")]++ assert forgotten_candidates(prompt) is None++ def test_a_forgotten_candidate_outside_the_two_lists_is_still_found(+ self,+ ) -> None:+ """By its text, not its tags: a list the guard does not know about."""+ extra = Message(+ role="user",+ content=f"<RELATED FACTS>\n[{{'idx': 2, 'fact': 'Carol owns Borealis', "+ f"'name': '{FORGOTTEN_FACT}'}}, {{'idx': 3, 'fact': '{FORGOTTEN_FACT}'}}]"+ "\n</RELATED FACTS>",+ )+ prompt = [*_dedup_prompt([FORGOTTEN_FACT], ["Bob leads Atlas"]), extra]++ assert forgotten_candidates(prompt) == {0, 2, 3}++ def test_with_the_tags_gone_the_placeholder_still_marks_its_candidate(+ self,+ ) -> None:+ prompt = _retagged(_retagged(_mixed_prompt(), _EXISTING, ""), _CANDIDATES, "")+ text = "\n".join(message.content for message in prompt)++ assert recall_ingest._shown(text) == {0}+ assert forgotten_candidates(prompt) is None++ def test_a_placeholder_outside_any_candidate_names_no_edge(+ self, caplog: pytest.LogCaptureFixture+ ) -> None:+ """Both lists read, and it is logged at error like any prompt the+ guard cannot read in full, once for the shape+ (``r6-dedup-shapes.py``, ``placeholder_outside_candidate``)."""+ prompt = _edited(+ _mixed_prompt(), "NEW FACT>\n", f"NEW FACT>\n{FORGOTTEN_FACT} "+ )++ assert forgotten_candidates(prompt) is None+ assert forgotten_candidates(prompt) is None+ [error] = [r for r in caplog.records if r.levelname == "ERROR"]+ assert "outside a candidate" in error.getMessage()+++class TestAnUnreadablePrompt:+ """Codex's r5 ``corrupt_partial`` and its relatives: when the prompt+ shows the placeholder, anything short of both lists, once each and+ readable, names no edge, so the statement is saved as new."""++ @pytest.mark.parametrize(+ "damage",+ [+ lambda p: _retagged(p, _EXISTING, "EXISTING_EDGES"),+ lambda p: _retagged(p, _CANDIDATES, "INVALIDATION_EDGES"),+ lambda p: [+ *p,+ Message(role="user", content=f"<{_EXISTING}>[]</{_EXISTING}>"),+ ],+ lambda p: _edited(p, f"<{_CANDIDATES}>\n[", f"<{_CANDIDATES}>\n[oops, "),+ lambda p: _edited(p, "'fact': 'Bob'", "'text': 'Bob'"),+ ],+ ids=[+ "existing renamed",+ "candidates renamed",+ "shown twice",+ "unparsable",+ "no fact",+ ],+ )+ @pytest.mark.asyncio+ async def test_names_no_edge(+ self, damage: Callable[[list[Message]], list[Message]]+ ) -> None:+ client = ForgetAwareLLMClient(_inner(dict(_NAMES_LIVE_EDGES)))++ answer = await client.generate_response(damage(_mixed_prompt()), EdgeDuplicate)++ assert answer == _NAMES_NOTHING++ @pytest.mark.asyncio+ async def test_the_intact_prompt_keeps_the_live_edges_named(self) -> None:+ client = ForgetAwareLLMClient(_inner(dict(_NAMES_LIVE_EDGES)))++ answer = await client.generate_response(_mixed_prompt(), EdgeDuplicate)++ assert answer == _NAMES_LIVE_EDGES++ def test_is_logged_at_error_once_per_shape(+ self, caplog: pytest.LogCaptureFixture+ ) -> None:+ renamed = _retagged(_mixed_prompt(), _EXISTING, "EXISTING_EDGES")+ for prompt in (renamed, renamed, _retagged(_mixed_prompt(), _CANDIDATES, "")):+ forgotten_candidates(prompt)++ errors = [r for r in caplog.records if r.levelname == "ERROR"]+ assert len(errors) == 2+ assert "EXISTING_EDGES" in errors[0].getMessage()++ @pytest.mark.asyncio+ async def test_without_the_placeholder_passes_the_answer_but_is_logged(+ self, caplog: pytest.LogCaptureFixture+ ) -> None:+ prompt = _retagged(_dedup_prompt(["Alice"], ["Bob"]), _EXISTING, "RENAMED")+ client = ForgetAwareLLMClient(_inner({"duplicate_facts": [0]}))++ answer = await client.generate_response(prompt, EdgeDuplicate)++ assert answer == {"duplicate_facts": [0]}+ assert "RENAMED" in caplog.text+++class TestOffForgotten:+ def test_a_forgotten_edge_is_neither_duplicate_nor_contradiction(self) -> None:+ answer = {"duplicate_facts": [0, 1], "contradicted_facts": [1, 3]}++ assert off_forgotten(answer, {0, 3}) == {+ "duplicate_facts": [1],+ "contradicted_facts": [1],+ }++ def test_unreadable_lists_keep_the_new_fact_new(self) -> None:+ answer = {"duplicate_facts": [1], "contradicted_facts": [2]}++ assert off_forgotten(answer, None) == {+ "duplicate_facts": [],+ "contradicted_facts": [],+ }++ def test_a_malformed_answer_is_left_for_graphiti_to_reject(self) -> None:+ assert off_forgotten({"duplicate_facts": "x"}, {0}) == {"duplicate_facts": "x"}+++class TestForgetAwareLLMClient:+ @pytest.mark.asyncio+ async def test_edge_dedup_answers_are_kept_off_forgotten_edges(self) -> None:+ inner = _inner({"duplicate_facts": [0], "contradicted_facts": [1]})+ client = ForgetAwareLLMClient(inner)+ prompt = _dedup_prompt([FORGOTTEN_FACT], ["Bob leads Atlas"])++ answer = await client.generate_response(+ prompt,+ EdgeDuplicate,+ model_size=ModelSize.small,+ prompt_name="dedupe_edges.resolve_edge",+ )++ assert answer == {"duplicate_facts": [], "contradicted_facts": [1]}+ inner.generate_response.assert_awaited_once_with(+ prompt,+ EdgeDuplicate,+ None,+ ModelSize.small,+ None,+ "dedupe_edges.resolve_edge",+ attribute_extraction=False,+ )++ @pytest.mark.asyncio+ async def test_every_other_call_passes_through(self) -> None:+ answer = {"edges": [], "note": FORGOTTEN_FACT}+ client = ForgetAwareLLMClient(_inner(answer))+ prompt = [Message(role="user", content=f"extract: {FORGOTTEN_FACT}")]++ assert await client.generate_response(prompt, ExtractedEdges) == answer++ @pytest.mark.asyncio+ async def test_an_attribute_answer_keeps_only_what_its_type_declares(+ self,+ ) -> None:+ """Codex's probe: graphiti-core 0.30.2 keeps every key its model+ answers, so one inventing a forget's marker and audit copies would+ have them saved on a live fact. Through the client, graphiti's own+ merge keeps only ``MemoryFact``'s fields."""+ secret = "Alice works on the secret Atlas plan"+ answer = {+ "status": "active",+ "scope": "real:global",+ "forgotten_at": secret,+ "fact_redacted": secret,+ "name_redacted": secret,+ "ratified_at": "2026-01-01T00:00:00+00:00",+ "made_up": secret,+ }+ client = ForgetAwareLLMClient(_inner(dict(answer)))+ prompt = [Message(role="user", content="Alice works on Atlas")]++ kept = await client.generate_response(+ prompt, MemoryFact, attribute_extraction=True+ )+ merged, _ = apply_capped_attributes(kept, MemoryFact, {}, merge_mode="replace")++ assert kept == merged == {"status": "active", "scope": "real:global"}+ unfiltered, _ = apply_capped_attributes(+ answer, MemoryFact, {}, merge_mode="replace"+ )+ assert unfiltered["forgotten_at"] == secret, "graphiti keeps it unfiltered"++ @pytest.mark.asyncio+ async def test_an_entity_attribute_answer_keeps_its_types_fields(self) -> None:+ answer = {"role": "CTO", "email": "[email protected]", "forgotten_at": "x"}+ client = ForgetAwareLLMClient(_inner(answer))++ kept = await client.generate_response([], Person, attribute_extraction=True)++ assert kept == {"role": "CTO", "email": "[email protected]"}++ def test_it_shares_the_inner_clients_tracer_and_token_counts(self) -> None:+ inner = _inner({})+ client = ForgetAwareLLMClient(inner)+ tracer = MagicMock()++ client.set_tracer(tracer)++ inner.set_tracer.assert_called_once_with(tracer)+ assert client.token_tracker is inner.token_tracker+++def test_every_graphiti_client_is_built_forget_aware() -> None:+ inner = _inner({})+ with patch("graphiti_core.Graphiti") as graphiti:+ _build_graphiti(+ "user_abc",+ inner,+ embedder=MagicMock(),+ cross_encoder=MagicMock(),+ graph_driver=MagicMock(),+ )++ wrapped = graphiti.call_args.kwargs["llm_client"]+ assert isinstance(wrapped, ForgetAwareLLMClient) and wrapped.inner is inner+++def _errors(caplog: pytest.LogCaptureFixture) -> list[str]:+ return [r.getMessage() for r in caplog.records if r.levelname == "ERROR"]+++def _failing_parser(text: str) -> None:+ raise RuntimeError("parser bug")+++_THEN = datetime(2026, 9, 27, tzinfo=timezone.utc)+++def _edge(uuid: str, fact: str) -> EntityEdge:+ """An edge with its valid time known, so graphiti asks no timestamps."""+ return EntityEdge(+ uuid=uuid,+ group_id="user_abc",+ source_node_uuid="alice",+ target_node_uuid="atlas",+ created_at=_THEN,+ valid_at=_THEN,+ name="works_on",+ fact=fact,+ )+++class TestAGuardThatHitsAnError:+ """Sentry's review of #14972: a candidate that reads as a literal but+ fails validation, or anything unexpected while the guard reads the+ prompt, names no edge, is logged at error once per shape and never+ stops graphiti's edge resolution."""++ @pytest.mark.parametrize(+ "old, new",+ [+ ("{'idx': 1, 'fact'", "{'idx': 'one', 'fact'"),+ ("{'idx': 0, 'fact'", "{'idx': 'zero', 'fact'"),+ ],+ ids=["a live candidate", "the forgotten candidate"],+ )+ @pytest.mark.asyncio+ async def test_a_candidate_that_fails_validation_names_no_edge(+ self, old: str, new: str, caplog: pytest.LogCaptureFixture+ ) -> None:+ client = ForgetAwareLLMClient(_inner(dict(_NAMES_LIVE_EDGES)))+ prompt = _edited(_mixed_prompt(), old, new)++ first = await client.generate_response(prompt, EdgeDuplicate)+ logged = _errors(caplog)+ again = await client.generate_response(prompt, EdgeDuplicate)++ assert first == again == _NAMES_NOTHING+ assert logged and _errors(caplog) == logged, "once per shape"++ @pytest.mark.asyncio+ async def test_an_unexpected_error_while_reading_names_no_edge(+ self, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture+ ) -> None:+ monkeypatch.setattr(recall_ingest, "_listed", _failing_parser)+ client = ForgetAwareLLMClient(_inner(dict(_NAMES_LIVE_EDGES)))++ for _ in range(2):+ answer = await client.generate_response(_mixed_prompt(), EdgeDuplicate)+ assert answer == _NAMES_NOTHING++ [error] = [r for r in caplog.records if r.levelname == "ERROR"]+ assert "raised RuntimeError" in error.getMessage()+ assert error.exc_info is not None, "with its traceback"++ @pytest.mark.asyncio+ async def test_graphitis_edge_resolution_still_completes(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """graphiti's own resolution of a new statement, its model naming+ the forgotten edge a duplicate: the statement stays a new edge and+ the forgotten one is untouched, instead of the error ending+ ``add_episode``."""+ monkeypatch.setattr(recall_ingest, "_listed", _failing_parser)+ duplicate = {"duplicate_facts": [0], "contradicted_facts": []}+ client = ForgetAwareLLMClient(_inner(duplicate))+ forgotten = _edge("forgotten", FORGOTTEN_FACT)+ new = _edge("new", "Alice is assigned to work on the Atlas project")++ episode = EpisodicNode(+ name="s-2",+ group_id="user_abc",+ source=EpisodeType.text,+ source_description="chat",+ content=new.fact,+ created_at=_THEN,+ valid_at=_THEN,+ )++ resolved, invalidated, duplicates = await resolve_extracted_edge(+ client, new, [forgotten], [], episode+ )++ assert (resolved.uuid, invalidated, duplicates) == ("new", [], [])+ assert (forgotten.fact, forgotten.episodes) == (FORGOTTEN_FACT, [])+++def _episode_record(uuid: str) -> dict[str, Any]:+ when = datetime(2026, 10, 1, tzinfo=timezone.utc)+ return {+ "uuid": uuid,+ "name": f"conversation_{uuid}",+ "group_id": "user_g",+ "source": "message",+ "source_description": "",+ "content": f"said {uuid}",+ "created_at": when,+ "valid_at": when,+ "entity_edges": [],+ }+++async def test_earlier_episodes_come_back_in_the_order_asked_for() -> None:+ """``add_episode`` shows its prompts the episodes in the order+ ``get_by_uuids`` returns them; the graph yields them in its own order."""+ driver = MagicMock(graph_operations_interface=None, provider=GraphProvider.FALKORDB)+ driver.execute_query = AsyncMock(+ return_value=([_episode_record(u) for u in ("c", "a", "b")], None, None)+ )++ episodes = await EpisodicNode.get_by_uuids(driver, ["a", "b", "c"])++ assert [episode.uuid for episode in episodes] == ["a", "b", "c"]diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_integration_fixtures.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_integration_fixtures.pynew file mode 100644index 0000000000..f31a71f15b--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_integration_fixtures.py@@ -0,0 +1,290 @@+"""Shared set-up for the live recall-policy tests (``*_integration_test.py``).++Facts go in through graphiti's real ``add_episode`` with only the LLM+boundary scripted (the ``stub_graphiti_client`` fixture), so what the tests+read back is what production would have written; ``ingest_through_the_worker``+goes one step further out, through the production ingestion worker. Not+collected by pytest.+"""++import asyncio+import json+from collections.abc import Callable, Coroutine+from datetime import datetime, timedelta, timezone+from typing import Any, cast+from unittest.mock import AsyncMock, patch++from graphiti_core import Graphiti+from graphiti_core.llm_client.client import LLMClient+from graphiti_core.nodes import EpisodeType+from pytest_mock import MockerFixture++from backend.api.features.memory import routes as memory_routes+from backend.copilot.tools import graphiti_search++from . import ingest, recall+from .falkordb_driver import AutoGPTFalkorDriver+from .recall_fake_redis import FakeRedis+from .recall_ingest import ForgetAwareLLMClient+from .scope import MemoryScope+from .types import EDGE_TYPE_MAP, EDGE_TYPES, ENTITY_TYPES++# (source entity, target entity, fact sentence)+Fact = tuple[str, str, str]+BuildClient = Callable[[AutoGPTFalkorDriver, dict[str, dict]], Graphiti]++ALICE: Fact = ("Alice", "Atlas", "Alice works on Atlas")+BOB: Fact = ("Bob", "Atlas", "Bob leads Atlas")+CAROL: Fact = ("Carol", "Borealis", "Carol owns Borealis")++_INGEST_TIMEOUT_SECONDS = 25.0+++def patch_recall_boundaries(+ mocker: MockerFixture, driver: AutoGPTFalkorDriver, build_client: BuildClient+) -> None:+ """Search through a stubbed-LLM Graphiti on the test graph, count hits in+ memory, and report memory as enabled for the settings routes and tools."""+ client = build_client(driver, {})+ mocker.patch.object(+ recall, "get_graphiti_client", mocker.AsyncMock(return_value=client)+ )+ mocker.patch(+ "backend.data.redis_client.get_redis_async",+ mocker.AsyncMock(return_value=FakeRedis()),+ )+ enabled = mocker.AsyncMock(return_value=True)+ mocker.patch.object(memory_routes, "is_enabled_for_user", enabled)+ mocker.patch.object(graphiti_search, "is_enabled_for_user", enabled)+++def model_of(client: Graphiti) -> LLMClient:+ """The scripted model under ``client``'s forget-aware wrapper+ (``recall_ingest.ForgetAwareLLMClient``), to patch or inspect."""+ return cast(ForgetAwareLLMClient, client.llm_client).inner+++def capture_spawned_tasks(mocker: MockerFixture) -> list[asyncio.Task]:+ """Keep the hit-recording tasks ``memory_search`` detaches, so a test can+ await them instead of racing them."""+ spawned: list[asyncio.Task] = []++ def spawn(coro: Coroutine[Any, Any, None], *, name: str) -> asyncio.Task:+ task = asyncio.create_task(coro, name=name)+ spawned.append(task)+ return task++ mocker.patch.object(graphiti_search, "spawn_background_task", spawn)+ return spawned+++def scripted_responses(facts: list[Fact], *, status: str = "active") -> dict[str, dict]:+ """What the scripted LLM answers so ``add_episode`` extracts ``facts``."""+ names = list(dict.fromkeys(name for s, t, _ in facts for name in (s, t)))+ edges = [+ {+ "source_entity_name": source,+ "target_entity_name": target,+ "relation_type": "MemoryFact",+ "fact": sentence,+ "valid_at": None,+ "invalid_at": None,+ }+ for source, target, sentence in facts+ ]+ return {+ "ExtractedEntities": {+ "extracted_entities": [{"name": n, "entity_type_id": 0} for n in names]+ },+ "NodeResolutions": {+ "entity_resolutions": [+ {"id": i, "name": n, "duplicate_candidate_id": -1}+ for i, n in enumerate(names)+ ]+ },+ "ExtractedEdges": {"edges": edges},+ "MemoryFact": {"status": status, "scope": "real:global"},+ "EdgeDuplicate": {"duplicate_facts": [], "contradicted_facts": []},+ # Asked for an entity with no summary and no new fact in the episode+ # (after a forget blanked it); no answer keeps the summary empty.+ "SummarizedEntities": {"summaries": []},+ }+++async def ingest_facts(+ driver: AutoGPTFalkorDriver,+ scope: MemoryScope,+ build_client: BuildClient,+ facts: list[Fact],+ *,+ status: str = "active",+ session_id: str | None = None,+ body: str | None = None,+) -> tuple[str, dict[str, str]]:+ """One episode through graphiti's ``add_episode`` holding ``facts``.++ Its text is the fact sentences unless ``body`` is given, and it is named+ like a chat turn of ``session_id`` when one is. Returns the episode uuid+ and each fact sentence's edge uuid.+ """+ client = build_client(driver, scripted_responses(facts, status=status))+ name, description = _episode_labels(facts, session_id)+ result = await client.add_episode(+ name=name,+ episode_body=body or ". ".join(sentence for _, _, sentence in facts),+ source=EpisodeType.text,+ source_description=description,+ reference_time=datetime.now(timezone.utc) - timedelta(minutes=1),+ group_id=scope.group_id,+ entity_types=ENTITY_TYPES,+ edge_types=EDGE_TYPES,+ edge_type_map=EDGE_TYPE_MAP,+ )+ edges = {edge.fact: edge.uuid for edge in result.edges}+ assert set(edges) == {sentence for _, _, sentence in facts}, (+ "add_episode did not persist the scripted facts; re-run with "+ "--log-cli-level=WARNING to see why"+ )+ return result.episode.uuid, edges+++async def ingest_through_the_worker(+ driver: AutoGPTFalkorDriver,+ scope: MemoryScope,+ client: Graphiti,+ facts: list[Fact],+ *,+ session_id: str,+ body: str | None = None,+) -> str:+ """One chat turn stating ``facts`` (its text ``body`` when given) through+ the production worker (``ingest.enqueue_episode``), with ``client`` as+ the scope's graphiti client. Returns the new episode's uuid.++ The caller cancels the worker afterwards (``stop_ingestion_workers``).+ """+ name, description = _episode_labels(facts, session_id)+ completion = ingest.IngestionCompletion()+ with patch.object(ingest, "get_graphiti_client", AsyncMock(return_value=client)):+ queued = await ingest.enqueue_episode(+ scope,+ session_id,+ name=name,+ episode_body=body or ". ".join(sentence for _, _, sentence in facts),+ source_description=description,+ completion=completion,+ )+ assert queued, "the ingestion queue refused the episode"+ completion.register()+ done = await ingest.wait_for_ingestion(completion, _INGEST_TIMEOUT_SECONDS)+ assert done, "the ingestion worker did not finish"+ found = await rows(+ driver,+ "MATCH (ep:Episodic {name: $name}) RETURN ep.uuid AS uuid",+ name=name,+ )+ assert len(found) == 1, "the worker did not write the episode"+ return found[0]["uuid"]+++async def stop_ingestion_workers() -> None:+ """Cancel the per-scope workers a test's ingestion started."""+ workers = list(ingest._get_loop_state().group_workers.values())+ for worker in workers:+ worker.cancel()+ await asyncio.gather(*workers, return_exceptions=True)+++def _episode_labels(facts: list[Fact], session_id: str | None) -> tuple[str, str]:+ """``(name, source_description)``, as ingestion writes them for a chat+ turn of ``session_id``."""+ if session_id is None:+ return f"episode-{len(facts)}-{facts[0][2]}", "recall integration test"+ return f"conversation_{session_id}", f"User message in session {session_id}"+++async def rows(+ driver: AutoGPTFalkorDriver, query: str, **params: object+) -> list[dict[str, Any]]:+ result = await driver.execute_query(query, **params)+ assert result is not None, "a Cypher read returned no result set"+ return result[0]+++async def edge_row(driver: AutoGPTFalkorDriver, edge_uuid: str) -> dict[str, Any]:+ """The edge's audit fields, or ``{}`` once it is gone."""+ found = await rows(+ driver,+ """+ MATCH ()-[e:RELATES_TO {uuid: $uuid}]->()+ RETURN e.status AS status, e.expired_at AS expired_at,+ e.invalid_at AS invalid_at, e.expiration_reason AS reason,+ e.forgotten_at AS forgotten_at, e.episodes AS episodes,+ e.fact AS fact, e.fact_redacted AS fact_redacted,+ e.name AS name, e.name_redacted AS name_redacted+ """,+ uuid=edge_uuid,+ )+ return found[0] if found else {}+++async def episode_row(driver: AutoGPTFalkorDriver, episode_uuid: str) -> dict[str, Any]:+ """The stored episode, redacted or not, or ``{}`` once it is gone."""+ found = await rows(+ driver,+ """+ MATCH (ep:Episodic {uuid: $uuid})+ RETURN ep.content AS content, ep.redacted_at AS redacted_at,+ ep.entity_edges AS entity_edges, ep.name AS name,+ ep.hard_deleted_at AS hard_deleted_at,+ ep.provenance AS provenance+ """,+ uuid=episode_uuid,+ )+ return found[0] if found else {}+++async def count(driver: AutoGPTFalkorDriver, query: str) -> int:+ return int((await rows(driver, query))[0]["c"])+++async def recalled_facts(scope: MemoryScope) -> set[str]:+ return {fact.uuid for fact in await recall.search_facts(scope, "Atlas", limit=20)}+++async def recalled_episodes(scope: MemoryScope) -> set[str]:+ return {episode.uuid for episode in await recall.recent_episodes(scope, 5)}+++async def sentence_properties(driver: AutoGPTFalkorDriver, sentence: str) -> set[str]:+ """Every node and edge property anywhere in the graph that holds+ ``sentence``, as ``Label.property``."""+ nodes = await rows(+ driver, "MATCH (n) RETURN labels(n) AS labels, properties(n) AS properties"+ )+ edges = await rows(+ driver,+ "MATCH ()-[e]->() RETURN [type(e)] AS labels, properties(e) AS properties",+ )+ return {+ f"{_kind(row['labels'])}.{key}"+ for row in nodes + edges+ for key, value in row["properties"].items()+ if sentence in json.dumps(value, default=str)+ }+++def _kind(labels: list[str]) -> str:+ known = [label for label in ("Episodic", "Entity", "Community") if label in labels]+ return (known or labels)[0]+++async def live_facts(driver: AutoGPTFalkorDriver) -> dict[str, str]:+ """Every live fact's sentence by edge uuid, under the recall policy."""+ found = await rows(+ driver,+ "MATCH ()-[e:RELATES_TO]->() "+ f"WHERE {recall.live_fact_predicate('e')} "+ "RETURN e.uuid AS uuid, e.fact AS fact",+ )+ return {row["uuid"]: row["fact"] for row in found}diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_integration_test.pynew file mode 100644index 0000000000..d2280307c5--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_integration_test.py@@ -0,0 +1,309 @@+"""The recall policy against a live FalkorDB: a forgotten fact and the+episode text it came from can no longer be recalled; a superseded fact is not+recalled either, while its episode, which no forget touched, still is.++Facts go in through graphiti's real ``add_episode`` (only the LLM boundary is+scripted, see ``recall_integration_fixtures.py``) and are read back through+the same ``recall`` functions warm context, ``memory_search``,+``memory_forget_search`` and the settings page use. The unit siblings+(``recall_test.py``, ``recall_forget_test.py``) pin the Cypher; this file+proves it does what it says on FalkorDB. The forget contract's hard cases are+in ``recall_forget_integration_test.py``, ``recall_hard_forget_integration_test.py``+and ``recall_dream_writers_integration_test.py``.++Run with FalkorDB reachable (see ``conftest.py``)::++ poetry run pytest -m integration backend/copilot/graphiti/recall_integration_test.py+"""++import asyncio+import uuid+from datetime import datetime, timedelta, timezone++import pytest+from graphiti_core.edges import EntityEdge++from backend.api.features.memory import routes as memory_routes+from backend.copilot.dream.fetch import _fetch_active_facts, _fetch_recent_episodes+from backend.copilot.dream.hidden_sessions import hidden_session_ids+from backend.copilot.dream.ratification import try_ratify_on_hit+from backend.copilot.dream.ratification_hits import get_hit_count+from backend.copilot.model import ChatSession+from backend.copilot.tools import graphiti_search+from backend.copilot.tools.graphiti_forget import mark_edges_superseded+from backend.copilot.tools.models import MemorySearchResponse++from . import context+from .falkordb_driver import open_driver+from .memory_model import MemoryForgetFailureCode+from .recall import (+ FORGOTTEN_FACT,+ forgotten_fact_predicate,+ is_forgotten,+ search_forgotten_facts,+)+from .recall_forget import retract+from .recall_integration_fixtures import (+ ALICE,+ BOB,+ CAROL,+ Fact,+ capture_spawned_tasks,+ edge_row,+ ingest_facts,+ patch_recall_boundaries,+ recalled_episodes,+ recalled_facts,+ rows,+)+from .scope import MemoryScope++_LONG_AGO = "2025-01-01T00:00:00+00:00"+# (status, expiration_reason, expired_at, invalid_at, forgotten_at) -> forgotten?+# Only the forget's marker, or the pre-policy shape, makes a fact forgotten.+_EDGE_SHAPES = {+ ("active", None, None, None, None): False,+ ("retracted", "user_signal", _LONG_AGO, None, _LONG_AGO): True,+ ("retracted", None, None, None, None): False,+ ("retracted", "user_signal", _LONG_AGO, None, None): False,+ ("superseded", "user_signal", _LONG_AGO, None, None): False,+ ("superseded", "stale_fact", _LONG_AGO, None, None): False,+ ("superseded", "stale_fact", _LONG_AGO, _LONG_AGO, _LONG_AGO): True,+ ("active", None, _LONG_AGO, _LONG_AGO, None): False,+ ("active", None, _LONG_AGO, None, None): True,+}++[email protected](autouse=True)+def boundaries(mocker, scope_graph, stub_graphiti_client):+ patch_recall_boundaries(mocker, scope_graph[0], stub_graphiti_client)+++def hit_tasks(mocker) -> list[asyncio.Task]:+ return capture_spawned_tasks(mocker)+++async def _settings_view(scope: MemoryScope) -> tuple[int, set[str]]:+ """The settings page's fact count and fact list for the scope."""+ overview = await memory_routes._get_overview_impl(scope.owner_user_id, None)+ listing = await memory_routes._list_facts_impl(scope.owner_user_id, None, 50)+ return overview.facts, {item.uuid for item in listing.items}+++async def test_retracted_fact_leaves_recall_and_hides_its_episode(+ scope_graph, stub_graphiti_client+) -> None:+ driver, scope = scope_graph+ episode, edges = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE, BOB]+ )+ alice, bob = edges[ALICE[2]], edges[BOB[2]]+ assert await recalled_facts(scope) == {alice, bob}+ assert await recalled_episodes(scope) == {episode}+ assert await _settings_view(scope) == (2, {alice, bob})++ first = await retract(scope, [alice])++ assert first.deleted == [alice] and first.failures == []+ row = await edge_row(driver, alice)+ assert row["status"] == "retracted"+ assert row["reason"] == "user_signal"+ assert row["expired_at"] is not None+ assert row["forgotten_at"] is not None+ assert row["invalid_at"] is None, "a forget is not a world change (Snodgrass)"+ assert (row["fact"], row["fact_redacted"]) == (FORGOTTEN_FACT, ALICE[2])+ assert await recalled_facts(scope) == {bob}+ assert await _settings_view(scope) == (1, {bob})+ # The episode holds Alice's sentence too, so it goes at once; Bob's fact+ # stays recallable as a fact.+ assert first.redacted_episodes == [episode]+ assert await recalled_episodes(scope) == set()++ second = await retract(scope, [bob])++ assert second.redacted_episodes == [episode]+ assert await recalled_facts(scope) == set()+ assert await _settings_view(scope) == (0, set())+++async def test_warm_context_and_memory_search_skip_what_was_forgotten(+ scope_graph, stub_graphiti_client, hit_tasks+) -> None:+ driver, scope = scope_graph+ forgotten_episode, forgotten = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE]+ )+ _, kept = await ingest_facts(driver, scope, stub_graphiti_client, [CAROL])+ await retract(scope, [forgotten[ALICE[2]]])++ warm = await context._fetch(scope, "Atlas")+ await asyncio.gather(*context._pending_hit_tasks)++ assert warm is not None+ assert CAROL[2] in warm+ assert ALICE[2] not in warm, "a forgotten fact reached warm context"+ session = ChatSession.new(scope.owner_user_id, dry_run=False)+ found = await graphiti_search.MemorySearchTool()._execute(+ scope.owner_user_id, session, query="Atlas"+ )+ await asyncio.gather(*hit_tasks)+ assert isinstance(found, MemorySearchResponse)+ assert any(CAROL[2] in fact for fact in found.facts)+ assert not any(ALICE[2] in fact for fact in found.facts)+ assert not any(+ ALICE[2] in episode for episode in found.recent_episodes+ ), "the forgotten fact's episode text resurfaced"+ assert forgotten_episode not in await recalled_episodes(scope)+ assert kept[CAROL[2]] in await recalled_facts(scope)+++async def test_memory_search_records_a_hit_on_what_it_returns(+ scope_graph, stub_graphiti_client, hit_tasks+) -> None:+ driver, scope = scope_graph+ _, edges = await ingest_facts(+ driver, scope, stub_graphiti_client, [CAROL], status="tentative"+ )+ session = ChatSession.new(scope.owner_user_id, dry_run=False)++ await graphiti_search.MemorySearchTool()._execute(+ scope.owner_user_id, session, query="Borealis"+ )+ await asyncio.gather(*hit_tasks)++ assert await get_hit_count(scope, edges[CAROL[2]]) == 1+++async def test_superseded_fact_is_not_recalled(+ scope_graph, stub_graphiti_client+) -> None:+ driver, scope = scope_graph+ _, edges = await ingest_facts(driver, scope, stub_graphiti_client, [ALICE, BOB])+ alice, bob = edges[ALICE[2]], edges[BOB[2]]++ demoted, failed = await mark_edges_superseded(+ driver,+ [alice],+ reason="stale_fact",+ user_id=scope.owner_user_id,+ group_id=scope.group_id,+ )++ assert (demoted, failed) == ([alice], [])+ assert await recalled_facts(scope) == {bob}+ assert await _settings_view(scope) == (1, {bob})+++async def test_retracted_fact_is_neither_gathered_nor_ratified_by_the_dream(+ scope_graph, stub_graphiti_client+) -> None:+ driver, scope = scope_graph+ active_episode, active = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE]+ )+ tentative_episode, tentative = await ingest_facts(+ driver, scope, stub_graphiti_client, [CAROL], status="tentative"+ )+ await retract(scope, [active[ALICE[2]], tentative[CAROL[2]]])++ facts = await _fetch_active_facts(driver, scope.group_id, 50)+ window_start = datetime.now(timezone.utc) - timedelta(days=14)+ episodes = await _fetch_recent_episodes(driver, scope.group_id, window_start, 50)++ assert facts == []+ assert {e.uuid for e in episodes}.isdisjoint({active_episode, tentative_episode})+ assert await try_ratify_on_hit(scope, [tentative[CAROL[2]]]) == 0+ assert (await edge_row(driver, tentative[CAROL[2]]))["status"] == "retracted"+++async def test_the_dream_leaves_out_the_sessions_a_forget_hid(+ scope_graph, stub_graphiti_client+) -> None:+ driver, scope = scope_graph+ _, forgotten = await ingest_facts(+ driver, scope, stub_graphiti_client, [ALICE], session_id="s-forgotten"+ )+ await ingest_facts(+ driver, scope, stub_graphiti_client, [CAROL], session_id="s-kept"+ )+ assert await hidden_session_ids(driver, scope.group_id) == set()++ await retract(scope, [forgotten[ALICE[2]]])++ assert await hidden_session_ids(driver, scope.group_id) == {"s-forgotten"}+++async def test_the_forgotten_fact_cypher_and_python_agree(+ scope_graph, stub_graphiti_client+) -> None:+ driver, scope = scope_graph+ facts: list[Fact] = [+ (f"Person{i}", "Atlas", f"Person{i} works on Atlas")+ for i in range(len(_EDGE_SHAPES))+ ]+ _, edges = await ingest_facts(driver, scope, stub_graphiti_client, facts)+ uuids = [edges[sentence] for _, _, sentence in facts]+ for edge_uuid, shape in zip(uuids, _EDGE_SHAPES):+ status, reason, expired, invalid, forgotten = shape+ await driver.execute_query(+ "MATCH ()-[e:RELATES_TO {uuid: $uuid}]->() SET e.status = $status, "+ "e.expiration_reason = $reason, e.expired_at = $expired, "+ "e.invalid_at = $invalid, e.forgotten_at = $forgotten",+ uuid=edge_uuid,+ status=status,+ reason=reason,+ expired=expired,+ invalid=invalid,+ forgotten=forgotten,+ )++ found = await rows(+ driver,+ "MATCH ()-[e:RELATES_TO]->() RETURN e.uuid AS uuid, "+ f"CASE WHEN {forgotten_fact_predicate('e')} THEN true ELSE false END AS f",+ )+ in_cypher = {row["uuid"]: row["f"] for row in found}+ read_back = await EntityEdge.get_by_uuids(driver, uuids)+ assert in_cypher == {edge.uuid: is_forgotten(edge) for edge in read_back}+ assert [in_cypher[edge_uuid] for edge_uuid in uuids] == list(_EDGE_SHAPES.values())+ # The search for a permanent delete ranks these alone: with every fact+ # equally close (the stub embeds everything alike), six that are not+ # forgotten take none of the slots, however few.+ forgotten = {edge_uuid for edge_uuid, gone in in_cypher.items() if gone}+ every = await search_forgotten_facts(scope, "Atlas", limit=len(uuids))+ assert {edge.uuid for edge in every} == forgotten+ two = await search_forgotten_facts(scope, "Atlas", limit=2)+ assert len(two) == 2 and {edge.uuid for edge in two} <= forgotten+++async def test_forgetting_in_a_scope_without_a_graph_creates_none(+ falkordb_available,+) -> None:+ scope = MemoryScope.for_user(f"test-{uuid.uuid4().hex[:16]}")++ result = await retract(scope, ["nonexistent"])++ assert result.deleted == []+ assert [f.code for f in result.failures] == [MemoryForgetFailureCode.NO_MATCH]+ driver = open_driver(scope)+ try:+ assert scope.group_id not in await driver.client.list_graphs()+ finally:+ await driver.close()diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_orphans.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_orphans.pynew file mode 100644index 0000000000..79ced8547c--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_orphans.py@@ -0,0 +1,167 @@+"""A hard forget's last step: empty and delete what the forgotten edges leave.++It runs after ``recall_forget.retract`` has retracted and hidden the facts+(``recall_hide.py``), so every step here works on facts recall already+hides, and every step is safe to repeat: the edges are deleted last, in one+query each, so forgetting again after any failure finds them and finishes.++This is ownership, not visibility. An episode that no remaining fact edge+cites, whatever that edge's status (a retracted edge kept for audit still+needs its source), is not deleted but emptied into a tombstone: ``content``+cleared, ``hard_deleted_at`` set, its name, source description and envelope+provenance kept, so the dream still knows which chat session to leave out+(``dream/hidden_sessions.py``). Its mentions go, and so does every entity+left with no fact and no mention.+"""++import logging+from typing import Any++from graphiti_core.driver.driver import GraphDriver++from .memory_model import ForgetResult, MemoryForgetFailure, envelope_provenance++logger = logging.getLogger(__name__)+++async def purge(+ driver: GraphDriver,+ group_id: str,+ uuids: list[str],+ now: str,+ result: ForgetResult,+) -> None:+ """Tombstone the episodes only ``uuids`` cite, then delete each edge and+ the entities it alone kept; a failed step is each edge's+ ``cleanup_error``."""+ try:+ result.tombstoned_episodes = await _tombstone(driver, uuids, now)+ except Exception as exc:+ _report(group_id, uuids, exc, result)+ return+ for edge_uuid in uuids:+ await _delete_edge(driver, group_id, edge_uuid, result)+ tombstoned = set(result.tombstoned_episodes)+ result.redacted_episodes = [+ episode for episode in result.redacted_episodes if episode not in tombstoned+ ]+++async def _tombstone(driver: GraphDriver, uuids: list[str], now: str) -> list[str]:+ """Empty every episode nothing outside ``uuids`` cites, keeping the+ envelope provenance its text carried (parsed here, not in Cypher)."""+ citing = _rows(await driver.execute_query(_CITING_EPISODES_QUERY, uuids=uuids))+ provenance = [+ [row["uuid"], found]+ for row in citing+ if (found := envelope_provenance(row["content"])) is not None+ ]+ rows = _rows(+ await driver.execute_query(+ _TOMBSTONE_QUERY, uuids=uuids, provenance=provenance, now=now+ )+ )+ return [row["uuid"] for row in rows]+++async def _delete_edge(+ driver: GraphDriver,+ group_id: str,+ edge_uuid: str,+ result: ForgetResult,+) -> None:+ try:+ rows = _rows(+ await driver.execute_query(+ _DELETE_EDGE_QUERY, uuid=edge_uuid, group_id=group_id+ )+ )+ except Exception as exc:+ _report(group_id, [edge_uuid], exc, result)+ return+ if not rows:+ result.failures.append(MemoryForgetFailure.no_match(edge_uuid))+ return+ result.deleted.append(edge_uuid)+ result.deleted_entities.extend(rows[0]["deleted_entities"])+++def _report(+ group_id: str, uuids: list[str], exc: Exception, result: ForgetResult+) -> None:+ logger.warning(+ f"Edges retracted and hidden but hard deletion failed in graph "+ f"{group_id[:20]}",+ exc_info=True,+ )+ result.failures.extend(+ MemoryForgetFailure.cleanup_error(edge_uuid, exc) for edge_uuid in uuids+ )+++def _rows(result: Any) -> list[dict[str, Any]]:+ return result[0] if result else []+++_CITING_EPISODES_QUERY = """+MATCH (ep:Episodic)+WHERE any(x IN coalesce(ep.entity_edges, []) WHERE x IN $uuids)+RETURN ep.uuid AS uuid, ep.content AS content+"""++# An episode is cited by an edge its ``entity_edges`` names or whose+# ``episodes`` names it (graphiti writes both). ``coalesce`` keeps a first+# tombstone's stamps and provenance on a repeat. The tombstone keeps+# ``entity_edges`` until its edges are deleted, so a retry still finds it.+_TOMBSTONE_QUERY = """+MATCH (ep:Episodic)+WHERE any(x IN coalesce(ep.entity_edges, []) WHERE x IN $uuids)+OPTIONAL MATCH ()-[ref:RELATES_TO]->()+WHERE NOT ref.uuid IN $uuids+ AND (ref.uuid IN ep.entity_edges OR ep.uuid IN coalesce(ref.episodes, []))+WITH ep, count(ref) AS refs+WHERE refs = 0+SET ep.provenance = coalesce(+ ep.provenance, [p IN $provenance WHERE p[0] = ep.uuid | p[1]][0]+ ),+ ep.content = '',+ ep.redacted_at = coalesce(ep.redacted_at, $now),+ ep.hard_deleted_at = coalesce(ep.hard_deleted_at, $now)+RETURN ep.uuid AS uuid+"""++# One edge, atomically: delete it, drop it from every episode's+# ``entity_edges``, drop every tombstone's mentions, then delete each entity+# among its endpoints and the entities those mentions named that is left+# with no fact and no mention. Ids are captured before any ``DELETE``:+# FalkorDB cannot read a deleted element (FalkorDB #1393). A community+# membership (``HAS_MEMBER``) does not keep an entity: communities are+# rebuilt from the facts.+_DELETE_EDGE_QUERY = """+MATCH (source)-[e:MENTIONS|RELATES_TO|HAS_MEMBER {uuid: $uuid}]->(target)+WHERE e.group_id = $group_id OR e.group_id IS NULL+WITH e, e.uuid AS uuid, source.uuid AS source_uuid, target.uuid AS target_uuid+DELETE e+WITH uuid, source_uuid, target_uuid+OPTIONAL MATCH (ep:Episodic)+WHERE uuid IN coalesce(ep.entity_edges, [])+WITH uuid, source_uuid, target_uuid, collect(ep) AS citing+FOREACH (ep IN citing |+ SET ep.entity_edges = [x IN ep.entity_edges WHERE x <> uuid])+WITH uuid, source_uuid, target_uuid+OPTIONAL MATCH (tomb:Episodic)-[m:MENTIONS]->(mentioned:Entity)+WHERE tomb.hard_deleted_at IS NOT NULL+WITH uuid, source_uuid, target_uuid, collect(m) AS mentions,+ collect(DISTINCT mentioned.uuid) AS mentioned_uuids+FOREACH (m IN mentions | DELETE m)+WITH uuid, [source_uuid, target_uuid] + mentioned_uuids AS candidates+OPTIONAL MATCH (candidate:Entity)+WHERE candidate.uuid IN candidates+OPTIONAL MATCH (candidate)-[link:RELATES_TO|MENTIONS]-()+WITH uuid, candidate, count(link) AS links+WITH uuid,+ collect(CASE WHEN links = 0 THEN candidate END) AS orphans,+ collect(CASE WHEN links = 0 THEN candidate.uuid END) AS orphan_uuids+FOREACH (orphan IN orphans | DETACH DELETE orphan)+RETURN uuid, orphan_uuids AS deleted_entities+"""diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_orphans_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_orphans_test.pynew file mode 100644index 0000000000..6b33f198cd--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_orphans_test.py@@ -0,0 +1,169 @@+"""Unit tests for ``recall_orphans.purge``, a hard forget's last step,+against a mock driver: the tombstones, the per-edge delete, and that every+step before the delete is safe to repeat.++The live run is ``recall_hard_forget_integration_test.py``.+"""++import json+from unittest.mock import AsyncMock++import pytest++from . import recall_orphans+from .memory_model import ForgetResult, MemoryForgetFailureCode+from .scope import MemoryScope++_SCOPE = MemoryScope.for_user("user-abc")+_NOW = "2026-09-26T12:00:00+00:00"+_CLEANUP = MemoryForgetFailureCode.CLEANUP_ERROR+_ENVELOPE = json.dumps(+ {"content": "Alice works on Atlas", "provenance": "session:s1#msg:3"}+)+++def _driver(*results) -> AsyncMock:+ driver = AsyncMock()+ driver.execute_query.side_effect = [+ r if isinstance(r, Exception) else (r, [], None) for r in results+ ]+ return driver+++def _deleted(uuid: str, *entities: str) -> list[dict]:+ return [{"uuid": uuid, "deleted_entities": list(entities)}]+++class TestPurge:+ @pytest.mark.asyncio+ async def test_tombstones_then_deletes_each_edge(self) -> None:+ driver = _driver(+ [+ {"uuid": "ep-chat", "content": "Alice works on Atlas"},+ {"uuid": "ep-memory", "content": _ENVELOPE},+ ],+ [{"uuid": "ep-chat"}, {"uuid": "ep-memory"}],+ _deleted("u1", "alice"),+ _deleted("u2"),+ )+ result = ForgetResult(redacted_episodes=["ep-chat", "ep-memory", "ep-kept"])++ await recall_orphans.purge(driver, _SCOPE.group_id, ["u1", "u2"], _NOW, result)++ assert result.deleted == ["u1", "u2"] and result.failures == []+ assert result.tombstoned_episodes == ["ep-chat", "ep-memory"]+ assert result.redacted_episodes == [+ "ep-kept"+ ], "a tombstone is not listed twice"+ assert result.deleted_entities == ["alice"]+ citing, tombstone, *deletes = driver.execute_query.await_args_list+ assert citing.args == (recall_orphans._CITING_EPISODES_QUERY,)+ assert citing.kwargs == {"uuids": ["u1", "u2"]}+ assert tombstone.args == (recall_orphans._TOMBSTONE_QUERY,)+ assert tombstone.kwargs == {+ "uuids": ["u1", "u2"],+ "provenance": [["ep-memory", "session:s1#msg:3"]],+ "now": _NOW,+ }+ assert [(d.args, d.kwargs) for d in deletes] == [+ (+ (recall_orphans._DELETE_EDGE_QUERY,),+ {"uuid": u, "group_id": _SCOPE.group_id},+ )+ for u in ("u1", "u2")+ ]++ @pytest.mark.asyncio+ @pytest.mark.parametrize("failing", [0, 1], ids=["citing-read", "tombstone"])+ async def test_a_failed_tombstone_step_deletes_nothing(self, failing: int) -> None:+ """The edges stay, so forgetting again finds them and finishes."""+ results: list = [[], []]+ results[failing] = RuntimeError("down")+ driver = _driver(*results)+ result = ForgetResult()++ await recall_orphans.purge(driver, _SCOPE.group_id, ["u1", "u2"], _NOW, result)++ assert result.deleted == []+ assert [(f.uuid, f.code) for f in result.failures] == [+ ("u1", _CLEANUP),+ ("u2", _CLEANUP),+ ]+ queries = [call.args[0] for call in driver.execute_query.await_args_list]+ assert recall_orphans._DELETE_EDGE_QUERY not in queries++ @pytest.mark.asyncio+ async def test_a_failed_delete_is_that_edges_cleanup_error(self) -> None:+ driver = _driver([], [], _deleted("u1"), RuntimeError("down"))+ result = ForgetResult()++ await recall_orphans.purge(driver, _SCOPE.group_id, ["u1", "u2"], _NOW, result)++ assert result.deleted == ["u1"]+ assert [(f.uuid, f.code) for f in result.failures] == [("u2", _CLEANUP)]++ @pytest.mark.asyncio+ async def test_an_edge_gone_before_its_delete_is_a_no_match(self) -> None:+ driver = _driver([], [], [])+ result = ForgetResult()++ await recall_orphans.purge(driver, _SCOPE.group_id, ["u1"], _NOW, result)++ assert result.deleted == []+ assert [f.code for f in result.failures] == [MemoryForgetFailureCode.NO_MATCH]+++class TestTombstoneQuery:+ def test_an_episode_is_emptied_only_when_no_remaining_edge_cites_it(+ self,+ ) -> None:+ """Ownership, whatever the citing edge's status (a retracted edge kept+ for audit still needs its source), checked in the writing query."""+ query = recall_orphans._TOMBSTONE_QUERY+ assert (+ "AND (ref.uuid IN ep.entity_edges OR ep.uuid IN coalesce(ref.episodes, []))"+ in query+ )+ assert "WHERE NOT ref.uuid IN $uuids" in query+ assert "WHERE refs = 0" in query+ assert "status" not in query and "expired_at" not in query++ def test_a_tombstone_keeps_what_names_its_session(self) -> None:+ query = recall_orphans._TOMBSTONE_QUERY+ assert "ep.content = ''" in query+ assert "DELETE" not in query, "an episode is never deleted"+ for kept in ("ep.name", "ep.source_description", "ep.entity_edges"):+ assert f"{kept} =" not in query+ assert "ep.provenance = coalesce(" in query+ assert "[p IN $provenance WHERE p[0] = ep.uuid | p[1]][0]" in query++ def test_a_repeat_keeps_the_first_stamps(self) -> None:+ query = recall_orphans._TOMBSTONE_QUERY+ assert "ep.redacted_at = coalesce(ep.redacted_at, $now)" in query+ assert "ep.hard_deleted_at = coalesce(ep.hard_deleted_at, $now)" in query+++class TestDeleteEdgeQuery:+ def test_one_query_deletes_the_edge_and_what_it_alone_kept(self) -> None:+ query = recall_orphans._DELETE_EDGE_QUERY+ assert query.count("MATCH (source)-[e:MENTIONS|RELATES_TO|HAS_MEMBER") == 1+ assert "WHERE e.group_id = $group_id OR e.group_id IS NULL" in query+ assert "SET ep.entity_edges = [x IN ep.entity_edges WHERE x <> uuid]" in query+ assert "WHERE tomb.hard_deleted_at IS NOT NULL" in query+ assert "FOREACH (m IN mentions | DELETE m)" in query+ assert "FOREACH (orphan IN orphans | DETACH DELETE orphan)" in query+ assert "RETURN uuid, orphan_uuids AS deleted_entities" in query++ def test_ids_are_read_before_anything_is_deleted(self) -> None:+ """FalkorDB cannot read a deleted element."""+ query = recall_orphans._DELETE_EDGE_QUERY+ capture = query.index(+ "WITH e, e.uuid AS uuid, source.uuid AS source_uuid, "+ "target.uuid AS target_uuid"+ )+ assert capture < query.index("DELETE e\n")++ def test_an_entity_goes_only_with_no_fact_and_no_mention(self) -> None:+ query = recall_orphans._DELETE_EDGE_QUERY+ assert "OPTIONAL MATCH (candidate)-[link:RELATES_TO|MENTIONS]-()" in query+ assert "CASE WHEN links = 0 THEN candidate END" in querydiff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_recheck.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_recheck.pynew file mode 100644index 0000000000..f61a013f25--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_recheck.py@@ -0,0 +1,81 @@+"""The last graph read before recall shows what it found.++Warm context and ``memory_search`` read facts (``recall.search_facts``) and+recent episodes (``recall.recent_episodes``) side by side and render them+once both reads are done; the fact search can take seconds (warm context+reranks with a cross-encoder), and a forget can answer in the meantime. So+right before rendering they call ``recheck``, which checks every fact and+episode they are about to show again, by uuid, in one statement, and keeps+the facts still live and the episodes still recallable. Every item shown+passed that check, its own last graph read. A forget writes each fact's+marker before anything else, so a check that began after the marker+committed drops the fact and every episode citing it; a response whose+check began earlier can still carry them while it is delivered (rendering,+scheduling, network), and no later read will.+"""++from graphiti_core.driver.driver import GraphDriver+from graphiti_core.edges import EntityEdge+from graphiti_core.nodes import EpisodicNode++from . import recall+from .scope import MemoryScope+++async def recheck(+ scope: MemoryScope,+ facts: list[EntityEdge],+ episodes: list[EpisodicNode],+ *,+ include_tentative: bool = True,+) -> tuple[list[EntityEdge], list[EpisodicNode]]:+ """``facts`` still live and ``episodes`` still recallable, each in its+ order: one statement on the driver the fact search used, so no forget+ lands between the check of one item and another's."""+ if not facts and not episodes:+ return facts, episodes+ client = await recall.get_graphiti_client(scope.group_id)+ live, recallable = await _still_shown(+ client.driver, facts, episodes, include_tentative+ )+ return (+ [fact for fact in facts if fact.uuid in live],+ [episode for episode in episodes if episode.uuid in recallable],+ )+++async def _still_shown(+ driver: GraphDriver,+ facts: list[EntityEdge],+ episodes: list[EpisodicNode],+ include_tentative: bool,+) -> tuple[set[str], set[str]]:+ """The uuids of ``facts`` still live and of ``episodes`` still+ recallable."""+ result = await driver.execute_query(+ _recheck_query(include_tentative),+ fact_uuids=[fact.uuid for fact in facts],+ episode_uuids=[episode.uuid for episode in episodes],+ )+ rows = result[0] if result else []+ # No row reads as nothing found: nothing is shown unchecked.+ row = rows[0] if rows else {"facts": [], "episodes": []}+ return set(row["facts"]), set(row["episodes"])+++def _recheck_query(include_tentative: bool) -> str:+ """The live-fact and recallable-episode tests on the given uuids, in one+ statement returning one row."""+ live = recall.live_fact_predicate("fact", include_tentative=include_tentative)+ return (+ recall.forgotten_facts_clause()+ + f"""+OPTIONAL MATCH ()-[fact:RELATES_TO]->()+WHERE fact.uuid IN $fact_uuids AND {live}+WITH forgotten, collect(DISTINCT fact.uuid) AS facts+OPTIONAL MATCH (episode:Episodic)+WHERE episode.uuid IN $episode_uuids+ AND {recall.recallable_episode_predicate("episode")}+RETURN facts, collect(DISTINCT episode.uuid) AS episodes+"""+ )diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_recheck_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_recheck_test.pynew file mode 100644index 0000000000..a7440ae21e--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_recheck_test.py@@ -0,0 +1,123 @@+"""Unit tests for ``recall_recheck``: what a read is about to show is checked+again by uuid, facts and episodes in one statement, and only what is still+live or recallable is kept.++The live runs, a forget answering while warm context's cross-encoder or the+``memory_search`` search is paused, or once warm context's fact check has+passed, are in ``recall_inflight_read_integration_test.py``.+"""++from datetime import datetime, timezone+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from graphiti_core.edges import EntityEdge+from graphiti_core.nodes import EpisodeType, EpisodicNode++from . import recall, recall_recheck+from .scope import MemoryScope++_NOW = datetime(2026, 9, 26, 12, 0, tzinfo=timezone.utc)+_SCOPE = MemoryScope.for_user("user-abc")+++def _fact(uuid: str) -> EntityEdge:+ return EntityEdge(+ uuid=uuid,+ group_id=_SCOPE.group_id,+ source_node_uuid="alice",+ target_node_uuid="atlas",+ created_at=_NOW,+ name="works_on",+ fact=f"fact {uuid}",+ )+++def _episode(uuid: str) -> EpisodicNode:+ return EpisodicNode(+ uuid=uuid,+ name=uuid,+ group_id=_SCOPE.group_id,+ source=EpisodeType.text,+ source_description="chat",+ content=f"episode {uuid}",+ created_at=_NOW,+ valid_at=_NOW,+ )+++def _client(still: set[str]) -> MagicMock:+ """The scope's graphiti client, whose driver finds only the uuids in+ ``still``, in one row as the statement returns them."""++ async def execute_query(+ query: str, *, fact_uuids: list[str], episode_uuids: list[str]+ ):+ facts = [uuid for uuid in fact_uuids if uuid in still]+ episodes = [uuid for uuid in episode_uuids if uuid in still]+ return [{"facts": facts, "episodes": episodes}], [], None++ client = MagicMock()+ client.driver.execute_query = AsyncMock(side_effect=execute_query)+ return client+++async def _recheck(client: MagicMock, *args, **kwargs):+ with patch.object(recall, "get_graphiti_client", AsyncMock(return_value=client)):+ return await recall_recheck.recheck(_SCOPE, *args, **kwargs)+++class TestRecheck:+ @pytest.mark.asyncio+ async def test_keeps_only_what_is_still_live_or_recallable_in_order(+ self,+ ) -> None:+ facts = [_fact("f1"), _fact("f2"), _fact("f3")]+ episodes = [_episode("ep1"), _episode("ep2")]++ kept, recalled = await _recheck(_client({"f3", "f1", "ep2"}), facts, episodes)++ assert [f.uuid for f in kept] == ["f1", "f3"]+ assert [e.uuid for e in recalled] == ["ep2"]++ @pytest.mark.asyncio+ async def test_facts_and_episodes_are_checked_in_one_statement(self) -> None:+ """No forget can land between the check of a fact and of an episode+ (a split check let one through: ``r6-read-split-check.py``)."""+ client = _client(set())++ await _recheck(+ client, [_fact("f1")], [_episode("ep1")], include_tentative=False+ )++ [call] = client.driver.execute_query.await_args_list+ query = call.args[0]+ assert query.startswith(recall.forgotten_facts_clause())+ assert recall.live_fact_predicate("fact", include_tentative=False) in query+ assert recall.recallable_episode_predicate("episode") in query+ assert call.kwargs == {"fact_uuids": ["f1"], "episode_uuids": ["ep1"]}++ @pytest.mark.asyncio+ async def test_nothing_to_show_reads_nothing(self) -> None:+ get_client = AsyncMock()+ with patch.object(recall, "get_graphiti_client", get_client):+ assert await recall_recheck.recheck(_SCOPE, [], []) == ([], [])++ get_client.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_no_answer_shows_nothing(self) -> None:+ client = MagicMock()+ client.driver.execute_query = AsyncMock(return_value=([], [], None))++ assert await _recheck(client, [_fact("f1")], [_episode("ep1")]) == ([], [])++ @pytest.mark.asyncio+ async def test_a_failed_read_raises_for_the_caller_to_degrade(self) -> None:+ """Warm context then shows no context and memory_search reports+ itself unavailable: nothing is shown unchecked."""+ client = _client(set())+ client.driver.execute_query.side_effect = RuntimeError("falkordb down")++ with pytest.raises(RuntimeError):+ await _recheck(client, [_fact("f1")], [])diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_render.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_render.pynew file mode 100644index 0000000000..3ec585cace--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_render.py@@ -0,0 +1,88 @@+"""How recalled memory is written into a prompt or a tool result.++The recall policy (``recall.py``) decides what may be read back; this module+writes out what it returns. A fact line always says when the fact holds, so+an ended or retired fact never reads as a current one.+"""++from graphiti_core.edges import EntityEdge+from graphiti_core.nodes import EpisodicNode+from pydantic import BaseModel, ValidationError++from .memory_model import MemoryStatus+from .recall import fact_status, is_live++# Scope of an episode that is not a ``MemoryEnvelope`` (plain conversation).+GLOBAL_SCOPE = "real:global"+# Episode bodies are cut to this many characters when rendered.+EPISODE_DISPLAY_CHARS = 500++_RETIRED_STATUSES = frozenset(+ status.value+ for status in (+ MemoryStatus.superseded,+ MemoryStatus.contradicted,+ MemoryStatus.retracted,+ )+)+++def render(fact: EntityEdge) -> str:+ """One recall line for ``fact``: its text and when it holds.++ A live fact shows its valid-time interval (``fact_validity``), which may+ already have ended: recall still returns a fact that held in the past,+ and its end date keeps it from reading as current. A retired fact+ (expired, or in a status recall skips) is labelled with how and when it+ was retired instead; recall never returns one, the label keeps any other+ caller honest.+ """+ text = fact_text(fact)+ if is_live(fact):+ valid_from, valid_to = fact_validity(fact)+ return f"{text} (valid: {valid_from} — {valid_to})"+ retired_at = str(fact.expired_at) if fact.expired_at else "at an unknown time"+ return f"{text} ({_retired_label(fact)} {retired_at})"+++def fact_text(fact: EntityEdge) -> str:+ """The fact sentence, or the relation name when extraction left none."""+ return fact.fact or fact.name+++def fact_validity(fact: EntityEdge) -> tuple[str, str]:+ """``(valid_from, valid_to)`` in valid time.++ ``valid_to`` is "present" only while no ``invalid_at`` is known; a past+ one is shown as it is, so an ended fact reads as history, not as now.+ """+ valid_from = str(fact.valid_at) if fact.valid_at else "unknown"+ valid_to = str(fact.invalid_at) if fact.invalid_at else "present"+ return valid_from, valid_to+++def render_episode(episode: EpisodicNode) -> str:+ """``[created_at] body``, the body cut to ``EPISODE_DISPLAY_CHARS``."""+ return f"[{episode.created_at}] {episode.content[:EPISODE_DISPLAY_CHARS]}"+++def episode_scope(episode: EpisodicNode) -> str:+ """The ``MemoryEnvelope`` scope an episode was stored under.++ An episode that is not an envelope (plain conversation, or JSON that is+ not an object) belongs to ``GLOBAL_SCOPE``. Reads the full body: an+ envelope cut to display length is no longer valid JSON.+ """+ try:+ return _EnvelopeScope.model_validate_json(episode.content).scope+ except ValidationError:+ return GLOBAL_SCOPE+++class _EnvelopeScope(BaseModel):+ scope: str = GLOBAL_SCOPE+++def _retired_label(fact: EntityEdge) -> str:+ status = fact_status(fact)+ return status if status in _RETIRED_STATUSES else "expired"diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_render_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_render_test.pynew file mode 100644index 0000000000..93a169b653--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_render_test.py@@ -0,0 +1,139 @@+"""Unit tests for how recalled memory is written out (``recall_render.py``)."""++from datetime import datetime, timezone++import pytest+from graphiti_core.edges import EntityEdge+from graphiti_core.nodes import EpisodeType, EpisodicNode++from . import recall_render+from .memory_model import MemoryEnvelope, MemoryKind, SourceKind+from .scope import MemoryScope++_NOW = datetime(2026, 9, 26, 12, 0, tzinfo=timezone.utc)+_SCOPE = MemoryScope.for_user("user-abc")+++def _fact(+ *,+ status: str = "active",+ expired_at: datetime | None = None,+ valid_at: datetime | None = None,+ invalid_at: datetime | None = None,+ fact: str = "Alice works on Atlas",+) -> EntityEdge:+ return EntityEdge(+ group_id=_SCOPE.group_id,+ source_node_uuid="alice",+ target_node_uuid="atlas",+ created_at=_NOW,+ name="works_on",+ fact=fact,+ expired_at=expired_at,+ valid_at=valid_at,+ invalid_at=invalid_at,+ attributes={"status": status},+ )+++def _episode(content: str) -> EpisodicNode:+ return EpisodicNode(+ name="ep1",+ group_id=_SCOPE.group_id,+ source=EpisodeType.text,+ source_description="chat",+ content=content,+ created_at=_NOW,+ valid_at=_NOW,+ )+++class TestRender:+ def test_live_fact_shows_its_validity(self) -> None:+ fact = _fact(valid_at=datetime(2025, 1, 1, tzinfo=timezone.utc))+ assert recall_render.render(fact) == (+ "Alice works on Atlas (valid: 2025-01-01 00:00:00+00:00 — present)"+ )++ def test_live_fact_whose_valid_time_ended_reads_as_history(self) -> None:+ """Not expired, so recall still returns it: it held until+ ``invalid_at``. The interval shows that end instead of "present",+ and no retirement label is given, because it was never retired."""+ fact = _fact(+ valid_at=datetime(2024, 1, 1, tzinfo=timezone.utc),+ invalid_at=datetime(2025, 1, 1, tzinfo=timezone.utc),+ )+ assert recall_render.render(fact) == (+ "Alice works on Atlas "+ "(valid: 2024-01-01 00:00:00+00:00 — 2025-01-01 00:00:00+00:00)"+ )++ @pytest.mark.parametrize("status", ["retracted", "superseded", "contradicted"])+ def test_retired_fact_is_labelled_never_present(self, status: str) -> None:+ fact = _fact(status=status, expired_at=_NOW)+ rendered = recall_render.render(fact)+ assert rendered == f"Alice works on Atlas ({status} 2026-09-26 12:00:00+00:00)"+ assert "present" not in rendered++ def test_expired_fact_with_a_live_status_is_labelled_expired(self) -> None:+ # What graphiti's own contradiction handling leaves behind: expired,+ # status never changed.+ fact = _fact(status="active", expired_at=_NOW)+ assert recall_render.render(fact) == (+ "Alice works on Atlas (expired 2026-09-26 12:00:00+00:00)"+ )++ def test_retired_status_without_a_timestamp(self) -> None:+ fact = _fact(status="retracted")+ assert recall_render.render(fact) == (+ "Alice works on Atlas (retracted at an unknown time)"+ )++ def test_fact_text_falls_back_to_the_relation_name(self) -> None:+ assert recall_render.fact_text(_fact(fact="")) == "works_on"+++class TestRenderEpisode:+ def test_timestamp_and_body(self) -> None:+ assert recall_render.render_episode(_episode("talked about coffee")) == (+ "[2026-09-26 12:00:00+00:00] talked about coffee"+ )++ def test_body_cut_to_display_length(self) -> None:+ body = "x" * recall_render.EPISODE_DISPLAY_CHARS+ assert recall_render.render_episode(_episode("x" * 1000)) == (+ f"[2026-09-26 12:00:00+00:00] {body}"+ )+++class TestEpisodeScope:+ @pytest.mark.parametrize(+ "content",+ ["plain conversation text", "[1, 2, 3]", '"just a string"', "null"],+ )+ def test_non_envelope_bodies_are_global(self, content: str) -> None:+ scope = recall_render.episode_scope(_episode(content))+ assert scope == recall_render.GLOBAL_SCOPE++ def test_envelope_scope_is_read(self) -> None:+ envelope = MemoryEnvelope(content="project note", scope="project:crm")+ episode = _episode(envelope.model_dump_json())+ assert recall_render.episode_scope(episode) == "project:crm"++ def test_envelope_without_scope_is_global(self) -> None:+ episode = _episode('{"content": "x"}')+ assert recall_render.episode_scope(episode) == "real:global"++ def test_long_envelope_is_parsed_whole(self) -> None:+ """An envelope longer than the display cut is still parsed: scoping+ the truncated body would leak a project memory into global recall."""+ envelope = MemoryEnvelope(+ content="x" * 600,+ source_kind=SourceKind.user_asserted,+ scope="project:crm",+ memory_kind=MemoryKind.fact,+ )+ body = envelope.model_dump_json()+ assert len(body) > recall_render.EPISODE_DISPLAY_CHARS++ assert recall_render.episode_scope(_episode(body)) == "project:crm"diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_reteach_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_reteach_integration_test.pynew file mode 100644index 0000000000..4a4991420c--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_reteach_integration_test.py@@ -0,0 +1,244 @@+"""Facts stated again after a forget, several in one episode, through the+production worker against a live FalkorDB.++graphiti's model may call each new statement a duplicate of a different+edge between the same entities, forgotten or live. A forgotten one is never+a duplicate (``recall_ingest.ForgetAwareLLMClient``), so each statement+merged into a forgotten edge is saved as its own live edge, and one merged+into a live edge stays merged, as graphiti decided: no statement is lost or+saved twice, and graphiti extracts the episode once. Reproduced first by an+independent validation (``r3-ingestion-independent.py``, ``multi_merge``;+``r4-ingestion-repair-extra.py``, the mixed forgotten and live pair;+``r5-ingestion-independent.py``, ``corrupt_partial``, a dedup prompt the+guard cannot read in full).++Run with FalkorDB reachable (see ``conftest.py``)::++ poetry run pytest -m integration backend/copilot/graphiti/recall_reteach_integration_test.py+"""++import ast+import re+from collections.abc import AsyncIterator+from typing import Any+from unittest.mock import AsyncMock, patch++import pytest+import pytest_asyncio+from graphiti_core.prompts.dedupe_edges import EdgeDuplicate+from graphiti_core.prompts.models import Message+from pydantic import BaseModel+from pytest_mock import MockerFixture++from .falkordb_driver import AutoGPTFalkorDriver+from .recall import FORGOTTEN_FACT+from .recall_forget import retract+from .recall_ingest import ForgetAwareLLMClient+from .recall_integration_fixtures import (+ ALICE,+ BuildClient,+ Fact,+ edge_row,+ episode_row,+ ingest_through_the_worker,+ live_facts,+ model_of,+ patch_recall_boundaries,+ recalled_episodes,+ scripted_responses,+ stop_ingestion_workers,+)+from .scope import MemoryScope++_BUDGET: Fact = ("Alice", "Atlas", "Alice owns the Atlas budget")+_ASSIGNED: Fact = ("Alice", "Atlas", "Alice is assigned to work on the Atlas project")+_MANAGES: Fact = ("Alice", "Atlas", "Alice manages the Atlas budget")+_SECTION = re.compile(r"<EXISTING FACTS>\s*(.*?)\s*</EXISTING FACTS>", re.DOTALL)+_NEW_FACT = re.compile(r"<NEW FACT>\s*(.*?)\s*</NEW FACT>", re.DOTALL)++[email protected](autouse=True)+def boundaries(mocker, scope_graph, stub_graphiti_client):+ patch_recall_boundaries(mocker, scope_graph[0], stub_graphiti_client)+++@pytest_asyncio.fixture(loop_scope="function")+async def ingest_worker_cleanup(mocker: MockerFixture) -> AsyncIterator[None]:+ mocker.patch(+ "backend.copilot.graphiti.ingest.ensure_scope_scheduled",+ AsyncMock(return_value=None),+ )+ yield+ await stop_ingestion_workers()+++def _responses(facts: list[Fact], valid_at: str) -> dict[str, dict]:+ responses = scripted_responses(facts)+ for edge in responses["ExtractedEdges"]["edges"]:+ edge["valid_at"] = valid_at+ return responses+++async def _learn(+ driver: AutoGPTFalkorDriver, scope: MemoryScope, build: BuildClient, facts+) -> dict[str, str]:+ """``facts`` through the worker: each live fact's sentence by uuid."""+ client = build(driver, _responses(facts, "2026-01-01T00:00:00Z"))+ await ingest_through_the_worker(driver, scope, client, facts, session_id="s-1")+ return await live_facts(driver)+++async def _say_again(+ driver: AutoGPTFalkorDriver,+ scope: MemoryScope,+ build: BuildClient,+ facts: list[Fact],+) -> tuple[str, list[str]]:+ """``facts`` in a new chat turn, graphiti's model calling a work+ statement a duplicate of the work fact and a budget statement one of the+ budget fact, whether forgotten or live: the new episode's uuid and the+ answer each model call asked for."""+ client = build(driver, _responses(facts, "2026-06-01T00:00:00Z"))+ model = model_of(client)+ answer = model._generate_response++ async def resolve(messages, response_model=None, *args: Any, **kwargs: Any):+ if response_model is not None and response_model.__name__ == "EdgeDuplicate":+ return _merge_by_topic(messages[-1].content)+ return await answer(messages, response_model, *args, **kwargs)++ with patch.object(model, "_generate_response", side_effect=resolve) as calls:+ episode = await ingest_through_the_worker(+ driver, scope, client, facts, session_id="s-2"+ )+ return episode, [call.args[1].__name__ for call in calls.await_args_list]+++def _merge_by_topic(prompt: str) -> dict[str, list[int]]:+ """The duplicate a model would name: the existing fact on the new+ statement's topic, the forgotten one read by its audit topic."""+ found = _NEW_FACT.search(prompt)+ budget = found is not None and "budget" in found.group(1)+ section = _SECTION.search(prompt)+ existing = ast.literal_eval(section.group(1)) if section else []+ wanted = _BUDGET[2] if budget else ALICE[2]+ same = [c["idx"] for c in existing if c["fact"] == wanted]+ forgotten = [c["idx"] for c in existing if c["fact"] == FORGOTTEN_FACT]+ return {"duplicate_facts": (same or forgotten)[:1], "contradicted_facts": []}+++async def _audits(driver: AutoGPTFalkorDriver, uuids) -> dict[str, dict[str, Any]]:+ return {uuid: await edge_row(driver, uuid) for uuid in uuids}+++async def test_two_forgotten_facts_said_again_both_come_back(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup+) -> None:+ driver, scope = scope_graph+ forgotten = list(+ await _learn(driver, scope, stub_graphiti_client, [ALICE, _BUDGET])+ )+ await retract(scope, forgotten)+ audits = await _audits(driver, forgotten)++ episode, asked = await _say_again(+ driver, scope, stub_graphiti_client, [_ASSIGNED, _MANAGES]+ )++ live = await live_facts(driver)+ assert sorted(live.values()) == sorted([_ASSIGNED[2], _MANAGES[2]])+ assert await _audits(driver, forgotten) == audits, "a forgotten edge changed"+ assert sorted((await episode_row(driver, episode))["entity_edges"]) == sorted(live)+ assert episode in await recalled_episodes(scope)+ assert asked.count("ExtractedEdges") == 1, "no second extraction"+++async def test_a_statement_merged_into_a_live_fact_is_not_saved_twice(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup+) -> None:+ """Work is forgotten, the budget fact live: the work statement comes+ back as its own fact, the budget paraphrase stays merged into the live+ budget fact, and no third fact appears."""+ driver, scope = scope_graph+ learned = await _learn(driver, scope, stub_graphiti_client, [ALICE, _BUDGET])+ [work] = [uuid for uuid, fact in learned.items() if fact == ALICE[2]]+ [budget] = [uuid for uuid, fact in learned.items() if fact == _BUDGET[2]]+ await retract(scope, [work])+ audit = await edge_row(driver, work)++ episode, asked = await _say_again(+ driver, scope, stub_graphiti_client, [_ASSIGNED, _MANAGES]+ )++ live = await live_facts(driver)+ assert sorted(live.values()) == sorted([_BUDGET[2], _ASSIGNED[2]])+ assert episode in (await edge_row(driver, budget))["episodes"], "merged"+ assert await edge_row(driver, work) == audit+ assert asked.count("ExtractedEdges") == 1, "no second extraction"+++async def test_an_episode_that_only_mentions_the_entity_adds_no_fact(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup+) -> None:+ driver, scope = scope_graph+ [alice] = await _learn(driver, scope, stub_graphiti_client, [ALICE])+ await retract(scope, [alice])+ audit = await edge_row(driver, alice)+ client = stub_graphiti_client(driver, scripted_responses([]))++ episode = await ingest_through_the_worker(+ driver, scope, client, [], session_id="s-2", body="Alice called to say hello."+ )++ assert await live_facts(driver) == {}+ assert await edge_row(driver, alice) == audit+ assert episode in await recalled_episodes(scope)+++async def test_a_dedup_prompt_the_guard_cannot_read_saves_the_statement_as_new(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup+) -> None:+ """The existing-facts tags renamed on the way to the guard, the model+ naming the forgotten edge a duplicate: the statement is saved as its own+ live fact and the forgotten edge keeps its marker and audit copies."""+ driver, scope = scope_graph+ [alice] = await _learn(driver, scope, stub_graphiti_client, [ALICE])+ await retract(scope, [alice])+ audit = await edge_row(driver, alice)+ responses = _responses([_ASSIGNED], "2026-06-01T00:00:00Z")+ responses["EdgeDuplicate"] = {"duplicate_facts": [0], "contradicted_facts": []}+ client = stub_graphiti_client(driver, responses)+ guard = ForgetAwareLLMClient.generate_response++ async def renamed(+ self: ForgetAwareLLMClient,+ messages: list[Message],+ response_model: type[BaseModel] | None = None,+ *args: Any,+ **kwargs: Any,+ ) -> dict[str, Any]:+ if response_model is EdgeDuplicate:+ messages = [+ m.model_copy(+ update={"content": m.content.replace("EXISTING FACTS", "RENAMED")}+ )+ for m in messages+ ]+ return await guard(self, messages, response_model, *args, **kwargs)++ with patch.object(ForgetAwareLLMClient, "generate_response", renamed):+ episode = await ingest_through_the_worker(+ driver, scope, client, [_ASSIGNED], session_id="s-2"+ )++ assert list((await live_facts(driver)).values()) == [_ASSIGNED[2]]+ assert await edge_row(driver, alice) == audit, "the forgotten edge changed"+ assert episode in await recalled_episodes(scope)diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_scrub_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_scrub_integration_test.pynew file mode 100644index 0000000000..884281741e--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_scrub_integration_test.py@@ -0,0 +1,192 @@+"""What graphiti read out of a forgotten sentence onto entities, through the+production worker against a live FalkorDB.++graphiti keeps a typed attribute an extraction filled from the sentence+(``Person.role``) and a summary on every entity an episode mentions, and+sends both to its attribute, summary and entity resolution prompts. A forget+clears them on every entity the fact joins or its episodes mention+(``recall_hide.scrub_entities``). After it, the only places the sentence+remains are the audit copies: the episode it came from and the edge's+``*_redacted`` fields (plus what the synthetic case seeds into audit-only+metadata). Reproduced first by an independent validation+(``r3-ingestion-independent.py``, ``typed_attribute_scrub`` and+``synthetic_scrub``).++Run with FalkorDB reachable (see ``conftest.py``)::++ poetry run pytest -m integration backend/copilot/graphiti/recall_scrub_integration_test.py+"""++import json+import uuid+from collections.abc import AsyncIterator+from typing import Any+from unittest.mock import AsyncMock, patch++import pytest+import pytest_asyncio+from pytest_mock import MockerFixture++from .falkordb_driver import AutoGPTFalkorDriver+from .recall_forget import retract+from .recall_integration_fixtures import (+ ALICE,+ BuildClient,+ Fact,+ ingest_through_the_worker,+ live_facts,+ model_of,+ patch_recall_boundaries,+ rows,+ scripted_responses,+ sentence_properties,+ stop_ingestion_workers,+)+from .scope import MemoryScope++# The ``ExtractedEntities`` id of ``Person``: 0 is plain ``Entity``, then+# ``types.ENTITY_TYPES`` in order.+_PERSON = 1+_AUDIT_ONLY = {"Episodic.content", "RELATES_TO.fact_redacted"}++[email protected](autouse=True)+def boundaries(mocker, scope_graph, stub_graphiti_client):+ patch_recall_boundaries(mocker, scope_graph[0], stub_graphiti_client)+++@pytest_asyncio.fixture(loop_scope="function")+async def ingest_worker_cleanup(mocker: MockerFixture) -> AsyncIterator[None]:+ mocker.patch(+ "backend.copilot.graphiti.ingest.ensure_scope_scheduled",+ AsyncMock(return_value=None),+ )+ yield+ await stop_ingestion_workers()+++def _alice_a_person(facts: list[Fact], *, role: str | None) -> dict[str, dict]:+ """``facts`` extracted with Alice as a ``Person`` whose ``role`` the+ model fills with ``role``."""+ responses = scripted_responses(facts)+ if not facts:+ names = [{"name": "Alice", "entity_type_id": _PERSON}]+ responses["ExtractedEntities"] = {"extracted_entities": names}+ for entity in responses["ExtractedEntities"]["extracted_entities"]:+ if entity["name"] == "Alice":+ entity["entity_type_id"] = _PERSON+ responses["Person"] = {"role": role} if role else {}+ return responses+++async def _learn_then_forget(+ driver: AutoGPTFalkorDriver, scope: MemoryScope, build: BuildClient+) -> str:+ client = build(driver, _alice_a_person([ALICE], role=ALICE[2]))+ await ingest_through_the_worker(+ driver, scope, client, [ALICE], session_id="s-first"+ )+ [alice] = await live_facts(driver)+ return alice+++async def _hello(+ driver: AutoGPTFalkorDriver, scope: MemoryScope, build: BuildClient+) -> list[str]:+ """A later episode that only mentions Alice; every message graphiti sent+ its model for it."""+ client = build(driver, _alice_a_person([], role=None))+ model = model_of(client)+ answer = model._generate_response+ with patch.object(model, "_generate_response", side_effect=answer) as generate:+ await ingest_through_the_worker(+ driver,+ scope,+ client,+ [],+ session_id="s-hello",+ body="Alice called to say hello.",+ )+ return [m.content for call in generate.await_args_list for m in call.args[0]]+++async def test_a_typed_attribute_holding_the_sentence_is_cleared(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup+) -> None:+ driver, scope = scope_graph+ alice = await _learn_then_forget(driver, scope, stub_graphiti_client)+ assert "Entity.role" in await sentence_properties(driver, ALICE[2])++ await retract(scope, [alice])++ assert await sentence_properties(driver, ALICE[2]) == _AUDIT_ONLY+ sent = await _hello(driver, scope, stub_graphiti_client)+ assert sent, "graphiti asked its model nothing"+ assert [message for message in sent if ALICE[2] in message] == []+++async def test_everything_graphiti_keeps_about_the_sentence_is_cleared(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup+) -> None:+ """Every place graphiti can keep text, seeded with the sentence: entity+ attributes, a serialized ``attributes`` map, a summary on an entity the+ episode only mentions, a community, the edge's name and the episode's+ own metadata. Only audit copies and audit-only metadata keep it."""+ driver, scope = scope_graph+ alice = await _learn_then_forget(driver, scope, stub_graphiti_client)+ await _seed_everywhere(driver, scope, alice)++ await retract(scope, [alice])++ assert await sentence_properties(driver, ALICE[2]) == _AUDIT_ONLY | {+ "Episodic.name",+ "Episodic.source_description",+ "RELATES_TO.name_redacted",+ "Community.name",+ }+ mentioned = await rows(+ driver, "MATCH (n:Entity {name: 'MentionOnly'}) RETURN properties(n) AS p"+ )+ assert mentioned[0]["p"]["summary"] == ""+ sent = await _hello(driver, scope, stub_graphiti_client)+ assert sent and [message for message in sent if ALICE[2] in message] == []+++async def _seed_everywhere(+ driver: AutoGPTFalkorDriver, scope: MemoryScope, alice: str+) -> None:+ params: dict[str, Any] = {+ "uuid": alice,+ "sentence": ALICE[2],+ "attributes": json.dumps({"prior_fact": ALICE[2]}),+ "group_id": scope.group_id,+ "community": uuid.uuid4().hex,+ "member": uuid.uuid4().hex,+ "entity": uuid.uuid4().hex,+ "mention": uuid.uuid4().hex,+ }+ for query in _SEEDS:+ await driver.execute_query(query, **params)+++# Codex's ``synthetic_scrub`` seeds, one statement each.+_SEEDS = (+ "MATCH (n:Entity) SET n.role = $sentence, n.attributes = $attributes",+ """MATCH (ep:Episodic)+ WHERE $uuid IN ep.entity_edges+ SET ep.name = $sentence, ep.source_description = $sentence""",+ """MATCH (s)-[e:RELATES_TO {uuid: $uuid}]->()+ SET e.name = $sentence+ CREATE (c:Community {uuid: $community, name: $sentence,+ summary: $sentence, group_id: $group_id})+ CREATE (c)-[:HAS_MEMBER {uuid: $member, group_id: $group_id}]->(s)""",+ """MATCH (ep:Episodic)+ WHERE $uuid IN ep.entity_edges+ CREATE (n:Entity {uuid: $entity, group_id: $group_id,+ name: 'MentionOnly', summary: $sentence})+ CREATE (ep)-[:MENTIONS {uuid: $mention, group_id: $group_id}]->(n)""",+)diff --git a/autogpt_platform/backend/backend/copilot/graphiti/recall_test.py b/autogpt_platform/backend/backend/copilot/graphiti/recall_test.pynew file mode 100644index 0000000000..f490aaad93--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/recall_test.py@@ -0,0 +1,350 @@+"""Unit tests for the recall policy's read side (``recall.py``).++The live-graph counterparts are ``recall_integration_test.py`` and+``recall_forget_integration_test.py``; these pin the policy itself — which+facts are live or forgotten, which episodes stay recallable and what the fact+search is asked for — against mocks. The episode reads are pinned in+``recall_episodes_test.py``, rendering in ``recall_render_test.py``.+"""++from datetime import datetime, timezone+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from graphiti_core.driver.driver import GraphProvider+from graphiti_core.edges import EntityEdge+from graphiti_core.search.search_config import EdgeReranker, SearchResults+from graphiti_core.search.search_config_recipes import EDGE_HYBRID_SEARCH_CROSS_ENCODER+from graphiti_core.search.search_filters import ComparisonOperator+from graphiti_core.search.search_utils import DEFAULT_MIN_SCORE++from . import recall+from .scope import MemoryScope++_NOW = datetime(2026, 9, 26, 12, 0, tzinfo=timezone.utc)+_SCOPE = MemoryScope.for_user("user-abc")+++def _fact(+ uuid: str = "e1",+ *,+ status: str | None = "active",+ reason: str | None = None,+ expired_at: datetime | None = None,+ invalid_at: datetime | None = None,+ forgotten_at: str | None = None,+) -> EntityEdge:+ attributes = {+ "status": status,+ "expiration_reason": reason,+ "forgotten_at": forgotten_at,+ }+ return EntityEdge(+ uuid=uuid,+ group_id=_SCOPE.group_id,+ source_node_uuid="alice",+ target_node_uuid="atlas",+ created_at=_NOW,+ name="works_on",+ fact="Alice works on Atlas",+ expired_at=expired_at,+ invalid_at=invalid_at,+ attributes={k: v for k, v in attributes.items() if v is not None},+ )+++def _record(uuid: str) -> dict:+ """A forgotten edge as the graph returns it to graphiti's record reader."""+ return {+ "uuid": uuid,+ "source_node_uuid": "alice",+ "target_node_uuid": "atlas",+ "group_id": _SCOPE.group_id,+ "created_at": _NOW.isoformat(),+ "name": "[forgotten]",+ "fact": "[forgotten]",+ "episodes": [],+ "expired_at": _NOW.isoformat(),+ "valid_at": None,+ "invalid_at": None,+ "attributes": {"forgotten_at": _NOW.isoformat()},+ }+++class TestLiveFactPredicate:+ def test_cypher_matches_live_statuses_and_unset_expiry(self) -> None:+ assert recall.live_fact_predicate("e") == (+ "e.expired_at IS NULL AND e.forgotten_at IS NULL"+ " AND (e.status IS NULL OR e.status IN ['active', 'tentative'])"+ )++ def test_cypher_without_tentative(self) -> None:+ assert recall.live_fact_predicate("live", include_tentative=False) == (+ "live.expired_at IS NULL AND live.forgotten_at IS NULL"+ " AND (live.status IS NULL OR live.status IN ['active'])"+ )++ @pytest.mark.parametrize(+ ("status", "expired_at", "include_tentative", "live"),+ [+ ("active", None, True, True),+ (None, None, True, True), # predates MemoryFact: counts as active+ ("tentative", None, True, True),+ ("tentative", None, False, False),+ ("superseded", None, True, False),+ ("contradicted", None, True, False),+ ("retracted", None, True, False),+ ("active", _NOW, True, False), # graphiti-expired, status untouched+ ("retracted", _NOW, True, False),+ ],+ )+ def test_is_live(+ self,+ status: str | None,+ expired_at: datetime | None,+ include_tentative: bool,+ live: bool,+ ) -> None:+ fact = _fact(status=status, expired_at=expired_at)+ assert recall.is_live(fact, include_tentative=include_tentative) is live++ def test_a_forgotten_fact_is_never_live_whatever_its_status(self) -> None:+ """Only a forget writes ``forgotten_at``; a later write that set the+ status back (graphiti's attribute rewrite) cannot make it live."""+ fact = _fact(status="active", forgotten_at=_NOW.isoformat())+ assert not recall.is_live(fact)+++class TestForgottenFactPredicate:+ def test_cypher(self) -> None:+ assert recall.forgotten_fact_predicate("f") == (+ "(f.forgotten_at IS NOT NULL"+ " OR (f.expired_at IS NOT NULL AND f.invalid_at IS NULL"+ " AND f.expiration_reason IS NULL))"+ )++ @pytest.mark.parametrize(+ ("status", "reason", "expired_at", "invalid_at", "forgotten"),+ [+ ("active", None, None, None, False),+ (None, None, None, None, False),+ # A status or reason alone is no forget: graphiti's extraction+ # fills both from an episode's text, and a dream picks its reason.+ ("retracted", None, None, None, False), # "I retracted my offer"+ ("retracted", "user_signal", _NOW, None, False),+ ("retracted", "settings_page", _NOW, None, False),+ ("superseded", "user_signal", _NOW, None, False), # a dream demotion+ ("superseded", "stale_fact", _NOW, None, False), # dream's own call+ ("active", None, _NOW, _NOW, False), # graphiti's contradiction+ ("active", None, _NOW, None, True), # the pre-policy forget+ ("superseded", None, _NOW, None, True), # ...once backfilled a status+ ],+ )+ def test_is_forgotten(+ self,+ status: str | None,+ reason: str | None,+ expired_at: datetime | None,+ invalid_at: datetime | None,+ forgotten: bool,+ ) -> None:+ fact = _fact(+ status=status, reason=reason, expired_at=expired_at, invalid_at=invalid_at+ )+ assert recall.is_forgotten(fact) is forgotten+ assert not (forgotten and recall.is_live(fact)), "a forgotten fact is live"++ @pytest.mark.parametrize(+ ("status", "reason", "invalid_at"),+ [+ ("retracted", "user_signal", None), # as the forget left it+ ("superseded", "stale_fact", _NOW), # a dream write landed over it+ ("active", None, None), # graphiti rewrote its attributes+ ],+ )+ def test_the_forget_marker_outlasts_any_other_write(+ self, status: str, reason: str | None, invalid_at: datetime | None+ ) -> None:+ fact = _fact(+ status=status,+ reason=reason,+ expired_at=_NOW,+ invalid_at=invalid_at,+ forgotten_at=_NOW.isoformat(),+ )+ assert recall.is_forgotten(fact)+ assert not recall.is_live(fact)+++class TestRecallableEpisodePredicate:+ def test_cypher(self) -> None:+ assert recall.recallable_episode_predicate("ep", "gone") == (+ "ep.redacted_at IS NULL"+ " AND none(x IN coalesce(ep.entity_edges, []) WHERE x IN gone)"+ )++ def test_clause_collects_every_forgotten_fact(self) -> None:+ clause = recall.forgotten_facts_clause("gone")+ assert clause.startswith("OPTIONAL MATCH ()-[forgotten_fact:RELATES_TO]->()")+ assert recall.forgotten_fact_predicate("forgotten_fact") in clause+ assert clause.rstrip().endswith("WITH collect(forgotten_fact.uuid) AS gone")++ @pytest.mark.parametrize(+ ("entity_edges", "redacted", "recallable"),+ [+ (["e1", "e2"], False, True),+ (["e1", "gone-1"], False, False), # one forgotten fact hides it all+ ([], True, False), # stamped by a forget+ ([], False, True),+ ],+ )+ def test_is_recallable_episode(+ self, entity_edges: list[str], redacted: bool, recallable: bool+ ) -> None:+ assert (+ recall.is_recallable_episode(entity_edges, {"gone-1"}, redacted=redacted)+ is recallable+ )+++class TestSearchFacts:+ @staticmethod+ def _client(edges: list[EntityEdge], still: list[str] | None = None) -> MagicMock:+ """graphiti found ``edges``; reading them again by uuid finds+ ``still`` (every one of them unless given)."""+ client = MagicMock()+ client.search_ = AsyncMock(return_value=SearchResults(edges=edges))+ found = [edge.uuid for edge in edges] if still is None else still+ rows = [{"uuid": uuid} for uuid in found]+ client.driver.execute_query = AsyncMock(return_value=(rows, [], None))+ return client++ @pytest.mark.asyncio+ async def test_a_fact_forgotten_while_the_search_ran_is_not_returned(+ self,+ ) -> None:+ """The search's last step reads the facts again by uuid under the+ live test (``live_now``); what a forget took meanwhile is dropped."""+ client = self._client([_fact("forgotten"), _fact("kept")], still=["kept"])+ with patch.object(+ recall, "get_graphiti_client", AsyncMock(return_value=client)+ ):+ facts = await recall.search_facts(_SCOPE, "atlas", limit=10)++ assert [f.uuid for f in facts] == ["kept"]+ query = client.driver.execute_query.await_args.args[0]+ assert "e.uuid IN $uuids" in query+ assert recall.live_fact_predicate("e") in query+ assert client.driver.execute_query.await_args.kwargs == {+ "uuids": ["forgotten", "kept"]+ }++ @pytest.mark.asyncio+ async def test_nothing_found_reads_nothing_again(self) -> None:+ client = self._client([])+ with patch.object(+ recall, "get_graphiti_client", AsyncMock(return_value=client)+ ):+ assert await recall.search_facts(_SCOPE, "atlas", limit=10) == []++ client.driver.execute_query.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_keeps_live_facts_and_drops_retired_ones(self) -> None:+ client = self._client(+ [+ _fact("active"),+ _fact("no-status", status=None),+ _fact("tentative", status="tentative"),+ _fact("superseded", status="superseded"),+ _fact("contradicted", status="contradicted"),+ _fact("retracted", status="retracted"),+ _fact("forgotten", forgotten_at=_NOW.isoformat()),+ ]+ )+ with patch.object(+ recall, "get_graphiti_client", AsyncMock(return_value=client)+ ):+ facts = await recall.search_facts(_SCOPE, "atlas", limit=10)++ assert [f.uuid for f in facts] == ["active", "no-status", "tentative"]++ @pytest.mark.asyncio+ async def test_tentative_dropped_when_not_wanted(self) -> None:+ client = self._client([_fact("active"), _fact("tentative", status="tentative")])+ with patch.object(+ recall, "get_graphiti_client", AsyncMock(return_value=client)+ ):+ facts = await recall.search_facts(+ _SCOPE, "atlas", limit=10, include_tentative=False+ )++ assert [f.uuid for f in facts] == ["active"]+ query = client.driver.execute_query.await_args.args[0]+ assert recall.live_fact_predicate("e", include_tentative=False) in query++ @pytest.mark.asyncio+ async def test_asks_graphiti_for_unexpired_facts_in_the_scope(self) -> None:+ client = self._client([])+ scope = MemoryScope.for_expert("user-abc", "expert-1")+ get_client = AsyncMock(return_value=client)+ with patch.object(recall, "get_graphiti_client", get_client):+ await recall.search_facts(scope, "atlas", limit=7)++ get_client.assert_awaited_once_with(scope.group_id)+ kwargs = client.search_.await_args.kwargs+ assert kwargs["query"] == "atlas"+ assert kwargs["group_ids"] == [scope.group_id]+ [[date_filter]] = kwargs["search_filter"].expired_at+ assert date_filter.comparison_operator == ComparisonOperator.is_null+ # Default recipe: the hybrid RRF config ``Graphiti.search`` uses, so+ # ranking matches what memory_search did before.+ assert kwargs["config"].edge_config.reranker == EdgeReranker.rrf+ assert kwargs["config"].limit == 7++ @pytest.mark.asyncio+ async def test_recipe_limit_set_on_a_copy(self) -> None:+ client = self._client([])+ recipe_limit = EDGE_HYBRID_SEARCH_CROSS_ENCODER.limit+ with patch.object(+ recall, "get_graphiti_client", AsyncMock(return_value=client)+ ):+ await recall.search_facts(+ _SCOPE, "atlas", limit=3, recipe=EDGE_HYBRID_SEARCH_CROSS_ENCODER+ )++ config = client.search_.await_args.kwargs["config"]+ assert config.edge_config.reranker == EdgeReranker.cross_encoder+ assert config.limit == 3+ assert EDGE_HYBRID_SEARCH_CROSS_ENCODER.limit == recipe_limit+++class TestSearchForgottenFacts:+ @pytest.mark.asyncio+ async def test_ranks_the_forgotten_facts_alone_by_meaning(self) -> None:+ """For a soft forget made permanent: the request is embedded and only+ forgotten facts are ranked against it, best first, above graphiti's+ similarity threshold, up to ``limit``, so however many live facts+ match better, none takes a forgotten one's slot."""+ client = MagicMock()+ client.embedder.create = AsyncMock(return_value=[0.25, 0.75])+ client.driver.provider = GraphProvider.FALKORDB+ rows = [_record("first"), _record("pre-policy")]+ client.driver.execute_query = AsyncMock(return_value=(rows, [], None))+ get_client = AsyncMock(return_value=client)+ with patch.object(recall, "get_graphiti_client", get_client):+ facts = await recall.search_forgotten_facts(_SCOPE, "the\natlas", limit=2)++ assert [f.uuid for f in facts] == ["first", "pre-policy"]+ get_client.assert_awaited_once_with(_SCOPE.group_id)+ client.embedder.create.assert_awaited_once_with(input_data=["the atlas"])+ query = client.driver.execute_query.await_args.args[0]+ assert f"AND {recall.forgotten_fact_predicate('e')}" in query+ assert "vec.cosineDistance(e.fact_embedding" in query+ assert query.endswith("ORDER BY score DESC LIMIT $limit")+ assert client.driver.execute_query.await_args.kwargs == {+ "group_id": _SCOPE.group_id,+ "search_vector": [0.25, 0.75],+ "min_score": DEFAULT_MIN_SCORE,+ "limit": 2,+ }diff --git a/autogpt_platform/backend/backend/copilot/graphiti/scope.py b/autogpt_platform/backend/backend/copilot/graphiti/scope.pyindex 796ec5b0e0..f2208717eb 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/scope.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/scope.py@@ -16,7 +16,9 @@ from pydantic import BaseModel, ConfigDict, Field, model_validatorfrom .client import derive_memory_group_id, derive_memory_scope_key-RedisKeyFamily = Literal["dream_lock", "last_completed", "hits", "rebuild_lock"]+RedisKeyFamily = Literal[+ "dream_lock", "last_completed", "hits", "rebuild_lock", "write_lock"+]# One in-flight dream per scope (dream/locks.py).DREAM_LOCK_KEY_PREFIX = "dream:inflight:"@@ -27,6 +29,15 @@ LAST_COMPLETED_KEY_PREFIX = "dream:last_completed:"HIT_TRACKER_KEY_PREFIX = "mem:hits"# One community rebuild per graph (graphiti/communities.py).REBUILD_LOCK_KEY_PREFIX = "graphiti:community_rebuild_lock:"+# One writer at a time per graph: an ingestion, a forget or the+# legacy-forget backfill (graphiti/scope_lock.py).+WRITE_LOCK_KEY_PREFIX = "graphiti:write_lock:"+++def write_lock_key(group_id: str) -> str:+ """The write lock of graph ``group_id``. Keyed on the graph, not the+ scope: the ingestion worker that takes it knows only the graph."""+ return f"{WRITE_LOCK_KEY_PREFIX}{group_id}"class MemoryScope(BaseModel):@@ -81,8 +92,9 @@ class MemoryScope(BaseModel):before this class existed.Every family keys on ``scope_key`` (the raw user id for the account,- the group id for an expert) except ``rebuild_lock``, which keeps its- ``group_id`` layout. ``hits`` needs the counted ``edge_uuid``.+ the group id for an expert) except ``rebuild_lock`` and+ ``write_lock``, which key on ``group_id``. ``hits`` needs the+ counted ``edge_uuid``."""match family:case "dream_lock":@@ -95,6 +107,8 @@ class MemoryScope(BaseModel):return f"{HIT_TRACKER_KEY_PREFIX}:{self.scope_key}:{edge_uuid}"case "rebuild_lock":return f"{REBUILD_LOCK_KEY_PREFIX}{self.group_id}"+ case "write_lock":+ return write_lock_key(self.group_id)@model_validator(mode="after")def _ids_match_derivation(self) -> "MemoryScope":diff --git a/autogpt_platform/backend/backend/copilot/graphiti/scope_lock.py b/autogpt_platform/backend/backend/copilot/graphiti/scope_lock.pynew file mode 100644index 0000000000..ba052e4768--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/scope_lock.py@@ -0,0 +1,175 @@+"""One writer at a time per memory graph, so a forget does not overlap an+ingestion.++graphiti's ``add_episode`` saves the edges and entities it read when it+began (``SET r = edge``, ``SET n = node``): a forget that landed while it+ran would be written over by the older copy. So these writers hold this+lock: the ingestion worker around ``add_episode`` and what it writes after+it (``ingest.py``), ``recall_forget.retract`` around the whole forget, and+the legacy-forget backfill around each graph's restamp+(``migrations/backfill_legacy_forgets.py``); the weekly community rebuild+and the settings page's erase-all do not (``graphiti/AGENTS.md``). It is one+Redis key per graph (``MemoryScope.redis_key("write_lock")``), set NX with a+token and a five-minute expiry that the holder keeps renewing, and released+and renewed by the dream lock's compare-and-delete and compare-and-extend+scripts (``dream/locks.py``), so a holder whose key expired never touches a+newer holder's.++Waiters take turns. A writer that finds the lock held claims the next turn+(``MemoryScope.redis_key``'s ``write_lock`` key plus ``:next``, set NX with+its token, renewed at every poll and lapsing ``_TURN_CLAIM_SECONDS`` after+its waiter stops), and while another waiter holds that claim no one else+takes the lock, the writer that just let it go included. So a worker+writing a backlog episode after episode hands the lock to a waiter between+two of them instead of taking it straight back within one Redis round trip,+which a poll every ``_POLL_SECONDS`` almost never beat.++A writer waits a bounded time: an ingestion ``INGEST_LOCK_WAIT_SECONDS``,+then its episode goes to the back of the queue after a pause and waits+again, as often as it takes, never dropped for waiting+(``ingest._write_or_requeue``); a forget ``FORGET_LOCK_WAIT_SECONDS``, then+it fails as ``busy`` with nothing written; the backfill skips the graph as+busy. Two windows remain+in which a forget can be written over, and neither has a bound. When Redis+cannot be reached, the ingestion worker and a forget go ahead without the+lock and log a warning (a lock taken by a request that timed out after Redis+applied it is released, best-effort). And a holder that loses its key (its+renewals fail, or its event loop stalls past the expiry) keeps writing+while another writer takes the lock: the renewal only warns, and nothing+fences the graph write.+"""++import asyncio+import logging+import sys+import uuid+from collections.abc import AsyncIterator+from contextlib import asynccontextmanager+from enum import Enum+from typing import Any, cast++from backend.copilot.dream.locks import EXTEND_SCRIPT, UNLOCK_SCRIPT+from backend.data.redis_client import get_redis_async++from .scope import write_lock_key++if sys.version_info >= (3, 11):+ from asyncio import timeout+else:+ from async_timeout import timeout++logger = logging.getLogger(__name__)++WRITE_LOCK_TTL_SECONDS = 300+INGEST_LOCK_WAIT_SECONDS = 60+FORGET_LOCK_WAIT_SECONDS = 20+_POLL_SECONDS = 0.25+# How long a waiter's claim on the next turn outlives its last poll.+_TURN_CLAIM_SECONDS = 2+_RENEW_EVERY_SECONDS = WRITE_LOCK_TTL_SECONDS / 3+# ``get_redis_async`` retries a lost connection for minutes; every call here+# is bounded, so Redis being down costs a writer seconds, not minutes.+_REDIS_TIMEOUT_SECONDS = 2.0+++class LockState(str, Enum):+ """What a writer got: the lock, a lock held by another writer for the+ whole wait, or no Redis to take it from."""++ HELD = "held"+ BUSY = "busy"+ UNAVAILABLE = "unavailable"+++@asynccontextmanager+async def graph_write_lock(+ group_id: str, *, wait_seconds: float+) -> AsyncIterator[LockState]:+ """Hold graph ``group_id``'s write lock for the block, waiting up to+ ``wait_seconds`` for it. Yields ``BUSY`` (another writer kept it for the+ whole wait) or ``UNAVAILABLE`` (Redis could not be reached) instead of+ raising; the caller decides what to do without it: the worker and a+ forget write anyway, the backfill skips the graph."""+ key = write_lock_key(group_id)+ token = uuid.uuid4().hex+ state = await _acquire(key, token, wait_seconds)+ if state is not LockState.HELD:+ yield state+ return+ renewal = asyncio.create_task(_renew(key, token))+ try:+ yield state+ finally:+ renewal.cancel()+ await _release(key, token)+++async def _acquire(key: str, token: str, wait_seconds: float) -> LockState:+ loop = asyncio.get_running_loop()+ deadline = loop.time() + wait_seconds+ while True:+ try:+ async with timeout(_REDIS_TIMEOUT_SECONDS):+ acquired = await _take_or_wait_turn(key, token)+ except Exception:+ logger.warning(+ f"Redis unreachable for {key[:48]}: write lock unavailable",+ exc_info=True,+ )+ # The SET may have landed before the request gave up: free it.+ await _release(key, token)+ return LockState.UNAVAILABLE+ if acquired:+ return LockState.HELD+ if loop.time() >= deadline:+ # Stop waiting, and let the next waiter have the turn at once.+ await _release(f"{key}:next", token)+ return LockState.BUSY+ await asyncio.sleep(_POLL_SECONDS)+++async def _take_or_wait_turn(key: str, token: str) -> bool:+ """One attempt at the lock: taken unless another waiter claimed the next+ turn; else the next turn is claimed for this writer, or its claim kept."""+ redis = await get_redis_async()+ turn = f"{key}:next"+ claimed = await redis.get(turn)+ if claimed in (None, token):+ if await redis.set(key, token, nx=True, px=WRITE_LOCK_TTL_SECONDS * 1000):+ if claimed == token:+ await cast(Any, redis.eval(UNLOCK_SCRIPT, 1, turn, token))+ return True+ if claimed is None:+ await redis.set(turn, token, nx=True, px=_TURN_CLAIM_SECONDS * 1000)+ elif claimed == token:+ await cast(Any, redis.eval(EXTEND_SCRIPT, 1, turn, token, _TURN_CLAIM_SECONDS))+ return False+++async def _renew(key: str, token: str) -> None:+ """Keep a held lock from expiring under a long write; stop once the key+ is no longer this holder's."""+ while True:+ await asyncio.sleep(_RENEW_EVERY_SECONDS)+ try:+ renewed = await _script(EXTEND_SCRIPT, key, token, WRITE_LOCK_TTL_SECONDS)+ except Exception:+ logger.warning(f"Renewing {key[:48]} failed", exc_info=True)+ continue+ if not renewed:+ logger.warning(f"{key[:48]} expired while held: writers may overlap")+ return+++async def _release(key: str, token: str) -> None:+ try:+ await _script(UNLOCK_SCRIPT, key, token)+ except Exception:+ logger.warning(f"Releasing {key[:48]} failed; it expires", exc_info=True)+++async def _script(script: str, key: str, token: str, *args: int) -> int:+ async with timeout(_REDIS_TIMEOUT_SECONDS):+ redis = await get_redis_async()+ # redis-py types ``eval`` as returning ``str``; the dream lock casts too.+ return int(await cast(Any, redis.eval(script, 1, key, token, *args)))diff --git a/autogpt_platform/backend/backend/copilot/graphiti/scope_lock_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/scope_lock_integration_test.pynew file mode 100644index 0000000000..bc99907c33--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/scope_lock_integration_test.py@@ -0,0 +1,213 @@+"""The graph's write lock as its writers meet it, through the production+worker against a live FalkorDB whose own Redis holds the lock+(``scope_lock.py``): a forget the lock keeps waiting fails as busy and writes+nothing (the chat tool tries once more), an ingestion it keeps waiting goes+to the back of the queue, as often as it takes, and without Redis both go+ahead.+``recall_inflight_integration_test.py``+covers a forget and an ingestion meeting mid-episode.++Run with FalkorDB reachable (see ``conftest.py``)::++ poetry run pytest -m integration backend/copilot/graphiti/scope_lock_integration_test.py+"""++from collections.abc import AsyncIterator+from typing import Any+from unittest.mock import AsyncMock, patch++import pytest+import pytest_asyncio+from pytest_mock import MockerFixture+from redis.asyncio import Redis++from backend.copilot.model import ChatSession+from backend.copilot.tools import graphiti_forget+from backend.copilot.tools.graphiti_forget import MemoryForgetConfirmTool+from backend.copilot.tools.models import MemoryForgetConfirmResponse++from . import ingest, recall_forget, scope_lock+from .falkordb_driver import AutoGPTFalkorDriver+from .memory_model import ForgetResult, MemoryForgetFailureCode+from .recall_forget import retract+from .recall_integration_fixtures import (+ ALICE,+ BOB,+ BuildClient,+ count,+ edge_row,+ ingest_through_the_worker,+ live_facts,+ patch_recall_boundaries,+ scripted_responses,+ stop_ingestion_workers,+)+from .scope import MemoryScope, write_lock_key++[email protected](autouse=True)+def boundaries(mocker, scope_graph, stub_graphiti_client):+ patch_recall_boundaries(mocker, scope_graph[0], stub_graphiti_client)+++@pytest_asyncio.fixture(loop_scope="function")+async def ingest_worker_cleanup(mocker: MockerFixture) -> AsyncIterator[None]:+ mocker.patch(+ "backend.copilot.graphiti.ingest.ensure_scope_scheduled",+ AsyncMock(return_value=None),+ )+ yield+ await stop_ingestion_workers()+++async def _learn(driver: AutoGPTFalkorDriver, scope: MemoryScope, build: BuildClient):+ client = build(driver, scripted_responses([ALICE]))+ await ingest_through_the_worker(driver, scope, client, [ALICE], session_id="s-1")+ [alice] = await live_facts(driver)+ return alice+++async def test_a_forget_the_lock_keeps_waiting_is_busy_and_writes_nothing(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup, live_lock: Redis+) -> None:+ driver, scope = scope_graph+ alice = await _learn(driver, scope, stub_graphiti_client)+ before = await edge_row(driver, alice)+ await live_lock.set(write_lock_key(scope.group_id), "a long ingestion", px=60000)++ with patch.object(recall_forget, "FORGET_LOCK_WAIT_SECONDS", 0.5):+ result = await retract(scope, [alice])++ assert result.deleted == []+ assert [f.code for f in result.failures] == [MemoryForgetFailureCode.BUSY]+ assert await edge_row(driver, alice) == before, "nothing written"+ await live_lock.delete(write_lock_key(scope.group_id))+ assert (await retract(scope, [alice])).deleted == [alice], "a retry works"+++async def test_the_chat_tool_tries_a_busy_forget_once_more(+ scope_graph, stub_graphiti_client, ingest_worker_cleanup, live_lock: Redis+) -> None:+ """The first try finds an ingestion holding the lock and writes nothing;+ the ingestion finishes while the tool waits, and the second try forgets."""+ driver, scope = scope_graph+ alice = await _learn(driver, scope, stub_graphiti_client)+ key = write_lock_key(scope.group_id)+ await live_lock.set(key, "a long ingestion", px=60000)+ tries: list[ForgetResult] = []++ async def then_the_ingestion_finishes(*args: Any, **kwargs: Any) -> ForgetResult:+ tries.append(await retract(*args, **kwargs))+ await live_lock.delete(key)+ return tries[-1]++ session = ChatSession.new(scope.owner_user_id, dry_run=False)+ enabled = AsyncMock(return_value=True)+ with (+ patch.object(recall_forget, "FORGET_LOCK_WAIT_SECONDS", 0.3),+ patch.object(graphiti_forget, "_BUSY_RETRY_SECONDS", 0.1),+ patch.object(graphiti_forget, "retract", then_the_ingestion_finishes),+ patch.object(graphiti_forget, "is_enabled_for_user", enabled),+ ):+ response = await MemoryForgetConfirmTool()._execute(+ scope.owner_user_id, session, uuids=[alice]+ )++ assert [[f.code for f in t.failures] for t in tries] == [["busy"], []]+ assert isinstance(response, MemoryForgetConfirmResponse)+ assert (response.deleted_uuids, response.failed_uuids) == ([alice], [])+ assert await live_facts(driver) == {}+++async def test_a_worker_a_forget_keeps_waiting_requeues_the_episode(+ scope_graph,+ stub_graphiti_client,+ ingest_worker_cleanup,+ live_lock: Redis,+) -> None:+ """The first wait runs out and writes nothing; the forget finishes while+ the episode pauses, and the episode, back at the end of the queue, is+ written."""+ driver, scope = scope_graph+ key = write_lock_key(scope.group_id)+ await live_lock.set(key, "a long forget", px=60000)++ async def forget_finishes(waits: int) -> None:+ await live_lock.delete(key)++ client = stub_graphiti_client(driver, scripted_responses([BOB]))+ with (+ patch.object(ingest, "INGEST_LOCK_WAIT_SECONDS", 0.5),+ patch.object(ingest, "_pause_before_retry", forget_finishes),+ ):+ await ingest_through_the_worker(driver, scope, client, [BOB], session_id="s-q")++ assert list((await live_facts(driver)).values()) == [BOB[2]]+++async def test_a_worker_locked_out_wait_after_wait_keeps_the_episode(+ scope_graph,+ stub_graphiti_client,+ ingest_worker_cleanup,+ live_lock: Redis,+ caplog: pytest.LogCaptureFixture,+) -> None:+ """A stuck forget keeps the lock through four waits: the episode is never+ dropped, a warning names it at the third, and it is written once the+ lock frees."""+ driver, scope = scope_graph+ key = write_lock_key(scope.group_id)+ await live_lock.set(key, "a stuck forget", px=60000)+ client = stub_graphiti_client(driver, scripted_responses([BOB]))+ completion = ingest.IngestionCompletion()++ async def pause(waits: int) -> None:+ if waits == 4:+ await live_lock.delete(key)++ with (+ patch.object(ingest, "INGEST_LOCK_WAIT_SECONDS", 0.3),+ patch.object(ingest, "_pause_before_retry", pause),+ patch.object(ingest, "get_graphiti_client", AsyncMock(return_value=client)),+ ):+ assert await ingest.enqueue_episode(+ scope,+ "s-d",+ name="conversation_s-d",+ episode_body=BOB[2],+ source_description="User message in session s-d",+ completion=completion,+ )+ completion.register()+ assert await ingest.wait_for_ingestion(completion, 20), "never completed"++ assert (completion.written, completion.pending) == (1, 0)+ assert "'conversation_s-d' is still queued" in caplog.text+ assert await count(driver, "MATCH (ep:Episodic) RETURN count(ep) AS c") == 1+++async def test_without_redis_both_writers_go_ahead_and_warn(+ scope_graph,+ stub_graphiti_client,+ ingest_worker_cleanup,+ caplog: pytest.LogCaptureFixture,+) -> None:+ driver, scope = scope_graph+ down = AsyncMock(side_effect=ConnectionError("redis down"))++ with patch.object(scope_lock, "get_redis_async", down):+ alice = await _learn(driver, scope, stub_graphiti_client)+ result = await retract(scope, [alice])++ assert (result.deleted, result.failures) == ([alice], [])+ assert caplog.text.count("write lock unavailable") >= 2diff --git a/autogpt_platform/backend/backend/copilot/graphiti/scope_lock_test.py b/autogpt_platform/backend/backend/copilot/graphiti/scope_lock_test.pynew file mode 100644index 0000000000..8bac55b43e--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/scope_lock_test.py@@ -0,0 +1,215 @@+"""Unit tests for ``scope_lock.graph_write_lock`` on the in-memory Redis+(``conftest.lock_redis``).++The live runs, through the production worker and a real Redis, are in+``recall_inflight_integration_test.py``.+"""++import asyncio+from unittest.mock import AsyncMock, patch++import pytest++from . import scope_lock+from .recall_fake_redis import FakeRedis+from .scope import write_lock_key+from .scope_lock import LockState, graph_write_lock++_GROUP = "user_abc"+_KEY = write_lock_key(_GROUP)+++async def test_lock_works_without_python311_asyncio_timeout(+ lock_redis: FakeRedis, monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.delattr(asyncio, "timeout", raising=False)++ async with graph_write_lock(_GROUP, wait_seconds=0) as lock:+ assert lock is LockState.HELD+ assert _KEY in lock_redis.values++ assert _KEY not in lock_redis.values+++async def test_redis_deadline_without_python311_asyncio_timeout(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.delattr(asyncio, "timeout", raising=False)+ monkeypatch.setattr(scope_lock, "_REDIS_TIMEOUT_SECONDS", 0.01)++ async def hang() -> FakeRedis:+ await asyncio.sleep(60)+ return FakeRedis()++ monkeypatch.setattr(scope_lock, "get_redis_async", hang)+ with pytest.raises(asyncio.TimeoutError):+ await scope_lock._script(scope_lock.UNLOCK_SCRIPT, _KEY, "token")+++async def test_holds_the_key_with_its_token_and_releases_it(+ lock_redis: FakeRedis,+) -> None:+ async with graph_write_lock(_GROUP, wait_seconds=0) as lock:+ assert lock is LockState.HELD+ assert _KEY in lock_redis.values+ assert lock_redis.ttls[_KEY] == scope_lock.WRITE_LOCK_TTL_SECONDS++ assert _KEY not in lock_redis.values+++async def test_a_lock_held_for_the_whole_wait_is_busy_and_left_alone(+ lock_redis: FakeRedis,+) -> None:+ lock_redis.values[_KEY] = "another writer"++ async with graph_write_lock(_GROUP, wait_seconds=0) as lock:+ assert lock is LockState.BUSY++ assert lock_redis.values[_KEY] == "another writer"+++async def test_a_writer_waits_for_the_lock_to_come_free(+ lock_redis: FakeRedis,+) -> None:+ lock_redis.values[_KEY] = "another writer"++ async def finish() -> None:+ await asyncio.sleep(0.3)+ del lock_redis.values[_KEY]++ other = asyncio.create_task(finish())+ async with graph_write_lock(_GROUP, wait_seconds=5) as lock:+ assert lock is LockState.HELD+ await other+++async def test_a_writer_that_lets_go_does_not_take_the_lock_back_from_a_waiter(+ lock_redis: FakeRedis,+) -> None:+ """A worker writing a backlog lets the lock go and asks for it again at+ once, within one Redis round trip: the writer already waiting has+ claimed the next turn, so it goes first."""+ order: list[str] = []++ async def waiter() -> None:+ async with graph_write_lock(_GROUP, wait_seconds=5) as lock:+ assert lock is LockState.HELD+ order.append("waiter")++ async def turn_claimed() -> None:+ while f"{_KEY}:next" not in lock_redis.values:+ await asyncio.sleep(0.01)++ async with graph_write_lock(_GROUP, wait_seconds=0) as lock:+ assert lock is LockState.HELD+ waiting = asyncio.create_task(waiter())+ await asyncio.wait_for(turn_claimed(), 2)+ assert lock_redis.ttls[f"{_KEY}:next"] == scope_lock._TURN_CLAIM_SECONDS+ async with graph_write_lock(_GROUP, wait_seconds=5) as lock:+ assert lock is LockState.HELD+ order.append("backlog")+ await waiting++ assert order == ["waiter", "backlog"]+ assert f"{_KEY}:next" not in lock_redis.values+++async def test_a_lock_a_timed_out_request_took_is_given_back(+ lock_redis: FakeRedis, caplog: pytest.LogCaptureFixture+) -> None:+ """Redis applied the SET but its answer came too late: the writer goes+ ahead without the lock, and the key it was given is not left holding+ every other writer off for the lock's whole expiry."""+ stored = lock_redis.set++ async def answers_late(+ key: str,+ value: object,+ *,+ nx: bool = False,+ ex: int | None = None,+ px: int | None = None,+ ) -> bool | None:+ result = await stored(key, value, nx=nx, ex=ex, px=px)+ if key == _KEY:+ await asyncio.sleep(60)+ return result++ with (+ patch.object(lock_redis, "set", answers_late),+ patch.object(scope_lock, "_REDIS_TIMEOUT_SECONDS", 0.05),+ ):+ async with graph_write_lock(_GROUP, wait_seconds=5) as lock:+ assert lock is LockState.UNAVAILABLE++ assert _KEY not in lock_redis.values+ assert "write lock unavailable" in caplog.text++[email protected]("failure", ["raises", "hangs"])+async def test_without_redis_the_writer_goes_ahead_and_warns(+ failure: str, caplog: pytest.LogCaptureFixture+) -> None:+ async def hang() -> FakeRedis:+ await asyncio.sleep(60)+ return FakeRedis()++ down = hang if failure == "hangs" else AsyncMock(side_effect=ConnectionError())+ with (+ patch.object(scope_lock, "get_redis_async", down),+ patch.object(scope_lock, "_REDIS_TIMEOUT_SECONDS", 0.05),+ ):+ async with graph_write_lock(_GROUP, wait_seconds=5) as lock:+ assert lock is LockState.UNAVAILABLE++ assert "write lock unavailable" in caplog.text+++async def test_release_never_deletes_a_newer_holders_lock(+ lock_redis: FakeRedis,+) -> None:+ async with graph_write_lock(_GROUP, wait_seconds=0):+ lock_redis.values[_KEY] = "a writer after our lock expired"++ assert lock_redis.values[_KEY] == "a writer after our lock expired"+++async def test_a_long_hold_keeps_renewing_the_lock(lock_redis: FakeRedis) -> None:+ renewals: list[int] = []+ extend = lock_redis.eval++ async def counting(script: str, numkeys: int, key: str, *args: object) -> int:+ if script == scope_lock.EXTEND_SCRIPT:+ renewals.append(int(str(args[1])))+ return await extend(script, numkeys, key, *args)++ with (+ patch.object(scope_lock, "_RENEW_EVERY_SECONDS", 0.05),+ patch.object(lock_redis, "eval", counting),+ ):+ async with graph_write_lock(_GROUP, wait_seconds=0):+ await asyncio.sleep(0.3)++ assert len(renewals) >= 2+ assert set(renewals) == {scope_lock.WRITE_LOCK_TTL_SECONDS}+++async def test_a_failed_release_only_warns(+ lock_redis: FakeRedis, caplog: pytest.LogCaptureFixture+) -> None:+ with patch.object(lock_redis, "eval", AsyncMock(side_effect=RuntimeError("x"))):+ async with graph_write_lock(_GROUP, wait_seconds=0) as lock:+ assert lock is LockState.HELD++ assert "Releasing" in caplog.textdiff --git a/autogpt_platform/backend/backend/copilot/graphiti/scope_test.py b/autogpt_platform/backend/backend/copilot/graphiti/scope_test.pyindex a3c101c3cc..345ca7a569 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/scope_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/scope_test.py@@ -9,6 +9,7 @@ from .scope import (HIT_TRACKER_KEY_PREFIX,LAST_COMPLETED_KEY_PREFIX,REBUILD_LOCK_KEY_PREFIX,+ WRITE_LOCK_KEY_PREFIX,MemoryScope,)@@ -95,6 +96,13 @@ class TestRedisKeys:legacy = f"{REBUILD_LOCK_KEY_PREFIX}{group_id}"assert scope.redis_key("rebuild_lock") == legacy+ @BOTH_SCOPES+ def test_write_lock_keys_on_group_id(self, expert_id: str | None) -> None:+ """The ingestion worker takes it knowing only the graph."""+ scope = MemoryScope.build(USER_ID, expert_id)+ group_id = derive_memory_group_id(USER_ID, expert_id)+ assert scope.redis_key("write_lock") == (f"{WRITE_LOCK_KEY_PREFIX}{group_id}")+def test_hits_key_requires_edge_uuid(self) -> None:with pytest.raises(ValueError, match="edge_uuid"):MemoryScope.for_user(USER_ID).redis_key("hits")diff --git a/autogpt_platform/backend/backend/copilot/graphiti/types.py b/autogpt_platform/backend/backend/copilot/graphiti/types.pyindex 520a9fad3c..058d573258 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/types.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/types.py@@ -7,15 +7,50 @@ Per the integration audit (dream/dreaming-graphiti.md §6.4), passingthe durable ``:RELATES_TO`` edge — not only in the ``:Episodic.content``JSON blob, which Cypher cannot filter without parsing.-After this lands, search filters can do ``WHERE e.status = 'active'``-natively and ratification can flip ``status`` with a single SET.+Cypher can then filter on ``e.status`` (``recall.live_fact_predicate``)+and ratification can flip it with a single SET. graphiti's own search does+not filter on it: ``recall.search_facts`` checks it in Python.++graphiti's attribute extraction writes only what these models declare:+``extracted_attributes`` drops every other key its model answers."""-from datetime import datetime+from typing import Anyfrom pydantic import BaseModel, Field-from .memory_model import MemoryStatus, SourceKind+from .memory_model import ExtractedStatus, SourceKind++# Edge properties only the platform writes, never graphiti's extraction: a+# forget's marker and audit copies (``recall_forget.py``, ``recall_hide.py``),+# the ratification stamp (``dream/ratification.py``), and the web-verification+# stamp, kept for a verification path that does not exist yet. No extraction+# model declares one (``types_test.py``), so ``extracted_attributes`` drops any+# the model invents; a stored one still reads back from the edge.+POLICY_EDGE_FIELDS = frozenset(+ {+ "forgotten_at",+ "fact_redacted",+ "name_redacted",+ "ratified_at",+ "web_verified_at",+ }+)+++def extracted_attributes(+ answer: dict[str, Any], model: type[BaseModel]+) -> dict[str, Any]:+ """The attributes in graphiti's extraction ``answer`` that ``model``+ declares, none of them a policy field.++ graphiti-core 0.30.2 does not validate the answer against ``model``: it+ keeps every key that fits its length cap (``apply_capped_attributes``)+ and saves each on the edge or entity. An invented ``forgotten_at`` would+ make a live fact look forgotten, and carry whatever text it holds to+ wherever that date is shown."""+ allowed = model.model_fields.keys() - POLICY_EDGE_FIELDS+ return {key: value for key, value in answer.items() if key in allowed}class MemoryFact(BaseModel):@@ -27,8 +62,8 @@ class MemoryFact(BaseModel):properties preserve the same shape."""- status: MemoryStatus = Field(- default=MemoryStatus.active,+ status: ExtractedStatus = Field(+ default=ExtractedStatus.active,description="Lifecycle state — active, tentative, superseded, contradicted.",)confidence: float | None = Field(@@ -49,20 +84,12 @@ class MemoryFact(BaseModel):default=None,description="Origin reference — 'session:{id}#msg:{sequence}', tool_call_id, or URL.",)- web_verified_at: datetime | None = Field(- default=None,- description="When (if ever) this fact was verified against external web sources.",- )- ratified_at: datetime | None = Field(- default=None,- description="When a tentative fact was promoted to active via the ratification loop.",- )expiration_reason: str | None = Field(default=None,description=(- "Short reason recorded when a fact is retracted or superseded. Examples: "+ "Short reason recorded when a fact is superseded. Examples: ""'stale_fact', 'contradicted_by:{uuid}', 'entity_invalidated:{uuid}', "- "'web_contradicted:{url}', 'user_signal', 'unratified'."+ "'web_contradicted:{url}', 'unratified'."),)diff --git a/autogpt_platform/backend/backend/copilot/graphiti/types_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/types_integration_test.pyindex 27662f4171..fc7cc2f969 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/types_integration_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/types_integration_test.py@@ -1,11 +1,12 @@"""Integration tests for the custom edge-type schema (P-1.1).Validates the schema contract that ``MemoryFact`` declares: when a-``:RELATES_TO`` edge carries the new properties (``status``,-``confidence``, ``source_kind``, ``scope``, ``provenance``,-``web_verified_at``, ``ratified_at``, ``expiration_reason``), all of-them round-trip cleanly through FalkorDB + the AutoGPTFalkorDriver,-and Cypher queries can filter on them natively.+``:RELATES_TO`` edge carries its properties (``status``, ``confidence``,+``source_kind``, ``scope``, ``provenance``, ``expiration_reason``) and the+platform's attestation stamps (``web_verified_at``, ``ratified_at``, which+no extraction writes: ``types.POLICY_EDGE_FIELDS``), all of them+round-trip cleanly through FalkorDB + the AutoGPTFalkorDriver, and Cypher+queries can filter on them natively.What this does NOT test: end-to-end ``add_episode`` calls that exercisethe LLM-driven entity extractor. That path requires a live LLM key anddiff --git a/autogpt_platform/backend/backend/copilot/graphiti/types_test.py b/autogpt_platform/backend/backend/copilot/graphiti/types_test.pyindex cd113bba45..b5571b5a5c 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/types_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/types_test.py@@ -8,16 +8,18 @@ LLM extraction step and ends up on production :RELATES_TO edges.from __future__ import annotations-from datetime import datetime, timezone+import jsonimport pytestfrom pydantic import ValidationError-from .memory_model import MemoryStatus, SourceKind+from .memory_model import ExtractedStatus, MemoryStatus, SourceKind+from .recall import USER_FORGET_REASONfrom .types import (EDGE_TYPE_MAP,EDGE_TYPES,ENTITY_TYPES,+ POLICY_EDGE_FIELDS,MemoryFact,Organization,Person,@@ -32,8 +34,6 @@ def test_memoryfact_defaults_match_envelope():assert fact.source_kind == SourceKind.user_assertedassert fact.scope == "real:global"assert fact.provenance is None- assert fact.web_verified_at is None- assert fact.ratified_at is Noneassert fact.expiration_reason is None@@ -56,6 +56,21 @@ def test_memoryfact_status_enum_restricted():MemoryFact(status="archived") # type: ignore[arg-type]+def test_memoryfact_leaves_a_forgets_marks_out_of_the_extraction_schema():+ """``MemoryFact`` is graphiti's schema for what its extraction model fills+ from an episode's text: every status but ``retracted``, and no+ ``user_signal`` among the example reasons, since only a forget writes+ them ("I retracted my application" is no forget)."""+ assert {s.value for s in ExtractedStatus} == {s.value for s in MemoryStatus} - {+ MemoryStatus.retracted.value+ }+ with pytest.raises(ValidationError):+ MemoryFact.model_validate({"status": MemoryStatus.retracted.value})+ schema = json.dumps(MemoryFact.model_json_schema())+ assert MemoryStatus.retracted.value not in schema+ assert USER_FORGET_REASON not in schema++def test_memoryfact_carries_full_envelope_metadata():"""All audit-required fields can be set in one shot."""fact = MemoryFact(@@ -64,8 +79,6 @@ def test_memoryfact_carries_full_envelope_metadata():source_kind=SourceKind.assistant_derived,scope="project:foo",provenance="session:s1#msg:42",- web_verified_at=datetime(2026, 5, 12, tzinfo=timezone.utc),- ratified_at=datetime(2026, 5, 14, tzinfo=timezone.utc),expiration_reason="stale_fact",)dumped = fact.model_dump(mode="json")@@ -74,8 +87,6 @@ def test_memoryfact_carries_full_envelope_metadata():assert dumped["source_kind"] == "assistant_derived"assert dumped["scope"] == "project:foo"assert dumped["provenance"] == "session:s1#msg:42"- assert dumped["web_verified_at"] == "2026-05-12T00:00:00Z"- assert dumped["ratified_at"] == "2026-05-14T00:00:00Z"assert dumped["expiration_reason"] == "stale_fact"@@ -125,3 +136,21 @@ def test_organization_entity_has_optional_industry():assert o.industry is Noneo = Organization(industry="aerospace")assert o.industry == "aerospace"+++ "model", [*EDGE_TYPES.values(), *ENTITY_TYPES.values()], ids=lambda m: m.__name__+)+def test_no_extraction_model_declares_a_policy_field(model) -> None:+ """Only the platform writes these: a model declaring one would let+ graphiti's extraction write it (``extracted_attributes``)."""+ assert not model.model_fields.keys() & POLICY_EDGE_FIELDS+ assert not any(+ field in json.dumps(model.model_json_schema()) for field in POLICY_EDGE_FIELDS+ )+++def test_the_attestation_stamps_are_the_platforms():+ """The ratification loop writes ``ratified_at``, and ``web_verified_at``+ has no writer yet: neither is offered to graphiti's extraction model."""+ assert {"ratified_at", "web_verified_at"} <= POLICY_EDGE_FIELDSdiff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_forget.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_forget.pyindex c2da6463d1..634a545b88 100644--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_forget.py+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_forget.py@@ -2,15 +2,40 @@Step 1 (memory_forget_search): search for matching facts, return candidates.Step 2 (memory_forget_confirm): delete specific edges by UUID after user confirms.++Both steps go through the recall policy (``graphiti/recall.py`` and+``graphiti/recall_forget.py``): the candidates are the facts recall would+return, and, when asked (``include_forgotten``), the forgotten ones+(``recall.search_forgotten_facts``), so a soft forget can be made permanent;+a confirmed forget is ``recall_forget.retract``, with the guarantees and+limits ``graphiti/AGENTS.md`` lists.++The demotion helpers below are the dream's writers. They write only over+live facts, so a demotion can never overwrite a user's forget."""+import asyncioimport loggingfrom datetime import datetime, timezonefrom typing import Any, Literal-from backend.copilot.graphiti._format import extract_fact, extract_temporal_validity-from backend.copilot.graphiti.client import get_graphiti_client+from graphiti_core.edges import EntityEdge+from backend.copilot.graphiti.config import is_enabled_for_user+from backend.copilot.graphiti.memory_model import (+ ForgetResult,+ MemoryForgetFailure,+ MemoryForgetFailureCode,+)+from backend.copilot.graphiti.recall import (+ FORGOTTEN_FACT,+ live_fact_predicate,+ search_facts,+ search_forgotten_facts,+)+from backend.copilot.graphiti.recall_forget import retract+from backend.copilot.graphiti.recall_recheck import recheck+from backend.copilot.graphiti.recall_render import fact_text, fact_validityfrom backend.copilot.graphiti.scope import MemoryScopefrom backend.copilot.model import ChatSession@@ -19,17 +44,12 @@ from .models import (ErrorResponse,MemoryForgetCandidatesResponse,MemoryForgetConfirmResponse,- MemoryForgetFailure,- MemoryForgetFailureCode,ToolResponseBase,)-# Reason shown when the delete query ran fine but matched no edge — the UUID-# is stale, already deleted, or not a forgettable edge type.-_NO_MATCH_REASON = (- "No matching edge found — it may already be deleted, or the UUID is not a "- "forgettable edge (RELATES_TO, MENTIONS, HAS_MEMBER)."-)+# A forget that found memory busy (another writer, usually an ingestion,+# holding the graph's write lock) is tried once more after this long.+_BUSY_RETRY_SECONDS = 5# Cap on how many per-UUID failure reasons are inlined into the confirm# message. Keeps a wholesale-failure batch from blowing past the tool-output@@ -53,19 +73,6 @@ def _now_iso() -> str:logger = logging.getLogger(__name__)-def _delete_error_reason(exc: Exception) -> str:- """Sanitized, actionable reason for a delete query that raised.-- Surfaces the exception type plus its first message arg (e.g. FalkorDB's- ``Unknown function 'datetime'``) so the model can tell a real query error- from a plain no-match — but never the full ``repr(exc)``, which can carry- connection/host details. The complete exception is logged server-side with- ``exc_info=True`` at each call site for debugging.- """- detail = exc.args[0] if exc.args else type(exc).__name__- return f"Deletion query failed: {type(exc).__name__}: {detail}"--class MemoryForgetSearchTool(BaseTool):"""Search the current assistant's memories for deletion candidates."""@@ -90,6 +97,10 @@ class MemoryForgetSearchTool(BaseTool):"type": "string","description": "Natural language description of what to forget (e.g. 'the Q2 marketing budget')",},+ "include_forgotten": {+ "type": "boolean",+ "description": "Also find already-forgotten memories, to delete them permanently.",+ },},"required": ["query"],}@@ -104,6 +115,7 @@ class MemoryForgetSearchTool(BaseTool):session: ChatSession,*,query: str = "",+ include_forgotten: bool = False,**kwargs,) -> ToolResponseBase:if not user_id:@@ -125,7 +137,7 @@ class MemoryForgetSearchTool(BaseTool):)try:- group_id = MemoryScope.build(user_id, session.expert_id).group_id+ memory_scope = MemoryScope.build(user_id, session.expert_id)except ValueError:return ErrorResponse(message="Invalid user ID for memory operations.",@@ -133,12 +145,17 @@ class MemoryForgetSearchTool(BaseTool):)try:- client = await get_graphiti_client(group_id)- edges = await client.search(- query=query,- group_ids=[group_id],- num_results=10,+ # Only facts recall would still return: a forgotten one is not+ # offered for forgetting again, unless asked for, so a soft+ # forget can be made permanent.+ edges = await search_facts(memory_scope, query, limit=10)+ forgotten = (+ await search_forgotten_facts(memory_scope, query, limit=10)+ if include_forgotten+ else [])+ if include_forgotten:+ edges, _ = await recheck(memory_scope, edges, [])except Exception:logger.warning("Memory forget search failed for user %s", user_id[:12], exc_info=True@@ -148,31 +165,18 @@ class MemoryForgetSearchTool(BaseTool):session_id=session.session_id,)- if not edges:+ if not edges and not forgotten:return MemoryForgetCandidatesResponse(message="No matching memories found.",session_id=session.session_id,candidates=[],)- candidates = []- for e in edges:- edge_uuid = getattr(e, "uuid", None) or getattr(e, "id", None)- if not edge_uuid:- continue- fact = extract_fact(e)- valid_from, valid_to = extract_temporal_validity(e)- candidates.append(- {- "uuid": str(edge_uuid),- "fact": fact,- "valid_from": str(valid_from),- "valid_to": str(valid_to),- }- )+ candidates = [_candidate(edge) for edge in edges]+ candidates += [_forgotten_candidate(edge) for edge in forgotten]return MemoryForgetCandidatesResponse(- message=f"Found {len(candidates)} candidate(s). Show these to the user and ask which to delete, then call tool:memory_forget_confirm with the UUIDs.",+ message=_candidates_message(len(edges), len(forgotten)),session_id=session.session_id,candidates=candidates,)@@ -253,53 +257,49 @@ class MemoryForgetConfirmTool(BaseTool):)try:- group_id = MemoryScope.build(user_id, session.expert_id).group_id+ memory_scope = MemoryScope.build(user_id, session.expert_id)except ValueError:return ErrorResponse(message="Invalid user ID for memory operations.",session_id=session.session_id,)+ # A soft forget is a *system* retraction, not a world change: the+ # edge keeps its ``invalid_at``. See ``retract``.try:- client = await get_graphiti_client(group_id)+ result = await _retract_once_more_if_busy(memory_scope, uuids, hard_delete)except Exception:logger.warning(- "Failed to get Graphiti client for user %s", user_id[:12], exc_info=True+ "Memory forget failed for user %s", user_id[:12], exc_info=True)return ErrorResponse(message="Memory service is temporarily unavailable.",session_id=session.session_id,)- driver = getattr(client, "graph_driver", None) or getattr(- client, "driver", None- )- if not driver:- return ErrorResponse(- message="Could not access graph driver for deletion.",- session_id=session.session_id,- )-- if hard_delete:- deleted, failures = await _hard_delete_edges(driver, uuids, user_id)- mode = "permanently deleted"- else:- # User-initiated forget is a *system* retraction, not a world- # change. Per Snodgrass bi-temporal semantics, only `expired_at`- # is set. `_soft_delete_edges` (which also sets `invalid_at`)- # is reserved for the contradiction detector.- deleted, failures = await _retract_edges(driver, uuids, user_id)- mode = "retracted from memory"-+ mode = "permanently deleted" if hard_delete else "retracted from memory"return MemoryForgetConfirmResponse(- message=_build_confirm_message(len(deleted), mode, failures),+ message=_build_confirm_message(len(result.deleted), mode, result.failures),session_id=session.session_id,- deleted_uuids=deleted,- failed_uuids=[f.uuid for f in failures],- failures=failures,+ deleted_uuids=result.deleted,+ failed_uuids=[f.uuid for f in result.failures],+ failures=result.failures,)+async def _retract_once_more_if_busy(+ scope: MemoryScope, uuids: list[str], hard: bool+) -> ForgetResult:+ """``retract``, tried again once after ``_BUSY_RETRY_SECONDS`` when+ memory was busy; a forget that finds it busy writes nothing, so the+ retry is safe."""+ result = await retract(scope, uuids, hard=hard)+ if not any(f.code == MemoryForgetFailureCode.BUSY for f in result.failures):+ return result+ await asyncio.sleep(_BUSY_RETRY_SECONDS)+ return await retract(scope, uuids, hard=hard)++def _build_confirm_message(deleted_count: int, mode: str, failures: list[MemoryForgetFailure]) -> str:@@ -324,107 +324,65 @@ def _build_confirm_message(return f"{summary} {len(failures)} failed — {detail}"-async def _retract_edges(- driver, uuids: list[str], user_id: str-) -> tuple[list[str], list[MemoryForgetFailure]]:- """System retraction — set ONLY ``expired_at`` on the edge.-- Per Snodgrass bi-temporal semantics (see ``dream/dreaming-graphiti.md``- §6.13), ``expired_at`` is *transaction time* ("we retracted the- record") and ``invalid_at`` is *valid time* ("the world changed").- User-initiated forget, dream demotion, and entity invalidation are- all system retractions and must NOT set ``invalid_at``.-- For contradiction detection (the world really did change) use- ``_soft_delete_edges`` below, which sets both.-- Matches the same edge types as ``_hard_delete_edges`` so that edges of- any type (RELATES_TO, MENTIONS, HAS_MEMBER) can be retracted.-- Returns ``(succeeded_uuids, failures)`` where each failure carries an- actionable reason (SECRT-2371) — a swallowed error used to look- identical to a no-op no-match.- """- deleted: list[str] = []- failures: list[MemoryForgetFailure] = []- for uuid in uuids:- try:- records, _, _ = await driver.execute_query(- """- MATCH ()-[e:MENTIONS|RELATES_TO|HAS_MEMBER {uuid: $uuid}]->()- SET e.expired_at = $now- RETURN e.uuid AS uuid- """,- uuid=uuid,- now=_now_iso(),- )- if records:- deleted.append(uuid)- else:- failures.append(- MemoryForgetFailure(- uuid=uuid,- code=MemoryForgetFailureCode.NO_MATCH,- reason=_NO_MATCH_REASON,- )- )- except Exception as exc:- logger.warning(- "Failed to retract edge %s for user %s",- uuid,- user_id[:12],- exc_info=True,- )- failures.append(- MemoryForgetFailure(- uuid=uuid,- code=MemoryForgetFailureCode.QUERY_ERROR,- reason=_delete_error_reason(exc),- )- )- return deleted, failures---async def _soft_delete_edges(- driver, uuids: list[str], user_id: str-) -> tuple[list[str], list[str]]:- """Bi-temporal invalidation — mark edges as both expired AND invalid.-- Reserved for the *contradiction detector*: when new evidence proves- a fact ceased being true in the world, set ``invalid_at`` (valid time)- in addition to ``expired_at`` (transaction time). User-initiated- forget should use ``_retract_edges`` instead; conflating the two- breaks the bi-temporal model (audit §6.13).-- Matches RELATES_TO, MENTIONS, HAS_MEMBER edges.- """- deleted = []- failed = []- for uuid in uuids:- try:- records, _, _ = await driver.execute_query(- """- MATCH ()-[e:MENTIONS|RELATES_TO|HAS_MEMBER {uuid: $uuid}]->()- SET e.invalid_at = $now,- e.expired_at = $now- RETURN e.uuid AS uuid- """,- uuid=uuid,- now=_now_iso(),- )- if records:- deleted.append(uuid)- else:- failed.append(uuid)- except Exception:- logger.warning(- "Failed to soft-delete edge %s for user %s",- uuid,- user_id[:12],- exc_info=True,- )- failed.append(uuid)- return deleted, failed+def _candidate(edge: EntityEdge) -> dict[str, str]:+ """One forget candidate, in the shape ``memory_forget_search`` returns."""+ valid_from, valid_to = fact_validity(edge)+ return {+ "uuid": edge.uuid,+ "fact": fact_text(edge),+ "valid_from": valid_from,+ "valid_to": valid_to,+ }+++def _forgotten_candidate(edge: EntityEdge) -> dict[str, str]:+ """A forgotten fact as a candidate for a permanent delete: that it+ exists and when, never what it said. Its sentence, the audit copy its+ forget kept (``fact_redacted``), its scope and the dates it held true are+ all left out, the forget keeping them from the model; the delete needs+ only the uuid. The two dates are read as timestamps and written out+ anew (``_timestamp``), so no text an edge property holds goes with them."""+ forgotten_at = edge.attributes.get("forgotten_at") or edge.expired_at+ return {+ "uuid": edge.uuid,+ "fact": FORGOTTEN_FACT,+ "stored_at": _timestamp(edge.created_at),+ "forgotten_at": _timestamp(forgotten_at),+ "forgotten": "true",+ }+++def _timestamp(value: Any) -> str:+ """``value`` as an ISO 8601 UTC timestamp to the second, or ``"unknown"``+ when it does not read as one (a missing value included)."""+ try:+ moment = datetime.fromisoformat(str(value))+ if moment.tzinfo is None:+ moment = moment.replace(tzinfo=timezone.utc)+ return moment.astimezone(timezone.utc).isoformat(timespec="seconds")+ except (ValueError, OverflowError):+ return "unknown"+++def _candidates_message(live: int, forgotten: int) -> str:+ """What ``memory_forget_search`` tells the model to do with its+ candidates: the live ones shown for the user to choose from, the+ forgotten ones offered for a permanent delete by count and date alone."""+ parts = []+ if live:+ parts.append(+ f"Found {live} candidate(s). Show these to the user and ask which to "+ "delete, then call tool:memory_forget_confirm with the UUIDs."+ )+ if forgotten:+ parts.append(+ f"{forgotten} memory(ies) matched that are already forgotten; what "+ "they said stays hidden, so do not guess at it. Tell the user how "+ "many and when they were stored and forgotten, and offer to delete "+ "them permanently: tool:memory_forget_confirm with their UUIDs and "+ "hard_delete=true."+ )+ return " ".join(parts)async def mark_edges_superseded(@@ -434,12 +392,13 @@ async def mark_edges_superseded(new_status: Literal["superseded", "contradicted"] = "superseded",user_id: str | None = None,group_id: str | None = None,+ expected_status: str | None = None,) -> tuple[list[str], list[str]]:"""Retract edges AND set the custom audit-trail ``status`` property.- Intended for the dream pass (P0.3 stale-fact deprecation): retract- the edge per ``_retract_edges`` semantics and stamp- ``status='superseded'`` (or ``'contradicted'``) plus+ Intended for the dream pass (P0.3 stale-fact deprecation): retire+ the edge (``expired_at``; ``invalid_at`` is left alone) and+ stamp ``status='superseded'`` (or ``'contradicted'``) plus``expiration_reason=<reason>`` so the demotion is queryable fromsearch (``WHERE e.status = 'superseded'``).@@ -447,37 +406,31 @@ async def mark_edges_superseded(against the per-user FalkorDB database, but when provided theCypher predicate also requires the edge's ``group_id`` to match soa future caller holding the wrong driver can't touch another- user's edges. ``None`` keeps the unscoped match (current- ratification behavior).+ user's edges. ``None`` keeps the unscoped match.++ The write lands only on a live fact (``recall.live_fact_predicate``), or,+ with ``expected_status``, on an unexpired, unforgotten edge still in that+ status, so a forget or other change made since the caller read the edge+ is never overwritten; an edge that no longer qualifies is reported failed.Returns ``(succeeded_uuids, failed_uuids)``."""deleted = []failed = []user_log = (user_id or "?")[:12]- edge_match = (- "MATCH ()-[e:RELATES_TO {uuid: $uuid, group_id: $group_id}]->()"- if group_id is not None- else "MATCH ()-[e:RELATES_TO {uuid: $uuid}]->()"+ query = _supersede_query(+ scoped=group_id is not None, guarded=expected_status is not None)- query = f"""- {edge_match}- SET e.expired_at = $now,- e.status = $new_status,- e.expiration_reason = $reason- RETURN e.uuid AS uuid- """+ params: dict[str, str] = {"new_status": new_status, "reason": reason}+ if group_id is not None:+ params["group_id"] = group_id+ if expected_status is not None:+ params["expected_status"] = expected_statusfor uuid in uuids:try:- params: dict[str, str] = {- "uuid": uuid,- "new_status": new_status,- "reason": reason,- "now": _now_iso(),- }- if group_id is not None:- params["group_id"] = group_id- records, _, _ = await driver.execute_query(query, **params)+ records, _, _ = await driver.execute_query(+ query, uuid=uuid, now=_now_iso(), **params+ )if records:deleted.append(uuid)else:@@ -493,19 +446,42 @@ async def mark_edges_superseded(return deleted, failed+def _supersede_query(*, scoped: bool, guarded: bool) -> str:+ """``mark_edges_superseded``'s Cypher, with its ``group_id`` match and its+ ``expected_status`` guard when asked for."""+ edge_match = (+ "MATCH ()-[e:RELATES_TO {uuid: $uuid, group_id: $group_id}]->()"+ if scoped+ else "MATCH ()-[e:RELATES_TO {uuid: $uuid}]->()"+ )+ guard = (+ "WHERE e.status = $expected_status AND e.expired_at IS NULL"+ " AND e.forgotten_at IS NULL"+ if guarded+ else f"WHERE {live_fact_predicate('e')}"+ )+ return f"""+ {edge_match}+ {guard}+ SET e.expired_at = $now,+ e.status = $new_status,+ e.expiration_reason = $reason+ RETURN e.uuid AS uuid+ """++async def invalidate_entity_direct_neighbors(driver,group_id: str,entity_uuid: str,reason: str,) -> list[str]:- """Demote every ``:RELATES_TO`` edge directly attached to an entity.+ """Demote every live ``:RELATES_TO`` edge directly attached to an entity.**Single-hop only** — does NOT propagate to neighbors-of-neighbors.The instinct to write ``[r:RELATES_TO*1..N]`` is exactly therunaway-demotion bug we are protecting against (P0.3b in the dream- spec). Keep the single-hop discipline; ratification (P0.4) re-promotes- good facts that get caught in the cascade.+ spec). Only live neighbours (``recall.live_fact_predicate``) are touched.Returns the list of edge UUIDs that were demoted. ``DISTINCT``matters: the undirected ``-[r]-`` pattern can yield the same edge@@ -513,9 +489,10 @@ async def invalidate_entity_direct_neighbors(demotion counts reported in ``DreamPassResult`` / the admin UI(the ``SET`` itself is idempotent)."""- query = """- MATCH (e:Entity {uuid: $entity_uuid, group_id: $group_id})+ query = f"""+ MATCH (e:Entity {{uuid: $entity_uuid, group_id: $group_id}})MATCH (e)-[r:RELATES_TO]-(other)+ WHERE {live_fact_predicate("r")}SET r.expired_at = $now,r.status = 'superseded',r.expiration_reason = $reason@@ -538,77 +515,3 @@ async def invalidate_entity_direct_neighbors(exc_info=True,)return []---async def _hard_delete_edges(- driver, uuids: list[str], user_id: str-) -> tuple[list[str], list[MemoryForgetFailure]]:- """Permanent removal — delete edges and clean up back-references.-- Uses graphiti's ``Edge.delete()`` pattern (handles MENTIONS,- RELATES_TO, HAS_MEMBER in one query). Does NOT delete orphaned- entity nodes — they may have summaries, embeddings, or future- connections. Cleans up episode ``entity_edges`` back-references.-- Returns ``(succeeded_uuids, failures)`` with a per-UUID reason on each- failure (SECRT-2371).- """- deleted: list[str] = []- failures: list[MemoryForgetFailure] = []- for uuid in uuids:- try:- # Use WITH to capture the uuid before DELETE so we don't- # access properties of deleted relationships (FalkorDB #1393).- # Single atomic query avoids TOCTOU between check and delete.- records, _, _ = await driver.execute_query(- """- MATCH ()-[e:MENTIONS|RELATES_TO|HAS_MEMBER {uuid: $uuid}]->()- WITH e.uuid AS uuid, e- DELETE e- RETURN uuid- """,- uuid=uuid,- )- if not records:- failures.append(- MemoryForgetFailure(- uuid=uuid,- code=MemoryForgetFailureCode.NO_MATCH,- reason=_NO_MATCH_REASON,- )- )- continue- # Edge was deleted — report success regardless of cleanup outcome.- deleted.append(uuid)- # Clean up episode back-references (best-effort).- try:- await driver.execute_query(- """- MATCH (ep:Episodic)- WHERE $uuid IN ep.entity_edges- SET ep.entity_edges = [x IN ep.entity_edges WHERE x <> $uuid]- """,- uuid=uuid,- )- except Exception:- logger.warning(- "Edge %s deleted but back-ref cleanup failed for user %s",- uuid,- user_id[:12],- exc_info=True,- )- except Exception as exc:- logger.warning(- "Failed to hard-delete edge %s for user %s",- uuid,- user_id[:12],- exc_info=True,- )- failures.append(- MemoryForgetFailure(- uuid=uuid,- code=MemoryForgetFailureCode.QUERY_ERROR,- reason=_delete_error_reason(exc),- )- )- return deleted, failuresdiff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_forget_retry_test.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_forget_retry_test.pynew file mode 100644index 0000000000..fe427f4a97--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_forget_retry_test.py@@ -0,0 +1,84 @@+"""The memory_forget_confirm tool when memory is busy: a forget that found+an ingestion holding the graph's write lock wrote nothing, so the tool waits+five seconds and tries once more, then reports it. ``retract`` is mocked;+its busy failure is pinned in ``graphiti/recall_forget_test.py``.+"""++from unittest.mock import AsyncMock, patch++import pytest++from backend.copilot.graphiti.memory_model import (+ ForgetResult,+ MemoryForgetFailure,+ MemoryForgetFailureCode,+)+from backend.copilot.model import ChatSession+from backend.copilot.tools.graphiti_forget import MemoryForgetConfirmTool+from backend.copilot.tools.models import MemoryForgetConfirmResponse++_MODULE = "backend.copilot.tools.graphiti_forget"+++async def _enabled(_user_id: str) -> bool:+ return True+++class TestForgetConfirmWhenMemoryIsBusy:+ """A forget that found memory busy wrote nothing: the tool waits five+ seconds and tries once more, then reports it."""++ @pytest.mark.asyncio+ async def test_a_busy_forget_is_tried_once_more(self) -> None:+ busy = ForgetResult(failures=[MemoryForgetFailure.busy("e1")])+ retract = AsyncMock(side_effect=[busy, ForgetResult(deleted=["e1"])])+ sleep = AsyncMock()+ session = ChatSession.new("user-abc", dry_run=False)+ with (+ patch(f"{_MODULE}.is_enabled_for_user", _enabled),+ patch(f"{_MODULE}.retract", retract),+ patch(f"{_MODULE}.asyncio.sleep", sleep),+ ):+ response = await MemoryForgetConfirmTool()._execute(+ "user-abc", session, uuids=["e1"]+ )++ assert isinstance(response, MemoryForgetConfirmResponse)+ assert (response.deleted_uuids, response.failed_uuids) == (["e1"], [])+ sleep.assert_awaited_once_with(5)+ assert retract.await_count == 2++ @pytest.mark.asyncio+ async def test_busy_twice_is_reported_as_busy(self) -> None:+ busy = ForgetResult(failures=[MemoryForgetFailure.busy("e1")])+ retract = AsyncMock(return_value=busy)+ session = ChatSession.new("user-abc", dry_run=False)+ with (+ patch(f"{_MODULE}.is_enabled_for_user", _enabled),+ patch(f"{_MODULE}.retract", retract),+ patch(f"{_MODULE}.asyncio.sleep", AsyncMock()),+ ):+ response = await MemoryForgetConfirmTool()._execute(+ "user-abc", session, uuids=["e1"]+ )++ assert isinstance(response, MemoryForgetConfirmResponse)+ assert [f.code for f in response.failures] == [MemoryForgetFailureCode.BUSY]+ assert "nothing was forgotten" in response.message+ assert retract.await_count == 2++ @pytest.mark.asyncio+ async def test_any_other_failure_is_not_retried(self) -> None:+ missing = ForgetResult(failures=[MemoryForgetFailure.no_match("e1")])+ retract = AsyncMock(return_value=missing)+ sleep = AsyncMock()+ session = ChatSession.new("user-abc", dry_run=False)+ with (+ patch(f"{_MODULE}.is_enabled_for_user", _enabled),+ patch(f"{_MODULE}.retract", retract),+ patch(f"{_MODULE}.asyncio.sleep", sleep),+ ):+ await MemoryForgetConfirmTool()._execute("user-abc", session, uuids=["e1"])++ sleep.assert_not_awaited()+ retract.assert_awaited_once()diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_forget_test.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_forget_test.pyindex 85c702f37b..d9b5d4ca88 100644--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_forget_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_forget_test.py@@ -1,9 +1,25 @@-"""Tests for graphiti_forget delete helpers."""+"""Tests for the memory_forget tools and the demotion helpers they share a+module with.-from unittest.mock import AsyncMock+The retraction itself (Cypher, per-uuid failures) is pinned in+``graphiti/recall_forget_test.py``; here the tools are exercised with that+layer either mocked or driven through a mock driver.+"""++from datetime import datetime, timezone+from unittest.mock import AsyncMock, MagicMock, patchimport pytest+from graphiti_core.edges import EntityEdge+from backend.copilot.graphiti import scope_lock+from backend.copilot.graphiti.memory_model import (+ ForgetResult,+ MemoryForgetFailure,+ MemoryForgetFailureCode,+)+from backend.copilot.graphiti.recall import FORGOTTEN_FACT, live_fact_predicate+from backend.copilot.graphiti.recall_fake_redis import FakeRedisfrom backend.copilot.graphiti.scope import MemoryScopefrom backend.copilot.model import ChatSessionfrom backend.copilot.tools.graphiti_forget import (@@ -11,300 +27,320 @@ from backend.copilot.tools.graphiti_forget import (MemoryForgetConfirmTool,MemoryForgetSearchTool,_build_confirm_message,- _hard_delete_edges,- _retract_edges,- _soft_delete_edges,+ _timestamp,invalidate_entity_direct_neighbors,mark_edges_superseded,)from backend.copilot.tools.models import (+ MemoryForgetCandidatesResponse,MemoryForgetConfirmResponse,- MemoryForgetFailure,- MemoryForgetFailureCode,)+_MODULE = "backend.copilot.tools.graphiti_forget"-class TestSoftDeleteOverReportsSuccess:- """_soft_delete_edges always appends UUID to deleted list even when- the Cypher MATCH found no edge (query succeeds but matches nothing).- """- @pytest.mark.asyncio- async def test_reports_failure_when_no_edge_matched(self) -> None:- driver = AsyncMock()- # execute_query returns empty result set — no edge matched- driver.execute_query.return_value = ([], None, None)+async def _enabled(_user_id: str) -> bool:+ return True- deleted, failed = await _soft_delete_edges(- driver, ["nonexistent-uuid"], "test-user"- )- # Should NOT report success when nothing was actually updated- assert deleted == [], f"over-reported success: {deleted}"- assert failed == ["nonexistent-uuid"][email protected](autouse=True)+def lock_redis(mocker) -> FakeRedis:+ """The real ``retract`` takes the graph's write lock; keep it in memory."""+ redis = FakeRedis()+ mocker.patch.object(+ scope_lock, "get_redis_async", mocker.AsyncMock(return_value=redis)+ )+ return redis-class TestExpertMemoryScope:- @pytest.mark.asyncio- async def test_forget_search_uses_expert_memory_group(- self, monkeypatch: pytest.MonkeyPatch- ) -> None:- async def _enabled(_user_id: str) -> bool:- return True-- client = type("Client", (), {"search": AsyncMock(return_value=[])})()- opened: list[str] = []- async def _get_client(group_id: str):- opened.append(group_id)- return client+def _mock_driver(*results) -> AsyncMock:+ """A FalkorDB driver whose queries return ``results`` in order, for+ driving the real ``retract`` from the confirm tool."""+ driver = AsyncMock()+ driver.execute_query.side_effect = [(r, [], None) for r in results]+ return driver- monkeypatch.setattr(- "backend.copilot.tools.graphiti_forget.is_enabled_for_user", _enabled- )- monkeypatch.setattr(- "backend.copilot.tools.graphiti_forget.get_graphiti_client", _get_client- )- session = ChatSession.new(- "user-abc",- dry_run=False,- expert_id="expert-1",- )- await MemoryForgetSearchTool()._execute(- "user-abc",- session,- query="private fact",- )+class TestExpertMemoryScope:+ @pytest.mark.asyncio+ async def test_forget_search_uses_expert_memory_group(self) -> None:+ search_facts = AsyncMock(return_value=[])+ session = ChatSession.new("user-abc", dry_run=False, expert_id="expert-1")+ with (+ patch(f"{_MODULE}.is_enabled_for_user", _enabled),+ patch(f"{_MODULE}.search_facts", search_facts),+ ):+ await MemoryForgetSearchTool()._execute(+ "user-abc", session, query="private fact"+ )- expert_group = MemoryScope.for_expert("user-abc", "expert-1").group_id- assert opened == [expert_group]- client.search.assert_awaited_once_with(- query="private fact",- group_ids=[expert_group],- num_results=10,+ search_facts.assert_awaited_once_with(+ MemoryScope.for_expert("user-abc", "expert-1"), "private fact", limit=10)@pytest.mark.asyncio- async def test_forget_confirm_uses_expert_memory_group(- self, monkeypatch: pytest.MonkeyPatch- ) -> None:- async def _enabled(_user_id: str) -> bool:- return True+ async def test_forget_confirm_uses_expert_memory_group(self) -> None:+ retract = AsyncMock(return_value=ForgetResult())+ session = ChatSession.new("user-abc", dry_run=False, expert_id="expert-1")+ with (+ patch(f"{_MODULE}.is_enabled_for_user", _enabled),+ patch(f"{_MODULE}.retract", retract),+ ):+ await MemoryForgetConfirmTool()._execute(+ "user-abc", session, uuids=["private-edge"]+ )- driver = AsyncMock()- driver.execute_query.return_value = ([], None, None)- client = type("Client", (), {"graph_driver": driver})()- opened: list[str] = []+ retract.assert_awaited_once_with(+ MemoryScope.for_expert("user-abc", "expert-1"),+ ["private-edge"],+ hard=False,+ )- async def _get_client(group_id: str):- opened.append(group_id)- return client- monkeypatch.setattr(- "backend.copilot.tools.graphiti_forget.is_enabled_for_user", _enabled- )- monkeypatch.setattr(- "backend.copilot.tools.graphiti_forget.get_graphiti_client", _get_client+class TestForgetSearchCandidates:+ @pytest.mark.asyncio+ async def test_candidates_carry_uuid_fact_and_validity(self) -> None:+ edge = EntityEdge(+ uuid="e1",+ group_id="user_user-abc",+ source_node_uuid="a",+ target_node_uuid="b",+ created_at=datetime(2025, 1, 1, tzinfo=timezone.utc),+ name="works_on",+ fact="Alice works on Atlas",+ valid_at=datetime(2025, 1, 1, tzinfo=timezone.utc),)+ session = ChatSession.new("user-abc", dry_run=False)+ with (+ patch(f"{_MODULE}.is_enabled_for_user", _enabled),+ patch(f"{_MODULE}.search_facts", AsyncMock(return_value=[edge])),+ ):+ response = await MemoryForgetSearchTool()._execute(+ "user-abc", session, query="atlas"+ )- session = ChatSession.new(- "user-abc",- dry_run=False,- expert_id="expert-1",- )- await MemoryForgetConfirmTool()._execute(- "user-abc",- session,- uuids=["private-edge"],- )+ assert isinstance(response, MemoryForgetCandidatesResponse)+ assert response.candidates == [+ {+ "uuid": "e1",+ "fact": "Alice works on Atlas",+ "valid_from": "2025-01-01 00:00:00+00:00",+ "valid_to": "present",+ }+ ]- assert opened == [MemoryScope.for_expert("user-abc", "expert-1").group_id]+class TestForgetSearchForgottenCandidates:+ @staticmethod+ def _edge(+ uuid: str,+ fact: str,+ expired_at: datetime | None = None,+ valid_at: datetime | None = None,+ **attributes: str,+ ) -> EntityEdge:+ return EntityEdge(+ uuid=uuid,+ group_id="user_user-abc",+ source_node_uuid="a",+ target_node_uuid="b",+ created_at=datetime(2025, 1, 1, tzinfo=timezone.utc),+ name="works_on",+ fact=fact,+ expired_at=expired_at,+ valid_at=valid_at,+ attributes=attributes,+ )-class TestSoftDeleteNoMatchReportsFailure:- """When the query returns empty records (no edge with that UUID exists- in the database), _soft_delete_edges should report it as failed.- """+ async def _search(self, include_forgotten: bool, live, forgotten):+ session = ChatSession.new("user-abc", dry_run=False)+ search_forgotten = AsyncMock(return_value=forgotten)+ with (+ patch(f"{_MODULE}.is_enabled_for_user", _enabled),+ patch(f"{_MODULE}.search_facts", AsyncMock(return_value=live)),+ patch(f"{_MODULE}.search_forgotten_facts", search_forgotten),+ patch(f"{_MODULE}.recheck", AsyncMock(return_value=(live, []))),+ ):+ response = await MemoryForgetSearchTool()._execute(+ "user-abc",+ session,+ query="atlas",+ include_forgotten=include_forgotten,+ )+ assert isinstance(response, MemoryForgetCandidatesResponse)+ return response, search_forgotten@pytest.mark.asyncio- async def test_soft_delete_handles_non_relates_to_edge(self) -> None:- driver = AsyncMock()- # Simulate: RELATES_TO match returns nothing (edge is MENTIONS type)- driver.execute_query.return_value = ([], None, None)-- deleted, failed = await _soft_delete_edges(- driver, ["mentions-edge-uuid"], "test-user"- )- # With the bug, this reports success even though nothing was updated- assert "mentions-edge-uuid" not in deleted+ async def test_are_left_out_unless_asked_for(self) -> None:+ live = [self._edge("e1", "Bob leads Atlas")]+ response, search_forgotten = await self._search(False, live, [])-class TestHardDeleteBasicFlow:- """Verify _hard_delete_edges calls the right queries."""+ search_forgotten.assert_not_awaited()+ assert [c["uuid"] for c in response.candidates] == ["e1"]+ assert "forgotten" not in response.candidates[0]@pytest.mark.asyncio- async def test_hard_delete_calls_both_queries(self) -> None:- driver = AsyncMock()- # First call (delete) returns a matched record, second (cleanup) returns empty- driver.execute_query.side_effect = [- ([{"uuid": "uuid-1"}], None, None),- ([], None, None),+ async def test_when_asked_come_after_the_live_ones_saying_nothing_they_said(+ self,+ ) -> None:+ """A soft forget can be made permanent, but nothing a forgotten fact+ said goes back to the model: not the audit copy its forget kept+ (``fact_redacted``), not the text a fact forgotten before the policy+ still has, not its scope nor the dates it held true. Only that it+ exists, when it was stored and when forgotten, searched for in the+ whole response the model reads."""+ live = [self._edge("e1", "Bob leads Atlas")]+ gone = [+ self._edge(+ "e2",+ FORGOTTEN_FACT,+ valid_at=datetime(2024, 3, 9, tzinfo=timezone.utc),+ fact_redacted="Alice works on the secret Atlas plan",+ name_redacted="works_on_secret_plan",+ scope="project:Orion",+ forgotten_at="2026-01-01T00:00:00+00:00",+ ),+ # Forgotten before the policy: its own text was never scrubbed.+ self._edge(+ "e3",+ "Carol owns the Atlas budget",+ expired_at=datetime(2025, 6, 1, tzinfo=timezone.utc),+ ),]- deleted, failed = await _hard_delete_edges(driver, ["uuid-1"], "test-user")- assert deleted == ["uuid-1"]- assert failed == []- # Should call: 1) delete edge, 2) clean episode back-refs- assert driver.execute_query.call_count == 2-- @pytest.mark.asyncio- async def test_hard_delete_reports_failure_when_no_edge_matched(self) -> None:- driver = AsyncMock()- # Delete query returns no records — edge not found- driver.execute_query.return_value = ([], None, None)+ response, search_forgotten = await self._search(True, live, gone)- deleted, failed = await _hard_delete_edges(- driver, ["nonexistent-uuid"], "test-user"+ search_forgotten.assert_awaited_once_with(+ MemoryScope.for_user("user-abc"), "atlas", limit=10)- assert deleted == []- assert [f.uuid for f in failed] == ["nonexistent-uuid"]- assert failed[0].reason # actionable, non-empty reason- assert failed[0].code == MemoryForgetFailureCode.NO_MATCH- # Only the delete query should run — cleanup skipped- assert driver.execute_query.call_count == 1+ assert response.candidates[1:] == [+ {+ "uuid": "e2",+ "fact": FORGOTTEN_FACT,+ "stored_at": "2025-01-01T00:00:00+00:00",+ "forgotten_at": "2026-01-01T00:00:00+00:00",+ "forgotten": "true",+ },+ {+ "uuid": "e3",+ "fact": FORGOTTEN_FACT,+ "stored_at": "2025-01-01T00:00:00+00:00",+ "forgotten_at": "2025-06-01T00:00:00+00:00",+ "forgotten": "true",+ },+ ]+ assert response.candidates[0]["fact"] == "Bob leads Atlas"+ seen = response.model_dump_json()+ for said in ("secret", "Carol", "budget", "Orion", "2024-03-09"):+ assert said not in seen, said+ assert "hard_delete=true" in response.message+ @pytest.mark.asyncio+ async def test_their_dates_are_only_ever_timestamps(self) -> None:+ """Codex's probe: a ``forgotten_at`` that is not a timestamp (one an+ extraction invented before attribute answers were filtered) reads+ "unknown", and what it held reaches no part of the response. A real+ one is written out anew, in UTC to the second."""+ secret = "Alice works on the secret Atlas plan"+ gone = [+ self._edge("e2", FORGOTTEN_FACT, forgotten_at=secret),+ self._edge(+ "e3", FORGOTTEN_FACT, forgotten_at="2026-01-01T07:30:15.250+02:00"+ ),+ self._edge("e4", FORGOTTEN_FACT, forgotten_at="0001-01-01T00:00:00+05:00"),+ ]-class TestRetractEdgesSnodgrass:- """`_retract_edges` is the system-retraction soft delete — must set- ONLY `expired_at`, never `invalid_at`. Conflating the two breaks the- bi-temporal model (see graphiti audit §6.13)."""+ response, _ = await self._search(True, [], gone)++ assert [c["forgotten_at"] for c in response.candidates] == [+ "unknown",+ "2026-01-01T05:30:15+00:00",+ "unknown",+ ]+ assert secret not in response.model_dump_json()++ def test_a_date_that_is_not_one_reads_unknown(self) -> None:+ naive = datetime(2025, 3, 4, 5, 6, 7)++ assert [+ _timestamp(value)+ for value in (None, "", "yesterday", 12, naive, "2025-03-04")+ ] == [+ "unknown",+ "unknown",+ "unknown",+ "unknown",+ "2025-03-04T05:06:07+00:00",+ "2025-03-04T00:00:00+00:00",+ ]@pytest.mark.asyncio- async def test_retract_sets_only_expired_at(self) -> None:- driver = AsyncMock()- driver.execute_query.return_value = ([{"uuid": "u1"}], None, None)+ async def test_are_offered_when_nothing_live_matches(self) -> None:+ gone = [self._edge("e2", FORGOTTEN_FACT, fact_redacted="Alice works on Atlas")]- await _retract_edges(driver, ["u1"], "test-user")+ response, _ = await self._search(True, [], gone)- query = driver.execute_query.call_args.args[0]- assert "e.expired_at = $now" in query- # ``now`` parameter is bound from Python (FalkorDB doesn't- # implement Cypher's no-arg ``datetime()``).- assert "now" in driver.execute_query.call_args.kwargs- # Critical contract: must NOT touch invalid_at- assert "invalid_at" not in query+ assert [c["uuid"] for c in response.candidates] == ["e2"]- @pytest.mark.asyncio- async def test_retract_never_uses_falkordb_incompatible_datetime(self) -> None:- """Root cause of SECRT-2371: the shipped soft delete used Cypher's- no-arg ``datetime()``, which FalkorDB rejects with "Unknown function- 'datetime'". The raised error was swallowed, so every soft delete- reported "0 invalidated, N failed" while hard delete (no datetime())- worked. The timestamp must be bound from Python as ``$now``."""- driver = AsyncMock()- driver.execute_query.return_value = ([{"uuid": "u1"}], None, None)- await _retract_edges(driver, ["u1"], "test-user")+class TestForgetConfirmModes:+ @pytest.mark.asyncio+ async def test_hard_delete_asks_for_a_hard_retract(self) -> None:+ retract = AsyncMock(return_value=ForgetResult(deleted=["e1"]))+ session = ChatSession.new("user-abc", dry_run=False)+ with (+ patch(f"{_MODULE}.is_enabled_for_user", _enabled),+ patch(f"{_MODULE}.retract", retract),+ ):+ response = await MemoryForgetConfirmTool()._execute(+ "user-abc", session, uuids=["e1"], hard_delete=True+ )- query = driver.execute_query.call_args.args[0]- assert "datetime()" not in query- assert "$now" in query+ assert retract.await_args is not None+ assert retract.await_args.kwargs == {"hard": True}+ assert isinstance(response, MemoryForgetConfirmResponse)+ assert response.message == "1 memory edge(s) permanently deleted."@pytest.mark.asyncio- async def test_retract_reports_failure_on_no_match(self) -> None:- driver = AsyncMock()- driver.execute_query.return_value = ([], None, None)+ async def test_unavailable_graph_is_an_error_response(self) -> None:+ session = ChatSession.new("user-abc", dry_run=False)+ with (+ patch(f"{_MODULE}.is_enabled_for_user", _enabled),+ patch(f"{_MODULE}.retract", AsyncMock(side_effect=OSError("refused"))),+ ):+ response = await MemoryForgetConfirmTool()._execute(+ "user-abc", session, uuids=["e1"]+ )- deleted, failed = await _retract_edges(driver, ["missing"], "test-user")- assert deleted == []- assert [f.uuid for f in failed] == ["missing"]- # No-match must carry an actionable reason, not a bare count.- assert failed[0].reason- assert failed[0].code == MemoryForgetFailureCode.NO_MATCH+ assert not isinstance(response, MemoryForgetConfirmResponse)+ assert "temporarily unavailable" in response.messageclass TestForgetFailuresAreActionable:"""SECRT-2371: soft delete must not fail silently. Every failure — whetherthe query errored or matched nothing — has to carry a per-UUID reason sothe model can act (retry, hard-delete, or tell the user) instead of seeing- a bare "0 invalidated, N failed"."""+ a bare "0 invalidated, N failed". Driven through the real ``retract``+ with a mock driver."""@pytest.mark.asyncio- async def test_retract_surfaces_query_exception_reason(self) -> None:- driver = AsyncMock()- # Reproduces the original root cause: FalkorDB rejected the no-arg- # Cypher datetime() with "Unknown function 'datetime'". The exception- # used to be swallowed, leaving the model no clue why it failed.- driver.execute_query.side_effect = RuntimeError("Unknown function 'datetime'")-- deleted, failed = await _retract_edges(driver, ["u1"], "test-user")-- assert deleted == []- assert [f.uuid for f in failed] == ["u1"]- assert "datetime" in failed[0].reason- assert failed[0].code == MemoryForgetFailureCode.QUERY_ERROR-- @pytest.mark.asyncio- async def test_retract_distinguishes_no_match_from_error(self) -> None:- driver = AsyncMock()- # First uuid errors, second matches nothing.- driver.execute_query.side_effect = [- RuntimeError("boom"),- ([], None, None),- ]-- deleted, failed = await _retract_edges(- driver, ["errored", "missing"], "test-user"- )-- assert deleted == []- by_uuid = {f.uuid: f for f in failed}- assert "boom" in by_uuid["errored"].reason- # No-match reason must be distinct from the error reason.- assert by_uuid["missing"].reason != by_uuid["errored"].reason- # ...and the machine-switchable codes distinguish them too.- assert by_uuid["errored"].code == MemoryForgetFailureCode.QUERY_ERROR- assert by_uuid["missing"].code == MemoryForgetFailureCode.NO_MATCH-- @pytest.mark.asyncio- async def test_hard_delete_surfaces_query_exception_reason(self) -> None:- driver = AsyncMock()- driver.execute_query.side_effect = RuntimeError("edge locked")-- deleted, failed = await _hard_delete_edges(driver, ["u1"], "test-user")-- assert deleted == []- assert [f.uuid for f in failed] == ["u1"]- assert "edge locked" in failed[0].reason- assert failed[0].code == MemoryForgetFailureCode.QUERY_ERROR-- @pytest.mark.asyncio- async def test_confirm_tool_reports_reasons_in_response(self, monkeypatch) -> None:+ async def test_confirm_tool_reports_reasons_in_response(self) -> None:"""End to end: a soft delete that matches nothing must return theper-UUID reason in both the structured `failures` field and thehuman-readable message — not a bare "0 invalidated, 1 failed"."""-- async def _enabled(_user_id: str) -> bool:- return True-- driver = AsyncMock()- driver.execute_query.return_value = ([], None, None)- client = type("Client", (), {"graph_driver": driver})()-- async def _get_client(_group_id: str):- return client-- monkeypatch.setattr(- "backend.copilot.tools.graphiti_forget.is_enabled_for_user", _enabled- )- monkeypatch.setattr(- "backend.copilot.tools.graphiti_forget.get_graphiti_client", _get_client- )-+ driver = _mock_driver([]) # the edge lookup finds nothingsession = ChatSession.new("user-abc", dry_run=False)- response = await MemoryForgetConfirmTool()._execute(- "user-abc", session, uuids=["missing-uuid"]- )+ with (+ patch(f"{_MODULE}.is_enabled_for_user", _enabled),+ patch(+ "backend.copilot.graphiti.recall_forget.open_driver",+ MagicMock(return_value=driver),+ ),+ ):+ response = await MemoryForgetConfirmTool()._execute(+ "user-abc", session, uuids=["missing-uuid"]+ )assert isinstance(response, MemoryForgetConfirmResponse)assert response.deleted_uuids == []@@ -316,36 +352,28 @@ class TestForgetFailuresAreActionable:assert "missing-uuid" in response.message@pytest.mark.asyncio- async def test_confirm_tool_mixed_batch_reports_both(self, monkeypatch) -> None:+ async def test_confirm_tool_mixed_batch_reports_both(self) -> None:"""A batch where some UUIDs delete and some fail must co-populate`deleted_uuids` and `failures`, and the message must carry BOTH thesuccess count and the per-UUID failure detail."""-- async def _enabled(_user_id: str) -> bool:- return True-- driver = AsyncMock()- # _retract_edges runs one query per UUID: first matches, second doesn't.- driver.execute_query.side_effect = [- ([{"uuid": "kept"}], None, None),- ([], None, None),- ]- client = type("Client", (), {"graph_driver": driver})()-- async def _get_client(_group_id: str):- return client-- monkeypatch.setattr(- "backend.copilot.tools.graphiti_forget.is_enabled_for_user", _enabled- )- monkeypatch.setattr(- "backend.copilot.tools.graphiti_forget.get_graphiti_client", _get_client+ driver = _mock_driver(+ [{"uuid": "kept"}], # lookup: only "kept" exists+ [{"uuid": "kept"}], # retract "kept"+ [], # scrub its sentence+ [], # find the entities to scrub (none)+ [], # redact its episodes)-session = ChatSession.new("user-abc", dry_run=False)- response = await MemoryForgetConfirmTool()._execute(- "user-abc", session, uuids=["kept", "gone"]- )+ with (+ patch(f"{_MODULE}.is_enabled_for_user", _enabled),+ patch(+ "backend.copilot.graphiti.recall_forget.open_driver",+ MagicMock(return_value=driver),+ ),+ ):+ response = await MemoryForgetConfirmTool()._execute(+ "user-abc", session, uuids=["kept", "gone"]+ )assert isinstance(response, MemoryForgetConfirmResponse)assert response.deleted_uuids == ["kept"]@@ -355,6 +383,38 @@ class TestForgetFailuresAreActionable:assert "1 failed" in response.messageassert "gone" in response.message+ @pytest.mark.asyncio+ async def test_confirm_tool_reports_a_failed_clean_up(self) -> None:+ """Retracted, but the episode redaction failed: the model is told the+ fact is forgotten and that the clean-up did not finish."""+ driver = AsyncMock()+ driver.execute_query.side_effect = [+ ([{"uuid": "u1"}], [], None), # lookup+ ([{"uuid": "u1"}], [], None), # retract+ ([], [], None), # scrub its sentence+ ([], [], None), # find the entities to scrub (none)+ RuntimeError("down"), # redact its episodes+ ]+ session = ChatSession.new("user-abc", dry_run=False)+ with (+ patch(f"{_MODULE}.is_enabled_for_user", _enabled),+ patch(+ "backend.copilot.graphiti.recall_forget.open_driver",+ MagicMock(return_value=driver),+ ),+ ):+ response = await MemoryForgetConfirmTool()._execute(+ "user-abc", session, uuids=["u1"]+ )++ assert isinstance(response, MemoryForgetConfirmResponse)+ assert response.deleted_uuids == ["u1"]+ assert [f.code for f in response.failures] == [+ MemoryForgetFailureCode.CLEANUP_ERROR+ ]+ assert "no longer recalled" in response.message+ assert "RuntimeError: down" in response.message+class TestBuildConfirmMessage:"""`_build_confirm_message` formatting: the no-failure early return, the@@ -387,25 +447,6 @@ class TestBuildConfirmMessage:assert len(message) < _MAX_FAILURE_DETAIL * 300-class TestSoftDeleteContradictionPath:- """`_soft_delete_edges` is reserved for the contradiction detector- and MUST still set both expired_at AND invalid_at."""-- @pytest.mark.asyncio- async def test_soft_delete_sets_both_timestamps(self) -> None:- driver = AsyncMock()- driver.execute_query.return_value = ([{"uuid": "u1"}], None, None)-- await _soft_delete_edges(driver, ["u1"], "test-user")-- query = driver.execute_query.call_args.args[0]- assert "e.invalid_at = $now" in query- assert "e.expired_at = $now" in query- # ``now`` parameter is bound from Python (FalkorDB doesn't- # implement Cypher's no-arg ``datetime()``).- assert "now" in driver.execute_query.call_args.kwargs--class TestMarkEdgesSuperseded:@pytest.mark.asyncioasync def test_sets_status_and_reason(self) -> None:@@ -486,6 +527,42 @@ class TestMarkEdgesSuperseded:assert "group_id" not in queryassert "group_id" not in driver.execute_query.call_args.kwargs+ @pytest.mark.asyncio+ async def test_expected_status_makes_the_write_conditional(self) -> None:+ """The ratification sweep's guard: only an edge still in that status+ and unexpired is touched, so a forget made since it was listed is+ kept, and the edge is reported failed."""+ driver = AsyncMock()+ driver.execute_query.return_value = ([], None, None) # no longer tentative++ deleted, failed = await mark_edges_superseded(+ driver, ["u1"], reason="unratified", expected_status="tentative"+ )++ assert (deleted, failed) == ([], ["u1"])+ query = driver.execute_query.call_args.args[0]+ assert (+ "WHERE e.status = $expected_status AND e.expired_at IS NULL"+ " AND e.forgotten_at IS NULL" in query+ )+ assert driver.execute_query.call_args.kwargs["expected_status"] == "tentative"++ @pytest.mark.asyncio+ async def test_without_expected_status_only_a_live_fact_is_written(self) -> None:+ """The dream's demotions: an edge retired or forgotten since the dream+ read it is not overwritten, and is reported failed."""+ driver = AsyncMock()+ driver.execute_query.return_value = ([], None, None) # forgotten meanwhile++ deleted, failed = await mark_edges_superseded(+ driver, ["u1"], reason="stale_fact"+ )++ assert (deleted, failed) == ([], ["u1"])+ query = driver.execute_query.call_args.args[0]+ assert f"WHERE {live_fact_predicate('e')}" in query+ assert "expected_status" not in driver.execute_query.call_args.kwargs+class TestInvalidateEntityDirectNeighbors:"""Single-hop demotion. The instinct to write [r:RELATES_TO*1..N] is@@ -514,6 +591,9 @@ class TestInvalidateEntityDirectNeighbors:# MUST set status + reason for audit trailassert "r.status = 'superseded'" in queryassert "r.expiration_reason = $reason" in query+ # MUST leave a retired or forgotten neighbour's audit fields alone+ assert f"WHERE {live_fact_predicate('r')}" in query+ assert "forgotten_at =" not in query@pytest.mark.asyncioasync def test_returns_distinct_edge_uuids(self) -> None:diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_search.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_search.pyindex 1d1bff472c..e065bc73a1 100644--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_search.py+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_search.py@@ -2,20 +2,17 @@import asyncioimport logging-from datetime import datetime, timezonefrom typing import Any-from backend.copilot.graphiti._format import (- extract_episode_body,- extract_episode_body_raw,- extract_episode_timestamp,- extract_fact,- extract_temporal_validity,-)-from backend.copilot.graphiti.client import get_graphiti_client+from graphiti_core.edges import EntityEdge+from backend.copilot.graphiti.config import is_enabled_for_user+from backend.copilot.graphiti.recall import recent_episodes, record_hit, search_facts+from backend.copilot.graphiti.recall_recheck import recheck+from backend.copilot.graphiti.recall_render import episode_scope, render, render_episodefrom backend.copilot.graphiti.scope import MemoryScopefrom backend.copilot.model import ChatSession+from backend.util.background import spawn_background_taskfrom .base import BaseToolfrom .models import ErrorResponse, MemorySearchResponse, ToolResponseBase@@ -23,6 +20,8 @@ from .models import ErrorResponse, MemorySearchResponse, ToolResponseBaselogger = logging.getLogger(__name__)_MAX_LIMIT = 50+# Recent raw episodes returned next to the facts.+_RECENT_EPISODES = 5class MemorySearchTool(BaseTool):@@ -102,7 +101,7 @@ class MemorySearchTool(BaseTool):limit = min(limit, _MAX_LIMIT)try:- group_id = MemoryScope.build(user_id, session.expert_id).group_id+ memory_scope = MemoryScope.build(user_id, session.expert_id)except ValueError:return ErrorResponse(message="Invalid user ID for memory operations.",@@ -110,20 +109,13 @@ class MemorySearchTool(BaseTool):)try:- client = await get_graphiti_client(group_id)-edges, episodes = await asyncio.gather(- client.search(- query=query,- group_ids=[group_id],- num_results=limit,- ),- client.retrieve_episodes(- reference_time=datetime.now(timezone.utc),- group_ids=[group_id],- last_n=5,- ),+ search_facts(memory_scope, query, limit=limit),+ recent_episodes(memory_scope, _RECENT_EPISODES),)+ # The last read before rendering: a forget that answered while+ # the search ran is not shown (``recall_recheck.py``).+ edges, episodes = await recheck(memory_scope, edges, episodes)except Exception:logger.warning("Memory search failed for user %s", user_id[:12], exc_info=True@@ -133,15 +125,16 @@ class MemorySearchTool(BaseTool):session_id=session.session_id,)- facts = _format_edges(edges)-- # Scope hard-filter: if a scope was requested, filter episodes- # whose MemoryEnvelope JSON contains a different scope.- # Skip redundant _format_episodes() when scope is set.- if scope:- recent = _filter_episodes_by_scope(episodes, scope)- else:- recent = _format_episodes(episodes)+ _count_hits(memory_scope, edges)+ facts = [render(edge) for edge in edges]+ # Scope hard-filter: when a scope is requested, drop episodes whose+ # MemoryEnvelope names a different one (plain conversation counts as+ # ``real:global``).+ recent = [+ render_episode(ep)+ for ep in episodes+ if not scope or episode_scope(ep) == scope+ ]if not facts and not recent:return MemorySearchResponse(@@ -164,51 +157,14 @@ class MemorySearchTool(BaseTool):)-def _format_edges(edges) -> list[str]:- results = []- for e in edges:- fact = extract_fact(e)- valid_from, valid_to = extract_temporal_validity(e)- results.append(f"{fact} (valid: {valid_from} — {valid_to})")- return results---def _format_episodes(episodes) -> list[str]:- results = []- for ep in episodes:- ts = extract_episode_timestamp(ep)- body = extract_episode_body(ep)- results.append(f"[{ts}] {body}")- return results+def _count_hits(memory_scope: MemoryScope, edges: list[EntityEdge]) -> None:+ """Count the returned facts as used, so a tentative one can be ratified.--def _filter_episodes_by_scope(episodes, scope: str) -> list[str]:- """Filter episodes by scope — hard filter on MemoryEnvelope JSON content.-- Episodes that are plain conversation text (not JSON envelopes) are- included by default since they have no scope metadata and belong- to the implicit ``real:global`` scope.-- Uses ``extract_episode_body_raw`` (no truncation) for JSON parsing- so that long MemoryEnvelope payloads are parsed correctly.+ Detached, like warm context's hit hook: the answer never waits on Redis."""- import json-- results = []- for ep in episodes:- raw_body = extract_episode_body_raw(ep)- try:- data = json.loads(raw_body)- if not isinstance(data, dict):- raise TypeError("non-dict JSON")- ep_scope = data.get("scope", "real:global")- if ep_scope != scope:- continue- except (json.JSONDecodeError, TypeError):- # Not JSON or non-dict JSON — plain conversation episode, treat as real:global- if scope != "real:global":- continue- display_body = extract_episode_body(ep)- ts = extract_episode_timestamp(ep)- results.append(f"[{ts}] {display_body}")- return results+ if not edges:+ return+ spawn_background_task(+ record_hit(memory_scope, [edge.uuid for edge in edges]),+ name=f"memory-search-hits-{memory_scope.owner_user_id[:12]}",+ )diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_search_test.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_search_test.pyindex 469e99a4ce..4f8852b9f9 100644--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_search_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_search_test.py@@ -1,112 +1,168 @@-"""Tests for graphiti_search helper functions."""+"""Tests for the memory_search tool."""-from types import SimpleNamespace-from unittest.mock import AsyncMock, patch+from datetime import datetime, timezone+from unittest.mock import AsyncMock, MagicMock, patchimport pytest+from graphiti_core.edges import EntityEdge+from graphiti_core.nodes import EpisodeType, EpisodicNodefrom backend.copilot.graphiti.memory_model import MemoryEnvelope, MemoryKind, SourceKindfrom backend.copilot.graphiti.scope import MemoryScopefrom backend.copilot.model import ChatSession-from backend.copilot.tools.graphiti_search import (- MemorySearchTool,- _filter_episodes_by_scope,- _format_episodes,-)+from backend.copilot.tools.graphiti_search import MemorySearchToolfrom backend.copilot.tools.models import MemorySearchResponse+_MODULE = "backend.copilot.tools.graphiti_search"+_NOW = datetime(2025, 1, 1, tzinfo=timezone.utc)-async def test_expert_session_searches_only_expert_memory_group() -> None:- session = ChatSession.new(- "user-1",- dry_run=False,- expert_id="expert-1",++def _edge(uuid: str, fact: str) -> EntityEdge:+ return EntityEdge(+ uuid=uuid,+ group_id="user_user-1",+ source_node_uuid="a",+ target_node_uuid="b",+ created_at=_NOW,+ name="relates",+ fact=fact,+ valid_at=_NOW,+ attributes={"status": "active"},)- client = SimpleNamespace(- search=AsyncMock(return_value=[]),- retrieve_episodes=AsyncMock(return_value=[]),+++def _episode(content: str) -> EpisodicNode:+ return EpisodicNode(+ name="ep",+ group_id="user_user-1",+ source=EpisodeType.text,+ source_description="chat",+ content=content,+ created_at=_NOW,+ valid_at=_NOW,)++async def _search(+ edges: list[EntityEdge],+ episodes: list[EpisodicNode],+ *,+ expert_id: str | None = None,+ still: tuple[list[EntityEdge], list[EpisodicNode]] | None = None,+ **tool_kwargs,+):+ """Run the tool with both recall reads, their recheck (``still``, or+ everything read) and the hit recorder mocked."""+ session = ChatSession.new("user-1", dry_run=False, expert_id=expert_id)+ search_facts = AsyncMock(return_value=edges)+ recent_episodes = AsyncMock(return_value=episodes)+ recheck = AsyncMock(return_value=still or (edges, episodes))+ record_hit = MagicMock(return_value="hit-coroutine")+ spawn = MagicMock()with (- patch(- "backend.copilot.tools.graphiti_search.is_enabled_for_user",- new_callable=AsyncMock,- return_value=True,- ),- patch(- "backend.copilot.tools.graphiti_search.get_graphiti_client",- new_callable=AsyncMock,- return_value=client,- ) as get_client_mock,+ patch(f"{_MODULE}.is_enabled_for_user", AsyncMock(return_value=True)),+ patch(f"{_MODULE}.search_facts", search_facts),+ patch(f"{_MODULE}.recent_episodes", recent_episodes),+ patch(f"{_MODULE}.recheck", recheck),+ patch(f"{_MODULE}.record_hit", record_hit),+ patch(f"{_MODULE}.spawn_background_task", spawn),):result = await MemorySearchTool()._execute(- "user-1",- session,- query="private fact",+ "user-1", session, query="private fact", **tool_kwargs)+ return result, search_facts, recent_episodes, record_hit, spawn+++async def test_expert_session_searches_only_expert_memory_group() -> None:+ result, search_facts, recent_episodes, _, _ = await _search(+ [], [], expert_id="expert-1"+ )assert isinstance(result, MemorySearchResponse)- expert_group = MemoryScope.for_expert("user-1", "expert-1").group_id- get_client_mock.assert_awaited_once_with(expert_group)- client.search.assert_awaited_once_with(- query="private fact",- group_ids=[expert_group],- num_results=15,+ expert_scope = MemoryScope.for_expert("user-1", "expert-1")+ search_facts.assert_awaited_once_with(expert_scope, "private fact", limit=15)+ recent_episodes.assert_awaited_once_with(expert_scope, 5)+++async def test_renders_facts_and_episodes() -> None:+ result, *_ = await _search(+ [_edge("e1", "Alice works on Atlas")], [_episode("talked about Atlas")])- assert client.retrieve_episodes.await_args.kwargs["group_ids"] == [expert_group]---class TestFilterEpisodesByScopeTruncation:- """extract_episode_body() truncates to 500 chars. A MemoryEnvelope- with a long content field exceeds that limit, producing invalid JSON.- _filter_episodes_by_scope then treats it as a plain-text episode- (real:global), leaking project-scoped data into global results.- """-- def test_long_envelope_filtered_by_scope(self) -> None:- envelope = MemoryEnvelope(- content="x" * 600,- source_kind=SourceKind.user_asserted,- scope="project:crm",- memory_kind=MemoryKind.fact,- )- ep = SimpleNamespace(- content=envelope.model_dump_json(),- created_at="2025-01-01T00:00:00Z",- )- # Requesting real:global scope — this project:crm episode should be excluded- results = _filter_episodes_by_scope([ep], "real:global")- assert (- results == []- ), f"project-scoped episode leaked into global results: {results}"-- def test_short_envelope_filtered_correctly(self) -> None:- """Short envelopes (under 500 chars) are parsed correctly."""- envelope = MemoryEnvelope(- content="short note",- scope="project:crm",- )- ep = SimpleNamespace(- content=envelope.model_dump_json(),- created_at="2025-01-01T00:00:00Z",- )- results = _filter_episodes_by_scope([ep], "real:global")- assert results == []+ assert isinstance(result, MemorySearchResponse)+ assert result.facts == [+ "Alice works on Atlas (valid: 2025-01-01 00:00:00+00:00 — present)"+ ]+ assert result.recent_episodes == ["[2025-01-01 00:00:00+00:00] talked about Atlas"]-class TestRedundantFormatting:- """_format_episodes is called even when scope filter will overwrite it.- Not a correctness bug, but verify the scope path doesn't depend on it.- """- def test_scope_filter_independent_of_format_episodes(self) -> None:- envelope = MemoryEnvelope(content="note", scope="real:global")- ep = SimpleNamespace(- content=envelope.model_dump_json(),- created_at="2025-01-01T00:00:00Z",- )- from_format = _format_episodes([ep])- from_scope = _filter_episodes_by_scope([ep], "real:global")- assert len(from_format) == 1- assert len(from_scope) == 1+async def test_only_what_the_recheck_still_finds_is_shown_and_counted() -> None:+ """A forget that answered while the search ran: the last read before+ rendering (``recall_recheck.recheck``) no longer finds the fact or its+ episode, so neither is shown or counted as a hit."""+ kept = _edge("e2", "Bob leads Atlas")+ edges = [_edge("e1", "Alice works on Atlas"), kept]++ result, _, _, record_hit, _ = await _search(+ edges, [_episode("Alice works on Atlas")], still=([kept], [])+ )++ assert isinstance(result, MemorySearchResponse)+ assert [fact.split(" (")[0] for fact in result.facts] == ["Bob leads Atlas"]+ assert result.recent_episodes == []+ record_hit.assert_called_once_with(MemoryScope.for_user("user-1"), ["e2"])+++async def test_returned_facts_are_counted_as_hits() -> None:+ """memory_search feeds the ratification sweep: a tentative fact the+ model retrieves here has been used, like one warm context surfaced."""+ edges = [_edge("e1", "fact one"), _edge("e2", "fact two")]++ _, _, _, record_hit, spawn = await _search(edges, [])++ record_hit.assert_called_once_with(MemoryScope.for_user("user-1"), ["e1", "e2"])+ spawn.assert_called_once()+ assert spawn.call_args.args == ("hit-coroutine",)+++async def test_no_facts_no_hit_task() -> None:+ _, _, _, record_hit, spawn = await _search([], [_episode("just chatting")])++ record_hit.assert_not_called()+ spawn.assert_not_called()+++async def test_scope_filter_drops_other_scopes_even_for_long_envelopes() -> None:+ """A MemoryEnvelope longer than the 500-char display cut is still scoped+ by its full body, so a project memory cannot leak into a global search."""+ long_project = MemoryEnvelope(+ content="x" * 600,+ source_kind=SourceKind.user_asserted,+ scope="project:crm",+ memory_kind=MemoryKind.fact,+ ).model_dump_json()+ episodes = [_episode(long_project), _episode("plain conversation")]++ result, *_ = await _search([], episodes, scope="real:global")++ assert isinstance(result, MemorySearchResponse)+ assert result.recent_episodes == ["[2025-01-01 00:00:00+00:00] plain conversation"]+++async def test_search_failure_is_reported_not_raised() -> None:+ session = ChatSession.new("user-1", dry_run=False)+ with (+ patch(f"{_MODULE}.is_enabled_for_user", AsyncMock(return_value=True)),+ patch(f"{_MODULE}.search_facts", AsyncMock(side_effect=RuntimeError("down"))),+ patch(f"{_MODULE}.recent_episodes", AsyncMock(return_value=[])),+ ):+ result = await MemorySearchTool()._execute("user-1", session, query="x")++ assert "temporarily unavailable" in result.messagediff --git a/autogpt_platform/backend/backend/copilot/tools/models.py b/autogpt_platform/backend/backend/copilot/tools/models.pyindex 545e5940f9..69eea2c192 100644--- a/autogpt_platform/backend/backend/copilot/tools/models.py+++ b/autogpt_platform/backend/backend/copilot/tools/models.py@@ -6,6 +6,7 @@ from typing import Any, Literal, Selffrom pydantic import BaseModel, Field, PrivateAttr, model_validator+from backend.copilot.graphiti.memory_model import MemoryForgetFailurefrom backend.copilot.tools.execution_utils import NodeFailureSummaryfrom backend.data.graph import BaseGraph, GraphTriggerInfofrom backend.data.model import CredentialsMetaInput@@ -1407,31 +1408,6 @@ class MemoryForgetCandidatesResponse(ToolResponseBase):candidates: list[dict[str, str]] = Field(default_factory=list)-class MemoryForgetFailureCode(str, Enum):- """Stable, machine-switchable reason a forget delete failed.-- The frontend/model can branch on this code (retry vs. give up) without- parsing the free-text ``reason``. New codes may be added over time, so- consumers must tolerate unknown values.- """-- NO_MATCH = "no_match"- QUERY_ERROR = "query_error"---class MemoryForgetFailure(BaseModel):- """One edge that could not be deleted, with an actionable reason.-- Surfaced so the assistant (and user) can tell *why* a delete failed —- e.g. the edge was not found vs. the query itself errored — instead of a- bare "N failed" count that gives the model nothing to act on.- """-- uuid: str- code: MemoryForgetFailureCode- reason: str--class MemoryForgetConfirmResponse(ToolResponseBase):"""Response after deleting specific memory edges."""diff --git a/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py b/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.pyindex 492f5ba4df..c1e2ed9c10 100644--- a/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py@@ -258,6 +258,14 @@ from ._test_data import make_session# The margin is deliberate and is the same exception the wire budget's #14476# note names: this is queued while dev is still moving, and a measured-plus-one# ceiling reds the queue's merge ref on the next reworded description.+# Bumped 77_014 -> 77_401 for memory_forget_search's `include_forgotten`+# argument (#14972): a soft forget left a fact the chat could no longer find,+# so it could never be made permanent with hard_delete. No tool was added.+# Measured on this branch merged with dev 8432f734f5:+# merged tree, before 76,975 (89 tools)+# + include_forgotten +126 77,101 (89 tools)+# + headroom +300 77,401+# The same deliberate margin as the line above, for the same reason.# Bumped 77_014 -> 77_176 for connect_integration's managed `ayrshare`# provider (SECRT-2878): the enum entry, its place in the supported list, one# sentence telling the model it returns the account-linking link (never a key@@ -267,7 +275,11 @@ from ._test_data import make_session# dev 583fc0bb4e 76,975 (89 tools)# + this PR's connect_integration +200 77,175 (89 tools)# Plus one.-_CHAR_BUDGET = 77_176+# Both bumps met in the memory-core stack's merge with dev f05d2d7393;+# re-measured on that merged tree (89 tools), with the stack's margin:+# merged tree 77,301 (89 tools)+# + headroom +300 77,601+_CHAR_BUDGET = 77_601@pytest.fixture(scope="module")@@ -462,7 +474,15 @@ def test_total_schema_char_budget() -> None:# 69,461, 22 under the line above; find_capability's expert kind adds 27.# merged tree 69,488# + headroom +300 69,788-_SESSION_WIRE_BUDGET = 69_788+#+# Re-measured on the memory-core stack merged with dev f05d2d7393. Neither+# side moved this line, but each grew the largest session under it: the+# stack's memory_forget_search `include_forgotten` (69,609 alone) and dev's+# connect_integration `ayrshare` (69,687 alone). Together they measure 20+# over it. Same headroom, for the same reason.+# merged tree 69,808+# + headroom +300 70,108+_SESSION_WIRE_BUDGET = 70_108def test_largest_declared_session_wire_budget() -> None:diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 5d9ba792cf..4650b1ba8e 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -23152,6 +23152,21 @@"title": "Entity Invalidation Count","default": 0},+ "dropped_forgotten": {+ "type": "integer",+ "title": "Dropped Forgotten",+ "default": 0+ },+ "pending_writes": {+ "type": "integer",+ "title": "Pending Writes",+ "default": 0+ },+ "failed_writes": {+ "type": "integer",+ "title": "Failed Writes",+ "default": 0+ },"summary_for_user": {"type": "string","title": "Summary For User",@@ -26053,6 +26068,10 @@"summary": {"anyOf": [{ "type": "string" }, { "type": "null" }],"title": "Summary"+ },+ "hard_deleted_at": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Hard Deleted At"}},"type": "object",@@ -26806,7 +26825,7 @@"type": "string","enum": ["drained", "skipped", "timed_out"],"title": "IngestionDrainStatus",- "description": "Fate of a dream pass's enqueued graph writes at the moment the pass\nwas reported complete.\n\nThree distinct states, so consumers (and a future Memory Visualizer)\ncan tell a healthy by-design skip from a real failure:\n\n * ``drained`` — the pass's own episodes were confirmed landed in the\n graph before the pass returned (sync path), or the pass enqueued\n nothing to drain. Healthy.\n * ``skipped`` — the drain was intentionally not run to avoid stalling\n a shared serial loop (batch path). The episodes process\n fire-and-forget; healthy by design, NOT a failure signal.\n * ``timed_out`` — the sync-path drain was attempted but did not finish\n within its budget. The reported write/proposal counts include\n episodes still queued in-process, which are lost if the worker's pod\n restarts. This is the only value that warrants an operator warning."+ "description": "Fate of a dream pass's enqueued graph writes at the moment the pass\nwas reported complete.\n\nThree distinct states, so consumers (and a future Memory Visualizer)\ncan tell a healthy by-design skip from a real failure:\n\n * ``drained`` — the worker finished every one of the pass's own\n episodes before the pass returned (sync path): each was written,\n dropped for resting on a forget (``dropped_forgotten``), or failed\n with a logged error; or the pass enqueued nothing to drain. Healthy.\n * ``skipped`` — the drain was intentionally not run to avoid stalling\n a shared serial loop (batch path). The episodes process\n fire-and-forget, counted pending; healthy by design, NOT a failure\n signal.\n * ``timed_out`` — the sync-path drain was attempted but did not finish\n within its budget. The episodes not written yet are counted pending,\n not written; they stay queued in-process, and are lost if the\n worker's pod restarts. This is the only value that warrants an\n operator warning."},"InputValidationErrorResponse": {"properties": {@@ -28387,7 +28406,7 @@},"MemoryForgetFailureCode": {"type": "string",- "enum": ["no_match", "query_error"],+ "enum": ["no_match", "query_error", "cleanup_error", "busy"],"title": "MemoryForgetFailureCode","description": "Stable, machine-switchable reason a forget delete failed.\n\nThe frontend/model can branch on this code (retry vs. give up) without\nparsing the free-text ``reason``. New codes may be added over time, so\nconsumers must tolerate unknown values."},