Skip to content

perf(objects): Cache identities and extract objects once per message - #3310

Merged
BYK merged 3 commits into
mainfrom
perf/object-identity-cache
Nov 12, 2026
Merged

BYK merged 3 commits into
mainfrom
perf/object-identity-cache

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

Problem

  • Each signed-in request reads its viewer (the User and their user Identities) from the database. These rows change rarely.
  • Each person card reads the same rows again.
  • The dashboard asked for cards one link at a time, as each link came into view. One message could make many requests.

Review of #3296 asked for a fix.

Fix

  • chat/identities/user-cache.ts keeps each User and their user Identities (Slack, GitHub handle, photo) in the state cache. The entries are by User id and by primary email → User id. The TTL is 24 h, as a safety net. No migration and no new table.
  • Every writer clears the entry:
    • upsertIdentityRecord: link, profile sync, photo sync, and Google sign-in.
    • Both unlink paths.
    • User insert, display name, and admin role.
  • Other junior_users columns are not in the cache, so their writers do not clear it.
  • A write can run in a transaction, so it clears the cache before the commit. Each write sets a new version. For 10 s after a write, reads go to the database and do not fill the cache. Old rows cannot stay in the cache.
  • The viewer read and the person resolver use the cache. Person cards skip the 60 s card cache, so a link or an unlink shows at once. Conversation cards keep the card cache for each viewer.
  • Dashboard: the links of one message resolve together. The first link that comes into view or gets the pointer asks for every link in the message, in one request. Each link reads the same query.
  • The dashboard finds the links of each message, and the server caches each object. A message id does not identify most display text, such as briefs, Review items, and Automations, so the server does not extract links by message id.

Numbers

These are on 5,000 Users with Slack and GitHub Identities: the mean of 50 warm calls. The state adapter in tests keeps data in memory. With Redis, each state read is 1 round trip.

Read Before After
Viewer resolve 1 query, 1.63 ms 0 queries, 3 state reads, 0.45 ms
Person card 1 query, 1.08 ms 0 queries, 3 state reads, 0.37 ms
Conversation card 7 queries, 5.5 ms No change. The 60 s card cache is still in front of it.
Message with 2 links, 1 in view 2 requests 1 request

Tests

  • objects.test.ts: a person card comes from the cache for a second viewer, after a database write that skips the writers.
  • objects.test.ts: a GitHub link, then an unlink, shows on the same viewer's next read.
  • conversation-object-links.spec.ts: a reply with 2 links far apart asks for both in one request.
  • Each new test fails without the change.
  • Integration tests now reset state after each test, because the database is reset for each test and the cache keeps its rows. viewer-display-name.test.ts does the same, because each of its tests has its own database.
  • pnpm typecheck, pnpm lint, the junior, plugin, and dashboard unit tests, and the full dashboard e2e (160 passed) pass. attachment-s3-storage.test.ts fails locally because no S3 server runs. It fails on main too.

No UI change, so no screenshots.

via @BYK.

--

View Session [Sentry]

Cache each User and their user Identities in the state cache, by User id
and by primary email. Identity and User writers clear the entry. The
viewer read and person cards use the cache.

Resolve the links of one message in one request.

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

vercel Bot commented Nov 12, 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 12, 2026 11:34pm UTC

Request Review

@BYK
BYK enabled auto-merge (squash) November 12, 2026 22:21
@blacksmith-sh

blacksmith-sh Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Found 1 test failure on Blacksmith runners:

Failure

Test View Logs
tests/dashboard-output.test.ts/dashboard Nitro production output >
serves dashboard routes from core app config
View Logs

Fix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

…cache

# Conflicts:
#	packages/junior-dashboard/e2e/conversation-object-links.spec.ts
#	packages/junior/tests/integration/api/objects.test.ts
- A link that registers after the first card request of its message now
  joins a new request, so its card shows.
- The email to User id cache keeps only found Users. A first sign-in can
  create a User at any time, so a cached miss could hide it. This also
  removes clearCachedEmail and the hash in the key.
- The Nitro output test uses memory state, because its job runs no Redis
  server.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 1b84eea. Configure here.

@github-actions

Copy link
Copy Markdown
Contributor

15 screenshot changes — 15 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 · Desktop
Conversation Attachment · Desktop
Changed
Conversation Attachment · Mobile
Conversation Attachment · Mobile
Changed
Conversation Attachment Modal · Desktop
Conversation Attachment Modal · Desktop
Changed
Conversation Attachment Zoom · Desktop
Conversation Attachment Zoom · Desktop
Changed
Show 9 more changed screenshots
Conversation Attachment Zoom · Mobile
Conversation Attachment Zoom · 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 Long Transcript Touch · Mobile
Conversation Long Transcript Touch · Mobile
Changed
Conversation Object Links Light · Desktop
Conversation Object Links Light · Desktop
Changed
Conversation Share Details · Desktop
Conversation Share Details · Desktop
Changed
Conversation Task Line · Desktop
Conversation Task Line · Desktop
Changed
Push Notifications Tip · Desktop
Push Notifications Tip · Desktop
Changed
Settings Mcp · Desktop
Settings Mcp · Desktop
Changed

@BYK
BYK merged commit 715fda3 into main Nov 12, 2026
45 checks passed
@BYK
BYK deleted the perf/object-identity-cache branch November 12, 2026 23:43
BYK pushed a commit that referenced this pull request Nov 13, 2026
…3317)

## Problem

#3310 moved the person resolver onto the identity cache. When a User has
no name and no Slack Identity, the fallback now reads the name of the
User a second time, so the person card has no name. Before #3310, the
fallback read the name of an Identity, for example a Google Identity.
Bugbot found this on #3310 after it merged
([comment](#3310 (comment))).

## Fix

The name comes from the User first, then from the Slack Identity, then
from any other Identity.

## Tests

- `tests/integration/api/objects.test.ts` adds a User whose only name is
on a Google Identity. The test fails without the fix, because the card
label is the email.
- `pnpm typecheck`, `pnpm lint` and the full dashboard e2e (162 passed)
pass.

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

--

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

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

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
thomasjiangcy added a commit that referenced this pull request Nov 13, 2026
#3310 added a Redis cache of Users (`email:<addr>` → User id, then the
User). Evals reset Postgres before each test but keep Redis, so a later
test resolves its viewer email to a User id from an earlier test, which
no longer exists. The viewer is then not a participant: the auth prompt
is hidden, DM titles fall back to "Direct Message", and DM tool payloads
are redacted. The eval clock restarts at the same instant for every
test, so the 10-second settle window does not protect the `user:`
entries either.

Before each live eval, `src/setup.ts` now deletes every key under the
user cache namespace. The prefix comes from the cache itself
(`cacheKeyPrefix(USER_CACHE_NAMESPACE)`, then `redisStateKey`), so a new
key in that namespace is covered without fixture changes. Redis is not
flushed, because the tests share the cached Sandbox snapshots in it. The
OAuth eval `identityChecks` helper now fails with an assertion when a
bash call has no output, instead of crashing on
`JSON.stringify(undefined).includes`.

Local results, whole files, `ROACH_MODE=off`, Docker sandbox provider,
suite default model:

| File | `main` | This branch |
| --- | --- | --- |
| `auth/dashboard.eval.ts` | 2/3, case 3 fails (`expected undefined to
deeply equal Any<String>`) | 3/3, 2 of 2 runs |
| `conversation/assistant-thread.eval.ts` | 3/3 | 3/3 |
| `conversation/steering.eval.ts` | 7/7 | 7/7 |
| `src/fixture/history.eval.ts` | 5/5 | 5/5 |
| `auth/oauth.eval.ts` | 1/6 | 1/6, same 5 cases |

The `oauth.eval.ts` failures are the same on `main` and are local only:
with the Docker provider, the credentialed `curl
https://example.com/junior-eval-oauth/whoami` gets a 404 instead of the
eval OAuth fixture. CI on this PR is the check for that file. Run alone,
the other files did not show the leak on `main` locally. It needs an
earlier long test in the same Redis.

**Product cache, not changed here:** The cache does not recover when a
cached User id points at a row that no longer exists. No product path
deletes or merges a User, and a primary email never changes, so this
happens only if someone removes a row by hand. If it does:
- `readCachedUserByEmail` keeps `email:` → old id for 24 hours
(`user-cache.ts:181`) and does not look up the email again when that id
has no User (`user-cache.ts:184`).
- If the `user:` entry is cached (24 hours, `user-cache.ts:157`), the
viewer resolves to the removed User, which is the same failure as in the
evals.
- If not, `resolveViewerUser` recovers on each request with an insert
and a read (`viewer.ts:97`), but nothing is cached. `findUserByEmail`
and person cards (`api/objects/person.ts:32`) return nothing until the
email entry expires.

A product fix would be a separate PR.

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

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

--

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

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

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

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 1b84eeaf Deployed Nov 12, 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.

2 participants