Skip to content

feat(bff): one path to erserver — upstreamFetch and the drift gate (telemetry P18) - #434

Merged
athvin merged 1 commit into
mainfrom
bm/telemetry-p18-upstream
Nov 3, 2026
Merged

athvin merged 1 commit into
mainfrom
bm/telemetry-p18-upstream

Conversation

@athvin

@athvin athvin commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Five call sites each built the erserver URL and header set by hand — rawFetch (the tenant proxy), operatorFetch (the console), relayEmail, the imports multipart route and the public widget route — plus a dead sixth, lib/api/erserver.ts, which this deletes. A path with no callers is still a path the next person can revive.

They had already drifted: four set cache: "no-store" and one did not, which for per-identity control-plane responses is a correctness bug rather than a performance one.

That drift is why this is worth shipping on its own. The telemetry work needs traceparent on every upstream request, and with five paths "every" is something you hope for rather than something you can gate.

What deliberately stays with the caller

authorization is a parameter rather than resolved inside upstreamFetch, because the three credential stories are genuinely different — a stored org key selected by role, the operator token, and the widget's signed token which is the credential — and collapsing them would be the wrong kind of sharing. Error mapping stays with callers for the same reason: relayEmail returns false, the widget returns a 404 that leaks nothing, the proxy raises a typed BffFailure. So upstreamFetch does not catch; every caller already has the try/catch that knows what its own failure means.

The multipart route passes body rather than json, and upstreamFetch sets no content-type in that case. That omission is load-bearing: fetch generates the multipart boundary itself, and setting the header by hand produces a body the upstream cannot parse. It is why multipart has no reason to escape again.

The gate

scripts/check-bff-upstream.mjs, wired into pnpm check so both CI and make frontend run it. The invariant is expressed as only upstream.ts may name ERSERVER_BASE_URL — the one thing a new path cannot avoid doing, which makes it a far better tripwire than looking for fetch(, of which the app has many legitimate uses.

It copies all four hard-won properties of check-bff-authz.mjs:

  1. walk all of src/, not a hand-listed subtree
  2. an allowlist keyed by path with a stated reason
  3. every expected caller must contain the literal upstreamFetch(
  4. print the examined count, so a moved directory reads as a suspicious zero rather than success

Deliberately not semgrep — .semgrep.yml's own header records that this class of invariant was tried there and semgrep rejected the pattern as invalid TypeScript while still reporting the rule as run with zero findings.

Observed failing before being trusted, both ways:

# a sixth file naming ERSERVER_BASE_URL
upstream gate FAILED — 1 file(s) name ERSERVER_BASE_URL outside src/lib/bff/upstream.ts
# an expected caller that stops calling it
upstream gate FAILED — src/lib/bff/relay.ts — does not call upstreamFetch(

Not done here, on purpose

No traceparent and no x-er-dataset yet.

Trace context must arrive through AsyncLocalStorage in P19, not from next/headers here. headers() is a Request-time API in Next 16 and opts its caller into dynamic rendering, and forward is reachable from a server component — so reading it inside the shared fetch helper would change rendering behaviour for pages that merely fetch. Verified against node_modules/next/dist/docs/01-app/03-api-reference/04-functions/headers.md rather than assumed, per AGENTS.md.

And an unread x-er-dataset would be a third dead primitive of exactly the kind this change removes, so it lands when erserver reads it.

Verification

pnpm check passes — eslint, prettier, tsc, the authz gate (94 route handlers), the new upstream gate (363 files, 5 callers), and 722 tests with the coverage floor.

🤖 Generated with Claude Code

…elemetry P18)

Five call sites each built the erserver URL and header set by hand: `rawFetch`
(the tenant proxy), `operatorFetch` (the console), `relayEmail`, the imports
multipart route and the public widget route — plus a dead sixth,
`lib/api/erserver.ts`, which this deletes. A path with no callers is still a
path the next person can revive.

They had already drifted: four set `cache: "no-store"` and one did not, which
is a correctness bug rather than a performance one for per-identity control
plane responses.

That drift is the reason this is worth shipping on its own. The telemetry work
needs `traceparent` on EVERY upstream request, and with five paths "every" is
something you hope for rather than something you can gate.

## What deliberately stays with the caller

`authorization` is a parameter rather than resolved inside `upstreamFetch`,
because the three credential stories are genuinely different — a stored org key
selected by role, the operator token, and the widget's signed token which IS
the credential — and collapsing them would be the wrong kind of sharing. Error
mapping stays with callers for the same reason: `relayEmail` returns `false`,
the widget returns a 404 that leaks nothing, and the proxy raises a typed
`BffFailure`. So `upstreamFetch` does not catch; every caller already has the
try/catch that knows what its own failure means.

The multipart route passes `body` rather than `json`, and `upstreamFetch` sets
no content-type for that case. The omission is load-bearing: `fetch` generates
the multipart boundary itself and setting the header by hand produces a body
the upstream cannot parse. That is why multipart has no reason to escape again.

## The gate

`scripts/check-bff-upstream.mjs`, wired into `pnpm check` so both CI and
`make frontend` run it. The invariant is expressed as **only upstream.ts may
name ERSERVER_BASE_URL** — the one thing a new path cannot avoid doing, which
makes it a far better tripwire than looking for `fetch(`.

It copies all four hard-won properties of check-bff-authz.mjs: walk all of
`src/`, an allowlist keyed by path WITH a stated reason, every expected caller
must contain the literal `upstreamFetch(`, and print the examined count so a
moved directory reads as a suspicious zero rather than as success. Deliberately
not semgrep — `.semgrep.yml`'s own header records that this class of invariant
was tried there and semgrep rejected the pattern as invalid TypeScript while
still reporting the rule as run with zero findings.

Observed failing before being trusted, both ways: a sixth file naming
ERSERVER_BASE_URL, and an expected caller that stops calling upstreamFetch.

## Not done here, on purpose

No `traceparent` or `x-er-dataset` yet. Trace context must arrive through
AsyncLocalStorage in P19, NOT from `next/headers` here: `headers()` is a
Request-time API in Next 16 and opts its caller into dynamic rendering, and
`forward` is reachable from a server component — so reading it inside the
shared fetch helper would change rendering behaviour for pages that merely
fetch. Verified against node_modules/next/dist/docs rather than assumed, per
AGENTS.md. And an unread `x-er-dataset` would be a third dead primitive of
exactly the kind this change removes.

`pnpm check` passes: 722 tests, both gates, coverage floor.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@athvin
athvin merged commit cdcf118 into main Nov 3, 2026
37 checks passed
@athvin
athvin deleted the bm/telemetry-p18-upstream branch November 3, 2026 03:42
athvin pushed a commit that referenced this pull request Nov 6, 2026
…r fires

A /skill-audit of `api-contract` found twelve findings, two of which would
mislead a session into doing the wrong thing rather than merely confusing it.

**The server-only client it named does not exist.** `src/lib/api/erserver.ts`
was deleted in #434, which consolidated five hand-built upstream paths into
`lib/bff/upstream.ts` so `traceparent` could be attached to every one of them.
The skill still routed a reader there from the pipeline diagram, the two-clients
table and the whole of eval 4 — and `upstream.ts`'s own header anticipates the
consequence: "a path with no callers is still a path the next person can
revive". Following the skill was a route back to the dead sixth path.

**`every_generated_entity_schema_is_registered` does not enforce its name.** It
asserts the *size* of `ApiDoc`'s hand-written registration list, so it is blind
to `entities/`: add an entity, never register its schema, and the size is
unchanged and the test is green. The skill called it "the one that catches the
quiet mistake: adding an entity and forgetting to register its schema", which is
precisely the mistake it cannot see. Stated plainly now, with the gap in the
gate table and a new eval scenario; the durable fix is a set difference against
`entities/`, left as follow-up because which entities belong on the control lane
is a design decision, not a doc edit.

Verified against source and corrected, each previously asserted as fact:

- `er-openapi-check` runs in the Rust `entities` job, not `lint` (two places),
  and only on pull requests — on a push to `main` it has no base and examines
  nothing.
- "the utoipa doc paths in `lib.rs`" named annotations that do not exist; there
  are no `utoipa::path` attributes in `er-server`. The route shape is the axum
  router's hand-written `.route(...)` calls.
- `mock_contract.rs` holds the mock to the router in **one** direction (the mock
  may serve no route the router lacks), not both.
- The regeneration command could not work from either directory: the cargo
  workspace root is `rust/`, so the redirect has to be relative to it.
- Nothing in `frontend/src` imports `schema.d.ts`, and `openapi-fetch` has no
  call sites. The gates prove the document and the types agree with the Rust
  structs; they prove nothing about frontend call sites, and the skill now says
  so instead of implying the opposite.

Routing, per the standard's both-ways rule: `entity-framework` never named
`api-contract`, so it won the trigger on "add a column and expose it in the API"
and answered only the entity half. #686 landed that direction while this was
open, so what remains here is the other one — `api-contract` gained the
`frontend-bff` tail that eval 4 always assumed.

Also deduplicated: the reference is canonical for the gate and breaking-change
tables, which is how two of the corrections above came to be wrong in two files
at once.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
athvin added a commit that referenced this pull request Nov 6, 2026
…r fires

A /skill-audit of `api-contract` found twelve findings, two of which would
mislead a session into doing the wrong thing rather than merely confusing it.

**The server-only client it named does not exist.** `src/lib/api/erserver.ts`
was deleted in #434, which consolidated five hand-built upstream paths into
`lib/bff/upstream.ts` so `traceparent` could be attached to every one of them.
The skill still routed a reader there from the pipeline diagram, the two-clients
table and the whole of eval 4 — and `upstream.ts`'s own header anticipates the
consequence: "a path with no callers is still a path the next person can
revive". Following the skill was a route back to the dead sixth path.

**`every_generated_entity_schema_is_registered` does not enforce its name.** It
asserts the *size* of `ApiDoc`'s hand-written registration list, so it is blind
to `entities/`: add an entity, never register its schema, and the size is
unchanged and the test is green. The skill called it "the one that catches the
quiet mistake: adding an entity and forgetting to register its schema", which is
precisely the mistake it cannot see. Stated plainly now, with the gap in the
gate table and a new eval scenario; the durable fix is a set difference against
`entities/`, left as follow-up because which entities belong on the control lane
is a design decision, not a doc edit.

Verified against source and corrected, each previously asserted as fact:

- `er-openapi-check` runs in the Rust `entities` job, not `lint` (two places),
  and only on pull requests — on a push to `main` it has no base and examines
  nothing.
- "the utoipa doc paths in `lib.rs`" named annotations that do not exist; there
  are no `utoipa::path` attributes in `er-server`. The route shape is the axum
  router's hand-written `.route(...)` calls.
- `mock_contract.rs` holds the mock to the router in **one** direction (the mock
  may serve no route the router lacks), not both.
- The regeneration command could not work from either directory: the cargo
  workspace root is `rust/`, so the redirect has to be relative to it.
- Nothing in `frontend/src` imports `schema.d.ts`, and `openapi-fetch` has no
  call sites. The gates prove the document and the types agree with the Rust
  structs; they prove nothing about frontend call sites, and the skill now says
  so instead of implying the opposite.

Routing, per the standard's both-ways rule: `entity-framework` never named
`api-contract`, so it won the trigger on "add a column and expose it in the API"
and answered only the entity half. #686 landed that direction while this was
open, so what remains here is the other one — `api-contract` gained the
`frontend-bff` tail that eval 4 always assumed.

Also deduplicated: the reference is canonical for the gate and breaking-change
tables, which is how two of the corrections above came to be wrong in two files
at once.

Co-authored-by: bmoore813 <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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.

2 participants