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:
@@ -1,7 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
// Session 55 — the self-learning loop's public read endpoints. Redis is mocked
|
||||
// so these run offline; the store is seeded per-test via cacheGet.
|
||||
// Truth-Everywhere Part 2 (item 7) — the public accuracy endpoints now read the
|
||||
// CLEAN Postgres ledger aggregate (getModelAggregate / getAccuracyView), NOT the
|
||||
// Redis outcome log. We partial-mock ledgerService so these run offline.
|
||||
|
||||
const request = require('supertest');
|
||||
|
||||
@@ -14,12 +15,37 @@ jest.mock('../../src/utils/redis', () => ({
|
||||
isDegraded: () => false,
|
||||
}));
|
||||
|
||||
jest.mock('../../src/services/ledgerService', () => {
|
||||
const actual = jest.requireActual('../../src/services/ledgerService');
|
||||
return {
|
||||
...actual,
|
||||
getAccuracyView: jest.fn(),
|
||||
getModelAggregate: jest.fn(),
|
||||
accuracyBucketsFromAgg: actual.accuracyBucketsFromAgg, // keep the real bucketer
|
||||
};
|
||||
});
|
||||
|
||||
const ledgerService = require('../../src/services/ledgerService');
|
||||
const app = require('../../src/app');
|
||||
|
||||
beforeEach(() => { mockStore = {}; });
|
||||
const EMPTY_AGG = {
|
||||
window_days: 30, min_sample: 20, settled: 0, hits: 0, misses: 0, pushes: 0,
|
||||
hit_pct: null, beat_close_pct: null, clv_distribution: null, by_tier: {}, pending: 0,
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockStore = {};
|
||||
ledgerService.getAccuracyView.mockReset();
|
||||
ledgerService.getModelAggregate.mockReset();
|
||||
});
|
||||
|
||||
describe('GET /api/accuracy', () => {
|
||||
test('cold cache → valid empty-safe shape', async () => {
|
||||
test('cold (no data) → valid empty-safe shape from the ledger view', async () => {
|
||||
ledgerService.getAccuracyView.mockResolvedValue({
|
||||
overall: { sport: 'overall', window_days: 30, sample: 0, min_sample: 20,
|
||||
overall: { hits: 0, misses: 0, pushes: 0, total: 0, pct: null }, byGrade: {} },
|
||||
sports: {}, min_sample: 20, updated_at: null,
|
||||
});
|
||||
const res = await request(app).get('/api/accuracy');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveProperty('overall');
|
||||
@@ -27,13 +53,14 @@ describe('GET /api/accuracy', () => {
|
||||
expect(res.body.min_sample).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test('returns the persisted record when present', async () => {
|
||||
mockStore['accuracy:overall'] = {
|
||||
sport: 'overall', updated_at: '2026-07-10T00:00:00Z', window_days: 30, sample: 20,
|
||||
overall: { hits: 14, misses: 6, pushes: 0, total: 20, pct: 70 },
|
||||
byGrade: { 'A': { hits: 8, misses: 2, pushes: 0, total: 10, pct: 80 } },
|
||||
};
|
||||
mockStore['accuracy:mlb'] = mockStore['accuracy:overall'];
|
||||
test('returns the clean ledger record when present', async () => {
|
||||
ledgerService.getAccuracyView.mockResolvedValue({
|
||||
overall: { sport: 'overall', window_days: 30, sample: 20, min_sample: 20,
|
||||
overall: { hits: 14, misses: 6, pushes: 0, total: 20, pct: 70 },
|
||||
byGrade: { A: { hits: 8, misses: 2, pushes: 0, total: 10, pct: 80 } } },
|
||||
sports: { mlb: { sport: 'mlb', overall: { hits: 14, misses: 6, pushes: 0, total: 20, pct: 70 }, byGrade: {} } },
|
||||
min_sample: 20, updated_at: null,
|
||||
});
|
||||
const res = await request(app).get('/api/accuracy');
|
||||
expect(res.body.overall.overall.pct).toBe(70);
|
||||
expect(res.body.sports.mlb).toBeTruthy();
|
||||
@@ -41,25 +68,25 @@ describe('GET /api/accuracy', () => {
|
||||
});
|
||||
|
||||
describe('GET /api/ledger/accuracy', () => {
|
||||
test('returns grade-tier buckets from the accuracy record', async () => {
|
||||
mockStore['accuracy:overall'] = {
|
||||
sport: 'overall', updated_at: '2026-07-10T00:00:00Z', window_days: 30, sample: 15,
|
||||
overall: { hits: 10, misses: 5, pushes: 0, total: 15, pct: 67 },
|
||||
byGrade: {
|
||||
'A+': { hits: 3, misses: 0, pushes: 0, total: 3, pct: 100 },
|
||||
'A': { hits: 5, misses: 2, pushes: 0, total: 7, pct: 71 },
|
||||
'B': { hits: 2, misses: 3, pushes: 0, total: 5, pct: 40 },
|
||||
test('returns grade-tier buckets from the clean ledger aggregate', async () => {
|
||||
ledgerService.getModelAggregate.mockResolvedValue({
|
||||
...EMPTY_AGG, settled: 30, hits: 20, misses: 10, hit_pct: 67,
|
||||
by_tier: {
|
||||
'A+': { settled: 3, hits: 3, misses: 0, pushes: 0, hit_pct: null },
|
||||
'A': { settled: 22, hits: 15, misses: 7, pushes: 0, hit_pct: 68 },
|
||||
'B': { settled: 5, hits: 2, misses: 3, pushes: 0, hit_pct: null },
|
||||
},
|
||||
};
|
||||
});
|
||||
const res = await request(app).get('/api/ledger/accuracy');
|
||||
expect(res.status).toBe(200);
|
||||
expect(Array.isArray(res.body.buckets)).toBe(true);
|
||||
const grades = res.body.buckets.map((b) => b.grade);
|
||||
expect(grades).toContain('A+');
|
||||
expect(grades).toContain('A');
|
||||
expect(grades).toContain('A'); // A+ folds into A in the public strip
|
||||
expect(res.body.overall.pct).toBe(67);
|
||||
});
|
||||
|
||||
test('cold cache → empty buckets, never 500', async () => {
|
||||
test('empty aggregate → empty buckets, never 500', async () => {
|
||||
ledgerService.getModelAggregate.mockResolvedValue({ ...EMPTY_AGG });
|
||||
const res = await request(app).get('/api/ledger/accuracy');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.buckets).toEqual([]);
|
||||
|
||||
@@ -65,6 +65,7 @@ describe('getModelAggregate scoping', () => {
|
||||
});
|
||||
|
||||
test('user scope renders percentages at n≥20 like the public record', async () => {
|
||||
process.env.CLV_CAPTURE_RELIABLE = '1'; // exercise the CLV math (suppressed by default, item 7)
|
||||
const rows = [
|
||||
...Array.from({ length: 14 }, () => ({ outcome: 'hit', clv_result: 'beat', grade: 'A' })),
|
||||
...Array.from({ length: 7 }, () => ({ outcome: 'miss', clv_result: 'faded', grade: 'B' })),
|
||||
@@ -73,5 +74,6 @@ describe('getModelAggregate scoping', () => {
|
||||
expect(agg.settled).toBe(21);
|
||||
expect(agg.hit_pct).toBe(Math.round((14 / 21) * 100));
|
||||
expect(agg.beat_close_pct).toBe(Math.round((14 / 21) * 100));
|
||||
delete process.env.CLV_CAPTURE_RELIABLE;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -206,6 +206,27 @@ describe('captureClosing — real feed values only', () => {
|
||||
});
|
||||
|
||||
describe('getModelAggregate — never a % under min sample', () => {
|
||||
// beat_close/CLV are suppressed by default (item 7 — CLV capture broken until
|
||||
// C4). These tests exercise the CLV MATH, so enable the reliable flag; a
|
||||
// separate test below locks the default-suppressed behavior.
|
||||
beforeAll(() => { process.env.CLV_CAPTURE_RELIABLE = '1'; });
|
||||
afterAll(() => { delete process.env.CLV_CAPTURE_RELIABLE; });
|
||||
|
||||
test('beat_close is SUPPRESSED by default until C4 (CLV capture broken)', async () => {
|
||||
delete process.env.CLV_CAPTURE_RELIABLE; // default state
|
||||
const sb = fakeSb();
|
||||
sb._state.selectResults = [[
|
||||
...Array.from({ length: 13 }, () => ({ outcome: 'hit', clv_result: 'beat' })),
|
||||
...Array.from({ length: 7 }, () => ({ outcome: 'miss', clv_result: 'faded' })),
|
||||
]];
|
||||
sb._state.countResult = 0;
|
||||
const agg = await ledger.getModelAggregate({ sb });
|
||||
expect(agg.hit_pct).toBe(65); // hit rate still renders (it's real)
|
||||
expect(agg.beat_close_pct).toBeNull(); // BEAT CLOSE hidden — measured-wrong
|
||||
expect(agg.clv_distribution).toBeNull();
|
||||
process.env.CLV_CAPTURE_RELIABLE = '1'; // restore for the rest of the block
|
||||
});
|
||||
|
||||
test('below 20 settles → hit_pct/beat_close_pct null, counts real', async () => {
|
||||
const sb = fakeSb();
|
||||
sb._state.selectResults = [
|
||||
@@ -296,6 +317,9 @@ describe('indexProps — prefers a book row with both sides priced', () => {
|
||||
// centralized HERE (getModelAggregate) — consumers never re-derive it.
|
||||
describe('getModelAggregate — clv_distribution (n>=20 gate lives in the service)', () => {
|
||||
const { clvBucketIndex, CLV_BUCKETS } = ledger.__internals;
|
||||
// CLV suppressed by default (item 7); enable to test the distribution math.
|
||||
beforeAll(() => { process.env.CLV_CAPTURE_RELIABLE = '1'; });
|
||||
afterAll(() => { delete process.env.CLV_CAPTURE_RELIABLE; });
|
||||
|
||||
test('below 20 settles → clv_distribution is null (never a small-sample chart)', async () => {
|
||||
const sb = fakeSb();
|
||||
|
||||
Reference in New Issue
Block a user