:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(intercom): drain companies scroll before fetching segments (#65194)
Tom Owers3a28092125be
diff --git a/posthog/temporal/data_imports/sources/intercom/intercom.py b/posthog/temporal/data_imports/sources/intercom/intercom.pyindex 94786f913793..42daad4ececa 100644--- a/posthog/temporal/data_imports/sources/intercom/intercom.py+++ b/posthog/temporal/data_imports/sources/intercom/intercom.py@@ -383,17 +383,25 @@ def _iter_companies(session: Session) -> Iterator[dict[str, Any]]:def _company_segments_generator(session: Session) -> Iterator[dict[str, Any]]:"""Walk all companies and yield each attached segment with `company_id`injected. Full refresh — Intercom has no server-side timestamp filter on- either parent or child."""- for company in _iter_companies(session):+ either parent or child.++ The scroll is drained up front rather than interleaved with the per-company+ segment fetches: Intercom expires an idle companies scroll after ~1 minute,+ and a slow stretch of `/companies/{id}/segments` calls between two scroll+ pages lets the cursor lapse — the next continuation then 404s mid-walk.+ Draining first keeps the scroll requests back-to-back so it stays alive;+ only the ids are held, so the memory cost stays flat regardless of count."""+ company_ids = [company["id"] for company in _iter_companies(session)]+ for company_id in company_ids:try:- payload = _intercom_get(session, f"/companies/{company['id']}/segments")+ payload = _intercom_get(session, f"/companies/{company_id}/segments")except HTTPError as exc:if _is_not_found(exc):- logger.warning("intercom_company_not_found", company_id=company["id"])+ logger.warning("intercom_company_not_found", company_id=company_id)continueraisefor seg in payload.get("data", []) or []:- seg["company_id"] = company["id"]+ seg["company_id"] = company_idyield segdiff --git a/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py b/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.pyindex 8d1b9ef17a33..0a2068916d0f 100644--- a/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py+++ b/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py@@ -312,15 +312,16 @@ class TestSubstreamGenerators:list(_conversation_parts_generator(mock_session, "updated_at", None))def test_company_segments_skips_404_parent(self):- # The parent companies walk (scroll) and the per-company segments fetch- # both go through GET, so the calls interleave on `session.get` in order:- # scroll page -> segments(co1) -> segments(co2) -> empty scroll page.+ # The scroll is drained fully before any per-company segment fetch, so+ # `session.get` is called in order: scroll page -> empty scroll page ->+ # segments(co1) -> segments(co2). A 404 on a single segment fetch (the+ # company vanished between listing and fetch) skips that company only.mock_session = mock.MagicMock()mock_session.get.side_effect = [_make_response({"data": [{"id": "co1"}, {"id": "co2"}], "scroll_param": "s1"}),+ _make_response({"data": [], "scroll_param": "s2"}),_make_response(None, status_code=404, text="Not Found"),_make_response({"data": [{"id": "s2"}]}),- _make_response({"data": [], "scroll_param": "s2"}),]segments = list(_company_segments_generator(mock_session))@@ -329,9 +330,13 @@ class TestSubstreamGenerators:assert segments[0]["company_id"] == "co2"def test_company_segments_reraises_non_404(self):+ # 500 is retried inline by `_scroll_companies_get`; a non-404 on the+ # per-company segment fetch (not the scroll) must surface. Drain the+ # scroll first, then fail the segment fetch with a 500.mock_session = mock.MagicMock()mock_session.get.side_effect = [_make_response({"data": [{"id": "co1"}], "scroll_param": "s1"}),+ _make_response({"data": []}),_make_response(None, status_code=500, text="Server Error"),]@@ -342,9 +347,9 @@ class TestSubstreamGenerators:mock_session = mock.MagicMock()mock_session.get.side_effect = [_make_response({"data": [{"id": "co1"}, {"id": "co2"}], "scroll_param": "s1"}),+ _make_response({"data": [], "scroll_param": "s2"}),_make_response({"data": [{"id": "s1"}]}),_make_response({"data": [{"id": "s2"}, {"id": "s3"}]}),- _make_response({"data": [], "scroll_param": "s2"}),]segments = list(_company_segments_generator(mock_session))@@ -353,6 +358,28 @@ class TestSubstreamGenerators:assert segments[0]["company_id"] == "co1"assert segments[1]["company_id"] == "co2"+ def test_company_segments_drains_scroll_before_fetching_segments(self):+ # Intercom expires an idle companies scroll after ~1 min. Fetching+ # segments between scroll pages let the cursor lapse mid-walk, 404ing the+ # next continuation. The scroll must be fully walked before any segment+ # fetch, so the scroll requests stay back-to-back.+ mock_session = mock.MagicMock()+ mock_session.get.side_effect = [+ _make_response({"data": [{"id": "co1"}], "scroll_param": "s1"}),+ _make_response({"data": [{"id": "co2"}], "scroll_param": "s2"}),+ _make_response({"data": []}),+ _make_response({"data": [{"id": "seg1"}]}),+ _make_response({"data": [{"id": "seg2"}]}),+ ]++ segments = list(_company_segments_generator(mock_session))++ assert [s["id"] for s in segments] == ["seg1", "seg2"]+ urls = [call.args[0] for call in mock_session.get.call_args_list]+ last_scroll = max(i for i, u in enumerate(urls) if u.endswith("/companies/scroll"))+ first_segments = min(i for i, u in enumerate(urls) if u.endswith("/segments"))+ assert last_scroll < first_segments+def test_iter_companies_walks_scroll(self):# `POST /companies/list` is capped at 10,000 companies (60 * 167 page# crosses the ceiling and Intercom 400s). The Scroll API has no ceiling: