Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

10.0 KB
Raw Blame
import os
import importlib

import pytest
from unittest.mock import AsyncMock, MagicMock, patch

from django.test import override_settings

from anthropic.types import Message, TextBlock, Usage

from products.signals.backend.temporal import llm
from products.signals.backend.temporal.llm import call_llm
from products.signals.eval.llm_gen.client import CanonicalSignal, CanonicalSignalBatch, generate_canonical_signals

MODULE_PATH = "products.signals.backend.temporal.llm"


def _text_response(text: str) -> Message:
    return Message(
        id="msg_test",
        content=[TextBlock(text=text, type="text")],
        model="claude-sonnet-4-5",
        role="assistant",
        type="message",
        usage=Usage(input_tokens=1, output_tokens=1),
    )


def _mock_anthropic_client() -> MagicMock:
    client = MagicMock()
    client.messages.create = AsyncMock(return_value=_text_response("ok"))
    return client


@pytest.mark.asyncio
@pytest.mark.parametrize("has_text", [False, True])
async def test_explicit_refusal_is_not_treated_as_text(has_text: bool) -> None:
    response = _text_response("refused")
    response.stop_reason = "refusal"
    if not has_text:
        response.content = []
    client = _mock_anthropic_client()
    client.messages.create.return_value = response
    with patch(f"{MODULE_PATH}.get_async_anthropic_gateway_client", return_value=client):
        with pytest.raises(llm.LLMRefusalError):
            await call_llm(team_id=1, system_prompt="s", user_prompt="u", validate=lambda text: text)


@pytest.mark.asyncio
@override_settings(AI_GATEWAY_URL="https://ai-gateway.example/v1", AI_GATEWAY_API_KEY="phs_test")
async def test_gateway_mode_omits_legacy_stage_header():
    client = _mock_anthropic_client()
    with patch(f"{MODULE_PATH}.build_async_anthropic_client", return_value=client) as build_client:
        await call_llm(
            team_id=1,
            system_prompt="s",
            user_prompt="u",
            validate=lambda text: text,
            stage="match",
            ai_product="signals_grouping",
            trace_id="decision-1",
            properties={"signals_decision_id": "decision-1"},
        )

    assert build_client.call_args.kwargs["trace_id"] == "decision-1"
    assert build_client.call_args.kwargs["properties"] == {"signals_decision_id": "decision-1"}
    # In gateway mode the labels ride on the builder's X-PostHog-Properties blob; the per-key
    # ai_stage header (which the Go gateway drops) must not be sent.
    assert "extra_headers" not in client.messages.create.call_args.kwargs


@pytest.mark.asyncio
@override_settings(AI_GATEWAY_URL="", AI_GATEWAY_API_KEY="")
async def test_fallback_mode_sends_legacy_stage_header():
    client = _mock_anthropic_client()
    with patch(f"{MODULE_PATH}.build_async_anthropic_client", return_value=client):
        await call_llm(
            team_id=1,
            system_prompt="s",
            user_prompt="u",
            validate=lambda text: text,
            stage="match",
            ai_product="signals_grouping",
        )

    # On the Python-gateway fallback the stage still rides as a per-key header the route reads.
    assert client.messages.create.call_args.kwargs["extra_headers"] == {"x-posthog-property-ai_stage": "match"}


@pytest.mark.asyncio
@override_settings(AI_GATEWAY_URL="https://ai-gateway.example/v1", AI_GATEWAY_API_KEY="phs_test")
async def test_without_ai_product_stays_on_python_gateway_even_with_env_set():
    client = _mock_anthropic_client()
    with (
        patch(f"{MODULE_PATH}.get_async_anthropic_gateway_client", return_value=client) as legacy,
        patch(f"{MODULE_PATH}.build_async_anthropic_client") as gateway,
    ):
        await call_llm(team_id=1, system_prompt="s", user_prompt="u", validate=lambda text: text, stage="match")

    # A call site that hasn't opted in (no ai_product) never touches the Go-gateway builder, even
    # with the env configured, and keeps the legacy per-key stage header.
    legacy.assert_called_once()
    gateway.assert_not_called()
    assert client.messages.create.call_args.kwargs["extra_headers"] == {"x-posthog-property-ai_stage": "match"}


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "cache_system_prompt,expected_system",
    [
        (False, "s"),
        (True, [{"type": "text", "text": "s", "cache_control": {"type": "ephemeral"}}]),
    ],
)
async def test_cache_system_prompt_marks_the_system_block(
    cache_system_prompt: bool, expected_system: str | list[dict[str, object]]
) -> None:
    client = _mock_anthropic_client()
    with patch(f"{MODULE_PATH}.build_async_anthropic_client", return_value=client):
        await call_llm(
            team_id=1,
            system_prompt="s",
            user_prompt="u",
            validate=lambda text: text,
            stage="safety_filter",
            ai_product="signals_safety",
            cache_system_prompt=cache_system_prompt,
        )

    assert client.messages.create.call_args.kwargs["system"] == expected_system


@pytest.mark.asyncio
@override_settings(AI_GATEWAY_URL="https://ai-gateway.example/v1", AI_GATEWAY_API_KEY="phs_test")
async def test_non_message_response_raises_descriptive_error():
    client = _mock_anthropic_client()
    client.messages.create.return_value = "not json"

    with (
        patch(f"{MODULE_PATH}.build_async_anthropic_client", return_value=client),
        pytest.raises(TypeError, match="Expected Anthropic Message response, got str"),
    ):
        await call_llm(
            team_id=1,
            system_prompt="s",
            user_prompt="u",
            validate=lambda text: text,
            stage="match",
            ai_product="signals_grouping",
        )


# `ai_product` is the opt-in switch, not just a label: dropping it from a call site silently
# reverts that site to the Python gateway and unattributes its spend, with no failing call to
# notice. Each site that opts in pins its own tag and stage.


@pytest.mark.asyncio
async def test_eval_fixture_generation_opts_in_as_signals_eval():
    batch = CanonicalSignalBatch(signals=[CanonicalSignal(title="a" * 10, body="b" * 20)])
    with patch("products.signals.eval.llm_gen.client.call_llm", new=AsyncMock(return_value=batch)) as generation_call:
        await generate_canonical_signals(team_id=1, system_prompt="s", user_prompt="u")

    kwargs = generation_call.call_args.kwargs
    assert kwargs["ai_product"] == "signals_eval"
    assert kwargs["stage"] == "eval_signal_generation"


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "model,thinking,expect_prefill,expect_temperature,expect_thinking,expect_effort",
    [
        ("claude-sonnet-4-5", False, True, True, None, None),
        ("claude-sonnet-4-5", True, False, True, "enabled", None),
        ("claude-sonnet-5", False, False, False, None, "medium"),
        ("claude-sonnet-5", True, False, False, "adaptive", "medium"),
        ("claude-sonnet-5-5", False, False, False, None, "medium"),
        ("claude-sonnet-5-5", True, False, False, "adaptive", "medium"),
        ("claude-sonnet-4-6", False, False, True, None, "medium"),
    ],
)
async def test_request_shape_follows_model_capabilities(
    model, thinking, expect_prefill, expect_temperature, expect_thinking, expect_effort
):
    client = _mock_anthropic_client()
    with (
        patch(f"{MODULE_PATH}.MATCHING_MODEL", model),
        patch(f"{MODULE_PATH}.get_async_anthropic_gateway_client", return_value=client),
    ):
        await call_llm(
            team_id=1,
            system_prompt="s",
            user_prompt="u",
            validate=lambda text: text,
            thinking=thinking,
            stage="match",
        )

    kwargs = client.messages.create.call_args.kwargs
    prefilled = kwargs["messages"][-1]["role"] == "assistant"
    assert prefilled is expect_prefill
    assert ("temperature" in kwargs) is expect_temperature
    assert (kwargs.get("thinking") or {}).get("type") == expect_thinking
    assert kwargs.get("output_config") == ({"effort": expect_effort} if expect_effort else None)


@pytest.mark.asyncio
async def test_explicit_model_overrides_the_matching_model() -> None:
    client = _mock_anthropic_client()
    with (
        patch(f"{MODULE_PATH}.MATCHING_MODEL", "claude-sonnet-4-5"),
        patch(f"{MODULE_PATH}.get_async_anthropic_gateway_client", return_value=client),
    ):
        await call_llm(
            team_id=1,
            system_prompt="s",
            user_prompt="u",
            validate=lambda text: text,
            stage="safety_filter",
            model="claude-sonnet-5",
        )

    kwargs = client.messages.create.call_args.kwargs

    # The capabilities follow the override, not MATCHING_MODEL, so dropping it flips all three.
    assert kwargs["model"] == "claude-sonnet-5"
    assert kwargs["messages"][-1]["role"] == "user"
    assert "temperature" not in kwargs


def _reload_model_constants(env: dict[str, str]) -> tuple[str, str]:
    # Both constants resolve at import, so the environment has to change around a reload. The
    # second reload puts the module back on the real environment for the rest of the session.
    try:
        with patch.dict(os.environ, env):
            for key in ("SIGNAL_MATCHING_LLM_MODEL", "SIGNAL_SAFETY_LLM_MODEL"):
                if key not in env:
                    os.environ.pop(key, None)
            importlib.reload(llm)
            return llm.MATCHING_MODEL, llm.SAFETY_MODEL
    finally:
        importlib.reload(llm)


@pytest.mark.parametrize(
    "env,expected_matching,expected_safety",
    [
        ({}, "claude-sonnet-5-5", "claude-sonnet-5-5"),
        ({"SIGNAL_MATCHING_LLM_MODEL": "claude-opus-5"}, "claude-opus-5", "claude-sonnet-5-5"),
        (
            {"SIGNAL_MATCHING_LLM_MODEL": "claude-opus-5", "SIGNAL_SAFETY_LLM_MODEL": "claude-haiku-4-5"},
            "claude-opus-5",
            "claude-haiku-4-5",
        ),
    ],
)
def test_safety_model_does_not_follow_the_matching_model(
    env: dict[str, str], expected_matching: str, expected_safety: str
) -> None:
    matching, safety = _reload_model_constants(env)

    assert matching == expected_matching
    assert safety == expected_safety