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

@@ -219,6 +219,7 @@ func matchConfirmedCandidate(entry matching.Entry) api.MatchConfirmedCandidate {
ConfirmationStatus: api.MatchConfirmedCandidateConfirmationStatusConfirmed,
PublicContext: matchPublicContext(entry.PublicContext),
EvidenceSummary: matchEvidenceSummary(entry.EvidenceSummary),
SemanticEvidence: matchSemanticEvidence(entry.SemanticEvidence),
UnknownFlags: matchUnknownFlags(entry.UnknownFlags),
}
candidate.DeclaredGrade = matchDeclaredGrade(entry.DeclaredGrade)
@@ -251,6 +252,7 @@ func matchUnconfirmedCandidate(entry matching.Entry) api.MatchUnconfirmedCandida
DeclaredGrade: matchDeclaredGrade(entry.DeclaredGrade),
PublicContext: matchPublicContext(entry.PublicContext),
EvidenceSummary: matchEvidenceSummary(entry.EvidenceSummary),
SemanticEvidence: matchSemanticEvidence(entry.SemanticEvidence),
Components: api.MatchUnconfirmedComponents{Fit: entry.Components.Fit, Coverage: entry.Components.Coverage},
UnknownFlags: matchUnknownFlags(entry.UnknownFlags),
}
@@ -300,6 +302,24 @@ func matchEvidenceSummary(items []matching.EvidenceItem) api.MatchEvidenceSummar
return summary
}
// matchSemanticEvidence maps the computed explanation. The list is never
// null, so a client tells "nothing to explain" from a missing field.
func matchSemanticEvidence(items []matching.SemanticEvidence) api.MatchSemanticEvidenceList {
evidence := make(api.MatchSemanticEvidenceList, 0, len(items))
for _, item := range items {
title := api.NilString{Null: true}
if item.ProjectTitle != nil {
title = api.NewNilString(*item.ProjectTitle)
}
evidence = append(evidence, api.MatchSemanticEvidence{
RequirementKind: api.MatchRequirementKind(item.RequirementKind), RequirementText: item.RequirementText,
ItemKind: api.MatchSemanticItemKind(item.ItemKind), ProjectTitle: title,
RepositoryVerified: item.RepositoryVerified, Similarity: api.MatchSimilarityBand(item.Similarity),
})
}
return evidence
}
func matchUnknownFlags(flags []matching.UnknownFlag) api.MatchUnknownFlags {
response := make(api.MatchUnknownFlags, 0, len(flags))
for _, flag := range flags {
@@ -112,7 +112,7 @@ func TestSearchOrganizationMatchesMapsANeedRequestAndKeepsUnconfirmedEntriesStri
}
want := `[{"candidate_handle":"` + handle + `","specialization":"network_engineer","confirmation_status":"unconfirmed","declared_grade":"junior",` +
`"category":null,"strength":null,"public_context":{"stack":null,"experience_band":null,"work_formats":null,"salary_compatibility":null},` +
`"evidence_summary":[{"code":"declared_grade","state":"declared_only"}],"components":{"fit":0.3125,"coverage":0.25},` +
`"evidence_summary":[{"code":"declared_grade","state":"declared_only"}],"semantic_evidence":[],"components":{"fit":0.3125,"coverage":0.25},` +
`"unknown_flags":[{"component":"assessment","reason":"no_confirmation_for_specialization"}]}]`
if string(encoded) != want {
t.Fatalf("unconfirmed entries = %s\nwant %s", encoded, want)
@@ -565,6 +565,19 @@ func anonymousCardResponse(view domain.AnonymousCandidateViewDTO) api.AnonymousC
if view.SalaryCompatibility != nil {
content.SalaryCompatibility.SetTo(api.CandidateSalaryCompatibility(*view.SalaryCompatibility))
}
for _, project := range view.Projects {
description := api.NilString{Null: true}
if project.Description != nil {
description.SetTo(*project.Description)
}
stack := make([]api.StackTag, 0, len(project.Stack))
for _, tag := range project.Stack {
stack = append(stack, api.StackTag(tag))
}
content.Projects = append(content.Projects, api.CardProject{
Title: project.Title, Description: description, Stack: stack, RepositoryVerified: project.RepositoryVerified,
})
}
return content
}
@@ -3,6 +3,7 @@ package v1
import (
"context"
"errors"
"math"
"github.com/google/uuid"
@@ -18,6 +19,7 @@ import (
type ProfileOperations interface {
GetMyProfile(ctx context.Context, userID uuid.UUID) (profile.View, error)
SaveMyProfile(ctx context.Context, userID uuid.UUID, expectedVersion int64, input domain.CandidateProfileInput) (profile.View, error)
RequestRepositoryChecks(ctx context.Context, userID uuid.UUID) (int, error)
}
// ProfileHandler serves the candidate's own profile operations.
@@ -46,6 +48,39 @@ func (handler *Handler) SaveMyProfile(ctx context.Context, request *api.Candidat
return handler.profile.SaveMyProfile(ctx, request)
}
func (handler *Handler) RequestMyRepositoryChecks(ctx context.Context) (api.RequestMyRepositoryChecksRes, error) {
if handler.profile == nil {
return handler.UnimplementedHandler.RequestMyRepositoryChecks(ctx)
}
return handler.profile.RequestMyRepositoryChecks(ctx)
}
// RequestMyRepositoryChecks schedules new checks of the linked repositories.
func (handler *ProfileHandler) RequestMyRepositoryChecks(ctx context.Context) (api.RequestMyRepositoryChecksRes, error) {
principal, ok := PrincipalFromContext(ctx)
if !ok {
return (*api.RequestMyRepositoryChecksUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
}
scheduled, err := handler.operations.RequestRepositoryChecks(ctx, principal.UserID)
if err != nil {
if limit, ok := errors.AsType[*profile.RepositoryCheckLimitError](err); ok {
return problem.RateLimited(ctx, int32(math.Ceil(limit.RetryAfter.Seconds()))), nil
}
switch {
case errors.Is(err, profile.ErrForbidden), errors.Is(err, profile.ErrEmailNotVerified):
return (*api.RequestMyRepositoryChecksForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
case errors.Is(err, outbound.ErrUserNotFound):
return (*api.RequestMyRepositoryChecksUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
default:
return nil, err
}
}
return &api.ProfileRepositoryCheckRequestHeaders{
CacheControl: api.CacheControlNoStoreNoStore,
Response: api.ProfileRepositoryCheckRequest{Scheduled: int32(scheduled)}, //nolint:gosec // at most ten projects
}, nil
}
func (handler *ProfileHandler) GetMyProfile(ctx context.Context) (api.GetMyProfileRes, error) {
principal, ok := PrincipalFromContext(ctx)
if !ok {
@@ -167,6 +202,7 @@ func candidateProfileInput(request *api.CandidateProfileUpdate) domain.Candidate
for _, item := range request.SoftSkills {
input.SoftSkills = append(input.SoftSkills, domain.ProfileClaimInput[string]{Value: string(item.Skill), Status: domain.ProfileClaimStatus(item.Status)})
}
input.Projects = projectInputs(request.Projects)
if value, ok := request.Terms.Location.Get(); ok {
input.Location = profileStringInput(string(value.Value), value.Status)
}
@@ -277,6 +313,7 @@ func candidateProfileResponse(view profile.View) api.CandidateProfile {
Skill: api.ProfileSoftSkill(item.Value()), Status: api.ProfileItemStatus(item.Status()), Source: profileClaimSourceResponse(item.Source()),
})
}
response.Projects = projectResponses(content, view.RepositoryChecks)
response.Terms = candidateProfileTerms(content)
updatedAt := api.NilTimestamp{}
if saved.UpdatedAt().IsZero() {
@@ -289,6 +326,75 @@ func candidateProfileResponse(view profile.View) api.CandidateProfile {
return response
}
// projectInputs maps the request projects. An absent list keeps the stored
// projects and maps to nil; an empty one clears them.
func projectInputs(items []api.ProfileProjectItemInput) *[]domain.ProfileClaimInput[domain.ProfileProject] {
if items == nil {
return nil
}
projects := make([]domain.ProfileClaimInput[domain.ProfileProject], 0, len(items))
for _, item := range items {
description := ""
if value, ok := item.Description.Get(); ok {
description = string(value)
}
repository := ""
if value, ok := item.RepositoryURL.Get(); ok {
repository = string(value)
}
stack := make([]string, 0, len(item.Stack))
for _, tag := range item.Stack {
stack = append(stack, string(tag))
}
projects = append(projects, domain.ProfileClaimInput[domain.ProfileProject]{
Value: domain.NewProfileProject(string(item.Title), description, stack, repository),
Status: domain.ProfileClaimStatus(item.Status),
})
}
return &projects
}
func projectResponses(content domain.CandidateProfileContent, checks map[string]outbound.RepositoryCheck) []api.ProfileProjectItem {
projects := make([]api.ProfileProjectItem, 0, len(content.Projects()))
for _, item := range content.Projects() {
projects = append(projects, profileProjectResponse(item, checks))
}
return projects
}
// profileProjectResponse maps one project with the state of its repository
// check. A linked repository without a stored check is pending.
func profileProjectResponse(item domain.ProfileClaim[domain.ProfileProject], checks map[string]outbound.RepositoryCheck) api.ProfileProjectItem {
value := item.Value()
description := api.NilProfileProjectDescription{Null: true}
if value.Description != "" {
description.SetTo(api.ProfileProjectDescription(value.Description))
}
stack := make(api.ProfileProjectStack, 0, len(value.StackTags()))
for _, tag := range value.StackTags() {
stack = append(stack, api.StackTag(tag))
}
project := api.ProfileProjectItem{
Title: api.ProfileProjectTitle(value.Title), Description: description, Stack: stack,
RepositoryURL: api.NilProfileRepositoryUrl{Null: true}, RepositoryCheck: api.NilProfileRepositoryCheck{Null: true},
Status: api.ProfileItemStatus(item.Status()), Source: profileClaimSourceResponse(item.Source()),
}
repository, linked := value.Repository()
if !linked {
return project
}
project.RepositoryURL.SetTo(api.ProfileRepositoryUrl(repository.URL()))
check := api.ProfileRepositoryCheck{State: api.ProfileRepositoryCheckStatePending, CheckedAt: api.NilTimestamp{Null: true}}
if stored, ok := checks[repository.Key()]; ok {
check.State = api.ProfileRepositoryCheckState(stored.State)
if !stored.CheckedAt.IsZero() {
check.CheckedAt.SetTo(api.Timestamp(stored.CheckedAt))
}
}
project.RepositoryCheck.SetTo(check)
return project
}
// confirmedCategoriesResponse maps the categories in their stored order. The
// result is never nil, so a profile without categories encodes as an empty list.
func confirmedCategoriesResponse(categories []domain.CandidateAssessmentCategory) []api.ProfileConfirmedCategory {