-
Notifications
You must be signed in to change notification settings - Fork 1.3k
java: plumb AbortSignal through ToolInvocation for cooperative cancellation #1707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gimenete
wants to merge
3
commits into
github:main
Choose a base branch
from
gimenete:gimenete/java-abort-signal-tool-invocation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+484
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
123 changes: 123 additions & 0 deletions
123
java/src/main/java/com/github/copilot/rpc/AbortSignal.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package com.github.copilot.rpc; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| /** | ||
| * A signal that indicates whether a tool invocation has been aborted. | ||
| * <p> | ||
| * An {@code AbortSignal} is passed to tool handlers via | ||
| * {@link ToolInvocation#getAbortSignal()} and is triggered when | ||
| * {@link com.github.copilot.CopilotSession#abort()} is called while the tool is | ||
| * executing. Tool handlers can use this to implement cooperative cancellation, | ||
| * allowing them to stop long-running work gracefully when the session is | ||
| * aborted. | ||
| * | ||
| * <h2>Example Usage</h2> | ||
| * | ||
| * <pre>{@code | ||
| * ToolHandler handler = invocation -> { | ||
| * AbortSignal signal = invocation.getAbortSignal(); | ||
| * return CompletableFuture.supplyAsync(() -> { | ||
| * while (!signal.isAborted()) { | ||
| * // do incremental work here | ||
| * } | ||
| * throw new CancellationException("Tool aborted"); | ||
| * }); | ||
| * }; | ||
| * }</pre> | ||
| * | ||
| * <h2>Callback Registration</h2> | ||
| * | ||
| * <pre>{@code | ||
| * ToolHandler handler = invocation -> { | ||
| * AbortSignal signal = invocation.getAbortSignal(); | ||
| * signal.onAborted(() -> System.out.println("Aborting tool!")); | ||
| * // ... perform work ... | ||
| * return CompletableFuture.completedFuture("done"); | ||
| * }; | ||
| * }</pre> | ||
| * | ||
| * @see ToolInvocation#getAbortSignal() | ||
| * @see com.github.copilot.CopilotSession#abort() | ||
| * @since 1.6.0 | ||
| */ | ||
| public final class AbortSignal { | ||
|
|
||
| private final AtomicBoolean aborted = new AtomicBoolean(false); | ||
| private final CopyOnWriteArrayList<Runnable> listeners = new CopyOnWriteArrayList<>(); | ||
|
|
||
| /** | ||
| * Returns whether this signal has been aborted. | ||
| * | ||
| * @return {@code true} if {@link com.github.copilot.CopilotSession#abort()} was | ||
| * called while this tool invocation was in progress; {@code false} | ||
| * otherwise | ||
| */ | ||
| public boolean isAborted() { | ||
| return aborted.get(); | ||
| } | ||
|
|
||
| /** | ||
| * Registers a callback to be invoked when this signal is aborted. | ||
| * <p> | ||
| * If the signal is already aborted at the time of registration, the callback is | ||
| * invoked immediately on the calling thread. | ||
| * <p> | ||
| * The callback is guaranteed to be invoked at most once, regardless of | ||
| * concurrent calls to {@link #abort()} and {@code onAborted}. Any | ||
| * {@link Throwable} thrown by the callback is silently ignored. | ||
| * | ||
| * @param listener | ||
| * the callback to invoke on abort | ||
| * @throws NullPointerException | ||
| * if listener is null | ||
| */ | ||
| public void onAborted(Runnable listener) { | ||
| Objects.requireNonNull(listener, "listener must not be null"); | ||
| // Wrap in an AtomicBoolean-guarded runnable so the callback fires at most once | ||
| // even if abort() races with this method between listeners.add() and the | ||
| // aborted.get() check below. | ||
| AtomicBoolean fired = new AtomicBoolean(false); | ||
| Runnable once = () -> { | ||
| if (fired.compareAndSet(false, true)) { | ||
| try { | ||
| listener.run(); | ||
| } catch (Throwable ignored) { | ||
| // Throwables from listeners are silently ignored | ||
| } | ||
| } | ||
| }; | ||
| listeners.add(once); | ||
| if (aborted.get()) { | ||
| once.run(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Triggers this abort signal, notifying all registered listeners. | ||
| * <p> | ||
| * <strong>Note:</strong> This method is intended for internal SDK use only. It | ||
| * is called by the SDK when {@link com.github.copilot.CopilotSession#abort()} | ||
| * is invoked while this tool invocation is in progress. | ||
| * <p> | ||
| * Calling this method more than once has no effect — the signal fires exactly | ||
| * once. Any {@link Throwable} thrown by a listener is silently ignored. | ||
| */ | ||
| public void abort() { | ||
| if (aborted.compareAndSet(false, true)) { | ||
| for (Runnable listener : listeners) { | ||
| try { | ||
| listener.run(); | ||
| } catch (Throwable ignored) { | ||
| // Throwables from listeners are silently ignored | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
gimenete marked this conversation as resolved.
|
||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.