Skip to content

Commit b2f8660

Browse files
committed
Merge remote-tracking branch 'origin/dev' into pr-596-review
2 parents 9f9a185 + d455619 commit b2f8660

6 files changed

Lines changed: 171 additions & 216 deletions

File tree

lib/hooks.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ const INTERNAL_AGENT_SIGNATURES = [
4646
"Summarize what was done in this conversation",
4747
]
4848

49+
function isInternalAgentCall(systemPrompts: string[]): boolean {
50+
const primaryPrompt = systemPrompts[0]
51+
if (typeof primaryPrompt !== "string" || primaryPrompt.length === 0) {
52+
return false
53+
}
54+
55+
return INTERNAL_AGENT_SIGNATURES.some((signature) => primaryPrompt.includes(signature))
56+
}
57+
4958
export function createSystemPromptHandler(
5059
state: SessionState,
5160
logger: Logger,
@@ -65,8 +74,7 @@ export function createSystemPromptHandler(
6574
return
6675
}
6776

68-
const systemText = output.system.join("\n")
69-
if (INTERNAL_AGENT_SIGNATURES.some((sig) => systemText.includes(sig))) {
77+
if (isInternalAgentCall(output.system)) {
7078
logger.info("Skipping DCP system prompt injection for internal agent")
7179
return
7280
}

lib/messages/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +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 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*$/
1012

1113
const generateStableId = (prefix: string, seed: string): string => {
1214
const hash = createHash("sha256").update(seed).digest("hex").slice(0, SUMMARY_ID_HASH_LENGTH)
@@ -163,7 +165,12 @@ export const replaceBlockIdsWithBlocked = (text: string): string => {
163165
}
164166

165167
export const stripHallucinationsFromString = (text: string): string => {
166-
return text.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, "")
167174
}
168175

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

0 commit comments

Comments
 (0)