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): persist MemoryFact edge attributes (status/provenance/source_kind) (#13390)
Nicholas Tindleeb9d1ecce22b
diff --git a/autogpt_platform/backend/backend/copilot/dream/apply.py b/autogpt_platform/backend/backend/copilot/dream/apply.pyindex 9a5fda9d2d..9ade412571 100644--- a/autogpt_platform/backend/backend/copilot/dream/apply.py+++ b/autogpt_platform/backend/backend/copilot/dream/apply.py@@ -68,6 +68,24 @@ def _episode_name(pass_id: str, phase: str, counter: int) -> str:return f"dream_{pass_id}_{phase}_{counter:03d}"+def _edge_metadata(envelope: MemoryEnvelope) -> dict:+ """Cypher-serializable MemoryFact attributes from a dream envelope.++ Stamped onto the edges the envelope's episode newly creates (see+ ``ingest._stamp_edge_metadata``) so dream provenance/status/source_kind+ land deterministically on the edge — graphiti's text-based attribute+ extraction can't recover them from the episode body. Enums are reduced+ to their string values; ``confidence``/``provenance`` may be None.+ """+ return {+ "status": envelope.status.value,+ "source_kind": envelope.source_kind.value,+ "scope": envelope.scope,+ "confidence": envelope.confidence,+ "provenance": envelope.provenance,+ }++async def _write_consolidated_fact(user_id: str,pass_id: str,@@ -94,6 +112,7 @@ async def _write_consolidated_fact(f"{','.join(fact.source_episode_uuids[:5])}"),is_json=True,+ edge_metadata=_edge_metadata(envelope),)@@ -125,6 +144,7 @@ async def _write_proposed_finding(episode_body=envelope.model_dump_json(),source_description="; ".join(description_parts),is_json=True,+ edge_metadata=_edge_metadata(envelope),)diff --git a/autogpt_platform/backend/backend/copilot/dream/apply_test.py b/autogpt_platform/backend/backend/copilot/dream/apply_test.pyindex 6a7b120634..c22fb9bc40 100644--- a/autogpt_platform/backend/backend/copilot/dream/apply_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/apply_test.py@@ -100,6 +100,12 @@ async def test_writes_become_active_envelopes():body = call_kwargs["episode_body"]assert '"status":"active"' in bodyassert '"source_kind":"assistant_derived"' in body+ # #13389: edge_metadata is threaded so the write's envelope fields land+ # ON the edge (not just in the episode body). Active consolidated fact.+ em = call_kwargs["edge_metadata"]+ assert em["status"] == "active"+ assert em["source_kind"] == "assistant_derived"+ assert em["provenance"].startswith("dream:p-abc:consolidate")@pytest.mark.asyncio@@ -121,6 +127,12 @@ async def test_proposals_become_tentative_envelopes():assert call_kwargs["name"].startswith("dream_p-2_recombine_")body = call_kwargs["episode_body"]assert '"status":"tentative"' in body+ # #13389: a proposal rides edge_metadata as tentative so ratification+ # can find it on the edge.+ em = call_kwargs["edge_metadata"]+ assert em["status"] == "tentative"+ assert em["source_kind"] == "assistant_derived"+ assert em["provenance"].startswith("dream:p-2:recombine")@pytest.mark.asynciodiff --git a/autogpt_platform/backend/backend/copilot/graphiti/backfill_edge_props_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/backfill_edge_props_integration_test.pyindex daa3ab2111..c696e69ebe 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/backfill_edge_props_integration_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/backfill_edge_props_integration_test.py@@ -1,11 +1,16 @@"""Integration test for the P-1.2 backfill migration.The migration script-(``backend/copilot/graphiti/migrations/backfill_edge_props.py``) sets-default ``status='active'`` on every existing ``:RELATES_TO`` edge that-lacks the property. This file pins that contract against live FalkorDB:--- Pre-migration edges without ``status`` are updated to ``status='active'``.+(``backend/copilot/graphiti/migrations/backfill_edge_props.py``) stamps+``status``/``source_kind``/``scope`` on every existing ``:RELATES_TO``+edge that lacks them, recovering ``source_kind``/``scope`` from the+originating ``MemoryEnvelope`` JSON that survives in+``:Episodic.content``. This file pins that contract against live+FalkorDB:++- Pre-migration edges without metadata get the defaults.+- Edges sourced from an envelope episode recover the envelope's real+ ``source_kind``/``scope`` (the #13389 mislabeling fix).- The migration is idempotent — re-running is a no-op.- Edges that already have ``status`` set are not overwritten (e.g. analready-superseded edge stays superseded).@@ -13,7 +18,8 @@ lacks the property. This file pins that contract against live FalkorDB:import pytest-from .migrations.backfill_edge_props import BACKFILL_QUERY, backfill_one_user+from .memory_model import MemoryEnvelope, SourceKind+from .migrations.backfill_edge_props import backfill_graph, backfill_one_user@pytest.mark.integration@@ -22,7 +28,7 @@ async def test_backfill_adds_active_status_to_legacy_edges(clean_graph,) -> None:"""Edges created before P-1.1 have no ``status`` property; backfill- sets them to 'active'. Run the query directly against the driver+ sets them to 'active'. Run against the fixture driver directly(the higher-level ``backfill_one_user`` walks user_id → group_id,which we sidestep here since the fixture mints its own group_id)."""@@ -49,17 +55,71 @@ async def test_backfill_adds_active_status_to_legacy_edges()assert records[0]["n"] == 2- records, _, _ = await driver.execute_query(BACKFILL_QUERY)- assert records[0]["updated"] == 2+ assert await backfill_graph(driver) == 2- # Every edge now has status='active'.+ # Every edge now has status='active' and the historical defaults.records, _, _ = await driver.execute_query("""MATCH ()-[e:RELATES_TO]->()- RETURN e.status AS status+ RETURN e.status AS status, e.source_kind AS source_kind, e.scope AS scope""")assert {r["status"] for r in records} == {"active"}+ assert {r["source_kind"] for r in records} == {"user_asserted"}+ assert {r["scope"] for r in records} == {"real:global"}+++async def test_backfill_recovers_envelope_source_kind_and_scope(+ clean_graph,+) -> None:+ """The #13389 mislabeling fix: an edge whose originating envelope was+ ``assistant_derived``/project-scoped must recover those values from+ the surviving ``:Episodic.content`` JSON — NOT be branded+ ``user_asserted``/``real:global``, which would grant a dream-derived+ fact the user-level trust ratification keys on."""+ driver, group_id = clean_graph++ dream_body = MemoryEnvelope(+ content="Atlas deploys are gated on the smoke suite",+ source_kind=SourceKind.assistant_derived,+ scope="project:atlas",+ ).model_dump_json()++ await driver.execute_query(+ """+ CREATE+ (a:Entity {uuid: 'a', name: 'Alice', group_id: $gid}),+ (b:Entity {uuid: 'b', name: 'Atlas', group_id: $gid}),+ (ep:Episodic {uuid: 'ep-dream', group_id: $gid, content: $dream_body}),+ (conv:Episodic {uuid: 'ep-conv', group_id: $gid,+ content: 'User: hello\\nAssistant: hi'}),+ (a)-[:RELATES_TO {uuid: 'dream-edge', group_id: $gid,+ fact: 'deploy gate', episodes: ['ep-dream']}]->(b),+ (a)-[:RELATES_TO {uuid: 'merged-edge', group_id: $gid,+ fact: 'merged fact',+ episodes: ['ep-dream', 'ep-conv']}]->(b)+ """,+ gid=group_id,+ dream_body=dream_body,+ )++ assert await backfill_graph(driver) == 2++ records, _, _ = await driver.execute_query(+ """+ MATCH ()-[e:RELATES_TO]->()+ RETURN e.uuid AS uuid, e.source_kind AS source_kind, e.scope AS scope+ """+ )+ by_uuid = {r["uuid"]: r for r in records}+ # Sole-sourced by the dream envelope → envelope values recovered.+ assert by_uuid["dream-edge"]["source_kind"] == "assistant_derived"+ assert by_uuid["dream-edge"]["scope"] == "project:atlas"+ # Merged with a conversation episode → most-trusted source wins.+ assert by_uuid["merged-edge"]["source_kind"] == "user_asserted"+ assert by_uuid["merged-edge"]["scope"] == "real:global"@pytest.mark.integration@@ -78,11 +138,8 @@ async def test_backfill_is_idempotent(clean_graph) -> None:gid=group_id,)- records, _, _ = await driver.execute_query(BACKFILL_QUERY)- assert records[0]["updated"] == 1-- records, _, _ = await driver.execute_query(BACKFILL_QUERY)- assert records[0]["updated"] == 0, "second run must update zero edges"+ assert await backfill_graph(driver) == 1+ assert await backfill_graph(driver) == 0, "second run must update zero edges"@pytest.mark.integration@@ -93,6 +150,12 @@ async def test_backfill_does_not_overwrite_existing_status("""If an edge already has ``status`` set to something other than'active' (e.g. an edge that was demoted before the backfill ran),the migration must NOT clobber it.++ A demoted edge is still touched by the backfill: ``mark_edges_superseded``+ only writes ``status``/``expired_at``/``expiration_reason``, so its+ ``source_kind``/``scope`` are NULL and legitimately get the defaults.+ The coalesce guards ``status`` so the existing 'superseded' value+ survives."""driver, group_id = clean_graph@@ -117,18 +180,25 @@ async def test_backfill_does_not_overwrite_existing_status(gid=group_id,)- records, _, _ = await driver.execute_query(BACKFILL_QUERY)- assert records[0]["updated"] == 1, "only the unstatussed edge should be touched"+ # Both edges match the WHERE clause: the legacy edge needs all three+ # defaults, and the superseded edge still needs source_kind/scope.+ assert await backfill_graph(driver) == 2- # Verify the superseded edge kept its status.+ # Verify the superseded edge kept its status while gaining the other+ # backfilled defaults.records, _, _ = await driver.execute_query("""MATCH ()-[e:RELATES_TO {uuid: 'already-superseded'}]->()- RETURN e.status AS status, e.expiration_reason AS reason+ RETURN e.status AS status,+ e.expiration_reason AS reason,+ e.source_kind AS source_kind,+ e.scope AS scope""")assert records[0]["status"] == "superseded"assert records[0]["reason"] == "previous_pass"+ assert records[0]["source_kind"] == "user_asserted"+ assert records[0]["scope"] == "real:global"# And the legacy edge got 'active'.records, _, _ = await driver.execute_query(diff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest.pyindex 4d46e6f664..12605807ad 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/ingest.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/ingest.py@@ -69,6 +69,111 @@ 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).+_STAMP_EDGE_METADATA_QUERY = """+MATCH ()-[e:RELATES_TO]->()+WHERE e.uuid IN $uuids AND e.group_id = $gid+SET e.status = $status,+ e.source_kind = $source_kind,+ e.scope = $scope,+ e.confidence = $confidence,+ e.provenance = $provenance+"""+++def _is_past(timestamp: datetime | None, *, now: datetime) -> bool:+ """True if a temporal-validity timestamp marks an edge already retired.++ ``invalid_at`` is graphiti's scheduled end-of-validity and can be+ FUTURE-dated (a fact true now with a known end date) — such an edge is+ still live. Only a past/now timestamp means the edge is already out of+ validity. Mirrors the backfill migration's deliberate "don't treat+ future invalid_at as retired" reasoning.+ """+ return timestamp is not None and timestamp <= now+++async def _stamp_edge_metadata(+ client,+ group_id: str,+ result,+ edge_metadata: dict,+ user_id: str,+) -> None:+ """Deterministically write dream-envelope metadata onto the edges a+ dream episode just created.++ Safety invariant (verified against graphiti-core 0.28.2): a freshly+ EXTRACTED edge is built with ``episodes == [episode.uuid]``+ (edge_operations.create path). When graphiti instead DEDUPES the+ extracted fact into a pre-existing edge, it appends our uuid to that+ edge's ``episodes`` (so ``len >= 2``); invalidated edges predate this+ episode entirely. Therefore ``episodes == [episode_uuid]`` selects+ ONLY edges solely sourced by this dream write — never a user-authored+ edge a dream fact happened to merge into. That is what prevents+ clobbering ``source_kind='user_asserted'`` provenance on real user+ facts. Anything not matching is left exactly as graphiti wrote it.++ Best-effort: a stamp failure must not fail ingestion — the edge still+ exists with graphiti's defaults; only the dream metadata is missing.+ """+ episode_uuid = result.episode.uuid+ now = datetime.now(timezone.utc)+ targets = [+ edge.uuid+ for edge in result.edges+ # Sole-sourced by THIS episode (newly created, not a dedup-merge or+ # an invalidated pre-existing edge) AND temporally live: graphiti can+ # self-expire a brand-new edge within the same add_episode — a newer+ # contradicting fact sets ``expired_at``, and a past ``invalid_at``+ # marks it already out of validity. Stamping a live status onto a+ # retired edge would contradict its temporal fields, so skip those.+ # A FUTURE ``invalid_at`` is still live (true now, ends later) and+ # MUST be stamped, or its dream status/source_kind/provenance never+ # land and ratification filters miss it.+ if list(edge.episodes) == [episode_uuid]+ and edge.expired_at is None+ and not _is_past(edge.invalid_at, now=now)+ ]+ if not targets:+ return+ # The sole producer (``dream/apply._edge_metadata``) always supplies+ # these three from a ``MemoryEnvelope`` whose fields are non-null by+ # default. Guard anyway so a future partial payload skips the stamp+ # loudly instead of silently NULL-clobbering required edge fields.+ required = ("status", "source_kind", "scope")+ missing = [key for key in required if edge_metadata.get(key) is None]+ if missing:+ logger.warning(+ "Incomplete edge_metadata for user %s (missing %s) — skipping "+ "stamp; edges keep graphiti defaults",+ user_id[:12],+ ",".join(missing),+ )+ return+ try:+ await client.driver.execute_query(+ _STAMP_EDGE_METADATA_QUERY,+ uuids=targets,+ gid=group_id,+ status=edge_metadata["status"],+ source_kind=edge_metadata["source_kind"],+ scope=edge_metadata["scope"],+ confidence=edge_metadata.get("confidence"),+ provenance=edge_metadata.get("provenance"),+ )+ except Exception:+ logger.warning(+ "Failed to stamp dream edge metadata for user %s (%d edges) — "+ "edges keep graphiti defaults",+ user_id[:12],+ len(targets),+ exc_info=True,+ )++async def _ingestion_worker(user_id: str, queue: asyncio.Queue) -> None:"""Process episodes sequentially for a single user.@@ -91,16 +196,30 @@ async def _ingestion_worker(user_id: str, queue: asyncio.Queue) -> None:try:group_id = derive_group_id(user_id)client = await get_graphiti_client(group_id)+ # ``_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.- await client.add_episode(+ result = await client.add_episode(**payload,entity_types=ENTITY_TYPES,edge_types=EDGE_TYPES,edge_type_map=EDGE_TYPE_MAP,)+ # 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 Exception:logger.warning("Graphiti ingestion failed for user %s",@@ -212,6 +331,7 @@ async def enqueue_episode(episode_body: str,source_description: str = "Conversation memory",is_json: bool = False,+ edge_metadata: dict | None = None,) -> bool:"""Enqueue an arbitrary episode for background ingestion.@@ -222,6 +342,13 @@ async def enqueue_episode(is_json: When ``True``, ingest as ``EpisodeType.json`` (forstructured ``MemoryEnvelope`` payloads). Otherwise uses``EpisodeType.text``.+ edge_metadata: Optional ``{status, source_kind, scope, confidence,+ provenance}`` (Cypher-serializable scalars) stamped onto the+ edges this episode newly creates, AFTER ingestion. Dream+ writes pass this so their envelope metadata lands on the edge+ deterministically (graphiti's extractor can't recover it from+ the episode text). ``None`` (conversation turns / memory-store)+ leaves edges at MemoryFact defaults — no behavior change.Returns ``True`` if the episode was queued, ``False`` if it was dropped."""@@ -257,6 +384,9 @@ async def enqueue_episode("reference_time": datetime.now(timezone.utc),"group_id": group_id,"custom_extraction_instructions": CUSTOM_EXTRACTION_INSTRUCTIONS,+ # Sidecar — popped by the worker before the add_episode+ # spread; carries dream metadata for post-write stamping.+ "_edge_metadata": edge_metadata,})return Truediff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest_dedup_invariant_test.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest_dedup_invariant_test.pynew file mode 100644index 0000000000..2303b64489--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/graphiti/ingest_dedup_invariant_test.py@@ -0,0 +1,219 @@+"""Pin graphiti-core's episode-provenance behavior on edges.++``_stamp_edge_metadata`` (``ingest.py``) relies on an upstream invariant+to select ONLY edges solely sourced by the dream episode it just wrote:++- a freshly EXTRACTED edge is created with ``episodes == [episode.uuid]``;+- when graphiti DEDUPES an extracted fact into a pre-existing edge, it+ APPENDS the new episode uuid to that edge's ``episodes`` (len >= 2).++The stamping unit tests hand-build edges, so they cannot detect upstream+drift. ``pyproject.toml`` pins ``graphiti-core = "^0.28.2"`` — minor/patch+bumps are allowed — so this file exercises the REAL graphiti-core+extraction/resolution code (only the LLM boundary is stubbed) and fails+loudly if a bump changes the invariant, instead of letting+``_stamp_edge_metadata`` silently mis-stamp user-authored edges.+"""++from datetime import datetime, timezone++import pytest+from graphiti_core.edges import EntityEdge+from graphiti_core.graphiti_types import GraphitiClients+from graphiti_core.llm_client.client import LLMClient+from graphiti_core.llm_client.config import ModelSize+from graphiti_core.nodes import EntityNode, EpisodeType, EpisodicNode+from graphiti_core.prompts.models import Message+from graphiti_core.utils.maintenance.edge_operations import (+ extract_edges,+ resolve_extracted_edge,+)++GROUP_ID = "test_group"+++class ScriptedLLMClient(LLMClient):+ """Real ``LLMClient`` whose responses are canned per response_model.++ Records every requested model so tests can assert which graphiti+ code path ran (e.g. the exact-match fast path must not call the LLM).+ """++ def __init__(self, responses: dict[str, dict] | None = None) -> None:+ super().__init__(config=None, cache=False)+ self.responses = responses or {}+ self.prompts_requested: list[str] = []++ async def _generate_response(+ self,+ messages: list[Message],+ response_model=None,+ max_tokens: int = 16384,+ model_size: ModelSize = ModelSize.medium,+ ) -> dict:+ name = response_model.__name__ if response_model else "raw"+ self.prompts_requested.append(name)+ if name not in self.responses:+ raise AssertionError(f"Unexpected LLM call for {name}")+ return self.responses[name]+++def _now() -> datetime:+ return datetime.now(timezone.utc)+++def _entity(name: str) -> EntityNode:+ return EntityNode(+ name=name, group_id=GROUP_ID, labels=["Entity"], created_at=_now()+ )+++def _episode(content: str) -> EpisodicNode:+ return EpisodicNode(+ name="test-episode",+ group_id=GROUP_ID,+ source=EpisodeType.json,+ source_description="test",+ content=content,+ valid_at=_now(),+ created_at=_now(),+ )+++def _existing_edge(+ source: EntityNode, target: EntityNode, fact: str, episodes: list[str]+) -> EntityEdge:+ return EntityEdge(+ source_node_uuid=source.uuid,+ target_node_uuid=target.uuid,+ name="works_on",+ group_id=GROUP_ID,+ fact=fact,+ episodes=episodes,+ created_at=_now(),+ )+++async def test_extracted_edge_is_sole_sourced_by_its_episode():+ """Creation leg: real ``extract_edges`` builds every new edge with+ ``episodes == [episode.uuid]`` — the exact predicate+ ``_stamp_edge_metadata`` uses to recognize edges this write created."""+ alice, atlas = _entity("Alice"), _entity("Atlas")+ episode = _episode('{"content": "Alice works on Atlas"}')+ llm = ScriptedLLMClient(+ {+ "ExtractedEdges": {+ "edges": [+ {+ "source_entity_name": "Alice",+ "target_entity_name": "Atlas",+ "relation_type": "works_on",+ "fact": "Alice works on Atlas",+ "valid_at": None,+ "invalid_at": None,+ }+ ]+ }+ }+ )+ clients = GraphitiClients.model_construct(llm_client=llm)++ edges = await extract_edges(+ clients,+ episode,+ [alice, atlas],+ previous_episodes=[],+ edge_type_map={},+ group_id=GROUP_ID,+ )++ assert len(edges) == 1+ assert list(edges[0].episodes) == [episode.uuid]+++async def test_exact_match_dedup_appends_episode_to_existing_edge():+ """Fast-path dedup (identical fact text): graphiti reuses the existing+ edge and APPENDS the new episode uuid — len >= 2, so the stamp+ selector must skip it. No LLM involved on this path."""+ alice, atlas = _entity("Alice"), _entity("Atlas")+ episode = _episode('{"content": "Alice works on Atlas"}')+ existing = _existing_edge(+ alice, atlas, "Alice works on Atlas", episodes=["older-episode"]+ )+ extracted = _existing_edge(+ alice, atlas, "Alice works on Atlas", episodes=[episode.uuid]+ )+ llm = ScriptedLLMClient()++ resolved, invalidated, duplicates = await resolve_extracted_edge(+ llm,+ extracted,+ related_edges=[existing],+ existing_edges=[],+ episode=episode,+ )++ assert resolved.uuid == existing.uuid+ assert list(resolved.episodes) == ["older-episode", episode.uuid]+ assert llm.prompts_requested == [], "fast path must not call the LLM"+++async def test_llm_dedup_appends_episode_to_existing_edge():+ """LLM-judged dedup (paraphrased fact): the resolved edge is the+ pre-existing one with the new episode uuid appended — len >= 2."""+ alice, atlas = _entity("Alice"), _entity("Atlas")+ episode = _episode('{"content": "Alice is a member of the Atlas project"}')+ existing = _existing_edge(+ alice, atlas, "Alice works on Atlas", episodes=["older-episode"]+ )+ extracted = _existing_edge(+ alice, atlas, "Alice is on the Atlas project", episodes=[episode.uuid]+ )+ llm = ScriptedLLMClient(+ {"EdgeDuplicate": {"duplicate_facts": [0], "contradicted_facts": []}}+ )++ resolved, invalidated, duplicates = await resolve_extracted_edge(+ llm,+ extracted,+ related_edges=[existing],+ existing_edges=[],+ episode=episode,+ )++ assert resolved.uuid == existing.uuid+ assert list(resolved.episodes) == ["older-episode", episode.uuid]+ assert duplicates == [existing]+++async def test_novel_edge_survives_resolution_sole_sourced():+ """When the LLM finds no duplicate among candidates, the extracted+ edge passes through resolution still sole-sourced by its episode —+ eligible for stamping."""+ alice, atlas = _entity("Alice"), _entity("Atlas")+ episode = _episode('{"content": "Alice leads the Atlas launch"}')+ existing = _existing_edge(+ alice, atlas, "Alice works on Atlas", episodes=["older-episode"]+ )+ extracted = _existing_edge(+ alice, atlas, "Alice leads the Atlas launch", episodes=[episode.uuid]+ )+ llm = ScriptedLLMClient(+ {"EdgeDuplicate": {"duplicate_facts": [], "contradicted_facts": []}}+ )++ resolved, invalidated, duplicates = await resolve_extracted_edge(+ llm,+ extracted,+ related_edges=[existing],+ existing_edges=[],+ episode=episode,+ )++ assert resolved.uuid == extracted.uuid+ assert list(resolved.episodes) == [episode.uuid]+ assert list(existing.episodes) == ["older-episode"], "existing edge untouched"diff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.pyindex aee025554f..d0813d2ea0 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py@@ -1,6 +1,7 @@"""Tests for Graphiti ingestion queue and worker logic."""import asyncio+from datetime import datetime, timedelta, timezonefrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock, patch@@ -386,3 +387,175 @@ class TestWorkerIdleTimeout:# After idle timeout the worker should have cleaned up.assert user_id not in state.user_queuesassert user_id not in state.user_workers+++class TestStampEdgeMetadata:+ """#13389: dream-envelope metadata is stamped onto the edges a dream+ episode newly created, gated on the ``episodes == [episode_uuid]``+ dedup-safety invariant so user-authored edges are never clobbered."""++ # The real producer (``dream/apply._edge_metadata``) always emits all+ # five keys, so tests pass a complete payload unless exercising the+ # incomplete-payload guard explicitly.+ FULL_META = {+ "status": "active",+ "source_kind": "user_asserted",+ "scope": "real:global",+ "confidence": None,+ "provenance": None,+ }++ def _edge(self, uuid: str, episodes: list[str], expired_at=None, invalid_at=None):+ return SimpleNamespace(+ uuid=uuid,+ episodes=episodes,+ expired_at=expired_at,+ invalid_at=invalid_at,+ )++ def _result(self, episode_uuid: str, edges):+ return SimpleNamespace(episode=SimpleNamespace(uuid=episode_uuid), edges=edges)++ def _client(self):+ client = SimpleNamespace()+ client.driver = SimpleNamespace(execute_query=AsyncMock())+ return client++ @pytest.mark.asyncio+ async def test_stamps_only_sole_sourced_edges(self) -> None:+ """The core safety test: only edges whose ``episodes`` is exactly+ [this episode] get stamped. A dedup-merge (extra uuid) and an+ invalidated pre-existing edge are both skipped — they may be+ user-authored, and stamping would overwrite their provenance."""+ client = self._client()+ result = self._result(+ "ep-1",+ [+ self._edge("new", ["ep-1"]), # freshly created → stamp+ self._edge("merged", ["ep-1", "old-ep"]), # dedup merge → skip+ self._edge("invalidated", ["other-ep"]), # predates → skip+ ],+ )+ await ingest._stamp_edge_metadata(+ client, "user_abc", result, self.FULL_META, "abc"+ )+ client.driver.execute_query.assert_awaited_once()+ kwargs = client.driver.execute_query.await_args.kwargs+ assert kwargs["uuids"] == ["new"]+ assert kwargs["gid"] == "user_abc"++ @pytest.mark.asyncio+ async def test_skips_retired_but_stamps_future_invalid_at(self) -> None:+ """A brand-new edge graphiti retired in the same add_episode+ (episodes==[uuid] but expired_at, or a PAST invalid_at) must NOT be+ stamped with a live status — that would contradict its temporal+ fields. A FUTURE invalid_at is still live (true now, ends later) and+ MUST be stamped, else its dream metadata never lands."""+ past = datetime.now(timezone.utc) - timedelta(days=1)+ future = datetime.now(timezone.utc) + timedelta(days=365)+ client = self._client()+ result = self._result(+ "ep-1",+ [+ self._edge("live", ["ep-1"]), # new + temporally live → stamp+ self._edge("expired", ["ep-1"], expired_at=past), # retired → skip+ self._edge("invalid_past", ["ep-1"], invalid_at=past), # → skip+ self._edge(+ "invalid_future", ["ep-1"], invalid_at=future+ ), # still live → stamp+ ],+ )+ await ingest._stamp_edge_metadata(+ client, "user_abc", result, self.FULL_META, "abc"+ )+ kwargs = client.driver.execute_query.await_args.kwargs+ assert kwargs["uuids"] == ["live", "invalid_future"]++ @pytest.mark.asyncio+ async def test_no_new_edges_skips_query_entirely(self) -> None:+ """A dream fact that only merged into existing edges produces no+ sole-sourced target → no Cypher runs at all."""+ client = self._client()+ result = self._result("ep-1", [self._edge("merged", ["ep-1", "old"])])+ await ingest._stamp_edge_metadata(+ client, "user_abc", result, self.FULL_META, "abc"+ )+ client.driver.execute_query.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_incomplete_metadata_skips_stamp(self) -> None:+ """A partial payload missing a required field (status/source_kind/+ scope) must skip the stamp entirely rather than NULL-clobber the+ edge's required props."""+ client = self._client()+ result = self._result("ep-1", [self._edge("new", ["ep-1"])])+ await ingest._stamp_edge_metadata(+ client, "user_abc", result, {"status": "active"}, "abc"+ )+ client.driver.execute_query.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_sets_all_five_envelope_fields(self) -> None:+ client = self._client()+ result = self._result("ep-1", [self._edge("new", ["ep-1"])])+ meta = {+ "status": "tentative",+ "source_kind": "assistant_derived",+ "scope": "real:global",+ "confidence": 0.8,+ "provenance": "dream:p1:recombine:2026",+ }+ await ingest._stamp_edge_metadata(client, "user_abc", result, meta, "abc")+ kwargs = client.driver.execute_query.await_args.kwargs+ for k, v in meta.items():+ assert kwargs[k] == v++ @pytest.mark.asyncio+ async def test_stamp_failure_is_swallowed(self) -> None:+ """A stamp failure must not propagate — the edge still exists with+ graphiti defaults; ingestion must not be failed by a metadata miss."""+ client = self._client()+ client.driver.execute_query = AsyncMock(side_effect=RuntimeError("boom"))+ result = self._result("ep-1", [self._edge("new", ["ep-1"])])+ # Should not raise.+ await ingest._stamp_edge_metadata(+ client, "user_abc", result, self.FULL_META, "abc"+ )+++class TestEnqueueEpisodeEdgeMetadata:+ @pytest.mark.asyncio+ async def test_edge_metadata_rides_payload_sidecar(self) -> None:+ with (+ patch.object(ingest, "derive_group_id", return_value="user_abc"),+ patch.object(ingest, "_ensure_worker", new_callable=AsyncMock) as w,+ ):+ q: asyncio.Queue = asyncio.Queue(maxsize=100)+ w.return_value = q+ meta = {"status": "active", "provenance": "dream:p1"}+ await ingest.enqueue_episode(+ user_id="abc",+ session_id="s",+ name="dream_ep",+ episode_body="{}",+ is_json=True,+ edge_metadata=meta,+ )+ payload = q.get_nowait()+ assert payload["_edge_metadata"] == meta++ @pytest.mark.asyncio+ async def test_default_sidecar_is_none_for_non_dream_writes(self) -> None:+ """Conversation turns / memory-store calls pass no edge_metadata →+ sidecar is None → worker skips stamping → no behavior change."""+ with (+ patch.object(ingest, "derive_group_id", return_value="user_abc"),+ patch.object(ingest, "_ensure_worker", new_callable=AsyncMock) as w,+ ):+ q: asyncio.Queue = asyncio.Queue(maxsize=100)+ w.return_value = q+ await ingest.enqueue_episode(+ user_id="abc", session_id="s", name="ep", episode_body="hi"+ )+ payload = q.get_nowait()+ assert payload["_edge_metadata"] is Nonediff --git a/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_edge_props.py b/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_edge_props.pyindex 105aa8c8ba..4e5c51b0ed 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_edge_props.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_edge_props.py@@ -7,9 +7,19 @@ into ``add_episode``, the LLM extractor produced edges with onlyratification loop want to filter edges by ``status`` natively in Cypher,which requires the property to exist on every edge.-This script walks every per-user FalkorDB database and sets-``e.status = 'active'`` on every ``:RELATES_TO`` edge where ``status`` is-unset. It is idempotent — re-running is a no-op.+This script walks every per-user FalkorDB database and stamps+``status`` / ``source_kind`` / ``scope`` on every ``:RELATES_TO`` edge+where they are unset. It is idempotent — re-running is a no-op.++``source_kind`` and ``scope`` are NOT blanket defaults: the original+``MemoryEnvelope`` JSON survives in ``:Episodic.content``, so the+migration joins each edge back to its source episodes and recovers the+envelope's real values. Without this, a pre-fix edge whose envelope was+``assistant_derived`` (a dream write) would be branded+``user_asserted`` — exactly the provenance signal ratification uses to+distinguish dream facts from user facts. Defaults apply only when no+envelope evidence exists (conversation-derived edges, missing+episodes).Usage:@@ -22,20 +32,74 @@ one user (useful for canary / debugging).import argparseimport asyncio+import jsonimport loggingimport sys+from collections import defaultdictfrom backend.copilot.graphiti.client import derive_group_idfrom backend.copilot.graphiti.config import graphiti_configfrom backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver+from backend.copilot.graphiti.memory_model import MemoryEnvelope, SourceKindlogger = logging.getLogger(__name__)-BACKFILL_QUERY = """+DEFAULT_SOURCE_KIND = SourceKind.user_asserted.value+DEFAULT_SCOPE = "real:global"++# When an edge merges facts from multiple episodes (graphiti dedup), the+# most-trusted source among them wins: a fact the user actually asserted+# stays user-trusted even if a dream write later merged into the same+# edge. Conversation-derived (non-envelope) episodes count as+# ``user_asserted`` — that was the implicit assumption before envelopes+# carried explicit metadata.+_SOURCE_KIND_PRECEDENCE = (+ SourceKind.user_asserted,+ SourceKind.tool_observed,+ SourceKind.assistant_derived,+)++SELECT_NULL_EDGES_QUERY = """MATCH ()-[e:RELATES_TO]->()-WHERE e.status IS NULL-SET e.status = 'active'+WHERE e.status IS NULL OR e.source_kind IS NULL OR e.scope IS NULL+RETURN e.uuid AS uuid, e.episodes AS episodes+"""++EPISODE_CONTENT_QUERY = """+MATCH (ep:Episodic)+WHERE ep.uuid IN $uuids+RETURN ep.uuid AS uuid, ep.content AS content+"""++# Each property is ``coalesce``-guarded so the query is idempotent AND a+# partial prior run (or a real value already set by+# ``mark_edges_superseded``) is never clobbered.+#+# ``status`` is temporal-aware, gated on ``expired_at`` ONLY: graphiti+# stamps ``expired_at = now()`` exactly when an edge is actually+# invalidated, so it's the reliable "already retired" signal — such an+# edge defaults to ``'superseded'`` rather than being mislabeled+# ``'active'``. We deliberately do NOT trigger on ``invalid_at``:+# ``invalid_at`` is parsed from LLM-extracted dates and can be+# FUTURE-dated (a fact true now but with a known end date), so a+# currently-valid edge would otherwise be wrongly marked superseded.+#+# ``status`` is also deliberately NOT recovered from the envelope: a+# pre-fix dream proposal whose envelope said ``tentative`` has been+# behaving as active ever since, and resurrecting ``tentative`` now+# would feed ratification edges with zero recorded hits and >30d age —+# mass supersession on the next pass. ``confidence``/``provenance``+# legitimately default to NULL, so we leave them unset.+APPLY_DEFAULTS_QUERY = """+MATCH ()-[e:RELATES_TO]->()+WHERE e.uuid IN $uuids+SET e.source_kind = coalesce(e.source_kind, $source_kind),+ e.scope = coalesce(e.scope, $scope),+ e.status = coalesce(+ e.status,+ CASE WHEN e.expired_at IS NOT NULL THEN 'superseded' ELSE 'active' END+ )RETURN count(e) AS updated"""@@ -44,9 +108,12 @@ RETURN count(e) AS updated# constant even when the user base grows to millions of rows.USER_BATCH_SIZE = 1000+# Max uuids per ``IN $uuids`` Cypher parameter list.+_UUID_CHUNK_SIZE = 500+async def backfill_one_user(user_id: str) -> int:- """Set default status='active' on edges for one user. Returns count updated."""+ """Recover/default edge metadata for one user. Returns count updated."""try:group_id = derive_group_id(user_id)except ValueError:@@ -62,12 +129,7 @@ async def backfill_one_user(user_id: str) -> int:database=group_id,)try:- # ``execute_query`` is typed as ``tuple | None`` upstream; coerce- # to an empty tuple so pyright stops complaining about iterating- # over a None and the runtime stays single-pass.- result = await driver.execute_query(BACKFILL_QUERY)- records = result[0] if result else []- updated = records[0]["updated"] if records else 0+ updated = await backfill_graph(driver)if updated:logger.info("Backfilled %d edges for user %s", updated, user_id[:12])return updated@@ -81,6 +143,117 @@ async def backfill_one_user(user_id: str) -> int:await driver.close()+async def backfill_graph(driver: AutoGPTFalkorDriver) -> int:+ """Backfill every NULL-metadata edge in one graph. Returns count updated.++ Three steps: select the edges missing metadata, recover each edge's+ ``source_kind``/``scope`` from its source-episode envelopes, then+ apply one coalesce-guarded SET per distinct (source_kind, scope)+ bucket.+ """+ edges = await _fetch_null_edges(driver)+ if not edges:+ return 0++ episode_uuids = {uuid for _, uuids in edges for uuid in uuids}+ envelopes = await _fetch_envelopes(driver, sorted(episode_uuids))++ buckets: dict[tuple[str, str], list[str]] = defaultdict(list)+ for edge_uuid, edge_episode_uuids in edges:+ recovered = _recover_edge_metadata(edge_episode_uuids, envelopes)+ buckets[recovered].append(edge_uuid)++ updated = 0+ for (source_kind, scope), edge_uuids in buckets.items():+ for chunk in _chunked(edge_uuids):+ result = await driver.execute_query(+ APPLY_DEFAULTS_QUERY,+ uuids=chunk,+ source_kind=source_kind,+ scope=scope,+ )+ records = result[0] if result else []+ updated += records[0]["updated"] if records else 0+ return updated+++def _recover_edge_metadata(+ episode_uuids: list[str],+ envelopes: dict[str, MemoryEnvelope],+) -> tuple[str, str]:+ """Resolve (source_kind, scope) for an edge from its source episodes.++ An episode uuid absent from ``envelopes`` is a non-envelope source+ (conversation turn, or an episode node that no longer exists) and+ votes ``user_asserted`` / ``real:global``. With no episodes at all+ the historical defaults apply unchanged.+ """+ parsed = [envelopes[uuid] for uuid in episode_uuids if uuid in envelopes]+ if not parsed:+ return DEFAULT_SOURCE_KIND, DEFAULT_SCOPE++ has_non_envelope = len(parsed) < len(episode_uuids)+ kinds = {envelope.source_kind for envelope in parsed}+ if has_non_envelope:+ kinds.add(SourceKind.user_asserted)+ source_kind = next(k for k in _SOURCE_KIND_PRECEDENCE if k in kinds).value++ scopes = {envelope.scope for envelope in parsed}+ if has_non_envelope:+ scopes.add(DEFAULT_SCOPE)+ scope = next(iter(scopes)) if len(scopes) == 1 else DEFAULT_SCOPE+ return source_kind, scope+++async def _fetch_null_edges(+ driver: AutoGPTFalkorDriver,+) -> list[tuple[str, list[str]]]:+ """Return (edge_uuid, episode_uuids) for every edge missing metadata."""+ result = await driver.execute_query(SELECT_NULL_EDGES_QUERY)+ records = result[0] if result else []+ return [(r["uuid"], r["episodes"] or []) for r in records]+++async def _fetch_envelopes(+ driver: AutoGPTFalkorDriver, episode_uuids: list[str]+) -> dict[str, MemoryEnvelope]:+ """Fetch episode contents and parse the ones that are envelopes.++ Episodes whose content is not valid ``MemoryEnvelope`` JSON+ (conversation turns are plain text) are simply omitted.+ """+ envelopes: dict[str, MemoryEnvelope] = {}+ for chunk in _chunked(episode_uuids):+ result = await driver.execute_query(EPISODE_CONTENT_QUERY, uuids=chunk)+ records = result[0] if result else []+ for record in records:+ envelope = _parse_envelope(record["content"])+ if envelope is not None:+ envelopes[record["uuid"]] = envelope+ return envelopes+++def _parse_envelope(content: object) -> MemoryEnvelope | None:+ if not isinstance(content, str):+ return None+ try:+ payload = json.loads(content)+ except ValueError:+ return None+ if not isinstance(payload, dict):+ return None+ try:+ return MemoryEnvelope.model_validate(payload)+ except ValueError:+ return None+++def _chunked(items: list[str]) -> list[list[str]]:+ return [+ items[i : i + _UUID_CHUNK_SIZE] for i in range(0, len(items), _UUID_CHUNK_SIZE)+ ]++async def backfill_all_users() -> tuple[int, int]:"""Walk every User row in Postgres and backfill their graph.diff --git a/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_edge_props_test.py b/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_edge_props_test.pyindex af6cc9c8b4..1c86ae26e6 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_edge_props_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_edge_props_test.py@@ -1,8 +1,11 @@"""Tests for the backfill migration script — Cypher boundary mocked.-The migration walks every User row and runs one Cypher per group.-These tests pin its three branches: success, no-graph (FalkorDB raises-on a missing database), and invalid-group-id.+The migration selects NULL-metadata edges, recovers ``source_kind`` /+``scope`` from the surviving ``MemoryEnvelope`` JSON on the source+episodes, and applies one coalesce-guarded SET per bucket. These tests+pin the recovery rules (the #13389 review finding: a blanket+``user_asserted`` default would grant dream-derived facts user-level+trust) and the driver-flow branches: success, no-graph, invalid-group."""from __future__ import annotations@@ -11,6 +14,8 @@ from unittest.mock import AsyncMockimport pytest+from backend.copilot.graphiti.memory_model import MemoryEnvelope, SourceKind+from . import backfill_edge_props as mig@@ -26,21 +31,170 @@ def _stub_driver(mocker):return driver+def _envelope(source_kind: SourceKind, scope: str = "real:global") -> MemoryEnvelope:+ return MemoryEnvelope(content="fact", source_kind=source_kind, scope=scope)+++class TestRecoverEdgeMetadata:+ """Pure recovery rules — the heart of the mislabeling fix."""++ def test_dream_envelope_keeps_assistant_derived_and_scope(self):+ """A pre-fix dream edge must NOT be branded user_asserted."""+ envelopes = {+ "ep1": _envelope(SourceKind.assistant_derived, "project:atlas"),+ }+ assert mig._recover_edge_metadata(["ep1"], envelopes) == (+ "assistant_derived",+ "project:atlas",+ )++ def test_user_asserted_envelope_recovers_custom_scope(self):+ envelopes = {"ep1": _envelope(SourceKind.user_asserted, "book:dune")}+ assert mig._recover_edge_metadata(["ep1"], envelopes) == (+ "user_asserted",+ "book:dune",+ )++ def test_merged_user_and_dream_episodes_favor_user_asserted(self):+ """Dedup-merged edge: the user actually asserted the fact, so the+ most-trusted source wins."""+ envelopes = {+ "ep-user": _envelope(SourceKind.user_asserted),+ "ep-dream": _envelope(SourceKind.assistant_derived),+ }+ source_kind, _ = mig._recover_edge_metadata(["ep-user", "ep-dream"], envelopes)+ assert source_kind == "user_asserted"++ def test_tool_observed_outranks_assistant_derived(self):+ envelopes = {+ "ep-tool": _envelope(SourceKind.tool_observed),+ "ep-dream": _envelope(SourceKind.assistant_derived),+ }+ source_kind, _ = mig._recover_edge_metadata(["ep-tool", "ep-dream"], envelopes)+ assert source_kind == "tool_observed"++ def test_non_envelope_episode_votes_user_asserted_global(self):+ """A conversation-turn episode (no envelope) merged with a dream+ envelope makes the edge user_asserted / real:global."""+ envelopes = {+ "ep-dream": _envelope(SourceKind.assistant_derived, "project:atlas"),+ }+ assert mig._recover_edge_metadata(["ep-conv", "ep-dream"], envelopes) == (+ "user_asserted",+ "real:global",+ )++ def test_mixed_scopes_fall_back_to_global(self):+ envelopes = {+ "ep1": _envelope(SourceKind.assistant_derived, "project:atlas"),+ "ep2": _envelope(SourceKind.assistant_derived, "project:zeus"),+ }+ assert mig._recover_edge_metadata(["ep1", "ep2"], envelopes) == (+ "assistant_derived",+ "real:global",+ )++ def test_no_episodes_defaults(self):+ assert mig._recover_edge_metadata([], {}) == (+ "user_asserted",+ "real:global",+ )+++class TestParseEnvelope:+ def test_valid_envelope_json_parses(self):+ body = _envelope(SourceKind.assistant_derived, "project:x").model_dump_json()+ parsed = mig._parse_envelope(body)+ assert parsed is not None+ assert parsed.source_kind is SourceKind.assistant_derived++ @pytest.mark.parametrize(+ "content",+ [+ "User: hello\nAssistant: hi", # conversation turn, not JSON+ '["not", "a", "dict"]',+ '{"no_content_key": true}',+ '{"content": "x", "source_kind": "not-a-kind"}',+ None,+ 12,+ ],+ )+ def test_non_envelope_content_returns_none(self, content):+ assert mig._parse_envelope(content) is None+++def test_apply_query_sets_all_defaultable_fields_idempotently():+ """#13389: the SET must cover the three Cypher-filterable fields+ (status/source_kind/scope), idempotently (coalesce so re-runs and+ real values are never clobbered), with temporal-aware status so an+ already-expired edge isn't mislabeled 'active'. ``source_kind`` and+ ``scope`` are parameterized (recovered per bucket), never literal+ defaults baked into the query."""+ q = mig.APPLY_DEFAULTS_QUERY+ assert "e.source_kind = coalesce(e.source_kind, $source_kind)" in q+ assert "e.scope = coalesce(e.scope, $scope)" in q+ assert "'user_asserted'" not in q and "'real:global'" not in q+ assert "coalesce(" in q and "e.status" in q+ # status is gated on expired_at ONLY (the reliable already-retired+ # signal); invalid_at is excluded because it can be future-dated.+ assert "WHEN e.expired_at IS NOT NULL THEN 'superseded'" in q+ assert "e.invalid_at" not in q+ # confidence/provenance legitimately stay NULL — not forced.+ assert "e.confidence =" not in q+ assert "e.provenance =" not in q++@pytest.mark.asyncioasync def test_backfill_one_user_counts_updated(_stub_driver):- """Happy path — Cypher returns one row with the updated count."""- _stub_driver.execute_query = AsyncMock(return_value=([{"updated": 7}], None, None))+ """Happy path — edges with no episodes bucket to the defaults and the+ apply query's count comes back."""+ _stub_driver.execute_query = AsyncMock(+ side_effect=[+ (+ [{"uuid": "e1", "episodes": None}, {"uuid": "e2", "episodes": []}],+ None,+ None,+ ),+ ([{"updated": 2}], None, None),+ ]+ )updated = await mig.backfill_one_user("9aa20a1c-805e-4128-8bb2-c27515140264")- assert updated == 7- _stub_driver.execute_query.assert_awaited_once_with(mig.BACKFILL_QUERY)+ assert updated == 2+ apply_call = _stub_driver.execute_query.await_args_list[1]+ assert apply_call.args[0] == mig.APPLY_DEFAULTS_QUERY+ assert sorted(apply_call.kwargs["uuids"]) == ["e1", "e2"]+ assert apply_call.kwargs["source_kind"] == "user_asserted"+ assert apply_call.kwargs["scope"] == "real:global"_stub_driver.close.assert_awaited_once()+async def test_backfill_one_user_recovers_envelope_bucket(_stub_driver):+ """An edge sourced from a dream envelope is stamped with the+ envelope's own source_kind/scope, not the defaults."""+ body = _envelope(SourceKind.assistant_derived, "project:atlas").model_dump_json()+ _stub_driver.execute_query = AsyncMock(+ side_effect=[+ ([{"uuid": "e1", "episodes": ["ep1"]}], None, None),+ ([{"uuid": "ep1", "content": body}], None, None),+ ([{"updated": 1}], None, None),+ ]+ )+ updated = await mig.backfill_one_user("9aa20a1c-805e-4128-8bb2-c27515140264")+ assert updated == 1+ episode_call = _stub_driver.execute_query.await_args_list[1]+ assert episode_call.args[0] == mig.EPISODE_CONTENT_QUERY+ apply_call = _stub_driver.execute_query.await_args_list[2]+ assert apply_call.kwargs["source_kind"] == "assistant_derived"+ assert apply_call.kwargs["scope"] == "project:atlas"++@pytest.mark.asyncioasync def test_backfill_one_user_no_records_returns_zero(_stub_driver):"""Empty result set (no edges needed backfilling)."""_stub_driver.execute_query = AsyncMock(return_value=([], None, None))assert await mig.backfill_one_user("9aa20a1c-805e-4128-8bb2-c27515140264") == 0+ _stub_driver.execute_query.assert_awaited_once()@pytest.mark.asynciodiff --git a/autogpt_platform/backend/backend/copilot/graphiti/types.py b/autogpt_platform/backend/backend/copilot/graphiti/types.pyindex 32164512f4..520a9fad3c 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/types.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/types.py@@ -120,3 +120,19 @@ EDGE_TYPES: dict[str, type[BaseModel]] = {"MemoryFact": MemoryFact}EDGE_TYPE_MAP: dict[tuple[str, str], list[str]] = {(src, tgt): ["MemoryFact"] for src in ENTITY_TYPES for tgt in ENTITY_TYPES}++# The ('Entity', 'Entity') wildcard is load-bearing, not redundant.+# graphiti-core resolves an edge's allowed types from the cross-product of+# (source.labels + ['Entity']) x (target.labels + ['Entity']) — see the+# ``label_tuples`` build then ``edge_type_map.get(label_tuple, [])`` in+# graphiti_core/utils/maintenance/edge_operations.py. Because 'Entity' is+# ALWAYS appended to both sides, the ('Entity','Entity') key matches 100%+# of edges, including every edge touching a node that carries only the+# base ``Entity`` label (no custom entity type). Without this key those+# edges resolve to NO custom type, so MemoryFact's attributes+# (status/confidence/scope/source_kind/provenance) are never attached and+# the edge persists with only the stock graphiti schema — the #13389 bug.+# graphiti-core's own default is ``{('Entity','Entity'): list(edge_types)}``;+# our custom map above accidentally overrode that default without re-adding+# the wildcard.+EDGE_TYPE_MAP[("Entity", "Entity")] = ["MemoryFact"]diff --git a/autogpt_platform/backend/backend/copilot/graphiti/types_test.py b/autogpt_platform/backend/backend/copilot/graphiti/types_test.pyindex 83b3860cc1..cd113bba45 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/types_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/types_test.py@@ -92,14 +92,25 @@ def test_edge_types_include_memoryfact():def test_edge_type_map_spans_full_cartesian_product():"""Every (entity, entity) pair maps to MemoryFact — the audit's- 'one edge type, narrow entity vocabulary' shape."""- # Six entity types × six entity types = 36 pairs.- assert len(EDGE_TYPE_MAP) == len(ENTITY_TYPES) ** 2+ 'one edge type, narrow entity vocabulary' shape. Plus the+ ('Entity','Entity') wildcard, so the cartesian pairs + 1 catch-all."""+ # Six entity types × six entity types = 36 pairs, + the wildcard = 37.+ assert len(EDGE_TYPE_MAP) == len(ENTITY_TYPES) ** 2 + 1for src in ENTITY_TYPES:for tgt in ENTITY_TYPES:assert EDGE_TYPE_MAP.get((src, tgt)) == ["MemoryFact"]+def test_edge_type_map_has_entity_wildcard_catchall():+ """#13389 regression: graphiti-core resolves edge types against the+ base ``Entity`` label (it appends 'Entity' to both endpoints'+ label sets, so ('Entity','Entity') is in every edge's label_tuples+ — see graphiti_core/utils/maintenance/edge_operations.py). Without+ this wildcard key, edges touching an Entity-only node never resolve+ to MemoryFact and persist with NONE of the custom attributes."""+ assert EDGE_TYPE_MAP[("Entity", "Entity")] == ["MemoryFact"]++def test_person_entity_has_optional_role_and_email():p = Person()assert p.role is None