AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
refactor(platform): remove duplicate PostHog events and add base properties (#14835)
Krzysztof Czerwinski98c0dc8276b5
diff --git a/autogpt_platform/analytics/queries/chat_turn.sql b/autogpt_platform/analytics/queries/chat_turn.sqlindex a745e26420..123f023ad0 100644--- a/autogpt_platform/analytics/queries/chat_turn.sql+++ b/autogpt_platform/analytics/queries/chat_turn.sql@@ -6,7 +6,7 @@-- One row per user-role chat message (last 90 days): the copilot-- equivalent of a graph execution. This is where "run_autopilot"-- and "run_expert" come from for Looker, and it is the SQL twin of--- the PostHog events of the same names.+-- the PostHog event chat_message_sent.---- Dream/daydream sessions (memory maintenance, not the user) are-- excluded. Sessions with origin 'automation' are model-authoreddiff --git a/autogpt_platform/backend/backend/api/features/analytics.py b/autogpt_platform/backend/backend/api/features/analytics.pyindex 4e8ab352f9..1f03e6ef21 100644--- a/autogpt_platform/backend/backend/api/features/analytics.py+++ b/autogpt_platform/backend/backend/api/features/analytics.py@@ -1,6 +1,5 @@"""Analytics API"""-import loggingfrom typing import Annotatedimport fastapi@@ -8,12 +7,10 @@ import pydanticfrom autogpt_libs.auth import get_user_idfrom autogpt_libs.auth.dependencies import requires_user-import backend.data.analyticsfrom backend.data import attribution as attribution_dbfrom backend.data.attribution import UserAttribution, UserAttributionInputrouter = fastapi.APIRouter(dependencies=[fastapi.Security(requires_user)])-logger = logging.getLogger(__name__)@router.post(@@ -48,65 +45,3 @@ async def report_user_attribution(except pydantic.ValidationError as error:raise fastapi.HTTPException(status_code=422, detail=error.errors()) from errorreturn await attribution_db.record_user_attribution(user_id, data)---class LogRawMetricRequest(pydantic.BaseModel):- metric_name: str = pydantic.Field(..., min_length=1)- metric_value: float = pydantic.Field(..., allow_inf_nan=False)- data_string: str = pydantic.Field(..., min_length=1)--[email protected](path="/log_raw_metric")-async def log_raw_metric(- user_id: Annotated[str, fastapi.Security(get_user_id)],- request: LogRawMetricRequest,-):- try:- result = await backend.data.analytics.log_raw_metric(- user_id=user_id,- metric_name=request.metric_name,- metric_value=request.metric_value,- data_string=request.data_string,- )- return result.id- except Exception as e:- logger.exception(- "Failed to log metric %s for user %s: %s", request.metric_name, user_id, e- )- raise fastapi.HTTPException(- status_code=500,- detail={- "message": str(e),- "hint": "Check analytics service connection and retry.",- },- )--[email protected]("/log_raw_analytics")-async def log_raw_analytics(- user_id: Annotated[str, fastapi.Security(get_user_id)],- type: Annotated[str, fastapi.Body(..., embed=True)],- data: Annotated[- dict,- fastapi.Body(..., embed=True, description="The data to log"),- ],- data_index: Annotated[- str,- fastapi.Body(- ...,- embed=True,- description="Indexable field for any count based analytical measures like page order clicking, tutorial step completion, etc.",- ),- ],-):- try:- result = await backend.data.analytics.log_raw_analytics(- user_id, type, data, data_index- )- return result.id- except Exception as e:- logger.exception("Failed to log analytics for user %s: %s", user_id, e)- raise fastapi.HTTPException(- status_code=500,- detail={"message": str(e), "hint": "Ensure analytics DB is reachable."},- )diff --git a/autogpt_platform/backend/backend/api/features/analytics_test.py b/autogpt_platform/backend/backend/api/features/analytics_test.pydeleted file mode 100644index 2493bdb7e4..0000000000--- a/autogpt_platform/backend/backend/api/features/analytics_test.py+++ /dev/null@@ -1,340 +0,0 @@-"""Tests for analytics API endpoints."""--import json-from unittest.mock import AsyncMock, Mock--import fastapi-import fastapi.testclient-import pytest-import pytest_mock-from pytest_snapshot.plugin import Snapshot--from .analytics import router as analytics_router--app = fastapi.FastAPI()-app.include_router(analytics_router)--client = fastapi.testclient.TestClient(app)--[email protected](autouse=True)-def setup_app_auth(mock_jwt_user):- """Setup auth overrides for all tests in this module."""- from autogpt_libs.auth.jwt_utils import get_jwt_payload-- app.dependency_overrides[get_jwt_payload] = mock_jwt_user["get_jwt_payload"]- yield- app.dependency_overrides.clear()---# =============================================================================-# /log_raw_metric endpoint tests-# =============================================================================---def test_log_raw_metric_success(- mocker: pytest_mock.MockFixture,- configured_snapshot: Snapshot,- test_user_id: str,-) -> None:- """Test successful raw metric logging."""- mock_result = Mock(id="metric-123-uuid")- mock_log_metric = mocker.patch(- "backend.data.analytics.log_raw_metric",- new_callable=AsyncMock,- return_value=mock_result,- )-- request_data = {- "metric_name": "page_load_time",- "metric_value": 2.5,- "data_string": "/dashboard",- }-- response = client.post("/log_raw_metric", json=request_data)-- assert response.status_code == 200, f"Unexpected response: {response.text}"- assert response.json() == "metric-123-uuid"-- mock_log_metric.assert_called_once_with(- user_id=test_user_id,- metric_name="page_load_time",- metric_value=2.5,- data_string="/dashboard",- )-- configured_snapshot.assert_match(- json.dumps({"metric_id": response.json()}, indent=2, sort_keys=True),- "analytics_log_metric_success",- )--- "metric_value,metric_name,data_string,test_id",- [- (100, "api_calls_count", "external_api", "integer_value"),- (0, "error_count", "no_errors", "zero_value"),- (-5.2, "temperature_delta", "cooling", "negative_value"),- (1.23456789, "precision_test", "float_precision", "float_precision"),- (999999999, "large_number", "max_value", "large_number"),- (0.0000001, "tiny_number", "min_value", "tiny_number"),- ],-)-def test_log_raw_metric_various_values(- mocker: pytest_mock.MockFixture,- configured_snapshot: Snapshot,- metric_value: float,- metric_name: str,- data_string: str,- test_id: str,-) -> None:- """Test raw metric logging with various metric values."""- mock_result = Mock(id=f"metric-{test_id}-uuid")- mocker.patch(- "backend.data.analytics.log_raw_metric",- new_callable=AsyncMock,- return_value=mock_result,- )-- request_data = {- "metric_name": metric_name,- "metric_value": metric_value,- "data_string": data_string,- }-- response = client.post("/log_raw_metric", json=request_data)-- assert response.status_code == 200, f"Failed for {test_id}: {response.text}"-- configured_snapshot.assert_match(- json.dumps(- {"metric_id": response.json(), "test_case": test_id},- indent=2,- sort_keys=True,- ),- f"analytics_metric_{test_id}",- )--- "invalid_data,expected_error",- [- ({}, "Field required"),- ({"metric_name": "test"}, "Field required"),- (- {"metric_name": "test", "metric_value": "not_a_number", "data_string": "x"},- "Input should be a valid number",- ),- (- {"metric_name": "", "metric_value": 1.0, "data_string": "test"},- "String should have at least 1 character",- ),- (- {"metric_name": "test", "metric_value": 1.0, "data_string": ""},- "String should have at least 1 character",- ),- ],- ids=[- "empty_request",- "missing_metric_value_and_data_string",- "invalid_metric_value_type",- "empty_metric_name",- "empty_data_string",- ],-)-def test_log_raw_metric_validation_errors(- invalid_data: dict,- expected_error: str,-) -> None:- """Test validation errors for invalid metric requests."""- response = client.post("/log_raw_metric", json=invalid_data)-- assert response.status_code == 422- error_detail = response.json()- assert "detail" in error_detail, f"Missing 'detail' in error: {error_detail}"-- error_text = json.dumps(error_detail)- assert (- expected_error in error_text- ), f"Expected '{expected_error}' in error response: {error_text}"---def test_log_raw_metric_service_error(- mocker: pytest_mock.MockFixture,- test_user_id: str,-) -> None:- """Test error handling when analytics service fails."""- mocker.patch(- "backend.data.analytics.log_raw_metric",- new_callable=AsyncMock,- side_effect=Exception("Database connection failed"),- )-- request_data = {- "metric_name": "test_metric",- "metric_value": 1.0,- "data_string": "test",- }-- response = client.post("/log_raw_metric", json=request_data)-- assert response.status_code == 500- error_detail = response.json()["detail"]- assert "Database connection failed" in error_detail["message"]- assert "hint" in error_detail---# =============================================================================-# /log_raw_analytics endpoint tests-# =============================================================================---def test_log_raw_analytics_success(- mocker: pytest_mock.MockFixture,- configured_snapshot: Snapshot,- test_user_id: str,-) -> None:- """Test successful raw analytics logging."""- mock_result = Mock(id="analytics-789-uuid")- mock_log_analytics = mocker.patch(- "backend.data.analytics.log_raw_analytics",- new_callable=AsyncMock,- return_value=mock_result,- )-- request_data = {- "type": "user_action",- "data": {- "action": "button_click",- "button_id": "submit_form",- "timestamp": "2023-01-01T00:00:00Z",- "metadata": {"form_type": "registration", "fields_filled": 5},- },- "data_index": "button_click_submit_form",- }-- response = client.post("/log_raw_analytics", json=request_data)-- assert response.status_code == 200, f"Unexpected response: {response.text}"- assert response.json() == "analytics-789-uuid"-- mock_log_analytics.assert_called_once_with(- test_user_id,- "user_action",- request_data["data"],- "button_click_submit_form",- )-- configured_snapshot.assert_match(- json.dumps({"analytics_id": response.json()}, indent=2, sort_keys=True),- "analytics_log_analytics_success",- )---def test_log_raw_analytics_complex_data(- mocker: pytest_mock.MockFixture,- configured_snapshot: Snapshot,-) -> None:- """Test raw analytics logging with complex nested data structures."""- mock_result = Mock(id="analytics-complex-uuid")- mocker.patch(- "backend.data.analytics.log_raw_analytics",- new_callable=AsyncMock,- return_value=mock_result,- )-- request_data = {- "type": "agent_execution",- "data": {- "agent_id": "agent_123",- "execution_id": "exec_456",- "status": "completed",- "duration_ms": 3500,- "nodes_executed": 15,- "blocks_used": [- {"block_id": "llm_block", "count": 3},- {"block_id": "http_block", "count": 5},- {"block_id": "code_block", "count": 2},- ],- "errors": [],- "metadata": {- "trigger": "manual",- "user_tier": "premium",- "environment": "production",- },- },- "data_index": "agent_123_exec_456",- }-- response = client.post("/log_raw_analytics", json=request_data)-- assert response.status_code == 200-- configured_snapshot.assert_match(- json.dumps(- {"analytics_id": response.json(), "logged_data": request_data["data"]},- indent=2,- sort_keys=True,- ),- "analytics_log_analytics_complex_data",- )--- "invalid_data,expected_error",- [- ({}, "Field required"),- ({"type": "test"}, "Field required"),- (- {"type": "test", "data": "not_a_dict", "data_index": "test"},- "Input should be a valid dictionary",- ),- ({"type": "test", "data": {"key": "value"}}, "Field required"),- ],- ids=[- "empty_request",- "missing_data_and_data_index",- "invalid_data_type",- "missing_data_index",- ],-)-def test_log_raw_analytics_validation_errors(- invalid_data: dict,- expected_error: str,-) -> None:- """Test validation errors for invalid analytics requests."""- response = client.post("/log_raw_analytics", json=invalid_data)-- assert response.status_code == 422- error_detail = response.json()- assert "detail" in error_detail, f"Missing 'detail' in error: {error_detail}"-- error_text = json.dumps(error_detail)- assert (- expected_error in error_text- ), f"Expected '{expected_error}' in error response: {error_text}"---def test_log_raw_analytics_service_error(- mocker: pytest_mock.MockFixture,- test_user_id: str,-) -> None:- """Test error handling when analytics service fails."""- mocker.patch(- "backend.data.analytics.log_raw_analytics",- new_callable=AsyncMock,- side_effect=Exception("Analytics DB unreachable"),- )-- request_data = {- "type": "test_event",- "data": {"key": "value"},- "data_index": "test_index",- }-- response = client.post("/log_raw_analytics", json=request_data)-- assert response.status_code == 500- error_detail = response.json()["detail"]- assert "Analytics DB unreachable" in error_detail["message"]- assert "hint" in error_detaildiff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.pyindex 72cac07ba1..3b1a2e06ee 100644--- a/autogpt_platform/backend/backend/api/features/chat/routes.py+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py@@ -1974,6 +1974,7 @@ async def stream_chat_post(is_user_message=request.is_user_message,expert_id=session.expert_id,session_origin=session.metadata.origin,+ session_source_platform=session.metadata.source_platform,context=request.context,voice=request.voice,file_ids=sanitized_file_ids,diff --git a/autogpt_platform/backend/backend/api/features/experts/experts_db.py b/autogpt_platform/backend/backend/api/features/experts/experts_db.pyindex 1a21bb80a5..877c929da9 100644--- a/autogpt_platform/backend/backend/api/features/experts/experts_db.py+++ b/autogpt_platform/backend/backend/api/features/experts/experts_db.py@@ -62,6 +62,7 @@ from backend.api.features.experts.models import (ExpertWorkflowLabel,ExpertWorkflowRef,HireResult,+ HireSurface,RaiseAttachment,RaiseResult,decode_day_one,@@ -919,7 +920,12 @@ async def resolve_private_expert_tenancy(return organization_id, team_id-async def hire_expert(user_id: str, template_id: str, name: str | None) -> HireResult:+async def hire_expert(+ user_id: str,+ template_id: str,+ name: str | None,+ surface: HireSurface | None = None,+) -> HireResult:"""Hire *template_id* and return as soon as the expert's row exists.Its workflows, skills and routines install afterwards (``_run_hire_setup``);@@ -927,7 +933,7 @@ async def hire_expert(user_id: str, template_id: str, name: str | None) -> HireRwhose setup failed or was abandoned starts that setup again."""try:- return await _hire_expert_impl(user_id, template_id, name)+ return await _hire_expert_impl(user_id, template_id, name, surface)except Exception:emit_funnel_event(user_id,@@ -938,7 +944,7 @@ async def hire_expert(user_id: str, template_id: str, name: str | None) -> HireRasync def _hire_expert_impl(- user_id: str, template_id: str, name: str | None+ user_id: str, template_id: str, name: str | None, surface: HireSurface | None) -> HireResult:template = await prisma.models.Expert.prisma().find_first(where={"id": template_id, "isTemplate": True, "isArchived": False},@@ -992,21 +998,39 @@ async def _hire_expert_impl(if state == "created" or await _claim_setup(expert.id):spawn_background_task(_run_hire_setup(- user_id, expert.id, template.id, count_hire=state != "existing"+ user_id,+ expert.id,+ template.id,+ count_hire=state != "existing",+ surface=surface,),name=f"hire-setup-{expert.id}",)if state != "created":expert = await _reload_expert(expert)elif state == "revived":- emit_funnel_event(- user_id,- PostHogEvent.HIRE_COMPLETED,- {"template_id": template.id, "failed_preloads_count": 0},- )+ _emit_expert_hired(user_id, expert, template.id, 0, surface)return HireResult(expert=_to_model(expert))+def _emit_expert_hired(+ user_id: str,+ expert: prisma.models.Expert,+ template_id: str,+ failed_preloads_count: int,+ surface: HireSurface | None,+) -> None:+ hired: dict[str, str | int] = {+ "expert_id": expert.id,+ "template_id": template_id,+ "name": expert.name,+ "failed_preloads_count": failed_preloads_count,+ }+ if surface is not None:+ hired["surface"] = surface+ emit_funnel_event(user_id, PostHogEvent.EXPERT_HIRED, hired)++async def _reload_expert(row: prisma.models.Expert) -> prisma.models.Expert:refreshed = await prisma.models.Expert.prisma().find_unique(where={"id": row.id}, include=_WORKFLOW_INCLUDE@@ -1040,13 +1064,18 @@ async def _claim_setup(expert_id: str) -> bool:async def _run_hire_setup(- user_id: str, expert_id: str, template_id: str, *, count_hire: bool = False+ user_id: str,+ expert_id: str,+ template_id: str,+ *,+ count_hire: bool = False,+ surface: HireSurface | None = None,) -> None:"""Install a hire's workflows, skills and routines, retrying what failed.Every step skips what an earlier run installed, so a retry or a re-claimedsetup finishes the job instead of duplicating it. *count_hire* emits the- hire's ``hire_completed`` once the failed preloads are known.+ hire's ``expert_hired`` once the failed preloads are known."""failures: list[str] | None = Nonefailed_preloads: list[str] = []@@ -1069,7 +1098,7 @@ async def _run_hire_setup(if failures is None:failed_preloads = await _uninstalled_preloads(expert_id, template_id)failures = failed_preloads- await prisma.models.Expert.prisma().update(+ settled = await prisma.models.Expert.prisma().update(where={"id": expert_id},data={"setupStatus": (@@ -1080,12 +1109,8 @@ async def _run_hire_setup("setupFailures": failures or [],},)- if count_hire:- emit_funnel_event(- user_id,- PostHogEvent.HIRE_COMPLETED,- {"template_id": template_id, "failed_preloads_count": len(failed_preloads)},- )+ if count_hire and settled is not None:+ _emit_expert_hired(user_id, settled, template_id, len(failed_preloads), surface)async def _uninstalled_preloads(expert_id: str, template_id: str) -> list[str]:@@ -2077,7 +2102,7 @@ async def _install_library_workflow(PostHogEvent.WORKFLOW_INSTALLED_ON_EXPERT,{"expert_id": expert_id,- "source": "library",+ "workflow_source": "library","library_agent_id": library_agent_id,},)@@ -2126,7 +2151,7 @@ async def _install_marketplace_workflow(PostHogEvent.WORKFLOW_INSTALLED_ON_EXPERT,{"expert_id": expert_id,- "source": "marketplace",+ "workflow_source": "marketplace","store_listing_version_id": store_listing_version_id,},)diff --git a/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py b/autogpt_platform/backend/backend/api/features/experts/experts_db_test.pyindex b9ebbf95a9..f95c0b5783 100644--- a/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py+++ b/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py@@ -203,8 +203,8 @@ def hire_waits_for_setup(monkeypatch):"""Most tests assert on what a hire installs, so let them read it backonce the background setup has finished."""- async def hire_and_finish_setup(user_id, template_id, name):- result = await _hire_without_waiting(user_id, template_id, name)+ async def hire_and_finish_setup(user_id, template_id, name, surface=None):+ result = await _hire_without_waiting(user_id, template_id, name, surface)await _finish_hire_setup()row = await prisma.models.Expert.prisma().find_unique(where={"id": result.expert.id}, include=experts_db._WORKFLOW_INCLUDE@@ -6123,15 +6123,39 @@ async def test_seed_roster_files_every_template_under_a_canonical_category(# =============================================================================+def _expert_hired(result, template_id: str, **extra) -> dict:+ return {+ "expert_id": result.expert.id,+ "template_id": template_id,+ "name": result.expert.name,+ "failed_preloads_count": 0,+ **extra,+ }++@pytest.mark.asyncio(loop_scope="session")-async def test_hire_expert_emits_hire_completed(server: SpinTestServer, test_user):+async def test_hire_expert_emits_expert_hired(server: SpinTestServer, test_user):template = await _seed_template(name="Maria", preload_listings=[])with patch.object(experts_db, "emit_funnel_event") as emit:- await experts_db.hire_expert(test_user.id, template.id, None)+ result = await experts_db.hire_expert(test_user.id, template.id, None)+ emit.assert_called_once_with(+ test_user.id, "expert_hired", _expert_hired(result, template.id)+ )++[email protected](loop_scope="session")+async def test_expert_hired_carries_the_hiring_surface(+ server: SpinTestServer, test_user+):+ template = await _seed_template(name="Maria", preload_listings=[])+ with patch.object(experts_db, "emit_funnel_event") as emit:+ result = await experts_db.hire_expert(+ test_user.id, template.id, None, "onboarding"+ )emit.assert_called_once_with(test_user.id,- "hire_completed",- {"template_id": template.id, "failed_preloads_count": 0},+ "expert_hired",+ _expert_hired(result, template.id, surface="onboarding"),)@@ -6150,7 +6174,7 @@ async def test_hire_expert_emits_hire_failed_on_unknown_template(@pytest.mark.asyncio(loop_scope="session")-async def test_hire_completed_reports_failed_preloads_count(+async def test_expert_hired_reports_failed_preloads_count(server: SpinTestServer, test_user):slv_id = await _seed_store_listing(server)@@ -6162,11 +6186,11 @@ async def test_hire_completed_reports_failed_preloads_count(side_effect=RuntimeError("install exploded"),):with patch.object(experts_db, "emit_funnel_event") as emit:- await experts_db.hire_expert(test_user.id, template.id, None)+ result = await experts_db.hire_expert(test_user.id, template.id, None)emit.assert_called_once_with(test_user.id,- "hire_completed",- {"template_id": template.id, "failed_preloads_count": 1},+ "expert_hired",+ _expert_hired(result, template.id, failed_preloads_count=1),)@@ -6200,8 +6224,8 @@ async def test_a_raising_retry_keeps_the_failures_an_earlier_attempt_named(assert done.setup_failures == ["Agent X", "skill-a"]emit.assert_called_once_with(test_user.id,- "hire_completed",- {"template_id": template.id, "failed_preloads_count": 1},+ "expert_hired",+ _expert_hired(hired, template.id, failed_preloads_count=1),)@@ -6236,13 +6260,13 @@ async def test_a_setup_that_only_raised_reports_the_preloads_it_lacks(assert done.setup_failures == namedemit.assert_called_once_with(test_user.id,- "hire_completed",- {"template_id": template.id, "failed_preloads_count": 1},+ "expert_hired",+ _expert_hired(hired, template.id, failed_preloads_count=1),)@pytest.mark.asyncio(loop_scope="session")-async def test_idempotent_rehire_does_not_reemit_hire_completed(+async def test_idempotent_rehire_does_not_reemit_expert_hired(server: SpinTestServer, test_user):template = await _seed_template(name="Maria", preload_listings=[])@@ -6253,18 +6277,16 @@ async def test_idempotent_rehire_does_not_reemit_hire_completed(@pytest.mark.asyncio(loop_scope="session")-async def test_reviving_archived_expert_emits_hire_completed(+async def test_reviving_archived_expert_emits_expert_hired(server: SpinTestServer, test_user):template = await _seed_template(name="Maria", preload_listings=[])hired = await experts_db.hire_expert(test_user.id, template.id, None)await experts_db.archive_expert(test_user.id, hired.expert.id)with patch.object(experts_db, "emit_funnel_event") as emit:- await experts_db.hire_expert(test_user.id, template.id, None)+ revived = await experts_db.hire_expert(test_user.id, template.id, None)emit.assert_called_once_with(- test_user.id,- "hire_completed",- {"template_id": template.id, "failed_preloads_count": 0},+ test_user.id, "expert_hired", _expert_hired(revived, template.id))@@ -6284,18 +6306,18 @@ async def test_reviving_a_failed_setup_counts_the_hire_once_setup_settles(assert hired.expert.setup_status == "failed"await experts_db.archive_expert(test_user.id, hired.expert.id)with patch.object(experts_db, "emit_funnel_event") as emit:- await experts_db.hire_expert(test_user.id, template.id, None)+ revived = await experts_db.hire_expert(test_user.id, template.id, None)# Kills: emitting from the request before the re-claimed setup has run.emit.assert_called_once_with(test_user.id,- "hire_completed",- {"template_id": template.id, "failed_preloads_count": 1},+ "expert_hired",+ _expert_hired(revived, template.id, failed_preloads_count=1),)@pytest.mark.asyncio(loop_scope="session")-async def test_concurrent_revival_emits_hire_completed_once(+async def test_concurrent_revival_emits_expert_hired_once(server: SpinTestServer, test_user):template = await _seed_template(name="Maria", preload_listings=[])@@ -6313,9 +6335,7 @@ async def test_concurrent_revival_emits_hire_completed_once(assert not first.expert.is_archivedassert not second.expert.is_archivedemit.assert_called_once_with(- test_user.id,- "hire_completed",- {"template_id": template.id, "failed_preloads_count": 0},+ test_user.id, "expert_hired", _expert_hired(first, template.id))@@ -6366,7 +6386,7 @@ async def test_install_workflow_emits_workflow_installed("workflow_installed_on_expert",{"expert_id": hired.expert.id,- "source": "marketplace",+ "workflow_source": "marketplace","store_listing_version_id": slv_id,},)@@ -6394,7 +6414,7 @@ async def test_install_workflow_emits_for_the_library_source_too("workflow_installed_on_expert",{"expert_id": hired.expert.id,- "source": "library",+ "workflow_source": "library","library_agent_id": library_agent.id,},)diff --git a/autogpt_platform/backend/backend/api/features/experts/models.py b/autogpt_platform/backend/backend/api/features/experts/models.pyindex 3843d0bf26..6390081653 100644--- a/autogpt_platform/backend/backend/api/features/experts/models.py+++ b/autogpt_platform/backend/backend/api/features/experts/models.py@@ -399,6 +399,10 @@ class HireResult(BaseModel):expert: Expert+# Where a hire was made, for the ``expert_hired`` analytics event.+HireSurface = Literal["onboarding", "expert_page", "copilot"]++RaiseAttachmentKind = Literal["workflow", "skill"]RaiseAttachmentSource = Literal["marketplace", "library"]RaiseAttachmentFailureReason = Literal["unavailable", "installation_failed"]diff --git a/autogpt_platform/backend/backend/api/features/experts/routes.py b/autogpt_platform/backend/backend/api/features/experts/routes.pyindex 5a56907583..2b7f269b29 100644--- a/autogpt_platform/backend/backend/api/features/experts/routes.py+++ b/autogpt_platform/backend/backend/api/features/experts/routes.py@@ -31,6 +31,7 @@ from backend.api.features.experts.models import (ExpertTemplate,ExpertWorkflowRef,HireResult,+ HireSurface,RaiseAttachment,RaiseResult,validate_avatar_url,@@ -44,7 +45,6 @@ from backend.copilot.computer import ()from backend.copilot.config import ChatConfigfrom backend.copilot.tools.e2b_sandbox import SandboxOwner, kill_expert_sandbox-from backend.util import product_analyticsfrom backend.util.exceptions import NotFoundErrorlogger = logging.getLogger(__name__)@@ -68,6 +68,7 @@ public_router = APIRouter(prefix="/experts", tags=["experts"])class HireRequest(BaseModel):template_id: strname: str | None = Field(default=None, max_length=100)+ surface: HireSurface | None = Noneclass InstallWorkflowRequest(BaseModel):@@ -177,8 +178,8 @@ async def hire_expert(user_id: str = Security(autogpt_auth_lib.get_user_id),) -> HireResult:try:- result = await experts_db.hire_expert(- user_id, request.template_id, request.name+ return await experts_db.hire_expert(+ user_id, request.template_id, request.name, request.surface)except experts_db.ExpertTemplateNotFoundError as e:raise fastapi.HTTPException(status_code=404, detail=str(e))@@ -199,13 +200,6 @@ async def hire_expert(status_code=409,detail={"code": "active_expert_limit", "limit": e.limit},)- product_analytics.track_expert_hired(- user_id=user_id,- expert_id=result.expert.id,- template_id=request.template_id,- name=result.expert.name,- )- return result@router.post(diff --git a/autogpt_platform/backend/backend/api/features/experts/routes_test.py b/autogpt_platform/backend/backend/api/features/experts/routes_test.pyindex 8b7f8e0ff8..11fb181a8d 100644--- a/autogpt_platform/backend/backend/api/features/experts/routes_test.py+++ b/autogpt_platform/backend/backend/api/features/experts/routes_test.py@@ -290,7 +290,41 @@ def test_hire_expert_returns_expert(assert response.status_code == 200data = response.json()assert data["expert"]["id"] == "expert-1"- mock_hire.assert_awaited_once_with(test_user_id, "template-1", None)+ mock_hire.assert_awaited_once_with(test_user_id, "template-1", None, None)+++def test_hire_expert_passes_the_hiring_surface_through(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ mock_hire = mocker.patch(+ "backend.api.features.experts.routes.experts_db.hire_expert",+ new_callable=AsyncMock,+ return_value=HireResult(expert=_make_expert()),+ )++ response = client.post(+ "/experts", json={"template_id": "template-1", "surface": "onboarding"}+ )++ assert response.status_code == 200+ mock_hire.assert_awaited_once_with(test_user_id, "template-1", None, "onboarding")+++def test_hire_expert_rejects_an_unknown_surface(+ mocker: pytest_mock.MockerFixture,+) -> None:+ mock_hire = mocker.patch(+ "backend.api.features.experts.routes.experts_db.hire_expert",+ new_callable=AsyncMock,+ )++ response = client.post(+ "/experts", json={"template_id": "template-1", "surface": "somewhere"}+ )++ assert response.status_code == 422+ mock_hire.assert_not_awaited()def test_hire_expert_twice_returns_same_expert_id(diff --git a/autogpt_platform/backend/backend/api/features/experts/spend_approval_routes_test.py b/autogpt_platform/backend/backend/api/features/experts/spend_approval_routes_test.pyindex 38b6a44334..b54fc85cd3 100644--- a/autogpt_platform/backend/backend/api/features/experts/spend_approval_routes_test.py+++ b/autogpt_platform/backend/backend/api/features/experts/spend_approval_routes_test.py@@ -139,7 +139,6 @@ async def test_run_agent_from_expert_chat_is_parked_and_says_so(mocker, parked)AsyncMock(return_value=library_agent),)mocker.patch.object(run_agent_mod, "emit_tool_display_name")- mocker.patch.object(run_agent_mod, "track_agent_run_success")mocker.patch.object(run_agent_mod, "_safe_link_to_chat_share", AsyncMock())session = make_session(user_id="owner", expert_id="expert-1")diff --git a/autogpt_platform/backend/backend/api/features/integrations/oauth_analytics_test.py b/autogpt_platform/backend/backend/api/features/integrations/oauth_analytics_test.pyindex 57e4d681fb..abe07161cf 100644--- a/autogpt_platform/backend/backend/api/features/integrations/oauth_analytics_test.py+++ b/autogpt_platform/backend/backend/api/features/integrations/oauth_analytics_test.py@@ -10,7 +10,7 @@ from pydantic import SecretStrfrom backend.api.features.integrations.router import routerfrom backend.data.model import OAuth2Credentials, OAuthState-from backend.util import product_analytics+from backend.util import posthog_clientapp = fastapi.FastAPI()app.include_router(router)@@ -33,7 +33,7 @@ def setup_auth(mock_jwt_user):@pytest.fixturedef capture(monkeypatch: pytest.MonkeyPatch) -> Mock:posthog = Mock()- monkeypatch.setattr(product_analytics, "get_posthog_client", lambda: posthog)+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: posthog)return posthog.capturediff --git a/autogpt_platform/backend/backend/api/features/orgs/regression_test.py b/autogpt_platform/backend/backend/api/features/orgs/regression_test.pyindex d08b2a809e..81ba0e5434 100644--- a/autogpt_platform/backend/backend/api/features/orgs/regression_test.py+++ b/autogpt_platform/backend/backend/api/features/orgs/regression_test.py@@ -1646,7 +1646,6 @@ class TestPR10WebhookTenancy:"backend.copilot.tools.run_agent._safe_link_to_chat_share",AsyncMock(),)- mocker.patch("backend.copilot.tools.run_agent.track_agent_run_success")@pytest.mark.asyncioasync def test_copilot_agent_run_passes_org_team_to_execution(self, mocker):@@ -1677,7 +1676,6 @@ class TestPR10WebhookTenancy:new_callable=AsyncMock,return_value=mock_lib_agent,)- mocker.patch("backend.copilot.tools.run_agent.track_agent_run_success")await tool._run_agent(user_id=USER_ID,diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex af32e6574f..a6b9ba79f8 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -1771,7 +1771,7 @@ async def stream_chat_completion_baseline(message_length=len(message or ""),expert_id=session.expert_id,origin=session.metadata.origin,- surface=session.metadata.source_platform,+ source_platform=session.metadata.source_platform,)# Capture count *before* the pending drain so is_first_turn and thediff --git a/autogpt_platform/backend/backend/copilot/briefing/generate_test.py b/autogpt_platform/backend/backend/copilot/briefing/generate_test.pyindex da3b02efe2..3349c59d08 100644--- a/autogpt_platform/backend/backend/copilot/briefing/generate_test.py+++ b/autogpt_platform/backend/backend/copilot/briefing/generate_test.py@@ -8,7 +8,7 @@ from backend.copilot.briefing.generate import AgentInfo, compose_briefingfrom backend.copilot.briefing.models import BriefingContentfrom backend.copilot.briefing.render import render_briefing_markdownfrom backend.data.execution import ExecutionStatus, GraphExecutionMeta-from backend.util import funnel_analytics+from backend.util import posthog_clientfrom backend.util.feature_flag import FlagNOW = datetime(2026, 8, 7, 9, 0, tzinfo=timezone.utc)@@ -1252,7 +1252,7 @@ async def test_generate_still_delivers_when_posthog_is_down(monkeypatch):"""Isolation lives inside emit_funnel_event, so the real emitter is left inplace and PostHog is what fails."""monkeypatch.setattr(- funnel_analytics,+ posthog_client,"get_posthog_client",MagicMock(side_effect=RuntimeError("analytics down")),)diff --git a/autogpt_platform/backend/backend/copilot/executor/utils.py b/autogpt_platform/backend/backend/copilot/executor/utils.pyindex 4fdf422b85..d1f206207d 100644--- a/autogpt_platform/backend/backend/copilot/executor/utils.py+++ b/autogpt_platform/backend/backend/copilot/executor/utils.py@@ -679,6 +679,7 @@ async def schedule_chat_turn(is_user_message: bool = True,expert_id: str | None = None,session_origin: str | None = None,+ session_source_platform: str | None = None,context: dict[str, str] | None = None,voice: bool = False,file_ids: list[str] | None = None,@@ -749,7 +750,7 @@ async def schedule_chat_turn(message_length=raw_message_length,expert_id=expert_id,origin=session_origin,- surface="chat",+ source_platform=session_source_platform,)if is_duplicate:diff --git a/autogpt_platform/backend/backend/copilot/executor/utils_test.py b/autogpt_platform/backend/backend/copilot/executor/utils_test.pyindex 1148112954..dab5ce82e2 100644--- a/autogpt_platform/backend/backend/copilot/executor/utils_test.py+++ b/autogpt_platform/backend/backend/copilot/executor/utils_test.py@@ -244,6 +244,42 @@ async def test_schedule_chat_turn_leaves_a_typed_message_alone() -> None:assert dispatch.await_args.kwargs["message"] == "what did I run yesterday"+async def test_schedule_chat_turn_tracks_the_session_channel() -> None:+ # A web message in a session opened from Discord still counts as Discord,+ # the same as the engines report it for turns they save themselves.+ slot = MagicMock(admitted=True)++ @asynccontextmanager+ async def acquire(*_args, **_kwargs):+ yield slot++ tracked = MagicMock()+ with (+ patch.object(utils, "acquire_turn_slot", new=acquire),+ patch("backend.copilot.model.append_and_save_message", new=AsyncMock()),+ patch("backend.copilot.tracking.track_user_message", new=tracked),+ patch.object(utils, "dispatch_turn", new=AsyncMock()),+ ):+ await utils.schedule_chat_turn(+ session_id="s1",+ user_id="u1",+ message="hello",+ expert_id="expert-1",+ session_origin="interactive",+ session_source_platform="discord",+ )++ tracked.assert_called_once_with(+ user_id="u1",+ session_id="s1",+ message_length=5,+ expert_id="expert-1",+ origin="interactive",+ source_platform="discord",+ )++@pytest.mark.asyncioasync def test_schedule_chat_turn_leaves_an_already_saved_message_alone() -> None:# The row was saved by an earlier call; prefixing now would put the twodiff --git a/autogpt_platform/backend/backend/copilot/gate/spend_test.py b/autogpt_platform/backend/backend/copilot/gate/spend_test.pyindex a4a4f200fb..38ac6373df 100644--- a/autogpt_platform/backend/backend/copilot/gate/spend_test.py+++ b/autogpt_platform/backend/backend/copilot/gate/spend_test.py@@ -352,7 +352,7 @@ async def test_a_workflow_run_charges_its_pre_flight_estimate(gate, chat):f"{_RUN}.execution_utils.add_graph_execution",AsyncMock(return_value=execution),),- patch(f"{_RUN}.track_agent_run_success"),+ patch(f"{_RUN}.track_chat_outcome"),):await RunAgentTool()._run_agent("user-1", _session(), graph, {}, {}, dry_run=Falsediff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 9dfb2be191..018b70699a 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -4668,7 +4668,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesmessage_length=len(message or ""),expert_id=session.expert_id,origin=session.metadata.origin,- surface=session.metadata.source_platform,+ source_platform=session.metadata.source_platform,)# Structured log prefix: [SDK][<session>][T<turn>]diff --git a/autogpt_platform/backend/backend/copilot/tools/expert_change_test.py b/autogpt_platform/backend/backend/copilot/tools/expert_change_test.pyindex 2eb3e987c5..a33d15be29 100644--- a/autogpt_platform/backend/backend/copilot/tools/expert_change_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/expert_change_test.py@@ -532,7 +532,7 @@ class TestConfirm:assert isinstance(resp, ExpertChangeAppliedResponse)assert resp.applied is Trueassert resp.kind == "hire"- db.hire_expert.assert_awaited_once_with(_USER, "tpl-scout", "Recon")+ db.hire_expert.assert_awaited_once_with(_USER, "tpl-scout", "Recon", "copilot")@pytest.mark.asyncio(loop_scope="session")async def test_confirm_raises_exactly_what_was_previewed(self):diff --git a/autogpt_platform/backend/backend/copilot/tools/expert_proposal.py b/autogpt_platform/backend/backend/copilot/tools/expert_proposal.pyindex b3bc56da96..c4c5db1a78 100644--- a/autogpt_platform/backend/backend/copilot/tools/expert_proposal.py+++ b/autogpt_platform/backend/backend/copilot/tools/expert_proposal.py@@ -339,6 +339,7 @@ async def _apply_hire(user_id,preview.template_id,preview.name or None,+ "copilot",)except Exception as e:return _hire_failure_response(e, session_id)diff --git a/autogpt_platform/backend/backend/copilot/tools/run_agent.py b/autogpt_platform/backend/backend/copilot/tools/run_agent.pyindex d490abae98..af7f1f9d3d 100644--- a/autogpt_platform/backend/backend/copilot/tools/run_agent.py+++ b/autogpt_platform/backend/backend/copilot/tools/run_agent.py@@ -21,7 +21,7 @@ from backend.copilot.gate.subject import ()from backend.copilot.model import ChatSessionfrom backend.copilot.tool_display import emit_tool_display_name-from backend.copilot.tracking import track_agent_run_success, track_agent_scheduled+from backend.copilot.tracking import track_chat_outcomefrom backend.copilot.tree import charge_creditsfrom backend.data.db_accessors import execution_db, graph_db, library_db, user_dbfrom backend.data.execution import (@@ -1037,16 +1037,14 @@ class RunAgentTool(BaseTool):session.successful_agent_runs.get(library_agent.graph_id, 0) + 1)await charge_credits(user_id, lambda: graph_cost_credits(graph))-- # Track in PostHog- track_agent_run_success(- user_id=user_id,- session_id=session_id,- graph_id=library_agent.graph_id,- graph_name=library_agent.name,- execution_id=execution.id,- library_agent_id=library_agent.id,- )+ track_chat_outcome(+ user_id,+ session_id,+ "agent_run_success",+ graph_id=library_agent.graph_id,+ execution_id=execution.id,+ library_agent_id=library_agent.id,+ )# If wait_for_result is requested, wait for execution to completeif wait_for_result > 0:@@ -1348,15 +1346,13 @@ class RunAgentTool(BaseTool):session.successful_agent_schedules[library_agent.graph_id] = (session.successful_agent_schedules.get(library_agent.graph_id, 0) + 1)-- # Track in PostHog- track_agent_scheduled(- user_id=user_id,- session_id=session_id,+ track_chat_outcome(+ user_id,+ session_id,+ "schedule_created",+ target="agent",graph_id=library_agent.graph_id,- graph_name=library_agent.name,schedule_id=result.id,- schedule_name=schedule_name,cron=cron,library_agent_id=library_agent.id,)diff --git a/autogpt_platform/backend/backend/copilot/tools/run_agent_test.py b/autogpt_platform/backend/backend/copilot/tools/run_agent_test.pyindex b468303ba8..78f8718e0f 100644--- a/autogpt_platform/backend/backend/copilot/tools/run_agent_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/run_agent_test.py@@ -1194,7 +1194,6 @@ async def test_run_agent_attributes_execution_to_session_org(mocker, expert_id):"backend.copilot.tools.run_agent.get_or_create_library_agent",AsyncMock(return_value=lib),)- mocker.patch("backend.copilot.tools.run_agent.track_agent_run_success")mocker.patch("backend.copilot.tools.run_agent._safe_link_to_chat_share", AsyncMock())@@ -1263,7 +1262,7 @@ async def test_run_agent_pauses_irreversible_actions_for_attended_chats("backend.copilot.tools.run_agent.get_or_create_library_agent",AsyncMock(return_value=lib),)- mocker.patch("backend.copilot.tools.run_agent.track_agent_run_success")+ mocker.patch("backend.copilot.tools.run_agent.track_chat_outcome")mocker.patch("backend.copilot.tools.run_agent._safe_link_to_chat_share", AsyncMock())@@ -1306,7 +1305,6 @@ async def test_run_agent_falls_back_to_default_team_for_tenantless_session(mocke"backend.copilot.tools.run_agent.get_or_create_library_agent",AsyncMock(return_value=lib),)- mocker.patch("backend.copilot.tools.run_agent.track_agent_run_success")mocker.patch("backend.copilot.tools.run_agent._safe_link_to_chat_share", AsyncMock())@@ -1504,7 +1502,6 @@ async def test_run_preset_executes_with_merged_inputs():"backend.copilot.tools.run_agent._safe_link_to_chat_share",new=AsyncMock(),),- patch("backend.copilot.tools.run_agent.track_agent_run_success"),):result = await tool._handle_preset_run("preset-user", session, RunAgentInput(preset_id="p1", inputs={"b": 99})@@ -1674,7 +1671,6 @@ def _completed_run_mocks("backend.copilot.tools.run_agent.get_or_create_library_agent",AsyncMock(return_value=lib),)- mocker.patch("backend.copilot.tools.run_agent.track_agent_run_success")mocker.patch("backend.copilot.tools.run_agent._safe_link_to_chat_share", AsyncMock())@@ -1802,6 +1798,28 @@ async def test_wet_run_omits_node_trace_dry_run_inlines_it(mocker):assert dry.execution.node_executions[0]["status"] == "FAILED"[email protected](loop_scope="session")[email protected]("dry_run", [False, True])+async def test_only_a_real_run_reports_agent_run_success(mocker, dry_run):+ _completed_run_mocks(mocker, outputs={"result": ["ok"]}, node_executions=[])+ mocker.patch("backend.copilot.tools.run_agent.charge_credits", AsyncMock())+ tracked = mocker.patch("backend.copilot.tools.run_agent.track_chat_outcome")++ await _run_waited(mocker, dry_run=dry_run)++ if dry_run:+ tracked.assert_not_called()+ else:+ tracked.assert_called_once_with(+ "user-1",+ mocker.ANY,+ "agent_run_success",+ graph_id="graph-1",+ execution_id="exec-1",+ library_agent_id="lib-1",+ )++@pytest.mark.asyncio(loop_scope="session")async def test_detailed_fetch_failure_degrades_to_summary(mocker):"""When the per-node trace fetch raises, the run response still returnsdiff --git a/autogpt_platform/backend/backend/copilot/tools/schedule_followup.py b/autogpt_platform/backend/backend/copilot/tools/schedule_followup.pyindex 5968283c23..eda1f8bbe3 100644--- a/autogpt_platform/backend/backend/copilot/tools/schedule_followup.py+++ b/autogpt_platform/backend/backend/copilot/tools/schedule_followup.py@@ -38,7 +38,7 @@ from apscheduler.triggers.cron import CronTriggerfrom backend.copilot.model import ChatSession, get_chat_sessionfrom backend.copilot.tools.session_context import is_followups_feature_enabled-from backend.copilot.tracking import track_followup_scheduled+from backend.copilot.tracking import track_chat_outcomefrom backend.data.db_accessors import user_dbfrom backend.util.clients import get_scheduler_clientfrom backend.util.timezone_utils import get_user_timezone_or_utc@@ -279,10 +279,13 @@ class ScheduleFollowupTool(BaseTool):)is_recurring = cron is not None- track_followup_scheduled(- user_id=user_id,- session_id=target_session_id,+ track_chat_outcome(+ user_id,+ current_session_id,+ "schedule_created",+ target="followup",schedule_id=info.id,+ target_chat_session_id=target_session_id,is_recurring=is_recurring,)if target_session_id is None:diff --git a/autogpt_platform/backend/backend/copilot/tools/schedule_followup_test.py b/autogpt_platform/backend/backend/copilot/tools/schedule_followup_test.pyindex 69e4852392..6c9b53806f 100644--- a/autogpt_platform/backend/backend/copilot/tools/schedule_followup_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/schedule_followup_test.py@@ -453,10 +453,13 @@ async def test_returns_feature_disabled_when_flag_off(tool):side_effect=AssertionError("scheduler must not be touched when flag off"))- with patch(- f"{_TOOL_PATH}.is_followups_feature_enabled",- new=AsyncMock(return_value=False),- ), patch(f"{_TOOL_PATH}.get_scheduler_client", return_value=scheduler_client):+ with (+ patch(+ f"{_TOOL_PATH}.is_followups_feature_enabled",+ new=AsyncMock(return_value=False),+ ),+ patch(f"{_TOOL_PATH}.get_scheduler_client", return_value=scheduler_client),+ ):result = await tool._execute(user_id=_USER,session=session,@@ -467,3 +470,45 @@ async def test_returns_feature_disabled_when_flag_off(tool):assert isinstance(result, ErrorResponse)assert result.error == "feature_disabled"scheduler_client.add_copilot_turn_schedule.assert_not_awaited()++[email protected]("target", [None, "session-other-owned-by-same-user"])+async def test_schedule_created_is_attributed_to_the_scheduling_chat(+ tool, session, target+):+ # The event belongs to the chat the user was in; the target it fires+ # into (None = a fresh chat) rides along as target_chat_session_id.+ mock_user_db = MagicMock()+ mock_user_db().get_user_by_id = AsyncMock(return_value=MagicMock(timezone="UTC"))+ mock_client = AsyncMock()+ mock_client.add_copilot_turn_schedule = AsyncMock(return_value=_info())+ tracked = MagicMock()++ with (+ patch(f"{_TOOL_PATH}.user_db", return_value=mock_user_db()),+ patch(f"{_TOOL_PATH}.get_scheduler_client", return_value=mock_client),+ patch(+ f"{_TOOL_PATH}.get_chat_session",+ new=AsyncMock(return_value=MagicMock(expert_id=None)),+ ),+ patch(f"{_TOOL_PATH}.track_chat_outcome", new=tracked),+ ):+ result = await tool._execute(+ user_id=_USER,+ session=session,+ message="check CI",+ delay_seconds=600,+ session_id=target,+ )++ assert isinstance(result, ScheduleCreatedResponse)+ tracked.assert_called_once_with(+ _USER,+ session.session_id,+ "schedule_created",+ target="followup",+ schedule_id="cop-1",+ target_chat_session_id=target,+ is_recurring=False,+ )diff --git a/autogpt_platform/backend/backend/copilot/tools/test_session_dry_run.py b/autogpt_platform/backend/backend/copilot/tools/test_session_dry_run.pyindex aa18256c0e..64aaa3fe64 100644--- a/autogpt_platform/backend/backend/copilot/tools/test_session_dry_run.py+++ b/autogpt_platform/backend/backend/copilot/tools/test_session_dry_run.py@@ -211,7 +211,6 @@ class TestRunAgentToolSessionDryRun:new=AsyncMock(return_value=(None, None)),),patch("backend.copilot.tools.run_agent.execution_utils") as mock_exec_utils,- patch("backend.copilot.tools.run_agent.track_agent_run_success"),):mock_exec_utils.add_graph_execution = AsyncMock(return_value=mock_execution)diff --git a/autogpt_platform/backend/backend/copilot/tracking.py b/autogpt_platform/backend/backend/copilot/tracking.pyindex 4539c75fc2..1290fc3a66 100644--- a/autogpt_platform/backend/backend/copilot/tracking.py+++ b/autogpt_platform/backend/backend/copilot/tracking.py@@ -1,23 +1,20 @@-"""PostHog analytics tracking for the chat system."""+"""PostHog analytics tracking for the chat system.-import logging-from typing import Any+Event names are the product analytics plan's ``chat_*`` family. The events+captured here carry ``source: chat_copilot``; ``chat_message_sent`` goes+through ``product_analytics`` and carries ``source: platform``. Events are only+sent for a known user: a synthetic distinct id would create a PostHog person+nobody can merge.+"""-from backend.util import product_analytics-from backend.util.posthog_client import get_posthog_client as _get_posthog_client-from backend.util.posthog_events import PostHogEvent-from backend.util.settings import Settings+from typing import Any, Literal-logger = logging.getLogger(__name__)-settings = Settings()+from backend.util import posthog_client, product_analytics+from backend.util.posthog_events import PostHogEvent+SOURCE = "chat_copilot"-def _get_base_properties() -> dict[str, Any]:- """Get base properties included in all events."""- return {- "environment": settings.config.app_env.value,- "source": "chat_copilot",- }+ChatOutcomeType = Literal["agent_run_success", "schedule_created"]def track_user_message(@@ -27,47 +24,32 @@ def track_user_message(*,expert_id: str | None = None,origin: str | None = None,- surface: str | None = None,+ source_platform: str | None = None,) -> None:"""Track when a user sends a message in chat.+ One ``chat_message_sent`` per turn (``expert_id`` set in an expert chat),+ carrying the message length.+Args:- user_id: The user's ID (or None for anonymous)+ user_id: The user's ID; no event without onesession_id: The chat session IDmessage_length: Length of the user's messageexpert_id: Expert the session is scoped to, if any- origin: Session origin ("interactive" | "automation"), when known- surface: Where the message came from (web chat, slack, telegram, ...)+ origin: Session origin ("interactive" | "automation"), when known;+ automation turns are not sent+ source_platform: The bot platform the message came from (slack,+ discord, ...); unset for the web app"""- # The activation event (run_autopilot / run_expert) is the one GTM- # dashboards and experiments build on; copilot_message_sent stays for- # the existing copilot dashboards.product_analytics.track_chat_turn(user_id=user_id,session_id=session_id,expert_id=expert_id,origin=origin,- surface=surface,+ source_platform=source_platform,+ message_length=message_length,)- client = _get_posthog_client()- if not client:- return-- try:- properties = {- **_get_base_properties(),- "session_id": session_id,- "message_length": message_length,- }- client.capture(- distinct_id=user_id or f"anonymous_{session_id}",- event=PostHogEvent.COPILOT_MESSAGE_SENT.value,- properties=properties,- )- except Exception as e:- logger.warning(f"Failed to track user message: {e}")-def track_tool_called(user_id: str | None,@@ -78,197 +60,44 @@ def track_tool_called("""Track when a tool is called in chat.Args:- user_id: The user's ID (or None for anonymous)+ user_id: The user's ID; no event without onesession_id: The chat session IDtool_name: Name of the tool being calledtool_call_id: Unique ID of the tool call"""- client = _get_posthog_client()- if not client:- logger.info("PostHog client not available for tool tracking")- return-- try:- properties = {- **_get_base_properties(),- "session_id": session_id,+ posthog_client.capture(+ user_id,+ PostHogEvent.CHAT_TOOL_CALLED,+ {+ "chat_session_id": session_id,"tool_name": tool_name,"tool_call_id": tool_call_id,- }- distinct_id = user_id or f"anonymous_{session_id}"- logger.info(- f"Sending copilot_tool_called event to PostHog: distinct_id={distinct_id}, "- f"tool_name={tool_name}"- )- client.capture(- distinct_id=distinct_id,- event=PostHogEvent.COPILOT_TOOL_CALLED.value,- properties=properties,- )- except Exception as e:- logger.warning(f"Failed to track tool call: {e}")---def track_agent_run_success(- user_id: str,- session_id: str,- graph_id: str,- graph_name: str,- execution_id: str,- library_agent_id: str,-) -> None:- """Track when an agent is successfully run.-- Args:- user_id: The user's ID- session_id: The chat session ID- graph_id: ID of the agent graph- graph_name: Name of the agent- execution_id: ID of the execution- library_agent_id: ID of the library agent- """- client = _get_posthog_client()- if not client:- return-- try:- properties = {- **_get_base_properties(),- "session_id": session_id,- "graph_id": graph_id,- "graph_name": graph_name,- "execution_id": execution_id,- "library_agent_id": library_agent_id,- }- client.capture(- distinct_id=user_id,- event=PostHogEvent.COPILOT_AGENT_RUN_SUCCESS.value,- properties=properties,- )- except Exception as e:- logger.warning(f"Failed to track agent run: {e}")---def track_agent_scheduled(- user_id: str,- session_id: str,- graph_id: str,- graph_name: str,- schedule_id: str,- schedule_name: str,- cron: str,- library_agent_id: str,-) -> None:- """Track when an agent is successfully scheduled.-- Args:- user_id: The user's ID- session_id: The chat session ID- graph_id: ID of the agent graph- graph_name: Name of the agent- schedule_id: ID of the schedule- schedule_name: Name of the schedule- cron: Cron expression for the schedule- library_agent_id: ID of the library agent- """- client = _get_posthog_client()- if not client:- return-- try:- properties = {- **_get_base_properties(),- "session_id": session_id,- "graph_id": graph_id,- "graph_name": graph_name,- "schedule_id": schedule_id,- "schedule_name": schedule_name,- "cron": cron,- "library_agent_id": library_agent_id,- }- client.capture(- distinct_id=user_id,- event=PostHogEvent.COPILOT_AGENT_SCHEDULED.value,- properties=properties,- )- except Exception as e:- logger.warning(f"Failed to track agent schedule: {e}")---def track_followup_scheduled(- user_id: str,- session_id: str | None,- schedule_id: str,- is_recurring: bool,-) -> None:- """Track when the copilot schedules a follow-up turn on itself.-- Args:- user_id: The user's ID- session_id: The chat session ID being followed up. ``None`` for- the fresh-chat sentinel (the destination session doesn't- exist yet — it will be created when the schedule fires).- schedule_id: ID of the created schedule- is_recurring: True if cron, False if one-shot- """- client = _get_posthog_client()- if not client:- return-- try:- properties = {- **_get_base_properties(),- "session_id": session_id,- "schedule_id": schedule_id,- "is_recurring": is_recurring,- }- client.capture(- distinct_id=user_id,- event=PostHogEvent.COPILOT_FOLLOWUP_SCHEDULED.value,- properties=properties,- )- except Exception as e:- logger.warning(f"Failed to track followup schedule: {e}")+ },+ source=SOURCE,+ )-def track_trigger_setup(+def track_chat_outcome(user_id: str,session_id: str,- graph_id: str,- graph_name: str,- trigger_type: str,- library_agent_id: str,+ outcome_type: ChatOutcomeType,+ **properties: Any,) -> None:- """Track when a trigger is set up for an agent.+ """Track a moment of value in a chat: the copilot ran or scheduled+ something for the user. ``outcome_type`` uses the analytics plan's values.Args:user_id: The user's ID- session_id: The chat session ID- graph_id: ID of the agent graph- graph_name: Name of the agent- trigger_type: Type of trigger (e.g., 'webhook')- library_agent_id: ID of the library agent+ session_id: The chat session the outcome happened in+ outcome_type: Which kind of result the chat produced+ properties: Ids describing the result (``graph_id``, ...)"""- client = _get_posthog_client()- if not client:- return-- try:- properties = {- **_get_base_properties(),- "session_id": session_id,- "graph_id": graph_id,- "graph_name": graph_name,- "trigger_type": trigger_type,- "library_agent_id": library_agent_id,- }- client.capture(- distinct_id=user_id,- event=PostHogEvent.COPILOT_TRIGGER_SETUP.value,- properties=properties,- )- except Exception as e:- logger.warning(f"Failed to track trigger setup: {e}")+ posthog_client.capture(+ user_id,+ PostHogEvent.CHAT_OUTCOME,+ {**properties, "chat_session_id": session_id, "outcome_type": outcome_type},+ source=SOURCE,+ )def track_library_check_outcome(@@ -290,22 +119,14 @@ def track_library_check_outcome(threshold) — included so sub-threshold near-misses caninform retuning ``LIBRARY_SIMILARITY_THRESHOLD``."""- client = _get_posthog_client()- if not client:- return-- try:- properties = {- **_get_base_properties(),- "session_id": session_id,+ posthog_client.capture(+ user_id,+ PostHogEvent.CHAT_LIBRARY_CHECK_OUTCOME,+ {+ "chat_session_id": session_id,"outcome": outcome,"matches_count": matches_count,"top_score": top_score,- }- client.capture(- distinct_id=user_id,- event=PostHogEvent.COPILOT_LIBRARY_CHECK_OUTCOME.value,- properties=properties,- )- except Exception as e:- logger.warning(f"Failed to track library check outcome: {e}")+ },+ source=SOURCE,+ )diff --git a/autogpt_platform/backend/backend/copilot/tracking_test.py b/autogpt_platform/backend/backend/copilot/tracking_test.pynew file mode 100644index 0000000000..2bbcd2530f--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/tracking_test.py@@ -0,0 +1,80 @@+"""Chat analytics: one event per turn, and no event without a user."""++from unittest.mock import Mock++import pytest++from backend.copilot import tracking+from backend.util import posthog_client+++def capture(monkeypatch: pytest.MonkeyPatch) -> Mock:+ client = Mock()+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: client)+ return client.capture+++def test_a_chat_turn_is_one_event_carrying_the_message_length(capture: Mock) -> None:+ tracking.track_user_message("user-1", "session-1", 42)++ capture.assert_called_once()+ kwargs = capture.call_args.kwargs+ assert kwargs["event"] == "chat_message_sent"+ assert kwargs["properties"]["message_length"] == 42+ assert kwargs["properties"]["chat_session_id"] == "session-1"+++def test_an_expert_chat_turn_is_the_same_event_with_the_expert(capture: Mock) -> None:+ tracking.track_user_message("user-1", "session-1", 7, expert_id="expert-1")++ capture.assert_called_once()+ assert capture.call_args.kwargs["event"] == "chat_message_sent"+ assert capture.call_args.kwargs["properties"]["expert_id"] == "expert-1"+++def test_a_turn_without_a_user_sends_nothing(capture: Mock) -> None:+ tracking.track_user_message(None, "session-1", 42)++ capture.assert_not_called()+++def test_a_tool_call_carries_the_copilot_source(capture: Mock) -> None:+ tracking.track_tool_called("user-1", "session-1", "run_agent", "call-1")++ kwargs = capture.call_args.kwargs+ assert kwargs["distinct_id"] == "user-1"+ assert kwargs["event"] == "chat_tool_called"+ assert kwargs["properties"]["source"] == "chat_copilot"+ assert kwargs["properties"]["tool_name"] == "run_agent"+++def test_a_tool_call_without_a_user_is_not_sent_under_a_made_up_id(+ capture: Mock,+) -> None:+ tracking.track_tool_called(None, "session-1", "run_agent", "call-1")++ capture.assert_not_called()+++def test_the_library_check_outcome_carries_the_copilot_source(capture: Mock) -> None:+ tracking.track_library_check_outcome("user-1", "session-1", "no_matches")++ kwargs = capture.call_args.kwargs+ assert kwargs["event"] == "chat_library_check_outcome"+ assert kwargs["properties"]["source"] == "chat_copilot"+ assert kwargs["properties"]["outcome"] == "no_matches"+++def test_a_chat_outcome_carries_its_type_and_session(capture: Mock) -> None:+ tracking.track_chat_outcome(+ "user-1", "session-1", "agent_run_success", graph_id="graph-1"+ )++ kwargs = capture.call_args.kwargs+ assert kwargs["distinct_id"] == "user-1"+ assert kwargs["event"] == "chat_outcome"+ assert kwargs["properties"]["outcome_type"] == "agent_run_success"+ assert kwargs["properties"]["chat_session_id"] == "session-1"+ assert kwargs["properties"]["graph_id"] == "graph-1"+ assert kwargs["properties"]["source"] == "chat_copilot"diff --git a/autogpt_platform/backend/backend/copilot/turn_queue.py b/autogpt_platform/backend/backend/copilot/turn_queue.pyindex 845426d00e..31ec7bcf06 100644--- a/autogpt_platform/backend/backend/copilot/turn_queue.py+++ b/autogpt_platform/backend/backend/copilot/turn_queue.py@@ -438,7 +438,7 @@ async def dispatch_next_for_user(user_id: str) -> bool:message_length=len(pending.content),expert_id=head.expert_id,origin=head.metadata.origin,- surface="chat",+ source_platform=head.metadata.source_platform,)except Exception:logger.warning("Failed to track promoted chat turn", exc_info=True)diff --git a/autogpt_platform/backend/backend/copilot/turn_queue_test.py b/autogpt_platform/backend/backend/copilot/turn_queue_test.pyindex b8d1f1d37b..7c04a1d1dd 100644--- a/autogpt_platform/backend/backend/copilot/turn_queue_test.py+++ b/autogpt_platform/backend/backend/copilot/turn_queue_test.py@@ -223,6 +223,7 @@ async def test_try_enqueue_turn_raises_when_at_inflight_cap() -> None:("expert_id","origin",+ "source_platform","role","claim","dispatch_fails",@@ -230,18 +231,20 @@ async def test_try_enqueue_turn_raises_when_at_inflight_cap() -> None:"expected_event",),[- ("expert-1", "interactive", "user", True, False, False, True),- (None, "automation", "user", True, False, False, True),- ("expert-1", "interactive", "user", False, False, False, False),- ("expert-1", "interactive", "user", True, True, False, False),- ("expert-1", "interactive", "assistant", True, False, False, False),- ("expert-1", "interactive", "user", True, False, True, True),+ ("expert-1", "interactive", None, "user", True, False, False, True),+ (None, "interactive", "discord", "user", True, False, False, True),+ (None, "automation", None, "user", True, False, False, True),+ ("expert-1", "interactive", None, "user", False, False, False, False),+ ("expert-1", "interactive", None, "user", True, True, False, False),+ ("expert-1", "interactive", None, "assistant", True, False, False, False),+ ("expert-1", "interactive", None, "user", True, False, True, True),],)async def test_promoted_turn_tracking_preserves_session_attribution(tracked_message: MagicMock,expert_id: str | None,origin: str,+ source_platform: str | None,role: str,claim: bool,dispatch_fails: bool,@@ -253,6 +256,7 @@ async def test_promoted_turn_tracking_preserves_session_attribution(head = _mock_session()head.expert_id = expert_idhead.metadata.origin = origin+ head.metadata.source_platform = source_platformhead.metadata.llm_auth_provider = "codex"pending = _pyd_message(role=role)db = MagicMock()@@ -284,7 +288,7 @@ async def test_promoted_turn_tracking_preserves_session_attribution(message_length=5,expert_id=expert_id,origin=origin,- surface="chat",+ source_platform=source_platform,)else:tracked_message.assert_not_called()diff --git a/autogpt_platform/backend/backend/data/analytics.py b/autogpt_platform/backend/backend/data/analytics.pyindex 2e148cd10f..05a0a8b6e4 100644--- a/autogpt_platform/backend/backend/data/analytics.py+++ b/autogpt_platform/backend/backend/data/analytics.py@@ -2,11 +2,9 @@ import loggingfrom datetime import datetime, timedelta, timezonefrom typing import Optional-import prisma.typesfrom pydantic import BaseModelfrom backend.data.db import query_raw_with_schema-from backend.util.json import SafeJsonlogger = logging.getLogger(__name__)@@ -40,44 +38,6 @@ class AccuracyTrendsResponse(BaseModel):historical_data: Optional[list[AccuracyLatestData]] = None-async def log_raw_analytics(- user_id: str,- type: str,- data: dict,- data_index: str,-):- details = await prisma.models.AnalyticsDetails.prisma().create(- data=prisma.types.AnalyticsDetailsCreateInput(- userId=user_id,- type=type,- data=SafeJson(data),- dataIndex=data_index,- )- )- return details---async def log_raw_metric(- user_id: str,- metric_name: str,- metric_value: float,- data_string: str,-):- if metric_value < 0:- raise ValueError("metric_value must be non-negative")-- result = await prisma.models.AnalyticsMetrics.prisma().create(- data=prisma.types.AnalyticsMetricsCreateInput(- value=metric_value,- analyticMetric=metric_name,- userId=user_id,- dataString=data_string,- )- )-- return result--async def get_accuracy_trends_and_alerts(graph_id: str,days_back: int = 30,diff --git a/autogpt_platform/backend/backend/data/credit.py b/autogpt_platform/backend/backend/data/credit.pyindex 3f683cb314..fdf284a5d5 100644--- a/autogpt_platform/backend/backend/data/credit.py+++ b/autogpt_platform/backend/backend/data/credit.py@@ -5,7 +5,6 @@ from abc import ABC, abstractmethodfrom datetime import datetime, timedelta, timezonefrom typing import TYPE_CHECKING, Any, Literal, cast-import posthogimport stripefrom prisma.enums import (CreditRefundRequestStatus,@@ -46,6 +45,7 @@ from backend.data.subscription_checkout import (from backend.data.subscription_trial_stripe import reconcile_trial_subscriptionfrom backend.data.user import get_user_by_id, get_user_email_by_idfrom backend.notifications.queue import queue_notification_async+from backend.util import posthog_clientfrom backend.util.cache import cachedfrom backend.util.exceptions import InsufficientBalanceErrorfrom backend.util.feature_flag import Flag, get_feature_flag_value@@ -62,9 +62,6 @@ if TYPE_CHECKING:settings = Settings()stripe.api_key = settings.secrets.stripe_api_key-if settings.secrets.posthog_api_key:- posthog.api_key = settings.secrets.posthog_api_key- posthog.host = settings.secrets.posthog_hostlogger = logging.getLogger(__name__)base_url = settings.config.frontend_base_url or settings.config.platform_base_url@@ -1180,7 +1177,7 @@ class UserCredit(UserCreditBase):# webhook/retry replays don't double-emit.if activation is not None and amount > 0:_track_billing_event(- PostHogEvent.CREDIT_TOPUP_SUCCESS,+ PostHogEvent.TOPUP_COMPLETED,user_id,{"amount_credits": amount,@@ -1323,7 +1320,7 @@ class UserCredit(UserCreditBase):)if activation is not None:_track_billing_event(- PostHogEvent.CREDIT_TOPUP_SUCCESS,+ PostHogEvent.TOPUP_COMPLETED,credit_transaction.userId,{"amount_credits": credit_transaction.amount,@@ -2199,9 +2196,10 @@ async def modify_stripe_subscription_for_tier(# the DB flip fails, so gating here avoids double-firing on success.if db_flip_succeeded and is_tier_upgrade(current_tier, tier):_track_billing_event(- PostHogEvent.SUBSCRIPTION_UPGRADED,+ PostHogEvent.SUBSCRIPTION_CHANGED,user_id,{+ "change_type": "upgrade","previous_subscription_tier": current_tier.value,"subscription_tier": tier.value,"billing_cycle": billing_cycle,@@ -2916,9 +2914,10 @@ async def _sync_subscription_tier_from_stripe(stripe_subscription: dict) -> Nonemetadata.get("billing_cycle") if isinstance(metadata, dict) else None)_track_billing_event(- PostHogEvent.SUBSCRIPTION_UPGRADED,+ PostHogEvent.SUBSCRIPTION_CHANGED,user.id,{+ "change_type": "upgrade","previous_subscription_tier": current_tier.value,"subscription_tier": tier.value,"billing_cycle": billing_cycle,@@ -2990,9 +2989,7 @@ def _invoice_subscription_id(invoice: dict) -> str:return legacy if isinstance(legacy, str) and legacy else ""-TIER_RECONCILIATION_DISCREPANCY_EVENT = (- PostHogEvent.SUBSCRIPTION_TIER_RECONCILIATION_DISCREPANCY-)+TIER_RECONCILIATION_DISCREPANCY_EVENT = PostHogEvent.SUBSCRIPTION_TIER_RECONCILEDdef log_tier_reconciliation_discrepancy(@@ -3055,26 +3052,13 @@ async def alert_tier_reconciliation_discrepancy(message: str) -> None:def _track_billing_event(event: PostHogEvent, distinct_id: str, properties: dict[str, Any]) -> None:- if not settings.secrets.posthog_api_key:- return-- try:- posthog.capture(- event=event.value,- distinct_id=distinct_id,- properties=properties,- )- except Exception:- logger.warning(- "failed to track billing event %s for user %s",- event,- distinct_id,- exc_info=True,- )+ # The shared client, never the posthog module's globals: another library+ # (graphiti-core) configures those for its own telemetry (SECRT-2710).+ posthog_client.capture(distinct_id, event, properties)async def _track_subscription_payment_success(user: User, invoice: dict) -> None:- if not settings.secrets.posthog_api_key:+ if posthog_client.get_posthog_client() is None:returntry:@@ -3087,13 +3071,10 @@ async def _track_subscription_payment_success(user: User, invoice: dict) -> Noneawait get_user_billing_cycle(user.id) or "monthly")- posthog.capture(- event=PostHogEvent.SUBSCRIPTION_PAYMENT_SUCCESS.value,- distinct_id=user.id,- properties={- "subscription_tier": tier,- "billing_cycle": billing_cycle,- },+ _track_billing_event(+ PostHogEvent.PAYMENT_SUCCEEDED,+ user.id,+ {"subscription_tier": tier, "billing_cycle": billing_cycle},)except Exception:logger.warning(diff --git a/autogpt_platform/backend/backend/data/credit_subscription_test.py b/autogpt_platform/backend/backend/data/credit_subscription_test.pyindex e27394b5b0..3194076cc6 100644--- a/autogpt_platform/backend/backend/data/credit_subscription_test.py+++ b/autogpt_platform/backend/backend/data/credit_subscription_test.py@@ -58,6 +58,10 @@ def _clear_active_subscription_cache():yield+# What the shared PostHog client adds to every event (see posthog_client.capture).+BASE_PROPERTIES = {"environment": "test", "source": "platform"}++@pytest.mark.asyncioasync def test_set_subscription_tier_updates_db():with (@@ -175,20 +179,25 @@ async def test_sync_subscription_from_stripe_tracks_upgrade():"backend.data.credit._cleanup_stale_subscriptions", new_callable=AsyncMock),patch("backend.data.credit.set_subscription_tier", new_callable=AsyncMock),- patch("backend.data.credit.settings.secrets.posthog_api_key", new="phc_test"),- patch("backend.data.credit.posthog.capture", new=track_mock),+ patch(+ "backend.util.posthog_client.get_posthog_client",+ return_value=MagicMock(capture=track_mock),+ ),+ patch("backend.util.posthog_client._environment", return_value="test"),patch.object(get_pending_subscription_change, "cache_delete"),):await sync_subscription_from_stripe(stripe_sub)track_mock.assert_called_once()_, kwargs = track_mock.call_args- assert kwargs["event"] == "subscription_upgraded"+ assert kwargs["event"] == "subscription_changed"assert kwargs["distinct_id"] == "user-1"assert kwargs["properties"] == {+ "change_type": "upgrade","previous_subscription_tier": "BASIC","subscription_tier": "PRO","billing_cycle": "yearly",+ **BASE_PROPERTIES,}@@ -604,8 +613,11 @@ async def test_cancel_stripe_subscription_tracks_cancellation():new_callable=AsyncMock,return_value=1,),- patch("backend.data.credit.settings.secrets.posthog_api_key", new="phc_test"),- patch("backend.data.credit.posthog.capture", new=track_mock),+ patch(+ "backend.util.posthog_client.get_posthog_client",+ return_value=MagicMock(capture=track_mock),+ ),+ patch("backend.util.posthog_client._environment", return_value="test"),patch.object(get_pending_subscription_change, "cache_delete"),):result = await cancel_stripe_subscription("user-1")@@ -615,7 +627,10 @@ async def test_cancel_stripe_subscription_tracks_cancellation():_, kwargs = track_mock.call_argsassert kwargs["event"] == "subscription_cancellation_scheduled"assert kwargs["distinct_id"] == "user-1"- assert kwargs["properties"] == {"subscription_tier": "PRO"}+ assert kwargs["properties"] == {+ "subscription_tier": "PRO",+ **BASE_PROPERTIES,+ }@pytest.mark.asyncio@@ -1985,8 +2000,11 @@ async def test_handle_subscription_payment_success_tracks_paid_plan_when_grants_"backend.data.credit.UserCredit._add_transaction",new=AsyncMock(),) as add_tx_mock,- patch("backend.data.credit.settings.secrets.posthog_api_key", new="phc_test"),- patch("backend.data.credit.posthog.capture", new=track_mock),+ patch(+ "backend.util.posthog_client.get_posthog_client",+ return_value=MagicMock(capture=track_mock),+ ),+ patch("backend.util.posthog_client._environment", return_value="test"),_patch_credit_grant_config(False),):await handle_subscription_payment_success(invoice)@@ -1994,7 +2012,7 @@ async def test_handle_subscription_payment_success_tracks_paid_plan_when_grants_add_tx_mock.assert_not_called()track_mock.assert_called_once()_, kwargs = track_mock.call_args- assert kwargs["event"] == "subscription_payment_success"+ assert kwargs["event"] == "payment_succeeded"assert kwargs["distinct_id"] == "user-1"assert kwargs["properties"]["subscription_tier"] == "PRO"assert kwargs["properties"]["billing_cycle"] == "yearly"@@ -2022,8 +2040,11 @@ async def test_handle_subscription_payment_success_metadata_absent_uses_user_tienew_callable=AsyncMock,return_value="monthly",),- patch("backend.data.credit.settings.secrets.posthog_api_key", new="phc_test"),- patch("backend.data.credit.posthog.capture", new=track_mock),+ patch(+ "backend.util.posthog_client.get_posthog_client",+ return_value=MagicMock(capture=track_mock),+ ),+ patch("backend.util.posthog_client._environment", return_value="test"),_patch_credit_grant_config(False),):await handle_subscription_payment_success(invoice)@@ -2056,8 +2077,11 @@ async def test_handle_subscription_payment_success_reads_parent_metadata():return_value=MagicMock(find_first=AsyncMock(return_value=mock_user)),),patch("backend.data.credit.UserCredit._add_transaction", new=AsyncMock()),- patch("backend.data.credit.settings.secrets.posthog_api_key", new="phc_test"),- patch("backend.data.credit.posthog.capture", new=track_mock),+ patch(+ "backend.util.posthog_client.get_posthog_client",+ return_value=MagicMock(capture=track_mock),+ ),+ patch("backend.util.posthog_client._environment", return_value="test"),_patch_credit_grant_config(False),):await handle_subscription_payment_success(invoice)@@ -2088,8 +2112,11 @@ async def test_handle_subscription_payment_success_swallows_posthog_errors():return_value=MagicMock(find_first=AsyncMock(return_value=mock_user)),),patch("backend.data.credit.UserCredit._add_transaction", new=AsyncMock()),- patch("backend.data.credit.settings.secrets.posthog_api_key", new="phc_test"),- patch("backend.data.credit.posthog.capture", new=track_mock),+ patch(+ "backend.util.posthog_client.get_posthog_client",+ return_value=MagicMock(capture=track_mock),+ ),+ patch("backend.util.posthog_client._environment", return_value="test"),_patch_credit_grant_config(False),):await handle_subscription_payment_success(invoice)@@ -2117,8 +2144,7 @@ async def test_handle_subscription_payment_success_skips_tracking_when_posthog_dreturn_value=MagicMock(find_first=AsyncMock(return_value=mock_user)),),patch("backend.data.credit.UserCredit._add_transaction", new=AsyncMock()),- patch("backend.data.credit.settings.secrets.posthog_api_key", new=""),- patch("backend.data.credit.posthog.capture", new=track_mock),+ patch("backend.util.posthog_client.get_posthog_client", return_value=None),_patch_credit_grant_config(False),):await handle_subscription_payment_success(invoice)@@ -2487,18 +2513,22 @@ async def test_top_up_credits_tracks_success():"backend.data.credit.stripe.PaymentIntent.create_async",return_value=payment_intent,),- patch("backend.data.credit.settings.secrets.posthog_api_key", new="phc_test"),- patch("backend.data.credit.posthog.capture", new=track_mock),+ patch(+ "backend.util.posthog_client.get_posthog_client",+ return_value=MagicMock(capture=track_mock),+ ),+ patch("backend.util.posthog_client._environment", return_value="test"),):await credit_system._top_up_credits("user-1", 500)track_mock.assert_called_once()_, kwargs = track_mock.call_args- assert kwargs["event"] == "credit_topup_success"+ assert kwargs["event"] == "topup_completed"assert kwargs["distinct_id"] == "user-1"assert kwargs["properties"] == {"amount_credits": 500,"top_up_type": "UNCATEGORIZED",+ **BASE_PROPERTIES,}@@ -2536,18 +2566,22 @@ async def test_fulfill_checkout_tracks_credit_topup_success():new_callable=AsyncMock,return_value=(2500, "pi_test_topup"),),- patch("backend.data.credit.settings.secrets.posthog_api_key", new="phc_test"),- patch("backend.data.credit.posthog.capture", new=track_mock),+ patch(+ "backend.util.posthog_client.get_posthog_client",+ return_value=MagicMock(capture=track_mock),+ ),+ patch("backend.util.posthog_client._environment", return_value="test"),):await credit_system.fulfill_checkout(session_id="cs_test_topup")track_mock.assert_called_once()_, kwargs = track_mock.call_args- assert kwargs["event"] == "credit_topup_success"+ assert kwargs["event"] == "topup_completed"assert kwargs["distinct_id"] == "user-1"assert kwargs["properties"] == {"amount_credits": 2500,"top_up_type": "CHECKOUT",+ **BASE_PROPERTIES,}@@ -2911,7 +2945,7 @@ async def test_modify_stripe_subscription_for_tier_upgrade_immediate_proration()@pytest.mark.asyncioasync def test_modify_stripe_subscription_skips_emit_when_db_flip_fails():"""When ``set_subscription_tier`` fails after a successful Stripe modify,- the immediate ``subscription_upgraded`` emit must be skipped — the webhook+ the immediate ``subscription_changed`` emit must be skipped — the webhookis the fallback emit path and would otherwise produce a duplicate event."""mock_sub = stripe.Subscription.construct_from({@@ -2955,8 +2989,11 @@ async def test_modify_stripe_subscription_skips_emit_when_db_flip_fails():new_callable=AsyncMock,side_effect=PrismaError("db down"),),- patch("backend.data.credit.settings.secrets.posthog_api_key", new="phc_test"),- patch("backend.data.credit.posthog.capture", new=track_mock),+ patch(+ "backend.util.posthog_client.get_posthog_client",+ return_value=MagicMock(capture=track_mock),+ ),+ patch("backend.util.posthog_client._environment", return_value="test"),patch.object(get_pending_subscription_change, "cache_delete"),):result = await modify_stripe_subscription_for_tier(diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.pyindex 74a66ef69d..36a07f9f50 100644--- a/autogpt_platform/backend/backend/executor/manager.py+++ b/autogpt_platform/backend/backend/executor/manager.py@@ -78,10 +78,8 @@ from backend.util.exceptions import (get_execution_failure_reason,)from backend.util.file import clean_exec_files-from backend.util.funnel_analytics import emit_funnel_eventfrom backend.util.llm.saturation import set_executor_idfrom backend.util.logging import TruncatedLogger, configure_logging-from backend.util.posthog_events import PostHogEventfrom backend.util.process import AppProcess, set_service_namefrom backend.util.retry import (continuous_retry,@@ -713,27 +711,6 @@ async def _enqueue_next_nodes(]-def _expert_run_completed_event(- graph_exec: GraphExecutionEntry, status: ExecutionStatus-) -> Optional[dict]:- """Funnel payload for a finished top-level expert run, else None.-- Mirrors the gating in ``expert_posts._post_run_result`` so the funnel- counts exactly the runs that can post: an expert-attributed, non-dry-run,- top-level execution that reached a terminal status. Execution origin- (schedule vs manual vs webhook) is not persisted anywhere, so the event- covers every such run rather than pretending to know the trigger.- """- expert_id = expert_posts.completed_expert_id(graph_exec, status)- if expert_id is None:- return None- return {- "expert_id": expert_id,- "status": status.value,- "graph_exec_id": graph_exec.graph_exec_id,- }--class ExecutionProcessor:"""This class contains event handlers for the process pool executor events.@@ -1130,15 +1107,6 @@ class ExecutionProcessor:status=exec_meta.status,stats=exec_stats,)- # Only once the terminal state is persisted.- run_event = _expert_run_completed_event(graph_exec, exec_meta.status)- if run_event is not None:- emit_funnel_event(- graph_exec.user_id,- PostHogEvent.EXPERT_RUN_COMPLETED,- run_event,- f"expert_run_completed:{graph_exec.graph_exec_id}",- )async def charge_node_usage(self,diff --git a/autogpt_platform/backend/backend/executor/manager_expert_events_test.py b/autogpt_platform/backend/backend/executor/manager_expert_events_test.pydeleted file mode 100644index b1fa9ddbcf..0000000000--- a/autogpt_platform/backend/backend/executor/manager_expert_events_test.py+++ /dev/null@@ -1,65 +0,0 @@-from types import SimpleNamespace--from backend.data.execution import ExecutionStatus-from backend.executor.manager import _expert_run_completed_event---def _graph_exec(expert_id="e-1", dry_run=False, parent=None, user_id="u-1"):- return SimpleNamespace(- user_id=user_id,- graph_exec_id="run-1",- execution_context=SimpleNamespace(- expert_id=expert_id,- dry_run=dry_run,- parent_execution_id=parent,- ),- )---def test_emits_for_terminal_expert_run():- event = _expert_run_completed_event(_graph_exec(), ExecutionStatus.COMPLETED)- assert event == {- "expert_id": "e-1",- "status": ExecutionStatus.COMPLETED.value,- "graph_exec_id": "run-1",- }---def test_emits_for_failed_expert_run():- event = _expert_run_completed_event(_graph_exec(), ExecutionStatus.FAILED)- assert event == {- "expert_id": "e-1",- "status": ExecutionStatus.FAILED.value,- "graph_exec_id": "run-1",- }---def test_skips_non_expert_run():- assert (- _expert_run_completed_event(- _graph_exec(expert_id=None), ExecutionStatus.COMPLETED- )- is None- )---def test_skips_dry_run():- assert (- _expert_run_completed_event(- _graph_exec(dry_run=True), ExecutionStatus.COMPLETED- )- is None- )---def test_skips_subgraph_run():- assert (- _expert_run_completed_event(- _graph_exec(parent="parent-1"), ExecutionStatus.COMPLETED- )- is None- )---def test_skips_non_terminal_status():- assert _expert_run_completed_event(_graph_exec(), ExecutionStatus.RUNNING) is Nonediff --git a/autogpt_platform/backend/backend/notifications/trial.py b/autogpt_platform/backend/backend/notifications/trial.pyindex bf89d25334..b9fe29d519 100644--- a/autogpt_platform/backend/backend/notifications/trial.py+++ b/autogpt_platform/backend/backend/notifications/trial.py@@ -38,13 +38,13 @@ TrialNoticeKind = Literal[]TRIAL_NOTICE_EVENTS: dict[TrialNoticeKind, PostHogEvent] = {- "started": PostHogEvent.SUBSCRIPTION_TRIAL_STARTED,- "ending": PostHogEvent.SUBSCRIPTION_TRIAL_ENDING,- "canceled": PostHogEvent.SUBSCRIPTION_TRIAL_CANCELED,- "resumed": PostHogEvent.SUBSCRIPTION_TRIAL_RESUMED,- "ended": PostHogEvent.SUBSCRIPTION_TRIAL_ENDED,- "converted": PostHogEvent.SUBSCRIPTION_TRIAL_CONVERTED,- "payment_failed": PostHogEvent.SUBSCRIPTION_TRIAL_PAYMENT_FAILED,+ "started": PostHogEvent.TRIAL_STARTED,+ "ending": PostHogEvent.TRIAL_ENDING,+ "canceled": PostHogEvent.TRIAL_CANCELED,+ "resumed": PostHogEvent.TRIAL_RESUMED,+ "ended": PostHogEvent.TRIAL_ENDED,+ "converted": PostHogEvent.TRIAL_CONVERTED,+ "payment_failed": PostHogEvent.PAYMENT_FAILED,}diff --git a/autogpt_platform/backend/backend/notifications/trial_test.py b/autogpt_platform/backend/backend/notifications/trial_test.pyindex 656f57b04b..d402b21e49 100644--- a/autogpt_platform/backend/backend/notifications/trial_test.py+++ b/autogpt_platform/backend/backend/notifications/trial_test.py@@ -171,16 +171,17 @@ async def test_notice_uses_shared_notification_queue(trial, outcome):release.assert_not_awaited()assert track.call_count == int(outcome == "sent")if outcome == "sent":- assert track.call_args.args[0] == "subscription_trial_started"+ assert track.call_args.args[0] == "trial_started"-def test_every_trial_notice_kind_keeps_its_event_name():- """The names used to be built as f"subscription_trial_{kind}"; the lookup- must cover every kind and send exactly those strings."""+def test_every_trial_notice_kind_has_the_analytics_plan_event_name():+ """Every kind maps to the analytics plan's ``trial_*`` name; a failed+ conversion charge is the plan's ``payment_failed``."""kinds = get_args(notices.TrialNoticeKind)assert set(notices.TRIAL_NOTICE_EVENTS) == set(kinds)for kind in kinds:- assert notices.TRIAL_NOTICE_EVENTS[kind].value == f"subscription_trial_{kind}"+ expected = "payment_failed" if kind == "payment_failed" else f"trial_{kind}"+ assert notices.TRIAL_NOTICE_EVENTS[kind].value == expecteddef test_canceled_trial_suppresses_late_ending_reminder(trial):diff --git a/autogpt_platform/backend/backend/util/funnel_analytics.py b/autogpt_platform/backend/backend/util/funnel_analytics.pyindex c96f5d89f6..9075c7f651 100644--- a/autogpt_platform/backend/backend/util/funnel_analytics.py+++ b/autogpt_platform/backend/backend/util/funnel_analytics.py@@ -8,11 +8,10 @@ through the DB manager.import loggingfrom typing import Any-from uuid import NAMESPACE_URL, uuid5import sentry_sdk-from backend.util.posthog_client import get_posthog_client+from backend.util import posthog_clientfrom backend.util.posthog_events import PostHogEventlogger = logging.getLogger(__name__)@@ -45,25 +44,4 @@ def emit_funnel_event(except Exception:logger.exception(f"Failed to breadcrumb funnel event {event_name}")- try:- client = get_posthog_client()- if client is None:- return- properties: dict[str, Any] = {**data}- event_uuid = None- if data_index is not None:- # Both, because the two are read at different layers: PostHog's- # ingestion keys on the event uuid, which the client otherwise- # randomises per call, and $insert_id is what its docs name.- properties["$insert_id"] = data_index- event_uuid = str(- uuid5(NAMESPACE_URL, f"{user_id}:{event_name}:{data_index}")- )- client.capture(- event=event_name,- distinct_id=user_id,- properties=properties,- uuid=event_uuid,- )- except Exception:- logger.exception(f"Failed to emit funnel event {event_name} for user {user_id}")+ posthog_client.capture(user_id, event, data, dedup_key=data_index)diff --git a/autogpt_platform/backend/backend/util/funnel_analytics_test.py b/autogpt_platform/backend/backend/util/funnel_analytics_test.pyindex 4f13e7cf9f..26e74e0aa8 100644--- a/autogpt_platform/backend/backend/util/funnel_analytics_test.py+++ b/autogpt_platform/backend/backend/util/funnel_analytics_test.py@@ -1,29 +1,30 @@import uuidfrom unittest.mock import MagicMock, patch-from backend.util import funnel_analytics+from backend.util import funnel_analytics, posthog_clientfrom backend.util.funnel_analytics import emit_funnel_eventfrom backend.util.posthog_events import PostHogEventdef test_captures_to_posthog_with_the_user_as_distinct_id():client = MagicMock()- with patch.object(funnel_analytics, "get_posthog_client", return_value=client):- emit_funnel_event(- "user-1", PostHogEvent.HIRE_COMPLETED, {"template_id": "tpl-1"}- )+ with (+ patch.object(posthog_client, "get_posthog_client", return_value=client),+ patch.object(posthog_client, "_environment", return_value="dev"),+ ):+ emit_funnel_event("user-1", PostHogEvent.HIRE_FAILED, {"template_id": "tpl-1"})client.capture.assert_called_once_with(- event="hire_completed",+ event="hire_failed",distinct_id="user-1",- properties={"template_id": "tpl-1"},+ properties={"template_id": "tpl-1", "environment": "dev", "source": "platform"},uuid=None,)def test_a_data_index_becomes_the_posthog_dedup_key():client = MagicMock()- with patch.object(funnel_analytics, "get_posthog_client", return_value=client):+ with patch.object(posthog_client, "get_posthog_client", return_value=client):emit_funnel_event("user-1",PostHogEvent.BRIEFING_DELIVERED,@@ -31,15 +32,14 @@ def test_a_data_index_becomes_the_posthog_dedup_key():"briefing_delivered:b-1",)- assert client.capture.call_args.kwargs["properties"] == {- "briefing_id": "b-1",- "$insert_id": "briefing_delivered:b-1",- }+ properties = client.capture.call_args.kwargs["properties"]+ assert properties["briefing_id"] == "b-1"+ assert properties["$insert_id"] == "briefing_delivered:b-1"def test_no_data_index_means_no_dedup_key():client = MagicMock()- with patch.object(funnel_analytics, "get_posthog_client", return_value=client):+ with patch.object(posthog_client, "get_posthog_client", return_value=client):emit_funnel_event("user-1", PostHogEvent.EXPERT_FIRED, {"expert_id": "e-1"})assert "$insert_id" not in client.capture.call_args.kwargs["properties"]@@ -48,7 +48,7 @@ def test_no_data_index_means_no_dedup_key():def test_breadcrumb_carries_the_event_and_its_payload():client = MagicMock()with (- patch.object(funnel_analytics, "get_posthog_client", return_value=client),+ patch.object(posthog_client, "get_posthog_client", return_value=client),patch.object(funnel_analytics.sentry_sdk, "add_breadcrumb") as crumb,):emit_funnel_event("user-1", PostHogEvent.HIRE_FAILED, {"template_id": "tpl-1"})@@ -67,7 +67,7 @@ def test_breadcrumb_is_added_before_the_capture():client = MagicMock()client.capture.side_effect = lambda **_: calls.append("capture")with (- patch.object(funnel_analytics, "get_posthog_client", return_value=client),+ patch.object(posthog_client, "get_posthog_client", return_value=client),patch.object(funnel_analytics.sentry_sdk,"add_breadcrumb",@@ -81,7 +81,7 @@ def test_breadcrumb_is_added_before_the_capture():def test_a_disabled_posthog_still_leaves_a_breadcrumb():with (- patch.object(funnel_analytics, "get_posthog_client", return_value=None),+ patch.object(posthog_client, "get_posthog_client", return_value=None),patch.object(funnel_analytics.sentry_sdk, "add_breadcrumb") as crumb,):emit_funnel_event("user-1", PostHogEvent.EXPERT_FIRED, {"expert_id": "e-1"})@@ -93,16 +93,14 @@ def test_a_throwing_breadcrumb_still_lets_the_capture_through():"""The two sinks are independent: losing Sentry must not lose PostHog."""client = MagicMock()with (- patch.object(funnel_analytics, "get_posthog_client", return_value=client),+ patch.object(posthog_client, "get_posthog_client", return_value=client),patch.object(funnel_analytics.sentry_sdk,"add_breadcrumb",side_effect=RuntimeError("sentry unavailable"),),):- emit_funnel_event(- "user-1", PostHogEvent.HIRE_COMPLETED, {"template_id": "tpl-1"}- )+ emit_funnel_event("user-1", PostHogEvent.HIRE_FAILED, {"template_id": "tpl-1"})client.capture.assert_called_once()@@ -111,7 +109,7 @@ def test_a_data_index_also_becomes_a_deterministic_event_uuid():"""PostHog's ingestion keys on the event uuid, which the client otherwiserandomises per call, so a retry needs the same one."""client = MagicMock()- with patch.object(funnel_analytics, "get_posthog_client", return_value=client):+ with patch.object(posthog_client, "get_posthog_client", return_value=client):emit_funnel_event("user-1", PostHogEvent.BRIEFING_DELIVERED, {}, "briefing_delivered:b-1")@@ -127,7 +125,7 @@ def test_a_data_index_also_becomes_a_deterministic_event_uuid():def test_a_different_event_identity_gets_a_different_uuid():client = MagicMock()- with patch.object(funnel_analytics, "get_posthog_client", return_value=client):+ with patch.object(posthog_client, "get_posthog_client", return_value=client):emit_funnel_event("user-1", PostHogEvent.BRIEFING_DELIVERED, {}, "briefing_delivered:b-1")@@ -142,7 +140,7 @@ def test_a_different_event_identity_gets_a_different_uuid():def test_no_data_index_leaves_the_uuid_to_the_client():client = MagicMock()- with patch.object(funnel_analytics, "get_posthog_client", return_value=client):+ with patch.object(posthog_client, "get_posthog_client", return_value=client):emit_funnel_event("user-1", PostHogEvent.EXPERT_FIRED, {"expert_id": "e-1"})assert client.capture.call_args.kwargs["uuid"] is None@@ -151,27 +149,23 @@ def test_no_data_index_leaves_the_uuid_to_the_client():def test_a_failing_capture_never_raises_into_the_caller():client = MagicMock()client.capture.side_effect = RuntimeError("posthog unreachable")- with patch.object(funnel_analytics, "get_posthog_client", return_value=client):- emit_funnel_event(- "user-1", PostHogEvent.HIRE_COMPLETED, {"template_id": "tpl-1"}- )+ with patch.object(posthog_client, "get_posthog_client", return_value=client):+ emit_funnel_event("user-1", PostHogEvent.HIRE_FAILED, {"template_id": "tpl-1"})def test_a_failing_client_lookup_never_raises_into_the_caller():with patch.object(- funnel_analytics,+ posthog_client,"get_posthog_client",side_effect=RuntimeError("settings unreadable"),):- emit_funnel_event(- "user-1", PostHogEvent.HIRE_COMPLETED, {"template_id": "tpl-1"}- )+ emit_funnel_event("user-1", PostHogEvent.HIRE_FAILED, {"template_id": "tpl-1"})def test_the_payload_is_copied_not_shared_with_the_caller():client = MagicMock()payload = {"expert_id": "e-1"}- with patch.object(funnel_analytics, "get_posthog_client", return_value=client):+ with patch.object(posthog_client, "get_posthog_client", return_value=client):emit_funnel_event("user-1", PostHogEvent.EXPERT_FIRED, payload, "expert_fired:e-1")@@ -184,7 +178,7 @@ def test_the_event_goes_out_as_its_plain_name_and_keeps_its_uuid():name is the same plain string, and the dedup uuid derived from it matchesthe one events sent before the enum existed already carry."""client = MagicMock()- with patch.object(funnel_analytics, "get_posthog_client", return_value=client):+ with patch.object(posthog_client, "get_posthog_client", return_value=client):emit_funnel_event("user-1", PostHogEvent.BRIEFING_DELIVERED, {}, "briefing_delivered:b-1")diff --git a/autogpt_platform/backend/backend/util/posthog_client.py b/autogpt_platform/backend/backend/util/posthog_client.pyindex 0c19441dbf..036b74979d 100644--- a/autogpt_platform/backend/backend/util/posthog_client.py+++ b/autogpt_platform/backend/backend/util/posthog_client.py@@ -1,17 +1,24 @@"""Process-wide PostHog client shared by every server-side analytics emitter.The client batches events on a background thread, so one instance per-process is both cheaper and safer than one per module. Emitters must treat-``None`` as "analytics disabled" and never let tracking raise into the-request or execution that produced the event.+process is both cheaper and safer than one per module. Emitters send through+:func:`capture`, which adds the base properties every event carries+(``environment`` and ``source``, see ``docs/platform/tracking-plan.md``),+treats a missing client as "analytics disabled" and never lets tracking+raise into the request or execution that produced the event."""import atexitimport logging+from collections.abc import Mapping+from functools import cachefrom threading import Lock+from typing import Any+from uuid import NAMESPACE_URL, uuid5from posthog import Posthog+from backend.util.posthog_events import PostHogEventfrom backend.util.settings import Settingslogger = logging.getLogger(__name__)@@ -45,6 +52,63 @@ def get_posthog_client() -> Posthog | None:return _client+@cache+def _environment() -> str:+ return Settings().config.app_env.value+++def capture(+ distinct_id: str | None,+ event: PostHogEvent,+ properties: Mapping[str, Any] | None = None,+ *,+ source: str = "platform",+ dedup_key: str | None = None,+) -> None:+ """Send one event for *distinct_id* with the base properties.++ No user means no event: a synthetic distinct id would create a person+ nobody can merge. The base properties are applied last, so a caller can+ never overwrite ``environment`` or ``source`` by accident.++ ``dedup_key`` is an idempotency key for a redelivery or requeue that can+ send the same event twice; it is carried both as ``$insert_id`` and as a+ deterministic event uuid, because PostHog's ingestion keys on the uuid,+ which the client otherwise randomises per call.+ """+ if not distinct_id:+ return+ event_name = event.value+ try:+ client = get_posthog_client()+ if client is None:+ return+ payload: dict[str, Any] = {+ **(properties or {}),+ "environment": _environment(),+ "source": source,+ }+ event_uuid = None+ if dedup_key is not None:+ payload["$insert_id"] = dedup_key+ event_uuid = str(+ uuid5(NAMESPACE_URL, f"{distinct_id}:{event_name}:{dedup_key}")+ )+ client.capture(+ distinct_id=distinct_id,+ event=event_name,+ properties=payload,+ uuid=event_uuid,+ )+ except Exception:+ logger.warning(+ "Failed to send PostHog event %s for %s",+ event_name,+ distinct_id,+ exc_info=True,+ )++def _shutdown() -> None:if _client is not None:_client.flush()diff --git a/autogpt_platform/backend/backend/util/posthog_client_test.py b/autogpt_platform/backend/backend/util/posthog_client_test.pyindex 035ba6c72c..ebf540054f 100644--- a/autogpt_platform/backend/backend/util/posthog_client_test.py+++ b/autogpt_platform/backend/backend/util/posthog_client_test.py@@ -1,3 +1,4 @@+import uuidfrom concurrent.futures import ThreadPoolExecutor, TimeoutErrorfrom threading import Eventfrom unittest.mock import Mock@@ -5,6 +6,7 @@ from unittest.mock import Mockimport pytestfrom backend.util import posthog_client+from backend.util.posthog_events import PostHogEvent@pytest.fixture(autouse=True)@@ -103,3 +105,85 @@ def test_initialization_failure_is_safe_and_retryable(assert posthog_client.get_posthog_client() is clientassert posthog_client.get_posthog_client() is clientassert settings_factory.call_count == 2+++def test_capture_adds_the_base_properties_last(monkeypatch: pytest.MonkeyPatch) -> None:+ client = Mock()+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: client)+ monkeypatch.setattr(posthog_client, "_environment", lambda: "prod")++ posthog_client.capture(+ "user-1",+ PostHogEvent.SUBSCRIPTION_CHANGED,+ {"subscription_tier": "PRO", "source": "caller"},+ source="chat_copilot",+ )++ client.capture.assert_called_once_with(+ distinct_id="user-1",+ event="subscription_changed",+ properties={+ "subscription_tier": "PRO",+ "environment": "prod",+ "source": "chat_copilot",+ },+ uuid=None,+ )+++def test_capture_defaults_the_source_to_platform(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ client = Mock()+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: client)++ posthog_client.capture("user-1", PostHogEvent.TOPUP_COMPLETED)++ properties = client.capture.call_args.kwargs["properties"]+ assert properties["source"] == "platform"+ assert properties["environment"] in {"local", "dev", "prod"}++[email protected]("distinct_id", [None, ""])+def test_capture_without_a_user_sends_nothing(+ monkeypatch: pytest.MonkeyPatch, distinct_id: str | None+) -> None:+ client = Mock()+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: client)++ posthog_client.capture(distinct_id, PostHogEvent.CHAT_TOOL_CALLED)++ client.capture.assert_not_called()+++def test_capture_is_a_noop_when_analytics_is_off(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: None)++ posthog_client.capture("user-1", PostHogEvent.CHAT_TOOL_CALLED)+++def test_capture_never_raises(monkeypatch: pytest.MonkeyPatch) -> None:+ client = Mock()+ client.capture.side_effect = RuntimeError("posthog down")+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: client)++ posthog_client.capture("user-1", PostHogEvent.CHAT_TOOL_CALLED)+++def test_capture_dedup_key_is_insert_id_and_a_stable_uuid(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ client = Mock()+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: client)++ posthog_client.capture(+ "user-1", PostHogEvent.BRIEFING_DELIVERED, {}, dedup_key="briefing:b-1"+ )++ kwargs = client.capture.call_args.kwargs+ assert kwargs["properties"]["$insert_id"] == "briefing:b-1"+ assert kwargs["uuid"] == str(+ uuid.uuid5(uuid.NAMESPACE_URL, "user-1:briefing_delivered:briefing:b-1")+ )diff --git a/autogpt_platform/backend/backend/util/posthog_events.py b/autogpt_platform/backend/backend/util/posthog_events.pyindex b566e2d169..c1074f6291 100644--- a/autogpt_platform/backend/backend/util/posthog_events.py+++ b/autogpt_platform/backend/backend/util/posthog_events.py@@ -2,14 +2,17 @@``docs/platform/tracking-plan.md`` says what each event means, which sidesends it and which properties it carries. This module is the code half of-that plan: emitters take the name from here instead of spelling it out. The+that plan: emitters take the name from here instead of spelling it out.+The names follow the product analytics plan ("Every Second Counts"). Thefunnel ``data_index`` keys (``briefing_generated:<id>``, ...) embed the nameas a string, so a rename has to update those too.Never change the value of a member of ``PostHogEvent``. PostHog stores theraw string, so a rename orphans every insight, funnel and cohort built on the-old name, and past events cannot be backfilled. ``posthog_events_test.py``-pins the values so an accidental rename fails CI.+old name, and past events cannot be backfilled. The one-time move to the+analytics plan's names (SECRT-2722) is the exception, and the old names are+reserved in ``posthog_events_test.py``, which pins the values so an+accidental rename fails CI."""from enum import StrEnum@@ -19,56 +22,45 @@ class PostHogEvent(StrEnum):"""Events the backend emits today."""# Activation: backend/util/product_analytics.py- RUN_AGENT = "run_agent"- RUN_AUTOPILOT = "run_autopilot"- RUN_EXPERT = "run_expert"- AGENT_RUN_COMPLETED = "agent_run_completed"- AGENT_RUN_FAILED = "agent_run_failed"+ AGENT_RUN_STARTED = "agent_run_started"+ AGENT_RUN_FINISHED = "agent_run_finished"+ CHAT_MESSAGE_SENT = "chat_message_sent"SCHEDULE_CREATED = "schedule_created"SCHEDULE_FIRED = "schedule_fired"TRIGGER_FIRED = "trigger_fired"- EXPERT_HIRED = "expert_hired"INTEGRATION_CONNECTED = "integration_connected"CREDENTIAL_OAUTH_STARTED = "credential_oauth_started"CREDENTIAL_OAUTH_EXCHANGE_FAILED = "credential_oauth_exchange_failed"# Experts loop: backend/util/funnel_analytics.py+ EXPERT_HIRED = "expert_hired"HIRE_FAILED = "hire_failed"- HIRE_COMPLETED = "hire_completed"WRITING_STYLE_ADDED = "writing_style_added"WORKFLOW_INSTALLED_ON_EXPERT = "workflow_installed_on_expert"EXPERT_FIRED = "expert_fired"BRIEFING_GENERATED = "briefing_generated"BRIEFING_DELIVERED = "briefing_delivered"- EXPERT_RUN_COMPLETED = "expert_run_completed"- # Copilot: backend/copilot/tracking.py- COPILOT_MESSAGE_SENT = "copilot_message_sent"- COPILOT_TOOL_CALLED = "copilot_tool_called"- COPILOT_AGENT_RUN_SUCCESS = "copilot_agent_run_success"- COPILOT_AGENT_SCHEDULED = "copilot_agent_scheduled"- COPILOT_FOLLOWUP_SCHEDULED = "copilot_followup_scheduled"- COPILOT_LIBRARY_CHECK_OUTCOME = "copilot_library_check_outcome"- # Dead: track_trigger_setup has no caller.- COPILOT_TRIGGER_SETUP = "copilot_trigger_setup"+ # Chat (Autopilot): backend/copilot/tracking.py+ CHAT_TOOL_CALLED = "chat_tool_called"+ CHAT_OUTCOME = "chat_outcome"+ CHAT_LIBRARY_CHECK_OUTCOME = "chat_library_check_outcome"# Billing: backend/data/credit.py- CREDIT_TOPUP_SUCCESS = "credit_topup_success"+ TOPUP_COMPLETED = "topup_completed"SUBSCRIPTION_CANCELLATION_SCHEDULED = "subscription_cancellation_scheduled"- SUBSCRIPTION_UPGRADED = "subscription_upgraded"- SUBSCRIPTION_PAYMENT_SUCCESS = "subscription_payment_success"- SUBSCRIPTION_TIER_RECONCILIATION_DISCREPANCY = (- "subscription_tier_reconciliation_discrepancy"- )+ SUBSCRIPTION_CHANGED = "subscription_changed"+ PAYMENT_SUCCEEDED = "payment_succeeded"+ SUBSCRIPTION_TIER_RECONCILED = "subscription_tier_reconciled"# Trial lifecycle: backend/notifications/trial.py- SUBSCRIPTION_TRIAL_STARTED = "subscription_trial_started"- SUBSCRIPTION_TRIAL_ENDING = "subscription_trial_ending"- SUBSCRIPTION_TRIAL_CANCELED = "subscription_trial_canceled"- SUBSCRIPTION_TRIAL_RESUMED = "subscription_trial_resumed"- SUBSCRIPTION_TRIAL_ENDED = "subscription_trial_ended"- SUBSCRIPTION_TRIAL_CONVERTED = "subscription_trial_converted"- SUBSCRIPTION_TRIAL_PAYMENT_FAILED = "subscription_trial_payment_failed"+ TRIAL_STARTED = "trial_started"+ TRIAL_ENDING = "trial_ending"+ TRIAL_CANCELED = "trial_canceled"+ TRIAL_RESUMED = "trial_resumed"+ TRIAL_ENDED = "trial_ended"+ TRIAL_CONVERTED = "trial_converted"+ PAYMENT_FAILED = "payment_failed"# Person properties: backend/data/posthog_lifecycle_sync.py. PostHog's own# person-update event, not a user action; keep it out of funnels.diff --git a/autogpt_platform/backend/backend/util/posthog_events_test.py b/autogpt_platform/backend/backend/util/posthog_events_test.pyindex b5c94e7e4e..0fd768d150 100644--- a/autogpt_platform/backend/backend/util/posthog_events_test.py+++ b/autogpt_platform/backend/backend/util/posthog_events_test.py@@ -2,17 +2,16 @@A failure here means an event name changed. PostHog cannot backfill a rename,so every insight built on the old name silently goes empty. Add new members-freely; never edit an existing string.+freely; never edit an existing string. The names follow the product analytics+plan; the pre-plan names are reserved below."""from backend.util.posthog_events import PlannedPostHogEvent, PostHogEventLIVE_EVENT_NAMES = {- "run_agent",- "run_autopilot",- "run_expert",- "agent_run_completed",- "agent_run_failed",+ "agent_run_started",+ "agent_run_finished",+ "chat_message_sent","schedule_created","schedule_fired","trigger_fired",@@ -21,22 +20,57 @@ LIVE_EVENT_NAMES = {"credential_oauth_started","credential_oauth_exchange_failed","hire_failed",- "hire_completed","writing_style_added","workflow_installed_on_expert","expert_fired","briefing_generated","briefing_delivered",+ "chat_tool_called",+ "chat_outcome",+ "chat_library_check_outcome",+ "topup_completed",+ "subscription_cancellation_scheduled",+ "subscription_changed",+ "payment_succeeded",+ "subscription_tier_reconciled",+ "trial_started",+ "trial_ending",+ "trial_canceled",+ "trial_resumed",+ "trial_ended",+ "trial_converted",+ "payment_failed",+ "$set",+}++PLANNED_EVENT_NAMES = {+ "signup_completed",+ "onboarding_completed",+ "checkout_started",+ "subscription_ended",+ "listing_added_to_library",+ "listing_downloaded",+}++# No longer sent, or renamed (SECRT-2722). The names stay reserved: reusing one would+# splice a different action onto the history PostHog already holds for it.+RETIRED_EVENT_NAMES = {+ "hire_completed","expert_run_completed","copilot_message_sent",- "copilot_tool_called","copilot_agent_run_success","copilot_agent_scheduled","copilot_followup_scheduled",- "copilot_library_check_outcome","copilot_trigger_setup",+ # Renamed to the analytics plan's names (SECRT-2722).+ "run_agent",+ "run_autopilot",+ "run_expert",+ "agent_run_completed",+ "agent_run_failed",+ "copilot_tool_called",+ "copilot_library_check_outcome","credit_topup_success",- "subscription_cancellation_scheduled","subscription_upgraded","subscription_payment_success","subscription_tier_reconciliation_discrepancy",@@ -47,16 +81,6 @@ LIVE_EVENT_NAMES = {"subscription_trial_ended","subscription_trial_converted","subscription_trial_payment_failed",- "$set",-}--PLANNED_EVENT_NAMES = {- "signup_completed",- "onboarding_completed",- "checkout_started",- "subscription_ended",- "listing_added_to_library",- "listing_downloaded",}@@ -70,3 +94,7 @@ def test_planned_event_names_are_pinned():def test_a_planned_event_is_not_also_live():assert not LIVE_EVENT_NAMES & PLANNED_EVENT_NAMES+++def test_a_retired_event_name_is_never_reused():+ assert not RETIRED_EVENT_NAMES & (LIVE_EVENT_NAMES | PLANNED_EVENT_NAMES)diff --git a/autogpt_platform/backend/backend/util/product_analytics.py b/autogpt_platform/backend/backend/util/product_analytics.pyindex 3aeba77ec0..4803225d1f 100644--- a/autogpt_platform/backend/backend/util/product_analytics.py+++ b/autogpt_platform/backend/backend/util/product_analytics.py@@ -5,26 +5,30 @@ points (execution creation, executor completion, chat-turn persistence, thescheduler, webhook delivery), so PostHog funnels and experiments count thesame things the ``analytics.*`` SQL views count from the primary tables.-Event vocabulary (PostHog event name -> SQL equivalent):+Event vocabulary (PostHog event name -> SQL equivalent), named after the+product analytics plan:-- ``run_agent`` human-initiated agent run (manual UI, API key, copilot+- ``agent_run_started`` human-initiated agent run (manual UI, API key, copilottool). AgentGraphExecution.triggerSource IN- ('manual', 'api', 'copilot') AND expertId IS NULL.-- ``run_autopilot`` user turn in an Autopilot chat. ChatMessage role='user'- on a session with expertId IS NULL and interactive origin.-- ``run_expert`` user turn in an expert chat, or a human-initiated run of- an expert's workflow (property ``kind`` tells which).-- ``agent_run_completed`` / ``agent_run_failed`` terminal run outcome, with- ``trigger`` so failures can be split by how they started.- Includes subgraph and automated runs; filter to human- triggers when comparing outcomes with run-start events.+ ('manual', 'api', 'copilot'); an expert's workflow run+ has ``expert_id`` set and ``kind: workflow_run``.+- ``chat_message_sent`` user turn in an Autopilot or expert chat+ (``expert_id`` set). ChatMessage role='user' on a+ session with interactive origin.+- ``agent_run_finished`` terminal run outcome (``status``: completed | failed),+ with ``via`` (the run's triggerSource) so failures can+ be split by how they started. Includes subgraph and+ automated runs; filter to human triggers when+ comparing outcomes with run-start events. A top-level expert run is+ ``expert_id`` set and ``is_subgraph_run`` false.- ``schedule_created`` a schedule was registered (``target``: agent | autopilot |expert). ActivityEvent category SCHEDULE / schedule.created.- ``schedule_fired`` a schedule produced work. For agent/expert targets the runrow carries triggerSource='schedule' and triggerRef=scheduleid; for autopilot targets the session has origin='automation'.- ``trigger_fired`` a webhook produced a run (triggerSource='webhook').-- ``expert_hired`` a user hired an expert from a template.+- ``expert_hired`` a user hired an expert from a template (sent from+ ``experts_db.hire_expert``, so every surface counts).- ``integration_connected`` a user connected a credential (OAuth or manual).IntegrationCredential rows by createdByUserId.- ``credential_oauth_started`` the backend issued an OAuth login URL. With@@ -33,10 +37,10 @@ Event vocabulary (PostHog event name -> SQL equivalent):- ``credential_oauth_exchange_failed`` the OAuth callback returned an error, by``failure_class``. No SQL equivalent: nothing is stored.-``run_agent`` is not the same as a "task": the ``analytics.*`` views count a-copilot-started run through the chat turn that asked for it, so a task is-``run_agent`` / ``run_expert`` with ``trigger`` other than ``copilot``, plus-every ``run_autopilot`` and chat-turn ``run_expert``. Event names live in+``agent_run_started`` is not the same as a "task": the ``analytics.*`` views+count a copilot-started run through the chat turn that asked for it, so a+task is ``agent_run_started`` with ``via`` other than ``copilot``, plus+every ``chat_message_sent``. Event names live in``backend.util.posthog_events``; the full list and the task filter are in``docs/platform/tracking-plan.md``.@@ -49,16 +53,14 @@ from collections.abc import Iterablefrom datetime import datetimefrom typing import TYPE_CHECKING, Any, Literal-from backend.util.posthog_client import get_posthog_client+from backend.util import posthog_clientfrom backend.util.posthog_events import PostHogEvent-from backend.util.settings import Settingsif TYPE_CHECKING:from backend.data.execution import GraphExecutionEntry, GraphExecutionMetafrom backend.data.model import GraphExecutionStatslogger = logging.getLogger(__name__)-settings = Settings()ScheduleTarget = Literal["agent", "autopilot", "expert"]@@ -96,25 +98,17 @@ def track(user_id: str | None,event: PostHogEvent,properties: dict[str, Any] | None = None,+ *,+ dedup_key: str | None = None,) -> None:- """Send one event for *user_id*. Silently no-ops when analytics is off."""- if not user_id:- return- try:- client = get_posthog_client()- if client is None:- return- client.capture(- distinct_id=user_id,- event=event.value,- properties={- "environment": settings.config.app_env.value,- "source": "platform",- **{k: v for k, v in (properties or {}).items() if v is not None},- },- )- except Exception:- logger.warning("Failed to track %s for user %s", event.value, user_id)+ """Send one event for *user_id*, dropping unset properties. Silently+ no-ops when analytics is off or there is no user."""+ posthog_client.capture(+ user_id,+ event,+ {k: v for k, v in (properties or {}).items() if v is not None},+ dedup_key=dedup_key,+ )def track_agent_run_started(@@ -134,19 +128,14 @@ def track_agent_run_started(properties = {"graph_id": graph_id,"graph_exec_id": graph_exec_id,- "trigger": trigger_value,- "trigger_ref": trigger_ref,+ "via": trigger_value,+ "via_ref": trigger_ref,"expert_id": expert_id,"preset_id": preset_id,}if expert_id:- track(- user_id,- PostHogEvent.RUN_EXPERT,- {**properties, "kind": "workflow_run"},- )- else:- track(user_id, PostHogEvent.RUN_AGENT, properties)+ properties["kind"] = "workflow_run"+ track(user_id, PostHogEvent.AGENT_RUN_STARTED, properties)def track_agent_run_finished(@@ -161,28 +150,32 @@ def track_agent_run_finished(cost_cents: int | None = None,duration_seconds: float | None = None,is_dry_run: bool = False,+ is_subgraph_run: bool = False,) -> None:if is_dry_run:returnstatus_value = _enum_value(status)- if status_value == "COMPLETED":- event = PostHogEvent.AGENT_RUN_COMPLETED- elif status_value == "FAILED":- event = PostHogEvent.AGENT_RUN_FAILED- else:+ if status_value not in ("COMPLETED", "FAILED"):return+ # Keyed on the run alone: the event fires before the terminal status is+ # persisted, so a failed persist requeues and finishes the run again,+ # possibly with another status. COMPLETED/FAILED are absorbing, so one+ # run never legitimately finishes twice.track(user_id,- event,+ PostHogEvent.AGENT_RUN_FINISHED,{+ "status": status_value.lower(),"graph_id": graph_id,"graph_exec_id": graph_exec_id,- "trigger": _enum_value(trigger),+ "via": _enum_value(trigger),"expert_id": expert_id,"failure_reason": _enum_value(failure_reason),"cost_cents": cost_cents,"duration_seconds": duration_seconds,+ "is_subgraph_run": is_subgraph_run,},+ dedup_key=graph_exec_id,)@@ -204,6 +197,9 @@ def handle_run_finished(cost_cents=exec_stats.cost,duration_seconds=exec_stats.walltime,is_dry_run=exec_stats.is_dry_run,+ is_subgraph_run=(+ graph_exec.execution_context.parent_execution_id is not None+ ),)except Exception:logger.warning(@@ -219,20 +215,25 @@ def track_chat_turn(session_id: str,expert_id: str | None = None,origin: str | None = None,- surface: str | None = None,+ source_platform: str | None = None,+ message_length: int | None = None,) -> None:- """A person sent a chat message. Model-authored turns are not activation."""+ """A person sent a chat message. Model-authored turns are not activation.++ ``origin`` is the channel (``web``, or the bot platform the message came+ from); the session's own origin only filters out automation turns.+ """if origin == "automation":returntrack(user_id,- PostHogEvent.RUN_EXPERT if expert_id else PostHogEvent.RUN_AUTOPILOT,+ PostHogEvent.CHAT_MESSAGE_SENT,{- "session_id": session_id,+ "chat_session_id": session_id,"expert_id": expert_id,- "origin": origin,- "surface": surface or "chat",+ "origin": source_platform or "web","kind": "chat_turn",+ "message_length": message_length,},)@@ -265,7 +266,7 @@ def track_schedule_created("is_recurring": cron is not None,"run_at": run_at.isoformat() if run_at else None,"graph_id": graph_id,- "session_id": session_id,+ "chat_session_id": session_id,},)@@ -289,7 +290,7 @@ def track_schedule_fired("expert_id": expert_id,"graph_id": graph_id,"graph_exec_id": graph_exec_id,- "session_id": session_id,+ "chat_session_id": session_id,},)@@ -317,20 +318,6 @@ def track_trigger_fired()-def track_expert_hired(- *,- user_id: str,- expert_id: str,- template_id: str | None = None,- name: str | None = None,-) -> None:- track(- user_id,- PostHogEvent.EXPERT_HIRED,- {"expert_id": expert_id, "template_id": template_id, "name": name},- )--def track_integration_connected(*,user_id: str,diff --git a/autogpt_platform/backend/backend/util/product_analytics_test.py b/autogpt_platform/backend/backend/util/product_analytics_test.pyindex efc4980913..fb2cb5708e 100644--- a/autogpt_platform/backend/backend/util/product_analytics_test.py+++ b/autogpt_platform/backend/backend/util/product_analytics_test.py@@ -5,15 +5,14 @@ from unittest.mock import Mockimport pytest-from backend.util import product_analytics+from backend.util import posthog_client, product_analyticsfrom backend.util.posthog_events import PostHogEvent-from backend.util.settings import AppEnvironment@pytest.fixturedef capture(monkeypatch: pytest.MonkeyPatch) -> Mock:client = Mock()- monkeypatch.setattr(product_analytics, "get_posthog_client", lambda: client)+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: client)return client.capture@@ -24,23 +23,23 @@ def _only_call(capture: Mock) -> tuple[str, dict]:def test_track_is_a_noop_without_client(monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(product_analytics, "get_posthog_client", lambda: None)- product_analytics.track("user-1", PostHogEvent.RUN_AGENT, {"graph_id": "g"})+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: None)+ product_analytics.track("user-1", PostHogEvent.AGENT_RUN_STARTED, {"graph_id": "g"})def test_track_is_a_noop_without_user(capture: Mock) -> None:- product_analytics.track(None, PostHogEvent.RUN_AGENT, {"graph_id": "g"})+ product_analytics.track(None, PostHogEvent.AGENT_RUN_STARTED, {"graph_id": "g"})capture.assert_not_called()def test_track_adds_base_properties_and_drops_nulls(capture: Mock) -> None:product_analytics.track(- "user-1", PostHogEvent.RUN_AGENT, {"graph_id": "g", "expert_id": None}+ "user-1", PostHogEvent.AGENT_RUN_STARTED, {"graph_id": "g", "expert_id": None})event, properties = _only_call(capture)assert capture.call_args.kwargs["distinct_id"] == "user-1"- assert event == "run_agent"+ assert event == "agent_run_started"assert properties["source"] == "platform"assert "environment" in propertiesassert properties["graph_id"] == "g"@@ -49,11 +48,11 @@ def test_track_adds_base_properties_and_drops_nulls(capture: Mock) -> None:def test_track_swallows_client_errors(capture: Mock) -> None:capture.side_effect = RuntimeError("posthog down")- product_analytics.track("user-1", PostHogEvent.RUN_AGENT)+ product_analytics.track("user-1", PostHogEvent.AGENT_RUN_STARTED)@pytest.mark.parametrize("trigger", ["manual", "api", "copilot"])-def test_human_run_start_is_run_agent(capture: Mock, trigger: str) -> None:+def test_human_run_start_is_agent_run_started(capture: Mock, trigger: str) -> None:product_analytics.track_agent_run_started(user_id="user-1",graph_id="graph-1",@@ -63,9 +62,10 @@ def test_human_run_start_is_run_agent(capture: Mock, trigger: str) -> None:)event, properties = _only_call(capture)- assert event == "run_agent"- assert properties["trigger"] == trigger- assert properties["trigger_ref"] == "library"+ assert event == "agent_run_started"+ assert properties["via"] == trigger+ assert properties["via_ref"] == "library"+ assert "kind" not in propertiesdef test_run_start_accepts_enum_trigger(capture: Mock) -> None:@@ -79,11 +79,13 @@ def test_run_start_accepts_enum_trigger(capture: Mock) -> None:)event, properties = _only_call(capture)- assert event == "run_agent"- assert properties["trigger"] == "manual"+ assert event == "agent_run_started"+ assert properties["via"] == "manual"-def test_expert_workflow_run_start_is_run_expert(capture: Mock) -> None:+def test_expert_workflow_run_start_is_agent_run_started_with_expert(+ capture: Mock,+) -> None:product_analytics.track_agent_run_started(user_id="user-1",graph_id="graph-1",@@ -93,7 +95,7 @@ def test_expert_workflow_run_start_is_run_expert(capture: Mock) -> None:)event, properties = _only_call(capture)- assert event == "run_expert"+ assert event == "agent_run_started"assert properties["kind"] == "workflow_run"assert properties["expert_id"] == "expert-1"@@ -131,10 +133,69 @@ def test_run_finished_completed(capture: Mock) -> None:)event, properties = _only_call(capture)- assert event == "agent_run_completed"- assert properties["trigger"] == "schedule"+ assert event == "agent_run_finished"+ assert properties["status"] == "completed"+ assert properties["via"] == "schedule"assert properties["cost_cents"] == 12assert properties["duration_seconds"] == 3.5+ assert properties["is_subgraph_run"] is False+++def test_run_finished_is_deduplicated_per_run(capture: Mock) -> None:+ """A requeue after a failed status persist finishes the same run again,+ maybe with another status; both sends must share one event uuid."""+ from backend.data.execution import ExecutionStatus++ for status in (ExecutionStatus.COMPLETED, ExecutionStatus.FAILED):+ product_analytics.track_agent_run_finished(+ user_id="user-1",+ graph_id="graph-1",+ graph_exec_id="exec-1",+ status=status,+ trigger="manual",+ )+ product_analytics.track_agent_run_finished(+ user_id="user-1",+ graph_id="graph-1",+ graph_exec_id="exec-2",+ status=ExecutionStatus.COMPLETED,+ trigger="manual",+ )++ first, retry, other = capture.call_args_list+ assert first.kwargs["uuid"] is not None+ assert first.kwargs["uuid"] == retry.kwargs["uuid"]+ assert first.kwargs["properties"]["$insert_id"] == "exec-1"+ assert other.kwargs["uuid"] != first.kwargs["uuid"]+++ ("parent_execution_id", "is_subgraph_run"), [(None, False), ("parent-1", True)]+)+def test_run_finished_hook_tells_a_subgraph_run_from_a_top_level_one(+ capture: Mock, parent_execution_id: str | None, is_subgraph_run: bool+) -> None:+ """A top-level expert run is ``expert_id`` set and ``is_subgraph_run``+ false: the filter that replaced ``expert_run_completed``."""+ from backend.data.execution import ExecutionStatus++ graph_exec = Mock(user_id="user-1", graph_id="graph-1", graph_exec_id="exec-1")+ graph_exec.execution_context.parent_execution_id = parent_execution_id+ product_analytics.handle_run_finished(+ graph_exec,+ Mock(+ status=ExecutionStatus.FAILED,+ trigger_source="subgraph" if is_subgraph_run else "schedule",+ expert_id="expert-1",+ ),+ Mock(failure_reason=None, cost=5, walltime=1.0, is_dry_run=False),+ )++ event, properties = _only_call(capture)+ assert event == "agent_run_finished"+ assert properties["status"] == "failed"+ assert properties["expert_id"] == "expert-1"+ assert properties["is_subgraph_run"] is is_subgraph_rundef test_run_finished_failed_carries_failure_reason(capture: Mock) -> None:@@ -151,7 +212,8 @@ def test_run_finished_failed_carries_failure_reason(capture: Mock) -> None:)event, properties = _only_call(capture)- assert event == "agent_run_failed"+ assert event == "agent_run_finished"+ assert properties["status"] == "failed"assert properties["failure_reason"] == "insufficient_balance"@@ -179,19 +241,36 @@ def test_run_finished_terminated_or_dry_run_emits_nothing(capture: Mock) -> Nonedef test_chat_turn_autopilot_vs_expert(capture: Mock) -> None:product_analytics.track_chat_turn(user_id="user-1", session_id="s1")product_analytics.track_chat_turn(- user_id="user-1", session_id="s2", expert_id="expert-1", surface="slack"+ user_id="user-1",+ session_id="s2",+ expert_id="expert-1",+ source_platform="slack",)events = [c.kwargs["event"] for c in capture.call_args_list]- assert events == ["run_autopilot", "run_expert"]+ assert events == ["chat_message_sent", "chat_message_sent"]autopilot_props = capture.call_args_list[0].kwargs["properties"]expert_props = capture.call_args_list[1].kwargs["properties"]- assert autopilot_props["surface"] == "chat"+ assert autopilot_props["origin"] == "web"+ assert autopilot_props["chat_session_id"] == "s1"+ assert "session_id" not in autopilot_propsassert autopilot_props["kind"] == "chat_turn"- assert expert_props["surface"] == "slack"+ assert "expert_id" not in autopilot_props+ assert "message_length" not in autopilot_props+ assert expert_props["origin"] == "slack"assert expert_props["expert_id"] == "expert-1"+def test_chat_turn_carries_the_message_length(capture: Mock) -> None:+ product_analytics.track_chat_turn(+ user_id="user-1", session_id="s1", message_length=42+ )++ event, properties = _only_call(capture)+ assert event == "chat_message_sent"+ assert properties["message_length"] == 42++def test_automation_chat_turn_emits_nothing(capture: Mock) -> None:product_analytics.track_chat_turn(user_id="user-1", session_id="s1", origin="automation"@@ -238,9 +317,7 @@ def test_schedule_created_and_fired(capture: Mock) -> None:def test_integration_connected(capture: Mock, monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(- product_analytics.settings.config, "app_env", AppEnvironment.LOCAL- )+ monkeypatch.setattr(posthog_client, "_environment", lambda: "local")product_analytics.track_integration_connected(user_id="user-1",provider="github",@@ -259,7 +336,7 @@ def test_integration_connected(capture: Mock, monkeypatch: pytest.MonkeyPatch) -}-def test_trigger_fired_and_expert_hired(capture: Mock) -> None:+def test_trigger_fired(capture: Mock) -> None:product_analytics.track_trigger_fired(user_id="user-1",webhook_id="wh-1",@@ -267,23 +344,16 @@ def test_trigger_fired_and_expert_hired(capture: Mock) -> None:graph_exec_id="exec-1",expert_id="expert-1",)- product_analytics.track_expert_hired(- user_id="user-1", expert_id="expert-1", template_id="tmpl-1", name="Maria"- )- trigger, hired = capture.call_args_list- assert trigger.kwargs["event"] == "trigger_fired"- assert trigger.kwargs["properties"]["target"] == "expert"- assert hired.kwargs["event"] == "expert_hired"- assert hired.kwargs["properties"]["template_id"] == "tmpl-1"+ event, properties = _only_call(capture)+ assert event == "trigger_fired"+ assert properties["target"] == "expert"def test_credential_oauth_started(capture: Mock, monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(- product_analytics.settings.config, "app_env", AppEnvironment.LOCAL- )+ monkeypatch.setattr(posthog_client, "_environment", lambda: "local")product_analytics.track_credential_oauth_started(user_id="user-1", provider="google")@@ -300,9 +370,7 @@ def test_credential_oauth_started(def test_credential_oauth_exchange_failed(capture: Mock, monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(- product_analytics.settings.config, "app_env", AppEnvironment.LOCAL- )+ monkeypatch.setattr(posthog_client, "_environment", lambda: "local")product_analytics.track_credential_oauth_exchange_failed(user_id="user-1",provider="google",diff --git a/autogpt_platform/backend/snapshots/analytics_log_analytics_complex_data b/autogpt_platform/backend/snapshots/analytics_log_analytics_complex_datadeleted file mode 100644index 4d671672a7..0000000000--- a/autogpt_platform/backend/snapshots/analytics_log_analytics_complex_data+++ /dev/null@@ -1,30 +0,0 @@-{- "analytics_id": "analytics-complex-uuid",- "logged_data": {- "agent_id": "agent_123",- "blocks_used": [- {- "block_id": "llm_block",- "count": 3- },- {- "block_id": "http_block",- "count": 5- },- {- "block_id": "code_block",- "count": 2- }- ],- "duration_ms": 3500,- "errors": [],- "execution_id": "exec_456",- "metadata": {- "environment": "production",- "trigger": "manual",- "user_tier": "premium"- },- "nodes_executed": 15,- "status": "completed"- }-}\ No newline at end of filediff --git a/autogpt_platform/backend/snapshots/analytics_log_analytics_success b/autogpt_platform/backend/snapshots/analytics_log_analytics_successdeleted file mode 100644index a08eb0c89c..0000000000--- a/autogpt_platform/backend/snapshots/analytics_log_analytics_success+++ /dev/null@@ -1,3 +0,0 @@-{- "analytics_id": "analytics-789-uuid"-}\ No newline at end of filediff --git a/autogpt_platform/backend/snapshots/analytics_log_metric_success b/autogpt_platform/backend/snapshots/analytics_log_metric_successdeleted file mode 100644index a08f406588..0000000000--- a/autogpt_platform/backend/snapshots/analytics_log_metric_success+++ /dev/null@@ -1,3 +0,0 @@-{- "metric_id": "metric-123-uuid"-}\ No newline at end of filediff --git a/autogpt_platform/backend/snapshots/analytics_log_metric_success_improved b/autogpt_platform/backend/snapshots/analytics_log_metric_success_improveddeleted file mode 100644index a08f406588..0000000000--- a/autogpt_platform/backend/snapshots/analytics_log_metric_success_improved+++ /dev/null@@ -1,3 +0,0 @@-{- "metric_id": "metric-123-uuid"-}\ No newline at end of filediff --git a/autogpt_platform/backend/snapshots/analytics_log_metric_various_values b/autogpt_platform/backend/snapshots/analytics_log_metric_various_valuesdeleted file mode 100644index 6edf88cecc..0000000000--- a/autogpt_platform/backend/snapshots/analytics_log_metric_various_values+++ /dev/null@@ -1,3 +0,0 @@-{- "metric_id": "metric-456-uuid"-}\ No newline at end of filediff --git a/autogpt_platform/backend/snapshots/analytics_metric_float_precision b/autogpt_platform/backend/snapshots/analytics_metric_float_precisiondeleted file mode 100644index 8d3319fb8c..0000000000--- a/autogpt_platform/backend/snapshots/analytics_metric_float_precision+++ /dev/null@@ -1,4 +0,0 @@-{- "metric_id": "metric-float_precision-uuid",- "test_case": "float_precision"-}\ No newline at end of filediff --git a/autogpt_platform/backend/snapshots/analytics_metric_integer_value b/autogpt_platform/backend/snapshots/analytics_metric_integer_valuedeleted file mode 100644index 113a602abf..0000000000--- a/autogpt_platform/backend/snapshots/analytics_metric_integer_value+++ /dev/null@@ -1,4 +0,0 @@-{- "metric_id": "metric-integer_value-uuid",- "test_case": "integer_value"-}\ No newline at end of filediff --git a/autogpt_platform/backend/snapshots/analytics_metric_large_number b/autogpt_platform/backend/snapshots/analytics_metric_large_numberdeleted file mode 100644index 8ad9f60a66..0000000000--- a/autogpt_platform/backend/snapshots/analytics_metric_large_number+++ /dev/null@@ -1,4 +0,0 @@-{- "metric_id": "metric-large_number-uuid",- "test_case": "large_number"-}\ No newline at end of filediff --git a/autogpt_platform/backend/snapshots/analytics_metric_negative_value b/autogpt_platform/backend/snapshots/analytics_metric_negative_valuedeleted file mode 100644index 427967ee04..0000000000--- a/autogpt_platform/backend/snapshots/analytics_metric_negative_value+++ /dev/null@@ -1,4 +0,0 @@-{- "metric_id": "metric-negative_value-uuid",- "test_case": "negative_value"-}\ No newline at end of filediff --git a/autogpt_platform/backend/snapshots/analytics_metric_tiny_number b/autogpt_platform/backend/snapshots/analytics_metric_tiny_numberdeleted file mode 100644index c7543df027..0000000000--- a/autogpt_platform/backend/snapshots/analytics_metric_tiny_number+++ /dev/null@@ -1,4 +0,0 @@-{- "metric_id": "metric-tiny_number-uuid",- "test_case": "tiny_number"-}\ No newline at end of filediff --git a/autogpt_platform/backend/snapshots/analytics_metric_zero_value b/autogpt_platform/backend/snapshots/analytics_metric_zero_valuedeleted file mode 100644index 482cbf0f6c..0000000000--- a/autogpt_platform/backend/snapshots/analytics_metric_zero_value+++ /dev/null@@ -1,4 +0,0 @@-{- "metric_id": "metric-zero_value-uuid",- "test_case": "zero_value"-}\ No newline at end of filediff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useBrainDumpStep.test.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useBrainDumpStep.test.tsindex 6f25dc8b2c..91c70c4720 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useBrainDumpStep.test.ts+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useBrainDumpStep.test.ts@@ -413,7 +413,13 @@ describe("useBrainDumpStep — finishing a take", () => {expect(trackBrainDump).toHaveBeenCalledWith("brain_dump_completed", {duration_secs: 65,input_mode: "voice",+ finalize_latency_ms: expect.any(Number),});+ // The finalize round trip rides on the outcome, not on an event of its own.+ expect(trackBrainDump).not.toHaveBeenCalledWith(+ "finalize_latency_ms",+ expect.anything(),+ );});it("marks the take finalized by id and clears it before advancing", async () => {@@ -501,6 +507,7 @@ describe("useBrainDumpStep — finishing a take", () => {expect(result.current.screen).toBe("failed");expect(trackBrainDump).toHaveBeenCalledWith("transcription_failed", {error_code: "transcription_error",+ finalize_latency_ms: expect.any(Number),});expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBeNull();});@@ -517,6 +524,7 @@ describe("useBrainDumpStep — finishing a take", () => {expect(result.current.screen).toBe("failed");expect(trackBrainDump).toHaveBeenCalledWith("transcription_failed", {error_code: 500,+ finalize_latency_ms: expect.any(Number),});});@@ -551,6 +559,7 @@ describe("useBrainDumpStep — insufficient content", () => {expect(result.current.screen).toBe("insufficient");expect(trackBrainDump).toHaveBeenCalledWith("transcription_failed", {error_code: "no_usable_speech",+ finalize_latency_ms: expect.any(Number),});// No accidental advance, and nothing personalized to advance to.expect(useOnboardingWizardStore.getState().currentStep).toBe(1);diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpStep.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpStep.tsindex 42dde090a4..7eaecf5c0c 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpStep.ts+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpStep.ts@@ -197,6 +197,7 @@ export function useBrainDumpStep() {}const startedAt = performance.now();+ let finalizeLatencyMs: number;try {const response = await finalizeBrainDump({recording_id: recordingId,@@ -204,15 +205,15 @@ export function useBrainDumpStep() {duration_secs: durationSecs,mime_type: recorder.mimeType,});- trackBrainDump("finalize_latency_ms", {- ms: Math.round(performance.now() - startedAt),- input_mode: "voice",- });+ finalizeLatencyMs = Math.round(performance.now() - startedAt);if (response.status !== 200 || response.data.status === "failed") {const errorCode =response.status === 200 ? response.data.error_code : response.status;const rejected = isInsufficientDump(errorCode);- trackBrainDump("transcription_failed", { error_code: errorCode });+ trackBrainDump("transcription_failed", {+ error_code: errorCode,+ finalize_latency_ms: finalizeLatencyMs,+ });// The recording itself went through — it just didn't carry enough// to personalize from, so the recovery screen offers a fresh take// instead of a retry of this one. Nothing is cleared until the@@ -229,6 +230,7 @@ export function useBrainDumpStep() {trackBrainDump("brain_dump_completed", {duration_secs: Math.round(durationSecs),input_mode: "voice",+ finalize_latency_ms: finalizeLatencyMs,});await completeAndAdvance(recordingId, "A");}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/HireStep/__tests__/HireStep.test.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/HireStep/__tests__/HireStep.test.tsxindex e80aee049e..ba4a0e38b8 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/HireStep/__tests__/HireStep.test.tsx+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/HireStep/__tests__/HireStep.test.tsx@@ -122,7 +122,7 @@ describe("HireStep — the team", () => {expect(capture).toHaveBeenCalledWith("expert_recommended", {template_id: "tpl-maria",position: 0,- source: "llm",+ team_source: "llm",});expect(capture.mock.calls.filter(([event]) => event === "expert_recommended"),@@ -175,25 +175,30 @@ describe("HireStep — hiring", () => {await waitFor(() => {expect(screen.getByText("Hired")).toBeDefined();});- expect(hires).toEqual([{ template_id: "tpl-maria" }]);+ expect(hires).toEqual([+ { template_id: "tpl-maria", surface: "onboarding" },+ ]);expect(useOnboardingWizardStore.getState().hiredTemplateIds).toEqual(["tpl-maria",]);// One hire down, one still open.expect(screen.getAllByRole("button", { name: "Hire" })).toHaveLength(1);expect(screen.getByRole("button", { name: "Next" })).toBeDefined();- expect(capture).toHaveBeenCalledWith("hire_started", {- template_id: "tpl-maria",- source: "onboarding_hire_step",- });- expect(capture).toHaveBeenCalledWith("onboarding_expert_hired", {- template_id: "tpl-maria",- position: 0,- });+ // One hire_started per click; the hire itself is the backend's+ // expert_hired, so the browser sends no completion event.+ expect(+ capture.mock.calls.filter(([event]) => event === "hire_started"),+ ).toEqual([+ ["hire_started", { template_id: "tpl-maria", surface: "onboarding" }],+ ]);+ expect(capture).not.toHaveBeenCalledWith(+ "onboarding_expert_hired",+ expect.anything(),+ );});it("sends the funnel start and the DataFast hire goal", async () => {- // The backend emits this hire's hire_completed, so without the start the+ // The backend emits this hire's expert_hired, so without the start the// funnel's completion rate is unreadable for onboarding hires.configureCookiebot();installCookiebot({ statistics: true });@@ -209,6 +214,7 @@ describe("HireStep — hiring", () => {await waitFor(() =>expect(capture).toHaveBeenCalledWith("hire_started", {template_id: "tpl-maria",+ surface: "onboarding",}),);await waitFor(() =>diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/HireStep/useHireStep.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/HireStep/useHireStep.tsindex 3a8596f2b8..9456f5a0c5 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/HireStep/useHireStep.ts+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/HireStep/useHireStep.ts@@ -68,7 +68,7 @@ export function useHireStep() {trackBrainDump("expert_recommended", {template_id: expert.template_id,position,- source: team?.source ?? null,+ team_source: team?.source ?? null,});});}, [team, isPending]);@@ -81,24 +81,19 @@ export function useHireStep() {template_id: expert.template_id,position,});- trackBrainDump("hire_started", {+ trackFunnel("hire_started", {template_id: expert.template_id,- source: "onboarding_hire_step",+ surface: "onboarding",});- // The backend counts this hire's completion, so the funnel sink needs its- // start too — otherwise onboarding hires make completion rate unreadable.- trackFunnel("hire_started", { template_id: expert.template_id });setHiringTemplateId(expert.template_id);try {- await hireExpert({ data: { template_id: expert.template_id } });+ await hireExpert({+ data: { template_id: expert.template_id, surface: "onboarding" },+ });analytics.sendDatafastEvent("hire_completed", {template_id: expert.template_id,});markHired(expert.template_id);- trackBrainDump("onboarding_expert_hired", {- template_id: expert.template_id,- position,- });// Best effort: the roster is not on screen here, and a failed// invalidation must not read as a failed hire.await invalidateExpertRosterQueries(queryClient).catch(() => undefined);diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/SubscriptionStep.trial.test.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/SubscriptionStep.trial.test.tsxindex f8e59a64d0..c3348214d0 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/SubscriptionStep.trial.test.tsx+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/SubscriptionStep.trial.test.tsx@@ -267,7 +267,7 @@ test("does not surface an eligible offer after its trial has converted", async (render(<SubscriptionStep />);await waitFor(() =>expect(posthog.capture).toHaveBeenCalledWith(- "subscription_trial_offer_viewed",+ "trial_offer_viewed",expect.any(Object),),);diff --git a/autogpt_platform/frontend/src/app/(platform)/PaywallGate/__tests__/PaywallModal.trial.test.tsx b/autogpt_platform/frontend/src/app/(platform)/PaywallGate/__tests__/PaywallModal.trial.test.tsxindex 2c50e602c9..50667b62ba 100644--- a/autogpt_platform/frontend/src/app/(platform)/PaywallGate/__tests__/PaywallModal.trial.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/PaywallGate/__tests__/PaywallModal.trial.test.tsx@@ -179,7 +179,7 @@ test("reports the offer impression against the billing surface", async () => {await screen.findByRole("button", { name: "Start 7-day trial" });expect(posthog.capture).toHaveBeenCalledWith(- "subscription_trial_offer_viewed",+ "trial_offer_viewed",expect.objectContaining({ surface: "billing" }),);});diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/legacy-builder/agent-run-draft-view.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/legacy-builder/agent-run-draft-view.tsxdeleted file mode 100644index bebd8006d6..0000000000--- a/autogpt_platform/frontend/src/app/(platform)/build/components/legacy-builder/agent-run-draft-view.tsx+++ /dev/null@@ -1,818 +0,0 @@-"use client";-import React, {- useCallback,- useContext,- useEffect,- useMemo,- useState,-} from "react";--import {- CredentialsMetaInput,- CredentialsType,- Graph,- GraphExecutionID,- LibraryAgentPreset,- LibraryAgentPresetID,- LibraryAgentPresetUpdatable,- Schedule,-} from "@/lib/autogpt-server-api";-import { useBackendAPI } from "@/lib/autogpt-server-api/context";--import { RunAgentInputs } from "@/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/modals/RunAgentInputs/RunAgentInputs";-import { ScheduleTaskDialog } from "@/components/contextual/CronScheduler/cron-scheduler-dialog";-import ActionButtonGroup from "@/components/__legacy__/action-button-group";-import type { ButtonAction } from "@/components/__legacy__/types";-import {- Card,- CardContent,- CardHeader,- CardTitle,-} from "@/components/__legacy__/ui/card";-import {- IconCross,- IconPlay,- IconSave,-} from "@/components/__legacy__/ui/icons";-import { Input } from "@/components/__legacy__/ui/input";-import { Button } from "@/components/atoms/Button/Button";-import { CredentialsGroupedView } from "@/components/contextual/CredentialsInput/components/CredentialsGroupedView/CredentialsGroupedView";-import {- findSavedCredentialByProviderAndType,- findSavedUserCredentialByProviderAndType,-} from "@/components/contextual/CredentialsInput/components/CredentialsGroupedView/helpers";-import { InformationTooltip } from "@/components/molecules/InformationTooltip/InformationTooltip";-import {- useToast,- useToastOnFail,-} from "@/components/molecules/Toast/use-toast";-import { humanizeCronExpression } from "@/lib/cron-expression-utils";-import { cn, isEmpty } from "@/lib/utils";-import { CredentialsProvidersContext } from "@/providers/agent-credentials/credentials-provider";-import { CalendarClockIcon, Trash2Icon } from "lucide-react";--import { analytics } from "@/services/analytics";-import {- AgentStatus,- AgentStatusChip,-} from "@/app/(platform)/build/components/legacy-builder/agent-status-chip";-import {- Clock01Icon,- Copy01Icon,- InformationCircleIcon,-} from "@hugeicons/core-free-icons";-import { Icon } from "@/components/atoms/Icon/Icon";--export function AgentRunDraftView({- graph,- agentPreset,- doRun: _doRun,- onRun,- onCreatePreset,- onUpdatePreset,- doDeletePreset,- doCreateSchedule: _doCreateSchedule,- onCreateSchedule,- agentActions,- className,- recommendedScheduleCron,-}: {- graph: Graph;- agentActions?: ButtonAction[];- recommendedScheduleCron?: string | null;- doRun?: (- inputs: Record<string, any>,- credentialsInputs: Record<string, CredentialsMetaInput>,- ) => Promise<void>;- onRun?: (runID: GraphExecutionID) => void;- doCreateSchedule?: (- cronExpression: string,- scheduleName: string,- inputs: Record<string, any>,- credentialsInputs: Record<string, CredentialsMetaInput>,- ) => Promise<void>;- onCreateSchedule?: (schedule: Schedule) => void;- className?: string;-} & (- | {- onCreatePreset?: (preset: LibraryAgentPreset) => void;- agentPreset?: never;- onUpdatePreset?: never;- doDeletePreset?: never;- }- | {- onCreatePreset?: never;- agentPreset: LibraryAgentPreset;- onUpdatePreset: (preset: LibraryAgentPreset) => void;- doDeletePreset: (presetID: LibraryAgentPresetID) => void;- }-)): React.ReactNode {- const api = useBackendAPI();- const { toast } = useToast();- const toastOnFail = useToastOnFail();- const allProviders = useContext(CredentialsProvidersContext);-- const [inputValues, setInputValues] = useState<Record<string, any>>({});- const [inputCredentials, setInputCredentials] = useState<- Record<string, CredentialsMetaInput>- >({});- const [presetName, setPresetName] = useState<string>("");- const [presetDescription, setPresetDescription] = useState<string>("");- const [changedPresetAttributes, setChangedPresetAttributes] = useState<- Set<keyof LibraryAgentPresetUpdatable>- >(new Set());- const [cronScheduleDialogOpen, setCronScheduleDialogOpen] = useState(false);-- // Update values if agentPreset parameter is changed- useEffect(() => {- setInputValues(agentPreset?.inputs ?? {});- setInputCredentials(agentPreset?.credentials ?? {});- setPresetName(agentPreset?.name ?? "");- setPresetDescription(agentPreset?.description ?? "");- setChangedPresetAttributes(new Set());- }, [agentPreset]);-- const agentInputSchema = useMemo(- () => graph.trigger_setup_info?.config_schema ?? graph.input_schema,- [graph],- );- const agentInputFields = useMemo(- () =>- Object.fromEntries(- Object.entries(agentInputSchema.properties).filter(- ([_, subSchema]) => !subSchema.hidden,- ),- ),- [agentInputSchema],- );- const agentCredentialsInputFields = useMemo(- () => graph.credentials_input_schema.properties,- [graph],- );- const credentialFields = useMemo(- function getCredentialFields() {- return Object.entries(agentCredentialsInputFields);- },- [agentCredentialsInputFields],- );- const requiredCredentials = useMemo(- function getRequiredCredentials() {- return new Set(- (graph.credentials_input_schema?.required as string[]) || [],- );- },- [graph.credentials_input_schema?.required],- );-- useEffect(- function initializeDefaultCredentials() {- if (!allProviders) return;- if (!graph.credentials_input_schema?.properties) return;- if (requiredCredentials.size === 0) return;-- setInputCredentials(function updateCredentials(currentCreds) {- const next = { ...currentCreds };- let didAdd = false;-- for (const key of requiredCredentials) {- if (next[key]) continue;- const schema = graph.credentials_input_schema.properties[key];- if (!schema) continue;-- const providerNames = schema.credentials_provider || [];- const credentialTypes = schema.credentials_types || [];- const requiredScopes = schema.credentials_scopes;-- const userCredential = findSavedUserCredentialByProviderAndType(- providerNames,- credentialTypes,- requiredScopes,- allProviders,- );-- const savedCredential =- userCredential ||- findSavedCredentialByProviderAndType(- providerNames,- credentialTypes,- requiredScopes,- allProviders,- );-- if (!savedCredential) continue;-- next[key] = {- id: savedCredential.id,- provider: savedCredential.provider,- type: savedCredential.type as CredentialsType,- title: savedCredential.title,- };- didAdd = true;- }-- if (!didAdd) return currentCreds;- return next;- });- },- [- allProviders,- graph.credentials_input_schema?.properties,- requiredCredentials,- ],- );-- const [allRequiredInputsAreSet, missingInputs] = useMemo(() => {- const nonEmptyInputs = new Set(- Object.keys(inputValues).filter((k) => !isEmpty(inputValues[k])),- );- const requiredInputs = new Set(- agentInputSchema.required as string[] | undefined,- );- // Backwards-compatible implementation of isSupersetOf and difference- const isSuperset = Array.from(requiredInputs).every((item) =>- nonEmptyInputs.has(item),- );- const difference = Array.from(requiredInputs).filter(- (item) => !nonEmptyInputs.has(item),- );- return [isSuperset, difference];- }, [agentInputSchema.required, inputValues]);- const [allCredentialsAreSet, missingCredentials] = useMemo(- function getCredentialStatus() {- const missing = Array.from(requiredCredentials).filter((key) => {- const cred = inputCredentials[key];- return !cred || !cred.id;- });- return [missing.length === 0, missing];- },- [requiredCredentials, inputCredentials],- );- function addChangedCredentials(prev: Set<keyof LibraryAgentPresetUpdatable>) {- const next = new Set(prev);- next.add("credentials");- return next;- }-- function handleCredentialChange(key: string, value?: CredentialsMetaInput) {- setInputCredentials(function updateInputCredentials(currentCreds) {- const next = { ...currentCreds };- if (value === undefined) {- delete next[key];- return next;- }- next[key] = value;- return next;- });- setChangedPresetAttributes(addChangedCredentials);- }-- const notifyMissingInputs = useCallback(- (needPresetName: boolean = true) => {- const allMissingFields = (- needPresetName && !presetName- ? [graph.has_external_trigger ? "trigger_name" : "preset_name"]- : []- )- .concat(missingInputs)- .concat(missingCredentials);- toast({- title: "⚠️ Not all required inputs are set",- description: `Please set ${allMissingFields.map((k) => `\`${k}\``).join(", ")}`,- });- },- [missingInputs, missingCredentials],- );-- const doRun = useCallback(async () => {- // Manually running webhook-triggered agents is not supported- if (graph.has_external_trigger) return;-- if (!agentPreset || changedPresetAttributes.size > 0) {- if (!allRequiredInputsAreSet || !allCredentialsAreSet) {- notifyMissingInputs(false);- return;- }- if (_doRun) {- await _doRun(inputValues, inputCredentials);- return;- }- // TODO: on executing preset with changes, ask for confirmation and offer save+run- const newRun = await api- .executeGraph(- graph.id,- graph.version,- inputValues,- inputCredentials,- "library",- )- .catch(toastOnFail("execute agent"));-- if (newRun && onRun) onRun(newRun.id);- } else {- await api- .executeLibraryAgentPreset(agentPreset.id)- .then((newRun) => onRun && onRun(newRun.id))- .catch(toastOnFail("execute agent preset"));- }-- analytics.sendDatafastEvent("run_agent", {- name: graph.name,- id: graph.id,- });- }, [api, graph, inputValues, inputCredentials, onRun, toastOnFail]);-- const doCreatePreset = useCallback(async () => {- if (!onCreatePreset) return;-- if (!presetName || !allRequiredInputsAreSet || !allCredentialsAreSet) {- notifyMissingInputs();- return;- }-- await api- .createLibraryAgentPreset({- name: presetName,- description: presetDescription,- graph_id: graph.id,- graph_version: graph.version,- inputs: inputValues,- credentials: inputCredentials,- })- .then((newPreset) => {- onCreatePreset(newPreset);- setChangedPresetAttributes(new Set()); // reset change tracker- })- .catch(toastOnFail("save agent preset"));- }, [- api,- graph,- presetName,- presetDescription,- inputValues,- inputCredentials,- onCreatePreset,- toast,- toastOnFail,- ]);-- const doUpdatePreset = useCallback(async () => {- if (!agentPreset || changedPresetAttributes.size == 0) return;-- if (!presetName || !allRequiredInputsAreSet || !allCredentialsAreSet) {- notifyMissingInputs();- return;- }-- const updatePreset: LibraryAgentPresetUpdatable = {};- if (changedPresetAttributes.has("name")) updatePreset["name"] = presetName;- if (changedPresetAttributes.has("description"))- updatePreset["description"] = presetDescription;- if (- changedPresetAttributes.has("inputs") ||- changedPresetAttributes.has("credentials")- ) {- updatePreset["inputs"] = inputValues;- updatePreset["credentials"] = inputCredentials;- }- await api- .updateLibraryAgentPreset(agentPreset.id, updatePreset)- .then((updatedPreset) => {- onUpdatePreset(updatedPreset);- setChangedPresetAttributes(new Set()); // reset change tracker- })- .catch(toastOnFail("update agent preset"));- }, [- api,- graph,- presetName,- presetDescription,- inputValues,- inputCredentials,- onUpdatePreset,- toast,- toastOnFail,- ]);-- const doSetPresetActive = useCallback(- async (active: boolean) => {- if (!agentPreset) return;- const updatedPreset = await api.updateLibraryAgentPreset(agentPreset.id, {- is_active: active,- });- onUpdatePreset(updatedPreset);- },- [agentPreset, api, onUpdatePreset],- );-- const doSetupTrigger = useCallback(async () => {- // Setting up a trigger for non-webhook-triggered agents is not supported- if (!graph.trigger_setup_info || !onCreatePreset) return;-- if (!presetName || !allRequiredInputsAreSet || !allCredentialsAreSet) {- notifyMissingInputs();- return;- }-- await api- .setupAgentTrigger({- name: presetName,- description: presetDescription,- graph_id: graph.id,- graph_version: graph.version,- trigger_config: inputValues,- agent_credentials: inputCredentials,- })- .then((newPreset) => {- onCreatePreset(newPreset);- setChangedPresetAttributes(new Set()); // reset change tracker- })- .catch(toastOnFail("set up agent trigger"));- }, [- api,- graph,- presetName,- presetDescription,- inputValues,- inputCredentials,- onCreatePreset,- toast,- toastOnFail,- ]);-- const openScheduleDialog = useCallback(() => {- // Scheduling is not supported for webhook-triggered agents- if (graph.has_external_trigger) return;-- if (!allRequiredInputsAreSet || !allCredentialsAreSet) {- notifyMissingInputs(false);- return;- }-- setCronScheduleDialogOpen(true);- }, [- graph,- allRequiredInputsAreSet,- allCredentialsAreSet,- notifyMissingInputs,- ]);-- const doSetupSchedule = useCallback(- async (cronExpression: string, scheduleName: string) => {- // Scheduling is not supported for webhook-triggered agents- if (graph.has_external_trigger) return;-- if (_doCreateSchedule) {- await _doCreateSchedule(- cronExpression,- scheduleName || graph.name,- inputValues,- inputCredentials,- );- return;- }- const schedule = await api- .createGraphExecutionSchedule({- graph_id: graph.id,- graph_version: graph.version,- name: scheduleName || graph.name,- cron: cronExpression,- inputs: inputValues,- credentials: inputCredentials,- })- .catch(toastOnFail("set up agent run schedule"));-- analytics.sendDatafastEvent("schedule_agent", {- name: graph.name,- id: graph.id,- cronExpression: cronExpression,- });-- if (schedule && onCreateSchedule) onCreateSchedule(schedule);- },- [api, graph, inputValues, inputCredentials, onCreateSchedule, toastOnFail],- );-- const runActions: ButtonAction[] = useMemo(- () => [- // "Regular" agent: [run] + [save as preset] buttons- ...(!graph.has_external_trigger- ? ([- {- label: (- <>- <CalendarClockIcon className="mr-2 size-4" /> Schedule run- </>- ),- variant: "accent",- callback: openScheduleDialog,- extraProps: { "data-testid": "agent-schedule-button" },- },- {- label: (- <>- <IconPlay className="mr-2 size-4" /> Manual run- </>- ),- callback: doRun,- extraProps: { "data-testid": "agent-run-button" },- },- // {- // label: (- // <>- // <IconSave className="mr-2 size-4" /> Save as a preset- // </>- // ),- // callback: doCreatePreset,- // disabled: !(- // presetName &&- // allRequiredInputsAreSet &&- // allCredentialsAreSet- // ),- // },- ] satisfies ButtonAction[])- : []),- // Triggered agent: [setup] button- ...(graph.has_external_trigger && !agentPreset?.webhook_id- ? ([- {- label: (- <>- <IconPlay className="mr-2 size-4" /> Set up trigger- </>- ),- variant: "accent",- callback: doSetupTrigger,- disabled: !(- presetName &&- allRequiredInputsAreSet &&- allCredentialsAreSet- ),- },- ] satisfies ButtonAction[])- : []),- // Existing agent trigger: [enable]/[disable] button- ...(agentPreset?.webhook_id- ? ([- agentPreset.is_active- ? {- label: (- <>- <IconCross className="mr-2.5 size-3.5" /> Disable trigger- </>- ),- variant: "destructive",- callback: () => doSetPresetActive(false),- }- : {- label: (- <>- <IconPlay className="mr-2 size-4" /> Enable trigger- </>- ),- variant: "accent",- callback: () => doSetPresetActive(true),- },- ] satisfies ButtonAction[])- : []),- // Existing agent preset/trigger: [save] and [delete] buttons- ...(agentPreset- ? ([- {- label: (- <>- <IconSave className="mr-2 size-4" /> Save changes- </>- ),- callback: doUpdatePreset,- disabled: !(- changedPresetAttributes.size > 0 &&- presetName &&- allRequiredInputsAreSet &&- allCredentialsAreSet- ),- },- {- label: (- <>- <Trash2Icon className="mr-2 size-4" />- Delete {graph.has_external_trigger ? "trigger" : "preset"}- </>- ),- callback: () => doDeletePreset(agentPreset.id),- },- ] satisfies ButtonAction[])- : []),- ],- [- graph.has_external_trigger,- agentPreset,- doRun,- doSetupTrigger,- doCreatePreset,- doUpdatePreset,- doDeletePreset,- openScheduleDialog,- changedPresetAttributes,- presetName,- allRequiredInputsAreSet,- allCredentialsAreSet,- ],- );-- const triggerStatus: AgentStatus | null = !agentPreset- ? null- : !agentPreset.webhook- ? "broken"- : agentPreset.is_active- ? "active"- : "inactive";-- return (- <div className={cn("agpt-div flex gap-6", className)}>- <div className="flex min-w-0 flex-1 flex-col gap-4">- {graph.trigger_setup_info && agentPreset && (- <Card className="agpt-box">- <CardHeader className="flex-row items-center justify-between">- <CardTitle className="font-poppins text-lg">- Trigger status- </CardTitle>- {triggerStatus && <AgentStatusChip status={triggerStatus} />}- </CardHeader>- <CardContent className="flex flex-col gap-4">- {!agentPreset.webhook_id ? (- /* Shouldn't happen, but technically possible */- <p className="text-sm text-destructive">- This trigger is not attached to a webhook. Use "Set up- trigger" to fix this.- </p>- ) : !graph.trigger_setup_info.credentials_input_name ? (- /* Expose webhook URL if not auto-setup */- <div className="text-sm">- <p>- This trigger is ready to be used. Use the Webhook URL below- to set up the trigger connection with the service of your- choosing.- </p>- <div className="nodrag mt-5 flex flex-col gap-1">- Webhook URL:- <div className="flex gap-2 rounded-md bg-gray-50 p-2">- <code className="ph-no-capture select-all text-sm">- {agentPreset.webhook.url}- </code>- <Button- variant="outline"- size="icon"- className="size-7 flex-none p-1"- onClick={() =>- agentPreset.webhook &&- navigator.clipboard.writeText(agentPreset.webhook.url)- }- title="Copy webhook URL"- >- <Icon icon={Copy01Icon} className="size-4" />- </Button>- </div>- </div>- </div>- ) : (- <p className="text-sm text-muted-foreground">- This agent trigger is{" "}- {agentPreset.is_active- ? "ready. When a trigger is received, it will run with the provided settings."- : "disabled. It will not respond to triggers until you enable it."}- </p>- )}- </CardContent>- </Card>- )}-- <Card className="agpt-box">- <CardHeader>- <CardTitle className="font-poppins text-lg">Input</CardTitle>- </CardHeader>- <CardContent className="flex flex-col gap-4">- {/* Schedule recommendation tip */}- {recommendedScheduleCron && !graph.has_external_trigger && (- <div className="flex items-center gap-2 rounded-md border border-violet-200 bg-violet-50 p-3">- <Icon icon={Clock01Icon} className="h-4 w-4 text-violet-600" />- <p className="text-sm text-violet-800">- <strong>Tip:</strong> For best results, run this agent{" "}- {humanizeCronExpression(- recommendedScheduleCron,- ).toLowerCase()}- </p>- </div>- )}-- {/* Setup Instructions */}- {graph.instructions && (- <div className="flex items-start gap-2 rounded-md border border-violet-200 bg-violet-50 p-3">- <Icon- icon={InformationCircleIcon}- className="mt-0.5 h-4 w-4 flex-shrink-0 text-violet-600"- />- <div className="text-sm text-violet-800">- <strong>Setup Instructions:</strong>{" "}- <span className="whitespace-pre-wrap">- {graph.instructions}- </span>- </div>- </div>- )}-- {(agentPreset || graph.has_external_trigger) && (- <>- {/* Preset name and description */}- <div className="flex flex-col space-y-2">- <label className="flex items-center gap-1 text-sm font-medium">- {graph.has_external_trigger ? "Trigger" : "Preset"} Name- <InformationTooltip- description={`Name of the ${graph.has_external_trigger ? "trigger" : "preset"} you are setting up`}- />- </label>- <Input- value={presetName}- placeholder={`Enter ${graph.has_external_trigger ? "trigger" : "preset"} name`}- onChange={(e) => {- setPresetName(e.target.value);- setChangedPresetAttributes((prev) => prev.add("name"));- }}- />- </div>- <div className="flex flex-col space-y-2">- <label className="flex items-center gap-1 text-sm font-medium">- {graph.has_external_trigger ? "Trigger" : "Preset"}{" "}- Description- <InformationTooltip- description={`Description of the ${graph.has_external_trigger ? "trigger" : "preset"} you are setting up`}- />- </label>- <Input- value={presetDescription}- placeholder={`Enter ${graph.has_external_trigger ? "trigger" : "preset"} description`}- onChange={(e) => {- setPresetDescription(e.target.value);- setChangedPresetAttributes((prev) =>- prev.add("description"),- );- }}- />- </div>- </>- )}-- {/* Regular inputs */}- {Object.entries(agentInputFields).map(([key, inputSubSchema]) => (- <RunAgentInputs- key={key}- schema={inputSubSchema}- value={inputValues[key] ?? inputSubSchema.default}- placeholder={inputSubSchema.description}- onChange={(value) => {- setInputValues((obj) => ({- ...obj,- [key]: value,- }));- setChangedPresetAttributes((prev) => prev.add("inputs"));- }}- data-testid={`agent-input-${key}`}- />- ))}-- {/* Credentials inputs */}- {credentialFields.length > 0 && (- <CredentialsGroupedView- credentialFields={credentialFields}- requiredCredentials={requiredCredentials}- inputCredentials={inputCredentials}- inputValues={inputValues}- onCredentialChange={handleCredentialChange}- />- )}- </CardContent>- </Card>- </div>-- {/* Actions */}- <aside className="w-48 xl:w-56">- <div className="flex flex-col gap-8">- <ActionButtonGroup- title={`${graph.has_external_trigger ? "Trigger" : agentPreset ? "Preset" : "Run"} actions`}- actions={runActions}- />- <ScheduleTaskDialog- open={cronScheduleDialogOpen}- setOpen={setCronScheduleDialogOpen}- onSubmit={doSetupSchedule}- defaultScheduleName={graph.name}- defaultCronExpression={recommendedScheduleCron || undefined}- />-- {agentActions && agentActions.length > 0 && (- <ActionButtonGroup title="Agent actions" actions={agentActions} />- )}- </div>- </aside>- </div>- );-}diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/legacy-builder/agent-status-chip.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/legacy-builder/agent-status-chip.tsxdeleted file mode 100644index 60c4a5a944..0000000000--- a/autogpt_platform/frontend/src/app/(platform)/build/components/legacy-builder/agent-status-chip.tsx+++ /dev/null@@ -1,41 +0,0 @@-import React from "react";--import { Badge } from "@/components/__legacy__/ui/badge";--export type AgentStatus = "active" | "inactive" | "error" | "broken";--const statusData: Record<- AgentStatus,- { label: string; variant: keyof typeof statusStyles }-> = {- active: { label: "Active", variant: "success" },- error: { label: "Error", variant: "destructive" },- broken: { label: "Broken", variant: "destructive" },- inactive: { label: "Inactive", variant: "secondary" },-};--const statusStyles = {- success:- "bg-green-100 text-green-800 hover:bg-green-100 hover:text-green-800",- destructive: "bg-red-100 text-red-800 hover:bg-red-100 hover:text-red-800",- warning:- "bg-yellow-100 text-yellow-800 hover:bg-yellow-100 hover:text-yellow-800",- info: "bg-blue-100 text-blue-800 hover:bg-blue-100 hover:text-blue-800",- secondary:- "bg-slate-100 text-slate-800 hover:bg-slate-100 hover:text-slate-800",-};--export function AgentStatusChip({- status,-}: {- status: AgentStatus;-}): React.ReactElement {- return (- <Badge- variant="secondary"- className={`text-xs font-medium ${statusStyles[statusData[status]?.variant]} rounded-[45px] px-[9px] py-[3px]`}- >- {statusData[status]?.label}- </Badge>- );-}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/voice/__tests__/useVoiceMode.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/voice/__tests__/useVoiceMode.test.tsxindex 4a746dd1ce..6a97a0b0b8 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/voice/__tests__/useVoiceMode.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/voice/__tests__/useVoiceMode.test.tsx@@ -543,7 +543,6 @@ describe("useVoiceMode", () => {const events = tracked.map(([e]) => e);expect(events).toContain("voice_mode_started");- expect(events).toContain("voice_transcribe_latency_ms");expect(events).toContain("voice_turn_sent");expect(events).toContain("voice_turn_completed");// Without this the funnel cannot tell one turn from ten.@@ -551,6 +550,99 @@ describe("useVoiceMode", () => {expect(sent?.[1]?.turn_index).toBe(1);});+ it("carries the latencies on the turn events instead of sending their own", async () => {+ const view = render({});+ await enable(view);+ await speak();+ await reply(view, "On it. Building that now.");+ await waitFor(() => expect(view.result.current.state).toBe("listening"));++ const events = tracked.map(([e]) => e);+ expect(events).not.toContain("voice_transcribe_latency_ms");+ expect(events).not.toContain("voice_first_sound_latency_ms");+ const sent = tracked.find(([e]) => e === "voice_turn_sent");+ expect(sent?.[1]?.transcribe_latency_ms).toEqual(expect.any(Number));+ const completed = tracked.find(([e]) => e === "voice_turn_completed");+ expect(completed?.[1]).toHaveProperty("first_sound_latency_ms");+ });++ it("keeps the first-sound latency of a reply the user cut off", async () => {+ const play = vi+ .spyOn(window.HTMLMediaElement.prototype, "play")+ .mockImplementation(() => Promise.resolve());+ const view = render({});+ await enable(view);+ await speak();+ await act(async () => {+ view.rerender({+ messages: assistant("First sentence. Second half"),+ isStreaming: true,+ });+ });+ await waitFor(() => expect(play).toHaveBeenCalled());++ await act(async () => view.result.current.toggle());++ const events = tracked.map(([e]) => e);+ expect(events).not.toContain("voice_turn_completed");+ const dropped = tracked.filter(([e]) => e === "voice_turn_dropped");+ expect(dropped).toHaveLength(1);+ expect(dropped[0][1]).toMatchObject({+ reason: "interrupted",+ turn_index: 1,+ first_sound_latency_ms: expect.any(Number),+ });+ expect(events).toContain("voice_mode_stopped");+ });++ it("reports a turn left mid-thought as interrupted, once", async () => {+ const view = render({});+ await enable(view);+ await speak();+ expect(view.result.current.state).toBe("thinking");++ view.unmount();++ const dropped = tracked.filter(([e]) => e === "voice_turn_dropped");+ expect(dropped).toHaveLength(1);+ expect(dropped[0][1]).toMatchObject({+ reason: "interrupted",+ first_sound_latency_ms: null,+ });+ });++ it("does not complete a turn whose reply was still playing on unmount", async () => {+ const play = vi+ .spyOn(window.HTMLMediaElement.prototype, "play")+ .mockImplementation(() => Promise.resolve());+ const view = render({});+ await enable(view);+ await speak();+ await reply(view, "On it. Building that now.");+ await waitFor(() => expect(play).toHaveBeenCalled());+ expect(view.result.current.state).toBe("speaking");++ view.unmount();+ await act(async () => undefined);++ const events = tracked.map(([e]) => e);+ expect(events).not.toContain("voice_turn_completed");+ expect(events.filter((e) => e === "voice_turn_dropped")).toHaveLength(1);+ });++ it("drops nothing when voice mode is switched off between turns", async () => {+ const view = render({});+ await enable(view);+ await speak();+ await reply(view, "On it. Building that now.");+ await waitFor(() => expect(view.result.current.state).toBe("listening"));++ await act(async () => view.result.current.toggle());+ view.unmount();++ expect(tracked.map(([e]) => e)).not.toContain("voice_turn_dropped");+ });+it("distinguishes a silence timeout from the user leaving", async () => {vi.useFakeTimers();const view = render({ silenceTimeoutMs: 8000 });@@ -572,6 +664,7 @@ describe("useVoiceMode", () => {const dropped = tracked.find(([e]) => e === "voice_turn_dropped");expect(dropped?.[1]?.reason).toBe("filler_or_empty");+ expect(dropped?.[1]?.transcribe_latency_ms).toEqual(expect.any(Number));});it("marks turns as voice turns only while voice mode is on", async () => {diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/voice/useVoiceMode.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/voice/useVoiceMode.tsindex 427bd2a90b..4f21f92df9 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/voice/useVoiceMode.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/voice/useVoiceMode.ts@@ -95,6 +95,8 @@ export function useVoiceMode({const turnIndex = useRef(0);/** Speech end, for the two latencies the funnel measures. */const utteranceEndedAt = useRef(0);+ /** Speech end to the reply's first sound, sent on `voice_turn_completed`. */+ const firstSoundLatencyMs = useRef<number | null>(null);const wasInFlight = useRef(false);// Bumped by every activate and deactivate. Work started under an older// token belongs to a session the user has already left.@@ -209,6 +211,7 @@ export function useVoiceMode({}function deactivate(reason: "user" | "silence_timeout" = "user") {+ trackInterruptedTurn();if (stateRef.current !== "off") {trackVoiceMode(reason === "silence_timeout"@@ -229,6 +232,21 @@ export function useVoiceMode({dispatch({ type: "DISABLE" });}+ // A turn cut off before its reply finished never reaches+ // `voice_turn_completed`, and the slow replies are the ones most likely to+ // be cut off, so their first-sound latency goes out on the drop instead.+ function trackInterruptedTurn() {+ if (stateRef.current !== "thinking" && stateRef.current !== "speaking") {+ return;+ }+ trackVoiceMode("voice_turn_dropped", {+ reason: "interrupted",+ turn_index: turnIndex.current,+ first_sound_latency_ms: firstSoundLatencyMs.current,+ });+ firstSoundLatencyMs.current = null;+ }+function setStarting(value: boolean) {starting.current = value;setIsStarting(value);@@ -310,9 +328,7 @@ export function useVoiceMode({dispatch({ type: "TRANSCRIPT_DROPPED" });return;}- trackVoiceMode("voice_transcribe_latency_ms", {- ms: Date.now() - utteranceEndedAt.current,- });+ const transcribeLatencyMs = Date.now() - utteranceEndedAt.current;// Transcription takes a second or two. Sending a turn the user opted out// of during it is worse than losing the utterance.@@ -323,16 +339,21 @@ export function useVoiceMode({if (isRejectableTranscript(transcript)) {playerRef.current?.stop();- trackVoiceMode("voice_turn_dropped", { reason: "filler_or_empty" });+ trackVoiceMode("voice_turn_dropped", {+ reason: "filler_or_empty",+ transcribe_latency_ms: transcribeLatencyMs,+ });dispatch({ type: "TRANSCRIPT_DROPPED" });return;}startTurn();turnIndex.current += 1;+ firstSoundLatencyMs.current = null;trackVoiceMode("voice_turn_sent", {turn_index: turnIndex.current,transcript_chars: transcript.trim().length,+ transcribe_latency_ms: transcribeLatencyMs,});try {await inputs.current.onSend(transcript.trim());@@ -438,7 +459,11 @@ export function useVoiceMode({const next = voiceReduce(stateRef.current, event);if (next === stateRef.current) return;if (event.type === "REPLY_DONE") {- trackVoiceMode("voice_turn_completed", { turn_index: turnIndex.current });+ trackVoiceMode("voice_turn_completed", {+ turn_index: turnIndex.current,+ first_sound_latency_ms: firstSoundLatencyMs.current,+ });+ firstSoundLatencyMs.current = null;}stateRef.current = next;setState(next);@@ -471,9 +496,7 @@ export function useVoiceMode({},onPlaybackStart: () => {if (!utteranceEndedAt.current) return;- trackVoiceMode("voice_first_sound_latency_ms", {- ms: Date.now() - utteranceEndedAt.current,- });+ firstSoundLatencyMs.current = Date.now() - utteranceEndedAt.current;utteranceEndedAt.current = 0;},onError: (error) => {@@ -494,9 +517,13 @@ export function useVoiceMode({// Unmount without deactivate — navigating away mid-session. A mic session// still starting would otherwise finish after this, open the mic, and// flag later text turns as voice turns.+ trackInterruptedTurn();activation.current += 1;setVoiceTurnActive(false);clearTimers();+ // Off before the player stops: its idle callback would otherwise finish+ // the turn it just reported as dropped.+ dispatch({ type: "DISABLE" });playerRef.current?.destroy();void vadRef.current?.destroy();}diff --git a/autogpt_platform/frontend/src/app/(platform)/marketplace/components/ExpertsSection/components/ExpertHireControl.tsx b/autogpt_platform/frontend/src/app/(platform)/marketplace/components/ExpertsSection/components/ExpertHireControl.tsxindex e71ef79100..be06dbd879 100644--- a/autogpt_platform/frontend/src/app/(platform)/marketplace/components/ExpertsSection/components/ExpertHireControl.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/marketplace/components/ExpertsSection/components/ExpertHireControl.tsx@@ -8,7 +8,6 @@ import { Dialog } from "@/components/molecules/Dialog/Dialog";import { VoicePicker } from "@/components/organisms/VoicePicker/VoicePicker";import { useAuth } from "@/lib/auth/hooks/useAuth";import { trackFunnel } from "@/services/experts/experts-analytics";-import { markHireStarted } from "@/services/experts/hire-timing";import { useHireFlow } from "@/services/experts/useHireFlow";import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";import { AddTeamIcon, CheckmarkCircle02Icon } from "@hugeicons/core-free-icons";@@ -54,11 +53,13 @@ function EnabledExpertHireControl({ expert, isHired }: Props) {} = useHireFlow(expert);const isInFlight = isHiring || isVoicePickOpen || hireResult !== null;- // The clock starts on the click, not on the request: the flow finishes in- // a dialog, and sometimes on another page entirely.+ // The card runs the expert page's hire flow, which reports the hire as+ // `expert_page`, so its hire_started does too.function handleHire() {- markHireStarted(expert.id);- trackFunnel("hire_started", { template_id: expert.id });+ trackFunnel("hire_started", {+ template_id: expert.id,+ surface: "expert_page",+ });hire();}diff --git a/autogpt_platform/frontend/src/app/(platform)/marketplace/experts/[expertId]/components/ExpertHireActions.tsx b/autogpt_platform/frontend/src/app/(platform)/marketplace/experts/[expertId]/components/ExpertHireActions.tsxindex fd6ff6d3b3..5cb2c72cb4 100644--- a/autogpt_platform/frontend/src/app/(platform)/marketplace/experts/[expertId]/components/ExpertHireActions.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/marketplace/experts/[expertId]/components/ExpertHireActions.tsx@@ -3,7 +3,6 @@ import { Badge } from "@/components/atoms/Badge/Badge";import { Button } from "@/components/atoms/Button/Button";import { Icon } from "@/components/atoms/Icon/Icon";import { trackFunnel } from "@/services/experts/experts-analytics";-import { markHireStarted } from "@/services/experts/hire-timing";import { CheckmarkCircle02Icon } from "@hugeicons/core-free-icons";// Sized and shaped like the small button beside it so the pair reads as one row.@@ -66,11 +65,11 @@ export function ExpertHireActions({);}- // The clock starts on the click, not on the request: the hire flow finishes- // in a dialog, and sometimes on another page entirely.function handleHire() {- markHireStarted(expert.id);- trackFunnel("hire_started", { template_id: expert.id });+ trackFunnel("hire_started", {+ template_id: expert.id,+ surface: "expert_page",+ });onHire();}diff --git a/autogpt_platform/frontend/src/app/(platform)/marketplace/experts/__tests__/expert-page.test.tsx b/autogpt_platform/frontend/src/app/(platform)/marketplace/experts/__tests__/expert-page.test.tsxindex cba36f2874..85e5a007c7 100644--- a/autogpt_platform/frontend/src/app/(platform)/marketplace/experts/__tests__/expert-page.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/marketplace/experts/__tests__/expert-page.test.tsx@@ -238,7 +238,7 @@ describe("Marketplace expert page", () => {await waitFor(() =>expect(funnelCalls().find((body) => body.type === "hire_started")?.data,- ).toEqual({ template_id: mariaTemplate.id }),+ ).toEqual({ template_id: mariaTemplate.id, surface: "expert_page" }),);});diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 0f741fbd1b..3754f0f2b4 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -2785,74 +2785,6 @@"security": [{ "HTTPBearerJWT": [] }]}},- "/api/analytics/log_raw_analytics": {- "post": {- "tags": ["analytics"],- "summary": "Log Raw Analytics",- "operationId": "postAnalyticsLogRawAnalytics",- "requestBody": {- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/Body_postAnalyticsLogRawAnalytics"- }- }- },- "required": true- },- "responses": {- "200": {- "description": "Successful Response",- "content": { "application/json": { "schema": {} } }- },- "401": {- "$ref": "#/components/responses/HTTP401NotAuthenticatedError"- },- "422": {- "description": "Validation Error",- "content": {- "application/json": {- "schema": { "$ref": "#/components/schemas/HTTPValidationError" }- }- }- }- },- "security": [{ "HTTPBearerJWT": [] }]- }- },- "/api/analytics/log_raw_metric": {- "post": {- "tags": ["analytics"],- "summary": "Log Raw Metric",- "operationId": "postAnalyticsLogRawMetric",- "requestBody": {- "content": {- "application/json": {- "schema": { "$ref": "#/components/schemas/LogRawMetricRequest" }- }- },- "required": true- },- "responses": {- "200": {- "description": "Successful Response",- "content": { "application/json": { "schema": {} } }- },- "401": {- "$ref": "#/components/responses/HTTP401NotAuthenticatedError"- },- "422": {- "description": "Validation Error",- "content": {- "application/json": {- "schema": { "$ref": "#/components/schemas/HTTPValidationError" }- }- }- }- },- "security": [{ "HTTPBearerJWT": [] }]- }- },"/api/api-keys": {"get": {"tags": ["v1", "api-keys"],@@ -18400,25 +18332,6 @@"required": ["is_active"],"title": "Body_patchOauthUpdateAppStatus"},- "Body_postAnalyticsLogRawAnalytics": {- "properties": {- "type": { "type": "string", "title": "Type" },- "data": {- "additionalProperties": true,- "type": "object",- "title": "Data",- "description": "The data to log"- },- "data_index": {- "type": "string",- "title": "Data Index",- "description": "Indexable field for any count based analytical measures like page order clicking, tutorial step completion, etc."- }- },- "type": "object",- "required": ["type", "data", "data_index"],- "title": "Body_postAnalyticsLogRawAnalytics"- },"Body_postOauthIntrospect": {"properties": {"token": {@@ -23948,6 +23861,16 @@{ "type": "null" }],"title": "Name"+ },+ "surface": {+ "anyOf": [+ {+ "type": "string",+ "enum": ["onboarding", "expert_page", "copilot"]+ },+ { "type": "null" }+ ],+ "title": "Surface"}},"type": "object",@@ -25432,24 +25355,6 @@"title": "ListSessionsResponse","description": "Response model for listing chat sessions."},- "LogRawMetricRequest": {- "properties": {- "metric_name": {- "type": "string",- "minLength": 1,- "title": "Metric Name"- },- "metric_value": { "type": "number", "title": "Metric Value" },- "data_string": {- "type": "string",- "minLength": 1,- "title": "Data String"- }- },- "type": "object",- "required": ["metric_name", "metric_value", "data_string"],- "title": "LogRawMetricRequest"- },"LoginResponse": {"properties": {"login_url": { "type": "string", "title": "Login Url" },diff --git a/autogpt_platform/frontend/src/components/organisms/TrialCard/useTrialCard.ts b/autogpt_platform/frontend/src/components/organisms/TrialCard/useTrialCard.tsindex 23fddf8225..72946e4d30 100644--- a/autogpt_platform/frontend/src/components/organisms/TrialCard/useTrialCard.ts+++ b/autogpt_platform/frontend/src/components/organisms/TrialCard/useTrialCard.ts@@ -31,7 +31,7 @@ export function useTrialCard(returnTo: "onboarding" | "billing") {const identity = `${userID}:${offer.token}`;if (seenOffer.current === identity) return;seenOffer.current = identity;- posthog?.capture(TrialEvent.SUBSCRIPTION_TRIAL_OFFER_VIEWED, {+ posthog?.capture(TrialEvent.TRIAL_OFFER_VIEWED, {trial_offer_version: offer.version,subscription_tier: offer.tier,trial_duration_days: offer.duration_days,diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/client.tsindex 9732cbc2a6..2019e6a87f 100644--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts@@ -7,8 +7,6 @@ import { Key, storage } from "@/services/storage/local-storage";import * as Sentry from "@sentry/nextjs";import type {AddUserCreditsResponse,- AnalyticsDetails,- AnalyticsMetrics,APIKey,APIKeyCredentials,APIKeyPermission,@@ -458,14 +456,6 @@ export default class BackendAPI {return this._request("POST", `/integrations/webhooks/${webhook_id}/ping`);}- logMetric(metric: AnalyticsMetrics) {- return this._request("POST", "/analytics/log_raw_metric", metric);- }-- logAnalytic(analytic: AnalyticsDetails) {- return this._request("POST", "/analytics/log_raw_analytics", analytic);- }-async uploadFile(file: File,expiration_hours: number = 24,diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/types.tsindex f392fab4eb..6e24c9f249 100644--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts@@ -779,18 +779,6 @@ export enum SpecialBlockID {OUTPUT = "363ae599-353e-4804-937e-b2ee3cef3da4",}-export type AnalyticsMetrics = {- metric_name: string;- metric_value: number;- data_string: string;-};--export type AnalyticsDetails = {- type: string;- data: Record<string, any>;- index: string;-};-export type Pagination = {total_items: number;total_pages: number;diff --git a/autogpt_platform/frontend/src/providers/posthog/posthog-provider.tsx b/autogpt_platform/frontend/src/providers/posthog/posthog-provider.tsxindex cc6f9870da..5a8e5706ee 100644--- a/autogpt_platform/frontend/src/providers/posthog/posthog-provider.tsx+++ b/autogpt_platform/frontend/src/providers/posthog/posthog-provider.tsx@@ -6,6 +6,7 @@ import {followAnalyticsConsentForIdentity,getAnonymousID,} from "@/services/analytics/anonymous-id";+import { getPostHogBaseProperties } from "@/services/analytics/posthog-base-properties";import { PageEvent } from "@/services/analytics/posthog-events";import { useConsent } from "@/services/consent/useConsent";import { environment } from "@/services/environment";@@ -47,6 +48,7 @@ export function PostHogProvider({ children }: { children: ReactNode }) {}),...getConsentGatedConfig(),});+ posthog.register(getPostHogBaseProperties());unfollowConsent = followAnalyticsConsent();}return () => {diff --git a/autogpt_platform/frontend/src/services/analytics/__tests__/auth-identity.test.ts b/autogpt_platform/frontend/src/services/analytics/__tests__/auth-identity.test.tsindex 6a737b42a7..10a9eaee4f 100644--- a/autogpt_platform/frontend/src/services/analytics/__tests__/auth-identity.test.ts+++ b/autogpt_platform/frontend/src/services/analytics/__tests__/auth-identity.test.ts@@ -16,6 +16,7 @@ import {const posthog = vi.hoisted(() => ({get_distinct_id: vi.fn<() => string | undefined>(),reset: vi.fn(),+ register: vi.fn(),}));vi.mock("posthog-js", () => ({ default: posthog }));@@ -104,6 +105,9 @@ describe("analytics identity on account transitions", () => {expect(posthog.reset).toHaveBeenCalledOnce();expect(posthog.reset).toHaveBeenCalledWith(true);+ expect(posthog.register).toHaveBeenCalledWith(+ expect.objectContaining({ source: "web" }),+ );expect(observed).toEqual(["new-visitor"]);expect(getAnonymousID()).toBe(posthog.get_distinct_id());},diff --git a/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-base-properties.test.ts b/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-base-properties.test.tsnew file mode 100644index 0000000000..25447c7440--- /dev/null+++ b/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-base-properties.test.ts@@ -0,0 +1,25 @@+import { afterEach, describe, expect, it, vi } from "vitest";+import { getPostHogBaseProperties } from "../posthog-base-properties";++afterEach(() => {+ vi.unstubAllEnvs();+});++describe("getPostHogBaseProperties", () => {+ it("names the browser as the emitter and carries the app environment", () => {+ vi.stubEnv("NEXT_PUBLIC_APP_ENV", "prod");+ vi.stubEnv("NEXT_PUBLIC_VERCEL_ENV", "production");++ expect(getPostHogBaseProperties()).toEqual({+ source: "web",+ environment: "prod",+ });+ });++ it("tells a Vercel preview apart from production", () => {+ vi.stubEnv("NEXT_PUBLIC_APP_ENV", "prod");+ vi.stubEnv("NEXT_PUBLIC_VERCEL_ENV", "preview");++ expect(getPostHogBaseProperties().environment).toBe("preview");+ });+});diff --git a/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-events.test.ts b/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-events.test.tsindex ccbf177a63..585b11721a 100644--- a/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-events.test.ts+++ b/autogpt_platform/frontend/src/services/analytics/__tests__/posthog-events.test.ts@@ -28,42 +28,33 @@ const LIVE_EVENT_NAMES = ["credential_oauth_popup_blocked","credential_proceed_stuck_after_connect","credential_scope_shortfall_blocked_selection",- "experiment_exposed","expert_profile_opened","expert_recommendation_clicked","expert_recommended","expert_thread_created","experts_section_viewed",- "feature_flag_mismatch",- "finalize_latency_ms",+ "feature_flag_mismatched","hire_flow_abandoned",- "hire_flow_completed","hire_started","hire_step_continued","home_attention_actioned","home_team_member_clicked","home_viewed",- "intro_card_dismissed","intro_followup_sent","intro_path",- "intro_start_with_autopilot","later_dump_completed",- "onboarding_expert_hired",- "raise_door_clicked","subscription_trial_checkout_started",- "subscription_trial_offer_viewed","tab_intro_cta_clicked","tab_intro_dismissed","tab_intro_shown","transcription_failed",- "voice_first_sound_latency_ms",+ "trial_offer_viewed","voice_mode_error","voice_mode_permission_denied","voice_mode_started","voice_mode_stopped","voice_mode_timed_out","voice_recording_downloaded",- "voice_transcribe_latency_ms","voice_transcribe_retried","voice_turn_completed","voice_turn_dropped",@@ -83,6 +74,24 @@ const PLANNED_EVENT_NAMES = ["tour_started",];+// No longer sent, or renamed (SECRT-2722). The names stay reserved: reusing one would+// splice a different action onto the history PostHog already holds for it.+const RETIRED_EVENT_NAMES = [+ "experiment_exposed",+ // Renamed to feature_flag_mismatched before it reached production.+ "feature_flag_mismatch",+ "finalize_latency_ms",+ "hire_flow_completed",+ "intro_card_dismissed",+ "intro_start_with_autopilot",+ "onboarding_expert_hired",+ "raise_door_clicked",+ // Renamed to the analytics plan's trial_offer_viewed.+ "subscription_trial_offer_viewed",+ "voice_first_sound_latency_ms",+ "voice_transcribe_latency_ms",+];+describe("PostHog event names", () => {it("keeps every live name exactly as PostHog already stores it", () => {expect(Object.values(PostHogEvent).sort()).toEqual(LIVE_EVENT_NAMES);@@ -94,6 +103,14 @@ describe("PostHog event names", () => {);});+ it("never reuses a retired name", () => {+ const names = new Set<string>([+ ...Object.values(PostHogEvent),+ ...Object.values(PlannedPostHogEvent),+ ]);+ expect(RETIRED_EVENT_NAMES.filter((name) => names.has(name))).toEqual([]);+ });+it("never lists a planned name as live", () => {const live = new Set<string>(Object.values(PostHogEvent));expect(PLANNED_EVENT_NAMES.filter((name) => live.has(name))).toEqual([]);diff --git a/autogpt_platform/frontend/src/services/analytics/activation-goals.ts b/autogpt_platform/frontend/src/services/analytics/activation-goals.tsindex c2058879cb..675dafa370 100644--- a/autogpt_platform/frontend/src/services/analytics/activation-goals.ts+++ b/autogpt_platform/frontend/src/services/analytics/activation-goals.ts@@ -6,8 +6,9 @@ import { analytics } from "@/services/analytics";* These exist for marketing attribution (which channel brings users who* actually run things), so they fire only on human clicks, never for* server-triggered work. The server-side record of every run lives in- * PostHog (`run_agent`, `run_autopilot`, ...) and the `analytics.*` views;- * this file is only the DataFast mirror of the two goals it can see.+ * PostHog (`agent_run_started`, `chat_message_sent`, ...) and the+ * `analytics.*` views; this file is only the DataFast mirror of the two goals+ * it can see.** `run_agent` used to fire from a single library modal, so builder runs,* re-runs and the builder's run dialog never counted. Every human run pathdiff --git a/autogpt_platform/frontend/src/services/analytics/posthog-base-properties.ts b/autogpt_platform/frontend/src/services/analytics/posthog-base-properties.tsnew file mode 100644index 0000000000..750f729fb6--- /dev/null+++ b/autogpt_platform/frontend/src/services/analytics/posthog-base-properties.ts@@ -0,0 +1,16 @@+import { environment } from "@/services/environment";++// The base properties every PostHog event carries (see+// docs/platform/tracking-plan.md), registered once as super properties so+// no call site has to repeat them. `source` names the emitter; the backend+// sends `platform` or `chat_copilot`. Vercel previews share the production+// NEXT_PUBLIC_APP_ENV, so they are told apart here, as in+// `environment.getEnvironmentStr`.+export function getPostHogBaseProperties() {+ return {+ source: "web",+ environment: environment.isVercelPreview()+ ? "preview"+ : environment.getAppEnv(),+ };+}diff --git a/autogpt_platform/frontend/src/services/analytics/posthog-events.ts b/autogpt_platform/frontend/src/services/analytics/posthog-events.tsindex 2406212278..205e6a5a62 100644--- a/autogpt_platform/frontend/src/services/analytics/posthog-events.ts+++ b/autogpt_platform/frontend/src/services/analytics/posthog-events.ts@@ -3,9 +3,11 @@// docs/platform/tracking-plan.md; the backend's list is// backend/util/posthog_events.py.//-// Never change a value here. PostHog stores the raw string, so a rename-// orphans every insight and funnel built on the old name and cannot be-// backfilled. __tests__/posthog-events.test.ts pins the values.+// The names follow the product analytics plan ("Every Second Counts"). Never+// change a value here. PostHog stores the raw string, so a rename orphans+// every insight and funnel built on the old name and cannot be backfilled.+// __tests__/posthog-events.test.ts pins the values and reserves the names+// retired or renamed in SECRT-2722.//// Plain `as const` objects rather than enums so a string literal at a call// site still type-checks against the list. Call sites do pass literals, so@@ -15,14 +17,10 @@ export const PageEvent = {PAGEVIEW: "$pageview",} as const;-export const ExperimentEvent = {- EXPERIMENT_EXPOSED: "experiment_exposed",-} as const;-// Ops signal from the dual flag backend, not a user action: LaunchDarkly and// PostHog resolved the same flag to different values.export const FeatureFlagEvent = {- FEATURE_FLAG_MISMATCH: "feature_flag_mismatch",+ FEATURE_FLAG_MISMATCHED: "feature_flag_mismatched",} as const;export const ExpertsFunnelEvent = {@@ -38,12 +36,14 @@ export const ExpertsFunnelEvent = {} as const;export const HireFlowEvent = {- HIRE_FLOW_COMPLETED: "hire_flow_completed",HIRE_FLOW_ABANDONED: "hire_flow_abandoned",} as const;export const BrainDumpEvent = {BRAIN_DUMP_STARTED: "brain_dump_started",+ // `finalize_latency_ms` on this and on `transcription_failed` is the+ // wall-clock of the `finalizeBrainDump()` round trip: virus scan, storage,+ // transcription and extraction. The upload flush finishes before it starts.BRAIN_DUMP_COMPLETED: "brain_dump_completed",BRAIN_DUMP_CANCELED: "brain_dump_canceled",BRAIN_DUMP_SKIPPED: "brain_dump_skipped",@@ -54,11 +54,6 @@ export const BrainDumpEvent = {BRAIN_DUMP_DOWNLOAD: "brain_dump_download",BRAIN_DUMP_PERMISSION_DENIED: "brain_dump_permission_denied",BRAIN_DUMP_TYPED_FALLBACK: "brain_dump_typed_fallback",- // Wall-clock of the whole finalize round trip — upload flush, virus- // scan, storage, transcription and extraction. Named for what it- // actually measures: the client cannot see the transcription step on- // its own, so calling this "transcription latency" overstated it.- FINALIZE_LATENCY_MS: "finalize_latency_ms",// The welcome dialog shown on first copilot landing was closed — the// greeting fetch and reveal animation start from this moment.WELCOME_DIALOG_CLOSED: "welcome_dialog_closed",@@ -80,16 +75,10 @@ export const BrainDumpEvent = {// through, or skip straight to the builder.EXPERT_RECOMMENDED: "expert_recommended",EXPERT_RECOMMENDATION_CLICKED: "expert_recommendation_clicked",- HIRE_STARTED: ExpertsFunnelEvent.HIRE_STARTED,- // The wizard's hire step: a hire that landed from a card, and the step- // being left — with how many of the proposed experts were hired, so the- // funnel can tell "hired a team" from "skipped past it".- ONBOARDING_EXPERT_HIRED: "onboarding_expert_hired",+ // The wizard's hire step was left, with how many of the proposed experts+ // were hired, so the funnel can tell "hired a team" from "skipped past it".+ // The hires themselves are the backend's `expert_hired`.HIRE_STEP_CONTINUED: "hire_step_continued",- // Declared but never sent; the tracking plan marks them for removal.- INTRO_CARD_DISMISSED: "intro_card_dismissed",- RAISE_DOOR_CLICKED: "raise_door_clicked",- INTRO_START_WITH_AUTOPILOT: "intro_start_with_autopilot",} as const;export const TabIntroEvent = {@@ -112,18 +101,14 @@ export const VoiceModeEvent = {VOICE_MODE_TIMED_OUT: "voice_mode_timed_out",// A completed turn: heard, transcribed, sent. `turn_index` counts within// the session, so a histogram shows whether anyone gets past one.+ // `transcribe_latency_ms` is speech end to transcript in hand: the number+ // that decides whether streaming STT is worth building.VOICE_TURN_SENT: "voice_turn_sent",// Heard something and threw it away — VAD misfire, or filler/hallucination// like Whisper's "Thank you." on silence. Splits by `reason`.VOICE_TURN_DROPPED: "voice_turn_dropped",- // Speech end to transcript in hand. The spike measured 1.05 s on a- // streaming session; this route uploads the whole clip, so it is the- // number that decides whether streaming STT is worth building.- VOICE_TRANSCRIBE_LATENCY_MS: "voice_transcribe_latency_ms",- // Speech end to the first spoken word. The click answers "did it hear- // me" instantly; this measures how long the answer itself takes.- VOICE_FIRST_SOUND_LATENCY_MS: "voice_first_sound_latency_ms",// The mic reopened after a reply finished playing: a full loop closed.+ // `first_sound_latency_ms` is speech end to the first spoken word.VOICE_TURN_COMPLETED: "voice_turn_completed",// The user asked for a failed transcription to be tried again. Against// `voice_turn_dropped{reason:transcribe_failed}` this says how many of those@@ -154,13 +139,12 @@ export const CredentialConnectionFailureEvent = {} as const;export const TrialEvent = {- SUBSCRIPTION_TRIAL_OFFER_VIEWED: "subscription_trial_offer_viewed",+ TRIAL_OFFER_VIEWED: "trial_offer_viewed",SUBSCRIPTION_TRIAL_CHECKOUT_STARTED: "subscription_trial_checkout_started",} as const;export const PostHogEvent = {...PageEvent,- ...ExperimentEvent,...FeatureFlagEvent,...ExpertsFunnelEvent,...HireFlowEvent,diff --git a/autogpt_platform/frontend/src/services/analytics/reset-identity.ts b/autogpt_platform/frontend/src/services/analytics/reset-identity.tsindex 6cd1f1edc5..9a4258310b 100644--- a/autogpt_platform/frontend/src/services/analytics/reset-identity.ts+++ b/autogpt_platform/frontend/src/services/analytics/reset-identity.ts@@ -1,6 +1,7 @@import { syncPostHogConsent } from "@/providers/posthog/posthog-consent";import posthog from "posthog-js";import { resetAnonymousID } from "./anonymous-id";+import { getPostHogBaseProperties } from "./posthog-base-properties";export function resetAnalyticsIdentity(): void {let anonymousID: string | undefined;@@ -14,6 +15,7 @@ export function resetAnalyticsIdentity(): void {} catch {anonymousID = undefined;}+ if (didReset) restoreBaseProperties();resetAnonymousID(anonymousID);// reset() also drops PostHog's own opt-in, which opts a consenting visitor// out until the next page load unless it is restored here.@@ -25,3 +27,12 @@ export function resetAnalyticsIdentity(): void {}}}++// reset() wipes the super properties along with the identity.+function restoreBaseProperties() {+ try {+ posthog.register(getPostHogBaseProperties());+ } catch {+ // Analytics is never worth a broken logout.+ }+}diff --git a/autogpt_platform/frontend/src/services/experiments/__tests__/useLaunchDarklyExperiment.test.tsx b/autogpt_platform/frontend/src/services/experiments/__tests__/useLaunchDarklyExperiment.test.tsxdeleted file mode 100644index 3cc3ec1b1b..0000000000--- a/autogpt_platform/frontend/src/services/experiments/__tests__/useLaunchDarklyExperiment.test.tsx+++ /dev/null@@ -1,206 +0,0 @@-import { server } from "@/mocks/mock-server";-import { environment } from "@/services/environment";-import { QueryClient, QueryClientProvider } from "@tanstack/react-query";-import { renderHook, waitFor } from "@testing-library/react";-import { http, HttpResponse } from "msw";-import { ReactNode } from "react";-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";-import {- resetExposuresForTests,- useLaunchDarklyExperiment,-} from "../useLaunchDarklyExperiment";-import { resetReportedAssignmentsForTests } from "../useReportAssignment";--const ld = vi.hoisted(() => ({- flags: {} as Record<string, unknown>,- client: null as null | { waitForInitialization: () => Promise<void> },-}));--const auth = vi.hoisted(() => ({- user: null as { id: string } | null,-}));--const capture = vi.hoisted(() => vi.fn());--vi.mock("launchdarkly-react-client-sdk", () => ({- useFlags: () => ld.flags,- useLDClient: () => ld.client ?? undefined,-}));--vi.mock("posthog-js", () => ({ default: { capture } }));--vi.mock("@/lib/auth/hooks/useAuth", () => ({- useAuth: () => ({ user: auth.user, isUserLoading: false }),-}));--interface Props {- children: ReactNode;-}--function wrapper({ children }: Props) {- const client = new QueryClient({- defaultOptions: { queries: { retry: false }, mutations: { retry: false } },- });- return <QueryClientProvider client={client}>{children}</QueryClientProvider>;-}--function captureAssignments() {- const bodies: unknown[] = [];- server.use(- http.post("*/api/experiments/assignments", async ({ request }) => {- bodies.push(await request.json());- return HttpResponse.json({- experiment_key: "onboarding-copy",- variant: "b",- source: "launchdarkly",- assigned_at: "2026-09-02T00:00:00Z",- });- }),- );- return bodies;-}--beforeEach(() => {- ld.flags = {};- ld.client = { waitForInitialization: () => Promise.resolve() };- auth.user = { id: "user-1" };- capture.mockReset();- resetExposuresForTests();- resetReportedAssignmentsForTests();- vi.spyOn(environment, "areFeatureFlagsEnabled").mockReturnValue(true);- vi.spyOn(environment, "isPostHogEnabled").mockReturnValue("phc_test");-});--afterEach(() => {- vi.restoreAllMocks();-});--describe("useLaunchDarklyExperiment", () => {- it("resolves after the LaunchDarkly client initialises", async () => {- ld.flags = { "onboarding-copy": "b" };-- const { result } = renderHook(- () => useLaunchDarklyExperiment("onboarding-copy"),- { wrapper },- );-- expect(result.current.isResolved).toBe(false);- await waitFor(() => expect(result.current.isResolved).toBe(true));- expect(result.current.variant).toBe("b");- });-- it("reports the arm to the backend and to PostHog once", async () => {- const bodies = captureAssignments();- ld.flags = { "onboarding-copy": "b" };-- renderHook(() => useLaunchDarklyExperiment("onboarding-copy"), {- wrapper,- });-- await waitFor(() => expect(bodies).toHaveLength(1));- expect(bodies[0]).toEqual({- experiment_key: "onboarding-copy",- variant: "b",- source: "launchdarkly",- });- expect(capture).toHaveBeenCalledTimes(1);- expect(capture).toHaveBeenCalledWith("experiment_exposed", {- experiment_key: "onboarding-copy",- variant: "b",- provider: "launchdarkly",- "$feature/onboarding-copy": "b",- });-- renderHook(() => useLaunchDarklyExperiment("onboarding-copy"), {- wrapper,- });- await new Promise((resolve) => setTimeout(resolve, 20));- expect(bodies).toHaveLength(1);- expect(capture).toHaveBeenCalledTimes(1);- });-- it("reports separate exposures when the signed-in user changes", async () => {- const bodies = captureAssignments();- ld.flags = { "onboarding-copy": "b" };- const { rerender } = renderHook(- () => useLaunchDarklyExperiment("onboarding-copy"),- { wrapper },- );- await waitFor(() => expect(capture).toHaveBeenCalledTimes(1));-- auth.user = { id: "user-2" };- rerender();- await waitFor(() => expect(bodies).toHaveLength(2));- expect(capture).toHaveBeenCalledTimes(2);-- auth.user = { id: "user-1" };- rerender();- expect(capture).toHaveBeenCalledTimes(2);- });-- it("waits for a signed-in user before reporting an exposure", async () => {- captureAssignments();- auth.user = null;- ld.flags = { "onboarding-copy": "b" };- const { result, rerender } = renderHook(- () => useLaunchDarklyExperiment("onboarding-copy"),- { wrapper },- );- await waitFor(() => expect(result.current.isResolved).toBe(true));- expect(capture).not.toHaveBeenCalled();-- auth.user = { id: "user-1" };- rerender();- expect(capture).toHaveBeenCalledTimes(1);- });-- it("does not consume an exposure while PostHog is disabled", async () => {- captureAssignments();- const enabled = vi.spyOn(environment, "isPostHogEnabled");- enabled.mockReturnValue(false);- ld.flags = { "onboarding-copy": "b" };- const { result, rerender } = renderHook(- () => useLaunchDarklyExperiment("onboarding-copy"),- { wrapper },- );- await waitFor(() => expect(result.current.isResolved).toBe(true));- expect(capture).not.toHaveBeenCalled();-- enabled.mockReturnValue("phc_test");- rerender();- expect(capture).toHaveBeenCalledTimes(1);- });-- it("treats boolean or missing flags as not enrolled", async () => {- const bodies = captureAssignments();- ld.flags = { "onboarding-copy": true };-- const { result } = renderHook(- () => useLaunchDarklyExperiment("onboarding-copy"),- { wrapper },- );-- await waitFor(() => expect(result.current.isResolved).toBe(true));- expect(result.current.variant).toBeNull();- await new Promise((resolve) => setTimeout(resolve, 20));- expect(bodies).toHaveLength(0);- expect(capture).not.toHaveBeenCalled();- });-- it("ignores retained string variants when feature flags are disabled", async () => {- const bodies = captureAssignments();- vi.spyOn(environment, "areFeatureFlagsEnabled").mockReturnValue(false);- ld.flags = { "onboarding-copy": "b" };- ld.client = null;-- const { result } = renderHook(- () => useLaunchDarklyExperiment("onboarding-copy"),- { wrapper },- );-- expect.soft(result.current).toEqual({ variant: null, isResolved: true });- await new Promise((resolve) => setTimeout(resolve, 20));- expect.soft(bodies).toHaveLength(0);- expect(capture).not.toHaveBeenCalled();- });-});diff --git a/autogpt_platform/frontend/src/services/experiments/useExperiment.ts b/autogpt_platform/frontend/src/services/experiments/useExperiment.tsindex 43cdc0c9bb..f5663d5099 100644--- a/autogpt_platform/frontend/src/services/experiments/useExperiment.ts+++ b/autogpt_platform/frontend/src/services/experiments/useExperiment.ts@@ -19,9 +19,6 @@ export { resetReportedAssignmentsForTests } from "./useReportAssignment";* loading flags: render the control experience and hold any one-shot* side effects until it flips, so a late variant is never mis-recorded as* control. When PostHog is disabled the experiment resolves immediately.- *- * For an experiment bucketed by a LaunchDarkly flag instead, use- * `useLaunchDarklyExperiment`; both report into the same table.*/export function useExperiment(experimentKey: string) {const rawVariant = useFeatureFlagVariantKey(experimentKey);diff --git a/autogpt_platform/frontend/src/services/experiments/useLaunchDarklyExperiment.ts b/autogpt_platform/frontend/src/services/experiments/useLaunchDarklyExperiment.tsdeleted file mode 100644index 79f6faa7b5..0000000000--- a/autogpt_platform/frontend/src/services/experiments/useLaunchDarklyExperiment.ts+++ /dev/null@@ -1,92 +0,0 @@-"use client";--import { useAuth } from "@/lib/auth/hooks/useAuth";-import { ExperimentEvent } from "@/services/analytics/posthog-events";-import { environment } from "@/services/environment";-import { useFlags, useLDClient } from "launchdarkly-react-client-sdk";-import posthog from "posthog-js";-import { useEffect, useState } from "react";-import { useReportAssignment } from "./useReportAssignment";--// `waitForInitialization` takes seconds, not milliseconds.-const LD_READY_TIMEOUT_SECONDS = 5;--/**- * Run an experiment whose arms are a LaunchDarkly string flag.- *- * The team gates with LaunchDarkly, but LaunchDarkly only knows a user after- * login and PostHog is where funnels and significance live. This hook is- * the bridge: LaunchDarkly picks the arm, and the hook (1) reports the- * assignment to the backend like `useExperiment` does, so Looker can split- * by arm, and (2) sends PostHog an `experiment_exposed` event carrying the- * arm as `$feature/<flag>`, so a PostHog experiment can use it as its- * exposure and measure the activation events against it.- *- * `variant` is the flag's string value, or `null` when the flag is off,- * boolean, or not yet known. `isResolved` flips once the LaunchDarkly client- * has initialised (or timed out) so a late arm is never recorded as control.- */-export function useLaunchDarklyExperiment(flagKey: string) {- const { user } = useAuth();- const userID = user?.id;- const postHogEnabled = Boolean(environment.isPostHogEnabled());- const flags = useFlags<Record<string, unknown>>();- const client = useLDClient();- const [isClientReady, setIsClientReady] = useState(false);- const flagsEnabled = environment.areFeatureFlagsEnabled();-- useEffect(() => {- if (!client) return;- let active = true;- client- .waitForInitialization(LD_READY_TIMEOUT_SECONDS)- .catch(() => undefined)- .finally(() => {- if (active) setIsClientReady(true);- });- return () => {- active = false;- };- }, [client]);-- const isResolved = !flagsEnabled || isClientReady;- const raw = flags[flagKey];- const variant = flagsEnabled && typeof raw === "string" ? raw : null;-- useReportAssignment({- experimentKey: flagKey,- variant,- isResolved,- source: "launchdarkly",- });-- useEffect(() => {- if (!isResolved || !variant || !userID || !postHogEnabled) return;- if (!claimExposure(userID, flagKey, variant)) return;- try {- posthog.capture(ExperimentEvent.EXPERIMENT_EXPOSED, {- experiment_key: flagKey,- variant,- provider: "launchdarkly",- [`$feature/${flagKey}`]: variant,- });- } catch {- // Analytics must never break the experience it measures.- }- }, [isResolved, variant, flagKey, userID, postHogEnabled]);-- return { variant, isResolved };-}--const exposures = new Set<string>();--function claimExposure(userID: string, flagKey: string, variant: string) {- const key = JSON.stringify([userID, flagKey, variant]);- if (exposures.has(key)) return false;- exposures.add(key);- return true;-}--export function resetExposuresForTests() {- exposures.clear();-}diff --git a/autogpt_platform/frontend/src/services/experts/__tests__/hire-timing.test.ts b/autogpt_platform/frontend/src/services/experts/__tests__/hire-timing.test.tsdeleted file mode 100644index b8948e5327..0000000000--- a/autogpt_platform/frontend/src/services/experts/__tests__/hire-timing.test.ts+++ /dev/null@@ -1,53 +0,0 @@-import { afterEach, describe, expect, it, vi } from "vitest";-import { markHireStarted, takeHireElapsedMs } from "../hire-timing";--afterEach(() => {- vi.restoreAllMocks();- window.sessionStorage.clear();-});--describe("hire timing", () => {- it("reports the span between the hire click and the finish", () => {- vi.spyOn(Date, "now").mockReturnValue(1_000);- markHireStarted("template-maria");-- vi.spyOn(Date, "now").mockReturnValue(4_500);- expect(takeHireElapsedMs("template-maria")).toBe(3_500);- });-- it("stores the mark per template", () => {- markHireStarted("template-maria");-- expect(takeHireElapsedMs("template-max")).toBeNull();- expect(takeHireElapsedMs("template-maria")).not.toBeNull();- });-- it("clears the mark so a second finish cannot report a stale span", () => {- markHireStarted("template-maria");-- expect(takeHireElapsedMs("template-maria")).not.toBeNull();- expect(takeHireElapsedMs("template-maria")).toBeNull();- });-- it("returns null when the flow was never started", () => {- expect(takeHireElapsedMs("template-never")).toBeNull();- });-- it("ignores a corrupted mark", () => {- window.sessionStorage.setItem("autogpt:hire-started:template-maria", "abc");-- expect(takeHireElapsedMs("template-maria")).toBeNull();- });-- it("survives a storage that refuses reads and writes", () => {- vi.spyOn(window.sessionStorage, "setItem").mockImplementation(() => {- throw new Error("blocked");- });- vi.spyOn(window.sessionStorage, "getItem").mockImplementation(() => {- throw new Error("blocked");- });-- expect(() => markHireStarted("template-maria")).not.toThrow();- expect(takeHireElapsedMs("template-maria")).toBeNull();- });-});diff --git a/autogpt_platform/frontend/src/services/experts/__tests__/useHireFlow.test.tsx b/autogpt_platform/frontend/src/services/experts/__tests__/useHireFlow.test.tsxindex 30daa917a1..fc8e2fae4c 100644--- a/autogpt_platform/frontend/src/services/experts/__tests__/useHireFlow.test.tsx+++ b/autogpt_platform/frontend/src/services/experts/__tests__/useHireFlow.test.tsx@@ -5,7 +5,6 @@ import {import type { Expert } from "@/app/api/__generated__/models/expert";import { Toaster } from "@/components/molecules/Toast/toaster";import { server } from "@/mocks/mock-server";-import { markHireStarted } from "@/services/experts/hire-timing";import { useHireFlow } from "@/services/experts/useHireFlow";import { QueryClient, QueryClientProvider } from "@tanstack/react-query";import { act, renderHook, screen, waitFor } from "@testing-library/react";@@ -102,7 +101,6 @@ describe("useHireFlow", () => {beforeEach(() => {captureMock.mockReset();pushMock.mockReset();- window.sessionStorage.clear();server.use(getHireExpertMockHandler({ expert: hiredMaria }),getUpdateExpertSoulMockHandler(hiredMaria),@@ -110,9 +108,6 @@ describe("useHireFlow", () => {});test("walks hire → voice pick → saved voice and hands off to the thread", async () => {- vi.useFakeTimers({ toFake: ["Date"] });- markHireStarted("template-maria");- vi.advanceTimersByTime(1500);const { result } = renderHireFlow();await act(async () => {@@ -129,17 +124,31 @@ describe("useHireFlow", () => {expect(pushMock).toHaveBeenCalledWith("/copilot?expertId=expert-maria&kickoff=1",);- const completed = capturedEvent("hire_flow_completed")?.[1] as {- voice_picked: boolean;- elapsed_ms: number | null;- };- expect(completed.voice_picked).toBe(true);- expect(completed.elapsed_ms).toBeGreaterThanOrEqual(1500);- // The mark is consumed, so a second finish cannot report a stale span.- expect(- window.sessionStorage.getItem("autogpt:hire-started:template-maria"),- ).toBeNull();- vi.useRealTimers();+ });++ test("tells the backend where the hire came from and sends no hire event itself", async () => {+ let body: unknown;+ server.use(+ http.post("/api/proxy/api/experts", async ({ request }) => {+ body = await request.json();+ return HttpResponse.json({ expert: hiredMaria, failed_preloads: [] });+ }),+ );+ const { result } = renderHireFlow();++ await act(async () => {+ await result.current.hire();+ });+ await act(async () => {+ result.current.skipVoice();+ });++ // The backend's `expert_hired` is the one record of the hire.+ expect(body).toEqual({+ template_id: "template-maria",+ surface: "expert_page",+ });+ expect(captureMock).not.toHaveBeenCalled();});test("skipping the voice pick still celebrates the hire", async () => {@@ -156,10 +165,6 @@ describe("useHireFlow", () => {expect(pushMock).toHaveBeenCalledWith("/copilot?expertId=expert-maria&kickoff=1",);- expect(capturedEvent("hire_flow_completed")?.[1]).toMatchObject({- voice_picked: false,- elapsed_ms: null,- });});test("finishes straight away when the persona ships no writing samples", async () => {diff --git a/autogpt_platform/frontend/src/services/experts/experts-analytics.test.ts b/autogpt_platform/frontend/src/services/experts/experts-analytics.test.tsindex 1764d9dac4..95beb70511 100644--- a/autogpt_platform/frontend/src/services/experts/experts-analytics.test.ts+++ b/autogpt_platform/frontend/src/services/experts/experts-analytics.test.ts@@ -33,12 +33,15 @@ describe("trackFunnel", () => {});it("leaves a breadcrumb so the step shows on an error's timeline", () => {- trackFunnel("hire_started", { template_id: "template-maria" });+ trackFunnel("hire_started", {+ template_id: "template-maria",+ surface: "expert_page",+ });expect(addBreadcrumb).toHaveBeenCalledExactlyOnceWith({category: "funnel",message: "hire_started",- data: { template_id: "template-maria" },+ data: { template_id: "template-maria", surface: "expert_page" },level: "info",});});diff --git a/autogpt_platform/frontend/src/services/experts/experts-analytics.ts b/autogpt_platform/frontend/src/services/experts/experts-analytics.tsindex 021339c024..a35057f788 100644--- a/autogpt_platform/frontend/src/services/experts/experts-analytics.ts+++ b/autogpt_platform/frontend/src/services/experts/experts-analytics.ts@@ -8,6 +8,7 @@ import {HireFlowEvent,type EventName,} from "@/services/analytics/posthog-events";+import type { HireRequestSurface } from "@/app/api/__generated__/models/hireRequestSurface";import * as Sentry from "@sentry/nextjs";import posthog from "posthog-js";@@ -18,7 +19,10 @@ export type FunnelViewEvent =interface FunnelEventProperties {[ExpertsFunnelEvent.EXPERT_PROFILE_OPENED]: { template_id: string };- [ExpertsFunnelEvent.HIRE_STARTED]: { template_id: string };+ [ExpertsFunnelEvent.HIRE_STARTED]: {+ template_id: string;+ surface: NonNullable<HireRequestSurface>;+ };[ExpertsFunnelEvent.EXPERT_THREAD_CREATED]: { expert_id: string };[ExpertsFunnelEvent.BRIEFING_OUTCOME_CLICKED]: { status: string };[ExpertsFunnelEvent.HOME_ATTENTION_ACTIONED]: {diff --git a/autogpt_platform/frontend/src/services/experts/hire-timing.ts b/autogpt_platform/frontend/src/services/experts/hire-timing.tsdeleted file mode 100644index 1b80c9e874..0000000000--- a/autogpt_platform/frontend/src/services/experts/hire-timing.ts+++ /dev/null@@ -1,41 +0,0 @@-// How long a hire actually took, measured from the click that opened the flow-// to the moment it finishes. sessionStorage rather than component state-// because the flow can start on the marketplace profile sheet and finish-// inside the copilot dialog, and the two never share a React tree.--const KEY_PREFIX = "autogpt:hire-started:";--function storage() {- if (typeof window === "undefined") return null;- try {- return window.sessionStorage;- } catch {- return null;- }-}--export function markHireStarted(templateId: string) {- try {- storage()?.setItem(`${KEY_PREFIX}${templateId}`, String(Date.now()));- } catch {- // Private-mode and in-app browsers can refuse writes; a missing timing- // is never worth breaking a hire over.- }-}--/** Reads the mark and clears it, so a second finish cannot report a stale span. */-export function takeHireElapsedMs(templateId: string): number | null {- const store = storage();- if (!store) return null;- const key = `${KEY_PREFIX}${templateId}`;- try {- const raw = store.getItem(key);- store.removeItem(key);- if (!raw) return null;- const startedAt = Number(raw);- if (!Number.isFinite(startedAt)) return null;- return Math.max(0, Date.now() - startedAt);- } catch {- return null;- }-}diff --git a/autogpt_platform/frontend/src/services/experts/useHireFlow.tsx b/autogpt_platform/frontend/src/services/experts/useHireFlow.tsxindex 86b879c9d9..c3353d232d 100644--- a/autogpt_platform/frontend/src/services/experts/useHireFlow.tsx+++ b/autogpt_platform/frontend/src/services/experts/useHireFlow.tsx@@ -13,7 +13,6 @@ import {} from "@/components/organisms/VoicePicker/helpers";import { analytics } from "@/services/analytics";import { trackExperts } from "@/services/experts/experts-analytics";-import { takeHireElapsedMs } from "@/services/experts/hire-timing";import { invalidateExpertRosterQueries } from "@/services/experts/invalidate-experts";import { useQueryClient } from "@tanstack/react-query";import { useRouter } from "next/navigation";@@ -50,7 +49,6 @@ export function useHireFlow(expert: Expert | null) {const [hireResult, setHireResult] = useState<HireResult | null>(null);const [isVoicePickOpen, setIsVoicePickOpen] = useState(false);const pendingCelebrationRef = useRef<HireResult | null>(null);- const voicePickedRef = useRef(false);const { mutateAsync: hireExpert, isPending: isHiring } = useHireExpert();const { mutateAsync: updateSoul, isPending: isSavingVoice } =@@ -62,12 +60,6 @@ export function useHireFlow(expert: Expert | null) {setHireResult(null);setIsVoicePickOpen(false);if (completedHire) {- trackExperts("hire_flow_completed", {- template_id: expert?.id ?? null,- expert_id: completedHire.expert.id,- elapsed_ms: expert ? takeHireElapsedMs(expert.id) : null,- voice_picked: voicePickedRef.current,- });celebrate(completedHire);// Hiring isn't installing: hand the user straight to the expert's// thread with kickoff=1 so it introduces itself and starts its job.@@ -77,9 +69,10 @@ export function useHireFlow(expert: Expert | null) {async function hire() {if (!expert || !expert.is_template) return;- voicePickedRef.current = false;try {- const response = await hireExpert({ data: { template_id: expert.id } });+ const response = await hireExpert({+ data: { template_id: expert.id, surface: "expert_page" },+ });const result = response.data as HireResult;analytics.sendDatafastEvent("hire_completed", {template_id: expert.id,@@ -144,7 +137,6 @@ export function useHireFlow(expert: Expert | null) {});return;}- voicePickedRef.current = true;finish();}diff --git a/autogpt_platform/frontend/src/services/feature-flags/__tests__/flag-backends.test.tsx b/autogpt_platform/frontend/src/services/feature-flags/__tests__/flag-backends.test.tsxindex 976a3ba547..a04a987367 100644--- a/autogpt_platform/frontend/src/services/feature-flags/__tests__/flag-backends.test.tsx+++ b/autogpt_platform/frontend/src/services/feature-flags/__tests__/flag-backends.test.tsx@@ -228,7 +228,7 @@ describe("dual backend", () => {renderHook(() => useGetFlag(Flag.HIRE_EXPERTS));- expect(postHog.capture).toHaveBeenCalledWith("feature_flag_mismatch", {+ expect(postHog.capture).toHaveBeenCalledWith("feature_flag_mismatched", {flag: HIRE_EXPERTS,launchdarkly: { value: true, resolved: true },posthog: { value: false, resolved: true },@@ -281,7 +281,7 @@ describe("dual backend", () => {renderHook(() => useGetFlag(Flag.HIRE_EXPERTS));- expect(postHog.capture).toHaveBeenCalledWith("feature_flag_mismatch", {+ expect(postHog.capture).toHaveBeenCalledWith("feature_flag_mismatched", {flag: HIRE_EXPERTS,launchdarkly: { value: true, resolved: true },posthog: { value: null, resolved: true },diff --git a/autogpt_platform/frontend/src/services/feature-flags/backends/dual.ts b/autogpt_platform/frontend/src/services/feature-flags/backends/dual.tsindex dac1749169..af9ab41c76 100644--- a/autogpt_platform/frontend/src/services/feature-flags/backends/dual.ts+++ b/autogpt_platform/frontend/src/services/feature-flags/backends/dual.ts@@ -55,6 +55,6 @@ function useReportMismatch(if (!comparable || agree) return;const mismatch = JSON.parse(record);console.warn("feature-flag mismatch", mismatch);- posthog?.capture(FeatureFlagEvent.FEATURE_FLAG_MISMATCH, mismatch);+ posthog?.capture(FeatureFlagEvent.FEATURE_FLAG_MISMATCHED, mismatch);}, [comparable, agree, record, posthog]);}diff --git a/docs/platform/activation-metrics.md b/docs/platform/activation-metrics.mdindex 68c0568d90..02cb7d2a88 100644--- a/docs/platform/activation-metrics.md+++ b/docs/platform/activation-metrics.md@@ -21,7 +21,7 @@ exists, so a change to one must be made in all four.| **LaunchDarkly** | Feature gating (on/off, rollouts, targeting). Not for A/B measurement. | — || **DataFast** | Marketing attribution: which channel brought a visitor who signed up, ran an agent, paid. | Browser goals only (`signup`, `paywall_view`, `run_agent`, `schedule_agent`, ...). || **Admin dashboard** | Ops views: platform costs, copilot usage export, execution accuracy. | Backend admin routes. |-| **Sentry** | Errors and stack traces behind `agent_run_failed`. | — |+| **Sentry** | Errors and stack traces behind `agent_run_finished` with `status: failed`. | — |## Definitions@@ -65,15 +65,15 @@ Every event carries `environment` and `source: "platform"`.| GTM asked for | Event | Fires when | Key properties || --- | --- | --- | --- |-| run_agent | `run_agent` | A person starts an agent run (UI, API key, copilot tool). Not schedules, webhooks, sub-graphs or dry runs. | `trigger`, `trigger_ref`, `graph_id`, `graph_exec_id` |-| run_autopilot | `run_autopilot` | A person sends a message in an Autopilot chat. | `session_id`, `surface` (web chat, slack, telegram, discord) |-| run_expert | `run_expert` | A person sends a message in an expert chat (`kind: chat_turn`) or starts an expert workflow run (`kind: workflow_run`). | `expert_id`, `kind` |+| run_agent | `agent_run_started` without `expert_id` | A person starts an agent run (UI, API key, copilot tool). Not schedules, webhooks, sub-graphs or dry runs. | `trigger`, `trigger_ref`, `graph_id`, `graph_exec_id` |+| run_autopilot | `chat_message_sent` without `expert_id` | A person sends a message in an Autopilot chat. | `session_id`, `surface` (web chat, slack, telegram, discord) |+| run_expert | `chat_message_sent` / `agent_run_started` with `expert_id` | A person sends a message in an expert chat or starts an expert workflow run (`kind: workflow_run`). | `expert_id`, `kind` || schedule_agent_run / schedule_autopilot_run / schedule_expert_run | `schedule_created` | Any schedule is registered, from any surface. | `target`: `agent` / `autopilot` / `expert`, `cron`, `is_recurring`, `schedule_id` || schedule_agent_ran / schedule_autopilot_ran / schedule_expert_ran | `schedule_fired` | A schedule produced work. | `target`, `schedule_id`, `graph_exec_id` or `session_id` || (trigger) | `trigger_fired` | A webhook produced a run. | `webhook_id`, `graph_exec_id`, `target` |-| agent_fail | `agent_run_failed` | A run reaches FAILED. | `trigger`, `failure_reason`, `expert_id` |-| — | `agent_run_completed` | A run reaches COMPLETED. | `trigger`, `cost_cents`, `duration_seconds` |-| — | `expert_hired` | A user hires an expert from a template. | `expert_id`, `template_id` |+| agent_fail | `agent_run_finished` with `status: failed` | A run reaches FAILED. | `trigger`, `failure_reason`, `expert_id` |+| — | `agent_run_finished` with `status: completed` | A run reaches COMPLETED. | `trigger`, `cost_cents`, `duration_seconds` |+| — | `expert_hired` | A user hires an expert from a template, from any surface. | `expert_id`, `template_id`, `surface` || — | `integration_connected` | A user connects a credential, by OAuth or by pasting a key. | `provider`, `credential_type`, `method` || — | `credential_oauth_started` | The backend issues an OAuth login URL (`GET /api/integrations/{provider}/login`). Not Codex, which signs in with a device login. | `provider` || — | `credential_oauth_exchange_failed` | `POST /api/integrations/{provider}/callback` returns an error, on any path. Not Codex. | `provider`, `status_code` (unset for an unexpected error), `failure_class`, `detail` (error message or class, secrets removed, at most 200 characters) |@@ -99,10 +99,10 @@ status later (`ValueError` is a 400, for example).A granted scope set narrower than the one requested is not a callbackfailure: the credential is stored and `integration_connected` fires.-The pre-existing copilot events (`copilot_message_sent`, `copilot_tool_called`,-...) and billing events (`credit_topup_success`, `subscription_*`) are unchanged.-Every PostHog event, its sender, its properties and whether it is kept,-merged or planned is listed in the [PostHog Tracking Plan](tracking-plan.md).+The event names follow the product analytics plan; the chat events+(`chat_tool_called`, `chat_outcome`, ...) and billing events (`topup_completed`,+`trial_*`, `subscription_*`, ...) are listed there too. Every PostHog event, its sender, its properties and whether it is live,+planned or removed is listed in the [PostHog Tracking Plan](tracking-plan.md).## SQL views (Looker)@@ -113,7 +113,7 @@ untouched.| View | Grain | Answers || --- | --- | --- || `graph_execution` (extended) | one row per agent run | Now also `triggerSource`, `triggerRef`, `expertId`, `failureReason`, `isDryRun`, `isSubgraphRun`. |-| `chat_turn` | one row per human chat message | Autopilot vs expert usage; the SQL twin of `run_autopilot` / `run_expert`. |+| `chat_turn` | one row per human chat message | Autopilot vs expert usage; the SQL twin of `chat_message_sent`. || `user_task_daily` | user × day | How many tasks people run, by surface and by how they started; failures; our cost; credits charged; logins. || `user_lifecycle` | one row per user | First/last activity per surface, first-two-weeks behaviour, cost and revenue, and the labels `activated`, `stale_*`, `churned_30d`, `never_activated_30d`. The feature table for churn analysis. || `user_lifecycle_funnel_weekly` | signup-week cohort | Signup → onboarded → first task in 7d → activated in 14d → schedule / expert / purchase → retained at week 4. |@@ -174,14 +174,9 @@ device context yet), so bucket pre-login experiments on the client.side effects before then, or a late variant is mis-recorded as control.The hook reports the arm to `POST /api/experiments/assignments` once peruser and experiment; the backend keeps the **first** arm it sees.-3. Prefer a LaunchDarkly flag for the arms? Use `useLaunchDarklyExperiment(flagKey)`- with a string-valued flag. It reports the assignment the same way- (`source = launchdarkly`) and sends PostHog an `experiment_exposed` event- carrying `$feature/<flag>`, so a PostHog experiment can use it as its- exposure and measure the activation events against it.-4. Backend-decided experiments can call+3. Backend-decided experiments can call`backend.data.experiments.record_assignment(user_id, key, variant, source="backend")`.-5. Read results in PostHog (exposure + the activation events above as goals),+4. Read results in PostHog (exposure + the activation events above as goals),and in Looker by joining `analytics.experiment_assignment` to`user_lifecycle`, `user_task_daily` or `unit_economics_monthly`.@@ -208,8 +203,9 @@ is already wired this way.`CreditTransaction`; if staging shows that hurting, the follow-up is anightly-refreshed materialized view (a `generate_views.py` change), not aLooker source on the live view. Then wire the new views into Looker Studio.-4. Confirm in PostHog that `run_agent`, `run_autopilot`, `schedule_created`- and `agent_run_failed` are arriving with `source = platform`.+4. Confirm in PostHog that `agent_run_started`, `chat_message_sent`,+ `schedule_created` and `agent_run_finished` are arriving with+ `source = platform`.## The questions GTM will ask next@@ -275,11 +271,12 @@ missing.- *Can we A/B backend behaviour (model routing, default expert, prompt)?* —yes via `record_assignment(source="backend")`; LaunchDarkly stays thegate, PostHog and the assignment table hold the arm.-- *Can the team keep using LaunchDarkly for arms?* — yes, through- `useLaunchDarklyExperiment`; the arm still lands in PostHog and the- assignment table. **Gap**: the backend's LaunchDarkly context has no- `device` kind yet, so server-evaluated flags cannot bucket by the- anonymous id.+- *Can the team keep using LaunchDarkly for arms?* — not in the browser+ today: the unused `useLaunchDarklyExperiment` bridge and its+ `experiment_exposed` event were removed (SECRT-2722), so a LaunchDarkly arm+ reaches the assignment table only through `record_assignment`. **Gap**: the+ backend's LaunchDarkly context has no `device` kind yet, so server-evaluated+ flags cannot bucket by the anonymous id.**Experts and schedules**diff --git a/docs/platform/tracking-plan.md b/docs/platform/tracking-plan.mdindex f1ade7011e..a2aeafa007 100644--- a/docs/platform/tracking-plan.md+++ b/docs/platform/tracking-plan.md@@ -13,7 +13,7 @@ lists what is not aligned yet.In code the names live in two modules. Browser call sites may still pass aliteral (`trackBrainDump("brain_dump_started")`), which is type-checkedagainst these modules, and backend funnel `data_index` keys embed the name-too (`briefing_generated:<id>`, `expert_run_completed:<graph_exec_id>`). A+too (`briefing_generated:<id>`, `briefing_delivered:<id>`). Arename therefore has to search for the old string, not just edit the modules:| Sender | Module | Pin test |@@ -33,10 +33,9 @@ rename therefore has to search for the old string, not just edit the modules:lifecycle is `trial_*`.- **Rename once, then never again.** PostHog stores the raw string, so arename empties every insight, funnel and cohort built on the old name, and- past events cannot be backfilled. The live names that differ from the plan- (status `rename → X`) are renamed together in SECRT-2722, which lists- every old → new pair for the insights to update. After that, the pin tests- fail on any change to a live name.+ past events cannot be backfilled. The live names that differed from the+ plan were renamed together in SECRT-2722 (see [Renamed](#renamed)); the+ pin tests reserve the old names and fail on any change to a live one.- Put a new name in `PlannedPostHogEvent` when it is agreed, and move it intothe live list in the change that starts sending it.@@ -49,8 +48,8 @@ rename therefore has to search for the old string, not just edit the modules:- **Interactions are sent by the browser**: views, clicks, dismissals andclient-side timings that the backend never sees.- One action has **one** event. Where two emitters describe the same action,- the backend event survives and the other is merged into it (status- `merge`).+ the backend event survives and the other stops being sent (see+ [Removed](#removed)).### Base properties@@ -59,18 +58,17 @@ Every event must carry:| Property | Value || --- | --- || `environment` | `settings.config.app_env` on the backend (`local`, `dev`, `prod`), the matching app environment in the browser. |-| `source` | Which emitter sent it. Existing values stay: `platform` (`product_analytics.py`), `chat_copilot` (`copilot/tracking.py`). Newly covered backend emitters use `platform`; the browser uses `web`, registered once as a PostHog super property. |+| `source` | Which emitter sent it: `chat_copilot` (the events `copilot/tracking.py` captures itself; `chat_message_sent` goes through `product_analytics` and is `platform`), `platform` (every other backend emitter), `web` (the browser). |-Today only `product_analytics.py` and `copilot/tracking.py` send them. The-funnel (`funnel_analytics.py`), billing (`data/credit.py`) and trial-(`notifications/trial.py`) events and every browser event carry neither;-SECRT-2722 adds them.+Every backend event goes through `capture()` in+`backend/util/posthog_client.py`, which applies both last, so a caller cannot+overwrite them. The browser registers them once as PostHog super properties+(`src/services/analytics/posthog-base-properties.ts`), again after `reset()`+at logout; a Vercel preview reports `environment: preview`.-Three events already use `source` for something else and must move that-value to its own property before the base property is applied, or the two-will overwrite each other: `hire_started` (`source: onboarding_hire_step`),-`expert_recommended` (the team's `source`) and `workflow_installed_on_expert`-(`source: library | marketplace`).+`source` is reserved for the emitter. The three events that used it for+something else now use `hire_started.surface`,+`expert_recommended.team_source` and `workflow_installed_on_expert.workflow_source`.### Identity@@ -81,11 +79,9 @@ will overwrite each other: `hire_started` (`source: onboarding_hire_step`),(`src/services/analytics/anonymous-id.ts`), passed to `posthog.init` as thebootstrap `distinctID`. `identify` merges it into the user at login, and`resetAnalyticsIdentity` mints a fresh one at logout.-- **No synthetic ids.** A backend event with no user is dropped, as- `product_analytics.track` already does. `copilot/tracking.py` still falls- back to `anonymous_{session_id}` for `copilot_message_sent` and- `copilot_tool_called`; each of those creates a person nobody can merge, and- SECRT-2722 removes the fallback.+- **No synthetic ids.** A backend event with no user is dropped+ (`posthog_client.capture` does it for every emitter), since a made-up id+ creates a person nobody can merge.- **No email or name in event properties.** Identify ids only. The email anddisplay name are person properties set by `identify` and nowhere else.(`expert_hired.name` is the expert's display name, not a person's.)@@ -103,21 +99,20 @@ work a person asked for now:`automation` and whose kind is not `dream`.A run the copilot started (`triggerSource = copilot`) is **not** a second-task: the chat turn that asked for it already counts. `run_agent` does fire+task: the chat turn that asked for it already counts. `agent_run_started` does firefor copilot-started runs (with `trigger: copilot`), because it measureshuman-initiated runs, not tasks. `user_lifecycle.agent_runs_human_total` isits SQL twin and includes copilot runs too.-The same definition as a PostHog filter (with today's names; after-SECRT-2722 it reads `agent_run_started` and `chat_message_sent`):+The same definition as a PostHog filter:```sql-event IN ('run_agent', 'run_expert', 'run_autopilot')-AND coalesce(properties.trigger, '') != 'copilot'+event IN ('agent_run_started', 'chat_message_sent')+AND coalesce(properties.via, '') != 'copilot'```-In the insight UI: events `run_agent`, `run_expert` and `run_autopilot`,-filtered by `trigger` "is not" `copilot`. Chat turns carry no `trigger`, so+In the insight UI: events `agent_run_started` and `chat_message_sent`,+filtered by `via` "is not" `copilot`. Chat turns carry no `via`, sothe filter must keep events where it is unset. The emitters already leaveout dry runs, sub-graph runs, schedule and webhook runs, and automation-originturns.@@ -129,10 +124,10 @@ Automated work is measured separately: `schedule_fired` and `trigger_fired`.| Status | Meaning || --- | --- || `live` | Sent today under this name. Keep it. |-| `rename → X` | Sent today under an old name. SECRT-2722 renames it to `X`, the analytics plan's name. |-| `merge → X` | Sent today, duplicates `X`. SECRT-2722 stops sending it once dashboards read `X`; the name stays reserved. || `add` | Not sent yet. SECRT-2723 adds it; the name is already in `PlannedPostHogEvent`. |-| `remove` | Declared in code but never sent. SECRT-2722 deletes it. |++Events that are no longer sent are listed under [Removed](#removed), with+what replaced them.## Acquisition@@ -155,7 +150,7 @@ The tour funnel is sent to DataFast today (`tour_start`, `tour_scenario_start`,| `onboarding_step_viewed` | browser | add | `step` (`team`, `autopilot`, `role`, `pain_points`, `connect`, `hire`, `preparing`) | A wizard step is shown (once per tab, same keys as the DataFast `onboarding_<step>` goals). || `onboarding_completed` | backend | add | — | The onboarding is marked complete. || `brain_dump_started` | browser | live | — | Recording starts. |-| `brain_dump_completed` | browser | live | `input_mode`, `duration_secs` (voice) or `chars` (typed) | A dump was accepted and the wizard advances. |+| `brain_dump_completed` | browser | live | `input_mode`, `duration_secs` and `finalize_latency_ms` (voice) or `chars` (typed) | A dump was accepted and the wizard advances. `finalize_latency_ms` is the `finalizeBrainDump()` round trip, timed after the upload flush. || `brain_dump_canceled` | browser | live | — | Recording is cancelled. || `brain_dump_skipped` | browser | live | — | The step is skipped. || `brain_dump_recovery_shown` | browser | live | `parts` | A saved partial recording is offered back. |@@ -165,8 +160,7 @@ The tour funnel is sent to DataFast today (`tour_start`, `tour_scenario_start`,| `brain_dump_download` | browser | live | — | The recording is downloaded after a failure. || `brain_dump_permission_denied` | browser | live | — | Microphone permission is refused. || `brain_dump_typed_fallback` | browser | live | `reason` | The user types instead of speaking. |-| `transcription_failed` | browser | live | `error_code` | Finalize returns a failure. |-| `finalize_latency_ms` | browser | merge → `brain_dump_completed` | `ms`, `input_mode` | Finalize returns. Becomes a `finalize_latency_ms` property on `brain_dump_completed` and `transcription_failed`. |+| `transcription_failed` | browser | live | `error_code`, `finalize_latency_ms` | Finalize returns a failure. || `welcome_dialog_closed` | browser | live | — | The first-landing welcome dialog closes. || `capability_card_viewed` | browser | live | `card_index`, `deck` | A capability card is shown. || `capability_cards_completed` | browser | live | `card_index`, `deck` | The deck is finished. |@@ -174,13 +168,9 @@ The tour funnel is sent to DataFast today (`tour_start`, `tour_scenario_start`,| `intro_path` | browser | live | `path` | The intro path (A/B) is chosen. || `intro_followup_sent` | browser | live | `chars` | First message after the intro card. || `later_dump_completed` | browser | live | — | A brain dump sent later from the composer. |-| `expert_recommended` | browser | live | `template_id`, `position`, `source` (see base properties) | A recommended expert card is shown. |+| `expert_recommended` | browser | live | `template_id`, `position`, `team_source` | A recommended expert card is shown. || `expert_recommendation_clicked` | browser | live | `template_id`, `position` | A recommended expert card is clicked. || `hire_step_continued` | browser | live | `hired`, `recommended` | The hire step is left. |-| `onboarding_expert_hired` | browser | merge → `expert_hired` | `template_id`, `position` | A hire lands from the onboarding hire step. |-| `intro_card_dismissed` | browser | remove | — | Never sent. |-| `raise_door_clicked` | browser | remove | — | Never sent. |-| `intro_start_with_autopilot` | browser | remove | — | Never sent. || `tab_intro_shown` | browser | live | `tab` | A tab intro card is shown. || `tab_intro_cta_clicked` | browser | live | `tab`, `cta` | Its primary CTA is used. || `tab_intro_dismissed` | browser | live | `tab` | It is dismissed any other way. |@@ -197,43 +187,32 @@ The tour funnel is sent to DataFast today (`tour_start`, `tour_scenario_start`,| Event | Sender | Status | Required properties | Fires when || --- | --- | --- | --- | --- |-| `run_agent` | backend | rename → `agent_run_started` | `graph_id`, `graph_exec_id`, `trigger` (`manual`, `api`, `copilot`), `trigger_ref`, `preset_id` | A person starts a non-expert agent run. |-| `run_autopilot` | backend | rename → `chat_message_sent` | `session_id`, `origin`, `surface`, `kind: chat_turn` | A person sends a message in an Autopilot chat. |-| `run_expert` | backend | rename → `chat_message_sent` (chat turn) / `agent_run_started` (workflow run) | `expert_id`, `kind` (`chat_turn` or `workflow_run`); chat: `session_id`, `origin`, `surface`; run: `graph_id`, `graph_exec_id`, `trigger` | A person messages an expert or starts an expert workflow. |-| `copilot_message_sent` | backend | merge → `chat_message_sent` | `session_id`, `message_length` | Same chat turn. `message_length` moves onto `chat_message_sent`. |-| `agent_run_completed` | backend | rename → `agent_run_finished` (`status: completed`) | `graph_id`, `graph_exec_id`, `trigger`, `expert_id`, `cost_cents`, `duration_seconds` | A run reaches COMPLETED (sub-graph and automated runs included). |-| `agent_run_failed` | backend | rename → `agent_run_finished` (`status: failed`) | as above plus `failure_reason` | A run reaches FAILED. |-| `expert_run_completed` | backend | merge → `agent_run_finished` | `expert_id`, `status`, `graph_exec_id` | A top-level expert run reaches COMPLETED or FAILED. Same as `agent_run_*` with `expert_id` set on a top-level run. |-| `copilot_agent_run_success` | backend | rename → `chat_outcome` (`outcome_type: agent_run_success`) | `session_id`, `graph_id`, `graph_name`, `execution_id`, `library_agent_id` | The copilot started a run for the user: a moment of value in the chat. |-| `schedule_created` | backend | live | `schedule_id`, `target` (`agent`, `autopilot`, `expert`), `expert_id`, `cron`, `is_recurring`, `run_at`, `graph_id`, `session_id` | Any schedule is registered, from any surface. |-| `copilot_agent_scheduled` | backend | rename → `chat_outcome` (`outcome_type: schedule_created`) | `session_id`, `graph_id`, `schedule_id`, `cron`, ... | The copilot scheduled an agent. `schedule_created` (`target: agent`) still counts the schedule itself. |-| `copilot_followup_scheduled` | backend | rename → `chat_outcome` (`outcome_type: schedule_created`) | `session_id`, `schedule_id`, `is_recurring` | The copilot scheduled its own follow-up. `schedule_created` (`target: autopilot` or `expert`) still counts the schedule itself. |-| `copilot_tool_called` | backend | rename → `chat_tool_called` | `session_id`, `tool_name`, `tool_call_id` | The copilot calls a tool. |-| `copilot_library_check_outcome` | backend | rename → `chat_library_check_outcome` | `session_id`, `outcome`, `matches_count`, `top_score` | The create-agent library check ends. |-| `copilot_trigger_setup` | backend | remove | — | `track_trigger_setup` has no caller. |+| `agent_run_started` | backend | live | `graph_id`, `graph_exec_id`, `via` (`manual`, `api`, `copilot`), `via_ref`, `preset_id`; an expert's workflow run adds `expert_id` and `kind: workflow_run` | A person starts an agent run. |+| `chat_message_sent` | backend | live | `chat_session_id`, `origin` (`web`, or the bot platform the chat was opened from: `slack`, `discord`, ...), `kind: chat_turn`, `message_length`; `expert_id` in an expert chat | A person sends a message in an Autopilot or expert chat. |+| `agent_run_finished` | backend | live | `status` (`completed`, `failed`), `graph_id`, `graph_exec_id`, `via`, `expert_id`, `cost_cents`, `duration_seconds`, `is_subgraph_run`; `failure_reason` when failed | A run reaches COMPLETED or FAILED (sub-graph and automated runs included). A top-level expert run is `expert_id` set and `is_subgraph_run` false. Deduplicated on `graph_exec_id`, so a requeue or resume that finishes the same run again sends no second event. |+| `schedule_created` | backend | live | `schedule_id`, `target` (`agent`, `autopilot`, `expert`), `expert_id`, `cron`, `is_recurring`, `run_at`, `graph_id`, `chat_session_id` | Any schedule is registered, from any surface. |+| `chat_tool_called` | backend | live | `chat_session_id`, `tool_name`, `tool_call_id` | The copilot calls a tool. |+| `chat_outcome` | backend | live | `outcome_type` (`agent_run_success`, `schedule_created`), `chat_session_id`; `agent_run_success`: `graph_id`, `execution_id`, `library_agent_id`; `schedule_created`: `target` (`agent`: `graph_id`, `schedule_id`, `cron`, `library_agent_id`; `followup`: `schedule_id`, `target_chat_session_id`, `is_recurring`) | A moment of value in a chat: the copilot started a (non-dry) run or created a schedule for the user. `agent_run_started` / `schedule_created` still count the run or schedule itself. |+| `chat_library_check_outcome` | backend | live | `chat_session_id`, `outcome`, `matches_count`, `top_score` | The create-agent library check ends. || `voice_mode_started` | browser | live | `entry` | Voice mode is switched on. || `voice_mode_stopped` | browser | live | `turns`, `state` | Switched off by the user. || `voice_mode_timed_out` | browser | live | `turns`, `state` | Closed by the silence timeout. |-| `voice_turn_sent` | browser | live | `turn_index`, `transcript_chars` | A spoken turn is sent. |-| `voice_turn_dropped` | browser | live | `reason` | A spoken turn is discarded. |-| `voice_turn_completed` | browser | live | `turn_index` | The mic reopens after the reply. |+| `voice_turn_sent` | browser | live | `turn_index`, `transcript_chars`, `transcribe_latency_ms` | A spoken turn is sent. |+| `voice_turn_dropped` | browser | live | `reason` (`vad_misfire`, `transcribe_failed`, `filler_or_empty`, `interrupted`); `transcribe_latency_ms` with `filler_or_empty`; `turn_index` and `first_sound_latency_ms` (null when nothing played yet) with `interrupted` | A spoken turn is discarded, or with `interrupted` a sent turn's reply is cut off by switching voice mode off or leaving the page. A sent turn ends in exactly one of `voice_turn_completed` or this. |+| `voice_turn_completed` | browser | live | `turn_index`, `first_sound_latency_ms` (null when nothing played) | The mic reopens after the reply. || `voice_transcribe_retried` | browser | live | `turn_index` | A failed transcription is retried. || `voice_recording_downloaded` | browser | live | `turn_index` | The audio is downloaded instead. || `voice_mode_permission_denied` | browser | live | `stage` | Microphone permission is refused. || `voice_mode_error` | browser | live | `stage` | The VAD, synthesis or send failed. |-| `voice_transcribe_latency_ms` | browser | merge → `voice_turn_sent` | `ms` | Becomes a `transcribe_latency_ms` property on `voice_turn_sent` (and on `voice_turn_dropped` with `reason: filler_or_empty`). |-| `voice_first_sound_latency_ms` | browser | merge → `voice_turn_completed` | `ms` | Becomes a `first_sound_latency_ms` property on `voice_turn_completed`. || `experts_section_viewed` | browser | live | — | The marketplace experts shelf renders with results. || `expert_profile_opened` | browser | live | `template_id` | An expert profile page opens. |-| `hire_started` | browser | live | `template_id` | A hire button is clicked. Sent twice per click from the onboarding hire step (`useHireStep.ts`); SECRT-2722 keeps one. |+| `hire_started` | browser | live | `template_id`, `surface` (`onboarding`, `expert_page`) | A hire button is clicked. || `hire_flow_abandoned` | browser | live | `template_id`, `stage` | The hire dialog is closed before hiring. |-| `expert_hired` | backend | live | `expert_id`, `template_id`, `name` | An expert is hired. SECRT-2722 moves it into `hire_expert` so an idempotent re-hire of an active expert no longer fires, and adds `failed_preloads_count` and the hiring `surface`. |-| `hire_completed` | backend | merge → `expert_hired` | `template_id`, `failed_preloads_count` | Same hire, already skipping re-hires. |-| `hire_flow_completed` | browser | merge → `expert_hired` | `template_id`, `expert_id`, `elapsed_ms`, `voice_picked` | Same hire, from the marketplace hire dialog. |+| `expert_hired` | backend | live | `expert_id`, `template_id`, `name`, `failed_preloads_count`, `surface` (`onboarding`, `expert_page`, `copilot`; unset for other API callers) | An expert is hired or an archived one revived, from any surface (`experts_db.hire_expert`). Sent once the hire's background setup settles (`_run_hire_setup`), so `failed_preloads_count` is known; a revival with nothing to set up sends it at once. An idempotent re-hire of an active expert does not fire. || `hire_failed` | backend | live | `template_id`, `failed_preloads_count` | Hiring raised. || `expert_thread_created` | browser | live | `expert_id` | A new expert chat is created. || `writing_style_added` | backend | live | `expert_id` | A writing style is saved on an expert. |-| `workflow_installed_on_expert` | backend | live | `expert_id`, `source` (see base properties), `library_agent_id` or `store_listing_version_id` | A workflow is attached to an expert. |+| `workflow_installed_on_expert` | backend | live | `expert_id`, `workflow_source` (`library`, `marketplace`), `library_agent_id` or `store_listing_version_id` | A workflow is attached to an expert. || `home_viewed` | browser | live | — | The home dashboard renders with data. || `home_attention_actioned` | browser | live | `kind`, `action` | A "needs you" item is approved or declined. || `home_team_member_clicked` | browser | live | `expert_id` | A team member row is clicked. |@@ -249,21 +228,21 @@ The tour funnel is sent to DataFast today (`tour_start`, `tour_scenario_start`,| `billing_portal_opened` | browser | add | `surface` | The Stripe billing portal is opened. || `checkout_started` | backend | add | `checkout_kind` (`subscription`, `top_up`), `subscription_tier`, `billing_cycle`, `surface` | A Stripe Checkout session is created. || `checkout_abandoned` | browser | add | `checkout_kind`, `surface` | The user returns from Stripe Checkout without paying (the cancel URL). Browser-sent: Stripe only reports the expiry a day later. |-| `subscription_trial_offer_viewed` | browser | rename → `trial_offer_viewed` | `trial_offer_version`, `subscription_tier`, `trial_duration_days`, `surface` | A trial offer card is shown. |+| `trial_offer_viewed` | browser | live | `trial_offer_version`, `subscription_tier`, `trial_duration_days`, `surface` | A trial offer card is shown. || `subscription_trial_checkout_started` | browser | live | `trial_offer_version`, `surface` | Trial checkout is opened. Once `checkout_started` ships, fold this in as `checkout_kind: trial`. |-| `subscription_trial_started` | backend | rename → `trial_started` | `trial_id`, `trial_offer_version`, `subscription_tier`, `billing_cycle`, `trial_duration_days` | The trial starts. Like every trial lifecycle event, it is sent only when its notification email is queued. |-| `subscription_trial_ending` | backend | rename → `trial_ending` | as above | The reminder window opens. |-| `subscription_trial_canceled` | backend | rename → `trial_canceled` | as above | The trial is set to cancel. |-| `subscription_trial_resumed` | backend | rename → `trial_resumed` | as above | A cancelled trial is resumed. |-| `subscription_trial_payment_failed` | backend | rename → `payment_failed` | as above | The conversion charge fails. |-| `subscription_trial_converted` | backend | rename → `trial_converted` | as above | The trial converts to paid. |-| `subscription_trial_ended` | backend | rename → `trial_ended` | as above | The trial ends without converting. |-| `subscription_upgraded` | backend | rename → `subscription_changed` (`change_type: upgrade`) | `previous_subscription_tier`, `subscription_tier`, `billing_cycle` | A paid tier change takes effect. |-| `subscription_payment_success` | backend | rename → `payment_succeeded` | `subscription_tier`, `billing_cycle`; SECRT-2723 adds `amount_cents`, `currency` | A subscription invoice is paid. |-| `credit_topup_success` | backend | rename → `topup_completed` | `amount_credits`, `top_up_type`; SECRT-2723 adds `amount_cents`, `currency` | Credits are bought. |+| `trial_started` | backend | live | `trial_id`, `trial_offer_version`, `subscription_tier`, `billing_cycle`, `trial_duration_days` | The trial starts. Like every trial lifecycle event, it is sent only when its notification email is queued. |+| `trial_ending` | backend | live | as above | The reminder window opens. |+| `trial_canceled` | backend | live | as above | The trial is set to cancel. |+| `trial_resumed` | backend | live | as above | A cancelled trial is resumed. |+| `payment_failed` | backend | live | as above | The conversion charge fails. |+| `trial_converted` | backend | live | as above | The trial converts to paid. |+| `trial_ended` | backend | live | as above | The trial ends without converting. |+| `subscription_changed` | backend | live | `change_type: upgrade`, `previous_subscription_tier`, `subscription_tier`, `billing_cycle` | A paid tier upgrade takes effect. Downgrades and cancellations are not sent here yet. |+| `payment_succeeded` | backend | live | `subscription_tier`, `billing_cycle`; SECRT-2723 adds `amount_cents`, `currency` | A subscription invoice is paid. |+| `topup_completed` | backend | live | `amount_credits`, `top_up_type`; SECRT-2723 adds `amount_cents`, `currency` | Credits are bought. || `subscription_cancellation_scheduled` | backend | live | `subscription_tier` | A paid plan is set to cancel at period end. || `subscription_ended` | backend | add | `subscription_tier`, `billing_cycle`, `reason` | A paid subscription ends (Stripe `customer.subscription.deleted`). |-| `subscription_tier_reconciliation_discrepancy` | backend | rename → `subscription_tier_reconciled` | `direction`, `previous_subscription_tier`, `subscription_tier`, `via` | Ops signal: Stripe and the stored tier disagreed. Not a user action; keep out of funnels. |+| `subscription_tier_reconciled` | backend | live | `direction`, `previous_subscription_tier`, `subscription_tier`, `via` | Ops signal: Stripe and the stored tier disagreed. Not a user action; keep out of funnels. |The onboarding paywall's `paywall_view`, `paywall_checkout_cancelled` and`hire_completed` goals go to DataFast only, which is why the paywall has no@@ -273,7 +252,7 @@ PostHog funnel yet.| Event | Sender | Status | Required properties | Fires when || --- | --- | --- | --- | --- |-| `schedule_fired` | backend | live | `schedule_id`, `target`, `expert_id`, `graph_id`, `graph_exec_id` or `session_id` | A schedule produces work. |+| `schedule_fired` | backend | live | `schedule_id`, `target`, `expert_id`, `graph_id`, `graph_exec_id` or `chat_session_id` | A schedule produces work. || `trigger_fired` | backend | live | `webhook_id`, `graph_id`, `graph_exec_id`, `expert_id`, `preset_id`, `target` | A webhook produces a run. || `briefing_generated` | backend | live | `run_count`, `decision_count`, `has_content` | A morning briefing is composed (or found empty). || `briefing_delivered` | backend | live | `briefing_id` | It is posted to the user's thread. |@@ -288,9 +267,8 @@ Return visits are `$pageview`; account-level retention is computed in| Event | Sender | Status | Required properties | Fires when || --- | --- | --- | --- | --- |-| `experiment_exposed` | browser | live | `experiment_key`, `variant`, `provider: launchdarkly`, `$feature/<flag>` | A LaunchDarkly-bucketed arm is shown to a signed-in user (`useLaunchDarklyExperiment`). || `$feature_flag_called` | browser (posthog-js) | live | set by PostHog | A PostHog flag is read (`useExperiment`). |-| `feature_flag_mismatch` | browser | rename → `feature_flag_mismatched` | `flag`, `launchdarkly` (`value`, `resolved`), `posthog` (`value`, `resolved`) | Ops signal: with both flag vendors configured, LaunchDarkly and PostHog resolved a flag to different values (`useDualFlag`). Not a user action; keep out of funnels. The analytics plan has no name for it; SECRT-2722 gives it the `object_action` past-tense form before it reaches production. |+| `feature_flag_mismatched` | browser | live | `flag`, `launchdarkly` (`value`, `resolved`), `posthog` (`value`, `resolved`) | Ops signal: with both flag vendors configured, LaunchDarkly and PostHog resolved a flag to different values (`useDualFlag`). Not a user action; keep out of funnels. |Arms are also stored in the database (`analytics.experiment_assignment`),so an experiment can be read in PostHog and Looker alike.@@ -318,6 +296,73 @@ Dates are ISO-8601 UTC strings of the lifecycle moment, not of the sync. Adate that doesn't apply to the current status is `$unset`, never sent asnull.+## Removed++No longer sent (SECRT-2722). The names stay reserved: the pin tests fail if+one comes back, because reusing it would splice a different action onto the+history PostHog already holds.++| Event | Was sent by | Read instead |+| --- | --- | --- |+| `hire_completed` | backend | `expert_hired` (same hire; it now also skips idempotent re-hires). The DataFast `hire_completed` goal is unchanged. |+| `hire_flow_completed` | browser | `expert_hired` with `surface: expert_page`. `elapsed_ms` is PostHog's time to convert from `hire_started`; `voice_picked` is dropped. |+| `onboarding_expert_hired` | browser | `expert_hired` with `surface: onboarding`. The card `position` is on `expert_recommendation_clicked`. |+| `copilot_message_sent` | backend | `chat_message_sent`, which carries `message_length`. |+| `expert_run_completed` | backend | `agent_run_finished` with `expert_id` set and `is_subgraph_run: false`. |+| `finalize_latency_ms` | browser | The `finalize_latency_ms` property on `brain_dump_completed` and `transcription_failed`. |+| `voice_transcribe_latency_ms` | browser | The `transcribe_latency_ms` property on `voice_turn_sent` (and `voice_turn_dropped` with `reason: filler_or_empty`). |+| `voice_first_sound_latency_ms` | browser | The `first_sound_latency_ms` property on `voice_turn_completed`, or on `voice_turn_dropped` with `reason: interrupted` for a reply that was cut off. |+| `experiment_exposed` | browser | Nothing: `useLaunchDarklyExperiment` had no caller and was deleted. PostHog-bucketed experiments use `$feature_flag_called`. |+| `copilot_trigger_setup` | backend | Nothing: never sent (no caller). |+| `intro_card_dismissed`, `raise_door_clicked`, `intro_start_with_autopilot` | browser | Nothing: declared, never sent. |++## Renamed++Renamed once to the analytics plan's names (SECRT-2722). The old names are+reserved like removed ones; an insight on the old name must be pointed at the+new one.++| Old name | New name | Filter to keep the old meaning |+| --- | --- | --- |+| `run_agent` | `agent_run_started` | `expert_id` is not set |+| `run_expert` (`kind: workflow_run`) | `agent_run_started` | `expert_id` is set |+| `run_autopilot` | `chat_message_sent` | `expert_id` is not set |+| `run_expert` (`kind: chat_turn`) | `chat_message_sent` | `expert_id` is set |+| `agent_run_completed` | `agent_run_finished` | `status = completed` |+| `agent_run_failed` | `agent_run_finished` | `status = failed` |+| `copilot_tool_called` | `chat_tool_called` | — |+| `copilot_library_check_outcome` | `chat_library_check_outcome` | — |+| `copilot_agent_run_success` | `chat_outcome` | `outcome_type = agent_run_success`. Dry runs no longer count; `graph_name` is dropped. |+| `copilot_agent_scheduled` | `chat_outcome` | `outcome_type = schedule_created` and `target = agent`. `graph_name` and `schedule_name` are dropped. |+| `copilot_followup_scheduled` | `chat_outcome` | `outcome_type = schedule_created` and `target = followup`. `chat_session_id` is now the chat that scheduled it; the destination is `target_chat_session_id`. |+| `subscription_trial_offer_viewed` | `trial_offer_viewed` | — |+| `subscription_trial_started` | `trial_started` | — |+| `subscription_trial_ending` | `trial_ending` | — |+| `subscription_trial_canceled` | `trial_canceled` | — |+| `subscription_trial_resumed` | `trial_resumed` | — |+| `subscription_trial_converted` | `trial_converted` | — |+| `subscription_trial_ended` | `trial_ended` | — |+| `subscription_trial_payment_failed` | `payment_failed` | — (only trial conversion charges send it today) |+| `subscription_upgraded` | `subscription_changed` | `change_type = upgrade` |+| `subscription_payment_success` | `payment_succeeded` | — |+| `credit_topup_success` | `topup_completed` | — |+| `subscription_tier_reconciliation_discrepancy` | `subscription_tier_reconciled` | — |+| `feature_flag_mismatch` | `feature_flag_mismatched` | — (not in the analytics plan; renamed to the `object_action` past-tense form before it reached production) |++### Renamed properties++Renamed in the same release as the events, to the analytics plan's envelope,+so filters and breakdowns break once. Values are unchanged unless noted.++| Old property | New property | On |+| --- | --- | --- |+| `session_id` | `chat_session_id` | `chat_message_sent`, `chat_tool_called`, `chat_outcome`, `chat_library_check_outcome`, `schedule_created`, `schedule_fired` |+| `target_session_id` | `target_chat_session_id` | `chat_outcome` (`target = followup`) |+| `trigger` | `via` | `agent_run_started`, `agent_run_finished` |+| `trigger_ref` | `via_ref` | `agent_run_started` |+| `surface` (`chat`, or the bot platform) | `origin` (`web`, or the bot platform) | `chat_message_sent`. The plan's `origin` is the channel. |+| `origin` (`interactive`) | removed | `chat_message_sent`. Automation turns are not sent, so it was always `interactive`. |+## Differences from the analytics planLeft for follow-up changes, so this list and the plan can be compared line by@@ -329,8 +374,6 @@ line:`hire_step_continued` become properties of `onboarding_step_viewed` /`_completed` / `_skipped` / `_back`. Spoken turns (`voice_turn_sent`)become `chat_message_sent` with `input_mode: voice`.-- **Property names.** The plan's envelope says `chat_session_id` and `via`;- chat events still send `session_id` and run events `trigger`.- **`subscription_changed` covers upgrades only.** The plan also countscancellations and downgrades there; `subscription_cancellation_scheduled`and `subscription_ended` stay separate events.