Repository navigation
feat: AST-aware chunk boundaries for semantic splitting - #55
Conversation
There was a problem hiding this comment.
Pull request overview
Updates the Rust chunking logic to prefer semantic (AST-like) chunk boundaries so that substantive, differently-named top-level definitions are split into separate chunks even when they still fit under the soft token budget.
Changes:
- Added AST-aware split logic to
ChunkingService::chunk_spans()to separate substantive top-level definitions with different names. - Introduced unit tests in
codetriever-parsingvalidating the new boundary behavior. - Updated the
codetriever-indexingintegration test to reflect the new 3-chunk behavior for consecutive functions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| crates/codetriever-parsing/src/chunking/service.rs | Adds AST-aware split heuristic and new unit tests for definition-boundary chunking behavior. |
| crates/codetriever-indexing/tests/chunking_tests.rs | Updates integration test expectations/comments for new per-function chunking behavior. |
|
@copilot re-review please — addressed all feedback: current_kind/current_name now updated when accumulating definition spans, name comparison requires both Some, threshold uses multiplication (tokens*4 >= soft) to avoid integer division rounding. |
All three issues have been addressed in commit
All 49 tests pass. |
Adds semantic boundary detection to chunk_spans() so that substantive top-level definitions (function, method, class, struct, enum, trait, impl, interface) each get their own chunk rather than being greedily packed together. Split logic: when an incoming span is a named definition AND the accumulated buffer already holds a different named definition AND the buffer is substantive (>= 25% of soft token limit), emit the current chunk and start fresh. Tiny helpers below the 25% threshold continue to pack with the following span since they are almost certainly related context (improves similarity scores ~0.050 per published research). Ref #46 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
The existing test_chunking_service_simple expected 2 chunks because the old greedy logic packed unrelated functions together. With AST-aware boundaries, each substantive named definition gets its own chunk — the correct semantic behavior. Updated assertions from 2 to 3 chunks. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Track most recent definition kind/name when accumulating spans, so boundary check works even when non-definitions precede a definition - Require both span names to be Some before splitting on name difference (None vs Some no longer forces a split) - Use multiplication (tokens*4 >= soft) instead of integer division (soft/4) to avoid rounding errors in the 25% threshold Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
9b25445 to
968da43
Compare
Related Issue
Description
Updates the Rust chunking logic to prefer semantic (AST-like) chunk boundaries so that substantive, differently-named top-level definitions are split into separate chunks even when they still fit under the soft token budget.
chunk_spans()now prefers splitting at function boundaries instead of greedily packing unrelated definitions togetherSome) with different names and accumulated content is substantive (≥25% of soft token limit)current_kind/current_nameare updated when accumulating definition spans, ensuring boundary splits work correctly even when non-definition spans (e.g., imports/comments) precede a definition in the same chunkSome—NonevsSome(_)no longer forces an unintended split for unnamed definitionscurrent_tokens.saturating_mul(4) >= softinstead of integer division to avoid rounding errors in the ≥25% checkimpl MyStruct) stay packedType of Change
bugfeaturerefactordocumentationstyleperformancetestsbreakingsecurityinfrastructureVisuals
N/A
Test Plan
test_separate_named_functions_get_own_chunks— two substantive functions → 2 chunkstest_small_helper_packed_with_next_function— tiny helper packs with large functiontest_non_definition_spans_still_pack— imports/comments pack as beforetest_same_name_definitions_pack— same-named impl blocks stay togethertest_chunking_service_simpleintegration test for new 3-chunk behaviorChecklist
Additional Notes
🤖 Generated with Claude Code