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): keep the HTTP status for Claude streams resolved within the first-byte window (#2355)

Call White62e4017a7084
diff --git a/core/handlers/model_hub/turn_gateway.py b/core/handlers/model_hub/turn_gateway.py
index 889703917..20e530c55 100644
--- a/core/handlers/model_hub/turn_gateway.py
+++ b/core/handlers/model_hub/turn_gateway.py
@@ -75,7 +75,12 @@ _RESPONSE_CHUNK_BYTES: Final = 64 * 1024
# recovery waits, Source failover, or an upstream that sends nothing before its
# first model output, so a Claude stream commits its headers once resolution is
# slow and then sends SSE comments, which carry no event, until it resolves.
-_EARLY_STREAM_COMMIT_SECONDS: Final = 2.0
+# A commit also takes the HTTP status away: the CLI drops an unsupported beta or
+# thinking.display and retries only on an HTTP 400 naming it, so a rejection in
+# a committed stream fails the turn. Commit only shortly before the CLI's 300s
+# default first-byte window for a non-Anthropic base URL, which Hub launches
+# raise further; every faster resolution keeps its real status.
+_EARLY_STREAM_COMMIT_SECONDS: Final = 240.0
# Below the CLI's 10s heartbeat so every tick observes bytes.
_STREAM_KEEPALIVE_SECONDS: Final = 5.0
_STREAM_KEEPALIVE_FRAME: Final = b": keepalive\n\n"
diff --git a/modules/agents/model_hub.py b/modules/agents/model_hub.py
index c03449670..ae04dd953 100644
--- a/modules/agents/model_hub.py
+++ b/modules/agents/model_hub.py
@@ -231,9 +231,6 @@ def claude_settings_for_launch(base_settings: str, launch: ModelHubLaunch | None
# must not become launch-settings overrides of native user/project/local choices.
connection_env.pop("CLAUDE_CODE_MAX_CONTEXT_TOKENS", None)
connection_env.pop("CLAUDE_CODE_MAX_OUTPUT_TOKENS", None)
- # Timeout defaults likewise yield to an explicit native choice.
- for key in _CLAUDE_HUB_TIMEOUT_DEFAULTS:
- connection_env.pop(key, None)
settings["env"] = {**settings.get("env", {}), **connection_env}
settings["apiKeyHelper"] = ""
return json.dumps(settings)
@@ -364,8 +361,10 @@ def _localized_launch_error(
# The Hub resolves, waits out recovery, and fails over before a stream's first
-# model output, so Claude's pre-output windows are raised to the CLI's maximum.
-_CLAUDE_HUB_TIMEOUT_DEFAULTS = {
+# model output, so Claude's pre-output windows are pinned to the CLI's maximum.
+# The gateway keeps a slow resolution's HTTP status until shortly before the
+# CLI's default first-byte window; a shorter native choice would abort it first.
+_CLAUDE_HUB_TIMEOUTS = {
"API_TIMEOUT_MS": "1800000",
"CLAUDE_STREAM_FIRST_BYTE_TIMEOUT_MS": "1800000",
}
@@ -411,8 +410,7 @@ def build_claude_hub_env(
# first, so a Source that rejects the value would fail the turn instead
# of the CLI retrying without it. Thinking and effort are unaffected.
result["CLAUDE_CODE_THINKING_DISPLAY_UPDATES"] = "0"
- for key, value in _CLAUDE_HUB_TIMEOUT_DEFAULTS.items():
- result.setdefault(key, value)
+ result.update(_CLAUDE_HUB_TIMEOUTS)
else:
# A native_cli hop keeps the user's official CLI authentication.
result = dict(base_env)
diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yaml
index ff44adaae..9f71f3dd0 100644
--- a/tests/scenarios/model_hub/catalog.yaml
+++ b/tests/scenarios/model_hub/catalog.yaml
@@ -538,6 +538,14 @@ scenarios:
kind: lifecycle
layer: unit
test: tests/test_model_hub_api.py::test_runtime_recovery_does_not_wait_for_the_engine_download
+ - id: MH-RUNTIME-012
+ name: A Claude stream rejected before the early-commit threshold keeps the upstream HTTP status
+ status: covered
+ kind: recovery
+ layer: contract
+ test: tests/test_model_hub_retry_lifecycle.py::test_claude_stream_rejected_within_its_first_byte_budget_keeps_the_status
+ related_tests:
+ - tests/test_model_hub_retry_lifecycle.py::test_slow_stream_resolution_keeps_claude_connected
- id: MH-USAGE-001
name: Tokens an upstream reported before the turn ended are still metered
status: covered
diff --git a/tests/scenarios/model_hub/observations.yaml b/tests/scenarios/model_hub/observations.yaml
index add485e14..621bb821d 100644
--- a/tests/scenarios/model_hub/observations.yaml
+++ b/tests/scenarios/model_hub/observations.yaml
@@ -1,6 +1,12 @@
version: 2
capability: model_hub
observations:
+ - id: MH-OBS-013
+ source: claude-cli-2.1.288-thinking-display-relay-rejection-2026-10-03
+ summary: Claude Code 2.1.288 sends thinking.display "updates" with its beta by default, and a relay that rejects it answers HTTP 400. The CLI drops a rejected beta or display value and retries only on an HTTP 400 naming it, but a Hub resolution slower than 2s had already committed a 200 stream, so the rejection arrived as an in-stream error and ended the turn. Claude streams now keep the real status until shortly before the CLI's default first-byte window, and Claude hub launches pin both pre-output timeouts to the CLI maximum so no shorter native choice aborts that wait.
+ affects:
+ - MH-RUNTIME-010
+ - MH-RUNTIME-012
- id: MH-OBS-012
source: codex-0.156.0-hermetic-remote-compaction-wire-2026-09-29
summary: Codex 0.155.0 removed the remote_compaction_v2 switch and the legacy /responses/compact client, so MH-OBS-009's header-only compact probe is unreachable. Remote compaction posts to the ordinary Responses endpoint with a compaction_trigger input item and carries the new turn's identity in both the metadata header and body client_metadata. Codex 0.154.0, where v2 was already the default, and 0.158.0 send the same shape.
diff --git a/tests/test_model_hub_injection.py b/tests/test_model_hub_injection.py
index ab4e81dad..7d8853a86 100644
--- a/tests/test_model_hub_injection.py
+++ b/tests/test_model_hub_injection.py
@@ -250,20 +250,25 @@ def test_claude_catalog_limits_fill_only_absent_environment_values(channel, expl
@pytest.mark.parametrize(
- "explicit", [{}, {"API_TIMEOUT_MS": "900000"}, {"CLAUDE_STREAM_FIRST_BYTE_TIMEOUT_MS": ""}],
+ "explicit", [{}, {"API_TIMEOUT_MS": "60000"}, {"CLAUDE_STREAM_FIRST_BYTE_TIMEOUT_MS": "30000"}],
)
def test_claude_hub_waits_out_resolution_as_one_streaming_request(explicit):
- """MH-RUNTIME-010: the Hub owns pre-output waits, so the CLI neither aborts nor replays them."""
+ """MH-RUNTIME-010: the Hub owns pre-output waits, so the CLI neither aborts nor replays them.
+
+ The gateway withholds headers while a resolution is still short of the CLI's
+ default first-byte window, so a shorter native timeout would abort it.
+ """
timeouts = ("API_TIMEOUT_MS", "CLAUDE_STREAM_FIRST_BYTE_TIMEOUT_MS")
+ cli_maximum = dict.fromkeys(timeouts, "1800000")
native = {**explicit, "CLAUDE_CODE_DISABLE_NONSTREAMING_FALLBACK": "0"}
launch = hub_launch()
env = build_claude_hub_env(native, launch)
- assert {key: env[key] for key in timeouts} == {key: explicit.get(key, "1800000") for key in timeouts}
+ assert {key: env[key] for key in timeouts} == cli_maximum
assert env["CLAUDE_CODE_DISABLE_NONSTREAMING_FALLBACK"] == "1"
settings = json.loads(claude_settings_for_launch(json.dumps({"env": native}), launch))
- assert {key: settings["env"].get(key) for key in timeouts} == {key: explicit.get(key) for key in timeouts}
+ assert {key: settings["env"].get(key) for key in timeouts} == cli_maximum
assert settings["env"]["CLAUDE_CODE_DISABLE_NONSTREAMING_FALLBACK"] == "1"
official = hub_launch(channel="native_cli", gateway_base_url=None, gateway_token=None)
diff --git a/tests/test_model_hub_retry_lifecycle.py b/tests/test_model_hub_retry_lifecycle.py
index dfa96c38e..e2e1bc611 100644
--- a/tests/test_model_hub_retry_lifecycle.py
+++ b/tests/test_model_hub_retry_lifecycle.py
@@ -644,6 +644,42 @@ def test_expiry_after_mixed_real_failures_keeps_the_action_blocker_terminal(tmp_
KEEPALIVE = b": keepalive\n\n"
+def test_claude_stream_rejected_within_its_first_byte_budget_keeps_the_status(tmp_path):
+ """MH-RUNTIME-012: a rejection after a few seconds still reaches Claude as HTTP 400.
+
+ Claude Code downgrades an unsupported request feature (a beta, or
+ thinking.display) and retries only on an HTTP 400 naming it; a rejection
+ inside a committed 200 stream ends the turn instead.
+ """
+ rejection = "thinking.adaptive.display: Input should be 'summarized', 'omitted'"
+
+ async def run() -> None:
+ source = _source("src_rejects1", "Rejects", vendor="anthropic", protocol="anthropic")
+ service, _clock, models = configured_service(tmp_path, sources=[source])
+ calls = []
+
+ async def respond(request: web.Request) -> web.Response:
+ calls.append(await request.json())
+ # Slower than a fast Source, well within Claude's first-byte window.
+ await asyncio.sleep(2.5)
+ return web.json_response(
+ {"type": "error", "error": {"type": "invalid_request_error", "message": rejection}}, status=400,
+ )
+
+ async with loopback_engine(tmp_path, service, respond) as (adapter, _engine_client):
+ async with gateway_client(service, "claude", models["claude"]) as (_gateway, client, url, headers):
+ async with client.post(
+ url, headers=headers, json={"model": "shared-model", "stream": True},
+ ) as response:
+ body = await response.text()
+ assert response.status == 400, body
+ assert rejection in json.loads(body)["error"]["message"]
+ assert adapter._active_transports == 0
+ assert len(calls) == 1
+
+ asyncio.run(run())
+
+
@pytest.mark.parametrize("backend", BACKENDS)
@pytest.mark.parametrize("ending", ["served", "recovered", "exhausted", "disconnect"])
def test_slow_stream_resolution_keeps_claude_connected(monkeypatch, tmp_path, backend, ending):