Skip to content

fix(lark): 转发 + 留言时,留言和自动开工的话题种子配对,不再被丢 (#3784) - #3840

Merged
deepcoldy merged 2 commits into
deepcoldy:masterfrom
nengqi:fix/autostart-forward-comment
Nov 25, 2026
Merged

deepcoldy merged 2 commits into
deepcoldy:masterfrom
nengqi:fix/autostart-forward-comment

Conversation

@nengqi

@nengqi nengqi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #3784

复用已有的 forward-followup 机制:靠 autoStartOnNewTopic 自动开工的话题种子,先按住 forwardFollowupWaitMs(默认 1.5 秒),等同一发件人紧跟着的话题内留言来配对,不再把留言当成「不是发给本 bot 的话题回复」丢掉。

  • 代价:所有靠 autoStartOnNewTopic 自动开工的新话题(不只是转发)都晚 1.5 秒起会话。
  • 不覆盖:durable primary 路由。
  • 改动位置:实现在 src/im/lark/event-dispatcher.ts,测试在 test/event-dispatcher.test.ts。

问题

话题群开了 autoStartOnNewTopic、@ 策略是 topic(或默认 always)时,用户用「转发 + 留言」:转发卡片成为话题根,没有 @,靠新话题自动开工起会话;留言是同一话题里 0–1 秒后的回复,也没有 @。留言到达时本 bot 还没拥有这个话题,被当成「不是发给本 bot 的话题回复」静默丢掉,会话只看到转发内容。

改动点

forward-followup 原来只在 never / ambient 模式下按住话题种子(usesForwardFollowupDelay)。

  1. 按住种子:消息走到 [auto-start:新话题] 那条放行时,记下 autoTopicSeed,写进 PendingForwardTopicPayload,随 forward-followup 记录一起持久化。shouldDelayTopicSeed 改为在 usesForwardFollowupDelay(...) || (autoTopicSeed && !primary) 时按住,所以除 durable primary 路由外,任何 @ 模式下的自动开工种子都会被按住。
  2. 配对:同一发件人的话题内回复在配对处(forwardFollowups.take)取出种子。在 never / ambient 以外的模式下,种子带 autoTopicSeed 就配对;不带的照旧当陈旧种子丢弃。ambientRedirect 的判断没动。
  3. 放行:群聊 @ 闸(relax)加一条 isAllowed && pairedForwardSeed?.payload.autoTopicSeed。配对这一步本来就要求 isAllowed,这里再显式写一次,未授权的人既配不上对,也过不了这一条。
  4. 重启恢复:恢复持久化种子时(restoredForwardFollowups 循环),带 autoTopicSeed 的种子在任何 @ 模式下都按剩余时间重新按住。

配对后的行为和 never / ambient 下一致:留言成为本轮锚点,转发内容作为 forwardSeedData 进 prompt。没等到留言的种子,到点后照常自动开工。

不受影响的范围:被拉黑的发件人、autoStartExcludedChats 里的群、非话题群,本来就走不到自动开工那条放行,所以不受影响。

代价与限制

  • 所有靠 autoStartOnNewTopic 自动开工的新话题都会晚 forwardFollowupWaitMs(默认 1.5 秒)才起会话,不只是转发。按住时不区分种子是不是转发,因为普通新话题后面也可能紧跟一条补充说明,而收到种子时没法知道会不会有。设成 0 就关掉这个等待,和 never / ambient 下一样。
  • durable primary 路由不按住:按住种子的分支在 primary 时会 throw(durable primary routing does not yet support delayed forward-followup seeds),所以 shouldDelayTopicSeed 里用 !primary 把这条路由排除在外,那里的自动开工种子照旧立即开工,留言仍可能丢。

测试

test/event-dispatcher.test.ts 新增「forward + comment into an auto-start topic (#3784)」五条:

  • topic 模式下,留言和按住的自动开工种子配对,handleNewTopic 只调一次,锚点是留言,带 forwardSeedData;
  • 默认 always 模式下同样配对;
  • 没有留言的种子到点后照常自动开工;
  • autoStartExcludedChats 里的群既不按住也不开工;
  • 另一个有权限的人的回复不会配对,种子到点后照常自动开工。

验证:

  • 全量测试没有新增失败:改前、改后都是同样 143 条失败,都是和本改动无关的环境类 e2e。
  • 新用例:改代码前,前两条失败;改后五条全过。
  • 类型检查:tsc --noEmit 通过。
  • 相关单测:event-dispatcher、forward-followup、durable/primary 相关 42 个文件 1245 条全过。

…instead of dropping it (deepcoldy#3784)

With autoStartOnNewTopic on and the mention mode topic/always, a forward +
comment arrives as an un-@ topic root (auto-started) and an un-@ reply 0-1 s
later. The reply reached the dispatcher before the bot owned the topic and
was silently dropped as an unaddressed topic reply.

Reuse the forward-followup grace window: seeds admitted only by
autoStartOnNewTopic are now held under any mention mode (not under durable
primary routing, which cannot hold seeds yet), marked autoTopicSeed in the
persisted payload; a root-linked reply from the same allowed sender pairs
with such a seed, and the mention gate admits that paired reply. Restored
auto-start seeds are re-held for their remaining wait. Auto-started topics
now start forwardFollowupWaitMs (default 1.5 s) later; 0 disables the wait.
- 重启恢复:持久化的 autoTopicSeed 在 topic 模式下按剩余时间重新按住,
  重启后到达的同发件人留言仍能配对(覆盖 restore 分支的新增条件)
- 路由竞态:种子的 chat mode 查询未决时留言先到,gate 释放后仍正确配对,
  且只派发一次

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

Copy link
Copy Markdown
Owner

首审通过,无阻断项。在作者分支上追加了一个纯测试 commit(3461c3a8,ff 推送,未改动任何生产代码),补两条评审中发现的覆盖缺口:

  1. 重启恢复路径无测试:restoredForwardFollowups 恢复循环里新增的 autoTopicSeed === true 重新按住条件,原 5 条用例都走内存 buffer,覆盖不到。摘掉该条件后新用例稳定咬红(变异验证)。
  2. 种子路由未决的交叠窗口:种子的 chat mode 查询还在飞行中、留言先到时,waitForSeedRoutingGate 释放后必须仍能配对且只派发一次。

本地验证(rebase 到最新 master 后):

  • vitest run --project unit test/event-dispatcher.test.ts:767 条全绿(原 765 + 新增 2)
  • forward-followup / auto-start 相关 6 个文件 vitest 67 条全绿;bun 腿 5 个文件 45 条全绿
  • tsc --noEmit 通过
  • 变异复验:按住条件、relax 放行条款、配对分支、恢复条件 4 处守卫分别摘掉后,对应新老用例均按预期咬红

两个非阻断观察(不影响本 PR 合入):

  • 配对消费种子后、会话真正注册(isSessionOwner 翻 true)之前到达的第二条同话题留言仍会被静默丢弃——比本 PR 修复的首条留言窗口更窄,建议另开 issue 跟踪。
  • 新配对路径没有 mentionsOther 让路:topic/always 模式下,留言若 @ 了别的 bot 仍会触发本 bot(never/ambient 的 ambient redirect 在配对前生效,不受影响)。

最终以维护者审阅为准。

@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 与 3461c3a 一致)。

独立走查要点

  • autoTopicSeed 仅在群聊闸 access==='ignore' 的 auto-start 分支置位;blocked 发送者有否决腿,excluded chats 有用例覆盖。未授权发送者可按既有 D4 语义产生自动开工种子,但配对前置条件要求 isAllowed,新放行条款同样带 isAllowed——权限闸无绕过(已用非授权留言者探针实证:不配对、不派发,种子到点按 D4 开工)。
  • 非延迟模式分支:带标记种子配对、其余维持陈旧冲刷语义;durable primary 在持有(!primary)、配对(throw)、恢复(throw)三条路径均自洽排除。
  • 持久化闭环:持有落盘带标记 → 重启按 remainingMs 重持(非全量重计)→ 配对时 provisional 记录与派发前终写的 ctx 均带 forwardSeedData → 恢复后按已配对立即派发;take 清 timer 并摘除记录,无双派发。

实际验证(本机复跑)

  • bunx vitest run test/event-dispatcher.test.ts:767 全绿。
  • forward-followup 相关 6 文件 42 条全绿;bun 腿 4 文件 17 条全绿(逐文件独立进程)。
  • tsc --noEmit 通过。
  • 变异抽查:① 摘掉恢复路径的 || record.payload.autoTopicSeed === true → 重启恢复用例如期变红;② 配对分支回退为直接 stale → 4 条配对用例变红,同组 3 条无关用例保持绿。

对抗探针(真实组件临时用例,未提交)

  1. 第二条留言窗口(P3,建议 follow-up):配对消费种子后、会话注册前到达的第二条同 root 留言,实测既不走 handleNewTopic 也不走 handleThreadReply,静默丢弃。pre-PR 第一条也会丢,属修复未覆盖的残余窗口而非回归。
  2. mentionsOther(P3,但影响比首审描述更轻):@其他 bot 的留言会配对本 bot 种子并以留言为 anchor 派发一次。实测不会多出会话——autoStartOnNewTopic 下本 bot 的种子本来就会到点开工,配对只是把留言文本纳入本 bot prompt。可考虑在配对分支补 mentionsOther 让路(让种子随后单独开工),非阻断。
  3. 另注:BOTMUX_FORWARD_FOLLOWUP_WAIT_MS=0 只关闭新种子持有;重启恢复时带标记记录仍按剩余 dueAt 重持(≤1.5s),差异微小,可不处理。

@deepcoldy
deepcoldy merged commit 6389a34 into deepcoldy:master Nov 25, 2026
26 of 28 checks passed
@deepcoldy

Copy link
Copy Markdown
Owner

已合并,感谢贡献!squash 落地 master(6389a341),原作者署名保留。

评审中补充的两条重启恢复/路由竞态测试已随 PR 一起合入。两个非阻断的后续点,供后续改进(不影响本次合入):

  1. 配对消费种子后、会话真正注册(isSessionOwner 翻 true)之前到达的第二条同话题留言仍会被静默丢弃,窗口比本 PR 修复的首条留言更窄,建议另开 issue 跟踪。
  2. 新配对路径没有 mentionsOther 让路:topic/always 模式下留言若 @ 了别的 bot 仍会配对本 bot 种子。实际影响有限——自动开工的种子本来就会到点起会话,不会多开会话,只是该留言文本会进本 bot 的 prompt。

另外本次 CI 首跑有一条与本 PR 无关的 meego-auth 竞态 flake(#3852),同 SHA 重跑后全绿。最终以维护者审阅为准。

@github-actions

Copy link
Copy Markdown

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

这次运行覆盖 350ce55…6389a341d(8 个提交),同批合入的还有 #3835 #3836 #3832 #3838 #3795 #3837 #3827,失败不一定由本 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.

bug(lark): 话题群「转发 + 留言」时,留言在新话题自动开工前到达,被静默丢弃,不进会话

2 participants