Skip to content

ref(embeddings): Share embedding helpers across Conversations, events and memories - #3586

Merged
BYK merged 22 commits into
mainfrom
feat/shared-embedding-helpers
Nov 17, 2026
Merged

BYK merged 22 commits into
mainfrom
feat/shared-embedding-helpers

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Option B of #3582: one copy of the embedding code, and each owner keeps its own table, so every foreign key still deletes vectors with their source rows.

Follows #3564.

New subpath @sentry/junior-plugin-api/embeddings:

  • embeddingVector / embeddingHalfvec pgvector column types. These were in core's db/schema/vector.ts, and memory had its own copy.
  • cosineDistance, which casts to the query size so the HNSW expression index matches, and guardedCosineDistance, which wraps it in a CASE guard for vectors of other sizes.
  • parseEmbeddingBatch and embedText, with the provider/model mismatch check. The Brief path had no checks before.
  • refreshEmbeddings({ listMissing, write, embedder, batchSize, deadlineMs, maxItems }), the page, embed, then write loop.

Users:

  • Brief task: a vector of another model no longer blocks a new one. A vector of a newer Brief is still never replaced.
  • Event task: uses refreshEmbeddings and matches on provider as well as model. ConversationEventEmbedder is removed in favor of PluginEmbedder.
  • Memory heartbeat: uses refreshEmbeddings with the same list query and upsert. The recall SQL text is unchanged.
  • New Conversation embedding backfill, which the core heartbeat runs when Briefs are on. It embeds the latest Brief of each Conversation that has no vector of the configured model, so a model change no longer leaves Conversation search without vectors until each Conversation gets a new Brief. Each run has a 10 s budget, and a finished model is marked done for 1 h.

There is no schema change and no data migration.

Notes:

  • drizzle-orm is a regular dependency of plugin-api, not a peer. With autoInstallPeers: false, a peer dependency left apps/example with a missing-peer error. The helpers live on a subpath, so plugins that do not import them never load drizzle types.
  • A plugin can resolve another copy of drizzle-orm. So the distance helpers type their column parameters as unknown, and memory wraps their result in its own sql template. At run time, drizzle finds columns and SQL from any copy.
  • The three no-core-plugin-* ast-grep rules now allow subpaths of @sentry/junior-plugin-api.
  • The RRF helper from feat(conversations): Search a Conversation's event history by words and meaning #3564 stays as it is.

Refs #3582

via @BYK.

--

View Session [Sentry]

sentry-junior Bot and others added 21 commits November 17, 2026 10:24
…nd meaning

searchConversationEvents takes an optional `query`. It ranks events with
BM25 over Postgres lexemes and, when event embeddings are on, with cosine
distance over stored event vectors, and fuses both rankings with
reciprocal rank fusion. Each match has an excerpt around the best query
word. The compaction notes now point the agent to a query first.

Event vectors live in junior_conversation_event_embeddings. A core task
after each Turn embeds the newest unembedded events with the configured
embeddingModelId. The `eventEmbeddings` app flag is off by default.

The stored message cast that drops Unicode escapes moves out of the cache
warming backfill so that both queries share it.

Co-Authored-By: BYK <[email protected]>
The event search task now stores the lexemes of up to 2,000 of the
newest searchable events per Turn in junior_conversation_event_lexemes,
with a GIN index. Keyword search reads stored lexemes and computes them
only for the newest events that have none yet, so old history and apps
without the task stay searchable.

Term frequencies come from ts_filter over the query lexemes instead of
unnesting every lexeme. Unstored seqs are read first, so a stale row
estimate cannot rerun the subquery once per event.

Co-Authored-By: BYK <[email protected]>
An event with no searchable text gave null lexemes, so the insert
failed and the task retried the same batch on every Turn. Numeric
tool-call arguments are searchable text too.
The history search case now loads 84 build logs over two compactions.
Each log has the same steps with other checksums, and the question names
the build. A second case turns on event embeddings and asks with words
that no event has.

Loaded history can now follow an earlier call, so a test can compact
twice. Keyword search splits a number from a dot or a dash, so 7421
matches build-7421.log.

Co-Authored-By: BYK <[email protected]>
transcript.ts had 17 type assertions in the ratchet baseline. Type
guards and role narrowing replace them, and its baseline entry goes.
Text of only newlines or tabs passed the SQL filter, and the embedder
rejected the batch, so that event blocked embedding on every Turn. The
filter now needs a non-space character, and text that trims to nothing
is skipped. The meaning ranking keeps vectors of another size from the
distance operator.
…he keyword query

Event embeddings are now halfvec with STORAGE MAIN. That halves the
largest per-event table and keeps each vector in its row, so the exact
scan of a 20,000-event Conversation drops from about 67 ms to 18 ms.

The keyword tsquery is now built from immutable functions, so Postgres
folds it to a constant at plan time and can pick the GIN index for rare
words. A subquery hid the query from the planner.
…g case

The summary kept the date, so the agent answered without a search. The
date now sits above a long disk listing in one tool result, and a second
batch of reports forces a second compaction.
…nt-hybrid-search

# Conflicts:
#	packages/junior/migrations/meta/0115_snapshot.json
#	packages/junior/migrations/meta/_journal.json
Set app.event_search.* attributes on the searchConversationEvents span: leg
times, candidate counts, events computed without stored lexemes, and the
nearest cosine distance. The event search task stores lexemes and vectors
at the same time.
Stored lexemes need no provider call, so the event search task now runs
after every Turn. eventEmbeddings turns on only event vectors and meaning
search.

Co-Authored-By: BYK <[email protected]>
fuseRankedLists in @sentry/junior-plugin-api takes ranked lists, k, optional
per-list weights, and a tiebreak. Event search and junior-memory recall both
use it.

Co-Authored-By: BYK <[email protected]>
Every host check prints its own certificate end date, across 85 hosts and two
compactions. The question names the mail host by role only, and the case
fails when a compaction summary holds the date.

Co-Authored-By: BYK <[email protected]>
… and memories

Move the pgvector column types, the size-guarded cosine distance SQL, the
embed-batch checks with the model-mismatch check, and the refresh loop into
@sentry/junior-plugin-api/embeddings. The Brief task, the event task and the
memory heartbeat use them. Each owner keeps its own table, so every foreign
key still deletes vectors with their source rows.

Add a heartbeat backfill of Conversation embeddings, so a model change no
longer leaves Conversation search without vectors until the next Brief.

Refs #3582

Co-Authored-By: BYK <[email protected]>
@sentry-junior
sentry-junior Bot requested a review from BYK November 17, 2026 14:54
@sentry-junior
sentry-junior Bot added this pull request to stack #3587 November 17, 2026 14:54
@vercel

vercel Bot commented Nov 17, 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 17, 2026 3:17pm UTC

Request Review

@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 17, 2026
@BYK
BYK requested a review from thomasjiangcy November 17, 2026 15:02
Base automatically changed from feat/conversation-event-hybrid-search to main November 17, 2026 15:14
…-helpers

# Conflicts:
#	TELEMETRY.md
#	packages/junior-memory/src/reembed.ts
#	packages/junior/src/chat/conversations/event-embedding.ts
#	packages/junior/src/chat/conversations/event-search.ts
#	packages/junior/src/chat/tools/search-conversation-events.ts
#	packages/junior/src/db/schema/conversation-embeddings.ts
#	packages/junior/src/db/schema/conversation-event-embeddings.ts
#	packages/junior/tests/integration/search-conversation-events.test.ts
#	scripts/await-in-loop-baseline.json
#	tools/oxlint/await-in-loop.json
@BYK
BYK merged commit 4fbdc68 into main Nov 17, 2026
52 checks passed
@BYK
BYK deleted the feat/shared-embedding-helpers branch November 17, 2026 15:30
sentry-junior Bot added a commit that referenced this pull request Nov 17, 2026
Delete core's db/schema/vector.ts (#3586 moved its column types to the
plugin API) and the unused MemoryEmbeddingDimensions type, unexport
MAX_SEARCHED_EVENT_CHARS, add the vitest preload hook as a knip entry, and
drop isBriefsEnabled from the baseline now that main uses it.

Co-Authored-By: Aditya Mathur <[email protected]>
sentry-junior Bot added a commit that referenced this pull request Nov 17, 2026
Delete core's db/schema/vector.ts (#3586 moved its column types to the
plugin API) and the unused MemoryEmbeddingDimensions type, unexport
MAX_SEARCHED_EVENT_CHARS, add the vitest preload hook as a knip entry, and
drop isBriefsEnabled from the baseline now that main uses it.

Co-Authored-By: Aditya Mathur <[email protected]>

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 2c16cbb1 Deployed Nov 17, 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