Skip to content

fix(ai): rag chunks stay under the embedder's real token limit, not a four-characters-per-token estimate #1300

Closed

Description

@dan-petty

Problem

Copilot thread 6 on #1283, verified at 61f9b67. Chunks are bounded at 8,192 estimated tokens using four characters per token [code: src/devops_cli/ai/rag/chunker.py:26]. Text made of short identifiers or punctuation can exceed the embedding model's real limit and fail indexing. For v0.2.28, #1296 only corrects the wording. Note that context_budget.count_tokens uses gpt-4o's tokenizer, not the embedder's.

Acceptance criteria

  • Chunk boundaries are decided with a tokenizer that matches the configured embedding model, or with a documented, safe bound for it, and the duplicate estimate_tokens goes. A test with dense punctuation stays under the limit.
  • uv run devops ci passes.

Strategic alignment

Update 2026-10-08: refinement

The 2026-10-08 comment folds the chunker refactor in: code chunks on ai/ast spans, YAML per document, Markdown on markdown-it headings, and _LANGUAGE_PATTERNS, _C_LIKE_PATTERN, _chunk_by_regex_symbols and the wrappers (ai/rag/chunker.py:79-486) go.

Activity

  1. added
    type/bug
    scope/ai
    priority/p2-medium
    on Oct 7, 2026
  2. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Folded here instead of a new issue: the agent filing quota (#1153) is spent for this cycle, and this refactor shares this item's change surface. It is recorded here in full so it is not lost.

    refactor(ai): the rag chunker splits code on ai/ast's tree-sitter symbols, YAML on PyYAML nodes and markdown on markdown-it headings

    Problem

    Checked at release/v0.2.29 (05e9388). The RAG chunker (ai/rag/chunker.py) finds structure with regexes and line prefixes, although the parsers it needs are already dependencies.

    • Code. Go, Rust, TypeScript, JavaScript, Terraform and HCL, SQL and every C-like language are split on line-anchored regexes (_LANGUAGE_PATTERNS and _C_LIKE_PATTERN, :79-158; _chunk_by_regex_symbols, :486).
    • YAML. Chunk symbols come from lines starting with kind: and name: (:571-574), so a nested name: becomes a symbol. PyYAML is a direct dependency.
    • Markdown. Sections start at lines matching ^(#{1,4})\s+(.+)$ (:605), so a # comment line inside a fenced code block starts a section. markdown-it-py is a direct dependency, already used at release/changelog_fragments.py:126 and roadmap/close.py:131.

    Proposed remedy

    • Code: chunk on the PolyglotSymbol spans that ai/ast returns, for every language it parses. A language without a grammar falls back to line windows, not a regex table. Delete _LANGUAGE_PATTERNS, _C_LIKE_PATTERN, _chunk_by_regex_symbols and the per-language wrappers (_chunk_go, _chunk_rust, _chunk_js_ts, _chunk_c_like, _chunk_terraform, _chunk_sql).
    • YAML: yaml.compose_all with the safe loader. Each document is one chunk; its symbols come from the document's top-level kind and metadata.name nodes and its lines from the nodes' marks.
    • Markdown: sections come from the map line ranges of the heading tokens MarkdownIt("commonmark").parse returns. This is one public function, so the docs ingester can share it.
    • AsciiDoc and reStructuredText keep line windows; no dependency parses them.
    • Chunks stay within fix(ai): rag chunks stay under the embedder's real token limit, not a four-characters-per-token estimate #1300's token bound. Python keeps its ast path.

    Key questions

    • Does the change need a re-index? (proposed) Yes. Chunk boundaries and ids change, so the changelog fragment tells users to run devops ai rag index --force.

    Acceptance criteria

    • A table test per language with a grammar: an indented definition (a Rust fn in an impl, a TypeScript class method), a multi-line signature and a symbol-like line inside a block comment give the chunks tree-sitter reports. The indented-definition and comment rows fail on the current code.
    • A Markdown file whose fenced code block contains # comment lines gets no section boundary inside the fence. The test fails on the current code.
    • A two-document YAML file with a nested name: gives one chunk per document, whose symbols are the top-level kind and metadata.name only. The test fails on the current code.
    • A Hypothesis property test feeds generated input to each chunker and checks that it never raises and that every chunk's line range lies inside the file.
    • rg -n '_LANGUAGE_PATTERNS|_C_LIKE_PATTERN|_chunk_by_regex_symbols' src prints nothing.
    • The tests are pure parsing: offline, with no fakes and no patches.
    • Each new or changed test runs in under 1 s.
    • changelog.d/<n>.md records the change; CHANGELOG.md is not edited.
    • uv run devops ci passes.
    • (person-run) After devops ai rag index --force, devops ai rag query for a Rust impl method and a TypeScript class method returns the method's chunk.

    Related: #959, #1300, #1329, #1388, #1066

    Found by the 2026-10-08 client-library audit.

  3. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Linked: #540, which settles one language classifier; this item chunks on ai/ast spans but picks the language with the scanner's, so it builds on #540's answer. Found by the 2026-10-08 backlog review.

  4. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. It causes indexing failures for certain text patterns but is an edge case rather than a total outage, and no security advisory or failed CI run is cited.
    • Value: Medium. Fixes a real correctness bug that breaks RAG indexing for text with short identifiers or dense punctuation, but only affects a subset of inputs.
    • Effort: High. Beyond swapping the estimator, the refinement requires a full per-language chunker rewrite, removal of legacy regex chunking, and Hypothesis property tests.
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

    priority/p2-mediumscope/aitype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions