Skip to content

Key the timeline response cache by request shape, not maxSeq - #2218

Closed
SawyerHood wants to merge 1 commit into
mainfrom
fix/2066-timeline-cache-per-shape
Closed

SawyerHood wants to merge 1 commit into
mainfrom
fix/2066-timeline-cache-per-shape

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

What was wrong

createThreadTimelineCache keyed entries by ${maxSeq}|${paramsKey}. A thread's maxSeq is monotonic (getLatestThreadSequence = MAX(sequence)), so the moment an event is appended the previous entry for the same request window can never be looked up again, yet it stays strongly referenced until 127 more entries push it out of the LRU. During a streaming turn the web client refetches the same window after every event batch, so a single active thread fills all 128 slots with dead revisions within seconds. Reported by @Yazington in #2066; investigation report: https://get-bb.github.io/reports/issues/2066.html (on a seeded 9,001-event thread, 100 append+refetch rounds grew the post-GC heap by 19-22 MB on main vs ~3.6 MB with per-shape retention).

What changed

  • apps/server/src/services/threads/timeline-cache.ts: the cache is now keyed by paramsKey (request shape) and stores { maxSeq, value }. A hit requires an equal maxSeq. A miss deletes the slot before the row-cap check and re-inserts as most-recently-used, so an oversized newer revision (the streaming expanded-turn case) also drops the stale cached one rather than leaving it pinned. getOrBuild takes { paramsKey, maxSeq }.
  • apps/server/src/routes/threads/data.ts: computes paramsKey once and passes it to both the response cache and the latest-rows delta cache.
  • buildThreadTimelineCacheKey and the maxSeq field of ThreadTimelineCacheKeyArgs are deleted; nothing else used them. buildThreadTimelineParamsKey now takes the args type directly instead of Omit<..., "maxSeq">.
  • No wire change (server-internal only), no CLI/doc surface.

Relation to #2067 (draft, also by @Yazington, same root cause and same per-shape idea; credit to them for the diagnosis and the approach). This PR differs in that: (1) it stores the bare maxSeq number instead of a revisionKey string that re-embeds the whole params key, which is redundant once the map is already keyed by shape; (2) it removes the now-unused buildThreadTimelineCacheKey helper and maxSeq key field instead of keeping them; (3) it ships the tests the #2067 body describes but does not contain: a signature-independent route-level regression plus unit coverage for "oversized replacement evicts the stale revision" and "older-maxSeq request never receives the newer value"; (4) it is based on current main (#2067 is ~68 commits behind).

How you verified

  • New route test apps/server/test/public/public-thread-timeline-cache-retention.test.ts drives the real GET /api/v1/threads/:id/timeline against in-memory SQLite through 150 append+refetch rounds with the same request shape and asserts cache.size === 1. It only wraps the cache factory to read .size; it does not name the getOrBuild signature, so it runs unchanged before and after. Before the fix: AssertionError: expected 128 to be 1. After: passes.
  • apps/server/test/services/threads/timeline-cache.test.ts adapted to the new signature, with added cases: new maxSeq replaces the prior revision (size stays 1), an over-cap replacement drops the cached revision, an older-maxSeq request is never served the newer value, separate shapes are retained independently, LRU eviction still works.
  • pnpm exec turbo run test --filter=@bb/server: 199 files / 1901 tests passed.
  • pnpm exec turbo run typecheck lint --filter=@bb/server: clean.

Fixes #2066

AGENT GENERATED: by Claude Opus 5

The thread timeline cache keyed entries by `${maxSeq}|${paramsKey}`. A
thread's maxSeq only increases, so every appended event produced a new key
and left the previous revision of the same request window resident in the
128-entry LRU even though it could never be looked up again. During a
streaming turn the client refetches the same window after every event
batch, so one active thread filled the cache with dead revisions.

Key the cache by paramsKey and store `{ maxSeq, value }`. A hit requires an
equal maxSeq; a miss deletes the slot before the row-cap check (so an
oversized newer revision also drops the stale one) and re-inserts as MRU.
buildThreadTimelineCacheKey and the maxSeq field of
ThreadTimelineCacheKeyArgs had no other users and are removed.

Adds a route-level regression test that drives GET /threads/:id/timeline
through 150 append+refetch rounds and asserts cache.size is 1 (was 128).

Fixes #2066

Co-Authored-By: Claude <[email protected]>
@SawyerHood

Copy link
Copy Markdown
Collaborator Author

Closing. Superseded or out of date; will be redone on current main if still needed.

@SawyerHood SawyerHood closed this Aug 25, 2026
ymichael added a commit that referenced this pull request Nov 4, 2026
## Human comments

## What was wrong

The timeline response cache keyed entries by `maxSeq`, so every new
event left a dead entry behind. One streaming thread fills the 128 slots
with revisions nobody can hit again and evicts every idle thread.
Switching back to an idle thread then rebuilds its window from scratch.
The key also carried the thread's status and surface (title, workspace
and display settings), so a turn starting or ending, a generated title
or a settings change left the previous entry behind as well. Tracked in
#2066.

## What changed

`apps/server/src/services/threads/timeline-cache.ts`,
`thread-timeline-windows.ts`:

- The cache key is the request shape only: thread, page, `summaryOnly`,
`deferContent`, `includeNestedRows`.
- Each entry stores a revision stamp of the thread state it was built
from: `maxSeq`, status and surface. A request with the same stamp hits
the cache; anything else rebuilds and replaces the entry, so each
request shape keeps exactly one entry.
- Limits and invalidation are unchanged. This is the #2067 / #2218
approach, redone on current main.

## How you verified

- New cases in `timeline-cache.test.ts`: a maxSeq advance replaces the
entry, a status or title change replaces the entry, an idle thread stays
cached while another thread streams revisions, and the stale revision is
dropped when the rebuilt window is not cacheable. The key and revision
builders each differ for every one of their inputs.
- Route-level repro (`/api/v1/threads/:id/timeline`, seeded DB): 6 idle
threads were opened, then another thread appended N events with a
timeline read after each, then the idle threads were reopened. Median
idle reopen:

| events between visits | main | this PR |
|---|---|---|
| 30 | 0.3 ms | 0.4 ms |
| 100 | 0.2 ms | 0.3 ms |
| 130 | 10.8 ms | 0.3 ms |
| 300 | 10.8 ms | 0.3 ms |

- A live bb database showed about 170 events a minute across 8 threads,
so main's cache churns in under a minute while a thread is open and
streaming.
- Benchmark on the real timeline route: switching between 6 idle threads
while another thread streamed 25 events between switches, for 18
switches: median 108 ms on main, 3.6 ms on this PR (p90 157 ms vs 6.1
ms).
- `pnpm exec turbo run typecheck lint --filter=@bb/server`, and the
server `test/services/threads` and `test/public/public-thread-timeline*`
suites (38 files, 341 tests) pass after merging current main.

Fixes #2066

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: hellno <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Michael Yong <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Timeline cache retains obsolete revisions for the same request shape

1 participant