Repository navigation
perf(ui): reuse normalization and tool turns while loading older history - #160215
Conversation
Memoize finite-timestamp normalization by immutable message identity and reuse unchanged message-only turns across older-page prepends. Copy output wrappers so duplicate and grouping annotations cannot poison cached turns; keep transient turns and time-dependent normalization on the existing path. Align grouping fixtures with replacement-based message ownership. Prove normalization counts, seam-only coalescing, frozen-output safety, and mixed cold/warm build equivalence before and after prepends. Control UI release-note context: reduce repeated work while loading older chat history without changing message order, grouping, or live presentation. Validation: 1075 tests across 58 files; UI typecheck, formatter, whitespace; Codex review through P2 clean. Lite paging sanity: three complete runs.
Keep one latest coalesced turn per owner message in a long-lived WeakMap. Alternating split panes and other transcript builds no longer evict each other's cached work; changed inputs replace only their owner's entry. Replace the empty-pass eviction assertion with extraction-count coverage for interleaved transcripts, empty builds, and same-owner replacement. Control UI release-note context: preserve history paging cache reuse while multiple chat panes or transcript surfaces render. Validation: 391 tests across 13 files, UI typecheck, formatter, whitespace, and full-branch Codex review through P2.
|
🦞👀 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 28, 2026, 1:54 PM ET / 17:54 UTC (Revision 4). ClawSweeper reviewWhat this changesThe Control UI reuses normalized messages and unchanged tool turns while loading older chat history, with tests for replacement, grouping, and cold-versus-warm output. Merge readiness✅ Ready for maintainer review Keep this PR open. Current main still repeats message normalization and tool-turn coalescing during history rebuilds; the earlier merged paging optimization addressed a different parsing cost. The branch has focused coverage and measured browser-paging evidence, with no concrete correctness finding. Priority: P2 Review scores
Verification
How this fits togetherThe Control UI receives chat-history pages from the Gateway and turns them into ordered message, tool, and live-status rows. Normalization and tool-turn coalescing run before the chat view groups and renders those rows. flowchart LR
A[Gateway history pages] --> B[Reconciled messages]
B --> C[Normalization reuse check]
C --> D[Normalized messages]
D --> E[Tool-turn reuse check]
E --> F[Grouped chat rows]
F --> G[Chat view]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +51, Tests +381. Total +432 across 9 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep identity reuse in the normalization and coalescing owners, with message replacement as the invalidation contract and cold-versus-warm transcript coverage at the chat-build boundary. Do we have a high-confidence way to reproduce the issue? Yes. The PR documents a concrete Chromium paging scenario with a 6,972-message session, and current-main source shows the repeated work; this read-only review did not execute that scenario. Is this the best way to solve the issue? Yes. The caches sit at the existing preparation owners, while ordered-input checks, transient bypass, and replacement-based tests address the relevant invalidation paths. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 01747d9b2b11. LabelsLabel changes: No label changes. Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (3 earlier review cycles) |
The normalization cache relies on history owners replacing edited messages. Keep the unchanged question object and replace the edited response, preserving the navigation-visibility assertions.
|
CI note for run 36452341516 (head 476b635):
|
Copy returned turn wrappers in a loop instead of spreading inside map, store fresh coalesced output without an extra copy, and use bracket access for OpenClaw metadata fields.
|
Merged via squash.
|
…ory (openclaw#160215) * perf(ui): reuse history normalization and coalesced turns Memoize finite-timestamp normalization by immutable message identity and reuse unchanged message-only turns across older-page prepends. Copy output wrappers so duplicate and grouping annotations cannot poison cached turns; keep transient turns and time-dependent normalization on the existing path. Align grouping fixtures with replacement-based message ownership. Prove normalization counts, seam-only coalescing, frozen-output safety, and mixed cold/warm build equivalence before and after prepends. Control UI release-note context: reduce repeated work while loading older chat history without changing message order, grouping, or live presentation. Validation: 1075 tests across 58 files; UI typecheck, formatter, whitespace; Codex review through P2 clean. Lite paging sanity: three complete runs. * fix(ui): retain turn caches across transcript surfaces Keep one latest coalesced turn per owner message in a long-lived WeakMap. Alternating split panes and other transcript builds no longer evict each other's cached work; changed inputs replace only their owner's entry. Replace the empty-pass eviction assertion with extraction-count coverage for interleaved transcripts, empty builds, and same-owner replacement. Control UI release-note context: preserve history paging cache reuse while multiple chat panes or transcript surfaces render. Validation: 391 tests across 13 files, UI typecheck, formatter, whitespace, and full-branch Codex review through P2. * test(ui): publish edited position messages as replacements The normalization cache relies on history owners replacing edited messages. Keep the unchanged question object and replace the edited response, preserving the navigation-visibility assertions. * fix(ui): satisfy lint in coalesced turn caching Copy returned turn wrappers in a loop instead of spreading inside map, store fresh coalesced output without an extra copy, and use bracket access for OpenClaw metadata fields.
…every render (#162092) Pane renders repeatedly normalized both operands for every roster comparison, and abortability could scan the roster twice. Every stream delta also filtered the complete transcript to find persisted assistant messages for the active run. Bound the pure session-key memo to 4,096 inputs, precompute the fixed default main alias, and preserve false equivalence for blank keys. Share one roster pass with one normalized target. Cache persisted assistant filtering by immutable history-array identity and the latest run, using weak retention; keep all stream-dependent reconciliation live. This builds on immutable message replacement in #160215 and identity-keyed derivations in #162035. Microbenchmarks against a58e5ba, Node v24.21.0, median of seven warm samples after a first burst: - hasAbortableSessionRun, 1,500 rows x 100 calls: 38.083 -> 3.471 ms. - reconcilePersistedAssistantStream, 3,000 messages x 300 deltas: 44.310 -> 0.309 ms. First-burst times were 44.558 -> 4.302 ms and 49.742 -> 3.432 ms. No production perf rig was used. Protect bounded memo reuse, blank/opaque-key semantics, single-pass roster checks, and history-cache invalidation on run/array changes without test-only production hooks or timers. Control UI behavior remains unchanged.
…every render (openclaw#162092) Pane renders repeatedly normalized both operands for every roster comparison, and abortability could scan the roster twice. Every stream delta also filtered the complete transcript to find persisted assistant messages for the active run. Bound the pure session-key memo to 4,096 inputs, precompute the fixed default main alias, and preserve false equivalence for blank keys. Share one roster pass with one normalized target. Cache persisted assistant filtering by immutable history-array identity and the latest run, using weak retention; keep all stream-dependent reconciliation live. This builds on immutable message replacement in openclaw#160215 and identity-keyed derivations in openclaw#162035. Microbenchmarks against af6e033, Node v24.21.0, median of seven warm samples after a first burst: - hasAbortableSessionRun, 1,500 rows x 100 calls: 38.083 -> 3.471 ms. - reconcilePersistedAssistantStream, 3,000 messages x 300 deltas: 44.310 -> 0.309 ms. First-burst times were 44.558 -> 4.302 ms and 49.742 -> 3.432 ms. No production perf rig was used. Protect bounded memo reuse, blank/opaque-key semantics, single-pass roster checks, and history-cache invalidation on run/array changes without test-only production hooks or timers. Control UI behavior remains unchanged.
What Problem This Solves
Fixes: every time an older page of history loads in a long chat, the chat item builder re-normalizes every loaded message and re-coalesces every tool turn, so page-load stalls grow with the history already loaded.
User Impact
User impact: scrolling up through long chats stalls less on each older-history page, especially deep into the history. Order, grouping, live output, and presentation are unchanged.
Why This Change Was Made
After #160030, profiling history paging on a real 6,972-message session showed
buildChatItemsat ~110 ms per page once ~6k messages were loaded. Of that, ~31 ms was normalizing every message again and ~46 ms was coalescing every tool turn again. Already-loaded message objects keep their identity across page loads (verified with instrumentation), so both results can be reused at their owners:message-normalizer.ts):normalizeMessagecaches its result per original message object when the message has a finite timestamp. Messages without one still take the current-clock fallback on every call. An audit (file:line evidence in the review record) found no settings, locale, or other outside inputs, and no consumer that mutates normalized output. Tests freeze cached results to prove that.chat-tool-activity-coalesce.ts): each message-only turn's output is memoized in one long-livedWeakMapkeyed by the turn's first message. Reuse requires identical ordered inputs: kind, key, message, content array and blocks,duplicateCount, andstartsTurn. Turns with live tool streams, streams, notices, or other transient wrappers are never cached. Hits and misses both return fresh shallow wrapper copies, so grouping and duplicate annotations cannot write into the cache. Split panes and other transcript surfaces share the map without evicting each other's entries, which die with their messages.A prepended page recomputes only its own turns and the seam turn. The builder's global passes (placement, canvas, search, live items, grouping) still run over the whole history, but their per-message work is now mostly cache hits.
Test fixtures in
chat-thread-grouping.test.tsandchat-thread.test.tsthat mutated already-admitted message objects in place now publish replacement messages instead, matching how the application's owners update messages (history merge, session projection, tool stream). All their original assertions are unchanged.Evidence
Real 6,972-message session through the mocked Gateway with production paging, bundled Control UI, Chromium 1280×900, scrolling up until all history was loaded. Main (
1fe560c198c2) and this branch were built alternately (main, branch, branch, main, main, branch), 3 runs per build, with a fresh bundle for every invocation.Tests:
chat-thread-incremental.test.ts(new): a mixed fixture (tools, canvas, compaction, duplicates, live streams, reading indicator, queued-input placement, search with tools shown and hidden). Warm builds with the caches equal cold builds from cloned inputs, before and after a prepend. Cached normalized output is recursively frozen.chat-tool-activity-coalesce.test.ts: repeated turns do no extraction work; a prepend recomputes only incoming and seam turns; replacing a message or block invalidates only its turn; mutating a returned wrapper cannot poison the cache; interleaved builds of two transcripts both keep their hits. Fails on main.message-normalizer.test.ts: retained messages normalize once; replacements re-normalize; clock-fallback timestamps still advance under a fake clock. Fails on main.node scripts/run-vitest.mjs run <file> --maxWorkers=1:chat-thread-incremental.test.ts4.0 s warm / 14.8 s cold wall (4 tests, ~28 ms of test time),chat-tool-activity-coalesce.test.ts4.2 s (8 tests),message-normalizer.test.ts2.1 s (80 tests). No real timers or sleeps.pnpm tsgo:ui, oxfmt, andgit diff --check. An independent Codex review of the full branch was scoped-clean.Production lines: +52 / −4. The growth is the two identity-keyed caches, finite-timestamp admission, and the explicit turn-input list. The inputs are listed out on purpose, because an omitted input is how a cache like this goes stale.