Repository navigation
Description
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:
constants.pyhas a line coverage of 0.01 anddefaults.py0.05.- Over 806 source files, the line coverage is 0.846.
- In the identifier probe of the embedding serving study (feat(ai): track embedding and reranking serving for retrieval #4961), 2 of 10 misses were module constants that no search arm can find.
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.pyanddefaults.pyare 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.pyand fordefaults.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_sizeset to 512 andai.rag.chunk_overlapset to 64 - THEN every chunk holds at most 512 tokens as
count_chunk_tokenscounts them, and the description ofai.rag.chunk_sizenames 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_sizeset to 16,000 andai.rag.chunk_overlapset 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
--forceafter 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]- Chosen: This item first. The re-index of feat(ai): track embedding and reranking serving for retrieval #4961 is forced after it, so one embedding run covers the chunker change and the hybrid index.
- Rejected: The re-index first. The indexer skips unchanged files by content hash (
indexer.py:397), so a second forced re-index would be needed after this change, at twice the embedding cost. - Rejected: Both in one pull request. The chunker change has offline tests, while the hybrid re-index is an operational run of feat(ai): track embedding and reranking serving for retrieval #4961, so they stay separate items.
- Which unit should
ai.rag.chunk_sizeandai.rag.chunk_overlapuse for the line-window chunker?: Tokens.count_chunk_tokensalready 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_tokensalready 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).
- Chosen: Tokens. The cap is in tokens, and
- 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 ofchunk_sizetokens withchunk_overlaptokens 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.bodyand trackscovered_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.
- Chosen: Extend the AST pass. The pass already walks
- 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
@10check 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]- Chosen: Defer to a follow-up issue. The criterion needs test(ai): a gold set measures retrieval of knowledge base facts #3902's gold set and a live embedding run, neither of which this change can build or check offline.
- Rejected: Keep the criterion here. The check would hold the chunker change until test(ai): a gold set measures retrieval of knowledge base facts #3902 merges and a Qdrant run completes, and no offline test can assert it.
- Rejected: Drop the criterion. The issue asks for the check, so it stays as a filed follow-up.
Fit
Fits in one pull request.
Suspected block
None.
Dependencies
None.
Activity
dan-petty commentedon Nov 12, 2026
Both touch the token cap in chunker.py, but #1300 fixes which tokenizer computes the cap and the candidate changes the chunk-size unit from line count to tokens.devops-cli-service commentedon Nov 15, 2026
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)
Covers:
Lacks:
Tools considered:
Searches run:
Sources:
Checked on 2026-11-15. A person decides what this means for the item. The check changes no label and closes no issue.
devops-cli-service commentedon Nov 16, 2026
Status set to Ready at 0cdf3e4 (see proposed design in issue body).
Moved to new items: #5954.
devops-cli-service commentedon Nov 28, 2026
Released in v0.241.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.241.0
devops-cli-service commentedon Nov 29, 2026
Live since 2026-11-29 00:41 UTC