Repository navigation
fix(responses): 伺服端對話歷史暫存 + 按模型有效上下文上限 - #6
Merged
2951461586 merged 3 commits intoNov 20, 2026
Merged
2951461586 merged 3 commits into
2951461586 merged 3 commits into
Conversation
問題: - /v1/responses 遇 previous_response_id 直接回 400 unsupported_parameter, 依賴服務端狀態的客戶端(Claude Code / Codex / magpie 插件)收到 400 後 靜默重開對話,造成「對話到一半失憶」。 - 上游無狀態,單一 120k 上限對多數模型過於保守;上游甚至會在約 128k 靜默丟棄舊歷史,造成另一種失憶。 修改: - 新增 response_history.go:進程內對話歷史暫存(TTL 24h / 最多 500 輪), 四條路徑(透傳/合成 x 串流/非串流)皆記錄;收到 previous_response_id 時沿鏈重組完整 input 送上游,不再回 400。 - truncation:auto 且重放鏈超過上限時,裁掉最舊中段並插入 user 角色標記; 未設 truncation 則明確 400,不靜默砍歷史。 - 有效上下文上限改按模型查表:9 個免費模型 320k、glyph-cluster 256k、 mimo-2.6-flash 140k(170k 起上游靜默截斷);其餘 min(標示值, 320k)。 可用 DEEPAI2API_EFFECTIVE_CONTEXT_WINDOW / DEEPAI2API_CONTEXT_WINDOWS 覆寫。 - token 估算校準(ASCII ~1.6 bytes/token),裁剪標記改用 user 角色 (system 出現在中段會被上游 400)。 - 回歸測試 10 條全過,go test ./... 全綠。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題
/v1/responses遇到previous_response_id直接回400 unsupported_parameter。依賴服務端狀態的客戶端(Claude Code / Codex / magpie 插件)收到 400 後會靜默重開對話,造成「對話到一半失憶」(對應 issue Context/amnesia: conversation history not preserved (Responses previous_response_id unsupported; affinity vs stateless contract unclear) #5 的症狀)。修改內容
internal/server/response_history.go):進程內記錄每輪 input/output(TTL 24h、最多 500 輪,皆可設定)。收到previous_response_id時沿鏈重組完整 input 送上游,取代原本的 400 拒絕;透傳/合成 × 串流/非串流四條路徑皆覆蓋。新回應有自己的 id,可繼續鏈接。truncation:"auto"且重放鏈超過上限時,裁掉最舊中段並插入「中間對話已省略」標記(用user角色——system出現在中段會被上游 400);未設 truncation 則回明確 400 提示客戶端壓縮,绝不靜默砍歷史。glyph-cluster→ 256k(260k 完整、320k 上游硬拒絕,與目錄標示一致)mimo-2.6-flash→ 140k(170k 起上游靜默截斷,提前裁剪避免偷砍)min(標示值, 320k);可用DEEPAI2API_EFFECTIVE_CONTEXT_WINDOW(全域)與DEEPAI2API_CONTEXT_WINDOWS(按模型 JSON)覆寫。len/3對程式碼低估近一倍,會讓壓縮太晚觸發)。.env.example。測試
go test ./...全綠(10 個 package);新增回歸測試涵蓋鏈式記憶 3 輪、非鏈式不回歸、串流事件完整、TTL/容量、conversation 拒絕、truncation 裁剪、未設 truncation 回 400、11 個模型上限值與環境覆寫。glm-5.3-flash重放鏈累積 26 萬input_tokens=268421完整未裁剪(不再誤砍);mimo-2.6-flash累積 18 萬時在約 13.6 萬正確裁剪,無上游靜默丟歷史;服務重啟後 health 正常。限制
previous_response_id需重送完整 input。conversation/item_reference無法重建,保留明確 400。🤖 Generated with Claude Code