Implement the following plan: Real-Time Turbopuffer Indexing via Cloudflare Queue…
- User
Implement the following plan:
Real-Time Turbopuffer Indexing via Cloudflare Queue
Context
Checkpoints are indexed in Turbopuffer for search. Currently indexing only happens via the backfill script. When new checkpoints arrive via GitHub webhooks, they need to be indexed in Turbopuffer in real-time. The indexing must be async and non-blocking to the webhook processing pipeline, using Cloudflare Queues (already set up for other flows).
Approach
Add a new
SEARCH_INDEX_QUEUECloudflare Queue. After checkpoint data is upserted into PlanetScale (in bothprocessSessionsPushEventandprocessCommitPushEvent), send a lightweight message to the queue. A new queue consumer fetches checkpoint data from PlanetScale, embeds search text via OpenAI, and upserts vectors to Turbopuffer.Files to Modify
File Action Description api/src/lib/search-index-queue.tsCreate Message type, type guard, batch processor api/src/lib/turbopuffer.tsModify Add indexCheckpoints()function (embed + upsert)api/src/env.tsModify Add optional SEARCH_INDEX_QUEUE?: Queueapi/wrangler.jsoncModify Add queue producer + consumer (staging & production) api/src/index.tsModify Add queue consumer branch for search index messages api/src/lib/webhook/processing.tsModify Enqueue after upsert in both push event handlers Implementation Details
1. Search Index Queue (
api/src/lib/search-index-queue.ts)Follow existing
RepoSyncMessage/UserRepoSyncMessagepattern fromrepo-sync-queue.tsanduser-repo-sync.ts.typescript Assistant
I'll start by reading the key existing files to understand the patterns, then implement all changes.
Assistant
Now let me read the actual files I need to work with directly.
Assistant
Now let me read the critical sections of the webhook processing file and find the backfill script.
Assistant
Now I have all the context I need. Let me also check the DB checkpoints module for the
getByIdsmethod or similar.Assistant
Now I have everything I need. Let me implement all the changes.
Assistant
Now let me implement all changes. Starting with the simpler modifications in parallel:
Assistant
Now add
indexCheckpoints()to turbopuffer.ts:Assistant
Now add the
indexCheckpointsfunction at the end of the file:Assistant
Now update env.ts, wrangler.jsonc, index.ts, and processing.ts:
Assistant
Now update wrangler.jsonc — staging producers:
Assistant
Now the production section:
Assistant
Now update index.ts — add imports and queue consumer branch:
Assistant
Now update the queue handler type and add the branch:
Assistant
Now update processing.ts — add import and enqueue calls:
Assistant
Now add the enqueue call after the session checkpoint upsert (after line 965):
Assistant
Now add the enqueue call after the commit checkpoint upsert:
Assistant
All changes implemented. Let me verify the files look correct:
Assistant
All files look correct. Let me do a quick check on the TypeScript compilation to make sure there are no issues:
Assistant
One type error with the TextDecoder constructor. Let me fix it:
Assistant
Clean build, no errors. Here's a summary of all changes:
Changes Made
New file
api/src/lib/search-index-queue.ts— Message type (SearchIndexMessage), type guard (isSearchIndexMessage), and batch processor (processSearchIndexBatch) following the same pattern asrepo-sync-queue.ts
Modified files
-
api/src/lib/turbopuffer.ts— Added:truncateToBytes(),buildSearchText(),hashToNumericId()helpersembedTexts()for batch embedding (alongside existingembedQuery()for single texts)indexCheckpoints()— builds search text, embeds via OpenAI, upserts to Turbopuffer v2 columnar API with deterministic FNV-1a hash IDs
-
— Added optional binding
- User
can we locally test this follow with logging to see how it's working?
Assistant
Cloudflare Queues do work locally via Miniflare, but the easiest way to test the core flow is a small script that hits the local DB, builds search text, embeds, and upserts — with verbose logging at each step. Let me check what data is available locally first.
Assistant
MySQL is running. Let me try the right way to connect:
Assistant
Good, there's data. Let me grab a few checkpoints to test with:
Assistant
We have good test data. Now let me create the test script:
Assistant
Good — both
TURBOPUFFER_API_KEYandOPENAI_API_KEYare configured. Let me create a test script:Assistant
Now let's run it — first with
--dry-runto see the data, then for real:Assistant
Data looks good. Now let's run it for real:
Assistant
The Turbopuffer v2 endpoint returned 404. Let me check what API version the existing backfill uses and fix it:
Assistant
The v1 endpoint works with the
upsertsarray format. The v2 columnar endpoint doesn't exist. Let me fixturbopuffer.tsto use the v1 row-based format:Assistant
Also update the test script to match:
Assistant
Now let's run the full test:
Assistant
That error is from the 1-dimensional test vector I created with curl earlier. Let me delete it and retry:
Assistant
There's a leftover reference to
idsin the upsert success logging. Let me fix that:Assistant
Everything works end-to-end. Here's what the test confirms:
- DB fetch - 3 checkpoints loaded from local MySQL with prompt, commit, files data
- Search text - Built
Prompt: ...\nCommit: ...\nFiles: ...format matching the backfill script - OpenAI embedding - 3 texts embedded in 1.2s (178 tokens)
- Turbopuffer upsert - 3 vectors written in 492ms, all 3 rows affected
- Verification search - Queried back with the first checkpoint's text, got it as the top result (dist=0.0598, very close match), with the other two further away (dist=~0.64)
Let me also verify the TypeScript still compiles cleanly after the v1 API fix:
Assistant
Clean. The full pipeline is working — DB fetch, OpenAI embedding, Turbopuffer upsert, and search verification all pass. The test script is at
api/scripts/test-search-index.tsand can be run with:bash# Dry-run (shows data without API calls) npx tsx scripts/test-search-index.ts --dry-run --limit 5 # Full test (embed + upsert + verify) npx tsx scripts/test-search-index.ts --limit 5 # Specific repo npx tsx scripts/test-search-index.ts --repo entireio/cli --limit 10- User
okay let's commit and push this
Assistant
Committed and pushed to
tp_poc. Commitaa62e0d45with 7 files changed (782 insertions).- User
okay next let's enable the full text search as well that turbopuffer supports
Assistant
Let me read the search endpoint to understand the current API surface.
Assistant
Now let me also check how the frontend calls the search endpoint to understand what query params we have to work with.
Assistant
Good — the frontend just sends
qand optionalbranch. Now I have everything I need to write the plan.- User
[Request interrupted by user for tool use]