Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

refactor(extractor): resolve chat model config once per run (#21960)

xugangqiang8e1c2acccfd4
diff --git a/internal/ingestion/component/extractor.go b/internal/ingestion/component/extractor.go
index c45a59e0d0..99db93bfe9 100644
--- a/internal/ingestion/component/extractor.go
+++ b/internal/ingestion/component/extractor.go
@@ -445,6 +445,97 @@ func getExtractorChatTargetResolverOverride() func(llmID string) (driver, modelN
return extractorChatTargetResolverOverride
}
+// extractorResolvedTarget is the memoized result of a successful chat-target
+// resolution. Only successful resolutions are cached (see target), so the
+// stored entry never carries an error.
+type extractorResolvedTarget struct {
+ driver, modelName, apiKey, baseURL string
+}
+
+// extractorModelCache memoizes per-run chat-model resolution. Within a single
+// ExtractorComponent.Invoke the llm_id is constant, so the chat target
+// (driver/model/apiKey/baseURL) and the context length are resolved once and
+// reused across every chunk / LLM call, avoiding thousands of redundant DAO
+// reads per document. It is scoped to one run via context (never package
+// global) so concurrent documents cannot leak state across tenants, and it is
+// concurrency-safe because the extractor pool runs chunk jobs in parallel.
+type extractorModelCache struct {
+ mu sync.Mutex
+ targets map[string]extractorResolvedTarget
+ ctxLens map[string]int
+
+ // resolveTarget / resolveCtxLen are the underlying resolvers, wired to the
+ // uncached implementations by newExtractorModelCache. Tests may replace
+ // them to avoid a database.
+ resolveTarget func(ctx context.Context, db *gorm.DB, llmID string) (string, string, string, string, error)
+ resolveCtxLen func(ctx context.Context, db *gorm.DB, llmID string) int
+}
+
+func newExtractorModelCache() *extractorModelCache {
+ return &extractorModelCache{
+ targets: make(map[string]extractorResolvedTarget),
+ ctxLens: make(map[string]int),
+ resolveTarget: resolveExtractorChatTargetUncached,
+ resolveCtxLen: extractorContextLengthUncached,
+ }
+}
+
+// target returns the resolved chat target for llmID, computing and caching it
+// on first use. Only successful resolutions are cached; a failed resolution is
+// not memoized so a transient error is re-attempted and the run still fails
+// fast on the first chunk instead of reusing a stale error.
+func (m *extractorModelCache) target(ctx context.Context, db *gorm.DB, llmID string) (driver, modelName, apiKey, baseURL string, err error) {
+ m.mu.Lock()
+ if r, ok := m.targets[llmID]; ok {
+ m.mu.Unlock()
+ return r.driver, r.modelName, r.apiKey, r.baseURL, nil
+ }
+ m.mu.Unlock()
+
+ driver, modelName, apiKey, baseURL, err = m.resolveTarget(ctx, db, llmID)
+ if err == nil {
+ m.mu.Lock()
+ m.targets[llmID] = extractorResolvedTarget{driver, modelName, apiKey, baseURL}
+ m.mu.Unlock()
+ }
+ return driver, modelName, apiKey, baseURL, err
+}
+
+// ctxLen returns the resolved context length for llmID, computing and caching
+// it on first use. A zero result (unknown model / no canvas state) is a valid,
+// cacheable resolution meaning "skip fitting".
+func (m *extractorModelCache) ctxLen(ctx context.Context, db *gorm.DB, llmID string) int {
+ m.mu.Lock()
+ if v, ok := m.ctxLens[llmID]; ok {
+ m.mu.Unlock()
+ return v
+ }
+ m.mu.Unlock()
+
+ v := m.resolveCtxLen(ctx, db, llmID)
+ m.mu.Lock()
+ m.ctxLens[llmID] = v
+ m.mu.Unlock()
+ return v
+}
+
+type extractorModelCacheKey struct{}
+
+// withExtractorModelCache attaches a fresh per-run model cache to ctx. The
+// cache is created once per ExtractorComponent.Invoke and shared by every
+// downstream chunk job, so resolution happens at most once per distinct
+// llm_id for the whole run.
+func withExtractorModelCache(ctx context.Context) context.Context {
+ return context.WithValue(ctx, extractorModelCacheKey{}, newExtractorModelCache())
+}
+
+func extractorModelCacheFromCtx(ctx context.Context) *extractorModelCache {
+ if c, ok := ctx.Value(extractorModelCacheKey{}).(*extractorModelCache); ok {
+ return c
+ }
+ return nil
+}
+
// getExtractorChatInvoker returns the current default invoker.
func getExtractorChatInvoker() extractorChatInvoker {
extractorChatInvokerMu.RLock()
@@ -558,7 +649,7 @@ type extractorInputs struct {
// that is empty — the tenant-default chat model resolved once per run in
// Invoke). Carrying it on the struct lets the per-chunk cache-key builders
// reuse the single run-level resolution instead of re-resolving the default
- // model for every chunk (review finding #3). Empty means "not yet resolved"
+ // model for every chunk. Empty means "not yet resolved"
// and triggers a per-call fallback so direct unit-test construction of
// extractorInputs keeps working.
modelID string
@@ -661,11 +752,23 @@ func (c *ExtractorComponent) Invoke(ctx context.Context, db *gorm.DB, inputs map
if err := c.Param.Validate(); err != nil {
return nil, fmt.Errorf("extractor: %w", err)
}
+ // Attach a per-run model cache so the chat target and context length are
+ // resolved once per distinct llm_id and reused across every chunk, instead
+ // of being re-resolved in every callRaw / fitExtractorMessages.
+ ctx = withExtractorModelCache(ctx)
in := c.resolveInputs(inputs)
// Resolve the cache-key model identity once for the whole run and reuse it
// across every chunk, rather than re-resolving the (most common) default
- // model per chunk when keying the per-chunk cache. See review finding #3.
+ // model per chunk when keying the per-chunk cache.
in.modelID = extractorCacheModelID(ctx, db, in.llmID)
+ // Warm the cache before fan-out so the (parallel) chunk jobs read a
+ // populated entry: the resolution happens exactly once here, eliminating
+ // any first-batch resolution herd. A failed resolution is not cached, so
+ // the first chunk's callRaw still surfaces it and the run fails fast.
+ if cache := extractorModelCacheFromCtx(ctx); cache != nil {
+ cache.target(ctx, db, in.llmID)
+ cache.ctxLen(ctx, db, in.llmID)
+ }
common.Debug("extractor stage",
zap.String("component", "Extractor"),
zap.Int("input_chunks", len(in.chunks)),
@@ -1453,11 +1556,22 @@ func (c *ExtractorComponent) callStructured(ctx context.Context, db *gorm.DB, in
return parsed, nil
}
-// resolveExtractorChatTarget resolves the llm_id into driver / model /
-// api_key / base_url. The llm_id may be a bare tenant_model UUID or
-// a composite "model@provider" string. Errors from DAO resolution are
-// propagated so the caller sees the real failure reason.
+// resolveExtractorChatTarget resolves the chat target for llmID, reusing a
+// per-run cache attached to ctx when present. Without the cache it behaves
+// exactly as resolveExtractorChatTargetUncached (the historical per-call path),
+// so callers and tests that do not inject a cache are unchanged.
func resolveExtractorChatTarget(ctx context.Context, db *gorm.DB, llmID string) (driver, modelName, apiKey, baseURL string, err error) {
+ if c := extractorModelCacheFromCtx(ctx); c != nil {
+ return c.target(ctx, db, llmID)
+ }
+ return resolveExtractorChatTargetUncached(ctx, db, llmID)
+}
+
+// resolveExtractorChatTargetUncached resolves the llm_id into driver / model /
+// api_key / base_url without any per-run cache. The llm_id may be a bare
+// tenant_model UUID or a composite "model@provider" string. Errors from DAO
+// resolution are propagated so the caller sees the real failure reason.
+func resolveExtractorChatTargetUncached(ctx context.Context, db *gorm.DB, llmID string) (driver, modelName, apiKey, baseURL string, err error) {
if override := getExtractorChatTargetResolverOverride(); override != nil {
if driver, modelName, apiKey, baseURL, ok := override(llmID); ok {
return driver, modelName, apiKey, baseURL, nil
@@ -1623,19 +1737,30 @@ func getExtractorContextLengthOverride() func(ctx context.Context, llmID string)
return extractorContextLengthOverride
}
-// extractorContextLength returns the chat model's context window
-// (content_length) for the effective chat model used by a call, or 0 when
-// unavailable. Mirrors Python's chat_mdl.max_length. Used as the token
-// budget for message fitting so oversized prompts are trimmed instead of
-// rejected by the provider. When llm_id is empty the call falls back to the
-// tenant default chat model (see resolveExtractorChatTarget), so the same
-// model is resolved here; otherwise the default-model path would never get
-// message fitting. Returns 0 (skip fitting) when the model is unknown (e.g.
-// unit tests with synthetic llm_id or no canvas state).
+// extractorContextLength returns the chat model's context window for llmID,
+// reusing a per-run cache attached to ctx when present. The context-length
+// override seam is still consulted first (unchanged test priority), and without
+// a cache the behavior is exactly extractorContextLengthUncached.
func extractorContextLength(ctx context.Context, db *gorm.DB, llmID string) int {
if fn := getExtractorContextLengthOverride(); fn != nil {
return fn(ctx, llmID)
}
+ if c := extractorModelCacheFromCtx(ctx); c != nil {
+ return c.ctxLen(ctx, db, llmID)
+ }
+ return extractorContextLengthUncached(ctx, db, llmID)
+}
+
+// extractorContextLengthUncached returns the chat model's context window
+// (content_length) for llmID, or 0 when unavailable. Mirrors Python's
+// chat_mdl.max_length. Used as the token budget for message fitting so
+// oversized prompts are trimmed instead of rejected by the provider. When
+// llm_id is empty the call falls back to the tenant default chat model (see
+// resolveExtractorChatTarget), so the same model is resolved here; otherwise
+// the default-model path would never get message fitting. Returns 0 (skip
+// fitting) when the model is unknown (e.g. unit tests with synthetic llm_id or
+// no canvas state).
+func extractorContextLengthUncached(ctx context.Context, db *gorm.DB, llmID string) int {
state, err := runtime.GetStateFromContext(ctx)
if err != nil || state == nil {
return 0
diff --git a/internal/ingestion/component/extractor_cache_test.go b/internal/ingestion/component/extractor_cache_test.go
index 1ad60b509b..0c4e1af452 100644
--- a/internal/ingestion/component/extractor_cache_test.go
+++ b/internal/ingestion/component/extractor_cache_test.go
@@ -258,20 +258,14 @@ func TestCallTextCached_NoStoreStillCalls(t *testing.T) {
}
}
-// TestExtractor_ResolvesDefaultModelOncePerRun is the regression test for
-// review finding #3: on the default-model path (empty llm_id) the model
-// identity used to key the per-chunk cache must be resolved exactly ONCE per
-// run and reused across every chunk — not re-resolved per chunk. The per-chunk
-// re-resolution was a real performance regression introduced by this PR's
-// correctness fix (the cache key now derives from the resolved default model,
-// not the empty override), and is most visible when there is no cache at all
-// (every call reaches the resolver for nothing).
+// TestExtractor_ResolvesDefaultModelOncePerRun asserts that on the
+// default-model path (empty llm_id) the chat-target resolution happens exactly
+// ONCE for the whole run and is reused across every chunk — not re-resolved
+// per chunk, and not even once per callRaw. The run-level warm
+// (withExtractorModelCache in Invoke) resolves the default model a single
+// time; every callRaw and the per-chunk cache-key build then hit the cache.
//
-// The invariant: the chat-target resolver override is consulted once for each
-// real LLM call (callRaw resolves its target) and exactly once more for the
-// whole run (the run-level cache-key model resolution). So
-// resolverCalls == stub.Calls()+1. Before the fix the cache-key build
-// re-resolved the model per chunk, giving 2*stub.Calls().
+// The invariant: resolverCalls == 1 regardless of how many LLM calls happened.
func TestExtractor_ResolvesDefaultModelOncePerRun(t *testing.T) {
var resolverCalls atomic.Int32
SetExtractorChatTargetResolverOverride(func(llmID string) (string, string, string, string, bool) {
@@ -289,7 +283,7 @@ func TestExtractor_ResolvesDefaultModelOncePerRun(t *testing.T) {
}
c := &ExtractorComponent{Param: schema.ExtractorParam{
- LLMID: "", // default-model path — the one #3 is about
+ LLMID: "", // default-model path
Keywords: schema.KeywordExtractConfig{Enabled: true, TopN: 3},
Metadata: schema.MetadataExtractConfig{
Enabled: true,
@@ -303,14 +297,14 @@ func TestExtractor_ResolvesDefaultModelOncePerRun(t *testing.T) {
// Sanity: the run actually exercised the LLM so the assertion below is
// not vacuously true.
- if n := stub.Calls(); n == 0 {
+ llmCalls := stub.Calls()
+ if llmCalls == 0 {
t.Fatal("no LLM calls happened; test did not exercise the extraction path")
}
- want := stub.Calls() + 1
- if got := resolverCalls.Load(); got != want {
- t.Errorf("resolver calls = %d, want %d (== stub.Calls()+1: one run-level model resolution, reused across all chunks)",
- got, want)
+ if got := resolverCalls.Load(); got != 1 {
+ t.Errorf("resolver calls = %d, want 1 (default model resolved once per run, reused across all %d LLM calls)",
+ got, llmCalls)
}
}
diff --git a/internal/ingestion/component/extractor_model_cache_test.go b/internal/ingestion/component/extractor_model_cache_test.go
new file mode 100644
index 0000000000..2690350494
--- /dev/null
+++ b/internal/ingestion/component/extractor_model_cache_test.go
@@ -0,0 +1,322 @@
+//
+// 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 component
+
+import (
+ "context"
+ "errors"
+ "sync"
+ "sync/atomic"
+ "testing"
+
+ "gorm.io/gorm"
+ "ragflow/internal/common"
+ "ragflow/internal/ingestion/component/schema"
+)
+
+// errTestModelCache is a sentinel error used by cache unit tests.
+var errTestModelCache = errors.New("model cache test error")
+
+// TestExtractor_ResolveChatTargetOncePerRun_NonDefault asserts that within a
+// single ExtractorComponent.Invoke the chat-target resolution happens exactly
+// ONCE per distinct llm_id and is reused across every chunk, instead of being
+// re-resolved in every callRaw. The override seam is used as a counter because
+// it is consulted inside the underlying resolver.
+func TestExtractor_ResolveChatTargetOncePerRun_NonDefault(t *testing.T) {
+ var resolverCalls atomic.Int32
+ SetExtractorChatTargetResolverOverride(func(llmID string) (string, string, string, string, bool) {
+ resolverCalls.Add(1)
+ return "openai", "gpt-4o", "ak", "http://x", true
+ })
+ t.Cleanup(func() { SetExtractorChatTargetResolverOverride(nil) })
+
+ stub := withStubChatInvoker(t,
+ stubResponse{Content: "s0"},
+ stubResponse{Content: "s1"},
+ stubResponse{Content: "s2"},
+ )
+
+ const nChunks = 3
+ chunks := make([]map[string]any, nChunks)
+ for i := range chunks {
+ chunks[i] = map[string]any{"text": "body"}
+ }
+
+ c := &ExtractorComponent{Param: schema.ExtractorParam{
+ LLMID: "some-model",
+ Summary: schema.SummaryExtractConfig{Enabled: true},
+ }}
+ if _, err := c.Invoke(t.Context(), nil, map[string]any{"chunks": chunks}); err != nil {
+ t.Fatalf("Invoke: %v", err)
+ }
+
+ if n := stub.Calls(); n != nChunks {
+ t.Errorf("LLM calls = %d, want %d", n, nChunks)
+ }
+ if got := resolverCalls.Load(); got != 1 {
+ t.Errorf("resolver calls = %d, want 1 (resolved once per run and reused across all %d chunks)", got, nChunks)
+ }
+}
+
+// TestExtractor_ResolveChatTargetOncePerRun_Default covers the empty-llm_id
+// (tenant default model) path: the chat-target resolution must happen exactly
+// once per run and be reused across every chunk.
+func TestExtractor_ResolveChatTargetOncePerRun_Default(t *testing.T) {
+ var resolverCalls atomic.Int32
+ SetExtractorChatTargetResolverOverride(func(llmID string) (string, string, string, string, bool) {
+ resolverCalls.Add(1)
+ return "openai", "gpt-4o", "ak", "http://x", true
+ })
+ t.Cleanup(func() { SetExtractorChatTargetResolverOverride(nil) })
+
+ stub := withStubChatInvoker(t,
+ stubResponse{Content: `{"category":"x"}`},
+ stubResponse{Content: `{"category":"x"}`},
+ )
+
+ const nChunks = 2
+ chunks := make([]map[string]any, nChunks)
+ for i := range chunks {
+ chunks[i] = map[string]any{"text": "body"}
+ }
+
+ c := &ExtractorComponent{Param: schema.ExtractorParam{
+ LLMID: "",
+ Keywords: schema.KeywordExtractConfig{Enabled: true, TopN: 3},
+ Metadata: schema.MetadataExtractConfig{
+ Enabled: true,
+ Metadata: []common.MetadataFieldDef{{Key: "category", Type: "string"}},
+ },
+ }}
+ if _, err := c.Invoke(t.Context(), nil, map[string]any{"chunks": chunks}); err != nil {
+ t.Fatalf("Invoke: %v", err)
+ }
+
+ if n := stub.Calls(); n == 0 {
+ t.Fatal("no LLM calls happened; test did not exercise the extraction path")
+ }
+ if got := resolverCalls.Load(); got != 1 {
+ t.Errorf("resolver calls = %d, want 1 (default model resolved once per run, reused across all chunks)", got)
+ }
+}
+
+// TestExtractor_ResolveChatTargetNoCacheCallsEachTime guards the NO-cache
+// branch: when no per-run cache is present in ctx (e.g. direct callers /
+// other entry points), resolveExtractorChatTarget must keep consulting the
+// resolver exactly once per call.
+func TestExtractor_ResolveChatTargetNoCacheCallsEachTime(t *testing.T) {
+ var resolverCalls atomic.Int32
+ SetExtractorChatTargetResolverOverride(func(llmID string) (string, string, string, string, bool) {
+ resolverCalls.Add(1)
+ return "openai", "gpt-4o", "ak", "http://x", true
+ })
+ t.Cleanup(func() { SetExtractorChatTargetResolverOverride(nil) })
+
+ const n = 4
+ for i := 0; i < n; i++ {
+ if _, _, _, _, err := resolveExtractorChatTarget(t.Context(), nil, "some-model"); err != nil {
+ t.Fatalf("resolveExtractorChatTarget: %v", err)
+ }
+ }
+ if got := resolverCalls.Load(); got != n {
+ t.Errorf("resolver calls = %d, want %d (no-cache path must resolve per call)", got, n)
+ }
+}
+
+// TestExtractor_ContextLengthNoCacheCallsEachTime guards the NO-cache branch
+// for extractorContextLength: without a per-run cache the context-length
+// override seam must be consulted every call.
+func TestExtractor_ContextLengthNoCacheCallsEachTime(t *testing.T) {
+ var calls atomic.Int32
+ SetExtractorContextLengthOverride(func(_ context.Context, _ string) int {
+ calls.Add(1)
+ return 123
+ })
+ t.Cleanup(func() { SetExtractorContextLengthOverride(nil) })
+
+ const n = 4
+ for i := 0; i < n; i++ {
+ if got := extractorContextLength(t.Context(), nil, "some-model"); got != 123 {
+ t.Fatalf("extractorContextLength = %d, want 123", got)
+ }
+ }
+ if got := calls.Load(); got != n {
+ t.Errorf("context-length override calls = %d, want %d (no-cache path must resolve per call)", got, n)
+ }
+}
+
+// newSpyModelCache builds an extractorModelCache with injected spy resolvers so
+// the cache behavior can be tested without a database.
+func newSpyModelCache(t *testing.T, targetCalls *atomic.Int32, ctxLenCalls *atomic.Int32) *extractorModelCache {
+ t.Helper()
+ return &extractorModelCache{
+ targets: make(map[string]extractorResolvedTarget),
+ ctxLens: make(map[string]int),
+ resolveTarget: func(_ context.Context, _ *gorm.DB, llmID string) (string, string, string, string, error) {
+ targetCalls.Add(1)
+ return "d", llmID, "k", "u", nil
+ },
+ resolveCtxLen: func(_ context.Context, _ *gorm.DB, _ string) int {
+ ctxLenCalls.Add(1)
+ return 123
+ },
+ }
+}
+
+// TestExtractorModelCache_TargetCachesSuccess verifies a successful resolution
+// is computed once and reused.
+func TestExtractorModelCache_TargetCachesSuccess(t *testing.T) {
+ var tc, cc atomic.Int32
+ cache := newSpyModelCache(t, &tc, &cc)
+
+ for i := 0; i < 5; i++ {
+ d, m, k, u, err := cache.target(t.Context(), nil, "x")
+ if err != nil {
+ t.Fatalf("target: %v", err)
+ }
+ if d != "d" || m != "x" || k != "k" || u != "u" {
+ t.Fatalf("unexpected target: %q %q %q %q", d, m, k, u)
+ }
+ }
+ if got := tc.Load(); got != 1 {
+ t.Errorf("target resolver calls = %d, want 1 (cached after first)", got)
+ }
+}
+
+// TestExtractorModelCache_TargetDoesNotCacheError verifies a failed resolution
+// is never memoized: every call re-invokes the resolver and returns the error,
+// so a transient failure is re-attempted and the run fails fast on first use.
+func TestExtractorModelCache_TargetDoesNotCacheError(t *testing.T) {
+ var tc atomic.Int32
+ cache := &extractorModelCache{
+ targets: make(map[string]extractorResolvedTarget),
+ ctxLens: make(map[string]int),
+ resolveTarget: func(_ context.Context, _ *gorm.DB, _ string) (string, string, string, string, error) {
+ tc.Add(1)
+ return "", "", "", "", errTestModelCache
+ },
+ }
+
+ const n = 4
+ for i := 0; i < n; i++ {
+ if _, _, _, _, err := cache.target(t.Context(), nil, "x"); err != errTestModelCache {
+ t.Fatalf("target err = %v, want errTestModelCache", err)
+ }
+ }
+ if got := tc.Load(); got != n {
+ t.Errorf("target resolver calls = %d, want %d (errors must not be cached)", got, n)
+ }
+}
+
+// TestExtractorModelCache_TargetConcurrentWarmThenRead proves the production
+// shape: the run warms the cache once (serial), then the parallel chunk jobs
+// read a populated entry — the resolver is invoked exactly once and every
+// concurrent reader observes the identical value.
+func TestExtractorModelCache_TargetConcurrentWarmThenRead(t *testing.T) {
+ var tc, cc atomic.Int32
+ cache := newSpyModelCache(t, &tc, &cc)
+
+ // Warm serially (mirrors Invoke's pre-fan-out warm).
+ if _, _, _, _, err := cache.target(t.Context(), nil, "x"); err != nil {
+ t.Fatalf("warm: %v", err)
+ }
+ if got := tc.Load(); got != 1 {
+ t.Fatalf("warm resolver calls = %d, want 1", got)
+ }
+
+ const readers = 64
+ var wg sync.WaitGroup
+ wg.Add(readers)
+ var bad atomic.Int32
+ for i := 0; i < readers; i++ {
+ go func() {
+ defer wg.Done()
+ d, m, k, u, err := cache.target(t.Context(), nil, "x")
+ if err != nil || d != "d" || m != "x" || k != "k" || u != "u" {
+ bad.Add(1)
+ }
+ }()
+ }
+ wg.Wait()
+
+ if bad.Load() != 0 {
+ t.Errorf("%d concurrent readers observed a wrong value", bad.Load())
+ }
+ if got := tc.Load(); got != 1 {
+ t.Errorf("target resolver calls = %d, want 1 (warm populated; concurrent reads hit cache)", got)
+ }
+}
+
+// TestExtractorModelCache_TargetConcurrentMiss exercises the safety-net case
+// where many goroutines miss simultaneously. It asserts correctness (all
+// readers get the same value, no panic/deadlock) rather than an exact call
+// count, which depends on scheduling.
+func TestExtractorModelCache_TargetConcurrentMiss(t *testing.T) {
+ var tc atomic.Int32
+ cache := &extractorModelCache{
+ targets: make(map[string]extractorResolvedTarget),
+ ctxLens: make(map[string]int),
+ resolveTarget: func(_ context.Context, _ *gorm.DB, llmID string) (string, string, string, string, error) {
+ tc.Add(1)
+ return "d", llmID, "k", "u", nil
+ },
+ }
+
+ const readers = 64
+ var wg sync.WaitGroup
+ wg.Add(readers)
+ var bad atomic.Int32
+ for i := 0; i < readers; i++ {
+ go func() {
+ defer wg.Done()
+ d, m, k, u, err := cache.target(t.Context(), nil, "x")
+ if err != nil || d != "d" || m != "x" || k != "k" || u != "u" {
+ bad.Add(1)
+ }
+ }()
+ }
+ wg.Wait()
+
+ if bad.Load() != 0 {
+ t.Errorf("%d concurrent readers observed a wrong value", bad.Load())
+ }
+ if got := tc.Load(); got < 1 {
+ t.Errorf("target resolver calls = %d, want >= 1", got)
+ }
+}
+
+// TestExtractorModelCache_CtxLenCaches verifies the context length is resolved
+// once and reused, including the zero (unknown model) result which is valid.
+func TestExtractorModelCache_CtxLenCaches(t *testing.T) {
+ var cc atomic.Int32
+ cache := &extractorModelCache{
+ targets: make(map[string]extractorResolvedTarget),
+ ctxLens: make(map[string]int),
+ resolveCtxLen: func(_ context.Context, _ *gorm.DB, _ string) int {
+ cc.Add(1)
+ return 0
+ },
+ }
+ for i := 0; i < 5; i++ {
+ if v := cache.ctxLen(t.Context(), nil, "x"); v != 0 {
+ t.Fatalf("ctxLen = %d, want 0", v)
+ }
+ }
+ if got := cc.Load(); got != 1 {
+ t.Errorf("ctxLen resolver calls = %d, want 1 (zero result is cached)", got)
+ }
+}