SAGE Architecture
This page explains how a request moves through SAGE and how SAGE decides where to send the next one. The design and the reasoning behind each choice are in the repository’s ARCHITECTURE.md.
The Middleware Chain
Every relay passes through a chain of middleware. Each one wraps the next and holds one concern, so a behaviour can be read, tested and switched off on its own. In the default order, outermost first:
| Stage | What it does |
|---|---|
| Request ID, client IP | Tags the request; resolves the real client address behind trusted_proxies |
| Parse, validate | Reads Target-Service-Id, detects the RPC type, loads the service’s QoS plugin, rejects what the service cannot serve |
| Timeout | Sets one deadline per request that covers every attempt inside it |
| Quorum | Optional: sends one request to several operators and answers by majority |
| Cache, batch, singleflight | Answers finalized data from cache; fans a JSON-RPC batch out per item; coalesces identical concurrent requests |
| Retry, hedge | Retries on another endpoint; races a second endpoint when the first is slow |
| Circuit breaker, method blocks | Skips broken domains, and hosts that cannot serve this method |
| Endpoint selection | Picks the endpoint by reputation and QoS (height, archival depth) |
| Send, grade | Signs and sends the relay; grades the response and attributes any error |
The order comes from gateway_config.middleware_chain, or a built-in default. Startup refuses an order that breaks a rule the design depends on: endpoint selection, for example, must run inside retry so that a retry rotates to a different endpoint.
Response Grading and Attribution
An HTTP 200 does not mean a good answer. SAGE grades each response in tiers: the status code, the response’s shape (empty body, HTML error page), the JSON-RPC envelope, and known error wordings. The verdict carries an attribution:
| Attribution | Example | Effect |
|---|---|---|
| Supplier | Timeout, HTML error page, a node that is out of sync | Counts against the endpoint; usually retried elsewhere |
| Blockchain | execution reverted, block not found, pruned state | The chain’s real answer: delivered to the client, no penalty |
| Client | Malformed request, method the service does not offer | Returned to the client, no penalty |
| Unknown | Nothing conclusive | Neither punished nor trusted |
Retry and circuit breaking are separate decisions. A retryable failure moves the request to another endpoint; it never by itself takes a whole domain out of the pool. A domain is broken out only on a failure rate, and an all-broken pool fails open rather than refusing every request.
Reputation
SAGE scores endpoints by backend URL, because many staked suppliers often share one backend. Each attempt contributes one signal; retries and hedge arms do not distort the score of the endpoint they moved away from, and a cancelled attempt is never held against anyone.
- Tiered selection. Endpoints are grouped by score, and the best tier is served first.
- Probation. A low-scoring endpoint keeps a small share of traffic, so it can recover without an operator’s help.
- Party signals. Some evidence describes an operator rather than one URL, and applies to every key that operator holds.
- Timeline. Each score change is recorded with its reason, readable through
GET /admin/timeline/{serviceID}, which answers “why is this endpoint getting no traffic”.
The per-signal weights and thresholds are configurable; see docs/scoring.md.
QoS: Height and History
QoS plugins know each chain’s notion of “up to date”:
- Height tracking. A per-service head comes from heights that more than one operator agrees on. Endpoints too far behind it (the service’s
sync_allowance) are not selected while better ones exist. - Archival routing (EVM). SAGE learns which hosts serve old state and old logs, and sends requests for history only to hosts that hold it.
- Health checks. Probes per service measure height, chain ID and the answers a host gives. With several replicas, one elected leader probes and every replica applies the results.
Sessions and Signing
SAGE reads sessions from the full node and caches them. It signs each relay for the session at the current height, as a delegated gateway for its configured applications, then verifies the supplier’s signature on the response. Session rollover is handled from the session’s own boundaries.
WebSocket
A client WebSocket is bridged to a supplier connection. SAGE tracks the client’s subscriptions so it can move the client to another supplier (at a session end, when a supplier stops answering, or when it is drained) and replay what was pending. Writes in flight during such a move are answered with an error saying they may or may not have been applied, rather than being sent twice.
Observation Pipeline
Deeper analysis runs off the request path: a sample of relays, and every health check, is published to an asynchronous queue and processed there, so it never adds latency to a client request.