fix(chunker): QA audit — position invariant, recursive split, perf
Resolves the issues surfaced by a self-review of the adaptive chunking work. Tests covering each regression were added first to prove the fix. Critical - Heading splitter no longer prepends breadcrumb to Content. The breadcrumb now lives on Chunk.ContextHeader; EmbeddingContent() combines header + content for the embedder. This restores the End-Start == len(Content) invariant relied on by knowledge.go's document-reconstruction path (summary generation, UI highlight) and also eliminates the duplicate-heading regression where the section heading appeared twice in a chunk's body. - splitBySeparators is now genuinely recursive: when one separator leaves a piece > ChunkSize, the next-priority separator is applied inside that piece. Mirrors the Python reference. Without this, a document with one paragraph break followed by a long run of newline-separated lines could emit a single ~1950-rune chunk for ChunkSize=300. Medium - SplitParentChild forces children onto the recursive tier, skipping the per-parent profile pass (was N extra O(N) scans). - Heuristic splitter snaps overlap start to the nearest semantic boundary or newline instead of slicing mid-line / mid-word. - Strategy.Split returns the legacy tier's output directly when the whole chain rejects rather than running SplitText a second time. Low - ensureDefaults caps ChunkOverlap at ChunkSize/2 so pathological configs (TokenLimit clamping ChunkSize tiny while Overlap stays high) cannot produce ~97% overlap chunks. - DefaultChunkOverlap docstring spells out the migration story for KBs that stored ChunkOverlap=0. ContextHeader plumbing - chunker.Chunk and types.ParsedChunk gain ContextHeader plus an EmbeddingContent() helper. - types.Chunk gets a transient (gorm:"-") ContextHeader field so the knowledge service can build embedding inputs as titlePrefix + chunk.EmbeddingContent() without a DB schema change. https://claude.ai/code/session_01XADhx6mtu2ZYW3DE9Lun6k
This commit is contained in:
@@ -163,6 +163,24 @@ type Chunk struct {
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
// Soft delete marker, supports data recovery
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
||||
// ContextHeader is an in-memory-only context string (e.g. a Markdown
|
||||
// heading breadcrumb) that the indexing pipeline prepends to Content
|
||||
// when generating embeddings. NOT persisted — populated by the chunker
|
||||
// during initial splitting and discarded after indexing.
|
||||
ContextHeader string `json:"-" gorm:"-"`
|
||||
}
|
||||
|
||||
// EmbeddingContent returns the chunk content with ContextHeader prepended
|
||||
// when set. Use this where the embedding model needs section context that
|
||||
// isn't part of the literal Content.
|
||||
func (c *Chunk) EmbeddingContent() string {
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
if c.ContextHeader == "" {
|
||||
return c.Content
|
||||
}
|
||||
return c.ContextHeader + "\n\n" + c.Content
|
||||
}
|
||||
|
||||
// AssignChunkSeqIDs assigns sequential SeqIDs to a batch of chunks that have SeqID == 0.
|
||||
|
||||
@@ -69,11 +69,16 @@ type DocParserVLMConfig struct {
|
||||
|
||||
type ParsedChunk struct {
|
||||
Content string
|
||||
Seq int
|
||||
Start int
|
||||
End int
|
||||
Images []ParsedImage
|
||||
ChunkID string // populated by processChunks with the actual DB UUID
|
||||
// ContextHeader is an optional context string (e.g. a Markdown heading
|
||||
// breadcrumb) that should be prepended at embedding time but is NOT
|
||||
// part of the stored Content. Lets retrieval pipelines see section
|
||||
// context without breaking End-Start == len(Content) invariants.
|
||||
ContextHeader string
|
||||
Seq int
|
||||
Start int
|
||||
End int
|
||||
Images []ParsedImage
|
||||
ChunkID string // populated by processChunks with the actual DB UUID
|
||||
|
||||
// ParentIndex is set when using parent-child chunking strategy.
|
||||
// -1 (or unset/0 for flat chunks) means this is a top-level chunk.
|
||||
@@ -82,6 +87,17 @@ type ParsedChunk struct {
|
||||
ParentIndex int
|
||||
}
|
||||
|
||||
// EmbeddingContent returns the text that should be sent to the embedding
|
||||
// model: ContextHeader (if any) prepended to Content. Mirrors
|
||||
// chunker.Chunk.EmbeddingContent so the choice is consistent across the
|
||||
// chunker output and the indexing pipeline.
|
||||
func (c ParsedChunk) EmbeddingContent() string {
|
||||
if c.ContextHeader == "" {
|
||||
return c.Content
|
||||
}
|
||||
return c.ContextHeader + "\n\n" + c.Content
|
||||
}
|
||||
|
||||
// ParsedParentChunk represents a parent chunk in the parent-child strategy.
|
||||
// Parent chunks are stored in DB for context retrieval but NOT vector-indexed.
|
||||
type ParsedParentChunk struct {
|
||||
|
||||
Reference in New Issue
Block a user