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(custom): keep resource_defaults incremental keys away from the engine (#112268)

Tom Owers41367ec6f07b
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/source.py
index e38625f93ac9..15f1b542cb83 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/source.py
@@ -1269,12 +1269,15 @@ class CustomSource(SimpleSource[CustomSourceConfig]):
# drop parent rows (and with them their children); they full-scan
# every run, matching the built-in fan-out sources (Typeform/Sentry).
chain = _fanout_chain(manifest, inputs.schema_name)
- chosen = chain.child
+ manifest_without_default_incremental, default_incremental = _split_default_incremental(manifest)
+ chosen = _with_default_incremental(chain.child, default_incremental)
engine_resources = [
*(_without_incremental_config(r) for r in chain.ancestors),
- _strip_engine_unsupported_incremental_keys(chain.child),
+ _strip_engine_unsupported_incremental_keys(chosen),
]
- engine_manifest = cast(RESTAPIConfig, {**manifest, "resources": engine_resources})
+ engine_manifest = cast(
+ RESTAPIConfig, {**manifest_without_default_incremental, "resources": engine_resources}
+ )
# Backstop for manifests stored before create-time validation covered this: an
# endpoint.incremental block missing start_param crashes the engine with a bare,
@@ -1450,7 +1453,7 @@ class CustomSource(SimpleSource[CustomSourceConfig]):
engine_manifest = cast(
RESTAPIConfig,
{
- **manifest,
+ **_split_default_incremental(manifest)[0],
"resources": engine_resources,
"client": {
**client,
@@ -2111,6 +2114,39 @@ def _strip_engine_unsupported_incremental_keys(resource: dict[str, Any]) -> dict
return {**resource, "endpoint": {**endpoint, "incremental": cleaned}}
+def _split_default_incremental(manifest: dict[str, Any]) -> tuple[dict[str, Any], Any]:
+ """Return ``manifest`` without ``resource_defaults.endpoint.incremental``, plus that block.
+
+ The engine merges ``resource_defaults.endpoint`` into each resource shallowly, so a
+ default ``incremental`` block reaches every resource that has none of its own. It would
+ skip ``_strip_engine_unsupported_incremental_keys`` and the full scan of fan-out
+ ancestors. Callers apply the returned block to the chosen resource themselves.
+ """
+ defaults = manifest.get("resource_defaults")
+ if not isinstance(defaults, dict):
+ return manifest, None
+ default_endpoint = defaults.get("endpoint")
+ if not isinstance(default_endpoint, dict) or "incremental" not in default_endpoint:
+ return manifest, None
+ engine_defaults = {**defaults, "endpoint": exclude_keys(default_endpoint, {"incremental"})}
+ return {**manifest, "resource_defaults": engine_defaults}, default_endpoint["incremental"]
+
+
+def _with_default_incremental(resource: dict[str, Any], default_incremental: Any) -> dict[str, Any]:
+ """Return ``resource`` with ``default_incremental`` applied when it declares no
+ ``endpoint.incremental`` of its own, as the engine's defaults merge would."""
+ if default_incremental is None:
+ return resource
+ endpoint = resource.get("endpoint")
+ if isinstance(endpoint, str):
+ endpoint = {"path": endpoint}
+ elif endpoint is None:
+ endpoint = {}
+ if not isinstance(endpoint, dict) or "incremental" in endpoint:
+ return resource
+ return {**resource, "endpoint": {**endpoint, "incremental": default_incremental}}
+
+
def _build_resource_graph(
manifest: dict[str, Any],
) -> tuple[Any, dict[str, EndpointResource], dict[str, Optional[list[ResolvedParam]]]]:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_custom_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_custom_source.py
index ebb48a6ef77b..816195760f32 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_custom_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_custom_source.py
@@ -1763,18 +1763,21 @@ class TestCustomSourceSourceForPipeline(SimpleTestCase):
threaded_config = mock_resources.call_args.args[0]
assert threaded_config["client"]["paginator"] == paginator_config
+ @parameterized.expand([("on_resource",), ("in_resource_defaults",)])
@patch("products.warehouse_sources.backend.temporal.data_imports.sources.custom.source.rest_api_resources")
- def test_cursor_type_stripped_before_rest_engine(self, mock_resources):
+ def test_cursor_type_stripped_before_rest_engine(self, location, mock_resources):
# cursor_type informs schema field typing but is not a valid kwarg for the
# engine's Incremental(**config) — it must be removed before the manifest
- # reaches the REST engine, while the other incremental keys survive.
+ # reaches the REST engine, while the other incremental keys survive. The engine
+ # merges resource_defaults into each resource itself, so the defaults must not
+ # carry it either.
mock_resources.return_value = [_fake_resource("users")]
manifest = _minimal_manifest()
- manifest["resources"][0]["endpoint"]["incremental"] = {
- "cursor_path": "updated_at",
- "start_param": "since",
- "cursor_type": "integer",
- }
+ incremental = {"cursor_path": "updated_at", "start_param": "since", "cursor_type": "integer"}
+ if location == "on_resource":
+ manifest["resources"][0]["endpoint"]["incremental"] = incremental
+ else:
+ manifest["resource_defaults"] = {"endpoint": {"incremental": incremental}}
source = CustomSource()
config = CustomSourceConfig(manifest_json=json.dumps(manifest), auth_token="abc")
@@ -1787,8 +1790,12 @@ class TestCustomSourceSourceForPipeline(SimpleTestCase):
)
source.source_for_pipeline(config, inputs)
- threaded_incremental = mock_resources.call_args.args[0]["resources"][0]["endpoint"]["incremental"]
- assert threaded_incremental == {"cursor_path": "updated_at", "start_param": "since"}
+ threaded_manifest = mock_resources.call_args.args[0]
+ assert threaded_manifest["resources"][0]["endpoint"]["incremental"] == {
+ "cursor_path": "updated_at",
+ "start_param": "since",
+ }
+ assert "incremental" not in threaded_manifest.get("resource_defaults", {}).get("endpoint", {})
class TestCustomSourceNonRetryableErrors(SimpleTestCase):
@@ -2790,6 +2797,9 @@ class TestCustomSourcePreviewResource(SimpleTestCase):
manifest = _minimal_manifest()
manifest["resources"][0]["endpoint"]["incremental"] = {"cursor_path": "updated_at", "start_param": "since"}
manifest["resources"][0]["endpoint"]["paginator"] = {"type": "offset", "limit": 100}
+ manifest["resource_defaults"] = {
+ "endpoint": {"incremental": {"cursor_path": "id", "start_param": "after", "cursor_type": "integer"}}
+ }
source = CustomSource()
config = CustomSourceConfig(manifest_json=json.dumps(manifest), auth_token="abc")
source.preview_resource(config, team_id=999, resource_name="users")
@@ -2798,6 +2808,7 @@ class TestCustomSourcePreviewResource(SimpleTestCase):
endpoint = engine_manifest["resources"][0]["endpoint"]
assert endpoint["paginator"] == {"type": "single_page"}
assert "incremental" not in endpoint
+ assert "incremental" not in engine_manifest["resource_defaults"]["endpoint"]
assert isinstance(engine_manifest["client"]["session"], _PreviewSession)
assert engine_manifest["client"]["max_retries"] == 1
assert mock_resources.call_args.kwargs["db_incremental_field_last_value"] is None