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): credit a Vibey gateway request only to the turn it names (#2586)

Call White9a32f83fced3
diff --git a/core/handlers/model_hub/provenance.py b/core/handlers/model_hub/provenance.py
index 5a11bab51..50383f884 100644
--- a/core/handlers/model_hub/provenance.py
+++ b/core/handlers/model_hub/provenance.py
@@ -1348,28 +1348,22 @@ class TurnCorrelationRegistry:
return trace.hosted_executors if trace is not None else None
def hosted_executor_bindings(
- self, *, backend: str, token: str,
+ self, *, backend: str, token: str, request_metadata: Mapping[str, str] | None,
) -> tuple[str, dict[str, tuple[str, str, str] | None]] | None:
- """The live turn a credential belongs to and the proxy executors it bound, per tool; None without one.
+ """The live turn a request belongs to and the proxy executors it bound, per tool; None without one.
- Only a credential whose route is the current route of an active turn
- that still admits work has bindings: a superseded or turnless
- credential, or a turn being stopped or finalized, cannot start a call.
+ Only the turn the request names, while it holds the credential's route
+ and still admits work, has bindings: a request from a finished,
+ superseded or unnamed turn, or from a turn being stopped or finalized,
+ cannot start a call.
"""
with self._lock:
- credential = self._credential(backend, token)
- scope = self._scopes.get(credential.scope_key) if credential is not None else None
- if scope is None or credential.route is None:
+ owner = self._request_turn(backend, token, request_metadata)
+ if owner is None:
return None
- for turn_id in scope.active_turns:
- trace = self._traces.get(turn_id)
- if (
- scope.prepared_routes.get(turn_id) == credential.route
- and (trace is None or not trace.admission_closed)
- ):
- return turn_id, dict(credential.route.hosted_executors)
- return None
+ credential = self._credential(backend, token)
+ return owner[0], dict(credential.route.hosted_executors)
def gateway_primary_origin(self, *, backend: str, token: str) -> HopOrigin | None:
"""Read the immutable launch origin, independent of live turn ownership."""
@@ -1422,6 +1416,52 @@ class TurnCorrelationRegistry:
and scope.prepared_routes.get(turn_id) == route
)
+ def _request_turn(
+ self,
+ backend: str,
+ token: str,
+ request_metadata: Mapping[str, str] | None,
+ ) -> tuple[str, ScopeKey] | None:
+ """The live turn one gateway request on a route credential belongs to; call under `_lock`.
+
+ Vibey names its turn on every request (``VIBEY_TURN_HEADER``), because
+ its credential answers for a route that later turns of the session
+ reuse: the named turn owns the request only while it holds that route
+ and still admits work, so a request from a finished turn is never
+ credited to its successor. Other credential-bound clients name no
+ turn, and their sole live turn is inferred.
+ """
+
+ if backend != "vibey":
+ return self._exact_turn(backend, token)
+ turn_id = self._named_route_turn(backend, token, request_metadata)
+ if turn_id is None:
+ return None
+ trace = self._traces.get(turn_id)
+ if trace is not None and (trace.ambiguous or trace.admission_closed):
+ return None
+ return turn_id, self._credential(backend, token).scope_key
+
+ def _named_route_turn(
+ self,
+ backend: str,
+ token: str,
+ request_metadata: Mapping[str, str] | None,
+ ) -> str | None:
+ """The turn a request names, if it holds the request credential's route; call under `_lock`."""
+
+ credential = self._credential(backend, token)
+ scope = self._scopes.get(credential.scope_key) if credential is not None else None
+ turn_id = request_metadata.get("avibe_turn_id") if isinstance(request_metadata, Mapping) else None
+ if (
+ scope is None
+ or credential.route is None
+ or not isinstance(turn_id, str)
+ or not self._explicit_turn_owns_route(scope, turn_id=turn_id, route=credential.route)
+ ):
+ return None
+ return turn_id
+
def retire_scope(
self,
backend: str,
@@ -1517,9 +1557,10 @@ class TurnCorrelationRegistry:
backend: str,
token: str,
requested_model_id: str,
+ request_metadata: Mapping[str, str] | None = None,
) -> Optional[str]:
with self._lock:
- exact = self._exact_turn(backend, token)
+ exact = self._request_turn(backend, token, request_metadata)
if exact is None:
return None
turn_id, key = exact
@@ -1819,6 +1860,7 @@ class TurnCorrelationRegistry:
backend=backend,
token=token,
requested_model_id=gateway_model_id,
+ request_metadata=request_metadata,
)
if owner != prepared_turn_id:
owner = None
@@ -1938,9 +1980,13 @@ class TurnCorrelationRegistry:
# can resolve to one upstream model id, and crediting a turn for a
# request that belongs to the other one is how a turn ends up
# ambiguous over a model it never asked for.
- claimed = any(
- prepared == route and turn_id in scope.active_turns
- for turn_id, prepared in scope.prepared_routes.items()
+ claimed = (
+ self._named_route_turn(backend, token, request_metadata) is not None
+ if backend == "vibey"
+ else any(
+ prepared == route and turn_id in scope.active_turns
+ for turn_id, prepared in scope.prepared_routes.items()
+ )
)
return route.requested_model_id, claimed
@@ -2016,7 +2062,7 @@ class TurnCorrelationRegistry:
)
return turn_id
- exact = self._exact_turn(backend, token)
+ exact = self._request_turn(backend, token, request_metadata)
if exact is None:
return None
turn_id, _ = exact
@@ -2028,10 +2074,6 @@ class TurnCorrelationRegistry:
):
return None
if backend == "vibey":
- if credential.route != scope.prepared_routes.get(turn_id):
- # A superseded credential must not poison the successor
- # even when malformed JSON fails before body attribution.
- return None
trace.gateway_requests.add(request_id)
trace.pending_attempts[request_id] = AttemptIdentity(
source_id=trace.gateway_source_id,
diff --git a/core/handlers/model_hub/turn_gateway.py b/core/handlers/model_hub/turn_gateway.py
index 8c50313d5..3a81ec758 100644
--- a/core/handlers/model_hub/turn_gateway.py
+++ b/core/handlers/model_hub/turn_gateway.py
@@ -160,6 +160,23 @@ def _codex_request_metadata(raw: str | None) -> dict | None:
return {key: metadata[key] for key in _CODEX_ROUTE_FIELDS}
+# The turn a Vibey gateway request belongs to. Vibey sends it on every request
+# from ``HopResolution.request_headers``: its credential answers for a route
+# that the session's later turns reuse, so ownership is named, never inferred.
+VIBEY_TURN_HEADER: Final = "x-avibe-turn-id"
+
+
+def _request_metadata(backend: str, request: web.Request) -> dict | None:
+ """The request identity a client carries beside its credential, if its backend carries one."""
+
+ if backend == "codex":
+ return _codex_request_metadata(request.headers.get(_CODEX_TURN_METADATA))
+ if backend == "vibey":
+ turn_id = request.headers.get(VIBEY_TURN_HEADER)
+ return {"avibe_turn_id": turn_id} if turn_id else None
+ return None
+
+
def _consume_codex_request_metadata(payload: dict, header: dict | None) -> bool:
"""Verify duplicate identity and keep private routing fields local."""
@@ -706,7 +723,9 @@ class ModelHubTurnGateway:
return refused(400, "invalid_request_error")
# Checked and owned with no await between them: once admitted, the
# call belongs to its turn, and stopping or finalizing the turn ends it.
- live = self.correlation.hosted_executor_bindings(backend="vibey", token=token)
+ live = self.correlation.hosted_executor_bindings(
+ backend="vibey", token=token, request_metadata=_request_metadata("vibey", request),
+ )
try:
with self._own_turn_call(live[0]) if live is not None else nullcontext():
result = await self.service.run_hosted_tool(
@@ -787,10 +806,7 @@ class ModelHubTurnGateway:
terminalizer = self.correlation.gateway_terminalizer(
backend=backend,
token=token,
- request_metadata=(
- _codex_request_metadata(request.headers.get(_CODEX_TURN_METADATA))
- if backend == "codex" else None
- ),
+ request_metadata=_request_metadata(backend, request),
)
execution = _TurnExecution()
with self._own_turn_request(terminalizer, execution), terminalizer:
diff --git a/docs/plans/agent-core-contracts/hop-resolution.schema.json b/docs/plans/agent-core-contracts/hop-resolution.schema.json
index e840d73a1..c578607d2 100644
--- a/docs/plans/agent-core-contracts/hop-resolution.schema.json
+++ b/docs/plans/agent-core-contracts/hop-resolution.schema.json
@@ -54,7 +54,7 @@
"additionalProperties": {
"type": "string"
},
- "description": "Correlation metadata the gateway needs; always present, empty when there is none."
+ "description": "Correlation metadata the gateway needs, sent on every gateway request; always present, empty when there is none. A Turn's resolution carries `x-avibe-turn-id`."
},
"capabilities": {
"type": "object",
diff --git a/docs/plans/agent-core-contracts/model-hub-consumer.md b/docs/plans/agent-core-contracts/model-hub-consumer.md
index e267cd34a..55a0ac7db 100644
--- a/docs/plans/agent-core-contracts/model-hub-consumer.md
+++ b/docs/plans/agent-core-contracts/model-hub-consumer.md
@@ -23,6 +23,12 @@ consumer's requirements, and the Model Hub lane lands the change in both places.
and capabilities. A capability Model Hub does not know is `null`, never guessed; `input_limit` is `null` until Model
Hub stores one. The agent treats unknown values conservatively (C-2). It resolves once per run and again after a
retryable error.
+ - `request_headers` carries `x-avibe-turn-id`, the Turn the resolution was made for, and the agent sends it on
+ every gateway request: model calls and proxied hosted calls.
+ - The token answers for the route, which later Turns of the Session reuse. The gateway credits a request only
+ to the Turn it names, and only while that Turn holds the token's route and still admits work.
+ - A model request from a finished Turn still routes but is credited to no Turn. A hosted call from one is
+ `executor_not_bound`.
4. **Served-hop report.** Every gateway response for `vibey` carries `x-avibe-served-hop`: compact, ASCII-escaped JSON
with exactly `provider`, `api`, `model`, `source` (since Model Hub contract v12), taken from the winning attempt
(Source vendor, Source protocol, upstream target, Source id) and sent before the first model byte. The gateway does
@@ -98,7 +104,8 @@ consumer's requirements, and the Model Hub lane lands the change in both places.
- The served-hop report carries `source`, the serving Source id (item 4, Model Hub contract v12), so that a
hosted block's payload is tagged with the exact Source; two Sources can share an origin. The gateway and the
parser in `conversion-tables.md` changed together in that bump. Native hosted mode lands with or after v12.
- - **The proxy executor.** `POST {base_url}/hosted-tools/{tool}` with the turn's Bearer token and the body
+ - **The proxy executor.** `POST {base_url}/hosted-tools/{tool}` with the turn's Bearer token, its
+ `request_headers` (item 3), and the body
`{"arguments": <canonical arguments>, "executor": HopResolution.hosted_tools[tool].executor}`. The executor
may be `null`.
- The executor must be the one the caller's live turn bound for that tool. The turn's first resolution
diff --git a/modules/agents/model_hub.py b/modules/agents/model_hub.py
index f06555aa9..a40aa1629 100644
--- a/modules/agents/model_hub.py
+++ b/modules/agents/model_hub.py
@@ -61,7 +61,7 @@ from core.handlers.model_hub.service import (
turn_refusal,
)
from core.services.settings import load_config_or_default
-from core.handlers.model_hub.turn_gateway import ModelHubTurnGateway
+from core.handlers.model_hub.turn_gateway import VIBEY_TURN_HEADER, ModelHubTurnGateway
from core.native_dispatch_phase import mark_prewrite_recovery_required
from modules.agents.catalog import display_name_for_backend
from vibe.codex_config import format_toml_basic_string
@@ -1376,6 +1376,20 @@ class ModelHubRuntimeRouter:
if self.turn_gateway is None:
prefix = await self._source_prefix(source.id)
runtime_model = f"{prefix}/{target_model}"
+ request_metadata: dict[str, str] = {}
+ if self.turn_gateway is not None and backend == "codex":
+ request_metadata = self.turn_gateway.correlation.gateway_request_metadata(
+ backend=backend, token=gateway_token, turn_id=turn_id,
+ route=PreparedGatewayRoute(
+ requested_model_id=requested_model,
+ resolved_model_id=target_model,
+ source_id=source.id,
+ gateway_request_model_id=runtime_model,
+ ),
+ )
+ elif self.turn_gateway is not None and backend == "vibey" and str(turn_id or "").strip():
+ # Vibey names its turn on every gateway request (HopResolution.request_headers).
+ request_metadata = {VIBEY_TURN_HEADER: str(turn_id).strip()}
launch = ModelHubLaunch(
backend=backend,
channel="hub",
@@ -1385,19 +1399,7 @@ class ModelHubRuntimeRouter:
source_id=source.id,
gateway_base_url=gateway_base_url,
gateway_token=gateway_token,
- gateway_request_metadata=(
- self.turn_gateway.correlation.gateway_request_metadata(
- backend=backend, token=gateway_token, turn_id=turn_id,
- route=PreparedGatewayRoute(
- requested_model_id=requested_model,
- resolved_model_id=target_model,
- source_id=source.id,
- gateway_request_model_id=runtime_model,
- ),
- )
- if self.turn_gateway is not None and backend == "codex"
- else {}
- ),
+ gateway_request_metadata=request_metadata,
context_window=context_window,
max_output_tokens=max_output_tokens,
supports_tools=supports_tools,
diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yaml
index af69d9ece..1e4c273f3 100644
--- a/tests/scenarios/model_hub/catalog.yaml
+++ b/tests/scenarios/model_hub/catalog.yaml
@@ -399,6 +399,14 @@ scenarios:
test: tests/test_model_hub_vibey_consumer.py::test_a_vibey_sessions_turns_share_its_scope_and_each_keeps_its_provenance
related_tests:
- tests/test_vibey_agent.py::test_every_model_hub_resolution_names_its_turn
+ - id: MH-VIBEY-009
+ name: A Vibey request names its Turn, so a finished Turn's late model or hosted-tool request is never credited to the next Turn on the same route
+ status: covered
+ kind: lifecycle
+ layer: contract
+ test: tests/test_model_hub_vibey_consumer.py::test_a_finished_turns_request_is_never_credited_to_the_next_turn_on_its_route
+ related_tests:
+ - tests/test_model_hub_vibey_consumer.py::test_a_vibey_sessions_turns_share_its_scope_and_each_keeps_its_provenance
- id: MH-CODEX-ROUTING-001
name: Shared Codex authentication stays stable while concurrent aliases route and settle independently
status: covered
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 7a2006124..db29f7b33 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
@@ -289,7 +289,10 @@ async def _post_turn(
payload: dict[str, object] | None = None,
) -> tuple[int, bytes]:
headers = {"Authorization": f"Bearer {launch.gateway_token}"}
- if launch.gateway_request_metadata:
+ if launch.backend == "vibey":
+ # Vibey sends its resolution's request headers, which name its Turn.
+ headers.update(launch.gateway_request_metadata)
+ elif launch.gateway_request_metadata:
headers["x-codex-turn-metadata"] = json.dumps(launch.gateway_request_metadata)
request_payload = payload or {
"model": launch.runtime_model,
diff --git a/tests/test_model_hub_codex_image_tool.py b/tests/test_model_hub_codex_image_tool.py
index a1a7abf96..ce971e369 100644
--- a/tests/test_model_hub_codex_image_tool.py
+++ b/tests/test_model_hub_codex_image_tool.py
@@ -238,6 +238,7 @@ async def test_other_callers_reach_the_engine_with_exactly_their_declared_tools(
router = ModelHubRuntimeRouter(service=service, turn_gateway=gateway)
hop = await router.resolve_hop("menu-alias", process_scope="vibey:image", turn_id="turn-image")
url, token, model = f'{hop["base_url"]}/{path}', hop["token"], hop["runtime_model"]
+ pin = {**hop["request_headers"], **(pin or {})}
else:
base_url, token = await gateway.endpoint(
backend, process_scope="/repo", turn_id="turn_image_tool",
diff --git a/tests/test_model_hub_google.py b/tests/test_model_hub_google.py
index 316cad99b..a14ff48af 100644
--- a/tests/test_model_hub_google.py
+++ b/tests/test_model_hub_google.py
@@ -332,7 +332,7 @@ async def test_google_router_gateway_failover_preserves_visible_history_and_orig
async with aiohttp.ClientSession() as client:
async with client.post(
f'{hop["base_url"]}/models/{hop["runtime_model"]}:{action}',
- json=signed, headers={"x-goog-api-key": hop["token"]},
+ json=signed, headers={"x-goog-api-key": hop["token"], **hop["request_headers"]},
) as response:
assert response.status == 200, await response.text()
assert await response.read() == raw
@@ -449,7 +449,7 @@ async def test_google_local_errors_are_native_and_never_admit_a_hop(tmp_path, su
async with aiohttp.ClientSession() as client:
async with client.post(
f'{hop["base_url"]}/models/{hop["runtime_model"]}{suffix}', json=payload,
- headers={"x-goog-api-key": hop["token"] if token_valid else "invalid"},
+ headers={"x-goog-api-key": hop["token"] if token_valid else "invalid", **hop["request_headers"]},
) as response:
assert response.status == status
assert SERVED_HOP_HEADER not in response.headers
@@ -505,7 +505,7 @@ async def test_google_skips_unsupported_hops_before_admission(
async with aiohttp.ClientSession() as client:
async with client.post(
f'{base}/models/{hop["runtime_model"]}:{action}',
- headers={"x-goog-api-key": hop["token"]},
+ headers={"x-goog-api-key": hop["token"], **hop["request_headers"]},
json={"contents": [{"role": "user", "parts": [{"text": "hello"}]}]},
) as response:
if working_fallback:
diff --git a/tests/test_model_hub_hosted_executor.py b/tests/test_model_hub_hosted_executor.py
index 5207ad752..d03311ec3 100644
--- a/tests/test_model_hub_hosted_executor.py
+++ b/tests/test_model_hub_hosted_executor.py
@@ -547,7 +547,7 @@ async def test_a_turn_owns_its_proxied_calls(tmp_path):
async with aiohttp.ClientSession() as client:
pending = asyncio.create_task(client.post(
f'{hop["base_url"]}/hosted-tools/web_search',
- headers={"Authorization": f'Bearer {hop["token"]}'}, data=body,
+ headers={"Authorization": f'Bearer {hop["token"]}', **hop["request_headers"]}, data=body,
))
await asyncio.wait_for(blocked.started.wait(), timeout=5)
finished = []
@@ -559,7 +559,7 @@ async def test_a_turn_owns_its_proxied_calls(tmp_path):
# The turn no longer admits work: a new call is refused before anything runs.
async with client.post(
f'{hop["base_url"]}/hosted-tools/web_search',
- headers={"Authorization": f'Bearer {hop["token"]}'}, data=body,
+ headers={"Authorization": f'Bearer {hop["token"]}', **hop["request_headers"]}, data=body,
) as response:
assert response.status == 409
assert (await response.json())["error"]["code"] == "executor_not_bound"
@@ -640,7 +640,7 @@ async def test_a_turn_keeps_its_hosted_binding_until_the_next_turn(tmp_path):
async with aiohttp.ClientSession() as client:
async with client.post(
f'{again["base_url"]}/hosted-tools/web_search',
- headers={"Authorization": f'Bearer {again["token"]}'},
+ headers={"Authorization": f'Bearer {again["token"]}', **again["request_headers"]},
data=json.dumps({"arguments": {"query": "q"}, "executor": again["hosted_tools"]["web_search"]["executor"]}),
) as response:
assert response.status == 200
@@ -714,7 +714,9 @@ async def test_a_call_cancelled_before_its_handle_is_still_metered(tmp_path):
await service.run_hosted_tool(
"web_search",
{"arguments": {"query": "q"}, "executor": {"source_id": XAI, "model_id": "grok-4.6"}},
- bindings=gateway.correlation.hosted_executor_bindings(backend="vibey", token=hop["token"])[1],
+ bindings=gateway.correlation.hosted_executor_bindings(
+ backend="vibey", token=hop["token"], request_metadata={"avibe_turn_id": "turn-cancel"},
+ )[1],
)
finally:
await gateway.close()
diff --git a/tests/test_model_hub_hosted_pinning.py b/tests/test_model_hub_hosted_pinning.py
index a96abee40..8dc03f351 100644
--- a/tests/test_model_hub_hosted_pinning.py
+++ b/tests/test_model_hub_hosted_pinning.py
@@ -59,7 +59,7 @@ def _verify(service, source, model_id, tool, aspect, *, engine=None):
async def _post(hop, body, *, pin=None, model=None):
- headers = {"Authorization": f'Bearer {hop["token"]}'}
+ headers = {"Authorization": f'Bearer {hop["token"]}', **hop["request_headers"]}
if pin is not None:
headers[PIN] = pin
if model is not None:
@@ -351,7 +351,7 @@ async def test_a_pinned_hop_must_speak_the_requests_protocol(tmp_path):
async with aiohttp.ClientSession() as client:
async with client.post(
f'{hop["base_url"]}/messages',
- headers={"Authorization": f'Bearer {hop["token"]}', PIN: primary.id},
+ headers={"Authorization": f'Bearer {hop["token"]}', **hop["request_headers"], PIN: primary.id},
json={"model": hop["runtime_model"], "max_tokens": 64, "messages": [{"role": "user", "content": "q"}]},
) as response:
assert (response.status, await response.json()) == (409, PIN_UNAVAILABLE)
diff --git a/tests/test_model_hub_vibey_consumer.py b/tests/test_model_hub_vibey_consumer.py
index 15a778246..81b567b65 100644
--- a/tests/test_model_hub_vibey_consumer.py
+++ b/tests/test_model_hub_vibey_consumer.py
@@ -258,6 +258,7 @@ async def test_failover_strips_opaque_history_only_when_origin_changes(tmp_path,
f'{hop["base_url"]}/{PROTOCOL_ENDPOINTS[protocol]}',
headers={
"Authorization": f'Bearer {hop["token"]}',
+ **hop["request_headers"],
"anthropic-beta": "fixture-beta",
"User-Agent": "fixture-agent",
},
@@ -300,16 +301,17 @@ async def test_opaque_history_uses_launch_snapshot_without_requiring_turn_attrib
try:
if origin_state == "unverified":
base, token = await gateway.endpoint("vibey", process_scope="vibey:unverified")
- base_url, model = f"{base}/v1", "menu-alias"
+ base_url, model, request_headers = f"{base}/v1", "menu-alias", {}
else:
hop = await router.resolve_hop("menu-alias", process_scope="vibey:snapshot", turn_id=turn_id)
base_url, model, token = hop["base_url"], hop["runtime_model"], hop["token"]
+ request_headers = hop["request_headers"]
if origin_state == "changed":
service.store.config.sources[0].vendor = "custom"
async with aiohttp.ClientSession() as client:
async with client.post(
f"{base_url}/messages",
- headers={"Authorization": f"Bearer {token}"},
+ headers={"Authorization": f"Bearer {token}", **request_headers},
json={"model": model, **signed},
) as response:
assert response.status == 200
@@ -358,7 +360,7 @@ async def test_opaque_history_origin_survives_recovery_walks(tmp_path, recovered
async with aiohttp.ClientSession() as client:
async with client.post(
f'{hop["base_url"]}/messages',
- headers={"Authorization": f'Bearer {hop["token"]}'},
+ headers={"Authorization": f'Bearer {hop["token"]}', **hop["request_headers"]},
json={"model": hop["runtime_model"], **signed},
) as response:
assert response.status == 200
@@ -463,7 +465,7 @@ async def test_sequential_vibey_retry_replaces_route_and_retains_served_provenan
async with aiohttp.ClientSession() as client:
async with client.post(
f'{initial["base_url"]}/messages',
- headers={"Authorization": f'Bearer {initial["token"]}'},
+ headers={"Authorization": f'Bearer {initial["token"]}', **initial["request_headers"]},
json={"model": initial["runtime_model"], "stream": True, "messages": []},
) as response:
assert response.status == 200
@@ -472,7 +474,7 @@ async def test_sequential_vibey_retry_replaces_route_and_retains_served_provenan
assert retry["source_id"] == fallback.id
async with client.post(
f'{retry["base_url"]}/responses',
- headers={"Authorization": f'Bearer {retry["token"]}'},
+ headers={"Authorization": f'Bearer {retry["token"]}', **retry["request_headers"]},
json={"model": retry["runtime_model"], "input": "retry"},
) as response:
assert response.status == 200
@@ -485,7 +487,7 @@ async def test_sequential_vibey_retry_replaces_route_and_retains_served_provenan
))
async with client.post(
f'{initial["base_url"]}/messages',
- headers={"Authorization": f'Bearer {initial["token"]}'},
+ headers={"Authorization": f'Bearer {initial["token"]}', **initial["request_headers"]},
**(
{"data": "invalid json"} if late_request == "malformed"
else {"json": {"model": initial["runtime_model"], "messages": []}}
@@ -532,7 +534,7 @@ async def test_a_vibey_sessions_turns_share_its_scope_and_each_keeps_its_provena
tokens += [preflight["token"], later["token"]]
async with client.post(
f'{later["base_url"]}/messages',
- headers={"Authorization": f'Bearer {later["token"]}'},
+ headers={"Authorization": f'Bearer {later["token"]}', **later["request_headers"]},
json={"model": later["runtime_model"], "messages": []},
) as response:
assert response.status == 200
@@ -548,6 +550,64 @@ async def test_a_vibey_sessions_turns_share_its_scope_and_each_keeps_its_provena
await gateway.close()
assert len(set(tokens)) == 1
+async def test_a_finished_turns_request_is_never_credited_to_the_next_turn_on_its_route(tmp_path):
+ """MH-VIBEY-009: a Vibey request names its Turn, so a finished Turn's late request reaches no successor.
+
+ The Session's next Turn reuses the route credential. Only the Turn the
+ request names may own it, for a model call and for a proxied hosted call.
+ """
+ primary = _source("src_primary01", "Primary", vendor="anthropic", protocol="anthropic")
+ service = _vibey_service(tmp_path, [primary], handles=[
+ LiveInvokeHandle(_outcome(RawOutcomeKind.SUCCESS, source_id=primary.id), (b"{}",)),
+ # The late request's refusal; a request error never changes Source health.
+ LiveInvokeHandle(_outcome(
+ RawOutcomeKind.HTTP_ERROR, status=400, code="invalid_request_error", source_id=primary.id,
+ ), ()),
+ LiveInvokeHandle(_outcome(RawOutcomeKind.SUCCESS, source_id=primary.id), (b"{}",)),
+ ])
+ # On, with no executor: a call its live Turn owns answers a failed record; any other call is not bound.
+ service.store.config.hosted_tools["web_search"] = ModelHubHostedToolConfig(enabled=True, executor=None)
+ gateway = ModelHubTurnGateway(service)
+ router = ModelHubRuntimeRouter(service=service, turn_gateway=gateway)
+
+ async def post(client, hop, turn_headers, path, body):
+ async with client.post(
+ f'{hop["base_url"]}/{path}',
+ headers={"Authorization": f'Bearer {hop["token"]}', **turn_headers},
+ json=body,
+ ) as response:
+ return response.status, await response.json(content_type=None)
+
+ search = {"arguments": {"query": "q"}, "executor": None}
+ try:
+ async with aiohttp.ClientSession() as client:
+ finished = await router.resolve_hop("menu-alias", process_scope="vibey:session", turn_id="turn-finished")
+ message = {"model": finished["runtime_model"], "messages": []}
+ assert (await post(client, finished, finished["request_headers"], "messages", message))[0] == 200
+ completion = router.settle_turn("turn-finished", settled_by=SETTLED_BY_TERMINAL_RESULT, ts=NOW.isoformat())
+ if completion:
+ await completion
+ live = await router.resolve_hop("menu-alias", process_scope="vibey:session", turn_id="turn-live")
+ assert live["token"] == finished["token"]
+ stale = finished["request_headers"]
+ assert (await post(client, live, stale, "messages", message))[0] == 400
+ status, answer = await post(client, live, stale, "hosted-tools/web_search", search)
+ assert (status, answer["error"]["code"]) == (409, "executor_not_bound")
+ status, answer = await post(client, live, live["request_headers"], "hosted-tools/web_search", search)
+ assert status == 200 and answer["record"]["status"] == "failed"
+ assert (await post(client, live, live["request_headers"], "messages", message))[0] == 200
+ completion = router.settle_turn("turn-live", settled_by=SETTLED_BY_TERMINAL_RESULT, ts=NOW.isoformat())
+ if completion:
+ await completion
+ record = service.provenance.get("turn-live")
+ assert record is not None and record["outcome"] == "served"
+ assert record["failed_attempts"] == [] and record["terminal_error"] is None
+ _validate("turn-provenance.schema.json", record)
+ finally:
+ await gateway.close()
+
+
@pytest.mark.asyncio
async def test_vibey_route_replacement_refuses_overlap_without_poisoning_current_request(tmp_path):
"""MH-VIBEY-006: a new launch cannot take an in-flight request's turn identity."""
@@ -574,7 +634,7 @@ async def test_vibey_route_replacement_refuses_overlap_without_poisoning_current
async with aiohttp.ClientSession() as client:
pending = asyncio.create_task(client.post(
f'{first["base_url"]}/messages',
- headers={"Authorization": f'Bearer {first["token"]}'},
+ headers={"Authorization": f'Bearer {first["token"]}', **first["request_headers"]},
json={"model": first["runtime_model"], "messages": []},
))
try:
@@ -1128,7 +1188,7 @@ async def test_failover_origin_is_available_before_body_and_matches_served_attem
async with aiohttp.ClientSession() as client:
async with client.post(
f'{hop["base_url"]}/{PROTOCOL_ENDPOINTS[protocol]}',
- headers={"Authorization": f'Bearer {hop["token"]}'},
+ headers={"Authorization": f'Bearer {hop["token"]}', **hop["request_headers"]},
json={"model": hop["runtime_model"], "stream": stream, "messages": []},
) as response:
assert response.status == 200, await response.text() if response.status != 200 else ""
@@ -1268,7 +1328,9 @@ async def test_terminal_response_origin_follows_its_carrier(
if backend == "vibey":
protocol = launch.protocol
headers = {"Authorization": f"Bearer {launch.gateway_token}"}
- if launch.gateway_request_metadata:
+ if backend == "vibey":
+ headers.update(launch.gateway_request_metadata)
+ elif launch.gateway_request_metadata:
headers["x-codex-turn-metadata"] = json.dumps(launch.gateway_request_metadata)
async with aiohttp.ClientSession() as client:
async with client.post(
@@ -1353,7 +1415,7 @@ async def test_origin_header_limit_is_checked_before_each_vibey_hop(tmp_path, mo
async with aiohttp.ClientSession() as client:
async with client.post(
f'{hop["base_url"]}/{PROTOCOL_ENDPOINTS[hop["protocol"]]}',
- headers={"Authorization": f'Bearer {hop["token"]}'},
+ headers={"Authorization": f'Bearer {hop["token"]}', **hop["request_headers"]},
json={"model": hop["runtime_model"], "stream": False, "messages": []},
) as response:
if allowed:
@@ -1413,7 +1475,7 @@ async def test_slow_anthropic_resolution_waits_to_publish_origin_and_keeps_sourc
hop = await router.resolve_hop("menu-alias", process_scope="vibey:test", turn_id="turn-slow")
async with aiohttp.ClientSession() as client:
task = asyncio.create_task(client.post(
- f'{hop["base_url"]}/messages', headers={"x-api-key": hop["token"]},
+ f'{hop["base_url"]}/messages', headers={"x-api-key": hop["token"], **hop["request_headers"]},
json={"model": hop["runtime_model"], "stream": True, "messages": []},
))
await asyncio.wait_for(entered.wait(), 2)
@@ -1479,7 +1541,7 @@ async def test_concurrent_responses_keep_their_own_origin(tmp_path):
async def read(hop):
async with client.post(
f'{hop["base_url"]}/{PROTOCOL_ENDPOINTS[hop["protocol"]]}',
- headers={"Authorization": f'Bearer {hop["token"]}'},
+ headers={"Authorization": f'Bearer {hop["token"]}', **hop["request_headers"]},
json={"model": hop["runtime_model"], "stream": True, "messages": []},
) as response:
assert response.status == 200
diff --git a/tests/test_vibey_agent.py b/tests/test_vibey_agent.py
index 5cf299cf1..77f3fe6a7 100644
--- a/tests/test_vibey_agent.py
+++ b/tests/test_vibey_agent.py
@@ -1295,8 +1295,15 @@ async def test_model_hub_records_a_source_stall_as_the_backend_reported_it(engin
harness.controller.model_hub_runtime = router
request = harness.request("write the page")
turn_id = _turn(request.context)
- token = registry.credentials("vibey", f"vibey:{harness.session_id}", turn_id)
- registry.begin_gateway_request(backend="vibey", token=token, requested_model_id="test-model")
+ token = registry.prepare_gateway_turn(
+ backend="vibey", token=registry.credentials("vibey", f"vibey:{harness.session_id}", turn_id),
+ turn_id=turn_id, requested_model_id="test-model", resolved_model_id="test-model",
+ source_id="src_stalled01", via_mapping=False,
+ )
+ # A Vibey gateway request names its Turn.
+ registry.begin_gateway_request(
+ backend="vibey", token=token, requested_model_id="test-model", request_metadata={"avibe_turn_id": turn_id},
+ )
registry.begin_attempt(
turn_id, source_id="src_stalled01", resolved_model_id="test-model", channel="hub", via_mapping=False
)