sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[FEATURE] Framework-level enforced hook priority that cannot be overridden

Open nagabharann opened this issue 10 months ago • 1 comments

Problem Statement

There is no mechanism to enforce hook execution order at the framework level. Even with a priority API, users could potentially override or circumvent security-critical hook ordering. Security hooks need guaranteed execution order that cannot be bypassed.

Proposed Solution

Provide a mechanism for "system" or "framework" level hooks with enforced priority:

  1. Reserved priority levels that user hooks cannot access
  2. A separate registration path for framework-enforced hooks
  3. Configuration to lock certain priority ranges

Use Case

For enterprise and compliance use cases:

  1. Security scanning hooks must always run first—users should not be able to register hooks that run before them
  2. Audit logging hooks must always run last—users should not be able to suppress or run after them
  3. Compliance checks must have deterministic, tamper-proof execution order

Alternatives Solutions

Without framework enforcement, we must rely on documentation and trust, which is insufficient for security-critical scenarios.

Additional Context

No response

nagabharann avatar Jan 29 '26 20:01 nagabharann

Have to think about this more but I still want to mention that hooks are guaranteed to execute in definition/insertion order. So if you have the following let's say:

from strands import Agent
from strands.plugins import Plugin, hook
from strands.hooks import BeforeToolCallEvent

class MyPlugin(Plugin):
    name = "my-plugin"

    @hook
    def op1(self, event: BeforeToolCallEvent) -> None:
        print("op1")

    @hook
    def op2(self, event: BeforeToolCallEvent) -> None:
        print("op2")

agent = Agent(plugins=[MyPlugin()])

op1 will run before op2. We do need to document this under https://strandsagents.com/docs/user-guide/concepts/agents/hooks/.

pgrayy avatar Mar 17 '26 15:03 pgrayy