Skip to content

fix(botmux): 优化 CoCo 粘贴和多 bot 线程路由 - #23

Closed
ZBCccc wants to merge 1 commit into
deepcoldy:masterfrom
ZBCccc:fix/coco-tmux-peer-routing
Closed

ZBCccc wants to merge 1 commit into
deepcoldy:masterfrom
ZBCccc:fix/coco-tmux-peer-routing

Conversation

@ZBCccc

@ZBCccc ZBCccc commented May 20, 2026 •

Copy link
Copy Markdown

背景

排查 CoCo / Trae CLI 在 botmux tmux 模式下的多行输入提交链路时,发现有几类问题需要一起修复:

  1. CoCo 的全局 history.jsonl 路径是平台相关的:macOS 写入 ~/Library/Caches/coco/history.jsonl,Linux 写入 ~/.cache/coco/history.jsonl。此前 adapter 和测试都只按 Linux 路径处理,macOS 下会误读不到真实提交历史,影响提交确认、retry/failure 判断。
  2. CoCo 依赖 tmux paste-buffer 传递多行内容。普通 botmux tmux 会话当前实际走 TmuxPipeBackend,不是 legacy TmuxBackend;只改 TmuxBackend 会漏掉主路径。两套 backend 的 paste 行为需要保持一致。
  3. 多 bot 群聊中,已有线程后续回复是否允许免 @mention,不应该再由群内 bot 总数决定。真正需要 disambiguate 的场景是同一个 thread/chat conversation unit 已经有 peer bot active session;如果当前 bot 是唯一 session owner,用户在该线程内继续无 @mention 回复应能路由回当前 bot。
  4. Copilot review 指出 hasPeerSession 在 group follow-up 热路径上同步扫描 session 文件。已补充 daemon 侧短 TTL 缓存,避免同一 conversation unit 的高频消息重复触发 readFileSync + JSON.parse 扫描。

改动

  • src/adapters/cli/coco.ts
    • CoCo history path 改为平台相关:macOS 使用 ~/Library/Caches/coco/history.jsonl,其他平台保持 ~/.cache/coco/history.jsonl。
    • 更新注释,避免继续写死 Linux 路径。
  • src/adapters/backend/tmux-backend.ts
    • pasteText() 的 tmux paste-buffer 增加 -p,在应用启用 bracketed paste 时插入 paste 边界控制码。
  • src/adapters/backend/tmux-pipe-backend.ts
    • 同步 pasteText() 的 paste-buffer -p,覆盖当前普通 tmux 会话实际使用的 backend。
  • src/im/lark/event-dispatcher.ts / src/daemon.ts
    • 新增 hasPeerSession handler。
    • group message permission gating 改为:当前 bot owns session 且发送人 allowed 且没有 peer session 时,允许免 @mention;存在 peer session 时仍要求 @mention。
    • hasPeerSession 通过 PeerSessionCache 做 1s TTL 缓存,按 (scope, rootMessageId/chatId) 缓存跨 bot session-store 查询结果。
  • src/core/peer-session-cache.ts
    • 新增 daemon 侧 peer-session TTL cache,缓存 session-store 同步扫描结果,并按当前 app id 计算是否存在 peer。
  • 测试
    • test/write-input.test.ts 使用与 adapter 一致的平台相关 CoCo history path,保留 retry/failure 回归用例。
    • test/tmux-pipe-backend.test.ts 覆盖 paste-buffer -d -p。
    • test/event-dispatcher.test.ts 覆盖 sole owner 免 @mention 与 peer owner 需要 @mention 两条路径。
    • test/peer-session-cache.test.ts 覆盖 TTL 内复用查询、TTL 过期刷新、chat-scope 按 chatId 查询且排除当前 app。

排查结论

本次改动中 review 发现的关键问题已修复:

  • CoCo 测试 helper 如果仍写旧路径,会让 adapter 在 macOS 下误走 fresh-install shortcut,掩盖 retry/failure 回归。
  • paste-buffer -p 必须同步到 TmuxPipeBackend,否则正常 botmux tmux 会话路径不会得到 bracketed paste 修复。
  • hasPeerSession 不能每次 group follow-up 都同步扫 session 文件;现已在 daemon 侧增加 1s TTL 缓存,降低热路径阻塞风险。TTL 很短,跨 daemon session 变化最多短暂延迟;同一 conversation unit 高频消息不会重复扫盘。

验证

  • pnpm exec tsc --noEmit ✅
  • pnpm vitest run test/peer-session-cache.test.ts test/event-dispatcher.test.ts ✅ 60 tests
  • pnpm vitest run test/tmux-pipe-backend.test.ts ✅ 26 tests
  • pnpm vitest run test/write-input.test.ts ✅ 68 tests
  • git diff --check ✅

备注:本机 Node 为 v20.20.2,pnpm 会提示项目期望 node >=22,但上述验证命令均已通过。

@ZBCccc
ZBCccc requested a review from deepcoldy as a code owner May 20, 2026 08:01
Copilot AI review requested due to automatic review settings May 20, 2026 08:01

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.

Pull request overview

该 PR 针对 botmux 在 tmux 模式下的 CoCo 多行粘贴提交确认、以及多 bot 群聊线程内的路由/免 @mention 规则做了修复与一致性补齐,以提升 macOS 兼容性与多 bot 协作体验。

Changes:

  • 按平台修正 CoCo history.jsonl 路径(macOS vs Linux),并同步更新相关测试。
  • 统一 TmuxBackend 与 TmuxPipeBackend 的 pasteText() 行为:tmux paste-buffer 增加 -p 以配合 bracketed paste。
  • 多 bot 群聊权限/路由 gating 改为基于“是否存在 peer session”而非群内 bot 数量,并新增对应测试覆盖。

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/adapters/cli/coco.ts CoCo 提交确认读取的 history 路径改为平台相关,并更新注释说明。
src/adapters/backend/tmux-backend.ts pasteText() 的 tmux paste-buffer 增加 -p 以插入 bracketed paste 标记。
src/adapters/backend/tmux-pipe-backend.ts 同步 paste-buffer -p,保证 adopt/pipe backend 行为一致。
src/im/lark/event-dispatcher.ts 引入 hasPeerSession 并调整群聊免 @mention 的 relax 判定逻辑。
src/daemon.ts 在 daemon 侧实现 hasPeerSession,用于检测同会话单元是否有其他 bot 会话。
test/write-input.test.ts 测试中使用与 adapter 一致的平台相关 CoCo history 路径。
test/tmux-pipe-backend.test.ts 覆盖 paste-buffer 调用参数包含 -d 与 -p。
test/event-dispatcher.test.ts 增加 sole owner 可免 @mention 与 peer session 必须 @mention 的覆盖。

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/daemon.ts Outdated
Comment on lines +1183 to +1187
hasPeerSession: (anchor, appId, scope, chatId) => {
const peers = scope === 'thread'
? sessionStore.findActiveSessionsByRoot(anchor)
: sessionStore.findActiveChatScopeSessionsByChat(chatId);
return peers.some(s => s.larkAppId && s.larkAppId !== appId);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in b691be0. Added PeerSessionCache with a 1s TTL keyed by (scope, rootMessageId/chatId) and changed the daemon hasPeerSession handler to read through that cache instead of synchronously scanning session files on every owned group message. Added test/peer-session-cache.test.ts covering TTL reuse, refresh after expiry, and chat-scope lookup behavior; reran tsc, dispatcher, tmux-pipe, and write-input tests.

1. tmux-backend pasteText 添加 -p 标志强制 bracketed-paste 标记
2. coco.ts HISTORY_PATH 适配 macOS ~/Library/Caches 路径
@deepcoldy

Copy link
Copy Markdown
Owner

感谢 @ZBCccc 的排查与修复!🙏

这个 PR 的实际改动(coco.ts 平台相关路径 + tmux-backend.ts 的 paste-buffer -p)与 #25 完全一致,而 #25 还多带了配套的 test/write-input.test.ts 同步更新(否则 macOS 下测试会误走 fresh-install shortcut)。因此已合并 #25 作为完整版。

另外提醒:本 PR 的描述里提到的 peer-session 路由 / peer-session-cache.ts / event-dispatcher / daemon 缓存等改动,实际并不在本分支的 diff 里(分支只有 CoCo 这一处修复)。如果那部分工作确实想提交,欢迎单独开一个 PR。

本 PR 作为 superseded 关闭,改动已通过 #25 进入 master。再次感谢!

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.

3 participants