Skip to content

fix(verify_commits.sh): replay auto commits in isolated worktrees, surface errors in PR comment - #26

Merged
jcommelin merged 2 commits into
masterfrom
verify-commits-fixes
Apr 27, 2026
Merged

jcommelin merged 2 commits into
masterfrom
verify-commits-fixes

Conversation

@bryangingechen

Copy link
Copy Markdown
Contributor

This fixes a few bugs in verify_commits.sh:

  • Per-commit replay shared the main checkout, so any untracked file an earlier auto commit's command produced (e.g., from a > hits.txt redirect) blocked the next commit's git checkout --detach $parent with "untracked working tree files would be overwritten". The "fix" to earlier commit N+1 then reported a confusing checkout error instead of its real failure.

  • JSON output was hand-rolled with echo "{ \"...\": \"$subject\" }" and only escaped quotes. Subjects containing literal newlines (created by some commit-message generators) produced unparseable JSON, which then caused the comment-rendering step to fail silently with jq: parse error: Invalid escape.

  • The comment surfaced no actual error output. Authors saw "❌ Failed" and had to dig through CI logs to learn what went wrong.

This rewrite:

  • Replays each auto commit in a fresh git worktree add --detach $parent under a single mktemp'd parent. Worktrees are removed after each iteration, on EXIT (trap), and git worktree prune runs at startup so any stale state from interrupted runs is cleared. The same isolation applies to the transient-commit cherry-pick replay.

  • Captures combined stdout+stderr via tee so live CI logs still show the command's output while we also save it for the JSON report.

  • Builds JSON via jq -nc --arg ... so commit subjects with arbitrary bytes (newlines, backslashes, control chars) round-trip safely.

  • Records a failure_kind (command_failed, timed_out, or tree_mismatch) plus the last 4KB / 40 lines of output, and for tree mismatches a truncated git diff-tree --stat. The summary script dispatches on failure_kind (no keyword matching on error text — the underlying tool's message is the diagnosis) and renders each failed commit as a <details> block containing the actual output.

  • Caps total comment size at 50KB, falling back to "see CI logs" links for late-truncated content.

  • Replaces the literal ✅/❌ escape strings (which only rendered correctly inside jq output) with real UTF-8 ✅/❌.

  • Drops the bash-4 mapfile and associative-array dependencies; both scripts now run on bash 3.2+, easing local testing.

  • Recovers from unparseable JSON: if verify_commits.sh crashes before writing the JSON file, the summary script emits a fallback comment pointing to the CI logs rather than producing empty output.

Smoke-tested against the failing run for leanprover-community/mathlib4#38480. The new comment shows the actual bash parse error / xargs / sed / git rm output for each commit independently, with no cascading "checkout would overwrite" noise.

Prepared with Claude code.

bryangingechen and others added 2 commits April 26, 2026 23:05
…PR comment

The commit verification script had several bugs that combined to produce
unhelpful PR comments and cascading false-positive failures:

- Per-commit replay shared the main checkout, so any untracked file an
  earlier auto commit's command produced (e.g., from a `> hits.txt`
  redirect) blocked the next commit's `git checkout --detach $parent`
  with "untracked working tree files would be overwritten". The "fix" to
  earlier commit N+1 then reported a confusing checkout error instead of
  its real failure.

- JSON output was hand-rolled with `echo "{ \"...\": \"$subject\" }"` and
  only escaped quotes. Subjects containing literal newlines (created by
  some commit-message generators) produced unparseable JSON, which then
  caused the comment-rendering step to fail silently with `jq: parse
  error: Invalid escape`.

- The comment surfaced no actual error output. Authors saw "❌ Failed"
  and had to dig through CI logs to learn what went wrong.

This rewrite:

- Replays each auto commit in a fresh `git worktree add --detach $parent`
  under a single mktemp'd parent. Worktrees are removed after each
  iteration, on EXIT (trap), and `git worktree prune` runs at startup so
  any stale state from interrupted runs is cleared. The same isolation
  applies to the transient-commit cherry-pick replay.

- Captures combined stdout+stderr via `tee` so live CI logs still show
  the command's output while we also save it for the JSON report.

- Builds JSON via `jq -nc --arg ...` so commit subjects with arbitrary
  bytes (newlines, backslashes, control chars) round-trip safely.

- Records a `failure_kind` (`command_failed`, `timed_out`, or
  `tree_mismatch`) plus the last 4KB / 40 lines of output, and for tree
  mismatches a truncated `git diff-tree --stat`. The summary script
  dispatches on `failure_kind` (no keyword matching on error text — the
  underlying tool's message is the diagnosis) and renders each failed
  commit as a `<details>` block containing the actual output.

- Caps total comment size at 50KB, falling back to "see CI logs" links
  for late-truncated content.

- Replaces the literal `✅`/`❌` escape strings (which only
  rendered correctly inside `jq` output) with real UTF-8 ✅/❌.

- Drops the bash-4 `mapfile` and associative-array dependencies; both
  scripts now run on bash 3.2+, easing local testing.

- Recovers from unparseable JSON: if `verify_commits.sh` crashes before
  writing the JSON file, the summary script emits a fallback comment
  pointing to the CI logs rather than producing empty output.

Smoke-tested against the failing run for leanprover-community/mathlib4#38480.
The new comment shows the actual bash parse error / xargs / sed / git rm
output for each commit independently, with no cascading "checkout would
overwrite" noise.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Hard-coded 600s makes the timeout-handling path effectively unreachable
from test harnesses that build a synthetic git history and need a quick
artificial timeout. Use the same `${VAR:-default}` pattern already used
for TRANSIENT_PREFIX et al., so a test scenario can set
TIMEOUT_SECONDS=1 and exercise the timed_out failure_kind path in
seconds.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@jcommelin
jcommelin merged commit c5cdcc9 into master Apr 27, 2026
@bryangingechen
bryangingechen deleted the verify-commits-fixes branch April 27, 2026 10:15
kim-em pushed a commit to kim-em/mathlib-ci that referenced this pull request May 22, 2026
…rface errors in PR comment (leanprover-community#26)

* fix: replay auto commits in isolated worktrees and surface errors in PR comment

The commit verification script had several bugs that combined to produce
unhelpful PR comments and cascading false-positive failures:

- Per-commit replay shared the main checkout, so any untracked file an
  earlier auto commit's command produced (e.g., from a `> hits.txt`
  redirect) blocked the next commit's `git checkout --detach $parent`
  with "untracked working tree files would be overwritten". The "fix" to
  earlier commit N+1 then reported a confusing checkout error instead of
  its real failure.

- JSON output was hand-rolled with `echo "{ \"...\": \"$subject\" }"` and
  only escaped quotes. Subjects containing literal newlines (created by
  some commit-message generators) produced unparseable JSON, which then
  caused the comment-rendering step to fail silently with `jq: parse
  error: Invalid escape`.

- The comment surfaced no actual error output. Authors saw "❌ Failed"
  and had to dig through CI logs to learn what went wrong.

This rewrite:

- Replays each auto commit in a fresh `git worktree add --detach $parent`
  under a single mktemp'd parent. Worktrees are removed after each
  iteration, on EXIT (trap), and `git worktree prune` runs at startup so
  any stale state from interrupted runs is cleared. The same isolation
  applies to the transient-commit cherry-pick replay.

- Captures combined stdout+stderr via `tee` so live CI logs still show
  the command's output while we also save it for the JSON report.

- Builds JSON via `jq -nc --arg ...` so commit subjects with arbitrary
  bytes (newlines, backslashes, control chars) round-trip safely.

- Records a `failure_kind` (`command_failed`, `timed_out`, or
  `tree_mismatch`) plus the last 4KB / 40 lines of output, and for tree
  mismatches a truncated `git diff-tree --stat`. The summary script
  dispatches on `failure_kind` (no keyword matching on error text — the
  underlying tool's message is the diagnosis) and renders each failed
  commit as a `<details>` block containing the actual output.

- Caps total comment size at 50KB, falling back to "see CI logs" links
  for late-truncated content.

- Replaces the literal `✅`/`❌` escape strings (which only
  rendered correctly inside `jq` output) with real UTF-8 ✅/❌.

- Drops the bash-4 `mapfile` and associative-array dependencies; both
  scripts now run on bash 3.2+, easing local testing.

- Recovers from unparseable JSON: if `verify_commits.sh` crashes before
  writing the JSON file, the summary script emits a fallback comment
  pointing to the CI logs rather than producing empty output.

Smoke-tested against the failing run for leanprover-community/mathlib4#38480.
The new comment shows the actual bash parse error / xargs / sed / git rm
output for each commit independently, with no cascading "checkout would
overwrite" noise.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* fix: allow TIMEOUT_SECONDS to be overridden via environment variable

Hard-coded 600s makes the timeout-handling path effectively unreachable
from test harnesses that build a synthetic git history and need a quick
artificial timeout. Use the same `${VAR:-default}` pattern already used
for TRANSIENT_PREFIX et al., so a test scenario can set
TIMEOUT_SECONDS=1 and exercise the timed_out failure_kind path in
seconds.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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.

2 participants