From e4239b25c3a7809db786412f5df5686122754347 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Mon, 23 Feb 2026 15:31:09 -0500 Subject: [PATCH] Keep only the last segment after bracketed metadata in LLM responses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- cogs/chat.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/cogs/chat.py b/cogs/chat.py index 3c45ee1..93f1e74 100644 --- a/cogs/chat.py +++ b/cogs/chat.py @@ -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