(feat) : implement cancellation support with CancellationToken and CancellationTokenSource - #1434
Open
rohan-naik07 wants to merge 3 commits into
Open
Conversation
…llationTokenSource
…llationTokenSource
…ub.com/rohan-naik07/adk-java into feature/run-cancellation-abort-support
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to Issue or Description of Change
Related issue: #1341
Problem:
Java ADK does not provide an invocation-wide cancellation mechanism. Disposing the returned
RxJava subscription only controls that subscription and cannot be inspected consistently by nested
agents, model flows, tools, or callbacks. As a result, applications cannot implement graceful Stop
buttons or propagate request deadlines through an agent invocation.
Solution:
Add explicit, cooperative cancellation through
CancellationTokenandCancellationTokenSource:The token is stored in
RunConfigand shared throughInvocationContext, so the same signal isvisible throughout the invocation, including nested agents.
CancellationToken.none()preservesthe existing behavior when callers do not configure cancellation.
CancellationTokenSourceusesatomic state, making
cancel()thread-safe and idempotent.Cancellation checkpoints cover:
runAsyncandrunLiveexecution.Cancellation is graceful: the event stream completes normally, without an error or a synthetic
cancelled event. Events emitted before cancellation remain available to the caller and can still be
persisted. An already-running atomic model or tool operation may finish before the following
checkpoint. When a live receive flow terminates, its send task is disposed and its model connection
is closed.
RxJava disposal remains complementary and continues to cancel disposable upstream operations. It
is not used as the invocation-wide public cancellation contract.
Testing Plan
Unit Tests:
RunConfig.builder(existingConfig).close()behavior.event and prevents tool execution.
Focused test command:
./mvnw -pl core \ -Dtest=CancellationTokenSourceTest,RunConfigTest,BaseLlmFlowTest testResult: 42 tests run, 0 failures, 0 errors.
Compilation also succeeds:
The required
./mvnw testcommand was also run. It currently reports unrelated failures in thecheckout, including
NoSuchElementExceptionfrom the pre-existingLlmAgent.maybeSaveOutputToState()implementation. The cancellation-focused tests pass in bothconfigured Surefire executions.
Manual End-to-End (E2E) Tests:
Not run. The behavior is covered at the model and tool execution boundaries with deterministic unit
tests.
Checklist
Additional context
The API follows the explicit token/source pattern used by other structured cancellation systems
while retaining RxJava as the execution mechanism. It also aligns with TypeScript ADK's documented
graceful, cooperative, and idempotent cancellation semantics.
No cancellation event is written into conversation history. Cancellation represents invocation
lifecycle metadata rather than model-visible conversation content; applications that need to show
a cancelled status can derive it from the token they own.