Files
lincodex/backend/internal/domain/policy_matching.go
T

337 lines
13 KiB
Go

package domain
import "slices"
// MatchingPolicy holds the versioned coefficients of the deterministic basic
// matching (FR09). Every share is a BasisPoints value, so a comparison and a
// stored document carry an exact integer instead of a binary fraction. The
// section is optional: a policy version without it cannot rank, and an absent
// section never acquires defaults.
//
// Fit = FitCoverageWeight*Cov + FitDomainWeight*Dom + FitSemanticsWeight*Sem
// P = T + CompetitionBoost*(1-T)*V
// S = (RankFitWeight*Fit + RankStrengthWeight*P) *
//
// (FreshnessFloor + (1-FreshnessFloor)*Fr) *
// (ResponsivenessFloor + (1-ResponsivenessFloor)*Rsp)
//
// A skill contributes the best product of the trust, attribution, and
// relation coefficients over the permitted evidence; Cov is the weighted mean
// of the contributions of the requested skills.
type MatchingPolicy struct {
FitCoverageWeight BasisPoints
FitDomainWeight BasisPoints
FitSemanticsWeight BasisPoints
RankFitWeight BasisPoints
RankStrengthWeight BasisPoints
// FreshnessFloor and ResponsivenessFloor are the multipliers of a
// component equal to zero; a component equal to one multiplies by 1.
FreshnessFloor BasisPoints
ResponsivenessFloor BasisPoints
// CompetitionBoost is the share of the remaining distance (1-T) that a
// competition contribution of one adds to the strength of the profile.
CompetitionBoost BasisPoints
// NeutralUnknown replaces a component whose value is unknown. The
// unknown state is reported next to it and is never read as zero ability.
NeutralUnknown BasisPoints
// NearSkillThreshold is the contribution below which a requested
// required skill moves a confirmed profile to the near group.
NearSkillThreshold BasisPoints
RequiredSkillWeight int32
DesiredSkillWeight int32
// The trust coefficients are indexed by the evidence trust level 0..3.
TrustStated BasisPoints
TrustOnline BasisPoints
TrustIdentity BasisPoints
TrustInPerson BasisPoints
AttributionSelf BasisPoints
// AttributionTeamRole is a relevant role in a team result and
// AttributionTeamUnattributed a team result without a role.
AttributionTeamRole BasisPoints
AttributionTeamUnattributed BasisPoints
// The relation coefficients apply when the evidence skill equals the
// requested one (or a synonym), is narrower than it, or is broader.
RelationExact BasisPoints
RelationNarrower BasisPoints
RelationBroader BasisPoints
// PercentileMinCohort is the smallest number of comparable real
// candidates that allows a percentile; below it T is neutral.
PercentileMinCohort int32
// MaxResultsPerGroup bounds how many entries of each group one search may
// return. The whole visible bank is ranked and only the output is cut, so
// a search cannot read the whole bank at once. It is a demonstration value
// that stays below the daily card-view allowance of the first tier.
MaxResultsPerGroup int32
// SkillTaxonomy lists, for each specialization, the technologies that fall
// inside it. Evidence for a specialization is broader evidence for each of
// them, and evidence for one of them is narrower evidence for the
// specialization. It is part of the version, so the policy version fully
// determines a result.
SkillTaxonomy []MatchingSkillGroup
}
// MatchingSkillGroup is the technologies that one specialization contains.
type MatchingSkillGroup struct {
Specialization SpecializationCode
Skills []CandidateViewSkillCode
}
// Clone copies the slices, so a caller cannot change a stored version.
func (policy MatchingPolicy) Clone() MatchingPolicy {
policy.SkillTaxonomy = slices.Clone(policy.SkillTaxonomy)
for index := range policy.SkillTaxonomy {
policy.SkillTaxonomy[index].Skills = slices.Clone(policy.SkillTaxonomy[index].Skills)
}
return policy
}
// RelateSkill classifies the skill of a piece of evidence against a requested
// skill by the taxonomy of this policy version. Both are taxonomy codes; an
// unmapped code has no relation.
func (policy MatchingPolicy) RelateSkill(requested, evidence string) EvidenceSkillRelation {
switch {
case requested == "" || evidence == "":
return EvidenceSkillRelationNone
case requested == evidence:
return EvidenceSkillRelationExact
case policy.contains(evidence, requested):
return EvidenceSkillRelationBroader
case policy.contains(requested, evidence):
return EvidenceSkillRelationNarrower
default:
return EvidenceSkillRelationNone
}
}
func (policy MatchingPolicy) contains(specialization, skill string) bool {
for _, group := range policy.SkillTaxonomy {
if string(group.Specialization) != specialization {
continue
}
for _, contained := range group.Skills {
if string(contained) == skill {
return true
}
}
}
return false
}
// Sanity bounds of the matching section. They reject a value in the wrong
// unit; the demo values themselves are recorded in DemoMatchingPolicy.
const (
maxMatchingSkillWeight = 100
maxMatchingCohortMinimum = 100000
maxMatchingResults = 1000
)
// DemoMatchingPolicy returns the initial matching coefficients. They are the
// design values of PRD 4.9 for verifying the flow, not measured results: no
// labelled set supports them, and a calibrated policy must replace them.
func DemoMatchingPolicy() MatchingPolicy {
return MatchingPolicy{
FitCoverageWeight: 7500, FitDomainWeight: 1500, FitSemanticsWeight: 1000,
RankFitWeight: 5000, RankStrengthWeight: 5000,
FreshnessFloor: 9000, ResponsivenessFloor: 9000,
CompetitionBoost: 3000,
NeutralUnknown: 5000, NearSkillThreshold: 2500,
RequiredSkillWeight: 2, DesiredSkillWeight: 1,
TrustStated: 2500, TrustOnline: 6000, TrustIdentity: 8500, TrustInPerson: 10000,
AttributionSelf: 10000, AttributionTeamRole: 8000, AttributionTeamUnattributed: 5000,
RelationExact: 10000, RelationNarrower: 8000, RelationBroader: 4000,
PercentileMinCohort: 30,
// 10 per group is at most 30 reveals for one search, below the 50
// distinct card views per day of the first tier, so one search cannot
// spend a whole allowance.
MaxResultsPerGroup: 10,
SkillTaxonomy: []MatchingSkillGroup{
{
Specialization: SpecializationBackend,
Skills: []CandidateViewSkillCode{
CandidateViewSkillDocker, CandidateViewSkillGo, CandidateViewSkillKafka, CandidateViewSkillPostgreSQL,
},
},
{Specialization: SpecializationDevopsSRE, Skills: []CandidateViewSkillCode{CandidateViewSkillDocker}},
},
}
}
// TrustCoefficient returns the coefficient of an evidence trust level. An
// unknown level has none.
func (policy MatchingPolicy) TrustCoefficient(trust EvidenceTrust) (BasisPoints, bool) {
switch trust {
case EvidenceTrustStated:
return policy.TrustStated, true
case EvidenceTrustOnlineUnconfirmed:
return policy.TrustOnline, true
case EvidenceTrustIdentityTied:
return policy.TrustIdentity, true
case EvidenceTrustInPerson:
return policy.TrustInPerson, true
default:
return 0, false
}
}
// AttributionCoefficient returns the coefficient of an evidence attribution.
func (policy MatchingPolicy) AttributionCoefficient(attribution EvidenceAttribution) (BasisPoints, bool) {
switch attribution {
case EvidenceAttributionPersonal:
return policy.AttributionSelf, true
case EvidenceAttributionTeamRole:
return policy.AttributionTeamRole, true
case EvidenceAttributionTeamUnattributed:
return policy.AttributionTeamUnattributed, true
default:
return 0, false
}
}
// RelationCoefficient returns the coefficient of a skill relation. A skill
// that is unrelated to the requested one earns nothing.
func (policy MatchingPolicy) RelationCoefficient(relation EvidenceSkillRelation) BasisPoints {
switch relation {
case EvidenceSkillRelationExact:
return policy.RelationExact
case EvidenceSkillRelationNarrower:
return policy.RelationNarrower
case EvidenceSkillRelationBroader:
return policy.RelationBroader
case EvidenceSkillRelationNone:
return 0
default:
return 0
}
}
// Validate rejects a zero, out-of-range, or inconsistent section.
func (policy MatchingPolicy) Validate() error {
return firstError(
policy.validateShares(),
policy.validateWeights(),
policy.validateOrdering(),
)
}
func (policy MatchingPolicy) validateShares() error {
const location = "MatchingPolicy.Validate"
shares := []struct {
field string
value BasisPoints
}{
{"matching.competition_boost_bp", policy.CompetitionBoost},
{"matching.neutral_unknown_bp", policy.NeutralUnknown},
{"matching.near_skill_threshold_bp", policy.NearSkillThreshold},
{"matching.trust_stated_bp", policy.TrustStated},
{"matching.trust_online_bp", policy.TrustOnline},
{"matching.trust_identity_bp", policy.TrustIdentity},
{"matching.trust_in_person_bp", policy.TrustInPerson},
{"matching.attribution_personal_bp", policy.AttributionSelf},
{"matching.attribution_team_role_bp", policy.AttributionTeamRole},
{"matching.attribution_team_unattributed_bp", policy.AttributionTeamUnattributed},
{"matching.relation_exact_bp", policy.RelationExact},
{"matching.relation_narrower_bp", policy.RelationNarrower},
{"matching.relation_broader_bp", policy.RelationBroader},
}
for _, share := range shares {
if err := checkRange(location, share.field, share.value, 0, basisPointsPerUnit); err != nil {
return err
}
}
// A floor of zero would let one unknown multiplier erase the score, and a
// floor of one would switch the multiplier off.
return firstError(
checkRange(location, "matching.freshness_floor_bp", policy.FreshnessFloor, 1, basisPointsPerUnit),
checkRange(location, "matching.responsiveness_floor_bp", policy.ResponsivenessFloor, 1, basisPointsPerUnit),
)
}
func (policy MatchingPolicy) validateWeights() error {
const location = "MatchingPolicy.Validate"
if err := firstError(
checkRange(location, "matching.required_skill_weight", policy.RequiredSkillWeight, 1, maxMatchingSkillWeight),
checkRange(location, "matching.desired_skill_weight", policy.DesiredSkillWeight, 1, maxMatchingSkillWeight),
checkRange(location, "matching.percentile_min_cohort", policy.PercentileMinCohort, 1, maxMatchingCohortMinimum),
checkRange(location, "matching.max_results_per_group", policy.MaxResultsPerGroup, 1, maxMatchingResults),
policy.validateTaxonomy(),
); err != nil {
return err
}
for _, sum := range []struct {
field string
values []BasisPoints
}{
{"matching.fit_*_weight_bp", []BasisPoints{policy.FitCoverageWeight, policy.FitDomainWeight, policy.FitSemanticsWeight}},
{"matching.rank_*_weight_bp", []BasisPoints{policy.RankFitWeight, policy.RankStrengthWeight}},
} {
var total BasisPoints
for _, value := range sum.values {
if err := checkRange(location, sum.field, value, 0, basisPointsPerUnit); err != nil {
return err
}
total += value
}
if total != basisPointsPerUnit {
return invalidPolicy(location, sum.field+" must add up to 1.0000")
}
}
return nil
}
// validateOrdering keeps a stronger confirmation from counting for less than
// a weaker one, and a closer relation from counting for less than a looser one.
func (policy MatchingPolicy) validateOrdering() error {
const location = "MatchingPolicy.Validate"
if policy.TrustStated > policy.TrustOnline || policy.TrustOnline > policy.TrustIdentity || policy.TrustIdentity > policy.TrustInPerson {
return invalidPolicy(location, "matching trust coefficients must not decrease with the trust level")
}
if policy.AttributionTeamUnattributed > policy.AttributionTeamRole || policy.AttributionTeamRole > policy.AttributionSelf {
return invalidPolicy(location, "matching attribution coefficients must not increase from personal to team results")
}
if policy.RelationBroader > policy.RelationNarrower || policy.RelationNarrower > policy.RelationExact {
return invalidPolicy(location, "matching relation coefficients must not increase from broader to exact")
}
return nil
}
// validateTaxonomy keeps each specialization and technology known and listed
// once, so a relation never depends on the order or a repeat.
func (policy MatchingPolicy) validateTaxonomy() error {
const location = "MatchingPolicy.Validate"
seen := make(map[SpecializationCode]struct{}, len(policy.SkillTaxonomy))
if len(policy.SkillTaxonomy) > len(specializationCodes) {
return invalidPolicy(location, "matching.skill_taxonomy has more groups than specializations")
}
for _, group := range policy.SkillTaxonomy {
if !group.Specialization.IsKnown() {
return invalidPolicy(location, "matching.skill_taxonomy names an unknown specialization")
}
if _, repeated := seen[group.Specialization]; repeated {
return invalidPolicy(location, "matching.skill_taxonomy repeats a specialization")
}
seen[group.Specialization] = struct{}{}
if len(group.Skills) == 0 || len(group.Skills) > len(candidateViewSkillCodes) {
return invalidPolicy(location, "matching.skill_taxonomy needs between one and every known technology for a specialization")
}
skills := make(map[CandidateViewSkillCode]struct{}, len(group.Skills))
for _, skill := range group.Skills {
if !skill.IsKnown() {
return invalidPolicy(location, "matching.skill_taxonomy names an unknown technology")
}
if _, repeated := skills[skill]; repeated {
return invalidPolicy(location, "matching.skill_taxonomy repeats a technology")
}
skills[skill] = struct{}{}
}
}
return nil
}