Skip to content

fix(llm): 后台调用统一带固定 session id,OpenCode 路由不再 400 (#668) - #670

Open
modusensus wants to merge 1 commit into
mainfrom
fix/llm-session-id
Open

modusensus wants to merge 1 commit into
mainfrom
fix/llm-session-id

Conversation

@modusensus

@modusensus modusensus commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

背景

#668 报告:插件后台 LLM 调用走 OpenCode 路由必然 400(缺 x-opencode-session)。

根因已核实:pi-ai 的 dist/providers/opencode-headers.js 中 withSessionHeader 只在 options.sessionId 存在时注入该头;而本插件 7 处 llm.stream 调用全部没传 sessionId。多数网关对此无声无息,OpenCode 直接 400。

改动

新增 src/llm-session.js:

  • BACKGROUND_SESSION_ID = "dsh-mneme"
  • withBackgroundSession(options):options.sessionId 已存在时原样返回不覆盖;否则浅拷贝补上固定标识

7 个调用点全部经它包一层(src/dream.js 与 src/dream/sleep.js 各有本地 streamText,改单点即覆盖全部 7 个 dream/sleep 相位;另五处 src/index.js、src/summarize.js、src/pre-inject-gate.js、src/api.js、src/image-describe.js)。

为什么固定标识而不是按 run id 派生:按 run id 会让网关把每次整理当一个新会话,限流按会话计数时更容易撞墙,排障时分不清来源层。固定一个既满足头部要求,也让网关侧能认出这是插件的后台流量。

为什么不覆盖宿主的真会话:注入链路上宿主可能带真实用户会话进来,那时换掉 id 等于伪造来源。

测试

test/llm-session.test.js 6 条:填 id 且不改入参 / 已给真 id 不覆盖 / 容忍 undefined、null / 7 个调用点源码各含预期 needle / src 全域不得再出现裸 .stream(options) 或 .stream({ ...route, / 常量就是 "dsh-mneme" 且长度 1–64。

变异自证五轮,每处各自变红后复原:

变异 结果
撤掉 src/dream.js 接线 2 红
helper 改成恒等返回 1 红
撤掉 src/image-describe.js 接线 1 红
改成无条件覆盖 sessionId 1 红
撤掉 src/summarize.js 接线 1 红

全量 npm test:3407 tests / 3406 pass / 0 fail / 1 skipped;node scripts/check-sync.js ✓ 124 文件。

兼容性

  • 纯增量,无配置键变化、无 schema 变化、无默认值变化
  • 宿主已显式传 sessionId 的调用路径行为不变
  • 与后台上报的审计字段无交互(llm_audit_logs.session_key 仍为 NULL——那是另一件事)

已知边界(本 PR 不修)

Summary by CodeRabbit

  • Bug 修复
    • 修复后台 AI 请求因缺少会话标识而失败的问题,提升梦境处理、内容总结、图像描述及连接测试等功能的可靠性。
    • 自动补充后台请求所需的会话标识;已有会话标识时保持不变。

Copilot AI lite review requested due to automatic review settings November 12, 2026 06:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

新增固定后台会话标识及选项包装函数。后台 LLM 流调用使用该函数补充缺失的 sessionId,并新增测试验证函数行为和调用点接线。

Changes: 后台 LLM 会话

Layer / File(s) Summary
会话包装函数与测试
dsh-mneme/src/llm-session.js, dsh-mneme/lib/llm-session.js, dsh-mneme/test/llm-session.test.js, dsh-mneme/CHANGELOG.md
新增 BACKGROUND_SESSION_ID 和 withBackgroundSession(options)。当选项对象已有真值 sessionId 时,函数原样返回;否则浅拷贝对象并补上固定标识。测试覆盖无效输入、已有标识、输入对象未修改及调用点接线。
梦境、睡眠与后台任务流
dsh-mneme/src/dream.js, dsh-mneme/lib/dream.js, dsh-mneme/src/dream/sleep.js, dsh-mneme/lib/dream/sleep.js, dsh-mneme/src/summarize.js, dsh-mneme/lib/summarize.js, dsh-mneme/src/pre-inject-gate.js, dsh-mneme/lib/pre-inject-gate.js
这些模块的 LLM 流调用通过 withBackgroundSession 包装选项。摘要调用仍按原有逻辑处理 reasoningEffort。
连通性、实体与图像流
dsh-mneme/src/api.js, dsh-mneme/lib/api.js, dsh-mneme/src/index.js, dsh-mneme/lib/index.js, dsh-mneme/src/image-describe.js, dsh-mneme/lib/image-describe.js
连通性探测、实体提取和图像描述的 LLM 流调用通过 withBackgroundSession 包装请求参数;其他请求参数及流处理逻辑保持不变。

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • slow-stack/mneme#668:该 issue 描述后台 LLM 请求缺少 sessionId 时返回 MissingSessionID 400;本次变更为后台调用补充固定标识。issue 中关于 kbEnabled 的问题不在本次变更范围内。

Suggested reviewers: anans-amarre, heptaspirit

Merge Risk: Minimal · up to d8cfa

Background LLM calls now carry a fixed session id, which should prevent HTTP 400 responses from OpenCode-style gateways. No concrete defect was found in the change. The one open question is how external hosts treat a shared id, and there is no evidence it causes problems.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix(llm): 后台调用统一带固定 session id,OpenCode 路由不再 400 (#668)”与变更内容一致。变更新增 src/llm-session.js,导出 BACKGROUND_SESSION_ID 和 withBackgroundSession,并把后台 llm.stream 调用点(dream、sleep、index、summarize、pre-inject-g…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

标题“fix(llm): 后台调用统一带固定 session id,OpenCode 路由不再 400 (#668)”与变更内容一致。变更新增 src/llm-session.js,导出 BACKGROUND_SESSION_ID 和 withBackgroundSession,并把后台 llm.stream 调用点(dream、sleep、index、summarize、pre-inject-gate、api、image-describe)统一包装,以解决 OpenCode 路由因缺少 sessionId 返回 400 的问题。标题简短、具体,说明了主要变更和修复目标。标题中的编号 #668 与 PR 目标中提到的 #670 不一致,但这不影响标题与变更内容的相关性判断。

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between b470b3d and d8cfaf3.

Files selected for processing: 18

  • dsh-mneme/CHANGELOG.md
  • dsh-mneme/lib/api.js
  • dsh-mneme/lib/dream.js
  • dsh-mneme/lib/dream/sleep.js
  • dsh-mneme/lib/image-describe.js
  • dsh-mneme/lib/index.js
  • dsh-mneme/lib/llm-session.js
  • dsh-mneme/lib/pre-inject-gate.js
  • dsh-mneme/lib/summarize.js
  • dsh-mneme/src/api.js
  • dsh-mneme/src/dream.js
  • dsh-mneme/src/dream/sleep.js
  • dsh-mneme/src/image-describe.js
  • dsh-mneme/src/index.js
  • dsh-mneme/src/llm-session.js
  • dsh-mneme/src/pre-inject-gate.js
  • dsh-mneme/src/summarize.js
  • dsh-mneme/test/llm-session.test.js

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@codecov

codecov Bot commented Nov 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

2 participants