sdk-python
sdk-python copied to clipboard
[FEATURE] Consolidate cancel and endTurn into a unified cancel field
SDK Language
Both / Not language-specific
Problem Statement
The hook system currently has two separate mechanisms for halting the agent loop early — cancel (on BeforeInvocationEvent, BeforeModelCallEvent, BeforeToolCallEvent, BeforeToolsEvent) and endTurn (on AfterToolsEvent) — that differ only in when they fire:
cancelprevents the next operation from starting (model call or tool execution).endTurnhalts the loop after tools finish, before the next model call.
From the hook author's perspective these are the same intent ("stop the loop and return a result") attached to different lifecycle points with different field names. This creates confusion:
- Python uses
cancel_toolforBeforeToolCallEventwhile TypeScript usescancel— same concept, different name. - Hook authors need to learn which field to set depending on which event they're in.
Proposed Solution
Standardize on cancel as the single field name across all hookable events in the agent loop:
// TypeScript — on every lifecycle event inside the loop
cancel: boolean | string = false
// true → stop with default message
// string → stop with that string as the final assistant content
# Python — same semantics
cancel: bool | str = False
At a high level:
- Keep
cancelonBeforeInvocationEvent,BeforeModelCallEvent,BeforeToolCallEvent,BeforeToolsEvent(already correct). - Replace
endTurnonAfterToolsEventwithcancel. - Rename
cancel_toolon Python'sBeforeToolCallEventtocancel. - All produce the same
AgentResultshape:stopReason: 'endTurn'with the supplied (or default) message.
Use Case
- Hook authors writing guardrails that block unsafe tool calls or model prompts without memorizing per-event field names.
- Plugins that implement budget caps, timeout policies, or human-in-the-loop gates — one pattern works everywhere.
- Cross-SDK parity: contributors porting a plugin between Python and TypeScript use the same concept and field name.
- Simplifies documentation: one section on "cancelling from a hook" instead of explaining cancel vs endTurn vs cancel_tool
Draft proposal to enable this: https://github.com/strands-agents/harness-sdk/pull/3506