fix(llm): stop 'role: fact' 422 that broke terminal chat#240
Merged
Conversation
Every message in the terminal/voice chat 422'd — the LLM server rejected
role="fact" ("Input should be 'user'|'assistant'|'system'|'developer'|'tool'"),
and the repeated failure surfaced to the user as "Qwen busy".
Cause: the `conversations` memory table holds role="fact" rows (77 of them —
written by fact_extract / memory_save alongside real user/assistant turns), and
the session bootstrap replayed the last 10 rows verbatim as chat messages. A
fact row became an invalid message.
Fixed at two layers:
- codec_core.py (generated launch script) + codec_session.py (runtime): the
previous-session replay now keeps only real turns (role in user/assistant/
system). Facts stay in the DB for the memory system; they were never chat turns.
- codec_llm._build_request (the single chokepoint call() and stream() share):
_sanitize_roles() relabels any non-standard role to 'user' — valid at ANY
position, so it can't 422 (bad role) nor 500 ('system message must be first'
if a stray fact lands mid-conversation). Content preserved.
Verified LIVE against the Qwen server: a messages array containing a role="fact"
entry now returns a clean answer (previously 422). 4 new tests. ruff clean.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Terminal/voice chat 422'd on every message (role='fact' invalid) → surfaced as 'Qwen busy'. The conversations table holds 77 fact rows and the session bootstrap replayed them verbatim as messages. Fixed at two layers: the replay now keeps only real turns; codec_llm sanitizes any stray non-standard role to 'user' at the shared chokepoint. Verified live against Qwen. 4 tests.