[PR #2629] [MERGED] fix(game): merge party/cards against fresh metadata in /party/recruit and /party/remove (#2627) #5694

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2629
Author: @Romuromylus
Created: 6/18/2026
Status: ✅ Merged
Merged: 6/18/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/2627-party-stale-snapshot-race


📝 Commits (7)

  • 8f399a1 fix(game): merge party/cards against fresh metadata in /party/recruit and /party/remove (#2627)
  • 58353f0 Merge branch 'staging' into fix/2627-party-stale-snapshot-race
  • d5aafb7 fix(game): return party chat reloaded after the characterIds mirror write
  • e4ccf42 fix(game): serialize the characterIds mirror inside the metadata patch queue
  • 1e44bd9 fix(game): derive the recruit added flag from the fresh queued party
  • fbaca96 Merge branch 'staging' into fix/2627-party-stale-snapshot-race
  • cef8971 fix(game): only write a recruit card when the request generated one

📊 Changes

2 files changed (+199 additions, -33 deletions)

View changed files

📝 packages/server/src/routes/game.routes.ts (+162 -33)
📝 packages/server/src/services/storage/chats.storage.ts (+37 -0)

📄 Description

Linked issue

Closes #2627

Why this change

In game mode, /party/recruit and /party/remove persisted the party, character cards, and setup-config from a metadata snapshot read before their work, then wrote the whole blob back. /party/recruit returned its precomputed party/card/setup-config from a patchMetadata updater that ignored the fresh current argument; /party/remove wrote a stale blob through the non-queued updateMetadata. Because both derivations were based on the stale read, two overlapping party operations on the same chat — two recruits, or a recruit racing a remove — could drop a freshly added member or resurrect a removed one. This is the residual concurrency facet of #2613 for the two party handlers, deferred from #2623 to keep that batch's scope bounded.

#2627 specifically asks that "no member addition or removal is lost" for concurrent recruit/remove. Fixing only the recruit handler would leave the remove-commits-last case broken — a non-queued remove can still overwrite a recruit's queued patch — so this PR converts both handlers.

What changed

  • Added mergeRecruitIntoGameMetadata: a pure helper that, inside the /party/recruit patchMetadata updater, re-reads gamePartyCharacterIds / gameCharacterCards / gameSetupConfig from the fresh current metadata, merges this recruit's id and card (matching an existing card by name), and derives the characterIds column mirror from the merged party. The handler now calls it through patchMetadata((current) => ...) and writes the characterIds mirror after the queued patch resolves.
  • Added removeMemberFromGameMetadata: the symmetric helper for /party/remove. The handler now routes through patchMetadata((current) => ...) instead of the non-queued updateMetadata, prunes the resolved member from the fresh party, and deliberately omits gameCharacterCards from the patch — removing a member never deletes its card, so leaving the key out preserves a concurrent recruit's freshly added card instead of clobbering it with the stale array.
  • Net effect: both party mutations now reconcile against the metadata committed during their window, on the per-chat queue, so concurrent recruit/remove no longer lose a member or a card.

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

pnpm check passes locally and the per-package server test suite passes. I added focused regression tests for both handlers — concurrent-recruit-preserved, no-resurrect-on-remove, card-reuse-not-clobbered, no-duplicate-card, NPC-id handling, idempotent remove, and no-stale-card-clobber — kept local-only per our test-file convention. The boxes above are left unticked as a reviewer to-do; below is what I verified directly on a build of this branch deployed to my private instance.

Confirmed on the deployed branch (a real game chat, with the original party snapshotted first):

  • Recruit happy path: recruited a library character into a 4-member party. The party grew to 5 with the original four intact, the generated card was appended, and the denormalized characterIds column mirrored the merged party.
  • Remove happy path: removed that same member. HTTP 200, the party was restored exactly to the original four ids, the characterIds column matched, and the other cards were untouched. (The removed member's card remains in gameCharacterCards by design — removal never deletes cards.)

The concurrency window itself — two overlapping operations during the multi-second recruit-card LLM call — can't be driven deterministically by hand; the added regression tests cover those interleavings.

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

  • Improvements
    • Improved the reliability of party recruit and remove operations during concurrent updates.
    • Ensured chat party/metadata and the denormalized character ID list stay synchronized as changes are applied.
    • Updates are now applied more consistently within the same queued patch workflow to reduce mismatches between party state and chat character references.

🔄 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/2629 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/18/2026 **Status:** ✅ Merged **Merged:** 6/18/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/2627-party-stale-snapshot-race` --- ### 📝 Commits (7) - [`8f399a1`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8f399a1257fb2a14fa75e7221239ba2cad0eeeee) fix(game): merge party/cards against fresh metadata in /party/recruit and /party/remove (#2627) - [`58353f0`](https://github.com/Pasta-Devs/Marinara-Engine/commit/58353f0b8c91e08a1412e2ffd70df5439a960ed0) Merge branch 'staging' into fix/2627-party-stale-snapshot-race - [`d5aafb7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d5aafb7ff7211c4df4392b95acacb87c6cc5064a) fix(game): return party chat reloaded after the characterIds mirror write - [`e4ccf42`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e4ccf4283696adabcc05df55d4a2eb6cb6817209) fix(game): serialize the characterIds mirror inside the metadata patch queue - [`1e44bd9`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1e44bd93f9063cf6e4b9032ac8307010a9f57f57) fix(game): derive the recruit `added` flag from the fresh queued party - [`fbaca96`](https://github.com/Pasta-Devs/Marinara-Engine/commit/fbaca96d8b4b5de517865015ca5cc8fd6ab15f5c) Merge branch 'staging' into fix/2627-party-stale-snapshot-race - [`cef8971`](https://github.com/Pasta-Devs/Marinara-Engine/commit/cef8971906377077a252b77449710ab7667f01b3) fix(game): only write a recruit card when the request generated one ### 📊 Changes **2 files changed** (+199 additions, -33 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/game.routes.ts` (+162 -33) 📝 `packages/server/src/services/storage/chats.storage.ts` (+37 -0) </details> ### 📄 Description ## Linked issue Closes #2627 ## Why this change In game mode, `/party/recruit` and `/party/remove` persisted the party, character cards, and setup-config from a metadata snapshot read before their work, then wrote the whole blob back. `/party/recruit` returned its precomputed party/card/setup-config from a `patchMetadata` updater that ignored the fresh `current` argument; `/party/remove` wrote a stale blob through the non-queued `updateMetadata`. Because both derivations were based on the stale read, two overlapping party operations on the same chat — two recruits, or a recruit racing a remove — could drop a freshly added member or resurrect a removed one. This is the residual concurrency facet of #2613 for the two party handlers, deferred from #2623 to keep that batch's scope bounded. #2627 specifically asks that "no member addition or removal is lost" for concurrent recruit/remove. Fixing only the recruit handler would leave the remove-commits-last case broken — a non-queued remove can still overwrite a recruit's queued patch — so this PR converts both handlers. ## What changed - Added `mergeRecruitIntoGameMetadata`: a pure helper that, inside the `/party/recruit` `patchMetadata` updater, re-reads `gamePartyCharacterIds` / `gameCharacterCards` / `gameSetupConfig` from the fresh `current` metadata, merges this recruit's id and card (matching an existing card by name), and derives the `characterIds` column mirror from the merged party. The handler now calls it through `patchMetadata((current) => ...)` and writes the `characterIds` mirror after the queued patch resolves. - Added `removeMemberFromGameMetadata`: the symmetric helper for `/party/remove`. The handler now routes through `patchMetadata((current) => ...)` instead of the non-queued `updateMetadata`, prunes the resolved member from the fresh party, and deliberately omits `gameCharacterCards` from the patch — removing a member never deletes its card, so leaving the key out preserves a concurrent recruit's freshly added card instead of clobbering it with the stale array. - Net effect: both party mutations now reconcile against the metadata committed during their window, on the per-chat queue, so concurrent recruit/remove no longer lose a member or a card. ## 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 `pnpm check` passes locally and the per-package server test suite passes. I added focused regression tests for both handlers — concurrent-recruit-preserved, no-resurrect-on-remove, card-reuse-not-clobbered, no-duplicate-card, NPC-id handling, idempotent remove, and no-stale-card-clobber — kept local-only per our test-file convention. The boxes above are left unticked as a reviewer to-do; below is what I verified directly on a build of this branch deployed to my private instance. Confirmed on the deployed branch (a real game chat, with the original party snapshotted first): - **Recruit happy path:** recruited a library character into a 4-member party. The party grew to 5 with the original four intact, the generated card was appended, and the denormalized `characterIds` column mirrored the merged party. - **Remove happy path:** removed that same member. HTTP 200, the party was restored exactly to the original four ids, the `characterIds` column matched, and the other cards were untouched. (The removed member's card remains in `gameCharacterCards` by design — removal never deletes cards.) The concurrency window itself — two overlapping operations during the multi-second recruit-card LLM call — can't be driven deterministically by hand; the added regression tests cover those interleavings. ## 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 * **Improvements** * Improved the reliability of party **recruit** and **remove** operations during concurrent updates. * Ensured chat **party/metadata** and the denormalized **character ID list** stay synchronized as changes are applied. * Updates are now applied more consistently within the same queued patch workflow to reduce mismatches between party state and chat character references. <!-- 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:18:36 +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#5694
No description provided.