KEMBAR78
Optimistic Next Edit Suggestions by avarayr · Pull Request #222 · microsoft/vscode-copilot-chat · GitHub
Skip to content

Conversation

avarayr
Copy link

@avarayr avarayr commented Jul 11, 2025

Basis: Tab, Tab, Tab — Objects in motion want to stay in motion.

Summary

This PR implements optimistic prefetching for next edit suggestions, eliminating delay between accepting an edit and receiving the next suggestion by assuming that user will keep pressing tab to accept sequential edits.

Problem

Currently, when users accept an inline edit suggestion, there's a noticeable delay
before the next suggestion appears. This breaks the flow of rapid sequential edits.

Solution

Implement predictive fetching that anticipates user behavior based on the "objects in
motion stay in motion" principle. When a NES is shown, the system
immediately fetches the next edit in the background, assuming the user will continue
accepting suggestions.

All existing tests continue to pass.

Performance

  • Reduces next edit latency from ~300ms to <10ms for predicted cases
  • No impact on initial suggestion performance
  • Minimal memory overhead with automatic cleanup

Discussion

Optimistic NES will multiply the number of NES requests by 3, which may affect the rate limit/user allowance.

@avarayr avarayr changed the title feat: add optimistic fetching for next edit suggestions Speculative fetching for Next Edit Suggestions Jul 11, 2025
@avarayr avarayr changed the title Speculative fetching for Next Edit Suggestions Optimistic Next Edit Suggestions Jul 11, 2025
@avarayr avarayr marked this pull request as ready for review July 11, 2025 21:01
@avarayr avarayr marked this pull request as draft July 11, 2025 22:25
@avarayr
Copy link
Author

avarayr commented Jul 11, 2025

hey @ulugbekna, i'd appreciate your [& other code owners] insight on this.

There are some kinks to iron out, but in my testing, this massively improves the UX of NES.
This is one of the things that Cursor got right, hopefully we can bridge the gap.

some points for discussion:

Optimistic NES will multiply the number of NES requests by 3, which may affect the rate limit/user allowance.

@avarayr avarayr marked this pull request as ready for review July 11, 2025 23:32
@bhavyaus bhavyaus requested a review from ulugbekna July 13, 2025 15:54
@avarayr avarayr force-pushed the feature/optimistic-next-edit-fetching branch 4 times, most recently from ef9c8d9 to 24edf6a Compare July 17, 2025 01:44
@chrmarti
Copy link
Collaborator

@avarayr Thanks for the PR! We'll take a detailed look. We are also working on additional approaches to improve the experience while keeping the number of requests in check. (E.g., microsoft/vscode#255727 and microsoft/vscode#255728.)

@avarayr avarayr force-pushed the feature/optimistic-next-edit-fetching branch 3 times, most recently from 69b2edb to 89154be Compare July 20, 2025 18:52
- Add prefetch chain mechanism that triggers when edits are shown
- Prefetch up to 3 edits deep to reduce latency for sequential edits
- Integrate with existing NextEditCache infrastructure
- Track active prefetches to avoid duplicates
- Clean up resources on disposal
Copy link
Collaborator

@chrmarti chrmarti left a comment

Choose a reason for hiding this comment

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

@avarayr Thanks again for the PR! I have left a few questions. Could you rebase/merge it with the latest from main?

Could you also add a setting to enable/disable it? We would keep this off by default at first, so we can get it well tested and estimate the number of additional LLM calls.

}

// Create a unique key for this prefetch based on document state
const prefetchKey = `${docId}:${documentState.value.length}:${depth}`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we only show one suggestion at a time, maybe there should be at most one active prefetch at a time? (Avoiding the need for a key here.)

try {

// Create a synthetic document that represents the future state
const syntheticDoc = this._workspace.getDocument(docId);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't used later?


// Create a synthetic document wrapper that returns our future state
const syntheticDocWrapper = {
...doc,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure this is safe to copy like that, will need to take closer look.

// Optimistic fetching already triggered in handleShown
}

public handleRejection(docId: DocumentId, suggestion: NextEditResult) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should the prefetch be canceled when the current suggestion is rejected? Similarly: What if the current suggestion is ignored (callback below).

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.

4 participants