Skip to content

fix(chat): keep conversation history starting on a user turn - #655

Open
subidhkhanal wants to merge 1 commit into
Open-Finance-Lab:mainfrom
subidhkhanal:fix/chat-history-starts-with-user
Open

subidhkhanal wants to merge 1 commit into
Open-Finance-Lab:mainfrom
subidhkhanal:fix/chat-history-starts-with-user

Conversation

@subidhkhanal

Copy link
Copy Markdown
Contributor

The bug. The chat keeps the last 12 messages (6 questions and 6 answers). On the 7th question it cuts the history from the front, but only one message is removed: the oldest question. Its answer stays at the top. So from the 7th message on, the model receives a conversation that opens with an answer to a question it never saw.

Before:  q1 a1 q2 a2 ... q6 a6 q7  ->  cut  ->  a1 q2 a2 ... q6 a6 q7   (starts with an answer)
After:                                       ->  q2 a2 ... q6 a6 q7      (starts with a question)

Why it matters. It doesn't throw an error, which is why nobody noticed. It silently makes answers worse in any chat longer than 6 messages. I checked this with one real request to Anthropic in the same shape ([assistant: "Yes, I sold it.", user: "Why?"]). The API accepted it, and the model replied "I don't have context for…". Anthropic's docs also say the first message should be from the user. I couldn't test the CommonStack route used in production (no key), so its exact behaviour is unverified, but the model gets the same broken context either way.

The failure path had the same problem: after a failed request, the history could be left starting with an answer.

The fix (domain/chat/service.py). A small helper, _trim_history, keeps the last 12 messages and then drops any answer left at the front. In effect, the oldest question and its answer are removed together. It replaces both places that trimmed the history.

Tests (tests/domain/chat/test_service.py):

  • A 7th message after 6 full exchanges: the conversation actually sent to the model starts with a question. This fails on the old code (it sent a1 first).
  • A failed request: the history still starts with a question. This also fails on the old code.
  • The existing cap test used 12 answer-only messages, which can't happen in real use. It now uses a normal question/answer history.

@vercel

vercel Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@subidhkhanal is attempting to deploy a commit to the allan-feng's projects Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant