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
+44 -4
View File
@@ -18,7 +18,12 @@
* - game logs unavailable → consistency defaults to 'unknown'
*
* 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)
@@ -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 };
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: [] };
const result = await consistencyScore.getConsistency({
playerName, sport, statType, gameLogs: logs,
@@ -231,8 +244,35 @@ async function computeFeaturesForProp(rawProp = {}) {
const game = teamAbbr ? await lookupTodayGame({ sport, teamAbbr }) : null;
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 = {
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({
@@ -344,7 +384,7 @@ async function computeFeaturesForProp(rawProp = {}) {
});
const { result: consistency, gameLogs } = await safeGetConsistency({
playerName: player, sport, statType,
playerName: player, sport, statType, statRows,
});
return {
+33 -1
View File
@@ -41,6 +41,32 @@ function classify(cv) {
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) {
const clean = values.filter((v) => Number.isFinite(v));
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 s = statsFor(values);
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) };
}
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) {
const delta20 = (features.l20_avg - line) / line;
if (overWeighted && delta20 > 0) factors.push({ label: 'l20_over_line', delta: 1.0, magnitude: Math.abs(delta20) });
else if (!overWeighted && delta20 < 0) factors.push({ label: 'l20_under_line', delta: 1.0, magnitude: Math.abs(delta20) });
if (overWeighted) {
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.
+76
View File
@@ -199,6 +199,80 @@ function nbaGameLogFeatures(res, statType) {
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) {
// 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
@@ -401,6 +475,8 @@ function getCacheStats() {
module.exports = {
getFeatures,
getStatRows,
gameCountInWindow,
clearCache,
getCacheStats,
// 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
// when the stats-resolve fallback misses. Returns {} for MLB / errors.
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 ts = deps.now();
@@ -255,6 +262,22 @@ async function runSnapshot(sport, opts = {}) {
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
// letting it write (we re-write an ENRICHED version below).
let envelope = null;