Skip to content

feat(adapters): codex-recovery-proxy — mneme's first host adapter - #9

Merged
DottytheHomeless merged 1 commit into
mainfrom
codex-recovery-proxy-for-org
Jul 21, 2026
Merged

DottytheHomeless merged 1 commit into
mainfrom
codex-recovery-proxy-for-org

Conversation

@MXAntian

@MXAntian MXAntian commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds adapters/codex-recovery-proxy/: a stdio↔Streamable-HTTP shim that lets Codex Desktop keep using mneme through the known upstream MCP session wedge bug (openai/codex#32470).

Problem. Codex Desktop's Streamable HTTP MCP client can lose a completion event after a tool call. Once that happens, every subsequent call on that long-lived session hangs forever (no timeout, no error). mneme itself is healthy — the failure is entirely on codex's completion-tracking layer — but the user sees "mneme is dead" until they restart the codex task.

Fix (in the adapter, not in mneme server). Codex sees a normal stdio MCP server (chinatsu-memory). Internally the proxy opens a fresh Streamable HTTP session for every tools/list and every tools/call, DELETEs the session as soon as the call completes, and enforces a bounded per-call timeout. Tool calls are never automatically retried — at-most-once semantics preserved so a lost response cannot become a duplicate store_memory write.

Adapter charter (new pattern for the repo)

  • adapters/<host-name>/ = host-specific glue only. Never modifies mneme server code.
  • Independent README explains: which upstream bug, how to install, when to archive.
  • When upstream fixes the wedge, this adapter can be archived (kept as reference) and codex reconfigured to talk to mneme HTTP directly.

Files

  • adapters/codex-recovery-proxy/proxy.mjs (~245 lines) — the stdio↔HTTP shim
  • adapters/codex-recovery-proxy/proxy.test.mjs (~305 lines) — 60-shot fault matrix + connect-leg hang + post-init hang
  • adapters/codex-recovery-proxy/smoke.mjs — real-mneme end-to-end + /health session-leak check
  • adapters/codex-recovery-proxy/README.md — install, config, archive plan
  • .github/workflows/ci.yml — new step wires the three-scenario matrix into CI

Security / correctness notes

  • Boot-time refuses any C0 control byte (0x00-0x1f) or DEL (0x7f) in the token. Blocks CRLF header smuggling and ANSI-escape terminal contamination.
  • safeError() redacts the token from any error text before it reaches stderr. redactMcpError() rebuilds server-side JSON-RPC McpError with redacted message + data before rethrow, so a bearer-echoing server auth layer cannot leak into the JSON-RPC envelope codex sees.
  • Both tools/list and tools/call wrapped in try/catch — no transport throw can wedge the proxy loop.
  • Server-side JSON-RPC McpError code+data preserved (method-not-found, invalid-params, auth failure). Transport-layer failures (ConnectionClosed -32000, RequestTimeout -32001) flatten to isError so at-most-once retry semantics stay intact. The transport-code set is documented as a today-observation, not a protocol invariant — those codes sit inside JSON-RPC's Server-error reserved range and a future server could technically use them.
  • Only Authorization: Bearer <token> is forwarded — no additional headers, no caching, no rewriting.

Test plan

  • proxy.test.mjs scenario 1: PASS fresh sessions=60, calls=59, live sessions=0 (60 tool calls across 60 fresh HTTP sessions; the hang tool call yields isError: true and does not poison the next call; 8 concurrent calls all succeed on distinct sessions)
  • proxy.test.mjs scenario 2: PASS hanging remote surfaced error in ~680ms (<3000ms ceiling) — proves the connect() / initialize-leg timeout
  • proxy.test.mjs scenario 3: PASS post-init hang surfaced error in ~445ms (<3000ms ceiling, sessions established=1) — proves the tools/list / tools/call timeout leg (exact shape of upstream codex#32470)
  • smoke.mjs vs real mneme @127.0.0.1:18792: active_sessions 0 → 0 (no session leak)
  • node --check on both .mjs files
  • CI step added; will run against node 20 + 22 on ubuntu-latest

Not in this PR

  • No changes to mneme server code (index.mjs, mcp-server.mjs, migrations).
  • No changes to any existing tests.
  • No new server-side dependencies (adapter uses @modelcontextprotocol/sdk which is already a prod dep).

Review history (fanout status)

Round 1 on a personal fork (MXAntian/mneme#1, closed as wrong-target). Surfaced 5 P1 findings, all folded in before opening here.

Round 2 on this org PR (independent claude pass, source-traced against actual SDK internals in node_modules rather than trusting comments). Surfaced 2 P0 + 2 P1 + 3 P2:

Finding Fix in this commit
P0-1 README table description still contained "source_host" residual (same drift pattern round 1 caught, one instance survived) Removed. grep -r source_host now returns zero hits.
P0-2 throw error on the "preserve server-side McpError" path bypassed safeError() redaction. If a server auth layer ever put the bearer into an McpError.message/data, it would leak to codex uncensored. Added redactMcpError() — rebuilds the error with redacted message + data before rethrow.
P1-3 {-32000, -32001} carve-out comment claimed a protocol invariant that doesn't exist. Those codes sit in JSON-RPC's Server-error reserved range. Comment now names it as a today-SDK-observation with caveat.
P1-4 Original hanging-remote test only covered the connect() leg (withFreshRemote runs connect() before operation(); the hanging server stalls initialize first). Left the tools/list / tools/call timeout path unproven. Added scenario 3 (post-init hang) — fake server answers initialize normally, then holds every subsequent JSON-RPC request.
P2-2 Control-char boot guard only blocked \r\n\x00, not the rest of C0. Widened to [\x00-\x1f\x7f].

P2-1 (fallback error text imprecise for Zod schema-reject sub-case) and P2-3 (no repo-wide lint enforcing "adapters/** never touches mneme server code") acknowledged but out of scope for this PR.

🤖 Generated with Claude Code

Adds `adapters/codex-recovery-proxy/`: a stdio↔Streamable-HTTP shim that
lets Codex Desktop keep using mneme through the known MCP session wedge
bug ([openai/codex#32470](openai/codex#32470)).

Problem
-------
Codex Desktop's Streamable HTTP MCP client can lose a `completion` event
after a tool call. Once that happens, every subsequent call on that
long-lived session hangs forever (no timeout, no error). mneme itself is
healthy — the failure is entirely on codex's completion-tracking layer —
but the user sees "mneme is dead" until they restart the codex task.

Fix (in the adapter, not in mneme server)
-----------------------------------------
Do not patch codex (upstream will). Instead, keep the bug from propagating:

- Codex sees a normal stdio MCP server (`chinatsu-memory`).
- Internally the proxy opens a **fresh Streamable HTTP session** for
  every `tools/list` and every `tools/call`, DELETEs the session as soon
  as the call completes, and enforces a bounded per-call timeout.
- Tool calls are **never automatically retried** — at-most-once semantics
  are preserved so a lost response cannot become a duplicate
  `store_memory` write.
- `Authorization: Bearer <token>` is forwarded per-call unmodified;
  the mneme server sees an identical bearer to direct-connect.

Result: a single wedged call pollutes only that call. The next call
opens a brand-new session and is unaffected.

Adapter charter
---------------
- Only host-specific glue (`adapters/<host-name>/`). Never modifies
  mneme server code.
- Independent README explains: which upstream bug, how to install, when
  to archive.
- When upstream fixes the wedge and stabilises, this adapter can be
  archived (kept for future reference) and codex reconfigured to talk to
  mneme HTTP directly.

Files
-----
- `adapters/codex-recovery-proxy/proxy.mjs` (~245 lines) — the stdio↔HTTP
  shim. Boot-time refuses C0/DEL bytes in the bearer (blocks CRLF /
  ANSI-escape smuggling into stderr or on-wire headers). `safeError()`
  redacts the token from every error string before it reaches stderr;
  `redactMcpError()` rebuilds server-side JSON-RPC errors with redacted
  message + data before rethrow so a bearer-echoing auth layer cannot
  leak into the JSON-RPC envelope codex sees. `tools/list` and
  `tools/call` both wrapped in try/catch. Server-side JSON-RPC McpError
  code+data preserved (method-not-found, invalid-params, auth failure);
  transport-layer failures (ConnectionClosed -32000, RequestTimeout
  -32001) flatten to `isError` so at-most-once retry stays intact. The
  transport-code set is documented as a today-observation, not a
  protocol invariant.
- `adapters/codex-recovery-proxy/proxy.test.mjs` (~305 lines) — three
  scenarios:
  1. 60-shot fault-injection matrix: fresh sessions per call, hanging
     tool call must isolate, subsequent calls must succeed, no session
     leak on the remote, no retry on failure.
  2. Connect-leg hang: a remote that accepts TCP but never writes anything
     — proves the `client.connect()` / initialize-leg timeout surfaces
     an error to codex within the configured window.
  3. Post-init hang: a remote that answers initialize normally then
     stalls every subsequent JSON-RPC request. This is the exact shape
     of upstream codex#32470 — session establishes fine, later call
     wedges. Proves the tools/list / tools/call timeout path works, not
     just the initialize leg.
- `adapters/codex-recovery-proxy/smoke.mjs` — real-mneme end-to-end
  smoke that also checks for remote session leak via `/health`.
- `adapters/codex-recovery-proxy/README.md` — install, config, archive
  plan.
- `.github/workflows/ci.yml` — new step runs the adapter's three-scenario
  test alongside the existing mneme test suite.

Fanout self-review — two rounds
--------------------------------
Round 1 on a personal fork (MXAntian/mneme#1, closed as
wrong-target). Surfaced 5 P1 findings, all folded in before opening
here: README false claims about source_host/quarantine dropped
(concepts that live in an unmerged private mneme branch, not OSS
mneme); bearer redaction in safeError() + control-char guard at boot;
tools/list wrapped in try/catch; McpError shape preserved for
server-side errors; CI step added; hanging-remote test added.

Round 2 on this org PR. Surfaced 2 P0 + 2 P1 + 3 P2 — all P0/P1
folded in as part of this commit:
- P0-1: README table description still contained "source_host" residual
  from the same drift pattern round 1 caught. grep -r source_host now
  returns zero hits.
- P0-2: `throw error` on the "preserve server-side McpError" path was
  bypassing safeError() redaction. If a server auth layer ever echoed
  the Authorization header into an McpError.message or .data, the
  bearer would reach codex. redactMcpError() now rebuilds message + data
  through the same redact() pipeline before rethrow.
- P1-3: {-32000, -32001} carve-out was comment-asserted as a protocol
  invariant. It's a today-observation about SDK-only usage; those codes
  sit in JSON-RPC's Server-error reserved range and a future server
  could legitimately use them. Comment now says so + notes the caveat.
- P1-4: Original hanging-remote test only covered the connect() leg
  because withFreshRemote runs connect() before operation() — under
  that server, the initialize POST stalls first and operation() is
  never reached. Added scenario 3 above.
- P2-2 (opportunistic): control-char guard widened from /[\r\n\x00]/ to
  /[\x00-\x1f\x7f]/, blocking ANSI ESC and the rest of C0.

Verified locally after each round:
- proxy.test.mjs: PASS fresh sessions=60, calls=59, live sessions=0
- proxy.test.mjs: PASS hanging remote surfaced error in ~680ms (<3000ms)
- proxy.test.mjs: PASS post-init hang surfaced error in ~445ms (<3000ms,
  sessions established=1)
- smoke.mjs vs real [email protected]:18792: active_sessions 0 → 0

[prediction] 修复: codex Desktop 遇到 mneme MCP session wedge 时不再永久
  失效;用户体感"mneme 挂了"从"重启 codex task"降级为"下次调用自动是新
  session";at-most-once + JSON-RPC 错误 shape 保留 + bearer 不进任何输出
  路径(stderr / JSON-RPC error envelope 都覆盖)
[prediction] 风险: TRANSPORT_ERROR_CODES 硬编码 -32000/-32001。若 SDK
  或未来的 server 在 -32099..-32000 范围加新 error code,会被错误分类
  为 transport 层失败。文件属 adapter scope,mneme server 侧不受影响
[prediction] 验证: CI 跑 adapters/codex-recovery-proxy/proxy.test.mjs 三
  个 PASS 行;codex 侧下次真遇到 mneme 短时不可达时看 codex UI 报
  "tool failed" 而不是"MCP error -32001"

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@MXAntian
MXAntian force-pushed the codex-recovery-proxy-for-org branch from 04e155d to 89902ef Compare July 21, 2026 11:12
@MXAntian
MXAntian marked this pull request as ready for review July 21, 2026 11:13
@DottytheHomeless
DottytheHomeless merged commit aee256e into main Jul 21, 2026
2 checks passed
@DottytheHomeless
DottytheHomeless deleted the codex-recovery-proxy-for-org branch July 21, 2026 11:43
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