Skip to content

perf(gateway): skip redundant startup history prewarming - #184438

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-session-transcript-boot-pool-20261026
Oct 26, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-session-transcript-boot-pool-20261026

Conversation

@steipete

@steipete steipete commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Startup can submit another history prewarm after recovery has already activated the foreground history pool. That repeats store discovery and can start an extra reader while the first reader is busy.

User Impact

Avoid redundant startup work while preserving cold-history preparation, cancellation, and restart recovery. Recovery scheduling and reader concurrency limits are unchanged. Other configured databases that have not been read yet still open through normal first-read admission.

Why This Change Was Made

Make cold-only prewarming the operation's default and remove the private onlyIfCold switch. The authenticated connection path already requested this behavior. Each foreground history worker loads the history modules at creation, including workers first activated by recovery, so an active pool does not need a separate startup warmup.

This is a small cleanup of startup demand, not a reduction of every transcript pool. Main-session orphan marking and resumption already run serially; discovery, projection, maintenance, usage, and model-context work retain their separate owners.

Evidence

The existing warm-store test now calls the default API and fails on the baseline because it discovers/prewarms stores despite an active history pool. It passes with the change. The other cold-store/error/cancellation cases and connection/startup scheduling tests pass: 71 tests across three files. All 193 existing main-session restart-recovery tests pass (305.07 seconds test-file duration).

Real-owner microbenchmark, AB/BA, 3,000 calls per arm, two samples per revision, 1,000-session synthetic database, native allocation counter (no allocation sampling or forced GC):

Main-thread cost per 3,000 warm calls Base Candidate Change
Allocated bytes 70,534,388 2,497,168 −96.46%
CPU 89.783 ms 0.745 ms −99.17%
Wall time 119.136 ms 0.884 ms −99.26%

These percentages apply only to redundant warm calls, not total Gateway CPU. Reproduce with node --import ./scripts/tsx.mjs scripts/perf/session-history-prewarm-measure.mjs --state-dir <synthetic-fixture> --iterations 3000; baseline uses --source-file <frozen-baseline-owner.ts>.

Boot/recovery comparison: three interleaved pairs (AB, BA, AB), separately built baseline 2499f2be3e88 and the same source with this patch. Each fresh fixture has 10,000 sessions, four agents, 3,981 settled subagent runs, 8 MiB of transcript text, 65 WebSocket clients, and four real mock-provider turns interrupted by killing the owned Gateway. The recovery window is 75 seconds from restart spawn, including startup and reconnect polling. All 24 interrupted turns resumed; zero connection/RPC errors.

Pair / arm Ready All 4 turns resumed Transcript workers started RSS high-water Main CPU through ready Main allocation through ready Host load (1m)
1 base 16.416 s 20.421 s 7 2819.5 MiB 9.698 s 6106.7 MiB 24.6
1 candidate 17.885 s 23.229 s 7 2730.3 MiB 9.724 s 5864.7 MiB 24.2
2 candidate 15.398 s 20.169 s 7 2941.7 MiB 8.613 s 5619.5 MiB 25.1
2 base 14.522 s 18.725 s 7 2889.9 MiB 8.816 s 6251.6 MiB 23.5
3 base 19.112 s 24.150 s 7 2724.1 MiB 9.942 s 5625.1 MiB 35.0
3 candidate 14.457 s 19.472 s 7 2808.0 MiB 8.352 s 5743.3 MiB 29.2

Mean recovery time: 21.099 → 20.957 s (−0.142 s, −0.67%). RSS high-water: 2811.2 → 2826.7 MiB (+15.5 MiB, +0.55%). The memory/latency changes are mixed across pairs; no overall speed or memory win is claimed. Main CPU/allocation through the first one-second sample after readiness averaged 9.485 → 8.897 s / 5994.5 → 5742.5 MiB; these include cold module loading and are diagnostic. macOS native resourceUsage().maxRSS is reported, not Linux /proc VmHWM.

The reliable work-count win is four prewarm tasks → zero in every boot. Both arms had five transcript workers by readiness, seven started during recovery, and one burst worker retired before teardown. First-boot concurrent turns reached eight workers in both arms. Real history work still needs the second history worker: this patch does not shrink the aggregate pool budget.

Client sessions.list p99, base/candidate: 545/621 ms (799/780 samples), 511/577 ms (780/780), and 463/915 ms (726/780). Every arm is below the 1,000-sample tail gate; these are context, not an adverse verdict or a throughput claim. This targeted cold-start deletion used the short recovery proof rather than a ten-minute steady-state campaign. Server-handler latency, GC, and event-loop p99 were not measured; no conclusions about those metrics are drawn. No shared or production Gateway was used.

Reproduce each fresh arm with scripts/perf/team-shape-fixture.ts --sessions 10000 --runs 3981 --settled-runs --team-stress --transcript-mib 8 --approvals 0 --cron 0 --projects 0, followed by scripts/perf/post-restart-burst-measure.ts --state-dir <fixture> --entry dist/entry.js --port 41825 --mock-port 41826 --clients 65 --runs 4 --seconds 75 --poll-ms 5000 --worker-census (both invoked with node --import ./scripts/tsx.mjs; fixture also requires --state-dir and --port). Alternate the complete built dist trees at the same entry path, only after the previous Gateway exits. The source rebase was patch-identical; broad proof is exact-head PR CI.

Format, changed-file lint, export-collision guard, and both eager-import/PR-wrapper closure guards pass (26 closure tests). Independent subagent review (autoreview route unavailable): clean after fixing a benchmark observer startup-listener issue. The corrected observer waits for the real server's message listener; a delayed-listener smoke verifies startup-port delivery, callback removal, and payload omission.

Runtime production change: net −1 line; non-test diff including performance scripts is +295/−3 lines, of which 293 additions are measurement tooling. Added benchmark code records native worker births/tasks, main-thread CPU/allocation, RSS high-water mark, host load, and time until all interrupted requests resume; it is opt-in tooling and adds no Gateway runtime path. The shared performance README is unchanged.

Use cold-only history prewarming at the owner and remove the private caller switch. Recovery already activates fully prepared foreground readers; real reads keep their normal database admission and concurrency. Add synthetic warm-owner and crash/restart worker census measurements.
@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-26T09:07:15.211569Z 5887a49 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Security sensitive changes

This maintainer PR changes sensitive security components.

No secops approval is required. This comment is informational because the PR author has Maintain or Admin access.

  • Current SHA: 5887a496622fc3c7c62e8dded88faf7bb73e7a0d
  • Maintainer: @steipete
  • Repository role: admin

These security sensitive changes were made:

  • src/gateway/server/ws-connection/connect-session.ts: Controls Gateway authentication, pairing, or caller permissions. Check identity validation, scope checks, and revocation.

Carefully review these changes before merging.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime scripts Repository scripts size: M labels Oct 26, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 26, 2026
@clawsweeper

clawsweeper Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Make Gateway history prewarming cold-only by default and add opt-in startup measurement tooling.

Example: Restart a Gateway with four configured agents after recovery has activated the foreground history pool.

  • Before: Startup submits four additional prewarm tasks.
  • After: Startup submits zero additional prewarm tasks; normal history reads retain database admission.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-level deletion with relevant shipped-entry-point evidence; limited samples support the work-count result rather than broad performance conclusions.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The supplied full-body results exercise built isolated Gateways through crash, restart, WebSocket reconnects, and real agent execution against a synthetic HTTP model provider; candidate boots eliminate redundant prewarm tasks while every interrupted turn resumes, and the author identifies the patch-identical source rebase and limits the performance claims.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Gateway restarts can repeat history preparation after recovery has already activated the foreground readers.
Reason: The feature-history owner supplied this direction and measured a repeatable work-count reduction without changing reader limits or claiming an unsupported overall speedup.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR removes verified redundant startup work without changing reader admission or concurrency; no actionable defect was found.

Priority: P2
Reviewed head: 5887a496622fc3c7c62e8dded88faf7bb73e7a0d

Before merge

None.

Findings

None.

Agent review details

How this fits together

Gateway startup and authenticated operator connections request best-effort history preparation; the transcript worker owner admits databases and serves subsequent history reads.

flowchart TD
  A[Gateway startup] --> C[History prewarm owner]
  B[Authenticated operator connection] --> C
  C --> D[Foreground pool cold check]
  D --> E[Skip redundant preparation]
  D --> F[Discover configured databases]
  F --> G[Existing worker admission]
  H[Normal history reads] --> G
Loading

Technical review

Best possible solution:

Keep the cold-only decision in the existing prewarm owner while preserving normal first-read admission.

Do we have a high-confidence way to reproduce the issue?

Current-main source shows startup can discover and prewarm configured stores despite an active history lane; the supplied boot measurements corroborate this path, but no target code was executed during review.

Is this the best way to solve the issue?

This is the smallest runtime fix: reuse the existing pool-cold predicate at its current owner, remove the redundant caller option, and retain the existing regression scenarios.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 54408a978fc9.

Provenance checked

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Pinned introduced change: The introduced diff moves the existing cold check into the prewarm owner's default contract and removes the caller switch; it leaves cancellation and database preparation unchanged. (src/gateway/server-history-prewarm.ts:24, 5887a496622f)
  • Existing preparation and admission owners: The foreground lane already enables prewarmHistory, worker creation loads the history modules, and normal reads retain cold database admission independently of Gateway prewarming. (src/config/sessions/session-transcript.worker.ts:40, 5887a496622f)
  • Current main still performs redundant startup preparation: Fetched main retains the optional onlyIfCold switch, while the startup caller supplies only cancellation. (src/gateway/server-history-prewarm.ts:24, 54408a978fc9)
  • Original product intent: perf(gateway): warm the session history worker before the first transcript read #159818 introduced best-effort preparation to avoid slow first transcript reads, preserving normal custody and keeping preparation detached from connection admission. (src/gateway/server-history-prewarm.ts:16, 9ac4735581d6)
  • Supplied production-entry-point evidence: The full PR body reports interleaved isolated built-Gateway crash/restart runs: four prewarm tasks become zero in every candidate boot, all 24 interrupted turns resume, and no connection/RPC errors occur; aggregate latency and memory improvements are expressly unclaimed. (5887a496622f)
  • Current-head gates: The reviewed head is mergeable; CI remains pending without reported failed checks, and the security-sensitive status confirms maintainer authority with no additional SecOps approval required. (5887a496622f)

Likely related people:

  • Peter Steinberger: Raw commit 9ac4735 adds src/gateway/server-history-prewarm.ts:16 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 9ac4735581d6; files: src/gateway/server-history-prewarm.ts)

PR surface

Source -1, Tests -1, Other +293. Total +291 across 7 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 2 3 -1
Tests 2 1 2 -1
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 3 293 0 +293
Total 7 296 5 +291

Review metrics

Metric Value Why it matters
Measurement controls 1 benchmark flag and 1 observer environment variable --worker-census and OPENCLAW_BENCH_TRANSCRIPT_BOOT_DIR enable opt-in measurement without adding a Gateway configuration setting.

Labels

Label changes:

  • add P2: Removes bounded redundant startup preparation without establishing an urgent user-facing outage.
  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: Removes bounded redundant startup preparation without establishing an urgent user-facing outage.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 26, 2026, 5:24 AM ET / 09:24 UTC.

@steipete
steipete merged commit 79798ed into main Oct 26, 2026
140 of 141 checks passed
@steipete
steipete deleted the steipete/perf-session-transcript-boot-pool-20261026 branch October 26, 2026 09:32
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 27, 2026
…4438)

Use cold-only history prewarming at the owner and remove the private caller switch. Recovery already activates fully prepared foreground readers; real reads keep their normal database admission and concurrency. Add synthetic warm-owner and crash/restart worker census measurements.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. scripts Repository scripts security-sensitive-changed size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant