94b8d5d118
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Waiting to run
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Blocked by required conditions
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Blocked by required conditions
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Blocked by required conditions
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Blocked by required conditions
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Blocked by required conditions
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Waiting to run
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Blocked by required conditions
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Blocked by required conditions
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Blocked by required conditions
Docker / promote-latest (push) Blocked by required conditions
Init Native E2E / init-native (macos-latest, claude) (push) Waiting to run
Init Native E2E / init-native (macos-latest, codex) (push) Waiting to run
Init Native E2E / init-native (macos-latest, copilot) (push) Waiting to run
Install Native E2E / install-native (macos-latest) (push) Waiting to run
Wrap Native E2E / wrap-native (macos-latest) (push) Waiting to run
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / changes (push) Failing after 2s
Deploy Documentation / deploy (push) Failing after 0s
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 1s
Dev Containers / validate-worktree (push) Failing after 1s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Init E2E / docker-init-e2e (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 0s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
Release Please / release-please (push) Failing after 0s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Merge Conflicts / merge-conflicts (push) Failing after 2s
OpenCode Plugin / typecheck + build + test (push) Failing after 2s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 3s
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
170 lines
6.1 KiB
Python
170 lines
6.1 KiB
Python
"""Integration RBP for the SemanticCache key fix.
|
|
|
|
Drives the real ``/v1/messages`` handler path with the cache enabled and a mocked
|
|
upstream, proving end to end that a second request with the same messages but a
|
|
different ``system`` prompt is NOT served the first request's cached response
|
|
(no cross-request contamination), while a repeat of the first request IS served
|
|
from cache. This is the deterministic stand-in for a live real-upstream e2e
|
|
(no API credits, fully reproducible) and covers what the cache-key unit tests
|
|
cannot: that the handler actually threads the response-shaping fields into the
|
|
cache get/set calls.
|
|
|
|
Before the fix the cache key omitted ``system``, so request B collided with
|
|
request A: it returned A's response and the upstream was never called.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
pytest.importorskip("fastapi")
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from headroom.proxy.server import ProxyConfig, create_app
|
|
|
|
|
|
def _make_cached_proxy_client() -> TestClient:
|
|
config = ProxyConfig(
|
|
optimize=False,
|
|
cache_enabled=True,
|
|
rate_limit_enabled=False,
|
|
cost_tracking_enabled=False,
|
|
log_requests=False,
|
|
ccr_inject_tool=False,
|
|
ccr_handle_responses=False,
|
|
ccr_context_tracking=False,
|
|
image_optimize=False,
|
|
)
|
|
return TestClient(create_app(config))
|
|
|
|
|
|
def _body(system: str) -> dict:
|
|
return {
|
|
"model": "claude-haiku-4-5",
|
|
"max_tokens": 64,
|
|
"system": system,
|
|
"messages": [{"role": "user", "content": "Say hi."}],
|
|
"stream": False,
|
|
}
|
|
|
|
|
|
def _text(response: httpx.Response) -> str:
|
|
return response.json()["content"][0]["text"]
|
|
|
|
|
|
def test_different_system_not_served_from_cache() -> None:
|
|
calls = {"n": 0}
|
|
|
|
with _make_cached_proxy_client() as client:
|
|
proxy = client.app.state.proxy
|
|
|
|
async def _fake_retry(method, url, headers, body, stream=False, **kwargs): # noqa: ANN001
|
|
calls["n"] += 1
|
|
system = body.get("system")
|
|
sys_text = system if isinstance(system, str) else json.dumps(system)
|
|
text = "Bonjour" if "French" in sys_text else "Hello"
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "msg_1",
|
|
"type": "message",
|
|
"role": "assistant",
|
|
"content": [{"type": "text", "text": text}],
|
|
"usage": {
|
|
"input_tokens": 10,
|
|
"output_tokens": 3,
|
|
"cache_read_input_tokens": 0,
|
|
"cache_creation_input_tokens": 0,
|
|
},
|
|
},
|
|
)
|
|
|
|
proxy._retry_request = _fake_retry
|
|
headers = {"x-api-key": "test-key", "anthropic-version": "2023-06-01"}
|
|
|
|
# A: French system -> upstream call 1, cached under the French key.
|
|
a = client.post("/v1/messages", headers=headers, json=_body("Answer only in French."))
|
|
assert a.status_code == 200
|
|
assert _text(a) == "Bonjour"
|
|
assert calls["n"] == 1
|
|
|
|
# B: English system, SAME messages -> must reach the upstream again, not
|
|
# be served A's cached French response. Before the fix this returned
|
|
# "Bonjour" with calls["n"] still 1 (the bug).
|
|
b = client.post("/v1/messages", headers=headers, json=_body("Answer only in English."))
|
|
assert b.status_code == 200
|
|
assert _text(b) == "Hello"
|
|
assert calls["n"] == 2
|
|
|
|
# A again: French system -> served from cache, upstream NOT called.
|
|
a2 = client.post("/v1/messages", headers=headers, json=_body("Answer only in French."))
|
|
assert a2.status_code == 200
|
|
assert _text(a2) == "Bonjour"
|
|
assert calls["n"] == 2
|
|
|
|
|
|
def _body_thinking(thinking: dict) -> dict:
|
|
return {
|
|
"model": "claude-haiku-4-5",
|
|
"max_tokens": 64,
|
|
"system": "You are helpful.",
|
|
"messages": [{"role": "user", "content": "Say hi."}],
|
|
"thinking": thinking,
|
|
"stream": False,
|
|
}
|
|
|
|
|
|
def test_different_thinking_not_served_from_cache() -> None:
|
|
"""Same system + messages, different ``thinking`` config -> B must reach the
|
|
upstream, not be served A's cached response. ``thinking`` is the field the
|
|
#1473 review called out as still missing from the Anthropic key."""
|
|
calls = {"n": 0}
|
|
|
|
with _make_cached_proxy_client() as client:
|
|
proxy = client.app.state.proxy
|
|
|
|
async def _fake_retry(method, url, headers, body, stream=False, **kwargs): # noqa: ANN001
|
|
calls["n"] += 1
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "msg_1",
|
|
"type": "message",
|
|
"role": "assistant",
|
|
"content": [{"type": "text", "text": f"resp-{calls['n']}"}],
|
|
"usage": {
|
|
"input_tokens": 10,
|
|
"output_tokens": 3,
|
|
"cache_read_input_tokens": 0,
|
|
"cache_creation_input_tokens": 0,
|
|
},
|
|
},
|
|
)
|
|
|
|
proxy._retry_request = _fake_retry
|
|
headers = {"x-api-key": "test-key", "anthropic-version": "2023-06-01"}
|
|
enabled = {"type": "enabled", "budget_tokens": 2048}
|
|
disabled = {"type": "disabled"}
|
|
|
|
# A: thinking enabled -> upstream call 1, cached under A's key.
|
|
a = client.post("/v1/messages", headers=headers, json=_body_thinking(enabled))
|
|
assert a.status_code == 200
|
|
assert _text(a) == "resp-1"
|
|
assert calls["n"] == 1
|
|
|
|
# B: thinking disabled, SAME messages -> must reach the upstream again.
|
|
b = client.post("/v1/messages", headers=headers, json=_body_thinking(disabled))
|
|
assert b.status_code == 200
|
|
assert _text(b) == "resp-2"
|
|
assert calls["n"] == 2
|
|
|
|
# A again -> served from cache, upstream NOT called.
|
|
a2 = client.post("/v1/messages", headers=headers, json=_body_thinking(enabled))
|
|
assert a2.status_code == 200
|
|
assert _text(a2) == "resp-1"
|
|
assert calls["n"] == 2
|