Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/modules/ai/vendor.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 720d47fae476b0d38477ae4401697d372b1ea2bc |
| Branch | feature/opportunity-to-outcome |
| Path or archive member | internal/modules/ai/vendor.go |
| Original SHA-256 | 0a91057472e0f4711b87dd0ac878a9bb0272a781c036d57d2ccf36d578ba9e1e |
| Git blob SHA | 75ea5a9c18a52cd7f91a482d347d566c3fac7afd |
| Reading-copy SHA-256 | 0a91057472e0f4711b87dd0ac878a9bb0272a781c036d57d2ccf36d578ba9e1e |
Every recorded occurrence
These 5 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/modules/ai/vendor.go | feature/opportunity-to-outcome | 720d47fae476b0d38477ae4401697d372b1ea2bc | Open original |
| Janady13/missionmatch / internal/modules/ai/vendor.go | master | 1e9249840a4302ec82e123b9c9cb6f75badea3e4 | Open original |
| Janady13/missionmatch / internal/modules/ai/vendor.go | presentation-readiness | eb574acaa29d71ce4d0f852572e87c275c79fdb7 | Open original |
| Janady13/missionmatch / internal/modules/ai/vendor.go | vendor-intelligence | fe9796a19c261951d3f2565206c0c1b5ac294465 | Open original |
| Janady13/missionmatch / internal/modules/ai/vendor.go | feature/aioe-upgrade | 2178e7298d0e5d9af83afa12402fd587f0ba6ed8 | Open original |
Recovered source transcript
Download complete source text ↓231 lines · 12.2 KB
package ai
// Phase 2 Vendor Intelligence: measuring a staffing vendor's or subcontractor's performance WITHOUT ever
// rescoring a person. Two closed Registry intents over the staffing_vendors and vendor_submissions tables
// (migration 0042):
//
// - vendor_performance_scorecard: a plain TAG aggregate. Per vendor for the caller's org, the outcome metrics
// (fill rate, submit to interview rate, average time to submit) plus a deterministic weighted
// PerformanceScore, all exact numeric. Its quality component is the average of the ALREADY COMPUTED frozen
// v1.4.0 engine bands (candidate_evaluations.band, green 1.0 / yellow 0.5 / red 0.0) of the DISTINCT
// candidates the vendor submitted (each candidate reduced to their most recent representative band, so a
// repeat submission counts the person once), computed ONLY when the vendor has at least ten evaluated
// DISTINCT candidates (a k floor over PEOPLE, so a thin vendor's quality, or a handful of people submitted
// many times, can never re identify an individual), never recomputed and never diluted by an unevaluated
// submission. It reads no per band headcount partition, so it needs no cell suppression; it is an outcome
// aggregate.
//
// - vendor_quality_distribution: a BENCH intent, k=10 small cell suppressed. Per vendor, the headcount of its
// submitted candidates in each frozen readiness band (green, yellow, red). This IS a person derived
// headcount partition, so it runs only through the suppressing bench executor: each vendor's three band
// cells are suppressed below k so a sub k vendor band is never a raw count.
//
// The frozen engine stays the ONLY per candidate scorer: both intents read the band the engine ALREADY decided,
// neither computes a score or a band. Neither references the eeo schema or a protected attribute. Every number
// the scorecard surfaces is exact numeric scanned into shopspring/decimal, never float.
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
"github.com/missionworkforce/missionmatch/internal/platform/tenant"
)
// IntentVendorPerformanceScorecard is the registered name of the deterministic vendor performance scorecard (a
// plain TAG aggregate intent, run through the standard ExecuteIntent path).
const IntentVendorPerformanceScorecard = "vendor_performance_scorecard"
// IntentVendorQualityDistribution is the registered name of the k=10 suppressed vendor quality distribution (a
// BENCH intent, run ONLY through the suppressing bench executor, never the raw ExecuteIntent path).
const IntentVendorQualityDistribution = "vendor_quality_distribution"
// vendorPerformanceScorecardSQL aggregates each vendor's submissions to the deterministic performance metrics
// and the weighted PerformanceScore, RLS scoped to the caller's org. It is TWO joins deliberately kept apart.
// The sub CTE is SUBMISSION level: fill_rate is placements over submissions, interview_rate is interviews or
// placements over submissions, avg_time_to_submit_days is the mean days from the requirement's creation to the
// submission. The per_candidate CTE is PERSON level: it reduces each (vendor, candidate) to ONE representative
// frozen band, the candidate's MOST RECENT evaluation among that vendor's submissions (DISTINCT ON with an
// evaluated_at then requirement_id total order), so a candidate the vendor submitted for several requirements
// counts ONCE, never once per submission. The qual CTE then averages those per candidate bands and counts the
// DISTINCT evaluated candidates. quality is that mean BUT only when the vendor has at least ten evaluated
// candidates (the k floor over DISTINCT PEOPLE, not submissions, so a thin vendor's quality is null, not a re
// identifying average, and a handful of people submitted many times can never clear the floor). An unevaluated
// submission (no candidate_evaluations row) contributes NOTHING to quality: per_candidate INNER joins the
// evaluation, so a NULL band can never dilute the average as an implicit zero. The PerformanceScore weights
// fill 0.35, quality 0.30 (a neutral 0.5 when quality is suppressed), submit speed 0.20 (one minus the time to
// submit normalized against a thirty day cap), interview 0.15; the weights sum to one. Every ratio is numeric,
// exact for the shopspring/decimal scan. A vendor with no submissions has null metrics (the NULLIF guards) and
// a low score resting on the neutral quality. It reads the frozen candidate_evaluations band, NEVER recomputing
// a per candidate score.
const vendorPerformanceScorecardSQL = `
WITH sub AS (
SELECT
v.id AS vendor_id,
v.name AS vendor_name,
v.vendor_type AS vendor_type,
count(vs.vendor_id) AS submissions,
(count(*) FILTER (WHERE vs.status = 'placed'))::numeric
/ NULLIF(count(vs.vendor_id), 0) AS fill_rate,
(count(*) FILTER (WHERE vs.status IN ('interviewed', 'placed')))::numeric
/ NULLIF(count(vs.vendor_id), 0) AS interview_rate,
avg(EXTRACT(EPOCH FROM (vs.submitted_at - r.created_at)) / 86400.0) AS avg_time_to_submit_days
FROM staffing_vendors v
LEFT JOIN vendor_submissions vs
ON vs.organization_id = v.organization_id AND vs.vendor_id = v.id
LEFT JOIN requirements r
ON r.organization_id = vs.organization_id AND r.id = vs.requirement_id
GROUP BY v.id, v.name, v.vendor_type
),
per_candidate AS (
SELECT DISTINCT ON (v.id, ce.candidate_id)
v.id AS vendor_id,
CASE ce.band WHEN 'green' THEN 1.0 WHEN 'yellow' THEN 0.5 WHEN 'red' THEN 0.0 END AS band_value
FROM staffing_vendors v
JOIN vendor_submissions vs
ON vs.organization_id = v.organization_id AND vs.vendor_id = v.id
JOIN candidate_evaluations ce
ON ce.organization_id = vs.organization_id AND ce.candidate_id = vs.candidate_id AND ce.requirement_id = vs.requirement_id
ORDER BY v.id, ce.candidate_id, ce.evaluated_at DESC, ce.requirement_id DESC
),
qual AS (
SELECT vendor_id,
count(*) AS evaluated_candidates,
avg(band_value) AS quality
FROM per_candidate
GROUP BY vendor_id
)
SELECT
sub.vendor_id::text AS vendor_id,
sub.vendor_name AS vendor_name,
sub.vendor_type AS vendor_type,
sub.submissions AS submissions,
round(COALESCE(sub.fill_rate, 0), 4) AS fill_rate,
round(COALESCE(sub.interview_rate, 0), 4) AS interview_rate,
round(COALESCE(sub.avg_time_to_submit_days, 0), 2) AS avg_time_to_submit_days,
CASE WHEN COALESCE(qual.evaluated_candidates, 0) >= 10 THEN round(qual.quality, 4) ELSE NULL END AS quality,
round(
COALESCE(sub.fill_rate, 0) * 0.35
+ COALESCE(CASE WHEN COALESCE(qual.evaluated_candidates, 0) >= 10 THEN qual.quality ELSE NULL END, 0.5) * 0.30
+ (1 - LEAST(1, COALESCE(sub.avg_time_to_submit_days, 30) / 30.0)) * 0.20
+ COALESCE(sub.interview_rate, 0) * 0.15
, 4) AS performance_score
FROM sub
LEFT JOIN qual ON qual.vendor_id = sub.vendor_id
ORDER BY performance_score DESC, sub.vendor_name, sub.vendor_id`
// vendorQualityDistributionSQL returns, per vendor, the headcount of DISTINCT submitted candidates in each
// frozen readiness band the engine ALREADY assigned. It reads candidate_evaluations.band on a join, never
// recomputing. RLS scoped. The per_candidate CTE reduces each (vendor, candidate) to ONE representative band,
// the candidate's MOST RECENT evaluation among that vendor's submissions (DISTINCT ON with an evaluated_at then
// requirement_id total order), so a candidate the vendor submitted for several requirements with DIFFERENT
// bands lands in EXACTLY ONE cell, never several. That true partition is what the k suppression relies on: if a
// person could appear in two bands, the per vendor cells would not sum to the distinct candidate count and a
// published cell plus a suppressed cell could leak the overlap. It returns only the (vendor, band) pairs that
// have evaluated candidates; the bench executor seeds the complete {green, yellow, red} partition per vendor
// with true zeros before suppression, so complementary suppression is well defined. Deterministic ORDER BY
// vendor name, id, band.
const vendorQualityDistributionSQL = `
WITH per_candidate AS (
SELECT DISTINCT ON (v.id, ce.candidate_id)
v.id::text AS vendor_id,
v.name AS vendor_name,
ce.band AS band
FROM staffing_vendors v
JOIN vendor_submissions vs
ON vs.organization_id = v.organization_id AND vs.vendor_id = v.id
JOIN candidate_evaluations ce
ON ce.organization_id = vs.organization_id AND ce.candidate_id = vs.candidate_id AND ce.requirement_id = vs.requirement_id
ORDER BY v.id, ce.candidate_id, ce.evaluated_at DESC, ce.requirement_id DESC
)
SELECT
vendor_id,
vendor_name,
band,
count(*) AS headcount
FROM per_candidate
GROUP BY vendor_id, vendor_name, band
ORDER BY vendor_name, vendor_id, band`
// VendorQualityRow is one suppressed (vendor, band) cell of the vendor quality distribution: the vendor and the
// frozen band, plus the small cell suppressed headcount Cell (the raw Count is never surfaced; the caller
// renders RenderCount so a sub k count shows the sentinel).
type VendorQualityRow struct {
VendorID string
VendorName string
Band string
Cell Cell
}
// VendorQualityDistribution runs the vendor_quality_distribution bench intent under RLS, reading the ALREADY
// AUDITED candidate_evaluations band of each vendor's submitted candidates and reporting the per vendor band
// distribution the frozen engine ALREADY decided. It NEVER recomputes a score. For EACH vendor it builds the
// complete {green, yellow, red} partition with true zeros, then pipes THAT vendor's three cells through Suppress
// with the caller supplied threshold k. Because the SQL reduces each candidate to ONE representative band, every
// vendor's three bands sum to that vendor's DISTINCT evaluated candidate count (a true partition), so
// complementary suppression is well defined per vendor. The caller renders RenderCount, never the raw Count.
func VendorQualityDistribution(ctx context.Context, db *tenant.DB, k int) ([]VendorQualityRow, error) {
if db == nil {
return nil, errors.New("ai: no database")
}
type vendorKey struct{ id, name string }
var order []vendorKey
seen := map[string]bool{}
counts := map[string]map[string]int{} // vendor id -> band -> headcount
err := db.RunInTenantTx(ctx, func(ctx context.Context, tx pgx.Tx) error {
if err := GuardReadOnlyQuery(vendorQualityDistributionSQL, ReadAllowlist); err != nil {
return err
}
rows, err := tx.Query(ctx, vendorQualityDistributionSQL)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var vid, vname, band string
var headcount int
if err := rows.Scan(&vid, &vname, &band, &headcount); err != nil {
return err
}
if !seen[vid] {
seen[vid] = true
order = append(order, vendorKey{vid, vname})
counts[vid] = map[string]int{}
}
if _, ok := counts[vid][band]; ok || bandIsKnown(band) {
counts[vid][band] = headcount
}
}
return rows.Err()
})
if err != nil {
return nil, err
}
var out []VendorQualityRow
for _, v := range order {
cells := make([]Cell, len(readinessBands))
for i, b := range readinessBands {
cells[i] = Cell{Key: []string{v.id, b}, Count: counts[v.id][b]}
}
cells = Suppress(cells, k)
for i, b := range readinessBands {
out = append(out, VendorQualityRow{VendorID: v.id, VendorName: v.name, Band: b, Cell: cells[i]})
}
}
return out, nil
}
// bandIsKnown reports whether a scanned band is one of the three frozen readiness bands, so an unexpected value
// is ignored rather than added to the partition.
func bandIsKnown(band string) bool {
for _, b := range readinessBands {
if b == band {
return true
}
}
return false
}
Mathematical records from this source
0102
vendor
This relation defines - vendor_quality_distribution: a BENCH intent, k in the source section “vendor”. It specifies the displayed relationship between the named quantities or…
vendor
This relation defines IntentVendorQualityDistribution is the registered name of the k in the source section “vendor”. It specifies the displayed relationship between the named…