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
@@ -0,0 +1,120 @@
|
||||
// Package semanticbackfill implements the operator command that schedules
|
||||
// vector jobs for existing published candidates and needs, for example after
|
||||
// semantic matching is enabled, the semantic policy is published, or the ML
|
||||
// model changes. The jobs run in the backend worker; the command only
|
||||
// schedules them and prints the resume cursor of every page.
|
||||
package semanticbackfill
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/config"
|
||||
"lincodex/backend/internal/adapter/secondary/postgres"
|
||||
"lincodex/backend/internal/usecase/semantic"
|
||||
)
|
||||
|
||||
const usage = "usage: semantic-backfill candidates|needs|all --run <label> [--after <uuid>]"
|
||||
|
||||
// Command is a parsed, validated request.
|
||||
type Command struct {
|
||||
Scopes []semantic.BackfillScope
|
||||
Run string
|
||||
After uuid.UUID
|
||||
}
|
||||
|
||||
// Runner schedules the jobs of one scope.
|
||||
type Runner interface {
|
||||
Run(ctx context.Context, scope semantic.BackfillScope, run string, after uuid.UUID, report func(semantic.BackfillProgress) error) error
|
||||
}
|
||||
|
||||
// Parse validates every argument before Run reads configuration or connects.
|
||||
func Parse(args []string) (Command, error) {
|
||||
if len(args) == 0 {
|
||||
return Command{}, errors.New(usage)
|
||||
}
|
||||
var scopes []semantic.BackfillScope
|
||||
switch args[0] {
|
||||
case string(semantic.BackfillCandidates):
|
||||
scopes = []semantic.BackfillScope{semantic.BackfillCandidates}
|
||||
case string(semantic.BackfillNeeds):
|
||||
scopes = []semantic.BackfillScope{semantic.BackfillNeeds}
|
||||
case "all":
|
||||
scopes = []semantic.BackfillScope{semantic.BackfillCandidates, semantic.BackfillNeeds}
|
||||
default:
|
||||
return Command{}, errors.New(usage)
|
||||
}
|
||||
flags := flag.NewFlagSet("semantic-backfill", flag.ContinueOnError)
|
||||
flags.SetOutput(io.Discard)
|
||||
run := flags.String("run", "", "run label; a new label recomputes again")
|
||||
after := flags.String("after", "", "resume after this identifier")
|
||||
if err := flags.Parse(args[1:]); err != nil || flags.NArg() != 0 || *run == "" {
|
||||
return Command{}, errors.New(usage)
|
||||
}
|
||||
command := Command{Scopes: scopes, Run: *run}
|
||||
if *after != "" {
|
||||
if len(scopes) != 1 {
|
||||
return Command{}, errors.New("--after needs a single scope")
|
||||
}
|
||||
parsed, err := uuid.Parse(*after)
|
||||
if err != nil {
|
||||
return Command{}, errors.New("--after must be a UUID")
|
||||
}
|
||||
command.After = parsed
|
||||
}
|
||||
return command, nil
|
||||
}
|
||||
|
||||
// Run parses the command, loads only DATABASE_URL, and schedules.
|
||||
func Run(ctx context.Context, args []string, lookup config.LookupEnv, output io.Writer, clock func() time.Time) error {
|
||||
command, err := Parse(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if lookup == nil || output == nil || clock == nil {
|
||||
return errors.New("semantic backfill command dependencies are required")
|
||||
}
|
||||
databaseURL := config.DatabaseURLFromEnv(lookup)
|
||||
if databaseURL == "" {
|
||||
return errors.New("DATABASE_URL is not set")
|
||||
}
|
||||
pool, err := postgres.NewPool(ctx, databaseURL)
|
||||
if err != nil {
|
||||
return errors.New("database configuration is invalid")
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
return errors.New("database is unavailable")
|
||||
}
|
||||
backfill, err := semantic.NewBackfill(postgres.NewTransactor(pool), clock)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Execute(ctx, command, backfill, output)
|
||||
}
|
||||
|
||||
// Execute runs every scope and prints one line per page.
|
||||
func Execute(ctx context.Context, command Command, runner Runner, output io.Writer) error {
|
||||
for _, scope := range command.Scopes {
|
||||
total := 0
|
||||
err := runner.Run(ctx, scope, command.Run, command.After, func(progress semantic.BackfillProgress) error {
|
||||
total += progress.Scheduled
|
||||
_, err := fmt.Fprintf(output, "%s: scheduled %d (total %d), resume with --after %s\n",
|
||||
progress.Scope, progress.Scheduled, total, progress.After)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return errors.New("the backfill stopped; run it again with --after of the last printed line")
|
||||
}
|
||||
if _, err := fmt.Fprintf(output, "%s: done, %d jobs scheduled; the backend worker computes them\n", scope, total); err != nil {
|
||||
return errors.New("could not write the result")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package semanticbackfill
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/usecase/semantic"
|
||||
)
|
||||
|
||||
type runnerFake struct {
|
||||
scopes []semantic.BackfillScope
|
||||
err error
|
||||
}
|
||||
|
||||
func (runner *runnerFake) Run(_ context.Context, scope semantic.BackfillScope, _ string, after uuid.UUID, report func(semantic.BackfillProgress) error) error {
|
||||
runner.scopes = append(runner.scopes, scope)
|
||||
if runner.err != nil {
|
||||
return runner.err
|
||||
}
|
||||
return report(semantic.BackfillProgress{Scope: scope, Scheduled: 2, After: after, Done: true})
|
||||
}
|
||||
|
||||
func TestParseRequiresAScopeAndARunLabel(t *testing.T) {
|
||||
command, err := Parse([]string{"all", "--run", "2026-10-11"})
|
||||
if err != nil || len(command.Scopes) != 2 || command.Run != "2026-10-11" {
|
||||
t.Fatalf("Parse(all) = %+v, %v", command, err)
|
||||
}
|
||||
after := uuid.New()
|
||||
command, err = Parse([]string{"needs", "--run", "r1", "--after", after.String()})
|
||||
if err != nil || command.After != after {
|
||||
t.Fatalf("Parse(needs --after) = %+v, %v", command, err)
|
||||
}
|
||||
for _, args := range [][]string{
|
||||
nil, {"vectors"}, {"candidates"}, {"all", "--run", "r1", "--after", after.String()},
|
||||
{"needs", "--run", "r1", "--after", "x"}, {"needs", "--run", "r1", "extra"},
|
||||
} {
|
||||
if _, err := Parse(args); err == nil {
|
||||
t.Errorf("Parse(%v) accepted invalid arguments", args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteReportsEveryScopeAndHidesStorageErrors(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
runner := &runnerFake{}
|
||||
command := Command{Scopes: []semantic.BackfillScope{semantic.BackfillCandidates, semantic.BackfillNeeds}, Run: "r1"}
|
||||
if err := Execute(context.Background(), command, runner, &output); err != nil {
|
||||
t.Fatalf("Execute() error = %v", err)
|
||||
}
|
||||
if len(runner.scopes) != 2 || !strings.Contains(output.String(), "needs: done, 2 jobs scheduled") {
|
||||
t.Fatalf("output = %q", output.String())
|
||||
}
|
||||
failing := &runnerFake{err: errors.New("pq: password authentication failed for user x")}
|
||||
err := Execute(context.Background(), command, failing, &bytes.Buffer{})
|
||||
if err == nil || strings.Contains(err.Error(), "password") {
|
||||
t.Fatalf("Execute(failing) error = %v, want a safe message", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user