From f7ec759cc6cb91864adfdea9f9551f9c60929d06 Mon Sep 17 00:00:00 2001 From: darcy Date: Tue, 14 Jul 2026 11:44:42 +0800 Subject: [PATCH] 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. --- .../application/service/knowledge_process.go | 8 ++++- .../service/knowledge_process_config_test.go | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/internal/application/service/knowledge_process.go b/internal/application/service/knowledge_process.go index ccbc192a..b109b9af 100644 --- a/internal/application/service/knowledge_process.go +++ b/internal/application/service/knowledge_process.go @@ -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 } diff --git a/internal/application/service/knowledge_process_config_test.go b/internal/application/service/knowledge_process_config_test.go index 7645f415..ae90c578 100644 --- a/internal/application/service/knowledge_process_config_test.go +++ b/internal/application/service/knowledge_process_config_test.go @@ -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) +}