Fix stale Diceroll debug blocks on undiced swipes saveReply reuses the message's extra object in place, so swipes generated without a roll (guided swipes, roll failures, extension off) inherited the previous swipe's roll record and kept showing its debug block. Drop the record when a main-type generation finishes unsteered, hide the block as soon as a swipe/regenerate starts instead of leaving it until the result arrives, and skip rendering while an overswipe's generation is in flight.

da6a5b45329915f4f914fee9105096bc892b6b69

permissionBRICK <40219477+permissionBRICK@users.noreply.github.com>

1 files changed, +36 -9Showing whitespace changes
public/scripts/extensions/diceroll/index.js+36 -9
@@ -86,6 +86,11 @@ let isRolling = false;
8686// Roll data waiting to be stamped onto the message the steered generation produces.
8787let pendingStamp = null;
8888
89+// Message id stamped by the current generation. MESSAGE_RECEIVED consumes the pending stamp and
90+// CHARACTER_MESSAGE_RENDERED fires right after for the same message; without this marker the
91+// second event would mistake the freshly stamped message for an unsteered one and wipe it.
92+let stampedMesId = null;
93+
8994// Whether the main generation being intercepted was started with its own custom prompt
9095// (e.g. a swipe/regenerate triggered with an additional instruction). Tracked via
9196// GENERATION_STARTED because the interceptor does not receive the generation params.
@@ -316,6 +321,14 @@ async function onGenerationIntercept(coreChat, _contextSize, _abort, type) {
316321 return;
317322 }
318323
324+ stampedMesId = null;
325+
326+ // The message being replaced still shows the previous swipe's debug block; hide it as soon as
327+ // the new generation starts instead of leaving it stuck until the result arrives.
328+ if (type === 'swipe' || type === 'regenerate') {
329+ $(`#chat .mes[mesid="${chat.length - 1}"] .diceroll_debug`).remove();
330+ }
331+
319332 // A guided swipe/response (or any generation carrying its own instruction) takes precedence:
320333 // no roll, no direction injection — the manual instruction alone steers this generation.
321334 if (hasManualSteering(s)) {
@@ -379,21 +392,27 @@ async function onGenerationIntercept(coreChat, _contextSize, _abort, type) {
379392
380393/**
381394 * Copies the roll that steered a finished generation onto the produced message, so the debug view
382395 * stays correct per message and survives reloads. When a main-type generation finishes WITHOUT a
396+ * roll (extension disabled, roll failed, or a manual instruction steered it), the roll record
397+ * inherited in place from the previous swipe's extra is dropped instead.
383398 * @param {number} chatId Message index
399+ * @param {string} type Generation type the message event was emitted with
384400 */
385401function stampMessage(chatId, type) {
386- if (!pendingStamp) {
387- return;
388- }
389402 const message = chat[chatId];
390403 if (!message || message.is_user || message.is_system) {
391404 return;
392405 }
406+ if (pendingStamp) {
393407 message.extra = message.extra || {};
394408 message.extra.diceroll = structuredClone(pendingStamp);
395409 pendingStamp = null;
410+ stampedMesId = chatId;
396411 saveChatDebounced();
412+ } else if (STEERED_TYPES.has(type) && stampedMesId !== chatId && message.extra?.diceroll) {
413+ delete message.extra.diceroll;
414+ saveChatDebounced();
415+ }
397416}
398417
399418function renderDebugForMessage(chatId) {
@@ -403,11 +422,18 @@ function renderDebugForMessage(chatId) {
403422 }
404423 mesElement.find('.diceroll_debug').remove();
405424
406425 const datamessage = chat[chatId]?.extra?.diceroll;
426+ const data = message?.extra?.diceroll;
407427 if (!getSettings().debugDisplay || !data || !Array.isArray(data.options)) {
408428 return;
409429 }
410430
431+ // An overswipe points swipe_id one past the existing swipes while its generation is running;
432+ // the roll record on the message still belongs to the previous swipe then, so show nothing.
433+ if (typeof message.swipe_id === 'number' && Array.isArray(message.swipes) && message.swipe_id >= message.swipes.length) {
434+ return;
435+ }
436+
411437 const chosen = data.options[data.chosenIndex];
412438 const details = $('<details class="diceroll_debug"></details>');
413439 const rollInfo = Number.isFinite(data.roll) ? `, roll ${data.roll.toFixed(1)}/${formatProbability(data.total)}` : '';
@@ -531,14 +557,15 @@ async function init() {
531557 }
532558 });
533559
534560 eventSource.on(event_types.MESSAGE_RECEIVED, (chatId, type) => stampMessage(chatId, type));
535561 eventSource.on(event_types.CHARACTER_MESSAGE_RENDERED, (chatId, type) => {
536562 stampMessage(chatId, type);
537563 renderDebugForMessage(chatId);
538564 });
539565 eventSource.on(event_types.MESSAGE_SWIPED, (chatId) => renderDebugForMessage(chatId));
540566 eventSource.on(event_types.CHAT_CHANGED, () => {
541567 pendingStamp = null;
568+ stampedMesId = null;
542569 clearDirectionInjection();
543570 renderAllDebug();
544571 });