Skip to content

refactor(lib): simplify the second third of hook-utils.sh - #15399

Merged
kyle-sexton merged 2 commits into
mainfrom
refactor/simplify-hook-utils-part-2
Nov 11, 2026
Merged

kyle-sexton merged 2 commits into
mainfrom
refactor/simplify-hook-utils-part-2

Conversation

@kyle-sexton

Copy link
Copy Markdown
Member

Closes #14332

Summary

Simplifies the second third of lib/hook-utils.sh (lines 1747-3492 at 42517b5) without changing its behavior, per #14332. Changed: repo_root_uncached_to, repo_relative_path_to, resolve_read_timeout_to, resolve_read_slice_to, begin, extract_bash_subject_to and emit_telemetry, with three private helpers (_rstrip_slashes_to, _strip_root_to, _micros_to). The fail-closed core of buffer_stdin_to and jq_fields_uncached is untouched.

Built by hand rather than by the issue lane: the lane's patch was refused at the 262144-byte scan cap because hook-utils.sh has 20 regenerated copies under plugins/. The hand build was agreed with the hub session.

session: Simplify Planning

Fix

  • 194 hand-changed lines (+79/-115) in one source file (cap 400 lines, 15 files).
  • scripts/sync-shared-copies.sh regenerated the 20 copies; each carrier plugin has one bump: patch fragment ("Internal simplification, no behavior change."). One fragment was renamed in a second commit because its random suffix tripped the typos check.
  • Only edge-case difference: locals in the timeout helpers now carry a __hu_ prefix, so a caller whose destination variable is named micros, whole or frac no longer collides with them. No caller uses those names.

Verification

  • A before/after pin script compared base and new output under the C and C.UTF-8 locales, on Linux and on msys with a cygpath stub, including divergent-tree cases: identical. Two deliberate breaks were caught by the pin.
  • hook-utils suite 703 pass, 1 fail that also fails at base (biome installed at a mise path on this host); secret-pattern detection suite 312 pass; sync-shared-copies.test.sh 96 pass.
  • shellcheck, check-shell-portability, sync-shared-copies.sh --check (767 copies), fragment --check and --check-bump: clean.

Related

Follow-ups

  • Pre-existing (advisory): lib/hook-utils.sh already fails shfmt under the repo .editorconfig (switch_case_indent) at base; not reformatted here.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VQb9Q3tnh28AzDYZ8AUjhH

kyle-sexton and others added 2 commits November 11, 2026 10:18
Simplify the functions that start in range 2 of 3 (lines 1747-3492 of the
5237-line file at 42517b5) without changing behavior:

- hook::repo_relative_path_to: the four trailing-slash trim loops become
  hook::_rstrip_slashes_to, the two prefix-strip-with-fold blocks become
  hook::_strip_root_to, and the Windows OSTYPE test runs once.
- hook::resolve_read_timeout_to and hook::resolve_read_slice_to share
  hook::_micros_to for the fixed-point seconds parse; the zero test is
  dropped because zero already falls under the 10 microsecond floor.
- hook::begin resolves FILE_REL in one place for --relative and a wired sink.
- hook::extract_bash_subject_to folds its three bail-outs into one.
- hook::emit_telemetry counts brackets with two substitutions instead of
  four and removes its jq temp file in one place.
- hook::repo_root_uncached_to drops a redundant assignment.

Regenerated the 20 shared copies with scripts/sync-shared-copies.sh and
added a patch fragment for each carrying plugin.

Refs: #14332

Claude-Session: https://claude.ai/code/session_01VQb9Q3tnh28AzDYZ8AUjhH
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The random suffix 4c6ba5aa contains "ba", which typos flags. Same
fragment content under a new suffix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VQb9Q3tnh28AzDYZ8AUjhH
@kyle-sexton
kyle-sexton marked this pull request as ready for review November 11, 2026 17:27
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T17:29:37.128253Z c1dbc44 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

claude-security-review reviewed c1dbc44a07ab6ae3f4e79999efdfcfa28764968f: read 41 of 41 changed file(s); 0 finding(s), 0 as inline comments.

Security review: PR #15399, refactor(lib): simplify the second third of hook-utils.sh

No security issues found.

critical-findings: 0

Scope

I read every hunk of the distinct diff: lib/hook-utils.sh, the first-path copy plugins/animation/hooks/hook-utils.sh and the first changelog fragment .changes/animation/...-4c61e5f0.md. The other 19 hook-utils.sh copies and 19 fragments match those first paths exactly (per identical.txt), so all 41 listed files are covered. Each copy sits at the same kind of path as its first path: the plugin hooks/ directories, plus the existing repo-hygiene/skills/clean/scripts/ copy. The fragments are changelog data that nothing runs. No copy's path changes what its content does. None of the changed files matches a TRUSTED PATHS glob. The PR changes no plugin manifest, userConfig key, hook registration, workflow or standing instruction. This is the first review of this PR (blocking only: no), and there are no prior findings or settled threads.

What I checked

  • hook::extract_bash_subject_to (this decides what part of a Bash command reaches telemetry). Before, a quote in a prefix token returned the bare tool name. Now the loop does break, and the merged final condition (first_token != *["']* and not an assignment shape) falls through to printf ... "$tool". So a quoted token, an assignment-shaped token (TOKEN=/a/b/secret) and an empty basename each still emit only the bare Bash. No value fragment can now reach the subject that was blocked before.
  • hook::resolve_read_timeout_to / _micros_to (the stdin read bound, a fail-closed guard). Malformed values, zero and values under the floor still fall back to 2. The zero case now goes through the floor check, since HOOK_STDIN_READ_TIMEOUT_MIN_MICROS=10 (lib/hook-utils.sh#L2183). The fractional-support fallback is unchanged. An overflow still gives a value under the floor or an unusable slice, which falls back as before.
  • hook::repo_relative_path_to / _strip_root_to / _rstrip_slashes_to (redacts absolute paths in telemetry). The prefix match, case folding (Windows only) and fallback order give the same results as before. Retrying with the caller's own spelling when the cygpath spelling matched gives the same answer, because the strings are identical in that case. The basename redaction and HOOK_REPO_RELATIVE_DEGRADED for absolute, drive-letter and UNC paths are untouched (L2056-L2063).
  • emit_telemetry. In printf ... && envelope=$(jq ...) || envelope_rc=1, a failure of either command sets envelope_rc=1. The temp file is removed on every path, and the function still returns early on failure. The brace-depth count (${#closers_deleted} - ${#openers_deleted}) works out to the same arithmetic as before.
  • begin. telemetry_enabled is now evaluated once, and the path is relativized under the same condition as before.

Spend

No spend or billing change.

No embedded instructions found in the PR context.

@github-actions

Copy link
Copy Markdown
Contributor

claude-review reviewed c1dbc44a07ab6ae3f4e79999efdfcfa28764968f: read 41 of 41 changed file(s); 0 finding(s), 0 as inline comments.

Code review: refactor(lib): simplify the second third of hook-utils.sh

No findings. I read every changed hunk: the lib/hook-utils.sh source, the first regenerated copy (plugins/animation/hooks/hook-utils.sh) and the first changelog fragment. The other 19 copies and 19 fragments are byte-identical to those. Every copy sits at a hooks/hook-utils.sh (or skills/clean/scripts/hook-utils.sh) path that sources the same library, so no copy's path changes what its content does.

I checked each rewrite against the code it replaces:

  • repo_root_uncached_to: folding the two assignments into one gives the same value.
  • _rstrip_slashes_to: the old file loop stopped at ${#p} -gt 1 and the root loop at != /. For a string that ends in /, those two stop conditions are the same, so the shared helper keeps both behaviors.
  • repo_relative_path_to: __hu_rp_fold now gets set before the empty-root check, but it is only read inside the -n "$__hu_rp_root_n" branch (lib/hook-utils.sh:2034-2051), so moving it is harmless. The old three-way elif only tried the caller's own spelling when the cygpath forms differed. When they don't differ, _strip_root_to on the _n pair repeats the _c attempt that already failed and falls through to __hu_rp_rel=$__hu_rp_file_c, which is the same result. ${2:${#pre}} matches ${file#"$pre"} whenever the case-sensitive prefix matched.
  • resolve_read_timeout_to: dropping the explicit all-zeros check is safe because HOOK_STDIN_READ_TIMEOUT_MIN_MICROS=10 (lib/hook-utils.sh:2183), so zero still falls under the floor. Truncation past six decimals and overflow handling are unchanged. The one difference: a value under the floor now short-circuits before the read_supports_fractional_timeout probe. The result is still 2, so no output changes.
  • resolve_read_slice_to: dropping -n "$__hu_slice" is safe because the regex that follows already needs at least one character.
  • begin: telemetry_enabled is a plain env test (lib/hook-utils.sh:3190-3192), so evaluating it before the path call changes nothing. The relative path is still computed once when either flag is on.
  • extract_bash_subject_to: a break on a quoted prefix token leaves the quote in first_token, so the merged check after the loop still prints the bare tool name. The other exits are the same.
  • Correlation brace-depth count: ${#corr_c} - ${#corr_o} comes to openers minus closers, which matches the old sum. The escaped classes [\{\[] / [\}\]] keep the } from ending the expansion. The decoy-session_id correlation tests (lib/hook-utils.test.sh:5037-5102) would fail if the depth count broke.
  • emit_telemetry: with printf … && envelope=$(jq …) || envelope_rc=1, a failed write or a failed jq still removes the temp file and returns 0, as before.

Closing issue #14332, acceptance criteria:

  • Behavior unchanged: consistent with my reading. The existing hook-utils.test.sh suite covers these functions (27 references to the reworked helpers). The PR's pin-script comparison and suite results are author-claimed; I did not run them, since this lane has no shell.
  • Edits only the listed file, its copies and fragments: met. The diff touches lib/hook-utils.sh, the 20 regenerated copies and 20 bump: patch fragments.
  • Within 400 changed lines and 15 hand-edited files: met (+79/−115 in one source file).
  • Only functions in range 2 of 3 changed: every changed hunk is between base lines 1868 and 3466, inside the stated 1747–3492 range.
  • sync-shared-copies.sh run and copies committed: the copies in the diff match the source change.
  • ci-status green: not something this lane can check.

No plugin.json or marketplace.json changes, so no manifest lines apply. The bump: patch fragments fit a change with no behavior difference. No paved-path, settled-thread or prior-finding entries apply (this lane has reviewed 0 earlier heads).

@kyle-sexton
kyle-sexton added this pull request to the merge queue Nov 11, 2026
Merged via the queue into main with commit b373af0 Nov 11, 2026
87 of 88 checks passed
@kyle-sexton
kyle-sexton deleted the refactor/simplify-hook-utils-part-2 branch November 11, 2026 17:51
@github-actions github-actions Bot added the merge-path: owner-agent Post-merge accounting record (README.md, Merge paths) label Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-path: owner-agent Post-merge accounting record (README.md, Merge paths)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simplify the hook-utils.sh shared lib source (part 2 of 3)

1 participant