Skip to content

Load the thread storage file tree on demand - #2139

Merged
SawyerHood merged 1 commit into
mainfrom
bb/fix-1072-lazy-thread-chunks
Aug 21, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
bb/fix-1072-lazy-thread-chunks

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

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/diffs and 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.ts imported @pierre/trees/react statically, and ThreadDetailView calls 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/trees on main:

0. apps/app/src/views/SplitWorkspaceRoute.tsx
1. apps/app/src/views/thread-detail/SplitThreadArea.tsx
2. apps/app/src/views/thread-detail/ThreadDetailView.tsx
3. apps/app/src/components/secondary-panel/useThreadStorageBrowser.ts
4. node_modules/@pierre/trees/dist/react/index.js

What changed

All in apps/app.

  • src/components/secondary-panel/ThreadStorageFileTree.tsx (new): the only runtime importer of @pierre/trees. Exports createThreadStorageTreeModel(onSelectionChange) (the new FileTree(...) that useFileTree wrapped) and the ThreadStorageFileTree render component with the host style that used to live in ThreadStorageBrowser.tsx.
  • useThreadStorageBrowser.ts: no static tree import. When a thread has storage files, an effect imports the gate module (through createRetryingModuleLoader, the same helper the pierre worker pool uses) and builds the model; model is null until then, the resetPaths/selection sync effects wait for it, and cleanup calls model.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 through LazyThreadStorageFileTree (new wrapper in lazySecondaryPanelComponents.tsx) and shows the existing "Loading files..." state while model is null.
  • bundle-budget.json: @pierre/trees added to the SplitWorkspaceRoute forbidden list and to onDemandPackages (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 as ThreadStorageFileTree-*.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 shared workspace-checkout-display chunk). It is visible on every thread page, PromptBoxInternal.tsx owns the useEditor instance 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

  • New useThreadStorageBrowser.test.tsx (real @pierre/trees in jsdom, vi.mock factories only record when the library is first evaluated):
    • "loads the tree library only once there are files to show" fails on main's source files with AssertionError: expected "vi.fn()" to not be called at all, but actually been called 1 times ... "@pierre/trees/react" and passes on this branch.
    • "syncs files and selection into the model once it arrives, then destroys it on unmount": the files and selectedPath that arrived before the chunk are applied to the model; reconciling selection does not echo through onSelectPath; unmount calls cleanUp() once.
  • node scripts/check-bundle-budget.mjs with the new bundle-budget.json against 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 later rerender({ files: [] }) calls did not compile and the CI bundle-budget step never ran. The test now declares initialProps with 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.
  • Manual, own dev instance + headless Chrome: opened a codex thread with two seeded storage files (docs/notes.md, main.ts); thread painted; opened the right panel, the "Thread storage" tree rendered docs and main.ts; clicking main.ts opened the file preview tab; switching back to Info showed the same tree (model survived the tab toggle).

Fixes #1072

AGENT GENERATED: by Claude Opus 5

Independent verification

Round 2, by an independent verifier on a fresh worktree checked out at 3cf9d8d10 (rebased on origin/main 2c0b747a5; git merge-base --is-ancestor origin/main HEAD true, 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 as ThreadStorageFileTree-BVIqkJpc.js (253,881 B).
  • pnpm exec turbo run typecheck --filter=@bb/app --force → Tasks: 3 successful, 3 total, no error 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):

  • With bundle-budget.json, ThreadStorageBrowser.tsx, lazySecondaryPanelComponents.tsx, useThreadStorageBrowser.ts checked out from origin/main and ThreadStorageFileTree.tsx removed: 1 failed | 1 passed — AssertionError: expected "vi.fn()" to not be called at all, but actually been called 1 times with 1st vi.fn() call: ["@pierre/trees/react"] at useThreadStorageBrowser.test.tsx:45:38.
  • Restored to the PR tree: Tests 2 passed (2).

Repro on the fixed branch (own dev instance, production apps/app/dist served on a private port with /api proxied, headless Chrome, performance.getEntriesByType("resource") with an enlarged buffer; codex thread thr_52anzhhcu7 seeded with docs/notes.md and main.ts in its thread-storage dir):

  • Right panel closed: thread painted; SplitWorkspaceRoute-BX4iePCW.js (267,669 B) and workspace-checkout-display-BuOjOA0-.js (1,074,222 B) fetched at ~290 ms; no /thread-storage/files request and no ThreadStorageFileTree chunk at all.
  • Right panel open on the Info tab: /thread-storage/files at 1087 ms, then ThreadStorageFileTree-BVIqkJpc.js at 1207 ms; the "Thread storage" tree rendered docs and main.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:

AGENT GENERATED: by Claude Opus 5

`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
SawyerHood force-pushed the bb/fix-1072-lazy-thread-chunks branch from c12c595 to 3cf9d8d Compare August 21, 2026 04:41
@SawyerHood
SawyerHood marked this pull request as ready for review August 21, 2026 04:52
@SawyerHood
SawyerHood merged commit abde653 into main Aug 21, 2026
13 checks passed
@SawyerHood
SawyerHood deleted the bb/fix-1072-lazy-thread-chunks branch August 21, 2026 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant