Skip to content

armgit / avibe

Public Imported from GitHub
0

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.

fix(model-hub): forward the caller's User-Agent to the engine (#2245)

Call White2bf3576d1681
diff --git a/core/handlers/model_hub/request.py b/core/handlers/model_hub/request.py
index ff9816ecb..fc36f26c4 100644
--- a/core/handlers/model_hub/request.py
+++ b/core/handlers/model_hub/request.py
@@ -2,7 +2,22 @@
from __future__ import annotations
-from typing import Any, Mapping
+from typing import Any, Final, Mapping
+
+# Caller headers that cross from an agent CLI's request to the local engine.
+# Protocol capability headers select request features. ``user-agent`` keeps
+# the calling client's identity: the engine forwards it upstream, and some
+# Anthropic-compatible gateways rewrite the system prompt of any request that
+# does not identify as Claude Code. Without it the upstream sees Avibe's HTTP
+# library instead of the agent. Credentials and all other headers stay local.
+FORWARDED_CALLER_HEADERS: Final = frozenset(
+ {
+ "anthropic-beta",
+ "anthropic-version",
+ "openai-beta",
+ "user-agent",
+ }
+)
class ModelHubRequest(dict[str, Any]):
diff --git a/core/handlers/model_hub/turn_gateway.py b/core/handlers/model_hub/turn_gateway.py
index f7bf73771..4e8a936d3 100644
--- a/core/handlers/model_hub/turn_gateway.py
+++ b/core/handlers/model_hub/turn_gateway.py
@@ -41,7 +41,7 @@ from .provenance import (
project_turn_outcome_copy,
render_turn_outcome_copy,
)
-from .request import ModelHubRequest
+from .request import FORWARDED_CALLER_HEADERS, ModelHubRequest
from .retry import RECOVERY_EXHAUSTED_CODE, RECOVERY_EXHAUSTED_MESSAGE
from .resolver import parse_model_hub_timestamp
from .stream_wire import (
@@ -113,14 +113,6 @@ def _rewind_and_measure(payload: BinaryIO) -> int:
return size
-_PROTOCOL_HEADERS: Final = frozenset(
- {
- "anthropic-beta",
- "anthropic-version",
- "openai-beta",
- }
-)
-
_CODEX_TURN_METADATA: Final = "x-codex-turn-metadata"
_CODEX_ROUTE_FIELDS: Final = ("avibe_route_id", "avibe_turn_id")
@@ -929,8 +921,8 @@ class ModelHubTurnGateway:
protocol = _REQUEST_PROTOCOLS[endpoint]
try:
- protocol_headers = {
- name.lower(): value for name, value in request.headers.items() if name.lower() in _PROTOCOL_HEADERS
+ caller_headers = {
+ name.lower(): value for name, value in request.headers.items() if name.lower() in FORWARDED_CALLER_HEADERS
}
if backend == "opencode" and protocol == "openai_chat":
translation = translate_opencode_tool_names(payload)
@@ -943,7 +935,7 @@ class ModelHubTurnGateway:
request=ModelHubRequest(
payload,
protocol=protocol,
- headers=protocol_headers,
+ headers=caller_headers,
),
stream=stream,
supply_channel="hub",
diff --git a/docs/plans/backend-model-catalogs.md b/docs/plans/backend-model-catalogs.md
index 2b4228c54..d2a1b13cc 100644
--- a/docs/plans/backend-model-catalogs.md
+++ b/docs/plans/backend-model-catalogs.md
@@ -549,8 +549,9 @@ display names, no protocol chatter.
`disable-claude-cloak-mode: true`, credential `cloak.mode: never`, and
`rebuild-mid-system-message: false` (S3), so a same-protocol Anthropic request body is
forwarded unchanged apart from the mandatory `model` rewrite to the stored hop's model id
- (user-configured substitutions stay authoritative, `model-hub.md` §4.5); transport headers
- and upstream auth are the engine's own. Responses bodies toward a `codex-api-key` upstream
+ (user-configured substitutions stay authoritative, `model-hub.md` §4.5). The caller's
+ `User-Agent` reaches the upstream unchanged; other transport headers and upstream auth are
+ the engine's own. Responses bodies toward a `codex-api-key` upstream
are rewritten by the engine (S3): the implementation verifies each real `openai_responses`
Source still serves such requests and files the engine follow-up. A Hub-held Claude
subscription (OAuth credential) keeps the engine's cloaking: the vendor accepts those tokens
diff --git a/docs/plans/model-hub-contracts/opencode-overlay.md b/docs/plans/model-hub-contracts/opencode-overlay.md
index 634f537cd..f82ebbcbc 100644
--- a/docs/plans/model-hub-contracts/opencode-overlay.md
+++ b/docs/plans/model-hub-contracts/opencode-overlay.md
@@ -80,9 +80,10 @@ contract (one OpenAI-compatible provider, `vendor/model` ids); nothing of that s
body-level (S3): with the engine flags
`disable-claude-cloak-mode: true`, credential `cloak.mode: never`, and
`rebuild-mid-system-message: false`, an Anthropic-frontend body reaches an `anthropic`
- upstream byte-identical; the engine's transport headers remain its own (a Claude-CLI
- fingerprint: `User-Agent`, `Anthropic-Beta`, `X-Stainless-*`, session id) and upstream auth
- is the engine's Bearer form. A Responses-frontend body reaching a `codex-api-key` upstream
+ upstream byte-identical. The Gateway forwards only the caller's `User-Agent` and protocol
+ capability headers (`FORWARDED_CALLER_HEADERS`); the engine passes that `User-Agent` upstream
+ unchanged (measured on CLIProxyAPI v7.3.16, which otherwise sends `CLIProxyAPI/<version>`),
+ so an upstream sees the calling agent's identity. Upstream auth is the engine's Bearer form. A Responses-frontend body reaching a `codex-api-key` upstream
is rewritten by the engine's Codex executor regardless of flags (`max_output_tokens`
dropped; `parallel_tool_calls: true`, `instructions: ""`, and an `image_generation` tool
added) — a known engine limitation the implementation verifies against each real
diff --git a/tests/scenarios/model_hub/test_model_hub_live_resolution_scenarios.py b/tests/scenarios/model_hub/test_model_hub_live_resolution_scenarios.py
index e1d78ee26..d0b8a10de 100644
--- a/tests/scenarios/model_hub/test_model_hub_live_resolution_scenarios.py
+++ b/tests/scenarios/model_hub/test_model_hub_live_resolution_scenarios.py
@@ -896,7 +896,7 @@ def test_turn_gateway_tokens_are_bound_to_backend_origin(tmp_path: Path) -> None
asyncio.run(exercise())
-def test_turn_gateway_preserves_only_protocol_capability_headers(tmp_path: Path) -> None:
+def test_turn_gateway_preserves_only_forwarded_caller_headers(tmp_path: Path) -> None:
async def exercise() -> None:
adapter = AdapterBoundaryFake([AdapterResult(RawOutcomeKind.SUCCESS, status=200, body=b'{"ok":true}')])
store = MemoryStore(_config(_source("src_primary1")))
@@ -916,6 +916,8 @@ def test_turn_gateway_preserves_only_protocol_capability_headers(tmp_path: Path)
"x-api-key": token,
"Authorization": "Bearer upstream-must-not-cross",
"anthropic-beta": "interleaved-thinking",
+ "User-Agent": "claude-cli/2.1.280 (external, sdk-cli)",
+ "x-app": "cli",
},
json={"model": _requested_model("claude"), "messages": []},
) as response:
@@ -923,6 +925,7 @@ def test_turn_gateway_preserves_only_protocol_capability_headers(tmp_path: Path)
request = adapter.requests[0]
assert getattr(request, "headers") == {
"anthropic-beta": "interleaved-thinking",
+ "user-agent": "claude-cli/2.1.280 (external, sdk-cli)",
}
finally:
await gateway.close()
diff --git a/tests/test_model_hub_request_body_budget.py b/tests/test_model_hub_request_body_budget.py
index a8dc11af1..f7466b1d2 100644
--- a/tests/test_model_hub_request_body_budget.py
+++ b/tests/test_model_hub_request_body_budget.py
@@ -144,6 +144,7 @@ async def _post(ingress, body: bytes, framing: str, *, token: str | None = None)
"Content-Type": "application/json; charset=utf-8",
"anthropic-version": "2023-06-01",
"openai-beta": "fixture",
+ "User-Agent": "fixture-agent/1.0",
},
) as response:
headers = response.request_info.headers
@@ -212,7 +213,11 @@ async def test_large_multi_image_request_is_forwarded_exactly(ingress, framing):
assert forwarded == payload
assert _encode(forwarded) == body
assert forwarded.protocol == ingress.protocol
- assert forwarded.headers == {"anthropic-version": "2023-06-01", "openai-beta": "fixture"}
+ assert forwarded.headers == {
+ "anthropic-version": "2023-06-01",
+ "openai-beta": "fixture",
+ "user-agent": "fixture-agent/1.0",
+ }
record = _settle(ingress)
if ingress.backend == "opencode":
assert record is None
diff --git a/tests/test_model_hub_runtime.py b/tests/test_model_hub_runtime.py
index 2dbf7ec77..d41c3d97a 100644
--- a/tests/test_model_hub_runtime.py
+++ b/tests/test_model_hub_runtime.py
@@ -4156,6 +4156,65 @@ def test_adapter_uses_origin_protocol_for_engine_translation(
asyncio.run(run())
+def test_engine_client_sends_the_caller_user_agent_instead_of_its_own() -> None:
+ """Upstream gateways key Claude Code system-prompt handling on User-Agent."""
+
+ from aiohttp import web
+
+ async def run() -> dict[str, str]:
+ received: dict[str, str] = {}
+
+ async def respond(request: web.Request) -> web.Response:
+ received.update({name.lower(): value for name, value in request.headers.items()})
+ return web.json_response({"type": "message", "content": []})
+
+ 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:
+ source = SourceRecord(
+ source_id="src_fixture123",
+ vendor="custom",
+ protocol="anthropic",
+ base_url="https://api.example.test",
+ credential_ref="cred_fixture123",
+ allowed_origins=(),
+ model_ids=("model-a",),
+ prefix="source-fixture123",
+ )
+ handle = await EngineClient(
+ EngineConnection(
+ base_url=f"http://127.0.0.1:{runner.addresses[0][1]}",
+ management_key="management-key",
+ gateway_token="gateway-token",
+ )
+ ).invoke(
+ source,
+ "model-a",
+ {},
+ stream=False,
+ request_headers={
+ "User-Agent": "claude-cli/2.1.280 (external, sdk-cli)",
+ "anthropic-beta": "interleaved-thinking",
+ "Authorization": "Bearer caller-must-not-cross",
+ "x-api-key": "caller-must-not-cross",
+ },
+ )
+ await handle.outcome()
+ finally:
+ await runner.cleanup()
+ return received
+
+ received = asyncio.run(run())
+
+ assert received["user-agent"] == "claude-cli/2.1.280 (external, sdk-cli)"
+ assert received["anthropic-beta"] == "interleaved-thinking"
+ assert received["authorization"] == "Bearer gateway-token"
+ assert "x-api-key" not in received
+
+
def test_adapter_restores_source_projection_when_reload_fails(tmp_path: Path) -> None:
class Supervisor:
def __init__(self) -> None:
diff --git a/vibe/model_hub_runtime/client.py b/vibe/model_hub_runtime/client.py
index 58ca3dd75..1e33106ed 100644
--- a/vibe/model_hub_runtime/client.py
+++ b/vibe/model_hub_runtime/client.py
@@ -36,6 +36,7 @@ from core.handlers.model_hub.json_wire import (
JSONScope,
project_json_reader,
)
+from core.handlers.model_hub.request import FORWARDED_CALLER_HEADERS
from core.message_output import plain_untrusted_text
from core.handlers.model_hub.stream_wire import (
ErrorEnvelopePath,
@@ -59,7 +60,6 @@ _ERROR_OBSERVATION_BYTES = 256 * 1024
# its own budget so the client's connection budget never limits it.
_LOCAL_PROJECTION_TIMEOUT_SECONDS = ENGINE_TRANSPORT_TIMEOUT_SECONDS
_OFFICIAL_BASE_URLS = official_api_key_base_urls()
-_PROTOCOL_HEADERS = frozenset({"anthropic-beta", "anthropic-version", "openai-beta"})
logger = logging.getLogger(__name__)
_ProjectedJSON = TypeVar("_ProjectedJSON")
@@ -427,7 +427,7 @@ class EngineClient:
body["model"] = routed_model
body["stream"] = stream
headers = {
- key.lower(): value for key, value in (request_headers or {}).items() if key.lower() in _PROTOCOL_HEADERS
+ key.lower(): value for key, value in (request_headers or {}).items() if key.lower() in FORWARDED_CALLER_HEADERS
}
headers.update(
{