Skip to content

Commit 26e674b

Browse files
LHMQ878cursoragent
andcommitted
fix: strip injected message-id suffix before paired tag regex
Remove the legitimately injected <dcp-message-id> suffix before running DCP_PAIRED_TAG_REGEX so in-text tag mentions cannot pair with it and truncate message content. Fixes #556 Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 85b6f5c commit 26e674b

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/messages/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const SUMMARY_ID_HASH_LENGTH = 16
77
const DCP_BLOCK_ID_TAG_REGEX = /(<dcp-message-id(?=[\s>])[^>]*>)b\d+(<\/dcp-message-id>)/g
88
const DCP_PAIRED_TAG_REGEX = /<dcp[^>]*>[\s\S]*?<\/dcp[^>]*>/gi
99
const DCP_UNPAIRED_TAG_REGEX = /<\/?dcp[^>]*>/gi
10+
const INJECTED_MESSAGE_ID_SUFFIX_REGEX = /\n<dcp-message-id>m\d+<\/dcp-message-id>\s*$/
1011

1112
const generateStableId = (prefix: string, seed: string): string => {
1213
const hash = createHash("sha256").update(seed).digest("hex").slice(0, SUMMARY_ID_HASH_LENGTH)
@@ -163,7 +164,8 @@ export const replaceBlockIdsWithBlocked = (text: string): string => {
163164
}
164165

165166
export const stripHallucinationsFromString = (text: string): string => {
166-
return text.replace(DCP_PAIRED_TAG_REGEX, "").replace(DCP_UNPAIRED_TAG_REGEX, "")
167+
const withoutInjectedSuffix = text.replace(INJECTED_MESSAGE_ID_SUFFIX_REGEX, "")
168+
return withoutInjectedSuffix.replace(DCP_PAIRED_TAG_REGEX, "").replace(DCP_UNPAIRED_TAG_REGEX, "")
167169
}
168170

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

tests/message-priority.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,18 @@ test("hallucination stripping does not affect non-dcp tags", async () => {
814814
)
815815
})
816816

817+
test("hallucination stripping preserves content when dcp-message-id is mentioned in text (issue #556)", () => {
818+
const input =
819+
"The tag called `<dcp-message-id>` is used to track messages. " +
820+
"This text should survive.\n\n" +
821+
"<dcp-message-id>m0369</dcp-message-id>"
822+
823+
assert.equal(
824+
stripHallucinationsFromString(input),
825+
"The tag called `` is used to track messages. This text should survive.\n",
826+
)
827+
})
828+
817829
test("injectMessageIds skips empty assistant messages to avoid prefill (issue #463)", () => {
818830
const sessionID = "ses_empty_assistant"
819831
const messages: WithParts[] = [

0 commit comments

Comments
 (0)