Skip to content

fix(agent): fail closed when a blocking hook chain is cancelled - #43

Merged
alexb-openrouter merged 2 commits into
mainfrom
fix/fail-closed-aborted-blocking-hooks
Nov 30, 2026
Merged

alexb-openrouter merged 2 commits into
mainfrom
fix/fail-closed-aborted-blocking-hooks

Conversation

@alexb-openrouter

@alexb-openrouter alexb-openrouter commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

execute_handler_chain stops when context.cancel_event is set. That happens when an AsyncOutput handler's async_timeout_ms expires (the manager's on_async_timeout sets the event) or when abort_inflight() is called. The chain then returned blocked=False. The executor reads that as approval, so a later deny guard that never ran was skipped and the tool executed. UserPromptSubmit had the same problem with reject.

This ports the upstream @openrouter/agent fix (hooks-emit.ts):

if context.cancel_event.is_set():
    if behavior and behavior.block_field:   # PreToolUse, UserPromptSubmit
        blocked = True
    break

Hooks without a block field are unchanged. The executor's fallback reason is used ("Blocked by PreToolUse hook" / "Prompt rejected by hook").

HooksManager.drain() also changed. When a tracked task was already done, await asyncio.gather(...) returned without yielding to the event loop. The scheduled _cleanup callbacks could not run, so drain() looped forever at 100% CPU. This is exactly what happens when a detached timeout fires during the chain: call_model hung in teardown. drain() now removes the tasks it awaited.

Tests

tests/unit/test_hooks_abort_fail_closed.py:

  • Detached hook with a 10 ms async timeout, then a slow handler, then a deny guard. Through call_model: the guard and the tool do not run, and the model gets the block output. On main, call_model hangs in drain(). With only the chain fix, the tool would run.
  • abort_inflight() mid-chain reports PreToolUse as blocked.
  • A UserPromptSubmit chain cut short by a timeout reports blocked.
  • Controls: detached work that settles in time still runs every handler, and an aborted PostToolUse chain stays blocked=False.
  • drain() returns after the timed-out detached work.

The new emit-level tests fail on main (blocked=False). The full unit suite, ruff and mypy pass.

A PreToolUse or UserPromptSubmit chain that stopped on cancel_event (an
AsyncOutput async_timeout_ms expiring, or abort_inflight()) returned
blocked=False, so a later deny guard was skipped and the tool ran. Report
blocked=True when a hook with a block field stops early. Non-blocking hooks
are unchanged.

Also stop HooksManager.drain() from spinning forever when tracked tasks
already finished before their cleanup callbacks could run.
@devin-ai-integration

devin-ai-integration Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access that start with 'Devin'.

  • Disable automatic comment, CI, and merge conflict monitoring
@alexb-openrouter
alexb-openrouter marked this pull request as ready for review November 30, 2026 17:03
@alexb-openrouter
alexb-openrouter merged commit 61be9b7 into main Nov 30, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant