[PR #954] [MERGED] Fix Gemini user-message regeneration swipes #4858

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/954
Author: @Minsklatte
Created: 5/17/2026
Status: ✅ Merged
Merged: 5/20/2026
Merged by: @cha1latte

Base: staging ← Head: codex/fix-gemini-user-regenerate


📝 Commits (10+)

  • ae8d81d Fix Gemini user-message regeneration swipes
  • 09e0546 Preserve prefill for Gemini user regeneration
  • 06b2dcb Document user regeneration helper
  • fdc715f Preserve attachments for user regeneration
  • 19bb332 Preserve user regen context for prompt shaping
  • 9f70553 test: cover transformed user regen prompt source
  • be8b215 fix: rebuild user regen prompt after regex transforms
  • 51c51cf Merge branch 'staging' into codex/fix-gemini-user-regenerate
  • 92b7375 fix: ignore malformed regeneration attachments
  • 1fdfa1c fix: block hidden user message regeneration

📊 Changes

4 files changed (+562 additions, -13 deletions)

View changed files

📝 packages/server/src/routes/generate.routes.ts (+66 -12)
📝 packages/server/src/routes/generate/generate-route-utils.ts (+118 -1)
➕ tests/user-message-regeneration-hidden-from-ai.test.ts (+46 -0)
➕ tests/user-message-regeneration-instruction.test.ts (+332 -0)

📄 Description

Linked issue

Fixes #947.

Why this change

Roleplay user-message regeneration is intended to create an alternate swipe on the clicked user-authored message.

The existing regenerate path excludes the target message from context before generation. For a bottommost user-message regenerate, that can leave Gemini's native request ending on the previous assistant/model turn. I reproduced Gemini returning HTTP 200 with finishReason: STOP and empty text for that shape, while an explicit final user-role rewrite instruction returns replacement text.

This PR keeps the existing swipe behavior, but fixes the Gemini-specific empty-response path without changing OpenAI or other providers' prompt construction.

What changed

  • Added a user-message regeneration instruction builder.
  • Captured that instruction only when the regenerate target is a user-authored message.
  • For Google Gemini / Vertex only, append the instruction as a final user-role message before generation.
  • Preserve assistant prefill by placing it before the Gemini/Vertex user-message regeneration instruction.
  • Preserve original user-message attachments during regeneration: readable files are embedded into the rewrite instruction, and image data is carried on the final user prompt message.
  • Added focused regression tests for the generated rewrite instruction, tail-message ordering, assistant prefill preservation, and attachment preservation.

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

  • Ran packages/server/node_modules/.bin/tsx --test tests/user-message-regeneration-instruction.test.ts; result: 6 tests passed, 0 failed.
  • Ran pnpm --filter @marinara-engine/server build; result: passed.
  • Ran pnpm build; result: passed.
  • Ran pnpm lint; result: passed.
  • Ran pnpm check; result: blocked before lint/build at impeccable:check with Impeccable context loader did not return valid JSON, so the pnpm check validation box is intentionally left unchecked.
  • Manually verified OpenAI user-message regenerate keeps the old prompt path and saves a swipe on the original user message.
  • Manually verified Gemini user-message regenerate appends the provider-specific rewrite instruction and saves a swipe on the original user message.
  • Manually verified Gemini user-message regenerate with assistant prefill preserves the prefill but still ends the provider prompt with the user rewrite instruction.
  • Manually verified the attachment edge case with Gemini: repro'd the original bottommost-user regeneration bug using a user message of describe this image plus an image attachment, regenerated that user message, and the regenerated swipe correctly described the image.
  • Confirmed neither provider path creates a new assistant message when regenerating the user message.
  • No UI surface changed, so light/dark/mobile visual checks are not applicable; the checked edge cases are provider path, assistant-prefill, no-attachment, and attachment-backed regeneration paths.
  • Container validation was not run, so the container checkbox is intentionally left unchecked.

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)

No UI changes.

Summary by CodeRabbit

  • New Features

    • Regeneration preserves original user text and attachments (including images/data-URLs) and stores regeneration-specific attachments with regenerated assistant messages.
    • Assistant prefill insertion is now consistent across providers, preserving intended ordering relative to regenerated user input.
    • Hidden-from-AI user messages are respected during regeneration when supported.
  • Tests

    • Added tests covering regeneration instruction formatting, attachment/image handling, whitespace trimming, hidden-from-AI behavior, and provider-specific tail-message ordering.

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/954 **Author:** [@Minsklatte](https://github.com/Minsklatte) **Created:** 5/17/2026 **Status:** ✅ Merged **Merged:** 5/20/2026 **Merged by:** [@cha1latte](https://github.com/cha1latte) **Base:** `staging` ← **Head:** `codex/fix-gemini-user-regenerate` --- ### 📝 Commits (10+) - [`ae8d81d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ae8d81dd0487488770e9e309261088616e66e1d4) Fix Gemini user-message regeneration swipes - [`09e0546`](https://github.com/Pasta-Devs/Marinara-Engine/commit/09e05465c1302bd630e11c69862f2400d0615a56) Preserve prefill for Gemini user regeneration - [`06b2dcb`](https://github.com/Pasta-Devs/Marinara-Engine/commit/06b2dcb4901d080e63898284ae5ffeaf45fdd574) Document user regeneration helper - [`fdc715f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/fdc715ffbd5e4ef7ac882197e5148b8ada24cb07) Preserve attachments for user regeneration - [`19bb332`](https://github.com/Pasta-Devs/Marinara-Engine/commit/19bb332b5a3316b31dc56809b602615b0364335d) Preserve user regen context for prompt shaping - [`9f70553`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9f70553231ebb38438229add01cb3ef42d8ea8a3) test: cover transformed user regen prompt source - [`be8b215`](https://github.com/Pasta-Devs/Marinara-Engine/commit/be8b21578b2d00a9d4ebf284e9a5185fa9994c44) fix: rebuild user regen prompt after regex transforms - [`51c51cf`](https://github.com/Pasta-Devs/Marinara-Engine/commit/51c51cf3b5824238ea7285d67069c93f426d666a) Merge branch 'staging' into codex/fix-gemini-user-regenerate - [`92b7375`](https://github.com/Pasta-Devs/Marinara-Engine/commit/92b73753efcc0fd590fbf0a8373d8fe773b2067d) fix: ignore malformed regeneration attachments - [`1fdfa1c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1fdfa1c2b1827b428441c5c7d9bb82e4bc637392) fix: block hidden user message regeneration ### 📊 Changes **4 files changed** (+562 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/generate.routes.ts` (+66 -12) 📝 `packages/server/src/routes/generate/generate-route-utils.ts` (+118 -1) ➕ `tests/user-message-regeneration-hidden-from-ai.test.ts` (+46 -0) ➕ `tests/user-message-regeneration-instruction.test.ts` (+332 -0) </details> ### 📄 Description ## Linked issue Fixes #947. ## Why this change Roleplay user-message regeneration is intended to create an alternate swipe on the clicked user-authored message. The existing regenerate path excludes the target message from context before generation. For a bottommost user-message regenerate, that can leave Gemini's native request ending on the previous assistant/model turn. I reproduced Gemini returning HTTP 200 with `finishReason: STOP` and empty text for that shape, while an explicit final user-role rewrite instruction returns replacement text. This PR keeps the existing swipe behavior, but fixes the Gemini-specific empty-response path without changing OpenAI or other providers' prompt construction. ## What changed - Added a user-message regeneration instruction builder. - Captured that instruction only when the regenerate target is a user-authored message. - For Google Gemini / Vertex only, append the instruction as a final user-role message before generation. - Preserve assistant prefill by placing it before the Gemini/Vertex user-message regeneration instruction. - Preserve original user-message attachments during regeneration: readable files are embedded into the rewrite instruction, and image data is carried on the final user prompt message. - Added focused regression tests for the generated rewrite instruction, tail-message ordering, assistant prefill preservation, and attachment preservation. ## Validation - [ ] `pnpm check` passes locally - [ ] 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 - Ran `packages/server/node_modules/.bin/tsx --test tests/user-message-regeneration-instruction.test.ts`; result: 6 tests passed, 0 failed. - Ran `pnpm --filter @marinara-engine/server build`; result: passed. - Ran `pnpm build`; result: passed. - Ran `pnpm lint`; result: passed. - Ran `pnpm check`; result: blocked before lint/build at `impeccable:check` with `Impeccable context loader did not return valid JSON`, so the `pnpm check` validation box is intentionally left unchecked. - Manually verified OpenAI user-message regenerate keeps the old prompt path and saves a swipe on the original user message. - Manually verified Gemini user-message regenerate appends the provider-specific rewrite instruction and saves a swipe on the original user message. - Manually verified Gemini user-message regenerate with assistant prefill preserves the prefill but still ends the provider prompt with the user rewrite instruction. - Manually verified the attachment edge case with Gemini: repro'd the original bottommost-user regeneration bug using a user message of `describe this image` plus an image attachment, regenerated that user message, and the regenerated swipe correctly described the image. - Confirmed neither provider path creates a new assistant message when regenerating the user message. - No UI surface changed, so light/dark/mobile visual checks are not applicable; the checked edge cases are provider path, assistant-prefill, no-attachment, and attachment-backed regeneration paths. - Container validation was not run, so the container checkbox is intentionally left unchecked. ## 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) No UI changes. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Regeneration preserves original user text and attachments (including images/data-URLs) and stores regeneration-specific attachments with regenerated assistant messages. * Assistant prefill insertion is now consistent across providers, preserving intended ordering relative to regenerated user input. * Hidden-from-AI user messages are respected during regeneration when supported. * **Tests** * Added tests covering regeneration instruction formatting, attachment/image handling, whitespace trimming, hidden-from-AI behavior, and provider-specific tail-message ordering. <!-- 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/954?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:13 +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#4858
No description provided.