Skip to content

[FEATURE] HookRegistry.add_callback should return a cleanup function for unregistering #2206

@opieter-aws

Description

@opieter-aws

Problem Statement

add_callback() in registry.py returns None. Once a callback is registered, there is no mechanism to unregister it. TypeScript's addCallback() returns a HookCleanup function that removes the callback when called.

Use Case

Dynamic hook management during agent lifetime:

  • Temporarily adding a logging hook for debugging, then removing it
  • Plugins that register hooks on init and clean up on teardown
  • Test fixtures that add hooks and need to clean up

Proposed Solution

Return a cleanup callable from add_callback():

def add_callback(self, event_type, callback):
    self._callbacks[event_type].append(callback)
    def cleanup():
        self._callbacks[event_type].remove(callback)
    return cleanup

Additional context

This was a finding from an agentic cross-SDK consistency scan between Python and TypeScript SDKs (strands-agents/sdk-typescript#789). The finding has been triaged, but verify that this needs an actual fix.

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