mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #6146] World generation sends an oversized request at mid-size lore selections, while larger selections refuse for free #3418
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#3418
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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)
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:
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:11012setsoptions.responseFormatto the caller's JSON schema, 69 lines before the fit, on the same options object the fit readsmaxTokensfrom.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,fitMessagesToModelAccessContextaccepts only messages, policy, maxTokens and tools.packages/server/src/services/llm/base-provider.ts:296-298,ContextFitOptionsis aPickof ChatOptions that deliberately omitsresponseFormat, so the omission is structural rather than a missed argument at one call site. Lines 497 and 529 reserve for tool definitions viaestimateToolDefinitionTokens, 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 towindow minus prompt minus 64. From that point on, whatever the lore size, the request total is pinned atmaxContext 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
responseFormatfield toContextFitOptions, an estimator besideestimateToolDefinitionTokens, thread it throughfitMessagesToModelAccessContext, and pass it atgame.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:260drops it on the mlx backend, as the route's own comment atgame.routes.ts:11006-11011says. 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
minimumOutputTokensfloor. 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: neithermodel-access-policy.tsnorbase-provider.tschanged, 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
ac14dd704on 2026-09-13. Please verify before fixing.Related issue from the same sweep: #6145 (the silent macro clamp on the same route).
@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.