Skip to content

Keep late thinking on its originating history page - #3110

Merged
ymichael merged 3 commits into
mainfrom
bb/fixer-keep-late-completed-thinking-on-its-origin-thr_yyvz7agy78
Sep 8, 2026
Merged

ymichael merged 3 commits into
mainfrom
bb/fixer-keep-late-completed-thinking-on-its-origin-thr_yyvz7agy78

Conversation

@ymichael

@ymichael ymichael commented Sep 5, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The shared timeline window closure treated every scoped item that crossed a page boundary as belonging to the newer sequence window. For a completed thinking item whose start was in an older user turn but whose completion arrived after the next prompt began, standard turn pagination therefore separated the lifecycle: the newest page rendered the completion beneath the wrong prompt as “Thought for 0ms,” and loading the older page left the originating turn without its thought. This followed 6e83eb2 from #3066.

A thought that never receives item/completed could also disappear from both pages when its original turn moved into older history.

What changed

  • Expose the completion sequence in the existing item-span query.
  • Give the shared whole-item closure explicit origin-versus-later page ownership.
  • Keep an unfinished thought on its origin page until it receives a newer-page event; then keep the active lifecycle on the newer page without duplication.
  • Assign completed items to their originating user segment while preserving existing later-page ownership for byte and details windows.
  • Constrain span lookups to item events and distribute their thread/type constraints into each scoped lookup, allowing SQLite to use the existing (thread_id, turn_id, type, item_id, sequence) index.
  • Guard that multi-turn query plan and the unfinished and late-completed latest/older/details behavior with focused regressions.

There are no database schema or migration, host-daemon wire, CLI, documentation, UI-component, pagination-limit, or mobile changes.

How you verified

  • The focused regression fails before the fixes: late completion appears on the newest page, while a never-completed thought disappears from both pages.
  • pnpm exec turbo run test --filter=@bb/server -- test/services/threads/timeline-in-turn-window.test.ts — 28/28 passed after rebasing onto current local origin/main.
  • pnpm exec turbo run test --filter=@bb/db — 448/448 passed.
  • pnpm exec turbo run typecheck --filter=@bb/server and pnpm exec turbo run typecheck --filter=@bb/db — passed.
  • The committed multi-turn EXPLAIN QUERY PLAN regression proves the span lookup uses events_thread_turn_type_item_sequence_idx; no new index is created.
  • DevBrowser verified the real app history boundary: after loading the older page, the original turn expands through “Worked for 5s” to the interrupted “Thought for 2s,” its text, and “Stopped manually,” with no thought under the next prompt.

AGENT GENERATED

@ymichael

ymichael commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator Author

@slopcop please review the late-completed-thinking pagination fix when you have a moment.

@bb-slop-cop

bb-slop-cop Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

🚨 SLOP COP 🚨 · review

I’m the Slop Cop, and I’m reviewing this change now. I’ll check security, code quality and architectural duplication, performance, and the practical end-to-end behavior before posting a consolidated review.

@ymichael

ymichael commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator Author

@slopcop another ping for review on the late-completed-thinking pagination fix.

@bb-slop-cop

bb-slop-cop Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

🚨 SLOP COP 🚨 · review

I’m the Slop Cop, and I’m reviewing this change now. I’ll check security, code quality and architectural duplication, performance, and the practical end-to-end behavior before posting a consolidated review.

})
: windowRows;
const wholeItemWindowRows =
window.requiresWholeItemClosure || page.kind === "older"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 slopcop/review — [P2] Avoid a full-thread span scan on every paginated page

This expanded condition now calls ensureSequenceWindowWholeItemRows for every ordinary older page, while the knownHasOlderSegments change also puts normal latest pages through it whenever older history exists. The helper’s span aggregate is constrained by thread and item predicates, but the current indexes make SQLite choose the thread/sequence index and build a temporary B-tree for GROUP BY, so it scans the full thread rather than just the page’s items. With the actual schema, the specialist pass measured roughly 1.9 ms at 10k events, 13.0 ms at 100k, and 53.8 ms at 500k for a 20-item query. Because SQLite runs synchronously, routine refreshes gain history-sized event-loop work and a full older-page walk trends toward O(history × pages). Please target this lookup with an appropriate item-leading index/query shape, or make origin ownership use the existing lifecycle-specific path instead of the all-event MIN/MAX aggregate.

for (const span of spans) {
const key = scopedItemRefKey(span);
if (args.ownership === "origin") {
if (span.completedSeq === null || span.minSequence < args.sequenceStart) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 slopcop/review — [P1] Keep unfinished items visible when they have no post-boundary event

When turn 2 starts before a late completion arrives, this branch classifies completedSeq === null as newer-window-owned and removes the item from the older page. The latest window, however, discovers items only from rows already inside that window, so an interrupted reasoning item whose start and delta occurred in turn 1 vanishes from both pages. A direct in-memory repro at this SHA produced a latest page containing only User 2 with activeThinking: null, while the older page contained User 1 and “Stopped manually” but not the “Checking” thought. If the provider never emits completion, the loss is permanent. Please add the pre-completion two-page case and either retain the unfinished item on its origin page or explicitly hydrate open items into the latest page before filtering it here.

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 SLOP COP 🚨 · review

Plain-English summary: this PR tries to keep a thought attached to the user turn where it began, even when the provider reports that thought as finished only after the next turn has started. It adds explicit page-ownership rules and fetches the late completion back into the older turn.

I found two issues:

  1. [P1] An unfinished cross-page thought can disappear from every page. The new origin-page rule removes all items with no completion, but the latest page can discover one only if it has an event after that page’s boundary. I reproduced the gap between turn 2 starting and the late completion arriving: the latest page had no active thought, and the older page had the manual-stop row but not the interrupted thought. This can persist forever if completion never arrives. The regression test covers only the state after completion.
  2. [P2] Routine pagination now performs a full-thread item-span scan. The whole-item helper runs on every ordinary older page and on latest pages with older history. With the current indexes, the aggregate scans the thread and uses a temporary grouping B-tree; the measured cost rose from about 1.9 ms at 10k events to 53.8 ms at 500k events for 20 page items. Walking all pages can therefore become quadratic in practice.

The security review found no authorization, injection, or cross-thread disclosure issue. The query remains scoped to the authorized thread and preserves inline-output limits. A project-wide architecture scan found no duplicate ownership helper to reuse; the logic is in the right subsystem, though extracting the ownership classification into a pure function would make these edge cases easier to test and audit.

Verification at dec40ec0973af43a177b649423a86e38cfe61a9b:

  • GitHub CI is green.
  • Forced @bb/server typecheck passed.
  • Forced timeline-window suite passed 28/28.
  • Forced DB events suite passed 88/88.
  • The synthetic 10,019-event timeline performance test passed its broad ceiling: 12 pages, 1,185 rows, 180.1 ms minimum full walk.
  • The dev app started successfully and loaded through Doobie. Its isolated database had no threads, so that browser pass was a startup/UI smoke test; the P1 behavior was instead verified directly against an in-memory migrated database.

The first finding is merge-blocking in my view, but this is a comment-only review as requested; I did not approve or request changes.

@ymichael
ymichael force-pushed the bb/fixer-keep-late-completed-thinking-on-its-origin-thr_yyvz7agy78 branch 2 times, most recently from 45ec8a7 to f503f3c Compare September 5, 2026 21:34
@ymichael
ymichael force-pushed the bb/fixer-keep-late-completed-thinking-on-its-origin-thr_yyvz7agy78 branch from f503f3c to 71bc927 Compare September 8, 2026 17:18
@ymichael
ymichael merged commit 0628951 into main Sep 8, 2026
16 checks passed
@ymichael
ymichael deleted the bb/fixer-keep-late-completed-thinking-on-its-origin-thr_yyvz7agy78 branch September 8, 2026 17:50
ymichael added a commit that referenced this pull request Sep 8, 2026
## Human comments

## What was wrong

An older timeline page could correctly keep an item that completed late
beside the prompt where it started, but omit the newer user prompt that
separates the two responses while rebuilding the older row. The older
summary and its details endpoint then calculated different source ranges
(`1–9` versus `4–8`), so exact detail lookup failed with “Failed to load
turn details.” This completes the behavior introduced by [PR
#3110](#3110); the reproduced case is
documented in the [fixer
handoff](https://ymichael.getbb.app/projects/proj_qrv2s45kmq/threads/thr_qtzru86rhh).

## What changed

Standard older-page selection now includes the next prompt only as
projection context when an item or turn lifecycle was backfilled past
the cursor. The prompt establishes the same response boundary used by
full-history and details projection, then is removed before the older
page is returned. Existing late-item ownership, normal latest pages,
byte and sequence pagination, interruption current work, and
retained-output previews remain unchanged.

The regression expands the older summary returned by the real server
timeline builder and proves it returns the same details as the complete
timeline.

## How you verified

- The focused regression failed before the fix with `Timeline turn
summary details could not match range 1-9` and passes after it.
- The exact database/server reproduction reproduced `1–9` versus `4–8`
before the fix and `4–8` versus `4–8` with no error after it.
- `pnpm exec turbo run test --filter=@bb/server --
test/services/threads/timeline-in-turn-window.test.ts` — 30/30 passed
after rebasing onto current local `origin/main`.
- `pnpm exec turbo run typecheck --filter=@bb/server` — passed.
- Earlier adjacent validation passed 91 server pagination/route tests
and 26 DB query-plan tests; the boundary lookup uses the existing
thread/type/sequence index.

Fixes: no linked issue.

> AGENT GENERATED
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