Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/modules/ai/teaching_test.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 720d47fae476b0d38477ae4401697d372b1ea2bc |
| Branch | feature/opportunity-to-outcome |
| Path or archive member | internal/modules/ai/teaching_test.go |
| Original SHA-256 | 7a16177ee9d74017e43d6c03625bcecc044de9d4a2d2f5461ad45783721549dd |
| Git blob SHA | 413c2bc60f9036dade4cc0155387d78a0f727261 |
| Reading-copy SHA-256 | 7a16177ee9d74017e43d6c03625bcecc044de9d4a2d2f5461ad45783721549dd |
Every recorded occurrence
These 14 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/teaching_test.go | feature/opportunity-to-outcome | 720d47fae476b0d38477ae4401697d372b1ea2bc | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | collision-deconfliction | 4ee5a09972608ed89f8377c6665542d5f450f0a6 | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | m5-price-to-win | 41000ad30fff4129fdaf5ec84e15ba14c27d4890 | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | master | 1e9249840a4302ec82e123b9c9cb6f75badea3e4 | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | ws8-teaching-loop | c823f0fb6bb2aba0f39001eb6de96f4000d9321f | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | mag-substrate | b82c7100a492dec67dfd1eda6b3f53def859be35 | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | time-to-fill-survival | b66e1e505863d4f17741a4b9ede73509e45bd6c6 | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | graphrag-foundation | b0ddeb9f633fd138d09309d1a17b7d13bc3a67f7 | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | ws9-bench-ingestion | f585a9ef32fa554663ce1fba9a414a54b6e49cf0 | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | presentation-readiness | eb574acaa29d71ce4d0f852572e87c275c79fdb7 | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | vendor-intelligence | fe9796a19c261951d3f2565206c0c1b5ac294465 | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | phase2-connectors | e038d4c687aec16f8bd9bc4d907eea14d91f8704 | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | feature/aioe-upgrade | 2178e7298d0e5d9af83afa12402fd587f0ba6ed8 | Open original |
| Janady13/missionmatch / internal/modules/ai/teaching_test.go | capture-readiness | bae6af7cb2997e819174a9e9cfff5e16067a1975 | Open original |
Recovered source transcript
Download complete source text ↓307 lines · 14.4 KB
package ai
// teaching_test.go unit tests the WS8 producer's folding and persist logic with NO database. fold is a pure
// function of the stored decision evidence, so a fixture of DecisionRows fully exercises it: the four craft
// patterns, the similarity banding, the decision to outcome mapping, the observation and success counts, the
// engine model version stamp, and the scrub boundary acceptance of every produced signal. persist runs the
// teach and write loop against a fake writer and a gated off inference client, so the live in tenant write
// shape (tenant_id set, dp_applied false) is asserted off box. The full AggregateForOrg path (the RLS scoped
// tenant tx read and the audit on that tx) needs a real Postgres and is covered by the integration test.
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/google/uuid"
"github.com/shopspring/decimal"
"github.com/missionworkforce/missionmatch/internal/modules/matching/engine"
"github.com/missionworkforce/missionmatch/internal/platform/database/dbgen"
"github.com/missionworkforce/missionmatch/internal/platform/inference"
"github.com/missionworkforce/missionmatch/internal/platform/scrub"
)
func dec(s string) decimal.Decimal { return decimal.RequireFromString(s) }
// matchedSkillsJSON builds the matched_skills jsonb the engine persists. The engine SkillMatch type has no
// json tags, so the real column keys are the Go field names; the fixture uses those same keys so the test
// exercises the exact bytes the producer reads in production.
func matchedSkillsJSON(t *testing.T, skills ...map[string]any) []byte {
t.Helper()
b, err := json.Marshal(skills)
if err != nil {
t.Fatalf("marshal matched skills fixture: %v", err)
}
return b
}
func semanticSkill(similarity string) map[string]any {
return map[string]any{"Similarity": similarity, "Exact": false, "Matched": true}
}
func exactSkill() map[string]any {
return map[string]any{"Similarity": "1", "Exact": true, "Matched": true}
}
// findSignal returns the produced signal for a (pattern, band, outcome) bucket, or fails the test.
func findSignal(t *testing.T, sigs []scrub.TeachingSignal, pattern string, band scrub.SimilarityBand, outcome scrub.Outcome) scrub.TeachingSignal {
t.Helper()
for _, s := range sigs {
if s.Pattern == pattern && s.Band == band && s.Outcome == outcome {
return s
}
}
t.Fatalf("no signal for pattern=%s band=%s outcome=%s in %+v", pattern, band, outcome, sigs)
return scrub.TeachingSignal{}
}
// TestFoldAdjacentSkillSubstitution proves a non exact matched skill folds into
// adjacent_skill_substitution_outcome, banded by its cosine similarity, with the right observation and
// success counts, and that an exact match contributes nothing to this pattern.
func TestFoldAdjacentSkillSubstitution(t *testing.T) {
rows := []DecisionRow{
// approved, one high band (0.92) semantic substitution and one exact (ignored here)
{Decision: "approved", RequiredLevel: "none", ClearanceScore: dec("100"), ExperienceScore: dec("100"),
MatchedSkills: matchedSkillsJSON(t, semanticSkill("0.92"), exactSkill())},
// approved, one medium band (0.85) semantic substitution
{Decision: "approved", RequiredLevel: "none", ClearanceScore: dec("100"), ExperienceScore: dec("100"),
MatchedSkills: matchedSkillsJSON(t, semanticSkill("0.85"))},
// rejected, one low band (0.70) semantic substitution
{Decision: "rejected", RequiredLevel: "none", ClearanceScore: dec("100"), ExperienceScore: dec("100"),
MatchedSkills: matchedSkillsJSON(t, semanticSkill("0.70"))},
// approved, another high band (0.95) substitution, so the high+approved bucket reaches two
{Decision: "approved", RequiredLevel: "none", ClearanceScore: dec("100"), ExperienceScore: dec("100"),
MatchedSkills: matchedSkillsJSON(t, semanticSkill("0.95"))},
}
sigs := fold(rows, engine.ModelVersion)
high := findSignal(t, sigs, "adjacent_skill_substitution_outcome", scrub.BandHigh, scrub.OutcomeApproved)
if high.Observations != 2 || high.Successes != 2 {
t.Fatalf("high/approved = obs %d succ %d, want 2/2", high.Observations, high.Successes)
}
medium := findSignal(t, sigs, "adjacent_skill_substitution_outcome", scrub.BandMedium, scrub.OutcomeApproved)
if medium.Observations != 1 || medium.Successes != 1 {
t.Fatalf("medium/approved = obs %d succ %d, want 1/1", medium.Observations, medium.Successes)
}
low := findSignal(t, sigs, "adjacent_skill_substitution_outcome", scrub.BandLow, scrub.OutcomeRejected)
if low.Observations != 1 || low.Successes != 0 {
t.Fatalf("low/rejected = obs %d succ %d, want 1/0 (rejected is not a success)", low.Observations, low.Successes)
}
// The exact match must never appear as a substitution band.
for _, s := range sigs {
if s.Pattern == "adjacent_skill_substitution_outcome" && s.Band == scrub.BandExact {
t.Fatalf("an exact match was folded as a substitution: %+v", s)
}
}
}
// TestFoldClearanceGapOverride proves a required clearance the engine scored zero folds into
// clearance_gap_override_outcome at the unknown band, and that a met clearance or a not required clearance
// folds nothing.
func TestFoldClearanceGapOverride(t *testing.T) {
rows := []DecisionRow{
// required secret, clearance_score 0, approved => an override (success)
{Decision: "approved", RequiredLevel: "secret", ClearanceScore: dec("0"), ExperienceScore: dec("100")},
// required secret, clearance_score 0, rejected => a gap observed but not overridden
{Decision: "rejected", RequiredLevel: "secret", ClearanceScore: dec("0"), ExperienceScore: dec("100")},
// required secret but met (score 100) => not a gap, folds nothing here
{Decision: "approved", RequiredLevel: "secret", ClearanceScore: dec("100"), ExperienceScore: dec("100")},
// no clearance required (none) with score 0 => not a gap (engine passes everyone), folds nothing
{Decision: "approved", RequiredLevel: "none", ClearanceScore: dec("0"), ExperienceScore: dec("100")},
}
sigs := fold(rows, engine.ModelVersion)
app := findSignal(t, sigs, "clearance_gap_override_outcome", scrub.BandUnknown, scrub.OutcomeApproved)
if app.Observations != 1 || app.Successes != 1 {
t.Fatalf("clearance approved = obs %d succ %d, want 1/1", app.Observations, app.Successes)
}
rej := findSignal(t, sigs, "clearance_gap_override_outcome", scrub.BandUnknown, scrub.OutcomeRejected)
if rej.Observations != 1 || rej.Successes != 0 {
t.Fatalf("clearance rejected = obs %d succ %d, want 1/0", rej.Observations, rej.Successes)
}
// Exactly two clearance signals (the approved and the rejected bucket), never a spurious one.
clearanceSignals := 0
for _, s := range sigs {
if s.Pattern == "clearance_gap_override_outcome" {
clearanceSignals++
}
}
if clearanceSignals != 2 {
t.Fatalf("got %d clearance signals, want 2 (the met and the not required members must not fold)", clearanceSignals)
}
}
// TestFoldExperienceShortfall proves an experience_score below 100 folds into experience_shortfall_outcome,
// and a full experience score folds nothing.
func TestFoldExperienceShortfall(t *testing.T) {
rows := []DecisionRow{
{Decision: "approved", RequiredLevel: "none", ClearanceScore: dec("100"), ExperienceScore: dec("60")},
{Decision: "approved", RequiredLevel: "none", ClearanceScore: dec("100"), ExperienceScore: dec("100")}, // full, no shortfall
{Decision: "pending", RequiredLevel: "none", ClearanceScore: dec("100"), ExperienceScore: dec("0")},
}
sigs := fold(rows, engine.ModelVersion)
app := findSignal(t, sigs, "experience_shortfall_outcome", scrub.BandUnknown, scrub.OutcomeApproved)
if app.Observations != 1 || app.Successes != 1 {
t.Fatalf("experience approved = obs %d succ %d, want 1/1", app.Observations, app.Successes)
}
pend := findSignal(t, sigs, "experience_shortfall_outcome", scrub.BandUnknown, scrub.OutcomePending)
if pend.Observations != 1 || pend.Successes != 0 {
t.Fatalf("experience pending = obs %d succ %d, want 1/0 (pending is not a success)", pend.Observations, pend.Successes)
}
}
// TestFoldRerankerLabelAgreementIsZeroToday documents and pins the honest gap: no stored evidence supports
// reranker_label_agreement, so the producer derives nothing for it. If a future change starts emitting it,
// this test fails and forces the author to wire real stored evidence first.
func TestFoldRerankerLabelAgreementIsZeroToday(t *testing.T) {
rows := []DecisionRow{
{Decision: "approved", RequiredLevel: "secret", ClearanceScore: dec("0"), ExperienceScore: dec("50"),
MatchedSkills: matchedSkillsJSON(t, semanticSkill("0.9"))},
}
for _, s := range fold(rows, engine.ModelVersion) {
if s.Pattern == "reranker_label_agreement" {
t.Fatalf("reranker_label_agreement was emitted with no stored evidence to support it: %+v", s)
}
}
}
// TestFoldNoStoredEvaluationFoldsNoSubstitution proves a slate member with no candidate_evaluations row (the
// LEFT join yields a NULL matched_skills) contributes nothing to the substitution pattern, so the producer
// never fabricates a skill match it cannot see.
func TestFoldNoStoredEvaluationFoldsNoSubstitution(t *testing.T) {
rows := []DecisionRow{
{Decision: "approved", RequiredLevel: "none", ClearanceScore: dec("100"), ExperienceScore: dec("100"), MatchedSkills: nil},
}
for _, s := range fold(rows, engine.ModelVersion) {
if s.Pattern == "adjacent_skill_substitution_outcome" {
t.Fatalf("a substitution was folded from a member with no stored evaluation: %+v", s)
}
}
}
// TestFoldModelVersionIsEngineVersion proves every produced signal is stamped with engine.ModelVersion, never
// a literal, so a drifted engine version is visible at the boundary.
func TestFoldModelVersionIsEngineVersion(t *testing.T) {
rows := []DecisionRow{
{Decision: "approved", RequiredLevel: "secret", ClearanceScore: dec("0"), ExperienceScore: dec("100")},
}
sigs := fold(rows, engine.ModelVersion)
if len(sigs) == 0 {
t.Fatal("expected at least one signal")
}
for _, s := range sigs {
if s.ModelVersion != engine.ModelVersion {
t.Fatalf("signal ModelVersion = %q, want engine.ModelVersion %q", s.ModelVersion, engine.ModelVersion)
}
}
}
// TestEveryProducedSignalCrossesTheScrubBoundary proves the boundary holds: every signal the producer builds
// from a rich fixture is accepted by scrub.Marshal (the same validation the inference plane runs on receive),
// and its serialized form carries nothing but the six closed fields, no candidate id, no name, no raw score.
func TestEveryProducedSignalCrossesTheScrubBoundary(t *testing.T) {
// register here so the test is order independent of the package init; RegisterModelVersion is idempotent.
scrub.RegisterModelVersion(engine.ModelVersion)
rows := []DecisionRow{
{Decision: "approved", RequiredLevel: "secret", ClearanceScore: dec("0"), ExperienceScore: dec("50"),
MatchedSkills: matchedSkillsJSON(t, semanticSkill("0.93"), semanticSkill("0.82"))},
{Decision: "rejected", RequiredLevel: "top_secret", ClearanceScore: dec("0"), ExperienceScore: dec("100"),
MatchedSkills: matchedSkillsJSON(t, semanticSkill("0.70"))},
{Decision: "pending", RequiredLevel: "none", ClearanceScore: dec("100"), ExperienceScore: dec("20")},
}
sigs := fold(rows, engine.ModelVersion)
if len(sigs) == 0 {
t.Fatal("expected produced signals to validate")
}
want := map[string]bool{"pattern": true, "band": true, "outcome": true, "observations": true, "successes": true, "model_version": true}
for _, s := range sigs {
raw, err := scrub.Marshal(s)
if err != nil {
t.Fatalf("scrub.Marshal refused a produced signal %+v: %v", s, err)
}
var m map[string]json.RawMessage
if err := json.Unmarshal(raw, &m); err != nil {
t.Fatalf("unmarshal produced signal: %v", err)
}
if len(m) != len(want) {
t.Fatalf("produced signal has %d fields, want %d: %s", len(m), len(want), raw)
}
for k := range m {
if !want[k] {
t.Fatalf("produced signal carries an unexpected field %q: %s", k, raw)
}
}
}
}
// recordingWriter records every aggregate upsert so the test asserts the live in tenant write shape.
type recordingWriter struct {
upserts []dbgen.UpsertTeachingSignalAggregateParams
}
func (w *recordingWriter) UpsertTeachingSignalAggregate(_ context.Context, arg dbgen.UpsertTeachingSignalAggregateParams) error {
w.upserts = append(w.upserts, arg)
return nil
}
// TestPersistWritesLiveInTenantRows exercises the teach and write loop with a fake migrator writer and an
// inference client whose TeachingEnabled is false. It proves every folded signal is persisted with tenant_id
// set to the org, dp_applied false, the engine model version, valid window dates, and the folded counts, and
// that Teach is exercised end to end (scrub.Marshal) without transmitting anything off box.
func TestPersistWritesLiveInTenantRows(t *testing.T) {
scrub.RegisterModelVersion(engine.ModelVersion)
org := uuid.New()
ctx := context.Background()
rows := []DecisionRow{
{Decision: "approved", RequiredLevel: "secret", ClearanceScore: dec("0"), ExperienceScore: dec("100")},
{Decision: "approved", RequiredLevel: "none", ClearanceScore: dec("100"), ExperienceScore: dec("40"),
MatchedSkills: matchedSkillsJSON(t, semanticSkill("0.91"))},
}
signals := fold(rows, engine.ModelVersion)
writer := &recordingWriter{}
// TeachingEnabled false: Teach validates the boundary but transmits nothing, so the placeholder BaseURL is
// never reached (a network call would fail the test).
teacher, err := inference.New(inference.Config{BaseURL: "http://127.0.0.1:0", TeachingEnabled: false})
if err != nil {
t.Fatalf("inference client: %v", err)
}
prod := &TeachingProducer{
writer: writer,
teacher: teacher,
now: func() time.Time { return time.Date(2026, 6, 30, 12, 0, 0, 0, time.UTC) },
}
if err := prod.persist(ctx, org.String(), signals); err != nil {
t.Fatalf("persist: %v", err)
}
if len(writer.upserts) != len(signals) {
t.Fatalf("persisted %d aggregates, want %d (one per signal)", len(writer.upserts), len(signals))
}
for _, u := range writer.upserts {
if u.TenantID == nil || *u.TenantID != org {
t.Fatalf("aggregate tenant_id = %v, want the org %v (live in tenant row)", u.TenantID, org)
}
if u.DpApplied {
t.Fatal("aggregate dp_applied = true, want false for a live in tenant row")
}
if u.ModelVersion != engine.ModelVersion {
t.Fatalf("aggregate model_version = %q, want %q", u.ModelVersion, engine.ModelVersion)
}
if !u.WindowStart.Valid || !u.WindowEnd.Valid {
t.Fatal("aggregate window dates must be valid, never NULL")
}
if u.Observations < u.Successes || u.Successes < 0 {
t.Fatalf("aggregate counts not a valid aggregate: obs %d succ %d", u.Observations, u.Successes)
}
}
}