Repository navigation
improve(gateway): reuse verified metadata during startup - #136141
Conversation
Return the final accepted preflight config/metadata generation and adopt its existing cache after successful foreground config preparation. Preserve fresh rereads, package checks, migration leases, and Gateway activation order. The final config-read phase improved in all 40 measured A/B pairs. Overall startup and CPU savings remain unproven; retain the mixed results in the PR.
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
Verification for The repair carries the final accepted config/metadata generation out of preflight and adopts its existing cache only after successful foreground configuration preparation. Fresh verification, config rereads, package/fingerprint checks, migration leases, quarantine, and Gateway activation ordering remain intact. Fresh managed Codex autoreview had no accepted/actionable findings. 138 focused tests passed, including the new regressions that failed on the original code: OPENCLAW_VITEST_MAX_WORKERS=2 node --import ./scripts/tsx.mjs scripts/test-projects.mts \
src/cli/program/config-guard.test.ts \
src/commands/doctor-config-preflight.plugin-persistence.test.ts \
src/commands/doctor-config-preflight.state-migration.test.ts \
src/commands/doctor-config-preflight.state-migration-input.test.ts
pnpm build
node scripts/check-changed.mjs -- \
src/cli/program/config-guard.ts \
src/cli/program/config-guard.test.ts \
src/commands/doctor-config-preflight-startup.ts \
src/commands/doctor-config-preflight.ts \
src/commands/doctor-config-preflight.plugin-persistence.test.ts
git diff --checkThe refreshed full build and all changed-file checks passed. Two further live smoke runs used isolated state and ports: node --import ./scripts/tsx.mjs scripts/bench-gateway-startup.ts \
--case default --case fiftyPlugins --runs 1 --warmup 0 \
--output .artifacts/startup-performance/live-main-refresh.jsonBoth reached The original 40-pair A/B evidence remains pinned to baseline Production delta is +15 net lines; tests +74. No configuration, dependency, schema, or protocol change. Native preparation is complete. ClawSweeper's completed exact-head review reports no actionable correctness or security findings and no before-merge items; its full review has been read. The review's automated stored-data classification was checked against the production diff and cache owner. This patch changes the returned accepted read and adopts an existing in-memory plugin-cache object; it introduces no schema, SQL, serialization, stored representation, or migration-order change. Existing migration identity checks and lease cleanup remain in place, and the two state-migration suites plus the real-fixture final-generation regression passed. No new data migration is needed. |
|
Codex review: needs maintainer review before merge. Reviewed September 2, 2026, 4:22 AM ET / 08:22 UTC. ClawSweeper reviewWhat this changesThis PR reuses the plugin-metadata cache produced by successful Gateway startup preflight during the final Gateway configuration read, avoiding a redundant metadata rebuild. Merge readiness✅ Ready for maintainer review Keep open: current main does not contain this focused startup-cache repair, and direct review found no introduced correctness or security defect. The repository state identifies the author as a MEMBER; normal maintainer merge handling remains appropriate. Priority: P3 Review scores
Verification
How this fits togetherGateway startup validates configuration and plugin metadata before it initializes the runtime. This change carries only the verified cache generation into the subsequent fresh Gateway configuration read; Gateway startup later remains the sole publisher of active plugin metadata. flowchart LR
A[Gateway command] --> B[Startup preflight]
B --> C[Verified plugin metadata]
C --> D[Accepted cache generation]
D --> E[Fresh Gateway config read]
E --> F[Gateway metadata publication]
F --> G[Running Gateway]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +15, Tests +74. Total +89 across 5 files. View PR surface stats
Review metrics
Stored data modelPersistent data-model change detected: Technical reviewBest possible solution: Keep one cache-ownership flow: return the post-convergence accepted metadata generation, reuse its bound cache for the fresh Gateway read, and publish active metadata only at Gateway bootstrap. Do we have a high-confidence way to reproduce the issue? Not applicable as a bug reproduction: this is a bounded performance and cache-ownership improvement. Source identifies the prior redundant-read boundary, and the supplied PR evidence reports real isolated Gateway smoke runs. Is this the best way to solve the issue? Yes. Reusing the already bound cache after the final accepted preflight read preserves fresh validation and avoids premature Gateway metadata publication; publishing the snapshot early or rebuilding it later would violate one of those boundaries. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 121ab522241a. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
|
Merged via squash.
|
Return the final accepted preflight config/metadata generation and adopt its existing cache after successful foreground config preparation. Preserve fresh rereads, package checks, migration leases, and Gateway activation order. The final config-read phase improved in all 40 measured A/B pairs. Overall startup and CPU savings remain unproven; retain the mixed results in the PR.
Additional instructions
This same-repository branch is available for maintainer edits.
What Problem This Solves
Resolves repeated work during foreground Gateway startup: the final configuration read rebuilt plugin metadata that startup preflight had just verified. Baseline medians for this final read ranged from roughly 124–207 ms.
This is a bounded startup cache-ownership repair requested by a maintainer. No related issue is claimed fixed.
Why This Change Was Made
Startup preflight now returns its final accepted config/metadata generation instead of discarding the post-convergence read. After successful foreground config preparation, the CLI carries that generation's existing cache into the final config reread. It does not publish Gateway activation policy early or introduce a second cache.
Config-file rereads, fresh persistence/convergence verification, migration leases, fingerprint/package checks, quarantine, and readiness work remain intact. Ordinary Doctor calls, service management, read-only diagnostics, invalid-config overrides, and failed preparation do not adopt the startup cache.
User Impact
Foreground Gateway startup avoids one redundant plugin-metadata rebuild. The final config-read phase improved in all 40 measured A/B pairs, with medians falling to 3–4 ms. There are no new settings, environment variables, dependencies, schema changes, protocol changes, or operator steps.
Overall startup and CPU improvement are not established. Whole-startup results were mixed on the busy measurement host, and the unfavorable measurements are retained below. This PR claims the observed final-read improvement, not a startup-wide percentage reduction.
Evidence
Measured the built Gateway before and after the patch against source baseline
ebd79690f2652eb24eb509d63d0fecc01af16baf, using matched full builds and Node v24.19.0 on macOS arm64. There were 80 measured starts plus 12 excluded warmups, using fresh temporary state and isolated loopback ports. Every start reached HTTP 200 on both/healthzand/readyz; loaded inventories matched at 17 plugins for default and 51 for the 50-fixture-plugin case. No runs were selectively retried or removed; the operator Gateway was not touched.The readiness-only block retained ten paired measurements per fixture, with no CPU/RSS polling, preloads, or profiling:
Readiness means changed −6.3% for default but +3.4% for the 50-plugin fixture. Two retained process-stat blocks, pooled to ten pairs per fixture, showed worse readiness medians (+8.1%/+9.3%) and CPU medians (+2.1%/+12.3%, default/50 plugins). Their final config-read medians still improved from 124.35 → 4.25 ms and 184.2 → 3.5 ms. These blocks are not discarded or treated as proof of a total CPU benefit.
Separate main-thread Inspector profiles captured real startup through readiness and showed approximately 18% less weighted sampled time with visible plugin-metadata ancestry in both fixtures. That metric is neither OS CPU time nor a complete asynchronous phase duration. Diagnostic cache counts also confirmed that the 50-plugin fixture's early handoff did not discard populated SDK/module-loader memos; this does not explain away the adverse CPU result.
The new regression cases failed on the original code. After the fix, 138 tests across four files passed, covering the final accepted generation, foreground variants, convergence drift, persistence, lease cleanup, invalid config, management/diagnostic isolation, and failed runtime-config preparation:
Both revisions passed full
pnpm build. Changed-file checks passed, including production/test typechecking, formatting, lint, dead exports, import cycles, plugin boundaries, and state/database guards:The same 138 focused tests and a full
pnpm buildalso passed after moving the byte-identical five-file patch onto main331d8a2265ff07d5db51e459c32d38c10fc7dc24. Two additional isolated live startup smoke runs reached HTTP 200 on both endpoints, retained the expected 17/51-plugin inventories, and exited normally. Those two runs are correctness smoke tests, not part of the 80 measured starts above. That base includes the separate prepared-normalizer optimization in #136063; the A/B percentages above are tied to their original measured baseline, not presented as measurements against the newer main.Refreshed changed-file checks also passed, including production/test typechecks, formatting, lint, all three dead-export scans, import cycles, plugin boundaries, and state/database guards. Exact-head CI run 33606750778 completed successfully for
345688564ebcce7e9a1d9ac57d0de69ada6dbf40, including the requiredopenclaw/ci-gate.Production delta is +15 lines (39 added/24 removed); tests are +74 lines (81 added/7 removed). The production growth carries the accepted config/metadata owner across the existing preflight boundary; redundant parameters and duplicate success blocks were removed.
Measurement limits and follow-up
These are source-checkout fixtures, not installed-package or OS-cold benchmarks. Host contention, synchronous process-stat sampling on the probe event loop, and CPU capture after readiness limit end-to-end conclusions. A quiet-host run with a defined CPU boundary is still needed before claiming startup-wide savings. The measurement tooling should move process-stat collection off the probe loop and avoid inherited worker profile-file collisions; this patch does not change those tools or weaken startup verification.