gateway: a Claude thinking progress beat reaches the client with its count (#5390)
During a long think Claude streams thinking_delta events that say nothing and carry estimated_tokens: its live progress beat. Both decoders (the Claude subscription's readOutput and decodeAnthropic) read only the text, and anthropicEncoder skipped a thinking delta with no text, so Claude Code heard nothing but keepalives for the whole think and never showed "thinking ... with xhigh effort". The count now rides the Event (Estimated). An empty thinking delta that carries one opens the thinking block the way a spoken delta does and goes to the client as it came; one without a count is still skipped (#1445). A delta with text carries its count too. On a direct request (last candidate, thinkingShown) the block's content_block_start is eventThinking, so the stream flows at the first beat; on a group still holding reasoning for a refusal handover (#248) the beat is held with the rest, as text thinking is. Run (mote, -tags nogui): - TestThinkingProgressTokensReachTheClient, TestBareEmptyThinkingDeltaStaysQuiet, TestAnthropicDecoderKeepsProgressCount: ok. - Broken by hand: anthropic.go and claude_subscription.go put back to origin/main, tests kept: TestThinkingProgressTokensReachTheClient FAIL ("the client never heard the progress beat "estimated_tokens":50"), TestAnthropicDecoderKeepsProgressCount FAIL; the bare-empty test passes on both (it pins #1445). - -run 'Thinking|Anthropic|TestClaudeCutStream|TestStreamOutputCounted|TestTurn': ok. - go test -timeout 45m ./internal/gateway/: ok 862s. GOOS=linux and GOOS=windows go vet ./internal/gateway/: clean. gofmt -l: nothing. Not tried: a real Claude subscription during a long think (the beat's shape is the reporter's TUI tap), and Claude Code's own display of the beat. Reported by dusk2999 (#5390). Patch by ggbdpq; the decoder test is ours. Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
@@ -1310,12 +1310,13 @@ func decodeAnthropic(data string, emit func(Event)) error {
|
||||
Signature string `json:"signature"`
|
||||
} `json:"content_block"`
|
||||
Delta struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
PartialJSON string `json:"partial_json"`
|
||||
Thinking string `json:"thinking"`
|
||||
Signature string `json:"signature"`
|
||||
StopReason string `json:"stop_reason"`
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
PartialJSON string `json:"partial_json"`
|
||||
Thinking string `json:"thinking"`
|
||||
EstimatedTokens *int `json:"estimated_tokens"`
|
||||
Signature string `json:"signature"`
|
||||
StopReason string `json:"stop_reason"`
|
||||
// auto mode's review of the reply's calls (automode.go)
|
||||
SafeguardResults json.RawMessage `json:"safeguard_results"`
|
||||
StopDetails json.RawMessage `json:"stop_details"`
|
||||
@@ -1371,7 +1372,7 @@ func decodeAnthropic(data string, emit func(Event)) error {
|
||||
case "input_json_delta":
|
||||
emit(Event{Kind: KToolArgs, Text: ev.Delta.PartialJSON})
|
||||
case "thinking_delta":
|
||||
emit(Event{Kind: KThink, Text: ev.Delta.Thinking})
|
||||
emit(Event{Kind: KThink, Text: ev.Delta.Thinking, Estimated: ev.Delta.EstimatedTokens})
|
||||
case "signature_delta":
|
||||
emit(Event{Kind: KSig, Name: sealAnthropic, Text: ev.Delta.Signature})
|
||||
}
|
||||
@@ -1666,10 +1667,22 @@ func (e *anthropicEncoder) event(ev Event) {
|
||||
return
|
||||
case KThink:
|
||||
if ev.Text == "" {
|
||||
if ev.Estimated == nil {
|
||||
return
|
||||
}
|
||||
// an empty thinking delta that carries the vendor's progress
|
||||
// count goes to the client as it came: the live beat that says
|
||||
// the think is moving, not silence (#5390)
|
||||
e.thinking()
|
||||
e.delta(map[string]any{"type": "thinking_delta", "thinking": "", "estimated_tokens": *ev.Estimated})
|
||||
return
|
||||
}
|
||||
e.thinking()
|
||||
e.delta(map[string]any{"type": "thinking_delta", "thinking": ev.Text})
|
||||
d := map[string]any{"type": "thinking_delta", "thinking": ev.Text}
|
||||
if ev.Estimated != nil {
|
||||
d["estimated_tokens"] = *ev.Estimated
|
||||
}
|
||||
e.delta(d)
|
||||
case KSig:
|
||||
// a block signed with no text goes as it came, signed (#1445)
|
||||
if e.open == Thinking || e.fresh {
|
||||
|
||||
@@ -3873,6 +3873,7 @@ func (r *subscriptionRun) readOutput(rd io.Reader) {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
Thinking string `json:"thinking"`
|
||||
EstimatedTokens *int `json:"estimated_tokens"`
|
||||
Signature string `json:"signature"`
|
||||
PartialJSON string `json:"partial_json"`
|
||||
StopReason string `json:"stop_reason"`
|
||||
@@ -4184,7 +4185,7 @@ func (r *subscriptionRun) readOutput(rd io.Reader) {
|
||||
case "text_delta":
|
||||
said(e.Delta.Text)
|
||||
case "thinking_delta":
|
||||
r.emit(Event{Kind: KThink, Text: e.Delta.Thinking})
|
||||
r.emit(Event{Kind: KThink, Text: e.Delta.Thinking, Estimated: e.Delta.EstimatedTokens})
|
||||
case "signature_delta":
|
||||
r.emit(Event{Kind: KSig, Name: sealAnthropic, Text: e.Delta.Signature})
|
||||
case "input_json_delta":
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// progressStart opens a thinking block the way a Claude subscription run
|
||||
// does, and progressDelta is the vendor's live beat during a long think:
|
||||
// a thinking_delta that says nothing but carries estimated_tokens (#5390).
|
||||
var progressStart = []string{
|
||||
`{"type":"stream_event","event":{"type":"message_start","message":{"id":"msg_prog","model":"claude-opus-5-5","usage":{"input_tokens":2,"output_tokens":1}}}}`,
|
||||
`{"type":"stream_event","event":{"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":"","signature":""}}}`,
|
||||
}
|
||||
|
||||
func progressDelta(estimated int) string {
|
||||
return `{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"","estimated_tokens":` + strconv.Itoa(estimated) + `}}}`
|
||||
}
|
||||
|
||||
// TestThinkingProgressTokensReachTheClient: an empty thinking_delta that
|
||||
// carries estimated_tokens is the vendor's live progress beat; the client
|
||||
// gets it as it came instead of hearing nothing during a long think.
|
||||
func TestThinkingProgressTokensReachTheClient(t *testing.T) {
|
||||
run := &subscriptionRun{}
|
||||
lines := append([]string{}, progressStart...)
|
||||
lines = append(lines,
|
||||
progressDelta(50),
|
||||
progressDelta(120),
|
||||
`{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"so far, so good. "}}}`,
|
||||
`{"type":"stream_event","event":{"type":"content_block_stop","index":0}}`,
|
||||
`{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":777}}}`,
|
||||
`{"type":"stream_event","event":{"type":"message_stop"}}`,
|
||||
`{"type":"result","subtype":"success","is_error":false,"result":""}`,
|
||||
)
|
||||
_, _, _, _, raw, _, ok := claudeReply(t, run, lines, true)
|
||||
if !ok {
|
||||
t.Fatalf("the run did not end cleanly")
|
||||
}
|
||||
for _, beat := range []string{`"estimated_tokens":50`, `"estimated_tokens":120`} {
|
||||
if !strings.Contains(raw, beat) {
|
||||
t.Errorf("the client never heard the progress beat %s; got:\n%s", beat, raw)
|
||||
}
|
||||
}
|
||||
for _, line := range strings.Split(raw, "\n") {
|
||||
if strings.Contains(line, "thinking_delta") && strings.Contains(line, `"thinking":""`) && !strings.Contains(line, "estimated_tokens") {
|
||||
t.Errorf("a progress beat went out without its count: %s", line)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(raw, `"thinking":"so far, so good. "`) {
|
||||
t.Errorf("the real thinking text is missing; got:\n%s", raw)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBareEmptyThinkingDeltaStaysQuiet: an empty thinking_delta that
|
||||
// carries no progress count stays skipped (#1445) — no empty deltas.
|
||||
func TestBareEmptyThinkingDeltaStaysQuiet(t *testing.T) {
|
||||
run := &subscriptionRun{}
|
||||
lines := append([]string{}, progressStart...)
|
||||
lines = append(lines,
|
||||
`{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":""}}}`,
|
||||
`{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"words. "}}}`,
|
||||
`{"type":"stream_event","event":{"type":"content_block_stop","index":0}}`,
|
||||
`{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":777}}}`,
|
||||
`{"type":"stream_event","event":{"type":"message_stop"}}`,
|
||||
`{"type":"result","subtype":"success","is_error":false,"result":""}`,
|
||||
)
|
||||
_, _, _, _, raw, _, ok := claudeReply(t, run, lines, true)
|
||||
if !ok {
|
||||
t.Fatalf("the run did not end cleanly")
|
||||
}
|
||||
for _, line := range strings.Split(raw, "\n") {
|
||||
if !strings.Contains(line, "thinking_delta") {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(line, `"thinking":""`) {
|
||||
t.Errorf("a bare empty thinking delta reached the client: %s", line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestAnthropicDecoderKeepsProgressCount: the API leg's decoder carries
|
||||
// the same beat's count, so a stream from Anthropic's own API keeps it on
|
||||
// the way to the client too (#5390).
|
||||
func TestAnthropicDecoderKeepsProgressCount(t *testing.T) {
|
||||
var got []Event
|
||||
data := `{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"","estimated_tokens":50}}`
|
||||
if err := decodeAnthropic(data, func(ev Event) { got = append(got, ev) }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Kind != KThink || got[0].Estimated == nil || *got[0].Estimated != 50 {
|
||||
t.Fatalf("decoded %+v, want one KThink carrying estimated_tokens 50", got)
|
||||
}
|
||||
}
|
||||
@@ -445,6 +445,10 @@ type Event struct {
|
||||
// StopDetails: Anthropic's stop_details, what its classifier said of
|
||||
// a refusal (#1768), carried to the client as it came
|
||||
StopDetails json.RawMessage
|
||||
// Estimated: the vendor's estimated_tokens on an empty thinking_delta,
|
||||
// its live progress beat during a long think (#5390); nil when the
|
||||
// delta carried none
|
||||
Estimated *int
|
||||
|
||||
// Wait: for KError, how long the vendor said to wait before asking
|
||||
// again, when it said; sent on as Retry-After
|
||||
|
||||
Reference in New Issue
Block a user