Skip to content

Commit 264aa0d

Browse files
authored
Merge pull request #601 from LHMQ878/fix/556-injected-tag-suffix
fix: strip injected message-id suffix before paired tag regex
2 parents 35616fb + 25417d2 commit 264aa0d

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

lib/messages/utils.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ 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 HALLUCINATED_PARAMETER_SUFFIX_REGEX = /\nm\d+<\/parameter>\s*$/
10+
const INJECTED_MESSAGE_ID_SUFFIX_REGEX = /(?<=\n)<dcp-message-id[^>]*>m\d+<\/dcp-message-id>\s*$/
11+
const HALLUCINATED_PARAMETER_SUFFIX_REGEX = /(?<=\n)m\d+<\/parameter>\s*$/
1112

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

166167
export const stripHallucinationsFromString = (text: string): string => {
167-
const withoutHallucinatedParameter = text.replace(HALLUCINATED_PARAMETER_SUFFIX_REGEX, "")
168-
return withoutHallucinatedParameter.replace(DCP_PAIRED_TAG_REGEX, "").replace(DCP_UNPAIRED_TAG_REGEX, "")
168+
const withoutKnownSuffixes = text
169+
.replace(INJECTED_MESSAGE_ID_SUFFIX_REGEX, "")
170+
.replace(HALLUCINATED_PARAMETER_SUFFIX_REGEX, "")
171+
return withoutKnownSuffixes
172+
.replace(DCP_PAIRED_TAG_REGEX, "")
173+
.replace(DCP_UNPAIRED_TAG_REGEX, "")
169174
}
170175

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

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/message-priority.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,30 @@ 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\n",
826+
)
827+
})
828+
829+
test("hallucination stripping handles priority on injected message-id suffixes", () => {
830+
const input =
831+
"The tag called `<dcp-message-id>` is used to track messages. " +
832+
"This text should survive.\n\n" +
833+
'<dcp-message-id priority="low">m0370</dcp-message-id>'
834+
835+
assert.equal(
836+
stripHallucinationsFromString(input),
837+
"The tag called `` is used to track messages. This text should survive.\n\n",
838+
)
839+
})
840+
817841
test("hallucination stripping removes trailing mXXXX</parameter> artifact (issue #555)", () => {
818842
assert.equal(
819843
stripHallucinationsFromString("Total: maybe 20 lines changed.\n\nm0340</parameter>\n\n"),

0 commit comments

Comments
 (0)