Skip to content

improve(gateway): reuse verified metadata during startup - #136141

Merged
steipete merged 1 commit into
mainfrom
codex/gateway-startup-accepted-cache
Sep 2, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/gateway-startup-accepted-cache

Conversation

@steipete

@steipete steipete commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor
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 /healthz and /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:

Fixture Final config-read p50, before → after Readiness p50, before → after Faster readiness pairs
Default 194.7 → 4.35 ms (−97.8%) 5.153 → 4.365 s (−15.3%) 5/10
50 fixture plugins 206.6 → 3.4 ms (−98.4%) 4.950 → 4.725 s (−4.5%) 6/10

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:

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

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:

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 --check

The same 138 focused tests and a full pnpm build also passed after moving the byte-identical five-file patch onto main 331d8a2265ff07d5db51e459c32d38c10fc7dc24. 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.

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.json

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 required openclaw/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.

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.
@clawsweeper

clawsweeper Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added cli CLI command changes commands Command implementations size: S maintainer Maintainer-authored PR labels Sep 2, 2026
@steipete
steipete marked this pull request as ready for review September 2, 2026 08:02
@steipete

steipete commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Verification for 345688564ebcce7e9a1d9ac57d0de69ada6dbf40 is complete: CI run 33606750778 succeeded, including openclaw/ci-gate, and native OPENCLAW_TESTBOX=1 scripts/pr prepare-run 136141 accepted that exact-head evidence without rebasing or changing the PR head.

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 --check

The 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.json

Both reached /healthz and /readyz HTTP 200, loaded the expected 17/51 plugins, and exited normally. The operator Gateway was untouched.

The original 40-pair A/B evidence remains pinned to baseline ebd79690f2652eb24eb509d63d0fecc01af16baf plus this patch: final config-read medians improved from roughly 124–207 ms to 3–4 ms in all 40 pairs. Overall startup and CPU gains remain unproven; the mixed readiness results and the 50-plugin process-stat CPU median increase of 12.3% are retained in the PR. No installed-package or OS-cold performance claim is made.

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.

@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 Sep 2, 2026
@clawsweeper

clawsweeper Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 2, 2026, 4:22 AM ET / 08:22 UTC.

ClawSweeper review

What this changes

This 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
Reviewed head: 345688564ebcce7e9a1d9ac57d0de69ada6dbf40

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, source-consistent cache-ownership repair with exact-head CI and targeted coverage, with no actionable review finding.
Proof confidence 🌊 off-meta tidepool Not applicable: Not applicable to the external-contributor proof gate because Repository State identifies a MEMBER author. The supplied maintainer evidence is nevertheless source-linked: the benchmark harness starts an isolated Gateway process and requires successful /healthz and /readyz probes after the changed startup path.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: Not applicable to the external-contributor proof gate because Repository State identifies a MEMBER author. The supplied maintainer evidence is nevertheless source-linked: the benchmark harness starts an isolated Gateway process and requires successful /healthz and /readyz probes after the changed startup path.
Evidence reviewed 6 items Introduced cache handoff: The handoff occurs only after a valid configuration is accepted, only for foreground gateway startup, and adopts the cache bound to the preflight metadata snapshot.
Final accepted generation: After convergence, the preflight rereads with current metadata disallowed, validates the migration identity, and returns that final read to the caller.
Fresh downstream read and sole publisher: Gateway startup still performs a fresh configuration-and-metadata read, while only the later Gateway bootstrap publishes the active metadata snapshot.
Findings None None.
Security None None.

How this fits together

Gateway 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]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +15, Tests +74. Total +89 across 5 files.

View PR surface stats
Area Files Added Removed Net
Source 3 39 24 +15
Tests 2 81 7 +74
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 120 31 +89

Review metrics

Metric Value Why it matters
Production versus test delta production +15 net (39 added, 24 removed); tests +74 net (81 added, 7 removed) The production growth carries the final verified cache owner across the existing preflight boundary; tests cover foreground variants, failed adoption, and final accepted generation.

Stored data model

Persistent data-model change detected: migration/backfill/repair: src/cli/program/config-guard.ts, migration/backfill/repair: src/commands/doctor-config-preflight-startup.ts, migration/backfill/repair: src/commands/doctor-config-preflight.ts, persistent cache schema: src/cli/program/config-guard.ts, unknown-data-model-change: src/commands/doctor-config-preflight-startup.ts. Confirm migration or upgrade compatibility proof before merge.

Technical review

Best 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.

Labels

Label changes:

  • add P3: This is a bounded Gateway startup-performance improvement with no new setting, dependency, schema, protocol, or operator action.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: Not applicable to the external-contributor proof gate because Repository State identifies a MEMBER author. The supplied maintainer evidence is nevertheless source-linked: the benchmark harness starts an isolated Gateway process and requires successful /healthz and /readyz probes after the changed startup path.

Label justifications:

  • P3: This is a bounded Gateway startup-performance improvement with no new setting, dependency, schema, protocol, or operator action.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: Not applicable to the external-contributor proof gate because Repository State identifies a MEMBER author. The supplied maintainer evidence is nevertheless source-linked: the benchmark harness starts an isolated Gateway process and requires successful /healthz and /readyz probes after the changed startup path.

Evidence

What I checked:

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete
steipete merged commit 91fdd6b into main Sep 2, 2026
358 checks passed
@steipete
steipete deleted the codex/gateway-startup-accepted-cache branch September 2, 2026 08:27
@steipete

steipete commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 3, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes commands Command implementations maintainer Maintainer-authored PR 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