Skip to content

perf(dashboard): Build feed and spend tokens from what they show - #2922

Merged
BYK merged 3 commits into
mainfrom
perf/versions-stable-tokens
Nov 8, 2026
Merged

BYK merged 3 commits into
mainfrom
perf/versions-stable-tokens

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

#2863 made the /api/versions tokens too broad. Prod on 0.285.0 (11-07, 16:07–20:00Z):

  • 66% of versions polls (505/765) caused a list refetch. For the busiest client it was 75%. Spend refetched on 55% of that client's polls.
  • The list and spend refetch gap fell to p50 5.2s. On 0.284 it was 10.9s / 10.2s.
  • Requests per tab-minute went from 34.1 to 40.2. The list is the heaviest read: 295ms and 24.6 queries.

Cause

  • conversations summed detail_version across each listed tree. Every Task Event or tool event in any tree moved it.
  • spend used the Conversations' updated_at, which every write moves.

Change

  • Feed token (conversationFeedVersionQuery, still one statement): per first-page root, it hashes only what a feed row shows:
    • id, pin, pin position;
    • title, channel, execution_status, stopped_at, last_activity_at;
    • the viewer's read position;
    • for nested Threads: id, title, status, stopped_at, last_activity_at, plus the archived count;
    • tree annotations (count and max updated_at) for the unfinished-work sections.
  • Spend token: the window start, plus the count, own cost and UTC activity day of the viewer's own rows in the window. It uses the same index scan as the old updated_at marker and skips the tree join of the spend read, so its cost stays flat. Only a recorded cost, or a row that moves to a new day, moves it.
  • Removes the /me/spend cache (people/spend.ts). That 5-minute cache was keyed on the old updated_at marker. /me/spend now reads straight from SQL. When spend moves, the cache would miss anyway. Spend changes show at once instead of up to 5 minutes later.

Proxy fields

Preview and participants have no column of their own. They come from the root's last_activity_at, which every content write moves. For unread, the token hashes whether Junior's latest visible reply is after the read position, the same check the feed row uses. It does not hash the read position, so scroll reports (about one every 750ms) don't move the token. Root tool events still move the token, the same as before during the root's own Turn. Task activity no longer moves it. As before, the token doesn't track access or visibility changes.

The spend token reads only the viewer's own rows. A Task runs as the person whose Turn started it, so the viewer's own Tasks are covered. A Task that another person's Turn started under the viewer's root counts toward the viewer's spend but doesn't move the token. That cost shows on the next spend read: a page load, a tab focus, or the viewer's next own cost.

Measured (local Postgres 17)

Seed: 50 roots with 2 Tasks each. 200 polls. Writes are 9 Task Events for each root Event.

before after
polls that move conversations 100% 10%
polls that move spend 10% 0%
requests / tab-minute (5s poll)* 25.2 13.2
feed token p50 2.9ms 4.2ms
spend token p50, 50 roots 1.1ms 1.25ms
spend token p50, 500 roots 1.6ms 1.9ms
/me/spend aggregate p50, 50 / 500 roots 2.5 / 6.9ms 2.5 / 6.5ms

* Derived from the move rates. The mock dashboard returns fixed tokens, so it can't show the change.

The feed token costs about 1.3ms more per poll, and the spend token about 0.2ms more. Each list refetch it avoids costs about 295ms and 24.6 queries in prod. All plans use index scans.

Spend token options: full /api/versions poll p50

400 polls per run, 2 Tasks per root.

roots old updated_at marker per-day aggregate (59e3076) cost-only marker (this PR)
50 5.4ms 6.8–10.2ms 5.4ms
500 5.3ms 11.8–12.6ms 6.3ms

The old-marker and cost-only columns both use this PR's feed token. They are medians of 3 interleaved runs. The per-day column is 2 runs at be0937d. main's full poll is 4.3 / 4.2ms; the feed token makes up the rest of the difference.

Tests

  • versions.test.ts:
    • a Task's Turn moves only conversation;
    • a recorded root Turn moves conversation, conversations and spend;
    • the cost of the viewer's own Task moves conversation and spend, not conversations;
    • a root reply plus a tool event moves conversation and conversations, not spend;
    • a read report before the reply moves no token; a read report that clears unread moves only conversations.
  • people/spend.test.ts loses its cache steps. It now checks that the 7- and 30-day spend update as cost changes.
  • Passed: pnpm typecheck, root pnpm lint, pnpm file-length:check, tests/integration/api (68 files, 200 tests).

No UI change.

via @BYK.

--

View Session in Sentry

The feed token summed detail_version across each listed tree, so any
Task's own writes refetched the list. The spend token used updated_at,
which moves on any write. The feed token now holds the fields a feed row
shows, and the spend token holds the spend of each day in the window.

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

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

Request Review

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 8, 2026
@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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

Review screenshots in Frameshift

Conversation Attachment Zoom · Desktop
Conversation Attachment Zoom · Desktop
Changed
Conversation Captured Memories Popover · Desktop
Conversation Captured Memories Popover · Desktop
Changed
Conversation Long Transcript Touch · Mobile
Conversation Long Transcript Touch · Mobile
Changed
Conversation Stop Active · Mobile
Conversation Stop Active · Mobile
Changed
Memories · Mobile
Memories · Mobile
Changed
Settings Mcp · Desktop
Settings Mcp · Desktop
Changed

@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 59e3076. Configure here.

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

Will this not hurt the performance of profile pages? Or their load on the system?
Also how does this affect our unread position tracking?

@sentry-junior

sentry-junior Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Profile pages: /api/people/:email doesn't use the spend code, so profile pages aren't affected. The spend meter in the profile menu is affected, and you're right that it adds load. It uses /me/spend plus the spend token in /api/versions.

The new spend token runs the full per-day aggregate on every versions poll. Its cost grows with how many Conversations the viewer had in the last 30 days. Local Postgres 17, p50:

Viewer's 30-day roots (each with 2 Tasks) old marker new token /me/spend aggregate
50 1.1ms 2.7ms ~2.5ms
500 1.6ms 6.3ms ~6.5ms
  • Before: the poll was cheap, but the marker moved on almost any write. In prod, spend refetched on 55% of the busiest client's polls. Each refetch was another request: a marker check, then the aggregate when the in-process cache missed.
  • After: the token moved on 0% of polls locally, so the extra /me/spend requests stop. But a heavy user with no new spend now pays about 6ms of DB time per poll instead of 1.6ms.

A cheap and precise spend token needs one more change. I see two options:

  1. Keep the feed token fix and restore the old spend marker and the cache. Spend refetches stay as they are today.
  2. Make a cost-only marker over the same index scan as the old marker, without the tree join. This needs a check that it still catches Task cost under the viewer's roots. I haven't built or measured it yet.

Tell me which one you want and I'll push it.

Unread position tracking: this PR doesn't change it. Reports, storage and the unread rule are the same.

  • Read reports, Mark as unread, and the stored last_read_seq work as before.
  • readPosition is only on the detail report. The detail token guards it, and this PR doesn't touch that token.
  • readUnreadConversationIds uses the same condition, now through a shared unreadReplySql helper. conversations/read-position.test.ts and conversations/unread.test.ts pass.
  • The only change is when the feed token moves. It moves when a row's unread state flips: a new reply, a read in another tab or device, or Mark as unread. It no longer moves on each scroll report. versions.test.ts checks both cases.

@BYK

BYK commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Make a cost-only marker over the same index scan as the old marker, without the tree join. This needs a check that it still catches Task cost under the viewer's roots. I haven't built or measured it yet.

Sounds like this is the better approach if the numbers and code complexity justifies it, right?

@sentry-junior

sentry-junior Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Yes. It costs about the same as the old marker, so I shipped it in 24e77f2. The spend token now reads only the viewer's own rows, with the same index scan as the old marker and no tree join. It is one statement over the window start, count, own cost, and UTC activity day. Only a recorded cost, or a row that moves to a new day, moves it.

p50 on local Postgres 17, 2 Tasks per root:

old updated_at marker per-day aggregate (59e3076) cost-only (24e77f2)
spend token, 50 / 500 roots 1.1 / 1.6ms 2.5 / 6.7ms 1.25 / 1.9ms
full /api/versions poll, 50 / 500 roots 5.4 / 5.3ms 6.8–10.2 / 11.8–12.6ms 5.4 / 6.3ms

Both poll columns use this PR's feed token, so only the spend token differs. In the bench, spend still moves on 0% of polls.

The diff is +34/−22 lines in spend.query.ts, with no new abstraction: the spend read and the marker share one window filter.

Task cost: a Task runs as the person whose Turn started it, so the viewer's own Tasks are covered. versions.test.ts checks that a cost on the viewer's Task moves conversation and spend.

Gap: another person's Turn can start a Task under the viewer's root. That cost doesn't move the token. It shows on the next spend read: a page load, a tab focus, or the viewer's next own cost. Before, the cache could hold spend for up to 5 minutes.

Typecheck, root lint and tests/integration/api (68 files, 200 tests) pass. I updated the PR body.

@BYK
BYK merged commit e0793eb into main Nov 8, 2026
44 checks passed
@BYK
BYK deleted the perf/versions-stable-tokens branch November 8, 2026 15:56
BYK added a commit that referenced this pull request Nov 9, 2026
…2994)

After #2922, 35% of `/api/versions` polls in prod (0.287.0) still
refetched the list. With a live Turn open it was 61–74%.

## Cause

The feed token hashed each root's `last_activity_at`. Every Event in a
live Turn moves it: `turn_started`, each `assistant_message`,
`tool_execution_started` and `tool_result`, checkpoints, and
`turn_completed`. So each poll during a Turn moved `conversations`.

I reproduced it locally with a real web Turn (fake model, 20 tool steps
3s apart) and the versions poll every 5s. `conversations` moved on **13
of 13 polls**, and `last_activity_at` changed each time. Only 2 of those
polls had a new Message, 2 a status change, and 1 a title change.

`reviewItems` never moved during the Turn. It still refetches the list,
because waiting Conversations outside the first page join the feed. The
new diagnostic will show how often that happens in prod.

## Change

What the feed token hashes for each row:

- **Last activity:** `date_trunc('minute', last_activity_at)`, for the
root and for its nested Threads. Rows show last activity only as a
whole-minute age (`now`, `3m`, `5 minutes ago`) and, while a Turn runs,
a spinner. The token moves at most once a minute during a Turn, and the
age is never more than one minute stale.
- **Order:** rows are hashed in feed order (pinned first, then last
activity), not by id. A reorder still moves the token in the same poll.
- **Preview and participants:** the seq of the newest assistant Message
and the newest user Message. They are two index walks per row on
`junior_conversation_events_message_role_idx`. Before,
`last_activity_at` stood in for both.
- **Unchanged:** title, status, `stopped_at`, unread, pins, Threads, and
annotations are hashed as before. A new reply, status, title, unread
state, or pin still shows within one poll.

**Diagnostic:** when a versions poll moves a token, the browser sends a
`function` span named `Refresh changed reads`. Its
`app.dashboard.versions.moved` attribute lists the tokens that moved
(for example `conversations,reviewItems`). A poll that moves nothing
adds no span. A query recipe is in `TELEMETRY.md`.

**Side effect:** while a Turn runs, the list's `lastSeenAt` can be up to
a minute stale. Archive compares `lastSeenAt` with the stored value, so
an archive during a live Turn can return 409 "Conversation received new
activity". It already could: before this change, each 5s poll window
during a Turn had a new Event. Idle rows are not affected.

## Measured (local Postgres 17)

Polls that refetch the list (`conversations` moved, since `reviewItems`
did not move), on a live Turn of about 65s with a 5s poll:

| | before | after |
| --- | --- | --- |
| live Turn, run 1 | 13/13 (100%) | 2/13 (15%) |
| live Turn, run 2 | 13/13 (100%) | 3/13 (23%) |
| idle, no writes | 0/20 | 0/20 |

The polls that still move are the Turn start (status and title), the
Turn end (status and the reply), and one minute change. A longer Turn
adds about one move per minute plus one per visible Message, so the
floor is about 8% of 5s polls. The feed token doesn't depend on the open
Conversation, so a live Turn in a row that isn't open gives the same
numbers.

Full `/api/versions` poll p50 (400 polls, median of 3 interleaved runs,
50-row first page):

| roots | before | after |
| --- | --- | --- |
| 50 | 6.2ms | 7.5ms |
| 500 | 7.4ms | 7.4ms |

That is about 1ms more per poll, for the two Message lookups. Each list
refetch it avoids costs about 295ms and 24.6 queries in prod.

## Tests

- `versions.test.ts`:
  - starting a child agent now moves only `conversation`;
  - a root tool Event in the same minute moves only `conversation`;
  - one in the next minute also moves `conversations`;
  - a reply with no change to last activity still moves `conversations`.
- Passed: `pnpm typecheck`, root `pnpm lint`, `tests/integration/api`
(71 files, 209 tests), dashboard unit tests (59 files, 408 tests).

No visible UI change.

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

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

--

[View Session in
Sentry](https://sentry.sentry.io/explore/conversations/agent%3A1ea20ccbf455ecc4f9e350b69bc5af97/?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 10, 2026
Since #3023 the spend token reads every Turn metric row and includes
`max(updated_at)`. A checkpoint deletes and rewrites all metric rows of
its Turn (duration, tokens, cost) with a new `updated_at`. So every
checkpoint of the viewer's own Turns moved `spend`, including tool steps
that add time but no cost.

#3023 moved personal spend to the Turn Actor's metric rows. This PR
keeps that and restores the cost-only token from #2922.

## Change

- **Spend token:** reads only `cost_usd` rows. It holds the window start
and the two totals that `/me/spend` shows: 30 days, and 7 days by
`occurred_at`. Both are rounded to 9 decimals so float summing order
cannot move them. `count(*)` and `max(updated_at)` are gone.
- **Spend read:** filters to `cost_usd` rows in `WHERE`. Before, it
aggregated all 8 metric rows of each Turn and kept cost through
`FILTER`. The result does not change.
- `occurred_at` is the Turn start and stays the same across checkpoints,
so the 7-day split moves only with new cost or at UTC midnight.

## Bench

Local Postgres 17. The viewer has 50 or 500 Turns with cost. One live
Turn writes 20 checkpoints, alternating a model step (more tokens and
cost) and a tool step (more duration only). The p50 values are over 300
reads, median of 3 interleaved runs.

| | before | after |
| --- | --- | --- |
| checkpoints that move `spend` | 20/20 | 10/20 (only the ones with new
cost) |
| `/api/versions` p50, 50 Turns | 5.9ms | 5.7ms |
| `/api/versions` p50, 500 Turns | 6.7ms | 6.4ms |
| spend read p50, 50 Turns | 1.5ms | 1.1ms |
| spend read p50, 500 Turns | 2.5ms | 1.5ms |

The read still uses `(actor_identity_id, occurred_at)`, so it still
visits all metric rows of the viewer's Turns and drops the non-cost ones
after the index scan. An index that leads with `metric` would cut that
further, but it needs a migration, so I left it out.

## Tests

- `versions.test.ts`: a checkpoint of the viewer's Task Turn that adds
duration and tokens but no cost moves only `conversation`, not `spend`.
This step fails on `main`.
- Passed: `pnpm typecheck`, root `pnpm lint`, `tests/integration/api`
(74 files, 218 tests), including `people/spend.test.ts`.

No UI change.

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

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

--

[View Session in
Sentry](https://sentry.sentry.io/explore/conversations/agent%3A1ea20ccbf455ecc4f9e350b69bc5af97/?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 — 24e77f27 Deployed Nov 8, 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