Skip to content

fix: 保留切换仓库后的普通群会话作用域 - #529

Merged
deepcoldy merged 2 commits into
deepcoldy:masterfrom
Barrierml:fix/preserve-chat-scope-on-repo-switch
Jul 21, 2026
Merged

deepcoldy merged 2 commits into
deepcoldy:masterfrom
Barrierml:fix/preserve-chat-scope-on-repo-switch

Conversation

@Barrierml

Copy link
Copy Markdown
Contributor

问题现象

普通群使用 chat scope 时,先通过 /repo(或仓库选择卡片)切换工作目录,再由会话内的 Claude Code 调用 botmux send,消息会发送失败。

典型错误链路是:BotMux 把 Lark 群 ID(oc_...)当作消息 ID,用 thread reply API 尝试回复,最终被 Lark 拒绝。daemon 重启后,受影响会话也会按错误的 thread scope 恢复。

复现步骤

  1. 在 Lark 普通群中启动一个 chat-scope BotMux 会话。

  2. 确认初始持久化 session 形态正常:

    {
      "chatId": "oc_xxx",
      "rootMessageId": "om_xxx",
      "scope": "chat"
    }
  3. 在群中执行 /repo 1;也可通过仓库选择卡片切换目录。

  4. 查看对应 sessions-<appId>.json,切换后重建的记录会变成:

    {
      "chatId": "oc_xxx",
      "rootMessageId": "oc_xxx"
    }

    即 scope 丢失,且 rootMessageId 被写成 chat routing anchor。

  5. 在该会话中执行 botmux send ...。CLI 读取 scope-less session 后按兼容逻辑将其视作 thread session,并尝试回复 rootMessageId = oc_xxx,从而发送失败。

根因

文本 /repo 与仓库选择卡片的 mid-session 切换路径都会调用 createSession() 重建 session:

  • chat scope 下传入的 rootId 实际是路由用的 chatId(oc_...),不是用于追踪的原始消息 ID(om_...);
  • 重建时没有传递 ds.scope;
  • createSession() 会立即保存,所以如果在调用后再补 session.scope,磁盘上仍存在先写入坏记录的窗口。

运行中的 daemon 仍持有 ds.scope = "chat",因此问题在当前内存路由中不明显;但 botmux send 和 daemon 重启恢复读取的是持久化 session,因而会触发错误。

修复方案

  • 扩展 createSession(),允许在首次落盘时原子写入 scope。

  • 文本 /repo 与仓库选择卡片重建 session 时:

    • 显式传递原有 ds.scope;
    • chat scope 保留旧 session 的原始 om_... rootMessageId;
    • thread scope 继续使用当前 rootId,保持原行为。
  • 加入窄范围历史数据修复,仅匹配以下已知坏记录:

    • scope === undefined;
    • chatId 以 oc_ 开头;
    • rootMessageId === chatId。

    对这些记录补上 scope: "chat"。普通 scope-less 旧版 thread session 不受影响。已经丢失的原始 om_... trace root 无法恢复,但 chat scope 实际按 chatId 路由,不影响消息投递。

  • 修复数据写回失败时,保留已经成功加载并在内存中修复的 session,避免 ENOSPC/EACCES 等错误将有效会话清空。

测试

新增/更新回归覆盖:

  • chat-scope 文本 /repo 保留 scope 与原始 trace root;
  • chat-scope 仓库选择卡片保留 scope 与原始 trace root;
  • 精确迁移已知坏记录;
  • 不迁移普通旧版 scope-less thread 记录;
  • 迁移写回失败时仍保留全部已加载 session。

验证结果:

  • 相关回归测试:267 项通过;
  • pnpm build:通过;
  • git diff --check:通过;
  • 独立只读代码审查:无发现。

全量单测在清洁环境中仍有 1 个可独立复现、与本 PR 无关的既有失败:test/v3-distillation-runner.test.ts 的 PID namespace 用例中 helper PID 为 0;本 PR 涉及的测试均通过。

🤖 Generated with Claude Code

@Barrierml
Barrierml requested a review from deepcoldy as a code owner July 20, 2026 01:27
@deepcoldy
deepcoldy force-pushed the fix/preserve-chat-scope-on-repo-switch branch from 29569df to d73c280 Compare July 20, 2026 19:18
@deepcoldy
deepcoldy force-pushed the fix/preserve-chat-scope-on-repo-switch branch from d73c280 to f71d4c7 Compare July 20, 2026 19:22

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

二审通过:rebase 到最新 master 后无冲突,repair 签名足够窄、load() catch 合理、createSession 可选 scope 兼容 master 其他调用点。tsc / pnpm build / 290 PR 相关测试 / 9777 完整 unit 全绿。不涉及隧道协议,platform/ 无需同步。

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