[PR #504] [MERGED] Prefer trimming old history over collapsing completion budget #4644

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/504
Author: @Xelvanis
Created: 5/7/2026
Status: ✅ Merged
Merged: 5/7/2026
Merged by: @Xelvanis

Base: main ← Head: fix/context-fit-128-token-collapse


📝 Commits (1)

  • a747a0c Prefer trimming old history over collapsing completion budget

📊 Changes

2 files changed (+86 additions, -4 deletions)

View changed files

📝 packages/server/src/services/llm/base-provider.ts (+26 -4)
📝 packages/server/test/context-fit.test.ts (+60 -0)

📄 Description

When the input prompt slightly exceeds the remaining budget, the context fitter now trims annotated history and unmarked old turns before reducing the output token budget. The output budget is only reduced when the requested budget consumes nearly the whole context window, preventing a valid large response budget from collapsing to the minimum floor.

Linked issue

Closes #

Why this change

  • Fixes a generation bug where context fitting could shrink the model’s usable completion budget down to the internal 128-token floor instead of trimming older chat history first. This could produce visibly truncated responses and make post-processing agents fail downstream.

What changed

  • Updated LLM context fitting to prefer trimming old marked chat history before reducing a usable output budget.
  • Preserved legacy/manual prompt behavior for prompts that do not annotate history with contextKind.
  • Kept important non-history prompt blocks protected by reducing output before removing broader prompt content.
  • Added regression tests for marked history, unmarked history, prompt-block protection, and oversized output-budget behavior.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • Automated validation run by Codex:
    • pnpm --filter @marinara-engine/server exec tsx --test test/context-fit.test.ts
    • pnpm check
  • Confirmed I didn't straight up break generations, but please review if this is the right fix.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

Summary by CodeRabbit

  • Bug Fixes
    • Better handling of token budget enforcement to avoid dropping protected prompt content.
  • Refactor
    • Trimming is now annotation-driven: prioritize removing annotated "history" blocks, with a fallback to older turns; system-message trimming now continues via conversation-block trimming until fits.
  • Tests
    • Added and tightened unit tests covering budget reduction and prioritized trimming behavior.

🔄 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/504 **Author:** [@Xelvanis](https://github.com/Xelvanis) **Created:** 5/7/2026 **Status:** ✅ Merged **Merged:** 5/7/2026 **Merged by:** [@Xelvanis](https://github.com/Xelvanis) **Base:** `main` ← **Head:** `fix/context-fit-128-token-collapse` --- ### 📝 Commits (1) - [`a747a0c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a747a0cf77b69cd56caa6ed24b62bdabe3f0aec3) Prefer trimming old history over collapsing completion budget ### 📊 Changes **2 files changed** (+86 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/services/llm/base-provider.ts` (+26 -4) 📝 `packages/server/test/context-fit.test.ts` (+60 -0) </details> ### 📄 Description When the input prompt slightly exceeds the remaining budget, the context fitter now trims annotated history and unmarked old turns before reducing the output token budget. The output budget is only reduced when the requested budget consumes nearly the whole context window, preventing a valid large response budget from collapsing to the minimum floor. ## Linked issue <!-- Every PR should reference a feature request or issue report. If there isn't one yet, open one first to gather opinions: --> <!-- https://github.com/Pasta-Devs/Marinara-Engine/issues/new/choose --> Closes # ## Why this change <!-- What user problem, bug, or goal does this solve? --> - Fixes a generation bug where context fitting could shrink the model’s usable completion budget down to the internal 128-token floor instead of trimming older chat history first. This could produce visibly truncated responses and make post-processing agents fail downstream. ## What changed <!-- List the key changes in this PR --> - Updated LLM context fitting to prefer trimming old marked chat history before reducing a usable output budget. - Preserved legacy/manual prompt behavior for prompts that do not annotate history with `contextKind`. - Kept important non-history prompt blocks protected by reducing output before removing broader prompt content. - Added regression tests for marked history, unmarked history, prompt-block protection, and oversized output-budget behavior. ## Validation <!-- Required: include commands run and/or manual checks performed --> - [X] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [X] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [X] Read and followed `CONTRIBUTING.md` ### Manual verification notes <!-- Describe exactly what you tested in a real browser/app, step by step. --> <!-- ⚠️ If an AI agent filled out or ticked these boxes for you, treat them --> <!-- as a TO-DO LIST, not as proof the work is done. Verify each item yourself --> <!-- before submitting. If you haven't verified it, untick the box. --> - Automated validation run by Codex: - `pnpm --filter @marinara-engine/server exec tsx --test test/context-fit.test.ts` - `pnpm check` - Confirmed I didn't straight up break generations, but please review if this is the right fix. ## Docs and release impact - [X] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) <!-- Add before/after screenshots or recordings for UI changes --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Better handling of token budget enforcement to avoid dropping protected prompt content. * **Refactor** * Trimming is now annotation-driven: prioritize removing annotated "history" blocks, with a fallback to older turns; system-message trimming now continues via conversation-block trimming until fits. * **Tests** * Added and tightened unit tests covering budget reduction and prioritized trimming behavior. <!-- 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-22 04:09:26 +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#4644
No description provided.