Skip to content

Commit f160425

Browse files
committed
perf(gateway): thread discovery into applyPluginAutoEnable call sites
Every gateway applyPluginAutoEnable call now passes the snapshot's PluginDiscoveryResult so the bundled-channel cascade (collectConfiguredChannelIds → listBundledChannelIdsWith* → listPotentialConfiguredChannelPresenceSignals) short-circuits instead of each leaf re-firing discovery. Startup-time sites pull discovery from the snapshot/lookup-table they already hold: - server-plugin-bootstrap.ts (pluginLookUpTable) - server-startup-plugins.ts (pluginMetadataSnapshot) - server-startup-config.ts (pluginMetadataSnapshot) - server-plugins.ts (pluginLookUpTable, both call sites) Per-RPC sites (server.impl getRuntimeConfig callback, server-methods/channels status + start handlers, server-methods/send) source discovery via getCurrentPluginMetadataSnapshot using the runtime config to validate compatibility. Falls through to the original slow path when the snapshot is absent or incompatible.
1 parent 67ee631 commit f160425

7 files changed

Lines changed: 44 additions & 7 deletions

File tree

src/gateway/server-methods/channels.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { readConfigFileSnapshot } from "../../config/config.js";
1313
import { applyPluginAutoEnable } from "../../config/plugin-auto-enable.js";
1414
import type { OpenClawConfig } from "../../config/types.openclaw.js";
1515
import { getChannelActivity } from "../../infra/channel-activity.js";
16+
import { getCurrentPluginMetadataSnapshot } from "../../plugins/current-plugin-metadata-snapshot.js";
1617
import { DEFAULT_ACCOUNT_ID } from "../../routing/session-key.js";
1718
import { defaultRuntime } from "../../runtime.js";
1819
import { normalizeOptionalString } from "../../shared/string-coerce.js";
@@ -301,9 +302,16 @@ export const channelsHandlers: GatewayRequestHandlers = {
301302
const rawChannel = (params as { channel?: unknown }).channel;
302303
const requestedChannel =
303304
typeof rawChannel === "string" ? normalizeChannelId(rawChannel) : undefined;
305+
const runtimeConfig = context.getRuntimeConfig();
306+
const currentSnapshot = getCurrentPluginMetadataSnapshot({
307+
config: runtimeConfig,
308+
env: process.env,
309+
});
304310
const cfg = applyPluginAutoEnable({
305-
config: context.getRuntimeConfig(),
311+
config: runtimeConfig,
306312
env: process.env,
313+
manifestRegistry: currentSnapshot?.manifestRegistry,
314+
discovery: currentSnapshot?.discovery,
307315
}).config;
308316
const runtime = context.getRuntimeSnapshot();
309317
const plugins = listChannelPlugins();
@@ -579,9 +587,16 @@ export const channelsHandlers: GatewayRequestHandlers = {
579587
return;
580588
}
581589
try {
590+
const runtimeConfig = context.getRuntimeConfig();
591+
const currentSnapshot = getCurrentPluginMetadataSnapshot({
592+
config: runtimeConfig,
593+
env: process.env,
594+
});
582595
const cfg = applyPluginAutoEnable({
583-
config: context.getRuntimeConfig(),
596+
config: runtimeConfig,
584597
env: process.env,
598+
manifestRegistry: currentSnapshot?.manifestRegistry,
599+
discovery: currentSnapshot?.discovery,
585600
}).config;
586601
const payload = await startChannelAccount({
587602
channelId,

src/gateway/server-methods/send.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { maybeResolveIdLikeTarget } from "../../infra/outbound/target-resolver.j
2121
import { resolveOutboundTarget } from "../../infra/outbound/targets.js";
2222
import { extractToolPayload } from "../../infra/outbound/tool-payload.js";
2323
import { getAgentScopedMediaLocalRoots } from "../../media/local-roots.js";
24+
import { getCurrentPluginMetadataSnapshot } from "../../plugins/current-plugin-metadata-snapshot.js";
2425
import { normalizePollInput } from "../../polls.js";
2526
import { parseThreadSessionSuffix } from "../../sessions/session-key-utils.js";
2627
import {
@@ -131,9 +132,16 @@ async function resolveRequestedChannel(params: {
131132
error: errorShape(ErrorCodes.INVALID_REQUEST, params.unsupportedMessage(channelInput)),
132133
};
133134
}
135+
const runtimeConfig = params.context.getRuntimeConfig();
136+
const currentSnapshot = getCurrentPluginMetadataSnapshot({
137+
config: runtimeConfig,
138+
env: process.env,
139+
});
134140
const cfg = applyPluginAutoEnable({
135-
config: params.context.getRuntimeConfig(),
141+
config: runtimeConfig,
136142
env: process.env,
143+
manifestRegistry: currentSnapshot?.manifestRegistry,
144+
discovery: currentSnapshot?.discovery,
137145
}).config;
138146
let channel = normalizedChannel;
139147
if (!channel) {

src/gateway/server-plugin-bootstrap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export function prepareGatewayPluginLoad(params: GatewayPluginBootstrapParams) {
8383
...(params.pluginLookUpTable?.manifestRegistry
8484
? { manifestRegistry: params.pluginLookUpTable.manifestRegistry }
8585
: {}),
86+
discovery: params.pluginLookUpTable?.discovery,
8687
});
8788
const resolvedConfig =
8889
activationSourceConfig === params.cfg

src/gateway/server-plugins.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ export function loadGatewayPlugins(params: {
636636
...(params.pluginLookUpTable?.manifestRegistry
637637
? { manifestRegistry: params.pluginLookUpTable.manifestRegistry }
638638
: {}),
639+
discovery: params.pluginLookUpTable?.discovery,
639640
})
640641
: undefined;
641642
const autoEnableMs = performance.now() - started;
@@ -659,6 +660,7 @@ export function loadGatewayPlugins(params: {
659660
...(params.pluginLookUpTable?.manifestRegistry
660661
? { manifestRegistry: params.pluginLookUpTable.manifestRegistry }
661662
: {}),
663+
discovery: params.pluginLookUpTable?.discovery,
662664
});
663665
const resolvedConfigMs = performance.now() - started;
664666
const resolvedConfig = autoEnabled.config;

src/gateway/server-startup-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export async function loadGatewayStartupConfigSnapshot(params: {
125125
...(pluginMetadataSnapshot?.manifestRegistry
126126
? { manifestRegistry: pluginMetadataSnapshot.manifestRegistry }
127127
: {}),
128+
discovery: pluginMetadataSnapshot?.discovery,
128129
}),
129130
);
130131
if (autoEnable.changes.length === 0) {

src/gateway/server-startup-plugins.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export async function prepareGatewayPluginBootstrap(params: {
8787
...(params.pluginMetadataSnapshot?.manifestRegistry
8888
? { manifestRegistry: params.pluginMetadataSnapshot.manifestRegistry }
8989
: {}),
90+
discovery: params.pluginMetadataSnapshot?.discovery,
9091
}).config,
9192
});
9293
const pluginsGloballyDisabled = gatewayPluginConfig.plugins?.enabled === false;

src/gateway/server.impl.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { startDiagnosticHeartbeat, stopDiagnosticHeartbeat } from "../logging/di
4141
import { createSubsystemLogger, runtimeForLogger } from "../logging/subsystem.js";
4242
import {
4343
clearCurrentPluginMetadataSnapshot,
44+
getCurrentPluginMetadataSnapshot,
4445
setCurrentPluginMetadataSnapshot,
4546
} from "../plugins/current-plugin-metadata-snapshot.js";
4647
import type { PluginHookGatewayCronService } from "../plugins/hook-types.js";
@@ -836,11 +837,19 @@ export async function startGatewayServer(
836837
});
837838
const { createChannelManager } = await import("./server-channels.js");
838839
const channelManager = createChannelManager({
839-
getRuntimeConfig: () =>
840-
applyPluginAutoEnable({
841-
config: getRuntimeConfig(),
840+
getRuntimeConfig: () => {
841+
const runtimeConfig = getRuntimeConfig();
842+
const currentSnapshot = getCurrentPluginMetadataSnapshot({
843+
config: runtimeConfig,
842844
env: process.env,
843-
}).config,
845+
});
846+
return applyPluginAutoEnable({
847+
config: runtimeConfig,
848+
env: process.env,
849+
manifestRegistry: currentSnapshot?.manifestRegistry,
850+
discovery: currentSnapshot?.discovery,
851+
}).config;
852+
},
844853
channelLogs,
845854
channelRuntimeEnvs,
846855
resolveChannelRuntime: getChannelRuntime,

0 commit comments

Comments
 (0)