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

[FEATURE] Consolidate cancel and endTurn into a unified cancel field

Open liramon2 opened this issue 4 months ago • 1 comments

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:

  • cancel prevents the next operation from starting (model call or tool execution).
  • endTurn halts 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_tool for BeforeToolCallEvent while TypeScript uses cancel — 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 cancel on BeforeInvocationEvent, BeforeModelCallEvent, BeforeToolCallEvent, BeforeToolsEvent (already correct).
  • Replace endTurn on AfterToolsEvent with cancel.
  • Rename cancel_tool on Python's BeforeToolCallEvent to cancel.
  • All produce the same AgentResult shape: 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

liramon2 avatar Jul 27 '26 16:07 liramon2

Draft proposal to enable this: https://github.com/strands-agents/harness-sdk/pull/3506

Unshure avatar Jul 28 '26 15:07 Unshure