claude-code - 💡(How to fix) Fix [FEATURE] PreToolUse hooks should have a way of updating the environment

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Fix Action

Fix / Workaround

The workaround I use when I need this is to simply kill Claude Code and resume the session; it then inherits the result of the next zsh preexec hook execution, which finds the correct SSH_AUTH_SOCK. However, this loses state, e.g. of dialogs in progress of being shown or background agents currently running. So simply making exit-and-resume lose less state would also be an acceptable solution - and better in some ways, since it centralizes the environment handling in the shell.

Code Example

if [[ -n $TMUX ]]; then
  function refresh_sock () {
    local sock=$(tmux show-environment SSH_AUTH_SOCK)
    if [[ $sock[1] = '-' ]]; then
      unset SSH_AUTH_SOCK
    else
      export $sock
    fi
  }
  add-zsh-hook -Uz preexec refresh_sock
fi
RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing requests and this feature hasn't been requested yet
  • This is a single feature request (not multiple features)

Problem Statement

When used in tmux, Claude Code suffers from the classic "stale SSH_AUTH_SOCK" problem: when tmux is reattached from a new ssh session, the SSH_AUTH_SOCK set by the new shell is not automatically inherited. As a result, SSH agent forwarding breaks, and anything that depends on the host agent (for example, signing commits with SSH keys) stops working until Claude Code is restarted.

Proposed Solution

There is a solution for this, e.g. in zsh:

if [[ -n $TMUX ]]; then
  function refresh_sock () {
    local sock=$(tmux show-environment SSH_AUTH_SOCK)
    if [[ $sock[1] = '-' ]]; then
      unset SSH_AUTH_SOCK
    else
      export $sock
    fi
  }
  add-zsh-hook -Uz preexec refresh_sock
fi

Claude Code could implement something like that. The crucial missing feature is some way of exporting environment variables from a PreToolUse hook - maybe making CLAUDE_ENV_FILE available there, the way it is with SessionStart hooks.

Alternative Solutions

The workaround I use when I need this is to simply kill Claude Code and resume the session; it then inherits the result of the next zsh preexec hook execution, which finds the correct SSH_AUTH_SOCK. However, this loses state, e.g. of dialogs in progress of being shown or background agents currently running. So simply making exit-and-resume lose less state would also be an acceptable solution - and better in some ways, since it centralizes the environment handling in the shell.

Priority

Medium - Would be very helpful

Feature Category

Interactive mode (TUI)

Use Case Example

Example scenario:

  1. I have an SSH resident key on my local YubiKey, which effectively says that I signed off on what has been signed.
  2. I have one or more Claude Code sessions running in a tmux session on a VM.
  3. I detach that tmux session and resume it later to pick up where I left off.
  4. Claude Code takes some action (e.g. creating a git commit) that I would like to sign off on.

Additional Context

No response

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING