[GH-ISSUE #1252] [Issue]: LTM prompt serializer drops chunks without refill and the budget ignores overhead #638

Closed
opened 2026-10-20 17:29:15 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Promansis on GitHub (Oct 7, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1252

Originally assigned to: @Promansis on GitHub.

Summary

The recall budget fills to maxTokens counting chunk text only, then the prompt serializer adds its own overhead and drops items with pop() without refilling. A high-value chunk can be dropped while a smaller one that now fits is never reconsidered.

Affected package

long-term-memory (packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/budget.ts, prompt.ts, generation-injection.ts)

Expected behavior

The serializer's overhead (preamble, framing, labels, escaping) is reserved in the budget, and when an item is dropped, previously skipped items that now fit are retried. Selected chunks are not silently lost after the budget accepted them.

Actual behavior

  • budget.ts:143-149 fills to maxTokens counting chunk text only.
  • prompt.ts:64-70 re-fits in tier order from 6 tokens; prompt.ts:73-108 does selected.pop() until the prompt fits, with no refill.
  • generation-injection.ts:170-173 passes the same maxTokens to both.

Scenario (maxTokens 4096): the budget selects a (tier 1, 100 tokens), b (tier 3, 3895 tokens, top-scoring hit) and c (tier 3, 97 tokens). The serializer keeps a and b and skips c. Overhead pushes the total past 4096, so it pops b. The prompt ends with only a, and c is never reconsidered although it fits. Whenever the budget is filled near maxTokens, the overhead guarantees at least one pop. On real sample runs at 256-512 token budgets, ~2-3 chunks per query were dropped after the budget had selected them. It does not trigger at the 4096 default with typical chunk sizes.

Steps to reproduce

  1. Set a small recall token budget (e.g. 256-512), or construct chunks as in the scenario.
  2. Run recall so the budget fills close to maxTokens.
  3. Compare the budget's selection with the serialized prompt: the top-scoring chunk is popped and skipped items are not refilled.

Environment

  • Marinara Engine version: 2.5.0 line (staging)
  • Package version: long-term-memory 1.4.7
  • Install type: source (found by code audit and tsx probes against src/)
  • OS + version: not platform-specific
  • Browser or app shell: not UI-specific

Logs, screenshots, or video

None. Full write-up: audit finding LTM-SCORE-05 (local scratch/audits/ltm-recall-scoring.md, not tracked).

Additional context

Proposed fix:

  • Reserve the serializer overhead in the budget's maxTokens.
  • After a pop, retry the previously skipped items, or drop the lowest-score item rather than the last one.
  • Add a serializer test with a large last item (pop-without-refill) and a test for how the budget accounts for overhead.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Promansis on GitHub (Oct 7, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1252 Originally assigned to: @Promansis on GitHub. ## Summary The recall budget fills to `maxTokens` counting chunk text only, then the prompt serializer adds its own overhead and drops items with `pop()` without refilling. A high-value chunk can be dropped while a smaller one that now fits is never reconsidered. ## Affected package long-term-memory (`packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/budget.ts`, `prompt.ts`, `generation-injection.ts`) ## Expected behavior The serializer's overhead (preamble, framing, labels, escaping) is reserved in the budget, and when an item is dropped, previously skipped items that now fit are retried. Selected chunks are not silently lost after the budget accepted them. ## Actual behavior - `budget.ts:143-149` fills to `maxTokens` counting chunk text only. - `prompt.ts:64-70` re-fits in tier order from 6 tokens; `prompt.ts:73-108` does `selected.pop()` until the prompt fits, with no refill. - `generation-injection.ts:170-173` passes the same `maxTokens` to both. Scenario (maxTokens 4096): the budget selects a (tier 1, 100 tokens), b (tier 3, 3895 tokens, top-scoring hit) and c (tier 3, 97 tokens). The serializer keeps a and b and skips c. Overhead pushes the total past 4096, so it pops **b**. The prompt ends with only a, and c is never reconsidered although it fits. Whenever the budget is filled near `maxTokens`, the overhead guarantees at least one pop. On real sample runs at 256-512 token budgets, ~2-3 chunks per query were dropped after the budget had selected them. It does not trigger at the 4096 default with typical chunk sizes. ## Steps to reproduce 1. Set a small recall token budget (e.g. 256-512), or construct chunks as in the scenario. 2. Run recall so the budget fills close to `maxTokens`. 3. Compare the budget's selection with the serialized prompt: the top-scoring chunk is popped and skipped items are not refilled. ## Environment - Marinara Engine version: 2.5.0 line (`staging`) - Package version: long-term-memory 1.4.7 - Install type: source (found by code audit and tsx probes against `src/`) - OS + version: not platform-specific - Browser or app shell: not UI-specific ## Logs, screenshots, or video None. Full write-up: audit finding LTM-SCORE-05 (local `scratch/audits/ltm-recall-scoring.md`, not tracked). ## Additional context Proposed fix: - Reserve the serializer overhead in the budget's `maxTokens`. - After a pop, retry the previously skipped items, or drop the lowest-score item rather than the last one. - Add a serializer test with a large last item (pop-without-refill) and a test for how the budget accounts for overhead. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-20 17:29:15 +07:00
  • closed this issue
  • added the
    bug
    label
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-Agents#638
No description provided.