Skip to content

perf(conversations): Read status with fewer event scans - #4120

Merged
dcramer merged 1 commit into
mainfrom
perf/conversation-status-sql
Nov 26, 2026
Merged

dcramer merged 1 commit into
mainfrom
perf/conversation-status-sql

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

Status is still derived exactly and in one place (conversationStatusSql). This change makes the reads it depends on cheaper. Follows #2951 (part of #2888).

  • Sign-in wait (authorizationWait): reads the newest authorization_requested, then checks with NOT EXISTS for a later event of the four end types. The old form walked back over every later event.
  • Failed Turn (conversationTurnFailedSql): uses a short-circuit form. It takes the newest turn_failed and checks for no later start or complete. Status, the feed and the aggregates all share this one derivation.
  • Aggregates: locations read failed once per root instead of once per joined tree row. People check stopped_at before reading events.
  • Latest reply: migration 0167 adds junior_conversation_events_message_role_seq_idx (CONCURRENTLY IF NOT EXISTS), a partial index on type = 'message' ordered by seq, so the status read no longer sorts every assistant message. message_role_idx stays, because the activity preview and outline read in created_at order.

No status cache.

via @dcramer.

--

View Session [Sentry]

@vercel

vercel Bot commented Nov 23, 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 26, 2026 12:32am UTC

Request Review

@dcramer

dcramer commented Nov 25, 2026

Copy link
Copy Markdown
Member

Give me a thorough review of this change. I wanna make sure we're not adding stuff that's gonna be a problem later

@sentry-junior

sentry-junior Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Review of 1feb81c21. Verdict: ship with fixes. The rewritten queries return the same results as the old ones, and the new index is worth its write cost. The risks are the stale branch and two rules the tests don't pin.

1. Must-fix: the branch is stale against main. It conflicts in _journal.json and authorization-wait.ts, and it uses the flat 0167 migration format, which main no longer accepts after #4129/#4242 (Drizzle v1 timestamped folders, enforced by check-drizzle-migrations.mjs). #4206 also exported hasUnexpiredAuthorizationRequest, which the feed token uses (api/conversations/list.ts:419). If the conflict takes this PR's side, that export is gone and the token breaks. A rebuild on main is already being pushed: it uses a timestamped migration with the single CREATE INDEX CONCURRENTLY IF NOT EXISTS form that src/db/migrate.ts requires, and it keeps hasUnexpiredAuthorizationRequest. Both that check and the wait should derive their expiry from authorizationWaitStart(), so the 24h rule lives in one place.

2. Should-fix: the tests don't catch two of the rewritten rules. I mutation-tested them:

  • tests/integration/api/locations/list.test.ts:134: I changed failed to count any child's failed Turn. All 158 conversation, locations and people tests still pass, because the failed child sits under a root that fails on its own. Put the failed child under a root that has not failed (for example slack:C1:retried) and keep failed: 1.
  • tests/integration/api/conversations/status.test.ts:177: I removed turn_completed and turn_failed from AUTHORIZATION_WAIT_END_TYPES, and nothing failed. A missing end type would leave a Conversation at needs_input for 24h. Add one case where a request is followed by turn_completed and one where it is followed by turn_failed.

3. Optional: index and comments.

  • Keep the index. With 20k assistant Messages, the latest-reply read drops from a bitmap scan plus a 20k-row sort (8.7 ms) to one backward index step (0.07 ms).
  • This leaves two near-duplicate partial indexes on the hot events table. message_role_idx (created_at, seq) is still needed only by activity-preview.ts (latestMessage, latestMessageSeqQuery). The PR body is wrong that the outline needs it: outline.ts orders by seq. A follow-up can move the preview to seq order and drop message_role_idx.
  • The literal type = 'message' only matters for generic plans. Unnamed pg statements get custom plans today, so the literal is defensive. It follows outline.ts:121.
  • Two stale comments: outline.ts:96 still names message_role_idx, and authorization-wait.ts:77-80 says the request read uses authorization_request_idx (EXPLAIN shows type_idx).

Checked, no issue: seq ties ((conversation_id, seq) is the PK); end events before the request; the 24h expiry; null payloads; old NULL vs. new false for failed (the readers at list.ts:808 and agent-invocations/list.ts:107 test truthiness); the locations root row (roots point to themselves); the people stopped_at reorder; no second derivation of a rule; and the plan-cost budgets in versions.test.ts/profile.test.ts, which move about 1–3%.

The sign-in wait reads only the newest authorization request, then checks
that no event that ends a wait follows it. The failed-Turn check reads the
newest turn_failed, then checks for a later Turn start or end, so most
Conversations stop at the first read. Location aggregates read a root's
failed Turn once per root, not once per tree row.

The latest assistant reply reads a new partial index in seq order, so it
no longer sorts every assistant Message of the Conversation.
@sentry-junior
sentry-junior Bot force-pushed the perf/conversation-status-sql branch from 5e9ec82 to 4ccd581 Compare November 26, 2026 00:31
@dcramer
dcramer marked this pull request as ready for review November 26, 2026 00:41
@sentry-junior
sentry-junior Bot requested a review from dcramer November 26, 2026 00:41
@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 26, 2026
@dcramer
dcramer merged commit 4f84e36 into main Nov 26, 2026
59 checks passed
@dcramer
dcramer deleted the perf/conversation-status-sql branch November 26, 2026 00:45
BYK added a commit that referenced this pull request Nov 26, 2026
This prepares `main` for a merge queue. Two PRs that each add a
migration off the same parent both pass CI on their own, then leave two
migration leaves on `main`. That happened with #4377 and #4120, and
before them with #4224, #4290, #4313 and #4279. A merge queue tests each
PR merged on top of the ones ahead of it, but only if `ci / required`
reports on `merge_group` runs. That's what this adds.

On `merge_group`, `getsentry/action-filter-paths` matches every filter,
as on a push, so every path-gated job that `ci / required` needs runs.
Two jobs still skip, as they do on a push to `main`, and a skipped job
passes `ci / required`:
- `pack-release-artifacts` runs only on push.
- `ios` runs only on pull requests, because `ios.yml` runs on its own
for pushes to `main`.

Frameshift doesn't save or report screenshots on queue runs.

Turning on the merge queue itself is a separate change to the `main`
ruleset. "Require branches to be up to date" stays off.

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

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

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A9ec51d25699838a155f2ddc2f0ffb45f)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A9ec51d25699838a155f2ddc2f0ffb45f/?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 — 4ccd581c Deployed Nov 26, 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