[PORT] BeforeToolsEvent + AfterToolsEvent batch hooks
Source SDK
strands-ts
Target SDK
strands-py
Related Files
strands-ts/src/hooks/events.ts
strands-ts/src/hooks/index.ts
strands-ts/src/hooks/__tests__/events.test.ts
strands-ts/src/agent/agent.ts
strands-ts/src/types/agent.ts
strands-ts/src/index.ts
strands-ts/src/__fixtures__/mock-plugin.ts
strands-ts/src/agent/__tests__/agent.test.ts
strands-ts/src/agent/__tests__/agent.hook.test.ts
strands-ts/src/agent/__tests__/agent.interrupt.test.ts
strands-ts/src/agent/__tests__/agent-delegation.test.ts
site/src/content/docs/user-guide/concepts/agents/hooks.mdx
site/src/content/docs/user-guide/concepts/interrupts.mdx
site/src/content/docs/user-guide/concepts/streaming/async-iterators.mdx
site/src/content/docs/user-guide/concepts/streaming/index.mdx
site/src/content/docs/user-guide/concepts/tools/executors.mdx
What needs to be migrated
TypeScript has a batch-level BeforeToolsEvent hook that fires once before any tools in a batch execute; Python only has per-tool BeforeToolCallEvent. The BeforeToolsEvent gives hooks a single point to inspect the full set of requested tool calls and cancel the entire batch, and supports interrupts for human-in-the-loop approval of tool batches.
Event definition (strands-ts/src/hooks/events.ts:733)
• BeforeToolsEvent extends HookableEvent and implements Interruptible.
• Readonly fields: agent (:736), message (assistant-role Message containing all ToolUseBlocks, :737), invocationState (:738).
• Mutable control field: cancel: boolean | string = false (:746) — when true, all tools are cancelled with a default error message; when string, that string is used as the error text in each ToolResultBlock.
• Does NOT use reverse callback ordering (:710 returns false).
• Implements interrupt() method (:755) for human-in-the-loop workflows — halts the agent and stores pending tool execution state for resume.
Agent loop integration (strands-ts/src/agent/agent.ts:2192-2228)
• executeTools() constructs and yields BeforeToolsEvent before any tool execution (:2192-2195).
• On interrupt error, stores pending tool execution state for resume before re-throwing (:2196-2203).
• After yielding, checks beforeToolsEvent.cancel and agent.isCancelled (:2220-2225): if either is truthy, produces error ToolResultBlocks for every tool in the batch without executing any, then falls through to AfterToolsEvent.
Interrupt support (strands-ts/src/agent/__tests__/agent.interrupt.test.ts:197-614)
• Hooks can call event.interrupt({ name, reason }) to halt execution before any tools run.
• Multiple hooks can each raise separate interrupts on the same event (:507).
• On resume, the agent replays from the stored pending state — tools execute normally on the next cycle (:598).
• Interrupt ID format: hook:beforeTools:<name>.
Python landing points
• strands-py/src/strands/hooks/events.py: add BeforeToolsEvent dataclass with message: Message, invocation_state: dict[str, Any], mutable cancel: bool | str = False, and _Interruptible mixin. Writable field: cancel.
• strands-py/src/strands/hooks/__init__.py: export the new event, add to __all__.
• strands-py/src/strands/event_loop/event_loop.py (around :795): dispatch BeforeToolsEvent before tool_executor._execute(); if cancel is truthy or agent cancel signal is set, produce error ToolResults for every ToolUse without executing, then skip to AfterToolsEvent dispatch. On interrupt, store pending tool execution state for resume.
• strands-py/src/strands/__init__.py: re-export if hook events are surfaced at top level.
Gotchas / open questions
• Cross-SDK naming: TS beforeToolsEvent (type string) → Python uses class-based dispatch so no string equivalent needed. Field cancel keeps the same name.
• Interrupt resume state: TS stores { assistantMessageData, completedToolResults } on interrupt. Python needs an equivalent in agent._interrupt_state.context — confirm alignment with the existing BeforeToolCallEvent interrupt resume path.
• Dual cancel check: TS checks both beforeToolsEvent.cancel and agent.isCancelled. Python should mirror this — the event-level cancel is hook-driven, the agent-level cancel is user-driven (via agent.cancel()).
• Relationship to per-tool cancel: BeforeToolsEvent.cancel cancels the entire batch; BeforeToolCallEvent.cancel_tool cancels a single tool. Both can coexist — batch cancel takes precedence (tools never reach BeforeToolCallEvent if batch was cancelled).
• _interrupt_id format: Python should use f"v1:before_tools:{uuid.uuid5(uuid.NAMESPACE_OID, name)}" to avoid collisions with BeforeToolCallEvent interrupt IDs.
Confirmation
- [x] The change is complete in the source SDK, and every related artifact is listed above (source, tests, and docs, if the change has any).
AfterToolsEvent (merged from #3497)
Related Files (additional)
strands-ts/src/hooks/events.ts
strands-ts/src/hooks/index.ts
strands-ts/src/agent/agent.ts
strands-ts/src/types/agent.ts
strands-ts/src/index.ts
strands-ts/src/__fixtures__/mock-plugin.ts
strands-ts/src/agent/__tests__/agent.test.ts
strands-ts/src/agent/__tests__/agent.hook.test.ts
site/src/content/docs/user-guide/concepts/agents/hooks.mdx
site/src/content/docs/user-guide/concepts/interrupts.mdx
site/src/content/docs/user-guide/concepts/streaming/async-iterators.mdx
site/src/content/docs/user-guide/concepts/streaming/index.mdx
site/src/content/docs/user-guide/concepts/tools/vended-tools.mdx
What needs to be migrated
TypeScript has a batch-level AfterToolsEvent hook that fires once after all tools in a batch complete; Python only has per-tool AfterToolCallEvent. The AfterToolsEvent gives hooks aggregate visibility over all tool results and enables early loop termination via its mutable endTurn field.
Event definition (strands-ts/src/hooks/events.ts)
• AfterToolsEvent extends HookableEvent.
• Readonly fields: agent, message (assistant-role Message containing all ToolUseBlocks), toolResults (array of completed ToolResultBlocks), invocationState.
• Mutable control field: endTurn: boolean = false — when true, the agent loop terminates after this tool batch instead of sending results back to the model for another turn.
• Uses reverse callback ordering (cleanup semantics — mirrors AfterToolCallEvent).
Agent loop integration
• executeTools() constructs and yields AfterToolsEvent after all tool executions complete (after all individual AfterToolCallEvents have fired).
• After yielding, checks afterToolsEvent.endTurn: if truthy, signals the outer loop to stop (no further model calls).
Python landing points
• strands-py/src/strands/hooks/events.py: add AfterToolsEvent dataclass with message: Message, tool_results: list[ToolResult], invocation_state: dict[str, Any], mutable end_turn: bool = False. Uses reverse callback ordering.
• strands-py/src/strands/hooks/__init__.py: export the new event, add to __all__.
• strands-py/src/strands/event_loop/event_loop.py: dispatch AfterToolsEvent after all tool executions complete; if end_turn is truthy, break the event loop (no further model call).
• strands-py/src/strands/__init__.py: re-export if hook events are surfaced at top level.
Gotchas / open questions
• Relationship to BeforeToolsEvent: These are symmetric — BeforeToolsEvent fires before the batch, AfterToolsEvent fires after. Both are in this issue.
• endTurn vs end_turn: TS uses camelCase; Python should use snake_case per convention.
• Reverse ordering: AfterToolsEvent uses reverse callback ordering (like AfterToolCallEvent) for proper cleanup semantics; BeforeToolsEvent does NOT.
• No interrupt support: Unlike BeforeToolsEvent, AfterToolsEvent is NOT Interruptible — tools have already run, so halting mid-execution doesn't apply.