Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/modules/ai/graph_teaming.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 720d47fae476b0d38477ae4401697d372b1ea2bc |
| Branch | feature/opportunity-to-outcome |
| Path or archive member | internal/modules/ai/graph_teaming.go |
| Original SHA-256 | 61f6839ce2d8cdabec6fc97f917d7da6dfbe7ab91c1df81ae1535a3ec1ea1149 |
| Git blob SHA | a02aafd9c6bd6a928436912bd3ce0181a91b367e |
| Reading-copy SHA-256 | 61f6839ce2d8cdabec6fc97f917d7da6dfbe7ab91c1df81ae1535a3ec1ea1149 |
Every recorded occurrence
These 12 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/graph_teaming.go | feature/opportunity-to-outcome | 720d47fae476b0d38477ae4401697d372b1ea2bc | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_teaming.go | collision-deconfliction | 4ee5a09972608ed89f8377c6665542d5f450f0a6 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_teaming.go | m5-price-to-win | 41000ad30fff4129fdaf5ec84e15ba14c27d4890 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_teaming.go | master | 1e9249840a4302ec82e123b9c9cb6f75badea3e4 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_teaming.go | mag-substrate | b82c7100a492dec67dfd1eda6b3f53def859be35 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_teaming.go | time-to-fill-survival | b66e1e505863d4f17741a4b9ede73509e45bd6c6 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_teaming.go | graphrag-foundation | b0ddeb9f633fd138d09309d1a17b7d13bc3a67f7 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_teaming.go | presentation-readiness | eb574acaa29d71ce4d0f852572e87c275c79fdb7 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_teaming.go | vendor-intelligence | fe9796a19c261951d3f2565206c0c1b5ac294465 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_teaming.go | phase2-connectors | e038d4c687aec16f8bd9bc4d907eea14d91f8704 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_teaming.go | feature/aioe-upgrade | 2178e7298d0e5d9af83afa12402fd587f0ba6ed8 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_teaming.go | capture-readiness | bae6af7cb2997e819174a9e9cfff5e16067a1975 | Open original |
Recovered source transcript
Download complete source text ↓404 lines · 21.6 KB
package ai
// G2 public vendor co occurrence and teaming field graph. This file adds the partner and competitor field
// intelligence as FIXED, named, parameterized QueryIntents over the GLOBAL PUBLIC market tables only:
// usaspending_awards (the federal award cache, the prime recipient of record per segment) and
// opportunity_records (the unified catalog, the finer (agency, naics, psc, set_aside, pop_state) tuple and
// the incumbent context). Where G1 reads the competitor field of a segment, G9 the recompete lifecycle, and
// G7 the buyer pattern, G2 reads the VENDOR co occurrence plane: which UEIs appear together in a segment (the
// likely teaming and competitor field), whether a given partner tends to team with the tenant's own past
// performance or to hold the prime against it, and the co occurring vendor neighborhood around the incumbent
// of an expiring contract (the likely teaming field before the RFP drops).
//
// PUBLIC DATA ONLY, the Phase 1 scope boundary, enforced by construction. The tenant gap intersection
// questions (which partner closes OUR bench gap, which join a requirement or a candidate table) are DEFERRED
// to Phase 2 and G6, when the person plane comes online under RLS. NO intent in this file reads a tenant
// table (no requirements, no candidates, no bench, never the eeo schema). The only tenant side these reads
// touch is the PUBLIC is_galapagos self tag on usaspending_awards, a column on the public award catalog, not
// a tenant table read; the tenant's own past performance is just another public award row flagged
// is_galapagos.
//
// The graph NEVER computes a match score, a band, or any employment decision number. A co occurrence count, a
// dollar concentration, a team versus compete ratio are MARKET aggregates, structure the capture team reads,
// and they are explicitly NOT match scores; the frozen engine in internal/modules/matching/engine stays the
// only scorer and is untouched. The model never authors this SQL: every statement is a static, reviewed
// constant, registered in the closed Registry, asserted by ValidateRegistry at boot, and re guarded by
// GuardReadOnlyQuery on every execution.
//
// Honesty mandates from the GraphRAG review, encoded by construction:
// - usaspending_awards carries NO psc, NO set_aside, and NO pop_state column, so the awards co occurrence
// keys ONLY on (awarding_agency, naics_code). The finer five tuple precision is opportunity_records
// sourced and each edge carries its source_table so a reader never mistakes an award keyed on (agency,
// naics) for a slice the award table cannot support.
// - every vendor edge is labeled prime-of-record (a constant label), never the staffing subcontractor,
// because the sub award labor reality is absent from the public award data; the prime recipient or the
// catalog recorded incumbent is the only honest vendor the data supports.
// - the team versus compete ratio is computed from PUBLIC co occurrence alone (the is_galapagos self tag on
// the public awards), never a tenant table; a market where the partner co occurs with an is_galapagos row
// reads as same side teaming context, a market where the partner holds the prime in the tenant core NAICS
// reads as compete context.
//
// Decimal, not float. Every dollar figure (award_amount, estimated_value), every concentration, and the team
// versus compete ratio is Postgres numeric end to end and is scanned into shopspring/decimal in Go, NEVER
// float64 and NEVER cast through ::float8: a surfaced market figure is a decision adjacent number and the
// platform's decimal rule applies. The ratio is a numeric over a numeric guarded with NULLIF so a partner
// with no compete market divides safely (a null ratio, never a divide by zero), and nullable text columns are
// COALESCEd and nullable numerics are kept as decimal pointers (the G9 NULL discipline), so a row with a
// missing piece scans cleanly.
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/shopspring/decimal"
"github.com/missionworkforce/missionmatch/internal/platform/tenant"
)
// The registered names of the three G2 teaming intents. Each is a FIXED entry in the closed Registry in
// tag.go, so ValidateRegistry asserts it at boot and ExecuteIntent runs it through the same guard, tenant
// transaction, and audit posture as every other TAG intent.
const (
IntentVendorCooccurrenceField = "vendor_cooccurrence_field"
IntentPartnerOrCompetitorRatio = "partner_or_competitor_ratio"
IntentRecompeteTeamingField = "recompete_teaming_field"
)
// vendorCooccurrenceFieldSQL is the FIXED co occurrence field. Given an agency ($1) and a NAICS ($2), it
// returns the set of recipient and incumbent UEIs that co occur in that segment, so the likely teaming and
// competitor field is visible. The awards arm keys ONLY on (awarding_agency, naics_code), the only keys
// usaspending_awards carries, and the opportunity_records arm contributes the finer tuple sourced incumbent
// context for the SAME (agency, naics); each row carries its source_table so an award keyed on (agency,
// naics) is never conflated with an opportunity sourced slice. Every vendor edge is labeled prime-of-record.
//
// Honesty and determinism, line by line:
// - the awards arm aggregates the segment award dollars per recipient UEI (a numeric, the dollar
// concentration signal), keyed only on (awarding_agency, naics_code), with the contributing award count
// as evidence and is_galapagos flagging the tenant's own past performance,
// - the opportunity_records arm contributes the catalog recorded incumbent UEIs in the SAME (agency, naics),
// labeled opportunity_records sourced, so the finer catalog context appears without pretending the award
// table carries the slice,
// - a UEI is dropped only when it is the empty string (no honest vendor identity), so the field is the real
// co occurring set,
// - the result is ordered by the dollar concentration DESC (the awards signal, a numeric) with a UEI
// tiebreaker, a total order over the grouped rows, so the same inputs yield byte identical rows.
const vendorCooccurrenceFieldSQL = `
WITH award_field AS (
SELECT
a.recipient_uei AS vendor_uei,
max(a.recipient_name) AS vendor_name,
bool_or(a.is_galapagos) AS is_galapagos,
count(*) AS edge_count,
sum(a.award_amount)::numeric AS segment_dollars
FROM usaspending_awards a
WHERE a.awarding_agency = $1
AND a.naics_code = $2
AND a.recipient_uei <> ''
AND a.award_amount IS NOT NULL
GROUP BY a.recipient_uei
), opp_field AS (
SELECT
o.incumbent_uei AS vendor_uei,
max(o.incumbent_name) AS vendor_name,
count(*) AS edge_count
FROM opportunity_records o
WHERE o.agency = $1
AND o.naics = $2
AND o.incumbent_uei <> ''
GROUP BY o.incumbent_uei
)
SELECT
af.vendor_uei AS vendor_uei,
coalesce(af.vendor_name, '') AS vendor_name,
af.is_galapagos AS is_galapagos,
'usaspending_awards' AS source_table,
'prime-of-record' AS vendor_role,
af.edge_count AS edge_count,
af.segment_dollars AS segment_dollars
FROM award_field af
UNION ALL
SELECT
of2.vendor_uei AS vendor_uei,
coalesce(of2.vendor_name, '') AS vendor_name,
false AS is_galapagos,
'opportunity_records' AS source_table,
'prime-of-record' AS vendor_role,
of2.edge_count AS edge_count,
NULL::numeric AS segment_dollars
FROM opp_field of2
ORDER BY segment_dollars DESC NULLS LAST, source_table, vendor_uei`
// partnerOrCompetitorRatioSQL is the FIXED team versus compete read. Given a target partner UEI ($1) and a
// core NAICS ($2), it returns, from PUBLIC co occurrence alone, the count of (agency, naics) markets where the
// partner co occurs with the tenant's own past performance (an is_galapagos award in the same (agency, naics)
// segment, the same side teaming context) versus the count of markets where the partner holds the prime in
// the tenant core NAICS without a tenant award present (the compete context), expressed as a team versus
// compete ratio. It reads ONLY usaspending_awards and the is_galapagos self tag, NO tenant table.
//
// Honesty and decimal, line by line:
// - the markets are the distinct (awarding_agency, naics_code) segments the partner appears in within the
// core NAICS, the only keys usaspending_awards carries,
// - a team market is one where the same segment also carries an is_galapagos award (the tenant's own past
// performance co occurs), a same side signal from the public flag, never a tenant table,
// - a compete market is one in the core NAICS where the partner holds the prime and no is_galapagos award is
// present, the partner standing on the other side,
// - the ratio is team_markets over compete_markets in numeric, guarded with NULLIF(compete_markets, 0) so a
// partner with no compete market divides to a null ratio rather than a divide by zero, never a float,
// - the single summary row is a total order on its own (one row), so the output is byte stable.
const partnerOrCompetitorRatioSQL = `
WITH partner_markets AS (
SELECT DISTINCT a.awarding_agency, a.naics_code
FROM usaspending_awards a
WHERE a.recipient_uei = $1
AND a.naics_code = $2
AND a.recipient_uei <> ''
), galapagos_markets AS (
SELECT DISTINCT a.awarding_agency, a.naics_code
FROM usaspending_awards a
WHERE a.is_galapagos = true
AND a.naics_code = $2
), classified AS (
SELECT
pm.awarding_agency,
pm.naics_code,
(gm.awarding_agency IS NOT NULL) AS is_team_market
FROM partner_markets pm
LEFT JOIN galapagos_markets gm
ON gm.awarding_agency = pm.awarding_agency
AND gm.naics_code = pm.naics_code
)
SELECT
$1::text AS partner_uei,
'prime-of-record' AS partner_role,
count(*) FILTER (WHERE c.is_team_market)::int AS team_markets,
count(*) FILTER (WHERE NOT c.is_team_market)::int AS compete_markets,
(count(*) FILTER (WHERE c.is_team_market))::numeric
/ NULLIF((count(*) FILTER (WHERE NOT c.is_team_market))::numeric, 0) AS team_vs_compete_ratio
FROM classified c`
// recompeteTeamingFieldSQL is the FIXED recompete teaming field. Given an agency ($1) and a forward window in
// months ($2), it returns the opportunities whose pop_end_date falls inside the forward window, each carrying
// its incumbent UEI, joined to that incumbent's co occurring vendor neighborhood in the same NAICS over the
// public awards, the likely teaming field before the RFP drops. It reads ONLY opportunity_records and
// usaspending_awards, NO tenant table.
//
// Honesty and determinism, line by line:
// - the expiring opportunities are opportunity_records whose pop_end_date is inside the forward window for
// the agency, the recompete clock surface,
// - the incumbent neighborhood is the set of recipient UEIs that co occur with the opportunity's incumbent
// in the SAME naics over usaspending_awards (the neighbor and the incumbent both hold an award in that
// (agency, naics) segment), keyed only on (awarding_agency, naics_code), the only keys the award carries,
// - the incumbent's own UEI is excluded from its neighborhood so a vendor is never listed as its own
// neighbor,
// - every neighbor edge is labeled prime-of-record and carries its co occurring segment dollars (a numeric,
// the concentration signal), is_galapagos flagging the tenant's own past performance,
// - the result is ordered by pop_end_date then the opportunity id then the neighbor UEI, a total order over
// the rows, so the same inputs yield byte identical rows.
const recompeteTeamingFieldSQL = `
WITH expiring AS (
SELECT
o.id,
o.title,
o.agency,
o.naics,
o.incumbent_uei,
o.incumbent_name,
o.pop_end_date
FROM opportunity_records o
WHERE o.agency = $1
AND o.incumbent_uei <> ''
AND o.pop_end_date IS NOT NULL
AND o.pop_end_date >= now()::date
AND o.pop_end_date <= (now()::date + ($2::int * 30))
), neighborhood AS (
SELECT
e.id AS opportunity_id,
a.recipient_uei AS neighbor_uei,
max(a.recipient_name) AS neighbor_name,
bool_or(a.is_galapagos) AS is_galapagos,
count(*) AS edge_count,
sum(a.award_amount)::numeric AS neighbor_dollars
FROM expiring e
JOIN usaspending_awards a
ON a.naics_code = e.naics
AND a.recipient_uei <> ''
AND a.recipient_uei <> e.incumbent_uei
AND a.award_amount IS NOT NULL
AND EXISTS (
SELECT 1 FROM usaspending_awards inc
WHERE inc.awarding_agency = a.awarding_agency
AND inc.naics_code = a.naics_code
AND inc.recipient_uei = e.incumbent_uei
)
GROUP BY e.id, a.recipient_uei
)
SELECT
e.id::text AS opportunity_id,
e.title AS opportunity_title,
e.agency AS agency,
e.naics AS naics,
e.incumbent_uei AS incumbent_uei,
coalesce(e.incumbent_name, '') AS incumbent_name,
'prime-of-record' AS incumbent_role,
coalesce(n.neighbor_uei, '') AS neighbor_uei,
coalesce(n.neighbor_name, '') AS neighbor_name,
coalesce(n.is_galapagos, false) AS neighbor_is_galapagos,
coalesce(n.edge_count, 0) AS neighbor_edge_count,
n.neighbor_dollars AS neighbor_dollars,
e.pop_end_date AS pop_end_date
FROM expiring e
LEFT JOIN neighborhood n ON n.opportunity_id = e.id
ORDER BY e.pop_end_date, e.id, neighbor_uei`
// CooccurrenceRow is one vendor in a co occurrence field: the vendor UEI, whether it is the tenant's own past
// performance (is_galapagos), the source table the edge came from (so an award keyed on (agency, naics) is
// never conflated with an opportunity sourced slice), the prime-of-record label, the contributing edge count
// (the evidence), and the segment dollars (a decimal pointer, null on an opportunity sourced edge that
// carries no award dollars). It is pure market structure: there is no match score and no employment decision
// number on it.
type CooccurrenceRow struct {
VendorUEI string `json:"vendor_uei"`
VendorName string `json:"vendor_name"`
IsGalapagos bool `json:"is_galapagos"`
SourceTable string `json:"source_table"`
VendorRole string `json:"vendor_role"`
EdgeCount int `json:"edge_count"`
SegmentDollars *decimal.Decimal `json:"segment_dollars,omitempty"`
}
// VendorCooccurrenceField runs the registered vendor_cooccurrence_field intent for an agency and a NAICS,
// scanning the segment dollars into a decimal pointer (null on the opportunity sourced edge). The awards arm
// keys on (agency, naics) the only way usaspending_awards supports; every edge is labeled prime-of-record by
// construction. The generic ExecuteIntent path remains available to the narration layer.
func VendorCooccurrenceField(ctx context.Context, db *tenant.DB, agency, naics string) ([]CooccurrenceRow, error) {
if db == nil {
return nil, errors.New("ai: no database")
}
var out []CooccurrenceRow
err := db.RunInTenantTx(ctx, func(ctx context.Context, tx pgx.Tx) error {
if err := GuardReadOnlyQuery(vendorCooccurrenceFieldSQL, ReadAllowlist); err != nil {
return err
}
rows, err := tx.Query(ctx, vendorCooccurrenceFieldSQL, agency, naics)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var r CooccurrenceRow
if err := rows.Scan(&r.VendorUEI, &r.VendorName, &r.IsGalapagos, &r.SourceTable,
&r.VendorRole, &r.EdgeCount, &r.SegmentDollars); err != nil {
return err
}
out = append(out, r)
}
return rows.Err()
})
return out, err
}
// PartnerRatioRow is the team versus compete summary for one partner UEI in the tenant core NAICS, from PUBLIC
// co occurrence alone: the partner labeled prime-of-record, the count of team markets (where the partner co
// occurs with the tenant's own is_galapagos past performance, the same side context), the count of compete
// markets (where the partner holds the prime without the tenant present), and the team versus compete ratio (a
// decimal pointer, null when the partner has no compete market, the divide by zero guard). It is pure market
// structure: there is no match score and no employment decision number on it, and it reads no tenant table.
type PartnerRatioRow struct {
PartnerUEI string `json:"partner_uei"`
PartnerRole string `json:"partner_role"`
TeamMarkets int `json:"team_markets"`
CompeteMarkets int `json:"compete_markets"`
TeamVsCompeteRatio *decimal.Decimal `json:"team_vs_compete_ratio,omitempty"`
}
// PartnerOrCompetitorRatio runs the registered partner_or_competitor_ratio intent for a partner UEI and a core
// NAICS, scanning the ratio into a decimal pointer. The ratio is team markets over compete markets, guarded
// with NULLIF so a partner with no compete market divides to a null ratio rather than a divide by zero. It
// reads ONLY usaspending_awards and the is_galapagos self tag, NO tenant table.
func PartnerOrCompetitorRatio(ctx context.Context, db *tenant.DB, partnerUEI, naics string) ([]PartnerRatioRow, error) {
if db == nil {
return nil, errors.New("ai: no database")
}
var out []PartnerRatioRow
err := db.RunInTenantTx(ctx, func(ctx context.Context, tx pgx.Tx) error {
if err := GuardReadOnlyQuery(partnerOrCompetitorRatioSQL, ReadAllowlist); err != nil {
return err
}
rows, err := tx.Query(ctx, partnerOrCompetitorRatioSQL, partnerUEI, naics)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var r PartnerRatioRow
if err := rows.Scan(&r.PartnerUEI, &r.PartnerRole, &r.TeamMarkets, &r.CompeteMarkets, &r.TeamVsCompeteRatio); err != nil {
return err
}
out = append(out, r)
}
return rows.Err()
})
return out, err
}
// TeamingFieldRow is one opportunity plus one neighbor in the recompete teaming field: the expiring
// opportunity, its incumbent UEI labeled prime-of-record, one co occurring neighbor UEI (also prime-of-record,
// the empty string when the incumbent has no neighborhood), whether the neighbor is the tenant's own past
// performance (is_galapagos), the contributing edge count, the neighbor's co occurring segment dollars (a
// decimal pointer), and the pop_end_date that puts the opportunity in the window. It carries no score and
// reads no tenant table.
type TeamingFieldRow struct {
OpportunityID string `json:"opportunity_id"`
OpportunityTitle string `json:"opportunity_title"`
Agency string `json:"agency"`
NAICS string `json:"naics"`
IncumbentUEI string `json:"incumbent_uei"`
IncumbentName string `json:"incumbent_name"`
IncumbentRole string `json:"incumbent_role"`
NeighborUEI string `json:"neighbor_uei"`
NeighborName string `json:"neighbor_name"`
NeighborIsGalapagos bool `json:"neighbor_is_galapagos"`
NeighborEdgeCount int `json:"neighbor_edge_count"`
NeighborDollars *decimal.Decimal `json:"neighbor_dollars,omitempty"`
PopEndDate string `json:"pop_end_date"`
}
// RecompeteTeamingField runs the registered recompete_teaming_field intent for an agency and a window in
// months, scanning the neighbor dollars into a decimal pointer. The window is clamped to the bounded range by
// the registered ParamInt Min and Max on the structured path; this typed helper clamps with
// ClampRecompeteWindowMonths so the scan is always bounded. It reads ONLY opportunity_records and
// usaspending_awards, NO tenant table.
func RecompeteTeamingField(ctx context.Context, db *tenant.DB, agency string, windowMonths int) ([]TeamingFieldRow, error) {
if db == nil {
return nil, errors.New("ai: no database")
}
var out []TeamingFieldRow
err := db.RunInTenantTx(ctx, func(ctx context.Context, tx pgx.Tx) error {
if err := GuardReadOnlyQuery(recompeteTeamingFieldSQL, ReadAllowlist); err != nil {
return err
}
rows, err := tx.Query(ctx, recompeteTeamingFieldSQL, agency, ClampRecompeteWindowMonths(windowMonths))
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var (
r TeamingFieldRow
popEnd pgtype.Date
)
if err := rows.Scan(&r.OpportunityID, &r.OpportunityTitle, &r.Agency, &r.NAICS,
&r.IncumbentUEI, &r.IncumbentName, &r.IncumbentRole, &r.NeighborUEI, &r.NeighborName,
&r.NeighborIsGalapagos, &r.NeighborEdgeCount, &r.NeighborDollars, &popEnd); err != nil {
return err
}
r.PopEndDate = isoDate(popEnd)
out = append(out, r)
}
return rows.Err()
})
return out, err
}