[PR #1641] [MERGED] Surface invalid combat action responses instead of advancing combat #5171

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

📋 Pull Request Information

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

Base: refactor ← Head: fix/encounter-invalid-response


📝 Commits (2)

  • ef99b48 Surface invalid combat action responses instead of advancing combat
  • 703a000 Format encounter fix to repository Prettier style

📊 Changes

4 files changed (+158 additions, -0 deletions)

View changed files

📝 src/engine/contracts/types/combat-encounter.ts (+6 -0)
➕ src/engine/modes/roleplay/encounter/encounter-service.test.ts (+135 -0)
📝 src/engine/modes/roleplay/encounter/encounter-service.ts (+9 -0)
📝 src/features/modes/roleplay/encounter/hooks/use-encounter.ts (+8 -0)

📄 Description

Why this change

A malformed / non-JSON combat action response was silently treated as a successful turn. completeJsonObject returns null on unparseable output or an LLM error, but resolveRoleplayEncounterAction then ran recordValue(parsed?.result) ?? parsed (which is null) through sanitizeCombatActionResult, which rebuilds a fully valid-looking CombatActionResult from the deterministic fallback. The caller had no way to tell "sanitized a garbage response" from "a real turn", so it advanced party/enemy HP and the log, and could even reach victory + a summary writeback to the chat from a malformed reply.

What changed

  • combat-encounter.ts: add an optional invalid?: boolean to EncounterActionResponse (documented: the caller must surface a retryable error and leave combat unchanged).
  • encounter-service.ts: when completeJsonObject returns null, return { result: fallback, invalid: true } before the sanitize path runs. The fallback is returned only to satisfy the required result field; the caller must not apply it.
  • use-encounter.ts: check res.invalid first and reuse the existing recoverable error path (setError("AI returned an invalid response. Try again.") + clear processing + early return), which runs before any updateCombat/endCombat/generateSummary, so combat state and the chat are untouched and the retry controls stay mounted. No new UI.

A present-but-partial response (a real record missing result/combatStats) is still a real turn and continues through the sanitizer unchanged, and the genuine empty-input interrupted branch stays reachable, because only a null parse flags invalid.

Verification

  • pnpm exec tsc -b clean; pnpm check:architecture clean (no new cross-layer import).
  • New engine tests (encounter-service.test.ts) cover: malformed non-JSON output flags invalid, an LLM error flags invalid, a valid JSON turn resolves normally (not invalid), and a present-but-partial response is not flagged invalid.

Risk / note

This also routes a transient LLM-call failure (timeout, 5xx) into the same retryable error instead of the previous silent fallback turn. That is the intended, more-correct behavior, but it does broaden the trigger beyond pure malformed JSON, so it is called out here. The hook + inline error rendering are covered by code reading; the engine root cause is covered by the unit tests.

Linked issue

Closes #1520.


🔄 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/1641 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/30/2026 **Status:** ✅ Merged **Merged:** 5/30/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/encounter-invalid-response` --- ### 📝 Commits (2) - [`ef99b48`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ef99b48b3ffdfa7a3cbeb28a09dd523114fedd36) Surface invalid combat action responses instead of advancing combat - [`703a000`](https://github.com/Pasta-Devs/Marinara-Engine/commit/703a000b5adad52da76964a1db8902ebc05949f1) Format encounter fix to repository Prettier style ### 📊 Changes **4 files changed** (+158 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `src/engine/contracts/types/combat-encounter.ts` (+6 -0) ➕ `src/engine/modes/roleplay/encounter/encounter-service.test.ts` (+135 -0) 📝 `src/engine/modes/roleplay/encounter/encounter-service.ts` (+9 -0) 📝 `src/features/modes/roleplay/encounter/hooks/use-encounter.ts` (+8 -0) </details> ### 📄 Description ## Why this change A malformed / non-JSON combat action response was silently treated as a successful turn. `completeJsonObject` returns `null` on unparseable output or an LLM error, but `resolveRoleplayEncounterAction` then ran `recordValue(parsed?.result) ?? parsed` (which is `null`) through `sanitizeCombatActionResult`, which rebuilds a fully valid-looking `CombatActionResult` from the deterministic fallback. The caller had no way to tell "sanitized a garbage response" from "a real turn", so it advanced party/enemy HP and the log, and could even reach victory + a summary writeback to the chat from a malformed reply. ## What changed - `combat-encounter.ts`: add an optional `invalid?: boolean` to `EncounterActionResponse` (documented: the caller must surface a retryable error and leave combat unchanged). - `encounter-service.ts`: when `completeJsonObject` returns `null`, return `{ result: fallback, invalid: true }` before the sanitize path runs. The fallback is returned only to satisfy the required `result` field; the caller must not apply it. - `use-encounter.ts`: check `res.invalid` first and reuse the existing recoverable error path (`setError("AI returned an invalid response. Try again.")` + clear processing + early return), which runs before any `updateCombat`/`endCombat`/`generateSummary`, so combat state and the chat are untouched and the retry controls stay mounted. No new UI. A present-but-partial response (a real record missing `result`/`combatStats`) is still a real turn and continues through the sanitizer unchanged, and the genuine empty-input `interrupted` branch stays reachable, because only a `null` parse flags invalid. ## Verification - `pnpm exec tsc -b` clean; `pnpm check:architecture` clean (no new cross-layer import). - New engine tests (`encounter-service.test.ts`) cover: malformed non-JSON output flags invalid, an LLM error flags invalid, a valid JSON turn resolves normally (not invalid), and a present-but-partial response is not flagged invalid. ## Risk / note This also routes a transient LLM-call failure (timeout, 5xx) into the same retryable error instead of the previous silent fallback turn. That is the intended, more-correct behavior, but it does broaden the trigger beyond pure malformed JSON, so it is called out here. The hook + inline error rendering are covered by code reading; the engine root cause is covered by the unit tests. ## Linked issue Closes #1520. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:13:55 +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#5171
No description provided.