perf(embed): cursor-paginated stale loading + rate-limit backoff + partial index#987
Closed
garrytan-agents wants to merge 1 commit into
Closed
perf(embed): cursor-paginated stale loading + rate-limit backoff + partial index#987garrytan-agents wants to merge 1 commit into
garrytan-agents wants to merge 1 commit into
Conversation
…rtial index Three fixes for embed --stale on large brains (300K+ chunks): ## 1. Cursor-paginated listStaleChunks (embed timeout fix) The previous implementation pulled ALL stale rows (up to 100K) in one query. On a 373K-row content_chunks table with 48K stale rows, this query took >2 min and hit Supabase's 2-min statement_timeout, causing embed --stale to silently fail with zero progress. Fix: keyset pagination on (page_id, chunk_index) with a default batch size of 2000 rows. Each query finishes in <1s. The embedAllStale loop pages through batches, embeds each batch, then advances the cursor. ## 2. Rate-limit-aware retry (429 backoff) The OpenAI SDK's built-in retry has a ~4s max backoff window, which is too short for TPM (tokens-per-minute) limits on large pages (~90K tokens). The embed loop would fail after 3 SDK retries and skip the page entirely. Fix: embedBatchWithBackoff wrapper parses the retry delay from the 429 error message (e.g. 'try again in 248ms') and sleeps for that duration + 500ms padding. Up to 5 retries with parsed delays (60s fallback when unparseable). ## 3. Migration v58: partial index for NULL embeddings `CREATE INDEX idx_chunks_embedding_null ON content_chunks (page_id, chunk_index) WHERE embedding IS NULL` — makes countStaleChunks() and the paginated listStaleChunks() instant instead of full-table-scanning 373K rows. ## Testing Verified on a 99K-page / 373K-chunk brain with 48K stale chunks. Before: embed --stale hung for 2+ min then timed out (0 progress). After: loads 2K rows in <1s, embeds concurrently, pages through all stale chunks without timeout.
Owner
|
Superseded by work on |
garrytan
added a commit
that referenced
this pull request
May 15, 2026
Resolves migration version collision: master shipped v58 (edges_backfilled_at_v0_33_3 — v0.33.3 W0c symbol-resolution backfill watermark) ahead of this branch. The embed-perf cherry-pick from PR #987 also claimed v58 for its idx_chunks_embedding_null partial index; renumbered to v59 since master landed first. Both migrations coexist unchanged at their new slots. No other conflicts. Typecheck clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Three fixes for
embed --staleon large brains (300K+ chunks, 48K stale):1. Cursor-paginated
listStaleChunksPrevious: one query pulling ALL stale rows (LIMIT 100000). On 373K-row
content_chunkswith 48K stale, this took >2 min and hit Supabase'sstatement_timeout.Fix: keyset pagination on
(page_id, chunk_index), 2000 rows per batch. Each query finishes in <1s.2. Rate-limit-aware retry (
embedBatchWithBackoff)Previous: OpenAI SDK's built-in retry maxes at ~4s backoff — too short for TPM limits on large pages (~90K tokens). Pages silently skipped after 3 failed attempts.
Fix: wrapper parses retry delay from 429 message (e.g.
try again in 248ms), sleeps with 500ms padding, up to 5 retries. 60s conservative fallback.3. Migration v58: partial index
Makes
countStaleChunks()and paginatedlistStaleChunks()instant.Testing
Verified on 99K-page / 373K-chunk brain with 48K stale chunks:
embed --stalehung 2+ min then timed out (0 progress)Files changed
src/commands/embed.ts— paginatedembedAllStale+embedBatchWithBackoffsrc/core/engine.ts— updatedlistStaleChunksinterface with cursor paramssrc/core/postgres-engine.ts— keyset-paginated querysrc/core/pglite-engine.ts— matching PGLite implementationsrc/core/types.ts— addedpage_idtoStaleChunkRowsrc/core/migrate.ts— v58 partial index migration