Skip to content

[Bug]: AGENT hook evaluator runs ambient project-plugin hooks, although hook_config=None is commented as disabling hooks #6013

Description

@u00dxk2

Is there an existing issue for the same bug?

  • I have searched existing issues and this is not a duplicate.

My keyword searches of this repo's issues and PRs found no title naming this mechanism (I didn't read every body), and I could see no published security advisories (queries and counts at the end).

Bug Description

An AGENT-type hook's evaluator conversation runs the hooks of an ambient plugin found in its working directory. I saw it two ways:

  • Calling the evaluator directly: within that test the evaluator was the only conversation created, and a project plugin's SessionStart hook ran inside it.
  • Through an ordinary conversation with an AGENT Stop hook: the plugin's SessionStart hook ran in my conversation (expected, feat: auto-load installed and local plugins into conversations #3846), and then again in a second conversation.

The evaluator is built with hooks meant to be off (openhands-sdk/openhands/sdk/hooks/executor.py:269-274 at 75105bd4, the commit I last ran on):

# hook_config=None disables hooks in the sub-conversation (no recursion)
conversation = LocalConversation(
    agent=agent,
    workspace=self.working_dir,
    plugins=None,
    hook_config=None,

But ambient plugin loading runs regardless of plugins=None (local_conversation.py:1132-1165 at 75105bd), and when hook_config is None the plugin hooks become the hook config, and run_session_start() is called (local_conversation.py:1270-1308). So the evaluator gets the plugin's hooks.

To be fair to the current code: ambient loading is deliberate (#3846), and the constructor documents that plugin hooks are combined with hook_config (local_conversation.py:273-274), so hook_config=None does not disable plugin hooks at either commit I ran on. It's the evaluator's comment that doesn't hold. plugin/discovery.py:6-7 describes the ambient set as auto-loading into interactive conversations, and the evaluator isn't one, so I don't think this was intended.

Steps to Reproduce

  1. Check out 75105bd4, the commit I last ran on. (I first reproduced it at 6e7a8532; line numbers in local_conversation.py differ there.)
  2. Save the test below as tests/sdk/hooks/test_repro_extract.py.
  3. uv sync --dev, then uv run pytest tests/sdk/hooks/test_repro_extract.py -q -rA -s (the exact flags I used are under Environment)

The test plants a project plugin at <work_dir>/.openhands/plugins/aop-probe whose SessionStart command hook appends its stdin (the hook event JSON) to a marker file, then calls the AGENT-hook evaluator directly with the scripted TestLLM. Ambient discovery also looks in the supported user-plugin directories and at enabled installed plugins, so plugins of yours found there may load during the test as well.

import json
from pathlib import Path

from openhands.sdk.hooks.config import HookDefinition, HookType
from openhands.sdk.hooks.executor import HookExecutor
from openhands.sdk.hooks.types import HookEvent, HookEventType
from openhands.sdk.llm import Message, TextContent
from openhands.sdk.testing import TestLLM
from tests.command_utils import python_command

VERDICT = '{"decision": "allow", "reason": "ok"}'


def _scripted_llm(n: int = 4) -> TestLLM:
    return TestLLM.from_messages(
        [Message(role="assistant", content=[TextContent(text=VERDICT)])] * n
    )


def _plant_plugin(work_dir: Path, marker: Path) -> None:
    hooks_dir = work_dir / ".openhands" / "plugins" / "aop-probe" / "hooks"
    hooks_dir.mkdir(parents=True)
    script = (
        "import sys, pathlib; "
        f"pathlib.Path({str(marker)!r}).open('a', encoding='utf-8')"
        ".write(sys.stdin.read().replace(chr(10), ' ') + chr(10))"
    )
    config = {
        "SessionStart": [
            {
                "matcher": "*",
                "hooks": [{"type": "command", "command": python_command(script)}],
            }
        ]
    }
    (hooks_dir / "hooks.json").write_text(json.dumps(config), encoding="utf-8")


def _run_evaluator(work_dir: Path):
    executor = HookExecutor(working_dir=str(work_dir), llm=_scripted_llm())
    hook = HookDefinition(type=HookType.AGENT, system_prompt="Evaluate.")
    event = HookEvent(event_type=HookEventType.STOP, session_id="parent-session")
    return executor._execute_agent_hook(hook, event)


def _marker_lines(marker: Path) -> list[str]:
    if not marker.exists():
        return []
    return [ln for ln in marker.read_text(encoding="utf-8").splitlines() if ln]


def test_bug_evaluator_does_not_run_plugin_hooks(tmp_path):
    work_dir = tmp_path / "ws"
    work_dir.mkdir()
    marker = tmp_path / "marker.txt"
    _plant_plugin(work_dir, marker)

    result = _run_evaluator(work_dir)

    lines = _marker_lines(marker)
    print("AOP result (plugin planted):", result.decision, result.reason, result.error)
    print("AOP marker lines (evaluator):", lines)
    assert lines == [], f"evaluator ran a plugin hook {len(lines)} time(s): {lines}"

This is a standalone, shorter version of the bug test in a longer file (below). I ran exactly this text as tests/sdk/hooks/test_repro_extract.py at 75105bd4; its output is below.

Expected Behavior

No plugin hook runs inside the evaluator: the marker file stays empty.

Actual Behavior

Selected lines from running the test above on its own (temp-folder prefix shortened to <tmp>, and pytest's tests\sdk\hooks\test_repro_extract.py prefix removed from the first line):

AOP result (plugin planted): HookDecision.ALLOW ok None
E   AssertionError: evaluator ran a plugin hook 1 time(s): ['{"event_type":"SessionStart","tool_name":null,"tool_input":null,"tool_response":null,"message":null,"session_id":"4266d8af-e45b-45ca-8b37-c2415f04855c","working_dir":"<tmp>\\\\test_bug_evaluator_does_not_ru0\\\\ws","metadata":{}}']
============================== 1 failed in 1.39s ==============================

The marker's session id is the evaluator conversation's own: the log shows Created new conversation 4266d8af-e45b-45ca-8b37-c2415f04855c, then Loaded hooks from <tmp>\test_bug_evaluator_does_not_ru0\ws\.openhands\plugins\aop-probe\hooks\hooks.json. It is not the synthetic parent-session id of the triggering event. working_dir is the evaluator's workspace.

The longer file (six tests; I can post it in a comment if it helps) wraps HookExecutor._execute_agent_hook to record each call, its result, and the id of every LocalConversation built during it. With that, both bug tests first check that the evaluator ran once and returned a parsed allow with no error, that each stray marker is a SessionStart event whose session id is one of the evaluator's own conversations, and that its working_dir is the evaluator's workspace; they then fail only on the marker itself. Its run at 75105bd4: 2 failed, 4 passed in 3.19s.

The ordinary-conversation case in that file: a LocalConversation whose hook_config has one Stop matcher holding an AGENT hook, then send_message("Say done.") and run(). The marker file held exactly one line from my conversation's session id and one from a second session id, and the wrapper recorded that second id as the conversation built inside the evaluator call.

Controls in that file, all passing: the planted directory loads as plugin aop-probe with hooks; an ordinary conversation runs the hook (the #3846 behaviour); with no plugin planted, the evaluator reaches a parsed allow and writes no marker; an ordinary conversation with no AGENT hook makes no evaluator call and writes only its own marker.

Acceptance Criteria

Suggested direction (a probe, not a finished patch)

To check the cause, I let the evaluator opt out of ambient plugins: a private constructor flag _load_ambient_plugins: bool = True on LocalConversation; when it's False, ambient_plugins = {} is used instead of calling load_available_plugins(...); HookExecutor._execute_agent_hook passes _load_ambient_plugins=False. With that change the reproduction tests pass (at 75105bd4: 7 passed across both files), and reverting it makes them fail again (3 failed, 4 passed). (At 6e7a8532, tests/sdk/hooks/test_executor.py was not green on my machine either way: two tests failed with the change, three after reverting it, two of them in both runs. I didn't look into why, and didn't run it at 75105bd4.)

That change also keeps ambient plugins' skills and MCP servers out of the evaluator, not only their hooks, which seems right for an evaluator but is your call. A narrower alternative would keep the evaluator's final hook config at None. I'm happy to open a PR in whichever shape you prefer.

Not claimed

My runs show one extra SessionStart command running inside an evaluator. They don't show a changed policy verdict (the evaluator still returned its scripted allow), a security boundary being crossed, recursion, user-level or installed plugins (I planted only a project plugin), the agent-server path, or a count that holds across configurations. One question from reading the code only, not tested: if an ambient plugin carried an AGENT-type Stop hook, what stops the evaluator's own finish from starting another evaluator?

Environment

  • Installation Method: source checkout, uv sync --dev --frozen --python 3.13
  • SDK Version: v1.54.0 (banner), commit 75105bd4 (first reproduced at 6e7a8532). Main has since moved to f5f849f5 (v1.55.0); executor.py, local_conversation.py and plugin/discovery.py are unchanged between the two, but I haven't re-run there, so I've left "LATEST version" unticked.
  • Python Version: 3.13.14 (pytest 9.0.3)
  • Model Name: none (scripted TestLLM)
  • Operating System: Windows (all my runs were on Windows; I don't have Linux set up on this machine to try)
  • Notes: the install was not sealed: it used my machine's uv package cache, and installed litellm 1.93.0 from it. Tests ran as uv run --no-sync --offline pytest tests/sdk/hooks/test_repro_extract.py -p no:cacheprovider -q -rA -s, with proxy variables pointed at a dead port and UV_OFFLINE=1.

Searches I ran (2026-10-15)

gh search issues --include-prs in this repo, capped at 50 results per query: agent hook plugin 0, hook evaluator plugins 0, evaluator SessionStart 0, hook recursion 0, sub-conversation hooks 0, agent hook recursion 0, project plugins hooks sub-conversation 0, plugin hooks agent-type hook 0, evaluator ambient 0, agent hook ambient 0, hook_config None plugin 0, evaluator plugin hooks run 0; agent hook evaluator 1 (#3052, which added AGENT hooks); _execute_agent_hook 2 (#3052, #4359); ambient plugins 6 (incl. #3846, and #5628, a different bug about plugins on resume); hook_config=None 23 and plugins=None 50 (hit the cap), none of whose titles is about this. Published repository security advisories visible to me: 0.


OpenHands AI triage

The following comments and acceptance criteria were added by the OpenHands AI agent.

Triage

Confirmed, self-contained bug in the SDK's AGENT-hook evaluator; it asks for no new user-facing behavior. HookExecutor._execute_agent_hook builds its evaluator LocalConversation with plugins=None and hook_config=None, commented as disabling hooks in the sub-conversation (openhands-sdk/openhands/sdk/hooks/executor.py:269-274). LocalConversation._ensure_plugins_loaded ignores plugins=None when it builds the ambient plugin set, merges the ambient plugins' hooks into the final hook config, and then calls run_session_start() (openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py:1132-1165, 1270-1308), and close() calls run_session_end() (:2838-2840). A project plugin found in the evaluator's working directory therefore runs its SessionStart hook inside the evaluator and its SessionEnd hook on close. plugin/discovery.py:6-7 describes the ambient set as auto-loading into interactive conversations; the evaluator is a programmatic, non-interactive judge built from an explicit tool list, so the isolation its constructor signals is not met.

Scope: the AGENT-hook evaluator must run no ambient plugin hooks, for every hook event, and ordinary conversations must keep the ambient auto-load behavior added in #3846.

Priority: priority:low. The report is a real, executed reproduction on real SDK code paths, but it records no user-visible impact: the reporter's own "Not claimed" list confirms no changed policy verdict, no crossed security boundary, no recursion, and no agent-server path, and the observable effect is one extra hook command running in an internal evaluator conversation. No support, customer, or production evidence is recorded, so this does not meet the bar for priority:medium/priority:high.

Non-goals: (1) whether ambient plugin skills, MCP servers, and file-based agents should also stay out of the evaluator. The reporter deferred this ("your call"), and the constructor's plugins=None is the only signal in-tree; the criteria below are satisfied by either a hooks-only suppression or a full ambient-plugin opt-out, so this is left to review. Note for the implementer that the narrower shape still lets ambient plugin MCP servers (with ${VAR} secret expansion) and skills reach an LLM-driven, security-relevant judge. (2) Other internal sub-conversations that also load ambient plugins (openhands-tools/openhands/tools/delegate/impl.py, openhands-tools/openhands/tools/task/manager.py). (3) Changing ambient auto-load for ordinary conversations. (4) The pre-existing, unrelated tests/sdk/hooks/test_executor.py failures the reporter saw at 6e7a8532.

Acceptance Criteria

  • With an ambient project plugin whose SessionStart and SessionEnd command hooks append to a marker file, an AGENT hook evaluator invoked through HookExecutor._execute_agent_hook leaves the marker file empty, and the evaluator still returns its parsed allow/deny verdict with error is None.
  • No plugin hook runs inside the evaluator for any hook event: no SessionStart at evaluator start, and no SessionEnd when the executor closes the evaluator conversation in its finally block.
  • An ambient plugin that defines an AGENT-type Stop hook does not make the evaluator's own finish start a second evaluator conversation: exactly one evaluator conversation is created per triggering hook event.
  • The behavior holds for every ambient source the discovery path reads (project .openhands/plugins / .agents/plugins, user-level plugin directories, and enabled installed plugins), not only the project directory used in the reproducer.
  • Ordinary conversations are unchanged: they still auto-load ambient plugins and run their SessionStart/SessionEnd hooks (feat: auto-load installed and local plugins into conversations #3846), including a skills-only ambient plugin still updating the agent.
  • Automated validation: the reproducer from this issue fails on the pre-change commit and passes after the change, and uv run pytest tests/sdk/hooks/ tests/sdk/conversation/test_local_conversation_plugins.py passes.
  • Documentation effect: any inline comment or docstring claiming hook_config=None disables hooks in the evaluator sub-conversation matches the shipped behavior; if the intended behavior is instead that plugin hooks may run there, that comment is corrected rather than left contradicting the code.
  • Compatibility: ambient plugin auto-load remains the default for ordinary conversations, and the public Conversation/LocalConversation API defaults are unchanged for callers that do not opt out of ambient plugins.
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

    ready-for-dev

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions