RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(go): align retrieval and chat pipeline with Python (#19934)
qinling021047a444039790
diff --git a/cmd/ragflow_server.go b/cmd/ragflow_server.goindex 1af2f2eb3f..a2c8679ce2 100644--- a/cmd/ragflow_server.go+++ b/cmd/ragflow_server.go@@ -1206,33 +1206,24 @@ func startServer(ctx context.Context, args *serverArgs) error {// "gpt-4o"), i.e. Python chat_mdl.llm_name. It is the gen_json reply-cache// key's model component; empty disables that cache.resolvedModelName := ""- if req.ModelID != "" {- if target, mErr := modelSolver.ResolveModelConfig(ctx, req.TenantID, entity.ModelTypeChat, req.ModelID); mErr == nil {- if inv := component.NewResolvedInvoker(target.Driver, target.ModelName, target.APIConfig); inv != nil {- // MaxLength mirrors Python LLMBundle.max_length (the model's- // context window in tokens); message-fitting nodes (calculate,- // structure_qa) use it as their chat.FitMessages budget.- model = &agenticruntime.InvokerSessionModel{Invoker: inv, DB: dao.DB, MaxLength: target.ContextLength}- resolvedModelName = target.ModelName- // Reuse the same resolved driver/name/api as the invoker so- // the outer loop and the inner tool calls share one model.- outerModel = modelModule.NewChatModel(target.Driver, &target.ModelName, target.APIConfig)- }- } else {- common.Warn("runtime: failed to resolve chat model for reasoning; runtime will degrade to direct search", zap.Error(mErr))- }- }-- // OuterSupportsTools mirrors Python `if not chat_mdl.is_tools`: gate the- // outer react loop on the model's tool-calling capability, not mere- // existence of an outer model. A model that can't emit tool_calls must fall- // back to the direct graph (Python async_chat) rather than bind tools and- // return a retrieval-less direct answer.- outerSupportsTools := false- if req.ModelID != "" {- if ts, tsErr := modelProviderService.ResolveModelToolSupport(ctx, req.TenantID, entity.ModelTypeChat, req.ModelID); tsErr == nil {- outerSupportsTools = ts+ // The resolution goes through the same entry point the pipeline's own+ // capability probe uses, so the model decided there is the model wired+ // here: req.ModelID may be empty (a dialog without an llm_id), and both+ // layers must then fall back to the tenant default rather than resolving+ // nothing here and judging capability there.+ if target, mErr := modelProviderService.ResolveChatModelTarget(ctx, req.TenantID, req.ModelID); mErr == nil {+ if inv := component.NewResolvedInvoker(target.Driver, target.ModelName, target.APIConfig); inv != nil {+ // MaxLength mirrors Python LLMBundle.max_length (the model's+ // context window in tokens); message-fitting nodes (calculate,+ // structure_qa) use it as their chat.FitMessages budget.+ model = &agenticruntime.InvokerSessionModel{Invoker: inv, DB: dao.DB, MaxLength: target.ContextLength}+ resolvedModelName = target.ModelName+ // Reuse the same resolved driver/name/api as the invoker so+ // the outer loop and the inner tool calls share one model.+ outerModel = modelModule.NewChatModel(target.Driver, &target.ModelName, target.APIConfig)}+ } else {+ common.Warn("runtime: failed to resolve chat model for reasoning; runtime will degrade to direct search", zap.Error(mErr))}// Load the KB objects (mirroring Python RAGTools' self.kbs via@@ -1270,9 +1261,6 @@ func startServer(ctx context.Context, args *serverArgs) error {// first hop; resolveEffectiveQuestion (agentic_rag.py:865) prefers// this original over that rewrite when both describe the same turn.OriginalQuestion: req.Question,- // OuterSupportsTools gates the outer react loop on tool capability- // (Python is_tools); false → fall back to direct RunAgenticRAG.- OuterSupportsTools: outerSupportsTools,// DocScope mirrors Python RAGTools(doc_scope=...): the narrowed doc_ids// (chat-level doc_ids + meta_data_filter) restrict every agentic// retrieval to the user-selected documents instead of the whole kb.diff --git a/internal/common/http.go b/internal/common/http.goindex b5ddd3cb66..74d7f56000 100644--- a/internal/common/http.go+++ b/internal/common/http.go@@ -32,6 +32,7 @@ import ("time""github.com/gin-gonic/gin"+ "go.uber.org/zap")const (@@ -88,51 +89,104 @@ func GetSchemeSafeHTTPClient() *http.Client {return schemeSafeHttpClient}+// providerStreamLogThreshold / providerCallLogThreshold are the durations past+// which a provider call's timings are reported without LLM_DEBUG. A streaming+// answer is reported earlier because it is the one the user waits on.+const (+ providerStreamLogThreshold = 5 * time.Second+ providerCallLogThreshold = 30 * time.Second+)+func newProviderLoggingTransport(base http.RoundTripper) http.RoundTripper {if base == nil {base = http.DefaultTransport}- if !IsLLMDebugEnabled() {- return base- }- return &providerLoggingTransport{base: base, now: time.Now}+ // Always installed: LLM_DEBUG decides whether payloads are logged, not whether+ // timings are collected — first-token only exists while the call is in flight.+ return &providerLoggingTransport{base: base, now: time.Now, debug: IsLLMDebugEnabled()}}type providerLoggingTransport struct {- base http.RoundTripper- now func() time.Time+ base http.RoundTripper+ now func() time.Time+ debug bool}func (t *providerLoggingTransport) RoundTrip(req *http.Request) (*http.Response, error) {- payload, err := readAndRestoreRequestBody(req)- if err != nil {- return nil, err- }providerURL := redactProviderURL(req.URL)- logPayload := redactProviderBody(payload)+ logPayload := ""+ if t.debug {+ payload, err := readAndRestoreRequestBody(req)+ if err != nil {+ return nil, err+ }+ logPayload = redactProviderBody(payload)+ }startedAt := t.now()resp, err := t.base.RoundTrip(req)if err != nil {- logProviderCall(providerURL, logPayload, 0, "", t.now().Sub(startedAt), 0, err)+ if t.debug {+ logProviderCall(providerURL, logPayload, 0, "", t.now().Sub(startedAt), 0, err)+ }return nil, err}if resp.Body == nil {- logProviderCall(providerURL, logPayload, resp.StatusCode, "", t.now().Sub(startedAt), 0, nil)+ if t.debug {+ logProviderCall(providerURL, logPayload, resp.StatusCode, "", t.now().Sub(startedAt), 0, nil)+ }return resp, nil}+ summary := providerCallTiming{+ url: providerURL,+ status: resp.StatusCode,+ streaming: strings.Contains(resp.Header.Get("Content-Type"), "event-stream"),+ }resp.Body = &providerResponseBody{ReadCloser: resp.Body,startedAt: startedAt,now: t.now,+ capture: t.debug,log: func(body []byte, took, firstToken time.Duration) {- logProviderCall(providerURL, logPayload, resp.StatusCode, redactProviderBody(body), took, firstToken, nil)+ if t.debug {+ logProviderCall(providerURL, logPayload, resp.StatusCode, redactProviderBody(body), took, firstToken, nil)+ return+ }+ summary.report(took, firstToken)},}return resp, nil}+// providerCallTiming describes one provider call worth reporting.+type providerCallTiming struct {+ url string+ status int+ streaming bool+}++// report logs the call once it is slow enough to be worth explaining. firstToken+// is queueing, connection setup and prefill; the rest is generation — or a+// consumer stalling the stream, which shows as a large took next to a small+// firstToken.+func (p providerCallTiming) report(took, firstToken time.Duration) {+ limit := providerCallLogThreshold+ if p.streaming {+ limit = providerStreamLogThreshold+ }+ if took < limit {+ return+ }+ Info("Provider call",+ zap.String("url", p.url),+ zap.Int("status", p.status),+ zap.Bool("streaming", p.streaming),+ zap.Duration("took", took),+ zap.Duration("firstToken", firstToken),+ zap.Duration("afterFirstToken", took-firstToken))+}+// providerResponseBody captures bytes while callers consume them, preserving// streaming delivery instead of eagerly reading the entire provider response.type providerResponseBody struct {@@ -143,6 +197,7 @@ type providerResponseBody struct {firstToken time.DurationfirstOnce sync.OncelogOnce sync.Once+ capture boollog func([]byte, time.Duration, time.Duration)}@@ -152,7 +207,9 @@ func (b *providerResponseBody) Read(p []byte) (int, error) {b.firstOnce.Do(func() {b.firstToken = b.now().Sub(b.startedAt)})- _, _ = b.body.Write(p[:n])+ if b.capture {+ _, _ = b.body.Write(p[:n])+ }}if err == io.EOF {b.writeLogOnce()diff --git a/internal/common/http_test.go b/internal/common/http_test.goindex 142539e669..275ed71fb6 100644--- a/internal/common/http_test.go+++ b/internal/common/http_test.go@@ -88,6 +88,7 @@ func TestProviderLoggingRecordsTimingsAndTruncatesVectors(t *testing.T) {}timeIndex := 0transport := &providerLoggingTransport{+ debug: true,base: roundTripFunc(func(req *http.Request) (*http.Response, error) {return &http.Response{StatusCode: http.StatusOK,@@ -173,8 +174,10 @@ func TestProviderLoggingDisabledAvoidsPayloadWork(t *testing.T) {if err != nil {t.Fatalf("Do() error = %v", err)}- if resp.Body != http.NoBody {- t.Fatalf("response body was wrapped while LLM_DEBUG is disabled")+ // The body is still wrapped so the call can be timed, but nothing is+ // captured and nothing is logged while LLM_DEBUG is disabled.+ if wrapped, ok := resp.Body.(*providerResponseBody); ok && wrapped.capture {+ t.Fatalf("response body was captured while LLM_DEBUG is disabled")}_ = resp.Body.Close()@@ -185,3 +188,78 @@ func TestProviderLoggingDisabledAvoidsPayloadWork(t *testing.T) {t.Fatalf("provider log count = %d, want 0", count)}}++// TestProviderSlowStreamReportsTimingsWithoutDebug covers the always-on timing+// line: a slow streaming call reports took/firstToken even with LLM_DEBUG off,+// and never carries payloads.+func TestProviderSlowStreamReportsTimingsWithoutDebug(t *testing.T) {+ t.Setenv(EnvLLMDebug, "")+ core, logs := observer.New(zapcore.InfoLevel)+ previousLogger := Logger+ Logger = zap.New(core)+ t.Cleanup(func() {+ Logger = previousLogger+ })++ startedAt := time.Unix(100, 0)+ times := []time.Time{+ startedAt,+ startedAt.Add(time.Second),+ startedAt.Add(7 * time.Second),+ }+ timeIndex := 0+ transport := &providerLoggingTransport{+ base: roundTripFunc(func(req *http.Request) (*http.Response, error) {+ header := make(http.Header)+ header.Set("Content-Type", "text/event-stream")+ return &http.Response{+ StatusCode: http.StatusOK,+ Header: header,+ Body: io.NopCloser(strings.NewReader("data: {\"choices\":[]}\n\n")),+ Request: req,+ }, nil+ }),+ now: func() time.Time {+ current := times[timeIndex]+ timeIndex+++ return current+ },+ }++ req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "https://provider.example/v1/chat/completions", nil)+ if err != nil {+ t.Fatalf("NewRequestWithContext() error = %v", err)+ }+ resp, err := transport.RoundTrip(req)+ if err != nil {+ t.Fatalf("RoundTrip() error = %v", err)+ }+ if _, err = io.ReadAll(resp.Body); err != nil {+ t.Fatalf("ReadAll() error = %v", err)+ }+ _ = resp.Body.Close()++ entries := logs.All()+ if len(entries) != 1 {+ t.Fatalf("provider log count = %d, want 1", len(entries))+ }+ fields := entries[0].ContextMap()+ durationField := func(key string) (time.Duration, bool) {+ switch v := fields[key].(type) {+ case time.Duration:+ return v, true+ case int64:+ return time.Duration(v), true+ }+ return 0, false+ }+ if took, ok := durationField("took"); !ok || took != 7*time.Second {+ t.Errorf("took = %v, want 7s (fields %v)", fields["took"], fields)+ }+ if firstToken, ok := durationField("firstToken"); !ok || firstToken != time.Second {+ t.Errorf("firstToken = %v, want 1s (fields %v)", fields["firstToken"], fields)+ }+ if _, ok := fields["payload"]; ok {+ t.Errorf("timing log must not carry payloads while LLM_DEBUG is disabled: %v", fields)+ }+}diff --git a/internal/engine/elasticsearch/chunk.go b/internal/engine/elasticsearch/chunk.goindex cff83c9f2a..683ea1a10b 100644--- a/internal/engine/elasticsearch/chunk.go+++ b/internal/engine/elasticsearch/chunk.go@@ -1257,6 +1257,10 @@ func (e *Engine) Search(ctx context.Context, req *types.SearchRequest) (*types.S// knn leg's w; a text-only query has no such pairing, so the// boolean filter wrapper must not apply the constant boost —// otherwise every BM25 score is silently multiplied by 0.5.+ //+ // Logged because it is the boost that orders the first-stage+ // result set, and so decides which candidates survive the+ // caller's size cap.if hasVectorMatch {boolMap["boost"] = 1.0 - vectorSimilarityWeight}@@ -1419,6 +1423,11 @@ func (e *Engine) Search(ctx context.Context, req *types.SearchRequest) (*types.Sreturn nil, fmt.Errorf("error encoding query: %w", err)}+ // Two legs (text + knn) decide the result set; when the window comes back short+ // the body is logged below, to tell "the index has less" from "the request+ // asked for less".+ hybrid := hasTextMatch && hasVectorMatch+// Execute search. When useSearchAfter is true we must NOT send// from/size (we dropped them above) and instead walk the result set// page-by-page with the search_after cursor — ES otherwise returns@@ -1447,6 +1456,20 @@ func (e *Engine) Search(ctx context.Context, req *types.SearchRequest) (*types.Scommon.Warn("Elasticsearch query failed", zap.String("index", indexName), zap.Error(esErr))continue}+ if hybrid {+ // A window the backend could not fill is a candidate shortfall+ // upstream; only then is the request body worth its size.+ common.InfoCtx(ctx, "Elasticsearch hybrid response",+ zap.String("index", indexName),+ zap.Int64("total", indexTotal),+ zap.Int("returned", len(searchChunks)),+ zap.Int("window", limit))+ if limit > 0 && len(searchChunks) < limit {+ common.InfoCtx(ctx, "Elasticsearch hybrid body (window not filled)",+ zap.Strings("indexes", req.IndexNames),+ zap.String("body", elideQueryVector(payload)))+ }+ }totalHits += indexTotalallResults = append(allResults, searchChunks...)}@@ -1458,10 +1481,9 @@ func (e *Engine) Search(ctx context.Context, req *types.SearchRequest) (*types.S// Post-processing: Sort results by scoreif len(allResults) > 0 && (matchText != nil || hasVectorMatch) {+ // "_score" is the ES hit score. "SCORE" is Infinity's column and reads+ // back empty here, which flattened every hybrid chunk's score to 0.scoreColumn := "_score"- if matchText != nil && hasVectorMatch {- scoreColumn = "SCORE"- }pagerankField := common.PAGERANK_FLDif isSkillIndex {@@ -2121,7 +2143,7 @@ func buildQueryStringQuery(matchText *types.MatchTextExpr, isSkillIndex, isMemor"query_string": map[string]interface{}{"fields": fields,"type": "best_fields",- "query": strings.ToLower(matchText.MatchingText),+ "query": lowerCaseQueryText(matchText.MatchingText),"minimum_should_match": minimumShouldMatch,"boost": boost,},@@ -3075,6 +3097,75 @@ func getDefaultSkillMapping() map[string]interface{} {}// convertESResponse converts ES SearchResponse to unified chunks format+// queryStringOperators holds the tokens Lucene's query_string parser only+// recognises as operators in upper case.+var queryStringOperators = map[string]bool{"AND": true, "OR": true, "NOT": true}++// lowerCaseQueryText folds the terms of a query_string expression but keeps its+// upper-case boolean operators. The *_tks/*_ltks fields are whitespace-analyzed+// and so case-sensitive, while Lucene only reads AND/OR/NOT in upper case:+//+// "病毒 OR 勒索" -> content_ltks:病毒 content_ltks:勒索+// "病毒 or 勒索" -> content_ltks:病毒 content_ltks:or content_ltks:勒索+//+// That folded "or" is an extra clause every minimum_should_match percentage is+// re-based on — a 94-hit search came back with 32. Quoted text is a phrase, never+// an operator, so it is folded too.+func lowerCaseQueryText(text string) string {+ var b strings.Builder+ b.Grow(len(text))+ inQuote := false+ escaped := false+ for i := 0; i < len(text); {+ c := text[i]+ if escaped {+ b.WriteByte(c)+ escaped = false+ i+++ continue+ }+ switch {+ case c == '\\':+ b.WriteByte(c)+ escaped = true+ i+++ case c == '"':+ inQuote = !inQuote+ b.WriteByte(c)+ i+++ case isASCIILetter(c):+ j := i+ for j < len(text) && isASCIILetter(text[j]) {+ j+++ }+ word := text[i:j]+ if !inQuote && queryStringOperators[word] {+ b.WriteString(word)+ } else {+ b.WriteString(strings.ToLower(word))+ }+ i = j+ default:+ b.WriteByte(c)+ i+++ }+ }+ return b.String()+}++func isASCIILetter(c byte) bool {+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')+}++// queryVectorField matches the serialized query vector: 1024 floats that say+// nothing about why a search came back short, so they never reach the log.+var queryVectorField = regexp.MustCompile(`"query_vector":\s*\[[^\]]*\]`)++// elideQueryVector drops that vector from a body before it is logged.+func elideQueryVector(body []byte) string {+ return queryVectorField.ReplaceAllString(string(body), `"query_vector":"<elided>"`)+}+func convertESResponse(esResp *SearchResponse, vectorFieldName string) []map[string]interface{} {if esResp == nil || esResp.Hits.Hits == nil {return []map[string]interface{}{}@@ -3193,8 +3284,9 @@ func sortByScore(chunks []map[string]interface{}, limit int) []map[string]interfreturn chunks}- // Sort by _score descending- sort.Slice(chunks, func(i, j int) bool {+ // Stable: the reference does not re-sort at all, so ties keep the order ES+ // returned them in.+ sort.SliceStable(chunks, func(i, j int) bool {scoreI := getChunkScore(chunks[i])scoreJ := getChunkScore(chunks[j])return scoreI > scoreJdiff --git a/internal/engine/elasticsearch/chunk_helpers_test.go b/internal/engine/elasticsearch/chunk_helpers_test.goindex a69a475860..d28e630866 100644--- a/internal/engine/elasticsearch/chunk_helpers_test.go+++ b/internal/engine/elasticsearch/chunk_helpers_test.go@@ -56,6 +56,22 @@ func TestBuildQueryStringQueryLowercasesMatchingText(t *testing.T) {"isabel wood co-lead ross feldner bob musil bird watch wonder program")}+func TestBuildQueryStringQueryKeepsBooleanOperators(t *testing.T) {+ // Only AND/OR/NOT are operators, and only in upper case: a folded "or" is a+ // term clause, which re-bases minimum_should_match and turns a 94-hit search+ // into a 32-hit one (see lowerCaseQueryText).+ query := buildQueryStringQuery(&types.MatchTextExpr{+ MatchingText: `(病毒 OR 勒索)^0.3 (系统)^0.2 ("系统 因为 勒索"~2)^1.5 OR ("MES"^0.7)`,+ }, false, false)++ queryString, ok := query["query_string"].(map[string]interface{})+ if !ok {+ t.Fatalf("query_string missing from %#v", query)+ }+ assertEqual(t, queryString["query"],+ `(病毒 OR 勒索)^0.3 (系统)^0.2 ("系统 因为 勒索"~2)^1.5 OR ("mes"^0.7)`)+}+func TestSearchUsesConfiguredKNNNumCandidates(t *testing.T) {if err := common.InitLogger("info", common.FileOutput{}, "elasticsearch_test"); err != nil {t.Fatalf("init logger: %v", err)diff --git a/internal/rag/agentic-rag/agentic_rag.go b/internal/rag/agentic-rag/agentic_rag.goindex f3acc73471..4bfe9116d8 100644--- a/internal/rag/agentic-rag/agentic_rag.go+++ b/internal/rag/agentic-rag/agentic_rag.go@@ -194,11 +194,6 @@ type RAGTools struct {// When nil, Rag falls back to the direct RunAgenticRAG path, so callers that don't wire// an outer model see unchanged behaviour.Outer *models.ChatModel- // OuterSupportsTools gates the outer react loop: when the outer model exists but- // cannot emit tool calls, the loop must be skipped (it would otherwise bind tools and,- // producing no tool_call, return a retrieval-less direct answer). Callers set it from- // ModelProviderService.ResolveModelToolSupport.- OuterSupportsTools bool// Cache answers near-identical re-asks from an earlier answer. Rag builds a fresh// per-turn RAGCache when this is nil, so caching is ON by default. To share one cache// across multiple Rag() calls within a single turn (the concurrent tool_call case),@@ -1219,7 +1214,7 @@ func Rag(ctx context.Context, deps RAGTools, req runtime.RunRequest) *RunResponsdeps.Cache = NewRAGCache()}- if deps.Outer != nil && deps.OuterSupportsTools {+ if deps.Outer != nil {if deps.AnswerSink != nil {return runOuterReactStream(ctx, deps, req, logger)}@@ -1289,17 +1284,17 @@ func Rag(ctx context.Context, deps RAGTools, req runtime.RunRequest) *RunResponsdeps.Finalize = compose// Say why this run goes straight to the research graph — DEVELOPER LOG ONLY.- // The decision is recorded where it is taken (the outer branch above did not- // fire, and no cache hit short-circuited), because a capability probe that fails- // closed is otherwise invisible: nothing in the run says whether the outer model- // was absent or merely reported no tool support.+ // Only one reason remains: no outer model was wired (the outer branch above did+ // not fire, and no cache hit short-circuited). Whether the request's chat model+ // can call tools at all is decided before this call, by the caller that picks+ // between the agentic path and the regular chat.//// It stays out of the think block on purpose: it describes the runtime' wiring,// not the question or the research, and a reader gets one of these on every run// in a deployment without an outer loop. The reader-visible trace simply has no// "[Tool loop]" section in that configuration, which is the honest shape — the// section exists when a loop ran.- logger.Printf("[Agentic RAG] %s", outerLoopAbsentLine(deps))+ logger.Printf("[Agentic RAG] No outer model is wired, so this run has no outer tool loop.")RunAgenticRAG(ctx, deps, req, searchDeps, kb, resp, logger, spec)@@ -1907,22 +1902,6 @@ func outerLoopEndLine(ragCalls int, answered bool) string {}}-// outerLoopAbsentLine says why a run has NO "[Tool loop]" section — which is the-// normal shape of a deployment that never wires an outer model, and the shape of a-// tool-incapable one.-//-// It is reported to the DEVELOPER LOG only (see the call site in Rag). The two cases-// are kept apart because they are different situations: a model that merely failed-// to report its tool capability looks identical to one that has none (the probe-// fails closed in cmd/ragflow_server.go), so this line is what tells an operator-// which of the two they are looking at.-func outerLoopAbsentLine(deps RAGTools) string {- if deps.Outer == nil {- return "No outer model is wired, so this run has no outer tool loop."- }- return "The outer model cannot call tools, so this run has no outer tool loop."-}-// outerReactSession implements models.ToolCallSession for the outer react loop.type outerReactSession struct {ctx context.Contextdiff --git a/internal/rag/agentic-rag/agentic_rag_test.go b/internal/rag/agentic-rag/agentic_rag_test.goindex b4a4ab8a18..4dda3c9812 100644--- a/internal/rag/agentic-rag/agentic_rag_test.go+++ b/internal/rag/agentic-rag/agentic_rag_test.go@@ -29,7 +29,6 @@ import ("unicode/utf8""github.com/cloudwego/eino/schema"- "ragflow/internal/entity/models""ragflow/internal/rag/agentic-rag/runtime")@@ -717,10 +716,6 @@ func TestOuterLoopEndLine(t *testing.T) {// the question or the research, and a deployment without an outer loop would print// it on every single run — while the reader-visible trace is simply and honestly// missing its "[Tool loop]" section.-//-// It also pins that the two reasons stay apart: no outer model wired vs a wired one-// that cannot emit tool calls (the capability probe fails closed, so an operator-// needs the log to tell a failed probe from a deployment that has no outer model).func TestOuterLoopAbsentLineStaysOutOfTheThinkBlock(t *testing.T) {var think, logged strings.Builderctx := runtime.WithSteps(context.Background(), runtime.StepReporter{@@ -739,11 +734,6 @@ func TestOuterLoopAbsentLineStaysOutOfTheThinkBlock(t *testing.T) {if strings.Contains(think.String(), "outer tool loop") {t.Errorf("the wiring note must not reach the think block; got:\n%s", think.String())}-- if got := outerLoopAbsentLine(RAGTools{Outer: &models.ChatModel{}}); got !=- "The outer model cannot call tools, so this run has no outer tool loop." {- t.Errorf("outerLoopAbsentLine (tool-incapable model) = %q", got)- }}// TestRagToolResultLine pins the outer `rag` result wording: it reports what thediff --git a/internal/rag/agentic-rag/coverage_step.go b/internal/rag/agentic-rag/coverage_step.goindex b548668829..4f7b06c4c5 100644--- a/internal/rag/agentic-rag/coverage_step.go+++ b/internal/rag/agentic-rag/coverage_step.go@@ -87,8 +87,12 @@ func RunCoverageResolve(ctx context.Context, deps RAGTools, st *AgenticState, lo}// Bounded like every other single-purpose call in this graph: the answer composition- // still has to fit inside the request's clock, so this step may not spend it.- t := nodeClock(CoverageResolveTimeoutS, 10.0, st.RemainingS()-10.0)+ // still has to fit inside the request's clock, so this step may not spend it — and+ // unlike the research nodes it does not claim its floor once that room is gone.+ t := 0.0+ if room := st.RemainingS() - 10.0; room > 0 {+ t = nodeClock(CoverageResolveTimeoutS, 10.0, room)+ }callCtx, cancel := context.WithTimeout(ctx, time.Duration(t*float64(time.Second)))defer cancel()diff --git a/internal/rag/agentic-rag/policy.go b/internal/rag/agentic-rag/policy.goindex c53825c653..b99881ce19 100644--- a/internal/rag/agentic-rag/policy.go+++ b/internal/rag/agentic-rag/policy.go@@ -38,17 +38,14 @@ package agentic_rag// floorS, and capped by the room the QUESTION still has (roomS is what is left after the// reserve the callers below it need).//-// The FLOOR WINS over a room that is merely small: with room 2s and a floor of 10s the-// call gets 10s, because a node that cannot finish inside its floor is not worth starting-// at all. A caller that needs the room protected therefore passes a smaller floor, or a-// roomS that already has the reserve subtracted.-//-// A room already GONE (roomS <= 0) yields zero, which every caller treats as "do not-// start" rather than "start and get cancelled".+// The FLOOR WINS over a room that is merely small OR already spent: with room 2s — or -50s+// — and a floor of 10s the call gets 10s, because a node that cannot finish inside its+// floor is not worth starting at all, and a node that is the only chance to gather a+// candidate pool is worth its floor even on an overrun budget. A caller that needs the room+// protected therefore passes a smaller floor, or a roomS that already has the reserve+// subtracted; a step that must not run at all on a spent budget says so at its own call+// site (see RunCoverageResolve).func nodeClock(budgetS, floorS, roomS float64) float64 {- if roomS <= 0 {- return 0- }return min(budgetS, max(floorS, roomS))}diff --git a/internal/rag/agentic-rag/policy_test.go b/internal/rag/agentic-rag/policy_test.gonew file mode 100644index 0000000000..8c4c0815e4--- /dev/null+++ b/internal/rag/agentic-rag/policy_test.go@@ -0,0 +1,60 @@+//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.+//++package agentic_rag++import "testing"++// TestNodeClockKeepsItsFloorOnASpentBudget pins the property that decides whether a run+// whose research budget is already gone still retrieves anything: the clock is+// min(budget, max(floor, room)), with NO "room gone -> zero" branch.+//+// Measured 2026-09-20 (glm-4.6, a single-question MES ransomware run): the fan-out plus the+// slot-table decomposition spent 185.4s of the 180s budget, so the prefetch arrived with+// room == RemainingS() - MinRoundHeadroomS == -50. Returning zero there built a+// context.WithTimeout(ctx, 0); FanoutSearch's first act is a ctx.Done() check, so it+// admitted nothing and logged "Added 0 new passages" without running a single search. The+// run then reached the answer with an empty pool — the draft had nothing to summarise, the+// SCA had nothing to judge, and the reply was the no-evidence hedge — while the searches it+// never ran took 25ms each on the same corpus.+func TestNodeClockKeepsItsFloorOnASpentBudget(t *testing.T) {+ cases := []struct {+ name string+ budgetS, floorS, roomS float64+ want float64+ }{+ // The prefetch: its own 90s cap, floored at 10s.+ {"prefetch with the budget spent", PrefetchTimeoutS, 10.0, -50.0, 10.0},+ {"prefetch with no room at all", PrefetchTimeoutS, 10.0, 0, 10.0},+ {"prefetch with less room than its floor", PrefetchTimeoutS, 10.0, 4.0, 10.0},+ {"prefetch with room left", PrefetchTimeoutS, 10.0, 40.0, 40.0},+ {"prefetch capped by its own budget", PrefetchTimeoutS, 10.0, 600.0, 90.0},+ // The draft and the SCA review: 60s each, floored at 15s.+ {"draft with the budget spent", DraftTimeoutS, 15.0, -10.0, 15.0},+ {"sca with the budget spent", SCATimeoutS, 15.0, -10.0, 15.0},+ // The gap -> query rewrite call: 45s, floored at 10s.+ {"rewrite with the budget spent", RewriteTimeoutS, 10.0, -10.0, 10.0},+ // The slot research pass has no floor of its own — its caller floors it at 20s, and+ // only reaches it with MinRoundHeadroomS still on the clock.+ {"research pass with the budget spent", PassTimeoutS, 0, -25.0, 0},+ }+ for _, tc := range cases {+ if got := nodeClock(tc.budgetS, tc.floorS, tc.roomS); got != tc.want {+ t.Errorf("%s: nodeClock(%g, %g, %g) = %g, want %g",+ tc.name, tc.budgetS, tc.floorS, tc.roomS, got, tc.want)+ }+ }+}diff --git a/internal/service/chat_pipeline.go b/internal/service/chat_pipeline.goindex 4894069e26..ce18d33f66 100644--- a/internal/service/chat_pipeline.go+++ b/internal/service/chat_pipeline.go@@ -26,6 +26,7 @@ import ("ragflow/internal/engine""ragflow/internal/entity"modelModule "ragflow/internal/entity/models"+ ragprompts "ragflow/internal/rag/prompts""ragflow/internal/service/file""ragflow/internal/service/graph""ragflow/internal/service/nlp"@@ -34,6 +35,7 @@ import ("strings""sync""time"+ "unicode/utf8""ragflow/internal/dao"@@ -654,8 +656,14 @@ func (s *ChatPipelineService) AsyncChat(// reasoning is an integer level 0..4 (mirrors Python rag_agent): 0 = off// (regular RAG via async_chat), 1..4 = low/medium/high/ultra (harness// agentic). It comes from the request kwargs first, then prompt_config.+ //+ // Python rag_agent also refuses the agentic loop when the model cannot+ // call tools, and routes those requests to async_chat.reasoningLevel := resolveReasoningLevel(kwargs, map[string]interface{}(chat.PromptConfig))- useReasoning := reasoningLevel > 0+ // The chat model's tool capability rides on the config Phase 2 resolved,+ // so the guard costs nothing here and judges the model the request+ // actually runs on.+ useReasoning := reasoningNeedsAgenticGraph(chat, llmModelConfig, reasoningLevel)common.Info("Phase 9: Retrieval",zap.Bool("has_knowledge_param", hasKnowledgeParam),zap.Int("reasoning_level", reasoningLevel),@@ -1052,8 +1060,12 @@ func (s *ChatPipelineService) AsyncChat(}}if systemPrompt != "" {+ // Python logs characters; a Chinese prompt is ~3x longer in bytes, so+ // report both to keep the two comparable.common.Info("System prompt built",- zap.Int("length", len(systemPrompt)))+ zap.Int("length", len(systemPrompt)),+ zap.Int("runes", utf8.RuneCountInString(systemPrompt)),+ zap.Int("knowledgeRunes", utf8.RuneCountInString(knowledge)))}// Build citation prompt if quoting is enabled.@@ -1066,13 +1078,18 @@ func (s *ChatPipelineService) AsyncChat(quote = quote && promptConfigQuote}if len(knowledges) > 0 && quote {- prompt4citation = citationPrompt()+ // Python's citation_prompt() (generator.py:226) renders+ // citation_prompt.md — the full rules with examples, which the agent+ // path already reads from the same embedded copy. The standard path+ // used a ~380-char paraphrase of it.+ prompt4citation = ragprompts.CitationPrompt("")}if prompt4citation != "" {common.Info("Citation prompt built",zap.Bool("quote", quote),- zap.Int("length", len(prompt4citation)))+ zap.Int("length", len(prompt4citation)),+ zap.Int("runes", utf8.RuneCountInString(prompt4citation)))}// Build the message list: system + cleaned user/assistant messages.@@ -1148,7 +1165,21 @@ func (s *ChatPipelineService) AsyncChat(}chatCfg := BuildChatConfig(chat, kwargs)- if adjusted, ok, err := clampChatConfigMaxTokens(chatCfg, modelMaxTokens, usedTokenCount); err != nil {+ // The citation template joins the prompt only when the request is issued+ // (prompt+prompt4citation, below), which is after messageFitIn sized the+ // window — so its tokens are added here by hand. They are deliberately NOT+ // folded into messageFitIn: the reference implementation trims on the same+ // budget (dialog_service async_chat: message_fit_in(msg, max_tokens*0.95),+ // then prompt + prompt4citation), and moving the trim boundary would change+ // which messages get cut. The completion clamp is where the omission bites:+ // it derives the completion budget from the room the prompt leaves, so+ // leaving ~2k tokens of citation instructions out of that sum can hand the+ // provider a prompt plus completion that overruns the context window.+ citationTokens := 0+ if prompt4citation != "" {+ citationTokens = graph.NumTokensFromString(prompt4citation)+ }+ if adjusted, ok, err := clampChatConfigMaxTokens(chatCfg, modelMaxTokens, usedTokenCount+citationTokens); err != nil {out <- AsyncChatResult{Answer: fmt.Sprintf("**ERROR**: %s", err.Error()),Final: true,@@ -1567,7 +1598,7 @@ func (s *ChatPipelineService) AsyncChatSolo(}// 4. Build the chat model wrapper.- target, err := s.resolveChatModelTarget(ctx, chat)+ target, err := s.ModelProviderSvc.ResolveChatModelTarget(ctx, chat.TenantID, chat.LLMID)if err != nil {out <- AsyncChatResult{Answer: fmt.Sprintf("**ERROR**: %s", err.Error()),@@ -2088,22 +2119,21 @@ func (s *ChatPipelineService) getLLMModelConfig(ctx context.Context, chat *entit}// Probe the default model's enrolled types so a vision-capable// default dispatches as image2text (same rule as the explicit-LLM- // branches below).- modelRef := target.ModelID- if modelRef == "" {- modelRef = fmt.Sprintf("%s@%s@%s", target.ModelName, target.InstanceName, target.ProviderName)- }- cfg["model_type"] = s.resolveChatModelType(ctx, chat.TenantID, modelRef)+ // branches below), and carry the tool capability the resolution+ // already computed.+ cfg["model_type"] = s.resolveChatModelType(ctx, chat.TenantID, modelTargetRef(target))+ cfg["is_tools"] = target.SupportsToolsreturn cfg, modelName, factoryName, baseURL, nil}- // Branches 1/2: explicit LLM. Probe model types and pick IMAGE2TEXT- // when the LLM is registered as such, otherwise CHAT.- modelTypeStr := s.resolveChatModelType(ctx, chat.TenantID, chat.LLMID)- modelType := entity.ModelTypeChat- if modelTypeStr == "image2text" {- modelType = entity.ModelTypeImage2Text- }+ // Branches 1/2: explicit LLM. Resolve the enrolled type first — IMAGE2TEXT+ // when the LLM is registered as vision-capable, CHAT otherwise — and let the+ // same resolution report the model's tool capability.+ //+ // This mirrors Python, which resolves chat_mdl once in get_models() and then+ // reads chat_mdl.is_tools off it (dialog_service.py rag_agent): one lookup, and+ // the model that runs is by construction the model that was judged.+ modelType := s.ModelProviderSvc.modelSolver().ResolveChatModelType(ctx, chat.TenantID, chat.LLMID)target, err := s.ModelProviderSvc.modelSolver().ResolveModelConfig(ctx, chat.TenantID, modelType, chat.LLMID)if err != nil {return nil, "", "", "", err@@ -2114,38 +2144,63 @@ func (s *ChatPipelineService) getLLMModelConfig(ctx context.Context, chat *entitif err != nil {return nil, "", "", "", err}- cfg["model_type"] = modelTypeStr+ cfg["model_type"] = chatModelTypeName(modelType)+ cfg["is_tools"] = target.SupportsToolsreturn cfg, modelName, factoryName, baseURL, nil}-func (s *ChatPipelineService) resolveChatModelTarget(ctx context.Context, chat *entity.Chat) (*ModelTarget, error) {- if chat.LLMID == "" {- return s.ModelProviderSvc.modelSolver().ResolveDefaultModelConfig(ctx, chat.TenantID, entity.ModelTypeChat)+// reasoningNeedsAgenticGraph collapses Python rag_agent's two guards: reasoning on+// and a tool-calling chat model. The loop exists so the outer model can call the+// bound rag tool — with a model that never emits a tool_call the whole research+// budget goes to turns that retrieve nothing, so Python routes those requests to+// async_chat, the regular RAG branch below.+func reasoningNeedsAgenticGraph(chat *entity.Chat, cfg map[string]interface{}, reasoningLevel int) bool {+ if chat == nil || reasoningLevel <= 0 {+ return false+ }+ if chatConfigSupportsTools(cfg) {+ return true}+ common.Info("LLM does not support tool calls; falling back to regular RAG chat",+ zap.Int("reasoning_level", reasoningLevel))+ return false+}- modelType := entity.ModelTypeChat- if s.resolveChatModelType(ctx, chat.TenantID, chat.LLMID) == "image2text" {- modelType = entity.ModelTypeImage2Text+// chatConfigSupportsTools mirrors Python's `getattr(chat_mdl, "is_tools", False)`.+// The flag rides on the resolved model config — getLLMModelConfig copies it off+// the resolution (resolvedModel.supportsTools) — so this is a field read, exactly+// like Python's, with no second lookup that could resolve a different model than+// the one the request runs on. A missing flag means the model was never resolved,+// which counts as unsupported, the same as Python's default.+func chatConfigSupportsTools(cfg map[string]interface{}) bool {+ if cfg == nil {+ return false}- return s.ModelProviderSvc.modelSolver().ResolveModelConfig(ctx, chat.TenantID, modelType, chat.LLMID)+ // Read the value the way the persisted flag is read (extraToolSupport): it is+ // written as a JSON boolean but has historically also been spelled as a string.+ switch v := cfg["is_tools"].(type) {+ case bool:+ return v+ case string:+ return strings.EqualFold(strings.TrimSpace(v), "true")+ case float64:+ return v != 0+ }+ return false}-// resolveChatModelType probes the enrolled model types for llmRef and-// returns "image2text" when the model is vision-capable (enrolled with an-// image2text / "vision" type), "chat" otherwise. Probe failures are-// conservative: they yield "chat", which drops image attachments instead-// of risking a provider-side rejection.+// resolveChatModelType renders the enrolled type of llmRef as the config's+// model_type value. Probe failures are conservative: they yield "chat", which+// drops image attachments instead of risking a provider-side rejection.func (s *ChatPipelineService) resolveChatModelType(ctx context.Context, tenantID, llmRef string) string {- modelTypes, err := s.ModelProviderSvc.modelSolver().ResolveModelType(ctx, tenantID, llmRef)- if err != nil {- return "chat"- }- for _, mt := range modelTypes {- // ModelType is a bitmask: a model enrolled as chat+image2text- // reports a combined value, so test membership, not equality.- if mt.Has(entity.ModelTypeImage2Text) {- return "image2text"- }+ return chatModelTypeName(s.ModelProviderSvc.modelSolver().ResolveChatModelType(ctx, tenantID, llmRef))+}++// chatModelTypeName renders a resolved model type as the model_type value+// downstream image-attachment dispatch compares against.+func chatModelTypeName(modelType entity.ModelType) string {+ if modelType.Has(entity.ModelTypeImage2Text) {+ return "image2text"}return "chat"}@@ -2235,7 +2290,7 @@ func (s *ChatPipelineService) getModels(ctx context.Context, chat *entity.Chat)}// Chat model.- target, err := s.resolveChatModelTarget(ctx, chat)+ target, err := s.ModelProviderSvc.ResolveChatModelTarget(ctx, chat.TenantID, chat.LLMID)var chatModel *modelModule.ChatModelif err == nil {chatModel = modelModule.NewChatModel(target.Driver, &target.ModelName, target.APIConfig)@@ -2809,7 +2864,7 @@ func (s *ChatPipelineService) buildChatDriver(ctx context.Context, chat *entity.if chatModel != nil {return chatModel}- target, err := s.resolveChatModelTarget(ctx, chat)+ target, err := s.ModelProviderSvc.ResolveChatModelTarget(ctx, chat.TenantID, chat.LLMID)if err != nil {return nil}@@ -3519,16 +3574,6 @@ func (s *ChatPipelineService) extractVisibleAnswer(text string) string {return ExtractVisibleAnswer(text)}-// citationPrompt returns the citation instruction prompt.-// Mirrors Python's citation_prompt() in rag/prompts/generator.py.-func citationPrompt() string {- return "\n\n### Citation\nWhen answering, please cite sources using the format [ID:N] " +- "after each sentence where the information from that chunk is used, " +- "where N is the id printed at the start of the evidence block (" +- "the blocks are numbered from 0, so the FIRST block is [ID:0]). " +- "Cite each source individually ([ID:0][ID:1]); never merge consecutive citations into a range such as [ID:1-3]."-}-// -----------------------------------------------------------------------// Moved from sql_fallback.go (2026-06-12). SQL retrieval system, repair// helpers, and Python parity helpers. Kept in async_chat.go because thediff --git a/internal/service/chat_pipeline_test.go b/internal/service/chat_pipeline_test.goindex 55748ea758..a7c6f47ff1 100644--- a/internal/service/chat_pipeline_test.go+++ b/internal/service/chat_pipeline_test.go@@ -25,7 +25,11 @@ import ("strings""testing"+ "github.com/glebarez/sqlite"+ "gorm.io/gorm"+"ragflow/internal/common"+ "ragflow/internal/dao""ragflow/internal/engine""ragflow/internal/entity"modelModule "ragflow/internal/entity/models"@@ -2161,3 +2165,263 @@ func TestDecorateHarnessAnswerKeepsPositionsAfterUnknownBlock(t *testing.T) {t.Fatalf("reference doc_aggs = %#v, want d2 (c3)", aggs[0])}}++// setupChatPipelineToolSupportTestDB seeds an in-memory scope whose models differ+// only in their persisted tool-calling flag, so the probe runs without the provider+// catalog:+//+// model-tools-on — chat, extra {"is_tools": true}+// model-tools-off — chat, extra {"is_tools": false}+// model-tools-disabled — chat, extra {"is_tools": true}, status inactive+// model-embedding-tools — embedding, extra {"is_tools": true}+func setupChatPipelineToolSupportTestDB(t *testing.T) *gorm.DB {+ t.Helper()++ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{TranslateError: true})+ if err != nil {+ t.Fatalf("failed to open sqlite: %v", err)+ }+ if err := db.AutoMigrate(+ &entity.Tenant{},+ &entity.UserTenant{},+ &entity.TenantModelProvider{},+ &entity.TenantModelInstance{},+ &entity.TenantModel{},+ ); err != nil {+ t.Fatalf("failed to migrate model tables: %v", err)+ }++ orig := dao.DB+ dao.DB = db+ t.Cleanup(func() { dao.DB = orig })++ activeStatus := "1"+ rows := []interface{}{+ &entity.UserTenant{ID: "user-tenant-1", UserID: "user-1", TenantID: "tenant-1", Role: "owner", InvitedBy: "user-1", Status: &activeStatus},+ &entity.TenantModelProvider{ID: "provider-zhipu", TenantID: "tenant-1", ProviderName: "ZHIPU-AI"},+ &entity.TenantModelInstance{ID: "instance-zhipu", ProviderID: "provider-zhipu", InstanceName: "default", APIKey: "sk-test", Status: "active", Extra: "{}"},+ &entity.TenantModel{ID: "model-tools-on", ProviderID: "provider-zhipu", InstanceID: "instance-zhipu", ModelName: "glm-4-plus", ModelType: int(entity.ModelTypeChat), Status: "active", Extra: `{"is_tools":true}`},+ &entity.TenantModel{ID: "model-tools-off", ProviderID: "provider-zhipu", InstanceID: "instance-zhipu", ModelName: "glm-4-flash", ModelType: int(entity.ModelTypeChat), Status: "active", Extra: `{"is_tools":false}`},+ &entity.TenantModel{ID: "model-tools-disabled", ProviderID: "provider-zhipu", InstanceID: "instance-zhipu", ModelName: "glm-4-air", ModelType: int(entity.ModelTypeChat), Status: "inactive", Extra: `{"is_tools":true}`},+ &entity.TenantModel{ID: "model-embedding-tools", ProviderID: "provider-zhipu", InstanceID: "instance-zhipu", ModelName: "embedding-3", ModelType: int(entity.ModelTypeEmbedding), Status: "active", Extra: `{"is_tools":true}`},+ // Enrolled ONLY as image-to-text: a valid chat-pipeline input, and the case+ // that used to be resolved as chat and rejected.+ &entity.TenantModel{ID: "model-image2text-tools", ProviderID: "provider-zhipu", InstanceID: "instance-zhipu", ModelName: "glm-4v-plus", ModelType: int(entity.ModelTypeImage2Text), Status: "active", Extra: `{"is_tools":true}`},+ }+ for _, row := range rows {+ if err := db.Create(row).Error; err != nil {+ t.Fatalf("failed to seed %T: %v", row, err)+ }+ }+ return db+}++// seedChatPipelineToolSupportTenant sets the tenant's default chat model, which+// the no-explicit-LLM branch of the probe resolves.+func seedChatPipelineToolSupportTenant(t *testing.T, db *gorm.DB, defaultLLMID string) {+ t.Helper()+ status := string(entity.StatusValid)+ if err := db.Create(&entity.Tenant{+ ID: "tenant-1",+ LLMID: defaultLLMID,+ Status: &status,+ }).Error; err != nil {+ t.Fatalf("failed to create tenant: %v", err)+ }+}++// TestGetLLMModelConfigCarriesToolSupport pins that the resolution reports the+// capability of the row it actually loaded — the config the pipeline then carries+// through the request, which is where Python reads chat_mdl.is_tools from too.+func TestGetLLMModelConfigCarriesToolSupport(t *testing.T) {+ setupChatPipelineToolSupportTestDB(t)+ svc := NewChatPipelineService()++ for _, tc := range []struct {+ name string+ llm string+ wantTools bool+ wantType string+ }{+ {"tool-capable model", "model-tools-on", true, "chat"},+ {"model without tool support", "model-tools-off", false, "chat"},+ // Enrolled only as image-to-text. The type has to be resolved before the+ // row is loaded, otherwise the load is rejected as a chat model and the+ // capability reads as absent while the pipeline runs on this same model.+ {"image2text-only model", "model-image2text-tools", true, "image2text"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ cfg, _, _, _, err := svc.getLLMModelConfig(t.Context(), &entity.Chat{TenantID: "tenant-1", LLMID: tc.llm})+ if err != nil {+ t.Fatalf("getLLMModelConfig(%s) failed: %v", tc.llm, err)+ }+ if got := chatConfigSupportsTools(cfg); got != tc.wantTools {+ t.Errorf("is_tools for %s = %v, want %v", tc.llm, got, tc.wantTools)+ }+ if got, _ := cfg["model_type"].(string); got != tc.wantType {+ t.Errorf("model_type for %s = %q, want %q", tc.llm, got, tc.wantType)+ }+ })+ }+}++// TestResolveChatModelTargetAcceptsImage2TextOnlyModel pins the shared entry point+// the agentic wiring and the generation path both use: it must load a model+// enrolled only as image-to-text — and report its capability — instead of+// rejecting it as a chat model, which is what made such a request silently fall+// back to the regular RAG path.+func TestResolveChatModelTargetAcceptsImage2TextOnlyModel(t *testing.T) {+ setupChatPipelineToolSupportTestDB(t)+ svc := NewChatPipelineService()++ target, err := svc.ModelProviderSvc.ResolveChatModelTarget(t.Context(), "tenant-1", "model-image2text-tools")+ if err != nil {+ t.Fatalf("ResolveChatModelTarget(image2text-only model) failed: %v", err)+ }+ if !target.ModelType.Has(entity.ModelTypeImage2Text) {+ t.Errorf("resolved type = %v, want image2text", target.ModelType)+ }+ if !target.SupportsTools {+ t.Error("resolved target reported no tool support, want true")+ }+}++// TestGetLLMModelConfigRejectsUnusableModels pins the fail-closed rule: a disabled+// model, a model not enrolled as a chat model, and an unenrolled reference all+// fail to resolve (or resolve without a capability) instead of reporting tool+// support — the same outcome as Python's `getattr(chat_mdl, "is_tools", False)`+// default.+func TestGetLLMModelConfigRejectsUnusableModels(t *testing.T) {+ setupChatPipelineToolSupportTestDB(t)+ svc := NewChatPipelineService()++ for _, tc := range []struct {+ name string+ llm string+ }{+ {"disabled model", "model-tools-disabled"},+ {"not enrolled as chat", "model-embedding-tools"},+ {"unknown model", "no-such-model"},+ {"unenrolled composite reference", "ghost@default@ZHIPU-AI"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ cfg, _, _, _, err := svc.getLLMModelConfig(t.Context(), &entity.Chat{TenantID: "tenant-1", LLMID: tc.llm})+ if err == nil && chatConfigSupportsTools(cfg) {+ t.Errorf("unusable model %s reported tool support", tc.llm)+ }+ })+ }+}++// TestGetLLMModelConfigFallsBackToTenantDefault covers the no-explicit-LLM branch:+// the resolution must fall back to the tenant default chat model (composite+// "model@provider" reference) and report its capability.+func TestGetLLMModelConfigFallsBackToTenantDefault(t *testing.T) {+ db := setupChatPipelineToolSupportTestDB(t)+ seedChatPipelineToolSupportTenant(t, db, "glm-4-plus@ZHIPU-AI")+ svc := NewChatPipelineService()++ cfg, _, _, _, err := svc.getLLMModelConfig(t.Context(), &entity.Chat{TenantID: "tenant-1"})+ if err != nil {+ t.Fatalf("getLLMModelConfig(tenant default) failed: %v", err)+ }+ if !chatConfigSupportsTools(cfg) {+ t.Error("tenant default with is_tools=true reported no tool support, want true")+ }+}++// TestGetLLMModelConfigTenantDefaultWithoutToolSupport is the mirror case: a+// tenant default that cannot call tools must not be reported as capable.+func TestGetLLMModelConfigTenantDefaultWithoutToolSupport(t *testing.T) {+ db := setupChatPipelineToolSupportTestDB(t)+ seedChatPipelineToolSupportTenant(t, db, "glm-4-flash@ZHIPU-AI")+ svc := NewChatPipelineService()++ cfg, _, _, _, err := svc.getLLMModelConfig(t.Context(), &entity.Chat{TenantID: "tenant-1"})+ if err != nil {+ t.Fatalf("getLLMModelConfig(tenant default) failed: %v", err)+ }+ if chatConfigSupportsTools(cfg) {+ t.Error("tenant default with is_tools=false reported tool support, want false")+ }+}++// TestChatConfigSupportsToolsFailClosed is the guard case for the config read: a+// missing config, a config without the flag, and a flag carrying anything but a+// truthy boolean all mean "no tool support", the same outcome as Python's default.+func TestChatConfigSupportsToolsFailClosed(t *testing.T) {+ if chatConfigSupportsTools(nil) {+ t.Error("nil config reported tool support, want false")+ }+ if chatConfigSupportsTools(map[string]interface{}{"model_type": "chat"}) {+ t.Error("config without an is_tools flag reported tool support, want false")+ }+ for _, v := range []interface{}{false, "false", "", 0, nil} {+ if chatConfigSupportsTools(map[string]interface{}{"is_tools": v}) {+ t.Errorf("is_tools=%#v reported tool support, want false", v)+ }+ }+ if !chatConfigSupportsTools(map[string]interface{}{"is_tools": true}) {+ t.Error("is_tools=true reported no tool support, want true")+ }+}++// TestReasoningNeedsAgenticGraphGatesOnToolSupport pins the gate Python+// rag_agent applies before building its agentic loop: reasoning level 0 turns it+// off, and so does a chat model that cannot call tools. Only reasoning enabled+// together with tool support drives the agentic graph.+func TestReasoningNeedsAgenticGraphGatesOnToolSupport(t *testing.T) {+ capable := map[string]interface{}{"model_type": "chat", "is_tools": true}+ incapable := map[string]interface{}{"model_type": "chat", "is_tools": false}+ chat := &entity.Chat{TenantID: "tenant-1", LLMID: "model-tools-on"}++ for _, tc := range []struct {+ name string+ chat *entity.Chat+ cfg map[string]interface{}+ reasoningLevel int+ want bool+ }{+ {"reasoning off", chat, capable, 0, false},+ {"reasoning on and tool-capable", chat, capable, 2, true},+ {"reasoning on but no tool support", chat, incapable, 2, false},+ {"reasoning on, unresolvable model", chat, nil, 4, false},+ {"reasoning on, nil dialog", nil, capable, 2, false},+ } {+ t.Run(tc.name, func(t *testing.T) {+ if got := reasoningNeedsAgenticGraph(tc.chat, tc.cfg, tc.reasoningLevel); got != tc.want {+ t.Errorf("reasoningNeedsAgenticGraph(level=%d) = %v, want %v", tc.reasoningLevel, got, tc.want)+ }+ })+ }+}++// TestGetLLMModelConfigReadsToolSupportPerResolution pins the consequence of+// carrying the flag on the resolution instead of memoizing it: a model edited+// after the first request is seen by the next resolution, with no TTL to wait out.+func TestGetLLMModelConfigReadsToolSupportPerResolution(t *testing.T) {+ db := setupChatPipelineToolSupportTestDB(t)+ svc := NewChatPipelineService()+ chat := &entity.Chat{TenantID: "tenant-1", LLMID: "model-tools-on"}++ cfg, _, _, _, err := svc.getLLMModelConfig(t.Context(), chat)+ if err != nil {+ t.Fatalf("getLLMModelConfig failed: %v", err)+ }+ if !chatConfigSupportsTools(cfg) {+ t.Fatal("tool-capable model reported no support")+ }++ if err := db.Model(&entity.TenantModel{}).Where("id = ?", "model-tools-on").+ Update("extra", `{"is_tools":false}`).Error; err != nil {+ t.Fatalf("flip is_tools: %v", err)+ }++ cfg, _, _, _, err = svc.getLLMModelConfig(t.Context(), chat)+ if err != nil {+ t.Fatalf("getLLMModelConfig after the flag flip failed: %v", err)+ }+ if chatConfigSupportsTools(cfg) {+ t.Error("a flipped is_tools flag was not seen by the next resolution")+ }+}diff --git a/internal/service/kb_prompt.go b/internal/service/kb_prompt.goindex 6b5b17a6f0..8d3ea647ec 100644--- a/internal/service/kb_prompt.go+++ b/internal/service/kb_prompt.go@@ -97,14 +97,14 @@ func formatChunkEntry(ck SourcedChunk, index int) string {var b strings.Builderfmt.Fprintf(&b, "ID: %d\n", index)if ck.DocName != "" {- fmt.Fprintf(&b, "├── Title: %s\n", ck.DocName)+ fmt.Fprintf(&b, "├── Title: %s\n", collapseNewlines(ck.DocName))}if ck.URL != "" {- fmt.Fprintf(&b, "├── URL: %s\n", ck.URL)+ fmt.Fprintf(&b, "├── URL: %s\n", collapseNewlines(ck.URL))}if ck.DocumentMetadata != nil {for k, v := range ck.DocumentMetadata {- fmt.Fprintf(&b, "├── %s: %v\n", k, v)+ fmt.Fprintf(&b, "├── %s: %v\n", k, collapseNewlines(fmt.Sprint(v)))}}b.WriteString("└── Content:\n")@@ -112,3 +112,16 @@ func formatChunkEntry(ck SourcedChunk, index int) string {b.WriteString("\n\n")return b.String()}++// collapseNewlines mirrors draw_node() in Python's _kb_block(): a title, URL or+// metadata value is a single line in the rendered block, so embedded newlines+// must not break the tree the model is reading.+func collapseNewlines(s string) string {+ if !strings.Contains(s, "\n") {+ return s+ }+ for strings.Contains(s, "\n\n") {+ s = strings.ReplaceAll(s, "\n\n", "\n")+ }+ return strings.ReplaceAll(s, "\n", " ")+}diff --git a/internal/service/kb_prompt_test.go b/internal/service/kb_prompt_test.goindex 95871bd7df..e9c65508c4 100644--- a/internal/service/kb_prompt_test.go+++ b/internal/service/kb_prompt_test.go@@ -1,6 +1,7 @@package serviceimport (+ "strings""testing""ragflow/internal/tokenizer"@@ -146,3 +147,49 @@ func TestKbPrompt_AllFit(t *testing.T) {t.Error("both chunks should fit under generous limit")}}++// TestKBPromptRendersPythonBlockShape pins the block shape to Python's+// _kb_block() (rag/prompts/generator.py:140): ID, Title, URL, metadata lines and+// Content, with embedded newlines in the single-line fields collapsed to spaces.+func TestKBPromptRendersPythonBlockShape(t *testing.T) {+ svc := &ChatPipelineService{}+ chunks := []map[string]interface{}{+ {+ "id": "a",+ "docnm_kwd": "规范.docx",+ "content_with_weight": "正文",+ },+ {+ "id": "b",+ "docnm_kwd": "多行\n标题.docx",+ "url": "https://example.com/x\n",+ "document_metadata": map[string]interface{}{"author": "a\nb"},+ "content_with_weight": "第二块",+ },+ }++ blocks := svc.kbPrompt(map[string]interface{}{"chunks": chunks}, 8000)+ if len(blocks) != 2 {+ t.Fatalf("blocks = %d, want 2", len(blocks))+ }++ if want := "\nID: 0\n├── Title: 规范.docx\n└── Content:\n正文"; blocks[0] != want {+ t.Errorf("block 0 = %q, want %q", blocks[0], want)+ }++ second := blocks[1]+ for _, want := range []string{+ "\nID: 1\n",+ "├── Title: 多行 标题.docx\n",+ "├── URL: https://example.com/x \n",+ "├── author: a b\n",+ "└── Content:\n第二块",+ } {+ if !strings.Contains(second, want) {+ t.Errorf("block 1 = %q, missing %q", second, want)+ }+ }+ if strings.Contains(second, "\n标题.docx") || strings.Contains(second, "a\nb") {+ t.Errorf("block 1 kept raw newlines in a single-line field: %q", second)+ }+}diff --git a/internal/service/model_service.go b/internal/service/model_service.goindex 5b54289f45..003d758918 100644--- a/internal/service/model_service.go+++ b/internal/service/model_service.go@@ -3546,7 +3546,7 @@ func (m *ModelProviderService) ResolveModelToolSupport(ctx context.Context, tenaif !allowed {return false, fmt.Errorf("tenant %s has no access to provider owned by tenant %s", tenantID, provider.TenantID)}- return toolSupportFromTenantModel(modelObj.Extra, provider.ProviderName, modelObj.ModelName), nil+ return toolSupportFromEnrollment(modelObj.Extra, "", provider.ProviderName, modelObj.ModelName), nil} else if !errors.Is(err, gorm.ErrRecordNotFound) {return false, err}@@ -3577,7 +3577,7 @@ func (m *ModelProviderService) ResolveModelToolSupport(ctx context.Context, tenaif modelObj.Status != "active" {return false, fmt.Errorf("model %q is disabled", modelRef)}- return toolSupportFromTenantModel(modelObj.Extra, provider.ProviderName, modelObj.ModelName), nil+ return toolSupportFromEnrollment(modelObj.Extra, "", provider.ProviderName, modelObj.ModelName), nil}if !errors.Is(err, gorm.ErrRecordNotFound) {return false, fmt.Errorf("model %q lookup failed: %w", modelRef, err)@@ -3589,16 +3589,63 @@ func (m *ModelProviderService) ResolveModelToolSupport(ctx context.Context, tenareturn catalogToolSupport(providerName, pureModelName), nil}-// toolSupportFromTenantModel returns the is_tools flag persisted on a tenant-// model's extra JSON when present, otherwise the provider catalog's declaration-// for that model. Mirrors Python's model_extra.get("is_tools", is_tool).-func toolSupportFromTenantModel(extra, providerName, modelName string) bool {+// ResolveChatModelTarget resolves the chat model a request will run on: the+// caller's reference when it has one, the tenant default otherwise — a dialog+// without an llm_id still runs on the default (dialog_service get_models).+// Shared by the capability probe and the agentic wiring so the two cannot resolve+// different models for one request.+func (m *ModelProviderService) ResolveChatModelTarget(ctx context.Context, tenantID, modelRef string) (*ModelTarget, error) {+ if m == nil {+ return nil, fmt.Errorf("%w: model provider service is not initialized", errModelConfigUnavailable)+ }+ solver := m.modelSolver()+ if strings.TrimSpace(modelRef) == "" {+ return solver.ResolveDefaultModelConfig(ctx, tenantID, entity.ModelTypeChat)+ }+ // Resolve the enrolled type first: a reference enrolled only as image-to-text+ // is a valid chat-pipeline input (its driver answers chat requests), and+ // resolving it as chat would fail the type check outright.+ return solver.ResolveModelConfig(ctx, tenantID, solver.ResolveChatModelType(ctx, tenantID, modelRef), modelRef)+}++// modelTargetRef renders a resolved model as the lookups' reference: its+// tenant_model id, or the composite "model@instance@provider" form.+func modelTargetRef(target *ModelTarget) string {+ if target == nil {+ return ""+ }+ if target.ModelID != "" {+ return target.ModelID+ }+ return fmt.Sprintf("%s@%s@%s", target.ModelName, target.InstanceName, target.ProviderName)+}++// toolSupportFromEnrollment applies the capability precedence the rest of the+// service uses: the is_tools flag persisted on the enrolled model, then the flag+// carried by the instance credential payload, then the provider catalog's+// declaration for the model.+//+// Python's precedence has the same shape — model_extra.get("is_tools", is_tool),+// where is_tool comes out of the instance api_key payload — with the catalog as+// our last resort, because a tenant may enrol a model the catalog does not+// describe at all.+func toolSupportFromEnrollment(extra, instanceAPIKey, providerName, modelName string) bool {if ts, ok := extraToolSupport(extra); ok {return ts}+ // The instance credential can be a JSON object carrying the key together with+ // capability flags; extraToolSupport reads is_tools out of either blob and+ // reports "absent" for a plain credential string.+ if ts, ok := extraToolSupport(instanceAPIKey); ok {+ return ts+ }return catalogToolSupport(providerName, modelName)}+// The tool-calling verdict is no longer memoized: it is computed while the model+// is resolved (see resolvedModel.supportsTools) and travels with the resolution,+// so there is no second lookup to amortize.+// tenantCanReachProviderTenant reports whether userID owns the provider's tenant// or is a joined member of it. Mirrors Python's tenant_model_service// get_model_config_by_id tenant check (:342-347).diff --git a/internal/service/model_solver.go b/internal/service/model_solver.goindex c76c2ad7c3..e95534dd80 100644--- a/internal/service/model_solver.go+++ b/internal/service/model_solver.go@@ -45,6 +45,12 @@ type ModelTarget struct {ModelInfo *modelModule.ModelContextLength intMaxTokens int+ // SupportsTools is the resolved model's tool-calling capability, computed+ // while the model is being resolved (see resolvedModel.supportsTools) so+ // callers never have to look the model up a second time to read it. Python+ // carries the same value as the `is_tools` field of the model config it+ // resolved once, and every consumer reads it from there.+ SupportsTools bool}// ModelSolver is the new model-resolution entry point. The existing@@ -110,9 +116,43 @@ func (s *ModelSolver) ResolveModelConfig(ctx context.Context, tenantID string, mModelInfo: model.modelInfo,ContextLength: contextLength,MaxTokens: maxTokens,+ SupportsTools: model.supportsTools(),}, nil}+// ResolveChatModelType returns the type a chat reference must be resolved as:+// ModelTypeImage2Text when the reference is enrolled vision-capable, chat+// otherwise.+//+// A dialog's llm_id may name a model that is enrolled ONLY as image-to-text, and+// that model is still a valid chat-pipeline input. Resolving such a reference as+// chat fails outright ("cannot be used as chat model"), so every caller that+// needs a capability of the request's chat model must agree on which row to load+// — otherwise the model the request runs on and the model it is judged by are two+// different rows (or, for an image2text-only enrollment, the second lookup fails+// and the capability reads as absent).+//+// Probe failures are conservative and yield chat: that is the type a plain chat+// model is enrolled as, and it keeps image attachments out of a model whose+// vision support could not be established.+func (s *ModelSolver) ResolveChatModelType(ctx context.Context, tenantID, modelRef string) entity.ModelType {+ if s == nil || strings.TrimSpace(modelRef) == "" {+ return entity.ModelTypeChat+ }+ modelTypes, err := s.ResolveModelType(ctx, tenantID, modelRef)+ if err != nil {+ return entity.ModelTypeChat+ }+ for _, mt := range modelTypes {+ // ModelType is a bitmask: a model enrolled as chat+image2text reports a+ // combined value, so test membership, not equality.+ if mt.Has(entity.ModelTypeImage2Text) {+ return entity.ModelTypeImage2Text+ }+ }+ return entity.ModelTypeChat+}+// ResolveDefaultModelConfig resolves the tenant's configured default model// for modelType. It first uses the tenant model ID when present and falls back// to the stored model reference if that ID is unavailable.@@ -220,6 +260,35 @@ type resolvedModel struct {maxTokens int}+// supportsTools reports whether the resolved model can emit tool calls, using the+// same precedence the standalone probe uses: the flag persisted on the enrolled+// model, then the instance credential payload, then the provider catalog.+//+// It is a method on the resolution rather than a separate lookup so that the+// capability is answered by the row that was just loaded, instead of by a second+// lookup the caller has to key on a type it may get wrong (see+// ResolveChatModelType).+func (m *resolvedModel) supportsTools() bool {+ if m == nil {+ return false+ }+ var extra, providerName, modelName, instanceAPIKey string+ if m.modelEntity != nil {+ extra = m.modelEntity.Extra+ modelName = m.modelEntity.ModelName+ }+ if m.providerEntity != nil {+ providerName = m.providerEntity.ProviderName+ }+ if modelName == "" {+ modelName = m.modelName+ }+ if m.apiConfig != nil && m.apiConfig.ApiKey != nil {+ instanceAPIKey = *m.apiConfig.ApiKey+ }+ return toolSupportFromEnrollment(extra, instanceAPIKey, providerName, modelName)+}+func (s *ModelSolver) lookupTenantModel(ctx context.Context, tenantID, modelRef string) (*entity.TenantModel, error) {if s == nil || s.service == nil {return nil, fmt.Errorf("%w: model solver is not initialized", errModelConfigUnavailable)diff --git a/internal/service/nlp/query_builder.go b/internal/service/nlp/query_builder.goindex 27ed03d1c4..f0bbe73462 100644--- a/internal/service/nlp/query_builder.go+++ b/internal/service/nlp/query_builder.go@@ -538,7 +538,9 @@ func (qb *QueryBuilder) Question(txt string, tbl string, minMatch float64) (*typ// termParts collects query parts for each term in the segment.var termParts []stringfor _, termWeight := range terms {- termParts = append(termParts, fmt.Sprintf("(%s)^%.1f", termWeight.term, termWeight.weight))+ // %v, not %.1f: the reference (rag/nlp/query.py:152) writes the raw+ // weight, and one decimal flattens every weight below 0.05 to ^0.0.+ termParts = append(termParts, fmt.Sprintf("(%s)^%v", termWeight.term, termWeight.weight))}// tmsStr is the query string for the current segment.tmsStr := strings.Join(termParts, " ")@@ -572,8 +574,6 @@ func (qb *QueryBuilder) Question(txt string, tbl string, minMatch float64) (*typif tmsStr != "" {qs = append(qs, tmsStr)- } else {- fmt.Println("tmsStr is empty")}}diff --git a/internal/service/nlp/retrieval.go b/internal/service/nlp/retrieval.goindex cec0583224..eecb202f04 100644--- a/internal/service/nlp/retrieval.go+++ b/internal/service/nlp/retrieval.go@@ -143,7 +143,22 @@ func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest)if req.RerankModel != nil && req.Page != 1 {return nil, fmt.Errorf("Pagination is not supported when rerank_mdl is specified. Please set page=1 to retrieve the top %d results.", pageSize)}- common.DebugCtx(ctx, "Retrieval rerank candidate params", zap.Int("page", req.Page), zap.Int("pageSize", pageSize), zap.Int("rerankCandidatesCount", rerankCandidatesCount))+ // Request-scoped pool size (chat settings / tool arguments): without it a+ // shortfall is indistinguishable from a search that simply matched less.+ engineType := ""+ if s.docEngine != nil {+ engineType = s.docEngine.GetType()+ }+ common.InfoCtx(ctx, "Retrieval candidates",+ zap.Int("page", req.Page),+ zap.Int("pageSize", pageSize),+ zap.Int("rerankCandidatesCount", rerankCandidatesCount),+ zap.Int("knnTopK", *req.KNNTopK),+ zap.Int("knnNumCandidates", *req.KNNNumCandidates),+ zap.Float64("similarityThreshold", *req.SimilarityThreshold),+ zap.Float64("vectorSimilarityWeight", *req.VectorSimilarityWeight),+ zap.String("engine", engineType),+ zap.Bool("vectorOnly", req.VectorOnly))// Execute search via Search()searchReq := &RetrievalSearchRequest{@@ -157,6 +172,7 @@ func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest)KNNNumCandidates: *req.KNNNumCandidates,RankFeature: *req.RankFeature,EmbeddingModel: req.EmbeddingModel,+ SimilarityThreshold: *req.SimilarityThreshold,VectorSimilarityWeight: req.VectorSimilarityWeight,Highlight: req.Highlight,AllowDenseFallback: req.AllowDenseFallback,@@ -607,26 +623,36 @@ func buildRetrievalFusionExpr(docEngineType string, topn int, vectorSimilarityWereturn buildInfinityFusionExpr(topn, vectorSimilarityWeight)}- // The caller weight MUST reach the engine: the previous hardcoded- // "0.05,0.95" silently discarded it, so every hybrid search on the ES- // backend ran at 95% vector / 5% keyword regardless of what the tool or- // the model asked for - and chunk.go derives the BM25 boost from this- // same value, so the lexical leg was effectively switched off.- vectorWeight := 0.3- if vectorSimilarityWeight != nil {- vectorWeight = *vectorSimilarityWeight- }- termWeight := math.Round((1.0-vectorWeight)*10000) / 10000-+ // Matches rag/nlp/search.py:331 — every backend but Infinity takes this fixed+ // pair, so the first search is a vector recall pass and the caller's+ // vector_similarity_weight is applied afterwards, by RerankWithKNN+ // (tkWeight = 1-vw, vtWeight = vw). Feeding it in here instead ranks the+ // window by BM25 and cuts a different top-N than the reference does.return &types.FusionExpr{Method: "weighted_sum",TopN: topn,FusionParams: map[string]interface{}{- "weights": fmt.Sprintf("%g,%g", termWeight, vectorWeight),+ "weights": esFusionWeights,},}}+// esFusionWeights is the pair the reference gives every non-Infinity backend+// (rag/nlp/search.py:331). 0.001 rather than 0 keeps a lexical leg for engines+// that reject a zero weight.+const esFusionWeights = "0.001,1"++// minMatch mirrors Python's `min_match = vector_similarity_weight < 0.8`+// (rag/nlp/search.py:773): a search that is almost entirely vector-weighted asks+// the text leg for nothing in particular, so its keyword query matches on any term+// instead of a share of them (0.3 first, 0.1 on the looser retry).+func minMatch(vectorSimilarityWeight *float64, withMatch float64) float64 {+ if vectorSimilarityWeight != nil && *vectorSimilarityWeight >= 0.8 {+ return 0.0+ }+ return withMatch+}+type RetrievalSearchResult struct {Chunks []map[string]interface{} // Search resultsTotal int64 // Total number of matches@@ -718,7 +744,7 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque// Non-empty question// Compute keywords via QueryBuilder- matchText, keywords := GetQueryBuilder().Question(req.Question, "", 0.3)+ matchText, keywords := GetQueryBuilder().Question(req.Question, "", minMatch(req.VectorSimilarityWeight, 0.3))for _, k := range keywords {kwds[k] = struct{}{}}@@ -821,7 +847,7 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque// and lower vector similarity threshold (0.17 vs default 0.1-0.2).// This provides a second chance for queries that were too strict// on the first attempt.- matchText, _ := GetQueryBuilder().Question(req.Question, "qa", 0.1)+ matchText, _ := GetQueryBuilder().Question(req.Question, "qa", minMatch(req.VectorSimilarityWeight, 0.1))matchDense = cloneDenseExpr(denseTemplate)matchDense.ExtraOptions["similarity"] = 0.17if req.VectorOnly || matchText == nil {@@ -869,6 +895,37 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque}searchResult := engineResult+ // The window the engine was asked for vs what it returned, together with what+ // it was asked FOR: a shortfall is indistinguishable from a narrower query+ // without both, and it is what a caller sees as a thin candidate pool (the ES+ // backend is asked for rerankCandidatesCount candidates, see "Retrieval+ // candidates"). The query is read off MatchExprs — the expressions that+ // actually went to the engine — so the retries that re-ask with a lower+ // minimum_should_match are reported as issued.+ if searchResult != nil {+ engineQuery := ""+ if len(searchRequest.MatchExprs) > 0 {+ if mt, ok := searchRequest.MatchExprs[0].(*types.MatchTextExpr); ok && mt != nil {+ engineQuery = mt.MatchingText+ }+ }+ knnTopN, knnExtra := 0, map[string]interface{}(nil)+ if len(searchRequest.MatchExprs) > 1 {+ if de, ok := searchRequest.MatchExprs[1].(*types.MatchDenseExpr); ok && de != nil {+ knnTopN, knnExtra = de.TopN, de.ExtraOptions+ }+ }+ common.InfoCtx(ctx, "Search window",+ zap.Int("limit", limit), zap.Int("offset", pg*pageSize),+ zap.Int("engineChunks", len(searchResult.Chunks)), zap.Int64("engineTotal", searchResult.Total),+ zap.Strings("indexNames", searchRequest.IndexNames),+ zap.Int("matchExprs", len(searchRequest.MatchExprs)),+ zap.String("query", engineQuery),+ zap.Any("searchFilters", filters),+ zap.Any("requestFilter", req.Filter),+ zap.Strings("docIDs", req.DocIDs),+ zap.Int("knnTopN", knnTopN), zap.Any("knnExtra", knnExtra))+ }ids := s.docEngine.GetChunkIDs(searchResult.Chunks)common.Info("GetChunkIDs result", zap.Int("count", len(ids)), zap.Strings("ids", ids))diff --git a/internal/service/nlp/retrieval_test.go b/internal/service/nlp/retrieval_test.goindex 3756b96861..dbcdd7418d 100644--- a/internal/service/nlp/retrieval_test.go+++ b/internal/service/nlp/retrieval_test.go@@ -511,19 +511,21 @@ func assertFusionWeights(t *testing.T, request *types.SearchRequest, want string}}-func TestBuildRetrievalFusionExprKeepsPythonWeightsOutsideInfinity(t *testing.T) {- expr := buildRetrievalFusionExpr(string(engine.EngineElasticsearch), 10, float64Ptr(0.8))-- // Elasticsearch must honour the caller weight exactly like Infinity does:- // it used to be hardcoded to "0.05,0.95", which silently discarded it.- if got := expr.FusionParams["weights"]; got != "0.2,0.8" {- t.Fatalf("expected Elasticsearch weights=0.2,0.8, got %v", got)+func TestBuildRetrievalFusionExprMatchesPythonEngineWeights(t *testing.T) {+ // Elasticsearch takes the reference's fixed pair regardless of the caller+ // weight: its first search is a vector recall pass, and the caller weight is+ // applied afterwards by RerankWithKNN.+ for _, callerWeight := range []*float64{float64Ptr(0.8), float64Ptr(0.3), nil} {+ expr := buildRetrievalFusionExpr(string(engine.EngineElasticsearch), 10, callerWeight)+ if got := expr.FusionParams["weights"]; got != esFusionWeights {+ t.Fatalf("expected Elasticsearch weights=%s, got %v", esFusionWeights, got)+ }}- // nil weight falls back to the documented default (0.3 vector).- expr = buildRetrievalFusionExpr(string(engine.EngineElasticsearch), 10, nil)- if got := expr.FusionParams["weights"]; got != "0.7,0.3" {- t.Fatalf("expected default Elasticsearch weights=0.7,0.3, got %v", got)+ // Infinity fuses the two legs itself, so it keeps the caller weight.+ expr := buildRetrievalFusionExpr(string(engine.EngineInfinity), 10, float64Ptr(0.8))+ if got := expr.FusionParams["weights"]; got != "0.2,0.8" {+ t.Fatalf("expected Infinity weights=0.2,0.8, got %v", got)}}@@ -558,9 +560,7 @@ func TestSearchKeepsPythonFusionWeightForElasticsearch(t *testing.T) {if !ok {t.Fatalf("expected third match expression to be FusionExpr, got %T", docEngine.searchRequest.MatchExprs[2])}- // Elasticsearch must honour the caller weight (0.8 vector -> "0.2,0.8")- // instead of the legacy hardcoded "0.05,0.95".- if got := fusionExpr.FusionParams["weights"]; got != "0.2,0.8" {- t.Fatalf("expected Elasticsearch weights=0.2,0.8, got %v", got)+ if got := fusionExpr.FusionParams["weights"]; got != esFusionWeights {+ t.Fatalf("expected Elasticsearch weights=%s, got %v", esFusionWeights, got)}}