From b38ecee4da4fd775fc36b29b6443a11abe74d2dc Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Fri, 19 Jun 2026 14:01:43 +0100 Subject: [PATCH] refactor(embedding): drop init(), build default registry eagerly Replace the import-time init() that mutated the DefaultRegistry global with a constructor that returns a ready registry, removing mutate-after-init global state. Exported API unchanged. --- internal/embedding/model.go | 20 +++++++++++++++++++- internal/embedding/service.go | 11 ----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/internal/embedding/model.go b/internal/embedding/model.go index c30caa2..8d85b37 100644 --- a/internal/embedding/model.go +++ b/internal/embedding/model.go @@ -138,7 +138,25 @@ func (r *ModelRegistry) List() []ModelMetadata { } // DefaultRegistry is the global model registry with all available models. -var DefaultRegistry = NewModelRegistry() +// It is constructed eagerly at package load via newDefaultRegistry, which +// registers the built-in BGE model — replacing the previous init()-based +// mutate-after-init pattern while preserving identical behaviour. +var DefaultRegistry = newDefaultRegistry() + +// newDefaultRegistry builds a model registry pre-populated with the built-in +// models (currently BGE). Registering at construction time keeps DefaultRegistry +// ready-to-use without a separate init() mutating it after package load. +func newDefaultRegistry() *ModelRegistry { + r := NewModelRegistry() + r.Register(ModelMetadata{ + Name: BGEModelName, + Version: BGEModelVersion, + Dimensions: EmbeddingDim, + Description: "High-quality semantic search model", + Default: true, + }, newBGEModel) + return r +} // RegisterModel adds a model to the default registry. func RegisterModel(meta ModelMetadata, factory ModelFactory) { diff --git a/internal/embedding/service.go b/internal/embedding/service.go index 66b2fa9..7ecb0a2 100644 --- a/internal/embedding/service.go +++ b/internal/embedding/service.go @@ -562,17 +562,6 @@ func (m *bgeModel) Close() error { return nil } -// Register the BGE model with the default registry at init time -func init() { - RegisterModel(ModelMetadata{ - Name: BGEModelName, - Version: BGEModelVersion, - Dimensions: EmbeddingDim, - Description: "High-quality semantic search model", - Default: true, - }, newBGEModel) -} - // Service provides thread-safe text embedding generation with model abstraction. type Service struct { model EmbeddingModel