Skip to content

feat(lark): 平台团队 bot 免 /grant——union_id 自学、roster team-sync 落盘与闸门接入 - #343

Merged
deepcoldy merged 1 commit into
masterfrom
wt/claude-bot-grant-botmux
Jul 3, 2026
Merged

deepcoldy merged 1 commit into
masterfrom
wt/claude-bot-grant-botmux

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

背景

平台团队(中心化 botmux platform)拉的群里 bot 互 @ 仍弹 /grant 卡:平台拉群不经 daemon(platform dc677d1 起平台直建,旧链路的 daemon /api/groups/create 也不写 recordTeamGroup),信任根 team-groups/team-bots 两头落空;而学习 union_id 的入口(event-dispatcher recordTeamBot)又要求群先命中 team-groups——鸡生蛋,两条信任腿全断。

方案(与申晗讨论收敛)

平台每团队建一个 bot-only「机器人大厅」作 union_id 自学回声室 + 受信团队群;bot 自学 union_id 随心跳上报,平台聚合 roster 按版本比对下发;接收侧按团队成员关系落盘。平台团队与旧版联邦团队同权,双模式都免 /grant。

改动

  • bot-union-ids-store(新):bot 从自家群消息回声(isSelfMessage 分支)自学自己的 union_id 落盘(一次性)。这是唯一可靠来源——bot/v3/info 不返回 union_id、contact API 查不了 bot、单聊事件只推用户→bot。
  • platform-team-store(新):平台 team-sync(rev 版本比对,随隧道心跳应答)下发的团队 bot roster + 团队群清单落盘;群清单以 platform: 前缀镜像进 team-groups(replace-by-prefix,绝不动旧联邦条目),isTeamGroupChat / recordTeamBot 消费路径零改动。
  • 闸门:isTrustedTeamBotSender / canOperate 接入 isPlatformTeamBot(union_id 锚定;按团队成员关系增删、不设 30d 过期)。
  • evaluateTalk 新增 teamBot 腿 并在 enforceMessageQuotaForCliInput 透传 union_id:修复 feat(lark): 团队 bot 协作免 introduce/grant(按 union_id 与团队群信任) #332 在受限 bot 上的端到端断点——外部 bot 闸门放行的团队 bot 消息,此前会在配额复查处被 evaluateTalk 静默丢弃(evaluateTalk 只有 peer 腿,没有团队腿)。
  • 安全不变量:union_id 信任腿只对飞书盖章的 bot 发送方生效(teamTrustUnionId call-site 门)。roster 是成员机器自报的,不锁 sender_type 的话,恶意成员把真人 union_id 报成"自家 bot"即可让真人在全团队获得 talk+operate——与旧联邦"学习入口限 bot sender"对齐同一结构保证。
  • dashboard:tunnel-client 处理 team-sync 下行 + 上报 teamSyncRev / PlatformBotInfo.unionId;大厅打卡(无 union_id 的本地 bot 经 daemon IPC /api/platform/hall-announce 往大厅发一条,回声自学;10min 节流 + 每进程 6 次上限)。

配套平台侧已上线(platform master 7c26d8d):大厅建群(app_id 批 5 + 整批拒逐个重试、群主不转让、不拉人)、团队加 bot 时探测大厅已解散 → 自动重建重拉全部 bot、team-sync 聚合与版本比对下发、瞬时 DB 故障跳过本轮(绝不当空团队下发吊销全网信任)。

兼容与降级

  • 老 daemon 不上报 teamSyncRev → 平台不对它下发,完全向后兼容。
  • self-echo 事件不带 union_id / bot-only 群不推事件的 bot:自动退回大厅群 chatId 兜底腿(团队群内首次接触仍免 grant,接收方顺带学 union_id),不会比现状差。

验证

  • 新增 15 条单测(bot-union-ids-store / platform-team-store / platform-team-trust)全绿;
  • 全量 7280 条测试:30 个失败与干净基线(HEAD worktree 复跑)完全一致——19 个环境依赖单测 + 11 个需真实飞书的 e2e,零回归;
  • tsc + pnpm build 绿。

平台团队(中心化 botmux platform)拉的群里 bot 互 @ 一直要 /grant:平台拉群不经
daemon,信任根 team-groups/team-bots 两头落空(学习入口又要求群先受信,鸡生蛋)。
本改动打通平台模式的信任链,与旧版联邦团队同权——两条路都算团队模式、都免 /grant:

- bot-union-ids-store:bot 从自家群消息回声(isSelfMessage 分支)自学自己的
  union_id 落盘(一次性);随平台隧道 register/heartbeat 的 PlatformBotInfo 上报
- platform-team-store:平台 team-sync(rev 版本比对)下发的团队 bot roster +
  团队群清单落盘;群清单以 platform: 前缀镜像进 team-groups(replace-by-prefix,
  绝不动旧联邦条目),isTeamGroupChat/recordTeamBot 消费路径零改动
- 闸门:isTrustedTeamBotSender/canOperate 接入 isPlatformTeamBot(union_id 锚定,
  按团队成员关系增删、不设 30d 过期)
- evaluateTalk 新增 teamBot 腿并在 enforceMessageQuotaForCliInput 透传 union_id:
  修复受限 bot 上 #332 的端到端断点——外部闸门放行的团队 bot 消息此前会在配额
  复查处被 evaluateTalk 静默丢弃
- 安全不变量:union_id 信任腿只对飞书盖章的 bot 发送方生效(teamTrustUnionId
  call-site 门),防恶意成员把真人 union_id 报成 bot 骗取 talk/operate
- dashboard:tunnel-client 处理 team-sync + 上报 teamSyncRev;大厅打卡(无
  union_id 的本地 bot 往平台建的 bot-only 大厅发一条,回声自学;节流+上限)
- daemon IPC 新增 /api/platform/hall-announce

配套平台侧改动见 platform 仓同名分支(大厅建群/解散重建、team-sync 聚合下发)。
@deepcoldy

Copy link
Copy Markdown
Owner Author

Review result: LGTM from code review, but GitHub rejected an approval from this CLI auth because the authenticated account is the PR author: Review Can not approve your own pull request.\n\nI focused on the four requested areas:\n\n- event-dispatcher gates: team trust is wired into the foreign-bot routing gate and the quota recheck path via evaluateTalk; canOperate only uses union_id-backed team bot predicates, not the chat-level team group predicate.\n- daemon union_id call sites: the new teamTrustUnionId/threadTeamTrustUnionId plumbing is gated to bot/app sender types, with the thread fallback only for already recognized foreign bots, so human sender union_ids are not passed into the teamBot operate/talk legs.\n- replaceTeamGroupsByPrefix: platform mirroring is isolated under the platform: prefix and preserves legacy federation team-groups; the added tests cover replace/removal and legacy preservation.\n- evaluateTalk teamBot leg: it grants talk only, produces no quotaKey, and does not affect canOperate except through the separate union_id-backed canOperate predicate. That matches the intended peer-bot unlimited talk semantics.\n\nVerification:\n- pnpm vitest run test/platform-team-trust.test.ts test/platform-team-store.test.ts test/bot-union-ids-store.test.ts test/team-operate.test.ts test/team-peer-trust.test.ts test/grant-gates.test.ts: 34 tests passed.\n- pnpm exec tsc --noEmit: passed.\n- pnpm test: unrelated existing/environment failures in terminal-url/recall-frozen-cards due WEB public URL override (received https://m-ff27a1b1e45b4504.botmux.bytedance.net/... instead of expected local http://10.37.110.93:port URLs); PR-related tests passed in that run.

@deepcoldy
deepcoldy merged commit 0008edf into master Jul 3, 2026
1 check passed
@deepcoldy
deepcoldy deleted the wt/claude-bot-grant-botmux branch July 3, 2026 00:57
deepcoldy added a commit that referenced this pull request Jul 3, 2026
#343 设计里 bot 自学 union_id 唯一入口是自家群消息回声(isSelfMessage),
但回声要求应用有 receive-all 群消息 scope,且 bot-only 机器人大厅实测
完全不推任何消息事件(打卡消息发出后全体 daemon 0 接收)——自学链路
在生产上从未走通,roster 永远没有 union_id,平台团队免 /grant 落空。

飞书给消息事件 mentions[] 里每个被 @ 实体都盖了 union_id,且 @ 驱动的
事件投递不需要额外 scope:bot 在任何群被 @ 一次(用户日常 @ 即可)就能
学到自己的 union_id。新增 recordBotUnionIdFromMentions(严格按 open_id
匹配自己;app_id 形态 mention 不带 union_id,忽略),event-dispatcher
在处理 mentions 时顺带调用。回声分支保留作兜底。
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