Skip to content

feat: Add API endpoint to cancel in-progress agent tasks#5983

Open
Oxygen56 wants to merge 2 commits into
google:mainfrom
Oxygen56:feat/cancel-agent-tasks
Open

feat: Add API endpoint to cancel in-progress agent tasks#5983
Oxygen56 wants to merge 2 commits into
google:mainfrom
Oxygen56:feat/cancel-agent-tasks

Conversation

@Oxygen56
Copy link
Copy Markdown

@Oxygen56 Oxygen56 commented Jun 5, 2026

Summary

Adds a POST /apps/{app}/users/{user}/sessions/{session}:cancel endpoint that sets temp:cancelled in 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 /cancel endpoint with FastAPI
  • base_llm_flow.py: _is_session_cancelled() check + early return in _call_llm_async()
  • functions.py: Cancellation check before tool execution
  • tests/unittests/cli/test_cancel_session.py: 12 unit tests

Testing Plan

pytest tests/unittests/cli/test_cancel_session.py -v

Tests cover:

  • _is_session_cancelled with no session / no state / no flag / flag true / flag false
  • Cancellation detection in _call_llm_async flow
  • Cancel event construction with temp:cancelled state delta
  • Cancel response format validation

🤖 Generated with Claude Code
Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com

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>
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Jun 5, 2026

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.

@adk-bot adk-bot added the services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc label Jun 5, 2026
@adk-bot
Copy link
Copy Markdown
Collaborator

adk-bot commented Jun 5, 2026

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:

  1. Contributor License Agreement (CLA):

    • It looks like the CLA check has failed. Please sign the Google Contributor License Agreement so we can officially accept your changes. You can visit https://cla.developers.google.com/ to sign it or check your current agreements.
  2. Unit Tests:

    • Our guidelines require all code changes to include comprehensive tests to prevent regressions. Please add unit tests covering the new endpoint and cancellation behavior (e.g., under tests/unittests/).
  3. Manual End-to-End (E2E) Tests & Evidence:

    • For new features, please include a testing plan section in your PR description.
    • Please also provide manual E2E test evidence (such as console logs or screenshots) showing the cancel endpoint being called and the execution halting gracefully as expected.

This information will help our reviewers better understand and verify your fix more efficiently. Thank you!

@Oxygen56 Oxygen56 force-pushed the feat/cancel-agent-tasks branch from c86b11f to 138fd56 Compare June 5, 2026 17:34
@rohityan rohityan self-assigned this Jun 5, 2026
@rohityan
Copy link
Copy Markdown
Collaborator

rohityan commented Jun 5, 2026

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!

@rohityan rohityan added the request clarification [Status] The maintainer need clarification or more information from the author label Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

request clarification [Status] The maintainer need clarification or more information from the author services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants