Skip to content

server: keep tool markup quoted in Markdown code spans and fences as prose - #3588

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:fix-prose-quoted-tool-calls
Nov 17, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
gilby:fix-prose-quoted-tool-calls

Conversation

@gilby

@gilby gilby commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

What this changes

Tool-call markup quoted inside Markdown inline code spans or fenced code blocks (backtick or tilde fences) in assistant prose previously opened, hid trailing prose, rejected following real calls, or parsed and streamed as live tool calls; a linear-time Markdown code-span and fence scanner keeps code-quoted call markup as prose across whole-reply parsing, SSE streaming, single-call filtering, thinking-tail lifting, LFM2 rewrites, logprob ranges, and EOS call closing while leaving whole-reply bare JSON fences unchanged. zig/src/server/reply_code.zig is written from scratch following CommonMark inline code-span and fenced code-block rules, and zig/src/server/tool_policy.zig is a structural split from zig/src/server/tool_stream.zig to keep every file within 600 lines.

Checks on this head

  • Head commit: 98f2d0df00dafa5c5c478ea6bdc1c3b325d83d55
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: base 077998f7d9b14adebec3a2acc2caa47ff7ae6ec7 -> 6578 files, 221 problems (inherited); head 98f2d0df00dafa5c5c478ea6bdc1c3b325d83d55 -> 6578 files, 221 problems (python3 tools/zig/lean_new.py -> lean: 221 problems, 221 on the base, 0 new; python3 tools/pr_check.py --base origin/main -> lean 221 vs 221, 0 new, 0 errors, all 12 changed files <= 600 lines).
  • Tests run, with the command and pass, fail and skip counts: pre-fix on origin/main (077998f7d): zig build test-tool-parse-host --summary all failed (76 pass, 4 fail (80 total) — all 4 new tests in tool_parse_visibility_test.zig red) and zig build test-tool-visibility-http-host --summary all failed (28 pass, 3 fail (31 total) — all 3 new tests in tool_prose_code_http_test.zig red). Mutation checks bypassing code.check per module failed as expected (zig build test-tool-parse-host: tool_parse.zig -> 80 pass, 2 fail; reply_text.zig -> 80 pass, 2 fail; tool_stream.zig -> 81 pass, 1 fail; tool_policy.zig -> 80 pass, 2 fail). Post-fix on 98f2d0df00dafa5c5c478ea6bdc1c3b325d83d55: zig build test-tool-parse-host test-tool-visibility-http-host test-thinking-tail-host --summary all (9/9 steps succeeded; 150/150 tests passed — 82/82, 31/31, 37/37), zig build test test-golden dev-tools -Dcpu=apple_m1 --summary all (1076/1076 steps succeeded; 14232/14350 tests passed (118 skipped); test-golden: 55/55 and 322/335 equal to server goldens, 0 unexpected differences; dev-tools built), zig build -Dcpu=apple_m1 --summary all (297/297 steps succeeded), zig build test-kernel-contract -Dcpu=apple_m1 --summary all (34/34 steps succeeded; 241/241 tests passed), zig fmt --check build.zig zig (exit 0), and git diff --check origin/main..HEAD (exit 0).
  • CI: pending first push of 98f2d0df00dafa5c5c478ea6bdc1c3b325d83d55 (not pushed yet; will update once CI runs).
  • Not run, and why: CUDA, HIP, XPU, Vulkan, Windows, app (npm), and live GPU inference benchmarks were not run locally because this host is macOS Apple Silicon (Metal host suite only) and the change only touches post-decode UTF-8 reply-text parsing and SSE filtering in zig/src/server/, which cannot reach inference kernels, weights, or KV caches.

Receipt

  • Main against branch: cells and numbers, or why they can't be run: n/a — post-decode reply-text parsing and SSE filtering in zig/src/server/ cannot reach model forward passes or inference cells.
  • Boxes it reaches: n/a — server HTTP/SSE text parsing only; does not reach engine, scheduler, or device kernel boxes.
  • New switch? Default, accepted values, profile row: n/a — no new switch or environment setting.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: base 077998f7d9b14adebec3a2acc2caa47ff7ae6ec7, head 98f2d0df00dafa5c5c478ea6bdc1c3b325d83d55, Zig 0.17.0, Apple M5 Max (48 GB, macOS 27.0.1) running host/Metal-host test targets (-Dcpu=apple_m1); checkpoint and revision: n/a (no weights loaded; unit and HTTP/golden host tests only).
  • Paths and platforms the change reaches, which I ran and which I could not: reaches tool_parse, reply_text, tool_stream, tool_policy, reply_tool_tail, tool_lfm2, logprob_parts, and chat_generation across non-streaming and SSE Chat Completions, Anthropic Messages, Responses, and Ollama chat formatting; ran on macOS Apple M5 Max (-Dcpu=apple_m1 / native host); did not run Linux, Windows, CUDA, HIP, XPU, or Metal M1–M4 hardware.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: n/a — cannot reach token generation, drafting, KV state, or prompt execution.
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): n/a — strictly post-decode text parsing in zig/src/server/ with no effect on generated tokens or engine throughput; parity with main is covered by zig build test test-golden -Dcpu=apple_m1.
  • Decode speed before and after, from tools/bench_openai.py: n/a — cannot reach token decode.
  • Prompt speed before and after: the cold first request's ttft_s from tools/bench_cache.py --require-cold on a long prompt, or "n/a" with the reason when the change cannot reach prompt processing: n/a — cannot reach prompt processing.
  • Licence of any new weights or drafter, or "n/a": n/a

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

ashhart added a commit that referenced this pull request Nov 17, 2026
ashhart added a commit that referenced this pull request Nov 17, 2026
…inted commit

Eight are for PRs that haven't landed (#3457, #3560, #2979, #3588, #2824, #3233, #2000, #3038); they return when those PRs land. The ninth repeated #2878's line, which is already in its release.
gilby and others added 2 commits November 17, 2026 09:32
…prose

Tool-call openers quoted inside Markdown inline code spans (`...`) or fenced code blocks (```...``` or ~~~...~~~) in generated assistant prose were treated as live call openers. Quoted `<tool_call>` tags paired with a following real call's `</tool_call>` and rejected it, quoted `<tool_call>NAME<arg_key>...` fragments naming an offered tool streamed bogus arguments, and complete quoted calls inside prose or code blocks executed as tool calls.

Add a linear-time Markdown code-span and fence scanner (reply_code.zig) and wire it into whole parsing (tool_parse.zig), streaming call deltas (tool_stream.zig), single-call prose filtering (tool_policy.zig), prose hiding and thinking-tail lifting (reply_text.zig, reply_tool_tail.zig), LFM2 rewrites (tool_lfm2.zig), logprob ranges (logprob_parts.zig), and EOS call closing (chat_generation.zig). While streaming, an inline backtick span containing a tool opener waits until a matching closer confirms code or a paragraph break or end proves it literal.

zig build test-tool-parse-host test-tool-visibility-http-host test-thinking-tail-host, zig build test test-golden dev-tools -Dcpu=apple_m1, zig fmt --check build.zig zig, and python3 tools/pr_check.py --base origin/main pass.
@ashhart
ashhart force-pushed the fix-prose-quoted-tool-calls branch from 98f2d0d to 483de20 Compare November 17, 2026 09:32
@ashhart
ashhart merged commit 9e76a98 into ashhart:main Nov 17, 2026
2 of 11 checks passed
@ashhart

ashhart commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

outcastofmusic added a commit to outcastofmusic/TensorFold that referenced this pull request Nov 25, 2026
…ashhart#3588)

A reply that showed a call in backticks or a code fence ran it as a live call, and the stream hid it.
code_spans.py scans CommonMark inline spans and fences in linear time, as upstream's reply_code.zig
does. The whole parsers (Qwen XML, namespaced, Gemma, DSML, LFM2's list), the end-token closer, the
streamed deltas, the stream's prose hider, the one-call filter and the thinking split skip a quoted
opener. While streaming, an opener that may still turn out quoted waits. ThinkCall leaves an opener
quoted in the thinking where it is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012882unKVWq5Ve1jHg3xizt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants