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): accept requests through 128 MiB (#1958)

Call Whitef932dbdda226
diff --git a/core/handlers/model_hub/turn_gateway.py b/core/handlers/model_hub/turn_gateway.py
index 2a66d4ccb..1e7b99271 100644
--- a/core/handlers/model_hub/turn_gateway.py
+++ b/core/handlers/model_hub/turn_gateway.py
@@ -15,7 +15,9 @@ from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import BinaryIO, Final, Optional
+import aiohttp
from aiohttp import web
+from packaging.version import Version
from config import paths
from core.run_settlement import SETTLED_BY_TERMINAL_RESULT
@@ -63,7 +65,7 @@ from .service import (
)
-_MAX_REQUEST_BYTES: Final = 16 * 1024 * 1024
+_MAX_REQUEST_BYTES: Final = 128 * 1024 * 1024
_BUFFERED_RESPONSE_MEMORY_BYTES: Final = 256 * 1024
_RESPONSE_CHUNK_BYTES: Final = 64 * 1024
_SUPPORTED_PATHS: Final = frozenset(
@@ -490,7 +492,12 @@ class ModelHubTurnGateway:
async with self._start_lock:
if self._runner is not None:
return
- app = web.Application(client_max_size=_MAX_REQUEST_BYTES)
+ # Before 3.14, aiohttp's reader rejects at >= instead of >.
+ # Compensate only there so our byte budget stays inclusive.
+ client_max_size = _MAX_REQUEST_BYTES
+ if Version(aiohttp.__version__).release < (3, 14):
+ client_max_size += 1
+ app = web.Application(client_max_size=client_max_size)
app.router.add_get("/{backend}/v1/models", self._handle_models)
app.router.add_post("/{backend}/v1/{endpoint:.*}", self._handle_request)
runner = web.AppRunner(
@@ -758,6 +765,15 @@ class ModelHubTurnGateway:
)
try:
payload = await request.json(loads=json.loads)
+ except web.HTTPRequestEntityTooLarge:
+ terminalizer.fail("invalid_parameter")
+ return self._terminal_error_response(
+ execution,
+ terminalizer,
+ status=413,
+ code="request_too_large",
+ turn_outcome=REQUEST_NONFALLBACK_TURN_OUTCOME,
+ )
except (json.JSONDecodeError, UnicodeDecodeError):
terminalizer.fail("invalid_parameter")
return self._terminal_error_response(
@@ -1225,7 +1241,11 @@ class ModelHubTurnGateway:
copy = project_turn_outcome_copy(turn_outcome) if turn_outcome is not None else None
message = render_turn_outcome_copy(turn_outcome, language) if turn_outcome is not None else None
key = copy.key if copy is not None else None
- if message is None and fallback_code is not None:
+ if fallback_code == "request_too_large":
+ # Refine the local admission error without changing its turn outcome.
+ key = "modelHub.errors.request_too_large"
+ message = i18n_t(key, language, limit_mib=f"{_MAX_REQUEST_BYTES / (1024 * 1024):g}")
+ elif message is None and fallback_code is not None:
fallback_key = f"modelHub.errors.{fallback_code}"
fallback_message = i18n_t(fallback_key, language)
if fallback_message == fallback_key:
diff --git a/tests/scenarios/INDEX.yaml b/tests/scenarios/INDEX.yaml
index 3c805ad79..85004ed4f 100644
--- a/tests/scenarios/INDEX.yaml
+++ b/tests/scenarios/INDEX.yaml
@@ -151,6 +151,7 @@ capabilities:
- tests/test_model_hub_injection.py
- tests/test_model_hub_runtime.py
- tests/test_model_hub_inference_wait.py
+ - tests/test_model_hub_request_body_budget.py
- tests/test_model_hub_l3.py
- tests/test_model_hub_usage.py
- tests/test_local_deps.py
diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yaml
index 3b31714d4..6da5693e2 100644
--- a/tests/scenarios/model_hub/catalog.yaml
+++ b/tests/scenarios/model_hub/catalog.yaml
@@ -9,6 +9,7 @@ capability:
- tests/test_model_hub_injection.py
- tests/test_model_hub_runtime.py
- tests/test_model_hub_inference_wait.py
+ - tests/test_model_hub_request_body_budget.py
- tests/test_model_hub_l3.py
- tests/test_model_hub_usage.py
- tests/test_local_deps.py
@@ -230,6 +231,24 @@ scenarios:
kind: compatibility
layer: e2e
test: tests/e2e/test_model_hub_routing.py::test_mh_routing_013_legacy_empty_inherits_matching_and_exact_passthrough_through_real_cpa
+ - id: MH-REQUEST-BUDGET-001
+ name: Large multi-image requests retain exact model input through authenticated HTTP ingress
+ status: covered
+ kind: contract
+ layer: unit
+ test: tests/test_model_hub_request_body_budget.py::test_large_multi_image_request_is_forwarded_exactly
+ - id: MH-REQUEST-BUDGET-002
+ name: The inclusive request byte budget returns a local structured error without upstream admission or health mutation
+ status: covered
+ kind: boundary
+ layer: unit
+ test: tests/test_model_hub_request_body_budget.py::test_request_budget_is_inclusive_and_local
+ - id: MH-REQUEST-BUDGET-003
+ name: Invalid gateway authentication cannot parse or admit a request body
+ status: covered
+ kind: security
+ layer: unit
+ test: tests/test_model_hub_request_body_budget.py::test_bad_auth_does_not_parse_or_admit
- id: MH-PROTOCOL-001
name: Source protocol is response-observed before persistence with no transient residue
status: covered
diff --git a/tests/test_model_hub_request_body_budget.py b/tests/test_model_hub_request_body_budget.py
new file mode 100644
index 000000000..a8dc11af1
--- /dev/null
+++ b/tests/test_model_hub_request_body_budget.py
@@ -0,0 +1,379 @@
+"""Real loopback ingress for the authenticated Model Hub request byte budget."""
+
+from __future__ import annotations
+
+import asyncio
+import json
+from types import SimpleNamespace
+from urllib.parse import urlsplit
+
+import aiohttp
+from aiohttp import web
+import pytest
+
+from config import paths
+from core.handlers.model_hub import turn_gateway
+from core.handlers.model_hub.adapter import RawOutcomeKind
+from core.run_settlement import SETTLED_BY_STOPPED, SETTLED_BY_TERMINAL_RESULT
+from tests.scenario_harness.model_hub import (
+ MemoryModelHubStore,
+ ModelHubScenarioAdapter,
+ ScenarioCallResult,
+ config_with_sources,
+ fixed_model,
+ service_for,
+ source,
+)
+from vibe.i18n import t
+
+
+MIB = 1024 * 1024
+MODEL = "fixture-exact-model"
+TURN = "turn_request_budget"
+TEXT = "请检查这批合成图片的内容吧"
+WIRE_CASES = (
+ ("claude", "messages", "anthropic"),
+ ("codex", "responses", "openai_responses"),
+ ("opencode", "chat/completions", "openai_chat"),
+)
+
+
[email protected](("version", "expected_limit"), [
+ ("3.8.0", 4097),
+ ("3.13.3", 4097),
+ ("3.13.5", 4097),
+ ("3.14.0", 4096),
+ ("3.14.3", 4096),
+])
+async def test_aiohttp_request_limit_compatibility(tmp_path, monkeypatch, version, expected_limit):
+ """Map the inclusive budget to each aiohttp reader's comparison semantics."""
+ monkeypatch.setattr(aiohttp, "__version__", version)
+ monkeypatch.setattr(turn_gateway, "_MAX_REQUEST_BYTES", 4096)
+ paths.get_state_dir().relative_to(tmp_path)
+ gateway = turn_gateway.ModelHubTurnGateway(SimpleNamespace())
+ try:
+ await gateway._ensure_started()
+ assert gateway._runner.app._client_max_size == expected_limit
+ finally:
+ await gateway.close()
+
+
+def budget():
+ return None
+
+
[email protected](params=WIRE_CASES, ids=["messages", "responses", "chat"])
+async def ingress(request, tmp_path, monkeypatch, budget):
+ if budget is not None:
+ monkeypatch.setattr(turn_gateway, "_MAX_REQUEST_BYTES", budget)
+ backend, endpoint, protocol = request.param
+ first = source("src_budget001", [MODEL], vendor="custom", protocol=protocol)
+ second = source("src_budget002", [MODEL], vendor="custom", protocol=protocol)
+ menu_model = MODEL if backend == "opencode" else fixed_model(backend)
+ store = MemoryModelHubStore(config_with_sources(
+ [first, second], backend=backend, menu_model=menu_model,
+ hops=[(first.id, MODEL), (second.id, MODEL)],
+ ))
+ adapter = ModelHubScenarioAdapter(invoke_results=[
+ ScenarioCallResult(RawOutcomeKind.SUCCESS, status=200),
+ ])
+ service = service_for(tmp_path, store, adapter)
+ gateway = turn_gateway.ModelHubTurnGateway(service)
+ base_url, token = await gateway.endpoint(
+ backend,
+ process_scope="fixture-budget",
+ turn_id=TURN,
+ requested_model_id=menu_model,
+ resolved_model_id=MODEL,
+ source_id=first.id,
+ )
+ # Both explicitly supplied stores and implicit service-owned state are private.
+ paths.get_state_dir().relative_to(tmp_path)
+ before = store.load().to_payload()
+ try:
+ yield SimpleNamespace(
+ gateway=gateway, service=service, store=store, adapter=adapter,
+ before=before, backend=backend, protocol=protocol,
+ url=f"{base_url}/v1/{endpoint}", token=token, first=first,
+ menu_model=menu_model,
+ )
+ finally:
+ await gateway.close()
+
+
+def _payload(protocol: str, *, image_chars: int = 0) -> dict:
+ text_type = "input_text" if protocol == "openai_responses" else "text"
+ content = [{"type": text_type, "text": TEXT}]
+ for index in range(20 if image_chars else 0):
+ # Synthetic base64-shaped content, not images or history from a user.
+ data = "iVBORw0KGgo" + chr(ord("A") + index) * image_chars + "="
+ if protocol == "anthropic":
+ image = {"type": "image", "source": {
+ "type": "base64", "media_type": "image/png", "data": data,
+ }}
+ elif protocol == "openai_responses":
+ image = {"type": "input_image", "image_url": f"data:image/png;base64,{data}"}
+ else:
+ image = {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{data}"}}
+ content.append(image)
+ field = "input" if protocol == "openai_responses" else "messages"
+ return {"model": MODEL, field: [{"role": "user", "content": content}], "stream": False}
+
+
+def _encode(payload: dict) -> bytes:
+ return json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
+
+
+async def _chunks(body: bytes):
+ # Split a multibyte character deliberately; framing must not change byte semantics.
+ split = body.find(TEXT.encode("utf-8"))
+ split = split + 1 if split >= 0 else 1
+ yield body[:split]
+ for offset in range(split, len(body), 64 * 1024):
+ yield body[offset:offset + 64 * 1024]
+
+
+async def _post(ingress, body: bytes, framing: str, *, token: str | None = None):
+ async with aiohttp.ClientSession(trust_env=False) as client:
+ async with client.post(
+ ingress.url,
+ data=_chunks(body) if framing == "chunked" else body,
+ headers={
+ "Authorization": f"Bearer {ingress.token if token is None else token}",
+ "Content-Type": "application/json; charset=utf-8",
+ "anthropic-version": "2023-06-01",
+ "openai-beta": "fixture",
+ },
+ ) as response:
+ headers = response.request_info.headers
+ if framing == "chunked":
+ assert headers["Transfer-Encoding"] == "chunked"
+ assert "Content-Length" not in headers
+ else:
+ assert int(headers["Content-Length"]) == len(body)
+ assert "Transfer-Encoding" not in headers
+ return response.status, await response.json(), response.headers
+
+
+def _assert_not_admitted(ingress) -> None:
+ assert ingress.adapter.invocations == []
+ assert ingress.adapter.requests == []
+ assert ingress.adapter.synced == []
+ assert len(ingress.adapter.invoke_results) == 1
+ assert ingress.store.saved_payloads == []
+ assert ingress.store.load().to_payload() == ingress.before
+ assert ingress.service.events.list() == []
+ assert ingress.service.usage.window(days=1, now=ingress.service.now()) == []
+ assert ingress.gateway.resource_leak_records == ()
+
+
+def _settle(ingress, *, settled_by=SETTLED_BY_TERMINAL_RESULT):
+ ingress.gateway.correlation.settle(TURN, settled_by=settled_by)
+ return ingress.service.provenance.get(TURN)
+
+
+def _assert_local_failure(ingress) -> None:
+ _assert_not_admitted(ingress)
+ if ingress.backend == "opencode":
+ # Shared OpenCode processes intentionally cannot attribute one HTTP call.
+ assert TURN not in ingress.gateway.correlation._traces
+ assert _settle(ingress) is None
+ return
+ projection = ingress.gateway.correlation._traces[TURN].terminal_outcome
+ assert projection.outcome == "failed_terminal"
+ assert projection.discriminator == "request_nonfallback"
+ record = _settle(ingress)
+ assert record["outcome"] == "failed_terminal"
+ assert record["requested_model_id"] == ingress.menu_model
+ assert record["terminal_error"] == {
+ "source_id": ingress.first.id,
+ "configured_model_id": MODEL,
+ "channel": "hub",
+ "reason": "invalid_parameter",
+ "stream_started": False,
+ }
+ assert record["served"] is None
+ assert record["failed_attempts"] == []
+
+
[email protected]("framing", ["content-length", "chunked"])
+async def test_large_multi_image_request_is_forwarded_exactly(ingress, framing):
+ """MH-REQUEST-BUDGET-001: ~42 MiB of synthetic images survive real HTTP ingress."""
+ assert turn_gateway._MAX_REQUEST_BYTES == 128 * MIB
+ assert len(TEXT.encode("utf-8")) == 39
+ payload = _payload(ingress.protocol, image_chars=42 * MIB // 20 // 4 * 4)
+ body = _encode(payload)
+ assert 42 * MIB <= len(body) < 43 * MIB
+ status, _result, _headers = await _post(ingress, body, framing)
+ assert status == 200
+ assert ingress.adapter.invocations == [(ingress.first.id, MODEL, ingress.backend)]
+ forwarded, = ingress.adapter.requests
+ assert forwarded == payload
+ assert _encode(forwarded) == body
+ assert forwarded.protocol == ingress.protocol
+ assert forwarded.headers == {"anthropic-version": "2023-06-01", "openai-beta": "fixture"}
+ record = _settle(ingress)
+ if ingress.backend == "opencode":
+ assert record is None
+ else:
+ assert record["outcome"] == "served"
+ assert record["served"]["configured_model_id"] == MODEL
+
+
[email protected]("framing", ["content-length", "chunked"])
[email protected]("delta", [-1, 0, 1], ids=["below", "equal", "over"])
[email protected]("language", ["en", "zh"])
[email protected]("budget", [4096])
+async def test_request_budget_is_inclusive_and_local(ingress, caplog, framing, delta, language):
+ """MH-REQUEST-BUDGET-002: equal bytes pass; +1 is a local 413 without admission."""
+ ingress.gateway._language_provider = lambda: language
+ payload = _payload(ingress.protocol)
+ encoded = _encode(payload)
+ body = encoded + b" " * (4096 + delta - len(encoded))
+ assert len(body) == 4096 + delta
+ assert len(body.decode("utf-8")) < 4096
+ status, result, headers = await _post(ingress, body, framing)
+ if delta <= 0:
+ assert status == 200
+ assert ingress.adapter.requests == [payload]
+ assert ingress.adapter.requests[0].protocol == ingress.protocol
+ return
+ assert status == 413
+ assert result == {"error": {
+ "type": "request_too_large",
+ "code": "request_too_large",
+ "message": t("modelHub.errors.request_too_large", language, limit_mib="0.00390625"),
+ }}
+ assert headers["Cache-Control"] == "no-store"
+ assert headers["X-Content-Type-Options"] == "nosniff"
+ assert "Retry-After" not in headers
+ assert TEXT not in json.dumps(result, ensure_ascii=False) + caplog.text
+ assert ingress.token not in json.dumps(result) + caplog.text
+ _assert_local_failure(ingress)
+
+
[email protected]("framing", ["content-length", "chunked"])
[email protected]("body", [b"{", b"x" * 4097], ids=["malformed", "oversized"])
[email protected]("budget", [4096])
+async def test_bad_auth_does_not_parse_or_admit(ingress, monkeypatch, framing, body):
+ """MH-REQUEST-BUDGET-003: invalid credentials never reach JSON parsing or admission."""
+ async def unexpected_json(*args, **kwargs):
+ pytest.fail("an unauthenticated request reached JSON parsing")
+
+ monkeypatch.setattr(web.Request, "json", unexpected_json)
+ status, result, _headers = await _post(ingress, body, framing, token="invalid-fixture-token")
+ assert status == 401
+ assert result["error"]["code"] == "authentication_error"
+ _assert_not_admitted(ingress)
+
+
[email protected]("framing", ["content-length", "chunked"])
[email protected]("body", [b"{", b'{"model":"\xff"}', b"[]"], ids=["json", "utf8", "array"])
+async def test_malformed_json_is_a_correlated_local_400(ingress, framing, body):
+ """Malformed JSON remains distinct from the local size boundary."""
+ status, result, _headers = await _post(ingress, body, framing)
+ assert status == 400
+ assert result["error"]["code"] == "invalid_request_error"
+ assert result["error"]["message"] == t("modelHub.launch.request_incompatible", "en")
+ _assert_local_failure(ingress)
+
+
+async def _open_partial_upload(ingress, framing: str, body: bytes, *, token: str | None = None):
+ url = urlsplit(ingress.url)
+ reader, writer = await asyncio.open_connection(url.hostname, url.port)
+ framing_header = "Transfer-Encoding: chunked" if framing == "chunked" else "Content-Length: 8192"
+ headers = (
+ f"POST {url.path} HTTP/1.1\r\n"
+ f"Host: {url.netloc}\r\n"
+ f"Authorization: Bearer {ingress.token if token is None else token}\r\n"
+ f"{framing_header}\r\nContent-Type: application/json\r\n\r\n"
+ ).encode("ascii")
+ # Neither framing completes: the server must respond or cancel mid-upload.
+ prefix = f"{len(body):x}\r\n".encode("ascii") + body + b"\r\n" if framing == "chunked" else body
+ writer.write(headers + prefix)
+ await writer.drain()
+ return reader, writer
+
+
+async def _read_json_response(reader):
+ head = await asyncio.wait_for(reader.readuntil(b"\r\n\r\n"), timeout=2)
+ status = int(head.split(b"\r\n", 1)[0].split()[1])
+ headers = dict(line.split(b": ", 1) for line in head.split(b"\r\n")[1:-2])
+ body = await asyncio.wait_for(reader.readexactly(int(headers[b"Content-Length"])), timeout=2)
+ return status, json.loads(body)
+
+
[email protected]("framing", ["content-length", "chunked"])
[email protected]("budget", [4096])
+async def test_oversize_rejection_does_not_wait_for_upload_eof(ingress, framing):
+ """MH-REQUEST-BUDGET-002: exceeding the budget rejects an unfinished upload."""
+ reader, writer = await _open_partial_upload(ingress, framing, b" " * 4097)
+ try:
+ status, result = await _read_json_response(reader)
+ assert status == 413
+ assert result["error"]["code"] == "request_too_large"
+ _assert_local_failure(ingress)
+ finally:
+ writer.close()
+ await writer.wait_closed()
+
+
[email protected]("framing", ["content-length", "chunked"])
+ ("ingress", "ending"),
+ [(case, ending) for case in WIRE_CASES for ending in ("disconnect", "stop")
+ if case[0] != "opencode" or ending == "disconnect"],
+ indirect=["ingress"],
+)
+async def test_partial_upload_cancellation_releases_turn_without_admission(ingress, monkeypatch, framing, ending):
+ """Cancellation during the real body read leaves no Source or usage facts."""
+ entered = asyncio.Event()
+ left = asyncio.Event()
+ original_json = web.Request.json
+
+ async def observe_read(request, **kwargs):
+ entered.set()
+ try:
+ return await original_json(request, **kwargs)
+ finally:
+ left.set()
+
+ monkeypatch.setattr(web.Request, "json", observe_read)
+ _reader, writer = await _open_partial_upload(ingress, framing, b'{"model":')
+ try:
+ await asyncio.wait_for(entered.wait(), timeout=2)
+ assert not left.is_set()
+ if ending == "disconnect":
+ writer.close()
+ await writer.wait_closed()
+ await asyncio.wait_for(left.wait(), timeout=2)
+ await ingress.gateway._drain_turn_requests(TURN)
+ record = _settle(ingress, settled_by=SETTLED_BY_STOPPED)
+ else:
+ completion = ingress.gateway.finalize_turn(
+ TURN,
+ settled_by=SETTLED_BY_STOPPED,
+ finish=lambda: _settle(ingress, settled_by=SETTLED_BY_STOPPED),
+ )
+ assert completion is not None
+ await asyncio.wait_for(completion, timeout=2)
+ assert left.is_set()
+ record = ingress.service.provenance.get(TURN)
+ _assert_not_admitted(ingress)
+ assert ingress.gateway._turn_requests == {}
+ if ingress.backend == "opencode":
+ assert record is None
+ else:
+ assert record["outcome"] == "canceled"
+ assert record["terminal_error"] is None
+ # Stop freezes the prepared identity before teardown; a disconnect
+ # clears it before settlement. Neither represents adapter admission.
+ assert record["canceled_attempt"] == ({
+ "source_id": ingress.first.id,
+ "configured_model_id": MODEL,
+ "channel": "hub",
+ } if ending == "stop" else None)
+ finally:
+ writer.close()
+ await writer.wait_closed()
diff --git a/vibe/i18n/en.json b/vibe/i18n/en.json
index b4614627d..9bf85f32c 100644
--- a/vibe/i18n/en.json
+++ b/vibe/i18n/en.json
@@ -975,6 +975,7 @@
"candidate_suppliers_changed": "The available suppliers changed. Reload the model choices and confirm again.",
"models_dev_unavailable": "models.dev is unavailable. Enter the model metadata manually or retry later.",
"codex_catalog_unavailable": "Codex could not prepare the local Model Hub catalog. Retry this turn.",
+ "request_too_large": "This request exceeds Avibe's local {limit_mib} MiB request limit. Reduce the images or conversation context and retry.",
"engine_down": "The local model gateway is unavailable.",
"engine_down_streamed": "The local model gateway is unavailable. This turn's output may be incomplete.",
"migration_item_conflict": "This migration item conflicts with the current model gateway state.",
diff --git a/vibe/i18n/zh.json b/vibe/i18n/zh.json
index 5bd16311a..85235e978 100644
--- a/vibe/i18n/zh.json
+++ b/vibe/i18n/zh.json
@@ -975,6 +975,7 @@
"candidate_suppliers_changed": "可用供应商已发生变化,请刷新模型选项后重新确认。",
"models_dev_unavailable": "models.dev 当前不可用,请手动填写模型参数或稍后重试。",
"codex_catalog_unavailable": "Codex 无法准备本地模型网关目录,请重试本回合。",
+ "request_too_large": "请求超过 Avibe 本地网关的 {limit_mib} MiB 大小上限。请减少图片或对话上下文后重试。",
"engine_down": "本地模型网关不可用。",
"engine_down_streamed": "本地模型网关不可用。本回合输出可能不完整。",
"migration_item_conflict": "此迁移项与当前模型网关状态冲突。",