: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(llma): stop flooding error tracking with Hog evaluator user errors (#64695)
Radu Raiceaf2ad8bde097b
diff --git a/posthog/temporal/ai_observability/evaluation_hog.py b/posthog/temporal/ai_observability/evaluation_hog.pyindex 0391f3d1b072..aed17ac0548a 100644--- a/posthog/temporal/ai_observability/evaluation_hog.py+++ b/posthog/temporal/ai_observability/evaluation_hog.py@@ -22,6 +22,8 @@ def run_hog_eval(bytecode: list, event_data: dict[str, Any], allows_na: bool = FUsed by both the Temporal activity and the test endpoint.Returns {"verdict": bool | None, "reasoning": str, "error": str | None}.When allows_na=True, a `return null` is treated as N/A (not an error).+ Sets "unexpected": True only when the bytecode raised something other than a+ HogVM error — i.e. a bug in our code rather than in the user's Hog source."""properties = event_data["properties"]if isinstance(properties, str):@@ -57,9 +59,14 @@ def run_hog_eval(bytecode: list, event_data: dict[str, Any], allows_na: bool = Freturn {"verdict": None, "reasoning": "", "error": "Memory limit exceeded"}except HogVMException as e:return {"verdict": None, "reasoning": "", "error": f"Runtime error: {e}"}- except Exception:+ except Exception as e:logger.exception("Unexpected error executing Hog eval bytecode")- return {"verdict": None, "reasoning": "", "error": "Unexpected error during evaluation"}+ return {+ "verdict": None,+ "reasoning": "",+ "error": f"Unexpected error during evaluation: {type(e).__name__}: {e}",+ "unexpected": True,+ }reasoning = "\n".join(response.stdout) if response.stdout else ""@@ -103,10 +110,29 @@ async def execute_hog_eval_activity(evaluation: dict[str, Any], event_data: dictresult = await database_sync_to_async(_execute, thread_sensitive=False)()if result["error"]:- raise ApplicationError(- f"Hog evaluation error: {result['error']}",- non_retryable=True,- )+ if result.get("unexpected"):+ # A genuine bug in our evaluation code (not the user's Hog). Raise so the Temporal+ # interceptor reports it to error tracking and we get paged to investigate.+ raise ApplicationError(+ f"Hog evaluation error: {result['error']}",+ non_retryable=True,+ )++ # The user's Hog source itself errored (invalid code, execution timeout, or a+ # non-boolean result). That's an expected outcome of running customer-authored code,+ # not a system fault — record it as a skipped evaluation the user can see, rather than+ # raising (which would flood error tracking with one event per matching generation).+ errored_result: EvaluationActivityResult = {+ "result_type": "boolean",+ "verdict": None if allows_na else False,+ "reasoning": result["error"],+ "allows_na": allows_na,+ "skipped": True,+ "skip_reason": "hog_error",+ }+ if allows_na:+ errored_result["applicable"] = False+ return errored_resultactivity_result: EvaluationActivityResult = {"result_type": "boolean",diff --git a/posthog/temporal/ai_observability/test_run_evaluation.py b/posthog/temporal/ai_observability/test_run_evaluation.pyindex b25dd4c2f932..7cf16be0c275 100644--- a/posthog/temporal/ai_observability/test_run_evaluation.py+++ b/posthog/temporal/ai_observability/test_run_evaluation.py@@ -877,7 +877,7 @@ class TestExecuteHogEvalActivity:@pytest.mark.asyncio@pytest.mark.django_db(transaction=True)- async def test_hog_eval_non_bool_raises_error(self, setup_data):+ async def test_hog_eval_non_bool_returns_skipped(self, setup_data):team = setup_data["team"]from posthog.cdp.validation import compile_hog@@ -896,8 +896,12 @@ class TestExecuteHogEvalActivity:event_data = create_mock_event_data(team.id)- with pytest.raises(ApplicationError, match="Must return boolean"):- await execute_hog_eval_activity(evaluation, event_data)+ # A non-boolean result is a user-authored Hog error: recorded as skipped, not raised.+ result = await execute_hog_eval_activity(evaluation, event_data)++ assert result["skipped"] is True+ assert result["skip_reason"] == "hog_error"+ assert "Must return boolean" in result["reasoning"]@pytest.mark.asyncio@pytest.mark.django_db(transaction=True)@@ -992,6 +996,75 @@ class TestExecuteHogEvalActivity:assert result["verdict"] is True+ @pytest.mark.asyncio+ @pytest.mark.django_db(transaction=True)+ async def test_hog_eval_runtime_error_returns_skipped(self, setup_data):+ team = setup_data["team"]++ from posthog.cdp.validation import compile_hog++ bytecode = compile_hog("return true", "destination")++ evaluation = {+ "id": str(setup_data["evaluation"].id),+ "name": "Hog Eval",+ "evaluation_type": "hog",+ "evaluation_config": {"source": "return true", "bytecode": bytecode},+ "output_type": "boolean",+ "output_config": {},+ "team_id": team.id,+ }++ event_data = create_mock_event_data(team.id)++ # A HogVM runtime error in user-authored Hog (e.g. unsupported function, unknown global)+ # is recorded as skipped, not raised — so it never reaches error tracking.+ user_error = {"verdict": None, "reasoning": "", "error": "Runtime error: Global variable not found: continue"}+ with patch(+ "posthog.temporal.ai_observability.evaluation_hog.run_hog_eval",+ return_value=user_error,+ ):+ result = await execute_hog_eval_activity(evaluation, event_data)++ assert result["skipped"] is True+ assert result["skip_reason"] == "hog_error"+ assert "Global variable not found" in result["reasoning"]++ @pytest.mark.asyncio+ @pytest.mark.django_db(transaction=True)+ async def test_hog_eval_unexpected_error_raises(self, setup_data):+ team = setup_data["team"]++ from posthog.cdp.validation import compile_hog++ bytecode = compile_hog("return true", "destination")++ evaluation = {+ "id": str(setup_data["evaluation"].id),+ "name": "Hog Eval",+ "evaluation_type": "hog",+ "evaluation_config": {"source": "return true", "bytecode": bytecode},+ "output_type": "boolean",+ "output_config": {},+ "team_id": team.id,+ }++ event_data = create_mock_event_data(team.id)++ # An unexpected error is a bug in our code, so it must still surface to error tracking.+ unexpected_error = {+ "verdict": None,+ "reasoning": "",+ "error": "Unexpected error during evaluation: KeyError: 'foo'",+ "unexpected": True,+ }+ with patch(+ "posthog.temporal.ai_observability.evaluation_hog.run_hog_eval",+ return_value=unexpected_error,+ ):+ with pytest.raises(ApplicationError, match="Unexpected error during evaluation"):+ await execute_hog_eval_activity(evaluation, event_data)+class TestExecuteSentimentEvalActivity:@pytest.mark.asyncio@@ -1196,7 +1269,7 @@ class TestExecuteHogEvalActivityAllowsNA:@pytest.mark.asyncio@pytest.mark.django_db(transaction=True)- async def test_hog_eval_null_return_without_allows_na_raises(self, setup_data):+ async def test_hog_eval_null_return_without_allows_na_returns_skipped(self, setup_data):team = setup_data["team"]from posthog.cdp.validation import compile_hog@@ -1215,8 +1288,12 @@ class TestExecuteHogEvalActivityAllowsNA:event_data = create_mock_event_data(team.id)- with pytest.raises(ApplicationError, match="Must return boolean"):- await execute_hog_eval_activity(evaluation, event_data)+ result = await execute_hog_eval_activity(evaluation, event_data)++ assert result["skipped"] is True+ assert result["skip_reason"] == "hog_error"+ assert result["verdict"] is False+ assert "Must return boolean" in result["reasoning"]class TestIncrementTrialEvalCountActivity: