Skip to content

Commit 3a8382f

Browse files
committed
perf(plugins): consolidate provider discovery planning
1 parent 1ab7b75 commit 3a8382f

3 files changed

Lines changed: 131 additions & 151 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Docs: https://docs.openclaw.ai
7070

7171
### Fixes
7272

73+
- **Provider discovery:** prepare manifest catalogs once and recover discarded discovery entries through the existing scoped loader, so a static catalog cannot hide another selected or credentialed provider after an entry fails.
7374
- **Image analysis startup:** prepare selected model providers and keep configuration reads independent of full runtime loading, avoiding unrelated discovery and redundant model resolution while preserving credentials and runtime cleanup.
7475
- **Browser extension relay:** route Runtime binding callbacks only to registered clients and preserve shared bindings during client cleanup, preventing raw callback payloads from reaching unrelated Playwright sessions.
7576
- **Control UI media playback:** restore inline audio and video rendition loading so readiness checks reach the Gateway instead of silently falling back to download cards. (#132832)

‎src/plugins/provider-discovery.runtime.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,64 @@ describe("resolvePluginDiscoveryProvidersRuntime", () => {
885885
},
886886
);
887887

888+
it.each(
889+
[
890+
{ scope: "selected", expectedFullIds: ["broken", "healthy"] },
891+
{ scope: "unscoped", expectedFullIds: ["healthy"] },
892+
{ scope: "entries-only", expectedFullIds: [] },
893+
].flatMap((scenario) =>
894+
["first", "last"].map((failurePosition) => ({ ...scenario, failurePosition })),
895+
),
896+
)(
897+
"recovers discarded discovery entries with $scope scope when failure is $failurePosition",
898+
({ scope, expectedFullIds, failurePosition }) => {
899+
const healthy = {
900+
...createManifestPlugin("healthy"),
901+
setup: { providers: [{ id: "healthy", envVars: ["HEALTHY_API_KEY"] }] },
902+
};
903+
const broken = createManifestPlugin("broken");
904+
mocks.resolveDiscoveredProviderPluginIds.mockReturnValue(["static", "healthy", "broken"]);
905+
mocks.loadPluginMetadataSnapshot.mockReturnValue({
906+
index: { plugins: [] },
907+
manifestRegistry: {
908+
plugins: [
909+
createManifestPluginWithModelCatalog("static"),
910+
...(failurePosition === "first" ? [broken, healthy] : [healthy, broken]),
911+
],
912+
diagnostics: [],
913+
},
914+
});
915+
mocks.loadSource.mockImplementation((modulePath: string) => {
916+
if (modulePath === broken.providerDiscoverySource) {
917+
throw new Error("discovery entry unavailable");
918+
}
919+
return { ...createProvider({ id: "healthy", mode: "catalog" }), label: "entry" };
920+
});
921+
const fullProviders = expectedFullIds.map((id) => ({
922+
...createProvider({ id, mode: "catalog" }),
923+
label: `full:${id}`,
924+
}));
925+
mocks.resolvePluginProvidersCore.mockReturnValue(fullProviders);
926+
927+
const providers = resolvePluginDiscoveryProvidersRuntime({
928+
env: { HEALTHY_API_KEY: "catalog-test-key" },
929+
...(scope === "unscoped" ? {} : { onlyPluginIds: ["static", "healthy", "broken"] }),
930+
discoveryEntriesOnly: scope === "entries-only",
931+
});
932+
933+
expect(providers.map(({ id, label }) => [id, label])).toEqual([
934+
["static", "static"],
935+
...fullProviders.map(({ id, label }) => [id, label]),
936+
]);
937+
if (expectedFullIds.length === 0) {
938+
expect(mocks.resolvePluginProvidersCore).not.toHaveBeenCalled();
939+
} else {
940+
expect(mocks.resolvePluginProvidersCore).toHaveBeenCalledOnce();
941+
expect(requireResolvePluginProvidersParams().onlyPluginIds).toEqual(expectedFullIds);
942+
}
943+
},
944+
);
945+
888946
it("does not fall back to full plugin loading when discovery entries are requested only", () => {
889947
mocks.loadPluginMetadataSnapshot.mockReturnValue({
890948
index: { plugins: [] },

‎src/plugins/provider-discovery.runtime.ts‎

Lines changed: 72 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ type ProviderDiscoveryEntryResult = {
2828
complete: boolean;
2929
pluginRecords: PluginManifestRecord[];
3030
entryPluginIds: Set<string>;
31-
manifestEntryPluginIds: Set<string>;
3231
runtimeManifestCatalogPluginIds: Set<string>;
3332
};
3433

@@ -156,22 +155,40 @@ function providerConfigFromManifestRows(
156155
};
157156
}
158157

159-
function resolveManifestModelCatalogProviders(
158+
function prepareManifestCatalogDiscovery(
160159
pluginRecords: readonly PluginManifestRecord[],
161160
config: OpenClawConfig,
162-
): ProviderPlugin[] {
161+
includeProviders: boolean,
162+
): Pick<ProviderDiscoveryEntryResult, "providers" | "runtimeManifestCatalogPluginIds"> {
163163
const providers: ProviderPlugin[] = [];
164+
const runtimeManifestCatalogPluginIds = new Set<string>();
164165
for (const plugin of pluginRecords) {
165-
if (!plugin.modelCatalog?.providers) {
166+
if (!plugin.modelCatalog) {
166167
continue;
167168
}
169+
const ownedProviders = new Set(
170+
plugin.providers.map((provider) => normalizeProviderId(provider)),
171+
);
172+
if (
173+
Object.entries(plugin.modelCatalog.discovery ?? {}).some(
174+
([provider, discovery]) =>
175+
(discovery === "runtime" || discovery === "refreshable") &&
176+
ownedProviders.has(normalizeProviderId(provider)),
177+
)
178+
) {
179+
runtimeManifestCatalogPluginIds.add(plugin.id);
180+
}
181+
if (!plugin.modelCatalog.providers) {
182+
continue;
183+
}
184+
// Static rows and runtime coverage must come from the same effective catalog.
168185
const plan = planEffectiveModelCatalogRows({ registry: { plugins: [plugin] }, config });
169186
for (const entry of plan.entries) {
170-
if (
171-
entry.rows.length === 0 ||
172-
entry.discovery === "runtime" ||
173-
entry.discovery === "refreshable"
174-
) {
187+
if (entry.discovery === "runtime" || entry.discovery === "refreshable") {
188+
runtimeManifestCatalogPluginIds.add(plugin.id);
189+
continue;
190+
}
191+
if (!includeProviders || entry.rows.length === 0) {
175192
continue;
176193
}
177194
const providerConfig = providerConfigFromManifestRows(entry.rows);
@@ -190,40 +207,7 @@ function resolveManifestModelCatalogProviders(
190207
});
191208
}
192209
}
193-
return providers;
194-
}
195-
196-
function resolveRuntimeManifestCatalogPluginIds(
197-
pluginRecords: readonly PluginManifestRecord[],
198-
config: OpenClawConfig,
199-
): Set<string> {
200-
const pluginIds = new Set<string>();
201-
for (const plugin of pluginRecords) {
202-
const ownedProviders = new Set(
203-
plugin.providers.map((provider) => normalizeProviderId(provider)),
204-
);
205-
const ownsRuntimeDiscovery = Object.entries(plugin.modelCatalog?.discovery ?? {}).some(
206-
([provider, discovery]) =>
207-
(discovery === "runtime" || discovery === "refreshable") &&
208-
ownedProviders.has(normalizeProviderId(provider)),
209-
);
210-
if (ownsRuntimeDiscovery) {
211-
pluginIds.add(plugin.id);
212-
}
213-
214-
if (!plugin.modelCatalog?.providers) {
215-
continue;
216-
}
217-
const plan = planEffectiveModelCatalogRows({ registry: { plugins: [plugin] }, config });
218-
if (
219-
plan.entries.some(
220-
(entry) => entry.discovery === "runtime" || entry.discovery === "refreshable",
221-
)
222-
) {
223-
pluginIds.add(plugin.id);
224-
}
225-
}
226-
return pluginIds;
210+
return { providers, runtimeManifestCatalogPluginIds };
227211
}
228212

229213
function resolveProviderDiscoveryEntryPlugins(params: {
@@ -253,17 +237,14 @@ function resolveProviderDiscoveryEntryPlugins(params: {
253237
});
254238
const pluginIdSet = new Set(pluginIds);
255239
const pluginRecords = manifestRegistry.plugins.filter((plugin) => pluginIdSet.has(plugin.id));
256-
const config = params.config ?? {};
257-
const runtimeManifestCatalogPluginIds = resolveRuntimeManifestCatalogPluginIds(
258-
pluginRecords,
259-
config,
260-
);
240+
const { providers: manifestProviders, runtimeManifestCatalogPluginIds } =
241+
prepareManifestCatalogDiscovery(
242+
pluginRecords,
243+
params.config ?? {},
244+
params.includeManifestModelCatalogProviders !== false,
245+
);
261246
const entryRecords = pluginRecords.filter((plugin) => plugin.providerDiscoverySource);
262247
const entryPluginIds = new Set(entryRecords.map((plugin) => plugin.id));
263-
const manifestProviders =
264-
params.includeManifestModelCatalogProviders === false
265-
? []
266-
: resolveManifestModelCatalogProviders(pluginRecords, config);
267248
const manifestEntryPluginIds = new Set<string>();
268249
for (const pluginId of manifestProviders.map((provider) => provider.pluginId)) {
269250
if (pluginId) {
@@ -276,30 +257,23 @@ function resolveProviderDiscoveryEntryPlugins(params: {
276257
}
277258
}
278259
const complete = entryPluginIds.size === pluginIdSet.size;
260+
const result = {
261+
providers: manifestProviders,
262+
complete,
263+
pluginRecords,
264+
entryPluginIds,
265+
runtimeManifestCatalogPluginIds,
266+
};
279267
const entriesOnlyComplete =
280268
new Set([...entryPluginIds, ...manifestEntryPluginIds]).size === pluginIdSet.size;
281269
if (entryRecords.length === 0) {
282-
return {
283-
providers: manifestProviders,
284-
complete,
285-
pluginRecords,
286-
entryPluginIds,
287-
manifestEntryPluginIds,
288-
runtimeManifestCatalogPluginIds,
289-
};
270+
return result;
290271
}
291272
if (
292273
params.requireCompleteDiscoveryEntryCoverage &&
293274
!(params.discoveryEntriesOnly === true ? entriesOnlyComplete : complete)
294275
) {
295-
return {
296-
providers: [],
297-
complete: false,
298-
pluginRecords,
299-
entryPluginIds,
300-
manifestEntryPluginIds,
301-
runtimeManifestCatalogPluginIds,
302-
};
276+
return { ...result, providers: [], complete: false };
303277
}
304278
const providers: ProviderPlugin[] = [];
305279
for (const manifest of entryRecords) {
@@ -315,51 +289,12 @@ function resolveProviderDiscoveryEntryPlugins(params: {
315289
),
316290
);
317291
} catch {
318-
// Discovery fast path is optional. Fall back to the full plugin loader
319-
// below so existing plugin diagnostics/load behavior remains canonical.
320-
return {
321-
providers: manifestProviders,
322-
complete: false,
323-
pluginRecords,
324-
entryPluginIds,
325-
manifestEntryPluginIds,
326-
runtimeManifestCatalogPluginIds,
327-
};
292+
// Entry loading is all-or-nothing: discarded results no longer cover their owners.
293+
// Keep static manifest coverage for the scope-aware full-loader fallback.
294+
return { ...result, complete: false, entryPluginIds: manifestEntryPluginIds };
328295
}
329296
}
330-
return {
331-
providers: [...manifestProviders, ...providers],
332-
complete,
333-
pluginRecords,
334-
entryPluginIds,
335-
manifestEntryPluginIds,
336-
runtimeManifestCatalogPluginIds,
337-
};
338-
}
339-
340-
function resolveSelectiveFullPluginIds(params: {
341-
entryResult: ProviderDiscoveryEntryResult;
342-
env: NodeJS.ProcessEnv;
343-
}): string[] {
344-
const missingEntryCredentialPluginIds = params.entryResult.pluginRecords
345-
.filter((plugin) => !params.entryResult.entryPluginIds.has(plugin.id))
346-
.filter((plugin) => hasProviderAuthEnvCredential(plugin, params.env))
347-
.map((plugin) => plugin.id);
348-
const runtimeManifestCatalogPluginIds = listRuntimeManifestCatalogPluginIds(params.entryResult);
349-
return sortUniqueStrings([
350-
...missingEntryCredentialPluginIds,
351-
...runtimeManifestCatalogPluginIds,
352-
]);
353-
}
354-
355-
function listRuntimeManifestCatalogPluginIds(entryResult: ProviderDiscoveryEntryResult): string[] {
356-
return [...entryResult.runtimeManifestCatalogPluginIds];
357-
}
358-
359-
function resolveMissingEntryPluginIds(entryResult: ProviderDiscoveryEntryResult): string[] {
360-
return entryResult.pluginRecords
361-
.filter((plugin) => !entryResult.entryPluginIds.has(plugin.id))
362-
.map((plugin) => plugin.id);
297+
return { ...result, providers: [...manifestProviders, ...providers] };
363298
}
364299

365300
function resolveRuntimeEntryProviders(entryResult: ProviderDiscoveryEntryResult): ProviderPlugin[] {
@@ -375,17 +310,6 @@ function resolveRuntimeEntryProviders(entryResult: ProviderDiscoveryEntryResult)
375310
});
376311
}
377312

378-
function withoutFullLoadedPluginEntries(
379-
providers: ProviderPlugin[],
380-
pluginIds: readonly string[],
381-
): ProviderPlugin[] {
382-
if (pluginIds.length === 0) {
383-
return providers;
384-
}
385-
const pluginIdSet = new Set(pluginIds);
386-
return providers.filter((provider) => !provider.pluginId || !pluginIdSet.has(provider.pluginId));
387-
}
388-
389313
export function resolvePluginDiscoveryProvidersRuntime(params: {
390314
config?: OpenClawConfig;
391315
workspaceDir?: string;
@@ -418,45 +342,42 @@ export function resolvePluginDiscoveryProvidersRuntime(params: {
418342
) {
419343
return runtimeEntryProviders;
420344
}
345+
let fullPluginIds = params.onlyPluginIds;
346+
let retainedProviders: ProviderPlugin[] | undefined;
421347
if (runtimeEntryProviders.length > 0 || entryResult.runtimeManifestCatalogPluginIds.size > 0) {
422348
// Runtime manifest owners do not cover siblings without discovery entries.
423349
// Preserve the selected scope; unscoped discovery stays credential-bounded.
424-
const fullPluginIds =
425-
params.onlyPluginIds === undefined
426-
? resolveSelectiveFullPluginIds({ entryResult, env })
427-
: sortUniqueStrings([
428-
...resolveMissingEntryPluginIds(entryResult),
429-
...listRuntimeManifestCatalogPluginIds(entryResult),
430-
]);
431-
const fullProviders =
432-
fullPluginIds.length > 0
433-
? resolvePluginProvidersCore({
434-
...params,
435-
env,
436-
onlyPluginIds: fullPluginIds,
437-
})
438-
: [];
439-
return [
440-
...withoutFullLoadedPluginEntries(runtimeEntryProviders, fullPluginIds),
441-
...fullProviders,
442-
];
443-
}
444-
if (entryProviders.length > 0) {
445-
const fullPluginIds = sortUniqueStrings(
350+
fullPluginIds = sortUniqueStrings([
351+
...entryResult.pluginRecords
352+
.filter(
353+
(plugin) =>
354+
!entryResult.entryPluginIds.has(plugin.id) &&
355+
(params.onlyPluginIds !== undefined || hasProviderAuthEnvCredential(plugin, env)),
356+
)
357+
.map((plugin) => plugin.id),
358+
...entryResult.runtimeManifestCatalogPluginIds,
359+
]);
360+
if (fullPluginIds.length === 0) {
361+
return [...runtimeEntryProviders];
362+
}
363+
const fullPluginIdSet = new Set(fullPluginIds);
364+
retainedProviders = runtimeEntryProviders.filter(
365+
(provider) => !provider.pluginId || !fullPluginIdSet.has(provider.pluginId),
366+
);
367+
} else if (entryProviders.length > 0) {
368+
const entryPluginIds = sortUniqueStrings(
446369
entryProviders
447370
.map((provider) => provider.pluginId)
448371
.filter((pluginId): pluginId is string => typeof pluginId === "string" && pluginId !== ""),
449372
);
450-
if (fullPluginIds.length > 0) {
451-
return resolvePluginProvidersCore({
452-
...params,
453-
env,
454-
onlyPluginIds: fullPluginIds,
455-
});
373+
if (entryPluginIds.length > 0) {
374+
fullPluginIds = entryPluginIds;
456375
}
457376
}
458-
return resolvePluginProvidersCore({
377+
const fullProviders = resolvePluginProvidersCore({
459378
...params,
460379
env,
380+
...(fullPluginIds ? { onlyPluginIds: fullPluginIds } : {}),
461381
});
382+
return retainedProviders ? [...retainedProviders, ...fullProviders] : fullProviders;
462383
}

0 commit comments

Comments
 (0)