Skip to content

fix(agent): run DeepSeek DSML tool-call markup instead of answering with it - #5103

Merged
kovtcharov-amd merged 4 commits into
mainfrom
fix/deepseek-dsml-tool-calls
Oct 15, 2026
Merged

kovtcharov-amd merged 4 commits into
mainfrom
fix/deepseek-dsml-tool-calls

Conversation

@kovtcharov-amd

@kovtcharov-amd kovtcharov-amd commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

On DeepSeek models, a task could silently stop and hand the user raw tool-call markup as the answer. In rc12 TUI testing on the gateway's amd.DeepSeek-V4.1-Flash, an expense audit (kw-06) wrote its 23rd tool call as DeepSeek's DSML markup in plain text; GAIA delivered <|DSML| calls><|DSML| invoke name="run_python">… as the final answer and the audit never ran. Now that markup runs as the tool call it describes (same confirmations and guards), broken markup gets one retry and is never shown, and no final answer carries raw tool-call markup in any supported format.

Eval-gated: this changes tool-call and answer parsing, so the tool_selection, gaia-code and gaia-files lanes of eval_flagship.yml must pass before merge.

Adversarial tests

26 tests in tests/unit/agents/test_deepseek_tool_markup_adversarial.py try to make markup run when it shouldn't, or get lost when it should. They found one real problem, fixed in this PR: telling whether markup sits inside code compared every match against every code span, so an answer with a few thousand inline-code mentions took seconds to scan (64 KB took about 2 s, 128 KB about 7 s). It now bisects over the sorted spans.

  • Markup in fenced, tilde, unclosed or inline code is neither run nor stripped, and a fenced example in a final answer arrives intact.
  • Prose naming <invoke name= or <|DSML| is not parsed as a call.
  • Mixed fullwidth/ASCII bars and two blocks parse. Nested, truncated or nameless markup raises instead of running part of it.
  • A value holding the closing tag raises rather than silently truncating. JSON-escaped, it round-trips. A 500 KB value parses in milliseconds.
  • Markup for an unregistered tool never runs. Gated markup calls still need approval, and two approved calls run in order.

Known gap, not changed here: an answer that mentions <invoke name="x"> in prose, outside backticks, is cut from that point on, because unclosed markup is stripped to the end.

Test plan

  • python -m pytest tests/unit/agents/test_deepseek_tool_markup.py — 38 pass; the 10 agent-loop tests fail against main (fixture is the real kw-06 markup, trimmed)
  • python -m pytest tests/unit/agents/ tests/unit/test_tool_call_priority.py — 2402 passed
  • Other unit tests that drive process_query / _parse_llm_response — 1944 passed
  • python util/lint.py --black --isort — clean
  • eval_flagship.yml lanes tool_selection, gaia-code, gaia-files pass against the flagship baseline
  • Re-run kw-06 in the TUI on amd.DeepSeek-V4.1-Flash: the audit continues past the DSML step and the answer has no markup
🔍 Technical details
  • Root cause: the reply was {"thought": "...</think>\n\n<|DSML| calls>…. _split_reasoning moved everything before </think> into reasoning, leaving only DSML, which _parse_llm_response took as plain text. The unfinished-answer guard matches <invoke name=, but in DSML the fullwidth bar sits between < and invoke, so it never fired.
  • New gaia/agents/base/tool_markup.py parses DSML (| and |, string="true" = raw text, otherwise JSON) and the older <|tool▁calls▁begin|> form. It runs only on non-JSON replies, so a JSON call that quotes markup in an argument is untouched.
  • DeepSeek mixed in goal/tool as DSML parameters; JSON-envelope keys the tool doesn't declare are dropped before dispatch.
  • MalformedToolMarkup is a ValueError, so it uses the existing parse-retry path. It gets one retry, reset after a clean parse, then a plain failure message.
  • _without_tool_markup runs before finalize_answer on all three answer paths: normal, step-cap and loop-break. Markup inside fenced (```/~~~) or inline code is left alone.
  • Not covered: while streaming, markup tokens still show live until the reply is parsed. That's the same as the existing JSON-call path.

Fixes #5101
Refs #5087

…ith it

DeepSeek models sometimes write their next tool call as native DSML markup
in plain text. The agent loop delivered that markup as the final answer, so
the task stopped and the user saw raw <|DSML| invoke ...> tags (kw-06 on
amd.DeepSeek-V4.1-Flash, rc12).

- Parse DSML (fullwidth and ASCII bar) and DeepSeek's older special-token
  form into real tool calls that go through the normal guards and
  confirmations; JSON-envelope keys the tool does not take are dropped.
- Truncated or malformed markup is a parse failure: one retry naming the
  defect, then a plain failure message, never the markup.
- Every final answer is stripped of tool-call markup (DSML, DeepSeek tokens,
  <tool_call>, <function_calls>/<invoke>, harmony calls) outside code, and
  the unfinished-answer guard recognises those formats.

Fixes #5101
Refs #5087
@github-actions github-actions Bot added tests Test changes agents labels Oct 14, 2026
Checking whether markup sat inside code compared every match against every
code span, so an answer with a few thousand inline-code mentions of tool
markup took seconds to scan (64 KB: ~2 s, 128 KB: ~7 s). The spans are
sorted and disjoint, so a bisect finds the only candidate.

Also pins: markup in fenced, tilde and unclosed fences or inline code is
neither run nor stripped; prose naming <invoke name= is not a call; mixed
bars and two blocks parse; nested, truncated or nameless markup raises
instead of running part of it; a value holding the closing tag raises
rather than truncating; a 500 KB value parses; markup for an unregistered
tool never runs; gated markup calls still need approval.
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions. DeepSeek tool calls written as markup now run as real calls instead of being shown to the user as the answer. The fix is well scoped and the adversarial tests are thorough. I found no correctness or security bugs. Calls written as markup still go through the same tool lookup and approval gate as JSON calls.

Before merge:

  • 🟡 The required evals haven't run yet. This changes how tool calls are parsed, so CLAUDE.md requires the tool_selection, gaia-code and gaia-files eval lanes to pass first. The re-run of kw-06 in the TUI is the real-world proof for the bug, and it's also unchecked. Please run both and record the results on the PR.

Nits:

  • 🟢 When broken markup fails its retry, the turn ends without the generated-image path that the other give-up path keeps. A broken call right after an image is generated would lose that path from the final answer.
  • 🟢 Answer cleanup now also matches <tool_call> and <function_calls>. An answer that explains those formats in plain prose, outside backticks, gets sent back to the model and then cut off at that point. This widens the known gap you already describe.
🔍 Technical details
  • Image path on markup give-up (src/gaia/agents/base/agent.py, the isinstance(parse_exc, MalformedToolMarkup) branch in the parse-failure handler): final_answer = _TOOL_MARKUP_FAILURE_ANSWER; break runs before the parse_failures >= 3 branch, which is the only place _last_image_path reaches the answer. Suggested fix: when _last_image_path is set, add the same "I generated your image at …" sentence to the failure answer.
  • Wider stripping: _unfinished_answer_kind now ORs in has_tool_markup(text). _ANY_MARKUP includes the <tool_call>, <function_calls> and harmony <|channel|>commentary to= openers from _STRIP_BLOCKS[2:]. Inside code these are skipped. Outside code with no closing tag, _strip_blocks deletes everything to the end. A test that pins the intended behaviour for an answer like "Qwen wraps calls in <tool_call> tags." would make this a deliberate choice rather than a side effect.
  • Checked and fine: MalformedToolMarkup subclasses ValueError, so it uses the existing retry path. markup_retries resets after a clean parse. Because of the _is_json guard, a JSON call that quotes markup inside an argument is not treated as markup. Multi-call markup returns the same tool_calls shape as native calls, so the fan-out path handles it unchanged.

# Conflicts:
#	src/gaia/agents/base/agent.py
flake8 read the imported fixture, reused as a test argument, as a
redefinition (F811).
@kovtcharov-amd
kovtcharov-amd merged commit 13c9064 into main Oct 15, 2026
@kovtcharov-amd
kovtcharov-amd deleted the fix/deepseek-dsml-tool-calls branch October 15, 2026 04:48
kovtcharov-amd added a commit that referenced this pull request Oct 15, 2026
…5131)

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
- [x] `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`
- [x] 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
- [x] 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
- [x] `python util/lint.py --black --isort`
kovtcharov pushed a commit to kovtcharov/gaia that referenced this pull request Oct 30, 2026
…er (amd#5404)

The proactive Claude reviews were failing about half the time, and each
failure was lost. The code and security audits failed 5 of the last 10
nights, and the doc walkthrough failed 4 of its last 5 runs. Running
three audits every night used up both Claude accounts' weekly limit by
mid-week, so most of those runs stopped before reviewing anything and
blamed a missing file. The next run never went back for the missed work.
Now one audit runs each night between 2 and 4am Pacific, so each audit
runs every third night. Each run checks Claude is available before
starting, and says plainly when it isn't. It picks up everything since
its last successful run, starts from what merged since then, and checks
against the bug types past audits missed. The dedup pass no longer
re-files duplicates that were already closed: 34 of them were closed
alongside this change.

## Test plan

- [ ] `python -m pytest tests/unit/test_audit_synthesis_prep.py
tests/unit/test_audit_history.py tests/unit/test_audit_rotation.py
tests/unit/test_require_claude.py tests/unit/test_recent_changes.py
tests/unit/test_miss_analysis.py
tests/unit/test_claude_audit_workflow_contract.py
tests/unit/test_closed_duplicates.py -q` passes (277 tests)
- [ ] After merge: `gh workflow run claude-audit-rotation.yml -R
amd/gaia -f dry_run=true`. The summary names tonight's audit and the
next five nights, and nothing is dispatched.
- [ ] After merge: `gh workflow run claude-nightly-audit.yml -R amd/gaia
-f dry_run=true`. Check the preflight summary: the mode, a window
reading "since this audit's last successful run", the Claude probe
passing, and "Recent changes read first". At the end, "Dry run
confirmed: no issue created".
- [ ] After merge: `gh workflow run claude-weekly-doc-walkthrough.yml -R
amd/gaia -f doc_filter=docs/quickstart.mdx -f dry_run=true`. The guide
is walked, "Using bash.exe from …" appears, and the dry-run watermark
step passes (it crashed before this change).
- [ ] After merge: `gh workflow run claude-security-audit.yml -R
amd/gaia`. Semgrep scans `tui`, `installer` and `.github/workflows`, and
the SARIF uploads.
- [ ] First scheduled night (10:13 UTC): the dispatched run is titled "…
rotation night N", files at most what's new, and an issue it files
carries `audit-cluster` / `audit-key` markers.

<details>
<summary>🔍 Technical details</summary>

**Root causes (last ~2 weeks)**
- Code and security audits, Oct 7/10/12/13/14/17: every lens returned
`is_error` in under a second with "You've hit your weekly limit". The
primary account ran out on Oct 10, the secondary on Oct 12–14, both on
Oct 17. The account switch fired correctly; both accounts were spent.
- Walkthrough Oct 12: same weekly limit on every guide, queued over 57
hours on the Strix Point pool. Oct 5 and Oct 19: the `xsj-aimlab-stxp`
images have no Git for Windows (only WSL's `bash.exe`). Also on Oct 5,
the executor step's `GITHUB_OUTPUT` got UTF-16 appended by
`start-lemonade.ps1` under PowerShell 5.1 `>>`. Sep 21–28: Application
Control blocked `LemonadeServer.exe` and the job landed on Strix Halo
machines; both were fixed by the move to the `xsj-dispatch` pool on Oct
1.
- The walkthrough's two dry-run steps had a literal `\n` mid-command, so
`dry_run=true` always failed.

**Schedule.** One cron, `13 10 * * *` = 03:13 PDT / 02:13 PST. Only
10:00–11:00 UTC is inside 2–4am Pacific in both seasons. Slot = days
since epoch mod 3 → code audit, security audit, doc walkthrough. The
audits lost their own crons; `workflow_dispatch` still runs any of them
on demand. Deep code sweep: whenever the last successful one is over 8
days old (every third turn, retried on the next turn if it fails).

**Dedup bug.** The 2026-09-07 burst (78 issues) predates the dedup pass:
the run checked out main 7 hours before amd#3310 merged. Three duplicate
pairs still got through after amd#3310 (amd#3832/amd#3471, amd#3825/amd#3414,
amd#5199/amd#4875). Backlog matching compared findings against issue titles
only, so a same-section duplicate worded differently scored 0.05–0.15,
below the 0.18 threshold. Now matching includes each audit issue's own
markers (all 13 known pairs rank 1st–2nd). Closed duplicates hand their
keys to the open canonical, so consolidating into amd#1077/amd#4331 does not
trigger re-filing. Both syntheses share the `claude-audit-issue-filing`
concurrency group. The security audit files to private code scanning,
not issues, so it cannot duplicate the other two.

**Closed as duplicates (34):** into amd#4331: amd#3204 amd#3413 amd#3422 amd#3439 amd#3466
amd#3476. Into amd#1077: amd#3173 amd#3412 amd#3418 amd#3426 amd#3429 amd#3430 amd#3433 amd#3437 amd#3442
amd#3449 amd#3454 amd#3455 amd#3464 amd#3468 amd#3475 amd#3477 amd#3482. Into amd#3198: amd#3436
amd#3462. Others: amd#3425→amd#3169, amd#3457→amd#3195, amd#3440→amd#3180, amd#3486→amd#3064,
amd#3471→amd#3832, amd#3414→amd#3825, amd#3434 amd#3485→amd#3219, amd#5199→amd#4875.

**Miss-analysis (merged `fix` PRs, Jun 21–Oct 19).** 29 of 1005 (3%)
closed an issue an audit filed first. Missed, by title: no deadline 90
(amd#5346 amd#5384 amd#5380), release/CI ordering 71 (amd#5127 amd#5147), Windows
behaviour 38 (amd#5018 amd#5377), dishonest status 36 (amd#5112 amd#5317), shared
state across processes 34 (amd#5337 amd#5282 amd#5378), model output handling 27
(amd#4973 amd#5103), doc drift 27, cold start / non-default setup 26 (amd#4951
amd#5130). About 380 more were Agent UI/TUI or agent-behaviour fixes, which
a static read rarely catches. Noise: of the audit issues closed as
duplicate or not planned, 31 were duplicates of amd#1077, 12 of the
test-coverage umbrella amd#2988, and 6 each of amd#4331, amd#3149, amd#3308 and
amd#2922. What changed: `scripts/audit/missed_bug_classes.md` has a
checklist per class and a "Don't report" list, and all three prompts
read it at run time. The code audit gets a deterministic list of calls
with no deadline. The security audit's semgrep run and worklist now
cover TypeScript, Go, the installer and the workflows.
`scripts/audit/miss_analysis.py` refreshes the evidence monthly.

**Recent changes.** Each audit's first job writes `recent-changes.md`:
first-parent commits since the commit its last successful run checked
out, read from the run history, with PR title, a short body excerpt and
the paths each touched, capped at 30 KB. If that commit is unknown it
covers 3 days and says so.
</details>
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.

fix(agent): DeepSeek's DSML tool-call markup is delivered as the final answer instead of running

1 participant