143 lines
5.8 KiB
Go
143 lines
5.8 KiB
Go
// Package policypublish implements the local operator command that adds the
|
|
// 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
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"time"
|
|
|
|
"lincodex/backend/config"
|
|
"lincodex/backend/internal/adapter/secondary/postgres"
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
"lincodex/backend/internal/usecase/policy"
|
|
)
|
|
|
|
const usage = "usage: policy-publish matching|semantic|semantic-ensemble --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"
|
|
// 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.
|
|
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)
|
|
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) && args[0] != string(TrackSemanticEnsemble)) {
|
|
return Command{}, errors.New(usage)
|
|
}
|
|
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 {
|
|
return Command{}, errors.New(usage)
|
|
}
|
|
value := strings.TrimSpace(*actor)
|
|
author, err := domain.ParsePolicyAuthor(value)
|
|
if err != nil || !strings.HasPrefix(value, "operator:") {
|
|
return Command{}, errors.New("actor must be operator:<handle>")
|
|
}
|
|
return Command{Track: track, Author: author}, nil
|
|
}
|
|
|
|
// Run parses the command, loads only DATABASE_URL from the supplied
|
|
// environment, and publishes. It does not load dotenv files.
|
|
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("policy publication 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")
|
|
}
|
|
return Execute(ctx, command, policy.NewPublisher(postgres.NewPolicyStore(pool).Policies(), clock), output)
|
|
}
|
|
|
|
// Execute publishes the section and prints the version number only.
|
|
func Execute(ctx context.Context, command Command, extender Extender, output io.Writer) error {
|
|
if extender == nil || output == nil {
|
|
return errors.New("policy publication command dependencies are required")
|
|
}
|
|
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"
|
|
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 {
|
|
return safeOperationError(err)
|
|
}
|
|
status := "already present"
|
|
if result.Created {
|
|
status = "published"
|
|
}
|
|
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
|
|
}
|
|
|
|
func safeOperationError(err error) error {
|
|
switch {
|
|
case errors.Is(err, policy.ErrNotDemoPolicy):
|
|
return errors.New("the latest product policy is not a demonstration version; publish the matching section with the calibrated policy instead")
|
|
case errors.Is(err, policy.ErrPolicyChanged):
|
|
return errors.New("the product policy changed while publishing; run the command again")
|
|
case errors.Is(err, outbound.ErrPolicyVersionNotFound):
|
|
return errors.New("the product policy has no version yet; start the service once so that it seeds the demo policy")
|
|
default:
|
|
return errors.New("policy publication failed")
|
|
}
|
|
}
|