server: don't stop generation inside a thinking block (and warn when it happens) - #681
Open
alangiu-gif wants to merge 2 commits into
Open
server: don't stop generation inside a thinking block (and warn when it happens)#681alangiu-gif wants to merge 2 commits into
alangiu-gif wants to merge 2 commits into
Conversation
Stopping generation in the middle of a thinking block does more damage than
returning a truncated reply: it silently poisons the session KV cache.
When the block never closes, `thinking_live` never becomes valid, so the
checkpoint is keyed on the raw token-text. That text embeds the hidden
reasoning the client never replays, so the stored prefix can never be a prefix
of a future request: the file is born dead. With disk checkpoints enabled this
writes hundreds of MiB on every session switch that are never read back, and
every return to a session restarts from the older cold checkpoint instead.
With --thinking-grace-tokens N, when the budget runs out while the block is
still open, generation is granted N extra tokens once, always within the
remaining context room, and the event is logged. Default 0 keeps the current
behaviour unchanged.
Measured on a MacBook Pro M4 Max 128 GB (macOS 26.6, Metal backend) with
DeepSeek-V4-Flash q2 (IQ2XXS), two ~21k-token conversations alternating through
a single-slot server with --kv-disk-dir:
max_tokens=48, no grace : 8/8 finish=length, 15 stores key=token-text,
0 key=thinking-visible, every hit falling back to
the cold checkpoint at 20480 tokens
max_tokens=48, grace=2000 : 7/8 finish=stop, 13 stores key=thinking-visible,
hits landing on the fresh frontiers
(stored 21507 -> hit 21507)
The grace fired 6 times, always at completion=48.
./ds4_test --server passes, but it does not exercise this path: generate_job is
not compiled into the DS4_SERVER_TEST build.
Until now the condition was only visible indirectly, as key=token-text instead of key=thinking-visible in the KV cache log. The client gets a reply cut mid-reasoning and, less obviously, the session checkpoint is born dead: no client replay can ever match a stored prefix that embeds hidden reasoning. Log the finish reason, completion, max_tokens and the grace state (off | unused | exhausted) together with the remedy, both on the server log and on --trace.
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.
The failure mode
When generation stops in the middle of a thinking block, the truncated reply is the
visible half of the problem. The invisible half is that it poisons the session KV cache.
If the block never closes,
thinking_livenever becomes valid, sokv_cache_store_current()falls back to keying the checkpoint on the raw token-text.That text embeds the hidden reasoning the client never replays, so the stored prefix can
never be a prefix of a future request. The checkpoint is born dead.
With
--kv-disk-direnabled and a client that switches between sessions, this meanshundreds of MiB written on every switch that are never read back, while every return to a
session keeps restarting from the older cold checkpoint. Nothing in the logs says so: the
only hint is
key=token-textwhere you would expectkey=thinking-visible.It is easy to hit by accident. Any client that sets a tight
max_tokensagainst athinking model does it on every request.
Evidence
MacBook Pro M4 Max 128 GB, macOS 26.6, Metal backend, DeepSeek-V4-Flash q2 (IQ2XXS),
single-slot server with
--kv-disk-dir, two ~21k-token conversations alternated fourtimes through the OpenAI-compatible endpoint.
max_tokens=48, no gracemax_tokens=48,--thinking-grace-tokens 2000finish=stopfinish=lengthkey=thinking-visiblekey=token-textThe grace fired 6 times, always at
completion=48, i.e. exactly when the budget ran outwith the block still open.
For reference, with
max_tokens=600(enough for the model to close the block on its own)the problem does not appear at all — which is what makes it easy to miss.
The two changes
--thinking-grace-tokens N: when the budget runs out while the block is still open,grant N extra tokens once, always bounded by the remaining context room, and log
it. Default
0keeps today's behaviour byte for byte.completion,
max_tokensand the grace state (off/unused/exhausted), on theserver log and on
--trace. This is the signal that was missing.The first is a mitigation, not a guarantee: a reasoning block longer than the grace still
ends truncated (1 case out of 8 above). Closing that gap would mean injecting
</think>when the grace is exhausted, reusing the recovery path already used when a tool marker
appears inside a thinking block. Happy to add it if you prefer that shape.
Testing
Note:
./ds4_test --serverpasses but does not exercise this path —generate_jobisnot compiled into the
DS4_SERVER_TESTbuild. The evidence above comes from the runningserver.
No speed regression is expected: with the default
0the added code is one boolean testper generated token, and the new warning runs once per request.