feat: add _sanitize_mentions helper and apply to comment/reply truncation

Introduce a new `_sanitize_mentions` method in `BotHelpersMixin` that deduplicates and removes self-mentions from text, using a compiled regex for `@handle` patterns. Apply this sanitizer before the 2000-character truncation in both `engage.py` comment posting and `social.py` reply posting to prevent duplicate or self-referential mentions from being cut off mid-handle. Additionally, fix profile URL parsing in `social.py` to strip trailing path segments, and refine `LLMClient.clean` to handle asterisks and underscores more precisely without breaking adjacent alphanumeric characters.
This commit is contained in:
2026-07-04 23:08:41 +00:00
parent 0f872336b1
commit f1bdefd834
4 changed files with 28 additions and 5 deletions
+5 -2
View File
@@ -28,7 +28,7 @@ class BotSocialMixin:
try:
await links.nth(i).click(timeout=5000)
await b._idle(1.0, 2.0)
uname = h.split("/profile/")[-1].split("?")[0]
uname = h.split("/profile/")[-1].split("?")[0].split("/")[0]
if uname not in self.state.known_users:
self.state.known_users.append(uname)
self.state.profiles_viewed += 1
@@ -446,6 +446,9 @@ class BotSocialMixin:
)
except Exception:
mentioner = ""
if mentioner and mentioner.lower() == self.state.username.lower():
self._log("Mention target resolves to self, skipping")
return False
try:
await comment_loc.scroll_into_view_if_needed(timeout=2000)
await b._idle(0.3, 0.9)
@@ -485,7 +488,7 @@ class BotSocialMixin:
return False
if mentioner and f"@{mentioner.lower()}" not in reply.lower():
reply = f"@{mentioner} {reply}"
reply = reply[:2000]
reply = self._sanitize_mentions(reply)[:2000]
textarea_sels = [
".comment .reply-form textarea[name='content']",
".reply-form textarea[name='content']",