Skip to content

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): facts an expert stores now persist as recallable memories (#14412)

Reinier van der Leerf58b08206324
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest.py
index 4d3091d1cf..b10f33cab6 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/ingest.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/ingest.py
@@ -127,12 +127,15 @@ class IngestionCompletion:
# clamp (``dream/fetch.py:MAX_SESSION_BODY_BYTES``).
MAX_EPISODE_BODY_BYTES = 64 * 1024
+# Graphiti keeps a fact only as an edge between two entities: the user and the
+# products they name must both be extractable, or a stored memory yields nothing.
CUSTOM_EXTRACTION_INSTRUCTIONS = """
-- Do not extract "User", "Assistant", "AI", "System", "CoPilot", or "human" as entity nodes.
-- Do not extract software tool names, block names, API endpoint names, or internal system identifiers as entities.
+- Do not extract "Assistant", "AI", "System", or "CoPilot" as entity nodes.
+- Do not extract AutoGPT block names, tool or function names, API endpoint names, or internal system identifiers as entities. Third-party products and services the user's business uses (a CRM, a shop platform, a payment provider) ARE entities.
- Do not extract action descriptions like "the assistant created..." as facts. Extract only the underlying user intent or real-world information.
- Focus on real-world entities: people, companies, products, projects, concepts, and preferences.
- Use canonical names: if the speaker says "my company" and context reveals it is "Acme Corp", use "Acme Corp".
+- In a JSON memory, the "user" field names the person the memory is about: always extract that value as an entity and relate the memory's content to it.
"""
@@ -387,7 +390,7 @@ async def enqueue_conversation_turn(
logger.warning("Invalid memory scope for ingestion: %s", user_id[:12])
return
- user_display_name = await _resolve_user_name(user_id)
+ user_display_name = await resolve_user_name(user_id)
episode_name = f"conversation_{session_id}"
@@ -425,6 +428,7 @@ async def enqueue_conversation_turn(
finding = _distill_finding(assistant_msg)
if finding:
envelope = MemoryEnvelope(
+ user=user_display_name,
content=finding,
source_kind=SourceKind.assistant_derived,
memory_kind=MemoryKind.finding,
@@ -607,7 +611,7 @@ async def _enqueue_payload(user_id: str, group_id: str, payload: dict) -> bool:
return True
-async def _resolve_user_name(user_id: str) -> str:
+async def resolve_user_name(user_id: str) -> str:
"""Get the user's display name from BusinessUnderstanding, or fall back to 'User'."""
try:
from backend.data.db_accessors import understanding_db
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py
index 7168ba140d..562e4b3895 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
+import json
import logging
from datetime import datetime, timedelta, timezone
from types import SimpleNamespace
@@ -338,7 +339,7 @@ class TestEnqueueConversationTurn:
),
patch.object(
ingest,
- "_resolve_user_name",
+ "resolve_user_name",
new_callable=AsyncMock,
return_value="Alice",
),
@@ -419,7 +420,7 @@ class TestQueueFullScenario:
return_value="user_abc-valid-id",
),
patch(
- "backend.copilot.graphiti.ingest._resolve_user_name",
+ "backend.copilot.graphiti.ingest.resolve_user_name",
new_callable=AsyncMock,
return_value="Alice",
),
@@ -455,7 +456,7 @@ class TestResolveUserName:
"backend.data.db_accessors.understanding_db",
mock_db,
):
- name = await ingest._resolve_user_name("some-user-id")
+ name = await ingest.resolve_user_name("some-user-id")
assert name == "User"
@@ -471,7 +472,7 @@ class TestResolveUserName:
"backend.data.db_accessors.understanding_db",
mock_db,
):
- name = await ingest._resolve_user_name("some-user-id")
+ name = await ingest.resolve_user_name("some-user-id")
assert name == "Alice"
@@ -484,7 +485,7 @@ class TestResolveUserName:
"backend.data.db_accessors.understanding_db",
mock_db,
):
- name = await ingest._resolve_user_name("some-user-id")
+ name = await ingest.resolve_user_name("some-user-id")
assert name == "User"
@@ -623,7 +624,7 @@ class TestDerivedFindingLane:
patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),
patch.object(ingest, "_enqueue_payload", new=enqueue_mock),
patch(
- "backend.copilot.graphiti.ingest._resolve_user_name",
+ "backend.copilot.graphiti.ingest.resolve_user_name",
new_callable=AsyncMock,
return_value="Alice",
),
@@ -637,6 +638,13 @@ class TestDerivedFindingLane:
# Should have 2 items: user episode + derived finding
assert q.qsize() == 2
+ q.get_nowait()
+ finding_payload = q.get_nowait()
+ # CUSTOM_EXTRACTION_INSTRUCTIONS tells the extractor to always make an
+ # entity of the envelope's "user"; a null one leaves the finding with
+ # nothing to attach to.
+ assert json.loads(finding_payload["episode_body"])["user"] == "Alice"
+
@pytest.mark.asyncio
async def test_short_assistant_msg_skips_finding(self) -> None:
q: asyncio.Queue = asyncio.Queue(maxsize=100)
@@ -645,7 +653,7 @@ class TestDerivedFindingLane:
patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),
patch.object(ingest, "_enqueue_payload", new=enqueue_mock),
patch(
- "backend.copilot.graphiti.ingest._resolve_user_name",
+ "backend.copilot.graphiti.ingest.resolve_user_name",
new_callable=AsyncMock,
return_value="Alice",
),
@@ -1019,3 +1027,14 @@ class TestEnqueueEpisodeEdgeMetadata:
)
payload = q.get_nowait()
assert payload["_completion"] is completion
+
+
+class TestExtractionInstructions:
+ """A stored fact becomes an edge only if the extractor may create both the
+ user and the product as entities; 0/25 did under the old wording."""
+
+ def test_user_and_products_stay_extractable(self) -> None:
+ text = ingest.CUSTOM_EXTRACTION_INSTRUCTIONS
+ assert '"User"' not in text
+ assert "software tool names" not in text
+ assert '"user" field' in text
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/memory_model.py b/autogpt_platform/backend/backend/copilot/graphiti/memory_model.py
index d8105cb731..30eeb9a5ae 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/memory_model.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/memory_model.py
@@ -93,6 +93,10 @@ class MemoryEnvelope(BaseModel):
``ProcedureMemory`` for structured steps.
"""
+ user: str | None = Field(
+ default=None,
+ description="Display name of the person the memory is about",
+ )
content: str = Field(
description="The memory content — the actual fact, rule, or finding"
)
diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_store.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_store.py
index e8597c8f69..bb43085102 100644
--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_store.py
+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_store.py
@@ -4,7 +4,11 @@ import logging
from typing import Any
from backend.copilot.graphiti.config import is_enabled_for_user
-from backend.copilot.graphiti.ingest import MAX_EPISODE_BODY_BYTES, enqueue_episode
+from backend.copilot.graphiti.ingest import (
+ MAX_EPISODE_BODY_BYTES,
+ enqueue_episode,
+ resolve_user_name,
+)
from backend.copilot.graphiti.memory_model import (
MemoryEnvelope,
MemoryKind,
@@ -228,6 +232,7 @@ class MemoryStoreTool(BaseTool):
provenance = f"session:{session.session_id}"
envelope = MemoryEnvelope(
+ user=await resolve_user_name(user_id),
content=content,
source_kind=resolved_source,
scope=scope,
diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_store_test.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_store_test.py
index b260758911..6b32303eb5 100644
--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_store_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_store_test.py
@@ -28,9 +28,47 @@ def _make_session(
)
[email protected](autouse=True)
+def _display_name():
+ with patch(
+ "backend.copilot.tools.graphiti_store.resolve_user_name",
+ new_callable=AsyncMock,
+ return_value="Alice",
+ ):
+ yield
+
+
class TestMemoryStoreTool:
"""Tests for MemoryStoreTool._execute."""
+ @pytest.mark.asyncio
+ async def test_store_names_the_user_as_subject(self):
+ """A JSON episode has no speaker, so the envelope must name the user
+ for the extractor to have a second entity to relate the fact to."""
+ tool = MemoryStoreTool()
+ mock_enqueue = AsyncMock(return_value=True)
+
+ with (
+ patch(
+ "backend.copilot.tools.graphiti_store.is_enabled_for_user",
+ new_callable=AsyncMock,
+ return_value=True,
+ ),
+ patch(
+ "backend.copilot.tools.graphiti_store.enqueue_episode",
+ mock_enqueue,
+ ),
+ ):
+ await tool._execute(
+ user_id="user-1",
+ session=_make_session(),
+ name="crm_is_hubspot",
+ content="CRM used is HubSpot.",
+ )
+
+ envelope = json.loads(mock_enqueue.await_args.kwargs["episode_body"])
+ assert envelope["user"] == "Alice"
+
@pytest.mark.asyncio
async def test_store_no_user_returns_error(self):
tool = MemoryStoreTool()