Repository navigation
feat(card): 基于飞书 Pin 来源恢复重启后的安全清理 - #1156
Conversation
Co-authored-by: TRAE CLI <[email protected]>
Co-authored-by: TRAE CLI <[email protected]>
Co-authored-by: TRAE CLI <[email protected]>
Co-authored-by: TRAE CLI <[email protected]>
Co-authored-by: TRAE CLI <[email protected]>
Co-authored-by: TRAE CLI <[email protected]>
Co-authored-by: TRAE CLI <[email protected]>
Co-authored-by: TRAE CLI <[email protected]>
Co-authored-by: TRAE CLI <[email protected]>
Co-authored-by: TRAE CLI <[email protected]>
…-recovery Co-authored-by: TRAE CLI <[email protected]>
Co-authored-by: TRAE CLI <[email protected]>
…-recovery Co-authored-by: TRAE CLI <[email protected]>
|
CI 说明:本 PR 的 |
|
你好,感谢这个 PR —— 设计上的克制很到位: 下面是自动评审的初步意见,其中第 1 条经过真实 API 验证,建议合入前修复。 🔴 1(阻断):
|
| 变异 | 结果 |
|---|---|
| 整条来源守卫删掉 | 🔴 4 条红 |
只删 operatorId !== larkAppId(放任何 app 进来) |
🔴 3 条红 |
只删 operatorIdType !== 'app_id'(放 open_id 进来) |
🟢 全绿 |
第三行是真实缺口,不是惰性变异 —— 我用同一个探针跑了变异前后两遍,观测值确实不同:构造一条 {operatorId: 'app-pin', operatorIdType: 'open_id'} 且命中本地候选的 Pin,修复态 unpin 列表是 [],变异态变成 ["om_frozen"]。
现有用例里 om_open_id_same_bot / om_frozen_human 用的 operatorId 都是 ou_*,会先被 operatorId !== larkAppId 挡掉,所以走不到类型检查那一步。建议补一条 operatorId 等于 larkAppId、但 operatorIdType 为 open_id 的用例,把这个维度单独钉住。
(另外顺带说明:我也变异了 candidateIds 交集那行,虽然全绿,但探针证明观测值与基线逐字相同 —— 它被 enabled 分支的 entry.frozenIds.filter 和 disabled 分支的 provenIds 构造挡住了,属于冗余防御而非缺口,不建议改动。这条列在这里只是为了说明我没把它误判成问题。)
🟡 3(可选):reconcileRestoredStreamingCardPinsForRequest 里的合成 DaemonSession 兜底像是死代码
worker-pool.ts:2653-2659 有一个 liveSessions.get(...) ?? ({...} as DaemonSession) 的兜底。但进入这个循环前,byChat 的构造已经在 2626-2627 做过 if (!ds) continue,且 liveSessions 在这两处之间没有再被写过,所以 ?? 右侧应当不可达。
我把右侧换成 throw 后跑测试仍然 34 passed 全绿,佐证了这一点。这个 as DaemonSession 断言构造的是一个字段不完整的对象,一旦将来 pinStreamingCardIfEnabled 多读一个字段就会静默走到错误分支。建议要么删掉兜底、要么加注释说明它防的是哪种时序。这条不阻断。
其它已核对、结论正常的点
apiOnly边界:listChatPins没有像同文件其它 primitive 那样调assertLarkTransport,我一开始担心它绕过api-only-mode-wiring.test.ts的不变量。实测是安全的 —— 它走getBotClient,该函数本身就是权威闸门,探测结果为LarkTransportDisabledError: ... (attempted: getBotClient)。那条不变量测试的 op 白名单里也没有listChatPins,所以不算破坏。只是风格上与邻居不一致,可加可不加。- 启动时序:
restoreSessionsAndScheduleStartupRecovery用queueMicrotask做 fire-and-forget,readiness 不被 Pin API 拖住,api-only-mode-wiring里 restore→ready 的顺序锁也同步更新了,正确。 - FIFO 串行:复用 per-bot 队列与 per-message mutation queue,「bot-wide off 清理」与「重启恢复」在同一 message id 上的串行化有专门用例覆盖,正确。
- 分页严格性:空 token / 前后空白 token / 重复 token 全部抛错而不是静默截断,这个设计是对的。
- 基线与回归:我把 PR 分支在最新
origin/master(7bd2e974d) 上做了本地 rebase(未 force-push,作者提交未改动),无冲突,rebase 后 diff 与原 PR 逐字一致。bun run build通过;相关 8 个测试文件 313 passed。 - CI:
bun-test红是既有问题,不是本 PR 引入 —— 该 job 是continue-on-error: true(ci.yml:81,非门禁),且我按 per-job 查过 master 自己的最新两个 run(含当前 tip7bd2e974d)同样是failure;本 PR 的 4 个测试文件都不在 35 个失败文件里。门禁build是通过的。
综上:建议先修第 1 条(page_size → 50,它让特性完全不生效),第 2 条建议一并补上;第 3 条可选。修完我可以再跑一轮验证。
以上是自动评审的初步意见,可能有疏漏,最终以维护者审阅为准。
|
复审确认(自动评审第二轮,独立验证,非维护者终审): 首审的三条结论我逐条独立复现,全部成立,无新增阻断项。 ① page_size=100 恒失效 — 在另一个 app 上复现 失效链也核对过:SDK 的 ② 变异结论复现:删整条守卫 4 红 / 只删 operatorId 判定 3 红 / 只删 operatorIdType 判定全绿,与首审一致。 ③ 死代码确认: 两点首审之外的实测补充(都对本 PR 有利):
影响面(worker-pool 公共层 + daemon 启动路径)我额外核了:FIFO 分支只对带 结论与首审一致:建议作者修改后再合,修第 1 条即可解除阻断,第 2 条建议一并补。 |
Co-authored-by: TRAE CLI <[email protected]>
Co-authored-by: TRAE CLI <[email protected]>
Co-authored-by: TRAE CLI <[email protected]>
…-recovery Co-authored-by: TRAE CLI <[email protected]>
|
已按两轮自动评审的三点建议全部更新,并同步到最新
提交:
最新 HEAD
烦请再复核。 |
|
最新 CI 已结束:
PR 正文已同步更新为当前 HEAD、每页 50 条的真实 API 契约及 737 passed 的定向矩阵。 |
DeepColds
left a comment
There was a problem hiding this comment.
发现一个来源安全边界上的阻断问题:
[阻断] enabled 启动恢复会把“人工/其它应用已经 Pin 的当前卡”重新登记成 Botmux 自己的 Pin,后续 close/transfer 会误 Unpin。
reconcileRestoredStreamingCardPinsForRequest 已经拿到了 remotePins 和严格的 provenIds,但 enabled 分支不看当前卡的远端 operator,始终调用 pinStreamingCardIfEnabled(entry.ds, entry.currentId)。pinMessage 只判断 code === 0;而飞书对“消息已经被 Pin”是幂等成功,pin.create 会返回 code 0。随后 pinStreamingCardIfEnabled 无条件 rememberOwnedStreamingCard。
因此以下路径会突破本 PR 声明的 same-app provenance 不变量:
- 当前
streamCardId已被人工或另一个 app Pin;Pin list 返回该 currentId,但 operator 不是当前 app。 - daemon 重启,enabled recovery 对同一 currentId 再调
pin.create,得到幂等成功。 - 该 currentId 被记进 process-local owned registry。
- 后续关闭、转移或关闭总开关时,Botmux 将这条人工/其它 app Pin 移除。
现有测试覆盖了“frozen card 是人工 Pin”以及“远端有无关人工 Pin”,但没有覆盖 currentId 本身命中人工/其它 app Pin;所以这条路径仍然会绿。
建议:
- 若 Pin list 已包含 currentId,只有
operator_id_type === 'app_id' && operator_id === larkAppId才能恢复 ownership;若 operator 是人工/其它 app,应保持该 Pin 原样,不能再通过幂等 create 把它认领。 - 更稳妥的是让 create 窄封装返回
data.pinprovenance,并在成功后也只对 same-app operator 调rememberOwnedStreamingCard,避免 list→create 间的竞态。 - 补 enabled recovery 回归:currentId 精确命中
open_id/ other-app 的 Pin,恢复后再触发 disable/close,断言 currentId 从未被 Unpin。
其余分页上限 50、operatorIdType 正交用例、真实 DaemonSession 围栏、per-bot FIFO 与 no-transport 边界均已复核。定向矩阵在本地运行 732 passed;另一个与改动无关的 release-ancestry-pin suite 因当前共享依赖目录缺少 js-yaml 未能导入,GitHub 的 build / bun-binary / bun-binary-musl 均已通过。
|
再次感谢,三条意见都修到位了,而且修法比我建议的更好。这轮复审结论:未再发现阻断项。 ✅ 逐条复核1. 不只是改了数字,还抽了具名常量 对真实接口重跑确认: 2. 新增 用例构造正是我描述的那个盲区: 我把上轮那枪变异原样重打了一遍验证缺口真的关上了:
第三行由绿转红,且死掉的正是新增的那条用例( 3. 复用真实会话条目( 这个修法比我建议的「删掉或加注释」更彻底 —— 直接把 我验了它是行为等价的重构而非未覆盖:把该行换成按 sessionId 反查(等价写法)→ 35 passed 全绿;再把该行换成 验证记录
一点非阻断的观察(不必改)重启恢复是按群发 以上是自动评审意见,最终以维护者审阅为准。我这边没有其它需要作者改的了。 |
|
交叉确认(自动评审第三轮,独立验证,非维护者终审): 三条修复逐一对账,全部成立,我这边结论也是可合。 ① ②
由绿转红的那 1 条正是新增用例;operatorId 维度稳定 3 红,没有为让新用例过而放宽别的判据。 ③ 基线与交叉影响:PR head 合入的 master 是 非阻断观察同首审:每群一次 GET 在 bot×群上是乘法关系,修好后才真正产生开销,留档即可。 双方自动评审结论一致:可合。按流程报维护者确认,以维护者审阅为准。 |
deepcoldy
left a comment
There was a problem hiding this comment.
双审收敛(自动评审 + 交叉复核),三条意见作者已全部修复并经反变异验证在最新主干上仍有效。维护者已确认合入。
|
🚀 Released in v3.18.12 |
背景
PR #1067 的实时卡 Pin 生命周期刻意不扫描全群 Pin;daemon 重启后若进程内 provenance 丢失,关闭或转移会保留无法安全判定来源的陈旧 Pin。飞书 Pin 列表会返回创建操作的
operator_id_type/operator_id,可用于恢复当前应用创建的 Pin 来源。改动
operator_id_type === "app_id" && operator_id === 当前 larkAppId时认定 Pin 由当前应用创建。DaemonSession引用,由现有 status/current ID/transport/registry identity fence 做执行时复核,不再构造不完整的 synthetic session。安全边界
/card、最终回复、CoT、关闭卡等非实时状态卡。影响面
验证
mise exec [email protected] -- bun vitest run --project unit test/lark-pin-message.test.ts test/streaming-card-pinning.test.ts test/pin-streaming-card-recovery-wiring.test.ts test/api-only-mode-wiring.test.ts test/recall-frozen-cards.test.ts test/worker-ready-display-mode.test.ts test/transfer-session.test.ts test/mojo-explicit-close.test.ts test/close-stream-card-untouched.test.ts test/card-handler-resume-receipt.test.ts test/card-action-authorization.test.ts test/card-prefs-auto-start.test.ts test/bot-config-store.test.ts test/command-handler.test.ts test/release-ancestry-pin.test.ts:14 个实际执行文件,737 passed。operator_id_type正交用例经临时删除 type guard 的 mutation 验证会转红;mutation 已还原且未提交。mise exec [email protected] -- bun run build:通过。git diff --check origin/master...HEAD:通过。build、bun-binary、bun-binary-musl通过;非门禁bun-test的 34 个失败文件与当前 master 基线同型,增量相关测试不在失败列表。Closes #1125