Skip to content

fix: Optimized and support for inference context return - #539

Merged
chenyme merged 2 commits into
devfrom
conversation
Jul 31, 2026
Merged

fix: Optimized and support for inference context return#539
chenyme merged 2 commits into
devfrom
conversation

Conversation

@chenyme

@chenyme chenyme commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

「回传推理上下文」自 5781834b 起完全失效:reasoning_content 正常入库,但从未被读出来过。

根因是 ListMessageAncestors / ListMessageAncestorsUntil 两处 WITH RECURSIVE CTE 手写了列清单并漏掉 reasoning_content。该清单原本是为规避「GORM Scan 遇到未知字段会出错」而写的——这个前提经实测是错的,GORM 按列名映射并静默忽略未匹配列(gorm/scan.go:276-281,与驱动无关)。所以本 PR 不只补列,而是把两处外层投影改为 SELECT *,从根上消除「手写列清单随新增列漂移」这一 bug class。

排查中发现同一 bug class 的另外两处,一并修复:cloneSharedMessage 手写字段清单同样漏掉 ReasoningContent(且复制了 ReasoningTokens,造成行内自相矛盾);中断生成的落库更新未带该字段,而 UpdateAssistantMessageCompletion 是无条件覆盖,会把已产出的推理抹成空——interrupted 状态的 assistant 消息仍会进入后续轮次上下文。

另外扩展了 Chat Completions 的回传厂商白名单,加入 moonshot / zhipu(issue 中提到的 K3 需求)。两家均要求历史 assistant 消息原样携带 reasoning_content:Moonshot 缺失时服务端直接返回 400,智谱默认 clear_thinking=false 即保留思考。字段名与放置位置和现有序列化一致,未改动协议层。

Fixes #529

Change type

  • Bug fix
  • Feature
  • Documentation
  • Refactor
  • Configuration / deployment
  • Security hardening
  • Other

Affected areas

  • Frontend / UI
  • Backend / API
  • Authentication / authorization
  • Conversations / streaming
  • Files / RAG / extraction
  • Model routing / providers
  • MCP / tools
  • Billing / payments
  • Admin console
  • Deployment / Docker / configuration
  • Documentation

Verification

  • pnpm check — 通过(api-contract 契约一致、biome lint 544 文件无问题、前后端 typecheck)
  • pnpm test — 通过
  • go build ./... / go vet ./... / gofmt -l internal/ — 全部干净
  • go test ./... — 55 包全绿,0 失败

补充验证:

  • 变异测试:每处修复都撤销后确认对应测试变红,再恢复,确保守卫不是「写完就绿」
  • 真实 Postgres 18:CTE 改动在临时容器中跑通真实 repo 方法(此前仅覆盖 SQLite in-memory),确认 SELECT * 的语法与 _depth 辅助列扫描行为一致

未验证:没有挂真实 DeepSeek / Kimi / GLM 渠道端到端抓包确认出站请求携带 reasoning_content。建议合并后在同一对话连发 3 轮实测确认。

Screenshots, API examples, or logs

变异测试输出(撤销修复时):

ListMessageAncestors dropped columns
ListMessageAncestors: ancestor 1 reasoning content = "", want "reasoning 2"
cloned reasoning content = "", want preserved
expected trimmed reasoning to be retained, got "stale"
ancestor walked into conversation 1: ... Content:"FOREIGN_SECRET" ...

真实 Postgres 18 验证:

postgres OK: reasoning="PG_HISTORICAL_REASONING" pricing="{"in":1}" reasoningTokens=125

Configuration, migration, and compatibility notes

  • 无数据库迁移reasoning_content 列早已由迁移添加(postgres.go:397),本 PR 仅修正读取
  • 无配置变更,无环境变量、Docker、CORS、公开 URL 变动
  • API 契约未变pnpm check 的 swagger/TS 契约校验通过),无生成物改动
  • 前端零改动:回传开关是全局布尔(chat.reasoning_content_passback),vendor 判定全在后端;开关文案本就是 vendor 无关的「支持的 Chat Completions 推理模型」
  • 向后兼容SELECT * 对 DB 多出的列忽略、模型多出的字段取零值,行为不弱于原显式清单
  • PostgreSQL 与 SQLite 均受影响且均已修复——两者共用 internal/infra/persistence/postgres 仓储实现(persistence/sqlite/ 只含连接配置)

Documentation

  • Documentation is not needed for this change.
  • Documentation was updated.
  • Documentation still needs to be updated.

Security and privacy

  • No secrets, tokens, credentials, local config, or personal data are included.
  • User data access remains scoped by authenticated user context unless an admin-only path explicitly requires broader access.
  • Security-sensitive behavior was reviewed, including authentication, authorization, provider routing, file processing, billing, and admin APIs where relevant.

安全加固说明:ListMessageAncestors 的递归项此前未约束 m.conversation_id(兄弟方法 ListMessageAncestorsUntil 有)。经核查不是当前可利用漏洞——三条写入路径均强制同会话父指针,HTTP 层虽接受调用方传 parentMessagePublicID,但经会话+用户双重限定的 GetMessageByPublicID 校验后才落库。但 parent_message_id没有外键,不变量纯靠应用层保证;且本 PR 的 SELECT * 会扩大一旦不变量被破坏时的泄漏面(原本连推理内容都读不出来)。已补上约束并加回归测试,测试绕过应用层直接植入跨会话父指针,验证遍历会停在会话边界。

Checklist

  • I searched existing issues and pull requests.
  • Changes are focused and do not include unrelated refactors.
  • Tests or static verification were run where practical.
  • User-facing behavior, deployment steps, API contracts, or configuration changes are documented.
  • Generated artifacts are included only when this project explicitly requires them.
  • Caches, build output, .pyc files, .env files, and local storage data are not committed.

@chenyme chenyme self-assigned this Jul 31, 2026
@chenyme
chenyme merged commit 05c3d95 into dev Jul 31, 2026
11 checks passed
@chenyme
chenyme deleted the conversation branch July 31, 2026 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 回传推理上下文功能失效,历史推理从未被实际回传

1 participant