0ef5fcb1c5
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 / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (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 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
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 / test-dashboard-ui (push) Has been cancelled
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 / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
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) Has been cancelled
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) Has been cancelled
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) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
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) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
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) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
95 lines
3.7 KiB
Python
95 lines
3.7 KiB
Python
"""Image compression offload (perf): the Anthropic and OpenAI handlers must run the
|
|
CPU-bound `ImageCompressor.compress()` on the bounded compression executor, not inline
|
|
on the event loop, and fail open if it raises — matching the text-compression path.
|
|
|
|
Mirrors test_gemini_compression_offload.py. The wiring (each image block awaits
|
|
`_run_compression_in_executor(lambda: compressor.compress(...))`) reuses the proven
|
|
text path; these tests assert the observable properties that wiring delivers: the
|
|
blocks are async + offloaded + fail open, and the executor keeps the loop responsive
|
|
while a slow compression runs on a worker thread.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import inspect
|
|
import threading
|
|
import time
|
|
|
|
from headroom.proxy.handlers.anthropic import AnthropicHandlerMixin
|
|
from headroom.proxy.handlers.openai import OpenAIHandlerMixin
|
|
from headroom.proxy.server import ProxyConfig, create_app
|
|
|
|
|
|
def _make_proxy(): # noqa: ANN202 — returns the internal HeadroomProxy
|
|
app = create_app(
|
|
ProxyConfig(
|
|
optimize=True,
|
|
cache_enabled=False,
|
|
rate_limit_enabled=False,
|
|
cost_tracking_enabled=False,
|
|
)
|
|
)
|
|
return app.state.proxy
|
|
|
|
|
|
def test_image_blocks_offload_compress_and_fail_open() -> None:
|
|
"""Each image-compress block must be async, offload compress() onto the executor with a
|
|
timeout, and fail open. Guards a future refactor from silently re-inlining compress()."""
|
|
for mixin, method in (
|
|
(AnthropicHandlerMixin, "handle_anthropic_messages"),
|
|
(OpenAIHandlerMixin, "handle_openai_chat"),
|
|
):
|
|
fn = getattr(mixin, method)
|
|
assert inspect.iscoroutinefunction(fn), f"{method} must be async to await the offload"
|
|
src = inspect.getsource(fn)
|
|
assert "compressor.compress(" in src, f"{method}: image compress call missing"
|
|
assert "_run_compression_in_executor(" in src, f"{method}: compress not offloaded"
|
|
assert "COMPRESSION_TIMEOUT_SECONDS" in src, f"{method}: offload missing a timeout"
|
|
assert "Image compression failed" in src, f"{method}: image compress not fail-open"
|
|
|
|
|
|
async def test_image_compress_offload_runs_on_worker_thread() -> None:
|
|
"""The exact call the image blocks make — _run_compression_in_executor(lambda: compress()) —
|
|
runs compress() on a 'headroom-compress' executor thread, not the event-loop thread."""
|
|
proxy = _make_proxy()
|
|
loop_thread = threading.current_thread().name
|
|
seen: dict[str, str] = {}
|
|
|
|
def _slow_compress(): # noqa: ANN202 — stands in for ImageCompressor.compress
|
|
seen["thread"] = threading.current_thread().name
|
|
time.sleep(0.1)
|
|
return [{"role": "user", "content": "compressed"}]
|
|
|
|
result = await proxy._run_compression_in_executor(_slow_compress, timeout=10)
|
|
|
|
assert result == [{"role": "user", "content": "compressed"}]
|
|
assert seen["thread"].startswith("headroom-compress")
|
|
assert seen["thread"] != loop_thread
|
|
|
|
|
|
async def test_image_compress_offload_keeps_event_loop_responsive() -> None:
|
|
"""While a slow image compression runs on the executor, the loop keeps scheduling
|
|
coroutines. The bug this fixes — a bare inline compress() — would starve them to ~0 ticks."""
|
|
proxy = _make_proxy()
|
|
ticks = 0
|
|
|
|
async def _ticker() -> None:
|
|
nonlocal ticks
|
|
while True:
|
|
await asyncio.sleep(0.01)
|
|
ticks += 1
|
|
|
|
def _slow_compress(): # noqa: ANN202
|
|
time.sleep(0.3)
|
|
return "x"
|
|
|
|
tick_task = asyncio.create_task(_ticker())
|
|
try:
|
|
result = await proxy._run_compression_in_executor(_slow_compress, timeout=10)
|
|
finally:
|
|
tick_task.cancel()
|
|
|
|
assert result == "x"
|
|
assert ticks >= 5 # ~30 expected at 10ms over 0.3s; a blocked loop yields near zero
|