Keep only the last segment after bracketed metadata in LLM responses

The model dumps paraphrased context and style labels in [brackets]
before its actual roast. Instead of just removing bracket lines
(which leaves the preamble text), split on them and keep only the
last non-empty segment — the real answer is always last.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-23 15:31:09 -05:00
parent 02b2870f2b
commit e4239b25c3

View File

@@ -205,12 +205,13 @@ class ChatCog(commands.Cog):
await typing_ctx.__aexit__(None, None, None)
# Strip leaked metadata the LLM may echo back.
# The LLM paraphrases/reformats injected context in unpredictable ways,
# so nuke any [bracketed block] that sits on its own line — real roasts
# don't use standalone bracketed text.
# The LLM often dumps paraphrased context and style labels in [brackets]
# before/between its actual answer. Split on those bracket lines and
# keep only the last non-empty segment — the real roast is always last.
if response:
response = re.sub(r"^\s*\[[^\]]*\]\s*$", "", response, flags=re.MULTILINE)
response = re.sub(r"\n{2,}", "\n", response).strip()
segments = re.split(r"^\s*\[[^\]]*\]\s*$", response, flags=re.MULTILINE)
segments = [s.strip() for s in segments if s.strip()]
response = segments[-1] if segments else ""
if not response:
log_channel = discord.utils.get(message.guild.text_channels, name="bcs-log")
@@ -317,10 +318,11 @@ class ChatCog(commands.Cog):
recent_bot_replies=recent_bot_replies,
)
# Strip leaked metadata (same catch-all as main chat path)
# Strip leaked metadata (same approach as main chat path)
if response:
response = re.sub(r"^\s*\[[^\]]*\]\s*$", "", response, flags=re.MULTILINE)
response = re.sub(r"\n{2,}", "\n", response).strip()
segments = re.split(r"^\s*\[[^\]]*\]\s*$", response, flags=re.MULTILINE)
segments = [s.strip() for s in segments if s.strip()]
response = segments[-1] if segments else ""
if not response:
return