Keep mid-conversation system items in history order #871
The fix is right for #870. On main, the announcement replaces the leading prompt, and the chat sent to the model ends on assistant: 'It is sunny.'. On this branch the original prompt stays first and the announcement comes last. The new tests pass, and 2 of the 3 fail against main's src.
I found two problems:
1. The full suite has one new failure: tests/test_responses_api_language_model.py::test_responses_backend_keeps_injected_context_with_session_instructions (assert 0 == 1 at line 1969). The test adds a system snapshot after a user message and expects it to become init_chat_message, which is the Open WebUI snapshot behaviour from #687. The snapshot still reaches the model, now as a system message in the middle of the history. So the test needs updating to the new behaviour, not the code.
2. With --compact_history, a system item inside the compacted range is now lost. _render_transcript skips role == "system" (compaction_prompt.py:78), but the item's id is still in marker_ids, so _apply_compaction deletes it. I checked this with a stub compactor: a system message "The operator's name is Dana", added after the first turn, appears neither in the summarizer input nor in the chat after compaction. On main it survives because it is moved into the leading prompt. Either render system items into the transcript (for example as [System: ...]), or leave them out of marker_ids so they stay in the buffer.
Thanks for catching both of these — you were right on each point.
1. I updated test_responses_backend_keeps_injected_context_with_session_instructions to the new behaviour. A snapshot added after a user turn stays as a mid-history system message and still reaches the model once; it is no longer folded into init_chat_message.
2. Mid-conversation system items are now left out of marker_ids, so --compact_history keeps them in the buffer after the summary splice instead of dropping them.
Pushed in 7f1156e.
Good work, thank you!
<img src="https://static.klipy.com/ii/925f17378dd1893b674a723c07535afe/fa/af/SnlGKUg7.gif"/>
andimarafioti
ZYZ666-RGB
Relates to #870.
Problem
A Realtime system item arriving after conversation content replaced
init_chat_messageinstead of staying in order. The transformed chat still ended on the previous assistant turn. llama.cpp's default assistant-prefill behavior can continue that turn instead of processing the announcement.Change
role: systemfor Chat Completions and Responses.prepend_instructions()behavior for that prompt.The separate 16/24 kHz audio report in #870 is not changed.
Validation
Regression tests failed before the fix and pass after it, covering message placement and running-tool state.
Ruff passed; 350 files already formatted; mypy passed for 179 source files; selected pytest coverage: 286 passed.
Disclosure and limitations
Prepared with Cursor.
The required live tool-use conversation eval (
python -m speech_to_speech.evals.tool_conversations) was not run: this environment has no model API credentials, llama.cpp server, or model weights. The spoken repetition was not reproduced against a live model. The selected tests include the eval harness tests, not a live model evaluation. The full pytest tree was not run.No DCO or CLA signature is included; the commit is unsigned.