Repository navigation
fix(gh-cli): stop SessionStart hooks piling duplicate exports into the env - #347
Conversation
…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.
|
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.
|
Done — 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 Firing at two env files in one session directory, then back at the first:
So the directory-wide I deliberately did not narrow the grep to the current file. Scoping it to I also checked |
Fixes #341.
The bug
setup-shims.shandpersist-session-id.shappended to$CLAUDE_ENV_FILEwith an unconditional>>on every SessionStart. SessionStart fires onstartup,resumeandcompact, so a long-lived session accumulated oneexport PATH=...and oneexport 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 repeatedstartupfires, so the dedupe is the more complete of the two.Verification
Ran the hooks against a fake session-env directory:
CLAUDE_SESSION_IDlinesorigin/main, 3 firesThat 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: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 againstorigin/main, which is where the two failures above come from.bash -nclean on both scripts.