Skip to content

FIX: user autocomplete search cache pollution #34208

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

Merged
merged 3 commits into from
Aug 11, 2025

Conversation

tyb-talks
Copy link
Contributor

@tyb-talks tyb-talks commented Aug 11, 2025

The current chat autocomplete pipeline can mutate the user object selected for autocomplete here:

Specifically this occurs when we attempt to create a record in the store service where it already exists, and the logic for fetching the right record there deletes some properties from the input object:

untrack(() => {
// Only update properties whose values actually changed to optimize rerenders
// If a property value is unchanged, remove it from the update list
updatedProperties &&
Object.keys(updatedProperties).forEach((key) => {
if (existing[key] === updatedProperties[key]) {
delete updatedProperties[key];
}
});
});

This mutates the object that is eventually used to replace the search term (and therefore causes an error as it is now missing all its properties except for id). This also pollutes the upstream cache in the userSearch JS module.

This PR fixes both issues by cloning the user objects before they get passed into the processing functions.

@github-actions github-actions bot added the chat PRs which include a change to Chat plugin label Aug 11, 2025
@tyb-talks tyb-talks marked this pull request as ready for review August 11, 2025 03:19
Copy link
Contributor

@nattsw nattsw left a comment

Choose a reason for hiding this comment

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

Cool, thanks for the desc

@tyb-talks tyb-talks merged commit a71eb41 into main Aug 11, 2025
16 checks passed
@tyb-talks tyb-talks deleted the fix-autocomplete-search-cache-pollution branch August 11, 2025 05:14
channel_1.add(autocomplete_user)
end

it "fails on subsequent autocomplete selection due to cache pollution" do
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this be?

does not fail on subsequent autocomplete selection due to cache pollution

tyb-talks added a commit that referenced this pull request Aug 14, 2025
…ults (#34283)

Related to #34208.

Previously, missing name properties for a selected user search result
would fail silently which made it difficult to tell if something had
gone wrong with the user autocomplete.

This PR adds some error handling to make it more obvious that an error
has occurred. I've opted for using `toasts` here since it's less
intrusive compared to the `dialog` modal.

https://github.com/user-attachments/assets/92f585d8-95bf-4c5b-a08a-698c1a527b79
yuriyaran pushed a commit that referenced this pull request Aug 21, 2025
The current chat autocomplete pipeline can mutate the user object
selected for autocomplete here:

https://github.com/discourse/discourse/blob/071e82140cf375e2875b87d1664ae596c19320dd/plugins/chat/assets/javascripts/discourse/components/chat-composer.gjs#L485

Specifically this occurs when we attempt to create a record in the store
service where it already exists, and the logic for fetching the right
record there deletes some properties from the input object:

https://github.com/discourse/discourse/blob/216df43502e25fcb9a228bfc09f90548712d7f49/app/assets/javascripts/discourse/app/services/store.js#L426-L435

This mutates the object that is eventually used to replace the search
term (and therefore causes an error as it is now missing all its
properties except for `id`). This also pollutes the upstream cache in
the userSearch JS module.

This PR fixes both issues by cloning the user objects before they get
passed into the processing functions.
yuriyaran pushed a commit that referenced this pull request Aug 21, 2025
…ults (#34283)

Related to #34208.

Previously, missing name properties for a selected user search result
would fail silently which made it difficult to tell if something had
gone wrong with the user autocomplete.

This PR adds some error handling to make it more obvious that an error
has occurred. I've opted for using `toasts` here since it's less
intrusive compared to the `dialog` modal.

https://github.com/user-attachments/assets/92f585d8-95bf-4c5b-a08a-698c1a527b79
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chat PRs which include a change to Chat plugin
Development

Successfully merging this pull request may close these issues.

3 participants