Skip to content

[Bug]: Loading the same plugin twice duplicates its hooks, so they run twice per message #5810

Closed

Description

@enyst

I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.

Is there an existing issue for the same bug?

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

Bug Description

POST /api/conversations/{id}/load_plugin (and LocalConversation.load_plugin) loads a plugin's hooks by appending them to the conversation's hook_config. Loading the same plugin a second time (both calls answer 200) appends its hooks again, so hook_config holds two identical entries and each hook runs twice per message. The plugin's skills are not duplicated (they are merged by name). A client that retries load_plugin doubles every hook's side effects.

Expected Behavior

After a second load_plugin of the same plugin, hook_config holds one copy of each of the plugin's hooks (as for its skills), and a UserPromptSubmit hook runs once per user message.

Actual Behavior

hook_config.user_prompt_submit holds two identical entries (echo QA_PLUGIN_HOOK), and one user message produces two HookExecutionEvents for that hook. agent.agent_context.skills still lists the plugin's skill once.

Steps to Reproduce

  1. Register a local marketplace with one plugin that has a UserPromptSubmit hook on the agent's agent_context.registered_marketplaces and start a conversation.
  2. POST /api/conversations/{id}/load_plugin with {"plugin_ref": "<plugin>@<marketplace>"}: 200.
  3. Repeat the same request: 200.
  4. GET /api/conversations/{id}: hook_config.user_prompt_submit has length 2 (two identical matchers); GET ...?include_skills=true lists the skill once.
  5. Send a user message: the hook runs twice.

In Python: conv.load_plugin("p@m"); conv.load_plugin("p@m") then conv.state.hook_config.user_prompt_submit has two identical matchers.

With the verify-agent-server skill: control-agent-server map run --file F09 --only F09.load-plugin-twice --fresh reports the known-bug bullet as an expected failure (xfail) while the bug lasts.

Acceptance Criteria

  • Loading the same plugin twice leaves one copy of each of its hooks in hook_config (REST and Python SDK load_plugin), so the hook runs once per message.
  • Hooks from other sources (explicit hook_config, other plugins) are still merged as before.
  • A unit test in tests/sdk/conversation/test_local_conversation_plugins.py covers loading the same plugin twice.
  • The map bullet F09.load-plugin-twice passes with its # bug marker removed.

Installation Method

uv sync in a checkout of main

Version

main at 6a298a4

Additional Context

Root cause: LocalConversation._merge_runtime_plugin_hooks (openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py:1332) merges with HookConfig.merge([existing_config, plugin_hooks]), and HookConfig.merge (openhands-sdk/openhands/sdk/hooks/config.py:373) concatenates the matchers of every event type without de-duplicating. Skills go through plugin.add_skills_to, which merges by name, so they are not duplicated.

Suggested fix: in _merge_runtime_plugin_hooks, keep only the plugin matchers the live config does not already hold, and return early (no hook processor restart) when nothing is new.

Related: the verify-agent-server feature map (#5621).

Found by the first full agentic run of verify-agent-server; tracked in #5653.


OpenHands AI triage

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

Triage

Root cause confirmed in the repository. LocalConversation._merge_runtime_plugin_hooks (openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py) merges with HookConfig.merge([existing_config, plugin_hooks]), and HookConfig.merge (openhands-sdk/openhands/sdk/hooks/config.py) concatenates the matchers of each event type without de-duplicating. Skills do not duplicate because Plugin.add_skills_to merges by name, and MCP config merges by server key. LocalConversation.load_plugin is the single path behind both the Python SDK call and POST /api/conversations/{id}/load_plugin (the router delegates to the conversation service), so one fix covers both surfaces.

Scope: make a repeat load_plugin of the same plugin idempotent for hooks, so each of the plugin's matchers is present once and runs once per triggering event. Non-goals: changing the general concatenation semantics of HookConfig.merge for distinct configs; de-duplicating identical matchers contributed by different sources or plugins; changing skill or MCP merge behavior, which is already idempotent; changing the resolved_plugins bookkeeping.

Acceptance Criteria

  • Calling LocalConversation.load_plugin(ref) twice with the same ref leaves exactly one copy of each of that plugin's HookMatcher entries per event type in state.hook_config; for a plugin whose UserPromptSubmit hook is echo QA_PLUGIN_HOOK, state.hook_config.user_prompt_submit has length 1 after the second call (was 2).
  • POST /api/conversations/{id}/load_plugin with the same plugin_ref twice returns 200 both times, and a following GET /api/conversations/{id} reports hook_config.user_prompt_submit with length 1.
  • After the second load, one user message produces exactly one HookExecutionEvent for the plugin's UserPromptSubmit hook (was two).
  • Loading the same plugin again is a no-op: it adds no new matchers and does not re-run the plugin's SessionStart hooks.
  • Hooks from other sources are preserved: when hook_config already holds explicit hooks and/or hooks from a different plugin, a repeat load keeps those matchers and exactly one copy of the re-loaded plugin's matchers; a first load of a plugin with new hooks still installs them and restarts the hook processor. tests/sdk/conversation/test_local_conversation_plugins.py::test_load_plugin_merges_runtime_hooks_and_restarts_processor still passes.
  • A unit test in tests/sdk/conversation/test_local_conversation_plugins.py loads the same plugin twice and asserts state.hook_config holds one copy of the plugin's hook(s).
  • The verify-agent-server feature-map bullet F09.load-plugin-twice passes as a normal expectation: its trailing # bug: expected-failure annotation is removed and the run reports it as passing rather than xfail.

Activity

  1. added
    bug
    ready-for-dev
    on Oct 13, 2026
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

    bugready-for-dev

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions