[GH-ISSUE #2370] [Issue]: Persona-stats tracker merge clobbers player inventory / stat bars / status on empty agent output (roleplay data loss) #1170

Closed
opened 2026-10-21 05:28:17 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @Romuromylus on GitHub (Jun 5, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2370

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: High — data-loss (regression vs main). In roleplay mode the persona-stats tracker agent runs every turn, and the refactor's merge in gameStatePatchFromAgentResult overwrites the player's inventory, persona stat bars, and status whenever the agent under-produces (emits inventory: [], stats: [], or status: ""). The cleared data is persisted to chat.gameState and becomes the next turn's base, so the loss is permanent until the user manually re-adds everything. Main guards this merge and ignores empty output.

Expected behavior

When the persona-stats agent returns an empty/partial result, the existing inventory, stat bars, and status should be preserved — only non-empty fields should overwrite. This is what main does, and it is also what the sibling custom-tracker merge in this very file already does.

Actual behavior

src/engine/generation/tracker-snapshots.ts:452-463 assigns each field guarded only by Array.isArray(...) / key-presence, which are all true for empty values:

if (result.agentType === "persona-stats" || result.type === "persona_stats_update") {
  const playerStats = clonePlayerStats(snapshot.playerStats);
  if (Object.prototype.hasOwnProperty.call(data, "status")) playerStats.status = readString(data.status).trim();
  if (Array.isArray(data.inventory)) {
    playerStats.inventory = data.inventory.map(parseInventoryItem).filter((item): item is InventoryItem => !!item);
  }
  const patch: TrackerStatePatch = { playerStats };
  if (Array.isArray(data.stats)) {
    patch.personaStats = data.stats.map(parseStat).filter((stat): stat is CharacterStat => !!stat);
  }
  return patch;
}
  • inventory: [] → playerStats.inventory is replaced with [], wiping every carried item.
  • status: "" → playerStats.status is cleared.
  • stats: [] → patch.personaStats is replaced with [], wiping all persona stat bars.

The emptied playerStats / personaStats are written to both the tracker snapshot and chat.gameState (tracker-snapshots.ts:519-521) and become the base for the next turn. normalizeGameState only re-reads the already-clobbered record, so there is no recovery — the loss shows in PersonaTrackerPanel / PersonaInventoryRow / RoleplayHUDPlayerWidget and persists.

This is exactly the routine LLM failure mode (truncated JSON, an agent momentarily omitting an array, cross-contaminated batch output) — not an edge case.

Steps to reproduce

  1. Start a roleplay chat with a persona-stats tracker agent enabled and some inventory items / persona stat bars set.
  2. Take a turn where the agent's persona-stats JSON happens to contain inventory: [] (or stats: [], or status: "") — common when output is truncated or the agent under-merges.
  3. Observe the player's inventory / stat bars / status are wiped from the HUD.
  4. Take another turn — the loss persists, because the cleared state is the new baseline.

Impact area

  • Roleplay mode
  • Runtime systems: generation, world-state, visuals, tracker, or haptics
  • React-free engine behavior, contracts, agents, or capability ports

Environment

  • Branch or commit: refactor @ 330f69a8
  • Run mode: Tauri desktop (engine code, applies to both desktop and web shell)
  • OS + version: not relevant (static source audit)
  • Node + pnpm versions: not relevant
  • Rust version, if relevant: not relevant
  • Remote Runtime URL configured? not relevant
  • Provider, model, or integration, if relevant: any model — triggered by the persona-stats agent emitting empty/partial JSON

Logs, screenshots, or video

Static source audit, cited to src/engine/generation/tracker-snapshots.ts:452-463 and :519-521.

Regression notes

Regression vs main — worked on main, broken on refactor. Main guards the same merge with non-empty checks over a { ...existingPS } base in both the live and retry paths:

  • Live: packages/server/src/routes/generate.routes.ts:9123 (if (bars.length > 0) updates.personaStats = ...), :9131 (if (status) mergedPS.status = status), :9132 (if (inventory.length > 0) mergedPS.inventory = inventory).
  • Retry: packages/server/src/routes/generate/retry-agents-route.ts:1540,1547,1548.

So on main an empty agent result is ignored and existing data is preserved. The refactor dropped all three guards.

Additional context

Fix-pattern-already-in-repo: the sibling custom-tracker merge in the same function, mergeCustomTrackerFieldsFromAgent (src/engine/shared/game-state/player-stats.ts:188), already returns the previous fields when incoming.length === 0. The persona-stats branch just needs the same treatment.

Fix sketch: only assign inventory when the parsed array is non-empty, only set status when the trimmed string is non-empty, and only set patch.personaStats when the parsed stats array is non-empty:

const items = (Array.isArray(data.inventory) ? data.inventory.map(parseInventoryItem).filter(Boolean) : []);
if (items.length) playerStats.inventory = items;
const status = Object.prototype.hasOwnProperty.call(data, "status") ? readString(data.status).trim() : "";
if (status) playerStats.status = status;
const stats = (Array.isArray(data.stats) ? data.stats.map(parseStat).filter(Boolean) : []);
if (stats.length) patch.personaStats = stats;

Not a duplicate of #2317 (world-state manualOverrides null path, fixed), #2327 (debounced stale-overwrite race), or #2294 (avatar toggle).

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 5, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2370 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: High — data-loss (regression vs main).** In roleplay mode the persona-stats tracker agent runs every turn, and the refactor's merge in `gameStatePatchFromAgentResult` overwrites the player's inventory, persona stat bars, and status whenever the agent under-produces (emits `inventory: []`, `stats: []`, or `status: ""`). The cleared data is persisted to `chat.gameState` and becomes the next turn's base, so the loss is permanent until the user manually re-adds everything. Main guards this merge and ignores empty output. ## Expected behavior When the persona-stats agent returns an empty/partial result, the existing inventory, stat bars, and status should be preserved — only non-empty fields should overwrite. This is what main does, and it is also what the sibling custom-tracker merge in this very file already does. ## Actual behavior `src/engine/generation/tracker-snapshots.ts:452-463` assigns each field guarded only by `Array.isArray(...)` / key-presence, which are all true for empty values: ```ts if (result.agentType === "persona-stats" || result.type === "persona_stats_update") { const playerStats = clonePlayerStats(snapshot.playerStats); if (Object.prototype.hasOwnProperty.call(data, "status")) playerStats.status = readString(data.status).trim(); if (Array.isArray(data.inventory)) { playerStats.inventory = data.inventory.map(parseInventoryItem).filter((item): item is InventoryItem => !!item); } const patch: TrackerStatePatch = { playerStats }; if (Array.isArray(data.stats)) { patch.personaStats = data.stats.map(parseStat).filter((stat): stat is CharacterStat => !!stat); } return patch; } ``` - `inventory: []` → `playerStats.inventory` is replaced with `[]`, wiping every carried item. - `status: ""` → `playerStats.status` is cleared. - `stats: []` → `patch.personaStats` is replaced with `[]`, wiping all persona stat bars. The emptied `playerStats` / `personaStats` are written to both the tracker snapshot and `chat.gameState` (`tracker-snapshots.ts:519-521`) and become the base for the next turn. `normalizeGameState` only re-reads the already-clobbered record, so there is no recovery — the loss shows in `PersonaTrackerPanel` / `PersonaInventoryRow` / `RoleplayHUDPlayerWidget` and persists. This is exactly the routine LLM failure mode (truncated JSON, an agent momentarily omitting an array, cross-contaminated batch output) — not an edge case. ## Steps to reproduce 1. Start a roleplay chat with a persona-stats tracker agent enabled and some inventory items / persona stat bars set. 2. Take a turn where the agent's persona-stats JSON happens to contain `inventory: []` (or `stats: []`, or `status: ""`) — common when output is truncated or the agent under-merges. 3. Observe the player's inventory / stat bars / status are wiped from the HUD. 4. Take another turn — the loss persists, because the cleared state is the new baseline. ## Impact area - [x] Roleplay mode - [x] Runtime systems: generation, world-state, visuals, tracker, or haptics - [x] React-free engine behavior, contracts, agents, or capability ports ## Environment - Branch or commit: refactor @ 330f69a8 - Run mode: Tauri desktop (engine code, applies to both desktop and web shell) - OS + version: not relevant (static source audit) - Node + pnpm versions: not relevant - Rust version, if relevant: not relevant - Remote Runtime URL configured? not relevant - Provider, model, or integration, if relevant: any model — triggered by the persona-stats agent emitting empty/partial JSON ## Logs, screenshots, or video Static source audit, cited to `src/engine/generation/tracker-snapshots.ts:452-463` and `:519-521`. ## Regression notes Regression vs main — worked on main, broken on refactor. Main guards the same merge with non-empty checks over a `{ ...existingPS }` base in both the live and retry paths: - Live: `packages/server/src/routes/generate.routes.ts:9123` (`if (bars.length > 0) updates.personaStats = ...`), `:9131` (`if (status) mergedPS.status = status`), `:9132` (`if (inventory.length > 0) mergedPS.inventory = inventory`). - Retry: `packages/server/src/routes/generate/retry-agents-route.ts:1540,1547,1548`. So on main an empty agent result is ignored and existing data is preserved. The refactor dropped all three guards. ## Additional context Fix-pattern-already-in-repo: the sibling custom-tracker merge in the **same function**, `mergeCustomTrackerFieldsFromAgent` (`src/engine/shared/game-state/player-stats.ts:188`), already returns the previous fields when `incoming.length === 0`. The persona-stats branch just needs the same treatment. Fix sketch: only assign `inventory` when the parsed array is non-empty, only set `status` when the trimmed string is non-empty, and only set `patch.personaStats` when the parsed stats array is non-empty: ```ts const items = (Array.isArray(data.inventory) ? data.inventory.map(parseInventoryItem).filter(Boolean) : []); if (items.length) playerStats.inventory = items; const status = Object.prototype.hasOwnProperty.call(data, "status") ? readString(data.status).trim() : ""; if (status) playerStats.status = status; const stats = (Array.isArray(data.stats) ? data.stats.map(parseStat).filter(Boolean) : []); if (stats.length) patch.personaStats = stats; ``` Not a duplicate of #2317 (world-state `manualOverrides` null path, fixed), #2327 (debounced stale-overwrite race), or #2294 (avatar toggle). ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:28:17 +07:00
Author
Owner

@Romuromylus commented on GitHub (Jun 6, 2026):

Fixed by #2380, merged into refactor (squash b2310dd7).

<!-- gh-comment-id:4637085671 --> @Romuromylus commented on GitHub (Jun 6, 2026): Fixed by #2380, merged into `refactor` (squash b2310dd7).
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#1170
No description provided.