Skip to content

fix(ai): RAG chunks cover module code and use a token size #4967

Done

Description

@dan-petty

Problem or Motivation

The Python chunker keeps top-level functions, classes and a preamble of at most 60 lines (src/devops_cli/ai/rag/chunker.py:381-427). Module-level code after the preamble is not in Qdrant:

The line-window chunker also reads chunk_size and chunk_overlap as line counts (chunker.py:694-698). A 1,000-line window goes over the 8,192-token cap and is split again.

A dense, BM25 or sparse search cannot find text that is not in a chunk.

Proposed Solution & Architectural Design

  • Chunk module-level statements after the preamble into windows of the set size.
  • Give the line-window chunker one size unit (tokens or characters), and state the unit in the setting description.
  • Merge this before the hybrid re-index of feat(ai): track embedding and reranking serving for retrieval #4961 (child 6), so that one re-index carries both changes.

Target Subsystem

src/devops_cli/ai/rag/chunker.py and the RAG settings.

Existing Tool Considered

The chunker already uses the standard library ast module, tree-sitter and the tiktoken encoding of context_budget. The change adds no dependency. Estimated cost: about 0.20M tokens and 3 hours.

Acceptance criteria

  • Chunk line coverage over the Python source files is at least 0.98. constants.py and defaults.py are each at least 0.95.
  • A test: a module with 300 lines of constants after its imports gives chunks that hold each constant.
  • No line-window chunk goes over the token cap before the token split.
  • On the eval set of test(ai): a gold set measures retrieval of knowledge base facts #3902, exact_identifier nDCG@10 does not fall (paired comparison).

Dependencies

None. The eval check of the last criterion uses #3902 when it is merged.

Source: #4961

Alternatives check of 2026-11-27: partly.
Tool: feyninc/chonkie (https://github.com/feyninc/chonkie), MIT, last activity 2026-11-24, maintained.
Reason: Searches and Chonkie's repository facts show a code chunker that splits by structure with a tokenizer-token chunk size, which covers part of the need; it gives no module-level coverage or token-cap guarantee and adds a dependency the issue rules out.

Covers:

  • Splits code into structurally meaningful chunks
  • Chunk size measured in tokenizer tokens, tiktoken included
  • One installable Python package with a token chunker

Lacks:

  • No guarantee module-level statements after the preamble are chunked
  • No project setting for chunk size or overlap with unit description
  • No clamp of a window to the 8192-token cap
  • Adds a third-party dependency the issue rules out

Proposed design

Problem statement

The Python chunker keeps only top-level functions, classes and a preamble of at most 60 lines. Module-level code after the preamble never reaches Qdrant, so dense, BM25 and sparse search cannot find it. Over 806 source files the line coverage is 0.846, and constants.py and defaults.py are near zero. The line-window chunker also reads chunk_size and chunk_overlap as line counts, so a large window exceeds the 8,192-token cap and is split again.

Acceptance criteria

Requirement: Module-level statements are chunked

The Python chunker SHALL place the module-level statements that follow the preamble into chunks of the configured token size.

Scenario: 300 constant lines after the imports

  • WHEN a Python module has 300 lines of constant assignments after its import block
  • THEN each of the 300 constant names appears in the text of a chunk

Requirement: Python source line coverage

The Python chunker SHALL cover at least 0.98 of the lines of the repository's Python source files, and at least 0.95 of the lines of constants.py and defaults.py.

Scenario: Coverage over the repository source

  • WHEN the chunker chunks every Python file under src/
  • THEN the ratio of covered lines is at least 0.98 overall, and at least 0.95 for constants.py and for defaults.py

Requirement: One size unit for the line window

The line-window chunker SHALL measure ai.rag.chunk_size and ai.rag.chunk_overlap in tokens of the chunker's count_chunk_tokens, and the description of ai.rag.chunk_size SHALL state that unit.

Scenario: A 512-token window

  • WHEN the chunker chunks a file with ai.rag.chunk_size set to 512 and ai.rag.chunk_overlap set to 64
  • THEN every chunk holds at most 512 tokens as count_chunk_tokens counts them, and the description of ai.rag.chunk_size names tokens

Requirement: Line-window chunks stay under the embedding token cap

The line-window chunker MUST clamp its window to CONST_RAG_MAX_CHUNK_TOKENS, so that no line-window chunk exceeds the 8,192-token cap before the token split step.

Scenario: A configured window larger than the cap

  • WHEN the chunker chunks a file with ai.rag.chunk_size set to 16,000 and ai.rag.chunk_overlap set to 1,600
  • THEN no chunk holds more than 8,192 tokens, and the token split step has no chunk to split

Existing Tool Considered

Chonkie (https://github.com/feyninc/chonkie, MIT, last activity 2026-11-13, maintained) covers AST-based code chunking with a chunk size in tokenizer tokens, including tiktoken tokenizers. It lacks a guarantee that module-level statements after the preamble land in a chunk, defines no ai.rag.chunk_size setting or description, and gives no guarantee that a line-window chunk stays under the 8,192-token cap; it would also add a third-party dependency, which the issue rules out. The change keeps the built-in ast and tiktoken chunker. The written cost of that code is about 0.20M tokens and 3 hours.

Key questions

  • Which item merges first, this chunker change or the hybrid re-index of feat(ai): track embedding and reranking serving for retrieval #4961?: This item merges first. The indexer skips a file whose content hash equals its cache entry, so a chunker change alone changes no file hash and an incremental run would embed nothing. The feat(ai): track embedding and reranking serving for retrieval #4961 re-index must therefore run with --force after this change, and that one forced run then carries both changes. (best practice) [sources: src/devops_cli/ai/rag/indexer.py:391, src/devops_cli/ai/rag/indexer.py:397, src/devops_cli/ai/rag/indexer.py:883]
  • Which unit should ai.rag.chunk_size and ai.rag.chunk_overlap use for the line-window chunker?: Tokens. count_chunk_tokens already counts the embedding input in the tiktoken encoding of the configured model, and the 8,192-token cap (CONST_RAG_MAX_CHUNK_TOKENS) is in the same unit, so one number bounds both the window and the cap. (best practice) [sources: src/devops_cli/ai/rag/chunker.py:28, src/devops_cli/ai/rag/chunker.py:704, src/devops_cli/ai/context_budget.py:90, src/devops_cli/config/constants.py:2804, docs/CONFIGURATION.md:245]
    • Chosen: Tokens. The cap is in tokens, and count_chunk_tokens already exists, so a token window bounds the embedding input directly.
    • Rejected: Characters. Characters vary with the content, so a character window cannot bound the token count of a chunk.
    • Rejected: Lines (current). Lines are the defect: a 1,000-line window goes over the token cap and is split again (chunker.py:694-718).
  • How does the chunker put module-level statements after the preamble into chunks?: Extend the AST pass in _chunk_python: after the preamble chunk, group the lines that no function or class chunk covers into windows of chunk_size tokens with chunk_overlap tokens of overlap, and keep the <module> symbol on the preamble chunk. (best practice) [sources: src/devops_cli/ai/rag/chunker.py:401, src/devops_cli/ai/rag/chunker.py:408, src/devops_cli/ai/rag/chunker.py:424, src/devops_cli/ai/rag/chunker.py:694]
    • Chosen: Extend the AST pass. The pass already walks tree.body and tracks covered_lines, so the uncovered lines are exactly the module-level statements; a token window over them adds no dependency.
    • Rejected: Raise the preamble line limit. A longer preamble still stops at a fixed line count, has no token bound, and would duplicate the code of the functions and classes that follow.
    • Rejected: Adopt Chonkie. It adds a dependency and does not guarantee module-level coverage or the token cap.
  • Which tool covers AST-aware, token-sized code chunking, and does the project adopt it?: Chonkie covers AST-aware chunking with a tokenizer-token chunk size. The project does not adopt it: it does not guarantee module-level coverage, defines no setting and no cap, and adds a dependency the issue rules out. The change extends the built-in chunker instead. (best practice) [sources: src/devops_cli/ai/rag/chunker.py:28, src/devops_cli/ai/rag/chunker.py:381]
    • Chosen: Extend the built-in chunker. It adds no dependency, keeps the settings and the cap in project control, and reaches the line-coverage target.
    • Rejected: Adopt feyninc/chonkie. It lacks the module-level coverage guarantee, the settings change and the cap guarantee, and adds a third-party dependency.
  • Can the exact_identifier nDCG@10 check run inside this change?: No. The eval set of test(ai): a gold set measures retrieval of knowledge base facts #3902 is still open and absent from the tree, and a paired comparison needs a live embedding run against a Qdrant collection. The chunker change has offline tests, so the nDCG check moves to its own issue. (best practice) [sources: openspec/changes/1928-rank-the-benchmark-with-numpy-and/proposal.md:63, src/devops_cli/ai/benchmark/code_chunk_corpus.py:53]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    on Nov 12, 2026
  2. added
    type/bug
    scope/ai
    status/new
    value/high
    effort/medium
    on Nov 12, 2026
  3. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor
  4. added
    priority/p2-medium
    on Nov 12, 2026
  5. devops-cli-service commented on Nov 15, 2026

    @devops-cli-service
    Contributor

    Alternatives check: partly

    Reason: Searches found Chonkie, an open Python chunking library whose CodeChunker splits source code on the AST and whose chunk size is measured in tokenizer tokens (tiktoken tokenizers are supported), so it covers the AST-aware, token-sized half of the need. It does not deliver the module-level line-coverage target for this repository's files, the settings-description and token-cap changes to the existing line-window chunker, or the eval criteria, and it would add a dependency the issue says it does not need.

    Tool: feyninc/chonkie (https://github.com/feyninc/chonkie)

    • License: MIT
    • Last activity: 2026-11-13
    • Maintained: yes

    Covers:

    • AST (tree-sitter) based chunking of source code that keeps function and class boundaries
    • chunk_size measured in tokenizer tokens, with tiktoken tokenizers available
    • an installable Python package that also writes chunks straight into Qdrant

    Lacks:

    • no guarantee that module-level statements after the preamble land in a chunk, so the 0.98 line-coverage target for constants.py and defaults.py is not met by the library itself
    • does not define or describe the RAG setting that gives the line-window chunker one size unit
    • no guarantee that a line-window chunk stays under the 8,192-token cap before the token split
    • adds a third-party dependency, while the issue states the change adds none

    Tools considered:

    Searches run:

    • Python AST-aware code chunker token-based chunk size library, results: 5
    • chonkie code chunker AST module-level code tokens, results: 5
    • chonkie CodeChunker pypi, results: 5
    • llama_index CodeSplitter AST chunk lines tokens, results: 5
    • "astchunk" python ast chunking repository, results: 5
    • chonkie chunking library github, results: 5
    • chonkie, results: 5
    • semantic-text-splitter rust python token chunking, results: 5
    • chonkie CodeChunker tree-sitter code chunking chunk_size tokens, results: 5
    • chonkie CodeChunker documentation code chunker python example, results: 5

    Sources:

    Checked on 2026-11-15. A person decides what this means for the item. The check changes no label and closes no issue.

  6. added
    status/ready
    and removed
    status/new
    on Nov 16, 2026
  7. devops-cli-service commented on Nov 16, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 0cdf3e4 (see proposed design in issue body).

    Moved to new items: #5954.

  8. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 28, 2026
  9. removed
    status/in-review
    owner/agent
    on Nov 28, 2026
  10. devops-cli-service commented on Nov 29, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-29 00:41 UTC

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

    effort/mediumpriority/p2-mediumscope/aisource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions