Files
LocalAI/core/config/header_cache_test.go
mudler-agentandEttore Di Giacinto c7ce0c602c fix: stop listing the models directory on request paths and cache GGUF header reads (#12683)
* fix(http): stop listing the models directory on embeddings and bearer requests

Every embeddings request read the whole models directory before the body
was parsed, even when the body named its model. On a network-backed
models directory this cost seconds per request.

Embeddings now binds the body first and looks up a default model only
when the body has none, like chat. The pre-bind default lookup and the
gpt-4o constant default are gone from that route.

The first-available default lookup skips the directory when the filter
cannot accept an unconfigured file. Every usecase filter rejects files
without a config, so the scan never added results for them. Match-all,
nil and name filters keep the scan.

The bearer token check is now an exact test: a configured name, or one
stat of that name in the models directory. The value is a literal, not a
regular expression, and a name that is not a local relative path never
matches a file.

Assisted-by: Claude Code:claude-haiku-5-5

* perf(config): cache GGUF header reads for model defaults

Model defaults re-parsed the GGUF header on every SetDefaults call, which
runs per request and at startup reload. Put a bounded LRU in front of the
parser, keyed by absolute path and validated by size and modification
time. The cached value is a snapshot of the fields the defaults use, not
the parser output.

A changed size or mtime is a miss. Missing files and parse errors are not
cached, so the existing fallback is unchanged. LOCALAI_DISABLE_GUESSING,
the empty-path guard, the alias early return, the panic recovery and the
context-size default are all kept in front of the cache.

Assisted-by: Claude:claude-haiku-5-5

* test(http): cover listing-free default lookup on every usecase route

Assisted-by: Claude Code:claude-haiku-5-5

* feat(config): persist the GGUF header cache per instance

The header cache kept parsed GGUF results only in memory, so every restart
parsed each model file again before its defaults were known. Write the cache
to a per-instance file in the data path and load it at startup. A restarted
frontend with a fixed instance ID starts warm.

The file is versioned and records the estimator inputs (gguf-parser version,
candidate contexts, auto context size, offload count). A mismatch on any of
them makes the file ignored. Each entry keeps path, size and mtime, so a
changed model file is a miss on load. Writes run on one background saver,
debounced, through a temp file and rename. The entry bound applies to the
file as well. Without a data path the cache stays in memory only.

Documented as a per-instance exception in the model configuration docs.

Assisted-by: Claude:claude-haiku-5-5

* docs(embeddings): document model selection and the no-model error

Assisted-by: Claude Code:claude-haiku-5-5

* refactor(config): split the header cache into a generic cache

The GGUF header cache mixed two things: the mechanics (bounded LRU keyed
by path and identity, per-instance file, debounced atomic save, tolerant
load) and the GGUF fields. Move the mechanics into headerCache[S], driven
by a headerCacheSpec[S] that names the file prefix, schema, fingerprint,
parser and the snapshot encode/decode pair. The GGUF cache becomes one
instance of it.

File names, the identity check, LRU size, debounce, atomic write and the
corruption handling are unchanged. The on-disk JSON keeps the "estimator"
key so existing files still load.

Assisted-by: Claude Code:claude-haiku-5-5

* feat(config): share one GGUF header cache file between frontends

Every frontend used to write its own header cache file, named after its
instance ID, so a header read by one frontend was never reused by another.
The cache now uses one file, header-cache.json, in the data path, for all
frontends and for standalone mode. The instance ID no longer changes the
file name.

Frontends share the file in three ways. A save first merges the file's
entries in, so one frontend's write does not drop another's entries. A
miss reloads the file when its modification time has changed, and a hit
never reads it. Writes go to a temp file that is renamed into place, so a
reader never sees a partial file.

Two frontends saving at the same moment can still drop each other's last
entries. That costs one re-parse, never a wrong value, because every
entry is checked against its model file before use.

The shared file is valid only when every frontend mounts the same data
volume. The distributed-mode and model-configuration docs state this mode
and its volume limit.

Assisted-by: Claude:claude-haiku-5-5

* feat(config): give an instance ID its own header cache file

Assisted-by: Claude Code:claude-haiku-5-5

* style(config): gofmt the distributed config struct

Assisted-by: Claude Code:claude-haiku-5-5

---------

Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
2026-10-14 01:06:14 +02:00

347 lines
11 KiB
Go

// SPDX-License-Identifier: MIT
package config
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
// fakeSnap is a snapshot of a made-up format. The generic cache must work with
// it exactly as it does with the GGUF snapshot, without knowing its fields.
type fakeSnap struct {
Name string `json:"name"`
Bytes int64 `json:"bytes"`
}
// fakeSpecWith returns a spec whose parser counts its calls through parses.
func fakeSpecWith(parses *int32, fingerprint string, schema int) headerCacheSpec[fakeSnap] {
return headerCacheSpec[fakeSnap]{
name: "fake header cache",
schema: schema,
fingerprint: fingerprint,
parse: func(path string) (fakeSnap, error) {
*parses++
info, err := os.Stat(path)
if err != nil {
return fakeSnap{}, err
}
return fakeSnap{Name: filepath.Base(path), Bytes: info.Size()}, nil
},
encode: func(s fakeSnap) any { return s },
decode: func(raw json.RawMessage) (fakeSnap, error) {
var s fakeSnap
err := json.Unmarshal(raw, &s)
return s, err
},
}
}
// newFakeCache returns a cache for the fake format attached to dataPath, the
// way startup attaches the GGUF cache. The saver is stopped in cleanup.
func newFakeCache(dataPath string, spec headerCacheSpec[fakeSnap]) *headerCache[fakeSnap] {
c := newHeaderCache(spec, 8)
c.attachDisk(context.Background(), dataPath, "")
DeferCleanup(c.stopPersist)
return c
}
func writeFakeModel(path string, size int) {
ExpectWithOffset(1, os.WriteFile(path, make([]byte, size), 0o600)).To(Succeed())
}
func fakeCacheFile(dataPath string) string {
return headerCachePath(dataPath, "")
}
var _ = Describe("Generic header cache", func() {
var parses int32
BeforeEach(func() {
parses = 0
// Each test flushes explicitly, so the timer-driven saver stays out of the way.
prev := headerCacheSaveDebounce
headerCacheSaveDebounce = time.Hour
DeferCleanup(func() { headerCacheSaveDebounce = prev })
})
It("serves a snapshot from the file after a restart without parsing", func() {
dir := GinkgoT().TempDir()
model := filepath.Join(dir, "model.bin")
writeFakeModel(model, 42)
first := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
snap, err := first.load(model)
Expect(err).NotTo(HaveOccurred())
Expect(snap).To(Equal(fakeSnap{Name: "model.bin", Bytes: 42}))
Expect(parses).To(Equal(int32(1)))
Expect(first.flush()).To(Succeed())
first.stopPersist()
restarted := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
Expect(restarted.len()).To(Equal(1))
again, err := restarted.load(model)
Expect(err).NotTo(HaveOccurred())
Expect(again).To(Equal(snap))
Expect(parses).To(Equal(int32(1)), "the restarted cache must not parse again")
})
It("ignores a file whose schema version differs", func() {
dir := GinkgoT().TempDir()
model := filepath.Join(dir, "model.bin")
writeFakeModel(model, 8)
c := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
_, _ = c.load(model)
Expect(c.flush()).To(Succeed())
c.stopPersist()
other := newFakeCache(dir, fakeSpecWith(&parses, "v1", 2))
Expect(other.len()).To(Equal(0), "a file with another schema must be ignored")
})
It("ignores a file whose format fingerprint differs", func() {
dir := GinkgoT().TempDir()
model := filepath.Join(dir, "model.bin")
writeFakeModel(model, 8)
c := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
_, _ = c.load(model)
Expect(c.flush()).To(Succeed())
c.stopPersist()
other := newFakeCache(dir, fakeSpecWith(&parses, "v2", 1))
Expect(other.len()).To(Equal(0), "a file with another fingerprint must be ignored")
})
It("misses when the file identity no longer matches", func() {
dir := GinkgoT().TempDir()
model := filepath.Join(dir, "model.bin")
writeFakeModel(model, 8)
c := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
_, _ = c.load(model)
Expect(parses).To(Equal(int32(1)))
writeFakeModel(model, 16)
snap, err := c.load(model)
Expect(err).NotTo(HaveOccurred())
Expect(parses).To(Equal(int32(2)), "a changed size must be a miss")
Expect(snap.Bytes).To(Equal(int64(16)))
})
It("ignores a corrupt file without panicking", func() {
dir := GinkgoT().TempDir()
model := filepath.Join(dir, "model.bin")
writeFakeModel(model, 8)
path := fakeCacheFile(dir)
for _, content := range []string{
"{not json",
`{"schema":1,"estimator":"v1","entries":[{"path":42}]}`,
`{"schema":1,"estimator":"v1","entries":[{"path":"/x","header":{"name":5}}]}`,
} {
ExpectWithOffset(1, os.WriteFile(path, []byte(content), 0o600)).To(Succeed())
var c *headerCache[fakeSnap]
Expect(func() { c = newFakeCache(dir, fakeSpecWith(&parses, "v1", 1)) }).NotTo(Panic())
Expect(c.len()).To(Equal(0), "content %q must be ignored", content)
c.stopPersist()
}
})
It("leaves no temp file behind after a write", func() {
dir := GinkgoT().TempDir()
model := filepath.Join(dir, "model.bin")
writeFakeModel(model, 8)
c := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
_, _ = c.load(model)
Expect(c.flush()).To(Succeed())
entries, err := os.ReadDir(dir)
Expect(err).NotTo(HaveOccurred())
for _, e := range entries {
Expect(strings.Contains(e.Name(), ".tmp")).To(BeFalse(), "temp file %s left behind", e.Name())
}
Expect(fakeCacheFile(dir)).To(BeAnExistingFile())
})
It("works without a data path: memory only, nothing is written", func() {
dir := GinkgoT().TempDir()
model := filepath.Join(dir, "model.bin")
writeFakeModel(model, 8)
c := newFakeCache("", fakeSpecWith(&parses, "v1", 1))
_, _ = c.load(model)
_, _ = c.load(model)
Expect(parses).To(Equal(int32(1)), "the in-memory cache still works")
Expect(c.flush()).To(Succeed())
entries, err := os.ReadDir(dir)
Expect(err).NotTo(HaveOccurred())
Expect(entries).To(HaveLen(1), "only the model file is present")
})
It("uses header-cache.json for every instance", func() {
Expect(headerCachePath("/data", "")).To(Equal(filepath.Join("/data", "header-cache.json")))
dir := GinkgoT().TempDir()
model := filepath.Join(dir, "model.bin")
writeFakeModel(model, 8)
a := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
b := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
_, _ = a.load(model)
Expect(a.flush()).To(Succeed())
Expect(b.flush()).To(Succeed())
entries, err := os.ReadDir(dir)
Expect(err).NotTo(HaveOccurred())
names := []string{}
for _, e := range entries {
names = append(names, e.Name())
}
Expect(names).To(ConsistOf("model.bin", "header-cache.json"))
})
It("serves an entry written by another instance after a reload on miss, without parsing", func() {
dir := GinkgoT().TempDir()
model := filepath.Join(dir, "model.bin")
writeFakeModel(model, 42)
reader := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
writer := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
_, err := writer.load(model)
Expect(err).NotTo(HaveOccurred())
Expect(parses).To(Equal(int32(1)))
Expect(writer.flush()).To(Succeed())
snap, err := reader.load(model)
Expect(err).NotTo(HaveOccurred())
Expect(snap).To(Equal(fakeSnap{Name: "model.bin", Bytes: 42}))
Expect(parses).To(Equal(int32(1)), "the reader must reload the file on its miss instead of parsing")
})
It("merges on save so that the file holds the union of two instances", func() {
dir := GinkgoT().TempDir()
m1 := filepath.Join(dir, "one.bin")
m2 := filepath.Join(dir, "two.bin")
writeFakeModel(m1, 1)
writeFakeModel(m2, 2)
a := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
b := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
_, _ = a.load(m1)
_, _ = b.load(m2)
Expect(a.flush()).To(Succeed())
Expect(b.flush()).To(Succeed())
file := readFakeFile(dir)
paths := []string{}
for _, e := range file.Entries {
paths = append(paths, e.Path)
}
Expect(paths).To(ConsistOf(m1, m2))
})
It("produces a valid file under concurrent saves from two instances", func() {
dir := GinkgoT().TempDir()
var models [2][3]string
for i := range models {
for j := range models[i] {
models[i][j] = filepath.Join(dir, fmt.Sprintf("m-%d-%d.bin", i, j))
writeFakeModel(models[i][j], 4+i*3+j)
}
}
// Each instance counts its own parses: the counter is not synchronized, and
// the two goroutines below run in parallel.
var parsesA, parsesB int32
insts := [2]*headerCache[fakeSnap]{
newFakeCache(dir, fakeSpecWith(&parsesA, "v1", 1)),
newFakeCache(dir, fakeSpecWith(&parsesB, "v1", 1)),
}
var wg sync.WaitGroup
errs := make(chan error, 16)
for i := range insts {
wg.Add(1)
go func(c *headerCache[fakeSnap], files [3]string) {
defer wg.Done()
for _, f := range files {
if _, err := c.load(f); err != nil {
errs <- err
return
}
if err := c.flush(); err != nil {
errs <- err
return
}
}
}(insts[i], models[i])
}
wg.Wait()
close(errs)
for err := range errs {
Expect(err).NotTo(HaveOccurred())
}
// Two racing writers may drop each other's last entries. A final pair of
// sequential saves merges everything back, because each save folds in what
// is on disk. The bound (8) is larger than the six entries, so none is evicted.
Expect(insts[0].flush()).To(Succeed())
Expect(insts[1].flush()).To(Succeed())
file := readFakeFile(dir)
Expect(file.Entries).To(HaveLen(6))
Expect(tmpFilesIn(dir)).To(BeEmpty())
})
It("ignores a corrupt or wrong-schema file and overwrites it with a valid save", func() {
dir := GinkgoT().TempDir()
model := filepath.Join(dir, "model.bin")
writeFakeModel(model, 8)
path := fakeCacheFile(dir)
for _, content := range []string{
"{not json",
`{"schema":99,"estimator":"v1","entries":[]}`,
} {
ExpectWithOffset(1, os.WriteFile(path, []byte(content), 0o600)).To(Succeed())
c := newFakeCache(dir, fakeSpecWith(&parses, "v1", 1))
Expect(c.len()).To(Equal(0))
_, err := c.load(model)
Expect(err).NotTo(HaveOccurred())
Expect(c.flush()).To(Succeed())
c.stopPersist()
file := readFakeFile(dir)
Expect(file.Schema).To(Equal(1))
Expect(file.Entries).To(HaveLen(1), "content %q must be replaced by a valid save", content)
}
})
})
// readFakeFile decodes the shared cache file in dir, failing the spec if it is not valid.
func readFakeFile(dir string) headerFile {
raw, err := os.ReadFile(fakeCacheFile(dir))
ExpectWithOffset(1, err).NotTo(HaveOccurred())
var file headerFile
ExpectWithOffset(1, json.Unmarshal(raw, &file)).To(Succeed())
return file
}
// tmpFilesIn lists temp files left in dir.
func tmpFilesIn(dir string) []string {
entries, err := os.ReadDir(dir)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
var out []string
for _, e := range entries {
if strings.Contains(e.Name(), ".tmp") {
out = append(out, e.Name())
}
}
return out
}