Skip to content
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

Agent: add cancelation support #787

Merged
merged 1 commit into from
Aug 23, 2023
Merged

Agent: add cancelation support #787

merged 1 commit into from
Aug 23, 2023

Conversation

olafurpg
Copy link
Member

@olafurpg olafurpg commented Aug 22, 2023

Previously, the agent didn't support canceling autocomplete requests. This PR adds support for cancellation through the new $/cancelRequest notification, which mirrors the structure of the same notification in LSP.

Test plan

Manually tested with IntelliJ here https://github.com/sourcegraph/sourcegraph/pull/56119

@@ -336,6 +337,21 @@ export class Position implements VSCodePosition {
}
}

export class Location implements VSCodeLocation {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is unrelated to cancelation. There was a new esbuild linking error from new graph/codeintel related changes so I fixed it here while working on cancelation.

export class CancellationTokenSource {
public token: unknown

export class CancellationToken implements vscode_types.CancellationToken {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I managed to mirror the behavior correctly here. Would be good to get a second pair of eyes on the implementation.

olafurpg added a commit to sourcegraph/sourcegraph-public-snapshot that referenced this pull request Aug 22, 2023
Fixes https://github.com/sourcegraph/sourcegraph/issues/56042

Previously, we didn't correctly propagate autocomplete cancelation to
the agent. This was problematic because we could end up queueing a lot
of autocomplete requests making the experience lag. This PR fixes the
problem by sending a `$/cancelRequest` notification to the agent as
implemented in sourcegraph/cody#787
@@ -162,13 +162,12 @@ export class Agent extends MessageHandler {
})
return null
})
this.registerRequest('autocomplete/execute', async params => {
this.registerRequest('autocomplete/execute', async (params, token) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Not sure about best practices, but I find the word "token" overused in programming in general. (Think access tokens, refresh tokens, bearer tokens, cancellation tokens, etc.) As a result, I usually err on the side of being specific in every case; I'd probably call this one "cancellationToken", but "cancelToken" is also fine, to match the rest of the code. WDYT? (I know you just moved this from a local variable, but it might be a good time for a rename.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree in the general case but I think it's fine to use token here since we're wrapping that vscode API that uses token

		/**
		 * Provides inline completion items for the given position and document.
		 * If inline completions are enabled, this method will be called whenever the user stopped typing.
		 * It will also be called when the user explicitly triggers inline completions or explicitly asks for the next or previous inline completion.
		 * In that case, all available inline completions should be returned.
		 * `context.triggerKind` can be used to distinguish between these scenarios.
		 *
		 * @param document The document inline completions are requested for.
		 * @param position The position inline completions are requested for.
		 * @param context A context object with additional information.
		 * @param token A cancellation token.
		 * @return An array of completion items or a thenable that resolves to an array of completion items.
		 */
		provideInlineCompletionItems(document: TextDocument, position: Position, context: InlineCompletionContext, token: CancellationToken): ProviderResult<InlineCompletionItem[] | InlineCompletionList>;

olafurpg added a commit to sourcegraph/sourcegraph-public-snapshot that referenced this pull request Aug 22, 2023
* JetBrains: add cancelation support

Fixes https://github.com/sourcegraph/sourcegraph/issues/56042

Previously, we didn't correctly propagate autocomplete cancelation to
the agent. This was problematic because we could end up queueing a lot
of autocomplete requests making the experience lag. This PR fixes the
problem by sending a `$/cancelRequest` notification to the agent as
implemented in sourcegraph/cody#787

* Document cancelation

* Dispose cancelation token to avoid redundant requests

Previously, we sent `$/cancelRequest` for every completion including
successful ones.
Previously, the agent didn't support canceling autocomplete requests.
This PR adds support for cancellation through the new `$/cancelRequest`
notification, which mirrors the structure of the same notification in
LSP.
Copy link
Contributor

@vdavid vdavid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We did the code review in a sync session and now it looks 👌

@olafurpg olafurpg merged commit d32f6b9 into main Aug 23, 2023
9 checks passed
@olafurpg olafurpg deleted the olafurpg/cancel branch August 23, 2023 08:54
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.

None yet

2 participants