Skip to content

Commit c6c2268

Browse files
authored
perf(plugins): unify progressive plugin cache ownership (#131442)
* fix(gateway): stop rediscovering plugin metadata while running * perf(plugins): unify progressive plugin cache ownership * test(plugins): cover shared registry migration reads * fix(plugins): reuse bundled channel metadata snapshots * test(gateway): exercise channel rollback through public boundary
1 parent 2097a2f commit c6c2268

254 files changed

Lines changed: 8206 additions & 6540 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎config/assertion-safety-baseline.txt‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ src/agents/prepared-model-runtime.configured.ts 2
19571957
src/agents/prepared-model-runtime.facts.ts 1
19581958
src/agents/prepared-model-runtime.ts 1
19591959
src/agents/provider-attribution.ts 14
1960-
src/agents/provider-auth-aliases.ts 2
1960+
src/agents/provider-auth-aliases.ts 1
19611961
src/agents/provider-http-errors.ts 1
19621962
src/agents/provider-local-service.ts 4
19631963
src/agents/provider-model-normalization.runtime.ts 1
@@ -2254,7 +2254,6 @@ src/channels/account-inspection.ts 2
22542254
src/channels/account-snapshot-fields.ts 1
22552255
src/channels/allowlist-match.ts 1
22562256
src/channels/allowlists/resolve-utils.ts 3
2257-
src/channels/bundled-channel-catalog-read.ts 1
22582257
src/channels/chat-meta-shared.ts 1
22592258
src/channels/direct-dm.ts 1
22602259
src/channels/draft-stream-loop.ts 1
@@ -3428,7 +3427,7 @@ src/plugin-sdk/dedupe-runtime.ts 1
34283427
src/plugin-sdk/discord.ts 3
34293428
src/plugin-sdk/extension-shared.ts 6
34303429
src/plugin-sdk/facade-activation-check.runtime.ts 3
3431-
src/plugin-sdk/facade-loader.ts 6
3430+
src/plugin-sdk/facade-loader.ts 4
34323431
src/plugin-sdk/facade-runtime.ts 6
34333432
src/plugin-sdk/file-lock.ts 10
34343433
src/plugin-sdk/inbound-reply-dispatch.ts 1
@@ -3540,7 +3539,7 @@ src/plugins/loader-channel-runtime.ts 1
35403539
src/plugins/loader-channel-setup.ts 17
35413540
src/plugins/loader-cli-registry.ts 2
35423541
src/plugins/loader-discovery.ts 1
3543-
src/plugins/loader-load-context.ts 5
3542+
src/plugins/loader-load-context.ts 4
35443543
src/plugins/loader-module-runtime.ts 13
35453544
src/plugins/loader-records.ts 1
35463545
src/plugins/loader-runtime-candidate.ts 1
@@ -3570,7 +3569,7 @@ src/plugins/plugin-metadata-snapshot.runtime.ts 2
35703569
src/plugins/plugin-module-loader-cache.ts 1
35713570
src/plugins/plugin-package-update.ts 1
35723571
src/plugins/plugin-peer-link.ts 6
3573-
src/plugins/plugin-sdk-native-resolver.ts 5
3572+
src/plugins/plugin-sdk-native-resolver.ts 3
35743573
src/plugins/prepared-message-tool-catalog.ts 1
35753574
src/plugins/provider-api-key-auth.ts 2
35763575
src/plugins/provider-auth-choice-helpers.ts 12
@@ -3587,7 +3586,7 @@ src/plugins/provider-runtime.ts 2
35873586
src/plugins/provider-self-hosted-setup.ts 1
35883587
src/plugins/provider-thinking-active.ts 1
35893588
src/plugins/providers.ts 1
3590-
src/plugins/public-surface-loader.ts 5
3589+
src/plugins/public-surface-loader.ts 3
35913590
src/plugins/recommended-tool-installs.ts 1
35923591
src/plugins/register-plugin-cli-command-groups.ts 1
35933592
src/plugins/registry-api.ts 2
@@ -3612,7 +3611,7 @@ src/plugins/runtime/runtime-llm-isolated.ts 4
36123611
src/plugins/runtime/runtime-llm.runtime.ts 5
36133612
src/plugins/runtime/runtime-plugin-boundary.ts 2
36143613
src/plugins/runtime/runtime-taskflow.ts 1
3615-
src/plugins/runtime/runtime-web-channel-plugin.ts 4
3614+
src/plugins/runtime/runtime-web-channel-plugin.ts 3
36163615
src/plugins/schema-validator.ts 6
36173616
src/plugins/sdk-alias.ts 3
36183617
src/plugins/services.ts 1

‎config/env-var-count-budget.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# Ratchet: lower this number when cleanup removes names; never raise it without owner approval.
33
# Worker bundle installation no longer injects OPENCLAW_STATE_DIR into a remote npm process.
44
# The internal Node-version check no longer contributes a false OPENCLAW_* name.
5-
501
5+
500

‎docs/gateway/configuration.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,13 @@ stopped accounts; use an explicit channel start to resume those accounts.
576576
`gateway.reload` and `gateway.remote` are exceptions under `gateway.*` - changing them does **not** trigger a restart. Individual plugins can also override this table: a loaded plugin may declare its own restart-triggering config prefixes (for example the bundled Canvas plugin restarts the Gateway for `plugins.enabled`, `plugins.allow`, and `plugins.deny`, not just its own `plugins.entries.canvas`), so the actual behavior depends on which plugins are active.
577577
</Note>
578578

579+
Plugin hot reload uses the package metadata discovered at Gateway startup.
580+
Enablement, plugin config, and account changes do not rescan plugin files.
581+
Install, update, uninstall, and explicit plugin metadata refresh require a
582+
Gateway restart; `hybrid` schedules that restart, while `off` leaves it to you.
583+
Changing an agent's workspace also does not discover plugins in the new
584+
directory until restart. See [Plugin metadata snapshots](/plugins/architecture#plugin-metadata-snapshot-and-lookup-table).
585+
579586
### Reload planning
580587

581588
When you edit a source file that is referenced through `$include`, OpenClaw plans

‎docs/plugins/architecture-internals.md‎

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -114,54 +114,48 @@ metadata-only while the setup module contributes other setup hooks.
114114

115115
### Plugin cache boundary
116116

117-
OpenClaw does not cache plugin discovery results or direct manifest registry
118-
data behind wall-clock windows. Installs, manifest edits, and load-path changes
119-
must become visible on the next explicit metadata read or snapshot rebuild.
120-
The manifest file parser keeps a bounded file-signature cache keyed by the
121-
opened manifest path plus device/inode, size, and mtime/ctime; that cache only
122-
avoids re-parsing unchanged bytes and must not cache discovery, registry,
123-
owner, or policy answers.
124-
125-
The safe metadata fast path is explicit object ownership, not a hidden cache.
126-
Gateway startup hot paths should pass the current `PluginMetadataSnapshot`, the
127-
derived `PluginLookUpTable`, or an explicit manifest registry through the call
128-
chain. Config validation, startup auto-enable, plugin bootstrap, and provider
129-
selection can reuse those objects while they represent the current config and
130-
plugin inventory. Setup lookup still reconstructs manifest metadata on demand
131-
unless the specific setup path receives an explicit manifest registry; keep
132-
that as a cold-path fallback rather than adding hidden lookup caches. When the
133-
input changes, rebuild and replace the snapshot instead of mutating it or
134-
keeping historical copies. Views over the active plugin registry and bundled
135-
channel bootstrap helpers should be recomputed from the current
136-
registry/root. Short-lived maps are fine inside one call to dedupe work or
137-
guard reentry; they must not become process metadata caches.
138-
139-
For plugin loading, the persistent cache layer is runtime loading. It may reuse
140-
loader state when code or installed artifacts are actually loaded, such as:
141-
142-
- `PluginLoaderCacheState` and compatible active runtime registries
143-
- jiti/module caches and public-surface loader caches used to avoid importing
144-
the same runtime surface repeatedly
145-
- filesystem caches for installed plugin artifacts
146-
- short-lived per-call maps for path normalization or duplicate resolution
147-
148-
Those caches are data-plane implementation details. They must not answer
149-
control-plane questions such as "which plugin owns this provider?" unless the
150-
caller deliberately asked for runtime loading.
151-
152-
Do not add persistent or wall-clock caches for:
153-
154-
- discovery results
155-
- direct manifest registries
156-
- manifest registries reconstructed from the installed plugin index
157-
- provider owner lookup, model suppression, provider policy, or public-artifact
158-
metadata
159-
- any other manifest-derived answer where a changed manifest, installed index,
160-
or load path should be visible on the next metadata read
161-
162-
Callers that rebuild manifest metadata from the persisted installed plugin
163-
index reconstruct that registry on demand. The installed index is durable
164-
source-plane state; it is not a hidden in-process metadata cache.
117+
One `PluginCache` owns plugin facts from first access until Gateway shutdown.
118+
CLI preflight and startup progressively fill the same cache; later access fills
119+
only facts not yet acquired. Its immutable metadata snapshot combines the installed index, manifests, owner maps, and available
120+
discovery facts from every configured agent workspace. Disabled plugins remain
121+
in the inventory so later enablement does not require discovery. Conflicting
122+
plugin IDs from different workspace sources remain rejected.
123+
124+
Runtime readers use this `PluginMetadataSnapshot`, a derived `PluginLookUpTable`,
125+
or an explicit manifest registry. Plugin scopes are in-memory projections;
126+
config changes, account changes, and run workspace changes must not trigger
127+
filesystem scanning, `stat`/`realpath` freshness polling, manifest rereads, or
128+
hashing. Activation and runtime service generations can change while their
129+
package metadata stays fixed. Account health and authentication state are not
130+
part of the immutable package inventory.
131+
132+
Explicit install, update, registry refresh, and doctor operations use isolated
133+
generations of the same cache type, acquired after their lifecycle lease. They may inspect changed files and rebuild the persisted
134+
installed index, but cannot clear or replace the running Gateway's inventory.
135+
The new inventory takes effect after restart. The `plugins.refresh` RPC reports
136+
`restartRequired: true`; with reload disabled, it leaves the running inventory
137+
in place until a manual restart.
138+
139+
The shared cache owns checked file contents, parsed package and manifest data,
140+
bundle MCP/LSP/settings files, plugin skill paths, discovery paths, installed-index
141+
projections, compiled model policies, SDK aliases, artifact locations, and lazy
142+
module exports. Missing files and artifacts are facts too: they remain
143+
missing until a new generation. Discovery, registry assembly, and index hashing
144+
reuse the same checked bytes rather than reopening a file at each stage.
145+
146+
Actual code imports retain their boundary and file-identity checks before first
147+
execution. Consent checks use a fresh inspection after an awaited approval so
148+
changed artifacts cannot inherit approval for older capabilities. Failed module
149+
evaluation remains retryable; a successful import is shared across consumers.
150+
151+
Registered services, hooks, tools, session MCP overlays, generated skill-link
152+
publication, and activation state remain runtime-owned.
153+
An active registry pins its chosen artifact binding so source and built modules
154+
cannot split its registrations. Native ESM module lifetime still follows Node's
155+
module loader. Manifest-derived questions such as "which plugin owns this
156+
provider?" use the metadata snapshot without executing plugin code. The persisted
157+
installed index belongs to management and startup; it is not a freshness signal
158+
for runtime readers.
165159

166160
## Registry model
167161

‎docs/plugins/architecture.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ That split lets OpenClaw validate config, explain missing/disabled plugins, and
138138

139139
### Plugin metadata snapshot and lookup table
140140

141-
Gateway startup builds one `PluginMetadataSnapshot` for the current config snapshot. The snapshot is metadata-only: it stores the installed plugin index, manifest registry, manifest diagnostics, owner maps, a plugin id normalizer, and manifest records. It does not hold loaded plugin modules, provider SDKs, package contents, or runtime exports.
141+
One `PluginCache` starts on the first plugin metadata access, including CLI preflight before Gateway startup, and fills progressively as metadata and artifacts are needed. Gateway startup retains that owner and builds its immutable `PluginMetadataSnapshot`. The snapshot includes plugin metadata from all configured agent workspaces, including disabled plugins, with source precedence and workspace provenance preserved. It stores the installed plugin index, manifest registry, manifest diagnostics, owner maps, and a plugin id normalizer. Package contents and lazily loaded module exports belong to other typed views of the same cache, not the snapshot itself.
142142

143143
Plugin-aware config validation, startup auto-enable, and Gateway plugin bootstrap consume that snapshot instead of rebuilding manifest/index metadata independently. `PluginLookUpTable` is derived from the same snapshot and adds the startup plugin plan for the current runtime config.
144144

145-
After startup, Gateway keeps the current metadata snapshot as a replaceable runtime product. Repeated runtime provider discovery can borrow that snapshot instead of reconstructing the installed index and manifest registry for each provider-catalog pass. The snapshot is cleared or replaced on Gateway shutdown, config/plugin inventory changes, and installed index writes; callers fall back to the cold manifest/index path when no compatible current snapshot exists. Compatibility checks must include plugin discovery roots such as `plugins.load.paths` and the default agent workspace, because workspace plugins are part of the metadata scope.
145+
After startup, runtime readers reuse that inventory without filesystem discovery, manifest rereads, or freshness checks. Narrow plugin selections are in-memory views of the same inventory. Changing config, account state, or an agent's run workspace does not invalidate it. Plugin installs, updates, removals, manifest edits, and discovery-root changes become visible to the runtime after a Gateway restart.
146146

147147
The snapshot and lookup table keep repeated startup decisions on the fast path:
148148

@@ -154,11 +154,11 @@ The snapshot and lookup table keep repeated startup decisions on the fast path:
154154
- plugin config schema and channel config schema validation
155155
- startup auto-enable decisions
156156

157-
The safety boundary is snapshot replacement, not mutation. Rebuild the snapshot when config, plugin inventory, install records, or persisted index policy changes. Do not treat it as a broad mutable global registry, and do not keep unbounded historical snapshots. Runtime plugin loading remains separate from metadata snapshots so stale runtime state cannot be hidden behind a metadata cache.
157+
Activation policy and runtime bindings have a separate lifetime. Hot reload can recompute enablement, replace plugin services, and refresh account state using current config against the fixed startup inventory. Plugin runtime imports remain lazy; retaining metadata does not activate every discovered plugin.
158158

159-
The cache rule is documented in [Plugin architecture internals](/plugins/architecture-internals#plugin-cache-boundary): manifest and discovery metadata are fresh unless a caller holds an explicit snapshot, lookup table, or manifest registry for the current flow. Hidden metadata caches and wall-clock TTLs are not part of plugin loading. Only runtime loader, module, and dependency-artifact caches may persist after code or installed artifacts are actually loaded.
159+
The cache rule is documented in [Plugin architecture internals](/plugins/architecture-internals#plugin-cache-boundary): Gateway retains one cache generation, while explicit management operations use isolated generations of the same cache. There are no wall-clock TTLs for Gateway metadata.
160160

161-
Some cold-path callers still reconstruct manifest registries directly from the persisted installed plugin index instead of receiving a Gateway `PluginLookUpTable`. That path now reconstructs the registry on demand; prefer passing the current lookup table or an explicit manifest registry through runtime flows when a caller already has one.
161+
Install, update, registry refresh, and doctor flows may read fresh package metadata to validate their changes. Their snapshots and installed-index writes do not replace the running Gateway's inventory. Runtime flows must use the startup snapshot or its lookup table instead of falling back to those cold management paths.
162162

163163
### Activation planning
164164

‎docs/plugins/manage-plugins.md‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ that explicit trust by adding the selected plugin to an existing restrictive
5151
`plugins.allow` list. An explicit `plugins.deny` entry remains authoritative and
5252
must be removed before enabling the plugin.
5353

54-
Installing or removing plugin code requires a Gateway restart. Enablement
55-
changes can be applied without a restart when the installed plugin and current
56-
Gateway runtime support it; otherwise the UI tells you a restart is required.
54+
Installing, updating, or removing plugin code requires a Gateway restart.
55+
Enablement changes for plugins in the startup inventory can be applied without
56+
a restart when the plugin and current Gateway runtime support it; otherwise
57+
the UI tells you a restart is required.
5758
OAuth-backed MCP connectors still need a one-time `openclaw mcp login <name>`
5859
from the CLI after they are added.
5960

@@ -234,6 +235,17 @@ openclaw gateway restart
234235
openclaw plugins inspect <plugin-id> --runtime --json
235236
```
236237

238+
The Gateway keeps the plugin inventory it discovered at startup. Management
239+
commands can inspect a newly installed package before restart, but that does
240+
not make its code or metadata available to the running Gateway. Manifest edits
241+
and plugins added to an agent workspace also require a restart. Ordinary config,
242+
enablement, and account changes can still hot-apply against the existing
243+
inventory.
244+
245+
For API clients, `plugins.refresh` reports `restartRequired: true` and requests
246+
a restart through config reload. With `gateway.reload.mode: "off"`, restart
247+
manually; refresh does not rescan or replace the running inventory.
248+
237249
`inspect --runtime` loads the plugin module and proves it registered runtime
238250
surfaces (tools, hooks, services, Gateway methods, HTTP routes, plugin-owned
239251
CLI commands). Plain `inspect` and `list` are cold manifest/config/registry

0 commit comments

Comments
 (0)