[PR #2255] [MERGED] Fix lorebook regex timeout #5517

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2255
Author: @Promansis
Created: 6/5/2026
Status: ✅ Merged
Merged: 6/5/2026
Merged by: @Promansis

Base: refactor ← Head: fix/issue-2199-regex-timeout


📝 Commits (1)

  • 08ca36a Fix lorebook regex timeout

📊 Changes

8 files changed (+524 additions, -36 deletions)

View changed files

📝 src/engine/agents-runtime/knowledge/knowledge-router.ts (+14 -7)
📝 src/engine/generation-core/lorebooks/keyword-scanner.ts (+6 -6)
📝 src/engine/generation-core/lorebooks/prompt-injector.test.ts (+2 -2)
➕ src/engine/generation-core/lorebooks/regex-timeout.test.ts (+182 -0)
📝 src/engine/generation-core/lorebooks/regex-timeout.ts (+144 -11)
📝 src/engine/generation/active-lorebook-scanner.ts (+5 -5)
➕ src/engine/shared/regex/lorebook-keyword-matching.test.ts (+81 -0)
📝 src/engine/shared/regex/lorebook-keyword-matching.ts (+90 -5)

📄 Description

Linked issue

Closes #2199

Why this change

  • Lorebook regex keyword matching preserved static pattern checks but lost the legacy hard execution timeout, so accepted user regexes could still monopolize generation or preview scans.

What changed

  • Runs lorebook regex execution through a disposable browser Worker with a hard timeout and fail-closed behavior.
  • Adds a documented bounded fallback for safe, short regex scans when Worker support is unavailable, instead of silently turning every regex activation into a non-match.
  • Makes lorebook keyword scanning asynchronous so runtime scans can await the timeout-protected executor.
  • Updates active lorebook scanning and knowledge-router keyword prefiltering to await the async scanner.
  • Keeps shared keyword helpers available for both sync and async match paths.

Refactor impact

Primary owner:

engine generation

Impact areas reviewed:

  • Chat, roleplay, and game lorebook activation paths.
  • Recursive lorebook scanning.
  • Knowledge-router keyword candidate prefiltering.
  • Existing prompt injector coverage that consumes activated lorebook entries.

Boundary notes:

  • Behavior stays in src/engine and shared regex helpers.
  • No React, Tauri, shared API, storage, provider, or remote-runtime boundary changes.
  • Browser/Tauri hard timeout uses a local Worker rather than a Node vm import, keeping engine code hostable in the frontend runtime.
  • Worker-unavailable runtimes use a smaller bounded sync fallback only after static safety checks and scan-length caps pass.

Pressure points touched:

  • Runtime generation lorebook keyword scanning.
  • Knowledge-router candidate selection.

Validation

  • Matching validation command passes locally (for example pnpm typecheck, pnpm build, pnpm check:architecture, pnpm check:docs, or full pnpm check when warranted)
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • pnpm typecheck
  • pnpm check:unused
  • Earlier on this branch before the fallback repair: pnpm check:architecture, pnpm build, pnpm exec vitest run src/engine/generation-core/lorebooks/prompt-injector.test.ts, pnpm check
  • Browser proof through Vite on http://127.0.0.1:4177/: imported the runtime modules, confirmed normal regex match succeeds, oversized scan text fails closed, static-safety accepts ^(a|aa)+$, and the timeout executor returns false for ^(a|aa)+$ against a long aaaa...! input at the configured deadline.
  • Browser no-Worker proof: temporarily removed window.Worker before module import and confirmed the bounded fallback returns true for a safe short regex match and false for fallback text over 10,000 characters.

Feature Discoverability

Check exactly one:

  • Updated src/features/shell/discovery/ because this PR adds or materially changes a user-discoverable feature, workflow, setting, mode, panel, import path, agent, media capability, or advanced tool.
  • N/A because this PR is only a bugfix, refactor, test, docs, internal wiring, visual polish, copy edit, or compatibility fix and does not add a new thing users need to find.

Reason:

  • This restores internal lorebook regex safety behavior and does not add a discoverable feature.

Docs and release impact

  • No docs changes needed
  • Updated README.md
  • Updated CONTRIBUTING.md
  • Updated docs/developer/
  • Updated repo skills or AGENTS.md
  • Confirmed this PR does not restore old staging/package-workspace/release claims

UI evidence

No visible UI changes.


🔄 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/2255 **Author:** [@Promansis](https://github.com/Promansis) **Created:** 6/5/2026 **Status:** ✅ Merged **Merged:** 6/5/2026 **Merged by:** [@Promansis](https://github.com/Promansis) **Base:** `refactor` ← **Head:** `fix/issue-2199-regex-timeout` --- ### 📝 Commits (1) - [`08ca36a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/08ca36a6e48ee612b50680c4a0bfd7c87328be6f) Fix lorebook regex timeout ### 📊 Changes **8 files changed** (+524 additions, -36 deletions) <details> <summary>View changed files</summary> 📝 `src/engine/agents-runtime/knowledge/knowledge-router.ts` (+14 -7) 📝 `src/engine/generation-core/lorebooks/keyword-scanner.ts` (+6 -6) 📝 `src/engine/generation-core/lorebooks/prompt-injector.test.ts` (+2 -2) ➕ `src/engine/generation-core/lorebooks/regex-timeout.test.ts` (+182 -0) 📝 `src/engine/generation-core/lorebooks/regex-timeout.ts` (+144 -11) 📝 `src/engine/generation/active-lorebook-scanner.ts` (+5 -5) ➕ `src/engine/shared/regex/lorebook-keyword-matching.test.ts` (+81 -0) 📝 `src/engine/shared/regex/lorebook-keyword-matching.ts` (+90 -5) </details> ### 📄 Description ## Linked issue Closes #2199 ## Why this change - Lorebook regex keyword matching preserved static pattern checks but lost the legacy hard execution timeout, so accepted user regexes could still monopolize generation or preview scans. ## What changed - Runs lorebook regex execution through a disposable browser Worker with a hard timeout and fail-closed behavior. - Adds a documented bounded fallback for safe, short regex scans when Worker support is unavailable, instead of silently turning every regex activation into a non-match. - Makes lorebook keyword scanning asynchronous so runtime scans can await the timeout-protected executor. - Updates active lorebook scanning and knowledge-router keyword prefiltering to await the async scanner. - Keeps shared keyword helpers available for both sync and async match paths. ## Refactor impact Primary owner: engine generation Impact areas reviewed: - Chat, roleplay, and game lorebook activation paths. - Recursive lorebook scanning. - Knowledge-router keyword candidate prefiltering. - Existing prompt injector coverage that consumes activated lorebook entries. Boundary notes: - Behavior stays in `src/engine` and shared regex helpers. - No React, Tauri, shared API, storage, provider, or remote-runtime boundary changes. - Browser/Tauri hard timeout uses a local Worker rather than a Node `vm` import, keeping engine code hostable in the frontend runtime. - Worker-unavailable runtimes use a smaller bounded sync fallback only after static safety checks and scan-length caps pass. Pressure points touched: - Runtime generation lorebook keyword scanning. - Knowledge-router candidate selection. ## Validation - [x] Matching validation command passes locally (for example `pnpm typecheck`, `pnpm build`, `pnpm check:architecture`, `pnpm check:docs`, or full `pnpm check` when warranted) - [x] Full `pnpm check` passes before PR push/handoff - [ ] Human/manual validation completed by contributor or reviewer ### Manual verification notes - `pnpm typecheck` - `pnpm check:unused` - Earlier on this branch before the fallback repair: `pnpm check:architecture`, `pnpm build`, `pnpm exec vitest run src/engine/generation-core/lorebooks/prompt-injector.test.ts`, `pnpm check` - Browser proof through Vite on `http://127.0.0.1:4177/`: imported the runtime modules, confirmed normal regex match succeeds, oversized scan text fails closed, static-safety accepts `^(a|aa)+$`, and the timeout executor returns `false` for `^(a|aa)+$` against a long `aaaa...!` input at the configured deadline. - Browser no-Worker proof: temporarily removed `window.Worker` before module import and confirmed the bounded fallback returns `true` for a safe short regex match and `false` for fallback text over 10,000 characters. ## Feature Discoverability Check exactly one: - [ ] Updated `src/features/shell/discovery/` because this PR adds or materially changes a user-discoverable feature, workflow, setting, mode, panel, import path, agent, media capability, or advanced tool. - [x] N/A because this PR is only a bugfix, refactor, test, docs, internal wiring, visual polish, copy edit, or compatibility fix and does not add a new thing users need to find. Reason: - This restores internal lorebook regex safety behavior and does not add a discoverable feature. ## Docs and release impact - [x] No docs changes needed - [ ] Updated `README.md` - [ ] Updated `CONTRIBUTING.md` - [ ] Updated `docs/developer/` - [ ] Updated repo skills or `AGENTS.md` - [x] Confirmed this PR does not restore old staging/package-workspace/release claims ## UI evidence No visible UI changes. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:17:00 +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#5517
No description provided.