feat: Add API endpoint to cancel in-progress agent tasks#5983
Conversation
Adds a POST /apps/{app_name}/users/{user_id}/sessions/{session_id}:cancel
endpoint that sets a 'temp:cancelled' flag in the session state. The agent
execution loop checks this flag at two key checkpoints:
1. Before LLM calls (base_llm_flow.py:_call_llm_async) — yields a
cancellation response and stops the turn immediately.
2. Before tool execution (functions.py:handle_function_call_list_async) —
skips all pending tool calls and returns None.
Uses the 'temp:' prefix convention so the flag bypasses state schema
validation and is automatically cleaned up when the session ends.
Fixes google#2425
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Response from ADK Triaging Agent Hello @Oxygen56, thank you for creating this PR to add the session cancellation API! This is a very useful feature. To help us review and merge your contribution, please address the following items according to our Contribution Guidelines:
This information will help our reviewers better understand and verify your fix more efficiently. Thank you! |
c86b11f to
138fd56
Compare
|
Hi @Oxygen56 , Thank you for your contribution! It appears you haven't yet signed the Contributor License Agreement (CLA). Please visit https://cla.developers.google.com/ to complete the signing process. Once the CLA is signed, we'll be able to proceed with the review of your PR. Thank you! |
Summary
Adds a
POST /apps/{app}/users/{user}/sessions/{session}:cancelendpoint that setstemp:cancelledin the session state via EventActions. The agent checks this flag at key execution points (before LLM calls, before tool execution) and gracefully halts when cancellation is detected.Changes
api_server.py: New/cancelendpoint with FastAPIbase_llm_flow.py:_is_session_cancelled()check + early return in_call_llm_async()functions.py: Cancellation check before tool executiontests/unittests/cli/test_cancel_session.py: 12 unit testsTesting Plan
Tests cover:
_is_session_cancelledwith no session / no state / no flag / flag true / flag false_call_llm_asyncflowtemp:cancelledstate delta🤖 Generated with Claude Code
Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com