ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

server: extend reasoning-delta token-boundary fix to Responses and Anthropic

Open Flor1an-B opened this issue 3 months ago • 0 comments

Complements #692 (@rareba) — extends the same fix to the two other paths issue #685 explicitly flagged as having the identical bug, which #692 doesn't touch.

Background

Issue #685 reports streaming reasoning deltas split at an arbitrary fixed byte offset (7 bytes before the end, held back to detect a possibly-split </think> marker) instead of at actual token boundaries — across three independently-shaped but identical code paths: OpenAI chat/completions, Responses, and Anthropic. #692 already fixes the OpenAI path (its diff is scoped to openai_sse_stream_update() only): track the end of the previous update's text (reasoning_token_end) and use that as the emit boundary, since production code calls *_sse_stream_update() once per newly decoded token, so the boundary between two calls is always a real token edge.

This PR applies the exact same technique, 1:1, to responses_sse_stream_update() and anthropic_sse_stream_update() — the two other places #685 pointed at (~line 7188 and ~line 8098 respectively). Not a competing fix for the OpenAI path; doesn't touch it at all.

Changes

In both responses_stream and anthropic_stream:

  • add reasoning_token_end
  • set it to raw_len at each point the THINKING branch returns without a close tag or finish (the prefix-check early return, the tool-seen-but-incomplete return, and the plain "still streaming" return)
  • use it instead of the fixed "</think>") - 1 holdback when computing the emit limit

Testing

  • Full existing ds4_test suite passes unchanged.
  • New test_anthropic_stream_preserves_reasoning_token_boundaries and test_responses_stream_preserves_reasoning_token_boundaries, mirroring #692's own test structure: three *_sse_stream_update() calls simulating three decode steps assert reasoning deltas land on the actual per-call text growth ("We need" / " to generate a title"), not a fixed mid-word suffix cut, for both paths.

🤖 Generated with Claude Code

Flor1an-B avatar Aug 05 '26 18:08 Flor1an-B