Skip to content

fix: consume Codex per-call _meta.threadId in the MCP shim (#2024) - #2027

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-10-04_codex-mcp-native-threadid
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-10-04_codex-mcp-native-threadid

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2024

What & why

Codex >=0.160 stamps the REAL thread id on every MCP tools/call via _meta.threadId (codex-rs core/src/mcp_tool_call.rs with_mcp_tool_call_ids_meta; its sessionId is a transient run id and is deliberately NOT consumed). The MCP shim ignored it entirely and kept routing through the stale BILI_CONVERSATION_ID baked into the generated Codex MCP config — so in Code Mode (where the inner compress call never appears as an independent upstream function_call, hence no outbound witness either) every compress/decompress/status/cache/search call failed with unknown plugin conversation. Incident evidence + reproducible PoC: #2024.

The fix lands at the SHARED tool entry (all five tools), not a compress special-case.

Changes

src/mcp.ts — consume strictly-validated per-call _meta.threadId (non-empty string after trim; anything else treated as ABSENT so malformed metadata can never shadow the legacy env/meta/per-call channels). Applied per-call only: NEVER written back to the shim's global conversationId (main/sub-agent threads sharing one MCP process must not clobber each other's binding). Host-stamped calls are flagged to the proxy as nativeCaller in the POST body. The #841 search-target exception is extended so a native caller keeps args.conversation_id as a read-only cross-session target even when no default binding exists.

src/plugin.ts — the routing ladder gains rung 0: a host-stamped nativeCaller outranks a conflicting unique outbound witness (a collision, not truth — the explicit boundary in #2024). Unmarked ids keep the exact #1685 precedence (witness still beats model-transcribed/static ids). An unresolvable native id fails loudly (404 + #1158 diagnostic) — no witness rescue, no sibling adoption.

tests/mcp-native-thread.test.ts — three layers: (1) shim contract (real src/mcp.ts over stdio against a mock proxy capturing the exact POST bodies); (2) proxy routing policy (real startServer: native-vs-conflicting-witness, unmarked-control, loud-failure legs); (3) incident-shape e2e leg (real server + real shim over stdio with a stale env id, interleaved main/child threads, meta-less call still failing against the untouched residue).

CLIENTS.md / CLIENTS.zh-CN.md — document the new channel for codex (zh/en synced).

Behavior changes (old -> new, all intentional)

  1. New wire field nativeCaller on POST /__bili/plugin/tool — set ONLY by the MCP shim and ONLY when the id came from _meta.threadId. Old proxies ignore the unknown field; old shims never send it.
  2. Routing precedence — before, a unique outbound witness beat ANY differing body id. Now, for flagged calls only, the host-stamped id wins over a conflicting unique witness; unflagged calls are byte-for-byte unchanged (零注入会话身份:移除模型可见 conversation_id,以出站 tool_use 环见证路由 MCP 工具调用(fixes #1611 的非注入路线) #1685 control leg pinned in tests).
  3. search_context without a default binding + valid _meta.threadId — before, args.conversation_id was the routing fallback (then stripped); now the call routes to the native thread and the target stays read-only in args. Only affects hosts sending Codex-style per-call metadata (new capability); legacy hosts unchanged.
  4. New warn log line on a witness/native conflict; the execution log can now read routed by native.

Config surface

None changed. nativeCaller is a wire field set only by the MCP shim — no new env var, no config.json field, no CLI flag.

Verification

  • npm run typecheck clean.
  • New test tests/mcp-native-thread.test.ts: 7/7 pass.
  • Targeted neighbors on the rebased head (e2e-plugin-tool-routing test: e2e the #1685 plugin tool routing ladder + MCP shim lane #1773, issue760-percall-conversation, tool-ring, tool-witness-pipes, search-context-cross-session): 49/49 pass.
  • npm run build success.
  • Rebased onto current master (ec95a6d4) before running these checks; full-suite regression left to CI.

Codex >=0.160 stamps the real thread id on every MCP tools/call via _meta.threadId; the shim ignored it and kept routing through the stale BILI_CONVERSATION_ID baked into the generated config, so in Code Mode (no outbound witness) every tool call failed with 'unknown plugin conversation'.

mcp.ts: strictly validate per-call _meta.threadId (malformed => absent), apply per-call only (never overwrite the shared binding), flag host-stamped calls as nativeCaller. Extend the #841 search-target exception so a native caller keeps args.conversation_id read-only even without a default binding.

plugin.ts: routing ladder gains rung 0 — a flagged nativeCaller outranks a conflicting unique outbound witness; unmarked calls keep exact #1685 precedence. Unresolvable native id fails loudly (no witness rescue/adoption).

tests/mcp-native-thread.test.ts: shim contract + proxy policy + incident e2e. CLIENTS.md/CLIENTS.zh-CN.md: document the channel (zh/en synced).
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-04_codex-mcp-native-threadid (1460932)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2061-pr.2027.32bd87b

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2027.tgz
npm install -g package

Install instructions are refreshed on each push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codex Code Mode 压缩失败:MCP shim 忽略每次调用的 _meta.threadId,错误使用过期 env ID

1 participant