Repository navigation
Description
What variant of Codex are you using?
CLI
What feature would you like to see?
I’d like to propose a narrow way for hooks launched by the app-server daemon to receive explicitly selected environment values from the originating Codex client.
Today, when hooks are spawned by the central app-server daemon, they inherit the daemon/server context rather than the shell context of the codex client process. That is the right default, but it makes some hook use cases difficult or impossible: terminal/tab targeting, terminal notifications, status integrations, or client-local routing based on values such as WT_SESSION, TERM_PROGRAM, TMUX, SSH_TTY, etc.
This is adjacent to issues like #37537 and #24638, and related to the broader environment-contract discussion in #26901. I am proposing something narrower than general env inheritance or hook-produced env overlays.
Proposal
Allow user or managed config to define an explicit env-var allowlist scoped by hook event type, e.g.:
[hooks.client_env.SessionStart]
include = ["WT_SESSION", "TERM_PROGRAM", "TMUX"]
[hooks.client_env.SessionEnd]
include = ["WT_SESSION"]
[hooks.client_env.PreToolUse]
include = ["TERM_PROGRAM"]For matching hook events, Codex would pass the captured values as structured hook input:
{
"hook_event_name": "SessionStart",
"cwd": "/path/to/repo",
"client_env": {
"WT_SESSION": "...",
"TERM_PROGRAM": "Apple_Terminal",
"TMUX": "..."
}
}The values would not be merged into the hook process environment by default. Hook commands would read client_env from stdin like other hook payload fields.
Security / behavior constraints
Suggested constraints:
- Only user or managed config may request client env forwarding.
- Repo-local config should not be able to request arbitrary client env vars.
- Explicit names only; no wildcards initially.
- Values are exposed only to hook payloads for the configured hook event type.
- This should not affect model-run shell command environments.
- This should not replace shell_environment_policy; it solves a different provenance problem.
- Values should be treated as hook input, not transcript/model-visible context unless hooks explicitly emit them.
Optional per-matcher form
If finer scoping is useful, this could also support rules:
[[hooks.client_env.SessionStart.rules]]
matcher = "^startup$"
include = ["WT_SESSION", "TERM_PROGRAM", "TMUX"]
[[hooks.client_env.SessionStart.rules]]
matcher = "^resume$"
include = ["WT_SESSION"]Motivation
The daemon architecture means hook process ancestry no longer identifies the originating terminal/client. Some terminal-targeted hooks worked when the TUI process was in the terminal process tree, but now hooks are spawned by the daemon. Passing a small, explicitly configured set of client env values gives hooks a stable, documented way to recover client-local context without broad env inheritance.
Open questions
- Should this be app-server protocol data like client_env_by_hook, or a more general client_context field?
Additional information
No response
Activity
github-actions commentedon Sep 11, 2026
Potential duplicates detected. Please review them and close your issue if it is a duplicate.
Powered by Codex Action
yzcj105 commentedon Sep 26, 2026
Adding a concrete case where the missing client context is actively wrong, not just unavailable, plus how a scoped design could answer the concerns raised on #44774.
It's already misattributing, not just missing
The issue says hooks "inherit the daemon/server context", and that this "is the right default". In practice, with 0.157's managed app-server daemon, that context is a client's context: whichever TUI first spawned the daemon. Reproduced on 0.157.1 with tmux (details in #48500):
codex, submit a prompt (the daemon starts and inherits A'sTMUX/TMUX_PANE).CODEX_HOME: startcodex, submit a prompt.UserPromptSubmit/Stophooks receive A'sTMUX_PANEalongside B'ssession_id.A status integration (workmux) then writes B's session id into A's pane state. Input or status routed by that mapping reaches the wrong session, silently. If A's pane is gone, B's updates are dropped. So hooks already depend on "a particular client": an arbitrary one. #48500 asks for the stale terminal-identity vars to be removed from the daemon's hook env so consumers fail safely; this issue is what would let them work correctly afterwards.
Answering the #44774 concerns with a narrower scope
nullwhen no client did (a detached thread, or a daemon-initiated event).UserPromptSubmitcarries the client that submitted that prompt, which is unambiguous. Tool-call hooks (PreToolUse/PostToolUse) andStopcould carry it only when the turn had exactly one submitting client, elsenull. Consumers treatnullas "don't route".For tmux, the minimum useful allowlist is
TMUX+TMUX_PANE.TMUXalone identifies the server, not the pane.Co-authored by AI assistant.
vityas-off commentedon Sep 26, 2026
Another concrete case, from a terminal emulator rather than tmux: Kmux, a KDE Konsole fork that shows each agent's running / idle / needs-input status on its terminal tab. Its Codex hooks find their tab through variables the terminal sets per tab (
KMUX_DBUS_SERVICE,KMUX_DBUS_SESSION,KMUX_CODEX_PID), the same way tmux integrations useTMUX_PANE.On 0.157.1 the daemon misattributes exactly as #48500 describes: tab B's
UserPromptSubmit/Stophooks carry B'ssession_idwith tab A'sKMUX_DBUS_SESSION, so B's turns show as "running" on A's tab and B's own tab never changes. Two details that may matter for the design:~/.codex/shell_snapshots/<thread>.*.sh) also holds tab A's variables, so commands the model runs for B see A's terminal identity too. BothHooks::newand the shell snapshot take the server process environment (std::env::vars_os()/std::env::vars()).The event-scoped
client_envwould solve it for us withinclude = ["KMUX_DBUS_SERVICE", "KMUX_DBUS_SESSION", "KMUX_CODEX_PID"]. A local-only pid of the triggering client would also be enough on Linux, since the hook can resolve the terminal from that process.Until then, Kmux's Codex launcher passes
--no-daemonto the sessions it starts, so hooks run in the tab's own process. We would drop that once hooks can identify their originating client.Start Codex without the shared background server
jleme commentedon Oct 5, 2026
We ran into this once the TUI started attaching to the shared app-server daemon (observed on codex-cli 0.160.0, macOS; hooks received the launch environment on 0.154.0, where the session ran in the launched process).
Effects for hooks:
codexprocess started with specific environment variables gets a session whose hooks run inside the daemon, with the daemon's environment. Values set for that launch are invisible to its ownSessionStartandSessionEndhooks.session_id,cwd,source,model, ...) carries nothing that identifies the client that started the session. With severalcodexinstances open in the same repository, everySessionStarthook receives equivalent input, so a hook cannot correlate a session with the launch that created it without guessing.--no-daemonrestores the previous behavior, but gives up the shared server for those sessions (codex agents, remote control).The
client_envallowlist proposed here would address 1 to 3 without exposing the client environment wholesale. Two points matter for this use case:SessionEndas well asSessionStart, so the same launch can be identified at both ends of the session.hooks.json, once trusted, should be able to receive values the user allowlisted in user-level config. Otherwise every project-level hook setup also has to edit the user's global config.NateChristiansen commentedon Oct 9, 2026
Lifecycle-scoped originating-client context would let a simple harness target the correct terminal without changing managed-daemon startup. In our use case, a user-allowlisted per-launch terminal marker needs to reach
SessionStart, including fresh launch, direct resume and in-TUI resume, rather than arriving only through turn metadata after a prompt.The context should describe the client initiating this lifecycle event; values inherited from the daemon or the thread's original launcher can point at another terminal. If several clients or a server-initiated event make the origin ambiguous, an explicit unavailable result lets the integration refuse targeting safely.
Please document matching lifecycle context for
SessionEndwhen applicable, while preserving the distinction between a thread ending and a frontend exiting. An old thread's delayed end must not clear the successor's state in the same terminal. Before-input startup timing is discussed in #51360; actual open/closed interactive presence is #35676. A terminal marker solves association, not lifetime, so these requests complement one another.rhuss commentedon Oct 15, 2026
Adding a Zellij use case in support of this proposal.
cc-deck is a Zellij-based workspace with a sidebar that shows the state of each agent session (working, waiting, done) next to its pane. The Codex integration is a set of hooks in
~/.codex/hooks.jsonthat callcc-deck hook --agent codex --pane-id "$ZELLIJ_PANE_ID". Zellij setsZELLIJ_PANE_IDandZELLIJ_SESSION_NAMEper pane, the same way tmux setsTMUX_PANE.With the managed daemon (0.162.1 here), every hook gets the daemon's environment. On my machine the TUI ran in pane 19 while the daemon still carried
ZELLIJ_PANE_ID=27from a pane that had been closed for days, so every event from every Codex TUI pointed at a pane that no longer existed. The session never appeared in the sidebar. When that pane does still exist, events land in the wrong row instead. zj-radar reports the same thing.I understand from #48500 that hooks are client-agnostic by design, and I agree that is the right default. A hook-scoped allowlist as proposed here would solve our case without changing it. For us it would be
ZELLIJ_PANE_IDandZELLIJ_SESSION_NAMEfrom the client that started the turn (or none for a headless turn). In the meantime we will correlatesession_idandcwdwith the TUI processes, as several other tools now do. That works, but it is a heuristic, and two TUIs in the same directory cannot be told apart.fix(hook): attribute Codex events to the right pane under the shared …
IvoryHeart commentedon Oct 21, 2026
+1, with a concrete case and two small asks.
Terminal multiplexers (Herdr, tmux sidebars, Zellij plugins) need to know which pane shows which thread. Since the shared daemon, hooks run with the daemon's environment, so every report carries the environment of whichever pane started the daemon (#48500). There's no supported way left to tie a thread to a pane.
thread/start,thread/resume,turn/start), pass the hook an allowlisted set of variables from that client's environment, declared in hook config, for exampleclient_env = ["HERDR_PANE_ID", "TMUX_PANE", "ZELLIJ_PANE_ID"]. The TUI sends its values when it attaches. Hooks stay client-agnostic, because the variables are opaque to Codex and opt-in.thread-idtitle item. Each item is cut to 32 characters, so the title shows only the first 29 characters of the UUID plus.... With the full ID, multiplexers could bind a pane from its title without prefix matching. We use this now as a workaround, and it follows thread switches.zengzheqing commentedon Nov 12, 2026
I reproduced this on macOS arm64 with both the Codex CLI and managed app-server on 0.167.1. The Rex integration reads
REX_SESSIONandREX_BLOCKin a trustedSessionStartcommand hook. A new conversation in pane B received the IDs of the pane that started the daemon. I checked the hook's model-visible output against the live Rex pane IDs and confirmed that the mismatched values were present in the daemon's own environment. This is the same stale-attribution failure reported in #48500.Minimal reproduction without a terminal integration
In a disposable Codex setup, configure and trust a
SessionStartcommand hook that printsprintf 'pane marker = %s\n' "${DEMO_PANE_MARKER-<unset>}". With no daemon running in that setup, start the daemon from a shell withDEMO_PANE_MARKER=pane-a. Then launch ordinarycodexin a second shell withDEMO_PANE_MARKER=pane-band submit its first prompt. Launch a thirdcodexfrom a shell with the marker unset. All three steps should use the same isolatedCODEX_HOME.The desired readings are
pane-bfor the second shell and<unset>for the third. Falling back topane-amakes a hook address the wrong pane. In the actual Rex test, embedded mode supplied the correct IDs.Reference implementation and verification
I tested a local prototype against the
rust-v0.167.1source. An empty-by-default, daemon-owned allowlist determines which values the local TUI may send over the managed Unix control socket. Daemon launch clears inherited values for those names, the server independently filters incoming names, andSessionStartsees the values for the originating request. A missing value stays unset. The protocol treats the values as opaque strings, so the design also works for terminal integrations with non-UUID identifiers.On macOS, the daemon, hook and transport unit-test suites passed. With one shared daemon, two ordinary Codex launches in different Rex panes each delivered their own IDs, an allowlisted non-UUID marker arrived intact, an unlisted client variable was dropped, and a launch outside Rex supplied no terminal context. These checks cover fresh launches and
SessionStart. Resume,SessionEnd, and shell-tool inheritance would need their own validation.