Skip to content

perf(models): avoid repeated policy loading after hot reload - #168733

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-models-list-hot-reload-policy-cache-20260919
Oct 11, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-models-list-hot-reload-policy-cache-20260919

Conversation

@steipete

@steipete steipete commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Personal model catalogs can repeatedly resolve provider policy artifacts after a model configuration hot reload, blocking the Gateway event loop.

User Impact

Keeps provider policy lookup warm across repeated models.list requests after changing the default model. Model visibility, authentication policy, and reload behavior stay the same.

Why This Change Was Made

Completed private registries do not have an active-registry publication version. The policy cache treated them as mutable forever. A single cached entry per provider also let private preparation and active-registry publication evict each other.

Reuse the loader's completed identity to admit private registries to the existing cache, and partition that cache by weak registry keys. Registration stays uncached, publication versions still invalidate reused registries, and metadata invalidation clears the cache. Weak keys allow retired registries and their policy artifacts to be collected. Capture the current runtime/source config pair once at the model-list preparation owner so per-model route factories do not repeatedly clone the same config; retain the original request-config identity for freshness checks.

Evidence

The new completed-private-registry cases fail on the unchanged base: both present and absent policies resolve artifact candidates three times instead of once. The four existing generation cases pass on that base.

Same isolated Linux rig, Node 26.8.1, 100 configured synthetic OpenAI models, a synthetic signed-in identity and personal account, 12 actual RPC executions per phase:

Revision Config phase p50 p95 Artifact resolutions across 12 reads
Base Before reload 178.5 ms 311.9 ms 9,733
Base After reload 287.2 ms 302.1 ms 9,732
Fix Before reload 186.7 ms 288.1 ms 1
Fix After reload 145.1 ms 175.1 ms 0

Post-reload median improved 1.98× and p95 improved 1.73×. Pre-reload median did not improve. Equivalent provider-name case variants force distinct RPC share keys, so the one-second response cache cannot hide work; every response is still the same canonical provider catalog. The change uses no sleeps or forced catalog refreshes. The Gateway regression fails on base with 9,732 post-reload artifact resolutions and passes after the fix. No production Gateway is used.

Independent review of the complete change is clean through P2. On the isolated Linux rig, the final pnpm build passed in 269 seconds; 202 affected plugin tests passed across 10 files in 63 seconds, and 45 Gateway/config tests passed across seven files in 133 seconds.

Exact cost commands: pnpm test src/plugins/provider-policy-surface.generation.test.ts --maxWorkers=1 passed all six cases in 35.40 seconds wall time. pnpm test src/gateway/gateway.models-policy-reload.e2e.test.ts --maxWorkers=1 passed in 435.15 seconds wall time, including required runtime preparation. Measured test-execution cost for CI budgeting: 99 ms for the two new private-registry cases, and 19.505 seconds for the end-to-end case; shared compilation/runtime preparation is additional.

The first changed gate stopped on the pre-existing duplicate UI test import fixed by 48588a652222. This branch was rebased onto current main to include that fix; its patch and reviewed source context are unchanged. The rebased production typecheck and all 28 test typecheck groups passed. The remaining selected lint, import-cycle, schema, runtime-loader, webhook, and pairing checks passed. One new-test lint diagnostic was corrected with an equivalent ASCII string split and independently reviewed; no production source changed.

Remote prerequisites: Blacksmith Testbox tbx_01m4kshphdqyf461b48mr2h5xr first needed a frozen-lockfile dependency install; early shared-response probes passed, then the explicit-account probe was blocked by a sync timeout. AWS cbx_bd0014f0257e fell back to an unsuitable 2 GB instance after standard-instance quota exhaustion and was released. Hetzner cbx_4746f3c3343f completed source sync but had no Node/pnpm runtime (exit 127) and was released. Validation continued on the authorized isolated Linux rig. Those leases are stopped. Final validation used Blacksmith Testbox tbx_01m4m4qwrb0d8vpw0p26dnh47p (also stopped): the changed gate passed production types and all 28 test type graphs before failing the baseline-only dead-export scan; the remaining planned commands passed after the test lint correction. node scripts/run-vitest.mjs test/scripts/eager-import-closure.test.ts test/scripts/pr-wrapper-source-closure.test.ts test/scripts/ci-node-test-plan.test.ts test/scripts/ci-node-test-plan.commands.test.ts passed all 143 tests. OPENCLAW_E2E_VERBOSE=1 node scripts/run-vitest.mjs src/plugins/provider-policy-surface.generation.test.ts src/gateway/gateway.models-policy-reload.e2e.test.ts passed all seven tests, including zero post-reload artifact loads. Combined final test command: 340 seconds, exit 0.

Production LOC: +16. The growth is the weak per-registry partition, completed-load admission check, and one config capture at the preparation boundary. These preserve generation invalidation and collect retired registries while avoiding cross-registry cache eviction and per-model config projection. The existing cache is replaced; no second persistent cache, config option, schema change, or compatibility layer is introduced.

Inherited CI failures

Initial CI attempt 38096234973 tested merge b461455be7703d34c655f6b8e3adf1d7a899e7d9 (main baseline dd4b258a3cfa6fdef41535e5518fe260f5665a9c plus the initial reviewed PR head fde4f08b351ab69bebef772d47905fe238cadd85). Two independent failures reproduce on that unchanged main baseline on the isolated Linux rig:

  • check-lint-core-2: src/infra/sqlite-worker-plugin-comparison.test.ts:95, typescript(unbound-method). The exact single-file oxlint command reproduces the same diagnostic, exit 1 (14.1 seconds).
  • checks-node-compact-small-2: three assertions in test/scripts/plugin-sdk-surface-report.test.ts fail from stale SDK budgets: exports 3672 > 3656, callables 2133 > 2122, deprecated exports 158 > 154, plus two deprecated-entrypoint limits. node scripts/run-vitest.mjs test/scripts/plugin-sdk-surface-report.test.ts reproduces the same three failures, exit 1 (37.00 seconds).

The failing test files, SDK report implementation and SDK source tree are unchanged between the baseline and tested merge. This lane adds no SDK exports. CI's successful fail-fast monitor canceled the remaining jobs after these failures; canceled jobs are not claimed as passing. No CI rerun or extra push was used to chase green. The scoped remote checks are complete.

The separate intermittent UNAVAILABLE: Session changed while preparing its metadata error was not reproduced. Team was not touched.

Local gate exceptions independently reproduced on the unchanged main baseline on the isolated Linux rig: node --import tsx scripts/check-deadcode-exports.mts reports the identical 8 production and 23 full-tree unused exports (exit 1), and node --import ./scripts/tsx.mjs scripts/check-export-name-collisions.mts reports the identical existing resolveChannelDmPolicy collision in src/channels/account-resolution.ts and src/channels/plugins/dm-access.ts (exit 1). This lane adds no exported symbols. The scoped gates and regressions pass; neither inherited guard failure is hidden or called green.

Final head and landing qualification

Final head: be8905c9c1e5bb3dcc70e296690da76b7541e0eb. The only follow-up commit replaces an ASCII string spread in the regression with the equivalent split("") spelling to satisfy lint; independent review is clean and the final test bytes were verified on Testbox.

Final CI attempt 38100198768 executed merge 6c3dcbf5973fb1116a2eb3901b30e6177b93065d against newer main baseline bbd2daf8b6def57eb0ac47c5e270c9b39cc168e6. Its only root failure is job 114354585023: 70 cases in test/canonical-descendant.integration.test.ts fail with Codex session binding changed during initialization / Codex initialization binding owner changed before rollback. On that exact unchanged main baseline, node scripts/run-vitest.mjs test/canonical-descendant.integration.test.ts reproduces the identical 70 failing case names (70 failed, 4 passed); CI's other three files contribute 42 passing tests. The test and Codex plugin sources are unchanged by this PR.

The final security check and separate security review passed. The successful failure monitor canceled 26 sibling jobs; canceled coverage is not asserted as passing. Landing uses Peter's standing authorization for independently proven inherited failures, with the exact PR head pinned. The native merge wrapper requires a separate canonical worktree and performs a branch-delete push, outside this lane's checkout/push restrictions, so the explicitly allowed merge API fallback is used.

@clawsweeper

clawsweeper Bot commented Oct 10, 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 docs Improvements or additions to documentation gateway Gateway runtime size: M labels Oct 10, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Oct 10, 2026
@clawsweeper

clawsweeper Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR reuses provider policies across completed private registries and captures model-list configuration once per preparation.

Example: A personal account lists 100 configured OpenAI models after changing the default from openai/model-0 to openai/model-1.

  • Before: The supplied isolated Gateway measurement records 9,732 artifact resolutions across 12 reads, with a 287.2 ms median.
  • After: The same measurement records zero post-reload artifact resolutions, with a 145.1 ms median.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Focused production-owner and Gateway harness evidence supports a coherent repair; no introduced defect was found, and the current CI failure independently matches baseline failures.
Proof confidence 🦐 gold shrimp (3/6) The host identifies this as maintainer-authored, outside the ordinary contributor-proof gate; supplied evidence exercises real isolated Gateway RPCs and production policy resolution in a test harness, rather than a standalone shipped-entry-point run.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Personal model catalogs repeat synchronous policy resolution after a model-config hot reload, delaying Gateway responses.
Reason: The supplied comparison demonstrates useful savings, the patch stays within existing owners, and the host identifies maintainer-authored direction.

Merge readiness

✅ Ready for maintainer review

Keep this PR open: current main still repeats policy resolution for completed private registries, and this focused performance repair has no actionable introduced defects.

Priority: P2
Reviewed head: be8905c9c1e5bb3dcc70e296690da76b7541e0eb

Before merge

None.

Findings

None.

Agent review details

How this fits together

Gateway model-list preparation consumes a catalog owner's config and plugin registry, resolves provider policies, and returns model visibility, availability, and account-selection facts.

flowchart TD
A[Model list RPC] --> B[Catalog preparation owner]
B --> C[Captured runtime and source config]
B --> D[Completed plugin registry]
D --> E[Weak registry policy cache]
E --> F[Provider policy artifact]
C --> G[Prepared model catalog response]
F --> G
Loading

Technical review

Best possible solution:

Reuse completed registry policies and one captured config pair while retaining generation invalidation and managed-instance admission.

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

Current-main source shows completed private registries bypassing policy memoization; the supplied base-failing regressions exercise that path, but this read-only review did not execute them.

Is this the best way to solve the issue?

The repair addresses the cache admission and cross-registry eviction causes in their existing owners, and reuses the existing config-capture contract without changing request freshness.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Provider policy cache admission changes intended behavior with a stated reason (1c44067: Memoize policy artifacts while bypassing mutable registration and unpublished registries.)
  • Registry retention keeps the original intent (40d735e: Avoid keeping retired registry graphs alive solely for cache identity.)
  • Loader request and resolved identities changes intended behavior with a stated reason (2716950: Reuse registrations across raw requests and prepared manifest identities; this PR delays identity availability until registration completes.)
  • Model-list preparation owner keeps the original intent (dd76168: Prepare model facts with the catalog owner's registry while keeping published reads pure.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 1c44067 adds src/plugins/provider-policy-surface.ts:87 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 1c44067fcd74; files: src/plugins/provider-policy-surface.ts)

PR surface

Source +16, Tests +236, Docs +2. Total +254 across 8 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 5 31 15 +16
Tests 2 240 4 +236
Docs 1 5 3 +2
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 8 276 22 +254

Review metrics

Metric Value Why it matters
Production versus test growth Production +31/-15, net +16; tests +240/-4, net +236. Production growth implements the existing cache partition and config capture; tests cover private-registry admission and its distinct Gateway reload composition.

Labels

Label changes:

  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • remove status: ⏳ waiting on author: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P2: Repeated personal-catalog reads incur avoidable synchronous work, with a bounded performance impact and a focused repair.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • 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 need explicit maintainer commands such as @clawsweeper autofix or @clawsweeper automerge.

History

Review history (2 earlier review cycles)
  • reviewed 2026-10-10T23:42:13.046Z sha 63db1c359605eb01536c5e6ee46de35b9ebdd4e3 :: needs changes before merge. :: none
  • reviewed 2026-10-10T23:55:17.459Z sha fde4f08 :: needs changes before merge. :: none

Reviewed October 10, 2026, 9:07 PM ET / October 11, 2026, 01:07 UTC (Revision 3).

Cache completed private registry policies by weak registry identity and preserve publication invalidation. Capture one runtime/source config pair for model-list preparation to avoid per-model projection and cloning.
@steipete
steipete force-pushed the steipete/perf-micro-models-list-hot-reload-policy-cache-20260919 branch from 63db1c3 to fde4f08 Compare October 10, 2026 23:44
@steipete
steipete marked this pull request as ready for review October 10, 2026 23:47
@clawsweeper clawsweeper Bot added status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Oct 11, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

Landing the pinned head be8905c9c1e5bb3dcc70e296690da76b7541e0eb under the authorized inherited-failure exception.

Final CI 38100198768 fails only in test/canonical-descendant.integration.test.ts. The exact main baseline bbd2daf8b6def57eb0ac47c5e270c9b39cc168e6 reproduces all 70 failing case names and both session-binding initialization/rollback diagnostics on the isolated Linux rig (70 failed, 4 passed; CI has 42 additional passes in other files). The failing test and Codex plugin source are unchanged by this PR. The final security check and security review passed; the 26 fail-fast cancellations remain unrun coverage.

Scope proof is complete: production types and 28 test type graphs, remaining lint/import/ownership checks, 143 closure/planning tests, six policy-generation tests, and the real-Gateway hot-reload regression passed. The separate unused-export and export-collision guards reproduce unchanged on main. Full implementation review and the test-only lint correction review are clean.

@steipete
steipete merged commit 6b13e9e into main Oct 11, 2026
122 of 151 checks passed
@steipete
steipete deleted the steipete/perf-micro-models-list-hot-reload-policy-cache-20260919 branch October 11, 2026 01:19
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 11, 2026
…w#168733)

* perf(models): reuse policy and config generations after reload

Cache completed private registry policies by weak registry identity and preserve publication invalidation. Capture one runtime/source config pair for model-list preparation to avoid per-model projection and cloning.

* test(gateway): fix provider spelling generation lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation gateway Gateway runtime P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. 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