TECHNOLOGY

OpenHands command hooks block with exit code 2

In this article
THE QUICK TAKE

A policy hook needs the documented blocking signal at a block-capable synchronous lifecycle stage; a failing script alone does not establish denied execution.

Diagnose a failed check that did not block

A repository hook reports a linter failure, yet the agent finishes its task. The script may be using a conventional error code instead of the hook contract. OpenHands’ SDK hooks guide says command hooks use exit 0 for success and exit 2 to block. Exit 1 and other nonzero values report nonblocking errors. Preserve that distinction when adapting an ordinary check script into an enforcing hook.

Choose a lifecycle stage that can intervene

PreToolUse, UserPromptSubmit and Stop can block their respective operation. PostToolUse and session lifecycle notifications cannot retroactively prevent an action. A Stop hook can keep the agent working when an acceptance check is missing, while a pre-tool gate concerns the proposed operation before execution. Choose the stage that matches the policy rather than attaching every check to whichever event is easiest to observe.

Keep asynchronous observation separate

The repository hooks guide says asynchronous hooks cannot block regardless of event type. They are suitable for background observations but not a synchronous enforcement decision. The guide also loads hooks from the conversation’s working folder rather than searching parent directories. Before debugging the exit code, verify that the expected hook definition was actually discovered in the backend workspace and invoked for the intended event.

Distinguish policy feedback from user approval

The security guide describes action-confirmation policies separately. A user approving a tool call and a hook rejecting an operation are different controls with different evidence. For a Stop gate, return a concrete reason so the agent knows which check or artifact remains incomplete. Avoid reporting a blocked finish as a completed task merely because the agent had already written a final-sounding message.

Test the denied path with a harmless fixture

Use a disposable failing check and observe whether the intended operation is prevented, then a passing check to establish the allowed path. Record hook discovery, exit result and subsequent execution separately. Tool observations provide that evidence chain; a task contract defines the gate’s actual condition. Keep the blocking behavior explicit when changing the wrapper around a checker. Logging failure and enforcing failure are not interchangeable outcomes.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles