Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/copilot): start each morning briefing in a new Otto chat (#15222)

Toran Bruce Richardsc15274b55247
diff --git a/autogpt_platform/backend/backend/copilot/briefing/generate.py b/autogpt_platform/backend/backend/copilot/briefing/generate.py
index 62e68a344f..3362549300 100644
--- a/autogpt_platform/backend/backend/copilot/briefing/generate.py
+++ b/autogpt_platform/backend/backend/copilot/briefing/generate.py
@@ -342,6 +342,7 @@ async def generate_and_deliver_briefing(user_id: str) -> BriefingResult:
content=render_briefing_markdown(content),
message_id=message_id,
metadata={"kind": "morning_briefing", "briefing_id": record.id},
+ title=f"Morning briefing — {briefing_date.isoformat()}",
)
await client.mark_briefing_delivered(user_id, record.id)
emit_funnel_event(
diff --git a/autogpt_platform/backend/backend/copilot/briefing/generate_test.py b/autogpt_platform/backend/backend/copilot/briefing/generate_test.py
index f9ba3815bd..1414496b66 100644
--- a/autogpt_platform/backend/backend/copilot/briefing/generate_test.py
+++ b/autogpt_platform/backend/backend/copilot/briefing/generate_test.py
@@ -431,6 +431,7 @@ async def test_generate_delivers_and_composes_briefing(monkeypatch):
"content": render_briefing_markdown(expected_content),
"message_id": expected_message_id,
"metadata": {"kind": "morning_briefing", "briefing_id": "briefing-1"},
+ "title": "Morning briefing — 2026-08-07",
}
client.mark_briefing_delivered.assert_awaited_once_with("user-1", "briefing-1")
diff --git a/autogpt_platform/backend/backend/copilot/db.py b/autogpt_platform/backend/backend/copilot/db.py
index c9299ef8ea..0fad0ec727 100644
--- a/autogpt_platform/backend/backend/copilot/db.py
+++ b/autogpt_platform/backend/backend/copilot/db.py
@@ -12,6 +12,7 @@ from prisma.models import ChatMessage as PrismaChatMessage
from prisma.models import ChatSession as PrismaChatSession
from prisma.types import (
ChatMessageCreateInput,
+ ChatMessageCreateWithoutRelationsInput,
ChatMessageUpdateInput,
ChatMessageWhereInput,
ChatSessionCreateInput,
@@ -1612,66 +1613,45 @@ async def append_plain_session_message(
content: str,
message_id: str,
metadata: dict[str, Any] | None = None,
+ title: str | None = None,
) -> str | None:
- """Post an assistant message into the user's latest non-expert (plain
- Otto) session, creating one when none exists — this is the user's
- "primary thread", e.g. where a morning briefing lands.
+ """Deliver an assistant message in a new, non-expert Otto chat.
- Deduplicates on *message_id* (deterministic per event at the caller), so
- retries and double-fires never produce duplicate posts.
- Returns the session id the message landed in, or None when deduped.
+ Each distinct message gets its own chat. Deduplicates on *message_id*
+ so retries and double-fires create neither duplicate posts nor empty chats.
+ Returns the new session id, or None when deduped.
"""
existing = await PrismaChatMessage.prisma().find_unique(where={"id": message_id})
if existing is not None:
return None
- # Dream-pass sessions are also ``expertId IS NULL`` but are hidden from
- # every listing surface (see :data:`_EXCLUDE_DREAM_SESSIONS_SQL`), so
- # posting into one would drop the message somewhere the user can never
- # open. Same exclusion as the listing queries.
- sessions = await db.query_raw_with_schema(
- 'SELECT * FROM {schema_prefix}"ChatSession" WHERE "userId" = $1 '
- f'AND "expertId" IS NULL AND {_EXCLUDE_DREAM_SESSIONS_SQL} '
- 'ORDER BY "updatedAt" DESC LIMIT 1',
- user_id,
- model=PrismaChatSession,
+ session_id = str(uuid.uuid4())
+ data = _chat_session_create_input(
+ session_id=session_id,
+ user_id=user_id,
+ organization_id=None,
+ team_id=None,
+ expert_id=None,
+ metadata=await _default_route_metadata(user_id, origin="interactive"),
)
- if sessions:
- session_id = sessions[0].id
- else:
- # An automation writes the first message, but the thread itself is the
- # user's primary chat — the one the product opens them into. Stamping
- # it ``automation`` (the unset-metadata default) would leave a user
- # whose briefing arrived before their first chat unable to staff their
- # team from the very thread they were handed.
- created = await create_chat_session(
- session_id=str(uuid.uuid4()),
- user_id=user_id,
- metadata=await _default_route_metadata(user_id, origin="interactive"),
- )
- session_id = created.session_id
-
- async def write_with_fresh_sequence() -> None:
- await add_chat_message(
- session_id=session_id,
- role="assistant",
- sequence=await get_next_sequence(session_id),
- content=content,
- message_id=message_id,
- metadata=metadata,
- )
+ message: ChatMessageCreateWithoutRelationsInput = {
+ "id": message_id,
+ "role": "assistant",
+ "sequence": 0,
+ "content": sanitize_string(content),
+ }
+ if metadata is not None:
+ message["metadata"] = SafeJson(metadata)
+ if title is not None:
+ data["title"] = sanitize_string(title)
+ data["Messages"] = {"create": message}
- # Same Redis NX lock as append_expert_run_message: the sequence read +
- # insert must not interleave with a concurrent turn writer picking the
- # same sequence and PK-colliding on (sessionId, sequence).
- async with _get_session_lock(session_id):
- try:
- await write_with_fresh_sequence()
- except UniqueViolationError as e:
- if is_duplicate_chat_message_id_error(e):
- return None
- # Reachable only in lock-degraded mode (Redis down yields the
- # lock without acquiring); one retry with a fresh sequence is
- # enough at this write volume.
- await write_with_fresh_sequence()
+ # The nested write is atomic: a duplicate message rolls back its new chat too.
+ try:
+ await PrismaChatSession.prisma().create(data=data)
+ except UniqueViolationError:
+ # Nested writes report only "id", without naming the conflicting model.
+ if await PrismaChatMessage.prisma().find_unique(where={"id": message_id}):
+ return None
+ raise
return session_id
diff --git a/autogpt_platform/backend/backend/copilot/plain_session_post_test.py b/autogpt_platform/backend/backend/copilot/plain_session_post_test.py
index a3bf618565..8ac0d6261b 100644
--- a/autogpt_platform/backend/backend/copilot/plain_session_post_test.py
+++ b/autogpt_platform/backend/backend/copilot/plain_session_post_test.py
@@ -1,213 +1,274 @@
-"""Integration tests for append_plain_session_message."""
+"""Integration tests for briefing chat creation and delivery retries."""
-import logging
+import asyncio
+from unittest.mock import AsyncMock
from uuid import uuid4
import pytest
+import pytest_asyncio
from prisma.errors import UniqueViolationError
-from prisma.models import ChatMessage, Expert, User
+from prisma.models import ChatMessage
+from prisma.models import ChatSession as PrismaChatSession
+from prisma.models import Expert, User
from backend.copilot import db as copilot_db
from backend.copilot.model import ChatSession, ChatSessionMetadata
from backend.copilot.tools.expert_proposal import autopilot_session_guard
from backend.util.test import SpinTestServer
-logger = logging.getLogger(__name__)
-
-async def _create_user(user_id: str) -> None:
- try:
- await User.prisma().create(
- data={
- "id": user_id,
- "email": f"plain-session-{user_id}@example.com",
- "name": "Plain Session Test",
- }
- )
- except UniqueViolationError:
- pass
-
-
-async def _cleanup(user_id: str) -> None:
+@pytest_asyncio.fixture(loop_scope="session")
+async def user_id(server: SpinTestServer):
+ user_id = f"plain-session-{uuid4()}"
+ await User.prisma().create(data={"id": user_id, "email": f"{user_id}@example.com"})
try:
- # ChatSession -> User, ChatMessage -> ChatSession, and Expert.ownerUserId
- # -> User are all onDelete: Cascade, so deleting the user sweeps
- # everything created for it, including any expert + expert-scoped
- # session set up for the discrimination check below.
+ yield user_id
+ finally:
await User.prisma().delete_many(where={"id": user_id})
- except Exception as exc:
- logger.warning("cleanup for %s failed: %s", user_id, exc)
@pytest.mark.asyncio(loop_scope="session")
-async def test_append_plain_session_message_creates_session_and_dedupes(
- server: SpinTestServer,
-):
- user_id = f"plain-session-{uuid4()}"
- await _create_user(user_id)
- try:
- message_id = str(uuid4())
-
- session_id = await copilot_db.append_plain_session_message(
- user_id=user_id,
- content="## Briefing",
- message_id=message_id,
- metadata={"kind": "morning_briefing"},
+async def test_briefing_leaves_existing_conversations_unchanged(user_id: str):
+ existing_ids = await _seed_existing_conversations(user_id)
+ sessions_before = await PrismaChatSession.prisma().find_many(
+ where={"id": {"in": existing_ids}}, order={"id": "asc"}
+ )
+ messages_before = await ChatMessage.prisma().find_many(
+ where={"sessionId": {"in": existing_ids}}, order={"id": "asc"}
+ )
+ message_id = str(uuid4())
+ metadata = {"kind": "morning_briefing", "briefing_id": "briefing-1"}
+
+ session_id = await copilot_db.append_plain_session_message(
+ user_id=user_id,
+ content="## Briefing",
+ message_id=message_id,
+ metadata=metadata,
+ )
+
+ assert session_id is not None and session_id not in existing_ids
+ created = await PrismaChatSession.prisma().find_unique(
+ where={"id": session_id}, include={"Messages": True}
+ )
+ assert created is not None and created.expertId is None
+ assert created.userId == user_id
+ assert created.Messages is not None and len(created.Messages) == 1
+ message = created.Messages[0]
+ assert (message.id, message.role, message.sequence) == (message_id, "assistant", 0)
+ assert message.content == "## Briefing"
+ assert message.metadata == metadata
+ assert (
+ await PrismaChatSession.prisma().find_many(
+ where={"id": {"in": existing_ids}}, order={"id": "asc"}
)
- assert session_id is not None
-
- # Same message id -> dedup, no second message.
- assert (
- await copilot_db.append_plain_session_message(
- user_id=user_id, content="## Briefing", message_id=message_id
- )
- is None
+ == sessions_before
+ )
+ assert (
+ await ChatMessage.prisma().find_many(
+ where={"sessionId": {"in": existing_ids}}, order={"id": "asc"}
)
+ == messages_before
+ )
- # Reuses the latest plain session instead of creating another.
- second = await copilot_db.append_plain_session_message(
- user_id=user_id, content="## Briefing 2", message_id=str(uuid4())
- )
- assert second == session_id
-
- # An expert-scoped session that is the MOST RECENTLY updated session
- # for this user must still be skipped -- the {"expertId": None}
- # filter has to actively discriminate, not just "reuse whatever's
- # newest." Without the filter this would wrongly post into the
- # expert's thread.
- expert = await Expert.prisma().create(
- data={
- "ownerUserId": user_id,
- "name": "Test Expert",
- "role": "assistant",
- "identity": "test",
- }
- )
- expert_session = await copilot_db.create_chat_session(
- session_id=str(uuid4()), user_id=user_id, expert_id=expert.id
- )
- assert expert_session.session_id != session_id
- third = await copilot_db.append_plain_session_message(
- user_id=user_id, content="## Briefing 3", message_id=str(uuid4())
- )
- assert third == session_id
- assert third != expert_session.session_id
-
- expert_messages = await ChatMessage.prisma().find_many(
- where={"sessionId": expert_session.session_id}
[email protected](loop_scope="session")
+async def test_distinct_briefings_create_separate_chats_and_retries_dedupe(
+ user_id: str,
+):
+ first_message_id = str(uuid4())
+ first = await copilot_db.append_plain_session_message(
+ user_id=user_id, content="## Briefing", message_id=first_message_id
+ )
+ second = await copilot_db.append_plain_session_message(
+ user_id=user_id, content="## Next briefing", message_id=str(uuid4())
+ )
+ assert first is not None and second is not None and first != second
+
+ assert (
+ await copilot_db.append_plain_session_message(
+ user_id=user_id, content="## Briefing", message_id=first_message_id
)
- assert expert_messages == []
- finally:
- await _cleanup(user_id)
+ is None
+ )
+ sessions = await PrismaChatSession.prisma().find_many(
+ where={"userId": user_id}, include={"Messages": True}
+ )
+ assert {session.id for session in sessions} == {first, second}
+ assert all(session.Messages and len(session.Messages) == 1 for session in sessions)
@pytest.mark.asyncio(loop_scope="session")
-async def test_append_plain_session_message_creates_an_interactive_thread(
- server: SpinTestServer,
+async def test_briefing_chat_preserves_interactive_origin_and_default_route(
+ user_id: str, mocker
):
- """The auto-created primary thread must not be stamped as an automation.
-
- A new user in the experts cohort gets a briefing before their first chat
- and is dropped straight into this thread. Inheriting the unset-metadata
- ``automation`` default would have ``autopilot_session_guard`` tell them
- their own thread "was started by an automation" the moment they ask to
- hire someone.
- """
- user_id = f"plain-session-origin-{uuid4()}"
- await _create_user(user_id)
- try:
- session_id = await copilot_db.append_plain_session_message(
- user_id=user_id, content="## Briefing", message_id=str(uuid4())
- )
- assert session_id is not None
+ route = mocker.patch.object(
+ copilot_db,
+ "resolve_default_chat_route",
+ AsyncMock(return_value=("codex", "user-codex-credential")),
+ )
+ session_id = await copilot_db.append_plain_session_message(
+ user_id=user_id, content="## Briefing", message_id=str(uuid4())
+ )
+ assert session_id is not None
+ created = await copilot_db.get_chat_session_metadata(session_id)
+ assert created is not None
+ assert created.metadata.origin == "interactive"
+ assert created.metadata.kind == "normal"
+ assert created.metadata.builder_graph_id is None
+ assert created.metadata.dry_run is False
+ assert created.metadata.llm_auth_provider == "codex"
+ assert created.metadata.llm_credential_id == "user-codex-credential"
+ route.assert_awaited_once_with(user_id)
+ session = ChatSession.new(user_id, dry_run=False, session_id=session_id)
+ session.metadata = created.metadata
+ assert autopilot_session_guard(user_id, session) is None
- created = await copilot_db.get_chat_session_metadata(session_id)
- assert created is not None
- assert created.metadata.origin == "interactive"
- session = ChatSession.new(user_id, dry_run=False, session_id=session_id)
- session.metadata = created.metadata
- assert autopilot_session_guard(user_id, session) is None
- finally:
- await _cleanup(user_id)
[email protected](loop_scope="session")
+async def test_briefing_chat_sanitizes_content_and_title(user_id: str):
+ session_id = await copilot_db.append_plain_session_message(
+ user_id=user_id,
+ content="## Morning\x00 briefing\n\nToday's updates",
+ message_id=str(uuid4()),
+ title="Morning\x00 briefing — 2026-10-07",
+ )
+ assert session_id is not None
+ created = await PrismaChatSession.prisma().find_unique(
+ where={"id": session_id}, include={"Messages": True}
+ )
+ assert created is not None
+ assert created.title == "Morning briefing — 2026-10-07"
+ assert created.Messages is not None and len(created.Messages) == 1
+ assert created.Messages[0].content == "## Morning briefing\n\nToday's updates"
@pytest.mark.asyncio(loop_scope="session")
-async def test_append_plain_session_message_retries_on_sequence_collision(
- server: SpinTestServer, mocker
+async def test_concurrent_deliveries_create_one_chat_without_orphans(
+ user_id: str, mocker
):
- """Lock-degraded path: a sequence PK collision (not a duplicate message
- id) must be retried once with a fresh sequence rather than propagating."""
- user_id = f"plain-session-retry-{uuid4()}"
- await _create_user(user_id)
- try:
- real_add_chat_message = copilot_db.add_chat_message
- calls = {"n": 0}
-
- async def collide_once(*args, **kwargs):
- calls["n"] += 1
- if calls["n"] == 1:
- # A (sessionId, sequence) collision, NOT a duplicate
- # ChatMessage id — the branch under test.
- raise UniqueViolationError(
- {
- "user_facing_error": {
- "message": "Unique constraint failed on the "
- "fields: (`sessionId`,`sequence`)"
- }
- }
- )
- return await real_add_chat_message(*args, **kwargs)
-
- mocker.patch.object(copilot_db, "add_chat_message", side_effect=collide_once)
-
- message_id = str(uuid4())
- session_id = await copilot_db.append_plain_session_message(
- user_id=user_id, content="## Briefing", message_id=message_id
- )
+ existing = await copilot_db.create_chat_session(
+ session_id=str(uuid4()), user_id=user_id
+ )
+ message_actions = ChatMessage.prisma()
+ real_find_unique = message_actions.find_unique
+ both_prechecks_arrived = asyncio.Event()
+ arrivals = 0
+
+ async def race_precheck(**kwargs):
+ nonlocal arrivals
+ arrivals += 1
+ if arrivals <= 2:
+ if arrivals == 2:
+ both_prechecks_arrived.set()
+ await asyncio.wait_for(both_prechecks_arrived.wait(), timeout=10)
+ return None
+ return await real_find_unique(**kwargs)
+
+ mocker.patch.object(
+ type(message_actions), "find_unique", AsyncMock(side_effect=race_precheck)
+ )
+ message_id = str(uuid4())
+ results = await asyncio.gather(
+ *[
+ copilot_db.append_plain_session_message(
+ user_id=user_id, content="## Briefing", message_id=message_id
+ )
+ for _ in range(2)
+ ]
+ )
+ assert results.count(None) == 1
+ delivered = next(result for result in results if result is not None)
+ assert delivered != existing.session_id
+ sessions = await PrismaChatSession.prisma().find_many(
+ where={"userId": user_id}, include={"Messages": True}
+ )
+ assert {session.id for session in sessions} == {existing.session_id, delivered}
+ assert sum(len(session.Messages or []) for session in sessions) == 1
+ original = next(
+ session for session in sessions if session.id == existing.session_id
+ )
+ assert original.Messages == []
- assert calls["n"] == 2 # first write collided, retry succeeded
- assert session_id is not None
- stored = await ChatMessage.prisma().find_unique(where={"id": message_id})
- assert stored is not None
- assert stored.sessionId == session_id
- finally:
- await _cleanup(user_id)
+
[email protected](loop_scope="session")
+async def test_chat_creation_failure_propagates(user_id: str, mocker):
+ session_actions = PrismaChatSession.prisma()
+ mocker.patch.object(
+ type(session_actions),
+ "create",
+ AsyncMock(side_effect=RuntimeError("chat creation failed")),
+ )
+
+ with pytest.raises(RuntimeError, match="chat creation failed"):
+ await copilot_db.append_plain_session_message(
+ user_id=user_id, content="## Briefing", message_id=str(uuid4())
+ )
@pytest.mark.asyncio(loop_scope="session")
-async def test_append_plain_session_message_skips_dream_sessions(
- server: SpinTestServer,
+async def test_unique_violation_for_a_new_message_propagates_and_writes_nothing(
+ user_id: str, mocker
):
- """Dream sessions are also ``expertId IS NULL`` but are hidden from every
- listing surface, so a briefing posted into one is invisible forever — and
- still gets stamped delivered."""
- user_id = f"plain-session-dream-{uuid4()}"
- await _create_user(user_id)
- try:
- plain = await copilot_db.append_plain_session_message(
- user_id=user_id, content="## Briefing", message_id=str(uuid4())
+ existing = await copilot_db.create_chat_session(
+ session_id=str(uuid4()), user_id=user_id
+ )
+ session_actions = PrismaChatSession.prisma()
+ real_create = session_actions.create
+
+ async def create_with_colliding_session_id(*, data, **kwargs):
+ return await real_create(data={**data, "id": existing.session_id}, **kwargs)
+
+ mocker.patch.object(
+ type(session_actions),
+ "create",
+ AsyncMock(side_effect=create_with_colliding_session_id),
+ )
+ message_id = str(uuid4())
+
+ with pytest.raises(UniqueViolationError):
+ await copilot_db.append_plain_session_message(
+ user_id=user_id, content="## Briefing", message_id=message_id
)
- assert plain is not None
-
- # Created after the plain session, so it is the most recently updated
- # expertId-IS-NULL session for this user.
- dream_session = await copilot_db.create_chat_session(
- session_id=str(uuid4()),
- user_id=user_id,
- metadata=ChatSessionMetadata(kind="dream"),
+ assert await ChatMessage.prisma().find_unique(where={"id": message_id}) is None
+ sessions = await PrismaChatSession.prisma().find_many(
+ where={"userId": user_id}, include={"Messages": True}
+ )
+ assert [(session.id, session.Messages) for session in sessions] == [
+ (existing.session_id, [])
+ ]
+
+
+async def _seed_existing_conversations(user_id: str) -> list[str]:
+ expert = await Expert.prisma().create(
+ data={
+ "ownerUserId": user_id,
+ "name": "Test Expert",
+ "role": "assistant",
+ "identity": "test",
+ }
+ )
+ sessions = [
+ await copilot_db.create_chat_session(
+ session_id=str(uuid4()), user_id=user_id, metadata=metadata
)
- assert dream_session.session_id != plain
-
- landed = await copilot_db.append_plain_session_message(
- user_id=user_id, content="## Briefing 2", message_id=str(uuid4())
+ for metadata in (
+ ChatSessionMetadata(origin="interactive"),
+ ChatSessionMetadata(
+ origin="interactive", builder_graph_id="existing-agent"
+ ),
+ ChatSessionMetadata(kind="dream"),
)
-
- assert landed == plain
- dream_messages = await ChatMessage.prisma().find_many(
- where={"sessionId": dream_session.session_id}
+ ]
+ sessions.append(
+ await copilot_db.create_chat_session(
+ session_id=str(uuid4()), user_id=user_id, expert_id=expert.id
)
- assert dream_messages == []
- finally:
- await _cleanup(user_id)
+ )
+ for session in sessions:
+ await copilot_db.add_chat_message(
+ session_id=session.session_id,
+ role="assistant",
+ sequence=0,
+ content="Existing conversation",
+ )
+ return [session.session_id for session in sessions]