fix(tui): keep model switch markers out of API history - #48356
Open
markoub wants to merge 2 commits into
Open
Conversation
…tch-session-meta-repair # Conflicts: # tests/test_tui_gateway_server.py # tui_gateway/server.py
teknium1
reviewed
Jul 14, 2026
teknium1
left a comment
Contributor
There was a problem hiding this comment.
Thanks for separating transcript-only model-switch metadata from provider-visible history. The current-main premise remains real: tui_gateway/server.py:2448 persists a switch marker as a user message, and _run_prompt_submit passes raw history to AIAgent at tui_gateway/server.py:9048.
Problems
- The new legacy filter at
tui_gateway/server.py:4663only removes markers whose role issystem. Current main changed new markers touserin commit61622bb(tui_gateway/server.py:2444-2470), so already-persisted user-role model-switch markers remain in the provider history after this change.
Suggested changes
- Recognize the exact model-switch marker independent of its stored role, then filter legacy
userandsystemrows plus newsession_metarows. Add a test covering a persisted user-role marker without filtering ordinary user turns.
Automated hermes-sweeper review.
| # provider-visible instructions. | ||
| if msg.get("role") == "session_meta": | ||
| continue | ||
| if msg.get("role") == "system" and _is_model_switch_marker(msg): |
Contributor
There was a problem hiding this comment.
Current main has persisted model-switch markers as role="user" since 61622bb. This condition removes only older system rows, so those user-role markers still reach conversation_history; recognize the exact marker content independently of role.
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.
What does this PR do?
Fixes TUI model-switch metadata so it cannot become a mid-conversation
systemmessage in provider API history.When
/modelchanges the active runtime, Hermes already refreshes and persists the session system prompt. This PR keeps the additional marker as transcript-onlysession_metaand filters both newsession_metarows and legacy model-switchsystemmarkers before handing history toAIAgent.The merge path then reconciles the agent's append-style result back onto the raw TUI transcript so transcript-only rows are preserved locally without being sent to the provider.
This covers new switches and already-persisted markers from older versions that could trigger strict OpenAI-compatible backends such as vLLM/Qwen to return HTTP 400.
Related Issue
Fixes #48338
Type of Change
Changes Made
tui_gateway/server.py: store model-switch markers assession_meta, notsystem.tui_gateway/server.py: filter transcript-onlysession_metarows and legacy TUI model-switchsystemmarkers beforeprompt.submitcallsAIAgent.tui_gateway/server.py: merge append-style agent results back onto the raw TUI transcript so filtered local metadata is not lost.tests/test_tui_gateway_server.py: cover newsession_metamarkers, legacysystemmodel-switch markers, preserved ordinary system messages, and transcript merge behavior.How to Test
systemmessage.uv run scripts/run_tests.sh tests/test_tui_gateway_server.py -- -k 'model_switch or config_set_model or preserves_transcript_meta' --tb=longuv run scripts/run_tests.sh tests/run_agent/test_session_meta_filtering.py -- --tb=longgit diff --check && uv run ruff check tui_gateway/server.py tests/test_tui_gateway_server.pyChecklist
Code
pytest tests/ -qand all tests passuv run scripts/run_tests.shDocumentation
cli-config.yaml.exampleif adding config options, or this change does not add config optionsCONTRIBUTING.mdorAGENTS.mdif changing development workflow, or this change does not affect development workflowFor New Skills
N/A
Screenshots / Logs
uv run scripts/run_tests.sh tests/test_tui_gateway_server.py -- -k 'model_switch or config_set_model or preserves_transcript_meta' --tb=longpassed 12 tests.
uv run scripts/run_tests.sh tests/run_agent/test_session_meta_filtering.py -- --tb=longpassed 5 tests.
git diff --check && uv run ruff check tui_gateway/server.py tests/test_tui_gateway_server.pypassed.
Note: I also ran the full
tests/test_tui_gateway_server.pyfile. It passed 277/278 tests; the only failure was the unrelated local browser-launch assertiontest_browser_manage_connect_default_local_reports_launch_hint, which also fails when run alone in this checkout.