feat(matching): rank with semantic embeddings and explain matches by evidence
This commit is contained in:
1 parent
facc1789b1
commit
24b64d698a
215 files changed
+16951
-327
No files matched your search
@@ -1,5 +1,6 @@
|
||||
// Package policypublish implements the local operator command that adds the
|
||||
// demo matching section to the product policy. Until an administrative policy
|
||||
// demo matching section, or the semantic matching 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
|
||||
@@ -20,24 +21,38 @@ import (
|
||||
"lincodex/backend/internal/usecase/policy"
|
||||
)
|
||||
|
||||
const usage = "usage: policy-publish matching --actor operator:<handle>"
|
||||
const usage = "usage: policy-publish matching|semantic --actor operator:<handle>"
|
||||
|
||||
// Track names which section a command publishes.
|
||||
type Track string
|
||||
|
||||
const (
|
||||
// TrackMatching adds the PRD matching section (Sem weight 0.10).
|
||||
TrackMatching Track = "matching"
|
||||
// TrackSemantic adds the semantic matching section (Sem weight 0.5, the
|
||||
// encoder identity, the calibration, and the explanation thresholds).
|
||||
TrackSemantic Track = "semantic"
|
||||
)
|
||||
|
||||
// Command is a parsed, validated request.
|
||||
type Command struct {
|
||||
Track Track
|
||||
Author domain.PolicyAuthor
|
||||
}
|
||||
|
||||
// Extender publishes the demo matching section.
|
||||
type Extender interface {
|
||||
EnsureDemoMatchingSection(ctx context.Context, author domain.PolicyAuthor) (policy.MatchingResult, error)
|
||||
EnsureDemoSemanticMatchingSection(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] != "matching" {
|
||||
if len(args) == 0 || (args[0] != string(TrackMatching) && args[0] != string(TrackSemantic)) {
|
||||
return Command{}, errors.New(usage)
|
||||
}
|
||||
flags := flag.NewFlagSet("policy-publish matching", flag.ContinueOnError)
|
||||
track := Track(args[0])
|
||||
flags := flag.NewFlagSet("policy-publish "+args[0], flag.ContinueOnError)
|
||||
flags.SetOutput(io.Discard)
|
||||
actor := flags.String("actor", "", "operator handle")
|
||||
if err := flags.Parse(args[1:]); err != nil || flags.NArg() != 0 {
|
||||
@@ -48,7 +63,7 @@ func Parse(args []string) (Command, error) {
|
||||
if err != nil || !strings.HasPrefix(value, "operator:") {
|
||||
return Command{}, errors.New("actor must be operator:<handle>")
|
||||
}
|
||||
return Command{Author: author}, nil
|
||||
return Command{Track: track, Author: author}, nil
|
||||
}
|
||||
|
||||
// Run parses the command, loads only DATABASE_URL from the supplied
|
||||
@@ -81,7 +96,14 @@ func Execute(ctx context.Context, command Command, extender Extender, output io.
|
||||
if extender == nil || output == nil {
|
||||
return errors.New("policy publication command dependencies are required")
|
||||
}
|
||||
result, err := extender.EnsureDemoMatchingSection(ctx, command.Author)
|
||||
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 {
|
||||
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"
|
||||
}
|
||||
result, err := publish(ctx, command.Author)
|
||||
if err != nil {
|
||||
return safeOperationError(err)
|
||||
}
|
||||
@@ -89,11 +111,7 @@ func Execute(ctx context.Context, command Command, extender Extender, output io.
|
||||
if result.Created {
|
||||
status = "published"
|
||||
}
|
||||
if _, err := fmt.Fprintf(output,
|
||||
"%s: product policy version %d carries the demo matching section\n"+
|
||||
"note: demonstration values, not calibrated; they apply to new searches at once and change no stored result\n",
|
||||
status, result.Version.Version(),
|
||||
); err != nil {
|
||||
if _, err := fmt.Fprintf(output, "%s: product policy version %d carries %s\n%s", status, result.Version.Version(), section, note); err != nil {
|
||||
return errors.New("could not write the result")
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -13,9 +13,10 @@ import (
|
||||
)
|
||||
|
||||
type extenderFake struct {
|
||||
result policy.MatchingResult
|
||||
err error
|
||||
author domain.PolicyAuthor
|
||||
result policy.MatchingResult
|
||||
err error
|
||||
author domain.PolicyAuthor
|
||||
semantic bool
|
||||
}
|
||||
|
||||
func (extender *extenderFake) EnsureDemoMatchingSection(_ context.Context, author domain.PolicyAuthor) (policy.MatchingResult, error) {
|
||||
@@ -23,11 +24,21 @@ func (extender *extenderFake) EnsureDemoMatchingSection(_ context.Context, autho
|
||||
return extender.result, extender.err
|
||||
}
|
||||
|
||||
func (extender *extenderFake) EnsureDemoSemanticMatchingSection(_ context.Context, author domain.PolicyAuthor) (policy.MatchingResult, error) {
|
||||
extender.author = author
|
||||
extender.semantic = 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" {
|
||||
if err != nil || command.Author != "operator:night" || command.Track != TrackMatching {
|
||||
t.Fatalf("Parse(valid) = %+v, %v", command, err)
|
||||
}
|
||||
semantic, err := Parse([]string{"semantic", "--actor", "operator:night"})
|
||||
if err != nil || semantic.Track != TrackSemantic {
|
||||
t.Fatalf("Parse(semantic) = %+v, %v", semantic, err)
|
||||
}
|
||||
for _, args := range [][]string{
|
||||
nil, {"list"}, {"matching"}, {"matching", "--actor", "alice"}, {"matching", "--actor", "system:demo-seed"},
|
||||
{"matching", "--actor", "operator:night", "extra"},
|
||||
@@ -62,3 +73,14 @@ func TestExecutePrintsOnlyTheVersionAndMapsFailuresToSafeMessages(t *testing.T)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteSemanticTrackPublishesTheSemanticSection(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
extender := &extenderFake{}
|
||||
if err := Execute(context.Background(), Command{Track: TrackSemantic, Author: "operator:night"}, extender, &output); err != nil {
|
||||
t.Fatalf("Execute() error = %v", err)
|
||||
}
|
||||
if !extender.semantic || !strings.Contains(output.String(), "the semantic matching section") {
|
||||
t.Fatalf("output = %q, semantic called %v", output.String(), extender.semantic)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user