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 xAI reasoning ciphertext on replay by rendering its models is-compat (#2767)

Call Whitee376dca45a83
diff --git a/tests/e2e/test_model_hub_engine_wire.py b/tests/e2e/test_model_hub_engine_wire.py
index 50f3153af..4f43ca051 100644
--- a/tests/e2e/test_model_hub_engine_wire.py
+++ b/tests/e2e/test_model_hub_engine_wire.py
@@ -9,6 +9,7 @@ including Model Hub's own outcome for the Turn, so two engines can be compared.
from __future__ import annotations
import asyncio
+import base64
import json
import os
import signal
@@ -80,11 +81,12 @@ _CHAT_FINISH = _sse({"id": "chatcmpl-fixture", "object": "chat.completion.chunk"
_CHAT_USAGE = _sse({"id": "chatcmpl-fixture", "object": "chat.completion.chunk", "created": 1700000000,
"model": MODEL, "choices": [],
"usage": {"prompt_tokens": 3, "completion_tokens": 1, "total_tokens": 4}})
-# Fernet-shaped, so the engine keeps it with its id (CPA strips other shapes).
+# OpenAI's reasoning ciphertext is Fernet-shaped; xAI's carries no envelope, only opaque base64.
_ENCRYPTED = "g" + "A" * 97
+_XAI_ENCRYPTED = base64.b64encode(bytes(range(7, 199))).decode()
-def _responses_reasoning_tool_frames() -> list[bytes]:
+def _responses_reasoning_tool_frames(encrypted: str = _ENCRYPTED) -> list[bytes]:
events = [
{"type": "response.created", "response": {"id": "resp_rt", "object": "response", "status": "in_progress",
"model": MODEL, "output": []}},
@@ -101,7 +103,7 @@ def _responses_reasoning_tool_frames() -> list[bytes]:
{"type": "response.reasoning_summary_part.done", "item_id": "rs_1", "output_index": 0,
"summary_index": 0, "part": {"type": "summary_text", "text": "Checking the weather tool."}},
{"type": "response.output_item.done", "output_index": 0,
- "item": {"id": "rs_1", "type": "reasoning", "encrypted_content": _ENCRYPTED,
+ "item": {"id": "rs_1", "type": "reasoning", "encrypted_content": encrypted,
"summary": [{"type": "summary_text", "text": "Checking the weather tool."}]}},
{"type": "response.output_item.added", "output_index": 1,
"item": {"id": "fc_1", "type": "function_call", "status": "in_progress", "call_id": "call_fixture",
@@ -172,7 +174,9 @@ class _Harness:
@asynccontextmanager
-async def _through_engine(tmp_path, monkeypatch, protocol: str, *handlers, model_id: str = MODEL, efforts=()):
+async def _through_engine(
+ tmp_path, monkeypatch, protocol: str, *handlers, model_id: str = MODEL, efforts=(), vendor: str = "custom",
+):
"""A route of one Source per handler, in order, all speaking ``protocol`` and serving ``model_id``."""
requests: list[list[dict]] = [[] for _ in handlers]
@@ -192,12 +196,12 @@ async def _through_engine(tmp_path, monkeypatch, protocol: str, *handlers, model
sources = []
for index, handler in enumerate(handlers):
root = await stack.enter_async_context(_server(recording(index, handler)))
- source = _source(f"src_wire{index:04d}", f"Wire fixture {index}", vendor="custom",
+ source = _source(f"src_wire{index:04d}", f"Wire fixture {index}", vendor=vendor,
protocol=protocol, model_id=model_id)
source.models[0].reasoning_efforts = list(efforts)
source.base_url = root + ("/v1" if protocol == "openai_responses" else "")
source.credential_ref = adapter.state_store.store_api_key(
- f"sk-synthetic-wire-{index}", vendor="custom", protocol=protocol, base_url=source.base_url,
+ f"sk-synthetic-wire-{index}", vendor=vendor, protocol=protocol, base_url=source.base_url,
)
sources.append(source)
config = _vibey_service(tmp_path, sources).store.config
@@ -424,14 +428,17 @@ async def test_chat_stream_terminal_depends_on_finish_reason(tmp_path, monkeypat
assert following["status"] == 200, following
-async def test_responses_stream_with_reasoning_and_tool_call(tmp_path, monkeypatch) -> None:
- """Reasoning summaries and a tool call pass through in order; the replay keeps its items."""
[email protected](("vendor", "encrypted"), [("custom", _ENCRYPTED), ("xai", _XAI_ENCRYPTED)],
+ ids=["fernet", "xai"])
+async def test_responses_stream_with_reasoning_and_tool_call(tmp_path, monkeypatch, vendor, encrypted) -> None:
+ """MH-HOSTED-GATEWAY-008: reasoning summaries and a tool call pass through in order; the replay keeps its
+ items, and a reasoning item reaches the Source that issued it with its ciphertext and id, xAI's included."""
async def handler(request, body):
- return await _stream(request, _responses_reasoning_tool_frames())
+ return await _stream(request, _responses_reasoning_tool_frames(encrypted))
replay = [
{"type": "message", "role": "user", "content": [{"type": "input_text", "text": "weather in 上海?"}]},
- {"type": "reasoning", "id": "rs_1", "status": "completed", "encrypted_content": _ENCRYPTED,
+ {"type": "reasoning", "id": "rs_1", "status": "completed", "encrypted_content": encrypted,
"summary": [{"type": "summary_text", "text": "Checking the weather tool."}]},
{"type": "function_call", "id": "fc_1", "status": "completed", "call_id": "call_fixture",
"name": "weather", "arguments": "{\"city\":\"上海\"}"},
@@ -439,7 +446,7 @@ async def test_responses_stream_with_reasoning_and_tool_call(tmp_path, monkeypat
]
tools = [{"type": "function", "name": "weather", "description": "fixture",
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}}}]
- async with _through_engine(tmp_path, monkeypatch, "openai_responses", handler) as harness:
+ async with _through_engine(tmp_path, monkeypatch, "openai_responses", handler, vendor=vendor) as harness:
url, body = _frontend_request("openai_responses", harness.hop, stream=True, extra={
"tools": tools, "reasoning": {"effort": "high", "summary": "auto"},
"include": ["reasoning.encrypted_content"],
@@ -463,7 +470,7 @@ async def test_responses_stream_with_reasoning_and_tool_call(tmp_path, monkeypat
assert [item["type"] for item in completed["response"]["output"]] == ["reasoning", "function_call"]
assert second["status"] == 200, second
reasoning, call = replayed[1], replayed[2]
- assert reasoning["id"] == "rs_1" and reasoning["encrypted_content"] == _ENCRYPTED, reasoning
+ assert reasoning["id"] == "rs_1" and reasoning["encrypted_content"] == encrypted, reasoning
assert call["call_id"] == "call_fixture" and call["arguments"] == "{\"city\":\"上海\"}", call
diff --git a/tests/scenarios/INDEX.yaml b/tests/scenarios/INDEX.yaml
index 7ce7f649f..bf42da7b4 100644
--- a/tests/scenarios/INDEX.yaml
+++ b/tests/scenarios/INDEX.yaml
@@ -44,6 +44,7 @@ capabilities:
- tests/e2e/test_model_hub_runtime.py
- tests/e2e/test_model_hub_sources.py
- tests/e2e/test_model_hub_usage_events.py
+ - tests/e2e/test_model_hub_engine_wire.py
- tests/e2e/test_model_hub_service_acceptance.py
- tests/e2e/test_model_hub_codex_image.py
- tests/e2e/test_model_hub_images.py
diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yaml
index b9d5dcbe2..70ffacbac 100644
--- a/tests/scenarios/model_hub/catalog.yaml
+++ b/tests/scenarios/model_hub/catalog.yaml
@@ -105,6 +105,7 @@ capability:
- tests/e2e/test_model_hub_runtime.py
- tests/e2e/test_model_hub_sources.py
- tests/e2e/test_model_hub_usage_events.py
+ - tests/e2e/test_model_hub_engine_wire.py
- tests/e2e/test_model_hub_service_acceptance.py
- tests/e2e/test_model_hub_codex_image.py
- tests/e2e/test_model_hub_images.py
@@ -2585,6 +2586,14 @@ scenarios:
- tests/test_model_hub_hosted_images.py::test_an_image_call_costs_its_delivered_images_at_their_list_price
- tests/e2e/test_model_hub_images.py::test_each_offered_image_path_reaches_the_vendors_images_endpoint
- tests/e2e/test_model_hub_images.py::test_only_an_upstream_refusal_teaches_that_a_source_lacks_an_image_model
+ - id: MH-HOSTED-GATEWAY-008
+ name: A reasoning item replayed to the Source that issued it reaches that upstream through the pinned engine with its encrypted_content and id, xAI's envelope-less ciphertext included
+ status: covered
+ kind: contract
+ layer: e2e
+ test: tests/e2e/test_model_hub_engine_wire.py::test_responses_stream_with_reasoning_and_tool_call
+ related_tests:
+ - tests/test_model_hub_runtime.py::test_only_an_xai_responses_entry_marks_its_models_is_compat
next_priority:
- MH-PROTOCOL-004
- MH-RUNTIME-001
diff --git a/tests/test_model_hub_runtime.py b/tests/test_model_hub_runtime.py
index 438ee192d..ee128de17 100644
--- a/tests/test_model_hub_runtime.py
+++ b/tests/test_model_hub_runtime.py
@@ -1876,6 +1876,42 @@ def test_an_api_key_engine_entry_registers_its_vendors_image_models(tmp_path: Pa
assert f'{sources["src_gemini0001"].prefix}/gemini-3-pro-image-preview' in expected
+def test_only_an_xai_responses_entry_marks_its_models_is_compat(tmp_path: Path) -> None:
+ """MH-HOSTED-GATEWAY-008: the engine keeps a replayed reasoning item's envelope-less ciphertext only for an
+ is-compat model; xAI's ciphertext has none, OpenAI's Fernet one does, and other Responses Sources stay stock."""
+
+ store = EngineStateStore(tmp_path / "state")
+ instance_dir, runtime_secrets = store.prepare_instance("install-1")
+ store.sync_sources([
+ _binding(store.store_api_key("xai-secret", vendor="xai", protocol="openai_responses"),
+ source_id="src_xai000001", vendor="xai", protocol="openai_responses", base_url=None,
+ model_ids=("grok-4.3", "grok-4.20-0309-reasoning")),
+ _binding(store.store_api_key("openai-secret", vendor="openai", protocol="openai_responses"),
+ source_id="src_openai0001", vendor="openai", protocol="openai_responses", base_url=None,
+ model_ids=("gpt-6-astra",)),
+ _binding(store.store_api_key("custom-secret", protocol="openai_responses",
+ base_url="https://relay.example.test/v1"),
+ source_id="src_custom0001", protocol="openai_responses", base_url="https://relay.example.test/v1",
+ model_ids=("grok-4.3",)),
+ ])
+ config_path = instance_dir / "config.yaml"
+ write_engine_config(
+ config_path, host="127.0.0.1", port=18231, auth_dir=store.auth_dir, runtime_secrets=runtime_secrets,
+ sources=store.list_sources(), state_store=store,
+ )
+ payload = yaml.safe_load(config_path.read_text(encoding="utf-8"))
+
+ compat = {
+ entry["base-url"]: {model["name"]: model.get("is-compat") for model in entry["models"]}
+ for entry in payload["codex-api-key"]
+ }
+ assert compat == {
+ "https://api.x.ai/v1": {"grok-4.3": True, "grok-4.20-0309-reasoning": True},
+ "https://api.openai.com/v1": {"gpt-6-astra": None, "gpt-image-2": None},
+ "https://relay.example.test/v1": {"grok-4.3": None},
+ }
+
+
def _rendered_text_only(tmp_path: Path, model_ids: tuple[str, ...], text_only: tuple[str, ...]) -> set[str]:
store = EngineStateStore(tmp_path / "state")
instance_dir, runtime_secrets = store.prepare_instance("install-1")
diff --git a/vibe/model_hub_runtime/config.py b/vibe/model_hub_runtime/config.py
index e470959fc..01a19c221 100644
--- a/vibe/model_hub_runtime/config.py
+++ b/vibe/model_hub_runtime/config.py
@@ -221,6 +221,13 @@ def _append_source(
if not base_url:
raise EngineStateError("Responses API source requires a base URL")
entry = {"api-key": api_key, "prefix": source.prefix, "base-url": base_url}
+ if models and source.vendor == "xai":
+ # CPA (MIT) v8.0.23, internal/runtime/executor/openai_responses_signature.go: a replayed
+ # reasoning item keeps an encrypted_content without OpenAI's Fernet envelope, and its id,
+ # only for an is-compat model. xAI's ciphertext has no envelope, so without the flag the
+ # engine strips both and xAI loses its reasoning on every replay. The flag also replays
+ # reasoning content as xAI returned it and drops Codex multi-agent fields xAI never takes.
+ models = [{**model, "is-compat": True} for model in models]
if models:
entry["models"] = models
payload.setdefault("codex-api-key", []).append(entry)