mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
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#5826
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?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/3166
Author: @kolacheee
Created: 7/3/2026
Status: ✅ Merged
Merged: 7/3/2026
Merged by: @SpicyMarinara
Base:
staging← Head:fix/3164-regex-macro-display-cost📝 Commits (1)
fb16c96fix(client,shared): stop regex-script macro replacements freezing the UI (#3164)📊 Changes
6 files changed (+151 additions, -25 deletions)
View changed files
📝
packages/client/src/components/chat/ChatArea.tsx(+62 -6)📝
packages/client/src/components/chat/ChatMessage.tsx(+5 -1)📝
packages/client/src/components/chat/ConversationMessage.tsx(+20 -4)📝
packages/client/src/components/game/GameNarration.tsx(+21 -5)📝
packages/client/src/lib/chat-macros.ts(+30 -2)📝
packages/shared/src/utils/macro-engine.ts(+13 -7)📄 Description
Linked issue
Relates to #3164 — please have the reporter confirm on this branch before closing (their profiler produced the 702–3000ms numbers; the benchmarks below reproduce the mechanism, not their exact hardware).
Why this change
#3164 reports severe UI freezes with regex scripts whose replacements contain macros (e.g. replace
thewith a weighted{{random::…}}). Verified legitimate, with the repro benchmarked against current staging. Two compounding causes:useQueriesreturns a fresh result-array identity each render, sochatCharacterRows→characterMapwere rebuilt — new Map, new value objects — on every re-render, plus a guaranteed rebuild every 60s (presence clock) and on every chat metadata write (constant on agent chats). The new identity defeatsmemo(ChatMessage)and invalidates every message'sdisplayContentmemo. 160 mounted messages × 5 scripts ≈ 200–610ms per storm on a fast desktop — the reported 702–3000ms on slower hardware — re-paid on chat open, mid-stream agent-store flips, message invalidations, and the idle 60s tick (the reporter's "lag even outside of generation, bafflingly").Intl.DateTimeFormat(...).formatToParts()(~45% of the pipeline's fixed cost) even with no date macro present, and N scripts sharing one replacement did N identical resolutions. During streaming, the display random seed included message content, so every streamed chunk re-rolled every{{random}}/{{roll}}— the visible text churn in the report.What changed
Four coordinated fixes:
ChatArea.tsx):chatCharIdskeys on the rawcharacterIdsfield;chatCharacterRowsreuses the previous array when every element is reference-unchanged;characterMapreuses its previous identity when a rebuild is value-equal (comparator covers all 14 value fields;avatarCropcompared by value). Unrelated re-renders, presence ticks, and metadata writes that don't change displayed character data no longer re-run the display pipeline for any message, across all three chat surfaces. Genuine changes still propagate.packages/shared/.../macro-engine.ts): the six date/time parts are built only when a date/time macro is present;nowis still captured once per invocation so all six agree on the instant. Output identical (date strings can't contain$sequences).chat-macros.ts): one resolver instance serves all of a message's replacement/trim/pattern strings; identical templates with a fixed (context, seed) resolve identically, so repeats are cache hits. The cache hard-disables from the first variable-op macro onward — conditionals and the{{name}}catch-all can read variables without matching the var-op pattern, so any write poisons cachability for the rest of the instance.ChatMessage.tsx,GameNarration.tsx,ConversationMessage.tsx): seed bymessage.id+ active swipe index (+ segment/part index where applicable) instead of content. Streaming stops re-rolling picks every chunk; swipes keep distinct picks; regenerations (new swipe index) roll fresh.ConversationMessagepreviously passed no seed at all (Math.random()), re-rolling finished messages on every recompute — it now gains stability it never had.Deliberately not done (so reviewers don't ask): per-script
RegExp/isPatternSafecaching — measured at 0.00007ms/0.0005ms per application, three orders of magnitude below the resolver cost; throttling or freezing regex application during streaming — skipping application would leak content that display scripts exist to hide (scripts keep applying to every streamed frame); the deep ChatArea presence-overlay/combinerefactor — follow-up scope, the comparator shims already deliver the identity stability.Measured (tsx harness against this branch vs staging)
{{random::…}}replacement resolution (fresh resolver)Documented behavior deltas
{{random}}picks once on upgrade (picks are render-time only, never persisted; edit/copy paths use raw content). Worth a release-note line.Math.random()rolls (affects multi-script user-input paths; arguably the desired consistency).Validation
pnpm checkpasses locallyCONTRIBUTING.mdManual verification notes
pnpm checkserver build step fails on clean staging on Windows (pre-existing v2.1.0build.mjsunquoted-execPathspawn bug, reproduced with this branch's changes stashed — separate fix needed; not caused by this PR).setvarcase (a cached conditional re-reads correctly after a later variable write), variable write/read sequencing through the cached resolver, same-seed{{random}}determinism across resolver instances, and date/time macros resolving after the lazy Intl construction. (*.test.tsis gitignored per repo convention — matrix kept local.)Still needs manual verification:
{{random::…}}) and confirms the freeze is gone during streaming and idle.{{random}}picks hold steady during a stream and settle once at completion; swiping shows different picks per swipe; presence dots still update (60s schedule transitions and status changes); character edits (name/avatar/colors) still propagate to open chats in all three modes.Docs and release impact
Suggested release-note line: "Displayed {{random}}/{{roll}} results are now stable per message/swipe instead of re-rolling on every render or streamed chunk; existing messages will re-roll their displayed picks once after updating."
UI evidence (if applicable)
No visual change — displayed content is identical except that
{{random}}/{{roll}}picks are now stable during streaming instead of flickering per chunk.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.