← Back to Pull requests

Keep mid-conversation system items in history order #871

Merged
ZYZ666-RGB merged 7 files (407 lines) on Nov 30, 2026 14:09 UTC
View on GitHub
ZYZ666-RGB ZYZ666-RGB commented on Nov 24, 2026 06:55 UTC
Author

Relates to #870.

Problem


A Realtime system item arriving after conversation content replaced init_chat_message instead 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


  • Keep mid-conversation system items in place and serialize them as role: system for Chat Completions and Responses.

  • Preserve leading prompt replacement before conversation content and prepend_instructions() behavior for that prompt.

  • Ignore in-order system items in running-tool state so they neither make tool results late nor close incomplete tool batches.
  • 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.

    python -m ruff check src tests
    python -m ruff format --check src tests
    python -m mypy src
    PYTHONPATH=src python -m pytest tests/test_chat.py tests/test_running_tool_calls.py tests/test_chat_completions_backend.py tests/test_response_overrides.py tests/openai_realtime/test_running_tool_calls.py tests/openai_realtime/test_conversation_delete.py tests/evals/test_tool_conversations.py -q
    

    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.

    Diff: 407 lines (+360 / -47) across 7 files Reviewer: andimarafioti Review duration: 9074m
    All Vouch review confidence checks passed
    Residual risk 0.10 = Change Risk (0.53) × (1 − Review Confidence (0.81)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.53 · 407 lines diff across 7 files
    0.53
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.66 · 1 substantive comment(s)
    0.66
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    81%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.360
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    4 items
    1AbdullahButt 1AbdullahButt commented on Nov 25, 2026 08:28 UTC
    Reviewer

    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.

    ZYZ666-RGB ZYZ666-RGB commented on Nov 25, 2026 10:20 UTC
    Author

    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.

    andimarafioti andimarafioti approved these changes on Nov 30, 2026 14:09 UTC
    Approved Reviewer
    Reviewers
    Assignees
    ZYZ666-RGB ZYZ666-RGB
    Labels
    risk: low confidence: 81.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52.8% (MEDIUM) · Higher = larger blast radius
    52.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 81.4% · Lower = rushed or shallow review
    81.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 9.8% · Safe to merge
    9.8%