Preserve live KV cache across normalized tool replay - #985
Open
rratmansky wants to merge 1 commit into
Open
Conversation
Author
|
Manual end-to-end validation on Apple M5 Max / Metal with
Representative trace entry: The stale ordinary token common-prefix position while the full live frontier was reused confirms that the new visible-transcript path—not an incidental exact-token hit—provided the speedup. One boundary remains: after a tool-enabled turn finished with a normal final answer rather than a tool call, a later user turn cold-missed. This patch records successful tool-call frontiers, so it fixes the repeated tool-call/tool-result failure mode but does not claim to cover every non-tool final-answer transition. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Preserve live KV cache across normalized tool replay
Summary
Preserve the resident KV checkpoint when a Chat Completions client replays a successful tool-call turn with semantically equivalent but byte-normalized assistant text.
The server already uses a remembered visible transcript to continue from richer live state after tool-less thinking. This extends that existing mechanism to successful tool-call turns:
The visible continuation check now runs before GLM token rewind. Requests that do not match still use the existing token, rendered-text, disk-cache, or cold-prefill paths.
Motivation
A long GLM-5.3 agent session repeatedly lost its entire resident cache because parse-and-replay normalized one trailing newline immediately before a tool call:
Although the visible conversation was the same and more than 99.9% of the token history agreed, the request prefetched all 161,124 tokens from zero. Cache misses at this context took roughly 420–525 seconds; a neighboring successful resident-cache hit completed in about 14 seconds.
This is particularly painful for tool-using agents because every tool result immediately triggers another request.
Implementation
cache_source=chat-visibleand logvisible chat continuation match=visible-prefix.Responses API behavior is unchanged; it retains its existing protocol-specific continuation mechanism.
Safety
This does not perform an arbitrary token rewind and does not modify GLM KDA/indexer state.
The sampled live KV state is reused only when the next request exactly extends the remembered client-visible transcript at the same live frontier. Edited histories, unrelated branches, stale slots, non-extending prompts, failed/length-truncated tool turns, and image-identity mismatches continue through the existing fallback paths.
The implementation uses the existing syntax-aware tool renderer, so it covers both DeepSeek DSML and GLM
<tool_call>replay.Related work
This is a narrowly scoped, current-
mainimplementation of the visible-checkpoint approach discussed in several existing reports and PRs:reasoning_content; this PR was validated with a client that replays parsed reasoning.tool_call_id, an alternative protocol-specific solution to the same repeated-prefill symptom.Related broader reports include #691, #816, #781, and #364.
Tests
Machine and test configuration:
ds4flash.gguf->GLM-5.3-Flash-Q2.ggufCommands run:
Results:
The server tests include:
"):\n"->"):"tool-turn replay;A complete
./ds4_test --allrun was also attempted with the local GLM-5.3 Q2 model. Its server, tool-call quality, long-context, GLM continued-prefill, and Metal kernel sections passed. It reported 20 model/golden-vector discrepancies in logprob vectors, SSD-streaming vectors, a local golden vector, and long-prompt Metal tensor equivalence. Those checks compare the locally configured GLM model against existing expected vectors and are outside this server-only change; they are reported here as notable failures perCONTRIBUTING.md.