Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/modules/ai/graph_lifecycle.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 720d47fae476b0d38477ae4401697d372b1ea2bc |
| Branch | feature/opportunity-to-outcome |
| Path or archive member | internal/modules/ai/graph_lifecycle.go |
| Original SHA-256 | c7dfa628181ce77620bee4f88e310447421a865deea5b3c78c4341d32413390a |
| Git blob SHA | 59277588a78ebdbb051548effaa66f1b750546b0 |
| Reading-copy SHA-256 | c7dfa628181ce77620bee4f88e310447421a865deea5b3c78c4341d32413390a |
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_lifecycle.go | feature/opportunity-to-outcome | 720d47fae476b0d38477ae4401697d372b1ea2bc | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_lifecycle.go | collision-deconfliction | 4ee5a09972608ed89f8377c6665542d5f450f0a6 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_lifecycle.go | m5-price-to-win | 41000ad30fff4129fdaf5ec84e15ba14c27d4890 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_lifecycle.go | master | 1e9249840a4302ec82e123b9c9cb6f75badea3e4 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_lifecycle.go | mag-substrate | b82c7100a492dec67dfd1eda6b3f53def859be35 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_lifecycle.go | time-to-fill-survival | b66e1e505863d4f17741a4b9ede73509e45bd6c6 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_lifecycle.go | graphrag-foundation | b0ddeb9f633fd138d09309d1a17b7d13bc3a67f7 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_lifecycle.go | presentation-readiness | eb574acaa29d71ce4d0f852572e87c275c79fdb7 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_lifecycle.go | vendor-intelligence | fe9796a19c261951d3f2565206c0c1b5ac294465 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_lifecycle.go | phase2-connectors | e038d4c687aec16f8bd9bc4d907eea14d91f8704 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_lifecycle.go | feature/aioe-upgrade | 2178e7298d0e5d9af83afa12402fd587f0ba6ed8 | Open original |
| Janady13/missionmatch / internal/modules/ai/graph_lifecycle.go | capture-readiness | bae6af7cb2997e819174a9e9cfff5e16067a1975 | Open original |
Recovered source transcript
Download complete source text ↓387 lines · 19.0 KB
package ai
// G9 recompete lifecycle and foresight graph. This file adds the federal lifecycle traversals as FIXED,
// named, parameterized QueryIntents over the GLOBAL public market tables: opportunity_records (the unified
// catalog whose record_kind partitions a record into forecast, active_solicitation, and contract_award),
// usaspending_awards (the federal award cache), federal_mission_tags, and mission_areas. Where G1 reads the
// competitor field and the incumbent of record, G9 reads the lifecycle a single procurement walks through
// time: the forecast that announces it, the solicitation that opens it, and the predecessor award it
// recompetes, plus the forward windows a capture team watches (the recompete clock and the forecast
// runway).
//
// The graph NEVER computes a match score, a band, or any employment decision number. A prior award amount,
// a forecast lead time, a recompete window are MARKET facts, 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:
// - the procurement succession walk lives ENTIRELY in opportunity_records, the only table that carries the
// full (agency, naics, psc, pop_state, set_aside) segment tuple plus the dedupe_key that links the
// phases, so the succession never reaches for a column usaspending_awards lacks. It is a FIXED set of
// LEFT JOINs (forecast to solicitation to predecessor award is at most three phases), NOT a recursive
// CTE, exactly as the determinism mandate prefers when the data already carries the keys.
// - the recompete clock UNIONs opportunity_records and usaspending_awards; the award arm keys ONLY on
// awarding_agency (the only agency key the award table carries), each row carrying its source_table so an
// award row and an opportunity row are never conflated.
// - every incumbent edge is labeled prime-of-record (a constant label column), 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 incumbent the data supports.
//
// Decimal, not float. Every dollar figure (estimated_value, award_amount) 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.
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 G9 lifecycle 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 (
IntentProcurementSuccession = "procurement_succession"
IntentRecompeteClock = "recompete_clock"
IntentForecastRunway = "forecast_runway"
)
// procurementSuccessionSQL is the FIXED lifecycle succession. Given an active_solicitation opportunity_records
// id ($1), it walks the dedupe_key succession within the SAME (agency, naics, psc, pop_state, set_aside)
// segment to the predecessor contract_award and, when present, the announcing forecast, returning whether the
// solicitation is new or a recompete, the prior award_amount (the predecessor contract_award estimated_value,
// decimal) and the prior prime UEI labeled prime-of-record, with the cited record ids and dates.
//
// It is a FIXED depth set of LEFT JOINs, NOT a recursive CTE: a procurement passes through at most three
// phases (forecast, solicitation, award), and every phase carries the same dedupe_key and the same full
// segment tuple in opportunity_records, so a recursive walk would add nothing but a determinism risk. The
// whole walk lives in opportunity_records, the only table that carries the full (agency, naics, psc,
// pop_state, set_aside) tuple, so the succession never reaches for a column usaspending_awards lacks.
//
// Honesty and determinism, line by line:
// - the anchor is the named solicitation; the predecessor award and the forecast are LEFT JOINed on the
// shared dedupe_key AND the full five tuple, so a missing predecessor leaves is_recompete false (a new
// start) rather than dropping the row,
// - the predecessor award join excludes the anchor row itself (a.id <> o.id) so a solicitation never
// matches itself, and picks the most recent predecessor by award_date with the id tiebreaker, a total
// order for a byte stable single row,
// - is_recompete is true exactly when a predecessor contract_award was found, is_new is its negation,
// - the prior award amount is the predecessor estimated_value (numeric, scanned into decimal, never float),
// and the prior prime UEI is labeled with the constant prime-of-record.
const procurementSuccessionSQL = `
WITH anchor AS (
SELECT
o.id, o.dedupe_key, o.agency, o.naics, o.psc, o.pop_state, o.set_aside,
o.title, o.posted_date, o.response_deadline
FROM opportunity_records o
WHERE o.id = $1
AND o.record_kind = 'active_solicitation'
), predecessor AS (
SELECT
a.id, a.incumbent_uei, a.incumbent_name, a.estimated_value, a.award_date
FROM opportunity_records a
JOIN anchor an
ON a.dedupe_key = an.dedupe_key
AND an.dedupe_key <> ''
AND a.agency = an.agency
AND a.naics = an.naics
AND a.psc = an.psc
AND a.pop_state = an.pop_state
AND a.set_aside = an.set_aside
AND a.id <> an.id
WHERE a.record_kind = 'contract_award'
ORDER BY a.award_date DESC NULLS LAST, a.id
LIMIT 1
), forecast AS (
SELECT f.id, f.estimated_award_date
FROM opportunity_records f
JOIN anchor an
ON f.dedupe_key = an.dedupe_key
AND an.dedupe_key <> ''
AND f.agency = an.agency
AND f.naics = an.naics
AND f.psc = an.psc
AND f.pop_state = an.pop_state
AND f.set_aside = an.set_aside
AND f.id <> an.id
WHERE f.record_kind = 'forecast'
ORDER BY f.estimated_award_date DESC NULLS LAST, f.id
LIMIT 1
)
SELECT
an.id::text AS solicitation_id,
an.title AS solicitation_title,
an.agency AS agency,
an.naics AS naics,
an.psc AS psc,
an.pop_state AS pop_state,
an.set_aside AS set_aside,
(p.id IS NOT NULL) AS is_recompete,
(p.id IS NULL) AS is_new,
COALESCE(p.id::text, '') AS predecessor_award_id,
COALESCE(p.incumbent_uei, '') AS incumbent_uei,
COALESCE(p.incumbent_name, '') AS incumbent_name,
CASE WHEN p.id IS NOT NULL THEN 'prime-of-record' ELSE '' END AS incumbent_role,
p.estimated_value AS prior_award_amount,
p.award_date AS prior_award_date,
COALESCE(f.id::text, '') AS forecast_id,
f.estimated_award_date AS forecast_estimated_award_date
FROM anchor an
LEFT JOIN predecessor p ON true
LEFT JOIN forecast f ON true`
// recompeteClockSQL is the FIXED recompete clock. Given an agency ($1) and a window in months ($2), it
// returns the opportunity_records and the usaspending_awards whose pop_end_date falls inside the forward
// window for that agency, the takeaway pipeline a capture team watches, each row carrying its source_table so
// an award row and an opportunity row are never conflated, with the incumbent prime named and labeled
// prime-of-record. The two arms are UNIONed and the result is ordered by pop_end_date with a record id
// tiebreaker, a total order for byte stable output.
//
// Both arms key the agency the same honest way: opportunity_records on its agency column, awards on
// awarding_agency (the only agency key the award table carries). estimated_value and award_amount are
// numeric, scanned into decimal, never float.
const recompeteClockSQL = `
SELECT
'opportunity_records' AS source_table,
o.id::text AS record_id,
o.title AS title,
o.agency AS agency,
o.naics AS naics,
o.incumbent_uei AS incumbent_uei,
'prime-of-record' AS incumbent_role,
o.estimated_value AS dollars,
o.value_source AS value_source,
o.pop_end_date AS pop_end_date
FROM opportunity_records o
WHERE o.agency = $1
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))
UNION ALL
SELECT
'usaspending_awards' AS source_table,
a.generated_internal_id AS record_id,
a.description AS title,
a.awarding_agency AS agency,
a.naics_code AS naics,
a.recipient_uei AS incumbent_uei,
'prime-of-record' AS incumbent_role,
a.award_amount AS dollars,
a.source AS value_source,
a.pop_end_date AS pop_end_date
FROM usaspending_awards a
WHERE a.awarding_agency = $1
AND a.pop_end_date IS NOT NULL
AND a.pop_end_date >= now()::date
AND a.pop_end_date <= (now()::date + ($2::int * 30))
ORDER BY pop_end_date, record_id`
// forecastRunwaySQL is the FIXED forecast runway. Given a window in months ($1), it returns the
// opportunity_records of record_kind forecast whose estimated_award_date falls inside the forward window, the
// lead time view, joined to federal_mission_tags and mission_areas for the readable mission area and carrying
// the place of performance state. estimated_value is numeric, scanned into decimal, never float.
//
// The mission area is a LEFT JOIN so an untagged forecast still appears (its mission area is the empty
// string), and federal_mission_tags keys on the record_id as the text form of the opportunity uuid. The
// result is ordered by estimated_award_date with the opportunity id tiebreaker, a total order for byte
// stable output; the id appears in the ORDER BY even though one forecast can carry several mission tags, so a
// multi tag forecast is still totally ordered.
const forecastRunwaySQL = `
SELECT
o.id::text AS forecast_id,
o.title AS title,
o.agency AS agency,
o.naics AS naics,
o.psc AS psc,
o.pop_state AS pop_state,
coalesce(ma.code, '') AS mission_code,
coalesce(ma.name, '') AS mission_area,
o.estimated_value AS dollars,
o.value_source AS value_source,
o.estimated_award_date AS estimated_award_date
FROM opportunity_records o
LEFT JOIN federal_mission_tags fmt
ON fmt.record_table = 'opportunity_records'
AND fmt.record_id = o.id::text
LEFT JOIN mission_areas ma
ON ma.code = fmt.mission_code
WHERE o.record_kind = 'forecast'
AND o.estimated_award_date IS NOT NULL
AND o.estimated_award_date >= now()::date
AND o.estimated_award_date <= (now()::date + ($1::int * 30))
ORDER BY o.estimated_award_date, o.id, mission_code`
// SuccessionRow is the lifecycle of one solicitation: the named solicitation, whether it is a new start or a
// recompete, the predecessor award (if any) with its prior amount (decimal, never float) and prime UEI
// labeled prime-of-record, and the announcing forecast (if any), with the cited record ids and dates. It is
// pure market structure: there is no match score and no employment decision number on it.
type SuccessionRow struct {
SolicitationID string `json:"solicitation_id"`
SolicitationTitle string `json:"solicitation_title"`
Agency string `json:"agency"`
NAICS string `json:"naics"`
PSC string `json:"psc"`
PopState string `json:"pop_state"`
SetAside string `json:"set_aside"`
IsRecompete bool `json:"is_recompete"`
IsNew bool `json:"is_new"`
PredecessorAwardID string `json:"predecessor_award_id,omitempty"`
IncumbentUEI string `json:"incumbent_uei,omitempty"`
IncumbentName string `json:"incumbent_name,omitempty"`
IncumbentRole string `json:"incumbent_role,omitempty"`
PriorAwardAmount *decimal.Decimal `json:"prior_award_amount,omitempty"`
PriorAwardDate string `json:"prior_award_date,omitempty"`
ForecastID string `json:"forecast_id,omitempty"`
ForecastEstAwardDate string `json:"forecast_estimated_award_date,omitempty"`
}
// ProcurementSuccession runs the registered procurement_succession intent for an active_solicitation
// opportunity id and scans the result, carrying the prior award amount as decimal.Decimal (the numeric column
// is scanned directly into decimal, never through float64). The walk is a FIXED set of LEFT JOINs over
// opportunity_records, the only table that carries the full segment tuple, so no phantom award column is ever
// touched. The generic ExecuteIntent path remains available to the narration layer.
func ProcurementSuccession(ctx context.Context, db *tenant.DB, opportunityID string) ([]SuccessionRow, error) {
if db == nil {
return nil, errors.New("ai: no database")
}
var out []SuccessionRow
err := db.RunInTenantTx(ctx, func(ctx context.Context, tx pgx.Tx) error {
if err := GuardReadOnlyQuery(procurementSuccessionSQL, ReadAllowlist); err != nil {
return err
}
rows, err := tx.Query(ctx, procurementSuccessionSQL, opportunityID)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var (
r SuccessionRow
priorDate pgtype.Date
fcastDate pgtype.Date
)
if err := rows.Scan(&r.SolicitationID, &r.SolicitationTitle, &r.Agency, &r.NAICS, &r.PSC,
&r.PopState, &r.SetAside, &r.IsRecompete, &r.IsNew, &r.PredecessorAwardID,
&r.IncumbentUEI, &r.IncumbentName, &r.IncumbentRole, &r.PriorAwardAmount, &priorDate,
&r.ForecastID, &fcastDate); err != nil {
return err
}
r.PriorAwardDate = isoDate(priorDate)
r.ForecastEstAwardDate = isoDate(fcastDate)
out = append(out, r)
}
return rows.Err()
})
return out, err
}
// RecompeteClockRow is one recompete the capture team watches: the source table it came from (so an award is
// never conflated with an opportunity), the record, the agency, the incumbent UEI labeled prime-of-record,
// the dollars (decimal, never float), and the period of performance end that puts it in the window. It
// carries no score.
type RecompeteClockRow struct {
SourceTable string `json:"source_table"`
RecordID string `json:"record_id"`
Title string `json:"title"`
Agency string `json:"agency"`
NAICS string `json:"naics"`
IncumbentUEI string `json:"incumbent_uei"`
IncumbentRole string `json:"incumbent_role"`
Dollars *decimal.Decimal `json:"dollars,omitempty"`
ValueSource string `json:"value_source"`
PopEndDate string `json:"pop_end_date"`
}
// RecompeteClock runs the registered recompete_clock intent for an agency and a window in months, scanning the
// dollars into decimal. 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.
func RecompeteClock(ctx context.Context, db *tenant.DB, agency string, windowMonths int) ([]RecompeteClockRow, error) {
if db == nil {
return nil, errors.New("ai: no database")
}
var out []RecompeteClockRow
err := db.RunInTenantTx(ctx, func(ctx context.Context, tx pgx.Tx) error {
if err := GuardReadOnlyQuery(recompeteClockSQL, ReadAllowlist); err != nil {
return err
}
rows, err := tx.Query(ctx, recompeteClockSQL, agency, ClampRecompeteWindowMonths(windowMonths))
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var (
r RecompeteClockRow
popEnd pgtype.Date
)
if err := rows.Scan(&r.SourceTable, &r.RecordID, &r.Title, &r.Agency, &r.NAICS,
&r.IncumbentUEI, &r.IncumbentRole, &r.Dollars, &r.ValueSource, &popEnd); err != nil {
return err
}
r.PopEndDate = isoDate(popEnd)
out = append(out, r)
}
return rows.Err()
})
return out, err
}
// ForecastRunwayRow is one forecast landing in the forward window: the forecast, its agency and NAICS, its
// mission area (from federal_mission_tags and mission_areas, the empty string when untagged), its place of
// performance state, the dollars (decimal, never float), and the estimated award date that puts it on the
// runway. It carries no score.
type ForecastRunwayRow struct {
ForecastID string `json:"forecast_id"`
Title string `json:"title"`
Agency string `json:"agency"`
NAICS string `json:"naics"`
PSC string `json:"psc"`
PopState string `json:"pop_state"`
MissionCode string `json:"mission_code"`
MissionArea string `json:"mission_area"`
Dollars *decimal.Decimal `json:"dollars,omitempty"`
ValueSource string `json:"value_source"`
EstimatedAwardDate string `json:"estimated_award_date"`
}
// ForecastRunway runs the registered forecast_runway intent for a window in months, scanning the dollars into
// decimal. The window is clamped to the bounded range so the scan is always bounded.
func ForecastRunway(ctx context.Context, db *tenant.DB, windowMonths int) ([]ForecastRunwayRow, error) {
if db == nil {
return nil, errors.New("ai: no database")
}
var out []ForecastRunwayRow
err := db.RunInTenantTx(ctx, func(ctx context.Context, tx pgx.Tx) error {
if err := GuardReadOnlyQuery(forecastRunwaySQL, ReadAllowlist); err != nil {
return err
}
rows, err := tx.Query(ctx, forecastRunwaySQL, ClampRecompeteWindowMonths(windowMonths))
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var (
r ForecastRunwayRow
estDate pgtype.Date
)
if err := rows.Scan(&r.ForecastID, &r.Title, &r.Agency, &r.NAICS, &r.PSC, &r.PopState,
&r.MissionCode, &r.MissionArea, &r.Dollars, &r.ValueSource, &estDate); err != nil {
return err
}
r.EstimatedAwardDate = isoDate(estDate)
out = append(out, r)
}
return rows.Err()
})
return out, err
}