Skip to content

fix(onboarding): make managed MOSA bot activation recoverable - #572

Merged
deepcoldy merged 8 commits into
deepcoldy:masterfrom
BigTrue221:codex/mosa-auth-gate-v2
Jul 24, 2026
Merged

deepcoldy merged 8 commits into
deepcoldy:masterfrom
BigTrue221:codex/mosa-auth-gate-v2

Conversation

@BigTrue221

Copy link
Copy Markdown
Contributor

Summary

  • recover existing Feishu apps using the exact application owner instead of the bridge operator
  • keep managed bot configs activation-pending until critical permissions and the exact current-app scan receipt are confirmed
  • make config commit, PM2 activation, and retry/readback crash-safe without creating a second app
  • preserve raw bot indexes so concurrent pending registrations cannot make a daemon start the wrong app

Why this belongs in botmux

MOSA owns Space/owner ACLs, durable commands, leases/fences, and Workboard projection. botmux owns the Feishu Open Platform setup and daemon activation data plane. The failure happened between permission application and botmux startup, so the runtime must fail closed and expose a recoverable receipt to MOSA.

Verification

  • targeted onboarding/managed activation Vitest suite: 90/90 passed
  • TypeScript typecheck (no emit): passed
  • production build and exact dist/domain audit: passed
  • independently reviewed against the exact final diff: approved

Safety

  • no replay of prior failed or ambiguous provisioning commands
  • no second App is created during recovery
  • activation remains blocked on missing, stale, or ambiguous permission evidence

@BigTrue221
BigTrue221 requested a review from deepcoldy as a code owner July 23, 2026 11:49

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

这版方向对,权限/事件回读、owner 扫码、PM2 错误与空列表分离也都值得保留;但当前实现仍有两个会破坏“失败即锁死 + 可恢复”的阻塞问题,建议修完再合。

  1. activationPending 在启动 ACK 前已经从持久配置删除,Dashboard 崩溃或启动结果不确定时无法 fail-closed。

    src/dashboard/bot-onboarding.ts:871-900 先写回不带 activationPending 的 bots.json,再 await runLiveStart();只有 Promise 正常返回失败时才补回标记。我用一个不 resolve 的 startBotLive 复现了这段窗口:启动尚未 ACK 时,磁盘行已经没有 marker。此时新建 Manager 模拟 Dashboard 重启,账本会把任务恢复成 failed / permission_recovery_interrupted,内存快照显示 activationPending:true,但它不会把 marker 重写回 bots.json,所以 fleet restart 仍可启动该 bot。

    同一问题在不确定启动结果下更明显:CLI 超时/异常可能发生在 PM2 已经创建进程之后;当前 rollback 只补 marker,不会精确 stop + 回读确认当前进程已消失,因而可能出现“磁盘 pending、daemon 仍在线”。需要让 activation transition 本身可恢复:例如持久化 activating 阶段并在启动/重启时 reconcile,或让 daemon 持一次性 activation receipt 启动后再提交 ACK;任何 ambiguous/failed start 都必须 stop exact row 并回读 absence 后才能声称重新锁死。

  2. “精确 PM2 启动 ACK”和原始下标 fence 目前都只做了一半。

    • src/cli.ts:2581-2589 在 pm2 start --only 返回 0 后直接返回 state:'started',没有重新 jlist 验证 exact process 仍为 online(更没有核对该 row 的 BOTMUX_BOT_INDEX / app identity)。这不足以作为上面状态机依赖的启动 ACK;PM2 子进程随即崩溃或命令超时后的部分成功都会被误分类。
    • src/daemon.ts:16284-16303 首次确实用 loadBotConfigAtIndex(idx),但 profile bootstrap 后又用 compact 后的 loadBotConfigs(),找不到原 App 时 fallback 到 botConfigs[idx]。并发配置变化时这仍可能选到别的 App;如果 bootstrap 因目标刚被标 pending 返回 bot_not_in_config,当前代码也只是 warn,然后继续用标记前读到的 stale cfg 注册。建议在 registerBot 前再次按 raw index 解析并断言 appId 未漂移;pending/missing 必须致命退出,不能 fallback 到 compact index,也不能继续 stale config。

本地验证:

  • 相关 5 个 suite:178/178 通过
  • pnpm exec tsc --noEmit:通过
  • pnpm build(含 public-domain/dist audit):通过
  • 全量 unit:10039/10039 通过(显式设 BOTMUX_SCHEDULE_TIMEZONE=America/Los_Angeles,避免本机全局 Asia/Shanghai 配置污染时区用例)
  • diff 未发现私有部署域名或密钥;未改隧道协议,因此不需要 platform 镜像改动

另外,MOSA 在 master 中此前没有出现;它是否属于可公开术语仍请 maintainer 单独确认,这不替代上面两个代码阻塞项。

@BigTrue221

Copy link
Copy Markdown
Contributor Author

Addressed the requested-change blockers in ab2bd31:

  1. Managed activation now persists an exact activationStarting marker before PM2 start. The daemon waits on that marker before registering or receiving traffic; startup/timeout uncertainty is reconciled by exact stop + readback before restoring activationPending.
  2. start-bot now re-reads PM2 jlist after start and requires exact process name, raw BOTMUX_BOT_INDEX, BOTMUX_LARK_APP_ID, and online state.
  3. Post-bootstrap daemon reload remains bound to loadBotConfigAtIndex(idx) and rejects App drift; it no longer falls back through a compacted array.

Verification:

  • targeted regression: 118 passed
  • full unit: 10140 passed; 4 pre-existing vc-meeting-daemon-session failures also reproduce on origin/master
  • tsc --noEmit and pnpm build passed

Please re-review the current head.

@BigTrue221
BigTrue221 requested a review from deepcoldy July 24, 2026 08:03

@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 ab2bd319:activationStarting + gated daemon、start 后 PM2 jlist 精确回读,以及 success 分支的 raw-index 重载,方向都对;但 crash-safe 状态机仍有 3 个阻塞空窗,原 review 的 stale-config 分支也还没完全关掉,因此继续 CHANGES_REQUESTED。

  1. 恢复开始的“deactivation”阶段没有持久化/重启 reconcile,旧 daemon 仍可能在 activationPending:true 时在线。

    runPermissionRecovery() 先调用 holdRecoveryActivation() 把 activationPending:true 写入 bots.json,随后才 await runLiveStop()(src/dashboard/bot-onboarding.ts 约 1670 行)。如果 Dashboard 在 stop ACK 前退出,现有 daemon 不会因为磁盘 marker 改变而自行断开长连接。新 Manager 的 reconcileInterruptedManagedActivations() 只扫描 activationStarting,不会检查 activationPending,所以重启后也不会补 stop。

    我用 never-resolving stopBotLive 卡在这个精确 crash-point,再创建第二个 Manager 模拟 Dashboard 重启,结果是:

    {"pendingOnDisk":true,"restartStopCalls":0}

    也就是磁盘声称 pending,但旧 daemon 仍可能收消息。需要把 deactivating intent 也纳入 durable state/reconcile;重启看到这类 managed pending 必须 exact stop + absence readback,成功前不能进入扫码/完成路径。

  2. 当前 PM2 “精确 ACK”没有证明进程是本次 gated/quiescent activation daemon。

    isExactPm2BotApp() 只核对 process name、raw index、BOTMUX_LARK_APP_ID 和 online;BotmuxPm2App 也没有暴露 BOTMUX_MANAGED_ACTIVATION_APP_ID / job receipt。于是一个同 App/index 的旧普通 daemon 会走 already-online,即使它没有等待 activationStarting、早已在飞书长连接上。这个场景会被问题 1 直接触发:旧 daemon 未停 → complete 写 activationStarting → start-bot 把旧进程当 ACK → Dashboard 清 marker。

    managed activation 的 ACK 至少要区分“普通 online”和“本次 gated online”(最好绑定 jobId/nonce);遇到旧普通 row 应先 exact stop + absence,再从带 receipt 的 ecosystem entry 重建,不能返回 already-online。

  3. commit 后、ledger completed 落盘前仍有一个不可恢复窗口。

    commitManagedActivation() 先原子删除 activationStarting,返回后 activateRecoveredBot() 才 patch(liveStarted:true) 并持久化 completed ledger。若在两者之间退出,磁盘已经是 ready/daemon 已获放行,但 ledger 仍是 propagation-pending。重启后没有 activationStarting 可 reconcile;再次 completeScopePropagation() 会因为 beginManagedActivation() 要求 activationPending:true 而固定失败。

    我用真实生成的 pending ledger,构造“刚清 marker 就退出”的精确磁盘状态,重启后结果:

    {"result":{"ok":false,"error":"permission_recovery_activation_failed"},"startCalls":0}

    需要一个 durable committing/completed receipt,或在 restore 时能够无歧义地 reconcile “config 已 commit、ledger 未 commit”;并补这个 instruction-boundary crash test。

  4. 原 review 的 bootstrap failure 分支仍会用 stale config 注册。

    src/daemon.ts 中只有 profileBootstrap.ok 时才按 raw index 重新加载并断言 appId;bot_not_in_config / config_unavailable / validation_failed 仍只是 warn,随后直接 registerBot(cfg)。目标在 bootstrap 期间变为 pending/missing 时,parseBotConfigsFromText() 会过滤它并返回 bot_not_in_config,这里仍会继续使用 marker 之前读到的 stale cfg。显式 BOTMUX_BOT_INDEX 路径应在 所有 bootstrap 结果之后、registerBot 之前 再做一次 raw-index load + appId fence;pending/missing/drift 必须致命退出。

验证结果:

  • 新增针对性 3 suite:118/118 通过
  • 全量 unit:10167/10167 通过
  • pnpm exec tsc --noEmit:通过
  • pnpm build:通过(含 public-domain/dist audit)
  • git diff --check:通过

因此这些仍是 crash 时序与 ACK 语义的测试盲区,不是现有断言失败。请补 deactivation/commit 两端的 crash-point 测试、gated receipt 校验,以及 bootstrap failure fence 后再 re-review。未合并。

@BigTrue221

Copy link
Copy Markdown
Contributor Author

Addressed the second review in 304cd0b:

  • Persisted activationDeactivating before recovery stop; restart now re-runs exact stop/absence before any recovery or QR automation.
  • Added the per-job PM2 receipt (App ID + job ID); same App/index ordinary daemons are replaced rather than accepted as the activation ACK.
  • Added durable activationCommitted plus ledger sequencing. A restart with an unfinished commit is fail-closed: exact stop, then restore activationPending.
  • Explicit daemons now re-read their exact raw bots.json slot after every profile-bootstrap result and reject a pending/missing/replaced target before registerBot.

Validation: pnpm exec tsc --noEmit, focused activation tests (129 passing), and pnpm build pass. Full unit retains the pre-existing four failures in test/vc-meeting-daemon-session.test.ts; the activation paths are covered by the focused regression tests.

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

复审 304cd0b:前两轮 CHANGES_REQUESTED 的 4 个阻塞项(deactivation 持久化/reconcile、gated PM2 receipt 校验、commit/completed 崩溃窗口、bootstrap failure 后 stale-config fence)已全部修复并配套崩溃点测试。针对性 129/129、typecheck、公有域名审计通过;全量 unit 仅 3 个既有环境相关 suite 失败(master 同样失败,与本 PR 无关)。放行。

@deepcoldy
deepcoldy merged commit ec146a4 into deepcoldy:master Jul 24, 2026
qqxhb pushed a commit to qqxhb/botmux that referenced this pull request Jul 26, 2026
ec146a4(deepcoldy#572)把 foreign-bot 授权门扩大到所有 scope(含 native topic)并删掉
了 ownsSession 豁免,导致「同机兄弟 bot 首次 bot→bot 直连、接收侧 cross-ref
尚未学到对方 receiver-scoped open_id」被误判为未知外部 bot,弹 /grant 卡。
根因:cross-ref(bot-openids-<appId>.json)是被动从 mentions[] 学习,第一次直连
存在冷启动窗口;飞书 open_id 按接收应用隔离,不能靠自报 open_id 判断同伴。

修复:在判定 unknown、发授权卡前,新增一次 live sibling 补全:
- 新增 client.ts resolveSiblingBotBySenderOpenId:用当前群 live /members/bots
  roster,仅当 sender open_id 命中唯一同名本机配置 sibling、且该 app 独立
  is_in_chat 确认时才认(复用 auth-grade resolveCurrentChatBotOpenIdsByLarkAppIds
  做严格再校验)。任何 API 失败/歧义/同名冲突/真外部 bot 一律 fail-closed 回
  授权卡——绝不放行自报名字。
- event-dispatcher.ts gate:命中 sibling 则 updateBotOpenIdCrossRef 回填
  cross-ref(后续同一对 bot 直接命中 isKnownPeerBot,且同条消息下游 daemon
  evaluateTalk 的 peer 腿也随之放行),再 route;否则维持原授权卡逻辑。

影响面:仅收窄「配了 allowlist 的受限态 + foreign-bot @mention」这一门,未动
oncall/grant/team-peer/open-mode 其它腿。真外部 bot 授权语义不变(负向用例覆盖)。
已有 native-topic 授权卡回归测试仍绿(resolver 默认 fail-closed)。

测试:pnpm build 绿;新增 event-dispatcher 4 例(冷 sibling 自愈+回填、真外部
bot 仍弹卡、known-peer 不走 live、granted-bot 不走 live)+ list-chat-bot-members
7 例(resolver 正/负/歧义/roster 缺失/API 失败/空 open_id/不在群);定向
event-dispatcher(236)/list-chat-bot-members(32)/exact-chat-grant/peer-bot-operate/
team-peer-trust/grant-gates/bot-routing/grant-card 全通。

Co-Authored-By: Claude <[email protected]>
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