Add OpenHands stop hook for pre-commit checks#21
Conversation
Add .openhands/hooks directory with on_stop.sh hook that runs pre-commit before allowing the agent to finish. This ensures code quality by blocking agent completion when pre-commit checks fail. Files added: - .openhands/hooks.json - Hook configuration - .openhands/hooks/on_stop.sh - Pre-commit hook script Co-authored-by: openhands <openhands@all-hands.dev>
|
🚀 Deploy Preview PR Created/Updated A deploy preview has been created/updated for this PR. Deploy PR: https://github.com/OpenHands/deploy/pull/3598 Once the deploy PR's CI passes, the automation service will be deployed to the feature environment. |
all-hands-bot
left a comment
There was a problem hiding this comment.
🟡 Acceptable - Code is clean and pragmatic, but where's the proof it works?
The implementation is straightforward and doesn't over-engineer the problem. However, the PR description talks about how it works without showing that it actually works. See inline comments.
| # -------------------------- | ||
| >&2 echo "=== Running pre-commit run --all-files ===" | ||
| if command -v uv &> /dev/null; then | ||
| PRECOMMIT_OUTPUT=$(uv run pre-commit run --all-files 2>&1) |
There was a problem hiding this comment.
🟡 Suggestion - Error Handling: What happens if pre-commit isn't installed at all (neither via uv nor standalone)?
The command will fail and PRECOMMIT_EXIT will be non-zero, causing the hook to block with a message about "pre-commit checks failed" - but the real issue is that pre-commit isn't available. The agent might waste time trying to "fix" non-existent issues.
Consider adding an explicit check:
if ! command -v uv &> /dev/null && ! command -v pre-commit &> /dev/null; then
>&2 echo "⚠️ Neither 'uv' nor 'pre-commit' found in PATH"
log_issue "## Pre-commit Not Found\n\nCannot run pre-commit checks. Please install pre-commit or uv."
fiThis makes the failure mode explicit rather than cryptic.
| { | ||
| "type": "command", | ||
| "command": ".openhands/hooks/on_stop.sh", | ||
| "timeout": 300 |
There was a problem hiding this comment.
🟢 Acceptable - Timeout: 300 seconds (5 minutes) for pre-commit is generous but reasonable for large repos or slow CI environments.
If this becomes a bottleneck in practice, consider making it configurable or running --files on changed files only (though --all-files is the safer default for agent work).
Summary
Adds an OpenHands stop hook that runs pre-commit checks before allowing the agent to finish. This ensures code quality by blocking agent completion when pre-commit checks fail.
Changes
.openhands/hooks.json- Hook configuration that registers theon_stop.shhook to run when the agent attempts to stop/finish.openhands/hooks/on_stop.sh- Shell script that:uv run pre-commit run --all-filesHow it works
When an agent tries to finish its task, the hook intercepts the stop action and:
pre-commit run --all-filesusinguv(falls back to directpre-commitif uv unavailable){"decision": "allow"}and allows the agent to complete{"decision": "deny", "additionalContext": "<error details>"}with exit code 2, blocking the agent and providing feedbackFuture enhancements
This is the first of potentially more hooks. Additional capabilities we may add later (inspired by the SDK repo):