Fix duplicate SessionStart exports in CLAUDE_ENV_FILE - #668
Fix duplicate SessionStart exports in CLAUDE_ENV_FILE#668manavagarwal123 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 40bf9da8d3
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const existing = fs.readFileSync(process.env.CLAUDE_ENV_FILE, "utf8"); | ||
| if (existing.includes(line)) { | ||
| return; |
There was a problem hiding this comment.
Keep revisited SessionStart values effective
When values in one CLAUDE_ENV_FILE follow A→B→A, such as when an earlier Claude session is resumed after another start, existing.includes(line) finds the stale first A assignment and returns even though the later B assignment remains effective when the file is sourced. Commands then receive the wrong session ID and transcript path, breaking session-scoped job handling and transferring the wrong transcript; replace/deduplicate assignments by variable, or only skip when the matching assignment is already the last effective one.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Thanks for the review! I updated the implementation to remove any existing assignment for the same environment variable before writing the new one. This ensures the last effective assignment always reflects the current SessionStart values and avoids the stale-value case you pointed out.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 77d09cf6fd
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| fs.writeFileSync( | ||
| process.env.CLAUDE_ENV_FILE, | ||
| lines.join("\n") + "\n", | ||
| "utf8" |
There was a problem hiding this comment.
Preserve concurrent updates to the shared env file
When another SessionStart hook writes to the shared CLAUDE_ENV_FILE after this process reads it but before this call, the full-file overwrite silently discards that hook's exports. Matching hooks can execute concurrently, and this hook performs the vulnerable read-modify-write cycle three times, so users with multiple SessionStart hooks can intermittently lose unrelated environment variables; synchronize the update or retain append-only writes rather than replacing the shared file.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Note
To use Codex here, create a Codex account and connect to github.
Closes #661
Summary
Prevent duplicate environment variable exports from being appended to
CLAUDE_ENV_FILEon everySessionStartevent.Changes
appendEnvVar()idempotent.Testing
SessionStartevents no longer append duplicate export lines.