Skip to content

Fix activities missing when sleep dbt build times out - #1315

Merged
Asherlc merged 6 commits into
mainfrom
Asherlc/missing-from-activities
Jun 19, 2026
Merged

Asherlc merged 6 commits into
mainfrom
Asherlc/missing-from-activities

Conversation

@Asherlc

@Asherlc Asherlc commented Jun 18, 2026 •

Copy link
Copy Markdown
Owner

Summary

Production /activities was stale because sleep_heart_rate_sample microbatch runs timed out (121 daily batches with a 120-day lookback), failing the entire dbt build and blocking deduped_activities from refreshing. This splits analytics-worker dbt into activity and sleep/dashboard phases so activity read models keep updating even when sleep builds fail. It also rewrites sleep_heart_rate_sample from microbatch to dirty-key incremental updates, scoping work to changed sleeps, sensors, and overlapping activities instead of reprocessing the full lookback every run. Tests were updated for the split build and dirty-key sleep model pattern.

Test plan

  • Verify read_model_microbatch.sql.test.ts passes locally
  • After deploy, confirm deduped_activities refreshes even if sleep models fail
  • Confirm missing activity 550063f5-bcd1-4c63-88bf-c4533b62b9c5 appears on /activities
  • Monitor first sleep_heart_rate_sample run after deploy (120-day bootstrap may be heavy, then incremental runs should be fast)

Made with Cursor


Summary by cubic

Splits analytics builds into activity and sleep/dashboard phases and rewrites sleep_heart_rate_sample to an append-based dirty-key incremental model with ReplacingMergeTree(refresh_version). This avoids microbatch timeouts, keeps /activities fresh, and fixes the table’s ClickHouse sort/order stability.

  • Bug Fixes

    • Activity read models keep refreshing even if sleep builds fail by running the activity phase first.
    • Stabilized timestamps in sleep_heart_rate_sample using a refresh_clock and source_refreshed_at to avoid nondeterminism.
    • Ensured non-null key fields with assumeNotNull for sleep_id, user_id, recorded_at, and recorded_date.
    • Fixed activity visibility unit guard to prevent incorrect hiding/showing of activities.
    • Corrected ClickHouse sort key for sleep_heart_rate_sample to improve merges and determinism.
  • Refactors

    • Split analytics-worker builds in entrypoint.sh into DBT_ACTIVITY_MODELS and DBT_SLEEP_DASHBOARD_MODELS, combined into DBT_SAFE_MODELS and executed via run_dbt_safe_builds.
    • Replaced sleep_heart_rate_sample microbatch with append + ReplacingMergeTree(refresh_version) and dirty-key increments from changed sleeps (_peerdb_synced_at), heart rate updates (refreshed_at), overlapping activities, and deleted/absent records; emit tombstones for stale samples and include an initial_lookback_days bootstrap.

Written for commit 3a38c18. Summary will update on new commits.

Review in cubic

Summary by CodeRabbit

  • Bug Fixes

    • Improved data freshness and consistency for sleep-related metrics with updated incremental processing logic and stale data handling.
  • Chores

    • Updated backend orchestration to optimize build sequencing for improved performance and reliability.
  • Tests

    • Enhanced test coverage to validate incremental data processing and model behavior.

Split dbt into activity and sleep/dashboard phases so deduped_activities keeps refreshing, and replace sleep_heart_rate_sample microbatch with dirty-key incremental updates.

Co-authored-by: Cursor <cursoragent@cursor.com>
Copilot AI review requested due to automatic review settings June 18, 2026 23:54
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry @Asherlc, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@coderabbitai

coderabbitai Bot commented Jun 18, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 7b01db5e-a433-4434-9173-8ad3702cd8ef

📥 Commits

Reviewing files that changed from the base of the PR and between 313fd36 and 3a38c18.

📒 Files selected for processing (3)
  • analytics/models/read_models/read_model_microbatch.sql.test.ts
  • analytics/models/read_models/sleep_heart_rate_sample.sql
  • entrypoint.sh

📝 Walkthrough

Walkthrough

sleep_heart_rate_sample.sql switches from microbatch to append incremental strategy, replacing the prior simple CTE with a dirty-key pipeline that computes affected (user_id, sleep_id) pairs from sleep/sensor/activity changes and emits both current rows and tombstone rows (is_deleted=1). entrypoint.sh adds run_dbt_safe_builds(), splitting safe builds into a sequential activity-then-sleep/dashboard pair. Tests are updated to match both changes.

Changes

sleep_heart_rate_sample rewrite and entrypoint build split

Layer / File(s) Summary
entrypoint.sh: split safe builds into activity-then-sleep/dashboard
entrypoint.sh, analytics/models/read_models/read_model_microbatch.sql.test.ts
Replaces the single DBT_SAFE_MODELS selector with DBT_ACTIVITY_MODELS and DBT_SLEEP_DASHBOARD_MODELS env vars; introduces run_dbt_safe_builds() that runs two sequential dbt build commands; updates sync, analytics, and analytics-worker modes; test asserts ordering, combined selector, and --select args.
sleep_heart_rate_sample: switch to append + incremental state setup
analytics/models/read_models/sleep_heart_rate_sample.sql
incremental_strategy changes from microbatch to append, microbatch params removed; initial_lookback_days variable and target_state CTE added to compute last_refreshed_at/is_empty for gating downstream filters.
sleep_heart_rate_sample: dirty-key pipeline
analytics/models/read_models/sleep_heart_rate_sample.sql
Defines sleep_source, active_sleep, sleep_dirty_keys, sensor_dirty_keys (from changed heart-rate samples), activity_dirty_keys, initial_dirty_keys, and stale_sleep_dirty_keys (left-join to detect sessions needing tombstones).
sleep_heart_rate_sample: current samples, tombstones, and final output
analytics/models/read_models/sleep_heart_rate_sample.sql, analytics/models/read_models/read_model_microbatch.sql.test.ts
Adds sleep_activity_bounds/active_activity exclusion logic, current_samples CTE, existing_samples/stale_sample_tombstones for deletion rows, refresh_clock, and final UNION ALL of is_deleted=0 current rows and is_deleted=1 tombstones; tests assert the append strategy, dirty-key derivation, tombstone presence, and absence of microbatch/lookback patterns.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • Asherlc/dofek#1185: Introduced the microbatch design in sleep_heart_rate_sample.sql that this PR replaces with the append/dirty-key approach.
  • Asherlc/dofek#1183: Modifies entrypoint.sh's dbt safe-model selection, directly overlapping with the DBT_ACTIVITY_MODELS/DBT_SLEEP_DASHBOARD_MODELS split made here.
  • Asherlc/dofek#1190: Changes entrypoint.sh and the read-model microbatch test suite around how dbt safe builds execute per worker, intersecting with run_dbt_safe_builds() ordering added here.

Suggested labels

area/infra, type/bug

Suggested reviewers

  • cubic-dev-ai
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly addresses the main issue (activities missing due to sleep dbt build timeouts) and reflects the core solution (splitting builds to isolate failures).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

github-actions Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Storybook previews for a0f81a3b are ready:

This comment updates automatically on each PR push.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

3 issues found across 3 files

Confidence score: 2/5

  • In entrypoint.sh, the analytics-worker dbt phase commands are not chained, so an early phase failure can be masked and the cycle may report success despite failed activity models; this undermines fail-fast behavior and can hide broken runs in production — chain the phase commands (or explicitly propagate exit codes) before merging.
  • In analytics/models/read_models/sleep_heart_rate_sample.sql, dirty-key logic misses activity removals (deleted/provider-absent transitions), so overlapping sleep windows are not recomputed and stale sleep heart-rate rows can persist until unrelated updates occur — include removal transitions in dirty-key detection before merge.
  • In analytics/models/read_models/sleep_heart_rate_sample.sql, the incremental watermark uses refreshed_at without activity _peerdb_synced_at, so activity-only changes can stall progress and trigger repeated reprocessing of the same sleep keys each run — incorporate the activity sync timestamp into watermark advancement to de-risk merge.

You’re at about 94% of the monthly reviewed-line limit. You may want to disable incremental reviews to conserve quota. Reviews will continue until that limit is exceeded. If you need help avoiding interruptions, please contact contact@cubic.dev.

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread entrypoint.sh Outdated
Comment thread analytics/models/read_models/sleep_heart_rate_sample.sql Outdated
Comment thread analytics/models/read_models/sleep_heart_rate_sample.sql Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

0 issues found across 1 file (changes from recent commits).

You’re at about 95% of the monthly reviewed-line limit. You may want to disable incremental reviews to conserve quota. Reviews will continue until that limit is exceeded. If you need help avoiding interruptions, please contact contact@cubic.dev.

Requires human review: Auto-approval blocked by 3 unresolved issues from previous reviews.

Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

0 issues found across 1 file (changes from recent commits).

You’re at about 95% of the monthly reviewed-line limit. You may want to disable incremental reviews to conserve quota. Reviews will continue until that limit is exceeded. If you need help avoiding interruptions, please contact contact@cubic.dev.

Requires human review: Auto-approval blocked by 2 unresolved issues from previous reviews.

Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

0 issues found across 2 files (changes from recent commits).

You’re at about 95% of the monthly reviewed-line limit. You may want to disable incremental reviews to conserve quota. Reviews will continue until that limit is exceeded. If you need help avoiding interruptions, please contact contact@cubic.dev.

Requires human review: Auto-approval blocked by 2 unresolved issues from previous reviews.

Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

0 issues found across 3 files (changes from recent commits).

You’re at about 95% of the monthly reviewed-line limit. You may want to disable incremental reviews to conserve quota. Reviews will continue until that limit is exceeded. If you need help avoiding interruptions, please contact contact@cubic.dev.

Requires human review: Restructures dbt orchestration, updates sleep_heart_rate_sample to an incremental pattern, and changes production analytics pipelines; may impact data freshness and metrics, so human review is required.

Re-trigger cubic

@Asherlc
Asherlc merged commit 752d377 into main Jun 19, 2026
65 checks passed
@Asherlc
Asherlc deleted the Asherlc/missing-from-activities branch June 19, 2026 01:50
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.

2 participants