feat(matching): rank with semantic embeddings and explain matches by evidence

This commit is contained in:
shira committed 2026-10-11 10:39:46 +03:00
1 parent facc1789b1
commit 24b64d698a
215 files changed
+16951 -327

No files matched your search

+126
View File
@@ -0,0 +1,126 @@
package config
import (
"errors"
"net/url"
"strings"
"time"
)
const (
// Semantic matching is off by default: a stand without the ML service
// ranks with the neutral Sem, as before.
defaultSemanticMatchingEnabled = false
// defaultMLRequestTimeout bounds one embedding request. A whole card is at
// most a dozen texts; on four CPU threads each takes well under a second.
defaultMLRequestTimeout = 30 * time.Second
maxMLRequestTimeout = 5 * time.Minute
// After this many consecutive failures the client stops calling the ML
// service for the open duration, so a dead service costs no timeouts.
defaultMLCircuitFailures = 3
maxMLCircuitFailures = 100
defaultMLCircuitOpen = 30 * time.Second
maxMLCircuitOpen = time.Hour
// Repository checks call api.github.com; they are off unless enabled.
defaultGitHubChecksEnabled = false
defaultGitHubTimeout = 10 * time.Second
maxGitHubTimeout = time.Minute
// defaultRepositoryCheckInterval is the shortest time between two check
// requests of one candidate through the refresh operation.
defaultRepositoryCheckInterval = 10 * time.Minute
maxRepositoryCheckInterval = 24 * time.Hour
)
// SemanticSettings configure semantic matching and repository checks.
type SemanticSettings struct {
// SemanticMatchingEnabled schedules vector jobs on changes and runs them
// against MLServiceURL. The search reads stored vectors either way.
SemanticMatchingEnabled bool
MLServiceURL string
MLRequestTimeout time.Duration
MLCircuitFailures int
MLCircuitOpen time.Duration
// GitHubChecksEnabled schedules and runs repository checks against the
// public GitHub API.
GitHubChecksEnabled bool
// GitHubToken is an optional read-only token that raises the GitHub rate
// limit. It is a secret: never log it.
GitHubToken string
GitHubTimeout time.Duration
RepositoryCheckInterval time.Duration
}
// LoadSemanticSettings reads the semantic settings; the operator commands
// that need only them use it directly.
func LoadSemanticSettings(lookup LookupEnv) (SemanticSettings, error) {
settings := SemanticSettings{
MLServiceURL: strings.TrimSpace(valueOrDefault(lookup, "ML_SERVICE_URL", "")),
GitHubToken: strings.TrimSpace(valueOrDefault(lookup, "GITHUB_TOKEN", "")),
}
var err error
if settings.SemanticMatchingEnabled, err = boolOrDefault(lookup, "SEMANTIC_MATCHING_ENABLED", defaultSemanticMatchingEnabled); err != nil {
return SemanticSettings{}, err
}
if settings.MLRequestTimeout, err = durationOrDefault(lookup, "ML_REQUEST_TIMEOUT", defaultMLRequestTimeout); err != nil {
return SemanticSettings{}, err
}
if settings.MLCircuitFailures, err = intOrDefault(lookup, "ML_CIRCUIT_FAILURES", defaultMLCircuitFailures); err != nil {
return SemanticSettings{}, err
}
if settings.MLCircuitOpen, err = durationOrDefault(lookup, "ML_CIRCUIT_OPEN", defaultMLCircuitOpen); err != nil {
return SemanticSettings{}, err
}
if settings.GitHubChecksEnabled, err = boolOrDefault(lookup, "GITHUB_REPOSITORY_CHECKS_ENABLED", defaultGitHubChecksEnabled); err != nil {
return SemanticSettings{}, err
}
if settings.GitHubTimeout, err = durationOrDefault(lookup, "GITHUB_TIMEOUT", defaultGitHubTimeout); err != nil {
return SemanticSettings{}, err
}
if settings.RepositoryCheckInterval, err = durationOrDefault(lookup, "REPOSITORY_CHECK_INTERVAL", defaultRepositoryCheckInterval); err != nil {
return SemanticSettings{}, err
}
return settings, settings.Validate()
}
// Validate checks the values even while the features are off, so enabling
// them later cannot surface an old mistake.
func (settings SemanticSettings) Validate() error {
if err := settings.validateMLService(); err != nil {
return err
}
return settings.validateLimits()
}
func (settings SemanticSettings) validateMLService() error {
const location = "SemanticSettings.Validate"
if settings.SemanticMatchingEnabled && settings.MLServiceURL == "" {
return errors.New(location + " - ML_SERVICE_URL is required when SEMANTIC_MATCHING_ENABLED is true")
}
if settings.MLServiceURL != "" {
parsed, err := url.Parse(settings.MLServiceURL)
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Host == "" ||
parsed.User != nil || parsed.RawQuery != "" || parsed.Fragment != "" || strings.Trim(parsed.Path, "/") != "" {
return errors.New(location + " - ML_SERVICE_URL must be an http or https origin without credentials or path")
}
}
return nil
}
func (settings SemanticSettings) validateLimits() error {
const location = "SemanticSettings.Validate"
switch {
case settings.MLRequestTimeout <= 0 || settings.MLRequestTimeout > maxMLRequestTimeout:
return errors.New(location + " - ML_REQUEST_TIMEOUT must be positive and at most 5m")
case settings.MLCircuitFailures < 1 || settings.MLCircuitFailures > maxMLCircuitFailures:
return errors.New(location + " - ML_CIRCUIT_FAILURES must be between 1 and 100")
case settings.MLCircuitOpen <= 0 || settings.MLCircuitOpen > maxMLCircuitOpen:
return errors.New(location + " - ML_CIRCUIT_OPEN must be positive and at most 1h")
case settings.GitHubTimeout <= 0 || settings.GitHubTimeout > maxGitHubTimeout:
return errors.New(location + " - GITHUB_TIMEOUT must be positive and at most 1m")
case settings.RepositoryCheckInterval <= 0 || settings.RepositoryCheckInterval > maxRepositoryCheckInterval:
return errors.New(location + " - REPOSITORY_CHECK_INTERVAL must be positive and at most 24h")
case strings.ContainsAny(settings.GitHubToken, " \t\r\n"):
return errors.New(location + " - GITHUB_TOKEN must not contain white space")
}
return nil
}