Skip to content

Commit e080fe2

Browse files
committed
perf(plugins): reuse lifecycle metadata on secrets hot paths
1 parent e20a329 commit e080fe2

19 files changed

Lines changed: 378 additions & 69 deletions

‎src/config/plugin-auto-enable.shared.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ export function resolveConfiguredPluginAutoEnableCandidates(params: {
779779
config: params.config,
780780
env: params.env,
781781
pluginIds: setupPluginIds,
782+
manifestRegistry: params.registry,
782783
})) {
783784
changes.push({
784785
pluginId: entry.pluginId,

‎src/gateway/server-reload-handlers.test.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5750,26 +5750,38 @@ describe("gateway plugin hot reload handlers", () => {
57505750
stopChannel,
57515751
reloadPlugins,
57525752
});
5753+
const sourceConfig: OpenClawConfig = {
5754+
plugins: { enabled: false, allow: ["discord"] },
5755+
};
57535756

57545757
try {
5755-
await applyHotReload(createPluginReloadPlan(), {
5756-
plugins: {
5757-
enabled: false,
5758+
await applyHotReload(
5759+
createPluginReloadPlan(),
5760+
{
5761+
plugins: {
5762+
enabled: false,
5763+
},
57585764
},
5759-
});
5765+
{
5766+
sourceConfig,
5767+
isCurrent: () => true,
5768+
publish: async (commit) => await commit(),
5769+
},
5770+
);
57605771
} finally {
57615772
restoreChannelReloadEnv();
57625773
}
57635774

57645775
const [reloadParams] = reloadPlugins.mock.calls.at(-1) ?? [];
57655776
const reloadParamsRecord = reloadParams as
5766-
| { nextConfig?: unknown; changedPaths?: unknown }
5777+
| { nextConfig?: unknown; sourceConfig?: unknown; changedPaths?: unknown }
57675778
| undefined;
57685779
expect(reloadParamsRecord?.nextConfig).toEqual({
57695780
plugins: {
57705781
enabled: false,
57715782
},
57725783
});
5784+
expect(reloadParamsRecord?.sourceConfig).toBe(sourceConfig);
57735785
expect(reloadParamsRecord?.changedPaths).toEqual(["plugins.enabled"]);
57745786
expect(stopChannel).toHaveBeenCalledWith("discord", undefined, { manual: false });
57755787
expect(startChannel).not.toHaveBeenCalled();

‎src/plugins/bundled-manifest-contract-plugins.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ export function resolveEnabledBundledManifestContractPlugins(params: {
3232
env?: NodeJS.ProcessEnv;
3333
onlyPluginIds?: readonly string[];
3434
contract: PluginManifestContractListKey;
35+
manifestRecords?: readonly PluginManifestRecord[];
3536
}): PluginManifestRecord[] {
3637
if (params.config?.plugins?.enabled === false) {
3738
return [];
3839
}
39-
let manifestRecords: readonly PluginManifestRecord[] | undefined;
40-
const loadManifestRecords = (config?: OpenClawConfig) => {
40+
let manifestRecords = params.manifestRecords;
41+
const loadManifestRecords = () => {
4142
manifestRecords ??= loadManifestContractSnapshot({
42-
config,
43+
config: params.config,
4344
workspaceDir: params.workspaceDir,
4445
env: params.env,
4546
}).plugins;
@@ -54,13 +55,13 @@ export function resolveEnabledBundledManifestContractPlugins(params: {
5455
applyAutoEnable: true,
5556
resolveBundledPluginIds: (compatParams) =>
5657
listBundledManifestContractPluginIds({
57-
plugins: loadManifestRecords(compatParams.config),
58+
plugins: loadManifestRecords(),
5859
contract: params.contract,
5960
onlyPluginIds: compatParams.onlyPluginIds,
6061
}),
6162
});
6263
const onlyPluginIdSet = createPluginIdScopeSet(params.onlyPluginIds);
63-
return loadManifestRecords(activation.config).filter((plugin) => {
64+
return loadManifestRecords().filter((plugin) => {
6465
if (
6566
plugin.origin !== "bundled" ||
6667
(onlyPluginIdSet && !onlyPluginIdSet.has(plugin.id)) ||

‎src/plugins/manifest-contract-eligibility.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ describe("loadManifestContractSnapshot", () => {
270270
expect(mocks.loadPluginMetadataSnapshot).not.toHaveBeenCalled();
271271
});
272272

273-
it("normalizes omitted config before checking unscoped snapshot compatibility", () => {
273+
it("preserves configless default-discovery snapshot compatibility", () => {
274274
const env = { HOME: "/home/default-config" } as NodeJS.ProcessEnv;
275275
const snapshot = {
276276
index: { plugins: [{ pluginId: "demo" }] },
@@ -284,12 +284,12 @@ describe("loadManifestContractSnapshot", () => {
284284
});
285285

286286
expect(mocks.resolvePluginMetadataSnapshot).toHaveBeenCalledWith({
287-
config: {},
287+
config: undefined,
288288
env,
289289
allowWorkspaceScopedCurrent: true,
290290
});
291291
expect(mocks.loadPluginMetadataSnapshot).toHaveBeenCalledWith({
292-
config: {},
292+
config: undefined,
293293
env,
294294
allowWorkspaceScopedCurrent: true,
295295
});

‎src/plugins/manifest-contract-eligibility.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,9 @@ export function loadManifestMetadataSnapshot(params: {
155155
workspaceDir?: string;
156156
env?: NodeJS.ProcessEnv;
157157
}): PluginMetadataSnapshot {
158-
const config = params.config ?? {};
159-
const env = params.env ?? process.env;
160158
return resolvePluginMetadataSnapshot({
161-
config,
162-
env,
159+
config: params.config,
160+
env: params.env ?? process.env,
163161
...(params.workspaceDir ? { workspaceDir: params.workspaceDir } : {}),
164162
allowWorkspaceScopedCurrent: params.workspaceDir === undefined,
165163
});

‎src/plugins/plugin-registry-contributions.current-snapshot.test.ts‎

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
55
import { setCurrentPluginMetadataSnapshot } from "./current-plugin-metadata-snapshot.js";
66
import { resolveInstalledPluginIndexPolicyHash } from "./installed-plugin-index-policy.js";
77
import type { InstalledPluginIndex } from "./installed-plugin-index.js";
8+
import { loadManifestMetadataSnapshot } from "./manifest-contract-eligibility.js";
89
import type { PluginManifestRecord } from "./manifest-registry.js";
910
import { clearPluginMetadataLifecycleCaches } from "./plugin-metadata-lifecycle.js";
1011
import type { PluginMetadataSnapshot } from "./plugin-metadata-snapshot.types.js";
11-
import { loadPluginManifestRegistryForPluginRegistry } from "./plugin-registry-contributions.js";
12+
import {
13+
loadPluginManifestRegistryForPluginRegistry,
14+
resolveManifestContractOwnerPluginId,
15+
resolveManifestContractPluginIds,
16+
} from "./plugin-registry-contributions.js";
1217
import { loadPluginRegistrySnapshotWithMetadata } from "./plugin-registry-snapshot.js";
1318

1419
afterEach(() => {
20+
vi.restoreAllMocks();
1521
clearPluginMetadataLifecycleCaches();
1622
});
1723

@@ -40,7 +46,7 @@ function createManifest(id: string): PluginManifestRecord {
4046
channels: [],
4147
channelConfigs: {},
4248
cliBackends: [],
43-
contracts: {},
49+
contracts: { webSearchProviders: [`${id}-search`] },
4450
} as unknown as PluginManifestRecord;
4551
}
4652

@@ -95,6 +101,74 @@ function createSnapshot(params: {
95101
}
96102

97103
describe("loadPluginManifestRegistryForPluginRegistry current snapshot", () => {
104+
it("reuses an allowlisted published snapshot for configless manifest reads", () => {
105+
const config: OpenClawConfig = { plugins: { allow: ["enabled"] } };
106+
const env = {
107+
HOME: "/tmp/openclaw-test-home",
108+
OPENCLAW_DISABLE_BUNDLED_PLUGINS: "1",
109+
};
110+
const workspaceDir = "/workspace";
111+
const snapshot = createSnapshot({ config, workspaceDir });
112+
setCurrentPluginMetadataSnapshot(snapshot, { config, env, workspaceDir });
113+
const readDirectory = vi.spyOn(fs, "readdirSync");
114+
const readFile = vi.spyOn(fs, "readFileSync");
115+
116+
expect(loadManifestMetadataSnapshot({ env })).toBe(snapshot);
117+
expect(readDirectory).not.toHaveBeenCalled();
118+
expect(readFile).not.toHaveBeenCalled();
119+
});
120+
121+
it("projects supplied contract manifests without dropping disabled owners", () => {
122+
const config: OpenClawConfig = { plugins: { allow: ["enabled"] } };
123+
const env = {
124+
HOME: "/tmp/openclaw-test-home",
125+
OPENCLAW_DISABLE_BUNDLED_PLUGINS: "1",
126+
};
127+
const workspaceDir = "/workspace";
128+
const snapshot = createSnapshot({ config, workspaceDir });
129+
setCurrentPluginMetadataSnapshot(snapshot, { config, env, workspaceDir });
130+
const readDirectory = vi.spyOn(fs, "readdirSync");
131+
const readFile = vi.spyOn(fs, "readFileSync");
132+
133+
expect(
134+
resolveManifestContractPluginIds({
135+
contract: "webSearchProviders",
136+
config,
137+
env,
138+
workspaceDir,
139+
}),
140+
).toEqual(["disabled", "enabled"]);
141+
expect(
142+
resolveManifestContractOwnerPluginId({
143+
contract: "webSearchProviders",
144+
value: " DISABLED-SEARCH ",
145+
config,
146+
env,
147+
workspaceDir,
148+
}),
149+
).toBe("disabled");
150+
expect(
151+
resolveManifestContractPluginIds({
152+
contract: "webSearchProviders",
153+
config: { plugins: { allow: ["incompatible-policy"] } },
154+
env,
155+
manifestRecords: snapshot.plugins,
156+
onlyPluginIds: ["disabled"],
157+
}),
158+
).toEqual(["disabled"]);
159+
expect(
160+
resolveManifestContractOwnerPluginId({
161+
contract: "webSearchProviders",
162+
value: "disabled-search",
163+
config: { plugins: { allow: ["incompatible-policy"] } },
164+
env,
165+
manifestRecords: snapshot.plugins,
166+
}),
167+
).toBe("disabled");
168+
expect(readDirectory).not.toHaveBeenCalled();
169+
expect(readFile).not.toHaveBeenCalled();
170+
});
171+
98172
it("reuses compatible current manifest metadata", () => {
99173
const config: OpenClawConfig = {};
100174
const env = {

‎src/plugins/plugin-registry-contributions.ts‎

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
PluginManifestRecord,
1414
PluginManifestRegistry,
1515
} from "./manifest-registry.js";
16+
import { resolvePluginMetadataSnapshot } from "./plugin-metadata-snapshot.js";
1617
import type { PluginMetadataSnapshot } from "./plugin-metadata-snapshot.types.js";
1718
import type { PluginOrigin } from "./plugin-origin.types.js";
1819
import {
@@ -62,13 +63,17 @@ type ListPluginContributionIdsParams = PluginRegistryContributionOptions & {
6263
contribution: PluginRegistryContributionKey;
6364
};
6465

65-
type ResolveManifestContractPluginIdsParams = LoadPluginRegistryParams & {
66+
type ManifestContractLookupParams = LoadPluginRegistryParams & {
67+
manifestRecords?: readonly PluginManifestRecord[];
68+
};
69+
70+
type ResolveManifestContractPluginIdsParams = ManifestContractLookupParams & {
6671
contract: PluginManifestContractListKey;
6772
origin?: PluginOrigin;
6873
onlyPluginIds?: readonly string[];
6974
};
7075

71-
type ResolveManifestContractOwnerPluginIdParams = LoadPluginRegistryParams & {
76+
type ResolveManifestContractOwnerPluginIdParams = ManifestContractLookupParams & {
7277
contract: PluginManifestContractListKey;
7378
value: string | undefined;
7479
origin?: PluginOrigin;
@@ -99,16 +104,44 @@ function listManifestContractValues(
99104
return plugin.contracts?.[contract] ?? [];
100105
}
101106

102-
function loadManifestContractRegistry(
103-
params: LoadPluginRegistryParams & {
107+
function loadManifestContractRecords(
108+
params: ManifestContractLookupParams & {
104109
onlyPluginIds?: readonly string[];
105110
},
106-
): PluginManifestRegistry {
107-
return loadPluginManifestRegistryForPluginRegistry({
108-
...params,
109-
pluginIds: params.onlyPluginIds,
110-
includeDisabled: true,
111-
});
111+
): readonly PluginManifestRecord[] {
112+
let records = params.manifestRecords;
113+
if (!records) {
114+
const requiresExplicitRegistry =
115+
params.index !== undefined ||
116+
params.preferPersisted === false ||
117+
params.allowCurrent === false ||
118+
params.stateDir !== undefined ||
119+
params.filePath !== undefined ||
120+
params.pluginIndexFilePath !== undefined ||
121+
params.installRecords !== undefined ||
122+
params.candidates !== undefined ||
123+
params.diagnostics !== undefined ||
124+
params.discovery !== undefined ||
125+
params.now !== undefined;
126+
if (requiresExplicitRegistry) {
127+
return loadPluginManifestRegistryForPluginRegistry({
128+
...params,
129+
pluginIds: params.onlyPluginIds,
130+
includeDisabled: true,
131+
}).plugins;
132+
}
133+
records = resolvePluginMetadataSnapshot({
134+
config: params.config,
135+
env: params.env,
136+
...(params.workspaceDir !== undefined ? { workspaceDir: params.workspaceDir } : {}),
137+
allowWorkspaceScopedCurrent: params.workspaceDir === undefined,
138+
}).plugins;
139+
}
140+
if (params.onlyPluginIds === undefined) {
141+
return records;
142+
}
143+
const pluginIds = new Set(params.onlyPluginIds);
144+
return records.filter((record) => pluginIds.has(record.id));
112145
}
113146

114147
function listManifestContributionIds(
@@ -308,8 +341,8 @@ export function resolvePluginContributionOwners(
308341
export function resolveManifestContractPluginIds(
309342
params: ResolveManifestContractPluginIdsParams,
310343
): string[] {
311-
return loadManifestContractRegistry(params)
312-
.plugins.filter(
344+
return loadManifestContractRecords(params)
345+
.filter(
313346
(plugin) =>
314347
(!params.origin || plugin.origin === params.origin) &&
315348
listManifestContractValues(plugin, params.contract).length > 0,
@@ -325,7 +358,7 @@ export function resolveManifestContractOwnerPluginId(
325358
if (!normalizedValue) {
326359
return undefined;
327360
}
328-
return loadManifestContractRegistry(params).plugins.find(
361+
return loadManifestContractRecords(params).find(
329362
(plugin) =>
330363
(!params.origin || plugin.origin === params.origin) &&
331364
listManifestContractValues(plugin, params.contract).some(

‎src/plugins/web-fetch-providers.runtime.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function resolveWebFetchCandidatePluginIds(params: {
2727
onlyPluginIds?: readonly string[];
2828
origin?: PluginManifestRecord["origin"];
2929
sandboxed?: boolean;
30+
manifestRecords?: readonly PluginManifestRecord[];
3031
}): string[] | undefined {
3132
return resolveManifestDeclaredWebProviderCandidatePluginIds({
3233
contract: "webFetchProviders",
@@ -37,6 +38,7 @@ function resolveWebFetchCandidatePluginIds(params: {
3738
onlyPluginIds: params.onlyPluginIds,
3839
origin: params.origin,
3940
sandboxed: params.sandboxed,
41+
manifestRecords: params.manifestRecords,
4042
});
4143
}
4244

@@ -62,6 +64,7 @@ export function resolvePluginWebFetchProviders(params: {
6264
mode?: "runtime" | "setup";
6365
origin?: PluginManifestRecord["origin"];
6466
sandboxed?: boolean;
67+
manifestRecords?: readonly PluginManifestRecord[];
6568
}): PluginWebFetchProviderEntry[] {
6669
return resolvePluginWebProviders(params, {
6770
resolveBundledResolutionConfig: resolveBundledWebFetchResolutionConfig,
@@ -80,6 +83,7 @@ export function resolveRuntimeWebFetchProviders(params: {
8083
env?: PluginLoadOptions["env"];
8184
onlyPluginIds?: readonly string[];
8285
origin?: PluginManifestRecord["origin"];
86+
manifestRecords?: readonly PluginManifestRecord[];
8387
}): PluginWebFetchProviderEntry[] {
8488
return resolveRuntimeWebProviders(params, {
8589
resolveBundledResolutionConfig: resolveBundledWebFetchResolutionConfig,

‎src/plugins/web-fetch-providers.shared.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Shares web fetch provider loading helpers across provider plugins.
22
import type { PluginLoadOptions } from "./loader.js";
3+
import type { PluginManifestRecord } from "./manifest-registry.js";
34
import type { PluginWebFetchProviderEntry } from "./types.js";
45
import {
56
resolveBundledWebProviderResolutionConfig,
@@ -23,15 +24,18 @@ export function resolveBundledWebFetchResolutionConfig(params: {
2324
config?: PluginLoadOptions["config"];
2425
workspaceDir?: string;
2526
env?: PluginLoadOptions["env"];
27+
manifestRecords?: readonly PluginManifestRecord[];
2628
}): {
2729
config: PluginLoadOptions["config"];
2830
activationSourceConfig?: PluginLoadOptions["config"];
2931
autoEnabledReasons: Record<string, string[]>;
32+
manifestRecords?: readonly PluginManifestRecord[];
3033
} {
3134
return resolveBundledWebProviderResolutionConfig({
3235
contract: "webFetchProviders",
3336
config: params.config,
3437
workspaceDir: params.workspaceDir,
3538
env: params.env,
39+
manifestRecords: params.manifestRecords,
3640
});
3741
}

0 commit comments

Comments
 (0)