feat(ml): fuse two embedding models and protect the ML service with a token

This commit is contained in:
shira committed 2026-10-11 14:40:52 +03:00
1 parent bc00d6901f
commit b24e5ff906
63 files changed
+2287 -276

No files matched your search

@@ -51,6 +51,34 @@ func (publisher *Publisher) EnsureDemoSemanticMatchingSection(ctx context.Contex
domain.DemoSemanticMatchingPolicy)
}
// EnsureDemoSemanticEnsembleSection makes the latest demonstration version
// carry the two-model semantic section
// (domain.DemoSemanticEnsembleMatchingPolicy). A version whose semantic
// section already names exactly the ensemble models, in order, is left
// alone. Otherwise it publishes the next version, a copy of the latest one
// whose matching section is replaced, so the single-model section stays as
// the previous version and a rollback needs no recomputed vectors.
func (publisher *Publisher) EnsureDemoSemanticEnsembleSection(ctx context.Context, author domain.PolicyAuthor) (MatchingResult, error) {
ensemble := domain.DemoMatchingSemanticsEnsemblePolicy()
return publisher.ensureMatching(ctx, author, "Publisher.EnsureDemoSemanticEnsembleSection",
func(current *domain.MatchingPolicy) bool {
return current != nil && current.Semantics != nil && sameMemberModels(current.Semantics.Members, ensemble.Members)
},
domain.DemoSemanticEnsembleMatchingPolicy)
}
func sameMemberModels(left, right []domain.MatchingSemanticMember) bool {
if len(left) != len(right) {
return false
}
for index := range left {
if left[index].Model != right[index].Model {
return false
}
}
return true
}
func (publisher *Publisher) ensureMatching(
ctx context.Context,
author domain.PolicyAuthor,
@@ -81,3 +81,34 @@ func TestEnsureDemoMatchingSectionDoesNotRetryAfterAConcurrentPublication(t *tes
t.Fatalf("versions = %d, want only the concurrent publication stored and no built-on-stale version", len(store.versions))
}
}
func TestEnsureDemoSemanticEnsembleSectionKeepsTheSingleModelVersionBeforeIt(t *testing.T) {
store := &memoryPolicies{}
seedVersion(t, store, domain.PolicyKindDemo, domain.DemoProductPolicyV5())
publisher := NewPublisher(store, testClock)
result, err := publisher.EnsureDemoSemanticEnsembleSection(context.Background(), "operator:night")
if err != nil || !result.Created || result.Version.Version() != 2 {
t.Fatalf("EnsureDemoSemanticEnsembleSection() = %+v, %v, want version 2 created", result, err)
}
semantics := result.Version.Policy().Matching.Semantics
if semantics == nil || len(semantics.Members) != 2 ||
semantics.Members[0].Model.ID != domain.GigaEmbeddingsModelID || semantics.Members[1].Model.ID != domain.Qwen3EmbeddingModelID {
t.Fatalf("semantic members = %+v, want Giga first and Qwen3 second", semantics)
}
if previous := store.versions[0].Policy().Matching.Semantics; len(previous.Members) != 1 {
t.Fatalf("previous version members = %d, want the single-model version kept", len(previous.Members))
}
if result.Version.Policy().Assessment.Signals == nil || result.Version.Policy().Support == nil {
t.Fatal("the ensemble version dropped an earlier section")
}
again, err := publisher.EnsureDemoSemanticEnsembleSection(context.Background(), "operator:night")
if err != nil || again.Created || len(store.versions) != 2 {
t.Fatalf("second call = %+v, %v with %d versions, want no new version", again, err, len(store.versions))
}
single, err := publisher.EnsureDemoSemanticMatchingSection(context.Background(), "operator:night")
if err != nil || single.Created {
t.Fatalf("the single-model command after the ensemble = %+v, %v, want it to leave the ensemble alone", single, err)
}
}