Repository navigation
feat(bff): one path to erserver — upstreamFetch and the drift gate (telemetry P18) - #434
Merged
Merged
Conversation
…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]>
This was referenced Nov 3, 2026
Merged
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
traceparenton 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
authorizationis a parameter rather than resolved insideupstreamFetch, 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:relayEmailreturnsfalse, the widget returns a 404 that leaks nothing, the proxy raises a typedBffFailure. SoupstreamFetchdoes not catch; every caller already has thetry/catchthat knows what its own failure means.The multipart route passes
bodyrather thanjson, andupstreamFetchsets no content-type in that case. That omission is load-bearing:fetchgenerates 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 intopnpm checkso both CI andmake frontendrun it. The invariant is expressed as onlyupstream.tsmay nameERSERVER_BASE_URL— the one thing a new path cannot avoid doing, which makes it a far better tripwire than looking forfetch(, of which the app has many legitimate uses.It copies all four hard-won properties of
check-bff-authz.mjs:src/, not a hand-listed subtreeupstreamFetch(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:
Not done here, on purpose
No
traceparentand nox-er-datasetyet.Trace context must arrive through
AsyncLocalStoragein P19, not fromnext/headershere.headers()is a Request-time API in Next 16 and opts its caller into dynamic rendering, andforwardis reachable from a server component — so reading it inside the shared fetch helper would change rendering behaviour for pages that merely fetch. Verified againstnode_modules/next/dist/docs/01-app/03-api-reference/04-functions/headers.mdrather than assumed, perAGENTS.md.And an unread
x-er-datasetwould be a third dead primitive of exactly the kind this change removes, so it lands when erserver reads it.Verification
pnpm checkpasses — 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