Skip to content

perf(gateway): reuse admitted plugin migration facts in warm startup config reads - #169613

Merged
steipete merged 2 commits into
mainfrom
steipete/warm-start-snapshot-reuse
Oct 12, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/warm-start-snapshot-reuse

Conversation

@steipete

@steipete steipete commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Warm openclaw gateway run starts (the state database already exists, which is every start after the first, including the macOS app's Bun-hosted Gateway) spent ~170 ms synchronously copying the shared-state SQLite database in a spawned child process, only to re-read pending plugin migration rows that startup had read moments earlier.

User Impact

Warm Gateway starts reach /readyz about 170 ms sooner (−4.3% on an idle M4 Pro Mac mini, app-identical launches on the pinned Bun 65d94e715). Fresh starts are unchanged. No config, schema, or operator action.

Why This Change Was Made

Startup's config preflight already reads the pending plugin migration rows inside its admitted shared-state snapshot, and the CLI's final config reread already reuses that same preflight's plugin metadata (#136141). The migration rows describe obligations for those same installed plugins, so they belong to the same accepted generation rather than a separate artifact-preserving copy.

Config readiness now hands the accepted gateway-run preflight's migration facts to the gateway-run bootstrap record, in the branch that already adopts the preflight plugin cache. The final reread takes them once per start attempt and passes them as deferredPluginMigrations (both shell-env rereads share them), so config IO never opens shared state there.

Freshness: no Gateway owns state between the preflight and the final reread, so a later pre-ownership read is not a stronger contract — writers could commit just after it as well, and Gateway runtime reloads and config writes keep reading current rows. The startup preflight itself never writes migration rows (only Doctor flows do). Invalid preflights, interactive Doctor recovery, retries after InvalidConfigError recovery, missing config (dev bootstrap may create one), and --dev --reset never accept facts, so they keep reading rows. The server bootstrap consumes this snapshot as-is and does not re-read migration rows.

Rejected alternative: prepareValidation: "runtime" in the final reread

Instrumented runs show it does not miss the adopted plugin-metadata cache (resolveConfigWidePluginMetadataSnapshotAsync 0.2 ms, install records ~0 ms). Its entire cost is readDeferredPluginMigrationsAsync reading through the async state-read worker: 1,341–1,346 ms on a fresh start and 416–419 ms warm. On a fresh start the Gateway has already created the shared database by the final reread (about 3.7 s in), so this is a real worker read with a cold Bun transpiler cache, not an absent-database probe. Reads of a missing database already return without a worker (startOpenClawStateReadOperation, since #148290): openclaw config validate and Doctor lint on fresh state spend 2–3 ms there.

Evidence

Idle steipete-mini-sf (M4 Pro, load 2.2–4.0), base 04914b1ab803 vs this branch, each built with pnpm build and staged with scripts/stage-mac-runtime.sh (candidate = cloned stage refreshed with its own packed build). Launches use the staged Bun 1.4.3-canary.1+65d94e715 and libsqlite3, fresh HOME/OPENCLAW_STATE_DIR/OPENCLAW_CONFIG_PATH under $(getconf DARWIN_USER_TEMP_DIR), a random port, {gateway:{mode:"local",auth:{mode:"token"}},logging:{file}}, OPENCLAW_GATEWAY_STARTUP_TRACE=1, and OPENCLAW_GATEWAY_HOST_LIFELINE=stdin with stdin held open. 7 interleaved pairs (order alternating), each pair a fresh then a warm start in one state dir:

Metric (median) Base Candidate Within-pair delta
Warm cli.config-snapshot 169.4 ms 0.7 ms −168.7 ms (7/7)
Warm /readyz 3,990 ms 3,822 ms −171 ms (7/7)
Fresh cli.config-snapshot 1.1 ms 0.7 ms −0.4 ms (7/7)
Fresh /readyz 4,877 ms 4,866 ms −7 ms (4/7, noise)

A separate instrumented copy (timing wrappers on the compiled functions) counted synchronous snapshot subprocesses (runSqliteReadOnlyWorkerSync) across the whole startup: base warm = 1 (158–162 ms, inside readDeferredPluginMigrations), candidate warm = 0, all fresh = 0.

Tests: config-guard.test.ts covers acceptance (pending and empty generations handed off once; missing config and invalid preflight not accepted), and run.option-collisions.test.ts asserts the final reread receives the accepted facts. Restoring run.ts and config-guard.ts from main fails exactly those three positive cases. run.ts and run.option-collisions.test.ts both shrink (line-cap ratchet). pnpm test src/cli/program/config-guard.test.ts --maxWorkers=1: 24 tests, ~3.5 s test time on a load-70 host (the first new row spends 1.5 s loading the gateway bootstrap's lazy config IO graph; the others take 5–9 ms). Local check:changed: typecheck, lint, format, ratchets, and boundaries pass; its dead export scan fails only on src/agents/agent-run-result.ts: agentRunHasVisibleReply, inherited from main since #169554 removed its last caller (untouched here). Autoreview (Codex, P0–P2): scoped-clean.

Inherited CI failure: checks-node-changed-4 fails on src/cli/gateway-cli/run-loop.forced-cron.process.test.ts (all three cases: Missing process proof: held:starts=1:pending=true). This PR does not touch that test or its fixture, and none of the changed modules are in the fixture's static import graph. The file is selected only because this PR touches src/cli/gateway-cli/. It fails identically at clean base 04914b1ab803 on an idle Mac under the pinned Bun (OPENCLAW_VITEST_RUNTIME=bun node scripts/run-vitest.mjs src/cli/gateway-cli/run-loop.forced-cron.process.test.ts) and under Node, so it is broken on main independently of this change and is tracked separately. Landing uses the inherited-failure admin exception.

…startup read

Warm `openclaw gateway run` starts paid a synchronous artifact-preserving
SQLite snapshot subprocess in the final config reread only to read pending
deferred plugin migration rows. The startup preflight already read those
rows inside its admitted shared-state snapshot, and the final reread already
reuses that generation's plugin metadata.

Config readiness now hands the accepted gateway-run preflight's migration
generation to the gateway-run bootstrap record, next to the existing plugin
metadata cache adoption. The final reread takes it once per start attempt and
passes it as deferredPluginMigrations, so no shared-state copy is needed.
Invalid, recovered, missing-config, and dev-reset starts keep reading rows.
@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: S labels Oct 12, 2026
@steipete
steipete marked this pull request as ready for review October 12, 2026 04:54
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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 reuses admitted plugin migration facts during the final Gateway startup configuration read.

Example: A warm Gateway launch with an existing state database on the reported M4 Pro setup.

  • Before: The final configuration read took a reported median 169.4 ms.
  • After: The reported median fell to 0.7 ms without the redundant SQLite snapshot subprocess.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, correct optimization supported by reported production-launch measurements and bounded lifecycle regressions.
Proof confidence 🐚 platinum hermit (4/6) The maintainer-authored PR is outside the ordinary contributor proof gate; its body reports seven interleaved built Gateway launch pairs through readiness, fresh and warm cases, and subprocess instrumentation, but raw startup traces were not supplied and the inspected screenshots show another change.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Warm Gateway startup spends additional time copying shared state to reread migration facts already admitted during preflight.
Reason: The maintainer-directed optimization has measured value, uses existing ownership, and adds no settings or persisted format.

Merge readiness

✅ Ready for maintainer review

Keep open: this PR provides a focused, maintainer-directed startup optimization absent from current main, with no actionable introduced defect found.

Priority: P3
Reviewed head: 2c7ebfa9090f38137b9e4f5b9dd164d0799b7525

Before merge

None.

Findings

None.

Agent review details

How this fits together

CLI startup admission reads configuration, plugin metadata, and migration obligations; the final configuration reader consumes accepted facts and passes its snapshot to Gateway server bootstrap.

flowchart TD
A[Gateway CLI] --> B[Startup admission]
B --> C[Shared state snapshot]
C --> D[Prepared startup record]
D --> E[Final config read]
E --> F[Gateway server bootstrap]
Loading

Technical review

Best possible solution:

Carry admitted migration facts through the existing startup owner while retaining fresh reads for repair retries and runtime operations.

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

Source tracing confirms main resolves migration rows again during its final startup read; the author reports matched warm-start measurements, and this read-only review executed no target code.

Is this the best way to solve the issue?

The patch uses the existing config-reader contract and bootstrap record, preserves configuration selection guards, and avoids introducing a competing cache.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Accepted startup metadata handoff keeps the original intent (91fdd6b: Reuse accepted plugin metadata during the final configuration read without publishing Gateway activation policy early.)
  • Foreground configuration and environment guards keeps the original intent (df521a6: Converge selected runtime inputs and guard foreground startup mutation boundaries.)
  • Shell-environment startup plan keeps the original intent (6e06137: Use one startup shell-environment plan, with later policy edits taking effect on the next start.)
  • Configuration read failure handling keeps the original intent (be36d55: Keep unavailable configuration reads retryable while retaining permanent configuration and storage refusals.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 91fdd6b adds src/cli/program/config-guard.ts:354 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 91fdd6b8ea3e; files: src/cli/program/config-guard.ts)
  • Chunyue Wang: Raw commit df521a6 adds src/cli/gateway-cli/run.ts:327 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: df521a645977; files: src/cli/gateway-cli/run.ts)

PR surface

Source +37, Tests +41. Total +78 across 5 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 3 55 18 +37
Tests 2 52 11 +41
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 107 29 +78

Review metrics

Metric Value Why it matters
Production versus test growth Production +37 net lines; tests +41 net lines. Growth carries one admitted generation through the existing lifecycle and covers acceptance exclusions, one-shot consumption, and final-read forwarding.

Labels

Label changes:

No label changes.

Label justifications:

  • P3: This is a bounded startup performance improvement without a demonstrated broken user workflow.
  • 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-12T04:59:50.059Z sha 2c7ebfa :: needs maintainer review before merge. :: none

Reviewed October 12, 2026, 1:29 AM ET / 05:29 UTC (Revision 2).

@steipete steipete self-assigned this Oct 12, 2026
@steipete
steipete merged commit edf6c4d into main Oct 12, 2026
202 of 206 checks passed
@steipete
steipete deleted the steipete/warm-start-snapshot-reuse branch October 12, 2026 06:14
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via explicitly authorized admin squash with attributed pre-existing CI failures.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 13, 2026
…config reads (openclaw#169613)

* perf(gateway): reuse accepted preflight migration facts in the final startup read

Warm `openclaw gateway run` starts paid a synchronous artifact-preserving
SQLite snapshot subprocess in the final config reread only to read pending
deferred plugin migration rows. The startup preflight already read those
rows inside its admitted shared-state snapshot, and the final reread already
reuses that generation's plugin metadata.

Config readiness now hands the accepted gateway-run preflight's migration
generation to the gateway-run bootstrap record, next to the existing plugin
metadata cache adoption. The final reread takes it once per start attempt and
passes it as deferredPluginMigrations, so no shared-state copy is needed.
Invalid, recovered, missing-config, and dev-reset starts keep reading rows.

* test(cli): import gateway bootstrap owner statically in config guard tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S 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