From e712ededc9245e84aa4e336e0f82121c092ff1e4 Mon Sep 17 00:00:00 2001 From: "Aleks L.T" Date: Mon, 28 Jul 2025 20:54:32 +0200 Subject: [PATCH] =?UTF-8?q?fixes=20#4324:=20injectedIndices=20becomes=20in?= =?UTF-8?q?validated=20with=20a=20off-by-one=20in=E2=80=A6=20(#4325)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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> --- public/script.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/public/script.js b/public/script.js index c92d2d7aa..14ecee400 100644 --- a/public/script.js +++ b/public/script.js @@ -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));