From 3de758e793e2a6d8fca93f61e870d07e86057edf Mon Sep 17 00:00:00 2001 From: ctermiii Date: Sat, 5 Sep 2026 16:37:23 +0800 Subject: [PATCH] fix(cli): auto-heal missing reasoning blocks in responses history - support pipe-delimited call_id lookup (callId|itemId) for pi coding agent - automatically backfill missing reasoning blocks before function_call in normalizeInputItems - fallback to recent reasoning proof in conversation history when cache is cold - prevents tool call deadlocks and infinite repetition in long-running agent loops --- .../infra/provider/cli/responses_history.go | 65 +++++++++++++++++++ .../provider/conversation/reasoning_cache.go | 14 ++-- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/backend/internal/infra/provider/cli/responses_history.go b/backend/internal/infra/provider/cli/responses_history.go index c5675dec..fd4c57ad 100644 --- a/backend/internal/infra/provider/cli/responses_history.go +++ b/backend/internal/infra/provider/cli/responses_history.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" "strings" + + "github.com/chenyme/grok2api/backend/internal/infra/provider/conversation" ) // normalizeInputItems 将 Codex/Responses 扩展历史降级为 Grok Build 可接受的结构, @@ -34,6 +36,34 @@ func (c *responsesToolCompatibility) normalizeInputItems(items []any) ([]any, [] c.changed = true rewritten = append(rewritten, converted) case "function_call": + callID := strings.TrimSpace(stringField(item, "call_id")) + if !hasPrecedingReasoningItem(rewritten) { + lookupID := callID + if strings.Contains(lookupID, "|") { + lookupID = strings.Split(lookupID, "|")[0] + } + if reasoningItem, found := conversation.GetReasoningForCall(lookupID); found && reasoningItem.ID != "" { + reasoningBlock := map[string]any{ + "type": "reasoning", + "id": reasoningItem.ID, + "status": "completed", + } + if reasoningItem.Encrypted != "" { + reasoningBlock["encrypted_content"] = reasoningItem.Encrypted + } + if len(reasoningItem.Summary) > 0 { + reasoningBlock["summary"] = reasoningItem.Summary + } + if len(reasoningItem.Content) > 0 { + reasoningBlock["content"] = reasoningItem.Content + } + rewritten = append(rewritten, reasoningBlock) + c.changed = true + } else if fallback := findLastReasoningBlock(rewritten, items); fallback != nil { + rewritten = append(rewritten, fallback) + c.changed = true + } + } converted, err := c.normalizeFunctionCallInput(item, param) if err != nil { return nil, nil, nil, err @@ -378,3 +408,38 @@ func encodeFunctionArguments(value any) (string, error) { } return string(encoded), nil } + +func hasPrecedingReasoningItem(items []any) bool { + if len(items) == 0 { + return false + } + last, ok := items[len(items)-1].(map[string]any) + if !ok { + return false + } + return stringField(last, "type") == "reasoning" +} + +func findLastReasoningBlock(rewritten []any, originalItems []any) map[string]any { + // 优先在已重写的序列中倒序查找最近的合法 reasoning + for i := len(rewritten) - 1; i >= 0; i-- { + item, ok := rewritten[i].(map[string]any) + if !ok { + continue + } + if stringField(item, "type") == "reasoning" && stringField(item, "encrypted_content") != "" { + return cloneJSONObject(item) + } + } + // 其次在原始输入序列中倒序查找 + for i := len(originalItems) - 1; i >= 0; i-- { + item, ok := originalItems[i].(map[string]any) + if !ok { + continue + } + if stringField(item, "type") == "reasoning" && stringField(item, "encrypted_content") != "" { + return cloneJSONObject(item) + } + } + return nil +} diff --git a/backend/internal/infra/provider/conversation/reasoning_cache.go b/backend/internal/infra/provider/conversation/reasoning_cache.go index 8b636571..02febdec 100644 --- a/backend/internal/infra/provider/conversation/reasoning_cache.go +++ b/backend/internal/infra/provider/conversation/reasoning_cache.go @@ -1,6 +1,7 @@ package conversation import ( + "strings" "sync" "time" ) @@ -99,13 +100,16 @@ func (c *ReasoningCache) Get(callID string) (responseItem, bool) { defer c.mu.RUnlock() entry, exists := c.entries[callID] - if !exists { - return responseItem{}, false + if exists && time.Since(entry.createdAt) <= c.ttl { + return entry.item, true } - if time.Since(entry.createdAt) > c.ttl { - return responseItem{}, false + if strings.Contains(callID, "|") { + prefix := strings.Split(callID, "|")[0] + if entry, exists := c.entries[prefix]; exists && time.Since(entry.createdAt) <= c.ttl { + return entry.item, true + } } - return entry.item, true + return responseItem{}, false } func (c *ReasoningCache) evictOldestLocked(now time.Time) {