[PR #415] [MERGED] fix(game): tighten bg fuzzy matcher so new scenes don't pin to first library bg #4613

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

📋 Pull Request Information

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

Base: main ← Head: fix/game-bg-fuzzy-matcher


📝 Commits (3)

  • dd125a1 fix(game): tighten bg fuzzy matcher so new scenes don't pin to first library bg
  • bbaf11f fix(game): scope stricter fuzzy threshold to backgrounds only
  • e6756bd fix(game): preserve canonical backgrounds:generated:* tags before fuzzy match

📊 Changes

1 file changed (+32 additions, -9 deletions)

View changed files

📝 packages/client/src/lib/asset-fuzzy-match.ts (+32 -9)

📄 Description

Linked issue

Closes #414

Why this change

In Game Mode with image generation enabled, starting a new scene renders the wrong background and never triggers a fresh generation. On installs that include the legacy SillyTavern data/backgrounds/ pack (which gets manifest-tagged as backgrounds:user:<name>), unrelated scenes pin to whichever bg comes first in iteration order — typically backgrounds:user:ancient_library.

The root cause is a fuzzy-matcher that's too lax:

  • tagScore in packages/client/src/lib/asset-fuzzy-match.ts counts the universal category prefix word (backgrounds) on both the model-emitted tag and every candidate manifest tag, giving every comparison a free +1.
  • bestMatch accepts any score > 0, so that single free point declares a match and returns the first tag walked.

Downstream in applySceneResult:

  1. setCurrentBackground(resolved) immediately sets the wrong bg.
  2. The unresolvedBg find concludes the bg is already in the manifest, so backgroundTag is omitted from the /game/generate-assets payload — no generation request is ever made.

Server-side logs [game/scene-wrap] bg "<chosen>" not in manifest; generation will be deferred, but the deferred call from the client never names the bg, so nothing is generated and the scene stays pinned to the wrongly-matched library image.

What changed

packages/client/src/lib/asset-fuzzy-match.ts:

  • tagScore now takes a category argument and excludes the category word from both prose words and tag parts. The category prefix is universal across every candidate, so counting it adds noise without signal.
  • bestMatch now requires at least 2 specific-word overlaps (initial bestScore = 1, accept on s > bestScore). A brand-new location with only the (already stripped) category prefix in common no longer gets pinned to an arbitrary library bg.
  • The single call site in resolveAssetTag was updated to pass the category through.

Backwards-compatible behavior preserved:

  • Tags that genuinely overlap a library bg (≥ 2 specific-word matches) still resolve to the library entry — e.g. a scene tag containing both "dark" and "forest" still reuses backgrounds:user:dark_forest.
  • Tags that already exist exactly in the manifest take the early return at line 73 and never hit the fuzzy matcher.
  • Backgrounds with no fuzzy match still fall through to the backgrounds:generated:<slug> branch, which is what triggers the /game/generate-assets request the bug was suppressing.

No public type changes (tagScore and bestMatch are file-local).

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

Tested on a Dockerfile build of this branch deployed to a private EasyPanel instance:

  1. Started a fresh Game Mode chat with an image-generation connection configured and enableSpriteGeneration on. Confirmed legacy data/backgrounds/ pack present (13 jpgs incl. ancient_library.jpg) and data/game-assets/backgrounds/{fantasy,modern,scifi}/ empty.
  2. Sent the first user message, expected the scene to land in a brand-new location.
  3. Before fix: the bg snapped to ancient_library.jpg and stayed there; server logs showed bg "X" not in manifest; generation will be deferred followed by no /game/generate-assets background generation entry.
  4. After fix: the scene model's tag fell through to backgrounds:generated:<slug>, the /game/generate-assets call carried backgroundTag, and the freshly generated image rendered.
  5. Verified that a scene tag containing words that genuinely overlap a library bg (≥ 2 specific-word matches) still reuses the library entry rather than re-generating.

Edge cases box left unchecked: this is a non-UI client lib change with no light/dark, mobile, or empty-state visual surface — the matcher is invoked from applySceneResult only. Server-rendered visuals were verified against an existing Game Mode chat.

Docs and release impact

  • No docs changes needed

UI evidence (if applicable)

Not a UI change — affects the client-side asset-tag resolver only. The visible effect is "the right bg renders" (or generation begins) instead of "the wrong stock bg sticks". A demonstration would require generating a scene against a configured image provider, which I can attach as a follow-up if helpful.

Summary by CodeRabbit

  • Bug Fixes
    • Improved asset tag matching accuracy by excluding the category word from tag/prose comparisons.
    • Made background asset matching stricter to reduce false positives (requires greater overlap).
    • Avoided re-processing already-generated background tags to prevent incorrect rematches.

🔄 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/415 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/4/2026 **Status:** ✅ Merged **Merged:** 5/5/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `main` ← **Head:** `fix/game-bg-fuzzy-matcher` --- ### 📝 Commits (3) - [`dd125a1`](https://github.com/Pasta-Devs/Marinara-Engine/commit/dd125a1498586d1b97207f02dffaf4f9389855fc) fix(game): tighten bg fuzzy matcher so new scenes don't pin to first library bg - [`bbaf11f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/bbaf11f62248529a10b97e712a60390a346713e1) fix(game): scope stricter fuzzy threshold to backgrounds only - [`e6756bd`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e6756bd609ebb70a7e9a9c65d5996c95070fd157) fix(game): preserve canonical backgrounds:generated:* tags before fuzzy match ### 📊 Changes **1 file changed** (+32 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/lib/asset-fuzzy-match.ts` (+32 -9) </details> ### 📄 Description ## Linked issue Closes #414 ## Why this change In Game Mode with image generation enabled, starting a new scene renders the wrong background and never triggers a fresh generation. On installs that include the legacy SillyTavern `data/backgrounds/` pack (which gets manifest-tagged as `backgrounds:user:<name>`), unrelated scenes pin to whichever bg comes first in iteration order — typically `backgrounds:user:ancient_library`. The root cause is a fuzzy-matcher that's too lax: - `tagScore` in `packages/client/src/lib/asset-fuzzy-match.ts` counts the universal category prefix word (`backgrounds`) on both the model-emitted tag and every candidate manifest tag, giving every comparison a free +1. - `bestMatch` accepts any `score > 0`, so that single free point declares a match and returns the first tag walked. Downstream in `applySceneResult`: 1. `setCurrentBackground(resolved)` immediately sets the wrong bg. 2. The `unresolvedBg` find concludes the bg is already in the manifest, so `backgroundTag` is omitted from the `/game/generate-assets` payload — no generation request is ever made. Server-side logs `[game/scene-wrap] bg "<chosen>" not in manifest; generation will be deferred`, but the deferred call from the client never names the bg, so nothing is generated and the scene stays pinned to the wrongly-matched library image. ## What changed `packages/client/src/lib/asset-fuzzy-match.ts`: - `tagScore` now takes a `category` argument and excludes the category word from both prose words and tag parts. The category prefix is universal across every candidate, so counting it adds noise without signal. - `bestMatch` now requires at least 2 specific-word overlaps (initial `bestScore = 1`, accept on `s > bestScore`). A brand-new location with only the (already stripped) category prefix in common no longer gets pinned to an arbitrary library bg. - The single call site in `resolveAssetTag` was updated to pass the category through. Backwards-compatible behavior preserved: - Tags that genuinely overlap a library bg (≥ 2 specific-word matches) still resolve to the library entry — e.g. a scene tag containing both "dark" and "forest" still reuses `backgrounds:user:dark_forest`. - Tags that already exist exactly in the manifest take the early return at line 73 and never hit the fuzzy matcher. - Backgrounds with no fuzzy match still fall through to the `backgrounds:generated:<slug>` branch, which is what triggers the `/game/generate-assets` request the bug was suppressing. No public type changes (`tagScore` and `bestMatch` are file-local). ## 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 Tested on a Dockerfile build of this branch deployed to a private EasyPanel instance: 1. Started a fresh Game Mode chat with an image-generation connection configured and `enableSpriteGeneration` on. Confirmed legacy `data/backgrounds/` pack present (13 jpgs incl. `ancient_library.jpg`) and `data/game-assets/backgrounds/{fantasy,modern,scifi}/` empty. 2. Sent the first user message, expected the scene to land in a brand-new location. 3. Before fix: the bg snapped to `ancient_library.jpg` and stayed there; server logs showed `bg "X" not in manifest; generation will be deferred` followed by no `/game/generate-assets` background generation entry. 4. After fix: the scene model's tag fell through to `backgrounds:generated:<slug>`, the `/game/generate-assets` call carried `backgroundTag`, and the freshly generated image rendered. 5. Verified that a scene tag containing words that genuinely overlap a library bg (≥ 2 specific-word matches) still reuses the library entry rather than re-generating. Edge cases box left unchecked: this is a non-UI client lib change with no light/dark, mobile, or empty-state visual surface — the matcher is invoked from `applySceneResult` only. Server-rendered visuals were verified against an existing Game Mode chat. ## Docs and release impact - [x] No docs changes needed ## UI evidence (if applicable) Not a UI change — affects the client-side asset-tag resolver only. The visible effect is "the right bg renders" (or generation begins) instead of "the wrong stock bg sticks". A demonstration would require generating a scene against a configured image provider, which I can attach as a follow-up if helpful. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved asset tag matching accuracy by excluding the category word from tag/prose comparisons. * Made background asset matching stricter to reduce false positives (requires greater overlap). * Avoided re-processing already-generated background tags to prevent incorrect rematches. <!-- 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:09:10 +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#4613
No description provided.