Skip to content

fix(server): optimize transcript body construction for performance - #8109

Open
bankosegger wants to merge 2 commits into
pingdotgg:mainfrom
bankosegger:fix/history-bootstrap-linear-fit
Open

fix(server): optimize transcript body construction for performance#8109
bankosegger wants to merge 2 commits into
pingdotgg:mainfrom
bankosegger:fix/history-bootstrap-linear-fit

Conversation

@bankosegger

@bankosegger bankosegger commented Aug 24, 2026

Copy link
Copy Markdown

What Changed

PERFORMANCE

Rewrote buildBootstrapInput in apps/web/src/historyBootstrap.ts to find the largest fitting suffix of transcript blocks using running length totals instead of rebuilding the array and rejoining the full string on every iteration.

Why

The original loop was O(n²): each of the n messages triggered a full array copy, reverse, and string join just to check the candidate length against the budget. On a long thread (thousands of messages), this scaled badly, measured ~33s for a 20,000-message history vs ~4ms after the fix. Since joined-string length doesn't depend on block order, the fitting count can be computed with O(1) work per block, then the actual string is built once at the end. Verified against the original implementation with 20,000 randomized trials (varying block count/content, prompt length, and budget, including edge cases), output is byte-identical in every case.

UI Changes

N/A pure logic change, no UI surface currently calls this function.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes
  • I included a video for animation/interaction changes

Note

Low Risk
Internal string-budget algorithm only; intended byte-identical output with no API or security surface change.

Overview
buildBootstrapInput in historyBootstrap.ts now picks how many newest transcript blocks fit under the char budget without rebuilding arrays and rejoining the full transcript on every block.

The inclusion loop precomputes fixedLength (preamble, headers, latest prompt) and tracks joinedLength / includedCount so each step only adds block length and omitted-summary length to the running total. After the loop, it slices once to get the included blocks; the existing while path still builds the final transcript string and calls finalizeWithPrompt.

Same fitting logic as before (joined length is order-independent for the count step), aimed at avoiding O(n²) work on very long histories.

Reviewed by Cursor Bugbot for commit 7579aad. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Optimize buildBootstrapInput transcript body construction to avoid O(n²) array rebuilds

Reworks the block-inclusion loop in historyBootstrap.ts to precompute constant header and prompt sizes (fixedLength) and use a running joinedLength accumulator with includedCount to determine how many newest-first blocks fit within the budget. This replaces repeated array rebuilds and full-string joins on each iteration with a single slice of newestFirstBlocks after the count is computed. The subsequent transcript body construction loop remains unchanged.

Macroscope summarized 7579aad.

@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 528489f7-0cf3-417f-a31f-03d6f888e7a5

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Aug 24, 2026
@macroscopeapp

macroscopeapp Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at d9c8aeb

Macroscope's review found this PR approvable — This is a focused performance refactor that replaces repeated O(n²) transcript assembly with running length accounting while preserving the existing output construction and selection flow. It touches only one file and introduces no schema, feature, security, or deployment changes.

You can add or adjust custom eligibility rules. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant