Skip to content

fix(cache-warming): Skip NUL escapes when the history scan reads a message - #3528

Merged
BYK merged 1 commit into
mainfrom
fix/refill-unicode-escape
Nov 16, 2026
Merged

BYK merged 1 commit into
mainfrom
fix/refill-unicode-escape

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

The history scan of the cache warming backfill and the savings re-fill casts the stored message text of each schema v2 event to jsonb. A message with a NUL character is stored with a \u0000 escape, and jsonb rejects it (unsupported Unicode escape sequence, 22P05). That failed the query of the whole batch. The scan skips a batch only when the query is canceled, so a re-fill retried the same batch on each slice and never finished. The gap backfill reads events with the same query, so it failed the same way.

loadEvents now removes each \u0000 escape before the cast. The regex keeps an escaped backslash followed by u0000 as it is. Text without the escape skips the regex. The other fields of the message stay, so the event still counts. This works on all Postgres versions; pg_input_is_valid needs Postgres 16 and would drop the whole message.

via @BYK.

--

View Session [Sentry]

@vercel

vercel Bot commented Nov 16, 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 16, 2026 8:00pm UTC

Request Review

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

Okay what exactly are we doing with the message text during warm up data refill? Like why do we even need it and why are we casting it to jsonb?

@sentry-junior

sentry-junior Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

The re-fill doesn't use the message text. It needs a few small fields of each stored assistant_message and tool_result to replay where Turns ended and idle periods started:

  • stopReason: whether a model call ended the Turn or waited on a tool.
  • timestamp: when the request started, which ends the idle period before it.
  • toolName and isError: a successful answer tool result (like reply) ends the Turn, the way live recording does. These are also in the WHERE.
  • model: picks the price.

Schema v2 stores the whole Pi message as one JSON string in payload.message (encodeHistoryPayload in history-payload.ts). Only model, provider, usage, and toolCallId are copied as top-level keys. So to read stopReason, timestamp, toolName, and isError in SQL, the query has to parse that string, and ::jsonb is how it does that. The parse covers the whole message, text and tool output included, so one NUL anywhere in it fails the cast, even though we never read that content.

Other options:

  1. Write the fields as top-level keys too (stopReason, timestamp, toolName, isError, the same as model/usage). New rows would then need no parse at all. Rows that already exist still need one, so this doesn't replace the fix. It does make the parse a fallback for old history only.
  2. Parse in JS. That pulls every full message, large tool results included, into the function. It's worse for the batch time budget.
  3. Read model from payload->>'model'. It's already a top-level key. It's a small cleanup and doesn't remove the need for the cast.

This PR keeps the cast and drops the escape, which is the smallest fix that makes existing history readable. I can add (1) and/or (3) here or in a follow-up if you want.

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

1 and 3 as follow ups please

@BYK
BYK marked this pull request as ready for review November 16, 2026 20:09
@sentry-junior
sentry-junior Bot requested a review from BYK November 16, 2026 20:10
@BYK
BYK enabled auto-merge (squash) November 16, 2026 20:10
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

24 screenshot changes — 24 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 Attachment Zoom · Mobile
Conversation Attachment Zoom · Mobile
Changed
Conversation Captured Memories Popover · Desktop
Conversation Captured Memories Popover · Desktop
Changed
Conversation Child Agents · Desktop
Conversation Child Agents · Desktop
Changed
Show 12 more changed screenshots
Conversation Details Watched Linked Work · Desktop
Conversation Details Watched Linked Work · Desktop
Changed
Conversation Long Transcript Touch · Mobile
Conversation Long Transcript Touch · Mobile
Changed
Conversation Memory List Jump · Desktop
Conversation Memory List Jump · Desktop
Changed
Conversation Object Links Light · Desktop
Conversation Object Links Light · Desktop
Changed
Conversation Pending Attachment · Desktop
Conversation Pending Attachment · Desktop
Changed
Conversation Pending Attachment · Mobile
Conversation Pending Attachment · Mobile
Changed
Conversation Task Line · Desktop
Conversation Task Line · Desktop
Changed
Push Notifications Tip · Desktop
Push Notifications Tip · Desktop
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

6 more changed screenshots in Frameshift.

@BYK
BYK merged commit 31c08d0 into main Nov 16, 2026
48 checks passed
@BYK
BYK deleted the fix/refill-unicode-escape branch November 16, 2026 20:13
@sentry-junior

sentry-junior Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Options 1 and 3 are in #3529, a draft follow-up against main.

BYK pushed a commit that referenced this pull request Nov 16, 2026
Follow-up to #3528.

The cache warming history scan reads `stopReason`, `timestamp`,
`toolName`, and `isError` of each assistant message and tool result. In
schema v2 they are only in the `message` JSON string, so each row had to
be parsed with `::jsonb`.

- `encodeHistoryPayload` now also writes these fields as payload keys,
next to `model` and `usage`.
- `loadEvents` reads a row with these keys directly and parses the
message string only for rows from older releases.
- `modelId` comes from the `model` payload key, which v1 and v2 rows
both have.

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A1e35b4e6bddd6e31a352f1d86ec131e7 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A1e35b4e6bddd6e31a352f1d86ec131e7)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A1e35b4e6bddd6e31a352f1d86ec131e7/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
BYK added a commit that referenced this pull request Nov 17, 2026
… parses a message (#3557)

#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.

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

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A1e35b4e6bddd6e31a352f1d86ec131e7 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A1e35b4e6bddd6e31a352f1d86ec131e7)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A1e35b4e6bddd6e31a352f1d86ec131e7/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Burak Yigit Kaya <[email protected]>

This branch was successfully deployed

1 active deployment
Preview – junior-docs — bf50b876 Deployed Nov 16, 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