fix: MOIM injection crashes session when conversation ends with assistant message#7421
fix: MOIM injection crashes session when conversation ends with assistant message#7421angiejones wants to merge 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug where MOIM (Model-Oriented Information Message) injection crashes the session when conversations end with an assistant message. The bug occurred because the old logic always inserted MOIM before the last assistant message, creating a trailing assistant message that would be removed by fix_conversation, causing API validation failures with a 400 error from providers like Anthropic.
Changes:
- Modified
inject_moimto append MOIM after the last message when conversation ends with assistant - Added test case validating the fix for trailing assistant message scenarios
|
Hey, @angiejones 👋 The MOIM never moves a real user message back. And it never pushes an assistant message forward. It would be better to fix this in the reply loop, I think. This is definitely a real issue, but the MOIM injection is not the cause and we probably shouldn't try to fix it there. |
|
The conversation being sent back to the inference provider should not end with an assistant message ever. I think we're seeing that error crop up when a tool call request exceeds the max tokens for a single reply, which then gets sent half baked to the inference provider instead of correctly processed. I haven't RCA'ed it, just my initial thoughts This case used to fail silently, but since Anthropic stopped allowing prefills, it now fails loudly (again, just a guess) |
ok sending you my session for debugging |
|
Working in #7424 |
Summary
MOIM injection kills sessions when the conversation ends with an assistant message (e.g. text-only response during retry). The conversation gets sent to the API ending with an assistant message, and models that don't support prefill reject it with a 400.
The bug —
inject_moimalways inserts before the last assistant message:Old code inserts MOIM at index 10 (before the last assistant):
This creates:
fix_conversationremoves the trailing assistant →has_unexpected_issuesfires → falls back to the original conversation which still ends with assistant → 400 from Anthropic: "This model does not support assistant message prefill. The conversation must end with a user message." Retried 3x, all fail, session dies.The fix is to append MOIM after the last message when the conversation ends with an assistant message.
Type of Change
AI Assistance
Testing
New test
test_moim_injection_trailing_assistantreproduces the exact scenario. Verified it fails on old code and passes with the fix:All existing tests pass (4 moim + 38 conversation). Clippy clean.