Repository navigation
[codex] Fix Next Up anchor query - #41
Conversation
📝 WalkthroughWalkthroughThe ChangesNext-up query refactoring with hidden-items filtering
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/catalog/nextup_repo.go (1)
157-207: Validate the query plan and supporting indexes before wider rollout.Dropping the pre-cap means this path now walks the user's full completed history and runs the hidden-items anti-join for each candidate anchor. I'd EXPLAIN this against prod-sized histories and make sure the access path is backed by composite indexes that match the new predicates, especially on
user_watch_progressanduser_history_hidden_items, so the correctness fix doesn't become a latency regression.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/catalog/nextup_repo.go` around lines 157 - 207, The new query removed the pre-cap and now scans the user's full completed history and repeatedly anti-joins to user_history_hidden_items; run EXPLAIN (ANALYZE, BUFFERS) on the completed_episodes CTE and full query against prod-like data to verify plan and hotspots, and if needed add composite indexes that match the predicates used by completed_episodes and the hidden-items anti-join — specifically consider indexes on user_watch_progress covering (user_id, profile_id, completed, updated_at, media_item_id) and on user_history_hidden_items covering (user_id, profile_id, media_item_id, hidden_before) (or same columns in an order matching the planner’s filter usage) to support the EXISTS/NOT EXISTS checks and ordering so this correctness change does not cause a latency regression.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@internal/catalog/nextup_repo.go`:
- Around line 157-207: The new query removed the pre-cap and now scans the
user's full completed history and repeatedly anti-joins to
user_history_hidden_items; run EXPLAIN (ANALYZE, BUFFERS) on the
completed_episodes CTE and full query against prod-like data to verify plan and
hotspots, and if needed add composite indexes that match the predicates used by
completed_episodes and the hidden-items anti-join — specifically consider
indexes on user_watch_progress covering (user_id, profile_id, completed,
updated_at, media_item_id) and on user_history_hidden_items covering (user_id,
profile_id, media_item_id, hidden_before) (or same columns in an order matching
the planner’s filter usage) to support the EXISTS/NOT EXISTS checks and ordering
so this correctness change does not cause a latency regression.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: c253a4b0-f96d-49bb-aa2d-077775003c27
📒 Files selected for processing (2)
internal/catalog/nextup_repo.gointernal/catalog/nextup_repo_test.go
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3a0687bc32
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } | ||
|
|
||
| query, args := buildListNextUpQuery(q, completedIDs, limit) | ||
| query, args := buildListNextUpQuery(q, limit) |
There was a problem hiding this comment.
Keep Next Up anchored to the user store
When userdb.backend=sqlite is configured, watch progress is written through SQLiteProvider into the per-user SQLite store, but this path now skips store.ListProgress and derives completed anchors only from the Postgres user_watch_progress table. In those deployments /Shows/NextUp and the home Next Up section will miss completed anchors (often returning no next-up rows) even though the user's store has completed episodes; the previous code worked because it read completed IDs from the configured UserStore before joining against catalog episodes.
Useful? React with 👍 / 👎.
| %s | ||
| %s | ||
| ORDER BY e.series_id, uwp.updated_at DESC | ||
| ORDER BY e.series_id, uwp.updated_at DESC, e.season_number DESC, e.episode_number DESC |
There was a problem hiding this comment.
Bound the completed-anchor scan for large histories
After removing the completedIDs prefilter, this CTE now has to read every completed progress row for the profile, join each row to episodes, and sort by series before the final LIMIT can apply; the checked progress indexes only cover (user_id, profile_id, updated_at DESC), so they do not support this distinct-by-series ordering. On the large imported histories this change targets, every home/Next Up request can become an unbounded scan/sort of the user's full completed history; add a purpose-built indexed/materialized anchor path or otherwise bound the work while still avoiding the old 100-row cap.
Useful? React with 👍 / 👎.
Summary
Root Cause
Next Up previously loaded only the 100 most recent completed progress rows before grouping by series. Large imported histories could have those rows dominated by fully-watched series, causing the section to return no candidates even when older completed series had valid next episodes.
Validation
go test ./internal/cataloggo test ./internal/sectionsmake dev-deploy;/api/v1/readyreturned{"status":"ok"}.AI Use
Implemented with Codex.
Summary by CodeRabbit
Release Notes