[PR #2423] [MERGED] Guard debounced manual world-state patch against stale chat.gameState write #5599

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

📋 Pull Request Information

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

Base: refactor ← Head: fix/world-state-stale-target-guard


📝 Commits (1)

  • 39c59b1 Guard debounced manual world-state patch against stale chat.gameState write

📊 Changes

1 file changed (+30 additions, -1 deletions)

View changed files

📝 src/features/runtime/world-state/api/world-state-api.ts (+30 -1)

📄 Description

Linked issue

Closes #2327

Why this change

  • A 500ms-debounced manual tracker edit pins its target message at edit time. If an assistant turn completes inside that debounce window, the queued flush wrote the OLD message's snapshot into chat.gameState, clobbering the current turn's persisted state (notably breaks gameStateCarryoverPatch seeding a new session).

What changed

  • src/features/runtime/world-state/api/world-state-api.ts: the chat.gameState write in worldStateApi.patch is now gated by a new canPersistChatGameState helper. Patches without an explicit requestedTarget (per-turn snapshot writes, clearManualState) still always write. A patch that pinned a specific target (debounced manual edits) re-resolves the current visible assistant target at flush time and only writes chat.gameState when the pinned target is still visible; otherwise it keeps the per-message snapshot and lets the visible-read path own chat.gameState.
  • The per-message snapshot trackerSnapshotApi.save stays unconditional. Fail-open on error preserves current behavior.

Refactor impact

Primary owner: world-state

Impact areas reviewed:

  • src/features/runtime/world-state/api/world-state-api.ts (patch, new canPersistChatGameState)

Boundary notes:

  • None. Single feature-layer file. Extra latestAssistantTarget (one listChatMessages) call only on the low-frequency manual-edit path; the per-turn hot path returns immediately. No Rust/DB/schema change.

Pressure points touched:

  • None.

Validation

  • Matching validation command passes locally
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • pnpm vitest run (world-state-api) — a local-only test drove the race: a stale requestedTarget (pinned msg-OLD, visible advanced to msg-NEW) saves the per-message snapshot but performs ZERO storageApi.update("chats", …) writes; a still-visible target writes chat.gameState once; both no-requestedTarget paths still write (no per-turn regression).
  • pnpm exec tsc -b — clean (with and without the local test).

Feature Discoverability

  • Updated src/features/shell/discovery/
  • N/A because this PR is a data-loss race bugfix.

Reason:

  • No new discoverable surface.

Docs and release impact

  • No docs changes needed

🔄 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/2423 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/6/2026 **Status:** ✅ Merged **Merged:** 6/6/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/world-state-stale-target-guard` --- ### 📝 Commits (1) - [`39c59b1`](https://github.com/Pasta-Devs/Marinara-Engine/commit/39c59b130513b62e842a75fc84a21d38839f0052) Guard debounced manual world-state patch against stale chat.gameState write ### 📊 Changes **1 file changed** (+30 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `src/features/runtime/world-state/api/world-state-api.ts` (+30 -1) </details> ### 📄 Description ## Linked issue Closes #2327 ## Why this change - A 500ms-debounced manual tracker edit pins its target message at edit time. If an assistant turn completes inside that debounce window, the queued flush wrote the OLD message's snapshot into `chat.gameState`, clobbering the current turn's persisted state (notably breaks `gameStateCarryoverPatch` seeding a new session). ## What changed - `src/features/runtime/world-state/api/world-state-api.ts`: the `chat.gameState` write in `worldStateApi.patch` is now gated by a new `canPersistChatGameState` helper. Patches without an explicit `requestedTarget` (per-turn snapshot writes, `clearManualState`) still always write. A patch that pinned a specific target (debounced manual edits) re-resolves the current visible assistant target at flush time and only writes `chat.gameState` when the pinned target is still visible; otherwise it keeps the per-message snapshot and lets the visible-read path own `chat.gameState`. - The per-message snapshot `trackerSnapshotApi.save` stays unconditional. Fail-open on error preserves current behavior. ## Refactor impact Primary owner: world-state Impact areas reviewed: - `src/features/runtime/world-state/api/world-state-api.ts` (`patch`, new `canPersistChatGameState`) Boundary notes: - None. Single feature-layer file. Extra `latestAssistantTarget` (one `listChatMessages`) call only on the low-frequency manual-edit path; the per-turn hot path returns immediately. No Rust/DB/schema change. Pressure points touched: - None. ## Validation - [x] Matching validation command passes locally - [ ] Full `pnpm check` passes before PR push/handoff - [ ] Human/manual validation completed by contributor or reviewer ### Manual verification notes - `pnpm vitest run` (world-state-api) — a local-only test drove the race: a stale `requestedTarget` (pinned `msg-OLD`, visible advanced to `msg-NEW`) saves the per-message snapshot but performs ZERO `storageApi.update("chats", …)` writes; a still-visible target writes `chat.gameState` once; both no-`requestedTarget` paths still write (no per-turn regression). - `pnpm exec tsc -b` — clean (with and without the local test). ## Feature Discoverability - [ ] Updated `src/features/shell/discovery/` - [x] N/A because this PR is a data-loss race bugfix. Reason: - No new discoverable surface. ## Docs and release impact - [x] No docs changes needed --- <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:42 +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#5599
No description provided.