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.

feat(warehouse-sources): Decagon knowledge articles and usage tables (#80184)

jake sciotto68b0f2144f64
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/canonical_descriptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/canonical_descriptions.py
index 691adad18e46..80da44caeb4b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/canonical_descriptions.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/canonical_descriptions.py
@@ -38,4 +38,32 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
),
},
},
+ "articles": {
+ "description": (
+ "A knowledge base article Decagon's AI agent can answer with, including its full "
+ "body content and where it was synced from."
+ ),
+ "docs_url": "https://docs.decagon.ai/api-reference/getting-started",
+ "columns": {
+ "id": "Unique identifier for the article.",
+ "content": "Full body content of the article.",
+ "source": "System the article comes from.",
+ "url": "URL of the article in its source system.",
+ "metadata": "Custom metadata attached to the article.",
+ "external_document_id": (
+ "Identifier of the article in the system it was synced from. Null for articles "
+ "created manually in Decagon."
+ ),
+ "tags": "Tags applied to the article.",
+ "created_at": "Timestamp at which the article was created.",
+ "updated_at": "Timestamp at which the article was last updated.",
+ },
+ },
+ "article_usage": {
+ "description": (
+ "Usage counts per knowledge base article, as returned by Decagon's article usage "
+ "endpoint. A point-in-time snapshot bucketed in UTC, replaced on every sync."
+ ),
+ "docs_url": "https://docs.decagon.ai/api-reference/getting-started",
+ },
}
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 6e3e8d81798e..73da27379277 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
@@ -306,13 +306,17 @@ def get_rows(
total = data.get(config.total_key) if config.total_key else None
if config.pagination == "page":
- # Terminate against the reported total using rows actually received, which
- # stays exact even if the server caps the requested page size. A missing or
- # malformed total falls back to short-page termination, the only end signal
- # left besides an empty page.
- rows_walked += len(items)
+ # Terminate against the reported total using rows actually kept: a row that
+ # shifted pages mid-walk arrives twice but counts once toward the server's
+ # unique total, so counting raw items could reach the total a page early and
+ # drop the final page. Counting kept rows also stays exact if the server caps
+ # the requested page size. A page that contributes nothing new cannot make
+ # progress against the total, so it ends the walk rather than spinning on a
+ # server that ignores the page param. A missing or malformed total falls back
+ # to short-page termination, the only end signal left besides an empty page.
+ rows_walked += len(fresh)
if isinstance(total, int | float):
- exhausted = not items or rows_walked >= total
+ exhausted = not fresh or rows_walked >= total
else:
exhausted = not items or (config.page_size is not None and len(items) < config.page_size)
if fresh:
@@ -375,4 +379,6 @@ def decagon_source(
partition_format="month" if partitioned else None,
partition_keys=[config.partition_key] if config.partition_key is not None else None,
sort_mode=config.sort_mode,
+ chunk_size=config.chunk_size,
+ chunk_size_bytes=config.chunk_size_bytes,
)
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 20622371e5de..69fb86e17af2 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
@@ -71,6 +71,11 @@ class DecagonEndpointConfig:
# Whether the table is selected by default on a new source. False for tables that
# fan out row counts or sync data a team should opt into deliberately.
should_sync_default: bool = True
+ # Batcher overrides for endpoints whose rows are large (whole documents), so the
+ # source-to-Arrow conversion does not materialize an oversized table. None keeps the
+ # pipeline defaults.
+ chunk_size: Optional[int] = None
+ chunk_size_bytes: Optional[int] = None
# Decagon's per-endpoint contracts differ enough that everything rides this catalog: four
@@ -142,6 +147,38 @@ DECAGON_ENDPOINTS: dict[str, DecagonEndpointConfig] = {
sort_mode="desc",
supports_append=True,
),
+ # /article/all is the knowledge-base catalog Decagon's AI agent deflects with. It
+ # pages on page/page_size over a `total` count and exposes no server-side timestamp
+ # filter, so the table is full refresh only. `content` holds full article bodies,
+ # which is why the batcher chunks are capped and the table ships opt-in until row
+ # sizes are confirmed against a real knowledge base.
+ "articles": DecagonEndpointConfig(
+ name="articles",
+ path="/article/all",
+ data_key="articles",
+ primary_keys=["id"],
+ incremental_fields=[],
+ pagination="page",
+ page_size=100,
+ total_key="total",
+ partition_key="created_at",
+ should_sync_default=False,
+ chunk_size=500,
+ chunk_size_bytes=50 * 1024 * 1024,
+ ),
+ # /article/usage returns per-article usage in one unpaginated response. The spec
+ # elides the item schema behind {"usage": [...]}, so no primary key is claimed and
+ # the table syncs as a full-refresh snapshot. The timezone is pinned to UTC so the
+ # usage bucketing cannot silently shift with an account-level setting.
+ "article_usage": DecagonEndpointConfig(
+ name="article_usage",
+ path="/article/usage",
+ data_key="usage",
+ primary_keys=None,
+ incremental_fields=[],
+ pagination="single",
+ extra_params={"timezone": "UTC"},
+ ),
}
ENDPOINTS = tuple(DECAGON_ENDPOINTS.keys())
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 893dd60f99f8..f726a5f4fb68 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
@@ -504,6 +504,86 @@ class TestAgentAssistActions:
assert response.sort_mode == "desc"
+class TestArticleTables:
+ def test_articles_walk_pages_to_the_total_and_dedupes_shifted_rows(self) -> None:
+ # The catalog can mutate between page fetches, so a row can shift pages mid-walk;
+ # full-refresh writes are appends, so the shifted copy must be skipped.
+ manager = _fresh_manager()
+ responses = [
+ _make_response({"articles": [{"id": 1}, {"id": 2}], "total": 3}),
+ _make_response({"articles": [{"id": 2}, {"id": 3}], "total": 3}),
+ ]
+ sent_params, batches = _drive_rows(manager, responses, endpoint="articles")
+
+ assert sent_params == [
+ {"page": "1", "page_size": "100"},
+ {"page": "2", "page_size": "100"},
+ ]
+ assert [[r["id"] for r in b] for b in batches] == [[1, 2], [3]]
+
+ def test_shifted_duplicate_does_not_end_the_walk_before_the_total(self) -> None:
+ # The server's total counts unique articles. A row that shifted pages arrives
+ # twice but is kept once; counting raw items against the total would stop one
+ # page early and silently drop the final page's articles.
+ manager = _fresh_manager()
+ responses = [
+ _make_response({"articles": [{"id": 1}, {"id": 2}], "total": 4}),
+ _make_response({"articles": [{"id": 2}, {"id": 3}], "total": 4}),
+ _make_response({"articles": [{"id": 4}], "total": 4}),
+ ]
+ sent_params, batches = _drive_rows(manager, responses, endpoint="articles")
+
+ assert len(sent_params) == 3
+ assert [[r["id"] for r in b] for b in batches] == [[1, 2], [3], [4]]
+
+ def test_page_contributing_nothing_new_ends_the_walk(self) -> None:
+ # A server that ignores the page param would otherwise repeat the same page
+ # forever without the kept-row count ever reaching the total.
+ manager = _fresh_manager()
+ responses = [
+ _make_response({"articles": [{"id": 1}, {"id": 2}], "total": 10}),
+ _make_response({"articles": [{"id": 1}, {"id": 2}], "total": 10}),
+ ]
+ sent_params, batches = _drive_rows(manager, responses, endpoint="articles")
+
+ assert len(sent_params) == 2
+ assert [[r["id"] for r in b] for b in batches] == [[1, 2]]
+
+ def test_article_usage_is_a_single_request_pinned_to_utc(self) -> None:
+ # The timezone param changes how usage is bucketed; leaving it to the account
+ # default would let a dashboard setting silently shift the numbers.
+ manager = _fresh_manager()
+ responses = [_make_response({"usage": [{"article_id": 1, "count": 5}]})]
+ sent_params, batches = _drive_rows(manager, responses, endpoint="article_usage")
+
+ assert sent_params == [{"timezone": "UTC"}]
+ assert [len(b) for b in batches] == [1]
+ manager.save_state.assert_not_called()
+
+ def test_articles_response_caps_batcher_chunks_for_document_rows(self) -> None:
+ response = decagon_source(
+ api_key="key",
+ endpoint="articles",
+ logger=MagicMock(),
+ resumable_source_manager=MagicMock(spec=ResumableSourceManager),
+ )
+ assert response.primary_keys == ["id"]
+ assert response.partition_keys == ["created_at"]
+ assert response.chunk_size == 500
+ assert response.chunk_size_bytes == 50 * 1024 * 1024
+
+ def test_article_usage_response_is_keyless_and_unpartitioned(self) -> None:
+ response = decagon_source(
+ api_key="key",
+ endpoint="article_usage",
+ logger=MagicMock(),
+ resumable_source_manager=MagicMock(spec=ResumableSourceManager),
+ )
+ assert response.primary_keys is None
+ assert response.partition_mode is None
+ assert response.partition_keys is None
+
+
class TestToEpochSeconds:
# The pipeline hands the DateTime watermark back as a datetime, a date, or an epoch
# number depending on how it round-tripped through storage; the request boundary must
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/tests/test_decagon_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/tests/test_decagon_source.py
index d4aaf31486bc..31e9cd24b4ba 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/tests/test_decagon_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/tests/test_decagon_source.py
@@ -69,6 +69,17 @@ class TestDecagonSource:
assert actions.supports_append is True
assert [f["field"] for f in actions.incremental_fields] == ["created_at"]
+ articles = next(s for s in schemas if s.name == "articles")
+ # The catalog has no server-side timestamp filter, so no incremental option; the
+ # table also starts unselected until article body sizes are confirmed safe.
+ assert articles.supports_incremental is False
+ assert articles.supports_append is False
+ assert articles.should_sync_default is False
+
+ usage = next(s for s in schemas if s.name == "article_usage")
+ assert usage.supports_incremental is False
+ assert usage.supports_append is False
+
def test_get_schemas_filtered_by_names(self) -> None:
assert [s.name for s in self.source.get_schemas(self.config, self.team_id, names=["conversations"])] == [
"conversations"