[FEATURE] PreToolUse hooks should have a way of updating the environment
Preflight Checklist
- [x] I have searched existing requests and this feature hasn't been requested yet
- [x] 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:
- I have an SSH resident key on my local YubiKey, which effectively says that I signed off on what has been signed.
- I have one or more Claude Code sessions running in a tmux session on a VM.
- I detach that tmux session and resume it later to pick up where I left off.
- Claude Code takes some action (e.g. creating a git commit) that I would like to sign off on.
Additional Context
_No response_
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗