caelum0x 4016a68d18 feat(ai): BYOK AI gateway for Scribase projects with per-key caps, traces and a secure connect page (PY1.4)
Railyard side of PY1.4 (ADR RY-0003). Acceptance:
1. Bridge `ai` gives a Scribase project environment one key (ry_ai_..., shown
   once, to the Scribase control plane only, stored here as SHA-256; `rotate`
   revokes the old one) and a one-time link to /connect/ai (15 minutes), the
   only place a provider key is typed. It is sealed with AES-256-GCM under
   RAILYARD_CREDENTIALS_KEY, bound to org and provider, and only a
   four-character hint is ever read back.
2. BYOK only: POST /api/v1/ai/chat/completions (OpenAI-compatible) refuses
   with 403 byok_required when the org has not connected the provider the
   model names, and never calls Relay then. When it does call, the customer's
   key fills that provider's slot and every other slot carries a refusal
   marker, so Relay (caller credential wins) can never use our keys, not even
   on failover; a test pins the slot list to Relay's PROVIDER_KEY_SLOTS.
   Credentials in a request body are refused.
3. Daily and monthly token caps per key (429 with the period); usage (atomic
   rpc) and one trace per call (model, provider, status, tokens, latency),
   read per project key by the bridge `ai_usage` route for the Scribase tab;
   Relay traces carry tensorzero::tags for the same filter.
4. Copy says "your provider bills you directly"; no credits are included
   anywhere (copy lint over the connect page and every gateway message).
apps/relay/config/tenants/README.md records the tenant rules.

Migration: apps/console/db/migrations/0003_ai_gateway.sql (idempotent;
applied twice to a scratch local cluster by test/migration.test.ts).
Env: RELAY_URL (var), RELAY_TENANT_KEY, RAILYARD_CREDENTIALS_KEY (secrets).

Tests: console 63/63 (gateway 6, bridge 12, migration 3, repo 5), tsc clean,
tests/contract 3/3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 21:44:04 +03:00

18 lines
1.9 KiB
Markdown

# Relay tenants: Scribase projects (Wave P PY1.4)
Scribase projects call AI models through the Railyard console's gateway
(`apps/console/lib/ai/gateway.ts`, `POST /api/v1/ai/chat/completions`), which
forwards to Relay with one tenant key. The rules, all enforced by tests:
| Rule | Where |
|---|---|
| **BYOK only** (ADR RY-0003). A call is forwarded only when the org connected its own account for the provider the model names. Otherwise 403 `byok_required`, and nothing reaches Relay. | `lib/ai/gateway.ts`, `gateway.test.ts` |
| **Never our keys.** The console puts the customer's key in that provider's `dynamic::` slot and a refusal marker in every other slot of `tensorzero::credentials`. Relay lets a caller-supplied credential win over its stored and environment keys (`crates/gateway/src/provider_keys.rs`, `existing.entry(name).or_insert(value)`), so no request on the tenant key can use ours, not even on failover. A test compares the console's slot list with `PROVIDER_KEY_SLOTS`. | `gateway.test.ts` |
| **Credentials never travel with a request.** A body that carries `tensorzero::credentials` is refused; keys are typed once on `/connect/ai` (one-time link) and sealed with AES-256-GCM, bound to org and provider. | `lib/ai/provision.ts` |
| **Caps.** Per project key: tokens per UTC day and per month (429 with the period). The tenant key itself has a Relay spend cap against runaway loops (`relay-tenancy.sh tenant-key`). | `gateway.ts`, infra-box `stacks/railyard/relay-tenancy.sh` |
| **Traces by key.** Each call records model, provider, status, tokens and latency per project key, and carries `tensorzero::tags` (`tenant=scribase`, the org and the key id) so Relay's own traces can be filtered the same way. | `ai_traces`, bridge `ai_usage` |
The tenant key is created on the box with `relay-tenancy.sh tenant-key` (saved
0600, never printed) and given to the console as `RELAY_TENANT_KEY` by the
owner (`wrangler secret put`, reading the file).