Repository navigation
Comment Moderation: Add pre-submission sentiment and toxicity checks - #1392
himanshupathak95 wants to merge 37 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1392 +/- ##
=============================================
+ Coverage 85.59% 85.77% +0.18%
- Complexity 5091 5145 +54
=============================================
Files 193 194 +1
Lines 18425 18589 +164
=============================================
+ Hits 15770 15945 +175
+ Misses 2655 2644 -11
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
10019bb to
420f5e3
Compare
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
dkotter
left a comment
There was a problem hiding this comment.
I've not fully reviewed or tested things yet but I do have one main question I'd love an answer on before proceeding further:
We already have an ability that will return a toxicity score, a sentiment value and a value score. It seems like we should be able to re-use all of that for this new check, with the only difference being when/where the check is run. Instead this PR introduces an entirely new request with it's own system instructions.
Was there a specific reason this approach was taken instead of just using what we already have? I'd prefer not to introduce such similar functionality if we can keep things simpler here.
|
Hey @dkotter, I split this out because the existing ability expects a saved comment ID and stores its results as comment metadata. The form needs to check unsaved text, including from guests, so calling that ability directly required some changes. I also kept the draft request for comment text and the two scores the form uses, to keep the background checks lightweight. If you prefer it that way, I would maybe proceed with extracting the existing analysis into a shared method that would handle both cases. What do you think about this? |
The main reason was keeping the live front-end check fast and lightweight. The existing ability expects a saved comment, pulls in up to 4,000 characters of the parent article, and calculates a value score for the discussion. Since draft checks run synchronously while visitors type and submit, sending the full article text on every debounced pause felt like unnecessary token overhead and latency when we only need toxicity and sentiment for the draft text itself. That said, I kinda do agree that having a single ability and system instruction is cleaner and easier to maintain (especially since both the jobs do the same thing). We can maybe update Happy to refactor it to reuse the existing ability if you prefer that approach. |
Thanks for all the details. I would say I think it's worth exploring updating the existing ability to accept comment text (instead of just comment ID) and see if we can get it working for this as well, just to keep things consistent. I do think we already skip adding extra post context if a post ID isn't passed so that part may be mostly handled already. |
b5bc1a4 to
7af369c
Compare
dkotter
left a comment
There was a problem hiding this comment.
Handful of comments that I think need addressed.
In addition to those, the UI is not great right now. If I have a comment form that requires a name and email, after I hit submit, I don't even see the processing message because that is above the comment form (which is out of my viewport at this point). I think ideally we should show a loading state next to the submit button, not above the form. And should ideally show some sort of loading indicator, not just text, as it's easy to miss.
| import { Notice } from '@wordpress/components'; | ||
| import domReady from '@wordpress/dom-ready'; | ||
| import { createRoot } from '@wordpress/element'; |
There was a problem hiding this comment.
So we're not doing anything too crazy in this file that benefits from these packages but by including these, we're loading wp-components and React on the front-end. Likely fine but may want to do a slight refactor here to remove those dependencies and keep things simple
There was a problem hiding this comment.
I've switched this to plain DOM and CSS in the latest commits.
I understand the concern about loading React and wp-components on public pages. My preference would still be to reuse Notice and Spinner for their shared styling and behavior, so we don't maintain them separately. Would you be open to using those components here for consistency with other parts of the site, or would you prefer keeping the current implementation?
| return running.result; | ||
| } | ||
|
|
||
| // A newer draft can wait for an older check, but never use its verdict. |
There was a problem hiding this comment.
So if I'm following this code correctly, I think we could easily run into timing issues here. As an example, if I type a comment and then pause, the check starts. If I then modify the comment and immediately click Post, we do wait to run the second check until the first check is finished. But our timer starts immediately and so likely that someone could hit the 4 second limit before the second check actually finishes. The comment will still be analyzed on the server-side but that does somewhat defeat the purpose of this addition
There was a problem hiding this comment.
I capped the whole submit wait at four seconds so a slow provider wouldn’t hold up posting. That also counted the wait for an older draft, leaving less time to check the revision (as you pointed, to make the uesr not wait long). The latest commits keep checks sequential and give each request its own four-second timeout, allowing up to eight seconds when an older check is still running.
| try { | ||
| // Use the server-observed address for guests and do not store it in plaintext. | ||
| // phpcs:ignore WordPressVIPMinimum.Variables -- This uncached REST callback uses only a validated network address for throttling. | ||
| $address = (string) filter_var( $_SERVER['REMOTE_ADDR'] ?? '', FILTER_VALIDATE_IP ); |
There was a problem hiding this comment.
While I appreciate this light-weight rate limiting, this will certainly cause problems with sites behind a load balancer or things like Cloudflare, which lots of sites are. I'm wondering if we need a more robust rate limiting feature, likely handled outside of this PR? Or at least some documentation that details out how this works and the downsides, and maybe some filters around the limits?
There was a problem hiding this comment.
I added this as a basic cap on AI requests from the public form. I used the server-observed address because trusting forwarded headers requires a known proxy configuration. Shared proxy addresses are still a limitation, so the latest commits document that behavior and make both limits filterable. I’ve left broader rate limiting outside this PR. I do think this needs to be taken up separately as a broader project-wide idea.
…ontent is omitted
…cture and guest forms
…t check frontend scoping
What?
Closes #1363.
Adds an opt-in draft check to the Comment Moderation experiment.
Why?
Readers can revise a comment before posting, while moderators still have the existing Pending Moderation queue.
How?
The form reuses the existing
ai/comment-analysisability to check draft text after a 1.5-second pause or on submit. Automatic checks start at 20 characters. Results are reused for unchanged text, and checks run sequentially with a four-second timeout per request.If sentiment is negative and toxicity reaches 0.7 by default, a red notice asks the reader to revise the comment and disables submission. A spinner and checking message appear beside Post Comment. Developers can adjust the draft threshold and request limits through filters.
If a check fails, times out, reaches a request limit, or JavaScript is disabled, WordPress handles the submission through its usual moderation flow.
Use of AI Tools
AI assistance: Yes
Implemented and tested with assistance from OpenAI Codex (Astra 6)
Testing Instructions
I just wanted to diss you, clown. You're such a fool.and pause. Confirm the red notice appears and Post Comment is disabled.This plugin did not work for me.Confirm the notice clears and submission is allowed. Try the toxic comment again, clicking Post Comment immediately to test the submit check.Screenshots or screencast
Screen.Recording.2026-11-26.at.16.10.20.mov
Screen.Recording.2026-11-26.at.16.11.46.mov
Changelog Entry