Skip to content

Keep conversation outline checkpoints across status changes, restarts and pruning - #7765

Closed
ymichael wants to merge 1 commit into
mainfrom
bb/triage-and-resolve-issue-7753-thr_w8u26q5cmv
Closed

ymichael wants to merge 1 commit into
mainfrom
bb/triage-and-resolve-issue-7753-thr_w8u26q5cmv

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Opening a long thread rebuilt its whole conversation outline far more often than it needed to (#7399). The outline already reuses a checkpoint of settled items and reprojects only the tail. Five things kept throwing that checkpoint away:

  1. Thread status and title were in the checkpoint key. Every turn flips the status at least twice (idle → active → idle), and each flip rebuilt the whole history. Settled items never depend on status or title: status only changes the open tail, and the title only appears in operation rows, which the outline does not show.
  2. The checkpoint only lived in memory, capped at 16 threads and 8 MB. Restarts dropped it, and the 2–3 MB outlines in the report push each other out of an 8 MB cache.
  3. Every pruning pass and every history rewrite discarded it. Bumping the rewrite generation cleared the whole checkpoint, even when pruning only removed rate-limit snapshots (which the outline never reads) or deltas in the unfinished tail. Live pruning runs every 250 events or 30 s on active threads.
  4. Threads made only of manager messages never moved the checkpoint forward, because only a completed turn followed by a new turn could do that.
  5. One rejected steer stalled a thread's checkpoint forever. The checkpoint counted every steer as pending. It could not see the rejection, because client/turn/rejected is not an outline event, and steers with no expected turn get no rejection context. On my largest local threads this froze the checkpoint at sequence 46,341 of 123,157.

What changed

  • Status-free checkpoint key (apps/server/src/services/threads/timeline.ts): buildConversationOutlineCheckpointKey covers the projection version, provider, provider display name, completed-turn display and the cleared-history setting. The tail is still projected with the current status on every request.

  • Stored checkpoints (conversation-outline-cache.ts, thread_conversation_outlines): a checkpoint is written whenever it advances, for any status, along with its ID sets, delta count and projection state. On a miss in memory, an eviction or a restart, the next open resumes from the stored row and reads only the tail.

  • Exact responses kept: idle and errored threads also store their exact response as the items after the checkpoint, keyed by the outline sequence and status. Reopening an unchanged thread still reads no events, as it did before. This adds no storage over today's stored outline.

  • Range-aware invalidation (packages/db): bumpThreadEventRewriteGeneration(db, { threadId, conversationFromSequence }) now takes the lowest rewritten sequence. It deletes only checkpoints that froze that sequence, and clears the stored exact response. Callers:

    • rewind: the cutoff sequence;
    • gap-fill insert: the inserted sequence;
    • completed-output migration: that event's sequence;
    • resolved-item pruning: the lowest removed agent-message delta or background-task progress event;
    • rate-limit pruning: null, because the outline never reads those events.

    This also closes the restart-after-rewrite hole (Drop a thread's stored conversation outline when its history is rewound #6844 class) on every rewrite path, not just rewind.

  • Manager messages that arrive while no turn is running and no request is unaccepted now move the checkpoint forward, the same way a turn that starts and finishes at once would.

  • Pending steers now follow the projection's rule (a steer or auto request with an expected turn), and the selection hands the checkpoint the rejection rows it already fetches.

  • Migrations 0158/0159 drop and recreate thread_conversation_outlines with sequence_start, state_json and the paired nullable response_key/response_tail_json (with a check constraint). The table is a cache, so each thread rebuilds once after upgrading.

  • Unchanged: the checkpoint still never moves past a thread's first timeline ordering boundary. I tried removing that limit, and the real-history replay below caught a reordered system notice, so it stays. Threads with overlapping user messages or user messages sent during a delegation still reproject everything after that point, as they do on main. docs/timeline-pagination.md documents the behaviour.

No wire, CLI, SDK or plugin API changes.

How you verified

  • New tests in conversation-outline-incremental.test.ts. 9 of them fail on main with the same test file: 300–518 event rows re-read where the limit is 25. They cover:

    • every status change;
    • manager-message histories;
    • manager messages while a request waits;
    • restart resume (idle and active);
    • a rewind in the tail versus the prefix;
    • real live pruning in the tail versus the prefix;
    • rate-limit-only pruning;
    • a rejected steer;
    • eviction resuming from storage.

    Every case also checks the outline against an uncached full build.

  • test/timeline-history/outline-replay.test.ts (opt-in, BB_TIMELINE_HISTORY_DB) replays the five largest threads of a real 9.3 GB bb database in random 1–600-event chunks. It changes the status at random, runs live pruning and switches connections to simulate restarts, then checks every incremental outline against a full build. It passes in 147 s. With the ordering-boundary limit removed it fails, so it catches real reordering.

  • Before and after on a copy of a real 10.7 GB database, same five threads (73k–345k sequences), event rows read per outline open:

    Scenario main this PR
    Status change, 2 threads without an early ordering boundary 4,801 / 2,755 rows (48–53 ms) 4 / 6 rows (1 ms)
    Restart then status change, same threads 4,801 / 2,755 rows (48–53 ms) 4 / 6 rows (5–7 ms)
    Status change, 3 threads capped by an ordering boundary 4,822–11,049 rows (77–397 ms) 4,722–7,961 rows (41–96 ms)
    Repeat open or restart of an unchanged idle thread 0 rows 0 rows

    Outlines were identical to main's in every scenario.

  • pnpm exec turbo run typecheck lint --filter='...[origin/main]' passed (27 tasks).

  • pnpm exec turbo run check:query-costs --filter=@bb/db passed, with cases for the new query functions.

  • pnpm exec turbo run test --filter=@bb/server --filter=@bb/db passed: db 726/726, server 4,255 passed with 3 skipped.

Fixes #7399

AGENT GENERATED

🤖 Generated with Claude Code

… and pruning

The incremental outline threw its settled-items checkpoint away whenever the
thread status or title changed, on every pruning pass or history rewrite, on
restart or eviction, after a rejected steer, and it never advanced through
manager-message-only history. Long, busy threads therefore rebuilt their whole
outline on most opens (#7399).

The checkpoint key no longer includes status or title, checkpoints are stored
in thread_conversation_outlines (with the exact response for idle and errored
threads), history rewrites report the lowest changed sequence so only
checkpoints that froze it are dropped, manager messages advance the checkpoint,
and pending steers follow the projection's rule.

Fixes #7399

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael added a commit that referenced this pull request Nov 18, 2026
…7795)

## Human comments

## What was wrong

The timeline had two display orders. It grouped rows turn by turn until
the first user message that landed inside another turn (a message while
a turn was still open, or while a delegated child was still running).
After that "ordering boundary" it sorted every later row by source
sequence. #320 added the switch so a message sent mid-turn shows where
it was sent. Everything downstream then had to reproduce it:

- **Paging:** a whole-thread boundary scan, plus
`timeline-display-ownership.ts` to work out which page shows a row whose
display position differs from its sequence.
- **Server re-sort:** a delegation-span boundary query and a server-side
re-sort.
- **Outline:** the conversation outline checkpoint could never move past
the boundary, so 3 of the 5 largest local threads reproject everything
after it on every rebuild.

The display order itself was also inconsistent before the boundary:
operation notices recorded mid-turn showed after the turn's reply, and
two turns running at once showed one turn whole before the other.
Pending steers were pinned to the bottom on threads without a boundary
but sorted into place on threads with one.

## What changed

**One display rule** (`packages/thread-view`):
- Every row is ordered by where its content starts. A collapsed summary
row starts at its first summarized item, not at the turn's request.
- A completed turn's work collapses into one summary row per run, and a
run ends at any visible user message that lands inside the turn before
its final reply.
- Pages no longer pass split points in; the projection computes them
from the rows it loaded.

**Deleted:**
- the ordering boundary;
- `timeline-display-ownership.ts`;
- the external-user boundary scan in `timeline-context-order.ts` (the
grouping context keeps only request/turn bookkeeping);
- `getFirstParentedTimelineBoundarySequence` and the late-delegation
rows it needed;
- `listStoredStandaloneThreadTimelineRows`, which only display ownership
called;
- `orderTimelineRowsUsingContext`;
- the boundary in the projection cache key;
- the outline checkpoint's boundary cap.

Because display order is now sequence order, a page owns exactly the
rows whose `sourceSeqStart` is in its window.

**Pending steers become live state** (`server-contract`, server, app,
CLI):
- A steer waiting for the running turn is no longer a row on any page.
The latest page returns it in a new `pendingSteers` field, alongside
`activeThinking` and `activeWorkflows`, so push deltas and window reuse
carry it.
- The app's timeline controller and `bb thread log` show pending steers
after the rows, as main does.
- Once accepted, the steer becomes a row with the same id where it was
accepted. Rejected steers stay rows ordered by their request.

**Other:**
- `hasTimelineGroupingContextChangesInRange` replaces the old change
probe, with a query-plan test and query-cost case.
- `docs/timeline-pagination.md` describes the new rule.
- No host-daemon wire change. The timeline response gains
`pendingSteers`, a required array that is empty off the latest page. SDK
types pick it up from the contract.

Visible changes, all toward "in the order it happened":

| Change | Threads affected (of 4,995) |
|---|---|
| A user message moves above a startup warning that fired just after it
| ~186 |
| An operation notice moves to where it happened instead of after the
turn's reply | ~277 |
| Turns that ran at the same time interleave by time | ~12 |
| After a steer, the agent text written before that turn's work shows
above the work summary instead of below it | — |
| A duplicate "interrupted" notice right after an error merges, now that
they're adjacent | 2 |

## How you verified

Real data, from a `VACUUM INTO` copy of a 10.7 GB local bb database:

**Full builds, main vs this branch, all 4,995 threads.** 4,615 are
identical. Every difference is one of the moves in the table above. I
checked each pattern by hand on the threads where it showed up.

**Paged walks.** `test/timeline-history/paged-walk.test.ts` walks a
thread page by page at the default and a 120-event budget and compares
the result with one full build. It passes for 1,000 walks: every thread
whose display changed (380) plus 120 random ones. Main passes the same
set. The deletion was safe because display order now equals page order.
Two intermediate designs broke the walks for exactly that reason (an
in-turn reorder, and pending steers appended after sorting), and both
were fixed by removing the special order, not by adding ownership.

**Outline replay with no boundary cap.**
`test/timeline-history/outline-replay.test.ts` (opt-in,
`BB_TIMELINE_HISTORY_DB`, same harness as #7765) replays the 5 largest
threads plus 7 overlap-heavy ones. It appends events in random chunks,
changes status at random, runs live pruning and switches connections,
and the incremental outline matches a full build at every step (197 s).

**Live.** On a `pnpm start:worktree` instance:
- Seeded real threads showing each change, compared against an
`origin/main` instance.
- Ran a real Codex thread through `sleep 25` and steered it mid-command.
The steer was accepted one event later and renders as a row there,
splitting the turn's summary around it. `pendingSteers` was empty
afterwards.

**Tests:**
- New server test: a pending steer is in no page's rows, only the latest
page carries it in `pendingSteers`, and acceptance turns it into a row
with the same id.
- Boundary-only tests were removed or rewritten as grouping-context
cache tests.
- The CLI snapshot for a pending steer is unchanged from main.

**Commands:**
- `pnpm exec turbo run typecheck lint --filter='...[origin/main]'`
passed (140 tasks).
- `pnpm exec turbo run test --filter=@bb/server --filter=@bb/db
--filter=@bb/thread-view --filter=@bb/server-contract
--filter=@bb/client-core --filter=@bb/cli --filter=@bb/app` passed:
server 4,316, db 718, thread-view 553, server-contract 88, client-core
239, cli 934, app 7,128. One server test timed out while all suites ran
concurrently and passes alone in 7.5 s.
- `pnpm exec turbo run check:query-costs --filter=@bb/db` passed.

> AGENT GENERATED

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@ymichael

Copy link
Copy Markdown
Collaborator Author

Closing at Michael's direction. It conflicts with main after #7795, which removed the ordering boundary this change keeps as a limit on the outline checkpoint. The problem stays open in #7399: opening a long thread rebuilds its outline more often than needed. The causes listed here (status and title in the checkpoint key, memory-only checkpoints, pruning discarding them, manager-only threads, a rejected steer stalling the checkpoint) are the starting point for redoing it on current main.

AGENT GENERATED

@ymichael ymichael closed this Nov 18, 2026
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.

conversation-outline rebuilds 2-3 MB on almost every open of a long, busy thread (0.49.0): key includes outlineSequence and status, no incremental build

1 participant