Skip to content

fix(gh-cli): stop SessionStart hooks piling duplicate exports into the env - #347

Merged
kz-tob merged 7 commits into
trailofbits:mainfrom
sushant-me:fix/gh-cli-dedupe-env-append
Oct 15, 2026
Merged

kz-tob merged 7 commits into
trailofbits:mainfrom
sushant-me:fix/gh-cli-dedupe-env-append

Conversation

@sushant-me

Copy link
Copy Markdown
Contributor

Fixes #341.

The bug

setup-shims.sh and persist-session-id.sh appended to $CLAUDE_ENV_FILE with an unconditional >> on every SessionStart. SessionStart fires on startup, resume and compact, so a long-lived session accumulated one export PATH=... and one export CLAUDE_SESSION_ID=... per fire. The reporter saw 88–246 duplicate lines across seven long-running sessions.

Once the runtime's concatenated env passes its ~8 KB cut, the truncation lands mid-line and every Bash tool call in that session fails.

The change

Both hooks write the line only when this session does not already carry it, checked across the session's env files — the fix the reporter suggested. Their alternative, skipping the hook on resume/compact, would not cover repeated startup fires, so the dedupe is the more complete of the two.

Verification

Ran the hooks against a fake session-env directory:

PATH lines CLAUDE_SESSION_ID lines
origin/main, 3 fires 3 3
fixed, 3 fires 1 1
fixed, 5 fires 1 —

That reproduces the issue's own expectation ("3 consecutive SessionStart runs leave exactly one line of each").

Tests

Three cases added to the existing bats files (bats 1.14.0) — two regression tests and a positive control. I checked that the regression tests fail against origin/main, so they pin the behaviour rather than passing vacuously:

not ok 9  does not duplicate the PATH export across repeated SessionStart fires
not ok 23 persist: does not duplicate CLAUDE_SESSION_ID across repeated fires

With the fix: 23 passing, 0 failing, including the 20 pre-existing cases unchanged.

My first attempt at that revert-check was wrong — I restored from HEAD, which was already my fix commit, so the tests passed and proved nothing. Redone against origin/main, which is where the two failures above come from.

bash -n clean on both scripts.

…e env

Fixes trailofbits#341.

setup-shims.sh and persist-session-id.sh appended unconditionally on every
SessionStart, which fires on startup, resume and compact. A long-lived session
accumulated one PATH line and one CLAUDE_SESSION_ID line per fire; once the
runtime's concatenated env passed its ~8 KB cut, the truncation landed mid-line
and every Bash tool call in that session failed.

Both now write the line only when this session does not already carry it,
checked across the session's env files. The reporter's alternative, skipping
the hook on resume/compact, would not cover repeated startup fires.

Verified by running the hooks against a fake session-env directory:

  origin/main, 3 fires : 6 lines (3 PATH, 3 CLAUDE_SESSION_ID)  <- the reported bug
  fixed,       3 fires : 2 lines (1 PATH, 1 CLAUDE_SESSION_ID)
  fixed,       5 fires : 1 line

Tests: three cases added to the existing bats files (bats 1.14.0). Two are
regression tests and were confirmed to FAIL against origin/main:
  not ok 9  does not duplicate the PATH export across repeated SessionStart fires
  not ok 23 persist: does not duplicate CLAUDE_SESSION_ID across repeated fires
All 23 gh-cli hook tests pass with the fix; the 20 pre-existing ones are
unaffected.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@kz-tob

kz-tob commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Can you bump the plugin version in both manifests and update from main? A quick check across two different session env files would help too.

SessionStart mints a second env file on resume/compact and the runtime
concatenates every env file in the session directory, so the export must appear
once across the set. The existing test only covered repeated fires at one file,
which passes whether the dedupe is scoped to the file or the directory.

Fires at two files then back at the first. Against the unfixed hook this fails
with four CLAUDE_SESSION_ID lines across the pair instead of one.
@sushant-me

Copy link
Copy Markdown
Contributor Author

Done — 9f96e61 bumps to 1.6.3 in both manifests, updates from main, and 1b77cdf adds the two-env-file check you asked for. make validate clean.

The check turned out to be worth writing rather than just running, because the existing test could not tell the two implementations apart. It fires repeatedly at one file, which passes whether the dedupe is scoped to $CLAUDE_ENV_FILE or to $(dirname "$CLAUDE_ENV_FILE")/*.sh — and that distinction is the whole point of the change.

Firing at two env files in one session directory, then back at the first:

CLAUDE_SESSION_ID lines across both files, after 4 fires
before (origin/main) 4
after 1

So the directory-wide grep -qxF -- "$env_line" "$(dirname …)"/*.sh is doing real work, and the new test now fails without it — persist: dedupes across two session env files in one session dir goes red against the unfixed hook, as does the original one. That is the behaviour the ~8 KB concatenation failure depends on: the runtime concatenates every env file in the session directory, so one line across the set is the goal, not one line per file.

I deliberately did not narrow the grep to the current file. Scoping it to $CLAUDE_ENV_FILE would also fix the duplicate-in-one-file case, but it would let the same export appear once per file in the concatenation — which is the failure mode the hook exists to prevent, just slower.

I also checked setup-shims.sh, which carries the identical grep shape for export PATH=…. The two hooks are consistent, so I left it alone rather than change one without the other; say the word if you want the same two-file test mirrored there.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gh-cli: SessionStart hooks append to CLAUDE_ENV_FILE on every resume/compact; session env exceeds ~8 KB and breaks all Bash calls

2 participants