Skip to content

fix(ui): hold native tool-call markup and long calls out of the chat - #5131

Merged
kovtcharov-amd merged 9 commits into
mainfrom
fix/agent-ui-stream-ux
Oct 15, 2026
Merged

kovtcharov-amd merged 9 commits into
mainfrom
fix/agent-ui-stream-ux

Conversation

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

When a hosted model wrote its tool calls as text (DeepSeek's DSML markup), every call streamed into the answer area while it was being written: up to 50 times in one turn, with long Python mangled by Markdown into headings. Long calls in GAIA's own JSON format either leaked the same way or sat behind a bare "Reading the tool results · 90s" timer. Now the call is held back and dropped, and the activity line says what is being written and how big it is ("Writing Python code — 4,210 characters so far"), never its content. Markup quoted in a code block, or a tag named in prose, still shows.

Stacked on #5121 (the JSON-envelope half of this filter); pairs with #5103, which makes the agent run DSML calls instead of answering with them. This changes what the LLM's output looks like to the user, not what the LLM sees; the flagship eval that runs on main covers it.

Test plan

  • python -m pytest tests/unit/chat/ui/test_sse_tool_call_text.py tests/unit/chat/ui/test_sse_handler.py tests/unit/test_model_phase_progress.py tests/unit/test_tool_call_progress.py
  • Replayed the SSE streams captured from the affected Agent UI runs through the new filter: no DSML, tool JSON or code reaches a chunk event, and every final answer still streams
  • Drove the Agent UI with a scripted DSML run_python reply: before, the code scrolled past as headings; after, only the narration line and "Writing Python code — N characters so far" show
  • python util/lint.py --black --isort

A reply that called a tool with a long argument (a write_file carrying a
whole file) reached the TUI as a raw {"thought": ...} line above the tool
rows. The stream filter flushed any tool-call JSON past 2 KB as text, and
a plan envelope whose head the cleanup regex ate leaked its tail.

The handler now recognises the response envelope at the head of the
stream (optionally fenced), holds it until its closing brace with a
string-aware, resumable scan, and drops it; an "answer" field is still
emitted as the answer. Ordinary JSON answers and text that merely starts
with "{" are untouched.
A hosted model that writes its tool calls as DeepSeek DSML markup had every
call stream into the answer area, up to 50 times a turn, with long Python
mangled by markdown. A narration line sharing a token with a JSON envelope,
and a long thought with an unescaped quote, leaked the same way.

The stream filter now holds native call markup (DSML, the older DeepSeek
special tokens, XML invoke/function_calls/tool_call, harmony channels) from
opener to closer and drops it; markup quoted in a code block still shows.
An envelope that opens on its own line after prose is split off, and an
envelope led by a protocol key that will not parse is held whole.

While a call is held, the activity line now says what is being written and
how big it is ("Writing Python code — 4,210 characters so far"), or
"Reasoning — N words so far" while the envelope's thought streams, instead
of a bare timer.
A reply that mentioned a tag like <tool_call> in passing, or in its
reasoning, lost everything after it from the stream, since the filter took
it for a call with no closer. Plain-XML openers now count only at the start
of a line; DSML and DeepSeek's special tokens still count anywhere.

Code fences are tracked the way Markdown does (a marker at a line start,
closed by the same kind), on the text actually shown, so stray backticks in
prose or a dropped fenced envelope no longer make later calls look like
examples. Markup arriving in the token that closes an envelope is filtered
too.
@github-actions github-actions Bot added tests Test changes agents labels Oct 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

Tool calls that a model writes as text (DeepSeek DSML, Qwen/Claude XML, harmony channels) no longer stream into the chat. Long calls now show a progress line that gives the size, never the content. The filter is careful: it is fence-aware, handles tags split across tokens, resets state between model calls, and isn't quadratic. All 347 tests in the test plan pass locally. I found nothing blocking.

Two things to know:

  • Naming a DeepSeek or harmony tag mid-sentence still hides the rest of the reply while it streams. For example: "DeepSeek uses the <|DSML| calls> wrapper…". Only the plain-XML tags got the "start of a line only" rule. The final answer event repaints the full text, so the cut only shows while the reply is streaming. It's still worth closing, since the PR says tags named in prose show.
  • When the agent returns a markup-only reply as its final answer, the raw call still reaches the final bubble and the saved message. The PR's own evidence shows this (scenario C). It isn't a regression, and fix(agent): run DeepSeek DSML tool-call markup instead of answering with it #5103 is meant to close it, but it's worth naming in the description so nobody reads this PR as the full fix.

Real-world evidence

The evidence stage ran the real gaia.ui.server against a scripted stand-in Lemonade. It compared PR head with the pre-PR base on POST /api/chat/send.

  • A — narration, then a DSML call with planted code. PR head streams only the prose:
    "I'll check the weather tool first.\nDone. The forecast for Zephyr Bay is sunny."
    
    The base streamed the whole <|DSML| calls>…print('PLANTED-violet-otter-92', …)… block.
  • B — markup used as content. A <tool_call> named in prose and DSML inside a ``` fence both stream intact, and they match the answer event.
  • C — a 10K-char DSML call that the loop returns as the final answer. Chunks hold only 'Computing the table now.\n\n' plus progress statuses:
    {"type": "status", "status": "working", "message": "Preparing get_weather — 1,506 characters so far", "phase": "tool_call", "chars": 1506}
    The answer/done events and the persisted message still carry the raw DSML. The base behaves the same, so this isn't a regression.
  • D — native delta.tool_calls. The progress status (Preparing get_weather — 1,520 characters so far) comes through the moved helper. The planted value appears only in the tool-args panel.
  • Spot regression. JSON-envelope replies, the sessions/messages/active endpoints, and gaia chat -q all behave as before.

The rendered Agent UI screenshot is pending the strix-halo lane; this runner has no real inference. The route-level evidence above is enough for merge.

🔍 Technical details

🟢 Inline DSML / special-token / harmony openers in prose swallow the rest of the stream (src/gaia/ui/sse_handler.py, _MARKUP_FAMILIES / _next_markup_opener)

I fed these through SSEOutputHandler in 3-char pieces:

Input Streamed
DeepSeek uses the `<|DSML| calls>` wrapper for tools. Here is the rest… 'DeepSeek uses the `'
Inline: `<|tool▁call▁begin|>` marks a call. More text after. 'Inline: `'
Harmony uses <|channel|>commentary to=functions.x syntax. Rest of answer. 'Harmony uses'

Only families 4–6 are restricted to line starts. Inline code spans aren't treated like fences either. With no closer in the reply, the opener holds everything until end-of-stream, and the user sees "Preparing a tool call — N characters so far" in the meantime. One fix: skip an opener that sits inside an open single-backtick span on the current line. Or apply the line-start rule to every family except the calls/function_calls/tool_calls container forms, which real calls nearly always lead with. A regression test next to test_a_tag_named_in_prose_does_not_cut_the_reply would pin it.

🟢 Final-answer path doesn't strip markup (print_final_answer)

print_final_answer runs its JSON cleaners but has no DSML or special-token pass. When the loop treats a markup-only reply as final, the bubble (ChatView.tsx replaces it with answer) and the persisted message still show the raw call. This is out of scope if #5103 lands first. Otherwise, a _MARKUP_STRAY_RE-style strip of whole open…close spans there would match what the stream already does.

🟢 Behaviour change worth a line in the description

A reply led by thought/goal/plan/tool that won't parse as JSON is now held whole and dropped, where it used to be shown raw. Example: {"thought": "a "b" c", "answer": "The answer is 42."} streams ''. That's the intent, and the answer event still recovers the text through _clean_answer_json, so this is a note rather than a defect.

Strengths

  • Moving tool_call_progress_message into tools.py gives the native-delta path and the text-markup path one shared label table, so they can't drift.
  • The tests reproduce live failure shapes: split bars, 50 calls per turn, an unescaped quote in a thought, and fence state across replies. They also include explicit non-quadratic timing guards for 400 KB inputs.
  • State reset is in a finally and also in signal_done, so a stopped or cut-off call can't hide the next reply. Both cases are covered by tests.

# Conflicts:
#	src/gaia/agents/base/agent.py
# Conflicts:
#	src/gaia/ui/sse_handler.py
The envelope filter held every leading JSON object until its closing
brace, so an answer-first reply no longer streamed token by token as the
answer decoder on main does. Answer-first envelopes now go to that path;
every other envelope is still held and filtered.
Base automatically changed from fix/no-raw-thought-json to main October 15, 2026 12:39
…5131

# Conflicts:
#	src/gaia/agents/base/agent.py
#	src/gaia/ui/sse_handler.py
# Conflicts:
#	src/gaia/ui/sse_handler.py
#	tests/unit/chat/ui/test_sse_handler.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant