Repository navigation
perf(gateway): show the first chat sooner after a Gateway start - #162936
Conversation
The history worker reached the full session lookup/runtime graph through source candidate selection. Branch snapshots also imported their host list coordinator and writer-side reexports. Extract the shared candidate owner and isolate branch snapshots/cache from host coordination without changing selection, admission, watermark reuse, restoration, or cleanup. Prioritize complete history preparation, including the response encoder, and allow only this worker operation during foreground browser loading. Main-thread handler/optional preparation remains idle-only. Preserve the post-ready barrier, root admission, shutdown joins, and worker custody; ready never awaits this work. No schema, protocol, public SDK, configuration, dependency, persistence, or update behavior changes. Reproduction: three isolated seeded starts on Node 26, zero added RTT. Cold chat.startup server ms: 2020,586,2041; visible ms: 4440,2246,3739. Warm server ms: 12,12,12; visible ms: 544,12098,16714. The last two warm outliers occurred before WebSocket connection, not in chat.startup. Four interleaved rounds against a preserved build of main 83a9740, same worktree/runtime paths and cloned synthetic fixtures (1500 turns plus 30 x 60 turns), fresh browser per load, immediate navigation after ready: - Main cold server/visible ms: 647/2356,751/2557,556/2015,1034/2893. - Candidate cold server/visible ms: 59/2059,51/1644,55/1760,89/1877. - Main warm server/visible ms: 11/456,14/472,13/546,12/484. - Candidate warm server/visible ms: 11/503,13/493,13/596,13/505. Cold medians: server 699 -> 57 ms (-91.8%); visible 2456.5 -> 1818.5 ms (-26.0%). Warm medians: server 12.5 -> 13 ms; visible 478 -> 504 ms. Branch RPC cold medians: 657.5 -> 572.5 ms. CPU diagnostics aligned to the first chat.startup server interval: baseline 1171.4 ms under history-worker module loaders in a 1278 ms request; candidate 0 ms in a 71 ms request. Candidate loading instead sampled 2635.7 ms after ready, before the request. Samples are approximate wall-time attribution, not exclusive CPU accounting. Branch worker bootstrap still costs time: the final loaded-host profile sampled 1079 ms loading in a 1930 ms branch RPC. No claim that branch cold loading is eliminated. Full process-to-ready medians were 14352 -> 16372 ms on the shared host; listening-to-ready medians were 674.5 -> 646.5 ms. Warmup remains after ready; total restart-time improvement is not established. Validation: 224 tests across 12 focused files pass. Import-boundary and foreground-prewarm regressions fail before their fixes. Changed-file test wrapper wall costs: session-history-read.imports 10.86s (2 tests), server-startup-handler-prewarm 5.94s (12 tests), session-utils 51.46s (104 existing tests; only its type import moved). The architecture guard checks static worker boundaries; no timed performance assertions were added. Core, core-test, root-test typechecks pass; both import-cycle checks report zero; all five typed core lint stripes pass, with the affected Gateway stripe refreshed after the final change. Full runtime/postbuild/UI builds, UI performance checks, oxfmt, diff checks, and Codex P2 review pass.
|
🦞👀 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 October 1, 2026, 3:19 PM ET / 19:19 UTC (Revision 2). ClawSweeper reviewWhat this changesThe PR isolates session-history reader imports and prioritizes background worker preparation so chat transcripts load sooner after Gateway startup. Merge readiness✅ Ready for maintainer review The PR remains useful beyond the earlier merged prewarm work, and no blocking correctness or security defect was found. Current main still retains the cold-load import and scheduling bottlenecks. Priority: P2 Review scores
Verification
How this fits togetherThe Gateway serves stored chat transcripts to connected clients through a read-only history worker. Startup preparation loads that worker's readers and admits existing session databases before the first foreground history request. flowchart TD
A[Gateway ready] --> B[Prioritized history preparation]
B --> C[Existing database admission]
D[Client opens chat] --> E[History worker]
C --> E
E --> F[Read and encode transcript]
F --> G[Client displays history]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +3, Tests +33, Docs +8. Total +44 across 22 files. View PR surface stats
Review metricsNone. Technical reviewBest possible solution: Keep one shared store-selection owner and prepare the existing history worker after readiness while preserving foreground priority for optional discovery. Do we have a high-confidence way to reproduce the issue? Yes: opening a seeded chat from a fresh browser immediately after Gateway startup is a concrete benchmark path, and current-main source retains the implicated import and idle-gating behavior. This reviewer did not execute that path. Is this the best way to solve the issue? Yes: extracting the shared selection and snapshot owners removes unnecessary imports, while prioritizing the existing prewarm avoids adding a competing runtime or storage path. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 32dc6c861d3e. 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 (1 earlier review cycle)
|
Callers import GatewaySessionStoreDiscoveryCache from its new owner, session-utils-store-candidates.ts; the session-utils barrel re-export had no consumers and failed the production deadcode check.
|
Merged via squash.
|
* origin/main: fix(gateway): report approved-but-narrowed scope upgrades truthfully (#162951) fix: allow session-scoped shared publication reads (#162984) refactor(sessions): share session creation orchestration (#161336) perf(gateway): show the first chat sooner after a Gateway start (#162936) refactor(scripts): share npm verification command execution (#162978) fix(browser): clear MCP runtime cache after profile tests (#162982) fix(test): checkout fixture tests fail healthy runs on loaded macOS hosts (#162888) ci: bound PR extension lint to direct owners and smoke packages fix(ci): await native declaration discovery teardown feat(qa): drive forwarded text+photo bursts in Telegram userbot scenarios (#162906) build(macos): pin the app runtime to OpenClaw Bun 17c9ecf9eb (#162570) fix(test): frozen admission test fails with spawn ETXTBSY when a sibling test forks during the native parser copy (#162969) fix(ci): give direct published-driver callers the CI cell envelope (#162837) test(gateway): remove low-value tests (batch d136) (#162921)
…claw#162936) * perf(gateway): prepare history workers before the first chat read The history worker reached the full session lookup/runtime graph through source candidate selection. Branch snapshots also imported their host list coordinator and writer-side reexports. Extract the shared candidate owner and isolate branch snapshots/cache from host coordination without changing selection, admission, watermark reuse, restoration, or cleanup. Prioritize complete history preparation, including the response encoder, and allow only this worker operation during foreground browser loading. Main-thread handler/optional preparation remains idle-only. Preserve the post-ready barrier, root admission, shutdown joins, and worker custody; ready never awaits this work. No schema, protocol, public SDK, configuration, dependency, persistence, or update behavior changes. Reproduction: three isolated seeded starts on Node 26, zero added RTT. Cold chat.startup server ms: 2020,586,2041; visible ms: 4440,2246,3739. Warm server ms: 12,12,12; visible ms: 544,12098,16714. The last two warm outliers occurred before WebSocket connection, not in chat.startup. Four interleaved rounds against a preserved build of main 1eea3b8, same worktree/runtime paths and cloned synthetic fixtures (1500 turns plus 30 x 60 turns), fresh browser per load, immediate navigation after ready: - Main cold server/visible ms: 647/2356,751/2557,556/2015,1034/2893. - Candidate cold server/visible ms: 59/2059,51/1644,55/1760,89/1877. - Main warm server/visible ms: 11/456,14/472,13/546,12/484. - Candidate warm server/visible ms: 11/503,13/493,13/596,13/505. Cold medians: server 699 -> 57 ms (-91.8%); visible 2456.5 -> 1818.5 ms (-26.0%). Warm medians: server 12.5 -> 13 ms; visible 478 -> 504 ms. Branch RPC cold medians: 657.5 -> 572.5 ms. CPU diagnostics aligned to the first chat.startup server interval: baseline 1171.4 ms under history-worker module loaders in a 1278 ms request; candidate 0 ms in a 71 ms request. Candidate loading instead sampled 2635.7 ms after ready, before the request. Samples are approximate wall-time attribution, not exclusive CPU accounting. Branch worker bootstrap still costs time: the final loaded-host profile sampled 1079 ms loading in a 1930 ms branch RPC. No claim that branch cold loading is eliminated. Full process-to-ready medians were 14352 -> 16372 ms on the shared host; listening-to-ready medians were 674.5 -> 646.5 ms. Warmup remains after ready; total restart-time improvement is not established. Validation: 224 tests across 12 focused files pass. Import-boundary and foreground-prewarm regressions fail before their fixes. Changed-file test wrapper wall costs: session-history-read.imports 10.86s (2 tests), server-startup-handler-prewarm 5.94s (12 tests), session-utils 51.46s (104 existing tests; only its type import moved). The architecture guard checks static worker boundaries; no timed performance assertions were added. Core, core-test, root-test typechecks pass; both import-cycle checks report zero; all five typed core lint stripes pass, with the affected Gateway stripe refreshed after the final change. Full runtime/postbuild/UI builds, UI performance checks, oxfmt, diff checks, and Codex P2 review pass. * fix(gateway): drop unused discovery-cache type re-export Callers import GatewaySessionStoreDiscoveryCache from its new owner, session-utils-store-candidates.ts; the session-utils barrel re-export had no consumers and failed the production deadcode check.
What Problem This Solves
The first chat load after a Gateway start waits on the history worker loading modules:
chat.startupspent about 1.2 s importing the session lookup and runtime graph before running a ~50 ms history read.User Impact
User impact: opening a chat right after the Gateway starts or restarts shows the transcript sooner. In interleaved restart pairs at 0 ms added RTT, first-load
chat.startupdropped from a 699 ms to a 57 ms median and time to visible transcript from 2,456.5 ms to 1,818.5 ms. Warm loads, history results, admission, and readiness timing are unchanged.Why This Change Was Made
The history reader reached the full host session lookup/runtime graph only to pick candidate stores; that candidate selection now lives in its own small owner shared by host lookups and the worker. Branch snapshots and their watermark cache are likewise separated from host-side list coordination and archive restoration, so the branch worker no longer loads writer-side modules. The existing post-ready prewarm now prepares the complete history path first (readers, response encoder, read-only admission) and may overlap foreground browser loading; other prewarm items stay idle-gated, and readiness never awaits any of it. Database custody, admission, cleanup, and result validation are unchanged.
Evidence
Four interleaved restart rounds against a build of main
83a974025cbbwith identical synthetic sessions (1,500 turns plus 30 × 60 turns), fresh browser profiles, and bundled UI (ms):chat.startup(server)chat.startup(server)chat.startup: history-worker module loading fell from 1,171 ms to 0 ms (it now completes after ready, before the request). Coldsessions.branches.listmedian 657.5 → 572.5 ms; branch worker bootstrap still costs time and is not claimed fixed.--maxWorkers=1):session-history-read.imports10.86 s (2 tests),server-startup-handler-prewarm5.94 s (12 tests);session-utils.test.ts(51.46 s, 104 existing tests) only had a type import moved.