[GH-ISSUE #3027] [Issue]: Prompt-override editor's reverse-substitution silently drops ${cellHeight} when two variables share an example value (512), corrupting the saved sprite-sheet template #1596

Closed
opened 2026-10-21 05:40:25 +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/3027

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong
The Prompt Overrides editor reconstructs an editable ${var} template by string-replacing each variable's example value back into its token. When two variables share the same example value, every occurrence of that value is replaced with the FIRST variable's token and the second variable's token never appears. For the Sprites Expression Sheet key, cellWidth and cellHeight both have example value 512, so the editable template shows ${cellWidth} four times and no ${cellHeight}. If the user edits and saves, the persisted override has cellHeight's slots bound to ${cellWidth}, so at render time the cell-height instruction uses the cell-width value whenever they differ. The bug is silent — the preview re-expands ${cellWidth} back to 512, so it looks byte-identical to the original.

How to reproduce (try it yourself)

  1. Open Settings -> Prompt Overrides, select the "Sprites Expression Sheet" key (no existing override).
  2. Observe the editable Template textarea shows ${cellWidth}x${cellWidth} pixels and every ${cellWidth} pixels ... every ${cellWidth} pixels — ${cellHeight} is absent.
  3. Make any edit and click Save (a save with zero edits is blocked by the dirty guard, but edit-then-save is the normal flow).
  4. Generate an expression sprite sheet with cellWidth != cellHeight (e.g. cells 512x768).

You'll see: both cell dimensions in the image prompt render as the cellWidth value; the persisted override has no ${cellHeight}.
Expected: the editable default never silently binds cellHeight's slots to ${cellWidth}.

Where it is
packages/client/src/components/panels/settings/PromptOverridesEditor.tsx:59-79 (buildEditableDefaultTemplate; consumed at 191-194; preview masking at 81-97; dirty/canSave guard at 199-200). Shared example values in packages/server/src/services/prompt-overrides/registry/sprites.ts:71-81 (exampleContext) emitted at lines 51 and 57. Runtime corruption via sprites.routes.ts:1146 -> loadPrompt -> renderTemplate (packages/server/src/services/prompt-overrides/template.ts:40-51), cellWidth/cellHeight at sprites.routes.ts:233-234. Same collision also affects sprites.fullBodySheet (cellCount/poseCount both 6).

What's happening
The value->token reverse substitution has no guard for two variables sharing an example value. After the first token replacement consumes all instances of the shared value, the second variable's replacement finds nothing. The PUT route only rejects unknown variables, never missing ones, so a template lacking ${cellHeight} saves cleanly.

Suggested fix
Make buildEditableDefaultTemplate collision-aware on the client: a value is reverse-substituted to its ${token} only when it maps to EXACTLY ONE variable. When two or more variables share the same example value, leave the literal number in the editable default for all of them (the user can wire it via the existing "Available variables" chips). Concretely, count how many variables resolve to each example string and skip any example with count > 1 in the replacement loop (keep the existing length > 1 filter and longest-first sort). This fixes both the confirmed cellWidth/cellHeight case and the cellCount/poseCount twin, with no server change.

Regression avoided: do NOT use the tempting "run defaultBuilder with variable names wrapped as tokens" approach — sprites.fullBodySheet does arithmetic (Math.max(0, ctx.cellCount - ctx.poseCount)) and value-gated conditionals, so a token-string context yields NaN and silently drops whole instruction lines. The collision-aware client transform keeps defaultTemplate byte-stable so the dirty/canSave/reset guards and the preview round-trip keep working, and leaves keys without shared example values unchanged.


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/3027 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** The Prompt Overrides editor reconstructs an editable `${var}` template by string-replacing each variable's example value back into its token. When two variables share the same example value, every occurrence of that value is replaced with the FIRST variable's token and the second variable's token never appears. For the Sprites Expression Sheet key, `cellWidth` and `cellHeight` both have example value `512`, so the editable template shows `${cellWidth}` four times and no `${cellHeight}`. If the user edits and saves, the persisted override has `cellHeight`'s slots bound to `${cellWidth}`, so at render time the cell-height instruction uses the cell-width value whenever they differ. The bug is silent — the preview re-expands `${cellWidth}` back to 512, so it looks byte-identical to the original. **How to reproduce (try it yourself)** 1. Open Settings -> Prompt Overrides, select the "Sprites Expression Sheet" key (no existing override). 2. Observe the editable Template textarea shows `${cellWidth}x${cellWidth} pixels` and `every ${cellWidth} pixels ... every ${cellWidth} pixels` — `${cellHeight}` is absent. 3. Make any edit and click Save (a save with zero edits is blocked by the dirty guard, but edit-then-save is the normal flow). 4. Generate an expression sprite sheet with cellWidth != cellHeight (e.g. cells 512x768). You'll see: both cell dimensions in the image prompt render as the cellWidth value; the persisted override has no `${cellHeight}`. Expected: the editable default never silently binds `cellHeight`'s slots to `${cellWidth}`. **Where it is** `packages/client/src/components/panels/settings/PromptOverridesEditor.tsx:59-79` (`buildEditableDefaultTemplate`; consumed at 191-194; preview masking at 81-97; dirty/canSave guard at 199-200). Shared example values in `packages/server/src/services/prompt-overrides/registry/sprites.ts:71-81` (exampleContext) emitted at lines 51 and 57. Runtime corruption via `sprites.routes.ts:1146` -> `loadPrompt` -> `renderTemplate` (`packages/server/src/services/prompt-overrides/template.ts:40-51`), cellWidth/cellHeight at `sprites.routes.ts:233-234`. Same collision also affects `sprites.fullBodySheet` (cellCount/poseCount both 6). **What's happening** The value->token reverse substitution has no guard for two variables sharing an example value. After the first token replacement consumes all instances of the shared value, the second variable's replacement finds nothing. The PUT route only rejects unknown variables, never missing ones, so a template lacking `${cellHeight}` saves cleanly. **Suggested fix** Make `buildEditableDefaultTemplate` collision-aware on the client: a value is reverse-substituted to its `${token}` only when it maps to EXACTLY ONE variable. When two or more variables share the same example value, leave the literal number in the editable default for all of them (the user can wire it via the existing "Available variables" chips). Concretely, count how many variables resolve to each example string and skip any example with count > 1 in the replacement loop (keep the existing `length > 1` filter and longest-first sort). This fixes both the confirmed `cellWidth`/`cellHeight` case and the `cellCount`/`poseCount` twin, with no server change. Regression avoided: do NOT use the tempting "run defaultBuilder with variable names wrapped as tokens" approach — `sprites.fullBodySheet` does arithmetic (`Math.max(0, ctx.cellCount - ctx.poseCount)`) and value-gated conditionals, so a token-string context yields NaN and silently drops whole instruction lines. The collision-aware client transform keeps `defaultTemplate` byte-stable so the dirty/canSave/reset guards and the preview round-trip keep working, and leaves keys without shared example values unchanged. --- *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 on staging in PR #3031 (merged as f542e282). Closing manually because GitHub auto-close keywords only close issues when merged into the default branch (main), not staging.

<!-- gh-comment-id:4834697454 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed on staging in PR #3031 (merged as f542e282). Closing manually because GitHub auto-close keywords only close issues when merged into the default branch (main), not staging.
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#1596
No description provided.