Repository navigation
[PR] feat(codex): concurrent multi-session under the same CODEX_HOME + CODEX_SQLITE_HOME shared state DB (#3203 / #2222) | [PR] feat(codex): 同一 CODEX_HOME 并发多会话 + CODEX_SQLITE_HOME 共享状态库 (#3203 / #2222) - #3321
Conversation
…e via CODEX_SQLITE_HOME (#2222) bili codex used one shadow-home overlay to solve three unrelated problems (per-launch config injection, SQLite state isolation, everything else). The state-isolation half dragged every WAL-mode db into the overlay per launch and needed copy-on-launch + origin snapshots + startup fold + exit merge-back + provenance adjudication to stay correct (#381/#1917/#1918/ Codex >= 0.147.0 honors CODEX_SQLITE_HOME (source-verified across 0.147/0.150/0.155/0.159/0.160.1/main, empirically confirmed on the CI-pinned 0.147.0 binary). New default for such binaries: point it at the REAL home so the live dbs stay on their single physical path where native and bili runs coordinate through SQLite's own WAL locking. The overlay shrinks to the generated .env/config.toml plus symlinks; leftover legacy sets are folded back once through the existing provenance adjudication. BILI_CODEX_STATE_MODE (auto|shared|legacy, default auto) gates the strategy; old binaries (< 0.147.0 or unparseable version) stay on the legacy path unchanged. A user-set CODEX_SQLITE_HOME wins over our default. (cherry picked from commit 039c536)
…overlay slots + per-session proxy lanes (#3203) Shared-state mode (codex >= 0.147.0, BILI_CODEX_STATE_MODE auto/shared) now claims a per-session overlay slot from a bounded pool (<CODEX_HOME>-bili-s1..s16) BEFORE starting the proxy, and runs that launch's proxy on its own lane (codex-s<N>): two same-build launches sharing one lane would refuse each other at the port layer (#3044 twin rule), so concurrent sessions need distinct lanes. Slots persist and are reused (cold-start thread records resolve through the slot, #43bf59fc9); a crashed session's slot is reclaimed by the next launch. The pre-#3203 fixed-name <CODEX_HOME>-bili overlay migrates into the real home once on first launch, and is refused while a live legacy-era launch still holds it (dual-writer guard). Legacy mode keeps the fixed-name single overlay + exclusive lease byte-for-byte. Also: installation_id is seeded into the real home on first shared launch so every slot links one canonical identity file instead of quarantining copies; byte-identical merge-back losers are dropped silently instead of producing <name>.bili-conflict entries (matches the mergeSqliteSet snapshot rule).
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.3912-pr.3321.ce802a4This exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr3321.tgz
npm install -g packageInstall instructions are refreshed on each push. |
(ai: vllm-qwen/qwen3.8-27b) 查重:不重复。本 PR 对应两个独立 issue——#2222(状态库放哪, 开始审核:逐段读 diff、清点既有行为、审测试有效性、核对 CI,结论按结构化八问回复。 |
…ack unit test (#3321) CI gate scripts/check-unused-exports.mjs flagged five launcher symbols with no reference outside their declaring file. prepareCodexSessionSlot/codexSlotLane/ codexBinaryVersion/CodexStateMode are implementation details of runLaunch and the #2222 capability probe — drop the leading export. mergeOverlayEntry was meant to be exercised by the byte-identity drop test in tests/codex-concurrent-launch.test.ts; that import + test case were lost from the tree before the first commit, so restore them verbatim.
The SPAWN_RE tripwire in scripts/run-ci-tests.mjs (#1248) killed all 12 test shards plus the fake-upstream lane on the previous head: the new suite spawns child processes but was missing from LIVE_FILES. Inserted alphabetically; every suite after it shifts one zone band up (max base stays far below the Linux ephemeral range per the file's own docstring).
…#3321) The test hardcoded <CODEX_HOME>-bili, which shared-mode launches (#2222/#3203) no longer create — they use per-session slots <CODEX_HOME>-bili-s<N>. The e2e lane pins codex 0.147.0 (shared mode active), so the fixed-name path was absent and 'the overlay must own the generated .env' failed on CI while every name-filtered local run skipped the test. Scan for the actual overlay beside the real home (covers both naming shapes); the four content assertions are unchanged.
…drop branch The restored merge-back case covers identical-dst-newer and differing-src-newer; these two pin the other branches: an IDENTICAL slot-side copy that is NEWER than the real-home file must still be dropped silently (the src-newer identical branch added by #3203), and a DIVERGENT copy against a newer real-home edit must quarantine the slot side while the home edit wins. Also drops the unused home binding left in the adjacent test.
…on race
The concurrent pair flaked (1/4 locally, 1/1 ubuntu CI): two codex processes cold-starting milliseconds apart on a brand-new home interleave codex's non-atomic sqlx migrations; one of them aborts its whole state-runtime init ('while executing migration 2: table logs already exists' — WARN only, hidden at RUST_LOG=error), so that session's thread row is never written although its rollout JSONL survives on disk. That race lives upstream of bili (which owns neither codex's schema nor its migrations) and does not occur in #3203's real scenario — two terminals in the SAME project share an already-migrated home. One sequential warm session closes the fresh-db window so the lane deterministically tests what bili owns: slots, per-session lanes, the single live db.
(ai: vllm-qwen/qwen3.8-27b) 审核完成。PR 方向正确、实现质量总体高(槽位池 + 每会话 lane + 共享活库的设计与既有 overlay/租约架构一致),但发现 1 个上游竞态缺陷(旧版 #3203 e2e lane 不稳定的根因,本地复现并 strace 钉死因果链)和一批 CI/测试问题。全部已在 PR 分支上以增量 commit 修复(含与作者侧并行修复的协调去重),head 现为 结构化八问1. 是什么问题 2. 什么原因导致的 3. 影响范围与严重程度 4. 怎么修复的
5. 是否有回归问题 6. 是否破坏缓存 7. 是否改了设计方向 8. 是否引入了新的配置 CI 状态(head
|
…orced mtimes The forced-mtime approach failed on the Windows CI runners: fs.utimesSync rejects future timestamps there (EINVAL), which red'd both merge-back cases on windows-latest node 22/24. Past timestamps proved equally unreliable: on at least one Linux overlay filesystem a past-value utimesSync read back as a corrupted constant mtime, silently breaking the ordering premise (observed during local triage of head de82dd5). Recency now comes from natural write order plus a fixed 1.1s pause (the Atomics.wait idiom already used by tests/external-summary.test.ts) — portable across ext4/NTFS/APFS. The byte-identical cases need no pinning at all: identical bytes converge in BOTH recency directions, so the branch under test is unconditional. Also strengthened the no-quarantine assertions from a root-level readdir to a recursive findConflicts() walk (quarantines land NESTED next to the dst file, so the root-level check could never see them); the divergent cases now assert the exact quarantine set.
(ai: vllm-qwen/qwen3.8-27b) 跟进:Windows 单测修复已提交 + 剩余 CI 红全部定性为 master 既有红(已按测试名在 master 日志中逐一证实)。 1. 新增提交
|
| 测试 | 本 PR head | master 自身 | 证据(master job) |
|---|---|---|---|
#1086 T5 liveness: plain-client chat session keeps compressing as it grows |
shard-(x,2) 全红 | 红(ubu+win,not ok 574) |
116547487635 / 116547487840 |
storage-render golden (chat|anthropic|responses|google) |
shard-(x,3) 全红 | 红(ubu+win,not ok 756–759) |
116547487877 / 116547487595 |
master 最新一轮 CI 是 12 个 test shard 里 9 个红。按名检索 open+closed issue 均无既有跟踪条目。结论:这两簇是 master 存量问题(一个在 plain-client 压缩活性、一个在 storage-render 快照),不是本 PR 引入的回归;本 PR 自己拥有的 lane(unused-exports、e2e-fake-codex ubuntu 的 #3203/#1802、windows #2195 lane 及其余全部 guard/e2e/native/registry)在上一个 head 已全部转绿。
3. 待你拍板的两件事
- master 基线红:按流程本 PR 不动它们(不同子系统、不同机制,塞进来违反一 issue 一范围)。是否立一个汇总 issue(带来源标记,列这两簇 + 上面的 job 证据)?[PR] feat(codex): concurrent multi-session under the same CODEX_HOME + CODEX_SQLITE_HOME shared state DB (#3203 / #2222) | [PR] feat(codex): 同一 CODEX_HOME 并发多会话 + CODEX_SQLITE_HOME 共享状态库 (#3203 / #2222) #3321 是自动立项的 [PR] issue,按来源链规则我不擅自立项,等你确认。(若分支保护要求这些 shard 全绿才能合,则需先修 master 基线或豁免。)
- 上游 OpenAI/codex issue(上一条回复已列证据的新库并发迁移竞态):是否批准对外提交?
CI 状态:4111453c2 的 12 个 test shard 正在跑,预期 Windows 两用例转绿、其余红只剩上表两簇(master 基线)。跑完我再补一条确认。
(ai: vllm-qwen/qwen3.8-27b) CI 确认(head
三个红的定性:
收工判定:代码与行为层面本 PR 已完成闭环(功能 + 上游竞态的确定性处置 + 全部 review 修复),CI 上除 master 存量红与一次单发 flake 外全绿。合并只被 master 基线红卡住(若分支保护要求 test shards 全绿)。两件待你拍板的事维持上一条不变:① 是否为这两簇 master 基线红立一个汇总 issue(带来源标记);② 是否批准向 OpenAI/codex 上游立 issue(新库并发迁移竞态)。你确认后我立即执行。 |
…ack unit test (ranxianglei#3321) CI gate scripts/check-unused-exports.mjs flagged five launcher symbols with no reference outside their declaring file. prepareCodexSessionSlot/codexSlotLane/ codexBinaryVersion/CodexStateMode are implementation details of runLaunch and the ranxianglei#2222 capability probe — drop the leading export. mergeOverlayEntry was meant to be exercised by the byte-identity drop test in tests/codex-concurrent-launch.test.ts; that import + test case were lost from the tree before the first commit, so restore them verbatim. (cherry picked from commit c58e7e7)
…ei#3321) The SPAWN_RE tripwire in scripts/run-ci-tests.mjs (ranxianglei#1248) killed all 12 test shards plus the fake-upstream lane on the previous head: the new suite spawns child processes but was missing from LIVE_FILES. Inserted alphabetically; every suite after it shifts one zone band up (max base stays far below the Linux ephemeral range per the file's own docstring). (cherry picked from commit 752ae07)
…i#1802 test (ranxianglei#3321) The test hardcoded <CODEX_HOME>-bili, which shared-mode launches (ranxianglei#2222/ranxianglei#3203) no longer create — they use per-session slots <CODEX_HOME>-bili-s<N>. The e2e lane pins codex 0.147.0 (shared mode active), so the fixed-name path was absent and 'the overlay must own the generated .env' failed on CI while every name-filtered local run skipped the test. Scan for the actual overlay beside the real home (covers both naming shapes); the four content assertions are unchanged. (cherry picked from commit 3148100)
Fixes #2222
Closes #3203
查重:不重复 —— 本 PR 拾取 #2222 的分支(
2026-10-06_codex-sqlite-home-shared-state,以 cherry-pickbbc517061合入本分支),并在其上补齐 #3203 要求的并发能力。两者同属 codex overlay/锁机制家族但关注点不同:#2222 解决「状态库放哪」,#3203 解决「同一 home 能同时跑几个会话」。What this does
同一项目目录、同一
CODEX_HOME下并发多个bili codex会话(#3203)在 codex ≥ 0.147.0(默认BILI_CODEX_STATE_MODE=auto→shared)下可用:<CODEX_HOME>-bili-s1..s16认领一个独立的每会话 overlay 槽位;生成的.env/config.toml(代理路由 + MCP 会话绑定)按槽位隔离。codex-s<N>上——这是必须的:同构建的两次启动共享一个 lane 时会在端口层按 autoRestartOnUpdate + occupied target port -> orphan process leak: new instance silently hops to the next port, old instance never exits #3044 twin 规则互相拒绝(诊断中实测第二个会话在端口 18787 上作为第一个会话的「twin」而死)。CODEX_SQLITE_HOME留在真实$CODEX_HOME的单一物理路径(Bili Codex's startup mechanism has various issues that currently rely on band-aid patches. We need to explore a new mechanism to thoroughly resolve these problems. | bili codex 启动机制带了各种问题需要狗皮膏药打补丁,探索新机制彻底解决这些问题 #2222):两个会话的历史落在同一个活库上,由 SQLite 自己的 WAL 锁协调,退出顺序无关;崩溃恢复 = 标准 WAL 恢复。无逐次启动的数据库拷贝、无退出合并仲裁、无 shared 模式下的.bili-conflict代。<CODEX_HOME>-bilioverlay 在首次 shared 启动时一次性迁回真实 home;若仍有存活的旧版 launch 持有它则拒绝本次启动(双写者防护)。BILI_CODEX_STATE_MODE=legacy或 codex < 0.147.0)保持固定名单一 overlay + 独占租约,逐字节不变。Behavior changes (old → new)
<home>-bili<home>-bili-s1..s16,复用、绝不自动删除codexcodex-s<N>(port-zone.json 至多 +16 条记录);各会话退出时只停自己的代理installation_id.bili-conflict<name>.bili-conflict.sandbox_migration=v1产生了真实的冲突噪音;字节相同 ⇒ 无数据损失(与 mergeSqliteSet 快照规则一致)非目标(已注明):legacy 模式其余行为逐字节不变;directUrl-no-MCP 的并发启动仍共享 lane
codex。Config surface
BILI_CODEX_STATE_MODE(仅环境变量的逃生舱,默认auto,取值auto|shared|legacy)——本 PR 的 Bili Codex's startup mechanism has various issues that currently rely on band-aid patches. We need to explore a new mechanism to thoroughly resolve these problems. | bili codex 启动机制带了各种问题需要狗皮膏药打补丁,探索新机制彻底解决这些问题 #2222 commit 引入,已在 CONFIGURATION.md 中英 + website/config-reference/env-only.yaml 文档化。无新增配置文件键,无改名/改语义。为何不能只用 spawn env:codex 的load_dotenv()无条件set_var(),用户$CODEX_HOME/.env会压过 spawn env(bili codex 报错 MCP client for codex_apps failed to start 仍然存在 #1802),所以生成式每槽位.env仍是必需的。Review 发现与修复(2026-10-16)
发现的上游缺陷(旧版 #3203 lane 不稳定的根因):两个 codex 进程在毫秒级间隔内冷启动一个全新 home 时,会交错执行 codex 非原子的 sqlx 迁移,其中一方整个 state-runtime 初始化中止(
while executing migration 2: table logs already exists——仅 WARN,RUST_LOG=error下不可见)。该会话流程正常完成,但线程记录从未写入共享库(rollout JSONL 留在磁盘上;实测后续 backfill 不会自动恢复它)。复现证据(strace 时间线):B 以 O_CREAT 打开state_5.sqlite创建文件 → A 在 5ms 后(B 迁移进行中)打开同一文件 → 两者先后打开同一-wal/-shm→ B 报错退出 state 初始化且此后不再触碰该库(无重试)。根因在 codex ≤0.147.0 自身(bili 不拥有 codex 的 schema/迁移,无法在 bili 层修);且 #3203 的真实场景(同项目两个终端)共享已迁移的 home,不存在此窗口。处置:e2e lane 先用一个顺序会话预热 home(20f3a7b0a),让并发断言确定性地测试 bili 拥有的行为(槽位、每会话 lane、单一活库)。建议向 OpenAI/codex 上游立 issue——外部提交待 owner 批准后执行。bili 侧备选方案(冷启动串行化)评估为不可行而不做:没有可观察的「迁移完成」事件,任何固定时长等待都是任意值。其余 review 修复(增量提交,本地定向验证全绿):
c58e7e778unused-exports gate:移除 4 个 internal-only export;恢复逐字节相同合并回写的单元测试(首次提交前从树中丢失)。752ae0784新套件注册进scripts/live-files.mjs的 LIVE_FILES——缺注册导致前一 head 触发 SPAWN_RE tripwire,杀掉全部 CI test shards。31481000dbili codex 报错 MCP client for codex_apps failed to start 仍然存在 #1802 lane 由断言固定名 overlay 改为动态解析实际 overlay/slot 目录(shared 模式用槽位)。01d7aa206补齐逐字节相同 drop 分支的另两个 mtime 方向(src 更新者相同则丢弃 / dst 更新者分歧则隔离)。b710361d2ubuntu 失败 artifact 收集扩到全部 lane 的 workdir 前缀(原先只收e2e-codex-fake-*)。de82dd5dc中文文档全角逗号笔误。4111453c2逐字节合并回写单测原先用fs.utimesSync钉未来 mtime 制造新旧序——windows-latest(node 22/24)对未来的 utime 直接抛 EINVAL,CI 上两用例持续红;而钉过去 mtime 在部分 Linux overlay FS 上回读为损坏常量(实测探针),两个方向的强制 mtime 都不可移植。改为完全不依赖强制 mtime:字节相同用例无需任何钉定(相同字节时两个 recency 分支都静默丢弃 src——读mergeOverlayEntry源码确认);分歧用例改用自然写入顺序 + 固定 1.1s 停顿(Atomics.wait,仓库既有惯用法)。顺带把「无.bili-conflict」断言从根目录 readdir(看不到嵌套隔离文件,原本就是弱断言)升级为全树 walk + 精确集合断言。Tests
tests/codex-concurrent-launch.test.ts(9 个测试):槽位分配 +CODEX_SQLITE_HOME钉住;legacy 回归守卫;4 进程竞争 → 槽位互不相同且无BILI_OVERLAY_BUSY;耗尽(16 个槽位全被存活进程持有)拒绝;陈旧槽位回收;legacy overlay 迁移(sqlite 行保留、零冲突);存活 legacy 持有者的双写者拒绝;逐字节相同的合并回写静默丢弃 vs 真实冲突仍隔离(两个 mtime 方向各一用例)。tests/launcher-codex-sqlite-home.test.ts:shared 模式断言更新为槽位正则。tests/e2e/e2e-codex-fake.test.ts:新增#3203lane —— 同一 home 上先用一个顺序会话预热(绕开上文的上游新库迁移竞态),再跑两个并发bili codex exec会话:断言 lane/槽位互异、线程数 == 基线+2、任何-bili*overlay 内零 sqlite 文件、零.bili-conflict、installation_id归位真实 home、并发跑完后原生codex exec resume --last成功。既有#2222lane 更新为扫描全部-bili*overlay;#1802lane 的 overlay 路径改为动态解析(31481000d)。ci-windows-codex.yml:pattern 现为#2195|#1965|#2222|#3203,timeout 30 min,artifact 收集含tmp/e2e-codex-3203-*。4111453c2):typecheck 干净;定向单元 codex-concurrent-launch 9/9(连续 4 轮稳定)、launcher-codex-sqlite-home 7/7;e2e#3203连续 3/3 绿(预热前本地 4 次中失败 1 次)、#18021/1 绿(本机 codex-cli 0.147.0 = shared 模式最低版本)。全量判定以本 head 上的 CI 为准。