Files
CatoandClaude Opus 5 d050d1589c feat(health): /api/health/ai — make the 2026-08-28 outage observable BEFORE a user hits it (#184)
On 2026-08-28 /api/health reported "healthy" while every AI feature on
this site was failing on an invalid Groq key. Carrying `llm` in that body
fixed the REPORTING. It did not make the state observable before a user
hit it, because nothing calls a provider until somebody uses the bot — so
straight after a deploy the field reads "unknown", and unknown is what it
stays.

  GET /api/health/ai            free. What the last real call did.
  GET /api/health/ai?probe=1    a real call. 200 or 503. Needs the secret.

Opposite contract to /api/health on purpose: that route is LIVENESS and
must stay 200 while the LLM is down, because restarting does not fix an
expired key and failing on it would get a healthy process killed. This
one is the inverse, so an uptime monitor can page on a real AI outage
without paging on every deploy.

It probes THIS app's chain: `ask` (ai-kit 0.15.0) calls
generateWithBestProvider, the same function the chat routes use. A probe
built from its own provider list would test a path nothing else takes and
would drift from this one the first time the chain changed.

The secret is a GETTER — the handler is built once and reused, so a
captured string would be whatever the environment held on the first
request: un-rotatable without a restart, and untestable.

TWO THINGS THE TESTS TAUGHT ME, both about this app rather than the
probe:

  - generateWithBestProvider goes through getServerEnv(), which validates
    the WHOLE server env and throws on the first missing var. A probe
    test that sets only the AI keys fails on SUPABASE_SERVICE_ROLE_KEY,
    nowhere near the chain.
  - Ollama is tried first and its availability is a LIVE PING, so a fetch
    mock that answers every URL makes a local runtime appear to exist and
    win the chain. The mock now refuses :11434, which is prod's real
    shape — and the "which vendor served it" assertion looks at every
    call rather than calls[0], because calls[0] is that ping.

verify green — 301 tests.


Claude-Session: https://claude.ai/code/session_01NHDLTKGirKU7He8q2YR2y4

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-07 14:56:46 +00:00
..