Skip to content

Conversation

@bywang56
Copy link
Contributor

@bywang56 bywang56 commented Oct 3, 2025

Problem

Both completion suggestions (ghost text) and edit suggestions (diff view) could appear simultaneously due to a race condition. The EditSuggestionState flag was set too late in the edit display flow, allowing completion requests to check the state, see it as false, and proceed even while an edit was being displayed.

Solution

Fixed the race condition by setting the edit state immediately at the start of displaySvgDecoration() before any async operations:

  • Set EditSuggestionState to true at the beginning of the edit flow
  • Add state cleanup when edit cannot be shown (completion already active or invalid patch)
  • Avoid calling clearDecorations() in displayEditSuggestion() to prevent premature state reset
  • Check EditSuggestionState before making completion requests in recommendationService.ts.

This ensures only one suggestion type is displayed at a time with proper mutual exclusion.


  • Treat all work as PUBLIC. Private feature/x branches will not be squash-merged at release time.
  • Your code changes must meet the guidelines in CONTRIBUTING.md.
  • License: I confirm that my contribution is made under the terms of the Apache 2.0 license.

@bywang56 bywang56 requested a review from a team as a code owner October 3, 2025 22:25
@amazon-inspector-ohio
Copy link

⏳ I'm reviewing this pull request for security vulnerabilities and code quality issues. I'll provide an update when I'm done

@github-actions
Copy link

github-actions bot commented Oct 3, 2025

  • This pull request modifies code in src/* but no tests were added/updated.
    • Confirm whether tests should be added or ensure the PR description explains why tests are not required.
  • This pull request implements a feat or fix, so it must include a changelog entry (unless the fix is for an unreleased feature). Review the changelog guidelines.
    • Note: beta or "experiment" features that have active users should announce fixes in the changelog.
    • If this is not a feature or fix, use an appropriate type from the title guidelines. For example, telemetry-only changes should use the telemetry type.

@amazon-inspector-ohio
Copy link

✅ I finished the code review, and didn't find any security or code quality issues.

@bywang56
Copy link
Contributor Author

bywang56 commented Oct 6, 2025

/retryBuilds

@bywang56 bywang56 closed this Oct 6, 2025
@bywang56 bywang56 reopened this Oct 6, 2025
@amazon-inspector-ohio
Copy link

⏳ I'm reviewing this pull request for security vulnerabilities and code quality issues. I'll provide an update when I'm done

@amazon-inspector-ohio
Copy link

✅ I finished the code review, and didn't find any security or code quality issues.

@atonaamz atonaamz changed the title fix: block completion before active edit is accepted/rejected fix(amazonq): block completion before active edit is accepted/rejected Oct 6, 2025
@Will-ShaoHua Will-ShaoHua reopened this Oct 6, 2025
@amazon-inspector-ohio
Copy link

⏳ I'm reviewing this pull request for security vulnerabilities and code quality issues. I'll provide an update when I'm done

@amazon-inspector-ohio
Copy link

✅ I finished the code review, and didn't find any security or code quality issues.


await setContext('aws.amazonq.editSuggestionActive' as any, true)
EditSuggestionState.setEditSuggestionActive(true)
// Clear old decorations but don't reset state (state is already set in displaySvgDecoration)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to move the logic to displaySvgDecoration? I think we clear decoration after making sure that all condition passes which is in displayEditSuggestion? should we just need the condition in recommendationService.ts?

if (!isTriggerByDeletion && !request.partialResultToken && !EditSuggestionState.isEditSuggestionActive()) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The check in recommendationService.ts alone isn't sufficient. Without setting the state early in displaySvgDecoration(), there's a race condition window between when the edit flow starts and when the state is finally set. During this window (which includes async operations like isCompletionActive() and applyPatch()), completion requests can check the state, see it as false, and proceed. Both changes are needed for proper mutual exclusion.

) {
const originalCode = editor.document.getText()

// Set edit state immediately to prevent race condition with completion requests
Copy link
Contributor

Choose a reason for hiding this comment

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

what if the state is set to false later and we already skip completion request?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Setting state to TRUE early and then potentially setting it to FALSE later (if edit fails) is the correct behavior. When we set state to TRUE, we're indicating "an edit operation is in progress" - completion should be skipped during this time regardless of whether the edit ultimately succeeds or fails. If the edit fails, we clean up the state, and future completion requests will work normally. The alternative (not setting state early) is what causes the bug where both suggestions appear simultaneously.

@bywang56 bywang56 closed this Oct 6, 2025
@bywang56 bywang56 reopened this Oct 6, 2025
@amazon-inspector-ohio
Copy link

⏳ I'm reviewing this pull request for security vulnerabilities and code quality issues. I'll provide an update when I'm done

@amazon-inspector-ohio
Copy link

✅ I finished the code review, and didn't find any security or code quality issues.

@bywang56 bywang56 merged commit 7902e03 into aws:master Oct 6, 2025
58 of 62 checks passed
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.

3 participants