Skip to content

fix(cache-warming): Drop every Unicode escape before the history scan parses a message - #3557

Merged
BYK merged 1 commit into
mainfrom
fix/refill-invalid-message-json
Nov 17, 2026
Merged

BYK merged 1 commit into
mainfrom
fix/refill-invalid-message-json

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

#3528 dropped only the \u0000 escape before the history scan casts an older stored message to jsonb. Text cut inside an emoji keeps half of a surrogate pair, JSON.stringify writes it as a lone \ud83d, and jsonb rejects that too (invalid input syntax for type json). One such message fails the query of its batch, so the savings re-fill stops again.

The stored text comes from JSON.stringify, so it is valid JSON. jsonb rejects only some \uXXXX escapes in it. The cast now drops every \uXXXX escape that is not itself escaped, which covers this class of errors instead of one escape at a time. The fields that replay reads (stopReason, timestamp, toolName, isError) have no escapes, so their values do not change. This replaces the NUL-only regex.

via @BYK.

--

View Session [Sentry]

@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 9:14am UTC

Request Review

@BYK
BYK marked this pull request as ready for review November 17, 2026 09:18
@sentry-junior
sentry-junior Bot requested a review from BYK November 17, 2026 09:19
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

Review screenshots in Frameshift

Automation Run Details · Desktop
Automation Run Details · Desktop
Changed
Automations Runs Filters · Desktop
Automations Runs Filters · Desktop
Changed
Conversation Attachment Zoom · Desktop
Conversation Attachment Zoom · Desktop
Changed
Conversation Captured Memories Popover · Desktop
Conversation Captured Memories Popover · Desktop
Changed
Conversation Child Agents · Desktop
Conversation Child Agents · Desktop
Changed
Conversation Details Watched Linked Work · Desktop
Conversation Details Watched Linked Work · Desktop
Changed
Show 12 more changed screenshots
Conversation Event Log · Mobile
Conversation Event Log · Mobile
Changed
Conversation Long Transcript Touch · Mobile
Conversation Long Transcript Touch · Mobile
Changed
Conversation Memory List Jump · Desktop
Conversation Memory List Jump · Desktop
Changed
Conversation Memory List Jump · Mobile
Conversation Memory List Jump · Mobile
Changed
Conversation Task Line · Desktop
Conversation Task Line · Desktop
Changed
Memories · Mobile
Memories · Mobile
Changed
Review Desk Item · Mobile
Review Desk Item · Mobile
Changed
Settings · Desktop
Settings · Desktop
Changed
Settings Api Tokens · Desktop
Settings Api Tokens · Desktop
Changed
Settings Api Tokens Created · Desktop
Settings Api Tokens Created · Desktop
Changed
Settings Api Tokens Preview Handoff · Desktop
Settings Api Tokens Preview Handoff · Desktop
Changed
Settings Connections · Desktop
Settings Connections · Desktop
Changed

4 more changed screenshots in Frameshift.

@BYK
BYK merged commit fd74929 into main Nov 17, 2026
48 checks passed
@BYK
BYK deleted the fix/refill-invalid-message-json branch November 17, 2026 09:36
BYK added a commit that referenced this pull request Nov 17, 2026
…3566)

Frameshift reports 21 to 32 changed screenshots on every pull request,
also on backend-only ones such as #3557. Frameshift is
not broken. The dashboard e2e screenshots are different on each run:

| Cause | Screenshots | Fix |
| --- | --- | --- |
| The MCP settings show the dashboard URL, which has the random port of
the worker's e2e server | `settings*` | The capture shows a fixed
origin, then puts back the real text |
| The native `<video>` controls show a loading spinner that fades out on
its own clock | `conversation-task-line`, `-child-agents`,
`-attachment*`, `-share-details`, … | Hide the native controls (browser
UI) in screenshots, and wait for the first frame |
| A resize during capture moves the page under the old pointer position,
so a tooltip or hover shows | `memories__mobile`,
`conversation-share-details`, `conversation-file-draft` | Move the
pointer away before a capture resizes the viewport |
| The object links hover card closes when the desktop capture resizes
the page | `conversation-object-links*` | Set the desktop size before
the hover, the same as the next test |
| The image is centered on mobile, then the resize to desktop keeps a
different scroll position | `conversation-attachment*` | Center the
image in each view |
| The screenshot shows the place where a touch fling stops |
`conversation-long-transcript-touch` | Remove the screenshot |

The next report will show `conversation-long-transcript-touch__mobile`
as removed.

Open: some screenshots still change between runs. The causes are not
known:
- `automation-run-details__desktop` and
`automations-runs-filters__desktop` have sub-pixel glyph differences in
the last message of the run drawer.
- `conversation-attachment-zoom` shows the result of a pinch gesture.
- `conversation-event-log__mobile`, and sometimes
`conversation-question*`, have a different transcript scroll position
after load.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:local%3Aweb%3A4b688b3c1d097b3f15967161 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/local%3Aweb%3A4b688b3c1d097b3f15967161)
[[Sentry]](https://sentry.sentry.io/explore/conversations/local%3Aweb%3A4b688b3c1d097b3f15967161/?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]>
BYK added a commit that referenced this pull request Nov 17, 2026
…nd meaning (#3564)

Compaction summaries keep pull request numbers but lose most paths,
URLs, agent names, and older facts. The compaction benchmark
(`feat/compaction-benchmark`) showed that keyword search over the
removed history raises recall: from 38% to 49% on IDs with the summary,
and from 20% to 34% with nothing in context. Until now,
`searchConversationEvents` could only page events in seq order, with no
text query.

## What changes

`searchConversationEvents` now takes an optional `query`. It applies the
same access check, bounds, and type filter. With a query:

- **Words:** BM25 (k1 1.2, b 0.75) over Postgres lexemes, ranked in
TypeScript. Postgres `ts_rank` is not BM25. `/ # \ : @ ? & =` become
spaces first, and so do a dot or a dash next to a digit. So `file.ts`,
`1234`, or `7421` matches a full path, URL, `owner/repo#1234`, or
`build-7421.log`.
- **Meaning:** cosine distance over stored event vectors, filtered by
provider, model, and dimensions.
- The two rankings are fused with reciprocal rank fusion (k 60) by
`fuseRankedLists`, a new helper in `@sentry/junior-plugin-api` that
junior-memory recall now uses too. Results come best first. Each result
has an excerpt around the rarest query word, and the payload is omitted,
so the agent reads the full event with `readConversationEvent`.
- Searched types: messages, user and assistant messages, and tool
results. The text includes tool names and tool-call arguments. Runtime
context parts are skipped.

The compaction and older-messages notes now tell the agent to find a
fact with a query first.

The eval fixture can now load more history into a Conversation after an
earlier call, so one Conversation can compact twice.

**Stored lexemes:** after each Turn, the event search task always stores
the lexemes of up to 2,000 of the newest events that have none in
`junior_conversation_event_lexemes`, which has a GIN index. Keyword
search reads the stored lexemes.

**Fallback:** keyword search computes lexemes at query time for the
newest 20,000 events that have no stored row yet, so old history stays
searchable. A Conversation with 20,000 events is fully stored after 10
Turns.

**Event vectors:** the same task embeds up to 256 of the newest events
that have no vector (8,000 characters each) into
`junior_conversation_event_embeddings`, as `halfvec` with `STORAGE
MAIN`. It uses the configured `embeddingModelId` with Voyage's document
input type, through the host AI Gateway. Both tables cascade when events
are purged.

Stored lexemes need no provider call, so that half of the task always
runs. The new `eventEmbeddings: { enabled: true }` app setting turns on
only event vectors and meaning search. It is off by default and on in
the local CLI.

The `jsonb` cast that drops Unicode escapes (#3557)
moves into `conversations/sql/message-json.ts`. A lone surrogate in one
stored message would otherwise fail every search of that Conversation.

## Database cost

Local Postgres 17. Event text is 5 KB chunks of this repository's source
and docs. The table also holds 10 other Conversations of 2,000 events
each. Each cell is the range over five queries: a unique id, three
common words, two code words, `getsentry/junior#N`, and `build N`.
`getsentry` and `junior` are in every event, so the last two queries
match every event.

| Events | Stored lexemes |
| ------ | -------------- |
| 3,000  | 6–56 ms        |
| 20,000 | 19–350 ms      |

A query that matches few events is fast. Cost grows with the number of
matching events, because BM25 needs the term frequencies of each match.
On synthetic 5 KB events where every event matches every query, the
stored path takes 157–173 ms at 3,000 events and 1.10–1.16 s at 20,000.
The computed fallback takes 2.6–3.6 s and 17.6–18.8 s.

One run of the task stores 2,000 events in about 1.5–3 s.

- **Lexemes:** term frequencies come from `ts_filter(setweight(...))`
instead of unnesting every lexeme, which is 12 times faster on long
events. Unstored seqs are read before their lexemes are computed, so a
stale row estimate cannot run the subquery again for each event. The
tsquery is built from immutable functions, so Postgres folds it to a
constant at plan time and can use the GIN index for rare words.
- **Vectors:** `halfvec` stores 2 KB per 1,024-dimension vector instead
of 4 KB. `halfvec` needs pgvector 0.7 or later. `STORAGE MAIN` keeps the
vector in its row. The exact scan of 20,000 vectors takes 18 ms instead
of 67 ms. Meaning search needs no vector index, because it ranks one
Conversation at a time.
- **Storage per event, real text:** stored lexemes are about the size of
the compressed event payload, for every Conversation once this PR is
deployed. The GIN index adds about 20% of that. A vector adds 2 KB.
Stored lexemes without positions would be 40% smaller, but BM25 needs
the term frequencies.

## Open decisions

- **Turning on meaning search in production:** this is a one-line change
in getsentry/junior-prod `server.ts`, to follow after this PR ships. At
$0.02 per million tokens for `voyage-4-lite`, embedding is cheap.
Keyword search does not wait for it: stored lexemes fill in after each
Turn once this PR is deployed. Until a Conversation's events are stored,
its search uses the computed fallback, which takes up to about 18 s for
20,000 events. The statement timeout is 30 s.
- **Similarity floor:** the meaning ranking has no floor yet, because
none is tuned for `voyage-4-lite`. #3574 tracks it.

<!-- 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 — e3266b69 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: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant