AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/copilot): stop empty untitled dream sessions polluting the chat list (#13332)
Nicholas Tindlece6ab7b074a6
diff --git a/autogpt_platform/backend/backend/api/features/orgs/regression_test.py b/autogpt_platform/backend/backend/api/features/orgs/regression_test.pyindex fd43453ca2..0cc175ae2e 100644--- a/autogpt_platform/backend/backend/api/features/orgs/regression_test.py+++ b/autogpt_platform/backend/backend/api/features/orgs/regression_test.py@@ -699,25 +699,24 @@ class TestChatSessionUserIdIsolation:@pytest.mark.asyncioasync def test_regression_list_sessions_filtered_by_user(self):"""get_user_chat_sessions() filters by userId so each user only- sees their own chat sessions."""- session_rows = [_make_chat_session_row()]+ sees their own chat sessions.- mock_actions = AsyncMock()- mock_actions.find_many = AsyncMock(return_value=session_rows)+ The list path is raw SQL (Prisma's JsonFilter can't express the+ ``metadata->>'kind'`` dream exclusion), so the userId predicate+ lives in the SQL WHERE clause with the user_id bound as $1.+ """+ session_rows = [_make_chat_session_row()]- with patch(- "backend.copilot.db.PrismaChatSession.prisma",- return_value=mock_actions,- ):+ raw = AsyncMock(return_value=session_rows)+ with patch("backend.copilot.db.db.query_raw_with_schema", raw):from backend.copilot.db import get_user_chat_sessionsresults = await get_user_chat_sessions(USER_ID)- mock_actions.find_many.assert_called_once()- where_arg = mock_actions.find_many.call_args.kwargs.get(- "where", mock_actions.find_many.call_args[1].get("where")- )- assert where_arg["userId"] == USER_ID+ raw.assert_called_once()+ query = raw.call_args.args[0]+ assert '"userId" = $1' in query+ assert raw.call_args.args[1] == USER_IDassert len(results) == 1@pytest.mark.asyncio@@ -746,24 +745,18 @@ class TestChatSessionUserIdIsolation:@pytest.mark.asyncioasync def test_regression_list_sessions_excludes_other_users(self):- """get_user_chat_sessions() with OTHER_USER_ID should not return- sessions belonging to USER_ID."""- mock_actions = AsyncMock()- mock_actions.find_many = AsyncMock(return_value=[])-- with patch(- "backend.copilot.db.PrismaChatSession.prisma",- return_value=mock_actions,- ):+ """get_user_chat_sessions() with OTHER_USER_ID binds that user_id+ into the WHERE clause, so it can never return USER_ID's sessions."""+ raw = AsyncMock(return_value=[])+ with patch("backend.copilot.db.db.query_raw_with_schema", raw):from backend.copilot.db import get_user_chat_sessionsresults = await get_user_chat_sessions(OTHER_USER_ID)- mock_actions.find_many.assert_called_once()- where_arg = mock_actions.find_many.call_args.kwargs.get(- "where", mock_actions.find_many.call_args[1].get("where")- )- assert where_arg["userId"] == OTHER_USER_ID+ raw.assert_called_once()+ query = raw.call_args.args[0]+ assert '"userId" = $1' in query+ assert raw.call_args.args[1] == OTHER_USER_IDassert len(results) == 0@pytest.mark.asynciodiff --git a/autogpt_platform/backend/backend/api/features/search/content_handlers.py b/autogpt_platform/backend/backend/api/features/search/content_handlers.pyindex 0e3d0acdf1..4606c2bf2f 100644--- a/autogpt_platform/backend/backend/api/features/search/content_handlers.py+++ b/autogpt_platform/backend/backend/api/features/search/content_handlers.py@@ -19,6 +19,7 @@ from prisma.enums import ContentTypefrom backend.blocks import get_blocksfrom backend.blocks.llm import LLMModel+from backend.copilot.db import exclude_dream_sessions_sqlfrom backend.data.db import query_raw_with_schemafrom backend.util.docs import get_docs_rootfrom backend.util.text import split_camelcase@@ -951,6 +952,9 @@ class ChatSessionHandler(ContentHandler):return ContentType.CHAT_SESSIONasync def get_missing_items(self, batch_size: int) -> list[ContentItem]:+ # Dream-pass sessions are titled but are pipeline artifacts, not+ # user chats — keep them out of the embedding backfill (same+ # null-safe predicate as the sidebar list in copilot/db.py).missing = await query_raw_with_schema("""SELECT@@ -964,6 +968,9 @@ class ChatSessionHandler(ContentHandler):AND uce."userId" = cs."userId"WHERE cs.title IS NOT NULLAND btrim(cs.title) <> ''+ AND """+ + exclude_dream_sessions_sql("cs.metadata")+ + """AND uce."contentId" IS NULLLIMIT $1""",@@ -992,7 +999,8 @@ class ChatSessionHandler(ContentHandler):SELECT COUNT(*) as countFROM {schema_prefix}"ChatSession"WHERE title IS NOT NULL AND btrim(title) <> ''- """+ AND """+ + exclude_dream_sessions_sql())total = total_result[0]["count"] if total_result else 0@@ -1005,7 +1013,8 @@ class ChatSessionHandler(ContentHandler):AND uce."contentType" = 'CHAT_SESSION'::{schema_prefix}"ContentType"AND uce."userId" = cs."userId"WHERE cs.title IS NOT NULL AND btrim(cs.title) <> ''- """+ AND """+ + exclude_dream_sessions_sql("cs.metadata"))with_embeddings = with_result[0]["count"] if with_result else 0@@ -1018,13 +1027,16 @@ class ChatSessionHandler(ContentHandler):async def get_valid_content_ids(self) -> set[str]:# Mirrors ``get_missing_items``: sessions without a non-empty# title aren't embedded; a session that was deleted or had its- # title cleared should have its embedding cleaned up.+ # title cleared should have its embedding cleaned up. Excluding+ # dream sessions here also makes the cleanup sweep delete any+ # dream-session embeddings that were created before the filter.rows = await query_raw_with_schema("""SELECT idFROM {schema_prefix}"ChatSession"WHERE title IS NOT NULL AND btrim(title) <> ''- """,+ AND """+ + exclude_dream_sessions_sql(),)return {row["id"] for row in rows}diff --git a/autogpt_platform/backend/backend/api/features/search/content_handlers_test.py b/autogpt_platform/backend/backend/api/features/search/content_handlers_test.pyindex d4ea2dd81d..692054459a 100644--- a/autogpt_platform/backend/backend/api/features/search/content_handlers_test.py+++ b/autogpt_platform/backend/backend/api/features/search/content_handlers_test.py@@ -11,6 +11,7 @@ from prisma.enums import ContentTypefrom backend.api.features.search.content_handlers import (CONTENT_HANDLERS,BlockHandler,+ ChatSessionHandler,DocumentationHandler,LibraryAgentHandler,StoreAgentHandler,@@ -678,6 +679,34 @@ async def test_library_agent_handler_stats():assert stats["without_embeddings"] == 6[email protected](loop_scope="session")+async def test_chat_session_handler_excludes_dream_sessions_in_every_query():+ """Dream-pass sessions are titled pipeline artifacts — every+ ChatSessionHandler query (backfill, stats, cleanup validity) must carry+ the null-safe dream exclusion so nightly dream sessions are neither+ embedded nor treated as valid embedding owners."""+ captured: list[str] = []++ async def fake_query_raw(sql: str, *args, **kwargs):+ captured.append(sql)+ if "COUNT" in sql.upper():+ return [{"count": 0}]+ return []++ with patch(+ "backend.api.features.search.content_handlers.query_raw_with_schema",+ side_effect=fake_query_raw,+ ):+ handler = ChatSessionHandler()+ await handler.get_missing_items(batch_size=10)+ await handler.get_stats()+ await handler.get_valid_content_ids()++ assert len(captured) == 4+ for sql in captured:+ assert "IS DISTINCT FROM 'dream'" in sql, f"missing dream filter in: {sql}"++@pytest.mark.asyncioasync def test_documentation_handler_degrades_without_bundled_docs():"""A docs-less deployment must no-op (empty results), not crash thediff --git a/autogpt_platform/backend/backend/copilot/db.py b/autogpt_platform/backend/backend/copilot/db.pyindex 2f725403ef..c67b4d2750 100644--- a/autogpt_platform/backend/backend/copilot/db.py+++ b/autogpt_platform/backend/backend/copilot/db.py@@ -576,6 +576,35 @@ async def add_chat_messages_batch(raise RuntimeError(f"Failed to insert messages after {max_retries} attempts")+# WHERE fragment shared by the user-facing session list and its count so+# the sidebar list, its pagination total, and /search/global stay+# consistent.+#+# ``IS DISTINCT FROM`` (not ``<>``) is load-bearing: sessions created+# before ``ChatSessionMetadata.kind`` existed have no ``kind`` key in+# their metadata JSON, so ``metadata->>'kind'`` evaluates to SQL NULL+# for them. ``NULL <> 'dream'`` is NULL → WHERE drops the row → every+# legacy chat would vanish from the sidebar. ``IS DISTINCT FROM`` treats+# NULL as an ordinary comparable value, so missing-key / null-metadata+# rows stay visible and only rows with ``kind = 'dream'`` are excluded.+#+# Raw SQL because the Python Prisma client's ``JsonFilter`` supports only+# whole-value ``equals`` / ``not`` — it has no ``path`` access at all.+#+# ``exclude_dream_sessions_sql`` is the public form for OTHER ChatSession+# listing surfaces (e.g. the search-embedding backfill in+# ``api/features/search/content_handlers.py``) so the predicate can't+# drift between them; ``column`` lets aliased queries pass+# ``cs.metadata``.+++def exclude_dream_sessions_sql(column: str = "metadata") -> str:+ return f"({column}->>'kind' IS DISTINCT FROM 'dream')"+++_EXCLUDE_DREAM_SESSIONS_SQL = exclude_dream_sessions_sql()++async def get_user_chat_sessions(user_id: str,limit: int = 50,@@ -586,26 +615,40 @@ async def get_user_chat_sessions() -> list[ChatSessionInfo]:"""Get chat sessions for a user, ordered by most recent.+ Dream-pass sessions (``metadata.kind == "dream"``) are hidden from+ this LIST path — they are pipeline artifacts, not user chats, until+ the UI grows a dedicated surface for them (P6). Fetch-by-id paths+ (:func:`get_chat_session_metadata`, :func:`get_chat_messages_paginated`)+ intentionally still return them.+``title_contains`` is a case-insensitive substring filter used by/search/global so sessions are findable by literal title matchwithout waiting on async embedding.``expert_id`` restricts the listing to sessions scoped to that expert."""- where: ChatSessionWhereInput = {"userId": user_id}- if org_scope := _own_org_scope(organization_id):- where["AND"] = org_scope+ params: list[Any] = [user_id]+ conditions = ['"userId" = $1', _EXCLUDE_DREAM_SESSIONS_SQL]+ if organization_id is not None:+ params.append(organization_id)+ conditions.append(+ f'("organizationId" = ${len(params)} OR "organizationId" IS NULL)'+ )if title_contains:- where["title"] = {"contains": title_contains, "mode": "insensitive"}+ params.append(f"%{_escape_like(title_contains)}%")+ conditions.append(f'"title" ILIKE ${len(params)}')if expert_id:- where["expertId"] = expert_id- prisma_sessions = await PrismaChatSession.prisma().find_many(- where=where,- order=[{"isPinned": "desc"}, {"updatedAt": "desc"}],- take=limit,- skip=offset,+ params.append(expert_id)+ conditions.append(f'"expertId" = ${len(params)}')+ params.extend((limit, offset))+ query = (+ 'SELECT * FROM {schema_prefix}"ChatSession" WHERE '+ + " AND ".join(conditions)+ + ' ORDER BY "isPinned" DESC, "updatedAt" DESC '+ + f"LIMIT ${len(params) - 1} OFFSET ${len(params)}")- return [ChatSessionInfo.from_db(s) for s in prisma_sessions]+ sessions = await db.query_raw_with_schema(query, *params, model=PrismaChatSession)+ return [ChatSessionInfo.from_db(s) for s in sessions]async def get_user_session_count(@@ -613,13 +656,37 @@ async def get_user_session_count(organization_id: str | None = None,expert_id: str | None = None,) -> int:- """Get the total number of chat sessions for a user."""- where: ChatSessionWhereInput = {"userId": user_id}- if org_scope := _own_org_scope(organization_id):- where["AND"] = org_scope+ """Get the total number of chat sessions for a user.++ Applies the same dream-session exclusion, org scoping, and expert+ filter as :func:`get_user_chat_sessions` so pagination totals always+ match the visible list.+ """+ params: list[Any] = [user_id]+ conditions = ['"userId" = $1', _EXCLUDE_DREAM_SESSIONS_SQL]+ if organization_id is not None:+ params.append(organization_id)+ conditions.append(+ f'("organizationId" = ${len(params)} OR "organizationId" IS NULL)'+ )if expert_id:- where["expertId"] = expert_id- return await PrismaChatSession.prisma().count(where=where)+ params.append(expert_id)+ conditions.append(f'"expertId" = ${len(params)}')+ rows = await db.query_raw_with_schema(+ 'SELECT COUNT(*)::int AS "count" FROM {schema_prefix}"ChatSession" WHERE '+ + " AND ".join(conditions),+ *params,+ )+ return rows[0]["count"] if rows else 0+++def _escape_like(value: str) -> str:+ """Escape LIKE wildcards so ``title_contains`` matches literally.++ Parity with Prisma's ``contains`` filter, which escapes them too;+ Postgres' default LIKE escape character is the backslash.+ """+ return value.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")async def delete_chat_session(diff --git a/autogpt_platform/backend/backend/copilot/db_session_listing_test.py b/autogpt_platform/backend/backend/copilot/db_session_listing_test.pynew file mode 100644index 0000000000..a029164260--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/db_session_listing_test.py@@ -0,0 +1,234 @@+"""Tests for copilot.db session LIST paths — dream-session exclusion.++The dream pass creates ChatSession rows with ``metadata.kind == "dream"``.+Those must be hidden from the user-facing LIST paths (chat sidebar,+pagination count, /search/global title search) while staying fetchable+by id. The whole risk is SQL NULL semantics: most sessions predate the+``kind`` metadata key, so ``metadata->>'kind'`` is NULL for them and a+naive ``<>`` comparison would silently hide every normal chat.++Mock-based tests pin the exact WHERE construction; the integration tests+at the bottom prove the NULL semantics against the real Postgres.+"""++from __future__ import annotations++from datetime import UTC, datetime+from unittest.mock import AsyncMock, patch+from uuid import uuid4++import pytest+from prisma.enums import ResourceVisibility+from prisma.models import ChatSession as PrismaChatSession+from prisma.types import ChatSessionCreateInput++from backend.copilot.db import (+ create_chat_session,+ get_chat_session_metadata,+ get_user_chat_sessions,+ get_user_session_count,+ update_chat_session_title,+)+from backend.copilot.model import ChatSessionMetadata+from backend.util.json import SafeJson++_NULL_SAFE_DREAM_FILTER = "(metadata->>'kind' IS DISTINCT FROM 'dream')"+_RAW_QUERY_TARGET = "backend.copilot.db.db.query_raw_with_schema"+++def _make_prisma_session(session_id: str = "sess-1") -> PrismaChatSession:+ now = datetime.now(UTC)+ return PrismaChatSession(+ id=session_id,+ createdAt=now,+ updatedAt=now,+ userId="user-1",+ title="hello",+ credentials="{}",+ successfulAgentRuns="{}",+ successfulAgentSchedules="{}",+ metadata="{}",+ totalPromptTokens=0,+ totalCompletionTokens=0,+ chatStatus="idle",+ isPinned=False,+ isShared=False,+ shareToken=None,+ sharedAt=None,+ autoShareExecutions=False,+ visibility=ResourceVisibility.PRIVATE,+ )+++# ---------- WHERE construction (mocked raw query) ----------+++async def test_list_query_excludes_dreams_with_null_safe_operator():+ """The list WHERE clause uses IS DISTINCT FROM, never a bare inequality.++ ``NULL <> 'dream'`` evaluates to NULL for legacy rows without a+ ``kind`` key, which would hide every normal chat from the sidebar.+ """+ raw = AsyncMock(return_value=[])+ with patch(_RAW_QUERY_TARGET, raw):+ result = await get_user_chat_sessions("u1")++ assert result == []+ query = raw.call_args.args[0]+ assert _NULL_SAFE_DREAM_FILTER in query+ assert "<>" not in query+ assert "!=" not in query+ assert "ILIKE" not in query+ assert raw.call_args.args[1:] == ("u1", 50, 0)+ assert raw.call_args.kwargs["model"] is PrismaChatSession+++async def test_list_query_maps_raw_rows_to_chat_session_info():+ raw = AsyncMock(return_value=[_make_prisma_session("sess-42")])+ with patch(_RAW_QUERY_TARGET, raw):+ result = await get_user_chat_sessions("user-1")++ assert len(result) == 1+ assert result[0].session_id == "sess-42"+ assert result[0].user_id == "user-1"+ assert result[0].metadata.kind == "normal"+++async def test_title_search_escapes_like_wildcards_and_keeps_dream_filter():+ """User-supplied search text must match literally (no %/_ wildcards)."""+ raw = AsyncMock(return_value=[])+ with patch(_RAW_QUERY_TARGET, raw):+ await get_user_chat_sessions(+ "u1", limit=10, offset=5, title_contains="50%_done\\"+ )++ query = raw.call_args.args[0]+ assert _NULL_SAFE_DREAM_FILTER in query+ assert '"title" ILIKE $2' in query+ assert "LIMIT $3 OFFSET $4" in query+ assert raw.call_args.args[1:] == ("u1", "%50\\%\\_done\\\\%", 10, 5)+++async def test_count_query_uses_same_dream_exclusion_as_list():+ """Sidebar pagination count must stay consistent with the visible list."""+ raw = AsyncMock(return_value=[{"count": 7}])+ with patch(_RAW_QUERY_TARGET, raw):+ count = await get_user_session_count("u1")++ assert count == 7+ query = raw.call_args.args[0]+ assert _NULL_SAFE_DREAM_FILTER in query+ assert raw.call_args.args[1:] == ("u1",)+++async def test_count_returns_zero_when_query_yields_no_rows():+ raw = AsyncMock(return_value=[])+ with patch(_RAW_QUERY_TARGET, raw):+ assert await get_user_session_count("u1") == 0+++# ---------- NULL semantics against the real database ----------+++async def _create_legacy_session_without_kind_key(user_id: str) -> str:+ """Insert a pre-dream-PR row whose metadata JSON has no ``kind`` key."""+ legacy_id = str(uuid4())+ await PrismaChatSession.prisma().create(+ data=ChatSessionCreateInput(+ id=legacy_id,+ userId=user_id,+ credentials=SafeJson({}),+ successfulAgentRuns=SafeJson({}),+ successfulAgentSchedules=SafeJson({}),+ metadata=SafeJson({"dry_run": False}),+ )+ )+ return legacy_id++[email protected](loop_scope="session")+async def test_dream_sessions_hidden_from_list_but_fetchable_by_id(+ setup_test_user, test_user_id+):+ """Legacy rows (no ``kind`` key) stay listed; dream rows are hidden+ from the list yet still resolvable by session_id (Memory Visualizer+ and dream flows read them directly)."""+ normal = await create_chat_session(str(uuid4()), test_user_id)+ legacy_id = await _create_legacy_session_without_kind_key(test_user_id)+ dream = await create_chat_session(+ str(uuid4()),+ test_user_id,+ metadata=ChatSessionMetadata(kind="dream", dream_pass_id="pass-1"),+ )++ listed_ids = {+ s.session_id for s in await get_user_chat_sessions(test_user_id, limit=10_000)+ }+ assert normal.session_id in listed_ids+ assert legacy_id in listed_ids+ assert dream.session_id not in listed_ids++ fetched = await get_chat_session_metadata(dream.session_id)+ assert fetched is not None+ assert fetched.metadata.kind == "dream"+ assert fetched.metadata.dream_pass_id == "pass-1"++[email protected](loop_scope="session")+async def test_session_count_stays_consistent_with_visible_list(+ setup_test_user, test_user_id+):+ """Creating a dream session must not bump the pagination total."""+ count_before = await get_user_session_count(test_user_id)++ await create_chat_session(str(uuid4()), test_user_id)+ await create_chat_session(+ str(uuid4()),+ test_user_id,+ metadata=ChatSessionMetadata(kind="dream", dream_pass_id="pass-2"),+ )++ count_after = await get_user_session_count(test_user_id)+ assert count_after == count_before + 1++ listed = await get_user_chat_sessions(test_user_id, limit=10_000)+ assert count_after == len(listed)++[email protected](loop_scope="session")+async def test_title_search_matches_literally_and_skips_dream_sessions(+ setup_test_user, test_user_id+):+ """ILIKE search is case-insensitive, treats ``_`` literally, and never+ surfaces dream sessions even on an exact title match."""+ marker = f"Ndl{uuid4().hex[:8]}"++ normal = await create_chat_session(str(uuid4()), test_user_id)+ assert await update_chat_session_title(+ normal.session_id, test_user_id, f"{marker}_alpha plan"+ )+ decoy = await create_chat_session(str(uuid4()), test_user_id)+ assert await update_chat_session_title(+ decoy.session_id, test_user_id, f"{marker}Xalpha plan"+ )+ dream = await create_chat_session(+ str(uuid4()),+ test_user_id,+ metadata=ChatSessionMetadata(kind="dream", dream_pass_id="pass-3"),+ )+ assert await update_chat_session_title(+ dream.session_id, test_user_id, f"{marker}_alpha dream"+ )++ results = await get_user_chat_sessions(+ test_user_id, title_contains=f"{marker}_ALPHA"+ )+ result_ids = {s.session_id for s in results}+ assert normal.session_id in result_ids+ assert decoy.session_id not in result_ids+ assert dream.session_id not in result_idsdiff --git a/autogpt_platform/backend/backend/copilot/db_test.py b/autogpt_platform/backend/backend/copilot/db_test.pyindex 35843939ed..a00d325864 100644--- a/autogpt_platform/backend/backend/copilot/db_test.py+++ b/autogpt_platform/backend/backend/copilot/db_test.py@@ -659,15 +659,18 @@ async def test_update_chat_session_pinned_not_found():@pytest.mark.asyncioasync def test_get_user_chat_sessions_orders_pinned_first():- """Pinned sessions must be ordered ahead of unpinned, then by recency."""- find_many = AsyncMock(return_value=[])- with patch.object(- PrismaChatSession, "prisma", return_value=AsyncMock(find_many=find_many)- ):+ """Pinned sessions must be ordered ahead of unpinned, then by recency.++ The list uses raw SQL (Prisma's ``JsonFilter`` can't express the dream+ ``metadata->>'kind'`` exclusion), so the ordering lives in the ``ORDER+ BY`` clause rather than a Prisma ``order`` argument.+ """+ raw = AsyncMock(return_value=[])+ with patch("backend.copilot.db.db.query_raw_with_schema", raw):await get_user_chat_sessions("user-abc", limit=10, offset=0)- order = find_many.call_args.kwargs["order"]- assert order == [{"isPinned": "desc"}, {"updatedAt": "desc"}]+ query = raw.call_args.args[0]+ assert 'ORDER BY "isPinned" DESC, "updatedAt" DESC' in query# NOTE: previously this file had a separate suite for ``db.get_chat_session``diff --git a/autogpt_platform/backend/backend/copilot/dream/apply.py b/autogpt_platform/backend/backend/copilot/dream/apply.pyindex 8a3cc158aa..1638f2fd5b 100644--- a/autogpt_platform/backend/backend/copilot/dream/apply.py+++ b/autogpt_platform/backend/backend/copilot/dream/apply.py@@ -13,6 +13,8 @@ A ``ChatSession`` shell (``metadata.kind='dream'`` +provenance can reference its id; the assistant message holding``summary_for_user`` is appended LAST, after the ops above, so a partwayfailure leaves an empty dream rather than a narrative with no memory.+A pass with no operations at all creates neither — see the empty-pass+guard at the top of ``apply_operations``."""from __future__ import annotations@@ -43,6 +45,7 @@ from backend.copilot.tools.graphiti_forget import (from backend.util.feature_flag import Flag, is_feature_enabledfrom .batch_submit import read_input_bundle+from .fetch import DREAM_EPISODE_NAME_PREFIXfrom .locks import DreamLockHandle, DreamLockLostErrorfrom .schemas import (ConsolidatedFact,@@ -128,8 +131,13 @@ def _provenance(pass_id: str, phase: str) -> str:def _episode_name(pass_id: str, phase: str, counter: int) -> str:- """Stable, auditable episode name for dream-derived writes."""- return f"dream_{pass_id}_{phase}_{counter:03d}"+ """Stable, auditable episode name for dream-derived writes.++ Shares ``DREAM_EPISODE_NAME_PREFIX`` with the novelty check+ (:func:`fetch.is_dream_authored_episode`) so dream-authored episodes+ stay recognizable and never re-trigger a paid pass on themselves.+ """+ return f"{DREAM_EPISODE_NAME_PREFIX}{pass_id}_{phase}_{counter:03d}"def _edge_metadata(envelope: MemoryEnvelope) -> dict:@@ -430,6 +438,23 @@ async def _create_dream_session(user_id: str, pass_id: str) -> str:team_id=team_id,metadata=ChatSessionMetadata(kind="dream", dream_pass_id=pass_id),)+ # ``create_chat_session`` takes no title; set it via the dedicated+ # accessor so the session doesn't render as "(untitled)" in the chat+ # list. Best-effort: a cosmetic title failure must never abort apply —+ # on the batch path the at-most-once apply gate is already claimed by+ # the time we run, so an exception here would permanently lose the+ # dream (a retry hits the "duplicate" branch and skips apply).+ title = f"Dream summary — {datetime.now(timezone.utc).strftime('%Y-%m-%d')}"+ try:+ await chat_db().update_chat_session_title(+ session_id=session_id, user_id=user_id, title=title+ )+ except Exception:+ logger.warning(+ f"Failed to title dream session {session_id[:12]} "+ f"for user {user_id[:12]}",+ exc_info=True,+ )return session_id@@ -528,6 +553,15 @@ async def apply_operations(batch skip, ``drained`` is a fully-landed pass (see``_drain_ingestion``). Read it back via ``drain_status_from_stats``.+ An empty pass — no writes, proposals, demotions, or entity+ invalidations — returns zero counts and an empty snapshot WITHOUT+ creating the dream session or writing any message; the+ ``session_id`` key is absent so ``apply_stats.get("session_id")``+ reads as ``None`` for both the orchestrator and the batch callback.+ A pass WITH operations but an empty ``summary_for_user`` still+ creates the session and writes the fallback narrative (the ops+ landed; only the narrative is missing).+``known_fact_uuids`` is the set of edge uuids the dream passactually fetched (``DreamInput.known_fact_uuids``); demotionstargeting anything outside it are dropped before any Cypher runs@@ -558,6 +592,29 @@ async def apply_operations(try direct Prisma, hit "All connection attempts failed" whilethe engine is still booting)."""+ if not (ops.writes or ops.proposals or ops.demotions or ops.entity_invalidations):+ # Empty pass — nothing landed in memory, so don't manufacture a+ # user-visible artifact for it. Creating the session shell ++ # placeholder narrative here is what produced one untitled empty+ # chat per user per night for users with old facts but no new+ # activity. ``session_id`` is deliberately absent from the stats:+ # consumers read it via ``.get("session_id")`` and both the+ # orchestrator and batch_callbacks treat the missing key as None.+ logger.info(+ f"Dream pass {pass_id} for user {user_id[:12]} produced "+ f"no operations — skipping dream session creation"+ )+ return {+ "consolidated_count": 0,+ "proposal_count": 0,+ "demotion_count": 0,+ "demotion_failed_count": 0,+ "entity_invalidation_count": 0,+ # Vacuously drained — the pass enqueued nothing.+ "ingestion_drain_status": IngestionDrainStatus.drained,+ "snapshot": DreamOperationsSnapshot(),+ }+group_id = derive_group_id(user_id)# Phase A — create the session shell up front so the MemoryEnvelopediff --git a/autogpt_platform/backend/backend/copilot/dream/apply_test.py b/autogpt_platform/backend/backend/copilot/dream/apply_test.pyindex 33b24874f1..d4d58324da 100644--- a/autogpt_platform/backend/backend/copilot/dream/apply_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/apply_test.py@@ -76,6 +76,9 @@ def _stub_boundaries(mocker):"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)+ )mocker.patch("backend.copilot.db.add_chat_message", AsyncMock(return_value=None))# _create_dream_session's tenant lookup. Unmocked it runs REAL Prisma# queries on this test's function-scoped event loop whenever an earlier@@ -348,22 +351,96 @@ async def test_entity_invalidation_skipped_when_flag_off(mocker):@pytest.mark.asyncio-async def test_no_op_dream_still_writes_summary_session(mocker):- """Empty DreamOperations still creates the dream-kind ChatSession."""+async def test_empty_pass_creates_no_session_and_no_message():+ """A pass with no writes, proposals, demotions, or entity+ invalidations must not manufacture a user-visible chat — no+ dream-kind ChatSession, no placeholder message, zero stats, and no+ session_id. A non-empty summary alone is NOT an operation.+ Regression: nightly dreams for users with old facts but no new+ activity created one untitled empty chat per user per night."""from backend.copilot import db as copilot_db+ from .schemas import DreamOperationsSnapshot+ops = DreamOperations(summary_for_user="Nothing new today.")stats = await apply_mod.apply_operations(user_id="u-a", pass_id="p-5", ops=ops)+ copilot_db.create_chat_session.assert_not_awaited()+ copilot_db.add_chat_message.assert_not_awaited()+ apply_mod.enqueue_episode.assert_not_awaited()+ assert stats.get("session_id") is Noneassert stats["consolidated_count"] == 0assert stats["proposal_count"] == 0assert stats["demotion_count"] == 0- # ChatSession + ChatMessage were both created+ assert stats["demotion_failed_count"] == 0+ assert stats["entity_invalidation_count"] == 0+ assert stats["snapshot"] == DreamOperationsSnapshot()+++async def test_ops_with_empty_summary_still_create_session_with_placeholder():+ """Operations landed but the model returned no narrative — the+ session must still be created (the memory ops need their provenance+ + user-visible record) with the fallback placeholder message."""+ from backend.copilot import db as copilot_db++ ops = DreamOperations(+ writes=[ConsolidatedFact(content="A likes B", confidence=0.8)],+ summary_for_user="",+ )+ stats = await apply_mod.apply_operations(user_id="u-ph", pass_id="p-ph", ops=ops)+copilot_db.create_chat_session.assert_awaited_once()copilot_db.add_chat_message.assert_awaited_once()msg_kwargs = copilot_db.add_chat_message.await_args.kwargsassert msg_kwargs["role"] == "assistant"- assert msg_kwargs["content"] == "Nothing new today."+ assert msg_kwargs["content"] == "Dream pass completed with no narrative output."+ assert isinstance(stats["session_id"], str) and stats["session_id"]+++async def test_dream_session_titled_with_utc_date():+ """The dream-kind session gets a 'Dream summary — YYYY-MM-DD' title+ (UTC date) via update_chat_session_title, scoped to the owning user,+ so it doesn't render as '(untitled)' in the chat list."""+ from backend.copilot import db as copilot_db++ ops = DreamOperations(+ writes=[ConsolidatedFact(content="A likes B", confidence=0.8)],+ summary_for_user="ok",+ )+ stats = await apply_mod.apply_operations(+ user_id="u-title", pass_id="p-title", ops=ops+ )++ copilot_db.update_chat_session_title.assert_awaited_once()+ title_kwargs = copilot_db.update_chat_session_title.await_args.kwargs+ today = datetime.now(timezone.utc).strftime("%Y-%m-%d")+ assert title_kwargs["title"] == f"Dream summary — {today}"+ assert title_kwargs["session_id"] == stats["session_id"]+ assert title_kwargs["user_id"] == "u-title"+++async def test_title_failure_does_not_abort_apply(mocker):+ """The title write is cosmetic and best-effort — on the batch path the+ at-most-once apply gate is already claimed when apply runs, so an+ exception here would permanently lose the dream. The ops and the+ narrative must still land."""+ from backend.copilot import db as copilot_db++ mocker.patch(+ "backend.copilot.db.update_chat_session_title",+ AsyncMock(side_effect=ConnectionError("db blip")),+ )+ ops = DreamOperations(+ writes=[ConsolidatedFact(content="A likes B", confidence=0.8)],+ summary_for_user="ok",+ )+ stats = await apply_mod.apply_operations(user_id="u-tf", pass_id="p-tf", ops=ops)++ assert stats["consolidated_count"] == 1+ copilot_db.add_chat_message.assert_awaited_once()@pytest.mark.asynciodiff --git a/autogpt_platform/backend/backend/copilot/dream/fetch.py b/autogpt_platform/backend/backend/copilot/dream/fetch.pyindex 098be3e52d..5d8b317760 100644--- a/autogpt_platform/backend/backend/copilot/dream/fetch.py+++ b/autogpt_platform/backend/backend/copilot/dream/fetch.py@@ -76,6 +76,66 @@ class DreamInput(BaseModel):known_episode_uuids: set[str] = Field(default_factory=set)+# Prefix stamped on every dream-authored episode name (see+# ``apply._episode_name``: ``dream_{pass_id}_{phase}_{counter}``). Shared+# so the novelty check and the writer can't drift.+DREAM_EPISODE_NAME_PREFIX = "dream_"+++def parse_episode_timestamp(episode: EpisodeRow) -> datetime | None:+ """Latest tz-aware timestamp for an episode row, for novelty checks.++ Returns the *later* of ``valid_at`` (the ``reference_time`` set at+ enqueue, which the episode fetch Cypher orders by) and ``created_at``+ (graphiti's graph-node creation time). The two diverge when async+ ingestion lags the gather query: an episode enqueued with a past+ ``reference_time`` but materialized as a node only *after* a pass+ stamped its marker has ``valid_at`` < marker yet is genuinely new.+ Taking the max keeps it counted as new until a later pass consolidates+ it, instead of silently dropping it once the marker moves past its+ ``valid_at``. ``None`` means neither field parsed — callers must treat+ that as "age unknown" and fail open.+ """+ parsed = [+ ts+ for raw in (episode.valid_at, episode.created_at)+ if raw+ if (ts := _parse_iso_timestamp(raw)) is not None+ ]+ return max(parsed) if parsed else None+++def is_dream_authored_episode(episode: EpisodeRow) -> bool:+ """True for episodes written by the dream pipeline itself.++ A productive pass enqueues its consolidations/proposals with+ ``valid_at = now()`` — after the ``window_end`` the marker is stamped+ with — so counting them as new activity would make every subsequent+ nightly run a paid no-op that only re-reads its own output. They are+ identifiable by the stable ``dream_`` name prefix+ (:func:`apply._episode_name`); the ``dream-pass`` ``source_description``+ is a secondary signal for robustness.+ """+ name = episode.name+ if name is not None and name.startswith(DREAM_EPISODE_NAME_PREFIX):+ return True+ description = episode.source_description+ return description is not None and description.startswith("dream-pass")+++def _parse_iso_timestamp(raw: str) -> datetime | None:+ """ISO 8601 parse tolerating Cypher's ``toString(datetime)`` output+ (trailing ``Z``); naive values are assumed UTC so the result always+ compares safely against tz-aware datetimes."""+ try:+ if raw.endswith("Z"):+ raw = raw[:-1] + "+00:00"+ parsed = datetime.fromisoformat(raw)+ except ValueError:+ return None+ return parsed if parsed.tzinfo else parsed.replace(tzinfo=timezone.utc)++def _open_driver(group_id: str) -> AutoGPTFalkorDriver:return AutoGPTFalkorDriver(host=graphiti_config.falkordb_host,diff --git a/autogpt_platform/backend/backend/copilot/dream/orchestrator.py b/autogpt_platform/backend/backend/copilot/dream/orchestrator.pyindex 385e79f2ea..78299a9b8c 100644--- a/autogpt_platform/backend/backend/copilot/dream/orchestrator.py+++ b/autogpt_platform/backend/backend/copilot/dream/orchestrator.py@@ -29,7 +29,13 @@ from backend.util.feature_flag import Flag, is_feature_enabledfrom .apply import apply_operations, drain_status_from_statsfrom .billing import check_dream_budget, record_phase_cost-from .fetch import DreamInput, gather_dream_input+from .fetch import (+ DreamInput,+ EpisodeRow,+ gather_dream_input,+ is_dream_authored_episode,+ parse_episode_timestamp,+)from .llm import (CompletionUsage,DreamLLMError,@@ -139,6 +145,16 @@ DREAM_NON_LLM_HEADROOM_SECONDS = 120# work that doesn't belong in this sync clamp.MAX_ENTITY_INVALIDATIONS_PER_PASS = 2+# Per-user marker stamped after a successful (non-skipped) sync apply so+# the next nightly pass can skip all three LLM phases when no new episode+# landed since. Single key — prod Redis runs in cluster mode (see+# locks.py), so no multi-key primitives. The 35-day TTL comfortably+# outlives the 14-day episode window; an expired or missing marker just+# means one extra full pass (fail-open). The batch path does NOT stamp+# this marker yet — batch users simply never benefit from the skip.+LAST_COMPLETED_KEY_PREFIX = "dream:last_completed:"+LAST_COMPLETED_TTL_SECONDS = 35 * 24 * 60 * 60+def _resolve_lock_ttl(transport_is_local: bool) -> int:return LOCAL_LOCK_TTL_SECONDS if transport_is_local else DEFAULT_LOCK_TTL_SECONDS@@ -624,6 +640,97 @@ def _aggregate_usage()+def _last_completed_key(user_id: str) -> str:+ return f"{LAST_COMPLETED_KEY_PREFIX}{user_id}"+++async def _read_last_completed_marker(user_id: str) -> datetime | None:+ """When the user's last dream pass completed, or ``None``.++ Best-effort: a Redis error or an unparseable value fails open+ (``None`` ⇒ the pass runs) — the marker only exists to save LLM+ spend, so it must never block a dream.+ """+ # Lazy import matching locks.py — keeps the module cheap to import+ # in tests that mock redis.+ from backend.data.redis_client import get_redis_async++ try:+ redis = await get_redis_async()+ raw = await redis.get(_last_completed_key(user_id))+ except Exception:+ logger.warning(+ "Failed to read dream last-completed marker for user %s — "+ "running the pass",+ user_id[:12],+ exc_info=True,+ )+ return None+ if raw is None:+ return None+ try:+ if isinstance(raw, bytes):+ raw = raw.decode("utf-8")+ marker = datetime.fromisoformat(str(raw))+ except (UnicodeDecodeError, ValueError):+ logger.warning(+ "Unparseable dream last-completed marker for user %s — running the pass",+ user_id[:12],+ )+ return 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:+ """Record the upper bound of the episode window the pass consolidated.++ ``as_of`` must be the gather snapshot time (``DreamInput.window_end``),+ NOT apply-completion time: the three LLM phases + apply take minutes,+ and an episode enqueued in that window was absent from the bundle —+ stamping "now" would mark it as already consolidated and skip it until+ the next genuinely-new episode (or the marker TTL).++ Best-effort: a failed stamp only costs one extra full pass on the+ next nightly tick.+ """+ from backend.data.redis_client import get_redis_async++ try:+ redis = await get_redis_async()+ await redis.set(+ _last_completed_key(user_id),+ as_of.isoformat(),+ ex=LAST_COMPLETED_TTL_SECONDS,+ )+ except Exception:+ logger.warning(+ "Failed to stamp dream last-completed marker for user %s",+ user_id[:12],+ exc_info=True,+ )+++def _has_new_episodes_since(episodes: list[EpisodeRow], marker: datetime) -> bool:+ """True when any *user* episode is newer than the last-completed marker.++ Dream-authored episodes are excluded: a productive pass enqueues its+ own writes with ``valid_at = now()`` (after the stamped ``window_end``),+ so counting them would make every subsequent nightly run a paid no-op+ that only re-reads its own output.++ An episode with no parseable timestamp counts as new — we can't+ prove it's old, and skipping a pass we owed the user is worse than+ running one we didn't.+ """+ for episode in episodes:+ if is_dream_authored_episode(episode):+ continue+ episode_at = parse_episode_timestamp(episode)+ if episode_at is None or episode_at > marker:+ return True+ return False++async def _execute_dream_pass_async(user_id: str,*,@@ -702,6 +809,44 @@ async def _execute_dream_pass_async(skip_reason="no_input",)+ # No NEW activity since the last completed pass — every episode+ # in the bundle predates the marker, so re-running all three+ # LLM phases would only re-chew already-consolidated material+ # (and, before the empty-pass guard in apply.py, manufacture an+ # empty dream chat). Marker read is best-effort: missing,+ # unparseable, or Redis-down all mean "run the pass".+ #+ # Manual admin triggers (the only callers that set status_id)+ # bypass the marker: "dream now" is the memory-debugging tool+ # for re-running a pass after prompt/flag/model changes, and a+ # 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)+ if status_id is None+ else None+ )+ if last_completed is not None and not _has_new_episodes_since(+ input_bundle.episodes, last_completed+ ):+ logger.info(+ "Dream pass %s skipped for user %s — no episodes newer "+ "than last completed pass at %s",+ pass_id,+ user_id[:12],+ last_completed.isoformat(),+ )+ return DreamPassResult(+ user_id=user_id,+ pass_id=pass_id,+ started_at=started_at,+ completed_at=datetime.now(timezone.utc),+ elapsed_seconds=(asyncio.get_event_loop().time() - monotonic_start),+ execution_path=execution_path,+ skipped=True,+ skip_reason="no_new_activity",+ )+# ---- Anthropic batch path -----------------------------------## When routing picks ``anthropic_batch`` we persist the@@ -820,9 +965,17 @@ async def _execute_dream_pass_async(known_fact_uuids=input_bundle.known_fact_uuids,lock_handle=dream_lock_handle,)+ # Apply succeeded (even as a no-op) — stamp the marker so the+ # next nightly pass can skip when nothing new has landed.+ # Stamped with the gather-window end so episodes that arrived+ # 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)completed_at = datetime.now(timezone.utc)snapshot = apply_stats.get("snapshot")+ raw_session_id = apply_stats.get("session_id")def _as_int(key: str) -> int:v = apply_stats.get(key, 0)@@ -845,7 +998,11 @@ async def _execute_dream_pass_async(entity_invalidation_count=_as_int("entity_invalidation_count"),summary_for_user=ops.summary_for_user,ingestion_drain_status=ingestion_drain_status,- dream_session_id=str(apply_stats.get("session_id") or ""),+ # ``None`` (key absent) on an empty pass — apply skipped the+ # dream session entirely, so there is no id to surface.+ dream_session_id=(+ raw_session_id if isinstance(raw_session_id, str) else None+ ),operations=(snapshot if isinstance(snapshot, DreamOperationsSnapshot) else None),diff --git a/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py b/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.pyindex 778952fed3..a51806a0ce 100644--- a/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py@@ -135,6 +135,21 @@ def _stub_batch_flag(mocker):)[email protected](autouse=True)+def _stub_marker_redis(mocker):+ """No last-completed marker by default (get → None) so every existing+ test runs the full pass; the stamp write is a silent success. The+ no-new-activity tests reconfigure ``get`` on the returned mock."""+ redis = AsyncMock()+ redis.get = AsyncMock(return_value=None)+ redis.set = AsyncMock(return_value=True)+ mocker.patch(+ "backend.data.redis_client.get_redis_async",+ AsyncMock(return_value=redis),+ )+ return redis++@pytest.mark.asyncioasync def test_empty_input_returns_skipped(mocker):"""No episodes AND no facts ⇒ skipped, no LLM calls."""@@ -893,6 +908,501 @@ async def test_partial_failure_still_charges_completed_phases(mocker):apply_mock.assert_not_awaited()+# ---------------------------------------------------------------------------+# No-new-activity skip — the per-user ``dream:last_completed:{user_id}``+# marker short-circuits the pass before phase 1 when nothing new landed.+# ---------------------------------------------------------------------------+++def _input_with_episodes(episodes: list[EpisodeRow]) -> DreamInput:+ """Bundle with one old active fact + the given episodes — the 'old+ facts, maybe-stale episodes' nightly shape."""+ return DreamInput(+ user_id="u",+ group_id="g",+ window_start=datetime(2026, 5, 26, tzinfo=timezone.utc),+ window_end=datetime(2026, 6, 9, tzinfo=timezone.utc),+ episodes=episodes,+ facts=[+ FactRow(+ uuid="f0",+ source="A",+ target="B",+ name="likes",+ fact="A likes B",+ scope="real:global",+ confidence=0.7,+ status="active",+ created_at="2026-01-01T00:00:00Z",+ )+ ],+ recent_sessions=[],+ known_fact_uuids={"f0"},+ known_episode_uuids={e.uuid for e in episodes},+ )+++def _episode_at(+ valid_at: str | None,+ *,+ uuid: str = "e0",+ name: str | None = None,+ source_description: str | None = None,+ created_at: str | None = None,+) -> EpisodeRow:+ return EpisodeRow(+ uuid=uuid,+ name=name,+ content="hello",+ source_description=source_description,+ valid_at=valid_at,+ created_at=created_at,+ )+++def _input_with_episode_times(*valid_ats: str | None) -> DreamInput:+ """Bundle with plain user episodes at the given ``valid_at`` timestamps."""+ return _input_with_episodes(+ [_episode_at(valid_at, uuid=f"e{i}") for i, valid_at in enumerate(valid_ats)]+ )+++def _stub_three_phases_and_apply(mocker) -> AsyncMock:+ """Minimal happy-path pipeline: empty phase outputs + a stubbed apply.+ Returns the apply mock so callers can assert the pass actually ran."""+ mocker.patch.object(+ orchestrator_mod,+ "structured_completion",+ AsyncMock(+ side_effect=[+ _wrap(ConsolidationOutput(facts=[])),+ _wrap(RecombinationOutput(proposals=[])),+ _wrap(DreamOperations(summary_for_user="ok")),+ ]+ ),+ )+ return mocker.patch.object(+ orchestrator_mod,+ "apply_operations",+ AsyncMock(+ return_value={+ "session_id": "s",+ "consolidated_count": 0,+ "proposal_count": 0,+ "demotion_count": 0,+ "demotion_failed_count": 0,+ "entity_invalidation_count": 0,+ }+ ),+ )+++async def test_marker_newer_than_all_episodes_skips_with_no_new_activity(+ mocker, _stub_marker_redis+):+ """Every episode predates the last completed pass ⇒ skip before+ phase 1: no LLM calls, no apply, no fresh marker stamp."""+ _stub_marker_redis.get.return_value = "2026-06-08T00:00:00+00:00"+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(+ return_value=_input_with_episode_times(+ "2026-06-01T00:00:00Z", "2026-06-07T23:59:59Z"+ )+ ),+ )+ structured = mocker.patch.object(+ orchestrator_mod, "structured_completion", AsyncMock()+ )+ apply_mock = mocker.patch.object(orchestrator_mod, "apply_operations", AsyncMock())++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.skipped is True+ assert result.skip_reason == "no_new_activity"+ structured.assert_not_called()+ apply_mock.assert_not_awaited()+ _stub_marker_redis.set.assert_not_awaited()+++async def test_marker_with_old_facts_and_no_episodes_skips(mocker, _stub_marker_redis):+ """The literal nightly bug shape: old active facts, zero episodes in+ the window, marker present ⇒ no_new_activity (NOT no_input — facts+ exist, so the old code would have run all three phases)."""+ _stub_marker_redis.get.return_value = "2026-06-08T00:00:00+00:00"+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=_input_with_episode_times()),+ )+ structured = mocker.patch.object(+ orchestrator_mod, "structured_completion", AsyncMock()+ )++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.skipped is True+ assert result.skip_reason == "no_new_activity"+ structured.assert_not_called()+++async def test_no_marker_runs_full_pass(mocker, _stub_marker_redis):+ """First-ever pass (or expired marker): get → None means there's no+ baseline to compare against, so the pass runs."""+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=_input_with_episode_times("2026-06-01T00:00:00Z")),+ )+ apply_mock = _stub_three_phases_and_apply(mocker)++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.skipped is False+ assert result.error is None+ apply_mock.assert_awaited_once()+++async def test_redis_error_on_marker_read_fails_open_and_runs(+ mocker, _stub_marker_redis+):+ """A Redis blip on the marker read must never block a dream — the+ marker only exists to save LLM spend."""+ _stub_marker_redis.get.side_effect = ConnectionError("redis down")+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=_input_with_episode_times("2026-06-01T00:00:00Z")),+ )+ apply_mock = _stub_three_phases_and_apply(mocker)++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.skipped is False+ assert result.error is None+ apply_mock.assert_awaited_once()+++async def test_corrupt_marker_bytes_fail_open_and_run(mocker, _stub_marker_redis):+ """A marker holding invalid UTF-8 bytes must fail open like any other+ unparseable value — never escalate the cost optimization into a pass+ failure."""+ _stub_marker_redis.get.return_value = b"\xff\xfe corrupt"+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=_input_with_episode_times("2026-06-01T00:00:00Z")),+ )+ apply_mock = _stub_three_phases_and_apply(mocker)++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.skipped is False+ assert result.error is None+ apply_mock.assert_awaited_once()+++async def test_episode_newer_than_marker_runs_pass(mocker, _stub_marker_redis):+ """One episode landed after the last completed pass ⇒ there's new+ material to consolidate, so the pass runs."""+ _stub_marker_redis.get.return_value = "2026-06-05T00:00:00+00:00"+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(+ return_value=_input_with_episode_times(+ "2026-06-01T00:00:00Z", "2026-06-08T12:00:00Z"+ )+ ),+ )+ apply_mock = _stub_three_phases_and_apply(mocker)++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.skipped is False+ apply_mock.assert_awaited_once()+++async def test_unparseable_episode_timestamp_counts_as_new_and_runs(+ mocker, _stub_marker_redis+):+ """An episode whose valid_at/created_at can't be parsed can't be+ proven old — fail open and run the pass."""+ _stub_marker_redis.get.return_value = "2026-06-08T00:00:00+00:00"+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=_input_with_episode_times(None)),+ )+ apply_mock = _stub_three_phases_and_apply(mocker)++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.skipped is False+ apply_mock.assert_awaited_once()+++async def test_dream_authored_episode_newer_than_marker_does_not_count_as_new(+ mocker, _stub_marker_redis+):+ """A productive pass enqueues its own ``dream_``-prefixed episodes with+ ``valid_at = now()`` (after the stamped ``window_end``). They must NOT+ count as new activity, or every productive night would trigger a paid+ no-op pass the next night that only re-reads its own output."""+ _stub_marker_redis.get.return_value = "2026-06-08T00:00:00+00:00"+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(+ return_value=_input_with_episodes(+ [+ _episode_at("2026-06-01T00:00:00Z", uuid="e0"),+ _episode_at(+ "2026-06-30T00:00:00Z",+ uuid="e1",+ name="dream_p-xyz_consolidate_000",+ source_description="dream-pass consolidation; src_episodes=e0",+ ),+ ]+ )+ ),+ )+ structured = mocker.patch.object(+ orchestrator_mod, "structured_completion", AsyncMock()+ )+ apply_mock = mocker.patch.object(orchestrator_mod, "apply_operations", AsyncMock())++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.skipped is True+ assert result.skip_reason == "no_new_activity"+ structured.assert_not_called()+ apply_mock.assert_not_awaited()+++async def test_normal_episode_newer_than_marker_runs_even_beside_dream_episode(+ mocker, _stub_marker_redis+):+ """Excluding dream-authored episodes must not suppress genuine user+ activity: a normal episode newer than the marker still triggers the pass+ even when a dream-authored episode sits alongside it."""+ _stub_marker_redis.get.return_value = "2026-06-08T00:00:00+00:00"+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(+ return_value=_input_with_episodes(+ [+ _episode_at(+ "2026-06-30T00:00:00Z",+ uuid="e0",+ name="dream_p-xyz_recombine_000",+ source_description="dream-pass proposal",+ ),+ _episode_at("2026-06-09T12:00:00Z", uuid="e1"),+ ]+ )+ ),+ )+ apply_mock = _stub_three_phases_and_apply(mocker)++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.skipped is False+ apply_mock.assert_awaited_once()+++async def test_episode_created_after_marker_counts_as_new_despite_old_valid_at(+ mocker, _stub_marker_redis+):+ """An episode whose graph node is materialized by async ingestion after+ the gather query has ``valid_at`` (reference_time) < marker but+ ``created_at`` > marker. Novelty uses the later of the two, so it counts+ as new instead of being silently dropped."""+ _stub_marker_redis.get.return_value = "2026-06-08T00:00:00+00:00"+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(+ return_value=_input_with_episodes(+ [+ _episode_at(+ "2026-06-01T00:00:00Z",+ uuid="e0",+ created_at="2026-06-09T00:00:00Z",+ )+ ]+ )+ ),+ )+ apply_mock = _stub_three_phases_and_apply(mocker)++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.skipped is False+ apply_mock.assert_awaited_once()+++async def test_episode_old_in_both_valid_at_and_created_at_skips(+ mocker, _stub_marker_redis+):+ """Taking the max of valid_at/created_at must not spuriously run: when+ both timestamps predate the marker, the pass still skips."""+ _stub_marker_redis.get.return_value = "2026-06-08T00:00:00+00:00"+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(+ return_value=_input_with_episodes(+ [+ _episode_at(+ "2026-06-01T00:00:00Z",+ uuid="e0",+ created_at="2026-06-05T00:00:00Z",+ )+ ]+ )+ ),+ )+ structured = mocker.patch.object(+ orchestrator_mod, "structured_completion", AsyncMock()+ )+ apply_mock = mocker.patch.object(orchestrator_mod, "apply_operations", AsyncMock())++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.skipped is True+ assert result.skip_reason == "no_new_activity"+ structured.assert_not_called()+ apply_mock.assert_not_awaited()+++async def test_successful_pass_stamps_last_completed_marker(mocker, _stub_marker_redis):+ """A non-skipped sync apply stamps ``dream:last_completed:{user_id}``+ with an ISO timestamp and the ~35-day TTL so the next nightly pass+ can skip when nothing new landed."""+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=_input_with_episode_times("2026-06-08T12:00:00Z")),+ )+ _stub_three_phases_and_apply(mocker)++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.skipped is False+ assert result.error is None+ _stub_marker_redis.set.assert_awaited_once()+ set_args = _stub_marker_redis.set.await_args+ assert set_args.args[0] == "dream:last_completed:u"+ # Stamped with the gather-window end (NOT apply-completion time) so+ # episodes that arrive mid-pass still count as new next time.+ stamped = datetime.fromisoformat(set_args.args[1])+ assert stamped == datetime(2026, 6, 9, tzinfo=timezone.utc)+ assert set_args.kwargs == {"ex": orchestrator_mod.LAST_COMPLETED_TTL_SECONDS}+++async def test_admin_trigger_bypasses_no_new_activity_marker(+ mocker, _stub_marker_redis+):+ """status_id is set only by the admin 'dream now' trigger — the+ memory-debugging path must run the full pipeline even when the marker+ is newer than every episode, otherwise a re-run after prompt/flag+ changes silently no-ops for up to the marker TTL."""+ _stub_marker_redis.get.return_value = "2026-06-08T00:00:00+00:00"+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=_input_with_episode_times("2026-06-01T00:00:00Z")),+ )+ apply_mock = _stub_three_phases_and_apply(mocker)++ result = await orchestrator_mod.execute_dream_pass("u", status_id="job-1")++ assert result.skipped is False+ assert result.error is None+ apply_mock.assert_awaited_once()+ # The bypass must not even read the marker — the skip is caller-gated.+ _stub_marker_redis.get.assert_not_awaited()+++async def test_failed_pass_does_not_stamp_marker(mocker, _stub_marker_redis):+ """An LLM failure means no apply ran — the marker must NOT advance,+ otherwise tomorrow's pass would skip material this one never chewed."""+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=_input_with_episode_times("2026-06-08T12:00:00Z")),+ )+ mocker.patch.object(+ orchestrator_mod,+ "structured_completion",+ AsyncMock(side_effect=DreamLLMError("boom")),+ )++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.error is not None+ _stub_marker_redis.set.assert_not_awaited()+++async def test_empty_apply_stats_leave_dream_session_id_none(mocker):+ """When apply skips the dream session (empty pass), its stats carry+ no session_id — the result must surface ``dream_session_id=None``,+ not an empty string."""+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=_build_input()),+ )+ mocker.patch.object(+ orchestrator_mod,+ "structured_completion",+ AsyncMock(+ side_effect=[+ _wrap(ConsolidationOutput(facts=[])),+ _wrap(RecombinationOutput(proposals=[])),+ _wrap(DreamOperations(summary_for_user="Nothing new tonight.")),+ ]+ ),+ )+ mocker.patch.object(+ orchestrator_mod,+ "apply_operations",+ AsyncMock(+ return_value={+ "consolidated_count": 0,+ "proposal_count": 0,+ "demotion_count": 0,+ "demotion_failed_count": 0,+ "entity_invalidation_count": 0,+ }+ ),+ )++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.error is None+ assert result.dream_session_id is None+ assert result.summary_for_user == "Nothing new tonight."++_ = MagicMock # keep import for editor convenience; not directly useddiff --git a/autogpt_platform/backend/backend/copilot/dream/prompts.py b/autogpt_platform/backend/backend/copilot/dream/prompts.pyindex bb922b39ff..21599c05cc 100644--- a/autogpt_platform/backend/backend/copilot/dream/prompts.py+++ b/autogpt_platform/backend/backend/copilot/dream/prompts.py@@ -201,7 +201,8 @@ SANITIZE_SYSTEM = ("code fences. Do NOT use markdown formatting anywhere. Do NOT ""explain reasoning before or after the JSON. An empty result is "'still JSON: `{"writes": [], "proposals": [], "demotions": [], '- '"entity_invalidations": [], "summary_for_user": ""}`.\n\n'+ '"entity_invalidations": [], "summary_for_user": "Nothing new to '+ 'consolidate tonight — no memory changes made."}`.\n\n'"ROLE:\n""You are the sanitizer step of a sleep-time memory pass. You receive ""the consolidated facts and the recombination proposals; you decide "@@ -242,7 +243,10 @@ SANITIZE_SYSTEM = ("is the user, their projects, the people/orgs they work with, or ""their stated preferences.\n"" * Write a short ``summary_for_user`` (1-3 sentences) describing "- "what the pass found.\n\n"+ "what the pass found.\n"+ " * ``summary_for_user`` MUST ALWAYS be non-empty — even when there "+ "are no writes, proposals, demotions, or entity invalidations, say "+ "in one sentence that nothing changed.\n\n""JSON SCHEMA (your response MUST match this DreamOperations shape):\n"'{ "writes": [ConsolidatedFact...], "proposals": [ProposedFinding...], ''"demotions": [{"edge_uuid": str, "reason": str, 'diff --git a/autogpt_platform/backend/backend/copilot/dream/prompts_test.py b/autogpt_platform/backend/backend/copilot/dream/prompts_test.pyindex 6997b5e764..dde6fc41b7 100644--- a/autogpt_platform/backend/backend/copilot/dream/prompts_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/prompts_test.py@@ -213,6 +213,23 @@ def test_sanitize_prompt_emits_no_stale_candidates_placeholder_when_clean():assert "(no stale-fact candidates flagged this pass)" in msgs[1]["content"]+def test_sanitize_empty_result_example_models_non_empty_summary():+ """The sanitizer's empty-result example must not show+ ``"summary_for_user": ""`` — the literal empty string in the example+ taught the model to emit empty summaries on no-op passes+ (contradicting the summary rule), which rendered as blank dream+ chats. The example carries a real sentence and an explicit+ always-non-empty rule backs it up."""+ from .prompts import SANITIZE_SYSTEM++ assert '"summary_for_user": ""' not in SANITIZE_SYSTEM+ assert (+ '"summary_for_user": "Nothing new to consolidate tonight — '+ 'no memory changes made."' in SANITIZE_SYSTEM+ )+ assert "``summary_for_user`` MUST ALWAYS be non-empty" in SANITIZE_SYSTEM++def test_prompts_tolerate_empty_inputs():"""A bundle with no episodes / facts / sessions still produces validprompts — the model should answer with an empty list."""