GLM tool turns clear the live checkpoint, so every follow-up turn rebuilds its prefix
Hi Salvatore,
while running a long agent session against glm5_next (GLM-5.3-Flash, Q2 resident, ctx 393216, MTP on)
I noticed that every follow-up turn after a tool call rebuilds its prompt prefix from an older disk
block instead of continuing the live KV. On this machine that is ~30k tokens of prefill (~100 s) per
follow-up turn.
What the log shows (one server, one client session)
12:32:48 chat ctx=50681..50747:66 gen=92 TOOLS DSML_START DSML_END finish=tool_calls 7.054s
12:57:50 GLM live prefix rewind from 50839 to 50746 requires rebuild
12:57:50 kv cache skipped tokens=50746 reason=evict because KV payload staging failed: session has no valid checkpoint to stage
12:57:51 kv cache hit text tokens=20536 ... load=563.0 ms
12:57:51 chat ctx=20536..50747:30211 TOOLS prompt start <- 30211 tokens re-prefilled
Three observations:
- The follow-up request is 92 tokens shorter than the live frontier (50839 -> 50747): the client
(Hermes desktop) does not send the generated tool-call text back as reasoning. That is normal for
an OpenAI-style chat client — it is exactly the case the Qwen path was built for
(
build_qwen_tool_turn_visible_text, "Match clients that omit reasoning"). - The rewind cannot rescue it:
ds4_session_glm_mtp_rewind()only covers the two-token MTP cycle, sods4_session_rewind()finishes withstate_ok == false, clearscheckpoint_validand the caller rebuilds. That looks correct for the compressed DSA KV — truncation is not a valid rollback. - Therefore the only thing that can keep the live prefix usable is a visible checkpoint — and for GLM it is never set.
Root cause
should_remember_thinking_checkpoint():
const bool qwen_chat = r->model_syntax == SERVER_MODEL_SYNTAX_QWEN &&
r->api != API_RESPONSES && r->api != API_ANTHROPIC;
if ((r->has_tools || r->prompt_preserves_reasoning) && !qwen_chat) return false;
Qwen is exempted here, GLM is not. At the call site in the tool-call branch:
} else if (parsed_calls.len) {
if (!remember_qwen_tool_turn_visible_checkpoint(...)) /* hard Qwen-only check -> false */
{
thinking_live_clear(s, slot); /* live checkpoint is dropped */
}
}
For a GLM chat request with tools and known raw DSML, should_canonicalize_tool_checkpoint()
also returns false (raw_tool_text is known -> exact DSML replay is preferred), so no checkpoint is
written on any path. The live checkpoint is cleared instead, the disk staging then fails with
"session has no valid checkpoint to stage", no block at ~50k tokens is stored, and the next request
falls back to the newest older block that still matches.
Proposed fix
Mirror the Qwen behaviour for GLM: remember the visible key (prompt + closed think block + the
tool-call text) using the existing build_thinking_visible_text(), which already has a GLM branch.
The next request renders that same visible surface, thinking_live_visible_prefix_prompt() matches it
and the live KV is reused.
My branch adds glm_tool_turn_checkpoint_eligible() — eligibility mirrors what
build_thinking_visible_text() requires: chat completions, SERVER_MODEL_SYNTAX_GLM, not
Responses/Anthropic, finish == "tool_calls" (decode finish, not the promoted parser status), not
inside thinking, non-empty tool text, and a prompt that still ends with the open <think> marker —
and calls remember_thinking_checkpoint() in the tool-call branch when the Qwen helper declines.
Unit test test_glm_tool_turn_checkpoint_eligible() covers the positive case plus the rejections
(finish != tool_calls, inside thinking, empty content, no finish, Qwen syntax, Responses API, prompt
without the open marker, NULL request). ./ds4_test --server passes on the branch.
Status: unit-tested, live before/after not measured yet — the production server was busy with a live agent run while I prepared this, so I did not restart it. I can report the measured numbers (visible checkpoint remembered + follow-up prefill tokens) once it has run, if that helps before review.
Related, smaller observation
On a client cancel during prefill, kv_cache_discard_failed_disk_entry() unlinks the disk entry and
calls ds4_session_invalidate(). The entry itself was intact, so the next attempt starts from an
older block than necessary. I left that alone — the defensive delete looks deliberate.
Short status update after testing the branch on the host (GLM-5.3-Flash Q2, ctx 393216):
The change works at the code level. A probe with a tool-schema request now reaches the new branch and stores the checkpoint, where before it was cleared:
DIAG tool-turn branch: finish=tool_calls inside_thinking=0 content_len=721 reasoning_len=90 calls=1 syntax=1
thinking live checkpoint remembered ctx=1907..1976:69 live=2237 visible=8754
One correction that mattered: on a GLM tool turn parsed_content is empty (the DSML call is consumed out of the content), so the visible key has to be built from parsed_calls.raw_tool_text. The branch does that now.
What is still unproven: the end-to-end saving. In my probe the follow-up turn was incremental before the change as well, because the simulated client replays the whole turn — the plain token match reaches the frontier and the visible key is not needed. The configuration this fix targets (client omits the reasoning, so the prompt is strictly shorter than the live frontier and no token match exists) still needs a clean before/after measurement with a reasoning-heavy turn. I will report that number here; the PR stays a draft until then.
Measured the A/B on the host today (PR branch vs. production binary), same probe, KV cache cleared before each arm. Request 1: tool-schema turn that generates a reasoning block. Request 2: replays the turn without the reasoning plus a tool result.
| without the patch | with the patch | |
|---|---|---|
| after the tool turn | live kv cache miss … common=6041 |
thinking live checkpoint remembered … live=6997 |
| follow-up prefill | 765 tokens (4.2 s), from disk block 5897 | 84 tokens (1.2 s) |
| cache source | older disk block | live KV (visible prefix) |
common stops at the prompt end without the patch: the reasoning block sits in the live KV but not in the follow-up prompt, so there is no plain token match and the compressed DSA KV cannot be truncated back — the parser falls back to an older disk block. With the checkpoint the live frontier is reused.
Caveat, so the numbers are not read as more than they are: the two arms generated different amounts of reasoning (7497 vs 1537 characters), so this is one sample per arm. The mechanism is what the measurement shows — the saving scales with the reasoning the client omits, and in production that was the difference between a 50 747-token frontier and a 20 536-token block (~30k tokens / ~100 s) when no block near the frontier existed.