Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(agents): return the newest messages when a conversation exceeds the history window (#968)

Ryan Huber81ec36592e44
diff --git a/backend/app/agents/runtime/executor.py b/backend/app/agents/runtime/executor.py
index fb27f7fe..0d923357 100644
--- a/backend/app/agents/runtime/executor.py
+++ b/backend/app/agents/runtime/executor.py
@@ -346,6 +346,12 @@ class AgentExecutor:
# 6. Conversation history
# 7. The new user message (appended in step 4 below)
history = await conversation_service.list_messages(session, conversation_id, limit=agent.max_history_messages * 2 + 2)
+ # The window can open mid-turn: a turn with parallel tool calls has an
+ # odd number of rows, so the cut can land on a tool result whose
+ # assistant call fell outside it. Providers reject an orphaned tool
+ # result, so replay from the first user message in the window.
+ while history and history[0].role != "user":
+ history.pop(0)
messages: list[ChatMessage] = []
# Runtime guardrail goes FIRST and applies to every conversation,
# regardless of agent settings or per-agent system prompt. Locks
diff --git a/backend/app/agents/services/conversation_service.py b/backend/app/agents/services/conversation_service.py
index e0b36664..4c10f16f 100644
--- a/backend/app/agents/services/conversation_service.py
+++ b/backend/app/agents/services/conversation_service.py
@@ -69,13 +69,20 @@ async def delete_conversation(
async def list_messages(
session: AsyncSession, conversation_id: uuid.UUID, limit: int = 200
) -> list[Message]:
+ # Newest `limit` messages, returned in ascending ordinal order. Ordering
+ # ascending BEFORE applying the limit pinned the window to the start of
+ # the conversation: once a thread outgrew the executor's replay budget,
+ # the model never received the newest user turn and kept re-answering
+ # the last question inside the stale window.
q = (
select(Message)
.where(Message.conversation_id == conversation_id)
- .order_by(Message.ordinal.asc())
+ .order_by(Message.ordinal.desc())
.limit(limit)
)
- return list((await session.execute(q)).scalars().all())
+ rows = list((await session.execute(q)).scalars().all())
+ rows.reverse()
+ return rows
async def append_message(
diff --git a/backend/tests/test_agents_conversation_service.py b/backend/tests/test_agents_conversation_service.py
index d6e10651..33f27d88 100644
--- a/backend/tests/test_agents_conversation_service.py
+++ b/backend/tests/test_agents_conversation_service.py
@@ -164,3 +164,27 @@ async def test_delete_conversation_removes_row(session, test_user, test_workspac
)
assert await svc.delete_conversation(session, conv.id, test_workspace.id) is True
assert await svc.get_conversation(session, conv.id, test_workspace.id) is None
+
+
+async def test_list_messages_window_keeps_newest_turns(session, test_user, test_workspace, test_agent):
+ """Regression: with more messages than `limit`, the returned window must
+ end at the NEWEST message. The executor replays this window to the LLM —
+ when the limit selected the oldest rows instead, the model never saw the
+ user's latest question in long conversations and re-answered stale ones."""
+ conv = await svc.create_conversation(
+ session, workspace_id=test_workspace.id, user_id=test_user.id, agent_id=test_agent.id,
+ )
+ for i in range(30):
+ await svc.append_message(
+ session, conversation_id=conv.id,
+ role="user" if i % 2 == 0 else "assistant",
+ content=f"msg-{i}",
+ )
+
+ msgs = await svc.list_messages(session, conv.id, limit=10)
+
+ assert len(msgs) == 10
+ assert [m.content for m in msgs] == [f"msg-{i}" for i in range(20, 30)]
+ ordinals = [m.ordinal for m in msgs]
+ assert ordinals == sorted(ordinals)
diff --git a/backend/tests/test_agents_executor.py b/backend/tests/test_agents_executor.py
index 1f9a8785..80daaed4 100644
--- a/backend/tests/test_agents_executor.py
+++ b/backend/tests/test_agents_executor.py
@@ -28,6 +28,7 @@ from app.agents.providers.base import (
Usage,
)
from app.agents.runtime.executor import AgentExecutor, ExecutorEvent
+from app.agents.services import conversation_service
pytestmark = pytest.mark.asyncio
@@ -553,3 +554,51 @@ async def test_max_iterations_terminates_runaway_agent(session, test_user, test_
done = next((e for e in events if e.type == "done"), None)
assert err is not None and err.error_code == "max_iterations"
assert done is not None and done.finish_reason == "max_iterations"
+
+
+async def test_history_window_never_opens_on_an_orphaned_tool_result(
+ session, test_user, test_agent, test_conversation,
+):
+ """A turn with parallel tool calls has an odd number of rows, so the
+ history window can start on a tool result whose assistant call was cut
+ off. Providers reject that, so the replay must start at a user message."""
+ test_agent.max_history_messages = 3 # window of 3 * 2 + 2 = 8 rows
+ await session.commit()
+
+ async def add(role, content="", **kw):
+ await conversation_service.append_message(
+ session, conversation_id=test_conversation.id, role=role, content=content, **kw,
+ )
+
+ await add("user", "q0")
+ await add("assistant", tool_calls=[
+ {"id": "c1", "name": "securo__list_accounts", "arguments": {}},
+ {"id": "c2", "name": "securo__list_budgets", "arguments": {}},
+ ])
+ await add("tool", "{}", tool_result={"tool_call_id": "c1"})
+ await add("tool", "{}", tool_result={"tool_call_id": "c2"})
+ await add("assistant", "a0")
+ for i in (1, 2):
+ await add("user", f"q{i}")
+ await add("assistant", f"a{i}")
+
+ provider = _ScriptedProvider([[
+ ChatChunk(type="text_delta", text="ok"),
+ ChatChunk(type="finish", finish_reason="stop"),
+ ]])
+ with _patch_provider(provider):
+ await _drain(
+ AgentExecutor(mcp=_FakeMCP(tools=[])),
+ session=session,
+ agent=test_agent,
+ user_id=test_user.id,
+ conversation_id=test_conversation.id,
+ user_message="new question",
+ )
+
+ replayed = [m for m in provider.requests[0] if m.role != "system"]
+ assert [(m.role, m.content) for m in replayed] == [
+ ("user", "q1"), ("assistant", "a1"),
+ ("user", "q2"), ("assistant", "a2"),
+ ("user", "new question"),
+ ]