Skip to content

CONTRACT: feat(ui-sdk,core): relay Jev reranking for governed workers - #4024

Merged
schlessera merged 8 commits into
mainfrom
feat/3792-relay-jev-reranking
Nov 15, 2026
Merged

schlessera merged 8 commits into
mainfrom
feat/3792-relay-jev-reranking

Conversation

@schlessera

@schlessera schlessera commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Closes #3792

What changed

A governed or strict turn worker's brain search and brain_search are reranked by jev again. Before, the worker never held TYPESAFE_API_KEY and the CLI provider relay had no family for Jev, so every hosted search printed provider_unavailable and fell back to lifecycle order. The relay now has a typesafe family (x-brain-provider: typesafe) whose only route is POST /v1/systemone. The host puts its key on that request as Authorization: Bearer, for https://api.typesafe.ai or an exact origin the operator lists in BRAIN_UI_CLI_PROVIDER_ORIGINS. Each attempt is metered as operation reranking. No rate is held for it, so it counts as unknown spend and toward the unpriced-token stops, never as $0. The in-worker reranker (jevReranker) and its selection (selectReranker, brain doctor) use the relay when the key is withheld and a relay socket is present. A host without the relay keeps the notice. Credentialed maintenance runs keep TYPESAFE_API_KEY as before.

Why this shape

It follows the existing families (#1673, #2738): a fixed first-party route, exact origin and key binding, worker credentials stripped, the host key placed on the host, and budget admission before and just before dispatch. Nothing about validation, cancellation, redirects or redaction changes. I named the family after the vendor (typesafe), as gemini, anthropic and openai are, rather than after the model.

Maintenance runs deliberately keep the key. Once Jev is a built-in family, relayedProviderKeys would otherwise strip TYPESAFE_API_KEY from every credentialed maintenance run. That would break their sync judge and propagation review, which call TypeSafe directly through core's createJevClient, outside the relay. The issue puts those uses out of scope, so maintenanceRelayedKeys (packages/ui-server/src/brain/host-boundary.ts) leaves the key in place unless the operator lists it, which is the same behaviour as today. The agent-facing relay note and the worker-host status line now say JeV is relayed. The note also stopped saying video is not relayed, which has been wrong since #2738.

Contract (additive): a new relay family and route in docs/integration-contract/cli.md ("Provider relay"), plus an index entry in docs/integration-contract.md. The @schlessera/brain-common ProviderRelayFamily and ui-sdk ProviderOperation unions gain a member each. Both are /internal, and the regenerated api-report/ is unchanged. A minor changeset is included.

Proof

Failing first, on the tree before the change:

  • packages/core/tests/provider-relay-jev.test.ts: RELAYED_JEV_SELECTED, expected "jev", received "heuristic".
  • packages/ui-sdk/tests/provider-relay.test.ts (jev reranking (#3792), 12 cases): the reranking request got 403 instead of 200. Each refusal case failed on JEV_RELAY_REFUSES_OTHER_REQUESTS, getting family: null, reason: "unknown_family" instead of the route-specific reason.

Mutations, each restored after:

  • The issue's mutation. In jevReranker, replace providerRelayFetch("typesafe", apiKeyEnv) with undefined, so the client goes direct. Both bubblewrap end-to-end cases in packages/ui-sdk/tests/worker-provider-relay.test.ts (a governed/strict turn worker's jev reranking (#3792)) fail on RELAYED_JEV_NO_NOTICE.
  • Host key placement. Drop the typesafe key placement in provider-relay.ts. JEV_RELAY_PLACES_HOST_KEY (unit) and RELAYED_JEV_HOST_KEY_PLACED (both bubblewrap cases) fail.
  • Free pricing. Price reranking at $0 in priceProviderAttempt. JEV_RERANKING_UNPRICED in packages/ui-server/tests/provider-relay-budgets.test.ts fails.

End to end, under real bubblewrap, governed and strict, keyless:

  • The real brain search --rerank jev reaches the relay. A loopback stand-in on the Jev route gets Bearer <host key> and no worker headers, and the stand-in's choice ranks first.
  • The receipt is typesafe/reranking with the reported usage. The worker's environment holds no TYPESAFE_API_KEY and names it in BRAIN_WITHHELD_KEYS.
  • A blocked budget refuses before contact.

Local runs: 171 tests pass across the affected core, common, ui-sdk and ui-server relay, reranker and budget files. bun run typecheck, bun run lint, bun scripts/env-docs.ts --check and bun run api-report (no diff) are clean.

Draft heavy feedback at bb51fc83 (run 43736254493): unit 1-4, runtime, runtime_host, browser 1-3, layout 1-2, captures, endurance and pack all passed, including the maintenance relay and provider-history tests below.

Not run locally, needs hosted proof. This host cannot create the namespaces some tests need (unshare: write failed /proc/self/uid_map; bwrap: No permissions to create a new namespace), even with sandbox escalation. Those tests are:

  • packages/ui-server/tests/maintenance-provider-relay.test.ts. It already asserts maintenance keeps TYPESAFE_API_KEY and names only ANTHROPIC_API_KEY,GEMINI_API_KEY,OPENAI_API_KEY withheld, which is exactly what maintenanceRelayedKeys preserves.
  • The provider-history guards (packages/ui-backend-{claude,pi}/tests/provider-history.test.ts).
  • Four worker-sandbox.test.ts cases, which fail the same way on this host without the change.

Citations my edits moved (13) are repaired; no drift remains in the touched files.

Collisions at head bb51fc83009ecf32db07212fbbdefbe4285fafea (unchanged by the later merge of origin/main at 4035f977e, and later eb3c53ac2, which overlapped only docs/decisions/governed-writable-workers.md and docs/integration-contract/package-api.md and merged cleanly; check:pr rerun at the merge head. Later refreshes onto ed4eb5a0c and 2702e2bdd touched none of this PR's files and were merges only. Merging #4008 (e1ce188d7 on main) overlapped docs, host-boundary.ts and env.ts; its only conflicts were two citation line numbers in governed-writable-workers.md, resolved to the merged anchors, and check:pr was rerun at 86d8a9cd4) (bun scripts/collisions.ts --json --paths <diff>): path overlaps with #3622, #3806, #4002, #4008 and #4018, all in docs or host-boundary.ts/env.ts, plus closure-only overlaps with #3054, #3056, #3059, #3069, #3116, #3437, #3581, #4021, #4022 and #4023.

bun run check:pr --base origin/main
{"version":1,"kind":"brain-check-pr-receipt","head":"86d8a9cd40a80dc737c7456b1ca8a6127ef865a4","base":"origin/main","mode":"focused","startedAt":"2026-11-15T20:26:29.549Z","finishedAt":"2026-11-15T20:28:37.449Z","checks":[{"name":"all lint and leakage gates","categories":["changeset"],"outcome":"passed","elapsedMs":19090.412346000005},{"name":"environment documentation","categories":["changeset"],"outcome":"passed","elapsedMs":236.80904400000145},{"name":"pending changeset package names","categories":["changeset"],"outcome":"passed","elapsedMs":209.0428400000019},{"name":"contribution changeset","categories":["changeset"],"outcome":"passed","elapsedMs":25.69923800000106},{"name":"affected fast invariants","categories":["verify","unit"],"outcome":"passed","elapsedMs":108318.90104}]}

Guard checklist (the relay route allowlist gained one route):

  • Trigger. A worker request under the typesafe family other than an exact POST /v1/systemone with no query is refused (route_not_allowed / method_not_allowed). So is the key toward any origin other than https://api.typesafe.ai or a listed one (recipient_not_authorized). It does not cover what a request body contains, as for the other families.
  • Restriction. It restricts that one request, before contact. The rest of the turn and search continue, and search falls back to lifecycle order with a warning.
  • Restricted mode. The search itself is the restricted mode: it is already degraded, not refused.
  • Clearing. Nothing latches. Each request is decided on its own.
  • Record. The relay's refused callback and the turn's egress record, as for the other families.
  • Scope test. The refusal cases and the blocked-budget case show search still returning results.

Checklist

  • Focused local preflight/behavioral receipts passed; complete selected hosted proof is requested through confirmation.
  • Acceptance criteria from the issue are all met.
  • The PR and completed/advanced issues have verified release milestones under docs/process/github.md.
  • A changeset is included.
  • No personal data anywhere in the diff.
  • The integration contract index and relevant component are updated in this same commit.
  • Docs under docs/ reflect the change.
  • The route guard answers the proportionate-containment checklist above.
  • Provider history stays append-only: only the fixed relay-note constant changed, between releases, never within a session. The guard tests run in hosted proof; this host cannot run them.

Anything left open

Nothing for this issue. The sync judge's and propagation review's direct Jev use in maintenance workers stays out of scope, as the issue says (#2504 owns the model-alias mismatch).

🤖 Generated with Claude Code

A governed or strict turn worker never holds TYPESAFE_API_KEY, and the CLI
provider relay had no family for Jev, so every hosted search ran in
lifecycle order with a provider_unavailable notice.

The relay gains the `typesafe` family: one route, POST /v1/systemone, with
the host's key placed as a bearer token for https://api.typesafe.ai (or an
operator-listed exact origin). Each attempt is metered as operation
`reranking`; no rate is held for it, so its cost is unknown, never $0. The
in-worker jev reranker and its selection use the relay when the key is
withheld and a relay socket is present; hosts without the relay keep the
notice. Credentialed maintenance runs keep the key, since their sync judge
and propagation review call TypeSafe directly.

Refs #3792

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schlessera schlessera added this to the 0.45.0 milestone Nov 15, 2026
@schlessera schlessera added type: feat A new capability or a change in user-visible behaviour. area: core packages/core: the brain CLI, indexer, hybrid search, config, taxonomy, skills. area: ui-sdk Wire protocol, runtime schemas, the server and client SDK surfaces. area: ui-server Hono app factory: turn coordinator, auth, session catalog, routes, cron. contract Touches the integration contract: CONTRACT: commit, contract doc updated in the same commit. labels Nov 15, 2026
…eranking

# Conflicts:
#	docs/decisions/governed-writable-workers.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core packages/core: the brain CLI, indexer, hybrid search, config, taxonomy, skills. area: ui-sdk Wire protocol, runtime schemas, the server and client SDK surfaces. area: ui-server Hono app factory: turn coordinator, auth, session catalog, routes, cron. contract Touches the integration contract: CONTRACT: commit, contract doc updated in the same commit. type: feat A new capability or a change in user-visible behaviour.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ui-sdk: relay Jev reranking for governed workers, so hosted search is not always unranked

1 participant