diff --git a/lib/messages/utils.ts b/lib/messages/utils.ts index eae03327..2df246ff 100644 --- a/lib/messages/utils.ts +++ b/lib/messages/utils.ts @@ -7,6 +7,7 @@ const SUMMARY_ID_HASH_LENGTH = 16 const DCP_BLOCK_ID_TAG_REGEX = /(])[^>]*>)b\d+(<\/dcp-message-id>)/g const DCP_PAIRED_TAG_REGEX = /]*>[\s\S]*?<\/dcp[^>]*>/gi const DCP_UNPAIRED_TAG_REGEX = /<\/?dcp[^>]*>/gi +const HALLUCINATED_PARAMETER_SUFFIX_REGEX = /\nm\d+<\/parameter>\s*$/ const generateStableId = (prefix: string, seed: string): string => { const hash = createHash("sha256").update(seed).digest("hex").slice(0, SUMMARY_ID_HASH_LENGTH) @@ -163,7 +164,8 @@ export const replaceBlockIdsWithBlocked = (text: string): string => { } export const stripHallucinationsFromString = (text: string): string => { - return text.replace(DCP_PAIRED_TAG_REGEX, "").replace(DCP_UNPAIRED_TAG_REGEX, "") + const withoutHallucinatedParameter = text.replace(HALLUCINATED_PARAMETER_SUFFIX_REGEX, "") + return withoutHallucinatedParameter.replace(DCP_PAIRED_TAG_REGEX, "").replace(DCP_UNPAIRED_TAG_REGEX, "") } export const stripHallucinations = (messages: WithParts[]): void => { diff --git a/tests/message-priority.test.ts b/tests/message-priority.test.ts index 1342ca1b..4d5b2375 100644 --- a/tests/message-priority.test.ts +++ b/tests/message-priority.test.ts @@ -814,6 +814,13 @@ test("hallucination stripping does not affect non-dcp tags", async () => { ) }) +test("hallucination stripping removes trailing mXXXX artifact (issue #555)", () => { + assert.equal( + stripHallucinationsFromString("Total: maybe 20 lines changed.\n\nm0340\n\n"), + "Total: maybe 20 lines changed.\n\n", + ) +}) + test("injectMessageIds skips empty assistant messages to avoid prefill (issue #463)", () => { const sessionID = "ses_empty_assistant" const messages: WithParts[] = [