fix(knowledge): propagate chunking strategy to parent-child splitter configs
buildParentChildConfigs only copied ChunkSize/ChunkOverlap/Separators into the parent and child SplitterConfigs, dropping base.Strategy. Since an empty Strategy resolves to the legacy tier (resolveChainWithProfile treats "" as legacy for backwards compatibility), parent-child chunking always ran the legacy recursive splitter regardless of the configured strategy: no heading alignment and no ContextHeader breadcrumbs in child embeddings, silently. The flat path (buildSplitterConfigFromChunking) already propagates Strategy; parent-child predates the strategy system (b3334990, 2026-03) and was not updated when the 3-tier strategy landed (ae6fde23, 2026-05). SplitParentChild's docstring ("Child splitting honours childCfg.Strategy") and mergeBreadcrumbs both assume the strategy reaches the parent-child path. Copy base.Strategy into both configs and add a regression test.
This commit is contained in:
@@ -226,7 +226,11 @@ func buildSplitterConfigFromChunking(cc types.ChunkingConfig) chunker.SplitterCo
|
||||
}
|
||||
|
||||
// buildParentChildConfigs derives parent and child SplitterConfig from ChunkingConfig.
|
||||
// The base config (already validated with defaults) is used for separators.
|
||||
// The base config (already validated with defaults) is used for separators and
|
||||
// the splitting strategy. Strategy must be propagated: an empty Strategy resolves
|
||||
// to the legacy tier (see resolveChainWithProfile), which never runs the heading
|
||||
// splitter, so parent-child chunks would silently lose heading alignment and
|
||||
// ContextHeader breadcrumbs regardless of the configured strategy.
|
||||
func buildParentChildConfigs(cc types.ChunkingConfig, base chunker.SplitterConfig) (parent, child chunker.SplitterConfig) {
|
||||
parentSize := cc.ParentChunkSize
|
||||
if parentSize <= 0 {
|
||||
@@ -240,11 +244,13 @@ func buildParentChildConfigs(cc types.ChunkingConfig, base chunker.SplitterConfi
|
||||
ChunkSize: parentSize,
|
||||
ChunkOverlap: base.ChunkOverlap, // reuse configured overlap for parents
|
||||
Separators: base.Separators,
|
||||
Strategy: base.Strategy,
|
||||
}
|
||||
child = chunker.SplitterConfig{
|
||||
ChunkSize: childSize,
|
||||
ChunkOverlap: childSize / 5, // ~20% overlap for child chunks
|
||||
Separators: base.Separators,
|
||||
Strategy: base.Strategy,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"testing"
|
||||
|
||||
werrors "github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/infrastructure/chunker"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -348,3 +349,32 @@ func TestMergeParserEngineOverrides(t *testing.T) {
|
||||
"k3": "v3",
|
||||
}, merged)
|
||||
}
|
||||
|
||||
func TestBuildParentChildConfigs_PropagatesStrategy(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
base := chunker.SplitterConfig{
|
||||
ChunkSize: 1000,
|
||||
ChunkOverlap: 100,
|
||||
Separators: []string{"\n\n", "\n"},
|
||||
Strategy: chunker.StrategyAuto,
|
||||
}
|
||||
cc := types.ChunkingConfig{
|
||||
EnableParentChild: true,
|
||||
ParentChunkSize: 4096,
|
||||
ChildChunkSize: 512,
|
||||
}
|
||||
|
||||
parent, child := buildParentChildConfigs(cc, base)
|
||||
|
||||
require.Equal(t, chunker.StrategyAuto, parent.Strategy,
|
||||
"parent splitting must honour the configured strategy; empty resolves to the legacy tier")
|
||||
require.Equal(t, chunker.StrategyAuto, child.Strategy,
|
||||
"child splitting must honour the configured strategy; empty resolves to the legacy tier")
|
||||
require.Equal(t, 4096, parent.ChunkSize)
|
||||
require.Equal(t, 512, child.ChunkSize)
|
||||
require.Equal(t, base.ChunkOverlap, parent.ChunkOverlap)
|
||||
require.Equal(t, 512/5, child.ChunkOverlap)
|
||||
require.Equal(t, base.Separators, parent.Separators)
|
||||
require.Equal(t, base.Separators, child.Separators)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user