Skip to content

feat(groups): 补齐通用建群、置顶与群成员查询的 Daemon IPC 与 CLI 底座能力 - #3827

Merged
deepcoldy merged 3 commits into
masterfrom
wt/botmux-daemon-ipc-cli
Nov 25, 2026
Merged

deepcoldy merged 3 commits into
masterfrom
wt/botmux-daemon-ipc-cli

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

背景与目标

为官方及第三方无头插件服务提供免读 bots.json 密钥即可完成的通用群管理底座能力(建群协作与开场任务、群置顶、进群链接签发及群 Bot 成员 larkAppId 查询),全部保持平台无关、100% 向后兼容,且不涉及任何插件子系统文件。

改动内容

1. Daemon IPC & Dashboard POST /api/groups/create

  • 新增共享纯函数模块 src/core/group-ipc-options.ts(零 I/O,兼容单文件编译态),统一 Daemon IPC 与 Dashboard 的参数校验与转发准备逻辑:
    • 透出并校验 kickoffBotLarkAppId、kickoffPrompt(配对非空、在已选列表内、非 creator 自身)、ownerNotice(仅允许布尔值;默认或 true 沿用机器级 groupOwnerNotice 配置,显式传 false 静默跳过群主通知)、managerUserIds(严格校验 ou_*、去重、上限 50,并自动合入 userOpenIds 确保先入群再授管理员)以及可选 creatorLarkAppId。
    • 严格校验并归一化 larkAppIds(拒绝非数组/空数组/空白项/非字符串,trim 并保序去重),并对非对象 JSON(如 null / [])返回 400 bad_json。
    • Dashboard 选 creator 自动排除 kickoffBotLarkAppId 与 apiOnly Bot(支持可选 creatorLarkAppId 锁定且离线时不静默回退到其他 Bot 的应用作用域),并在转发体附带 creatorLarkAppId;Daemon IPC 校验 creatorLarkAppId 及 larkAppIds.includes(cachedLarkAppId),不匹配返回 409 bot_mismatch(带 accepted: false),防 IPC 端口复用跨 Bot 错投;apiOnly 守护进程前置拦截返回 400 lark_transport_disabled(带 accepted: false)。
    • Dashboard 代理异常捕获返回 502 { ok: false, error: 'group_create_proxy_failed' },与兄弟建群路由一致。
    • 响应完整透传 CreateGroupResult(含 chatId、shareLink、shareLinkError、kickoffMessageId、kickoffError、managersAdded、managerError、notifyMessageId 等)。
  • 修复 src/services/group-creator.ts(createGroupWithBots)群管理员授予与群主转让时序:先在 creator 仍是群主时调用 addChatManagers(避免转群主后被飞书返回 232025),仅在群主转让失败时回退将 transferOwnerTo 设为管理员;同时统一使用 trim 后的 creatorLarkAppId 与去重后的 larkAppIds。

2. CLI botmux create-group

  • --json-status 结构化完成状态新增 shareLink 与 shareLinkError 字段(shareLinkError 不反转 success,建群后异常回退 JSON 同步补齐为 null);默认不传 --json-status 仍严格保持单行 chatId 历史契约。
  • --kickoff-bot(src/cli/create-group-resolver.ts)按 ou_* 与 cli_* 严格前缀分区同时兼容 botOpenId 与 larkAppId:传 cli_* 时无需依赖 bots-info.json,畸形值、显示名、重复 botOpenId 或被污染的条目均 fail-closed。
  • 新增单趟左到右参数解析 parseCreateGroupCliArgs,避免正文含 flag 字样被误拦截,并拦截缺失值、空白 --kickoff-bot/--kickoff-prompt 及尾部裸重复 flag,同时保持 create-group owner-notice --help 行为兼容。

3. Daemon IPC 暴露群置顶与进群链接 + CLI 无头模式

  • 新增 POST /api/groups/:chatId/top-notice(复用 putChatTopNotice)与 POST /api/groups/:chatId/share-link(复用 getChatShareLink):
    • 复用服务端 Trusted-Host HMAC 鉴权(绑定 METHOD + PATH + boundPort)、requireBoundBot(503)、readJsonBodyOrBadJson(16 KiB 上限 + 400)、严格白名单键校验、可选 larkAppId 409 防复用、apiOnly 400、实时在群校验 isCurrentBotInChat(403 bot_not_in_chat / 502)及消息归属校验(404 message_chat_unavailable / 409 message_chat_mismatch)。
    • 路由参数 decodeURIComponent 捕获 URIError 统一返回 400 invalid_url_encoding(含 Dashboard members-display 代理)。
  • botmux top-notice set(src/cli/chat-top-notice-command.ts、src/cli.ts)新增可选 --bot <cli_xxx>(须同时指定 --chat-id <oc_xxx>,禁止与 --session-id 混用):无需会话上下文也不读 bots.json,通过目标 Bot 的 Daemon IPC 完成置顶并核验回执;若运行于静默消息监听会话内则 fail-closed 拦截(忽略被篡改的 BOTMUX_LARK_APP_ID,并输出干净错误而非堆栈)。原有会话上下文路径保持不变。

4. GET /api/groups/:chatId/members-display 补带 larkAppId

  • src/services/groups-store.ts(ChatMemberDisplay / listChatMemberDisplays)与 src/dashboard/web/roles.ts(GroupMemberDisplay)新增可选字段 larkAppId?: string,仅为已匹配到本机配置的 Bot 条目输出,外部 Bot 与人类成员不输出该键(零破坏性)。
  • 修复 /members 跨页分页重复 openId 去重及空 displayName 覆盖已有姓名问题;GET /api/groups/:chatId/members-display 在 selfOpenId 未就绪或回退 appId 场景下增加受保护的 self: true 兜底,且不误标同名外部 Bot。

影响范围评估

  • 平台:纯 TypeScript / Node HTTP / 飞书 SDK 封装,无平台特定路径或 shell 假设,Linux 与 macOS 行为一致;单文件编译态无 __dirname / import.meta.url 资源读取。
  • CLI 与会话类型:未改动任何 adapters/cli/ 或 adapters/backend/;原有 botmux top-notice set 会话路径、botmux create-group 默认单行 stdout 及既有 POST /api/groups/create 调用方 100% 保持原行为;src/services/riff-host-tools.ts 保持未放行 --bot / --chat-id。
  • 并行改动防冲突:未触碰 src/core/plugins/* 及 Dashboard 插件页文件。

对抗复核与问题清单

提交前针对权限边界、状态时序与 CLI 解析做了多维对抗复核,发现并随本 PR 一并修复的问题如下:

  1. 高危|cmdTopNotice --bot 静默监听器守卫在 BOTMUX_LARK_APP_ID 被篡改时 fail-open:隔离探针复现改写 BOTMUX_LARK_APP_ID 导致 resolveSessionById 返回失败从而跳过拦截。已改为忽略环境 BOTMUX_LARK_APP_ID、解析失败直接 fail-closed,并移除 --bot 路径的 riffModeSession 兜底与修补空白 BOTMUX_CHAT_ID 判断,补齐回归用例。
  2. 高危|createGroupWithBots 在 transferGroupOwner 之后调用 addChatManagers:POST /api/groups/create 同时传入群主转让与 managerUserIds 时,已降权 creator 调用飞书 add_managers 会触发 232025。已调整为转让前授非群主管理员,转让失败时再回退授目标为管理员,并以有状态 mock 验证调用顺序。
  3. 高危|POST /api/groups/create 缺少绑定 Bot 端口复用防错投及 apiOnly 前置拦截:已补充 creatorLarkAppId 校验、larkAppIds.includes(cachedLarkAppId) 409 拦截、isBoundBotApiOnly 400(accepted: false)与 Dashboard 选 creator 排除 apiOnly。
  4. 中危|create-group 扁平参数扫描吞后续 flag / 忽略空白 --kickoff-bot 与尾部重复 flag:隔离探针复现 --kickoff-prompt --json-status 与 --kickoff-bot "" 绕过校验。已替换为单趟左到右解析器 parseCreateGroupCliArgs 并补齐单测与 CLI 子进程测试。
  5. 中危|createGroupWithBots 局部未完全替换 trim 后 creatorLarkAppId、Dashboard 未自动邀请 managerUserIds、members-display 跨页去重与畸形 URL 编码返回 500:已全部修补并补齐覆盖。

测试与变异验证

  • 构建与静态审计:
    bun run build
    结果:通过(audit:domains 零私域主机名、tsc + typecheck 零错误、dashboard:bundle、audit-dist、audit-embedded-assets --require-dist 全部通过)。
  • 单文件编译态验证:
    bun run verify:binary
    结果:botmux-linux-x64 编译通过,15/15 smoke 检查全部通过。
  • Vitest 单元与真实 HTTP / CLI 子进程集成测试:
    bunx vitest run --project unit \
      test/group-ipc-capabilities.test.ts \
      test/dashboard-groups-create-route.integration.test.ts \
      test/groups-store-members-display.test.ts \
      test/create-group-resolver.test.ts \
      test/chat-top-notice.test.ts \
      test/cli-send-listener-silent.test.ts \
      test/dashboard-ipc-bound-bot-guard.test.ts \
      test/dashboard-ipc-bad-json.test.ts \
      test/group-creator.test.ts \
      test/groups-store.test.ts \
      test/dashboard-ipc.test.ts \
      test/dashboard-groups-redesign.test.ts \
      test/dashboard-session-write-origin.test.ts \
      test/api-only-mode-wiring.test.ts \
      test/task-routing-executor-store.test.ts \
      test/team-group.test.ts \
      test/test-hygiene.test.ts
    结果:17/17 文件通过(918 passed,含真实启动 src/index-dashboard.ts 与 src/cli.ts 子进程的端到端测试)。
  • Bun 运行时单文件隔离测试:
    test/group-ipc-capabilities.test.ts(17/17)、test/dashboard-groups-create-route.integration.test.ts(1/1)、test/groups-store-members-display.test.ts(4/4)、test/create-group-resolver.test.ts(42/42)、test/dashboard-ipc-bound-bot-guard.test.ts(98/98)、test/dashboard-ipc-bad-json.test.ts(46/46)全部通过(208 passed)。
  • 变异验证(12/12 守卫摘除全部咬红):
    在 isolate-run 隔离环境中逐一摘除或反转 12 处核心守卫(ownerNotice: false 透传、creatorLarkAppId 409、isBoundBotApiOnly 前置拦截、转群主前 addChatManagers、Dashboard 排除 kickoffBotLarkAppId、/top-notice 在群校验、/share-link 409、CLI shareLink 透传、resolveKickoff cli_* 分支、listChatMemberDisplays larkAppId、top-notice --bot 静默监听器拦截、IPC 回执 larkAppId 校验),12/12 均触发目标测试套件失败退出并无损恢复。

🤖 Generated with Claude Code

- Daemon IPC & Dashboard POST /api/groups/create:抽离共享纯校验与准备层(src/core/group-ipc-options.ts),透出并校验 kickoffBotLarkAppId、kickoffPrompt、ownerNotice(显式 false 静默跳过群主 @ 通知,默认/true 沿用全局配置)、managerUserIds(ou_* 校验、去重并自动合入邀请列表)及可选 creatorLarkAppId;Dashboard 选 creator 自动排除 kickoffBotLarkAppId 与 apiOnly Bot,并向 Daemon IPC 透传 creatorLarkAppId 防止端口复用跨 Bot 错投;修复 createGroupWithBots 先转群主再授管理员导致飞书 232025 的时序问题,完整返回 CreateGroupResult。
- CLI botmux create-group:--json-status 补齐 shareLink 与 shareLinkError 字段;--kickoff-bot 按前缀分区无歧义同时支持 botOpenId(ou_*)与 larkAppId(cli_*,不依赖 bots-info.json);抽离单趟参数解析 parseCreateGroupCliArgs 修复吞参数、空白值忽略与尾部重复 flag 边界。
- Daemon IPC 暴露 POST /api/groups/:chatId/top-notice 与 POST /api/groups/:chatId/share-link,复用 putChatTopNotice / getChatShareLink 及现有 Trusted-Host HMAC、requireBoundBot、apiOnly、实时在群与跨群消息校验;botmux top-notice set 新增 --bot <cli_xxx>(须配 --chat-id)无会话直连目标 Bot Daemon IPC 并对静默监听会话 fail-closed。
- GET /api/groups/:chatId/members-display(listChatMemberDisplays)为已配置 Bot 条目补带可选 larkAppId 字段,修复跨页去重与空 displayName 覆盖,并增强 self 标记启动窗口兜底;统一处理路由参数非法 percent-encoding 返回 400 invalid_url_encoding。

Co-Authored-By: Claude Code <[email protected]>

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

复审结论

当前本地审查 HEAD 为 0fd6003ffc(对应原 PR head 20148d713b0d,patch-id 一致)。发现 2 个 P2 正确性/兼容性问题,建议处理后再合入。新 IPC 路由的 trusted-host HMAC、bound-bot、实时在群与消息归属校验链路未发现绕过。

P2-1:members-display 在 openId 冲突时会把成员标成另一个本机 Bot

位置:src/services/groups-store.ts:789-798,尤其是 :792 的 botLarkAppId || prev.larkAppId。

该合并规则对“configured + external”碰撞能保留 configured 身份,但对“两个不同的非空 configured larkAppId 碰到同一 openId”会让后行静默覆盖前行。这个输入可由真实 discovery 路径产生:src/im/lark/client.ts:3684-3720 对每个 configured Bot 都使用同一个 name-keyed crossHit,同名 Bot 因而可得到相同 openId;legacy discovery 没有去重这两个 configured 行。

临时 Vitest 探针走真实 listChatBotMembers → listChatMemberDisplays,条件为两个同名且都在群内的本机 Bot、bot-openids-cli_self.json 中 Twin → ou_shared、关闭实验性 /members/bots:

discovery = [
  { larkAppId: "cli_self", openId: "ou_shared" },
  { larkAppId: "cli_peer", openId: "ou_shared" }
]
display   = [
  { openId: "ou_shared", name: "Twin", memberType: "bot", larkAppId: "cli_peer" }
]

即 observer/self 的行被错误标成 cli_peer。这直接破坏本 PR 新增的“查询群 Bot 的 larkAppId”能力;src/core/dashboard-ipc-server.ts:11092-11098 的 self 唯一性判断又发生在合并之后,无法识别这类冲突。

建议:按 openId 跟踪不同的非空 configured appId;一旦出现两个不同 appId,身份结论应为 ambiguous,至少省略 larkAppId,不要 last-write-wins。保留 configured + external(空 appId)时 configured 优先的现有行为。回归测试应覆盖真实 discovery 到 display 的链路,而不只 mock 已唯一的 Bot 行。

P2-2:/api/groups/create 破坏 legacy implicit-creator 调用形态

位置:src/core/dashboard-ipc-server.ts:14576-14596。

基线路由只要求 larkAppIds 是字符串数组,接受:

  • larkAppIds: []:bound creator 自己建 creator-only 群;
  • larkAppIds: ["cli_peer"]:bound creator + 一个 peer。

createGroupWithBots 的服务契约至今仍明确允许空数组(src/services/group-creator.ts:45-46),creator 由 creatorLarkAppId 隐式加入;test/group-creator.test.ts:1086-1093 也覆盖 creator-only 形态。当前 IPC 则分别返回 400 larkAppIds_required 和 409 bot_mismatch;后者已被 test/group-ipc-capabilities.test.ts:373-380 固定成新行为。

这与本 PR“默认/旧调用保持兼容”的目标冲突。端口复用防护不能从 legacy 请求中凭空得到额外身份意图;建议保留未传 creatorLarkAppId 时的 implicit-creator 兼容路径(新 Dashboard/headless 调用继续显式绑定并校验),或把该破坏作为明确迁移设计处理,而不是静默改变既有 IPC 行为。

对首审测试缺口的复核:安全定性应降级

src/cli.ts:10536-10545 的 daemon-only fallback 当前确实没有现成 fixture 覆盖,但删除 fallback 或把 cleanEnv 改回 process.env,不会造成静默监听 fail-open,因为 !originSession 的最终拒绝仍然成立。

fenced 子进程探针使用“本地无会话行、daemon 返回来源会话、毒化 BOTMUX_LARK_APP_ID=cli_fake”:

场景 当前代码 删除 fallback / 保留毒化 appId
daemon 返回 silent 来源,目标为来源群 exit 1,专门拒绝,0 写入 exit 1,来源无法校验的保守拒绝,仍 0 写入
daemon 返回普通来源 exit 0,1 次合法 IPC 写入 exit 1,误拒,0 写入
daemon 无来源行 exit 1,0 写入 exit 1,0 写入

所以这里应视为正向可用性与专门诊断分支的测试缺口,不是安全守卫失效。仅 seed 一个 fallback-only silent 行再断言“拒绝/零写入”,删除 fallback 仍会变绿;建议增加 daemon-only 普通来源可执行的正向用例,并对 silent 来源断言专门拒绝原因。

IPC 鉴权链路核查

  • 生产 daemon 在 src/daemon.ts:36244-36249 固定 authRequired: true;新路由不属于公开/窄能力白名单。
  • 服务端统一在路由分派前校验 method + pathname + bound port 的 HMAC(src/core/dashboard-ipc-server.ts:14884-14915)。
  • 两个新写路由均经过 requireBoundBot、16 KiB JSON 上限、白名单键与 ID 校验、可选 larkAppId 409、apiOnly 400、实时在群 403/502;top-notice 另有消息归属 404/409。
  • 上述 P2-2 是 legacy 请求兼容性问题,不是 HMAC 绕过。

本地验证

  • bun run typecheck:通过。
  • test/cli-send-listener-silent.test.ts、test/group-ipc-capabilities.test.ts:47/47 通过。
  • test/groups-store-members-display.test.ts、test/list-chat-bot-members.test.ts、test/create-group-resolver.test.ts、test/chat-top-notice.test.ts:89/89 通过。
  • 两个临时对抗探针结果如上;探针与源码变异均已清理,工作树干净。

- 同一 openId 被不同 configured Bot 命中时省略 larkAppId,避免身份覆盖与 self 误判
- 未显式传 creatorLarkAppId 时恢复空数组及 peer-only 列表的 implicit creator 契约,显式调用保留 bot_mismatch 防护
- 补充 daemon-only 普通来源的正向置顶回归,确保来源解析 fallback 的可用性被测试覆盖

Co-Authored-By: Kimi Code <[email protected]>
@deepcoldy

Copy link
Copy Markdown
Owner Author

修复已作为追加提交推送到新 head 9fc8eda1405232aed0e110a9d2c2acb905e8e39c,parent 为原 head 20148d713b0df5f58b6adae37f21224f0e8c611f,未 force-push。

  • 成员身份冲突:同一 openId 出现不同非空 configured appId 时持续按 ambiguous 处理并省略 larkAppId;configured + external(空 appId)的既有合并行为不变。
  • 隐式 creator:未传 creatorLarkAppId 时恢复空数组和 peer-only 数组;显式 creator 的精确身份及 roster membership 409 防护保留,并覆盖显式空数组/peer-only 两种拒绝。
  • 新增 daemon-only 普通来源的正向 top-notice --bot 用例:本地无会话行且 BOTMUX_LARK_APP_ID 毒化时仍能经 resolveSessionById 放行。

验证:

  • bun run build:通过(含 typecheck、dashboard bundle、dist/embedded-assets 审计)。
  • PR 相关 Vitest:17 文件,920 passed / 3 skipped。
  • 修改套件的 Bun 运行时复验:23 passed / 0 failed。
  • 变异复验:恢复 last-write-wins、移除 allowEmpty、把 creator 校验扩展到 legacy 请求、删除 daemon-only fallback,4 项均使对应测试咬红;源码已还原,最终工作树干净。

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

同步增量复审结论

a0aba44598bcd4c65591d7541404b44de1b7a020 的 master 同步增量未发现新的阻断问题,代码与树层面可通过。这不替代仍在等待的 G4 CI 首跑全绿。

独立核验:

  • 远端 head 与本地 OID 一致;parents 依次为 9fc8eda140…、749fb2d600…,未改写原提交。
  • 实际树 3d1849bca68702c4299219d742e6eaa04cab2802 与重新执行 git merge-tree --write-tree 9fc8eda140 749fb2d600 的结果逐字一致。
  • 同步前后整段 PR patch-id 均为 6c045acc32fa2667d1a5b4132ff6c1cbb06d248a。
  • 从共同祖先 502c250981 分别比较 master 与 PR 的 hunk:src/cli.ts、src/core/dashboard-ipc-server.ts、src/services/groups-store.ts 均无重叠或相邻修改。
  • 语义共存检查:createChat 保持无 UUID 的兼容包装;master 新增 collab 鉴权开口位于独立的 /__collab-ipc/v1 命名空间,不覆盖本 PR /api/groups/*;cmdSend 的 sourceTurnId 增量不进入 top-notice / create-group 路径。
  • 依赖文件无变化;PR-only delta 的 git diff --check 通过。

在合并树上实际运行:

  • 相关 9 个测试文件:153 passed / 6 skipped(含 PR 的建群、成员身份冲突、daemon-only 正向用例,以及 collab/cross-chat/HC 邻近回归)。
  • bun run build:通过,含 typecheck、dashboard bundle、dist 与 embedded-assets 审计。

审查时新 head CI 尚未完成,未将本地结果计入 G4;等待首跑结果即可。

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

二次同步合并树背书

精确树 06bb1a004afaf5315992ce1eef2500d9604bf499 可合,未发现新的阻断问题。 本背书只适用于该 tree;若落地时 squash commit 的 tree 不同,需要重新核验,不能沿用本结论。

独立核验:

  • 本地合并提交 f861e3e174ceeca69d230c56e5779f77c0618f0a 的 parents 为 a0aba44598… 与 18e6b14d0d…,其 tree 正是 06bb1a004a…。
  • 重新执行 git merge-tree --write-tree 18e6b14d0d a0aba44598,结果同为 06bb1a004a…。
  • PR 自身 patch-id 保持 6c045acc32fa2667d1a5b4132ff6c1cbb06d248a。
  • 新 master delta 与 PR 在 src/cli.ts、src/dashboard.ts 的 hunk 均不重叠、不相邻;依赖文件无变化。
  • 语义检查未发现交叉影响:Dashboard 新增前置处理器只接受 /oncall/sidebar/,不会截获 /api/groups/*;cmdSend 的原话题投递约束不进入本 PR 的 top-notice / create-group 实现。
  • PR-only delta 的 git diff --check 通过。

在该精确树上实际运行:

  • PR 与 #3795/#3828 相关 12 个测试文件:301 passed / 0 failed。
  • bun run build:通过,含 typecheck、dashboard bundle、dist 与 embedded-assets 审计。
  • 另核验远端 a0aba44598 的现有 checks 均为 pass。

落地时请继续执行预定的 squash tree 等值检查;master 若再次前进,应停止并重新生成、验证目标树。

@github-actions

Copy link
Copy Markdown

@deepcoldy 合入 master 后的 Midscene E2E 失败:Feishu live Midscene 失败。

这次运行覆盖 350ce55…6389a341d(8 个提交),同批合入的还有 #3835 #3836 #3832 #3838 #3795 #3837 #3840,失败不一定由本 PR 引起。

用例 跑道 状态 失败信息
Thread /status and /close commands feishu-core-conversation failed Timed out after 1500000ms waiting for shared chat lease dm:Claude
Claude DM thread mode gives each message its own topic and session feishu-core-dm-modes-claude failed Reply "ACK-e2e-dmthread-claude-1-1795645386433 RECALL ACK-e2e-dmthread-claude-1-1795645386433" appeared 0 time(s) within 240000ms, expected 1 (clicked 直接开启会话 2×; re-opened 1×; repo card buttons 0 vis…

@github-actions

Copy link
Copy Markdown

🚀 Released in v3.59.34

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.

1 participant