Skip to content

Conversation

@bkrabach
Copy link
Collaborator

@bkrabach bkrabach commented Jan 9, 2026

Summary

Adds graceful cancellation support to recipe execution, enabling users to cancel long-running recipes at any nesting depth via Ctrl+C or the recipes(operation="cancel") tool call.

Changes

SessionManager (session.py)

  • Added CancellationStatus enum with states: NONE, REQUESTED, IMMEDIATE, CANCELLED
  • Added cancellation methods:
    • request_cancellation(session_id, project_path, immediate=False) - Request graceful or immediate cancellation
    • clear_cancellation(session_id, project_path) - Clear cancellation status for resumption
    • get_cancellation_status(session_id, project_path) - Get current status
    • is_cancellation_requested(session_id, project_path) - Check if cancellation requested
    • is_immediate_cancellation(session_id, project_path) - Check if immediate cancellation
    • mark_cancelled(session_id, project_path, cancelled_at_step) - Mark session as cancelled

RecipeExecutor (executor.py)

  • Added CancellationRequestedError exception with context (session_id, is_immediate, current_step)
  • Added _check_cancellation() method to check session state
  • Added _check_coordinator_cancellation() to integrate with amplifier-core's CancellationToken
  • Inserted cancellation checks at all loop boundaries:
    • Before each step in flat recipe execution
    • Before each step in staged recipe execution
    • Before each iteration in sequential foreach loops
    • Before each iteration in parallel foreach loops
    • After agent steps complete (graceful checkpoint)
  • Added parent_session_id propagation for nested recipe cancellation

RecipesTool (__init__.py)

  • Exported CancellationRequestedError and CancellationStatus
  • Added cancel operation with optional immediate flag

Tests

  • Added comprehensive test suite (test_cancellation.py) with 18 tests
  • Updated existing test fixtures to mock new cancellation methods

Testing

All 304 tests pass (286 existing + 18 new cancellation tests).

Usage

# Graceful cancellation (complete current step, then stop)
recipes(operation="cancel", session_id="...")

# Immediate cancellation (stop ASAP)
recipes(operation="cancel", session_id="...", immediate=True)

Cancellation also integrates with CLI's Ctrl+C signal handling via the coordinator's CancellationToken.

🤖 Generated with Amplifier

Co-Authored-By: Amplifier [email protected]

Add graceful cancellation of recipe execution at any nesting depth.

Changes:
- Add CancellationStatus enum and cancellation methods to SessionManager
- Add CancellationRequestedError exception with session context
- Insert cancellation checks at all loop boundaries (steps, foreach, parallel)
- Add parent_session_id propagation for nested recipe cancellation
- Export CancellationRequestedError and CancellationStatus from module
- Add 'cancel' operation to RecipesTool
- Add comprehensive test suite for cancellation (18 tests)
- Update existing test fixtures to mock new cancellation methods

This enables users to gracefully cancel long-running recipes via Ctrl+C
or the recipes(operation="cancel") tool call, with proper state
preservation for potential resumption.

🤖 Generated with [Amplifier](https://github.com/microsoft/amplifier)

Co-Authored-By: Amplifier <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants