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.

refactor(backend/copilot): introduce MemoryScope and thread it through the memory code (#14955)

Nicholas Tindle785e439f204a
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.py
index ae077bcb76..739a5e2148 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
@@ -34,9 +34,8 @@ from backend.copilot.dream.job_status import (
from backend.copilot.dream.nightly_batch import NightlyBatchResult
from backend.copilot.dream.ratification import RatificationResult
from backend.copilot.dream.schemas import DreamPassResult
-from backend.copilot.graphiti.client import derive_memory_group_id
-from backend.copilot.graphiti.config import graphiti_config
-from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver
+from backend.copilot.graphiti.falkordb_driver import open_driver
+from backend.copilot.graphiti.scope import MemoryScope
from backend.util.clients import get_scheduler_client
from backend.util.url_redaction import redact_url
@@ -188,14 +187,12 @@ def _resolve_user_id(user_id: str, caller_id: str) -> str:
return caller_id if user_id == "me" else user_id
-async def _resolve_memory_scope(
- user_id: str, expert_id: str | None
-) -> tuple[str, str | None]:
- """Derive an owned memory group without trusting client group identifiers."""
+async def _resolve_memory_scope(user_id: str, expert_id: str | None) -> MemoryScope:
+ """Derive an owned memory scope without trusting client group identifiers."""
if expert_id is None:
try:
- return derive_memory_group_id(user_id), None
+ return MemoryScope.for_user(user_id)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
@@ -204,7 +201,7 @@ async def _resolve_memory_scope(
raise HTTPException(status_code=404, detail="Expert not found")
try:
- return derive_memory_group_id(user_id, expert.id), expert.id
+ return MemoryScope.for_expert(user_id, expert.id)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
@@ -249,11 +246,14 @@ async def _resolve_and_audit_memory_scope(
caller_id: str,
target_id: str,
jwt_payload: dict,
- expert_id: str | None = None,
-) -> tuple[str, str | None]:
- """Resolve an authorized scope while auditing successful and failed attempts."""
+ expert_id: str | None,
+) -> MemoryScope:
+ """Resolve an authorized scope while auditing successful and failed attempts.
+
+ ``expert_id`` is required so an account-only caller says so explicitly
+ instead of landing in the account graph by omission."""
try:
- group_id, resolved_expert_id = await _resolve_memory_scope(target_id, expert_id)
+ memory_scope = await _resolve_memory_scope(target_id, expert_id)
except Exception:
_audit_cross_user_access(
request=request,
@@ -269,10 +269,10 @@ async def _resolve_and_audit_memory_scope(
caller_id=caller_id,
target_id=target_id,
jwt_payload=jwt_payload,
- expert_id=resolved_expert_id,
- group_id=group_id,
+ expert_id=memory_scope.expert_id,
+ group_id=memory_scope.group_id,
)
- return group_id, resolved_expert_id
+ return memory_scope
async def _mark_schedule_failed(kind: JobKind, job_id: str, exc: Exception) -> None:
@@ -293,29 +293,6 @@ async def _mark_schedule_failed(kind: JobKind, job_id: str, exc: Exception) -> N
)
-def _open_driver(group_id: str) -> AutoGPTFalkorDriver:
- """Read-only driver — bypasses the full Graphiti client construction.
-
- The visualizer's read paths only need Cypher; we avoid the
- ~1s LLM-client + cross-encoder setup cost for what should be
- snappy dashboard calls.
-
- ``build_indices=False`` suppresses graphiti-core's per-init
- fire-and-forget indexing task. For a user whose graph the admin
- is inspecting, the indices are already in place from the
- long-lived chat-write client; firing the index-creation task per
- short-lived admin request creates a race with the route's own
- queries and produces "Buffer is closed" log spam.
- """
- return AutoGPTFalkorDriver(
- host=graphiti_config.falkordb_host,
- port=graphiti_config.falkordb_port,
- password=graphiti_config.falkordb_password or None,
- database=group_id,
- build_indices=False,
- )
-
-
_MISSING_GRAPH_MARKERS = ("no such graph", "does not exist", "invalid graph")
@@ -371,15 +348,16 @@ async def _get_memory_overview_impl(
expert_id: str | None,
) -> MemoryOverview:
target = _resolve_user_id(user_id, caller_id)
- group_id, resolved_expert_id = await _resolve_and_audit_memory_scope(
+ memory_scope = await _resolve_and_audit_memory_scope(
request=request,
caller_id=caller_id,
target_id=target,
jwt_payload=jwt_payload,
expert_id=expert_id,
)
+ group_id = memory_scope.group_id
- driver = _open_driver(group_id)
+ driver = open_driver(memory_scope)
try:
entities = await _count(driver, "MATCH (n:Entity) RETURN count(n) AS c")
episodes = await _count(driver, "MATCH (n:Episodic) RETURN count(n) AS c")
@@ -395,7 +373,7 @@ async def _get_memory_overview_impl(
return MemoryOverview(
user_id=target,
- expert_id=resolved_expert_id,
+ expert_id=memory_scope.expert_id,
group_id=group_id,
entities=entities,
episodes=episodes,
@@ -453,15 +431,16 @@ async def _list_entities_impl(
limit: int,
) -> EntityListResponse:
target = _resolve_user_id(user_id, caller_id)
- group_id, _ = await _resolve_and_audit_memory_scope(
+ memory_scope = await _resolve_and_audit_memory_scope(
request=request,
caller_id=caller_id,
target_id=target,
jwt_payload=jwt_payload,
expert_id=expert_id,
)
+ group_id = memory_scope.group_id
- driver = _open_driver(group_id)
+ driver = open_driver(memory_scope)
try:
result = await driver.execute_query(
"""
@@ -546,13 +525,14 @@ async def _list_facts_impl(
scope: str | None,
) -> FactListResponse:
target = _resolve_user_id(user_id, caller_id)
- group_id, _ = await _resolve_and_audit_memory_scope(
+ memory_scope = await _resolve_and_audit_memory_scope(
request=request,
caller_id=caller_id,
target_id=target,
jwt_payload=jwt_payload,
expert_id=expert_id,
)
+ group_id = memory_scope.group_id
# Build optional filters
where_clauses = ["e.group_id = $g"]
@@ -565,7 +545,7 @@ async def _list_facts_impl(
params["scope"] = scope
where = " AND ".join(where_clauses)
- driver = _open_driver(group_id)
+ driver = open_driver(memory_scope)
try:
result = await driver.execute_query(
f"""
@@ -677,15 +657,16 @@ async def _list_communities_impl(
limit: int,
) -> CommunityListResponse:
target = _resolve_user_id(user_id, caller_id)
- group_id, _ = await _resolve_and_audit_memory_scope(
+ memory_scope = await _resolve_and_audit_memory_scope(
request=request,
caller_id=caller_id,
target_id=target,
jwt_payload=jwt_payload,
expert_id=expert_id,
)
+ group_id = memory_scope.group_id
- driver = _open_driver(group_id)
+ driver = open_driver(memory_scope)
try:
# Graphiti stores membership as (Community)-[HAS_MEMBER]->(Entity); match
# in that direction so member_count and the size sort are correct.
@@ -779,13 +760,14 @@ async def _get_graph_impl(
include_communities: bool,
) -> GraphResponse:
target = _resolve_user_id(user_id, caller_id)
- group_id, resolved_expert_id = await _resolve_and_audit_memory_scope(
+ memory_scope = await _resolve_and_audit_memory_scope(
request=request,
caller_id=caller_id,
target_id=target,
jwt_payload=jwt_payload,
expert_id=expert_id,
)
+ group_id = memory_scope.group_id
# Build the labels-of-interest list for the node queries — one
# Cypher per label so the label can travel through the result row
@@ -796,7 +778,7 @@ async def _get_graph_impl(
if include_communities:
labels.append("Community")
- driver = _open_driver(group_id)
+ driver = open_driver(memory_scope)
nodes: list[GraphNode] = []
edges: list[GraphEdge] = []
truncated = False
@@ -909,7 +891,7 @@ async def _get_graph_impl(
return GraphResponse(
user_id=target,
- expert_id=resolved_expert_id,
+ expert_id=memory_scope.expert_id,
group_id=group_id,
nodes=nodes,
edges=edges,
@@ -1040,6 +1022,7 @@ async def trigger_dream_pass(
caller_id=caller_id,
target_id=target,
jwt_payload=jwt_payload,
+ expert_id=None,
)
job_id = str(_uuid.uuid4())
@@ -1121,6 +1104,7 @@ async def trigger_ratification_pass(
caller_id=caller_id,
target_id=target,
jwt_payload=jwt_payload,
+ expert_id=None,
)
try:
@@ -1164,6 +1148,7 @@ async def trigger_nightly_batch(
caller_id=caller_id,
target_id=target,
jwt_payload=jwt_payload,
+ expert_id=None,
)
job_id = str(_uuid.uuid4())
@@ -1258,6 +1243,7 @@ async def rebuild_communities(
caller_id=caller_id,
target_id=target,
jwt_payload=jwt_payload,
+ expert_id=None,
)
job_id = str(_uuid.uuid4())
diff --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.py
index 02be290134..d3cbd2912a 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
@@ -39,6 +39,12 @@ def _driver_returning(*query_results) -> AsyncMock:
return driver
+def _opened_group(open_driver: MagicMock) -> str:
+ """Group of the one scope the route opened a driver on."""
+ open_driver.assert_called_once()
+ return open_driver.call_args.args[0].group_id
+
+
def _expert(
expert_id: str,
) -> Expert:
@@ -72,7 +78,7 @@ class TestOverview:
[{"c": 153}], # mentions
[{"c": 30}], # communities
)
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.get("/admin/memory/abc/overview")
assert resp.status_code == 200
body = resp.json()
@@ -90,7 +96,7 @@ class TestOverview:
driver = _driver_returning(
[{"c": 0}], [{"c": 0}], [{"c": 0}], [{"c": 0}], [{"c": 0}]
)
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.get("/admin/memory/me/overview")
assert resp.status_code == 200
body = resp.json()
@@ -108,7 +114,7 @@ class TestOverview:
"Invalid graph operation on empty key"
)
driver.close = AsyncMock()
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.get("/admin/memory/abc/overview")
assert resp.status_code == 200
body = resp.json()
@@ -125,7 +131,7 @@ class TestOverview:
# client convert it to a 500 — we want to assert the exact type
# propagated past _count.
bare_client = fastapi.testclient.TestClient(app, raise_server_exceptions=True)
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
with pytest.raises(ResponseError, match="MATHC"):
bare_client.get("/admin/memory/abc/overview")
# Driver must still be closed even when the query blows up.
@@ -138,7 +144,7 @@ class TestOverview:
driver.execute_query.side_effect = TypeError("bad query param")
driver.close = AsyncMock()
bare_client = fastapi.testclient.TestClient(app, raise_server_exceptions=True)
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
with pytest.raises(TypeError, match="bad query param"):
bare_client.get("/admin/memory/abc/overview")
driver.close.assert_awaited_once()
@@ -148,10 +154,10 @@ class TestExpertMemoryScope:
def test_invalid_autopilot_scope_is_400_before_driver(self) -> None:
with (
patch(
- f"{_MOCK_MODULE}.derive_memory_group_id",
+ f"{_MOCK_MODULE}.MemoryScope.for_user",
side_effect=ValueError("invalid memory owner"),
),
- patch(f"{_MOCK_MODULE}._open_driver") as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver") as open_driver,
):
resp = client.get("/admin/memory/abc/overview")
@@ -167,10 +173,10 @@ class TestExpertMemoryScope:
new=AsyncMock(return_value=_expert(expert_id)),
),
patch(
- f"{_MOCK_MODULE}.derive_memory_group_id",
+ f"{_MOCK_MODULE}.MemoryScope.for_expert",
side_effect=ValueError("invalid expert memory scope"),
),
- patch(f"{_MOCK_MODULE}._open_driver") as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver") as open_driver,
):
resp = client.get(f"/admin/memory/abc/experts/{expert_id}/graph")
@@ -189,7 +195,7 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(return_value=_expert(expert_id)),
) as get_expert,
- patch(f"{_MOCK_MODULE}._open_driver", return_value=driver) as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver", return_value=driver) as open_driver,
):
resp = client.get(f"/admin/memory/abc/experts/{expert_id}/overview")
@@ -197,7 +203,7 @@ class TestExpertMemoryScope:
assert resp.json()["expert_id"] == expert_id
assert resp.json()["group_id"] == expected_group
get_expert.assert_awaited_once_with("abc", expert_id, include_workflows=False)
- open_driver.assert_called_once_with(expected_group)
+ assert _opened_group(open_driver) == expected_group
def test_owned_expert_graph_uses_expert_group(self) -> None:
expert_id = "expert-owned"
@@ -208,14 +214,14 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(return_value=_expert(expert_id)),
),
- patch(f"{_MOCK_MODULE}._open_driver", return_value=driver) as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver", return_value=driver) as open_driver,
):
resp = client.get(f"/admin/memory/abc/experts/{expert_id}/graph")
assert resp.status_code == 200
assert resp.json()["expert_id"] == expert_id
assert resp.json()["group_id"] == expected_group
- open_driver.assert_called_once_with(expected_group)
+ assert _opened_group(open_driver) == expected_group
@pytest.mark.parametrize(
"endpoint", ["overview", "graph", "entities", "facts", "communities"]
@@ -227,7 +233,7 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(return_value=None),
) as get_expert,
- patch(f"{_MOCK_MODULE}._open_driver") as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver") as open_driver,
):
resp = client.get(f"/admin/memory/abc/experts/{expert_id}/{endpoint}")
@@ -246,13 +252,13 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(return_value=_expert(expert_id)),
) as get_expert,
- patch(f"{_MOCK_MODULE}._open_driver", return_value=driver) as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver", return_value=driver) as open_driver,
):
resp = client.get(f"/admin/memory/abc/experts/{expert_id}/{endpoint}")
assert resp.status_code == 200
get_expert.assert_awaited_once_with("abc", expert_id, include_workflows=False)
- open_driver.assert_called_once_with(expected_group)
+ assert _opened_group(open_driver) == expected_group
@pytest.mark.parametrize("endpoint", ["entities", "facts", "communities"])
def test_list_without_expert_id_remains_autopilot(self, endpoint: str) -> None:
@@ -262,13 +268,13 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(),
) as get_expert,
- patch(f"{_MOCK_MODULE}._open_driver", return_value=driver) as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver", return_value=driver) as open_driver,
):
resp = client.get(f"/admin/memory/abc/{endpoint}")
assert resp.status_code == 200
get_expert.assert_not_awaited()
- open_driver.assert_called_once_with("user_abc")
+ assert _opened_group(open_driver) == "user_abc"
@pytest.mark.parametrize(
"endpoint", ["overview", "graph", "entities", "facts", "communities"]
@@ -282,7 +288,7 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(),
) as get_expert,
- patch(f"{_MOCK_MODULE}._open_driver") as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver") as open_driver,
):
resp = client.get(f"/admin/memory/abc/experts/{expert_id}/{endpoint}")
@@ -307,7 +313,7 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(),
) as get_expert,
- patch(f"{_MOCK_MODULE}._open_driver", return_value=driver) as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver", return_value=driver) as open_driver,
):
resp = client.get(
f"/admin/memory/abc/{endpoint}", params={"expert_id": "expert-owned"}
@@ -317,7 +323,7 @@ class TestExpertMemoryScope:
assert resp.json()["expert_id"] is None
assert resp.json()["group_id"] == "user_abc"
get_expert.assert_not_awaited()
- open_driver.assert_called_once_with("user_abc")
+ assert _opened_group(open_driver) == "user_abc"
@pytest.mark.parametrize("endpoint", ["overview", "graph"])
def test_raw_group_id_query_cannot_select_memory_scope(self, endpoint: str) -> None:
@@ -333,7 +339,7 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(),
) as get_expert,
- patch(f"{_MOCK_MODULE}._open_driver", return_value=driver) as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver", return_value=driver) as open_driver,
):
resp = client.get(
f"/admin/memory/abc/{endpoint}",
@@ -344,7 +350,7 @@ class TestExpertMemoryScope:
assert resp.json()["expert_id"] is None
assert resp.json()["group_id"] == "user_abc"
get_expert.assert_not_awaited()
- open_driver.assert_called_once_with("user_abc")
+ assert _opened_group(open_driver) == "user_abc"
@pytest.mark.parametrize("endpoint", ["overview", "graph"])
def test_max_length_expert_id_reaches_owned_scope_lookup(
@@ -363,7 +369,7 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(return_value=_expert(expert_id)),
) as get_expert,
- patch(f"{_MOCK_MODULE}._open_driver", return_value=driver),
+ patch(f"{_MOCK_MODULE}.open_driver", return_value=driver),
):
resp = client.get(f"/admin/memory/abc/experts/{expert_id}/{endpoint}")
@@ -380,7 +386,7 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(return_value=None),
),
- patch(f"{_MOCK_MODULE}._open_driver") as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver") as open_driver,
patch(f"{_MOCK_MODULE}.logger.info") as audit_log,
):
resp = client.get(f"/admin/memory/abc/experts/{expert_id}/{endpoint}")
@@ -402,7 +408,7 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(return_value=None),
),
- patch(f"{_MOCK_MODULE}._open_driver") as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver") as open_driver,
patch(f"{_MOCK_MODULE}.logger.info") as audit_log,
):
resp = client.get(
@@ -418,10 +424,10 @@ class TestExpertMemoryScope:
def test_failed_cross_user_autopilot_scope_is_audited(self, mock_jwt_admin) -> None:
with (
patch(
- f"{_MOCK_MODULE}.derive_memory_group_id",
+ f"{_MOCK_MODULE}.MemoryScope.for_user",
side_effect=ValueError("invalid memory owner"),
),
- patch(f"{_MOCK_MODULE}._open_driver") as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver") as open_driver,
patch(f"{_MOCK_MODULE}.logger.info") as audit_log,
):
resp = client.get("/admin/memory/abc/overview")
@@ -446,7 +452,7 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(),
) as get_expert,
- patch(f"{_MOCK_MODULE}._open_driver") as open_driver,
+ patch(f"{_MOCK_MODULE}.open_driver") as open_driver,
):
resp = client.get(f"/admin/memory/abc/experts/expert-owned/{endpoint}")
@@ -465,7 +471,7 @@ class TestExpertMemoryScope:
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(return_value=_expert(expert_id)),
),
- patch(f"{_MOCK_MODULE}._open_driver", return_value=driver),
+ patch(f"{_MOCK_MODULE}.open_driver", return_value=driver),
patch(f"{_MOCK_MODULE}.logger.info") as audit_log,
):
resp = client.get(f"/admin/memory/abc/experts/{expert_id}/graph")
@@ -484,7 +490,7 @@ class TestExpertMemoryScope:
[{"c": 0}], [{"c": 0}], [{"c": 0}], [{"c": 0}], [{"c": 0}]
)
with (
- patch(f"{_MOCK_MODULE}._open_driver", return_value=driver),
+ patch(f"{_MOCK_MODULE}.open_driver", return_value=driver),
patch(f"{_MOCK_MODULE}.logger.info") as audit_log,
):
resp = client.get("/admin/memory/abc/overview")
@@ -504,7 +510,7 @@ class TestListEntities:
{"uuid": "e2", "name": "Atlas", "summary": None},
]
)
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.get("/admin/memory/abc/entities?limit=10")
assert resp.status_code == 200
items = resp.json()["items"]
@@ -515,7 +521,7 @@ class TestListEntities:
def test_cross_user_audit_includes_autopilot_group(self, mock_jwt_admin) -> None:
driver = _driver_returning([])
with (
- patch(f"{_MOCK_MODULE}._open_driver", return_value=driver),
+ patch(f"{_MOCK_MODULE}.open_driver", return_value=driver),
patch(f"{_MOCK_MODULE}.logger.info") as audit_log,
):
resp = client.get("/admin/memory/abc/entities")
@@ -553,7 +559,7 @@ class TestListFacts:
}
]
)
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.get("/admin/memory/abc/facts")
assert resp.status_code == 200
# No status filter applied → query shouldn't include WHERE on status
@@ -563,7 +569,7 @@ class TestListFacts:
def test_status_filter_passed_to_cypher(self) -> None:
driver = _driver_returning([])
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.get(
"/admin/memory/abc/facts?status=superseded&scope=project:atlas"
)
@@ -595,7 +601,7 @@ class TestListCommunities:
},
]
)
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.get("/admin/memory/abc/communities?limit=10")
assert resp.status_code == 200
items = resp.json()["items"]
@@ -609,7 +615,7 @@ class TestGraph:
driver = AsyncMock()
driver.execute_query.side_effect = ResponseError("no such graph")
driver.close = AsyncMock()
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.get("/admin/memory/abc/graph")
assert resp.status_code == 200
body = resp.json()
@@ -623,7 +629,7 @@ class TestGraph:
driver.execute_query.side_effect = ResponseError("syntax error near 'MATHC'")
driver.close = AsyncMock()
bare_client = fastapi.testclient.TestClient(app, raise_server_exceptions=True)
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
with pytest.raises(ResponseError, match="MATHC"):
bare_client.get("/admin/memory/abc/graph")
driver.close.assert_awaited_once()
diff --git a/autogpt_platform/backend/backend/api/features/memory/routes.py b/autogpt_platform/backend/backend/api/features/memory/routes.py
index a294ac9ac4..f6eca31b75 100644
--- a/autogpt_platform/backend/backend/api/features/memory/routes.py
+++ b/autogpt_platform/backend/backend/api/features/memory/routes.py
@@ -28,9 +28,9 @@ from backend.api.features.memory.models import (
MemoryFactListResponse,
MemoryScopeOverview,
)
-from backend.copilot.graphiti.client import derive_memory_group_id
-from backend.copilot.graphiti.config import graphiti_config, is_enabled_for_user
-from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver
+from backend.copilot.graphiti.config import is_enabled_for_user
+from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver, open_driver
+from backend.copilot.graphiti.scope import MemoryScope
logger = logging.getLogger(__name__)
@@ -61,20 +61,8 @@ def _is_missing_graph_error(exc: BaseException) -> bool:
return any(marker in msg for marker in _MISSING_GRAPH_MARKERS)
-def _open_driver(group_id: str) -> AutoGPTFalkorDriver:
- """Cypher-only driver — skips LLM-client construction and the per-init
- index build (indices exist from the long-lived chat-write client)."""
- return AutoGPTFalkorDriver(
- host=graphiti_config.falkordb_host,
- port=graphiti_config.falkordb_port,
- password=graphiti_config.falkordb_password or None,
- database=group_id,
- build_indices=False,
- )
-
-
-async def _resolve_scope(user_id: str, expert_id: str | None) -> tuple[str, str | None]:
- """Resolve the caller-owned memory group for the requested scope.
+async def _resolve_scope(user_id: str, expert_id: str | None) -> MemoryScope:
+ """Resolve the caller-owned memory scope for the request.
Memory must be enabled for the caller, and an expert scope must name an
active expert the caller owns — archived experts 404 here, matching the
@@ -87,7 +75,7 @@ async def _resolve_scope(user_id: str, expert_id: str | None) -> tuple[str, str
if expert_id is None:
try:
- return derive_memory_group_id(user_id), None
+ return MemoryScope.for_user(user_id)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
@@ -96,7 +84,7 @@ async def _resolve_scope(user_id: str, expert_id: str | None) -> tuple[str, str
raise HTTPException(status_code=404, detail="Expert not found")
try:
- return derive_memory_group_id(user_id, expert.id), expert.id
+ return MemoryScope.for_expert(user_id, expert.id)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
@@ -115,8 +103,8 @@ async def _count(driver: AutoGPTFalkorDriver, query: str) -> int:
async def _get_overview_impl(
user_id: str, expert_id: str | None
) -> MemoryScopeOverview:
- group_id, resolved_expert_id = await _resolve_scope(user_id, expert_id)
- driver = _open_driver(group_id)
+ scope = await _resolve_scope(user_id, expert_id)
+ driver = open_driver(scope)
try:
facts = await _count(
driver,
@@ -128,7 +116,7 @@ async def _get_overview_impl(
finally:
await driver.close()
return MemoryScopeOverview(
- expert_id=resolved_expert_id,
+ expert_id=scope.expert_id,
facts=facts,
entities=entities,
episodes=episodes,
@@ -157,8 +145,8 @@ async def get_my_expert_memory_overview(
async def _list_facts_impl(
user_id: str, expert_id: str | None, limit: int
) -> MemoryFactListResponse:
- group_id, resolved_expert_id = await _resolve_scope(user_id, expert_id)
- driver = _open_driver(group_id)
+ scope = await _resolve_scope(user_id, expert_id)
+ driver = open_driver(scope)
try:
result = await driver.execute_query(
"""
@@ -173,7 +161,7 @@ async def _list_facts_impl(
ORDER BY e.created_at DESC
LIMIT $limit
""",
- g=group_id,
+ g=scope.group_id,
limit=limit,
)
rows = result[0] if result else []
@@ -195,7 +183,7 @@ async def _list_facts_impl(
)
for r in rows
]
- return MemoryFactListResponse(expert_id=resolved_expert_id, items=items)
+ return MemoryFactListResponse(expert_id=scope.expert_id, items=items)
@router.get("/facts", operation_id="list_my_memory_facts")
@@ -220,8 +208,8 @@ async def list_my_expert_memory_facts(
async def _forget_fact_impl(
user_id: str, expert_id: str | None, fact_uuid: str
) -> ForgetFactResponse:
- group_id, _ = await _resolve_scope(user_id, expert_id)
- driver = _open_driver(group_id)
+ 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``
@@ -234,7 +222,7 @@ async def _forget_fact_impl(
RETURN e.uuid AS uuid
""",
uuid=fact_uuid,
- g=group_id,
+ g=scope.group_id,
now=_now_iso(),
)
records = result[0] if result else []
@@ -273,8 +261,8 @@ async def forget_my_expert_memory_fact(
async def _erase_scope_impl(user_id: str, expert_id: str | None) -> EraseMemoryResponse:
- group_id, resolved_expert_id = await _resolve_scope(user_id, expert_id)
- driver = _open_driver(group_id)
+ scope = await _resolve_scope(user_id, expert_id)
+ driver = open_driver(scope)
deleted = 0
try:
deleted = await _count(driver, "MATCH (n) RETURN count(n) AS c")
@@ -291,10 +279,10 @@ async def _erase_scope_impl(user_id: str, expert_id: str | None) -> EraseMemoryR
logger.info(
f"Memory erase: user {user_id[:12]} wiped scope "
- f"{resolved_expert_id or 'Otto'} ({deleted} nodes)"
+ f"{scope.expert_id or 'Otto'} ({deleted} nodes)"
)
return EraseMemoryResponse(
- expert_id=resolved_expert_id, deleted_nodes=deleted, erased=True
+ expert_id=scope.expert_id, deleted_nodes=deleted, erased=True
)
diff --git a/autogpt_platform/backend/backend/api/features/memory/routes_test.py b/autogpt_platform/backend/backend/api/features/memory/routes_test.py
index 1d8c1e3869..eb7d724308 100644
--- a/autogpt_platform/backend/backend/api/features/memory/routes_test.py
+++ b/autogpt_platform/backend/backend/api/features/memory/routes_test.py
@@ -68,7 +68,7 @@ def _expert(expert_id: str = _EXPERT_ID) -> Expert:
class TestOverview:
def test_returns_scope_counts(self) -> None:
driver = _driver_returning([{"c": 12}], [{"c": 34}], [{"c": 5}])
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.get("/memory/overview")
assert resp.status_code == 200
body = resp.json()
@@ -85,7 +85,7 @@ class TestOverview:
def test_fact_count_only_counts_live_edges(self) -> None:
driver = _driver_returning([{"c": 0}], [{"c": 0}], [{"c": 0}])
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ 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
@@ -105,7 +105,7 @@ class TestListFacts:
}
]
)
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.get("/memory/facts?limit=5")
assert resp.status_code == 200
body = resp.json()
@@ -120,7 +120,7 @@ class TestListFacts:
driver = AsyncMock()
driver.execute_query.side_effect = ResponseError("no such graph")
driver.close = AsyncMock()
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.get("/memory/facts")
assert resp.status_code == 200
assert resp.json()["items"] == []
@@ -129,12 +129,12 @@ class TestListFacts:
driver = _driver_returning([])
opened: list[str] = []
- def open_driver(group_id: str):
- opened.append(group_id)
+ def open_driver(scope):
+ opened.append(scope.group_id)
return driver
with (
- patch(f"{_MOCK_MODULE}._open_driver", side_effect=open_driver),
+ patch(f"{_MOCK_MODULE}.open_driver", side_effect=open_driver),
patch(
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(return_value=_expert()),
@@ -160,7 +160,7 @@ 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):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.delete("/memory/facts/edge-1")
assert resp.status_code == 200
assert resp.json() == {"uuid": "edge-1", "forgotten": True}
@@ -171,7 +171,7 @@ class TestForgetFact:
def test_no_match_is_404(self) -> None:
driver = _driver_returning([])
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.delete("/memory/facts/edge-unknown")
assert resp.status_code == 404
@@ -179,7 +179,7 @@ class TestForgetFact:
class TestEraseScope:
def test_erases_all_nodes(self) -> None:
driver = _driver_returning([{"c": 214}], [])
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.delete("/memory")
assert resp.status_code == 200
body = resp.json()
@@ -192,7 +192,7 @@ class TestEraseScope:
driver = AsyncMock()
driver.execute_query.side_effect = ResponseError("no such graph")
driver.close = AsyncMock()
- with patch(f"{_MOCK_MODULE}._open_driver", return_value=driver):
+ with patch(f"{_MOCK_MODULE}.open_driver", return_value=driver):
resp = client.delete("/memory")
assert resp.status_code == 200
assert resp.json()["deleted_nodes"] == 0
@@ -201,7 +201,7 @@ class TestEraseScope:
def test_expert_erase_resolves_ownership(self, test_user_id) -> None:
driver = _driver_returning([{"c": 3}], [])
with (
- patch(f"{_MOCK_MODULE}._open_driver", return_value=driver),
+ patch(f"{_MOCK_MODULE}.open_driver", return_value=driver),
patch(
f"{_MOCK_MODULE}.experts_db.get_expert",
new=AsyncMock(return_value=_expert()),
diff --git a/autogpt_platform/backend/backend/copilot/dream/apply.py b/autogpt_platform/backend/backend/copilot/dream/apply.py
index 0b7ff3083f..ee0b68e033 100644
--- a/autogpt_platform/backend/backend/copilot/dream/apply.py
+++ b/autogpt_platform/backend/backend/copilot/dream/apply.py
@@ -24,9 +24,7 @@ import uuid as uuidlib
from collections.abc import Mapping
from datetime import datetime, timezone
-from backend.copilot.graphiti.client import derive_memory_group_id
-from backend.copilot.graphiti.config import graphiti_config
-from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver
+from backend.copilot.graphiti.falkordb_driver import open_driver
from backend.copilot.graphiti.ingest import (
IngestionCompletion,
enqueue_episode,
@@ -38,6 +36,7 @@ from backend.copilot.graphiti.memory_model import (
MemoryStatus,
SourceKind,
)
+from backend.copilot.graphiti.scope import MemoryScope
from backend.copilot.tools.graphiti_forget import (
invalidate_entity_direct_neighbors,
mark_edges_superseded,
@@ -160,14 +159,12 @@ def _edge_metadata(envelope: MemoryEnvelope) -> dict:
async def _write_consolidated_fact(
- user_id: str,
+ scope: MemoryScope,
pass_id: str,
counter: int,
fact: ConsolidatedFact,
session_id: str,
completion: IngestionCompletion,
- *,
- expert_id: str | None = None,
) -> bool:
envelope = MemoryEnvelope(
content=fact.content,
@@ -179,7 +176,7 @@ async def _write_consolidated_fact(
provenance=_provenance(pass_id, "consolidate"),
)
return await enqueue_episode(
- user_id=user_id,
+ scope,
session_id=session_id,
name=_episode_name(pass_id, "consolidate", counter),
episode_body=envelope.model_dump_json(),
@@ -190,19 +187,16 @@ async def _write_consolidated_fact(
is_json=True,
edge_metadata=_edge_metadata(envelope),
completion=completion,
- expert_id=expert_id,
)
async def _write_proposed_finding(
- user_id: str,
+ scope: MemoryScope,
pass_id: str,
counter: int,
finding: ProposedFinding,
session_id: str,
completion: IngestionCompletion,
- *,
- expert_id: str | None = None,
) -> bool:
envelope = MemoryEnvelope(
content=finding.content,
@@ -219,7 +213,7 @@ async def _write_proposed_finding(
if finding.source_fact_uuids:
description_parts.append(f"src_facts={','.join(finding.source_fact_uuids[:5])}")
return await enqueue_episode(
- user_id=user_id,
+ scope,
session_id=session_id,
name=_episode_name(pass_id, "recombine", counter),
episode_body=envelope.model_dump_json(),
@@ -227,7 +221,6 @@ async def _write_proposed_finding(
is_json=True,
edge_metadata=_edge_metadata(envelope),
completion=completion,
- expert_id=expert_id,
)
@@ -299,8 +292,7 @@ async def _filter_demotions_to_known_facts(
async def _apply_demotions(
- user_id: str,
- group_id: str,
+ scope: MemoryScope,
demotions: list[DreamDemotion],
) -> tuple[int, int, list[DemotionSummary]]:
"""Run mark_edges_superseded once per (reason, new_status) bucket.
@@ -317,15 +309,7 @@ async def _apply_demotions(
for d in demotions:
buckets.setdefault((d.new_status, d.reason), []).append(d.edge_uuid)
- driver = AutoGPTFalkorDriver(
- host=graphiti_config.falkordb_host,
- port=graphiti_config.falkordb_port,
- password=graphiti_config.falkordb_password or None,
- database=group_id,
- # Indices live with the chat-write client; skip the per-driver
- # indexing race ("Buffer is closed" spam).
- build_indices=False,
- )
+ driver = open_driver(scope)
succeeded = 0
failed = 0
succeeded_uuids: set[str] = set()
@@ -336,12 +320,12 @@ async def _apply_demotions(
uuids,
reason=reason,
new_status=new_status, # type: ignore[arg-type]
- user_id=user_id,
+ user_id=scope.owner_user_id,
# Defense-in-depth: the driver is already opened against
# the per-user database, but the group_id predicate keeps
# a future wrong-driver caller from touching another
# user's edges.
- group_id=group_id,
+ group_id=scope.group_id,
)
succeeded += len(ok)
failed += len(bad)
@@ -362,7 +346,7 @@ async def _apply_demotions(
async def _apply_entity_invalidations(
- group_id: str,
+ scope: MemoryScope,
invalidations: list[EntityInvalidation],
) -> tuple[int, list[EntityInvalidationSummary]]:
"""Single-hop demotion of every :RELATES_TO around each invalidated entity.
@@ -373,22 +357,14 @@ async def _apply_entity_invalidations(
"""
if not invalidations:
return 0, []
- driver = AutoGPTFalkorDriver(
- host=graphiti_config.falkordb_host,
- port=graphiti_config.falkordb_port,
- password=graphiti_config.falkordb_password or None,
- database=group_id,
- # Indices live with the chat-write client; skip the per-driver
- # indexing race ("Buffer is closed" spam).
- build_indices=False,
- )
+ driver = open_driver(scope)
total = 0
summaries: list[EntityInvalidationSummary] = []
try:
for inv in invalidations:
uuids = await invalidate_entity_direct_neighbors(
driver,
- group_id=group_id,
+ group_id=scope.group_id,
entity_uuid=inv.entity_uuid,
reason=inv.reason,
)
@@ -405,9 +381,7 @@ async def _apply_entity_invalidations(
return total, summaries
-async def _create_dream_session(
- user_id: str, pass_id: str, expert_id: str | None = None
-) -> str:
+async def _create_dream_session(scope: MemoryScope, pass_id: str) -> str:
"""Create the dream-kind ChatSession shell and return its id.
Written up front (before the memory ops) because the fact/proposal
@@ -427,6 +401,7 @@ async def _create_dream_session(
from backend.copilot.model import ChatSessionMetadata
from backend.data.db_accessors import chat_db, orgs_db
+ user_id = scope.owner_user_id
# Dream passes run per-user with no request context; the user's
# default (personal) org is the correct tenant for their dreams.
try:
@@ -455,7 +430,7 @@ async def _create_dream_session(
organization_id=org_id,
team_id=team_id,
metadata=metadata,
- expert_id=expert_id,
+ expert_id=scope.expert_id,
)
# ``create_chat_session`` takes no title; set it via the dedicated
# accessor so the session doesn't render as "(untitled)" in the chat
@@ -552,11 +527,10 @@ async def _drain_ingestion(
async def apply_operations(
- user_id: str,
+ scope: MemoryScope,
pass_id: str,
ops: DreamOperations,
*,
- expert_id: str | None = None,
known_fact_uuids: set[str] | None = None,
ingestion_drain_timeout: float = INGESTION_DRAIN_TIMEOUT_SECONDS,
lock_handle: DreamLockHandle | None = None,
@@ -612,6 +586,7 @@ async def apply_operations(
try direct Prisma, hit "All connection attempts failed" while
the engine is still booting).
"""
+ user_id = scope.owner_user_id
if not (ops.writes or ops.proposals or ops.demotions or ops.entity_invalidations):
# Empty pass — nothing landed in memory, so don't manufacture a
# user-visible artifact for it. Creating the session shell +
@@ -635,15 +610,11 @@ async def apply_operations(
"snapshot": DreamOperationsSnapshot(),
}
- group_id = derive_memory_group_id(user_id, expert_id)
-
# Phase A — create the session shell up front so the MemoryEnvelope
# provenance can reference its id. The user-facing narrative summary
# is written AFTER the ops (see below), so a partway failure leaves an
# empty dream rather than a 'completed' narrative with no memory.
- session_id = await _create_dream_session(
- user_id=user_id, pass_id=pass_id, expert_id=expert_id
- )
+ 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
@@ -655,13 +626,12 @@ async def apply_operations(
write_summaries: list[WriteSummary] = []
for i, fact in enumerate(ops.writes):
if await _write_consolidated_fact(
- user_id,
+ scope,
pass_id,
i,
fact,
session_id=session_id,
completion=completion,
- expert_id=expert_id,
):
completion.register()
written += 1
@@ -679,13 +649,12 @@ async def apply_operations(
proposal_summaries: list[WriteSummary] = []
for i, prop in enumerate(ops.proposals):
if await _write_proposed_finding(
- user_id,
+ scope,
pass_id,
i,
prop,
session_id=session_id,
completion=completion,
- expert_id=expert_id,
):
completion.register()
proposed += 1
@@ -745,7 +714,7 @@ async def apply_operations(
pass_id, ops.demotions, known_fact_uuids
)
demoted_ok, demoted_fail, demotion_summaries = await _apply_demotions(
- user_id, group_id, demotions
+ scope, demotions
)
# Entity invalidation single-hop demotes every edge around the
# entity — the most destructive op in the pass — so it stays behind
@@ -756,7 +725,7 @@ async def apply_operations(
Flag.DREAM_PASS_INVALIDATE_ENTITY, user_id
):
entity_edges_demoted, entity_summaries = await _apply_entity_invalidations(
- group_id, ops.entity_invalidations
+ scope, ops.entity_invalidations
)
else:
entity_edges_demoted, entity_summaries = 0, []
diff --git a/autogpt_platform/backend/backend/copilot/dream/apply_test.py b/autogpt_platform/backend/backend/copilot/dream/apply_test.py
index b5c83e4b99..f46872acce 100644
--- a/autogpt_platform/backend/backend/copilot/dream/apply_test.py
+++ b/autogpt_platform/backend/backend/copilot/dream/apply_test.py
@@ -17,6 +17,7 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
from backend.copilot.graphiti.ingest import IngestionCompletion
+from backend.copilot.graphiti.scope import MemoryScope
from backend.data.db_accessors import orgs_db as real_orgs_db
from . import apply as apply_mod
@@ -52,13 +53,11 @@ def _stub_boundaries(mocker):
# (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))
- # The driver constructor + close — apply.py opens a FalkorDB driver for
+ # The driver factory + close — apply.py opens a FalkorDB driver for
# demotions and entity invalidations. Patch where it's used.
driver = mocker.MagicMock()
driver.close = AsyncMock(return_value=None)
- mocker.patch.object(
- apply_mod, "AutoGPTFalkorDriver", mocker.MagicMock(return_value=driver)
- )
+ mocker.patch.object(apply_mod, "open_driver", mocker.MagicMock(return_value=driver))
# Helper functions
mocker.patch.object(
apply_mod,
@@ -119,7 +118,7 @@ async def test_writes_become_active_envelopes():
summary_for_user="ok",
)
stats = await apply_mod.apply_operations(
- user_id="u-1234567890ab", pass_id="p-abc", ops=ops
+ scope=MemoryScope.for_user("u-1234567890ab"), pass_id="p-abc", ops=ops
)
assert stats["consolidated_count"] == 1
@@ -153,7 +152,9 @@ async def test_proposals_become_tentative_envelopes():
],
summary_for_user="ok",
)
- await apply_mod.apply_operations(user_id="u-x", pass_id="p-2", ops=ops)
+ await apply_mod.apply_operations(
+ scope=MemoryScope.for_user("u-x"), pass_id="p-2", ops=ops
+ )
# First call was the consolidate write (there were no writes, so first call IS the proposal)
call_kwargs = apply_mod.enqueue_episode.await_args.kwargs
assert call_kwargs["name"].startswith("dream_p-2_recombine_")
@@ -179,7 +180,9 @@ async def test_demotions_group_by_status_and_reason():
),
],
)
- await apply_mod.apply_operations(user_id="u-y", pass_id="p-3", ops=ops)
+ await apply_mod.apply_operations(
+ scope=MemoryScope.for_user("u-y"), pass_id="p-3", ops=ops
+ )
# Three demotions but only TWO buckets: (superseded, stale) and
# (contradicted, contradicted_by:x)
@@ -200,7 +203,9 @@ async def test_demotions_pass_group_id_to_mark_edges_superseded():
ops = DreamOperations(
demotions=[DreamDemotion(edge_uuid="a", reason="stale")],
)
- await apply_mod.apply_operations(user_id="u-gid", pass_id="p-gid", ops=ops)
+ await apply_mod.apply_operations(
+ scope=MemoryScope.for_user("u-gid"), pass_id="p-gid", ops=ops
+ )
apply_mod.mark_edges_superseded.assert_awaited_once()
# derive_group_id prefixes user ids with "user_"
@@ -219,7 +224,7 @@ async def test_hallucinated_demotion_uuids_dropped_before_cypher():
],
)
stats = await apply_mod.apply_operations(
- user_id="u-filter",
+ scope=MemoryScope.for_user("u-filter"),
pass_id="p-filter",
ops=ops,
known_fact_uuids={"known-1", "known-2"},
@@ -250,7 +255,9 @@ async def test_batch_path_demotions_validated_against_persisted_bundle(mocker):
DreamDemotion(edge_uuid="ghost", reason="stale"),
],
)
- await apply_mod.apply_operations(user_id="u-batch", pass_id="p-batch", ops=ops)
+ await apply_mod.apply_operations(
+ scope=MemoryScope.for_user("u-batch"), pass_id="p-batch", ops=ops
+ )
apply_mod.read_input_bundle.assert_awaited_once_with("p-batch")
apply_mod.mark_edges_superseded.assert_awaited_once()
@@ -267,7 +274,9 @@ async def test_missing_input_bundle_fails_open_and_keeps_demotions():
ops = DreamOperations(
demotions=[DreamDemotion(edge_uuid="unverifiable", reason="stale")],
)
- await apply_mod.apply_operations(user_id="u-open", pass_id="p-open", ops=ops)
+ await apply_mod.apply_operations(
+ scope=MemoryScope.for_user("u-open"), pass_id="p-open", ops=ops
+ )
apply_mod.mark_edges_superseded.assert_awaited_once()
assert apply_mod.mark_edges_superseded.await_args.args[1] == ["unverifiable"]
@@ -291,7 +300,7 @@ async def test_redis_blip_on_bundle_fallback_fails_open(mocker, caplog):
)
with caplog.at_level(logging.WARNING, logger=apply_mod.logger.name):
stats = await apply_mod.apply_operations(
- user_id="u-blip", pass_id="p-blip", ops=ops
+ scope=MemoryScope.for_user("u-blip"), pass_id="p-blip", ops=ops
)
apply_mod.mark_edges_superseded.assert_awaited_once()
@@ -316,7 +325,7 @@ async def test_entity_invalidations_not_filtered_by_known_fact_uuids():
],
)
await apply_mod.apply_operations(
- user_id="u-ent",
+ scope=MemoryScope.for_user("u-ent"),
pass_id="p-ent",
ops=ops,
known_fact_uuids={"some-fact"},
@@ -332,7 +341,9 @@ async def test_entity_invalidation_calls_single_hop_helper():
EntityInvalidation(entity_uuid="ent-x", reason="dead_to_us"),
],
)
- await apply_mod.apply_operations(user_id="u-z", pass_id="p-4", ops=ops)
+ await apply_mod.apply_operations(
+ scope=MemoryScope.for_user("u-z"), pass_id="p-4", ops=ops
+ )
apply_mod.invalidate_entity_direct_neighbors.assert_awaited_once()
kwargs = apply_mod.invalidate_entity_direct_neighbors.await_args.kwargs
@@ -351,7 +362,9 @@ async def test_entity_invalidation_skipped_when_flag_off(mocker):
EntityInvalidation(entity_uuid="ent-x", reason="dead_to_us"),
],
)
- stats = await apply_mod.apply_operations(user_id="u-z", pass_id="p-off", ops=ops)
+ stats = await apply_mod.apply_operations(
+ scope=MemoryScope.for_user("u-z"), pass_id="p-off", ops=ops
+ )
apply_mod.invalidate_entity_direct_neighbors.assert_not_awaited()
assert stats["entity_invalidation_count"] == 0
@@ -371,7 +384,9 @@ async def test_empty_pass_creates_no_session_and_no_message():
from .schemas import DreamOperationsSnapshot
ops = DreamOperations(summary_for_user="Nothing new today.")
- stats = await apply_mod.apply_operations(user_id="u-a", pass_id="p-5", ops=ops)
+ stats = await apply_mod.apply_operations(
+ scope=MemoryScope.for_user("u-a"), pass_id="p-5", ops=ops
+ )
copilot_db.create_chat_session.assert_not_awaited()
copilot_db.add_chat_message.assert_not_awaited()
@@ -396,7 +411,9 @@ async def test_ops_with_empty_summary_still_create_session_with_placeholder():
writes=[ConsolidatedFact(content="A likes B", confidence=0.8)],
summary_for_user="",
)
- stats = await apply_mod.apply_operations(user_id="u-ph", pass_id="p-ph", ops=ops)
+ stats = await apply_mod.apply_operations(
+ scope=MemoryScope.for_user("u-ph"), pass_id="p-ph", ops=ops
+ )
copilot_db.create_chat_session.assert_awaited_once()
copilot_db.add_chat_message.assert_awaited_once()
@@ -418,7 +435,7 @@ async def test_dream_session_titled_with_utc_date():
summary_for_user="ok",
)
stats = await apply_mod.apply_operations(
- user_id="u-title", pass_id="p-title", ops=ops
+ scope=MemoryScope.for_user("u-title"), pass_id="p-title", ops=ops
)
copilot_db.update_chat_session_title.assert_awaited_once()
@@ -445,7 +462,9 @@ async def test_title_failure_does_not_abort_apply(mocker):
writes=[ConsolidatedFact(content="A likes B", confidence=0.8)],
summary_for_user="ok",
)
- stats = await apply_mod.apply_operations(user_id="u-tf", pass_id="p-tf", ops=ops)
+ stats = await apply_mod.apply_operations(
+ scope=MemoryScope.for_user("u-tf"), pass_id="p-tf", ops=ops
+ )
assert stats["consolidated_count"] == 1
copilot_db.add_chat_message.assert_awaited_once()
@@ -458,19 +477,19 @@ async def test_expert_dream_session_and_write_keep_expert_scope(mocker):
db.update_chat_session_title = AsyncMock(return_value=True)
mocker.patch("backend.data.db_accessors.chat_db", return_value=db)
- await apply_mod._create_dream_session("u1", "p1", "expert-1")
+ expert_scope = MemoryScope.for_expert("u1", "expert-1")
+ await apply_mod._create_dream_session(expert_scope, "p1")
assert db.create_chat_session.call_args.kwargs["expert_id"] == "expert-1"
await apply_mod._write_consolidated_fact(
- "u1",
+ expert_scope,
"p1",
0,
ConsolidatedFact(content="A likes B", confidence=0.8),
"session-1",
IngestionCompletion(),
- expert_id="expert-1",
)
- assert apply_mod.enqueue_episode.call_args.kwargs["expert_id"] == "expert-1"
+ assert apply_mod.enqueue_episode.call_args.args[0] == expert_scope
@pytest.mark.asyncio
@@ -494,7 +513,7 @@ async def test_dream_session_resolves_tenant_through_db_manager(mocker):
return_value=db_manager,
)
- await apply_mod._create_dream_session("u1", "p1")
+ await apply_mod._create_dream_session(MemoryScope.for_user("u1"), "p1")
db_manager.get_user_default_team.assert_awaited_once_with("u1")
kwargs = db.create_chat_session.call_args.kwargs
@@ -534,7 +553,7 @@ async def test_summary_written_after_memory_ops(mocker):
)
await apply_mod.apply_operations(
- user_id="u-1",
+ scope=MemoryScope.for_user("u-1"),
pass_id="p-1",
ops=DreamOperations(
writes=[ConsolidatedFact(content="A likes B", confidence=0.8)],
@@ -568,7 +587,7 @@ async def test_apply_waits_for_ingestion_drain_before_reporting_counts(mocker):
summary_for_user="ok",
)
stats = await apply_mod.apply_operations(
- user_id="u-drain", pass_id="p-drain", ops=ops
+ scope=MemoryScope.for_user("u-drain"), pass_id="p-drain", ops=ops
)
drain.assert_awaited_once()
@@ -594,7 +613,7 @@ async def test_drain_timeout_reports_partial_visibility_not_failure(mocker, capl
)
with caplog.at_level(logging.WARNING, logger=apply_mod.logger.name):
stats = await apply_mod.apply_operations(
- user_id="u-slow", pass_id="p-slow", ops=ops
+ scope=MemoryScope.for_user("u-slow"), pass_id="p-slow", ops=ops
)
assert stats["ingestion_drain_status"] is IngestionDrainStatus.timed_out
@@ -621,7 +640,7 @@ async def test_zero_drain_timeout_skips_wait_and_reports_skipped(mocker, caplog)
)
with caplog.at_level(logging.INFO, logger=apply_mod.logger.name):
stats = await apply_mod.apply_operations(
- user_id="u-batch",
+ scope=MemoryScope.for_user("u-batch"),
pass_id="p-batch",
ops=ops,
ingestion_drain_timeout=apply_mod.BATCH_INGESTION_DRAIN_TIMEOUT_SECONDS,
@@ -643,7 +662,7 @@ async def test_no_enqueued_writes_skips_ingestion_drain(mocker):
)
ops = DreamOperations(summary_for_user="Nothing new today.")
stats = await apply_mod.apply_operations(
- user_id="u-empty", pass_id="p-empty", ops=ops
+ scope=MemoryScope.for_user("u-empty"), pass_id="p-empty", ops=ops
)
drain.assert_not_awaited()
@@ -663,7 +682,10 @@ async def test_sync_path_renews_lock_before_drain(mocker):
summary_for_user="ok",
)
await apply_mod.apply_operations(
- user_id="u-lock", pass_id="p-lock", ops=ops, lock_handle=lock_handle
+ scope=MemoryScope.for_user("u-lock"),
+ pass_id="p-lock",
+ ops=ops,
+ lock_handle=lock_handle,
)
lock_handle.extend.assert_awaited_once_with(apply_mod.LOCK_DRAIN_RENEWAL_SECONDS)
@@ -689,7 +711,10 @@ async def test_failed_lock_renewal_aborts_before_drain_and_demotions(mocker):
with pytest.raises(DreamLockLostError):
await apply_mod.apply_operations(
- user_id="u-lost", pass_id="p-lost", ops=ops, lock_handle=lock_handle
+ scope=MemoryScope.for_user("u-lost"),
+ pass_id="p-lost",
+ ops=ops,
+ lock_handle=lock_handle,
)
drain.assert_not_awaited()
@@ -710,7 +735,10 @@ async def test_demotions_only_pass_renews_lock_before_destructive_tail(mocker):
summary_for_user="tidied up",
)
await apply_mod.apply_operations(
- user_id="u-demote", pass_id="p-demote", ops=ops, lock_handle=lock_handle
+ scope=MemoryScope.for_user("u-demote"),
+ pass_id="p-demote",
+ ops=ops,
+ lock_handle=lock_handle,
)
lock_handle.extend.assert_awaited_once_with(apply_mod.LOCK_DRAIN_RENEWAL_SECONDS)
@@ -725,7 +753,10 @@ async def test_empty_pass_does_not_renew_lock(mocker):
lock_handle.extend = AsyncMock(return_value=None)
ops = DreamOperations(summary_for_user="Nothing new today.")
await apply_mod.apply_operations(
- user_id="u-empty", pass_id="p-empty", ops=ops, lock_handle=lock_handle
+ scope=MemoryScope.for_user("u-empty"),
+ pass_id="p-empty",
+ ops=ops,
+ lock_handle=lock_handle,
)
lock_handle.extend.assert_not_awaited()
@@ -820,7 +851,7 @@ async def test_apply_operations_never_auto_connects_prisma(mocker):
connect_spy = mocker.patch("backend.data.db.connect", new_callable=AsyncMock)
await apply_mod.apply_operations(
- user_id="u-1",
+ scope=MemoryScope.for_user("u-1"),
pass_id="p-1",
ops=DreamOperations(
writes=[],
@@ -883,7 +914,7 @@ async def test_apply_operations_returns_snapshot_with_per_op_detail(mocker):
summary_for_user="ok",
)
stats = await apply_mod.apply_operations(
- user_id="u-snap", pass_id="p-snap", ops=ops
+ scope=MemoryScope.for_user("u-snap"), pass_id="p-snap", ops=ops
)
snap = stats["snapshot"]
@@ -927,7 +958,7 @@ async def test_apply_operations_demotion_summary_marks_applied_false_on_miss(moc
],
)
stats = await apply_mod.apply_operations(
- user_id="u-miss", pass_id="p-miss", ops=ops
+ scope=MemoryScope.for_user("u-miss"), pass_id="p-miss", ops=ops
)
snap = stats["snapshot"]
assert isinstance(snap, DreamOperationsSnapshot)
diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py
index 01eb1af639..c56e6446f8 100644
--- a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py
+++ b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py
@@ -42,6 +42,7 @@ from typing import TYPE_CHECKING, Any, Literal
from pydantic import ValidationError
+from backend.copilot.graphiti.scope import MemoryScope
from backend.copilot.trial_cost_context import TrialCostContext, restore_cost_context
from backend.copilot.usage_activation import UsageActivationUnavailable
@@ -246,9 +247,7 @@ async def _best_effort_cleanup(pass_id: str) -> None:
)
-async def _release_lock(
- user_id: str, pass_id: str, expert_id: str | None = None
-) -> None:
+async def _release_lock(user_id: str, pass_id: str, expert_id: str | None) -> None:
"""Release the disowned dream lock with the ownership token persisted
alongside the input bundle. Must run before ``delete_input_bundle`` —
the token rides on that key. A missing token (bundle TTL'd out,
@@ -269,7 +268,16 @@ async def _release_lock(
"leaving the lock to its TTL",
pass_id,
)
- await release_dream_lock(user_id, token, expert_id)
+ try:
+ scope = MemoryScope.build(user_id, expert_id)
+ except ValueError:
+ logger.warning(
+ "Invalid memory scope for disowned dream lock of user %s — "
+ "leaving it for the TTL to clear",
+ user_id[:12],
+ )
+ return
+ await release_dream_lock(scope, token)
# ---------------------------------------------------------------------------
@@ -449,7 +457,7 @@ async def _handle_phase_result(
*,
rows: list[BatchResultRow],
user_id: str,
- expert_id: str | None = None,
+ expert_id: str | None,
input_bundle: DreamInput,
pass_id: str,
job_id: str,
@@ -544,7 +552,7 @@ async def _handle_phase_result(
async def _chain_next_phase(
*,
user_id: str,
- expert_id: str | None = None,
+ expert_id: str | None,
input_bundle: DreamInput,
pass_id: str,
job_id: str,
@@ -679,7 +687,7 @@ async def _claim_apply_gate(pass_id: str) -> Literal["claimed", "duplicate", "er
async def _finalize_complete(
*,
user_id: str,
- expert_id: str | None = None,
+ expert_id: str | None,
input_bundle: DreamInput,
pass_id: str,
job_id: str,
@@ -795,10 +803,9 @@ async def _finalize_complete(
# every other user's pending batch. See
# ``BATCH_INGESTION_DRAIN_TIMEOUT_SECONDS``.
apply_stats = await apply_operations(
- user_id,
+ MemoryScope.build(user_id, expert_id),
pass_id,
ops,
- expert_id=expert_id,
known_fact_uuids=input_bundle.known_fact_uuids,
ingestion_drain_timeout=BATCH_INGESTION_DRAIN_TIMEOUT_SECONDS,
)
@@ -888,7 +895,7 @@ async def _finalize_complete(
async def _fail_pass(
*,
user_id: str,
- expert_id: str | None = None,
+ expert_id: str | None,
pass_id: str,
job_id: str,
phase_models: dict[str, str],
diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py
index c255940a1e..c837017038 100644
--- a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py
+++ b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py
@@ -18,8 +18,8 @@ from backend.copilot.dream.batch_callbacks import handle_dream_batch_result
from backend.copilot.dream.batch_submit import input_bundle_key, persist_input_bundle
from backend.copilot.dream.fetch import DreamInput
from backend.copilot.dream.job_status import read_status, write_initial_status
-from backend.copilot.dream.locks import _lock_key
from backend.copilot.dream.schemas import IngestionDrainStatus
+from backend.copilot.graphiti.scope import MemoryScope
from backend.copilot.trial_cost_context import get_trial_cost_context
from backend.copilot.usage_activation import UsageActivationUnavailable
from backend.executor.batch_executor import PendingEntry, _dispatch
@@ -190,7 +190,7 @@ class TestPhaseChaining:
mark_errored.await_args.kwargs["error"]
== "batch DreamInput missing; memory scope unavailable"
)
- release_lock.assert_awaited_once_with("u1", None, None)
+ release_lock.assert_awaited_once_with(MemoryScope.for_user("u1"), None)
@pytest.mark.asyncio
async def test_autopilot_payload_cannot_apply_expert_dream(
@@ -241,7 +241,9 @@ class TestPhaseChaining:
mark_errored.await_args.kwargs["error"]
== "batch payload memory scope mismatch"
)
- release_lock.assert_awaited_once_with("u1", "tok-expert", "expert-1")
+ release_lock.assert_awaited_once_with(
+ MemoryScope.for_expert("u1", "expert-1"), "tok-expert"
+ )
@pytest.mark.asyncio
async def test_cross_expert_payload_cannot_apply_other_expert_dream(
@@ -299,7 +301,9 @@ class TestPhaseChaining:
mark_errored.await_args.kwargs["error"]
== "batch payload memory scope mismatch"
)
- release_lock.assert_awaited_once_with("u1", "tok-expert", "expert-1")
+ release_lock.assert_awaited_once_with(
+ MemoryScope.for_expert("u1", "expert-1"), "tok-expert"
+ )
@pytest.mark.asyncio
async def test_consolidate_result_submits_recombine_batch(self, fake_redis):
@@ -478,7 +482,7 @@ class TestPhaseChaining:
# The batch path disowned the dream lock to this callback; the
# terminal handler must release it with the ownership token the
# input bundle carried — compare-and-delete, never a blind DEL.
- release_lock.assert_awaited_once_with("u1", "tok-u1", None)
+ release_lock.assert_awaited_once_with(MemoryScope.for_user("u1"), "tok-u1")
# One cost-log row per phase (consolidate, recombine, sanitize)
assert record_cost.await_count == 3
for call in record_cost.await_args_list:
@@ -548,8 +552,10 @@ class TestPhaseChaining:
[_row(custom_id="p-expert:sanitize", content=_SANITIZE_CONTENT)],
)
- assert apply.call_args.kwargs["expert_id"] == "expert-1"
- release_lock.assert_awaited_once_with("u1", "tok-expert", "expert-1")
+ assert apply.call_args.args[0] == MemoryScope.for_expert("u1", "expert-1")
+ release_lock.assert_awaited_once_with(
+ MemoryScope.for_expert("u1", "expert-1"), "tok-expert"
+ )
@pytest.mark.asyncio
async def test_redispatch_after_charge_does_not_double_bill(self, fake_redis):
@@ -682,7 +688,7 @@ class TestPhaseChaining:
# safe no-op) and clean up state.
assert release_lock.await_count == 2
for call in release_lock.await_args_list:
- assert call.args == ("u1", "tok-u1", None)
+ assert call.args == (MemoryScope.for_user("u1"), "tok-u1")
@pytest.mark.asyncio
async def test_apply_gate_redis_outage_fails_pass_not_silent_success(
@@ -922,7 +928,7 @@ class TestErrorPaths:
mark_errored.assert_awaited_once()
assert "handler crashed" in mark_errored.call_args.kwargs["error"]
# Released with the ownership token the input bundle carried.
- release_lock.assert_awaited_once_with("u1", "tok-u1", None)
+ release_lock.assert_awaited_once_with(MemoryScope.for_user("u1"), "tok-u1")
class TestMalformedPayload:
@@ -941,7 +947,7 @@ class TestMalformedPayload:
entry,
[_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT)],
)
- release.assert_awaited_once_with("u1", None, None)
+ release.assert_awaited_once_with(MemoryScope.for_user("u1"), None)
@pytest.mark.asyncio
async def test_unknown_phase_label_releases_lock_with_persisted_token(
@@ -968,7 +974,7 @@ class TestMalformedPayload:
entry.payload["phase"] = "some_fake_phase"
with patch("backend.copilot.dream.batch_callbacks.release_dream_lock", release):
await handle_dream_batch_result(entry, [_row(custom_id="x", content="y")])
- release.assert_awaited_once_with("u1", "tok-u1", None)
+ release.assert_awaited_once_with(MemoryScope.for_user("u1"), "tok-u1")
class TestLockTokenWiring:
@@ -1065,7 +1071,7 @@ class TestLockTokenWiring:
mark_errored.assert_not_awaited()
# Token read failed → token-less release; release_dream_lock then
# defers to the lock TTL rather than blind-deleting.
- release_lock.assert_awaited_once_with("u1", None, None)
+ release_lock.assert_awaited_once_with(MemoryScope.for_user("u1"), None)
@pytest.mark.asyncio
async def test_cleanup_failure_after_complete_keeps_job_completed(self, fake_redis):
@@ -1252,7 +1258,9 @@ async def test_dispatch_invalid_attribution_closes_job_without_billing(
lock_token="original-token",
)
await write_initial_status(kind="dream_pass", job_id="j1", user_id="owner")
- lock_key = _lock_key("owner", "expert-original")
+ lock_key = MemoryScope.for_expert("owner", "expert-original").redis_key(
+ "dream_lock"
+ )
string_store[lock_key] = "newer-token" if newer_lock else "original-token"
string_store["dream:inflight:u1"] = "unrelated-token"
state_store["dream:batch:state:p1"] = {
diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_submit.py b/autogpt_platform/backend/backend/copilot/dream/batch_submit.py
index 5ab5f3ee96..4575d15c7a 100644
--- a/autogpt_platform/backend/backend/copilot/dream/batch_submit.py
+++ b/autogpt_platform/backend/backend/copilot/dream/batch_submit.py
@@ -24,6 +24,7 @@ from typing import TYPE_CHECKING, Literal
from pydantic import BaseModel
+from backend.copilot.graphiti.scope import MemoryScope
from backend.copilot.trial_cost_context import capture_cost_context
from backend.executor.batch_executor import (
INITIAL_POLL_DELAY_SECONDS,
@@ -337,7 +338,7 @@ async def persist_input_bundle(
payload = _input_bundle_to_dict(input_bundle)
if lock_token is None:
lock_token = await read_dream_lock_token(
- input_bundle.user_id, input_bundle.expert_id
+ MemoryScope.build(input_bundle.user_id, input_bundle.expert_id)
)
if lock_token is not None:
payload["lock_token"] = lock_token
diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_submit_test.py b/autogpt_platform/backend/backend/copilot/dream/batch_submit_test.py
index e5ea87a63a..92ad7e76d3 100644
--- a/autogpt_platform/backend/backend/copilot/dream/batch_submit_test.py
+++ b/autogpt_platform/backend/backend/copilot/dream/batch_submit_test.py
@@ -24,7 +24,7 @@ from backend.copilot.dream.batch_submit import (
submit_phase,
)
from backend.copilot.dream.fetch import DreamInput
-from backend.copilot.dream.locks import DREAM_LOCK_KEY_PREFIX
+from backend.copilot.graphiti.scope import DREAM_LOCK_KEY_PREFIX, MemoryScope
from backend.util.llm.providers import BatchSubmissionRef
@@ -174,7 +174,7 @@ async def test_expert_scope_round_trips_and_routes_live_lock_lookup(fake_redis):
):
await persist_input_bundle("p-expert", _bundle("expert-1"))
- read_live_token.assert_awaited_once_with("u1", "expert-1")
+ read_live_token.assert_awaited_once_with(MemoryScope.for_expert("u1", "expert-1"))
bundle = await read_input_bundle("p-expert")
assert bundle is not None
assert bundle.expert_id == "expert-1"
diff --git a/autogpt_platform/backend/backend/copilot/dream/fetch.py b/autogpt_platform/backend/backend/copilot/dream/fetch.py
index 5393ab6a7d..db5182580c 100644
--- a/autogpt_platform/backend/backend/copilot/dream/fetch.py
+++ b/autogpt_platform/backend/backend/copilot/dream/fetch.py
@@ -18,9 +18,8 @@ from datetime import datetime, timedelta, timezone
from pydantic import BaseModel, Field
-from backend.copilot.graphiti.client import derive_memory_group_id
-from backend.copilot.graphiti.config import graphiti_config
-from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver
+from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver, open_driver
+from backend.copilot.graphiti.scope import MemoryScope
from backend.data.db_accessors import chat_db
logger = logging.getLogger(__name__)
@@ -137,20 +136,6 @@ def _parse_iso_timestamp(raw: str) -> datetime | None:
return parsed if parsed.tzinfo else parsed.replace(tzinfo=timezone.utc)
-def _open_driver(group_id: str) -> AutoGPTFalkorDriver:
- return AutoGPTFalkorDriver(
- host=graphiti_config.falkordb_host,
- port=graphiti_config.falkordb_port,
- password=graphiti_config.falkordb_password or None,
- database=group_id,
- # Indices are built by the long-lived chat-write client when the
- # user first writes a memory; the dream pass reads from an
- # already-indexed graph and shouldn't refire the
- # background-task race that produces "Buffer is closed" spam.
- build_indices=False,
- )
-
-
async def _fetch_recent_episodes(
driver: AutoGPTFalkorDriver,
group_id: str,
@@ -253,10 +238,9 @@ async def _fetch_active_facts(
async def _fetch_recent_sessions(
- user_id: str,
+ scope: MemoryScope,
window_start: datetime,
limit: int,
- expert_id: str | None = None,
) -> list[SessionRow]:
"""Pull the most recent N chat sessions and their first chunk of content.
@@ -274,14 +258,15 @@ async def _fetch_recent_sessions(
clamp does the bounding.
"""
_ = window_start
+ user_id = scope.owner_user_id
try:
- if expert_id is None:
+ if scope.expert_id is None:
sessions = await chat_db().get_user_chat_sessions(
user_id, limit=limit, autopilot_only=True
)
else:
sessions = await chat_db().get_user_chat_sessions(
- user_id, limit=limit, expert_id=expert_id
+ user_id, limit=limit, expert_id=scope.expert_id
)
except Exception:
logger.warning(
@@ -335,9 +320,8 @@ async def _fetch_recent_sessions(
async def gather_dream_input(
- user_id: str,
+ scope: MemoryScope,
*,
- expert_id: str | None = None,
window_days: int = DEFAULT_WINDOW_DAYS,
max_episodes: int = MAX_EPISODES,
max_facts: int = MAX_ACTIVE_FACTS,
@@ -349,11 +333,11 @@ async def gather_dream_input(
the three sources fails (Cypher error, Prisma timeout) the others
still proceed — a partial dream is better than no dream.
"""
- group_id = derive_memory_group_id(user_id, expert_id)
+ group_id = scope.group_id
window_end = datetime.now(timezone.utc)
window_start = window_end - timedelta(days=window_days)
- driver = _open_driver(group_id)
+ driver = open_driver(scope)
try:
episodes = await _fetch_recent_episodes(
driver, group_id, window_start, max_episodes
@@ -362,13 +346,11 @@ async def gather_dream_input(
finally:
await driver.close()
- sessions = await _fetch_recent_sessions(
- user_id, window_start, max_sessions, expert_id
- )
+ sessions = await _fetch_recent_sessions(scope, window_start, max_sessions)
return DreamInput(
- user_id=user_id,
- expert_id=expert_id,
+ user_id=scope.owner_user_id,
+ expert_id=scope.expert_id,
group_id=group_id,
window_start=window_start,
window_end=window_end,
diff --git a/autogpt_platform/backend/backend/copilot/dream/fetch_test.py b/autogpt_platform/backend/backend/copilot/dream/fetch_test.py
index 9278593fbe..9c750ae387 100644
--- a/autogpt_platform/backend/backend/copilot/dream/fetch_test.py
+++ b/autogpt_platform/backend/backend/copilot/dream/fetch_test.py
@@ -4,6 +4,8 @@ from unittest.mock import AsyncMock
import pytest
+from backend.copilot.graphiti.scope import MemoryScope
+
from . import fetch as fetch_mod
@@ -12,7 +14,9 @@ async def test_autopilot_dream_fetches_only_unscoped_sessions(mocker):
database = SimpleNamespace(get_user_chat_sessions=AsyncMock(return_value=[]))
mocker.patch.object(fetch_mod, "chat_db", return_value=database)
- await fetch_mod._fetch_recent_sessions("user-1", datetime.now(timezone.utc), 10)
+ await fetch_mod._fetch_recent_sessions(
+ MemoryScope.for_user("user-1"), datetime.now(timezone.utc), 10
+ )
database.get_user_chat_sessions.assert_awaited_once_with(
"user-1", limit=10, autopilot_only=True
@@ -25,7 +29,7 @@ async def test_expert_dream_fetches_only_that_experts_sessions(mocker):
mocker.patch.object(fetch_mod, "chat_db", return_value=database)
await fetch_mod._fetch_recent_sessions(
- "user-1", datetime.now(timezone.utc), 10, "expert-1"
+ MemoryScope.for_expert("user-1", "expert-1"), datetime.now(timezone.utc), 10
)
database.get_user_chat_sessions.assert_awaited_once_with(
diff --git a/autogpt_platform/backend/backend/copilot/dream/locks.py b/autogpt_platform/backend/backend/copilot/dream/locks.py
index 4f37c492d4..8335a72dd7 100644
--- a/autogpt_platform/backend/backend/copilot/dream/locks.py
+++ b/autogpt_platform/backend/backend/copilot/dream/locks.py
@@ -28,12 +28,10 @@ import uuid
from contextlib import asynccontextmanager
from typing import Any, cast
-from backend.copilot.graphiti.client import derive_memory_scope_key
+from backend.copilot.graphiti.scope import MemoryScope
logger = logging.getLogger(__name__)
-DREAM_LOCK_KEY_PREFIX = "dream:inflight:"
-
# Sync-path lock TTL (30 min).
DEFAULT_LOCK_TTL_SECONDS = 1800
@@ -62,11 +60,6 @@ _EXTEND_SCRIPT = (
)
-def _lock_key(user_id: str, expert_id: str | None = None) -> str:
- scope_id = derive_memory_scope_key(user_id, expert_id)
- return f"{DREAM_LOCK_KEY_PREFIX}{scope_id}"
-
-
class DreamLockHeld(Exception):
"""Raised when another process is already running a dream for this user."""
@@ -144,10 +137,8 @@ class DreamLockHandle:
@asynccontextmanager
async def dream_lock(
- user_id: str,
+ scope: MemoryScope,
ttl_seconds: int = DEFAULT_LOCK_TTL_SECONDS,
- *,
- expert_id: str | None = None,
):
"""Acquire a per-user advisory lock for the dream pass.
@@ -160,8 +151,9 @@ async def dream_lock(
# Lazy import so this module is cheap to import in tests that mock redis.
from backend.data.redis_client import get_redis_async
+ user_id = scope.owner_user_id
redis = await get_redis_async()
- key = _lock_key(user_id, expert_id)
+ key = scope.redis_key("dream_lock")
token = str(uuid.uuid4())
acquired = await redis.set(key, token, nx=True, ex=ttl_seconds)
@@ -201,9 +193,7 @@ async def dream_lock(
)
-async def read_dream_lock_token(
- user_id: str, expert_id: str | None = None
-) -> str | None:
+async def read_dream_lock_token(scope: MemoryScope) -> str | None:
"""Current holder's ownership token, or None when no lock is held.
The batch path calls this while still holding the lock (at input-bundle
@@ -214,7 +204,7 @@ async def read_dream_lock_token(
from backend.data.redis_client import get_redis_async
redis = await get_redis_async()
- raw = await redis.get(_lock_key(user_id, expert_id))
+ raw = await redis.get(scope.redis_key("dream_lock"))
if raw is None:
return None
if isinstance(raw, bytes):
@@ -222,9 +212,7 @@ async def read_dream_lock_token(
return str(raw)
-async def release_dream_lock(
- user_id: str, token: str | None, expert_id: str | None = None
-) -> None:
+async def release_dream_lock(scope: MemoryScope, token: str | None) -> None:
"""Release a disowned dream lock (batch path) once the pass terminates.
Compare-and-delete on ``token``: a blind delete is NOT safe here — the
@@ -236,6 +224,7 @@ async def release_dream_lock(
extra lockout beats releasing someone else's lock. A failed delete
likewise falls back to the TTL.
"""
+ user_id = scope.owner_user_id
if token is None:
logger.warning(
"No ownership token for disowned dream lock of user %s — "
@@ -252,7 +241,7 @@ async def release_dream_lock(
redis.eval(
_UNLOCK_SCRIPT,
1,
- _lock_key(user_id, expert_id),
+ scope.redis_key("dream_lock"),
token,
),
)
diff --git a/autogpt_platform/backend/backend/copilot/dream/locks_test.py b/autogpt_platform/backend/backend/copilot/dream/locks_test.py
index 6d0da71f14..6d206af952 100644
--- a/autogpt_platform/backend/backend/copilot/dream/locks_test.py
+++ b/autogpt_platform/backend/backend/copilot/dream/locks_test.py
@@ -7,6 +7,8 @@ from unittest.mock import AsyncMock
import pytest
+from backend.copilot.graphiti.scope import MemoryScope
+
from .locks import (
BATCH_LOCK_TTL_SECONDS,
DEFAULT_LOCK_TTL_SECONDS,
@@ -41,7 +43,9 @@ async def test_dream_lock_stores_uuid_token_and_releases_via_compare_and_delete(
redis = _redis_mock()
_patch_redis(mocker, redis)
- async with dream_lock("user-a", ttl_seconds=DEFAULT_LOCK_TTL_SECONDS) as handle:
+ async with dream_lock(
+ MemoryScope.for_user("user-a"), ttl_seconds=DEFAULT_LOCK_TTL_SECONDS
+ ) as handle:
token = handle.token
# Acquire: SETNX with a uuid4 ownership token as the value, not "1".
@@ -67,7 +71,7 @@ async def test_dream_lock_raises_when_already_held(mocker):
_patch_redis(mocker, redis)
with pytest.raises(DreamLockHeld):
- async with dream_lock("user-b"):
+ async with dream_lock(MemoryScope.for_user("user-b")):
pytest.fail("should not enter the body when lock is held")
# And critically: we don't try to delete a key that wasn't ours.
@@ -80,7 +84,7 @@ async def test_expert_dream_lock_uses_isolated_memory_scope_key(mocker):
redis = _redis_mock()
_patch_redis(mocker, redis)
- async with dream_lock("user-a", expert_id="expert-a"):
+ async with dream_lock(MemoryScope.for_expert("user-a", "expert-a")):
pass
key = redis.set.call_args.args[0]
@@ -96,7 +100,7 @@ async def test_dream_lock_swallows_release_failure(mocker, caplog):
# The TTL is the fallback release, so a release failure must not
# propagate up to the orchestrator.
- async with dream_lock("user-c"):
+ async with dream_lock(MemoryScope.for_user("user-c")):
pass
assert "Failed to release dream lock" in caplog.text
@@ -109,7 +113,7 @@ async def test_release_on_exit_skips_delete_when_token_mismatch(mocker, caplog):
redis = _redis_mock(eval=AsyncMock(return_value=0))
_patch_redis(mocker, redis)
- async with dream_lock("user-d"):
+ async with dream_lock(MemoryScope.for_user("user-d")):
pass
redis.eval.assert_awaited_once()
@@ -125,7 +129,7 @@ async def test_dream_lock_disown_skips_release_and_extends_ttl(mocker):
redis = _redis_mock()
_patch_redis(mocker, redis)
- async with dream_lock("user-e") as handle:
+ async with dream_lock(MemoryScope.for_user("user-e")) as handle:
await handle.extend(BATCH_LOCK_TTL_SECONDS)
handle.disown()
@@ -151,7 +155,7 @@ async def test_extend_warns_when_lock_already_expired(mocker, caplog):
redis = _redis_mock(eval=AsyncMock(return_value=0))
_patch_redis(mocker, redis)
- async with dream_lock("user-f") as handle:
+ async with dream_lock(MemoryScope.for_user("user-f")) as handle:
await handle.extend(BATCH_LOCK_TTL_SECONDS)
handle.disown()
@@ -191,7 +195,7 @@ async def test_extend_leaves_lock_reacquired_by_newer_pass_untouched(mocker, cap
)
_patch_redis(mocker, redis)
- async with dream_lock("user-m") as handle:
+ async with dream_lock(MemoryScope.for_user("user-m")) as handle:
# Simulate expiry + re-acquire by a newer pass before our extend.
store[key] = "tok-newer-pass"
ttls[key] = DEFAULT_LOCK_TTL_SECONDS
@@ -208,7 +212,7 @@ async def test_release_dream_lock_compare_and_deletes_with_token(mocker):
redis = _redis_mock()
_patch_redis(mocker, redis)
- await release_dream_lock("user-g", "tok-g")
+ await release_dream_lock(MemoryScope.for_user("user-g"), "tok-g")
redis.eval.assert_awaited_once()
eval_args = redis.eval.call_args.args
@@ -226,7 +230,7 @@ async def test_release_skips_delete_when_token_mismatch(mocker, caplog):
redis = _redis_mock(eval=AsyncMock(return_value=0))
_patch_redis(mocker, redis)
- await release_dream_lock("user-h", "stale-token")
+ await release_dream_lock(MemoryScope.for_user("user-h"), "stale-token")
redis.eval.assert_awaited_once()
redis.delete.assert_not_awaited()
@@ -240,7 +244,7 @@ async def test_release_without_token_leaves_lock_for_ttl(mocker, caplog):
redis = _redis_mock()
_patch_redis(mocker, redis)
- await release_dream_lock("user-i", None)
+ await release_dream_lock(MemoryScope.for_user("user-i"), None)
redis.eval.assert_not_awaited()
redis.delete.assert_not_awaited()
@@ -252,7 +256,7 @@ async def test_release_dream_lock_swallows_redis_failure(mocker, caplog):
redis = _redis_mock(eval=AsyncMock(side_effect=Exception("redis down")))
_patch_redis(mocker, redis)
- await release_dream_lock("user-j", "tok-j")
+ await release_dream_lock(MemoryScope.for_user("user-j"), "tok-j")
assert "Failed to release disowned dream lock" in caplog.text
@@ -262,7 +266,7 @@ async def test_read_dream_lock_token_decodes_current_holder(mocker):
redis = _redis_mock(get=AsyncMock(return_value=b"tok-bytes"))
_patch_redis(mocker, redis)
- assert await read_dream_lock_token("user-k") == "tok-bytes"
+ assert await read_dream_lock_token(MemoryScope.for_user("user-k")) == "tok-bytes"
redis.get.assert_awaited_once_with("dream:inflight:user-k")
@@ -271,4 +275,4 @@ async def test_read_dream_lock_token_none_when_unheld(mocker):
redis = _redis_mock(get=AsyncMock(return_value=None))
_patch_redis(mocker, redis)
- assert await read_dream_lock_token("user-l") is None
+ assert await read_dream_lock_token(MemoryScope.for_user("user-l")) is None
diff --git a/autogpt_platform/backend/backend/copilot/dream/orchestrator.py b/autogpt_platform/backend/backend/copilot/dream/orchestrator.py
index b00a2492fb..65d4463b11 100644
--- a/autogpt_platform/backend/backend/copilot/dream/orchestrator.py
+++ b/autogpt_platform/backend/backend/copilot/dream/orchestrator.py
@@ -25,7 +25,7 @@ from datetime import datetime, timezone
from typing import TypeVar
from backend.copilot.config import ChatConfig
-from backend.copilot.graphiti.client import derive_memory_scope_key
+from backend.copilot.graphiti.scope import MemoryScope
from backend.copilot.trial_cost_context import attributed_usage
from backend.util.feature_flag import Flag, is_feature_enabled
@@ -153,7 +153,7 @@ MAX_ENTITY_INVALIDATIONS_PER_PASS = 2
# outlives the 14-day episode window; an expired or missing marker just
# means one extra full pass (fail-open). The batch path does NOT stamp
# this marker yet — batch users simply never benefit from the skip.
-LAST_COMPLETED_KEY_PREFIX = "dream:last_completed:"
+# The key is ``MemoryScope.redis_key("last_completed")``.
LAST_COMPLETED_TTL_SECONDS = 35 * 24 * 60 * 60
@@ -637,14 +637,7 @@ def _aggregate_usage(
)
-def _last_completed_key(user_id: str, expert_id: str | None = None) -> str:
- scope_id = derive_memory_scope_key(user_id, expert_id)
- return f"{LAST_COMPLETED_KEY_PREFIX}{scope_id}"
-
-
-async def _read_last_completed_marker(
- user_id: str, expert_id: str | None = None
-) -> datetime | None:
+async def _read_last_completed_marker(scope: MemoryScope) -> datetime | None:
"""When the user's last dream pass completed, or ``None``.
Best-effort: a Redis error or an unparseable value fails open
@@ -655,9 +648,10 @@ async def _read_last_completed_marker(
# in tests that mock redis.
from backend.data.redis_client import get_redis_async
+ user_id = scope.owner_user_id
try:
redis = await get_redis_async()
- raw = await redis.get(_last_completed_key(user_id, expert_id))
+ raw = await redis.get(scope.redis_key("last_completed"))
except Exception:
logger.warning(
"Failed to read dream last-completed marker for user %s — "
@@ -681,9 +675,7 @@ async def _read_last_completed_marker(
return marker if marker.tzinfo else marker.replace(tzinfo=timezone.utc)
-async def _stamp_last_completed_marker(
- user_id: str, as_of: datetime, expert_id: str | None = None
-) -> None:
+async def _stamp_last_completed_marker(scope: MemoryScope, as_of: datetime) -> None:
"""Record the upper bound of the episode window the pass consolidated.
``as_of`` must be the gather snapshot time (``DreamInput.window_end``),
@@ -700,14 +692,14 @@ async def _stamp_last_completed_marker(
try:
redis = await get_redis_async()
await redis.set(
- _last_completed_key(user_id, expert_id),
+ scope.redis_key("last_completed"),
as_of.isoformat(),
ex=LAST_COMPLETED_TTL_SECONDS,
)
except Exception:
logger.warning(
"Failed to stamp dream last-completed marker for user %s",
- user_id[:12],
+ scope.owner_user_id[:12],
exc_info=True,
)
@@ -737,7 +729,7 @@ def _has_new_episodes_since(episodes: list[EpisodeRow], marker: datetime) -> boo
async def _execute_dream_pass_async(
user_id: str,
*,
- expert_id: str | None = None,
+ expert_id: str | None,
config: ChatConfig | None = None,
status_id: str | None = None,
) -> DreamPassResult:
@@ -770,7 +762,8 @@ async def _execute_dream_pass_async(
ttl = DEFAULT_LOCK_TTL_SECONDS
try:
- lock_context = dream_lock(user_id, ttl_seconds=ttl, expert_id=expert_id)
+ scope = MemoryScope.build(user_id, expert_id)
+ lock_context = dream_lock(scope, ttl_seconds=ttl)
async with lock_context as dream_lock_handle:
# Pre-flight billing check. Runs inside the lock so a
# paywalled user doesn't burn the slot for an eligible
@@ -797,7 +790,7 @@ async def _execute_dream_pass_async(
skip_reason=budget_skip or "insufficient_credits",
)
- input_bundle = await gather_dream_input(user_id, expert_id=expert_id)
+ input_bundle = await gather_dream_input(scope)
if not input_bundle.episodes and not input_bundle.facts:
# Nothing to consolidate — early-return as skipped so the
@@ -826,9 +819,7 @@ async def _execute_dream_pass_async(
# silent no_new_activity skip would neuter it for up to the
# marker's 35-day TTL.
last_completed = (
- await _read_last_completed_marker(user_id, expert_id)
- if status_id is None
- else None
+ await _read_last_completed_marker(scope) if status_id is None else None
)
if last_completed is not None and not _has_new_episodes_since(
input_bundle.episodes, last_completed
@@ -963,10 +954,9 @@ async def _execute_dream_pass_async(
known_fact_uuids=input_bundle.known_fact_uuids,
)
apply_stats = await apply_operations(
- user_id,
+ scope,
pass_id,
ops,
- expert_id=expert_id,
known_fact_uuids=input_bundle.known_fact_uuids,
lock_handle=dream_lock_handle,
)
@@ -976,9 +966,7 @@ async def _execute_dream_pass_async(
# mid-pass still count as new next time. Sync path only: batch
# apply runs hours later in batch_callbacks, which doesn't
# stamp yet.
- await _stamp_last_completed_marker(
- user_id, input_bundle.window_end, expert_id
- )
+ await _stamp_last_completed_marker(scope, input_bundle.window_end)
completed_at = datetime.now(timezone.utc)
snapshot = apply_stats.get("snapshot")
diff --git a/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py b/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py
index 1039643bc3..2c0eb4b8cb 100644
--- a/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py
+++ b/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py
@@ -15,6 +15,7 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
from backend.copilot import usage_activation_fixtures
+from backend.copilot.graphiti.scope import MemoryScope
from backend.executor.scheduler import SCHEDULER_DREAM_OPERATION_TIMEOUT_SECONDS
from . import orchestrator as orchestrator_mod
@@ -192,8 +193,9 @@ async def test_expert_scope_is_threaded_to_lock_and_gather(mocker):
result = await orchestrator_mod.execute_dream_pass("u", expert_id="expert-1")
assert result.skipped is True
- assert lock_calls[0][1]["expert_id"] == "expert-1"
- gather.assert_awaited_once_with("u", expert_id="expert-1")
+ expert_scope = MemoryScope.for_expert("u", "expert-1")
+ assert lock_calls[0][0][0] == expert_scope
+ gather.assert_awaited_once_with(expert_scope)
@pytest.mark.asyncio
@@ -222,10 +224,11 @@ async def test_expert_scope_reaches_marker_and_apply_on_nonempty_pass(mocker):
assert result.skipped is False
assert result.error is None
- gather.assert_awaited_once_with("u", expert_id="expert-1")
- read_marker.assert_awaited_once_with("u", "expert-1")
- assert apply_mock.await_args.kwargs["expert_id"] == "expert-1"
- stamp_marker.assert_awaited_once_with("u", input_bundle.window_end, "expert-1")
+ expert_scope = MemoryScope.for_expert("u", "expert-1")
+ gather.assert_awaited_once_with(expert_scope)
+ read_marker.assert_awaited_once_with(expert_scope)
+ assert apply_mock.await_args.args[0] == expert_scope
+ stamp_marker.assert_awaited_once_with(expert_scope, input_bundle.window_end)
@pytest.mark.asyncio
@@ -567,11 +570,10 @@ async def test_clamps_oversized_sanitizer_output(mocker):
captured: dict[str, DreamOperations] = {}
async def fake_apply(
- user_id,
+ scope,
pass_id,
ops,
*,
- expert_id=None,
known_fact_uuids=None,
lock_handle=None,
):
@@ -636,11 +638,10 @@ async def test_demotions_capped_at_five_percent_of_active_facts(mocker):
captured: dict[str, DreamOperations] = {}
async def fake_apply(
- user_id,
+ scope,
pass_id,
ops,
*,
- expert_id=None,
known_fact_uuids=None,
lock_handle=None,
):
@@ -754,11 +755,10 @@ async def test_sync_path_filters_hallucinated_demotion_before_cap(mocker):
captured: dict[str, DreamOperations] = {}
async def fake_apply(
- user_id,
+ scope,
pass_id,
ops,
*,
- expert_id=None,
known_fact_uuids=None,
lock_handle=None,
):
@@ -853,7 +853,7 @@ async def test_lock_held_returns_skipped_lock_held(mocker):
@asynccontextmanager
async def busy_lock(*args, **kwargs):
- raise DreamLockHeld(args[0] if args else "?")
+ raise DreamLockHeld(args[0].owner_user_id if args else "?")
yield # pragma: no cover
mocker.patch.object(orchestrator_mod, "dream_lock", busy_lock)
diff --git a/autogpt_platform/backend/backend/copilot/dream/ratification.py b/autogpt_platform/backend/backend/copilot/dream/ratification.py
index 691a70be6d..653dfe5bfc 100644
--- a/autogpt_platform/backend/backend/copilot/dream/ratification.py
+++ b/autogpt_platform/backend/backend/copilot/dream/ratification.py
@@ -23,13 +23,11 @@ from typing import Any
from pydantic import BaseModel, Field
-from backend.copilot.graphiti.client import derive_memory_group_id
-from backend.copilot.graphiti.config import graphiti_config
-from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver
+from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver, open_driver
+from backend.copilot.graphiti.scope import HIT_TRACKER_KEY_PREFIX, MemoryScope
from backend.copilot.tools.graphiti_forget import mark_edges_superseded
from .ratification_hits import (
- HIT_TRACKER_KEY_PREFIX,
RATIFICATION_GRACE_PERIOD,
get_hit_count,
parse_created_at,
@@ -85,7 +83,7 @@ async def run_ratification_pass(
result = RatificationResult(user_id=user_id, started_at=started_at)
try:
- group_id = derive_memory_group_id(user_id, expert_id)
+ scope = MemoryScope.build(user_id, expert_id)
except ValueError as exc:
result.error = f"invalid_user_id: {exc}"
result.completed_at = datetime.now(timezone.utc)
@@ -94,15 +92,7 @@ async def run_ratification_pass(
)
return result
- driver = AutoGPTFalkorDriver(
- host=graphiti_config.falkordb_host,
- port=graphiti_config.falkordb_port,
- password=graphiti_config.falkordb_password or None,
- database=group_id,
- # Indices live with the chat-write client; skip the
- # background-task race that produces "Buffer is closed" spam.
- build_indices=False,
- )
+ driver = open_driver(scope)
try:
try:
tentatives = await _list_tentative_edges(driver)
@@ -129,9 +119,7 @@ async def run_ratification_pass(
for edge in tentatives:
try:
await _process_edge(
- user_id=user_id,
- expert_id=expert_id,
- group_id=group_id,
+ scope=scope,
driver=driver,
edge=edge,
now=now,
@@ -171,9 +159,7 @@ async def run_ratification_pass(
async def _process_edge(
*,
- user_id: str,
- expert_id: str | None,
- group_id: str,
+ scope: MemoryScope,
driver: AutoGPTFalkorDriver,
edge: dict[str, Any],
now: datetime,
@@ -191,7 +177,7 @@ async def _process_edge(
result.per_edge_errors.append("missing_uuid")
return
- hits = await _get_hit_count(user_id, edge_uuid, expert_id)
+ hits = await _get_hit_count(scope, edge_uuid)
if hits >= 1:
promoted = await _promote_edge(driver, edge_uuid)
@@ -214,8 +200,8 @@ async def _process_edge(
[edge_uuid],
reason="unratified",
new_status="superseded",
- user_id=user_id,
- group_id=group_id,
+ user_id=scope.owner_user_id,
+ group_id=scope.group_id,
)
if succeeded:
result.superseded_count += 1
@@ -264,19 +250,14 @@ async def _promote_edge(driver: AutoGPTFalkorDriver, edge_uuid: str) -> bool:
return bool(records)
-async def try_ratify_on_hit(
- user_id: str,
- edge_uuids: list[str],
- *,
- expert_id: str | None = None,
-) -> int:
+async def try_ratify_on_hit(scope: MemoryScope, edge_uuids: list[str]) -> int:
"""Record warm-context hits and promote any tentative edges inline.
Called from warm-context retrieval (``graphiti/context.py``) once
per turn with the list of edge uuids that landed in the user's
context. For each uuid we:
- 1. Bump the ``mem:hits:{user_id}:{edge_uuid}`` Redis counter
+ 1. Bump the ``mem:hits:{scope_key}:{edge_uuid}`` Redis counter
(so the nightly ratification sweep also sees the hit and
agrees on promotion if Cypher fails here).
2. Issue a targeted Cypher ``SET status='active'`` filtered by
@@ -296,35 +277,20 @@ async def try_ratify_on_hit(
"""
if not edge_uuids:
return 0
- if not user_id:
- return 0
+ user_id = scope.owner_user_id
# Step 1: bump hit counters (Redis, best-effort, swallows errors).
# Done before the Cypher promotion so the counter survives even
# when the promotion path fails.
for uuid in edge_uuids:
- await record_memory_hit(user_id, uuid, expert_id)
+ await record_memory_hit(scope, uuid)
# Step 2: targeted Cypher promotion. We open our own driver here
# because callers are warm-context retrieval call sites that have
# a higher-level graphiti client but no raw driver — and we want
# the brief write-lock semantics to be local to this function.
- try:
- group_id = derive_memory_group_id(user_id, expert_id)
- except ValueError as exc:
- logger.debug("try_ratify_on_hit: invalid user_id %s: %s", user_id[:12], exc)
- return 0
-
promoted_count = 0
- driver = AutoGPTFalkorDriver(
- host=graphiti_config.falkordb_host,
- port=graphiti_config.falkordb_port,
- password=graphiti_config.falkordb_password or None,
- database=group_id,
- # Indices live with the chat-write client; skip the
- # background-task race that produces "Buffer is closed" spam.
- build_indices=False,
- )
+ driver = open_driver(scope)
try:
for uuid in edge_uuids:
try:
@@ -374,7 +340,5 @@ async def _promote_if_tentative(driver: AutoGPTFalkorDriver, edge_uuid: str) ->
# Local indirection so tests can mock ``_get_hit_count`` on this module
# rather than the helper module (matches the poison-pill test pattern).
-async def _get_hit_count(
- user_id: str, edge_uuid: str, expert_id: str | None = None
-) -> int:
- return await get_hit_count(user_id, edge_uuid, expert_id)
+async def _get_hit_count(scope: MemoryScope, edge_uuid: str) -> int:
+ return await get_hit_count(scope, edge_uuid)
diff --git a/autogpt_platform/backend/backend/copilot/dream/ratification_hits.py b/autogpt_platform/backend/backend/copilot/dream/ratification_hits.py
index 9e27680150..0eb6c22c14 100644
--- a/autogpt_platform/backend/backend/copilot/dream/ratification_hits.py
+++ b/autogpt_platform/backend/backend/copilot/dream/ratification_hits.py
@@ -1,8 +1,8 @@
"""Redis hit tracker for tentative MemoryFact edges.
-Owns the ``mem:hits:{user_id}:{edge_uuid}`` key shape and the
-INCR/read paths the ratification pass consults. Kept in its own
-module so ``ratification.py`` stays focused on the pass logic and
+Owns the INCR/read paths the ratification pass consults on the
+per-scope hit counters (``MemoryScope.redis_key("hits")``). Kept in its
+own module so ``ratification.py`` stays focused on the pass logic and
fits the file-length budget.
Wiring note: warm-context retrieval (``graphiti/context.py``) fires
@@ -19,7 +19,7 @@ import logging
from datetime import datetime, timedelta, timezone
from typing import Any
-from backend.copilot.graphiti.client import derive_memory_scope_key
+from backend.copilot.graphiti.scope import MemoryScope
logger = logging.getLogger(__name__)
@@ -28,15 +28,8 @@ logger = logging.getLogger(__name__)
# get several sessions worth of opportunities to retrieve the memory.
RATIFICATION_GRACE_PERIOD = timedelta(days=30)
-# Redis key prefix for warm-context hit tracking. One key per
-# (user, edge) so we can INCR without contention and let TTL clean up
-# automatically. Format: ``mem:hits:{user_id}:{edge_uuid}``.
-HIT_TRACKER_KEY_PREFIX = "mem:hits"
-
-async def record_memory_hit(
- user_id: str, edge_uuid: str, expert_id: str | None = None
-) -> None:
+async def record_memory_hit(scope: MemoryScope, edge_uuid: str) -> None:
"""INCR the Redis hit counter for one tentative edge.
Called from any code path that surfaces a tentative edge to the
@@ -53,7 +46,7 @@ async def record_memory_hit(
from backend.data.redis_client import get_redis_async
redis = await get_redis_async()
- key = hit_key(user_id, edge_uuid, expert_id)
+ key = scope.redis_key("hits", edge_uuid=edge_uuid)
ttl_seconds = int(RATIFICATION_GRACE_PERIOD.total_seconds())
# SET with NX + EX seeds the key with TTL, then INCR (which
# never touches TTL), then EXPIRE so every hit refreshes the
@@ -65,21 +58,19 @@ async def record_memory_hit(
except Exception:
logger.debug(
"record_memory_hit failed for user %s edge %s",
- user_id[:12],
+ scope.owner_user_id[:12],
edge_uuid,
exc_info=True,
)
-async def get_hit_count(
- user_id: str, edge_uuid: str, expert_id: str | None = None
-) -> int:
+async def get_hit_count(scope: MemoryScope, edge_uuid: str) -> int:
"""Read the Redis hit counter for one edge. Missing key → 0."""
try:
from backend.data.redis_client import get_redis_async
redis = await get_redis_async()
- raw = await redis.get(hit_key(user_id, edge_uuid, expert_id))
+ raw = await redis.get(scope.redis_key("hits", edge_uuid=edge_uuid))
if raw is None:
return 0
if isinstance(raw, bytes):
@@ -90,18 +81,13 @@ async def get_hit_count(
# until the grace period elapses; we never falsely promote.
logger.debug(
"hit-count read failed for user %s edge %s",
- user_id[:12],
+ scope.owner_user_id[:12],
edge_uuid,
exc_info=True,
)
return 0
-def hit_key(user_id: str, edge_uuid: str, expert_id: str | None = None) -> str:
- scope_id = derive_memory_scope_key(user_id, expert_id)
- return f"{HIT_TRACKER_KEY_PREFIX}:{scope_id}:{edge_uuid}"
-
-
def parse_created_at(value: Any) -> datetime | None:
"""Best-effort coerce FalkorDB's ``created_at`` into an aware datetime.
diff --git a/autogpt_platform/backend/backend/copilot/dream/ratification_test.py b/autogpt_platform/backend/backend/copilot/dream/ratification_test.py
index da124c348c..95cd82a30a 100644
--- a/autogpt_platform/backend/backend/copilot/dream/ratification_test.py
+++ b/autogpt_platform/backend/backend/copilot/dream/ratification_test.py
@@ -3,7 +3,7 @@ boundary, verify each tentative-edge outcome produces the right
Cypher / mark_edges_superseded call and the right counters.
These tests do NOT touch FalkorDB or a live Redis. ratification.py
-opens an ``AutoGPTFalkorDriver`` for its own Cypher and calls
+opens a driver via ``open_driver`` for its own Cypher and calls
``mark_edges_superseded`` for demotions; both are patched at the
module surface so the dispatch logic can be exercised in isolation.
"""
@@ -15,13 +15,15 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
+from backend.copilot.graphiti.scope import MemoryScope
+
from . import ratification as ratification_mod
from .ratification import (
RATIFICATION_GRACE_PERIOD,
RatificationResult,
run_ratification_pass,
)
-from .ratification_hits import hit_key, record_memory_hit
+from .ratification_hits import record_memory_hit
def _make_driver(records_for_list: list[dict], records_for_promote=None):
@@ -59,7 +61,7 @@ def _patch_driver_constructor(mocker):
driver = _make_driver(records_for_list=[])
mocker.patch.object(
ratification_mod,
- "AutoGPTFalkorDriver",
+ "open_driver",
MagicMock(return_value=driver),
)
return driver
@@ -89,7 +91,7 @@ def fake_redis(mocker):
async def get(self, key: str):
self.get_calls.append(key)
- # Keys are ``mem:hits:{user_id}:{edge_uuid}`` — split off the uuid.
+ # Keys are ``mem:hits:{scope_key}:{edge_uuid}`` — split off the uuid.
edge_uuid = key.rsplit(":", 1)[-1]
value = self.hits.get(edge_uuid, 0)
return str(value).encode() if value else None
@@ -121,9 +123,7 @@ async def test_tentative_edge_with_hits_is_ratified_to_active(
"""Hit count >= 1 → flip to active via the promote Cypher; no demotion."""
edge = {"uuid": "edge-hot", "created_at": _hours_ago(2)}
driver = _make_driver(records_for_list=[edge])
- mocker.patch.object(
- ratification_mod, "AutoGPTFalkorDriver", MagicMock(return_value=driver)
- )
+ mocker.patch.object(ratification_mod, "open_driver", MagicMock(return_value=driver))
fake_redis.hits["edge-hot"] = 3
result = await run_ratification_pass("u-ratified")
@@ -159,9 +159,7 @@ async def test_tentative_edge_without_hits_past_grace_is_superseded_as_unratifie
"created_at": _days_ago(RATIFICATION_GRACE_PERIOD.days + 2),
}
driver = _make_driver(records_for_list=[edge])
- mocker.patch.object(
- ratification_mod, "AutoGPTFalkorDriver", MagicMock(return_value=driver)
- )
+ mocker.patch.object(ratification_mod, "open_driver", MagicMock(return_value=driver))
result = await run_ratification_pass("u-stale")
@@ -184,9 +182,7 @@ async def test_supersede_failure_is_reported_not_silently_dropped(mocker, fake_r
"created_at": _days_ago(RATIFICATION_GRACE_PERIOD.days + 2),
}
driver = _make_driver(records_for_list=[edge])
- mocker.patch.object(
- ratification_mod, "AutoGPTFalkorDriver", MagicMock(return_value=driver)
- )
+ mocker.patch.object(ratification_mod, "open_driver", MagicMock(return_value=driver))
mocker.patch.object(
ratification_mod,
"mark_edges_superseded",
@@ -208,9 +204,7 @@ async def test_tentative_edge_within_grace_without_hits_is_untouched(
"""Zero hits but still inside the grace window → no promote, no demote."""
edge = {"uuid": "edge-young", "created_at": _hours_ago(6)}
driver = _make_driver(records_for_list=[edge])
- mocker.patch.object(
- ratification_mod, "AutoGPTFalkorDriver", MagicMock(return_value=driver)
- )
+ mocker.patch.object(ratification_mod, "open_driver", MagicMock(return_value=driver))
result = await run_ratification_pass("u-young")
@@ -237,17 +231,17 @@ async def test_expert_ratification_uses_expert_graph_and_scoped_hit_key(
}
driver = _make_driver(records_for_list=[edge])
driver_factory = MagicMock(return_value=driver)
- mocker.patch.object(ratification_mod, "AutoGPTFalkorDriver", driver_factory)
+ mocker.patch.object(ratification_mod, "open_driver", driver_factory)
result = await run_ratification_pass("user-1", expert_id="expert-1")
assert result.superseded_count == 1
- group_id = driver_factory.call_args.kwargs["database"]
+ scope = driver_factory.call_args.args[0]
+ assert scope == MemoryScope.for_expert("user-1", "expert-1")
+ group_id = scope.group_id
assert group_id.startswith("expert_")
assert stub_mark_superseded.call_args.kwargs["group_id"] == group_id
- assert hit_key("user-1", "edge-expert", "expert-1") == (
- f"mem:hits:{group_id}:edge-expert"
- )
+ assert fake_redis.get_calls == [f"mem:hits:{group_id}:edge-expert"]
def test_grace_period_is_thirty_days_per_spec():
@@ -270,9 +264,7 @@ async def test_unhit_edge_just_inside_grace_window_is_untouched(
)
edge = {"uuid": "edge-near-boundary", "created_at": created_at}
driver = _make_driver(records_for_list=[edge])
- mocker.patch.object(
- ratification_mod, "AutoGPTFalkorDriver", MagicMock(return_value=driver)
- )
+ mocker.patch.object(ratification_mod, "open_driver", MagicMock(return_value=driver))
result = await run_ratification_pass("u-near-boundary")
@@ -292,9 +284,7 @@ async def test_already_active_edges_are_not_in_scope_of_the_listing_query(
# edge would not show up in the list query; we model that by leaving
# the list empty here.
driver = _make_driver(records_for_list=[])
- mocker.patch.object(
- ratification_mod, "AutoGPTFalkorDriver", MagicMock(return_value=driver)
- )
+ mocker.patch.object(ratification_mod, "open_driver", MagicMock(return_value=driver))
result = await run_ratification_pass("u-only-active")
@@ -335,20 +325,16 @@ async def test_per_edge_failure_does_not_kill_the_rest_of_the_pass(
},
]
driver = _make_driver(records_for_list=edges)
- mocker.patch.object(
- ratification_mod, "AutoGPTFalkorDriver", MagicMock(return_value=driver)
- )
+ mocker.patch.object(ratification_mod, "open_driver", MagicMock(return_value=driver))
fake_redis.hits["edge-good-hot"] = 5
# edge-poison: any hit-count read for this edge raises so the
# per-edge try/except can catch it without breaking the others.
original_get_hit_count = ratification_mod._get_hit_count
- async def hit_count_with_poison(
- user_id: str, edge_uuid: str, expert_id: str | None = None
- ) -> int:
+ async def hit_count_with_poison(scope: MemoryScope, edge_uuid: str) -> int:
if edge_uuid == "edge-poison":
raise RuntimeError("simulated redis explosion")
- return await original_get_hit_count(user_id, edge_uuid, expert_id)
+ return await original_get_hit_count(scope, edge_uuid)
mocker.patch.object(
ratification_mod, "_get_hit_count", side_effect=hit_count_with_poison
@@ -363,9 +349,10 @@ async def test_per_edge_failure_does_not_kill_the_rest_of_the_pass(
assert result.superseded_count == 1
assert len(result.per_edge_errors) == 1
assert "edge-poison" in result.per_edge_errors[0]
+ scope = MemoryScope.build("u-mixed", expert_id)
assert fake_redis.get_calls == [
- hit_key("u-mixed", "edge-good-hot", expert_id),
- hit_key("u-mixed", "edge-good-stale", expert_id),
+ scope.redis_key("hits", edge_uuid="edge-good-hot"),
+ scope.redis_key("hits", edge_uuid="edge-good-stale"),
]
@@ -381,12 +368,13 @@ async def test_hit_refreshes_ttl_to_full_grace_period(fake_redis):
by the initial SET NX EX runs out mid-window and the nightly sweep
falsely reads zero hits for an edge that was being used."""
grace_seconds = int(RATIFICATION_GRACE_PERIOD.total_seconds())
+ scope = MemoryScope.for_user("u-ttl")
- await record_memory_hit("u-ttl", "edge-busy")
- await record_memory_hit("u-ttl", "edge-busy")
- await record_memory_hit("u-ttl", "edge-busy")
+ await record_memory_hit(scope, "edge-busy")
+ await record_memory_hit(scope, "edge-busy")
+ await record_memory_hit(scope, "edge-busy")
- key = hit_key("u-ttl", "edge-busy")
+ key = scope.redis_key("hits", edge_uuid="edge-busy")
assert fake_redis.expire_calls == [(key, grace_seconds)] * 3
assert fake_redis.hits["edge-busy"] == 3
@@ -401,7 +389,7 @@ async def test_record_memory_hit_swallows_ttl_refresh_failure(fake_redis):
fake_redis.expire = exploding_expire
- await record_memory_hit("u-ttl", "edge-busy")
+ await record_memory_hit(MemoryScope.for_user("u-ttl"), "edge-busy")
assert fake_redis.hits["edge-busy"] == 1
@@ -419,9 +407,9 @@ async def test_try_ratify_on_hit_empty_edge_list_returns_zero_without_redis_or_c
record_spy = mocker.patch.object(
ratification_mod, "record_memory_hit", new=AsyncMock()
)
- driver_spy = mocker.patch.object(ratification_mod, "AutoGPTFalkorDriver")
+ driver_spy = mocker.patch.object(ratification_mod, "open_driver")
- promoted = await ratification_mod.try_ratify_on_hit("u1", [])
+ promoted = await ratification_mod.try_ratify_on_hit(MemoryScope.for_user("u1"), [])
assert promoted == 0
record_spy.assert_not_called()
@@ -437,9 +425,11 @@ async def test_try_ratify_on_hit_records_hits_for_every_retrieved_edge(mocker):
mocker.patch.object(ratification_mod, "record_memory_hit", new=record_spy)
# Driver returns no promotions (everything already-active).
driver = _make_driver(records_for_list=[], records_for_promote=[])
- mocker.patch.object(ratification_mod, "AutoGPTFalkorDriver", return_value=driver)
+ mocker.patch.object(ratification_mod, "open_driver", return_value=driver)
- await ratification_mod.try_ratify_on_hit("u1", ["edge-a", "edge-b", "edge-c"])
+ await ratification_mod.try_ratify_on_hit(
+ MemoryScope.for_user("u1"), ["edge-a", "edge-b", "edge-c"]
+ )
assert record_spy.await_count == 3
called_uuids = [c.args[1] for c in record_spy.await_args_list]
@@ -471,10 +461,10 @@ async def test_try_ratify_on_hit_returns_count_of_actually_promoted_edges(mocker
return (list(next(promote_results)), None, None)
driver.execute_query = AsyncMock(side_effect=fake_execute)
- mocker.patch.object(ratification_mod, "AutoGPTFalkorDriver", return_value=driver)
+ mocker.patch.object(ratification_mod, "open_driver", return_value=driver)
promoted = await ratification_mod.try_ratify_on_hit(
- "u1", ["edge-1", "edge-2", "edge-3"]
+ MemoryScope.for_user("u1"), ["edge-1", "edge-2", "edge-3"]
)
assert promoted == 2
@@ -497,10 +487,10 @@ async def test_try_ratify_on_hit_swallows_per_edge_cypher_failures(mocker):
driver = MagicMock()
driver.close = AsyncMock(return_value=None)
driver.execute_query = AsyncMock(side_effect=fake_execute)
- mocker.patch.object(ratification_mod, "AutoGPTFalkorDriver", return_value=driver)
+ mocker.patch.object(ratification_mod, "open_driver", return_value=driver)
promoted = await ratification_mod.try_ratify_on_hit(
- "u1", ["edge-a", "edge-poison", "edge-c"]
+ MemoryScope.for_user("u1"), ["edge-a", "edge-poison", "edge-c"]
)
# The poison edge errored; the others promoted.
@@ -509,15 +499,22 @@ async def test_try_ratify_on_hit_swallows_per_edge_cypher_failures(mocker):
@pytest.mark.asyncio
-async def test_try_ratify_on_hit_empty_user_id_is_noop(mocker):
- record_spy = mocker.patch.object(
- ratification_mod, "record_memory_hit", new=AsyncMock()
+async def test_try_ratify_on_hit_stays_in_the_given_scope(mocker):
+ """The hit counter and the promotion driver both use the scope the
+ caller built, so an expert's retrieval never touches the account's
+ counters or graph."""
+ record_spy = AsyncMock()
+ mocker.patch.object(ratification_mod, "record_memory_hit", new=record_spy)
+ driver = _make_driver(records_for_list=[], records_for_promote=[])
+ open_driver = mocker.patch.object(
+ ratification_mod, "open_driver", return_value=driver
)
- driver_spy = mocker.patch.object(ratification_mod, "AutoGPTFalkorDriver")
- promoted = await ratification_mod.try_ratify_on_hit("", ["edge-a"])
- assert promoted == 0
- record_spy.assert_not_called()
- driver_spy.assert_not_called()
+ scope = MemoryScope.for_expert("u1", "expert-1")
+
+ await ratification_mod.try_ratify_on_hit(scope, ["edge-a"])
+
+ record_spy.assert_awaited_once_with(scope, "edge-a")
+ open_driver.assert_called_once_with(scope)
# ---------------------------------------------------------------------------
diff --git a/autogpt_platform/backend/backend/copilot/dream/scheduling.py b/autogpt_platform/backend/backend/copilot/dream/scheduling.py
index 91ca93bb63..2d3daf11ca 100644
--- a/autogpt_platform/backend/backend/copilot/dream/scheduling.py
+++ b/autogpt_platform/backend/backend/copilot/dream/scheduling.py
@@ -55,6 +55,7 @@ from typing import Any, Awaitable, Callable
from pydantic.dataclasses import dataclass
+from backend.copilot.graphiti.scope import MemoryScope
from backend.util.feature_flag import Flag, is_feature_enabled
logger = logging.getLogger(__name__)
@@ -231,6 +232,14 @@ async def _resolve_user_timezone(user_id: str) -> str | None:
return None
+def _registration_key(user_id: str, key_prefix: str) -> str:
+ # Keyed on the owning user whichever memory scope triggered the write:
+ # the dream-system crons are registered per user.
+ return MemoryScope.for_user(user_id).redis_key(
+ "registration", registration_prefix=key_prefix
+ )
+
+
async def _read_registration_tz(user_id: str, key_prefix: str) -> str | None:
"""Read the timezone the cron was last registered with.
@@ -244,7 +253,7 @@ async def _read_registration_tz(user_id: str, key_prefix: str) -> str | None:
from backend.data.redis_client import get_redis_async
redis = await get_redis_async()
- key = f"{key_prefix}:{user_id}"
+ key = _registration_key(user_id, key_prefix)
stored = await redis.get(key)
if stored is None:
return None
@@ -274,7 +283,7 @@ async def _write_registration_tz(
from backend.data.redis_client import get_redis_async
redis = await get_redis_async()
- key = f"{key_prefix}:{user_id}"
+ key = _registration_key(user_id, key_prefix)
await redis.set(key, current_tz, ex=REGISTRATION_TTL_SECONDS)
except Exception:
logger.debug(
@@ -300,7 +309,7 @@ async def clear_registration_marker(user_id: str, key_prefix: str) -> None:
from backend.data.redis_client import get_redis_async
redis = await get_redis_async()
- await redis.delete(f"{key_prefix}:{user_id}")
+ await redis.delete(_registration_key(user_id, key_prefix))
except Exception:
logger.warning(
"Redis delete failed for %s:%s; marker will expire via TTL",
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/AGENTS.md b/autogpt_platform/backend/backend/copilot/graphiti/AGENTS.md
index d49f0529dc..6a0b3ef1cc 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/AGENTS.md
+++ b/autogpt_platform/backend/backend/copilot/graphiti/AGENTS.md
@@ -17,6 +17,7 @@ Runtime prompt instructions live in `prompting.py:get_graphiti_supplement()`.
## Design Intent
- Preserve per-user isolation through `group_id`-scoped databases and clients.
+- 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.
@@ -24,12 +25,13 @@ Runtime prompt instructions live in `prompting.py:get_graphiti_supplement()`.
Run everything from `autogpt_platform/backend` and use `poetry run ...`.
-Get the `group_id` for a user:
+Get the `group_id` for a user (use `MemoryScope.for_expert(user_id, expert_id)`
+for one of their experts; the snippets below take either):
```bash
poetry run python - <<'PY'
-from backend.copilot.graphiti.client import derive_group_id
-print(derive_group_id("883cc9da-fe37-4863-839b-acba022bf3ef"))
+from backend.copilot.graphiti.scope import MemoryScope
+print(MemoryScope.for_user("883cc9da-fe37-4863-839b-acba022bf3ef").group_id)
PY
```
@@ -38,12 +40,11 @@ Inspect graph counts:
```bash
poetry run python - <<'PY'
import asyncio
-from backend.copilot.graphiti.client import derive_group_id
-from backend.copilot.graphiti.config import graphiti_config
-from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver
+from backend.copilot.graphiti.falkordb_driver import open_driver
+from backend.copilot.graphiti.scope import MemoryScope
USER_ID = "883cc9da-fe37-4863-839b-acba022bf3ef"
-GROUP_ID = derive_group_id(USER_ID)
+SCOPE = MemoryScope.for_user(USER_ID)
QUERIES = {
"entities": "MATCH (n:Entity) RETURN count(n) AS count",
@@ -53,12 +54,7 @@ QUERIES = {
}
async def run():
- driver = AutoGPTFalkorDriver(
- host=graphiti_config.falkordb_host,
- port=graphiti_config.falkordb_port,
- password=graphiti_config.falkordb_password or None,
- database=GROUP_ID,
- )
+ driver = open_driver(SCOPE)
try:
for name, query in QUERIES.items():
records, _, _ = await driver.execute_query(query)
@@ -75,20 +71,14 @@ List entities or relation-name counts:
```bash
poetry run python - <<'PY'
import asyncio
-from backend.copilot.graphiti.client import derive_group_id
-from backend.copilot.graphiti.config import graphiti_config
-from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver
+from backend.copilot.graphiti.falkordb_driver import open_driver
+from backend.copilot.graphiti.scope import MemoryScope
USER_ID = "883cc9da-fe37-4863-839b-acba022bf3ef"
-GROUP_ID = derive_group_id(USER_ID)
+SCOPE = MemoryScope.for_user(USER_ID)
async def run():
- driver = AutoGPTFalkorDriver(
- host=graphiti_config.falkordb_host,
- port=graphiti_config.falkordb_port,
- password=graphiti_config.falkordb_password or None,
- database=GROUP_ID,
- )
+ driver = open_driver(SCOPE)
try:
records, _, _ = await driver.execute_query(
"MATCH (n:Entity) RETURN n.name AS name, n.summary AS summary ORDER BY n.name"
@@ -119,21 +109,15 @@ Inspect facts around one node:
```bash
poetry run python - <<'PY'
import asyncio
-from backend.copilot.graphiti.client import derive_group_id
-from backend.copilot.graphiti.config import graphiti_config
-from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver
+from backend.copilot.graphiti.falkordb_driver import open_driver
+from backend.copilot.graphiti.scope import MemoryScope
USER_ID = "883cc9da-fe37-4863-839b-acba022bf3ef"
-GROUP_ID = derive_group_id(USER_ID)
+SCOPE = MemoryScope.for_user(USER_ID)
TARGET = "sarah"
async def run():
- driver = AutoGPTFalkorDriver(
- host=graphiti_config.falkordb_host,
- port=graphiti_config.falkordb_port,
- password=graphiti_config.falkordb_password or None,
- database=GROUP_ID,
- )
+ driver = open_driver(SCOPE)
try:
records, _, _ = await driver.execute_query(
"""
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/communities.py b/autogpt_platform/backend/backend/copilot/graphiti/communities.py
index b85ba22211..f233c46f5a 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/communities.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/communities.py
@@ -39,11 +39,11 @@ from backend.data.redis_client import get_redis_async
from .client import (
close_graphiti_client,
- derive_memory_group_id,
get_graphiti_client,
make_flex_graphiti_client,
)
from .config import graphiti_config
+from .scope import MemoryScope
logger = logging.getLogger(__name__)
@@ -240,8 +240,7 @@ async def _activity_since_last_rebuild(
# (``SCHEDULER_DREAM_OPERATION_TIMEOUT_SECONDS`` = 1800s; every caller wraps
# the rebuild in ``run_async(..., timeout=...)``), so the lock cannot expire
# mid-rebuild and no lease-renewal watchdog is needed — the TTL is purely a
-# crash backstop.
-_REBUILD_LOCK_KEY_PREFIX = "graphiti:community_rebuild_lock:"
+# crash backstop. The key is ``MemoryScope.redis_key("rebuild_lock")``.
_REBUILD_LOCK_TTL_SECONDS = 1800 + 120
_REBUILD_UNLOCK_SCRIPT = (
@@ -250,10 +249,6 @@ _REBUILD_UNLOCK_SCRIPT = (
)
-def _rebuild_lock_key(group_id: str) -> str:
- return f"{_REBUILD_LOCK_KEY_PREFIX}{group_id}"
-
-
async def _release_rebuild_lock(redis, key: str, token: str) -> None:
"""Compare-and-delete: drop the lock only if we still hold this token.
@@ -306,11 +301,12 @@ async def rebuild_communities_for_user(
}
try:
- group_id = derive_memory_group_id(user_id, expert_id)
+ scope = MemoryScope.build(user_id, expert_id)
except ValueError as exc:
result["error"] = f"invalid_user_id: {exc}"
logger.warning("Skipping community rebuild — invalid user_id %s", user_id[:12])
return result
+ group_id = scope.group_id
# When the flex flag is on we build a one-shot Graphiti client whose
# LLM calls run on OpenAI's flex tier (~50% discount, best-effort
@@ -334,7 +330,7 @@ async def rebuild_communities_for_user(
# concurrent rebuild short-circuits instead of racing the DETACH DELETE
# below. A contended acquire rides the existing ``skipped`` contract.
redis = await get_redis_async()
- lock_key = _rebuild_lock_key(group_id)
+ lock_key = scope.redis_key("rebuild_lock")
lock_token = uuid4().hex
if not await redis.set(lock_key, lock_token, nx=True, ex=_REBUILD_LOCK_TTL_SECONDS):
result["skipped"] = True
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/communities_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/communities_integration_test.py
index 10f347e792..5b2360b0f5 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/communities_integration_test.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/communities_integration_test.py
@@ -67,11 +67,10 @@ async def test_rebuild_returns_structured_result_on_failure(
)
user_id = _make_user_id(group_id)
- # derive_group_id will fail validation if we don't have the user_ prefix,
- # so we have to point the rebuild at the actual fixture group_id by
- # patching derive_group_id for this call only.
+ # The fixture's group_id has no ``user_`` prefix, so point the rebuild's
+ # MemoryScope at it by patching the group derivation for this call only.
with patch(
- "backend.copilot.graphiti.communities.derive_memory_group_id",
+ "backend.copilot.graphiti.scope.derive_memory_group_id",
return_value=group_id,
):
result = await rebuild_communities_for_user(user_id)
@@ -128,7 +127,7 @@ async def test_detach_delete_clears_orphan_community_nodes(
# and the orphans this test seeds would stay in place — which would
# mask the regression the test is supposed to catch.
with patch(
- "backend.copilot.graphiti.communities.derive_memory_group_id",
+ "backend.copilot.graphiti.scope.derive_memory_group_id",
return_value=group_id,
):
await rebuild_communities_for_user(user_id, force=True)
@@ -184,7 +183,7 @@ async def test_rebuild_does_not_touch_other_users_communities(
user_id = _make_user_id(group_id)
with patch(
- "backend.copilot.graphiti.communities.derive_memory_group_id",
+ "backend.copilot.graphiti.scope.derive_memory_group_id",
return_value=group_id,
):
await rebuild_communities_for_user(user_id)
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/communities_test.py b/autogpt_platform/backend/backend/copilot/graphiti/communities_test.py
index d8b66151ee..a5597a4355 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/communities_test.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/communities_test.py
@@ -13,6 +13,7 @@ from .communities import (
_summarize_communities,
rebuild_communities_for_user,
)
+from .scope import MemoryScope
@pytest.fixture(autouse=True)
@@ -241,10 +242,6 @@ class TestRebuildCommunitiesForUser:
make_client = AsyncMock(return_value=client)
with (
- patch(
- "backend.copilot.graphiti.communities.derive_memory_group_id",
- return_value="expert_resolved_group",
- ) as derive_group,
patch(
"backend.copilot.graphiti.communities.make_flex_graphiti_client",
make_client,
@@ -261,12 +258,12 @@ class TestRebuildCommunitiesForUser:
)
assert result["error"] is None
- derive_group.assert_called_once_with(
+ expert_group = MemoryScope.for_expert(
"883cc9da-fe37-4863-839b-acba022bf3ef", "expert-1"
- )
- make_client.assert_awaited_once_with("expert_resolved_group")
+ ).group_id
+ make_client.assert_awaited_once_with(expert_group)
lock_key = _free_rebuild_lock.set.call_args.args[0]
- assert lock_key == "graphiti:community_rebuild_lock:expert_resolved_group"
+ assert lock_key == f"graphiti:community_rebuild_lock:{expert_group}"
@pytest.mark.asyncio
async def test_failure_path_returns_error_in_result(self) -> None:
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/context.py b/autogpt_platform/backend/backend/copilot/graphiti/context.py
index 1df4643698..d098fb1fbe 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/context.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/context.py
@@ -11,8 +11,9 @@ from ._format import (
extract_fact,
extract_temporal_validity,
)
-from .client import derive_memory_group_id, get_graphiti_client
+from .client import get_graphiti_client
from .config import graphiti_config
+from .scope import MemoryScope
logger = logging.getLogger(__name__)
@@ -33,8 +34,9 @@ async def fetch_warm_context(
return None
try:
+ scope = MemoryScope.build(user_id, expert_id)
return await asyncio.wait_for(
- _fetch(user_id, message, expert_id),
+ _fetch(scope, message),
timeout=graphiti_config.context_timeout,
)
except asyncio.TimeoutError:
@@ -48,16 +50,14 @@ async def fetch_warm_context(
return None
-async def _fetch(
- user_id: str, message: str, expert_id: str | None = None
-) -> str | None:
+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 = derive_memory_group_id(user_id, expert_id)
+ group_id = scope.group_id
client = await get_graphiti_client(group_id)
# P-1.4: warm context is the single most-impactful retrieval per
@@ -91,7 +91,7 @@ async def _fetch(
# warm-context hit counter. Fire-and-forget so the chat turn
# never blocks on Redis or FalkorDB writes.
if edges:
- _spawn_ratification_hits(user_id, expert_id, edges)
+ _spawn_ratification_hits(scope, edges)
if not edges and not episodes:
return None
@@ -115,7 +115,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(user_id: str, expert_id: str | None, edges) -> None:
+def _spawn_ratification_hits(scope: MemoryScope, edges) -> None:
"""Fire-and-forget the ratification hit-hook for retrieved edges.
Imports lazily so the dream/ratification module isn't pulled into
@@ -130,8 +130,8 @@ def _spawn_ratification_hits(user_id: str, expert_id: str | None, edges) -> None
from backend.copilot.dream.ratification import try_ratify_on_hit
task = asyncio.create_task(
- try_ratify_on_hit(user_id, edge_uuids, expert_id=expert_id),
- name=f"ratify-hits-{user_id[:12]}",
+ try_ratify_on_hit(scope, edge_uuids),
+ name=f"ratify-hits-{scope.owner_user_id[:12]}",
)
_pending_hit_tasks.add(task)
task.add_done_callback(_on_hit_task_done)
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/context_test.py b/autogpt_platform/backend/backend/copilot/graphiti/context_test.py
index 48b73ded5b..fa02a6c832 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/context_test.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/context_test.py
@@ -11,6 +11,7 @@ from . 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 .scope import MemoryScope
class TestFetchWarmContextEmptyUserId:
@@ -25,7 +26,7 @@ class TestFetchWarmContextTimeout:
async def test_returns_none_on_timeout(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
- async def _slow_fetch(user_id: str, message: str, expert_id: str | None) -> str:
+ async def _slow_fetch(scope: MemoryScope, message: str) -> str:
await asyncio.sleep(10)
return "<temporal_context>data</temporal_context>"
@@ -40,18 +41,11 @@ class TestFetchWarmContextTimeout:
class TestFetchWarmContextGeneralError:
@pytest.mark.asyncio
async def test_returns_none_on_unexpected_error(self) -> None:
- with (
- patch.object(
- context,
- "derive_memory_group_id",
- return_value="user_abc",
- ),
- patch.object(
- context,
- "get_graphiti_client",
- new_callable=AsyncMock,
- side_effect=RuntimeError("connection lost"),
- ),
+ with patch.object(
+ context,
+ "get_graphiti_client",
+ new_callable=AsyncMock,
+ side_effect=RuntimeError("connection lost"),
):
result = await fetch_warm_context("abc", "hello")
@@ -84,16 +78,13 @@ class TestFetchInternal:
mock_client.search_.return_value = _search_results([])
mock_client.retrieve_episodes.return_value = []
- with (
- patch.object(context, "derive_memory_group_id", return_value="user_abc"),
- patch.object(
- context,
- "get_graphiti_client",
- new_callable=AsyncMock,
- return_value=mock_client,
- ),
+ with patch.object(
+ context,
+ "get_graphiti_client",
+ new_callable=AsyncMock,
+ return_value=mock_client,
):
- result = await context._fetch("test-user", "hello")
+ result = await context._fetch(MemoryScope.for_user("abc"), "hello")
assert result is None
@@ -103,28 +94,19 @@ class TestFetchInternal:
mock_client.search_.return_value = _search_results([])
mock_client.retrieve_episodes.return_value = []
- with (
- patch.object(
- context,
- "derive_memory_group_id",
- return_value="expert_private_group",
- ) as derive_mock,
- patch.object(
- context,
- "get_graphiti_client",
- new_callable=AsyncMock,
- return_value=mock_client,
- ) as get_client_mock,
- ):
- await context._fetch("user-1", "hello", "expert-1")
-
- derive_mock.assert_called_once_with("user-1", "expert-1")
- get_client_mock.assert_awaited_once_with("expert_private_group")
- assert mock_client.search_.await_args.kwargs["group_ids"] == [
- "expert_private_group"
- ]
+ with patch.object(
+ context,
+ "get_graphiti_client",
+ new_callable=AsyncMock,
+ return_value=mock_client,
+ ) as get_client_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_private_group"
+ expert_group
]
@pytest.mark.asyncio
@@ -139,16 +121,13 @@ class TestFetchInternal:
mock_client.search_.return_value = _search_results([edge])
mock_client.retrieve_episodes.return_value = []
- with (
- patch.object(context, "derive_memory_group_id", return_value="user_abc"),
- patch.object(
- context,
- "get_graphiti_client",
- new_callable=AsyncMock,
- return_value=mock_client,
- ),
+ with patch.object(
+ context,
+ "get_graphiti_client",
+ new_callable=AsyncMock,
+ return_value=mock_client,
):
- result = await context._fetch("test-user", "hello")
+ result = await context._fetch(MemoryScope.for_user("abc"), "hello")
assert result is not None
assert "<temporal_context>" in result
@@ -164,16 +143,13 @@ class TestFetchInternal:
mock_client.search_.return_value = _search_results([])
mock_client.retrieve_episodes.return_value = [ep]
- with (
- patch.object(context, "derive_memory_group_id", return_value="user_abc"),
- patch.object(
- context,
- "get_graphiti_client",
- new_callable=AsyncMock,
- return_value=mock_client,
- ),
+ with patch.object(
+ context,
+ "get_graphiti_client",
+ new_callable=AsyncMock,
+ return_value=mock_client,
):
- result = await context._fetch("test-user", "hello")
+ result = await context._fetch(MemoryScope.for_user("abc"), "hello")
assert result is not None
assert "talked about coffee" in result
@@ -190,16 +166,13 @@ class TestFetchInternal:
mock_client.search_.return_value = _search_results([])
mock_client.retrieve_episodes.return_value = []
- with (
- patch.object(context, "derive_memory_group_id", return_value="user_abc"),
- patch.object(
- context,
- "get_graphiti_client",
- new_callable=AsyncMock,
- return_value=mock_client,
- ),
+ with patch.object(
+ context,
+ "get_graphiti_client",
+ new_callable=AsyncMock,
+ return_value=mock_client,
):
- await context._fetch("test-user", "hello world")
+ await context._fetch(MemoryScope.for_user("abc"), "hello world")
mock_client.search_.assert_awaited_once()
kwargs = mock_client.search_.await_args.kwargs
@@ -368,7 +341,7 @@ class TestRatificationHitHookFiresFireAndForget:
return AsyncMock()
monkeypatch.setattr(context.asyncio, "create_task", fake_create_task)
- context._spawn_ratification_hits("user-abc", None, edges=[])
+ context._spawn_ratification_hits(MemoryScope.for_user("user-abc"), edges=[])
assert created_tasks == []
def test_spawn_helper_creates_task_with_retrieved_uuids(
@@ -377,15 +350,10 @@ class TestRatificationHitHookFiresFireAndForget:
"""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."""
- captured_calls: list[tuple[str, list[str], str | None]] = []
+ captured_calls: list[tuple[MemoryScope, list[str]]] = []
- async def fake_try_ratify(
- user_id: str,
- edge_uuids: list[str],
- *,
- expert_id: str | None = None,
- ):
- captured_calls.append((user_id, edge_uuids, expert_id))
+ async def fake_try_ratify(scope: MemoryScope, edge_uuids: list[str]):
+ captured_calls.append((scope, edge_uuids))
from backend.copilot.dream import ratification as ratification_mod
@@ -400,16 +368,16 @@ class TestRatificationHitHookFiresFireAndForget:
SimpleNamespace(uuid=None), # filtered
SimpleNamespace(), # no uuid attr at all → filtered
]
- context._spawn_ratification_hits("user-xyz", "expert-1", edges=edges)
+ context._spawn_ratification_hits(
+ MemoryScope.for_expert("user-xyz", "expert-1"), edges=edges
+ )
# Yield once so the spawned task runs.
await asyncio.sleep(0)
asyncio.run(driver())
- assert len(captured_calls) == 1
- user_id, uuids, expert_id = captured_calls[0]
- assert user_id == "user-xyz"
- assert uuids == ["edge-a", "edge-b"]
- assert expert_id == "expert-1"
+ assert captured_calls == [
+ (MemoryScope.for_expert("user-xyz", "expert-1"), ["edge-a", "edge-b"])
+ ]
class TestRatificationHitTaskRetention:
@@ -427,17 +395,12 @@ class TestRatificationHitTaskRetention:
context._pending_hit_tasks.clear()
release = asyncio.Event()
- async def fake_try_ratify(
- user_id: str,
- edge_uuids: list[str],
- *,
- expert_id: str | None = None,
- ):
+ async def fake_try_ratify(scope: MemoryScope, edge_uuids: list[str]):
await release.wait()
monkeypatch.setattr(ratification_mod, "try_ratify_on_hit", fake_try_ratify)
context._spawn_ratification_hits(
- "user-xyz", None, edges=[SimpleNamespace(uuid="edge-a")]
+ MemoryScope.for_user("user-xyz"), edges=[SimpleNamespace(uuid="edge-a")]
)
# Strong ref held while the task is in flight.
assert len(context._pending_hit_tasks) == 1
@@ -461,17 +424,12 @@ class TestRatificationHitTaskRetention:
async def driver():
context._pending_hit_tasks.clear()
- async def fake_try_ratify(
- user_id: str,
- edge_uuids: list[str],
- *,
- expert_id: str | None = None,
- ):
+ async def fake_try_ratify(scope: MemoryScope, edge_uuids: list[str]):
raise RuntimeError("falkordb down")
monkeypatch.setattr(ratification_mod, "try_ratify_on_hit", fake_try_ratify)
context._spawn_ratification_hits(
- "user-xyz", None, edges=[SimpleNamespace(uuid="edge-a")]
+ MemoryScope.for_user("user-xyz"), edges=[SimpleNamespace(uuid="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.py
index df8856b2a0..f69c7ffd7f 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
@@ -51,6 +51,7 @@ from . import ingest as ingest_mod
from .client import derive_group_id
from .config import graphiti_config
from .falkordb_driver import AutoGPTFalkorDriver
+from .scope import MemoryScope
PASS_ID = "e2e-pass"
SESSION_ID = "e2e-session"
@@ -254,7 +255,7 @@ async def _ingest_dream_proposal(user_id: str) -> None:
"""Write one dream proposal through the production path and await it."""
completion = ingest_mod.IngestionCompletion()
queued = await apply_mod._write_proposed_finding(
- user_id,
+ MemoryScope.for_user(user_id),
PASS_ID,
0,
PROPOSAL,
@@ -357,7 +358,7 @@ async def test_warm_context_hit_promotes_the_tentative_edge(dream_graph) -> None
await _ingest_dream_proposal(user_id)
edge_uuid = (await _sole_edge(driver))["uuid"]
- assert await try_ratify_on_hit(user_id, [edge_uuid]) == 1
+ assert await try_ratify_on_hit(MemoryScope.for_user(user_id), [edge_uuid]) == 1
edge = await _sole_edge(driver)
assert edge["status"] == "active"
@@ -375,7 +376,7 @@ async def test_warm_context_hit_promotes_the_tentative_edge(dream_graph) -> None
assert edge["confidence"] == pytest.approx(PROPOSAL.confidence)
assert (
- await try_ratify_on_hit(user_id, [edge_uuid]) == 0
+ await try_ratify_on_hit(MemoryScope.for_user(user_id), [edge_uuid]) == 0
), "the status='tentative' guard makes repeat hits no-ops"
@@ -392,7 +393,7 @@ async def test_sweep_promotes_a_tentative_edge_that_earned_a_hit(dream_graph) ->
driver, user_id = dream_graph
await _ingest_dream_proposal(user_id)
edge_uuid = (await _sole_edge(driver))["uuid"]
- await record_memory_hit(user_id, edge_uuid)
+ await record_memory_hit(MemoryScope.for_user(user_id), edge_uuid)
result = await run_ratification_pass(user_id)
@@ -416,10 +417,10 @@ async def test_repeated_hits_accumulate_in_the_counter(dream_graph) -> None:
await _ingest_dream_proposal(user_id)
edge_uuid = (await _sole_edge(driver))["uuid"]
- await record_memory_hit(user_id, edge_uuid)
- await record_memory_hit(user_id, edge_uuid)
+ await record_memory_hit(MemoryScope.for_user(user_id), edge_uuid)
+ await record_memory_hit(MemoryScope.for_user(user_id), edge_uuid)
- assert await get_hit_count(user_id, edge_uuid) == 2
+ assert await get_hit_count(MemoryScope.for_user(user_id), edge_uuid) == 2
@pytest.mark.integration
@@ -466,7 +467,7 @@ async def test_sweep_never_touches_an_already_active_edge(dream_graph) -> None:
edge_uuid = (await _sole_edge(driver))["uuid"]
# Promote it the production way: a real hit, then the sweep's promote leg.
- await record_memory_hit(user_id, edge_uuid)
+ await record_memory_hit(MemoryScope.for_user(user_id), edge_uuid)
assert (await run_ratification_pass(user_id)).ratified_count == 1
promoted = await _sole_edge(driver)
assert promoted["status"] == "active"
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/falkordb_driver.py b/autogpt_platform/backend/backend/copilot/graphiti/falkordb_driver.py
index 6ec86bf8b8..70fb6f2159 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/falkordb_driver.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/falkordb_driver.py
@@ -12,6 +12,7 @@ from graphiti_core.helpers import validate_group_ids
from graphiti_core.utils.datetime_utils import convert_datetimes_to_strings
from .config import graphiti_config
+from .scope import MemoryScope
logger = logging.getLogger(__name__)
@@ -344,3 +345,22 @@ class AutoGPTFalkorDriver(FalkorDriver):
return ""
return fulltext_query
+
+
+def open_driver(
+ scope: MemoryScope, *, build_indices: bool = False
+) -> AutoGPTFalkorDriver:
+ """Open a Cypher driver on ``scope``'s graph; the caller must close it.
+
+ The one place scope-level code constructs a driver. ``build_indices``
+ stays False unless the caller is about to write — see
+ ``AutoGPTFalkorDriver`` for why a bare construction must never create a
+ graph.
+ """
+ return AutoGPTFalkorDriver(
+ host=graphiti_config.falkordb_host,
+ port=graphiti_config.falkordb_port,
+ password=graphiti_config.falkordb_password or None,
+ database=scope.group_id,
+ build_indices=build_indices,
+ )
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/falkordb_driver_test.py b/autogpt_platform/backend/backend/copilot/graphiti/falkordb_driver_test.py
index 00ec8d1725..61ca918786 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/falkordb_driver_test.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/falkordb_driver_test.py
@@ -4,6 +4,7 @@ import pytest
from . import falkordb_driver as fdb
from .falkordb_driver import AutoGPTFalkorDriver
+from .scope import MemoryScope
class _FakeResult:
@@ -468,6 +469,18 @@ def test_clone_returns_subclass_with_indices_disabled() -> None:
assert driver.clone("user_a") is driver
+def test_open_driver_targets_the_scope_graph_without_building_indices() -> None:
+ """The factory opens the scope's own graph and keeps the default that a
+ bare construction never creates one."""
+ scope = MemoryScope.for_expert("user-1", "expert-1")
+ with patch.object(fdb, "AutoGPTFalkorDriver") as driver_cls:
+ assert fdb.open_driver(scope) is driver_cls.return_value
+
+ kwargs = driver_cls.call_args.kwargs
+ assert kwargs["database"] == scope.group_id
+ assert kwargs["build_indices"] is False
+
+
@pytest.mark.asyncio
async def test_unrelated_readonly_wording_does_not_trigger_write_fallback(
driver: AutoGPTFalkorDriver,
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest.py
index 805ca99cdf..5d636b83c9 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/ingest.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/ingest.py
@@ -12,8 +12,9 @@ from datetime import datetime, timezone
from graphiti_core.nodes import EpisodeType
-from .client import derive_memory_group_id, ensure_indices_once, get_graphiti_client
+from .client import ensure_indices_once, get_graphiti_client
from .memory_model import MemoryEnvelope, MemoryKind, MemoryStatus, SourceKind
+from .scope import MemoryScope
from .types import EDGE_TYPE_MAP, EDGE_TYPES, ENTITY_TYPES
logger = logging.getLogger(__name__)
@@ -385,7 +386,7 @@ async def enqueue_conversation_turn(
return
try:
- group_id = derive_memory_group_id(user_id, expert_id)
+ group_id = MemoryScope.build(user_id, expert_id).group_id
except ValueError:
logger.warning("Invalid memory scope for ingestion: %s", user_id[:12])
return
@@ -451,7 +452,7 @@ async def enqueue_conversation_turn(
async def enqueue_episode(
- user_id: str,
+ scope: MemoryScope,
session_id: str,
*,
name: str,
@@ -460,9 +461,8 @@ async def enqueue_episode(
is_json: bool = False,
edge_metadata: dict | None = None,
completion: IngestionCompletion | None = None,
- expert_id: str | None = None,
) -> bool:
- """Enqueue an arbitrary episode for background ingestion.
+ """Enqueue an arbitrary episode for background ingestion in ``scope``.
Used by ``MemoryStoreTool`` so that explicit memory-store calls go
through the same memory-group serialization queue as conversation turns.
@@ -489,14 +489,8 @@ async def enqueue_episode(
``True`` — a dropped episode is never enqueued and the worker never
completes it.
"""
- if not user_id:
- return False
-
- try:
- group_id = derive_memory_group_id(user_id, expert_id)
- except ValueError:
- logger.warning("Invalid memory scope for episode ingestion: %s", user_id[:12])
- return False
+ user_id = scope.owner_user_id
+ group_id = scope.group_id
body_bytes = len(episode_body.encode("utf-8"))
if body_bytes > MAX_EPISODE_BODY_BYTES:
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py
index 562e4b3895..28ad4e9e38 100644
--- a/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py
+++ b/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py
@@ -11,6 +11,7 @@ import pytest
from graphiti_core.nodes import EpisodeType
from . import ingest
+from .scope import MemoryScope
@pytest.fixture(autouse=True)
@@ -103,8 +104,8 @@ class TestIngestionWorkerExceptionHandling:
with (
patch.object(
- ingest,
- "derive_memory_group_id",
+ ingest.MemoryScope,
+ "build",
side_effect=AssertionError("worker must not re-derive memory scope"),
) as derive_mock,
patch.object(
@@ -143,14 +144,11 @@ class TestIngestionWorkerExceptionHandling:
}
)
- with (
- patch.object(ingest, "derive_memory_group_id", return_value="user_test"),
- patch.object(
- ingest,
- "get_graphiti_client",
- new_callable=AsyncMock,
- side_effect=RuntimeError("connection failed"),
- ),
+ with patch.object(
+ ingest,
+ "get_graphiti_client",
+ new_callable=AsyncMock,
+ side_effect=RuntimeError("connection failed"),
):
original_timeout = ingest._WORKER_IDLE_TIMEOUT
ingest._WORKER_IDLE_TIMEOUT = 0.05
@@ -278,14 +276,11 @@ class TestWaitForIngestion:
}
)
- with (
- patch.object(ingest, "derive_memory_group_id", return_value="user_scoped"),
- patch.object(
- ingest,
- "get_graphiti_client",
- new_callable=AsyncMock,
- return_value=mock_client,
- ),
+ with patch.object(
+ ingest,
+ "get_graphiti_client",
+ new_callable=AsyncMock,
+ return_value=mock_client,
):
worker = asyncio.create_task(
ingest._ingestion_worker(user_id, "user_scoped", queue)
@@ -327,11 +322,6 @@ class TestEnqueueConversationTurn:
queue: asyncio.Queue = asyncio.Queue(maxsize=10)
enqueue_mock = _enqueue_mock(queue)
with (
- patch.object(
- ingest,
- "derive_memory_group_id",
- return_value="expert_private_group",
- ) as derive_mock,
patch.object(
ingest,
"_enqueue_payload",
@@ -351,13 +341,10 @@ class TestEnqueueConversationTurn:
expert_id="expert-1",
)
- derive_mock.assert_called_once_with("user-1", "expert-1")
+ expert_group = MemoryScope.for_expert("user-1", "expert-1").group_id
enqueue_mock.assert_awaited_once()
- assert enqueue_mock.await_args.args[:2] == (
- "user-1",
- "expert_private_group",
- )
- assert queue.get_nowait()["group_id"] == "expert_private_group"
+ assert enqueue_mock.await_args.args[:2] == ("user-1", expert_group)
+ assert queue.get_nowait()["group_id"] == expert_group
class TestMemoryGroupQueueIsolation:
@@ -413,17 +400,10 @@ class TestQueueFullScenario:
return_value=mock_understanding
)
- with (
- patch.object(
- ingest,
- "derive_memory_group_id",
- return_value="user_abc-valid-id",
- ),
- patch(
- "backend.copilot.graphiti.ingest.resolve_user_name",
- new_callable=AsyncMock,
- return_value="Alice",
- ),
+ with patch(
+ "backend.copilot.graphiti.ingest.resolve_user_name",
+ new_callable=AsyncMock,
+ return_value="Alice",
):
# Create a tiny queue so it fills instantly.
tiny_q: asyncio.Queue = asyncio.Queue(maxsize=1)
@@ -495,12 +475,9 @@ class TestEnqueueEpisode:
async def test_enqueue_episode_returns_true_on_success(self) -> None:
q: asyncio.Queue = asyncio.Queue(maxsize=100)
enqueue_mock = _enqueue_mock(q)
- with (
- patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),
- patch.object(ingest, "_enqueue_payload", new=enqueue_mock),
- ):
+ with patch.object(ingest, "_enqueue_payload", new=enqueue_mock):
result = await ingest.enqueue_episode(
- user_id="abc",
+ scope=MemoryScope.for_user("abc"),
session_id="sess1",
name="test_ep",
episode_body="hello",
@@ -510,27 +487,16 @@ class TestEnqueueEpisode:
assert not q.empty()
@pytest.mark.asyncio
- async def test_enqueue_episode_returns_false_for_empty_user(self) -> None:
- result = await ingest.enqueue_episode(
- user_id="",
- session_id="sess1",
- name="test_ep",
- episode_body="hello",
- )
- assert result is False
-
- @pytest.mark.asyncio
- async def test_enqueue_episode_returns_false_on_invalid_user(self) -> None:
- with patch.object(
- ingest, "derive_memory_group_id", side_effect=ValueError("bad id")
- ):
- result = await ingest.enqueue_episode(
- user_id="bad",
- session_id="sess1",
- name="test_ep",
- episode_body="hello",
+ async def test_enqueue_episode_writes_to_the_scope_it_was_given(self) -> None:
+ q: asyncio.Queue = asyncio.Queue(maxsize=100)
+ enqueue_mock = _enqueue_mock(q)
+ scope = MemoryScope.for_expert("abc", "expert-1")
+ with patch.object(ingest, "_enqueue_payload", new=enqueue_mock):
+ assert await ingest.enqueue_episode(
+ scope, "sess1", name="test_ep", episode_body="hello"
)
- assert result is False
+ assert enqueue_mock.await_args.args[:2] == ("abc", scope.group_id)
+ assert q.get_nowait()["group_id"] == scope.group_id
@pytest.mark.asyncio
async def test_enqueue_episode_rejects_oversized_body_without_queueing(
@@ -540,12 +506,9 @@ class TestEnqueueEpisode:
worker or queue is touched — degraded dream writes must not reach
FalkorDB or the extraction LLM."""
enqueue_mock = AsyncMock()
- with (
- patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),
- patch.object(ingest, "_enqueue_payload", new=enqueue_mock),
- ):
+ with patch.object(ingest, "_enqueue_payload", new=enqueue_mock):
result = await ingest.enqueue_episode(
- user_id="abc",
+ scope=MemoryScope.for_user("abc"),
session_id="sess1",
name="runaway_consolidated_fact",
episode_body="x" * (ingest.MAX_EPISODE_BODY_BYTES + 1),
@@ -558,12 +521,9 @@ class TestEnqueueEpisode:
async def test_enqueue_episode_accepts_body_at_exact_size_cap(self) -> None:
q: asyncio.Queue = asyncio.Queue(maxsize=100)
enqueue_mock = _enqueue_mock(q)
- with (
- patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),
- patch.object(ingest, "_enqueue_payload", new=enqueue_mock),
- ):
+ with patch.object(ingest, "_enqueue_payload", new=enqueue_mock):
result = await ingest.enqueue_episode(
- user_id="abc",
+ scope=MemoryScope.for_user("abc"),
session_id="sess1",
name="cap_sized_ep",
episode_body="x" * ingest.MAX_EPISODE_BODY_BYTES,
@@ -576,14 +536,11 @@ class TestEnqueueEpisode:
"""Multi-byte UTF-8 content is measured in encoded bytes, so a
char-count under the cap can still be rejected."""
enqueue_mock = AsyncMock()
- with (
- patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),
- patch.object(ingest, "_enqueue_payload", new=enqueue_mock),
- ):
+ with patch.object(ingest, "_enqueue_payload", new=enqueue_mock):
# "é" encodes to 2 bytes — half the cap in chars, just over in bytes.
body = "é" * (ingest.MAX_EPISODE_BODY_BYTES // 2 + 1)
result = await ingest.enqueue_episode(
- user_id="abc",
+ scope=MemoryScope.for_user("abc"),
session_id="sess1",
name="multibyte_ep",
episode_body=body,
@@ -595,12 +552,9 @@ class TestEnqueueEpisode:
async def test_enqueue_episode_json_mode(self) -> None:
q: asyncio.Queue = asyncio.Queue(maxsize=100)
enqueue_mock = _enqueue_mock(q)
- with (
- patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),
- patch.object(ingest, "_enqueue_payload", new=enqueue_mock),
- ):
+ with patch.object(ingest, "_enqueue_payload", new=enqueue_mock):
result = await ingest.enqueue_episode(
- user_id="abc",
+ scope=MemoryScope.for_user("abc"),
session_id="sess1",
name="test_ep",
episode_body='{"content": "hello"}',
@@ -621,7 +575,6 @@ class TestDerivedFindingLane:
enqueue_mock = _enqueue_mock(q)
with (
- 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",
@@ -650,7 +603,6 @@ class TestDerivedFindingLane:
q: asyncio.Queue = asyncio.Queue(maxsize=100)
enqueue_mock = _enqueue_mock(q)
with (
- 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",
@@ -974,13 +926,10 @@ class TestEnqueueEpisodeEdgeMetadata:
@pytest.mark.asyncio
async def test_edge_metadata_rides_payload_sidecar(self) -> None:
q: asyncio.Queue = asyncio.Queue(maxsize=100)
- with (
- patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),
- patch.object(ingest, "_enqueue_payload", new=_enqueue_mock(q)),
- ):
+ with patch.object(ingest, "_enqueue_payload", new=_enqueue_mock(q)):
meta = {"status": "active", "provenance": "dream:p1"}
await ingest.enqueue_episode(
- user_id="abc",
+ scope=MemoryScope.for_user("abc"),
session_id="s",
name="dream_ep",
episode_body="{}",
@@ -995,12 +944,12 @@ class TestEnqueueEpisodeEdgeMetadata:
"""Conversation turns / memory-store calls pass no edge_metadata →
sidecar is None → worker skips stamping → no behavior change."""
q: asyncio.Queue = asyncio.Queue(maxsize=100)
- with (
- patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),
- patch.object(ingest, "_enqueue_payload", new=_enqueue_mock(q)),
- ):
+ with patch.object(ingest, "_enqueue_payload", new=_enqueue_mock(q)):
await ingest.enqueue_episode(
- user_id="abc", session_id="s", name="ep", episode_body="hi"
+ scope=MemoryScope.for_user("abc"),
+ session_id="s",
+ name="ep",
+ episode_body="hi",
)
payload = q.get_nowait()
assert payload["_edge_metadata"] is None
@@ -1012,13 +961,10 @@ class TestEnqueueEpisodeEdgeMetadata:
"""A scoped-drain caller's completion tracker is threaded onto the
payload so the worker can signal it after processing."""
q: asyncio.Queue = asyncio.Queue(maxsize=100)
- with (
- patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),
- patch.object(ingest, "_enqueue_payload", new=_enqueue_mock(q)),
- ):
+ with patch.object(ingest, "_enqueue_payload", new=_enqueue_mock(q)):
completion = ingest.IngestionCompletion()
await ingest.enqueue_episode(
- user_id="abc",
+ scope=MemoryScope.for_user("abc"),
session_id="s",
name="dream_ep",
episode_body="{}",
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_edge_props.py b/autogpt_platform/backend/backend/copilot/graphiti/migrations/backfill_edge_props.py
index 4e5c51b0ed..77b7d47bd6 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
@@ -37,10 +37,9 @@ import logging
import sys
from collections import defaultdict
-from backend.copilot.graphiti.client import derive_group_id
-from backend.copilot.graphiti.config import graphiti_config
-from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver
+from backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriver, open_driver
from backend.copilot.graphiti.memory_model import MemoryEnvelope, SourceKind
+from backend.copilot.graphiti.scope import MemoryScope
logger = logging.getLogger(__name__)
@@ -115,19 +114,14 @@ _UUID_CHUNK_SIZE = 500
async def backfill_one_user(user_id: str) -> int:
"""Recover/default edge metadata for one user. Returns count updated."""
try:
- group_id = derive_group_id(user_id)
+ scope = MemoryScope.for_user(user_id)
except ValueError:
logger.warning(
"Skipping user %s — invalid for group_id derivation", user_id[:12]
)
return 0
- driver = AutoGPTFalkorDriver(
- host=graphiti_config.falkordb_host,
- port=graphiti_config.falkordb_port,
- password=graphiti_config.falkordb_password or None,
- database=group_id,
- )
+ driver = open_driver(scope)
try:
updated = await backfill_graph(driver)
if updated:
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.py
index 1c86ae26e6..9db8841037 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
@@ -21,13 +21,11 @@ from . import backfill_edge_props as mig
@pytest.fixture(autouse=True)
def _stub_driver(mocker):
- """Replace AutoGPTFalkorDriver with a MagicMock that returns canned
+ """Replace the driver factory with a MagicMock that returns canned
execute_query results."""
driver = mocker.MagicMock()
driver.close = AsyncMock(return_value=None)
- mocker.patch.object(
- mig, "AutoGPTFalkorDriver", mocker.MagicMock(return_value=driver)
- )
+ mocker.patch.object(mig, "open_driver", mocker.MagicMock(return_value=driver))
return driver
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/scope.py b/autogpt_platform/backend/backend/copilot/graphiti/scope.py
new file mode 100644
index 0000000000..4c47de1ed0
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/graphiti/scope.py
@@ -0,0 +1,126 @@
+"""Which memory an operation reads and writes: the account's or one expert's.
+
+A ``MemoryScope`` is built once where a request enters the memory code (API
+routes, chat tools, ingestion, the dream and community-rebuild entry points)
+and passed down. Internal code reads ``group_id`` / ``scope_key`` off it
+instead of re-deriving them from a loose ``(user_id, expert_id)`` pair, so no
+caller can land in the account's graph by forgetting the ``expert_id``.
+
+The derivations stay in ``client.py``; this module only carries their
+results and owns the Redis key layouts keyed on them.
+"""
+
+from typing import Literal
+
+from pydantic import BaseModel, ConfigDict, Field, model_validator
+
+from .client import derive_memory_group_id, derive_memory_scope_key
+
+RedisKeyFamily = Literal[
+ "dream_lock", "last_completed", "hits", "rebuild_lock", "registration"
+]
+
+# One in-flight dream per scope (dream/locks.py).
+DREAM_LOCK_KEY_PREFIX = "dream:inflight:"
+# When the scope's last sync dream pass completed (dream/orchestrator.py).
+LAST_COMPLETED_KEY_PREFIX = "dream:last_completed:"
+# Warm-context hit counters for tentative edges, one key per edge so INCR
+# never contends and each TTL cleans up on its own (dream/ratification_hits.py).
+HIT_TRACKER_KEY_PREFIX = "mem:hits"
+# One community rebuild per graph (graphiti/communities.py).
+REBUILD_LOCK_KEY_PREFIX = "graphiti:community_rebuild_lock:"
+
+
+class MemoryScope(BaseModel):
+ """The owner, the expert (if any), and the ids derived from them.
+
+ ``group_id`` is the Graphiti namespace and FalkorDB database;
+ ``scope_key`` keys queues, locks and Redis markers. Both are validated
+ against the derivations, so a scope can never carry an id that does not
+ belong to its owner and expert.
+ """
+
+ model_config = ConfigDict(frozen=True)
+
+ owner_user_id: str = Field(min_length=1)
+ expert_id: str | None = Field(min_length=1)
+ group_id: str
+ scope_key: str
+
+ @classmethod
+ def for_user(cls, user_id: str) -> "MemoryScope":
+ """The account (Otto) scope. Raises ``ValueError`` for an invalid id."""
+ return cls(
+ owner_user_id=user_id,
+ expert_id=None,
+ group_id=derive_memory_group_id(user_id),
+ scope_key=derive_memory_scope_key(user_id),
+ )
+
+ @classmethod
+ def for_expert(cls, user_id: str, expert_id: str) -> "MemoryScope":
+ """One expert's scope. Raises ``ValueError`` for an invalid id."""
+ return cls(
+ owner_user_id=user_id,
+ expert_id=expert_id,
+ group_id=derive_memory_group_id(user_id, expert_id),
+ scope_key=derive_memory_scope_key(user_id, expert_id),
+ )
+
+ @classmethod
+ def build(cls, user_id: str, expert_id: str | None = None) -> "MemoryScope":
+ """``for_user`` when ``expert_id`` is None, else ``for_expert``."""
+ if expert_id is None:
+ return cls.for_user(user_id)
+ return cls.for_expert(user_id, expert_id)
+
+ @property
+ def is_expert(self) -> bool:
+ return self.expert_id is not None
+
+ def redis_key(
+ self,
+ family: RedisKeyFamily,
+ *,
+ edge_uuid: str | None = None,
+ registration_prefix: str | None = None,
+ ) -> str:
+ """This scope's Redis key for ``family``, exactly as it was keyed
+ before this class existed.
+
+ Most families key on ``scope_key`` (the raw user id for the account,
+ the group id for an expert). Two do not, and keep their layouts:
+ ``rebuild_lock`` keys on ``group_id``, and ``registration`` keys on
+ the owner's user id even for an expert scope, because the dream
+ crons are registered per user. ``hits`` needs the counted
+ ``edge_uuid``; ``registration`` needs the cron's marker prefix (see
+ ``dream/scheduling.py``).
+ """
+ match family:
+ case "dream_lock":
+ return f"{DREAM_LOCK_KEY_PREFIX}{self.scope_key}"
+ case "last_completed":
+ return f"{LAST_COMPLETED_KEY_PREFIX}{self.scope_key}"
+ case "hits":
+ if edge_uuid is None:
+ raise ValueError("the hits key needs an edge_uuid")
+ return f"{HIT_TRACKER_KEY_PREFIX}:{self.scope_key}:{edge_uuid}"
+ case "rebuild_lock":
+ return f"{REBUILD_LOCK_KEY_PREFIX}{self.group_id}"
+ case "registration":
+ if registration_prefix is None:
+ raise ValueError("the registration key needs a prefix")
+ return f"{registration_prefix}:{self.owner_user_id}"
+
+ @model_validator(mode="after")
+ def _ids_match_derivation(self) -> "MemoryScope":
+ derived = (
+ derive_memory_group_id(self.owner_user_id, self.expert_id),
+ derive_memory_scope_key(self.owner_user_id, self.expert_id),
+ )
+ if (self.group_id, self.scope_key) != derived:
+ raise ValueError(
+ "group_id and scope_key must be derived from owner_user_id "
+ "and expert_id"
+ )
+ return self
diff --git a/autogpt_platform/backend/backend/copilot/graphiti/scope_test.py b/autogpt_platform/backend/backend/copilot/graphiti/scope_test.py
new file mode 100644
index 0000000000..f0744ede7c
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/graphiti/scope_test.py
@@ -0,0 +1,172 @@
+"""Tests for MemoryScope: ids and Redis keys must match the legacy derivations."""
+
+import pytest
+from pydantic import ValidationError
+
+from backend.copilot.dream.scheduling import (
+ COMMUNITY_REBUILD_REGISTRATION_PREFIX,
+ NIGHTLY_BATCH_REGISTRATION_PREFIX,
+)
+
+from .client import derive_group_id, derive_memory_group_id, derive_memory_scope_key
+from .scope import (
+ DREAM_LOCK_KEY_PREFIX,
+ HIT_TRACKER_KEY_PREFIX,
+ LAST_COMPLETED_KEY_PREFIX,
+ REBUILD_LOCK_KEY_PREFIX,
+ MemoryScope,
+)
+
+USER_ID = "883cc9da-fe37-4863-839b-acba022bf3ef"
+EXPERT_ID = "11111111-2222-3333-4444-555555555555"
+
+BOTH_SCOPES = pytest.mark.parametrize("expert_id", [None, EXPERT_ID])
+
+
+class TestDerivedIds:
+ def test_account_scope_matches_legacy_derivations(self) -> None:
+ scope = MemoryScope.for_user(USER_ID)
+
+ assert scope.owner_user_id == USER_ID
+ assert scope.expert_id is None
+ assert not scope.is_expert
+ assert scope.group_id == derive_memory_group_id(USER_ID)
+ assert scope.group_id == derive_group_id(USER_ID)
+ assert scope.scope_key == derive_memory_scope_key(USER_ID)
+ assert scope.scope_key == USER_ID
+
+ def test_expert_scope_matches_legacy_derivations(self) -> None:
+ scope = MemoryScope.for_expert(USER_ID, EXPERT_ID)
+
+ assert scope.owner_user_id == USER_ID
+ assert scope.expert_id == EXPERT_ID
+ assert scope.is_expert
+ assert scope.group_id == derive_memory_group_id(USER_ID, EXPERT_ID)
+ assert scope.scope_key == derive_memory_scope_key(USER_ID, EXPERT_ID)
+ assert scope.scope_key == scope.group_id
+
+ def test_expert_and_account_never_share_a_graph(self) -> None:
+ account = MemoryScope.for_user(USER_ID)
+ expert = MemoryScope.for_expert(USER_ID, EXPERT_ID)
+
+ assert expert.group_id != account.group_id
+ assert expert.scope_key != account.scope_key
+
+ @BOTH_SCOPES
+ def test_build_dispatches_on_expert_id(self, expert_id: str | None) -> None:
+ expected = (
+ MemoryScope.for_user(USER_ID)
+ if expert_id is None
+ else MemoryScope.for_expert(USER_ID, expert_id)
+ )
+ assert MemoryScope.build(USER_ID, expert_id) == expected
+
+ def test_scope_is_frozen_and_hashable(self) -> None:
+ scope = MemoryScope.for_user(USER_ID)
+
+ with pytest.raises(ValidationError):
+ scope.group_id = "user_someone_else"
+ assert {scope: 1}[MemoryScope.for_user(USER_ID)] == 1
+
+
+class TestRedisKeys:
+ """Each family must equal the key its owning module built before
+ ``MemoryScope`` existed, for the account and for an expert."""
+
+ @BOTH_SCOPES
+ def test_dream_lock_keys_on_scope_key(self, expert_id: str | None) -> None:
+ scope = MemoryScope.build(USER_ID, expert_id)
+ legacy = f"{DREAM_LOCK_KEY_PREFIX}{derive_memory_scope_key(USER_ID, expert_id)}"
+ assert scope.redis_key("dream_lock") == legacy
+
+ @BOTH_SCOPES
+ def test_last_completed_keys_on_scope_key(self, expert_id: str | None) -> None:
+ scope = MemoryScope.build(USER_ID, expert_id)
+ scope_key = derive_memory_scope_key(USER_ID, expert_id)
+ legacy = f"{LAST_COMPLETED_KEY_PREFIX}{scope_key}"
+ assert scope.redis_key("last_completed") == legacy
+
+ @BOTH_SCOPES
+ def test_hits_key_on_scope_key_and_edge(self, expert_id: str | None) -> None:
+ scope = MemoryScope.build(USER_ID, expert_id)
+ scope_key = derive_memory_scope_key(USER_ID, expert_id)
+ legacy = f"{HIT_TRACKER_KEY_PREFIX}:{scope_key}:edge-1"
+ assert scope.redis_key("hits", edge_uuid="edge-1") == legacy
+
+ @BOTH_SCOPES
+ def test_rebuild_lock_keys_on_group_id(self, expert_id: str | None) -> None:
+ scope = MemoryScope.build(USER_ID, expert_id)
+ group_id = derive_memory_group_id(USER_ID, expert_id)
+ legacy = f"{REBUILD_LOCK_KEY_PREFIX}{group_id}"
+ assert scope.redis_key("rebuild_lock") == legacy
+
+ @BOTH_SCOPES
+ @pytest.mark.parametrize(
+ "prefix",
+ [COMMUNITY_REBUILD_REGISTRATION_PREFIX, NIGHTLY_BATCH_REGISTRATION_PREFIX],
+ )
+ def test_registration_keys_on_owner_user(
+ self, expert_id: str | None, prefix: str
+ ) -> None:
+ scope = MemoryScope.build(USER_ID, expert_id)
+ legacy = f"{prefix}:{USER_ID}"
+ assert scope.redis_key("registration", registration_prefix=prefix) == legacy
+
+ def test_hits_key_requires_edge_uuid(self) -> None:
+ with pytest.raises(ValueError, match="edge_uuid"):
+ MemoryScope.for_user(USER_ID).redis_key("hits")
+
+ def test_registration_key_requires_prefix(self) -> None:
+ with pytest.raises(ValueError, match="prefix"):
+ MemoryScope.for_user(USER_ID).redis_key("registration")
+
+
+class TestValidation:
+ def test_empty_user_id_is_rejected(self) -> None:
+ with pytest.raises(ValueError, match="user_id must be non-empty"):
+ MemoryScope.for_user("")
+ with pytest.raises(ValueError, match="user_id must be non-empty"):
+ MemoryScope.for_expert("", EXPERT_ID)
+ with pytest.raises(ValueError, match="user_id must be non-empty"):
+ MemoryScope.build("")
+
+ def test_empty_expert_id_is_rejected(self) -> None:
+ with pytest.raises(ValueError, match="expert_id must be non-empty"):
+ MemoryScope.for_expert(USER_ID, "")
+ with pytest.raises(ValueError, match="expert_id must be non-empty"):
+ MemoryScope.build(USER_ID, "")
+
+ def test_invalid_characters_raise_the_legacy_error(self) -> None:
+ with pytest.raises(ValueError, match="invalid characters"):
+ MemoryScope.for_user("abc.def")
+ with pytest.raises(ValueError, match="invalid characters"):
+ MemoryScope.for_expert(USER_ID, "expert/1")
+
+ def test_direct_construction_rejects_empty_ids(self) -> None:
+ account = MemoryScope.for_user(USER_ID)
+ with pytest.raises(ValidationError):
+ MemoryScope(
+ owner_user_id="",
+ expert_id=None,
+ group_id=account.group_id,
+ scope_key=account.scope_key,
+ )
+ with pytest.raises(ValidationError):
+ MemoryScope(
+ owner_user_id=USER_ID,
+ expert_id="",
+ group_id=account.group_id,
+ scope_key=account.scope_key,
+ )
+
+ def test_direct_construction_rejects_ids_from_another_scope(self) -> None:
+ """An expert scope carrying the account's ids is the silent-fallback
+ bug this type exists to prevent."""
+ account = MemoryScope.for_user(USER_ID)
+ with pytest.raises(ValidationError, match="must be derived"):
+ MemoryScope(
+ owner_user_id=USER_ID,
+ expert_id=EXPERT_ID,
+ group_id=account.group_id,
+ scope_key=account.scope_key,
+ )
diff --git a/autogpt_platform/backend/backend/copilot/learning/memory_link.py b/autogpt_platform/backend/backend/copilot/learning/memory_link.py
index c18f8bf8d1..4d80ffcbf2 100644
--- a/autogpt_platform/backend/backend/copilot/learning/memory_link.py
+++ b/autogpt_platform/backend/backend/copilot/learning/memory_link.py
@@ -21,6 +21,7 @@ from backend.copilot.graphiti.memory_model import (
MemoryStatus,
SourceKind,
)
+from backend.copilot.graphiti.scope import MemoryScope
from backend.data.skill_versions import SkillVersionRecord
logger = logging.getLogger(__name__)
@@ -57,7 +58,7 @@ async def link_version_to_memory(user_id: str, version: SkillVersionRecord) -> b
# ``dream_`` name prefix: the dream pass must not treat our own
# write as new user activity worth a paid consolidation pass.
return await enqueue_episode(
- user_id,
+ MemoryScope.build(user_id, version.expert_id),
f"skill_learning_{version.id}",
name=f"dream_skill_{version.id}",
episode_body=envelope.model_dump_json(),
@@ -70,7 +71,6 @@ async def link_version_to_memory(user_id: str, version: SkillVersionRecord) -> b
"confidence": None,
"provenance": envelope.provenance,
},
- expert_id=version.expert_id,
)
except Exception:
logger.warning(
diff --git a/autogpt_platform/backend/backend/copilot/learning/memory_link_test.py b/autogpt_platform/backend/backend/copilot/learning/memory_link_test.py
new file mode 100644
index 0000000000..87f4da0a69
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/learning/memory_link_test.py
@@ -0,0 +1,56 @@
+"""A learned skill version's reference episode goes into its own memory scope:
+the account's for an account skill, the expert's for an expert's, through
+``enqueue_episode`` as it is declared (the episode's scope its first
+argument)."""
+
+from datetime import datetime, timezone
+from unittest.mock import AsyncMock, patch
+
+import pytest
+
+from backend.copilot.graphiti.scope import MemoryScope
+from backend.data.skill_versions import SkillVersionRecord
+
+from . import memory_link
+
+
+def _version(expert_id: str | None) -> SkillVersionRecord:
+ return SkillVersionRecord(
+ id="version-1",
+ user_id="user-1",
+ expert_id=expert_id,
+ owner_key="owner",
+ skill_name="import-csv",
+ head_id="head-1",
+ version=2,
+ content="# Import a CSV",
+ content_hash="hash",
+ description="Import a CSV",
+ origin="learned",
+ actor_user_id=None,
+ summary="",
+ base_version_id=None,
+ restored_from_version_id=None,
+ review_id=None,
+ state="published",
+ state_reason="",
+ blocked_pattern_class=None,
+ blocked_step=None,
+ created_at=datetime.now(timezone.utc),
+ )
+
+
[email protected]("expert_id", [None, "expert-1"], ids=["account", "expert"])
+async def test_a_learned_version_is_linked_in_its_own_scope(expert_id):
+ with (
+ patch.object(memory_link, "is_enabled_for_user", AsyncMock(return_value=True)),
+ patch.object(memory_link, "enqueue_episode", autospec=True) as enqueue,
+ ):
+ enqueue.return_value = True
+ linked = await memory_link.link_version_to_memory("user-1", _version(expert_id))
+
+ assert linked is True
+ assert enqueue.await_args.args[0] == MemoryScope.build("user-1", expert_id)
+ assert enqueue.await_args.args[1] == "skill_learning_version-1"
+ assert enqueue.await_args.kwargs["name"] == "dream_skill_version-1"
diff --git a/autogpt_platform/backend/backend/copilot/tools/delegate_to_expert.py b/autogpt_platform/backend/backend/copilot/tools/delegate_to_expert.py
index d6aaa466ae..8f306acf4a 100644
--- a/autogpt_platform/backend/backend/copilot/tools/delegate_to_expert.py
+++ b/autogpt_platform/backend/backend/copilot/tools/delegate_to_expert.py
@@ -12,7 +12,7 @@ backs every other copilot turn, so the delegated work:
- speaks and acts as the target expert (``<expert_identity>`` is rebuilt from
the target's Soul by the normal per-turn path),
- reads and writes the *target's* memory namespace
- (:func:`derive_memory_group_id` keys on the session's ``expert_id``),
+ (the session's ``expert_id`` selects its ``MemoryScope``),
- attributes the agent runs it starts to the *target's* weekly budget (only
graph executions accrue weekly spend; the delegated conversation's own LLM
cost does not), which is why a paused/archived teammate is refused here,
diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_forget.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_forget.py
index e23ae41ecf..c2da6463d1 100644
--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_forget.py
+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_forget.py
@@ -9,8 +9,9 @@ from datetime import datetime, timezone
from typing import Any, Literal
from backend.copilot.graphiti._format import extract_fact, extract_temporal_validity
-from backend.copilot.graphiti.client import derive_memory_group_id, get_graphiti_client
+from backend.copilot.graphiti.client import get_graphiti_client
from backend.copilot.graphiti.config import is_enabled_for_user
+from backend.copilot.graphiti.scope import MemoryScope
from backend.copilot.model import ChatSession
from .base import BaseTool
@@ -124,7 +125,7 @@ class MemoryForgetSearchTool(BaseTool):
)
try:
- group_id = derive_memory_group_id(user_id, session.expert_id)
+ group_id = MemoryScope.build(user_id, session.expert_id).group_id
except ValueError:
return ErrorResponse(
message="Invalid user ID for memory operations.",
@@ -252,7 +253,7 @@ class MemoryForgetConfirmTool(BaseTool):
)
try:
- group_id = derive_memory_group_id(user_id, session.expert_id)
+ group_id = MemoryScope.build(user_id, session.expert_id).group_id
except ValueError:
return ErrorResponse(
message="Invalid user ID for memory operations.",
diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_forget_test.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_forget_test.py
index 12f890741c..85c702f37b 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,10 @@
"""Tests for graphiti_forget delete helpers."""
-from unittest.mock import AsyncMock, MagicMock
+from unittest.mock import AsyncMock
import pytest
+from backend.copilot.graphiti.scope import MemoryScope
from backend.copilot.model import ChatSession
from backend.copilot.tools.graphiti_forget import (
_MAX_FAILURE_DETAIL,
@@ -50,19 +51,16 @@ class TestExpertMemoryScope:
async def _enabled(_user_id: str) -> bool:
return True
- derive = MagicMock(return_value="expert_private_group")
client = type("Client", (), {"search": AsyncMock(return_value=[])})()
+ opened: list[str] = []
async def _get_client(group_id: str):
- assert group_id == "expert_private_group"
+ 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.derive_memory_group_id", derive
- )
monkeypatch.setattr(
"backend.copilot.tools.graphiti_forget.get_graphiti_client", _get_client
)
@@ -78,10 +76,11 @@ class TestExpertMemoryScope:
query="private fact",
)
- derive.assert_called_once_with("user-abc", "expert-1")
+ 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_private_group"],
+ group_ids=[expert_group],
num_results=10,
)
@@ -92,21 +91,18 @@ class TestExpertMemoryScope:
async def _enabled(_user_id: str) -> bool:
return True
- derive = MagicMock(return_value="expert_private_group")
driver = AsyncMock()
driver.execute_query.return_value = ([], None, None)
client = type("Client", (), {"graph_driver": driver})()
+ opened: list[str] = []
async def _get_client(group_id: str):
- assert group_id == "expert_private_group"
+ 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.derive_memory_group_id", derive
- )
monkeypatch.setattr(
"backend.copilot.tools.graphiti_forget.get_graphiti_client", _get_client
)
@@ -122,7 +118,7 @@ class TestExpertMemoryScope:
uuids=["private-edge"],
)
- derive.assert_called_once_with("user-abc", "expert-1")
+ assert opened == [MemoryScope.for_expert("user-abc", "expert-1").group_id]
class TestSoftDeleteNoMatchReportsFailure:
diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_search.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_search.py
index 4a49e73498..1d1bff472c 100644
--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_search.py
+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_search.py
@@ -12,8 +12,9 @@ from backend.copilot.graphiti._format import (
extract_fact,
extract_temporal_validity,
)
-from backend.copilot.graphiti.client import derive_memory_group_id, get_graphiti_client
+from backend.copilot.graphiti.client import get_graphiti_client
from backend.copilot.graphiti.config import is_enabled_for_user
+from backend.copilot.graphiti.scope import MemoryScope
from backend.copilot.model import ChatSession
from .base import BaseTool
@@ -101,7 +102,7 @@ class MemorySearchTool(BaseTool):
limit = min(limit, _MAX_LIMIT)
try:
- group_id = derive_memory_group_id(user_id, session.expert_id)
+ group_id = MemoryScope.build(user_id, session.expert_id).group_id
except ValueError:
return ErrorResponse(
message="Invalid user ID for memory operations.",
diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_search_test.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_search_test.py
index b288444f40..469e99a4ce 100644
--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_search_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_search_test.py
@@ -6,6 +6,7 @@ from unittest.mock import AsyncMock, patch
import pytest
from backend.copilot.graphiti.memory_model import MemoryEnvelope, MemoryKind, SourceKind
+from backend.copilot.graphiti.scope import MemoryScope
from backend.copilot.model import ChatSession
from backend.copilot.tools.graphiti_search import (
MemorySearchTool,
@@ -33,10 +34,6 @@ async def test_expert_session_searches_only_expert_memory_group() -> None:
new_callable=AsyncMock,
return_value=True,
),
- patch(
- "backend.copilot.tools.graphiti_search.derive_memory_group_id",
- return_value="expert_private_group",
- ) as derive_mock,
patch(
"backend.copilot.tools.graphiti_search.get_graphiti_client",
new_callable=AsyncMock,
@@ -50,16 +47,14 @@ async def test_expert_session_searches_only_expert_memory_group() -> None:
)
assert isinstance(result, MemorySearchResponse)
- derive_mock.assert_called_once_with("user-1", "expert-1")
- get_client_mock.assert_awaited_once_with("expert_private_group")
+ 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_private_group"],
+ group_ids=[expert_group],
num_results=15,
)
- assert client.retrieve_episodes.await_args.kwargs["group_ids"] == [
- "expert_private_group"
- ]
+ assert client.retrieve_episodes.await_args.kwargs["group_ids"] == [expert_group]
class TestFilterEpisodesByScopeTruncation:
diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_store.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_store.py
index bb43085102..50c272c52d 100644
--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_store.py
+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_store.py
@@ -18,6 +18,7 @@ from backend.copilot.graphiti.memory_model import (
RuleMemory,
SourceKind,
)
+from backend.copilot.graphiti.scope import MemoryScope
from backend.copilot.model import ChatSession
from .base import BaseTool
@@ -190,6 +191,14 @@ class MemoryStoreTool(BaseTool):
session_id=session.session_id,
)
+ try:
+ 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,
+ )
+
rule_model = None
if rule and memory_kind == "rule":
try:
@@ -261,13 +270,12 @@ class MemoryStoreTool(BaseTool):
)
queued = await enqueue_episode(
- user_id,
+ memory_scope,
session.session_id,
name=name,
episode_body=episode_body,
source_description=source_description,
is_json=True,
- expert_id=session.expert_id,
)
if not queued:
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 6b32303eb5..d7d58ab7fa 100644
--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_store_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_store_test.py
@@ -7,6 +7,7 @@ from unittest.mock import AsyncMock, patch
import pytest
from backend.copilot.graphiti.ingest import MAX_EPISODE_BODY_BYTES
+from backend.copilot.graphiti.scope import MemoryScope
from backend.copilot.model import ChatSession
from backend.copilot.tools.graphiti_store import MemoryStoreTool
from backend.copilot.tools.models import ErrorResponse, MemoryStoreResponse
@@ -264,7 +265,36 @@ class TestMemoryStoreTool:
)
assert isinstance(result, MemoryStoreResponse)
- assert mock_enqueue.await_args.kwargs["expert_id"] == "expert-1"
+ assert mock_enqueue.await_args.args[0] == MemoryScope.for_expert(
+ "user-1", "expert-1"
+ )
+
+ @pytest.mark.asyncio
+ async def test_store_rejects_an_invalid_memory_scope_before_enqueue(self):
+ 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,
+ ),
+ ):
+ result = await tool._execute(
+ user_id="not a valid id",
+ session=_make_session(),
+ name="some_fact",
+ content="A fact worth remembering.",
+ )
+
+ assert isinstance(result, ErrorResponse)
+ assert result.message == "Invalid user ID for memory operations."
+ mock_enqueue.assert_not_awaited()
@pytest.mark.asyncio
async def test_store_invalid_source_kind_falls_back(self):