[PR #736] [MERGED] Fix macro resolution in lorebooks and regex scripts #4754

Closed
opened 2026-10-22 04:10:22 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/736
Author: @Xelvanis
Created: 5/12/2026
Status: ✅ Merged
Merged: 5/12/2026
Merged by: @Xelvanis

Base: main ← Head: fix/lorebook-regex-macros


📝 Commits (2)

  • eab2ff7 fix: resolve macros in lorebooks and regex scripts
  • b670375 Address lorebook macro review comments

📊 Changes

22 files changed (+1299 additions, -196 deletions)

View changed files

📝 packages/client/src/components/agents/RegexScriptEditor.tsx (+44 -8)
📝 packages/client/src/components/chat/ChatInput.tsx (+6 -5)
📝 packages/client/src/components/chat/ChatMessage.tsx (+9 -4)
📝 packages/client/src/components/chat/ConversationInput.tsx (+17 -11)
📝 packages/client/src/components/game/GameNarration.tsx (+16 -6)
📝 packages/client/src/hooks/use-apply-regex.ts (+18 -8)
📝 packages/client/src/lib/chat-macros.ts (+18 -2)
📝 packages/server/src/routes/chats.routes.ts (+8 -4)
📝 packages/server/src/routes/game.routes.ts (+43 -24)
📝 packages/server/src/routes/generate.routes.ts (+23 -34)
📝 packages/server/src/routes/generate/dry-run-route.ts (+17 -23)
📝 packages/server/src/routes/lorebooks.routes.ts (+44 -1)
📝 packages/server/src/services/lorebook/index.ts (+321 -15)
📝 packages/server/src/services/lorebook/keyword-scanner.ts (+8 -3)
📝 packages/server/src/services/prompt/assembler.ts (+13 -8)
📝 packages/server/src/services/prompt/index.ts (+2 -0)
📝 packages/server/src/services/prompt/macro-context.ts (+34 -2)
📝 packages/server/src/services/prompt/marker-expander.ts (+47 -30)
📝 packages/server/src/services/regex/regex-application.ts (+18 -5)
📝 packages/server/test/lorebook-processing.test.ts (+493 -1)

...and 2 more files

📄 Description

Linked issue

#738

Why this change

  • Lorebook entries and regex scripts now support prompt macros, but several paths either left macro text unresolved or resolved it at the wrong point in the prompt pipeline.
  • That could make lorebook budgets inaccurate, recursive activation disagree with final prompt text, regex replacements run with literal macro text, or macro side effects commit in the wrong order.

What changed

  • Resolve lorebook entry content before final budgeting and token estimates, while preserving constant-entry selection priority.
  • Commit selected lorebook macro side effects in final injection order so variable macros behave like the prompt that is sent.
  • Reuse selected/resolved lorebook content for recursive scans and active world-info display.
  • Resolve regex macros in find, replacement, and trim strings on server and client paths, including user-input draft context for {{input}}.
  • Update the regex editor live test to resolve macros against sample User/Character context.
  • Add focused lorebook and regex regression tests.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Automated/local validation run by Codex:

  • pnpm --filter @marinara-engine/server exec tsx --import ./test/setup-env.ts --test test/lorebook-processing.test.ts test/regex-application.test.ts passed, 30 tests.

  • pnpm --filter @marinara-engine/client lint passed.

  • pnpm check passed.

  • git diff --check passed with only line-ending normalization warnings.

  • Server touched-area console. scan found no matches.

  • Verified through recursive lorebook testing, manual variable lorebook testing, and regex macro live testing. All gave expected results.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

No docs or release/version files were changed in this branch.

UI evidence (if applicable)

Not included yet. The regex editor live-test behavior should get a quick manual browser check before marking this ready for review.

Summary by CodeRabbit

Release Notes

  • New Features

    • Regex script editor now supports macro-aware live testing with runtime value resolution.
    • Macros in regex find/replace patterns are now resolved contextually during script application.
    • Lorebook entries now support macro resolution with proper context awareness.
  • Improvements

    • Enhanced macro resolution consistency across chat messages, input fields, and game narration.
    • Macro resolution now integrates with regex script processing for unified variable handling.

Review Change Stack


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/736 **Author:** [@Xelvanis](https://github.com/Xelvanis) **Created:** 5/12/2026 **Status:** ✅ Merged **Merged:** 5/12/2026 **Merged by:** [@Xelvanis](https://github.com/Xelvanis) **Base:** `main` ← **Head:** `fix/lorebook-regex-macros` --- ### 📝 Commits (2) - [`eab2ff7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/eab2ff7cb2dd2dc3f149240e784abf5d149b6ebd) fix: resolve macros in lorebooks and regex scripts - [`b670375`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b670375c22e67f2a02a7f117b5cff2c2aba18222) Address lorebook macro review comments ### 📊 Changes **22 files changed** (+1299 additions, -196 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/components/agents/RegexScriptEditor.tsx` (+44 -8) 📝 `packages/client/src/components/chat/ChatInput.tsx` (+6 -5) 📝 `packages/client/src/components/chat/ChatMessage.tsx` (+9 -4) 📝 `packages/client/src/components/chat/ConversationInput.tsx` (+17 -11) 📝 `packages/client/src/components/game/GameNarration.tsx` (+16 -6) 📝 `packages/client/src/hooks/use-apply-regex.ts` (+18 -8) 📝 `packages/client/src/lib/chat-macros.ts` (+18 -2) 📝 `packages/server/src/routes/chats.routes.ts` (+8 -4) 📝 `packages/server/src/routes/game.routes.ts` (+43 -24) 📝 `packages/server/src/routes/generate.routes.ts` (+23 -34) 📝 `packages/server/src/routes/generate/dry-run-route.ts` (+17 -23) 📝 `packages/server/src/routes/lorebooks.routes.ts` (+44 -1) 📝 `packages/server/src/services/lorebook/index.ts` (+321 -15) 📝 `packages/server/src/services/lorebook/keyword-scanner.ts` (+8 -3) 📝 `packages/server/src/services/prompt/assembler.ts` (+13 -8) 📝 `packages/server/src/services/prompt/index.ts` (+2 -0) 📝 `packages/server/src/services/prompt/macro-context.ts` (+34 -2) 📝 `packages/server/src/services/prompt/marker-expander.ts` (+47 -30) 📝 `packages/server/src/services/regex/regex-application.ts` (+18 -5) 📝 `packages/server/test/lorebook-processing.test.ts` (+493 -1) _...and 2 more files_ </details> ### 📄 Description ## Linked issue #738 ## Why this change - Lorebook entries and regex scripts now support prompt macros, but several paths either left macro text unresolved or resolved it at the wrong point in the prompt pipeline. - That could make lorebook budgets inaccurate, recursive activation disagree with final prompt text, regex replacements run with literal macro text, or macro side effects commit in the wrong order. ## What changed - Resolve lorebook entry content before final budgeting and token estimates, while preserving constant-entry selection priority. - Commit selected lorebook macro side effects in final injection order so variable macros behave like the prompt that is sent. - Reuse selected/resolved lorebook content for recursive scans and active world-info display. - Resolve regex macros in find, replacement, and trim strings on server and client paths, including user-input draft context for `{{input}}`. - Update the regex editor live test to resolve macros against sample User/Character context. - Add focused lorebook and regex regression tests. ## Validation <!-- Required: include commands run and/or manual checks performed --> - [x] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes Automated/local validation run by Codex: - `pnpm --filter @marinara-engine/server exec tsx --import ./test/setup-env.ts --test test/lorebook-processing.test.ts test/regex-application.test.ts` passed, 30 tests. - `pnpm --filter @marinara-engine/client lint` passed. - `pnpm check` passed. - `git diff --check` passed with only line-ending normalization warnings. - Server touched-area `console.` scan found no matches. - Verified through recursive lorebook testing, manual variable lorebook testing, and regex macro live testing. All gave expected results. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) No docs or release/version files were changed in this branch. ## UI evidence (if applicable) Not included yet. The regex editor live-test behavior should get a quick manual browser check before marking this ready for review. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **New Features** * Regex script editor now supports macro-aware live testing with runtime value resolution. * Macros in regex find/replace patterns are now resolved contextually during script application. * Lorebook entries now support macro resolution with proper context awareness. * **Improvements** * Enhanced macro resolution consistency across chat messages, input fields, and game narration. * Macro resolution now integrates with regex script processing for unified variable handling. [![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/Pasta-Devs/Marinara-Engine/pull/736) <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:10:22 +07:00
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#4754
No description provided.