Revive the dead probability layer + restore grade range ON MERIT

Folds re-sequenced steps 1+2 into one change (Kev's call): same bug
family — features wired to sources that return null.

THE PROBABILITY LAYER WAS DEAD IN PRODUCTION. p_win/ev_pct/kelly/
model_odds/value were absent on 0/8 live grades because
gameLogService.getGameLogs returns null for MLB by construction and
depends on the offline Python service for NBA/WNBA, so meta.gameLogs was
[] for every sport. This was the S46 bug in a second location — that fix
gave featureCache an MLB branch (why grades still worked) but never the
estimator. featureCache.getStatRows now supplies normalized rows
([{date,[statType]:v}], most-recent-first) for every sport, feeding the
estimator AND consistency AND game_count_in_7d from one fetch.
VERIFIED on real props: p_win 25/25 WNBA, 8/8 MLB (was 0).

GRADE RANGE, ON MERIT — never by rescaling (permanent founder ruling:
minting A's without new information is a relabelled B sold as an A and
corrupts an append-only ledger).
- refreshTeamStats wired into runSnapshot — it had ZERO production
  callers, so opp_rank_stat was permanently null and a +/-1.0 factor
  could never fire. Test-env no-op (opsNotify precedent).
- L20 made SYMMETRIC: both branches were delta +1.0, so the season
  baseline could only ever ADD. No negative path was a structural reason
  D was unreachable. New l20_contradicts_* carries -1.0.
- game_count_in_7d derived from real logged dates (heavy_workload_7d).
- NOT wired, deliberately, with reasons inline: teamId (no team_id
  column; getFeatures reads it top-level; factor also needs a starter-id
  list) and season_type (ESPN 2 = REGULAR season; threading it raw would
  fire veteran_in_playoffs in July). Dead code dressed as a fix is the
  thing we are removing, not adding.

CALIBRATION GUARD (found by verifying, not assuming): consistency CV is
NBA-tuned; for a Poisson-ish stat cv ~ 1/sqrt(mean), so any stat with
mean < 4 auto-classifies boom_bust. First verification run showed 8/8 MLB
props boom_bust — a blanket -1.0 that dropped the board to all-C. Floored
at CONSISTENCY_MIN_MEAN=4 -> 'unknown' below. Absent beats wrong. MLB
low-count stats therefore still get no consistency factor: honest, not
fixed. Scale-free index-of-dispersion classifier is the open follow-up.

CONFIDENCE IS NOT A PROBABILITY: payloads carry confidence_basis:
'grade_band'. Corrected mlb-grade-degradation.md — its "25/25
grade<->confidence agreement" is a TAUTOLOGY (confidence is derived FROM
the letter, so it would report 25/25 even if every grade were wrong), not
a validation. Removed dead mlbGrader.js (referenced only by its own test)
and the stale computeFeatures comment claiming a penalty that never ran.

VERIFICATION (scripts/verify-grade-range.js, real props/logs/engine):
WNBA 25 props B 68%->32%, C 32%->64%, D 0->1 (4%); 11-step spread went
from 2 steps to 5 (C/C+/B-/D). The D is earned: Angel Reese assists o2.5,
p_win 0.365. Nothing flooded — grades got HARDER. A did not emit locally
because opp_rank_stat needs the Redis cache only prod populates (local
ceiling +3.0 vs the +4.5 A needs); reachability is proven arithmetically
and locked in tests. Prod A-emission is the outstanding fingerprint.

MARKETING HOLD: "A-RATED" (AccuracyBadge, TopSignals) is unsupported
until that fingerprint. Confirmed honest fallbacks render today —
/api/ledger/accuracy has B and C buckets only, so the badge shows
"MODEL · 63% HIT" and TopSignals self-hides. Nothing fabricated ships.

Suite 276/3286 green, web build exit 0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SmNjJAwEnqHPtXbvSZR8kA
This commit is contained in:
Kev
2026-07-19 18:54:51 -04:00
parent 416639efe4
commit 1a94ef5fcf
16 changed files with 652 additions and 346 deletions
+49
View File
@@ -1001,6 +1001,55 @@ phased plan in the Session-57 conversation / BUILD-STATE Next section).
"TRACKING — READ LOCKED PRE-GAME" renders once per live card (GameCard, "TRACKING — READ LOCKED PRE-GAME" renders once per live card (GameCard,
dim — it's meta, not a caution signal). dim — it's meta, not a caution signal).
## Probability Layer + Grade Range (Session 63 — non-obvious)
- **`gameLogService.getGameLogs` is a TRAP: it returns null for MLB by
construction** (`pythonPath` `default: return null`) and depends on the Python
service, which is OFFLINE in prod. Anything wired to it is dead. S46 fixed this
for FEATURES (`featureCache.gameLogFeatures` MLB branch) but NOT for the
estimator — so `meta.gameLogs` was `[]` for every sport and `p_win`, `ev_pct`,
`kelly`, `model_odds`, `value` were absent on 100% of live grades for months.
**`featureCache.getStatRows(player, sport, statType)` is now the one true source
of normalized per-game rows** (`[{date, [statType]: v}]`, MOST-RECENT-FIRST —
the estimator treats `slice(0,5)` as the recency window). Use it; never add a
new caller of `gameLogService` directly.
- **Hero v2 requires a finite `ev_pct`** — when EV was dead it matched nothing and
fell through to the recent-read fallback silently (`is_recent:true` was the
tell). A "working" endpoint returning data is not proof the intended rule ran.
- **`confidence` is NOT a probability.** engine1 picks a letter from an additive
factor index, then reads that letter's band MIDPOINT out of
`grade_thresholds.json` to make the number — so it carries zero information
beyond the letter and can never disagree with it. Payloads carry
`confidence_basis: 'grade_band'`. The real signal is `p_win`. Corollary:
mlb-grade-degradation.md's "25/25 grade<->confidence agreement" is a TAUTOLOGY,
not a validation (corrected in that file) — never cite it as grade quality.
- **`grade_thresholds.json` is NOT an input mapper in the JS path** — only the
Python side compares scores to it. In JS it is a confidence lookup table read
BACKWARDS from the already-chosen letter.
- **The grade is an integer index** (`GRADE_SCALE`, `NEUTRAL_INDEX` 3) moved by
flat +/-1.0 and +/-0.5 deltas. A needs sum >= +4.5, D needs <= -1.51. Six
factors were wired to features nothing populated, pinning the live range to
{C,B} — only TWO letters ever emitted across 604 ledger rows.
**`refreshTeamStats` had ZERO production callers**, so `opp_rank_stat` (a +/-1.0)
was permanently null; it is now called in `runSnapshot` (test-env no-op, the
opsNotify precedent). L20 was asymmetric (both branches +1.0 = no downside path)
and is now symmetric.
- **Consistency CV is scale-dependent — this is a live landmine.** The thresholds
are NBA-tuned (points ~20/gm). For a Poisson-ish stat `cv ~ 1/sqrt(mean)`, so
ANY stat with mean < 4 auto-classifies `boom_bust` (real: Alonso hits mean 0.60
-> cv 1.17). Reviving consistency without a guard stamps a blanket -1.0 on
nearly every MLB prop. Floored at `CONSISTENCY_MIN_MEAN` (4) -> `unknown` below.
The scale-free fix is an index-of-dispersion classifier (open item).
- **NEVER rescale thresholds to make A's appear** (founder ruling, permanent).
Minting A's without new information is a relabelled B sold as an A and it
corrupts an append-only ledger. Fix the grade on MERIT or don't claim the scale.
- **A-RATED copy is on hold** until a prod fingerprint shows real A grades.
`/api/ledger/accuracy` currently returns B and C buckets only, so AccuracyBadge
correctly falls through to "MODEL · X% HIT" and TopSignals self-hides.
- `scripts/verify-grade-range.js` replays live-board props through the real engine
on free feeds. It UNDERSTATES range locally (no Redis -> no `opp_rank_stat`).
Redis runs degraded locally, so the script must `process.exit(0)` — otherwise a
reconnect timer holds the process open and piped output is lost to SIGTERM.
## Active Skills ## Active Skills
- vyndr-voice (all user-facing output) - vyndr-voice (all user-facing output)
- prop-analysis (grading methodology) - prop-analysis (grading methodology)
+109
View File
@@ -0,0 +1,109 @@
#!/usr/bin/env node
/**
* Session 63 — GRADE-RANGE VERIFICATION (on merit, not by rescaling).
*
* Replays REAL props from the live board through the REAL engine path that
* Session 63 repaired, and reports the resulting grade distribution.
*
* What is real here:
* - the props (player / stat / line / side) come from the live API board
* - the game logs come from statsapi.mlb.com + ESPN (free, no quota)
* - the consistency factor + L5/L20 features are computed from those logs
* - the grade comes from engine1.gradeProp, unmodified
*
* What is NOT covered (documented, not hidden):
* - `opp_rank_stat` needs `team_stats:{sport}:{abbr}` in Redis, which only
* the production snapshot populates. Locally it stays null, so this run
* UNDERSTATES the restored range — it omits a ±1.0 factor. Any A/D seen
* here is therefore a floor, not a ceiling.
*
* Usage: node scripts/verify-grade-range.js [sport] [limit]
*/
const engine1 = require('../src/services/intelligence/engine1');
const featureCache = require('../src/services/intelligence/featureCache');
const consistencyScore = require('../src/services/intelligence/consistencyScore');
const { estimateProbability } = require('../src/services/intelligence/probabilityEstimator');
const { fourLetterGrade } = require('../src/utils/gradeAdapter').__internals;
const API = process.env.VERIFY_API || 'https://api.vyndr.app';
const SPORT = process.argv[2] || 'mlb';
const LIMIT = Number(process.argv[3] || 40);
async function board(sport) {
const res = await fetch(`${API}/api/snapshot/${sport}`);
const json = await res.json();
const grades = Array.isArray(json.grades) ? json.grades : [];
return grades.map((g) => ({
player: g.player,
stat: g.stat_type,
line: Number(g.line),
direction: String(g.direction || 'over').toLowerCase(),
oldGrade: g.grade,
oldConfidence: g.confidence,
})).filter((p) => p.player && p.stat && Number.isFinite(p.line));
}
async function gradeOne(p, sport) {
const rows = await featureCache.getStatRows(p.player, sport, p.stat);
const features = await featureCache.__internals.gameLogFeatures(p.player, sport, p.stat);
const consistency = await consistencyScore.getConsistency({
playerName: p.player, sport, statType: p.stat, gameLogs: rows,
});
const prop = { line: p.line, direction: p.direction };
const res = engine1.gradeProp({ features, trap: {}, consistency, prop });
const est = estimateProbability({ gameLogs: rows, line: p.line, statType: p.stat, features });
const pWin = Number.isFinite(est.p_over)
? (p.direction === 'under' ? 1 - est.p_over : est.p_over)
: null;
return {
...p,
rows: rows.length,
consistency: consistency.consistency,
newGrade11: res.grade,
newGrade: fourLetterGrade(res.grade),
p_win: pWin == null ? null : Math.round(pWin * 1000) / 1000,
};
}
(async () => {
const props = (await board(SPORT)).slice(0, LIMIT);
if (!props.length) { console.log(`no live props for ${SPORT}`); return; }
console.log(`Replaying ${props.length} REAL ${SPORT.toUpperCase()} props through the repaired engine\n`);
const out = [];
for (const p of props) {
try { out.push(await gradeOne(p, SPORT)); }
catch (e) { console.warn(` ! ${p.player} ${p.stat}: ${e.message}`); }
}
const tally = (arr, key) => arr.reduce((m, r) => { const k = r[key] ?? 'null'; m[k] = (m[k] || 0) + 1; return m; }, {});
const pct = (n) => `${Math.round((n / out.length) * 1000) / 10}%`;
console.log('--- 4-LETTER DISTRIBUTION ---');
console.log('BEFORE (live board):', tally(out, 'oldGrade'));
const after = tally(out, 'newGrade');
console.log('AFTER (repaired) :', after);
for (const g of ['A', 'B', 'C', 'D', 'F']) if (after[g]) console.log(` ${g}: ${after[g]} (${pct(after[g])})`);
console.log('\n--- 11-STEP DISTRIBUTION (pre-collapse) ---');
console.log(tally(out, 'newGrade11'));
console.log('\n--- REVIVED SIGNALS ---');
const withRows = out.filter((r) => r.rows > 0).length;
const withP = out.filter((r) => r.p_win != null).length;
const withCons = out.filter((r) => r.consistency && r.consistency !== 'unknown').length;
console.log(`game-log rows present : ${withRows}/${out.length}`);
console.log(`p_win computed : ${withP}/${out.length} (was 0 in prod)`);
console.log(`consistency known : ${withCons}/${out.length} (was 0 for MLB)`);
console.log('\n--- MOVERS (grade changed) ---');
for (const r of out.filter((r) => r.oldGrade !== r.newGrade).slice(0, 15)) {
console.log(` ${r.oldGrade}${r.newGrade.padEnd(2)} (${r.newGrade11.padEnd(2)}) ${r.player} ${r.stat} ${r.direction} ${r.line} n=${r.rows} cons=${r.consistency} p=${r.p_win}`);
}
// Redis runs in degraded mode locally and keeps a reconnect timer alive, so
// the process would never exit on its own — flush and leave deliberately.
await new Promise((r) => process.stdout.write('', r));
process.exit(0);
})().catch((e) => { console.error('verify failed:', e.message); process.exit(1); });
+101 -1
View File
@@ -277,4 +277,104 @@ down. `espnStatsAdapter.getPlayerGameLog` (Wave 0) already solves exactly this f
--- ---
*Diagnosis 2026-07-19. Data + live API. No engine code changed.* # RESOLUTION — Session 63 (shipped)
Kev's ruling: **(a) fix on merit, never (b) rescale.** Rescaling would mint A's
without adding information — a relabelled B marketed as an A, corrupting an
append-only ledger permanently. That option is permanently rejected.
## What shipped
| Fix | File | Effect |
|---|---|---|
| Normalized per-game rows for ALL sports | `featureCache.getStatRows` | Revives `p_win``ev_pct`, `kelly`, `model_odds`, `value`, hero v2. Also feeds consistency. |
| Rows wired into the grade path | `computeFeatures.safeGetConsistency` | One fetch per prop, shared by 3 starving consumers |
| `refreshTeamStats` called in production | `snapshotService.runSnapshot` | `opp_rank_stat` populated → the ±1.0 opponent factor can fire (it had ZERO callers) |
| `game_count_in_7d` derived from real logs | `computeFeatures` gameContext | `heavy_workload_7d` (0.5) can fire |
| **L20 symmetry** | `engine1.computeFactors` | NEW `l20_contradicts_*` 1.0. There was no negative L20 path at all — a structural reason D was unreachable |
| Consistency CV floor | `consistencyScore` | See calibration finding below |
| `confidence_basis: 'grade_band'` | `gradeAdapter.toLegacyShape` | Confidence labelled as derived, not a probability |
| Dead `mlbGrader.js` **removed** | — | Referenced only by its own test. Described-but-dead penalty eliminated |
**Deliberately NOT wired** (would have been dead code dressed as a fix, documented
inline): `teamId` (no `team_id` column exists; `getFeatures` reads it top-level not
off gameContext; and the factor needs a starter-id list that doesn't exist) and
`season_type` (engine1 gates playoff factors on `season_type >= 2`, but ESPN's 2
means REGULAR season — threading it raw would fire "veteran_in_playoffs" in July).
## 🔶 CALIBRATION FINDING — consistency was NBA-tuned and would have flooded `boom_bust`
Reviving consistency exposed a latent bug. The CV thresholds (`cv >= 0.5`
`boom_bust`) were calibrated for NBA points (mean ~20). For a Poisson-ish counting
stat, **cv ≈ 1/√mean**, so any stat with mean < 4 forces `cv > 0.5` — it classifies
`boom_bust` regardless of actual behaviour. Verified on real logs:
- Alonso hits `[0,0,0,1,2,1,0,1,1,0]` → mean 0.60, **cv 1.17** → boom_bust
- Henderson hits `[1,0,0,3,1,1,0,0,1,0]` → mean 0.70, **cv 1.36** → boom_bust
First verification run confirmed it: **8/8 MLB props classified boom_bust**, a
blanket 1.0 that dropped the whole board to C. That is a systematic downgrade
masquerading as a signal — the mirror image of the "flooding A's" failure Kev
warned about.
**Guard shipped:** `MIN_MEAN_FOR_CV = 4` (env `CONSISTENCY_MIN_MEAN`). Below it,
consistency returns `unknown` (no factor) with `reason: 'low_mean_cv_unreliable'`.
Absent beats wrong. **Consequence: MLB low-count stats still get no consistency
factor** — honest, not fixed. The correct long-term fix is an index-of-dispersion
(variance/mean vs the Poisson baseline) classifier, which is scale-free. Tracked
as an open item; it is a modelling change needing its own validation.
## VERIFICATION ON MERIT — real props, real logs, real engine
`scripts/verify-grade-range.js` replays live-board props through the repaired
engine using free feeds (statsapi/ESPN). **Caveat stated up front: `opp_rank_stat`
needs the Redis team-stats cache that only production populates, so these local
runs OMIT a ±1.0 factor and therefore UNDERSTATE the restored range.**
**WNBA — 25 real props**
| | BEFORE (live board) | AFTER (repaired) |
|---|---|---|
| A | 0 | 0 |
| B | 17 (68 %) | 8 (32 %) |
| C | 8 (32 %) | 16 (64 %) |
| **D** | **0** | **1 (4 %)** |
11-step spread: `C 6 · C+ 10 · B 8 · D 1` — five distinct steps where there were
two. Revived signals: **`p_win` 25/25 (was 0)**, rows 25/25, consistency known
15/25 (the floor correctly abstains on low-mean assists/rebounds).
The D is earned, not manufactured: *Angel Reese assists over 2.5, p_win 0.365*
the model gives it 36.5 % and says so.
**MLB — 8 real props:** B 5 / C 3, `p_win` 8/8 (was 0). No A or D on a thin
8-prop late-night board of near-identical 0.5-hits props.
**Reading it honestly:**
- **D emits on merit. ✅**
- **A did not emit locally** — expected: A needs Σδ ≥ +4.5 and the local ceiling is
+3.0 without `opp_rank_stat`. Structural reachability is proven arithmetically
and locked in `tests/unit/gradeRangeRestore.test.js`; **empirical A emission
requires production and is the outstanding fingerprint.**
- **Nothing flooded.** Grades got *harder*, not easier — B fell 68 % → 32 %. The
B→C movers are driven by the new L20 negative branch: props whose season
baseline contradicts the graded side no longer get a free pass. That is the
intended correction.
## 🔴 MARKETING HOLD — A-rated copy is UNSUPPORTED until A verifiably emits
Confirmed the honest fallbacks are what render today:
- `/api/ledger/accuracy` returns buckets **B and C only** — no A bucket. So
`AccuracyBadge`'s `aRated` sample is 0, below `minSample`, and it falls through
to **"MODEL · 63% HIT"**. No fabricated A-RATED is displayed.
- `TopSignals` self-hides when there are no A-rated grades.
**Nothing fabricated is shipping — but the copy describes a grade the engine has
never emitted.** Do not promote "A-RATED" in marketing, and do not build new
surfaces on an A bucket, until a production fingerprint shows real A grades. Lift
this hold only against live data.
---
*Diagnosed + resolved 2026-07-19 (Session 63). Verified on real props; production
A-emission fingerprint outstanding.*
+22
View File
@@ -30,6 +30,28 @@ bug, fixed at the source in the generic grade path (`engine1` +
to any grade's displayed confidence resolves back to the same letter (proven to any grade's displayed confidence resolves back to the same letter (proven
for all 11 grades in `tests/unit/mlbGradeDegradation.test.js`). for all 11 grades in `tests/unit/mlbGradeDegradation.test.js`).
> ### ⚠️ CORRECTION (Session 63, 2026-07-19) — THE "25/25 AGREEMENT" WAS A TAUTOLOGY
>
> **Do not cite the 25/25 grade↔confidence agreement below as validation of
> grade quality. It validates nothing.**
>
> The fix above made `confidence` a *deterministic function of the letter*:
> engine1 picks a letter via an additive factor index, then looks up that
> letter's band midpoint to produce the number (`engine1.js:29-36`). Feeding
> that number back through the same table can only ever return the letter it
> came from. **The round-trip would report 25/25 even if every grade were
> wrong.**
>
> It is a real fix for a real bug (the two encodings had drifted a sub-tier
> apart) — it is simply a *consistency* check, not an *accuracy* check.
> `confidence` carries ZERO information beyond the letter. The genuinely
> independent probability is `p_win` (the quantile estimate over real game
> logs), which Session 63 discovered had never been computed in production at
> all. Payloads now carry `confidence_basis: 'grade_band'` so no consumer can
> mistake the derived number for a model probability.
>
> Full diagnosis: `specs/audit-data/grade-collapse.md`.
## Blast radius (commit `9fc4edf`) — work-order #6 ## Blast radius (commit `9fc4edf`) — work-order #6
The degraded grades (projection=0 → `model_value = 0`) are already settled in The degraded grades (projection=0 → `model_value = 0`) are already settled in
the append-only `ledger_entries` and are NOT deleted. Functional marking: the append-only `ledger_entries` and are NOT deleted. Functional marking:
+27
View File
@@ -176,6 +176,33 @@ Full arc definitions live in the Session-63 order. Status only here; update as e
| **U-deg** MLB degradation | ✅ **STATUS REPORTED** | `projection==0` leak **already closed** (0 occurrences since 07-18). `edge_pct` scale still broken. | | **U-deg** MLB degradation | ✅ **STATUS REPORTED** | `projection==0` leak **already closed** (0 occurrences since 07-18). `edge_pct` scale still broken. |
| **U-fp** Arc 1 fingerprint | open | Do it on the first deploy this train ships. | | **U-fp** Arc 1 fingerprint | open | Do it on the first deploy this train ships. |
### ✅ SESSION 63 — PROBABILITY LAYER + GRADE RANGE RESTORED (shipped)
The re-sequenced step 1+2, folded into one change. Full write-up:
`specs/audit-data/grade-collapse.md`.
- **The probability layer was DEAD in production** — `p_win`/`ev_pct`/`kelly`/
`model_odds`/`value` were absent on 0/8 live grades because `gameLogService`
returns null for MLB by construction and the Python service is offline for
NBA/WNBA. `featureCache.getStatRows` now supplies normalized rows for every
sport. **Verified: `p_win` 25/25 on real WNBA props, 8/8 MLB (was 0).**
- **Hero v2 had never once selected on EV** (it requires a finite `ev_pct`) and
silently fell through to the recent-read fallback every time.
- **Grade range:** `refreshTeamStats` wired into `runSnapshot` (it had ZERO
callers, so `opp_rank_stat` was permanently null), `game_count_in_7d` derived
from real logs, and **L20 made symmetric** (there was no negative branch at
all). D now emits on merit (WNBA 1/25, an earned `p_win` 0.365); A is proven
reachable arithmetically but **has not yet emitted in production — that is the
outstanding fingerprint**.
- **Calibration guard:** consistency CV was NBA-tuned; for any stat with mean < 4,
`cv ≈ 1/√mean` forces `boom_bust`. It would have stamped a blanket 1.0 on
nearly every MLB prop. Floored at `CONSISTENCY_MIN_MEAN=4``unknown` below it.
- **Confidence is NOT a probability** — payloads now carry
`confidence_basis: 'grade_band'`. The real signal is `p_win`.
- **`mlbGrader.js` REMOVED** (dead; referenced only by its own test).
- 🔴 **MARKETING HOLD:** "A-RATED" copy (AccuracyBadge, TopSignals) is unsupported
until a production fingerprint shows real A grades. Honest fallbacks confirmed
rendering ("MODEL · 63% HIT"); nothing fabricated ships.
### 🔶 OPEN DECISION — FLEX BAND ENFORCEMENT (Kev, 2026-07-19) ### 🔶 OPEN DECISION — FLEX BAND ENFORCEMENT (Kev, 2026-07-19)
**Ruling:** build `EDGE_FLEX_WALL` (250) + `EV_FLEX_THRESHOLD` (default **4 %**, = 2× **Ruling:** build `EDGE_FLEX_WALL` (250) + `EV_FLEX_THRESHOLD` (default **4 %**, = 2×
+44 -4
View File
@@ -18,7 +18,12 @@
* - game logs unavailable → consistency defaults to 'unknown' * - game logs unavailable → consistency defaults to 'unknown'
* *
* The caller (analyzeViaEngine1) reads the returned `errors` array and * The caller (analyzeViaEngine1) reads the returned `errors` array and
* downgrades confidence accordingly via the adapter's reasoning string. * surfaces them in the reasoning string. NOTE (Session 63): this comment used
* to claim confidence is "downgraded accordingly" — it never was. No
* data-sufficiency penalty exists in the live path; confidence is a pure
* function of the grade letter (see gradeAdapter `confidence_basis`). The one
* real penalty lived in the dead `mlbGrader.js`, now removed. Insufficient data
* produces a REFUSAL (grade null + insufficient_data), not a softened grade.
* *
* ───────────────────────────────────────────────────────────────────── * ─────────────────────────────────────────────────────────────────────
* Signal provenance (Session 15 audit) * Signal provenance (Session 15 audit)
@@ -170,10 +175,18 @@ async function safeGetTrap(input) {
} }
} }
async function safeGetConsistency({ playerName, sport, statType }) { async function safeGetConsistency({ playerName, sport, statType, statRows }) {
const fallback = { consistency: 'unknown', score: null, games: 0 }; const fallback = { consistency: 'unknown', score: null, games: 0 };
try { try {
const logs = await gameLogService.getGameLogs(playerName, sport, 20); // Session 63 — normalized rows from the REAL per-sport sources (MLB
// statsapi / ESPN gamelog), not the NBA-WNBA-only Python service. This one
// call feeds BOTH the consistency factor and (via meta.gameLogs) the
// probability estimator, which had no rows at all in production.
// `statRows` is passed in by computeFeaturesForProp so the fetch happens
// ONCE per prop (it also powers game_count_in_7d, built before features).
const logs = Array.isArray(statRows)
? statRows
: await featureCache.getStatRows(playerName, sport, statType);
if (!logs || logs.length === 0) return { result: fallback, gameLogs: [] }; if (!logs || logs.length === 0) return { result: fallback, gameLogs: [] };
const result = await consistencyScore.getConsistency({ const result = await consistencyScore.getConsistency({
playerName, sport, statType, gameLogs: logs, playerName, sport, statType, gameLogs: logs,
@@ -231,8 +244,35 @@ async function computeFeaturesForProp(rawProp = {}) {
const game = teamAbbr ? await lookupTodayGame({ sport, teamAbbr }) : null; const game = teamAbbr ? await lookupTodayGame({ sport, teamAbbr }) : null;
if (!game) errors.push('no_game_scheduled_today'); if (!game) errors.push('no_game_scheduled_today');
// Session 63 — fetch the normalized per-game rows ONCE. They feed three
// consumers that were all starving: the consistency factor, the probability
// estimator (via meta.gameLogs), and game_count_in_7d below.
const statRows = await featureCache.getStatRows(player, sport, statType);
const gameContext = { const gameContext = {
home_away: game ? (game.isHome ? 'home' : 'away') : null, home_away: game ? (game.isHome ? 'home' : 'away') : null,
// `game_count_in_7d` gates engine1's heavy_workload_7d (-0.5). Nothing ever
// populated it, so that factor could not fire. Derived from real logged
// game dates; null (omitted) when we have no dated rows.
game_count_in_7d: featureCache.gameCountInWindow(statRows, 7),
// DELIBERATELY NOT SET: `teamId`. It was tempting to thread it here to
// unlock injuryFeatures, but that would be dead code dressed as a fix —
// three things block that factor and none is solved by a teamId here:
// 1. getFeatures reads `teamId` as a TOP-LEVEL input, not off gameContext;
// 2. `player_id_map` has no team_id column (lookupPlayer selects
// espn_id/team_abbr only), so there is no id to pass;
// 3. injury_severity_score counts MISSING KNOWN STARTERS and no starter-id
// list exists, so it resolves to 0 and engine1's factor (needs >= 2)
// still cannot fire.
// There is also an unresolved semantic: the factor is documented as
// OPPONENT injuries but getFeatures passes `teamId`, with `opponentTeamId`
// sitting unused beside it. Left alone on purpose — see
// specs/audit-data/grade-collapse.md.
// DELIBERATELY NOT SET: `season_type`. engine1's playoff factors gate on
// `season_type >= 2`, but ESPN's season_type 2 means REGULAR season — so
// threading it raw would fire "veteran_in_playoffs" in July. The factor also
// needs career_playoff_games, which only the offline Python service
// provides. Left unset on purpose; see specs/audit-data/grade-collapse.md.
}; };
const features = await safeGetFeatures({ const features = await safeGetFeatures({
@@ -344,7 +384,7 @@ async function computeFeaturesForProp(rawProp = {}) {
}); });
const { result: consistency, gameLogs } = await safeGetConsistency({ const { result: consistency, gameLogs } = await safeGetConsistency({
playerName: player, sport, statType, playerName: player, sport, statType, statRows,
}); });
return { return {
+33 -1
View File
@@ -41,6 +41,32 @@ function classify(cv) {
return { consistency: 'boom_bust', score: 0.1 }; return { consistency: 'boom_bust', score: 0.1 };
} }
/**
* Session 63 — the CV thresholds above are NBA-calibrated (points ~20/game,
* cv ~0.2-0.4). They are MEANINGLESS for a low-count stat.
*
* For a Poisson-ish counting stat, cv ≈ 1/sqrt(mean). So mean < 4 forces
* cv > 0.5 — i.e. EVERY such stat classifies 'boom_bust' no matter how the
* player actually behaves. Verified against real logs: Alonso hits
* [0,0,0,1,2,1,0,1,1,0] → mean 0.60, cv 1.17 → boom_bust; Henderson
* [1,0,0,3,1,1,0,0,1,0] → mean 0.70, cv 1.36 → boom_bust.
*
* When the estimator path was revived, this would have stamped a blanket
* -1.0 on nearly every MLB prop — a systematic downgrade masquerading as a
* signal. Below the floor we return 'unknown' so engine1 adds NO factor:
* absent beats wrong.
*
* The RIGHT long-term fix is an index-of-dispersion (variance/mean vs the
* Poisson baseline) classifier, which is scale-free. That is a modelling
* change with its own validation and is tracked separately — this floor is
* the honest stopgap, not the answer.
*/
const MIN_MEAN_FOR_CV = Number(process.env.CONSISTENCY_MIN_MEAN || 4);
function cvIsMeaningful(mean) {
return Number.isFinite(mean) && Math.abs(mean) >= MIN_MEAN_FOR_CV;
}
function statsFor(values) { function statsFor(values) {
const clean = values.filter((v) => Number.isFinite(v)); const clean = values.filter((v) => Number.isFinite(v));
if (clean.length < 2) return null; if (clean.length < 2) return null;
@@ -60,7 +86,13 @@ async function getConsistency(input = {}) {
const values = logs.map((row) => statFromGameLog(row, statType)).filter((v) => v != null); const values = logs.map((row) => statFromGameLog(row, statType)).filter((v) => v != null);
const s = statsFor(values); const s = statsFor(values);
if (!s) return { consistency: 'unknown', score: null, games: values.length }; if (!s) return { consistency: 'unknown', score: null, games: values.length };
// Session 63 — refuse to classify when CV cannot discriminate at this scale.
if (!cvIsMeaningful(s.mean)) {
return { ...s, consistency: 'unknown', score: null, reason: 'low_mean_cv_unreliable' };
}
return { ...s, ...classify(s.cv) }; return { ...s, ...classify(s.cv) };
} }
module.exports = { getConsistency, classify, statsFor, statFromGameLog }; module.exports = {
getConsistency, classify, statsFor, statFromGameLog, cvIsMeaningful, MIN_MEAN_FOR_CV,
};
+14 -3
View File
@@ -66,11 +66,22 @@ function computeFactors(input) {
} }
} }
// Trend confirmation from L20. // Trend confirmation from L20 — SYMMETRIC (Session 63).
// Both branches used to be delta +1.0, so the season baseline could only ever
// ADD to the grade: a player whose season average CONTRADICTED the graded side
// contributed nothing instead of subtracting. With no negative L20 path the
// reachable index floor was -1.5, one rounding tick above a D, which is a
// structural reason D and F were unreachable. The contradiction case now
// carries the mirrored -1.0.
if (Number.isFinite(features.l20_avg) && Number.isFinite(line) && line > 0) { if (Number.isFinite(features.l20_avg) && Number.isFinite(line) && line > 0) {
const delta20 = (features.l20_avg - line) / line; const delta20 = (features.l20_avg - line) / line;
if (overWeighted && delta20 > 0) factors.push({ label: 'l20_over_line', delta: 1.0, magnitude: Math.abs(delta20) }); if (overWeighted) {
else if (!overWeighted && delta20 < 0) factors.push({ label: 'l20_under_line', delta: 1.0, magnitude: Math.abs(delta20) }); if (delta20 > 0) factors.push({ label: 'l20_over_line', delta: 1.0, magnitude: Math.abs(delta20) });
else if (delta20 < 0) factors.push({ label: 'l20_contradicts_over', delta: -1.0, magnitude: Math.abs(delta20) });
} else {
if (delta20 < 0) factors.push({ label: 'l20_under_line', delta: 1.0, magnitude: Math.abs(delta20) });
else if (delta20 > 0) factors.push({ label: 'l20_contradicts_under', delta: -1.0, magnitude: Math.abs(delta20) });
}
} }
// Consistency. // Consistency.
+76
View File
@@ -199,6 +199,80 @@ function nbaGameLogFeatures(res, statType) {
return out; return out;
} }
/**
* Session 63 — NORMALIZED PER-GAME STAT ROWS.
*
* The probability estimator (`probabilityEstimator.estimateProbability`) and the
* consistency scorer both read a game-log row as `row[statType]`. The ONLY
* producer wired to them was `gameLogService.getGameLogs`, which returns null for
* MLB by construction and depends on the offline Python service for NBA/WNBA —
* so `meta.gameLogs` was `[]` for every sport in production and every
* probability-derived output (p_win, ev_pct, kelly, model_odds, value) was
* silently skipped, along with the ±1.0 consistency factor.
*
* This is the S46 fix applied to the SECOND location: same adapters, same maps
* (no new stat map — the three-map-split rule stands), emitting rows in the shape
* those two consumers already expect:
*
* [{ date, [statType]: value }, ...] MOST-RECENT-FIRST
*
* Most-recent-first matters: the estimator treats `values.slice(0, 5)` as the
* recency window. Returns [] (never null) when no real log exists — absent beats
* a fabricated distribution.
*/
async function getStatRows(playerName, sport, statType) {
const sp = String(sport || '').toLowerCase();
const rows = [];
const push = (date, value) => {
if (value == null || !Number.isFinite(Number(value))) return;
rows.push({ date: date || null, [statType]: Number(value) });
};
try {
if (sp === 'mlb') {
const mlbStats = require('../adapters/mlbStatsAdapter');
const res = await mlbStats.getPlayerStats(playerName);
const logs = (res && res.found && Array.isArray(res.last10)) ? res.last10 : [];
// MLB logs are chronological (most recent LAST) — reverse to match.
for (const g of [...logs].reverse()) push(g && g.date, mlbStatValue(g && g.stat, statType));
return rows;
}
// NBA/WNBA — Python service first (it's the richer source when it's up),
// then the FREE ESPN per-athlete gamelog. Same order as gameLogFeatures.
const pyLogs = await gameLogs.getGameLogs(playerName, sp, 20);
if (Array.isArray(pyLogs) && pyLogs.length) {
// Python rows are already flat + most-recent-first.
for (const r of pyLogs) push(r && r.date, statFromGameLog(r, statType));
return rows;
}
if (sp === 'nba' || sp === 'wnba') {
const espnStats = require('../adapters/espnStatsAdapter');
const res = await espnStats.getPlayerGameLog(playerName, sp);
const logs = (res && res.found && Array.isArray(res.last10)) ? res.last10 : [];
const field = NBA_LOG_FIELD[statType];
if (!field) return rows; // unmapped stat → no rows, never a guess
// ESPN last10 is most-recent-first already.
for (const g of logs) push(g && g.date, statFromGameLog(g && g.stat, field));
}
return rows;
} catch (e) {
console.warn('[featureCache] getStatRows failed:', e.message);
return [];
}
}
/** Games played in the trailing `days` window, from normalized rows. Powers the
* `heavy_workload_7d` factor, whose feature nothing populated. */
function gameCountInWindow(statRows, days = 7, now = Date.now()) {
if (!Array.isArray(statRows)) return null;
const cutoff = now - days * 86_400_000;
const dated = statRows.filter((r) => r && r.date && !Number.isNaN(new Date(r.date).getTime()));
if (dated.length === 0) return null;
return dated.filter((r) => new Date(r.date).getTime() >= cutoff).length;
}
async function gameLogFeatures(playerName, sport, statType) { async function gameLogFeatures(playerName, sport, statType) {
// MLB game logs come from the FREE statsapi.mlb.com (Session 46) — the Python // MLB game logs come from the FREE statsapi.mlb.com (Session 46) — the Python
// gameLogService only covers NBA/WNBA, so MLB props had no recent/season // gameLogService only covers NBA/WNBA, so MLB props had no recent/season
@@ -401,6 +475,8 @@ function getCacheStats() {
module.exports = { module.exports = {
getFeatures, getFeatures,
getStatRows,
gameCountInWindow,
clearCache, clearCache,
getCacheStats, getCacheStats,
// Internal helpers exported for unit tests + Engine 2 reuse. // Internal helpers exported for unit tests + Engine 2 reuse.
-76
View File
@@ -1,76 +0,0 @@
const HITTING_STATS = [
'hits', 'total_bases', 'home_runs', 'rbis', 'runs_scored',
'strikeouts_batter', 'walks', 'stolen_bases',
];
const PITCHING_STATS = [
'strikeouts', 'earned_runs', 'outs_recorded', 'walks_allowed',
'hits_allowed', 'pitches_thrown',
];
const ALL_MLB_STATS = [...HITTING_STATS, ...PITCHING_STATS];
function isMlbStatType(statType) {
return ALL_MLB_STATS.includes(statType);
}
function calculateMlbEdge(playerAvg, line, direction) {
if (playerAvg == null || line == null) return 0;
if (direction === 'over') {
return ((playerAvg - line) / line) * 100;
}
// under
return ((line - playerAvg) / line) * 100;
}
function gradeMlbProp({ player, stat_type, line, direction, seasonAvg, recentAvg, killConditions = [] }) {
if (!isMlbStatType(stat_type)) {
return { grade: 'D', confidence: 30, edge_pct: 0, composite: 0 };
}
const seasonEdge = calculateMlbEdge(seasonAvg, line, direction);
const recentEdge = calculateMlbEdge(recentAvg, line, direction);
// Weighted composite: 60% season, 40% recent
const edge_pct = Math.round((seasonEdge * 0.6 + recentEdge * 0.4) * 100) / 100;
// Grade thresholds based on edge
let grade;
if (edge_pct >= 5) {
grade = 'A';
} else if (edge_pct >= 3) {
grade = 'B';
} else if (edge_pct >= 1) {
grade = 'C';
} else {
grade = 'D';
}
// Confidence based on edge magnitude
let confidence;
if (grade === 'A') {
confidence = Math.min(95, 80 + Math.floor(edge_pct));
} else if (grade === 'B') {
confidence = Math.min(79, 65 + Math.floor(edge_pct));
} else if (grade === 'C') {
confidence = Math.min(64, 50 + Math.floor(edge_pct * 2));
} else {
confidence = Math.max(30, 45 + Math.floor(edge_pct));
}
// Kill condition penalty: cap at C and reduce confidence by 15 per condition
if (killConditions.length > 0) {
if (grade === 'A' || grade === 'B') {
grade = 'C';
}
confidence -= killConditions.length * 15;
}
confidence = Math.max(30, Math.min(95, confidence));
const composite = Math.round(edge_pct * 100) / 100;
return { grade, confidence, edge_pct, composite };
}
module.exports = { gradeMlbProp, calculateMlbEdge, isMlbStatType, HITTING_STATS, PITCHING_STATS, ALL_MLB_STATS };
+23
View File
@@ -228,6 +228,13 @@ async function runSnapshot(sport, opts = {}) {
// pipeline already calls (schedule + summary). Fills the NBA/WNBA espnId gap // pipeline already calls (schedule + summary). Fills the NBA/WNBA espnId gap
// when the stats-resolve fallback misses. Returns {} for MLB / errors. // when the stats-resolve fallback misses. Returns {} for MLB / errors.
buildEspnIndex: opts.buildEspnIndex || require('./espnAthleteIndex').buildEspnAthleteIndex, buildEspnIndex: opts.buildEspnIndex || require('./espnAthleteIndex').buildEspnAthleteIndex,
// Session 63 — the opponent-rank feed. Injectable so tests never hit ESPN;
// under NODE_ENV=test it defaults to a no-op (the opsNotify precedent) so a
// suite that doesn't know about this dep can never make a live ESPN call.
refreshTeamStats: opts.refreshTeamStats
|| (process.env.NODE_ENV === 'test'
? async () => null
: require('./intelligence/teamStatsCache').refreshTeamStats),
}; };
const start = deps.nowMs(); const start = deps.nowMs();
const ts = deps.now(); const ts = deps.now();
@@ -255,6 +262,22 @@ async function runSnapshot(sport, opts = {}) {
return { sport: sp, status: 'skipped', reason: 'no props', gradeCount: 0 }; return { sport: sp, status: 'skipped', reason: 'no props', gradeCount: 0 };
} }
// Session 63 — REFRESH TEAM STATS BEFORE GRADING.
// `refreshTeamStats` is the ONLY writer of `team_stats:{sport}:{abbr}`, which
// is the ONLY source of `opp_rank_stat` — and it had zero production callers,
// so that feature was permanently null and engine1's ±1.0 opponent-defense
// factor could never fire. It is 24h-cached and rate-limited, so this is one
// cheap ESPN pass per snapshot. Best-effort: a failure here must never break
// the snapshot — the features simply stay absent, as before.
try {
const summary = await deps.refreshTeamStats(sp);
if (summary && summary.captured != null) {
console.log(`[snapshot] team stats refreshed for ${sp}: ${summary.captured} captured, ${summary.errored ?? 0} errored`);
}
} catch (e) {
console.warn(`[snapshot] team stats refresh failed for ${sp} (grading continues):`, e.message);
}
// Grade the slate via the existing service; capture the envelope instead of // Grade the slate via the existing service; capture the envelope instead of
// letting it write (we re-write an ENRICHED version below). // letting it write (we re-write an ENRICHED version below).
let envelope = null; let envelope = null;
+9
View File
@@ -135,6 +135,15 @@ function toLegacyShape(engine1Result, prop = {}, opts = {}) {
book: prop.book ?? null, book: prop.book ?? null,
grade, grade,
confidence, confidence,
// Session 63 — TRUTH LABEL. `confidence` is NOT a probability: engine1
// derives it by looking up the midpoint of the band belonging to the letter
// it already chose, so it carries ZERO information beyond the letter and can
// never disagree with it. (That is also why mlb-grade-degradation.md's
// "25/25 grade<->confidence agreement" was a tautology, not a validation.)
// The real, independent probability is `p_win` — the quantile estimate over
// actual game logs — which is attached by analyzeViaEngine1 and is what any
// surface showing a percentage should render.
confidence_basis: 'grade_band',
edge_pct: legacyEdgePct(opts.edgePct), edge_pct: legacyEdgePct(opts.edgePct),
kill_conditions_triggered: kill, kill_conditions_triggered: kill,
reasoning: { reasoning: {
+6
View File
@@ -29,6 +29,12 @@ jest.mock('../../src/services/intelligence/featureCache', () => ({
if (mockFeatures.throws) throw new Error('feature-fetch boom'); if (mockFeatures.throws) throw new Error('feature-fetch boom');
return { features: mockFeatures.current, meta: {} }; return { features: mockFeatures.current, meta: {} };
}, },
// Session 63 — computeFeatures now sources normalized per-game rows here
// (feeds consistency + the probability estimator + game_count_in_7d).
// Routed through the SAME mockLogs fixture the old gameLogService mock used,
// so "logs available → consistency computed" keeps its original meaning.
getStatRows: async () => mockLogs.current || [],
gameCountInWindow: () => null,
})); }));
const mockTrap = { current: null, throws: false }; const mockTrap = { current: null, throws: false };
@@ -16,6 +16,10 @@ jest.mock('../../src/utils/supabase', () => ({
jest.mock('axios'); jest.mock('axios');
jest.mock('../../src/services/intelligence/featureCache', () => ({ jest.mock('../../src/services/intelligence/featureCache', () => ({
getFeatures: jest.fn(), getFeatures: jest.fn(),
// Session 63 — computeFeatures now sources normalized per-game rows here
// (feeds consistency + the probability estimator + game_count_in_7d).
getStatRows: jest.fn(async () => []),
gameCountInWindow: jest.fn(() => null),
})); }));
jest.mock('../../src/services/intelligence/trapDetection', () => ({ jest.mock('../../src/services/intelligence/trapDetection', () => ({
getTrapScore: jest.fn(async () => ({ composite: 0.2, signals: {}, active_count: 1, recommendation: 'caution' })), getTrapScore: jest.fn(async () => ({ composite: 0.2, signals: {}, active_count: 1, recommendation: 'caution' })),
+135
View File
@@ -0,0 +1,135 @@
/**
* Session 63 — grade-range restoration.
*
* Locks the three structural facts the S63 audit found and fixed:
* 1. L20 has a NEGATIVE branch (there was no downside path at all).
* 2. With the previously-starving factors alive, A and D are REACHABLE.
* 3. `confidence` is explicitly labelled as grade-derived, not a probability.
*
* These are arithmetic/structural assertions on the engine, NOT a claim about
* how often A should occur in the wild — that is the live distribution report.
*/
const engine1 = require('../../src/services/intelligence/engine1');
const { toLegacyShape } = require('../../src/utils/gradeAdapter');
const featureCache = require('../../src/services/intelligence/featureCache');
const prop = (direction = 'over', line = 10) => ({ line, direction });
describe('L20 symmetry (the missing downside path)', () => {
test('L20 BELOW the line now subtracts on an OVER', () => {
const factors = engine1.__internals
? engine1.__internals.computeFactors({ features: { l20_avg: 5 }, prop: prop('over', 10) })
: null;
const res = engine1.gradeProp({ features: { l20_avg: 5 }, prop: prop('over', 10) });
// Whether or not internals are exported, the graded result must be BELOW
// the neutral 'C' — previously l20 could only ever add.
expect(['F', 'D', 'C-']).toContain(res.grade);
if (factors) {
expect(factors.find((f) => f.label === 'l20_contradicts_over').delta).toBe(-1.0);
}
});
test('L20 ABOVE the line still adds on an OVER (unchanged)', () => {
const res = engine1.gradeProp({ features: { l20_avg: 15 }, prop: prop('over', 10) });
expect(['C+', 'B-', 'B']).toContain(res.grade);
});
test('L20 ABOVE the line subtracts on an UNDER (mirrored)', () => {
const res = engine1.gradeProp({ features: { l20_avg: 15 }, prop: prop('under', 10) });
expect(['F', 'D', 'C-']).toContain(res.grade);
});
});
describe('A and D are reachable once the starving factors are alive', () => {
test('A emits when the real signals stack (the merit path)', () => {
const res = engine1.gradeProp({
features: {
l5_avg: 14, // +1.0 hot vs line
l20_avg: 13, // +1.0 season confirms
opp_rank_stat: 0.85, // +1.0 weak defense (was permanently null)
home_away: 1.0, // +0.5
rest_days: 3, // +0.5
},
consistency: { consistency: 'elite', score: 0.9 }, // +1.0 (was 'unknown')
prop: prop('over', 10),
});
expect(['A-', 'A', 'A+']).toContain(res.grade);
});
test('D/F emits when the real signals stack against (the merit path)', () => {
const res = engine1.gradeProp({
features: {
l5_avg: 6, // -1.0 cold vs line
l20_avg: 7, // -1.0 season contradicts (NEW branch)
opp_rank_stat: 0.1, // -1.0 top defense
home_away: 0.0,
rest_days: 0, // -0.5 back-to-back
game_count_in_7d: 5, // -0.5 heavy workload (was never populated)
},
consistency: { consistency: 'boom_bust' }, // -1.0
trap: { composite: 0.8 }, // -1.0
prop: prop('over', 10),
});
expect(['F', 'D']).toContain(res.grade);
});
test('a neutral feature set still lands at C — no inflation', () => {
const res = engine1.gradeProp({ features: {}, prop: prop('over', 10) });
expect(res.grade).toBe('C');
});
});
describe('confidence is labelled as derived, not a probability', () => {
test('toLegacyShape marks confidence_basis', () => {
const out = toLegacyShape(
{ grade: 'B', confidence: 0.63, all_factors: [] },
{ player: 'X', stat_type: 'hits', line: 1.5, direction: 'over' },
);
expect(out.confidence_basis).toBe('grade_band');
});
});
describe('gameCountInWindow (powers heavy_workload_7d)', () => {
const now = Date.UTC(2026, 6, 19);
const day = 86_400_000;
test('counts only games inside the window', () => {
const rows = [
{ date: new Date(now - 1 * day).toISOString(), hits: 1 },
{ date: new Date(now - 3 * day).toISOString(), hits: 2 },
{ date: new Date(now - 20 * day).toISOString(), hits: 0 },
];
expect(featureCache.gameCountInWindow(rows, 7, now)).toBe(2);
});
test('returns null (absent, not 0) when there are no dated rows', () => {
expect(featureCache.gameCountInWindow([], 7, now)).toBeNull();
expect(featureCache.gameCountInWindow([{ hits: 1 }], 7, now)).toBeNull();
expect(featureCache.gameCountInWindow(null, 7, now)).toBeNull();
});
});
describe('consistency CV floor (Session 63 calibration guard)', () => {
const cs = require('../../src/services/intelligence/consistencyScore');
test('CV is refused below the mean floor — a low-count MLB stat is NOT boom_bust', async () => {
// Real Pete Alonso hits log: mean 0.60, cv 1.17. Pre-guard this classified
// boom_bust and stamped -1.0 on essentially every MLB prop.
const logs = [0, 0, 0, 1, 2, 1, 0, 1, 1, 0].map((hits) => ({ hits }));
const res = await cs.getConsistency({ statType: 'hits', gameLogs: logs });
expect(res.consistency).toBe('unknown');
expect(res.reason).toBe('low_mean_cv_unreliable');
});
test('CV still classifies normally above the floor (NBA-scale stat)', async () => {
const logs = [20, 22, 19, 21, 20, 23, 18, 21, 20, 22].map((points) => ({ points }));
const res = await cs.getConsistency({ statType: 'points', gameLogs: logs });
expect(['elite', 'reliable']).toContain(res.consistency);
});
test('cvIsMeaningful is the explicit gate', () => {
expect(cs.cvIsMeaningful(0.6)).toBe(false);
expect(cs.cvIsMeaningful(12)).toBe(true);
});
});
-261
View File
@@ -1,261 +0,0 @@
const { gradeMlbProp, calculateMlbEdge, isMlbStatType } = require('../../src/services/mlbGrader');
const { evaluateMlbKillConditions, classifyLineMove, checkWeather } = require('../../src/services/mlbKillConditions');
const { MLB_PARKS, getParkByTeam } = require('../../src/constants/mlbParks');
jest.mock('axios');
const axios = require('axios');
describe('mlbGrader', () => {
describe('grade thresholds', () => {
test('Grade A when edge >= 5%', () => {
const result = gradeMlbProp({
player: 'Aaron Judge',
stat_type: 'home_runs',
line: 0.5,
direction: 'over',
seasonAvg: 0.7,
recentAvg: 0.8,
});
expect(result.grade).toBe('A');
expect(result.edge_pct).toBeGreaterThanOrEqual(5);
});
test('Grade B when edge 3-4%', () => {
// seasonAvg=5.15, line=5, direction=over => seasonEdge=(5.15-5)/5*100=3%
// recentAvg=5.2, line=5 => recentEdge=(5.2-5)/5*100=4%
// composite = 3*0.6 + 4*0.4 = 1.8+1.6 = 3.4
const result = gradeMlbProp({
player: 'Test Player',
stat_type: 'strikeouts',
line: 5,
direction: 'over',
seasonAvg: 5.15,
recentAvg: 5.2,
});
expect(result.grade).toBe('B');
expect(result.edge_pct).toBeGreaterThanOrEqual(3);
expect(result.edge_pct).toBeLessThan(5);
});
test('Grade C when edge 1-2%', () => {
// seasonAvg=5.05, line=5, direction=over => seasonEdge=1%
// recentAvg=5.1 => recentEdge=2%
// composite = 1*0.6 + 2*0.4 = 0.6+0.8 = 1.4
const result = gradeMlbProp({
player: 'Test Player',
stat_type: 'hits',
line: 5,
direction: 'over',
seasonAvg: 5.05,
recentAvg: 5.1,
});
expect(result.grade).toBe('C');
expect(result.edge_pct).toBeGreaterThanOrEqual(1);
expect(result.edge_pct).toBeLessThan(3);
});
test('Grade D when negative edge', () => {
const result = gradeMlbProp({
player: 'Test Player',
stat_type: 'hits',
line: 2,
direction: 'over',
seasonAvg: 1.5,
recentAvg: 1.3,
});
expect(result.grade).toBe('D');
expect(result.edge_pct).toBeLessThan(1);
});
});
describe('isMlbStatType', () => {
test('returns true for valid hitting stat', () => {
expect(isMlbStatType('hits')).toBe(true);
expect(isMlbStatType('home_runs')).toBe(true);
expect(isMlbStatType('stolen_bases')).toBe(true);
});
test('returns true for valid pitching stat', () => {
expect(isMlbStatType('strikeouts')).toBe(true);
expect(isMlbStatType('earned_runs')).toBe(true);
expect(isMlbStatType('pitches_thrown')).toBe(true);
});
test('returns false for invalid stat type', () => {
expect(isMlbStatType('three_pointers')).toBe(false);
expect(isMlbStatType('touchdowns')).toBe(false);
expect(isMlbStatType('')).toBe(false);
});
});
describe('calculateMlbEdge', () => {
test('calculates positive edge for over', () => {
const edge = calculateMlbEdge(6, 5, 'over');
expect(edge).toBe(20);
});
test('calculates positive edge for under', () => {
const edge = calculateMlbEdge(4, 5, 'under');
expect(edge).toBe(20);
});
test('returns 0 for null inputs', () => {
expect(calculateMlbEdge(null, 5, 'over')).toBe(0);
expect(calculateMlbEdge(5, null, 'over')).toBe(0);
});
});
});
describe('mlbKillConditions', () => {
function makeContext(overrides = {}) {
return {
inLineup: true,
pitcherScratched: false,
weather: { wind_speed: 5, wind_direction: 'OUT', temp: 75, humidity: 50 },
platoonDelta: 5,
paVsHandedness: 100,
lineMovement: 0,
hoursFromOpen: 1,
parkFactor: 1.0,
rainProbability: 10,
onInjuryReport: false,
...overrides,
};
}
test('LINEUP_OUT triggers when player not in lineup', () => {
const result = evaluateMlbKillConditions(makeContext({ inLineup: false }));
expect(result.some(c => c.code === 'LINEUP_OUT')).toBe(true);
});
test('PITCHER_SCRATCH triggers when pitcher scratched', () => {
const result = evaluateMlbKillConditions(makeContext({ pitcherScratched: true }));
expect(result.some(c => c.code === 'PITCHER_SCRATCH')).toBe(true);
});
test('WIND_IN triggers at 15mph+ blowing in', () => {
const result = evaluateMlbKillConditions(makeContext({
weather: { wind_speed: 18, wind_direction: 'IN', temp: 75, humidity: 50 },
}));
expect(result.some(c => c.code === 'WIND_IN')).toBe(true);
});
test('PLATOON_DISADVANTAGE triggers when delta > 12%', () => {
const result = evaluateMlbKillConditions(makeContext({ platoonDelta: 15 }));
expect(result.some(c => c.code === 'PLATOON_DISADVANTAGE')).toBe(true);
});
test('SMALL_SAMPLE triggers under 50 PA', () => {
const result = evaluateMlbKillConditions(makeContext({ paVsHandedness: 30 }));
expect(result.some(c => c.code === 'SMALL_SAMPLE')).toBe(true);
});
test('LINE_MOVE_AGAINST triggers at 0.5+ movement', () => {
const result = evaluateMlbKillConditions(makeContext({ lineMovement: 0.7, hoursFromOpen: 1 }));
expect(result.some(c => c.code === 'LINE_MOVE_AGAINST')).toBe(true);
});
test('PARK_SUPPRESSOR triggers below 0.90', () => {
const result = evaluateMlbKillConditions(makeContext({ parkFactor: 0.85 }));
expect(result.some(c => c.code === 'PARK_SUPPRESSOR')).toBe(true);
});
test('WEATHER_RAIN triggers above 50% probability', () => {
const result = evaluateMlbKillConditions(makeContext({ rainProbability: 65 }));
expect(result.some(c => c.code === 'WEATHER_RAIN')).toBe(true);
});
test('INJURY_REPORT triggers when on injury report', () => {
const result = evaluateMlbKillConditions(makeContext({ onInjuryReport: true }));
expect(result.some(c => c.code === 'INJURY_REPORT')).toBe(true);
});
test('HUMIDITY_SUPPRESSOR triggers at humidity > 80% and temp < 60F', () => {
const result = evaluateMlbKillConditions(makeContext({
weather: { wind_speed: 5, wind_direction: 'OUT', temp: 55, humidity: 85 },
}));
expect(result.some(c => c.code === 'HUMIDITY_SUPPRESSOR')).toBe(true);
});
});
describe('classifyLineMove', () => {
test('returns sharp for movement within first 2 hours', () => {
expect(classifyLineMove(0.7, 1)).toBe('sharp');
expect(classifyLineMove(-0.5, 0.5)).toBe('sharp');
});
test('returns public for movement after 4 hours', () => {
expect(classifyLineMove(0.6, 5)).toBe('public');
expect(classifyLineMove(-0.8, 6)).toBe('public');
});
test('returns null for movement under 0.5', () => {
expect(classifyLineMove(0.3, 1)).toBeNull();
});
});
describe('checkWeather', () => {
beforeEach(() => {
jest.clearAllMocks();
});
test('falls back to open-meteo on api.weather.gov timeout', async () => {
// Mock weather.gov to timeout
axios.get.mockImplementation((url) => {
if (url.includes('weather.gov')) {
return Promise.reject(new Error('timeout of 3000ms exceeded'));
}
// open-meteo fallback
return Promise.resolve({
data: {
hourly: {
temperature_2m: Array(24).fill(72),
relative_humidity_2m: Array(24).fill(50),
wind_speed_10m: Array(24).fill(10),
wind_direction_10m: Array(24).fill(180),
precipitation_probability: Array(24).fill(20),
},
},
});
});
const result = await checkWeather([40.8296, -73.9262], 3000);
expect(result.wind_speed).toBe(10);
expect(result.temp).toBe(72);
// Verify weather.gov was attempted first
expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining('weather.gov'),
expect.any(Object)
);
});
});
describe('mlbParks', () => {
test('has exactly 30 entries', () => {
expect(Object.keys(MLB_PARKS).length).toBe(30);
});
test('getParkByTeam returns correct park for NYY', () => {
const park = getParkByTeam('NYY');
expect(park).not.toBeNull();
expect(park.name).toBe('Yankee Stadium');
expect(park.coords).toEqual([40.8296, -73.9262]);
});
test('getParkByTeam returns correct park for LAD', () => {
const park = getParkByTeam('LAD');
expect(park.name).toBe('Dodger Stadium');
});
test('getParkByTeam returns null for invalid team', () => {
expect(getParkByTeam('XXX')).toBeNull();
});
test('every park has name, coords, and team', () => {
for (const [key, park] of Object.entries(MLB_PARKS)) {
expect(park.name).toBeDefined();
expect(park.coords).toHaveLength(2);
expect(park.team).toBeDefined();
}
});
});