fixes #4324: injectedIndices becomes invalidated with a off-by-one in… (#4325)

* fixes #4324: injectedIndices becomes invalidated with a off-by-one index shift when PHI/jailbreak message is added. Could lead to dropped injections/history.

* Run formatter

* Fix pre-allocation of injections in Text Completions prompt builder

---------

Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
This commit is contained in:
Aleks L.T
2025-07-28 21:54:32 +03:00
committed by GitHub
co-authored by Cohee
parent 086f873f2f
commit e712ededc9
+4 -1
View File
@@ -3796,7 +3796,10 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
coreChat.splice(coreChat.length - 1, 0, { mes: jailbreak, is_user: true });
}
else {
// This operation will result in the injectedIndices indexes being off by one
coreChat.push({ mes: jailbreak, is_user: true });
// Add +1 to the elements to correct for the new PHI/Jailbreak message.
injectedIndices.forEach((e, idx) => injectedIndices[idx] = e + 1);
}
}
}
@@ -4757,7 +4760,7 @@ async function doChatInject(messages, isContinue) {
if (roleMessages.length) {
const depth = isContinue && i === 0 ? 1 : i;
const injectIdx = depth + totalInsertedMessages;
const injectIdx = Math.min(depth + totalInsertedMessages, messages.length);
messages.splice(injectIdx, 0, ...roleMessages);
totalInsertedMessages += roleMessages.length;
injectedIndices.push(...Array.from({ length: roleMessages.length }, (_, i) => injectIdx + i));