[PR #215] [MERGED] Fix message order, metadata, and game-state in branching #4509

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/215
Author: @Xelvanis
Created: 4/24/2026
Status: ✅ Merged
Merged: 4/26/2026
Merged by: @LukaTheHero

Base: main ← Head: fix/chat-branching


📝 Commits (1)

  • 27f8102 fix(chat): preserve message order, extra metadata, and game-state when branching

📊 Changes

2 files changed (+130 additions, -7 deletions)

View changed files

📝 packages/server/src/routes/chats.routes.ts (+113 -7)
📝 packages/server/src/services/storage/game-state.storage.ts (+17 -0)

📄 Description

Why this change

Starting a new branch from a roleplay chat message caused multiple issues: message history would sometimes appear out of order, tracker metadata (date, time, location, weather, characters, stats) was completely lost, and per-message display metadata was silently discarded. This annoyed me as a very branch-happy user, since jumbled message order and losing tracker data was a bummer.

What changed

All changes are confined to the POST /:id/branch handler in chats.routes.ts. No client code, generation logic, or other endpoints are affected.

  • Preserved original message timestamps — messages are now created with their source createdAt via timestampOverrides, preventing out-of-order display caused by same-millisecond now() timestamps
  • Preserved per-message extra metadata — original extra JSON (displayText, generationInfo, tokenCount, etc.) is merged into branched messages via updateMessageExtra()
  • Copied all game-state snapshots — every snapshot from the source chat is re-keyed to the new branch's chatId and messageIds, ensuring tracker state (date, time, location, weather, characters, player stats, persona stats) carries over. Copying all snapshots (not just the latest) ensures branching a branch at an earlier point finds the correct tracker state for that specific message
  • Fixed swipeIndex mismatch — branched messages only have swipe index 0, so copied snapshots are now keyed to swipeIndex: 0 instead of the source's possibly different index
  • Fixed branch sort position — after the message copy loop, storage.update() resets updatedAt to now() so the branch appears at the top of the chat list instead of being sorted as if it was created at the last source message's original timestamp

Validation

  • pnpm check — lint + build passes cleanly
  • Manual verification completed (describe below)

Manual verification notes

  • RP chat with tracker agents (world-state, character-tracker) — chatted several exchanges until HUD showed non-trivial values
  • Branched from assistant message — messages in correct order, timestamps preserved, tracker HUD matches branch point
  • Branched from user message — partial history correct, tracker shows previous assistant state
  • Branched a branch at an earlier message — tracker shows state at the earlier point, not the latest
  • Generated in a branch — tracker continues from seed, does not reset
  • Swiped to alternative response then branched — tracker state still carries over
  • Branch appears at/near top of chat list (not buried with older chats)
  • Normal chat creation, swiping, and editing still work

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)

Summary by CodeRabbit

  • Bug Fixes
    • Chat branching preserves original message timestamps and per-message metadata so branched conversations retain accurate history.
    • Game-state snapshots are duplicated during branching so messages and their associated game-state remain consistent.
    • Malformed metadata or snapshot copy errors are safely ignored to prevent failures; a bootstrap snapshot copy is attempted when available.

🔄 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/215 **Author:** [@Xelvanis](https://github.com/Xelvanis) **Created:** 4/24/2026 **Status:** ✅ Merged **Merged:** 4/26/2026 **Merged by:** [@LukaTheHero](https://github.com/LukaTheHero) **Base:** `main` ← **Head:** `fix/chat-branching` --- ### 📝 Commits (1) - [`27f8102`](https://github.com/Pasta-Devs/Marinara-Engine/commit/27f8102c0d1f34fc520a6e8b2e85bf3c4cf5302b) fix(chat): preserve message order, extra metadata, and game-state when branching ### 📊 Changes **2 files changed** (+130 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/chats.routes.ts` (+113 -7) 📝 `packages/server/src/services/storage/game-state.storage.ts` (+17 -0) </details> ### 📄 Description ## Why this change Starting a new branch from a roleplay chat message caused multiple issues: message history would sometimes appear out of order, tracker metadata (date, time, location, weather, characters, stats) was completely lost, and per-message display metadata was silently discarded. This annoyed me as a very branch-happy user, since jumbled message order and losing tracker data was a bummer. ## What changed All changes are confined to the `POST /:id/branch` handler in `chats.routes.ts`. No client code, generation logic, or other endpoints are affected. - **Preserved original message timestamps** — messages are now created with their source `createdAt` via `timestampOverrides`, preventing out-of-order display caused by same-millisecond `now()` timestamps - **Preserved per-message `extra` metadata** — original `extra` JSON (displayText, generationInfo, tokenCount, etc.) is merged into branched messages via `updateMessageExtra()` - **Copied all game-state snapshots** — every snapshot from the source chat is re-keyed to the new branch's `chatId` and `messageId`s, ensuring tracker state (date, time, location, weather, characters, player stats, persona stats) carries over. Copying all snapshots (not just the latest) ensures branching a branch at an earlier point finds the correct tracker state for that specific message - **Fixed `swipeIndex` mismatch** — branched messages only have swipe index 0, so copied snapshots are now keyed to `swipeIndex: 0` instead of the source's possibly different index - **Fixed branch sort position** — after the message copy loop, `storage.update()` resets `updatedAt` to `now()` so the branch appears at the top of the chat list instead of being sorted as if it was created at the last source message's original timestamp ## Validation - [x] `pnpm check` — lint + build passes cleanly - [x] Manual verification completed (describe below) ### Manual verification notes - [x] RP chat with tracker agents (world-state, character-tracker) — chatted several exchanges until HUD showed non-trivial values - [x] Branched from assistant message — messages in correct order, timestamps preserved, tracker HUD matches branch point - [x] Branched from user message — partial history correct, tracker shows previous assistant state - [x] Branched a branch at an earlier message — tracker shows state at the earlier point, not the latest - [x] Generated in a branch — tracker continues from seed, does not reset - [x] Swiped to alternative response then branched — tracker state still carries over - [x] Branch appears at/near top of chat list (not buried with older chats) - [x] Normal chat creation, swiping, and editing still work ## 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) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Chat branching preserves original message timestamps and per-message metadata so branched conversations retain accurate history. * Game-state snapshots are duplicated during branching so messages and their associated game-state remain consistent. * Malformed metadata or snapshot copy errors are safely ignored to prevent failures; a bootstrap snapshot copy is attempted when available. <!-- 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:08:15 +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#4509
No description provided.