Repository navigation
Load the thread storage file tree on demand - #2139
Merged
Merged
Conversation
`useThreadStorageBrowser` imported `@pierre/trees/react` statically and `ThreadDetailView` calls that hook, so the tree model, renderer and preact were part of the SplitWorkspaceRoute static closure: every thread open downloaded and parsed the file tree before the timeline painted, although only the storage browser in the Info tab uses it. The tree library now lives behind one gate module, `ThreadStorageFileTree.tsx`. The hook imports it dynamically the first time a thread has storage files to show and builds the model from it; the browser renders the tree through `React.lazy` from the same chunk. `model` is `null` until the chunk lands and the sync effects wait for it. `bundle-budget.json` forbids `@pierre/trees` in the route closure, names the gate under `onDemandPackages`, and ratchets the route budget to 10% above the new measurement. SplitWorkspaceRoute closure: 2260.4 KB raw / 599.1 KB brotli before, 2013.9 KB raw / 537.4 KB brotli after. Co-Authored-By: Claude <[email protected]>
SawyerHood
force-pushed
the
bb/fix-1072-lazy-thread-chunks
branch
from
August 21, 2026 04:41
c12c595 to
3cf9d8d
Compare
SawyerHood
marked this pull request as ready for review
August 21, 2026 04:52
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
Issue #1072 (report: https://get-bb.github.io/reports/issues/1072.html) tracks heavy packages that sit in the static import closure of the lazy thread route (
SplitWorkspaceRoute), so every thread open downloads and parses them before the timeline paints. Since the report was written, main already cut three of the five edges it names:FilePreview -> @pierre/diffsand the secondary-panel surfaces (#1894),markdown-preview -> rehype-katex(#1891), and Handlebars (#2080). The budget check in CI now ratchets that closure.One edge was still open:
useThreadStorageBrowser.tsimported@pierre/trees/reactstatically, andThreadDetailViewcalls that hook on every thread page (it has to, so the tree model survives the Info-tab/file-tab toggle). That put the tree model, its renderer and preact (~250 KB raw / ~62 KB brotli emitted) in the route closure, although only the "Thread storage" browser in the Info tab renders a tree, and the storage file list itself is only fetched once the panel opens.node scripts/why-eager.mjs --from=views/SplitWorkspaceRoute.tsx node_modules/@pierre/treeson main:What changed
All in
apps/app.src/components/secondary-panel/ThreadStorageFileTree.tsx(new): the only runtime importer of@pierre/trees. ExportscreateThreadStorageTreeModel(onSelectionChange)(thenew FileTree(...)thatuseFileTreewrapped) and theThreadStorageFileTreerender component with the host style that used to live inThreadStorageBrowser.tsx.useThreadStorageBrowser.ts: no static tree import. When a thread has storage files, an effect imports the gate module (throughcreateRetryingModuleLoader, the same helper the pierre worker pool uses) and builds the model;modelisnulluntil then, theresetPaths/selection sync effects wait for it, and cleanup callsmodel.cleanUp()and ignores a load that resolves after unmount. A failed chunk fetch logs a warning and the next mount retries.ThreadStorageBrowser.tsx: renders the tree throughLazyThreadStorageFileTree(new wrapper inlazySecondaryPanelComponents.tsx) and shows the existing "Loading files..." state whilemodelisnull.bundle-budget.json:@pierre/treesadded to theSplitWorkspaceRouteforbidden list and toonDemandPackages(gate:ThreadStorageFileTree.tsx, so no chunk outside that gate may reach it); route budget ratcheted to 10% above the new measurement (2,268,430 raw / 605,332 brotli, from 2,533,484 / 671,561).Route closure,
node scripts/check-bundle-budget.mjs: before 2260.4 KB raw / 599.1 KB brotli, after 2013.9 KB raw / 537.4 KB brotli (-11% raw, -10% brotli). The tree now ships asThreadStorageFileTree-*.js(253,881 B raw / 64,341 B brotli) fetched when the storage browser first has files.Not in this PR: the composer (
PromptBoxInternal -> tiptap/prosemirror, ~600 KB raw in the sharedworkspace-checkout-displaychunk). It is visible on every thread page,PromptBoxInternal.tsxowns theuseEditorinstance across 3,600 lines, and a first-focus handoff has to keep draft restore, autofocus, the mention menu and iOS keyboard-on-tap working. #1894 deliberately left it allowed in the route budget for that reason; it needs its own design.A previous agent's WIP on this branch (a null-rendering "tree model host" component returned from the hook as a ReactNode) was discarded in favour of the imperative model factory above.
How you verified
useThreadStorageBrowser.test.tsx(real@pierre/treesin jsdom,vi.mockfactories only record when the library is first evaluated):AssertionError: expected "vi.fn()" to not be called at all, but actually been called 1 times ... "@pierre/trees/react"and passes on this branch.selectedPaththat arrived before the chunk are applied to the model; reconciling selection does not echo throughonSelectPath; unmount callscleanUp()once.node scripts/check-bundle-budget.mjswith the newbundle-budget.jsonagainst main's build:@pierre/trees is in the SplitWorkspaceRoute closure (assets/SplitWorkspaceRoute-9lmZmaba.js). It must load on demand, exit 1. After rebuilding on this branch:Bundle budget OK(including the new on-demand gate check).pnpm exec turbo run typecheck --filter=@bb/app --force,pnpm exec turbo run lint --filter=@bb/app --force,pnpm exec turbo run build --filter=@bb/app: pass. (The first push of this PR had two TS2322 errors in the new test file:renderHook(..., { initialProps: { files: undefined } })inferred the props type as{ files: undefined }, so the laterrerender({ files: [] })calls did not compile and the CI bundle-budget step never ran. The test now declaresinitialPropswith an explicit{ files: readonly WorkspaceFile[] | undefined }type; the forced typecheck is clean and the fail-before/pass-after result above was re-run with the typed test.)pnpm exec turbo run test --filter=@bb/app --force: all files pass.docs/notes.md,main.ts); thread painted; opened the right panel, the "Thread storage" tree rendereddocsandmain.ts; clickingmain.tsopened the file preview tab; switching back to Info showed the same tree (model survived the tab toggle).Fixes #1072
Independent verification
Round 2, by an independent verifier on a fresh worktree checked out at
3cf9d8d10(rebased onorigin/main2c0b747a5;git merge-base --is-ancestor origin/main HEADtrue,mergeable: MERGEABLE).Commands run:
pnpm install --frozen-lockfile --prefer-offline && pnpm exec turbo run build(18/18 tasks).cd apps/app && node scripts/check-bundle-budget.mjs→SplitWorkspaceRoute closure: 2013.9 KB raw / 537.4 KB brotli(budget 2215.3 / 591.1),Bundle budget OK.node scripts/why-eager.mjs --from=views/SplitWorkspaceRoute.tsx node_modules/@pierre/trees→0 eagerly reachable module(s) ... only reachable behind a dynamic import (56 modules). The tree is emitted asThreadStorageFileTree-BVIqkJpc.js(253,881 B).pnpm exec turbo run typecheck --filter=@bb/app --force→Tasks: 3 successful, 3 total, noerror TS(the round-1 TS2322 errors in the test file are gone).pnpm exec turbo run lint --filter=@bb/app --force→Tasks: 1 successful, 1 total.pnpm exec turbo run test --filter=@bb/app --force→Test Files 415 passed (415),Tests 3191 passed | 3 skipped.Fail-before / pass-after (
apps/app,pnpm exec vitest run src/components/secondary-panel/useThreadStorageBrowser.test.tsx):bundle-budget.json,ThreadStorageBrowser.tsx,lazySecondaryPanelComponents.tsx,useThreadStorageBrowser.tschecked out fromorigin/mainandThreadStorageFileTree.tsxremoved:1 failed | 1 passed—AssertionError: expected "vi.fn()" to not be called at all, but actually been called 1 timeswith1st vi.fn() call: ["@pierre/trees/react"]atuseThreadStorageBrowser.test.tsx:45:38.Tests 2 passed (2).Repro on the fixed branch (own dev instance, production
apps/app/distserved on a private port with/apiproxied, headless Chrome,performance.getEntriesByType("resource")with an enlarged buffer; codex threadthr_52anzhhcu7seeded withdocs/notes.mdandmain.tsin its thread-storage dir):SplitWorkspaceRoute-BX4iePCW.js(267,669 B) andworkspace-checkout-display-BuOjOA0-.js(1,074,222 B) fetched at ~290 ms; no/thread-storage/filesrequest and noThreadStorageFileTreechunk at all./thread-storage/filesat 1087 ms, thenThreadStorageFileTree-BVIqkJpc.jsat 1207 ms; the "Thread storage" tree rendereddocsandmain.ts.CI on
3cf9d8d10: all checks pass (Checks incl.check-bundle-budget.mjs, Tests app-1/2/3, integration, packages, server, Package Smoke ubuntu/macos).Residual risks / notes for the maintainer:
PromptBoxInternal -> @tiptap/*/prosemirror-*, ~600 KB raw insideworkspace-checkout-display) is still a static import of the thread route; it is named in the issue title and is the largest remaining item behind "no editor parsing on first paint".bundle-budget.jsonallows it by design (Gate the pierre worker pool and trim/ratchet the SplitWorkspaceRoute closure (A2, A1) #1894). If you want Thread route still statically imports the 2.15 MB shared chunk (+ katex): FilePreview → @pierre/diffs, markdown-preview → rehype-katex, PromptBox → tiptap should be lazy #1072 to keep tracking that edge, changeFixes #1072toRefs #1072or open a follow-up issue before merging; otherwise this PR closes it.console.warn, and retries on the next mount. The model is now created per "files non-empty" span instead of once perThreadDetailViewmount, so it is rebuilt after the file list empties and refills;resetPathsre-applies expansion from the current search state in both designs, so no visible difference.