Skip to content

feat(assistant): assistantGroupChat 控制助理模式 bot 是否在多人群工作(默认只服务单人群) - #3871

Merged
deepcoldy merged 7 commits into
deepcoldy:masterfrom
xu4wang:feat/assistant-multi-user-group
Nov 26, 2026
Merged

deepcoldy merged 7 commits into
deepcoldy:masterfrom
xu4wang:feat/assistant-multi-user-group

Conversation

@xu4wang

@xu4wang xu4wang commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

背景

个人助理模式(#3652)下,一个群归一个主人:群里每个人的发言都进主人的会话,也都能借助理看到主人让它记住的内容。主人把同事拉进群,就等于把自己的助理空间分享给了他们。对于全员可私聊、主要处理个人数据的 bot(财务对账、个人邮件、私聊汇总),这个默认值太宽了。

改动

新增 bot 配置 assistantGroupChat(布尔,默认关,/botconfig 和 dashboard 都能即时改)。只在 assistantMode 开着时有意义。

关着时,助理模式 bot 只在恰好有 1 个真人的群里工作(bot 不计入人数,用 chat.get 的 user_count 判断):

  • 群消息(event-dispatcher,在群上下文采集、命令、认领、建会话之前拦截):
    • 群里真人 ≥2 时,整条消息不处理。
    • 真人 @ 本 bot 时回一句「助理模式不支持多人群……」,同一个群 10 分钟最多回一次;其余消息不出声。
    • 人数查不到,或返回里缺少 user_count,都按多人群处理。
  • 人数缓存:
    • 缓存说是单人群时一律现查,防止有人刚进群、而进群事件推给了别的副本。
    • 真人 @ 时也现查。主动退群在飞书里是单独的事件(im.chat.member.user.withdrawn_v1),我们没有订阅;不现查的话,人走后 bot 要等缓存过期才恢复。
  • 单人群里说话的人不是登记的主人:说明主人已经不在群里(多半是主动退群,同样没有事件)。复用 handleChatMemberUserDeleted → handleAssistantOwnerLeft 处理:标记主人已离开、停掉群内会话、提示重新认领。这条消息不会进入原主人的目录。
  • bot 被拉进多人群:不跑入群钩子、入群命令、自动邀请 owner、归属登记和自动开工。
  • 定时任务:目标是多人群,或主人不在群里的单人群时,到点拒绝执行,不发任何东西。投回建任务那个私聊的任务不查。
  • 消息监听轮询、编辑消息的群上下文采集:同样受上面的规则约束。

开关打开时,行为与现状完全一致。

不在本 PR 范围内(有意为之)

  • 由主人自己在私聊里发起、目标是群的操作(relay、transfer、会议监听、建定时任务)不拦。这些都是主人本人的主动操作。
  • 群里有插件正在等待输入时(captureHumanInput,在 SDK ACK 之前同步执行),第二个人进群后,主人的回答仍会交给插件。要拦就得把异步查人数放进同步 ACK 路径。
  • autoInviteOwnerOnGroupAdd 不随开关联动:别人把 bot 拉进他的单人群时,默认会把 bot 管理员也拉进来,群就变成两人。需要的部署方自行关掉这个开关。
  • 上游 handleAssistantOwnerLeft 只订阅了 user.deleted_v1。本 PR 在开关关着时用「单人群里说话的不是主人」兜底;开关打开时,主人主动退群的问题仍然存在,建议后续单独补订阅 user.withdrawn_v1。

测试

  • 新增或修改的测试:assistant-command、event-dispatcher(消息入口、单人群放行、开关打开、入群、主人不在)、scheduler-silent-execute(多人群、缺字段、查询失败、单人群放行、主人不在、私聊任务不查)、bot-config-store。另有 3 个测试文件的 assistant-spawn mock 补了新导出。
  • 变异验证:人数阈值、缓存重查、@ 现查、限频、bot 发言者判断、消息入口、入群检查、定时任务阈值、缺字段处理、私聊任务跳过、主人不在判断(消息与定时任务两处),共 17 处,全部能被测试抓到。
  • 回归:与改动相关的 437 个测试文件,和基线 master 对照没有新增失败。剩下的失败在 master 上同样存在,或是满载下的偶发。bun test test/assistant-command.test.ts 通过。
  • 真机(测试机,助理模式 bot):
    • 单人群 ping 正常回复。
    • 拉第二个人进群后,不 @ 的消息不理;@ 时回提示,10 分钟内不重复。
    • 第二个人退群后,下一次 @ 恢复。
    • 私聊不受影响。

🤖 Generated with Claude Code

xu4wang and others added 4 commits November 26, 2026 12:39
…sistant-mode bots

With assistantGroupChat off (the default), an assistant-mode bot only works
in groups with a single human member (bots are not counted). In groups with
two or more people it ignores every message; a human @-mention gets one
notice per group per 10 minutes. Unknown member counts are treated as
multi-user.

Gates: inbound group messages (before group-context ingest, commands and
claiming), the bot being added to a multi-user group (no claim, no
auto-start), and scheduled runs targeting a multi-user group.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Re-read a cached single-human count before trusting it (the shared
  revalidateSolo only refreshed groups with at most one bot).
- Serve only an exact count of one human: a missing user_count (0) or an
  unknown count is multi-user, for messages, bot-added and scheduled runs.
- Apply the gate to polled message-listener matches and to edited-message
  group-context ingest.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A voluntary leave arrives as a different event than a removal, so the
cached multi-user count can outlive the second person by the full cache
TTL, while the rate-limited notice keeps the bot silent. A human @ now
always re-reads the count, so the bot recovers on the next mention.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… the owner

The voluntary-leave event is not delivered, so an owner who left a group
stays recorded. Once the second person was alone, the group counted as a
one-person group and their messages ran in the absent owner's workspace.

When a human who is not the recorded owner speaks in a one-person group,
the owner cannot be a member: treat it as the owner leaving (mark left,
stop the chat's sessions, prompt for a new claim). A scheduled run into a
one-person group is refused unless the owner is a member.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@deepcoldy

Copy link
Copy Markdown
Owner

你好,这个 PR 的自动评审群已建,麻烦点击链接加入群聊、查看评审意见并跟进后续修改:
https://applink.larkoffice.com/client/chat/chatter/add_by_link?link_token=773g1faf-b751-450c-a52a-e0bea8ce3438

另外你暂时不在自动拉群名单里,也可以把 GitHub 账号和飞书信息补进名单文档,补好后后续评审会自动拉你进群:https://bytedance.larkoffice.com/wiki/WJ1nwWbtxi89erkNGNbcgkt9nUe

(本条为自动流程发出的提示消息)

@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.

复审结论:暂不建议合入

复核对象为作者 HEAD 4f0ae402 / 本地 rebase 后 5912e011f2;四个提交逐一 patch-id 相同。首审的「0 阻断」需要修正:以下 2 个 P1 + 3 个 P2 都有临时运行时探针复现,另有 1 个 P3。探针使用真实生产模块、mock Lark API,运行后均已还原。

1. [P1] DM 任务改投多人群会绕过群闸,并按原 DM owner 执行

位置:src/core/session-manager.ts:4913-4918。

creatorChatId === undefined 被当成「仍投回原始 DM」,但这不是稳定事实:

  • 私聊 /schedule 创建路径 src/core/command-handler.ts:1137-1165 不写 creatorChatId;
  • Dashboard 改目标时 src/core/scheduler.ts:2117-2123 只改 chatId/chatIds,不同步 chatType;
  • 因而一个 chatType='p2p'、creatorChatId=undefined 的 DM 任务改投群后,在 4914 行直接跳过所有现查;后续 src/core/session-manager.ts:5415-5421 又仍按 p2p 使用原任务 owner。

探针序列:合法原 DM ownerOpenId + 上述任务形态,目标 mock 为 userCount=2 的群。真实 resolveAssistantSpawn 将原 owner 写入新会话,forkWorker 调用 1 次,而 getChatRoutingFacts/getChatInfo 均为 0 次。

建议:只有 creatorChatId === task.chatId 时才走私聊快捷路径;undefined 也必须现查。现查结果为群时,还要保证后续 spawn 使用目标的真实 chatType,而不是继续沿用 stale p2p。

2. [P1] bot 发言者绕过「唯一真人不是 owner」处理,可进入已离群 owner 的会话

位置:src/im/lark/assistant-command.ts:80-93,调用点 src/im/lark/event-dispatcher.ts:6085-6095。

87 行对 senderIsBot 直接返回 undefined。可达序列:

  1. 归属表仍记录旧 owner(主动退群没有 withdrawn 订阅,所以记录未标 left);
  2. 群内剩下另一名真人和一个已获 chat grant 的 bot,userCount=1,人数闸放行;
  3. 真人保持沉默,bot @ 本助理;由于没有人类 sender 可供比较,不触发 owner-left 清理;
  4. 群聊归属解析继续以归属表中的旧 owner 为准,消息进入其会话/工作目录。

探针结果:真实 dispatcher 在上述状态下 handleChatMemberUserDeleted 调用 0 次,handleThreadReply 调用 1 次。现有群归属规则会解析为该旧 owner,因此不是只发通知的表层问题。

建议:bot sender 无法用 sender≠owner 推断 owner 离群,但也不能据此默认 owner 仍在;在实际路由前现查成员列表并 fail-closed,或让所有 sender 共用一个「唯一真人且就是当前 owner」的异步准入门。

3. [P2] 多人群中轮询丢弃的消息没有结算,群恢复单人后会延迟补投

位置:src/im/lark/event-dispatcher.ts:4384-4389。

多人群 return 发生在 claimMessageOnce 之前,所以该 message_id 没有已处理记录。在 30 分钟 backfill 窗口内,第二人离群并触发缓存失效后,下轮轮询会重新处理同一条旧消息。实时入口与轮询入口的语义因此不同:前者已吞掉,后者只是延期。

探针序列:同一 listener 消息第一次以 userCount=2 轮询,handleNewTopic=0;模拟离群后的缓存失效,再以 userCount=1 轮询同一消息,handleNewTopic=1。

建议:对已经 match、且本次判定应丢弃的消息同步留下已处理记录(最简单是把 claim 放到群闸之前),不要让它在成员变化后重新获得执行资格。

4. [P2] 调度闸忽略成员列表中已经出现的第二位真人

位置:src/core/session-manager.ts:4917-4922。

人数和成员列表分两次 await,但第二次只检查 .includes(owner)。若 getChatInfo 返回 1 后第二人入群,成员列表返回 [owner, second_person],当前条件仍把 multiUser 置为 false。即使不考虑竞态,两个权威结果相互矛盾时也不应继续放行。

探针结果:userCount=1、成员列表同时含 owner 和第二人时,任务仍 forkWorker=1。

建议:用同一次成员列表结果要求「去重后恰好一人且就是 owner」,不要只检查包含 owner;这也能顺带合并首审指出的重复 chat.get。

5. [P2] 卡片点击仍未覆盖多人群与「唯一成员非 owner」两类新约束

位置:src/services/card-click-turn-runtime.ts:115-130,164-198;建会话时旧 owner 的目录来源见 src/services/card-click-turn-bind.ts:140-164。

这条链路不经过 processMessageEvent,也没有人数/owner 在场检查。持有未过期签名卡片且能通过现有 talk/canOperate 的点击者,在以下两种情况都可继续进入旧 owner 的会话:

  • 群已从单人变成多人;
  • owner 主动退群后只剩另一名真人,但归属表尚未标 left。

首审将其列为仅限全开放 bot 的 P3,范围需要扩大:最新 HEAD 已明确「单人群只在唯一成员是 owner 时服务」,而卡片链路连这一条也不检查;显式获授权的非 owner 同样可达,不只全开放 bot。

探针结果:把 binder 的 assistantMultiUserGroupsOff 置为 true 后,现有「非 owner 点击者在 owner workDir 建会话」用例仍通过,accept/run 和 forkWorker 均成功,说明新开关完全没有接入。

建议:在卡片 accept 后的实际 startTurn 投递边界复用同一个 fail-closed 群闸,并覆盖已有会话与新会话;仅限制创建分支会漏掉 live-session 注入。

6. [P3] 未认领单人群会先发布任务横幅,再拒绝执行

位置:src/core/session-manager.ts:4920-4922。

spawn.kind === 'unclaimed' 时 multiUser 保持 false,前置闸放行;非 silent 任务随后在 5008/5114/5184/5202 等分支发通知,直到 5417 才以 has no assistant owner 失败。这不符合本 PR 声明的「不发任何东西」,但目前探针只观察到横幅、没有 worker 执行,所以列 P3。

探针结果:userCount=1 + unclaimed 时,sendMessage 已调用,forkWorker=0,最终错误为 no assistant owner。建议群目标只有在 spawn.kind==='owner' 且成员核验通过时才继续。

验证记录

  • bun run build:通过。
  • 首审涉及的 7 个 vitest 文件:1278/1278 通过。
  • bun test test/assistant-command.test.ts:29/29 通过。
  • 所有探针还原后复跑 event-dispatcher / scheduler-silent-execute / card-click-turn-bind:868/868 通过;git diff --exit-code 干净。
  • 四个作者提交与本地 rebase 提交逐一 patch-id:全部 MATCH。
  • 本轮未重复真机飞书验证;上述问题均由真实生产模块 + 可控 Lark mock 的运行时探针复现,不是仅凭源码猜测。

首审的 HTTP trigger / Dashboard 管理员入口仍按现有鉴权与 PR 非目标边界处理,不计为阻断;VC / relay / transfer / captureHumanInput / autoInviteOwner / withdrawn 订阅等声明的有意排除也未计入上述清单。

…es and card clicks

A group is served only while its live member list (bots
excluded), deduplicated, is exactly the recorded owner; a lookup failure refuses.

- schedules: only a task delivered back to its own DM (creatorChatId === chatId)
  skips the lookup; a DM task retargeted to a group is checked and spawns as a
  group. Unclaimed one-person groups are refused before the banner. The member
  list replaces the second chat.get and a contains-owner check.
- messages: a bot speaker cannot vouch for the owner, so the member list decides
  (owner gone => treated as left; otherwise unverifiable => dropped).
- message-listener polling: claim the message before the group check so a
  message dropped in a multi-user group is not backfilled once it turns solo;
  polled senders go through the same member-list owner check.
- card clicks: existing and new sessions both pass the same check before the
  nonce is consumed (reason assistant_group).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@xu4wang
xu4wang force-pushed the feat/assistant-multi-user-group branch from 3176350 to bedb9f9 Compare November 26, 2026 08:48
@xu4wang

xu4wang commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

感谢复审,6 条全部接受,修复在 bedb9f9。核心改动是新增 assistantSoloGroupAdmission(src/core/assistant-spawn.ts),作为统一的 fail-closed 准入检查:现拉成员列表(不含 bot),去重后必须恰好一人且就是登记的主人,拉不到就拒绝。人数缓存只用于提前拒绝,放行一律走这个检查。

  1. [P1] DM 任务改投群:只有 creatorChatId === task.chatId 才走私聊快捷路径,undefined 也会现查。现查结果是群时,本次执行把 task.chatType 改为 group,后续 resolveAssistantSpawn、createSession 都按群处理。测试:scheduler-silent-execute 中的 "a DM task without creatorChatId that now targets a group…"。两处都做了变异验证:恢复 undefined 快捷路径会失败;去掉 chatType 修正也会失败。
  2. [P1] bot 发言者:assistantSoloGroupAbsentOwner 改为异步的 assistantSoloGroupOwnerCheck。真人发言者仍按 sender≠owner 判断;bot 发言者走成员列表核对:主人不在,按主人退群处理;人数不对或拉不到,丢弃。测试覆盖 assistant-command 和 event-dispatcher 两层(与你探针相同的序列:handleChatMemberUserDeleted 现在会被调用)。
  3. [P2] 轮询结算:claimMessageOnce 移到群检查之前。轮询来的发言人身份不一定可信,统一按 bot 发言者的方式核对主人。测试按「先 2 人轮询 → 失效缓存变 1 人 → 再轮询同一条」的顺序执行,第二次不再投递。
  4. [P2] 调度只查 includes:改为同一次成员列表「去重后恰好一人且是主人」,同时去掉了第二次 chat.get(现在只剩 getChatRoutingFacts,用于判断 p2p/群)。测试中 [owner, other] 会以 multi_user 被拒。
  5. [P2] 卡片点击:runtime 新增可选依赖 assistantGroupAdmitted,binder 注入上面的统一检查。检查放在 acceptCardClickTurn 里,位于 sessionBlockedShape 之后、消费 nonce 之前。已有会话和新建会话都要经过这一步,失败返回新原因 assistant_group,并给出对应提示。位置和你建议的 startTurn 边界不同,原因是:放在消费 nonce 之前,被拒时按钮不会作废,也不会先建出空会话。accept 到 run() 之间只隔一次 ack。如果你认为这个窗口也要关,我在 startTurn 里再补一次检查。
  6. [P3] 未认领单人群先发横幅:调度检查中,unclaimed 也会被拒,在任何 send 之前;测试断言 sendMessage、replyMessage 都没有被调用。

验证:

  • 新增与改写的用例做了 7 处变异,每处都至少让一个用例失败。
  • 全量 vitest 与 base 对照,失败集合完全一致(同一批 13 个文件、29 条,本机环境原因,与本改动无关)。
  • bun test test/assistant-command.test.ts 通过;其余三个测试文件使用 vi.hoisted/importOriginal,本来就不在 bun 腿里。
  • 真机(dev-jack,TEST bot):2 人群 @ 回「不支持多人群」;主人退群后,剩下的人 @ 会按主人离开处理(日志 owner … is not in solo group …; treating as left → suspended 1)。

- 轮询区分明确非法的终态丢弃与瞬时查询失败,恢复后可重试且避免重复成员查询\n- 定时任务绑定当前 owner 与执行者,补 union-only 身份换算及最终 authority 复核\n- 卡片处理等待期过期、最终投递、双点击接管和半建会话的 durable 清理

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

Copy link
Copy Markdown
Owner

已在作者实现 bedb9f93e2 之上追加 follow-up 提交 f6302f8819,未 force-push、未改写已有提交。

这份增量不重复原六项修复,集中处理并发与持久状态交叠:

  • 轮询:只读预检避免重复成员查询;明确多人 / owner absent 的消息终态结算,lookup_failed 不消费消息,Lark 恢复后可重试。
  • 定时任务:当前群 owner 与已记录执行者绑定;union-only / 跨 app 身份必须可信换算到本 app,否则拒绝。入口、已有会话注入、新会话 fork 前都复核;最终成员查询后再查 authority,暂停/撤销不会继续投递或留下 fresh candidate。
  • 卡片:nonce 使用 await 后的当前时间,覆盖等待期间过期;accept 后及最终投递前复查群准入与 registry/status;pending-turn 计数防止双点击误清理,durable close 成功后才移除 registry,close 失败保留可重试状态。

验证:

  • bun run build:通过。
  • 定向 vitest 9 文件:1428/1428。
  • bun test test/assistant-command.test.ts:30/30。
  • 针对轮询瞬时故障与终态、union-only actor、最终查询期间暂停、卡片过期、双点击接管、close 失败及反向 close 顺序的临时运行时探针全部通过。
  • 对上述守卫做 8 处变异复验,相关测试均按预期变红;探针与变异均已还原,git diff --check 通过。

本轮未重复真机飞书验证;作者上一提交的真机结果保持不变。请按新 head f6302f8819 复核。

合并 91d7bec。卡片 startTurnInner 保留群准入、nextStep 与 registry/status 投递前复查,复查后接 master 的 plannedOpeningBackendType input 构造;测试 mock 同时保留 assistant-spawn 与 created-groups-store 配置。

Co-Authored-By: Claude Code <[email protected]>
@deepcoldy
deepcoldy merged commit 6a69b89 into deepcoldy:master Nov 26, 2026
15 checks passed
deepcoldy pushed a commit that referenced this pull request Nov 26, 2026
合码窗口 master 再进 4 提交。与本 PR 原创代码仅 daemon.ts、card-handler.ts
两文件重叠且改动区段不相邻(不同函数,相隔数十至数千行),git 自动合并零冲突。

合并 tree fd4c644 与 git merge-tree --write-tree 预测完全一致(三点一线)。
合并树验证:bun run build(tsc/typecheck/audit)通过;定向 458 用例全绿,
含本 PR 核心(streaming-card/capacity/session-contract-gateway)与新进提交
自带交集测试(ask-chat-scope/scheduler-silent/trigger-session);
bun.lock/package.json 未变。

按复审意见不走 #109 豁免,本 merge commit 触发全新完整 CI 首跑后再合。

Co-Authored-By: Claude Fable 5 <[email protected]>
@github-actions

Copy link
Copy Markdown

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

这次运行覆盖 6a69b89…0331c8a78(5 个提交),同批合入的还有 #3892 #3888 #3884 #3889,失败不一定由本 PR 引起。

用例 跑道 状态 失败信息
Claude group reply mode new-topic starts a session per topic feishu-core-group-modes failed Reply "ACK-e2e-gr-new-topic-claude-2-1795742368638 RECALL ACK-e2e-gr-new-topic-claude-2-1795742368638" appeared 0 time(s) within 240000ms, expected 1 (clicked 直接开启会话 0×; re-opened 1×; repo card butto…

@github-actions

Copy link
Copy Markdown

🚀 Released in v3.59.35

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