Skip to content

fix(#829): decode JSON backslash escapes in the hook stdin extractors - #830

Merged
fdaviddpt merged 2 commits into
Digital-Process-Tools:mainfrom
PeterShanxin:fix/829
Sep 28, 2026
Merged

fdaviddpt merged 2 commits into
Digital-Process-Tools:mainfrom
PeterShanxin:fix/829

Conversation

@PeterShanxin

Copy link
Copy Markdown
Contributor

Fixes #829.

What

Every hook stdin extractor now decodes JSON's \\ escape to a single backslash:

  • _stdin_json_string in session-start-hook.sh, session-end-hook.sh and post-tool-hook.sh
  • _stdin_json_string_into in session-start-hook.sh
  • _stdin_cwd and _stdin_cwd_into in user-prompt-hook.sh

Before this change, a Windows cwd from Codex, which never sets CLAUDE_PROJECT_DIR, arrived as C:\\work\\proj. session_dir_slug turned that into c---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 form resolve-paths.sh:237 already uses. No JSON parser is involved, per #340/#344/#447. Paths, session ids, source and reason cannot 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) and tests/test_fast_path_subshells_511.py (test_stdin_cwd_into_agrees_with_stdin_cwd) passed the extracted function bodies to bash as a -c argument. Git for Windows' bash.exe re-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 by json.dumps:

  • Windows path decodes to the path it encodes.
  • UNC \\server decodes pairwise.
  • Positive control: POSIX path comes back verbatim.

Red (fix reverted, harness changes kept): 12 failed, 32 passed across the 829, 447 and 511 files. The 12 are exactly the Windows and UNC cases of the new file:

FAILED tests/test_stdin_json_backslash_decode_829.py::test_windows_cwd_is_decoded[session-start]
FAILED tests/test_stdin_json_backslash_decode_829.py::test_windows_cwd_is_decoded[session-start/into]
FAILED tests/test_stdin_json_backslash_decode_829.py::test_windows_cwd_is_decoded[session-end]
FAILED tests/test_stdin_json_backslash_decode_829.py::test_windows_cwd_is_decoded[post-tool]
FAILED tests/test_stdin_json_backslash_decode_829.py::test_windows_cwd_is_decoded[user-prompt]
FAILED tests/test_stdin_json_backslash_decode_829.py::test_windows_cwd_is_decoded[user-prompt/into]
FAILED tests/test_stdin_json_backslash_decode_829.py::test_unc_prefix_decodes_pairwise[session-start]
...  (same six ids for test_unc_prefix_decodes_pairwise)

e.g. returned the JSON-escaped form 'C:\\\\work\\\\proj' instead of the path it encodes, 'C:\\work\\proj'.

Green (with the fix): 44 passed for 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*) gave 153 passed, 196 skipped. The full suite was not run locally; CI covers the matrix.

Observed vs reasoned

…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.
Copilot AI lite review requested due to automatic review settings September 28, 2026 04:35

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@fdaviddpt

Copy link
Copy Markdown
Contributor

Review: needs-fix

CI is red on all four Windows legs (pytest (windows-latest, 3.9/3.10/3.11/3.12)), 17 tests
failing per leg. main at the PR's merge-base (e8b502a) was fully green (12/12 legs), so these
are new failures introduced by this branch, not pre-existing flakes -- confirmed via
gh-branch:main:e8b502a.

Two distinct causes, both within this PR's own diff:

  1. Incomplete harness fix. The PR's own description explains that Git-for-Windows' bash.exe
    re-parses a -c command-line argument and collapses \\, so any test harness that still embeds
    an extractor function body as a -c argument (rather than piping it on stdin) will see a
    corrupted script once the body contains the new decode line. The PR fixes exactly two files this
    way (tests/test_stdin_extractor_top_level_wins_447.py, tests/test_fast_path_subshells_511.py)
    but at least two more use the same -c-argument-embedding pattern and were not touched:
    tests/test_entrypoint_sniff_bookkeeping_message_803.py and
    tests/test_previous_transcript_no_sort_691.py. Both now fail on Windows with
    syntax error: unexpected end of file from \{'` -- the same collapse the PR's own harness-change
    section already diagnoses, just not applied everywhere it needed to be.

  2. Line-pinned test broken by the diff's own line-count shift. scripts/post-tool-hook.sh
    gained 3 lines from this PR. tests/test_locale_ranges_695.py pins an exemption to a specific
    line number in that file (line 424) and now fails, quoted from the job log, not a directive:

    scripts/post-tool-hook.sh:424 no longer holds a letter range -- the exemption
    (a case pattern. Measured under tr_TR o...) has drifted off its line

    This needs the exemption's pinned line number(s) updated to match the new line count, the same
    way any other line-shifting change to that file would.

Both are mechanical fixes scoped entirely to this PR's own change; no new issue filed, this stays
on #830. Once both are addressed and Windows CI is green, this looks ready -- the decode logic
itself and the new tests/test_stdin_json_backslash_decode_829.py coverage are sound, and the
POSIX/macOS/Linux legs are unaffected.

(Note, unrelated to this PR: main's current head e994e0f has no CI run recorded at all per
gh-branch:main (19h old) -- flagging in case it is a trigger/path-filter issue worth a separate
look; not blocking this review.)

…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]>
Copilot AI review requested due to automatic review settings September 28, 2026 07:44

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@fdaviddpt
fdaviddpt merged commit a0573cd into Digital-Process-Tools:main Sep 28, 2026
13 checks passed
@fdaviddpt

Copy link
Copy Markdown
Contributor

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.

@PeterShanxin
PeterShanxin deleted the fix/829 branch September 28, 2026 10:24
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.

Hook stdin extractors never decode JSON escapes: Codex on Windows yields a doubled-backslash PROJECT_DIR and the wrong session slug

3 participants