[GH-ISSUE #2993] [Issue]: A base sprite file (e.g. sad.png) is permanently unreachable when 2+ underscore variants exist; resolveExpression random-picks a variant #1566

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

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong
When a character's sprite folder has a bare expression file plus two or more underscore variants sharing its name (e.g. sad.png, sad_01.png, sad_02.png), the bare file becomes impossible to ever display. The model is only ever shown the group key sad, and when it picks sad the resolver runs the prefix-variant random pick BEFORE the exact-match check, so it returns a random variant (sad_01 or sad_02) and never the bare sad.png. The chosen expression also flickers between variants on every retry/swipe.

How to reproduce (try it yourself)

  1. Enable the Expression Engine agent in a roleplay/visual-novel chat.
  2. For one cast character, place sprite files named exactly sad.png, sad_01.png, and sad_02.png in their sprites directory.
  3. Generate a turn whose response is clearly sad; the agent returns expression sad.
  4. Retry/swipe the message.

You'll see: the displayed sprite is sad_01 or sad_02 (random), never sad.png; the variant changes randomly on each swipe.
Expected: the bare sad.png is reachable, and an explicit exact match is honored.

Where it is
packages/server/src/routes/generate/expression-agent-utils.ts:196-219 (resolveExpression; the prefix branch at 202-204 runs before the exact-match check at 206), getExpressionPrefixVariant lines 152-156, pickRandomExpression lines 131-134, called from validateSpriteExpressionEntries at line 352. Interacts with packages/server/src/services/game/sprite.service.ts:55-86 (buildSpriteExpressionChoices collapses variants to one key shown to the model).

What's happening
resolveExpression runs the "prefix variant → random pick" branch before the exact-match branch. getExpressionPrefixVariant only matches entries of the form ${group}_..., so a bare file whose name equals the group key is never a prefix candidate and is shadowed by its own variants whenever 2+ variants exist (prefixMatches.length > 1). buildSpriteExpressionChoices compounds this by collapsing the variants under the same key already emitted by the bare file, so the model is never offered a way to distinguish them.

Suggested fix
Let the bare base file join the group's random pool, and only random-pick when the group is actually collapsed (2+ underscore variants). Replace the prefix/exact block (lines 200-207):

  const lower = trimmed.toLowerCase();

  const prefixMatches = availableExpressions.filter((entry) => getExpressionPrefixVariant(entry, trimmed));
  const exact = availableExpressions.find((entry) => entry.toLowerCase() === lower);

  if (prefixMatches.length > 1) {
    const groupPool = exact ? [exact, ...prefixMatches] : prefixMatches;
    const randomGroupMatch = pickRandomExpression(groupPool);
    if (randomGroupMatch) return randomGroupMatch;
  }

  if (exact) return exact;

The remaining normalized-exact and containment fallback lines (209-218) are unchanged. Regression avoided: the naive "just move exact-match ahead of the prefix block" suggestion would make the bare sad.png win EVERY time and render sad_01/sad_02 permanently unreachable — the inverse bug, and a violation of the documented "randomly display a concrete matching sprite" group contract. Gating the random pool on the existing prefixMatches.length > 1 condition and adding the bare file only then leaves the single-variant case (happy, happy_01 — not collapsed, listed separately to the model) deterministic as today. All validation entry points funnel through this one function, so the single-site fix covers batched, individual, retry, and fallback paths.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2993 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** When a character's sprite folder has a bare expression file plus two or more underscore variants sharing its name (e.g. sad.png, sad_01.png, sad_02.png), the bare file becomes impossible to ever display. The model is only ever shown the group key `sad`, and when it picks `sad` the resolver runs the prefix-variant random pick BEFORE the exact-match check, so it returns a random variant (sad_01 or sad_02) and never the bare sad.png. The chosen expression also flickers between variants on every retry/swipe. **How to reproduce (try it yourself)** 1. Enable the Expression Engine agent in a roleplay/visual-novel chat. 2. For one cast character, place sprite files named exactly sad.png, sad_01.png, and sad_02.png in their sprites directory. 3. Generate a turn whose response is clearly sad; the agent returns expression `sad`. 4. Retry/swipe the message. You'll see: the displayed sprite is sad_01 or sad_02 (random), never sad.png; the variant changes randomly on each swipe. Expected: the bare sad.png is reachable, and an explicit exact match is honored. **Where it is** `packages/server/src/routes/generate/expression-agent-utils.ts:196-219` (`resolveExpression`; the prefix branch at 202-204 runs before the exact-match check at 206), `getExpressionPrefixVariant` lines 152-156, `pickRandomExpression` lines 131-134, called from `validateSpriteExpressionEntries` at line 352. Interacts with `packages/server/src/services/game/sprite.service.ts:55-86` (`buildSpriteExpressionChoices` collapses variants to one key shown to the model). **What's happening** `resolveExpression` runs the "prefix variant → random pick" branch before the exact-match branch. `getExpressionPrefixVariant` only matches entries of the form `${group}_...`, so a bare file whose name equals the group key is never a prefix candidate and is shadowed by its own variants whenever 2+ variants exist (prefixMatches.length > 1). `buildSpriteExpressionChoices` compounds this by collapsing the variants under the same key already emitted by the bare file, so the model is never offered a way to distinguish them. **Suggested fix** Let the bare base file join the group's random pool, and only random-pick when the group is actually collapsed (2+ underscore variants). Replace the prefix/exact block (lines 200-207): ```ts const lower = trimmed.toLowerCase(); const prefixMatches = availableExpressions.filter((entry) => getExpressionPrefixVariant(entry, trimmed)); const exact = availableExpressions.find((entry) => entry.toLowerCase() === lower); if (prefixMatches.length > 1) { const groupPool = exact ? [exact, ...prefixMatches] : prefixMatches; const randomGroupMatch = pickRandomExpression(groupPool); if (randomGroupMatch) return randomGroupMatch; } if (exact) return exact; ``` The remaining normalized-exact and containment fallback lines (209-218) are unchanged. Regression avoided: the naive "just move exact-match ahead of the prefix block" suggestion would make the bare sad.png win EVERY time and render sad_01/sad_02 permanently unreachable — the inverse bug, and a violation of the documented "randomly display a concrete matching sprite" group contract. Gating the random pool on the existing `prefixMatches.length > 1` condition and adding the bare file only then leaves the single-variant case (happy, happy_01 — not collapsed, listed separately to the model) deterministic as today. All validation entry points funnel through this one function, so the single-site fix covers batched, individual, retry, and fallback paths. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in merged staging PR #3004.

<!-- gh-comment-id:4832281281 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in merged staging PR #3004.
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#1566
No description provided.