Skip to content

Retain recently opened thread timelines for delta reopen - #4653

Merged
SawyerHood merged 2 commits into
mainfrom
perf/retain-recent-timelines
Oct 1, 2026
Merged

SawyerHood merged 2 commits into
mainfrom
perf/retain-recent-timelines

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Thread timeline and thread-detail bootstrap queries used React Query's default five-minute cache lifetime. Coming back to a thread after a few minutes threw away its cached rows and paid for a cold full-page timeline load, even though the server can answer with a small afterSequence delta when the client still has rows.

What changed

Split out of #4532 (the retention part of commit dbcbfe1, re-applied on current main). Part 4 of 6.

  • apps/app/src/hooks/cache-owners/thread-open-cache-owner.ts (new): keeps a per-QueryClient recency list of opened threads. Past 20 threads (8 on coarse pointers) the least recently opened thread's inactive timeline and bootstrap queries are removed. A thread still mounted in another pane is never evicted. A deleted thread is dropped from the list and its bootstrap query removed (added after review).
  • apps/app/src/hooks/queries/thread-queries.ts: the timeline and bootstrap queries are kept for one hour after unmount, and useThreadTimeline touches its thread on mount.

Reopening a retained thread shows its cached rows immediately and refreshes them through the delta read main already has. A cached bootstrap still triggers the existing refetchOnMount: "always" thread read. Trade-off: rows up to an hour old are visible for one request round trip.

Not included from #4532: prefetch on hover or focus, keeping the scroll body mounted, first-window rendering, and timeline push over the socket. No server change.

How you verified

  • pnpm exec turbo run typecheck lint --filter=@bb/app: pass (0 lint errors).
  • vitest run src/hooks/queries/thread-queries.test.tsx src/hooks/cache-owners/cache-owner-registry.test.ts: 34/34 pass. Six tests are new: a deleted thread frees its slot (fails without the fix), hour-long retention, eviction past the cap, keeping a thread open in another pane, the smaller cap on touch devices, and reopening a retained timeline with a delta from its cached sequence. The other five fail with main's thread-queries.ts restored.
  • Full @bb/app suite at concurrency 1: 5,180 passed, 3 failed (ToolsView.plugin-detail, CommandPalette.cold-open, CommandPalette). Each of those files passes when run alone on this branch and none touch the changed code; they were not run on clean main.
  • Not re-measured on this base. Speed up thread switching, resume, realtime, and composer #4532 measured reopen after 5.5 minutes at 769 ms / 27 KB → 609 ms / 1.1 KB on its older baseline.

🤖 Generated with Claude Code

AGENT GENERATED

Thread timeline and thread-detail bootstrap queries now stay cached for an
hour instead of the default five minutes after the thread view unmounts.
Reopening a retained thread renders its cached first page immediately and
refreshes it with the existing afterSequence delta read instead of a cold
full-page load.

A per-QueryClient recency list bounds the retention: every useThreadTimeline
mount touches its thread, and past 20 threads (8 on coarse pointers) the
least recently opened thread's inactive timeline and bootstrap queries are
removed. Threads still mounted in another pane are never evicted.

Carved out of #4532; intent prefetch, the mounted scroll body, and timeline
websocket push are not included.

Co-authored-by: hemaaanth <[email protected]>
Co-Authored-By: Claude Code <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-01T21:56:30.346661Z 99fdaf2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 99fdaf2796

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +71 to +73
touched.delete(threadId);
touched.add(threadId);
evictThreadOpenCache(queryClient, touched);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Remove deleted threads from the open-cache LRU

When a recently opened thread is deleted, removeThreadScopedQueries removes its timeline but neither removes its retained bootstrap query nor can remove the ID from this private touched set. If the deleted IDs are newer than other cached threads, subsequent opens evict those older, still-valid timelines first while the deleted entries continue consuming the cap for up to an hour, reducing the number of threads that can benefit from delta reopening. Add a removal path for this bookkeeping and invoke it from the thread-deletion cache cleanup.

Useful? React with 👍 / 👎.

A deleted thread kept its slot in the recency list and its retained bootstrap query, so later opens evicted still-valid timelines first. Forget the thread when its scoped queries are removed.

Co-Authored-By: Claude Code <[email protected]>
@SawyerHood
SawyerHood merged commit dc683db into main Oct 1, 2026
21 checks passed
@SawyerHood
SawyerHood deleted the perf/retain-recent-timelines branch October 1, 2026 22:38
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.

1 participant