fix(claude): drop replayed web search citations when search results are missing

- Attach request-scoped web search result cache lookup before Responses to Claude request translation so cached search hits are restored before evaluating turn citations.
- Strip web_search_result_location citations from assistant text blocks whenever a web_search_tool_result in the same assistant turn ends up without content while preserving the response text.

Closes: #7213
This commit is contained in:
Luis Pater
2026-11-03 11:21:33 +08:00
parent a1eeb60ec3
commit 9acf81d3a6
9 changed files with 441 additions and 32 deletions

View File

@@ -80,6 +80,9 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
ctx = helps.WithClaudeSessionID(ctx, claudeSessionID)
}
webSearchSessionKey, webSearchTTL, webSearchLimit := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload)
ctx = helps.WithClaudeWebSearchResultRestore(ctx, webSearchSessionKey, webSearchTTL)
isCompat := helps.APIKeyModelIsCompat(req)
originalTranslated, body, err := helps.TranslateRequestPairReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, originalPayload, req.Payload, upstreamStream, isCompat)
if err != nil {
@@ -92,7 +95,6 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); err != nil {
return resp, err
}
webSearchSessionKey, webSearchTTL, webSearchLimit := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload)
helps.RestoreClaudeWebSearchResultsContextDoc(ctx, doc, webSearchSessionKey, webSearchTTL)
if rebuildMidSystemMessageEnabled(e.cfg, auth) {
rebuildMidSystemMessagesToTopLevelDoc(doc)

View File

@@ -85,6 +85,9 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
ctx = helps.WithClaudeSessionID(ctx, claudeSessionID)
}
webSearchSessionKey, webSearchTTL, webSearchLimit := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload)
ctx = helps.WithClaudeWebSearchResultRestore(ctx, webSearchSessionKey, webSearchTTL)
isCompat := helps.APIKeyModelIsCompat(req)
originalTranslated, body, err := helps.TranslateRequestPairReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, originalPayload, req.Payload, true, isCompat)
if err != nil {
@@ -97,7 +100,6 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); err != nil {
return nil, err
}
webSearchSessionKey, webSearchTTL, webSearchLimit := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload)
helps.RestoreClaudeWebSearchResultsContextDoc(ctx, doc, webSearchSessionKey, webSearchTTL)
if rebuildMidSystemMessageEnabled(e.cfg, auth) {
rebuildMidSystemMessagesToTopLevelDoc(doc)

View File

@@ -37,6 +37,13 @@ func (e *ClaudeExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Aut
responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
to := sdktranslator.FromString("claude")
originalPayload := req.Payload
if len(opts.OriginalRequest) > 0 {
originalPayload = opts.OriginalRequest
}
webSearchSessionKey, webSearchTTL, _ := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload)
ctx = helps.WithClaudeWebSearchResultRestore(ctx, webSearchSessionKey, webSearchTTL)
// Use streaming translation to preserve function calling, except for claude.
stream := from != to
body, err := helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, req.Payload, stream, helps.APIKeyModelIsCompat(req))
@@ -53,11 +60,6 @@ func (e *ClaudeExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Aut
if errThinking := helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); errThinking != nil {
return cliproxyexecutor.Response{}, errThinking
}
originalPayload := req.Payload
if len(opts.OriginalRequest) > 0 {
originalPayload = opts.OriginalRequest
}
webSearchSessionKey, webSearchTTL, _ := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload)
helps.RestoreClaudeWebSearchResultsContextDoc(ctx, doc, webSearchSessionKey, webSearchTTL)
if rebuildMidSystemMessageEnabled(e.cfg, auth) {
rebuildMidSystemMessagesToTopLevelDoc(doc)
@@ -164,6 +166,8 @@ func (e *ClaudeExecutor) countTokensUpstream(ctx context.Context, auth *cliproxy
if fp.ProfileClaudeCodeCLI {
claudeSessionID = helps.ClaudeAgentSessionUUIDForRequest(incomingHeaders, originalPayload, req.Payload, confirmedClaudeCode, opts.Metadata, req.Metadata)
}
webSearchSessionKey, webSearchTTL, _ := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload)
ctx = helps.WithClaudeWebSearchResultRestore(ctx, webSearchSessionKey, webSearchTTL)
// Use streaming translation to preserve function calling, except for claude.
stream := from != to
body, err := helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, req.Payload, stream, helps.APIKeyModelIsCompat(req))
@@ -180,7 +184,6 @@ func (e *ClaudeExecutor) countTokensUpstream(ctx context.Context, auth *cliproxy
if errThinking := helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); errThinking != nil {
return cliproxyexecutor.Response{}, errThinking
}
webSearchSessionKey, webSearchTTL, _ := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload)
helps.RestoreClaudeWebSearchResultsContextDoc(ctx, doc, webSearchSessionKey, webSearchTTL)
if rebuildMidSystemMessageEnabled(e.cfg, auth) {
rebuildMidSystemMessagesToTopLevelDoc(doc)

View File

@@ -143,10 +143,11 @@ func runClaudeWebSearchScenario(t *testing.T, cfg *config.Config, stream bool, r
const (
claudeWebSearchExecutorUser = `{"type":"message","role":"user","content":[{"type":"input_text","text":"search for q"}]}`
// The history as Codex stores it: the web_search_call has no results.
// The history as Codex stores it: the web_search_call has no results, while
// the assistant message keeps its web_search_result_location annotations.
claudeWebSearchExecutorHistory = claudeWebSearchExecutorUser +
`,{"type":"web_search_call","id":"ws_srvtoolu_01abc","status":"completed","action":{"type":"search","query":"q"}},` +
`{"type":"message","role":"assistant","content":[{"type":"output_text","text":"found two pages"}]},` +
`{"type":"message","role":"assistant","content":[{"type":"output_text","text":"found two pages","annotations":[{"type":"web_search_result_location","url":"https://example.com/a","title":"A","cited_text":"found two pages","encrypted_index":"IDX_A"}]}]},` +
`{"type":"message","role":"user","content":[{"type":"input_text","text":"how many results?"}]}`
)
@@ -185,12 +186,21 @@ func TestClaudeExecutor_RestoresWebSearchResultsDroppedByResponsesClient(t *test
if got := claudeWebSearchResultContent(t, bodies[1]); got != claudeWebSearchExecutorResults {
t.Fatalf("same session: web_search_tool_result content = %s, want original results", got)
}
if got := ojson.GetBytes(bodies[1], "messages.1.content.2.citations.0.encrypted_index").String(); got != "IDX_A" {
t.Fatalf("same session: expected restored search results to keep citations, got %q in %s", got, bodies[1])
}
if got := claudeWebSearchResultContent(t, bodies[2]); got != "[]" {
t.Fatalf("other session: web_search_tool_result content = %s, want []", got)
}
if citations := ojson.GetBytes(bodies[2], "messages.1.content.2.citations"); citations.Exists() {
t.Fatalf("other session: expected unbacked citations to be dropped, got %s", citations.Raw)
}
if got := claudeWebSearchResultContent(t, bodies[3]); got != "[]" {
t.Fatalf("no session identity: web_search_tool_result content = %s, want []", got)
}
if citations := ojson.GetBytes(bodies[3], "messages.1.content.2.citations"); citations.Exists() {
t.Fatalf("no session identity: expected unbacked citations to be dropped, got %s", citations.Raw)
}
})
}
}
@@ -208,6 +218,12 @@ func TestClaudeExecutor_WebSearchResultCacheDisabledByDefault(t *testing.T) {
if got := claudeWebSearchResultContent(t, bodies[1]); got != "[]" {
t.Fatalf("disabled cache: web_search_tool_result content = %s, want []", got)
}
if citations := ojson.GetBytes(bodies[1], "messages.1.content.2.citations"); citations.Exists() {
t.Fatalf("disabled cache: expected unbacked citations to be dropped, got %s", citations.Raw)
}
if got := ojson.GetBytes(bodies[1], "messages.1.content.2.text").String(); got != "found two pages" {
t.Fatalf("disabled cache: expected text to remain intact, got %q", got)
}
})
}
}
@@ -267,9 +283,15 @@ func TestClaudeExecutor_WebSearchResultCacheHonorsConfiguredLimit(t *testing.T)
if got := claudeWebSearchResultContent(t, bodies[2]); got != "[]" {
t.Fatalf("evicted session-1: web_search_tool_result content = %s, want []", got)
}
if citations := ojson.GetBytes(bodies[2], "messages.1.content.2.citations"); citations.Exists() {
t.Fatalf("evicted session-1: expected citations to be dropped, got %s", citations.Raw)
}
if got := claudeWebSearchResultContent(t, bodies[3]); got != claudeWebSearchExecutorResults {
t.Fatalf("retained session-2: web_search_tool_result content = %s, want original results", got)
}
if got := ojson.GetBytes(bodies[3], "messages.1.content.2.citations.0.encrypted_index").String(); got != "IDX_A" {
t.Fatalf("retained session-2: expected citations to be kept, got %q", got)
}
}
func TestClaudeExecutor_CountTokensRestoresWebSearchResultsWhenEnabled(t *testing.T) {
@@ -351,15 +373,27 @@ func TestClaudeExecutor_WebSearchResultCacheScopesByCredential(t *testing.T) {
if got := claudeWebSearchResultContent(t, bodies[1]); got != "[]" {
t.Fatalf("different credential ID in same session: web_search_tool_result content = %s, want []", got)
}
if citations := ojson.GetBytes(bodies[1], "messages.1.content.2.citations"); citations.Exists() {
t.Fatalf("different credential ID in same session: expected citations to be dropped, got %s", citations.Raw)
}
if got := claudeWebSearchResultContent(t, bodies[2]); got != claudeWebSearchExecutorResults {
t.Fatalf("original credential ID in same session: web_search_tool_result content = %s, want original results", got)
}
if got := ojson.GetBytes(bodies[2], "messages.1.content.2.citations.0.encrypted_index").String(); got != "IDX_A" {
t.Fatalf("original credential ID in same session: expected citations to be kept, got %q", got)
}
if got := claudeWebSearchResultContent(t, bodies[4]); got != "[]" {
t.Fatalf("different API key in same session: web_search_tool_result content = %s, want []", got)
}
if citations := ojson.GetBytes(bodies[4], "messages.1.content.2.citations"); citations.Exists() {
t.Fatalf("different API key in same session: expected citations to be dropped, got %s", citations.Raw)
}
if got := claudeWebSearchResultContent(t, bodies[5]); got != claudeWebSearchExecutorResults {
t.Fatalf("original API key in same session: web_search_tool_result content = %s, want original results", got)
}
if got := ojson.GetBytes(bodies[5], "messages.1.content.2.citations.0.encrypted_index").String(); got != "IDX_A" {
t.Fatalf("original API key in same session: expected citations to be kept, got %q", got)
}
})
}
}

View File

@@ -9,11 +9,32 @@ import (
"github.com/MoeForge/moego/ojson"
"github.com/router-for-me/CLIProxyAPI/v8/internal/cache"
responsesclaude "github.com/router-for-me/CLIProxyAPI/v8/internal/translator/claude/openai/responses"
translatorcommon "github.com/router-for-me/CLIProxyAPI/v8/internal/translator/common"
"github.com/router-for-me/CLIProxyAPI/v8/internal/util"
log "github.com/sirupsen/logrus"
)
var claudeWebSearchToolResultMarker = []byte(`"web_search_tool_result"`)
var (
claudeWebSearchToolResultMarker = []byte(`"web_search_tool_result"`)
claudeWebSearchCitationMarker = []byte(`"web_search_result_location"`)
)
// WithClaudeWebSearchResultRestore attaches a request-scoped cache lookup so
// Responses -> Claude request translation can restore web_search_tool_result
// blocks before deciding whether assistant citations remain backed by results.
func WithClaudeWebSearchResultRestore(ctx context.Context, sessionKey string, ttl ...time.Duration) context.Context {
if strings.TrimSpace(sessionKey) == "" {
return ctx
}
return responsesclaude.WithWebSearchResultLookup(ctx, func(toolUseID string) ([]byte, bool) {
cached, ok, errGet := cache.GetClaudeWebSearchResultRequired(ctx, sessionKey, toolUseID, ttl...)
if errGet != nil || !ok || len(cached) == 0 {
return nil, false
}
return cached, true
})
}
// RecordClaudeWebSearchResults stores the web_search_tool_result blocks of a
// Claude response for sessionKey. data is either a complete Messages response
@@ -77,7 +98,10 @@ func RestoreClaudeWebSearchResultsDoc(doc *ojson.Doc, sessionKey string, ttl ...
// blocks whose content is missing or empty with the content stored for
// sessionKey using ctx for Home KV operations.
func RestoreClaudeWebSearchResultsContext(ctx context.Context, body []byte, sessionKey string, ttl ...time.Duration) []byte {
if strings.TrimSpace(sessionKey) == "" || !bytes.Contains(body, claudeWebSearchToolResultMarker) {
if !bytes.Contains(body, claudeWebSearchToolResultMarker) {
return body
}
if strings.TrimSpace(sessionKey) == "" && !bytes.Contains(body, claudeWebSearchCitationMarker) {
return body
}
doc := ojson.NewDoc(body)
@@ -89,42 +113,109 @@ func RestoreClaudeWebSearchResultsContext(ctx context.Context, body []byte, sess
// RestoreClaudeWebSearchResultsContextDoc fills assistant web_search_tool_result
// blocks in doc whose content is missing or empty with the content stored for
// sessionKey using ctx for Home KV operations.
// sessionKey using ctx for Home KV operations, and drops web_search_result_location
// citations from assistant turns whose web_search_tool_result remains empty.
func RestoreClaudeWebSearchResultsContextDoc(ctx context.Context, doc *ojson.Doc, sessionKey string, ttl ...time.Duration) bool {
if doc == nil || doc.Len() == 0 || strings.TrimSpace(sessionKey) == "" {
if doc == nil || doc.Len() == 0 {
return false
}
messages := doc.Get("messages")
if !messages.IsArray() {
return false
}
trimmedSessionKey := strings.TrimSpace(sessionKey)
restored := 0
droppedCitations := 0
turnHasEmptyWebSearchResult := false
for i, message := range messages.Array() {
if message.Get("role").String() != "assistant" {
role := message.Get("role").String()
if role != "assistant" {
if claudeUserMessageIsTurnBoundary(message) {
turnHasEmptyWebSearchResult = false
}
continue
}
for j, block := range message.Get("content").Array() {
blocks := message.Get("content").Array()
for j, block := range blocks {
if block.Get("type").String() != "web_search_tool_result" || !claudeWebSearchResultContentEmpty(block.Get("content")) {
continue
}
cached, ok, errGet := cache.GetClaudeWebSearchResultRequired(ctx, sessionKey, block.Get("tool_use_id").String(), ttl...)
if errGet != nil || !ok {
if trimmedSessionKey != "" {
cached, ok, errGet := cache.GetClaudeWebSearchResultRequired(ctx, trimmedSessionKey, block.Get("tool_use_id").String(), ttl...)
if errGet == nil && ok && len(cached) > 0 {
if errSet := doc.SetRawBytes(fmt.Sprintf("messages.%d.content.%d.content", i, j), cached); errSet == nil {
restored++
continue
} else {
log.Debugf("claude web search results: restore messages.%d.content.%d failed: %v", i, j, errSet)
}
}
}
turnHasEmptyWebSearchResult = true
}
if !turnHasEmptyWebSearchResult {
continue
}
for j, block := range blocks {
if block.Get("type").String() != "text" {
continue
}
if errSet := doc.SetRawBytes(fmt.Sprintf("messages.%d.content.%d.content", i, j), cached); errSet != nil {
log.Debugf("claude web search results: restore messages.%d.content.%d failed: %v", i, j, errSet)
continue
}
restored++
dropped := stripClaudeWebSearchCitationsDoc(doc, fmt.Sprintf("messages.%d.content.%d", i, j), block.Get("citations"))
droppedCitations += dropped
}
}
if restored > 0 {
log.Debugf("claude web search results: restored %d result block(s) from cache", restored)
}
if droppedCitations > 0 {
log.Debugf("claude web search results: dropped %d web_search_result_location citation(s) from assistant turn without search results", droppedCitations)
}
return restored > 0 || droppedCitations > 0
}
func claudeUserMessageIsTurnBoundary(message ojson.Result) bool {
content := message.Get("content")
if !content.IsArray() {
return true
}
for _, block := range content.Array() {
if block.Get("type").String() != "tool_result" {
return true
}
}
return false
}
func stripClaudeWebSearchCitationsDoc(doc *ojson.Doc, blockPath string, citations ojson.Result) int {
if !citations.Exists() || !citations.IsArray() {
return 0
}
var kept [][]byte
dropped := 0
citations.ForEach(func(_, citation ojson.Result) bool {
if citation.Get("type").String() == "web_search_result_location" {
dropped++
return true
}
kept = append(kept, []byte(citation.Raw))
return true
})
if dropped == 0 {
return 0
}
citationsPath := blockPath + ".citations"
if len(kept) == 0 {
if errDelete := doc.Delete(citationsPath); errDelete != nil {
return 0
}
return dropped
}
if errSet := doc.SetRawBytes(citationsPath, translatorcommon.JoinRawArray(kept)); errSet != nil {
return 0
}
return dropped
}
func claudeWebSearchResultContentEmpty(content ojson.Result) bool {
if !content.Exists() || content.Type == ojson.Null {
return true

View File

@@ -83,3 +83,40 @@ func TestRecordClaudeWebSearchResultsIgnoresEmptyResults(t *testing.T) {
t.Fatal("empty result content was cached")
}
}
func TestRestoreClaudeWebSearchResultsDropsCitationsWhenResultRemainsEmpty(t *testing.T) {
resetClaudeWebSearchResults(t)
event := []byte(`data: {"type":"content_block_start","index":1,"content_block":{"type":"web_search_tool_result","tool_use_id":"srvtoolu_1","content":` + claudeWebSearchTestResults + `}}`)
RecordClaudeWebSearchResults("session-1", event, 30*time.Minute, 16)
citedBody := []byte(`{"model":"claude-opus-5-5","messages":[` +
`{"role":"user","content":[{"type":"text","text":"search"}]},` +
`{"role":"assistant","content":[` +
`{"type":"server_tool_use","id":"srvtoolu_1","name":"web_search","input":{"query":"q"}},` +
`{"type":"web_search_tool_result","tool_use_id":"srvtoolu_1","content":[]},` +
`{"type":"text","text":"answer","citations":[{"type":"web_search_result_location","url":"https://example.com/a","title":"A","cited_text":"answer","encrypted_index":"IDX_A"}]}]},` +
`{"role":"user","content":[{"type":"text","text":"how many results?"}]}]}`)
restored := RestoreClaudeWebSearchResults(citedBody, "session-1", 30*time.Minute)
if got := ojson.GetBytes(restored, "messages.1.content.1.content").Raw; got != claudeWebSearchTestResults {
t.Fatalf("restored content = %s, want %s", got, claudeWebSearchTestResults)
}
if got := ojson.GetBytes(restored, "messages.1.content.2.citations.0.encrypted_index").String(); got != "IDX_A" {
t.Fatalf("restored turn citation encrypted_index = %q, want IDX_A: %s", got, restored)
}
for name, sessionKey := range map[string]string{
"cache miss": "session-2",
"empty scope": "",
} {
t.Run(name, func(t *testing.T) {
missed := RestoreClaudeWebSearchResults(citedBody, sessionKey, 30*time.Minute)
if got := ojson.GetBytes(missed, "messages.1.content.2.text").String(); got != "answer" {
t.Fatalf("text = %q, want answer", got)
}
if citations := ojson.GetBytes(missed, "messages.1.content.2.citations"); citations.Exists() {
t.Fatalf("expected citations to be dropped on %s, got: %s", name, citations.Raw)
}
})
}
}