Repository navigation
improve: keep Gateway responsive while loading session history - #147363
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: blocked before merge. Reviewed September 13, 2026, 5:49 PM ET / 21:49 UTC (Revision 3). ClawSweeper reviewWhat this changesMoves disk-backed session-history reads and display preparation into a bounded worker, preserving local incognito and CLI-import behavior and rechecking access before publishing results. Merge readiness⛔ Blocked before merge - 1 item remains This PR remains useful and has no identified blocking correctness defect. The final rebase preserves cold-read recovery, and the discussion explicitly accepts the resource tradeoff raised in the previous review. Priority: P2 Review scores
Verification
How this fits togetherThe Gateway serves conversation history to RPC and HTTP clients from SQLite transcripts. History preparation produces display-ready messages, while the Gateway retains responsibility for storage recovery, current profile information, and permission checks. flowchart TD
A[RPC and HTTP history requests] --> B[Select history source]
B --> C[Incognito or CLI local reader]
B --> D[Bounded history worker]
E[SQLite transcripts] --> D
D --> F[Prepared display page]
C --> F
F --> G[Gateway profile and access checks]
G --> H[History response or retryable error]
Before merge
Agent review detailsSecurityNone. PR surfaceSource +467, Tests +705, Docs +2. Total +1174 across 58 files. View PR surface stats
Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Retain one bounded history worker and the shared page kernel, with Gateway-owned recovery and final access checks under the documented, accepted resource tradeoff. Do we have a high-confidence way to reproduce the issue? Yes for the main-thread history-work mechanism: current main performs SQLite reads and display preparation through local async APIs, and the supplied paired measurements exercise that workload. This review did not reproduce the original multi-minute incidents. Is this the best way to solve the issue? Yes. Reusing the existing page kernel and worker infrastructure moves expensive preparation off the Gateway thread while preserving storage and publication ownership. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 7944cd5c8263. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (2 earlier review cycles)
|
0dd2ab2 to
6fee6b0
Compare
|
The measured resource tradeoff is accepted for this change: a single bounded history worker improves large-read responsiveness, with the documented cold-start, RSS, distinct-request CPU, and small-page tail costs. The PR body keeps those costs visible; it does not claim uniformly faster requests or measured suitability for smaller hosts. The only merge conflict was main independently landing the async cold-transcript retry repair. It is resolved in 6fee6b0, preserving the read-only worker path and main's retry behavior. The other 57 PR-owned files and package/lock inputs are unchanged. All 28 targeted integration checks pass, and a fresh complete-branch independent review has no actionable P0–P2 findings. Exact-head CI is running: https://github.com/openclaw/openclaw/actions/runs/34784416641 . @clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
Merged via squash.
|
…law#147363) * perf(gateway): offload session history page preparation * perf(gateway): narrow history worker dependencies and contracts
Related: #138042
Builds on: #147234
What Problem This Solves
Loading large or concurrent session-history pages occupies the Gateway event loop with SQLite reads and display preparation, delaying other requests.
User Impact
History preparation leaves more time for the Gateway to serve other requests. Equivalent queued requests share work; later reads take fresh snapshots. Incognito and bound CLI history retain their local behavior. No configuration, schema, or transcript-format change is required.
This has a resource tradeoff: measured cold history reads take 424–477 ms, versus 10–34 ms locally, and can recur after the existing 60-second worker idle retirement. Warm whole-process RSS is about 270 MiB higher. Distinct requests use more CPU, and small-page tail latency can increase. Large-page responsiveness improves substantially; this does not eliminate other Gateway stalls. The bounded worker is an intentional resource tradeoff for responsiveness during history bursts.
Why This Change Was Made
Use one page-preparation implementation for local and worker reads, returning completed display pages across the worker boundary. Coalesce only equivalent queued requests and charge every retained caller, including canceled callers until settlement. Current profile overlays, cold restoration, reconciliation, and final identity/access checks remain with their existing Gateway owners. Worker failures produce explicit retryable responses.
Move synchronous preview preparation out of the history read module, load CLI history support only for bound sessions, and share pure error-presentation functions without importing the live provider/plugin loader. Shared worker data types live in a leaf contract, avoiding a dependency cycle through Gateway implementations. Superseded raw-snapshot and duplicate test helpers are removed.
Evidence
Three alternating baseline/candidate process pairs on the same Linux Testbox (4 available CPUs, Node 24.19.0). Each process contains 1,200 synthetic messages of 1,024 characters, five warm samples per workload, and eight concurrent requests. All 972 measured responses match expected contents; append/reset checks and process cleanup pass.
CPU changes use the median effect across the three matched process pairs; the first baseline process was noisier. CPU includes workers and readiness probes and is sampled in 10 ms kernel ticks. RSS is sampled at batch endpoints, not measured as a peak. Small-page p95 latency is mixed: equivalent pages 26.5 → 45.2 ms; distinct pages 28.5 → 31.2 ms.
The earlier worker version took 535–621 ms on first reads. The final dependency cut brought this to 424–477 ms on the reused host, a directional comparison across successive runs. It retains an explicit cold-start cost.
The real Gateway proof passes all 12 API cases with 466 synthetic writes and zero history retries: RPC/startup pages, exact anchors, UTF-8/byte budgets, JSON/SSE agreement, cursor/reset behavior, and persistence across process replacement. The combined workload completes all 400 history reads, 12 turns, 40 session updates, and expected control-plane probes. Its history p95 is 467 ms and maximum 585 ms; rolling event-loop samples still report pressure. The repaired built-CLI SQLite lifecycle proof also passes. These use synthetic provider responses with real Gateway/storage/transports, with memory indexing disabled in the history measurement; no browser-rendering or external-provider claim is made.
Remote proof: https://github.com/openclaw/openclaw/actions/runs/34779458989 . All Gateway processes and the owned Testbox lease are stopped and independently verified. The measured staged source tree is
e05403707184f99aa3d3b79d152b1e7188b64322, based ona250dd68; it includes the final presentation cut. Subsequent repairs move types without changing emitted production JavaScript and update test observation points. Commit0dd2ab2bdc583bc7778d7698aadfbd8db3fe2d22is bound to this evidence: the four changed production modules emit identical JavaScript, the other measured runtime sources are byte-identical, and the new type contract has no runtime imports or statements. The final rebase,6fee6b081992b4f27862a07d35a6350df7d0a82b, keeps 57 of 58 PR-owned files byte-identical and combines the worker read-only option with main’s independently landed async cold-retry fix. Package and lockfile are unchanged; current-main integration has separate checks rather than a claim that all transitive inputs were unchanged.Local validation includes 290 worker/CLI/Talk/profile cases, then 262 presentation/classification/history cases. Independent reviews found no actionable P0–P2 issues. CI exposed stale profile-reader mocks, a main-thread spy that cannot observe worker projection, a type-only dependency cycle, and a proof assuming a concurrent reset/read always succeeds. The repairs preserve assertions, move the linear-work guard onto the shared read kernel, break the type cycle with the shared contract, and verify fresh reset/deletion history after the exact retryable superseded-read outcome. The final repair pass passes all 240 cases in the nine affected files and all selected type, lint, cycle, SQLite, and dead-export checks. The sparse-work guard was shown to reject a repeated-prefix projection mutation (5,625 rows against a 3,000 limit), then passed after exact source restoration. After that main refresh, all 28 selected cold-reader, storage, and native-worker cases pass, and a fresh complete-branch review is scoped-clean through P2. Exact-head CI passed for
6fee6b081992b4f27862a07d35a6350df7d0a82b: https://github.com/openclaw/openclaw/actions/runs/34784416641 .