octavioloffaro Claude Opus 5 commited on
Commit
992fce0
·
1 Parent(s): 0e5d04f

Fix scoring engine: restore ML path, remove synthetic ensemble, secure key issuance

Browse files

Brings the Space up to date with the audit fixes.

Availability:
- sqlite3 shim re-exports the real Row/Connection/Cursor. The stub Row broke
the SQLite path outright and the missing Connection broke pandas.read_sql_query
at 17 call sites, which silently dropped every request to the baseline heuristic.
- init_db() no longer aborts module import, so a database outage returns
503 DEGRADED from /health instead of crash-looping uvicorn.
- Unknown user_id returns 404 rather than scoring default features and leaking a
foreign-key error; all scoring connections are wrapped in try/finally with
rollback, so one failed request no longer leaves a write lock behind.

Scoring correctness:
- Snapshot date defaults to the user's own latest transaction rather than the
global maximum, which had left 98% of users on the cold-start heuristic.
Adds a data_freshness block reporting snapshot age.
- Removes the synthetic ensemble that added np.random.normal() noise to the
XGBoost output four times and averaged it back over the prediction. Reports
the calibrated probability directly; the rule-based baseline becomes a real
second opinion under model_agreement.
- One positive sign convention for the OOD anomaly score across scoring,
uncertainty_science and confidence_governance; shift_strength clamped to [0,1].
Epistemic uncertainty is normalised against the detector's own cut-point so
total_uncertainty stays in [0,1].
- Removes three remaining /100.0 divisors applied to an already 0-1 probability.
The cold-start branch computes real macro sensitivities and evaluates the
thin-file cohort instead of hardcoding 0.5 and False.

Security:
- API key generation and revocation move behind an ADMIN_API_KEY secret,
compared with secrets.compare_digest and failing closed when unset. Both
endpoints previously accepted anonymous callers.

Adds src/load_to_postgres.py to seed a fresh Postgres from a local SQLite file.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

12 files changed +643 -123
Files changed (12)
src/api.py+64-11
src/confidence_governance.py+6-1
src/continuous_learning.py+37-4
src/load_to_postgres.pyAdded+251
src/models/archetype_discoverer.pkl+1-1
src/models/fca_model_card.json+6-6
src/models/fca_model_card.md+6-6
src/models/model_metadata.pkl+1-1
src/models/multi_account_priors.pkl+1-1
src/scoring.py+229-82
src/sqlite3.py+25-5
src/uncertainty_science.py+16-5