Skip to content

fix(ask): 按原始话题隔离文字答复 - #3614

Closed
wzono wants to merge 1 commit into
deepcoldy:masterfrom
wzono:pr/ask-scope-isolation
Closed

wzono wants to merge 1 commit into
deepcoldy:masterfrom
wzono:pr/ask-scope-isolation

Conversation

@wzono

@wzono wzono commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

群外层 botmux ask 等待时,同 Bot 同群其它话题的文字会被当成该 Ask 的自定义答案,原会话收不到这条指令。旧匹配把 rootMessageId=null 当成了跨话题通配符;chat/shared 模式还会将原始话题归入 chat anchor,所以只比较会话 anchor 仍会串答案。

修复后,群外层 Ask 只接受群外层文字;发在话题 A 的 Ask 只接受 A 的文字。原始消息位置在路由折叠前保留,内嵌回复卡 Ask 同样保留发起 turn 的冻结话题,持久化与恢复保留该位置。私聊继续沿用连续会话的答复语义。

影响面为 Ask 文字候选查询、提问入口和重启恢复投影;不增加配置,不改变调用身份、按钮答复权限或卡片交互协议。

复现与探针证据

问题 触发及实际行为 修法与回归
P1:群级 Ask 跨话题捕获正文 群 Ask 先登记后发送话题指令,Ask 错误结算,worker 未收到正文;新增 broker 矩阵与真实 daemon 路由用例对旧实现咬红 群外层和话题候选分开,explicit/hook、同时存在多个 Ask 均覆盖
P1:chat/shared 路由折叠绕过隔离 原始事件带 root_id 与 thread_id,真实 dispatcher 得到 scope=chat、anchor=chatId;原始事件探针 4 个场景错误结算,worker 输入为空 按原始 messageRootId 与冻结提问话题匹配,真实 dispatcher→daemon 的 11 格 live/restore 探针全部通过
同项恢复与内嵌回归 cardRoot 丢失时重启恢复和内嵌 Ask 新用例失败 增加可选持久化字段与恢复投影,内嵌 Ask 也保留冻结话题;用例先红后绿

另用真实 broker 与可控时钟验证人工取消/换绑、部分持久化失败、超时与重试交叠,7/7 通过。摘掉新增守卫后,4 个新增用例失败。

验收覆盖

验收项 受影响行为 必要调用节点 验证证据 结果
群外层和话题隔离 群外层 Ask 等待时话题正文到 CLI;同时有群/话题 Ask 仅结算本话题 handleThreadReply → list/findPendingAsksByAnchor → submitCustomReply/普通路由 broker 候选矩阵、explicit/hook 真实路由先红后绿 通过
chat/shared 隔离 共用 chat anchor 时仍保留原始话题边界 dispatcher → parseMessage → daemon messageRootId → broker 11 格真实原始事件/live/restore 探针,原失败场景转绿 P1 closed
内嵌提问的冻结话题 发起 A 后最新目标变 B,B 输入继续进 CLI,A 才结算 handleAskRequest → frozenReplyContextForTurn → explicit continuation → broker → dispatcher/daemon 真实生产提问入口与原始事件独立探针 通过
重启恢复隔离 cardRoot 随恢复及重连保留 persistFromInternal → PersistedAsk → restorePersistedAsks → registerAsk reconnect 真实磁盘恢复正反向矩阵、先红后绿回归 通过
既有输入与生命周期兼容 p2p 旧回复、顶层引用、异步 exact parent;取消、部分失败、超时重试 dispatcher、daemon、broker、card callback、persist store 3/3 必要探针、7/7 真实组件时序探针、271/271 必要回归 通过
构建与共享 CLI hook/explicit/native Ask 兼容 Claude/OpenCode/Dsh hook、turn reply、daemon route 15 文件 507/507、完整 build、diff-check 通过
线上生效 正式服务加载新版本后的效果 发布门禁 → activate → 线上验收 本次未执行部署 未验证

验证

从 master ec8cf07767fac4c5ad08f887bb7d8660cb916811 整理单独提交后执行:

bun run build
bun run test test/ask-broker.test.ts test/ask-dormant-hook-text-routing.test.ts test/ask-resume-restart.test.ts test/ask-waiter-lifecycle.test.ts test/ask-custom-reply-candidate.test.ts test/ask-custom-input.test.ts test/ask-chat-scope-turn-target.test.ts test/turn-reply-ask.test.ts test/ask-explicit-fsync-failure.test.ts test/explicit-ask-continuation.test.ts test/ask-hook-claude.test.ts test/ask-hook-opencode.test.ts test/ask-hook-dsh.test.ts test/ask-custom-adversarial.test.ts test/daemon-rename-route.test.ts
git diff HEAD^ HEAD --check

完整构建通过,15 文件 507/507 通过,无 unhandled errors,diff 检查通过。尚未部署,线上效果未验证。

@wzono
wzono marked this pull request as ready for review November 19, 2026 11:09
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T11:20:10.430558Z d580e1a Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d580e1a48c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/daemon.ts
Comment on lines +11327 to 11328
const cardRoot = asyncInput || askTurnTarget?.mode !== 'thread'
? undefined : { cardRootMessageId: askTurnTarget.rootMessageId };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor the frozen topic when an inline Ask creates its card

With shared/chat routing, unified reply cards and native CoT enabled, the first Ask can arrive before the answer card has a messageId, because native CoT suppresses the start card. publishReplyCardAsk then creates it using ask.rootMessageId (src/core/turn-reply-ask.ts:74-78), posting it to the outer chat, but the newly retained cardRootMessageId restricts text answers to the original topic. A production-store/publisher probe confirms zero candidates for text at the delivered card's location, so those replies no longer settle the Ask. Make initial inline delivery honor the frozen root, or use standalone delivery when no card exists.

AGENTS.md reference: AGENTS.md:L152-L152

Useful? React with 👍 / 👎.

Comment thread src/core/ask-broker.ts
larkAppId: p.larkAppId,
chatId: p.chatId,
rootMessageId: p.rootMessageId,
cardRootMessageId: p.cardRootMessageId,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Backfill topic roots when restoring pre-change Ask records

When upgrading with a chat-scoped Ask already pending in a topic, its existing v2 record lacks cardRootMessageId, because the previous writer omitted that field. Restore leaves it undefined, and registerAsk/reattachByRequest do not apply the reconnecting input's root, so the new matcher treats this topic Ask as outer-chat-only. A real-store restore/reconnect probe with a controlled clock produces zero original-topic candidates and one outer-chat candidate even when reconnect supplies the correct root. Legitimate topic answers now miss the Ask, while unrelated outer-chat text can settle it. Backfill from authoritative origin context or explicitly handle legacy unknown-location records before applying the new matching semantics.

AGENTS.md reference: AGENTS.md:L172-L172

Useful? React with 👍 / 👎.

@deepcoldy

Copy link
Copy Markdown
Owner

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

整体修复方向正确:messageRootId 三态(undefined 走旧 anchor 语义 / null 群外层 / om_ 话题根)+ cardRootMessageId 持久化的设计闭环完整,p2p 连续会话语义保留,本地 15 个相关测试文件 574 用例全绿、typecheck 与完整 build 通过;对三处关键守卫做变异(回退旧通配匹配 / 去掉 daemon 侧 messageRootId / 去掉持久化字段)分别咬红 8 / 2 / 1 个用例,测试能钉住行为。

但独立核实时发现以下 2 个问题,建议修改后再合入:

1.(建议按 P2 处理)内嵌 Ask 首次发卡落点与文字捕获区背离

src/core/turn-reply-ask.ts:74-78 的传输层用 ask.rootMessageId 决定首发位置;chat-scope 会话该值经 bindSessionScopedIpcIdentity 归一化为 null,于是当统一回复卡记录还没有 messageId(native CoT 抑制了起始卡,或占位卡被延迟/发送失败)时,内嵌 Ask 触发的首次建卡走 sendMessage(chatId) 发到群外层;但本 PR 给同一 Ask 设了 cardRootMessageId=<冻结话题 om_root>,文字答复只在该话题内命中。用户在卡片所在的群外层回复文字,listPendingAsksByAnchor({ messageRootId: null }) 候选为 0,文字不会结算 Ask 而被当成新指令送进 CLI。

用真实 TurnReplyCardStore + 真实 publishReplyCardAsk + stub 飞书客户端探针复现:记录无 messageId、chat-scope(rootMessageId=null, cardRootMessageId=om_root, replyCardTarget 存在)时,首发调用的是 sendMessage(om_top_level)而非 replyMessage(om_root);此时群外层文字候选 0、话题内候选 1。按钮点击不受影响(不走 anchor 匹配)。普通流程(起始卡已在话题内发出)只走 PATCH 不受影响;非内嵌 Ask 走 ask-card.ts:103 的 cardRootMessageId ?? rootMessageId 也没有这个问题。

建议修法:让 publishReplyCardAsk 的 send 与 sendOverflow 与 ask-card.ts:103 对齐,用 ask.cardRootMessageId ?? ask.rootMessageId 作为 reply root(om_ 前缀才 reply,否则 send 到 chatId)。thread-scope 下 cardRoot 恒为 undefined、行为不变;chat-scope 纯顶层 turn 两者皆空、仍走 sendMessage,行为也不变。

2.(建议按 P3 处理)升级瞬间已在途的旧持久化记录缺少 cardRootMessageId

旧版本 cardRootMessageId 只在内存持有、不落盘(已核对合入前代码)。升级重启后,旧 v2 记录恢复出的 chat-scope 话题 Ask 该字段为 undefined,而 reattachByRequest(src/core/ask-broker.ts:571)只复用恢复对象、不合并重连输入,新匹配把它永久当作 askRoot=null:话题内文字命不中、群外层文字反而可能结算。影响仅限升级时已在途的 resumable Ask(hook / host_explicit),升级后新建的 Ask 不受影响、可自愈;恢复后到 hook 重连前 Ask 处于 dormant,本来就不在候选里,所以实际窗口是重连之后的剩余存活期。

需要注意:PersistedAsk 没有持久化 originTurnId,「从权威 turn 上下文回填」拿不到键,不可行。建议改为旧形状回退:恢复时对缺少 cardRootMessageId 的群聊记录打未知位置标记,匹配沿用旧的通配谓词(等价升级前行为),新记录一律走新语义;该兼容分支后续可随版本清理。

本地验证记录

  • bun run typecheck、bun run build 通过
  • 定向 15 个测试文件 574/574 通过(含 ask-broker、ask-dormant-hook-text-routing、ask-resume-restart、ask-chat-scope-turn-target、daemon-rename-route、三组 hook、fsync 等)
  • 3 组变异复验全部先红后绿;问题 1 经真实组件探针复现后已删除探针

@deepcoldy

Copy link
Copy Markdown
Owner

复审确认(head d580e1a48c)

结论:P2 / P3 均成立,仍建议修改后再合入。 P2 的建议补丁可直接采用;P3 的兼容方向可行,但实现需要补充两个边界。

1. P2 确认:内嵌 Ask 首发落点与文字捕获区不一致

src/core/turn-reply-ask.ts:74-90 的首发与 overflow 仍只看 rootMessageId,没有使用本 PR 给内嵌 Ask 补上的 cardRootMessageId。

用真实 TurnReplyCardStore、真实 publishReplyCardAsk 和 broker 复现:统一回复卡已 prepare、尚无 messageId,Ask 为 rootMessageId=null、cardRootMessageId=om_ask_topic 时,首发调用 sendMessage(oc_chat),不是 replyMessage(om_ask_topic);同时群外层文字候选为 0,原话题候选为 1。即使引用群外层这张卡,候选集合也已先被位置过滤,不能结算 Ask。

临时按首审建议把 send / sendOverflow 改为以 cardRootMessageId ?? rootMessageId 选择 reply root 后,同一探针确认首发进入 om_ask_topic,不再发群外层,文字匹配保持正确。建议同时补一条“真实 dispatcher + 回复卡尚无 messageId”的回归测试;当前新增测试的自定义 dispatcher 没覆盖这条路径。

2. P3 确认:旧持久化记录升级后被当成群外层 Ask

旧记录缺少 cardRootMessageId 时,src/core/ask-broker.ts:940 回退到 rootMessageId=null;reattachByRequest 也不合并重连输入里的新字段。真实 store/broker 探针复现了 restore → reattach 后:原话题候选为 0,群外层候选为原 Ask,群外层文字可被错误结算。

需要修正首审中“dormant 期不参与候选”的概括:它只适用于 hook。host_explicit 被 isUnclaimedHookAsk 刻意排除,daemon 本身就是 claimant。另一个真实探针确认,旧 host_explicit 在 restore 后、不做任何 reattach,群外层文字就已被接受并 stash。因此只在 hook 重连时补字段不足以闭合问题;P3 等级仍可维持,但 host_explicit 的误答窗口从恢复后即开始。

3. P3 修法必须区分“旧记录缺字段”和“新版群外层记录”

本 PR 新建的群外层 Ask(rootMessageId=null、无 cardRootMessageId)落盘时同样没有该属性。若恢复时把所有缺字段记录都视为旧记录并回退旧通配谓词,新版群外层 Ask 经下一次重启又会跨话题抢文字。

建议增加明确的持久化 schema/路由版本或“位置已知”标记:

  • 旧版本缺字段的记录才走兼容策略;首审建议的旧通配回退可用于保持升级前行为。若选择更保守的隔离策略,也可禁用这些未知位置旧记录的自由文本捕获、保留按钮,但应明确这是行为变化。
  • 新版群外层记录必须标记为“已知位置=null”,继续精确匹配,不能与旧未知位置记录共用缺字段形状。
  • 旧记录再次落盘(尤其 host_explicit answer stash)及二次重启后,兼容标记不能丢失。

至少补四组升级回归:旧 hook restore→reattach、旧 host_explicit restore 后未 reattach、新版群外层记录再次重启、旧记录再次落盘后的二次重启。

本地验证

  • 相关既有 5 个测试文件:144/144 通过。
  • P2、P3 hook、P3 host_explicit 均由独立真实组件探针复现;P2 临时补丁验证有效。
  • 探针与临时补丁均已删除/还原,工作树干净;PR 当前 head CI 全绿。

@deepcoldy

Copy link
Copy Markdown
Owner

补充:问题 2(升级兼容)的两个实现边界

进一步用真实 store/broker 探针核实后,原评论中问题 2 的修法需要收紧,补充如下:

边界一:host_explicit 的误答窗口从恢复后立即开始,不等到 reattach。
isUnclaimedHookAsk(src/core/ask-broker.ts:949)的 dormant 过滤只对 RESUMABLE_ORIGINS = {'hook'} 生效,host_explicit 被刻意排除(daemon 自身就是 claimant)。探针证实:旧形状的 host_explicit 记录经 restorePersistedAsks 恢复后、不做任何 reattach,已出现在 listPendingAsksByAnchor({ messageRootId: null }) 候选中,而 hook 记录同状态下被正确过滤。因此只在 hook 重连路径补字段不能闭合问题。

边界二:不能用「字段缺失」判定旧记录——新版群外层 Ask 落盘同样没有该属性。
persistFromInternal 写出的 cardRootMessageId: undefined 会被 JSON.stringify 丢弃,探针确认新版群外层 Ask(rootMessageId=null)的磁盘 JSON 不含该键。若恢复时把所有缺字段记录回退旧通配谓词,新版群外层 Ask 经历一次重启后又会跨话题抢文字,本 PR 的修复在重启后失效。

建议的落地形态:

  1. 引入显式的持久化 schema/路由版本(如 v: 3)或「位置已知」标记:新写入的记录无论群外层还是话题内都带标记(群外层显式记 null、话题内记 om_ 根),只有不带标记的旧 v2 记录走兼容策略;
  2. 兼容策略二选一:旧未知位置记录回退旧通配谓词(保持升级前行为),或更保守地禁用其自由文本捕获、仅保留按钮答复(需在 PR 描述注明这是行为变化);
  3. 标记必须在再次落盘时保留——特别是 host_explicit 的 answer stash 写入与二次重启恢复,不能在中途丢掉形状信息;
  4. 建议至少覆盖四组升级回归:旧 hook restore→reattach、旧 host_explicit restore 后未 reattach、新版群外层记录二次重启、旧记录再次落盘后的二次重启。

问题 1(P2)的建议修法不变:turn-reply-ask.ts 的 send/sendOverflow 以 ask.cardRootMessageId ?? ask.rootMessageId 选择 reply root,并补一条「真实 dispatcher + 回复卡尚无 messageId」的回归测试。以上仍为自动评审初步意见,最终以维护者审阅为准。

@deepcoldy

Copy link
Copy Markdown
Owner

感谢 @wzono 对这个问题的根因定位与核心方案——本 PR 的全部有效改动已由 #3860 在最新 master 上接手落地(合并提交 0854e93),并在其基础上补齐了内嵌回复卡首发落点一致性、旧版持久化记录升级兼容,以及交叉身份选择卡与旧卡线程引用的边界处理。你的署名已作为 Co-authored-by 保留在落地提交中。

由于 #3860 已完整覆盖本 PR 的范围并合入,这里先关闭本 PR,避免重复评审。如有任何未覆盖的场景或后续想法,欢迎随时 reopen 或在新 PR 中继续。再次感谢!

@deepcoldy deepcoldy closed this Nov 26, 2026
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