Repository navigation
perf(ui): memoize sidebar row projection at its owner - #160876
Conversation
The sidebar rebuilt navigation state, the row forest, catalogs, zoned sections, and the Home row on every Lit update. The navigation element now owns staged slot memos keyed by snapshot identities and explicit revisions: attention, sessions-store publication, outbox snapshot, projection membership/created order, and a held-subtitle deadline. Collections that were mutated in place are replaced immutably. Production bundle, 300-session roster: sidebar projection CPU per cold /chat ~77 -> ~36 ms; per session switch ~16 -> ~10 ms.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed September 29, 2026, 9:25 PM ET / September 30, 2026, 01:25 UTC (Revision 2). ClawSweeper reviewWhat this changesThe Control UI sidebar reuses session navigation, row, catalog, section, and Home projections across updates, with explicit invalidation inputs and regression coverage. Merge readiness✅ Ready for maintainer review Current main still rebuilds unchanged sidebar projections. The earlier menu-only PR closed without merging, so this broader candidate remains useful. The prior import-cycle finding is fixed, and this review found no actionable defect in the pinned head. Priority: P2 Review scores
Verification
How this fits togetherThe Control UI sidebar combines Gateway session lists with local attention, outbox, and view state. It projects session rows and sections for the sidebar renderer. flowchart LR
A[Gateway session lists] --> D[Sidebar projection memo]
B[Attention and outbox snapshots] --> D
C[View preferences] --> D
D --> E[Session rows and sections]
E --> F[Sidebar renderer]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +339, Tests +484. Total +823 across 25 files. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Technical reviewBest possible solution: Keep one sidebar projection owner with explicit invalidation from the session, attention, outbox, and view-state publishers. Do we have a high-confidence way to reproduce the issue? Yes, at source level: current main rebuilds session projections during every sidebar update, including menu-only updates. The contributor supplies a concrete 300-session browser scenario; this review did not execute it. Is this the best way to solve the issue? Yes. Caching at the projection owner with publisher-backed invalidation addresses the repeated work while preserving one rendering path. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 452c808d5b1b. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (1 earlier review cycle)
|
…pete/perf-sidebar-row-projection-memo # Conflicts: # ui/src/components/app-sidebar-session-navigation.ts
* perf(ui): memoize sidebar row projection at its owner The sidebar rebuilt navigation state, the row forest, catalogs, zoned sections, and the Home row on every Lit update. The navigation element now owns staged slot memos keyed by snapshot identities and explicit revisions: attention, sessions-store publication, outbox snapshot, projection membership/created order, and a held-subtitle deadline. Collections that were mutated in place are replaced immutably. Production bundle, 300-session roster: sidebar projection CPU per cold /chat ~77 -> ~36 ms; per session switch ~16 -> ~10 ms. * fix(ui): break sidebar projection memo import cycle * fix(ui): align sidebar projection host signatures
What Problem This Solves
Fixes: the Control UI sidebar re-projects every session row on every sidebar update, including unrelated menu, hover, footer, and presence updates. On a 300-session roster that is ~77 ms of main-thread CPU per cold
/chatload and ~16 ms per session switch.User Impact
User impact: cold
/chatloads and session switches spend less main-thread time on sidebar projection: −53% per cold load (~77 → ~36 ms) and −37% per switch (~16 → ~10 ms). Unrelated sidebar updates no longer re-project rows. No visible behavior change.Why This Change Was Made
The navigation element that owns the sidebar projection now memoizes each stage (navigation state, rows, catalogs, sections, Home) keyed by snapshot identities and explicit revisions. Every live input the per-row callbacks read (attention, outbox/drafts, sessions-store facts, projection state, locale) got an explicit invalidation source instead of time- or poll-based freshness.
Problem detail, root cause, change, and growth
The Control UI sidebar rebuilt its whole session projection on every sidebar update, whatever had changed: navigation state (filter and sort of the roster), the row forest (
projectSidebarAgentSessionRows→ archive visibility →projectSessionTree→ per-rowtoSidebarSession), catalogs, zoned sections, and the Home row. That included updates from unrelated menus, footer state, hover and presence. On a 300-session roster, the production-bundle profile attributed ~77 ms of main-thread CPU per cold/chatload and ~16 ms per session switch to these projections, spread thinly across per-row helpers. Micro-optimizing those helpers would not help; the lever is not recomputing when the inputs are unchanged.Root cause
AppSidebarSessionNavigationElementowns the projection, butAppSidebar.willUpdate()rebuilt it unconditionally and held the result only for one update. Several live inputs had no identity a cache could key on: the attention resolver was recreated every update; outbox and draft badges came from stable call-time callbacks, backed by a manualrequestUpdate()poke from the shell; sessions-store facts (PR summary, prepared work sessions, deletion and archive visibility) had no revision; and several collections were mutated in place. The Home row separately re-ran archive visibility and tree projection on every render and inupdated().Change
One owner and staged memos. The navigation element owns memos for navigation state, rows, catalogs, zoned sections, and one Home memo per agent. Each stage compares a slot tuple with
Object.isand recomputes only when a slot or its upstream stage value changes. A stage lists only the inputs its upstream value does not already key. The per-update cache and its overrides inAppSidebarare deleted. Stage inputs and projection bodies live insidebar-projection-memo.tsnext to the owner, which keeps the navigation module under its line cap. There is no second projection path.Every live input now has an explicit invalidation source (nothing is time- or poll-based):
sessionResultsByAgent,pendingCatalogArchives,visibleSessionLimits, androsterVisibleSessionLimitsare replaced immutably instead of mutated in place.revision, bumped where the store notifies subscribers.SessionAttentionControllerowns a revision bumped by question-prompt changes, overlay approval changes, Gateway resync, and the agent-status expiry timer, which keeps its deliberate +1 ms guard.createResolver()is memoized by that revision.storedOutboxes) as a sidebar property, and its identity changes when the outbox publishes. This replaces theoutboxAttentionCountForSession/hasSessionDraftcallbacks and the manual sidebar poke.SidebarSessionProjectionrevisions cover created order (only when keys are added), sticky membership, and child expansion. Held subtitles get one earliest-deadline timer, cleared on disconnect, so a replacement deferred by the 2 s display floor lands at its deadline instead of waiting for an unrelated update.i18n.getLocale()is a navigation slot, since row labels are translated.The roster child element now re-renders with its mutable host (selection, menus, drag, presence) through
hasChangedonhost. Before, it re-rendered on every sidebar update only becausesectionswas a fresh array each time.Route switches still run two full row projections: first for the route key, then for the lineage publication, which can change row fields and structure. An active-flags-only post-pass cannot safely replace that structural invalidation, so none was added.
Production code grows by 286 net lines. The navigation owner shrinks by 17. The new stage module is 250 lines, about 110 of which are the existing navigation and section projection bodies moved out of the owner. Projection revisions and the subtitle deadline add ~59, the attention revision ~23, the sessions revision ~7, and immutable publications ~7. The
AppSidebarcache and callback removal saves 44. The growth is the explicit slot list for ~25 live inputs; the alternative (time-based freshness or deep comparison) was rejected.Behavior preserved
Grouping, catalogs, archive and deletion visibility, attention summaries, child loading and expansion, show-more, organizer drag/drop, pinned rows, owner filters, roster and chip modes, the Home row, hovercards, narration, and PR indicators behave as before. No config options, dependencies, or Gateway changes.
Evidence
Setup: scratch Playwright rig against the production bundle (mocked Gateway, 300-session roster, Chromium, CPU throttle 1). Base
632b150d462fvs this change, run back-to-back on the same host. Sampled inclusive CPU per trace, source-mapped, median of 5 traces./chat: rows (projectSidebarAgentSessionRows)/chat: Home (projectSidebarHomeSession)/chat: navigation state/chattotalSlot evaluation costs about 0.5 ms per cold load (navigation inputs) plus ~0.2 ms (resolver check).
Page-level medians: cold
/chat(7 iterations) script 276 → 255 ms and task 1,350 → 1,241 ms. First sidebar row 1,032 → 1,024 ms (unchanged; in both builds it usually lands in the same long task as the transcript tail). Switch task 1,824 → 1,750 ms, switch median 441 → 435 ms. Style and layout work counters are unchanged (switch styleEls ≈21.8k, layoutObjs ≈12.5k in both), as expected for a JS-only change. The host ran at load ~50, so wall-clock deltas are within noise; the attributed CPU is the reliable signal.Projection invocation counts (300 rows, Lit lifecycle kept, DOM suppressed): initial setup 6 → 2, 20 unrelated updates 20 → 0, 4 route switches 12 → 8. Total 38 → 10 full row projections.
ui/src/components/app-sidebar.projection-memo.test.ts(5 tests, mounted sidebar, spies on the exported projection functions, no production test hooks). Unrelated updates reuse the projection. Route, list, sort, show-cron/system, status and owner filters, agent selection, question and approval attention, agent-status expiry, outbox/draft snapshots, PR summary, archive lifecycle, catalogs, hidden catalogs, child loading, and Home row changes each recompute and render the change. Roster selection and menus update without re-projection. Held subtitles swap at their deadline without re-projecting rows. Fails on base (four unrelated updates → four projections). Cost: ~2.6 s test time locally (node scripts/run-vitest.mjs <file> --maxWorkers=1), 3.45 s runner wall on Testbox.tbx_01m3n6hbd050gjt0c8bmwc184h(blacksmith-testbox, viascripts/crabbox-wrapper.mjs): 51 sidebar/outbox/shell unit files, 1,122/1,122 passed. Sidebar browser tests 86/86. Sidebar e2e (27 files) 135/135.check-changed: typecheck core/UI/tests, tsgo boundary, line-cap and dead-export ratchets green. Its lint step flagged 5 findings in this change, fixed afterward, and the repo oxlint wrapper is clean on the changed files.tbx_01m3n3913gbc43dkbe5588wq71:sidebar-roster.e2efailed its 8 px spacing assertion (measured 7.9999 px) on both base and branch. It passed on the later lease, so it is environmental, not caused by this change.sidebar-cached-list-stability.e2eneeded Playwright's FFmpeg on the lease.0cd53b77e50bon fresh Testbox leases:app-sidebar.projection-memo,app-sidebar.people, andapp-sidebar.sessionstests 297/297 (tbx_01m3nbqma3wmb4gxh4ct9bfzk0);node scripts/check-changed.mjsgreen (tbx_01m3ncehbq8ag5xyb45wbjhkk0; format and lint scoped to exactly the 25 changed files).