mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[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
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1513
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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)
regex_scriptsentry sets neithermarkdownOnlynorpromptOnly(e.g.{ scriptName, findRegex: "/foo/g", replaceString: "bar", placement: [2] }). It imports with Prompt Only = off.foo.bar.foo.bartoo (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—applyRegexScriptsToPromptTextdoesif (!isPromptOnly(script.promptOnly)) continue;, so any non-promptOnly script is skipped for the prompt entirely.packages/shared/src/schemas/regex.schema.ts:26—promptOnlydefaults tofalse; there is nomarkdownOnly/"both" field.packages/server/src/services/import/st-character.importer.ts:153— import only mapspromptOnly; ST'smarkdownOnlyis never read.packages/client/src/components/agents/RegexScriptEditor.tsx:747— single "Prompt Only" switch, no "both" state.packages/client/src/hooks/use-apply-regex.ts:72-76.What's happening
promptOnlyis 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 producemarkdownOnly. Every prompt-side call (generate.routes.ts,chats.routes.ts,dry-run-route.ts) funnels throughapplyRegexScriptsToPromptText, 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 separatemarkdownOnly/promptOnlylike ST), map both ST flags on import, and add a third UI choice. Make the prompt-side filter run scripts whose scope ispromptorboth.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)
SHOULD_NEVER_MATCH, flags =gi, replace = (empty), trim strings =the.the cat sat on the mat.cat sat on mat(boththes deleted).Where it is
packages/server/src/services/regex/regex-application.ts:121-124— after the replace, for each trim it doesresult = result.split(resolvedTrim).join("")with no match-count gate and applied to the wholeresult.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
.replacecallback), and skip trimming entirely when there were zero matches.3. A regex without the
gflag silently replaces only the first matchWhat's wrong
If a script's flags don't include
g, only the first occurrence is replaced. The editor lets you togglegoff with no warning, and the schema's default ofgiisn'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)
Bob, flags =i(togglegoff), replace =Alice.Bob said hi. Bob waved.Alice said hi. Bob waved.(only the firstBobchanged).gis off.Where it is
packages/server/src/services/regex/regex-application.ts:117-118—new RegExp(findRegex, flags)uses the stored flags verbatim;gis never forced.packages/client/src/components/agents/RegexScriptEditor.tsx:570-589— thegflag button is freely toggleable with no warning.packages/shared/src/schemas/regex.schema.ts:25—flagsdefaults togibut that default isn't applied to already-stored rows lackingg.packages/client/src/hooks/use-apply-regex.ts:106.What's happening
This is standard JS
String.replacesemantics (nog= 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 thatgis off.Suggested fix
Either force
gwhen compiling apply-time patterns, or show a clear warning in the editor whengis 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:1452returns raw forisSystem;regex-application.ts:141maps 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.
@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.
@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
gflag behavior are separate behavior changes and were not changed in this patch.