Skip to content

Commit 6173e5e

Browse files
authored
Merge pull request #460 from Opencode-DCP/dev
fix: broaden hallucination stripping to catch all dcp-prefixed XML tags
2 parents 74b5ffc + 3fc1ee3 commit 6173e5e

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

lib/messages/utils.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ import type { SessionState, WithParts } from "../state"
55
import type { UserMessage } from "@opencode-ai/sdk/v2"
66

77
const SUMMARY_ID_HASH_LENGTH = 16
8-
const DCP_MESSAGE_ID_TAG_REGEX =
9-
/<dcp-message-id(?=[\s>])[^>]*>(?:m\d+|b\d+|BLOCKED)<\/dcp-message-id>/g
108
const DCP_BLOCK_ID_TAG_REGEX = /(<dcp-message-id(?=[\s>])[^>]*>)b\d+(<\/dcp-message-id>)/g
11-
const DCP_SYSTEM_REMINDER_REGEX =
12-
/<dcp-system-reminder(?=[\s>])[^>]*>[\s\S]*?<\/dcp-system-reminder>/g
9+
const DCP_ANY_TAG_REGEX = /<dcp[^>]*>[\s\S]*?<\/dcp[^>]*>/gi
1310

1411
const generateStableId = (prefix: string, seed: string): string => {
1512
const hash = createHash("sha256").update(seed).digest("hex").slice(0, SUMMARY_ID_HASH_LENGTH)
@@ -174,7 +171,7 @@ export const replaceBlockIdsWithBlocked = (text: string): string => {
174171
}
175172

176173
export const stripHallucinationsFromString = (text: string): string => {
177-
return text.replace(DCP_SYSTEM_REMINDER_REGEX, "").replace(DCP_MESSAGE_ID_TAG_REGEX, "")
174+
return text.replace(DCP_ANY_TAG_REGEX, "")
178175
}
179176

180177
export const stripHallucinations = (messages: WithParts[]): void => {

tests/message-priority.test.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -611,25 +611,32 @@ test("range-mode rendered compressed summaries keep block IDs", () => {
611611
assert.doesNotMatch(summaryText, /<dcp-message-id>BLOCKED<\/dcp-message-id>/)
612612
})
613613

614-
test("hallucination stripping removes exact metadata tags and preserves lookalikes", async () => {
614+
test("hallucination stripping removes all dcp-prefixed XML tags including variants", async () => {
615615
const text =
616-
'alpha<dcp-message-id priority="high">m0007</dcp-message-id>' +
617-
"<dcp-message-id>BLOCKED</dcp-message-id>" +
616+
"alpha" +
617+
'<dcp-message-id priority="low">m0008</dcp-message-id>' +
618618
'<dcp-message-id-extra priority="high">m0008</dcp-message-id-extra>' +
619-
'<dcp-system-reminder kind="nudge">remove this</dcp-system-reminder>' +
620-
"<dcp-system-reminder-extra>keep this</dcp-system-reminder-extra>" +
619+
"<dcp-system-reminder>strip this</dcp-system-reminder>" +
620+
"<dcp-system-reminder-extra>strip this too</dcp-system-reminder-extra>" +
621621
"omega"
622622

623-
assert.equal(
624-
stripHallucinationsFromString(text),
625-
'alpha<dcp-message-id-extra priority="high">m0008</dcp-message-id-extra><dcp-system-reminder-extra>keep this</dcp-system-reminder-extra>omega',
626-
)
623+
assert.equal(stripHallucinationsFromString(text), "alphaomega")
627624

628625
const handler = createTextCompleteHandler()
629626
const output = { text }
630627
await handler({ sessionID: "session", messageID: "message", partID: "part" }, output)
628+
assert.equal(output.text, "alphaomega")
629+
})
630+
631+
test("hallucination stripping removes colon and underscore dcp tag variants", async () => {
632+
assert.equal(
633+
stripHallucinationsFromString("before<dcp:message_id>m0074</dcp:message_id>after"),
634+
"beforeafter",
635+
)
631636
assert.equal(
632-
output.text,
633-
'alpha<dcp-message-id-extra priority="high">m0008</dcp-message-id-extra><dcp-system-reminder-extra>keep this</dcp-system-reminder-extra>omega',
637+
stripHallucinationsFromString(
638+
'start<dcp-function_calls><invoke name="Bash"></invoke></dcp-function_calls>end',
639+
),
640+
"startend",
634641
)
635642
})

0 commit comments

Comments
 (0)