From 3fc1ee3dffac30c0ce1bcf5e4b47669f49b664f4 Mon Sep 17 00:00:00 2001 From: Daniel Smolsky Date: Wed, 25 Mar 2026 12:29:13 -0400 Subject: [PATCH] fix: broaden hallucination stripping to catch all dcp-prefixed XML tags --- lib/messages/utils.ts | 7 ++----- tests/message-priority.test.ts | 29 ++++++++++++++++++----------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/messages/utils.ts b/lib/messages/utils.ts index bbc5fd11..10c82eb0 100644 --- a/lib/messages/utils.ts +++ b/lib/messages/utils.ts @@ -5,11 +5,8 @@ import type { SessionState, WithParts } from "../state" import type { UserMessage } from "@opencode-ai/sdk/v2" const SUMMARY_ID_HASH_LENGTH = 16 -const DCP_MESSAGE_ID_TAG_REGEX = - /])[^>]*>(?:m\d+|b\d+|BLOCKED)<\/dcp-message-id>/g const DCP_BLOCK_ID_TAG_REGEX = /(])[^>]*>)b\d+(<\/dcp-message-id>)/g -const DCP_SYSTEM_REMINDER_REGEX = - /])[^>]*>[\s\S]*?<\/dcp-system-reminder>/g +const DCP_ANY_TAG_REGEX = /]*>[\s\S]*?<\/dcp[^>]*>/gi const generateStableId = (prefix: string, seed: string): string => { const hash = createHash("sha256").update(seed).digest("hex").slice(0, SUMMARY_ID_HASH_LENGTH) @@ -174,7 +171,7 @@ export const replaceBlockIdsWithBlocked = (text: string): string => { } export const stripHallucinationsFromString = (text: string): string => { - return text.replace(DCP_SYSTEM_REMINDER_REGEX, "").replace(DCP_MESSAGE_ID_TAG_REGEX, "") + return text.replace(DCP_ANY_TAG_REGEX, "") } export const stripHallucinations = (messages: WithParts[]): void => { diff --git a/tests/message-priority.test.ts b/tests/message-priority.test.ts index 855c2c61..e02f36a8 100644 --- a/tests/message-priority.test.ts +++ b/tests/message-priority.test.ts @@ -611,25 +611,32 @@ test("range-mode rendered compressed summaries keep block IDs", () => { assert.doesNotMatch(summaryText, /BLOCKED<\/dcp-message-id>/) }) -test("hallucination stripping removes exact metadata tags and preserves lookalikes", async () => { +test("hallucination stripping removes all dcp-prefixed XML tags including variants", async () => { const text = - 'alpham0007' + - "BLOCKED" + + "alpha" + + 'm0008' + 'm0008' + - 'remove this' + - "keep this" + + "strip this" + + "strip this too" + "omega" - assert.equal( - stripHallucinationsFromString(text), - 'alpham0008keep thisomega', - ) + assert.equal(stripHallucinationsFromString(text), "alphaomega") const handler = createTextCompleteHandler() const output = { text } await handler({ sessionID: "session", messageID: "message", partID: "part" }, output) + assert.equal(output.text, "alphaomega") +}) + +test("hallucination stripping removes colon and underscore dcp tag variants", async () => { + assert.equal( + stripHallucinationsFromString("beforem0074after"), + "beforeafter", + ) assert.equal( - output.text, - 'alpham0008keep thisomega', + stripHallucinationsFromString( + 'startend', + ), + "startend", ) })