Skip to content

improve(startup): start the Gateway faster on the Bun runtime - #169543

Merged
steipete merged 2 commits into
mainfrom
perf/drop-dist-esm-resolve-hook
Oct 12, 2026
Merged

steipete merged 2 commits into
mainfrom
perf/drop-dist-esm-resolve-hook

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

The Gateway and CLI start several seconds slower than necessary on the Bun runtime that the macOS app bundles, on both first start and every restart.

User Impact

User impact: macOS app users (and anyone running OpenClaw on the pinned Bun fork) reach a ready Gateway noticeably sooner; Node installs are unaffected.

Why This Change Was Made

A dist-relative ESM resolve hook (#128541) existed to let Node skip package-scope format detection for relative .js imports. #139754 later emitted core chunks as .mjs, which already declare their format, so the hook now short-circuits about 0.1% of resolutions. Meanwhile the pinned Bun fork gained Module.registerHooks: with any resolve hook registered, Bun sends every resolution through its JS customization-hooks chain instead of its native parallel loader. The hook had become pure overhead on Node and a large serial cost on Bun, so this removes it and its two call sites rather than special-casing Bun.

Evidence

Hook usage for the same gateway + reply-runtime import graph (load-independent counts):

Runtime Resolutions through the hook Short-circuited
Node 26.11.1 28,038 30
Bun fork 1.4.3-canary.1+65d94e715 303,185 140

Fresh-process import of that graph on the pinned Bun fork, staged macOS-app package layout, 9 interleaved pairs on a loaded host (load ~34): median wall 12,654 ms with the hook vs 2,544 ms without (ranges 3,471–20,239 vs 2,014–3,416 ms). On Node the same comparison is within noise (9,135 vs 7,537 ms median at the same load).

Full app-style gateway run (fresh isolated state, token auth, random port, bundled Bun + SQLite, exactly the macOS child launch), 7 interleaved fresh/warm pairs per build. On an idle Mac mini (M4 Pro, load ~3) the fixed build was faster in all 7 fresh and all 7 warm pairs:

Start Baseline readyz median This PR Change
Fresh state 4,646 ms 4,423 ms −223 ms (−4.8%)
Warm state 3,768 ms 3,590 ms −178 ms (−4.7%)

The saving sits where resolution happens: process.bootstrap 2,024 → 1,874 ms fresh and 1,522 → 1,379 ms warm, gateway.server-start-import 302 → 283 / 163 → 139 ms, sidecars.reply-runtime 211 → 196 / 115 → 98 ms. Under CPU contention the effect grows because the hook serializes resolution onto one JS thread while Bun's native loader is parallel: on a shared 32-core host at load 68–97 the same A/B moved fresh readyz medians from 67.5 s to 49.6 s and warm from 57.1 s to 51.1 s (noisy; shown for direction only).

CPU profiles of the baseline attribute the largest share of startup main-thread time to runResolveHooksBun → defaultResolve → defaultResolveForHooks (about 1.8–2.6 s sampled during CLI bootstrap, and most of the sidecars.reply-runtime phase).

Checks: pnpm test src/cli/run-main.cleanup.test.ts src/entry.test.ts src/entry.run-main.test.ts src/index.entrypoint.test.ts src/index.test.ts --maxWorkers=1 passed (3 Vitest shards, 134 s wall on a loaded host); pnpm check:test-mock-exports OK; node scripts/check-changed.mjs passed every lane except typecheck core tests, which failed only on ui/src/components/mcp-app-*.test.ts inherited from the base and already fixed on main by #169439. Autoreview (Codex) scoped-clean at P0–P2.

The dist-relative resolve hook (#128541) short-circuited relative ".js"
imports so Node skipped package-scope format detection. #139754 then
emitted core chunks as ".mjs", which already carry their format, so the
hook now matches about 0.1% of resolutions (30 of 28,038 on Node for the
gateway/reply import graph).

The pinned Bun fork has since gained Module.registerHooks. With any
resolve hook registered, Bun routes every resolution through its JS
customization-hooks chain instead of its native parallel loader: 303,185
hook calls for the same graph, 140 short-circuits. In fresh Bun processes
importing that graph, median wall time drops from 12.65 s to 2.54 s
without the hook (9 interleaved pairs, loaded host).

Remove the hook and its call sites instead of special-casing Bun; Node
keeps the explicit-format chunks that replaced it.
@clawsweeper

clawsweeper Bot commented Oct 12, 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.

@openclaw-barnacle openclaw-barnacle Bot added cli CLI command changes size: M labels Oct 12, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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. labels Oct 12, 2026
@clawsweeper

clawsweeper Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR removes the dist-relative ESM resolver hook from both executable entry points.

Example: Reported fresh-state Gateway startup with bundled Bun on an idle M4 Pro Mac mini.

  • Before: Median readiness was 4,646 ms with the hook.
  • After: Median readiness was 4,423 ms without the hook.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, complete simplification supported by detailed reported production measurements; source and history were verified without independently observing those runs.
Proof confidence 🐚 platinum hermit (4/6) The supplied maintainer-author classification exempts ordinary contributor proof; the body reports fresh and warm bundled-Bun gateway run comparisons through readiness, without raw captures independently inspected.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Users wait unnecessarily for the bundled Bun Gateway to become ready on startup and restart.
Reason: The supplied maintainer-author classification establishes direction, and reported production benefit accompanies a focused simplification with no new setting or public contract.

Merge readiness

✅ Ready for maintainer review

Keep open: this PR removes a superseded startup optimization with a concrete performance benefit, and review found no actionable correctness or security defect.

Priority: P2
Reviewed head: ce8f17f178d02caac3bf51a7b825baaf32338876

Before merge

None.

Findings

None.

Agent review details

How this fits together

CLI and macOS app launches enter executable bootstrap, resolve packaged modules, and start the Gateway until readiness is published.

flowchart LR
A[CLI or macOS app] --> B[Executable bootstrap]
B --> C[Native runtime resolver]
C --> D[Packaged ESM modules]
D --> E[Gateway startup]
E --> F[Ready Gateway]
Loading

Technical review

Best possible solution:

Remove the obsolete hook completely and use native resolution with the existing explicit-format build output.

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

Current main registers the hook and the pinned Bun source confirms the JavaScript resolution chain; the PR reports comparative Gateway measurements, but this read-only review did not reproduce the timing.

Is this the best way to solve the issue?

Complete removal fits the existing .mjs build contract, preserves user resolver hooks, and avoids retaining a redundant optimization behind a runtime-specific branch.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 83f3c3d67998.

Provenance checked

  • Dist-relative resolver optimization changes intended behavior with a stated reason (74af716: Avoid repeated package-scope format detection for relative .js imports during Node startup.)
  • User preload and loader observations keeps the original intent (88c684c: Prevent OpenClaw's later short-circuit hook from bypassing previously registered user resolver hooks.)
  • Explicit-format runtime chunks keeps the original intent (8b37368: Reduce packaged module-loading overhead while preserving named public .js paths.)
  • Legacy entrypoint lazy imports keeps the original intent (ec8df3a: Defer CLI-only imports until after the version fast path.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 269bc5c adds src/entry.ts:113 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 269bc5c89e71; files: src/entry.ts)
  • Leon-SK668: Raw commit 88c684c adds src/infra/node-options.ts:3 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 88c684c13efe; files: src/infra/node-options.ts)

PR surface

Source -136, Tests -289, Other -1. Total -426 across 6 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 3 0 136 -136
Tests 2 0 289 -289
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 0 1 -1
Total 6 0 426 -426

Review metrics

Metric Value Why it matters
Production versus test LOC Production −136; tests −289; mock baseline −1 The reduction removes a superseded implementation and its dedicated coverage without introducing another mechanism.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: This is a bounded startup performance improvement with no demonstrated outage or loss of capability.
  • 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.

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.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-12T02:34:35.700Z sha ff43b01 :: needs maintainer review before merge. :: none

Reviewed October 11, 2026, 11:54 PM ET / October 12, 2026, 03:54 UTC (Revision 2).

@steipete
steipete merged commit 02f0ea3 into main Oct 12, 2026
115 checks passed
@steipete
steipete deleted the perf/drop-dist-esm-resolve-hook branch October 12, 2026 04:20
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 12, 2026
…9543)

The dist-relative resolve hook (openclaw#128541) short-circuited relative ".js"
imports so Node skipped package-scope format detection. openclaw#139754 then
emitted core chunks as ".mjs", which already carry their format, so the
hook now matches about 0.1% of resolutions (30 of 28,038 on Node for the
gateway/reply import graph).

The pinned Bun fork has since gained Module.registerHooks. With any
resolve hook registered, Bun routes every resolution through its JS
customization-hooks chain instead of its native parallel loader: 303,185
hook calls for the same graph, 140 short-circuits. In fresh Bun processes
importing that graph, median wall time drops from 12.65 s to 2.54 s
without the hook (9 interleaved pairs, loaded host).

Remove the hook and its call sites instead of special-casing Bun; Node
keeps the explicit-format chunks that replaced it.
yanosea added a commit to yanosea/yanoNixFiles that referenced this pull request Nov 15, 2026
- remove the esm loader shim wrapper: 2026.11.3 no longer has the
  resolve hook (openclaw/openclaw#169543), so `programs.openclaw`
  uses the default package.
- remove the overlay that copied `discord` into the gateway: the
  plugin loads from `plugins.load.paths`, which overrides that copy.
- stop removing the retired `checkin`, `dev-checkin` and
  `dev-daily-thread` jobs, which no longer exist.
- remove the `webhooks` plugin entry, which 2026.11.3 dropped.
- bring the check-in comments in line with the gate jobs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. 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