Skip to content

Resubscribe thread timelines with the cached window's tag - #6395

Merged
ymichael merged 1 commit into
mainfrom
bb/fix-timeline-push-resubscribe
Oct 27, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/fix-timeline-push-resubscribe

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

#6220 made thread-timeline subscriptions tag-aware: a subscribe message tells the server which window the client holds (timelineTags), and a subscriber whose tag is null gets no pushes until a tag arrives. The client only sent a tag after a fetch (fetchThreadTimeline → syncThreadTimelineSubscriptionTag). WebSocketManager.unsubscribe() deleted the tracked tag when you left a thread, so coming back resubscribed with null and the server recorded a pending baseline that pushTarget skips. If the cached timeline was less than 2s old (global staleTime), remounting didn't refetch. With pushes on, that is the usual case for a running thread, because every applied push refreshes dataUpdatedAt. So no tag was ever sent. In push mode, appended events also don't invalidate an open timeline (dirtyThreadTimelineQueriesForAppendedEvents). The thread stayed frozen until a focus refetch or reconnect. This only happens with the timelinePush experiment, which early adopters get by default.

A real app run also showed a race that hides the bug on fast-streaming threads. A push still in flight after the unsubscribe re-recorded the tag in handleIncomingMessage, so the bug showed only when you left during a quiet moment. That stale entry could also outlive the cached window it named.

What changed

  • apps/app/src/lib/ws.ts: WebSocketManager.setHeldThreadTimelineTagSource(source) registers a lookup for the tag of the window the client holds. Every subscribe message, both a single subscribe and the batched one sent when the socket opens or reconnects, uses that tag first, then the tracked tag, then null. It also records the tag it sends, so later syncThreadTimelineTag calls dedupe against what the server holds. A pushed tag is recorded only while the target is still subscribed.
  • apps/app/src/hooks/cache-owners/thread-timeline-push-owner.ts: trackHeldThreadTimelineTags(queryClient) points that lookup at the React Query cache: the .tag of the cached ThreadTimelineResponse. The cache is the source of truth. useWebSocket installs it next to the existing applyThreadTimelineSignal wiring.
  • Edge cases:
    • Garbage-collected cache entry. The lookup returns nothing, so the client subscribes with null. The mount fetch that always follows sends the tag. This is Shrink timeline pushes and diff idle windows against active ones #6220's first-open path, so the double full-window push it removed doesn't come back.
    • Segment limit / compact viewport. The query key is per thread, not per segment limit. A cached window fetched with the other limit is still an exact baseline, because the server looks baselines up by content tag per thread and diffs window to window. fetchThreadTimeline already sends afterTag across limits the same way.
    • Reconnect. Sends the cached window's tag rather than the tag of a push the client received but couldn't apply.
    • Stale cache on return. The pushed delta and the mount refetch's HTTP delta are both built against the same tag. Only if the server has evicted that window (16-thread / 32MB backstops) do both send a full window. The client keeps the newer one.
  • No server change. With this fix, a client sends null only when it holds no window, and then a fetch always follows and syncs the tag. A server-side fallback for pending subscribers would bring back the full-window push before the GET that Shrink timeline pushes and diff idle windows against active ones #6220 removed. I added a server test that pins the assumption the fix relies on: a subscriber that leaves and returns with the last pushed window's tag gets a delta, not a full window, even after the 30s read hold has expired.
  • docs/timeline-pagination.md: updated the "Deltas and pushes" paragraph.
  • No wire change and no HOST_DAEMON_PROTOCOL_VERSION bump. No server/daemon field changed, and app/server subscribe messages keep the same shape.

How you verified

  • New apps/app/src/hooks/queries/thread-timeline-resubscribe.test.tsx renders the real useWebSocket and useThreadTimeline, with the real wsManager on a fake socket and the app query client (staleTime 2000):
    • Leaving and returning within the stale window resubscribes with the cached tag and doesn't refetch. Fails on main: the tag sent is null.
    • On reconnect after an unapplied push, the client sends the tag of the window it holds. Fails on main: it sends the pushed tag.
    • After the cache entry is removed, the client subscribes with null, then sends the refetched tag.
  • apps/app/src/lib/ws.test.ts: a push that lands after the last subscriber left doesn't leave a tag behind.
  • apps/server/test/public/public-thread-timeline-push.test.ts: a subscriber returning with its last pushed tag gets a delta that matches a fresh GET.
  • pnpm exec turbo run typecheck lint test --filter=@bb/app --filter=@bb/server: 14/14 tasks pass. App: 6310 passed. Server: 3690 passed. The first run had one failure in the existing pushes deltas from the fetched window that keep a subscriber equal to a fresh GET, which this PR doesn't touch. It uses 50ms push-settle timing under full-suite load. The file passed 3/3 times alone, and the full rerun was green.
  • Real dev app (pnpm dev, fresh data dir, Claude Code provider, bb settings experiment timelinePush true, headless Chromium with a WebSocket send/receive hook). I opened a running thread that runs a 4s Bash step at a time, waited for a push, then clicked another thread and clicked back about 450ms later:
    • Without the useWebSocket wiring (main behaviour): the resubscribe carried null. No pushes arrived in the next 12s, and the view stayed on step 3 while the server was past step 6.
    • With the fix: the resubscribe carried the cached tag. Pushes kept arriving as ~1.1KB deltas (none full), and the view kept updating.
    • On a fast-streaming thread, main sometimes escaped the bug through the in-flight push race described above. That race is why the bug showed up only intermittently.

AGENT GENERATED

🤖 Generated with Claude Code

Leaving a thread forgot its timeline tag, so returning within the query's
stale window resubscribed with null. The server sends nothing to a null
subscriber, no refetch followed, and appended events don't invalidate a
pushed timeline, so the thread froze until a focus refetch or reconnect.

Subscribe and reconnect messages now carry the tag of the window in the
React Query cache, falling back to the tracked tag, and pushes that land
after the last subscriber left no longer re-record a tag.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 406fb09 into main Oct 27, 2026
51 checks passed
@ymichael
ymichael deleted the bb/fix-timeline-push-resubscribe branch October 27, 2026 21: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