Skip to content

Hook-scoped client env allowlist for app-server launched hooks #44902

Open

Description

@sding3

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

  1. added
    enhancement
    on Sep 11, 2026
  2. added
    CLI
    hooks
    app-server
    on Sep 11, 2026
  3. github-actions commented on Sep 11, 2026

    @github-actions
    Contributor

    Potential duplicates detected. Please review them and close your issue if it is a duplicate.

    Powered by Codex Action

  4. yzcj105 commented on Sep 26, 2026

    @yzcj105

    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):

    1. Pane A: start codex, submit a prompt (the daemon starts and inherits A's TMUX / TMUX_PANE).
    2. Pane B, same CODEX_HOME: start codex, submit a prompt.
    3. B's UserPromptSubmit / Stop hooks receive A's TMUX_PANE alongside B's session_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

    • Zero or several attached clients: make the field event-scoped, never thread- or turn-wide. It describes the client that triggered this event, and is null when no client did (a detached thread, or a daemon-initiated event).
    • Steering (A starts a turn, B steers it): UserPromptSubmit carries the client that submitted that prompt, which is unambiguous. Tool-call hooks (PreToolUse / PostToolUse) and Stop could carry it only when the turn had exactly one submitting client, else null. Consumers treat null as "don't route".
    • Data flow: hook stdin only, local; never forwarded to the Responses API or model context.
    • Client-side preference: agreed. The allowlist here is exactly "the client declares what it's willing to expose". A client pid alongside the allowlisted values would let consumers verify (e.g. walk the process tree to a terminal pane) rather than trust a string.

    For tmux, the minimum useful allowlist is TMUX + TMUX_PANE. TMUX alone identifies the server, not the pane.

    Co-authored by AI assistant.

  5. vityas-off commented on Sep 26, 2026

    @vityas-off

    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 use TMUX_PANE.

    On 0.157.1 the daemon misattributes exactly as #48500 describes: tab B's UserPromptSubmit / Stop hooks carry B's session_id with tab A's KMUX_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:

    • It is not only hooks. The shell snapshot of a thread started from tab B (~/.codex/shell_snapshots/<thread>.*.sh) also holds tab A's variables, so commands the model runs for B see A's terminal identity too. Both Hooks::new and the shell snapshot take the server process environment (std::env::vars_os() / std::env::vars()).
    • Captured values go stale. The daemon outlives the terminal that started it. Once tab A closes or the terminal app restarts, the captured D-Bus name and object path point at nothing, and every other session's hooks fail. A snapshot taken once at thread or daemon start would not fix that; the values need to come from the client that triggered the event, as proposed here.

    The event-scoped client_env would solve it for us with include = ["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-daemon to the sessions it starts, so hooks run in the tab's own process. We would drop that once hooks can identify their originating client.

  6. jleme commented on Oct 5, 2026

    @jleme

    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:

    1. Launch context no longer reaches hooks. A codex process 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 own SessionStart and SessionEnd hooks.
    2. Sessions cannot be told apart. The documented hook input (session_id, cwd, source, model, ...) carries nothing that identifies the client that started the session. With several codex instances open in the same repository, every SessionStart hook receives equivalent input, so a hook cannot correlate a session with the launch that created it without guessing.
    3. The daemon's environment is frozen and borrowed. The daemon is spawned by whichever client starts it first and keeps that process's environment for its lifetime. Launch-specific values present in that first client leak into the hooks of every later, unrelated session.
    4. The change is silent. The hooks documentation does not describe the daemon or the environment hooks run with, and nothing warns at runtime. Existing hook setups simply stop receiving their context.
    5. The only workaround opts out entirely. --no-daemon restores the previous behavior, but gives up the shared server for those sessions (codex agents, remote control).

    The client_env allowlist proposed here would address 1 to 3 without exposing the client environment wholesale. Two points matter for this use case:

    • It needs to cover SessionEnd as well as SessionStart, so the same launch can be identified at both ends of the session.
    • Hooks registered in a project-level 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.
  7. NateChristiansen commented on Oct 9, 2026

    @NateChristiansen

    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 SessionEnd when 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.

  8. rhuss commented on Oct 15, 2026

    @rhuss

    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.json that call cc-deck hook --agent codex --pane-id "$ZELLIJ_PANE_ID". Zellij sets ZELLIJ_PANE_ID and ZELLIJ_SESSION_NAME per pane, the same way tmux sets TMUX_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=27 from 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_ID and ZELLIJ_SESSION_NAME from the client that started the turn (or none for a headless turn). In the meantime we will correlate session_id and cwd with 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.

  9. IvoryHeart commented on Oct 21, 2026

    @IvoryHeart

    +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.

    1. The fix this issue asks for: for hook events caused by a client request (thread/start, thread/resume, turn/start), pass the hook an allowlisted set of variables from that client's environment, declared in hook config, for example client_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.
    2. Something smaller that helps today: don't truncate the thread-id title 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.
  10. zengzheqing commented on Nov 12, 2026

    @zengzheqing

    I reproduced this on macOS arm64 with both the Codex CLI and managed app-server on 0.167.1. The Rex integration reads REX_SESSION and REX_BLOCK in a trusted SessionStart command 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 SessionStart command hook that prints printf 'pane marker = %s\n' "${DEMO_PANE_MARKER-<unset>}". With no daemon running in that setup, start the daemon from a shell with DEMO_PANE_MARKER=pane-a. Then launch ordinary codex in a second shell with DEMO_PANE_MARKER=pane-b and submit its first prompt. Launch a third codex from a shell with the marker unset. All three steps should use the same isolated CODEX_HOME.

    The desired readings are pane-b for the second shell and <unset> for the third. Falling back to pane-a makes 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.1 source. 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, and SessionStart sees 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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    CLIapp-serverenhancementhooks

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions