Files
chopratejas--headroom/headroom/proxy/image_compression_decision.py
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 12:03:20 +08:00

117 lines
4.4 KiB
Python

"""``ImageCompressionDecision``: canonical "should this request have
images compressed?" gate.
Mirror of :class:`CompressionDecision` (text compression) and
:class:`MemoryDecision`. Pre-this-PR image compression was gated at
two sites (``openai.py:1203``, ``anthropic.py:868``) by inline
conjunctions. Both already checked ``_bypass`` (no drift bug like
the text-compression Gemini bypass-misses) — but consolidating into
a value type still pays off:
* Locks the contract via tests so a future site can't drift
* ``apply_to_tags()`` surfaces ``image_skip_reason`` to
:class:`RequestOutcome.tags` for dashboard slicing
* Rust-portable shape, same as the other decision types
Precedence (highest first):
1. ``bypass_header`` — user's explicit opt-out
2. ``image_optimize_disabled`` — operator ``config.image_optimize=False``
3. ``no_messages`` — empty / missing messages
4. otherwise → ``should_compress=True``
Distinct from text :class:`CompressionDecision`'s
``compression_disabled`` reason: operators can enable text + disable
image (or vice versa) independently. Same shape, different gate.
"""
from __future__ import annotations
from collections.abc import Sequence
from dataclasses import dataclass
from typing import Any
from headroom.proxy.image_compression_policy import (
apply_image_skip_reason,
decide_image_compression,
)
@dataclass(frozen=True)
class ImageCompressionDecision:
"""Immutable, value-equal snapshot of the image-compression gate.
Construction policy: use :meth:`decide`. Direct construction is
allowed for tests but unusual — handlers always go through
``decide``. The constituent observability booleans
(``bypass_header_set`` etc.) MUST match the inputs ``decide`` saw;
the factory enforces that invariant, and the dataclass being
frozen means downstream code can't violate it.
"""
should_compress: bool
# When ``should_compress`` is False, the canonical reason surfaced
# in ``RequestOutcome.tags["image_skip_reason"]`` so the dashboard
# can slice image-skipped traffic by cause. One of:
# * "bypass_header" — user set x-headroom-bypass/mode
# * "image_optimize_disabled" — operator config off
# * "no_messages" — empty / missing messages
# When ``should_compress`` is True, this is None.
passthrough_reason: str | None
# Observability: every constituent boolean exposed so debug tools
# answer "what did the decision see?" without re-running it.
bypass_header_set: bool
image_optimize_enabled: bool
has_messages: bool
@classmethod
def decide(
cls,
*,
headers: Any,
config: Any,
messages: Sequence[Any] | None,
) -> ImageCompressionDecision:
"""Compute the canonical image-compression decision.
Parameters
----------
headers
Inbound request headers. Accepts any object with a
``.get(key)`` method (dict, starlette Headers, mapping).
Bypass detected via ``_headroom_bypass_enabled``.
config
``HeadroomConfig``-shaped object; only ``image_optimize``
is read.
messages
Request messages. ``None`` and ``[]`` are equivalent.
"""
decision = decide_image_compression(
headers=headers,
image_optimize_enabled=bool(getattr(config, "image_optimize", False)),
has_messages=bool(messages),
)
return cls(
should_compress=decision.should_compress,
passthrough_reason=decision.passthrough_reason,
bypass_header_set=decision.bypass_header_set,
image_optimize_enabled=decision.image_optimize_enabled,
has_messages=decision.has_messages,
)
def apply_to_tags(self, tags: dict[str, str]) -> None:
"""Stamp the skip reason into a tags dict for dashboard slicing.
Mutates ``tags`` in place. No-op when ``should_compress=True``
— absence vs presence is the signal.
Mirror of :meth:`CompressionDecision.apply_to_tags` and
:meth:`MemoryDecision.apply_to_tags`. Multiple decision tags
coexist in the same dict (``passthrough_reason``,
``memory_skip_reason``, ``image_skip_reason``) for full
dashboard slicing.
"""
apply_image_skip_reason(tags, self.passthrough_reason)