Skip to content

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

fix(decagon): stop an ungranted add-on param blocking a whole table (#97407)

posthog[bot]4c9a6ea3a362
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/decagon.py b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/decagon.py
index 8b85991046ca..ac2185e69ef3 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/decagon.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/decagon.py
@@ -224,6 +224,27 @@ def get_rows(
return response.json()
+ # Decagon refuses the whole request when an optional add-on param names an entitlement
+ # the team does not hold, so a table whose base response would sync fine never loads.
+ # Drop the add-on params and retry; a 403 about the endpoint itself fails again and
+ # still surfaces. The drop persists for the rest of the walk so each page costs one
+ # request, which matters against the 1 rps limit.
+ optional_params_dropped = not config.optional_params
+
+ def fetch_page_with_optional(params: dict[str, str]) -> dict[str, Any]:
+ nonlocal optional_params_dropped
+ if not optional_params_dropped:
+ try:
+ return fetch_page({**params, **config.optional_params})
+ except requests.HTTPError as err:
+ if err.response is None or err.response.status_code != 403:
+ raise
+ optional_params_dropped = True
+ logger.warning(
+ f"Decagon: {endpoint} refused {sorted(config.optional_params)}; retrying without the add-on params"
+ )
+ return fetch_page(params)
+
def save_position(**position: Any) -> None:
# Persisted only after a yield, so a crash re-yields the last batch rather than
# skipping it (the duplicate rows a resumed re-yield can produce are bounded to
@@ -264,7 +285,7 @@ def get_rows(
if timestamp_filter and config.timestamp_filter_param:
params[config.timestamp_filter_param] = timestamp_filter
- data = fetch_page(params)
+ data = fetch_page_with_optional(params)
items = data.get(config.data_key) or []
fresh: list[dict[str, Any]] = []
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/settings.py
index 68c7d46eb3d8..e06b7467b5e6 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/settings.py
@@ -24,7 +24,7 @@ IncrementalParamFormat = Literal["epoch_seconds", "iso8601"]
CONVERSATIONS_NEXT_CURSOR_KEYS = ("next_page_cursor", "next_cursor", "next_page_updated_after")
-@dataclass
+@dataclass(frozen=True)
class DecagonEndpointConfig:
name: str
path: str
@@ -64,6 +64,10 @@ class DecagonEndpointConfig:
timestamp_filter_param: Optional[str] = None
# Static query params always sent.
extra_params: dict[str, str] = field(default_factory=dict)
+ # Params requesting an add-on Decagon entitles separately from the endpoint behind it.
+ # It 403s the whole request when a team lacks the add-on rather than returning the base
+ # response, so these are sent optimistically and dropped on a 403 (see get_rows).
+ optional_params: dict[str, str] = field(default_factory=dict)
# Order rows arrive in when the endpoint documents one. "desc" is also the safe
# declaration for endpoints whose order is undocumented: the pipeline then defers the
# incremental watermark to the end of a successful run instead of checkpointing
@@ -126,7 +130,9 @@ DECAGON_ENDPOINTS: dict[str, DecagonEndpointConfig] = {
# merging on a guessed composite key that could silently merge distinct actions.
# include_details adds a `detail` object (carrying detail.conversation_id, the join
# key to conversations) only when detail export is enabled for the team, so nothing
- # may depend on it being present.
+ # may depend on it being present. A team without that add-on is refused the whole
+ # request rather than served a response without `detail`, so the param rides
+ # optional_params and the walk retries without it.
"agent_assist_actions": DecagonEndpointConfig(
name="agent_assist_actions",
path="/agent_assist/actions/export",
@@ -146,7 +152,7 @@ DECAGON_ENDPOINTS: dict[str, DecagonEndpointConfig] = {
partition_key="created_at",
incremental_param="min_timestamp",
incremental_param_format="epoch_seconds",
- extra_params={"include_details": "true"},
+ optional_params={"include_details": "true"},
# This export documents no ordering, so desc is the safe declaration (see the
# field comment).
sort_mode="desc",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/tests/test_decagon.py b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/tests/test_decagon.py
index 97612a8d06ff..c8b74b737cd9 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/tests/test_decagon.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/tests/test_decagon.py
@@ -2,6 +2,7 @@ import json
from datetime import UTC, date, datetime
from typing import Any
+import pytest
from unittest.mock import MagicMock, patch
from parameterized import parameterized
@@ -492,6 +493,40 @@ class TestAgentAssistActions:
saved = [call.args[0] for call in manager.save_state.call_args_list]
assert saved == [DecagonResumeConfig(cursor="cur-1", min_timestamp=int(epoch))]
+ def test_a_refused_details_add_on_retries_the_walk_without_it(self) -> None:
+ # Detail export is entitled separately from the actions export, and a team without
+ # it is refused the whole request, so the table only syncs if the walk drops the
+ # param and retries.
+ manager = _fresh_manager()
+ responses = [
+ _make_response({"detail": "detail export is not enabled for this team."}, status_code=403),
+ _make_response({"events": [{"agent_name": "a"}], "has_more": False, "next_cursor": None}),
+ ]
+ sent_params, batches = _drive_rows(manager, responses, endpoint="agent_assist_actions")
+
+ assert sent_params == [{"include_details": "true"}, {}]
+ assert [len(b) for b in batches] == [1]
+
+ def test_a_refused_details_add_on_stays_dropped_for_later_pages(self) -> None:
+ manager = _fresh_manager()
+ responses = [
+ _make_response({}, status_code=403),
+ _make_response({"events": [{"agent_name": "a"}], "has_more": True, "next_cursor": "cur-1"}),
+ _make_response({"events": [{"agent_name": "b"}], "has_more": False, "next_cursor": None}),
+ ]
+ sent_params, batches = _drive_rows(manager, responses, endpoint="agent_assist_actions")
+
+ assert sent_params == [{"include_details": "true"}, {}, {"cursor": "cur-1"}]
+ assert [len(b) for b in batches] == [1, 1]
+
+ def test_a_403_without_add_on_params_still_fails_the_walk(self) -> None:
+ # The endpoint itself being refused must stay a failure rather than be swallowed
+ # by the retry.
+ manager = _fresh_manager()
+ responses = [_make_response({}, status_code=403), _make_response({}, status_code=403)]
+ with pytest.raises(HTTPError):
+ _drive_rows(manager, responses, endpoint="agent_assist_actions")
+
def test_source_response_is_a_keyless_append_stream(self) -> None:
response = decagon_source(
api_key="key",