Skip to content

fix(server): retain one timeline revision per request shape - #2067

Closed
Yazington wants to merge 1 commit into
get-bb:mainfrom
Yazington:fix/timeline-cache-revision-retention
Closed

Yazington wants to merge 1 commit into
get-bb:mainfrom
Yazington:fix/timeline-cache-revision-retention

Conversation

@Yazington

Copy link
Copy Markdown

What was wrong

The timeline response cache included the thread max sequence in its map key. Each appended event therefore made the prior entry unreachable by future lookups while still retaining its full payload until global LRU eviction. Active threads could accumulate many obsolete revisions for the same request shape, contributing avoidable server heap pressure. See #2066.

What changed

  • Key the cache map by normalized request shape and store the current revision alongside the value.
  • Return a hit only when the stored revision matches the requested revision.
  • Replace the previous revision immediately when a new cacheable response is built.
  • Remove the previous revision when the replacement exceeds the cache size limit, preventing stale data from remaining strongly retained.
  • Preserve independent entries for distinct request shapes and the existing global LRU bound.
  • Add regression coverage for revision replacement, independent shapes, oversized replacements, repeat hits, and LRU eviction.

How you verified

  • Focused timeline-cache.test.ts Vitest suite: 7/7 passed; the revision-retention assertion failed before the implementation change.
  • @bb/server Turbo typecheck passed.
  • ESLint and Prettier checks passed for all three changed files.
  • git diff --check origin/main...HEAD passed after rebasing onto current main.
  • No Playwright or browser automation was used.

Fixes #2066

AGENT GENERATED: by OpenAI Codex (GPT-5)

@SawyerHood

Copy link
Copy Markdown
Collaborator

This is an automated message. We have closed this pull request because it has been open for more than two weeks.

@SawyerHood SawyerHood closed this Sep 4, 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

2 participants