Nested codex exec commands inherit parent CODEX_THREAD_ID instead of nested thread id

Resolved 💬 2 comments Opened Mar 23, 2026 by aquiles-ai123 Closed Apr 7, 2026

Nested codex exec commands inherit the parent CODEX_THREAD_ID instead of the nested thread id

Summary

When codex exec is launched from inside an existing Codex session, the nested session gets its own thread.started.thread_id, but commands executed by that nested session still see the parent CODEX_THREAD_ID in their environment.

This looks like environment leakage from the outer Codex process into nested Codex command executions.

Why this matters

Any script, hook, or command that relies on CODEX_THREAD_ID for correlation, logging, or per-thread state will attribute work to the wrong thread.

Environment

  • Codex CLI: codex-cli 0.116.0
  • Outer shell environment includes:
CODEX_THREAD_ID=019d1afb-b2c7-72d3-b240-f3ee9d31b2c9

Repro

This repro is intended to be run from inside an existing Codex session.

Read-only example

codex exec --ephemeral --skip-git-repo-check --color never --json   -s read-only -C /home/cryptotrading74   'Run env | sort | rg "^CODEX_THREAD_ID=" and stop.'   > /tmp/codex_threadid_mismatch.jsonl

Observed transcript:

{"type":"thread.started","thread_id":"019d1be2-5752-7553-80c4-66e6dbddb174"}
{"type":"item.completed","item":{"type":"command_execution","aggregated_output":"CODEX_THREAD_ID=019d1afb-b2c7-72d3-b240-f3ee9d31b2c9\n","exit_code":0,"status":"completed"}}

Workspace-write example

codex exec --ephemeral --skip-git-repo-check --color never --json   -s workspace-write -C /home/cryptotrading74   'Run env | sort | rg "^CODEX_THREAD_ID=" and stop.'   > /tmp/codex_threadid_mismatch_ws.jsonl

Observed transcript:

{"type":"thread.started","thread_id":"019d1be2-fe81-74b3-8809-3cc1f9c8f3a8"}
{"type":"item.completed","item":{"type":"command_execution","aggregated_output":"CODEX_THREAD_ID=019d1afb-b2c7-72d3-b240-f3ee9d31b2c9\n","exit_code":0,"status":"completed"}}

Expected behavior

Commands executed by a nested codex exec session should see the nested session's thread id, or CODEX_THREAD_ID should be unset if that variable is not meant to be inherited.

Actual behavior

Commands see the parent session's CODEX_THREAD_ID, even though the nested session has a different thread.started.thread_id.

Files / artifacts

  • /tmp/codex_threadid_mismatch.jsonl
  • /tmp/codex_threadid_mismatch_ws.jsonl

Suggested title

Nested codex exec commands inherit parent CODEX_THREAD_ID instead of nested thread id

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗