[GH-ISSUE #6146] World generation sends an oversized request at mid-size lore selections, while larger selections refuse for free #3418

Closed
opened 2026-10-21 06:02:53 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Sep 13, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6146

Originally assigned to: @SpicyMarinara on GitHub.

World generation checks whether the prompt fits the connection's context window before calling the model, but it does not count the JSON reply format it sends along with that prompt. At mid-size lore selections the check says the request fits, the call is made and billed, and what actually goes out is larger than the context size the user configured. Picking even more lore behaves better: it refuses instantly and for free.

How to reproduce (try it yourself)

  1. Go to Settings, then Connections. Open the connection your Game chat uses, set Context Size to 8192, and save. (16384 behaves the same at roughly 55,000 characters of lore. Any connection under about 27,700 context is exposed.)
  2. Open a Game Mode chat running the Pixelforge Experience and go to the world setup screen.
  3. In the lorebook picker, tick entries until the selected entry content totals about 24,000 characters. Anywhere between roughly 22,000 and 26,400 characters works.
  4. Press Generate. Nothing else is needed: Pixelforge sends its own reply schema on every world generation call, and it reaches the wire as about 2,471 characters, roughly 618 tokens.

What you'll see: No context limit message. One real completion is billed, visible on your provider dashboard. By the route's own token arithmetic the request it sends is about 6,070 tokens of prompt, plus about 618 tokens of reply format, plus a 1,558 token reply reserve, so about 8,246 tokens against the 8,192 token window you just set. A provider that enforces the window rejects it and the Experience reports a generic failure.

What should happen: The pre-call check should refuse with a clear context limit message before spending the call, the way it already does when you tick more lore.

Two contrasts worth running, because they make the cause obvious:

  • Tick more lore, to about 27,000 characters. It now refuses instantly and for free with a clear context limit message. The bigger request is handled correctly, the smaller one is not.
  • Keep the same connection and select almost no lore. It fits and succeeds, as it should, which shows the check is running rather than being skipped.

Where it is

At staging ac14dd704:

  • packages/server/src/routes/game.routes.ts:11081-11085, the fit call: fitMessagesToModelAccessContext({ messages: attemptMessages, policy: modelAccessPolicy, maxTokens: options.maxTokens }). No reply format is passed, and no parameter exists to pass one.
  • packages/server/src/routes/game.routes.ts:11012 sets options.responseFormat to the caller's JSON schema, 69 lines before the fit, on the same options object the fit reads maxTokens from.
  • packages/server/src/routes/game.routes.ts:10768-10783, the only bound on that schema: 8,000 serialized characters, so the route itself permits roughly 2,000 uncounted tokens.
  • packages/server/src/services/generation/model-access-policy.ts:59-75, fitMessagesToModelAccessContext accepts only messages, policy, maxTokens and tools.
  • packages/server/src/services/llm/base-provider.ts:296-298, ContextFitOptions is a Pick of ChatOptions that deliberately omits responseFormat, so the omission is structural rather than a missed argument at one call site. Lines 497 and 529 reserve for tool definitions via estimateToolDefinitionTokens, the only non-message payload that is counted. Line 351-353 is the only cushion, max(500, 2% of context).

What's happening

The shared fitter models messages and tool definitions, and nothing else. The reply format payload is real bytes on the wire but is invisible to the budget, so the budget is short by the size of the schema minus the safety margin.

The reason the failure is a band rather than a knife edge is the output clamp at base-provider.ts:556: once the prompt exceeds the input budget, the reply reserve is set to window minus prompt minus 64. From that point on, whatever the lore size, the request total is pinned at maxContext minus safety margin minus 64 plus schema tokens. At 8192 that is a constant 8,246 tokens, which is over the window for every lore size in the band. The overflow is positive for every connection whose safety margin is under the schema size, which for Pixelforge's 618 token schema means every connection under about 27,700 context.

Suggested fix

Make the pre-call check account for everything the route puts on the wire, not just the messages. Tool definitions already have this treatment: add a responseFormat field to ContextFitOptions, an estimator beside estimateToolDefinitionTokens, thread it through fitMessagesToModelAccessContext, and pass it at game.routes.ts:11081.

One real constraint for whoever writes this: not every provider transmits the reply format. The Anthropic provider never reads it, and sidecar-inference.service.ts:260 drops it on the mlx backend, as the route's own comment at game.routes.ts:11006-11011 says. Charging schema tokens unconditionally would produce false context limit refusals on requests that would have succeeded, which on this route is the worse failure. So the reservation has to be gated on whether the resolved provider actually sends the reply format. That gating is the substance of the work, not a reason to leave the budget wrong.

Blast radius is small: the five sibling game paths send a plain JSON object format with no schema, so this is the only current caller producing a schema big enough to matter.

Context

The check this sits inside came from PR #5977, for issue #5952. The scoping discussion on #5952 is entirely about lore budgets and trimming versus refusing, so reply format accounting looks like a blind spot rather than a deliberate deferral. This is not #6129: that one is the output side, the reply budget collapsing to its floor, and its fix added the minimumOutputTokens floor. PR #6137 edited this exact code block to add that floor and also raised the request body limit from 64 KiB to 1 MiB for #6122, which makes larger lore selections easier to reach. It added no schema accounting: neither model-access-policy.ts nor base-provider.ts changed, and the fit call's arguments are byte for byte what they were before. The net effect of #6137 here was to cut off the far end of the overflow band, since very large selections now refuse for free, while leaving the near end intact. Unreleased on staging (latest release tag is v2.4.4).

One honesty note on the arithmetic: the totals above use the engine's own rough character based token estimate, and a real provider tokenizer will differ by 10 to 20 percent in either direction, so no claim is made that a specific provider rejects a specific request. The tokenizer independent claim is narrower: the route's own budget arithmetic, evaluated on its own numbers, sends more than its own declared window.

Found by an AI-assisted sweep and re-verified against staging ac14dd704 on 2026-09-13. Please verify before fixing.

Related issue from the same sweep: #6145 (the silent macro clamp on the same route).

Originally created by @kolacheee on GitHub (Sep 13, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6146 Originally assigned to: @SpicyMarinara on GitHub. World generation checks whether the prompt fits the connection's context window before calling the model, but it does not count the JSON reply format it sends along with that prompt. At mid-size lore selections the check says the request fits, the call is made and billed, and what actually goes out is larger than the context size the user configured. Picking even more lore behaves better: it refuses instantly and for free. ## How to reproduce (try it yourself) 1. Go to Settings, then Connections. Open the connection your Game chat uses, set Context Size to 8192, and save. (16384 behaves the same at roughly 55,000 characters of lore. Any connection under about 27,700 context is exposed.) 2. Open a Game Mode chat running the Pixelforge Experience and go to the world setup screen. 3. In the lorebook picker, tick entries until the selected entry content totals about 24,000 characters. Anywhere between roughly 22,000 and 26,400 characters works. 4. Press Generate. Nothing else is needed: Pixelforge sends its own reply schema on every world generation call, and it reaches the wire as about 2,471 characters, roughly 618 tokens. **What you'll see:** No context limit message. One real completion is billed, visible on your provider dashboard. By the route's own token arithmetic the request it sends is about 6,070 tokens of prompt, plus about 618 tokens of reply format, plus a 1,558 token reply reserve, so about 8,246 tokens against the 8,192 token window you just set. A provider that enforces the window rejects it and the Experience reports a generic failure. **What should happen:** The pre-call check should refuse with a clear context limit message before spending the call, the way it already does when you tick more lore. Two contrasts worth running, because they make the cause obvious: - Tick more lore, to about 27,000 characters. It now refuses instantly and for free with a clear context limit message. The bigger request is handled correctly, the smaller one is not. - Keep the same connection and select almost no lore. It fits and succeeds, as it should, which shows the check is running rather than being skipped. ## Where it is At staging ac14dd704: - `packages/server/src/routes/game.routes.ts:11081-11085`, the fit call: `fitMessagesToModelAccessContext({ messages: attemptMessages, policy: modelAccessPolicy, maxTokens: options.maxTokens })`. No reply format is passed, and no parameter exists to pass one. - `packages/server/src/routes/game.routes.ts:11012` sets `options.responseFormat` to the caller's JSON schema, 69 lines before the fit, on the same options object the fit reads `maxTokens` from. - `packages/server/src/routes/game.routes.ts:10768-10783`, the only bound on that schema: 8,000 serialized characters, so the route itself permits roughly 2,000 uncounted tokens. - `packages/server/src/services/generation/model-access-policy.ts:59-75`, `fitMessagesToModelAccessContext` accepts only messages, policy, maxTokens and tools. - `packages/server/src/services/llm/base-provider.ts:296-298`, `ContextFitOptions` is a `Pick` of ChatOptions that deliberately omits `responseFormat`, so the omission is structural rather than a missed argument at one call site. Lines 497 and 529 reserve for tool definitions via `estimateToolDefinitionTokens`, the only non-message payload that is counted. Line 351-353 is the only cushion, `max(500, 2% of context)`. ## What's happening The shared fitter models messages and tool definitions, and nothing else. The reply format payload is real bytes on the wire but is invisible to the budget, so the budget is short by the size of the schema minus the safety margin. The reason the failure is a band rather than a knife edge is the output clamp at `base-provider.ts:556`: once the prompt exceeds the input budget, the reply reserve is set to `window minus prompt minus 64`. From that point on, whatever the lore size, the request total is pinned at `maxContext minus safety margin minus 64 plus schema tokens`. At 8192 that is a constant 8,246 tokens, which is over the window for every lore size in the band. The overflow is positive for every connection whose safety margin is under the schema size, which for Pixelforge's 618 token schema means every connection under about 27,700 context. ## Suggested fix Make the pre-call check account for everything the route puts on the wire, not just the messages. Tool definitions already have this treatment: add a `responseFormat` field to `ContextFitOptions`, an estimator beside `estimateToolDefinitionTokens`, thread it through `fitMessagesToModelAccessContext`, and pass it at `game.routes.ts:11081`. One real constraint for whoever writes this: not every provider transmits the reply format. The Anthropic provider never reads it, and `sidecar-inference.service.ts:260` drops it on the mlx backend, as the route's own comment at `game.routes.ts:11006-11011` says. Charging schema tokens unconditionally would produce false context limit refusals on requests that would have succeeded, which on this route is the worse failure. So the reservation has to be gated on whether the resolved provider actually sends the reply format. That gating is the substance of the work, not a reason to leave the budget wrong. Blast radius is small: the five sibling game paths send a plain JSON object format with no schema, so this is the only current caller producing a schema big enough to matter. ## Context The check this sits inside came from PR #5977, for issue #5952. The scoping discussion on #5952 is entirely about lore budgets and trimming versus refusing, so reply format accounting looks like a blind spot rather than a deliberate deferral. This is not #6129: that one is the output side, the reply budget collapsing to its floor, and its fix added the `minimumOutputTokens` floor. PR #6137 edited this exact code block to add that floor and also raised the request body limit from 64 KiB to 1 MiB for #6122, which makes larger lore selections easier to reach. It added no schema accounting: neither `model-access-policy.ts` nor `base-provider.ts` changed, and the fit call's arguments are byte for byte what they were before. The net effect of #6137 here was to cut off the far end of the overflow band, since very large selections now refuse for free, while leaving the near end intact. Unreleased on staging (latest release tag is v2.4.4). One honesty note on the arithmetic: the totals above use the engine's own rough character based token estimate, and a real provider tokenizer will differ by 10 to 20 percent in either direction, so no claim is made that a specific provider rejects a specific request. The tokenizer independent claim is narrower: the route's own budget arithmetic, evaluated on its own numbers, sends more than its own declared window. Found by an AI-assisted sweep and re-verified against staging ac14dd704 on 2026-09-13. Please verify before fixing. Related issue from the same sweep: #6145 (the silent macro clamp on the same route).
bsakaguchi 2026-10-21 06:02:53 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Sep 13, 2026):

Codex is addressing this report on behalf of @SpicyMarinara in draft PR #6168 as part of the requested issue sweep. I will verify the reported path against staging and preserve existing behavior outside the narrow fix.

<!-- gh-comment-id:5654637183 --> @SpicyMarinara commented on GitHub (Sep 13, 2026): Codex is addressing this report on behalf of @SpicyMarinara in draft PR #6168 as part of the requested issue sweep. I will verify the reported path against staging and preserve existing behavior outside the narrow fix.
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#3418
No description provided.