[PR #5136] [MERGED] feat(game): host-run structured generation for game-surface Experiences #6647

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5136
Author: @kolacheee
Created: 8/16/2026
Status: ✅ Merged
Merged: 8/16/2026
Merged by: @kolacheee

Base: staging ← Head: feat/5135-experience-generation


📝 Commits (5)

  • ec24b16 feat(game): host-run structured generation for game-surface Experiences (#5135)
  • 4e5b83c docs: changelog entry for #5135
  • 608e856 fix(game): harden experience-generation per adversarial review (#5135)
  • 22b6bfe Merge upstream/staging: adopt the filesystem-discovered regression runner
  • 44d5d54 fix(game): apply CodeRabbit review to experience-generation (#5135)

📊 Changes

5 files changed (+563 additions, -2 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/middleware/error-handler.ts (+3 -1)
📝 packages/server/src/middleware/rate-limit.ts (+19 -1)
📝 packages/server/src/routes/game.routes.ts (+270 -0)
➕ scripts/regressions/experience-generation.regression.ts (+270 -0)

📄 Description

Closes #5135

Why

A game-surface Experience can store timeline-anchored state (#5102), but it has no way to run the ONE structured LLM call its content pipeline needs at game creation — e.g. turning the player's wizard preferences into a compact world brief that a deterministic generator compiles into a tile world ("the LLM decides what exists, the algorithm decides where every tile goes"). Packages are client-only here; the capability language-model host is reachable only from privileged server entrypoints, and abusing a visible chat turn pollutes the story and breaks on formatting whims.

What changed

POST /api/game/:chatId/experience-generation — one host-run, bounded, non-streaming structured-output call on the chat's GM connection, modeled on /game/scene-wrap (not /game/setup, whose 340 lines are wizard-context assembly and whose blind-retry/hand-repair-dialog failure mode fits a creation wizard, not a package call):

  • Gate: identical to the experience-state routes — 404 missing chat, 409 unless a game-mode chat carries a validly-stamped gameExperienceId. Only the chat's own Experience can spend tokens, only on its own chat.
  • Input: package instructions (≤16k chars) + userContent (≤8k) + optional JSON schema (≤8k serialized), forwarded as provider-native structured output where supported (OpenAI-compatible response_format: json_schema, Google responseSchema). Anthropic and the local sidecar ignore it by design, so the tolerant parser remains the real contract. responseFormat is set after gameGenOptions because its suppressModelParameters branch copies a fixed field list and silently drops it from overrides.
  • Spend bounds: a dedicated 20/min rate-limit class (the agent-suite-rewrite precedent — a buggy package loop hits this wall, not the 600/min default), the per-chat asset-generation lock, a 64KB route bodyLimit, and output tokens floored at 2048 / clamped by known-model caps (the caller may only tighten, never raise).
  • Truncation is checked BEFORE the parse. The new regression lane caught this pre-commit: parseGameJsonish happily repairs a cut-off reply by closing its unbalanced containers, which would have returned a silently amputated brief as ok: true — and there is deliberately no host-side semantic validator behind this route to catch it (that's package-owned). A finish_reason: length reply now 422s with the existing actionable "increase max output tokens" message.
  • Failure path: one illustrator-style repair round-trip (the model's bad output appended as an assistant turn plus a corrective user turn — converges far better than /setup's blind re-run), then a 422 carrying the raw text so the package can degrade to its own defaults. Never a hand-repair dialog; this is not a wizard.

New tracked lane regression:experience-generation (in regression:issues), 5 cases against a mock OpenAI-compatible provider: all gates verified to never reach the provider, happy path asserts the schema passthrough and token floor in the captured upstream request body, the repair round-trip converges in exactly two upstream calls with the corrective turns present, truncation 422s without a futile retry, and double-garbage degrades with the raw text.

Driving case: Pixelforge 0.4.0's preference-driven world generation, whose theme layer already shipped on its package branch. A two-lens adversarial review (abuse/cost + correctness/contract) is running on this diff; findings land as a follow-up commit, per the pattern of #5106.

Validation

  • pnpm check
  • pnpm regression:experience-generation (new)
  • pnpm regression:docs
  • Manually verify in browser: from a Pixelforge game chat, run a generation and confirm one bounded call on the GM connection; hammer the route and confirm the 429 wall at 20/min.

Summary by CodeRabbit

  • New Features

    • Added host-controlled structured generation for game Experiences through a JSON API.
    • Supports optional schemas, configurable output limits, connection overrides, and debug options.
    • Provides parsed JSON responses with recovery for empty or malformed output and clear errors for invalid, truncated, or unavailable results.
  • Bug Fixes

    • Improved rate-limit handling for encoded paths and duplicate slashes.
    • Standardized oversized-request error messaging across endpoints.
    • Prevents conflicting Experience generation requests from running simultaneously.

🔄 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/5136 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/16/2026 **Status:** ✅ Merged **Merged:** 8/16/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `feat/5135-experience-generation` --- ### 📝 Commits (5) - [`ec24b16`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ec24b16c1b595d95d54cefe79e3f3aaa6743cbb1) feat(game): host-run structured generation for game-surface Experiences (#5135) - [`4e5b83c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4e5b83c5dc752768a067e8555c18bd64bf65c800) docs: changelog entry for #5135 - [`608e856`](https://github.com/Pasta-Devs/Marinara-Engine/commit/608e85634464345824e93b64cddbc050c66aff3f) fix(game): harden experience-generation per adversarial review (#5135) - [`22b6bfe`](https://github.com/Pasta-Devs/Marinara-Engine/commit/22b6bfe83072cb00b0018714579f42c463548d43) Merge upstream/staging: adopt the filesystem-discovered regression runner - [`44d5d54`](https://github.com/Pasta-Devs/Marinara-Engine/commit/44d5d54f2d1388dec715ac46f9835f5948bd4e2e) fix(game): apply CodeRabbit review to experience-generation (#5135) ### 📊 Changes **5 files changed** (+563 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/middleware/error-handler.ts` (+3 -1) 📝 `packages/server/src/middleware/rate-limit.ts` (+19 -1) 📝 `packages/server/src/routes/game.routes.ts` (+270 -0) ➕ `scripts/regressions/experience-generation.regression.ts` (+270 -0) </details> ### 📄 Description Closes #5135 ## Why A game-surface Experience can store timeline-anchored state (#5102), but it has no way to run the ONE structured LLM call its content pipeline needs at game creation — e.g. turning the player's wizard preferences into a compact world brief that a deterministic generator compiles into a tile world ("the LLM decides what exists, the algorithm decides where every tile goes"). Packages are client-only here; the capability language-model host is reachable only from privileged server entrypoints, and abusing a visible chat turn pollutes the story and breaks on formatting whims. ## What changed **`POST /api/game/:chatId/experience-generation`** — one host-run, bounded, non-streaming structured-output call on the chat's GM connection, modeled on `/game/scene-wrap` (not `/game/setup`, whose 340 lines are wizard-context assembly and whose blind-retry/hand-repair-dialog failure mode fits a creation wizard, not a package call): - **Gate:** identical to the experience-state routes — 404 missing chat, 409 unless a game-mode chat carries a validly-stamped `gameExperienceId`. Only the chat's own Experience can spend tokens, only on its own chat. - **Input:** package `instructions` (≤16k chars) + `userContent` (≤8k) + optional JSON `schema` (≤8k serialized), forwarded as provider-native structured output where supported (OpenAI-compatible `response_format: json_schema`, Google `responseSchema`). Anthropic and the local sidecar ignore it by design, so the tolerant parser remains the real contract. `responseFormat` is set **after** `gameGenOptions` because its `suppressModelParameters` branch copies a fixed field list and silently drops it from overrides. - **Spend bounds:** a dedicated 20/min rate-limit class (the `agent-suite-rewrite` precedent — a buggy package loop hits this wall, not the 600/min default), the per-chat asset-generation lock, a 64KB route `bodyLimit`, and output tokens floored at 2048 / clamped by known-model caps (the caller may only tighten, never raise). - **Truncation is checked BEFORE the parse.** The new regression lane caught this pre-commit: `parseGameJsonish` happily repairs a cut-off reply by closing its unbalanced containers, which would have returned a silently amputated brief as `ok: true` — and there is deliberately no host-side semantic validator behind this route to catch it (that's package-owned). A `finish_reason: length` reply now 422s with the existing actionable "increase max output tokens" message. - **Failure path:** one illustrator-style repair round-trip (the model's bad output appended as an assistant turn plus a corrective user turn — converges far better than `/setup`'s blind re-run), then a 422 carrying the raw text so the package can degrade to its own defaults. Never a hand-repair dialog; this is not a wizard. **New tracked lane** `regression:experience-generation` (in `regression:issues`), 5 cases against a mock OpenAI-compatible provider: all gates verified to never reach the provider, happy path asserts the schema passthrough and token floor in the captured upstream request body, the repair round-trip converges in exactly two upstream calls with the corrective turns present, truncation 422s without a futile retry, and double-garbage degrades with the raw text. Driving case: Pixelforge 0.4.0's preference-driven world generation, whose theme layer already shipped on its package branch. A two-lens adversarial review (abuse/cost + correctness/contract) is running on this diff; findings land as a follow-up commit, per the pattern of #5106. ## Validation - [x] `pnpm check` - [x] `pnpm regression:experience-generation` (new) - [x] `pnpm regression:docs` - [x] Manually verify in browser: from a Pixelforge game chat, run a generation and confirm one bounded call on the GM connection; hammer the route and confirm the 429 wall at 20/min. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added host-controlled structured generation for game Experiences through a JSON API. * Supports optional schemas, configurable output limits, connection overrides, and debug options. * Provides parsed JSON responses with recovery for empty or malformed output and clear errors for invalid, truncated, or unavailable results. * **Bug Fixes** * Improved rate-limit handling for encoded paths and duplicate slashes. * Standardized oversized-request error messaging across endpoints. * Prevents conflicting Experience generation requests from running simultaneously. <!-- 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:12:24 +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#6647
No description provided.