Skip to content

feat(codex): reuse session processes - #1189

Merged
yaozheng-fang merged 1 commit into
mainfrom
feat/codex-session-process-reuse
Oct 29, 2026
Merged

yaozheng-fang merged 1 commit into
mainfrom
feat/codex-session-process-reuse

Conversation

@tiantt

@tiantt tiantt commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

背景

Codex direct + thread_mode="resume" 场景下,旧路径每轮都会重新启动 codex app-server、导入 rollout 并恢复 thread。短回合、多轮 session 里这些固定开销会快速压过实际模型/工具执行成本,同时 rollout 和请求体持续膨胀。

本 PR 将 session 级 Codex process reuse 做成默认开启的 runtime 优化,并补上 MCP/ADK 工具回合的 guarded reuse:工具 manifest 固定在进程凭据上,每轮重新绑定 executor/callback/workspace/budget,并用 SDK 确认的 thread/turn 身份与进程级 request id 防止旧轮次或重连请求误执行。

主要变更

  • 新增 Codex warm process pool,默认保留 20 个 idle process,VEADK_CODEX_PROCESS_POOL_SIZE=0 可关闭;idle TTL 默认 600s,单进程最多复用 100 轮。
  • 默认启用 mcp_process_reuse=True,让稳定 ADK/MCP 工具回合也能复用进程;VEADK_CODEX_MCP_PROCESS_REUSE=0 可仅关闭 MCP process reuse。
  • 用 thread-store pool_namespace 以及 (thread_id, version) 身份校验修复跨 store/session ABA 复用风险;取消 identity check 时会关闭已取出的 warm process,避免泄漏子进程和 CODEX_HOME。
  • 扩展 McpBridge:新增 guarded process credential、turn activation、request-id 去重、finish/drain/revoke lifecycle,idle process 不接受工具执行。
  • 增加 VEADK_CODEX_DENY_READ_PATHS 沙箱隔离增强,并修正已有空 workspace 被误报 reset 的行为。
  • 新增 scripts/benchmark_codex_tools.py 和 tool workload smoke,覆盖 single/parallel/serial/batch 工具场景与 warm hit 清理。
  • 更新中英文 runtime 文档,说明默认开启范围、关闭开关、内存/延迟取舍和 batch tool 示例。

性能收益

本地 macOS、codex-cli 0.159.2、真实 Codex binary + loopback stub Responses backend 的 20 轮同 session benchmark:

  • 第 1 轮:300ms -> 291ms,基本无变化。
  • 第 2-5 轮平均:204ms/轮 -> 41.7ms/轮,约 4.9x。
  • 第 2-20 轮累计:4.416s -> 0.885s,降低约 80%。
  • 20 轮总耗时:4.716s -> 1.176s,降低约 75.1%。
  • 第 20 轮模型请求体:367.5KB -> 47.1KB,降低约 87.2%。
  • 第 20 轮持久化 rollout:844.5KB -> 181.7KB,降低约 78.5%。

理想连续同 session 下 warm 命中率从 0% 提升为 (N-1)/N;20 轮场景为 95%,第 2 轮以后为 100%。线上实际命中率取决于 session 粘性、idle TTL、pool size、配置/fingerprint 稳定性和 store 是否被其它实例推进。

合约与兼容性

  • 用户可见行为:默认开启 direct + resume 下的无工具和稳定工具回合进程复用;不改变 shim 或 ephemeral 路径。
  • 配置/环境变量:新增/记录 mcp_process_reuse、VEADK_CODEX_MCP_PROCESS_REUSE、VEADK_CODEX_PROCESS_POOL_SIZE、VEADK_CODEX_PROCESS_IDLE_TTL_SECONDS、VEADK_CODEX_DENY_READ_PATHS。
  • Runtime lifecycle:错误、中断、保存失败、thread/store 身份不匹配、request id 重复或无法 drain 的工具执行都会关闭进程并回冷路径。
  • 资源成本:默认最多 20 个 idle codex app-server,以常驻内存换后续轮延迟和上下文膨胀下降。

验证

  • pass: git fetch origin && git rebase origin/main -> branch already up to date
  • pass: uv run --extra dev pre-commit run --all-files
  • pass: git diff --check
  • pass: CODEX_RUN_SMOKE=1 uv run --extra dev pytest tests/runtime/codex/test_codex_tool_workloads.py -m codex_smoke -p no:xdist -rs -q -> 8 passed, 3 deselected
  • pass: earlier focused Codex/MCP regression on this diff: uv run --extra dev pytest tests/runtime/codex/test_codex_mcp_bridge.py tests/runtime/codex/test_codex_process_pool.py tests/runtime/codex/test_codex_runtime_sdk.py tests/runtime/codex/test_codex_thread_store.py tests/runtime/codex/test_codex_turn_contract.py tests/runtime/codex/test_codex_tool_workloads.py -q -> 181 passed, 38 skipped
  • fail unrelated: uv run --extra dev --extra extensions pytest -n 8 -m "not codex_smoke and not piagent_smoke" -> 1 failed, 7942 passed, 55 skipped, 4 xfailed. Failure is tests/frontend/server/runtime_artifacts/test_writer.py::test_success_uses_trusted_scope_and_hashes_utf8_bytes, where .md content type was application/octet-stream instead of expected text/markdown; neither the test nor artifact writer files are touched by this PR.
  • blocked existing docs gate: npm --prefix docs run types:check previously failed in the generated docs scaffold at docs/lib/source.ts(3,22): docs/.source/server.ts is not a module.
  • blocked existing docs lint: npm --prefix docs run lint previously failed on existing Biome schema/format/import-order diagnostics outside the edited MDX content.

风险

  • Process reuse is intentionally process-local; cross-instance traffic still resumes from thread store and will not hit warm processes.
  • Real model latency can dominate end-to-end wall time; this optimization mainly removes repeated startup/resume/import/framework overhead and reduces request/rollout growth.
  • The default memory tradeoff should be watched via pool size, idle TTL, warm hit rate, startup duration and total turn duration metrics.

@tiantt
tiantt force-pushed the feat/codex-session-process-reuse branch from 987e6f1 to 0b9a3b5 Compare October 27, 2026 10:19
Comment thread veadk/runtime/codex/proxy.py Fixed
@tiantt
tiantt force-pushed the feat/codex-session-process-reuse branch 3 times, most recently from cab1de4 to 5eaf216 Compare October 27, 2026 11:07
Reuse Codex session processes with private runtime directories, crash recovery coverage, and process-pool lifecycle tests.

Remove repo-local prd-spec/specs records and update repository guidance so public-change evidence is checked before committing.

Co-authored-by: TRAE CLI <traecli@bytedance.com>
@yaozheng-fang
yaozheng-fang merged commit 65f30f2 into main Oct 29, 2026
32 checks passed
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