Skip to content

Fix excessive WP-CLI command invocations in Assistant tab - #2472

Merged
fredrikekelund merged 1 commit into
trunkfrom
stu-1254-fix-excessive-wp-cli-calls
Jan 26, 2026
Merged

Fix excessive WP-CLI command invocations in Assistant tab#2472
fredrikekelund merged 1 commit into
trunkfrom
stu-1254-fix-excessive-wp-cli-calls

Conversation

@fredrikekelund

Copy link
Copy Markdown
Contributor

Related issues

Proposed Changes

The 1.7.0 release introduced a bug that caused an excessive number of WP-CLI invocations in the Assistant tab (triggered by the updateFromSite thunk). This led to the commands failing and showing up as JSON parsing errors in the browser console. This seems to be caused by mutations of the selectedSite object returned by the useSiteDetails hook.

This PR fixes the issue by changing the dependency array of the useEffect hook that triggers the updateFromSite thunk, and by adding a condition config to the thunk itself that prevents parallel invocations.

Testing Instructions

  1. Run npm start
  2. Open the Assistant tab
  3. Send a message
  4. Ensure that there are no Unexpected end of JSON input messages in the devtools console

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?
@fredrikekelund
fredrikekelund requested a review from a team January 26, 2026 09:27
@fredrikekelund fredrikekelund self-assigned this Jan 26, 2026
Comment on lines +389 to +390
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ dispatch, selectedSite.id ] );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the key change. The selectedSite object is being mutated frequently somewhere, so we change the dependency to selectedSite.id

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good catch! I think we could create a follow-up to identify the root cause, but for the time being, this is a good fix that prevents the re-runs when properties other than id change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think we could create a follow-up to identify the root cause

It could be that the themeDetails object is overwritten more frequently now. That's not necessarily an error, IMO. Depending on the entire selectedSite object here might just have been a flawed strategy…

Still, it would be good to understand why this happened in the first place.

Comment thread src/stores/chat-slice.ts
Comment on lines -114 to +119
'x-quota-reset': z.string().datetime( { offset: true } ),
'x-quota-reset': z.iso.datetime( { offset: true } ),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread src/stores/chat-slice.ts
Comment on lines +92 to 98
{
condition: ( { site }, { getState } ) => {
const { chat } = getState();
const thunkIsCurrentlyLoading = chat.isLoadingUpdateFromSiteDict[ site.id ];
return ! thunkIsCurrentlyLoading;
},
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Adds additional safety by preventing multiple parallel updateFromSite invocations

@wpmobilebot

Copy link
Copy Markdown
Collaborator

📊 Performance Test Results

Comparing 408de2c vs trunk

site-editor

Metric trunk 408de2c Diff Change
load 2901.00 ms 2889.00 ms -12.00 ms 🟢 -0.4%

site-startup

Metric trunk 408de2c Diff Change
siteCreation 7082.00 ms 7103.00 ms +21.00 ms 🔴 0.3%
siteStartup 3932.00 ms 3930.00 ms -2.00 ms 🟢 -0.1%

Results are median values from multiple test runs.

Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change

@epeicher epeicher left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix @fredrikekelund! I have tested it, and it works as expected. I don't see any errors in the console after using the AI assistant.

Before After
Image Image
Comment on lines +389 to +390
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ dispatch, selectedSite.id ] );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good catch! I think we could create a follow-up to identify the root cause, but for the time being, this is a good fix that prevents the re-runs when properties other than id change.

@gcsecsey gcsecsey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The approach and the changes make sense to me. I couldn't reproduce the issue on trunk on Mac, so I can't confirm yet if the changes eliminated the errors on my machine.

I tested sending messages to ask about the installed plugins/themes on the site, or to print some WP CLI commands for me that I ran, but didn't get any errors in the console:

trunk After
Image Image

I see in Sentry that the issue is platform agnostic, so I think I should be able to reproduce it on Mac:
Image

@fredrikekelund do you have any tips or steps on how to produce the excessive WPCLI calls on trunk?

@ivan-ottinger ivan-ottinger left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix, Fredrik. The change looks good and I did not observe any regressions. The Assistant still works as expected.

Regarding the root issue - have you been able to reproduce it? I haven't been able so far.

Comment thread src/stores/chat-slice.ts
import * as Sentry from '@sentry/electron/renderer';
import { WPCOM } from 'wpcom/types';
import { z } from 'zod';
import { DEFAULT_PHP_VERSION } from 'common/constants';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice catch!

@fredrikekelund

Copy link
Copy Markdown
Contributor Author

have you been able to reproduce it?

I have. It looks like @epeicher could, too (see #2472 (review)).

The issue comes from excessive updateFromSite thunk invocations. chat-slice starts triggering those as soon as the Assistant tab is opened, basically.

@gcsecsey @ivan-ottinger, maybe you cannot trigger the errors on trunk because the server wasn't running…

@fredrikekelund
fredrikekelund merged commit 861cbc4 into trunk Jan 26, 2026
14 checks passed
@fredrikekelund
fredrikekelund deleted the stu-1254-fix-excessive-wp-cli-calls branch January 26, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants