diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 9c048638a..b15f0ab6d 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -1163,7 +1163,13 @@ history to the 1h cache at 2×. after the last reply that calls no tool, `historyEnd`) in `` (#1365). Told as one stretch of Human:/Assistant: text, earlier images read as just sent. A first turn, or messages with no - reply among them, are told as before. `start` logs "a new Claude Code + reply among them, are told as before. A reply's thinking and + redacted_thinking are never told as words, here, in the messages since + a reply (`renderClaudeTurn`) or in a resumed transcript + (`renderCachedParts`): written as the assistant's text, earlier + reasoning reached the model in a user message, Anthropic refused it as + `reasoning_extraction`, and every run started anew told it again + (#6623). `start` logs "a new Claude Code is told the whole conversation" with the message and image counts. Claude Code notes every image of the message after it, so after the turn to answer: `[Image: source: …/images/N.png]` where it keeps the diff --git a/internal/gateway/claude_replay_thinking_test.go b/internal/gateway/claude_replay_thinking_test.go new file mode 100644 index 000000000..393eeda3d --- /dev/null +++ b/internal/gateway/claude_replay_thinking_test.go @@ -0,0 +1,79 @@ +package gateway + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// A conversation told to a Claude Code run in words leaves the replies' +// thinking out. Written into the history as the assistant's text, earlier +// thinking reached the model as words in a user message, which Anthropic +// refused as reasoning extraction, and every run started anew after the +// refusal told it again (#6623; solllin on X). The words, tool calls and +// tool results stay. +func TestClaudeReplayLeavesThinkingOut(t *testing.T) { + const secret = "PRIVATE-CHAIN-OF-THOUGHT-6623" + const sealed = "U0VBTEVELVJFREFDVEVELTY2MjM=" + conv := `[` + + `{"role":"user","content":"fix the bug"},` + + `{"role":"assistant","content":[{"type":"thinking","thinking":"` + secret + ` one","signature":"sig1"},{"type":"redacted_thinking","data":"` + sealed + `"},{"type":"text","text":"reading the file"},{"type":"tool_use","id":"call_1","name":"read","input":{"path":"a.go"}}]},` + + `{"role":"user","content":[{"type":"tool_result","tool_use_id":"call_1","content":"package a"}]},` + + `{"role":"assistant","content":[{"type":"thinking","thinking":"` + secret + ` two","signature":"sig2"},{"type":"text","text":"fixed it"}]},` + + `{"role":"user","content":"now add a test"}]` + fields := `"tools":[{"name":"read","input_schema":{"type":"object"}}],` + told := func(t *testing.T, blocks []map[string]any) string { + t.Helper() + var all strings.Builder + for _, b := range blocks { + s, _ := b["text"].(string) + all.WriteString(s) + all.WriteString("\n") + } + return all.String() + } + check := func(t *testing.T, s string, want ...string) { + t.Helper() + if strings.Contains(s, secret) || strings.Contains(s, sealed) { + t.Fatalf("the replay carries the replies' thinking:\n%s", s) + } + for _, w := range want { + if !strings.Contains(s, w) { + t.Fatalf("the replay lost %q:\n%s", w, s) + } + } + } + for _, marks := range []bool{false, true} { + req := clientCacheRequest(t, fields, conv) + if !marks { + req.clientCache = nil + } + blocks, err := renderClaudePrompt(req) + if err != nil { + t.Fatal(err) + } + check(t, told(t, blocks), "fix the bug", "reading the file", "[tool call", "call_1", "package a", "fixed it", "now add a test") + } + + // the messages since a reply, told to a run that has the conversation + // (the -p run's next message, and the interactive run's typed turn) + req := clientCacheRequest(t, fields, conv) + check(t, told(t, renderClaudeTurn(req.Messages[1:], req.Tools)), "reading the file", "[tool call", "package a", "fixed it", "now add a test") + + // the transcript a new interactive run resumes + dir := t.TempDir() + long := clientCacheRequest(t, fields, strings.TrimSuffix(conv, "]")+`,{"role":"assistant","content":[{"type":"thinking","thinking":"`+secret+` three","signature":"sig3"},{"type":"text","text":"added"}]},{"role":"user","content":"thanks"}]`) + if replaySession([]string{dir}, dir, "claude-sonnet-5", long) == nil { + t.Fatal("no transcript was written") + } + files, _ := filepath.Glob(filepath.Join(dir, "*.jsonl")) + if len(files) != 1 { + t.Fatalf("%d transcripts", len(files)) + } + b, err := os.ReadFile(files[0]) + if err != nil { + t.Fatal(err) + } + check(t, string(b), "fix the bug", "reading the file", "package a", "fixed it", "now add a test", "added") +} diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index 8c2dcd83a..cb012f705 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -5566,7 +5566,11 @@ func renderCachedParts(blocks []map[string]any, text *strings.Builder, parts []P case File: text.WriteString(attachmentText(p)) case Thinking: - text.WriteString(p.Text) + // A reply's thinking (and redacted_thinking) is never told as + // words: written as the assistant's text in the history, it + // reached the model as earlier reasoning pasted into a user + // message, which Anthropic refused as reasoning extraction, + // and every run started anew after that told it again (#6623) case ToolCall: name := p.Name if offered[name] {