Skip to content

fix(sdk): treat a non-string hook decision as no decision - #4773

Merged
neubig merged 5 commits into
OpenHands:mainfrom
alanhuangyoo:fix/hook-non-string-decision
Sep 24, 2026
Merged

neubig merged 5 commits into
OpenHands:mainfrom
alanhuangyoo:fix/hook-non-string-decision

Conversation

@alanhuangyoo

@alanhuangyoo alanhuangyoo commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:

Ran a hook printing {"decision": null} and one printing a number; both now come back as no decision with the real exit code instead of a synthetic -1.


AGENT:

Why

A command hook that exits 0 and prints {"decision": null} came back as a failure:

HookResult(success=False, exit_code=-1,
           error="Hook execution failed: 'NoneType' object has no attribute 'lower'")

JSON null parses to None and .lower() was called on it. Only JSONDecodeError is caught around that block, so the AttributeError reached the outer handler, which discarded the captured subprocess result and reported a synthetic exit -1 for a process that had exited 0.

It is not specific to null — any non-string fails the same way:

decision null     success=False  exit=-1   error=Hook execution failed: 'NoneType' object has no attribute 'lower'
decision allow    success=True   exit=0    decision=HookDecision.ALLOW
decision number   success=False  exit=-1   error=Hook execution failed: 'int' object has no attribute 'lower'

Summary

  • Check for a string before lowering, so any other shape means the hook expressed no decision rather than a failed run. The agent-hook path at executor.py:416 already goes through str() for the same reason; this is the sibling that did not.
  • Three tests: null, a number, and that string decisions still parse.

I left the except json.JSONDecodeError as it is. Widening it would hide the next bug of this kind rather than fix it; the point here is not to raise.

Issue Number

Closes #4766

How to Test

uv run python -c "
import tempfile
from openhands.sdk.hooks.config import HookDefinition
from openhands.sdk.hooks.executor import HookExecutor
from openhands.sdk.hooks.types import HookEvent, HookEventType

ex = HookExecutor(working_dir=tempfile.mkdtemp())
ev = HookEvent(event_type=HookEventType.PRE_TOOL_USE, tool_name='BashTool',
               tool_input={}, session_id='s')
for payload in ('{\"decision\": null}', '{\"decision\": \"allow\"}', '{\"decision\": 1}'):
    r = ex.execute(HookDefinition(command=f\"echo '{payload}'\"), ev)
    print(payload, '->', r.success, r.exit_code, r.decision, r.error or '')
"

On this branch all three come back success=True exit=0, with HookDecision.ALLOW only for the string.

uv run pytest tests/sdk/hooks/ -q
# 146 passed

uv run ruff check && uv run ruff format --check
# clean

Two of the three new tests fail on main:

assert result.success
E   assert False
E    +  where False = HookResult(success=False, exit_code=-1, ...,
        error="Hook execution failed: 'int' object has no attribute 'lower'").success

Video/Screenshots

Not applicable — no GUI surface; the console output above is the evidence.

Design Doc

Not added; the change is one type check.

A command hook that exits 0 and prints {"decision": null} came back as a
failure:

    HookResult(success=False, exit_code=-1,
               error="Hook execution failed: 'NoneType' object has no attribute 'lower'")

JSON null parses to None and .lower() was called on it. Only
JSONDecodeError is caught around that block, so the AttributeError
reached the outer handler, which discarded the captured subprocess result
and reported a synthetic exit -1 for a process that had exited 0.

It is not specific to null: {"decision": 1} fails the same way with
'int' object has no attribute 'lower'.

Check for a string instead, so any other shape means the hook expressed
no decision. The agent-hook path already went through str() for the same
reason.

Closes OpenHands#4766
@all-hands-bot

Copy link
Copy Markdown
Collaborator

👋 This PR needs a couple of things fixed before OpenHands can review it:

  • the PR description's HUMAN: section needs at least 20 characters describing what you tested, not just the template placeholder

Push an update once this is addressed and this check re-runs automatically.

This is an automated check - no AI was used to generate this comment.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

🚦 CI is currently failing on this PR's latest commit.

Please fix the failing checks before OpenHands reviews it - this is re-checked automatically once you push a new commit. (A maintainer can also request @all-hands-bot as a reviewer to have it reviewed regardless of CI status.)

This is an automated check - no AI was used to generate this comment.

@neubig neubig added the openhands-review-canary Run the Agent Canvas review-extension canary label Sep 22, 2026
@all-hands-bot

Copy link
Copy Markdown
Collaborator

⏳ Review waiting on checks

The current head 1be3a67aaf37 still has checks that have not finished, so no review conversation was started:

  • integration-test

No action is needed. The scheduled scan retries once every check on the head reports a conclusion.

This is an automated check - no AI was used to generate this comment.

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

Scope

This is an SDK bug fix in openhands-sdk/openhands/sdk/hooks/executor.py, which this repository owns. No product/architecture decision is needed.

What I verified

  • Root cause is real. On the base commit, a command hook that exits 0 and emits {"decision": null} (or any non-string) raised AttributeError on .lower(); only JSONDecodeError was caught, so the outer except Exception discarded the captured subprocess result and returned success=False, exit_code=-1.
  • Fix behaves as claimed on head (1be3a67a). Running the PR's repro shows {"decision": null} and {"decision": 1} now return success=True, exit_code=0, decision=None, while {"decision": "allow"} / "deny" still map to HookDecision.ALLOW / HookDecision.DENY (deny still sets blocked=True).
  • Tests. The 3 new tests pass; the full tests/sdk/hooks/ suite on head is 145 passed / 1 failed, and that one failure (TestAsyncHookExecution::test_execute_async_hook_process_tracked) reproduces identically on the base commit 6a1e4d0f — a pre-existing flake unrelated to this diff. The extra async failures I saw in one local run did not reproduce on the base or in CI and are likewise unrelated.
  • CI. All check runs on head 1be3a67a are success/skipped (53 runs), including sdk-tests, pre-commit, coverage-report, windows-tests, and integration-test.

Notes (non-blocking)

_parse_decision (agent/prompt hooks) uses str(data.get("decision", "")).lower(), so null there becomes "None", an invalid decision that falls open with success=False. The command path now treats the same payload as success=True, decision=None. That asymmetry predates this PR and is consistent with the command path's existing handling of a missing decision key, so it does not block this change — but it is worth keeping in mind if HookResult.success semantics are ever unified across hook types.

No material issues found. The change is minimal, matches the command-hook exit-code contract documented on HookResult, and preserves decision parsing.

✅ APPROVED

@all-hands-bot all-hands-bot removed the openhands-review-canary Run the Agent Canvas review-extension canary label Sep 22, 2026
Co-authored-by: openhands <openhands@all-hands.dev>

@neubig neubig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was submitted by an AI agent (OpenHands) on behalf of @neubig.

The issue is valid and the implementation is the narrowest correct fix: command-hook decision is optional, non-string values now mean no decision, and the actual subprocess success, exit code, stdout, and stderr are preserved. Existing allow/deny behavior remains unchanged.

I also simplified the redundant historical commentary in commit 9aea74cd; no behavior or assertions changed. Focused verification on the updated head passed: 58 hook-executor tests, Ruff lint, Ruff formatting, and git diff --check.

Approved for merge.

@neubig
neubig enabled auto-merge (squash) September 23, 2026 03:05
@alanhuangyoo

Copy link
Copy Markdown
Contributor Author

Thanks @neubig. The red pre-commit isn't the change: the baseline check runs git show <base.sha>:scripts/forbidden_dynamic_attributes_baseline.json, and this PR's recorded base is still 6a1e4d0 (v1.46.0), from before that file existed. Merging main into the branch doesn't move base.sha, so any PR opened before the baseline landed will hit the same thing. Retargeting the base should refresh it, or the check could skip when the file is missing at the ref.

@neubig
neubig merged commit b874a47 into OpenHands:main Sep 24, 2026
39 checks passed
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.

[Bug]: HookExecutor converts exit-0 hooks with decision: null into execution failures

3 participants