Skip to content

fix(dispatch): 未绑定派单的 --into 不再追加 --dispatch-root 回报协议 - #3765

Merged
deepcoldy merged 3 commits into
deepcoldy:masterfrom
big-omega:fix/dispatch-into-report-protocol
Nov 24, 2026
Merged

deepcoldy merged 3 commits into
deepcoldy:masterfrom
big-omega:fix/dispatch-into-report-protocol

Conversation

@big-omega

@big-omega big-omega commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

背景与原因

botmux dispatch --into <root> --bot-app <appId> 向已有话题追加消息时(例如 botmux-handoff 场景下的同话题接力,或向非 botmux dispatch 新建的普通话题派发),此前在 cmdDispatch 中无条件追加了以下尾部协议:

  • botmux report --dispatch-root <intoRoot>
  • 「不要在本话题 @ 来源 bot(那会另起一个没有上下文的新会话)」

但 --into 路径不会向 orchestrate-dispatch.json 写入派单绑定(这是刻意的安全不变量,避免 CLI 认领任意既有话题的回报权限)。结果子 bot 执行 botmux report --dispatch-root <intoRoot> 时必然查表 404,触发 dispatch_target_gone 兜底卡片(未送达主编排:派单目标已不在);同时因为尾部禁止在当前话题 @ 来源 bot,来源会话无法被唤醒。

改动内容

  • src/core/dispatch-report-binding.ts:新增 hasVerifiedDispatchReportBindingForCaller,复用 resolveVerifiedDispatchReportTarget 校验 orchestrate-dispatch.json 中的 HMAC 签名与 attachment 有效性,并校验绑定是否归属当前调用方(targetLarkAppId + targetSessionId,或同 app 同群的 chat-scope 续接会话)。
  • src/core/report-session-relay.ts:收紧既有 fallback 的持久行补充规则:原会话实际为 thread-scope 时拒绝落入 chat 候选;已签名 targetChatId 优先于移动后的持久行,持久行只能补充旧绑定缺失的字段,不能推翻签名边界。
  • src/cli.ts(cmdDispatch):
    • 验签先行:只要 --into 命中指向当前调用方的有效签名绑定(包括 chat-scope 编排会话在已绑定子话题内被触发的场景),继续追加精确的 botmux report --dispatch-root 协议;
    • 未命中有效绑定时,--bot-app 与同话题 legacy --bot 不再追加完成回报协议,跨话题 legacy --bot 保持原有兼容协议;
    • chat-scope 同话题判定只采用新鲜进程树 marker / protected claim 的精确 turnId;宿主 watcher 重执行仅在授权标记与 worker parent PID 绑定同时成立时使用其新注入的 turn。普通继承的 BOTMUX_TURN_ID 与 quoteOnly 引用回复均保守按跨话题处理,不借用可能已被后续入队消息覆盖的 currentReplyTarget;
    • 对缺少 targetScope / targetChatId 的旧签名绑定,复用 resolveReportRelayFallbackTarget 与宿主会话记录,只在原会话已关闭/缺失且当前调用方是同 app、同群唯一活跃 chat-scope 续接者时保留协议;活跃原会话、跨群、多候选或 fallback 选中其他会话均 fail-closed;
    • 对无有效绑定的 --into 提前拒绝 --result-delivery publish|publish-and-relay,避免发送后子 bot 回报阶段才 404。
  • 测试:在 test/dispatch-report-binding.test.ts、test/dispatch-thread-id.test.ts、test/dispatch-result-delivery-cli.test.ts、test/sandbox-relay-watcher.test.ts 补充行为回归用例,覆盖未绑定/他人绑定/本会话绑定、旧绑定唯一续接、quoteOnly、过期 env turn、parent-bound 宿主重执行、排队覆盖 currentReplyTarget、缺失 targetChatId 及未绑定公开投递拒绝。

影响范围评估

  • 平台(macOS / Linux):涉及 orchestrate-dispatch.json 只读验签、宿主会话记录与 CLI 文本组装,无平台分支差异。
  • CLI 与后端:只改动 botmux dispatch --into 的完成回报协议拼装与前置校验,并收紧 report-session-relay 内部 fallback 目标判定;不触碰各 CLI 适配器、PTY/tmux 后端,/api/report-relay 与 /api/trigger wire format 均不变。
  • 会话类型:
    • thread-scope 新话题派单(无 --into):行为不变;
    • thread-scope / chat-scope 对已注册子话题的 --into:保留精确回报协议;
    • 普通话题 / 同话题 --into:不再追加失效的 --dispatch-root;
    • 跨话题 legacy --bot:保持原有 --legacy-dispatch 兼容路径。
  • Sandbox:--bot-app 经宿主 watcher 重执行,在宿主读取 orchestrate-dispatch.json、签名密钥与会话记录;watcher 先清除继承 turn,再注入 host authorize 返回的新 turn,调用方另外校验授权标记与 worker parent PID。无 secret、读表失败或身份/续接证明不足时 fail-closed。

测试验证

bun run test -- \
  test/test-hygiene.test.ts \
  test/dispatch-report-binding.test.ts \
  test/dispatch-thread-id.test.ts \
  test/dispatch-result-delivery-cli.test.ts \
  test/dispatch.test.ts \
  test/cli-dispatch-into-standby.test.ts \
  test/report-relay-fallback-cli-daemon.test.ts \
  test/report-session-relay.test.ts \
  test/sandbox-dispatch-routing.test.ts \
  test/multi-topic-feature-gate.test.ts \
  test/sandbox-relay-watcher.test.ts

bun run test -- \
  test/vc-meeting-send-policy.test.ts \
  test/session-marker-resolve.test.ts \
  test/cli-send-hook-context.test.ts

bun run test -- \
  test/report-recipient-cli.test.ts \
  test/reply-target-fallback.test.ts

bun run typecheck
bun run build
  • 第一组 11 个测试文件共 419 条用例全部通过;相邻 turn / parent 绑定回归 78 通过、3 跳过;quote/report recipient 回归 153 条全部通过。
  • tsconfig.typecheck.json 类型检查 0 错误,完整构建与产物审计通过。
  • 变异复验:原 chat-scope 三条件、targetChatId 存在性、quoteOnly、旧 env 回退、宿主授权/parent 绑定、旧绑定 fallback 与目标匹配、原会话 thread scope、签名 chat 优先级共 12 项变异均咬红。
  • 说明:未替换本机运行的二进制或重启 daemon;本次仍属 CLI 文本拼装、只读验签与宿主会话记录判定。

对抗复核记录

  • 人工中途干预:覆盖原会话仍活跃、原会话关闭、当前调用方已关闭、原会话迁移、多候选与跨群。另发现并已修复两个交叠问题:
    • P1 / 旧绑定的 thread 原会话误落 chat 续接者:修复前真实 cmdDispatch 探针仍发送 publish 协议,真实 relay 在原会话 404 后向 chat 候选发出第二次 trigger;修复后 CLI exit:1 / sent:0,relay 返回 409 fallback_scope_unsupported 且没有第二次 trigger。回归位于 report-session-relay.test.ts 与 dispatch-thread-id.test.ts。
    • P2 / 移动后的持久行覆盖签名 chat:修复前直接绑定校验为 false,但 fallback 采用移动后的 chat 并放行 CLI 协议;修复后保留签名 originalChatId,移动后的候选不再可用,CLI exit:1 / sent:0。删除签名 chat 优先级的变异会咬红。
  • 多步写入只完成一半:本次没有新增持久写入或绑定认领,所有新增证明都在发送前只读计算;未注册、签名不符、密钥/会话证明不足均按未绑定处理。取消、重启及半完成写入的额外时序怀疑未形成可复现的新缺陷。
  • 周期循环 / 长等待窗口:真实 watcher 探针确认 200ms tick 重执行前会清除继承 turn,只注入 host authorize 的新 turn;排队覆盖、旧 env、错误 parent PID 均有回归用例。fresh marker 优先于 env 与后续 currentReplyTarget,quoteOnly 保守保留跨话题协议。
  • 既有的候选后迁移竞态(非本 PR 引入,未宣称修复):受控等待点探针显示,fallback 候选选定后若其 chat/scope 再变化,既有 deliverReportSessionRelay 仍会按 sessionId trigger 并返回 200。本 PR 未修改该投递段;彻底修复需要目标 daemon 在接收/入队临界区原子校验预期 chat/scope 或路由代际,CLI 重读 /api/sessions 不能关闭该窗口。

🤖 Generated with Claude Code

botmux dispatch --into 在向普通话题或同话题接力追加消息时,此前会无条件追加「botmux report --dispatch-root <intoRoot>」和「不要在本话题 @ 来源 bot」的尾部协议;由于 --into 不向 orchestrate-dispatch.json 写入绑定,子 bot 执行回报时必然命中 dispatch_target_gone(未送达主编排:派单目标已不在)且无法唤醒来源 bot。

本次改动:
- 新增 hasVerifiedDispatchReportBindingForCaller,按已签名绑定校验归属当前调用方(含同 app 同群 chat-scope 续接会话)
- cmdDispatch 中验签先行:命中指向当前调用方的有效绑定时保留精确回报协议(含 chat-scope 同话题触发);未命中有效绑定时,--bot-app 与同话题 legacy --bot 不再追加完成回报协议,跨话题 legacy --bot 保持兼容
- chat-scope 同话题判定仅使用本轮精确 turnId(祖先上下文优先,缺失时回退 BOTMUX_TURN_ID),不借用可能已被后续排队消息覆盖的 currentReplyTarget
- 对无有效绑定的 --into 提前拒绝 --result-delivery publish|publish-and-relay
- 补充绑定校验、--into 回报协议门禁(含 chat-scope 精确回合与已绑定同话题)及公开投递守卫单测
hasVerifiedDispatchReportBindingForCaller 的 chat-scope 续接分支此前只有
同群成功续接的正向用例,摘掉 callerScope/targetScope/chatId 相等任一条件
测试全绿。补充跨群、thread-scope 调用方、缺失 callerChatId、thread 绑定
不走续接分支四类负向断言(含同条件正向对照),三个变异复验均咬红。

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

Copy link
Copy Markdown
Owner

自动评审(初步意见,最终以维护者审阅为准):

结论:修复方向正确,生产代码未发现阻断问题;已在作者分支追加一个测试补强 commit(401cd80fcf)。

评审要点:

  • 根因核实无误:--into 路径不写 orchestrate-dispatch.json 绑定(daemon 仅在新话题注册路由写 reportBinding),旧代码却无条件在简报尾部追加 botmux report --dispatch-root <root>,子 bot 回报时 resolveVerifiedDispatchReportTarget 必返 dispatch_target_unavailable(404),落入 dispatch_target_gone 兜底卡;同话题 legacy --bot 还附带「不要在本话题 @ 来源 bot」,来源会话无法被唤醒。
  • 新门控复用既有 HMAC 验签体系(resolveVerifiedDispatchReportTarget + 宿主密钥),不引入新的信任假设;无 secret/读表失败一律 fail-closed 视为未绑定,方向正确。
  • hasVerifiedDispatchReportBindingForCaller 的调用方匹配顺序(appId → sessionId → 同 app 同群 chat-scope 续接)与报告中继服务端 dispatch_route_mismatch 的独立校验形成两层防护。
  • chat-scope 同话题判定改为只按本轮精确 turnId(祖先上下文 → BOTMUX_TURN_ID)经 pickTurnReplyTarget 取 root,不借用会被排队消息覆盖的 currentReplyTarget,时序处理正确。
  • 未绑定 --into 前置拒绝 publish / publish-and-relay,把原来发送后才在回报阶段 404 的失败提前到发送前。

测试补强(401cd80fcf):新增的 hasVerifiedDispatchReportBindingForCaller 此前对 chat-scope 续接分支只有正向用例。变异复验发现摘掉 callerScope === 'chat'、targetScope === 'chat'、targetChatId === callerChatId 三个条件中的任一个,全部测试仍绿。已补充跨群、thread-scope 调用方、缺失 callerChatId、thread 绑定不走续接分支四类负向断言(含同条件正向对照),重放三个变异均咬红。

本地验证:10 个相关测试文件 388 用例全绿、另跑 10 个相邻测试文件 783 用例全绿、tsconfig.typecheck.json 0 错误、bun run build 通过。

@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 401cd80fcf 复核结论

生产代码未发现新的 P1/P2 缺陷。chat-scope 续接仍按同 app、同群、双方 chat-scope 收窄;cmdDispatch 使用本轮精确 turnId 经 pickTurnReplyTarget 判定同话题,报告中继的签名验证与 dispatch_route_mismatch 二层校验也仍在。追加测试已修复此前三个存活变异,但还发现 1 个 P2 测试缺口。

P2:targetChatId 存在性守卫缺少负向覆盖

  • 守卫位置:src/core/dispatch-report-binding.ts:327-330;现有负向用例:test/dispatch-report-binding.test.ts:211-258。
  • 分别移除 callerScope === 'chat'、targetScope === 'chat'、targetChatId === callerChatId,新增用例均按预期咬红;但只移除 !!binding.targetChatId 后,test/dispatch-report-binding.test.ts 仍是 47/47 全绿。
  • 真实函数对照探针:构造签名有效、targetScope: 'chat' 但没有 targetChatId 的绑定,以不同 sessionId、callerScope: 'chat' 且不带 callerChatId 的调用方查询,原逻辑返回 false;仅删除上述存在性守卫后返回 true。
  • 当前生产守卫本身正确,这不是已发生的生产行为回归;但对新增导出函数而言,该输入可达,且按本 PR 已采用的变异标准,这个分支尚未被测试钉住。建议补一条“绑定缺少 targetChatId、调用方也缺少 callerChatId 时不得按 chat-scope 续接命中”的负向断言,无需修改生产代码。

本地验证

  • PR 所列 10 个测试文件:388/388 通过。
  • 三个直接相关测试文件:122/122 通过。
  • bun run typecheck:通过。
  • 变异复验:上述三类条件变异均咬红;targetChatId 存在性变异存活,证据如上。
  • 变异后源码已恢复,工作树干净;本次仅复核,未推送新 commit。CI 尚有任务在排队/运行中。

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

补充更正:行为探针坐实 3 个生产问题

上一条评论中“生产代码未发现新的 P1/P2 缺陷”的结论作废。进一步用真实 cmdDispatch harness 和现有 report fallback 复现后,当前 head 401cd80fcf 有以下合前需修复的问题。

P1:quoteOnly 回合被误判为位于所引用话题

位置:src/cli.ts:16935-16938。

pickTurnReplyTarget 返回的 quoteOnly 没有参与 currentTopicRoot 判定;但既有 resolveSendTarget / resolveInboundReplyTarget 明确把这种情况作为 quote 而非 thread,即当前可见落点不是该话题。

探针:chat-scope 精确 turn 的 replyTargets[turn] = { rootMessageId: 'om_existing', quoteOnly: true },执行未绑定 legacy --into om_existing。期望保留跨话题 --legacy-dispatch,实际简报只有原始“完成任务”,回报协议被省略。建议仅在精确 reply target 非 quoteOnly 时采用其 root 作为当前话题。

P1:脱离进程树后使用会过期的 BOTMUX_TURN_ID

位置:src/cli.ts:16932-16936。

resolveSessionContext 的进程树 marker / protected claim 才携带新鲜 turn;环境变量是 spawn-time 快照。cmdReport 在 src/cli.ts:17660-17662 已明确禁止为该目的复活这个环境值,但本次 dispatch 又做了同样的回退。

探针:无 fresh process-tree context,旧 BOTMUX_TURN_ID 对应 om_existing,当前回复槽已指向另一 turn/root;执行未绑定 legacy --into om_existing,实际仍被当成同话题并删掉 --legacy-dispatch。建议同话题省略必须依赖新鲜 marker/protected capability;宿主 watcher 若需支持该优化,应传可验证的新鲜身份,而不是普通继承环境变量。

P2:旧签名绑定与现有 report fallback 的兼容语义不一致

位置:src/core/dispatch-report-binding.ts:325-330。

targetScope / targetChatId 是后续新增的可选字段,现有 test/report-session-relay.test.ts:524-543 明确保证无 targetScope 的旧绑定可借助原会话关闭记录回退到唯一 chat-scope 续接者。新 helper 却一律拒绝这种续接,导致 --bot-app --into 不再附带本可投递的回报协议,publish / publish-and-relay 还会被提前拒绝。

真实函数对照:同一旧绑定场景下,hasVerifiedDispatchReportBindingForCaller 返回 false,而 resolveReportRelayFallbackTarget 返回 { ok: true, target: session-current, reason: original_session_closed }。建议结合宿主会话记录复用现有 fallback 的证明条件,或提供安全迁移/重签;不要直接放开无法证明归属的旧绑定。

仍存在的 P2 测试缺口

上一条记录的 targetChatId 存在性守卫变异仍成立。建议负向用例已做临时双向验证:原守卫下 48/48 通过;删除 !!binding.targetChatId 后新增用例如期咬红。探针后均已恢复,工作树干净。

验证补充

  • 上述 quoteOnly 与旧 turnId 两条临时回归用例在真实 cmdDispatch harness 上均复现失败,其余 64 条通过。
  • PR 原 10 个测试文件 388/388 通过,bun run typecheck 与 bun run build 均通过;这些全绿不能覆盖上述场景。
  • 本次只发布复核结论,未推送新 commit。另请在本 PR 的通知投递/会话续接语义改动完成后,按仓库要求把人工干预、半完成写入、周期/等待窗口交叠三类对抗场景与探针结果补入 PR 描述。

- quoteOnly 与无新鲜上下文的旧 env turn 均保守保留跨话题协议;宿主重执行须同时满足授权标记和 worker parent 绑定。

- 旧绑定续接复用 report fallback 的唯一候选证明,并拒绝 thread 原会话及持久行覆盖签名 chat。

- 补齐真实 CLI、watcher、relay 回归与十二项变异复验。

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

Copy link
Copy Markdown
Owner

修复已追加到 987defb564ebe5cfde03bdcd0b50322094aeb48b,保留原有两个提交,未 force-push。

  • quoteOnly 与无新鲜上下文的旧 env turn 均保留跨话题 legacy 协议;宿主重执行只接受授权且 parent-bound 的新 turn。
  • 旧绑定续接复用共享 fallback 的唯一候选证明;原会话 thread-scope、跨群、多候选、候选非当前调用方均拒绝。
  • 对抗复核新增的两个反例也已修:thread 原会话不再落入 chat 候选,移动后的持久行不再覆盖签名 chat。
  • targetChatId 存在性负向用例已补;最终 12 项变异全部咬红。

验证:PR 相关 11 文件 419/419;相邻 turn/parent 回归 78 通过、3 跳过;quote/report recipient 153/153;sandbox 共用路径 62/62;bun run typecheck 与 bun run build 通过。

PR 描述已更新三类对抗场景和探针证据。另如实记录了一个非本 PR 引入的既有候选后迁移竞态:彻底修复需要目标 daemon 在 trigger 入队临界区校验路由代际,本次未扩展该 wire protocol。

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

G7 非相邻豁免背书

认可在 PR head 987defb564ebe5cfde03bdcd0b50322094aeb48b 上直接 squash,无需仅因本次 master 前进而同步生成新 head 并重跑首跑 CI。本背书覆盖已核到的 master 67ca6e9316be7e478d956636b867813abe8fe311;若 PR head 或 master 再变化,需重新核对交集。

核验证据:

  • 两个 CI run 的 head_sha 均为上述 PR head,run_attempt=1 且 conclusion 均为 success;当前全部 check 为 SUCCESS。
  • 从原 merge-base 4c32e2be57 到最新 master 的整段 delta 与 PR 的唯一交集文件仍为 src/cli.ts。复核期间新增的 #3778 只改 src/worker.ts、test/helpers/worker-function-harness.ts、test/worker-cot-malformed-entry.test.ts,与 PR 零交集。
  • 固定 SHA 重算 git merge-tree --write-tree:
    • fe6ce58ac8 + 987defb564e → 306765ed156aa9b61ba03b4a3ec033d7c3ad0345,与既有证据一致;
    • 最新 67ca6e9316 + 987defb564e → 39f736eeb883059935219a0769c3414bedff72fc,无冲突;
    • 两棵合并树中的全部 PR 文件逐字相同。
  • src/cli.ts 内两侧并非相邻 hunk:PR import 区在合并后 331/341 行,master 最早 setup hunk 从约 929 行开始;PR dispatch 改动从约 17178 行开始,master 最后的 schedule hunk 在约 10151 行。除 import 区外分属 setup/schedule 与 dispatch 不同函数。
  • package.json 与 bun.lock 到最新 master 均未变化。
  • test/schedule-cli-update.test.ts 在合并树中与最新 master 逐字相同;失败用例确实通过 chmodSync(..., 0o000) 构造不可读输入,而本机 uid=0。该单条本地失败按 root/DAC 环境限制记录,不作为本 PR 回归,也不影响上述非相邻判断。

@deepcoldy
deepcoldy merged commit af1da2f into deepcoldy:master Nov 24, 2026
15 checks passed
@deepcoldy

Copy link
Copy Markdown
Owner

已合并,感谢贡献!

这个修复让 botmux dispatch --into 在目标话题没有已验证派单绑定时,不再追加必然 404 的 botmux report --dispatch-root 回报指令;同时保留了新话题派单、跨话题 legacy --bot 以及旧绑定唯一续接会话的兼容路径,chat-scope 同话题判定也收紧为只信新鲜的进程树 turn 身份。

合入过程中补充的内容:

  • 收紧了引用回复(quoteOnly)与长驻 CLI 过期 BOTMUX_TURN_ID 两类同话题误判;
  • 旧签名绑定(无 targetScope/targetChatId)在原会话关闭时,复用既有 relay fallback 的唯一候选证明保留回报协议;
  • 补齐 chat-scope 续接身份判定的负向测试,相关守卫均经变异复验。

未尽 follow-up:评审中发现一个非本 PR 引入的既有竞态——relay fallback 候选选定后若会话在入队前迁移,trigger 只按 sessionId 投递、缺少 chat/scope 临界校验,已在 #3793 跟踪。

最终以维护者审阅为准,欢迎继续参与。

@github-actions

Copy link
Copy Markdown

@big-omega @deepcoldy 合入 master 后的 Midscene E2E 失败:Feishu live Midscene 失败。

这次运行覆盖 0d3e6e5…3d0de8711(7 个提交),同批合入的还有 #3764 #3762 #3758 #3778 #3766 #3763,失败不一定由本 PR 引起。

用例 跑道 状态 失败信息
Antigravity basic bot flow feishu-core-clis failed showStreamingOutput: the card's output toggle never read 「📕 隐藏输出」 within 45s (clicked 「显示输出」: yes; unfolded 「更多操作」 2×; reloaded the topic: failed (Feishu Messenger chat list did not load after 4 att…
Claude group reply mode topic answers in topics that share one session feishu-core-group-modes failed Node "feishu.runScenario" timed out after 1800000ms.

@github-actions

Copy link
Copy Markdown

🚀 Released in v3.59.32

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.

3 participants