Skip to content

feat(cdk): Add cursor age validation to StateDelegatingStream#890

Open
devin-ai-integration[bot] wants to merge 53 commits intomainfrom
devin/1770066385-state-delegating-stream-cursor-age-validation
Open

feat(cdk): Add cursor age validation to StateDelegatingStream#890
devin-ai-integration[bot] wants to merge 53 commits intomainfrom
devin/1770066385-state-delegating-stream-cursor-age-validation

Conversation

@devin-ai-integration
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot commented Feb 2, 2026

Summary

Adds an optional api_retention_period field to StateDelegatingStream that validates whether a cursor is within an API's data retention window before using incremental sync. When the cursor is older than the retention period, the connector automatically falls back to full refresh to avoid data loss.

This addresses the issue where APIs like Stripe Events only retain data for 30 days - if a sync fails mid-way and resumes after the retention window, incremental sync would miss data.

Key changes:

  • Added api_retention_period field to StateDelegatingStream schema (ISO8601 duration format: P30D, P1D, PT1H, etc.)
  • Added get_cursor_datetime_from_state to concurrent cursor hierarchy (Cursor base, ConcurrentCursor, ConcurrentPerPartitionCursor, FinalStateCursor)
  • Raises ValueError at model level when IncrementingCountCursor is used with api_retention_period
  • Emits warning log when falling back to full refresh due to stale cursor
  • Clears stream state and emits an empty state message to the platform when falling back to full refresh, so the platform does not retain stale state across sync attempts
  • No breaking changes — the field is optional with no default

Example usage:

type: StateDelegatingStream
name: events
api_retention_period: P30D  # 30 days
full_refresh_stream: ...
incremental_stream: ...

Updates since last revision

Latest changes (fix temporal ordering of state clearing — per Alfredo Garcia (@agarctfi)'s review):

Previously, full_refresh_stream was constructed before clearing state, so its cursor inherited the old stale cursor position — making the "full refresh" fallback start from the stale cursor instead of start_date. This fix restructures create_state_delegating_stream so that:

  1. Only the incremental_stream cursor is used for the retention check (no need to construct full_refresh_stream first)
  2. State is cleared before constructing full_refresh_stream, so its cursor starts from start_date
  3. NO_CURSOR_STATE_KEY is checked directly on the raw state dict — if present, the previous sync was a completed full refresh and the stream uses incremental (not full refresh again)
  4. _is_cursor_older_than_retention_period simplified: removed full_refresh_cursor parameter, only uses incremental_cursor

Previous changes (removed early return for NO_CURSOR_STATE_KEY — per tolik0's feedback):

  • Removed the explicit early return for NO_CURSOR_STATE_KEY in _is_cursor_older_than_retention_period
  • FinalStateCursor.get_cursor_datetime_from_state now handles this case by returning now() for NO_CURSOR_STATE_KEY: True state, which is always within any retention period
  • This simplifies the code by letting the cursor class handle its own state format

Earlier changes (state clearing on fallback to full refresh — per brianjlai):

  • When cursor age validation triggers a fallback to full refresh, the stream's state is now cleared via ConnectorStateManager.update_state_for_stream and an empty state message is emitted through the message repository.

Review & Testing Checklist for Human

  • Verify the temporal ordering fix — Confirm that full_refresh_stream is constructed after clearing state (line 3605-3610 in factory), so its cursor starts from start_date instead of the old stale cursor position. This is the core bug fix.
  • Verify NO_CURSOR_STATE_KEY behavior — When state contains {NO_CURSOR_STATE_KEY: True} (completed full refresh), verify the stream correctly uses incremental sync instead of falling back to full refresh again (test: test_no_cursor_state_key_uses_incremental_not_full_refresh).
  • Verify parent stream edge case — When a StateDelegatingStream is used as a parent stream (not in the configured catalog), verify it does not emit state messages that would cause "Stream not found" errors in the destination (test: test_unconfigured_parent_stream_does_not_emit_state_on_retention_fallback).
  • Verify state-clearing message ordering — The empty state message is emitted during stream creation (in the factory) before the read starts. Verify that the message repository correctly delivers this empty state message to the platform before any record messages.

Recommended test plan:

  1. Apply this change to the Stripe connector's events stream with api_retention_period: P30D, set a cursor state older than 30 days, and verify it falls back to full refresh with the expected warning message and that the entity endpoint is called with created[gte]=<start_date> (not the old cursor)
  2. Test with NO_CURSOR_STATE_KEY: True state (e.g., after a completed full refresh) to verify it correctly uses incremental sync without falling back to full refresh
  3. Test with IncrementingCountCursor to confirm it raises ValueError during discover() with a clear error message

Notes

Fixes: airbytehq/oncall#11103

Link to Devin run: https://app.devin.ai/sessions/782b85a317204e3c833c3ecc3bc02f1e
Previous Devin sessions: https://app.devin.ai/sessions/c6b25a1216c547139ef8242062f7f135, https://app.devin.ai/sessions/443d1d1a2f524eb1bc378aa48f24aa9c, https://app.devin.ai/sessions/5e2b4ff66c50407789e500d6e25e5d5c
Requested by: Alfredo Garcia (@agarctfi)

This adds an optional api_retention_period field to StateDelegatingStream
that validates whether a cursor is within an API's data retention window
before switching from full refresh to incremental sync.

When the cursor value is older than the retention period, the connector
automatically falls back to a full refresh to avoid data loss. This is
useful for APIs like Stripe Events API which only retain data for 30 days.

Key changes:
- Add api_retention_period field to StateDelegatingStream schema (ISO8601 duration)
- Implement cursor age validation in model_to_component_factory.py
- Emit warning log when falling back to full refresh due to stale cursor
- Add unit tests for cursor age validation

Fixes: airbytehq/oncall#11103
Co-Authored-By: unknown <>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@github-actions
Copy link

github-actions bot commented Feb 3, 2026

PyTest Results (Fast)

3 882 tests  +27   3 870 ✅ +27   7m 5s ⏱️ +38s
    1 suites ± 0      12 💤 ± 0 
    1 files   ± 0       0 ❌ ± 0 

Results for commit 5c6c88b. ± Comparison against base commit 15542de.

♻️ This comment has been updated with latest results.

@github-actions
Copy link

github-actions bot commented Feb 3, 2026

PyTest Results (Full)

3 885 tests  +27   3 873 ✅ +27   11m 27s ⏱️ +26s
    1 suites ± 0      12 💤 ± 0 
    1 files   ± 0       0 ❌ ± 0 

Results for commit 5c6c88b. ± Comparison against base commit 15542de.

♻️ This comment has been updated with latest results.

@agarctfi
Copy link
Contributor

Alfredo Garcia (agarctfi) commented Feb 3, 2026

/autofix

Auto-Fix Job Info

This job attempts to auto-fix any linting or formating issues. If any fixes are made,
those changes will be automatically committed and pushed back to the PR.

Note: This job can only be run by maintainers. On PRs from forks, this command requires
that the PR author has enabled the Allow edits from maintainers option.

PR auto-fix job started... Check job output.

✅ Changes applied successfully.

octavia-squidington-iii and others added 2 commits February 3, 2026 16:03
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
@agarctfi Alfredo Garcia (agarctfi) marked this pull request as ready for review February 3, 2026 17:34
Copilot AI review requested due to automatic review settings February 3, 2026 17:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds cursor age validation to StateDelegatingStream to automatically fall back to full refresh when a cursor is older than an API's data retention period. This prevents data loss scenarios where APIs (like Stripe Events) only retain data for a limited time window (e.g., 30 days), and a sync resumes after that window has passed.

Changes:

  • Added optional api_retention_period field to StateDelegatingStream schema (ISO8601 duration format)
  • Implemented cursor age validation logic that compares cursor datetime against retention cutoff
  • Added warning log emission when falling back to full refresh due to stale cursor

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
airbyte_cdk/sources/declarative/declarative_component_schema.yaml Added api_retention_period field definition with ISO8601 duration examples
airbyte_cdk/sources/declarative/models/declarative_component_schema.py Auto-generated model updates reflecting the new field in StateDelegatingStream
airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py Core implementation of cursor age validation with three new helper methods
unit_tests/sources/declarative/test_state_delegating_stream.py Added three test cases covering cursor age validation scenarios (too old, within retention, edge case)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Fix YAML bullet point indentation for consistency (Comment 8)
- Add type guard for cursor_value to handle unexpected types (Comment 9)
- Add test for warning log emission when cursor is too old (Comment 10)

Co-Authored-By: unknown <>
devin-ai-integration bot and others added 3 commits February 3, 2026 18:24
Co-Authored-By: unknown <>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
…_model

Addresses review comment from tolik0 - the incremental_sync check is now
performed in the calling method before invoking _is_cursor_older_than_retention_period.

Co-Authored-By: unknown <>
Addresses review comment from tolik0 - if the cursor value or format is
incorrect, we should use full_refresh_stream instead, as it indicates
that the stream_state is unusable.

Co-Authored-By: unknown <>
Address tolik0's review comment: During the first sync, the state will be
produced by full_refresh_stream, and during subsequent syncs, by
incremental_stream. We need to correctly parse the state for both cases.

Changes:
- Extract incremental_sync from both full_refresh_stream and incremental_stream
- Update _is_cursor_older_than_retention_period to accept list of sources
- Update _parse_cursor_datetime to collect and try formats from all sources

Co-Authored-By: unknown <>
Returns False if the cursor is within the retention period (safe to use incremental).
"""
# NO_CURSOR_STATE_KEY indicates a completed full refresh - cursor is "current"
if stream_state.get(NO_CURSOR_STATE_KEY):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this check, since it is already covered by the final cursor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! Yes, this check is still necessary because in practice, full_refresh_stream.cursor is a DatetimeBasedCursor (created from the manifest's full_refresh_stream definition), not a FinalStateCursor.

When we call full_refresh_cursor.get_cursor_datetime_from_state(stream_state) with state {NO_CURSOR_STATE_KEY: True}:

  • DatetimeBasedCursor tries to extract the cursor field from the state, but since the state only has NO_CURSOR_STATE_KEY, it returns None
  • Then incremental_cursor.get_cursor_datetime_from_state() (also DatetimeBasedCursor) returns None for the same reason
  • If both cursors return None, we fall back to full refresh (return True), which is incorrect for NO_CURSOR_STATE_KEY state

The FinalStateCursor.get_cursor_datetime_from_state implementation exists for completeness in the concurrent cursor hierarchy, but it's not used in this specific flow because the declarative stream factory creates DatetimeBasedCursor instances, not FinalStateCursor instances.

If you'd like to remove this early return, we'd need to ensure one of the cursors can correctly parse NO_CURSOR_STATE_KEY state. One option would be to add NO_CURSOR_STATE_KEY handling to DatetimeBasedCursor.get_cursor_datetime_from_state, but that feels like it's mixing concerns.

Would you prefer to:

  1. Keep the early return as-is (current approach)
  2. Move the NO_CURSOR_STATE_KEY handling into DatetimeBasedCursor
  3. Some other approach?

Devin session

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if the state is NO_CURSOR_STATE_KEY: True - it is produced by the finalcursor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right - the {NO_CURSOR_STATE_KEY: True} state is produced by FinalStateCursor.

The issue is that in the current flow, full_refresh_stream.cursor is created from the manifest's full_refresh_stream definition, which uses DatetimeBasedCursor (not FinalStateCursor). So when we call full_refresh_cursor.get_cursor_datetime_from_state(), we're calling DatetimeBasedCursor's method, not FinalStateCursor's method.

Would you like me to:

  1. Create a FinalStateCursor instance specifically for checking NO_CURSOR_STATE_KEY state, then call its get_cursor_datetime_from_state method (which returns now() and would correctly indicate the cursor is "current")?
  2. Or change the flow to use FinalStateCursor as the first cursor to check instead of full_refresh_stream.cursor?

Either approach would let us remove the early return and rely on FinalStateCursor.get_cursor_datetime_from_state to handle NO_CURSOR_STATE_KEY state.


Devin session

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is the finalcursor created to be used during the sync?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FinalStateCursor is created in two places:

  1. In create_simple_retriever (line 3301-3302): When no cursor is provided to the retriever (i.e., the stream doesn't have an incremental_sync defined):

    if cursor is None:
        cursor = FinalStateCursor(name, None, self._message_repository)
  2. In _build_concurrent_cursor (line 2289): When there's no incremental_sync defined for concurrent streams.

In the context of StateDelegatingStream:

When we call self._create_component_from_model(model.full_refresh_stream, ...), it creates a DefaultStream. The DefaultStream.cursor property returns the cursor from its retriever.

  • If full_refresh_stream doesn't have an incremental_sync defined → the retriever gets a FinalStateCursor → state produced is {NO_CURSOR_STATE_KEY: True}
  • If full_refresh_stream does have an incremental_sync defined → the retriever gets a DatetimeBasedCursor → state produced is {"cursor_field": "value"}

The early return for NO_CURSOR_STATE_KEY handles the first case where the full_refresh_stream used FinalStateCursor and produced {NO_CURSOR_STATE_KEY: True} state. In this case, full_refresh_stream.cursor (which is now a DatetimeBasedCursor if the incremental_stream has one) can't parse that state, so we need the explicit check.

Would you like me to change the approach to explicitly create a FinalStateCursor instance and use it to check NO_CURSOR_STATE_KEY state instead of the early return?


Devin session

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, finalcursor will be created if the stream does not define incremental sync at all. Why do we need to create it explicitly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right - if the full_refresh_stream doesn't define incremental_sync, then full_refresh_stream.cursor is already a FinalStateCursor, and it will handle NO_CURSOR_STATE_KEY correctly by returning now().

The early return is a defensive check for an edge case where:

  1. The state {NO_CURSOR_STATE_KEY: True} was produced by a previous sync (when full_refresh_stream used FinalStateCursor)
  2. But the manifest was later updated to add incremental_sync to full_refresh_stream
  3. Now full_refresh_stream.cursor is a DatetimeBasedCursor which can't parse NO_CURSOR_STATE_KEY state

If you think this edge case is unlikely and we should remove the early return to simplify the code, I can do that. The code would rely on FinalStateCursor.get_cursor_datetime_from_state to handle NO_CURSOR_STATE_KEY state, which works correctly when full_refresh_stream doesn't have incremental_sync.

Should I remove the early return?


Devin session

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Removed the early return for NO_CURSOR_STATE_KEY in commit 1dcc8ab. The FinalStateCursor.get_cursor_datetime_from_state method now handles this case by returning now(), which is always within any retention period.


Devin session

…quest

FinalStateCursor.get_cursor_datetime_from_state now handles NO_CURSOR_STATE_KEY
by returning now(), so the explicit early return is no longer needed.

Co-Authored-By: unknown <>
…havior

The test expected FinalStateCursor.get_cursor_datetime_from_state to be
called when state has NO_CURSOR_STATE_KEY, but both streams get
ConcurrentCursor instances which cannot parse that state format. Both
return None, causing the implementation to correctly fall back to full
refresh as the safe default. Updated the test to mock the full refresh
URL and reflect this behavior.

Co-Authored-By: unknown <>
Copy link
Contributor

@brianjlai Brian Lai (brianjlai) left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When cursor age validation detects a stale cursor on a StateDelegatingStream,
it clears the stream state and emits an empty state message. However, if the
stream is not in the user's configured catalog (e.g. a parent stream created
as a dependency), the destination does not know about it and crashes with
'Stream not found'.

This fix checks whether the stream is in the configured catalog before emitting
the state-clearing message. If no catalog is provided (e.g. during discover),
state is emitted as before for backward compatibility.

Co-Authored-By: unknown <>
…unconfigured streams

Instead of running cursor age validation and then suppressing just the state
message, skip the entire api_retention_period block for streams not in the
configured catalog. This avoids unnecessary work (creating both stream
components, comparing cursor age) for parent-stream dependencies that the
destination doesn't know about.

Co-Authored-By: unknown <>
…sor fallback

When cursor age validation detects a stale cursor, the state must be
cleared before constructing the full_refresh_stream so its cursor starts
from start_date instead of inheriting the old stale cursor position.

Changes:
- Reorder create_state_delegating_stream to clear state before stream construction
- Simplify _is_cursor_older_than_retention_period to only use incremental_cursor
- Handle NO_CURSOR_STATE_KEY directly on raw state dict (completed full refresh = current)
- Update test to verify NO_CURSOR_STATE_KEY uses incremental (not full refresh again)

Co-Authored-By: alfredo.garcia@airbyte.io <freddy.garcia7.fg@gmail.com>
devin-ai-integration bot added a commit to airbytehq/airbyte that referenced this pull request Mar 2, 2026
Bumps the source-declarative-manifest base image to pick up the
StateDelegatingStream cursor age validation fix from
airbytehq/airbyte-python-cdk#890.

Co-Authored-By: alfredo.garcia@airbyte.io <freddy.garcia7.fg@gmail.com>
devin-ai-integration bot added a commit to airbytehq/airbyte that referenced this pull request Mar 3, 2026
Bumps the source-declarative-manifest base image to pick up the
StateDelegatingStream cursor age validation fix from
airbytehq/airbyte-python-cdk#890. Bumps dockerImageTag to 5.15.21-rc.3.

Co-Authored-By: alfredo.garcia@airbyte.io <freddy.garcia7.fg@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants