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.
feat(backend): isolate expert memory (#14031)
Nicholas Tindleb2d39cab87f2
diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.pyindex 99a57b3ba0..3788bb681c 100644--- a/autogpt_platform/backend/backend/api/features/chat/routes.py+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py@@ -472,7 +472,7 @@ async def list_sessions(ctx: Annotated[auth.RequestContext, Security(auth.get_request_context)],limit: int = Query(default=50, ge=1, le=100),offset: int = Query(default=0, ge=0),- expert_id: str | None = Query(default=None, max_length=128),+ expert_id: str | None = Query(default=None, min_length=1, max_length=128),pinned_first: bool = Query(default=True,description=(diff --git a/autogpt_platform/backend/backend/api/features/chat/routes_test.py b/autogpt_platform/backend/backend/api/features/chat/routes_test.pyindex 024c743256..74ec47470f 100644--- a/autogpt_platform/backend/backend/api/features/chat/routes_test.py+++ b/autogpt_platform/backend/backend/api/features/chat/routes_test.py@@ -2453,6 +2453,22 @@ def test_list_sessions_filters_by_expert_id(assert mock_get.call_args.kwargs["expert_id"] == "expert-1"+def test_list_sessions_rejects_empty_expert_id(+ mocker: pytest_mock.MockerFixture,+) -> None:+ """GET /sessions?expert_id= must 422 at validation instead of reaching+ the db layer, whose ValueError on "" would surface as a 4xx/5xx."""+ mock_get = mocker.patch(+ "backend.api.features.chat.routes.get_user_sessions",+ new_callable=AsyncMock,+ )++ response = client.get("/sessions?expert_id=")++ assert response.status_code == 422+ mock_get.assert_not_awaited()++def test_list_sessions_can_request_strict_recency(mocker: pytest_mock.MockerFixture,) -> None:diff --git a/autogpt_platform/backend/backend/api/features/experts/experts_db.py b/autogpt_platform/backend/backend/api/features/experts/experts_db.pyindex 0ac1cd09d9..78b32db3d1 100644--- a/autogpt_platform/backend/backend/api/features/experts/experts_db.py+++ b/autogpt_platform/backend/backend/api/features/experts/experts_db.py@@ -214,7 +214,11 @@ async def owns_active_expert(user_id: str, expert_id: str) -> bool:async def get_expert(- user_id: str, expert_id: str, *, include_workflows: bool = True+ user_id: str,+ expert_id: str,+ *,+ include_workflows: bool = True,+ include_archived: bool = False,) -> Expert | None:"""Fetch a hired expert owned by *user_id*.@@ -222,14 +226,22 @@ async def get_expert(+ StoreListingVersion joins when the caller only needs the expert's owncolumns. The returned model then always carries an empty ``workflows``list — never use that flag to decide whether workflows are installed.++ Archived experts are hidden by default so product surfaces treat them as+ gone. Set ``include_archived=True`` when the caller must distinguish+ "archived" (reversible — re-hire revives) from "deleted": the scheduler's+ scope gate uses this to skip firings without destroying schedules that+ an un-archive should bring back."""+ where: prisma.types.ExpertWhereInput = {+ "id": expert_id,+ "ownerUserId": user_id,+ "isTemplate": False,+ }+ if not include_archived:+ where["isArchived"] = Falserow = await prisma.models.Expert.prisma().find_first(- where={- "id": expert_id,- "ownerUserId": user_id,- "isTemplate": False,- "isArchived": False,- },+ where=where,include=_WORKFLOW_INCLUDE if include_workflows else None,)if row is None:diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex 9770f881f5..34acc9ed1b 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -46,6 +46,8 @@ from backend.copilot.config import CopilotLLMModel, CopilotModefrom backend.copilot.context import get_workspace_manager, set_execution_contextfrom backend.copilot.expert_context import build_expert_identity_suffixfrom backend.copilot.graphiti.config import is_enabled_for_user+from backend.copilot.graphiti.context import fetch_warm_context+from backend.copilot.graphiti.ingest import enqueue_conversation_turnfrom backend.copilot.local_context_probe import (compaction_target_for_window,probe_local_context_window,@@ -1559,6 +1561,34 @@ async def _upload_final_transcript(logger.error("[Baseline] Transcript upload failed: %s", upload_err)+async def _fetch_graphiti_context(+ user_id: str,+ session: ChatSession,+ message: str | None,+) -> str | None:+ return await fetch_warm_context(+ user_id,+ message or "",+ expert_id=session.expert_id,+ )+++async def _enqueue_graphiti_turn(+ user_id: str,+ session: ChatSession,+ session_id: str,+ message: str,+ assistant_msg: str,+) -> None:+ await enqueue_conversation_turn(+ user_id,+ session_id,+ message,+ assistant_msg=assistant_msg,+ expert_id=session.expert_id,+ )++async def stream_chat_completion_baseline(session_id: str,message: str | None = None,@@ -1591,6 +1621,10 @@ async def stream_chat_completion_baseline(f"Session {session_id} not found. Please create a new session first.")+ expert_session_suffix = await build_expert_identity_suffix(+ session.user_id, session.expert_id+ )+# The session row is the tenancy anchor; the turn entry's org/team only# backfills sessions created before org tagging (pre-migration rows).if session.organization_id is None and organization_id:@@ -1781,9 +1815,6 @@ async def stream_chat_completion_baseline(# the ~20KB guide warm for the whole session. Empty string for# non-builder sessions keeps the cross-user cache hot.builder_session_suffix = await build_builder_system_prompt_suffix(session)- expert_session_suffix = await build_expert_identity_suffix(- session.user_id, session.expert_id- )system_prompt = (base_system_prompt+ SHARED_TOOL_NOTES@@ -1799,9 +1830,7 @@ async def stream_chat_completion_baseline(# after openai_messages is built — keeps system prompt static for caching.warm_ctx: str | None = Noneif graphiti_enabled and user_id and _pre_drain_msg_count <= 1:- from backend.copilot.graphiti.context import fetch_warm_context-- warm_ctx = await fetch_warm_context(user_id, message or "")+ warm_ctx = await _fetch_graphiti_context(user_id, session, message)# Context path: transcript content (compacted, isCompactSummary preserved) +# gap (DB messages after watermark) + current user turn.@@ -2524,17 +2553,16 @@ async def stream_chat_completion_baseline(# --- Graphiti: ingest conversation turn for temporal memory ---if graphiti_enabled and user_id and message and is_user_message:- from backend.copilot.graphiti.ingest import enqueue_conversation_turn-# Pass only the final assistant reply (after stripping tool-loop# chatter) so derived-finding distillation sees the substantive# response, not intermediate tool-planning text._ingest_task = asyncio.create_task(- enqueue_conversation_turn(+ _enqueue_graphiti_turn(user_id,+ session,session_id,message,- assistant_msg=final_text if state else "",+ final_text if state else "",))_background_tasks.add(_ingest_task)diff --git a/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py b/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.pyindex 8f9d34e4ab..4175602574 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py@@ -21,6 +21,8 @@ from backend.copilot.baseline.service import (_build_cached_system_message,_build_natural_finish_empty_fallback_events,_compress_session_messages,+ _enqueue_graphiti_turn,+ _fetch_graphiti_context,_fresh_anthropic_caching_headers,_fresh_ephemeral_cache_control,_is_anthropic_model,@@ -29,8 +31,10 @@ from backend.copilot.baseline.service import (_natural_finish_empty_notice_text,_split_user_message_after_skills_block,_supports_prompt_cache_markers,+ stream_chat_completion_baseline,)-from backend.copilot.model import ChatMessage+from backend.copilot.expert_context import ExpertSessionUnavailableError+from backend.copilot.model import ChatMessage, ChatSessionfrom backend.copilot.response_model import (StreamReasoningDelta,StreamReasoningEnd,@@ -45,6 +49,91 @@ from backend.util.prompt import CompressResultfrom backend.util.tool_call_loop import LLMLoopResponse, LLMToolCall, ToolCallResult+async def test_expert_identity_failure_precedes_baseline_turn_mutation() -> None:+ session = ChatSession.new("user-1", dry_run=False, expert_id="expert-1")+ identity_mock = AsyncMock(+ side_effect=ExpertSessionUnavailableError(+ "The expert for this session no longer exists or is archived."+ )+ )++ with (+ patch(+ "backend.copilot.baseline.service.build_expert_identity_suffix",+ new=identity_mock,+ ),+ pytest.raises(ExpertSessionUnavailableError),+ ):+ async for _ in stream_chat_completion_baseline(+ session_id=session.session_id,+ message="private prompt",+ user_id="user-1",+ session=session,+ ):+ pass++ identity_mock.assert_awaited_once_with("user-1", "expert-1")+ assert session.messages == []+++async def test_fetch_graphiti_context_uses_expert_session_scope() -> None:+ session = ChatSession.new(+ "user-1",+ dry_run=False,+ expert_id="expert-1",+ )+ fetch_mock = AsyncMock(return_value="expert context")++ with patch(+ "backend.copilot.baseline.service.fetch_warm_context",+ new=fetch_mock,+ ):+ context = await _fetch_graphiti_context(+ "user-1",+ session,+ "first prompt",+ )++ assert context == "expert context"+ fetch_mock.assert_awaited_once_with(+ "user-1",+ "first prompt",+ expert_id="expert-1",+ )+++async def test_enqueue_graphiti_turn_uses_expert_session_scope() -> None:+ session = ChatSession.new(+ "user-1",+ dry_run=False,+ expert_id="expert-1",+ )+ enqueue_mock = AsyncMock()++ with patch(+ "backend.copilot.baseline.service.enqueue_conversation_turn",+ new=enqueue_mock,+ ):+ await _enqueue_graphiti_turn(+ "user-1",+ session,+ "session-1",+ "private prompt",+ "private response",+ )++ enqueue_mock.assert_awaited_once_with(+ "user-1",+ "session-1",+ "private prompt",+ assistant_msg="private response",+ expert_id="expert-1",+ )++class TestBaselineStreamState:def test_defaults(self):state = _BaselineStreamState()diff --git a/autogpt_platform/backend/backend/copilot/db.py b/autogpt_platform/backend/backend/copilot/db.pyindex aa6bb6850e..c96f6a776b 100644--- a/autogpt_platform/backend/backend/copilot/db.py+++ b/autogpt_platform/backend/backend/copilot/db.py@@ -696,6 +696,7 @@ async def get_user_chat_sessions(organization_id: str | None = None,title_contains: str | None = None,expert_id: str | None = None,+ autopilot_only: bool = False,pinned_first: bool = True,) -> list[ChatSessionInfo]:"""Get chat sessions for a user, ordered by most recent.@@ -711,9 +712,18 @@ async def get_user_chat_sessions(without waiting on async embedding.``expert_id`` restricts the listing to sessions scoped to that expert.+ ``autopilot_only`` restricts it to sessions whose ``expertId`` is NULL.+ The explicit flag is necessary because ``expert_id=None`` retains the+ existing meaning of "all expert scopes" for user-facing session lists.+``pinned_first=False`` provides strict recency ordering for internaladoption flows; the user-facing sidebar keeps pinned sessions first."""+ if expert_id == "":+ raise ValueError("expert_id must be non-empty")+ if expert_id is not None and autopilot_only:+ raise ValueError("expert_id and autopilot_only are mutually exclusive")+params: list[Any] = [user_id]conditions = ['"userId" = $1', _EXCLUDE_DREAM_SESSIONS_SQL]if organization_id is not None:@@ -724,9 +734,11 @@ async def get_user_chat_sessions(if title_contains:params.append(f"%{_escape_like(title_contains)}%")conditions.append(f'"title" ILIKE ${len(params)}')- if expert_id:+ if expert_id is not None:params.append(expert_id)conditions.append(f'"expertId" = ${len(params)}')+ elif autopilot_only:+ conditions.append('"expertId" IS NULL')params.extend((limit, offset))ordering = ('"isPinned" DESC, "updatedAt" DESC' if pinned_first else '"updatedAt" DESC'@@ -745,6 +757,7 @@ async def get_user_session_count(user_id: str,organization_id: str | None = None,expert_id: str | None = None,+ autopilot_only: bool = False,) -> int:"""Get the total number of chat sessions for a user.@@ -752,6 +765,11 @@ async def get_user_session_count(filter as :func:`get_user_chat_sessions` so pagination totals alwaysmatch the visible list."""+ if expert_id == "":+ raise ValueError("expert_id must be non-empty")+ if expert_id is not None and autopilot_only:+ raise ValueError("expert_id and autopilot_only are mutually exclusive")+params: list[Any] = [user_id]conditions = ['"userId" = $1', _EXCLUDE_DREAM_SESSIONS_SQL]if organization_id is not None:@@ -759,9 +777,11 @@ async def get_user_session_count(conditions.append(f'("organizationId" = ${len(params)} OR "organizationId" IS NULL)')- if expert_id:+ if expert_id is not None:params.append(expert_id)conditions.append(f'"expertId" = ${len(params)}')+ elif autopilot_only:+ conditions.append('"expertId" IS NULL')rows = await db.query_raw_with_schema('SELECT COUNT(*)::int AS "count" FROM {schema_prefix}"ChatSession" WHERE '+ " AND ".join(conditions),diff --git a/autogpt_platform/backend/backend/copilot/db_session_listing_test.py b/autogpt_platform/backend/backend/copilot/db_session_listing_test.pyindex f50f281d76..de38726602 100644--- a/autogpt_platform/backend/backend/copilot/db_session_listing_test.py+++ b/autogpt_platform/backend/backend/copilot/db_session_listing_test.py@@ -113,6 +113,45 @@ async def test_title_search_escapes_like_wildcards_and_keeps_dream_filter():assert raw.call_args.args[1:] == ("u1", "%50\\%\\_done\\\\%", 10, 5)+async def test_autopilot_only_list_query_requires_null_expert_id():+ raw = AsyncMock(return_value=[])+ with patch(_RAW_QUERY_TARGET, raw):+ await get_user_chat_sessions("u1", autopilot_only=True)++ query = raw.call_args.args[0]+ assert '"expertId" IS NULL' in query+ assert raw.call_args.args[1:] == ("u1", 50, 0)+++async def test_autopilot_only_count_query_requires_null_expert_id():+ raw = AsyncMock(return_value=[{"count": 3}])+ with patch(_RAW_QUERY_TARGET, raw):+ count = await get_user_session_count("u1", autopilot_only=True)++ assert count == 3+ query = raw.call_args.args[0]+ assert '"expertId" IS NULL' in query+ assert raw.call_args.args[1:] == ("u1",)+++async def test_autopilot_only_and_expert_filter_are_mutually_exclusive():+ with pytest.raises(ValueError, match="mutually exclusive"):+ await get_user_chat_sessions("u1", expert_id="expert-1", autopilot_only=True)+ with pytest.raises(ValueError, match="mutually exclusive"):+ await get_user_session_count("u1", expert_id="expert-1", autopilot_only=True)+++async def test_empty_expert_filter_is_rejected_consistently():+ with pytest.raises(ValueError, match="non-empty"):+ await get_user_chat_sessions("u1", expert_id="")+ with pytest.raises(ValueError, match="non-empty"):+ await get_user_session_count("u1", expert_id="")++@pytest.mark.asyncioasync def test_count_query_uses_same_dream_exclusion_as_list():"""Sidebar pagination count must stay consistent with the visible list."""diff --git a/autogpt_platform/backend/backend/copilot/dream/apply.py b/autogpt_platform/backend/backend/copilot/dream/apply.pyindex 1638f2fd5b..e3d1bba938 100644--- a/autogpt_platform/backend/backend/copilot/dream/apply.py+++ b/autogpt_platform/backend/backend/copilot/dream/apply.py@@ -24,7 +24,7 @@ import uuid as uuidlibfrom collections.abc import Mappingfrom datetime import datetime, timezone-from backend.copilot.graphiti.client import derive_group_id+from backend.copilot.graphiti.client import derive_memory_group_idfrom backend.copilot.graphiti.config import graphiti_configfrom backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriverfrom backend.copilot.graphiti.ingest import (@@ -165,6 +165,8 @@ async def _write_consolidated_fact(fact: ConsolidatedFact,session_id: str,completion: IngestionCompletion,+ *,+ expert_id: str | None = None,) -> bool:envelope = MemoryEnvelope(content=fact.content,@@ -187,6 +189,7 @@ async def _write_consolidated_fact(is_json=True,edge_metadata=_edge_metadata(envelope),completion=completion,+ expert_id=expert_id,)@@ -197,6 +200,8 @@ async def _write_proposed_finding(finding: ProposedFinding,session_id: str,completion: IngestionCompletion,+ *,+ expert_id: str | None = None,) -> bool:envelope = MemoryEnvelope(content=finding.content,@@ -221,6 +226,7 @@ async def _write_proposed_finding(is_json=True,edge_metadata=_edge_metadata(envelope),completion=completion,+ expert_id=expert_id,)@@ -398,7 +404,9 @@ async def _apply_entity_invalidations(return total, summaries-async def _create_dream_session(user_id: str, pass_id: str) -> str:+async def _create_dream_session(+ user_id: str, pass_id: str, expert_id: str | None = None+) -> str:"""Create the dream-kind ChatSession shell and return its id.Written up front (before the memory ops) because the fact/proposal@@ -431,12 +439,14 @@ async def _create_dream_session(user_id: str, pass_id: str) -> str:org_id, team_id = None, Nonesession_id = str(uuidlib.uuid4())+ metadata = ChatSessionMetadata(kind="dream", dream_pass_id=pass_id)await chat_db().create_chat_session(session_id=session_id,user_id=user_id,organization_id=org_id,team_id=team_id,- metadata=ChatSessionMetadata(kind="dream", dream_pass_id=pass_id),+ metadata=metadata,+ expert_id=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@@ -537,6 +547,7 @@ async def apply_operations(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,@@ -615,13 +626,15 @@ async def apply_operations("snapshot": DreamOperationsSnapshot(),}- group_id = derive_group_id(user_id)+ 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)+ session_id = await _create_dream_session(+ user_id=user_id, pass_id=pass_id, expert_id=expert_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@@ -633,7 +646,13 @@ async def apply_operations(write_summaries: list[WriteSummary] = []for i, fact in enumerate(ops.writes):if await _write_consolidated_fact(- user_id, pass_id, i, fact, session_id=session_id, completion=completion+ user_id,+ pass_id,+ i,+ fact,+ session_id=session_id,+ completion=completion,+ expert_id=expert_id,):completion.register()written += 1@@ -651,7 +670,13 @@ async def apply_operations(proposal_summaries: list[WriteSummary] = []for i, prop in enumerate(ops.proposals):if await _write_proposed_finding(- user_id, pass_id, i, prop, session_id=session_id, completion=completion+ user_id,+ pass_id,+ i,+ prop,+ session_id=session_id,+ completion=completion,+ expert_id=expert_id,):completion.register()proposed += 1diff --git a/autogpt_platform/backend/backend/copilot/dream/apply_test.py b/autogpt_platform/backend/backend/copilot/dream/apply_test.pyindex d4d58324da..ca2b550127 100644--- a/autogpt_platform/backend/backend/copilot/dream/apply_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/apply_test.py@@ -72,14 +72,19 @@ def _stub_boundaries(mocker):# ChatSession + ChatMessage writes — apply.py imports them lazily inside# ``_create_dream_session`` / ``_write_dream_summary_message`` to avoid a# circular import. Patch where the symbol is looked up (copilot.db).+ database = mocker.MagicMock()+ database.create_chat_session = AsyncMock(+ return_value=mocker.MagicMock(session_id="s1")+ )+ database.update_chat_session_title = AsyncMock(return_value=True)+ database.add_chat_message = AsyncMock(return_value=None)+ mocker.patch("backend.data.db_accessors.chat_db", return_value=database)+ mocker.patch("backend.copilot.db.create_chat_session", database.create_chat_session)mocker.patch(- "backend.copilot.db.create_chat_session",- AsyncMock(return_value=mocker.MagicMock(session_id="s1")),- )- mocker.patch(- "backend.copilot.db.update_chat_session_title", AsyncMock(return_value=True)+ "backend.copilot.db.update_chat_session_title",+ database.update_chat_session_title,)- mocker.patch("backend.copilot.db.add_chat_message", AsyncMock(return_value=None))+ mocker.patch("backend.copilot.db.add_chat_message", database.add_chat_message)# _create_dream_session's tenant lookup. Unmocked it runs REAL Prisma# queries on this test's function-scoped event loop whenever an earlier# test already connected Prisma (its except swallows the failure when@@ -443,6 +448,28 @@ async def test_title_failure_does_not_abort_apply(mocker):copilot_db.add_chat_message.assert_awaited_once()+async def test_expert_dream_session_and_write_keep_expert_scope(mocker):+ db = mocker.MagicMock()+ db.create_chat_session = AsyncMock()+ 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")+ assert db.create_chat_session.call_args.kwargs["expert_id"] == "expert-1"++ await apply_mod._write_consolidated_fact(+ "u1",+ "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"++@pytest.mark.asyncioasync def test_summary_written_after_memory_ops(mocker):"""The user-facing narrative must be written AFTER the memory ops, so adiff --git a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.pyindex b8d7bfa05c..7532f2772c 100644--- a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py+++ b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py@@ -62,6 +62,8 @@ if TYPE_CHECKING:from backend.executor.batch_executor import PendingEntryfrom backend.util.llm.providers import BatchResultRow+ from .fetch import DreamInput+logger = logging.getLogger(__name__)@@ -118,7 +120,7 @@ async def _read_state(pass_id: str) -> dict[str, dict[str, Any]]:async def _write_phase_to_state(- *, pass_id: str, phase: DreamPhase, row: "BatchResultRow"+ *, pass_id: str, phase: DreamPhase, row: BatchResultRow) -> None:from backend.data.redis_client import get_redis_async@@ -240,7 +242,9 @@ async def _best_effort_cleanup(pass_id: str) -> None:)-async def _release_lock(user_id: str, pass_id: str) -> None:+async def _release_lock(+ user_id: str, pass_id: str, expert_id: str | None = None+) -> None:"""Release the disowned dream lock with the ownership token persistedalongside the input bundle. Must run before ``delete_input_bundle`` —the token rides on that key. A missing token (bundle TTL'd out,@@ -261,7 +265,7 @@ async def _release_lock(user_id: str, pass_id: str) -> None:"leaving the lock to its TTL",pass_id,)- await release_dream_lock(user_id, token)+ await release_dream_lock(user_id, token, expert_id)# ---------------------------------------------------------------------------@@ -270,7 +274,7 @@ async def _release_lock(user_id: str, pass_id: str) -> None:async def handle_dream_batch_result(- entry: "PendingEntry", rows: list["BatchResultRow"]+ entry: PendingEntry, rows: list[BatchResultRow]) -> None:"""BatchExecutor entry — called once per finished phase batch.@@ -286,6 +290,8 @@ async def handle_dream_batch_result("""payload = entry.payload or {}user_id = str(payload.get("user_id") or "")+ expert_id_raw = payload.get("expert_id")+ expert_id = str(expert_id_raw) if expert_id_raw is not None else Nonepass_id = str(payload.get("pass_id") or "")job_id = str(payload.get("job_id") or "")phase_models = _phase_models_from_payload(payload)@@ -303,19 +309,56 @@ async def handle_dream_batch_result(job_id, "batch payload missing user_id/pass_id/phase")if user_id:- await _release_lock(user_id, pass_id)+ await _release_lock(user_id, pass_id, expert_id)returnif phase not in NEXT_PHASE:logger.warning("Dream batch handler unknown phase=%r", phase)await _mark_job_errored_best_effort(job_id, f"unknown batch phase {phase!r}")- await _release_lock(user_id, pass_id)+ await _release_lock(user_id, pass_id, expert_id)return+ authoritative_user_id = user_id+ authoritative_expert_id = expert_idtry:+ input_bundle = await read_input_bundle(pass_id)+ if input_bundle is None:+ logger.error(+ "Dream batch input missing; refusing payload-only scope for pass=%s",+ pass_id,+ )+ await _fail_pass(+ user_id=user_id,+ expert_id=expert_id,+ pass_id=pass_id,+ job_id=job_id,+ phase_models=phase_models,+ error="batch DreamInput missing; memory scope unavailable",+ )+ return++ authoritative_user_id = input_bundle.user_id+ authoritative_expert_id = input_bundle.expert_id+ if user_id != authoritative_user_id or expert_id != authoritative_expert_id:+ logger.error(+ "Dream batch payload memory scope mismatch for pass=%s",+ pass_id,+ )+ await _fail_pass(+ user_id=authoritative_user_id,+ expert_id=authoritative_expert_id,+ pass_id=pass_id,+ job_id=job_id,+ phase_models=phase_models,+ error="batch payload memory scope mismatch",+ )+ return+await _handle_phase_result(rows=rows,user_id=user_id,+ expert_id=expert_id,+ input_bundle=input_bundle,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -334,7 +377,8 @@ async def handle_dream_batch_result()try:await _fail_pass(- user_id=user_id,+ user_id=authoritative_user_id,+ expert_id=authoritative_expert_id,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -343,17 +387,24 @@ async def handle_dream_batch_result(except Exception:logger.exception("Dream batch _fail_pass also failed for pass=%s", pass_id)try:- await _release_lock(user_id, pass_id)+ await _release_lock(+ authoritative_user_id,+ pass_id,+ authoritative_expert_id,+ )except Exception:logger.exception(- "Dream batch lock release failed for user=%s", user_id[:12]+ "Dream batch lock release failed for user=%s",+ authoritative_user_id[:12],)async def _handle_phase_result(*,- rows: list["BatchResultRow"],+ rows: list[BatchResultRow],user_id: str,+ expert_id: str | None = None,+ input_bundle: DreamInput,pass_id: str,job_id: str,phase_models: dict[str, str],@@ -372,6 +423,7 @@ async def _handle_phase_result(logger.warning("Dream batch handler got empty rows for pass=%s", pass_id)await _fail_pass(user_id=user_id,+ expert_id=expert_id,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -387,6 +439,7 @@ async def _handle_phase_result(await _write_phase_to_state(pass_id=pass_id, phase=phase, row=row)await _fail_pass(user_id=user_id,+ expert_id=expert_id,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -403,6 +456,7 @@ async def _handle_phase_result(await _write_phase_to_state(pass_id=pass_id, phase=phase, row=row)await _fail_pass(user_id=user_id,+ expert_id=expert_id,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -416,6 +470,8 @@ async def _handle_phase_result(if next_phase is not None:await _chain_next_phase(user_id=user_id,+ expert_id=expert_id,+ input_bundle=input_bundle,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -426,6 +482,8 @@ async def _handle_phase_result(# Terminal phase landed — apply + finalize.await _finalize_complete(user_id=user_id,+ expert_id=expert_id,+ input_bundle=input_bundle,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -440,6 +498,8 @@ async def _handle_phase_result(async def _chain_next_phase(*,user_id: str,+ expert_id: str | None = None,+ input_bundle: DreamInput,pass_id: str,job_id: str,phase_models: dict[str, str],@@ -447,22 +507,11 @@ async def _chain_next_phase() -> None:"""Submit the next phase in the chain.- Reads the persisted ``DreamInput`` + accumulated prior phase- outputs from Redis, builds the next phase's prompt, fires another+ Uses the validated ``DreamInput`` + accumulated prior phase outputs+ from Redis, builds the next phase's prompt, fires anotherbatch submission. On any failure to submit, marks the JobStatuserrored — silent submission failures are unrecoverable."""- input_bundle = await read_input_bundle(pass_id)- if input_bundle is None:- await _fail_pass(- user_id=user_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error=f"{next_phase}: DreamInput missing from Redis (TTL expired?)",- )- return-state = await _read_state(pass_id)consolidated_json = _content_for(state, "consolidate")recombined_json = _content_for(state, "recombine")@@ -471,6 +520,7 @@ async def _chain_next_phase(if api_key is None:await _fail_pass(user_id=user_id,+ expert_id=expert_id,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -498,6 +548,7 @@ async def _chain_next_phase()await _fail_pass(user_id=user_id,+ expert_id=expert_id,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -580,7 +631,13 @@ async def _claim_apply_gate(pass_id: str) -> Literal["claimed", "duplicate", "erasync def _finalize_complete(- *, user_id: str, pass_id: str, job_id: str, phase_models: dict[str, str]+ *,+ user_id: str,+ expert_id: str | None = None,+ input_bundle: DreamInput,+ pass_id: str,+ job_id: str,+ phase_models: dict[str, str],) -> None:"""Sanitize phase has landed. Run apply + cost log + complete."""try:@@ -594,6 +651,7 @@ async def _finalize_complete(logger.exception("Failed to import dream apply for pass=%s", pass_id)await _fail_pass(user_id=user_id,+ expert_id=expert_id,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -606,6 +664,7 @@ async def _finalize_complete(if sanitize_row is None or not sanitize_row.get("content"):await _fail_pass(user_id=user_id,+ expert_id=expert_id,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -618,6 +677,7 @@ async def _finalize_complete(except (json.JSONDecodeError, ValidationError) as exc:await _fail_pass(user_id=user_id,+ expert_id=expert_id,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -627,21 +687,15 @@ async def _finalize_complete(# Enforce the same per-pass operation caps the sync path applies# before writing — the model can over-emit past the prompt's limits.- # The 5%-of-active-facts demotion ceiling needs the original fact- # count; the persisted input bundle still exists here (deleted only- # after apply below). A missing bundle (-1) falls back to the- # absolute demotion cap rather than zeroing all demotions.- input_bundle = await read_input_bundle(pass_id)- active_fact_count = len(input_bundle.facts) if input_bundle is not None else -1+ # The 5%-of-active-facts demotion ceiling needs the original fact count.+ active_fact_count = len(input_bundle.facts)# Pass the known-fact allowlist so hallucinated demotion uuids are# filtered BEFORE the cap slice — otherwise they consume cap slots# and displace valid demotions (cap can floor at 1 on small graphs).ops = _clamp_operations(ops,active_fact_count,- known_fact_uuids=(- input_bundle.known_fact_uuids if input_bundle is not None else None- ),+ known_fact_uuids=input_bundle.known_fact_uuids,)# Batch results can re-dispatch (executor crash between dispatch and@@ -655,6 +709,7 @@ async def _finalize_complete(if gate == "error":await _fail_pass(user_id=user_id,+ expert_id=expert_id,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -678,7 +733,7 @@ async def _finalize_complete(await _log_all_phase_costs(user_id=user_id, pass_id=pass_id, state=state, phase_models=phase_models)- await _release_lock(user_id, pass_id)+ await _release_lock(user_id, pass_id, expert_id)await _best_effort_cleanup(pass_id)return@@ -686,11 +741,7 @@ async def _finalize_complete(str, int | str | IngestionDrainStatus | DreamOperationsSnapshot] = {}try:- # Thread the demotion allowlist from the bundle already in memory —- # letting apply re-read it would do a second Redis GET + full JSON- # deserialize and could fail open if the bundle's TTL lapses between- # the two reads. None (bundle expired) keeps apply's documented- # fail-open fallback.+ # Thread the demotion allowlist from the already-validated bundle.## Skip the ingestion drain on this path: apply runs inside this# handler, which BatchExecutor.walk_once awaits serially in its single@@ -701,9 +752,8 @@ async def _finalize_complete(user_id,pass_id,ops,- known_fact_uuids=(- input_bundle.known_fact_uuids if input_bundle is not None else None- ),+ expert_id=expert_id,+ known_fact_uuids=input_bundle.known_fact_uuids,ingestion_drain_timeout=BATCH_INGESTION_DRAIN_TIMEOUT_SECONDS,)except Exception as exc:@@ -713,6 +763,7 @@ async def _finalize_complete()await _fail_pass(user_id=user_id,+ expert_id=expert_id,pass_id=pass_id,job_id=job_id,phase_models=phase_models,@@ -784,13 +835,14 @@ async def _finalize_complete(# The batch path disowned the dream lock to this callback; release it now# that the pass has terminated so the next dream for this user can run.- await _release_lock(user_id, pass_id)+ await _release_lock(user_id, pass_id, expert_id)await _best_effort_cleanup(pass_id)async def _fail_pass(*,user_id: str,+ expert_id: str | None = None,pass_id: str,job_id: str,phase_models: dict[str, str],@@ -823,7 +875,7 @@ async def _fail_pass(phase_models=phase_models,)# Release the dream lock the batch path disowned to this callback.- await _release_lock(user_id, pass_id)+ await _release_lock(user_id, pass_id, expert_id)await _best_effort_cleanup(pass_id)diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.pyindex 1638cf2299..ff9fc3d225 100644--- a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py@@ -79,29 +79,36 @@ def fake_redis():def _entry(- *, pass_id: str = "p1", job_id: str = "j1", phase: str = "consolidate"+ *,+ pass_id: str = "p1",+ job_id: str = "j1",+ phase: str = "consolidate",+ expert_id: str | None = None,) -> PendingEntry:now = datetime.now(timezone.utc)custom_id = f"{pass_id}:{phase}"+ payload = {+ "user_id": "u1",+ "pass_id": pass_id,+ "job_id": job_id,+ "phase": phase,+ "phase_models": {+ "consolidate": "claude-sonnet-4-6",+ "recombine": "claude-opus-4-7",+ "sanitize": "claude-sonnet-4-6",+ },+ "custom_ids": [custom_id],+ "phase_for_custom_id": {custom_id: phase},+ }+ if expert_id is not None:+ payload["expert_id"] = expert_idreturn PendingEntry(provider="anthropic",provider_batch_id=f"msgbatch_{phase}",callback_namespace="dream_pass",submitted_at=now,next_poll_at=now,- payload={- "user_id": "u1",- "pass_id": pass_id,- "job_id": job_id,- "phase": phase,- "phase_models": {- "consolidate": "claude-sonnet-4-6",- "recombine": "claude-opus-4-7",- "sanitize": "claude-sonnet-4-6",- },- "custom_ids": [custom_id],- "phase_for_custom_id": {custom_id: phase},- },+ payload=payload,)@@ -124,7 +131,168 @@ _SANITIZE_CONTENT = ()+async def _persist_autopilot_bundle(pass_id: str = "p1") -> None:+ from backend.copilot.dream.batch_submit import persist_input_bundle+ from backend.copilot.dream.fetch import DreamInput++ now = datetime.now(timezone.utc)+ await persist_input_bundle(+ pass_id,+ DreamInput(+ user_id="u1",+ group_id="user_u1",+ window_start=now,+ window_end=now,+ ),+ )++class TestPhaseChaining:+ @pytest.mark.asyncio+ async def test_terminal_result_without_input_bundle_cannot_apply(+ self, fake_redis+ ) -> None:+ apply = AsyncMock()+ mark_errored = AsyncMock()+ release_lock = AsyncMock()++ with (+ patch("backend.copilot.dream.apply.apply_operations", apply),+ patch(+ "backend.copilot.dream.job_status.mark_errored",+ mark_errored,+ ),+ patch(+ "backend.copilot.dream.batch_callbacks.release_dream_lock",+ release_lock,+ ),+ ):+ await handle_dream_batch_result(+ _entry(pass_id="p-missing", phase="sanitize"),+ [+ _row(+ custom_id="p-missing:sanitize",+ content=_SANITIZE_CONTENT,+ )+ ],+ )++ apply.assert_not_awaited()+ assert (+ mark_errored.await_args.kwargs["error"]+ == "batch DreamInput missing; memory scope unavailable"+ )+ release_lock.assert_awaited_once_with("u1", None, None)++ @pytest.mark.asyncio+ async def test_autopilot_payload_cannot_apply_expert_dream(+ self, fake_redis+ ) -> None:+ from backend.copilot.dream.batch_submit import persist_input_bundle+ from backend.copilot.dream.fetch import DreamInput++ now = datetime.now(timezone.utc)+ await persist_input_bundle(+ "p-expert",+ DreamInput(+ user_id="u1",+ expert_id="expert-1",+ group_id="expert_resolved",+ window_start=now,+ window_end=now,+ ),+ lock_token="tok-expert",+ )+ apply = AsyncMock()+ mark_errored = AsyncMock()+ release_lock = AsyncMock()++ with (+ patch("backend.copilot.dream.apply.apply_operations", apply),+ patch(+ "backend.copilot.dream.job_status.mark_errored",+ mark_errored,+ ),+ patch(+ "backend.copilot.dream.batch_callbacks.release_dream_lock",+ release_lock,+ ),+ ):+ await handle_dream_batch_result(+ _entry(pass_id="p-expert", phase="sanitize"),+ [+ _row(+ custom_id="p-expert:sanitize",+ content=_SANITIZE_CONTENT,+ )+ ],+ )++ apply.assert_not_awaited()+ assert (+ mark_errored.await_args.kwargs["error"]+ == "batch payload memory scope mismatch"+ )+ release_lock.assert_awaited_once_with("u1", "tok-expert", "expert-1")++ @pytest.mark.asyncio+ async def test_cross_expert_payload_cannot_apply_other_expert_dream(+ self, fake_redis+ ) -> None:+ from backend.copilot.dream.batch_submit import persist_input_bundle+ from backend.copilot.dream.fetch import DreamInput++ now = datetime.now(timezone.utc)+ await persist_input_bundle(+ "p-expert",+ DreamInput(+ user_id="u1",+ expert_id="expert-1",+ group_id="expert_resolved",+ window_start=now,+ window_end=now,+ ),+ lock_token="tok-expert",+ )+ submit_phase = AsyncMock()+ mark_errored = AsyncMock()+ release_lock = AsyncMock()++ with (+ patch(+ "backend.copilot.dream.batch_callbacks.submit_phase",+ submit_phase,+ ),+ patch(+ "backend.copilot.dream.job_status.mark_errored",+ mark_errored,+ ),+ patch(+ "backend.copilot.dream.batch_callbacks.release_dream_lock",+ release_lock,+ ),+ ):+ await handle_dream_batch_result(+ _entry(+ pass_id="p-expert",+ phase="consolidate",+ expert_id="expert-2",+ ),+ [+ _row(+ custom_id="p-expert:consolidate",+ content=_CONSOLIDATE_CONTENT,+ )+ ],+ )++ submit_phase.assert_not_awaited()+ assert (+ mark_errored.await_args.kwargs["error"]+ == "batch payload memory scope mismatch"+ )+ release_lock.assert_awaited_once_with("u1", "tok-expert", "expert-1")+@pytest.mark.asyncioasync def test_consolidate_result_submits_recombine_batch(self, fake_redis):"""Phase 1 result must trigger phase 2 submission, NOT apply."""@@ -297,7 +465,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")+ release_lock.assert_awaited_once_with("u1", "tok-u1", None)# One cost-log row per phase (consolidate, recombine, sanitize)assert record_cost.await_count == 3for call in record_cost.await_args_list:@@ -312,6 +480,64 @@ class TestPhaseChaining:assert models_by_phase["recombine"] == "claude-opus-4-7"assert models_by_phase["sanitize"] == "claude-sonnet-4-6"+ @pytest.mark.asyncio+ async def test_expert_terminal_result_applies_and_releases_in_expert_scope(+ self, fake_redis+ ):+ from backend.copilot.dream.batch_callbacks import _write_phase_to_state+ from backend.copilot.dream.batch_submit import persist_input_bundle+ from backend.copilot.dream.fetch import DreamInput++ now = datetime.now(timezone.utc)+ await persist_input_bundle(+ "p-expert",+ DreamInput(+ user_id="u1",+ expert_id="expert-1",+ group_id="expert_resolved",+ window_start=now,+ window_end=now,+ ),+ lock_token="tok-expert",+ )+ await _write_phase_to_state(+ pass_id="p-expert",+ phase="consolidate",+ row=_row(custom_id="p-expert:consolidate", content=_CONSOLIDATE_CONTENT),+ )+ await _write_phase_to_state(+ pass_id="p-expert",+ phase="recombine",+ row=_row(custom_id="p-expert:recombine", content=_RECOMBINE_CONTENT),+ )++ apply = AsyncMock(+ return_value={+ "writes": 0,+ "ingestion_drain_status": IngestionDrainStatus.skipped,+ }+ )+ release_lock = AsyncMock()+ with (+ patch("backend.copilot.dream.apply.apply_operations", apply),+ patch("backend.copilot.dream.job_status.mark_complete", new=AsyncMock()),+ patch(+ "backend.copilot.dream.batch_callbacks.record_phase_cost",+ new=AsyncMock(),+ ),+ patch(+ "backend.copilot.dream.batch_callbacks.release_dream_lock",+ release_lock,+ ),+ ):+ await handle_dream_batch_result(+ _entry(pass_id="p-expert", phase="sanitize", expert_id="expert-1"),+ [_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")+@pytest.mark.asyncioasync def test_redispatch_after_charge_does_not_double_bill(self, fake_redis):"""If the BatchExecutor crashes between charging and@@ -438,7 +664,7 @@ class TestPhaseChaining:# safe no-op) and clean up state.assert release_lock.await_count == 2for call in release_lock.await_args_list:- assert call.args == ("u1", "tok-u1")+ assert call.args == ("u1", "tok-u1", None)@pytest.mark.asyncioasync def test_apply_gate_redis_outage_fails_pass_not_silent_success(@@ -450,6 +676,7 @@ class TestPhaseChaining:written, silently dropping the dream."""from backend.copilot.dream.batch_callbacks import _write_phase_to_state+ await _persist_autopilot_bundle()await _write_phase_to_state(pass_id="p1",phase="consolidate",@@ -530,6 +757,7 @@ class TestErrorPaths:@pytest.mark.asyncioasync def test_errored_row_short_circuits_to_mark_errored(self, fake_redis):+ await _persist_autopilot_bundle()mark_errored = AsyncMock()record_cost = AsyncMock()with patch(@@ -559,6 +787,7 @@ class TestErrorPaths:garbage. Pydantic's default ``model_validate`` is permissiveabout unknown fields (extra="ignore"), so the failure mode thetest pins is invalid-JSON not unknown-field."""+ await _persist_autopilot_bundle()submit_phase = AsyncMock()mark_errored = AsyncMock()record_cost = AsyncMock()@@ -665,7 +894,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")+ release_lock.assert_awaited_once_with("u1", "tok-u1", None)class TestMalformedPayload:@@ -684,7 +913,7 @@ class TestMalformedPayload:entry,[_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT)],)- release.assert_awaited_once_with("u1", None)+ release.assert_awaited_once_with("u1", None, None)@pytest.mark.asyncioasync def test_unknown_phase_label_releases_lock_with_persisted_token(@@ -711,7 +940,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")+ release.assert_awaited_once_with("u1", "tok-u1", None)class TestLockTokenWiring:@@ -803,7 +1032,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)+ release_lock.assert_awaited_once_with("u1", None, None)@pytest.mark.asyncioasync def test_cleanup_failure_after_complete_keeps_job_completed(self, fake_redis):diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_submit.py b/autogpt_platform/backend/backend/copilot/dream/batch_submit.pyindex 57458af60c..e891534c24 100644--- a/autogpt_platform/backend/backend/copilot/dream/batch_submit.py+++ b/autogpt_platform/backend/backend/copilot/dream/batch_submit.py@@ -155,6 +155,11 @@ async def submit_phase(job_id, and the per-phase model map so each phase is submitted —and later priced — with its own model."""+ # Each phase batch gets its own 24h provider SLA window, so a+ # multi-phase chain can legitimately outlive a TTL stamped once at the+ # first submit. Re-arm the bundle TTL at every submit so it stays alive+ # exactly as long as some phase batch may still call back.+ await refresh_input_bundle_ttl(pass_id)model = phase_models[phase]messages = _build_phase_messages(phase=phase,@@ -204,6 +209,7 @@ async def submit_phase(poll_delay_seconds=INITIAL_POLL_DELAY_SECONDS,payload={"user_id": user_id,+ "expert_id": input_bundle.expert_id,"pass_id": pass_id,"job_id": job_id,"phase": phase,@@ -327,7 +333,9 @@ async def persist_input_bundle(redis = await get_redis_async()payload = _input_bundle_to_dict(input_bundle)if lock_token is None:- lock_token = await read_dream_lock_token(input_bundle.user_id)+ lock_token = await read_dream_lock_token(+ input_bundle.user_id, input_bundle.expert_id+ )if lock_token is not None:payload["lock_token"] = lock_tokenawait redis.set(@@ -335,6 +343,32 @@ async def persist_input_bundle()+async def refresh_input_bundle_ttl(pass_id: str) -> None:+ """Reset the persisted input bundle's TTL to the full window.++ Best-effort: on the first phase submit the bundle was just persisted+ with a fresh TTL, and a genuinely lost bundle stays the callback's+ hard-fail guard's responsibility — so a missing key or a Redis blip+ here only logs.+ """+ from backend.data.redis_client import get_redis_async++ try:+ redis = await get_redis_async()+ refreshed = await redis.expire(input_bundle_key(pass_id), INPUT_TTL_SECONDS)+ except Exception:+ logger.warning(+ "Failed to refresh DreamInput TTL for pass=%s", pass_id, exc_info=True+ )+ return+ if not refreshed:+ logger.warning(+ "DreamInput missing at TTL refresh for pass=%s — the phase "+ "callback will fail closed if it stays gone",+ pass_id,+ )++async def read_lock_token(pass_id: str) -> str | None:"""Dream-lock ownership token persisted alongside the input bundle.@@ -387,6 +421,7 @@ async def delete_input_bundle(pass_id: str) -> None:def _input_bundle_to_dict(input_bundle: DreamInput) -> dict:return {"user_id": input_bundle.user_id,+ "expert_id": input_bundle.expert_id,"group_id": input_bundle.group_id,"window_start": input_bundle.window_start.isoformat(),"window_end": input_bundle.window_end.isoformat(),@@ -434,6 +469,7 @@ def _dict_to_input_bundle(data: dict) -> DreamInput:return DreamInput(user_id=data["user_id"],+ expert_id=data.get("expert_id"),group_id=data["group_id"],window_start=datetime.fromisoformat(data["window_start"]),window_end=datetime.fromisoformat(data["window_end"]),diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_submit_test.py b/autogpt_platform/backend/backend/copilot/dream/batch_submit_test.pyindex 8578717315..61a85a3976 100644--- a/autogpt_platform/backend/backend/copilot/dream/batch_submit_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/batch_submit_test.py@@ -15,6 +15,7 @@ from unittest.mock import AsyncMock, patchimport pytestfrom backend.copilot.dream.batch_submit import (+ INPUT_TTL_SECONDS,input_bundle_key,persist_input_bundle,read_input_bundle,@@ -26,10 +27,14 @@ from backend.copilot.dream.locks import DREAM_LOCK_KEY_PREFIXfrom backend.util.llm.providers import BatchSubmissionRef-def _bundle() -> DreamInput:+def _bundle(expert_id: str | None = None) -> DreamInput:now = datetime.now(timezone.utc)return DreamInput(- user_id="u1", group_id="user_u1", window_start=now, window_end=now+ user_id="u1",+ expert_id=expert_id,+ group_id="user_u1" if expert_id is None else "expert_resolved",+ window_start=now,+ window_end=now,)@@ -63,7 +68,7 @@ def fake_redis():@pytest.mark.asyncio-async def test_enqueue_failure_cancels_orphaned_batch_and_reraises():+async def test_enqueue_failure_cancels_orphaned_batch_and_reraises(fake_redis):submitted = BatchSubmissionRef(provider="anthropic",provider_batch_id="msgbatch_orphan",@@ -95,7 +100,7 @@ async def test_enqueue_failure_cancels_orphaned_batch_and_reraises():@pytest.mark.asyncio-async def test_successful_enqueue_does_not_cancel():+async def test_successful_enqueue_does_not_cancel(fake_redis):submitted = BatchSubmissionRef(provider="anthropic",provider_batch_id="msgbatch_ok",@@ -122,6 +127,57 @@ async def test_successful_enqueue_does_not_cancel():enqueue.assert_awaited_once()cancel.assert_not_awaited()assert ref.provider_batch_id == "msgbatch_ok"+ assert enqueue.call_args.args[0].payload["expert_id"] is None+++async def test_submit_phase_refreshes_input_bundle_ttl(fake_redis):+ """Each phase batch gets its own 24h SLA window, so the input bundle's+ TTL — stamped once at persist — must be re-armed on every phase submit+ or a multi-phase chain can outlive its bundle and hard-fail the paid+ dream at the callback's read_input_bundle guard."""+ stub, _ = fake_redis+ submitted = BatchSubmissionRef(+ provider="anthropic",+ provider_batch_id="msgbatch_chain",+ custom_id="p1_recombine",+ submitted_at=datetime.now(timezone.utc),+ )+ with patch(+ "backend.copilot.dream.batch_submit.call_provider",+ AsyncMock(return_value=submitted),+ ), patch(+ "backend.copilot.dream.batch_submit.enqueue_pending",+ AsyncMock(return_value=None),+ ):+ await submit_phase(+ user_id="u1",+ pass_id="p1",+ job_id="j1",+ phase="recombine",+ phase_models={"recombine": "claude-opus-4-6"},+ api_key="sk-test",+ input_bundle=_bundle(),+ consolidated_json="{}",+ )++ stub.expire.assert_awaited_once_with(input_bundle_key("p1"), INPUT_TTL_SECONDS)+++async def test_expert_scope_round_trips_and_routes_live_lock_lookup(fake_redis):+ read_live_token = AsyncMock(return_value="tok-expert")+ with patch(+ "backend.copilot.dream.batch_submit.read_dream_lock_token",+ read_live_token,+ ):+ await persist_input_bundle("p-expert", _bundle("expert-1"))++ read_live_token.assert_awaited_once_with("u1", "expert-1")+ bundle = await read_input_bundle("p-expert")+ assert bundle is not None+ assert bundle.expert_id == "expert-1"+ assert bundle.group_id == "expert_resolved"@pytest.mark.asynciodiff --git a/autogpt_platform/backend/backend/copilot/dream/fetch.py b/autogpt_platform/backend/backend/copilot/dream/fetch.pyindex 5d8b317760..5393ab6a7d 100644--- a/autogpt_platform/backend/backend/copilot/dream/fetch.py+++ b/autogpt_platform/backend/backend/copilot/dream/fetch.py@@ -18,7 +18,7 @@ from datetime import datetime, timedelta, timezonefrom pydantic import BaseModel, Field-from backend.copilot.graphiti.client import derive_group_id+from backend.copilot.graphiti.client import derive_memory_group_idfrom backend.copilot.graphiti.config import graphiti_configfrom backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriverfrom backend.data.db_accessors import chat_db@@ -64,6 +64,7 @@ class DreamInput(BaseModel):"""The whole gather-step bundle handed to the phase 1 prompt."""user_id: str+ expert_id: str | None = Nonegroup_id: strwindow_start: datetimewindow_end: datetime@@ -255,6 +256,7 @@ async def _fetch_recent_sessions(user_id: str,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.@@ -273,7 +275,14 @@ async def _fetch_recent_sessions("""_ = window_starttry:- sessions = await chat_db().get_user_chat_sessions(user_id, limit=limit)+ if 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+ )except Exception:logger.warning("Failed to fetch recent sessions for user %s",@@ -328,6 +337,7 @@ async def _fetch_recent_sessions(async def gather_dream_input(user_id: str,*,+ expert_id: str | None = None,window_days: int = DEFAULT_WINDOW_DAYS,max_episodes: int = MAX_EPISODES,max_facts: int = MAX_ACTIVE_FACTS,@@ -339,7 +349,7 @@ async def gather_dream_input(the three sources fails (Cypher error, Prisma timeout) the othersstill proceed — a partial dream is better than no dream."""- group_id = derive_group_id(user_id)+ group_id = derive_memory_group_id(user_id, expert_id)window_end = datetime.now(timezone.utc)window_start = window_end - timedelta(days=window_days)@@ -352,10 +362,13 @@ async def gather_dream_input(finally:await driver.close()- sessions = await _fetch_recent_sessions(user_id, window_start, max_sessions)+ sessions = await _fetch_recent_sessions(+ user_id, window_start, max_sessions, expert_id+ )return DreamInput(user_id=user_id,+ expert_id=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.pynew file mode 100644index 0000000000..9278593fbe--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/fetch_test.py@@ -0,0 +1,33 @@+from datetime import datetime, timezone+from types import SimpleNamespace+from unittest.mock import AsyncMock++import pytest++from . import fetch as fetch_mod+++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)++ database.get_user_chat_sessions.assert_awaited_once_with(+ "user-1", limit=10, autopilot_only=True+ )+++async def test_expert_dream_fetches_only_that_experts_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, "expert-1"+ )++ database.get_user_chat_sessions.assert_awaited_once_with(+ "user-1", limit=10, expert_id="expert-1"+ )diff --git a/autogpt_platform/backend/backend/copilot/dream/locks.py b/autogpt_platform/backend/backend/copilot/dream/locks.pyindex 74c46ace3b..a7f9db1cde 100644--- a/autogpt_platform/backend/backend/copilot/dream/locks.py+++ b/autogpt_platform/backend/backend/copilot/dream/locks.py@@ -31,6 +31,8 @@ import uuidfrom contextlib import asynccontextmanagerfrom typing import Any, cast+from backend.copilot.graphiti.client import derive_memory_scope_key+logger = logging.getLogger(__name__)DREAM_LOCK_KEY_PREFIX = "dream:inflight:"@@ -64,8 +66,9 @@ _EXTEND_SCRIPT = ()-def _lock_key(user_id: str) -> str:- return f"{DREAM_LOCK_KEY_PREFIX}{user_id}"+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):@@ -144,7 +147,12 @@ class DreamLockHandle:@asynccontextmanager-async def dream_lock(user_id: str, ttl_seconds: int = DEFAULT_LOCK_TTL_SECONDS):+async def dream_lock(+ user_id: str,+ ttl_seconds: int = DEFAULT_LOCK_TTL_SECONDS,+ *,+ expert_id: str | None = None,+):"""Acquire a per-user advisory lock for the dream pass.Raises ``DreamLockHeld`` if another process holds the lock.@@ -157,7 +165,7 @@ async def dream_lock(user_id: str, ttl_seconds: int = DEFAULT_LOCK_TTL_SECONDS):from backend.data.redis_client import get_redis_asyncredis = await get_redis_async()- key = _lock_key(user_id)+ key = _lock_key(user_id, expert_id)token = str(uuid.uuid4())acquired = await redis.set(key, token, nx=True, ex=ttl_seconds)@@ -197,7 +205,9 @@ async def dream_lock(user_id: str, ttl_seconds: int = DEFAULT_LOCK_TTL_SECONDS):)-async def read_dream_lock_token(user_id: str) -> str | None:+async def read_dream_lock_token(+ user_id: str, expert_id: str | None = None+) -> 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@@ -208,7 +218,7 @@ async def read_dream_lock_token(user_id: str) -> str | None:from backend.data.redis_client import get_redis_asyncredis = await get_redis_async()- raw = await redis.get(_lock_key(user_id))+ raw = await redis.get(_lock_key(user_id, expert_id))if raw is None:return Noneif isinstance(raw, bytes):@@ -216,7 +226,9 @@ async def read_dream_lock_token(user_id: str) -> str | None:return str(raw)-async def release_dream_lock(user_id: str, token: str | None) -> None:+async def release_dream_lock(+ user_id: str, token: str | None, expert_id: str | None = 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@@ -240,7 +252,13 @@ async def release_dream_lock(user_id: str, token: str | None) -> None:try:redis = await get_redis_async()deleted = await cast(- "Any", redis.eval(_UNLOCK_SCRIPT, 1, _lock_key(user_id), token)+ "Any",+ redis.eval(+ _UNLOCK_SCRIPT,+ 1,+ _lock_key(user_id, expert_id),+ token,+ ),)if deleted:logger.debug("Released disowned dream lock for user %s", user_id[:12])diff --git a/autogpt_platform/backend/backend/copilot/dream/locks_test.py b/autogpt_platform/backend/backend/copilot/dream/locks_test.pyindex 11f7b76a9c..6d0da71f14 100644--- a/autogpt_platform/backend/backend/copilot/dream/locks_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/locks_test.py@@ -75,6 +75,20 @@ async def test_dream_lock_raises_when_already_held(mocker):redis.delete.assert_not_awaited()+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"):+ pass++ key = redis.set.call_args.args[0]+ assert key.startswith("dream:inflight:expert_")+ assert key != "dream:inflight:user-a"+ assert redis.eval.call_args.args[2] == key++@pytest.mark.asyncioasync def test_dream_lock_swallows_release_failure(mocker, caplog):redis = _redis_mock(eval=AsyncMock(side_effect=Exception("redis down")))diff --git a/autogpt_platform/backend/backend/copilot/dream/orchestrator.py b/autogpt_platform/backend/backend/copilot/dream/orchestrator.pyindex 78299a9b8c..4eeb594814 100644--- a/autogpt_platform/backend/backend/copilot/dream/orchestrator.py+++ b/autogpt_platform/backend/backend/copilot/dream/orchestrator.py@@ -25,6 +25,7 @@ from datetime import datetime, timezonefrom typing import TypeVarfrom backend.copilot.config import ChatConfig+from backend.copilot.graphiti.client import derive_memory_scope_keyfrom backend.util.feature_flag import Flag, is_feature_enabledfrom .apply import apply_operations, drain_status_from_stats@@ -640,11 +641,14 @@ def _aggregate_usage()-def _last_completed_key(user_id: str) -> str:- return f"{LAST_COMPLETED_KEY_PREFIX}{user_id}"+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) -> datetime | None:+async def _read_last_completed_marker(+ user_id: str, expert_id: str | None = None+) -> datetime | None:"""When the user's last dream pass completed, or ``None``.Best-effort: a Redis error or an unparseable value fails open@@ -657,7 +661,7 @@ async def _read_last_completed_marker(user_id: str) -> datetime | None:try:redis = await get_redis_async()- raw = await redis.get(_last_completed_key(user_id))+ raw = await redis.get(_last_completed_key(user_id, expert_id))except Exception:logger.warning("Failed to read dream last-completed marker for user %s — "@@ -681,7 +685,9 @@ async def _read_last_completed_marker(user_id: str) -> datetime | None:return marker if marker.tzinfo else marker.replace(tzinfo=timezone.utc)-async def _stamp_last_completed_marker(user_id: str, as_of: datetime) -> None:+async def _stamp_last_completed_marker(+ user_id: str, as_of: datetime, expert_id: str | None = None+) -> None:"""Record the upper bound of the episode window the pass consolidated.``as_of`` must be the gather snapshot time (``DreamInput.window_end``),@@ -698,7 +704,7 @@ async def _stamp_last_completed_marker(user_id: str, as_of: datetime) -> None:try:redis = await get_redis_async()await redis.set(- _last_completed_key(user_id),+ _last_completed_key(user_id, expert_id),as_of.isoformat(),ex=LAST_COMPLETED_TTL_SECONDS,)@@ -734,6 +740,7 @@ def _has_new_episodes_since(episodes: list[EpisodeRow], marker: datetime) -> booasync def _execute_dream_pass_async(user_id: str,*,+ expert_id: str | None = None,transport_is_local: bool = False,config: ChatConfig | None = None,status_id: str | None = None,@@ -767,7 +774,8 @@ async def _execute_dream_pass_async(ttl = _resolve_lock_ttl(transport_is_local)try:- async with dream_lock(user_id, ttl_seconds=ttl) as dream_lock_handle:+ lock_context = dream_lock(user_id, ttl_seconds=ttl, expert_id=expert_id)+ 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# concurrent pass on a shared FalkorDB.@@ -793,7 +801,7 @@ async def _execute_dream_pass_async(skip_reason=budget_skip or "insufficient_credits",)- input_bundle = await gather_dream_input(user_id)+ input_bundle = await gather_dream_input(user_id, expert_id=expert_id)if not input_bundle.episodes and not input_bundle.facts:# Nothing to consolidate — early-return as skipped so the@@ -822,7 +830,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)+ await _read_last_completed_marker(user_id, expert_id)if status_id is Noneelse None)@@ -962,6 +970,7 @@ async def _execute_dream_pass_async(user_id,pass_id,ops,+ expert_id=expert_id,known_fact_uuids=input_bundle.known_fact_uuids,lock_handle=dream_lock_handle,)@@ -971,7 +980,9 @@ 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)+ await _stamp_last_completed_marker(+ user_id, input_bundle.window_end, expert_id+ )completed_at = datetime.now(timezone.utc)snapshot = apply_stats.get("snapshot")@@ -1057,7 +1068,10 @@ def _failure_result(async def execute_dream_pass(- user_id: str, *, status_id: str | None = None+ user_id: str,+ *,+ status_id: str | None = None,+ expert_id: str | None = None,) -> DreamPassResult:"""Public async entry point used by the scheduler + admin trigger.@@ -1067,7 +1081,9 @@ async def execute_dream_pass(user-visible status row as each phase lands. AgentProbe + othersync callers pass ``None`` and never hit the batch path."""- return await _execute_dream_pass_async(user_id, status_id=status_id)+ return await _execute_dream_pass_async(+ user_id, status_id=status_id, expert_id=expert_id+ )async def _submit_dream_pass_batch(diff --git a/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py b/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.pyindex a51806a0ce..f1c8c5d5fa 100644--- a/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py@@ -173,6 +173,61 @@ async def test_empty_input_returns_skipped(mocker):apply_mock.assert_not_awaited()+async def test_expert_scope_is_threaded_to_lock_and_gather(mocker):+ lock_calls: list[tuple[tuple, dict]] = []++ @asynccontextmanager+ async def scoped_lock(*args, **kwargs):+ lock_calls.append((args, kwargs))+ yield++ mocker.patch.object(orchestrator_mod, "dream_lock", scoped_lock)+ gather = mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=_build_input(episodes=0, facts=0)),+ )++ 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")+++async def test_expert_scope_reaches_marker_and_apply_on_nonempty_pass(mocker):+ input_bundle = _build_input().model_copy(+ update={"expert_id": "expert-1", "group_id": "expert_scope"}+ )+ gather = mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=input_bundle),+ )+ read_marker = mocker.patch.object(+ orchestrator_mod,+ "_read_last_completed_marker",+ AsyncMock(return_value=None),+ )+ stamp_marker = mocker.patch.object(+ orchestrator_mod,+ "_stamp_last_completed_marker",+ AsyncMock(),+ )+ apply_mock = _stub_three_phases_and_apply(mocker)++ result = await orchestrator_mod.execute_dream_pass("u", expert_id="expert-1")++ 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")++@pytest.mark.asyncio@pytest.mark.parametrize("flag_on", [True, False])async def test_batch_path_gated_by_flag_not_key(mocker, flag_on):@@ -510,7 +565,13 @@ async def test_clamps_oversized_sanitizer_output(mocker):captured: dict[str, DreamOperations] = {}async def fake_apply(- user_id, pass_id, ops, *, known_fact_uuids=None, lock_handle=None+ user_id,+ pass_id,+ ops,+ *,+ expert_id=None,+ known_fact_uuids=None,+ lock_handle=None,):captured["ops"] = opsreturn {@@ -573,7 +634,13 @@ async def test_demotions_capped_at_five_percent_of_active_facts(mocker):captured: dict[str, DreamOperations] = {}async def fake_apply(- user_id, pass_id, ops, *, known_fact_uuids=None, lock_handle=None+ user_id,+ pass_id,+ ops,+ *,+ expert_id=None,+ known_fact_uuids=None,+ lock_handle=None,):captured["ops"] = opsreturn {@@ -685,7 +752,13 @@ async def test_sync_path_filters_hallucinated_demotion_before_cap(mocker):captured: dict[str, DreamOperations] = {}async def fake_apply(- user_id, pass_id, ops, *, known_fact_uuids=None, lock_handle=None+ user_id,+ pass_id,+ ops,+ *,+ expert_id=None,+ known_fact_uuids=None,+ lock_handle=None,):captured["ops"] = opsreturn {diff --git a/autogpt_platform/backend/backend/copilot/dream/ratification.py b/autogpt_platform/backend/backend/copilot/dream/ratification.pyindex 98675fe824..691a70be6d 100644--- a/autogpt_platform/backend/backend/copilot/dream/ratification.py+++ b/autogpt_platform/backend/backend/copilot/dream/ratification.py@@ -23,7 +23,7 @@ from typing import Anyfrom pydantic import BaseModel, Field-from backend.copilot.graphiti.client import derive_group_id+from backend.copilot.graphiti.client import derive_memory_group_idfrom backend.copilot.graphiti.config import graphiti_configfrom backend.copilot.graphiti.falkordb_driver import AutoGPTFalkorDriverfrom backend.copilot.tools.graphiti_forget import mark_edges_superseded@@ -69,7 +69,9 @@ class RatificationResult(BaseModel):per_edge_errors: list[str] = Field(default_factory=list)-async def run_ratification_pass(user_id: str) -> RatificationResult:+async def run_ratification_pass(+ user_id: str, expert_id: str | None = None+) -> RatificationResult:"""Promote or supersede every ``status='tentative'`` edge for one user.Defensive in two ways:@@ -83,7 +85,7 @@ async def run_ratification_pass(user_id: str) -> RatificationResult:result = RatificationResult(user_id=user_id, started_at=started_at)try:- group_id = derive_group_id(user_id)+ group_id = derive_memory_group_id(user_id, expert_id)except ValueError as exc:result.error = f"invalid_user_id: {exc}"result.completed_at = datetime.now(timezone.utc)@@ -128,6 +130,8 @@ async def run_ratification_pass(user_id: str) -> RatificationResult:try:await _process_edge(user_id=user_id,+ expert_id=expert_id,+ group_id=group_id,driver=driver,edge=edge,now=now,@@ -168,6 +172,8 @@ async def run_ratification_pass(user_id: str) -> RatificationResult:async def _process_edge(*,user_id: str,+ expert_id: str | None,+ group_id: str,driver: AutoGPTFalkorDriver,edge: dict[str, Any],now: datetime,@@ -185,7 +191,7 @@ async def _process_edge(result.per_edge_errors.append("missing_uuid")return- hits = await _get_hit_count(user_id, edge_uuid)+ hits = await _get_hit_count(user_id, edge_uuid, expert_id)if hits >= 1:promoted = await _promote_edge(driver, edge_uuid)@@ -203,15 +209,21 @@ async def _process_edge(if now - created_at <= RATIFICATION_GRACE_PERIOD:return- succeeded, _failed = await mark_edges_superseded(+ succeeded, failed = await mark_edges_superseded(driver,[edge_uuid],reason="unratified",new_status="superseded",user_id=user_id,+ group_id=group_id,)if succeeded:result.superseded_count += 1+ # Surface non-matches too: an edge without a group_id property (legacy+ # write) matches nothing under the group-scoped predicate and would+ # otherwise be silently re-examined by every future sweep.+ for failed_uuid in failed:+ result.per_edge_errors.append(f"{failed_uuid}: supersede_failed")async def _list_tentative_edges(@@ -252,7 +264,12 @@ 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]) -> int:+async def try_ratify_on_hit(+ user_id: str,+ edge_uuids: list[str],+ *,+ expert_id: str | None = None,+) -> int:"""Record warm-context hits and promote any tentative edges inline.Called from warm-context retrieval (``graphiti/context.py``) once@@ -286,14 +303,14 @@ async def try_ratify_on_hit(user_id: str, edge_uuids: list[str]) -> int:# 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)+ await record_memory_hit(user_id, uuid, expert_id)# 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_group_id(user_id)+ 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@@ -357,5 +374,7 @@ 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) -> int:- return await get_hit_count(user_id, edge_uuid)+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)diff --git a/autogpt_platform/backend/backend/copilot/dream/ratification_hits.py b/autogpt_platform/backend/backend/copilot/dream/ratification_hits.pyindex a70e55a892..9e27680150 100644--- a/autogpt_platform/backend/backend/copilot/dream/ratification_hits.py+++ b/autogpt_platform/backend/backend/copilot/dream/ratification_hits.py@@ -19,6 +19,8 @@ import loggingfrom datetime import datetime, timedelta, timezonefrom typing import Any+from backend.copilot.graphiti.client import derive_memory_scope_key+logger = logging.getLogger(__name__)# Grace period a tentative edge has to earn a warm-context hit before@@ -32,7 +34,9 @@ RATIFICATION_GRACE_PERIOD = timedelta(days=30)HIT_TRACKER_KEY_PREFIX = "mem:hits"-async def record_memory_hit(user_id: str, edge_uuid: str) -> None:+async def record_memory_hit(+ user_id: str, edge_uuid: str, expert_id: str | None = None+) -> None:"""INCR the Redis hit counter for one tentative edge.Called from any code path that surfaces a tentative edge to the@@ -49,7 +53,7 @@ async def record_memory_hit(user_id: str, edge_uuid: str) -> None:from backend.data.redis_client import get_redis_asyncredis = await get_redis_async()- key = hit_key(user_id, edge_uuid)+ key = hit_key(user_id, edge_uuid, expert_id)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@@ -67,13 +71,15 @@ async def record_memory_hit(user_id: str, edge_uuid: str) -> None:)-async def get_hit_count(user_id: str, edge_uuid: str) -> int:+async def get_hit_count(+ user_id: str, edge_uuid: str, expert_id: str | None = None+) -> int:"""Read the Redis hit counter for one edge. Missing key → 0."""try:from backend.data.redis_client import get_redis_asyncredis = await get_redis_async()- raw = await redis.get(hit_key(user_id, edge_uuid))+ raw = await redis.get(hit_key(user_id, edge_uuid, expert_id))if raw is None:return 0if isinstance(raw, bytes):@@ -91,8 +97,9 @@ async def get_hit_count(user_id: str, edge_uuid: str) -> int:return 0-def hit_key(user_id: str, edge_uuid: str) -> str:- return f"{HIT_TRACKER_KEY_PREFIX}:{user_id}:{edge_uuid}"+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:diff --git a/autogpt_platform/backend/backend/copilot/dream/ratification_test.py b/autogpt_platform/backend/backend/copilot/dream/ratification_test.pyindex 83487e5bc9..da124c348c 100644--- a/autogpt_platform/backend/backend/copilot/dream/ratification_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/ratification_test.py@@ -84,9 +84,11 @@ def fake_redis(mocker):class FakeRedis:def __init__(self):self.hits: dict[str, int] = {}+ self.get_calls: list[str] = []self.expire_calls: list[tuple[str, int]] = []async def get(self, key: str):+ self.get_calls.append(key)# Keys are ``mem:hits:{user_id}:{edge_uuid}`` — split off the uuid.edge_uuid = key.rsplit(":", 1)[-1]value = self.hits.get(edge_uuid, 0)@@ -172,6 +174,33 @@ async def test_tentative_edge_without_hits_past_grace_is_superseded_as_unratifieassert call.kwargs["new_status"] == "superseded"+async def test_supersede_failure_is_reported_not_silently_dropped(mocker, fake_redis):+ """An edge the group-scoped supersede can't match (legacy write without a+ group_id property, or a query error) must land in per_edge_errors —+ otherwise it is silently re-examined by every future sweep forever."""+ edge = {+ "uuid": "edge-legacy",+ "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,+ "mark_edges_superseded",+ AsyncMock(side_effect=lambda driver, uuids, **kw: ([], list(uuids))),+ )++ result = await run_ratification_pass("u-legacy")++ assert result.superseded_count == 0+ assert len(result.per_edge_errors) == 1+ assert "edge-legacy" in result.per_edge_errors[0]+ assert "supersede_failed" in result.per_edge_errors[0]++@pytest.mark.asyncioasync def test_tentative_edge_within_grace_without_hits_is_untouched(mocker, fake_redis, stub_mark_superseded@@ -198,6 +227,29 @@ async def test_tentative_edge_within_grace_without_hits_is_untouched(assert promote_calls == []+async def test_expert_ratification_uses_expert_graph_and_scoped_hit_key(+ mocker, fake_redis, stub_mark_superseded+):+ edge = {+ "uuid": "edge-expert",+ "created_at": _days_ago(RATIFICATION_GRACE_PERIOD.days + 2),+ }+ driver = _make_driver(records_for_list=[edge])+ driver_factory = MagicMock(return_value=driver)+ mocker.patch.object(ratification_mod, "AutoGPTFalkorDriver", 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"]+ 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"+ )++def test_grace_period_is_thirty_days_per_spec():"""Spec §5: tentative edges get 30 days to earn a hit before theyare superseded. Pin the constant so a shorter window (which would@@ -268,8 +320,9 @@ async def test_empty_user_with_no_tentative_edges_returns_zero_counts_no_error(@pytest.mark.asyncio[email protected]("expert_id", [None, "expert-poison"])async def test_per_edge_failure_does_not_kill_the_rest_of_the_pass(- mocker, fake_redis, stub_mark_superseded+ mocker, fake_redis, stub_mark_superseded, expert_id):"""A poison-pill edge raises mid-pass; the others still get processed,and the failure is captured in ``per_edge_errors`` rather than raised."""@@ -290,16 +343,18 @@ async def test_per_edge_failure_does_not_kill_the_rest_of_the_pass(# 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) -> int:+ async def hit_count_with_poison(+ user_id: str, edge_uuid: str, expert_id: str | None = None+ ) -> int:if edge_uuid == "edge-poison":raise RuntimeError("simulated redis explosion")- return await original_get_hit_count(user_id, edge_uuid)+ return await original_get_hit_count(user_id, edge_uuid, expert_id)mocker.patch.object(ratification_mod, "_get_hit_count", side_effect=hit_count_with_poison)- result = await run_ratification_pass("u-mixed")+ result = await run_ratification_pass("u-mixed", expert_id=expert_id)assert result.examined_count == 3# The hot edge got ratified, the stale edge got superseded, the@@ -308,6 +363,10 @@ async def test_per_edge_failure_does_not_kill_the_rest_of_the_pass(assert result.superseded_count == 1assert len(result.per_edge_errors) == 1assert "edge-poison" in result.per_edge_errors[0]+ assert fake_redis.get_calls == [+ hit_key("u-mixed", "edge-good-hot", expert_id),+ hit_key("u-mixed", "edge-good-stale", expert_id),+ ]# ---------------------------------------------------------------------------diff --git a/autogpt_platform/backend/backend/copilot/executor/processor_test.py b/autogpt_platform/backend/backend/copilot/executor/processor_test.pyindex c39bf933ea..6325077ecf 100644--- a/autogpt_platform/backend/backend/copilot/executor/processor_test.py+++ b/autogpt_platform/backend/backend/copilot/executor/processor_test.py@@ -28,6 +28,10 @@ from backend.copilot.executor.processor import (sync_fail_close_session,)from backend.copilot.executor.utils import CoPilotExecutionEntry, CoPilotLogMetadata+from backend.copilot.expert_context import (+ EXPERT_SESSION_MISSING_MESSAGE,+ ExpertSessionUnavailableError,+)from backend.copilot.model import ChatSessionfrom backend.copilot.rate_limit import UserPaywalledErrorfrom backend.integrations.codex.transport import (@@ -340,6 +344,100 @@ class TestExecuteAsyncAclose:assert published.aclose_called is True+ @pytest.mark.asyncio+ async def test_persisted_expert_session_is_passed_to_engine(self) -> None:+ published = _TrackedStream(events=[])+ session = ChatSession.new(+ "user-1",+ dry_run=False,+ expert_id="expert-1",+ )+ session.session_id = "sess-1"+ stream_fn = MagicMock(return_value=MagicMock())++ with (+ patch(+ "backend.copilot.executor.processor.ChatConfig",+ return_value=MagicMock(test_mode=True),+ ),+ patch(+ "backend.copilot.executor.processor.stream_chat_completion_dummy",+ stream_fn,+ ),+ patch(+ "backend.copilot.executor.processor.stream_registry.stream_and_publish",+ return_value=published,+ ),+ patch(+ "backend.copilot.executor.processor.stream_registry.mark_session_completed",+ new=AsyncMock(),+ ),+ patch(+ "backend.copilot.model.get_chat_session",+ new=AsyncMock(return_value=session),+ ) as get_session_mock,+ ):+ await CoPilotProcessor()._execute_async(+ _make_entry(),+ threading.Event(),+ MagicMock(),+ _make_log(),+ )++ get_session_mock.assert_awaited_once_with("sess-1", "user-1")+ assert stream_fn.call_args.kwargs["session"] is session+ assert stream_fn.call_args.kwargs["session"].expert_id == "expert-1"++ @pytest.mark.asyncio+ async def test_missing_expert_publishes_actionable_stream_error(self) -> None:+ async def failing_expert_stream():+ raise ExpertSessionUnavailableError(EXPERT_SESSION_MISSING_MESSAGE)+ yield MagicMock()++ stream = failing_expert_stream()+ mark_completed = AsyncMock()+ session = ChatSession.new("user-1", dry_run=False, expert_id="expert-1")+ session.session_id = "sess-1"++ with (+ patch(+ "backend.copilot.executor.processor.ChatConfig",+ return_value=MagicMock(test_mode=True),+ ),+ patch(+ "backend.copilot.executor.processor.stream_chat_completion_dummy",+ return_value=stream,+ ),+ patch(+ "backend.copilot.executor.processor.wrap_stream_with_heartbeat",+ return_value=stream,+ ),+ patch(+ "backend.copilot.executor.processor.stream_registry.stream_and_publish",+ return_value=stream,+ ),+ patch(+ "backend.copilot.executor.processor.stream_registry.mark_session_completed",+ new=mark_completed,+ ),+ patch(+ "backend.copilot.model.get_chat_session",+ new=AsyncMock(return_value=session),+ ),+ pytest.raises(ExpertSessionUnavailableError),+ ):+ await CoPilotProcessor()._execute_async(+ _make_entry(),+ threading.Event(),+ MagicMock(),+ _make_log(),+ )++ mark_completed.assert_awaited_once_with(+ "sess-1",+ error_message=EXPERT_SESSION_MISSING_MESSAGE,+ )+def _codex_entry(*,diff --git a/autogpt_platform/backend/backend/copilot/expert_context.py b/autogpt_platform/backend/backend/copilot/expert_context.pyindex 3654be1888..095e348183 100644--- a/autogpt_platform/backend/backend/copilot/expert_context.py+++ b/autogpt_platform/backend/backend/copilot/expert_context.py@@ -11,13 +11,16 @@ Two layers with different prompt weights:should prefer ``run_agent`` on) or ``<team_context>`` (plain session:hired experts the model can suggest, never silently delegate to).-Everything here degrades silently to ``""`` — chat must never hard-fail on-expert lookup (archived/missing expert, DB error, no hired experts).+Expert identity lookup fails closed for an expert-scoped session: if its+persisted expert is missing, archived, or unavailable, the turn raises+``ExpertSessionUnavailableError`` instead of silently running as AutoPilot.+Plain-session team context and expert workflow context still degrade to ``""``.Returned strings carry their own separators so callers can concatenatedirectly (suffix: leading ``\\n\\n``; message blocks: trailing ``\\n\\n``)."""+import asyncioimport loggingfrom backend.api.features.experts.models import PROTECTED_SOUL_RULES, Expert@@ -26,29 +29,45 @@ from backend.data.db_accessors import experts_dblogger = logging.getLogger(__name__)+class ExpertSessionUnavailableError(RuntimeError):+ """The persisted expert scope cannot safely supply its identity."""+++EXPERT_SESSION_MISSING_MESSAGE = (+ "This expert is no longer available. Please start a new chat."+)+EXPERT_SESSION_TEMPORARY_MESSAGE = (+ "This expert is temporarily unavailable. Please try again."+)+_EXPERT_LOOKUP_RETRY_DELAY_SECONDS = 0.1+++def escape_prompt_xml_tags(value: str) -> str:+ return value.replace("<", "<").replace(">", ">")++async def build_expert_identity_suffix(user_id: str | None, expert_id: str | None) -> str:"""Build the ``<expert_identity>`` system-prompt suffix for an expertsession.- Returns ``""`` for plain sessions (keeps the system prompt byte-identical- for cross-user caching) and on any lookup failure.+ Returns ``""`` for plain sessions, keeping the system prompt byte-identical+ for cross-user caching. Expert-scoped sessions fail closed when their+ identity cannot be loaded.Runs on every turn, so it skips the workflow joins — only the expert'sown name/role/identity columns are read here."""- if not user_id or not expert_id:+ if expert_id is None:return ""- try:- expert = await experts_db().get_expert(- user_id, expert_id, include_workflows=False+ if not user_id:+ raise ExpertSessionUnavailableError(+ "Expert session identity is unavailable without an authenticated user.")- except Exception as e:- logger.warning(f"Failed to build expert identity suffix: {e}")- return ""+ expert = await _load_expert_identity(user_id, expert_id)if expert is None or expert.is_archived:- return ""+ raise ExpertSessionUnavailableError(EXPERT_SESSION_MISSING_MESSAGE)name = escape_prompt_xml_tags(expert.name)identity = escape_prompt_xml_tags(expert.identity)@@ -62,7 +81,6 @@ async def build_expert_identity_suffix(f"<identity_and_personality>\n{identity}\n</identity_and_personality>\n"f"<voice_preferences>\n{voice}\n</voice_preferences>\n"f"<boundaries>\n{boundaries}\n</boundaries>\n"- f"<what_ive_learned>\n- Nothing recorded yet.\n</what_ive_learned>\n"f"<protected_rules>\n{protected_rules}\n</protected_rules>\n"f"The base instructions above describe AutoPilot, the platform "f"engine you run on. All platform capabilities and tools remain "@@ -73,8 +91,25 @@ async def build_expert_identity_suffix()-def escape_prompt_xml_tags(value: str) -> str:- return value.replace("<", "<").replace(">", ">")+async def _load_expert_identity(user_id: str, expert_id: str) -> Expert | None:+ for attempt in range(2):+ try:+ return await experts_db().get_expert(+ user_id, expert_id, include_workflows=False+ )+ except Exception as error:+ if attempt == 0:+ logger.warning(+ "Expert identity lookup failed; retrying once",+ exc_info=True,+ )+ await asyncio.sleep(_EXPERT_LOOKUP_RETRY_DELAY_SECONDS)+ continue+ logger.warning("Expert identity lookup failed after retry", exc_info=True)+ raise ExpertSessionUnavailableError(+ EXPERT_SESSION_TEMPORARY_MESSAGE+ ) from error+ raise AssertionError("Expert identity lookup retry loop did not return")def fence_voice_preferences(voice: str) -> str:@@ -118,8 +153,8 @@ async def build_expert_context(user_id: str | None, expert_id: str | None) -> stasync def _expert_session_context(user_id: str, expert_id: str) -> str:expert = await experts_db().get_expert(user_id, expert_id)- # Archived/missing expert at stream time → omit the block and let the- # turn proceed as a plain Autopilot session.+ # Identity validation already failed closed before this context lookup.+ # If the expert changes between those reads, omit only this optional block.if expert is None or expert.is_archived:return ""diff --git a/autogpt_platform/backend/backend/copilot/expert_context_test.py b/autogpt_platform/backend/backend/copilot/expert_context_test.pyindex 7f1f9ae4c3..1d2eb3da9b 100644--- a/autogpt_platform/backend/backend/copilot/expert_context_test.py+++ b/autogpt_platform/backend/backend/copilot/expert_context_test.py@@ -5,8 +5,8 @@ Covers:<expert_workflows> in the first user message — never the other way round.- Plain sessions render a <team_context> block listing hired experts, andproduce an empty suffix so the system prompt stays byte-identical.-- Archived/missing expert, no hired experts, and lookup errors all degrade- silently to "" — chat must never hard-fail on expert lookup.+- Archived/missing expert sessions fail closed, transient identity lookup errors+ retry once, and optional team/workflow context still degrades silently to "".- inject_user_context() wires the message blocks in without touching thecacheable base prompt (byte-identical, verified via SHA-256 snapshot)."""@@ -21,7 +21,12 @@ from backend.api.features.experts.models import (Expert,ExpertWorkflowRef,)-from backend.copilot.expert_context import build_expert_identity_suffix+from backend.copilot.expert_context import (+ EXPERT_SESSION_MISSING_MESSAGE,+ EXPERT_SESSION_TEMPORARY_MESSAGE,+ ExpertSessionUnavailableError,+ build_expert_identity_suffix,+)_EC = "backend.copilot.expert_context"@@ -105,22 +110,81 @@ class TestBuildExpertIdentitySuffix:assert result == ""@pytest.mark.asyncio- async def test_archived_expert_returns_empty(self):+ async def test_expert_session_without_user_fails_before_lookup(self):+ db_factory = MagicMock()+ with (+ patch(f"{_EC}.experts_db", db_factory),+ pytest.raises(+ ExpertSessionUnavailableError,+ match="authenticated user",+ ),+ ):+ await build_expert_identity_suffix("", "exp-1")++ db_factory.assert_not_called()++ @pytest.mark.asyncio+ async def test_archived_expert_hidden_by_accessor_raises(self):+ mock_db = MagicMock()+ mock_db.get_expert = AsyncMock(return_value=None)+ with (+ patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)),+ pytest.raises(+ ExpertSessionUnavailableError,+ match="start a new chat",+ ),+ ):+ await build_expert_identity_suffix("user-1", "exp-1")++ @pytest.mark.asyncio+ async def test_archived_expert_returned_by_accessor_raises(self):mock_db = MagicMock()mock_db.get_expert = AsyncMock(return_value=_expert(is_archived=True))- with patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)):+ with (+ patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)),+ pytest.raises(+ ExpertSessionUnavailableError,+ match="start a new chat",+ ),+ ):+ await build_expert_identity_suffix("user-1", "exp-1")++ @pytest.mark.asyncio+ async def test_transient_lookup_error_is_retried_once(self):+ mock_db = MagicMock()+ mock_db.get_expert = AsyncMock(side_effect=[RuntimeError("db down"), _expert()])+ sleep_mock = AsyncMock()+ with (+ patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)),+ patch(f"{_EC}.asyncio.sleep", new=sleep_mock),+ ):result = await build_expert_identity_suffix("user-1", "exp-1")- assert result == ""+ assert "<expert_identity>" in result+ assert mock_db.get_expert.await_count == 2+ sleep_mock.assert_awaited_once_with(0.1)@pytest.mark.asyncio- async def test_lookup_error_returns_empty(self):+ async def test_lookup_error_after_retry_raises_friendly_error(self):mock_db = MagicMock()mock_db.get_expert = AsyncMock(side_effect=RuntimeError("db down"))- with patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)):- result = await build_expert_identity_suffix("user-1", "exp-1")-- assert result == ""+ with (+ patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)),+ patch(f"{_EC}.asyncio.sleep", new=AsyncMock()),+ pytest.raises(+ ExpertSessionUnavailableError,+ match="temporarily unavailable.*try again",+ ) as exc_info,+ ):+ await build_expert_identity_suffix("user-1", "exp-1")++ assert str(exc_info.value) == EXPERT_SESSION_TEMPORARY_MESSAGE+ assert mock_db.get_expert.await_count == 2++ def test_missing_error_is_actionable_for_stream_clients(self):+ assert EXPERT_SESSION_MISSING_MESSAGE == (+ "This expert is no longer available. Please start a new chat."+ )@pytest.mark.asyncioasync def test_latest_soul_fields_and_protected_rules_are_rendered(self):@@ -139,7 +203,8 @@ class TestBuildExpertIdentitySuffix:assert "I help teams find the clearest strategy." in resultassert "Warm, concise, and direct." in resultassert "Never invent customer evidence." in result- assert "Nothing recorded yet." in result+ assert "<what_ive_learned>" not in result+ assert "Nothing recorded yet." not in resultassert "discloses that it is AI" in resultassert "External actions require approval" in resultdiff --git a/autogpt_platform/backend/backend/copilot/graphiti/client.py b/autogpt_platform/backend/backend/copilot/graphiti/client.pyindex 1181fe9437..2c5c4e4b48 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/client.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/client.py@@ -1,6 +1,7 @@"""Graphiti client management with per-group_id isolation and LRU caching."""import asyncio+import hashlibimport loggingimport reimport weakref@@ -75,6 +76,51 @@ def derive_group_id(user_id: str) -> str:return group_id+def derive_memory_group_id(user_id: str, expert_id: str | None = None) -> str:+ """Derive the Graphiti namespace for an AutoPilot or expert session.++ Plain AutoPilot sessions retain the exact legacy ``user_<user_id>``+ namespace so all existing user memories remain available. Expert sessions+ use a fixed-length digest of the globally unique Expert ID, so memory stays+ with the expert if authorized ownership changes later. Access remains a+ separate server-side authorization concern; experts are owner-only today.+ """+ user_group_id = derive_group_id(user_id)+ if expert_id is None:+ return user_group_id+ if not expert_id:+ raise ValueError("expert_id must be non-empty to derive memory group_id")++ safe_expert_id = re.sub(r"[^a-zA-Z0-9_-]", "", expert_id)+ if not safe_expert_id:+ raise ValueError(+ f"expert_id '{expert_id[:32]}...' yields empty group_id after sanitization"+ )+ if safe_expert_id != expert_id:+ raise ValueError(+ "expert_id contains invalid characters for group_id derivation "+ f"(original length={len(expert_id)}, "+ f"sanitized='{safe_expert_id[:32]}'). "+ "Only [a-zA-Z0-9_-] are allowed."+ )++ scope_digest = hashlib.sha256(expert_id.encode()).hexdigest()+ return f"expert_{scope_digest}"+++def derive_memory_scope_key(user_id: str, expert_id: str | None = None) -> str:+ """Stable internal key for queues, locks, and background markers.++ AutoPilot keeps the legacy raw user key where existing Redis contracts use+ it. Expert scopes use the same opaque ID as their Graphiti namespace.+ """+ if expert_id is None:+ # Validate the opaque user ID while preserving the legacy raw Redis key.+ derive_group_id(user_id)+ return user_id+ return derive_memory_group_id(user_id, expert_id)++def _close_client_driver(client) -> None:"""Best-effort close of a Graphiti client's graph driver.diff --git a/autogpt_platform/backend/backend/copilot/graphiti/client_test.py b/autogpt_platform/backend/backend/copilot/graphiti/client_test.pyindex cc51039112..ad8a2e5123 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/client_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/client_test.py@@ -7,7 +7,13 @@ import pytestfrom backend.copilot.config import ChatConfigfrom . import client as client_mod-from .client import derive_group_id, evict_client, make_flex_graphiti_client+from .client import (+ derive_group_id,+ derive_memory_group_id,+ derive_memory_scope_key,+ evict_client,+ make_flex_graphiti_client,+)class TestDeriveGroupId:@@ -37,6 +43,70 @@ class TestDeriveGroupId:assert result == "user_a-b_c"+class TestDeriveMemoryGroupId:+ def test_autopilot_preserves_exact_legacy_namespace(self) -> None:+ user_id = "883cc9da-fe37-4863-839b-acba022bf3ef"++ assert derive_memory_group_id(user_id) == derive_group_id(user_id)+ assert derive_memory_group_id(user_id) == f"user_{user_id}"++ def test_experts_get_distinct_private_namespaces(self) -> None:+ first = derive_memory_group_id("user-1", "expert-1")+ second = derive_memory_group_id("user-1", "expert-2")++ assert first.startswith("expert_")+ assert second.startswith("expert_")+ assert first != second+ assert len(first) <= 128++ def test_same_expert_keeps_namespace_across_callers(self) -> None:+ assert derive_memory_group_id("user-1", "expert-1") == derive_memory_group_id(+ "user-2", "expert-1"+ )++ def test_different_experts_remain_distinct_across_callers(self) -> None:+ assert derive_memory_group_id("user-1", "expert-1") != derive_memory_group_id(+ "user-2", "expert-2"+ )++ def test_expert_namespace_still_validates_caller_id(self) -> None:+ with pytest.raises(ValueError, match="invalid characters"):+ derive_memory_group_id("user.with.dots", "expert-1")++ def test_expert_namespace_is_deterministic(self) -> None:+ assert derive_memory_group_id("user-1", "expert-1") == derive_memory_group_id(+ "user-1", "expert-1"+ )++ def test_long_valid_expert_id_is_hashed_without_truncation(self) -> None:+ expert_id = "expert_" + "a" * 256++ result = derive_memory_group_id("user-1", expert_id)++ assert result.startswith("expert_")+ assert len(result) == len("expert_") + 64++ def test_empty_expert_id_raises(self) -> None:+ with pytest.raises(ValueError, match="expert_id must be non-empty"):+ derive_memory_group_id("user-1", "")++ def test_invalid_expert_id_raises(self) -> None:+ with pytest.raises(ValueError, match="invalid characters"):+ derive_memory_group_id("user-1", "expert.with.dots")++ def test_internal_scope_key_preserves_legacy_autopilot_key(self) -> None:+ assert derive_memory_scope_key("user-1") == "user-1"++ def test_internal_scope_key_validates_autopilot_user_id(self) -> None:+ with pytest.raises(ValueError, match="invalid characters"):+ derive_memory_scope_key("user.with.dots")++ def test_internal_scope_key_uses_expert_namespace_for_experts(self) -> None:+ assert derive_memory_scope_key("user-1", "expert-1") == derive_memory_group_id(+ "user-1", "expert-1"+ )++class TestEvictClient:@pytest.mark.asyncioasync def test_evict_nonexistent_group_id_does_not_raise(self) -> None:diff --git a/autogpt_platform/backend/backend/copilot/graphiti/communities.py b/autogpt_platform/backend/backend/copilot/graphiti/communities.pyindex 332279ce97..b85ba22211 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/communities.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/communities.py@@ -39,7 +39,7 @@ from backend.data.redis_client import get_redis_asyncfrom .client import (close_graphiti_client,- derive_group_id,+ derive_memory_group_id,get_graphiti_client,make_flex_graphiti_client,)@@ -270,7 +270,10 @@ async def _release_rebuild_lock(redis, key: str, token: str) -> None:async def rebuild_communities_for_user(- user_id: str, *, force: bool = False+ user_id: str,+ *,+ expert_id: str | None = None,+ force: bool = False,) -> dict[str, Any]:"""Destroy and rebuild ``:Community`` nodes for one user's graph.@@ -303,7 +306,7 @@ async def rebuild_communities_for_user(}try:- group_id = derive_group_id(user_id)+ group_id = derive_memory_group_id(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])diff --git a/autogpt_platform/backend/backend/copilot/graphiti/communities_integration_test.py b/autogpt_platform/backend/backend/copilot/graphiti/communities_integration_test.pyindex 0a28346b17..10f347e792 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/communities_integration_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/communities_integration_test.py@@ -71,7 +71,7 @@ async def test_rebuild_returns_structured_result_on_failure(# so we have to point the rebuild at the actual fixture group_id by# patching derive_group_id for this call only.with patch(- "backend.copilot.graphiti.communities.derive_group_id",+ "backend.copilot.graphiti.communities.derive_memory_group_id",return_value=group_id,):result = await rebuild_communities_for_user(user_id)@@ -128,7 +128,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_group_id",+ "backend.copilot.graphiti.communities.derive_memory_group_id",return_value=group_id,):await rebuild_communities_for_user(user_id, force=True)@@ -184,7 +184,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_group_id",+ "backend.copilot.graphiti.communities.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.pyindex 33524d0d00..d8b66151ee 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/communities_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/communities_test.py@@ -229,6 +229,45 @@ class TestRebuildCommunitiesForUser:assert "DETACH DELETE c" in cleanup_queryclient.build_communities.assert_awaited_once()+ @pytest.mark.asyncio+ async def test_expert_scope_uses_resolved_group_for_client_and_lock(+ self, _free_rebuild_lock+ ) -> None:+ driver = AsyncMock()+ driver.execute_query.return_value = ([], None, None)+ client = MagicMock()+ client.graph_driver = driver+ client.build_communities = AsyncMock(return_value=[])+ 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,+ ),+ patch(+ "backend.copilot.graphiti.communities.close_graphiti_client",+ new=AsyncMock(),+ ),+ ):+ result = await rebuild_communities_for_user(+ "883cc9da-fe37-4863-839b-acba022bf3ef",+ expert_id="expert-1",+ force=True,+ )++ assert result["error"] is None+ derive_group.assert_called_once_with(+ "883cc9da-fe37-4863-839b-acba022bf3ef", "expert-1"+ )+ make_client.assert_awaited_once_with("expert_resolved_group")+ lock_key = _free_rebuild_lock.set.call_args.args[0]+ assert lock_key == "graphiti:community_rebuild_lock:expert_resolved_group"+@pytest.mark.asyncioasync def test_failure_path_returns_error_in_result(self) -> None:driver = AsyncMock()diff --git a/autogpt_platform/backend/backend/copilot/graphiti/context.py b/autogpt_platform/backend/backend/copilot/graphiti/context.pyindex 293e3dd853..1df4643698 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/context.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/context.py@@ -11,14 +11,16 @@ from ._format import (extract_fact,extract_temporal_validity,)-from .client import derive_group_id, get_graphiti_client+from .client import derive_memory_group_id, get_graphiti_clientfrom .config import graphiti_configlogger = logging.getLogger(__name__)-async def fetch_warm_context(user_id: str, message: str) -> str | None:- """Fetch relevant temporal context for the current user and message.+async def fetch_warm_context(+ user_id: str, message: str, expert_id: str | None = None+) -> str | None:+ """Fetch relevant temporal context for the current memory owner and message.Called at the start of a session (first turn) to pre-load facts fromprior conversations. Returns a formatted ``<temporal_context>`` block@@ -32,7 +34,7 @@ async def fetch_warm_context(user_id: str, message: str) -> str | None:try:return await asyncio.wait_for(- _fetch(user_id, message),+ _fetch(user_id, message, expert_id),timeout=graphiti_config.context_timeout,)except asyncio.TimeoutError:@@ -46,14 +48,16 @@ async def fetch_warm_context(user_id: str, message: str) -> str | None:return None-async def _fetch(user_id: str, message: str) -> str | None:+async def _fetch(+ user_id: str, message: str, expert_id: str | None = None+) -> 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_group_id(user_id)+ group_id = derive_memory_group_id(user_id, expert_id)client = await get_graphiti_client(group_id)# P-1.4: warm context is the single most-impactful retrieval per@@ -87,7 +91,7 @@ async def _fetch(user_id: str, message: str) -> str | None:# 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, edges)+ _spawn_ratification_hits(user_id, expert_id, edges)if not edges and not episodes:return None@@ -111,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, edges) -> None:+def _spawn_ratification_hits(user_id: str, expert_id: str | None, edges) -> None:"""Fire-and-forget the ratification hit-hook for retrieved edges.Imports lazily so the dream/ratification module isn't pulled into@@ -126,7 +130,7 @@ def _spawn_ratification_hits(user_id: str, edges) -> None:from backend.copilot.dream.ratification import try_ratify_on_hittask = asyncio.create_task(- try_ratify_on_hit(user_id, edge_uuids),+ try_ratify_on_hit(user_id, edge_uuids, expert_id=expert_id),name=f"ratify-hits-{user_id[:12]}",)_pending_hit_tasks.add(task)diff --git a/autogpt_platform/backend/backend/copilot/graphiti/context_test.py b/autogpt_platform/backend/backend/copilot/graphiti/context_test.pyindex 62d6363795..48b73ded5b 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/context_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/context_test.py@@ -25,7 +25,7 @@ class TestFetchWarmContextTimeout:async def test_returns_none_on_timeout(self, monkeypatch: pytest.MonkeyPatch) -> None:- async def _slow_fetch(user_id: str, message: str) -> str:+ async def _slow_fetch(user_id: str, message: str, expert_id: str | None) -> str:await asyncio.sleep(10)return "<temporal_context>data</temporal_context>"@@ -43,7 +43,7 @@ class TestFetchWarmContextGeneralError:with (patch.object(context,- "derive_group_id",+ "derive_memory_group_id",return_value="user_abc",),patch.object(@@ -85,7 +85,7 @@ class TestFetchInternal:mock_client.retrieve_episodes.return_value = []with (- patch.object(context, "derive_group_id", return_value="user_abc"),+ patch.object(context, "derive_memory_group_id", return_value="user_abc"),patch.object(context,"get_graphiti_client",@@ -97,6 +97,36 @@ class TestFetchInternal:assert result is None+ @pytest.mark.asyncio+ async def test_expert_scope_is_used_for_all_retrievals(self) -> None:+ mock_client = AsyncMock()+ mock_client.search_.return_value = _search_results([])+ mock_client.retrieve_episodes.return_value = []++ with (+ patch.object(+ context,+ "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"+ ]+ assert mock_client.retrieve_episodes.await_args.kwargs["group_ids"] == [+ "expert_private_group"+ ]+@pytest.mark.asyncioasync def test_returns_context_with_edges(self) -> None:edge = SimpleNamespace(@@ -110,7 +140,7 @@ class TestFetchInternal:mock_client.retrieve_episodes.return_value = []with (- patch.object(context, "derive_group_id", return_value="user_abc"),+ patch.object(context, "derive_memory_group_id", return_value="user_abc"),patch.object(context,"get_graphiti_client",@@ -135,7 +165,7 @@ class TestFetchInternal:mock_client.retrieve_episodes.return_value = [ep]with (- patch.object(context, "derive_group_id", return_value="user_abc"),+ patch.object(context, "derive_memory_group_id", return_value="user_abc"),patch.object(context,"get_graphiti_client",@@ -161,7 +191,7 @@ class TestFetchInternal:mock_client.retrieve_episodes.return_value = []with (- patch.object(context, "derive_group_id", return_value="user_abc"),+ patch.object(context, "derive_memory_group_id", return_value="user_abc"),patch.object(context,"get_graphiti_client",@@ -338,7 +368,7 @@ class TestRatificationHitHookFiresFireAndForget:return AsyncMock()monkeypatch.setattr(context.asyncio, "create_task", fake_create_task)- context._spawn_ratification_hits("user-abc", edges=[])+ context._spawn_ratification_hits("user-abc", None, edges=[])assert created_tasks == []def test_spawn_helper_creates_task_with_retrieved_uuids(@@ -347,10 +377,15 @@ class TestRatificationHitHookFiresFireAndForget:"""Edges with uuid attrs → fire-and-forget task scheduled withall of their uuids. Edges missing a uuid are filtered out sothe hook never passes ``None`` to the ratification module."""- captured_calls: list[tuple[str, list[str]]] = []+ captured_calls: list[tuple[str, list[str], str | None]] = []- async def fake_try_ratify(user_id: str, edge_uuids: list[str]):- captured_calls.append((user_id, edge_uuids))+ 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))from backend.copilot.dream import ratification as ratification_mod@@ -365,15 +400,16 @@ class TestRatificationHitHookFiresFireAndForget:SimpleNamespace(uuid=None), # filteredSimpleNamespace(), # no uuid attr at all → filtered]- context._spawn_ratification_hits("user-xyz", edges=edges)+ context._spawn_ratification_hits("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 = captured_calls[0]+ user_id, uuids, expert_id = captured_calls[0]assert user_id == "user-xyz"assert uuids == ["edge-a", "edge-b"]+ assert expert_id == "expert-1"class TestRatificationHitTaskRetention:@@ -391,12 +427,17 @@ class TestRatificationHitTaskRetention:context._pending_hit_tasks.clear()release = asyncio.Event()- async def fake_try_ratify(user_id: str, edge_uuids: list[str]):+ async def fake_try_ratify(+ user_id: str,+ edge_uuids: list[str],+ *,+ expert_id: str | None = None,+ ):await release.wait()monkeypatch.setattr(ratification_mod, "try_ratify_on_hit", fake_try_ratify)context._spawn_ratification_hits(- "user-xyz", edges=[SimpleNamespace(uuid="edge-a")]+ "user-xyz", None, edges=[SimpleNamespace(uuid="edge-a")])# Strong ref held while the task is in flight.assert len(context._pending_hit_tasks) == 1@@ -420,12 +461,17 @@ class TestRatificationHitTaskRetention:async def driver():context._pending_hit_tasks.clear()- async def fake_try_ratify(user_id: str, edge_uuids: list[str]):+ async def fake_try_ratify(+ user_id: str,+ edge_uuids: list[str],+ *,+ expert_id: str | None = None,+ ):raise RuntimeError("falkordb down")monkeypatch.setattr(ratification_mod, "try_ratify_on_hit", fake_try_ratify)context._spawn_ratification_hits(- "user-xyz", edges=[SimpleNamespace(uuid="edge-a")]+ "user-xyz", None, 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.pyindex e61b431ceb..df8856b2a0 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@@ -229,20 +229,22 @@ def stub_boundaries(@pytest_asyncio.fixture(loop_scope="function", autouse=True)async def cancel_ingestion_workers() -> AsyncIterator[None]:- """Stop the per-user ingestion worker before the test's loop closes.+ """Stop per-memory-group ingestion workers before the test loop closes.- ``_ensure_worker`` spawns a task that idles for 60s waiting for more+ ``_enqueue_payload`` spawns a task that idles for 60s waiting for moreepisodes. Left alone it outlives the test and pytest reports "Task wasdestroyed but it is pending". Reaching into ``_get_loop_state`` is theonly handle on it — the registry is loop-local by design."""yieldstate = ingest_mod._get_loop_state()- workers = list(state.user_workers.values())+ workers = list(state.group_workers.values())for worker in workers:worker.cancel()if workers:await asyncio.gather(*workers, return_exceptions=True)+ assert not state.group_workers+ assert not state.group_queues# --- Helpers ----------------------------------------------------------------diff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest.pyindex ec6c993080..ab9192a89c 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/ingest.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/ingest.py@@ -1,8 +1,8 @@-"""Async episode ingestion with per-user serialization.+"""Async episode ingestion with per-memory-namespace serialization.graphiti-core requires sequential ``add_episode()`` calls within the same-group_id. This module provides a per-user asyncio.Queue that serializes-ingestion while keeping it fire-and-forget from the caller's perspective.+group_id. This module provides one asyncio.Queue per resolved memory group so+AutoPilot and each hired expert serialize their own writes independently."""import asyncio@@ -12,7 +12,7 @@ from datetime import datetime, timezonefrom graphiti_core.nodes import EpisodeType-from .client import derive_group_id, get_graphiti_client+from .client import derive_memory_group_id, get_graphiti_clientfrom .memory_model import MemoryEnvelope, MemoryKind, MemoryStatus, SourceKindfrom .types import EDGE_TYPE_MAP, EDGE_TYPES, ENTITY_TYPES@@ -26,11 +26,11 @@ logger = logging.getLogger(__name__)# different loop". Scope the registry per running loop so each loop has its# own queues, workers, and lock. Entries auto-clean when the loop is GC'd.class _LoopIngestState:- __slots__ = ("user_queues", "user_workers", "workers_lock")+ __slots__ = ("group_queues", "group_workers", "workers_lock")def __init__(self) -> None:- self.user_queues: dict[str, asyncio.Queue] = {}- self.user_workers: dict[str, asyncio.Task] = {}+ self.group_queues: dict[str, asyncio.Queue] = {}+ self.group_workers: dict[str, asyncio.Task] = {}self.workers_lock = asyncio.Lock()@@ -52,10 +52,19 @@ def _get_loop_state() -> _LoopIngestState:_WORKER_IDLE_TIMEOUT = 60+class MemoryScopeViolationError(ValueError):+ """An ingestion payload targeted a different memory group than its worker.++ This is the worker's only isolation check — every payload that would+ cross an AutoPilot/expert memory boundary funnels through it — so it is+ raised (and logged) distinctly from transient ingestion failures.+ """++class IngestionCompletion:"""Tracks completion of a specific set of enqueued episodes.- The per-user ingestion queue is SHARED between live-chat ingestion and+ A memory group's ingestion queue is shared between live-chat ingestion anddream-pass writes. A caller that must wait for only its own episodes toland (dream-pass apply) creates one of these, passes it to each``enqueue_episode`` it makes, and awaits it. Unrelated activity on the@@ -232,11 +241,13 @@ async def _stamp_edge_metadata()-async def _ingestion_worker(user_id: str, queue: asyncio.Queue) -> None:- """Process episodes sequentially for a single user.+async def _ingestion_worker(user_id: str, group_id: str, queue: asyncio.Queue) -> None:+ """Process episodes sequentially for one resolved memory namespace.Exits after ``_WORKER_IDLE_TIMEOUT`` seconds of inactivity so that- idle workers don't leak memory indefinitely.+ idle workers don't leak memory indefinitely. ``group_id`` is resolved by+ the enqueuer and is never re-derived here; this is what prevents an expert+ write from falling back into the owning user's AutoPilot graph."""# Snapshot the loop-local state at task start so cleanup always runs# against the same state dict the worker was registered in, even if the@@ -249,7 +260,23 @@ async def _ingestion_worker(user_id: str, queue: asyncio.Queue) -> None:queue.get(), timeout=_WORKER_IDLE_TIMEOUT)except asyncio.TimeoutError:- break # idle — clean up below+ # Coordinate retirement with enqueue. Without this shared+ # lock, an enqueuer can retain this queue just before we+ # unregister it, then put work onto a queue with no worker.+ async with state.workers_lock:+ registered_queue = state.group_queues.get(group_id)+ registered_worker = state.group_workers.get(group_id)+ current_worker = asyncio.current_task()+ if registered_queue is not queue or (+ registered_worker is not None+ and registered_worker is not current_worker+ ):+ return+ if not queue.empty():+ continue+ state.group_queues.pop(group_id, None)+ state.group_workers.pop(group_id, None)+ return# Sidecar completion tracker — present only when the enqueuer# (dream-pass apply) needs to await this specific episode. Popped@@ -257,7 +284,10 @@ async def _ingestion_worker(user_id: str, queue: asyncio.Queue) -> None:# graph write raises. See ``IngestionCompletion``.completion: IngestionCompletion | None = payload.pop("_completion", None)try:- group_id = derive_group_id(user_id)+ if payload.get("group_id") != group_id:+ raise MemoryScopeViolationError(+ "Ingestion payload memory group mismatch"+ )client = await get_graphiti_client(group_id)# ``_edge_metadata`` is a sidecar (not an add_episode kwarg) —# pop it before the **payload spread. Present only for dream@@ -283,6 +313,15 @@ async def _ingestion_worker(user_id: str, queue: asyncio.Queue) -> None:await _stamp_edge_metadata(client, group_id, result, edge_metadata, user_id)+ except MemoryScopeViolationError:+ logger.error(+ "MEMORY ISOLATION VIOLATION: ingestion payload for user %s "+ "targeted group %r but the worker owns group %r — "+ "episode dropped",+ user_id[:12],+ payload.get("group_id"),+ group_id,+ )except Exception:logger.warning("Graphiti ingestion failed for user %s",@@ -300,9 +339,22 @@ async def _ingestion_worker(user_id: str, queue: asyncio.Queue) -> None:logger.debug("Ingestion worker cancelled for user %s", user_id[:12])raisefinally:- # Clean up so the next message re-creates the worker.- state.user_queues.pop(user_id, None)- state.user_workers.pop(user_id, None)+ # Cancellation and unexpected exits also clean up, but only when this+ # task still owns the registered queue. A replacement worker may have+ # been installed after normal idle retirement.+ async with state.workers_lock:+ if (+ state.group_queues.get(group_id) is queue+ and state.group_workers.get(group_id) is asyncio.current_task()+ ):+ if queue.empty():+ state.group_queues.pop(group_id, None)+ state.group_workers.pop(group_id, None)+ else:+ state.group_workers[group_id] = asyncio.create_task(+ _ingestion_worker(user_id, group_id, queue),+ name=f"graphiti-ingest-{group_id[:12]}",+ )async def enqueue_conversation_turn(@@ -310,6 +362,7 @@ async def enqueue_conversation_turn(session_id: str,user_msg: str,assistant_msg: str = "",+ expert_id: str | None = None,) -> None:"""Enqueue a conversation turn for async background ingestion.@@ -325,9 +378,9 @@ async def enqueue_conversation_turn(returntry:- group_id = derive_group_id(user_id)+ group_id = derive_memory_group_id(user_id, expert_id)except ValueError:- logger.warning("Invalid user_id for ingestion: %s", user_id[:12])+ logger.warning("Invalid memory scope for ingestion: %s", user_id[:12])returnuser_display_name = await _resolve_user_name(user_id)@@ -341,21 +394,20 @@ async def enqueue_conversation_turn(source_description = f"User message in session {session_id}"- queue = await _ensure_worker(user_id)-- try:- queue.put_nowait(- {- "name": episode_name,- "episode_body": episode_body_for_graphiti,- "source": EpisodeType.message,- "source_description": source_description,- "reference_time": datetime.now(timezone.utc),- "group_id": group_id,- "custom_extraction_instructions": CUSTOM_EXTRACTION_INSTRUCTIONS,- }- )- except asyncio.QueueFull:+ queued = await _enqueue_payload(+ user_id,+ group_id,+ {+ "name": episode_name,+ "episode_body": episode_body_for_graphiti,+ "source": EpisodeType.message,+ "source_description": source_description,+ "reference_time": datetime.now(timezone.utc),+ "group_id": group_id,+ "custom_extraction_instructions": CUSTOM_EXTRACTION_INSTRUCTIONS,+ },+ )+ if not queued:logger.warning("Graphiti ingestion queue full for user %s — dropping episode",user_id[:12],@@ -375,20 +427,19 @@ async def enqueue_conversation_turn(status=MemoryStatus.tentative,provenance=f"session:{session_id}",)- try:- queue.put_nowait(- {- "name": f"finding_{session_id}",- "episode_body": envelope.model_dump_json(),- "source": EpisodeType.json,- "source_description": f"Assistant-derived finding in session {session_id}",- "reference_time": datetime.now(timezone.utc),- "group_id": group_id,- "custom_extraction_instructions": CUSTOM_EXTRACTION_INSTRUCTIONS,- }- )- except asyncio.QueueFull:- pass # user canonical episode already queued — finding is best-effort+ await _enqueue_payload(+ user_id,+ group_id,+ {+ "name": f"finding_{session_id}",+ "episode_body": envelope.model_dump_json(),+ "source": EpisodeType.json,+ "source_description": f"Assistant-derived finding in session {session_id}",+ "reference_time": datetime.now(timezone.utc),+ "group_id": group_id,+ "custom_extraction_instructions": CUSTOM_EXTRACTION_INSTRUCTIONS,+ },+ )async def enqueue_episode(@@ -401,11 +452,12 @@ 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.Used by ``MemoryStoreTool`` so that explicit memory-store calls go- through the same per-user serialization queue as conversation turns.+ through the same memory-group serialization queue as conversation turns.Args:is_json: When ``True``, ingest as ``EpisodeType.json`` (for@@ -421,7 +473,7 @@ async def enqueue_episode(completion: Optional ``IngestionCompletion`` the worker signals oncethis episode is processed. Dream-pass apply passes one so it canawait ONLY its own episodes (scoped drain), not everything on the- shared per-user queue. ``None`` for chat / memory-store writes+ shared memory-group queue. ``None`` for chat / memory-store writesthat are pure fire-and-forget.Returns ``True`` if the episode was queued, ``False`` if it was dropped.@@ -433,9 +485,9 @@ async def enqueue_episode(return Falsetry:- group_id = derive_group_id(user_id)+ group_id = derive_memory_group_id(user_id, expert_id)except ValueError:- logger.warning("Invalid user_id for episode ingestion: %s", user_id[:12])+ logger.warning("Invalid memory scope for episode ingestion: %s", user_id[:12])return Falsebody_bytes = len(episode_body.encode("utf-8"))@@ -447,35 +499,33 @@ async def enqueue_episode()return False- queue = await _ensure_worker(user_id)-source = EpisodeType.json if is_json else EpisodeType.text- try:- queue.put_nowait(- {- "name": name,- "episode_body": episode_body,- "source": source,- "source_description": source_description,- "reference_time": datetime.now(timezone.utc),- "group_id": group_id,- "custom_extraction_instructions": CUSTOM_EXTRACTION_INSTRUCTIONS,- # Sidecar — popped by the worker before the add_episode- # spread; carries dream metadata for post-write stamping.- "_edge_metadata": edge_metadata,- # Sidecar — the worker calls ``complete_one`` on it after- # processing so a scoped-drain caller can await this episode.- "_completion": completion,- }- )- return True- except asyncio.QueueFull:+ queued = await _enqueue_payload(+ user_id,+ group_id,+ {+ "name": name,+ "episode_body": episode_body,+ "source": source,+ "source_description": source_description,+ "reference_time": datetime.now(timezone.utc),+ "group_id": group_id,+ "custom_extraction_instructions": CUSTOM_EXTRACTION_INSTRUCTIONS,+ # Sidecar — popped by the worker before the add_episode+ # spread; carries dream metadata for post-write stamping.+ "_edge_metadata": edge_metadata,+ # Sidecar — the worker calls ``complete_one`` on it after+ # processing so a scoped-drain caller can await this episode.+ "_completion": completion,+ },+ )+ if not queued:logger.warning("Graphiti ingestion queue full for user %s — dropping episode",user_id[:12],)- return False+ return queuedasync def wait_for_ingestion(@@ -490,7 +540,7 @@ async def wait_for_ingestion(apply) register each episode on ``completion`` and await this.Scoped to exactly the episodes registered on ``completion`` — the- per-user queue is shared with live-chat ingestion, so waiting on the+ memory-group queue is shared with live-chat ingestion, so waiting on thewhole queue would let unrelated chat activity extend (or never resolve)the wait. ``IngestionCompletion`` decouples the barrier from queueordering: it resolves as soon as the caller's own episodes are done,@@ -503,12 +553,12 @@ async def wait_for_ingestion(return await completion.wait(timeout_seconds)-async def _ensure_worker(user_id: str) -> asyncio.Queue:- """Create a queue and worker for *user_id* if one doesn't exist.+async def _enqueue_payload(user_id: str, group_id: str, payload: dict) -> bool:+ """Atomically select a group worker and enqueue one payload.- Returns the queue directly so callers don't need to look it up from- the state dict (which avoids a TOCTOU race if the worker times out- and cleans up between this call and the put_nowait).+ Queue selection, worker creation, and ``put_nowait`` share+ ``workers_lock`` with idle retirement. A caller can therefore never put+ work onto a queue after its worker has unregistered it.Also fires the auto-registration of the user's dream-systemschedules (community rebuild + dream pass + ratification pass) the@@ -519,19 +569,26 @@ async def _ensure_worker(user_id: str) -> asyncio.Queue:ingestion is never affected."""state = _get_loop_state()- is_new_user_for_this_process = False+ is_new_group_for_this_process = Falseasync with state.workers_lock:- if user_id not in state.user_queues:- q: asyncio.Queue = asyncio.Queue(maxsize=100)- state.user_queues[user_id] = q- state.user_workers[user_id] = asyncio.create_task(- _ingestion_worker(user_id, q),- name=f"graphiti-ingest-{user_id[:12]}",+ queue = state.group_queues.get(group_id)+ if queue is None:+ queue = asyncio.Queue(maxsize=100)+ state.group_queues[group_id] = queue+ is_new_group_for_this_process = True++ worker = state.group_workers.get(group_id)+ if worker is None or worker.done():+ state.group_workers[group_id] = asyncio.create_task(+ _ingestion_worker(user_id, group_id, queue),+ name=f"graphiti-ingest-{group_id[:12]}",)- is_new_user_for_this_process = True- queue = state.user_queues[user_id]+ try:+ queue.put_nowait(payload)+ except asyncio.QueueFull:+ return False- if is_new_user_for_this_process:+ if is_new_group_for_this_process:# Fire-and-forget; per-job Redis SETNX inside the helper# provides cross-process / cross-restart idempotency. Done# outside the workers_lock so the scheduler RPC can't@@ -543,7 +600,7 @@ async def _ensure_worker(user_id: str) -> asyncio.Queue:name=f"dream-system-register-{user_id[:12]}",)- return queue+ return Trueasync def _resolve_user_name(user_id: str) -> str:diff --git a/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py b/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.pyindex 6cedcd73e2..7168ba140d 100644--- a/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py+++ b/autogpt_platform/backend/backend/copilot/graphiti/ingest_test.py@@ -1,18 +1,20 @@"""Tests for Graphiti ingestion queue and worker logic."""import asyncio+import loggingfrom datetime import datetime, timedelta, timezonefrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock, patchimport pytest+from graphiti_core.nodes import EpisodeTypefrom . import ingest@pytest.fixture(autouse=True)def _stub_dream_registration(mocker):- """_ensure_worker fires ensure_dream_system_scheduled fire-and-forget for+ """_enqueue_payload fires ensure_dream_system_scheduled fire-and-forget forevery first-seen user. Unmocked it runs a REAL Prisma timezone lookup onthis test's function-scoped event loop whenever an earlier test alreadyconnected Prisma, leaving a pool connection bound to a dead loop that@@ -23,12 +25,66 @@ def _stub_dream_registration(mocker):)+def _enqueue_mock(queue: asyncio.Queue) -> AsyncMock:+ async def enqueue(_user_id: str, _group_id: str, payload: dict) -> bool:+ try:+ queue.put_nowait(payload)+ except asyncio.QueueFull:+ return False+ return True++ return AsyncMock(side_effect=enqueue)++# Per-loop state in ingest.py auto-isolates between tests: pytest-asyncio# creates a fresh event loop per test function, and the WeakKeyDictionary# forgets the previous loop's state when it is GC'd. No manual reset needed.class TestIngestionWorkerExceptionHandling:+ @pytest.mark.asyncio+ async def test_worker_drops_payload_for_a_different_memory_group(+ self, caplog+ ) -> None:+ queue: asyncio.Queue = asyncio.Queue(maxsize=10)+ queue.put_nowait(+ {+ "name": "cross-scope-episode",+ "episode_body": "private memory",+ "source": "message",+ "source_description": "test",+ "reference_time": None,+ "group_id": "expert_other",+ }+ )+ get_client = AsyncMock()++ with patch.object(ingest, "get_graphiti_client", new=get_client):+ original_timeout = ingest._WORKER_IDLE_TIMEOUT+ ingest._WORKER_IDLE_TIMEOUT = 0.05+ try:+ with caplog.at_level(logging.ERROR, logger=ingest.logger.name):+ await ingest._ingestion_worker(+ "test-user", "expert_expected", queue+ )+ finally:+ ingest._WORKER_IDLE_TIMEOUT = original_timeout++ get_client.assert_not_awaited()+ assert queue.empty()+ await asyncio.wait_for(queue.join(), timeout=0.1)+ # The sole isolation check in the worker must be loud and grep-able,+ # not folded into the generic "ingestion failed" warning.+ violations = [+ r+ for r in caplog.records+ if r.levelno == logging.ERROR+ and "MEMORY ISOLATION VIOLATION" in r.getMessage()+ ]+ assert len(violations) == 1+ assert "expert_other" in violations[0].getMessage()+ assert "expert_expected" in violations[0].getMessage()+@pytest.mark.asyncioasync def test_worker_continues_after_client_error(self) -> None:"""If get_graphiti_client raises, the worker logs and continues."""@@ -47,9 +103,9 @@ class TestIngestionWorkerExceptionHandling:with (patch.object(ingest,- "derive_group_id",- return_value="user_test",- ),+ "derive_memory_group_id",+ side_effect=AssertionError("worker must not re-derive memory scope"),+ ) as derive_mock,patch.object(ingest,"get_graphiti_client",@@ -61,12 +117,13 @@ class TestIngestionWorkerExceptionHandling:original_timeout = ingest._WORKER_IDLE_TIMEOUTingest._WORKER_IDLE_TIMEOUT = 0.1try:- await ingest._ingestion_worker("test-user", queue)+ await ingest._ingestion_worker("test-user", "user_test", queue)finally:ingest._WORKER_IDLE_TIMEOUT = original_timeout# Worker processed the item (task_done called) and exited.assert queue.empty()+ derive_mock.assert_not_called()@pytest.mark.asyncioasync def test_worker_marks_task_done_even_when_ingestion_fails(self) -> None:@@ -86,7 +143,7 @@ class TestIngestionWorkerExceptionHandling:)with (- patch.object(ingest, "derive_group_id", return_value="user_test"),+ patch.object(ingest, "derive_memory_group_id", return_value="user_test"),patch.object(ingest,"get_graphiti_client",@@ -97,7 +154,7 @@ class TestIngestionWorkerExceptionHandling:original_timeout = ingest._WORKER_IDLE_TIMEOUTingest._WORKER_IDLE_TIMEOUT = 0.05try:- await ingest._ingestion_worker("test-user", queue)+ await ingest._ingestion_worker("test-user", "user_test", queue)finally:ingest._WORKER_IDLE_TIMEOUT = original_timeout@@ -110,7 +167,7 @@ class TestIngestionCompletion:"""``IngestionCompletion`` — the per-pass barrier that lets a caller waiton ONLY its own enqueued episodes, decoupled from the shared queue.- These are pure in-memory and touch no process-global ``user_queues``,+ These are pure in-memory and touch no process-global ``group_queues``,so they leak no shared state across tests."""@pytest.mark.asyncio@@ -221,7 +278,7 @@ class TestWaitForIngestion:)with (- patch.object(ingest, "derive_group_id", return_value="user_scoped"),+ patch.object(ingest, "derive_memory_group_id", return_value="user_scoped"),patch.object(ingest,"get_graphiti_client",@@ -229,7 +286,9 @@ class TestWaitForIngestion:return_value=mock_client,),):- worker = asyncio.create_task(ingest._ingestion_worker(user_id, queue))+ worker = asyncio.create_task(+ ingest._ingestion_worker(user_id, "user_scoped", queue)+ )try:result = await ingest.wait_for_ingestion(completion, 5)assert result is True@@ -260,7 +319,86 @@ class TestEnqueueConversationTurn:user_msg="hi",)# No queue should have been created.- assert len(ingest._get_loop_state().user_queues) == 0+ assert len(ingest._get_loop_state().group_queues) == 0++ @pytest.mark.asyncio+ async def test_expert_turn_is_enqueued_in_expert_memory_group(self) -> None:+ 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",+ new=enqueue_mock,+ ),+ patch.object(+ ingest,+ "_resolve_user_name",+ new_callable=AsyncMock,+ return_value="Alice",+ ),+ ):+ await ingest.enqueue_conversation_turn(+ user_id="user-1",+ session_id="session-1",+ user_msg="private fact",+ expert_id="expert-1",+ )++ derive_mock.assert_called_once_with("user-1", "expert-1")+ 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"+++class TestMemoryGroupQueueIsolation:+ @pytest.mark.asyncio+ async def test_same_user_experts_get_distinct_workers(self) -> None:+ first_group = "expert_first"+ second_group = "expert_second"+ with patch.object(ingest, "_ingestion_worker", new_callable=AsyncMock):+ await ingest._enqueue_payload(+ "user-1", first_group, {"group_id": first_group}+ )+ await ingest._enqueue_payload(+ "user-1", second_group, {"group_id": second_group}+ )+ state = ingest._get_loop_state()++ assert (+ state.group_queues[first_group] is not state.group_queues[second_group]+ )+ assert set(state.group_queues) == {first_group, second_group}+ assert set(state.group_workers) == {first_group, second_group}++ workers = list(state.group_workers.values())+ await asyncio.gather(*workers, return_exceptions=True)++ @pytest.mark.asyncio+ async def test_existing_queue_without_worker_is_restarted(self) -> None:+ group_id = "expert_stale_worker"+ queue: asyncio.Queue = asyncio.Queue(maxsize=10)+ state = ingest._get_loop_state()+ state.group_queues[group_id] = queue++ worker_mock = AsyncMock()+ with patch.object(ingest, "_ingestion_worker", new=worker_mock):+ assert await ingest._enqueue_payload(+ "user-1", group_id, {"group_id": group_id}+ )+ worker = state.group_workers[group_id]+ await worker++ worker_mock.assert_awaited_once_with("user-1", group_id, queue)+ assert queue.get_nowait()["group_id"] == group_idclass TestQueueFullScenario:@@ -277,7 +415,7 @@ class TestQueueFullScenario:with (patch.object(ingest,- "derive_group_id",+ "derive_memory_group_id",return_value="user_abc-valid-id",),patch(@@ -287,11 +425,13 @@ class TestQueueFullScenario:),):# Create a tiny queue so it fills instantly.- await ingest._ensure_worker(user_id)- # Replace the queue with one that is already full.tiny_q: asyncio.Queue = asyncio.Queue(maxsize=1)tiny_q.put_nowait({"dummy": True})- ingest._get_loop_state().user_queues[user_id] = tiny_q+ state = ingest._get_loop_state()+ state.group_queues["user_abc-valid-id"] = tiny_q+ current_task = asyncio.current_task()+ assert current_task is not None+ state.group_workers["user_abc-valid-id"] = current_task# Should not raise even though the queue is full.await ingest.enqueue_conversation_turn(@@ -299,6 +439,8 @@ class TestQueueFullScenario:session_id="sess1",user_msg="hi",)+ state.group_queues.pop("user_abc-valid-id")+ state.group_workers.pop("user_abc-valid-id")class TestResolveUserName:@@ -350,15 +492,12 @@ class TestResolveUserName:class TestEnqueueEpisode:@pytest.mark.asyncioasync 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_group_id", return_value="user_abc"),- patch.object(- ingest, "_ensure_worker", new_callable=AsyncMock- ) as mock_worker,+ patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),+ patch.object(ingest, "_enqueue_payload", new=enqueue_mock),):- q: asyncio.Queue = asyncio.Queue(maxsize=100)- mock_worker.return_value = q-result = await ingest.enqueue_episode(user_id="abc",session_id="sess1",@@ -381,7 +520,9 @@ class TestEnqueueEpisode:@pytest.mark.asyncioasync def test_enqueue_episode_returns_false_on_invalid_user(self) -> None:- with patch.object(ingest, "derive_group_id", side_effect=ValueError("bad id")):+ with patch.object(+ ingest, "derive_memory_group_id", side_effect=ValueError("bad id")+ ):result = await ingest.enqueue_episode(user_id="bad",session_id="sess1",@@ -397,15 +538,11 @@ class TestEnqueueEpisode:"""A body over MAX_EPISODE_BODY_BYTES is rejected (False) before anyworker or queue is touched — degraded dream writes must not reachFalkorDB or the extraction LLM."""+ enqueue_mock = AsyncMock()with (- patch.object(ingest, "derive_group_id", return_value="user_abc"),- patch.object(- ingest, "_ensure_worker", new_callable=AsyncMock- ) as mock_worker,+ patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),+ patch.object(ingest, "_enqueue_payload", new=enqueue_mock),):- q: asyncio.Queue = asyncio.Queue(maxsize=100)- mock_worker.return_value = q-result = await ingest.enqueue_episode(user_id="abc",session_id="sess1",@@ -414,20 +551,16 @@ class TestEnqueueEpisode:is_json=True,)assert result is False- mock_worker.assert_not_awaited()- assert q.empty()+ enqueue_mock.assert_not_awaited()@pytest.mark.asyncioasync 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_group_id", return_value="user_abc"),- patch.object(- ingest, "_ensure_worker", new_callable=AsyncMock- ) as mock_worker,+ patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),+ patch.object(ingest, "_enqueue_payload", new=enqueue_mock),):- q: asyncio.Queue = asyncio.Queue(maxsize=100)- mock_worker.return_value = q-result = await ingest.enqueue_episode(user_id="abc",session_id="sess1",@@ -441,15 +574,11 @@ class TestEnqueueEpisode:async def test_enqueue_episode_size_cap_counts_bytes_not_chars(self) -> None:"""Multi-byte UTF-8 content is measured in encoded bytes, so achar-count under the cap can still be rejected."""+ enqueue_mock = AsyncMock()with (- patch.object(ingest, "derive_group_id", return_value="user_abc"),- patch.object(- ingest, "_ensure_worker", new_callable=AsyncMock- ) as mock_worker,+ patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),+ patch.object(ingest, "_enqueue_payload", new=enqueue_mock),):- q: asyncio.Queue = asyncio.Queue(maxsize=100)- mock_worker.return_value = q-# "é" 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(@@ -459,19 +588,16 @@ class TestEnqueueEpisode:episode_body=body,)assert result is False- assert q.empty()+ enqueue_mock.assert_not_awaited()@pytest.mark.asyncioasync 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_group_id", return_value="user_abc"),- patch.object(- ingest, "_ensure_worker", new_callable=AsyncMock- ) as mock_worker,+ patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),+ patch.object(ingest, "_enqueue_payload", new=enqueue_mock),):- q: asyncio.Queue = asyncio.Queue(maxsize=100)- mock_worker.return_value = q-result = await ingest.enqueue_episode(user_id="abc",session_id="sess1",@@ -481,8 +607,6 @@ class TestEnqueueEpisode:)assert result is Trueitem = q.get_nowait()- from graphiti_core.nodes import EpisodeType-assert item["source"] == EpisodeType.json@@ -492,21 +616,18 @@ class TestDerivedFindingLane:"""A substantive assistant message should enqueue both the userepisode and a derived-finding episode."""long_msg = "The analysis reveals significant growth patterns " + "x" * 200+ q: asyncio.Queue = asyncio.Queue(maxsize=100)+ enqueue_mock = _enqueue_mock(q)with (- patch.object(ingest, "derive_group_id", return_value="user_abc"),- patch.object(- ingest, "_ensure_worker", new_callable=AsyncMock- ) as mock_worker,+ 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",new_callable=AsyncMock,return_value="Alice",),):- q: asyncio.Queue = asyncio.Queue(maxsize=100)- mock_worker.return_value = q-await ingest.enqueue_conversation_turn(user_id="abc",session_id="sess1",@@ -518,20 +639,17 @@ class TestDerivedFindingLane:@pytest.mark.asyncioasync def test_short_assistant_msg_skips_finding(self) -> None:+ q: asyncio.Queue = asyncio.Queue(maxsize=100)+ enqueue_mock = _enqueue_mock(q)with (- patch.object(ingest, "derive_group_id", return_value="user_abc"),- patch.object(- ingest, "_ensure_worker", new_callable=AsyncMock- ) as mock_worker,+ 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",new_callable=AsyncMock,return_value="Alice",),):- q: asyncio.Queue = asyncio.Queue(maxsize=100)- mock_worker.return_value = q-await ingest.enqueue_conversation_turn(user_id="abc",session_id="sess1",@@ -569,20 +687,145 @@ class TestWorkerIdleTimeout:# Pre-populate state so cleanup can remove entries.state = ingest._get_loop_state()- state.user_queues[user_id] = queue- task_sentinel = MagicMock()- state.user_workers[user_id] = task_sentinel+ group_id = "user_idle-user"+ state.group_queues[group_id] = queue+ current_task = asyncio.current_task()+ assert current_task is not None+ state.group_workers[group_id] = current_taskoriginal_timeout = ingest._WORKER_IDLE_TIMEOUTingest._WORKER_IDLE_TIMEOUT = 0.05try:- await ingest._ingestion_worker(user_id, queue)+ await ingest._ingestion_worker(user_id, group_id, queue)finally:ingest._WORKER_IDLE_TIMEOUT = original_timeout# After idle timeout the worker should have cleaned up.- assert user_id not in state.user_queues- assert user_id not in state.user_workers+ assert group_id not in state.group_queues+ assert group_id not in state.group_workers++ @pytest.mark.asyncio+ async def test_enqueue_during_idle_retirement_keeps_payload_owned(self) -> None:+ """An enqueue already waiting on ``workers_lock`` must land before an+ idle worker can retire, and the worker must resume to process it."""+ user_id = "race-user"+ group_id = "user_race-user"+ queue: asyncio.Queue = asyncio.Queue(maxsize=10)+ state = ingest._get_loop_state()+ client = MagicMock()+ processed = asyncio.Event()++ async def add_episode(**_kwargs):+ processed.set()+ return MagicMock()++ client.add_episode = AsyncMock(side_effect=add_episode)++ original_timeout = ingest._WORKER_IDLE_TIMEOUT+ ingest._WORKER_IDLE_TIMEOUT = 0.02+ await state.workers_lock.acquire()+ worker = asyncio.create_task(ingest._ingestion_worker(user_id, group_id, queue))+ state.group_queues[group_id] = queue+ state.group_workers[group_id] = worker+ try:+ enqueue_task = asyncio.create_task(+ ingest._enqueue_payload(+ user_id,+ group_id,+ {+ "name": "racing-episode",+ "episode_body": "hello",+ "source": EpisodeType.text,+ "source_description": "race regression",+ "reference_time": datetime.now(timezone.utc),+ "group_id": group_id,+ "custom_extraction_instructions": "",+ },+ )+ )+ await asyncio.sleep(ingest._WORKER_IDLE_TIMEOUT * 2)+ with patch.object(+ ingest,+ "get_graphiti_client",+ new=AsyncMock(return_value=client),+ ):+ state.workers_lock.release()+ assert await enqueue_task is True+ await asyncio.wait_for(processed.wait(), timeout=1)++ client.add_episode.assert_awaited_once()+ finally:+ if state.workers_lock.locked():+ state.workers_lock.release()+ ingest._WORKER_IDLE_TIMEOUT = original_timeout+ workers = {worker, *state.group_workers.values()}+ for active_worker in workers:+ active_worker.cancel()+ await asyncio.gather(*workers, return_exceptions=True)++ @pytest.mark.asyncio+ async def test_enqueue_during_cancellation_starts_replacement_worker(self) -> None:+ """A payload queued before a cancelled worker cleans up must be handed+ to a replacement worker instead of being orphaned."""+ user_id = "cancel-race-user"+ group_id = "user_cancel-race-user"+ queue: asyncio.Queue = asyncio.Queue(maxsize=10)+ state = ingest._get_loop_state()+ client = MagicMock()+ processed = asyncio.Event()++ async def add_episode(**_kwargs):+ processed.set()+ return MagicMock()++ client.add_episode = AsyncMock(side_effect=add_episode)+ await state.workers_lock.acquire()+ worker = asyncio.create_task(ingest._ingestion_worker(user_id, group_id, queue))+ state.group_queues[group_id] = queue+ state.group_workers[group_id] = worker+ enqueue_task: asyncio.Task[bool] | None = None+ try:+ await asyncio.sleep(0)+ enqueue_task = asyncio.create_task(+ ingest._enqueue_payload(+ user_id,+ group_id,+ {+ "name": "cancellation-race-episode",+ "episode_body": "hello",+ "source": EpisodeType.text,+ "source_description": "cancellation race regression",+ "reference_time": datetime.now(timezone.utc),+ "group_id": group_id,+ "custom_extraction_instructions": "",+ },+ )+ )+ await asyncio.sleep(0)+ worker.cancel()+ await asyncio.sleep(0)++ with patch.object(+ ingest,+ "get_graphiti_client",+ new=AsyncMock(return_value=client),+ ):+ state.workers_lock.release()+ assert await enqueue_task is True+ await asyncio.wait_for(processed.wait(), timeout=1)++ replacement = state.group_workers[group_id]+ assert replacement is not worker+ client.add_episode.assert_awaited_once()+ finally:+ if state.workers_lock.locked():+ state.workers_lock.release()+ if enqueue_task is not None and not enqueue_task.done():+ enqueue_task.cancel()+ workers = {worker, *state.group_workers.values()}+ for active_worker in workers:+ active_worker.cancel()+ await asyncio.gather(*workers, return_exceptions=True)class TestStampEdgeMetadata:@@ -722,12 +965,11 @@ class TestStampEdgeMetadata:class TestEnqueueEpisodeEdgeMetadata:@pytest.mark.asyncioasync def test_edge_metadata_rides_payload_sidecar(self) -> None:+ q: asyncio.Queue = asyncio.Queue(maxsize=100)with (- patch.object(ingest, "derive_group_id", return_value="user_abc"),- patch.object(ingest, "_ensure_worker", new_callable=AsyncMock) as w,+ patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),+ patch.object(ingest, "_enqueue_payload", new=_enqueue_mock(q)),):- q: asyncio.Queue = asyncio.Queue(maxsize=100)- w.return_value = qmeta = {"status": "active", "provenance": "dream:p1"}await ingest.enqueue_episode(user_id="abc",@@ -744,12 +986,11 @@ class TestEnqueueEpisodeEdgeMetadata:async def test_default_sidecar_is_none_for_non_dream_writes(self) -> None:"""Conversation turns / memory-store calls pass no edge_metadata →sidecar is None → worker skips stamping → no behavior change."""+ q: asyncio.Queue = asyncio.Queue(maxsize=100)with (- patch.object(ingest, "derive_group_id", return_value="user_abc"),- patch.object(ingest, "_ensure_worker", new_callable=AsyncMock) as w,+ patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),+ patch.object(ingest, "_enqueue_payload", new=_enqueue_mock(q)),):- q: asyncio.Queue = asyncio.Queue(maxsize=100)- w.return_value = qawait ingest.enqueue_episode(user_id="abc", session_id="s", name="ep", episode_body="hi")@@ -762,12 +1003,11 @@ class TestEnqueueEpisodeEdgeMetadata:async def test_completion_rides_payload_sidecar(self) -> None:"""A scoped-drain caller's completion tracker is threaded onto thepayload so the worker can signal it after processing."""+ q: asyncio.Queue = asyncio.Queue(maxsize=100)with (- patch.object(ingest, "derive_group_id", return_value="user_abc"),- patch.object(ingest, "_ensure_worker", new_callable=AsyncMock) as w,+ patch.object(ingest, "derive_memory_group_id", return_value="user_abc"),+ patch.object(ingest, "_enqueue_payload", new=_enqueue_mock(q)),):- q: asyncio.Queue = asyncio.Queue(maxsize=100)- w.return_value = qcompletion = ingest.IngestionCompletion()await ingest.enqueue_episode(user_id="abc",diff --git a/autogpt_platform/backend/backend/copilot/model.py b/autogpt_platform/backend/backend/copilot/model.pyindex e4c155bcae..d7cace5409 100644--- a/autogpt_platform/backend/backend/copilot/model.py+++ b/autogpt_platform/backend/backend/copilot/model.py@@ -923,6 +923,7 @@ async def _save_session_to_db(organization_id=session.organization_id,team_id=session.team_id,metadata=session.metadata,+ expert_id=session.expert_id,)existing_message_count = 0diff --git a/autogpt_platform/backend/backend/copilot/model_test.py b/autogpt_platform/backend/backend/copilot/model_test.pyindex 1b2d8bcaa7..f58cedff6f 100644--- a/autogpt_platform/backend/backend/copilot/model_test.py+++ b/autogpt_platform/backend/backend/copilot/model_test.py@@ -11,8 +11,10 @@ from openai.types.chat.chat_completion_message_tool_call_param import (ChatCompletionMessageToolCallParam,Function,)+from prisma.models import Expertfrom pytest_mock import MockerFixture+from backend.data.redis_client import get_redis_asyncfrom backend.util.exceptions import NotFoundErrorfrom .model import (@@ -162,6 +164,40 @@ async def test_chatsession_db_storage(setup_test_user, test_user_id):assert len(orig.tool_calls) == len(loaded.tool_calls)[email protected](loop_scope="session")+async def test_expert_session_scope_round_trips_through_cache_and_db(+ setup_test_user, test_user_id+):+ """An expert scope must survive both Redis resume and a DB-only reload."""+ expert = await Expert.prisma().create(+ data={+ "ownerUserId": test_user_id,+ "name": "Memory Isolation Expert",+ "role": "assistant",+ "identity": "Keep this expert's memory private.",+ }+ )+ session = ChatSession.new(+ user_id=test_user_id,+ dry_run=False,+ expert_id=expert.id,+ )+ session.messages = [ChatMessage(role="user", content="Remember this privately")]++ saved = await upsert_chat_session(session)++ cached = await get_chat_session(saved.session_id, test_user_id)+ assert cached is not None+ assert cached.expert_id == expert.id++ redis = await get_redis_async()+ await redis.delete(f"chat:session:{saved.session_id}")++ loaded = await get_chat_session(saved.session_id, test_user_id)+ assert loaded is not None+ assert loaded.expert_id == expert.id++# --------------------------------------------------------------------------- ## _merge_consecutive_assistant_messages ## --------------------------------------------------------------------------- #@@ -1005,6 +1041,27 @@ async def test_save_session_to_db_persists_new_messages_when_windowed(assert new_msg.sequence == 1500[email protected](loop_scope="session")+async def test_save_session_to_db_creates_new_row_with_expert_scope(+ mocker: MockerFixture,+) -> None:+ session = ChatSession.new(+ user_id="u1",+ dry_run=False,+ expert_id="expert-1",+ )+ mock_db = mocker.MagicMock()+ mock_db.get_chat_session_metadata = mocker.AsyncMock(return_value=None)+ mock_db.create_chat_session = mocker.AsyncMock()+ mock_db.update_chat_session = mocker.AsyncMock()+ mock_db.add_chat_messages_batch = mocker.AsyncMock(return_value=0)+ mocker.patch("backend.copilot.model.chat_db", return_value=mock_db)++ await _save_session_to_db(session, existing_message_count=0)++ assert mock_db.create_chat_session.await_args.kwargs["expert_id"] == "expert-1"++@pytest.mark.asyncio(loop_scope="session")async def test_save_session_to_db_backfills_late_tool_calls(mocker: MockerFixture,diff --git a/autogpt_platform/backend/backend/copilot/prompting.py b/autogpt_platform/backend/backend/copilot/prompting.pyindex 251f6ebda8..e9efe7afc9 100644--- a/autogpt_platform/backend/backend/copilot/prompting.py+++ b/autogpt_platform/backend/backend/copilot/prompting.py@@ -627,7 +627,7 @@ def get_graphiti_supplement() -> str:return """## Memory System (Graphiti)-You have access to persistent temporal memory tools that remember facts across sessions.+You have access to persistent temporal memory tools scoped to the assistant running this session. AutoPilot uses the user's personal memory; each hired expert uses its own separate memory across that expert's sessions.### CRITICAL — ALWAYS SEARCH BEFORE ANSWERING:**You MUST call memory_search before responding to ANY question that could involve information from a prior conversation.** This includes questions about people, processes, preferences, tools, contacts, rules, workflows, or any factual question. Do NOT say "I don't have that information" without searching first. If the user asks "who should I CC" or "what CRM do we use" — SEARCH FIRST, then answer from results.@@ -649,7 +649,7 @@ You have access to persistent temporal memory tools that remember facts across s### MEMORY RULES:- Facts have temporal validity — if something CHANGED (e.g., user switched from Shopify to WooCommerce), store the new fact. The system automatically invalidates the old one.- Never fabricate memories. Only persist what the user actually said.-- Memory is private to this user — no other user can see it.+- Memory is private and isolated to the current assistant. AutoPilot and hired experts cannot read each other's memories.- group_id is handled automatically by the system — never set it yourself.- When storing, be specific about operational rules and instructions (e.g., "CC Sarah on client communications" not just "Sarah is the assistant")."""diff --git a/autogpt_platform/backend/backend/copilot/prompting_test.py b/autogpt_platform/backend/backend/copilot/prompting_test.pyindex e5ee040553..65fb76de34 100644--- a/autogpt_platform/backend/backend/copilot/prompting_test.py+++ b/autogpt_platform/backend/backend/copilot/prompting_test.py@@ -79,3 +79,15 @@ class TestToolDiscoveryPriorityAntiPattern:# has a concrete template to follow, not just a prohibition.assert "Correct flow" in resultassert 'find_block(query="<service> <action>")' in result+++class TestGraphitiMemoryScope:+ def test_supplement_describes_assistant_scoped_memory(self):+ result = prompting.get_graphiti_supplement()++ assert "scoped to the assistant running this session" in result+ assert "AutoPilot uses the user's personal memory" in result+ assert "each hired expert uses its own separate memory" in result+ assert "Memory is private and isolated to the current assistant" in result+ assert "cannot read each other's memories" in result+ assert "Memory is private to this user — no other user can see it" not in resultdiff --git a/autogpt_platform/backend/backend/copilot/sdk/building_mode_restart_test.py b/autogpt_platform/backend/backend/copilot/sdk/building_mode_restart_test.pyindex 60c804f7c1..0365a9e953 100644--- a/autogpt_platform/backend/backend/copilot/sdk/building_mode_restart_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/building_mode_restart_test.py@@ -12,7 +12,7 @@ import pytestfrom backend.copilot.model import ChatSession-from .service import _ready_for_building_mode_restart+from .service import _graphiti_ingest_allowed, _ready_for_building_mode_restartdef _session(*, requested: bool = True, guide_loaded: bool = False) -> ChatSession:@@ -139,3 +139,44 @@ class TestApplyBuildingModeRestart:_, state, _, _ = await self._run(mocker)assert state.adapter.thinking_only_reprompted is Falseassert state.adapter.prior_attempt_emitted_visible_content is False++ @pytest.mark.asyncio+ async def test_failed_expert_revalidation_keeps_ingest_gated(self, mocker):+ from backend.copilot.expert_context import ExpertSessionUnavailableError+ from backend.copilot.sdk.service import _apply_building_mode_restart++ session = _session(requested=True, guide_loaded=False)+ session.expert_id = "expert-1"+ state = self._state(prior_emitted=False, thinking_reprompted=False)+ mocker.patch(+ "backend.copilot.sdk.service.build_builder_system_prompt_suffix",+ new=mocker.AsyncMock(return_value="<building_guide>GUIDE</building_guide>"),+ )+ identity = mocker.patch(+ "backend.copilot.sdk.service.build_expert_identity_suffix",+ new=mocker.AsyncMock(+ side_effect=ExpertSessionUnavailableError("expert unavailable")+ ),+ )++ with pytest.raises(ExpertSessionUnavailableError):+ await _apply_building_mode_restart(+ session=session,+ state=state,+ sdk_options=MagicMock(),+ base_system_prompt="BASE",+ graphiti_supplement="",+ use_e2b=False,+ session_id="sess-1",+ message_id="msg-1",+ log_prefix="[test]",+ )++ identity.assert_awaited_once_with("user-1", "expert-1")+ assert not _graphiti_ingest_allowed(+ expert_identity_validated=False,+ graphiti_enabled=True,+ user_id="user-1",+ message="private prompt",+ is_user_message=True,+ )diff --git a/autogpt_platform/backend/backend/copilot/sdk/responsiveness_test.py b/autogpt_platform/backend/backend/copilot/sdk/responsiveness_test.pyindex e2921f8b8f..10ad373152 100644--- a/autogpt_platform/backend/backend/copilot/sdk/responsiveness_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/responsiveness_test.py@@ -12,14 +12,24 @@ from unittest.mock import AsyncMock, patchimport pytest+from backend.copilot.expert_context import ExpertSessionUnavailableErrorfrom backend.copilot.model import ChatMessage, ChatSession-from .service import _fetch_graphiti_context+from .service import (+ _enqueue_graphiti_turn,+ _fetch_graphiti_context,+ stream_chat_completion_sdk,+)-def _make_session(user_id: str, *, n_messages: int = 0) -> ChatSession:+def _make_session(+ user_id: str,+ *,+ n_messages: int = 0,+ expert_id: str | None = None,+) -> ChatSession:"""Build a minimal ChatSession with a controllable message count."""- session = ChatSession.new(user_id, dry_run=False)+ session = ChatSession.new(user_id, dry_run=False, expert_id=expert_id)for i in range(n_messages):session.messages.append(ChatMessage(role="user", content=f"prior message {i}"))return session@@ -73,19 +83,108 @@ async def test_fetch_graphiti_context_skips_warm_on_followup_turns():async def test_fetch_graphiti_context_loads_warm_context_on_first_turn():"""Turn 1 (≤1 prior message) with an authenticated user loads warm context."""session = _make_session("user-1")+ fetch_mock = AsyncMock(return_value="<warm_facts>important fact</warm_facts>")with (patch("backend.copilot.sdk.service.is_enabled_for_user",new=AsyncMock(return_value=True),),patch(- "backend.copilot.graphiti.context.fetch_warm_context",- new=AsyncMock(return_value="<warm_facts>important fact</warm_facts>"),+ "backend.copilot.sdk.service.fetch_warm_context",+ new=fetch_mock,),):enabled, ctx = await _fetch_graphiti_context("user-1", session, "first prompt")assert enabled is Trueassert ctx == "<warm_facts>important fact</warm_facts>"+ fetch_mock.assert_awaited_once_with("user-1", "first prompt", expert_id=None)+++async def test_fetch_graphiti_context_uses_expert_session_scope():+ session = _make_session("user-1", expert_id="expert-1")+ fetch_mock = AsyncMock(return_value="expert context")+ with (+ patch(+ "backend.copilot.sdk.service.is_enabled_for_user",+ new=AsyncMock(return_value=True),+ ),+ patch(+ "backend.copilot.sdk.service.fetch_warm_context",+ new=fetch_mock,+ ),+ ):+ enabled, ctx = await _fetch_graphiti_context("user-1", session, "first prompt")++ assert enabled is True+ assert ctx == "expert context"+ fetch_mock.assert_awaited_once_with("user-1", "first prompt", expert_id="expert-1")+++async def test_enqueue_graphiti_turn_uses_expert_session_scope():+ session = _make_session("user-1", expert_id="expert-1")+ enqueue_mock = AsyncMock()++ with patch(+ "backend.copilot.sdk.service.enqueue_conversation_turn",+ new=enqueue_mock,+ ):+ await _enqueue_graphiti_turn(+ "user-1",+ session,+ "session-1",+ "private prompt",+ "private response",+ )++ enqueue_mock.assert_awaited_once_with(+ "user-1",+ "session-1",+ "private prompt",+ assistant_msg="private response",+ expert_id="expert-1",+ )+++async def test_expert_identity_failure_precedes_memory_read_and_write():+ session = _make_session("user-1", expert_id="expert-1")+ identity_mock = AsyncMock(+ side_effect=ExpertSessionUnavailableError(+ "The expert for this session no longer exists or is archived."+ )+ )+ fetch_mock = AsyncMock()+ enqueue_mock = AsyncMock()++ with (+ patch(+ "backend.copilot.sdk.service.build_expert_identity_suffix",+ new=identity_mock,+ ),+ patch(+ "backend.copilot.sdk.service._fetch_graphiti_context",+ new=fetch_mock,+ ),+ patch(+ "backend.copilot.sdk.service._enqueue_graphiti_turn",+ new=enqueue_mock,+ ),+ pytest.raises(ExpertSessionUnavailableError),+ ):+ async for _ in stream_chat_completion_sdk(+ session_id=session.session_id,+ message="private prompt",+ user_id="user-1",+ session=session,+ ):+ pass++ identity_mock.assert_awaited_once_with("user-1", "expert-1")+ fetch_mock.assert_not_awaited()+ enqueue_mock.assert_not_awaited()+ assert session.messages == []@pytest.mark.asyncio@@ -98,7 +197,7 @@ async def test_fetch_graphiti_context_handles_empty_warm_context():new=AsyncMock(return_value=True),),patch(- "backend.copilot.graphiti.context.fetch_warm_context",+ "backend.copilot.sdk.service.fetch_warm_context",new=AsyncMock(return_value=None),),):@@ -119,14 +218,14 @@ async def test_fetch_graphiti_context_handles_none_message():new=AsyncMock(return_value=True),),patch(- "backend.copilot.graphiti.context.fetch_warm_context",+ "backend.copilot.sdk.service.fetch_warm_context",new=fetch_mock,),):enabled, ctx = await _fetch_graphiti_context("user-1", session, None)assert enabled is Trueassert ctx == "ctx"- fetch_mock.assert_awaited_once_with("user-1", "")+ fetch_mock.assert_awaited_once_with("user-1", "", expert_id=None)# ---------------------------------------------------------------------------diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 50ce36ce2c..c0493bee74 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -47,6 +47,8 @@ from backend.copilot.model_router import (resolve_codex_model_route,resolve_model_route,)+from backend.copilot.graphiti.context import fetch_warm_context+from backend.copilot.graphiti.ingest import enqueue_conversation_turnfrom backend.copilot.sdk.codex_compat_gateway import CodexAnthropicGatewayfrom backend.data.db_accessors import chat_dbfrom backend.data.redis_client import get_redis_async@@ -4024,6 +4026,11 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues# Type narrowing: session is guaranteed ChatSession after the check abovesession = cast(ChatSession, session)+ expert_session_suffix = await build_expert_identity_suffix(+ session.user_id, session.expert_id+ )+ expert_identity_validated = True+# Stamping state for THIS turn: messages beyond this index were created# by the current turn and get model/routing_source at persist time.# Pre-feature history rows (NULL model) must never be back-stamped.@@ -4291,9 +4298,6 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues# guide is already in this turn's cached system prompt.session.sdk_turn_active = Truesession.guide_in_system_prompt = bool(builder_session_suffix)- expert_session_suffix = await build_expert_identity_suffix(- session.user_id, session.expert_id- )system_prompt = (base_system_prompt+ get_sdk_supplement(use_e2b=use_e2b)@@ -4994,7 +4998,8 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues# interrupted.capture / snapshot restore here. Detection# cannot re-fire: guide_in_system_prompt flips True on# success, building_mode_requested flips False either way.- yield await _apply_building_mode_restart(+ expert_identity_validated = False+ restart_status = await _apply_building_mode_restart(session=session,state=state,sdk_options=sdk_options,@@ -5005,6 +5010,8 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesmessage_id=message_id,log_prefix=log_prefix,)+ expert_identity_validated = True+ yield restart_statuscontinueexcept _HandledStreamError as exc:# _run_stream_attempt already yielded a StreamError and@@ -5507,9 +5514,13 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuestask.add_done_callback(_background_tasks.discard)# --- Graphiti: ingest conversation turn for temporal memory ---- if graphiti_enabled and user_id and message and is_user_message:- from ..graphiti.ingest import enqueue_conversation_turn-+ if _graphiti_ingest_allowed(+ expert_identity_validated=expert_identity_validated,+ graphiti_enabled=graphiti_enabled,+ user_id=user_id,+ message=message,+ is_user_message=is_user_message,+ ):# Extract last assistant message from THIS TURN only (not all# session history) to avoid distilling stale content from prior# turns when the current turn errors before producing output.@@ -5522,8 +5533,12 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues_last_assistant = _assistant_msgs[-1] if _assistant_msgs else ""_ingest_task = asyncio.create_task(- enqueue_conversation_turn(- user_id, session_id, message, assistant_msg=_last_assistant+ _enqueue_graphiti_turn(+ user_id,+ session,+ session_id,+ message,+ _last_assistant,))_background_tasks.add(_ingest_task)@@ -5747,12 +5762,46 @@ async def _fetch_graphiti_context(if not (user_id and len(session.messages) <= 1):return True, ""- from ..graphiti.context import fetch_warm_context-- ctx = await fetch_warm_context(user_id, message or "") or ""+ ctx = (+ await fetch_warm_context(user_id, message or "", expert_id=session.expert_id)+ or ""+ )return True, ctx+def _graphiti_ingest_allowed(+ *,+ expert_identity_validated: bool,+ graphiti_enabled: bool,+ user_id: str | None,+ message: str | None,+ is_user_message: bool,+) -> bool:+ return bool(+ expert_identity_validated+ and graphiti_enabled+ and user_id+ and message+ and is_user_message+ )+++async def _enqueue_graphiti_turn(+ user_id: str,+ session: ChatSession,+ session_id: str,+ message: str,+ assistant_msg: str,+) -> None:+ await enqueue_conversation_turn(+ user_id,+ session_id,+ message,+ assistant_msg=assistant_msg,+ expert_id=session.expert_id,+ )++def _canonical_model(model: str) -> str:"""Spelling-insensitive model identity for fallback detection:vendor prefix stripped, dots→dashes, trailing -YYYYMMDD dropped."""diff --git a/autogpt_platform/backend/backend/copilot/tools/get_sub_session_result.py b/autogpt_platform/backend/backend/copilot/tools/get_sub_session_result.pyindex 70044640c0..583a517042 100644--- a/autogpt_platform/backend/backend/copilot/tools/get_sub_session_result.py+++ b/autogpt_platform/backend/backend/copilot/tools/get_sub_session_result.py@@ -139,7 +139,7 @@ class GetSubSessionResultTool(BaseTool):# shape for "doesn't exist" and "belongs to someone else" avoids# leaking session existence.sub = await get_chat_session(inner_session_id)- if sub is None or sub.user_id != user_id:+ if sub is None or sub.user_id != user_id or sub.expert_id != session.expert_id:return ErrorResponse(message=(f"No sub-session with id {inner_session_id}. It may have "diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_forget.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_forget.pyindex e47a1eff32..de1bdbb3bd 100644--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_forget.py+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_forget.py@@ -9,7 +9,7 @@ from datetime import datetime, timezonefrom typing import Any, Literalfrom backend.copilot.graphiti._format import extract_fact, extract_temporal_validity-from backend.copilot.graphiti.client import derive_group_id, get_graphiti_client+from backend.copilot.graphiti.client import derive_memory_group_id, get_graphiti_clientfrom backend.copilot.graphiti.config import is_enabled_for_userfrom backend.copilot.model import ChatSession@@ -66,7 +66,7 @@ def _delete_error_reason(exc: Exception) -> str:class MemoryForgetSearchTool(BaseTool):- """Search for memories to forget — returns candidates for user confirmation."""+ """Search the current assistant's memories for deletion candidates."""@propertydef name(self) -> str:@@ -75,8 +75,8 @@ class MemoryForgetSearchTool(BaseTool):@propertydef description(self) -> str:return (- "Search for stored memories matching a description so the user can "- "choose which to delete. Returns candidate facts with UUIDs. "+ "Search the current assistant's stored memories for a description so "+ "the user can choose which to delete. Returns candidate facts with UUIDs. ""Use memory_forget_confirm with the UUIDs to actually delete them.")@@ -124,7 +124,7 @@ class MemoryForgetSearchTool(BaseTool):)try:- group_id = derive_group_id(user_id)+ group_id = derive_memory_group_id(user_id, session.expert_id)except ValueError:return ErrorResponse(message="Invalid user ID for memory operations.",@@ -178,7 +178,7 @@ class MemoryForgetSearchTool(BaseTool):class MemoryForgetConfirmTool(BaseTool):- """Delete specific memory edges by UUID after user confirmation.+ """Delete edges from the current assistant's memory after confirmation.Supports both soft delete (temporal invalidation — reversible) andhard delete (remove from graph — irreversible, for GDPR).@@ -191,8 +191,9 @@ class MemoryForgetConfirmTool(BaseTool):@propertydef description(self) -> str:return (- "Delete specific memories by UUID. Use after memory_forget_search "- "returns candidates and the user confirms which to delete. "+ "Delete specific memories from the current assistant by UUID. Use after "+ "memory_forget_search returns candidates and the user confirms which to "+ "delete. ""Default is soft delete (marks as expired but keeps history). ""Set hard_delete=true for permanent removal (GDPR).")@@ -248,7 +249,7 @@ class MemoryForgetConfirmTool(BaseTool):)try:- group_id = derive_group_id(user_id)+ group_id = derive_memory_group_id(user_id, session.expert_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.pyindex 1c8aede4f1..12f890741c 100644--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_forget_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_forget_test.py@@ -1,6 +1,6 @@"""Tests for graphiti_forget delete helpers."""-from unittest.mock import AsyncMock+from unittest.mock import AsyncMock, MagicMockimport pytest@@ -8,6 +8,7 @@ from backend.copilot.model import ChatSessionfrom backend.copilot.tools.graphiti_forget import (_MAX_FAILURE_DETAIL,MemoryForgetConfirmTool,+ MemoryForgetSearchTool,_build_confirm_message,_hard_delete_edges,_retract_edges,@@ -41,6 +42,89 @@ class TestSoftDeleteOverReportsSuccess:assert failed == ["nonexistent-uuid"]+class TestExpertMemoryScope:+ @pytest.mark.asyncio+ async def test_forget_search_uses_expert_memory_group(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ async def _enabled(_user_id: str) -> bool:+ return True++ derive = MagicMock(return_value="expert_private_group")+ client = type("Client", (), {"search": AsyncMock(return_value=[])})()++ async def _get_client(group_id: str):+ assert group_id == "expert_private_group"+ 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+ )++ session = ChatSession.new(+ "user-abc",+ dry_run=False,+ expert_id="expert-1",+ )+ await MemoryForgetSearchTool()._execute(+ "user-abc",+ session,+ query="private fact",+ )++ derive.assert_called_once_with("user-abc", "expert-1")+ client.search.assert_awaited_once_with(+ query="private fact",+ group_ids=["expert_private_group"],+ num_results=10,+ )++ @pytest.mark.asyncio+ async def test_forget_confirm_uses_expert_memory_group(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ async def _enabled(_user_id: str) -> bool:+ return True++ derive = MagicMock(return_value="expert_private_group")+ driver = AsyncMock()+ driver.execute_query.return_value = ([], None, None)+ client = type("Client", (), {"graph_driver": driver})()++ async def _get_client(group_id: str):+ assert group_id == "expert_private_group"+ 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+ )++ session = ChatSession.new(+ "user-abc",+ dry_run=False,+ expert_id="expert-1",+ )+ await MemoryForgetConfirmTool()._execute(+ "user-abc",+ session,+ uuids=["private-edge"],+ )++ derive.assert_called_once_with("user-abc", "expert-1")++class TestSoftDeleteNoMatchReportsFailure:"""When the query returns empty records (no edge with that UUID existsin the database), _soft_delete_edges should report it as failed.diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_search.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_search.pyindex 0aef554bbf..4a49e73498 100644--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_search.py+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_search.py@@ -12,7 +12,7 @@ from backend.copilot.graphiti._format import (extract_fact,extract_temporal_validity,)-from backend.copilot.graphiti.client import derive_group_id, get_graphiti_client+from backend.copilot.graphiti.client import derive_memory_group_id, get_graphiti_clientfrom backend.copilot.graphiti.config import is_enabled_for_userfrom backend.copilot.model import ChatSession@@ -25,7 +25,7 @@ _MAX_LIMIT = 50class MemorySearchTool(BaseTool):- """Search the user's temporal knowledge graph for stored memories."""+ """Search the current assistant's temporal knowledge graph."""@propertydef name(self) -> str:@@ -34,9 +34,9 @@ class MemorySearchTool(BaseTool):@propertydef description(self) -> str:return (- "Search the user's memory graph for facts, preferences, and context "- "from prior sessions. Use before answering context-dependent questions "- "or when the user references something from a past conversation."+ "Search the current assistant's memory graph for facts, preferences, "+ "and context from prior sessions. Use before answering context-dependent "+ "questions or when the user references a past conversation.")@property@@ -101,7 +101,7 @@ class MemorySearchTool(BaseTool):limit = min(limit, _MAX_LIMIT)try:- group_id = derive_group_id(user_id)+ group_id = derive_memory_group_id(user_id, session.expert_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.pyindex 99e2de78ea..b288444f40 100644--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_search_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_search_test.py@@ -1,12 +1,65 @@"""Tests for graphiti_search helper functions."""from types import SimpleNamespace+from unittest.mock import AsyncMock, patch++import pytestfrom backend.copilot.graphiti.memory_model import MemoryEnvelope, MemoryKind, SourceKind+from backend.copilot.model import ChatSessionfrom backend.copilot.tools.graphiti_search import (+ MemorySearchTool,_filter_episodes_by_scope,_format_episodes,)+from backend.copilot.tools.models import MemorySearchResponse+++async def test_expert_session_searches_only_expert_memory_group() -> None:+ session = ChatSession.new(+ "user-1",+ dry_run=False,+ expert_id="expert-1",+ )+ client = SimpleNamespace(+ search=AsyncMock(return_value=[]),+ retrieve_episodes=AsyncMock(return_value=[]),+ )++ with (+ patch(+ "backend.copilot.tools.graphiti_search.is_enabled_for_user",+ 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,+ return_value=client,+ ) as get_client_mock,+ ):+ result = await MemorySearchTool()._execute(+ "user-1",+ session,+ query="private fact",+ )++ assert isinstance(result, MemorySearchResponse)+ derive_mock.assert_called_once_with("user-1", "expert-1")+ get_client_mock.assert_awaited_once_with("expert_private_group")+ client.search.assert_awaited_once_with(+ query="private fact",+ group_ids=["expert_private_group"],+ num_results=15,+ )+ assert client.retrieve_episodes.await_args.kwargs["group_ids"] == [+ "expert_private_group"+ ]class TestFilterEpisodesByScopeTruncation:diff --git a/autogpt_platform/backend/backend/copilot/tools/graphiti_store.py b/autogpt_platform/backend/backend/copilot/tools/graphiti_store.pyindex 08887d42cb..e8597c8f69 100644--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_store.py+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_store.py@@ -23,7 +23,7 @@ logger = logging.getLogger(__name__)class MemoryStoreTool(BaseTool):- """Store a memory/fact in the user's temporal knowledge graph."""+ """Store a fact in the current assistant's temporal knowledge graph."""@propertydef name(self) -> str:@@ -32,7 +32,8 @@ class MemoryStoreTool(BaseTool):@propertydef description(self) -> str:return (- "Store a memory or fact about the user for future recall. "+ "Store a memory or fact about the user for this assistant to recall "+ "in future sessions. ""Use when the user shares preferences, business context, decisions, ""relationships, or other important information worth remembering ""across sessions. Supports optional metadata for scoping and classification."@@ -261,6 +262,7 @@ class MemoryStoreTool(BaseTool):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.pyindex e222ac92bf..b260758911 100644--- a/autogpt_platform/backend/backend/copilot/tools/graphiti_store_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/graphiti_store_test.py@@ -12,7 +12,9 @@ from backend.copilot.tools.graphiti_store import MemoryStoreToolfrom backend.copilot.tools.models import ErrorResponse, MemoryStoreResponse-def _make_session(session_id: str = "test-session") -> ChatSession:+def _make_session(+ session_id: str = "test-session", *, expert_id: str | None = None+) -> ChatSession:return ChatSession(session_id=session_id,user_id="test-user",@@ -22,6 +24,7 @@ def _make_session(session_id: str = "test-session") -> ChatSession:credentials={},started_at=datetime.now(UTC),updated_at=datetime.now(UTC),+ expert_id=expert_id,)@@ -198,6 +201,33 @@ class TestMemoryStoreTool:envelope = json.loads(call_kwargs["episode_body"])assert envelope["content"] == "A fact worth remembering."+ @pytest.mark.asyncio+ async def test_store_expert_session_enqueues_in_expert_scope(self):+ tool = MemoryStoreTool()+ session = _make_session(expert_id="expert-1")+ 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="user-1",+ session=session,+ name="private_fact",+ content="Only this expert should remember this.",+ )++ assert isinstance(result, MemoryStoreResponse)+ assert mock_enqueue.await_args.kwargs["expert_id"] == "expert-1"+@pytest.mark.asyncioasync def test_store_invalid_source_kind_falls_back(self):"""Invalid enum values should fall back to defaults, not crash."""diff --git a/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py b/autogpt_platform/backend/backend/copilot/tools/run_sub_session.pyindex 3425e94b4b..87e341edb8 100644--- a/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py+++ b/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py@@ -151,6 +151,15 @@ class RunSubSessionTool(BaseTool):),session_id=session.session_id,)+ if owned.expert_id != session.expert_id:+ return ErrorResponse(+ message=(+ f"sub_autopilot_session_id {sub_session_param} is not "+ "in the current memory scope. Leave empty to start a "+ "fresh sub for this assistant."+ ),+ session_id=session.session_id,+ )if (owned.metadata.llm_auth_provider != session.metadata.llm_auth_provideror owned.metadata.llm_credential_id@@ -169,6 +178,7 @@ class RunSubSessionTool(BaseTool):team_id=session.team_id,llm_auth_provider=session.metadata.llm_auth_provider,llm_credential_id=session.metadata.llm_credential_id,+ expert_id=session.expert_id,)inner_session_id = new_session.session_iddiff --git a/autogpt_platform/backend/backend/copilot/tools/schedule_followup.py b/autogpt_platform/backend/backend/copilot/tools/schedule_followup.pyindex f093998d34..b82091aebd 100644--- a/autogpt_platform/backend/backend/copilot/tools/schedule_followup.py+++ b/autogpt_platform/backend/backend/copilot/tools/schedule_followup.py@@ -7,8 +7,9 @@ at the scheduled time.The ``session_id`` argument decides WHERE the follow-up lands:* Omitted / ``null`` — sentinel meaning "fire into a **fresh chat**".- At fire time the scheduler creates a new copilot session for the- user and routes the turn into it (no prior conversation context).+ At fire time the scheduler creates a new copilot session in the same+ Autopilot or expert scope and routes the turn into it (no prior+ conversation context).Use this for recurring "morning brief" / "daily digest" patternswhere a clean slate is preferred over polluting the current chat.@@ -16,8 +17,9 @@ The ``session_id`` argument decides WHERE the follow-up lands:full history. This is the right value for "remind me here in 20minutes": the model reads the current ``session_id`` from thetrusted ``<session_context>`` block injected on every turn and- passes it back verbatim. Ownership is validated — UUIDs belonging- to other users are rejected as ``session_not_found``.+ passes it back verbatim. Ownership and persona scope are validated —+ UUIDs belonging to other users or another expert are rejected as+ ``session_not_found``.The tool ends the current turn; the model should send its finaluser-facing message *before* calling this. The deferred work happens@@ -55,10 +57,10 @@ class ScheduleCreatedResponse(ToolResponseBase):class ScheduleFollowupTool(BaseTool):"""Schedule a follow-up turn on a copilot session.- Defaults to the current session ("check on this in 20 min"). Pass- ``session_id`` to target a different conversation owned by the- same user. Exactly one of ``delay_seconds`` or ``cron`` must be- provided.+ Omit ``session_id`` to create a fresh conversation in the current+ Autopilot or expert scope. Pass ``session_id`` to target a conversation+ owned by the same user in that same scope. Exactly one of+ ``delay_seconds`` or ``cron`` must be provided."""@property@@ -71,11 +73,13 @@ class ScheduleFollowupTool(BaseTool):"Schedule a copilot follow-up turn. The 'message' is sent ""at the scheduled time. The 'session_id' arg picks the ""destination: OMIT IT (or pass null) to fire into a brand-"- "new chat created at fire-time — best for daily briefs / "- "recurring digests / anything that should start fresh. "+ "new chat created at fire-time in this chat's same Autopilot "+ "or expert memory scope — best for daily briefs / recurring "+ "digests / anything that should start fresh. ""Pass an existing 'session_id' (you can read the current ""one from the trusted <session_context> block) to resume "- "that conversation with its full history — best for "+ "that conversation with its full history (the target must use "+ "the same Autopilot or expert scope) — best for ""'remind me here in 20 minutes'. Use 'delay_seconds' for ""one-shot followups ('in 20 minutes', 'at 7am tomorrow' — ""convert absolute times to a delay) or 'cron' for "@@ -122,11 +126,13 @@ class ScheduleFollowupTool(BaseTool):"anyOf": [{"type": "string"}, {"type": "null"}],"description": ("Target session UUID. OMIT or null = create a "- "brand-new chat at fire-time (no prior context). "+ "brand-new chat at fire-time in the current "+ "Autopilot or expert memory scope (no prior context). ""Pass the current session's id from <session_""context> to fire into THIS chat with full "- "history. Sessions owned by other users are "- "rejected as 'session_not_found'."+ "history. Sessions owned by other users or in a "+ "different expert scope are rejected as "+ "'session_not_found'."),},"name": {@@ -172,17 +178,17 @@ class ScheduleFollowupTool(BaseTool):# Target session: ``None`` (omitted / explicit null) = sentinel for# "create a fresh chat at fire time" — the scheduler handles it.# A non-null value pins the followup to an existing session; we- # validate ownership here (``get_chat_session`` returns None for- # both "not found" and "not yours", which collapse into the same- # error from the model's perspective).+ # validate ownership and persona scope here. ``get_chat_session``+ # returns None for both "not found" and "not yours", and all three+ # cases collapse into the same error from the model's perspective.target_session_id: str | None = kwargs.get("session_id")if target_session_id and target_session_id != current_session_id:target = await get_chat_session(target_session_id, user_id)- if target is None:+ if target is None or target.expert_id != session.expert_id:return ErrorResponse(message=(- f"Session {target_session_id} not found or not "- f"owned by the calling user."+ f"Session {target_session_id} not found, not owned by "+ "the calling user, or outside the current memory scope."),error="session_not_found",session_id=current_session_id,diff --git a/autogpt_platform/backend/backend/copilot/tools/schedule_followup_test.py b/autogpt_platform/backend/backend/copilot/tools/schedule_followup_test.pyindex 3aca35975c..69e4852392 100644--- a/autogpt_platform/backend/backend/copilot/tools/schedule_followup_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/schedule_followup_test.py@@ -18,7 +18,7 @@ _USER = "test-user-followup"_TOOL_PATH = "backend.copilot.tools.schedule_followup"-def _info(*, cron=None, run_at=None) -> CopilotTurnJobInfo:+def _info(*, cron=None, run_at=None, expert_id=None) -> CopilotTurnJobInfo:return CopilotTurnJobInfo(schedule_id="cop-1",user_id=_USER,@@ -26,6 +26,7 @@ def _info(*, cron=None, run_at=None) -> CopilotTurnJobInfo:message="check CI",cron=cron,run_at=run_at,+ expert_id=expert_id,id="cop-1",name="copilot turn",next_run_time="2026-05-22T19:00:00+00:00",@@ -126,6 +127,7 @@ async def test_one_shot_delay_omitted_session_id_passes_new_chat_sentinel(assert call_kwargs["cron"] is Noneassert call_kwargs["run_at"] is not Noneassert call_kwargs["user_timezone"] == "America/New_York"+ assert call_kwargs["expert_id"] is Noneassert "fresh chat" in result.message # user-facing copy reflects sentinel@@ -236,6 +238,34 @@ async def test_explicit_null_session_id_passes_new_chat_sentinel(tool, session):assert mock_client.add_copilot_turn_schedule.call_args.kwargs["session_id"] is None+async def test_expert_fresh_followup_inherits_expert_scope(tool, session):+ session.expert_id = "expert-a"+ mock_user_db = MagicMock()+ mock_user_db().get_user_by_id = AsyncMock(return_value=MagicMock(timezone="UTC"))+ mock_client = AsyncMock()+ mock_client.add_copilot_turn_schedule = AsyncMock(+ return_value=_info(expert_id="expert-a")+ )++ with (+ patch(f"{_TOOL_PATH}.user_db", return_value=mock_user_db()),+ patch(f"{_TOOL_PATH}.get_scheduler_client", return_value=mock_client),+ ):+ result = await tool._execute(+ user_id=_USER,+ session=session,+ message="daily brief",+ delay_seconds=600,+ )++ assert isinstance(result, ScheduleCreatedResponse)+ call_kwargs = mock_client.add_copilot_turn_schedule.call_args.kwargs+ assert call_kwargs["session_id"] is None+ assert call_kwargs["expert_id"] == "expert-a"+ assert "fresh chat" in result.message++@pytest.mark.asyncioasync def test_session_id_override_targets_a_different_owned_session(tool, session):"""Passing session_id targets that session instead of the current one."""@@ -244,7 +274,9 @@ async def test_session_id_override_targets_a_different_owned_session(tool, sessimock_user = MagicMock(timezone="UTC")mock_user_db().get_user_by_id = AsyncMock(return_value=mock_user)- mock_get_session = AsyncMock(return_value=MagicMock()) # session exists + owned+ mock_get_session = AsyncMock(+ return_value=MagicMock(expert_id=None)+ ) # session exists + ownedmock_client = AsyncMock()mock_client.add_copilot_turn_schedule = AsyncMock(return_value=_info())@@ -291,6 +323,62 @@ async def test_session_id_override_rejects_session_not_owned(tool, session):mock_client.add_copilot_turn_schedule.assert_not_awaited()+async def test_session_id_override_rejects_different_memory_scope(tool, session):+ session.expert_id = "expert-a"+ other_session_id = "session-for-expert-b"+ other_session = MagicMock(expert_id="expert-b")+ mock_get_session = AsyncMock(return_value=other_session)+ mock_client = AsyncMock()++ with (+ patch(f"{_TOOL_PATH}.get_scheduler_client", return_value=mock_client),+ patch(f"{_TOOL_PATH}.get_chat_session", new=mock_get_session),+ ):+ result = await tool._execute(+ user_id=_USER,+ session=session,+ message="x",+ delay_seconds=600,+ session_id=other_session_id,+ )++ assert isinstance(result, ErrorResponse)+ assert result.error == "session_not_found"+ mock_client.add_copilot_turn_schedule.assert_not_awaited()+++async def test_session_id_override_accepts_same_expert_scope(tool, session):+ session.expert_id = "expert-a"+ other_session_id = "another-session-for-expert-a"+ mock_get_session = AsyncMock(return_value=MagicMock(expert_id="expert-a"))+ mock_user_db = MagicMock()+ mock_user_db().get_user_by_id = AsyncMock(return_value=MagicMock(timezone="UTC"))+ mock_client = AsyncMock()+ mock_client.add_copilot_turn_schedule = AsyncMock(+ return_value=_info(expert_id="expert-a")+ )++ with (+ patch(f"{_TOOL_PATH}.user_db", return_value=mock_user_db()),+ patch(f"{_TOOL_PATH}.get_scheduler_client", return_value=mock_client),+ patch(f"{_TOOL_PATH}.get_chat_session", new=mock_get_session),+ ):+ result = await tool._execute(+ user_id=_USER,+ session=session,+ message="x",+ delay_seconds=600,+ session_id=other_session_id,+ )++ assert isinstance(result, ScheduleCreatedResponse)+ call_kwargs = mock_client.add_copilot_turn_schedule.call_args.kwargs+ assert call_kwargs["session_id"] == other_session_id+ assert call_kwargs["expert_id"] == "expert-a"++@pytest.mark.asyncioasync def test_session_id_override_to_current_session_skips_lookup(tool, session):"""Passing session_id equal to the current session avoids the get_chat_session round-trip."""diff --git a/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py b/autogpt_platform/backend/backend/copilot/tools/sub_session_test.pyindex 5cd3ec94ae..cb74a88782 100644--- a/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py@@ -28,14 +28,20 @@ from .run_sub_session import ()-def _session(user_id: str = "u", session_id: str = "s1") -> MagicMock:+def _session(+ user_id: str = "u",+ session_id: str = "s1",+ expert_id: str | None = None,+) -> MagicMock:sess = MagicMock()sess.session_id = session_id+ sess.user_id = user_idsess.dry_run = Falsesess.organization_id = Nonesess.team_id = Nonesess.metadata.llm_auth_provider = "platform"sess.metadata.llm_credential_id = None+ sess.expert_id = expert_idreturn sess@@ -138,6 +144,7 @@ def mock_model(monkeypatch):team_id: str | None = None,llm_auth_provider: str = "platform",llm_credential_id: str | None = None,+ expert_id: str | None = None,):sess = MagicMock()sess.session_id = f"inner-{len(created) + 1}"@@ -147,6 +154,7 @@ def mock_model(monkeypatch):sess.team_id = team_idsess.metadata.llm_auth_provider = llm_auth_providersess.metadata.llm_credential_id = llm_credential_id+ sess.expert_id = expert_idsess.messages = []created.append(sess)return sess@@ -231,6 +239,45 @@ class TestRunSubSession:assert mock_model["created"], "create_chat_session was never awaited"assert mock_model["created"][0].dry_run is True+ @pytest.mark.asyncio+ async def test_fresh_sub_inherits_expert_scope(+ self, mock_queue, mock_waiter, mock_model+ ):+ parent = _session("alice", expert_id="expert-a")++ await RunSubSessionTool()._execute(+ user_id="alice",+ session=parent,+ prompt="hi",+ wait_for_result=0,+ )++ assert mock_model["created"][0].expert_id == "expert-a"++ @pytest.mark.asyncio+ async def test_resume_rejects_different_expert_scope(+ self, monkeypatch, mock_queue, mock_waiter+ ):+ other_scope = _session("alice", "other-session", expert_id="expert-b")++ async def fake_get(_session_id: str):+ return other_scope++ monkeypatch.setattr(+ "backend.copilot.tools.run_sub_session.get_chat_session", fake_get+ )++ result = await RunSubSessionTool()._execute(+ user_id="alice",+ session=_session("alice", expert_id="expert-a"),+ prompt="continue",+ sub_autopilot_session_id="other-session",+ )++ assert isinstance(result, ErrorResponse)+ assert "current memory scope" in result.message+ mock_queue["enqueue_turn"].assert_not_awaited()+@pytest.mark.asyncioasync def test_forwards_parent_permissions_to_queue(self, monkeypatch, mock_queue, mock_waiter, mock_model@@ -406,7 +453,7 @@ class TestGetSubSessionResult:@pytest.mark.asyncioasync def test_other_user_cannot_access(self, monkeypatch):"""Cross-user lookups are indistinguishable from 'not found'."""- foreign = MagicMock(user_id="bob", messages=[])+ foreign = MagicMock(user_id="bob", expert_id=None, messages=[])async def foreign_get(_sid):return foreign@@ -421,9 +468,31 @@ class TestGetSubSessionResult:assert isinstance(r, ErrorResponse)assert "No sub-session" in r.message+ @pytest.mark.asyncio+ async def test_other_expert_scope_cannot_access(self, monkeypatch):+ other_scope = _session("alice", "expert-b-session", expert_id="expert-b")+ other_scope.messages = []++ async def fake_get(_sid):+ return other_scope++ monkeypatch.setattr(+ "backend.copilot.tools.get_sub_session_result.get_chat_session",+ fake_get,+ )++ result = await GetSubSessionResultTool()._execute(+ user_id="alice",+ session=_session("alice", expert_id="expert-a"),+ sub_session_id="expert-b-session",+ )++ assert isinstance(result, ErrorResponse)+ assert "No sub-session" in result.message+@pytest.mark.asyncioasync def test_wait_returns_running(self, monkeypatch, mock_waiter):- sub = MagicMock(user_id="alice", messages=[])+ sub = MagicMock(user_id="alice", expert_id=None, messages=[])async def fake_get(_sid):return sub@@ -455,7 +524,9 @@ class TestGetSubSessionResult:async def test_wait_returns_completed_with_response(self, monkeypatch, mock_waiter):"""'completed' outcome surfaces the SessionResult directly."""- sub = MagicMock(user_id="alice", messages=[]) # not terminal-looking+ sub = MagicMock(+ user_id="alice", expert_id=None, messages=[]+ ) # not terminal-lookingasync def fake_get(_sid):return sub@@ -492,7 +563,7 @@ class TestGetSubSessionResult:in flight, the tool returns 'completed' without ever callingwait_for_session_result — it rebuilds the response from thepersisted message instead."""- sub = MagicMock(user_id="alice")+ sub = MagicMock(user_id="alice", expert_id=None)assistant = MagicMock()assistant.role = "assistant"assistant.content = "already done"@@ -538,7 +609,7 @@ class TestGetSubSessionResult:prior.role = "assistant"prior.content = "OLD stale result"prior.tool_calls = None- sub = MagicMock(user_id="alice", messages=[prior])+ sub = MagicMock(user_id="alice", expert_id=None, messages=[prior])async def fake_get(_sid):return sub@@ -577,7 +648,7 @@ class TestGetSubSessionResult:):"""cancel=true fans out a CancelCoPilotEvent and returns 'cancelled'without waiting for the sub to finish (the worker will finalise)."""- sub = MagicMock(user_id="alice", messages=[])+ sub = MagicMock(user_id="alice", expert_id=None, messages=[])async def fake_get(_sid):return sub@@ -607,7 +678,7 @@ class TestGetSubSessionResult:log only holds the last message — yet the file manifest is stillpopulated from the authoritative workspace listing."""- sub = MagicMock(user_id="alice")+ sub = MagicMock(user_id="alice", expert_id=None)assistant = MagicMock()assistant.role = "assistant"assistant.content = "done — see the docs I wrote"diff --git a/autogpt_platform/backend/backend/executor/scheduler.py b/autogpt_platform/backend/backend/executor/scheduler.pyindex a87f5fecec..4ef03e4968 100644--- a/autogpt_platform/backend/backend/executor/scheduler.py+++ b/autogpt_platform/backend/backend/executor/scheduler.py@@ -38,6 +38,7 @@ from backend.copilot.executor.utils import schedule_turnfrom backend.copilot.graphiti.communities import rebuild_communities_for_userfrom backend.copilot.model import create_chat_session, get_chat_sessionfrom backend.copilot.optimize_blocks import optimize_block_descriptions+from backend.data.db_accessors import experts_dbfrom backend.data.execution import GraphExecutionWithNodesfrom backend.data.model import CredentialsMetaInput, GraphInputfrom backend.executor import utils as execution_utils@@ -247,18 +248,86 @@ def execute_copilot_turn(**kwargs):run_async(_execute_copilot_turn(**kwargs))+async def _expert_scope_status(+ user_id: str, expert_id: str+) -> Literal["active", "archived", "paused", "missing", "unavailable"]:+ """Resolve an expert schedule's owner/lifecycle state without guessing.++ Mirrors ``enforce_expert_run_budget``'s gate: ``archived`` and ``paused``+ (``schedulesPausedAt`` — set by a manual pause or a budget breach) both+ block the firing, but reversibly, so the schedule must survive them.+ ``missing`` is reserved for IDs that don't exist for this owner at all —+ the only state that deletes the schedule. ``unavailable`` is kept+ distinct so a transient DB/RPC failure skips the turn without+ permanently deleting a valid recurring schedule.+ """+ try:+ expert = await experts_db().get_expert(+ user_id, expert_id, include_workflows=False, include_archived=True+ )+ except Exception:+ logger.warning(+ "Could not validate expert scope %s for scheduled copilot turn",+ expert_id[:12],+ exc_info=True,+ )+ return "unavailable"+ if expert is None:+ return "missing"+ if expert.is_archived:+ return "archived"+ if expert.schedules_paused_at is not None:+ return "paused"+ return "active"+++async def _skip_inactive_expert_scope(+ args: "CopilotTurnJobArgs",+ status: Literal["archived", "paused", "missing", "unavailable"],+) -> None:+ """Shared skip path for a firing whose expert scope is not active.++ Only ``missing`` deletes the schedule: archive and pause are reversible,+ and copilot-turn schedules have no persisted cadence to revive from, so+ they must outlive both. A transient lookup failure re-schedules a+ one-shot because APScheduler drops it after the fire regardless.+ """+ logger.warning(+ "Copilot turn schedule %s skipped — expert scope %s is %s",+ args.schedule_id,+ (args.expert_id or "?")[:12],+ status,+ )+ if status == "missing":+ await _self_delete_copilot_turn_schedule(args)+ elif status == "unavailable" and args.run_at is not None:+ await _reschedule_one_shot_after_expert_unavailable(args)++async def _execute_copilot_turn(**kwargs):+ expert_scope_was_persisted = "expert_id" in kwargsargs = CopilotTurnJobArgs(**kwargs)start_time = asyncio.get_event_loop().time()try:# Resolve the target session. ``session_id=None`` means "fire into# a fresh chat" — create one now so the user has somewhere visible# for the scheduled message to land. For an explicit session_id- # we still verify it exists (the user may have deleted the chat- # between scheduling and now) and self-clean the dead schedule- # otherwise — orphan turns into a missing session would never- # surface in any UI.+ # we still verify it exists and remains in the scope captured when+ # the schedule was created. The user may have deleted the chat, and+ # stale or tampered scope data must not route a turn into another+ # persona's memory. Self-clean either invalid schedule.if args.session_id is None:+ if not expert_scope_was_persisted:+ logger.info(+ "Copilot turn schedule %s predates persisted memory scope; "+ "preserving its legacy AutoPilot behavior",+ args.schedule_id,+ )+ if args.expert_id is not None:+ expert_status = await _expert_scope_status(args.user_id, args.expert_id)+ if expert_status != "active":+ await _skip_inactive_expert_scope(args, expert_status)+ returnnew_session = await create_chat_session(args.user_id,dry_run=False,@@ -267,11 +336,20 @@ async def _execute_copilot_turn(**kwargs):expert_id=args.expert_id,)if args.expert_id and new_session.expert_id is None:+ # The scope check above passed, so the expert was archived or+ # deleted in the window before creation. `create_chat_session`+ # drops the attribution rather than failing, which would run an+ # expert's follow-up in AutoPilot memory scope — fail closed+ # instead. Skip without deleting: archive is reversible, and+ # this window can't tell it apart from deletion. The next+ # firing's scope check routes authoritatively (missing →+ # delete, archived/paused → keep skipping).logger.warning(- f"Copilot turn schedule {args.schedule_id}: expert "- f"{args.expert_id} is no longer active/owned — "- f"created a plain (non-expert) session instead"+ f"Copilot turn schedule {args.schedule_id} skipped — expert "+ f"{args.expert_id[:12]} stopped being active/owned while the "+ f"session was being created")+ returntarget_session_id = new_session.session_idtarget_session = new_sessionlogger.info(@@ -287,9 +365,29 @@ async def _execute_copilot_turn(**kwargs):)await _self_delete_copilot_turn_schedule(args)return+ if expert_scope_was_persisted and session.expert_id != args.expert_id:+ logger.warning(+ f"Copilot turn schedule {args.schedule_id} skipped — session "+ f"{args.session_id[:12]} memory scope no longer matches the "+ "persisted schedule scope; removing schedule"+ )+ await _self_delete_copilot_turn_schedule(args)+ return+ if not expert_scope_was_persisted:+ # Legacy explicit-session jobs predate the scope field. The+ # owned target session is the only authoritative provenance+ # available, so recover from it rather than interpreting the+ # missing field as AutoPilot.+ args = args.model_copy(update={"expert_id": session.expert_id})+ if args.expert_id is not None:+ expert_status = await _expert_scope_status(args.user_id, args.expert_id)+ if expert_status != "active":+ await _skip_inactive_expert_scope(args, expert_status)+ returntarget_session_id = args.session_idtarget_session = session+ assert target_session_id is not None# `schedule_turn` (not raw `enqueue_copilot_turn`) is the right entry# point: it acquires a per-user concurrency slot AND registers the# session in the stream registry before queue-publishing, so the@@ -343,21 +441,59 @@ def _session_id_label(args: "CopilotTurnJobArgs") -> str:# is time-sensitive — a multi-hour delay defeats the purpose._CONCURRENCY_RETRY_DELAY_SECONDS = 300_MAX_CAP_RETRIES = 1+_MAX_EXPERT_LOOKUP_RETRIES = 1async def _reschedule_one_shot_after_cap(args: "CopilotTurnJobArgs") -> None:- """Re-create a one-shot copilot-turn schedule a few minutes in the future.+ await _reschedule_one_shot(+ args,+ reason="concurrency cap",+ name_suffix="cap-retry",+ retry_kind="cap",+ )+++async def _reschedule_one_shot_after_expert_unavailable(+ args: "CopilotTurnJobArgs",+) -> None:+ await _reschedule_one_shot(+ args,+ reason="transient expert lookup failure",+ name_suffix="expert-lookup-retry",+ retry_kind="expert_lookup",+ )+++async def _reschedule_one_shot(+ args: "CopilotTurnJobArgs",+ *,+ reason: str,+ name_suffix: str,+ retry_kind: Literal["cap", "expert_lookup"],+) -> None:+ """Re-create a one-shot copilot-turn schedule after a transient failure.Best-effort: failures are logged. Schedules that have already been- retried ``_MAX_CAP_RETRIES`` times are dropped to avoid loops. The- retry depth is tracked in ``CopilotTurnJobArgs.cap_retry_count``- (which round-trips through APScheduler's persisted kwargs).+ retried the limit for this failure kind are dropped to avoid loops. Retry+ depths round-trip independently through APScheduler's persisted kwargs so+ a transient expert lookup does not consume the concurrency-cap budget."""- if args.cap_retry_count >= _MAX_CAP_RETRIES:+ if retry_kind == "cap":+ retry_count = args.cap_retry_count+ max_retries = _MAX_CAP_RETRIES+ next_cap_retry_count = retry_count + 1+ next_expert_lookup_retry_count = args.expert_lookup_retry_count+ else:+ retry_count = args.expert_lookup_retry_count+ max_retries = _MAX_EXPERT_LOOKUP_RETRIES+ next_cap_retry_count = args.cap_retry_count+ next_expert_lookup_retry_count = retry_count + 1++ if retry_count >= max_retries:logger.error(f"Dropping one-shot copilot turn for session "- f"{_session_id_label(args)} — exhausted {_MAX_CAP_RETRIES} "- f"concurrency-cap retry/retries"+ f"{_session_id_label(args)} — exhausted {max_retries} "+ f"retry/retries after {reason}")returntry:@@ -369,8 +505,9 @@ async def _reschedule_one_shot_after_cap(args: "CopilotTurnJobArgs") -> None:session_id=args.session_id,message=args.message,run_at=new_run_at,- name=f"{args.schedule_id or 'copilot'}-cap-retry",- cap_retry_count=args.cap_retry_count + 1,+ name=f"{args.schedule_id or 'copilot'}-{name_suffix}",+ cap_retry_count=next_cap_retry_count,+ expert_lookup_retry_count=next_expert_lookup_retry_count,# Preserve the user's timezone across the reschedule so the new# one-shot job's trigger/timezone matches the original request.user_timezone=args.user_timezone,@@ -379,19 +516,19 @@ async def _reschedule_one_shot_after_cap(args: "CopilotTurnJobArgs") -> None:organization_id=args.organization_id,team_id=args.team_id,# And for expert attribution — dropping it would fire the retried- # turn into a plain session, escaping the expert's thread/budget.+ # turn into a plain session, escaping the expert's thread/budget+ # and its isolated memory scope.expert_id=args.expert_id,)logger.info(f"Rescheduled one-shot copilot turn for session "f"{_session_id_label(args)} to {new_run_at.isoformat()} after "- f"concurrency cap (retry {args.cap_retry_count + 1}/"- f"{_MAX_CAP_RETRIES})"+ f"{reason} (retry {retry_count + 1}/{max_retries})")except Exception:logger.warning(- f"Failed to reschedule capped one-shot copilot turn for "- f"session {_session_id_label(args)}",+ f"Failed to reschedule one-shot copilot turn for session "+ f"{_session_id_label(args)} after {reason}",exc_info=True,)@@ -435,11 +572,14 @@ async def _best_effort_unschedule(async def _self_delete_copilot_turn_schedule(args: "CopilotTurnJobArgs") -> None:- """Convenience wrapper for copilot-turn schedules whose target session is gone."""+ """Remove a copilot schedule whose target is unavailable or out of scope."""# Copilot-turn schedules aren't graph-bound — no graph target for legacy# cleanup, so a schedule_id-less job can only be removed manually.await _best_effort_unschedule(- args.schedule_id, None, args.user_id, reason="session deleted"+ args.schedule_id,+ None,+ args.user_id,+ reason="session unavailable or scope mismatch",)@@ -1246,15 +1386,16 @@ class CopilotTurnJobArgs(BaseModel):# ``None`` means "create a fresh chat at fire-time" — the executor calls# ``create_chat_session`` and routes the turn into the newly-minted# session. A non-null value pins the followup to an existing session- # owned by the same user (current chat, sub-session, etc).+ # owned by the same user and in the persisted persona scope.session_id: str | None = Nonemessage: strcron: str | None = Nonerun_at: datetime | None = None- # Set by ``_reschedule_one_shot_after_cap`` when re-creating a one-shot- # schedule after a concurrency-cap miss. Bounds the retry depth so a- # persistently-capped user can't loop forever.+ # Independent persisted retry depths for transient failures. Keeping these+ # separate prevents an expert-lookup retry from consuming the later+ # concurrency-cap retry (or vice versa).cap_retry_count: int = 0+ expert_lookup_retry_count: int = 0# Persisted so ``_reschedule_one_shot_after_cap`` can preserve the user's# timezone when re-creating a one-shot job after a concurrency-cap miss —# otherwise the rescheduled job's trigger defaults to UTC and the timezone@@ -1269,8 +1410,10 @@ class CopilotTurnJobArgs(BaseModel):team_id: str | None = None# Expert captured at schedule time so a fresh session minted at fire time# is scoped to the same expert as the chat that scheduled the follow-up —- # its runs then count toward the expert's budget and surface on her thread.- # None for plain (non-expert) chats. Optional for backward compat.+ # its runs then count toward the expert's budget, surface on her thread,+ # and read/write her isolated memory scope. None keeps legacy schedules+ # and AutoPilot follow-ups in the user's account scope. Optional for+ # backward compat with rows persisted before this field was added.expert_id: str | None = None@@ -1815,6 +1958,7 @@ class Scheduler(AppService):name: Optional[str] = None,user_timezone: str | None = None,cap_retry_count: int = 0,+ expert_lookup_retry_count: int = 0,organization_id: str | None = None,team_id: str | None = None,expert_id: str | None = None,@@ -1822,12 +1966,13 @@ class Scheduler(AppService):"""Schedule a copilot turn at a future time.When *session_id* is ``None`` the executor creates a fresh chat- at fire time and routes the turn into it. Otherwise the turn- resumes the named (existing) session with its full history.+ at fire time in the persisted Autopilot or expert scope and routes+ the turn into it. Otherwise the turn resumes the named (existing)+ session with its full history, after re-validating that scope.- *cap_retry_count* is set internally by- ``_reschedule_one_shot_after_cap`` to bound the retry depth on- concurrency-cap misses; normal callers should leave it at 0.+ *cap_retry_count* and *expert_lookup_retry_count* are set internally+ to bound their respective transient retry paths; normal callers should+ leave both at 0."""user_timezone = _resolve_timezone(user_timezone, user_id)trigger = _build_trigger(cron=cron, run_at=run_at, user_timezone=user_timezone)@@ -1839,6 +1984,7 @@ class Scheduler(AppService):cron=cron,run_at=run_at,cap_retry_count=cap_retry_count,+ expert_lookup_retry_count=expert_lookup_retry_count,user_timezone=user_timezone,organization_id=organization_id,team_id=team_id,diff --git a/autogpt_platform/backend/backend/executor/scheduler_unit_test.py b/autogpt_platform/backend/backend/executor/scheduler_unit_test.pyindex b1ef6ece06..cb14e2ac50 100644--- a/autogpt_platform/backend/backend/executor/scheduler_unit_test.py+++ b/autogpt_platform/backend/backend/executor/scheduler_unit_test.py@@ -15,6 +15,7 @@ from apscheduler.triggers.date import DateTriggerfrom backend.executor.scheduler import (_MAX_CAP_RETRIES,+ _MAX_EXPERT_LOOKUP_RETRIES,CopilotTurnJobArgs,CopilotTurnJobInfo,GraphExecutionJobArgs,@@ -25,9 +26,11 @@ from backend.executor.scheduler import (_cleanup_old_schedules_without_id,_execute_copilot_turn,_execute_graph,+ _expert_scope_status,_job_to_info,_next_run_time_iso,_reschedule_one_shot_after_cap,+ _reschedule_one_shot_after_expert_unavailable,_self_delete_copilot_turn_schedule,reconcile_stripe_tiers,)@@ -125,6 +128,7 @@ def test_job_to_info_copilot_turn_kind():info = _job_to_info(job)assert isinstance(info, CopilotTurnJobInfo)assert info.session_id == "s"+ assert info.expert_id is Nonedef test_job_to_info_legacy_rows_without_kind_default_to_graph():@@ -154,6 +158,63 @@ def test_job_to_info_unparseable_returns_none():assert _job_to_info(job) is None+# ---------------------------------------------------------------------------+# _expert_scope_status+# ---------------------------------------------------------------------------+++def _patched_experts_db(get_expert: AsyncMock):+ accessor = MagicMock()+ accessor.return_value.get_expert = get_expert+ return patch(f"{_SCHEDULER_PATH}.experts_db", new=accessor)+++def _expert_row(*, is_archived: bool = False, schedules_paused_at=None) -> MagicMock:+ return MagicMock(is_archived=is_archived, schedules_paused_at=schedules_paused_at)+++async def test_expert_scope_status_active():+ get_expert = AsyncMock(return_value=_expert_row())+ with _patched_experts_db(get_expert):+ assert await _expert_scope_status("user-1", "expert-1") == "active"+ # include_archived=True is load-bearing: without it an archived expert is+ # indistinguishable from a deleted one and the schedule would be deleted.+ get_expert.assert_awaited_once_with(+ "user-1", "expert-1", include_workflows=False, include_archived=True+ )+++async def test_expert_scope_status_archived_is_not_missing():+ get_expert = AsyncMock(return_value=_expert_row(is_archived=True))+ with _patched_experts_db(get_expert):+ assert await _expert_scope_status("user-1", "expert-1") == "archived"+++async def test_expert_scope_status_paused_is_not_missing():+ get_expert = AsyncMock(+ return_value=_expert_row(schedules_paused_at=datetime.now(tz=timezone.utc))+ )+ with _patched_experts_db(get_expert):+ assert await _expert_scope_status("user-1", "expert-1") == "paused"+++async def test_expert_scope_status_missing_when_expert_does_not_exist():+ get_expert = AsyncMock(return_value=None)+ with _patched_experts_db(get_expert):+ assert await _expert_scope_status("user-1", "expert-1") == "missing"+++async def test_expert_scope_status_unavailable_on_lookup_failure():+ get_expert = AsyncMock(side_effect=RuntimeError("db down"))+ with _patched_experts_db(get_expert):+ assert await _expert_scope_status("user-1", "expert-1") == "unavailable"++# ---------------------------------------------------------------------------# _execute_copilot_turn# ---------------------------------------------------------------------------@@ -238,78 +299,234 @@ async def test_execute_copilot_turn_creates_fresh_session_when_session_id_is_non@pytest.mark.asyncio-async def test_execute_copilot_turn_forwards_expert_id_to_fresh_session():- """A follow-up scheduled from an expert chat mints its fresh session scoped- to that expert, so runs inside the scheduled turn stay attributed to her.- Session persistence performs the active-owner check atomically."""- args = _args(session_id=None, expert_id="expert-1")- new_session = MagicMock(session_id="new-session-uuid", expert_id="expert-1")+async def test_execute_copilot_turn_creates_fresh_expert_session_in_same_scope():+ args = _args(+ session_id=None,+ expert_id="expert-1",+ organization_id="org-sched",+ team_id="team-sched",+ )+ mock_schedule_turn = AsyncMock()+ new_session = MagicMock(session_id="new-expert-session", expert_id="expert-1")mock_create_session = AsyncMock(return_value=new_session)with (- patch("backend.executor.scheduler.schedule_turn", new=AsyncMock()),- patch("backend.executor.scheduler.get_chat_session", new=AsyncMock()),+ patch("backend.executor.scheduler.schedule_turn", new=mock_schedule_turn),+ patch(+ f"{_SCHEDULER_PATH}._expert_scope_status",+ new=AsyncMock(return_value="active"),+ ),+ patch(+ "backend.executor.scheduler.create_chat_session", new=mock_create_session+ ),+ ):+ await _execute_copilot_turn(**args.model_dump(mode="json"))++ mock_create_session.assert_awaited_once_with(+ "user-1",+ dry_run=False,+ organization_id="org-sched",+ team_id="team-sched",+ expert_id="expert-1",+ )+ assert mock_schedule_turn.call_args.kwargs["session_id"] == "new-expert-session"+++async def test_execute_copilot_turn_rejects_missing_expert_before_fresh_session():+ args = _args(session_id=None, expert_id="archived-expert")+ mock_schedule_turn = AsyncMock()+ mock_create_session = AsyncMock()+ mock_self_delete = AsyncMock()++ with (+ patch("backend.executor.scheduler.schedule_turn", new=mock_schedule_turn),+ patch(+ f"{_SCHEDULER_PATH}._expert_scope_status",+ new=AsyncMock(return_value="missing"),+ ),patch("backend.executor.scheduler.create_chat_session", new=mock_create_session),+ patch(+ f"{_SCHEDULER_PATH}._self_delete_copilot_turn_schedule",+ new=mock_self_delete,+ ),+ ):+ await _execute_copilot_turn(**args.model_dump(mode="json"))++ mock_create_session.assert_not_awaited()+ mock_schedule_turn.assert_not_awaited()+ mock_self_delete.assert_awaited_once_with(args)++[email protected]("status", ["archived", "paused"])+async def test_execute_copilot_turn_skips_but_keeps_schedule_for_inactive_expert(+ status,+):+ """Archive and pause are reversible, and copilot-turn schedules have no+ persisted cadence to revive from — so the firing is skipped WITHOUT+ deleting the schedule, and without burning the transient-lookup retry."""+ args = _args(session_id=None, expert_id="expert-1")+ mock_create_session = AsyncMock()+ mock_self_delete = AsyncMock()+ mock_reschedule = AsyncMock()++ with (+ patch(+ f"{_SCHEDULER_PATH}._expert_scope_status",+ new=AsyncMock(return_value=status),+ ),+ patch(+ f"{_SCHEDULER_PATH}.create_chat_session",+ new=mock_create_session,+ ),+ patch(+ f"{_SCHEDULER_PATH}._self_delete_copilot_turn_schedule",+ new=mock_self_delete,+ ),+ patch(+ f"{_SCHEDULER_PATH}._reschedule_one_shot_after_expert_unavailable",+ new=mock_reschedule,+ ),):await _execute_copilot_turn(**args.model_dump(mode="json"))- assert mock_create_session.call_args.kwargs["expert_id"] == "expert-1"+ mock_create_session.assert_not_awaited()+ mock_self_delete.assert_not_awaited()+ mock_reschedule.assert_not_awaited()@pytest.mark.asyncio-async def test_execute_copilot_turn_degrades_to_plain_session_when_expert_archived():- """If the expert was archived (or un-owned) between scheduling and fire- time, the atomic session write returns a plain session and the scheduled- turn still dispatches there."""[email protected]("status", ["archived", "paused"])+async def test_explicit_session_skips_but_keeps_schedule_for_inactive_expert(status):+ args = _args(expert_id="expert-1")+ mock_schedule_turn = AsyncMock()+ mock_get_session = AsyncMock(return_value=MagicMock(expert_id="expert-1"))+ mock_self_delete = AsyncMock()+ mock_reschedule = AsyncMock()++ with (+ patch(f"{_SCHEDULER_PATH}.schedule_turn", new=mock_schedule_turn),+ patch(f"{_SCHEDULER_PATH}.get_chat_session", new=mock_get_session),+ patch(+ f"{_SCHEDULER_PATH}._expert_scope_status",+ new=AsyncMock(return_value=status),+ ),+ patch(+ f"{_SCHEDULER_PATH}._self_delete_copilot_turn_schedule",+ new=mock_self_delete,+ ),+ patch(+ f"{_SCHEDULER_PATH}._reschedule_one_shot_after_expert_unavailable",+ new=mock_reschedule,+ ),+ ):+ await _execute_copilot_turn(**args.model_dump(mode="json"))++ mock_schedule_turn.assert_not_awaited()+ mock_self_delete.assert_not_awaited()+ mock_reschedule.assert_not_awaited()+++async def test_execute_copilot_turn_fails_closed_when_expert_lost_during_creation():+ """The scope pre-check can race an archive/delete, after which+ ``create_chat_session`` drops the attribution and hands back a plain+ session. Dispatching there would write an expert's follow-up into+ AutoPilot memory scope, so the turn is skipped — but the schedule is+ kept, because this window can't tell reversible archive from deletion;+ the next firing's scope check deletes it iff the expert is truly gone."""args = _args(session_id=None, expert_id="expert-1")new_session = MagicMock(session_id="new-session-uuid", expert_id=None)mock_create_session = AsyncMock(return_value=new_session)mock_schedule_turn = AsyncMock()+ mock_self_delete = AsyncMock()with (patch("backend.executor.scheduler.schedule_turn", new=mock_schedule_turn),- patch("backend.executor.scheduler.get_chat_session", new=AsyncMock()),+ patch(+ f"{_SCHEDULER_PATH}._expert_scope_status",+ new=AsyncMock(return_value="active"),+ ),patch("backend.executor.scheduler.create_chat_session", new=mock_create_session),+ patch(+ f"{_SCHEDULER_PATH}._self_delete_copilot_turn_schedule",+ new=mock_self_delete,+ ),):await _execute_copilot_turn(**args.model_dump(mode="json"))- assert mock_create_session.call_args.kwargs["expert_id"] == "expert-1"- mock_schedule_turn.assert_awaited_once() # turn still fires, just plain+ mock_schedule_turn.assert_not_awaited()+ mock_self_delete.assert_not_awaited()@pytest.mark.asyncio-async def test_execute_copilot_turn_existing_session_skips_expert_revalidation():- """A follow-up into an EXISTING session doesn't mint a session, so no- expert re-validation happens — the session keeps whatever expert scope it- already has, and the fire path must not add a per-fire expert lookup."""- args = _args(expert_id="expert-1") # session_id="session-1" from _args base+async def test_fresh_expert_one_shot_retries_when_lookup_is_unavailable():+ args = _args(session_id=None, expert_id="expert-1")mock_create_session = AsyncMock()+ mock_reschedule = AsyncMock()with (- patch("backend.executor.scheduler.schedule_turn", new=AsyncMock()),patch(- "backend.executor.scheduler.get_chat_session",- new=AsyncMock(return_value=MagicMock()),+ f"{_SCHEDULER_PATH}._expert_scope_status",+ new=AsyncMock(return_value="unavailable"),),patch(- "backend.executor.scheduler.create_chat_session",+ f"{_SCHEDULER_PATH}.create_chat_session",new=mock_create_session,),+ patch(+ f"{_SCHEDULER_PATH}._reschedule_one_shot_after_expert_unavailable",+ new=mock_reschedule,+ ),):await _execute_copilot_turn(**args.model_dump(mode="json"))mock_create_session.assert_not_awaited()+ mock_reschedule.assert_awaited_once_with(args)+++async def test_execute_copilot_turn_preserves_legacy_fresh_autopilot_job():+ args = _args(session_id=None)+ payload = args.model_dump(mode="json", exclude={"expert_id"})+ mock_schedule_turn = AsyncMock()+ new_session = MagicMock(session_id="new-legacy-session", expert_id=None)+ mock_create_session = AsyncMock(return_value=new_session)+ mock_self_delete = AsyncMock()++ with (+ patch("backend.executor.scheduler.schedule_turn", new=mock_schedule_turn),+ patch(+ "backend.executor.scheduler.create_chat_session", new=mock_create_session+ ),+ patch(+ f"{_SCHEDULER_PATH}._self_delete_copilot_turn_schedule",+ new=mock_self_delete,+ ),+ ):+ await _execute_copilot_turn(**payload)++ mock_create_session.assert_awaited_once_with(+ "user-1",+ dry_run=False,+ organization_id=None,+ team_id=None,+ expert_id=None,+ )+ assert mock_schedule_turn.call_args.kwargs["session_id"] == "new-legacy-session"+ mock_self_delete.assert_not_awaited()@pytest.mark.asyncioasync def test_execute_copilot_turn_dispatches_when_session_exists():args = _args()mock_schedule_turn = AsyncMock()- mock_get_session = AsyncMock(return_value=MagicMock())+ mock_get_session = AsyncMock(return_value=MagicMock(expert_id=None))with (patch("backend.executor.scheduler.schedule_turn", new=mock_schedule_turn),@@ -324,6 +541,124 @@ async def test_execute_copilot_turn_dispatches_when_session_exists():assert call_kwargs["tool_name"] == "schedule_followup"+async def test_execute_copilot_turn_dispatches_when_expert_scope_matches():+ args = _args(expert_id="expert-1")+ mock_schedule_turn = AsyncMock()+ mock_get_session = AsyncMock(return_value=MagicMock(expert_id="expert-1"))++ with (+ patch("backend.executor.scheduler.schedule_turn", new=mock_schedule_turn),+ patch("backend.executor.scheduler.get_chat_session", new=mock_get_session),+ patch(+ f"{_SCHEDULER_PATH}._expert_scope_status",+ new=AsyncMock(return_value="active"),+ ),+ ):+ await _execute_copilot_turn(**args.model_dump(mode="json"))++ mock_schedule_turn.assert_awaited_once()+++async def test_explicit_expert_one_shot_retries_when_lookup_is_unavailable():+ args = _args(expert_id="expert-1")+ mock_schedule_turn = AsyncMock()+ mock_get_session = AsyncMock(return_value=MagicMock(expert_id="expert-1"))+ mock_reschedule = AsyncMock()++ with (+ patch(f"{_SCHEDULER_PATH}.schedule_turn", new=mock_schedule_turn),+ patch(f"{_SCHEDULER_PATH}.get_chat_session", new=mock_get_session),+ patch(+ f"{_SCHEDULER_PATH}._expert_scope_status",+ new=AsyncMock(return_value="unavailable"),+ ),+ patch(+ f"{_SCHEDULER_PATH}._reschedule_one_shot_after_expert_unavailable",+ new=mock_reschedule,+ ),+ ):+ await _execute_copilot_turn(**args.model_dump(mode="json"))++ mock_schedule_turn.assert_not_awaited()+ mock_reschedule.assert_awaited_once_with(args)+++async def test_expert_cron_does_not_duplicate_retry_when_lookup_is_unavailable():+ args = _args(+ session_id=None,+ expert_id="expert-1",+ run_at=None,+ cron="* * * * *",+ )+ mock_reschedule = AsyncMock()++ with (+ patch(+ f"{_SCHEDULER_PATH}._expert_scope_status",+ new=AsyncMock(return_value="unavailable"),+ ),+ patch(+ f"{_SCHEDULER_PATH}._reschedule_one_shot_after_expert_unavailable",+ new=mock_reschedule,+ ),+ ):+ await _execute_copilot_turn(**args.model_dump(mode="json"))++ mock_reschedule.assert_not_awaited()+++async def test_execute_copilot_turn_recovers_legacy_scope_from_owned_session():+ args = _args()+ payload = args.model_dump(mode="json", exclude={"expert_id"})+ session = MagicMock(expert_id="expert-1")+ mock_schedule_turn = AsyncMock()+ mock_get_session = AsyncMock(return_value=session)+ mock_self_delete = AsyncMock()++ with (+ patch("backend.executor.scheduler.schedule_turn", new=mock_schedule_turn),+ patch("backend.executor.scheduler.get_chat_session", new=mock_get_session),+ patch(+ f"{_SCHEDULER_PATH}._expert_scope_status",+ new=AsyncMock(return_value="active"),+ ) as scope_status,+ patch(+ f"{_SCHEDULER_PATH}._self_delete_copilot_turn_schedule",+ new=mock_self_delete,+ ),+ ):+ await _execute_copilot_turn(**payload)++ scope_status.assert_awaited_once_with("user-1", "expert-1")+ mock_schedule_turn.assert_awaited_once()+ mock_self_delete.assert_not_awaited()+++async def test_execute_copilot_turn_rejects_changed_explicit_session_scope():+ args = _args(expert_id="expert-1")+ mock_schedule_turn = AsyncMock()+ mock_get_session = AsyncMock(return_value=MagicMock(expert_id="expert-2"))+ mock_self_delete = AsyncMock()++ with (+ patch("backend.executor.scheduler.schedule_turn", new=mock_schedule_turn),+ patch("backend.executor.scheduler.get_chat_session", new=mock_get_session),+ patch(+ f"{_SCHEDULER_PATH}._self_delete_copilot_turn_schedule",+ new=mock_self_delete,+ ),+ ):+ await _execute_copilot_turn(**args.model_dump(mode="json"))++ mock_schedule_turn.assert_not_awaited()+ mock_self_delete.assert_awaited_once_with(args)++@pytest.mark.asyncioasync def test_execute_copilot_turn_concurrency_cap_reschedules_one_shot():"""When schedule_turn raises ConcurrentTurnLimitError on a one-shot@@ -332,7 +667,7 @@ async def test_execute_copilot_turn_concurrency_cap_reschedules_one_shot():args = _args() # run_at set → one-shotmock_schedule_turn = AsyncMock(side_effect=ConcurrentTurnLimitError("cap"))- mock_get_session = AsyncMock(return_value=MagicMock())+ mock_get_session = AsyncMock(return_value=MagicMock(expert_id=None))mock_reschedule = AsyncMock()with (@@ -353,7 +688,7 @@ async def test_execute_copilot_turn_concurrency_cap_does_not_reschedule_cron():args = _args(run_at=None, cron="* * * * *") # recurringmock_schedule_turn = AsyncMock(side_effect=ConcurrentTurnLimitError("cap"))- mock_get_session = AsyncMock(return_value=MagicMock())+ mock_get_session = AsyncMock(return_value=MagicMock(expert_id=None))mock_reschedule = AsyncMock()with (@@ -371,7 +706,7 @@ async def test_execute_copilot_turn_swallows_generic_exceptions():"""Non-concurrency errors should be logged but not crash the scheduler."""args = _args()mock_schedule_turn = AsyncMock(side_effect=RuntimeError("transient queue error"))- mock_get_session = AsyncMock(return_value=MagicMock())+ mock_get_session = AsyncMock(return_value=MagicMock(expert_id=None))with (patch("backend.executor.scheduler.schedule_turn", new=mock_schedule_turn),@@ -505,10 +840,10 @@ async def test_reschedule_after_cap_preserves_user_timezone():@pytest.mark.asyncio-async def test_reschedule_after_cap_preserves_expert_id():+async def test_reschedule_after_cap_preserves_expert_scope():"""The reschedule path must forward the original expert_id, otherwise acapped expert follow-up retries into a plain session and its work escapes- the expert's thread and budget."""+ the expert's thread, budget, and isolated memory scope."""args = _args(cap_retry_count=0, expert_id="expert-1")mock_client = AsyncMock()with patch(f"{_SCHEDULER_PATH}.get_scheduler_client", return_value=mock_client):@@ -536,6 +871,63 @@ async def test_reschedule_after_cap_swallows_errors():await _reschedule_one_shot_after_cap(args)+async def test_expert_lookup_retry_preserves_scope_and_is_bounded():+ args = _args(+ cap_retry_count=0,+ expert_id="expert-1",+ organization_id="org-1",+ team_id="team-1",+ )+ mock_client = AsyncMock()+ with patch(f"{_SCHEDULER_PATH}.get_scheduler_client", return_value=mock_client):+ await _reschedule_one_shot_after_expert_unavailable(args)++ kwargs = mock_client.add_copilot_turn_schedule.await_args.kwargs+ assert kwargs["cap_retry_count"] == 0+ assert kwargs["expert_lookup_retry_count"] == 1+ assert kwargs["expert_id"] == "expert-1"+ assert kwargs["organization_id"] == "org-1"+ assert kwargs["team_id"] == "team-1"++ exhausted = args.model_copy(+ update={"expert_lookup_retry_count": _MAX_EXPERT_LOOKUP_RETRIES}+ )+ mock_client.reset_mock()+ with patch(f"{_SCHEDULER_PATH}.get_scheduler_client", return_value=mock_client):+ await _reschedule_one_shot_after_expert_unavailable(exhausted)+ mock_client.add_copilot_turn_schedule.assert_not_awaited()+++async def test_expert_lookup_and_cap_retries_have_independent_budgets():+ mock_client = AsyncMock()+ after_expert_retry = _args(+ cap_retry_count=0,+ expert_lookup_retry_count=_MAX_EXPERT_LOOKUP_RETRIES,+ expert_id="expert-1",+ )+ with patch(f"{_SCHEDULER_PATH}.get_scheduler_client", return_value=mock_client):+ await _reschedule_one_shot_after_cap(after_expert_retry)++ cap_kwargs = mock_client.add_copilot_turn_schedule.await_args.kwargs+ assert cap_kwargs["cap_retry_count"] == 1+ assert cap_kwargs["expert_lookup_retry_count"] == _MAX_EXPERT_LOOKUP_RETRIES++ mock_client.reset_mock()+ after_cap_retry = _args(+ cap_retry_count=_MAX_CAP_RETRIES,+ expert_lookup_retry_count=0,+ expert_id="expert-1",+ )+ with patch(f"{_SCHEDULER_PATH}.get_scheduler_client", return_value=mock_client):+ await _reschedule_one_shot_after_expert_unavailable(after_cap_retry)++ expert_kwargs = mock_client.add_copilot_turn_schedule.await_args.kwargs+ assert expert_kwargs["cap_retry_count"] == _MAX_CAP_RETRIES+ assert expert_kwargs["expert_lookup_retry_count"] == 1++# ---------------------------------------------------------------------------# GraphExecutionJobArgs back-compat — legacy rows have no `kind`# ---------------------------------------------------------------------------@@ -619,6 +1011,32 @@ def test_copilot_turn_args_cap_retry_count_defaults_to_zero():run_at=datetime.now(tz=timezone.utc),)assert args.cap_retry_count == 0+ assert args.expert_lookup_retry_count == 0+ assert args.expert_id is None+++def test_add_copilot_turn_schedule_persists_expert_scope():+ scheduler = Scheduler(register_system_tasks=False)+ job = _mock_job({})+ run_at = datetime.now(tz=timezone.utc) + timedelta(hours=1)++ with patch.object(scheduler, "_persist_schedule", return_value=job) as persist:+ info = scheduler.add_copilot_turn_schedule(+ user_id="user-1",+ session_id=None,+ message="daily brief",+ run_at=run_at,+ user_timezone="UTC",+ cap_retry_count=1,+ expert_lookup_retry_count=1,+ expert_id="expert-1",+ )++ job_args = persist.call_args.kwargs["job_args"]+ assert job_args.cap_retry_count == 1+ assert job_args.expert_lookup_retry_count == 1+ assert job_args.expert_id == "expert-1"+ assert info.expert_id == "expert-1"# ---------------------------------------------------------------------------diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 25dee44d55..82deacee05 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -3401,7 +3401,7 @@"required": false,"schema": {"anyOf": [- { "type": "string", "maxLength": 128 },+ { "type": "string", "minLength": 1, "maxLength": 128 },{ "type": "null" }],"title": "Expert Id"@@ -16176,6 +16176,11 @@"title": "Cap Retry Count","default": 0},+ "expert_lookup_retry_count": {+ "type": "integer",+ "title": "Expert Lookup Retry Count",+ "default": 0+ },"user_timezone": {"anyOf": [{ "type": "string" }, { "type": "null" }],"title": "User Timezone"