Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/modules/ai/safety_test.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | f585a9ef32fa554663ce1fba9a414a54b6e49cf0 |
| Branch | ws9-bench-ingestion |
| Path or archive member | internal/modules/ai/safety_test.go |
| Original SHA-256 | 07b3fc93af280cbf20e768748896100a841b61a64f8fc6bf1b13184a9b20b8e1 |
| Git blob SHA | 093a3d5f8146b667f792499fd3d10925c36c87de |
| Reading-copy SHA-256 | 07b3fc93af280cbf20e768748896100a841b61a64f8fc6bf1b13184a9b20b8e1 |
Recovered source transcript
Download complete source text ↓128 lines · 5.0 KB
package ai
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
func TestForbidEEORejectsDemographics(t *testing.T) {
bad := []string{
"SELECT * FROM eeo.candidate_demographics",
"select gender, race_ethnicity from eeo.candidate_demographics",
"join EEO.candidate_demographics on true",
"the candidate's veteran_status is",
}
for _, s := range bad {
if err := ForbidEEO(s); !errors.Is(err, ErrEEOForbidden) {
t.Errorf("ForbidEEO(%q) must refuse the eeo schema, got %v", s, err)
}
}
if err := ForbidEEO("SELECT full_name FROM candidates WHERE clearance_level='secret'"); err != nil {
t.Errorf("a clean query must pass: %v", err)
}
}
func TestGuardReadOnlyQuery(t *testing.T) {
bad := map[string]string{
"insert": "INSERT INTO candidates (full_name) VALUES ('x')",
"update": "UPDATE candidates SET available=true",
"delete": "DELETE FROM candidates",
"stacked": "SELECT 1 FROM candidates; DROP TABLE candidates",
"eeo read": "SELECT gender FROM eeo.candidate_demographics",
"hidden write in cte": "WITH x AS (DELETE FROM candidates RETURNING id) SELECT * FROM x",
"non allowlisted": "SELECT * FROM auth_credentials",
}
for name, sql := range bad {
if err := GuardReadOnlyQuery(sql, ReadAllowlist); err == nil {
t.Errorf("GuardReadOnlyQuery must refuse %q", name)
}
}
ok := []string{
"SELECT full_name, clearance_level FROM candidates WHERE available = true",
"WITH ranked AS (SELECT id FROM slate_members) SELECT count(*) FROM ranked",
"SELECT c.full_name FROM candidates c JOIN candidate_skills cs ON cs.candidate_id = c.id",
}
for _, sql := range ok {
if err := GuardReadOnlyQuery(sql, ReadAllowlist); err != nil {
t.Errorf("a clean allowlisted read must pass: %q: %v", sql, err)
}
}
}
// TestRegistryIsSafe asserts every curated TAG intent is read only, allowlisted, and eeo free, so an
// unsafe intent cannot ship even if added by mistake.
func TestRegistryIsSafe(t *testing.T) {
if err := ValidateRegistry(); err != nil {
t.Fatalf("a TAG intent is unsafe: %v", err)
}
if len(Registry) == 0 {
t.Fatal("the TAG registry is empty; the safety check is a no op")
}
}
// TestNoAIFileReferencesEEO is the required tripwire: it walks every Go file in the AI package, the W3
// staffing recommendations package, and the WS9 bench ingestion surface (the benchimport mapping, the
// sourcing ingest path, and the import-bench CLI), and fails if any of them, other than the prohibition
// guard (safety.go) and the tests, names the eeo schema or a demographic column. No embedding, retrieval,
// graph, memory, generated query, prompt builder, classifier, optimizer, value function, confidence
// function, bench record, embed profile composer, CSV or JSONL column mapping, IngestBenchRecord, or the
// CLI may reach demographics; this test makes that hold by construction, not by review.
func TestNoAIFileReferencesEEO(t *testing.T) {
markers := []string{"eeo.", "candidate_demographics", "race_ethnicity", "veteran_status", "disability_status", "from eeo", "join eeo"}
// The AI layer, the W3 staffing recommendations layer, and the WS9 bench ingestion surface. The WS9 dirs
// are relative to this test's location (internal/modules/ai): benchimport and sourcing are two levels up
// under platform, the CLI is three levels up under cmd. INVARIANT 4 requires the tripwire to cover them.
dirs := []string{".", "../staffing", "../../platform/benchimport", "../../platform/sourcing", "../../../cmd/import-bench"}
total := 0
for _, dir := range dirs {
total += scanDirForEEO(t, dir, markers)
}
if total == 0 {
t.Fatal("no source files were scanned; the tripwire is not actually running")
}
t.Logf("scanned %d AI, staffing, and bench ingestion source files, none reach eeo", total)
}
func scanDirForEEO(t *testing.T, dir string, markers []string) int {
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("read dir %s: %v", dir, err)
}
checked := 0
for _, e := range entries {
name := e.Name()
if !strings.HasSuffix(name, ".go") {
continue
}
// safety.go IS the prohibition guard (it must name eeo to forbid it); tests describe the rule.
if name == "safety.go" || strings.HasSuffix(name, "_test.go") {
continue
}
body, err := os.ReadFile(filepath.Join(dir, name))
if err != nil {
t.Fatalf("read %s/%s: %v", dir, name, err)
}
// A prohibition COMMENT may name eeo (the rule being documented); only a CODE reference is a reach.
// Drop comment lines, then scan the executable content.
var code strings.Builder
for _, line := range strings.Split(string(body), "\n") {
if strings.HasPrefix(strings.TrimSpace(line), "//") {
continue
}
code.WriteString(strings.ToLower(line))
code.WriteByte('\n')
}
lower := code.String()
for _, m := range markers {
if strings.Contains(lower, m) {
t.Errorf("EEO REACH: %s/%s contains %q in executable code; no AI or staffing path may name the eeo schema", dir, name, m)
}
}
checked++
}
return checked
}