Skip to content

feat(search): Backfill the event search index from the heartbeat - #3755

Merged
BYK merged 4 commits into
mainfrom
feat/event-search-backfill
Nov 19, 2026
Merged

BYK merged 4 commits into
mainfrom
feat/event-search-backfill

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

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 query found 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:

  • It always stores lexemes. It stores vectors only when eventEmbeddings is enabled.
  • Each run stops at 10 s, 1,000 lexeme rows and 256 vectors.
  • A run that leaves no backlog marks the index done for 10 minutes. Until then, a heartbeat makes one state request instead of a scan.
  • Runs are idempotent: stored rows are skipped. The backfill and the per-Turn task share one lock per Conversation, so they do not embed the same events twice.
  • The backlog query does not parse event payloads. It treats an event with empty stored lexemes as having no text to embed.

Telemetry: the span event_search.backfill carries 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]

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]>
@sentry-junior
sentry-junior Bot requested a review from BYK November 19, 2026 09:56
@vercel

vercel Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 19, 2026 10:17am UTC

Request Review

@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 19, 2026

@cursor cursor 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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.
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

9 screenshot changes — 9 changed · 0 added · 0 removed

Review screenshots in Frameshift

Automation Run Details · Desktop
Automation Run Details · Desktop
Changed
Automation Skill Editor · Desktop
Automation Skill Editor · Desktop
Changed
Conversation Header Auto Archive · Mobile
Conversation Header Auto Archive · Mobile
Changed
Conversation Question · Desktop
Conversation Question · Desktop
Changed
Conversation Question Answered · Desktop
Conversation Question Answered · Desktop
Changed
Conversation Question Light · Desktop
Conversation Question Light · Desktop
Changed
Show 3 more changed screenshots
Inbox Row Answer · Desktop
Inbox Row Answer · Desktop
Changed
Inbox Row Answer Light · Desktop
Inbox Row Answer Light · Desktop
Changed
System Cache Warming · Desktop
System Cache Warming · Desktop
Changed

The money unit check reads budget_exceeded as a money name.

@BYK BYK left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we can clean this code up after the next deploy, right? Let's track that work in an issue assigned to me.

@BYK
BYK merged commit d1eb5ae into main Nov 19, 2026
51 checks passed
@BYK
BYK deleted the feat/event-search-backfill branch November 19, 2026 12:00
@sentry-junior

sentry-junior Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

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:

  • A Conversation that was idle for more than 7 days and becomes active again. Without the backfill, the per-Turn task reaches its older history only slowly, newest first.
  • A single Turn that adds more than the per-run caps of 256 vectors and 2,000 lexemes.

The issue says when to decide: after the deploy, once app.event_search.backfill.outcome = completed and the backlog counts stay near 0. If those two gaps show up in the counts, keeping a smaller backfill that runs less often may beat removing it.

BYK added a commit that referenced this pull request Nov 21, 2026
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]>

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 4a16cf93 Deployed Nov 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant