Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/integration/mag_reranker_integration_test.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 720d47fae476b0d38477ae4401697d372b1ea2bc |
| Branch | feature/opportunity-to-outcome |
| Path or archive member | internal/integration/mag_reranker_integration_test.go |
| Original SHA-256 | cff96a0981e00da85e9a2ca5fa2bc8b7aae9069511ec1f7f66ed6fc3daf8737d |
| Git blob SHA | 437bd416764615628c73858f3161e3c2583ca35f |
| Reading-copy SHA-256 | cff96a0981e00da85e9a2ca5fa2bc8b7aae9069511ec1f7f66ed6fc3daf8737d |
Every recorded occurrence
These 10 records contain identical original source bytes. Their paths and revisions remain part of the archive.
| Original record | Branch or archive | Revision / identifier | Original location |
|---|---|---|---|
| Janady13/missionmatch / internal/integration/mag_reranker_integration_test.go | feature/opportunity-to-outcome | 720d47fae476b0d38477ae4401697d372b1ea2bc | Open original |
| Janady13/missionmatch / internal/integration/mag_reranker_integration_test.go | collision-deconfliction | 4ee5a09972608ed89f8377c6665542d5f450f0a6 | Open original |
| Janady13/missionmatch / internal/integration/mag_reranker_integration_test.go | master | 1e9249840a4302ec82e123b9c9cb6f75badea3e4 | Open original |
| Janady13/missionmatch / internal/integration/mag_reranker_integration_test.go | mag-substrate | b82c7100a492dec67dfd1eda6b3f53def859be35 | Open original |
| Janady13/missionmatch / internal/integration/mag_reranker_integration_test.go | time-to-fill-survival | b66e1e505863d4f17741a4b9ede73509e45bd6c6 | Open original |
| Janady13/missionmatch / internal/integration/mag_reranker_integration_test.go | presentation-readiness | eb574acaa29d71ce4d0f852572e87c275c79fdb7 | Open original |
| Janady13/missionmatch / internal/integration/mag_reranker_integration_test.go | vendor-intelligence | fe9796a19c261951d3f2565206c0c1b5ac294465 | Open original |
| Janady13/missionmatch / internal/integration/mag_reranker_integration_test.go | phase2-connectors | e038d4c687aec16f8bd9bc4d907eea14d91f8704 | Open original |
| Janady13/missionmatch / internal/integration/mag_reranker_integration_test.go | feature/aioe-upgrade | 2178e7298d0e5d9af83afa12402fd587f0ba6ed8 | Open original |
| Janady13/missionmatch / internal/integration/mag_reranker_integration_test.go | capture-readiness | bae6af7cb2997e819174a9e9cfff5e16067a1975 | Open original |
Recovered source transcript
Download complete source text ↓216 lines · 10.2 KB
//go:build integration
// MAG display order re ranker loop integration. Proves at runtime against a REAL Postgres and the migrated 0037
// prior only re ranker what the unit tests cannot: the offline TRAINER fits a pairwise learning to rank
// challenger from decided slate_members (approve/reject) over the frozen engine sub scores, the EVALUATOR
// compares its held out ranking accuracy to the prior only engine order champion AND runs the four fifths no
// adverse impact check recomputed under the challenger ranking, a human promotion flips the champion, and the
// learned weights then REORDER a slate that the engine order and the human decisions genuinely disagree on,
// with the frozen engine score untouched (the re ranker emits an ordering, never a candidate score, and a human
// still approves every member).
//
// The seeded signal is honest: approved candidates are high location and low clearance, rejected candidates are
// the reverse. The engine weights clearance (0.25) above location (0.10), so the engine order puts the rejected
// candidates first; the humans approved the opposite, so a correct re ranker learns to weight location above
// clearance and flips the order. NONE of the features is a protected attribute.
//
// Run: go test -tags=integration ./internal/integration/ -run MAGReranker -v
// Env: MM_APP_DSN, MM_MIGRATOR_DSN, MM_COMPLIANCE_DSN.
package integration
import (
"context"
"testing"
"github.com/google/uuid"
"github.com/missionworkforce/missionmatch/internal/modules/eeo"
"github.com/missionworkforce/missionmatch/internal/platform/magstore"
"github.com/missionworkforce/missionmatch/internal/platform/tenant"
)
// memberSpec is one seeded slate member: its approval and its six frozen sub score features in RerankFeatureOrder
// (clearance, skills, certs, customer, experience, location).
type memberSpec struct {
approved bool
features [6]float64
}
// seedDecidedSlate seeds one slate with decided members carrying the six frozen sub scores and the approve or
// reject decision, via the BYPASSRLS migrator (cross tenant seed). total_score and band are the engine weighted
// sum of the sub scores, so the prior only re ranker (the engine weights) orders by total exactly.
func seedDecidedSlate(t *testing.T, org uuid.UUID, specs []memberSpec) {
t.Helper()
ctx := context.Background()
req := seedRequirementWithAgency(t, org, "RERANK "+uuid.NewString()[:8])
slateID := uuid.New()
if _, err := migrator.Exec(ctx,
`INSERT INTO slates (id, organization_id, requirement_id, name) VALUES ($1,$2,$3,$4)`,
slateID, org, req, "reranker seed slate"); err != nil {
t.Fatalf("seed slate: %v", err)
}
for _, s := range specs {
cand := seedCandidate(t, org, "secret", "active", true)
total := 0.25*s.features[0] + 0.25*s.features[1] + 0.15*s.features[2] + 0.15*s.features[3] + 0.10*s.features[4] + 0.10*s.features[5]
band := "red"
if total >= 90 {
band = "green"
} else if total >= 75 {
band = "yellow"
}
decision := "rejected"
if s.approved {
decision = "approved"
}
if _, err := migrator.Exec(ctx,
`INSERT INTO slate_members
(organization_id, slate_id, candidate_id, total_score, band,
clearance_score, skills_score, certs_score, customer_score, experience_score, location_score,
explanation, trace, decision, decided_at)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,'seed','[]',$12, now())`,
org, slateID, cand.ID, total, band,
s.features[0], s.features[1], s.features[2], s.features[3], s.features[4], s.features[5],
decision); err != nil {
t.Fatalf("seed slate member: %v", err)
}
}
}
// TestMAGRerankerTrainsEvaluatesPromotesAndReorders drives the whole re ranker loop end to end on staging.
func TestMAGRerankerTrainsEvaluatesPromotesAndReorders(t *testing.T) {
modelKey := magstore.RerankModelKey
const priorOnlyVersion = 1
ctx := context.Background()
// Force the champion back to the prior only engine order v1 so the before and after comparison is
// deterministic, and restore it on cleanup so it never leaks a learned re ranker into other tests.
if _, err := magstore.PromoteChampion(ctx, migrator, modelKey, priorOnlyVersion); err != nil {
t.Fatalf("reset reranker champion to v%d: %v", priorOnlyVersion, err)
}
t.Cleanup(func() {
if _, cerr := magstore.PromoteChampion(ctx, migrator, modelKey, priorOnlyVersion); cerr != nil {
t.Logf("cleanup: could not restore prior only reranker champion v%d: %v", priorOnlyVersion, cerr)
}
})
// Seed slates with the honest signal: approved are high location (100) low clearance (0), rejected are the
// reverse; the middle four sub scores are fixed at 50. The engine weights clearance above location, so the
// engine order puts rejected first; a correct re ranker flips it.
approved := [6]float64{0, 50, 50, 50, 50, 100} // clearance 0, location 100
rejected := [6]float64{100, 50, 50, 50, 50, 0} // clearance 100, location 0
org := seedOrg(t)
for i := 0; i < 10; i++ {
seedDecidedSlate(t, org, []memberSpec{
{approved: true, features: approved},
{approved: true, features: approved},
{approved: false, features: rejected},
{approved: false, features: rejected},
})
}
// The prior only champion (the engine weights) orders a test slate by total: the rejected profile (higher
// total, because clearance outweighs location) ranks ABOVE the approved profile.
champV1, err := magstore.LoadModel(ctx, migrator, modelKey, priorOnlyVersion)
if err != nil {
t.Fatalf("load prior only champion: %v", err)
}
v1Coeffs := magstore.RerankCoeffs(champV1)
testSlate := []magstore.SlateMember{
{CandidateID: "approved", Features: approved[:], Approved: true},
{CandidateID: "rejected", Features: rejected[:], Approved: false},
}
engineOrder := magstore.OrderMembers(v1Coeffs, testSlate)
if engineOrder[0].CandidateID != "rejected" {
t.Fatalf("the engine order must rank the higher total (rejected profile) first, got %s", engineOrder[0].CandidateID)
}
// TRAIN: fit a pairwise re ranker challenger. min-pairs 20 so the seeded pairs clear the floor.
res, err := magstore.TrainReranker(ctx, migrator, modelKey, 1.0, 20, false)
if err != nil {
t.Fatalf("train reranker: %v", err)
}
if res.Skipped {
t.Fatalf("the fit must not be skipped with %d pairs: %s", res.Pairs, res.SkipReason)
}
if res.Version <= priorOnlyVersion {
t.Fatalf("the challenger version %d must be newer than the prior champion", res.Version)
}
// The learned location weight (index 5) must exceed the learned clearance weight (index 0): the human signal
// preferred location over clearance, the opposite of the engine.
if res.Coeffs[5] <= res.Coeffs[0] {
t.Fatalf("the learned re ranker must weight location above clearance, got location %.4f clearance %.4f", res.Coeffs[5], res.Coeffs[0])
}
t.Logf("challenger v%d learned weights: clearance %.4f location %.4f over %d pairs", res.Version, res.Coeffs[0], res.Coeffs[5], res.Pairs)
// The trainer must NOT flip champion.
stillV1, err := magstore.ChampionVersion(ctx, migrator, modelKey)
if err != nil {
t.Fatalf("champion after train: %v", err)
}
if stillV1 != priorOnlyVersion {
t.Fatalf("the trainer must not flip champion, moved to v%d", stillV1)
}
// EVALUATE ranking quality: the challenger must order approved above rejected far better than the engine
// order champion (which orders them backwards, accuracy near 0).
chal, err := magstore.LoadModel(ctx, migrator, modelKey, res.Version)
if err != nil {
t.Fatalf("load challenger: %v", err)
}
quality, err := magstore.EvaluateRerankerQuality(ctx, migrator, v1Coeffs, magstore.RerankCoeffs(chal), 0.30)
if err != nil {
t.Fatalf("evaluate reranker: %v", err)
}
// The learned challenger must clear the ranking quality gate (at least as good as the engine order within
// tolerance). Aggregate held out accuracy is dominated by the large pooled slate history on staging, so the
// robust proofs of learning are the weight direction (above) and the reorder (below), not the aggregate
// delta; the gate guards against a genuine regression.
t.Logf("held out %d slates, pairwise accuracy champion %.4f vs challenger %.4f", quality.HeldOutGroups, quality.ChampAccuracy, quality.ChalAccuracy)
if !quality.Pass {
t.Errorf("the learned challenger must clear the ranking quality gate, got %+v", quality)
}
// The FAIRNESS gate must be callable through the compliance pool. With no seeded demographics it recomputes
// clean (no group flagged), exercising the ranking recomputation path end to end.
if compliance != nil {
groups, gerr := magstore.ReadSlateGroups(ctx, migrator)
if gerr != nil {
t.Fatalf("read slate groups: %v", gerr)
}
orgGroups := magstore.GroupsByOrg(groups)[org.String()]
sel := magstore.CandidateSelection(magstore.RerankCoeffs(chal), orgGroups)
svc := eeo.NewService(tenant.New(compliance))
section, ferr := svc.AdverseImpactForSelection(tenant.WithOrg(ctx, org.String()), sel)
if ferr != nil {
t.Fatalf("the ranking fairness recomputation must be callable: %v", ferr)
}
if r := section.MinImpactRatio(); r < 0.80 {
t.Errorf("with no seeded demographics the recomputation must be clean, got min ratio %v", r)
}
}
// PROMOTE: the human governance action flips the champion.
from, err := magstore.PromoteChampion(ctx, migrator, modelKey, res.Version)
if err != nil {
t.Fatalf("promote reranker: %v", err)
}
if from != priorOnlyVersion {
t.Errorf("the promotion must replace the prior champion v%d, replaced v%d", priorOnlyVersion, from)
}
// REORDER: the learned champion now orders the SAME test slate the other way, the approved profile first,
// proving the display order moved off the engine order toward the human signal.
champLearned, err := magstore.LoadModel(ctx, migrator, modelKey, res.Version)
if err != nil {
t.Fatalf("load learned champion: %v", err)
}
learnedOrder := magstore.OrderMembers(magstore.RerankCoeffs(champLearned), testSlate)
if learnedOrder[0].CandidateID != "approved" {
t.Fatalf("the learned re ranker must reorder the approved profile first, got %s", learnedOrder[0].CandidateID)
}
if learnedOrder[0].CandidateID == engineOrder[0].CandidateID {
t.Fatalf("the display order must change from the engine order, both put %s first", engineOrder[0].CandidateID)
}
t.Logf("engine order first=%s, learned order first=%s (the re ranker flipped the display order)", engineOrder[0].CandidateID, learnedOrder[0].CandidateID)
}