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): back-fill in-memory sequences from actual batch start (#13057)

Zamil Majdycea8fdfda9f8
diff --git a/autogpt_platform/backend/backend/copilot/db.py b/autogpt_platform/backend/backend/copilot/db.py
index 582c7e1431..967cca982c 100644
--- a/autogpt_platform/backend/backend/copilot/db.py
+++ b/autogpt_platform/backend/backend/copilot/db.py
@@ -405,12 +405,15 @@ async def add_chat_messages_batch(
upserts and DB queries in the common case (no collision).
Returns:
- Next sequence number for the next message to be inserted. This equals
- start_sequence + len(messages) and allows callers to update their
- counters even when collision detection adjusts start_sequence.
+ The actual ``start_sequence`` used for the (successful) insert.
+ Equals the input on the no-collision common path; differs after a
+ retry when ``get_next_sequence`` returned a higher offset. Callers
+ must use this value to back-fill in-memory ``ChatMessage.sequence``
+ — using the original ``start_sequence`` after a retry would point
+ the in-memory rows at sequences a peer process already wrote.
"""
if not messages:
- # No messages to add - return current count
+ # No messages to add - the caller's offset is unchanged.
return start_sequence
max_retries = 5
@@ -471,8 +474,9 @@ async def add_chat_messages_batch(
),
)
- # Return next sequence number for counter sync
- return start_sequence + len(messages)
+ # Return the offset actually used so callers can back-fill
+ # in-memory sequences (matters after a retry).
+ return start_sequence
except UniqueViolationError as e:
# Two distinct unique constraints on ``ChatMessage`` can fire here:
diff --git a/autogpt_platform/backend/backend/copilot/model.py b/autogpt_platform/backend/backend/copilot/model.py
index 9cba86b03c..f02a157ce8 100644
--- a/autogpt_platform/backend/backend/copilot/model.py
+++ b/autogpt_platform/backend/backend/copilot/model.py
@@ -792,17 +792,19 @@ async def _save_session_to_db(
f"roles={[m['role'] for m in messages_data]}, "
f"start_sequence={existing_message_count}"
)
- await db.add_chat_messages_batch(
+ # Use the offset actually used for the insert when back-filling
+ # sequences — the batch helper retries from ``get_next_sequence`` on a
+ # unique-constraint collision, so the original ``existing_message_count``
+ # may be stale by the time the rows actually land. Back-filling with
+ # the stale value would desync in-memory ``ChatMessage.sequence`` from
+ # the DB, breaking later ``update_message_content_by_sequence`` calls.
+ actual_start = await db.add_chat_messages_batch(
session_id=session.session_id,
messages=messages_data,
start_sequence=existing_message_count,
)
-
- # Back-fill sequence numbers on the in-memory ChatMessage objects so
- # that downstream callers (inject_user_context) can persist updates
- # by sequence rather than falling back to index-based writes.
for i, msg in enumerate(new_messages):
- msg.sequence = existing_message_count + i
+ msg.sequence = actual_start + i
async def append_and_save_message(
diff --git a/autogpt_platform/backend/backend/copilot/model_test.py b/autogpt_platform/backend/backend/copilot/model_test.py
index 4c48b56391..b404d74dcb 100644
--- a/autogpt_platform/backend/backend/copilot/model_test.py
+++ b/autogpt_platform/backend/backend/copilot/model_test.py
@@ -19,6 +19,7 @@ from .model import (
ChatMessage,
ChatSession,
Usage,
+ _save_session_to_db,
append_and_save_message,
get_chat_session,
get_or_create_builder_session,
@@ -964,7 +965,7 @@ async def test_save_session_to_db_persists_new_messages_when_windowed(
mock_db = mocker.MagicMock()
mock_db.update_chat_session = mocker.AsyncMock()
- mock_db.add_chat_messages_batch = mocker.AsyncMock()
+ mock_db.add_chat_messages_batch = mocker.AsyncMock(return_value=1500)
mocker.patch("backend.copilot.model.chat_db", return_value=mock_db)
await _save_session_to_db(
@@ -976,7 +977,8 @@ async def test_save_session_to_db_persists_new_messages_when_windowed(
assert kwargs["start_sequence"] == 1500
assert len(kwargs["messages"]) == 1
assert kwargs["messages"][0]["content"] == "brand-new"
- # And the in-memory new message receives its sequence back-fill.
+ # And the in-memory new message receives its sequence back-fill from the
+ # actual start returned by the batch helper.
assert new_msg.sequence == 1500
@@ -1244,3 +1246,33 @@ def test_chat_message_from_db_round_trips_created_at() -> None:
assert msg.sequence == 3
assert msg.duration_ms == 4200
assert msg.created_at == created_at
+
+
[email protected](loop_scope="session")
+async def test_save_session_to_db_uses_actual_start_after_collision(
+ mocker: MockerFixture,
+) -> None:
+ """When ``add_chat_messages_batch`` retries from ``get_next_sequence``
+ after a unique-constraint collision, it returns the offset actually
+ used. ``_save_session_to_db`` must back-fill in-memory sequences from
+ that returned value — using the original ``existing_message_count``
+ desyncs in-memory rows from DB and breaks later
+ ``update_message_content_by_sequence`` calls."""
+ msg_a = ChatMessage(role="assistant", content="msg-a")
+ msg_b = ChatMessage(role="user", content="msg-b")
+ session = ChatSession.new(user_id="u1", dry_run=False)
+ session.messages = [msg_a, msg_b]
+
+ # Caller said start at 0 (brand-new session) but the helper retried
+ # after a peer race and the rows actually landed at sequence 5.
+ mock_db = mocker.MagicMock()
+ mock_db.update_chat_session = mocker.AsyncMock()
+ mock_db.add_chat_messages_batch = mocker.AsyncMock(return_value=5)
+ mocker.patch("backend.copilot.model.chat_db", return_value=mock_db)
+
+ await _save_session_to_db(
+ session, existing_message_count=0, skip_existence_check=True
+ )
+
+ assert msg_a.sequence == 5
+ assert msg_b.sequence == 6