Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(model-hub): let upstream response headers reach Avibe and stop them there (#2787)
Call Whitebce5aa6f33d0
diff --git a/tests/e2e/test_model_hub_runtime.py b/tests/e2e/test_model_hub_runtime.pyindex 6e8641ba0..84993ea16 100644--- a/tests/e2e/test_model_hub_runtime.py+++ b/tests/e2e/test_model_hub_runtime.py@@ -732,7 +732,7 @@ def test_a2_offline_engine_install_start_stop_and_hardened_config(assert config["disable-cooling"] is Trueassert config["usage-statistics-enabled"] is Falseassert config["force-model-prefix"] is True- assert config["passthrough-headers"] is False+ assert config["passthrough-headers"] is Truestopped = app.client.post("/api/models/runtime/stop", {})stopped_body = stopped.json()diff --git a/tests/test_model_hub_engine_response_headers.py b/tests/test_model_hub_engine_response_headers.pynew file mode 100644index 000000000..6360e1622--- /dev/null+++ b/tests/test_model_hub_engine_response_headers.py@@ -0,0 +1,110 @@+"""Upstream response headers the engine passes through stop at Avibe's gateway (#2780, #2666).++The engine renders ``passthrough-headers: true``, so it forwards an upstream's+response headers to Avibe: on success with hop-by-hop ones removed, on an error+with nothing filtered. Avibe reads only ``ENGINE_RESPONSE_HEADER_ALLOW_LIST`` and+builds every response it sends from its own headers. Each case drives the real+gateway and engine adapter against a loopback engine that answers with injected+upstream headers, and checks what reaches the caller.+"""++from __future__ import annotations++from contextlib import asynccontextmanager++import aiohttp+import pytest+from aiohttp import web++from core.handlers.model_hub.turn_gateway import ModelHubTurnGateway+from tests.test_model_hub_declared_tools import COMPLETED, INPUT, _wire+from tests.test_model_hub_l3 import _canonicalize_fixed_test_routes, _service, _source+from vibe.model_hub_runtime.client import ENGINE_RESPONSE_HEADER_ALLOW_LIST, _allowed_engine_header++# What an upstream may send and the engine forwards: none of it belongs to Avibe's callers.+INJECTED = {+ "Set-Cookie": "upstream-session=secret; Path=/; HttpOnly",+ "Request-Id": "req_upstream_1",+ "Anthropic-Organization-Id": "org-upstream",+ "Retry-After-Ms": "1500",+ "Anthropic-Ratelimit-Unified-5h-Reset": "1797000000",+ "X-Upstream-Injected": "1",+}+JSON_BODY = {+ "/v1/responses": b'{"id":"resp_1","object":"response","status":"completed","output":[],'+ b'"usage":{"input_tokens":2,"output_tokens":1}}',+ "/v1/messages": b'{"id":"msg_1","type":"message","role":"assistant","content":[{"type":"text","text":"ok"}],'+ b'"stop_reason":"end_turn","usage":{"input_tokens":2,"output_tokens":1}}',+}+RATE_LIMITED = {+ "/v1/responses": b'{"error":{"type":"rate_limit_exceeded","code":"rate_limit_exceeded","message":"slow down"}}',+ "/v1/messages": b'{"type":"error","error":{"type":"rate_limit_error","message":"slow down"}}',+}+++@asynccontextmanager+async def _engine(status: int, stream: bool):+ async def respond(request: web.Request) -> web.Response:+ await request.read()+ if status != 200:+ return web.Response(+ status=status, body=RATE_LIMITED[request.path], content_type="application/json",+ headers={**INJECTED, "Retry-After": "120"},+ )+ if stream:+ return web.Response(body=COMPLETED[request.path], content_type="text/event-stream", headers=INJECTED)+ return web.Response(body=JSON_BODY[request.path], content_type="application/json", headers=INJECTED)++ app = web.Application()+ app.router.add_post("/{path:.*}", respond)+ runner = web.AppRunner(app)+ await runner.setup()+ await web.TCPSite(runner, "127.0.0.1", 0).start()+ try:+ yield f"http://127.0.0.1:{runner.addresses[0][1]}"+ finally:+ await runner.cleanup()+++def test_only_the_retry_advice_avibe_uses_is_read_from_an_engine_response():+ assert ENGINE_RESPONSE_HEADER_ALLOW_LIST == {"retry-after"}+ headers = {**INJECTED, "Retry-After": "120"}+ assert _allowed_engine_header(headers, "Retry-After") == "120"+ assert {name: _allowed_engine_header(headers, name) for name in INJECTED} == dict.fromkeys(INJECTED)++[email protected](("status", "stream"), [(200, True), (200, False), (429, True), (429, False)],+ ids=["stream", "buffered", "stream-error", "buffered-error"])[email protected](("backend", "path"), [("claude", "messages"), ("codex", "responses")])+async def test_no_upstream_header_the_engine_forwards_reaches_the_caller(tmp_path, backend, path, status, stream):+ configured = _source("src_headers01", "Responses key", model_id="shared-model")+ if backend == "claude":+ configured.vendor, configured.protocol = "anthropic", "anthropic"+ async with _engine(status, stream) as root:+ service = _service(tmp_path, sources=[configured])+ _canonicalize_fixed_test_routes(service)+ _wire(service, tmp_path, root, {})+ gateway = ModelHubTurnGateway(service)+ try:+ base_url, token = await gateway.endpoint(+ backend, process_scope="/repo", turn_id="turn_headers",+ requested_model_id=service.store.requested_models[backend],+ resolved_model_id="shared-model", source_id=configured.id,+ )+ body = {"model": "shared-model", "stream": stream, "max_tokens": 16}+ body |= {"messages": [{"role": "user", "content": "hi"}]} if path == "messages" else {"input": INPUT}+ async with aiohttp.ClientSession() as downstream:+ async with downstream.post(+ f"{base_url}/v1/{path}", json=body, headers={"Authorization": f"Bearer {token}"},+ ) as response:+ await response.read()+ received = {name.lower(): value for name, value in response.headers.items()}+ served = response.status+ finally:+ await gateway.close()+ assert (served == 200) == (status == 200), (served, received)+ leaked = {name for name in received if name in {key.lower() for key in INJECTED}}+ assert leaked == set(), received+ # Retry advice reaches a caller only as the gateway's own whole-second header.+ assert received.get("retry-after", "120").isdigit(), receiveddiff --git a/vibe/model_hub_runtime/client.py b/vibe/model_hub_runtime/client.pyindex 8864e6cf9..2fa1c8e19 100644--- a/vibe/model_hub_runtime/client.py+++ b/vibe/model_hub_runtime/client.py@@ -99,6 +99,19 @@ _OFFICIAL_BASE_URLS = official_api_key_base_urls()logger = logging.getLogger(__name__)_ProjectedJSON = TypeVar("_ProjectedJSON")+# The engine forwards an upstream's response headers (``passthrough-headers``),+# minus hop-by-hop ones, and on errors it filters nothing. Avibe reads only these+# and builds every gateway response from its own headers, so no other upstream+# header (``set-cookie``, ``request-id``, an organization id) reaches Vibey, the+# Web UI or a gateway client. A header joins this list with the code that uses it.+ENGINE_RESPONSE_HEADER_ALLOW_LIST: frozenset[str] = frozenset({"retry-after"})+++def _allowed_engine_header(headers: Mapping[str, str], name: str) -> str | None:+ """An engine response header Avibe reads; one off the allow-list reads as absent."""++ return headers.get(name) if name.lower() in ENGINE_RESPONSE_HEADER_ALLOW_LIST else None+def upstream_api_url(root: str, path: str) -> str:"""Resolve a standard v1 endpoint from either an origin or an API root."""@@ -587,7 +600,7 @@ class EngineClient:)if response.status >= 300:response_received_at = datetime.now(timezone.utc)- retry_after = response.headers.get("Retry-After")+ retry_after = _allowed_engine_header(response.headers, "Retry-After")if retry_after is not None and (len(retry_after) > 128 or not retry_after.isascii()):retry_after = Noneerror_body = _StreamPrelude()diff --git a/vibe/model_hub_runtime/config.py b/vibe/model_hub_runtime/config.pyindex 01a19c221..018cfa25f 100644--- a/vibe/model_hub_runtime/config.py+++ b/vibe/model_hub_runtime/config.py@@ -92,7 +92,10 @@ def render_engine_config("redis-usage-queue-retention-seconds": 60,"proxy-url": "","force-model-prefix": True,- "passthrough-headers": False,+ # Upstream response headers reach Avibe, which is how an upstream's Retry-After on an+ # error arrives at all (#2666). They stop at Avibe: the gateway builds every response+ # it sends from its own headers, and reads only ENGINE_RESPONSE_HEADER_ALLOW_LIST.+ "passthrough-headers": True,"request-retry": 0,"max-retry-credentials": 1,"max-retry-interval": 0,