Repository navigation
feat(search): Backfill the event search index from the heartbeat - #3755
Conversation
The task after each completed Turn indexes at most the newest 256 events without vectors and 2,000 without lexemes. History from before the index, the events of a running Turn, and the rest of a long Conversation wait for later Turns, so most production searches had no vectors to compare. A heartbeat phase now stores missing lexemes, and missing vectors when event embeddings are on, in Conversations with activity in the last week, most recent first. A run stops at 10 s, 1,000 lexeme rows, and 256 vectors, and shares the per-Conversation lock with the task. Co-Authored-By: BYK <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit a165f28. Configure here.
The heartbeat backfill shares the per-Conversation lock. When it held the lock, the task skipped, and the Turn's events waited until the backfill ran again after marking the index done. The task now waits up to 30 s, longer than a backfill run's budget.
|
9 screenshot changes — 9 changed · 0 added · 0 removed Review screenshots in Frameshift |
The money unit check reads budget_exceeded as a money name.
BYK
left a comment
There was a problem hiding this comment.
I think we can clean this code up after the next deploy, right? Let's track that work in an issue assigned to me.
|
Mostly, yes. The bulk of the work is one-time history from before the index existed, so once the backlog drains most of this can go. I'm tracking it in #3762, assigned to you. Before removing it all, check two gaps that keep recurring:
The issue says when to decide: after the deploy, once |
The heartbeat backfill from #3755 stores nothing in production and loads the database. **Production, 2026-11-21 18:20–19:07 UTC, `sentry/junior` spans:** - 28 runs, every one `limit_reached`, with 0 lexemes and 0 vectors stored. - The backlog scan (`count(*) filter … GROUP BY … HAVING` over every event of the Conversations active in the last week) took p50 141 s and max 256 s. The 10 s budget started before the scan, so each run passed its deadline before the first Conversation. - The 60 s claim expired before the scan ended. A new heartbeat then started another scan, so about 1.5 scans ran at all times. - The reported backlog grew from 39,710 to 46,691 events without lexemes, and from 41,203 to 47,905 without a vector. **What changes:** - **Scan:** a run walks the Conversations with activity in the last week, newest first, 50 at a time from `junior_conversations_last_activity_idx`. A cursor in state lets each run continue where the last one stopped. For each Conversation, the run stores the missing lexemes with the existing per-Conversation anti-join, limited to the room left in the run. Then it checks for one event that has lexemes but no vector of the model, on the primary keys (`NOT EXISTS … LIMIT 1`). It embeds only when it finds one. The global count is gone. - **Statement timeout:** the page read and the per-Conversation queries run in a transaction with `statement_timeout` at 5 s. A Conversation whose query times out is skipped, and the pass goes on. The embedding call runs outside the transaction, so it holds no connection while it waits for the model. - **Claim:** a run holds its claim until it ends and deletes it in `finally`. The TTL, now 5 minutes, only frees the claim of a run that died. The 10 s budget starts after the claim and covers the page reads too. - **Outcome:** `completed` now means a whole pass stored and skipped nothing. `limit_reached` means the run stopped at its budget with Conversations left in the pass. `pass_ended` means a pass reached the end but stored or skipped rows, so the next run starts a new pass. Only `completed` sets the 10-minute done marker. **Telemetry:** the `backlog.*` attributes are gone, because a bounded run cannot count the whole backlog. New attributes on `event_search.backfill`: `conversation.count`, `skipped.count`, `pass.conversation.count` and `pass.event.count`. `lexeme.count`, `vector.count` and `outcome` stay. The TELEMETRY.md row already covers `app.event_search.backfill.*`. **Query plans:** local Postgres 17 seeded with 50,800 Conversations (600 active), 403k events, 356k lexeme rows and 198k vectors: | Query | Plan | Time | |---|---|---| | Page of active Conversations, after a cursor | Bitmap Index Scan on `junior_conversations_last_activity_idx`, then a top-N sort of the 563 active rows | 1.1 ms | | Missing-vector probe, 4,000-event Conversation | Bitmap Index Scan on `junior_conversation_event_lexemes_pk`, Nested Loop Anti Join on `junior_conversation_event_embeddings_pk` | 0.4 ms | | Missing-vector probe, fully embedded Conversation (no match) | Index Scan on `junior_conversation_event_lexemes_pk`, anti join on `junior_conversation_event_embeddings_pk` | 1.0 ms | | Missing lexemes, 4,000-event Conversation (unchanged query) | Bitmap Index Scan on `junior_conversation_events_type_idx`, Nested Loop Anti Join on `junior_conversation_event_lexemes_pk` | 9.5 ms | | Old global backlog scan, for comparison | Parallel Seq Scan on `junior_conversation_events`, two nested-loop left joins, GroupAggregate | 242 ms | On the same data, with a fake embedder, 15 runs (p50 455 ms, max 2.2 s) drained the seeded backlog of about 8,000 rows. The last run, a clean pass over all 600 active Conversations, returned `completed`. **Voyage cost:** unchanged from #3755. A run embeds at most 256 events of at most about 2,000 tokens each. <!-- junior-request-attribution:start --> via @BYK. <!-- junior-request-attribution:end --> <!-- junior-session-footer:start --> <!-- junior-conversation-id:agent%3A3077261f58a20b2c94e5ba91f4b0f3f3 --> -- [View Session](https://junior-prod.sentry.dev/conversations/agent%3A3077261f58a20b2c94e5ba91f4b0f3f3) [[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A3077261f58a20b2c94e5ba91f4b0f3f3/?project=4510944073809921) <!-- junior-session-footer:end --> Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: BYK <[email protected]>

History search matches by meaning only events that have a vector, and reads stored lexemes only for events that have them. The task after each completed Turn indexes a bounded slice: the newest 2,000 events without lexemes and the newest 256 without vectors. Three kinds of event wait for a later Turn: history from before the index, the events of a running Turn, and the rest of a long Conversation. In production, most searches with a
queryfound no vectors to compare (app.event_search.meaning.candidate.count= 0), and some computed lexemes for about 2,000 unindexed events per query.A new heartbeat phase (
chat/conversations/event-search-backfill.ts) indexes that backlog in Conversations with activity in the last week, most recent first:eventEmbeddingsis enabled.Telemetry: the span
event_search.backfillcarries the backlog size at the start of the run, the rows stored and the outcome (app.event_search.backfill.*). TELEMETRY.md has a new row for it.Voyage cost (estimate): voyage-4-lite costs $0.02 per million tokens. An event embeds at most about 2,000 tokens, so one run sends at most about 512,000 tokens, about $0.01. The heartbeat runs each minute, so a drain costs at most about $0.61 per hour. Most events are much shorter: 100,000 backlog events at about 500 tokens each would cost about $1 in total. As with the Brief backfill, the cost stays on the span and is not charged to a Conversation.
Related: #3574 (similarity floor) needs the distance data that this makes possible.
via @BYK.
--
View Session [Sentry]