Skip to content

fix(134): keep the bytes of a non-ASCII repository path when install writes its hook files - #142

Merged
cogc-factory[bot] merged 1 commit into
mainfrom
fix/134-nonascii-hook-scripts
Oct 13, 2026
Merged

cogc-factory[bot] merged 1 commit into
mainfrom
fix/134-nonascii-hook-scripts

Conversation

@cogc-factory

@cogc-factory cogc-factory Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #134

What changes

install --hooks native crashed with UnicodeEncodeError writing its files when the repository path is non-ASCII under an ASCII locale: git's path arrives escaped (#135) and a strict UTF-8 write cannot encode the escapes. The files are now written as os.fsencode(text) and read back as os.fsdecode(bytes), so the bytes on disk are the path's own bytes and a rerun compares equal. No spec change (triaged on the issue).

Proof

Acceptance

  • Install in a non-ASCII path under an ASCII locale writes the files, with the path's bytes in previous, and a rerun says "already installed": test_install_writes_and_recognises_its_hooks_in_a_repository_whose_path_has_a_non_ascii_character_under_an_ascii_locale
  • A hook manager refused after a native install still names the previous directory: test_the_refusal_names_the_previous_directory_in_a_repository_whose_path_has_a_non_ascii_character_under_an_ascii_locale

Demo (scratch repository /tmp/demo134/dé)

$ LC_ALL=C PYTHONUTF8=0 PYTHONCOERCECLOCALE=0 prepush-ci install
installed: /tmp/demo134/d??/.git/prepush-ci/hooks
$ prepush-ci install   # again, same environment
already installed: /tmp/demo134/d??/.git/prepush-ci/hooks
$ ls .git/prepush-ci/hooks
commit-msg  pre-push  previous  python

(d?? is how the ASCII terminal shows the é, as in #115.) On main the first command ends in a traceback.

Check: CI on 1a944c7: 1264 passed (Linux, 3.14), 100% branch coverage of core, 99% overall (run); no spec changed, so no spec validation ran.

Details for the reviewer

Mutants, each seen failing: reading the hook files strictly (install_spec: rerun), reading the previous record strictly in _previous (install_spec), in _lost_hooks (the second spec above), and writing with write_text (install_spec). Round 1: the two acceptance specs now run for a UTF-8 name (dé) and for a name that is not UTF-8 (d + byte 0xE9, skipped on Windows); _recorded_text no longer checks UTF-8, and a corrupt previous record (#119) is told apart by naming no absolute directory (_recorded_directory), with a new text case next to bytes and directory in the unreadable-record spec. Mutants seen failing: the absolute check, the strict read, and the _previous call; swapping the _lost_hooks call to the old form fails nothing, as it is equivalent for any record install writes. Hook-manager files name no path and are untouched. Only the install and install-managers specs were run locally (129 passed); the full suite runs on GitHub.

— chuck-norris

@cogc-factory cogc-factory Bot added the turn:build chuck-norris's move: implement or fix label Oct 13, 2026
@cogc-factory
cogc-factory Bot marked this pull request as ready for review October 13, 2026 16:03
@cogc-factory
cogc-factory Bot requested a review from k-sok as a code owner October 13, 2026 16:03
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  src/prepush_ci
  install_command.py
Project Total  

This report was generated by python-coverage-comment-action

@cogc-factory cogc-factory Bot mentioned this pull request Oct 13, 2026
@cogc-factory cogc-factory Bot added turn:review robocop's move: review the implementation and removed turn:build chuck-norris's move: implement or fix labels Oct 13, 2026
@cogc-factory

cogc-factory Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Factory ledger

What the loops spent on this so far, summed from the loop runtime's records. Cost is at API prices; the loops run on a subscription, so it measures plan usage, not a bill.

Loop Model Wakes Time Output tokens Cost at API prices
chuck-norris sonnet-5-5 2 27 min 31k $1.13
robocop opus-5-5 2 20 min 21k $1.32
terminator sonnet-5-5 1 9 min 23k $0.70
Total 5 56 min 75k $3.15

Updated automatically.

@cogc-factory

cogc-factory Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Verdict: REJECT

Reviewed head 510f259 (code round 1). Only its own commit was reviewed: git range-diff fdc4e1c^! baf73f2^! shows baf73f2 is identical to #135 as merged (fdc4e1c). The PR merges cleanly with main.

Findings

  1. src/prepush_ci/install_command.py:277-283 (_recorded_text): the reader rejects a record this same change writes. run now writes previous as os.fsencode(text), which is the raw bytes of the repository path. _recorded_text then requires those bytes to be valid UTF-8. A repository path with a non-ASCII byte that is not UTF-8 (d\351, i.e. dé in Latin-1, as unpacked from an old archive) therefore installs once, and every rerun after that refuses with cannot read …/d\udce9/…/previous: it is not valid UTF-8. This happens under LC_ALL=C and under C.utf8 alike (transcript below). The rerun refuses the tool's own file, so the expected behaviour install --hooks native: UnicodeEncodeError writing hook scripts when the repository path is non-ASCII under an ASCII locale #134 states ("a rerun says already installed") and install spec line 99 do not hold for this non-ASCII path. The message also shows a surrogate escape (\udce9) instead of the path. _lost_hooks catches the same ValueError and silently drops the previous directory from the manager refusal.
    What would be right: every record install writes reads back as what it wrote, and install --hooks native: traceback when the recorded previous hooks directory cannot be read #119's corrupt record (b"\xff\xfe", install_spec.py:843) is still a one-line error. That means either telling a corrupt record apart by something other than UTF-8 validity, or refusing up front, in one line that names the path, when the repository path is not UTF-8. If choosing between those needs a spec decision, send it to turn:spec. Add a test for the case either way.
    On main the first install already crashes for this path. The rerun refusal of its own record, though, exists only in this PR's code, so under LOOP.md it belongs here and not in a new issue.

Information only: ruff's structural check flags _install_manager (C901, 11 > 10), which this PR does not touch. Not a finding.

What holds: both acceptance tests exist, and both fail on main with the head's test files copied in, then pass on the head. No test was removed or weakened (the diff only adds test lines). The proof's demo reproduces.

Evidence

Required checks on 510f259: all pass (check 3.11, 3.14, windows, coverage, proof, ci-passed).

Local full check (sh scripts/ci in /tmp/review-prepush-ci-142-510f259):

505 passed in 1.00s
==> all tests: 95% branch coverage overall
1213 passed in 498.17s (0:08:18)
Totals: 12 passed, 0 failed (12 items)
==> full check passed

The proof's demo, dé stored as UTF-8 under LC_ALL=C PYTHONUTF8=0 PYTHONCOERCECLOCALE=0, on the head:

$ install
installed: /tmp/rc142/h1/d??/.git/prepush-ci/hooks
$ install again
already installed: /tmp/rc142/h1/d??/.git/prepush-ci/hooks
commit-msg  pre-push  previous  python

Finding 1, a path with the byte \351 (not UTF-8). The same output under LC_ALL=C and LC_ALL=C.utf8:

$ install
installed: /tmp/rc142/h2/d?/.git/prepush-ci/hooks
$ install again
prepush-ci install: cannot read /tmp/rc142/h2/d\udce9/.git/prepush-ci/hooks/previous: it is not valid UTF-8
$ od -c .git/prepush-ci/hooks/previous
/   t   m   p   /   r   c   1   4   2   /   h   2   /   d 351   /   .   g   i   t   /   h   o   o   k   s  \n

(main: UnicodeEncodeError … surrogates not allowed on the first install.)

Reproduce it: mkdir "$(printf 'd\351')"; cd "$(printf 'd\351')"; git init -q; printf 'check = "true"\n' > .prepush-ci.toml; prepush-ci install --hooks native; prepush-ci install --hooks native

— robocop

@cogc-factory
cogc-factory Bot force-pushed the fix/134-nonascii-hook-scripts branch from 510f259 to a7ed97a Compare October 13, 2026 16:45
@cogc-factory cogc-factory Bot added turn:build chuck-norris's move: implement or fix and removed turn:review robocop's move: review the implementation labels Oct 13, 2026
@cogc-factory

cogc-factory Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Note: chuck-norris rebased to a7ed97a at 16:43, a minute before my verdict above on 510f259. git range-diff 510f259~1..510f259 a7ed97a~1..a7ed97a shows 1: 510f259 = 1: a7ed97a, so the commit is identical and the REJECT and finding 1 stand for a7ed97a.

— robocop

…writes its hook files

`install --hooks native` crashed with UnicodeEncodeError when it wrote the
`previous` record of a repository whose path the locale cannot encode: after
the fix for #130 the path arrives escaped (`os.fsdecode`), and a strict
UTF-8 write cannot encode the escapes.

Decisions:
- The files are written as `os.fsencode(text)` and read back as
  `os.fsdecode(bytes)`, so the bytes on disk are the bytes of the path and a
  rerun compares equal ("already installed" instead of "updated" forever).
- The reads go through one helper, `_recorded_text`, with no UTF-8 check: a path
  that is not UTF-8 is a path. A corrupt `previous` record (#119) is told apart
  by naming no absolute directory, in `_recorded_directory`.
- Hook-manager files (`_read`, `write_text`) name no path and are untouched.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Loop: chuck-norris
@cogc-factory

cogc-factory Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Round 1, finding 1: fixed in 1a944c7 (folded into the fix commit). _recorded_text no longer demands UTF-8, so a path with the byte 0xE9 reads back as what install wrote and the rerun says "already installed"; the manager refusal names the previous directory again. A corrupt record is now told apart by naming no absolute directory (_recorded_directory), so the #119 case b"\xff\xfe" is still the one-line error (plus a new text case). The two acceptance specs run for dé and for d\xe9 (skipped on Windows). Chose reading over refusing up front: no spec decision needed, and such a path works. CI green on 1a944c7.

— chuck-norris

@cogc-factory cogc-factory Bot added turn:review robocop's move: review the implementation and removed turn:build chuck-norris's move: implement or fix labels Oct 13, 2026
@cogc-factory

cogc-factory Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Verdict: APPROVE

Reviewed head 1a944c7 (code round 2). Round 1 rejected 510f259, which was identical to a7ed97a.

Findings

None.

  • Round-1 finding 1 is fixed. The fix is folded into the fix commit, so this is no "address review" commit. _recorded_text decodes with os.fsdecode alone. _recorded_directory treats a record that names no absolute directory as corrupt. That fits because every path install records goes through _resolve, which makes it absolute (_previous, _hooks_path).
  • Tests: the two acceptance specs now run for dé and for d\xe9 (the not-utf8 case is skipped on Windows, with its reason). The install --hooks native: traceback when the recorded previous hooks directory cannot be read #119 parametrisation gains a text case. The only removed test line is that parametrize, which was widened. The three new cases fail against the round-1 source (510f259, with the head's test files copied in) and pass on the head.
  • Information only: ruff's structural check flags _install_manager (C901, 11 > 10), which this PR does not touch. In a path that is not UTF-8, refusals on stderr show the name as d\udce9. That comes from Python's backslashreplace on stderr and is not a finding here.

Evidence

Required checks on 1a944c7: all pass (check 3.11, 3.14, check-windows, coverage, proof, ci-passed).

Local full check (sh scripts/ci in /tmp/review-prepush-ci-142-1a944c7):

505 passed in 2.56s
1264 passed in 499.05s (0:08:19)
Totals: 13 passed, 0 failed (13 items)
==> full check passed

Install and then rerun install --hooks native, with PYTHONUTF8=0 PYTHONCOERCECLOCALE=0:

dé (UTF-8)   LC_ALL=C       installed … / already installed …   exit 0 / 0
dé (UTF-8)   LC_ALL=C.utf8  installed … / already installed …   exit 0 / 0
d\351        LC_ALL=C       installed … / already installed …   exit 0 / 0   (round 1: "it is not valid UTF-8")
d\351        LC_ALL=C.utf8  installed … / already installed …   exit 0 / 0

In d\351 under LC_ALL=C, the hooks work end to end and corrupt records are still refused:

$ PREPUSH_CI=full git commit -q -m one; git log -1 --format=%B
one

Prepush-CI: full
$ git push -q origin main   → rc=0, remote main at 4f140f2
$ printf '\377\376' > .git/prepush-ci/hooks/previous; prepush-ci install --hooks native
prepush-ci install: cannot read …/previous: it does not name a directory      rc=1
$ echo "no directory" > .git/prepush-ci/hooks/previous; prepush-ci install --hooks native
prepush-ci install: cannot read …/previous: it does not name a directory      rc=1

Mutant check (the head's tests against the source of 510f259):

FAILED install_spec.py::test_a_rerun_says_so_…_record_of_the_previous_directory_is_unreadable[text]
FAILED install_spec.py::test_install_writes_and_recognises_its_hooks_…[not-utf8]
FAILED install_managers_spec.py::test_the_refusal_names_the_previous_directory_…[not-utf8]
3 failed, 4 passed

→ turn:attack

— robocop

@cogc-factory cogc-factory Bot added turn:attack terminator's move: try to break the implementation and removed turn:review robocop's move: review the implementation labels Oct 13, 2026
@cogc-factory

cogc-factory Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Attack: PASS

Head: 1a944c7 · 8 hypotheses · 0 broken

# Hypothesis Result
1 install --hooks native, rerun, trailer commit + push, verify, run, doctor in dé, Latin-1 d\xe9, sp ace é, and a name with a newline, under LC_ALL=C PYTHONUTF8=0 PYTHONCOERCECLOCALE=0 holds: installs, reruns "already installed", the previous bytes are the path's own, the check runs on push, doctor passes. main (d695b20) tracebacks on all four.
2 Hooks written under a UTF-8 locale, rerun in the C and in the UTF-8 locale holds: the same bytes compare equal (the only change is the interpreter path of my two builds)
3 A path with ', $HOME, a backtick, a backslash and é holds: install, rerun, push and doctor all work
4 Previous hooks directory with a non-ASCII name (core.hooksPath), C and UTF-8 locale holds: installs, reports "hooks kept from", the previous pre-push still runs on push, and so does the check. main tracebacks under C.
5 Linked worktree, bare repository and --separate-git-dir repository in non-ASCII paths holds: main tracebacks on the last two
6 Hook managers (pre-commit, lefthook, husky) and interactive init in a non-ASCII path under the C locale holds: no traceback (init on main tracebacks)
7 Interrupted runs: empty previous, truncated pre-push, invalid bytes in commit-msg holds: a truncated or garbled hook is rewritten; an empty or relative previous is refused in one line naming the file (main treated an empty one as the current directory and rewrote it), as the new text case in the spec says
8 Merge with main (now with #140) and the other turn:human pull request (#153) clean (git merge-tree)
Reproduction scripts

/tmp/atk142/h1.sh … h4.sh on the attacker's workstation; each is SHA=<sha> bash hN.sh, using fresh repositories under /tmp/sweep/atk142*. Core of #1:

mkdir -p /tmp/t/remote.git "/tmp/t/dé"; git init -q --bare -b main /tmp/t/remote.git
cd "/tmp/t/dé" && git init -q -b main && git remote add origin /tmp/t/remote.git
echo 'check = "true"' > .prepush-ci.toml && git add . && git commit -qm init && git push -q origin main
export LC_ALL=C PYTHONUTF8=0 PYTHONCOERCECLOCALE=0
prepush-ci install --hooks native; prepush-ci install   # installed, then already installed
PREPUSH_CI=full git commit -q --allow-empty -m x && git push origin main && prepush-ci doctor

— terminator

@cogc-factory
cogc-factory Bot enabled auto-merge (rebase) October 13, 2026 18:05
@cogc-factory cogc-factory Bot added the turn:human Ready to merge: the human's move label Oct 13, 2026
@cogc-factory cogc-factory Bot removed the turn:attack terminator's move: try to break the implementation label Oct 13, 2026
@cogc-factory
cogc-factory Bot merged commit 5c093c0 into main Oct 13, 2026
12 checks passed
@cogc-factory
cogc-factory Bot deleted the fix/134-nonascii-hook-scripts branch October 13, 2026 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

turn:human Ready to merge: the human's move

Projects

None yet

Development

Successfully merging this pull request may close these issues.

install --hooks native: UnicodeEncodeError writing hook scripts when the repository path is non-ASCII under an ASCII locale

1 participant