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

@@ -1,6 +1,6 @@
// Package policypublish implements the local operator command that adds the
// demo matching section, or the semantic matching section, to the product
// policy. Until an administrative policy
// demo matching section, the semantic matching section, or the two-model
// semantic ensemble section to the product policy. Until an administrative policy
// editor exists, this is how a stand gets the coefficients that the match
// search needs: startup seeds only the first demo version.
package policypublish
@@ -21,7 +21,7 @@ import (
"lincodex/backend/internal/usecase/policy"
)
const usage = "usage: policy-publish matching|semantic --actor operator:<handle>"
const usage = "usage: policy-publish matching|semantic|semantic-ensemble --actor operator:<handle>"
// Track names which section a command publishes.
type Track string
@@ -32,6 +32,10 @@ const (
// TrackSemantic adds the semantic matching section (Sem weight 0.5, the
// encoder identity, the calibration, and the explanation thresholds).
TrackSemantic Track = "semantic"
// TrackSemanticEnsemble replaces the semantic section with the two-model
// ensemble (Giga and Qwen3-Embedding) and its calibration; the earlier
// single-model version stays as the previous version.
TrackSemanticEnsemble Track = "semantic-ensemble"
)
// Command is a parsed, validated request.
@@ -44,11 +48,12 @@ type Command struct {
type Extender interface {
EnsureDemoMatchingSection(ctx context.Context, author domain.PolicyAuthor) (policy.MatchingResult, error)
EnsureDemoSemanticMatchingSection(ctx context.Context, author domain.PolicyAuthor) (policy.MatchingResult, error)
EnsureDemoSemanticEnsembleSection(ctx context.Context, author domain.PolicyAuthor) (policy.MatchingResult, error)
}
// Parse validates every argument before Run reads configuration or connects.
func Parse(args []string) (Command, error) {
if len(args) == 0 || (args[0] != string(TrackMatching) && args[0] != string(TrackSemantic)) {
if len(args) == 0 || (args[0] != string(TrackMatching) && args[0] != string(TrackSemantic) && args[0] != string(TrackSemanticEnsemble)) {
return Command{}, errors.New(usage)
}
track := Track(args[0])
@@ -98,10 +103,16 @@ func Execute(ctx context.Context, command Command, extender Extender, output io.
}
publish, section, note := extender.EnsureDemoMatchingSection, "the demo matching section",
"note: demonstration values, not calibrated; they apply to new searches at once and change no stored result\n"
if command.Track == TrackSemantic {
switch command.Track {
case TrackSemantic:
publish, section, note = extender.EnsureDemoSemanticMatchingSection, "the semantic matching section",
"note: research-calibrated values (LincodexMatch DEV split), not product measurements; searches use them at once,\n"+
"and Sem stays neutral for subjects without vectors until the semantic-backfill command has run\n"
case TrackSemanticEnsemble:
publish, section, note = extender.EnsureDemoSemanticEnsembleSection, "the semantic ensemble section (Giga + Qwen3-Embedding)",
"note: research-calibrated values (LincodexMatch DEV split), not product measurements; the ML service must serve both models,\n"+
"and Sem stays neutral for subjects without Qwen3 vectors until the semantic-backfill command has run\n"
case TrackMatching:
}
result, err := publish(ctx, command.Author)
if err != nil {
@@ -17,6 +17,7 @@ type extenderFake struct {
err error
author domain.PolicyAuthor
semantic bool
ensemble bool
}
func (extender *extenderFake) EnsureDemoMatchingSection(_ context.Context, author domain.PolicyAuthor) (policy.MatchingResult, error) {
@@ -30,6 +31,12 @@ func (extender *extenderFake) EnsureDemoSemanticMatchingSection(_ context.Contex
return extender.result, extender.err
}
func (extender *extenderFake) EnsureDemoSemanticEnsembleSection(_ context.Context, author domain.PolicyAuthor) (policy.MatchingResult, error) {
extender.author = author
extender.ensemble = true
return extender.result, extender.err
}
func TestParseRequiresTheMatchingTrackAndAnOperatorHandle(t *testing.T) {
command, err := Parse([]string{"matching", "--actor", "operator:night"})
if err != nil || command.Author != "operator:night" || command.Track != TrackMatching {
@@ -39,6 +46,10 @@ func TestParseRequiresTheMatchingTrackAndAnOperatorHandle(t *testing.T) {
if err != nil || semantic.Track != TrackSemantic {
t.Fatalf("Parse(semantic) = %+v, %v", semantic, err)
}
ensemble, err := Parse([]string{"semantic-ensemble", "--actor", "operator:night"})
if err != nil || ensemble.Track != TrackSemanticEnsemble {
t.Fatalf("Parse(semantic-ensemble) = %+v, %v", ensemble, err)
}
for _, args := range [][]string{
nil, {"list"}, {"matching"}, {"matching", "--actor", "alice"}, {"matching", "--actor", "system:demo-seed"},
{"matching", "--actor", "operator:night", "extra"},
@@ -84,3 +95,14 @@ func TestExecuteSemanticTrackPublishesTheSemanticSection(t *testing.T) {
t.Fatalf("output = %q, semantic called %v", output.String(), extender.semantic)
}
}
func TestExecuteSemanticEnsembleTrackPublishesTheEnsembleSection(t *testing.T) {
var output bytes.Buffer
extender := &extenderFake{}
if err := Execute(context.Background(), Command{Track: TrackSemanticEnsemble, Author: "operator:night"}, extender, &output); err != nil {
t.Fatalf("Execute() error = %v", err)
}
if !extender.ensemble || extender.semantic || !strings.Contains(output.String(), "semantic ensemble section") {
t.Fatalf("output = %q, ensemble called %v", output.String(), extender.ensemble)
}
}