feat(ai): capability loader, gates and refusal envelope

loadAiCapabilityInputs reads the switches, module access, provider
presence and active consent kinds for one record in parallel, memoised
per request, and fails closed to check_failed. requireAiCapability,
getAiCapability and aiCapabilityForJob resolve for the request's
record, masked to the active grant like moduleAccess, or for a job's
record under the worker's authority.

apiHandler renders AiUnavailableError as one envelope with errorCode,
capability, reason and module. Existing codes stay: the operator
family keeps assistant.disabled.<switch>, modules keep module.disabled,
consent keeps consent.ai.required. New codes are ai.record.notPermitted,
ai.provider.none and ai.unavailable.
This commit is contained in:
Marc-André Bombeck committed 2026-09-24 07:46:12 +02:00
1 parent 1be2392c81
commit 7c757520f1
8 files changed
+873 -28

No files matched your search

+6 -3
View File
@@ -22,9 +22,11 @@
*
* Not in this list, and deliberately:
*
* - `assistant.disabled.<surface>` is built from a template at runtime
* (`src/lib/feature-flags/index.ts`), so the surface segment is open.
* `assistant.disabled.coach` is the one the iOS contract names.
* - `assistant.disabled.<switch>` is built from a template at runtime
* (`src/lib/ai/capabilities/refusal.ts`, and the retired
* `src/lib/feature-flags/index.ts` gate), so the last segment is open. It
* is one of `coach`, `briefing`, `insightStatus`, `documentAi` and
* `enabled`; `assistant.disabled.coach` is the one the iOS contract names.
* - The integration-probe classes — `credentials_rejected`, `rate_limited`,
* `upstream_error`, `timeout`, `connection_failed`, and the per-provider
* additions beside them — are chosen by each `/test` route's own
@@ -36,6 +38,7 @@
*/
export const ERROR_CODE_CATALOGUE: Readonly<Record<string, readonly string[]>> =
{
ai: ["ai.provider.none", "ai.record.notPermitted", "ai.unavailable"],
ai_provider: ["ai_provider.invalid", "ai_provider.no_fields"],
allergy: ["allergy.invalid"],
analytics: ["analytics.invalid_query"],