Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/sie_gateway/docs/architecture-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Rules enforced on the inference path:
- On queue result timeouts the gateway returns `504` with `X-SIE-Error-Code: GATEWAY_TIMEOUT` and `Retry-After: 5`. This means the gateway accepted and published the work item, but no worker result reached the gateway before `SIE_GATEWAY_REQUEST_TIMEOUT`; it must not be collapsed into worker-emitted `MODEL_LOADING`, which remains a separate retryable `503 MODEL_LOADING` signal.
- When workers report failure on every item of a batch with the **same retryable error code** in `WorkResult.error_code`, the gateway translates that into a `503` with the SDK-expected envelope (`error.code`, `Retry-After`, `X-SIE-Error-Code`) so the SDK auto-retries. Currently recognised codes: `RESOURCE_EXHAUSTED`, `MODEL_LOADING`, `LORA_LOADING`, and `QUEUE_FULL` (a worker that cannot serve the item now, such as a remote profile whose upstream is busy, unreachable, rate capped or behind an open circuit breaker; a remote profile whose upstream model is not ready answers `MODEL_LOADING`). A remote worker answers these upstream refusals at once with the upstream's wait rather than redelivering the item. Generation workers attach the operator's validated `oom_recovery.retry_after_s` value to a `RESOURCE_EXHAUSTED` terminal chunk, and a worker may set `WorkResult.retry_after_s` on a failed item; for `RESOURCE_EXHAUSTED`, `QUEUE_FULL` and `MODEL_LOADING` failures, the gateway uses the longest worker hint that qualifies as an integer in `1..=60` seconds, ignoring other hints. If no hint qualifies, or the code is `LORA_LOADING` even when a worker provides a hint, it uses the current five-second default compiled into the gateway. **Mixed batches** (different codes per item) keep going through the compatibility `500 all_items_failed` path with per-item `code` fields exposed in `details[]`, so callers can see exactly which items hit which failure mode. Workers reporting `RESOURCE_EXHAUSTED` are **not** marked unhealthy — losing an allocation race is not a worker-health signal.
- When **every** failed item carries **`MODEL_LOAD_FAILED`**, the gateway returns **`502 Bad Gateway`** with the SDK-style **`error`** object (`code`, `message`, `error_class`, `attempts`, `permanent`) — matching the terminal model-load contract consumed by SDK retry logic (no `Retry-After`; the SDK must not burn the `MODEL_LOADING` retry budget). The gateway synthesises conservative `attempts` / `permanent` fields on this queue path when the worker payload does not carry registry-shaped failure metadata.
- Every successful response and every `5xx` of an inference route carries `X-SIE-Served-By` (`local` or `remote`) once routing has resolved the dispatched model, and `X-SIE-Upstream` with the upstream's name when that model's default profile uses a remote adapter (`handlers::serving_disclosure`). The value is read from the dispatched route's configuration, as `sie_server.api.helpers.serving_disclosure_headers` reads it on the single server, so a `model:remote` variant or a remote-backed model reports `remote` and the model's local profile reports `local`. Client errors carry neither header. The compatibility routes (`/v1/embeddings`, `/v1/rerank`, `/v2/rerank`, `/v1/audio/transcriptions`, `/v1/messages`) forward both through `is_openai_compat_forwarded_header`.
- Every successful response and every `5xx` of an inference route carries `X-SIE-Served-By` (`local` or `remote`) once routing has resolved the dispatched model, and `X-SIE-Upstream` with the upstream's name when that model's default profile uses a remote adapter (`handlers::serving_disclosure`). The value is read from the dispatched route's configuration, as `sie_server.api.helpers.serving_disclosure_headers` reads it on the single server, so a `model:remote` variant or a remote-backed model reports `remote` and the model's local profile reports `local`. Client errors carry neither header. The compatibility routes (`/v1/embeddings`, `/v1/rerank`, `/v2/rerank`, `/v1/audio/transcriptions`, `/v1/audio/speech`, `/v1/messages`) forward both through `is_openai_compat_forwarded_header`.

Encode / extract tuning fields (`output_types`, `instruction`, `options`, `labels`, `output_schema`, `is_query`) are read **only** from the nested JSON **`params`** object (and the msgpack analogue: a top-level **`params`** map). The score path continues to read `query` / `instruction` / `options` at the top level of the request body, matching `sie_server`.

Expand All @@ -78,7 +78,7 @@ before the gateway publishes the worker item, preserving the worker-side typed

Gateway-generated **JSON error** bodies (validation, routing, auth, config read, pools) use a FastAPI-like envelope **`{"detail":{"code":"<STABLE_CODE>","message":"…", …}}`**. Extra diagnostic keys (e.g. `compatible_bundles`, `gpu`, `model`) live **inside** `detail` when present. This is intentionally **not** the same shape as SDK retry contracts, which remain **`{"error":{"code","message"}}`** for **503** retryable worker signals, SIE-native provisioning, and **`502`** **`MODEL_LOAD_FAILED`**. OpenAI-compatible provisioning uses the OpenAI error envelope. **200** success payloads keep their existing top-level shapes.

On the OpenAI-compatible routes (`/v1/chat/completions`, `/v1/completions`, `/v1/responses`, `/v1/embeddings`, `/v1/models`, `/v1/audio/transcriptions`, `/v1/moderations`, and every path below them) every error body uses the OpenAI envelope **`{"error":{"message","type","param","code"}}`**. The handlers build it themselves; refusals made outside them, such as the auth middleware's `401` and `500 GATEWAY_AUTH_MISCONFIGURED`, are re-rendered by the outer middleware `http_error::openai_error_envelope`, the twin of `anthropic_error_envelope` on `/v1/messages`. It turns a 4xx or 5xx JSON `detail` body into the OpenAI envelope: `message` is `detail.message` (or the `detail` string), `code` is `detail.code` verbatim (a string `detail` gets its status's SIE code: `UNAUTHORIZED`, `FORBIDDEN`, `NOT_FOUND`, `PAYLOAD_TOO_LARGE`, `RATE_LIMIT`, `INVALID_REQUEST` or `INTERNAL_SERVER_ERROR`), `type` follows the status (`authentication_error` for 401, `permission_error` for 403, `rate_limit_error` for 429, `invalid_request_error` for other 4xx, `server_error` for 5xx), `param` is `detail.param` when it is a string and otherwise `null`, and every other `detail` field stays in `error`. The status, the headers (`WWW-Authenticate`, `Retry-After`, `X-SIE-Error-Code`, `X-SIE-RateLimit-*`) and the response extensions are kept. Bodies already in the OpenAI envelope, other bodies such as a validation list, answers without a body such as an unrouted `405`, non-JSON, encoded or chunked bodies, bodies of unknown length or over 1 MiB, and successes pass through; a body that fails while it is read still fails. The SIE-native routes keep `detail`. A composition that adds admission layers around the gateway's routes applies both envelope layers outermost so its refusals take the same shapes; the two never apply to the same path, and applying one twice changes nothing.
On the OpenAI-compatible routes (`/v1/chat/completions`, `/v1/completions`, `/v1/responses`, `/v1/embeddings`, `/v1/models`, `/v1/audio/transcriptions`, `/v1/audio/speech`, `/v1/moderations`, and every path below them) every error body uses the OpenAI envelope **`{"error":{"message","type","param","code"}}`**. The handlers build it themselves; refusals made outside them, such as the auth middleware's `401` and `500 GATEWAY_AUTH_MISCONFIGURED`, are re-rendered by the outer middleware `http_error::openai_error_envelope`, the twin of `anthropic_error_envelope` on `/v1/messages`. It turns a 4xx or 5xx JSON `detail` body into the OpenAI envelope: `message` is `detail.message` (or the `detail` string), `code` is `detail.code` verbatim (a string `detail` gets its status's SIE code: `UNAUTHORIZED`, `FORBIDDEN`, `NOT_FOUND`, `PAYLOAD_TOO_LARGE`, `RATE_LIMIT`, `INVALID_REQUEST` or `INTERNAL_SERVER_ERROR`), `type` follows the status (`authentication_error` for 401, `permission_error` for 403, `rate_limit_error` for 429, `invalid_request_error` for other 4xx, `server_error` for 5xx), `param` is `detail.param` when it is a string and otherwise `null`, and every other `detail` field stays in `error`. The status, the headers (`WWW-Authenticate`, `Retry-After`, `X-SIE-Error-Code`, `X-SIE-RateLimit-*`) and the response extensions are kept. Bodies already in the OpenAI envelope, other bodies such as a validation list, answers without a body such as an unrouted `405`, non-JSON, encoded or chunked bodies, bodies of unknown length or over 1 MiB, and successes pass through; a body that fails while it is read still fails. The SIE-native routes keep `detail`. A composition that adds admission layers around the gateway's routes applies both envelope layers outermost so its refusals take the same shapes; the two never apply to the same path, and applying one twice changes nothing.

## 3. Configuration Write Path

Expand Down Expand Up @@ -1119,7 +1119,7 @@ lane. It is `false` by default and is a non-publishing hint.
budget, so the SDKs still retry after it.
- On the OpenAI-compatible routes (`/v1/chat/completions`,
`/v1/completions`, `/v1/responses`, `/v1/embeddings`,
`/v1/audio/transcriptions`, `/v1/messages`) and the rerank routes (`/v1/rerank`,
`/v1/audio/transcriptions`, `/v1/audio/speech`, `/v1/messages`) and the rerank routes (`/v1/rerank`,
`/v2/rerank`) the ceiling is 60 seconds. The OpenAI Python client
honors `Retry-After` only when it is greater than 0 and at most 60
seconds. Above that it ignores the header and retries on its own
Expand Down
Loading
Loading