Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

107 lines
4.5 KiB
Python

from collections.abc import Iterator
from typing import Any
import pytest
from llm_gateway.api.handler import (
ANTHROPIC_CONFIG,
BEDROCK_CONFIG,
CLOUDFLARE_ANTHROPIC_CONFIG,
CLOUDFLARE_OPENAI_CONFIG,
CLOUDFLARE_OPENAI_RESPONSES_CONFIG,
OPENAI_CONFIG,
OPENAI_RESPONSES_CONFIG,
OPENAI_TRANSCRIPTION_CONFIG,
ProviderConfig,
effort_from_output_config,
effort_from_reasoning,
effort_from_reasoning_effort,
handle_llm_request,
no_effort,
)
from llm_gateway.auth.models import AuthenticatedUser
from llm_gateway.request_context import effort_var, get_effort
class TestEffortExtractors:
@pytest.mark.parametrize(
"extractor, request_data, expected",
[
# Anthropic Messages: output_config.effort
(effort_from_output_config, {"output_config": {"effort": "medium"}}, "medium"),
(effort_from_output_config, {"output_config": {"effort": " medium "}}, "medium"),
# Structured-outputs-only request (format, no effort)
(effort_from_output_config, {"output_config": {"format": {"type": "json_schema"}}}, None),
(effort_from_output_config, {"output_config": "not-a-dict"}, None),
(effort_from_output_config, {"output_config": {"effort": 5}}, None),
(effort_from_output_config, {}, None),
# OpenAI chat completions: reasoning_effort
(effort_from_reasoning_effort, {"reasoning_effort": "high"}, "high"),
(effort_from_reasoning_effort, {"reasoning_effort": " "}, None),
# Permissive: a newly-introduced level passes through
(effort_from_reasoning_effort, {"reasoning_effort": "ultra"}, "ultra"),
(effort_from_reasoning_effort, {}, None),
# OpenAI Responses: reasoning.effort
(effort_from_reasoning, {"reasoning": {"effort": "xhigh"}}, "xhigh"),
(effort_from_reasoning, {"reasoning": "not-a-dict"}, None),
(effort_from_reasoning, {}, None),
# Endpoints without an effort param ignore everything
(no_effort, {"reasoning_effort": "high"}, None),
],
)
def test_extractor(self, extractor: Any, request_data: dict[str, Any], expected: str | None) -> None:
assert extractor(request_data) == expected
class TestEffortInstrumentation:
@pytest.fixture(autouse=True)
def reset_effort(self) -> Iterator[None]:
token = effort_var.set(None)
yield
effort_var.reset(token)
# One case per real config with its surface's natural request shape, so miswiring any
# config to the wrong extractor (silently dropping $ai_effort for that surface) fails here.
@pytest.mark.parametrize(
"provider_config, request_data, expected",
[
(ANTHROPIC_CONFIG, {"output_config": {"effort": "medium"}}, "medium"),
(BEDROCK_CONFIG, {"output_config": {"effort": "low"}}, "low"),
(OPENAI_CONFIG, {"reasoning_effort": "high"}, "high"),
(OPENAI_RESPONSES_CONFIG, {"reasoning": {"effort": "xhigh"}}, "xhigh"),
(OPENAI_TRANSCRIPTION_CONFIG, {"reasoning_effort": "high"}, None),
(CLOUDFLARE_ANTHROPIC_CONFIG, {"output_config": {"effort": "medium"}}, "medium"),
(CLOUDFLARE_OPENAI_CONFIG, {"reasoning_effort": "high"}, "high"),
(CLOUDFLARE_OPENAI_RESPONSES_CONFIG, {"reasoning": {"effort": "xhigh"}}, "xhigh"),
# No effort in the request resets any stale context value to None
(ANTHROPIC_CONFIG, {}, None),
],
)
@pytest.mark.asyncio
async def test_effort_from_request_reaches_context(
self,
authenticated_user: AuthenticatedUser,
provider_config: ProviderConfig,
request_data: dict[str, Any],
expected: str | None,
) -> None:
# Pre-seed a stale value: handle_llm_request must set effort unconditionally, so the
# no-effort case resets to None rather than leaking the stale value into the callback.
effort_var.set("stale")
captured: dict[str, Any] = {}
async def mock_llm_call(**kwargs: Any) -> dict[str, Any]:
captured["effort"] = get_effort()
return {"ok": True}
await handle_llm_request(
request_data=request_data,
user=authenticated_user,
model="test-model",
is_streaming=False,
provider_config=provider_config,
llm_call=mock_llm_call,
)
assert captured["effort"] == expected