From be689b88339264a543c1c25cb3f11ad383c260d2 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 05:30:58 +0800 Subject: [PATCH] gateway: a made reasoning item says its text once, and an item with no summary key doesn't panic (#2855) magpieMadeReasoning joined an item's summaries and its reasoning_text into the made summary. #2855's later rollout (lyufeng000, 10-31) has DeepSeek items whose content repeats their summary, so the made item would carry the words twice, and DeepSeek be handed them back twice (withReasoningText reads the summary). It now takes the text, else the summaries. It also ranged over item["summary"].([]any), a single-value assertion that panics on an item with no summary key; both reads now use the checked form. TestMadeReasoningSaysItsTextOnce. Reviewed and landed as one batch of 12 commits (gua's #4593, #3269, #3881, #2887, #3289, #2718, #2855, Edward4226's #2718 key-path sibling, and four review follow-ups). Run for the batch, whose last commit's tree is 5d58a75f3: - Full suite before the rebase (base d307aa648, wsl-run magpie-desktop, go test -tags nogui -count=1 -timeout 45m ./...): 81 packages ok. Reds, all with issues and none in code this batch touches: internal/agent TestInstalledVersion, TestGrokInstalled, TestOmpDetectedWithPiDir (#4033, #3548, #3141); gateway TestUnroutedHeldReportSaved (#4300); gui TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestColdTurnsTaken (#4510); provider TestClaudeAccountServesModelsListedSinceItsFetch (#3174). Run alone at origin/main 573b21f4b, TestGrokInstalled, TestOmpDetectedWithPiDir and TestSettingsSaveKeepsWhatItDoesNotSend are red there too. The other four pass there alone. - After the rebase onto origin/main 573b21f4b, at this tree: internal/usage, update, gateway and provider ok. In internal/agent, the #4033/#3141 install-detection reds plus TestCopilotFolderFrom- AnIDEPluginIsNotTheCLI and TestZedAppPath. Those two are flaky the same way at origin/main (-count=10 alone: 4 and 5 of 10 red) and are already listed in #4033. The #3269 tests (TestCopilotLauncher*) pass 5 of 5. - Rebased onto origin/main ef63ef4a2. The #2855 commit met 0b9727ec9 (#2974) in responses.go: the streamed item's done now takes both, the upstream's own id (thinkUp) and the made-over item; and in gateway-routing.md main's paragraph is kept with #2855's clause. That matches gua's latest head (range-diff: context only). At tree d7c76c6fa: internal/gateway, provider, usage, redact and update ok; vet GOOS=linux -tags nogui ./internal/..., darwin and GOOS=windows for gateway and provider. - Rebased last onto origin/main d8f7d089f (5 commits: internal/agent and GUI files only). At tree 0d4bfd8c8, the pushed one: internal/agent's TestCopilotLauncher*, TestClaude*, TestOwn* and TestVSCode* ok, -count=3; TestCopilotFolderFromAnIDEPluginIsNotTheCLI red once there, as it is at main (#4033). Vet of internal/agent: darwin, linux and windows. - gofmt -l clean. On mote: go vet darwin, GOOS=linux -tags nogui and GOOS=windows. - Broken by hand, each turning its guard red: the lift gate (TestReadingKeepsARefusalsRest, TestAutoVisionKeepsOffARefusedAccount); the named-time condition (TestNamedQuotaTimeReplacesAGuess); the filter-chain hop (WholeReply/glm-5.3-flash); Edward's aAt (TestKeyAllowanceElapsedResetStartsAgain); and the old magpieMadeReasoning, which panics in TestMadeReasoningSaysItsTextOnce/ no_summary. Every new test of gua's is red at base d307aa648. Not tried: real OpenAI, DeepSeek, GLM or ChatGPT accounts; a real VS Code 1.144 install on Windows; a GUI run (no GUI files are touched). --- internal/gateway/reasoning_content.go | 39 ++++++++++++---------- internal/gateway/reasoning_content_test.go | 19 +++++++++++ 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/internal/gateway/reasoning_content.go b/internal/gateway/reasoning_content.go index 974fe40ff..270f728bb 100644 --- a/internal/gateway/reasoning_content.go +++ b/internal/gateway/reasoning_content.go @@ -31,7 +31,7 @@ func reasoningWithText(item map[string]any) bool { // magpieMadeReasoning is the reasoning item magpie makes of one a // Responses upstream wrote with its text in it, nil of one that carries no -// content, which goes as it came. The item's summaries and its text become +// content, which goes as it came. Its text (else its summaries) becomes // one summary of magpie's; its content and its seal go, the text kept, so // an upstream that wants its reasoning back is sent it (withReasoningText). // Its own id is kept, the events of a stream naming the item by it. @@ -39,31 +39,36 @@ func magpieMadeReasoning(item map[string]any) map[string]any { if !reasoningWithText(item) { return nil } - var b strings.Builder - for _, p := range item["summary"].([]any) { - pm, _ := p.(map[string]any) - if pm == nil { - continue - } - s, _ := pm["text"].(string) - b.WriteString(s) - } - for _, p := range item["content"].([]any) { - pm, _ := p.(map[string]any) - if pm == nil { - continue - } - if t, _ := pm["type"].(string); t == "reasoning_text" { + // the text, else the summaries: a vendor that writes both may write + // the same words twice (#2855's later rollout: each item's content + // the summary over again), and the text is what a vendor that wants + // its reasoning back reads + text := func(key, typ string) string { + var b strings.Builder + parts, _ := item[key].([]any) + for _, p := range parts { + pm, _ := p.(map[string]any) + if pm == nil { + continue + } + if t, _ := pm["type"].(string); typ != "" && t != typ { + continue + } s, _ := pm["text"].(string) b.WriteString(s) } + return b.String() + } + said := text("content", "reasoning_text") + if said == "" { + said = text("summary", "") } id, _ := item["id"].(string) if id == "" { id = "rs_" + newID() } return map[string]any{"id": id, "type": "reasoning", "status": "completed", - "summary": []map[string]any{{"type": "summary_text", "text": b.String()}}} + "summary": []map[string]any{{"type": "summary_text", "text": said}}} } // magpieMadeReasoningRaw is the item's bytes, made over when the item diff --git a/internal/gateway/reasoning_content_test.go b/internal/gateway/reasoning_content_test.go index 55d83789a..42f058613 100644 --- a/internal/gateway/reasoning_content_test.go +++ b/internal/gateway/reasoning_content_test.go @@ -250,3 +250,22 @@ func TestForeignSealedReasoningWholeReplyCarriesNoContent(t *testing.T) { }) } } + +// The made item's summary is the item's text once: a vendor that writes +// its reasoning as both summary and content (#2855's later rollout) is not +// handed back the words twice, and an item with no summary key at all is +// made over, not a panic. +func TestMadeReasoningSaysItsTextOnce(t *testing.T) { + for name, raw := range map[string]string{ + "both": `{"type":"reasoning","id":"rs_1","summary":[{"type":"summary_text","text":"counting the lines"}],"content":[{"type":"reasoning_text","text":"counting the lines"}]}`, + "no summary": `{"type":"reasoning","id":"rs_1","content":[{"type":"reasoning_text","text":"counting the lines"}]}`, + } { + t.Run(name, func(t *testing.T) { + var it map[string]any + if err := json.Unmarshal(magpieMadeReasoningRaw(json.RawMessage(raw), ""), &it); err != nil { + t.Fatal(err) + } + assertResendable(t, it, "counting the lines") + }) + } +}