Skip to content

Bug: Plugin hooks cannot modify context due to ignored return value #537

@HamsteRider-m

Description

@HamsteRider-m

Bug: Plugin hooks cannot modify context due to ignored return value

Description

The plugin hook system in agent_loop.py calls _hook('agent_before', locals()) but does not handle the return value. This prevents plugins from modifying the agent context (e.g., system_prompt), making the hook mechanism non-functional for context modification use cases.

Current Behavior

When a plugin registers an agent_before hook and returns a modified context:

# plugins/example_plugin.py
from plugins import hooks

@hooks.register('agent_before')
def modify_context(ctx):
    ctx['system_prompt'] += '\n\n[Additional Context]\n...'
    return ctx

The modified system_prompt is ignored because agent_loop.py does not use the return value:

# agent_loop.py (current)
_hook('agent_before', locals())  # Return value is discarded

Expected Behavior

Plugins should be able to modify the agent context by returning an updated dictionary from their hook handlers. The hook system should apply these modifications.

Steps to Reproduce

  1. Create a plugin that modifies system_prompt in an agent_before hook
  2. Register the plugin using @hooks.register('agent_before')
  3. Run an agent session
  4. Observe that the system_prompt modifications are not applied

Root Cause

In agent_loop.py line 49 (commit 3b7e6ab), the hook is called but the return value is not captured or processed:

def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema, 
                      max_turns=40, verbose=True, initial_user_content=None, yield_info=False):
    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": initial_user_content if initial_user_content is not None else user_input}
    ]
    turn = 0;  handler.max_turns = max_turns
    _hook('agent_before', locals())  # ❌ Return value ignored
    while turn < handler.max_turns:
        # ...

Impact

  • Plugins cannot modify agent context (system prompt, user input, etc.)
  • The hook mechanism is only useful for read-only operations (logging, metrics)
  • Limits the extensibility of the plugin system

Environment

  • GenericAgent version: main branch (commit 3b7e6ab and later)
  • Python version: 3.x
  • OS: macOS / Linux / Windows

Proposed Solution

See PR #XXX for a fix that:

  1. Captures the hook return value
  2. Applies modifications to the context when a dict is returned
  3. Maintains backward compatibility with hooks that don't return values

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions