Repository navigation
Description
Problem
During review of #17868 a mom semantics divergence was found on the text/markdown/html path of TokenChunker (internal/ingestion/component/chunker/token.go).
invokeTextPayload forks on whether a primary delimiter is active (token.go:306):
- Branch B (active primary delimiter) →
applyChildrenDelim(token.go:1209) setsMom: seg, i.e. the primary-split segment. This matches Python (token_chunker.py:330-339,"mom": c). - Branch A (no active primary delimiter — e.g. default
delimiter_mode="token_size"+children_delimitersset) →mergeByTokenSize(token.go:554) builds token-merged chunks, thenapplyChildrenDelimText(token.go:1236, called attoken.go:672) setsMom: twheret = d.Text, i.e. the token-merged chunk produced bymergeByTokenSize.
Python always sets mom to the parent segment c it split children from (_split_text_by_pattern(c, custom_pattern) → "mom": c). In default mode c comes from naive_merge. Go's mergeByTokenSize uses sentenceDelimiter + greedy token merge, a different algorithm from Python's naive_merge, so the parent unit differs and mom content/boundaries diverge (only visible when the text spans multiple chunks; single-chunk text happens to match).
Secondary fragility: applyChildrenDelimText re-derives mom from d.Text instead of preserving any incoming Mom (unlike applyChildrenDelim, which takes the parent as an explicit seg arg). A doc that already carried a meaningful Mom flowing into applyChildrenDelimText would have it silently overwritten.
The JSON path (splitByChildren, token.go:733) is unaffected — its mom = ck.Text (the item text) matches Python.
Impact
- Parity harness flags
momdiffs for text-path children cases that span multiple chunks. - Downstream logic relying on
momas the child's parent-context pointer gets a Go-specific (different-granularity) segment vs Python. Embeddings are unaffected (momis typically not embedded), but reassembly / parent-context display can diverge. - Latent
Momoverwrite inapplyChildrenDelimText.
Correct behavior
Every child's mom must equal the exact parent segment it was split from, at the same granularity/boundaries as Python's naive_merge segment c (default mode) or primary-split segment (delimiter mode).
Proposed fix
- Low-risk (do first): make
applyChildrenDelimTextpreserve any incomingMom, falling back tod.Textonly when empty — symmetric withapplyChildrenDelim:mom := d.Mom if mom == "" { mom = d.Text } out = append(out, schema.ChunkDoc{Text: child, Mom: mom})
- Real alignment (larger effort, separate PR): align the Branch A merge with Python's
naive_mergeso children split operates on parent segments of the same granularity as Python. This is part of the broader Go↔Python merge-alignment work, not in scope for fix(chunker): drop delimiter from chunk text on primary and children splits #17868.
Scope
Go-side only. Python (rag/flow/chunker/token_chunker.py) is the reference and is correct; it must stay untouched. This divergence is pre-existing and unrelated to #17868 (which only changed delimiter retention in text, not mom).
🤖 Generated with CodeBuddy Code
Activity
test(chunker): drop historical-behaviour wording from Mom test comment
test(chunker): describe current Mom behaviour without history wording
fix(chunker): preserve incoming Mom in applyChildrenDelimText
fix(chunker): preserve incoming Mom in applyChildrenDelimText
fix(chunker): preserve incoming Mom in applyChildrenDelimText
fix(chunker): add leading-newline + symmetry tests; preserve TrimPref…
fix(chunker): revert Mom-preservation + drop dead tests (infiniflow#1…
test(chunker): replace REMOVED stubs with negative test for current M…
test(chunker): pin current applyChildrenDelimText behavior (#17876 pa…