litellm/tests/integration/routing/test_prompt_cache_pin_ttl.py
devin-ai-integration[bot] e136e91273
Some checks are pending
CodSpeed Benchmarks / benchmarks (push) Waiting to run
Deploy main to gateway-dev / dispatch (push) Waiting to run
Publish lint base counts / publish (basedpyright, scripts/type_check_gate.py) (push) Waiting to run
Publish lint base counts / publish (openapi-docs, scripts/openapi_docs_gate.py) (push) Waiting to run
Publish lint base counts / publish (ruff-strict, scripts/ruff_strict_gate.py) (push) Waiting to run
Publish lint base counts / publish (test-quality, scripts/test_quality_gate.py) (push) Waiting to run
Publish lint base counts / publish (type-discipline, scripts/type_discipline_gate.py) (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
unit / lens-python-310 (push) Blocked by required conditions
unit / caching-local (push) Blocked by required conditions
unit / core-utils (push) Blocked by required conditions
unit / endpoints (push) Blocked by required conditions
unit / integrations (push) Blocked by required conditions
unit / llms-bedrock (push) Blocked by required conditions
unit / llms-providers (push) Blocked by required conditions
unit / mcp-elicitation (push) Blocked by required conditions
unit / proxy-feature-endpoints (push) Blocked by required conditions
unit / router (push) Blocked by required conditions
unit / endpoints-and-responses (push) Blocked by required conditions
unit / enterprise-routing (push) Blocked by required conditions
unit / guardrails-hooks (push) Blocked by required conditions
unit / logging-misc (push) Blocked by required conditions
unit / root (push) Blocked by required conditions
unit / proxy-utils (push) Blocked by required conditions
unit / assert-shard-coverage (push) Waiting to run
unit / enterprise-managed-files (push) Blocked by required conditions
unit / proxy-server (push) Blocked by required conditions
unit / rust-bridge (push) Waiting to run
unit / helm (push) Waiting to run
unit / OpenAI 2.20.0 compatibility (push) Waiting to run
unit / OpenAI 3.0.0 compatibility (push) Waiting to run
unit / coverage (push) Blocked by required conditions
unit / unit passed (push) Blocked by required conditions
LiteLLM Rust / rust-lint (push) Waiting to run
LiteLLM Rust / rust-test (push) Waiting to run
LiteLLM Rust / rust-wheel (push) Waiting to run
Terraform Provider / gofmt, vet, build, test (push) Waiting to run
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Waiting to run
unit / enterprise-package (push) Blocked by required conditions
unit / enterprise-repositories-secrets (push) Blocked by required conditions
unit / llms-anthropic (push) Blocked by required conditions
unit / llms-openai-meta (push) Blocked by required conditions
unit / llms-vertex-ai (push) Blocked by required conditions
unit / mcp-server (push) Blocked by required conditions
unit / proxy-auth (push) Blocked by required conditions
unit / proxy-endpoints (push) Blocked by required conditions
unit / proxy-extras (push) Blocked by required conditions
unit / proxy-hooks-client (push) Blocked by required conditions
unit / proxy-infra (push) Blocked by required conditions
unit / proxy-infra-root (push) Blocked by required conditions
unit / responses-caching-types (push) Blocked by required conditions
unit / rust-bridge-harness (push) Blocked by required conditions
unit / auth-checks (push) Blocked by required conditions
unit / budgets (push) Blocked by required conditions
unit / custom-logging (push) Blocked by required conditions
unit / db-and-spend (push) Blocked by required conditions
unit / jwt-and-keys (push) Blocked by required conditions
unit / key-generation (push) Blocked by required conditions
unit / mcp-oauth (push) Blocked by required conditions
unit / proxy-runtime (push) Blocked by required conditions
unit / proxy-server-core (push) Blocked by required conditions
unit / ui-unit (push) Waiting to run
unit / docs (push) Waiting to run
unit / OpenAI 3.25.0 compatibility (push) Waiting to run
unit / redis-py 5.3.1 compatibility (push) Blocked by required conditions
unit / redis-py 6.4.0 compatibility (push) Blocked by required conditions
unit / redis-py 7.4.1 compatibility (push) Blocked by required conditions
unit / redis-py 8.0.1 compatibility (push) Blocked by required conditions
GitHub Actions Security Analysis / zizmor (push) Waiting to run
fix(router): keep the prompt caching affinity pin alive as long as the prefix's cache_control ttl (#46084)
* fix(router): keep the prompt caching affinity pin alive as long as the prefix's cache_control ttl

A prefix marked cache_control ttl 1h lost its pinned deployment after the fixed 300 s, so the next request went to another deployment and paid a cold cache. The pin TTL now follows the longest ephemeral breakpoint in the cacheable prefix, tools included: 3600 s for a 1h breakpoint and 300 s otherwise.

* fix(router): give prompt cache pins their own tier and honour any declared ttl

Pins now live in a DualCache of their own instead of the router's 200-slot
in-memory tier, so an hour-long pin no longer evicts cooldowns and rate windows.
The pin lifetime follows any <n>s|m|h cache_control ttl, floored at 300 s, not
only the literal "1h".

* fix(router): hash the pin ttl off the event loop and flush prompt cache pins with the router cache

Compute the cache_control ttl scan through offload_token_count beside the
prefix hash, clear the dedicated pin cache in Router.flush_cache, and point
the order-fallback and priority-group pin tests at the pin cache they read

* fix(router): pin 1h prefixes only on 1h-capable models, cap the pin at 1h, and give pins 10k in-memory slots

* fix(router): match the 1h pin gate to what Bedrock forwards

Bedrock deployments now decide the 1h pin with is_claude_4_5_on_bedrock,
the same check that decides whether the 1h ttl reaches Bedrock, so an
application inference profile ARN keeps a 5 minute pin whatever its
base_model prices

* test(router): give the anthropic_messages pin fixture its provider

* fix(router): let each pin write set its own lifetime and require a positive 1h price

In a group mixing 1h and 5m deployments the in-memory pin kept the lifetime of its first write, so a 5m deployment could hold an hour-long pin. Each write now clears the in-memory entry first, matching Redis. The Bedrock gate uses supports_bedrock_prompt_cache_ttl after #46058, and a zero 1h price no longer counts as 1h support.

* test(router): integration cells for the one-hour prompt cache pin lifetime

Cover every surface the pin lifetime touches: chat and /v1/messages through
both SDKs, the responses bridge, response-cache hits, each provider family's
1h gate, malformed and duplicated ttl values, rewrites of one prefix, mixed
and cross-group routing, concurrency, every Redis attach path, the in-memory
slot pressure without Redis reads, a Redis outage and a killed worker

* fix(caching): bound the in-memory expiry heap and count prompt cache pins in the memory debug routes

* test(integration): cover prompt cache pins in the memory debug routes and the bounded expiration heap

* fix(caching): serialize in-memory cache writes so a heap rebuild cannot lose another thread's expiry entry

* fix(caching): keep the in-memory cache bounded when entries have no usable expiry

* test(caching): cover the response cache bound when cached entries have no usable ttl

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-14 21:10:09 -07:00

646 lines
26 KiB
Python

import asyncio
import json
from collections.abc import Iterator, Mapping, Sequence
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from pathlib import Path
from typing import Final
import anthropic
import openai
import pytest
from integration._support.client import eventually, gateway_from_environment, held
from integration._support.process import OwnedProxy, graceful_stop_seconds, owned_proxy_process
from integration._support.redis_process import owned_redis
from integration._support.wire import Wire, wire_server
from integration.routing._prompt_cache_pin_support import (
BEDROCK_FIVE_MINUTE_MODEL,
BEDROCK_ONE_HOUR_MODEL,
DEFAULT_MARK,
FIVE_MINUTE_MARK,
INFERENCE_PROFILE_ARN,
ONE_HOUR_MARK,
OVERLOAD_TOKEN,
PROVIDER_KEY,
Pin,
anthropic_deployment,
ask_turn,
bedrock_deployment,
bedrock_runtime_peer,
chat_body,
claude_peer,
deployment,
end_user_row_lands,
follow_up,
lives_five_minutes,
lives_one_hour,
marked,
marker_of,
new_marker,
no_pin_written,
one_new_pin,
openai_peer,
pin_at,
pin_config,
pins_since,
post_chat,
question,
read_pins,
reference,
sent_with,
slow_claude_peer,
spend_row_lands,
spend_rows,
system_turn,
vertex_deployment,
vertex_peer,
wire_carries_one_hour,
)
from pydantic import JsonValue
from redis import Redis
pytestmark: Final = pytest.mark.timeout(2 * graceful_stop_seconds() + 300)
_CLAUDE: Final = "pin-claude"
_SLOW: Final = "pin-slow"
_MIXED: Final = "pin-mixed"
_OPENAI: Final = "pin-openai"
_LEFT: Final = "pin-claude-left"
_RIGHT: Final = "pin-claude-right"
_PRIMARY: Final = "pin-mixed-primary"
_SECONDARY: Final = "pin-mixed-secondary"
_MALFORMED_TTLS: Final[tuple[JsonValue, ...]] = (3600, "2h", "1H", "", None, ["1h"], "1h" * 2500)
@dataclass(frozen=True, slots=True)
class _Family:
group: str
identity: str
one_hour: bool
wire_one_hour: bool | None
_FAMILIES: Final = (
_Family("pin-bedrock-converse-1h", "pin-bedrock-converse-1h", one_hour=True, wire_one_hour=True),
_Family("pin-bedrock-converse-5m", "pin-bedrock-converse-5m", one_hour=False, wire_one_hour=False),
_Family("pin-bedrock-invoke-1h", "pin-bedrock-invoke-1h", one_hour=True, wire_one_hour=True),
_Family("pin-bedrock-arn-base", "pin-bedrock-arn-base", one_hour=False, wire_one_hour=False),
_Family("pin-bedrock-arn", "pin-bedrock-arn", one_hour=False, wire_one_hour=False),
_Family("pin-azure-ai", "pin-azure-ai", one_hour=True, wire_one_hour=True),
_Family("pin-vertex", "pin-vertex", one_hour=True, wire_one_hour=True),
_Family(_OPENAI, "pin-openai", one_hour=False, wire_one_hour=None),
_Family("pin-unmapped", "pin-unmapped", one_hour=False, wire_one_hour=True),
_Family("pin-zero-one-hour-price", "pin-zero-one-hour-price", one_hour=False, wire_one_hour=None),
)
@dataclass(frozen=True, slots=True)
class _Rig:
owned: OwnedProxy
redis: Redis
left: Wire
right: Wire
misc: Wire
bedrock: Wire
openai: Wire
primary: Wire
slow: Wire
def wires(self) -> tuple[Wire, ...]:
return (self.left, self.right, self.misc, self.bedrock, self.openai, self.primary, self.slow)
def base_url(self) -> str:
return str(self.owned.gateway.client.base_url).rstrip("/")
def served_by(self, marker: str) -> str:
identities: Final = {_LEFT: self.left, _RIGHT: self.right}
found: Final = [identity for identity, wire in identities.items() if sent_with((wire,), marker)]
assert len(found) == 1, found
return found[0]
def _second_in_order(entry: Mapping[str, JsonValue]) -> dict[str, JsonValue]:
params: Final = entry["litellm_params"]
assert isinstance(params, dict)
return {**entry, "litellm_params": {**params, "order": 2}}
def _model_list(rig_wires: Mapping[str, str]) -> list[JsonValue]:
bedrock: Final = rig_wires["bedrock"]
misc: Final = rig_wires["misc"]
return [
anthropic_deployment(_CLAUDE, _LEFT, rig_wires["left"]),
anthropic_deployment(_CLAUDE, _RIGHT, rig_wires["right"]),
anthropic_deployment(_SLOW, "pin-slow-left", rig_wires["slow"]),
anthropic_deployment(_SLOW, "pin-slow-right", rig_wires["slow"]),
anthropic_deployment(_MIXED, _PRIMARY, rig_wires["primary"], order=1),
_second_in_order(
bedrock_deployment(_MIXED, _SECONDARY, f"bedrock/converse/{BEDROCK_FIVE_MINUTE_MODEL}", bedrock)
),
bedrock_deployment(
"pin-bedrock-converse-1h", "pin-bedrock-converse-1h", f"bedrock/converse/{BEDROCK_ONE_HOUR_MODEL}", bedrock
),
bedrock_deployment(
"pin-bedrock-converse-5m",
"pin-bedrock-converse-5m",
f"bedrock/converse/{BEDROCK_FIVE_MINUTE_MODEL}",
bedrock,
),
bedrock_deployment(
"pin-bedrock-invoke-1h", "pin-bedrock-invoke-1h", f"bedrock/invoke/{BEDROCK_ONE_HOUR_MODEL}", bedrock
),
bedrock_deployment(
"pin-bedrock-arn-base",
"pin-bedrock-arn-base",
f"bedrock/converse/{INFERENCE_PROFILE_ARN}",
bedrock,
model_info={"base_model": f"bedrock/{BEDROCK_ONE_HOUR_MODEL}"},
),
bedrock_deployment("pin-bedrock-arn", "pin-bedrock-arn", f"bedrock/converse/{INFERENCE_PROFILE_ARN}", bedrock),
deployment(
"pin-azure-ai", "pin-azure-ai", model="azure_ai/claude-opus-5-5", api_base=misc, api_key=PROVIDER_KEY
),
vertex_deployment("pin-vertex", "pin-vertex", misc),
deployment(
_OPENAI, "pin-openai", model="openai/gpt-5.6", api_base=f"{rig_wires['openai']}/v1", api_key=PROVIDER_KEY
),
deployment(
"pin-unmapped",
"pin-unmapped",
model="anthropic/claude-pin-model-the-cost-map-lacks",
api_base=misc,
api_key=PROVIDER_KEY,
),
deployment(
"pin-zero-one-hour-price",
"pin-zero-one-hour-price",
model="sarvam/sarvam-m",
api_base=f"{rig_wires['openai']}/v1",
api_key=PROVIDER_KEY,
),
]
@pytest.fixture(scope="module")
def rig(tmp_path_factory: pytest.TempPathFactory) -> Iterator[_Rig]:
directory: Final = tmp_path_factory.mktemp("prompt-cache-pin-ttl")
with (
gateway_from_environment() as gateway,
owned_redis(directory) as cache,
wire_server(claude_peer) as left,
wire_server(claude_peer) as right,
wire_server(vertex_peer) as misc,
wire_server(bedrock_runtime_peer) as bedrock,
wire_server(openai_peer) as openai_wire,
wire_server(claude_peer) as primary,
wire_server(slow_claude_peer) as slow,
):
urls: Final = {
"left": left.url,
"right": right.url,
"misc": misc.url,
"bedrock": bedrock.url,
"openai": openai_wire.url,
"primary": primary.url,
"slow": slow.url,
}
config: Final = pin_config(directory, _model_list(urls))
with (
owned_proxy_process(
gateway,
directory,
{"REDIS_HOST": cache.host, "REDIS_PORT": str(cache.port)},
config=config,
workers=2,
) as owned,
Redis(host=cache.host, port=cache.port, socket_timeout=2) as redis,
):
yield _Rig(owned, redis, left, right, misc, bedrock, openai_wire, primary, slow)
def _before(rig: _Rig) -> frozenset[str]:
for wire in rig.wires():
wire.drain()
return frozenset(read_pins(rig.redis))
def _first_turn(session: str, marker: str, mark: JsonValue) -> list[JsonValue]:
return [system_turn(session, mark), ask_turn(marker)]
def _breakpoint_shapes(session: str, marker: str) -> Mapping[str, list[JsonValue]]:
return {
"system-1h": _first_turn(session, marker, ONE_HOUR_MARK),
"system-5m": _first_turn(session, marker, FIVE_MINUTE_MARK),
"system-default-ttl": _first_turn(session, marker, DEFAULT_MARK),
"system-1h-then-turn-5m": [
system_turn(session, ONE_HOUR_MARK),
{"role": "user", "content": "Earlier question."},
{"role": "assistant", "content": "sunny"},
ask_turn(marker, FIVE_MINUTE_MARK),
],
"earlier-user-turn-1h": [
system_turn(session, None),
{"role": "user", "content": [marked("Earlier question.", ONE_HOUR_MARK)]},
{"role": "assistant", "content": "sunny"},
ask_turn(marker),
],
"message-level-1h-on-string-content": [
{"role": "system", "content": reference(session), "cache_control": ONE_HOUR_MARK},
ask_turn(marker),
],
}
_ONE_HOUR_SHAPES: Final = frozenset(
{"system-1h", "system-1h-then-turn-5m", "earlier-user-turn-1h", "message-level-1h-on-string-content"}
)
@pytest.mark.parametrize("shape", sorted(_breakpoint_shapes("s", "m")))
def test_chat_pin_lifetime_follows_the_longest_breakpoint_ttl_in_the_cacheable_prefix(rig: _Rig, shape: str) -> None:
before: Final = _before(rig)
marker: Final = new_marker()
response_id, served = post_chat(
rig.owned.gateway, chat_body(_CLAUDE, _breakpoint_shapes(new_marker(), marker)[shape])
)
_, pin = one_new_pin(rig.redis, before)
assert served == rig.served_by(marker) == pin.model_id, (served, pin)
assert (lives_one_hour if shape in _ONE_HOUR_SHAPES else lives_five_minutes)(pin), (shape, pin)
spend_row_lands(response_id)
def test_chat_one_hour_mark_on_a_tool_definition_alone_writes_no_pin(rig: _Rig) -> None:
before: Final = _before(rig)
marker: Final = new_marker()
tool: Final[dict[str, JsonValue]] = {
"type": "function",
"function": {"name": "lookup_weather", "parameters": {"type": "object"}},
"cache_control": ONE_HOUR_MARK,
}
response_id, served = post_chat(
rig.owned.gateway, chat_body(_CLAUDE, _first_turn(new_marker(), marker, None), tools=[tool])
)
assert served == rig.served_by(marker), served
no_pin_written(rig.redis, before, response_id)
def _openai_client(rig: _Rig) -> openai.OpenAI:
return openai.OpenAI(base_url=f"{rig.base_url()}/v1", api_key=rig.owned.gateway.key, max_retries=0)
def _openai_chat(rig: _Rig, messages: list[JsonValue], *, asynchronous: bool, stream: bool) -> str:
body: Final = chat_body(_CLAUDE, messages)
if asynchronous:
return asyncio.run(_openai_chat_async(rig, body, stream=stream))
with _openai_client(rig) as client:
if not stream:
return client.chat.completions.create(**body).id
chunks: Final = tuple(client.chat.completions.create(**body, stream=True))
return chunks[0].id
async def _openai_chat_async(rig: _Rig, body: Mapping[str, JsonValue], *, stream: bool) -> str:
async with openai.AsyncOpenAI(
base_url=f"{rig.base_url()}/v1", api_key=rig.owned.gateway.key, max_retries=0
) as client:
if not stream:
return (await client.chat.completions.create(**body)).id
chunks: Final = [chunk async for chunk in await client.chat.completions.create(**body, stream=True)]
return chunks[0].id
def _messages_body(session: str, marker: str) -> dict[str, JsonValue]:
return {
"model": _CLAUDE,
"max_tokens": 16,
"messages": [
{"role": "user", "content": [marked(reference(session), ONE_HOUR_MARK), marked(question(marker), None)]}
],
}
def _anthropic_messages(rig: _Rig, body: Mapping[str, JsonValue], *, asynchronous: bool, stream: bool) -> str:
if asynchronous:
return asyncio.run(_anthropic_messages_async(rig, body, stream=stream))
with anthropic.Anthropic(base_url=rig.base_url(), api_key=rig.owned.gateway.key, max_retries=0) as client:
if not stream:
return client.messages.create(**body).id
with client.messages.stream(**body) as events:
return events.get_final_message().id
async def _anthropic_messages_async(rig: _Rig, body: Mapping[str, JsonValue], *, stream: bool) -> str:
async with anthropic.AsyncAnthropic(
base_url=rig.base_url(), api_key=rig.owned.gateway.key, max_retries=0
) as client:
if not stream:
return (await client.messages.create(**body)).id
async with client.messages.stream(**body) as events:
return (await events.get_final_message()).id
_CLIENT_MODES: Final = ("sync", "sync-stream", "async", "async-stream")
@pytest.mark.parametrize("mode", _CLIENT_MODES)
def test_openai_sdk_chat_pins_a_one_hour_breakpoint_for_an_hour(rig: _Rig, mode: str) -> None:
before: Final = _before(rig)
marker: Final = new_marker()
response_id: Final = _openai_chat(
rig,
_first_turn(new_marker(), marker, ONE_HOUR_MARK),
asynchronous=mode.startswith("async"),
stream=mode.endswith("stream"),
)
_, pin = one_new_pin(rig.redis, before)
assert pin.model_id == rig.served_by(marker), pin
assert lives_one_hour(pin), pin
spend_row_lands(response_id)
@pytest.mark.parametrize("mode", _CLIENT_MODES)
def test_anthropic_sdk_messages_pins_a_one_hour_breakpoint_for_an_hour(rig: _Rig, mode: str) -> None:
before: Final = _before(rig)
marker: Final = new_marker()
response_id: Final = _anthropic_messages(
rig,
_messages_body(new_marker(), marker),
asynchronous=mode.startswith("async"),
stream=mode.endswith("stream"),
)
assert response_id == f"msg_{marker}", response_id
_, pin = one_new_pin(rig.redis, before)
assert pin.model_id == rig.served_by(marker), pin
assert lives_one_hour(pin), pin
def _responses_id(rig: _Rig, body: Mapping[str, JsonValue], *, stream: bool) -> str:
with _openai_client(rig) as client:
if not stream:
return client.responses.create(**body).id
events: Final = tuple(client.responses.create(**body, stream=True))
return next(event.response.id for event in events if event.type == "response.completed")
@pytest.mark.parametrize("stream", [False, True], ids=["non-stream", "stream"])
def test_responses_api_one_hour_breakpoint_answers_and_writes_no_pin(rig: _Rig, stream: bool) -> None:
before: Final = _before(rig)
marker: Final = new_marker()
user: Final = f"pin-responses-{marker}"
body: Final[dict[str, JsonValue]] = {
"model": _CLAUDE,
"max_output_tokens": 16,
"user": user,
"input": [
{
"role": "system",
"content": [{"type": "input_text", "text": reference(new_marker()), "cache_control": ONE_HOUR_MARK}],
},
{"role": "user", "content": question(marker)},
],
}
assert _responses_id(rig, body, stream=stream).startswith("resp_")
assert rig.served_by(marker) in {_LEFT, _RIGHT}
end_user_row_lands(user)
held(lambda: pins_since(rig.redis, before), lambda found: not found, holding=3, seconds=20)
def _ask_surface(rig: _Rig, surface: str, session: str, marker: str) -> str:
if surface == "messages":
return _anthropic_messages(rig, _messages_body(session, marker), asynchronous=False, stream=False)
return _openai_chat(
rig, _first_turn(session, marker, ONE_HOUR_MARK), asynchronous=False, stream=surface == "chat-stream"
)
@pytest.mark.parametrize("surface", ["chat", "chat-stream", "messages"])
def test_response_cache_hit_rewrites_the_pin_with_the_breakpoint_lifetime(rig: _Rig, surface: str) -> None:
before: Final = _before(rig)
marker: Final = new_marker()
session: Final = new_marker()
first: Final = _ask_surface(rig, surface, session, marker)
key, pin = one_new_pin(rig.redis, before)
assert lives_one_hour(pin), pin
assert len(sent_with(rig.wires(), marker)) == 1
spend_row_lands(first)
rig.redis.delete(key)
second: Final = _ask_surface(rig, surface, session, marker)
assert sent_with(rig.wires(), marker) == ()
assert second == first
rewritten: Final = eventually(lambda: pin_at(rig.redis, key), lambda found: found is not None, seconds=30)
assert rewritten is not None and rewritten.model_id in {_LEFT, _RIGHT}, rewritten
assert lives_one_hour(rewritten), rewritten
@pytest.mark.parametrize("family", _FAMILIES, ids=[family.group for family in _FAMILIES])
def test_provider_family_pin_lifetime_agrees_with_the_ttl_the_wire_carries(rig: _Rig, family: _Family) -> None:
before: Final = _before(rig)
marker: Final = new_marker()
response_id, served = post_chat(
rig.owned.gateway, chat_body(family.group, _first_turn(new_marker(), marker, ONE_HOUR_MARK))
)
_, pin = one_new_pin(rig.redis, before)
sent: Final = sent_with(rig.wires(), marker)
assert served == pin.model_id == family.identity, (served, pin)
assert lives_one_hour(pin) is family.one_hour, pin
if family.wire_one_hour is not None:
assert [wire_carries_one_hour(request) for request in sent] == [family.wire_one_hour], sent
spend_row_lands(response_id)
def test_messages_through_bedrock_invoke_pins_an_hour_and_sends_the_hour(rig: _Rig) -> None:
before: Final = _before(rig)
marker: Final = new_marker()
response: Final = rig.owned.gateway.request(
"POST", "/v1/messages", {**_messages_body(new_marker(), marker), "model": "pin-bedrock-invoke-1h"}
)
assert response.status_code == 200, response.text
_, pin = one_new_pin(rig.redis, before)
assert pin.model_id == "pin-bedrock-invoke-1h", pin
assert lives_one_hour(pin), pin
assert [wire_carries_one_hour(request) for request in sent_with((rig.bedrock,), marker)] == [True]
@pytest.mark.parametrize("ttl", _MALFORMED_TTLS, ids=["int", "2h", "upper-1H", "empty", "null", "list", "5kb"])
def test_malformed_ttl_answers_and_pins_five_minutes_without_a_wire_ttl(rig: _Rig, ttl: JsonValue) -> None:
before: Final = _before(rig)
marker: Final = new_marker()
response_id, _ = post_chat(
rig.owned.gateway,
chat_body("pin-bedrock-converse-1h", _first_turn(new_marker(), marker, {"type": "ephemeral", "ttl": ttl})),
)
_, pin = one_new_pin(rig.redis, before)
assert lives_five_minutes(pin), pin
assert [wire_carries_one_hour(request) for request in sent_with((rig.bedrock,), marker)] == [False]
spend_row_lands(response_id)
@pytest.mark.parametrize("order", [("5m", "1h"), ("1h", "5m")], ids=["5m-then-1h", "1h-then-5m"])
def test_duplicated_ttl_key_takes_the_last_value_for_pin_and_wire(rig: _Rig, order: tuple[str, str]) -> None:
before: Final = _before(rig)
marker: Final = new_marker()
placeholder: Final = new_marker()
body: Final = json.dumps(
chat_body(
"pin-bedrock-converse-1h", _first_turn(new_marker(), marker, {"type": "ephemeral", "ttl": placeholder})
)
).replace(f'"ttl": "{placeholder}"', f'"ttl": "{order[0]}", "ttl": "{order[1]}"')
response: Final = rig.owned.gateway.client.post(
"/v1/chat/completions",
content=body.encode(),
headers={"Authorization": f"Bearer {rig.owned.gateway.key}", "Content-Type": "application/json"},
)
assert response.status_code == 200, response.text
_, pin = one_new_pin(rig.redis, before)
assert lives_one_hour(pin) is (order[1] == "1h"), pin
assert [wire_carries_one_hour(request) for request in sent_with((rig.bedrock,), marker)] == [order[1] == "1h"]
@pytest.mark.parametrize(
"mark", ["ephemeral", {"type": "persistent", "ttl": "1h"}], ids=["string", "non-ephemeral-type-1h"]
)
def test_non_ephemeral_cache_control_never_pins_an_hour(rig: _Rig, mark: JsonValue) -> None:
before: Final = _before(rig)
marker: Final = new_marker()
response: Final = rig.owned.gateway.request(
"POST", "/v1/chat/completions", chat_body(_CLAUDE, _first_turn(new_marker(), marker, mark))
)
assert response.status_code == 200, response.text
no_pin_written(rig.redis, before, str(response.json()["id"]))
def test_failed_first_turn_reports_the_error_and_writes_no_pin(rig: _Rig) -> None:
before: Final = _before(rig)
response: Final = rig.owned.gateway.request(
"POST",
"/v1/chat/completions",
chat_body(_CLAUDE, _first_turn(new_marker(), f"{new_marker()} {OVERLOAD_TOKEN}", ONE_HOUR_MARK)),
)
assert response.status_code == 500, response.text
assert "overloaded_error" in response.text, response.text
healthy, _ = post_chat(rig.owned.gateway, chat_body(_CLAUDE, _first_turn(new_marker(), new_marker(), None)))
spend_row_lands(healthy)
assert pins_since(rig.redis, before) == {}
def test_one_hour_breakpoint_after_a_five_minute_one_reports_the_provider_400_and_writes_no_pin(rig: _Rig) -> None:
before: Final = _before(rig)
marker: Final = new_marker()
messages: Final = [
system_turn(new_marker(), FIVE_MINUTE_MARK),
ask_turn(marker, ONE_HOUR_MARK),
]
response: Final = rig.owned.gateway.request("POST", "/v1/chat/completions", chat_body(_CLAUDE, messages))
assert response.status_code == 400, response.text
assert "must not come after" in response.text, response.text
healthy, _ = post_chat(rig.owned.gateway, chat_body(_CLAUDE, _first_turn(new_marker(), new_marker(), None)))
spend_row_lands(healthy)
assert pins_since(rig.redis, before) == {}
@pytest.mark.parametrize(
"marks", [(FIVE_MINUTE_MARK, ONE_HOUR_MARK), (ONE_HOUR_MARK, FIVE_MINUTE_MARK)], ids=["5m-then-1h", "1h-then-5m"]
)
def test_rewriting_one_prefix_moves_its_pin_to_the_latest_lifetime(
rig: _Rig, marks: tuple[Mapping[str, JsonValue], Mapping[str, JsonValue]]
) -> None:
before: Final = _before(rig)
session: Final = new_marker()
first_id, _ = post_chat(rig.owned.gateway, chat_body(_CLAUDE, _first_turn(session, new_marker(), dict(marks[0]))))
key, first = one_new_pin(rig.redis, before)
spend_row_lands(first_id)
second_id, served = post_chat(
rig.owned.gateway, chat_body(_CLAUDE, _first_turn(session, new_marker(), dict(marks[1])))
)
spend_row_lands(second_id)
second: Final = pin_at(rig.redis, key)
assert second is not None and second.model_id == first.model_id == served, (first, second)
assert lives_one_hour(first) is (marks[0] == ONE_HOUR_MARK), first
assert lives_one_hour(second) is (marks[1] == ONE_HOUR_MARK), second
assert frozenset(pins_since(rig.redis, before)) == {key}
def test_mixed_group_pin_takes_the_lifetime_of_the_deployment_that_served_last(rig: _Rig) -> None:
before: Final = _before(rig)
session: Final = new_marker()
first_turn: Final = _first_turn(session, new_marker(), ONE_HOUR_MARK)
first_id, first_served = post_chat(rig.owned.gateway, chat_body(_MIXED, first_turn))
key, first = one_new_pin(rig.redis, before)
assert first_served == first.model_id == _PRIMARY, first
assert lives_one_hour(first), first
spend_row_lands(first_id)
marker: Final = new_marker()
second_id, second_served = post_chat(
rig.owned.gateway, chat_body(_MIXED, follow_up(first_turn, f"{marker} {OVERLOAD_TOKEN}"))
)
spend_row_lands(second_id)
assert second_served == _SECONDARY
assert [wire_carries_one_hour(request) for request in sent_with((rig.bedrock,), marker)] == [False]
second: Final = pin_at(rig.redis, key)
assert second is not None and second.model_id == _SECONDARY, second
assert lives_five_minutes(second), second
def test_same_prefix_moving_from_a_five_minute_group_to_a_one_hour_group_repins_for_an_hour(rig: _Rig) -> None:
before: Final = _before(rig)
session: Final = new_marker()
first_id, _ = post_chat(rig.owned.gateway, chat_body(_OPENAI, _first_turn(session, new_marker(), ONE_HOUR_MARK)))
key, first = one_new_pin(rig.redis, before)
assert first.model_id == "pin-openai" and lives_five_minutes(first), first
spend_row_lands(first_id)
second_id, served = post_chat(
rig.owned.gateway, chat_body(_CLAUDE, _first_turn(session, new_marker(), ONE_HOUR_MARK))
)
spend_row_lands(second_id)
second: Final = pin_at(rig.redis, key)
assert second is not None and second.model_id == served, (second, served)
assert lives_one_hour(second), second
def test_follow_ups_stick_to_the_pinned_deployment_and_keep_an_hour(rig: _Rig) -> None:
before: Final = _before(rig)
first_turn: Final = _first_turn(new_marker(), new_marker(), ONE_HOUR_MARK)
first_id, first_served = post_chat(rig.owned.gateway, chat_body(_CLAUDE, first_turn))
key, first = one_new_pin(rig.redis, before)
spend_row_lands(first_id)
follow_ups: Final = tuple(
post_chat(rig.owned.gateway, chat_body(_CLAUDE, follow_up(first_turn, new_marker()))) for _ in range(5)
)
assert [served for _, served in follow_ups] == [first_served] * 5
for response_id, _ in follow_ups:
spend_row_lands(response_id)
pin: Final = pin_at(rig.redis, key)
assert pin is not None and pin.model_id == first.model_id == first_served, pin
assert lives_one_hour(pin), pin
assert frozenset(pins_since(rig.redis, before)) == {key}
def test_concurrent_first_turns_on_one_prefix_write_one_hour_long_pin(rig: _Rig) -> None:
before: Final = _before(rig)
session: Final = new_marker()
bodies: Final = tuple(chat_body(_CLAUDE, _first_turn(session, new_marker(), ONE_HOUR_MARK)) for _ in range(8))
with ThreadPoolExecutor(max_workers=8) as pool:
answers: Final = tuple(pool.map(lambda body: post_chat(rig.owned.gateway, body), bodies))
for response_id, _ in answers:
spend_row_lands(response_id)
pins: Final = pins_since(rig.redis, before)
assert len(pins) == 1, pins
pin: Final[Pin] = next(iter(pins.values()))
assert pin.model_id in {served for _, served in answers}, pin
assert lives_one_hour(pin), pin
def test_slow_upstream_burst_writes_every_one_hour_pin(rig: _Rig) -> None:
before: Final = _before(rig)
markers: Final = tuple(new_marker() for _ in range(24))
bodies: Final = tuple(chat_body(_SLOW, _first_turn(new_marker(), marker, ONE_HOUR_MARK)) for marker in markers)
with ThreadPoolExecutor(max_workers=24) as pool:
answers: Final = tuple(pool.map(lambda body: post_chat(rig.owned.gateway, body), bodies))
for response_id, _ in answers:
spend_row_lands(response_id)
pins: Final = pins_since(rig.redis, before)
assert len(pins) == 24, pins
assert sorted(pin.model_id for pin in pins.values()) == sorted(served for _, served in answers)
assert all(lives_one_hour(pin) for pin in pins.values()), pins
assert sorted(marker_of(request) for request in rig.slow.drain()) == sorted(markers)