Skip to content

Send history compaction only to viewers of the compacted thread - #4652

Merged
SawyerHood merged 2 commits into
mainfrom
perf/compaction-detail-only-change
Oct 1, 2026
Merged

SawyerHood merged 2 commits into
mainfrom
perf/compaction-detail-only-change

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Event pruning and the output-retention sweeps announced their work as history-rewritten. That change kind is broadcast to every connected client and makes each one refetch thread lists, search, prompt history and timelines, although compaction only changes stored event bodies. Idle sidebars refetched continuously while sweeps ran.

Separately, compaction changes timeline rows without advancing the thread's sequence. The timeline route computes afterSequence deltas against the last rows it served at that sequence, so once one viewer refetched, a second viewer at the same sequence got an empty delta and kept the pre-compaction rows. This already happened on main.

What changed

Split out of #4532 (the compaction part of commit 9537a77) plus a fix for the second-viewer case. Part 3 of 6.

  • packages/domain/src/change-kinds.ts: new thread change kind history-compacted.
  • apps/server/src/services/system/{event-pruning,periodic-sweeps,thread-pruning-sweep}.ts: emit history-compacted instead of history-rewritten; the pruning sweep sends one notification per thread at the end of the sweep.
  • apps/server/src/ws/hub.ts: a change that is only history-compacted goes to that thread's detail subscribers, not to every client.
  • apps/app/src/hooks/cache-owners/realtime-cache-registry.ts: history-compacted refreshes only the thread's timeline window and its loaded turn details (the second added after review; expired output otherwise stayed visible in expanded turns).
  • apps/server/src/routes/threads/data.ts (not in Speed up thread switching, resume, realtime, and composer #4532): after a rewrite or compaction, afterSequence requests at or below the sequence current at that moment get a full window instead of a delta. Compaction also clears the latest-rows cache as a rewrite does. The global ordering-context cache is still cleared only for rewrites; its entries are already keyed by the rewrite generation that pruning bumps.
  • docs/events-table-pruning.md updated.

Compatibility: an already-open tab running the old bundle drops the unknown kind (lenient parse) and simply does not refresh after a compaction. No host daemon wire change. Not included from #4532: moving history-rewritten from immediate to debounced, and invalidation without cancelling in-flight fetches.

How you verified

  • pnpm exec turbo run typecheck lint --filter=@bb/app --filter=@bb/server --filter=@bb/domain: pass (0 lint errors).
  • Server: vitest run test/system/thread-pruning-sweep.test.ts test/system/event-pruning.test.ts test/services/periodic-sweeps.test.ts test/app/hub.test.ts test/public/public-thread-timeline-delta.test.ts: 51/51 pass.
  • New test "answers every viewer of a compacted thread with a full window at the unchanged sequence" fails without the route change (second viewer receives { upsertRows: [] }) and passes with it.
  • App: vitest run src/hooks/realtime-cache-effects.test.ts src/hooks/cache-owners/cache-owner-registry.test.ts: pass, including the new test that compaction marks only the timeline and loaded turn details stale. @bb/domain tests: 204/204.
  • Not re-measured on this base. Speed up thread switching, resume, realtime, and composer #4532 measured idle sidebar refetches at 51 → 0 per minute on its older baseline.

🤖 Generated with Claude Code

AGENT GENERATED

Event pruning and output-retention sweeps announced their work as history-rewritten, which every connected client treats as a reason to refetch thread lists, search, prompt history and the timeline. Compaction changes only stored event bodies, so announce it as history-compacted, deliver it to that thread's detail subscribers only, and refresh just the timeline window on the client.

Compaction changes timeline rows without advancing the thread's sequence, so a row delta computed for one viewer could leave a second viewer at the same sequence holding the old rows. After a rewrite or compaction the timeline route now answers afterSequence requests at or below the sequence current at that moment with a full window.

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:55:11.832279Z 1595cb9 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: 1595cb99cb

ℹ️ 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 +370 to +372
"history-compacted": {
flush: "debounced",
dirty: [getThreadTimelineWindowInvalidationQueryKeys],

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 Invalidate cached turn-summary details on compaction

When the retained-output expiry sweep now emits history-compacted, this rule invalidates only the main timeline query. Expanded or previously loaded turns use useThreadTimelineTurnSummaryDetails, whose cache has staleTime: Infinity; because its query-key prefix is omitted here, those views can continue displaying output that the server has expired until the cache is discarded or the page reloads. Include the turn-summary-details queries in this invalidation while still avoiding unrelated thread/list queries.

Useful? React with 👍 / 👎.

Expanded turn details are cached without a stale time, so output expired by a retention sweep stayed visible in an open thread. Invalidate them together with the timeline window.

Co-Authored-By: Claude Code <[email protected]>
@SawyerHood
SawyerHood merged commit 9de1633 into main Oct 1, 2026
34 of 35 checks passed
@SawyerHood
SawyerHood deleted the perf/compaction-detail-only-change branch October 1, 2026 22:37
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