Item 7 — public accuracy reads the CLEAN ledger; BEAT CLOSE hidden until C4

Kev's call: the 30D accuracy surfaces must read TRUTH, not a cache that can't be
filtered. My earlier degraded-row exclusion only touched getModelAggregate
(Postgres); the public buckets/badge still read outcomeService (Redis outcome
log), which counts degraded projection-0 outcomes and has no field to filter on.

- /api/accuracy (AccuracyBadge) + /api/ledger/accuracy (buckets/ModelRecord)
  now source from the clean Postgres ledger aggregate via new
  ledgerService.getAccuracyView + accuracyBucketsFromAgg (model_value > 0
  excludes degraded rows). Same response shapes → no frontend change. Redis
  outcome log is now read by nothing public; it can age out or be rebuilt.

- BEAT CLOSE is a MEASURED-WRONG ZERO: captureClosing re-records the locked line
  as the "closing" line, so clv is flat on the whole sample and beat_close reads
  0% (comparing a number to itself). Full write-up: specs/audit-data/
  clv-capture-broken.md (the fix belongs to C4). Until then, beat_close_pct +
  clv_distribution are SUPPRESSED at the source (getModelAggregate, gated by
  clvCaptureReliable() / CLV_CAPTURE_RELIABLE=1). Every public surface already
  renders BEAT CLOSE only when non-null, so they all hide it now — no wrong zero
  anywhere. HIT RATE (real) is unaffected.

Suite 271/3261 green, web build exit 0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kev
2026-07-17 16:08:18 -04:00
parent 36e653d695
commit 89a2977f57
7 changed files with 222 additions and 35 deletions
+84 -2
View File
@@ -37,6 +37,12 @@ const AGG_WINDOW_DAYS = 30;
const AGG_FETCH_LIMIT = 5000;
/** Below this many settled rows, callers must not render a percentage. */
const MIN_AGG_SAMPLE = 20;
// Truth-Everywhere Part 2 (item 7) — CLV capture is broken (closing_line ==
// locked_line; see the C4 finding). Until C4 records a real closing line,
// beat_close/CLV are suppressed everywhere. Read at call time (not module load)
// so C4 can flip it via CLV_CAPTURE_RELIABLE=1 without a redeploy, and tests can
// exercise the CLV math directly.
function clvCaptureReliable() { return process.env.CLV_CAPTURE_RELIABLE === '1'; }
/**
* S6 (A1 board) — CLV distribution buckets (the MODEL tab strip). Signed CLV:
@@ -515,14 +521,22 @@ async function getModelAggregate(opts = {}) {
if (agg.settled >= MIN_AGG_SAMPLE && decided > 0) {
agg.hit_pct = Math.round((agg.hits / decided) * 100);
}
if (agg.settled >= MIN_AGG_SAMPLE && agg.clv_sample > 0) {
// Truth-Everywhere Part 2 (item 7) — CLV is currently MEASURED WRONG:
// captureClosing re-records the LOCKED line as the "closing" line
// (closing_line == locked_line across the whole sample), so every row's CLV
// computes to 0/flat and beat_close reads a fabricated-looking 0%. That's
// comparing a number to itself. Until C4 (real closing-line capture) lands,
// CLV_CAPTURE_RELIABLE stays false and beat_close_pct / clv_distribution are
// suppressed at the SOURCE — every public surface hides BEAT CLOSE rather
// than showing a measured-wrong zero. Flip this to true when C4 ships.
if (clvCaptureReliable() && agg.settled >= MIN_AGG_SAMPLE && agg.clv_sample > 0) {
agg.beat_close_pct = Math.round((agg.clv_beat / agg.clv_sample) * 100);
}
// S6 (A1 board) — clv_distribution rides the SAME n≥20 gate (this is the
// single home of the gate — consumers never re-derive it). Null below the
// sample floor or with zero settled clv values; the UI renders nothing.
agg.clv_distribution = null;
if (agg.settled >= MIN_AGG_SAMPLE && agg.clv_sample > 0) {
if (clvCaptureReliable() && agg.settled >= MIN_AGG_SAMPLE && agg.clv_sample > 0) {
const dist = CLV_BUCKETS.map((b) => ({ ...b, count: 0 }));
let counted = 0;
for (const r of settledRows || []) {
@@ -534,6 +548,72 @@ async function getModelAggregate(opts = {}) {
return agg;
}
// Truth-Everywhere Part 2 (item 7) — the public 30D accuracy VIEW, built from
// the CLEAN ledger aggregate (model_value > 0), NOT the Redis outcome log
// (which still counts degraded projection-0 rows and can't be filtered). Same
// shape the AccuracyBadge / buckets consumed from outcomeService, so no
// frontend change. Redis is a cache; when a cache can't be filtered, read truth.
const ACCURACY_VIEW_SPORTS = ['mlb', 'wnba', 'nba', 'soccer'];
function _aggToRecord(agg, sport) {
const byGrade = {};
for (const [tier, b] of Object.entries(agg.by_tier || {})) {
byGrade[tier] = {
hits: b.hits, misses: b.misses, pushes: b.pushes,
total: b.hits + b.misses + b.pushes, pct: b.hit_pct ?? null,
};
}
return {
sport,
updated_at: null,
window_days: agg.window_days,
sample: agg.settled,
min_sample: agg.min_sample,
overall: {
hits: agg.hits, misses: agg.misses, pushes: agg.pushes,
total: agg.hits + agg.misses + agg.pushes, pct: agg.hit_pct ?? null,
},
byGrade,
};
}
async function getAccuracyView(opts = {}) {
const base = { sb: opts.sb, nowMs: opts.nowMs };
const overallAgg = await getModelAggregate(base);
const sports = {};
for (const s of ACCURACY_VIEW_SPORTS) {
const a = await getModelAggregate({ ...base, sport: s });
if (a.settled > 0) sports[s] = _aggToRecord(a, s);
}
return {
overall: _aggToRecord(overallAgg, 'overall'),
sports,
min_sample: overallAgg.min_sample,
updated_at: null,
};
}
// Grade-tier buckets for the ledger accuracy strip, from the clean aggregate.
function accuracyBucketsFromAgg(agg) {
// First-letter buckets (A+ folds into A for the public strip, matching the
// old outcomeService.accuracyBuckets contract), n≥20 gate per bucket.
const order = ['A', 'B', 'C', 'D', 'F'];
const rolled = {};
for (const [tier, b] of Object.entries(agg.by_tier || {})) {
const k = tier === 'A+' ? 'A' : tier[0];
rolled[k] = rolled[k] || { hits: 0, misses: 0, total: 0 };
rolled[k].hits += b.hits;
rolled[k].misses += b.misses;
rolled[k].total += b.hits + b.misses + b.pushes;
}
return order
.filter((k) => rolled[k] && rolled[k].total > 0)
.map((k) => {
const r = rolled[k];
const decided = r.hits + r.misses;
const pct = r.total >= MIN_AGG_SAMPLE && decided > 0 ? Math.round((r.hits / decided) * 100) : null;
return { grade: k, hits: r.hits, total: r.total, pct };
});
}
module.exports = {
recordPipelineGrades,
captureClosing,
@@ -542,6 +622,8 @@ module.exports = {
applyRevision,
countRowsForDate,
getModelAggregate,
getAccuracyView,
accuracyBucketsFromAgg,
MIN_AGG_SAMPLE,
__internals: {
rowsFromSnapshot, computeClv, clvResultOf, indexProps, gameIdFor,