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.

chore(eval): port survey sandboxed evals (#57551)

Chris Volzer01021d998c59
diff --git a/ee/hogai/eval/sandboxed/base.py b/ee/hogai/eval/sandboxed/base.py
index 28effabd0d60..0fe2b4d1fb20 100644
--- a/ee/hogai/eval/sandboxed/base.py
+++ b/ee/hogai/eval/sandboxed/base.py
@@ -158,7 +158,11 @@ async def SandboxedEval(
continue
eval_cases.append(
EvalCase(
- input={"name": case.name, "prompt": case.prompt, "repo_fixture": case.repo_fixture},
+ input={
+ "name": case.name,
+ "prompt": case.prompt,
+ "repo_fixture": case.repo_fixture,
+ },
expected=case.expected,
metadata=case.metadata,
)
@@ -173,11 +177,13 @@ async def SandboxedEval(
original_case = cases_by_name.get(input["name"])
try:
- # The factory does Django ORM work (fresh org/team/user, ClickHouse
- # copy SQL, PSQL person sync). Django's async-safety guard rejects
- # sync ORM calls from async contexts, so run it in a worker thread.
- sandbox_context = await asyncio.to_thread(sandboxed_demo_data.make_context, eval_case.name)
-
+ # The factory does Django ORM work. Django's async-safety guard
+ # rejects sync ORM calls from async contexts, so run it in a worker
+ # thread.
+ sandbox_context = await asyncio.to_thread(
+ sandboxed_demo_data.make_context,
+ eval_case.name,
+ )
seed_result: dict[str, Any] = {}
if original_case is not None and original_case.setup is not None:
try:
diff --git a/ee/hogai/eval/sandboxed/seeders/survey.py b/ee/hogai/eval/sandboxed/seeders/survey.py
new file mode 100644
index 000000000000..f7853d45b8fd
--- /dev/null
+++ b/ee/hogai/eval/sandboxed/seeders/survey.py
@@ -0,0 +1,66 @@
+"""Seed helpers for sandboxed survey evals."""
+
+from __future__ import annotations
+
+import logging
+from typing import Any
+
+from posthog.models import FeatureFlag
+
+from products.tasks.backend.services.custom_prompt_internals import CustomPromptSandboxContext
+
+logger = logging.getLogger(__name__)
+
+__all__ = ["seed_survey_feature_flags"]
+
+
+def _serialize_flag(flag: FeatureFlag) -> dict[str, Any]:
+ return {
+ "id": flag.id,
+ "key": flag.key,
+ "name": flag.name,
+ "filters": flag.filters,
+ }
+
+
+def seed_survey_feature_flags(context: CustomPromptSandboxContext) -> dict[str, Any]:
+ """Create feature flags used by survey targeting eval cases."""
+ checkout_flag, _ = FeatureFlag.objects.update_or_create(
+ team_id=context.team_id,
+ key="new-checkout-flow",
+ defaults={
+ "name": "New Checkout Flow",
+ "created_by_id": context.user_id,
+ "active": True,
+ "filters": {"groups": [{"properties": [], "rollout_percentage": 50}]},
+ },
+ )
+ ab_test_flag, _ = FeatureFlag.objects.update_or_create(
+ team_id=context.team_id,
+ key="ab-test-experiment",
+ defaults={
+ "name": "A/B Test Experiment",
+ "created_by_id": context.user_id,
+ "active": True,
+ "filters": {
+ "groups": [{"properties": [], "rollout_percentage": 100}],
+ "multivariate": {
+ "variants": [
+ {"key": "control", "rollout_percentage": 50},
+ {"key": "treatment", "rollout_percentage": 50},
+ ]
+ },
+ },
+ },
+ )
+
+ feature_flags = {
+ checkout_flag.key: _serialize_flag(checkout_flag),
+ ab_test_flag.key: _serialize_flag(ab_test_flag),
+ }
+ logger.info(
+ "Seeded survey feature flags for team_id=%s: %s",
+ context.team_id,
+ ", ".join(sorted(feature_flags)),
+ )
+ return {"feature_flags_by_key": feature_flags}
diff --git a/ee/hogai/eval/sandboxed/surveys/eval_surveys.py b/ee/hogai/eval/sandboxed/surveys/eval_surveys.py
new file mode 100644
index 000000000000..0a2c3b39dfcd
--- /dev/null
+++ b/ee/hogai/eval/sandboxed/surveys/eval_surveys.py
@@ -0,0 +1,305 @@
+"""Survey creation eval cases for the sandboxed coding agent.
+
+Intent mirrors ``ee/hogai/eval/ci/eval_surveys.py``. The CI version calls
+Max's ``CreateSurveyTool`` directly and checks Django state. This version
+asks the sandboxed agent to create surveys through the PostHog MCP
+``survey-create`` tool and scores the payload/result it produced.
+
+To run:
+ pytest ee/hogai/eval/sandboxed/surveys/eval_surveys.py
+"""
+
+from __future__ import annotations
+
+from collections.abc import Callable
+from typing import Any
+
+import pytest
+
+from products.tasks.backend.services.custom_prompt_internals import CustomPromptSandboxContext
+
+from ee.hogai.eval.sandboxed.base import SandboxedPublicEval
+from ee.hogai.eval.sandboxed.config import SandboxedEvalCase
+from ee.hogai.eval.sandboxed.product_analytics.scorers import INSIGHT_WRITE_TOOLS
+from ee.hogai.eval.sandboxed.scorers import ExitCodeZero, NoToolCall
+from ee.hogai.eval.sandboxed.seeders.survey import seed_survey_feature_flags
+from ee.hogai.eval.sandboxed.surveys.scorers import (
+ SURVEY_FORBIDDEN_WRITE_TOOLS,
+ SurveyCreateOutcome,
+ SurveyCreateReturnedId,
+ SurveyCreateSchemaAlignment,
+ SurveyIdInFinalMessage,
+)
+
+
+def _survey_case(
+ *,
+ name: str,
+ prompt: str,
+ expected_survey: dict[str, Any],
+ setup: Callable[[CustomPromptSandboxContext], dict[str, Any]] | None = None,
+) -> SandboxedEvalCase:
+ return SandboxedEvalCase(
+ name=name,
+ prompt=prompt,
+ expected={
+ "survey_created": {"should_create": True},
+ "survey_create_alignment": expected_survey,
+ "survey_id_in_final_message": {"required": True},
+ },
+ setup=setup,
+ )
+
+
+async def eval_surveys(
+ sandboxed_demo_data: Any,
+ pytestconfig: pytest.Config,
+ posthog_client: Any,
+ mcp_mode: str,
+) -> None:
+ cases: list[SandboxedEvalCase] = [
+ _survey_case(
+ name="survey_nps_draft",
+ prompt=(
+ "Create a draft popover survey named '[sandboxed] NPS Survey' with description "
+ "'Net Promoter Score survey'. Add one NPS rating question: "
+ "'How likely are you to recommend us to a friend or colleague?' with lower label "
+ "'Not likely at all' and upper label 'Extremely likely'. Do not launch it."
+ ),
+ expected_survey={
+ "name": "[sandboxed] NPS Survey",
+ "description": "Net Promoter Score survey",
+ "type": "popover",
+ "should_launch": False,
+ "questions": [
+ {
+ "type": "rating",
+ "question": "How likely are you to recommend us to a friend or colleague?",
+ "scale": 10,
+ "display": "number",
+ "lowerBoundLabel": "Not likely at all",
+ "upperBoundLabel": "Extremely likely",
+ }
+ ],
+ },
+ ),
+ _survey_case(
+ name="survey_csat_launched",
+ prompt=(
+ "Create and launch a popover survey named '[sandboxed] CSAT Survey' with description "
+ "'Customer satisfaction survey'. It should ask one CSAT rating question: "
+ "'How satisfied are you with our product?'"
+ ),
+ expected_survey={
+ "name": "[sandboxed] CSAT Survey",
+ "description": "Customer satisfaction survey",
+ "type": "popover",
+ "should_launch": True,
+ "questions": [
+ {
+ "type": "rating",
+ "question": "How satisfied are you with our product?",
+ "scale": 5,
+ "display": "number",
+ }
+ ],
+ },
+ ),
+ _survey_case(
+ name="survey_nps_with_followup",
+ prompt=(
+ "Create a draft popover survey named '[sandboxed] NPS with Follow-up' with description "
+ "'NPS survey with optional follow-up question'. Add two questions in this order: an NPS rating "
+ "question 'How likely are you to recommend us?' and an optional open-text follow-up "
+ "'What could we improve?'."
+ ),
+ expected_survey={
+ "name": "[sandboxed] NPS with Follow-up",
+ "description": "NPS survey with optional follow-up question",
+ "type": "popover",
+ "should_launch": False,
+ "questions": [
+ {
+ "type": "rating",
+ "question": "How likely are you to recommend us?",
+ "scale": 10,
+ "display": "number",
+ },
+ {
+ "type": "open",
+ "question": "What could we improve?",
+ "optional": True,
+ },
+ ],
+ },
+ ),
+ _survey_case(
+ name="survey_pmf_single_choice",
+ prompt=(
+ "Create a draft popover PMF survey named '[sandboxed] PMF Survey' with description "
+ "'Product-market fit survey'. Ask one single-choice question: "
+ "'How would you feel if you could no longer use our product?' with choices "
+ "'Very disappointed', 'Somewhat disappointed', and 'Not disappointed'."
+ ),
+ expected_survey={
+ "name": "[sandboxed] PMF Survey",
+ "description": "Product-market fit survey",
+ "type": "popover",
+ "should_launch": False,
+ "questions": [
+ {
+ "type": "single_choice",
+ "question": "How would you feel if you could no longer use our product?",
+ "choices": ["Very disappointed", "Somewhat disappointed", "Not disappointed"],
+ }
+ ],
+ },
+ ),
+ _survey_case(
+ name="survey_checkout_feature_flag",
+ prompt=(
+ "Create a draft popover survey named '[sandboxed] Checkout Feedback' with description "
+ "'Feedback for new checkout flow users'. Ask one open-text question: "
+ "'How was your checkout experience?' Link it to the existing feature flag with key "
+ "'new-checkout-flow'."
+ ),
+ expected_survey={
+ "name": "[sandboxed] Checkout Feedback",
+ "description": "Feedback for new checkout flow users",
+ "type": "popover",
+ "should_launch": False,
+ "linked_flag_key": "new-checkout-flow",
+ "questions": [
+ {
+ "type": "open",
+ "question": "How was your checkout experience?",
+ }
+ ],
+ },
+ setup=seed_survey_feature_flags,
+ ),
+ _survey_case(
+ name="survey_ab_test_treatment_variant",
+ prompt=(
+ "Create a draft popover survey named '[sandboxed] A/B Test Treatment Survey' with description "
+ "'Survey for users in treatment variant'. Ask one CSAT rating question: "
+ "'How do you like the new design?' Link it to the existing feature flag with key "
+ "'ab-test-experiment' and target only users in the 'treatment' variant."
+ ),
+ expected_survey={
+ "name": "[sandboxed] A/B Test Treatment Survey",
+ "description": "Survey for users in treatment variant",
+ "type": "popover",
+ "should_launch": False,
+ "linked_flag_key": "ab-test-experiment",
+ "conditions": {"linkedFlagVariant": "treatment"},
+ "questions": [
+ {
+ "type": "rating",
+ "question": "How do you like the new design?",
+ "scale": 5,
+ "display": "number",
+ }
+ ],
+ },
+ setup=seed_survey_feature_flags,
+ ),
+ _survey_case(
+ name="survey_pricing_page_url_targeting",
+ prompt=(
+ "Create a draft popover survey named '[sandboxed] Pricing Page Feedback' with description "
+ "'Feedback from pricing page visitors'. Ask one single-choice question: "
+ "'Is our pricing clear?' with choices 'Yes, very clear', 'Somewhat clear', and "
+ "'Not clear at all'. Target URLs containing '/pricing'."
+ ),
+ expected_survey={
+ "name": "[sandboxed] Pricing Page Feedback",
+ "description": "Feedback from pricing page visitors",
+ "type": "popover",
+ "should_launch": False,
+ "conditions": {"url": "/pricing", "urlMatchType": "icontains"},
+ "questions": [
+ {
+ "type": "single_choice",
+ "question": "Is our pricing clear?",
+ "choices": ["Yes, very clear", "Somewhat clear", "Not clear at all"],
+ }
+ ],
+ },
+ ),
+ _survey_case(
+ name="survey_feature_usage_multiple_choice",
+ prompt=(
+ "Create a draft popover survey named '[sandboxed] Feature Usage Survey' with description "
+ "'Survey about feature usage'. Ask one multiple-choice question: 'Which features do you use most?' "
+ "with choices 'Dashboard', 'Insights', 'Session Replay', 'Feature Flags', and 'Experiments'."
+ ),
+ expected_survey={
+ "name": "[sandboxed] Feature Usage Survey",
+ "description": "Survey about feature usage",
+ "type": "popover",
+ "should_launch": False,
+ "questions": [
+ {
+ "type": "multiple_choice",
+ "question": "Which features do you use most?",
+ "choices": ["Dashboard", "Insights", "Session Replay", "Feature Flags", "Experiments"],
+ }
+ ],
+ },
+ ),
+ _survey_case(
+ name="survey_widget_feedback",
+ prompt=(
+ "Create a draft widget survey named '[sandboxed] Widget Feedback' with description "
+ "'Widget-based feedback survey'. Ask one open-text question: "
+ "'What do you think of our product?'"
+ ),
+ expected_survey={
+ "name": "[sandboxed] Widget Feedback",
+ "description": "Widget-based feedback survey",
+ "type": "widget",
+ "should_launch": False,
+ "questions": [
+ {
+ "type": "open",
+ "question": "What do you think of our product?",
+ }
+ ],
+ },
+ ),
+ _survey_case(
+ name="survey_empty_questions_allowed",
+ prompt=(
+ "Create a draft popover survey named '[sandboxed] Empty Questions Survey' with description "
+ "'Survey with no questions'. Use an empty questions list and do not add a placeholder question."
+ ),
+ expected_survey={
+ "name": "[sandboxed] Empty Questions Survey",
+ "description": "Survey with no questions",
+ "type": "popover",
+ "should_launch": False,
+ "questions": [],
+ },
+ ),
+ ]
+
+ await SandboxedPublicEval(
+ experiment_name=f"sandboxed-surveys-{mcp_mode}",
+ cases=cases,
+ scorers=[
+ ExitCodeZero(),
+ NoToolCall(
+ forbidden=SURVEY_FORBIDDEN_WRITE_TOOLS | INSIGHT_WRITE_TOOLS,
+ name="no_forbidden_survey_side_effects",
+ ),
+ SurveyCreateOutcome(),
+ SurveyCreateReturnedId(),
+ SurveyCreateSchemaAlignment(),
+ SurveyIdInFinalMessage(),
+ ],
+ pytestconfig=pytestconfig,
+ sandboxed_demo_data=sandboxed_demo_data,
+ posthog_client=posthog_client,
+ )
diff --git a/ee/hogai/eval/sandboxed/surveys/scorers.py b/ee/hogai/eval/sandboxed/surveys/scorers.py
new file mode 100644
index 000000000000..7e15874c09b4
--- /dev/null
+++ b/ee/hogai/eval/sandboxed/surveys/scorers.py
@@ -0,0 +1,613 @@
+"""Survey scorers for sandboxed agent evals."""
+
+from __future__ import annotations
+
+import re
+import json
+from typing import Any
+
+from autoevals.llm import LLMClassifier
+from braintrust import Score
+from braintrust_core.score import Scorer
+
+from ee.hogai.eval.sandboxed.log_parser import LogParser, ToolCall
+
+SURVEY_CREATE_TOOL_NAME = "survey-create"
+SURVEY_FORBIDDEN_WRITE_TOOLS = frozenset(
+ {
+ "survey-update",
+ "survey-delete",
+ "create-feature-flag",
+ "update-feature-flag",
+ "delete-feature-flag",
+ "feature-flags-bulk-delete-create",
+ "feature-flags-bulk-keys-create",
+ "feature-flags-bulk-update-tags-create",
+ "feature-flags-copy-flags-create",
+ "scheduled-changes-create",
+ "scheduled-changes-delete",
+ "scheduled-changes-update",
+ "scheduled-changes-update-full",
+ }
+)
+
+GRADED_ALIGNMENT_CHOICE_SCORES = {
+ "perfect": 1.0,
+ "near_perfect": 0.9,
+ "slightly_off": 0.75,
+ "somewhat_misaligned": 0.5,
+ "strongly_misaligned": 0.25,
+ "useless": 0.0,
+}
+
+GRADED_ALIGNMENT_RUBRIC = """
+How would you rate the alignment of the generated survey-create payload with the expected survey? Choose one:
+- perfect: The payload fully matches the expected survey on every material aspect.
+- near_perfect: The payload matches with at most one immaterial detail missed from the user question.
+- slightly_off: Mostly matches, with minor discrepancies that may slightly change the created survey.
+- somewhat_misaligned: Has some correct elements, but misses key aspects of the expected survey.
+- strongly_misaligned: Does not match the expected survey and fails to address the user request.
+- useless: Basically incomprehensible.
+
+Details matter greatly here, especially question type, launch state, targeting, and feature-flag variant conditions.
+""".strip()
+
+_JUDGE_MODEL = "gpt-5.4"
+_UUID_RE = re.compile(r"\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b")
+_ID_FIELD_RE = re.compile(r"\bid\s*[:=]\s*['\"]?([0-9a-fA-F-]{32,36})")
+
+
+class _JudgedScorer(LLMClassifier):
+ async def _run_eval_async(self, output, expected=None, **kwargs):
+ prepared = self._prepare(output, expected)
+ if isinstance(prepared, Score):
+ return prepared
+ try:
+ return await super()._run_eval_async(prepared["output"], prepared["expected"], **kwargs)
+ except Exception as exc:
+ return Score(name=self._name(), score=0.0, metadata={"reason": f"judge error: {exc}"})
+
+ def _run_eval_sync(self, output, expected=None, **kwargs):
+ prepared = self._prepare(output, expected)
+ if isinstance(prepared, Score):
+ return prepared
+ try:
+ return super()._run_eval_sync(prepared["output"], prepared["expected"], **kwargs)
+ except Exception as exc:
+ return Score(name=self._name(), score=0.0, metadata={"reason": f"judge error: {exc}"})
+
+ def _prepare(self, output, expected) -> dict[str, Any] | Score:
+ raise NotImplementedError
+
+
+def _parser_for(output: dict[str, Any] | None) -> LogParser | None:
+ if not output:
+ return None
+ raw_log = output.get("raw_log")
+ if not raw_log:
+ return None
+ return LogParser(raw_log, initial_prompt=output.get("prompt", "") or "")
+
+
+def _user_prompt(output: dict[str, Any] | None) -> str:
+ parser = _parser_for(output)
+ if parser is not None:
+ return parser.get_user_prompt()
+ if output:
+ prompt = output.get("prompt")
+ if isinstance(prompt, str):
+ return prompt
+ return ""
+
+
+def _last_survey_create_call(output: dict[str, Any] | None, *, successful: bool | None = None) -> ToolCall | None:
+ parser = _parser_for(output)
+ if parser is None:
+ return None
+ calls = parser.get_tool_calls(SURVEY_CREATE_TOOL_NAME)
+ if successful is True:
+ calls = [call for call in calls if not call.is_error]
+ elif successful is False:
+ calls = [call for call in calls if call.is_error]
+ if not calls:
+ return None
+ return calls[-1]
+
+
+def extract_last_survey_create_input(output: dict[str, Any] | None) -> dict[str, Any] | None:
+ call = _last_survey_create_call(output, successful=True)
+ return call.input if call is not None else None
+
+
+def _decode_json(value: str) -> Any:
+ try:
+ return json.loads(value)
+ except json.JSONDecodeError:
+ return None
+
+
+def _looks_like_error_message(value: str) -> bool:
+ value_lower = value.lower()
+ return (
+ "validation" in value_lower
+ or "required" in value_lower
+ or "bad request" in value_lower
+ or "not allowed" in value_lower
+ or "does not exist" in value_lower
+ or re.search(r"\berror\b", value_lower) is not None
+ )
+
+
+def _decoded_payload_has_error(value: Any) -> bool:
+ if isinstance(value, dict):
+ for key in ("is_error", "isError"):
+ is_error = value.get(key)
+ if isinstance(is_error, bool) and is_error:
+ return True
+
+ for key in ("error", "errors"):
+ error_value = value.get(key)
+ if error_value:
+ return True
+
+ for key in ("detail", "message"):
+ message = value.get(key)
+ if isinstance(message, str) and _looks_like_error_message(message):
+ return True
+
+ content = value.get("content")
+ if isinstance(content, str):
+ decoded_content = _decode_json(content)
+ if decoded_content is not None:
+ return _decoded_payload_has_error(decoded_content)
+ return _looks_like_error_message(content)
+
+ if isinstance(content, list):
+ return _decoded_payload_has_error(content)
+
+ if isinstance(value, list):
+ for item in value:
+ if isinstance(item, dict):
+ text = item.get("text")
+ if isinstance(text, str):
+ decoded_text = _decode_json(text)
+ if decoded_text is not None and _decoded_payload_has_error(decoded_text):
+ return True
+ if _looks_like_error_message(text):
+ return True
+ if _decoded_payload_has_error(item):
+ return True
+ elif isinstance(item, str) and _looks_like_error_message(item):
+ return True
+
+ return False
+
+
+def _survey_create_experienced_error(call: ToolCall) -> bool:
+ if call.is_error:
+ return True
+
+ decoded = _decode_json(call.output)
+ if decoded is not None:
+ return _decoded_payload_has_error(decoded)
+
+ return _looks_like_error_message(call.output)
+
+
+def _strip_analytics(payload: dict[str, Any]) -> dict[str, Any]:
+ return {key: value for key, value in payload.items() if key != "_analytics"}
+
+
+def _structured_result(call: ToolCall) -> dict[str, Any] | None:
+ decoded = _decode_json(call.output)
+ if isinstance(decoded, dict):
+ structured = decoded.get("structuredContent")
+ if isinstance(structured, dict):
+ return _strip_analytics(structured)
+ content = decoded.get("content")
+ if isinstance(content, list):
+ for block in content:
+ if not isinstance(block, dict):
+ continue
+ text = block.get("text")
+ if not isinstance(text, str):
+ continue
+ text_decoded = _decode_json(text)
+ if isinstance(text_decoded, dict):
+ return text_decoded
+ if "id" in decoded:
+ return decoded
+ return None
+
+
+def _extract_survey_id(call: ToolCall) -> str | None:
+ structured = _structured_result(call)
+ if structured is not None:
+ survey_id = structured.get("id")
+ if isinstance(survey_id, (str, int)):
+ return str(survey_id)
+ survey = structured.get("survey")
+ if isinstance(survey, dict):
+ nested_id = survey.get("id")
+ if isinstance(nested_id, (str, int)):
+ return str(nested_id)
+
+ id_match = _ID_FIELD_RE.search(call.output)
+ if id_match:
+ return id_match.group(1)
+ uuid_match = _UUID_RE.search(call.output)
+ if uuid_match:
+ return uuid_match.group(0)
+ return None
+
+
+def _expected_spec(expected: dict[str, Any] | None, key: str) -> dict[str, Any] | None:
+ if not isinstance(expected, dict):
+ return None
+ spec = expected.get(key)
+ return spec if isinstance(spec, dict) else None
+
+
+def _seeded_flag_id(output: dict[str, Any] | None, flag_key: str) -> int | None:
+ if not output:
+ return None
+ seed = output.get("seed")
+ if not isinstance(seed, dict):
+ return None
+ flags_by_key = seed.get("feature_flags_by_key")
+ if not isinstance(flags_by_key, dict):
+ return None
+ flag = flags_by_key.get(flag_key)
+ if not isinstance(flag, dict):
+ return None
+ flag_id = flag.get("id")
+ return flag_id if isinstance(flag_id, int) else None
+
+
+def _materialize_expected_survey(
+ expected: dict[str, Any] | None,
+ output: dict[str, Any] | None,
+) -> tuple[dict[str, Any] | None, str | None]:
+ spec = _expected_spec(expected, "survey_create_alignment")
+ if spec is None:
+ return None, "No expected.survey_create_alignment provided"
+
+ survey = json.loads(json.dumps(spec))
+ linked_flag_key = survey.pop("linked_flag_key", None)
+ if isinstance(linked_flag_key, str):
+ flag_id = _seeded_flag_id(output, linked_flag_key)
+ if flag_id is None:
+ return None, f"No seeded feature flag found for key '{linked_flag_key}'"
+ survey["linked_flag_id"] = flag_id
+ return survey, None
+
+
+class SurveyCreationSuccess(Scorer):
+ """Binary: did survey-create succeed when expected, or avoid success for invalid input?"""
+
+ def _name(self) -> str:
+ return "survey_created"
+
+ async def _run_eval_async(self, output, expected=None, **kwargs):
+ return self._evaluate(output, expected)
+
+ def _run_eval_sync(self, output, expected=None, **kwargs):
+ return self._evaluate(output, expected)
+
+ def _evaluate(self, output: dict[str, Any] | None, expected: dict[str, Any] | None) -> Score:
+ spec = _expected_spec(expected, self._name()) or {}
+ should_create = spec.get("should_create", True)
+ successful_call = _last_survey_create_call(output, successful=True)
+
+ if should_create:
+ if successful_call is None:
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={"reason": "Agent never ran survey-create successfully"},
+ )
+ return Score(
+ name=self._name(),
+ score=1.0,
+ metadata={"survey_id": _extract_survey_id(successful_call)},
+ )
+
+ if successful_call is None:
+ return Score(name=self._name(), score=1.0, metadata={})
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={
+ "reason": "survey-create succeeded but this case expected no survey",
+ "input": successful_call.input,
+ },
+ )
+
+
+class SurveyCreateOutcome(Scorer):
+ """Binary: did the eval take the expected survey-create path?
+
+ Creation cases should produce exactly one successful ``survey-create`` call.
+ Rejection cases should experience a ``survey-create`` error and never create
+ a survey successfully.
+ """
+
+ def _name(self) -> str:
+ return "survey_create_outcome"
+
+ async def _run_eval_async(self, output, expected=None, **kwargs):
+ return self._evaluate(output, expected)
+
+ def _run_eval_sync(self, output, expected=None, **kwargs):
+ return self._evaluate(output, expected)
+
+ def _evaluate(self, output: dict[str, Any] | None, expected: dict[str, Any] | None) -> Score:
+ spec = _expected_spec(expected, "survey_created") or {}
+ should_create = spec.get("should_create", True)
+ parser = _parser_for(output)
+ if parser is None:
+ return Score(name=self._name(), score=0.0, metadata={"reason": "No raw log"})
+
+ calls = parser.get_tool_calls(SURVEY_CREATE_TOOL_NAME)
+ if not calls:
+ return Score(name=self._name(), score=0.0, metadata={"reason": "survey-create was never attempted"})
+
+ successful_calls = [call for call in calls if not _survey_create_experienced_error(call)]
+ error_calls = [call for call in calls if _survey_create_experienced_error(call)]
+
+ if should_create:
+ if not successful_calls:
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={
+ "reason": "survey-create never succeeded",
+ "error_call_count": len(error_calls),
+ },
+ )
+ if len(successful_calls) > 1:
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={
+ "reason": "survey-create succeeded multiple times",
+ "successful_call_count": len(successful_calls),
+ "inputs": [call.input for call in successful_calls],
+ },
+ )
+ return Score(
+ name=self._name(),
+ score=1.0,
+ metadata={
+ "survey_id": _extract_survey_id(successful_calls[-1]),
+ "error_call_count": len(error_calls),
+ },
+ )
+
+ if successful_calls:
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={
+ "reason": "survey-create succeeded but this case expected an error",
+ "input": successful_calls[-1].input,
+ },
+ )
+ if not error_calls:
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={"reason": "Expected a survey-create error but no errored call was observed"},
+ )
+ return Score(
+ name=self._name(),
+ score=1.0,
+ metadata={"error_call_count": len(error_calls), "input": error_calls[-1].input},
+ )
+
+
+class SurveyCreateReturnedId(Scorer):
+ """Binary: did a successful survey-create result include an extractable survey ID?"""
+
+ def _name(self) -> str:
+ return "survey_create_returned_id"
+
+ async def _run_eval_async(self, output, expected=None, **kwargs):
+ return self._evaluate(output, expected)
+
+ def _run_eval_sync(self, output, expected=None, **kwargs):
+ return self._evaluate(output, expected)
+
+ def _evaluate(self, output: dict[str, Any] | None, expected: dict[str, Any] | None) -> Score:
+ spec = _expected_spec(expected, "survey_created") or {}
+ if spec.get("should_create", True) is not True:
+ return Score(name=self._name(), score=None, metadata={"reason": "Survey ID not expected"})
+
+ call = _last_survey_create_call(output, successful=True)
+ if call is None:
+ return Score(name=self._name(), score=0.0, metadata={"reason": "No successful survey-create call"})
+
+ survey_id = _extract_survey_id(call)
+ if not survey_id:
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={"reason": "Could not extract survey ID from tool output"},
+ )
+ return Score(name=self._name(), score=1.0, metadata={"survey_id": survey_id})
+
+
+class SurveyIdInFinalMessage(Scorer):
+ """Binary: does the final assistant message include the created survey ID?"""
+
+ def _name(self) -> str:
+ return "survey_id_in_final_message"
+
+ async def _run_eval_async(self, output, expected=None, **kwargs):
+ return self._evaluate(output, expected)
+
+ def _run_eval_sync(self, output, expected=None, **kwargs):
+ return self._evaluate(output, expected)
+
+ def _evaluate(self, output: dict[str, Any] | None, expected: dict[str, Any] | None) -> Score:
+ spec = _expected_spec(expected, self._name())
+ if spec is None:
+ return Score(name=self._name(), score=None, metadata={"reason": "No ID-in-final-message expectation"})
+ if spec.get("required", True) is not True:
+ return Score(name=self._name(), score=None, metadata={"reason": "ID-in-final-message not required"})
+
+ call = _last_survey_create_call(output, successful=True)
+ if call is None:
+ return Score(name=self._name(), score=0.0, metadata={"reason": "No successful survey-create call"})
+
+ survey_id = _extract_survey_id(call)
+ if not survey_id:
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={"reason": "Could not extract survey ID from tool output"},
+ )
+
+ last_message = output.get("last_message") if output else ""
+ if not isinstance(last_message, str):
+ last_message = str(last_message)
+ if survey_id in last_message:
+ return Score(name=self._name(), score=1.0, metadata={"survey_id": survey_id})
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={"reason": "Survey ID missing from final message", "survey_id": survey_id},
+ )
+
+
+class SurveyCreateRejected(Scorer):
+ """Binary: did the agent attempt survey-create and receive a validation failure?"""
+
+ def _name(self) -> str:
+ return "survey_create_rejected"
+
+ async def _run_eval_async(self, output, expected=None, **kwargs):
+ return self._evaluate(output, expected)
+
+ def _run_eval_sync(self, output, expected=None, **kwargs):
+ return self._evaluate(output, expected)
+
+ def _evaluate(self, output: dict[str, Any] | None, expected: dict[str, Any] | None) -> Score:
+ spec = _expected_spec(expected, self._name())
+ if spec is None:
+ return Score(name=self._name(), score=None, metadata={"reason": "No rejection expectation"})
+
+ parser = _parser_for(output)
+ if parser is None:
+ return Score(name=self._name(), score=None, metadata={"reason": "No raw log"})
+
+ calls = parser.get_tool_calls(SURVEY_CREATE_TOOL_NAME)
+ if not calls:
+ return Score(name=self._name(), score=0.0, metadata={"reason": "survey-create was never attempted"})
+
+ successful = [call for call in calls if not _survey_create_experienced_error(call)]
+ if successful:
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={"reason": "survey-create succeeded for an invalid survey", "input": successful[-1].input},
+ )
+
+ error_calls = [call for call in calls if _survey_create_experienced_error(call)]
+ last_call = error_calls[-1] if error_calls else calls[-1]
+ expected_questions = spec.get("questions")
+ if expected_questions is not None and last_call.input.get("questions") != expected_questions:
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={
+ "reason": "survey-create was attempted with different questions than expected",
+ "expected_questions": expected_questions,
+ "actual_questions": last_call.input.get("questions"),
+ },
+ )
+
+ if _survey_create_experienced_error(last_call):
+ return Score(name=self._name(), score=1.0, metadata={"input": last_call.input})
+
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={"reason": "survey-create did not surface a validation failure", "output": last_call.output[:500]},
+ )
+
+
+class SurveyCreateSchemaAlignment(_JudgedScorer):
+ """Graded score: how well does the actual survey-create input match the expected survey?"""
+
+ def _prepare(self, output, expected) -> dict[str, Any] | Score:
+ actual = extract_last_survey_create_input(output)
+ if actual is None:
+ spec = _expected_spec(expected, "survey_create_alignment")
+ if spec is None:
+ return Score(
+ name=self._name(),
+ score=None,
+ metadata={"reason": "No survey-create alignment expectation"},
+ )
+ return Score(
+ name=self._name(),
+ score=0.0,
+ metadata={"reason": "Agent never ran survey-create successfully"},
+ )
+
+ expected_survey, reason = _materialize_expected_survey(expected, output)
+ if expected_survey is None:
+ return Score(name=self._name(), score=None, metadata={"reason": reason})
+
+ return {
+ "output": {"survey": actual, "prompt": _user_prompt(output)},
+ "expected": {"survey": expected_survey},
+ }
+
+ def __init__(self, **kwargs):
+ super().__init__(
+ name="survey_create_alignment",
+ prompt_template="""
+You are judging whether an agent-produced PostHog `survey-create` MCP payload would create the survey the user asked for, compared to a reference expected survey.
+
+The bar is semantic equivalence, not strict field equality. Accept harmless defaults and equivalent wording, but reject material differences that would create a different survey.
+
+User prompt:
+<user_prompt>
+{{output.prompt}}
+</user_prompt>
+
+Material aspects:
+1. **Name and description**: The survey name should match the user request. Description may be omitted only when it was not material to the prompt.
+2. **Survey type**: `popover` is the default for in-app surveys. `widget`, `api`, or `external_survey` must be used when the prompt asks for that type.
+3. **Launch state**: If expected has `should_launch: true`, the actual payload must include a non-null `start_date`. If expected has `should_launch: false`, the actual payload must not set `start_date`.
+4. **Questions**: Count, order, question text, and required fields must align. NPS should be a `rating` question with `scale: 10` and numeric display. CSAT should be a `rating` question with `scale: 5`. Open questions should be `open`. PMF-style choices should be `single_choice`. Multiple-select questions should be `multiple_choice`.
+5. **Choice lists and labels**: Choice values, lower/upper rating labels, optional status, links, and button text are material when present in the expected survey.
+6. **Feature flag targeting**: `linked_flag_id` must match the expected ID. For variant targeting, `conditions.linkedFlagVariant` must match the expected variant.
+7. **URL/display conditions**: URL conditions must match the requested path and match type. Treat `contains` and `icontains` as equivalent for a "contains" URL request.
+
+Ignore:
+- `appearance`, `enable_partial_responses`, `schedule`, generated question IDs, `_posthogUrl`, analytics metadata, and other sensible defaults unless the prompt made them material.
+- Minor wording differences in question text that preserve the same respondent-facing meaning.
+
+Penalize:
+- Creating extra questions not requested by the user.
+- Launching a survey that was meant to stay draft, or failing to launch one the user explicitly asked to launch.
+- Missing targeting, wrong feature flag, wrong variant, wrong URL condition, wrong question type, or wrong rating scale.
+
+<expected_survey>
+{{expected.survey}}
+</expected_survey>
+
+<actual_payload>
+{{output.survey}}
+</actual_payload>
+""".strip()
+ + "\n\n"
+ + GRADED_ALIGNMENT_RUBRIC,
+ choice_scores=GRADED_ALIGNMENT_CHOICE_SCORES,
+ model=_JUDGE_MODEL,
+ max_completion_tokens=512,
+ **kwargs,
+ )