Skip to content

fix(git-hooks): keep unstaged bytes out of pre-commit formatting - #137723

Merged
steipete merged 2 commits into
mainfrom
steipete/peaceful-babbage-a7949d
Sep 4, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/peaceful-babbage-a7949d

Conversation

@steipete

@steipete steipete commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where committing with partial staging (hunk-level git apply --cached / git add -p) would silently include unstaged working-tree bytes in the commit. The pre-commit formatter selected staged files from the index, formatted the working-tree copies, and then git added the complete working-tree contents of every staged non-ignored file — including files not eligible for formatting at all. Deliberately-unstaged hunks (observed live: an unstaged import hunk during a Vitest fixture commit), unstaged edits to staged .txt/.json files, and even unstaged working-tree deletions were committed without the author asking for them.

Why This Change Was Made

Partial staging is an ordinary Git workflow; a formatting hook must never widen the commit beyond what the author staged. The fix repairs the formatter at its index owner:

  • Fully staged files (working tree byte-identical to the index) keep the existing fast batch path: oxfmt --write plus restage — now scoped to format-eligible files only.
  • Partially staged files are formatted index-only: staged blob through oxfmt --stdin-filepath=<path> (verified live to apply the same repo config and ignorePatterns as batch mode), then git hash-object --no-filters and one git update-index --index-info call. Working-tree bytes are never touched; staged file modes are preserved; symlinks and conflict stages are skipped.
  • Non-format staged files are no longer restaged at all.
  • Failure safety: a formatter that exits nonzero aborts before any index write, and a formatter that exits 0 with empty output for nonempty input fails the hook instead of staging an emptied file.
  • Sequencer early-exit, the pre/post-format secret-literal scans, and filename/option-injection protections are unchanged. Empty-array expansions are now guarded for macOS Bash 3.2 under set -u (latent pre-existing hazard).

User Impact

Maintainers and contributors using partial staging get exactly their staged hunks committed (formatted), with unstaged work preserved byte-for-byte in the working tree. Fully-staged commits behave and perform exactly as before. Unstaged working-tree deletions of a staged file no longer silently commit the deletion.

Evidence

  • Reproduced pre-fix in a disposable fixture repo with the real hook chain and real oxfmt: a deliberately-unstaged import hunk landed in the commit and the unstaged diff was destroyed. Post-fix, the same scenario commits only the formatted staged hunk and preserves the working tree.
  • Fixture scenarios verified post-fix: partial staging, fully staged batch path, unstaged worktree deletion, dirty non-format file, executable-mode partial staging; all paths also smoke-tested under /bin/bash 3.2.57.
  • test/git-hooks-pre-commit.test.ts and test/git-hooks-pre-commit-boundaries.test.ts: 70/70 pass. Nine new/rewritten regression tests fail on the pre-fix script for the intended reasons (verified by temporarily restoring the old script).
  • node scripts/check-changed.mjs -- <touched files> exit 0; Codex autoreview scoped-clean on this exact diff.
  • Production LOC: +85/-8 (all in scripts/pre-commit/format-staged.sh; the growth is the index-only formatting path — the old 6-line tail was the bug). Tests: +180/-47.

@steipete
steipete marked this pull request as ready for review September 4, 2026 00:51
@steipete
steipete requested a review from a team as a code owner September 4, 2026 00:51
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation app: web-ui App: web-ui gateway Gateway runtime scripts Repository scripts agents Agent runtime and tooling extensions: ollama size: XL maintainer Maintainer-authored PR labels Sep 4, 2026
format-staged.sh formatted working-tree files and then git-added the
complete working-tree contents of every staged file, so partial staging
(hunk-level git apply --cached) silently committed unstaged work, and
dirty non-format files were restaged wholesale.

Fully staged files keep the fast batch oxfmt --write + restage path, now
scoped to format-eligible files only. Partially staged files are formatted
index-only: the staged blob goes through oxfmt --stdin-filepath (same repo
config and ignorePatterns as batch mode, verified live), then
git hash-object --no-filters and git update-index --index-info, preserving
the staged mode and never touching working-tree bytes. Non-format staged
files are no longer restaged at all; staged deletions, unstaged working-tree
deletions, symlinks, and executable modes are preserved, and a formatter
that exits 0 with empty output for nonempty input fails the hook instead of
staging an emptied file.

Security scans (pre/post-format literal scan), filename protections, and
the sequencer early-exit are unchanged. Regression tests fail on the
pre-fix script; the guard/boundary suites now protect the staged-bytes-only
invariant instead of the old restage behavior.
@steipete
steipete force-pushed the steipete/peaceful-babbage-a7949d branch from 1656bd8 to 3af07a4 Compare September 4, 2026 00:52
@openclaw-barnacle openclaw-barnacle Bot added size: M and removed docs Improvements or additions to documentation app: web-ui App: web-ui gateway Gateway runtime agents Agent runtime and tooling extensions: ollama size: XL labels Sep 4, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Sep 4, 2026
@clawsweeper

clawsweeper Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed September 3, 2026, 9:18 PM ET / September 4, 2026, 01:18 UTC.

ClawSweeper review

What this changes

The PR formats partially staged, format-eligible files from their staged blobs and updates only Git’s index, preserving unstaged working-tree bytes.

Regression provenance

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

⛔ Blocked before merge - 1 item remains

No blocking correctness finding remains. The prior literal-pathspec finding is fixed on the exact head, and current main still has the broad restage behavior this PR repairs; the member-authored PR should remain open for normal exact-head CI and maintainer handling.

Priority: P2
Reviewed head: e7d77cfa0ed742a9f84d3a98329160e2b82efae3

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-boundary repair with substantial behavioral regression coverage; no remaining actionable defect was found.
Proof confidence 🌊 off-meta tidepool Not applicable: Repository state identifies the author as a MEMBER, so the external-contributor proof gate does not apply; the PR body additionally records an after-fix disposable-repository run, while source review confirms the exercised hook boundary.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: Repository state identifies the author as a MEMBER, so the external-contributor proof gate does not apply; the PR body additionally records an after-fix disposable-repository run, while source review confirms the exercised hook boundary.
Evidence reviewed 5 items Current-main defect: The current-main version collects every non-ignored staged path, formats only eligible files, then runs git add on the complete non-ignored set; a dirty staged file can therefore have its working-tree contents restaged.
Repair at the index owner: The PR identifies files differing from the index, formats their staged blob through stdin, hashes the resulting unfiltered blob, and applies index-info updates without writing the working tree.
Git command contract: Installed Git 2.55 documentation states that plain git diff compares working tree to index, --no-filters hashes bytes as-is, and update-index --index-info -z accepts NUL-delimited index records.
Findings None None.
Security None None.

How this fits together

The pre-commit hook receives staged paths, filters files eligible for formatting, then prepares Git’s index for the commit. The new branch separates clean worktree files from files with unstaged changes so a commit cannot absorb the latter’s bytes.

flowchart LR
  A[Staged paths] --> B[Format eligibility filter]
  B --> C[Compare working tree to index]
  C --> D[Fully staged files]
  C --> E[Partially staged files]
  D --> F[Format worktree and restage]
  E --> G[Format staged blob and update index]
  F --> H[Commit index]
  G --> H
Loading

Before merge

  • Resolve merge risk (P1) - The bug fix adds 79 net production lines for a distinct index-only formatting flow; the extra flow is warranted because Git must read the staged blob and update the index without touching the working tree, but exact-head CI should retain the broad fixture coverage.
Agent review details

Security

None.

PR surface

Tests +149, Other +79. Total +228 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 0 0 0 0
Tests 3 196 47 +149
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 87 8 +79
Total 4 283 55 +228

Review metrics

Metric Value Why it matters
Production versus test LOC production +87/-8 (net +79); tests and support +196/-47 (net +149) The production growth is the dedicated staged-blob/index-update path needed to preserve partial staging; regression coverage grows more than the implementation.

Merge-risk options

Maintainer options:

  1. Retain the index-only compatibility path (recommended)
    Merge after exact-head checks confirm the hook preserves staged/index semantics across supported Git and formatter environments.

Technical review

Best possible solution:

Keep the separate index-only branch: no net-neutral restage-based shape can format a partial staged snapshot without reading or overwriting unstaged working-tree bytes.

Do we have a high-confidence way to reproduce the issue?

Yes in source: stage a format-eligible file, modify it without staging the change, and commit through the hook; current main’s broad git add can absorb those working-tree bytes. This review did not execute the fixture because the checkout is inspection-only.

Is this the best way to solve the issue?

Yes. The repair removes the broad restage at the index-owning hook and uses Git’s documented working-tree/index comparison plus explicit blob/index interfaces; a stash or downstream guard would leave the owner boundary unsafe.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 3c0eb9fd822f.

Labels

Label changes:

  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: Repository state identifies the author as a MEMBER, so the external-contributor proof gate does not apply; the PR body additionally records an after-fix disposable-repository run, while source review confirms the exercised hook boundary.
  • remove rating: 🦐 gold shrimp: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: ⏳ waiting on author: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P2: This fixes a meaningful but bounded developer commit-workflow correctness issue.
  • merge-risk: 🚨 compatibility: The hook changes how existing partially staged repositories are formatted and indexed during commits.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: Repository state identifies the author as a MEMBER, so the external-contributor proof gate does not apply; the PR body additionally records an after-fix disposable-repository run, while source review confirms the exercised hook boundary.

Evidence

What I checked:

  • Current-main defect: The current-main version collects every non-ignored staged path, formats only eligible files, then runs git add on the complete non-ignored set; a dirty staged file can therefore have its working-tree contents restaged. (scripts/pre-commit/format-staged.sh:43, 3c0eb9fd822f)
  • Repair at the index owner: The PR identifies files differing from the index, formats their staged blob through stdin, hashes the resulting unfiltered blob, and applies index-info updates without writing the working tree. (scripts/pre-commit/format-staged.sh:65, e7d77cfa0ed7)
  • Git command contract: Installed Git 2.55 documentation states that plain git diff compares working tree to index, --no-filters hashes bytes as-is, and update-index --index-info -z accepts NUL-delimited index records.
  • Boundary regression coverage: The added tests cover formatted staged blobs, preserved dirty worktrees, deleted worktree copies, symlinks, empty formatter output, BOM names, and literal pathspec names; these are behavior-level fixture tests around the hook chain. (test/git-hooks-pre-commit.test.ts:228, e7d77cfa0ed7)
  • Area history: The current hook and its tests have recent maintenance by Peter Steinberger, including the prior private-hook guard repair and both commits in this PR; the latter commit directly addresses the prior ClawSweeper pathspec finding. (scripts/pre-commit/format-staged.sh:68, e7d77cfa0ed7)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (1 earlier review cycle)
  • reviewed 2026-09-04T01:02:32.807Z sha 3af07a4 :: blocked before merge. :: [P1] Treat recovered filenames as literal Git pathspecs

Enumerated staged filenames re-enter Git as pathspecs in the new
partial-state diff, staged-entry lookup, and restage add. Production runs
under the guard's GIT_LITERAL_PATHSPECS env, but the script now pins
--literal-pathspecs itself so bracket or ":(...)" magic filenames cannot
misclassify partial staging when invoked without that env. Regression
covers direct invocation with magic names; it fails without the flags.
@steipete

steipete commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the ClawSweeper review at head e7d77cf:

  • Treat recovered filenames as literal Git pathspecs (P1) — applied. The partial-state diff, the staged-entry ls-files lookup, and the restage add now pin --literal-pathspecs on the command itself, so bracket and :(...) magic filenames stay literal regardless of caller env. (Production already ran under the guard's GIT_LITERAL_PATHSPECS=1 spawn env — which is why the chosen[2].ts boundary test passed — but the script is now self-defending for standalone invocation.)
  • Rank-up: regression for magic filenames — added: treats recovered filenames as literal pathspecs even without the guard env invokes scripts/pre-commit/format-staged.sh directly (no guard env) with partially staged chosen[3].ts and :(exclude)partial.ts, asserting staged-byte boundary and worktree preservation. Verified to fail on the unhardened script (expected 'unstaged only' to be 'keep staged').
  • Production LOC — +90/−8, all in the hook script; the growth is the index-only formatting capability itself (the old 6-line restage tail was the bug). Attempted smaller shapes are documented in the PR body/review artifact: all-blob formatting rejected on ~160 ms/file oxfmt spawn cost, stash-based approaches rejected on interruption safety.

Suites: 71/71 at this head; Codex autoreview scoped-clean on the full branch diff.

@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Sep 4, 2026
@steipete
steipete merged commit 68fec85 into main Sep 4, 2026
231 of 235 checks passed
@steipete
steipete deleted the steipete/peaceful-babbage-a7949d branch September 4, 2026 01:34
@steipete

steipete commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

vincentkoc added a commit that referenced this pull request Sep 4, 2026
* origin/main:
  fix(git-hooks): keep unstaged bytes out of pre-commit formatting (#137723)
  test(gateway): drain session tool fixture cleanup (#137743)
  fix: avoid duplicate errors when a desktop disconnects (#137724)
  refactor: streamline temporary mention retention (#137714)
  fix(gateway): prevent hosted lifecycle crashes and self-waits (#137456)
  test(daemon): make managerless port fixture hermetic (#137692)
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 4, 2026
…nclaw#137723)

* fix(git-hooks): keep unstaged bytes out of pre-commit formatting

format-staged.sh formatted working-tree files and then git-added the
complete working-tree contents of every staged file, so partial staging
(hunk-level git apply --cached) silently committed unstaged work, and
dirty non-format files were restaged wholesale.

Fully staged files keep the fast batch oxfmt --write + restage path, now
scoped to format-eligible files only. Partially staged files are formatted
index-only: the staged blob goes through oxfmt --stdin-filepath (same repo
config and ignorePatterns as batch mode, verified live), then
git hash-object --no-filters and git update-index --index-info, preserving
the staged mode and never touching working-tree bytes. Non-format staged
files are no longer restaged at all; staged deletions, unstaged working-tree
deletions, symlinks, and executable modes are preserved, and a formatter
that exits 0 with empty output for nonempty input fails the hook instead of
staging an emptied file.

Security scans (pre/post-format literal scan), filename protections, and
the sequencer early-exit are unchanged. Regression tests fail on the
pre-fix script; the guard/boundary suites now protect the staged-bytes-only
invariant instead of the old restage behavior.

* fix(git-hooks): pin literal pathspecs in formatter Git lookups

Enumerated staged filenames re-enter Git as pathspecs in the new
partial-state diff, staged-entry lookup, and restage add. Production runs
under the guard's GIT_LITERAL_PATHSPECS env, but the script now pins
--literal-pathspecs itself so bracket or ":(...)" magic filenames cannot
misclassify partial staging when invoked without that env. Regression
covers direct invocation with magic names; it fails without the flags.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. scripts Repository scripts size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant