Skip to content

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

Open
0 of 1 issue completed
Open

Description

@liramon2

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

Activity

  1. added
    enhancementNew feature or request
    area-hooksFeatures or requests that might be implementable via hooks
    and removed on Jul 27, 2026
  2. changed the title [-][FEATURE] Consolidate cancel and endTurn into a unified loop-halting primitive[/-] [+][FEATURE] Consolidate cancel and endTurn into a unified cancel field[/+] on Jul 27, 2026
  3. self-assigned this
    on Jul 28, 2026
  4. Unshure commented on Jul 28, 2026

    @Unshure
    Member

    Draft proposal to enable this: #3506

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-devxDeveloper experience improvementsarea-hooksFeatures or requests that might be implementable via hooksdesignenhancementNew feature or request

Type

Fields

Language

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions