feat(api): validate every response against its Zod schema, and refuse a drift - #10435
Merged
Conversation
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
metagraphed-registry-sync-api | 859f040 | Aug 10 2026, 12:29 PM |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
metagraphed-wss-lb | 859f040 | Aug 10 2026, 12:29 PM |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
metagraphed-data-api | 859f040 | Aug 10 2026, 12:29 PM |
|
Superagent didn't find any vulnerabilities or security issues in this PR. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10435 +/- ##
=======================================
Coverage 95.77% 95.78%
=======================================
Files 670 670
Lines 41987 42019 +32
Branches 15533 15540 +7
=======================================
+ Hits 40212 40246 +34
+ Misses 599 598 -1
+ Partials 1176 1175 -1
🚀 New features to boost your workflow:
|
JSONbored
force-pushed
the
feat/api-response-validation-only
branch
2 times, most recently
from
August 10, 2026 12:17
246fafa to
47e1b38
Compare
… a drift The response tripwire shipped in #7860 as a five-route pilot: SCHEMA_LOADERS hand-listed subnets, subnet-detail, health, economics and subnet-stake-quote, with a comment saying to add an entry as later batches converted more routes. The batches landed and the entries did not, so 156 of 161 routes served unchecked -- and METAGRAPH_VALIDATE_RESPONSES was "false" in wrangler.jsonc, so the five did too. It is DERIVED now and covers everything by construction: a route names its artifact, schemaRefForArtifactPath maps that to the component id, and COMPONENT_SCHEMAS_BY_ID -- populated by register() itself, 322 entries -- hands back the Zod node. There is no list to fall behind. It THROWS, and is AWAITED in the response path rather than scheduled on waitUntil, where a throw would only be an unhandled rejection and the drifted body would ship anyway. A drift is a 500 the caller can act on. This is also what the GraphQL cutover was missing. The published schema takes its nullability from these components and graphql-js enforces non-null at execution, so "the Zod says non-null" was a claim nothing checked against the producer. A sweep of the served surface found exactly one disagreement -- endpoint_pools.source, null in production against a required enum, fixed here at the Zod -- and this is what keeps it at one.
JSONbored
force-pushed
the
feat/api-response-validation-only
branch
from
August 10, 2026 12:28
47e1b38 to
859f040
Compare
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.
Summary
Every REST response is now parsed against the Zod schema that defines it, on every request, and a response that does not match is not served.
Three things were wrong before:
src/response-validation-tripwire.tsshipped in types-epic B: generate OpenAPI 3.1 from the Zod layer; invert the schemas/ flow for covered routes #7860 with a hand-writtenSCHEMA_LOADERSlistingsubnets,subnet-detail,health,economicsandsubnet-stake-quote, and a comment saying "add an entry here as later types-epic B batches convert more routes." The batches landed. The entries did not.wrangler.jsoncset"METAGRAPH_VALIDATE_RESPONSES": "false", so the five did not run either.console.warnon mismatch, then served the body anyway.Derived, so there is no list to fall behind
A route names its artifact →
schemaRefForArtifactPathmaps that to the component id the OpenAPI document publishes →COMPONENT_SCHEMAS_BY_IDhands back the Zod node. That map is populated byregister()itself — 322 components — so a route converted tomorrow is covered the moment its component is registered, which is the same moment it appears inopenapi.json.schemaRefForArtifactPathis exported fromcontracts.ts; the map is new and one line insideregister.It throws, and it is awaited
waitUntilwas the wrong place the moment it started throwing: the response is already built by then, so a throw is an unhandled rejection and the drifted body ships regardless. It is awaited in the response path now, and both call sites turnResponseSchemaDriftErrorinto a 500:The tripwire's own failures — a broken import, an artifact with no contract entry — are still warned and swallowed. A drift is the route's fault and must fail the request; the tripwire falling over is not, and must not.
Cost, stated plainly: one parse per response while the flag is on, and a schema bug now fails a route instead of quietly shipping. That is the trade, and it is the reason the flag exists.
The one real disagreement it found
Sweeping the served surface for fields where a component claims non-null turned up exactly one where the producer disagrees:
endpoint_pools.sourceisnullin production against a required enum. Fixed at the Zod, with the evidence recorded inline —endpoint_pools(limit: 3) { source }answers"source": nulltoday.That matters beyond this route. The published GraphQL schema takes its nullability from these components and graphql-js enforces non-null at execution, so a single null where a component says non-null nulls the whole surrounding object and attaches an error — which is what
SelfHealthLane.detaildid on everyself_healthrequest (#10215). Until this ran, "the Zod says non-null" was a claim about the schema with nothing checking it against the producer. This is what keeps that count at one.Tests
Twelve, covering both halves and both call sites:
validate:openapiowns that invariantgaps— never one of the five pilots — validates cleanly against real fixture dataresponse_schema_drift, asserted separately forhandleApiRequest's generic path and forsubnet-stake-quote, which is matched and returned early and so has its own call siteregisterModuleStateResetis wired for the two module-level caches, so the warn-once does not leak across test files underisolate: false.Validation
npm run typecheck,npm run lint,npm run format:check— cleanvalidate:contract-drift,validate:module-state-resets— passnpx vitest run— 800 files, 18,483 passed, 11 skipped, 0 failed, with the flag ON for the whole suite8d3f84657Refs #10214.