[PR #1191] [MERGED] Strip avatarPath from agent gameState serialization #4968

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

📋 Pull Request Information

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

Base: refactor ← Head: fix/agent-context-avatar-bloat


📝 Commits (2)

  • e3bdae8 Strip avatarPath from agent gameState serialization
  • d9be35c Extract stripAvatarPathsReplacer and cover sibling sites

📊 Changes

3 files changed (+25 additions, -3 deletions)

View changed files

📝 src/engine/agents-runtime/executor/agent-executor.ts (+3 -2)
📝 src/engine/agents-runtime/knowledge/knowledge-router.ts (+2 -1)
➕ src/engine/agents-runtime/strip-avatar-paths.ts (+20 -0)

📄 Description

Why this change

Imported pre-refactor chats can carry full base64 image data URLs in gameState.presentCharacters[].avatarPath - the legacy backup format inlines avatars rather than referencing asset paths. Three sites in the agents-runtime layer serialize gameState (or a tracker summary built from it) into agent prompts, each of which can pick up a megabyte+ of base64 per present character.

On 1M-token providers (e.g. Gemini) the agent prompt blew past the ceiling and every tracker agent failed with:

Provider returned HTTP 400 Bad Request: prompt is too long: 2188040 tokens > 1000000 maximum

applyAgentResultEffects in src/features/runtime/generation/hooks/use-generate.ts short-circuits on !result.success, so the failures never reached the bubble / tracker handlers and the user-visible symptom was both panels stuck on their empty states ("No tracker data yet" and "No agent activity yet") with no error indication, even though the main chat reply itself succeeded.

What changed

New named helper stripAvatarPathsReplacer at src/engine/agents-runtime/strip-avatar-paths.ts - a JSON.stringify replacer that drops any avatarPath key from the serialized tree. avatarPath is a UI display field (image path or data URL the tracker panel renders); the model never reads it. Every other model-relevant field on each present character (name, mood, appearance, outfit, stats, thoughts, customFields, etc.) is preserved as before.

The helper is now applied at all three sites that serialize gameState or a tracker summary built from it into a prompt:

  1. buildAgentExtras <current_game_state> block at src/engine/agents-runtime/executor/agent-executor.ts - the original site identified in #1188.
  2. buildAgentMessages <committed_tracker_state> block at src/engine/agents-runtime/executor/agent-executor.ts - attaches a tracker summary to the last 3 assistant messages and assigns gs.presentCharacters whole; structured-output agents would inherit the avatarPath data URLs from history regardless of whether <current_game_state> is included.
  3. buildKnowledgeRouterQuery at src/engine/agents-runtime/knowledge/knowledge-router.ts - serializes context.gameState verbatim into the router query; latent until the knowledge router is enabled on an imported chat.

Co-locating the rule in a named helper makes it findable by symbol search and keeps future serialization sites a one-import-and-pass-the-replacer change rather than rediscovering the rule per-site.

Verification

  • pnpm check:frontend, pnpm check:architecture, pnpm check:docs all pass locally (595 modules, 2120 dependencies cruised).
  • Manually verified in pnpm tauri dev on Windows against an imported pre-refactor profile (the exact repro from the issue: roleplay chat with activeAgentIds: ["world-state", "quest", "character-tracker", "persona-stats"]). Before the fix: all four agent results returned success: false with the prompt-too-long error and both side panels stayed empty. After the fix: all four agents complete normally, the agent activity panel shows the four thought bubbles, and the tracker panel populates with world / character / quest / persona data. Re-verified after the helper extraction; behavior unchanged.

Out of scope

A second, separate UX bug is visible in passing: when every agent in a turn fails, the user sees only the empty-state panels - the failure surface (setFailedAgentFailures / failedAgentTypes in agent.store.ts) is defined but never called from applyAgentResultEffects, so genuine agent failures are silent. That made this bug much harder to spot than it should have been. Filed as #1192 to track separately; intentionally left out of this PR to keep the diff focused on the actual root cause.

Fixes #1188.

Summary by CodeRabbit

Bug Fixes

  • Optimized agent prompt serialization to exclude avatar path data, reducing unnecessary data transmission and improving performance when using legacy imported chats.

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/1191 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/25/2026 **Status:** ✅ Merged **Merged:** 5/25/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/agent-context-avatar-bloat` --- ### 📝 Commits (2) - [`e3bdae8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e3bdae84330ba382394f006897865c342c95843b) Strip avatarPath from agent gameState serialization - [`d9be35c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d9be35c310ec46c42e2499b00f9f9606da02efbb) Extract stripAvatarPathsReplacer and cover sibling sites ### 📊 Changes **3 files changed** (+25 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `src/engine/agents-runtime/executor/agent-executor.ts` (+3 -2) 📝 `src/engine/agents-runtime/knowledge/knowledge-router.ts` (+2 -1) ➕ `src/engine/agents-runtime/strip-avatar-paths.ts` (+20 -0) </details> ### 📄 Description ## Why this change Imported pre-refactor chats can carry full base64 image data URLs in `gameState.presentCharacters[].avatarPath` - the legacy backup format inlines avatars rather than referencing asset paths. Three sites in the agents-runtime layer serialize `gameState` (or a tracker summary built from it) into agent prompts, each of which can pick up a megabyte+ of base64 per present character. On 1M-token providers (e.g. Gemini) the agent prompt blew past the ceiling and every tracker agent failed with: ``` Provider returned HTTP 400 Bad Request: prompt is too long: 2188040 tokens > 1000000 maximum ``` `applyAgentResultEffects` in `src/features/runtime/generation/hooks/use-generate.ts` short-circuits on `!result.success`, so the failures never reached the bubble / tracker handlers and the user-visible symptom was both panels stuck on their empty states ("No tracker data yet" and "No agent activity yet") with no error indication, even though the main chat reply itself succeeded. ## What changed New named helper `stripAvatarPathsReplacer` at `src/engine/agents-runtime/strip-avatar-paths.ts` - a `JSON.stringify` replacer that drops any `avatarPath` key from the serialized tree. `avatarPath` is a UI display field (image path or data URL the tracker panel renders); the model never reads it. Every other model-relevant field on each present character (`name`, `mood`, `appearance`, `outfit`, `stats`, `thoughts`, `customFields`, etc.) is preserved as before. The helper is now applied at all three sites that serialize `gameState` or a tracker summary built from it into a prompt: 1. `buildAgentExtras` `<current_game_state>` block at `src/engine/agents-runtime/executor/agent-executor.ts` - the original site identified in #1188. 2. `buildAgentMessages` `<committed_tracker_state>` block at `src/engine/agents-runtime/executor/agent-executor.ts` - attaches a tracker summary to the last 3 assistant messages and assigns `gs.presentCharacters` whole; structured-output agents would inherit the avatarPath data URLs from history regardless of whether `<current_game_state>` is included. 3. `buildKnowledgeRouterQuery` at `src/engine/agents-runtime/knowledge/knowledge-router.ts` - serializes `context.gameState` verbatim into the router query; latent until the knowledge router is enabled on an imported chat. Co-locating the rule in a named helper makes it findable by symbol search and keeps future serialization sites a one-import-and-pass-the-replacer change rather than rediscovering the rule per-site. ## Verification - `pnpm check:frontend`, `pnpm check:architecture`, `pnpm check:docs` all pass locally (595 modules, 2120 dependencies cruised). - Manually verified in `pnpm tauri dev` on Windows against an imported pre-refactor profile (the exact repro from the issue: roleplay chat with `activeAgentIds: ["world-state", "quest", "character-tracker", "persona-stats"]`). Before the fix: all four agent results returned `success: false` with the prompt-too-long error and both side panels stayed empty. After the fix: all four agents complete normally, the agent activity panel shows the four thought bubbles, and the tracker panel populates with world / character / quest / persona data. Re-verified after the helper extraction; behavior unchanged. ## Out of scope A second, separate UX bug is visible in passing: when every agent in a turn fails, the user sees only the empty-state panels - the failure surface (`setFailedAgentFailures` / `failedAgentTypes` in `agent.store.ts`) is defined but never called from `applyAgentResultEffects`, so genuine agent failures are silent. That made this bug much harder to spot than it should have been. Filed as #1192 to track separately; intentionally left out of this PR to keep the diff focused on the actual root cause. Fixes #1188. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Bug Fixes - Optimized agent prompt serialization to exclude avatar path data, reducing unnecessary data transmission and improving performance when using legacy imported chats. <!-- 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/1191?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:12:06 +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#4968
No description provided.