Skip to content

tool_parse: a GLM call's keys without values read in linear time - #883

Merged
ashhart merged 1 commit into
ashhart:mainfrom
jishnuvenugopal:fix/tool-parse-glm-keys
Oct 16, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
jishnuvenugopal:fix/tool-parse-glm-keys

Conversation

@jishnuvenugopal

Copy link
Copy Markdown
Contributor

What this changes

glmCall (zig/src/server/tool_parse.zig) pairs each <arg_key> with the first </arg_key> that an <arg_value>
follows, and walked the later </arg_key> tags again for every <arg_key>. valueKey finds that </arg_key>, and
glmCall keeps it, and the first </arg_value> after it, until it has read past them. The keys only move forward,
so the pairs are the same.

Verified outcome

  • Why: a GLM-format tool call whose keys have no values slowed down with the square of its size: about 2.4 s to
    read at 128 KiB, whether the call was closed or the reply's end left it open.
  • What was done: matching each key to its value remembers the next closing tag, instead of scanning for it again
    for every key.
  • Result: the same calls read in about 3 ms, closed or open. Output is unchanged: 50,000 varied replies give
    byte-identical results.

Receipt

  • Environment: main at c7137bb, Zig 0.17.0, Apple M4, macOS 27.0.1. Host code: no model or GPU.

  • Exactness: n/a. No engine change; the reply's tokens and text are the same.

  • Decode speed before and after: n/a. bench_openai.py's prompts offer no tools, and only replies to requests that
    offer tools are parsed.

  • Prompt speed before and after: n/a, for the same reason.

  • Speed on <tool_call>lookup then <arg_key>n</arg_key> repeated, in ReleaseSafe as the server ships, fastest of
    three, c7137bb then this branch:

    Reader 16 KiB 128 KiB
    parse, </tool_call> after the keys 37 ms, 0.38 ms 2.36 s, 3.1 ms
    closeCall, the call left open 35 ms, 0.40 ms 2.34 s, 3.2 ms
  • Same output: a local fuzz harness (not part of this change) ran 50,000 edited replies through parse, closeCall,
    hideToolCalls and finishedProse on c7137bb and on this branch. A digest of everything they returned, call ids
    left out, is equal (4c8dc88dffd56f19).

  • Tests run:

    • New, a GLM call's keys without values read in linear time (tool_parse_test.zig): one call with 800 keys and
      one with 6,400. 8x the keys may take at most 24x the time (or under 20 ms), the bound fuzz-cortheon's
      linear-time test uses. It fails on c7137bb and passes on this branch.
    • zig build test test-golden -Dcpu=apple_m1, uncached: 164 of 164 steps, 1982 of 1990 tests pass, 8 skip,
      0 fail (c7137bb: 1981 of 1989, 8 skip); the one more is the new test. zig build fuzz-cortheon passes.
      zig fmt --check build.zig zig is clean. Lean: 527 problems on the base and 527 on this branch, 0 new
      (lean_new.py).
  • Not run, and why: no CUDA host here. The parser is server code that both backends use, and its tests need no GPU.

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off. (No engine
    change.)
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (No
    engine change; not re-run.)
  • No precision traded for speed. If the bits change, the description says which sums change and why. (No
    arithmetic changes.)
  • Prompt processing is no slower than the last release. (n/a, as above.)
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • 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. No AI attribution lines.

glmCall walked the later </arg_key> tags again for every <arg_key>. valueKey finds the first </arg_key> an
<arg_value> follows, and glmCall keeps it, and the first </arg_value> after it, until it has read past them.
@ashhart

ashhart commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Thank you, @jishnuvenugopal, for covering the key-only GLM call and its unchanged empty-object result.

At 904e2bd, zig/src/server/tool_parse.zig:430 retains the same first key closer followed by a value opener, and line 436 advances the cached value closer only when needed. The selected slices and residue advancement match the old loop, including absent closers and permissive key-only calls.

This is ready for parser integration checks alongside #881 and #882. The three heads are siblings, so the landing batch needs all three changes applied together.

Posted by Ash's AI Assistant Synthena

@ashhart
ashhart merged commit 801aa0f into ashhart:main Oct 16, 2026
ashhart added a commit that referenced this pull request Oct 16, 2026
… against the old search

#881's putBack is exact only for replacements without a NUL, which json.quote guarantees for its one caller; its
doc line now says so, and the sort line says the cost: one pass over the text, then M log M for M marks. The two
timing tests from #882 and #883 now name what they measure (each opener, and each key and value end, searched
once) instead of claiming that all tool parsing is linear. A new test parses 20,000 random GLM calls (keys without
values, values without closers, repeated keys, stray tags, whitespace) and checks each against the same pairs
found by #883's parent loop, written out plainly: the arguments must be identical.
ashhart added a commit that referenced this pull request Oct 16, 2026
…VIEW names the refused GLM pair requests rightly

One line each for #881, #882, #883, #889, #890, #709, #848, #892, #867, #868, #871 and #873, crediting their
authors. ZIG-PREVIEW said a two-Mac GLM pair refuses "temperature-1" requests; it refuses any temperature above 0
(backend.zig and engine.zig both test temperature > 0).
@ashhart

ashhart commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Thank you, @jishnuvenugopal, for eliminating repeated key-end and value-end searches in GLM calls.

#883 is included in 1.0.12 as merge 801aa0f, at 904e2bd.

Our 084406f adds a 20,000-case comparison against the parent's loop, covering missing values and closers, repeated keys, stray tags and whitespace. The arguments must match byte for byte. The timing test also names the operation it measures.

The comparison passes inside the full suites: macOS has 2,968 of 2,978 tests passing with 10 skipped, and Linux 1,368 of 1,375 with 7 skipped.

Posted by Ash's AI Assistant Synthena

@jishnuvenugopal
jishnuvenugopal deleted the fix/tool-parse-glm-keys branch October 17, 2026 03:06
outcastofmusic added a commit to outcastofmusic/TensorFold that referenced this pull request Oct 17, 2026
The argument regex's lazy key retried every later </arg_key> from every
<arg_key>, so a call whose keys have no values took 6.1 s for findall and
6.7 s for sub at 8,000 keys (191k characters); the parser runs both. A
forward-only scan returns the same pairs and leftover text: equal to the
regex on 20,000 random calls. Ports upstream ashhart#883.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
… against the old search

ashhart#881's putBack is exact only for replacements without a NUL, which json.quote guarantees for its one caller; its
doc line now says so, and the sort line says the cost: one pass over the text, then M log M for M marks. The two
timing tests from ashhart#882 and ashhart#883 now name what they measure (each opener, and each key and value end, searched
once) instead of claiming that all tool parsing is linear. A new test parses 20,000 random GLM calls (keys without
values, values without closers, repeated keys, stray tags, whitespace) and checks each against the same pairs
found by ashhart#883's parent loop, written out plainly: the arguments must be identical.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…VIEW names the refused GLM pair requests rightly

One line each for ashhart#881, ashhart#882, ashhart#883, ashhart#889, ashhart#890, ashhart#709, ashhart#848, ashhart#892, ashhart#867, ashhart#868, ashhart#871 and ashhart#873, crediting their
authors. ZIG-PREVIEW said a two-Mac GLM pair refuses "temperature-1" requests; it refuses any temperature above 0
(backend.zig and engine.zig both test temperature > 0).
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