Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

test(query-understand): pin fallback for unparsable LLM output

MidoriKuraged763816ebf5b
diff --git a/internal/application/service/chat_pipeline/query_understand_test.go b/internal/application/service/chat_pipeline/query_understand_test.go
index e63a0a427..ac2382f87 100644
--- a/internal/application/service/chat_pipeline/query_understand_test.go
+++ b/internal/application/service/chat_pipeline/query_understand_test.go
@@ -85,3 +85,67 @@ func TestApplyIntentPromptOverride_GlobalOnly(t *testing.T) {
t.Errorf("override: got %q, want %q", cm.SystemPromptOverride, "hi there")
}
}
+
+// TestParseOutput_UnparsableFallsBackToOriginalQuery pins the degradation
+// contract for query understanding: when the LLM returns output that cannot be
+// parsed as the structured {"rewrite_query","intent",...} JSON, RewriteQuery
+// must remain the original user query (which OnEvent sets before calling
+// parseOutput) instead of leaking the raw model text into the downstream
+// retrieval query.
+func TestParseOutput_UnparsableFallsBackToOriginalQuery(t *testing.T) {
+ p := &PluginQueryUnderstand{}
+ cm := &types.ChatManage{
+ PipelineState: types.PipelineState{
+ RewriteQuery: "original user query",
+ Intent: types.IntentKBSearch,
+ },
+ }
+
+ p.parseOutput(cm, "The answer is: check the admin console")
+
+ if cm.RewriteQuery != "original user query" {
+ t.Fatalf("RewriteQuery = %q, want original user query", cm.RewriteQuery)
+ }
+ if cm.Intent != types.IntentKBSearch {
+ t.Errorf("Intent = %q, want kb_search", cm.Intent)
+ }
+}
+
+// TestParseOutput_UnparsableBlankDoesNotRewrite verifies that empty LLM output
+// also leaves the original query untouched.
+func TestParseOutput_UnparsableBlankDoesNotRewrite(t *testing.T) {
+ p := &PluginQueryUnderstand{}
+ cm := &types.ChatManage{
+ PipelineState: types.PipelineState{
+ RewriteQuery: "original user query",
+ Intent: types.IntentKBSearch,
+ },
+ }
+
+ p.parseOutput(cm, " \n\t ")
+
+ if cm.RewriteQuery != "original user query" {
+ t.Fatalf("RewriteQuery = %q, want original user query", cm.RewriteQuery)
+ }
+}
+
+// TestParseOutput_ValidJSONStillAppliesRewrite guards the happy path: a
+// well-formed structured output still overrides RewriteQuery and Intent.
+func TestParseOutput_ValidJSONStillAppliesRewrite(t *testing.T) {
+ p := &PluginQueryUnderstand{}
+ cm := &types.ChatManage{
+ PipelineState: types.PipelineState{
+ RewriteQuery: "original user query",
+ Intent: types.IntentKBSearch,
+ },
+ }
+
+ p.parseOutput(cm, `{"rewrite_query":"rewritten query","intent":"summarize"}`)
+
+ if cm.RewriteQuery != "rewritten query" {
+ t.Fatalf("RewriteQuery = %q, want rewritten query", cm.RewriteQuery)
+ }
+ if cm.Intent != types.IntentSummarize {
+ t.Errorf("Intent = %q, want summarize", cm.Intent)
+ }
+}