Skip to content

fix(auth): 跨群 Bot 回执绑定原任务续办身份 - #3820

Merged
deepcoldy merged 8 commits into
deepcoldy:masterfrom
will7856:fix/task-continuation-20261125
Nov 25, 2026
Merged

deepcoldy merged 8 commits into
deepcoldy:masterfrom
will7856:fix/task-continuation-20261125

Conversation

@will7856

@will7856 will7856 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

问题

跨群 Bot 回执的当前 actor 必须保留为真实回执 Bot,但原任务真人又需要在严格绑定原请求的前提下恢复既有读取操作。若只依赖 preferred reader、机器账号或回执 Bot 自报身份,无法证明回执对应的源会话、源 turn 和原请求,容易造成跨任务串权或重放。

改动

  • 在 host-only 的跨群 follow binding 中保存 sourceTurnId,仅从仍有效的源会话精确真人 turn 生成独立 taskContinuation。
  • 当前 actor 始终保留为回执 Bot;continuation 单独携带 app、真人、源 session 和源 turn,并通过 daemon IPC、worker 与 MCP Gateway 的 host-owned 路径传递。
  • principal 相等性同时比较即时 Bot actor 与完整 continuation,failed-turn 合并也复用同一判断,避免不同任务或不同来源身份合并。
  • continuation 在 live worker 与 adopted pane 中均强制串行;pending repo、durable follower/head-tail 等路径冻结各自 continuation,不让新 tail 身份被旧 head 借用。
  • source turn 关闭、剪枝、改绑、Bot/synthetic/recovery/cross-chat 派生等情况 fail closed;legacy receipt 补录保留原发送时间,避免重放覆盖后续真实绑定。

影响范围

  • 修改 daemon/worker、跨群 follow binding、active-turn authority 与插件 MCP Gateway 的共用身份传播路径。
  • 不改变普通真人 IM、schedule、listener、普通 Bot 委托、slash/ask/directive/XPI 的身份语义;continuation 只用于既有任务的 worker identity propagation。
  • 不包含权限扩张、插件安装、正式发布、daemon 重启或线上数据迁移。
  • 不涉及平台路径或系统调用差异;Linux/macOS 共用纯数据与队列逻辑。PTY/Tmux、普通/adopt/durable 会话的相关边界均有定向回归。

验证

  • 定向组合:11 个测试文件,827 项通过。
  • MCP Gateway continuation 定向用例:1 项通过,51 项按筛选跳过。
  • test/test-hygiene.test.ts:28 项通过。
  • 完整 Gateway 文件:42 项通过;其余 10 项受当前共享依赖缺少 smol-toml 及既有 socket 夹具环境问题阻断,未将其记为通过。
  • bun run typecheck:当前共享 node_modules 缺少 smol-toml、pg 及相关既有类型,未完成全量类型验收;本次定向测试未出现新的明确类型错误。
  • 对 principal equality、source-turn current guard、failed-turn 隔离、replay 原时间、durable propagation、adopt admission/write-edge 串行等新增守卫做过变异复验,移除守卫时对应测试会失败。
  • 独立对抗复核覆盖人工中断/改绑、多步持久化部分完成、重放与长操作交叠;未发现新的 P0–P3 阻断项。

未验证边界

未部署本地 checkout,未重启 daemon,也未执行真实跨 Bot 生产写入验证;待正式合入并发布后再使用正式版本验收。

CI 修正(9f947216e166)

回查上一轮 CI 后,在本地原样复现 3 个失败断言,原因归于两处旧测试接入遗漏:

  • api-only-mode-wiring 将所有主体解析都算作 6 个直接消息入口。现在单独校验源 turn 的主体解析,原来 6 个入口的三态 sender 校验全部保留,不能把实际 Bot 消息标成真人。
  • new-topic-lark-caller 的真实函数执行夹具缺少新增的 turnTrustedCaller 自由变量。补齐与普通消息路径一致的依赖,同时保留“真人替换占位”和“无可信 caller 清除占位身份”两项行为断言。
  • 增加来源主体解析为 Bot、来源主体属于其他应用时必须拒绝的回归。

本次增量只修改 3 个测试文件,没有改变生产代码或权限门禁。

验收:修前定向 3 failed;修后使用真实依赖和仓库正式 Vitest 配置,5 个文件 96/96 通过;Bun 对两个精确测试文件 22/22 通过(与 Vitest 存在重叠,不累计为独立用例)。缺失开发依赖仅放在隔离临时目录,共享 node_modules 未修改,工作区依赖链接已恢复。没有部署本地版本、切换正式入口或重启共用 daemon。已提交同一 PR,远端新轮检查结果以平台为准,不将本地通过写成全量 CI 通过。

最终 head(054027520b)安全边界补充(评审收敛后)

后续修复在原方案上收紧了三处边界,最终行为以此为准:

  • 回执 Bot 正面授权:跨群 follow 路由对非 blocked Bot 的 talk 豁免只保留给旧式无 caller 路由;只有回执 Bot 在 followedChatId 上通过 evaluateBotTalk 授权时才构造源真人 taskContinuation,且源 principal 解析完成后二次复验(授权在 await 期间被撤销则不签发),未授权 Bot 的消息仍正常到达但不带任何真人身份。
  • MCP 元数据本地隔离:botmuxTrustedCaller(含 taskContinuation、真人 openId/unionId/邮箱)只注入本地 stdio 插件的 JSON-RPC 元数据;streamable-http 连接不再收到任何 caller 元数据(HTTP 头此前已是白名单剥离,本次对齐 body 路径)。stdio 本地插件行为不变。
  • 持久 principal-lane FIFO:带 continuation 与无 caller 的跨群回执都进入 lane 排队,不为后者伪造身份;enqueue/attempting 落盘失败时回滚内存状态并保留重试,避免队头卡死或重复入队。
  • worker IPC 入口、adopt 写入与 sendToPty 统一串行归一化:promoted tail 即使没带显式标记、或活跃回合是 continuation 时的无 caller 输入,都不会拼接进正在运行的回合。

最终验证(head 0540275,完整依赖环境)

  • bun run typecheck、bun run build:通过;test-hygiene 28/28。
  • 本 PR 触及的 12 个测试文件 787 项全绿;authority/cross-principal/failed-turn 等邻居回归 161 项全绿;合并最新 master 的本地合并树(merge-tree 5ac4726,零冲突)上含交集提交自带测试共 972 项全绿。
  • 独立变异复验:移除授权门、HTTP 隔离门均有测试确定咬红;adopt/lane/串行归一化各守卫同。
  • 未做真机跨 Bot 生产写入验证,发布后按正式版本验收。

最终 head(c4adc49518)边界再收紧

  • adopt 初始输入与 busy type-ahead 统一遵守续办串行约束,预留中的 init 回合不会被当作空闲 pane 提前写入。
  • 从通用 taskContinuation 移除 verifiedEmail,MCP 投影层只透传 appId/openId/unionId/sourceSessionId/sourceTurnId 五个公共字段,旧 worker 持久化数据里残留的邮箱或未知私有字段一律过滤。
  • 同毫秒跨会话/跨 root 的归属判为歧义并拒绝路由(fail closed),时间戳更大的唯一归属正常生效;同 provider receipt 的在途补录允许 enrich,但该 legacy 回合保持无 caller,换 receipt 仍拒绝。
  • remote-runner 对续办上下文做严格白名单投影(前缀与 ID 校验、appId 匹配、仅 bot actor),预派发校验失败在占用 active turn 前返回 untouched,保留后继重试能力。

最终验证(head c4adc49)

  • typecheck、build、test-hygiene 28/28 通过。
  • PR 自有 15 个测试文件 858 项全绿;本 commit 新增 8 文件 189 项;与最新 master(9ceff13)的本地合并树三点一线(tree 6e73ddb,交集文件改动非相邻、零冲突),含交集提交自带测试共 877 项全绿。
  • 五组守卫变异(同毫秒歧义、remote-runner 投影、type-ahead 串行、gateway 字段投影、legacy 补录门)摘除均确定咬红;唯一冗余的 init 内联标记经结构证实不可独立达,列为合后清理。
  • CI run 45606149844 首跑(attempt=1)全部 14 个 job success。
  • 未做真机跨 Bot 生产写入,发布后按正式版本验收。

王波 and others added 2 commits November 25, 2026 12:49
区分原任务主体解析与六个原有消息入口,保留 Bot 不得冒充真人的全部原门禁;为占位话题替换场景补传实际调用者,并增加来源主体为 Bot 或跨应用时的拒绝回归。

Co-Authored-By: Aiden
failed-turn 合并改用 sameTrustedPrincipal 后,原 alice/bob 夹具无法区分
新旧规则(openId 本就不同);新增同回执 Bot 不同 taskContinuation 的
折叠拒绝用例,变异回旧的 appId+openId 内联规则会咬红。补录门禁补两条
区分性用例:receipt 不匹配时不得给旧回执盖新 turn 归属、已带归属的
回执不得被重盖到别的 source turn,对应两个此前变异存活的守卫。

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

Copy link
Copy Markdown
Owner

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

整体评审结论:生产代码的身份建模与 fail-closed 边界未发现阻断问题,续办身份(TrustedCaller.taskContinuation)与即时 Bot actor 分离、principal 比较纳入完整续办元组、daemon/worker/adopt 三条路径强制串行的设计自洽。以下为评审中发现的测试覆盖缺口,已由追加 commit 30aca013a2(仅测试、零生产改动)补齐。

变异复验中发现的存活变异(已补咬红用例)

  1. failed-turn 合并判断(P2):mergeKeepsFailedInputDelivered 从内联的 appId + openId 比较改为 sameTrustedPrincipal 后,既有 alice/bob 夹具因 openId 本就不同,无法区分新旧规则——把函数变异回旧内联规则,全部 rewind 测试(209 项)仍绿。新增「同一回执 Bot、不同 taskContinuation 元组不得折叠失败输入」用例(test/failed-turn-rewind-daemon.test.ts),变异回旧规则即咬红,同元组重试仍可正常 merge。
  2. 绑定补录的两个门禁(P3):ensureCrossChatFollowThreadAtBinding 中 latest.messageId !== input.messageId(只允许补录同一张 provider 回执)与 latest.sourceTurnId(已归属的回执不得重盖)两个条件此前无区分性用例,各自单独摘除后测试全绿。test/cross-chat-follow-thread-store.test.ts 新增两条对应用例,摘除任一守卫即咬红。

已知不补的存活变异(均为当前不可达状态的纵深防御,附理由)

  • worker sendToPty 内由 trustedCaller.taskContinuation 二次推导 queueAfterActiveTurn:唯一另一调用方(raw-input follow-up)不传 trustedCaller,生产消息入口已在 daemon 侧打标,该推导是不可独立到达的第二道防线,保留不改。
  • daemon 构造段的 source.larkAppId !== larkAppId 检查:上游 resolveCrossChatFollowedThread 已按同 app 过滤候选会话,属重复纵深校验。
  • 补录门禁中的 off-tombstone 条件:生产侧 off 记录(persistCrossChatFollowThreadOffBeforeProvider)结构性不带 messageId,该分支当前不可达;且下游 resolveCrossChatFollowedThread 仍会跳过 off 记录,存在第二道独立防线。

验证记录(head 30aca013a2)

  • bun run typecheck、bun run build:通过
  • 定向 13 个测试文件 799 项全绿(含本 PR 全部新增/改动测试与 authority、cross-principal、follow-up 回归族)
  • 变异复验:continuation principal 比较、daemon 强制串行、adopt 串行准入、sourceTurnId 时效守卫、补录保留原时间戳、fail-closed 交叉校验、refork 冻结、gateway 传递共 8 处守卫摘除后均有测试咬红;上述 3 处存活变异已逐条说明不可达理由
  • 未做真机跨 Bot 写入验证,与 PR 描述的未验证边界一致

worker 的 sendToPty 内联了「trustedCaller 带 taskContinuation 即强制串行」
判定,该闭包没有单测 seam,变异后全套件存活。将判定抽到
pending-input-queue 的 inputForcesSerialTurn 纯函数,worker 改为调用它,
生产行为不变;补三个单测钉住显式串行标记、continuation 无标记也串行、
普通输入保持 type-ahead 三类行为,移除 continuation 分支时精确咬红。

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

Copy link
Copy Markdown
Owner

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

首审结论:生产代码未发现阻断项。跨群续办身份链路的 fail-closed 设计核查通过——源 turn 仅接受 om_/join_ 真人开窗且排除 recovery/synthetic/cross-chat/剪枝条目;当前 actor 保持为回执 Bot,taskContinuation 独立携带 app/真人/源 session/源 turn;MCP Gateway 仅在本地 stdio 的 trusted meta 暴露该字段、不进 HTTP 头,既有 verified-human-email 授权仍要求 senderType === 'user',无隐式扩权;同 app 校验与 host-only 绑定 journal 也都在位。

独立变异复验中发现 1 处测试缺口(生产守卫本身正确):worker 终端边界「trustedCaller 带 taskContinuation 即强制串行」的判定内联在 sendToPty 闭包里,没有单测 seam,移除后全套件存活。

已代修推送到本分支(提交 d8f1033,仅测试与等价重构,无生产行为变化):

  • 抽出 src/utils/pending-input-queue.ts 的 inputForcesSerialTurn 纯函数承载该判定,worker.ts 改为调用;
  • 补 3 个单测钉住:显式串行标记、continuation 无标记也强制串行、普通输入保持 type-ahead;移除 continuation 分支时精确咬红(仅新用例失败,其余 29 项不受影响)。

本地验证(基于最新 master 8c9e65e rebase 后):bun run typecheck 零错误;13 个定向测试文件 802 项全部通过;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.

复审结论:发现 2 个 P1,请求修改

复审对象:当前 head d8f1033ed623571ba9859a58f505a02737491ce1。本地 rebase 副本与 PR 四个提交的 git range-diff 全部相等。

P1 — 未授权的外部 Bot 也能获得源真人的 taskContinuation

src/daemon.ts:30946 构造 continuation 时,对当前回执发送者只要求 rawThreadTrustedCaller?.senderType === 'bot';buildTaskContinuationCaller 也只重复检查 senderType,没有验证该 Bot 是否是本次跨群投递的受托对象,或是否通过既有 Bot talk 授权。

上游现有准入并不能补足这个条件:src/im/lark/event-dispatcher.ts:6356-6361 对跨群 follow 只要求 botTalk.reason !== 'blocked'。现有测试 test/cross-chat-thread-binding.test.ts 甚至明确覆盖了“未入 allowlist 的外部 Bot 也豁免 talk gate 并路由到源会话”。因此,源 turn 自身的真人认证虽然 fail closed,回执 Bot 这一侧却没有委托边界:目标群里的陌生 Bot 只要回复被 follow 的话题并 @ 源 Bot,就能让源会话带着原真人的 continuation 执行它提供的指令;依赖该字段恢复读取能力的插件会把它当成原任务续办。

探针证据(真实 dispatcher / daemon 路径,临时探针已清理):

  1. 运行现有 dispatcher 用例 exempts foreign bots and non-allowlisted humans ...,确认未授权外部 Bot 会生成 crossChatThreadFollowUp 并进入源会话。
  2. 将该准入形状送入真实 handleThreadReply,发送者使用不在 allowlist 的 ou_topic_cont_stranger_bot,实际 fork 参数为:
PR3820_UNKNOWN_BOT_CALLER={"requestUserOpenId":"ou_topic_cont_stranger_bot","requestLarkAppId":"topic_cont_worker_app","senderType":"bot","taskContinuation":{"appId":"topic_cont_worker_app","openId":"ou_topic_cont_owner","sourceSessionId":"sess-live","sourceTurnId":"om_origin_human"}}

建议保留现有“无 caller 的跨群续聊”行为,但不要在此时附加 continuation。构造 continuation 前应证明回执 Bot 是本次投递的明确受托对象(最严格的做法是在 host-only binding 中记录并核对目标 Bot 身份);至少应要求该 sender 通过既有 evaluateBotTalk(...).allowed,而不是仅排除 blocked。不满足时按旧的无 caller 路径处理。

P1 — taskContinuation 会随 _meta 发往 HTTP MCP,而非仅限本地 stdio

src/core/plugins/mcp/gateway.ts:661 把完整 continuation 放进 botmuxTrustedCaller,但 withTrustedTurnMeta(同文件 :712-736)对所有 transport 的 client.callTool 都注入该 _meta,没有 connection.server.transport === 'stdio' 门禁。HTTP 头白名单只能约束 httpHeaders,不能约束 JSON-RPC 参数里的 _meta。

结果是 streamable-http MCP 也会收到源真人的 openId、unionId、verifiedEmail、sourceSessionId 和 sourceTurnId,与代码注释及“只暴露给本地 stdio”的边界相反;远端服务还会得到一份 host-attested 的续办身份。

探针证据:使用真实 Gateway 工具路由,先把 connection.server.transport 切换为 streamable-http,再调用同一个 echo 工具,下游仍收到:

meta={"botmuxTrustedCaller":{"requestUserOpenId":"ou_reply_bot","senderType":"bot","taskContinuation":{"openId":"ou_original_human","unionId":"on_original_human","verifiedEmail":"[email protected]","sourceSessionId":"source-session","sourceTurnId":"om_source_turn"},...}}

建议只对 stdio 连接注入 botmuxTrustedCaller(至少必须剔除 HTTP 的 taskContinuation),并增加 streamable-http 路由回归,断言 continuation 与 verifiedEmail 不出现在下游请求中。

其余重点核查

  • M6:d8f1033 对 sendToPty 的修改只是把原来的 opts.queueAfterActiveTurn || !!opts.trustedCaller?.taskContinuation 原样抽成 inputForcesSerialTurn(opts);对类型允许的输入,串行语义等价,未发现新增问题。
  • pending repo / durable follower / refork:新 tail 的 continuation 不会借到旧 head,acceptedInput 的 structuredClone 与会话整对象 JSON 持久化保留嵌套字段;未发现新的身份串写。
  • bun run build:通过。
  • 定向 7 个测试文件(task continuation、authority、follow store、worker queue/adopt、Gateway、真实 topic route):161/161 通过。这些绿测试未覆盖上述两条信任边界。
  • PR 当前 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.

复审结论(head d8f1033ed6)

30aca013a2 的三条新增测试归因准确,但生产代码复核另确认 3 个需要合入前处理的问题。前两条会造成跨任务身份/输入边界失效,第三条破坏 principal-lane FIFO。

1. [P1] 未授权外部 Bot 也能获得原任务真人的 continuation

src/im/lark/event-dispatcher.ts:6356-6362 对跨群 Bot 回执只排除 botTalk.reason === 'blocked';未授权、未知外部 Bot(包括 reason === 'none')仍可进入 follow-up 路由。随后 src/daemon.ts:30946-30964 只要求即时 sender 是 Bot,没有再次要求该 Bot 获得正面授权,follow binding 也没有记录并核对预期回执 Bot。

这不是既有的“外部 Bot 可以提示注入”本身,而是本 PR 新增的原真人身份授予:src/core/plugins/mcp/gateway.ts:661 会把该 continuation 传给本地插件,直接消费 taskContinuation 恢复读取/操作的消费者会把无关外部 Bot 当成源任务真人续办。verified-human-email protected grant 仍要求 senderType === 'user',但不能保护直接消费 continuation 的插件。

探针证据:真实 handleThreadReply 路由中,使用不在 allowedUsers 的外部 Bot,最终 sendWorkerInput 仍收到 trustedCaller.taskContinuation.openId = 源真人;现有 test/cross-chat-thread-binding.test.ts:464 同时证明此类外部 Bot 能通过 dispatcher 的 follow-up 路由。

建议:continuation 构造必须另要正面、可信的回执方授权。最小边界是未通过 botTalk.allowed 时只允许旧式路由、不得挂 continuation;更严格方案是在 follow binding 中记录预期回执 Bot 并与入站 sender 精确比对。

2. [P1] promoted durable tail 在 adopt 入口丢失强制串行边界

src/core/worker-pool.ts:18762-18798 的 promote IPC 会携带 trustedCaller.taskContinuation,却不设置 queueAfterActiveTurn。普通 worker 的 sendToPty 能从 continuation 二次推导串行,但 adopt IPC 在 src/worker.ts:42708-42727 只复制显式 flag,admitAdoptMessage(src/worker.ts:19491-19508)也只检查该 flag。

探针证据:

  • 真实 sendWorkerInput → durable tail → promoteQueuedActivationTail 后,IPC message 同时表现为“有 continuation、无 queueAfterActiveTurn”。
  • 将该形状交给真实 admitAdoptMessage,它走直接 runAdoptMessageForCapturedGeneration,而不是进入 pendingAdoptMessages。

因此前一轮 N 仍在运行时,N+1 continuation 可在 isolation-off 下被拼入 N 并保留 N 的身份,或在 isolation-on 下被拒绝,而不是等待独立串行 turn。建议在 worker IPC message 入口统一用 inputForcesSerialTurn(msg) 计算并同时供 authority 预检、adopt item 与 sendToPty 使用,或在 promoted-tail IPC 上补齐 flag。

3. [P2] continuation 绕过 principal-lane 的持久 FIFO

src/daemon.ts:32501 与 src/daemon.ts:32951 的 lane 分支仍检查 threadTrustedCaller;跨群 follow-up 刻意把它置为 undefined,真正携带 continuation 的是 turnTrustedCaller。因此 lane 有活动 turn 或持久排队项时,新 continuation 会直接走普通 sendWorkerInput/refork。随后 src/daemon.ts:32581-32587 经 setActiveInteractiveTurn 还会在 src/daemon.ts:23930-23934 改写 principalLaneRunningTurn。

真实路由探针:A 正在 lane 中运行、B 已持久排队时,C continuation 到达后,C 被直接交给 sendWorkerInput,B 仍独自留在 lane 队列,且 principalLaneRunningTurn 被改成 C。worker 的串行队列不能恢复 daemon 侧 A → B → C 的 FIFO。

建议:两条 lane 分支都使用 turnTrustedCaller 做准入;若 lane 暂不支持 continuation,应明确 fail closed,而不是绕出 lane。

对 30aca013a2 测试代修的复核

三条新增用例归因均准确:

  • test/failed-turn-rewind-daemon.test.ts:352:回退 src/core/worker-pool.ts:17870 到旧 appId + openId 内联规则,32 项中仅新用例失败,并实际把 task A 错并入 task B;把 continuation 一律判异时,:374 的同元组正向对照也咬红。
  • test/cross-chat-follow-thread-store.test.ts:224:摘除 src/services/cross-chat-follow-thread-store.ts:67 的 receipt 精确匹配,11 项中仅对应用例失败。
  • test/cross-chat-follow-thread-store.test.ts:248:摘除 :65 的既有归属保护,11 项中仅对应用例失败。
  • d8f1033ed6 的 inputForcesSerialTurn continuation 分支摘除后,test/worker-queue-merge.test.ts 30 项中仅新增 continuation 用例失败。

实际验证

  • bun run build、bun run typecheck:通过。
  • 最新 head 的 12 个变更测试文件:774 项全部通过。
  • 上述测试变异与 3 条生产问题探针均已实际执行;探针改动已全部还原,工作树干净。
  • 未做真机跨 Bot 写入验证;d8f1033ed6 的 CI 已触发,发布本结论时仍 queued。

@deepcoldy

Copy link
Copy Markdown
Owner

补充复审:同一 head 另确认 3 个 P1、2 个 P2,并扩大前序 MCP 问题的范围

以下均已用真实代码路径探针复现;临时探针已删除,工作区干净。

对前序 MCP P1 的补充:普通本地 stdio 插件也会收到 verifiedEmail

问题不限于 HTTP。buildTaskContinuationCaller 把 Contact 解析出的 verifiedEmail 放进嵌套 continuation,而 Gateway 将它作为普通 botmuxTrustedCaller 元数据发给所有下游插件;既有 verified-human-email protected grant 的 senderType / capability 门禁不会约束这个嵌套副本。因此,无 protected capability 的本地 stdio 插件也能读取或记录原真人邮箱。

修复不能只挡 HTTP:通用 caller 元数据中的 continuation 不应携带 verifiedEmail;确需邮箱的受保护消费者应通过显式 protected grant 获取。HTTP 的非 stdio _meta 隔离仍需同时保留。

P1 — continuation 构造失败后,callerless 输入可并入并继承正在运行的旧 continuation

src/daemon.ts:30969-30970 在所有跨群 follow 上清掉 threadTrustedCaller;一旦源 turn 已剪枝、源会话状态不满足或主体解析失败,turnTrustedCaller 就是 undefined。sendWorkerInput 的串行条件又只认 opts.trustedCaller?.taskContinuation,所以这类输入不带串行标记。

默认关闭 cross-principal isolation 时,worker 的 activeTurnBlocks 对这种 callerless 输入直接返回 false;若它 type-ahead 进正在执行的 continuation A,adoptActiveTurnWhenIsolationOff 会保留 A 的 caller。真实 worker 函数 + ActiveTurnAuthority 探针得到:

PR3820_CALLERLESS_ADOPT={"caller":{"senderType":"bot","taskContinuation":{"sourceTurnId":"om_source_A",...}},"turnId":"om_B","identityTurnId":"om_A","provenanceIncomplete":true}

即 B 没有通过源真人校验,却在 A 的 continuation 下运行。这不是 fail closed。建议所有跨群 follow 至少都拥有独立串行边界;凡 binding 指定了 sourceTurnId 而 continuation 构造失败时,应拒绝或强制串行,不能退回可 type-ahead 的 callerless 路径。前序“未授权 Bot 仍走无 caller 路由”的修复也必须同时满足这一点。

P1 — adopt refork 的 init prompt 绕过 continuation 串行边界

forkAdoptWorker 在 src/core/worker-pool.ts:32269-32284 只把 continuation caller 放进 init;init 没有 queueAfterActiveTurn。worker 在 src/worker.ts:42304-42318 构造的初始 pending item 因而不带显式串行标记,也不会经过 sendToPty 的 inputForcesSerialTurn 防护。

同时,init 已在 src/worker.ts:42118-42126 预留同一个新 turn,后续 activeTurnBlocks 因 turnId 相同而放行;pendingInputAllowsTypeAhead 又不检查 continuation。用真实 init 形状和真实 authority 预留复现:

PR3820_RESERVED_COLD_INIT={"reserved":true,"forcedSerial":true,"typeAhead":true,"authorityBlocks":false}

所以 adopted bridge worker 退出、外部 CLI 仍在执行 A 时,continuation B 可在支持 type-ahead 的适配器上直接写入忙中的 pane。需要让 init 产生的首项也受同一串行谓词约束,并确保“预留了 worker 逻辑 turn”不被误认为外部 pane 已到真实 prompt 边界。

P1 — 同毫秒、同 followed root 的跨会话绑定会由遍历顺序决定源真人

src/services/cross-chat-thread-binding.ts:198-200 只在 sentAtMs > best.sentAtMs 时替换 winner。两个源会话向同一 root 写入相同时间戳时,没有跨 journal 的全序;新复制的 sourceTurnId 因而取决于 active-session 遍历顺序。

真实 journal + resolver 探针仅颠倒会话数组顺序,结果就从 sess_A / om_source_A 变成 sess_B / om_source_B:

PR3820_TIE={"ab":{"sourceSessionId":"sess_A","sourceTurnId":"om_source_A",...},"ba":{"sourceSessionId":"sess_B","sourceTurnId":"om_source_B",...}}

这可能把回复归到另一个真人的源 turn。建议为跨会话绑定建立可证明的全局顺序;同优先级且无法确定唯一归属时,至少不得附 continuation(优先 fail closed),不要把任意遍历 winner 当成身份依据。

P2 — 同一 legacy receipt 的补录会把在途 follow-up 误判为改绑并静默丢弃

crossChatFollowUpIsCurrent 在 src/daemon.ts:4433 严格比较 current.sourceTurnId === expected.sourceTurnId。事件若在补录前完成路由,expected 为 undefined;并发 replay 随后给同一 provider receipt 补上 sourceTurnId,session、epoch、root、receipt 全未改变,也会被当成 stale。

真实 handleThreadReply 探针用受控下载等待制造交叠,补录成功后日志为 dropping stale follow-up,且:

PR3820_LEGACY_ENRICH_FORKS=0

建议在 match 中保留可区分的 provider receipt / 绑定记录身份,只允许同一 receipt 的 legacy undefined → attributed 补录通过;真正改绑仍应拒绝。

P2 — remote-runner 协议投影静默丢弃 continuation

src/adapters/backend/remote-runner-protocol.ts:133-144 的 projectRemoteRunnerTrustedCaller 仍只投影旧字段,导致 remote-runner provider 看不到 continuation。真实投影探针:

PR3820_REMOTE_PROJECT={"requestUserOpenId":"ou_reply_bot","requestLarkAppId":"app","senderType":"bot"}

若 remote-runner 属于本功能支持范围,应增加严格校验的 continuation 投影(不要透传 verifiedEmail);若刻意不支持,也应明确拒绝或记录该能力边界,而不是让 provider 静默退化成只有即时 Bot 身份。

回执 Bot 只有在 followedChatId 上通过 evaluateBotTalk 授权后才构造源真人 continuation;未授权或无法构造时仍保留无 caller 跨群路由。worker 在 continuation 回合活跃时也强制 callerless 输入串行,覆盖 PTY 与 adopt,避免 envelope adoption 继承旧身份。

MCP Gateway 的 botmuxTrustedCaller 元数据仅注入本地 stdio,streamable-http 不再收到 continuation、源真人身份或邮箱。补授权 chat 选择、HTTP 隔离及两种 worker 写边界回归;授权移除、sourceChatId 错用、HTTP 放行和串行调用点变异均已咬红。

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

Copy link
Copy Markdown
Owner

修复提交 2284fbd34b6bda05b5330298a14ca96dcae972be

已以 fast-forward 推送到 PR 分支(d8f1033ed6..2284fbd34b),未 force、未改写已有提交。API 已确认 PR head 指向该提交。

本次收口

  1. 回执 Bot 授权:仅在真实回执 Bot 通过 evaluateBotTalk(...).allowed 后构造 continuation,授权 chat 使用 followedChatId,不是已改写为源群的 ctx.chatId。未授权 Bot 仍按旧方式无 caller 路由,不会获得源真人身份。
  2. MCP transport 边界:botmuxTrustedCaller 只注入本地 stdio 的工具调用 _meta;streamable-http 不再收到 continuation、源真人 openId/unionId 或 verifiedEmail。既有 stdio continuation 用例仍通过。
  3. callerless 串行防线:continuation 活跃期间,callerless/其它输入在真实 sendToPty 与 admitAdoptMessage 边界都强制等待终端边界,避免默认关闭 cross-principal isolation 时通过 envelope adoption 继承旧 continuation。

验证

  • 相关 8 个 Vitest 文件:198/198 通过(含授权、Gateway、pending/durable、failed-turn、PTY/adopt 串行)。
  • bun test test/worker-queue-merge.test.ts:32/32。
  • bun test test/worker-adopt-serial-continuation.test.ts:5/5。
  • test/test-hygiene.test.ts:28/28。
  • bun run build(含完整 typecheck):通过。
  • 变异复验均精确咬红:移除 Bot 授权、把授权 chat 错改为 sourceChatId、重新放开 HTTP _meta、移除 active-continuation 串行条件、在 sendToPty 或 admitAdoptMessage 漏传 active caller。

尚未收口

前序补充评论中列出的 adopt refork init 串行绕过、同毫秒跨会话绑定归属、legacy 补录在途交叠、remote-runner 投影,以及普通 stdio 插件可见嵌套 verifiedEmail 不在本次提交中;本提交不宣称这些追加发现已经解决。新 head 的 CI 以平台最终结果为准。

@deepcoldy

Copy link
Copy Markdown
Owner

追加发现的分级与合码门槛

  • adopt refork init 串行绕过:P1,合前修。验收不能只在 init item 上补 queueAfterActiveTurn:init 已预留同一 turn,activeTurnBlocks 会因 matches 放行;还必须让 busy type-ahead 对 inputForcesSerialTurn 命中的初始项保持不可写,并覆盖 worker 重建 × 外部 pane 忙的路径。
  • 普通 stdio 插件可见 continuation.verifiedEmail:P2,合前修。当前没有生产消费者读取该嵌套邮箱,可从 continuation 构造与类型中移除;普通 stdio 插件也应收不到它。源真人的 openId/unionId/sourceSessionId/sourceTurnId 保留。
  • 同毫秒跨会话 tie:最终定为 P2 follow-up,不卡本次。触发要求同 root、同毫秒、多 active 源会话,且平局路由歧义属于既有行为;但后续修复应在无法确定唯一归属时 fail-closed、不附 continuation,不能用字典序伪装成时间全序。
  • legacy 同 receipt 补录的在途交叠:P2 follow-up;放行同一 receipt 的 undefined → attributed,真正改绑仍拒绝。
  • remote-runner 投影缺少 continuation:P3 follow-up;补严格投影(不带邮箱)或明确声明不支持。

本结论不改变当前 2284fbd34b 的 CHANGES_REQUESTED 状态;待上述前两项以 fast-forward 修复后再复审新 head。

- worker IPC 在 authority 预检前结合输入与 active caller 归一化串行标记,adopt 未打标 continuation 不再直接写入。
- source-principal 解析后再次复验回执 Bot 授权;continuation 与 callerless 跨群回执均进入 principal-lane FIFO,不伪造 caller。
- lane enqueue/attempting pre-IPC 写失败回滚内存状态并保留重试,避免重复入队或队头卡死。
- 补真实路由、IPC、adopt 与持久化故障回归及变异复验。

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

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

修复完成(054027520b)

已在此前 2284fbd34b 基础上 fast-forward 推送。最终增量处理授权、串行与 principal-lane 持久化边界,并保留 Gateway HTTP 元数据隔离。

修复内容

  • 回执授权:continuation 只按 followedChatId 的真实 evaluateBotTalk 结果签出;source-principal 异步解析完成后、实际签出前再次复验,覆盖构造期间撤授权。restricted-none 保留 callerless 旧式路由,open 模式语义不变。
  • worker 串行:IPC 在 authority 预检前结合输入自身与 active caller 归一化串行标记;adopt 和真实 sendToPty 边界同步生效。未打标的 promoted continuation 不再直接写入,callerless 输入也不能接管活跃 continuation 的身份。
  • principal-lane FIFO:continuation 与 callerless 跨群回执都进入持久队列;callerless 项不伪造 TrustedCaller,dead-worker 时仍先派发旧队头。enqueue 写失败恢复旧队列,attempting 写失败恢复 queued 并保留受控重试,不再卡死 pre-IPC 队头。
  • HTTP 元数据:streamable-http 下游不注入 botmuxTrustedCaller,本地 stdio 的 actor/continuation 传递保持不变。

探针与变异证据

  • 未授权回执、构造中撤授权:均只走 callerless;移除授权谓词后两条用例咬红,摘除签出前复验后撤授权用例咬红。
  • 无 flag continuation:真实 handleContractDaemonMessage 不再被 authority 预检拒绝,adopt 进入队列;分别摘除 IPC 归一化与 continuation 判定后对应测试咬红。
  • live/dead lane:continuation 与 callerless 均保持 B→C,不伪造 caller;恢复旧 caller 门后四条 FIFO 用例咬红。
  • enqueue/attempting 故障:分别恢复旧队列/queued,后续可重新落盘或派发原队头;移除回滚后对应测试咬红。
  • HTTP:恢复无条件注入后,streamable-http 用例咬红并实际显示完整 continuation 泄漏。

验证

  • bun run build、bun run typecheck:通过。
  • 相关 25 个测试文件:1096 项全部通过,覆盖 continuation、authority、完整 principal-lane、session-store/resume 回归族。
  • test/test-hygiene.test.ts:28/28 通过。
  • 对抗探针覆盖人工撤授权、持久化半完成、旧 turn terminal/后续 drive、live/dead worker 及 active continuation 交叠,最终未发现新的阻断问题。
  • 未做真机跨 Bot 写入验证;新 head 的 CI 已触发,结果以平台为准。

王波 and others added 2 commits November 25, 2026 15:39
Co-Authored-By: Aiden
- adopt 初始输入与 busy type-ahead 统一遵守续办串行约束。
- 从通用 continuation 移除邮箱,并过滤旧持久数据中的私有字段。
- 同毫秒跨会话归属歧义拒绝路由,同回执补录不再丢弃在途回复。
- remote-runner 严格投影续办上下文,预派发校验失败保留后续重试能力。
- 补充真实 worker、daemon、MCP 与 provider 调用链回归。

Co-Authored-By: Aiden
@will7856

will7856 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

已补齐前序评论中尚未解决的两项合前问题和三项 follow-up,提交 c4adc49518(基于 e7b5740e9 追加,未改写已有提交)。

  1. adopt refork init 串行边界:初始队列项补齐串行标记;busy type-ahead 同时检查 inputForcesSerialTurn,不再把同 turn 的 authority 预留当作外部 pane 已空闲。真实 flushPending 回归覆盖 Codex/Claude 两种路径,验证 busy 时零出队、真实 ready 后原项可继续。
  2. 普通 stdio 邮箱泄漏:从 continuation 构造和类型中移除 verifiedEmail;Gateway 逐字段投影,旧持久 caller 中残留的邮箱及额外字段也不会传给普通插件。原有 HTTP 隔离保留。
  3. 同毫秒跨会话归属歧义:最高优先级存在平局时拒绝跨群绑定,不靠遍历顺序或字典序选择源真人;出现更新且唯一的回执后可正常路由。
  4. legacy 同回执补录:绑定携带精确 provider receipt。只允许同一 receipt 的 undefined → attributed 补录通过在途复验,不把这次 callerless 输入升级为真人;真正改绑、生命周期变化仍拒绝。回归通过真实 handleThreadReply 的附件等待制造并发交叠。
  5. remote-runner 投影:增加严格校验的 continuation 出站投影,不携带邮箱;provider 回报不能生成可信 continuation。无效上下文在派发前返回 untouched,不会误标为未知写入或使后续有效 turn 无法执行;实跑 provider 回归覆盖这条恢复路径。

验证:

  • bun run build(含 typecheck):通过。
  • TMPDIR=/tmp bun scripts/fenced-run.ts -- bun run test 定向运行 16 个相关文件:364/364 通过,覆盖 worker、daemon、Gateway、provider、principal-lane 和测试卫生。
  • 原生 Bun 逐文件运行 task-continuation-caller、worker-idle-queue-stall、remote-runner-protocol:70/70 通过。
  • 新回归已在旧实现上观察到失败,再验证修复通过;本地 Bun 为 1.4.2,CI 钉定 1.4.4,新 head 的完整平台结果仍以 CI 为准。

未重启或部署本地 BotMux,未合入或发布。@deepcoldy 请基于新 head 复审,正式审批状态仍需 reviewer 更新。

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

增量复核结论(c4adc49518):通过,无新增阻断

逐项复核五类修复的实际调用链,未发现新的生产代码问题。

  • adopt init / busy type-ahead:src/utils/pending-input-queue.ts:193-206 会拒绝 continuation 输入在忙碌外部 pane 上 type-ahead;src/worker.ts:42951 的 init 内联标记虽被单独移除后 8 个文件 189 项仍全绿,但现有写入均受 sendToPty/type-ahead 串行规则约束,确认属于不可独立产生错误行为的冗余第二道防线,不列为阻断。
  • 邮箱与私有字段清理:src/core/plugins/mcp/gateway.ts:658-670 只投影 5 个公共 continuation 字段,旧持久数据中的 verifiedEmail/未知私有字段被过滤;全仓检索确认 continuation 已无 verifiedEmail 生产消费,保留的邮箱字段仅用于顶层 protected grant。
  • 同毫秒归属:src/services/cross-chat-thread-binding.ts:159-213 对跨会话或跨 root 的等时间戳返回 null,遍历顺序不影响结果;更晚的唯一归属仍可解析。
  • legacy 在途补录:src/daemon.ts:4430-4441 只允许同一 provider receipt 从 legacy enrich 到带 turn 的记录,且该在途输入保持 callerless;换 receipt/实际改绑仍 fail closed。
  • remote-runner 投影:src/adapters/backend/remote-runner-backend.ts:276-289 在设置 active turn 前完成严格投影,非法上下文返回 untouched,合法后继可继续使用同一 provider;协议层拒绝跨 app、非法 ID、synthetic turn 与非 Bot actor,私有字段不下发。

实际验证

  • 本 commit 8 个测试文件:189/189。
  • PR 自有 15 个测试文件:858/858。
  • bun run typecheck、bun run build:通过。
  • test/test-hygiene.test.ts:28/28。
  • 同毫秒歧义门、legacy enrich 门、Gateway 公共字段投影、remote-runner 严格校验、type-ahead 串行门分别移除后,对应测试均咬红;init 内联标记移除后 189 项全绿。所有变异现场均已还原,工作树干净。

代码评审层面可合;45606149844 首跑目前尚未全部完成,仍须等待该 run 的 required jobs 全绿后再执行合码门禁。

@deepcoldy
deepcoldy merged commit 749fb2d into deepcoldy:master Nov 25, 2026
15 checks passed
deepcoldy pushed a commit that referenced this pull request Nov 25, 2026
- 完整保留 #3810(防止空闲误判提前截断 IM 回合、用户中断识别、Scenario 3 晚到最终答复恢复与 Loop 2 回滚)与 #3820(跨群 Bot 回执原任务续办身份绑定)的全部语义。
- 在 claude-transcript / restart-interruption 中统一复用 hasTranscriptRole 拒绝 api-request-blob 等非对话记录,同时保留 isClaudeUserInterruptEvent / isSyntheticUserPrefixEvent / assistantToolUseIds / userToolResultIds 的全部行为。
- 调和 #3810 与 #3823 跨特性边界:Scenario 3A late-recovery-* 的 CoT 归属、hasActiveNonLocalTurn 判定及续发 Monitor/CronCreate 锚点均绑定回原回合 replyAnchorTurnId/replyAnchorDispatchAttempt;deferredTerminalTurnStarts.delete 移至 Loop 2 刷出后执行以兼容 Loop 2 异常回滚,并补齐交叉回归用例。

Co-Authored-By: Claude Code <[email protected]>
deepcoldy pushed a commit that referenced this pull request Nov 25, 2026
merge #3810/#3820 后 CI bun-test shard 1/4、4/4 各暴露一处本 PR 生产改动
引发的 Bun 运行时失败(vitest 腿均绿,生产代码不变):

- worker-function-harness:本 PR 在 bridgeApplyFingerprintSwitch 等 6 个 worker
  函数新增 observeClaudeBackgroundTasks() 调用,经 loadWorkerFunction 抽取且
  scope 未注入时 fallback 到 undefined,JavaScriptCore 报 'is not a function',
  worker-bridge-foreign-session 的 fingerprint switch 用例失败。在默认 state 给
  该薄封装一个 inert no-op(scope 仍可覆盖),与 INERT_BACKGROUND_TASK_TRACKER
  同思路,避免加载相关 worker 函数的测试逐个补桩。
- worker-bridge-duration-marker:本 PR 把 bridgeIngest 的 bridgeQueue.ingest 从
  3 参扩成 5 参(追加 pending-wake observer),钉 ingest→updateProvisionalExitWatch
  邻接的源码正则因写死旧签名失配。改为 [^;]* 容忍参数增减,仍锚定前三个关键
  参数与紧邻 arm 的结构护栏语义。

验证:bun 相关文件全绿(foreign-session 14、duration-marker 33、含 adopt-target
等 6 harness 文件 140),vitest 4 文件 104 绿;两处分别摘 no-op 默认、还原精确
正则均精准咬红后恢复;tsc 0、build 通过。

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

Copy link
Copy Markdown

🚀 Released in v3.59.33

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