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

+119 -2
View File
@@ -21,8 +21,31 @@ var (
// ErrEmailNotVerified reports an account whose current email is not confirmed.
ErrEmailNotVerified = errors.New("candidate profile requires a confirmed email")
// ErrRepositoryChecksTooSoon reports a check request inside the
// per-candidate interval; RepositoryCheckLimitError carries the wait.
ErrRepositoryChecksTooSoon = errors.New("repository checks were requested too recently")
)
// RepositoryCheckLimitError is ErrRepositoryChecksTooSoon with the time
// until the next request is allowed.
type RepositoryCheckLimitError struct {
RetryAfter time.Duration
}
func (err *RepositoryCheckLimitError) Error() string { return ErrRepositoryChecksTooSoon.Error() }
func (err *RepositoryCheckLimitError) Unwrap() error { return ErrRepositoryChecksTooSoon }
// SemanticTrigger schedules the background work that a profile change needs:
// the vector refresh of the card and the checks of linked repositories. A
// nil trigger schedules nothing.
type SemanticTrigger interface {
CandidateChanged(ctx context.Context, unit outbound.UnitOfWork, candidateID uuid.UUID, at time.Time) error
RepositoriesChanged(ctx context.Context, unit outbound.UnitOfWork, candidateID uuid.UUID, previous, current []domain.GitHubRepository, at time.Time) error
RequestChecks(ctx context.Context, unit outbound.UnitOfWork, candidateID uuid.UUID, repositories []domain.GitHubRepository, at time.Time) error
}
// View is the profile as the candidate reads it: the saved document and the
// categories that assessment results confirmed. The categories are derived by
// the server, are read-only, and are never part of the saved document.
@@ -31,6 +54,9 @@ type View struct {
// ConfirmedCategories holds at most one category per specialization, in
// catalog order.
ConfirmedCategories []domain.CandidateAssessmentCategory
// RepositoryChecks are the server checks of the linked repositories, by
// repository key.
RepositoryChecks map[string]outbound.RepositoryCheck
}
// Service is stateless between calls. Profile data, its history, and onboarding
@@ -38,8 +64,15 @@ type View struct {
type Service struct {
transactor outbound.Transactor
clock func() time.Time
semantic SemanticTrigger
// checkInterval is the shortest time between two check requests of one
// candidate through RequestRepositoryChecks.
checkInterval time.Duration
}
// defaultRepositoryCheckInterval applies until WithSemantic sets another.
const defaultRepositoryCheckInterval = 10 * time.Minute
// NewService takes the transaction boundary and application clock. A nil
// dependency is a wiring error and panics at construction.
func NewService(transactor outbound.Transactor, clock func() time.Time) *Service {
@@ -49,7 +82,16 @@ func NewService(transactor outbound.Transactor, clock func() time.Time) *Service
if clock == nil {
panic("profile.NewService - clock must not be nil")
}
return &Service{transactor: transactor, clock: clock}
return &Service{transactor: transactor, clock: clock, checkInterval: defaultRepositoryCheckInterval}
}
// WithSemantic sets the background trigger and the check request interval.
func (service *Service) WithSemantic(trigger SemanticTrigger, checkInterval time.Duration) *Service {
service.semantic = trigger
if checkInterval > 0 {
service.checkInterval = checkInterval
}
return service
}
// GetMyProfile reads only the profile owned by userID, with the categories
@@ -114,6 +156,9 @@ func (service *Service) SaveMyProfile(
if err != nil {
return fmt.Errorf("%s - save profile: %w", location, err)
}
if err := service.scheduleBackground(ctx, unit, userID, current, updated, at); err != nil {
return fmt.Errorf("%s - %w", location, err)
}
}
if err := repository.AdvanceOnboarding(ctx, userID, at); err != nil {
return fmt.Errorf("%s - advance onboarding: %w", location, err)
@@ -130,6 +175,68 @@ func (service *Service) SaveMyProfile(
return result, nil
}
// scheduleBackground schedules the card vector refresh and the checks of
// newly linked repositories in the save transaction, so they exist exactly
// when the change committed and the save never waits for them.
func (service *Service) scheduleBackground(
ctx context.Context, unit outbound.UnitOfWork, userID uuid.UUID, previous, updated domain.CandidateProfile, at time.Time,
) error {
if service.semantic == nil {
return nil
}
if err := service.semantic.RepositoriesChanged(ctx, unit, userID,
previous.Content().ProjectRepositories(), updated.Content().ProjectRepositories(), at); err != nil {
return fmt.Errorf("schedule repository checks: %w", err)
}
if err := service.semantic.CandidateChanged(ctx, unit, userID, at); err != nil {
return fmt.Errorf("schedule vector refresh: %w", err)
}
return nil
}
// RequestRepositoryChecks schedules a new check of every repository that the
// current profile links. A candidate may ask once per check interval; an
// earlier request answers RepositoryCheckLimitError and schedules nothing.
func (service *Service) RequestRepositoryChecks(ctx context.Context, userID uuid.UUID) (int, error) {
const location = "Service.RequestRepositoryChecks"
if userID == uuid.Nil {
return 0, fmt.Errorf("%s - user ID must be set: %w", location, domain.ErrInvalidCandidateProfile)
}
at := service.clock().UTC()
var scheduled int
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
if err := requireCandidate(ctx, unit, userID, location); err != nil {
return err
}
profile, err := unit.CandidateProfiles().Get(ctx, userID)
if err != nil {
return fmt.Errorf("%s - read profile: %w", location, err)
}
repositories := profile.Content().ProjectRepositories()
if len(repositories) == 0 {
return nil
}
latest, err := unit.ProjectRepositories().LatestRequest(ctx, userID)
if err != nil {
return fmt.Errorf("%s - read latest request: %w", location, err)
}
if next := latest.Add(service.checkInterval); !latest.IsZero() && at.Before(next) {
return &RepositoryCheckLimitError{RetryAfter: next.Sub(at)}
}
if service.semantic != nil {
if err := service.semantic.RequestChecks(ctx, unit, userID, repositories, at); err != nil {
return fmt.Errorf("%s - schedule checks: %w", location, err)
}
}
scheduled = len(repositories)
return nil
})
if err != nil {
return 0, fmt.Errorf("%s - transaction: %w", location, err)
}
return scheduled, nil
}
// readView attaches the confirmed categories of the candidate to a profile
// that was read or written in the same transaction.
func readView(
@@ -143,7 +250,17 @@ func readView(
if err != nil {
return View{}, fmt.Errorf("%s - read confirmed categories: %w", location, err)
}
return View{Profile: profile, ConfirmedCategories: inCatalogOrder(categories)}, nil
checks := make(map[string]outbound.RepositoryCheck)
if len(profile.Content().ProjectRepositories()) > 0 {
stored, err := unit.ProjectRepositories().List(ctx, userID)
if err != nil {
return View{}, fmt.Errorf("%s - read repository checks: %w", location, err)
}
for _, check := range stored {
checks[check.Key] = check
}
}
return View{Profile: profile, ConfirmedCategories: inCatalogOrder(categories), RepositoryChecks: checks}, nil
}
// inCatalogOrder sorts the categories by their place in the catalog. A