Repository navigation
fix(#829): decode JSON backslash escapes in the hook stdin extractors - #830
Conversation
…hook stdin extractors The stdin extractors returned JSON string values verbatim, so a Windows `cwd` from Codex (which never sets CLAUDE_PROJECT_DIR) arrived as `C:\work\proj`. The Claude Code session slug derived from it came out `c---work--proj`, and a Codex-hosted SessionStart could not find the previous Claude Code session it tries to recover. Decode each `\` to one backslash in every extractor variant. The Digital-Process-Tools#447 and Digital-Process-Tools#511 harnesses now feed bash the script on stdin: Git for Windows' bash.exe collapses `\` in a `-c` argument, which broke the extracted function bodies once they contained the decode.
Review: needs-fixCI is red on all four Windows legs ( Two distinct causes, both within this PR's own diff:
Both are mechanical fixes scoped entirely to this PR's own change; no new issue filed, this stays (Note, unrelated to this PR: |
…arse sites, fix a stale line pin PeterShanxin's tick-review pass (PR Digital-Process-Tools#830 comment) named two concrete, CI- confirmed causes for `needs-fix`: 1. `test_entrypoint_sniff_bookkeeping_message_803.py` and `test_previous_transcript_no_sort_691.py` still pass their extracted script bodies to bash as a `-c` argument -- the same shape `test_stdin_extractor_top_level_wins_447.py` and `test_fast_path_subshells_511.py` were already fixed for in this PR. Git for Windows' bash.exe re-parses its command line and collapses the `\\\\` sequences the decode-bearing extractor bodies contain (Digital-Process-Tools#829), which is why all four Windows legs failed. Both now feed the script on stdin via `bash -s --`, matching the already-fixed sibling files. 2. `test_locale_ranges_695.py`'s ALLOWLIST pinned six `case`-pattern exemptions to line numbers in post-tool-hook.sh, session-end-hook.sh and session-start-hook.sh that this PR's own diff shifted by +3 or +4 (the decode line and its comment, added to each of the three scripts). Every shifted entry is repointed to its pattern's current line; a full targeted run confirms no entry has drifted further. Red (before this commit, local): test_no_unguarded_letter_range_in_shipped_shell and test_every_allowlist_entry_still_points_at_a_range both failed -- three stale allowlist lines reported as live findings. The -c/reparse bug itself is Windows-only bash.exe behaviour and cannot be reproduced on this platform; CI's own logs (17 tests failing per Windows leg, all four legs) are the confirmation, per the review comment. Green (after this commit, local): all six touched/adjacent test files -- 71 passed, 40 skipped. No changelog fragment added: changelog.d/829.fixed.md already covers this issue and assemble_changelog.py --check still passes; this is a follow-up fix to the same PR/branch, not a new change. Co-Authored-By: Claude Sonnet 5 <[email protected]>
|
Thanks for the fix and the thorough write-up, @PeterShanxin! Merged. I added commit 7258234 on top to cover two Windows-only issues (the remaining -c-argument test harnesses and a line-pinned exemption that drifted with the diff's line-count shift) before merging. |
Fixes #829.
What
Every hook stdin extractor now decodes JSON's
\\escape to a single backslash:_stdin_json_stringinsession-start-hook.sh,session-end-hook.shandpost-tool-hook.sh_stdin_json_string_intoinsession-start-hook.sh_stdin_cwdand_stdin_cwd_intoinuser-prompt-hook.shBefore this change, a Windows
cwdfrom Codex, which never setsCLAUDE_PROJECT_DIR, arrived asC:\\work\\proj.session_dir_slugturned that intoc---work--proj, so a Codex-hosted SessionStart's recovery force-save looked for the previous Claude Code transcript in a directory that does not exist.It is a single parameter expansion per extractor,
value=${value//\\\\/\\}, the same replacement formresolve-paths.sh:237already uses. No JSON parser is involved, per #340/#344/#447. Paths, session ids,sourceandreasoncannot contain any other JSON escape, and the extractor already stops at the first".Test harness change
tests/test_stdin_extractor_top_level_wins_447.py(_call) andtests/test_fast_path_subshells_511.py(test_stdin_cwd_into_agrees_with_stdin_cwd) passed the extracted function bodies to bash as a-cargument. Git for Windows'bash.exere-parses its command line and collapses\\, so once the bodies contained the decode, both harnesses fed bash a broken script and all their cases failed on Windows. Both now pass the script on stdin. Their assertions are unchanged.Tests
New:
tests/test_stdin_json_backslash_decode_829.py. It runs all six extractor variants, pulled verbatim from their scripts, against payloads written byjson.dumps:\\serverdecodes pairwise.Red (fix reverted, harness changes kept):
12 failed, 32 passedacross the 829, 447 and 511 files. The 12 are exactly the Windows and UNC cases of the new file:e.g.
returned the JSON-escaped form 'C:\\\\work\\\\proj' instead of the path it encodes, 'C:\\work\\proj'.Green (with the fix):
44 passedfor those three files. The hook-related subset (test_hook_cwd*,test_windows*,test_codex*,test_env_cache*,test_project_root*,test_stdin*,test_post_tool*,test_session_start*,test_session_end*,test_user_prompt*) gave153 passed, 196 skipped. The full suite was not run locally; CI covers the matrix.Observed vs reasoned
PROJECT_DIRand theFileNotFoundErrorfrom Codex Desktop hooks, reported in Hook stdin extractors never decode JSON escapes: Codex on Windows yields a doubled-backslash PROJECT_DIR and the wrong session slug #829;-cargument collapsing\\with Git for Windows'bash.exe;bash -n./bin/bash3.2 handles${v//\\\\/\\}the same way; that is the same replacement form already shipped inresolve-paths.sh;C:\work\projis also what_remember_normalize_win_pathreturns for it (checked for that function alone, not end to end).