[GH-ISSUE #2935] [Issue]: Regex scripts: imported scripts only affect the display, trim-strings delete text everywhere, and non-global patterns silently replace only the first match #1513

Closed
opened 2026-10-21 05:37:44 +07:00 by bsakaguchi · 2 comments
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

Four issues in the regex script engine, all confirmed against v2.0.6. Each is independent.


1. Imported SillyTavern regex scripts change only what you see on screen, not what the AI receives

What's wrong
A regex script has a single "Prompt Only" toggle. When it's OFF (the default, and what every imported ST card gets), the script only rewrites the displayed message. The text actually sent to the AI is left untouched. ST has two separate modes (display-only and prompt-only) and a "both" option; Marinara collapses them into one switch with no way to say "apply to both display and prompt." So a typical imported formatting/cleanup script looks like it works but the model still sees the original, untransformed text.

How to reproduce (try it yourself)

  1. Import an ST character card whose embedded regex_scripts entry sets neither markdownOnly nor promptOnly (e.g. { scriptName, findRegex: "/foo/g", replaceString: "bar", placement: [2] }). It imports with Prompt Only = off.
  2. Open a chat and have an AI message that contains foo.
  3. On screen you see: bar.
  4. Open the dry-run / prompt preview for the outgoing request. You'll see: the prompt still contains foo.
  • Expected: the AI receives bar too (or at least there's a way to make a script apply to both display and prompt).

Where it is

  • packages/server/src/services/regex/regex-application.ts:95 — applyRegexScriptsToPromptText does if (!isPromptOnly(script.promptOnly)) continue;, so any non-promptOnly script is skipped for the prompt entirely.
  • packages/shared/src/schemas/regex.schema.ts:26 — promptOnly defaults to false; there is no markdownOnly/"both" field.
  • packages/server/src/services/import/st-character.importer.ts:153 — import only maps promptOnly; ST's markdownOnly is never read.
  • packages/client/src/components/agents/RegexScriptEditor.tsx:747 — single "Prompt Only" switch, no "both" state.
  • Display side mirror: packages/client/src/hooks/use-apply-regex.ts:72-76.

What's happening
promptOnly is treated as a mutually-exclusive flag: false = display-only, true = prompt-only. There is no value that means "apply in both contexts," and the import path can't even produce markdownOnly. Every prompt-side call (generate.routes.ts, chats.routes.ts, dry-run-route.ts) funnels through applyRegexScriptsToPromptText, which unconditionally drops non-promptOnly scripts — so there's no escape hatch.

Suggested fix
Model the three ST states explicitly (e.g. a scope: "display" | "prompt" | "both" field, or keep separate markdownOnly/promptOnly like ST), map both ST flags on import, and add a third UI choice. Make the prompt-side filter run scripts whose scope is prompt or both.


2. trim-strings delete text everywhere in the message, even when the find pattern matched nothing

What's wrong
A script's "trim strings" are removed from the entire message with a plain text delete, regardless of whether the main find/replace actually matched anything and regardless of where the match was. So trim strings can strip text far away from (or with no relation to) the matched region, and they fire even on a zero-match script.

How to reproduce (try it yourself)

  1. Create a script: find = SHOULD_NEVER_MATCH, flags = gi, replace = (empty), trim strings = the.
  2. Apply it to the message: the cat sat on the mat.
  3. You'll see: cat sat on mat (both thes deleted).
  • Expected: unchanged, because the find pattern matched nothing.

Where it is

  • packages/server/src/services/regex/regex-application.ts:121-124 — after the replace, for each trim it does result = result.split(resolvedTrim).join("") with no match-count gate and applied to the whole result.
  • Mirrored on display: packages/client/src/hooks/use-apply-regex.ts:111-114.

What's happening
Trim removal is a global string delete over the full result, decoupled from the regex match. ST applies trim strings only within each matched substring. There's no check that the find regex produced any matches before trimming.

Suggested fix
Apply trim strings only inside the text that the find pattern actually matched (i.e. trim within the replacement region during the .replace callback), and skip trimming entirely when there were zero matches.


3. A regex without the g flag silently replaces only the first match

What's wrong
If a script's flags don't include g, only the first occurrence is replaced. The editor lets you toggle g off with no warning, and the schema's default of gi isn't enforced on scripts that are saved without it — so a user expecting all occurrences to change gets a silent partial replacement.

How to reproduce (try it yourself)

  1. Create a script: find = Bob, flags = i (toggle g off), replace = Alice.
  2. Apply it to: Bob said hi. Bob waved.
  3. You'll see: Alice said hi. Bob waved. (only the first Bob changed).
  • Expected: both replaced (this is how ST behaves), or at least a warning that g is off.

Where it is

  • packages/server/src/services/regex/regex-application.ts:117-118 — new RegExp(findRegex, flags) uses the stored flags verbatim; g is never forced.
  • packages/client/src/components/agents/RegexScriptEditor.tsx:570-589 — the g flag button is freely toggleable with no warning.
  • packages/shared/src/schemas/regex.schema.ts:25 — flags defaults to gi but that default isn't applied to already-stored rows lacking g.
  • Display mirror: packages/client/src/hooks/use-apply-regex.ts:106.

What's happening
This is standard JS String.replace semantics (no g = first match only), so it's not a crash — but it diverges from ST, which applies replacements globally, and it's a silent footgun because nothing surfaces that g is off.

Suggested fix
Either force g when compiling apply-time patterns, or show a clear warning in the editor when g is disabled so the partial-replacement behavior is intentional and visible.


Note: a fourth claim in the original report — that native system-role history messages show raw text on screen but get regex-transformed in the prompt (ChatMessage.tsx:1452 returns raw for isSystem; regex-application.ts:141 maps system→ai_output) — is technically accurate but is kept out of this list as not practically reproducible: it requires both a user-authored system-role history message and a promptOnly ai_output script targeting it, which is an unusual combination, and the narrator path stays self-consistent.

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/2935 Originally assigned to: @SpicyMarinara on GitHub. Four issues in the regex script engine, all confirmed against v2.0.6. Each is independent. --- ### 1. Imported SillyTavern regex scripts change only what you see on screen, not what the AI receives **What's wrong** A regex script has a single "Prompt Only" toggle. When it's OFF (the default, and what every imported ST card gets), the script only rewrites the *displayed* message. The text actually sent to the AI is left untouched. ST has two separate modes (display-only and prompt-only) and a "both" option; Marinara collapses them into one switch with no way to say "apply to both display and prompt." So a typical imported formatting/cleanup script looks like it works but the model still sees the original, untransformed text. **How to reproduce (try it yourself)** 1. Import an ST character card whose embedded `regex_scripts` entry sets neither `markdownOnly` nor `promptOnly` (e.g. `{ scriptName, findRegex: "/foo/g", replaceString: "bar", placement: [2] }`). It imports with **Prompt Only = off**. 2. Open a chat and have an AI message that contains `foo`. 3. On screen you see: `bar`. 4. Open the dry-run / prompt preview for the outgoing request. You'll see: the prompt still contains `foo`. - **Expected:** the AI receives `bar` too (or at least there's a way to make a script apply to both display and prompt). **Where it is** - `packages/server/src/services/regex/regex-application.ts:95` — `applyRegexScriptsToPromptText` does `if (!isPromptOnly(script.promptOnly)) continue;`, so any non-promptOnly script is skipped for the prompt entirely. - `packages/shared/src/schemas/regex.schema.ts:26` — `promptOnly` defaults to `false`; there is no `markdownOnly`/"both" field. - `packages/server/src/services/import/st-character.importer.ts:153` — import only maps `promptOnly`; ST's `markdownOnly` is never read. - `packages/client/src/components/agents/RegexScriptEditor.tsx:747` — single "Prompt Only" switch, no "both" state. - Display side mirror: `packages/client/src/hooks/use-apply-regex.ts:72-76`. **What's happening** `promptOnly` is treated as a mutually-exclusive flag: false = display-only, true = prompt-only. There is no value that means "apply in both contexts," and the import path can't even produce `markdownOnly`. Every prompt-side call (`generate.routes.ts`, `chats.routes.ts`, `dry-run-route.ts`) funnels through `applyRegexScriptsToPromptText`, which unconditionally drops non-promptOnly scripts — so there's no escape hatch. **Suggested fix** Model the three ST states explicitly (e.g. a `scope: "display" | "prompt" | "both"` field, or keep separate `markdownOnly`/`promptOnly` like ST), map both ST flags on import, and add a third UI choice. Make the prompt-side filter run scripts whose scope is `prompt` or `both`. --- ### 2. trim-strings delete text everywhere in the message, even when the find pattern matched nothing **What's wrong** A script's "trim strings" are removed from the *entire* message with a plain text delete, regardless of whether the main find/replace actually matched anything and regardless of where the match was. So trim strings can strip text far away from (or with no relation to) the matched region, and they fire even on a zero-match script. **How to reproduce (try it yourself)** 1. Create a script: find = `SHOULD_NEVER_MATCH`, flags = `gi`, replace = (empty), trim strings = `the`. 2. Apply it to the message: `the cat sat on the mat`. 3. You'll see: `cat sat on mat` (both `the`s deleted). - **Expected:** unchanged, because the find pattern matched nothing. **Where it is** - `packages/server/src/services/regex/regex-application.ts:121-124` — after the replace, for each trim it does `result = result.split(resolvedTrim).join("")` with no match-count gate and applied to the whole `result`. - Mirrored on display: `packages/client/src/hooks/use-apply-regex.ts:111-114`. **What's happening** Trim removal is a global string delete over the full result, decoupled from the regex match. ST applies trim strings only within each matched substring. There's no check that the find regex produced any matches before trimming. **Suggested fix** Apply trim strings only inside the text that the find pattern actually matched (i.e. trim within the replacement region during the `.replace` callback), and skip trimming entirely when there were zero matches. --- ### 3. A regex without the `g` flag silently replaces only the first match **What's wrong** If a script's flags don't include `g`, only the first occurrence is replaced. The editor lets you toggle `g` off with no warning, and the schema's default of `gi` isn't enforced on scripts that are saved without it — so a user expecting all occurrences to change gets a silent partial replacement. **How to reproduce (try it yourself)** 1. Create a script: find = `Bob`, flags = `i` (toggle `g` off), replace = `Alice`. 2. Apply it to: `Bob said hi. Bob waved.` 3. You'll see: `Alice said hi. Bob waved.` (only the first `Bob` changed). - **Expected:** both replaced (this is how ST behaves), or at least a warning that `g` is off. **Where it is** - `packages/server/src/services/regex/regex-application.ts:117-118` — `new RegExp(findRegex, flags)` uses the stored flags verbatim; `g` is never forced. - `packages/client/src/components/agents/RegexScriptEditor.tsx:570-589` — the `g` flag button is freely toggleable with no warning. - `packages/shared/src/schemas/regex.schema.ts:25` — `flags` defaults to `gi` but that default isn't applied to already-stored rows lacking `g`. - Display mirror: `packages/client/src/hooks/use-apply-regex.ts:106`. **What's happening** This is standard JS `String.replace` semantics (no `g` = first match only), so it's not a crash — but it diverges from ST, which applies replacements globally, and it's a silent footgun because nothing surfaces that `g` is off. **Suggested fix** Either force `g` when compiling apply-time patterns, or show a clear warning in the editor when `g` is disabled so the partial-replacement behavior is intentional and visible. --- Note: a fourth claim in the original report — that native system-role history messages show raw text on screen but get regex-transformed in the prompt (`ChatMessage.tsx:1452` returns raw for `isSystem`; `regex-application.ts:141` maps system→ai_output) — is technically accurate but is kept out of this list as not practically reproducible: it requires both a user-authored system-role history message and a promptOnly ai_output script targeting it, which is an unusual combination, and the narrator path stays self-consistent. 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:4827556864 --> @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.
Author
Owner

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

Partial fix landed on staging: regex scripts now have an explicit Apply Mode (Only Prompt / Only Display / Both), legacy promptOnly rows derive the correct mode without auto-saving, and SillyTavern imports can map promptOnly/markdownOnly into the three-state model. Validation: pnpm regression:prompt; pnpm check.\n\nLeaving this issue open because the trim-string semantics and non-global g flag behavior are separate behavior changes and were not changed in this patch.

<!-- gh-comment-id:4827608633 --> @SpicyMarinara commented on GitHub (Jun 28, 2026): Partial fix landed on staging: regex scripts now have an explicit Apply Mode (Only Prompt / Only Display / Both), legacy promptOnly rows derive the correct mode without auto-saving, and SillyTavern imports can map promptOnly/markdownOnly into the three-state model. Validation: pnpm regression:prompt; pnpm check.\n\nLeaving this issue open because the trim-string semantics and non-global `g` flag behavior are separate behavior changes and were not changed in this patch.
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#1513
No description provided.