Skip to content

perf(api): Read the detail record and unread with the state - #2991

Merged
BYK merged 2 commits into
mainfrom
perf/detail-chain
Nov 9, 2026
Merged

BYK merged 2 commits into
mainfrom
perf/detail-chain

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

Shortens the detail read chain. A detail read reads the junior_conversations record twice, and the unread-reply check waits for the state row.

  • Record: the detail state statement now selects the Conversation record too. It uses the same columns and join conditions as selectConversationRecords, which record.ts now exports as the shared constants conversationRecordColumns and conversationRecordJoins. readConversationDetail uses that record instead of calling readConversationRecordFromSql after the ETag phase (span f1308061).
  • Mailbox: the mailbox checks that the Conversation exists through the shared per-request access read, instead of getConversationStore().get (the other full junior_conversations select, 07956ffe).
  • Unread: the unread-reply check (8c0a949c) is a column of the state statement (829eaa0b), built with the existing unreadReplySql. It no longer runs as a second round after the state row.

Why one statement and not parallel reads: the report must never be older than its ETag. If the record were read before or beside the version token, a write in between could pair an old record with a new ETag. Later polls would then answer 304 and keep the stale record, for example an execution status that never finishes. One statement reads one snapshot, so the record, unread and the version token always agree. Folding the unread check in also saves a query and a pool connection, where a parallel read would still use both.

The ETag value and its basis are unchanged.

Measurements

Local Postgres 17, pool max raised to 10 for the run. Seeded data: a private root with 20 visible messages, a child Conversation and a participant viewer with a read position. Requests go through createJuniorApi() with ?mailbox=true, matching the dashboard. Each run makes 100 requests after a warm-up; I ran three before/after pairs, interleaved on the same machine.

Read Metric Before After
304 poll (ETag matches) Queries 5 3
Rounds, one after another 4 3
Peak queries in flight 2 1
Wall p50 7.2–8.1ms 6.6–6.9ms
Full report Queries 23 20
Rounds, one after another 8–9 6–7
Peak queries in flight 10 10
Wall p50 23.8–25.7ms 22.1–23.1ms
Both State statement p50 1.5–2.0ms 2.3–2.9ms

The state statement costs about 0.8ms more locally. It replaces two reads, the record (~0.7ms) and the unread check (~1.1ms), that ran in their own rounds. In prod each removed query can also mean one fewer new Neon WebSocket (115–190ms when cold), so the saving there should be larger than local wall time shows (inference).

Checks

  • pnpm typecheck and pnpm lint pass.
  • vitest run tests/integration/api: 209 passed. This covers the detail, ETag, unread, read-position, pending-message and MCP tests.
  • tool-replies and agent-run-provider-retry component tests, which call readConversationDetail directly: 17 passed.

Server-only change, so no screenshots.

via @BYK.

--

View Session in Sentry

Fold the Conversation record and the unread-reply check into the detail
state statement, so the report's record comes from the same snapshot as
the ETag version token. The mailbox checks existence through the shared
access read instead of reading the Conversation again.

Co-Authored-By: BYK <[email protected]>
@vercel

vercel Bot commented Nov 9, 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 9, 2026 10:19am UTC

Request Review

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 9, 2026
The generic selectConversationRecords return type was too large for the
declaration emit in the package build (TS7056). Share the record columns
and join conditions as constants instead.
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

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

Review screenshots in Frameshift

Conversation Attachment · Desktop
Conversation Attachment · Desktop
Changed
Conversation Attachment · Mobile
Conversation Attachment · Mobile
Changed
Conversation Captured Memories Popover · Desktop
Conversation Captured Memories Popover · Desktop
Changed
Conversation Details Watched Linked Work · Desktop
Conversation Details Watched Linked Work · Desktop
Changed
Conversation Details Watched Linked Work · Mobile
Conversation Details Watched Linked Work · Mobile
Changed
Conversation Long Transcript Touch · Mobile
Conversation Long Transcript Touch · Mobile
Changed
Show 3 more changed screenshots
Conversations Sections Collapsed · Desktop
Conversations Sections Collapsed · Desktop
Changed
Memories · Mobile
Memories · Mobile
Changed
Settings Mcp · Desktop
Settings Mcp · Desktop
Changed

@BYK
BYK merged commit 84c694a into main Nov 9, 2026
44 checks passed
@BYK
BYK deleted the perf/detail-chain branch November 9, 2026 10:29
BYK added a commit that referenced this pull request Nov 17, 2026
Turns the perf patterns from #3184,
#2820, #2991, #3271,
#2921 and #3449 into checks that fail
CI, and fixes the code that breaks them. `policies/performance.md` lists
each rule and the check behind it. The DB and request budgets come in a
follow-up PR.

| Rule | Where | Fixed | Left |
| --- | --- | --- | --- |
| `unicorn/no-array-for-each` | root + dashboard | 43 | 0 |
| `query-invalidation/no-broad-query-invalidation` (new, dashboard JS
plugin) | dashboard `src` | 20 calls | 5 disable comments with reasons |
| `junior/bounded-fan-out` (new) | `packages/*/src` | 24 (19 behind
`pLimit`, 5 batched reads) | 13 disable comments naming the cap |
| `no-await-in-loop` | `packages/*/src` | 3 moved to bounded
`Promise.all`, 2 dashboard disables | 467 sites in 137 files, baselined
|

**`query-invalidation/no-broad-query-invalidation`** fails on
`invalidateQueries`, `refetchQueries`, `resetQueries` or `removeQueries`
when the key is missing, a literal of 2 or fewer parts, or a bare prefix
const, unless the call passes `exact: true`. The fixes:
- `invalidateConversationFeeds` replaces each `["dashboard",
"conversations"]` refresh.
- Rename writes the response title into the feeds and does not refetch.
- `reviewItemQueryKey` and `invalidateReviewItemCollections` let Review
item actions write item details from the response and refresh only
count, list and waiting conversations.

**`junior/bounded-fan-out`** flags
`Promise.all`/`allSettled(<list>.map(async …))` unless the callback
calls a `p-limit` limiter. A literal array is not flagged. Fixes use
limits of 5 for provider APIs and 8 for internal storage. Batched fixes
are `readSubscriptions` (one `mGet`) in `chat/events/store.ts` and
`getStateValues` in cache warming. `junior-github` gets a `p-limit`
dependency.

**`no-await-in-loop` uses a baseline** because 467 sites is too many to
triage safely in one PR. Many are intentionally sequential: cursor
pages, ordered writes, rate limits, stop-on-first-failure.
`tools/oxlint/await-in-loop.json` turns the rule off only for the listed
files, and `scripts/await-in-loop-baseline.json` holds their per-file
counts. `pnpm await-in-loop:check` (part of `pnpm lint`) fails when:
- a count goes up,
- a count goes down and the entry is not lowered or removed,
- the two file lists drift.

New files get the rule with no exemption.

**Automations exclusion:** Otto is rewriting this code in
#3570, so it is left alone. In the dashboard,
`pages/automations/**`, `inbox/pages/automations/**` and
`review-desk/inboxQueries.ts` have `no-broad-query-invalidation` and
`no-await-in-loop` turned off. The server-side Automations files sit in
the `no-await-in-loop` baseline like any other file. None of them had
fan-out sites. Remove the dashboard override when that rewrite lands.

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

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

--

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