[GH-ISSUE #2932] [Issue]: Regex scripts apply inconsistently in group chats, previews, idle-delay sends, and on user input #1509

Closed
opened 2026-10-21 05:37:38 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 28, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2932

Originally assigned to: @SpicyMarinara on GitHub.

The regex script engine applies find/replace transforms in several separate passes that don't line up. As a result, scoped (per-character) scripts and global scripts get skipped or double-counted in specific, reproducible situations: group chats, prompt previews, idle/away-delay sends, depth-gated display, and live user input. Each sub-bug below is independently confirmed against v2.0.6 with a concrete repro.

All of these were verified by tracing the code; please confirm the reproduction before fixing.


A. Idle/away "delayed" sends drop ALL prompt-side regex

What's wrong: When a character is idle/DND/offline and the response is delayed (the "will respond in a moment" path), every prompt-side regex script is silently skipped. A non-delayed send to the same character applies them normally.

How to reproduce:

  1. In a conversation chat, set a character's presence to idle (so the response delay kicks in).
  2. Add a global script: placement ai_output, promptOnly = true, find \bfoo\b, replace bar. Make sure assistant history contains the word foo.
  3. Send a normal (non-delayed) message and peek the prompt/logs — you'll see bar.
  4. Now trigger the idle-delayed send and peek the prompt.

You'll see: the delayed prompt still contains foo.
Expected: it should contain bar, same as the non-delayed send.

Where it is: generate.routes.ts, the delay branch around lines 2516-2530. After the delay it re-reads the DB and rebuilds finalMessages from raw conversationPromptHistoryContent, then runs macro resolution only (2530) — no regex call follows.

What's happening: The only global-regex pass is the iteration-0 pass at line 2043, which runs on mappedMessages. In the normal path finalMessages is derived from that already-rewritten array. But the delay branch overwrites finalMessages with fresh raw content and never re-applies regex. The downstream per-character pass (6117) is targetedOnly:true, so it skips globals. Result: regex is lost only when the character is away.

Suggested fix: After rebuilding finalMessages in the delay branch, re-run applyRegexScriptsToPromptMessages with the same options used at line 2043 before macro resolution.

(This one is harder to trigger on demand — it depends on the character status and the 1-5 minute delay window — but the code path is unambiguous.)


B. Character-scoped depth is measured against the trailing instruction, not the last chat message

What's wrong: For a per-character (scoped) script, maxDepth:0 is supposed to mean "the most recent chat message." Instead it points at the trailing "Respond ONLY as X" system instruction that gets appended last, so the script never hits the real last message. A global maxDepth:0 script works correctly, which proves the divergence.

How to reproduce:

  1. Sequential group chat (no specific target character).
  2. Scoped script: targetCharacterIds=[charB], placement ai_output, maxDepth:0, find text on charB's last line.
  3. Send. It never matches.
  4. Make the same script global (remove targetCharacterIds) and it matches the last message correctly.

Where it is: depth is totalMessages - 1 - index (regex-application.ts:142). The scoped/targeted pass runs at generate.routes.ts:6117 over an array that by then includes unshifted leading system/context blocks (3472) and the trailing "Respond ONLY as X" instruction pushed at 7239. So depth 0 = that trailing instruction.

What's happening: The global iteration-0 pass (2043) measured depth on a history-only array, so depth 0 was the last real message there. The scoped per-character pass measures depth on the fully-assembled prompt, shifting every depth by the number of leading/trailing blocks. The two passes disagree about what "depth 0" means.

Suggested fix: Compute regex depth from the original chat-history index, independent of injected leading/trailing prompt blocks, so scoped and global passes agree.


C. Merged group chats apply scoped regex only for the first character

What's wrong: In a merged group reply, character-scoped scripts only ever run for the first character in the group. Scripts scoped to any other member never apply.

How to reproduce:

  1. Merged group chat with A + B (no specific target character).
  2. Scoped script: targetCharacterIds=[charB], promptOnly = true, find X, replace Y, with X present in history.
  3. Send.

You'll see: the merged prompt keeps X.
Expected: charB's scoped script should rewrite it to Y, since charB participates.

Where it is: merged mode sets targetCharId = characterIds[0] (generate.routes.ts:7273), then the per-character targeted pass (6116-6121) runs with that first id only. For a group send without forCharacterId, promptTargetCharacterId is null (1974-1977), so the iteration-0 pass also skips scoped scripts (regex-application.ts:100-102).

Suggested fix: In merged mode, run the targeted scoped pass for every participating character id, not just characterIds[0].


D. Individual group loop never applies globals to earlier characters' in-turn replies

What's wrong: In an individual-mode group, when character A replies and then B's prompt is assembled, A's just-generated reply is inserted raw — global regex scripts never run over it. So B sees A's untransformed text.

How to reproduce:

  1. Individual-mode group A, B (no specific target character).
  2. Global script: promptOnly = true, find Voldemort, replace [REDACTED].
  3. Send; A's response contains Voldemort.
  4. Peek B's assembled prompt.

You'll see: B's prompt still contains Voldemort.
Expected: it should be [REDACTED].

Where it is: generate.routes.ts:7263 pushes genResult.response raw into runningMessages. The next character's generateForCharacter only runs the targetedOnly:true pass (6116-6121), which skips globals. Contrast the Professor Mari follow-up at 10957, which deliberately runs a global (non-targeted) regex pass on the freshly-appended turn — with a comment (10946-10949) explaining exactly this gap. The group loop is the unprotected sibling.

Suggested fix: Apply the same global regex pass to each character's response before it's appended to runningMessages, mirroring the Mari follow-up at 10957.


E. Prompt depth and on-screen display depth are computed over different arrays

What's wrong: A depth-gated display script (e.g. maxDepth:3) can apply in the prompt but be skipped on screen (or vice-versa) when there's a hidden-from-AI message in the chat, because the server and client count depth over different message lists.

How to reproduce:

  1. A chat with 6 visible messages, one of which is hidden-from-AI.
  2. An ai_output display script (promptOnly = false) with maxDepth:3.
  3. Look at an older message that lands at server-depth 3 vs client-depth 4.

You'll see: that message is transformed in the prompt but not on screen (depths disagree by 1+).
Expected: prompt and display agree on the message's depth.

Where it is: server depth is over chatMessages, which filters hiddenFromAI (generate.routes.ts:1607-1609) and applies contextMessageLimit (1657-1658), then depth = totalMessages-1-index. Client depth is (messages?.length ?? 0) - 1 - sourceIndex over the FULL transcript (ChatRoleplaySurface.tsx:1793), passed into applyToAIOutput (ChatMessage.tsx:1455). For any message older than a hidden-from-AI message, client depth = server depth + 1.

Suggested fix: Compute client display depth over the same AI-visible (hidden-filtered, context-limited) message set the server uses, so depth gates line up.


F. Send-time user_input regex ignores minDepth/maxDepth and bakes the result into the stored message

What's wrong: A user_input script with depth limits is applied to the message you just typed regardless of its depth, and the rewritten text is what gets saved and sent — there's no way to scope it by depth on the live turn.

How to reproduce:

  1. user_input script: promptOnly = false, minDepth:1, find \bteh\b, replace the.
  2. Type teh and send.

You'll see: the stored/sent message is the.
Expected: depth 0 (the live turn) is below minDepth:1, so it should have been left as teh.

Where it is: applyToUserInput is called with only {resolveMacros, scopedMode} and no depth (ChatInput.tsx:723-726 and 908-911; ConversationInput.tsx:769-772). The depth gate is if (options?.depth != null) (use-apply-regex.ts:98), so it's skipped entirely. The rewritten message is what gets persisted (createMessage content, ChatInput.tsx:757) and sent (userMessage, 778). Because the server applies promptOnly scripts only (regex-application.ts:95) and display renders user messages raw (ChatMessage.tsx:1452-1453), this send-time path is the ONLY application path for a non-promptOnly user_input script.

Suggested fix: Pass depth: 0 to applyToUserInput so depth gates apply to the live turn, or document that non-promptOnly user_input scripts intentionally ignore depth on send.


G. Character-scoped user_input scripts can never apply correctly on send

What's wrong: A user_input script scoped to a specific character either never runs (exclusive mode) or runs on every user message regardless of target (chat mode) — it can never apply per-character the way the equivalent ai_output path does.

How to reproduce:

  1. user_input script: targetCharacterIds=[X], promptOnly = false.
  2. Set the chat's scoped regex mode to exclusive → the script never runs on any user message.
  3. Set it to chat → the script runs on all user messages, ignoring the target.

(Default mode disabled suppresses it entirely, so this only shows under a non-default scoped mode.)

Where it is: applyToUserInput never passes characterId (ChatInput.tsx:723/908, ConversationInput.tsx:769). In exclusive mode the gate if (!charId || !script.targetCharacterIds.includes(charId)) continue (use-apply-regex.ts:90-92) always skips because charId is undefined; chat mode (87-89) has no target test, so it runs on everything. Contrast ChatMessage.tsx:1458, which DOES pass characterId for ai_output.

Suggested fix: Either pass a meaningful character context into applyToUserInput, or explicitly disallow target-scoping for user_input scripts so the behavior is predictable.


H. Dry-run and peek previews drop character-scoped scripts that the real send applies

What's wrong: The prompt preview (peek / dry-run) for a group chat omits character-scoped regex transforms that the actual send does perform, so the preview doesn't match what's really sent.

How to reproduce:

  1. Group chat with no cached prompt yet (fresh chat).
  2. Scoped script: targetCharacterIds=[charA], find X, replace Y.
  3. Open peek-prompt (live preview) or run a dry-run without a specific target character → it shows X.
  4. Do the real send to charA → it produces Y.

Where it is: dry-run-route.ts:872-875 applies regex once with targetCharacterId = promptTargetCharacterId and no targeted pass; when forCharacterId is absent, that id is null (754-757), so scoped scripts skip (regex-application.ts:100-102). The peek fallback (chats.routes.ts:2137-2139, in the explicit "no generation has happened yet" branch at 2015) passes neither targetCharacterId nor targetedOnly, dropping all scoped scripts. The real group send applies them via the per-character targeted pass (generate.routes.ts:6116-6121).

Suggested fix: Make the preview paths run the same per-character targeted scoped pass the real send uses, so previews reflect scoped scripts.


AI-investigated against v2.0.6; verify the reproduction before fixing.

Originally created by @kolacheee on GitHub (Jun 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2932 Originally assigned to: @SpicyMarinara on GitHub. The regex script engine applies find/replace transforms in several separate passes that don't line up. As a result, scoped (per-character) scripts and global scripts get skipped or double-counted in specific, reproducible situations: group chats, prompt previews, idle/away-delay sends, depth-gated display, and live user input. Each sub-bug below is independently confirmed against v2.0.6 with a concrete repro. All of these were verified by tracing the code; please confirm the reproduction before fixing. --- ## A. Idle/away "delayed" sends drop ALL prompt-side regex **What's wrong:** When a character is idle/DND/offline and the response is delayed (the "will respond in a moment" path), every prompt-side regex script is silently skipped. A non-delayed send to the same character applies them normally. **How to reproduce:** 1. In a conversation chat, set a character's presence to idle (so the response delay kicks in). 2. Add a global script: placement `ai_output`, promptOnly = true, find `\bfoo\b`, replace `bar`. Make sure assistant history contains the word `foo`. 3. Send a normal (non-delayed) message and peek the prompt/logs — you'll see `bar`. 4. Now trigger the idle-delayed send and peek the prompt. **You'll see:** the delayed prompt still contains `foo`. **Expected:** it should contain `bar`, same as the non-delayed send. **Where it is:** `generate.routes.ts`, the delay branch around lines 2516-2530. After the delay it re-reads the DB and rebuilds `finalMessages` from raw `conversationPromptHistoryContent`, then runs macro resolution only (2530) — no regex call follows. **What's happening:** The only global-regex pass is the iteration-0 pass at line 2043, which runs on `mappedMessages`. In the normal path `finalMessages` is derived from that already-rewritten array. But the delay branch overwrites `finalMessages` with fresh raw content and never re-applies regex. The downstream per-character pass (6117) is `targetedOnly:true`, so it skips globals. Result: regex is lost only when the character is away. **Suggested fix:** After rebuilding `finalMessages` in the delay branch, re-run `applyRegexScriptsToPromptMessages` with the same options used at line 2043 before macro resolution. (This one is harder to trigger on demand — it depends on the character status and the 1-5 minute delay window — but the code path is unambiguous.) --- ## B. Character-scoped depth is measured against the trailing instruction, not the last chat message **What's wrong:** For a per-character (scoped) script, `maxDepth:0` is supposed to mean "the most recent chat message." Instead it points at the trailing "Respond ONLY as X" system instruction that gets appended last, so the script never hits the real last message. A global `maxDepth:0` script works correctly, which proves the divergence. **How to reproduce:** 1. Sequential group chat (no specific target character). 2. Scoped script: `targetCharacterIds=[charB]`, placement `ai_output`, `maxDepth:0`, find text on charB's last line. 3. Send. It never matches. 4. Make the same script global (remove `targetCharacterIds`) and it matches the last message correctly. **Where it is:** depth is `totalMessages - 1 - index` (`regex-application.ts:142`). The scoped/targeted pass runs at `generate.routes.ts:6117` over an array that by then includes unshifted leading system/context blocks (3472) and the trailing "Respond ONLY as X" instruction pushed at 7239. So depth 0 = that trailing instruction. **What's happening:** The global iteration-0 pass (2043) measured depth on a history-only array, so depth 0 was the last real message there. The scoped per-character pass measures depth on the fully-assembled prompt, shifting every depth by the number of leading/trailing blocks. The two passes disagree about what "depth 0" means. **Suggested fix:** Compute regex depth from the original chat-history index, independent of injected leading/trailing prompt blocks, so scoped and global passes agree. --- ## C. Merged group chats apply scoped regex only for the first character **What's wrong:** In a merged group reply, character-scoped scripts only ever run for the first character in the group. Scripts scoped to any other member never apply. **How to reproduce:** 1. Merged group chat with A + B (no specific target character). 2. Scoped script: `targetCharacterIds=[charB]`, promptOnly = true, find `X`, replace `Y`, with `X` present in history. 3. Send. **You'll see:** the merged prompt keeps `X`. **Expected:** charB's scoped script should rewrite it to `Y`, since charB participates. **Where it is:** merged mode sets `targetCharId = characterIds[0]` (`generate.routes.ts:7273`), then the per-character targeted pass (6116-6121) runs with that first id only. For a group send without `forCharacterId`, `promptTargetCharacterId` is null (1974-1977), so the iteration-0 pass also skips scoped scripts (`regex-application.ts:100-102`). **Suggested fix:** In merged mode, run the targeted scoped pass for every participating character id, not just `characterIds[0]`. --- ## D. Individual group loop never applies globals to earlier characters' in-turn replies **What's wrong:** In an individual-mode group, when character A replies and then B's prompt is assembled, A's just-generated reply is inserted raw — global regex scripts never run over it. So B sees A's untransformed text. **How to reproduce:** 1. Individual-mode group A, B (no specific target character). 2. Global script: promptOnly = true, find `Voldemort`, replace `[REDACTED]`. 3. Send; A's response contains `Voldemort`. 4. Peek B's assembled prompt. **You'll see:** B's prompt still contains `Voldemort`. **Expected:** it should be `[REDACTED]`. **Where it is:** `generate.routes.ts:7263` pushes `genResult.response` raw into `runningMessages`. The next character's `generateForCharacter` only runs the `targetedOnly:true` pass (6116-6121), which skips globals. Contrast the Professor Mari follow-up at 10957, which deliberately runs a global (non-targeted) regex pass on the freshly-appended turn — with a comment (10946-10949) explaining exactly this gap. The group loop is the unprotected sibling. **Suggested fix:** Apply the same global regex pass to each character's response before it's appended to `runningMessages`, mirroring the Mari follow-up at 10957. --- ## E. Prompt depth and on-screen display depth are computed over different arrays **What's wrong:** A depth-gated display script (e.g. `maxDepth:3`) can apply in the prompt but be skipped on screen (or vice-versa) when there's a hidden-from-AI message in the chat, because the server and client count depth over different message lists. **How to reproduce:** 1. A chat with 6 visible messages, one of which is hidden-from-AI. 2. An `ai_output` display script (promptOnly = false) with `maxDepth:3`. 3. Look at an older message that lands at server-depth 3 vs client-depth 4. **You'll see:** that message is transformed in the prompt but not on screen (depths disagree by 1+). **Expected:** prompt and display agree on the message's depth. **Where it is:** server depth is over `chatMessages`, which filters hiddenFromAI (`generate.routes.ts:1607-1609`) and applies `contextMessageLimit` (1657-1658), then `depth = totalMessages-1-index`. Client depth is `(messages?.length ?? 0) - 1 - sourceIndex` over the FULL transcript (`ChatRoleplaySurface.tsx:1793`), passed into `applyToAIOutput` (`ChatMessage.tsx:1455`). For any message older than a hidden-from-AI message, client depth = server depth + 1. **Suggested fix:** Compute client display depth over the same AI-visible (hidden-filtered, context-limited) message set the server uses, so depth gates line up. --- ## F. Send-time user_input regex ignores minDepth/maxDepth and bakes the result into the stored message **What's wrong:** A `user_input` script with depth limits is applied to the message you just typed regardless of its depth, and the rewritten text is what gets saved and sent — there's no way to scope it by depth on the live turn. **How to reproduce:** 1. `user_input` script: promptOnly = false, `minDepth:1`, find `\bteh\b`, replace `the`. 2. Type `teh` and send. **You'll see:** the stored/sent message is `the`. **Expected:** depth 0 (the live turn) is below `minDepth:1`, so it should have been left as `teh`. **Where it is:** `applyToUserInput` is called with only `{resolveMacros, scopedMode}` and no `depth` (`ChatInput.tsx:723-726` and `908-911`; `ConversationInput.tsx:769-772`). The depth gate is `if (options?.depth != null)` (`use-apply-regex.ts:98`), so it's skipped entirely. The rewritten message is what gets persisted (createMessage content, `ChatInput.tsx:757`) and sent (userMessage, 778). Because the server applies promptOnly scripts only (`regex-application.ts:95`) and display renders user messages raw (`ChatMessage.tsx:1452-1453`), this send-time path is the ONLY application path for a non-promptOnly user_input script. **Suggested fix:** Pass `depth: 0` to `applyToUserInput` so depth gates apply to the live turn, or document that non-promptOnly user_input scripts intentionally ignore depth on send. --- ## G. Character-scoped user_input scripts can never apply correctly on send **What's wrong:** A `user_input` script scoped to a specific character either never runs (exclusive mode) or runs on every user message regardless of target (chat mode) — it can never apply per-character the way the equivalent ai_output path does. **How to reproduce:** 1. `user_input` script: `targetCharacterIds=[X]`, promptOnly = false. 2. Set the chat's scoped regex mode to `exclusive` → the script never runs on any user message. 3. Set it to `chat` → the script runs on all user messages, ignoring the target. (Default mode `disabled` suppresses it entirely, so this only shows under a non-default scoped mode.) **Where it is:** `applyToUserInput` never passes `characterId` (`ChatInput.tsx:723/908`, `ConversationInput.tsx:769`). In exclusive mode the gate `if (!charId || !script.targetCharacterIds.includes(charId)) continue` (`use-apply-regex.ts:90-92`) always skips because `charId` is undefined; chat mode (87-89) has no target test, so it runs on everything. Contrast `ChatMessage.tsx:1458`, which DOES pass `characterId` for ai_output. **Suggested fix:** Either pass a meaningful character context into `applyToUserInput`, or explicitly disallow target-scoping for user_input scripts so the behavior is predictable. --- ## H. Dry-run and peek previews drop character-scoped scripts that the real send applies **What's wrong:** The prompt preview (peek / dry-run) for a group chat omits character-scoped regex transforms that the actual send does perform, so the preview doesn't match what's really sent. **How to reproduce:** 1. Group chat with no cached prompt yet (fresh chat). 2. Scoped script: `targetCharacterIds=[charA]`, find `X`, replace `Y`. 3. Open peek-prompt (live preview) or run a dry-run without a specific target character → it shows `X`. 4. Do the real send to charA → it produces `Y`. **Where it is:** `dry-run-route.ts:872-875` applies regex once with `targetCharacterId = promptTargetCharacterId` and no targeted pass; when `forCharacterId` is absent, that id is null (754-757), so scoped scripts skip (`regex-application.ts:100-102`). The peek fallback (`chats.routes.ts:2137-2139`, in the explicit "no generation has happened yet" branch at 2015) passes neither `targetCharacterId` nor `targetedOnly`, dropping all scoped scripts. The real group send applies them via the per-character targeted pass (`generate.routes.ts:6116-6121`). **Suggested fix:** Make the preview paths run the same per-character targeted scoped pass the real send uses, so previews reflect scoped scripts. --- AI-investigated against v2.0.6; verify the reproduction before fixing.
Author
Owner

@kolacheee commented on GitHub (Jun 28, 2026):

Re-verified this against v2.0.6 (read the code + reproduced each part) and rewrote the description in plain language with concrete, try-it-yourself reproduction steps. Any original claims I could not actually reproduce were dropped (and called out at the end where relevant). Should be much easier to follow and confirm now — sorry the first version was so dense.

<!-- gh-comment-id:4827556741 --> @kolacheee commented on GitHub (Jun 28, 2026): Re-verified this against v2.0.6 (read the code + reproduced each part) and rewrote the description in plain language with concrete, try-it-yourself reproduction steps. Any original claims I could not actually reproduce were dropped (and called out at the end where relevant). Should be much easier to follow and confirm now — sorry the first version was so dense.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1509
No description provided.