Fix #281: implement the CLAUDE_ENV_FILE source-and-apply cycle#313
Open
ericleepi314 wants to merge 1 commit into
Open
Fix #281: implement the CLAUDE_ENV_FILE source-and-apply cycle#313ericleepi314 wants to merge 1 commit into
ericleepi314 wants to merge 1 commit into
Conversation
The executor set CLAUDE_ENV_FILE for SessionStart/Setup/CwdChanged hooks but nothing read it back — and the parent directory was never created, so the documented `echo export... > "$CLAUDE_ENV_FILE"` contract was doubly broken. - exports are SHELL-EVALUATED (sh -c 'set -a; . file; env -0') and diffed against the session baseline: export PATH="$HOME/bin:$PATH" expands instead of bricking every later spawn with a literal string; chained prepends compose because the baseline includes prior exports - scope matches TS sessionEnvironment.ts: exports land in a session dict (src/hooks/session_env.py) merged over os.environ at BASH TOOL spawn (foreground + background) — the host process env is untouched - per-event buckets with TS HOOK_ENV_PRIORITY precedence (Setup < SessionStart < CwdChanged); each fire REPLACES its event's bucket on both dispatch paths, so a cwd change drops the previous directory's exports (TS clearCwdEnvFiles) - only a hook that exits 0 gets its exports applied (documented divergence from TS, which sources unconditionally); the ephemeral file is discarded on every exit path - PowerShell hooks get no env file (TS !isPowerShell parity); env built once per fire so the apply step reads the same path the hook wrote; conftest-wide bucket reset keeps tests hermetic Closes #281 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #281
Summary
CLAUDE_ENV_FILEwas set for SessionStart/Setup/CwdChanged hooks but the documented contract — writeexport FOO=barto the file, subsequent Bash tool calls seeFOO— was unimplemented. It was doubly broken: nothing read the file back, and the parent directory was never created so the hook's redirect failed outright.Design (shaped heavily by critic review against the TS source)
/bin/sh -c 'set -a; . file; env -0'and the env delta is captured. This is load-bearing — the canonical idiomexport PATH="$HOME/bin:$PATH"(venv/conda activation) must expand; the first-draft literal parser would have set PATH to the literal string and bricked every subsequent spawn. Pinned bytest_path_prepend_does_not_brick_bash_spawn(real spawn resolution through the modified PATH).sessionEnvironment.tsparity): exports land in a session-scoped dict (src/hooks/session_env.py, leaf module) merged overos.environat Bash tool spawn — foreground and background. The host process env (provider SDK, MCP spawns) is never touched.HOOK_ENV_PRIORITYprecedence (Setup < SessionStart < CwdChanged). Each fire replaces its event's bucket — on both dispatch paths (_run_hooks_for_eventand therun_session_start_hooksrouter) — so a cwd change drops the previous directory's exports (TSclearCwdEnvFilessemantics), and a SessionStart re-fire can't accumulate.!isPowerShellparity). The env dict is built once per fire so the apply step reads the exact path the hook wrote (the previous per-branch build would have minted two different paths).Test plan
typescript/src/utils/sessionEnvironment.ts)🤖 Generated with Claude Code