Skip to content

[Bug] chunk_with_boundary returns more than budget chunks whenever overlap_ratio > 0 #2111

Closed

Description

@Eapp1e

Describe the bug

chunk_with_boundary() documents a hard upper bound — Cut ``text`` into ≤ ``budget`` chunks aligned to natural boundaries (deeptutor/services/memory/consolidator/chunker.py:52, also repeated in the module docstring at line 4, in modes/update.py:7 and modes/audit.py:7) — but it sizes the window as ceil(len(text) / budget) while advancing the cursor by target - overlap. The overlap is lost at every cut, so budget chunks only reach target + (budget - 1) * (target - overlap) characters and the function needs about budget / (1 - overlap_ratio) chunks to cover the text.

This is on the default configuration: services/memory/settings.py:23 uses l2_budget = 20 and :50 uses overlap_ratio = 0.10, i.e. 21 chunks for a 20-chunk budget (see update.py:207 / audit.py:159, which emit {"stage": "chunked", "chunks": 21, "budget": 20}).

To Reproduce

from deeptutor.services.memory.consolidator.chunker import chunk_with_boundary

text = "".join(f"paragraph {i}. " + "x" * 120 + "\n\n" for i in range(200))
for budget in (5, 10, 20):
    spans = chunk_with_boundary(
        text, budget=budget, overlap_ratio=0.10,
        min_chunk_chars=1000, max_chunk_chars=64000,
    )
    print(budget, len(spans))

Observed on dev (9ce7ba9):

5 6
10 11
20 21

Randomised check (400 inputs, budget 2–25, overlap_ratio 0–0.3, 300–60000 chars): 311 inputs exceed the budget.

The existing tests/services/memory/test_chunker.py::test_budget_caps_chunk_count asserts len(chunks) <= 5, but only passes because that particular text's paragraph-boundary expansion happens to add enough characters; the same assertion fails on other equally valid inputs.

Expected behavior

len(chunk_with_boundary(...)) <= budget for every overlap_ratio that keeps the stride positive, with the last chunk still covering the end of the text. Because each chunk is one LLM call (modes/update.py:254 and :485), an extra chunk is an extra paid call.

Activity

  1. Antonio-Faure commented on Oct 31, 2026

    @Antonio-Faure
    Contributor

    Fixed on dev by commit e1c05b852 (“size chunk windows by the stride that overlap leaves”, integrated via #2126). The chunker now sizes the target from span = budget - (budget - 1) * overlap_ratio, so chunk_with_boundary stays within budget when overlap_ratio > 0. #2148 was the competing PR and is now superseded. This can be closed.

  2. Eapp1e commented on Nov 1, 2026

    @Eapp1e
    ContributorAuthor

    Fixed in #2112, which is merged. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions