Skip to content

feat(codex): 飞书新话题自动创建共享会话 - #3844

Merged
deepcoldy merged 3 commits into
deepcoldy:masterfrom
Disaster-Terminator:feat/codex-shared-topic-create
Nov 25, 2026
Merged

deepcoldy merged 3 commits into
deepcoldy:masterfrom
Disaster-Terminator:feat/codex-shared-topic-create

Conversation

@Disaster-Terminator

@Disaster-Terminator Disaster-Terminator commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

改动

目前,BotMux 接入共享 Codex App Server 后,新话题仍需手动 /adopt。本改动增加自动创建:开启 existingAppServer.newTopics: "create" 后,飞书新话题直接在指定的 App Server 上创建 thread,供 BotMux、Codex Desktop 和 CLI 共用同一会话。

沿用 #974 的共享接入和现有消息处理链路,同时补齐创建失败和重启后的恢复,保留首条消息,避免重复创建和后续消息卡住。默认仍只接入已有 thread,其他入口保持原行为。

验证

完整构建通过;相关回归 2,633 个用例通过,2 个既有用例跳过;Bun 核心用例 27 个通过。真实 Codex App Server 和 worker 完成一轮模型测试服务推理,第二客户端读取同一 thread,2 个集成用例通过。移除回执防重、目录或关闭检查后,对应回归均失败。

飞书与 Desktop UI 的线上验收尚未执行。创建回执丢失时需核对实际 thread;初始化期间同时人工改名,初始名称可能覆盖人工名称。

复审修复与补充验证

  • 自动创建/绑定前把显式 serviceTier 归一为 inherit,避免 remote TUI worker 的附件守卫拒绝启动;覆盖真实 session store、重试和已绑定幂等再入。
  • 绑定后的最终 admission 拒绝只释放 runtime route claim,保留 durable activation head;后续 IM 按 FIFO 重放旧 head。异步 deferred 仍保留启动责任,不误清 gate,并保持 marginalReclaimScheduled out-flag 的调用方契约。同步 false、同步拒绝、异步拒绝与启动异常分别覆盖;启动异常补偿按精确 claim token 对齐,不把正常的 workerGeneration 推进误判为 stale。
  • /close 后同 sessionId reactivate 与旧 opener 交叠时,继续保持 sessionId 级创建 single-flight;旧尝试结束后向 replacement runtime 交接 durable head。交接受 runtime、generation、route、activation token、background epoch 和 claim token 防护;creating 未知态仍不发起第二次 thread/start,保持 /adopt 对账路径。
  • SharedThreadClient 的请求、initialized 通知和 bootstrap 错误响应补齐 jsonrpc:"2.0"。

实际执行:

  • bun run build:通过(含 tsc/typecheck、dashboard bundle 与 dist audit)。
  • 定向 Vitest:shared-thread-opening、session-store、session-lifecycle-start、existing-app-server、daemon-rename-route、worker-durable-expiry-order,6 文件、1016 用例全绿。其中包含路由级“gate 释放后下一条 IM 重放旧 head、 successor 留在 tail”的回归。
  • BOTMUX_TEST_REAL_CODEX_BIN=/root/.local/bin/codex bunx vitest run --project e2e test/shared-thread-real-server.e2e.ts:Codex 0.167.0,2/2 全绿。
  • Bun 引擎分别运行 shared-thread-opening 与 existing-app-server:29/29 全绿。
  • 变异复验:分别移除最终拒绝清理、replacement replay、activation token 防护、claim token 防护、out-flag 回填、同步 false 清理、同步/异步拒绝清理与 catch 清理,错误地要求 generation 不变、在 deferred 时释放 claim,或省略请求 jsonrpc 字段,对应测试均咬红;每次变异后均恢复实现并重新跑绿。

对抗复核问题清单与探针证据

  • P1(已修复):绑定成功后的同步 worker 启动异常会先把 workerGeneration 从 undefined 推进到 1;初版补偿错误地要求 generation 不变,导致 catch 无法释放 claim,后续 IM 只入队不重试。真实 forkWorker 探针复现了 worker=null、durable input/activation 保留、但 initialStartPending=true 的状态。修复改为按精确 claim token、activation token、route、background epoch 和 active owner fencing;原失败探针及正式 spawn-throw 回归均已通过。
  • 测试覆盖缺口(已补齐):同步 false 与 catch 清理曾各自缺少能独立咬红的用例。现用真实 tail-only quarantine 覆盖同步 false,用带 claim token 和 generation reservation 的 spawn-throw 覆盖 catch,并另测同步/异步拒绝;等待超时设为 5 秒以避免整机负载下的假失败。
  • 其余攻击场景未发现新增问题:close/reactivate/再次 close、route 改变、第三 runtime 接管、activation/claim 代际变化、SQLite 半绑定、creating 回执未知、deferred→accepted/rejected,以及 gate 释放后的真实后续 IM FIFO。

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 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-25T15:04:43.812893Z 81050a4 PR opened
ℹ️ 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: 81050a475c

ℹ️ 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 on lines +122 to +123
session.cliSessionId = threadId;
session.existingAppServerEndpoint = opening.endpoint;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Reset inherited service tiers before launching the shared worker

For a valid bot configured with serviceTier: "priority", "default", or "ultrafast", buildNewSession() retains that tier, but this binding never resets it as the existing /adopt path does. sessionServiceTier() therefore includes it in the worker init, and src/worker.ts:31187-31188 explicitly rejects shared-server attachments with any defined service tier. Executing the production builder, binding, tier resolver, and worker guard confirmed that the remote thread is created but its first task cannot start. Reset the session preference to inherit when binding the externally managed thread.

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

Useful? React with 👍 / 👎.

Comment thread src/core/worker-pool.ts Outdated
sessionStore.updateSession(ds.session);
}
reportAdmission('deferred');
if (pendingSharedThreadOpenings.has(ds.session.sessionId)) return true;

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 Hand off pending openers across session reactivation

If a session is closed and reactivated while its original opener awaits thread/read, a new input's fork is absorbed by this session-ID-only set even though it belongs to a new runtime and activation token. When the original opener completes, the runtime-identity guard discards its continuation, and finally removes the set entry without waking the replacement. A SQLite/fake-clock probe using the production opener, fork function, and reactivation writer leaves the replacement with worker=null, initialStartPending=true, and its new input queued; subsequent messages only keep buffering. Coalesced replacement attempts need an explicit handoff after the stale opener finishes.

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

Useful? React with 👍 / 👎.

Comment thread src/core/worker-pool.ts Outdated
Comment on lines +20949 to +20953
forkWorker(ds, input, { resume: true, turnId: ds.session.queuedActivationTurnId,
dispatchAttempt: ds.session.queuedActivationDispatchAttempt, codexAppInputGateFrozen: true,
...(initDurablePrimaryInbox ? { durablePrimaryInbox: true as const } : {}),
...(initAtMostOnce ? { atMostOnce: true as const } : {}),
}, opts);

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 Release the opening gate when the deferred fork is rejected

If memory becomes hard-blocked while the bootstrap awaits the server, this second forkWorker() call reports rejected and returns true without spawning a worker. That nonthrowing rejection bypasses the cleanup in .catch(), so the pending-opener entry disappears while initialStartPending remains true. A SQLite/fake-clock probe of the production opener and fork function reproduces this workerless, gated state; even after memory recovers, later topic messages buffer behind an opening that nobody will retry. Handle the recursive fork's admission outcome and release the runtime claim on rejection while retaining the durable input journal.

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

Useful? React with 👍 / 👎.

newTopics:"create" 自动创建的会话在 buildNewSession 阶段保留 bot 配置的
显式 serviceTier,而远端 TUI worker 在挂载既有 App Server 时拒绝任何已定义
的 serviceTier(worker 附件守卫),导致 thread 已创建但首个任务无法启动。
/openSharedThread 的首个检查点事务内把 serviceTier 归一为 inherit,与
/adopt 绑定路径的既有处理一致,并覆盖所有重试与已绑定幂等再入路径。

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

Copy link
Copy Markdown
Owner

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

整体实现质量较好:创建三态检查点(unrequested / creating / received id)与绑定字段同事务落盘,回执丢失时 fail-closed 不重复创建,持久层对迟到元数据写入做了 opening/identity 防回退,测试覆盖了重启恢复、路由变更、关闭竞争等关键交叠,且真机 e2e 为显式 opt-in,不会进入默认 CI。

已代修(commit 9b4a866,追加在作者提交之后,未改写原提交)

P1:显式 serviceTier 导致远端 worker 挂载必败

  • buildNewSession 会给普通 Codex 话题保留 bot 配置的显式 serviceTier(priority / default / ultrafast),而 worker.ts 的既有 App Server 附件守卫对任何 cfg.serviceTier !== undefined 直接抛错;配置校验只禁止了 codexRpcInput 等组合,并不禁止 serviceTier,/adopt 路径本来就在绑定时把它重置为 inherit。
  • 本 PR 的自动创建绑定缺少同一重置:远端 thread 能创建成功,但首个任务的 worker 初始化确定性失败,话题无法开工。
  • 修复:在 openSharedThread 首个检查点事务内归一 session.serviceTier = 'inherit',覆盖全部重试与已绑定幂等再入;补了两个回归用例(纯函数级 + 走真实 createSession/store 的端到端链路),摘除修复后双引擎均咬红。

建议后续处理(未阻断,未在本提交修改)

  1. 打开期间会话被关闭再恢复(同 sessionId reactivation):进行中的 opener 会因代际守卫丢弃续跑,而按 sessionId 去重的 in-flight 集合会吞掉新运行时的首次 fork,且旧 opener 结束时没有唤醒动作,需要再发一条消息驱动;若当时正处于 creating 未知态,则按设计只能 /adopt 人工对账。窗口很窄、fail-closed、无输入丢失,建议后续补显式交接。
  2. 绑定成功后的递归 forkWorker 若被 admission 非抛错拒绝:.catch 不会执行,运行时 claim 保持占用、无自动重试;下一条入站消息会经持久化 journal 重新驱动,阻塞卡片也会正常提示,无输入丢失,建议按拒绝结果释放 claim。
  3. 协议卫生(P3):SharedThreadClient 的出站消息(request、initialized、-32601 响应)都没带 jsonrpc: "2.0",与仓库内既有 app-server 客户端不一致;当前真机版本可容忍,建议补齐以防服务端收紧校验。

本地验证

  • bun run build(tsc)通过
  • vitest 定向 8 个相关文件 1020 用例全绿;bun 引擎可运行的 2 个文件 28 用例全绿
  • 5 组变异(含本次修复守卫)在 vitest/bun 双引擎按预期咬红(session-store 类文件 bun 侧受 vi.hoisted shim 限制本就不支持,基线同样报错)

@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 9b4a866)

P1 的 serviceTier 修复位置与持久化语义正确;但原 P3-1、P3-2 的“再发一条消息即可驱动”不成立。两条路径都会留下 initialStartPending=true,后续消息只追加 durable tail,不再 refork。建议升级为 P2 并在本 PR 内修复。原 P3-3 维持 P3,不单独阻断合并。

P2:绑定后的 admission 拒绝会持续保留 opening runtime gate,后续消息无法自恢复

位置:src/core/worker-pool.ts:20949-20957,后续入队分支见 src/daemon.ts:32227-32310。

绑定成功后递归调用的 forkWorker(...) 返回值/最终 admission 没有被检查。内存硬拒绝走 reportAdmission('rejected'); return true,不会抛错,因此外层 .catch 不会执行 initialStartPending=false / initialStartClaimToken=undefined 的清理。此时 durable head 仍在、worker=null,但 initialStartPending=true;下一条普通消息在 daemon.ts:32230 进入 follower 分支,追加 tail 后于 32310 返回,只有单独的 quarantine 标记才会触发 blank recovery fork,而本路径不会设置它。

真实 forkWorker 临时探针(使用 PR 现有测试组件,首次 admission 放行、绑定后的第二次拒绝)得到 deferred → rejected,并同时观察到 queuedActivationPending=true、worker=null、initialStartPending=true;期望可重试的 false 断言按预期咬红。现有路由用例 test/daemon-rename-route.test.ts:789 也证明该 gate 下后继消息不会 refork。

建议在递归 fork 的最终 rejected 分支按当前 session/generation/activation token 守护后释放 opening claim,保留 journal,并补一条“拒绝后下一条 IM 能重放旧 head”的路由级回归测试;不要把 deferred 误当作最终失败。

P2:close/reactivate 与旧 opener 交叠时,新 runtime 没有完成交接

位置:src/core/worker-pool.ts:20924-20925、20938-20941、20968。

创建途中 /close 后,同一 sessionId 可在尚未绑定 existingAppServerEndpoint 时恢复。reactivateClosedSession 会有意清除旧 FIFO(src/services/session-store.ts:2997-3009),新消息随后作为新 activation 入队;但 pendingSharedThreadOpenings 按 sessionId 命中后直接返回。旧 opener 随后因 active runtime 已替换而在代际守卫处退出,finally 仅删除 sessionId,没有唤醒或释放新 runtime 的 claim。新 runtime 同样留下 initialStartPending=true,所以“再发一条消息”仍只会追加 tail。

临时探针按上述语义驱动真实 forkWorker:旧 opener 被人为挂起;replacement runtime 的新输入 second 成功写入 journal,但没有第二次 open;旧 opener 完成后没有 child fork,replacement 的 initialStartPending 仍为 true,预期 false 的断言按预期咬红。

建议保留 sessionId 级远程创建去重(不能改成按 runtime 各自创建,否则会有重复 thread 风险),但为被去重的 replacement 注册完成后的交接/唤醒 continuation,并以当前 runtime、route、activation token 等代际条件守护。若最终状态是 creating 未知态,仍应保持 fail-closed,由 /adopt 对账。

P1 修复复核:通过

src/core/shared-thread-opening.ts:87 在首个 checkpoint 事务中写入显式 inherit,早于已绑定幂等再入的 early return,覆盖创建、重试与再入。这里不能用 undefined:src/services/session-store.ts:2222 会把旧 durable tier 回填。事务提交后 mutateOwnedSessionsAtomically 在原缓存对象上原地合并(session-store.ts:3156-3161),随后 sessionServiceTier 将 inherit 映射成 undefined(worker-pool.ts:4354-4357),因此递归启动不会再触发 worker.ts:31187 的附件守卫。新增测试同时覆盖真实 session store 和已绑定再入,位置恰当。

P3:JSON-RPC 字段仍建议补齐,但当前版本不阻断

src/core/shared-thread-opening.ts:14、:45、:56 的 error response、request、initialized notification 都缺少 jsonrpc:'2.0'。这与仓库既有 App Server 客户端不一致,也不符合 JSON-RPC 2.0 wire shape;建议随本轮修改顺手补齐并加 wire 断言,或单独后续处理。本机 Codex 0.167.0 的真实 App Server e2e 已通过,证明当前版本容忍该缺省,因此维持 P3。

实际验证

  • bun run build:通过(含 tsc/typecheck 与 dist audit)。
  • bunx vitest run --project unit test/shared-thread-opening.test.ts test/session-store.test.ts test/session-lifecycle-start.test.ts test/existing-app-server.test.ts test/daemon-rename-route.test.ts test/worker-durable-expiry-order.test.ts:6 文件、1006 用例全绿。
  • BOTMUX_TEST_REAL_CODEX_BIN=/root/.local/bin/codex bunx vitest run --project e2e test/shared-thread-real-server.e2e.ts:Codex 0.167.0,2/2 全绿,含真实远端 worker 首轮完成与另一客户端读取同一 thread。
  • 上述两个对抗探针均为临时测试、用于确认缺陷后已撤回;工作树恢复干净。
  • 发布时 CI:network-boundary 已成功,其余 build/test/bun-test/binary 任务仍在排队,尚无完整 CI 结论。

绑定后的 admission 拒绝、同步启动失败与 tail-only false 均释放 runtime claim,保留 durable activation 供后续 IM 重放。

close/reactivate 复用 sessionId 时保留创建 single-flight,并以代际栅栏向 replacement runtime 交接旧 head;SharedThreadClient 补齐 JSON-RPC 2.0 字段。

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

Copy link
Copy Markdown
Owner

已追加提交 46683a38139f95e32de8a743bbc435a40ac74a46,修复两个共享 thread opening gate liveness 问题,并补齐同步启动异常与 tail-only false 的恢复覆盖。

  • 最终 admission 拒绝、同步 false、同步 worker 启动异常及 catch 均释放 runtime claim,durable activation 保留;后续 IM 重放旧 head,successor 保持 FIFO。
  • 启动异常补偿按精确 claim token 等代际条件对齐,不再误要求 workerGeneration 不变。
  • close/reactivate 复用 sessionId 时继续保持创建 single-flight,旧 opener 结束后按 runtime/generation/route/activation/epoch/claim 防护向 replacement 交接;creating 未知态仍不重复创建。
  • 请求、initialized 通知和 bootstrap 错误响应补齐 jsonrpc:"2.0"。

验证:bun run build 通过;定向 Vitest 6 文件 1016/1016;真实 Codex 0.167.0 e2e 2/2;Bun 29/29;测试卫生 28/28。多组守卫变异均咬红后恢复并重新跑绿。对抗复核发现的 generation 清理问题已修复,原失败探针及聚焦复验通过。详细问题清单、探针证据和影响面已写入 PR 描述。

@deepcoldy
deepcoldy merged commit 63a4ea3 into deepcoldy:master Nov 25, 2026
15 checks passed
@deepcoldy

Copy link
Copy Markdown
Owner

已合并,感谢贡献!

后续跟进项(不阻断本次合入):

  • 飞书与 Codex Desktop 的真机线上验收尚未执行,建议部署后在真实话题里验证:新话题自动建 thread、Desktop/CLI 第二客户端读取同一 thread、创建失败后的人工 /adopt 对账路径。
  • 创建回执丢失(creating 未知态)叠加会话关闭/恢复时,话题内可能收到两条错误提示,语义正确(fail-closed、不重复创建),仅提示噪音,后续可做去重。

评审中修复的共享话题开工门闩问题(显式 serviceTier 挂载失败、admission 拒绝与关闭恢复后的 gate 卡死)均已带回归测试和变异验证。

@github-actions

Copy link
Copy Markdown

🚀 Released in v3.59.34

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