feat(chunker): heading-aware Tier 1 splitter

splitByHeadingsImpl detects the dominant Markdown heading level via the
profiler and splits the document at headings of that level (or shallower).
Each resulting chunk gets a breadcrumb prefix like
'# Chapter 1\n## Section 1.2' so downstream embedding sees the section
context, not just the body. Sections larger than ChunkSize fall back to
the legacy splitter for inner segmentation, with the breadcrumb prepended
to every sub-chunk and ChunkSize reduced by the breadcrumb length so the
final chunk still fits the budget.

- DominantHeadingLevel: prefer the lowest level with ≥3 hits, else the
  deepest level with ≥1 hit (so small docs with H1+H2 still split at H2)
- HeadingHierarchy state is updated for sub-headings within a section so
  later primary sections see the right active stack
- Code-block contents are skipped during heading detection so a `# foo`
  inside ``` doesn't create a fake boundary
- strategy.go uses context.Background() for the fallback log entry
  (GetLogger panics on nil context)

https://claude.ai/code/session_01XADhx6mtu2ZYW3DE9Lun6k
This commit is contained in:
Claude
2026-05-02 09:26:40 +00:00
committed by lyingbug
parent 592d3618dc
commit cf8789f84c
4 changed files with 346 additions and 11 deletions
+6 -2
View File
@@ -7,7 +7,11 @@
// compatibility — strategy.go simply layers a tier-selector on top.
package chunker
import "github.com/Tencent/WeKnora/internal/logger"
import (
"context"
"github.com/Tencent/WeKnora/internal/logger"
)
// Strategy values for SplitterConfig.Strategy.
const (
@@ -36,7 +40,7 @@ func Split(text string, cfg SplitterConfig) []Chunk {
if v := ValidateChunks(out, totalChars, cfg.ChunkSize); v.OK {
return out
} else {
logger.Debugf(nil, "chunker: tier %s rejected: %s", tier, v.Reason)
logger.Debugf(context.Background(), "chunker: tier %s rejected: %s", tier, v.Reason)
}
}
// Last-ditch fallback: always return *something*.