[PR #6382] [MERGED] feat(game): the combat director resolves a ruleset fight on its own ledger (real combat C3a, server half) #7156

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

📋 Pull Request Information

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

Base: staging ← Head: feat/ruleset-combat-director


📝 Commits (10+)

  • 61634f8 feat(game): the ruleset fight shares its target rule, and the director contract learns a third style
  • 62687ca feat(game): the director resolves a ruleset fight on its own ledger
  • 645710c feat(game): the Game Master picks a ruleset fight's candidate from the ruleset's own numbers
  • 0cb9437 feat(game): the encounter blueprint asks for opponents in the ruleset's own terms
  • 46f2772 test(game): the ruleset fight is driven with no database, on both example rulesets
  • 78a3a18 test(game): the ruleset fight runs through the real routes, with its sheets, its boss window and its blueprint
  • ce071b8 docs(game): the guide, the plan and the handoff say a ruleset fight now runs on the server
  • ca46706 fix(game): a ruleset fight reads and writes the game state row the sheet shows, not the newest one
  • cf7ef5f fix(game): the picker leaves the dying alone and uses every target an ability may take, and a saved candidate's ruleset fields are bounded
  • ddc5081 refactor(game): the boss prompt is built once for its own style, and two small tidies from review

📊 Changes

20 files changed (+3039 additions, -189 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 docs/development/game-combat-rulesets-implementation.md (+4 -2)
📝 docs/development/game-rulesets-and-sheets-implementation.md (+64 -2)
📝 docs/extending/writing-rulesets.md (+36 -8)
📝 packages/client/src/lib/ruleset-combat-bridge.ts (+1 -22)
📝 packages/server/src/routes/combat-director.routes.ts (+323 -65)
📝 packages/server/src/routes/encounter.routes.ts (+147 -56)
📝 packages/server/src/services/game/combat-boss.service.ts (+73 -1)
📝 packages/server/src/services/game/combat-director.service.ts (+11 -1)
➕ packages/server/src/services/game/ruleset-combat-director.service.ts (+756 -0)
📝 packages/shared/src/features/combat-director.ts (+73 -1)
📝 packages/shared/src/features/ruleset-combat/creatures.ts (+52 -1)
📝 packages/shared/src/features/ruleset-combat/index.ts (+3 -0)
📝 packages/shared/src/features/ruleset-combat/options.ts (+33 -7)
📝 packages/shared/src/features/ruleset-combat/resolve.ts (+12 -19)
📝 packages/shared/src/features/ruleset-combat/types.ts (+4 -1)
📝 packages/shared/src/features/rulesets/combat-bridge.ts (+27 -0)
📝 packages/shared/src/schemas/ruleset.schema.ts (+5 -3)
➕ scripts/regressions/ruleset-combat-director-route.regression.ts (+572 -0)
➕ scripts/regressions/ruleset-combat-director.regression.ts (+842 -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.

Linked issue

Part of #6361 (real ruleset combat, slice C3a of the arc: the server half of the playable fight; it does not close the issue) and of #6314. Builds on #6377 (bestiaries and the clamp, merged).

Why this change

  • Slices C1 and C2 built the format, the resolver and the bestiary, and nothing called them. This slice is where a fight by the ruleset's own rules becomes a real, saved, resumable battle on the server.
  • The combat handoff's invariant is ONE ledger: extend the accepted-action path of the existing combat director, do not build a second one. So the ruleset fight is a third style of the director session: same storage row and namespace, same revision, instance and request-id idempotency, same per-chat queue, and the same rule that a model only ever picks a candidate id from a menu the Engine enumerated.
  • There is still no screen. The client half (the Classic shell on real numbers, the setup text, the battle bridge stepping aside, coverage.combat) is the next PR. No game starts a ruleset fight yet, so no existing battle changes.

What changed

  • Contract (features/combat-director.ts): style gains "ruleset"; the view gains ruleset, a PROJECTION for a screen (order, the actor and who controls it, each combatant's health, defense, conditions, budgets, dying tracks, concentration, an opponent's tier and traits, the legal menu with targetIds on every option, the last 200 events with a running seq, the adjustments opponents were built with, the summary at the end). A party member's sheet build, live blob, catalogs, the seed and the dice cursor never cross the wire (pinned). One new command: { type: "ruleset", optionId, targetIds, payWith? }.
  • The session (ruleset-combat-director.service.ts, new, pure over the definition and the state): the server, not the client, decides what the fight is resolved by (resolveGameRuleset, the layered definition) and reads each party member's sheet from the chat's own data with the same name matching the battle bridge uses (moved into shared so both agree). A member without a sheet is refused by name. Opponents come from the bestiary by reference or by name, else from a proposed block pulled into its tier by the clamp, else from the tier's own numbers; every adjustment and fallback is kept and logged.
  • Acting: a ruleset command is accepted only for an actor a human controls and goes through applyRulesetCombatChoice; a refusal changes nothing, bumps no revision, spends no request id and answers 400 with a stable code (ruleset_combat_<reason>). continue plays ONE whole turn of an actor nobody controls and stops, so a client gets the log turn by turn. The picker enumerates the same menu a player sees and lets the existing chooseCombatCandidate choose; it never spends a budget twice, always ends its turn, points an ability at as many opponents as it may take, and never goes for somebody who is already down.
  • Boss opponents keep today's flow: a decision window whose candidates carry the ruleset's option id, targets and label, a ruleset-aware prompt beside buildCombatBossPrompt (same JSON-only answer, same debug logging, same timeout, same call cap, same fallback to the picker on garbage), showing the ruleset's own numbers and never the seed or future dice.
  • Live sheet state: health, pools, conditions, concentration and the dying tracks are written through the sheet's own rules by the resolver, and after every accepted step the server stores them in the SAME game state row the in-game sheet shows (the last assistant message's active swipe), inside the ledger's own save. There is no end-of-battle write-back for this style, a reload mid-fight is exact, and if the write fails the step fails.
  • Resuming a fight whose ruleset is gone comes back finished with one event saying why, and the row is left alone, so reinstalling the ruleset picks the fight up again.
  • The encounter blueprint: when the game's ruleset declares combat, /encounter/init lists the threat tiers and a bounded index of the bestiary and asks for each opponent's creature, or a tier plus an optional proposed block. A malformed proposal is dropped, not the blueprint. Without combat, the prompt is byte for byte today's (pinned).
  • Docs: the authoring guide, the plan (what C3a settled), the combat handoff's status header, CHANGELOG.md. No Capability API bump: the format does not change.
  • Regressions: ruleset-combat-director (pure, scripted and seeded, on the 5e example AND on Ember Roads) and ruleset-combat-director-route (the real routes: start, idempotent restart, stale revision, replayed request id, refusal codes, the live sheet write, a swiped-back chat, an unchanged classic fight, the boss window with a good id, a bad id and garbage, the blueprint prompt).

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

Commands run locally by the agent: pnpm check (exit 0), pnpm localization:check, and the full node regression lane (pnpm regression:node): 330 of 330 on the first complete run; on the last run, made while other regression loops were running on the same machine, 329 of 330, the odd one out being image-dimension-limit.regression.ts, which printed that it passed and then did not exit inside the lane's 30 seconds. This PR does not touch it, it passes on its own in under a second, it is the same pino-pretty worker lifetime that #6359 worked around for the storage shutdown regression, and CI's node lane has passed it on every ruleset PR. Four complete local CodeRabbit sweeps were run before opening (3, 1, 3 and 0 findings), all of them tidies in the regressions and one in how the boss prompt is built.

Three things the agent's own review changed, each proven by running the new regression case against the old code first: the fight read and wrote the party's live sheet state on the NEWEST game state row, while the in-game sheet uses the row of the last assistant message's active swipe, so a player who had swiped back would have seen the sheet and the fight disagree; and the opponent picker scored somebody already down as a one-hit finish, which made every opponent execute the dying, and pointed an ability that may take three targets at one. The third came out of one of the tidies: making a conditional assertion unconditional showed that the route regression depended on who won initiative (the route draws a random seed), and the long fights that turned up showed a real fault in the picker. The shared scoring weighs a blow as a share of the target's health, so against a sturdy target a careful opponent scored a standard action above every attack it had and stood there for thirty rounds. The Engine's own picker now drops standard actions and an empty turn whenever something that hurts or heals is on the menu (a Game Master's boss window still shows everything), a finished fight records its outcome once instead of twice, and the regression pins all ten temperaments.

There is no UI in this slice, so there is nothing to click and no e2e surface. Not verified: the boss prompt against a live provider (the regression injects the model call and checks the good, bad and garbage answers); a bestiary shipped as a package asset rather than inline.

Still to verify by hand:

  • Manually confirm a Classic and a Tactical directed battle, in a game with and without a ruleset, run exactly as before.
  • Manually read the ruleset boss prompt in the debug log once the screen slice makes a ruleset fight reachable.

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)

Docs touched: CHANGELOG.md, docs/extending/writing-rulesets.md, docs/development/game-combat-rulesets-implementation.md, the plan. The [docs-i18n] follow-up for the combat slices is batched into one issue at the end of the arc, as #6371 says.

UI evidence (if applicable)

None: this slice has no UI.

Summary by CodeRabbit

  • New Features

    • Ruleset-enabled games can now resolve complete combat encounters using configured dice, character sheets, actions, opponents, and threat tiers.
    • Damage, healing, and resource changes are saved as actions occur.
    • Game Master and AI-controlled opponents choose from legal ruleset actions.
    • Encounter setup supports ruleset-specific opponent details and bestiary options.
  • Limitations

    • The ruleset combat experience is not yet displayed in the battle UI; affected games continue using Marinara’s existing battle screen.
    • Games without combat-enabled rulesets remain unchanged.

🔄 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/6382 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 9/19/2026 **Status:** ✅ Merged **Merged:** 9/19/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `feat/ruleset-combat-director` --- ### 📝 Commits (10+) - [`61634f8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/61634f87c2241ddf0b695b39a51a13f08c978bfb) feat(game): the ruleset fight shares its target rule, and the director contract learns a third style - [`62687ca`](https://github.com/Pasta-Devs/Marinara-Engine/commit/62687ca44acab65e405176ff30860401c9204c71) feat(game): the director resolves a ruleset fight on its own ledger - [`645710c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/645710c0dbbec418f9cfe45eff778ac61ac3f31f) feat(game): the Game Master picks a ruleset fight's candidate from the ruleset's own numbers - [`0cb9437`](https://github.com/Pasta-Devs/Marinara-Engine/commit/0cb94379b413a043fcd2c99eee17d61cf87f2def) feat(game): the encounter blueprint asks for opponents in the ruleset's own terms - [`46f2772`](https://github.com/Pasta-Devs/Marinara-Engine/commit/46f2772999e1ec5df36d9aceaa66e5d1e790e65e) test(game): the ruleset fight is driven with no database, on both example rulesets - [`78a3a18`](https://github.com/Pasta-Devs/Marinara-Engine/commit/78a3a182a615d6cc60c34cf8eb61f71b5fefe66a) test(game): the ruleset fight runs through the real routes, with its sheets, its boss window and its blueprint - [`ce071b8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ce071b8f037aa22243394177a60df23acd4bd8b0) docs(game): the guide, the plan and the handoff say a ruleset fight now runs on the server - [`ca46706`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ca4670691a5fcf057542d93ab73bbc4833d8e5e1) fix(game): a ruleset fight reads and writes the game state row the sheet shows, not the newest one - [`cf7ef5f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/cf7ef5f2e68bb8b96961d0a3fc5f96c4e36d9a5b) fix(game): the picker leaves the dying alone and uses every target an ability may take, and a saved candidate's ruleset fields are bounded - [`ddc5081`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ddc50817c4460a81321169d30b10b6bfc7607b8d) refactor(game): the boss prompt is built once for its own style, and two small tidies from review ### 📊 Changes **20 files changed** (+3039 additions, -189 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `docs/development/game-combat-rulesets-implementation.md` (+4 -2) 📝 `docs/development/game-rulesets-and-sheets-implementation.md` (+64 -2) 📝 `docs/extending/writing-rulesets.md` (+36 -8) 📝 `packages/client/src/lib/ruleset-combat-bridge.ts` (+1 -22) 📝 `packages/server/src/routes/combat-director.routes.ts` (+323 -65) 📝 `packages/server/src/routes/encounter.routes.ts` (+147 -56) 📝 `packages/server/src/services/game/combat-boss.service.ts` (+73 -1) 📝 `packages/server/src/services/game/combat-director.service.ts` (+11 -1) ➕ `packages/server/src/services/game/ruleset-combat-director.service.ts` (+756 -0) 📝 `packages/shared/src/features/combat-director.ts` (+73 -1) 📝 `packages/shared/src/features/ruleset-combat/creatures.ts` (+52 -1) 📝 `packages/shared/src/features/ruleset-combat/index.ts` (+3 -0) 📝 `packages/shared/src/features/ruleset-combat/options.ts` (+33 -7) 📝 `packages/shared/src/features/ruleset-combat/resolve.ts` (+12 -19) 📝 `packages/shared/src/features/ruleset-combat/types.ts` (+4 -1) 📝 `packages/shared/src/features/rulesets/combat-bridge.ts` (+27 -0) 📝 `packages/shared/src/schemas/ruleset.schema.ts` (+5 -3) ➕ `scripts/regressions/ruleset-combat-director-route.regression.ts` (+572 -0) ➕ `scripts/regressions/ruleset-combat-director.regression.ts` (+842 -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. --> ## Linked issue Part of #6361 (real ruleset combat, slice C3a of the arc: the server half of the playable fight; it does not close the issue) and of #6314. Builds on #6377 (bestiaries and the clamp, merged). ## Why this change - Slices C1 and C2 built the format, the resolver and the bestiary, and nothing called them. This slice is where a fight by the ruleset's own rules becomes a real, saved, resumable battle on the server. - The combat handoff's invariant is ONE ledger: extend the accepted-action path of the existing combat director, do not build a second one. So the ruleset fight is a third `style` of the director session: same storage row and namespace, same revision, instance and request-id idempotency, same per-chat queue, and the same rule that a model only ever picks a candidate id from a menu the Engine enumerated. - **There is still no screen.** The client half (the Classic shell on real numbers, the setup text, the battle bridge stepping aside, `coverage.combat`) is the next PR. No game starts a ruleset fight yet, so no existing battle changes. ## What changed - **Contract** (`features/combat-director.ts`): `style` gains `"ruleset"`; the view gains `ruleset`, a PROJECTION for a screen (order, the actor and who controls it, each combatant's health, defense, conditions, budgets, dying tracks, concentration, an opponent's tier and traits, the legal menu with `targetIds` on every option, the last 200 events with a running `seq`, the adjustments opponents were built with, the summary at the end). A party member's sheet build, live blob, catalogs, the seed and the dice cursor never cross the wire (pinned). One new command: `{ type: "ruleset", optionId, targetIds, payWith? }`. - **The session** (`ruleset-combat-director.service.ts`, new, pure over the definition and the state): the server, not the client, decides what the fight is resolved by (`resolveGameRuleset`, the layered definition) and reads each party member's sheet from the chat's own data with the same name matching the battle bridge uses (moved into shared so both agree). A member without a sheet is refused by name. Opponents come from the bestiary by reference or by name, else from a proposed block pulled into its tier by the clamp, else from the tier's own numbers; every adjustment and fallback is kept and logged. - **Acting**: a `ruleset` command is accepted only for an actor a human controls and goes through `applyRulesetCombatChoice`; a refusal changes nothing, bumps no revision, spends no request id and answers 400 with a stable `code` (`ruleset_combat_<reason>`). `continue` plays ONE whole turn of an actor nobody controls and stops, so a client gets the log turn by turn. The picker enumerates the same menu a player sees and lets the existing `chooseCombatCandidate` choose; it never spends a budget twice, always ends its turn, points an ability at as many opponents as it may take, and never goes for somebody who is already down. - **Boss opponents** keep today's flow: a decision window whose candidates carry the ruleset's option id, targets and label, a ruleset-aware prompt beside `buildCombatBossPrompt` (same JSON-only answer, same debug logging, same timeout, same call cap, same fallback to the picker on garbage), showing the ruleset's own numbers and never the seed or future dice. - **Live sheet state**: health, pools, conditions, concentration and the dying tracks are written through the sheet's own rules by the resolver, and after every accepted step the server stores them in the SAME game state row the in-game sheet shows (the last assistant message's active swipe), inside the ledger's own save. There is no end-of-battle write-back for this style, a reload mid-fight is exact, and if the write fails the step fails. - **Resuming a fight whose ruleset is gone** comes back finished with one event saying why, and the row is left alone, so reinstalling the ruleset picks the fight up again. - **The encounter blueprint**: when the game's ruleset declares `combat`, `/encounter/init` lists the threat tiers and a bounded index of the bestiary and asks for each opponent's `creature`, or a `tier` plus an optional proposed block. A malformed proposal is dropped, not the blueprint. Without `combat`, the prompt is byte for byte today's (pinned). - Docs: the authoring guide, the plan (what C3a settled), the combat handoff's status header, `CHANGELOG.md`. No Capability API bump: the format does not change. - Regressions: `ruleset-combat-director` (pure, scripted and seeded, on the 5e example AND on Ember Roads) and `ruleset-combat-director-route` (the real routes: start, idempotent restart, stale revision, replayed request id, refusal codes, the live sheet write, a swiped-back chat, an unchanged classic fight, the boss window with a good id, a bad id and garbage, the blueprint prompt). ## 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 Commands run locally by the agent: `pnpm check` (exit 0), `pnpm localization:check`, and the full node regression lane (`pnpm regression:node`): 330 of 330 on the first complete run; on the last run, made while other regression loops were running on the same machine, 329 of 330, the odd one out being `image-dimension-limit.regression.ts`, which printed that it passed and then did not exit inside the lane's 30 seconds. This PR does not touch it, it passes on its own in under a second, it is the same pino-pretty worker lifetime that #6359 worked around for the storage shutdown regression, and CI's node lane has passed it on every ruleset PR. Four complete local CodeRabbit sweeps were run before opening (3, 1, 3 and 0 findings), all of them tidies in the regressions and one in how the boss prompt is built. Three things the agent's own review changed, each proven by running the new regression case against the old code first: the fight read and wrote the party's live sheet state on the NEWEST game state row, while the in-game sheet uses the row of the last assistant message's active swipe, so a player who had swiped back would have seen the sheet and the fight disagree; and the opponent picker scored somebody already down as a one-hit finish, which made every opponent execute the dying, and pointed an ability that may take three targets at one. The third came out of one of the tidies: making a conditional assertion unconditional showed that the route regression depended on who won initiative (the route draws a random seed), and the long fights that turned up showed a real fault in the picker. The shared scoring weighs a blow as a share of the target's health, so against a sturdy target a careful opponent scored a standard action above every attack it had and stood there for thirty rounds. The Engine's own picker now drops standard actions and an empty turn whenever something that hurts or heals is on the menu (a Game Master's boss window still shows everything), a finished fight records its outcome once instead of twice, and the regression pins all ten temperaments. There is no UI in this slice, so there is nothing to click and no e2e surface. Not verified: the boss prompt against a live provider (the regression injects the model call and checks the good, bad and garbage answers); a bestiary shipped as a package asset rather than inline. Still to verify by hand: - Manually confirm a Classic and a Tactical directed battle, in a game with and without a ruleset, run exactly as before. - Manually read the ruleset boss prompt in the debug log once the screen slice makes a ruleset fight reachable. ## 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) Docs touched: `CHANGELOG.md`, `docs/extending/writing-rulesets.md`, `docs/development/game-combat-rulesets-implementation.md`, the plan. The `[docs-i18n]` follow-up for the combat slices is batched into one issue at the end of the arc, as #6371 says. ## UI evidence (if applicable) None: this slice has no UI. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Ruleset-enabled games can now resolve complete combat encounters using configured dice, character sheets, actions, opponents, and threat tiers. * Damage, healing, and resource changes are saved as actions occur. * Game Master and AI-controlled opponents choose from legal ruleset actions. * Encounter setup supports ruleset-specific opponent details and bestiary options. * **Limitations** * The ruleset combat experience is not yet displayed in the battle UI; affected games continue using Marinara’s existing battle screen. * Games without combat-enabled rulesets remain unchanged. <!-- 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:17:03 +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#7156
No description provided.