perf(http): skip discovery for configured requests (#12628)

Exact configured requests need no loose-file enumeration. Reuse the
synchronized config lookup without adding a cache. Misses retain regex
discovery and raw-weight fallback with ALWAYS_INCLUDE.

Keep general listing and bearer discovery unchanged. Preserve request
validation, alias resolution, disabled-target checks, and model access.
Record diagnostics only for work performed.

Assisted-by: nib:gpt-6-astra

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
mudler-agentandEttore Di Giacinto authored and GitHub committed 2026-10-11 17:38:27 +02:00
1 parent 883e4c56eb
commit 99d6b35cb5
6 files changed
+264 -2

No files matched your search

+4 -1
View File
@@ -226,7 +226,10 @@ var _ = Describe("Diagnostics extraction", func() {
Expect(ends(diagnostics.PhaseBearerLookup)).To(HaveLen(tc.lookup))
Expect(ends(diagnostics.PhaseBodyLookup)).To(HaveLen(1))
Expect(ends(diagnostics.PhaseConfigLoadDefaults)).To(HaveLen(1))
Expect(ends(diagnostics.PhaseConfigFilter)).NotTo(BeEmpty())
// Exact body lookup does not filter configs or enumerate files.
// Only the unchanged default and bearer discovery paths do.
Expect(ends(diagnostics.PhaseConfigFilter)).To(HaveLen(tc.listing + tc.lookup))
Expect(ends(diagnostics.PhaseFSEnumeration)).To(HaveLen(tc.listing + tc.lookup))
id := events[0].ID
_, err := uuid.Parse(id)
Expect(err).NotTo(HaveOccurred())
+1 -1
View File
@@ -217,7 +217,7 @@ func (re *RequestExtractor) setModelAndConfig(initializer func() schema.LocalAIR
// like diffusers may download these on the fly.
if modelName != "" && !strings.Contains(modelName, "/") {
endLookup := diagnostics.Begin(ctx, diagnostics.PhaseBodyLookup)
exists, existsErr := galleryop.CheckIfModelExistsContext(ctx, re.modelConfigLoader, re.modelLoader, modelName, galleryop.ALWAYS_INCLUDE)
exists, existsErr := galleryop.CheckRequestModelExistsContext(ctx, re.modelConfigLoader, re.modelLoader, modelName)
endLookup(diagnosticOutcome(ctx, existsErr != nil || !exists), 0)
if existsErr == nil && !exists {
return c.JSON(http.StatusNotFound, schema.ErrorResponse{
@@ -0,0 +1,47 @@
//go:build auth
// SPDX-License-Identifier: MIT
package middleware_test
import (
"fmt"
"github.com/labstack/echo/v4"
"github.com/mudler/LocalAI/core/config"
"github.com/mudler/LocalAI/core/http/auth"
. "github.com/mudler/LocalAI/core/http/middleware"
"github.com/mudler/LocalAI/core/schema"
"github.com/mudler/LocalAI/pkg/model"
"github.com/mudler/LocalAI/pkg/system"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("WarmFastPath model access", func() {
It("keeps the requested alias subject to the user's allowlist", func() {
db, err := auth.InitDB(":memory:")
Expect(err).NotTo(HaveOccurred())
sqlDB, err := db.DB()
Expect(err).NotTo(HaveOccurred())
DeferCleanup(sqlDB.Close)
user := &auth.User{ID: "warm-user", Email: "warm@example.test", Role: auth.RoleUser, Status: auth.StatusActive}
Expect(db.Create(user).Error).To(Succeed())
Expect(auth.UpdateModelAllowlist(db, user.ID, auth.ModelAllowlist{Enabled: true, Models: []string{"target"}})).To(Succeed())
dir := GinkgoT().TempDir()
ac := config.NewApplicationConfig()
ac.SystemState = &system.SystemState{Model: system.Model{ModelsPath: dir}}
bcl := config.NewModelConfigLoader(dir)
bcl.ReplaceModelConfigs([]config.ModelConfig{{Name: "target", Backend: "test"}, {Name: "default", Alias: "target"}})
re := NewRequestExtractor(bcl, model.NewModelLoader(ac.SystemState), ac)
app := echo.New()
app.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error { c.Set("auth_user", user); return next(c) }
})
app.POST("/", func(c echo.Context) error { return c.NoContent(204) }, auth.RequireModelAccess(db), re.SetModelAndConfig(func() schema.LocalAIRequest { return new(schema.OpenAIRequest) }))
for _, tc := range []struct {
name string
code int
}{{"default", 403}, {"target", 204}} {
Expect(postJSON(app, "/", fmt.Sprintf(`{"model":%q}`, tc.name)).Code).To(Equal(tc.code))
}
})
})
+99
View File
@@ -0,0 +1,99 @@
// SPDX-License-Identifier: MIT
package middleware_test
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/labstack/echo/v4"
"github.com/mudler/LocalAI/core/config"
. "github.com/mudler/LocalAI/core/http/middleware"
"github.com/mudler/LocalAI/core/schema"
"github.com/mudler/LocalAI/pkg/diagnostics"
"github.com/mudler/LocalAI/pkg/model"
"github.com/mudler/LocalAI/pkg/system"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("WarmFastPath explicit requests", func() {
var bcl *config.ModelConfigLoader
var ac *config.ApplicationConfig
var app *echo.Echo
var events []diagnostics.Event
var served string
BeforeEach(func() {
dir := GinkgoT().TempDir()
ac = config.NewApplicationConfig()
ac.Context = context.Background()
ac.SystemState = &system.SystemState{Model: system.Model{ModelsPath: dir}}
ac.DiagnosticsRecorder = diagnostics.NewRecorder(func(e diagnostics.Event) { events = append(events, e) })
bcl = config.NewModelConfigLoader(dir)
target := config.ModelConfig{Name: "target", Backend: "test"}
target.Model = "weights.gguf"
bcl.ReplaceModelConfigs([]config.ModelConfig{target, {Name: "default", Alias: "target"}, {Name: "literal[", Backend: "test"}})
for i := 0; i < 128; i++ {
Expect(os.WriteFile(filepath.Join(dir, fmt.Sprintf("unrelated-%03d", i)), nil, 0600)).To(Succeed())
}
re := NewRequestExtractor(bcl, model.NewModelLoader(ac.SystemState), ac)
app = echo.New()
served = ""
events = nil
app.POST("/", func(c echo.Context) error {
served = c.Get(CONTEXT_LOCALS_KEY_MODEL_CONFIG).(*config.ModelConfig).Name
return c.NoContent(204)
}, re.SetModelAndConfig(func() schema.LocalAIRequest { return new(schema.OpenAIRequest) }))
})
count := func(phase diagnostics.Phase) int {
n := 0
for _, e := range events {
if e.Phase == phase && e.State == diagnostics.StateEnd {
n++
}
}
return n
}
It("performs zero enumerations for exact aliases, concrete and regex-metacharacter names", func() {
for _, name := range []string{"default", "target", "literal["} {
events = nil
Expect(postJSON(app, "/", fmt.Sprintf(`{"model":%q}`, name)).Code).To(Equal(204))
if name == "default" {
Expect(served).To(Equal("target"))
} else {
Expect(served).To(Equal(name))
}
Expect(count(diagnostics.PhaseBodyLookup)).To(Equal(1))
Expect(count(diagnostics.PhaseFSEnumeration)).To(BeZero(), name)
Expect(count(diagnostics.PhaseExistenceFallback)).To(BeZero())
for _, e := range events {
Expect(e.ID).To(Equal(events[0].ID))
}
}
})
It("retains discovery on misses and does not manufacture events when diagnostics are disabled", func() {
Expect(postJSON(app, "/", `{"model":"missing"}`).Code).To(Equal(404))
Expect(count(diagnostics.PhaseFSEnumeration)).To(Equal(1))
events = nil
ac.DiagnosticsRecorder = nil
Expect(postJSON(app, "/", `{"model":"target"}`).Code).To(Equal(204))
Expect(events).To(BeEmpty())
})
It("still rejects malformed requests and aliases with missing or cyclic targets", func() {
Expect(postJSON(app, "/", `{"model":`).Code).To(Equal(400))
for _, alias := range []string{"missing", "default"} {
bcl.ReplaceModelConfigs([]config.ModelConfig{{Name: "default", Alias: alias}})
Expect(postJSON(app, "/", `{"model":"default"}`).Code).To(Equal(400))
Expect(served).To(BeEmpty())
}
})
It("still rejects disabled concrete models and alias targets", func() {
disabled := true
bcl.ReplaceModelConfigs([]config.ModelConfig{{Name: "target", Backend: "test", Disabled: &disabled}, {Name: "default", Alias: "target"}})
for _, name := range []string{"target", "default"} {
Expect(postJSON(app, "/", fmt.Sprintf(`{"model":%q}`, name)).Code).To(Equal(403))
Expect(served).To(BeEmpty())
}
})
})
+16
View File
@@ -116,3 +116,19 @@ func checkIfModelExistsContext(ctx context.Context, bcl *config.ModelConfigLoade
return false, nil
}
// CheckRequestModelExistsContext checks explicit request names literally first.
// An exact configured name needs no unrelated directory enumeration; selected
// weights still undergo normal loading checks. Misses retain legacy regex and
// loose-file discovery, including its errors and raw-weight fallback.
// General regex existence and model-listing callers should use their existing APIs.
func CheckRequestModelExistsContext(ctx context.Context, bcl *config.ModelConfigLoader, ml *model.ModelLoader, modelName string) (bool, error) {
return checkRequestModelExistsContext(ctx, bcl, ml, modelName)
}
func checkRequestModelExistsContext(ctx context.Context, bcl *config.ModelConfigLoader, ml discoveryFiles, modelName string) (bool, error) {
if _, exists := bcl.GetModelConfig(modelName); exists {
return true, nil
}
return checkIfModelExistsContext(ctx, bcl, ml, modelName, ALWAYS_INCLUDE)
}
@@ -0,0 +1,97 @@
// SPDX-License-Identifier: MIT
package galleryop
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/mudler/LocalAI/core/config"
"github.com/mudler/LocalAI/pkg/model"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("WarmFastPath request existence", func() {
var bcl *config.ModelConfigLoader
var ml *model.ModelLoader
BeforeEach(func() {
dir := GinkgoT().TempDir()
bcl = config.NewModelConfigLoader(dir)
ml = &model.ModelLoader{ModelPath: dir}
bcl.ReplaceModelConfigs([]config.ModelConfig{{Name: "target"}, {Name: "default", Alias: "target"}, {Name: "literal["}})
for i := 0; i < 128; i++ {
Expect(os.WriteFile(filepath.Join(dir, fmt.Sprintf("unrelated-%03d", i)), nil, 0600)).To(Succeed())
}
Expect(os.WriteFile(filepath.Join(dir, "raw.gguf"), nil, 0600)).To(Succeed())
})
It("does no discovery for exact alias, concrete or literal regex names", func() {
for _, name := range []string{"default", "target", "literal["} {
d := &diagnosticDiscovery{ml: ml}
exists, err := checkRequestModelExistsContext(context.Background(), bcl, d, name)
Expect(err).NotTo(HaveOccurred())
Expect(exists).To(BeTrue())
Expect(d.lists).To(BeZero(), name)
Expect(d.exists).To(BeZero(), name)
}
})
It("keeps missing, regex, loose-file, raw-file and malformed regex behavior", func() {
for _, name := range []string{"missing", "tar.*", "unrelated-001", "raw.gguf", "absent["} {
old, next := &diagnosticDiscovery{ml: ml}, &diagnosticDiscovery{ml: ml}
want, wantErr := checkIfModelExistsContext(context.Background(), bcl, old, name, ALWAYS_INCLUDE)
got, err := checkRequestModelExistsContext(context.Background(), bcl, next, name)
Expect(got).To(Equal(want))
Expect(fmt.Sprint(err)).To(Equal(fmt.Sprint(wantErr)))
Expect(next.lists).To(Equal(old.lists))
Expect(next.exists).To(Equal(old.exists))
}
})
It("ignores unrelated directory failure only for exact configured requests", func() {
ml.ModelPath = filepath.Join(ml.ModelPath, "missing-directory")
d := &diagnosticDiscovery{ml: ml}
exists, err := checkRequestModelExistsContext(context.Background(), bcl, d, "target")
Expect(err).NotTo(HaveOccurred())
Expect(exists).To(BeTrue())
Expect(d.lists).To(BeZero())
for _, name := range []string{"missing", "tar.*", "raw.gguf"} {
exists, err = checkRequestModelExistsContext(context.Background(), bcl, d, name)
Expect(err).To(HaveOccurred())
Expect(exists).To(BeFalse())
}
Expect(d.lists).To(Equal(3))
Expect(d.exists).To(BeZero())
})
It("leaves general regex existence and full listing unchanged", func() {
d := &diagnosticDiscovery{ml: ml}
names, err := listModelsContext(context.Background(), bcl, d, nil, ALWAYS_INCLUDE)
Expect(err).NotTo(HaveOccurred())
Expect(names).To(HaveLen(131))
Expect(names).To(ContainElements("target", "default", "unrelated-001"))
exists, err := checkIfModelExistsContext(context.Background(), bcl, d, "tar.*", ALWAYS_INCLUDE)
Expect(err).NotTo(HaveOccurred())
Expect(exists).To(BeTrue())
Expect(d.lists).To(Equal(2))
})
It("observes replacement and deletion without a stale successful hit", func() {
d := &diagnosticDiscovery{ml: ml}
exists, err := checkRequestModelExistsContext(context.Background(), bcl, d, "target")
Expect(err).NotTo(HaveOccurred())
Expect(exists).To(BeTrue())
Expect(d.lists).To(BeZero())
bcl.ReplaceModelConfigs([]config.ModelConfig{{Name: "replacement"}})
exists, err = checkRequestModelExistsContext(context.Background(), bcl, d, "target")
Expect(err).NotTo(HaveOccurred())
Expect(exists).To(BeFalse())
Expect(d.lists).To(Equal(1))
exists, err = checkRequestModelExistsContext(context.Background(), bcl, d, "replacement")
Expect(err).NotTo(HaveOccurred())
Expect(exists).To(BeTrue())
Expect(d.lists).To(Equal(1))
bcl.RemoveModelConfig("replacement")
exists, err = checkRequestModelExistsContext(context.Background(), bcl, d, "replacement")
Expect(err).NotTo(HaveOccurred())
Expect(exists).To(BeFalse())
Expect(d.lists).To(Equal(2))
})
})