Skip to content

Commit 35616fb

Browse files
authored
Merge pull request #600 from LHMQ878/fix/555-hallucinated-parameter-suffix
fix: strip trailing mXXXX</parameter> hallucinations
2 parents 042010a + acb1fdc commit 35616fb

2 files changed

Lines changed: 10 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 HALLUCINATED_PARAMETER_SUFFIX_REGEX = /\nm\d+<\/parameter>\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 withoutHallucinatedParameter = text.replace(HALLUCINATED_PARAMETER_SUFFIX_REGEX, "")
168+
return withoutHallucinatedParameter.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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,13 @@ test("hallucination stripping does not affect non-dcp tags", async () => {
814814
)
815815
})
816816

817+
test("hallucination stripping removes trailing mXXXX</parameter> artifact (issue #555)", () => {
818+
assert.equal(
819+
stripHallucinationsFromString("Total: maybe 20 lines changed.\n\nm0340</parameter>\n\n"),
820+
"Total: maybe 20 lines changed.\n\n",
821+
)
822+
})
823+
817824
test("injectMessageIds skips empty assistant messages to avoid prefill (issue #463)", () => {
818825
const sessionID = "ses_empty_assistant"
819826
const messages: WithParts[] = [

0 commit comments

Comments
 (0)