[PR #5921] [MERGED] fix(dice): one shared dice-notation grammar for every roller #6993

Closed
opened 2026-10-23 04:15:33 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5921
Author: @kolacheee
Created: 9/7/2026
Status: ✅ Merged
Merged: 9/7/2026
Merged by: @kolacheee

Base: staging ← Head: fix/dice-notation-consolidation


📝 Commits (10+)

  • 149c49b fix(dice): consolidate dice notation onto one shared grammar
  • 396cb68 Merge remote-tracking branch 'origin/staging' into fix/dice-notation-consolidation
  • 34ef1c1 fix(dice): hold the modifier to the same exactness bar as count and sides
  • 857dad4 Merge remote-tracking branch 'origin/staging' into fix/dice-notation-consolidation
  • 45b6b97 docs(changelog): narrow the dice grammar claim to the readers that moved
  • 9ab3793 fix(dice): the sum is the dice as thrown, not total minus modifier
  • b8441ce fix(dice): hold the whole range of totals to the exactness bar, not just the modifier
  • e050af6 docs(dice): unsnarl the header's garden-path sentence and teach the tool its own headline
  • e0a4ca2 fix(dice): a clamped roll names the dice it threw, not the ones asked for (#5921)
  • 0af0530 docs(changelog): the clamp belongs to the dice menu, not the typed /roll

📊 Changes

10 files changed (+871 additions, -86 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 docs/game/dice-and-skill-checks.md (+1 -1)
📝 packages/client/src/lib/game-tag-parser.ts (+22 -20)
📝 packages/client/src/lib/slash-commands.ts (+11 -18)
📝 packages/server/src/services/game/dice.service.ts (+18 -28)
📝 packages/server/src/services/tools/tool-executor.ts (+20 -17)
📝 packages/shared/src/features/function-calls/tools/roll-dice/manifest.ts (+2 -2)
📝 packages/shared/src/index.ts (+1 -0)
➕ packages/shared/src/utils/dice-notation.ts (+168 -0)
➕ scripts/regressions/dice-notation.regression.ts (+627 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Note

Branch: fix/dice-notation-consolidation, based on staging. One commit.
This PR does not change how a die is rolled. Same unseeded Math.random loop, same number of RNG calls, same results for the same seed. Dice honesty is the next branch's work (feat/gm-sparse-checks-server-rolled), which is stacked on this one and reviewed separately.

Linked issue

Closes #

No issue is filed for this yet — one should be opened before this PR is submitted. This is groundwork for the Game Mode dice work being planned under #5798; it does not close that issue.

Why this change

  • The engine carried four private copies of the NdM dice grammar that disagreed with each other. A bare d20 was legal in /roll, in the client's /roll slash command and in the GM's [skill_check:] tag — and an error in the roll_dice tool, which demanded an explicit count. docs/game/dice-and-skill-checks.md already promises players the permissive form, so the tool was the odd one out against both the rest of the app and its own documentation.
  • Four grammars is also the wrong foundation for the two branches queued behind this one. Both the "engine rolls the GM's checks" change and the "GM calls a real dice tool" change need one grammar they can trust; doing the consolidation first keeps those PRs about behaviour instead of about regexes.
  • Consolidating is not free of user-visible effects, and this PR names all of them below rather than describing itself as mechanical.

What changed

  • One grammar. New packages/shared/src/utils/dice-notation.ts owns parseDiceNotation, isDiceNotation, the shared ceilings and rollParsedDice. All four readers import it: dice.service (the /roll route), the roll_dice tool executor, the client slash command, and the client GM tag parser.
  • Five call-site-visible changes, all deliberate:
    1. roll_dice accepts a bare d20. This is the point of the change — the permissive form is what every other path already shipped.
    2. roll_dice trims its notation, so " 2d6 " rolls instead of erroring, and the echoed notation and display carry the trimmed text.
    3. roll_dice's invalid-notation hint now reads 2d6, d20+5, 3d8-2, so it stops teaching a die count the tool no longer requires.
    4. A count or face value too large to be an exact integer (for example 99999999999999999999d6) is now refused as invalid notation. /roll used to accept it and silently clamp to 100 dice, printing the absurd notation beside 100 rolls.
    5. 0d6 and 1d0 now produce the invalid-notation error instead of a separate out-of-range one. Unreachable through the /roll route, whose request validation already rejected both.
  • A sixth clause the commit message under-states, stated properly here: change 4 also reclassifies those same oversized inputs inside roll_dice, from Dice values out of range (1-100 dice, 2-1000 sides) to Invalid dice notation … plus the hint. Refused before, refused after — an error-message reclassification, not a verdict change — but the commit frames the rule as a /roll change only.
  • Bounds policy stays at each call site, unchanged, because the sites genuinely disagree and each is correct where it lives: /roll clamps an oversized roll (the docs promise that), roll_dice refuses it and keeps its two-face floor so it never reports dice it did not throw, and the skill-check tag refuses a label past the ceilings. A dice label still refuses a modifier — 1d20+3 names no dice — now by comparing against the parsed NdM half rather than by carrying a narrower regex.
  • Left alone deliberately: the {{roll:NdM}} macro in macro-engine.ts has its own ceilings (1000 dice, 1e6 faces), requires a count, and uses a seeded roller, so folding it in would change macro output; AnimatedDiceRoll's regex is an unanchored prefix match that reads a face count off arbitrary label text rather than a grammar.
  • New regression scripts/regressions/dice-notation.regression.ts drives one accepted/refused table through the shared grammar and then through three real entry points (dice.service, executeToolCalls, parseGmTags).

Evidence this is not a silent behaviour change

An independent check transcribed the four old implementations out of git and drove roughly 90 notations through the old and new code side by side, with Math.random replaced by a seeded generator reset before each pair so roll sequences and RNG call counts are directly comparable. The corpus covered bare dN, case, leading/trailing/interior whitespace, leading zeros, every modifier sign, both ceilings either side, the exact-integer boundary either side, 20- and 40-digit values, fullwidth and Arabic-Indic digits, NUL bytes, and about 30 malformed forms.

  • The client slash roller and the GM skill-check tag came out byte-identical, including RNG call counts, on the whole corpus.
  • The /roll service and the roll_dice tool differ on 17 and 56 rows respectively, and every one of those rows is one of the changes listed above. No undisclosed verdict flip was found on any call site.
  • RNG output matched call-for-call against a hand-written copy of the historic loop, so "no RNG change" is a checked claim rather than an assertion.
  • Deleting or altering each guard in turn: 18 of 19 such edits fail the new regression. The one that does not is named under known limitations.

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

What was run in the review worktree, for information — the boxes above are still yours to tick after you have run and seen this yourself:

  • pnpm check exited 0 (full chain through the production build). pnpm version:check reported in sync for 2.4.5.
  • dice-notation, dice-display, and the game / tool / tag regression lanes passed.
  • The full regression suite ran 209 of 210 green. The single failure is unrelated: gallery-previews.regression.ts passes every assertion, then its cleanup block fails removing a Windows temp directory. It imports only Node builtins, touches nothing this PR changes, and reproduces without this branch.

Still to verify by hand in a real browser:

  • Manually verify /roll d20, /roll 2d6+3, /roll 101d20 and /roll 1d1001 in a chat — the first two roll, the last two clamp to the ceilings the way they did before.
  • Manually verify the roll_dice tool in a chat with Function Calling on: ask for a bare d20 (now rolls), a padded " 2d6 " (now rolls, echoing the trimmed text), and 500d6 (still refused, not silently clamped).
  • Manually verify a Game Mode turn whose GM writes a [skill_check:] tag still renders its dice card exactly as before.
  • Manually verify the invalid-notation error text in the tool result reads sensibly in both light and dark mode.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

CHANGELOG.md gains one entry. No version-bearing file is touched. No file under docs/ is added, renamed or deleted, so nothing needs mirroring on docs-i18n — but see the doc-drift item below, which is a one-sentence edit somebody should make.

UI evidence (if applicable)

No UI changes. The only user-visible strings that moved are the roll_dice error and hint text inside a tool result.


Known limitations, named here rather than left for review to find

  1. The client /roll slash command has no regression entry point. It is the fourth reader the commit names, and it is the only one the new regression cannot reach. Deleting its ceiling check — the guard whose own comment says it exists so /roll 99999999d6 does not lock up the render thread — leaves the suite passing. slash-commands.ts is already importable from a regression (conversation-game-slash.regression.ts does it), so a fourth loop over the existing table would close this in roughly fifteen lines. Not done in this PR.
  2. One documentation sentence is now a shade wrong. docs/game/dice-and-skill-checks.md promises that a request over the limits is trimmed instead of refused. That is still true of everything reachable in practice, but no longer true of counts or face values past the exact-integer limit, which are now refused outright. It needs a clause, and this PR does not add one.
  3. A latent name collision in packages/shared. There are now two module-scope MAX_DICE_COUNT / MAX_DICE_SIDES pairs with different values — the shared grammar's exported 100/1000, and the macro engine's private 1000/1e6 — and the package barrel re-exports across both files. It resolves correctly today (checked), but if anyone ever exports the macro pair, the ambiguous name is dropped silently rather than erroring. Renaming the macro constants would remove the trap.
  4. The roll_dice manifest description was left alone on purpose. It still teaches 2d6, 1d20+5, 3d8-2 and does not advertise the bare d20 the tool now accepts. Changing prompt-surface text in a consolidation change felt out of scope; flagging it so the choice is visible rather than accidental.

Summary by CodeRabbit

  • New Features

    • The roll_dice tool now accepts bare notation such as d20.
    • Dice notation is handled consistently across rolling, commands, and game tags, including modifiers and whitespace.
  • Bug Fixes

    • Improved validation for invalid and out-of-range dice expressions.
    • Oversized custom rolls are capped, with results showing the dice actually rolled.
    • Unsafe totals are rejected, and results consistently report modifiers and totals.
  • Documentation

    • Updated the dice guide with capped-roll behavior and examples.
  • Tests

    • Added regression coverage across dice parsing, rolling, commands, tools, and game tags.

🔄 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/5921 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 9/7/2026 **Status:** ✅ Merged **Merged:** 9/7/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `fix/dice-notation-consolidation` --- ### 📝 Commits (10+) - [`149c49b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/149c49bf26c740d30c99569aa87fc88022d37424) fix(dice): consolidate dice notation onto one shared grammar - [`396cb68`](https://github.com/Pasta-Devs/Marinara-Engine/commit/396cb68f5ece5ac75dd8e96933ad8182d6c904bf) Merge remote-tracking branch 'origin/staging' into fix/dice-notation-consolidation - [`34ef1c1`](https://github.com/Pasta-Devs/Marinara-Engine/commit/34ef1c17f86a2b0669935110c7273ce953eb6bab) fix(dice): hold the modifier to the same exactness bar as count and sides - [`857dad4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/857dad449b62be72e5be9f0a37d0a933fca3644c) Merge remote-tracking branch 'origin/staging' into fix/dice-notation-consolidation - [`45b6b97`](https://github.com/Pasta-Devs/Marinara-Engine/commit/45b6b975164abff8b700fb15fc6007cf187a3a48) docs(changelog): narrow the dice grammar claim to the readers that moved - [`9ab3793`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9ab3793fcce31c19df19c8285d3376edbe34d3f1) fix(dice): the sum is the dice as thrown, not total minus modifier - [`b8441ce`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b8441cea211a394788b655e82b4258975e1c25eb) fix(dice): hold the whole range of totals to the exactness bar, not just the modifier - [`e050af6`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e050af631a7136a8c238863b8849e15714051e6b) docs(dice): unsnarl the header's garden-path sentence and teach the tool its own headline - [`e0a4ca2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e0a4ca2c009a7521a1f4c6bda13e3e6a4e959783) fix(dice): a clamped roll names the dice it threw, not the ones asked for (#5921) - [`0af0530`](https://github.com/Pasta-Devs/Marinara-Engine/commit/0af0530ba67c7d886bc8f8530beaad8cab14f25d) docs(changelog): the clamp belongs to the dice menu, not the typed /roll ### 📊 Changes **10 files changed** (+871 additions, -86 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `docs/game/dice-and-skill-checks.md` (+1 -1) 📝 `packages/client/src/lib/game-tag-parser.ts` (+22 -20) 📝 `packages/client/src/lib/slash-commands.ts` (+11 -18) 📝 `packages/server/src/services/game/dice.service.ts` (+18 -28) 📝 `packages/server/src/services/tools/tool-executor.ts` (+20 -17) 📝 `packages/shared/src/features/function-calls/tools/roll-dice/manifest.ts` (+2 -2) 📝 `packages/shared/src/index.ts` (+1 -0) ➕ `packages/shared/src/utils/dice-notation.ts` (+168 -0) ➕ `scripts/regressions/dice-notation.regression.ts` (+627 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. <!-- See CONTRIBUTING.md § Branches. --> > [!NOTE] > **Branch:** `fix/dice-notation-consolidation`, based on `staging`. One commit. > **This PR does not change how a die is rolled.** Same unseeded `Math.random` loop, same number of RNG calls, same results for the same seed. Dice honesty is the next branch's work (`feat/gm-sparse-checks-server-rolled`), which is stacked on this one and reviewed separately. ## Linked issue Closes # No issue is filed for this yet — one should be opened before this PR is submitted. This is groundwork for the Game Mode dice work being planned under #5798; it does not close that issue. ## Why this change - The engine carried **four private copies of the NdM dice grammar** that disagreed with each other. A bare `d20` was legal in `/roll`, in the client's `/roll` slash command and in the GM's `[skill_check:]` tag — and an error in the `roll_dice` tool, which demanded an explicit count. `docs/game/dice-and-skill-checks.md` already promises players the permissive form, so the tool was the odd one out against both the rest of the app and its own documentation. - Four grammars is also the wrong foundation for the two branches queued behind this one. Both the "engine rolls the GM's checks" change and the "GM calls a real dice tool" change need one grammar they can trust; doing the consolidation first keeps those PRs about behaviour instead of about regexes. - Consolidating is not free of user-visible effects, and this PR names all of them below rather than describing itself as mechanical. ## What changed - **One grammar.** New `packages/shared/src/utils/dice-notation.ts` owns `parseDiceNotation`, `isDiceNotation`, the shared ceilings and `rollParsedDice`. All four readers import it: `dice.service` (the `/roll` route), the `roll_dice` tool executor, the client slash command, and the client GM tag parser. - **Five call-site-visible changes, all deliberate:** 1. `roll_dice` accepts a bare `d20`. This is the point of the change — the permissive form is what every other path already shipped. 2. `roll_dice` trims its notation, so `" 2d6 "` rolls instead of erroring, and the echoed notation and display carry the trimmed text. 3. `roll_dice`'s invalid-notation hint now reads `2d6, d20+5, 3d8-2`, so it stops teaching a die count the tool no longer requires. 4. A count or face value too large to be an exact integer (for example `99999999999999999999d6`) is now refused as invalid notation. `/roll` used to accept it and silently clamp to 100 dice, printing the absurd notation beside 100 rolls. 5. `0d6` and `1d0` now produce the invalid-notation error instead of a separate out-of-range one. Unreachable through the `/roll` route, whose request validation already rejected both. - **A sixth clause the commit message under-states, stated properly here:** change 4 also reclassifies those same oversized inputs inside `roll_dice`, from `Dice values out of range (1-100 dice, 2-1000 sides)` to `Invalid dice notation …` plus the hint. Refused before, refused after — an error-message reclassification, not a verdict change — but the commit frames the rule as a `/roll` change only. - **Bounds policy stays at each call site, unchanged**, because the sites genuinely disagree and each is correct where it lives: `/roll` clamps an oversized roll (the docs promise that), `roll_dice` refuses it and keeps its two-face floor so it never reports dice it did not throw, and the skill-check tag refuses a label past the ceilings. A dice label still refuses a modifier — `1d20+3` names no dice — now by comparing against the parsed NdM half rather than by carrying a narrower regex. - **Left alone deliberately:** the `{{roll:NdM}}` macro in `macro-engine.ts` has its own ceilings (1000 dice, 1e6 faces), requires a count, and uses a seeded roller, so folding it in would change macro output; `AnimatedDiceRoll`'s regex is an unanchored prefix match that reads a face count off arbitrary label text rather than a grammar. - **New regression** `scripts/regressions/dice-notation.regression.ts` drives one accepted/refused table through the shared grammar and then through three real entry points (`dice.service`, `executeToolCalls`, `parseGmTags`). ### Evidence this is not a silent behaviour change An independent check transcribed the four old implementations out of git and drove roughly 90 notations through the old and new code side by side, with `Math.random` replaced by a seeded generator reset before each pair so roll sequences and RNG call counts are directly comparable. The corpus covered bare `dN`, case, leading/trailing/interior whitespace, leading zeros, every modifier sign, both ceilings either side, the exact-integer boundary either side, 20- and 40-digit values, fullwidth and Arabic-Indic digits, NUL bytes, and about 30 malformed forms. - The client slash roller and the GM skill-check tag came out **byte-identical**, including RNG call counts, on the whole corpus. - The `/roll` service and the `roll_dice` tool differ on 17 and 56 rows respectively, and **every one of those rows is one of the changes listed above**. No undisclosed verdict flip was found on any call site. - RNG output matched call-for-call against a hand-written copy of the historic loop, so "no RNG change" is a checked claim rather than an assertion. - Deleting or altering each guard in turn: 18 of 19 such edits fail the new regression. The one that does not is named under known limitations. ## Validation <!-- Required: include commands run and/or manual checks performed --> - [ ] `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 What was run in the review worktree, for information — the boxes above are still yours to tick after you have run and seen this yourself: - `pnpm check` exited 0 (full chain through the production build). `pnpm version:check` reported in sync for 2.4.5. - `dice-notation`, `dice-display`, and the game / tool / tag regression lanes passed. - The full regression suite ran 209 of 210 green. The single failure is unrelated: `gallery-previews.regression.ts` passes every assertion, then its cleanup block fails removing a Windows temp directory. It imports only Node builtins, touches nothing this PR changes, and reproduces without this branch. Still to verify by hand in a real browser: - [ ] Manually verify `/roll d20`, `/roll 2d6+3`, `/roll 101d20` and `/roll 1d1001` in a chat — the first two roll, the last two clamp to the ceilings the way they did before. - [ ] Manually verify the `roll_dice` tool in a chat with Function Calling on: ask for a bare `d20` (now rolls), a padded `" 2d6 "` (now rolls, echoing the trimmed text), and `500d6` (still refused, not silently clamped). - [ ] Manually verify a Game Mode turn whose GM writes a `[skill_check:]` tag still renders its dice card exactly as before. - [ ] Manually verify the invalid-notation error text in the tool result reads sensibly in both light and dark mode. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) `CHANGELOG.md` gains one entry. No version-bearing file is touched. No file under `docs/` is added, renamed or deleted, so nothing needs mirroring on `docs-i18n` — but see the doc-drift item below, which is a one-sentence edit somebody should make. ## UI evidence (if applicable) No UI changes. The only user-visible strings that moved are the `roll_dice` error and hint text inside a tool result. --- ### Known limitations, named here rather than left for review to find 1. **The client `/roll` slash command has no regression entry point.** It is the fourth reader the commit names, and it is the only one the new regression cannot reach. Deleting its ceiling check — the guard whose own comment says it exists so `/roll 99999999d6` does not lock up the render thread — leaves the suite passing. `slash-commands.ts` is already importable from a regression (`conversation-game-slash.regression.ts` does it), so a fourth loop over the existing table would close this in roughly fifteen lines. Not done in this PR. 2. **One documentation sentence is now a shade wrong.** `docs/game/dice-and-skill-checks.md` promises that a request over the limits is trimmed instead of refused. That is still true of everything reachable in practice, but no longer true of counts or face values past the exact-integer limit, which are now refused outright. It needs a clause, and this PR does not add one. 3. **A latent name collision in `packages/shared`.** There are now two module-scope `MAX_DICE_COUNT` / `MAX_DICE_SIDES` pairs with different values — the shared grammar's exported 100/1000, and the macro engine's private 1000/1e6 — and the package barrel re-exports across both files. It resolves correctly today (checked), but if anyone ever exports the macro pair, the ambiguous name is dropped silently rather than erroring. Renaming the macro constants would remove the trap. 4. **The `roll_dice` manifest description was left alone on purpose.** It still teaches `2d6, 1d20+5, 3d8-2` and does not advertise the bare `d20` the tool now accepts. Changing prompt-surface text in a consolidation change felt out of scope; flagging it so the choice is visible rather than accidental. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * The `roll_dice` tool now accepts bare notation such as `d20`. * Dice notation is handled consistently across rolling, commands, and game tags, including modifiers and whitespace. * **Bug Fixes** * Improved validation for invalid and out-of-range dice expressions. * Oversized custom rolls are capped, with results showing the dice actually rolled. * Unsafe totals are rejected, and results consistently report modifiers and totals. * **Documentation** * Updated the dice guide with capped-roll behavior and examples. * **Tests** * Added regression coverage across dice parsing, rolling, commands, tools, and game tags. <!-- 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-23 04:15:33 +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#6993
No description provided.