mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #4124] [MERGED] Keep single-shot prompts intact when max_tokens eats the context window #6241
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#6241
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?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/4124
Author: @Gunterlie
Created: 7/26/2026
Status: ✅ Merged
Merged: 7/26/2026
Merged by: @SpicyMarinara
Base:
staging← Head:fix/context-fit-drops-single-shot-prompt📝 Commits (2)
4910777Keep single-shot prompts intact when max_tokens eats the context window1c65b43Merge branch 'staging' into fix/context-fit-drops-single-shot-prompt📊 Changes
3 files changed (+74 additions, -3 deletions)
View changed files
📝
package.json(+1 -1)📝
packages/server/src/services/llm/base-provider.ts(+12 -2)➕
scripts/regressions/context-fit.regression.ts(+61 -0)📄 Description
Linked issue
Closes #4110
Why this change
A user reported Noodle generating posts with no character cards and no lorebook entries. Their captured request to the model explains why — the prompt body was never sent:
buildRefreshPromptreturns three messages: system rules, the context body (# Active Noodle Accounts,# Character Profiles,# World / Lore, the timeline), then the JSON-format instruction (noodle-public-prompt.service.ts:621). Only two arrived. The entire context body was deleted in transit, so the model received rules and an output schema with nothing to write about — and invented accounts (user_123,Kaelen_Storm) to fill the void. Those invented handles then failedvalidateNoodleGeneratedRefresh, so the refresh died two steps downstream with a "no usable response" error that pointed nowhere near the real problem.The deletion happens in
fitMessagesToContext:findOldestRemovableConversationBlock(base-provider.ts:332) skips system messages and protects only the final message (index < messages.length - 1). Everything else is fair game.contextKind: "history", the fallback pass at:460removes unmarked non-system messages anyway. Noodle setscontextKindnowhere, so its context body is the first thing deleted and the trailing instruction is the thing kept.max_tokensis only reduced when the input budget has already fallen to the 128-token floor (:422). Between those two regimes there is a window where the budget is too small for the prompt but too large to trigger the reduction — so the output budget keeps its full value while the prompt is thrown away.Concretely, a connection storing
max_tokens: 32768against a ~36k context window leaves ~3.3k tokens of input budget. A ~4k-token prompt exceeds it, so the body is deleted andmax_tokensstays at 32768 — matching the captured request exactly, including the 1210 prompt tokens the local backend reported.This is not Noodle-specific. Any single-shot prompt builder — summarizers, one-off generators — has no history to annotate and is exposed the same way.
Why it surfaced now (it is not a recent regression)
This looked like fallout from the recent Noodle work. It isn't — both code ingredients are old:
max_tokens—a747a0cf7(2026-05-07), "Prefer trimming old history over collapsing completion budget", which introduced the exactinputBudget <= reservedInputFloorgate this PR relaxes.max_tokensrather than its own computed budget —958e5534f(2026-07-16) and the service extraction in0029d9551(2026-07-18).Neither matches the "worked in the morning, broken by evening" report, because the third ingredient is not code — it is prompt size. The body is deleted as soon as
context tokens > usableWindow − max_tokens. Withmax_tokens: 32768against a ~36k window that threshold sits near 3.3k tokens, and the Noodle context message grows on its own with normal use: the 48h recent-post window (up to 100 posts), recalled older memories, the number of invited characters, and lorebook context all add to it. No deploy is needed to cross the line — only continued use. It also explains the intermittency the reporter saw ("I refreshed a few times"): as the 48h window rolls, the prompt shrinks and grows, so the failure can appear to resolve itself and return.This is why the bug is invisible on a typical dev setup. A connection with no stored
max_tokensoverride, or a large context window, leaves an input budget of tens of thousands of tokens and never approaches the threshold. The trap is config-shaped, not version-shaped.Worth stating plainly for planning purposes: the recent Noodle features (recalled memories, character schedules, enhanced timeline writing, lorebook context) are not buggy, but each one makes the prompt richer and therefore moves affected users closer to a threshold that already existed. That argues for the "fail loudly when the prompt cannot fit" follow-up rather than against the features.
What changed
base-provider.ts— a prompt with nocontextKind: "history"message has nothing that is safe to drop, so the output budget yields instead: the existingmax_tokensreduction now also applies when there is no removable history, before any message is deleted. The later fallback passes are untouched, so a prompt that genuinely cannot fit is still handled.contextKind: "history", so history trimming still runs first and a large valid response budget does not collapse to the floor.scripts/regressions/context-fit.regression.ts(new, wired intopnpm regression:prompt) — asserts a Noodle-shaped single-shot prompt survives a 36864/32768 squeeze with its body intact and a usable reducedmax_tokens; that a roomy window changes nothing; that an annotated conversation still trims history; and that an unconfigured context window touches nothing.Validation
pnpm checkpasses locallyCONTRIBUTING.mdManual verification notes
# Character Profilesand# World / Lorepresent.[LLM context] Trimmed prompt ...for a normal Noodle refresh on that connection.Automated, run locally:
pnpm --filter @marinara-engine/server exec tsx ../../scripts/regressions/context-fit.regression.ts— passes with this change, fails without it (verified by revertingbase-provider.ts; it fails on "no message may be dropped from a single-shot prompt").pnpm check— passes (one pre-existing unrelated client lint warning, 0 errors).Docs and release impact
docs-i18nbranch updated to match, or a[docs-i18n]follow-up issue opened (see CONTRIBUTING.md § Translated documentation)UI evidence (if applicable)
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.