Skip to content

post-code: re-stage and retry when pre-commit hooks auto-fix files instead of failing #2852

Description

@waynesun09

Problem

When a pre-commit hook auto-fixes files (e.g. gofmt, ruff format, prettier), the post-code script treats the non-zero exit code as a hard failure and blocks the PR — even though the hook already produced the correct output. The entire agent run (~11 minutes of compute) is wasted.

Example: Run 28527664156 failed on issue #2783. Every hook passed except gofmt, which auto-formatted the files:

gofmt....................................................................Failed
- hook id: go-fmt
- files were modified by this hook

The reformatted files are sitting in the working tree, ready to be staged. Instead, the script exits 1.

Root cause

post-code.sh lines 308-316 run pre-commit run --files once and exit on any failure, without checking whether the hooks auto-fixed the files:

if pre-commit run --files "${changed_array[@]}"; then
  echo "Pre-commit passed — all hooks clean"
else
  echo "::error::BLOCKED — pre-commit hooks failed on agent's changes" >&2
  exit 1
fi

Proposed fix

After a failed pre-commit run, check git diff for unstaged modifications. If hooks auto-fixed files, re-stage them, amend the commit, and retry once:

if pre-commit run --files "${changed_array[@]}"; then
  echo "Pre-commit passed — all hooks clean"
else
  if git diff --name-only | grep -q .; then
    echo "::warning::Pre-commit hooks auto-fixed files — re-staging and retrying"
    git add -u
    git commit --amend --no-edit
    if pre-commit run --files "${changed_array[@]}"; then
      echo "Pre-commit passed after auto-fix re-stage"
    else
      echo "::error::BLOCKED — pre-commit hooks still fail after auto-fix" >&2
      echo "::error::The agent's code does not pass the repo's pre-commit hooks." >&2
      echo "::error::Fix the issues and re-run, or update the pre-commit config." >&2
      exit 1
    fi
  else
    echo "::error::BLOCKED — pre-commit hooks failed on agent's changes" >&2
    echo "::error::The agent's code does not pass the repo's pre-commit hooks." >&2
    echo "::error::Fix the issues and re-run, or update the pre-commit config." >&2
    exit 1
  fi
fi

This is safe because:

  • The retry is capped at one attempt — no infinite loops
  • Only unstaged modifications from hooks are re-staged (not untracked files)
  • The secret scan and all other gates already passed before this point
  • If the second run still fails, the script exits 1 as before

Related issues

This fix is the cheap safety net: even if #1865 lands and agents learn to format, the harness should still handle auto-fixing hooks gracefully rather than discarding the work.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent/codeCode agentagent/fixFix agentcomponent/harnessAgent harness, config, and skills loadingcomponent/runnerAgent runner behavior and lifecyclepriority/highSignificant impact, address soonready-to-codeTriggers code agent dispatchtype/bugConfirmed defect in existing behavior

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions