Skip to content

Uncertainty engine: Hono routes (#99)#110

Merged
ssilvius merged 2 commits intofeat/uncertainty-schemafrom
feat/uncertainty-routes
Apr 28, 2026
Merged

Uncertainty engine: Hono routes (#99)#110
ssilvius merged 2 commits intofeat/uncertainty-schemafrom
feat/uncertainty-routes

Conversation

@ssilvius
Copy link
Copy Markdown
Contributor

Closes #99. Stacks on #109.

Four endpoints under /api/uncertainty, all gated by requireAuth:

  • POST /predictions: emits a prediction. Computes cohort_key from surface|model|model_version|bucketLower(claimed_confidence). Writes state='emitted', orphan_after = now + ttl. Returns id + orphan_after.
  • PUT /predictions/:id/witness: idempotent. 404 if missing, 409 if already witnessed. Otherwise flips to state='witnessed' with outcome label/correctness/payload and witnessed_at.
  • GET /calibration?surface&model&model_version: returns the cached snapshot bucket rows for the matching cohort prefix, ordered by bucket lower bound.
  • GET /orphans: per-surface counts grouped by state.

Pure cohort/Brier helpers live in api/lib/uncertainty/cohort.ts so they unit-test without dragging Hono or D1 into the test surface. 18 tests cover bucketing, cohort composition, Brier math, and the schema guards on confidence range and the state/outcome enums.

Mounts in both api/app.ts (AppType source) and pages/api/[...slug].ts (the inlined runtime app, per the existing dual-mount pattern). db/client.ts now wires the uncertainty schema through drizzle.

Closes #99. Phase 1 of the uncertainty engine spike.

Four endpoints under /api/uncertainty, all gated by requireAuth:

- POST /predictions: emits a prediction. Computes cohort_key from
  surface|model|model_version|bucketLower. Writes state='emitted',
  orphan_after = now + ttl. Returns id + orphan_after.
- PUT /predictions/:id/witness: idempotent. 404 if missing,
  409 if already witnessed. Otherwise flips to state='witnessed'
  with outcome label/correctness/payload and witnessed_at.
- GET /calibration?surface&model&model_version: returns the
  cached snapshot bucket rows for the matching cohort prefix,
  ordered by bucket lower bound.
- GET /orphans: per-surface counts grouped by state.

Pure cohort/Brier helpers in api/lib/uncertainty/cohort.ts so they
unit-test without needing Hono or D1 in the test surface. Vitest
covers bucketing, cohort composition, Brier math, and the schema
guards on confidence range and state/outcome enums (18 tests).

Mounts in both api/app.ts (AppType source) and pages/api/[...slug].ts
(the inlined runtime app, per the existing dual-mount pattern).
db/client.ts now wires the uncertainty schema through drizzle.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@ssilvius ssilvius merged commit a8c945e into feat/uncertainty-schema Apr 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant