Repository navigation
improve: reduce repeated work when loading session history - #146607
Conversation
|
🦞👀 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 12, 2026, 10:59 PM ET / September 13, 2026, 02:59 UTC (Revision 6). ClawSweeper reviewWhat this changesThe PR reduces session-history loading overhead by reusing compiled SQLite queries, sharing pagination calculations, and removing unnecessary array construction. Merge readiness✅ Ready for maintainer review The PR remains useful beyond the merged history-query optimization. Source review found no actionable defects, and the supplied measurements support its bounded performance claims. Priority: P2 Review scores
Verification
How this fits togetherSession-history readers turn stored SQLite transcript events into bounded message pages for Gateway clients. They preserve reset boundaries, byte limits, cursors, and active-branch metadata. flowchart TD
A[History request] --> B[Transcript read snapshot]
B --> C[Visible message ranges]
C --> D[Reused query compilation]
D --> E[Fresh session and page bindings]
E --> F[SQLite metadata and payload reads]
F --> G[Messages and pagination metadata]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +95, Tests +54, Other 0. Total +149 across 9 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep query compilation connection-scoped while preserving fresh bindings, snapshot ownership, private iterators, and separate metadata sizing. Do we have a high-confidence way to reproduce the issue? Not applicable as a functional bug: this is a performance optimization, supported by paired actual-reader measurements rather than a failing behavior reproduction. Is this the best way to solve the issue? Yes. Reusing the existing binding compiler and iterator owner avoids a competing execution path while preserving SQL selection and pagination semantics. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 9ac1ceb51537. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (5 earlier review cycles)
|
e88e912 to
c0847b8
Compare
6344669 to
4854ae8
Compare
|
Merged via squash.
|
Related: #146390
What Problem This Solves
Loading and paging session history repeats query construction and array work even when each request returns only a small number of messages.
User Impact
Warm ordinary history reads use 21–31% less median wall time and 25–34% less CPU in paired measurements. The surrounding Gateway-tail operation improves about 6–9% in median wall time for ordinary histories. Messages, byte limits, paging, cursors, and authoritative active-leaf metadata are preserved. No schema change, new configuration, migration, or operator action is required.
Why This Change Was Made
Separate numeric range descriptions from SQL construction and reuse the two compiled Kysely range queries on each database connection. Every invocation binds current values; payload iterators retain private native statements and their existing lazy execution and cleanup. Sparse reset batches keep their existing selection, and metadata sizing remains separate from payload loading.
Current and historical anchors share one page-range calculation, visible delta rows reuse the existing parser, and compactions avoid an identity Set/filter and per-record singleton arrays. The SQLite Gateway adapter stops constructing an unused transcript-events array; archive adapters retain their existing fallback data.
Evidence
Paired actual-reader measurements use Node 24.21.0 / SQLite 3.53.4, 100 warmups, and 32 alternating batches of 50 reads per variant. Nine frozen reader modules from main f1c1f27 provide the baseline; both variants share unchanged dependencies and the synthetic database owner in the same process. Both include the upstream marker-search improvement. Values are medians of per-read batch timings.
Ordinary direct reads still execute three SQL statements, but repeated Kysely compilation falls from two calls to zero. The Gateway-tail operation retains eight statements and goes from three compiler calls to one. SQL text, statement order, bindings, JSON parse counts, and parsed bytes match exactly across all 12 ordinary, marker, and reset cases. Direct results are deeply equal. Gateway comparisons explicitly verify the sole internal shape difference—the retired transcript-events array—and exact equality of every remaining field.
The 10,000-event full-history guard is effectively flat in median wall time (36.640 to 36.695 ms); CPU time is 40.864 to 41.541 ms. Results and decoded bytes remain identical. Reset recent/offset reads improve roughly 8–16%; marker-heavy cases are approximately flat. Some p95 values worsen, including the full-history guard's 38.134 to 46.669 ms across 12 single-read batches. These measurements cover warm backend reads; database opening, first-use compilation, and complete RPC/browser latency are outside the timing claims.
All 169 focused loader tests pass across 13 files. Complete changed-file checks pass, including production/test types, core/script lint, dead-export scans, formatting, and architecture guards. Independent Codex P2 review is scoped-clean. The parser cleanup removes one duplicate assertion, and its baseline count shrinks accordingly.
CI exposed two separate fixture problems, both now repaired upstream. The branch includes the upgrade-cohort argument fix in #146606 and the primary-provider request synchronization fix in #146644. Our equivalent catalog-test patch was dropped during conflict resolution, keeping one upstream implementation. The loader optimization rebased patch-identically onto main 65db526, and all six measured production files retain their benchmarked hashes. The existing cross-session/page-bounds assertions remain alongside #146614's shared fixture constructors.
All 26 tests across the overlapping history-events and catalog-freshness fixtures pass after the refresh. Exact-head hosted CI passed for 4854ae8: https://github.com/openclaw/openclaw/actions/runs/34733562981. Fresh ClawSweeper review of this head found no actionable defects.