[PR #910] [MERGED] fix(commands): keep Professor Mari going after fetch + parse bracketed params #4832

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/910
Author: @Romuromylus
Created: 5/16/2026
Status: ✅ Merged
Merged: 5/16/2026
Merged by: @Romuromylus

Base: staging ← Head: fix/professor-mari-tool-completion


📝 Commits (2)

  • 7e7d3e4 fix(commands): keep Professor Mari going after fetch + parse bracketed params
  • ffe6455 fix(commands): address CodeRabbit findings on #910

📊 Changes

2 files changed (+162 additions, -51 deletions)

View changed files

📝 packages/server/src/routes/generate.routes.ts (+141 -38)
📝 packages/server/src/services/conversation/character-commands.ts (+21 -13)

📄 Description

Linked issue

Closes #898

Why this change

Two related Professor Mari regressions filed in #898:

  1. Update commands containing ] inside a quoted parameter value (e.g. [update_character: name="X", description="Status: [VIP] ..."]) silently failed to parse and landed in the conversation as plain text.
  2. After Mari emitted a [fetch:...], the fetched payload was persisted to chatMeta.mariContext but mariContext is only injected at the START of a generation pass. Mari went silent right after the fetch snackbar and the user had to send another message before getting a response that used the freshly-fetched data.

What changed

Parser fix — packages/server/src/services/conversation/character-commands.ts

The catch-all [^\]]+ capture group in the command regexes terminated at the first ], so a ] inside a quoted parameter value cut the match short. Replaced the catch-all with a QUOTED_PARAM_BLOCK snippet that matches either a non-quote/non-] char OR a complete double-quoted string (with \"-style escapes). Applied to every [command: ...]-shape regex that takes free-form params (schedule_update, scene, haptic, spotify, dm, create_persona, create_character, update_character, update_persona, create_lorebook, create_chat, navigate, fetch). Leaving selfie, cross_post, memory, and the XML-style block regexes untouched since they already use field-specific patterns. Same bug shape as PR #231's GM tag stripper fix.

Follow-up loop — packages/server/src/routes/generate.routes.ts

Wrapped the conversation-mode generation flow in a while loop capped at MAX_FOLLOW_UP_ITERATIONS = 2 extra iterations. When Professor Mari (PROFESSOR_MARI_ID) executes a [fetch:] during an iteration, the loop:

  • Appends the just-streamed assistant turn into runningMessagesForFollowUp so the next pass sees her prior message.
  • Re-reads chat metadata via chats.getById so the freshly-persisted mariContext reaches the next prompt.
  • Clears collectedCommands / collectedOocMessages (hoisted, accumulators) before continuing.
  • Gates impersonate and assistantPrefill injections to iteration 0 only, so a follow-up turn isn't re-shaped as a fresh user turn.

Variables that need to survive across iterations (firstSavedMsg, lastSavedMsg, pendingIllustration, collectedCommands, collectedOocMessages) are hoisted above the loop so the end-of-stream OOC posting, illustration await, and SSE done event see state from the latest iteration. The background memory-recall chunking call was moved to live inside the loop body so charInfo stays in scope; it runs exactly once on the final iteration (before break).

The follow-up only triggers when:

  • chatMode === "conversation" (Game/Roleplay modes are unaffected),
  • the request isn't an impersonate turn,
  • the request isn't a regenerateMessageId regen,
  • the abort signal hasn't fired,
  • the executed fetch was Professor Mari's (by characterId === PROFESSOR_MARI_ID, or characterId === null in a chat that includes Mari).

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

Tested on a fork build deployed to my private EasyPanel instance (NanoGPT - GLM connection, fresh Conversation chat with Professor Mari).

  • Bug 2 (fetch follow-up): Asked Mari "Please fetch the Lumine character card and give me a short summary of her personality." Mari emitted a fetch (visible as "one sec, pulling her card up"), the fetch snackbar fired, and she continued in the same turn with card-specific details — INFJ traveler, golden eyes, sword made of wind, her twin Aether being taken. Before the fix the stream ended after the snackbar; with the loop she now speaks to the fetched data.
  • Bug 1 (parser): Asked Mari "Please update the Lumine character. Add a new tag to her description: include the exact phrase [STATUS: TRAVELLING] (with square brackets) at the start of her description so I can grep for it later. Keep the rest intact." Mari ran the update; querying /api/characters directly confirmed Lumine's description now starts with the literal string [STATUS: TRAVELLING] "The world is so vast..." — the bracketed value made it through the parser cleanly. Before the fix this command would have failed to match the regex (terminated at the first ]) and leaked as plain text.

Things I'd appreciate the maintainers double-checking:

  • The hoisting of firstSavedMsg / lastSavedMsg / pendingIllustration out of the inner block — they now share state across iterations. firstSavedMsg is intentionally "first across the whole turn" rather than "first per iteration"; pendingIllustration will only await the most recent iteration's job (older illustration promises are fire-and-forget anyway). If anyone sees a case where iteration 1 needs a fresh firstSavedMsg, I'm happy to gate that.
  • The else if chain branching on Mari's fetch result (characterId === PROFESSOR_MARI_ID || (characterId === null && characterIds.includes(PROFESSOR_MARI_ID))) — this matches the same heuristic used elsewhere in the file (e.g. line ~2239 isMariChat), but I haven't audited every legacy chat to confirm null characterId always lines up with Mari being the speaker in a Mari-included chat.
  • Roleplay / Game-mode chats are explicitly excluded from the follow-up loop so this should not affect any GM flows, but worth a quick sanity pass.

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)

UI evidence (if applicable)

Not a UI change. The user-visible effect is that Mari now actually keeps talking after [fetch:] and update commands with brackets-in-quotes land cleanly instead of dumping as plain text.

Credit

The shape of both fixes follows a patch shared by @timwu in the Discord thread linked from #898 (fork commit). I ported it forward to current staging, dropped an unrelated charNameMap refactor that was in the original patch, and verified end-to-end against the live deploy.

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of fetch command execution in conversations through enhanced iterative generation handling
    • Enhanced command parsing to correctly handle complex parameters containing special characters when properly quoted

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/910 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/16/2026 **Status:** ✅ Merged **Merged:** 5/16/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `staging` ← **Head:** `fix/professor-mari-tool-completion` --- ### 📝 Commits (2) - [`7e7d3e4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7e7d3e47f019ce9fc01085b60e90647bedea7018) fix(commands): keep Professor Mari going after fetch + parse bracketed params - [`ffe6455`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ffe6455adab051dfe985df9cc14c9d93adbe42b8) fix(commands): address CodeRabbit findings on #910 ### 📊 Changes **2 files changed** (+162 additions, -51 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/generate.routes.ts` (+141 -38) 📝 `packages/server/src/services/conversation/character-commands.ts` (+21 -13) </details> ### 📄 Description ## Linked issue Closes #898 ## Why this change Two related Professor Mari regressions filed in #898: 1. Update commands containing `]` inside a quoted parameter value (e.g. `[update_character: name="X", description="Status: [VIP] ..."]`) silently failed to parse and landed in the conversation as plain text. 2. After Mari emitted a `[fetch:...]`, the fetched payload was persisted to `chatMeta.mariContext` but mariContext is only injected at the START of a generation pass. Mari went silent right after the fetch snackbar and the user had to send another message before getting a response that used the freshly-fetched data. ## What changed ### Parser fix — `packages/server/src/services/conversation/character-commands.ts` The catch-all `[^\]]+` capture group in the command regexes terminated at the first `]`, so a `]` inside a quoted parameter value cut the match short. Replaced the catch-all with a `QUOTED_PARAM_BLOCK` snippet that matches either a non-quote/non-`]` char OR a complete double-quoted string (with `\"`-style escapes). Applied to every `[command: ...]`-shape regex that takes free-form params (`schedule_update`, `scene`, `haptic`, `spotify`, `dm`, `create_persona`, `create_character`, `update_character`, `update_persona`, `create_lorebook`, `create_chat`, `navigate`, `fetch`). Leaving `selfie`, `cross_post`, `memory`, and the XML-style block regexes untouched since they already use field-specific patterns. Same bug shape as PR #231's GM tag stripper fix. ### Follow-up loop — `packages/server/src/routes/generate.routes.ts` Wrapped the conversation-mode generation flow in a `while` loop capped at `MAX_FOLLOW_UP_ITERATIONS = 2` extra iterations. When Professor Mari (`PROFESSOR_MARI_ID`) executes a `[fetch:]` during an iteration, the loop: - Appends the just-streamed assistant turn into `runningMessagesForFollowUp` so the next pass sees her prior message. - Re-reads chat metadata via `chats.getById` so the freshly-persisted `mariContext` reaches the next prompt. - Clears `collectedCommands` / `collectedOocMessages` (hoisted, accumulators) before continuing. - Gates `impersonate` and `assistantPrefill` injections to iteration 0 only, so a follow-up turn isn't re-shaped as a fresh user turn. Variables that need to survive across iterations (`firstSavedMsg`, `lastSavedMsg`, `pendingIllustration`, `collectedCommands`, `collectedOocMessages`) are hoisted above the loop so the end-of-stream OOC posting, illustration await, and SSE `done` event see state from the latest iteration. The background memory-recall chunking call was moved to live inside the loop body so `charInfo` stays in scope; it runs exactly once on the final iteration (before `break`). The follow-up only triggers when: - `chatMode === "conversation"` (Game/Roleplay modes are unaffected), - the request isn't an impersonate turn, - the request isn't a `regenerateMessageId` regen, - the abort signal hasn't fired, - the executed fetch was Professor Mari's (by `characterId === PROFESSOR_MARI_ID`, or `characterId === null` in a chat that includes Mari). ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] 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 Tested on a fork build deployed to my private EasyPanel instance (NanoGPT - GLM connection, fresh Conversation chat with Professor Mari). - **Bug 2 (fetch follow-up):** Asked Mari "Please fetch the Lumine character card and give me a short summary of her personality." Mari emitted a fetch (visible as "one sec, pulling her card up"), the fetch snackbar fired, and she continued in the same turn with card-specific details — INFJ traveler, golden eyes, sword made of wind, her twin Aether being taken. Before the fix the stream ended after the snackbar; with the loop she now speaks to the fetched data. - **Bug 1 (parser):** Asked Mari "Please update the Lumine character. Add a new tag to her description: include the exact phrase `[STATUS: TRAVELLING]` (with square brackets) at the start of her description so I can grep for it later. Keep the rest intact." Mari ran the update; querying `/api/characters` directly confirmed Lumine's `description` now starts with the literal string `[STATUS: TRAVELLING] "The world is so vast..."` — the bracketed value made it through the parser cleanly. Before the fix this command would have failed to match the regex (terminated at the first `]`) and leaked as plain text. Things I'd appreciate the maintainers double-checking: - The hoisting of `firstSavedMsg` / `lastSavedMsg` / `pendingIllustration` out of the inner block — they now share state across iterations. `firstSavedMsg` is intentionally "first across the whole turn" rather than "first per iteration"; `pendingIllustration` will only await the most recent iteration's job (older illustration promises are fire-and-forget anyway). If anyone sees a case where iteration 1 needs a fresh `firstSavedMsg`, I'm happy to gate that. - The `else if` chain branching on Mari's fetch result (`characterId === PROFESSOR_MARI_ID || (characterId === null && characterIds.includes(PROFESSOR_MARI_ID))`) — this matches the same heuristic used elsewhere in the file (e.g. line ~2239 `isMariChat`), but I haven't audited every legacy chat to confirm `null` characterId always lines up with Mari being the speaker in a Mari-included chat. - Roleplay / Game-mode chats are explicitly excluded from the follow-up loop so this should not affect any GM flows, but worth a quick sanity pass. ## 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) ## UI evidence (if applicable) Not a UI change. The user-visible effect is that Mari now actually keeps talking after `[fetch:]` and update commands with brackets-in-quotes land cleanly instead of dumping as plain text. ## Credit The shape of both fixes follows a patch shared by @timwu in the Discord thread linked from #898 ([fork commit](https://github.com/timwu/Marinara-Engine/commit/605f6d5473a94cc81a996522adba2b4ebd2fe50d)). I ported it forward to current `staging`, dropped an unrelated `charNameMap` refactor that was in the original patch, and verified end-to-end against the live deploy. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved reliability of fetch command execution in conversations through enhanced iterative generation handling * Enhanced command parsing to correctly handle complex parameters containing special characters when properly quoted <!-- review_stack_entry_start --> [![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/910?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) <!-- review_stack_entry_end --> <!-- 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:11:01 +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#4832
No description provided.